agent-recall-mcp 3.4.42 → 3.4.44

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/dist/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
3
  import { VERSION, getRoot, getLegacyRoot } from "agent-recall-core";
4
4
  import { server } from "./server.js";
5
+ import { installAmbientCapture } from "./lib/ambient-capture.js";
6
+ import { installLifecycleExitHandlers } from "./lib/lifecycle-exit.js";
5
7
  // ── v3.4 primary tools (5-tool surface) ──────────────────────────────────
6
8
  import { register as registerSessionStart } from "./tools/session-start.js";
7
9
  import { register as registerRemember } from "./tools/remember.js";
@@ -147,6 +149,13 @@ if (args.includes("--list-tools")) {
147
149
  process.stdout.write(JSON.stringify(tools, null, 2) + "\n");
148
150
  process.exit(0);
149
151
  }
152
+ // ── C-1 (Train C, 2026-08-12 wave) — passive ambient capture ────────────────
153
+ // Must run BEFORE any register*(server) call below: wraps server.registerTool
154
+ // once so EVERY tool registration (present AND future, default surface AND
155
+ // --full/AR_EXTRAS) gets one scrubbed working-memory line per call, keyed by
156
+ // this process's SESSION_ID. See lib/ambient-capture.ts's doc comment for
157
+ // the full design rationale.
158
+ installAmbientCapture(server);
150
159
  // ── Default surface: 5 tools (two verbs + three essentials) ─────────────────
151
160
  // Automaticity Law: only memory that arrives unasked gets used.
152
161
  // Push channels (session_start/end, hooks) drive behavior; pull channels don't.
@@ -187,6 +196,10 @@ registerSessionPrompts(server);
187
196
  async function main() {
188
197
  const transport = new StdioServerTransport();
189
198
  await server.connect(transport);
199
+ // C-3 (Train C, 2026-08-12 wave) — best-effort freshness on graceful exit.
200
+ // Installed once the transport is connected (nothing to distill before
201
+ // then — no tool calls could have happened yet). See lib/lifecycle-exit.ts.
202
+ installLifecycleExitHandlers();
190
203
  }
