@luisarg/memory-auto 0.1.2 → 0.1.4

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.
package/README.md CHANGED
@@ -38,16 +38,25 @@ Environment variables used at load time: `DSH_MEMORY_PATH`,
38
38
 
39
39
  ## Behavior
40
40
 
41
- - `session.created` — registers the session (project name resolution).
42
- - `session.disposed` / dispose path post-session digest of the transcript.
43
- - `agent/status` idle auto-capture gate: skips sessions without activity or
44
- with a checkpoint already delivered.
45
- - `tool.execute.after`-style event tracking detects `git commit*` and queues
46
- a commit checkpoint prompt for the running agent.
47
- - `experimental.session.compacting` pre-compaction capture (always fires when
48
- there is activity).
49
- - `agent/pre-step` delivers queued checkpoint prompts (the agent writes
50
- entries with the `store_*` MCP tools).
41
+ - `session/created` — registers the session and resolves the project name
42
+ (`openspec/` present directory basename; else `package.json` name; else
43
+ `pyproject.toml` `[project].name`; else first `#` heading of `README.md`;
44
+ else directory basename).
45
+ - `session/disposed` digests the transcript, skipping sessions with no
46
+ activity. A dispose-time `ctx.effect` batch-digests any session still
47
+ pending, using the tracked activity summary as a stand-in transcript.
48
+ - `agent/status` with status `idle` — runs the auto-capture gate
49
+ (`idleCheckpoint`): skips sessions without activity or already delivered, and
50
+ on the first idle of a session also digests the tracked activity summary.
51
+ - `session/event` — tracks activity and queues checkpoints:
52
+ - `tool/call` (also accepted as `tool_call`) whose `args.command` matches
53
+ `git commit` queues a commit checkpoint.
54
+ - `compaction/start` queues a pre-compaction checkpoint (fires whenever there
55
+ is activity, even if one was already delivered).
56
+ - `user/message` and `assistant/message` are recorded as tracked activity.
57
+ - `agent/pre-step` — delivers the queued checkpoint by pushing it onto
58
+ `payload.context` (or `payload.messages`), and the agent writes entries with
59
+ the `store_*` MCP tools.
51
60
 
52
61
  Extraction failures are retried with bounded backoff and logged; a failed
53
62
  digest never takes the agent down.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import Schema from "@deepseek-ai/schemastery";
2
2
  import { Context } from "@deepseek-ai/cordis";
3
-
4
3
  //#region src/plugin.d.ts
