@noetic-tools/acp 1.4.0 → 2.0.0-next.2
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/bin.js +19 -2
- package/dist/client-adapters.d.ts +2 -2
- package/dist/client.d.ts +2 -2
- package/dist/client.js +2 -1
- package/dist/connection.d.ts +6 -2
- package/dist/connection.js +213 -31
- package/dist/define.js +92 -4
- package/dist/items.d.ts +1 -1
- package/dist/keeper/client.d.ts +84 -0
- package/dist/keeper/client.js +436 -0
- package/dist/keeper/main.d.ts +6 -0
- package/dist/keeper/main.js +31 -0
- package/dist/keeper/process.d.ts +24 -0
- package/dist/keeper/process.js +422 -0
- package/dist/keeper/protocol.d.ts +72 -0
- package/dist/keeper/protocol.js +101 -0
- package/dist/keeper/relay.d.ts +88 -0
- package/dist/keeper/relay.js +420 -0
- package/dist/mcp/node.d.ts +6 -1
- package/dist/mcp/node.js +4 -4
- package/dist/permission-channel.d.ts +7 -2
- package/dist/permission-channel.js +39 -13
- package/dist/permissions.d.ts +6 -0
- package/dist/permissions.js +7 -0
- package/dist/runner/host.js +74 -16
- package/dist/runner/options.d.ts +2 -0
- package/dist/runner/options.js +2 -0
- package/dist/runner/protocol.d.ts +12 -0
- package/dist/runner/protocol.js +24 -0
- package/dist/runner/serve.d.ts +5 -0
- package/dist/runner/serve.js +93 -3
- package/dist/serve-permissions.d.ts +8 -2
- package/dist/serve-permissions.js +26 -5
- package/dist/serve-types.d.ts +7 -6
- package/dist/serve.d.ts +2 -2
- package/dist/serve.js +1 -1
- package/dist/server.js +1 -1
- package/dist/stdio-process.d.ts +38 -0
- package/dist/stdio-process.js +214 -0
- package/dist/stdio.d.ts +28 -6
- package/dist/stdio.js +145 -51
- package/dist/tool-bridge.d.ts +9 -0
- package/dist/tool-bridge.js +5 -1
- package/dist/transport-loopback.d.ts +2 -2
- package/dist/transport-loopback.js +1 -1
- package/dist/turn.d.ts +3 -2
- package/dist/turn.js +22 -2
- package/dist/update-filter.d.ts +22 -0
- package/dist/update-filter.js +119 -0
- package/package.json +3 -3
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host's side of a keeper (`process.ts`): start one for a new agent, or
|
|
3
|
+
* attach to the one an earlier host left running, and carry the agent's ACP
|
|
4
|
+
* stream and the bridge port over its socket.
|
|
5
|
+
*/
|
|
6
|
+
import * as net from 'node:net';
|
|
7
|
+
import type { AcpReattachHandle, AcpTransport, AcpWhenHostExits, SpawnCommand } from '@noetic-tools/types';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
/** @public Options for starting a keeper. */
|
|
10
|
+
export interface KeeperStartOptions {
|
|
11
|
+
whenHostExits: AcpWhenHostExits;
|
|
12
|
+
/** How long the agent may run with no host attached; 0 = no limit. */
|
|
13
|
+
detachedTimeoutMs: number;
|
|
14
|
+
}
|
|
15
|
+
declare const KeeperHandleSchema: z.ZodObject<{
|
|
16
|
+
kind: z.ZodLiteral<"noetic-acp-keeper">;
|
|
17
|
+
version: z.ZodLiteral<1>;
|
|
18
|
+
address: z.ZodString;
|
|
19
|
+
token: z.ZodString;
|
|
20
|
+
}, z.core.$strip>;
|
|
21
|
+
/** @public What a keeper's reattach handle holds. */
|
|
22
|
+
export type KeeperHandle = z.infer<typeof KeeperHandleSchema>;
|
|
23
|
+
/** The keeper part of a reattach handle, or undefined when it names no keeper. */
|
|
24
|
+
export declare function keeperHandleOf(handle: AcpReattachHandle): KeeperHandle | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* The keeper's entry script beside this module, or undefined when there is
|
|
27
|
+
* none on disk (a bundled single-file build), in which case agents are
|
|
28
|
+
* started directly and do not outlive their host.
|
|
29
|
+
*/
|
|
30
|
+
export declare function keeperScript(): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Whether `handle` names a keeper this user's keepers would listen on — so a
|
|
33
|
+
* runner never connects to an arbitrary socket a host names.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isKeeperAddress(address: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* One host's connection to a keeper. Control lines are answered here; every
|
|
38
|
+
* other line is the agent's ACP stream.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export declare class KeeperLink {
|
|
42
|
+
private readonly socket;
|
|
43
|
+
readonly handle: KeeperHandle;
|
|
44
|
+
private readonly agentId;
|
|
45
|
+
private next;
|
|
46
|
+
private readonly waiters;
|
|
47
|
+
private readonly welcomed;
|
|
48
|
+
private acp?;
|
|
49
|
+
/** ACP lines that arrived before the stream was taken. */
|
|
50
|
+
private readonly early;
|
|
51
|
+
private readonly encoder;
|
|
52
|
+
private closed;
|
|
53
|
+
private stopping?;
|
|
54
|
+
constructor(socket: net.Socket, handle: KeeperHandle, agentId: string);
|
|
55
|
+
/** Say hello; resolves with whether the keeper runs an agent. */
|
|
56
|
+
hello(whenHostExits: AcpWhenHostExits): Promise<'running' | 'none'>;
|
|
57
|
+
/** Serve the bridge at `target` on the keeper's port for `path`. */
|
|
58
|
+
reach(path: string, target: string): Promise<string>;
|
|
59
|
+
/** Start the agent and return its stream. */
|
|
60
|
+
launch(command: SpawnCommand): Promise<AcpTransport>;
|
|
61
|
+
/** The stream of the agent the keeper already runs. */
|
|
62
|
+
transport(): AcpTransport;
|
|
63
|
+
/** Stop the agent's tree and the keeper. Shared by every caller. */
|
|
64
|
+
stop(): Promise<void>;
|
|
65
|
+
/** Let go of the keeper without stopping it, as a host that goes away does. */
|
|
66
|
+
detach(): void;
|
|
67
|
+
private shutdown;
|
|
68
|
+
private receive;
|
|
69
|
+
private ask;
|
|
70
|
+
private write;
|
|
71
|
+
private id;
|
|
72
|
+
private lost;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Start a keeper and connect to it, or return undefined when there is no
|
|
76
|
+
* keeper script to run.
|
|
77
|
+
*/
|
|
78
|
+
export declare function startKeeper(agentId: string, opts: KeeperStartOptions): Promise<KeeperLink | undefined>;
|
|
79
|
+
/**
|
|
80
|
+
* Connect to the keeper `handle` names. Rejects with AcpConnectError when it
|
|
81
|
+
* is gone or no longer runs its agent.
|
|
82
|
+
*/
|
|
83
|
+
export declare function attachKeeper(agentId: string, handle: KeeperHandle, whenHostExits: AcpWhenHostExits): Promise<KeeperLink>;
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host's side of a keeper (`process.ts`): start one for a new agent, or
|
|
3
|
+
* attach to the one an earlier host left running, and carry the agent's ACP
|
|
4
|
+
* stream and the bridge port over its socket.
|
|
5
|
+
*/
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
8
|
+
import { chmodSync, existsSync, mkdirSync } from 'node:fs';
|
|
9
|
+
import * as net from 'node:net';
|
|
10
|
+
import { tmpdir } from 'node:os';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { AcpConnectError } from '@noetic-tools/types';
|
|
14
|
+
import { z } from 'zod';
|
|
15
|
+
import { isControlLine, KeeperLineSchema, LineSplitter } from './protocol.js';
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region Handle
|
|
18
|
+
const KeeperHandleSchema = z.object({
|
|
19
|
+
kind: z.literal('noetic-acp-keeper'),
|
|
20
|
+
version: z.literal(1),
|
|
21
|
+
address: z.string(),
|
|
22
|
+
token: z.string(),
|
|
23
|
+
});
|
|
24
|
+
/** The keeper part of a reattach handle, or undefined when it names no keeper. */
|
|
25
|
+
export function keeperHandleOf(handle) {
|
|
26
|
+
const parsed = KeeperHandleSchema.safeParse(handle);
|
|
27
|
+
return parsed.success ? parsed.data : undefined;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region Constants
|
|
31
|
+
/** How long a keeper may take to start listening. */
|
|
32
|
+
const START_TIMEOUT_MS = 1e4;
|
|
33
|
+
/** How long `stop` waits for the keeper to close its end. */
|
|
34
|
+
const STOP_WAIT_MS = 5e3;
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region Helpers
|
|
37
|
+
/**
|
|
38
|
+
* The keeper's entry script beside this module, or undefined when there is
|
|
39
|
+
* none on disk (a bundled single-file build), in which case agents are
|
|
40
|
+
* started directly and do not outlive their host.
|
|
41
|
+
*/
|
|
42
|
+
export function keeperScript() {
|
|
43
|
+
for (const name of [
|
|
44
|
+
'./main.js',
|
|
45
|
+
'./main.ts',
|
|
46
|
+
]) {
|
|
47
|
+
const url = new URL(name, import.meta.url);
|
|
48
|
+
if (url.protocol !== 'file:') {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const path = fileURLToPath(url);
|
|
52
|
+
if (existsSync(path)) {
|
|
53
|
+
return path;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
/** Where this user's keepers listen and log (POSIX). */
|
|
59
|
+
function keeperDirectory() {
|
|
60
|
+
return join(tmpdir(), `noetic-acp-${process.getuid?.() ?? 'user'}`);
|
|
61
|
+
}
|
|
62
|
+
/** The prefix of every keeper's named pipe (Windows). */
|
|
63
|
+
const PIPE_PREFIX = '\\\\.\\pipe\\noetic-acp-';
|
|
64
|
+
/**
|
|
65
|
+
* Whether `handle` names a keeper this user's keepers would listen on — so a
|
|
66
|
+
* runner never connects to an arbitrary socket a host names.
|
|
67
|
+
*/
|
|
68
|
+
export function isKeeperAddress(address) {
|
|
69
|
+
if (process.platform === 'win32') {
|
|
70
|
+
return address.startsWith(PIPE_PREFIX);
|
|
71
|
+
}
|
|
72
|
+
return (address.startsWith(`${keeperDirectory()}/`) &&
|
|
73
|
+
!address.includes('..') &&
|
|
74
|
+
address.endsWith('.sock'));
|
|
75
|
+
}
|
|
76
|
+
/** A fresh private address for a keeper, and its log file. */
|
|
77
|
+
function newAddress() {
|
|
78
|
+
const id = randomBytes(6).toString('hex');
|
|
79
|
+
if (process.platform === 'win32') {
|
|
80
|
+
return {
|
|
81
|
+
address: `${PIPE_PREFIX}${id}-${randomUUID()}`,
|
|
82
|
+
log: join(tmpdir(), `noetic-acp-${id}.log`),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Only this user may open the directory, so only this user reaches the socket.
|
|
86
|
+
const dir = keeperDirectory();
|
|
87
|
+
mkdirSync(dir, {
|
|
88
|
+
recursive: true,
|
|
89
|
+
mode: 0o700,
|
|
90
|
+
});
|
|
91
|
+
chmodSync(dir, 0o700);
|
|
92
|
+
return {
|
|
93
|
+
address: join(dir, `${id}.sock`),
|
|
94
|
+
log: join(dir, `${id}.log`),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async function readFirstLine(stream, ms) {
|
|
98
|
+
const splitter = new LineSplitter();
|
|
99
|
+
return await new Promise((resolve, reject) => {
|
|
100
|
+
const timer = setTimeout(() => reject(new Error('The keeper did not start in time.')), ms);
|
|
101
|
+
stream.on('data', (chunk) => {
|
|
102
|
+
const [first] = splitter.push(chunk);
|
|
103
|
+
if (first !== undefined) {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
resolve(first);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
stream.once('end', () => {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
reject(new Error('The keeper exited before it was ready.'));
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function connectSocket(address) {
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const socket = net.createConnection(address);
|
|
117
|
+
socket.once('connect', () => {
|
|
118
|
+
socket.off('error', reject);
|
|
119
|
+
resolve(socket);
|
|
120
|
+
});
|
|
121
|
+
socket.once('error', reject);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function definedEnv(env) {
|
|
125
|
+
const out = {};
|
|
126
|
+
for (const [name, value] of Object.entries(env)) {
|
|
127
|
+
if (value !== undefined) {
|
|
128
|
+
out[name] = value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region Keeper link
|
|
135
|
+
/**
|
|
136
|
+
* One host's connection to a keeper. Control lines are answered here; every
|
|
137
|
+
* other line is the agent's ACP stream.
|
|
138
|
+
* @public
|
|
139
|
+
*/
|
|
140
|
+
export class KeeperLink {
|
|
141
|
+
socket;
|
|
142
|
+
handle;
|
|
143
|
+
agentId;
|
|
144
|
+
next = 0;
|
|
145
|
+
waiters = new Map();
|
|
146
|
+
welcomed;
|
|
147
|
+
acp;
|
|
148
|
+
/** ACP lines that arrived before the stream was taken. */
|
|
149
|
+
early = [];
|
|
150
|
+
encoder = new TextEncoder();
|
|
151
|
+
closed = false;
|
|
152
|
+
stopping;
|
|
153
|
+
constructor(socket, handle, agentId) {
|
|
154
|
+
this.socket = socket;
|
|
155
|
+
this.handle = handle;
|
|
156
|
+
this.agentId = agentId;
|
|
157
|
+
let welcome = () => undefined;
|
|
158
|
+
let refuse = () => undefined;
|
|
159
|
+
this.welcomed = new Promise((resolve, reject) => {
|
|
160
|
+
welcome = resolve;
|
|
161
|
+
refuse = reject;
|
|
162
|
+
});
|
|
163
|
+
const splitter = new LineSplitter();
|
|
164
|
+
socket.on('data', (chunk) => {
|
|
165
|
+
for (const line of splitter.push(chunk)) {
|
|
166
|
+
this.receive(line, welcome);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
socket.on('error', () => undefined);
|
|
170
|
+
socket.on('close', () => {
|
|
171
|
+
this.closed = true;
|
|
172
|
+
refuse(this.lost('closed the connection before welcoming this host'));
|
|
173
|
+
for (const waiter of this.waiters.values()) {
|
|
174
|
+
waiter.reject(this.lost('closed the connection'));
|
|
175
|
+
}
|
|
176
|
+
this.waiters.clear();
|
|
177
|
+
try {
|
|
178
|
+
this.acp?.close();
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// Already closed.
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/** Say hello; resolves with whether the keeper runs an agent. */
|
|
186
|
+
async hello(whenHostExits) {
|
|
187
|
+
this.write({
|
|
188
|
+
keeper: 'hello',
|
|
189
|
+
token: this.handle.token,
|
|
190
|
+
whenHostExits,
|
|
191
|
+
});
|
|
192
|
+
return await this.welcomed;
|
|
193
|
+
}
|
|
194
|
+
/** Serve the bridge at `target` on the keeper's port for `path`. */
|
|
195
|
+
async reach(path, target) {
|
|
196
|
+
const reply = await this.ask({
|
|
197
|
+
keeper: 'reach',
|
|
198
|
+
id: this.id(),
|
|
199
|
+
path,
|
|
200
|
+
target,
|
|
201
|
+
});
|
|
202
|
+
if (reply.keeper !== 'reached') {
|
|
203
|
+
throw this.lost(`answered a reach with '${reply.keeper}'`);
|
|
204
|
+
}
|
|
205
|
+
return reply.url;
|
|
206
|
+
}
|
|
207
|
+
/** Start the agent and return its stream. */
|
|
208
|
+
async launch(command) {
|
|
209
|
+
const reply = await this.ask({
|
|
210
|
+
keeper: 'launch',
|
|
211
|
+
id: this.id(),
|
|
212
|
+
command: command.command,
|
|
213
|
+
args: [
|
|
214
|
+
...(command.args ?? []),
|
|
215
|
+
],
|
|
216
|
+
env: definedEnv(command.env ?? {}),
|
|
217
|
+
cwd: command.cwd,
|
|
218
|
+
});
|
|
219
|
+
if (reply.keeper !== 'launched') {
|
|
220
|
+
throw this.lost(`answered a launch with '${reply.keeper}'`);
|
|
221
|
+
}
|
|
222
|
+
return this.transport();
|
|
223
|
+
}
|
|
224
|
+
/** The stream of the agent the keeper already runs. */
|
|
225
|
+
transport() {
|
|
226
|
+
const readable = new ReadableStream({
|
|
227
|
+
start: (controller) => {
|
|
228
|
+
this.acp = controller;
|
|
229
|
+
for (const chunk of this.early.splice(0)) {
|
|
230
|
+
controller.enqueue(chunk);
|
|
231
|
+
}
|
|
232
|
+
if (this.closed) {
|
|
233
|
+
controller.close();
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
const writable = new WritableStream({
|
|
238
|
+
write: (chunk) => {
|
|
239
|
+
if (!this.closed) {
|
|
240
|
+
this.socket.write(chunk);
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
readable,
|
|
246
|
+
writable,
|
|
247
|
+
reattach: this.handle,
|
|
248
|
+
close: () => this.stop(),
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
/** Stop the agent's tree and the keeper. Shared by every caller. */
|
|
252
|
+
stop() {
|
|
253
|
+
this.stopping ??= this.shutdown();
|
|
254
|
+
return this.stopping;
|
|
255
|
+
}
|
|
256
|
+
/** Let go of the keeper without stopping it, as a host that goes away does. */
|
|
257
|
+
detach() {
|
|
258
|
+
this.socket.destroy();
|
|
259
|
+
}
|
|
260
|
+
async shutdown() {
|
|
261
|
+
if (this.closed) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
this.write({
|
|
265
|
+
keeper: 'stop',
|
|
266
|
+
});
|
|
267
|
+
await new Promise((resolve) => {
|
|
268
|
+
const timer = setTimeout(resolve, STOP_WAIT_MS);
|
|
269
|
+
timer.unref?.();
|
|
270
|
+
this.socket.once('close', () => {
|
|
271
|
+
clearTimeout(timer);
|
|
272
|
+
resolve();
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
this.socket.destroy();
|
|
276
|
+
}
|
|
277
|
+
receive(line, welcome) {
|
|
278
|
+
if (!isControlLine(line)) {
|
|
279
|
+
const bytes = this.encoder.encode(`${line}\n`);
|
|
280
|
+
if (this.acp) {
|
|
281
|
+
this.acp.enqueue(bytes);
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
this.early.push(bytes);
|
|
285
|
+
}
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
let parsedJson;
|
|
289
|
+
try {
|
|
290
|
+
parsedJson = JSON.parse(line);
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const parsed = KeeperLineSchema.safeParse(parsedJson);
|
|
296
|
+
if (!parsed.success) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const message = parsed.data;
|
|
300
|
+
if (message.keeper === 'welcome') {
|
|
301
|
+
welcome(message.agent);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (message.keeper === 'stderr') {
|
|
305
|
+
process.stderr.write(message.data);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const waiter = this.waiters.get(message.id);
|
|
309
|
+
if (!waiter) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
this.waiters.delete(message.id);
|
|
313
|
+
if (message.keeper === 'error') {
|
|
314
|
+
waiter.reject(this.lost(`refused: ${message.message}`));
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
waiter.resolve(message);
|
|
318
|
+
}
|
|
319
|
+
ask(line) {
|
|
320
|
+
if (this.closed) {
|
|
321
|
+
return Promise.reject(this.lost('closed the connection'));
|
|
322
|
+
}
|
|
323
|
+
return new Promise((resolve, reject) => {
|
|
324
|
+
this.waiters.set(line.id, {
|
|
325
|
+
resolve,
|
|
326
|
+
reject,
|
|
327
|
+
});
|
|
328
|
+
this.write(line);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
write(line) {
|
|
332
|
+
if (!this.closed) {
|
|
333
|
+
this.socket.write(`${JSON.stringify(line)}\n`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
id() {
|
|
337
|
+
this.next += 1;
|
|
338
|
+
return `k${this.next}`;
|
|
339
|
+
}
|
|
340
|
+
lost(what) {
|
|
341
|
+
return new AcpConnectError({
|
|
342
|
+
agentId: this.agentId,
|
|
343
|
+
message: `The keeper of ACP agent '${this.agentId}' ${what}.`,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
//#endregion
|
|
348
|
+
//#region Public API
|
|
349
|
+
/**
|
|
350
|
+
* Start a keeper and connect to it, or return undefined when there is no
|
|
351
|
+
* keeper script to run.
|
|
352
|
+
*/
|
|
353
|
+
export async function startKeeper(agentId, opts) {
|
|
354
|
+
const script = keeperScript();
|
|
355
|
+
if (!script) {
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
const { address, log } = newAddress();
|
|
359
|
+
const handle = {
|
|
360
|
+
kind: 'noetic-acp-keeper',
|
|
361
|
+
version: 1,
|
|
362
|
+
address,
|
|
363
|
+
token: `${randomUUID()}${randomUUID()}`,
|
|
364
|
+
};
|
|
365
|
+
const config = {
|
|
366
|
+
address,
|
|
367
|
+
token: handle.token,
|
|
368
|
+
log,
|
|
369
|
+
detachedTimeoutMs: opts.detachedTimeoutMs,
|
|
370
|
+
};
|
|
371
|
+
// Its own process group and session: the host's Ctrl-C or crash does not
|
|
372
|
+
// reach it, so it can keep the agent for the next host.
|
|
373
|
+
const child = spawn(process.execPath, [
|
|
374
|
+
script,
|
|
375
|
+
], {
|
|
376
|
+
detached: true,
|
|
377
|
+
stdio: [
|
|
378
|
+
'pipe',
|
|
379
|
+
'pipe',
|
|
380
|
+
'ignore',
|
|
381
|
+
],
|
|
382
|
+
windowsHide: true,
|
|
383
|
+
});
|
|
384
|
+
try {
|
|
385
|
+
await new Promise((resolve, reject) => {
|
|
386
|
+
child.once('spawn', resolve);
|
|
387
|
+
child.once('error', reject);
|
|
388
|
+
});
|
|
389
|
+
child.stdin?.end(`${JSON.stringify(config)}\n`);
|
|
390
|
+
const ready = child.stdout ? await readFirstLine(child.stdout, START_TIMEOUT_MS) : '';
|
|
391
|
+
if (!ready.includes('"ready"')) {
|
|
392
|
+
throw new Error('The keeper did not say it was ready.');
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
catch (cause) {
|
|
396
|
+
child.kill();
|
|
397
|
+
throw new AcpConnectError({
|
|
398
|
+
agentId,
|
|
399
|
+
message: `The keeper for ACP agent '${agentId}' failed to start.`,
|
|
400
|
+
cause,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
child.stdout?.destroy();
|
|
404
|
+
child.unref();
|
|
405
|
+
const link = new KeeperLink(await connectSocket(address), handle, agentId);
|
|
406
|
+
await link.hello(opts.whenHostExits);
|
|
407
|
+
return link;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Connect to the keeper `handle` names. Rejects with AcpConnectError when it
|
|
411
|
+
* is gone or no longer runs its agent.
|
|
412
|
+
*/
|
|
413
|
+
export async function attachKeeper(agentId, handle, whenHostExits) {
|
|
414
|
+
let socket;
|
|
415
|
+
try {
|
|
416
|
+
socket = await connectSocket(handle.address);
|
|
417
|
+
}
|
|
418
|
+
catch (cause) {
|
|
419
|
+
throw new AcpConnectError({
|
|
420
|
+
agentId,
|
|
421
|
+
message: `ACP agent '${agentId}' is no longer running: its keeper cannot be reached.`,
|
|
422
|
+
cause,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
const link = new KeeperLink(socket, handle, agentId);
|
|
426
|
+
const agent = await link.hello(whenHostExits);
|
|
427
|
+
if (agent !== 'running') {
|
|
428
|
+
await link.stop();
|
|
429
|
+
throw new AcpConnectError({
|
|
430
|
+
agentId,
|
|
431
|
+
message: `ACP agent '${agentId}' is no longer running.`,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return link;
|
|
435
|
+
}
|
|
436
|
+
//#endregion
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keeper's entry point: read its config from stdin, listen, say `ready`
|
|
3
|
+
* on stdout, then let go of both — the host that started it may go away
|
|
4
|
+
* while the keeper runs on.
|
|
5
|
+
*/
|
|
6
|
+
import { stdin, stdout } from 'node:process';
|
|
7
|
+
import { runKeeper } from './process.js';
|
|
8
|
+
import { KeeperConfigSchema, LineSplitter } from './protocol.js';
|
|
9
|
+
async function readConfig() {
|
|
10
|
+
const splitter = new LineSplitter();
|
|
11
|
+
for await (const chunk of stdin) {
|
|
12
|
+
const [first] = splitter.push(chunk);
|
|
13
|
+
if (first !== undefined) {
|
|
14
|
+
return JSON.parse(first);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
throw new Error('The keeper got no config.');
|
|
18
|
+
}
|
|
19
|
+
async function main() {
|
|
20
|
+
const config = KeeperConfigSchema.parse(await readConfig());
|
|
21
|
+
const { stopped } = await runKeeper(config);
|
|
22
|
+
stdout.write('{"ready":true}\n');
|
|
23
|
+
stdin.destroy();
|
|
24
|
+
stdout.end();
|
|
25
|
+
await stopped;
|
|
26
|
+
process.exit(0);
|
|
27
|
+
}
|
|
28
|
+
main().catch((error) => {
|
|
29
|
+
process.stderr.write(`noetic acp keeper: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keeper: a small detached process that runs one local agent for its
|
|
3
|
+
* host and keeps it running when the host goes away.
|
|
4
|
+
*
|
|
5
|
+
* It owns the agent's stdio and relays its ACP stream (`relay.ts`) to whichever
|
|
6
|
+
* host is attached over a private local socket — a Unix socket in a directory
|
|
7
|
+
* only its user can open, or a named pipe on Windows — that every connection
|
|
8
|
+
* must open with the keeper's token. It also owns the loopback port the agent
|
|
9
|
+
* reaches the host's tool bridge on, so the agent's MCP settings stay valid
|
|
10
|
+
* when a later host serves the bridge.
|
|
11
|
+
*
|
|
12
|
+
* When the attached host's connection drops without a `stop`, the keeper
|
|
13
|
+
* either stops the agent (`whenHostExits: 'stop'`) or keeps it working and
|
|
14
|
+
* holds what it sends until a host attaches again, for at most
|
|
15
|
+
* `detachedTimeoutMs`. It exits when the agent does.
|
|
16
|
+
*/
|
|
17
|
+
import type { KeeperConfig } from './protocol.js';
|
|
18
|
+
/**
|
|
19
|
+
* Run a keeper until its agent exits or it is stopped. Resolves once it
|
|
20
|
+
* listens on `config.address`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function runKeeper(config: KeeperConfig): Promise<{
|
|
23
|
+
stopped: Promise<void>;
|
|
24
|
+
}>;
|