@agentrq/acp-gateway 0.2.35 → 0.3.1
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 +40 -1
- package/dist/__tests__/index.test.js +104 -1
- package/dist/__tests__/index.test.js.map +1 -1
- package/dist/__tests__/native.test.js +688 -0
- package/dist/__tests__/native.test.js.map +1 -0
- package/dist/__tests__/nativeFrames.test.js +184 -0
- package/dist/__tests__/nativeFrames.test.js.map +1 -0
- package/dist/__tests__/nativeHeaders.test.js +41 -0
- package/dist/__tests__/nativeHeaders.test.js.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -1
- package/dist/mcpClient.js +2 -1
- package/dist/mcpClient.js.map +1 -1
- package/dist/native.js +441 -0
- package/dist/native.js.map +1 -0
- package/dist/nativeFrames.js +192 -0
- package/dist/nativeFrames.js.map +1 -0
- package/dist/version.js +8 -0
- package/dist/version.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import { PassThrough } from "node:stream";
|
|
4
|
+
import { nativeSupported, runNative } from "../native.js";
|
|
5
|
+
import { setVerbose } from "../log.js";
|
|
6
|
+
import { pkg } from "../version.js";
|
|
7
|
+
class FakeSocket {
|
|
8
|
+
url;
|
|
9
|
+
init;
|
|
10
|
+
static instances = [];
|
|
11
|
+
readyState = 0;
|
|
12
|
+
bufferedAmount = 0;
|
|
13
|
+
sent = [];
|
|
14
|
+
closedWith;
|
|
15
|
+
onopen = null;
|
|
16
|
+
onmessage = null;
|
|
17
|
+
onclose = null;
|
|
18
|
+
onerror = null;
|
|
19
|
+
constructor(url, init) {
|
|
20
|
+
this.url = url;
|
|
21
|
+
this.init = init;
|
|
22
|
+
FakeSocket.instances.push(this);
|
|
23
|
+
}
|
|
24
|
+
send(data) {
|
|
25
|
+
this.sent.push(data);
|
|
26
|
+
}
|
|
27
|
+
close(code) {
|
|
28
|
+
this.closedWith = code;
|
|
29
|
+
this.readyState = 3;
|
|
30
|
+
}
|
|
31
|
+
open() {
|
|
32
|
+
this.readyState = 1;
|
|
33
|
+
this.onopen?.({});
|
|
34
|
+
this.onerror?.({});
|
|
35
|
+
}
|
|
36
|
+
receive(data) {
|
|
37
|
+
this.onmessage?.({ data });
|
|
38
|
+
}
|
|
39
|
+
welcome(extra = {}) {
|
|
40
|
+
this.receive(JSON.stringify({ agentrqBridge: "welcome", connectionId: "AB", resumed: false, received: 0, graceSeconds: 120, ...extra }));
|
|
41
|
+
}
|
|
42
|
+
drop(code, reason = "") {
|
|
43
|
+
this.readyState = 3;
|
|
44
|
+
this.onclose?.({ code, reason });
|
|
45
|
+
}
|
|
46
|
+
controls() {
|
|
47
|
+
return this.sent.filter((f) => f.startsWith('{"agentrqBridge":')).map((f) => JSON.parse(f));
|
|
48
|
+
}
|
|
49
|
+
data() {
|
|
50
|
+
return this.sent.filter((f) => !f.startsWith('{"agentrqBridge":'));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function fakeChild() {
|
|
54
|
+
const c = new EventEmitter();
|
|
55
|
+
c.stdin = new PassThrough();
|
|
56
|
+
c.stdout = new PassThrough();
|
|
57
|
+
c.stderr = new PassThrough();
|
|
58
|
+
c.pid = 42;
|
|
59
|
+
c.written = [];
|
|
60
|
+
c.stdin.on("data", (d) => c.written.push(String(d)));
|
|
61
|
+
c.kill = vi.fn(() => c.emit("exit", null, "SIGTERM"));
|
|
62
|
+
return c;
|
|
63
|
+
}
|
|
64
|
+
const flush = () => new Promise((r) => setImmediate(r));
|
|
65
|
+
const status = (code, body = "") => vi.fn(async () => new Response(body || null, { status: code }));
|
|
66
|
+
async function start(over = {}) {
|
|
67
|
+
const children = [];
|
|
68
|
+
const spawnImpl = vi.fn(() => {
|
|
69
|
+
const c = fakeChild();
|
|
70
|
+
children.push(c);
|
|
71
|
+
return c;
|
|
72
|
+
});
|
|
73
|
+
const exit = vi.fn();
|
|
74
|
+
const fetchImpl = over.fetchImpl ?? status(426);
|
|
75
|
+
let finished = false;
|
|
76
|
+
const done = runNative({
|
|
77
|
+
version: 1,
|
|
78
|
+
acpCmdArgs: ["agent", "--acp"],
|
|
79
|
+
endpoint: "wss://h/api/acp/v1",
|
|
80
|
+
token: "t",
|
|
81
|
+
cwd: "/work",
|
|
82
|
+
WebSocketImpl: FakeSocket,
|
|
83
|
+
spawnImpl: spawnImpl,
|
|
84
|
+
nodeVersion: "22.0.0",
|
|
85
|
+
random: () => 0.5,
|
|
86
|
+
now: () => Date.now(),
|
|
87
|
+
exit,
|
|
88
|
+
...over,
|
|
89
|
+
fetchImpl: fetchImpl,
|
|
90
|
+
}).then(() => {
|
|
91
|
+
finished = true;
|
|
92
|
+
});
|
|
93
|
+
await flush();
|
|
94
|
+
return {
|
|
95
|
+
children,
|
|
96
|
+
spawnImpl,
|
|
97
|
+
exit,
|
|
98
|
+
fetchImpl,
|
|
99
|
+
done,
|
|
100
|
+
isDone: () => finished,
|
|
101
|
+
ws: () => FakeSocket.instances[FakeSocket.instances.length - 1],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** Starts, opens the socket and takes the first welcome. */
|
|
105
|
+
async function connected(over = {}) {
|
|
106
|
+
const t = await start(over);
|
|
107
|
+
t.ws().open();
|
|
108
|
+
t.ws().welcome();
|
|
109
|
+
return t;
|
|
110
|
+
}
|
|
111
|
+
let errors;
|
|
112
|
+
const printed = () => errors.mock.calls.map((c) => c.join(" ")).join("\n");
|
|
113
|
+
beforeEach(() => {
|
|
114
|
+
FakeSocket.instances = [];
|
|
115
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "setInterval", "clearInterval", "Date"] });
|
|
116
|
+
errors = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
117
|
+
});
|
|
118
|
+
afterEach(async () => {
|
|
119
|
+
// Stops any bridge a test left running, and its signal handlers with it.
|
|
120
|
+
process.emit("SIGTERM");
|
|
121
|
+
setVerbose(false);
|
|
122
|
+
vi.useRealTimers();
|
|
123
|
+
vi.unstubAllGlobals();
|
|
124
|
+
errors.mockRestore();
|
|
125
|
+
});
|
|
126
|
+
describe("nativeSupported", () => {
|
|
127
|
+
it("needs a WebSocket and Node 22", () => {
|
|
128
|
+
expect(nativeSupported(FakeSocket, "22.4.0")).toBe(true);
|
|
129
|
+
expect(nativeSupported(FakeSocket, "20.11.0")).toBe(false);
|
|
130
|
+
expect(nativeSupported(undefined, "26.0.0")).toBe(false);
|
|
131
|
+
expect(nativeSupported(FakeSocket)).toBe(Number(process.versions.node.split(".")[0]) >= 22);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
describe("runNative", () => {
|
|
135
|
+
it("connects with the subprotocol and headers, and no resume the first time", async () => {
|
|
136
|
+
const t = await start({ agentId: "codex-acp", modelId: "m", effort: "high", maxConcurrency: 2 });
|
|
137
|
+
expect(t.ws().url).toBe("wss://h/api/acp/v1");
|
|
138
|
+
expect(t.ws().init.protocols).toEqual(["agentrq.bridge.1"]);
|
|
139
|
+
expect(t.ws().init.headers).toMatchObject({
|
|
140
|
+
Authorization: "Bearer t",
|
|
141
|
+
"X-AgentRQ-Cwd": "/work",
|
|
142
|
+
"X-AgentRQ-Agent": "codex-acp",
|
|
143
|
+
"X-AgentRQ-Agent-Model": "m",
|
|
144
|
+
"X-AgentRQ-Agent-Effort": "high",
|
|
145
|
+
"X-AgentRQ-Max-Concurrency": "2",
|
|
146
|
+
"X-AgentRQ-Bridge-Version": pkg.version,
|
|
147
|
+
"X-AgentRQ-Platform": `${process.platform}/${process.arch}`,
|
|
148
|
+
});
|
|
149
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
150
|
+
expect(t.spawnImpl).toHaveBeenCalledWith("agent", ["--acp"], expect.objectContaining({ stdio: ["pipe", "pipe", "pipe"], cwd: "/work", shell: false }));
|
|
151
|
+
expect(t.fetchImpl).toHaveBeenCalledWith("https://h/api/acp/v1", expect.objectContaining({ headers: { Authorization: "Bearer t" }, redirect: "manual" }));
|
|
152
|
+
});
|
|
153
|
+
it("pipes agent lines to frames and frames to agent lines", async () => {
|
|
154
|
+
const t = await connected();
|
|
155
|
+
t.children[0].stdout.write('{"jsonrpc":"2.0","id":1,"result":{}}\n');
|
|
156
|
+
await flush();
|
|
157
|
+
expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":1,"result":{}}']);
|
|
158
|
+
t.ws().receive('{"jsonrpc":"2.0","id":1,"method":"initialize"}');
|
|
159
|
+
await flush();
|
|
160
|
+
expect(t.children[0].written.join("")).toBe('{"jsonrpc":"2.0","id":1,"method":"initialize"}\n');
|
|
161
|
+
});
|
|
162
|
+
it("holds lines written before the welcome and sends them once it arrives", async () => {
|
|
163
|
+
const t = await start();
|
|
164
|
+
t.children[0].stdout.write("early\n");
|
|
165
|
+
await flush();
|
|
166
|
+
t.ws().open();
|
|
167
|
+
expect(t.ws().sent).toEqual([]);
|
|
168
|
+
t.ws().welcome();
|
|
169
|
+
expect(t.ws().data()).toEqual(["early"]);
|
|
170
|
+
});
|
|
171
|
+
it("drops an agent line that forges a control frame", async () => {
|
|
172
|
+
const t = await connected();
|
|
173
|
+
t.children[0].stdout.write('{"agentrqBridge":"bye"}\n');
|
|
174
|
+
await flush();
|
|
175
|
+
expect(t.ws().sent).toEqual([]);
|
|
176
|
+
expect(printed()).toMatch(/Dropped a line that looks like a bridge control frame/);
|
|
177
|
+
});
|
|
178
|
+
it("drops a line over 16 MiB and tells AgentRQ", async () => {
|
|
179
|
+
const t = await connected();
|
|
180
|
+
t.children[0].stdout.write("x".repeat(16 * 1024 * 1024 + 1) + "\nafter\n");
|
|
181
|
+
await flush();
|
|
182
|
+
expect(t.ws().data()).toEqual(["after"]);
|
|
183
|
+
expect(t.ws().controls()).toEqual([
|
|
184
|
+
{ agentrqBridge: "stderr", lines: ["acp-gateway dropped a line over 16 MiB from the agent"] },
|
|
185
|
+
]);
|
|
186
|
+
});
|
|
187
|
+
it("drops a line that outgrows 16 MiB before its newline", async () => {
|
|
188
|
+
const t = await connected();
|
|
189
|
+
t.children[0].stdout.write("x".repeat(16 * 1024 * 1024 + 1));
|
|
190
|
+
t.children[0].stdout.write("tail\nnext\n");
|
|
191
|
+
await flush();
|
|
192
|
+
expect(t.ws().data()).toEqual(["next"]);
|
|
193
|
+
expect(printed()).toMatch(/Dropped a line over 16 MiB/);
|
|
194
|
+
});
|
|
195
|
+
it("drops a frame that contains a newline", async () => {
|
|
196
|
+
const t = await connected();
|
|
197
|
+
t.ws().receive('{"a":"x\ny"}');
|
|
198
|
+
await flush();
|
|
199
|
+
expect(t.children[0].written).toEqual([]);
|
|
200
|
+
expect(printed()).toMatch(/contains a newline/);
|
|
201
|
+
});
|
|
202
|
+
it("drops a binary frame", async () => {
|
|
203
|
+
const t = await connected();
|
|
204
|
+
t.ws().receive(new ArrayBuffer(2));
|
|
205
|
+
expect(printed()).toMatch(/binary frame/);
|
|
206
|
+
});
|
|
207
|
+
it("ignores control frames it does not know, and broken ones", async () => {
|
|
208
|
+
const t = await connected();
|
|
209
|
+
t.ws().receive('{"agentrqBridge":"later-kind","x":1}');
|
|
210
|
+
t.ws().receive('{"agentrqBridge":nope');
|
|
211
|
+
expect(t.exit).not.toHaveBeenCalled();
|
|
212
|
+
expect(t.children[0].written).toEqual([]);
|
|
213
|
+
});
|
|
214
|
+
it("acks every 64 frames", async () => {
|
|
215
|
+
const t = await connected();
|
|
216
|
+
for (let i = 0; i < 64; i++)
|
|
217
|
+
t.ws().receive(`{"n":${i}}`);
|
|
218
|
+
expect(t.ws().controls()).toEqual([{ agentrqBridge: "ack", received: 64 }]);
|
|
219
|
+
});
|
|
220
|
+
it("acks within a second while frames flow, and not when none did", async () => {
|
|
221
|
+
const t = await connected();
|
|
222
|
+
vi.advanceTimersByTime(1000);
|
|
223
|
+
expect(t.ws().controls()).toEqual([]);
|
|
224
|
+
t.ws().receive('{"n":1}');
|
|
225
|
+
vi.advanceTimersByTime(1000);
|
|
226
|
+
expect(t.ws().controls()).toEqual([{ agentrqBridge: "ack", received: 1 }]);
|
|
227
|
+
});
|
|
228
|
+
it("resumes with what it received and replays what the server missed", async () => {
|
|
229
|
+
const t = await connected();
|
|
230
|
+
t.children[0].stdout.write("l1\nl2\nl3\n");
|
|
231
|
+
await flush();
|
|
232
|
+
t.ws().receive('{"s":1}');
|
|
233
|
+
t.ws().receive('{"s":2}');
|
|
234
|
+
t.ws().receive('{"agentrqBridge":"ack","received":1}');
|
|
235
|
+
t.ws().drop(1006);
|
|
236
|
+
vi.advanceTimersByTime(999);
|
|
237
|
+
expect(FakeSocket.instances).toHaveLength(1);
|
|
238
|
+
vi.advanceTimersByTime(1);
|
|
239
|
+
const second = t.ws();
|
|
240
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
241
|
+
expect(second.init.headers["X-AgentRQ-Resume"]).toBe("AB:2");
|
|
242
|
+
second.open();
|
|
243
|
+
second.welcome({ resumed: true, received: 2 });
|
|
244
|
+
expect(second.data()).toEqual(["l3"]);
|
|
245
|
+
expect(t.children).toHaveLength(1);
|
|
246
|
+
});
|
|
247
|
+
it("buffers agent lines while disconnected and replays them on resume", async () => {
|
|
248
|
+
const t = await connected();
|
|
249
|
+
t.ws().drop(1006);
|
|
250
|
+
t.children[0].stdout.write("offline\n");
|
|
251
|
+
await flush();
|
|
252
|
+
vi.advanceTimersByTime(1000);
|
|
253
|
+
t.ws().open();
|
|
254
|
+
t.ws().welcome({ resumed: true, received: 0 });
|
|
255
|
+
expect(t.ws().data()).toEqual(["offline"]);
|
|
256
|
+
});
|
|
257
|
+
it("a fresh welcome on a reconnect restarts the agent first", async () => {
|
|
258
|
+
const t = await connected();
|
|
259
|
+
t.ws().drop(1006);
|
|
260
|
+
vi.advanceTimersByTime(1000);
|
|
261
|
+
t.ws().open();
|
|
262
|
+
t.ws().welcome({ resumed: false, connectionId: "CD" });
|
|
263
|
+
expect(t.children[0].kill).toHaveBeenCalled();
|
|
264
|
+
expect(t.children).toHaveLength(2);
|
|
265
|
+
t.ws().receive('{"jsonrpc":"2.0","id":0,"method":"initialize"}');
|
|
266
|
+
await flush();
|
|
267
|
+
expect(t.children[0].written).toEqual([]);
|
|
268
|
+
expect(t.children[1].written).toEqual(['{"jsonrpc":"2.0","id":0,"method":"initialize"}\n']);
|
|
269
|
+
});
|
|
270
|
+
it("ignores what a replaced agent still writes", async () => {
|
|
271
|
+
const t = await connected();
|
|
272
|
+
t.ws().receive('{"agentrqBridge":"restart","reason":"wedged"}');
|
|
273
|
+
t.children[0].stdout.write("stale\n");
|
|
274
|
+
await flush();
|
|
275
|
+
expect(FakeSocket.instances.flatMap((s) => s.data())).toEqual([]);
|
|
276
|
+
});
|
|
277
|
+
it("the restart control frame restarts the agent and reconnects fresh", async () => {
|
|
278
|
+
const t = await connected();
|
|
279
|
+
t.ws().receive('{"agentrqBridge":"restart","reason":"no answer to initialize in 60 s"}');
|
|
280
|
+
expect(t.children[0].kill).toHaveBeenCalled();
|
|
281
|
+
expect(t.children).toHaveLength(2);
|
|
282
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
283
|
+
expect(FakeSocket.instances[0].closedWith).toBe(1000);
|
|
284
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
285
|
+
expect(printed()).toMatch(/no answer to initialize in 60 s/);
|
|
286
|
+
// The new agent was started for this connection: its welcome does not restart it again.
|
|
287
|
+
t.ws().open();
|
|
288
|
+
t.ws().welcome({ connectionId: "EF" });
|
|
289
|
+
expect(t.children).toHaveLength(2);
|
|
290
|
+
});
|
|
291
|
+
it("a restart frame without a reason still restarts", async () => {
|
|
292
|
+
const t = await connected();
|
|
293
|
+
t.ws().receive('{"agentrqBridge":"restart"}');
|
|
294
|
+
expect(t.children).toHaveLength(2);
|
|
295
|
+
expect(printed()).toMatch(/AgentRQ asked for it/);
|
|
296
|
+
});
|
|
297
|
+
it("close 4000 restarts the agent and reconnects fresh after the backoff", async () => {
|
|
298
|
+
const t = await connected();
|
|
299
|
+
t.ws().drop(4000);
|
|
300
|
+
expect(t.children).toHaveLength(2);
|
|
301
|
+
vi.advanceTimersByTime(1000);
|
|
302
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
303
|
+
t.ws().open();
|
|
304
|
+
t.ws().welcome();
|
|
305
|
+
expect(t.children).toHaveLength(2);
|
|
306
|
+
});
|
|
307
|
+
it("close 4001 reconnects fresh, and the fresh welcome restarts the agent", async () => {
|
|
308
|
+
const t = await connected();
|
|
309
|
+
t.ws().drop(4001, "grace expired");
|
|
310
|
+
vi.advanceTimersByTime(1000);
|
|
311
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
312
|
+
t.ws().open();
|
|
313
|
+
t.ws().welcome();
|
|
314
|
+
expect(t.children).toHaveLength(2);
|
|
315
|
+
});
|
|
316
|
+
it("a resume it cannot replay starts over", async () => {
|
|
317
|
+
const t = await connected();
|
|
318
|
+
t.ws().drop(1006);
|
|
319
|
+
vi.advanceTimersByTime(1000);
|
|
320
|
+
t.ws().open();
|
|
321
|
+
t.ws().welcome({ resumed: true, received: 99 });
|
|
322
|
+
expect(t.children).toHaveLength(2);
|
|
323
|
+
expect(FakeSocket.instances).toHaveLength(3);
|
|
324
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
325
|
+
});
|
|
326
|
+
it("does not ask to resume once its replay ring has overflowed", async () => {
|
|
327
|
+
const t = await connected();
|
|
328
|
+
t.ws().drop(1006);
|
|
329
|
+
t.children[0].stdout.write(Array.from({ length: 1025 }, (_, i) => `l${i}`).join("\n") + "\n");
|
|
330
|
+
await flush();
|
|
331
|
+
vi.advanceTimersByTime(1000);
|
|
332
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
333
|
+
});
|
|
334
|
+
it("backs off longer after each failed reconnect, and starts again after a welcome", async () => {
|
|
335
|
+
const t = await connected();
|
|
336
|
+
t.ws().drop(1006);
|
|
337
|
+
vi.advanceTimersByTime(1000);
|
|
338
|
+
t.ws().drop(1006);
|
|
339
|
+
vi.advanceTimersByTime(1999);
|
|
340
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
341
|
+
vi.advanceTimersByTime(1);
|
|
342
|
+
expect(FakeSocket.instances).toHaveLength(3);
|
|
343
|
+
t.ws().open();
|
|
344
|
+
t.ws().welcome({ resumed: true, received: 0 });
|
|
345
|
+
t.ws().drop(1001, "server shutting down");
|
|
346
|
+
vi.advanceTimersByTime(1000);
|
|
347
|
+
expect(FakeSocket.instances).toHaveLength(4);
|
|
348
|
+
});
|
|
349
|
+
it("a version mismatch is printed and exits 1", async () => {
|
|
350
|
+
const t = await connected();
|
|
351
|
+
const message = "this agent speaks ACP v2; start it with --acp-version 2";
|
|
352
|
+
t.ws().receive(JSON.stringify({ agentrqBridge: "error", code: "version_mismatch", message }));
|
|
353
|
+
t.ws().drop(4002);
|
|
354
|
+
await t.done;
|
|
355
|
+
expect(printed()).toContain(message);
|
|
356
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
357
|
+
expect(t.children[0].kill).toHaveBeenCalled();
|
|
358
|
+
expect(FakeSocket.instances).toHaveLength(1);
|
|
359
|
+
});
|
|
360
|
+
it("a terminal login requirement is printed and exits 1", async () => {
|
|
361
|
+
const t = await connected();
|
|
362
|
+
const message = "the agent needs a terminal login: run npx -y @agentrq/acp-gateway@latest --login --agent x on this machine, then start it again";
|
|
363
|
+
t.ws().receive(JSON.stringify({ agentrqBridge: "error", code: "auth_required", message }));
|
|
364
|
+
t.ws().drop(4003);
|
|
365
|
+
await t.done;
|
|
366
|
+
expect(printed()).toContain(message);
|
|
367
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
368
|
+
});
|
|
369
|
+
it("a fatal error frame exits on the close that follows, whatever its code", async () => {
|
|
370
|
+
const t = await connected();
|
|
371
|
+
t.ws().receive(JSON.stringify({ agentrqBridge: "error", code: "workspace_archived" }));
|
|
372
|
+
t.ws().drop(1000);
|
|
373
|
+
await t.done;
|
|
374
|
+
expect(printed()).toContain("workspace_archived");
|
|
375
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
376
|
+
});
|
|
377
|
+
it("a policy close without an error frame prints its reason and exits 1", async () => {
|
|
378
|
+
const t = await connected();
|
|
379
|
+
t.ws().drop(1008, "too many agent connections");
|
|
380
|
+
await t.done;
|
|
381
|
+
expect(printed()).toContain("AgentRQ closed the connection (1008): too many agent connections");
|
|
382
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
383
|
+
});
|
|
384
|
+
it("a fatal close with no reason still says so", async () => {
|
|
385
|
+
const t = await connected();
|
|
386
|
+
t.ws().drop(4002);
|
|
387
|
+
await t.done;
|
|
388
|
+
expect(printed()).toContain("AgentRQ closed the connection (4002)");
|
|
389
|
+
});
|
|
390
|
+
it("a non-fatal error frame is printed and the bridge reconnects", async () => {
|
|
391
|
+
const t = await connected();
|
|
392
|
+
t.ws().receive(JSON.stringify({ agentrqBridge: "error", code: "server_shutdown", message: "the server is shutting down" }));
|
|
393
|
+
t.ws().receive(JSON.stringify({ agentrqBridge: "error" }));
|
|
394
|
+
t.ws().drop(1001);
|
|
395
|
+
vi.advanceTimersByTime(1000);
|
|
396
|
+
expect(printed()).toContain("the server is shutting down");
|
|
397
|
+
expect(printed()).toContain("unknown error");
|
|
398
|
+
expect(t.exit).not.toHaveBeenCalled();
|
|
399
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
400
|
+
});
|
|
401
|
+
it("an agent exit is reported and the agent respawned", async () => {
|
|
402
|
+
const t = await connected();
|
|
403
|
+
t.children[0].stderr.write("boom\npanic: x");
|
|
404
|
+
await flush();
|
|
405
|
+
t.children[0].emit("exit", 1, null);
|
|
406
|
+
const first = FakeSocket.instances[0];
|
|
407
|
+
expect(first.controls()).toEqual([
|
|
408
|
+
{ agentrqBridge: "stderr", lines: ["boom", "panic: x"] },
|
|
409
|
+
{ agentrqBridge: "exit", code: 1, signal: null },
|
|
410
|
+
]);
|
|
411
|
+
expect(first.closedWith).toBe(1000);
|
|
412
|
+
vi.advanceTimersByTime(1999);
|
|
413
|
+
expect(t.children).toHaveLength(1);
|
|
414
|
+
vi.advanceTimersByTime(1);
|
|
415
|
+
expect(t.children).toHaveLength(2);
|
|
416
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
417
|
+
expect(t.ws().init.headers["X-AgentRQ-Resume"]).toBeUndefined();
|
|
418
|
+
// The respawned agent belongs to the fresh connection: no second restart.
|
|
419
|
+
t.ws().open();
|
|
420
|
+
t.ws().welcome({ connectionId: "GH" });
|
|
421
|
+
expect(t.children).toHaveLength(2);
|
|
422
|
+
});
|
|
423
|
+
it("an agent exit while disconnected cancels the pending reconnect", async () => {
|
|
424
|
+
const t = await connected();
|
|
425
|
+
t.ws().drop(1006);
|
|
426
|
+
t.children[0].emit("exit", 0, null);
|
|
427
|
+
vi.advanceTimersByTime(2000);
|
|
428
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
429
|
+
expect(t.children).toHaveLength(2);
|
|
430
|
+
});
|
|
431
|
+
it("an agent that cannot be started counts as an exit", async () => {
|
|
432
|
+
const t = await connected();
|
|
433
|
+
t.children[0].emit("error", new Error("spawn agent ENOENT"));
|
|
434
|
+
t.children[0].emit("exit", -2, null);
|
|
435
|
+
expect(printed()).toContain("The agent could not run: spawn agent ENOENT");
|
|
436
|
+
vi.advanceTimersByTime(2000);
|
|
437
|
+
expect(t.children).toHaveLength(2);
|
|
438
|
+
});
|
|
439
|
+
it("five exits within five minutes give up", async () => {
|
|
440
|
+
const t = await connected();
|
|
441
|
+
for (let i = 0; i < 4; i++) {
|
|
442
|
+
t.children[i].stderr.write(`last words ${i}\n`);
|
|
443
|
+
await flush();
|
|
444
|
+
t.children[i].emit("exit", 1, null);
|
|
445
|
+
vi.advanceTimersByTime(60_000);
|
|
446
|
+
}
|
|
447
|
+
expect(t.children).toHaveLength(5);
|
|
448
|
+
t.children[4].stderr.write("final words\n");
|
|
449
|
+
await flush();
|
|
450
|
+
t.children[4].emit("exit", 1, null);
|
|
451
|
+
await t.done;
|
|
452
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
453
|
+
expect(printed()).toMatch(/exited 5 times within 5 minutes; giving up\. Its last output:\nfinal words/);
|
|
454
|
+
});
|
|
455
|
+
it("gives up without a tail when the agent said nothing", async () => {
|
|
456
|
+
const t = await connected();
|
|
457
|
+
for (let i = 0; i < 4; i++) {
|
|
458
|
+
t.children[i].emit("exit", 1, null);
|
|
459
|
+
vi.advanceTimersByTime(60_000);
|
|
460
|
+
}
|
|
461
|
+
t.children[4].emit("exit", 1, null);
|
|
462
|
+
await t.done;
|
|
463
|
+
expect(printed()).toMatch(/giving up\.$/m);
|
|
464
|
+
});
|
|
465
|
+
it("exits spread over more than five minutes keep respawning", async () => {
|
|
466
|
+
const t = await connected();
|
|
467
|
+
for (let i = 0; i < 4; i++) {
|
|
468
|
+
t.children[i].emit("exit", 1, null);
|
|
469
|
+
vi.advanceTimersByTime(60_000);
|
|
470
|
+
}
|
|
471
|
+
vi.advanceTimersByTime(120_000);
|
|
472
|
+
t.children[4].emit("exit", 1, null);
|
|
473
|
+
vi.advanceTimersByTime(60_000);
|
|
474
|
+
expect(t.exit).not.toHaveBeenCalled();
|
|
475
|
+
expect(t.children).toHaveLength(6);
|
|
476
|
+
});
|
|
477
|
+
it("passes the agent's stderr through under --verbose", async () => {
|
|
478
|
+
setVerbose(true);
|
|
479
|
+
const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
|
|
480
|
+
const t = await connected();
|
|
481
|
+
t.children[0].stderr.write("debug line\r\n");
|
|
482
|
+
await flush();
|
|
483
|
+
expect(write).toHaveBeenCalledWith("debug line\r\n");
|
|
484
|
+
write.mockRestore();
|
|
485
|
+
});
|
|
486
|
+
it("native mode on Node 20 refuses at start and classic is untouched", async () => {
|
|
487
|
+
vi.stubGlobal("WebSocket", undefined);
|
|
488
|
+
const t = await start({ WebSocketImpl: undefined });
|
|
489
|
+
await t.done;
|
|
490
|
+
expect(printed()).toContain("native mode needs Node 22 or newer");
|
|
491
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
492
|
+
expect(t.spawnImpl).not.toHaveBeenCalled();
|
|
493
|
+
expect(t.fetchImpl).not.toHaveBeenCalled();
|
|
494
|
+
const old = await start({ nodeVersion: "20.11.0" });
|
|
495
|
+
await old.done;
|
|
496
|
+
expect(old.spawnImpl).not.toHaveBeenCalled();
|
|
497
|
+
});
|
|
498
|
+
it("the preflight's 426 means go ahead", async () => {
|
|
499
|
+
const t = await start();
|
|
500
|
+
expect(FakeSocket.instances).toHaveLength(1);
|
|
501
|
+
expect(t.children).toHaveLength(1);
|
|
502
|
+
});
|
|
503
|
+
it("a server it cannot reach is left to the socket's own retries", async () => {
|
|
504
|
+
const t = await start({ fetchImpl: vi.fn(async () => Promise.reject(new Error("ECONNREFUSED"))) });
|
|
505
|
+
expect(FakeSocket.instances).toHaveLength(1);
|
|
506
|
+
expect(t.exit).not.toHaveBeenCalled();
|
|
507
|
+
});
|
|
508
|
+
it("an old server without the endpoint is named, not retried for ever", async () => {
|
|
509
|
+
const t = await start({ fetchImpl: status(404) });
|
|
510
|
+
await t.done;
|
|
511
|
+
expect(printed()).toContain("this AgentRQ server does not offer native ACP v1 (HTTP 404); update the server, or start without --acp-version");
|
|
512
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
513
|
+
expect(FakeSocket.instances).toHaveLength(0);
|
|
514
|
+
expect(t.spawnImpl).not.toHaveBeenCalled();
|
|
515
|
+
});
|
|
516
|
+
it("a bad token is reported with the server's reason", async () => {
|
|
517
|
+
const t = await start({ fetchImpl: status(401, "not a workspace access token\n") });
|
|
518
|
+
await t.done;
|
|
519
|
+
expect(printed()).toContain("AgentRQ refused the workspace token (HTTP 401): not a workspace access token");
|
|
520
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
521
|
+
});
|
|
522
|
+
it("a forbidden workspace is reported even without a body", async () => {
|
|
523
|
+
const t = await start({ fetchImpl: status(403) });
|
|
524
|
+
await t.done;
|
|
525
|
+
expect(printed()).toMatch(/refused the workspace token \(HTTP 403\)$/m);
|
|
526
|
+
});
|
|
527
|
+
it("a proxy that does not pass the upgrade is reported", async () => {
|
|
528
|
+
const t = await start({ fetchImpl: status(400, "Bad Request") });
|
|
529
|
+
await t.done;
|
|
530
|
+
expect(printed()).toContain("https://h/api/acp/v1 answered HTTP 400 instead of offering a WebSocket upgrade: Bad Request");
|
|
531
|
+
expect(printed()).toContain("must pass Upgrade for /api/acp/");
|
|
532
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
533
|
+
const bare = await start({ fetchImpl: status(502) });
|
|
534
|
+
await bare.done;
|
|
535
|
+
expect(printed()).toContain("answered HTTP 502 instead of offering a WebSocket upgrade. A proxy");
|
|
536
|
+
});
|
|
537
|
+
it("a body that cannot be read is treated as empty", async () => {
|
|
538
|
+
const res = { status: 401, text: () => Promise.reject(new Error("aborted")) };
|
|
539
|
+
const t = await start({ fetchImpl: vi.fn(async () => res) });
|
|
540
|
+
await t.done;
|
|
541
|
+
expect(printed()).toMatch(/\(HTTP 401\)$/m);
|
|
542
|
+
});
|
|
543
|
+
it("five reconnects without a welcome run the preflight again", async () => {
|
|
544
|
+
const fetchImpl = status(426);
|
|
545
|
+
const t = await start({ fetchImpl: fetchImpl });
|
|
546
|
+
for (let i = 0; i < 4; i++) {
|
|
547
|
+
t.ws().drop(1006);
|
|
548
|
+
await vi.advanceTimersByTimeAsync(1000 * 2 ** i);
|
|
549
|
+
}
|
|
550
|
+
expect(fetchImpl).toHaveBeenCalledTimes(1);
|
|
551
|
+
t.ws().drop(1006);
|
|
552
|
+
fetchImpl.mockImplementation(async () => new Response(null, { status: 404 }));
|
|
553
|
+
await vi.advanceTimersByTimeAsync(16_000);
|
|
554
|
+
await t.done;
|
|
555
|
+
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
|
556
|
+
expect(printed()).toContain("does not offer native ACP v1 (HTTP 404)");
|
|
557
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
558
|
+
expect(FakeSocket.instances).toHaveLength(5);
|
|
559
|
+
});
|
|
560
|
+
it("a preflight that passes goes on to reconnect", async () => {
|
|
561
|
+
const t = await start();
|
|
562
|
+
for (let i = 0; i < 5; i++) {
|
|
563
|
+
t.ws().drop(1006);
|
|
564
|
+
await vi.advanceTimersByTimeAsync(1000 * 2 ** i);
|
|
565
|
+
}
|
|
566
|
+
expect(t.fetchImpl).toHaveBeenCalledTimes(2);
|
|
567
|
+
expect(FakeSocket.instances).toHaveLength(6);
|
|
568
|
+
});
|
|
569
|
+
it("a reconnect overtaken by an agent exit during its preflight does not run", async () => {
|
|
570
|
+
let answer;
|
|
571
|
+
const fetchImpl = status(426);
|
|
572
|
+
const t = await start({ fetchImpl: fetchImpl });
|
|
573
|
+
for (let i = 0; i < 4; i++) {
|
|
574
|
+
t.ws().drop(1006);
|
|
575
|
+
await vi.advanceTimersByTimeAsync(1000 * 2 ** i);
|
|
576
|
+
}
|
|
577
|
+
t.ws().drop(1006);
|
|
578
|
+
fetchImpl.mockImplementation(() => new Promise((r) => (answer = r)));
|
|
579
|
+
await vi.advanceTimersByTimeAsync(16_000);
|
|
580
|
+
t.children[0].emit("exit", 1, null);
|
|
581
|
+
answer(new Response(null, { status: 426 }));
|
|
582
|
+
await flush();
|
|
583
|
+
expect(FakeSocket.instances).toHaveLength(5);
|
|
584
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
585
|
+
expect(FakeSocket.instances).toHaveLength(6);
|
|
586
|
+
});
|
|
587
|
+
it.each([[404], [426]])("a signal during a reconnect's preflight wins over its answer (%s)", async (code) => {
|
|
588
|
+
let answer;
|
|
589
|
+
const fetchImpl = status(426);
|
|
590
|
+
const t = await start({ fetchImpl: fetchImpl });
|
|
591
|
+
for (let i = 0; i < 4; i++) {
|
|
592
|
+
t.ws().drop(1006);
|
|
593
|
+
await vi.advanceTimersByTimeAsync(1000 * 2 ** i);
|
|
594
|
+
}
|
|
595
|
+
t.ws().drop(1006);
|
|
596
|
+
fetchImpl.mockImplementation(() => new Promise((r) => (answer = r)));
|
|
597
|
+
await vi.advanceTimersByTimeAsync(16_000);
|
|
598
|
+
process.emit("SIGTERM");
|
|
599
|
+
answer(new Response(null, { status: code }));
|
|
600
|
+
await flush();
|
|
601
|
+
await flush();
|
|
602
|
+
expect(t.exit).toHaveBeenCalledTimes(1);
|
|
603
|
+
expect(t.exit).toHaveBeenCalledWith(0);
|
|
604
|
+
expect(FakeSocket.instances).toHaveLength(5);
|
|
605
|
+
});
|
|
606
|
+
it("reports only the agent's last 40 stderr lines", async () => {
|
|
607
|
+
const t = await connected();
|
|
608
|
+
t.children[0].stderr.write(Array.from({ length: 45 }, (_, i) => `e${i}`).join("\n") + "\n");
|
|
609
|
+
await flush();
|
|
610
|
+
t.children[0].emit("exit", 1, null);
|
|
611
|
+
const [stderr] = FakeSocket.instances[0].controls();
|
|
612
|
+
expect(stderr.lines).toHaveLength(40);
|
|
613
|
+
expect(stderr.lines[0]).toBe("e5");
|
|
614
|
+
});
|
|
615
|
+
it("survives a broken pipe to a dying agent", async () => {
|
|
616
|
+
const t = await connected();
|
|
617
|
+
t.children[0].stdin.emit("error", new Error("write EPIPE"));
|
|
618
|
+
expect(t.exit).not.toHaveBeenCalled();
|
|
619
|
+
});
|
|
620
|
+
it("a socket that cannot be made is reported and exits 1", async () => {
|
|
621
|
+
class Throwing {
|
|
622
|
+
constructor() {
|
|
623
|
+
throw new SyntaxError("Invalid URL");
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
const t = await start({ WebSocketImpl: Throwing });
|
|
627
|
+
await t.done;
|
|
628
|
+
expect(printed()).toContain("Cannot connect to wss://h/api/acp/v1: Invalid URL");
|
|
629
|
+
expect(t.exit).toHaveBeenCalledWith(1);
|
|
630
|
+
expect(t.children[0].kill).toHaveBeenCalled();
|
|
631
|
+
});
|
|
632
|
+
it("SIGTERM says bye, closes and stops the agent", async () => {
|
|
633
|
+
const t = await connected();
|
|
634
|
+
process.emit("SIGTERM");
|
|
635
|
+
await t.done;
|
|
636
|
+
expect(FakeSocket.instances[0].controls()).toEqual([{ agentrqBridge: "bye" }]);
|
|
637
|
+
expect(FakeSocket.instances[0].closedWith).toBe(1000);
|
|
638
|
+
expect(t.children[0].kill).toHaveBeenCalled();
|
|
639
|
+
expect(t.exit).toHaveBeenCalledWith(0);
|
|
640
|
+
expect(printed()).toContain("Received SIGTERM");
|
|
641
|
+
// Stopped for good: nothing it scheduled runs, and a second signal is a no-op.
|
|
642
|
+
process.emit("SIGINT");
|
|
643
|
+
vi.advanceTimersByTime(120_000);
|
|
644
|
+
expect(t.exit).toHaveBeenCalledTimes(1);
|
|
645
|
+
});
|
|
646
|
+
it("SIGHUP before the socket opens still stops the agent", async () => {
|
|
647
|
+
const t = await start();
|
|
648
|
+
process.emit("SIGHUP");
|
|
649
|
+
await t.done;
|
|
650
|
+
expect(FakeSocket.instances[0].sent).toEqual([]);
|
|
651
|
+
expect(t.children[0].kill).toHaveBeenCalled();
|
|
652
|
+
expect(t.exit).toHaveBeenCalledWith(0);
|
|
653
|
+
});
|
|
654
|
+
it("a full socket pauses the agent's output", async () => {
|
|
655
|
+
const t = await connected();
|
|
656
|
+
const stdout = t.children[0].stdout;
|
|
657
|
+
t.ws().bufferedAmount = 9 * 1024 * 1024;
|
|
658
|
+
stdout.write("big\n");
|
|
659
|
+
await flush();
|
|
660
|
+
expect(stdout.isPaused()).toBe(true);
|
|
661
|
+
t.ws().bufferedAmount = 2 * 1024 * 1024;
|
|
662
|
+
vi.advanceTimersByTime(50);
|
|
663
|
+
expect(stdout.isPaused()).toBe(true);
|
|
664
|
+
t.ws().bufferedAmount = 0;
|
|
665
|
+
vi.advanceTimersByTime(50);
|
|
666
|
+
expect(stdout.isPaused()).toBe(false);
|
|
667
|
+
});
|
|
668
|
+
it("a dropped socket releases a paused agent", async () => {
|
|
669
|
+
const t = await connected();
|
|
670
|
+
t.ws().bufferedAmount = 9 * 1024 * 1024;
|
|
671
|
+
t.children[0].stdout.write("big\n");
|
|
672
|
+
await flush();
|
|
673
|
+
t.ws().drop(1006);
|
|
674
|
+
vi.advanceTimersByTime(50);
|
|
675
|
+
expect(t.children[0].stdout.isPaused()).toBe(false);
|
|
676
|
+
});
|
|
677
|
+
it("ignores events from a socket it already replaced", async () => {
|
|
678
|
+
const t = await connected();
|
|
679
|
+
const old = t.ws();
|
|
680
|
+
old.drop(1006);
|
|
681
|
+
old.receive('{"n":1}');
|
|
682
|
+
old.drop(1006);
|
|
683
|
+
expect(t.children[0].written).toEqual([]);
|
|
684
|
+
vi.advanceTimersByTime(1000);
|
|
685
|
+
expect(FakeSocket.instances).toHaveLength(2);
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
//# sourceMappingURL=native.test.js.map
|