@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
package/dist/runner/host.js
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { AcpConnectError } from '@noetic-tools/types';
|
|
9
9
|
import { chunksOf, fromBase64, RunnerFrameSchema, toBase64 } from './protocol.js';
|
|
10
|
+
/** How long a runner may take to answer a reattach. */
|
|
11
|
+
const ATTACH_TIMEOUT_MS = 1e4;
|
|
12
|
+
/** How long closing a stream waits for the runner to report it stopped. */
|
|
13
|
+
const KILL_WAIT_MS = 5e3;
|
|
10
14
|
/** Request headers that belong to one hop, which `fetch` sets for itself. */
|
|
11
15
|
const HOP_HEADERS = new Set([
|
|
12
16
|
'host',
|
|
@@ -22,7 +26,13 @@ const HOP_HEADERS = new Set([
|
|
|
22
26
|
* @public
|
|
23
27
|
*/
|
|
24
28
|
export function runnerMachine(openLink) {
|
|
25
|
-
return async ({ signal }) =>
|
|
29
|
+
return async ({ signal, whenHostExits = 'keep' }) => {
|
|
30
|
+
const peer = new RunnerPeer(await openLink(signal), signal);
|
|
31
|
+
// Agents this channel launches run through a keeper on the runner's
|
|
32
|
+
// machine, so they outlive the channel and a later one can reattach.
|
|
33
|
+
peer.keep(whenHostExits);
|
|
34
|
+
return peer.machine();
|
|
35
|
+
};
|
|
26
36
|
}
|
|
27
37
|
//#endregion
|
|
28
38
|
//#region Peer
|
|
@@ -50,6 +60,7 @@ class RunnerPeer {
|
|
|
50
60
|
reach: (bridge) => this.reach(bridge),
|
|
51
61
|
launch: (command) => this.start(command, 'launch'),
|
|
52
62
|
spawn: (command) => this.start(command, 'spawn'),
|
|
63
|
+
attach: (handle) => this.attach(handle),
|
|
53
64
|
close: () => this.link.close(),
|
|
54
65
|
};
|
|
55
66
|
}
|
|
@@ -68,21 +79,43 @@ class RunnerPeer {
|
|
|
68
79
|
url: reply.url,
|
|
69
80
|
};
|
|
70
81
|
}
|
|
71
|
-
|
|
82
|
+
keep(whenHostExits) {
|
|
83
|
+
this.link.send({
|
|
84
|
+
t: 'keep',
|
|
85
|
+
id: this.id(),
|
|
86
|
+
whenHostExits,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/** The stream of an agent an earlier channel launched and the runner kept. */
|
|
90
|
+
async attach(handle) {
|
|
72
91
|
const id = this.id();
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
const stream = this.openStream(id);
|
|
93
|
+
// A runner that does not keep agents never answers an attach.
|
|
94
|
+
const timeout = new Promise((_, reject) => {
|
|
95
|
+
const timer = setTimeout(() => reject(new AcpConnectError({
|
|
96
|
+
agentId: 'runner',
|
|
97
|
+
message: 'The remote runner did not answer a reattach; it may not keep agents.',
|
|
98
|
+
})), ATTACH_TIMEOUT_MS);
|
|
99
|
+
timer.unref?.();
|
|
76
100
|
});
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
101
|
+
const reply = await Promise.race([
|
|
102
|
+
this.ask({
|
|
103
|
+
t: 'attach',
|
|
104
|
+
handle,
|
|
105
|
+
}, id),
|
|
106
|
+
timeout,
|
|
107
|
+
]).catch((e) => {
|
|
108
|
+
this.streams.delete(id);
|
|
109
|
+
throw e;
|
|
85
110
|
});
|
|
111
|
+
if (reply.t !== 'started') {
|
|
112
|
+
throw unexpected(reply);
|
|
113
|
+
}
|
|
114
|
+
return this.transportFor(id, stream, handle);
|
|
115
|
+
}
|
|
116
|
+
async start(command, kind) {
|
|
117
|
+
const id = this.id();
|
|
118
|
+
const stream = this.openStream(id);
|
|
86
119
|
const reply = await this.ask({
|
|
87
120
|
t: 'start',
|
|
88
121
|
kind,
|
|
@@ -99,6 +132,30 @@ class RunnerPeer {
|
|
|
99
132
|
if (reply.t !== 'started') {
|
|
100
133
|
throw unexpected(reply);
|
|
101
134
|
}
|
|
135
|
+
return this.transportFor(id, stream, reply.reattach);
|
|
136
|
+
}
|
|
137
|
+
/** The readable for stream `id`, registered before the runner can answer. */
|
|
138
|
+
openStream(id) {
|
|
139
|
+
let exit = () => undefined;
|
|
140
|
+
const exited = new Promise((resolve) => {
|
|
141
|
+
exit = resolve;
|
|
142
|
+
});
|
|
143
|
+
const readable = new ReadableStream({
|
|
144
|
+
start: (controller) => {
|
|
145
|
+
this.streams.set(id, {
|
|
146
|
+
controller,
|
|
147
|
+
exited,
|
|
148
|
+
exit,
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
readable,
|
|
154
|
+
exited,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** Stream `id` as a transport; closing it stops what it carries. */
|
|
158
|
+
transportFor(id, stream, reattach) {
|
|
102
159
|
const writable = new WritableStream({
|
|
103
160
|
write: (chunk) => {
|
|
104
161
|
this.link.send({
|
|
@@ -110,8 +167,9 @@ class RunnerPeer {
|
|
|
110
167
|
});
|
|
111
168
|
let closed = false;
|
|
112
169
|
return {
|
|
113
|
-
readable,
|
|
170
|
+
readable: stream.readable,
|
|
114
171
|
writable,
|
|
172
|
+
reattach,
|
|
115
173
|
close: async () => {
|
|
116
174
|
if (closed) {
|
|
117
175
|
return;
|
|
@@ -122,8 +180,8 @@ class RunnerPeer {
|
|
|
122
180
|
id,
|
|
123
181
|
});
|
|
124
182
|
await Promise.race([
|
|
125
|
-
exited,
|
|
126
|
-
new Promise((resolve) => setTimeout(resolve,
|
|
183
|
+
stream.exited,
|
|
184
|
+
new Promise((resolve) => setTimeout(resolve, KILL_WAIT_MS)),
|
|
127
185
|
]);
|
|
128
186
|
},
|
|
129
187
|
};
|
package/dist/runner/options.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export declare function runnerOptions(values: {
|
|
|
16
16
|
spawn?: string[];
|
|
17
17
|
env?: string[];
|
|
18
18
|
cwd?: string;
|
|
19
|
+
/** How long a kept agent may run with no host attached; see {@link AcpRunnerOptions.detachedTimeoutMs}. */
|
|
20
|
+
detachedTimeoutMs?: number;
|
|
19
21
|
}): AcpRunnerOptions;
|
|
20
22
|
/** A runner does what whoever holds its token, or its stdio, asks of it. */
|
|
21
23
|
export declare function trustWarning(options: AcpRunnerOptions): string;
|
package/dist/runner/options.js
CHANGED
|
@@ -24,11 +24,13 @@ export function runnerOptions(values) {
|
|
|
24
24
|
if (grants.length === 0) {
|
|
25
25
|
return {
|
|
26
26
|
cwd: values.cwd,
|
|
27
|
+
detachedTimeoutMs: values.detachedTimeoutMs,
|
|
27
28
|
};
|
|
28
29
|
}
|
|
29
30
|
const extra = values.env ?? [];
|
|
30
31
|
return {
|
|
31
32
|
cwd: values.cwd,
|
|
33
|
+
detachedTimeoutMs: values.detachedTimeoutMs,
|
|
32
34
|
allow: grants.map((grant) => ({
|
|
33
35
|
...grant,
|
|
34
36
|
env: [
|
|
@@ -31,6 +31,17 @@ export declare const HostFrameSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
31
31
|
}, z.core.$strip>, z.ZodObject<{
|
|
32
32
|
t: z.ZodLiteral<"kill">;
|
|
33
33
|
id: z.ZodString;
|
|
34
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
35
|
+
t: z.ZodLiteral<"keep">;
|
|
36
|
+
id: z.ZodString;
|
|
37
|
+
whenHostExits: z.ZodEnum<{
|
|
38
|
+
stop: "stop";
|
|
39
|
+
keep: "keep";
|
|
40
|
+
}>;
|
|
41
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
42
|
+
t: z.ZodLiteral<"attach">;
|
|
43
|
+
id: z.ZodString;
|
|
44
|
+
handle: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
34
45
|
}, z.core.$strip>, z.ZodObject<{
|
|
35
46
|
t: z.ZodLiteral<"response">;
|
|
36
47
|
id: z.ZodString;
|
|
@@ -52,6 +63,7 @@ export declare const RunnerFrameSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
52
63
|
}, z.core.$strip>, z.ZodObject<{
|
|
53
64
|
t: z.ZodLiteral<"started">;
|
|
54
65
|
id: z.ZodString;
|
|
66
|
+
reattach: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
55
67
|
}, z.core.$strip>, z.ZodObject<{
|
|
56
68
|
t: z.ZodLiteral<"stdout">;
|
|
57
69
|
id: z.ZodString;
|
package/dist/runner/protocol.js
CHANGED
|
@@ -48,6 +48,28 @@ export const HostFrameSchema = z.discriminatedUnion('t', [
|
|
|
48
48
|
t: z.literal('kill'),
|
|
49
49
|
id: Id,
|
|
50
50
|
}),
|
|
51
|
+
/**
|
|
52
|
+
* Run the agents this channel launches through a keeper on the runner's
|
|
53
|
+
* machine, so they outlive the channel; `whenHostExits` says what happens
|
|
54
|
+
* to them when it drops. Sent once, first, by hosts that keep agents.
|
|
55
|
+
*/
|
|
56
|
+
z.object({
|
|
57
|
+
t: z.literal('keep'),
|
|
58
|
+
id: Id,
|
|
59
|
+
whenHostExits: z.enum([
|
|
60
|
+
'keep',
|
|
61
|
+
'stop',
|
|
62
|
+
]),
|
|
63
|
+
}),
|
|
64
|
+
/**
|
|
65
|
+
* Carry the stream of an agent an earlier channel launched, from the
|
|
66
|
+
* reattach handle its `started` frame gave; answered with `started`.
|
|
67
|
+
*/
|
|
68
|
+
z.object({
|
|
69
|
+
t: z.literal('attach'),
|
|
70
|
+
id: Id,
|
|
71
|
+
handle: z.record(z.string(), z.unknown()),
|
|
72
|
+
}),
|
|
51
73
|
z.object({
|
|
52
74
|
t: z.literal('response'),
|
|
53
75
|
id: Id,
|
|
@@ -74,6 +96,8 @@ export const RunnerFrameSchema = z.discriminatedUnion('t', [
|
|
|
74
96
|
z.object({
|
|
75
97
|
t: z.literal('started'),
|
|
76
98
|
id: Id,
|
|
99
|
+
/** For a kept agent: how a later channel reaches it again. */
|
|
100
|
+
reattach: z.record(z.string(), z.unknown()).optional(),
|
|
77
101
|
}),
|
|
78
102
|
z.object({
|
|
79
103
|
t: z.literal('stdout'),
|
package/dist/runner/serve.d.ts
CHANGED
|
@@ -19,6 +19,11 @@ export interface AcpRunnerOptions {
|
|
|
19
19
|
allow?: ReadonlyArray<AcpRunnerGrant>;
|
|
20
20
|
/** Working directory for a start that names none. */
|
|
21
21
|
cwd?: string;
|
|
22
|
+
/**
|
|
23
|
+
* How long an agent a host keeps may run with no host attached before its
|
|
24
|
+
* keeper stops it. Default `0`: until a host reattaches or it exits.
|
|
25
|
+
*/
|
|
26
|
+
detachedTimeoutMs?: number;
|
|
22
27
|
}
|
|
23
28
|
/** @public Options for {@link listenAcpRunner}. */
|
|
24
29
|
export interface AcpRunnerListenOptions extends AcpRunnerOptions {
|
package/dist/runner/serve.js
CHANGED
|
@@ -15,9 +15,10 @@ import * as https from 'node:https';
|
|
|
15
15
|
import { Readable, Writable } from 'node:stream';
|
|
16
16
|
import { frameworkCast } from '@noetic-tools/types';
|
|
17
17
|
import { WebSocketServer } from 'ws';
|
|
18
|
+
import { attachKeeper, isKeeperAddress, keeperHandleOf, startKeeper } from '../keeper/client.js';
|
|
18
19
|
import { serverEnv } from '../server-env.js';
|
|
19
20
|
import { checkStart } from './grants.js';
|
|
20
|
-
import { fromBase64, HostFrameSchema, isLoopbackHost, lineFrameLink, toBase64 } from './protocol.js';
|
|
21
|
+
import { chunksOf, fromBase64, HostFrameSchema, isLoopbackHost, lineFrameLink, toBase64, } from './protocol.js';
|
|
21
22
|
import { webSocketFrameLink } from './ws-link.js';
|
|
22
23
|
//#endregion
|
|
23
24
|
//#region Public API
|
|
@@ -33,6 +34,7 @@ export function serveAcpRunner(link, options = {}) {
|
|
|
33
34
|
ports: [],
|
|
34
35
|
responses: new Map(),
|
|
35
36
|
next: 0,
|
|
37
|
+
kept: new Map(),
|
|
36
38
|
};
|
|
37
39
|
return new Promise((resolve) => {
|
|
38
40
|
link.onFrame((raw) => {
|
|
@@ -112,11 +114,27 @@ const handlers = {
|
|
|
112
114
|
reach: openPort,
|
|
113
115
|
start: startProcess,
|
|
114
116
|
stdin: (session, frame) => {
|
|
117
|
+
const kept = session.kept.get(frame.id);
|
|
118
|
+
if (kept) {
|
|
119
|
+
void kept.writer.write(fromBase64(frame.data)).catch(() => undefined);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
115
122
|
session.children.get(frame.id)?.stdin?.write(fromBase64(frame.data));
|
|
116
123
|
},
|
|
117
|
-
kill: (session, frame) => {
|
|
124
|
+
kill: async (session, frame) => {
|
|
125
|
+
const kept = session.kept.get(frame.id);
|
|
126
|
+
if (kept) {
|
|
127
|
+
// The host closes its agent on purpose: stop it, keeper and all.
|
|
128
|
+
session.kept.delete(frame.id);
|
|
129
|
+
await kept.transport.close();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
118
132
|
session.children.get(frame.id)?.kill();
|
|
119
133
|
},
|
|
134
|
+
keep: (session, frame) => {
|
|
135
|
+
session.keep = frame.whenHostExits;
|
|
136
|
+
},
|
|
137
|
+
attach: attachKept,
|
|
120
138
|
response: (session, frame) => {
|
|
121
139
|
const res = session.responses.get(frame.id);
|
|
122
140
|
if (!res || res.headersSent) {
|
|
@@ -170,10 +188,14 @@ async function openPort(session, frame) {
|
|
|
170
188
|
if (address === null || typeof address === 'string') {
|
|
171
189
|
throw new Error('The bridge port did not bind.');
|
|
172
190
|
}
|
|
191
|
+
const local = `http://127.0.0.1:${address.port}${frame.path}`;
|
|
192
|
+
// A kept agent reaches the bridge on its keeper's port, which outlives this
|
|
193
|
+
// channel; the keeper forwards to this channel's port while it is open.
|
|
194
|
+
const keeper = await openKeeper(session);
|
|
173
195
|
session.link.send({
|
|
174
196
|
t: 'reached',
|
|
175
197
|
id: frame.id,
|
|
176
|
-
url:
|
|
198
|
+
url: keeper ? await keeper.reach(frame.path, local) : local,
|
|
177
199
|
});
|
|
178
200
|
}
|
|
179
201
|
async function relay(session, req, res) {
|
|
@@ -210,6 +232,20 @@ async function relay(session, req, res) {
|
|
|
210
232
|
}
|
|
211
233
|
async function startProcess(session, frame) {
|
|
212
234
|
const checked = checkStart(frame, session.options);
|
|
235
|
+
const keeper = frame.kind === 'launch' ? await openKeeper(session) : undefined;
|
|
236
|
+
if (keeper) {
|
|
237
|
+
const transport = await keeper.launch({
|
|
238
|
+
command: frame.command,
|
|
239
|
+
args: frame.args,
|
|
240
|
+
cwd: checked.cwd,
|
|
241
|
+
env: {
|
|
242
|
+
...process.env,
|
|
243
|
+
...checked.env,
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
carryKept(session, frame.id, transport);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
213
249
|
// An MCP server gets what the MCP SDK would give it here, the agent all of the runner's.
|
|
214
250
|
const env = frame.kind === 'spawn'
|
|
215
251
|
? serverEnv(checked.env)
|
|
@@ -253,6 +289,57 @@ async function startProcess(session, frame) {
|
|
|
253
289
|
id: frame.id,
|
|
254
290
|
});
|
|
255
291
|
}
|
|
292
|
+
/** Start this channel's keeper on first use, when the host keeps its agents. */
|
|
293
|
+
function openKeeper(session) {
|
|
294
|
+
const whenHostExits = session.keep;
|
|
295
|
+
if (!whenHostExits) {
|
|
296
|
+
return Promise.resolve(undefined);
|
|
297
|
+
}
|
|
298
|
+
session.keeper ??= startKeeper('runner', {
|
|
299
|
+
whenHostExits,
|
|
300
|
+
detachedTimeoutMs: session.options.detachedTimeoutMs ?? 0,
|
|
301
|
+
});
|
|
302
|
+
return session.keeper;
|
|
303
|
+
}
|
|
304
|
+
/** Send a kept agent's output as stream `id` until it ends. */
|
|
305
|
+
async function pumpKept(session, id, transport) {
|
|
306
|
+
for await (const chunk of chunksOf(transport.readable)) {
|
|
307
|
+
session.link.send({
|
|
308
|
+
t: 'stdout',
|
|
309
|
+
id,
|
|
310
|
+
data: toBase64(chunk),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
session.kept.delete(id);
|
|
314
|
+
session.link.send({
|
|
315
|
+
t: 'exit',
|
|
316
|
+
id,
|
|
317
|
+
code: null,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/** Carry a kept agent's stream over this channel as stream `id`. */
|
|
321
|
+
function carryKept(session, id, transport) {
|
|
322
|
+
session.kept.set(id, {
|
|
323
|
+
transport,
|
|
324
|
+
writer: transport.writable.getWriter(),
|
|
325
|
+
});
|
|
326
|
+
void pumpKept(session, id, transport).catch(() => undefined);
|
|
327
|
+
session.link.send({
|
|
328
|
+
t: 'started',
|
|
329
|
+
id,
|
|
330
|
+
reattach: transport.reattach,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** Take over an agent an earlier channel launched and kept. */
|
|
334
|
+
async function attachKept(session, frame) {
|
|
335
|
+
const handle = keeperHandleOf(frame.handle);
|
|
336
|
+
if (!handle || !isKeeperAddress(handle.address)) {
|
|
337
|
+
throw new Error('The handle names no keeper on this runner.');
|
|
338
|
+
}
|
|
339
|
+
const link = await attachKeeper('runner', handle, session.keep ?? 'keep');
|
|
340
|
+
session.keeper = Promise.resolve(link);
|
|
341
|
+
carryKept(session, frame.id, link.transport());
|
|
342
|
+
}
|
|
256
343
|
//#endregion
|
|
257
344
|
//#region Helpers
|
|
258
345
|
function release(session) {
|
|
@@ -268,6 +355,9 @@ function release(session) {
|
|
|
268
355
|
res.destroy();
|
|
269
356
|
}
|
|
270
357
|
session.responses.clear();
|
|
358
|
+
// A kept agent is let go, not stopped: its keeper decides (`whenHostExits`).
|
|
359
|
+
session.kept.clear();
|
|
360
|
+
void session.keeper?.then((link) => link?.detach());
|
|
271
361
|
}
|
|
272
362
|
function sameSecret(given, expected) {
|
|
273
363
|
const a = Buffer.from(given ?? '');
|
|
@@ -21,6 +21,10 @@ export declare function evaluateServePolicy(params: {
|
|
|
21
21
|
toolName: string;
|
|
22
22
|
kind?: AcpToolKind;
|
|
23
23
|
}): AcpServePermissionDecision;
|
|
24
|
+
/** A reply, and whether it is the broker's own because nobody answered in time. */
|
|
25
|
+
interface AskOutcome extends AcpServePermissionReply {
|
|
26
|
+
timedOut?: boolean;
|
|
27
|
+
}
|
|
24
28
|
/**
|
|
25
29
|
* Holds `ask` parks and routes each to its answerer. The layer registers the
|
|
26
30
|
* park *before* invoking `forward`, so an answer racing back in the same tick
|
|
@@ -31,9 +35,10 @@ export declare class ServePermissionBroker {
|
|
|
31
35
|
private readonly forward;
|
|
32
36
|
private readonly pending;
|
|
33
37
|
constructor(forward: (prompt: AcpServePermissionPrompt) => Promise<AcpServePermissionReply>);
|
|
34
|
-
|
|
38
|
+
/** Ask the client. With no `timeoutMs` the ask waits for an answer or a session cancel. */
|
|
39
|
+
ask(prompt: AcpServePermissionPrompt, timeoutMs?: number): Promise<AskOutcome>;
|
|
35
40
|
/** Resolve one park; later answers to the same request are dropped. */
|
|
36
|
-
answer(requestId: string, reply:
|
|
41
|
+
answer(requestId: string, reply: AskOutcome): void;
|
|
37
42
|
/** Unwind every park belonging to a session — the `session/cancel` path. */
|
|
38
43
|
cancelSession(sessionId: string): void;
|
|
39
44
|
}
|
|
@@ -53,3 +58,4 @@ export declare function createServePermissionLayer(params: {
|
|
|
53
58
|
broker: ServePermissionBroker;
|
|
54
59
|
presentation: ServeToolPresentation;
|
|
55
60
|
}): ContextLayer;
|
|
61
|
+
export {};
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { frameworkCast } from '@noetic-tools/types';
|
|
13
13
|
//#region Policy evaluation
|
|
14
|
-
|
|
14
|
+
/** The longest delay a timer takes: about 24.8 days. */
|
|
15
|
+
const MAX_HOOK_TIMEOUT_MS = 2 ** 31 - 1;
|
|
15
16
|
/**
|
|
16
17
|
* First decisive rule wins, checked `deny` → `ask` → `allow`; the policy
|
|
17
18
|
* `default` (or `allow`) applies when nothing matches.
|
|
@@ -47,6 +48,7 @@ export class ServePermissionBroker {
|
|
|
47
48
|
constructor(forward) {
|
|
48
49
|
this.forward = forward;
|
|
49
50
|
}
|
|
51
|
+
/** Ask the client. With no `timeoutMs` the ask waits for an answer or a session cancel. */
|
|
50
52
|
async ask(prompt, timeoutMs) {
|
|
51
53
|
let timer;
|
|
52
54
|
const parked = new Promise((resolve) => {
|
|
@@ -54,10 +56,14 @@ export class ServePermissionBroker {
|
|
|
54
56
|
sessionId: prompt.sessionId,
|
|
55
57
|
resolve,
|
|
56
58
|
});
|
|
59
|
+
if (timeoutMs === undefined) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
57
62
|
timer = setTimeout(() => {
|
|
58
63
|
this.answer(prompt.requestId, {
|
|
59
64
|
decision: 'deny',
|
|
60
65
|
reason: 'permission request timed out',
|
|
66
|
+
timedOut: true,
|
|
61
67
|
});
|
|
62
68
|
}, timeoutMs);
|
|
63
69
|
});
|
|
@@ -110,7 +116,7 @@ export class ServePermissionBroker {
|
|
|
110
116
|
*/
|
|
111
117
|
export function createServePermissionLayer(params) {
|
|
112
118
|
const { sessionId, policy, broker, presentation } = params;
|
|
113
|
-
const askTimeoutMs = policy
|
|
119
|
+
const { askTimeoutMs } = policy;
|
|
114
120
|
const beforeToolCall = async (hookParams) => {
|
|
115
121
|
const { toolName, toolArgs, callId } = hookParams;
|
|
116
122
|
const kind = presentation.kindOf(toolName);
|
|
@@ -143,6 +149,20 @@ export function createServePermissionLayer(params) {
|
|
|
143
149
|
kind,
|
|
144
150
|
args: toolArgs,
|
|
145
151
|
}, askTimeoutMs);
|
|
152
|
+
// Nobody at the editor answered. Rather than refuse, hand the call to the
|
|
153
|
+
// harness's approval seam, where a person can still answer it (spec 08 § Held
|
|
154
|
+
// tool calls). The editor's turn is waiting on it, so the turn stays live.
|
|
155
|
+
const approve = hookParams.ctx?.approve;
|
|
156
|
+
if (reply.timedOut && approve && callId !== undefined) {
|
|
157
|
+
return {
|
|
158
|
+
decision: await approve({
|
|
159
|
+
toolName,
|
|
160
|
+
toolArgs,
|
|
161
|
+
callId,
|
|
162
|
+
park: false,
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
146
166
|
if (reply.decision === 'allow') {
|
|
147
167
|
return {
|
|
148
168
|
decision: {
|
|
@@ -167,9 +187,10 @@ export function createServePermissionLayer(params) {
|
|
|
167
187
|
beforeToolCall,
|
|
168
188
|
},
|
|
169
189
|
timeouts: {
|
|
170
|
-
// The broker owns the
|
|
171
|
-
//
|
|
172
|
-
|
|
190
|
+
// The broker owns the editor's deadline (askTimeoutMs, none unless set). After it
|
|
191
|
+
// a call may be held for a person through the harness, for as long as that takes, so the
|
|
192
|
+
// lifecycle timeout is the longest a timer allows and only backstops a wedge.
|
|
193
|
+
beforeToolCall: MAX_HOOK_TIMEOUT_MS,
|
|
173
194
|
},
|
|
174
195
|
// A gate fails CLOSED: if the hook ever throws or blows the backstop
|
|
175
196
|
// timeout, the lifecycle denies the call rather than abstaining into the
|
package/dist/serve-types.d.ts
CHANGED
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* `@noetic-tools/chat-sdk` uses — so this package keeps depending only on
|
|
6
6
|
* `@noetic-tools/types`.
|
|
7
7
|
*/
|
|
8
|
-
import type
|
|
9
|
-
import type
|
|
8
|
+
import type * as acp from '@agentclientprotocol/sdk';
|
|
9
|
+
import type { AcpMcpServer, AcpToolKind, ExecuteArgument, ExecuteOptions, FsAdapter, Item, SessionScope, ShellAdapter, StreamEvent, StreamingItem, ToolAcpDeclaration } from '@noetic-tools/types';
|
|
10
10
|
/**
|
|
11
11
|
* The slice of `AgentHarnessContract` the ACP server drives. Any harness (or
|
|
12
12
|
* structural stand-in, in tests) satisfying this can be served.
|
|
13
13
|
* @public
|
|
14
14
|
*/
|
|
15
15
|
export interface AcpServeHarness {
|
|
16
|
-
execute(input:
|
|
16
|
+
execute(input: ExecuteArgument, options?: ExecuteOptions): Promise<void>;
|
|
17
17
|
getFullStream(scope?: SessionScope): AsyncIterable<StreamEvent>;
|
|
18
18
|
getItemStream(scope?: SessionScope): AsyncIterable<StreamingItem>;
|
|
19
19
|
seedSessionHistory(threadId: string, items: ReadonlyArray<Item>): void;
|
|
@@ -77,9 +77,10 @@ export interface AcpServePermissionPolicy {
|
|
|
77
77
|
default?: AcpServePermissionDecision;
|
|
78
78
|
rules?: ReadonlyArray<AcpServePermissionRule>;
|
|
79
79
|
/**
|
|
80
|
-
* How long an `ask` waits for the client's answer, in milliseconds.
|
|
81
|
-
*
|
|
82
|
-
*
|
|
80
|
+
* How long an `ask` waits for the client's answer, in milliseconds. With
|
|
81
|
+
* none, the ask waits until the client answers or the turn is cancelled. On
|
|
82
|
+
* expiry the call goes to the harness's approval seam, and is denied when
|
|
83
|
+
* there is none: waiting must not become approval.
|
|
83
84
|
*/
|
|
84
85
|
askTimeoutMs?: number;
|
|
85
86
|
}
|
package/dist/serve.d.ts
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* The adapter maps the protocol onto the harness's existing public surface —
|
|
10
10
|
* `execute`, the streams, `seedSessionHistory`, `abort` — and nothing else.
|
|
11
11
|
*/
|
|
12
|
-
import type * as acp from '@
|
|
13
|
-
import type { AgentSideConnection } from '@
|
|
12
|
+
import type * as acp from '@agentclientprotocol/sdk';
|
|
13
|
+
import type { AgentSideConnection } from '@agentclientprotocol/sdk';
|
|
14
14
|
import type { AcpServeHarnessSource, AcpServeOptions } from './serve-types.js';
|
|
15
15
|
/**
|
|
16
16
|
* The served agent: the protocol surface plus `dispose()`, which cancels
|
package/dist/serve.js
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* The adapter maps the protocol onto the harness's existing public surface —
|
|
10
10
|
* `execute`, the streams, `seedSessionHistory`, `abort` — and nothing else.
|
|
11
11
|
*/
|
|
12
|
+
import { PROTOCOL_VERSION, RequestError } from '@agentclientprotocol/sdk';
|
|
12
13
|
import { frameworkCast, NoeticConfigError } from '@noetic-tools/types';
|
|
13
|
-
import { PROTOCOL_VERSION, RequestError } from '@zed-industries/agent-client-protocol';
|
|
14
14
|
import { clientFsAdapter, clientShellAdapter } from './client-adapters.js';
|
|
15
15
|
import { contentBlockText } from './items.js';
|
|
16
16
|
import { isAbsolutePath } from './paths.js';
|
package/dist/server.js
CHANGED
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
19
|
import { Readable, Writable } from 'node:stream';
|
|
20
|
+
import { AgentSideConnection, ndJsonStream } from '@agentclientprotocol/sdk';
|
|
20
21
|
import { frameworkCast } from '@noetic-tools/types';
|
|
21
|
-
import { AgentSideConnection, ndJsonStream } from '@zed-industries/agent-client-protocol';
|
|
22
22
|
import { toAcpAgent } from './serve.js';
|
|
23
23
|
/**
|
|
24
24
|
* Serve a harness as an ACP agent over the current process's stdio. `await`
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stopping a stdio agent's whole process tree.
|
|
3
|
+
*
|
|
4
|
+
* Agents are often launched through a wrapper (`npx`, a Node launcher that
|
|
5
|
+
* `spawnSync`s a native binary), so killing the immediate child leaves the
|
|
6
|
+
* real agent running with our pipes inherited — and the host never exits. The
|
|
7
|
+
* transport therefore starts each agent as the leader of its own process group
|
|
8
|
+
* and stops the group: SIGTERM, a grace period, then SIGKILL. On Windows the
|
|
9
|
+
* tree is stopped with `taskkill /T /F`.
|
|
10
|
+
*/
|
|
11
|
+
import type { ChildProcess } from 'node:child_process';
|
|
12
|
+
/** @public How long a group has to exit after SIGTERM before it is killed. */
|
|
13
|
+
export declare const TERM_GRACE_MS = 2000;
|
|
14
|
+
/**
|
|
15
|
+
* Kill `child`'s process group when this process exits normally (`exit`), for
|
|
16
|
+
* a host whose agents must not outlive it. A host killed by a signal cannot
|
|
17
|
+
* run this; a child it started directly then sees its stdin close. Returns a
|
|
18
|
+
* function that forgets the child.
|
|
19
|
+
*/
|
|
20
|
+
export declare function stopWithHost(child: ChildProcess): () => void;
|
|
21
|
+
/** @public Options for {@link stopProcessTree}. */
|
|
22
|
+
export interface StopProcessTreeOptions {
|
|
23
|
+
/** Grace period between SIGTERM and SIGKILL. Default {@link TERM_GRACE_MS}. */
|
|
24
|
+
graceMs?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Stop the child's process group (POSIX) or process tree (Windows): end its
|
|
28
|
+
* stdin, SIGTERM, up to `graceMs` for it to exit, then SIGKILL. Then release
|
|
29
|
+
* our ends of the pipes, which a descendant that left the group (`setsid`)
|
|
30
|
+
* may still hold — such a descendant must manage its own lifetime. Bounded
|
|
31
|
+
* throughout, so an uncooperative child cannot hang the host.
|
|
32
|
+
*
|
|
33
|
+
* For a host that spawns its own agent process (a sandbox-wrapped command, a
|
|
34
|
+
* transport that rewrites lines): spawn it with `detached: true` on POSIX so it
|
|
35
|
+
* leads its own group, and call this from the transport's `close()`.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export declare function stopProcessTree(child: ChildProcess, opts?: StopProcessTreeOptions): Promise<void>;
|