@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,214 @@
|
|
|
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 { spawn } from 'node:child_process';
|
|
12
|
+
//#region Constants
|
|
13
|
+
/** @public How long a group has to exit after SIGTERM before it is killed. */
|
|
14
|
+
export const TERM_GRACE_MS = 2e3;
|
|
15
|
+
/** How long to wait for the group to vanish after SIGKILL, and for taskkill. */
|
|
16
|
+
const KILL_WAIT_MS = 1e3;
|
|
17
|
+
const POLL_MS = 20;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region Helpers
|
|
20
|
+
function wait(ms) {
|
|
21
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
22
|
+
}
|
|
23
|
+
function exited(child) {
|
|
24
|
+
return child.exitCode !== null || child.signalCode !== null;
|
|
25
|
+
}
|
|
26
|
+
/** Wait (bounded) for the child itself to exit and be reaped. */
|
|
27
|
+
function waitForExit(child, ms) {
|
|
28
|
+
if (exited(child)) {
|
|
29
|
+
return Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
const done = () => {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
child.removeListener('exit', done);
|
|
35
|
+
resolve();
|
|
36
|
+
};
|
|
37
|
+
const timer = setTimeout(done, ms);
|
|
38
|
+
child.once('exit', done);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function signalGroup(pid, signal) {
|
|
42
|
+
try {
|
|
43
|
+
process.kill(-pid, signal);
|
|
44
|
+
return 'sent';
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (!(error instanceof Error) || !('code' in error)) {
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
if (error.code === 'ESRCH') {
|
|
51
|
+
return 'gone';
|
|
52
|
+
}
|
|
53
|
+
if (error.code === 'EPERM') {
|
|
54
|
+
return 'zombie';
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Poll until the group is gone or `ms` passes. True when it is gone. */
|
|
60
|
+
async function waitForGroup(pid, ms) {
|
|
61
|
+
const deadline = Date.now() + ms;
|
|
62
|
+
while (signalGroup(pid, 0) !== 'gone') {
|
|
63
|
+
if (Date.now() >= deadline) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
await wait(POLL_MS);
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Signal the group, reaping an exited leader first when the group refuses
|
|
72
|
+
* signals. False when there is nothing left to signal.
|
|
73
|
+
*/
|
|
74
|
+
async function signalLiveGroup(child, pid, signal) {
|
|
75
|
+
const first = signalGroup(pid, signal);
|
|
76
|
+
if (first !== 'zombie') {
|
|
77
|
+
return first === 'sent' || signalChild(child, signal);
|
|
78
|
+
}
|
|
79
|
+
await waitForExit(child, KILL_WAIT_MS);
|
|
80
|
+
// Still refused once the leader is reaped: no member we may signal remains.
|
|
81
|
+
return signalGroup(pid, signal) === 'sent';
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A child that was not started as a group leader has no group to signal; stop
|
|
85
|
+
* the child itself rather than leave it running.
|
|
86
|
+
*/
|
|
87
|
+
function signalChild(child, signal) {
|
|
88
|
+
if (exited(child)) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
return child.kill(signal);
|
|
92
|
+
}
|
|
93
|
+
function killWindowsTree(pid) {
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
|
+
const killer = spawn('taskkill', [
|
|
96
|
+
'/pid',
|
|
97
|
+
String(pid),
|
|
98
|
+
'/T',
|
|
99
|
+
'/F',
|
|
100
|
+
], {
|
|
101
|
+
windowsHide: true,
|
|
102
|
+
stdio: 'ignore',
|
|
103
|
+
});
|
|
104
|
+
const timer = setTimeout(() => {
|
|
105
|
+
killer.kill();
|
|
106
|
+
reject(new Error('Timed out stopping the ACP process tree.'));
|
|
107
|
+
}, KILL_WAIT_MS);
|
|
108
|
+
killer.once('error', (error) => {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
reject(error);
|
|
111
|
+
});
|
|
112
|
+
killer.once('exit', (code) => {
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
// 128: no such process — the tree had already exited.
|
|
115
|
+
if (code !== 0 && code !== 128) {
|
|
116
|
+
reject(new Error(`taskkill failed with exit code ${code}.`));
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
resolve();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region Host exit
|
|
125
|
+
/** Children to kill when this process exits, for hosts that asked to stop them. */
|
|
126
|
+
const stopOnExit = new Set();
|
|
127
|
+
let exitHookInstalled = false;
|
|
128
|
+
function killOnExit() {
|
|
129
|
+
for (const child of stopOnExit) {
|
|
130
|
+
const pid = child.pid;
|
|
131
|
+
if (!pid || exited(child)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
// Synchronous on purpose: nothing asynchronous runs during 'exit'.
|
|
136
|
+
process.kill(process.platform === 'win32' ? pid : -pid, 'SIGKILL');
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// Already gone.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Kill `child`'s process group when this process exits normally (`exit`), for
|
|
145
|
+
* a host whose agents must not outlive it. A host killed by a signal cannot
|
|
146
|
+
* run this; a child it started directly then sees its stdin close. Returns a
|
|
147
|
+
* function that forgets the child.
|
|
148
|
+
*/
|
|
149
|
+
export function stopWithHost(child) {
|
|
150
|
+
if (!exitHookInstalled) {
|
|
151
|
+
exitHookInstalled = true;
|
|
152
|
+
process.on('exit', killOnExit);
|
|
153
|
+
}
|
|
154
|
+
stopOnExit.add(child);
|
|
155
|
+
child.once('exit', () => stopOnExit.delete(child));
|
|
156
|
+
return () => {
|
|
157
|
+
stopOnExit.delete(child);
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Stop the child's process group (POSIX) or process tree (Windows): end its
|
|
162
|
+
* stdin, SIGTERM, up to `graceMs` for it to exit, then SIGKILL. Then release
|
|
163
|
+
* our ends of the pipes, which a descendant that left the group (`setsid`)
|
|
164
|
+
* may still hold — such a descendant must manage its own lifetime. Bounded
|
|
165
|
+
* throughout, so an uncooperative child cannot hang the host.
|
|
166
|
+
*
|
|
167
|
+
* For a host that spawns its own agent process (a sandbox-wrapped command, a
|
|
168
|
+
* transport that rewrites lines): spawn it with `detached: true` on POSIX so it
|
|
169
|
+
* leads its own group, and call this from the transport's `close()`.
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
export async function stopProcessTree(child, opts = {}) {
|
|
173
|
+
try {
|
|
174
|
+
await signalTree(child, opts.graceMs ?? TERM_GRACE_MS);
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
child.stdin?.destroy();
|
|
178
|
+
child.stdout?.destroy();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async function signalTree(child, graceMs) {
|
|
182
|
+
const pid = child.pid;
|
|
183
|
+
// Reap a leader whose stdout already ended before signalling its group:
|
|
184
|
+
// macOS can refuse signals to a group whose last member is a zombie.
|
|
185
|
+
if (child.stdout?.readableEnded) {
|
|
186
|
+
await waitForExit(child, POLL_MS * 10);
|
|
187
|
+
}
|
|
188
|
+
child.stdin?.end();
|
|
189
|
+
if (!pid || pid <= 0) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (process.platform === 'win32') {
|
|
193
|
+
// Even when the child itself exited, its descendants may not have:
|
|
194
|
+
// taskkill answers 128 when there is nothing left.
|
|
195
|
+
await killWindowsTree(pid);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!(await signalLiveGroup(child, pid, 'SIGTERM'))) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const groupGone = await waitForGroup(pid, graceMs);
|
|
202
|
+
// A child that is not a group leader has no group to watch: it gets the
|
|
203
|
+
// kill wait as its grace period.
|
|
204
|
+
await waitForExit(child, groupGone ? KILL_WAIT_MS : 0);
|
|
205
|
+
if (groupGone && exited(child)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
await signalLiveGroup(child, pid, 'SIGKILL');
|
|
209
|
+
await Promise.all([
|
|
210
|
+
waitForExit(child, KILL_WAIT_MS),
|
|
211
|
+
waitForGroup(pid, KILL_WAIT_MS),
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
package/dist/stdio.d.ts
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* keeping the dependency off the main graph.
|
|
9
9
|
*/
|
|
10
10
|
import type { AcpMachineFactory, AcpTransportFactory } from '@noetic-tools/types';
|
|
11
|
+
export type { StopProcessTreeOptions } from './stdio-process.js';
|
|
12
|
+
export { stopProcessTree, TERM_GRACE_MS } from './stdio-process.js';
|
|
11
13
|
/** @public How to launch an ACP agent as a child process. */
|
|
12
14
|
export interface StdioAgentSpec {
|
|
13
15
|
/** Identifier used in error messages. */
|
|
@@ -21,15 +23,35 @@ export interface StdioAgentSpec {
|
|
|
21
23
|
/**
|
|
22
24
|
* Build a transport factory that spawns `spec.command` and speaks ACP over its
|
|
23
25
|
* stdin/stdout. The child's stderr is inherited so agent diagnostics reach the
|
|
24
|
-
* host's logs instead of vanishing.
|
|
26
|
+
* host's logs instead of vanishing. Closing the transport (or aborting its
|
|
27
|
+
* signal) closes the pipes and stops the agent's whole process group —
|
|
28
|
+
* SIGTERM, a grace period, then SIGKILL; on Windows, its process tree. Close
|
|
29
|
+
* is idempotent: every call shares one shutdown.
|
|
25
30
|
* @public
|
|
26
31
|
*/
|
|
27
32
|
export declare function stdioAcpTransport(spec: StdioAgentSpec): AcpTransportFactory;
|
|
33
|
+
/** @public Options for {@link localMachine}. */
|
|
34
|
+
export interface LocalMachineOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Run the agent through a keeper, so it can outlive this host and a later
|
|
37
|
+
* host can reattach to it (`AcpTransport.reattach`). Default `true`. Without
|
|
38
|
+
* one — or where no keeper script is on disk, as in a single-file bundle —
|
|
39
|
+
* the agent is a child process that ends with its host.
|
|
40
|
+
*/
|
|
41
|
+
keeper?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* How long a kept agent may run with no host attached before its keeper
|
|
44
|
+
* stops it. Default `0`: no limit — it runs until a host reattaches and
|
|
45
|
+
* closes it, or it exits.
|
|
46
|
+
*/
|
|
47
|
+
detachedTimeoutMs?: number;
|
|
48
|
+
}
|
|
28
49
|
/**
|
|
29
|
-
* This machine: the
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
50
|
+
* This machine: the agent runs through a keeper (see
|
|
51
|
+
* {@link LocalMachineOptions.keeper}), which also serves the tool bridge to it
|
|
52
|
+
* on a port that outlives the host; an MCP server beside it is a child
|
|
53
|
+
* process. The agent gets this process's environment; an MCP server, as the
|
|
54
|
+
* MCP SDK starts one, only `serverEnv`'s short list.
|
|
33
55
|
* @public
|
|
34
56
|
*/
|
|
35
|
-
export declare function localMachine(agentId?: string): AcpMachineFactory;
|
|
57
|
+
export declare function localMachine(agentId?: string, options?: LocalMachineOptions): AcpMachineFactory;
|
package/dist/stdio.js
CHANGED
|
@@ -10,11 +10,17 @@
|
|
|
10
10
|
import { spawn } from 'node:child_process';
|
|
11
11
|
import { Readable, Writable } from 'node:stream';
|
|
12
12
|
import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
|
|
13
|
+
import { attachKeeper, keeperHandleOf, startKeeper } from './keeper/client.js';
|
|
13
14
|
import { serverEnv } from './server-env.js';
|
|
15
|
+
import { stopProcessTree, stopWithHost } from './stdio-process.js';
|
|
16
|
+
export { stopProcessTree, TERM_GRACE_MS } from './stdio-process.js';
|
|
14
17
|
/**
|
|
15
18
|
* Build a transport factory that spawns `spec.command` and speaks ACP over its
|
|
16
19
|
* stdin/stdout. The child's stderr is inherited so agent diagnostics reach the
|
|
17
|
-
* host's logs instead of vanishing.
|
|
20
|
+
* host's logs instead of vanishing. Closing the transport (or aborting its
|
|
21
|
+
* signal) closes the pipes and stops the agent's whole process group —
|
|
22
|
+
* SIGTERM, a grace period, then SIGKILL; on Windows, its process tree. Close
|
|
23
|
+
* is idempotent: every call shares one shutdown.
|
|
18
24
|
* @public
|
|
19
25
|
*/
|
|
20
26
|
export function stdioAcpTransport(spec) {
|
|
@@ -27,44 +33,121 @@ export function stdioAcpTransport(spec) {
|
|
|
27
33
|
...opts.env,
|
|
28
34
|
},
|
|
29
35
|
cwd: opts.cwd,
|
|
30
|
-
},
|
|
36
|
+
}, {
|
|
37
|
+
signal: opts.signal,
|
|
38
|
+
whenHostExits: opts.whenHostExits,
|
|
39
|
+
});
|
|
31
40
|
}
|
|
32
41
|
/**
|
|
33
|
-
* This machine: the
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
42
|
+
* This machine: the agent runs through a keeper (see
|
|
43
|
+
* {@link LocalMachineOptions.keeper}), which also serves the tool bridge to it
|
|
44
|
+
* on a port that outlives the host; an MCP server beside it is a child
|
|
45
|
+
* process. The agent gets this process's environment; an MCP server, as the
|
|
46
|
+
* MCP SDK starts one, only `serverEnv`'s short list.
|
|
37
47
|
* @public
|
|
38
48
|
*/
|
|
39
|
-
export function localMachine(agentId = 'local') {
|
|
40
|
-
return async ({ signal }) => {
|
|
49
|
+
export function localMachine(agentId = 'local', options = {}) {
|
|
50
|
+
return async ({ signal, whenHostExits = 'keep' }) => {
|
|
41
51
|
const started = new Set();
|
|
52
|
+
const useKeeper = options.keeper !== false;
|
|
53
|
+
let keeper;
|
|
54
|
+
const keeperOptions = {
|
|
55
|
+
whenHostExits,
|
|
56
|
+
detachedTimeoutMs: options.detachedTimeoutMs ?? 0,
|
|
57
|
+
};
|
|
58
|
+
const openKeeper = () => {
|
|
59
|
+
keeper ??= useKeeper ? startKeeper(agentId, keeperOptions) : Promise.resolve(undefined);
|
|
60
|
+
return keeper;
|
|
61
|
+
};
|
|
62
|
+
signal?.addEventListener('abort', () => {
|
|
63
|
+
void keeper?.then((link) => link?.stop());
|
|
64
|
+
}, {
|
|
65
|
+
once: true,
|
|
66
|
+
});
|
|
42
67
|
const start = async (command, env) => {
|
|
43
68
|
const transport = await spawnStdio(agentId, {
|
|
44
69
|
...command,
|
|
45
70
|
env,
|
|
46
|
-
},
|
|
71
|
+
}, {
|
|
72
|
+
signal,
|
|
73
|
+
whenHostExits,
|
|
74
|
+
});
|
|
47
75
|
started.add(transport);
|
|
48
76
|
return transport;
|
|
49
77
|
};
|
|
50
78
|
return {
|
|
51
|
-
reach: async (bridge) =>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
reach: async (bridge) => {
|
|
80
|
+
const link = await openKeeper();
|
|
81
|
+
if (!link) {
|
|
82
|
+
return bridge;
|
|
83
|
+
}
|
|
84
|
+
const url = await link.reach(new URL(bridge.url).pathname, bridge.url);
|
|
85
|
+
return {
|
|
86
|
+
...bridge,
|
|
87
|
+
url,
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
launch: async (command) => {
|
|
91
|
+
const env = {
|
|
92
|
+
...process.env,
|
|
93
|
+
...command.env,
|
|
94
|
+
};
|
|
95
|
+
const link = await openKeeper();
|
|
96
|
+
if (!link) {
|
|
97
|
+
return await start(command, env);
|
|
98
|
+
}
|
|
99
|
+
const transport = await link.launch({
|
|
100
|
+
...command,
|
|
101
|
+
env,
|
|
102
|
+
});
|
|
103
|
+
started.add(transport);
|
|
104
|
+
return transport;
|
|
105
|
+
},
|
|
106
|
+
attach: async (handle) => {
|
|
107
|
+
const named = keeperHandleOf(handle);
|
|
108
|
+
if (!named) {
|
|
109
|
+
throw new AcpConnectError({
|
|
110
|
+
agentId,
|
|
111
|
+
message: `ACP agent '${agentId}' cannot be reattached: its handle names no keeper.`,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const link = attachKeeper(agentId, named, whenHostExits);
|
|
115
|
+
keeper = link;
|
|
116
|
+
const transport = (await link).transport();
|
|
117
|
+
started.add(transport);
|
|
118
|
+
return transport;
|
|
119
|
+
},
|
|
56
120
|
spawn: (command) => start(command, serverEnv(command.env)),
|
|
57
121
|
async close() {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
122
|
+
// In parallel: each stop may take its whole grace period.
|
|
123
|
+
const closing = [
|
|
124
|
+
...started,
|
|
125
|
+
].map((transport) => transport.close().catch(() => undefined));
|
|
61
126
|
started.clear();
|
|
127
|
+
closing.push((keeper ?? Promise.resolve(undefined))
|
|
128
|
+
.then((link) => link?.stop())
|
|
129
|
+
.catch(() => undefined));
|
|
130
|
+
await Promise.all(closing);
|
|
62
131
|
},
|
|
63
132
|
};
|
|
64
133
|
};
|
|
65
134
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Spawn `command` with exactly its `env` and carry its stdin/stdout as a byte
|
|
137
|
+
* stream. The child leads its own process group (POSIX), so closing or
|
|
138
|
+
* aborting stops everything it started — a wrapper's native grandchild
|
|
139
|
+
* included — and releases the pipes; see `stopProcessTree`. A missing binary
|
|
140
|
+
* or a signal that is already aborted rejects here, not at `close()`.
|
|
141
|
+
*/
|
|
142
|
+
async function spawnStdio(agentId, command, { signal, whenHostExits = 'keep', }) {
|
|
143
|
+
const fail = (message, cause) => new AcpConnectError({
|
|
144
|
+
agentId,
|
|
145
|
+
message: `ACP agent '${agentId}' (${command.command}) ${message}.`,
|
|
146
|
+
cause,
|
|
147
|
+
});
|
|
148
|
+
if (signal?.aborted) {
|
|
149
|
+
throw fail('was not started: the launch was already aborted', signal.reason);
|
|
150
|
+
}
|
|
68
151
|
const child = spawn(command.command, [
|
|
69
152
|
...(command.args ?? []),
|
|
70
153
|
], {
|
|
@@ -75,50 +158,61 @@ async function spawnStdio(agentId, command, signal) {
|
|
|
75
158
|
'pipe',
|
|
76
159
|
'inherit',
|
|
77
160
|
],
|
|
161
|
+
// Its own process group, so shutdown can signal the whole tree without
|
|
162
|
+
// ever signalling the host's group. Windows stops the tree by pid.
|
|
163
|
+
detached: process.platform !== 'win32',
|
|
164
|
+
windowsHide: true,
|
|
78
165
|
});
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
166
|
+
const forgetOnExit = whenHostExits === 'stop' ? stopWithHost(child) : () => undefined;
|
|
167
|
+
let closing;
|
|
168
|
+
const close = () => {
|
|
169
|
+
signal?.removeEventListener('abort', onAbort);
|
|
170
|
+
forgetOnExit();
|
|
171
|
+
closing ??= stopProcessTree(child).catch((error) => {
|
|
172
|
+
throw fail('could not be stopped', error);
|
|
86
173
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// returned. Capture it so the failure is reported against the agent rather
|
|
90
|
-
// than escaping as an unhandled 'error' event.
|
|
91
|
-
let spawnError;
|
|
92
|
-
child.once('error', (error) => {
|
|
93
|
-
spawnError = error;
|
|
94
|
-
});
|
|
174
|
+
return closing;
|
|
175
|
+
};
|
|
95
176
|
const onAbort = () => {
|
|
96
|
-
|
|
177
|
+
// An explicit close() still observes a failure; this only keeps it handled.
|
|
178
|
+
void close().catch(() => undefined);
|
|
97
179
|
};
|
|
98
180
|
signal?.addEventListener('abort', onAbort, {
|
|
99
181
|
once: true,
|
|
100
182
|
});
|
|
101
|
-
|
|
183
|
+
try {
|
|
184
|
+
await new Promise((resolve, reject) => {
|
|
185
|
+
child.once('spawn', resolve);
|
|
186
|
+
child.once('error', reject);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
await close().catch(() => undefined);
|
|
191
|
+
throw fail('failed to start', error);
|
|
192
|
+
}
|
|
193
|
+
// A late error (a broken pipe after spawn) must not escape as an unhandled
|
|
194
|
+
// 'error' event; the stream ending reports it to the connection.
|
|
195
|
+
child.on('error', () => undefined);
|
|
196
|
+
// Shutdown destroys our ends of the pipes, possibly mid-read; the web
|
|
197
|
+
// streams report that to their reader, so the Node-side error is handled.
|
|
198
|
+
child.stdin?.on('error', () => undefined);
|
|
199
|
+
child.stdout?.on('error', () => undefined);
|
|
200
|
+
if (signal?.aborted) {
|
|
201
|
+
await close().catch(() => undefined);
|
|
202
|
+
throw fail('was aborted while starting', signal.reason);
|
|
203
|
+
}
|
|
204
|
+
const stdin = child.stdin;
|
|
205
|
+
const stdout = child.stdout;
|
|
206
|
+
if (!stdin || !stdout) {
|
|
207
|
+
await close().catch(() => undefined);
|
|
208
|
+
throw fail('was spawned without usable stdio pipes', undefined);
|
|
209
|
+
}
|
|
102
210
|
return {
|
|
103
211
|
// `node:stream`'s web-stream types are structurally the same as the
|
|
104
212
|
// global ones but nominally distinct, so the bridge is cast once here
|
|
105
213
|
// rather than leaking a Node-specific type through the transport contract.
|
|
106
214
|
readable: frameworkCast(Readable.toWeb(stdout)),
|
|
107
215
|
writable: frameworkCast(Writable.toWeb(stdin)),
|
|
108
|
-
|
|
109
|
-
if (closed) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
closed = true;
|
|
113
|
-
signal?.removeEventListener('abort', onAbort);
|
|
114
|
-
child.kill();
|
|
115
|
-
if (spawnError) {
|
|
116
|
-
throw new AcpConnectError({
|
|
117
|
-
agentId,
|
|
118
|
-
message: `ACP agent '${agentId}' (${command.command}) failed to start.`,
|
|
119
|
-
cause: spawnError,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
},
|
|
216
|
+
close,
|
|
123
217
|
};
|
|
124
218
|
}
|
package/dist/tool-bridge.d.ts
CHANGED
|
@@ -47,8 +47,15 @@ export interface ToolBridge {
|
|
|
47
47
|
readonly server: AcpMcpServer;
|
|
48
48
|
readonly env: Record<string, string>;
|
|
49
49
|
readonly permits?: BridgedPermissionCheck;
|
|
50
|
+
/** What a later host needs to serve this bridge to the same agent. */
|
|
51
|
+
readonly credentials: ToolBridgeCredentials;
|
|
50
52
|
close(): Promise<void>;
|
|
51
53
|
}
|
|
54
|
+
/** The path and token a bridge serves under. */
|
|
55
|
+
export interface ToolBridgeCredentials {
|
|
56
|
+
readonly path: string;
|
|
57
|
+
readonly token: string;
|
|
58
|
+
}
|
|
52
59
|
/**
|
|
53
60
|
* Start the loopback MCP server, carry it to the agent's machine, and work
|
|
54
61
|
* out the agent's launch settings from the address it reaches there. The
|
|
@@ -64,4 +71,6 @@ export declare function startToolBridge(opts: {
|
|
|
64
71
|
env?: Record<string, string | undefined>;
|
|
65
72
|
/** Carries the bridge to the agent's machine; this machine's loopback by default. */
|
|
66
73
|
reach?: (bridge: ToolBridgeAddress) => Promise<ToolBridgeAddress>;
|
|
74
|
+
/** Serve under the path and token an earlier host gave a reattached agent. */
|
|
75
|
+
reuse?: ToolBridgeCredentials;
|
|
67
76
|
}): Promise<ToolBridge>;
|
package/dist/tool-bridge.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
export async function startToolBridge(opts) {
|
|
19
19
|
const { serveToolsOverHttp } = await import('./mcp/node.js');
|
|
20
|
-
const http = await serveToolsOverHttp(opts.getHost);
|
|
20
|
+
const http = await serveToolsOverHttp(opts.getHost, opts.reuse);
|
|
21
21
|
const local = {
|
|
22
22
|
serverName: opts.serverName,
|
|
23
23
|
url: http.url,
|
|
@@ -56,6 +56,10 @@ export async function startToolBridge(opts) {
|
|
|
56
56
|
},
|
|
57
57
|
env,
|
|
58
58
|
permits: opts.wiring.permits,
|
|
59
|
+
credentials: {
|
|
60
|
+
path: new URL(http.url).pathname,
|
|
61
|
+
token: http.token,
|
|
62
|
+
},
|
|
59
63
|
close: () => http.close(),
|
|
60
64
|
};
|
|
61
65
|
}
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* both directions — capability negotiation, session updates, permission
|
|
7
7
|
* round-trips, `fs/*` and `terminal/*` callbacks — without spawning a process.
|
|
8
8
|
*/
|
|
9
|
+
import type * as acp from '@agentclientprotocol/sdk';
|
|
10
|
+
import { AgentSideConnection } from '@agentclientprotocol/sdk';
|
|
9
11
|
import type { AcpMachineFactory, AcpTransport, AcpTransportFactory, SpawnCommand } from '@noetic-tools/types';
|
|
10
|
-
import type * as acp from '@zed-industries/agent-client-protocol';
|
|
11
|
-
import { AgentSideConnection } from '@zed-industries/agent-client-protocol';
|
|
12
12
|
/** @public The two ends of a loopback connection. */
|
|
13
13
|
export interface AcpLoopbackPair {
|
|
14
14
|
/** Give this to `openAcpConnection`. */
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* both directions — capability negotiation, session updates, permission
|
|
7
7
|
* round-trips, `fs/*` and `terminal/*` callbacks — without spawning a process.
|
|
8
8
|
*/
|
|
9
|
+
import { AgentSideConnection, ndJsonStream } from '@agentclientprotocol/sdk';
|
|
9
10
|
import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
|
|
10
|
-
import { AgentSideConnection, ndJsonStream } from '@zed-industries/agent-client-protocol';
|
|
11
11
|
function endpoint(readable, writable) {
|
|
12
12
|
let closed = false;
|
|
13
13
|
return {
|
package/dist/turn.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* {@link AcpTurnResult}: Noetic conversation items, the assistant text, the
|
|
4
4
|
* agent's plan, its slash commands, and its final mode.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Transcript updates are folded here; every update also reaches the host. See `user_message_chunk`
|
|
7
7
|
* below for the one whose payload is deliberately not turned into an item.
|
|
8
8
|
*/
|
|
9
9
|
import type { AcpPlanEntry, AcpSessionNotification, AcpStopReason, AcpToolCallContent, AcpTurnResult, TokenUsage } from '@noetic-tools/types';
|
|
@@ -12,7 +12,7 @@ import type { AcpPlanEntry, AcpSessionNotification, AcpStopReason, AcpToolCallCo
|
|
|
12
12
|
* carries. Diff and terminal content are rendered structurally rather than
|
|
13
13
|
* discarded, so the transcript keeps what the agent actually did.
|
|
14
14
|
*/
|
|
15
|
-
export declare function renderToolCallContent(content: ReadonlyArray<AcpToolCallContent>, rawOutput?:
|
|
15
|
+
export declare function renderToolCallContent(content: ReadonlyArray<AcpToolCallContent>, rawOutput?: unknown): string;
|
|
16
16
|
/** @public Folds a turn's session updates into a result. */
|
|
17
17
|
export declare class AcpTurnAccumulator {
|
|
18
18
|
private reasoning;
|
|
@@ -36,6 +36,7 @@ export declare class AcpTurnAccumulator {
|
|
|
36
36
|
stopReason: AcpStopReason;
|
|
37
37
|
usage?: TokenUsage;
|
|
38
38
|
cost?: number;
|
|
39
|
+
reportedUsage?: AcpTurnResult['reportedUsage'];
|
|
39
40
|
}): AcpTurnResult;
|
|
40
41
|
/**
|
|
41
42
|
* The agent's last message — the text after its final tool call — as a
|
package/dist/turn.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* {@link AcpTurnResult}: Noetic conversation items, the assistant text, the
|
|
4
4
|
* agent's plan, its slash commands, and its final mode.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Transcript updates are folded here; every update also reaches the host. See `user_message_chunk`
|
|
7
7
|
* below for the one whose payload is deliberately not turned into an item.
|
|
8
8
|
*/
|
|
9
9
|
import { assistantMessageItem, contentBlockText, functionCallItem, functionCallOutputItem, } from './items.js';
|
|
@@ -100,6 +100,7 @@ export class AcpTurnAccumulator {
|
|
|
100
100
|
text: this.finalText(),
|
|
101
101
|
usage: opts.usage,
|
|
102
102
|
cost: opts.cost,
|
|
103
|
+
reportedUsage: opts.reportedUsage,
|
|
103
104
|
plan: this.plan,
|
|
104
105
|
availableCommands: this.availableCommands,
|
|
105
106
|
currentModeId: this.currentModeId,
|
|
@@ -157,13 +158,32 @@ export class AcpTurnAccumulator {
|
|
|
157
158
|
];
|
|
158
159
|
return;
|
|
159
160
|
}
|
|
161
|
+
if (update.sessionUpdate === 'plan_update') {
|
|
162
|
+
// The draft plan protocol: an item plan replaces the snapshot; a file or
|
|
163
|
+
// markdown plan has no entries to fold, and stays on the event stream.
|
|
164
|
+
if (update.plan.type === 'items') {
|
|
165
|
+
this.plan = [
|
|
166
|
+
...update.plan.entries,
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (update.sessionUpdate === 'plan_removed') {
|
|
172
|
+
this.plan = [];
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
160
175
|
if (update.sessionUpdate === 'available_commands_update') {
|
|
161
176
|
this.availableCommands = [
|
|
162
177
|
...update.availableCommands,
|
|
163
178
|
];
|
|
164
179
|
return;
|
|
165
180
|
}
|
|
166
|
-
|
|
181
|
+
if (update.sessionUpdate === 'current_mode_update') {
|
|
182
|
+
this.currentModeId = update.currentModeId;
|
|
183
|
+
}
|
|
184
|
+
// Every other update (`usage_update`, `config_option_update`,
|
|
185
|
+
// `session_info_update`, …) carries no transcript content; it still reaches
|
|
186
|
+
// the host through `onSessionUpdate`.
|
|
167
187
|
}
|
|
168
188
|
trackToolCall(update) {
|
|
169
189
|
if (!this.toolCalls.has(update.toolCallId)) {
|