Files
waggle-os/packages/core/src/skill-hashes.ts
shumeiko.it 2e08f2ae96 refactor(server): move the install audit trail from core to the server (P1.2, UF-040)
Core keeps only shared infrastructure, so InstallAuditStore leaves it and
lives in packages/server/src/local/install-audit.ts. The trail itself stays:
the Audit and Activity tabs, the extend and export routes, and the threat
model entry keep working on the same waggle.db table.

- @waggle/shared gets RecordAuditInput and the narrow InstallAuditRecorder
  (record only), which the agent's skill tools now depend on.
- The server creates the audit table before the one-time legacy copy, which
  no longer constructs it in core.
- UF-040: GET /api/debug/logs called prepare() on the store's private db
  wrapper, which has none, so the support bundle never carried recent
  installs. It now reads them through getRecentSummary().
2026-10-25 21:32:38 +01:00

123 lines
4.0 KiB
TypeScript

/**
* Skill Hash Store — SHA-256 content hashing for skill update detection.
*
* Stores hashes of skill file content in waggle.db. On server startup,
* compares current file hashes to stored hashes to detect changes.
* Follows the same ensureTable pattern as CronStore.
*/
import * as crypto from 'node:crypto';
import type { WaggleDB } from './waggle-db.js';
// ── Types ──────────────────────────────────────────────────────────────
export interface SkillHash {
name: string;
hash: string;
verified_at: string;
}
// ── Table DDL ──────────────────────────────────────────────────────────
export const SKILL_HASHES_TABLE_SQL = `
CREATE TABLE IF NOT EXISTS skill_hashes (
name TEXT PRIMARY KEY,
hash TEXT NOT NULL,
verified_at TEXT NOT NULL DEFAULT (datetime('now'))
);
`;
// ── Helpers ────────────────────────────────────────────────────────────
/** Compute SHA-256 hex digest of skill content. */
export function computeSkillHash(content: string): string {
return crypto.createHash('sha256').update(content).digest('hex');
}
// ── Store ──────────────────────────────────────────────────────────────
export class SkillHashStore {
private db: WaggleDB;
constructor(db: WaggleDB) {
this.db = db;
this.ensureTable();
}
private ensureTable(): void {
const raw = this.db.getDatabase();
const exists = raw.prepare(
"SELECT name FROM sqlite_master WHERE type='table' AND name='skill_hashes'",
).get();
if (!exists) {
raw.exec(SKILL_HASHES_TABLE_SQL);
}
}
/** Store or update hash for a skill. */
setHash(name: string, hash: string): void {
this.db.getDatabase().prepare(`
INSERT INTO skill_hashes (name, hash) VALUES (?, ?)
ON CONFLICT (name) DO UPDATE SET hash = ?, verified_at = datetime('now')
`).run(name, hash, hash);
}
/** Get stored hash for a skill. */
getHash(name: string): SkillHash | undefined {
return this.db.getDatabase().prepare(
'SELECT * FROM skill_hashes WHERE name = ?',
).get(name) as SkillHash | undefined;
}
/** Remove hash (when skill is deleted). */
removeHash(name: string): void {
this.db.getDatabase().prepare('DELETE FROM skill_hashes WHERE name = ?').run(name);
}
/**
* Check all skills against stored hashes.
* Returns lists of changed, added, and removed skill names.
*/
checkAll(currentSkills: Array<{ name: string; content: string }>): {
changed: string[];
added: string[];
removed: string[];
} {
const storedHashes = this.db.getDatabase().prepare(
'SELECT name, hash FROM skill_hashes',
).all() as SkillHash[];
const storedMap = new Map(storedHashes.map(h => [h.name, h.hash]));
const currentNames = new Set(currentSkills.map(s => s.name));
const changed: string[] = [];
const added: string[] = [];
for (const skill of currentSkills) {
const currentHash = computeSkillHash(skill.content);
const storedHash = storedMap.get(skill.name);
if (!storedHash) {
added.push(skill.name);
} else if (storedHash !== currentHash) {
changed.push(skill.name);
}
}
const removed = storedHashes
.filter(h => !currentNames.has(h.name))
.map(h => h.name);
return { changed, added, removed };
}
/** Mark a skill as verified (update hash to current content). */
verify(name: string, content: string): void {
this.setHash(name, computeSkillHash(content));
}
/** Clear all hashes (for testing). */
clear(): void {
this.db.getDatabase().prepare('DELETE FROM skill_hashes').run();
}
}