5
- declare const name = "memory-auto";
6
- interface Config {
4
+ export declare const name = "memory-auto";
5
+ export interface Config {
7
6
  memoryPath: string;
8
7
  serverDir: string;
9
8
  provider: string;
@@ -12,8 +11,7 @@ interface Config {
12
11
  minTranscriptChars: number;
13
12
  enabled: boolean;
14
13
  }
15
- declare const Config: Schema<Config>;
16
- declare function apply(ctx: Context, config: Config): void;
14
+ export declare const Config: Schema<Config>;
15
+ export declare function apply(ctx: Context, config: Config): void;
17
16
  //#endregion
18
- export { Config, apply, name };
19
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/plugin.ts"],"sourcesContent":[],"mappings":";;;;cAkBa,IAAA;UAEI,MAAA;EAFJ,UAAI,EAAA,MAAA;EAEA,SAAM,EAAA,MAAA;EAUV,QAAA,EAQX,MAAA;EA+Cc,KAAA,EAAA,MAAK;;;;;cAvDR,QAAQ,OAAO;iBAuDZ,KAAA,MAAW,iBAAiB"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;qBAgCa;iBAEI;EACf;EACA;EACA;EACA;EACA;EACA;EACA;;qBAGW,QAAQ,OAAO;wBAuDZ,MAAM,KAAK,SAAS,QAAQ"}
package/dist/index.js CHANGED
@@ -7,10 +7,54 @@ import { readFile } from "node:fs/promises";
7
7
  import { spawn } from "node:child_process";
8
8
  import readline from "node:readline";
9
9
  import { BlockAssembler, createUserMessage } from "@deepseek-ai/dsh-llm";
10
-
11
10
  //#region src/pure.ts
11
+ /**
12
+ * DSH memory plugin (adapted from the OpenCode memory plugin).
13
+ *
14
+ * Pure helpers only: prompt builders, transcript chunking, JSON repair and
15
+ * entry validation. No I/O and no harness wiring — `plugin.ts` owns the hook
16
+ * registration and `digest.ts` owns the LLM call and vault writes.
17
+ */
12
18
  const CHECKPOINT_MARKER = "[memory-checkpoint]";
13
19
  /**
20
+ * Entry types the vault's MCP server can store via `store_*` tools. The
21
+ * extraction prompt is restricted to these so every produced entry has a
22
+ * write path (no `idea`/`context`/`source` — those have no store tool).
23
+ */
24
+ const EXTRACTABLE_TYPES = [
25
+ "decision",
26
+ "fact",
27
+ "learning",
28
+ "convention"
29
+ ];
30
+ /**
31
+ * Shared entry vocabulary: one definition list, used by BOTH the internal
32
+ * extraction prompt and the agent-facing checkpoint prompt. Keeping one source
33
+ * stops the two from drifting, which is how the checkpoint prompt ended up
34
+ * asking agents to write entries it never defined.
35
+ */
36
+ const ENTRY_TYPE_GLOSS = {
37
+ decision: "architectural or design choices that were made",
38
+ fact: "stable, verifiable statements about the project (versions, constraints)",
39
+ learning: "non-obvious lessons, debugging insights, or solutions found",
40
+ convention: "style rules, naming patterns, coding conventions agreed"
41
+ };
42
+ /** Content shape, quoted by both prompts. */
43
+ const ENTRY_CONTENT_RULE = "a single paragraph — no headings, no bullet lists, no markdown structure";
44
+ /**
45
+ * What "notable" means, shared by both prompts. The extraction model is told
46
+ * these rules; the agent writing checkpoints needs them just as much.
47
+ */
48
+ const ENTRY_SELECTION_RULES = [
49
+ "Skip trivia (greetings, \"ok\", \"thanks\", restating the request).",
50
+ "Prefer fewer, high-signal entries over many weak ones.",
51
+ "Do not record anything you cannot ground in this session's activity."
52
+ ];
53
+ /** Entry-type clauses as bullet lines, in EXTRACTABLE_TYPES order. */
54
+ function entryTypeBullets() {
55
+ return EXTRACTABLE_TYPES.map((t) => `- **${t}**: ${ENTRY_TYPE_GLOSS[t]}.`);
56
+ }
57
+ /**
14
58
  * Resolve the project name from a working directory.
15
59
  * Priority:
16
60
  * 1. OpenSpec presence: if `openspec/` exists, use the basename.
@@ -59,7 +103,13 @@ function buildCheckpointPrompt(state, activitySummary) {
59
103
  "Tracked activity:",
60
104
  activitySummary.trim() || "(none recorded)",
61
105
  "",
62
- "Write OKF entries for any notable decisions, facts, or learnings using the `store_*` MCP tools.",
106
+ "Write OKF entries for anything notable using the `store_*` MCP tools.",
107
+ "Entry types:",
108
+ ...entryTypeBullets(),
109
+ "",
110
+ `Set \`content\` to ${ENTRY_CONTENT_RULE}, and \`description\` to a one-sentence summary.`,
111
+ "Tag with lowercase-kebab tags (at least one, e.g. architecture/python/testing).",
112
+ ...ENTRY_SELECTION_RULES,
63
113
  "If nothing is notable, say so explicitly and exit."
64
114
  ].join("\n");
65
115
  }
@@ -89,22 +139,17 @@ function buildCommitCheckpointPrompt(state) {
89
139
  return [
90
140
  `${CHECKPOINT_MARKER} Memory capture after \`git commit\` in project \`${state.project}\`.`,
91
141
  "",
92
- "Review the staged/committed changes and write OKF entries for any notable decisions, facts, or learnings.",
142
+ "Review the staged/committed changes and write OKF entries through the `store_*` MCP tools.",
143
+ "Entry types:",
144
+ ...entryTypeBullets(),
145
+ "",
146
+ `Set \`content\` to ${ENTRY_CONTENT_RULE}, and \`description\` to a one-sentence summary.`,
147
+ "Tag with lowercase-kebab tags (at least one, e.g. architecture/python/testing).",
148
+ ...ENTRY_SELECTION_RULES,
93
149
  "If nothing is notable, say so explicitly and exit."
94
150
  ].join("\n");
95
151
  }
96
152
  /**
97
- * Entry types the vault's MCP server can store via `store_*` tools. The
98
- * extraction prompt is restricted to these so every produced entry has a
99
- * write path (no `idea`/`context`/`source` — those have no store tool).
100
- */
101
- const EXTRACTABLE_TYPES = [
102
- "decision",
103
- "fact",
104
- "learning",
105
- "convention"
106
- ];
107
- /**
108
153
  * Build the system + user messages for the in-process extraction call.
109
154
  * The system part instructs the model to return a JSON array restricted to
110
155
  * EXTRACTABLE_TYPES; the user part carries the transcript.
@@ -113,7 +158,7 @@ function buildExtractionPrompt(project, transcript, contextFiles = {}) {
113
158
  const sysParts = ["You are an assistant that extracts durable knowledge from a session transcript."];
114
159
  if (contextFiles.criticalFacts?.trim()) sysParts.push(`Always-loaded context: CRITICAL_FACTS.md\n${contextFiles.criticalFacts.trim()}`);
115
160
  if (contextFiles.claude?.trim()) sysParts.push(`Always-loaded context: _CLAUDE.md\n${contextFiles.claude.trim()}`);
116
- sysParts.push(`For the transcript of project \`${project}\`, identify:`, "- **Decisions**: architectural or design choices that were made.", "- **Facts**: stable, verifiable statements about the project (versions, conventions, constraints).", "- **Learnings**: non-obvious lessons, debugging insights, or solutions found.", "- **Conventions**: style rules, naming patterns, coding conventions agreed.", "", "Return a JSON array. Each element must have exactly:", " - \"entry_type\": one of \"decision\" | \"fact\" | \"learning\" | \"convention\"", " - \"content\": a single-paragraph statement (no headings, no lists)", " - \"description\": a one-sentence summary of `content` (queryable)", " - \"tags\": an array of lowercase-kebab tags (never empty if possible, at least 1 like architecture/python/testing)", " - \"confidence\": a number 0.0-1.0", " - \"openspec_change_id\": (optional) the change slug if the transcript names it", "", "Rules:", "- Skip trivial exchanges (greetings, \"ok\", \"thanks\", or anything with no project knowledge).", "- Prefer fewer, higher-signal entries over many weak ones.", "- Do not include anything not present in the transcript.", "", "Return only the JSON array. No prose, no markdown fences.");
161
+ sysParts.push(`For the transcript of project \`${project}\`, identify:`, ...entryTypeBullets(), "", "Return a JSON array. Each element must have exactly:", ` - "entry_type": one of ${EXTRACTABLE_TYPES.map((t) => `"${t}"`).join(" | ")}`, ` - "content": ${ENTRY_CONTENT_RULE}`, " - \"description\": a one-sentence summary of `content` (queryable)", " - \"tags\": an array of lowercase-kebab tags (never empty if possible, at least 1 like architecture/python/testing)", " - \"confidence\": a number 0.0-1.0", " - \"openspec_change_id\": (optional) the change slug if the transcript names it", "", "Rules:", ...ENTRY_SELECTION_RULES.map((r) => `- ${r}`), "", "Return only the JSON array. No prose, no markdown fences.");
117
162
  return {
118
163
  system: sysParts.join("\n"),
119
164
  user: `Project: ${project}\n\nTranscript:\n---\n${transcript}\n---`
@@ -213,7 +258,6 @@ function validateEntries(payload) {
213
258
  }
214
259
  return valid;
215
260
  }
216
-
217
261
  //#endregion
218
262
  //#region src/digest.ts
219
263
  const MIN_DIGEST_TRANSCRIPT_CHARS = 200;
@@ -233,9 +277,9 @@ function transcriptOfDSM(events) {
233
277
  return text ? `## assistant\n${text}` : null;
234
278
  }
235
279
  if (t === "tool/call" || t === "tool_call") {
236
- const name$1 = d.tool ?? d.name ?? "tool";
280
+ const name = d.tool ?? d.name ?? "tool";
237
281
  const args = d.args ?? d.arguments ?? {};
238
- return `## tool_call ${name$1}\n${JSON.stringify(args).slice(0, 1e3)}`;
282
+ return `## tool_call ${name}\n${JSON.stringify(args).slice(0, 1e3)}`;
239
283
  }
240
284
  if (t === "tool/result" || t === "tool_result") return `## tool_result\n${typeof d.output === "string" ? d.output : JSON.stringify(d).slice(0, 1e3)}`;
241
285
  if (t.startsWith("compaction")) return `## ${t}\n${JSON.stringify(d).slice(0, 500)}`;
@@ -387,13 +431,13 @@ function connectMcp(memoryPath, serverDir) {
387
431
  }).then(() => {
388
432
  if (closed) throw new Error("memory-vault-server closed during handshake");
389
433
  resolve({
390
- callTool: (name$1, args$1, timeoutMs = MCP_CALL_TIMEOUT_MS) => send("tools/call", {
391
- name: name$1,
392
- arguments: args$1
434
+ callTool: (name, args, timeoutMs = MCP_CALL_TIMEOUT_MS) => send("tools/call", {
435
+ name,
436
+ arguments: args
393
437
  }, timeoutMs).then((result) => {
394
438
  if (result?.isError) {
395
439
  const text = Array.isArray(result.content) ? result.content.map((c) => c?.text ?? "").join("") : JSON.stringify(result);
396
- throw new Error(`tool ${name$1} failed: ${text}`);
440
+ throw new Error(`tool ${name} failed: ${text}`);
397
441
  }
398
442
  return result;
399
443
  }),
@@ -427,7 +471,7 @@ async function writeEntries(client, project, entries) {
427
471
  content: e.content,
428
472
  ...e.description ? { description: e.description } : {},
429
473
  tags: e.tags,
430
- confidence: e.confidence,
474
+ ...e.entry_type === "fact" ? { confidence: e.confidence } : {},
431
475
  ...e.openspec_change_id ? { openspec_change_id: e.openspec_change_id } : {}
432
476
  });
433
477
  upserted += 1;
@@ -492,9 +536,22 @@ async function digestSessionDSM(ctx, config, sessionId, directory, project, even
492
536
  await client.close().catch(() => {});
493
537
  }
494
538
  }
495
-
496
539
  //#endregion
497
540
  //#region src/plugin.ts
541
+ /**
542
+ * Harness wiring for `memory-auto`. Registers exactly these hooks:
543
+ *
544
+ * - `session/created` resolve the project name for the session
545
+ * - `session/disposed` digest the transcript
546
+ * - `agent/status` (idle) auto-capture gate
547
+ * - `session/event` activity tracking; `tool/call` with a
548
+ * `git commit` command and `compaction/start`
549
+ * queue checkpoints
550
+ * - `agent/pre-step` deliver the queued checkpoint to the agent
551
+ * - `ctx.effect` dispose batch-digest sessions still pending
552
+ *
553
+ * The agent writes the entries; this plugin only prompts it.
554
+ */
498
555
  const name = "memory-auto";
499
556
  const Config = Schema.object({
500
557
  memoryPath: Schema.string().default(process.env.DSH_MEMORY_PATH ?? ""),
@@ -607,7 +664,9 @@ function apply(ctx, config) {
607
664
  return;
608
665
  }
609
666
  const dir = sessionDirs.get(sid) ?? "";
610
- await digestSessionDSM(ctx, digestConfig, sid, dir, await projectFor(dir), session?.events ?? []);
667
+ const proj = await projectFor(dir);
668
+ const evts = session?.events ?? [];
669
+ await digestSessionDSM(ctx, digestConfig, sid, dir, proj, evts);
611
670
  });
612
671
  ctx.on("agent/status", async (payload) => {
613
672
  const agent = payload?.agent;
@@ -619,10 +678,12 @@ function apply(ctx, config) {
619
678
  if (idleCheckpoint(st, summary(sid))) {
620
679
  console.log(`[memory-auto] idle checkpoint digest for ${sid}`);
621
680
  const dir = sessionDirs.get(sid) ?? "";
622
- await digestSessionDSM(ctx, digestConfig, sid, dir, await projectFor(dir), [{
681
+ const proj = await projectFor(dir);
682
+ const fakeEvents = [{
623
683
  type: "user/message",
624
684
  data: { text: summary(sid) }
625
- }]);
685
+ }];
686
+ await digestSessionDSM(ctx, digestConfig, sid, dir, proj, fakeEvents);
626
687
  }
627
688
  });
628
689
  ctx.on("session/event", async (session, event) => {
@@ -679,17 +740,18 @@ function apply(ctx, config) {
679
740
  for (const [sid, dir] of sessionDirs) {
680
741
  if (!states.get(sid)?.hasActivity) continue;
681
742
  projectFor(dir).then((proj) => {
682
- digestSessionDSM(ctx, digestConfig, sid, dir, proj, [{
743
+ const fakeEvents = [{
683
744
  type: "user/message",
684
745
  data: { text: summary(sid) }
685
- }]);
746
+ }];
747
+ digestSessionDSM(ctx, digestConfig, sid, dir, proj, fakeEvents);
686
748
  });
687
749
  }
688
750
  };
689
751
  });
690
752
  console.log(`[memory-auto] active memoryPath=${memoryPath} serverDir=${serverDir} llm=${config.provider}/${config.model}`);
691
753
  }
692
-
693
754
  //#endregion
694
755
  export { Config, apply, name };
756
+
695
757
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["sysParts: string[]","chunks: string[]","valid: ValidEntry[]","name","messages: Message[]","options: GenerateOptions","child: ChildProcess","msg: any","args","entries: ValidEntry[]","client: McpClient","Config: Schema<Config>","digestConfig: DigestConfig","sid: string | undefined"],"sources":["../src/pure.ts","../src/digest.ts","../src/plugin.ts"],"sourcesContent":["/**\n * DSH memory plugin (adapted from the OpenCode memory plugin).\n *\n * Implements session lifecycle hooks:\n * - session.created: project name resolution only (no memory auto-injection)\n * - session.idle: auto-capture gate (skip if no activity or already delivered)\n * - tool.execute.after: detect `git commit*` and queue a checkpoint\n * - tui.prompt.append: deliver queued checkpoint on next user message\n * - experimental.session.compacting: pre-compaction capture (always fires)\n * - session.end: invoke post-session digest\n * - /brain search|recall|profile: opt-in reads via MCP (2s health check)\n * - /checkpoint: manual structured review\n * - OpenCode version guard: warn on < 1.17.10, disable gracefully\n */\n\nimport { readFile } from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport { join, basename } from \"node:path\";\n\n// ── Constants ────────────────────────────────────────────────────────────\n\nexport const MIN_OPENCODE_VERSION = \"1.17.10\";\nexport const MCP_UNREACHABLE =\n \"> ⚠️ Memory server unreachable — search cannot be completed.\";\nconst CHECKPOINT_MARKER = \"[memory-checkpoint]\";\n\n// ── Version guard ────────────────────────────────────────────────────────\n\n/** Compare two \"x.y.z\" semver strings. Returns negative/0/positive. */\nfunction compareSemver(a: string, b: string): number {\n const [a1, a2, a3] = a.split(\".\").map((n) => parseInt(n, 10) || 0);\n const [b1, b2, b3] = b.split(\".\").map((n) => parseInt(n, 10) || 0);\n if (a1 !== b1) return a1 - b1;\n if (a2 !== b2) return a2 - b2;\n return (a3 || 0) - (b3 || 0);\n}\n\nexport function isSupportedVersion(version: string): boolean {\n return compareSemver(version, MIN_OPENCODE_VERSION) >= 0;\n}\n\n// ── Project name resolution ──────────────────────────────────────────────\n\n/**\n * Resolve the project name from a working directory.\n * Priority:\n * 1. OpenSpec presence: if `openspec/` exists, use the basename.\n * 2. package.json -> name\n * 3. pyproject.toml -> [project] -> name\n * 4. README.md: first 5 lines, heading pattern `# <ProjectName>`\n * 5. Fallback: basename of working directory\n */\nexport async function resolveProjectName(cwd: string): Promise<string> {\n if (existsSync(join(cwd, \"openspec\"))) {\n return basename(cwd);\n }\n // package.json\n const pkgPath = join(cwd, \"package.json\");\n if (existsSync(pkgPath)) {\n try {\n const pkg = JSON.parse(await readFile(pkgPath, \"utf-8\"));\n if (typeof pkg.name === \"string\" && pkg.name.trim()) {\n return pkg.name.trim();\n }\n } catch {\n // ignore parse errors; try next strategy\n }\n }\n // pyproject.toml (minimal regex parse)\n const pyprojectPath = join(cwd, \"pyproject.toml\");\n if (existsSync(pyprojectPath)) {\n try {\n const text = await readFile(pyprojectPath, \"utf-8\");\n const m = text.match(/\\[project\\][^\\[]*?name\\s*=\\s*[\"']([^\"']+)[\"']/);\n if (m) return m[1];\n } catch {\n // ignore\n }\n }\n // README.md heading\n const readmePath = join(cwd, \"README.md\");\n if (existsSync(readmePath)) {\n try {\n const text = await readFile(readmePath, \"utf-8\");\n const head = text.split(\"\\n\").slice(0, 5);\n for (const line of head) {\n const m = line.match(/^#\\s+(.+)$/);\n if (m) return m[1].trim();\n }\n } catch {\n // ignore\n }\n }\n return basename(cwd);\n}\n\n// ── Checkpoint state ─────────────────────────────────────────────────────\n\nexport interface SessionState {\n project: string;\n hasActivity: boolean;\n checkpointDelivered: boolean;\n queuedCheckpoint: string | null;\n}\n\nexport function createSessionState(project: string): SessionState {\n return {\n project,\n hasActivity: false,\n checkpointDelivered: false,\n queuedCheckpoint: null,\n };\n}\n\n/**\n * Build the checkpoint prompt body. Pure function — exported for testing.\n */\nexport function buildCheckpointPrompt(state: SessionState, activitySummary: string): string {\n return [\n `${CHECKPOINT_MARKER} End-of-session memory capture for project \\`${state.project}\\`.`,\n \"\",\n \"Tracked activity:\",\n activitySummary.trim() || \"(none recorded)\",\n \"\",\n \"Write OKF entries for any notable decisions, facts, or learnings using the `store_*` MCP tools.\",\n \"If nothing is notable, say so explicitly and exit.\",\n ].join(\"\\n\");\n}\n\n/**\n * Decide whether to deliver a checkpoint on `session.idle`.\n * Returns the prompt to deliver, or null to skip.\n */\nexport function idleCheckpoint(\n state: SessionState,\n activitySummary: string,\n): string | null {\n if (!state.hasActivity) return null;\n if (state.checkpointDelivered) return null;\n state.checkpointDelivered = true;\n return buildCheckpointPrompt(state, activitySummary);\n}\n\n/**\n * Decide whether to fire on `experimental.session.compacting`.\n * Per spec: always fires when activity exists, even if checkpoint was delivered.\n */\nexport function compactingCheckpoint(\n state: SessionState,\n activitySummary: string,\n): string | null {\n if (!state.hasActivity) return null;\n return buildCheckpointPrompt(state, activitySummary);\n}\n\n// ── Git commit detection ─────────────────────────────────────────────────\n\nconst GIT_COMMIT_PATTERN = /git\\s+commit\\b/;\n\nexport function isGitCommit(command: string): boolean {\n return GIT_COMMIT_PATTERN.test(command);\n}\n\nexport function buildCommitCheckpointPrompt(state: SessionState): string {\n return [\n `${CHECKPOINT_MARKER} Memory capture after \\`git commit\\` in project \\`${state.project}\\`.`,\n \"\",\n \"Review the staged/committed changes and write OKF entries for any notable decisions, facts, or learnings.\",\n \"If nothing is notable, say so explicitly and exit.\",\n ].join(\"\\n\");\n}\n\n// ── In-process session digest (ctx.llm) ──────────────────────────────────\n\n/**\n * Entry types the vault's MCP server can store via `store_*` tools. The\n * extraction prompt is restricted to these so every produced entry has a\n * write path (no `idea`/`context`/`source` — those have no store tool).\n */\nexport const EXTRACTABLE_TYPES = ['decision', 'fact', 'learning', 'convention'] as const\nexport type ExtractableType = (typeof EXTRACTABLE_TYPES)[number]\n\n/** One validated OKF entry ready to be stored through the vault server. */\nexport interface ValidEntry {\n entry_type: ExtractableType\n content: string\n description: string\n tags: string[]\n confidence: number\n openspec_change_id: string | null\n}\n\n/** Optional vault context files to embed in the extraction prompt. */\nexport interface DigestContextFiles {\n criticalFacts?: string\n claude?: string\n}\n\n/**\n * Build the system + user messages for the in-process extraction call.\n * The system part instructs the model to return a JSON array restricted to\n * EXTRACTABLE_TYPES; the user part carries the transcript.\n */\nexport function buildExtractionPrompt(\n project: string,\n transcript: string,\n contextFiles: DigestContextFiles = {},\n): { system: string; user: string } {\n const sysParts: string[] = [\n 'You are an assistant that extracts durable knowledge from a session transcript.',\n ]\n if (contextFiles.criticalFacts?.trim()) {\n sysParts.push(`Always-loaded context: CRITICAL_FACTS.md\\n${contextFiles.criticalFacts.trim()}`)\n }\n if (contextFiles.claude?.trim()) {\n sysParts.push(`Always-loaded context: _CLAUDE.md\\n${contextFiles.claude.trim()}`)\n }\n sysParts.push(\n `For the transcript of project \\`${project}\\`, identify:`,\n '- **Decisions**: architectural or design choices that were made.',\n '- **Facts**: stable, verifiable statements about the project (versions, conventions, constraints).',\n '- **Learnings**: non-obvious lessons, debugging insights, or solutions found.',\n '- **Conventions**: style rules, naming patterns, coding conventions agreed.',\n '',\n 'Return a JSON array. Each element must have exactly:',\n ' - \"entry_type\": one of \"decision\" | \"fact\" | \"learning\" | \"convention\"',\n ' - \"content\": a single-paragraph statement (no headings, no lists)',\n ' - \"description\": a one-sentence summary of `content` (queryable)',\n ' - \"tags\": an array of lowercase-kebab tags (never empty if possible, at least 1 like architecture/python/testing)',\n ' - \"confidence\": a number 0.0-1.0',\n ' - \"openspec_change_id\": (optional) the change slug if the transcript names it',\n '',\n 'Rules:',\n '- Skip trivial exchanges (greetings, \"ok\", \"thanks\", or anything with no project knowledge).',\n '- Prefer fewer, higher-signal entries over many weak ones.',\n '- Do not include anything not present in the transcript.',\n '',\n 'Return only the JSON array. No prose, no markdown fences.',\n )\n return {\n system: sysParts.join('\\n'),\n user: `Project: ${project}\\n\\nTranscript:\\n---\\n${transcript}\\n---`,\n }\n}\n\n/**\n * Split an oversized transcript into overlapping chunks (mirrors the legacy\n * digest script: 25k chars per chunk, 1k overlap, at most `cap` chunks).\n */\nexport function chunkTranscript(text: string, maxLen = 50_000, chunkSize = 25_000, overlap = 1_000, cap = 3): string[] {\n if (text.length <= maxLen) return [text]\n const chunks: string[] = []\n let i = 0\n while (i < text.length) {\n chunks.push(text.slice(i, i + chunkSize))\n i += chunkSize - overlap\n if (chunks.length >= cap) break\n }\n return chunks\n}\n\n/** Best-effort repair of common LLM JSON output; returns parsed value or null. */\nexport function repairJson(text: string): unknown {\n const s = text.trim()\n // Strip code fences: ```json ... ```\n const fenced = s.replace(/^```(?:json)?\\s*/i, '').replace(/\\s*```$/, '').trim()\n // Trailing commas: `,]` / `,}` -> `]` / `}`\n const noTrailing = fenced.replace(/,(\\s*[\\]}])/g, '$1')\n const candidates = [s, fenced, noTrailing]\n // Single-quote to double-quote conversion only when no double quotes exist.\n if (noTrailing.includes(\"'\") && !noTrailing.includes('\"')) {\n candidates.push(convertSingleQuotes(noTrailing))\n }\n for (const c of candidates) {\n try {\n return JSON.parse(c)\n } catch {\n // try the next candidate\n }\n }\n return null\n}\n\nfunction convertSingleQuotes(text: string): string {\n let out = ''\n let inString = false\n let i = 0\n while (i < text.length) {\n const ch = text[i]\n if (inString && ch === '\\\\') {\n if (i + 1 < text.length && text[i + 1] === \"'\") {\n out += \"'\"\n i += 2\n continue\n }\n out += ch\n if (i + 1 < text.length) {\n out += text[i + 1]\n i += 2\n continue\n }\n i += 1\n continue\n }\n if (ch === \"'\") {\n inString = !inString\n out += '\"'\n i += 1\n continue\n }\n out += ch\n i += 1\n }\n return out\n}\n\n/**\n * Validate a parsed extraction payload into OKF entries. Unknown types,\n * empty content and malformed values are dropped; tags and confidence are\n * normalized. Returns the valid entries (possibly empty).\n */\nexport function validateEntries(payload: unknown): ValidEntry[] {\n if (!Array.isArray(payload)) return []\n const valid: ValidEntry[] = []\n for (const item of payload) {\n if (typeof item !== 'object' || item === null) continue\n const raw = item as Record<string, unknown>\n const entryType = raw.entry_type\n if (typeof entryType !== 'string' || !(EXTRACTABLE_TYPES as readonly string[]).includes(entryType)) continue\n const content = typeof raw.content === 'string' ? raw.content.trim() : ''\n if (!content) continue\n const tags = Array.isArray(raw.tags) ? raw.tags.filter((t): t is string => typeof t === 'string' && t.length > 0) : []\n let confidence = 1\n if (typeof raw.confidence === 'number' && Number.isFinite(raw.confidence)) {\n confidence = Math.max(0, Math.min(1, raw.confidence))\n }\n const description = typeof raw.description === 'string' ? raw.description.trim() : ''\n const changeId = typeof raw.openspec_change_id === 'string' && raw.openspec_change_id ? raw.openspec_change_id : null\n valid.push({\n entry_type: entryType as ExtractableType,\n content,\n description,\n tags,\n confidence,\n openspec_change_id: changeId,\n })\n }\n return valid\n}\n\n// The full hook wiring is exposed for testing; the actual OpenCode integration\n// is done in `register.ts` (the entry point the OpenCode runtime loads via\n// package.json \"main\"). This module intentionally has no default export:\n// opencode 1.18.x only loads plugin modules with a single export.\nexport const __testing = {\n createSessionState,\n isSupportedVersion,\n resolveProjectName,\n idleCheckpoint,\n compactingCheckpoint,\n isGitCommit,\n buildCheckpointPrompt,\n buildCommitCheckpointPrompt,\n};\n","import { spawn, type ChildProcess } from 'node:child_process'\nimport { existsSync } from 'node:fs'\nimport { join } from 'node:path'\nimport readline from 'node:readline'\nimport { BlockAssembler, createUserMessage } from '@deepseek-ai/dsh-llm'\nimport type { FinishReason, GenerateOptions, Message } from '@deepseek-ai/dsh-llm'\nimport type { Context } from '@deepseek-ai/cordis'\nimport {\n buildExtractionPrompt,\n chunkTranscript,\n repairJson,\n validateEntries,\n type ValidEntry,\n} from './pure.js'\n\n// Post-session digest runner: in-process LLM extraction via the harness's own\n// `ctx.llm` service (no external CLI, no credentials of its own), followed by\n// OKF writes through the vault's MCP server over stdio (`store_*` tools keep\n// the Markdown source of truth and the SQLite FTS5 index in sync).\n\nconst MIN_DIGEST_TRANSCRIPT_CHARS = 200\nconst LLM_RETRIES = 3\nconst LLM_RETRY_DELAYS_MS = [2_000, 5_000]\nconst MCP_CALL_TIMEOUT_MS = 60_000\n\nfunction log(msg: string) {\n console.log(`[memory-auto] ${msg}`)\n}\n\nexport function transcriptOfDSM(events: any[]): string {\n // DSH SessionEvent -> transcript\n return (events ?? [])\n .map((ev: any) => {\n const t = ev?.type ?? ''\n const d = ev?.data ?? ev\n if (t === 'user/message' || t === 'user_message') {\n const text = typeof d.text === 'string' ? d.text : typeof d.content === 'string' ? d.content : JSON.stringify(d).slice(0, 500)\n return `## user\\n${text}`\n }\n if (t === 'assistant/message' || t === 'assistant_message') {\n const text = typeof d.text === 'string' ? d.text : typeof d.content === 'string' ? d.content : ''\n return text ? `## assistant\\n${text}` : null\n }\n if (t === 'tool/call' || t === 'tool_call') {\n const name = d.tool ?? d.name ?? 'tool'\n const args = d.args ?? d.arguments ?? {}\n return `## tool_call ${name}\\n${JSON.stringify(args).slice(0, 1000)}`\n }\n if (t === 'tool/result' || t === 'tool_result') {\n const out = typeof d.output === 'string' ? d.output : JSON.stringify(d).slice(0, 1000)\n return `## tool_result\\n${out}`\n }\n if (t.startsWith('compaction')) return `## ${t}\\n${JSON.stringify(d).slice(0, 500)}`\n return null\n })\n .filter((x): x is string => Boolean(x))\n .join('\\n\\n')\n}\n\n/** Translate a terminal stream finish into a thrown error, mirroring dsh's own summarizers. */\nfunction finishError(finish: FinishReason): Error | undefined {\n switch (finish.kind) {\n case 'error':\n case 'aborted': {\n const error = new Error(finish.failure.message) as Error & { code?: string }\n error.code = finish.failure.code\n return error\n }\n default:\n return undefined\n }\n}\n\nexport interface DigestConfig {\n memoryPath: string\n serverDir: string\n provider: string\n model: string\n maxTokens: number\n minTranscriptChars: number\n}\n\n/**\n * Run one extraction call through the harness's `ctx.llm` service: build the\n * prompt, stream, assemble, repair and validate the JSON entries.\n */\nexport async function extractEntriesWithLlm(\n ctx: Context,\n config: DigestConfig,\n project: string,\n transcript: string,\n contextFiles?: { criticalFacts?: string; claude?: string },\n signal?: AbortSignal,\n): Promise<ValidEntry[]> {\n const { system, user } = buildExtractionPrompt(project, transcript, contextFiles)\n const assembler = new BlockAssembler()\n const messages: Message[] = [\n createUserMessage({\n content: [{ type: 'text', text: user }],\n source: { kind: 'plugin', plugin: 'memory-auto' },\n }),\n ]\n const options: GenerateOptions = {\n provider: config.provider,\n model: config.model,\n messages,\n system,\n maxTokens: config.maxTokens,\n ...(signal === undefined ? {} : { signal }),\n }\n for await (const chunk of ctx.llm.stream(options)) assembler.push(chunk)\n const error = finishError(assembler.finish)\n if (error !== undefined) throw error\n const text = assembler\n .blocks()\n .filter((b) => b.type === 'text')\n .map((b) => b.text)\n .join('')\n const parsed = repairJson(text)\n return validateEntries(parsed)\n}\n\n/** Minimal MCP stdio client for the vault server (spawned via its launcher: uv, pip venv fallback). */\nexport interface McpClient {\n callTool(name: string, args: Record<string, unknown>, timeoutMs?: number): Promise<unknown>\n close(): Promise<void>\n}\n\nexport function connectMcp(memoryPath: string, serverDir: string): Promise<McpClient> {\n return new Promise((resolve, reject) => {\n // Single decision point: the server bundle's launcher.mjs picks `uv run`\n // or a pip-managed .venv. Legacy hand-made server dirs without a launcher\n // keep the old direct `uv run` spawn.\n const launcher = join(serverDir, 'launcher.mjs')\n const [command, args] = existsSync(launcher)\n ? [process.execPath, [launcher]]\n : ['uv', ['run', '--directory', serverDir, 'python', 'server.py']]\n const child: ChildProcess = spawn(\n command,\n args,\n {\n env: { ...process.env, MEMORY_PATH: memoryPath, UV_CACHE_DIR: process.env.UV_CACHE_DIR ?? '/tmp/uv-cache' },\n stdio: ['pipe', 'pipe', 'inherit'],\n },\n )\n const pending = new Map<number, { resolve: (v: unknown) => void; reject: (e: Error) => void; timer: NodeJS.Timeout }>()\n let nextId = 1\n let closed = false\n\n const failAll = (err: Error) => {\n for (const [, p] of pending) {\n clearTimeout(p.timer)\n p.reject(err)\n }\n pending.clear()\n }\n\n child.on('error', (err) => {\n closed = true\n failAll(new Error(`memory-vault-server spawn failed: ${err.message}`))\n reject(err)\n })\n child.on('exit', (code) => {\n if (closed) return\n closed = true\n failAll(new Error(`memory-vault-server exited unexpectedly (code ${code})`))\n reject(new Error(`memory-vault-server exited before initialize (code ${code})`))\n })\n\n const rl = readline.createInterface({ input: child.stdout!, crlfDelay: Infinity })\n rl.on('line', (line) => {\n let msg: any\n try {\n msg = JSON.parse(line)\n } catch {\n return\n }\n if (typeof msg?.id === 'number') {\n const p = pending.get(msg.id)\n if (!p) return\n pending.delete(msg.id)\n clearTimeout(p.timer)\n if (msg.error) p.reject(new Error(`MCP error: ${msg.error.message ?? JSON.stringify(msg.error)}`))\n else p.resolve(msg.result)\n }\n })\n\n const send = (method: string, params: unknown, timeoutMs: number = MCP_CALL_TIMEOUT_MS): Promise<unknown> =>\n new Promise((res, rej) => {\n if (closed || !child.stdin?.writable) {\n rej(new Error('memory-vault-server is not running'))\n return\n }\n const id = nextId++\n const timer = setTimeout(() => {\n pending.delete(id)\n rej(new Error(`MCP call ${method} timed out after ${timeoutMs}ms`))\n }, timeoutMs)\n pending.set(id, { resolve: res, reject: rej, timer })\n child.stdin!.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\\n')\n })\n\n const notify = (method: string, params: unknown) => {\n if (!closed && child.stdin?.writable) {\n child.stdin.write(JSON.stringify({ jsonrpc: '2.0', method, params }) + '\\n')\n }\n }\n\n // initialize handshake (notifications are fire-and-forget: no id, no reply)\n void send('initialize', {\n protocolVersion: '2025-06-18',\n capabilities: {},\n clientInfo: { name: 'memory-auto', version: '0.1.0' },\n })\n .then(() => {\n notify('notifications/initialized', {})\n })\n .then(() => {\n if (closed) throw new Error('memory-vault-server closed during handshake')\n resolve({\n callTool: (name, args, timeoutMs = MCP_CALL_TIMEOUT_MS) =>\n send('tools/call', { name, arguments: args }, timeoutMs).then((result: any) => {\n if (result?.isError) {\n const text = Array.isArray(result.content) ? result.content.map((c: any) => c?.text ?? '').join('') : JSON.stringify(result)\n throw new Error(`tool ${name} failed: ${text}`)\n }\n return result\n }),\n close: async () => {\n if (closed) return\n closed = true\n for (const [, p] of pending) clearTimeout(p.timer)\n pending.clear()\n if (child.exitCode !== null) return\n child.kill()\n await new Promise((r) => child.once('exit', r))\n },\n })\n })\n .catch((err) => {\n closed = true\n child.kill()\n reject(err)\n })\n })\n}\n\n/**\n * Write validated OKF entries through the vault server's `store_*` tools,\n * which upsert both the Markdown file and the SQLite FTS5 index.\n */\nexport async function writeEntries(client: McpClient, project: string, entries: ValidEntry[]): Promise<{ upserted: number; failed: number }> {\n let upserted = 0\n let failed = 0\n for (const e of entries) {\n try {\n await client.callTool(`store_${e.entry_type}`, {\n project,\n content: e.content,\n ...(e.description ? { description: e.description } : {}),\n tags: e.tags,\n confidence: e.confidence,\n ...(e.openspec_change_id ? { openspec_change_id: e.openspec_change_id } : {}),\n })\n upserted += 1\n } catch (err) {\n failed += 1\n console.warn(`[memory-auto] store_${e.entry_type} failed:`, err instanceof Error ? err.message : err)\n }\n }\n return { upserted, failed }\n}\n\n/**\n * Full post-session digest: transcript -> ctx.llm extraction (with retries) ->\n * OKF writes via the vault MCP server. Never throws; logs the outcome.\n */\nexport async function digestSessionDSM(\n ctx: Context,\n config: DigestConfig,\n sessionId: string,\n directory: string,\n project: string,\n events: any[],\n signal?: AbortSignal,\n): Promise<void> {\n const header = `## context\\nproject: ${project}\\ndirectory: ${directory}\\n`\n const transcript = (header + transcriptOfDSM(events)).trim()\n if (!transcript) {\n log(`digest skip ${sessionId}: empty`)\n return\n }\n if (transcript.length < (config.minTranscriptChars ?? MIN_DIGEST_TRANSCRIPT_CHARS)) {\n log(`digest skip ${sessionId}: too short ${transcript.length}`)\n return\n }\n\n const chunks = chunkTranscript(transcript)\n const entries: ValidEntry[] = []\n for (const chunk of chunks) {\n let attempt = 0\n for (;;) {\n try {\n const got = await extractEntriesWithLlm(ctx, config, project, chunk, undefined, signal)\n entries.push(...got)\n break\n } catch (err) {\n attempt += 1\n if (attempt >= LLM_RETRIES || signal?.aborted) {\n console.warn(`[memory-auto] digest extraction failed after ${attempt} attempt(s):`, err instanceof Error ? err.message : err)\n break\n }\n const delay = LLM_RETRY_DELAYS_MS[attempt - 1] ?? 5_000\n log(`digest extraction retry ${attempt}/${LLM_RETRIES} in ${delay}ms`)\n await new Promise((r) => setTimeout(r, delay))\n }\n }\n if (signal?.aborted) break\n }\n\n if (entries.length === 0) {\n log(`digest ${sessionId}: no entries extracted`)\n return\n }\n\n let client: McpClient\n try {\n client = await connectMcp(config.memoryPath, config.serverDir)\n } catch (err) {\n console.warn(`[memory-auto] digest ${sessionId}: cannot reach vault server:`, err instanceof Error ? err.message : err)\n return\n }\n try {\n const { upserted, failed } = await writeEntries(client, project, entries)\n log(`digest ${sessionId}: ${upserted} upserted, ${failed} failed (${entries.length} extracted)`)\n } finally {\n await client.close().catch(() => {})\n }\n}\n","import { cpSync, existsSync, mkdirSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { dirname, isAbsolute, join } from 'node:path'\nimport { fileURLToPath } from 'node:url'\nimport type { Context } from '@deepseek-ai/cordis'\nimport Schema from '@deepseek-ai/schemastery'\nimport {\n createSessionState,\n resolveProjectName,\n idleCheckpoint,\n compactingCheckpoint,\n isGitCommit,\n buildCheckpointPrompt,\n buildCommitCheckpointPrompt,\n type SessionState,\n} from './pure.js'\nimport { digestSessionDSM, type DigestConfig } from './digest.js'\n\nexport const name = 'memory-auto'\n\nexport interface Config {\n memoryPath: string\n serverDir: string\n provider: string\n model: string\n maxTokens: number\n minTranscriptChars: number\n enabled: boolean\n}\n\nexport const Config: Schema<Config> = Schema.object({\n memoryPath: Schema.string().default(process.env.DSH_MEMORY_PATH ?? ''),\n serverDir: Schema.string().default(process.env.DSH_MEMORY_SERVER_DIR ?? ''),\n provider: Schema.string().default('deepseek-official'),\n model: Schema.string().default('deepseek-v4-flash'),\n maxTokens: Schema.number().default(2048),\n minTranscriptChars: Schema.number().default(200),\n enabled: Schema.boolean().default(true),\n})\n\n/** Requires the harness LLM service: extraction runs in-process via ctx.llm. */\nexport const inject = ['llm']\n\n/**\n * Resolve the harness home the same way the harness does (`$DSH_HOME`, or\n * `~/.dsh`). Paths must never depend on the launch cwd: DSH does not chdir.\n */\nfunction dshHome(): string {\n const env = process.env.DSH_HOME?.trim()\n return env && env.length > 0 ? env : join(homedir(), '.dsh')\n}\n\n/** Absolute paths stay; empty/relative values resolve under the harness home. */\nfunction resolveUnderHome(value: string, fallbackSegment: string): string {\n const v = value.trim()\n if (v.length === 0) return join(dshHome(), fallbackSegment)\n return isAbsolute(v) ? v : join(dshHome(), v)\n}\n\nconst packageRoot = dirname(dirname(fileURLToPath(import.meta.url)))\n\n/** Copy the bundled dir into `target` when `key` is missing there. */\nfunction ensure(target: string, bundled: string, key: string): boolean {\n if (existsSync(join(target, key))) return false\n if (!existsSync(bundled)) return false\n mkdirSync(target, { recursive: true })\n cpSync(bundled, target, { recursive: true })\n return true\n}\n\n/** Copy one bundled file into `target` when missing (upgrades add files 0.1.1 → 0.1.2). */\nfunction ensureFile(target: string, bundled: string, file: string): boolean {\n const dest = join(target, file)\n if (existsSync(dest)) return false\n const src = join(bundled, file)\n if (!existsSync(src)) return false\n mkdirSync(target, { recursive: true })\n cpSync(src, dest)\n return true\n}\n\n// DSH session shape minimal\ntype DSHEvt = any\ntype DSHSession = { id: string; events: DSHEvt[]; cwd?: string }\n\nexport function apply(ctx: Context, config: Config) {\n if (!config.enabled) {\n console.log('[memory-auto] disabled via config')\n return\n }\n\n const memoryPath = resolveUnderHome(config.memoryPath, 'memory-vault')\n const serverDir = resolveUnderHome(config.serverDir, 'memory-vault-server')\n\n // Self-contained install: first boot copies the bundled server and vault\n // starter under the harness home when they are missing.\n if (ensure(serverDir, join(packageRoot, 'server'), 'server.py')) {\n console.log(`[memory-auto] installed memory-vault-server -> ${serverDir}`)\n }\n if (ensure(memoryPath, join(packageRoot, 'vault'), 'type-registry.yaml')) {\n console.log(`[memory-auto] installed vault starter -> ${memoryPath}`)\n }\n // launcher.mjs runs the server via uv or the pip-venv fallback; upgrades of\n // existing installs (server.py already present) still need the new files.\n const bundledServer = join(packageRoot, 'server')\n for (const file of ['launcher.mjs', 'requirements.txt']) {\n if (ensureFile(serverDir, bundledServer, file)) {\n console.log(`[memory-auto] installed ${file} -> ${serverDir}`)\n }\n }\n\n const digestConfig: DigestConfig = {\n memoryPath,\n serverDir,\n provider: config.provider,\n model: config.model,\n maxTokens: config.maxTokens,\n minTranscriptChars: config.minTranscriptChars,\n }\n\n if (!existsSync(join(serverDir, 'server.py'))) {\n console.warn(\n `[memory-auto] vault server not found at ${serverDir} and not bundled — the digest cannot write. ` +\n 'Set DSH_MEMORY_SERVER_DIR (or run `node scripts/bundle-assets.mjs` in a checkout).',\n )\n }\n if (!existsSync(join(memoryPath, 'type-registry.yaml'))) {\n console.warn(`[memory-auto] vault starter not found at ${memoryPath} — searches will fail until it exists.`)\n }\n\n const states = new Map<string, SessionState>()\n const activities = new Map<string, string[]>()\n const queued = new Map<string, string>()\n const sessionDirs = new Map<string, string>()\n const projectCache = new Map<string, string>()\n\n const ACTIVITY_MAX_LINES = 20\n const ACTIVITY_MAX_CHARS = 80\n\n const track = (sessionId: string, line: string) => {\n const st = states.get(sessionId)\n if (!st) return\n st.hasActivity = true\n const list = activities.get(sessionId) ?? []\n if (!list.includes(line)) {\n list.push(line)\n if (list.length > ACTIVITY_MAX_LINES) list.shift()\n activities.set(sessionId, list)\n }\n }\n\n const summary = (sid: string) => (activities.get(sid) ?? []).join('\\n')\n\n const projectFor = async (dir: string): Promise<string> => {\n const cached = projectCache.get(dir)\n if (cached) return cached\n const p = await resolveProjectName(dir || process.cwd())\n projectCache.set(dir, p)\n return p\n }\n\n // session/created -> init state\n ctx.on('session/created', async (session: DSHSession) => {\n const sid = (session as any)?.id ?? (session as any)?.sessionId\n const dir = (session as any)?.cwd ?? (session as any)?.directory ?? ''\n if (!sid) return\n const proj = await projectFor(dir)\n if (!states.has(sid)) {\n states.set(sid, createSessionState(proj))\n activities.set(sid, [])\n }\n sessionDirs.set(sid, dir)\n console.log(`[memory-auto] session created ${sid} project=${proj}`)\n })\n\n // session/disposed -> digest\n ctx.on('session/disposed', async (session: DSHSession) => {\n const sid = (session as any)?.id ?? (session as any)?.sessionId\n if (!sid) return\n const st = states.get(sid)\n if (!st) return\n if (!st.hasActivity) {\n console.log(`[memory-auto] digest skip ${sid}: no activity`)\n return\n }\n const dir = sessionDirs.get(sid) ?? ''\n const proj = await projectFor(dir)\n const evts: DSHEvt[] = (session as any)?.events ?? []\n await digestSessionDSM(ctx, digestConfig, sid, dir, proj, evts)\n })\n\n // agent/status idle -> in-session capture gate\n ctx.on('agent/status', async (payload: any) => {\n const agent = payload?.agent\n const status = payload?.status ?? payload?.agentStatus\n if (status !== 'idle') return\n const sid: string | undefined = agent?.sessionId ?? payload?.sessionId ?? agent?.id\n if (!sid) return\n const st = states.get(sid)\n if (!st) return\n const text = idleCheckpoint(st, summary(sid))\n if (text) {\n console.log(`[memory-auto] idle checkpoint digest for ${sid}`)\n const dir = sessionDirs.get(sid) ?? ''\n const proj = await projectFor(dir)\n const fakeEvents = [{ type: 'user/message', data: { text: summary(sid) } }]\n await digestSessionDSM(ctx, digestConfig, sid, dir, proj, fakeEvents)\n }\n })\n\n // session/event -> git commit detect + compaction start\n ctx.on('session/event', async (session: DSHSession, event: DSHEvt) => {\n const sid = (session as any)?.id ?? (session as any)?.sessionId ?? (event as any)?.sessionId\n if (!sid) return\n const t = event?.type ?? ''\n const d = event?.data ?? {}\n\n // compaction/start -> checkpoint injection (delivered on the next pre-step)\n if (t === 'compaction/start') {\n const st = states.get(sid)\n if (!st) return\n const txt = compactingCheckpoint(st, summary(sid))\n if (txt) {\n console.log(`[memory-auto] compaction checkpoint queued for ${sid}`)\n queued.set(sid, txt)\n }\n return\n }\n\n // tool/call -> git commit detection + activity track\n if (t === 'tool/call' || t === 'tool_call') {\n const cmd = d?.args?.command ?? d?.command ?? ''\n if (typeof cmd === 'string' && isGitCommit(cmd)) {\n const st = states.get(sid)\n if (st) {\n st.hasActivity = true\n queued.set(sid, buildCommitCheckpointPrompt(st))\n console.log(`[memory-auto] git commit queued for ${sid}`)\n }\n return\n }\n if (typeof cmd === 'string') {\n track(sid, `bash: ${cmd.trim().slice(0, ACTIVITY_MAX_CHARS)}`)\n }\n return\n }\n if (t === 'tool/result') {\n // ignore\n return\n }\n if (t === 'user/message' || t === 'assistant/message') {\n // track activity\n track(sid, `${t}: ${(d?.text ?? '').slice(0, ACTIVITY_MAX_CHARS)}`)\n }\n })\n\n // agent/pre-step Waterfall -> deliver queued checkpoint\n ctx.on('agent/pre-step', async (payload: any, next: any) => {\n const sid: string | undefined = payload?.agent?.sessionId ?? payload?.sessionId\n if (sid) {\n const q = queued.get(sid)\n if (q) {\n queued.delete(sid)\n // Best effort: append the checkpoint as user context\n if (Array.isArray(payload?.context)) payload.context.push(q)\n else if (Array.isArray(payload?.messages)) payload.messages.push({ role: 'user', content: q })\n else console.log(`[memory-auto] deliver queued checkpoint for ${sid}`)\n }\n }\n return next()\n })\n\n // dispose -> batch digest remaining sessions\n ctx.effect(() => {\n return () => {\n console.log(`[memory-auto] dispose batch ${sessionDirs.size} sessions`)\n // fire-and-forget digest for each remaining session\n for (const [sid, dir] of sessionDirs) {\n const st = states.get(sid)\n if (!st?.hasActivity) continue\n projectFor(dir).then((proj) => {\n const fakeEvents = [{ type: 'user/message', data: { text: summary(sid) } }]\n void digestSessionDSM(ctx, digestConfig, sid, dir, proj, fakeEvents)\n })\n }\n }\n })\n\n console.log(`[memory-auto] active memoryPath=${memoryPath} serverDir=${serverDir} llm=${config.provider}/${config.model}`)\n}\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,OAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,GAAG,mBAAmB,GAAG;AAEhE;;AAEF,MAAI,MAAM,cAER;AAEF,MAAI,MAAM,kBAAkB,MAAM,oBAEhC,OAAM,KAAK,GAAG,EAAE,KAAK,GAAG,QAAQ,IAAI,MAAM,GAAG,mBAAmB,GAAG;GAErE;AAGF,KAAI,GAAG,kBAAkB,OAAO,SAAc,SAAc;EAC1D,MAAMA,MAA0B,SAAS,OAAO,aAAa,SAAS;AACtE,MAAI,KAAK;GACP,MAAM,IAAI,OAAO,IAAI,IAAI;AACzB,OAAI,GAAG;AACL,WAAO,OAAO,IAAI;AAElB,QAAI,MAAM,QAAQ,SAAS,QAAQ,CAAE,SAAQ,QAAQ,KAAK,EAAE;aACnD,MAAM,QAAQ,SAAS,SAAS,CAAE,SAAQ,SAAS,KAAK;KAAE,MAAM;KAAQ,SAAS;KAAG,CAAC;QACzF,SAAQ,IAAI,+CAA+C,MAAM;;;AAG1E,SAAO,MAAM;GACb;AAGF,KAAI,aAAa;AACf,eAAa;AACX,WAAQ,IAAI,+BAA+B,YAAY,KAAK,WAAW;AAEvE,QAAK,MAAM,CAAC,KAAK,QAAQ,aAAa;AAEpC,QAAI,CADO,OAAO,IAAI,IAAI,EACjB,YAAa;AACtB,eAAW,IAAI,CAAC,MAAM,SAAS;AAE7B,KAAK,iBAAiB,KAAK,cAAc,KAAK,KAAK,MADhC,CAAC;MAAE,MAAM;MAAgB,MAAM,EAAE,MAAM,QAAQ,IAAI,EAAE;MAAE,CAAC,CACP;MACpE;;;GAGN;AAEF,SAAQ,IAAI,mCAAmC,WAAW,aAAa,UAAU,OAAO,OAAO,SAAS,GAAG,OAAO,QAAQ"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/pure.ts","../src/digest.ts","../src/plugin.ts"],"sourcesContent":["/**\n * DSH memory plugin (adapted from the OpenCode memory plugin).\n *\n * Pure helpers only: prompt builders, transcript chunking, JSON repair and\n * entry validation. No I/O and no harness wiring — `plugin.ts` owns the hook\n * registration and `digest.ts` owns the LLM call and vault writes.\n */\n\nimport { readFile } from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport { join, basename } from \"node:path\";\n\n// ── Constants ────────────────────────────────────────────────────────────\n\nexport const MIN_OPENCODE_VERSION = \"1.17.10\";\nexport const MCP_UNREACHABLE =\n \"> ⚠️ Memory server unreachable — search cannot be completed.\";\nconst CHECKPOINT_MARKER = \"[memory-checkpoint]\";\n\n/**\n * Entry types the vault's MCP server can store via `store_*` tools. The\n * extraction prompt is restricted to these so every produced entry has a\n * write path (no `idea`/`context`/`source` — those have no store tool).\n */\nexport const EXTRACTABLE_TYPES = ['decision', 'fact', 'learning', 'convention'] as const\nexport type ExtractableType = (typeof EXTRACTABLE_TYPES)[number]\n\n/**\n * Shared entry vocabulary: one definition list, used by BOTH the internal\n * extraction prompt and the agent-facing checkpoint prompt. Keeping one source\n * stops the two from drifting, which is how the checkpoint prompt ended up\n * asking agents to write entries it never defined.\n */\nconst ENTRY_TYPE_GLOSS: Record<ExtractableType, string> = {\n decision: \"architectural or design choices that were made\",\n fact: \"stable, verifiable statements about the project (versions, constraints)\",\n learning: \"non-obvious lessons, debugging insights, or solutions found\",\n convention: \"style rules, naming patterns, coding conventions agreed\",\n}\n\n/** Content shape, quoted by both prompts. */\nconst ENTRY_CONTENT_RULE =\n \"a single paragraph — no headings, no bullet lists, no markdown structure\"\n\n/**\n * What \"notable\" means, shared by both prompts. The extraction model is told\n * these rules; the agent writing checkpoints needs them just as much.\n */\nconst ENTRY_SELECTION_RULES = [\n \"Skip trivia (greetings, \\\"ok\\\", \\\"thanks\\\", restating the request).\",\n \"Prefer fewer, high-signal entries over many weak ones.\",\n \"Do not record anything you cannot ground in this session's activity.\",\n] as const\n\n/** Entry-type clauses as bullet lines, in EXTRACTABLE_TYPES order. */\nfunction entryTypeBullets(): string[] {\n return EXTRACTABLE_TYPES.map((t) => `- **${t}**: ${ENTRY_TYPE_GLOSS[t]}.`)\n}\n\n\n// ── Version guard ────────────────────────────────────────────────────────\n\n/** Compare two \"x.y.z\" semver strings. Returns negative/0/positive. */\nfunction compareSemver(a: string, b: string): number {\n const [a1, a2, a3] = a.split(\".\").map((n) => parseInt(n, 10) || 0);\n const [b1, b2, b3] = b.split(\".\").map((n) => parseInt(n, 10) || 0);\n if (a1 !== b1) return a1 - b1;\n if (a2 !== b2) return a2 - b2;\n return (a3 || 0) - (b3 || 0);\n}\n\nexport function isSupportedVersion(version: string): boolean {\n return compareSemver(version, MIN_OPENCODE_VERSION) >= 0;\n}\n\n// ── Project name resolution ──────────────────────────────────────────────\n\n/**\n * Resolve the project name from a working directory.\n * Priority:\n * 1. OpenSpec presence: if `openspec/` exists, use the basename.\n * 2. package.json -> name\n * 3. pyproject.toml -> [project] -> name\n * 4. README.md: first 5 lines, heading pattern `# <ProjectName>`\n * 5. Fallback: basename of working directory\n */\nexport async function resolveProjectName(cwd: string): Promise<string> {\n if (existsSync(join(cwd, \"openspec\"))) {\n return basename(cwd);\n }\n // package.json\n const pkgPath = join(cwd, \"package.json\");\n if (existsSync(pkgPath)) {\n try {\n const pkg = JSON.parse(await readFile(pkgPath, \"utf-8\"));\n if (typeof pkg.name === \"string\" && pkg.name.trim()) {\n return pkg.name.trim();\n }\n } catch {\n // ignore parse errors; try next strategy\n }\n }\n // pyproject.toml (minimal regex parse)\n const pyprojectPath = join(cwd, \"pyproject.toml\");\n if (existsSync(pyprojectPath)) {\n try {\n const text = await readFile(pyprojectPath, \"utf-8\");\n const m = text.match(/\\[project\\][^\\[]*?name\\s*=\\s*[\"']([^\"']+)[\"']/);\n if (m) return m[1];\n } catch {\n // ignore\n }\n }\n // README.md heading\n const readmePath = join(cwd, \"README.md\");\n if (existsSync(readmePath)) {\n try {\n const text = await readFile(readmePath, \"utf-8\");\n const head = text.split(\"\\n\").slice(0, 5);\n for (const line of head) {\n const m = line.match(/^#\\s+(.+)$/);\n if (m) return m[1].trim();\n }\n } catch {\n // ignore\n }\n }\n return basename(cwd);\n}\n\n// ── Checkpoint state ─────────────────────────────────────────────────────\n\nexport interface SessionState {\n project: string;\n hasActivity: boolean;\n checkpointDelivered: boolean;\n queuedCheckpoint: string | null;\n}\n\nexport function createSessionState(project: string): SessionState {\n return {\n project,\n hasActivity: false,\n checkpointDelivered: false,\n queuedCheckpoint: null,\n };\n}\n\n/**\n * Build the checkpoint prompt body. Pure function — exported for testing.\n */\nexport function buildCheckpointPrompt(state: SessionState, activitySummary: string): string {\n return [\n `${CHECKPOINT_MARKER} End-of-session memory capture for project \\`${state.project}\\`.`,\n \"\",\n \"Tracked activity:\",\n activitySummary.trim() || \"(none recorded)\",\n \"\",\n \"Write OKF entries for anything notable using the `store_*` MCP tools.\",\n \"Entry types:\",\n ...entryTypeBullets(),\n \"\",\n `Set \\`content\\` to ${ENTRY_CONTENT_RULE}, and \\`description\\` to a one-sentence summary.`,\n \"Tag with lowercase-kebab tags (at least one, e.g. architecture/python/testing).\",\n ...ENTRY_SELECTION_RULES,\n \"If nothing is notable, say so explicitly and exit.\",\n ].join(\"\\n\");\n}\n\n/**\n * Decide whether to deliver a checkpoint on `session.idle`.\n * Returns the prompt to deliver, or null to skip.\n */\nexport function idleCheckpoint(\n state: SessionState,\n activitySummary: string,\n): string | null {\n if (!state.hasActivity) return null;\n if (state.checkpointDelivered) return null;\n state.checkpointDelivered = true;\n return buildCheckpointPrompt(state, activitySummary);\n}\n\n/**\n * Decide whether to fire on `experimental.session.compacting`.\n * Per spec: always fires when activity exists, even if checkpoint was delivered.\n */\nexport function compactingCheckpoint(\n state: SessionState,\n activitySummary: string,\n): string | null {\n if (!state.hasActivity) return null;\n return buildCheckpointPrompt(state, activitySummary);\n}\n\n// ── Git commit detection ─────────────────────────────────────────────────\n\nconst GIT_COMMIT_PATTERN = /git\\s+commit\\b/;\n\nexport function isGitCommit(command: string): boolean {\n return GIT_COMMIT_PATTERN.test(command);\n}\n\nexport function buildCommitCheckpointPrompt(state: SessionState): string {\n return [\n `${CHECKPOINT_MARKER} Memory capture after \\`git commit\\` in project \\`${state.project}\\`.`,\n \"\",\n \"Review the staged/committed changes and write OKF entries through the `store_*` MCP tools.\",\n \"Entry types:\",\n ...entryTypeBullets(),\n \"\",\n `Set \\`content\\` to ${ENTRY_CONTENT_RULE}, and \\`description\\` to a one-sentence summary.`,\n \"Tag with lowercase-kebab tags (at least one, e.g. architecture/python/testing).\",\n ...ENTRY_SELECTION_RULES,\n \"If nothing is notable, say so explicitly and exit.\",\n ].join(\"\\n\");\n}\n\n// ── In-process session digest (ctx.llm) ──────────────────────────────────\n\n/** One validated OKF entry ready to be stored through the vault server. */\nexport interface ValidEntry {\n entry_type: ExtractableType\n content: string\n description: string\n tags: string[]\n confidence: number\n openspec_change_id: string | null\n}\n\n/** Optional vault context files to embed in the extraction prompt. */\nexport interface DigestContextFiles {\n criticalFacts?: string\n claude?: string\n}\n\n/**\n * Build the system + user messages for the in-process extraction call.\n * The system part instructs the model to return a JSON array restricted to\n * EXTRACTABLE_TYPES; the user part carries the transcript.\n */\nexport function buildExtractionPrompt(\n project: string,\n transcript: string,\n contextFiles: DigestContextFiles = {},\n): { system: string; user: string } {\n const sysParts: string[] = [\n 'You are an assistant that extracts durable knowledge from a session transcript.',\n ]\n if (contextFiles.criticalFacts?.trim()) {\n sysParts.push(`Always-loaded context: CRITICAL_FACTS.md\\n${contextFiles.criticalFacts.trim()}`)\n }\n if (contextFiles.claude?.trim()) {\n sysParts.push(`Always-loaded context: _CLAUDE.md\\n${contextFiles.claude.trim()}`)\n }\n sysParts.push(\n `For the transcript of project \\`${project}\\`, identify:`,\n ...entryTypeBullets(),\n '',\n 'Return a JSON array. Each element must have exactly:',\n ` - \"entry_type\": one of ${EXTRACTABLE_TYPES.map((t) => `\"${t}\"`).join(' | ')}`,\n ` - \"content\": ${ENTRY_CONTENT_RULE}`,\n ' - \"description\": a one-sentence summary of `content` (queryable)',\n ' - \"tags\": an array of lowercase-kebab tags (never empty if possible, at least 1 like architecture/python/testing)',\n ' - \"confidence\": a number 0.0-1.0',\n ' - \"openspec_change_id\": (optional) the change slug if the transcript names it',\n '',\n 'Rules:',\n ...ENTRY_SELECTION_RULES.map((r) => `- ${r}`),\n '',\n 'Return only the JSON array. No prose, no markdown fences.',\n )\n return {\n system: sysParts.join('\\n'),\n user: `Project: ${project}\\n\\nTranscript:\\n---\\n${transcript}\\n---`,\n }\n}\n\n/**\n * Split an oversized transcript into overlapping chunks (mirrors the legacy\n * digest script: 25k chars per chunk, 1k overlap, at most `cap` chunks).\n */\nexport function chunkTranscript(text: string, maxLen = 50_000, chunkSize = 25_000, overlap = 1_000, cap = 3): string[] {\n if (text.length <= maxLen) return [text]\n const chunks: string[] = []\n let i = 0\n while (i < text.length) {\n chunks.push(text.slice(i, i + chunkSize))\n i += chunkSize - overlap\n if (chunks.length >= cap) break\n }\n return chunks\n}\n\n/** Best-effort repair of common LLM JSON output; returns parsed value or null. */\nexport function repairJson(text: string): unknown {\n const s = text.trim()\n // Strip code fences: ```json ... ```\n const fenced = s.replace(/^```(?:json)?\\s*/i, '').replace(/\\s*```$/, '').trim()\n // Trailing commas: `,]` / `,}` -> `]` / `}`\n const noTrailing = fenced.replace(/,(\\s*[\\]}])/g, '$1')\n const candidates = [s, fenced, noTrailing]\n // Single-quote to double-quote conversion only when no double quotes exist.\n if (noTrailing.includes(\"'\") && !noTrailing.includes('\"')) {\n candidates.push(convertSingleQuotes(noTrailing))\n }\n for (const c of candidates) {\n try {\n return JSON.parse(c)\n } catch {\n // try the next candidate\n }\n }\n return null\n}\n\nfunction convertSingleQuotes(text: string): string {\n let out = ''\n let inString = false\n let i = 0\n while (i < text.length) {\n const ch = text[i]\n if (inString && ch === '\\\\') {\n if (i + 1 < text.length && text[i + 1] === \"'\") {\n out += \"'\"\n i += 2\n continue\n }\n out += ch\n if (i + 1 < text.length) {\n out += text[i + 1]\n i += 2\n continue\n }\n i += 1\n continue\n }\n if (ch === \"'\") {\n inString = !inString\n out += '\"'\n i += 1\n continue\n }\n out += ch\n i += 1\n }\n return out\n}\n\n/**\n * Validate a parsed extraction payload into OKF entries. Unknown types,\n * empty content and malformed values are dropped; tags and confidence are\n * normalized. Returns the valid entries (possibly empty).\n */\nexport function validateEntries(payload: unknown): ValidEntry[] {\n if (!Array.isArray(payload)) return []\n const valid: ValidEntry[] = []\n for (const item of payload) {\n if (typeof item !== 'object' || item === null) continue\n const raw = item as Record<string, unknown>\n const entryType = raw.entry_type\n if (typeof entryType !== 'string' || !(EXTRACTABLE_TYPES as readonly string[]).includes(entryType)) continue\n const content = typeof raw.content === 'string' ? raw.content.trim() : ''\n if (!content) continue\n const tags = Array.isArray(raw.tags) ? raw.tags.filter((t): t is string => typeof t === 'string' && t.length > 0) : []\n let confidence = 1\n if (typeof raw.confidence === 'number' && Number.isFinite(raw.confidence)) {\n confidence = Math.max(0, Math.min(1, raw.confidence))\n }\n const description = typeof raw.description === 'string' ? raw.description.trim() : ''\n const changeId = typeof raw.openspec_change_id === 'string' && raw.openspec_change_id ? raw.openspec_change_id : null\n valid.push({\n entry_type: entryType as ExtractableType,\n content,\n description,\n tags,\n confidence,\n openspec_change_id: changeId,\n })\n }\n return valid\n}\n\n// The full hook wiring is exposed for testing; the actual OpenCode integration\n// is done in `register.ts` (the entry point the OpenCode runtime loads via\n// package.json \"main\"). This module intentionally has no default export:\n// opencode 1.18.x only loads plugin modules with a single export.\nexport const __testing = {\n createSessionState,\n isSupportedVersion,\n resolveProjectName,\n idleCheckpoint,\n compactingCheckpoint,\n isGitCommit,\n buildCheckpointPrompt,\n buildCommitCheckpointPrompt,\n};\n","import { spawn, type ChildProcess } from 'node:child_process'\nimport { existsSync } from 'node:fs'\nimport { join } from 'node:path'\nimport readline from 'node:readline'\nimport { BlockAssembler, createUserMessage } from '@deepseek-ai/dsh-llm'\nimport type { FinishReason, GenerateOptions, Message } from '@deepseek-ai/dsh-llm'\nimport type { Context } from '@deepseek-ai/cordis'\nimport {\n buildExtractionPrompt,\n chunkTranscript,\n repairJson,\n validateEntries,\n type ValidEntry,\n} from './pure.js'\n\n// Post-session digest runner: in-process LLM extraction via the harness's own\n// `ctx.llm` service (no external CLI, no credentials of its own), followed by\n// OKF writes through the vault's MCP server over stdio (`store_*` tools keep\n// the Markdown source of truth and the SQLite FTS5 index in sync).\n\nconst MIN_DIGEST_TRANSCRIPT_CHARS = 200\nconst LLM_RETRIES = 3\nconst LLM_RETRY_DELAYS_MS = [2_000, 5_000]\nconst MCP_CALL_TIMEOUT_MS = 60_000\n\nfunction log(msg: string) {\n console.log(`[memory-auto] ${msg}`)\n}\n\nexport function transcriptOfDSM(events: any[]): string {\n // DSH SessionEvent -> transcript\n return (events ?? [])\n .map((ev: any) => {\n const t = ev?.type ?? ''\n const d = ev?.data ?? ev\n if (t === 'user/message' || t === 'user_message') {\n const text = typeof d.text === 'string' ? d.text : typeof d.content === 'string' ? d.content : JSON.stringify(d).slice(0, 500)\n return `## user\\n${text}`\n }\n if (t === 'assistant/message' || t === 'assistant_message') {\n const text = typeof d.text === 'string' ? d.text : typeof d.content === 'string' ? d.content : ''\n return text ? `## assistant\\n${text}` : null\n }\n if (t === 'tool/call' || t === 'tool_call') {\n const name = d.tool ?? d.name ?? 'tool'\n const args = d.args ?? d.arguments ?? {}\n return `## tool_call ${name}\\n${JSON.stringify(args).slice(0, 1000)}`\n }\n if (t === 'tool/result' || t === 'tool_result') {\n const out = typeof d.output === 'string' ? d.output : JSON.stringify(d).slice(0, 1000)\n return `## tool_result\\n${out}`\n }\n if (t.startsWith('compaction')) return `## ${t}\\n${JSON.stringify(d).slice(0, 500)}`\n return null\n })\n .filter((x): x is string => Boolean(x))\n .join('\\n\\n')\n}\n\n/** Translate a terminal stream finish into a thrown error, mirroring dsh's own summarizers. */\nfunction finishError(finish: FinishReason): Error | undefined {\n switch (finish.kind) {\n case 'error':\n case 'aborted': {\n const error = new Error(finish.failure.message) as Error & { code?: string }\n error.code = finish.failure.code\n return error\n }\n default:\n return undefined\n }\n}\n\nexport interface DigestConfig {\n memoryPath: string\n serverDir: string\n provider: string\n model: string\n maxTokens: number\n minTranscriptChars: number\n}\n\n/**\n * Run one extraction call through the harness's `ctx.llm` service: build the\n * prompt, stream, assemble, repair and validate the JSON entries.\n */\nexport async function extractEntriesWithLlm(\n ctx: Context,\n config: DigestConfig,\n project: string,\n transcript: string,\n contextFiles?: { criticalFacts?: string; claude?: string },\n signal?: AbortSignal,\n): Promise<ValidEntry[]> {\n const { system, user } = buildExtractionPrompt(project, transcript, contextFiles)\n const assembler = new BlockAssembler()\n const messages: Message[] = [\n createUserMessage({\n content: [{ type: 'text', text: user }],\n source: { kind: 'plugin', plugin: 'memory-auto' },\n }),\n ]\n const options: GenerateOptions = {\n provider: config.provider,\n model: config.model,\n messages,\n system,\n maxTokens: config.maxTokens,\n ...(signal === undefined ? {} : { signal }),\n }\n for await (const chunk of ctx.llm.stream(options)) assembler.push(chunk)\n const error = finishError(assembler.finish)\n if (error !== undefined) throw error\n const text = assembler\n .blocks()\n .filter((b) => b.type === 'text')\n .map((b) => b.text)\n .join('')\n const parsed = repairJson(text)\n return validateEntries(parsed)\n}\n\n/** Minimal MCP stdio client for the vault server (spawned via its launcher: uv, pip venv fallback). */\nexport interface McpClient {\n callTool(name: string, args: Record<string, unknown>, timeoutMs?: number): Promise<unknown>\n close(): Promise<void>\n}\n\nexport function connectMcp(memoryPath: string, serverDir: string): Promise<McpClient> {\n return new Promise((resolve, reject) => {\n // Single decision point: the server bundle's launcher.mjs picks `uv run`\n // or a pip-managed .venv. Legacy hand-made server dirs without a launcher\n // keep the old direct `uv run` spawn.\n const launcher = join(serverDir, 'launcher.mjs')\n const [command, args] = existsSync(launcher)\n ? [process.execPath, [launcher]]\n : ['uv', ['run', '--directory', serverDir, 'python', 'server.py']]\n const child: ChildProcess = spawn(\n command,\n args,\n {\n env: { ...process.env, MEMORY_PATH: memoryPath, UV_CACHE_DIR: process.env.UV_CACHE_DIR ?? '/tmp/uv-cache' },\n stdio: ['pipe', 'pipe', 'inherit'],\n },\n )\n const pending = new Map<number, { resolve: (v: unknown) => void; reject: (e: Error) => void; timer: NodeJS.Timeout }>()\n let nextId = 1\n let closed = false\n\n const failAll = (err: Error) => {\n for (const [, p] of pending) {\n clearTimeout(p.timer)\n p.reject(err)\n }\n pending.clear()\n }\n\n child.on('error', (err) => {\n closed = true\n failAll(new Error(`memory-vault-server spawn failed: ${err.message}`))\n reject(err)\n })\n child.on('exit', (code) => {\n if (closed) return\n closed = true\n failAll(new Error(`memory-vault-server exited unexpectedly (code ${code})`))\n reject(new Error(`memory-vault-server exited before initialize (code ${code})`))\n })\n\n const rl = readline.createInterface({ input: child.stdout!, crlfDelay: Infinity })\n rl.on('line', (line) => {\n let msg: any\n try {\n msg = JSON.parse(line)\n } catch {\n return\n }\n if (typeof msg?.id === 'number') {\n const p = pending.get(msg.id)\n if (!p) return\n pending.delete(msg.id)\n clearTimeout(p.timer)\n if (msg.error) p.reject(new Error(`MCP error: ${msg.error.message ?? JSON.stringify(msg.error)}`))\n else p.resolve(msg.result)\n }\n })\n\n const send = (method: string, params: unknown, timeoutMs: number = MCP_CALL_TIMEOUT_MS): Promise<unknown> =>\n new Promise((res, rej) => {\n if (closed || !child.stdin?.writable) {\n rej(new Error('memory-vault-server is not running'))\n return\n }\n const id = nextId++\n const timer = setTimeout(() => {\n pending.delete(id)\n rej(new Error(`MCP call ${method} timed out after ${timeoutMs}ms`))\n }, timeoutMs)\n pending.set(id, { resolve: res, reject: rej, timer })\n child.stdin!.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\\n')\n })\n\n const notify = (method: string, params: unknown) => {\n if (!closed && child.stdin?.writable) {\n child.stdin.write(JSON.stringify({ jsonrpc: '2.0', method, params }) + '\\n')\n }\n }\n\n // initialize handshake (notifications are fire-and-forget: no id, no reply)\n void send('initialize', {\n protocolVersion: '2025-06-18',\n capabilities: {},\n clientInfo: { name: 'memory-auto', version: '0.1.0' },\n })\n .then(() => {\n notify('notifications/initialized', {})\n })\n .then(() => {\n if (closed) throw new Error('memory-vault-server closed during handshake')\n resolve({\n callTool: (name, args, timeoutMs = MCP_CALL_TIMEOUT_MS) =>\n send('tools/call', { name, arguments: args }, timeoutMs).then((result: any) => {\n if (result?.isError) {\n const text = Array.isArray(result.content) ? result.content.map((c: any) => c?.text ?? '').join('') : JSON.stringify(result)\n throw new Error(`tool ${name} failed: ${text}`)\n }\n return result\n }),\n close: async () => {\n if (closed) return\n closed = true\n for (const [, p] of pending) clearTimeout(p.timer)\n pending.clear()\n if (child.exitCode !== null) return\n child.kill()\n await new Promise((r) => child.once('exit', r))\n },\n })\n })\n .catch((err) => {\n closed = true\n child.kill()\n reject(err)\n })\n })\n}\n\n/**\n * Write validated OKF entries through the vault server's `store_*` tools,\n * which upsert both the Markdown file and the SQLite FTS5 index.\n */\nexport async function writeEntries(client: McpClient, project: string, entries: ValidEntry[]): Promise<{ upserted: number; failed: number }> {\n let upserted = 0\n let failed = 0\n for (const e of entries) {\n try {\n await client.callTool(`store_${e.entry_type}`, {\n project,\n content: e.content,\n ...(e.description ? { description: e.description } : {}),\n tags: e.tags,\n // Only store_fact accepts and persists confidence. Sending it for every\n // type silently discarded it, so send it where it actually lands.\n ...(e.entry_type === 'fact' ? { confidence: e.confidence } : {}),\n ...(e.openspec_change_id ? { openspec_change_id: e.openspec_change_id } : {}),\n })\n upserted += 1\n } catch (err) {\n failed += 1\n console.warn(`[memory-auto] store_${e.entry_type} failed:`, err instanceof Error ? err.message : err)\n }\n }\n return { upserted, failed }\n}\n\n/**\n * Full post-session digest: transcript -> ctx.llm extraction (with retries) ->\n * OKF writes via the vault MCP server. Never throws; logs the outcome.\n */\nexport async function digestSessionDSM(\n ctx: Context,\n config: DigestConfig,\n sessionId: string,\n directory: string,\n project: string,\n events: any[],\n signal?: AbortSignal,\n): Promise<void> {\n const header = `## context\\nproject: ${project}\\ndirectory: ${directory}\\n`\n const transcript = (header + transcriptOfDSM(events)).trim()\n if (!transcript) {\n log(`digest skip ${sessionId}: empty`)\n return\n }\n if (transcript.length < (config.minTranscriptChars ?? MIN_DIGEST_TRANSCRIPT_CHARS)) {\n log(`digest skip ${sessionId}: too short ${transcript.length}`)\n return\n }\n\n const chunks = chunkTranscript(transcript)\n const entries: ValidEntry[] = []\n for (const chunk of chunks) {\n let attempt = 0\n for (;;) {\n try {\n const got = await extractEntriesWithLlm(ctx, config, project, chunk, undefined, signal)\n entries.push(...got)\n break\n } catch (err) {\n attempt += 1\n if (attempt >= LLM_RETRIES || signal?.aborted) {\n console.warn(`[memory-auto] digest extraction failed after ${attempt} attempt(s):`, err instanceof Error ? err.message : err)\n break\n }\n const delay = LLM_RETRY_DELAYS_MS[attempt - 1] ?? 5_000\n log(`digest extraction retry ${attempt}/${LLM_RETRIES} in ${delay}ms`)\n await new Promise((r) => setTimeout(r, delay))\n }\n }\n if (signal?.aborted) break\n }\n\n if (entries.length === 0) {\n log(`digest ${sessionId}: no entries extracted`)\n return\n }\n\n let client: McpClient\n try {\n client = await connectMcp(config.memoryPath, config.serverDir)\n } catch (err) {\n console.warn(`[memory-auto] digest ${sessionId}: cannot reach vault server:`, err instanceof Error ? err.message : err)\n return\n }\n try {\n const { upserted, failed } = await writeEntries(client, project, entries)\n log(`digest ${sessionId}: ${upserted} upserted, ${failed} failed (${entries.length} extracted)`)\n } finally {\n await client.close().catch(() => {})\n }\n}\n","/**\n * Harness wiring for `memory-auto`. Registers exactly these hooks:\n *\n * - `session/created` resolve the project name for the session\n * - `session/disposed` digest the transcript\n * - `agent/status` (idle) auto-capture gate\n * - `session/event` activity tracking; `tool/call` with a\n * `git commit` command and `compaction/start`\n * queue checkpoints\n * - `agent/pre-step` deliver the queued checkpoint to the agent\n * - `ctx.effect` dispose batch-digest sessions still pending\n *\n * The agent writes the entries; this plugin only prompts it.\n */\nimport { cpSync, existsSync, mkdirSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { dirname, isAbsolute, join } from 'node:path'\nimport { fileURLToPath } from 'node:url'\nimport type { Context } from '@deepseek-ai/cordis'\nimport Schema from '@deepseek-ai/schemastery'\nimport {\n createSessionState,\n resolveProjectName,\n idleCheckpoint,\n compactingCheckpoint,\n isGitCommit,\n buildCheckpointPrompt,\n buildCommitCheckpointPrompt,\n type SessionState,\n} from './pure.js'\nimport { digestSessionDSM, type DigestConfig } from './digest.js'\n\nexport const name = 'memory-auto'\n\nexport interface Config {\n memoryPath: string\n serverDir: string\n provider: string\n model: string\n maxTokens: number\n minTranscriptChars: number\n enabled: boolean\n}\n\nexport const Config: Schema<Config> = Schema.object({\n memoryPath: Schema.string().default(process.env.DSH_MEMORY_PATH ?? ''),\n serverDir: Schema.string().default(process.env.DSH_MEMORY_SERVER_DIR ?? ''),\n provider: Schema.string().default('deepseek-official'),\n model: Schema.string().default('deepseek-v4-flash'),\n maxTokens: Schema.number().default(2048),\n minTranscriptChars: Schema.number().default(200),\n enabled: Schema.boolean().default(true),\n})\n\n/** Requires the harness LLM service: extraction runs in-process via ctx.llm. */\nexport const inject = ['llm']\n\n/**\n * Resolve the harness home the same way the harness does (`$DSH_HOME`, or\n * `~/.dsh`). Paths must never depend on the launch cwd: DSH does not chdir.\n */\nfunction dshHome(): string {\n const env = process.env.DSH_HOME?.trim()\n return env && env.length > 0 ? env : join(homedir(), '.dsh')\n}\n\n/** Absolute paths stay; empty/relative values resolve under the harness home. */\nfunction resolveUnderHome(value: string, fallbackSegment: string): string {\n const v = value.trim()\n if (v.length === 0) return join(dshHome(), fallbackSegment)\n return isAbsolute(v) ? v : join(dshHome(), v)\n}\n\nconst packageRoot = dirname(dirname(fileURLToPath(import.meta.url)))\n\n/** Copy the bundled dir into `target` when `key` is missing there. */\nfunction ensure(target: string, bundled: string, key: string): boolean {\n if (existsSync(join(target, key))) return false\n if (!existsSync(bundled)) return false\n mkdirSync(target, { recursive: true })\n cpSync(bundled, target, { recursive: true })\n return true\n}\n\n/** Copy one bundled file into `target` when missing (upgrades add files 0.1.1 → 0.1.2). */\nfunction ensureFile(target: string, bundled: string, file: string): boolean {\n const dest = join(target, file)\n if (existsSync(dest)) return false\n const src = join(bundled, file)\n if (!existsSync(src)) return false\n mkdirSync(target, { recursive: true })\n cpSync(src, dest)\n return true\n}\n\n// DSH session shape minimal\ntype DSHEvt = any\ntype DSHSession = { id: string; events: DSHEvt[]; cwd?: string }\n\nexport function apply(ctx: Context, config: Config) {\n if (!config.enabled) {\n console.log('[memory-auto] disabled via config')\n return\n }\n\n const memoryPath = resolveUnderHome(config.memoryPath, 'memory-vault')\n const serverDir = resolveUnderHome(config.serverDir, 'memory-vault-server')\n\n // Self-contained install: first boot copies the bundled server and vault\n // starter under the harness home when they are missing.\n if (ensure(serverDir, join(packageRoot, 'server'), 'server.py')) {\n console.log(`[memory-auto] installed memory-vault-server -> ${serverDir}`)\n }\n if (ensure(memoryPath, join(packageRoot, 'vault'), 'type-registry.yaml')) {\n console.log(`[memory-auto] installed vault starter -> ${memoryPath}`)\n }\n // launcher.mjs runs the server via uv or the pip-venv fallback; upgrades of\n // existing installs (server.py already present) still need the new files.\n const bundledServer = join(packageRoot, 'server')\n for (const file of ['launcher.mjs', 'requirements.txt']) {\n if (ensureFile(serverDir, bundledServer, file)) {\n console.log(`[memory-auto] installed ${file} -> ${serverDir}`)\n }\n }\n\n const digestConfig: DigestConfig = {\n memoryPath,\n serverDir,\n provider: config.provider,\n model: config.model,\n maxTokens: config.maxTokens,\n minTranscriptChars: config.minTranscriptChars,\n }\n\n if (!existsSync(join(serverDir, 'server.py'))) {\n console.warn(\n `[memory-auto] vault server not found at ${serverDir} and not bundled — the digest cannot write. ` +\n 'Set DSH_MEMORY_SERVER_DIR (or run `node scripts/bundle-assets.mjs` in a checkout).',\n )\n }\n if (!existsSync(join(memoryPath, 'type-registry.yaml'))) {\n console.warn(`[memory-auto] vault starter not found at ${memoryPath} — searches will fail until it exists.`)\n }\n\n const states = new Map<string, SessionState>()\n const activities = new Map<string, string[]>()\n const queued = new Map<string, string>()\n const sessionDirs = new Map<string, string>()\n const projectCache = new Map<string, string>()\n\n const ACTIVITY_MAX_LINES = 20\n const ACTIVITY_MAX_CHARS = 80\n\n const track = (sessionId: string, line: string) => {\n const st = states.get(sessionId)\n if (!st) return\n st.hasActivity = true\n const list = activities.get(sessionId) ?? []\n if (!list.includes(line)) {\n list.push(line)\n if (list.length > ACTIVITY_MAX_LINES) list.shift()\n activities.set(sessionId, list)\n }\n }\n\n const summary = (sid: string) => (activities.get(sid) ?? []).join('\\n')\n\n const projectFor = async (dir: string): Promise<string> => {\n const cached = projectCache.get(dir)\n if (cached) return cached\n const p = await resolveProjectName(dir || process.cwd())\n projectCache.set(dir, p)\n return p\n }\n\n // session/created -> init state\n ctx.on('session/created', async (session: DSHSession) => {\n const sid = (session as any)?.id ?? (session as any)?.sessionId\n const dir = (session as any)?.cwd ?? (session as any)?.directory ?? ''\n if (!sid) return\n const proj = await projectFor(dir)\n if (!states.has(sid)) {\n states.set(sid, createSessionState(proj))\n activities.set(sid, [])\n }\n sessionDirs.set(sid, dir)\n console.log(`[memory-auto] session created ${sid} project=${proj}`)\n })\n\n // session/disposed -> digest\n ctx.on('session/disposed', async (session: DSHSession) => {\n const sid = (session as any)?.id ?? (session as any)?.sessionId\n if (!sid) return\n const st = states.get(sid)\n if (!st) return\n if (!st.hasActivity) {\n console.log(`[memory-auto] digest skip ${sid}: no activity`)\n return\n }\n const dir = sessionDirs.get(sid) ?? ''\n const proj = await projectFor(dir)\n const evts: DSHEvt[] = (session as any)?.events ?? []\n await digestSessionDSM(ctx, digestConfig, sid, dir, proj, evts)\n })\n\n // agent/status idle -> in-session capture gate\n ctx.on('agent/status', async (payload: any) => {\n const agent = payload?.agent\n const status = payload?.status ?? payload?.agentStatus\n if (status !== 'idle') return\n const sid: string | undefined = agent?.sessionId ?? payload?.sessionId ?? agent?.id\n if (!sid) return\n const st = states.get(sid)\n if (!st) return\n const text = idleCheckpoint(st, summary(sid))\n if (text) {\n console.log(`[memory-auto] idle checkpoint digest for ${sid}`)\n const dir = sessionDirs.get(sid) ?? ''\n const proj = await projectFor(dir)\n const fakeEvents = [{ type: 'user/message', data: { text: summary(sid) } }]\n await digestSessionDSM(ctx, digestConfig, sid, dir, proj, fakeEvents)\n }\n })\n\n // session/event -> git commit detect + compaction start\n ctx.on('session/event', async (session: DSHSession, event: DSHEvt) => {\n const sid = (session as any)?.id ?? (session as any)?.sessionId ?? (event as any)?.sessionId\n if (!sid) return\n const t = event?.type ?? ''\n const d = event?.data ?? {}\n\n // compaction/start -> checkpoint injection (delivered on the next pre-step)\n if (t === 'compaction/start') {\n const st = states.get(sid)\n if (!st) return\n const txt = compactingCheckpoint(st, summary(sid))\n if (txt) {\n console.log(`[memory-auto] compaction checkpoint queued for ${sid}`)\n queued.set(sid, txt)\n }\n return\n }\n\n // tool/call -> git commit detection + activity track\n if (t === 'tool/call' || t === 'tool_call') {\n const cmd = d?.args?.command ?? d?.command ?? ''\n if (typeof cmd === 'string' && isGitCommit(cmd)) {\n const st = states.get(sid)\n if (st) {\n st.hasActivity = true\n queued.set(sid, buildCommitCheckpointPrompt(st))\n console.log(`[memory-auto] git commit queued for ${sid}`)\n }\n return\n }\n if (typeof cmd === 'string') {\n track(sid, `bash: ${cmd.trim().slice(0, ACTIVITY_MAX_CHARS)}`)\n }\n return\n }\n if (t === 'tool/result') {\n // ignore\n return\n }\n if (t === 'user/message' || t === 'assistant/message') {\n // track activity\n track(sid, `${t}: ${(d?.text ?? '').slice(0, ACTIVITY_MAX_CHARS)}`)\n }\n })\n\n // agent/pre-step Waterfall -> deliver queued checkpoint\n ctx.on('agent/pre-step', async (payload: any, next: any) => {\n const sid: string | undefined = payload?.agent?.sessionId ?? payload?.sessionId\n if (sid) {\n const q = queued.get(sid)\n if (q) {\n queued.delete(sid)\n // Best effort: append the checkpoint as user context\n if (Array.isArray(payload?.context)) payload.context.push(q)\n else if (Array.isArray(payload?.messages)) payload.messages.push({ role: 'user', content: q })\n else console.log(`[memory-auto] deliver queued checkpoint for ${sid}`)\n }\n }\n return next()\n })\n\n // dispose -> batch digest remaining sessions\n ctx.effect(() => {\n return () => {\n console.log(`[memory-auto] dispose batch ${sessionDirs.size} sessions`)\n // fire-and-forget digest for each remaining session\n for (const [sid, dir] of sessionDirs) {\n const st = states.get(sid)\n if (!st?.hasActivity) continue\n projectFor(dir).then((proj) => {\n const fakeEvents = [{ type: 'user/message', data: { text: summary(sid) } }]\n void digestSessionDSM(ctx, digestConfig, sid, dir, proj, fakeEvents)\n })\n }\n }\n })\n\n console.log(`[memory-auto] active memoryPath=${memoryPath} serverDir=${serverDir} llm=${config.provider}/${config.model}`)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,MAAM,oBAAoB;;;;;;AAO1B,MAAa,oBAAoB;CAAC;CAAY;CAAQ;CAAY;AAAY;;;;;;;AAS9E,MAAM,mBAAoD;CACxD,UAAU;CACV,MAAM;CACN,UAAU;CACV,YAAY;AACd;;AAGA,MAAM,qBACJ;;;;;AAMF,MAAM,wBAAwB;CAC5B;CACA;CACA;AACF;;AAGA,SAAS,mBAA6B;CACpC,OAAO,kBAAkB,KAAK,MAAM,OAAO,EAAE,MAAM,iBAAiB,GAAG,EAAE;AAC3E;;;;;;;;;;AA6BA,eAAsB,mBAAmB,KAA8B;CACrE,IAAI,WAAW,KAAK,KAAK,UAAU,CAAC,GAClC,OAAO,SAAS,GAAG;CAGrB,MAAM,UAAU,KAAK,KAAK,cAAc;CACxC,IAAI,WAAW,OAAO,GACpB,IAAI;EACF,MAAM,MAAM,KAAK,MAAM,MAAM,SAAS,SAAS,OAAO,CAAC;EACvD,IAAI,OAAO,IAAI,SAAS,YAAY,IAAI,KAAK,KAAK,GAChD,OAAO,IAAI,KAAK,KAAK;CAEzB,QAAQ,CAER;CAGF,MAAM,gBAAgB,KAAK,KAAK,gBAAgB;CAChD,IAAI,WAAW,aAAa,GAC1B,IAAI;EAEF,MAAM,KAAI,MADS,SAAS,eAAe,OAAO,EAAA,CACnC,MAAM,+CAA+C;EACpE,IAAI,GAAG,OAAO,EAAE;CAClB,Q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AAE,MAAM,QAAQ,GAAG,EAAE;KAAE,CAAC;KAC1E,iBAAsB,KAAK,cAAc,KAAK,KAAK,MAAM,UAAU;IACrE,CAAC;GACH;EACF;CACF,CAAC;CAED,QAAQ,IAAI,mCAAmC,WAAW,aAAa,UAAU,OAAO,OAAO,SAAS,GAAG,OAAO,OAAO;AAC3H"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luisarg/memory-auto",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "description": "Auto-captura memoria DSH (idle, commit, compaction, dispose)",
6
6
  "repository": {
@@ -30,20 +30,20 @@
30
30
  "build": "tsdown",
31
31
  "dev": "tsdown --watch",
32
32
  "test": "vitest run --passWithNoTests",
33
- "prepare": "tsdown && node ../../scripts/bundle-assets.mjs",
33
+ "prepare": "tsdown",
34
34
  "typecheck": "tsc --noEmit"
35
35
  },
36
36
  "dependencies": {
37
- "@deepseek-ai/cordis": "4.0.1",
38
- "@deepseek-ai/schemastery": "3.18.1"
37
+ "@deepseek-ai/cordis": "4.0.2",
38
+ "@deepseek-ai/schemastery": "3.18.2"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@deepseek-ai/dsh-llm": ">=0.0.1-rc.1 <0.1.0 || >=0.1.0-rc.1 <0.2.0-0"
42
42
  },
43
43
  "devDependencies": {
44
- "typescript": "^5.9.2",
45
- "tsdown": "^0.15.6",
46
- "vitest": "^3.2.7",
47
- "@deepseek-ai/dsh-llm": "0.1.0-rc.7"
44
+ "typescript": "^7.0.2",
45
+ "tsdown": "^0.23.0",
46
+ "vitest": "^5.0.0",
47
+ "@deepseek-ai/dsh-llm": "0.1.0-rc.8"
48
48
  }
49
49
  }
@@ -12,6 +12,7 @@
12
12
 
13
13
  import { spawn, spawnSync } from 'node:child_process'
14
14
  import { existsSync } from 'node:fs'
15
+ import { tmpdir } from 'node:os'
15
16
  import { dirname, join } from 'node:path'
16
17
  import { fileURLToPath, pathToFileURL } from 'node:url'
17
18
 
@@ -57,7 +58,10 @@ function main() {
57
58
  const withUv = hasUv()
58
59
  const { command, args } = resolveRunner(withUv)
59
60
  if (!withUv) ensurePipEnv()
60
- const child = spawn(command, args, { cwd: DIR, env: process.env, stdio: 'inherit' })
61
+ // Default the uv cache under the OS temp dir (Windows has no /tmp); an
62
+ // explicit UV_CACHE_DIR from the patch layer or env still wins.
63
+ const env = { UV_CACHE_DIR: join(tmpdir(), 'uv-cache'), ...process.env }
64
+ const child = spawn(command, args, { cwd: DIR, env, stdio: 'inherit' })
61
65
  for (const sig of ['SIGTERM', 'SIGINT']) process.on(sig, () => child.kill(sig))
62
66
  child.on('error', (err) => {
63
67
  console.error(`[memory-vault-server] spawn failed (${command}): ${err.message}`)
@@ -0,0 +1,191 @@
1
+ """Rebuild the SQLite FTS5 index from the OKF Markdown bundle (index-only).
2
+
3
+ Walks `projects/<project>/<type>/*.md` and `raw/*.md`, parses each file and
4
+ inserts rows directly into SQLite. Markdown stays the source of truth: this
5
+ script never rewrites .md files (upsert_entry's write path would duplicate
6
+ them with today's date on a fresh DB).
7
+
8
+ Idempotent: row ids derive from file paths, dedup keys from content, so
9
+ re-running converges. Run after importing a bundle into a new vault dir
10
+ (memory.db* may be deleted first; the script also works on a populated DB):
11
+
12
+ uv run --directory memory-vault-server python rebuild_index.py \
13
+ --memory-path /path/to/vault
14
+
15
+ DB-only data (profiles) is NOT covered — migrate profiles separately.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import argparse
21
+ import hashlib
22
+ import os
23
+ import sys
24
+ from pathlib import Path
25
+
26
+ _store_mod = None # set in main() once MEMORY_PATH points at the target bundle
27
+
28
+
29
+ def _id_for(relpath: Path) -> str:
30
+ """Stable row id derived from the file path (idempotent re-runs)."""
31
+ digest = hashlib.sha1(str(relpath).encode()).hexdigest()[:24]
32
+ return f"rebuild-{digest}"
33
+
34
+
35
+ def _collect(memory_path: Path):
36
+ """Walk the bundle -> (entries, errors); entries are
37
+ (singular, project, relpath, parsed) tuples, DB-only types skipped."""
38
+ entries: list[tuple[str, str, Path, dict]] = []
39
+ errors: list[str] = []
40
+ dir_to_singular = _store_mod._DIR_TO_SINGULAR
41
+ parse = _store_mod._parse_okf_file
42
+
43
+ def handle(path: Path, parsed: dict | None, default_project: str, singular: str):
44
+ if parsed is None:
45
+ errors.append(f"unparseable: {path.relative_to(memory_path)}")
46
+ return
47
+ # Defensive: trust the directory, not the parsed label.
48
+ parsed["entry_type"] = singular
49
+ parsed["project"] = parsed.get("project") or default_project
50
+ entries.append((singular, parsed["project"], path.relative_to(memory_path), parsed))
51
+
52
+ projects_dir = memory_path / "projects"
53
+ if projects_dir.is_dir():
54
+ # Projects may nest (e.g. `@deepseek-ai/dsh-root`): any directory whose
55
+ # name is a type dir is indexed; its project is the dir path below
56
+ # `projects/`, joined with "/".
57
+ def scan(prefix: Path, project: str):
58
+ for d in sorted(child for child in prefix.iterdir() if child.is_dir()):
59
+ singular = dir_to_singular.get(d.name)
60
+ if singular is None:
61
+ if d.name != ".obsidian":
62
+ scan(d, f"{project}/{d.name}" if project else d.name)
63
+ continue
64
+ if singular == "profile":
65
+ continue
66
+ for f in sorted(d.glob("*.md")):
67
+ if f.name == "index.md":
68
+ continue
69
+ handle(f, parse(f), project, singular)
70
+
71
+ for proj_dir in sorted(p for p in projects_dir.iterdir() if p.is_dir()):
72
+ scan(proj_dir, proj_dir.name)
73
+
74
+ raw_dir = memory_path / "raw"
75
+ if raw_dir.is_dir():
76
+ for f in sorted(raw_dir.glob("*.md")):
77
+ handle(f, parse(f), "", "source")
78
+
79
+ return entries, errors
80
+
81
+
82
+ def rebuild(memory_path: Path) -> tuple[int, int, list[str]]:
83
+ """Insert all bundle entries into the index -> (inserted, updated, errors)."""
84
+ store = _store_mod.MemoryStore(storage_path=memory_path)
85
+ store.initialize()
86
+
87
+ entries, errors = _collect(memory_path)
88
+ if not entries:
89
+ print("No entries found in bundle; nothing to do.", file=sys.stderr)
90
+ return 0, 0, errors
91
+
92
+ n_inserted = n_updated = 0
93
+ for singular, project, relpath, e in entries:
94
+ try:
95
+ dedup_key = (
96
+ store._make_dedup_key(singular, project, e["content"])
97
+ if singular != "source"
98
+ else _id_for(relpath)
99
+ )
100
+ existing = store.db.execute(
101
+ "SELECT id FROM entries WHERE dedup_key = ?", (dedup_key,)
102
+ ).fetchone()
103
+ if existing:
104
+ if existing["id"] != _id_for(relpath):
105
+ # Same content already indexed (bundle duplicates collapse):
106
+ # adopt this file's id so re-runs converge, keep row content.
107
+ store.db.execute(
108
+ "UPDATE entries SET id = ?, created_at = ?, updated_at = ? WHERE dedup_key = ?",
109
+ (_id_for(relpath), e["created_at"], e["updated_at"], dedup_key),
110
+ )
111
+ store.db.commit()
112
+ n_updated += 1
113
+ continue
114
+ store._insert_row(
115
+ _id_for(relpath),
116
+ singular,
117
+ project,
118
+ e["content"],
119
+ e["tags"],
120
+ float(e["confidence"] or 1.0),
121
+ e["openspec_change_id"],
122
+ dedup_key,
123
+ e["created_at"],
124
+ e["updated_at"],
125
+ )
126
+ n_inserted += 1
127
+ except Exception as exc: # noqa: BLE001 — one bad file must not stop the rebuild
128
+ errors.append(f"failed: {project}/{relpath}: {exc}")
129
+ return n_inserted, n_updated, errors
130
+
131
+
132
+ def regen_missing_indexes(memory_path: Path, store) -> int:
133
+ """Regenerate `<project>/index.md` only where it is missing (nested too)."""
134
+ projects_dir = memory_path / "projects"
135
+ if not projects_dir.is_dir():
136
+ return 0
137
+ n = 0
138
+
139
+ def scan(prefix: Path):
140
+ for d in sorted(child for child in prefix.iterdir() if child.is_dir()):
141
+ if d.name in _store_mod._DIR_TO_SINGULAR or d.name == ".obsidian":
142
+ continue # type dir or editor cache — not a project
143
+ if not (d / "index.md").exists():
144
+ project = str(d.relative_to(projects_dir)).replace(os.sep, "/")
145
+ store._regenerate_project_index(project)
146
+ n += 1
147
+ scan(d)
148
+
149
+ for proj_dir in sorted(p for p in projects_dir.iterdir() if p.is_dir()):
150
+ if not (proj_dir / "index.md").exists():
151
+ store._regenerate_project_index(proj_dir.name)
152
+ n += 1
153
+ scan(proj_dir)
154
+ return n
155
+
156
+
157
+ def main(argv: list[str] | None = None) -> int:
158
+ global _store_mod
159
+ parser = argparse.ArgumentParser(description="Rebuild SQLite index from OKF bundle")
160
+ parser.add_argument(
161
+ "--memory-path",
162
+ default=None,
163
+ help="OKF bundle directory (default: $MEMORY_PATH or repo memory-vault)",
164
+ )
165
+ args = parser.parse_args(argv)
166
+
167
+ # store.py builds its type maps from MEMORY_PATH at import time — point it
168
+ # at the target bundle before importing.
169
+ if args.memory_path:
170
+ os.environ["MEMORY_PATH"] = str(Path(args.memory_path).resolve())
171
+ import store as _store_mod
172
+
173
+ memory_path = Path(os.environ.get("MEMORY_PATH", "")).resolve()
174
+ if not memory_path.is_dir():
175
+ print(f"error: {memory_path} is not a directory", file=sys.stderr)
176
+ return 2
177
+
178
+ print(f"Rebuilding index from {memory_path} ...")
179
+ n_ins, n_upd, errors = rebuild(memory_path)
180
+ store = _store_mod.MemoryStore(storage_path=memory_path)
181
+ store.initialize()
182
+ n_index = regen_missing_indexes(memory_path, store)
183
+ print(f"Inserted: {n_ins}, already-indexed: {n_upd}, index.md regenerated: {n_index}")
184
+ for err in errors:
185
+ print(f" {err}", file=sys.stderr)
186
+ print(f"Errors: {len(errors)}")
187
+ return 0 if not errors else 1
188
+
189
+
190
+ if __name__ == "__main__":
191
+ raise SystemExit(main())
package/server/server.py CHANGED
@@ -1,8 +1,13 @@
1
1
  """MCP server exposing memory store tools via the Model Context Protocol.
2
2
 
3
- Eight tools (per openspec/specs/memory-mcp-server/spec.md):
3
+ Ten tools (per openspec/specs/memory-mcp-server/spec.md):
4
4
  search_memory, store_decision, store_fact, store_learning,
5
- store_convention, store_profile, export_memories, get_profile, ping.
5
+ store_convention, store_profile, store_source, export_memories,
6
+ get_profile, ping.
7
+
8
+ Note: `type-registry.yaml` also declares `context` and `idea`. They are valid
9
+ `entries.entry_type` values and legal filters here, but no `store_*` tool
10
+ creates them — nothing writes them today.
6
11
 
7
12
  All reads are explicit (no background polling). Server validates storage
8
13
  accessibility at startup.
@@ -190,7 +195,13 @@ def _tool_definitions() -> list[Tool]:
190
195
  return [
191
196
  Tool(
192
197
  name="search_memory",
193
- description="Search memory entries across projects. Omit project to search all projects.",
198
+ description=(
199
+ "Search memory entries across projects. Omit project to search all projects. "
200
+ "The query is tokenized and OR-matched (any term hits), ranked by relevance. "
201
+ "Tags are OR-matched too: passing ['ci','release'] returns entries with either tag. "
202
+ "Filters narrow the result set; at most 50 entries are returned. "
203
+ "Source entries are excluded unless entry_type='source' with no other filter."
204
+ ),
194
205
  inputSchema={
195
206
  "type": "object",
196
207
  "properties": {
@@ -203,78 +214,143 @@ def _tool_definitions() -> list[Tool]:
203
214
  ),
204
215
  Tool(
205
216
  name="store_decision",
206
- description="Store a decision entry",
217
+ description=(
218
+ "Store a decision: an architectural or design choice that was made and why. "
219
+ "Deduplicated by content hash, so re-storing the same text updates instead of "
220
+ "duplicating."
221
+ ),
207
222
  inputSchema={
208
223
  "type": "object",
209
224
  "required": ["project", "content"],
210
225
  "properties": {
211
226
  "project": {"type": "string"},
212
- "content": {"type": "string"},
213
- "description": {"type": "string"},
214
- "tags": {"type": "array", "items": {"type": "string"}},
227
+ "content": {
228
+ "type": "string",
229
+ "description": "One paragraph, no headings or bullet lists.",
230
+ },
231
+ "description": {
232
+ "type": "string",
233
+ "description": "One-sentence queryable summary; derived from content if omitted.",
234
+ },
235
+ "tags": {
236
+ "type": "array",
237
+ "items": {"type": "string"},
238
+ "description": "Lowercase-kebab tags, e.g. architecture/python/testing.",
239
+ },
215
240
  "openspec_change_id": {"type": "string"},
216
241
  },
217
242
  },
218
243
  ),
219
244
  Tool(
220
245
  name="store_fact",
221
- description="Store a fact entry",
246
+ description=(
247
+ "Store a fact: a stable, verifiable statement about the project (version, "
248
+ "constraint, path, endpoint). Deduplicated by content hash. Prefer one atomic "
249
+ "fact per call over a bundle of several."
250
+ ),
222
251
  inputSchema={
223
252
  "type": "object",
224
253
  "required": ["project", "content"],
225
254
  "properties": {
226
255
  "project": {"type": "string"},
227
- "content": {"type": "string"},
228
- "description": {"type": "string"},
229
- "tags": {"type": "array", "items": {"type": "string"}},
256
+ "content": {
257
+ "type": "string",
258
+ "description": "One paragraph, no headings or bullet lists.",
259
+ },
260
+ "description": {
261
+ "type": "string",
262
+ "description": "One-sentence queryable summary; derived from content if omitted.",
263
+ },
264
+ "tags": {
265
+ "type": "array",
266
+ "items": {"type": "string"},
267
+ "description": "Lowercase-kebab tags, e.g. architecture/python/testing.",
268
+ },
230
269
  "confidence": {"type": "number", "minimum": 0.0, "maximum": 1.0},
231
270
  },
232
271
  },
233
272
  ),
234
273
  Tool(
235
274
  name="store_learning",
236
- description="Store a learning entry",
275
+ description=(
276
+ "Store a learning: a non-obvious lesson, debugging insight, or solution found — "
277
+ "something that cost effort and would otherwise be rediscovered. Deduplicated by "
278
+ "content hash."
279
+ ),
237
280
  inputSchema={
238
281
  "type": "object",
239
282
  "required": ["project", "content"],
240
283
  "properties": {
241
284
  "project": {"type": "string"},
242
- "content": {"type": "string"},
243
- "description": {"type": "string"},
244
- "tags": {"type": "array", "items": {"type": "string"}},
285
+ "content": {
286
+ "type": "string",
287
+ "description": "One paragraph, no headings or bullet lists.",
288
+ },
289
+ "description": {
290
+ "type": "string",
291
+ "description": "One-sentence queryable summary; derived from content if omitted.",
292
+ },
293
+ "tags": {
294
+ "type": "array",
295
+ "items": {"type": "string"},
296
+ "description": "Lowercase-kebab tags, e.g. architecture/python/testing.",
297
+ },
245
298
  },
246
299
  },
247
300
  ),
248
301
  Tool(
249
302
  name="store_convention",
250
- description="Store a convention entry",
303
+ description=(
304
+ "Store a convention: an agreed style rule, naming pattern, or coding standard. "
305
+ "Deduplicated by content hash."
306
+ ),
251
307
  inputSchema={
252
308
  "type": "object",
253
309
  "required": ["project", "content"],
254
310
  "properties": {
255
311
  "project": {"type": "string"},
256
- "content": {"type": "string"},
257
- "description": {"type": "string"},
258
- "tags": {"type": "array", "items": {"type": "string"}},
312
+ "content": {
313
+ "type": "string",
314
+ "description": "One paragraph, no headings or bullet lists.",
315
+ },
316
+ "description": {
317
+ "type": "string",
318
+ "description": "One-sentence queryable summary; derived from content if omitted.",
319
+ },
320
+ "tags": {
321
+ "type": "array",
322
+ "items": {"type": "string"},
323
+ "description": "Lowercase-kebab tags, e.g. architecture/python/testing.",
324
+ },
259
325
  },
260
326
  },
261
327
  ),
262
328
  Tool(
263
329
  name="store_profile",
264
- description="Store or update a user profile entry for a project",
330
+ description=(
331
+ "Replace the tech profile for a project. One profile per project: the content "
332
+ "overwrites the previous one, it does not append. Pass the complete profile text."
333
+ ),
265
334
  inputSchema={
266
335
  "type": "object",
267
336
  "required": ["project", "content"],
268
337
  "properties": {
269
338
  "project": {"type": "string"},
270
- "content": {"type": "string"},
339
+ "content": {
340
+ "type": "string",
341
+ "description": "The full profile — replaces the stored one.",
342
+ },
271
343
  "tags": {"type": "array", "items": {"type": "string"}},
272
344
  },
273
345
  },
274
346
  ),
275
347
  Tool(
276
348
  name="store_source",
277
- description="Store a source reference (article, transcript, PDF, video, link)",
349
+ description=(
350
+ "Store an external source reference (article, transcript, PDF, video, link) under "
351
+ "raw/. Immutable: a later store with the same URL returns the existing entry, and "
352
+ "reusing a title slug with different content is rejected."
353
+ ),
278
354
  inputSchema={
279
355
  "type": "object",
280
356
  "required": ["url", "title", "description", "source_kind"],
@@ -294,7 +370,10 @@ def _tool_definitions() -> list[Tool]:
294
370
  ),
295
371
  Tool(
296
372
  name="export_memories",
297
- description="Export all memory entries for a project (no limit)",
373
+ description=(
374
+ "Export every stored entry for one project, newest first, with no result limit. "
375
+ "Use for a full project dump, not for lookups — search_memory is cheaper."
376
+ ),
298
377
  inputSchema={
299
378
  "type": "object",
300
379
  "required": ["project"],
@@ -306,7 +385,11 @@ def _tool_definitions() -> list[Tool]:
306
385
  ),
307
386
  Tool(
308
387
  name="get_profile",
309
- description="Retrieve the global tech profile for a project",
388
+ description=(
389
+ "Retrieve the stored tech profile for a project. Returns profile entries by "
390
+ "default; pass entry_type to query another type instead (then it is a "
391
+ "most-recent-first lookup capped at 10)."
392
+ ),
310
393
  inputSchema={
311
394
  "type": "object",
312
395
  "required": ["project"],
package/server/store.py CHANGED
@@ -263,7 +263,10 @@ class MemoryStore:
263
263
  db_path = self.storage_path / "memory.db"
264
264
  if db_path.exists() and db_path.stat().st_size > 0:
265
265
  try:
266
- test_conn = sqlite3.connect(str(db_path))
266
+ # timeout=30: with WAL a live vault may be written by another
267
+ # client (DSH + opencode). Without it the probe waits only 5 s
268
+ # and a busy lock surfaces as "corrupt database".
269
+ test_conn = sqlite3.connect(str(db_path), timeout=30)
267
270
  row = test_conn.execute("PRAGMA integrity_check").fetchone()
268
271
  test_conn.close()
269
272
  if row[0].lower() != "ok":
@@ -271,7 +274,7 @@ class MemoryStore:
271
274
  except sqlite3.DatabaseError as e:
272
275
  raise RuntimeError(f"corrupt database: {e}") from e
273
276
 
274
- self._db = sqlite3.connect(str(db_path))
277
+ self._db = sqlite3.connect(str(db_path), timeout=30)
275
278
  self._db.row_factory = sqlite3.Row
276
279
  self.db.execute("PRAGMA journal_mode=WAL")
277
280
  self.db.execute("PRAGMA foreign_keys=ON")
@@ -767,12 +770,15 @@ class MemoryStore:
767
770
  project: str,
768
771
  entry_type: str | None = None,
769
772
  ) -> list[dict]:
770
- """Retrieve profile entries for a project."""
771
- sql = "SELECT * FROM entries WHERE project = ?"
772
- params: list = [project]
773
- if entry_type:
774
- sql += " AND entry_type = ?"
775
- params.append(entry_type)
773
+ """Retrieve the profile for a project.
774
+
775
+ Defaults to ``profile`` entries: the tool is named get_profile, so an
776
+ unfiltered call returning the 10 most recent rows of any type was a
777
+ silent wrong answer. Pass ``entry_type`` to use it as a recency query.
778
+ """
779
+ entry_type = entry_type or "profile"
780
+ sql = "SELECT * FROM entries WHERE project = ? AND entry_type = ?"
781
+ params: list = [project, entry_type]
776
782
  sql += " ORDER BY updated_at DESC LIMIT 10"
777
783
  rows = self.db.execute(sql, params).fetchall()
778
784
  return [dict(r) for r in rows]
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env python3
2
+ """Self-check: get_profile answers with the profile, not with recent noise.
3
+
4
+ Regression guard for the bug where an unfiltered get_profile returned the 10
5
+ most recently updated rows of ANY type, so a caller asking for the profile of a
6
+ busy project got unrelated decisions and facts.
7
+
8
+ Run: python3 memory-vault-server/test_get_profile.py
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import json
14
+ import os
15
+ import shutil
16
+ import sys
17
+ import tempfile
18
+ from pathlib import Path
19
+
20
+ SERVER_DIR = Path(__file__).resolve().parent
21
+ REPO_VAULT = SERVER_DIR.parent / "memory-vault"
22
+ sys.path.insert(0, str(SERVER_DIR))
23
+
24
+
25
+ def main() -> int:
26
+ with tempfile.TemporaryDirectory() as tmp:
27
+ # store.py resolves the type registry from MEMORY_PATH at import time,
28
+ # so seed the throwaway vault before importing it.
29
+ vault = Path(tmp)
30
+ (vault / "projects").mkdir()
31
+ shutil.copy(REPO_VAULT / "type-registry.yaml", vault / "type-registry.yaml")
32
+ os.environ["MEMORY_PATH"] = str(vault)
33
+ import store as store_mod
34
+
35
+ s = store_mod.MemoryStore(storage_path=vault)
36
+ s.initialize()
37
+
38
+ s.upsert_profile(project="proj", content="PROFILE: python + sqlite")
39
+ # Written afterwards, so these outrank the profile in updated_at:
40
+ # the old code returned them and called it a profile.
41
+ s.upsert_entry("decision", "proj", "DECISION: use sqlite for storage")
42
+ s.upsert_entry("fact", "proj", "FACT: python 3.11 is required")
43
+
44
+ got = s.get_profile(project="proj")
45
+ assert len(got) == 1, f"expected only the profile row, got {len(got)}: {got}"
46
+ assert got[0]["entry_type"] == "profile", got[0]["entry_type"]
47
+ assert "PROFILE" in got[0]["content"], got[0]["content"]
48
+
49
+ # entry_type=None must behave exactly like the default.
50
+ assert s.get_profile(project="proj", entry_type=None) == got
51
+
52
+ # Explicit entry_type still works (and is now a plain recency lookup).
53
+ facts = s.get_profile(project="proj", entry_type="fact")
54
+ assert len(facts) == 1 and facts[0]["entry_type"] == "fact", facts
55
+
56
+ # A project with no profile yields nothing rather than unrelated rows.
57
+ s.upsert_entry("fact", "other", "FACT: unrelated project")
58
+ assert s.get_profile(project="other") == [], "profile leaked across projects"
59
+
60
+ # The MCP tool handler agrees (binds the tool default to the store fix).
61
+ # Needs the pinned `mcp` version from requirements.txt; skip when the
62
+ # ambient one is older instead of failing the whole check.
63
+ try:
64
+ import server
65
+
66
+ found = json.loads(server.handle_get_profile(s, {"project": "proj"}))
67
+ assert len(found) == 1 and found[0]["entry_type"] == "profile", found
68
+ except ImportError as exc:
69
+ print(f"skip: MCP handler assertion ({exc})")
70
+
71
+ s._db.close()
72
+
73
+ print("ok: get_profile returns the profile, not the most recent rows")
74
+ return 0
75
+
76
+
77
+ if __name__ == "__main__":
78
+ raise SystemExit(main())