@theglitchking/semantic-memory 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@
12
12
 
13
13
  import { spawnSync } from "node:child_process";
14
14
  import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
15
- import { join, resolve, basename } from "node:path";
15
+ import { join, resolve, basename, dirname } from "node:path";
16
16
  import { createHash } from "node:crypto";
17
17
  import { fileURLToPath } from "node:url";
18
18
 
@@ -53,7 +53,7 @@ function findVaultPath(projectRoot) {
53
53
  const entries = data?.mcpServers || {};
54
54
  for (const [name, entry] of Object.entries(entries)) {
55
55
  if (!entry || typeof entry !== "object") continue;
56
- if (name !== "semantic-vault" && name !== "semantic-sidekick") continue;
56
+ if (name !== "semantic-vault" && name !== "semantic-sidekick" && name !== "semantic-memory") continue;
57
57
  const args = Array.isArray(entry.args) ? entry.args : [];
58
58
  const i = args.indexOf("--notes");
59
59
  if (i >= 0 && i + 1 < args.length) {
@@ -71,9 +71,15 @@ function findVaultPath(projectRoot) {
71
71
  }
72
72
 
73
73
  function findCliBin(projectRoot) {
74
- // Prefer local installed bin (when sidekick is a dependency)
75
- const installed = join(projectRoot, "node_modules", "@theglitchking", "semantic-sidekick", "bin", "semantic-sidekick");
76
- if (existsSync(installed)) return installed;
74
+ // Prefer local installed bin. Try the rebranded package first; fall back to the
75
+ // legacy semantic-sidekick path for machines mid-migration.
76
+ const candidates = [
77
+ join(projectRoot, "node_modules", "@theglitchking", "semantic-memory", "bin", "semantic-memory"),
78
+ join(projectRoot, "node_modules", "@theglitchking", "semantic-sidekick", "bin", "semantic-sidekick"),
79
+ ];
80
+ for (const p of candidates) {
81
+ if (existsSync(p)) return p;
82
+ }
77
83
  // Dev fallback: this hook lives in <repo>/hooks/, the built CLI is at <repo>/dist/cli/index.js
78
84
  const dev = resolve(new URL("../dist/cli/index.js", import.meta.url).pathname);
79
85
  if (existsSync(dev)) return dev;
@@ -130,7 +136,7 @@ export function formatContextBlock(source, query, hits) {
130
136
  if (!hits || hits.length === 0) return "";
131
137
  const lines = [];
132
138
  lines.push(`<vault-context source="${source}" query="${escapeAttr(query)}">`);
133
- lines.push(`The vault (semantic-sidekick) was proactively searched. Top hits:`);
139
+ lines.push(`The vault (semantic-memory) was proactively searched. Top hits:`);
134
140
  lines.push("");
135
141
  for (const h of hits.slice(0, 6)) {
136
142
  const score = typeof h.score === "number" ? h.score.toFixed(2) : "";
@@ -166,27 +172,74 @@ function sessionSeedQuery(projectRoot) {
166
172
  return parts.join(" ");
167
173
  }
168
174
 
175
+ // State file path resolution (Phase 1, v1.2):
176
+ //
177
+ // Each transient state file has TWO paths during the v1.x deprecation window:
178
+ // - new path (canonical, written by all v1.2+ writers): .claude/.semantic-memory/<file>
179
+ // - old path (legacy, still readable for backwards compat): .claude/.sidekick-<file>
180
+ //
181
+ // Reads check the new path first; if absent, fall back to old. Writes always go to
182
+ // the new path. This means: as soon as ANY write happens for a given state file, the
183
+ // new path takes over and the old path becomes a relic. The explicit
184
+ // `bin/semantic-memory migrate-state` command does the move in one shot for users
185
+ // who want the legacy files cleaned up immediately.
186
+ //
187
+ // In v2.0, the legacy fallback read is removed. Users have all of v1.x to migrate.
188
+ const STATE_FILES = {
189
+ mode: { new: [".semantic-memory", "mode"], old: [".sidekick-mode"] },
190
+ fingerprints: { new: [".semantic-memory", "fingerprints.json"], old: [".sidekick-fingerprints.json"] },
191
+ "capture-pending": { new: [".semantic-memory", "capture-pending.json"], old: [".sidekick-capture-pending.json"] },
192
+ };
193
+
194
+ /**
195
+ * Resolve the read and write paths for a named state file.
196
+ * Returns { newPath, oldPath, resolvedRead, writePath }.
197
+ * - resolvedRead is newPath if it exists, else oldPath (for backwards-compat reads)
198
+ * - writePath is always newPath (writes always go to the new location)
199
+ */
200
+ function statePath(projectRoot, name) {
201
+ const entry = STATE_FILES[name];
202
+ if (!entry) throw new Error(`unknown state file: ${name}`);
203
+ const newPath = join(projectRoot, ".claude", ...entry.new);
204
+ const oldPath = join(projectRoot, ".claude", ...entry.old);
205
+ return {
206
+ newPath,
207
+ oldPath,
208
+ resolvedRead: existsSync(newPath) ? newPath : oldPath,
209
+ writePath: newPath,
210
+ };
211
+ }
212
+
213
+ function ensureStateDir(writePath) {
214
+ try { mkdirSync(dirname(writePath), { recursive: true }); } catch {}
215
+ }
216
+
169
217
  function fingerprintPath(projectRoot) {
170
- const dir = join(projectRoot, ".claude");
171
- try { mkdirSync(dir, { recursive: true }); } catch {}
172
- return join(dir, ".sidekick-fingerprints.json");
218
+ // Returns the canonical write path (Phase 2, v1.2). Read sites that need the
219
+ // legacy-aware path use `statePath(projectRoot, "fingerprints").resolvedRead`.
220
+ const { writePath } = statePath(projectRoot, "fingerprints");
221
+ ensureStateDir(writePath);
222
+ return writePath;
173
223
  }
174
224
 
175
225
  function capturePendingPath(projectRoot) {
176
- const dir = join(projectRoot, ".claude");
177
- try { mkdirSync(dir, { recursive: true }); } catch {}
178
- return join(dir, ".sidekick-capture-pending.json");
226
+ // Returns the canonical write path. Read sites that need legacy-aware reads use
227
+ // `statePath(projectRoot, "capture-pending").resolvedRead`.
228
+ const { writePath } = statePath(projectRoot, "capture-pending");
229
+ ensureStateDir(writePath);
230
+ return writePath;
179
231
  }
180
232
 
181
233
  const VALID_MODES = new Set(["vault-first", "research", "outage-silence"]);
182
234
 
183
235
  function modePath(projectRoot) {
184
- return join(projectRoot, ".claude", ".sidekick-mode");
236
+ // Returns the canonical write path. Read sites use the legacy-aware resolver.
237
+ return statePath(projectRoot, "mode").writePath;
185
238
  }
186
239
 
187
240
  function readMode(projectRoot) {
188
241
  try {
189
- const m = readFileSync(modePath(projectRoot), "utf8").trim();
242
+ const m = readFileSync(statePath(projectRoot, "mode").resolvedRead, "utf8").trim();
190
243
  if (VALID_MODES.has(m)) return m;
191
244
  } catch {}
192
245
  return "vault-first";
@@ -195,9 +248,9 @@ function readMode(projectRoot) {
195
248
  function writeMode(projectRoot, mode) {
196
249
  if (!VALID_MODES.has(mode)) return;
197
250
  try {
198
- const dir = join(projectRoot, ".claude");
199
- mkdirSync(dir, { recursive: true });
200
- writeFileSync(modePath(projectRoot), mode);
251
+ const { writePath } = statePath(projectRoot, "mode");
252
+ ensureStateDir(writePath);
253
+ writeFileSync(writePath, mode);
201
254
  } catch (e) {
202
255
  debug(`mode write failed: ${e.message}`);
203
256
  }
@@ -227,16 +280,17 @@ function detectCaptureCue(prompt) {
227
280
 
228
281
  function addCapturePending(projectRoot, prompt, cueSource) {
229
282
  try {
230
- const p = capturePendingPath(projectRoot);
283
+ const { resolvedRead, writePath } = statePath(projectRoot, "capture-pending");
284
+ ensureStateDir(writePath);
231
285
  let state = { items: [] };
232
- try { state = JSON.parse(readFileSync(p, "utf8")); } catch {}
286
+ try { state = JSON.parse(readFileSync(resolvedRead, "utf8")); } catch {}
233
287
  if (!Array.isArray(state.items)) state.items = [];
234
288
  state.items.push({
235
289
  ts: new Date().toISOString(),
236
290
  cue: cueSource,
237
291
  excerpt: prompt.slice(0, 200),
238
292
  });
239
- writeFileSync(p, JSON.stringify(state));
293
+ writeFileSync(writePath, JSON.stringify(state));
240
294
  } catch (e) {
241
295
  debug(`capture pending write failed: ${e.message}`);
242
296
  }
@@ -244,15 +298,16 @@ function addCapturePending(projectRoot, prompt, cueSource) {
244
298
 
245
299
  function resetCapturePending(projectRoot) {
246
300
  try {
247
- const p = capturePendingPath(projectRoot);
248
- writeFileSync(p, JSON.stringify({ items: [] }));
301
+ const { writePath } = statePath(projectRoot, "capture-pending");
302
+ ensureStateDir(writePath);
303
+ writeFileSync(writePath, JSON.stringify({ items: [] }));
249
304
  } catch {}
250
305
  }
251
306
 
252
307
  function readCapturePending(projectRoot) {
253
308
  try {
254
- const p = capturePendingPath(projectRoot);
255
- const state = JSON.parse(readFileSync(p, "utf8"));
309
+ const path = statePath(projectRoot, "capture-pending").resolvedRead;
310
+ const state = JSON.parse(readFileSync(path, "utf8"));
256
311
  return Array.isArray(state.items) ? state.items : [];
257
312
  } catch {
258
313
  return [];
@@ -266,17 +321,20 @@ function normalizeFingerprint(prompt) {
266
321
  .slice(0, 16);
267
322
  }
268
323
 
269
- function loadFingerprints(path) {
324
+ function loadFingerprints(projectRoot) {
270
325
  try {
326
+ const path = statePath(projectRoot, "fingerprints").resolvedRead;
271
327
  return JSON.parse(readFileSync(path, "utf8"));
272
328
  } catch {
273
329
  return { recent: [] };
274
330
  }
275
331
  }
276
332
 
277
- function saveFingerprints(path, state) {
333
+ function saveFingerprints(projectRoot, state) {
278
334
  try {
279
- writeFileSync(path, JSON.stringify(state));
335
+ const { writePath } = statePath(projectRoot, "fingerprints");
336
+ ensureStateDir(writePath);
337
+ writeFileSync(writePath, JSON.stringify(state));
280
338
  } catch (e) {
281
339
  debug(`fingerprint save failed: ${e.message}`);
282
340
  }
@@ -547,8 +605,7 @@ async function handlePrompt(projectRoot, vaultPath, cliBin, input) {
547
605
  emit("UserPromptSubmit", "");
548
606
  return;
549
607
  }
550
- const fpPath = fingerprintPath(projectRoot);
551
- const state = loadFingerprints(fpPath);
608
+ const state = loadFingerprints(projectRoot);
552
609
  const fp = normalizeFingerprint(prompt);
553
610
  const recent = Array.isArray(state.recent) ? state.recent : [];
554
611
  if (recent.includes(fp)) {
@@ -558,7 +615,7 @@ async function handlePrompt(projectRoot, vaultPath, cliBin, input) {
558
615
  }
559
616
  const hits = runSearchCli(cliBin, ["--notes", vaultPath, "--limit", "8", prompt], 30_000);
560
617
  state.recent = [fp, ...recent].slice(0, 10);
561
- saveFingerprints(fpPath, state);
618
+ saveFingerprints(projectRoot, state);
562
619
  const cue = detectCaptureCue(prompt);
563
620
  if (cue) {
564
621
  addCapturePending(projectRoot, prompt, cue);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theglitchking/semantic-memory",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Unified memory-layer plugin for Claude Code. One MCP server, multiple typed corpora (vault, code, plans, docs, research, project-map), unified cross-corpus knowledge graph, conditional tool registration, in-process drift detection, and human↔code translation. Rebrand of @theglitchking/semantic-sidekick at v1.0 — see CHANGELOG for migration.",
5
5
  "type": "module",
6
6
  "main": "./dist/core/index.js",
@@ -28,6 +28,7 @@
28
28
  "skills/outage-silence",
29
29
  "README.md",
30
30
  "CHANGELOG.md",
31
+ "ROADMAP.md",
31
32
  "LICENSE"
32
33
  ],
33
34
  "scripts": {
@@ -12,14 +12,14 @@ const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
12
12
 
13
13
  try {
14
14
  runPostinstall({
15
- packageName: "@theglitchking/semantic-sidekick",
16
- pluginName: "semantic-sidekick",
17
- configFile: "semantic-sidekick.json",
15
+ packageName: "@theglitchking/semantic-memory",
16
+ pluginName: "semantic-memory",
17
+ configFile: "semantic-memory.json",
18
18
  skillsDir: "skills",
19
19
  packageRoot,
20
20
  hookCommand:
21
- "node ./node_modules/@theglitchking/semantic-sidekick/hooks/session-start.js",
21
+ "node ./node_modules/@theglitchking/semantic-memory/hooks/session-start.js",
22
22
  });
23
23
  } catch (err) {
24
- console.warn(`[semantic-sidekick] postinstall failed: ${err?.message || err}`);
24
+ console.warn(`[semantic-memory] postinstall failed: ${err?.message || err}`);
25
25
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/healthcheck.ts"],"sourcesContent":["import { readFile, writeFile, mkdir, stat } from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport { join, basename } from \"node:path\";\nimport { createHash } from \"node:crypto\";\nimport { homedir } from \"node:os\";\nimport { lintVault } from \"./lint.js\";\nimport { deriveProjectRoot } from \"./session.js\";\n\nexport type Tier = \"fast\" | \"slow\" | \"all\";\nexport type Severity = \"ok\" | \"warn\" | \"error\";\n\nexport interface DriftFinding {\n check: string;\n severity: Severity;\n summary: string;\n detail?: string;\n fixable_via?: \"skill-link\" | \"mcp-reconcile\" | \"regenerate-contract\" | \"reindex\" | \"none\";\n}\n\nexport interface HealthcheckResult {\n tier_run: Tier;\n ran_at: string;\n duration_ms: number;\n findings: DriftFinding[];\n counts: { ok: number; warn: number; error: number };\n cached?: boolean;\n}\n\nexport interface HealthcheckOptions {\n projectRoot: string;\n /** Vault path. When omitted, derived from projectRoot's .claude/.vault. */\n vaultPath?: string;\n tier?: Tier;\n /** Cache file path. Default: <projectRoot>/.claude/.semantic-memory/healthcheck-cache.json */\n cachePath?: string;\n /** Force re-run even if cache is <5 minutes old. */\n force?: boolean;\n /** Override \"now\" — used by tests. */\n nowMs?: number;\n}\n\nconst CACHE_TTL_MS = 5 * 60 * 1000;\n\n/**\n * Run the drift-detection healthcheck.\n *\n * - Fast tier: cheap file-system probes (~ms each). Plugin version, skill manifests,\n * .mcp.json registration, hook registration, index freshness, session staleness,\n * AGENTS.md managed-block sha. All independent, run in parallel. Total budget: <100ms\n * on a representative install.\n * - Slow tier: full vault lint. Adds tens of seconds on large vaults — invoked by\n * explicit /healthcheck, never by the SessionStart auto path.\n * - Cache: when force=false and the cache file is <5min old, return the cached result.\n *\n * Fail-open: any individual check that throws produces a `severity: \"warn\"` finding\n * with the error message; never propagates.\n */\nexport async function runHealthcheck(opts: HealthcheckOptions): Promise<HealthcheckResult> {\n const start = (opts.nowMs ?? Date.now());\n const tier: Tier = opts.tier ?? \"fast\";\n const cachePath = opts.cachePath ?? join(opts.projectRoot, \".claude\", \".semantic-memory\", \"healthcheck-cache.json\");\n\n if (!opts.force && existsSync(cachePath)) {\n try {\n const raw = await readFile(cachePath, \"utf-8\");\n const cached = JSON.parse(raw) as HealthcheckResult & { ran_at_ms: number };\n if (cached.tier_run === tier && start - cached.ran_at_ms < CACHE_TTL_MS) {\n return { ...cached, cached: true };\n }\n } catch {\n // ignore corrupt cache; will overwrite below\n }\n }\n\n const findings = tier === \"fast\" ? await runFastTier(opts) : tier === \"slow\" ? await runSlowTier(opts) : [...(await runFastTier(opts)), ...(await runSlowTier(opts))];\n\n const counts = countBySeverity(findings);\n const ranAt = new Date(start).toISOString();\n const result: HealthcheckResult = {\n tier_run: tier,\n ran_at: ranAt,\n duration_ms: Date.now() - start,\n findings,\n counts,\n };\n\n // Persist cache (best-effort; failure is non-fatal).\n try {\n await mkdir(join(opts.projectRoot, \".claude\", \".semantic-memory\"), { recursive: true });\n await writeFile(cachePath, JSON.stringify({ ...result, ran_at_ms: start }, null, 2), \"utf-8\");\n } catch {\n // never let cache write block the result\n }\n\n return result;\n}\n\n/**\n * Surface filter — return findings with severity >= warn. Used by the SessionStart hook\n * to decide whether to emit anything at all.\n */\nexport function filterToDrift(result: HealthcheckResult): DriftFinding[] {\n return result.findings.filter((f) => f.severity !== \"ok\");\n}\n\nfunction countBySeverity(findings: DriftFinding[]): { ok: number; warn: number; error: number } {\n const counts = { ok: 0, warn: 0, error: 0 };\n for (const f of findings) counts[f.severity]++;\n return counts;\n}\n\nasync function runFastTier(opts: HealthcheckOptions): Promise<DriftFinding[]> {\n const checks: Promise<DriftFinding | null>[] = [\n checkPluginVersion(opts.projectRoot),\n checkMcpJsonEntry(opts.projectRoot),\n checkHookRegistration(opts.projectRoot),\n checkIndexFreshness(opts.projectRoot, opts.vaultPath),\n checkSessionStaleness(opts.projectRoot),\n checkAgentsContract(opts.projectRoot),\n ...(await skillManifestChecks(opts.projectRoot)),\n ];\n const results = await Promise.all(checks.map((p) => p.catch((err) => failOpenWarn(err))));\n return results.filter((r): r is DriftFinding => r !== null);\n}\n\nasync function runSlowTier(opts: HealthcheckOptions): Promise<DriftFinding[]> {\n const findings: DriftFinding[] = [];\n if (opts.vaultPath && existsSync(opts.vaultPath)) {\n try {\n const report = await lintVault(opts.vaultPath);\n const total = report.findings.length;\n if (total === 0) {\n findings.push({ check: \"lint_vault\", severity: \"ok\", summary: `vault lint clean (${report.counts.filesLinted} files)` });\n } else {\n findings.push({\n check: \"lint_vault\",\n severity: report.counts.errors > 0 ? \"error\" : \"warn\",\n summary: `vault lint: ${total} findings (errors=${report.counts.errors}, warnings=${report.counts.warnings})`,\n detail: `Run \\`lint_vault\\` for the full report. Many lint findings need human review (stale notes, broken wikilinks).`,\n });\n }\n } catch (err: any) {\n findings.push({ check: \"lint_vault\", severity: \"warn\", summary: `lint_vault threw: ${err?.message ?? err}` });\n }\n }\n return findings;\n}\n\nfunction failOpenWarn(err: unknown): DriftFinding {\n return {\n check: \"internal\",\n severity: \"warn\",\n summary: `healthcheck internal error: ${err instanceof Error ? err.message : String(err)}`,\n };\n}\n\n// --- individual checks ---\n\nasync function checkPluginVersion(projectRoot: string): Promise<DriftFinding | null> {\n const pkgPaths = [\n join(projectRoot, \"node_modules\", \"@theglitchking\", \"semantic-memory\", \"package.json\"),\n join(projectRoot, \"node_modules\", \"@theglitchking\", \"semantic-sidekick\", \"package.json\"),\n ];\n for (const p of pkgPaths) {\n if (!existsSync(p)) continue;\n try {\n const installed = JSON.parse(await readFile(p, \"utf-8\"));\n if (typeof installed?.version === \"string\") {\n return { check: \"plugin_version\", severity: \"ok\", summary: `installed=${installed.version}` };\n }\n } catch {\n // continue to next candidate\n }\n }\n return null; // no installation present — not a drift event for non-npm dev environments\n}\n\nasync function checkMcpJsonEntry(projectRoot: string): Promise<DriftFinding | null> {\n const mcpPath = join(projectRoot, \".mcp.json\");\n if (!existsSync(mcpPath)) return null; // no .mcp.json — fine for dev\n const data = JSON.parse(await readFile(mcpPath, \"utf-8\"));\n const servers = data?.mcpServers ?? {};\n const hasEntry = Object.keys(servers).some((k) => k === \"semantic-vault\" || k === \"semantic-sidekick\" || k === \"semantic-memory\");\n if (!hasEntry) {\n return {\n check: \"mcp_json_entry\",\n severity: \"warn\",\n summary: \".mcp.json present but no semantic-memory/sidekick/vault server entry\",\n fixable_via: \"mcp-reconcile\",\n };\n }\n return { check: \"mcp_json_entry\", severity: \"ok\", summary: \".mcp.json server entry present\" };\n}\n\nasync function checkHookRegistration(projectRoot: string): Promise<DriftFinding | null> {\n const settings = join(projectRoot, \".claude\", \"settings.json\");\n if (!existsSync(settings)) return null;\n try {\n const data = JSON.parse(await readFile(settings, \"utf-8\"));\n const hooks = data?.hooks ?? {};\n const required = [\"SessionStart\", \"UserPromptSubmit\", \"Stop\"];\n const missing: string[] = [];\n for (const event of required) {\n const arr = hooks[event];\n if (!Array.isArray(arr) || arr.length === 0) missing.push(event);\n }\n if (missing.length > 0) {\n return {\n check: \"hook_registration\",\n severity: \"warn\",\n summary: `missing hook registrations: ${missing.join(\", \")}`,\n detail: `Run \\`/relink\\` or reinstall the plugin to refresh hook registrations.`,\n };\n }\n return { check: \"hook_registration\", severity: \"ok\", summary: \"all 3 hook events registered\" };\n } catch (err: any) {\n return failOpenWarn(err);\n }\n}\n\nasync function checkIndexFreshness(projectRoot: string, vaultPath?: string): Promise<DriftFinding | null> {\n const vault = vaultPath ?? defaultVaultPath(projectRoot);\n if (!vault || !existsSync(vault)) return null;\n const indexDir = join(vault, \".semantic-sidekick-index\");\n const meta = join(indexDir, \"meta.json\");\n if (!existsSync(meta)) {\n return {\n check: \"index_freshness\",\n severity: \"warn\",\n summary: \"no index built yet\",\n detail: \"Run `reindex` to build the vector index.\",\n fixable_via: \"reindex\",\n };\n }\n try {\n const data = JSON.parse(await readFile(meta, \"utf-8\"));\n if (typeof data.indexedAt === \"string\") {\n return { check: \"index_freshness\", severity: \"ok\", summary: `last reindex ${data.indexedAt}` };\n }\n } catch {\n // ignore\n }\n return null;\n}\n\nasync function checkSessionStaleness(projectRoot: string): Promise<DriftFinding | null> {\n const sessionPath = join(projectRoot, \".claude\", \".semantic-memory\", \"session.json\");\n if (!existsSync(sessionPath)) return null;\n try {\n const state = JSON.parse(await readFile(sessionPath, \"utf-8\"));\n if (state?.closed_at) return null;\n const last = state?.last_activity_at;\n if (typeof last !== \"string\") return null;\n const ageMs = Date.now() - new Date(last).getTime();\n if (ageMs > 24 * 60 * 60 * 1000) {\n return {\n check: \"session_staleness\",\n severity: \"warn\",\n summary: `session ${state.id} stale (>${Math.round(ageMs / (60 * 60 * 1000))}h since activity)`,\n detail: `Either resume the session (run session_run / session_finish) or remove .claude/.semantic-memory/session.json to abandon it.`,\n };\n }\n return { check: \"session_staleness\", severity: \"ok\", summary: `session ${state.id} active` };\n } catch {\n return null;\n }\n}\n\nasync function checkAgentsContract(projectRoot: string): Promise<DriftFinding | null> {\n const path = join(projectRoot, \"AGENTS.md\");\n if (!existsSync(path)) return null; // optional artifact\n try {\n const raw = await readFile(path, \"utf-8\");\n const begin = raw.indexOf(\"<!-- semantic-memory:begin contract -->\");\n const end = raw.indexOf(\"<!-- semantic-memory:end contract -->\");\n if (begin === -1 || end === -1) {\n return {\n check: \"agents_contract\",\n severity: \"warn\",\n summary: \"AGENTS.md exists but lacks managed-block markers\",\n detail: \"Either move custom content to a Local Notes section, or delete AGENTS.md and run `regenerate_contract` to bootstrap.\",\n fixable_via: \"regenerate-contract\",\n };\n }\n return { check: \"agents_contract\", severity: \"ok\", summary: \"AGENTS.md managed block intact\" };\n } catch {\n return null;\n }\n}\n\nasync function skillManifestChecks(projectRoot: string): Promise<Promise<DriftFinding | null>[]> {\n const home = homedir();\n const targets = [\n { agent: \"claude\", path: join(home, \".claude\", \"skills\") },\n { agent: \"codex\", path: join(home, \".codex\", \"skills\") },\n { agent: \"copilot\", path: join(home, \".copilot\", \"skills\") },\n { agent: \"pi\", path: join(home, \".pi\", \"agent\", \"skills\") },\n { agent: \"claude-local\", path: join(projectRoot, \".claude\", \"skills\") },\n ];\n const out: Promise<DriftFinding | null>[] = [];\n for (const t of targets) {\n out.push(checkOneSkillManifest(t.agent, t.path));\n }\n return out;\n}\n\nasync function checkOneSkillManifest(agentLabel: string, skillsRoot: string): Promise<DriftFinding | null> {\n const manifestPath = join(skillsRoot, \".semantic-memory-skill-manifest.json\");\n if (!existsSync(manifestPath)) return null; // not installed for this agent — fine\n try {\n const raw = await readFile(manifestPath, \"utf-8\");\n const manifest = JSON.parse(raw);\n if (typeof manifest?.installed_at !== \"string\") return null;\n const age = Date.now() - new Date(manifest.installed_at).getTime();\n // Manifest itself is the source of truth; deeper sha-recheck happens via slow tier\n return {\n check: `skill_manifest:${agentLabel}`,\n severity: \"ok\",\n summary: `${agentLabel} skills installed v${manifest.version} (${Math.round(age / (24 * 60 * 60 * 1000))}d ago)`,\n };\n } catch {\n return null;\n }\n}\n\nfunction defaultVaultPath(projectRoot: string): string {\n return join(projectRoot, \".claude\", \".vault\");\n}\n\n/**\n * Format a drift summary for the SessionStart hook. Returns the text to inject as\n * additionalContext, or an empty string when there's nothing to surface.\n */\nexport function formatDriftBanner(result: HealthcheckResult): string {\n const drift = filterToDrift(result);\n if (drift.length === 0) return \"\";\n const lines = [\n `<vault-drift count=\"${drift.length}\">`,\n `⚠️ semantic-memory: ${drift.length} drift issue${drift.length === 1 ? \"\" : \"s\"} detected`,\n ];\n for (const f of drift.slice(0, 5)) {\n const sev = f.severity === \"error\" ? \"✗\" : \"⚠\";\n lines.push(` ${sev} ${f.check}: ${f.summary}`);\n }\n if (drift.length > 5) lines.push(` …and ${drift.length - 5} more`);\n lines.push(`Run /healthcheck for details, or /healthcheck --fix to auto-fix safe items.`);\n lines.push(`</vault-drift>`);\n return lines.join(\"\\n\");\n}\n"],"mappings":";;;;;AAAA,SAAS,UAAU,WAAW,aAAmB;AACjD,SAAS,kBAAkB;AAC3B,SAAS,YAAsB;AAE/B,SAAS,eAAe;AAqCxB,IAAM,eAAe,IAAI,KAAK;AAgB9B,eAAsB,eAAe,MAAsD;AACzF,QAAM,QAAS,KAAK,SAAS,KAAK,IAAI;AACtC,QAAM,OAAa,KAAK,QAAQ;AAChC,QAAM,YAAY,KAAK,aAAa,KAAK,KAAK,aAAa,WAAW,oBAAoB,wBAAwB;AAElH,MAAI,CAAC,KAAK,SAAS,WAAW,SAAS,GAAG;AACxC,QAAI;AACF,YAAM,MAAM,MAAM,SAAS,WAAW,OAAO;AAC7C,YAAM,SAAS,KAAK,MAAM,GAAG;AAC7B,UAAI,OAAO,aAAa,QAAQ,QAAQ,OAAO,YAAY,cAAc;AACvE,eAAO,EAAE,GAAG,QAAQ,QA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