chat-recall 0.7.25 → 0.7.29

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.
@@ -42,16 +42,37 @@ export declare const MESSAGE_HOOK_EVENTS: readonly string[];
42
42
  * turn that follows ends in a Stop, which starts the next waiter.
43
43
  */
44
44
  export declare const MESSAGE_WAITER_EVENTS: readonly string[];
45
+ /**
46
+ * How long Claude Code leaves the waiter running, in seconds.
47
+ *
48
+ * Claude Code enforces `timeout` on an asyncRewake hook, unlike a plain async
49
+ * one, and kills it at 600 s when the entry sets none. Nothing starts another
50
+ * waiter while the session is idle, so a session idle for longer than that went
51
+ * deaf: a message to it waited until its user typed, which started a turn and
52
+ * delivered the message with the prompt. A day outlives any real idle period,
53
+ * and the waiter still ends with the session (it watches the session's own
54
+ * process) or with the first message (exit 2, which wakes the session).
55
+ */
56
+ export declare const MESSAGE_WAITER_TIMEOUT_SECONDS = 86400;
45
57
  /** The command Claude Code runs for the message hook. Both paths quoted, as above. */
46
58
  export declare function messageHookCommand(execPath: string, hookEntry: string, waiter?: boolean): string;
47
59
  /** True when this hook entry is the message hook, its turn run or its waiter. */
48
60
  export declare function isMessageHookEntry(entry: unknown): boolean;
49
61
  /** True when this hook entry is the waiter. */
50
62
  export declare function isMessageWaiterEntry(entry: unknown): boolean;
63
+ /**
64
+ * True when the waiter entry carries the lifetime this version needs.
65
+ *
66
+ * A profile written before the waiter had a timeout keeps a waiter that dies
67
+ * after 600 s, and `ensureHooks` must replace it even when the version it
68
+ * recorded has not changed.
69
+ */
70
+ export declare function isCurrentMessageWaiterEntry(entry: unknown): boolean;
51
71
  type HookCommand = {
52
72
  type: string;
53
73
  command: string;
54
74
  asyncRewake?: boolean;
75
+ timeout?: number;
55
76
  };
56
77
  /** The `[event, entry]` pairs for the message hook: the turn runs, then the waiters. */
