De-dupe hashing code.

This commit is contained in:
Christian Gunderman
2026-02-02 23:56:40 -08:00
parent 849b3b255c
commit 3dce0fe6e5
4 changed files with 17 additions and 4 deletions
@@ -94,4 +94,13 @@ describe('AcknowledgedAgentsService', () => {
false,
);
});
it('should compute consistent hashes', () => {
const content = 'some content';
const hash = AcknowledgedAgentsService.computeHash(content);
expect(hash).toBe(AcknowledgedAgentsService.computeHash(content));
expect(hash).not.toBe(
AcknowledgedAgentsService.computeHash('other content'),
);
});
});
@@ -6,6 +6,7 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as crypto from 'node:crypto';
import { Storage } from '../config/storage.js';
import { debugLogger } from '../utils/debugLogger.js';
import { getErrorMessage, isNodeError } from '../utils/errors.js';
@@ -21,6 +22,10 @@ export class AcknowledgedAgentsService {
private acknowledgedAgents: AcknowledgedAgentsMap = {};
private loaded = false;
static computeHash(content: string): string {
return crypto.createHash('sha256').update(content).digest('hex');
}
async load(): Promise<void> {
if (this.loaded) return;
+2 -2
View File
@@ -8,8 +8,8 @@ import yaml from 'js-yaml';
import * as fs from 'node:fs/promises';
import { type Dirent } from 'node:fs';
import * as path from 'node:path';
import * as crypto from 'node:crypto';
import { z } from 'zod';
import { AcknowledgedAgentsService } from './acknowledgedAgents.js';
import type { AgentDefinition } from './types.js';
import { isValidToolName } from '../tools/tool-names.js';
import { FRONTMATTER_REGEX } from '../skills/skillLoader.js';
@@ -347,7 +347,7 @@ export async function loadAgentsFromDirectory(
const filePath = path.join(dir, entry.name);
try {
const content = await fs.readFile(filePath, 'utf-8');
const hash = crypto.createHash('sha256').update(content).digest('hex');
const hash = AcknowledgedAgentsService.computeHash(content);
const agentDefs = await parseAgentMarkdown(filePath, content);
for (const def of agentDefs) {
const agent = markdownToAgentDefinition(def, { hash, filePath });