chat-recall 0.7.39 → 0.7.41
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 +22 -14
- package/dist/cli.js +698 -410
- package/dist/cli.js.map +1 -1
- package/dist/install-hooks.d.ts +11 -3
- package/dist/install-hooks.d.ts.map +1 -1
- package/dist/install-hooks.js +10 -5
- package/dist/install-hooks.js.map +1 -1
- package/dist/kit-store.d.ts +1 -1
- package/dist/kit-store.d.ts.map +1 -1
- package/dist/kit-store.js +2 -2
- package/dist/kit-store.js.map +1 -1
- package/dist/mcp-host.d.ts +50 -0
- package/dist/mcp-host.d.ts.map +1 -0
- package/dist/mcp-host.js +111 -0
- package/dist/mcp-host.js.map +1 -0
- package/dist/mcp.js +1023 -981
- package/dist/mcp.js.map +1 -1
- package/dist/memory-guard.d.ts +41 -0
- package/dist/memory-guard.d.ts.map +1 -0
- package/dist/memory-guard.js +56 -0
- package/dist/memory-guard.js.map +1 -0
- package/dist/message-hook.js +1 -1
- package/dist/service-installer.js +2 -2
- package/dist/service-installer.js.map +1 -1
- package/dist/session-exit.d.ts +50 -0
- package/dist/session-exit.d.ts.map +1 -0
- package/dist/session-exit.js +70 -0
- package/dist/session-exit.js.map +1 -0
- package/dist/star-nudge.js +1 -1
- package/dist/sync-client.d.ts.map +1 -1
- package/dist/sync-client.js +5 -6
- package/dist/sync-client.js.map +1 -1
- package/dist/vendor/mcp-kit/VERSION +1 -0
- package/dist/vendor/mcp-kit/host-main.d.ts +2 -0
- package/dist/vendor/mcp-kit/host-main.js +3 -0
- package/dist/vendor/mcp-kit/host-plain.d.ts +56 -0
- package/dist/vendor/mcp-kit/host-plain.js +502 -0
- package/dist/vendor/mcp-kit/host.d.ts +32 -0
- package/dist/vendor/mcp-kit/host.js +43 -0
- package/dist/vendor/mcp-kit/registry.d.ts +79 -0
- package/dist/vendor/mcp-kit/registry.js +194 -0
- package/dist/watch.js +47 -14
- package/package.json +2 -2
- package/dist/mcp-relay.d.ts +0 -3
- package/dist/mcp-relay.d.ts.map +0 -1
- package/dist/mcp-relay.js +0 -472
- package/dist/mcp-relay.js.map +0 -1
- package/dist/mcp-socket.d.ts +0 -98
- package/dist/mcp-socket.d.ts.map +0 -1
- package/dist/mcp-socket.js +0 -249
- package/dist/mcp-socket.js.map +0 -1
- package/dist/relay-lifecycle.d.ts +0 -92
- package/dist/relay-lifecycle.d.ts.map +0 -1
- package/dist/relay-lifecycle.js +0 -290
- package/dist/relay-lifecycle.js.map +0 -1
package/dist/mcp-socket.js
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Where a chat-recall MCP daemon listens, and how a session reaches it.
|
|
3
|
-
*
|
|
4
|
-
* One daemon per (profile, server, version) — not one per session. Every MCP
|
|
5
|
-
* session on a machine used to be a full copy of this package: the whole bundle
|
|
6
|
-
* loaded, its own background sync loop, its own skills check. They now share
|
|
7
|
-
* one, and each session is a thin relay onto it.
|
|
8
|
-
*
|
|
9
|
-
* Imported by BOTH the relay and the server, so it may use Node builtins only.
|
|
10
|
-
* Anything heavier here lands in the relay bundle and undoes the point of it.
|
|
11
|
-
*/
|
|
12
|
-
import { createHash } from 'node:crypto';
|
|
13
|
-
import { tmpdir } from 'node:os';
|
|
14
|
-
import { join } from 'node:path';
|
|
15
|
-
import { accessSync, chmodSync, constants, mkdirSync, statSync } from 'node:fs';
|
|
16
|
-
import { dirname } from 'node:path';
|
|
17
|
-
/** POSIX allows 108 bytes for a socket path, macOS 104. Stay well inside. */
|
|
18
|
-
const MAX_PATH = 100;
|
|
19
|
-
/** Room a candidate directory must leave for `/<16-hex slug>.sock`. */
|
|
20
|
-
const fits = (dir) => dir.length + 1 + 16 + 5 <= MAX_PATH;
|
|
21
|
-
/**
|
|
22
|
-
* Every directory the socket and the daemon log may live in, best first.
|
|
23
|
-
*
|
|
24
|
-
* XDG_RUNTIME_DIR first: it is per-user, mode 0700, and cleared on logout, so a
|
|
25
|
-
* socket never outlives the login session that made it. macOS never sets it,
|
|
26
|
-
* hence the per-uid directory under TMPDIR — which is also what keeps one
|
|
27
|
-
* user's socket out of another's reach on a shared /tmp. `/tmp` itself is the
|
|
28
|
-
* last resort: a TMPDIR long enough to break the path limit still leaves it.
|
|
29
|
-
*
|
|
30
|
-
* This is a LIST, not one answer, because every entry but the last comes from
|
|
31
|
-
* the environment. A sandbox, a read-only mount or a stale variable pointing at
|
|
32
|
-
* a directory this user cannot write makes the first choice fail, and one
|
|
33
|
-
* failed candidate must cost a session nothing: the next one is tried.
|
|
34
|
-
*/
|
|
35
|
-
export function socketDirCandidates() {
|
|
36
|
-
if (process.platform === 'win32') {
|
|
37
|
-
const out = [];
|
|
38
|
-
if (process.env.LOCALAPPDATA)
|
|
39
|
-
out.push(join(process.env.LOCALAPPDATA, 'chat-recall', 'run'));
|
|
40
|
-
if (process.env.TEMP)
|
|
41
|
-
out.push(join(process.env.TEMP, 'chat-recall-run'));
|
|
42
|
-
out.push(join(tmpdir(), 'chat-recall-run'));
|
|
43
|
-
return out;
|
|
44
|
-
}
|
|
45
|
-
const out = [];
|
|
46
|
-
const xdg = process.env.XDG_RUNTIME_DIR;
|
|
47
|
-
if (xdg && xdg.length > 0) {
|
|
48
|
-
const p = join(xdg, 'chat-recall');
|
|
49
|
-
if (fits(p))
|
|
50
|
-
out.push(p);
|
|
51
|
-
}
|
|
52
|
-
const uid = typeof process.getuid === 'function' ? process.getuid() : 0;
|
|
53
|
-
const named = join(tmpdir(), `chat-recall-${uid}`);
|
|
54
|
-
if (fits(named))
|
|
55
|
-
out.push(named);
|
|
56
|
-
const last = `/tmp/chat-recall-${uid}`;
|
|
57
|
-
if (!out.includes(last))
|
|
58
|
-
out.push(last);
|
|
59
|
-
return out;
|
|
60
|
-
}
|
|
61
|
-
/** How many missing levels this will build before giving up. A runtime
|
|
62
|
-
* directory is one or two below something that already exists; deeper than
|
|
63
|
-
* this means the path is wrong, not a tree worth creating. */
|
|
64
|
-
const MAX_MISSING_LEVELS = 8;
|
|
65
|
-
function isDir(path) {
|
|
66
|
-
try {
|
|
67
|
-
return statSync(path).isDirectory();
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Create `dir` if it is missing, and report whether it is a directory this
|
|
75
|
-
* process can write afterwards. Never throws: a candidate that cannot be used
|
|
76
|
-
* is a reason to try the next one, not a failure.
|
|
77
|
-
*
|
|
78
|
-
* Deliberately NOT `mkdirSync(dir, { recursive: true })`. Node 23's recursive
|
|
79
|
-
* mkdir walks BACK to the parent whenever a component reports ENOENT and
|
|
80
|
-
* forward again when one exists — and a path whose parent exists while the
|
|
81
|
-
* child still reports ENOENT makes it oscillate between the two forever, at
|
|
82
|
-
* 100% of a core. `/proc/anything` does exactly that on Linux: `mkdir
|
|
83
|
-
* /proc/x` returns ENOENT, not EACCES. This path comes from XDG_RUNTIME_DIR or
|
|
84
|
-
* TMPDIR — the values this program does not control — so it must never be
|
|
85
|
-
* handed to that loop. (Verified on Node v23.9.0: the recursive call never
|
|
86
|
-
* returns; a single-level `mkdirSync` on the same path throws ENOENT at once.)
|
|
87
|
-
*
|
|
88
|
-
* This walks up to the deepest ancestor that already exists, bounded, then
|
|
89
|
-
* creates forward from there ONE level at a time. It never revisits a level,
|
|
90
|
-
* so it cannot spin.
|
|
91
|
-
*/
|
|
92
|
-
export function usableDir(dir) {
|
|
93
|
-
if (!isDir(dir)) {
|
|
94
|
-
// Collect missing components up to the first existing ancestor.
|
|
95
|
-
const missing = [];
|
|
96
|
-
let cur = dir;
|
|
97
|
-
let base = false;
|
|
98
|
-
while (missing.length < MAX_MISSING_LEVELS) {
|
|
99
|
-
const parent = dirname(cur);
|
|
100
|
-
if (parent === cur)
|
|
101
|
-
break; // reached the root
|
|
102
|
-
missing.push(cur);
|
|
103
|
-
if (isDir(parent)) {
|
|
104
|
-
base = true;
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
cur = parent;
|
|
108
|
-
}
|
|
109
|
-
// No existing directory within the bound: the path is not somewhere this
|
|
110
|
-
// program should be building a tree.
|
|
111
|
-
if (!base)
|
|
112
|
-
return false;
|
|
113
|
-
for (let i = missing.length - 1; i >= 0; i--) {
|
|
114
|
-
try {
|
|
115
|
-
mkdirSync(missing[i]); // single level — throws ENOENT rather than spins
|
|
116
|
-
}
|
|
117
|
-
catch {
|
|
118
|
-
/* judged by what exists, not by what mkdir said (EEXIST is fine) */
|
|
119
|
-
}
|
|
120
|
-
if (!isDir(missing[i]))
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
try {
|
|
125
|
-
accessSync(dir, constants.W_OK);
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
catch {
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Make `dir` private to this user, and report whether that took effect.
|
|
134
|
-
*
|
|
135
|
-
* This is also the OWNERSHIP proof. `usableDir` says yes to anything writable,
|
|
136
|
-
* and on a shared /tmp that includes a directory another local user created
|
|
137
|
-
* first — our socket would then sit in their directory, theirs to swap out.
|
|
138
|
-
* chmod(2) fails with EPERM for anyone but the owner, so a chmod that
|
|
139
|
-
* succeeded means we own the directory; the mode check after it catches a
|
|
140
|
-
* filesystem that accepts chmod and ignores it (FAT, some FUSE mounts). No
|
|
141
|
-
* uid comparison, no window between a stat and a chmod. Windows has no POSIX
|
|
142
|
-
* modes; the per-user LOCALAPPDATA path is the protection there.
|
|
143
|
-
*/
|
|
144
|
-
export function privateDir(dir) {
|
|
145
|
-
if (process.platform === 'win32')
|
|
146
|
-
return true;
|
|
147
|
-
try {
|
|
148
|
-
chmodSync(dir, 0o700);
|
|
149
|
-
return (statSync(dir).mode & 0o077) === 0;
|
|
150
|
-
}
|
|
151
|
-
catch {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* The runtime directory for this user: the first candidate that can be used
|
|
157
|
-
* AND made private, created on the way. Throws only when NO candidate works,
|
|
158
|
-
* which the relay turns into serving the session in-process.
|
|
159
|
-
*/
|
|
160
|
-
export function ensureSocketDir() {
|
|
161
|
-
const candidates = socketDirCandidates();
|
|
162
|
-
for (const dir of candidates) {
|
|
163
|
-
if (!usableDir(dir))
|
|
164
|
-
continue;
|
|
165
|
-
if (!privateDir(dir))
|
|
166
|
-
continue;
|
|
167
|
-
return dir;
|
|
168
|
-
}
|
|
169
|
-
throw new Error(`no private runtime directory for the MCP daemon (tried ${candidates.join(', ')})`);
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Close the socket file itself to other users. connect(2) needs write
|
|
173
|
-
* permission on a Unix socket, so 0600 refuses every other uid even if the
|
|
174
|
-
* directory were traversable. Named pipes on Windows are not files.
|
|
175
|
-
*/
|
|
176
|
-
export function restrictSocket(path) {
|
|
177
|
-
if (process.platform === 'win32')
|
|
178
|
-
return;
|
|
179
|
-
try {
|
|
180
|
-
chmodSync(path, 0o600);
|
|
181
|
-
}
|
|
182
|
-
catch {
|
|
183
|
-
/* the directory is already 0700; this is the second lock, not the only one */
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* What makes two sessions share a daemon.
|
|
188
|
-
*
|
|
189
|
-
* The profile decides WHICH TOOLS a server registers — "lean" is 27, "full" is
|
|
190
|
-
* all 61 — and that list is fixed when the server object is built. Two sessions
|
|
191
|
-
* that asked for different lists therefore cannot share one daemon; keying on it
|
|
192
|
-
* gives them one each. The server URL is in the key for the same reason: a
|
|
193
|
-
* daemon holds a connection to one backend.
|
|
194
|
-
*
|
|
195
|
-
* The version is in it because without it an upgraded package would attach to a
|
|
196
|
-
* daemon still running the old code — the handshake would succeed and every
|
|
197
|
-
* answer would come from the version the user believes they replaced.
|
|
198
|
-
*/
|
|
199
|
-
export function socketSlug(version) {
|
|
200
|
-
const key = [
|
|
201
|
-
process.env.CHAT_RECALL_MCP_PROFILE ?? '',
|
|
202
|
-
process.env.CHAT_RECALL_SERVER ?? '',
|
|
203
|
-
version,
|
|
204
|
-
].join(' ');
|
|
205
|
-
return createHash('sha256').update(key).digest('hex').slice(0, 16);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* The address the daemon listens on, inside the runtime directory the caller
|
|
209
|
-
* resolved with `ensureSocketDir()`.
|
|
210
|
-
*
|
|
211
|
-
* Windows gets a named pipe rather than a file. Node's local-domain sockets ARE
|
|
212
|
-
* named pipes there, and a path outside the pipe namespace is not a valid one —
|
|
213
|
-
* so this is the native equivalent, not a workaround. It also needs no
|
|
214
|
-
* directory and leaves nothing behind when the daemon dies.
|
|
215
|
-
*/
|
|
216
|
-
export function socketPath(dir, version) {
|
|
217
|
-
const slug = socketSlug(version);
|
|
218
|
-
if (process.platform === 'win32')
|
|
219
|
-
return '\\\\.\\pipe\\chat-recall-' + slug;
|
|
220
|
-
return join(dir, `${slug}.sock`);
|
|
221
|
-
}
|
|
222
|
-
/** Where a daemon's diagnostics go. Never a session's stderr: it outlives it. */
|
|
223
|
-
export function logPath(dir, version) {
|
|
224
|
-
return join(dir, `${socketSlug(version)}.log`);
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* The flag the relay hands the daemon it starts. The relay already resolved a
|
|
228
|
-
* directory and is waiting on that exact path, so the daemon binds there rather
|
|
229
|
-
* than resolving again — a second resolution could land somewhere else if the
|
|
230
|
-
* environment shifted between the two processes.
|
|
231
|
-
*/
|
|
232
|
-
export const SOCKET_FLAG = '--socket';
|
|
233
|
-
/** The path given with `--socket`, if the process was started with one. */
|
|
234
|
-
export function socketPathFromArgv(argv) {
|
|
235
|
-
const i = argv.indexOf(SOCKET_FLAG);
|
|
236
|
-
if (i === -1)
|
|
237
|
-
return undefined;
|
|
238
|
-
const v = argv[i + 1];
|
|
239
|
-
return v && !v.startsWith('--') ? v : undefined;
|
|
240
|
-
}
|
|
241
|
-
/** True unless the user asked to keep everything inside their own session. */
|
|
242
|
-
export function daemonEnabled() {
|
|
243
|
-
const v = process.env.CHAT_RECALL_NO_DAEMON;
|
|
244
|
-
if (v === undefined)
|
|
245
|
-
return true;
|
|
246
|
-
const t = v.trim();
|
|
247
|
-
return t === '' || t === '0' || t === 'false';
|
|
248
|
-
}
|
|
249
|
-
//# sourceMappingURL=mcp-socket.js.map
|
package/dist/mcp-socket.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-socket.js","sourceRoot":"","sources":["../src/mcp-socket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAChF,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,6EAA6E;AAC7E,MAAM,QAAQ,GAAG,GAAG,CAAC;AAErB,uEAAuE;AACvE,MAAM,IAAI,GAAG,CAAC,GAAW,EAAW,EAAE,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,QAAQ,CAAC;AAE3E;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,mBAAmB;IACjC,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QACjC,MAAM,GAAG,GAAa,EAAE,CAAC;QACzB,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY;YAAE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC;QAC7F,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI;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CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACnB,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,OAAO,CAAC;AAChD,CAAC"}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* When a stdio MCP process must stop: its client closed the pipe, or the
|
|
3
|
-
* process that spawned it is gone.
|
|
4
|
-
*
|
|
5
|
-
* Imported by the relay, so it may use Node builtins only.
|
|
6
|
-
*
|
|
7
|
-
* A relay holds one connection on its daemon, and a daemon exits only when it
|
|
8
|
-
* has had none for CHAT_RECALL_DAEMON_IDLE_SECS. So one relay that outlives its
|
|
9
|
-
* session keeps a whole daemon — and every version-specific engine it loaded —
|
|
10
|
-
* resident for as long as that relay runs. The pipe closing is the normal
|
|
11
|
-
* signal. The parent check covers the case where the pipe never reports EOF,
|
|
12
|
-
* because another process still holds its other end after the AI tool died.
|
|
13
|
-
*/
|
|
14
|
-
import type { Duplex, Readable, Writable } from 'node:stream';
|
|
15
|
-
/** How often the parent is checked. The timer is unref'd and costs one syscall. */
|
|
16
|
-
export declare const PARENT_POLL_MS = 5000;
|
|
17
|
-
export interface ParentProbe {
|
|
18
|
-
/** The current parent pid. `process.ppid` is read live on every access. */
|
|
19
|
-
ppid(): number;
|
|
20
|
-
/** Whether a process with this pid exists. */
|
|
21
|
-
alive(pid: number): boolean;
|
|
22
|
-
}
|
|
23
|
-
/** `kill(pid, 0)` sends nothing and fails with ESRCH when the pid is gone. */
|
|
24
|
-
export declare function pidAlive(pid: number): boolean;
|
|
25
|
-
export declare const processProbe: ParentProbe;
|
|
26
|
-
/**
|
|
27
|
-
* Whether the process that spawned us has gone.
|
|
28
|
-
*
|
|
29
|
-
* On Linux and macOS an orphan is re-parented, so its ppid changes (to 1, or to
|
|
30
|
-
* a subreaper). Windows keeps the original ppid on an orphan, so the pid itself
|
|
31
|
-
* is probed as well.
|
|
32
|
-
*/
|
|
33
|
-
export declare function parentGone(originalPpid: number, probe: ParentProbe): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Call `onGone` once, when the parent that spawned this process is gone.
|
|
36
|
-
* Returns a function that stops the watch.
|
|
37
|
-
*
|
|
38
|
-
* A process that started with pid 1 as its parent was never spawned by a
|
|
39
|
-
* session (it was started by init or a service manager), so there is no
|
|
40
|
-
* parent to lose and no watch is set.
|
|
41
|
-
*/
|
|
42
|
-
export declare function watchParent(onGone: () => void, opts?: {
|
|
43
|
-
probe?: ParentProbe;
|
|
44
|
-
intervalMs?: number;
|
|
45
|
-
}): () => void;
|
|
46
|
-
/**
|
|
47
|
-
* Call `onEnd` once, when the client's side of stdio is finished.
|
|
48
|
-
*
|
|
49
|
-
* 'end' is the clean EOF. 'close' without 'end' and 'error' are the same fact
|
|
50
|
-
* reported by a pipe that broke: nothing more will ever arrive on it.
|
|
51
|
-
*/
|
|
52
|
-
export declare function onInputFinished(input: Readable, onEnd: () => void): void;
|
|
53
|
-
/**
|
|
54
|
-
* Move bytes both ways between the client's stdio and the daemon socket, and
|
|
55
|
-
* call `exit` exactly once when the session is over.
|
|
56
|
-
*
|
|
57
|
-
* The session is over when the daemon closes the socket, when the client's
|
|
58
|
-
* input finishes, or when the parent is gone. On input EOF the socket is
|
|
59
|
-
* half-closed first, so a reply the daemon is still writing reaches the
|
|
60
|
-
* client; the daemon's server closes its side on that EOF, which ends here as
|
|
61
|
-
* the socket's 'close'. A daemon that does not close within `drainMs` is not
|
|
62
|
-
* waited on any longer.
|
|
63
|
-
*/
|
|
64
|
-
export declare function bridge(input: Readable, output: Writable, sock: Duplex, exit: (code: number) => void, opts?: {
|
|
65
|
-
drainMs?: number;
|
|
66
|
-
probe?: ParentProbe;
|
|
67
|
-
parentPollMs?: number;
|
|
68
|
-
}): void;
|
|
69
|
-
/** The error a client gets for a call that was running when its daemon went away. */
|
|
70
|
-
export declare const DAEMON_RESTART_ERROR = "chat-recall restarted while this call ran. Call the tool again.";
|
|
71
|
-
/**
|
|
72
|
-
* Like `bridge`, but the session survives its daemon.
|
|
73
|
-
*
|
|
74
|
-
* A daemon goes away when it is upgraded or killed. `bridge` then ended the
|
|
75
|
-
* relay, and the AI tool showed the server as disconnected until a person
|
|
76
|
-
* reconnected it by hand. Here the relay connects again (`reconnect` starts a
|
|
77
|
-
* daemon when none answers), replays the client's `initialize` request and
|
|
78
|
-
* `notifications/initialized`, drops the new daemon's reply to that replay,
|
|
79
|
-
* and goes on.
|
|
80
|
-
*
|
|
81
|
-
* A request that had no reply when the daemon went away gets a JSON-RPC error
|
|
82
|
-
* that says to call again. Sending it again could run a write twice.
|
|
83
|
-
*
|
|
84
|
-
* The session is over when the client's input finishes, when the parent is
|
|
85
|
-
* gone, when output fails, or when no daemon can be reached again.
|
|
86
|
-
*/
|
|
87
|
-
export declare function resilientBridge(input: Readable, output: Writable, first: Duplex, reconnect: () => Promise<Duplex | null>, exit: (code: number) => void, opts?: {
|
|
88
|
-
drainMs?: number;
|
|
89
|
-
probe?: ParentProbe;
|
|
90
|
-
parentPollMs?: number;
|
|
91
|
-
}): void;
|
|
92
|
-
//# sourceMappingURL=relay-lifecycle.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"relay-lifecycle.d.ts","sourceRoot":"","sources":["../src/relay-lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE9D,mFAAmF;AACnF,eAAO,MAAM,cAAc,OAAQ,CAAC;AAEpC,MAAM,WAAW,WAAW;IAC1B,2EAA2E;IAC3E,IAAI,IAAI,MAAM,CAAC;IACf,8CAA8C;IAC9C,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC7B;AAED,8EAA8E;AAC9E,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAQ7C;AAED,eAAO,MAAM,YAAY,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290 +0,0 @@
|
|
|
1
|
-
/** How often the parent is checked. The timer is unref'd and costs one syscall. */
|
|
2
|
-
export const PARENT_POLL_MS = 5_000;
|
|
3
|
-
/** `kill(pid, 0)` sends nothing and fails with ESRCH when the pid is gone. */
|
|
4
|
-
export function pidAlive(pid) {
|
|
5
|
-
try {
|
|
6
|
-
process.kill(pid, 0);
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
catch (err) {
|
|
10
|
-
// EPERM: the pid exists and belongs to someone else. That is alive.
|
|
11
|
-
return err.code === 'EPERM';
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export const processProbe = {
|
|
15
|
-
ppid: () => process.ppid,
|
|
16
|
-
alive: pidAlive,
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Whether the process that spawned us has gone.
|
|
20
|
-
*
|
|
21
|
-
* On Linux and macOS an orphan is re-parented, so its ppid changes (to 1, or to
|
|
22
|
-
* a subreaper). Windows keeps the original ppid on an orphan, so the pid itself
|
|
23
|
-
* is probed as well.
|
|
24
|
-
*/
|
|
25
|
-
export function parentGone(originalPpid, probe) {
|
|
26
|
-
if (probe.ppid() !== originalPpid)
|
|
27
|
-
return true;
|
|
28
|
-
return !probe.alive(originalPpid);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Call `onGone` once, when the parent that spawned this process is gone.
|
|
32
|
-
* Returns a function that stops the watch.
|
|
33
|
-
*
|
|
34
|
-
* A process that started with pid 1 as its parent was never spawned by a
|
|
35
|
-
* session (it was started by init or a service manager), so there is no
|
|
36
|
-
* parent to lose and no watch is set.
|
|
37
|
-
*/
|
|
38
|
-
export function watchParent(onGone, opts = {}) {
|
|
39
|
-
const probe = opts.probe ?? processProbe;
|
|
40
|
-
const original = probe.ppid();
|
|
41
|
-
if (original <= 1)
|
|
42
|
-
return () => { };
|
|
43
|
-
const timer = setInterval(() => {
|
|
44
|
-
if (!parentGone(original, probe))
|
|
45
|
-
return;
|
|
46
|
-
clearInterval(timer);
|
|
47
|
-
onGone();
|
|
48
|
-
}, opts.intervalMs ?? PARENT_POLL_MS);
|
|
49
|
-
timer.unref?.();
|
|
50
|
-
return () => clearInterval(timer);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Call `onEnd` once, when the client's side of stdio is finished.
|
|
54
|
-
*
|
|
55
|
-
* 'end' is the clean EOF. 'close' without 'end' and 'error' are the same fact
|
|
56
|
-
* reported by a pipe that broke: nothing more will ever arrive on it.
|
|
57
|
-
*/
|
|
58
|
-
export function onInputFinished(input, onEnd) {
|
|
59
|
-
let fired = false;
|
|
60
|
-
const fire = () => {
|
|
61
|
-
if (fired)
|
|
62
|
-
return;
|
|
63
|
-
fired = true;
|
|
64
|
-
onEnd();
|
|
65
|
-
};
|
|
66
|
-
input.once('end', fire);
|
|
67
|
-
input.once('close', fire);
|
|
68
|
-
input.once('error', fire);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Move bytes both ways between the client's stdio and the daemon socket, and
|
|
72
|
-
* call `exit` exactly once when the session is over.
|
|
73
|
-
*
|
|
74
|
-
* The session is over when the daemon closes the socket, when the client's
|
|
75
|
-
* input finishes, or when the parent is gone. On input EOF the socket is
|
|
76
|
-
* half-closed first, so a reply the daemon is still writing reaches the
|
|
77
|
-
* client; the daemon's server closes its side on that EOF, which ends here as
|
|
78
|
-
* the socket's 'close'. A daemon that does not close within `drainMs` is not
|
|
79
|
-
* waited on any longer.
|
|
80
|
-
*/
|
|
81
|
-
export function bridge(input, output, sock, exit, opts = {}) {
|
|
82
|
-
let done = false;
|
|
83
|
-
let stopParentWatch = () => { };
|
|
84
|
-
const finish = () => {
|
|
85
|
-
if (done)
|
|
86
|
-
return;
|
|
87
|
-
done = true;
|
|
88
|
-
stopParentWatch();
|
|
89
|
-
try {
|
|
90
|
-
sock.destroy();
|
|
91
|
-
}
|
|
92
|
-
catch {
|
|
93
|
-
/* already gone */
|
|
94
|
-
}
|
|
95
|
-
exit(0);
|
|
96
|
-
};
|
|
97
|
-
input.pipe(sock);
|
|
98
|
-
sock.pipe(output);
|
|
99
|
-
sock.on('close', finish);
|
|
100
|
-
sock.on('error', finish);
|
|
101
|
-
// A client that is gone cannot read stdout. Writing to it is EPIPE, which
|
|
102
|
-
// must end the session instead of throwing out of the event loop.
|
|
103
|
-
output.on('error', finish);
|
|
104
|
-
onInputFinished(input, () => {
|
|
105
|
-
try {
|
|
106
|
-
sock.end();
|
|
107
|
-
}
|
|
108
|
-
catch {
|
|
109
|
-
finish();
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
const t = setTimeout(finish, opts.drainMs ?? 2_000);
|
|
113
|
-
t.unref?.();
|
|
114
|
-
});
|
|
115
|
-
stopParentWatch = watchParent(finish, { probe: opts.probe, intervalMs: opts.parentPollMs });
|
|
116
|
-
}
|
|
117
|
-
/** JSON-RPC id as a map key: ids may be numbers or strings, and 1 is not "1". */
|
|
118
|
-
const idKey = (id) => JSON.stringify(id);
|
|
119
|
-
function peek(line) {
|
|
120
|
-
try {
|
|
121
|
-
const m = JSON.parse(line);
|
|
122
|
-
return m && typeof m === 'object' ? m : null;
|
|
123
|
-
}
|
|
124
|
-
catch {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/** Calls up to newline-delimited lines; the remainder waits for the next chunk. */
|
|
129
|
-
function lineSplitter(onLine) {
|
|
130
|
-
const decoder = new TextDecoder('utf-8');
|
|
131
|
-
let rest = '';
|
|
132
|
-
return (chunk) => {
|
|
133
|
-
rest += typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
|
|
134
|
-
let nl = rest.indexOf('\n');
|
|
135
|
-
while (nl >= 0) {
|
|
136
|
-
const line = rest.slice(0, nl);
|
|
137
|
-
rest = rest.slice(nl + 1);
|
|
138
|
-
if (line.trim())
|
|
139
|
-
onLine(line);
|
|
140
|
-
nl = rest.indexOf('\n');
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
/** The error a client gets for a call that was running when its daemon went away. */
|
|
145
|
-
export const DAEMON_RESTART_ERROR = 'chat-recall restarted while this call ran. Call the tool again.';
|
|
146
|
-
/**
|
|
147
|
-
* Like `bridge`, but the session survives its daemon.
|
|
148
|
-
*
|
|
149
|
-
* A daemon goes away when it is upgraded or killed. `bridge` then ended the
|
|
150
|
-
* relay, and the AI tool showed the server as disconnected until a person
|
|
151
|
-
* reconnected it by hand. Here the relay connects again (`reconnect` starts a
|
|
152
|
-
* daemon when none answers), replays the client's `initialize` request and
|
|
153
|
-
* `notifications/initialized`, drops the new daemon's reply to that replay,
|
|
154
|
-
* and goes on.
|
|
155
|
-
*
|
|
156
|
-
* A request that had no reply when the daemon went away gets a JSON-RPC error
|
|
157
|
-
* that says to call again. Sending it again could run a write twice.
|
|
158
|
-
*
|
|
159
|
-
* The session is over when the client's input finishes, when the parent is
|
|
160
|
-
* gone, when output fails, or when no daemon can be reached again.
|
|
161
|
-
*/
|
|
162
|
-
export function resilientBridge(input, output, first, reconnect, exit, opts = {}) {
|
|
163
|
-
let done = false;
|
|
164
|
-
let inputEnded = false;
|
|
165
|
-
let sock = null;
|
|
166
|
-
/** Client lines held while no daemon is ready for them. */
|
|
167
|
-
let queue = [];
|
|
168
|
-
/** The replayed `initialize` request whose reply the client must not see. */
|
|
169
|
-
let replayId = null;
|
|
170
|
-
let initLine = null;
|
|
171
|
-
let initializedLine = null;
|
|
172
|
-
const pending = new Set();
|
|
173
|
-
let stopParentWatch = () => { };
|
|
174
|
-
const finish = (code = 0) => {
|
|
175
|
-
if (done)
|
|
176
|
-
return;
|
|
177
|
-
done = true;
|
|
178
|
-
stopParentWatch();
|
|
179
|
-
try {
|
|
180
|
-
sock?.destroy();
|
|
181
|
-
}
|
|
182
|
-
catch { /* already gone */ }
|
|
183
|
-
exit(code);
|
|
184
|
-
};
|
|
185
|
-
const toClient = (line) => {
|
|
186
|
-
try {
|
|
187
|
-
output.write(line + '\n');
|
|
188
|
-
}
|
|
189
|
-
catch {
|
|
190
|
-
finish();
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
const fromClient = (line) => {
|
|
194
|
-
const m = peek(line);
|
|
195
|
-
if (m?.method === 'initialize')
|
|
196
|
-
initLine = line;
|
|
197
|
-
else if (m?.method === 'notifications/initialized')
|
|
198
|
-
initializedLine = line;
|
|
199
|
-
if (m && m.id !== undefined && typeof m.method === 'string')
|
|
200
|
-
pending.add(idKey(m.id));
|
|
201
|
-
if (sock && replayId === null)
|
|
202
|
-
sock.write(line + '\n');
|
|
203
|
-
else
|
|
204
|
-
queue.push(line);
|
|
205
|
-
};
|
|
206
|
-
const attach = (s) => {
|
|
207
|
-
sock = s;
|
|
208
|
-
const onLine = lineSplitter((line) => {
|
|
209
|
-
if (s !== sock)
|
|
210
|
-
return;
|
|
211
|
-
const m = peek(line);
|
|
212
|
-
const isReply = m && m.id !== undefined && m.method === undefined;
|
|
213
|
-
if (isReply && replayId !== null && idKey(m.id) === replayId) {
|
|
214
|
-
replayId = null;
|
|
215
|
-
if (initializedLine)
|
|
216
|
-
s.write(initializedLine + '\n');
|
|
217
|
-
const held = queue;
|
|
218
|
-
queue = [];
|
|
219
|
-
for (const l of held)
|
|
220
|
-
s.write(l + '\n');
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
if (isReply)
|
|
224
|
-
pending.delete(idKey(m.id));
|
|
225
|
-
toClient(line);
|
|
226
|
-
});
|
|
227
|
-
s.on('data', onLine);
|
|
228
|
-
s.on('error', () => { });
|
|
229
|
-
s.on('close', () => { if (s === sock)
|
|
230
|
-
void lost(); });
|
|
231
|
-
};
|
|
232
|
-
const lost = async () => {
|
|
233
|
-
sock = null;
|
|
234
|
-
if (done)
|
|
235
|
-
return;
|
|
236
|
-
if (inputEnded) {
|
|
237
|
-
finish();
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
for (const key of pending) {
|
|
241
|
-
toClient(JSON.stringify({ jsonrpc: '2.0', id: JSON.parse(key), error: { code: -32000, message: DAEMON_RESTART_ERROR } }));
|
|
242
|
-
}
|
|
243
|
-
pending.clear();
|
|
244
|
-
const next = await reconnect().catch(() => null);
|
|
245
|
-
if (done) {
|
|
246
|
-
try {
|
|
247
|
-
next?.destroy();
|
|
248
|
-
}
|
|
249
|
-
catch { /* gone */ }
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
if (!next) {
|
|
253
|
-
finish(1);
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
if (initLine) {
|
|
257
|
-
replayId = idKey(peek(initLine)?.id);
|
|
258
|
-
attach(next);
|
|
259
|
-
next.write(initLine + '\n');
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
attach(next);
|
|
263
|
-
const held = queue;
|
|
264
|
-
queue = [];
|
|
265
|
-
for (const l of held)
|
|
266
|
-
next.write(l + '\n');
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
attach(first);
|
|
270
|
-
input.on('data', lineSplitter(fromClient));
|
|
271
|
-
output.on('error', () => finish());
|
|
272
|
-
onInputFinished(input, () => {
|
|
273
|
-
inputEnded = true;
|
|
274
|
-
if (!sock) {
|
|
275
|
-
finish();
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
try {
|
|
279
|
-
sock.end();
|
|
280
|
-
}
|
|
281
|
-
catch {
|
|
282
|
-
finish();
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
const t = setTimeout(() => finish(), opts.drainMs ?? 2_000);
|
|
286
|
-
t.unref?.();
|
|
287
|
-
});
|
|
288
|
-
stopParentWatch = watchParent(() => finish(), { probe: opts.probe, intervalMs: opts.parentPollMs });
|
|
289
|
-
}
|
|
290
|
-
//# sourceMappingURL=relay-lifecycle.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"relay-lifecycle.js","sourceRoot":"","sources":["../src/relay-lifecycle.ts"],"names":[],"mappings":"AAeA,mFAAmF;AACn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