57
78
  export declare function messageHookEntries(execPath: string, hookEntry: string): Array<[string, {
@@ -1 +1 @@
1
- {"version":3,"file":"board-hooks.d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81
81
  export const MESSAGE_WAITER_EVENTS = ['SessionStart', 'UserPromptSubmit', 'Stop'];
82
+ /**
83
+ * How long Claude Code leaves the waiter running, in seconds.
84
+ *
85
+ * Claude Code enforces `timeout` on an asyncRewake hook, unlike a plain async
86
+ * one, and kills it at 600 s when the entry sets none. Nothing starts another
87
+ * waiter while the session is idle, so a session idle for longer than that went
88
+ * deaf: a message to it waited until its user typed, which started a turn and
89
+ * delivered the message with the prompt. A day outlives any real idle period,
90
+ * and the waiter still ends with the session (it watches the session's own
91
+ * process) or with the first message (exit 2, which wakes the session).
92
+ */
93
+ export const MESSAGE_WAITER_TIMEOUT_SECONDS = 86_400;
82
94
  /** The command Claude Code runs for the message hook. Both paths quoted, as above. */
83
95
  export function messageHookCommand(execPath, hookEntry, waiter = false) {
84
96
  return `"${execPath}" "${hookEntry}"${waiter ? ' --wait' : ''}`;
@@ -92,13 +104,28 @@ export function isMessageHookEntry(entry) {
92
104
  export function isMessageWaiterEntry(entry) {
93
105
  return isMessageHookEntry(entry) && /--wait$/.test(entryCommand(entry).trim());
94
106
  }
107
+ /**
108
+ * True when the waiter entry carries the lifetime this version needs.
109
+ *
110
+ * A profile written before the waiter had a timeout keeps a waiter that dies
111
+ * after 600 s, and `ensureHooks` must replace it even when the version it
112
+ * recorded has not changed.
113
+ */
114
+ export function isCurrentMessageWaiterEntry(entry) {
115
+ if (!isMessageWaiterEntry(entry))
116
+ return false;
117
+ return entry.hooks?.[0]?.timeout === MESSAGE_WAITER_TIMEOUT_SECONDS;
118
+ }
95
119
  /** The `[event, entry]` pairs for the message hook: the turn runs, then the waiters. */
96
120
  export function messageHookEntries(execPath, hookEntry) {
97
121
  const turn = messageHookCommand(execPath, hookEntry);
98
122
  const waiter = messageHookCommand(execPath, hookEntry, true);
99
123
  return [
100
124
  ...MESSAGE_HOOK_EVENTS.map((event) => [event, { matcher: '', hooks: [{ type: 'command', command: turn }] }]),
101
- ...MESSAGE_WAITER_EVENTS.map((event) => [event, { matcher: '', hooks: [{ type: 'command', command: waiter, asyncRewake: true }] }]),
125
+ ...MESSAGE_WAITER_EVENTS.map((event) => [event, {
126
+ matcher: '',
127
+ hooks: [{ type: 'command', command: waiter, asyncRewake: true, timeout: MESSAGE_WAITER_TIMEOUT_SECONDS }],
128
+ }]),
102
129
  ];
103
130
  }
104
131
  /** Resolve the CLI entry that a hook should run. */
@@ -1 +1 @@
1
- {"version":3,"file":"board-hooks.js","sourceRoot":"","sources":["../src/board-hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAIjC,uEAAuE;AACvE,SAAS,YAAY,CAAC,KAAc;IAClC,MAAM,GAAG,GAAI,KAAkD,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC;IACrF,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9C,CAAC;AAED,yEAAyE;AACzE,MAAM,CAAC,MAAM,WAAW,GAA8D;IACpF,EAAE,KAAK,EAAE,kBAAkB,EAAE,IAAI,EAAE,OAAO,EAAE;IAC5C,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAgB,EAAE,QAAgB,EAAE,IAAuB;IAC1F,OAAO,IAAI,QAAQ,MAAM,QAAQ,iBAAiB,IAAI,EAAE,CAAC;AAC3D,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,MAAM,GAAG,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;IAChC,IAAI,GAAG,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC/B,IAAI,GAAG,CAAC,QAAQ,CAA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,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACxC,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACjE,CAAC;AAED,gEAAgE;AAChE,MAAM,UAAU,uBAAuB,CAAC,OAAe;IACrD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IACjD,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;AAC1E,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Does this argument list name chat-recall as a channel server?
3
+ *
4
+ * Each flag takes a list that runs until the next argument starting with `-`.
5
+ * The list can also hold `plugin:<name>@<marketplace>` and, when the prompt
6
+ * follows the flag, the prompt itself: a token only matches on an exact name.
7
+ */
8
+ export declare function listsOurServer(args: readonly string[]): boolean;
9
+ /** The command line of a process, or null when it is gone or unreadable. */
10
+ export declare function commandLineOf(pid: number): string[] | null;
11
+ /**
12
+ * The command lines above this process: the parent, then the grandparent.
13
+ *
14
+ * Two, for the same reason the message hook watches two: Claude Code may run us
15
+ * through a shell, and that shell can outlive the Claude process that started
16
+ * it. Deeper would reach a terminal multiplexer or an editor, whose flags say
17
+ * nothing about this session.
18
+ */
19
+ export declare function ancestorPids(from?: number, depth?: number): number[];
20
+ export declare function ancestorCommandLines(pids?: readonly number[]): string[][];
21
+ export interface ChannelDecision {
22
+ enabled: boolean;
23
+ /** Why, for CHAT_RECALL_MESSAGE_HOOK_DEBUG and the log. */
24
+ reason: string;
25
+ }
26
+ /** Whether this session wants channel delivery. */
27
+ export declare function channelDecision(env?: NodeJS.ProcessEnv, args?: readonly string[], ancestors?: () => string[][]): ChannelDecision;
28
+ //# sourceMappingURL=channel-detect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel-detect.d.ts","sourceRoot":"","sources":["../src/channel-detect.ts"],"names":[],"mappings":"AAsCA;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAQ/D;AAED,4EAA4E;AAC5E,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAc1D;AAWD;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,SAAe,EAAE,KAAK,SAAI,GAAG,MAAM,EAAE,CAQrE;AAED,wBAAgB,oBAAoB,CAAC,IAAI,GAAE,SAAS,MAAM,EAAmB,GAAG,MAAM,EAAE,EAAE,CAOzF;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,2DAA2D;IAC3D,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,mDAAmD;AACnD,wBAAgB,eAAe,CAC7B,GAAG,GAAE,MAAM,CAAC,UAAwB,EACpC,IAAI,GAAE,SAAS,MAAM,EAA0B,EAC/C,SAAS,GAAE,MAAM,MAAM,EAAE,EAAyB,GACjD,eAAe,CAQjB"}
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Whether this session asked for chat-recall's channel, and why.
3
+ *
4
+ * Claude Code delivers a channel notification into a session as if the user had
5
+ * typed it, which is the only way to reach a session that is idle: no hook runs,
6
+ * so nothing else can. It is off unless the session names our server on the
7
+ * command line, and the session cannot ask us: the MCP server sees neither the
8
+ * client's arguments nor its own, only its parent's.
9
+ *
10
+ * So this reads the argument lists of the processes above us. `claude` is the
11
+ * parent of the MCP server (or of a shell that runs the hook), and the flag is
12
+ * on its command line, where `/proc` and `ps` both show it.
13
+ *
14
+ * Three ways in, most specific first:
15
+ *
16
+ * CHAT_RECALL_CHANNEL=1 the session wants it (also what a test or a wrapper sets)
17
+ * CHAT_RECALL_CHANNEL=0 never, whatever the command line says
18
+ * --channels server:chat-recall
19
+ * --dangerously-load-development-channels server:chat-recall
20
+ *
21
+ * The second flag is the one that works today: a plain MCP server is not on the
22
+ * approved-channels allowlist, so `--channels` alone is refused and the flag it
23
+ * names is never registered. Both are read because the allowlist is Anthropic's
24
+ * to change, and the day chat-recall is on it, the plain form starts working
25
+ * with no change here.
26
+ *
27
+ * Nothing here imports the engine: the relay calls it before it decides how to
28
+ * serve a session, and the relay's whole point is not to pay for the engine.
29
+ */
30
+ import { execFileSync } from 'node:child_process';
31
+ import { readFileSync } from 'node:fs';
32
+ /** The argument that names our server, in both spellings Claude Code accepts. */
33
+ const OURS = new Set(['server:chat-recall', 'chat-recall']);
34
+ /** The two flags whose value list names the servers of a session. */
35
+ const FLAGS = new Set(['--channels', '--dangerously-load-development-channels']);
36
+ /**
37
+ * Does this argument list name chat-recall as a channel server?
38
+ *
39
+ * Each flag takes a list that runs until the next argument starting with `-`.
40
+ * The list can also hold `plugin:<name>@<marketplace>` and, when the prompt
41
+ * follows the flag, the prompt itself: a token only matches on an exact name.
42
+ */
43
+ export function listsOurServer(args) {
44
+ for (let i = 0; i < args.length; i += 1) {
45
+ if (!FLAGS.has(args[i]))
46
+ continue;
47
+ for (let j = i + 1; j < args.length && !args[j].startsWith('-'); j += 1) {
48
+ if (OURS.has(args[j]))
49
+ return true;
50
+ }
51
+ }
52
+ return false;
53
+ }
54
+ /** The command line of a process, or null when it is gone or unreadable. */
55
+ export function commandLineOf(pid) {
56
+ try {
57
+ const parts = readFileSync(`/proc/${pid}/cmdline`, 'utf-8').split('\0').filter(Boolean);
58
+ if (parts.length)
59
+ return parts;
60
+ }
61
+ catch { /* not Linux, or the process is gone */ }
62
+ // Windows has no `ps -o`; Git Bash's `ps` prints its usage to stderr instead,
63
+ // which is why the hook guards on the platform too.
64
+ if (process.platform === 'win32')
65
+ return null;
66
+ try {
67
+ const out = execFileSync('ps', ['-o', 'args=', '-p', String(pid)], {
68
+ encoding: 'utf8', timeout: 2000, stdio: ['ignore', 'pipe', 'ignore'],
69
+ }).trim();
70
+ return out ? out.split(/\s+/) : null;
71
+ }
72
+ catch {
73
+ return null;
74
+ }
75
+ }
76
+ /** The parent of a process, or null at pid 1 and on Windows. */
77
+ function parentOf(pid) {
78
+ try {
79
+ const stat = readFileSync(`/proc/${pid}/stat`, 'utf-8');
80
+ const ppid = Number(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[1]);
81
+ return ppid > 1 ? ppid : null;
82
+ }
83
+ catch {
84
+ return null;
85
+ }
86
+ }
87
+ /**
88
+ * The command lines above this process: the parent, then the grandparent.
89
+ *
90
+ * Two, for the same reason the message hook watches two: Claude Code may run us
91
+ * through a shell, and that shell can outlive the Claude process that started
92
+ * it. Deeper would reach a terminal multiplexer or an editor, whose flags say
93
+ * nothing about this session.
94
+ */
95
+ export function ancestorPids(from = process.ppid, depth = 2) {
96
+ const out = [];
97
+ let pid = from;
98
+ for (let i = 0; i < depth && pid !== null; i += 1) {
99
+ out.push(pid);
100
+ pid = parentOf(pid);
101
+ }
102
+ return out;
103
+ }
104
+ export function ancestorCommandLines(pids = ancestorPids()) {
105
+ const out = [];
106
+ for (const pid of pids) {
107
+ const argv = commandLineOf(pid);
108
+ if (argv)
109
+ out.push(argv);
110
+ }
111
+ return out;
112
+ }
113
+ /** Whether this session wants channel delivery. */
114
+ export function channelDecision(env = process.env, args = process.argv.slice(2), ancestors = ancestorCommandLines) {
115
+ const asked = env.CHAT_RECALL_CHANNEL;
116
+ if (asked === '0')
117
+ return { enabled: false, reason: 'CHAT_RECALL_CHANNEL=0' };
118
+ if (asked === '1')
119
+ return { enabled: true, reason: 'CHAT_RECALL_CHANNEL=1' };
120
+ if (asked !== undefined && asked !== '')
121
+ return { enabled: false, reason: `CHAT_RECALL_CHANNEL=${asked} is neither 0 nor 1` };
122
+ if (listsOurServer(args))
123
+ return { enabled: true, reason: 'our own --channels flag' };
124
+ if (ancestors().some(listsOurServer))
125
+ return { enabled: true, reason: 'a --channels flag above us' };
126
+ return { enabled: false, reason: 'no channel flag' };
127
+ }
128
+ //# sourceMappingURL=channel-detect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel-detect.js","sourceRoot":"","sources":["../src/channel-detect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,iFAAiF;AACjF,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,oBAAoB,EAAE,aAAa,CAAC,CAAC,CAAC;AAE5D,qEAAqE;AACrE,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,yCAAyC,CAAC,CAAC,CAAC;AAEjF;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,IAAuB;IACpD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAAE,SAAS;QAClC,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACxE,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBAAE,OAAO,IAAI,CAAC;QACrC,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,YAAY,CAAC,SAAS,GAAG,UAAU,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CA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qBAAqB,EAAE,CAAC;IAC9H,IAAI,cAAc,CAAC,IAAI,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,yBAAyB,EAAE,CAAC;IACtF,IAAI,SAAS,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,4BAA4B,EAAE,CAAC;IACrG,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;AACvD,CAAC"}
@@ -0,0 +1,35 @@
1
+ /** After this with no refresh, a waiter stops trusting the marker. */
2
+ export declare const CHANNEL_GONE_MS = 120000;
3
+ /** The state folder the message hook and the waiter already share. */
4
+ export declare function channelStateDir(): string;
5
+ /**
6
+ * Say which session this process belongs to, keyed by the processes above it.
7
+ *
8
+ * The hook is the only thing that ever learns the session id — Claude Code puts
9
+ * it in the hook payload — and it runs in a sibling process of the MCP server,
10
+ * so a file is the only thing they can share. Keyed by the parent pid because
11
+ * that is what both processes have in common: their own `claude`.
12
+ */
13
+ export declare function recordSessionForPids(sessionId: string, pids: readonly number[]): void;
14
+ /** The session id the hook recorded for one of the processes above us. */
15
+ export declare function sessionIdFromPids(pids: readonly number[]): string | null;
16
+ /**
17
+ * Forget the records of processes that are gone.
18
+ *
19
+ * One file per session, and nothing else would ever remove them: the hook is
20
+ * the only writer and it writes only for its own session.
21
+ */
22
+ export declare function pruneSessionPids(): void;
23
+ /**
24
+ * Claim delivery for this session, so the waiter stops competing for it.
25
+ *
26
+ * Two consumers of the same lease would each deliver a message: the loser is
27
+ * only spared a duplicate while the winner's lease holds. The marker is how the
28
+ * waiter knows which of them owns the session.
29
+ */
30
+ export declare function markChannelAlive(sessionId: string): void;
31
+ /** Remove the claim, so the waiter takes over again on the next turn. */
32
+ export declare function clearChannelAlive(sessionId: string): void;
33
+ /** Whether a live channel is delivering for this session right now. */
34
+ export declare function channelOwnsSession(sessionId: string, now?: number): boolean;
35
+ //# sourceMappingURL=channel-state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel-state.d.ts","sourceRoot":"","sources":["../src/channel-state.ts"],"names":[],"mappings":"AAuBA,sEAAsE;AACtE,eAAO,MAAM,eAAe,SAAU,CAAC;AAEvC,sEAAsE;AACtE,wBAAgB,eAAe,IAAI,MAAM,CAExC;AA8BD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAarF;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAWxE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,CASvC;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAMxD;AAED,yEAAyE;AACzE,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAOzD;AAgBD,uEAAuE;AACvE,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,SAAa,GAAG,OAAO,CAS/E"}
@@ -0,0 +1,192 @@
1
+ /**
2
+ * The two files the message hook and the channel share.
3
+ *
4
+ * They run in sibling processes — Claude Code starts one per hook and one MCP
5
+ * server — and they can tell each other only through the state folder both
6
+ * already use. Two questions cross it:
7
+ *
8
+ * by-pid/<pid> which Claude session is this process's parent?
9
+ * The hook is told the session id; the MCP server is not,
10
+ * and cannot ask. Written by the hook, read by the channel.
11
+ *
12
+ * <session>.channel the channel is delivering for this session.
13
+ * Written by the channel every 30 s, read by the waiter,
14
+ * which stands down while it is fresh. Without it both hold
15
+ * the same lease and race for the same message.
16
+ *
17
+ * Deliberately engine-free: the message hook is its own bundle and starts on
18
+ * every turn of every session, so it must not pay for the tool surface.
19
+ */
20
+ import { mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
21
+ import { join } from 'node:path';
22
+ import { getDataDir } from '@chat-recall/engine/core/data-dir.js';
23
+ /** After this with no refresh, a waiter stops trusting the marker. */
24
+ export const CHANNEL_GONE_MS = 120_000;
25
+ /** The state folder the message hook and the waiter already share. */
26
+ export function channelStateDir() {
27
+ return join(getDataDir(), 'hook-state', 'messages');
28
+ }
29
+ function byPidDir() {
30
+ return join(channelStateDir(), 'by-pid');
31
+ }
32
+ function markerPath(sessionId) {
33
+ return join(channelStateDir(), `${sessionId}.channel`);
34
+ }
35
+ function readText(path) {
36
+ try {
37
+ return readFileSync(path, 'utf-8').trim();
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ }
43
+ /**
44
+ * The kernel's boot-relative start time of a process, or null.
45
+ *
46
+ * A pid is reused, and a record that outlived its process would then name
47
+ * another session's claude. The start time is what tells two processes with the
48
+ * same pid apart: it differs even when the pid does not.
49
+ */
50
+ function startTimeOf(pid) {
51
+ try {
52
+ const stat = readFileSync(`/proc/${pid}/stat`, 'utf-8');
53
+ const rest = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
54
+ const started = rest[19]; // field 22 of /proc/<pid>/stat
55
+ return started && /^\d+$/.test(started) ? started : null;
56
+ }
57
+ catch {
58
+ return null;
59
+ } // not Linux, or the process is gone
60
+ }
61
+ /**
62
+ * Say which session this process belongs to, keyed by the processes above it.
63
+ *
64
+ * The hook is the only thing that ever learns the session id — Claude Code puts
65
+ * it in the hook payload — and it runs in a sibling process of the MCP server,
66
+ * so a file is the only thing they can share. Keyed by the parent pid because
67
+ * that is what both processes have in common: their own `claude`.
68
+ */
69
+ export function recordSessionForPids(sessionId, pids) {
70
+ if (!sessionId || !pids.length)
71
+ return;
72
+ try {
73
+ mkdirSync(byPidDir(), { recursive: true });
74
+ for (const pid of pids) {
75
+ if (!Number.isInteger(pid) || pid <= 1)
76
+ continue;
77
+ const path = join(byPidDir(), String(pid));
78
+ const started = startTimeOf(pid);
79
+ const want = started ? `${sessionId} ${started}` : sessionId;
80
+ if (readText(path) === want)
81
+ continue;
82
+ writeFileSync(path, want);
83
+ }
84
+ }
85
+ catch { /* a session without the file keeps the hook path */ }
86
+ }
87
+ /** The session id the hook recorded for one of the processes above us. */
88
+ export function sessionIdFromPids(pids) {
89
+ for (const pid of pids) {
90
+ const raw = readText(join(byPidDir(), String(pid)));
91
+ if (!raw)
92
+ continue;
93
+ const [id, started] = raw.split(' ');
94
+ // A record with a start time is only for the process that wrote it. One
95
+ // without (an older build, or a platform with no /proc) is taken as is.
96
+ if (started !== undefined && started !== startTimeOf(pid))
97
+ continue;
98
+ if (id)
99
+ return id;
100
+ }
101
+ return null;
102
+ }
103
+ /**
104
+ * Forget the records of processes that are gone.
105
+ *
106
+ * One file per session, and nothing else would ever remove them: the hook is
107
+ * the only writer and it writes only for its own session.
108
+ */
109
+ export function pruneSessionPids() {
110
+ try {
111
+ for (const f of readdirSync(byPidDir())) {
112
+ const pid = Number(f);
113
+ if (!Number.isInteger(pid) || pid <= 1) {
114
+ try {
115
+ unlinkSync(join(byPidDir(), f));
116
+ }
117
+ catch { /* raced */ }
118
+ continue;
119
+ }
120
+ if (alive(pid))
121
+ continue;
122
+ try {
123
+ unlinkSync(join(byPidDir(), f));
124
+ }
125
+ catch { /* raced */ }
126
+ }
127
+ }
128
+ catch { /* no directory yet */ }
129
+ }
130
+ /**
131
+ * Claim delivery for this session, so the waiter stops competing for it.
132
+ *
133
+ * Two consumers of the same lease would each deliver a message: the loser is
134
+ * only spared a duplicate while the winner's lease holds. The marker is how the
135
+ * waiter knows which of them owns the session.
136
+ */
137
+ export function markChannelAlive(sessionId) {
138
+ try {
139
+ mkdirSync(channelStateDir(), { recursive: true });
140
+ const started = startTimeOf(process.pid);
141
+ writeFileSync(markerPath(sessionId), started ? `${process.pid} ${started}` : String(process.pid));
142
+ }
143
+ catch { /* the waiter keeps delivering; a duplicate beats a silence */ }
144
+ }
145
+ /** Remove the claim, so the waiter takes over again on the next turn. */
146
+ export function clearChannelAlive(sessionId) {
147
+ try {
148
+ // Only our own claim: a session restarted since may have a newer channel,
149
+ // and removing its marker would hand a live session back to a waiter.
150
+ if (readMarker(sessionId)?.pid !== process.pid)
151
+ return;
152
+ unlinkSync(markerPath(sessionId));
153
+ }
154
+ catch { /* already gone */ }
155
+ }
156
+ function readMarker(sessionId) {
157
+ try {
158
+ const path = markerPath(sessionId);
159
+ const [pidText, started] = readFileSync(path, 'utf-8').trim().split(' ');
160
+ const pid = Number(pidText);
161
+ if (!Number.isInteger(pid) || pid <= 1)
162
+ return null;
163
+ return { pid, started, mtime: statSync(path).mtimeMs };
164
+ }
165
+ catch {
166
+ return null;
167
+ }
168
+ }
169
+ function alive(pid) {
170
+ try {
171
+ process.kill(pid, 0);
172
+ return true;
173
+ }
174
+ catch (e) {
175
+ return e.code === 'EPERM';
176
+ }
177
+ }
178
+ /** Whether a live channel is delivering for this session right now. */
179
+ export function channelOwnsSession(sessionId, now = Date.now()) {
180
+ const marker = readMarker(sessionId);
181
+ if (!marker)
182
+ return false;
183
+ if (now - marker.mtime > CHANNEL_GONE_MS)
184
+ return false;
185
+ if (!alive(marker.pid))
186
+ return false;
187
+ // The pid alone is not enough for a marker that lives for a week: the kernel
188
+ // reuses pids, and a reused one would hold a waiter off a session that has no
189
+ // channel — the silence this file exists to prevent.
190
+ return marker.started === undefined || marker.started === startTimeOf(marker.pid);
191
+ }
192
+ //# sourceMappingURL=channel-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel-state.js","sourceRoot":"","sources":["../src/channel-state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACpG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,MA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GAAG,IAAI,CAAC,GAAG,EAAE;IACpE,MAAM,MAAM,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC;IACrC,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAC1B,IAAI,GAAG,GAAG,MAAM,CAAC,KAAK,GAAG,eAAe;QAAE,OAAO,KAAK,CAAC;IACvD,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC;IACrC,6EAA6E;IAC7E,8EAA8E;IAC9E,qDAAqD;IACrD,OAAO,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,MAAM,CAAC,OAAO,KAAK,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACpF,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The channel: chat-recall's messages delivered into a session that is idle.
3
+ *
4
+ * Every other delivery path chat-recall has is a hook, and a hook runs at the
5
+ * edge of a turn. A session that finished its turn and is waiting for the user
6
+ * runs no hooks at all, so the only thing that could reach it was a waiter
7
+ * process held open with `asyncRewake` and woken by exit code 2 — a process
8
+ * with a lifetime, a timeout, a lock file and a parent to watch. Claude Code's
9
+ * channels need none of that: a local MCP server that declares
10
+ * `capabilities.experimental["claude/channel"]` sends
11
+ * `notifications/claude/channel` and the session takes a turn with it, exactly
12
+ * as if the user had typed it. No turn, no timeout, no lock.
13
+ *
14
+ * What this file does is the half that is ours: find out which session this
15
+ * process belongs to, hold the same long-poll the waiter holds, and hand each
16
+ * batch to the client. The store, the lease and the acknowledgement are the
17
+ * ones already there — a message that the channel takes is leased like any
18
+ * other, so a session cannot get it twice.
19
+ *
20
+ * It runs only when the session named our server on the command line
21
+ * (channel-detect.ts). A session that did not gets nothing from here: its
22
+ * notifications would be dropped by the client, and dropping a message we have
23
+ * already acknowledged is losing it, so we must not start polling at all.
24
+ */
25
+ import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
26
+ export interface Target {
27
+ base: string;
28
+ token: string;
29
+ }
30
+ /**
31
+ * Declare the capability the client looks for.
32
+ *
33
+ * It must be registered BEFORE the transport connects: the client reads
34
+ * `capabilities` from the initialize result and decides once, at connect, that
35
+ * this server may push messages into the session. Registering later is silent —
36
+ * the client has already made up its mind.
37
+ */
38
+ export declare function declareChannel(server: Server): void;
39
+ /**
40
+ * Prepare a session for channel delivery: declare the capability, and return the
41
+ * function that starts delivering, or null when this session did not ask.
42
+ *
43
+ * The two halves are separate because of when the client decides: it reads the
44
+ * capability once, from the initialize result, and never looks again. So the
45
+ * capability goes in before `connect()`, and the loop starts after — polling a
46
+ * server that is not connected yet would deliver into nothing.
47
+ */
48
+ export declare function beginChannel(server: Server, debug?: (reason: string) => void): (() => void) | null;
49
+ export interface ChannelDeps {
50
+ server: Server;
51
+ target: Target;
52
+ /** The session id, once the hook has recorded it. */
53
+ sessionId: () => string | null;
54
+ debug?: (reason: string) => void;
55
+ now?: () => number;
56
+ sleep?: (ms: number) => Promise<void>;
57
+ post?: (target: Target, path: string, body: unknown, ms: number) => Promise<{
58
+ status: number;
59
+ body: unknown;
60
+ }>;
61
+ }
62
+ /**
63
+ * Deliver messages until the session ends.
64
+ *
65
+ * The loop mirrors the waiter's, on purpose: same endpoint, same lease, same
66
+ * acknowledgement rule (ack only after the message has left this process), and
67
+ * the same give-up rules, so a server without this route costs a few retries
68
+ * rather than a spin.
69
+ */
70
+ export declare function runChannel(deps: ChannelDeps): Promise<void>;
71
+ //# sourceMappingURL=channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAcxE,MAAM,WAAW,MAAM;IAAG,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AAEvD;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,MA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