Files
waggle-os/packages/core/src/skill-hashes.ts
shumeiko.it ad2ca875cb feat(core): give Waggle its own database, apart from memory (P1.5)
Waggle kept its own tables in the memory database. Before the memory
engine can be replaced by mem0 they need a home of their own.

- add WaggleDB (<dataDir>/waggle.db). Automations, notifications, held
  actions, skill hashes, thumbs feedback, cost tracking, the profile card
  and awareness items live there. Their stores take a WaggleDB, so passing
  a memory database is a type error.
- the profile card and awareness items are now per scope ('personal' or
  'workspace:<id>') in one table each, instead of one table per .mind file
- on the first start, copy these tables from personal.mind and every
  workspace.mind (importLegacyMindData). The legacy files are only read,
  each scope is copied once and a table only while its target is empty.
- copy the logger, injection scanner, external-memory ingress guard,
  WorkspaceManager and the embedders from @waggle/hive-mind-core into
  @waggle/core, which now re-exports its own copies
- point the server, agent orchestrator and CLI at waggle.db; update the
  tests that seeded these tables in a memory database

ADR-0007 records the layout and the licensing question about the copied
files.
2026-10-23 14:31:28 +02: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 the .mind DB. On server startup,
* compares current file hashes to stored hashes to detect changes.
* Follows the same ensureTable pattern as CronStore / InstallAuditStore.
*/
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();
}
}