191
204
  main().catch((err) => {
192
205
  process.stderr.write(`Fatal: ${err}\n`);
package/dist/index.js.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CAAC,CAAC,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * ambient-capture.ts — C-1 (Train C, 2026-08-12 wave, design doc
3
+ * reports/2026-08-12-trainc-design.md).
4
+ *
5
+ * WHY: doctrine (owner, 2026-07-26) — the customer's only action is
6
+ * describing intent; memory must arrive front-to-back with ZERO commands.
7
+ * On Claude Code, hooks (hook-ambient) guarantee that by capturing every
8
+ * UserPromptSubmit into working memory (`wmAppend`, storage/working-memory.ts).
9
+ * On a hook-less host (Codex/Cursor/raw MCP) there is no hook-ambient at all
10
+ * — the ONLY signal the MCP server process ever sees is the tool calls
11
+ * themselves. This module makes every tool call carry the same minutes-level,
12
+ * crash-proof capture that hook-ambient gives Claude Code, without wiring a
13
+ * capture call into each of the ~20 individual tool files.
14
+ *
15
+ * Design decision (CHALLENGE noted in the Train C build report): the design
16
+ * doc sketches `withAmbientCapture(name, handler)` as a wrapper "applied at
17
+ * each `registerTool` site". This module instead wraps the `McpServer`
18
+ * INSTANCE's `registerTool` method ONCE, before any `register*(server)` call
19
+ * runs (see mcp-server/src/index.ts). That is a strictly stronger reading of
20
+ * the design's own stated intent — "the wrapper is the class, per-tool wiring
21
+ * is the anti-pattern" — because it requires editing ZERO of the ~20 existing
22
+ * tool files and, by construction, covers every FUTURE tool registered on
23
+ * this server with no additional wiring at all. Per-call-site wrapping would
24
+ * have needed a matching edit in every tools/*.ts file today AND every one
25
+ * added later — exactly the per-tool anti-pattern the design doc warns
26
+ * against.
27
+ *
28
+ * Contract (matches `wmAppend`'s own hot-path guarantees — this runs on
29
+ * EVERY tool call, not just session_start/hook-start):
30
+ * - Never throws: any error while capturing is swallowed. A gist that can't
31
+ * be built or written must never fail, delay, or alter the real tool
32
+ * response.
33
+ * - O(1) per call: no directory scans, no awaits — `wmAppend` itself is a
34
+ * single small file append (see working-memory.ts's own doc for why).
35
+ * - Runs synchronously BEFORE invoking the real handler and does not await
36
+ * anything, so it adds microseconds, not a tick, to the tool's response
37
+ * time.
38
+ * - Scrub-at-choke-point is entirely `wmAppend`'s job (content-guard.ts) —
39
+ * this module does not duplicate that; it only builds the raw gist text
40
+ * that flows into the SAME choke point every other WM writer already uses.
41
+ */
42
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
43
+ /**
44
+ * Install ambient capture on an `McpServer` instance by wrapping its
45
+ * `registerTool` method. Must be called BEFORE any `register*(server)`
46
+ * function runs (mcp-server/src/index.ts calls this immediately after
47
+ * importing `server` from `./server.js`, ahead of every tool registration).
48
+ *
49
+ * Idempotent-safe to call more than once on the same instance (each call
50
+ * re-wraps the CURRENT `registerTool`, so double-installation would only
51
+ * double-append — this module's own index.ts call site invokes it exactly
52
+ * once, and there is no other call site).
53
+ *
54
+ * H1 fix (review, post-build) — no-op on a hook-owned host (see
55
+ * `isHookOwnedHost`'s doc comment, agent-recall-core/host-profile.ts, for the
56
+ * full root-cause writeup). On Claude Code with hooks active, hook-ambient
57
+ * already captures every prompt into working memory under the REAL session
58
+ * id; this module's own `getSessionId()` is a random id generated once per
59
+ * MCP-server PROCESS, uncorrelated with that real id. Installing anyway
60
+ * would make every tool call append a SECOND, independently-orphaned
61
+ * working-memory file that C-2/C-3 would each turn into a duplicate
62
+ * card + recency entry for the same logical session (reproduced — see the
63
+ * fix report's live-process env probe). Skipping installation entirely
64
+ * means `server.registerTool` is left completely untouched on a hook-owned
65
+ * host — zero added overhead, not just zero writes.
66
+ *
67
+ * Accepted v1 tradeoff: a Claude Code user who has somehow disabled hooks
68
+ * but still runs this MCP server gets no ambient capture from the MCP side
69
+ * either (there is no reliable env signal to distinguish "Claude Code with
70
+ * hooks disabled" from "Claude Code with hooks active" — both set
71
+ * CLAUDECODE/CLAUDE_CODE_*). Acceptable: that host already had zero ambient
72
+ * capture before Train C shipped, so this is a no-regression case, not a new
73
+ * gap.
74
+ */
75
+ export declare function installAmbientCapture(server: McpServer): void;
76
+ //# sourceMappingURL=ambient-capture.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ambient-capture.d.ts","sourceRoot":"","sources":["../../src/lib/ambient-capture.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAgGzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAY7D"}
@@ -0,0 +1,175 @@
1
+ /**
2
+ * ambient-capture.ts — C-1 (Train C, 2026-08-12 wave, design doc
3
+ * reports/2026-08-12-trainc-design.md).
4
+ *
5
+ * WHY: doctrine (owner, 2026-07-26) — the customer's only action is
6
+ * describing intent; memory must arrive front-to-back with ZERO commands.
7
+ * On Claude Code, hooks (hook-ambient) guarantee that by capturing every
8
+ * UserPromptSubmit into working memory (`wmAppend`, storage/working-memory.ts).
9
+ * On a hook-less host (Codex/Cursor/raw MCP) there is no hook-ambient at all
10
+ * — the ONLY signal the MCP server process ever sees is the tool calls
11
+ * themselves. This module makes every tool call carry the same minutes-level,
12
+ * crash-proof capture that hook-ambient gives Claude Code, without wiring a
13
+ * capture call into each of the ~20 individual tool files.
14
+ *
15
+ * Design decision (CHALLENGE noted in the Train C build report): the design
16
+ * doc sketches `withAmbientCapture(name, handler)` as a wrapper "applied at
17
+ * each `registerTool` site". This module instead wraps the `McpServer`
18
+ * INSTANCE's `registerTool` method ONCE, before any `register*(server)` call
19
+ * runs (see mcp-server/src/index.ts). That is a strictly stronger reading of
20
+ * the design's own stated intent — "the wrapper is the class, per-tool wiring
21
+ * is the anti-pattern" — because it requires editing ZERO of the ~20 existing
22
+ * tool files and, by construction, covers every FUTURE tool registered on
23
+ * this server with no additional wiring at all. Per-call-site wrapping would
24
+ * have needed a matching edit in every tools/*.ts file today AND every one
25
+ * added later — exactly the per-tool anti-pattern the design doc warns
26
+ * against.
27
+ *
28
+ * Contract (matches `wmAppend`'s own hot-path guarantees — this runs on
29
+ * EVERY tool call, not just session_start/hook-start):
30
+ * - Never throws: any error while capturing is swallowed. A gist that can't
31
+ * be built or written must never fail, delay, or alter the real tool
32
+ * response.
33
+ * - O(1) per call: no directory scans, no awaits — `wmAppend` itself is a
34
+ * single small file append (see working-memory.ts's own doc for why).
35
+ * - Runs synchronously BEFORE invoking the real handler and does not await
36
+ * anything, so it adds microseconds, not a tick, to the tool's response
37
+ * time.
38
+ * - Scrub-at-choke-point is entirely `wmAppend`'s job (content-guard.ts) —
39
+ * this module does not duplicate that; it only builds the raw gist text
40
+ * that flows into the SAME choke point every other WM writer already uses.
41
+ */
42
+ import { getSessionId, wmAppend, truncateUtf8Bytes, isHookOwnedHost } from "agent-recall-core";
43
+ /**
44
+ * Byte cap for the raw gist text handed to `wmAppend`. `wmAppend` re-caps to
45
+ * `WM_PROMPT_BYTE_CAP` (300 bytes) internally regardless, but pre-trimming
46
+ * here keeps `JSON.stringify` cheap on a pathologically large tool-call
47
+ * payload (e.g. a `remember` call with an 8192-char content field) instead of
48
+ * serializing the whole thing just to throw most of it away one line later.
49
+ */
50
+ const GIST_PRETRIM_BYTES = 400;
51
+ /**
52
+ * Field names, checked in priority order, that best represent "what this
53
+ * tool call is about" for a human skimming working memory later — mirrors
54
+ * the design doc's own examples ("query text, remember content head, check
55
+ * goal"). Falls back to a compact JSON dump of the whole args object when
56
+ * none of these are present (covers tools like `pipeline_open`/`register_rule`
57
+ * whose salient field isn't in this list) so the gist is never empty for a
58
+ * tool call that did carry real arguments.
59
+ */
60
+ const PREFERRED_GIST_FIELDS = [
61
+ "query",
62
+ "goal",
63
+ "understanding",
64
+ "summary",
65
+ "content",
66
+ "action_description",
67
+ "name",
68
+ "keyword",
69
+ "project",
70
+ ];
71
+ /** Best-effort single-line summary of a tool call's arguments. Never throws. */
72
+ function gistOf(toolName, args) {
73
+ try {
74
+ let argsText = "";
75
+ if (args && typeof args === "object" && !Array.isArray(args)) {
76
+ const obj = args;
77
+ for (const key of PREFERRED_GIST_FIELDS) {
78
+ const v = obj[key];
79
+ if (typeof v === "string" && v.trim().length > 0) {
80
+ argsText = v;
81
+ break;
82
+ }
83
+ }
84
+ if (!argsText) {
85
+ const keys = Object.keys(obj);
86
+ argsText = keys.length > 0 ? JSON.stringify(obj) : "";
87
+ }
88
+ }
89
+ else if (typeof args === "string") {
90
+ argsText = args;
91
+ }
92
+ const gist = argsText ? `${toolName}: ${argsText}` : toolName;
93
+ return truncateUtf8Bytes(gist, GIST_PRETRIM_BYTES);
94
+ }
95
+ catch {
96
+ return toolName;
97
+ }
98
+ }
99
+ /**
100
+ * Wrap a tool handler so every invocation appends one scrubbed, byte-capped
101
+ * working-memory line before running the real handler. Handles BOTH tool
102
+ * calling conventions the SDK uses (see mcp.js's `executeToolHandler`):
103
+ * `(args, extra)` when the tool has an `inputSchema`, `(extra)` for a
104
+ * zero-argument tool. This wrapper is arity-agnostic (rest params) and
105
+ * forwards whatever it received, unchanged, to the real handler — it never
106
+ * has to know or guess which calling convention a given tool uses.
107
+ */
108
+ function withAmbientCapture(toolName, handler) {
109
+ const wrapped = (...handlerArgs) => {
110
+ try {
111
+ const args = handlerArgs.length > 1 ? handlerArgs[0] : undefined;
112
+ // H2 fix (review, post-build): hook-ambient's own wmAppend call site
113
+ // (packages/cli/src/index.ts) always passes `cwd` (falling back to
114
+ // this PROCESS's `process.cwd()` when the hook JSON carries none) —
115
+ // this call site omitted it entirely. `guessSlugFromWmLines`
116
+ // (storage/working-memory.ts) can ONLY attribute a rescued session to
117
+ // its real project via each line's `cwd` field; without it, every
118
+ // session captured through THIS module (MCP-only hosts — Codex,
119
+ // Cursor, raw MCP) fell back to the literal "auto" slug on rescue,
120
+ // even when the MCP server's own cwd unambiguously pointed at a real
121
+ // project directory. `process.cwd()` is the right signal here for the
122
+ // same reason it's hook-ambient's fallback: a Claude-Code-spawned hook
123
+ // process's cwd is that session's working directory, and an MCP
124
+ // server process's cwd is likewise the directory its host launched it
125
+ // from/in.
126
+ wmAppend(getSessionId(), { ts: new Date().toISOString(), prompt: gistOf(toolName, args), cwd: process.cwd() });
127
+ }
128
+ catch {
129
+ // Never let ambient capture affect the real tool call.
130
+ }
131
+ return handler(...handlerArgs);
132
+ };
133
+ return wrapped;
134
+ }
135
+ /**
136
+ * Install ambient capture on an `McpServer` instance by wrapping its
137
+ * `registerTool` method. Must be called BEFORE any `register*(server)`
138
+ * function runs (mcp-server/src/index.ts calls this immediately after
139
+ * importing `server` from `./server.js`, ahead of every tool registration).
140
+ *
141
+ * Idempotent-safe to call more than once on the same instance (each call
142
+ * re-wraps the CURRENT `registerTool`, so double-installation would only
143
+ * double-append — this module's own index.ts call site invokes it exactly
144
+ * once, and there is no other call site).
145
+ *
146
+ * H1 fix (review, post-build) — no-op on a hook-owned host (see
147
+ * `isHookOwnedHost`'s doc comment, agent-recall-core/host-profile.ts, for the
148
+ * full root-cause writeup). On Claude Code with hooks active, hook-ambient
149
+ * already captures every prompt into working memory under the REAL session
150
+ * id; this module's own `getSessionId()` is a random id generated once per
151
+ * MCP-server PROCESS, uncorrelated with that real id. Installing anyway
152
+ * would make every tool call append a SECOND, independently-orphaned
153
+ * working-memory file that C-2/C-3 would each turn into a duplicate
154
+ * card + recency entry for the same logical session (reproduced — see the
155
+ * fix report's live-process env probe). Skipping installation entirely
156
+ * means `server.registerTool` is left completely untouched on a hook-owned
157
+ * host — zero added overhead, not just zero writes.
158
+ *
159
+ * Accepted v1 tradeoff: a Claude Code user who has somehow disabled hooks
160
+ * but still runs this MCP server gets no ambient capture from the MCP side
161
+ * either (there is no reliable env signal to distinguish "Claude Code with
162
+ * hooks disabled" from "Claude Code with hooks active" — both set
163
+ * CLAUDECODE/CLAUDE_CODE_*). Acceptable: that host already had zero ambient
164
+ * capture before Train C shipped, so this is a no-regression case, not a new
165
+ * gap.
166
+ */
167
+ export function installAmbientCapture(server) {
168
+ if (isHookOwnedHost())
169
+ return;
170
+ const originalRegisterTool = server.registerTool.bind(server);
171
+ server.registerTool = ((name, config, handler) => {
172
+ return originalRegisterTool(name, config, withAmbientCapture(name, handler));
173
+ });
174
+ }
175
+ //# sourceMappingURL=ambient-capture.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ambient-capture.js","sourceRoot":"","sources":["../../src/lib/ambient-capture.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAGH,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAE/F;;;;;;GAMG;AACH,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAE/B;;;;;;;;GAQG;AACH,MAAM,qBAAqB,GAAG;IAC5B,OAAO;IACP,MAAM;IACN,eAAe;IACf,SAAS;IACT,SAAS;IACT,oBAAoB;IACpB,MAAM;IACN,SAAS;IACT,SAAS;CACD,CAAC;AAEX,gFAAgF;AAChF,SAAS,MAAM,CAAC,QAAgB,EAAE,IAAa;IAC7C,IAAI,CAAC;QACH,IAAI,QAAQ,GAAG,EAAE,CAAC;QAClB,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7D,MAAM,GAAG,GAAG,IAA+B,CAAC;YAC5C,KAAK,MAAM,GAAG,IAAI,qBAAqB,EAAE,CAAC;gBACxC,MAAM,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;gBACnB,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACjD,QAAQ,GAAG,CAAC,CAAC;oBACb,MAAM;gBACR,CAAC;YACH,CAAC;YACD,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAC9B,QAAQ,GAAG,IAAI,CA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CAAC,CAAC;IACpD,CAAC,CAAC;IACF,OAAO,OAAY,CAAC;AACtB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAiB;IACrD,IAAI,eAAe,EAAE;QAAE,OAAO;IAE9B,MAAM,oBAAoB,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAE9D,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC,IAAY,EAAE,MAAe,EAAE,OAA0C,EAAE,EAAE;QACnG,OAAQ,oBAA2D,CACjE,IAAI,EACJ,MAAM,EACN,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,CAClC,CAAC;IACJ,CAAC,CAA+B,CAAC;AACnC,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * lifecycle-exit.ts — C-3 (Train C, 2026-08-12 wave, design doc
3
+ * reports/2026-08-12-trainc-design.md).
4
+ *
5
+ * WHY: today the MCP server has NO lifecycle callback at all for a graceful
6
+ * client disconnect — the SDK's `StdioServerTransport` never listens for
7
+ * `stdin` "end"/"close" (recon fact, design doc), so a client that simply
8
+ * closes the connection leaves this process's working-memory file sitting
9
+ * on disk until a LATER session's orphan-rescue sweep (C-2,
10
+ * `rescueOrphanedWorkingMemory`) finds it — up to `WM_ORPHAN_WINDOW_MS` (1h)
11
+ * later. This module closes that gap for the graceful-close case: as soon as
12
+ * stdin ends/closes, or the process receives SIGTERM/SIGINT, distill this
13
+ * session's own working memory into a card immediately.
14
+ *
15
+ * `kill -9` (SIGKILL) is, by construction, UNCATCHABLE — no handler here (or
16
+ * anywhere in Node) can run in that case. That is fully expected and BY
17
+ * DESIGN: the design doc explicitly scopes this module to "best-effort
18
+ * freshness... kill -9 falls through to C-2 by design" — durability for the
19
+ * SIGKILL case is C-2's job (the next session_start/hook-start's orphan
20
+ * sweep), not this module's. This module is a freshness optimization on top
21
+ * of that durability guarantee, never a replacement for it.
22
+ */
23
+ /**
24
+ * Install the one-shot graceful-exit handlers. Call once, after the
25
+ * transport is connected (mcp-server/src/index.ts's `main()`). Safe to call
26
+ * only once per process — `runOnce`'s `fired` guard makes any additional
27
+ * registered listener a no-op, but this function itself is not meant to be
28
+ * (and is not) called more than once.
29
+ *
30
+ * H1 fix (review, post-build) — no-op on a hook-owned host (see
31
+ * `isHookOwnedHost`'s doc comment, agent-recall-core/host-profile.ts, for
32
+ * the full root-cause writeup, and ambient-capture.ts's matching gate on
33
+ * `installAmbientCapture`, which this mirrors). On Claude Code with hooks
34
+ * active, hook-end already owns end-of-session distillation for the REAL
35
+ * session id; this module's `distillSessionToCard(getSessionId())` targets
36
+ * a different, MCP-process-local random id. Post the ambient-capture gate,
37
+ * that id has no working-memory file to distill anyway, so this handler
38
+ * would already be a no-op in practice — but registering the SIGTERM/SIGINT
39
+ * listeners themselves is still skipped, so this MCP server process never
40
+ * intercepts a signal that the host's own hook/process-supervision stack
41
+ * expects to observe unhandled (e.g. Node's default terminate-on-SIGTERM),
42
+ * and never calls `process.exit(0)` on a host that did not ask for that.
43
+ */
44
+ export declare function installLifecycleExitHandlers(): void;
45
+ //# sourceMappingURL=lifecycle-exit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle-exit.d.ts","sourceRoot":"","sources":["../../src/lib/lifecycle-exit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAuCH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,4BAA4B,IAAI,IAAI,CAOnD"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * lifecycle-exit.ts — C-3 (Train C, 2026-08-12 wave, design doc
3
+ * reports/2026-08-12-trainc-design.md).
4
+ *
5
+ * WHY: today the MCP server has NO lifecycle callback at all for a graceful
6
+ * client disconnect — the SDK's `StdioServerTransport` never listens for
7
+ * `stdin` "end"/"close" (recon fact, design doc), so a client that simply
8
+ * closes the connection leaves this process's working-memory file sitting
9
+ * on disk until a LATER session's orphan-rescue sweep (C-2,
10
+ * `rescueOrphanedWorkingMemory`) finds it — up to `WM_ORPHAN_WINDOW_MS` (1h)
11
+ * later. This module closes that gap for the graceful-close case: as soon as
12
+ * stdin ends/closes, or the process receives SIGTERM/SIGINT, distill this
13
+ * session's own working memory into a card immediately.
14
+ *
15
+ * `kill -9` (SIGKILL) is, by construction, UNCATCHABLE — no handler here (or
16
+ * anywhere in Node) can run in that case. That is fully expected and BY
17
+ * DESIGN: the design doc explicitly scopes this module to "best-effort
18
+ * freshness... kill -9 falls through to C-2 by design" — durability for the
19
+ * SIGKILL case is C-2's job (the next session_start/hook-start's orphan
20
+ * sweep), not this module's. This module is a freshness optimization on top
21
+ * of that durability guarantee, never a replacement for it.
22
+ */
23
+ import { getSessionId, distillSessionToCard, isHookOwnedHost } from "agent-recall-core";
24
+ /** Hard ceiling on how long this handler may delay process exit (design doc C-3 guard). */
25
+ const EXIT_TIMEOUT_MS = 2000;
26
+ /** Idempotency guard — `stdin` "end" AND "close" can both fire, and a signal could race either. */
27
+ let fired = false;
28
+ function runOnce() {
29
+ if (fired)
30
+ return;
31
+ fired = true;
32
+ // Safety net: distillSessionToCard's own work (a small number of
33
+ // synchronous fs calls) completes in well under a millisecond in the
34
+ // normal case, but this handler must NEVER delay process exit by more
35
+ // than EXIT_TIMEOUT_MS regardless of what the filesystem does. `.unref()`
36
+ // so this timer itself can never be the reason the process stays alive.
37
+ const forceExit = setTimeout(() => {
38
+ process.exit(0);
39
+ }, EXIT_TIMEOUT_MS);
40
+ forceExit.unref();
41
+ try {
42
+ // Reuses the SAME WM→card mechanism C-2's orphan-rescue sweep uses
43
+ // (distillOneSession, storage/working-memory.ts) for THIS process's own,
44
+ // still-fresh session id — no age gate, because this fires precisely
45
+ // because the session is ending right now.
46
+ distillSessionToCard(getSessionId());
47
+ }
48
+ catch {
49
+ // distillSessionToCard never throws by its own contract — guard kept
50
+ // for defense-in-depth only, matching every other C-2/C-3 call site.
51
+ }
52
+ finally {
53
+ clearTimeout(forceExit);
54
+ process.exit(0);
55
+ }
56
+ }
57
+ /**
58
+ * Install the one-shot graceful-exit handlers. Call once, after the
59
+ * transport is connected (mcp-server/src/index.ts's `main()`). Safe to call
60
+ * only once per process — `runOnce`'s `fired` guard makes any additional
61
+ * registered listener a no-op, but this function itself is not meant to be
62
+ * (and is not) called more than once.
63
+ *
64
+ * H1 fix (review, post-build) — no-op on a hook-owned host (see
65
+ * `isHookOwnedHost`'s doc comment, agent-recall-core/host-profile.ts, for
66
+ * the full root-cause writeup, and ambient-capture.ts's matching gate on
67
+ * `installAmbientCapture`, which this mirrors). On Claude Code with hooks
68
+ * active, hook-end already owns end-of-session distillation for the REAL
69
+ * session id; this module's `distillSessionToCard(getSessionId())` targets
70
+ * a different, MCP-process-local random id. Post the ambient-capture gate,
71
+ * that id has no working-memory file to distill anyway, so this handler
72
+ * would already be a no-op in practice — but registering the SIGTERM/SIGINT
73
+ * listeners themselves is still skipped, so this MCP server process never
74
+ * intercepts a signal that the host's own hook/process-supervision stack
75
+ * expects to observe unhandled (e.g. Node's default terminate-on-SIGTERM),
76
+ * and never calls `process.exit(0)` on a host that did not ask for that.
77
+ */
78
+ export function installLifecycleExitHandlers() {
79
+ if (isHookOwnedHost())
80
+ return;
81
+ process.stdin.on("end", runOnce);
82
+ process.stdin.on("close", runOnce);
83
+ process.on("SIGTERM", runOnce);
84
+ process.on("SIGINT", runOnce);
85
+ }
86
+ //# sourceMappingURL=lifecycle-exit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle-exit.js","sourceRoot":"","sources":["../../src/lib/lifecycle-exit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAExF,2FAA2F;AAC3F,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,mGAAmG;AACnG,IAAI,KAAK,GAAG,KAAK,CAAC;AAElB,SAAS,OAAO;IACd,IAAI,KAAK;QAAE,OAAO;IAClB,KAAK,GAAG,IAAI,CAAC;IAEb,iEAAiE;IACjE,qEAAqE;IACrE,sEAAsE;IACtE,0EAA0E;IAC1E,wEAAwE;IACxE,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;QAChC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,EAAE,eAAe,CAAC,CAAC;IACpB,SAAS,CAAC,KAAK,EAAE,CAAC;IAElB,IAAI,CAAC;QACH,mEAAmE;QACnE,yEAAyE;QACzE,qEAAqE;QACrE,2CAA2C;QAC3C,oBAAoB,CAAC,YAAY,EAAE,CAAC,CAAC;IACvC,CAAC;IAAC,MAAM,CAAC;QACP,qEAAqE;QACrE,qEAAqE;IACvE,CAAC;YAAS,CAAC;QACT,YAAY,CAAC,SAAS,CAAC,CAAC;QACxB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,4BAA4B;IAC1C,IAAI,eAAe,EAAE;QAAE,OAAO;IAE9B,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACjC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACnC,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-recall-mcp",
3
- "version": "3.4.42",
3
+ "version": "3.4.44",
4
4
  "mcpName": "io.github.Goldentrii/agent-recall",
5
5
  "description": "Memory Palace MCP server for AI agents",
6
6
  "type": "module",
@@ -17,7 +17,7 @@
17
17
  "test": "node --test test/*.test.mjs"
18
18
  },
19
19
  "dependencies": {
20
- "agent-recall-core": "3.4.42",
20
+ "agent-recall-core": "3.4.44",
21
21
  "@modelcontextprotocol/sdk": "^1.28.0",
22
22
  "zod": "^3.24.0"
23
23
  },