opencode-memoir 3.0.5 → 3.1.1
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 +0 -4
- package/dist/capture.d.ts +0 -6
- package/dist/capture.d.ts.map +1 -1
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -81
- package/dist/index.js.map +4 -4
- package/dist/mcp-client.d.ts.map +1 -1
- package/dist/prompts/subagent-system.tmpl +16 -15
- package/dist/status.d.ts +2 -3
- package/dist/status.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/prompts/capture-task.tmpl +0 -34
- package/dist/prompts/startup-hint.tmpl +0 -3
- package/dist/store.d.ts +0 -18
- package/dist/store.d.ts.map +0 -1
- package/dist/turn-status.d.ts +0 -2
- package/dist/turn-status.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -83,7 +83,6 @@ All optional:
|
|
|
83
83
|
| `chat.message` | Captures the previous completed turn, tracks real parent messages, and auto-matches the memoir branch; waits for `promptAsync` to accept the visible capture task while ignoring synthetic and memoir-child messages |
|
|
84
84
|
| `tool.execute.before` | Marks memoir's `task` invocation with `background: true` when OpenCode background subagents are enabled |
|
|
85
85
|
| `tool.execute.after` + `event` | Tracks foreground completion and known background child idle/error events so checkout does not overlap an active capture |
|
|
86
|
-
| `experimental.chat.system.transform` | Compact status after real parent messages + startup hint and proactive recall once per session |
|
|
87
86
|
| `dispose` | Saves session markers when `MEMOIR_AUTO_SAVE=1` is explicit, closes the project MCP process, and clears instance state |
|
|
88
87
|
|
|
89
88
|
## How it works
|
|
@@ -101,8 +100,6 @@ After each real parent-session message, every model call in that turn receives a
|
|
|
101
100
|
- Without either enabling flag, the plugin does not add `background`; OpenCode runs the same capture task through the original foreground subagent path. Memory capture remains available, but the parent session may wait for it to finish.
|
|
102
101
|
- Before changing the shared memoir branch, the plugin waits for active foreground tasks (`tool.execute.after`) and native background tasks (the known child session's terminal idle/error event). A timeout defers checkout instead of moving the store underneath a running capture.
|
|
103
102
|
|
|
104
|
-
At session start, `experimental.chat.system.transform` injects a recall hint plus a `memoir_summarize` snapshot so the agent begins with prior context.
|
|
105
|
-
|
|
106
103
|
## Development
|
|
107
104
|
|
|
108
105
|
### Prerequisites
|
|
@@ -130,7 +127,6 @@ npm test # run the test suite
|
|
|
130
127
|
| `src/store.ts` | Store path derivation and serialized store-branch matching |
|
|
131
128
|
| `src/path.ts` | Symlink-safe project and store path helpers |
|
|
132
129
|
| `src/prompts.ts` | Cached prompt-template loader |
|
|
133
|
-
| `src/turn-status.ts` | Compact model-facing status formatter |
|
|
134
130
|
| `src/debug.ts` | File/stderr lifecycle and debug logger |
|
|
135
131
|
|
|
136
132
|
## Publishing
|
package/dist/capture.d.ts
CHANGED
|
@@ -27,12 +27,6 @@ export declare function lastTurnTranscript(messages: ChatMessage[]): string | nu
|
|
|
27
27
|
* A value of 0 disables the filter (capture everything).
|
|
28
28
|
*/
|
|
29
29
|
export declare function shouldCaptureTurn(transcript: string | null, minChars?: number): transcript is string;
|
|
30
|
-
/**
|
|
31
|
-
* Build the capture task handed to the memoir subagent. It applies the
|
|
32
|
-
* durability checks, writes each fact with an explicit 3-level taxonomy path,
|
|
33
|
-
* and returns a compact report based only on confirmed tool outcomes.
|
|
34
|
-
*/
|
|
35
|
-
export declare function buildTurnCaptureTask(transcript: string): string;
|
|
36
30
|
/**
|
|
37
31
|
* Capture the latest completed turn of a session into memoir via the subagent.
|
|
38
32
|
*
|
package/dist/capture.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../src/capture.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../src/capture.ts"],"names":[],"mappings":"AAOA,UAAU,QAAQ;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACtC,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC;CACpB;AAyDD;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,MAAM,CASvE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,MAAM,GAAG,IAAI,CAoBzE;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,MAAM,GAAG,IAAI,EACzB,QAAQ,SAAoB,GAC3B,UAAU,IAAI,MAAM,CAGtB;AAeD;;;;;;;;;;GAUG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,OAAO,CAAC,IAAI,CAAC,CA2Cf"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
|
+
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
1
2
|
import type { PluginModule } from "@opencode-ai/plugin";
|
|
3
|
+
/** Get current git branch (empty string if not in a git repo). */
|
|
4
|
+
export declare function currentGitBranch(cwd?: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Track the actual branch of one memoir store. The checkout is store-global,
|
|
7
|
+
* so a per-session cache would become stale as soon as another session changed
|
|
8
|
+
* the branch.
|
|
9
|
+
*/
|
|
10
|
+
export declare class MemoirBranchMatcher {
|
|
11
|
+
private matching;
|
|
12
|
+
match(client: Client, cwd: string, drain?: () => Promise<boolean>): Promise<void>;
|
|
13
|
+
private currentBranch;
|
|
14
|
+
private matchNow;
|
|
15
|
+
clear(): void;
|
|
16
|
+
}
|
|
2
17
|
declare const plugin: PluginModule;
|
|
3
18
|
export default plugin;
|
|
4
19
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACxE,OAAO,KAAK,EAA+B,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAyBrF,kEAAkE;AAClE,wBAAgB,gBAAgB,CAAC,GAAG,SAAgB,GAAG,MAAM,CAW5D;AAED;;;;GAIG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAoC;IAEpD,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAIhF;YAEa,aAAa;YAKb,QAAQ;IAsBtB,KAAK,IAAI,IAAI,CAEZ;CACF;AAsPD,QAAA,MAAM,MAAM,EAAE,YAGb,CAAC;eAEa,MAAM"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
3
|
+
|
|
1
4
|
// src/debug.ts
|
|
2
5
|
import { appendFileSync, mkdirSync } from "node:fs";
|
|
3
6
|
import { homedir } from "node:os";
|
|
@@ -186,10 +189,6 @@ function shouldCaptureTurn(transcript, minChars = captureMinChars()) {
|
|
|
186
189
|
if (minChars <= 0) return transcript !== null && transcript.length > 0;
|
|
187
190
|
return transcript !== null && transcript.length >= minChars;
|
|
188
191
|
}
|
|
189
|
-
var CAPTURE_TASK_TEMPLATE = loadPrompt("capture-task.tmpl");
|
|
190
|
-
function buildTurnCaptureTask(transcript) {
|
|
191
|
-
return CAPTURE_TASK_TEMPLATE.replace("{{TRANSCRIPT}}", transcript);
|
|
192
|
-
}
|
|
193
192
|
function lastAssistantMessageId(messages) {
|
|
194
193
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
195
194
|
if (messages[i].info?.role === "assistant" && messageText(messages[i])) {
|
|
@@ -231,7 +230,7 @@ async function captureTurn(client, sessionID, lastCaptured) {
|
|
|
231
230
|
return;
|
|
232
231
|
}
|
|
233
232
|
log("captureTurn: submitting memoir subtask (transcript", transcript.length, "chars)");
|
|
234
|
-
await runMemoirSubagent(client, sessionID,
|
|
233
|
+
await runMemoirSubagent(client, sessionID, transcript);
|
|
235
234
|
lastCaptured.set(sessionID, turnId);
|
|
236
235
|
} catch (e) {
|
|
237
236
|
log("captureTurn failed", e);
|
|
@@ -440,11 +439,6 @@ var MemoirRuntime = class {
|
|
|
440
439
|
this.cleanupClient();
|
|
441
440
|
continue;
|
|
442
441
|
}
|
|
443
|
-
if (this.serverProc === proc) {
|
|
444
|
-
this.serverUrl = url;
|
|
445
|
-
log("memoir-mcp HTTP server up at", url.toString());
|
|
446
|
-
return url;
|
|
447
|
-
}
|
|
448
442
|
this.serverUrl = url;
|
|
449
443
|
log("memoir-mcp HTTP server up at", url.toString());
|
|
450
444
|
return url;
|
|
@@ -554,9 +548,6 @@ async function callMemoirTool(client, name, args) {
|
|
|
554
548
|
}
|
|
555
549
|
}
|
|
556
550
|
|
|
557
|
-
// src/store.ts
|
|
558
|
-
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
559
|
-
|
|
560
551
|
// src/status.ts
|
|
561
552
|
function parseMemoirStatus(raw) {
|
|
562
553
|
if (!raw) return {};
|
|
@@ -571,9 +562,14 @@ function parseMemoirStatus(raw) {
|
|
|
571
562
|
}
|
|
572
563
|
}
|
|
573
564
|
|
|
574
|
-
// src/
|
|
575
|
-
function
|
|
576
|
-
|
|
565
|
+
// src/index.ts
|
|
566
|
+
function envFlag(name) {
|
|
567
|
+
const value = process.env[name]?.trim().toLowerCase();
|
|
568
|
+
if (value === void 0 || value === "") return void 0;
|
|
569
|
+
return value === "1" || value === "true";
|
|
570
|
+
}
|
|
571
|
+
function backgroundSubagentsEnabled() {
|
|
572
|
+
return envFlag("OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS") ?? envFlag("OPENCODE_EXPERIMENTAL") ?? false;
|
|
577
573
|
}
|
|
578
574
|
function currentGitBranch(cwd = process.cwd()) {
|
|
579
575
|
try {
|
|
@@ -613,31 +609,12 @@ var MemoirBranchMatcher = class {
|
|
|
613
609
|
this.matching = Promise.resolve();
|
|
614
610
|
}
|
|
615
611
|
};
|
|
616
|
-
|
|
617
|
-
// src/turn-status.ts
|
|
618
|
-
function buildTurnStatus(statusRaw) {
|
|
619
|
-
const { branch, memory_count } = parseMemoirStatus(statusRaw);
|
|
620
|
-
const memoryCount = memory_count ?? 0;
|
|
621
|
-
if (!branch && memoryCount <= 0) return "";
|
|
622
|
-
if (!branch) return `[memoir] memory available (${memoryCount} memories)`;
|
|
623
|
-
return `[memoir] ${branch}${memoryCount > 0 ? ` \xB7 memory available (${memoryCount} memories)` : ""}`;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
// src/index.ts
|
|
627
|
-
function envFlag(name) {
|
|
628
|
-
const value = process.env[name]?.trim().toLowerCase();
|
|
629
|
-
if (value === void 0 || value === "") return void 0;
|
|
630
|
-
return value === "1" || value === "true";
|
|
631
|
-
}
|
|
632
|
-
function backgroundSubagentsEnabled() {
|
|
633
|
-
return envFlag("OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS") ?? envFlag("OPENCODE_EXPERIMENTAL") ?? false;
|
|
634
|
-
}
|
|
635
612
|
var MemoirOpenCode = async (input, rawOptions) => {
|
|
636
613
|
const opts = rawOptions ?? {};
|
|
637
614
|
const pluginInput = input ?? {};
|
|
638
615
|
const sdkClient = pluginInput.client;
|
|
639
616
|
const directory = safeRealpath(pluginInput.directory ?? process.cwd());
|
|
640
|
-
const storePath =
|
|
617
|
+
const storePath = deriveStorePath(directory, opts.store);
|
|
641
618
|
const mcpCommand = ["memoir-mcp"];
|
|
642
619
|
if (storePath) {
|
|
643
620
|
mcpCommand.push("--store", storePath);
|
|
@@ -645,13 +622,11 @@ var MemoirOpenCode = async (input, rawOptions) => {
|
|
|
645
622
|
const environment = storePath ? { MEMOIR_STORE: storePath } : void 0;
|
|
646
623
|
const runtime = new MemoirRuntime(mcpCommand, environment, directory);
|
|
647
624
|
const branchMatcher = new MemoirBranchMatcher();
|
|
648
|
-
const sessionsWithStartupHint = /* @__PURE__ */ new Set();
|
|
649
625
|
const parentSessions = /* @__PURE__ */ new Set();
|
|
650
626
|
const lastCaptured = /* @__PURE__ */ new Map();
|
|
651
627
|
const capturePending = /* @__PURE__ */ new Set();
|
|
652
628
|
const captureLifecycle = new CaptureLifecycle();
|
|
653
629
|
const messageCounts = /* @__PURE__ */ new Map();
|
|
654
|
-
const turnStatus = /* @__PURE__ */ new Map();
|
|
655
630
|
let mcpServer;
|
|
656
631
|
const connectClient = async () => {
|
|
657
632
|
try {
|
|
@@ -738,10 +713,6 @@ var MemoirOpenCode = async (input, rawOptions) => {
|
|
|
738
713
|
const client = await connectClient();
|
|
739
714
|
if (client) {
|
|
740
715
|
await branchMatcher.match(client, directory, () => captureLifecycle.drain());
|
|
741
|
-
const status = await callMemoirTool(client, "memoir_status", {});
|
|
742
|
-
const line = buildTurnStatus(status);
|
|
743
|
-
if (line) turnStatus.set(sid, line);
|
|
744
|
-
else turnStatus.delete(sid);
|
|
745
716
|
}
|
|
746
717
|
log("chat.message: submitting capture for previous completed turn", sid);
|
|
747
718
|
await dispatchCapture(sid);
|
|
@@ -789,43 +760,6 @@ var MemoirOpenCode = async (input, rawOptions) => {
|
|
|
789
760
|
log("event: failed", e);
|
|
790
761
|
}
|
|
791
762
|
},
|
|
792
|
-
"experimental.chat.system.transform": async (input2, output) => {
|
|
793
|
-
try {
|
|
794
|
-
const sid = input2.sessionID ?? "default";
|
|
795
|
-
const status = turnStatus.get(sid);
|
|
796
|
-
if (status) output.system?.push(status);
|
|
797
|
-
if (input2.sessionID && parentSessions.has(sid) && !sessionsWithStartupHint.has(sid)) {
|
|
798
|
-
sessionsWithStartupHint.add(sid);
|
|
799
|
-
log("system.transform: startup hint injected for session", sid);
|
|
800
|
-
output.system?.unshift(loadPrompt("startup-hint.tmpl"));
|
|
801
|
-
const client = await connectClient();
|
|
802
|
-
if (client) {
|
|
803
|
-
if (process.env.MEMOIR_SUMMARIZE !== "0") {
|
|
804
|
-
const summary = await callMemoirTool(client, "memoir_summarize", {
|
|
805
|
-
depth: 1
|
|
806
|
-
});
|
|
807
|
-
if (summary) {
|
|
808
|
-
output.system?.unshift(
|
|
809
|
-
`[memoir] Prior context already stored (memoir):
|
|
810
|
-
${summary}`
|
|
811
|
-
);
|
|
812
|
-
log("system.transform: proactive recall injected for session", sid);
|
|
813
|
-
}
|
|
814
|
-
} else {
|
|
815
|
-
log("system.transform: recall skipped (MEMOIR_SUMMARIZE=0)");
|
|
816
|
-
}
|
|
817
|
-
const status2 = await callMemoirTool(client, "memoir_status", {});
|
|
818
|
-
if (status2) {
|
|
819
|
-
output.system?.unshift(`[memoir] Memory store status:
|
|
820
|
-
${status2}`);
|
|
821
|
-
log("system.transform: store status injected for session", sid);
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
} catch (e) {
|
|
826
|
-
log("system.transform: failed", e);
|
|
827
|
-
}
|
|
828
|
-
},
|
|
829
763
|
dispose: async () => {
|
|
830
764
|
try {
|
|
831
765
|
if (process.env.MEMOIR_AUTO_SAVE === "1") {
|
|
@@ -843,13 +777,11 @@ ${status2}`);
|
|
|
843
777
|
} catch (e) {
|
|
844
778
|
log("dispose: save failed", e);
|
|
845
779
|
}
|
|
846
|
-
sessionsWithStartupHint.clear();
|
|
847
780
|
parentSessions.clear();
|
|
848
781
|
capturePending.clear();
|
|
849
782
|
captureLifecycle.clear();
|
|
850
783
|
branchMatcher.clear();
|
|
851
784
|
messageCounts.clear();
|
|
852
|
-
turnStatus.clear();
|
|
853
785
|
lastCaptured.clear();
|
|
854
786
|
await runtime.close();
|
|
855
787
|
}
|
|
@@ -862,6 +794,8 @@ var plugin = {
|
|
|
862
794
|
};
|
|
863
795
|
var index_default = plugin;
|
|
864
796
|
export {
|
|
797
|
+
MemoirBranchMatcher,
|
|
798
|
+
currentGitBranch,
|
|
865
799
|
index_default as default
|
|
866
800
|
};
|
|
867
801
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/debug.ts", "../src/prompts.ts", "../src/subagent.ts", "../src/capture.ts", "../src/capture-lifecycle.ts", "../src/mcp-client.ts", "../src/path.ts", "../src/
|
|
4
|
-
"sourcesContent": ["import { appendFileSync, mkdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\n\n/**\n * Plugin logging.\n *\n * Logs go to a file (never stderr) so they do not pollute the opencode\n * terminal. The destination is controlled by MEMOIR_LOG:\n * - unset \u2192 $X/opencode/memoir-plugin-YYYY-MM-DD.log\n * ($X = XDG_STATE_HOME, falls back to ~/.local/state/...)\n * - \"stderr\" \u2192 stderr, for live local debugging\n * - <path> \u2192 an explicit log file path\n *\n * The log path is cached and re-resolved only when MEMOIR_LOG or the date changes,\n * so writes are fast (single appendFileSync) after the first call.\n *\n * `log(...)` always writes. MEMOIR_DEBUG=1 adds diagnostic detail: Error\n * objects include their name and stack instead of only their message. For\n * local verification, `tail -f` the log file.\n */\n\nlet cachedLogFile: string | null = null;\nlet cachedLogRaw: string | undefined;\nlet cachedLogDate: string | undefined;\n\nfunction resolveLogFile(): string | null {\n const raw = process.env.MEMOIR_LOG;\n if (raw === \"stderr\") return null;\n if (raw) return raw;\n const stateHome = process.env.XDG_STATE_HOME || join(homedir(), \".local\", \"state\");\n const date = new Date().toISOString().slice(0, 10);\n return join(stateHome, \"opencode\", `memoir-plugin-${date}.log`);\n}\n\nfunction getCachedLogFile(): string | null {\n const raw = process.env.MEMOIR_LOG;\n const date = new Date().toISOString().slice(0, 10);\n if (cachedLogRaw !== raw || cachedLogDate !== date) {\n cachedLogFile = resolveLogFile();\n cachedLogRaw = raw;\n cachedLogDate = date;\n }\n return cachedLogFile;\n}\n\nfunction formatValue(value: unknown): string {\n if (value instanceof Error) {\n if (process.env.MEMOIR_DEBUG === \"1\") {\n return value.stack ?? `${value.name}: ${value.message}`;\n }\n return value.message;\n }\n return String(value);\n}\n\nfunction formatArgs(args: unknown[]): string {\n const [message, ...details] = args;\n if (typeof message === \"string\" && details[0] instanceof Error) {\n return `${message.replace(/:\\s*$/, \"\")}: ${details.map(formatValue).join(\" \")}`;\n }\n return args.map(formatValue).join(\" \");\n}\n\nfunction writeLog(args: unknown[]): void {\n const ts = new Date().toISOString().replace(\"T\", \" \").replace(\"Z\", \"\");\n const line = `[memoir ${ts}] ${formatArgs(args)}\\n`;\n const logFilePath = getCachedLogFile();\n if (logFilePath) {\n try {\n mkdirSync(dirname(logFilePath), { recursive: true });\n appendFileSync(logFilePath, line);\n return;\n } catch {\n // fall through to stderr if the file is unavailable\n }\n }\n process.stderr.write(line);\n}\n\n/** Single logging entrypoint; MEMOIR_DEBUG=1 controls detail, not emission. */\nexport function log(...args: unknown[]): void {\n writeLog(args);\n}\n", "import { readFileSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\n\nconst cache = new Map<string, string>();\n\n/**\n * Load a prompt template from src/prompts (dev / tsx) or dist/prompts (bundled).\n * Cached per file. Templates are plain text with ${TOKEN} placeholders that the\n * caller substitutes. Keeping prompts as standalone files (not TS string\n * literals) makes them easy to read and edit in isolation \u2014 mirroring how the\n * upstream memoir plugin keeps hooks/prompts/*.tmpl as first-class artifacts.\n */\nexport function loadPrompt(file: string): string {\n const cached = cache.get(file);\n if (cached !== undefined) return cached;\n const path = fileURLToPath(new URL(`./prompts/${file}`, import.meta.url));\n const text = readFileSync(path, \"utf8\");\n cache.set(file, text);\n return text;\n}\n", "import type { AgentConfig } from \"@opencode-ai/sdk\";\nimport { log } from \"./debug.js\";\nimport { loadPrompt } from \"./prompts.js\";\n\n// opencode restricts an agent's available tools through its `permission`\n// ruleset (NOT a `tools` allowlist \u2014 that field is ignored for config agents).\n// `visibleTools` hides a tool only when the LAST rule matching its name has\n// `{ pattern: \"*\", action: \"deny\" }`. So we deny \"*\" first, then re-allow\n// the memoir_* MCP tools (and doom_loop) after it, then deny checkout. Order\n// matters: the specific checkout deny must be LAST so findLast keeps branch\n// ownership in the plugin while every other memoir tool remains available.\n//\n// The SDK's AgentConfig.permission type is narrower than what opencode's\n// runtime accepts (object form \u2192 Permission.fromConfig), hence the loose\n// intersection below.\ntype MemoirAgentPermission = Record<string, string>;\nexport type MemoirAgentSpec = AgentConfig & {\n permission: MemoirAgentPermission;\n steps?: number;\n};\n\nexport const MEMOIR_AGENT_NAME = \"memoir\";\nexport const MEMOIR_CHECKOUT_TOOL = \"memoir_memoir_checkout\";\n\n// System prompt for the memoir subagent, loaded from prompts/subagent-system.tmpl.\n// It is intentionally tool-free: the subagent is locked to the memoir_* tools\n// via its `permission` ruleset (see buildMemoirAgent), and opencode already\n// exposes those tools to it. The task prompt no longer carries the tool catalog\n// (it was removed); the subagent relies on its static memoir_* permissions\n// instead, so this prompt stays generic and reusable across memoir-mcp versions.\nconst MEMOIR_AGENT_PROMPT = loadPrompt(\"subagent-system.tmpl\");\n\n/**\n * Build the memoir subagent config.\n *\n * Locked to memoir_* tools except checkout (everything else denied) so it can\n * never touch the filesystem, shell, network, or the store-global branch.\n * opencode restricts an agent's tools through its `permission` ruleset \u2014 see\n * MemoirAgentSpec for why the checkout deny must follow the broad allow. Mode\n * \"subagent\" makes\n * opencode spawn it as a detached child session, rendered as a collapsed task\n * (visible in the parent timeline) when prompted via\n * client.session.prompt({ body: { agent: \"memoir\" } }).\n */\nexport function buildMemoirAgent(model?: string): MemoirAgentSpec {\n const agent: MemoirAgentSpec = {\n mode: \"subagent\",\n description:\n \"Memoir memory agent \u2014 captures durable facts and recalls prior context via the memoir MCP server.\",\n prompt: MEMOIR_AGENT_PROMPT,\n permission: {\n \"*\": \"deny\",\n doom_loop: \"allow\",\n \"memoir_*\": \"allow\",\n [MEMOIR_CHECKOUT_TOOL]: \"deny\",\n },\n temperature: 0,\n steps: 8,\n };\n if (model) agent.model = model;\n return agent;\n}\n\ninterface ModelResolution {\n summarizeModel?: string;\n smallModel?: string;\n model?: string;\n}\n\n/**\n * Resolve the model the memoir subagent should use.\n *\n * Priority:\n * 1. MEMOIR_AGENT_MODEL env (provider/model)\n * 2. config.small_model\n * 3. config.model\n * 4. omit \u2014 let opencode pick the session default\n *\n * Only \"provider/model\" shaped values are accepted; anything else is ignored\n * so we never hand opencode a bare model id it cannot resolve.\n */\nexport function resolveMemoirModel(opts: ModelResolution): string | undefined {\n const candidates = [\n process.env.MEMOIR_AGENT_MODEL?.trim(),\n opts.summarizeModel,\n opts.smallModel,\n opts.model,\n ];\n return candidates.find((candidate) => candidate?.includes(\"/\"));\n}\n\n/** Queue a visible subtask through OpenCode's supported promptAsync input API. */\nexport async function runMemoirSubagent(\n client: unknown,\n parentSessionID: string,\n task: string,\n): Promise<void> {\n type SubtaskPartInput = {\n type: \"subtask\";\n agent: string;\n description: string;\n prompt: string;\n command?: string;\n };\n type PromptAsync = (options: {\n path: { id: string };\n body: { parts: SubtaskPartInput[] };\n }) => Promise<unknown>;\n\n const api = (client as { session?: { promptAsync?: PromptAsync } } | null | undefined)?.session;\n if (!api?.promptAsync) {\n throw new Error(\"client.session.promptAsync unavailable\");\n }\n\n try {\n await api.promptAsync({\n path: { id: parentSessionID },\n body: {\n parts: [\n {\n type: \"subtask\",\n agent: MEMOIR_AGENT_NAME,\n description: \"capture\",\n prompt: task,\n command: \"\",\n },\n ],\n },\n });\n log(\"memoir subtask accepted (session\", parentSessionID, \", task\", task.length, \"chars)\");\n } catch (e) {\n log(\"runMemoirSubagent failed\", e);\n throw e;\n }\n}\n", "import { log } from \"./debug.js\";\nimport { loadPrompt } from \"./prompts.js\";\nimport { runMemoirSubagent } from \"./subagent.js\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\ninterface ChatPart {\n type?: string;\n text?: string;\n [key: string]: unknown;\n}\n\nexport interface ChatMessage {\n info?: { id?: string; role?: string };\n parts?: ChatPart[];\n}\n\n// Minimal structural view of the SDK client we use to pull a session transcript.\n// The SDK exposes this as `client.session.messages` (singular `session`).\ntype SessionMessagesClient = {\n session: {\n messages(input: {\n path: { id: string };\n query?: { limit?: number };\n }): Promise<{ data?: unknown }>;\n };\n};\n\n// ---------------------------------------------------------------------------\n// Config\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_CAPTURE_MIN_CHARS = 16;\n\nfunction captureMinChars(): number {\n const raw = process.env.MEMOIR_CAPTURE_MIN_CHARS?.trim();\n if (raw === undefined || raw === \"\") return DEFAULT_CAPTURE_MIN_CHARS;\n const n = Number.parseInt(raw, 10);\n return Number.isFinite(n) ? n : DEFAULT_CAPTURE_MIN_CHARS;\n}\n\nfunction autoSaveEnabled(): boolean {\n // ENABLED by default; only an explicit \"0\" disables it.\n return process.env.MEMOIR_AUTO_SAVE !== \"0\";\n}\n\n// ---------------------------------------------------------------------------\n// Transcript helpers\n// ---------------------------------------------------------------------------\n\nfunction messageText(msg: ChatMessage): string {\n const parts = msg.parts ?? [];\n return parts\n .filter((p) => p.type === \"text\" && typeof p.text === \"string\")\n .map((p) => p.text as string)\n .join(\"\\n\")\n .trim();\n}\n\nfunction formatRole(role: string | undefined): string {\n switch (role) {\n case \"user\":\n return \"USER\";\n case \"assistant\":\n return \"ASSISTANT\";\n case \"system\":\n return \"SYSTEM\";\n default:\n return (role ?? \"unknown\").toUpperCase();\n }\n}\n\n/**\n * Render a list of messages into a plain-text transcript.\n * Skips empty (no text) messages so tool-only turns stay quiet.\n */\nexport function formatSessionTranscript(messages: ChatMessage[]): string {\n return messages\n .map((m) => {\n const text = messageText(m);\n if (!text) return null;\n return `${formatRole(m.info?.role)}\\n${text}`;\n })\n .filter((line): line is string => line !== null)\n .join(\"\\n\\n\");\n}\n\n/**\n * Extract the most recent completed turn: the last assistant message that has\n * text, plus the user message(s) immediately preceding it. Returns null when\n * there is no completed assistant turn yet.\n */\nexport function lastTurnTranscript(messages: ChatMessage[]): string | null {\n let assistantIdx = -1;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].info?.role === \"assistant\" && messageText(messages[i])) {\n assistantIdx = i;\n break;\n }\n }\n if (assistantIdx < 0) return null;\n\n const userIdx = (() => {\n for (let i = assistantIdx - 1; i >= 0; i--) {\n if (messages[i].info?.role === \"user\") return i;\n }\n return -1;\n })();\n if (userIdx < 0) return null;\n\n const turn = messages.slice(userIdx, assistantIdx + 1);\n return formatSessionTranscript(turn);\n}\n\n/**\n * Local, LLM-free pre-filter. When MEMOIR_CAPTURE_MIN_CHARS (default 16) is\n * > 0, only transcripts at least that long are worth sending to the subagent.\n * A value of 0 disables the filter (capture everything).\n */\nexport function shouldCaptureTurn(\n transcript: string | null,\n minChars = captureMinChars(),\n): transcript is string {\n if (minChars <= 0) return transcript !== null && transcript.length > 0;\n return transcript !== null && transcript.length >= minChars;\n}\n\n// ---------------------------------------------------------------------------\n// Task builder (mirrors plugins/claude-code/hooks/stop_capture.tmpl)\n// ---------------------------------------------------------------------------\n\nconst CAPTURE_TASK_TEMPLATE = loadPrompt(\"capture-task.tmpl\");\n\n/**\n * Build the capture task handed to the memoir subagent. It applies the\n * durability checks, writes each fact with an explicit 3-level taxonomy path,\n * and returns a compact report based only on confirmed tool outcomes.\n */\nexport function buildTurnCaptureTask(transcript: string): string {\n return CAPTURE_TASK_TEMPLATE.replace(\"{{TRANSCRIPT}}\", transcript);\n}\n\n// ---------------------------------------------------------------------------\n// Capture orchestration\n// ---------------------------------------------------------------------------\n\nfunction lastAssistantMessageId(messages: ChatMessage[]): string | null {\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].info?.role === \"assistant\" && messageText(messages[i])) {\n return messages[i].info?.id ?? null;\n }\n }\n return null;\n}\n\n/**\n * Capture the latest completed turn of a session into memoir via the subagent.\n *\n * Fire-and-forget: the caller should NOT await the subagent's completion.\n * Deduped per session by the last assistant message id, so a turn is written\n * at most once. Skips entirely when MEMOIR_AUTO_SAVE=0.\n *\n * @param client SDK client (transcript source + subagent spawner)\n * @param sessionID parent session id\n * @param lastCaptured per-session map of already-captured assistant ids\n */\nexport async function captureTurn(\n client: unknown,\n sessionID: string,\n lastCaptured: Map<string, string>,\n): Promise<void> {\n if (!autoSaveEnabled()) {\n log(\"captureTurn: MEMOIR_AUTO_SAVE=0, skipping\");\n return;\n }\n log(\"captureTurn: started for session\", sessionID);\n\n try {\n const api = (client as SessionMessagesClient | null | undefined)?.session;\n if (!api?.messages) {\n log(\"captureTurn: client.session.messages unavailable\");\n log(\"captureTurn: skipped \u2014 session messages API unavailable\");\n return;\n }\n\n const res = await api.messages({\n path: { id: sessionID },\n query: { limit: 50 },\n });\n const raw = res.data;\n const messages: ChatMessage[] = Array.isArray(raw) ? (raw as ChatMessage[]) : [];\n if (messages.length === 0) return;\n\n const turnId = lastAssistantMessageId(messages);\n if (!turnId) return;\n if (lastCaptured.get(sessionID) === turnId) {\n log(\"captureTurn: skipped \u2014 already captured this turn\");\n return;\n }\n\n const transcript = lastTurnTranscript(messages);\n if (!shouldCaptureTurn(transcript)) {\n log(\"captureTurn: skipped \u2014 transcript below min-chars\");\n lastCaptured.set(sessionID, turnId);\n return;\n }\n\n log(\"captureTurn: submitting memoir subtask (transcript\", transcript.length, \"chars)\");\n await runMemoirSubagent(client, sessionID, buildTurnCaptureTask(transcript));\n lastCaptured.set(sessionID, turnId);\n } catch (e) {\n log(\"captureTurn failed\", e);\n }\n}\n", "import { log } from \"./debug.js\";\n\ntype ActiveCapture = {\n done: Promise<void>;\n resolve: () => void;\n childSessionID?: string;\n};\n\n/** Track actual memoir task execution, including native background jobs. */\nexport class CaptureLifecycle {\n private readonly active = new Map<string, ActiveCapture>();\n private readonly callsByChild = new Map<string, string>();\n\n begin(callID: string): void {\n if (this.active.has(callID)) return;\n let resolve!: () => void;\n const done = new Promise<void>((doneResolve) => {\n resolve = doneResolve;\n });\n this.active.set(callID, { done, resolve });\n }\n\n background(callID: string, childSessionID: string): void {\n const capture = this.active.get(callID);\n if (!capture) return;\n capture.childSessionID = childSessionID;\n this.callsByChild.set(childSessionID, callID);\n }\n\n finishCall(callID: string): void {\n const capture = this.active.get(callID);\n if (!capture) return;\n if (capture.childSessionID) this.callsByChild.delete(capture.childSessionID);\n this.active.delete(callID);\n capture.resolve();\n }\n\n finishSession(sessionID: string): void {\n const callID = this.callsByChild.get(sessionID);\n if (callID) this.finishCall(callID);\n }\n\n async drain(timeoutMs = 10_000): Promise<boolean> {\n const pending = [...this.active.values()].map((capture) => capture.done);\n if (pending.length === 0) return true;\n\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timedOut = new Promise<false>((resolve) => {\n timer = setTimeout(() => resolve(false), timeoutMs);\n });\n const drained = Promise.all(pending).then(() => true as const);\n const result = await Promise.race([drained, timedOut]);\n if (timer) clearTimeout(timer);\n if (!result) log(\"capture lifecycle: drain timed out; branch switch deferred\");\n return result;\n }\n\n clear(): void {\n for (const callID of [...this.active.keys()]) this.finishCall(callID);\n }\n}\n", "import { type ChildProcess, spawn } from \"node:child_process\";\nimport { createConnection, createServer } from \"node:net\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport { log } from \"./debug.js\";\nimport { safeRealpath } from \"./path.js\";\n\ninterface MemoirClientState {\n client: Client;\n}\n\ntype ClientConnection = {\n client: Client;\n transport: StreamableHTTPClientTransport;\n};\n\nexport type MemoirRuntimeDependencies = {\n createClientConnection?: (url: URL) => ClientConnection;\n};\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n// Random free port from the upper (ephemeral) range.\nconst LOW_PORT = 49152;\nconst HIGH_PORT = 65535;\n\nfunction randomHighPort(): number {\n return LOW_PORT + Math.floor(Math.random() * (HIGH_PORT - LOW_PORT + 1));\n}\n\nfunction isPortFree(port: number): Promise<boolean> {\n return new Promise((resolve) => {\n const srv = createServer();\n srv.once(\"error\", () => resolve(false));\n srv.once(\"listening\", () => srv.close(() => resolve(true)));\n srv.listen(port, \"127.0.0.1\");\n });\n}\n\nasync function pickFreeHighPort(tries = 20): Promise<number> {\n for (let i = 0; i < tries; i++) {\n const port = randomHighPort();\n if (await isPortFree(port)) return port;\n }\n throw new Error(\"memoir: could not find a free high port for the HTTP server\");\n}\n\nfunction waitForPort(port: number, timeoutMs = 10_000): Promise<void> {\n const deadline = Date.now() + timeoutMs;\n return new Promise((resolve, reject) => {\n const attempt = () => {\n const sock = createConnection({ port, host: \"127.0.0.1\" });\n sock.once(\"connect\", () => {\n sock.destroy();\n resolve();\n });\n sock.once(\"error\", () => {\n sock.destroy();\n if (Date.now() > deadline) {\n reject(new Error(\"memoir: HTTP server did not start in time\"));\n } else {\n setTimeout(attempt, 100);\n }\n });\n };\n attempt();\n });\n}\n\n/**\n * One memoir-mcp process and internal MCP client owned by one plugin instance.\n * OpenCode can host several project directories in one process, so none of\n * this state may live at module scope.\n */\nexport class MemoirRuntime {\n private state: MemoirClientState | null = null;\n private connecting: Promise<Client> | null = null;\n private serverProc: ChildProcess | null = null;\n private serverUrl: URL | null = null;\n private serverPort: number | null = null;\n private startingServer: Promise<URL> | null = null;\n\n constructor(\n private readonly command: string[],\n private readonly env?: Record<string, string>,\n private readonly cwd?: string,\n private readonly dependencies: MemoirRuntimeDependencies = {},\n ) {}\n\n private cleanupClient(): void {\n this.state = null;\n this.connecting = null;\n }\n\n async start(): Promise<URL> {\n if (this.serverUrl && this.serverProc) return this.serverUrl;\n if (this.startingServer) return this.startingServer;\n\n const start = (async (): Promise<URL> => {\n // Retry loop: pickFreeHighPort has a TOCTOU gap (isPortFree \u2192 spawn).\n // If waitForPort times out the port is likely taken \u2014 kill the child and\n // retry with a fresh port.\n const maxTries = 5;\n for (let attempt = 0; attempt < maxTries; attempt++) {\n const port = this.serverPort ?? (await pickFreeHighPort());\n this.serverPort = port;\n const url = new URL(`http://127.0.0.1:${port}/mcp`);\n\n const args = [\n ...this.command.slice(1),\n \"--http\",\n \"--host\",\n \"127.0.0.1\",\n \"--port\",\n String(port),\n ];\n\n const proc = spawn(this.command[0], args, {\n env: { ...process.env, ...this.env },\n cwd: this.cwd ? safeRealpath(this.cwd) : process.cwd(),\n stdio: [\"ignore\", \"ignore\", \"pipe\"],\n });\n\n if (proc.stderr) {\n proc.stderr.on(\"data\", (chunk: Buffer) => {\n log(\"memoir-mcp:\", chunk.toString().trimEnd());\n });\n }\n\n const exited = new Promise<number | null>((resolve) => {\n proc.once(\"exit\", (code) => resolve(code));\n });\n\n await new Promise<void>((resolve, reject) => {\n proc.once(\"spawn\", resolve);\n proc.once(\"error\", reject);\n }).catch((e: unknown) => {\n throw new Error(`memoir: failed to spawn ${this.command[0]}: ${errorMessage(e)}`);\n });\n\n this.serverProc = proc;\n\n try {\n await waitForPort(port);\n } catch {\n // Port may be taken \u2014 kill the child and retry.\n proc.kill();\n await exited;\n this.serverProc = null;\n this.serverUrl = null;\n this.serverPort = null;\n this.cleanupClient();\n continue;\n }\n\n // Late-exit guard: the port check succeeded but another session may\n // have already torn this child down. Only register the URL if our proc\n // is still the active one.\n if (this.serverProc === proc) {\n this.serverUrl = url;\n log(\"memoir-mcp HTTP server up at\", url.toString());\n return url;\n }\n\n this.serverUrl = url;\n log(\"memoir-mcp HTTP server up at\", url.toString());\n return url;\n }\n\n throw new Error(\"memoir: could not start HTTP server after several retries\");\n })();\n\n this.startingServer = start;\n try {\n return await start;\n } catch (e) {\n this.startingServer = null;\n this.serverUrl = null;\n if (this.serverProc) this.serverProc.kill();\n this.serverProc = null;\n this.serverPort = null;\n throw e;\n }\n }\n\n /** Get the lazily-connected internal client for this plugin instance. */\n async connect(): Promise<Client> {\n if (this.state) return this.state.client;\n\n if (!this.connecting) {\n this.connecting = (async () => {\n const url = await this.start();\n const { client, transport } = this.dependencies.createClientConnection?.(url) ?? {\n client: new Client({ name: \"opencode-memoir\", version: \"1.0.0\" }, { capabilities: {} }),\n transport: new StreamableHTTPClientTransport(url),\n };\n\n transport.onclose = () => {\n log(\"mcp-client: transport closed, resetting state\");\n this.cleanupClient();\n };\n\n try {\n await client.connect(transport);\n } catch (e) {\n this.cleanupClient();\n throw e;\n }\n\n this.state = { client };\n return client;\n })();\n }\n\n return this.connecting;\n }\n\n /** Close the MCP client and stop this instance's HTTP server. */\n async close(): Promise<void> {\n if (this.state) {\n try {\n await this.state.client.close();\n } catch (e) {\n log(\"closeMemoirClient: close failed\", e);\n }\n this.cleanupClient();\n }\n if (this.serverProc) {\n const proc = this.serverProc;\n this.serverProc = null;\n try {\n await new Promise<void>((resolve) => {\n if (proc.exitCode !== null) {\n resolve();\n return;\n }\n const timer = setTimeout(() => {\n proc.kill(\"SIGKILL\");\n resolve();\n }, 1_000);\n timer.unref();\n proc.once(\"exit\", () => {\n clearTimeout(timer);\n resolve();\n });\n proc.once(\"error\", () => {\n clearTimeout(timer);\n resolve();\n });\n proc.kill();\n });\n } catch (e) {\n log(\"closeMemoirClient: kill failed\", e);\n }\n }\n this.serverUrl = null;\n this.startingServer = null;\n this.connecting = null;\n }\n}\n\n/**\n * Wrapper around client.callTool that extracts text content.\n * Returns the first `{ type: \"text\" }` content block's text, or null.\n */\nexport async function callMemoirTool(\n client: Client,\n name: string,\n args?: Record<string, unknown>,\n): Promise<string | null> {\n try {\n const result = (await client.callTool({\n name,\n arguments: args ?? {},\n })) as { content: Array<{ type: string; text?: string }>; isError?: boolean };\n if (result.isError) {\n log(`callMemoirTool: ${name} returned error`);\n return null;\n }\n for (const block of result.content) {\n if (block.type === \"text\" && block.text != null) {\n return block.text;\n }\n }\n return null;\n } catch (e) {\n log(`callMemoirTool: ${name} failed`, e);\n return null;\n }\n}\n", "import { execFileSync } from \"node:child_process\";\nimport { realpathSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\n\n/** Resolve a path through symlinks, falling back to the input on failure. */\nexport function safeRealpath(p: string): string {\n try {\n return realpathSync(p);\n } catch {\n return p;\n }\n}\n\n/** Turn an absolute path into a filesystem-safe slug for store naming. */\nexport function slugify(p: string): string {\n return p.replace(/[/.]/g, \"-\");\n}\n\n/**\n * Resolve the memoir store path.\n * 1. `override` (plugin `store` option)\n * 2. MEMOIR_STORE env var\n * 3. Git root \u2192 `~/.memoir/<slug>` (all worktrees share one store)\n * 4. Resolved cwd \u2192 `~/.memoir/<slug>` for non-git dirs\n *\n * The project directory is realpath'd before slugifying so the store is\n * identical whether the repo is entered via a symlink or its real path.\n */\nexport function deriveStorePath(cwd: string = process.cwd(), override?: string): string {\n if (override) return override;\n const envStore = process.env.MEMOIR_STORE;\n if (envStore) return envStore;\n\n const realCwd = safeRealpath(cwd);\n let projectDir: string;\n try {\n const gitRoot = execFileSync(\"git\", [\"rev-parse\", \"--show-toplevel\"], {\n cwd: realCwd,\n encoding: \"utf8\",\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n timeout: 3_000,\n }).trim();\n projectDir = safeRealpath(gitRoot);\n } catch {\n projectDir = resolve(realCwd);\n }\n\n return join(homedir(), \".memoir\", slugify(projectDir));\n}\n", "import { execFileSync } from \"node:child_process\";\nimport type { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { callMemoirTool } from \"./mcp-client.js\";\nimport { deriveStorePath as deriveStorePathBase, safeRealpath } from \"./path.js\";\nimport { parseMemoirStatus } from \"./status.js\";\n\n/** Resolve the memoir store path, honoring the plugin `store` option override. */\nexport function deriveStorePath(cwd?: string, override?: string): string {\n return deriveStorePathBase(cwd, override);\n}\n\n/** Get current git branch (empty string if not in a git repo). */\nexport function currentGitBranch(cwd = process.cwd()): string {\n try {\n return execFileSync(\"git\", [\"branch\", \"--show-current\"], {\n cwd: safeRealpath(cwd),\n encoding: \"utf8\",\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n timeout: 3_000,\n }).trim();\n } catch {\n return \"\";\n }\n}\n\n/**\n * Track the actual branch of one memoir store. The checkout is store-global,\n * so a per-session cache would become stale as soon as another session changed\n * the branch.\n */\nexport class MemoirBranchMatcher {\n private matching: Promise<void> = Promise.resolve();\n\n match(client: Client, cwd: string, drain?: () => Promise<boolean>): Promise<void> {\n const next = this.matching.then(() => this.matchNow(client, cwd, drain));\n this.matching = next.catch(() => undefined);\n return next;\n }\n\n private async currentBranch(client: Client): Promise<string> {\n const raw = await callMemoirTool(client, \"memoir_status\");\n return parseMemoirStatus(raw).branch ?? \"\";\n }\n\n private async matchNow(\n client: Client,\n cwd: string,\n drain?: () => Promise<boolean>,\n ): Promise<void> {\n const codeBranch = currentGitBranch(cwd);\n if (!codeBranch) return;\n\n if ((await this.currentBranch(client)) === codeBranch) return;\n if (drain && !(await drain())) return;\n // A capture or external client may have changed HEAD while we waited.\n if ((await this.currentBranch(client)) === codeBranch) return;\n\n // memoir_checkout (per the v0.2.4 contract) switches to a branch, creating\n // it on demand when `create` is true. A single call replaces the old\n // branches-list \u2192 branch \u2192 checkout CLI sequence.\n await callMemoirTool(client, \"memoir_checkout\", {\n target: codeBranch,\n create: true,\n });\n }\n\n clear(): void {\n this.matching = Promise.resolve();\n }\n}\n", "/**\n * Parsed shape of the `memoir_status` tool response.\n *\n * Both the per-turn model status (turn-status.ts) and the branch matcher\n * (store.ts) read this payload, so the parse lives here once instead of being\n * duplicated with two slightly different decoders.\n */\nexport type MemoirStatus = {\n branch?: string;\n memory_count?: number;\n};\n\n/**\n * Decode a `memoir_status` text payload into a typed, sanitized object.\n * Returns an empty object on a missing or malformed payload so callers can\n * treat every field as optional without guarding parse errors themselves.\n */\nexport function parseMemoirStatus(raw: string | null | undefined): MemoirStatus {\n if (!raw) return {};\n try {\n const status = JSON.parse(raw) as MemoirStatus;\n return {\n branch: typeof status.branch === \"string\" ? status.branch : undefined,\n memory_count: typeof status.memory_count === \"number\" ? status.memory_count : undefined,\n };\n } catch {\n return {};\n }\n}\n", "import { parseMemoirStatus } from \"./status.js\";\n\nexport function buildTurnStatus(statusRaw: string | null): string {\n const { branch, memory_count } = parseMemoirStatus(statusRaw);\n const memoryCount = memory_count ?? 0;\n if (!branch && memoryCount <= 0) return \"\";\n if (!branch) return `[memoir] memory available (${memoryCount} memories)`;\n return `[memoir] ${branch}${memoryCount > 0 ? ` \u00B7 memory available (${memoryCount} memories)` : \"\"}`;\n}\n", "import type { Config, Plugin, PluginInput, PluginModule } from \"@opencode-ai/plugin\";\nimport type { AgentConfig } from \"@opencode-ai/sdk\";\nimport { captureTurn } from \"./capture.js\";\nimport { CaptureLifecycle } from \"./capture-lifecycle.js\";\nimport { log } from \"./debug.js\";\nimport { callMemoirTool, MemoirRuntime } from \"./mcp-client.js\";\nimport { safeRealpath } from \"./path.js\";\nimport { loadPrompt } from \"./prompts.js\";\nimport { deriveStorePath, MemoirBranchMatcher } from \"./store.js\";\nimport { buildMemoirAgent, MEMOIR_AGENT_NAME, resolveMemoirModel } from \"./subagent.js\";\nimport { buildTurnStatus } from \"./turn-status.js\";\n\nfunction envFlag(name: string): boolean | undefined {\n const value = process.env[name]?.trim().toLowerCase();\n if (value === undefined || value === \"\") return undefined;\n return value === \"1\" || value === \"true\";\n}\n\nfunction backgroundSubagentsEnabled(): boolean {\n return (\n envFlag(\"OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS\") ??\n envFlag(\"OPENCODE_EXPERIMENTAL\") ??\n false\n );\n}\n\nconst MemoirOpenCode: Plugin = async (input, rawOptions) => {\n const opts = (rawOptions ?? {}) as { store?: string };\n // SDK client (for subagent spawning + transcript reads) and working dir come\n // from the plugin input. Guard for callers that pass no input.\n const pluginInput = (input ?? {}) as PluginInput;\n const sdkClient = pluginInput.client;\n const directory = safeRealpath(pluginInput.directory ?? process.cwd());\n const storePath = deriveStorePath(directory, opts.store);\n\n const mcpCommand = [\"memoir-mcp\"];\n if (storePath) {\n mcpCommand.push(\"--store\", storePath);\n }\n\n const environment = storePath ? { MEMOIR_STORE: storePath } : undefined;\n const runtime = new MemoirRuntime(mcpCommand, environment, directory);\n const branchMatcher = new MemoirBranchMatcher();\n const sessionsWithStartupHint = new Set<string>();\n const parentSessions = new Set<string>();\n const lastCaptured = new Map<string, string>();\n const capturePending = new Set<string>();\n const captureLifecycle = new CaptureLifecycle();\n const messageCounts = new Map<string, number>();\n const turnStatus = new Map<string, string>();\n\n // The plugin owns a single shared memoir-mcp HTTP server (started in the\n // config hook). opencode connects to it as a remote MCP server; the plugin's\n // own hooks use the same URL via the internal client. One process, no stdio\n // double-spawn. Populated once the HTTP server is up.\n let mcpServer: { type: \"remote\"; url: string; enabled: boolean } | undefined;\n\n // Plugin's own MCP client (separate from the LLM-facing mcpServer).\n // Lazily connected on first use so merely loading the plugin \u2014 or running\n // hooks that never touch memoir \u2014 spawns no subprocess.\n const connectClient = async () => {\n try {\n return await runtime.connect();\n } catch (e) {\n log(\"plugin: failed to connect memoir client\", e);\n return null;\n }\n };\n\n const dispatchCapture = async (sid: string): Promise<void> => {\n if (!sdkClient || !parentSessions.has(sid) || capturePending.has(sid)) return;\n capturePending.add(sid);\n try {\n const client = await connectClient();\n if (client) await branchMatcher.match(client, directory, () => captureLifecycle.drain());\n await captureTurn(sdkClient, sid, lastCaptured);\n } catch (e) {\n log(\"dispatchCapture failed\", e);\n } finally {\n capturePending.delete(sid);\n }\n };\n\n const hooks: Record<string, unknown> = {\n name: \"memoir\",\n\n config: async (config: Config): Promise<void> => {\n log(\"config hook: mcpCommand =\", JSON.stringify(mcpCommand));\n\n try {\n const url = await runtime.start();\n mcpServer = { type: \"remote\", url: url.toString(), enabled: true };\n log(\"memoir MCP server registered at\", url.toString(), \"| store:\", storePath);\n } catch (e) {\n log(\"config hook: failed to start memoir HTTP server\", e);\n mcpServer = undefined;\n }\n\n const mcp = config.mcp ?? {};\n if (mcpServer) mcp.memoir = mcpServer;\n config.mcp = mcp;\n\n // Register on the returned hooks object too \u2014 opencode reads hooks.mcp\n // for the MCP server, and it must reflect the (now known) URL.\n if (mcpServer) hooks.mcp = { memoir: mcpServer };\n\n // Register the memoir subagent (capture + recall). Its model resolves\n // from MEMOIR_AGENT_MODEL \u2192 small_model \u2192 model \u2192 opencode default.\n // opencode reads the agent from `config.agent` (singular), not `agents`.\n const agentModel = resolveMemoirModel({\n summarizeModel: process.env.MEMOIR_AGENT_MODEL,\n smallModel: (config as { small_model?: string }).small_model,\n model: (config as { model?: string }).model,\n });\n const agentField = config as unknown as {\n agent?: Record<string, AgentConfig>;\n };\n agentField.agent = {\n ...(agentField.agent ?? {}),\n [MEMOIR_AGENT_NAME]: buildMemoirAgent(agentModel) as unknown as AgentConfig,\n };\n log(\"memoir subagent registered | model:\", agentModel ?? \"(opencode default)\");\n log(\n \"memoir capture execution mode:\",\n backgroundSubagentsEnabled() ? \"native background\" : \"foreground\",\n \"| OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS:\",\n process.env.OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS ?? \"(unset)\",\n \"| OPENCODE_EXPERIMENTAL:\",\n process.env.OPENCODE_EXPERIMENTAL ?? \"(unset)\",\n );\n\n config.command = config.command ?? {};\n config.command[\"memoir:onboard\"] = {\n description: \"Populate or refresh Memoir onboarding for this project\",\n template: loadPrompt(\"onboard.tmpl\"),\n };\n },\n\n \"shell.env\": async (\n _input: unknown,\n output: { env: Record<string, string> },\n ): Promise<void> => {\n try {\n if (storePath) {\n output.env.MEMOIR_STORE = storePath;\n }\n } catch (e) {\n log(\"shell.env: failed\", e);\n }\n },\n\n \"chat.message\": async (\n input: { sessionID: string; agent?: string; messageID?: string; variant?: string },\n output: {\n parts?: Array<{ type: string; text?: string; synthetic?: boolean; agent?: string }>;\n },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n const parts = output.parts ?? [];\n const isMemoirSubtask = parts.some(\n (part) => part.type === \"subtask\" && part.agent === MEMOIR_AGENT_NAME,\n );\n const isSynthetic = parts.length > 0 && parts.every((part) => part.synthetic === true);\n if (input.agent === MEMOIR_AGENT_NAME || isMemoirSubtask || isSynthetic) return;\n\n parentSessions.add(sid);\n messageCounts.set(sid, (messageCounts.get(sid) ?? 0) + 1);\n\n const client = await connectClient();\n if (client) {\n await branchMatcher.match(client, directory, () => captureLifecycle.drain());\n const status = await callMemoirTool(client, \"memoir_status\", {});\n const line = buildTurnStatus(status);\n if (line) turnStatus.set(sid, line);\n else turnStatus.delete(sid);\n }\n\n // chat.message runs before the new user message is persisted, so the\n // transcript still ends at the previous completed assistant turn.\n // This deliberate one-turn delay avoids capture triggering its own\n // session activity (and therefore recursively triggering itself).\n // promptAsync returns 204 after forking the prompt in OpenCode. Await\n // that acceptance so the visible capture task is queued before this\n // hook releases the parent prompt; subagent execution is not awaited.\n log(\"chat.message: submitting capture for previous completed turn\", sid);\n await dispatchCapture(sid);\n } catch (e) {\n log(\"chat.message: failed\", e);\n }\n },\n\n \"tool.execute.before\": async (\n input: { tool: string; callID: string },\n output: { args?: Record<string, unknown> },\n ): Promise<void> => {\n try {\n if (input.tool !== \"task\" || output.args?.subagent_type !== MEMOIR_AGENT_NAME) return;\n captureLifecycle.begin(input.callID);\n if (!backgroundSubagentsEnabled()) {\n log(\n \"memoir task remains foreground: OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS is disabled\",\n );\n return;\n }\n\n // A `subtask` prompt part does not expose TaskTool's `background`\n // parameter. SessionPrompt passes the mutable task arguments through\n // this hook immediately before TaskTool.execute, so opt only memoir's\n // capture task into OpenCode's native BackgroundJob path here.\n output.args.background = true;\n log(\"memoir capture task promoted to native background job\");\n } catch (e) {\n log(\"tool.execute.before: failed\", e);\n }\n },\n\n \"tool.execute.after\": async (\n input: { tool: string; callID: string; args?: Record<string, unknown> },\n output?: { metadata?: { background?: boolean; sessionId?: string } },\n ): Promise<void> => {\n try {\n if (input.tool !== \"task\" || input.args?.subagent_type !== MEMOIR_AGENT_NAME) return;\n const childSessionID = output?.metadata?.sessionId;\n if (output?.metadata?.background === true && childSessionID) {\n captureLifecycle.background(input.callID, childSessionID);\n return;\n }\n captureLifecycle.finishCall(input.callID);\n } catch (e) {\n log(\"tool.execute.after: failed\", e);\n captureLifecycle.finishCall(input.callID);\n }\n },\n\n event: async (input: { event?: { type?: string; properties?: { sessionID?: string } } }) => {\n try {\n const event = input.event;\n if (event?.type !== \"session.idle\" && event?.type !== \"session.error\") return;\n const sessionID = event.properties?.sessionID;\n if (sessionID) captureLifecycle.finishSession(sessionID);\n } catch (e) {\n log(\"event: failed\", e);\n }\n },\n\n \"experimental.chat.system.transform\": async (\n input: { sessionID?: string },\n output: { system?: string[] },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n\n const status = turnStatus.get(sid);\n if (status) output.system?.push(status);\n\n if (input.sessionID && parentSessions.has(sid) && !sessionsWithStartupHint.has(sid)) {\n sessionsWithStartupHint.add(sid);\n log(\"system.transform: startup hint injected for session\", sid);\n\n // Hint: the main agent owns proactive recall/store via memory tools.\n // Tool names are intentionally omitted \u2014 the agent already sees its\n // allowed memoir_* tools, so naming them here would just be hardcode.\n // The text lives in prompts/startup-hint.tmpl for easy editing.\n output.system?.unshift(loadPrompt(\"startup-hint.tmpl\"));\n\n // Proactive context: surface what memoir already holds + the active\n // store status so the agent starts oriented. Both are cheap and\n // LLM-free. The client is fetched once and reused for both calls.\n const client = await connectClient();\n if (client) {\n if (process.env.MEMOIR_SUMMARIZE !== \"0\") {\n const summary = await callMemoirTool(client, \"memoir_summarize\", {\n depth: 1,\n });\n if (summary) {\n output.system?.unshift(\n `[memoir] Prior context already stored (memoir):\\n${summary}`,\n );\n log(\"system.transform: proactive recall injected for session\", sid);\n }\n } else {\n log(\"system.transform: recall skipped (MEMOIR_SUMMARIZE=0)\");\n }\n\n const status = await callMemoirTool(client, \"memoir_status\", {});\n if (status) {\n output.system?.unshift(`[memoir] Memory store status:\\n${status}`);\n log(\"system.transform: store status injected for session\", sid);\n }\n }\n }\n } catch (e) {\n log(\"system.transform: failed\", e);\n }\n },\n\n dispose: async (): Promise<void> => {\n try {\n if (process.env.MEMOIR_AUTO_SAVE === \"1\") {\n const client = await connectClient();\n if (client) {\n for (const [sid, count] of messageCounts) {\n await callMemoirTool(client, \"memoir_remember\", {\n content: `Session ended \u2014 ${count} user messages exchanged`,\n path: `session.${sid}`,\n merge_policy: \"replace\",\n });\n }\n }\n }\n } catch (e) {\n log(\"dispose: save failed\", e);\n }\n sessionsWithStartupHint.clear();\n parentSessions.clear();\n capturePending.clear();\n captureLifecycle.clear();\n branchMatcher.clear();\n messageCounts.clear();\n turnStatus.clear();\n lastCaptured.clear();\n await runtime.close();\n },\n };\n\n return hooks;\n};\n\nconst plugin: PluginModule = {\n id: \"opencode-memoir\",\n server: MemoirOpenCode,\n};\n\nexport default plugin;\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,gBAAgB,iBAAiB;AAC1C,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AAoB9B,IAAI,gBAA+B;AACnC,IAAI;AACJ,IAAI;AAEJ,SAAS,iBAAgC;AACvC,QAAM,MAAM,QAAQ,IAAI;AACxB,MAAI,QAAQ,SAAU,QAAO;AAC7B,MAAI,IAAK,QAAO;AAChB,QAAM,YAAY,QAAQ,IAAI,kBAAkB,KAAK,QAAQ,GAAG,UAAU,OAAO;AACjF,QAAM,QAAO,oBAAI,KAAK,GAAE,YAAY,EAAE,MAAM,GAAG,EAAE;AACjD,SAAO,KAAK,WAAW,YAAY,iBAAiB,IAAI,MAAM;AAChE;AAEA,SAAS,mBAAkC;AACzC,QAAM,MAAM,QAAQ,IAAI;AACxB,QAA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YACH;AAAA,UACF;AAAA,QACF;AAAA,MACF,SAAS,GAAG;AACV,YAAI,wBAAwB,CAAC;AAAA,MAC/B;AACA,8BAAwB,MAAM;AAC9B,qBAAe,MAAM;AACrB,qBAAe,MAAM;AACrB,uBAAiB,MAAM;AACvB,oBAAc,MAAM;AACpB,oBAAc,MAAM;AACpB,iBAAW,MAAM;AACjB,mBAAa,MAAM;AACnB,YAAM,QAAQ,MAAM;AAAA,IACtB;AAAA,EACF;AAEA,SAAO;AACT;AAEA,IAAM,SAAuB;AAAA,EAC3B,IAAI;AAAA,EACJ,QAAQ;AACV;AAEA,IAAO,gBAAQ;",
|
|
6
|
-
"names": ["resolve", "homedir", "join", "resolve", "execFileSync", "
|
|
3
|
+
"sources": ["../src/index.ts", "../src/debug.ts", "../src/prompts.ts", "../src/subagent.ts", "../src/capture.ts", "../src/capture-lifecycle.ts", "../src/mcp-client.ts", "../src/path.ts", "../src/status.ts"],
|
|
4
|
+
"sourcesContent": ["import { execFileSync } from \"node:child_process\";\nimport type { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport type { Config, Plugin, PluginInput, PluginModule } from \"@opencode-ai/plugin\";\nimport type { AgentConfig } from \"@opencode-ai/sdk\";\nimport { captureTurn } from \"./capture.js\";\nimport { CaptureLifecycle } from \"./capture-lifecycle.js\";\nimport { log } from \"./debug.js\";\nimport { callMemoirTool, MemoirRuntime } from \"./mcp-client.js\";\nimport { deriveStorePath, safeRealpath } from \"./path.js\";\nimport { loadPrompt } from \"./prompts.js\";\nimport { parseMemoirStatus } from \"./status.js\";\nimport { buildMemoirAgent, MEMOIR_AGENT_NAME, resolveMemoirModel } from \"./subagent.js\";\n\nfunction envFlag(name: string): boolean | undefined {\n const value = process.env[name]?.trim().toLowerCase();\n if (value === undefined || value === \"\") return undefined;\n return value === \"1\" || value === \"true\";\n}\n\nfunction backgroundSubagentsEnabled(): boolean {\n return (\n envFlag(\"OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS\") ??\n envFlag(\"OPENCODE_EXPERIMENTAL\") ??\n false\n );\n}\n\n/** Get current git branch (empty string if not in a git repo). */\nexport function currentGitBranch(cwd = process.cwd()): string {\n try {\n return execFileSync(\"git\", [\"branch\", \"--show-current\"], {\n cwd: safeRealpath(cwd),\n encoding: \"utf8\",\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n timeout: 3_000,\n }).trim();\n } catch {\n return \"\";\n }\n}\n\n/**\n * Track the actual branch of one memoir store. The checkout is store-global,\n * so a per-session cache would become stale as soon as another session changed\n * the branch.\n */\nexport class MemoirBranchMatcher {\n private matching: Promise<void> = Promise.resolve();\n\n match(client: Client, cwd: string, drain?: () => Promise<boolean>): Promise<void> {\n const next = this.matching.then(() => this.matchNow(client, cwd, drain));\n this.matching = next.catch(() => undefined);\n return next;\n }\n\n private async currentBranch(client: Client): Promise<string> {\n const raw = await callMemoirTool(client, \"memoir_status\");\n return parseMemoirStatus(raw).branch ?? \"\";\n }\n\n private async matchNow(\n client: Client,\n cwd: string,\n drain?: () => Promise<boolean>,\n ): Promise<void> {\n const codeBranch = currentGitBranch(cwd);\n if (!codeBranch) return;\n\n if ((await this.currentBranch(client)) === codeBranch) return;\n if (drain && !(await drain())) return;\n // A capture or external client may have changed HEAD while we waited.\n if ((await this.currentBranch(client)) === codeBranch) return;\n\n // memoir_checkout (per the v0.2.4 contract) switches to a branch, creating\n // it on demand when `create` is true. A single call replaces the old\n // branches-list \u2192 branch \u2192 checkout CLI sequence.\n await callMemoirTool(client, \"memoir_checkout\", {\n target: codeBranch,\n create: true,\n });\n }\n\n clear(): void {\n this.matching = Promise.resolve();\n }\n}\n\nconst MemoirOpenCode: Plugin = async (input, rawOptions) => {\n const opts = (rawOptions ?? {}) as { store?: string };\n // SDK client (for subagent spawning + transcript reads) and working dir come\n // from the plugin input. Guard for callers that pass no input.\n const pluginInput = (input ?? {}) as PluginInput;\n const sdkClient = pluginInput.client;\n const directory = safeRealpath(pluginInput.directory ?? process.cwd());\n const storePath = deriveStorePath(directory, opts.store);\n\n const mcpCommand = [\"memoir-mcp\"];\n if (storePath) {\n mcpCommand.push(\"--store\", storePath);\n }\n\n const environment = storePath ? { MEMOIR_STORE: storePath } : undefined;\n const runtime = new MemoirRuntime(mcpCommand, environment, directory);\n const branchMatcher = new MemoirBranchMatcher();\n const parentSessions = new Set<string>();\n const lastCaptured = new Map<string, string>();\n const capturePending = new Set<string>();\n const captureLifecycle = new CaptureLifecycle();\n const messageCounts = new Map<string, number>();\n\n // The plugin owns a single shared memoir-mcp HTTP server (started in the\n // config hook). opencode connects to it as a remote MCP server; the plugin's\n // own hooks use the same URL via the internal client. One process, no stdio\n // double-spawn. Populated once the HTTP server is up.\n let mcpServer: { type: \"remote\"; url: string; enabled: boolean } | undefined;\n\n // Plugin's own MCP client (separate from the LLM-facing mcpServer).\n // Lazily connected on first use so merely loading the plugin \u2014 or running\n // hooks that never touch memoir \u2014 spawns no subprocess.\n const connectClient = async () => {\n try {\n return await runtime.connect();\n } catch (e) {\n log(\"plugin: failed to connect memoir client\", e);\n return null;\n }\n };\n\n const dispatchCapture = async (sid: string): Promise<void> => {\n if (!sdkClient || !parentSessions.has(sid) || capturePending.has(sid)) return;\n capturePending.add(sid);\n try {\n const client = await connectClient();\n if (client) await branchMatcher.match(client, directory, () => captureLifecycle.drain());\n await captureTurn(sdkClient, sid, lastCaptured);\n } catch (e) {\n log(\"dispatchCapture failed\", e);\n } finally {\n capturePending.delete(sid);\n }\n };\n\n const hooks: Record<string, unknown> = {\n name: \"memoir\",\n\n config: async (config: Config): Promise<void> => {\n log(\"config hook: mcpCommand =\", JSON.stringify(mcpCommand));\n\n try {\n const url = await runtime.start();\n mcpServer = { type: \"remote\", url: url.toString(), enabled: true };\n log(\"memoir MCP server registered at\", url.toString(), \"| store:\", storePath);\n } catch (e) {\n log(\"config hook: failed to start memoir HTTP server\", e);\n mcpServer = undefined;\n }\n\n const mcp = config.mcp ?? {};\n if (mcpServer) mcp.memoir = mcpServer;\n config.mcp = mcp;\n\n // Register on the returned hooks object too \u2014 opencode reads hooks.mcp\n // for the MCP server, and it must reflect the (now known) URL.\n if (mcpServer) hooks.mcp = { memoir: mcpServer };\n\n // Register the memoir subagent (capture + recall). Its model resolves\n // from MEMOIR_AGENT_MODEL \u2192 small_model \u2192 model \u2192 opencode default.\n // opencode reads the agent from `config.agent` (singular), not `agents`.\n const agentModel = resolveMemoirModel({\n summarizeModel: process.env.MEMOIR_AGENT_MODEL,\n smallModel: (config as { small_model?: string }).small_model,\n model: (config as { model?: string }).model,\n });\n const agentField = config as unknown as {\n agent?: Record<string, AgentConfig>;\n };\n agentField.agent = {\n ...(agentField.agent ?? {}),\n [MEMOIR_AGENT_NAME]: buildMemoirAgent(agentModel) as unknown as AgentConfig,\n };\n log(\"memoir subagent registered | model:\", agentModel ?? \"(opencode default)\");\n log(\n \"memoir capture execution mode:\",\n backgroundSubagentsEnabled() ? \"native background\" : \"foreground\",\n \"| OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS:\",\n process.env.OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS ?? \"(unset)\",\n \"| OPENCODE_EXPERIMENTAL:\",\n process.env.OPENCODE_EXPERIMENTAL ?? \"(unset)\",\n );\n\n config.command = config.command ?? {};\n config.command[\"memoir:onboard\"] = {\n description: \"Populate or refresh Memoir onboarding for this project\",\n template: loadPrompt(\"onboard.tmpl\"),\n };\n },\n\n \"shell.env\": async (\n _input: unknown,\n output: { env: Record<string, string> },\n ): Promise<void> => {\n try {\n if (storePath) {\n output.env.MEMOIR_STORE = storePath;\n }\n } catch (e) {\n log(\"shell.env: failed\", e);\n }\n },\n\n \"chat.message\": async (\n input: { sessionID: string; agent?: string; messageID?: string; variant?: string },\n output: {\n parts?: Array<{ type: string; text?: string; synthetic?: boolean; agent?: string }>;\n },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n const parts = output.parts ?? [];\n const isMemoirSubtask = parts.some(\n (part) => part.type === \"subtask\" && part.agent === MEMOIR_AGENT_NAME,\n );\n const isSynthetic = parts.length > 0 && parts.every((part) => part.synthetic === true);\n if (input.agent === MEMOIR_AGENT_NAME || isMemoirSubtask || isSynthetic) return;\n\n parentSessions.add(sid);\n messageCounts.set(sid, (messageCounts.get(sid) ?? 0) + 1);\n\n const client = await connectClient();\n if (client) {\n await branchMatcher.match(client, directory, () => captureLifecycle.drain());\n }\n\n // chat.message runs before the new user message is persisted, so the\n // transcript still ends at the previous completed assistant turn.\n // This deliberate one-turn delay avoids capture triggering its own\n // session activity (and therefore recursively triggering itself).\n // promptAsync returns 204 after forking the prompt in OpenCode. Await\n // that acceptance so the visible capture task is queued before this\n // hook releases the parent prompt; subagent execution is not awaited.\n log(\"chat.message: submitting capture for previous completed turn\", sid);\n await dispatchCapture(sid);\n } catch (e) {\n log(\"chat.message: failed\", e);\n }\n },\n\n \"tool.execute.before\": async (\n input: { tool: string; callID: string },\n output: { args?: Record<string, unknown> },\n ): Promise<void> => {\n try {\n if (input.tool !== \"task\" || output.args?.subagent_type !== MEMOIR_AGENT_NAME) return;\n captureLifecycle.begin(input.callID);\n if (!backgroundSubagentsEnabled()) {\n log(\n \"memoir task remains foreground: OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS is disabled\",\n );\n return;\n }\n\n // A `subtask` prompt part does not expose TaskTool's `background`\n // parameter. SessionPrompt passes the mutable task arguments through\n // this hook immediately before TaskTool.execute, so opt only memoir's\n // capture task into OpenCode's native BackgroundJob path here.\n output.args.background = true;\n log(\"memoir capture task promoted to native background job\");\n } catch (e) {\n log(\"tool.execute.before: failed\", e);\n }\n },\n\n \"tool.execute.after\": async (\n input: { tool: string; callID: string; args?: Record<string, unknown> },\n output?: { metadata?: { background?: boolean; sessionId?: string } },\n ): Promise<void> => {\n try {\n if (input.tool !== \"task\" || input.args?.subagent_type !== MEMOIR_AGENT_NAME) return;\n const childSessionID = output?.metadata?.sessionId;\n if (output?.metadata?.background === true && childSessionID) {\n captureLifecycle.background(input.callID, childSessionID);\n return;\n }\n captureLifecycle.finishCall(input.callID);\n } catch (e) {\n log(\"tool.execute.after: failed\", e);\n captureLifecycle.finishCall(input.callID);\n }\n },\n\n event: async (input: { event?: { type?: string; properties?: { sessionID?: string } } }) => {\n try {\n const event = input.event;\n if (event?.type !== \"session.idle\" && event?.type !== \"session.error\") return;\n const sessionID = event.properties?.sessionID;\n if (sessionID) captureLifecycle.finishSession(sessionID);\n } catch (e) {\n log(\"event: failed\", e);\n }\n },\n\n dispose: async (): Promise<void> => {\n try {\n if (process.env.MEMOIR_AUTO_SAVE === \"1\") {\n const client = await connectClient();\n if (client) {\n for (const [sid, count] of messageCounts) {\n await callMemoirTool(client, \"memoir_remember\", {\n content: `Session ended \u2014 ${count} user messages exchanged`,\n path: `session.${sid}`,\n merge_policy: \"replace\",\n });\n }\n }\n }\n } catch (e) {\n log(\"dispose: save failed\", e);\n }\n parentSessions.clear();\n capturePending.clear();\n captureLifecycle.clear();\n branchMatcher.clear();\n messageCounts.clear();\n lastCaptured.clear();\n await runtime.close();\n },\n };\n\n return hooks;\n};\n\nconst plugin: PluginModule = {\n id: \"opencode-memoir\",\n server: MemoirOpenCode,\n};\n\nexport default plugin;\n", "import { appendFileSync, mkdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\n\n/**\n * Plugin logging.\n *\n * Logs go to a file (never stderr) so they do not pollute the opencode\n * terminal. The destination is controlled by MEMOIR_LOG:\n * - unset \u2192 $X/opencode/memoir-plugin-YYYY-MM-DD.log\n * ($X = XDG_STATE_HOME, falls back to ~/.local/state/...)\n * - \"stderr\" \u2192 stderr, for live local debugging\n * - <path> \u2192 an explicit log file path\n *\n * The log path is cached and re-resolved only when MEMOIR_LOG or the date changes,\n * so writes are fast (single appendFileSync) after the first call.\n *\n * `log(...)` always writes. MEMOIR_DEBUG=1 adds diagnostic detail: Error\n * objects include their name and stack instead of only their message. For\n * local verification, `tail -f` the log file.\n */\n\nlet cachedLogFile: string | null = null;\nlet cachedLogRaw: string | undefined;\nlet cachedLogDate: string | undefined;\n\nfunction resolveLogFile(): string | null {\n const raw = process.env.MEMOIR_LOG;\n if (raw === \"stderr\") return null;\n if (raw) return raw;\n const stateHome = process.env.XDG_STATE_HOME || join(homedir(), \".local\", \"state\");\n const date = new Date().toISOString().slice(0, 10);\n return join(stateHome, \"opencode\", `memoir-plugin-${date}.log`);\n}\n\nfunction getCachedLogFile(): string | null {\n const raw = process.env.MEMOIR_LOG;\n const date = new Date().toISOString().slice(0, 10);\n if (cachedLogRaw !== raw || cachedLogDate !== date) {\n cachedLogFile = resolveLogFile();\n cachedLogRaw = raw;\n cachedLogDate = date;\n }\n return cachedLogFile;\n}\n\nfunction formatValue(value: unknown): string {\n if (value instanceof Error) {\n if (process.env.MEMOIR_DEBUG === \"1\") {\n return value.stack ?? `${value.name}: ${value.message}`;\n }\n return value.message;\n }\n return String(value);\n}\n\nfunction formatArgs(args: unknown[]): string {\n const [message, ...details] = args;\n if (typeof message === \"string\" && details[0] instanceof Error) {\n return `${message.replace(/:\\s*$/, \"\")}: ${details.map(formatValue).join(\" \")}`;\n }\n return args.map(formatValue).join(\" \");\n}\n\nfunction writeLog(args: unknown[]): void {\n const ts = new Date().toISOString().replace(\"T\", \" \").replace(\"Z\", \"\");\n const line = `[memoir ${ts}] ${formatArgs(args)}\\n`;\n const logFilePath = getCachedLogFile();\n if (logFilePath) {\n try {\n mkdirSync(dirname(logFilePath), { recursive: true });\n appendFileSync(logFilePath, line);\n return;\n } catch {\n // fall through to stderr if the file is unavailable\n }\n }\n process.stderr.write(line);\n}\n\n/** Single logging entrypoint; MEMOIR_DEBUG=1 controls detail, not emission. */\nexport function log(...args: unknown[]): void {\n writeLog(args);\n}\n", "import { readFileSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\n\nconst cache = new Map<string, string>();\n\n/**\n * Load a prompt template from src/prompts (dev / tsx) or dist/prompts (bundled).\n * Cached per file. Templates are plain text with ${TOKEN} placeholders that the\n * caller substitutes. Keeping prompts as standalone files (not TS string\n * literals) makes them easy to read and edit in isolation \u2014 mirroring how the\n * upstream memoir plugin keeps hooks/prompts/*.tmpl as first-class artifacts.\n */\nexport function loadPrompt(file: string): string {\n const cached = cache.get(file);\n if (cached !== undefined) return cached;\n const path = fileURLToPath(new URL(`./prompts/${file}`, import.meta.url));\n const text = readFileSync(path, \"utf8\");\n cache.set(file, text);\n return text;\n}\n", "import type { AgentConfig } from \"@opencode-ai/sdk\";\nimport { log } from \"./debug.js\";\nimport { loadPrompt } from \"./prompts.js\";\n\n// opencode restricts an agent's available tools through its `permission`\n// ruleset (NOT a `tools` allowlist \u2014 that field is ignored for config agents).\n// `visibleTools` hides a tool only when the LAST rule matching its name has\n// `{ pattern: \"*\", action: \"deny\" }`. So we deny \"*\" first, then re-allow\n// the memoir_* MCP tools (and doom_loop) after it, then deny checkout. Order\n// matters: the specific checkout deny must be LAST so findLast keeps branch\n// ownership in the plugin while every other memoir tool remains available.\n//\n// The SDK's AgentConfig.permission type is narrower than what opencode's\n// runtime accepts (object form \u2192 Permission.fromConfig), hence the loose\n// intersection below.\ntype MemoirAgentPermission = Record<string, string>;\nexport type MemoirAgentSpec = AgentConfig & {\n permission: MemoirAgentPermission;\n steps?: number;\n};\n\nexport const MEMOIR_AGENT_NAME = \"memoir\";\nexport const MEMOIR_CHECKOUT_TOOL = \"memoir_memoir_checkout\";\n\n// System prompt for the memoir subagent, loaded from prompts/subagent-system.tmpl.\n// It is intentionally tool-free: the subagent is locked to the memoir_* tools\n// via its `permission` ruleset (see buildMemoirAgent), and opencode already\n// exposes those tools to it. The task prompt no longer carries the tool catalog\n// (it was removed); the subagent relies on its static memoir_* permissions\n// instead, so this prompt stays generic and reusable across memoir-mcp versions.\nconst MEMOIR_AGENT_PROMPT = loadPrompt(\"subagent-system.tmpl\");\n\n/**\n * Build the memoir subagent config.\n *\n * Locked to memoir_* tools except checkout (everything else denied) so it can\n * never touch the filesystem, shell, network, or the store-global branch.\n * opencode restricts an agent's tools through its `permission` ruleset \u2014 see\n * MemoirAgentSpec for why the checkout deny must follow the broad allow. Mode\n * \"subagent\" makes\n * opencode spawn it as a detached child session, rendered as a collapsed task\n * (visible in the parent timeline) when prompted via\n * client.session.prompt({ body: { agent: \"memoir\" } }).\n */\nexport function buildMemoirAgent(model?: string): MemoirAgentSpec {\n const agent: MemoirAgentSpec = {\n mode: \"subagent\",\n description:\n \"Memoir memory agent \u2014 captures durable facts and recalls prior context via the memoir MCP server.\",\n prompt: MEMOIR_AGENT_PROMPT,\n permission: {\n \"*\": \"deny\",\n doom_loop: \"allow\",\n \"memoir_*\": \"allow\",\n [MEMOIR_CHECKOUT_TOOL]: \"deny\",\n },\n temperature: 0,\n steps: 8,\n };\n if (model) agent.model = model;\n return agent;\n}\n\ninterface ModelResolution {\n summarizeModel?: string;\n smallModel?: string;\n model?: string;\n}\n\n/**\n * Resolve the model the memoir subagent should use.\n *\n * Priority:\n * 1. MEMOIR_AGENT_MODEL env (provider/model)\n * 2. config.small_model\n * 3. config.model\n * 4. omit \u2014 let opencode pick the session default\n *\n * Only \"provider/model\" shaped values are accepted; anything else is ignored\n * so we never hand opencode a bare model id it cannot resolve.\n */\nexport function resolveMemoirModel(opts: ModelResolution): string | undefined {\n const candidates = [\n process.env.MEMOIR_AGENT_MODEL?.trim(),\n opts.summarizeModel,\n opts.smallModel,\n opts.model,\n ];\n return candidates.find((candidate) => candidate?.includes(\"/\"));\n}\n\n/** Queue a visible subtask through OpenCode's supported promptAsync input API. */\nexport async function runMemoirSubagent(\n client: unknown,\n parentSessionID: string,\n task: string,\n): Promise<void> {\n type SubtaskPartInput = {\n type: \"subtask\";\n agent: string;\n description: string;\n prompt: string;\n command?: string;\n };\n type PromptAsync = (options: {\n path: { id: string };\n body: { parts: SubtaskPartInput[] };\n }) => Promise<unknown>;\n\n const api = (client as { session?: { promptAsync?: PromptAsync } } | null | undefined)?.session;\n if (!api?.promptAsync) {\n throw new Error(\"client.session.promptAsync unavailable\");\n }\n\n try {\n await api.promptAsync({\n path: { id: parentSessionID },\n body: {\n parts: [\n {\n type: \"subtask\",\n agent: MEMOIR_AGENT_NAME,\n description: \"capture\",\n prompt: task,\n command: \"\",\n },\n ],\n },\n });\n log(\"memoir subtask accepted (session\", parentSessionID, \", task\", task.length, \"chars)\");\n } catch (e) {\n log(\"runMemoirSubagent failed\", e);\n throw e;\n }\n}\n", "import { log } from \"./debug.js\";\nimport { runMemoirSubagent } from \"./subagent.js\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\ninterface ChatPart {\n type?: string;\n text?: string;\n [key: string]: unknown;\n}\n\nexport interface ChatMessage {\n info?: { id?: string; role?: string };\n parts?: ChatPart[];\n}\n\n// Minimal structural view of the SDK client we use to pull a session transcript.\n// The SDK exposes this as `client.session.messages` (singular `session`).\ntype SessionMessagesClient = {\n session: {\n messages(input: {\n path: { id: string };\n query?: { limit?: number };\n }): Promise<{ data?: unknown }>;\n };\n};\n\n// ---------------------------------------------------------------------------\n// Config\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_CAPTURE_MIN_CHARS = 16;\n\nfunction captureMinChars(): number {\n const raw = process.env.MEMOIR_CAPTURE_MIN_CHARS?.trim();\n if (raw === undefined || raw === \"\") return DEFAULT_CAPTURE_MIN_CHARS;\n const n = Number.parseInt(raw, 10);\n return Number.isFinite(n) ? n : DEFAULT_CAPTURE_MIN_CHARS;\n}\n\nfunction autoSaveEnabled(): boolean {\n // ENABLED by default; only an explicit \"0\" disables it.\n return process.env.MEMOIR_AUTO_SAVE !== \"0\";\n}\n\n// ---------------------------------------------------------------------------\n// Transcript helpers\n// ---------------------------------------------------------------------------\n\nfunction messageText(msg: ChatMessage): string {\n const parts = msg.parts ?? [];\n return parts\n .filter((p) => p.type === \"text\" && typeof p.text === \"string\")\n .map((p) => p.text as string)\n .join(\"\\n\")\n .trim();\n}\n\nfunction formatRole(role: string | undefined): string {\n switch (role) {\n case \"user\":\n return \"USER\";\n case \"assistant\":\n return \"ASSISTANT\";\n case \"system\":\n return \"SYSTEM\";\n default:\n return (role ?? \"unknown\").toUpperCase();\n }\n}\n\n/**\n * Render a list of messages into a plain-text transcript.\n * Skips empty (no text) messages so tool-only turns stay quiet.\n */\nexport function formatSessionTranscript(messages: ChatMessage[]): string {\n return messages\n .map((m) => {\n const text = messageText(m);\n if (!text) return null;\n return `${formatRole(m.info?.role)}\\n${text}`;\n })\n .filter((line): line is string => line !== null)\n .join(\"\\n\\n\");\n}\n\n/**\n * Extract the most recent completed turn: the last assistant message that has\n * text, plus the user message(s) immediately preceding it. Returns null when\n * there is no completed assistant turn yet.\n */\nexport function lastTurnTranscript(messages: ChatMessage[]): string | null {\n let assistantIdx = -1;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].info?.role === \"assistant\" && messageText(messages[i])) {\n assistantIdx = i;\n break;\n }\n }\n if (assistantIdx < 0) return null;\n\n const userIdx = (() => {\n for (let i = assistantIdx - 1; i >= 0; i--) {\n if (messages[i].info?.role === \"user\") return i;\n }\n return -1;\n })();\n if (userIdx < 0) return null;\n\n const turn = messages.slice(userIdx, assistantIdx + 1);\n return formatSessionTranscript(turn);\n}\n\n/**\n * Local, LLM-free pre-filter. When MEMOIR_CAPTURE_MIN_CHARS (default 16) is\n * > 0, only transcripts at least that long are worth sending to the subagent.\n * A value of 0 disables the filter (capture everything).\n */\nexport function shouldCaptureTurn(\n transcript: string | null,\n minChars = captureMinChars(),\n): transcript is string {\n if (minChars <= 0) return transcript !== null && transcript.length > 0;\n return transcript !== null && transcript.length >= minChars;\n}\n\n// ---------------------------------------------------------------------------\n// Capture orchestration\n// ---------------------------------------------------------------------------\n\nfunction lastAssistantMessageId(messages: ChatMessage[]): string | null {\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].info?.role === \"assistant\" && messageText(messages[i])) {\n return messages[i].info?.id ?? null;\n }\n }\n return null;\n}\n\n/**\n * Capture the latest completed turn of a session into memoir via the subagent.\n *\n * Fire-and-forget: the caller should NOT await the subagent's completion.\n * Deduped per session by the last assistant message id, so a turn is written\n * at most once. Skips entirely when MEMOIR_AUTO_SAVE=0.\n *\n * @param client SDK client (transcript source + subagent spawner)\n * @param sessionID parent session id\n * @param lastCaptured per-session map of already-captured assistant ids\n */\nexport async function captureTurn(\n client: unknown,\n sessionID: string,\n lastCaptured: Map<string, string>,\n): Promise<void> {\n if (!autoSaveEnabled()) {\n log(\"captureTurn: MEMOIR_AUTO_SAVE=0, skipping\");\n return;\n }\n log(\"captureTurn: started for session\", sessionID);\n\n try {\n const api = (client as SessionMessagesClient | null | undefined)?.session;\n if (!api?.messages) {\n log(\"captureTurn: client.session.messages unavailable\");\n log(\"captureTurn: skipped \u2014 session messages API unavailable\");\n return;\n }\n\n const res = await api.messages({\n path: { id: sessionID },\n query: { limit: 50 },\n });\n const raw = res.data;\n const messages: ChatMessage[] = Array.isArray(raw) ? (raw as ChatMessage[]) : [];\n if (messages.length === 0) return;\n\n const turnId = lastAssistantMessageId(messages);\n if (!turnId) return;\n if (lastCaptured.get(sessionID) === turnId) {\n log(\"captureTurn: skipped \u2014 already captured this turn\");\n return;\n }\n\n const transcript = lastTurnTranscript(messages);\n if (!shouldCaptureTurn(transcript)) {\n log(\"captureTurn: skipped \u2014 transcript below min-chars\");\n lastCaptured.set(sessionID, turnId);\n return;\n }\n\n log(\"captureTurn: submitting memoir subtask (transcript\", transcript.length, \"chars)\");\n await runMemoirSubagent(client, sessionID, transcript);\n lastCaptured.set(sessionID, turnId);\n } catch (e) {\n log(\"captureTurn failed\", e);\n }\n}\n", "import { log } from \"./debug.js\";\n\ntype ActiveCapture = {\n done: Promise<void>;\n resolve: () => void;\n childSessionID?: string;\n};\n\n/** Track actual memoir task execution, including native background jobs. */\nexport class CaptureLifecycle {\n private readonly active = new Map<string, ActiveCapture>();\n private readonly callsByChild = new Map<string, string>();\n\n begin(callID: string): void {\n if (this.active.has(callID)) return;\n let resolve!: () => void;\n const done = new Promise<void>((doneResolve) => {\n resolve = doneResolve;\n });\n this.active.set(callID, { done, resolve });\n }\n\n background(callID: string, childSessionID: string): void {\n const capture = this.active.get(callID);\n if (!capture) return;\n capture.childSessionID = childSessionID;\n this.callsByChild.set(childSessionID, callID);\n }\n\n finishCall(callID: string): void {\n const capture = this.active.get(callID);\n if (!capture) return;\n if (capture.childSessionID) this.callsByChild.delete(capture.childSessionID);\n this.active.delete(callID);\n capture.resolve();\n }\n\n finishSession(sessionID: string): void {\n const callID = this.callsByChild.get(sessionID);\n if (callID) this.finishCall(callID);\n }\n\n async drain(timeoutMs = 10_000): Promise<boolean> {\n const pending = [...this.active.values()].map((capture) => capture.done);\n if (pending.length === 0) return true;\n\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timedOut = new Promise<false>((resolve) => {\n timer = setTimeout(() => resolve(false), timeoutMs);\n });\n const drained = Promise.all(pending).then(() => true as const);\n const result = await Promise.race([drained, timedOut]);\n if (timer) clearTimeout(timer);\n if (!result) log(\"capture lifecycle: drain timed out; branch switch deferred\");\n return result;\n }\n\n clear(): void {\n for (const callID of [...this.active.keys()]) this.finishCall(callID);\n }\n}\n", "import { type ChildProcess, spawn } from \"node:child_process\";\nimport { createConnection, createServer } from \"node:net\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport { log } from \"./debug.js\";\nimport { safeRealpath } from \"./path.js\";\n\ninterface MemoirClientState {\n client: Client;\n}\n\ntype ClientConnection = {\n client: Client;\n transport: StreamableHTTPClientTransport;\n};\n\nexport type MemoirRuntimeDependencies = {\n createClientConnection?: (url: URL) => ClientConnection;\n};\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n// Random free port from the upper (ephemeral) range.\nconst LOW_PORT = 49152;\nconst HIGH_PORT = 65535;\n\nfunction randomHighPort(): number {\n return LOW_PORT + Math.floor(Math.random() * (HIGH_PORT - LOW_PORT + 1));\n}\n\nfunction isPortFree(port: number): Promise<boolean> {\n return new Promise((resolve) => {\n const srv = createServer();\n srv.once(\"error\", () => resolve(false));\n srv.once(\"listening\", () => srv.close(() => resolve(true)));\n srv.listen(port, \"127.0.0.1\");\n });\n}\n\nasync function pickFreeHighPort(tries = 20): Promise<number> {\n for (let i = 0; i < tries; i++) {\n const port = randomHighPort();\n if (await isPortFree(port)) return port;\n }\n throw new Error(\"memoir: could not find a free high port for the HTTP server\");\n}\n\nfunction waitForPort(port: number, timeoutMs = 10_000): Promise<void> {\n const deadline = Date.now() + timeoutMs;\n return new Promise((resolve, reject) => {\n const attempt = () => {\n const sock = createConnection({ port, host: \"127.0.0.1\" });\n sock.once(\"connect\", () => {\n sock.destroy();\n resolve();\n });\n sock.once(\"error\", () => {\n sock.destroy();\n if (Date.now() > deadline) {\n reject(new Error(\"memoir: HTTP server did not start in time\"));\n } else {\n setTimeout(attempt, 100);\n }\n });\n };\n attempt();\n });\n}\n\n/**\n * One memoir-mcp process and internal MCP client owned by one plugin instance.\n * OpenCode can host several project directories in one process, so none of\n * this state may live at module scope.\n */\nexport class MemoirRuntime {\n private state: MemoirClientState | null = null;\n private connecting: Promise<Client> | null = null;\n private serverProc: ChildProcess | null = null;\n private serverUrl: URL | null = null;\n private serverPort: number | null = null;\n private startingServer: Promise<URL> | null = null;\n\n constructor(\n private readonly command: string[],\n private readonly env?: Record<string, string>,\n private readonly cwd?: string,\n private readonly dependencies: MemoirRuntimeDependencies = {},\n ) {}\n\n private cleanupClient(): void {\n this.state = null;\n this.connecting = null;\n }\n\n async start(): Promise<URL> {\n if (this.serverUrl && this.serverProc) return this.serverUrl;\n if (this.startingServer) return this.startingServer;\n\n const start = (async (): Promise<URL> => {\n // Retry loop: pickFreeHighPort has a TOCTOU gap (isPortFree \u2192 spawn).\n // If waitForPort times out the port is likely taken \u2014 kill the child and\n // retry with a fresh port.\n const maxTries = 5;\n for (let attempt = 0; attempt < maxTries; attempt++) {\n const port = this.serverPort ?? (await pickFreeHighPort());\n this.serverPort = port;\n const url = new URL(`http://127.0.0.1:${port}/mcp`);\n\n const args = [\n ...this.command.slice(1),\n \"--http\",\n \"--host\",\n \"127.0.0.1\",\n \"--port\",\n String(port),\n ];\n\n const proc = spawn(this.command[0], args, {\n env: { ...process.env, ...this.env },\n cwd: this.cwd ? safeRealpath(this.cwd) : process.cwd(),\n stdio: [\"ignore\", \"ignore\", \"pipe\"],\n });\n\n if (proc.stderr) {\n proc.stderr.on(\"data\", (chunk: Buffer) => {\n log(\"memoir-mcp:\", chunk.toString().trimEnd());\n });\n }\n\n const exited = new Promise<number | null>((resolve) => {\n proc.once(\"exit\", (code) => resolve(code));\n });\n\n await new Promise<void>((resolve, reject) => {\n proc.once(\"spawn\", resolve);\n proc.once(\"error\", reject);\n }).catch((e: unknown) => {\n throw new Error(`memoir: failed to spawn ${this.command[0]}: ${errorMessage(e)}`);\n });\n\n this.serverProc = proc;\n\n try {\n await waitForPort(port);\n } catch {\n // Port may be taken \u2014 kill the child and retry.\n proc.kill();\n await exited;\n this.serverProc = null;\n this.serverUrl = null;\n this.serverPort = null;\n this.cleanupClient();\n continue;\n }\n\n this.serverUrl = url;\n log(\"memoir-mcp HTTP server up at\", url.toString());\n return url;\n }\n\n throw new Error(\"memoir: could not start HTTP server after several retries\");\n })();\n\n this.startingServer = start;\n try {\n return await start;\n } catch (e) {\n this.startingServer = null;\n this.serverUrl = null;\n if (this.serverProc) this.serverProc.kill();\n this.serverProc = null;\n this.serverPort = null;\n throw e;\n }\n }\n\n /** Get the lazily-connected internal client for this plugin instance. */\n async connect(): Promise<Client> {\n if (this.state) return this.state.client;\n\n if (!this.connecting) {\n this.connecting = (async () => {\n const url = await this.start();\n const { client, transport } = this.dependencies.createClientConnection?.(url) ?? {\n client: new Client({ name: \"opencode-memoir\", version: \"1.0.0\" }, { capabilities: {} }),\n transport: new StreamableHTTPClientTransport(url),\n };\n\n transport.onclose = () => {\n log(\"mcp-client: transport closed, resetting state\");\n this.cleanupClient();\n };\n\n try {\n await client.connect(transport);\n } catch (e) {\n this.cleanupClient();\n throw e;\n }\n\n this.state = { client };\n return client;\n })();\n }\n\n return this.connecting;\n }\n\n /** Close the MCP client and stop this instance's HTTP server. */\n async close(): Promise<void> {\n if (this.state) {\n try {\n await this.state.client.close();\n } catch (e) {\n log(\"closeMemoirClient: close failed\", e);\n }\n this.cleanupClient();\n }\n if (this.serverProc) {\n const proc = this.serverProc;\n this.serverProc = null;\n try {\n await new Promise<void>((resolve) => {\n if (proc.exitCode !== null) {\n resolve();\n return;\n }\n const timer = setTimeout(() => {\n proc.kill(\"SIGKILL\");\n resolve();\n }, 1_000);\n timer.unref();\n proc.once(\"exit\", () => {\n clearTimeout(timer);\n resolve();\n });\n proc.once(\"error\", () => {\n clearTimeout(timer);\n resolve();\n });\n proc.kill();\n });\n } catch (e) {\n log(\"closeMemoirClient: kill failed\", e);\n }\n }\n this.serverUrl = null;\n this.startingServer = null;\n this.connecting = null;\n }\n}\n\n/**\n * Wrapper around client.callTool that extracts text content.\n * Returns the first `{ type: \"text\" }` content block's text, or null.\n */\nexport async function callMemoirTool(\n client: Client,\n name: string,\n args?: Record<string, unknown>,\n): Promise<string | null> {\n try {\n const result = (await client.callTool({\n name,\n arguments: args ?? {},\n })) as { content: Array<{ type: string; text?: string }>; isError?: boolean };\n if (result.isError) {\n log(`callMemoirTool: ${name} returned error`);\n return null;\n }\n for (const block of result.content) {\n if (block.type === \"text\" && block.text != null) {\n return block.text;\n }\n }\n return null;\n } catch (e) {\n log(`callMemoirTool: ${name} failed`, e);\n return null;\n }\n}\n", "import { execFileSync } from \"node:child_process\";\nimport { realpathSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\n\n/** Resolve a path through symlinks, falling back to the input on failure. */\nexport function safeRealpath(p: string): string {\n try {\n return realpathSync(p);\n } catch {\n return p;\n }\n}\n\n/** Turn an absolute path into a filesystem-safe slug for store naming. */\nexport function slugify(p: string): string {\n return p.replace(/[/.]/g, \"-\");\n}\n\n/**\n * Resolve the memoir store path.\n * 1. `override` (plugin `store` option)\n * 2. MEMOIR_STORE env var\n * 3. Git root \u2192 `~/.memoir/<slug>` (all worktrees share one store)\n * 4. Resolved cwd \u2192 `~/.memoir/<slug>` for non-git dirs\n *\n * The project directory is realpath'd before slugifying so the store is\n * identical whether the repo is entered via a symlink or its real path.\n */\nexport function deriveStorePath(cwd: string = process.cwd(), override?: string): string {\n if (override) return override;\n const envStore = process.env.MEMOIR_STORE;\n if (envStore) return envStore;\n\n const realCwd = safeRealpath(cwd);\n let projectDir: string;\n try {\n const gitRoot = execFileSync(\"git\", [\"rev-parse\", \"--show-toplevel\"], {\n cwd: realCwd,\n encoding: \"utf8\",\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n timeout: 3_000,\n }).trim();\n projectDir = safeRealpath(gitRoot);\n } catch {\n projectDir = resolve(realCwd);\n }\n\n return join(homedir(), \".memoir\", slugify(projectDir));\n}\n", "/**\n * Parsed shape of the `memoir_status` tool response.\n *\n * The branch matcher (store.ts) reads this payload, so the parse lives here\n * once instead of being duplicated across callers.\n */\nexport type MemoirStatus = {\n branch?: string;\n memory_count?: number;\n};\n\n/**\n * Decode a `memoir_status` text payload into a typed, sanitized object.\n * Returns an empty object on a missing or malformed payload so callers can\n * treat every field as optional without guarding parse errors themselves.\n */\nexport function parseMemoirStatus(raw: string | null | undefined): MemoirStatus {\n if (!raw) return {};\n try {\n const status = JSON.parse(raw) as MemoirStatus;\n return {\n branch: typeof status.branch === \"string\" ? status.branch : undefined,\n memory_count: typeof status.memory_count === \"number\" ? status.memory_count : undefined,\n };\n } catch {\n return {};\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,gBAAAA,qBAAoB;;;ACA7B,SAAS,gBAAgB,iBAAiB;AAC1C,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AAoB9B,IAAI,gBAA+B;AACnC,IAAI;AACJ,IAAI;AAEJ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tB;AAAA,EACF;AAEA,SAAO;AACT;AAEA,IAAM,SAAuB;AAAA,EAC3B,IAAI;AAAA,EACJ,QAAQ;AACV;AAEA,IAAO,gBAAQ;",
|
|
6
|
+
"names": ["execFileSync", "resolve", "homedir", "join", "resolve", "execFileSync", "input"]
|
|
7
7
|
}
|
package/dist/mcp-client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-client.d.ts","sourceRoot":"","sources":["../src/mcp-client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AAQnG,KAAK,gBAAgB,GAAG;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,6BAA6B,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,sBAAsB,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,gBAAgB,CAAC;CACzD,CAAC;AAqDF;;;;GAIG;AACH,qBAAa,aAAa;IAStB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;IACrB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAX/B,OAAO,CAAC,KAAK,CAAkC;IAC/C,OAAO,CAAC,UAAU,CAAgC;IAClD,OAAO,CAAC,UAAU,CAA6B;IAC/C,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,cAAc,CAA6B;IAEnD,YACmB,OAAO,EAAE,MAAM,EAAE,EACjB,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,YAAA,EAC5B,GAAG,CAAC,EAAE,MAAM,YAAA,EACZ,YAAY,GAAE,yBAA8B,EAC3D;IAEJ,OAAO,CAAC,aAAa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AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAoBxB"}
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
# WHO YOU ARE
|
|
2
2
|
|
|
3
|
-
You manage a git-versioned, taxonomy-structured long-term memory. Your access
|
|
4
|
-
is limited to that memory — every other capability is denied, so you can never
|
|
5
|
-
touch the filesystem, shell, or network.
|
|
3
|
+
You are the Memoir memory agent for this session. You manage a git-versioned, taxonomy-structured long-term memory. Your access is limited to that memory only — every other capability is denied, so you can never touch the filesystem, shell, or network.
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
- Record ONLY verified, durable facts: user-stated preferences/constraints,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- Use an explicit 3-level taxonomy path for every fact you store
|
|
14
|
-
(e.g. preferences.coding.languages, project.architecture.layout,
|
|
15
|
-
decisions.api.auth). Never let a classifier guess the path — you pick it.
|
|
5
|
+
## WHAT TO DO
|
|
6
|
+
|
|
7
|
+
- When you receive input, it is a conversation transcript from the most recently completed turn. Extract durable, verified facts from it and store them in your memory.
|
|
8
|
+
- Record ONLY verified, durable facts: user-stated preferences/constraints, project architecture/facts, decisions, and recurring patterns.
|
|
9
|
+
- Do NOT store raw conversation, ephemeral todo state, or speculative opinions. Any input is either recorded as a memory or discarded.
|
|
10
|
+
- Use an explicit 3-level taxonomy path for every fact you store (e.g. preferences.coding.languages, project.architecture.layout, decisions.api.auth). You pick the path — never let a classifier guess it.
|
|
16
11
|
- Prefer updating an existing path (read it first) over creating duplicates.
|
|
17
|
-
-
|
|
18
|
-
|
|
12
|
+
- NEVER switch the memoir branch. Branch checkout is owned by the plugin so a subagent cannot move the shared store while another session is using it.
|
|
13
|
+
|
|
14
|
+
## WHAT NOT TO DO
|
|
15
|
+
|
|
16
|
+
NEVER OUTPUT ANY TEXT!!!
|
|
17
|
+
NEVER RETURN A RESPONSE!!
|
|
18
|
+
UNDER NO CIRCUMSTANCES ANSWER, REPORT, SUMMARIZE, ACKNOWLEDGE, ASK, OR EMIT ANYTHING, NOT EVEN AN EMPTY MESSAGE!!!
|
|
19
|
+
THE CORRECT OUTCOME IS THE ABSENCE OF ANY REPLY, NOT AN EMPTY ONE!!!
|
package/dist/status.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Parsed shape of the `memoir_status` tool response.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* duplicated with two slightly different decoders.
|
|
4
|
+
* The branch matcher (store.ts) reads this payload, so the parse lives here
|
|
5
|
+
* once instead of being duplicated across callers.
|
|
7
6
|
*/
|
|
8
7
|
export type MemoirStatus = {
|
|
9
8
|
branch?: string;
|
package/dist/status.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,YAAY,CAW9E"}
|
package/package.json
CHANGED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
Capture durable memories from the following completed turn.
|
|
2
|
-
|
|
3
|
-
Follow these rules exactly:
|
|
4
|
-
1. Do not address the user. After processing, return only the compact factual
|
|
5
|
-
report defined below.
|
|
6
|
-
2. If there are no durable facts, make no tool calls.
|
|
7
|
-
3. Keep ONLY durable, verified facts:
|
|
8
|
-
- user-stated preferences / constraints
|
|
9
|
-
- project architecture, conventions, decisions
|
|
10
|
-
- recurring patterns or corrections
|
|
11
|
-
4. DROP: ephemeral todo state, raw command output, speculative opinions,
|
|
12
|
-
anything you inferred rather than observed.
|
|
13
|
-
5. For every fact, store it at an EXPLICIT 3-level taxonomy path
|
|
14
|
-
(e.g. preferences.coding.languages, project.architecture.layout,
|
|
15
|
-
decisions.api.auth). Do not let any classifier invent the path.
|
|
16
|
-
6. Before writing a path that may already exist, read it first and update in
|
|
17
|
-
place instead of duplicating.
|
|
18
|
-
7. Never delete memories or switch branches.
|
|
19
|
-
|
|
20
|
-
Final report format:
|
|
21
|
-
- For successful writes, return `Captured N memories:` followed by one line per
|
|
22
|
-
confirmed write: `- <taxonomy path> — <brief reason>`.
|
|
23
|
-
- If nothing was written, return exactly one line:
|
|
24
|
-
`Captured 0 memories: <brief reason>.`
|
|
25
|
-
- If a write failed, do not count it as captured; add one concise
|
|
26
|
-
`Capture error: <brief error>` line.
|
|
27
|
-
- Report only actual tool outcomes. Do not repeat the transcript, expose hidden
|
|
28
|
-
reasoning, or add advice and commentary. Keep the entire report under 8 lines.
|
|
29
|
-
|
|
30
|
-
Store in the default namespace.
|
|
31
|
-
|
|
32
|
-
--- TURN TRANSCRIPT ---
|
|
33
|
-
{{TRANSCRIPT}}
|
|
34
|
-
--- END TURN TRANSCRIPT ---
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
[memoir] You have memory tools (read + write long-term memory).
|
|
2
|
-
SAVE after: task completion, the user stating preferences/constraints, discovering project facts. RECALL at session start for prior context, and when the user asks about prior work.
|
|
3
|
-
Namespaces: "default" (user context), "codebase:onboard" (project facts in git repos). Taxonomy paths: preferences.*, project.*, codebase.*, decisions.*, learning.*.
|
package/dist/store.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
-
/** Resolve the memoir store path, honoring the plugin `store` option override. */
|
|
3
|
-
export declare function deriveStorePath(cwd?: string, override?: string): string;
|
|
4
|
-
/** Get current git branch (empty string if not in a git repo). */
|
|
5
|
-
export declare function currentGitBranch(cwd?: string): string;
|
|
6
|
-
/**
|
|
7
|
-
* Track the actual branch of one memoir store. The checkout is store-global,
|
|
8
|
-
* so a per-session cache would become stale as soon as another session changed
|
|
9
|
-
* the branch.
|
|
10
|
-
*/
|
|
11
|
-
export declare class MemoirBranchMatcher {
|
|
12
|
-
private matching;
|
|
13
|
-
match(client: Client, cwd: string, drain?: () => Promise<boolean>): Promise<void>;
|
|
14
|
-
private currentBranch;
|
|
15
|
-
private matchNow;
|
|
16
|
-
clear(): void;
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=store.d.ts.map
|
package/dist/store.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAKxE,kFAAkF;AAClF,wBAAgB,eAAe,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED,kEAAkE;AAClE,wBAAgB,gBAAgB,CAAC,GAAG,SAAgB,GAAG,MAAM,CAW5D;AAED;;;;GAIG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAoC;IAEpD,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAIhF;YAEa,aAAa;YAKb,QAAQ;IAsBtB,KAAK,IAAI,IAAI,CAEZ;CACF"}
|
package/dist/turn-status.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"turn-status.d.ts","sourceRoot":"","sources":["../src/turn-status.ts"],"names":[],"mappings":"AAEA,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAMhE"}
|