@agentrq/acp-gateway 0.4.1 → 0.4.3

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.
@@ -317,24 +317,82 @@ describe("runNative", () => {
317
317
  await flush();
318
318
  expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":1,"result":{"sessionId":"S2"}}']);
319
319
  });
320
- it("sends a second connection's initialize to an agent that never answered the first", async () => {
320
+ it("coalesces a second connection's initialize while the first is still in flight", async () => {
321
321
  const t = await connected();
322
322
  t.ws().receive('{"jsonrpc":"2.0","id":0,"method":"initialize","params":{}}');
323
323
  await flush();
324
324
  freshReconnect(t);
325
325
  await flush();
326
326
  t.children[0].written.length = 0;
327
+ // A second initialize arrives while the first (id 0) has not been answered yet.
328
+ // It should be held without sending a second initialize to the agent's stdin.
329
+ t.ws().receive('{"jsonrpc":"2.0","id":3,"method":"initialize","params":{}}');
330
+ await flush();
331
+ expect(t.children[0].written).toEqual([]);
332
+ // When the agent answers the in-flight initialize (id 0) with an error, the active
333
+ // connection receives that error for its pending id 3, and a retry is forwarded.
334
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":0,"error":{"code":-32603,"message":"busy"}}\n');
335
+ await flush();
336
+ expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":3,"error":{"code":-32603,"message":"busy"}}']);
337
+ t.ws().receive('{"jsonrpc":"2.0","id":4,"method":"initialize","params":{}}');
338
+ await flush();
339
+ expect(t.children[0].written).toEqual(['{"jsonrpc":"2.0","id":"1:4","method":"initialize","params":{}}\n']);
340
+ // Reconnect again while "1:4" is in flight, and send initialize id 7 on the new connection.
341
+ freshReconnect(t);
342
+ await flush();
343
+ t.children[0].written.length = 0;
344
+ t.ws().receive('{"jsonrpc":"2.0","id":7,"method":"initialize","params":{}}');
345
+ await flush();
346
+ expect(t.children[0].written).toEqual([]);
347
+ // Now the agent answers "1:4" with a result -> active connection gets id 7 answered,
348
+ // and initResult is cached for future connections.
349
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":"1:4","result":{"protocolVersion":1}}\n');
350
+ await flush();
351
+ expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":7,"result":{"protocolVersion":1}}']);
352
+ freshReconnect(t);
353
+ t.ws().receive('{"jsonrpc":"2.0","id":8,"method":"initialize","params":{}}');
354
+ await flush();
355
+ expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":8,"result":{"protocolVersion":1}}']);
356
+ });
357
+ it("remembers an initialize answer that arrives after a fresh reconnect, and ignores an error answer", async () => {
358
+ const t = await connected();
327
359
  t.ws().receive('{"jsonrpc":"2.0","id":0,"method":"initialize","params":{}}');
328
360
  await flush();
329
- expect(t.children[0].written).toEqual(['{"jsonrpc":"2.0","id":"1:0","method":"initialize","params":{}}\n']);
330
- t.children[0].stdout.write('{"jsonrpc":"2.0","id":"1:0","result":{"protocolVersion":1}}\n');
361
+ // Agent answers initialize with an error first — must not be cached as initResult.
362
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":0,"error":{"code":-32603,"message":"busy"}}\n');
331
363
  await flush();
332
- expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":0,"result":{"protocolVersion":1}}']);
333
- // ...and remembers that answer for the next connection.
364
+ // Send another initialize, then reconnect before the agent answers it.
365
+ t.ws().receive('{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}');
366
+ await flush();
367
+ freshReconnect(t);
368
+ await flush();
369
+ // Now the agent answers id 1 from before the reconnect.
370
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":1}}\n');
371
+ await flush();
372
+ t.ws().receive('{"jsonrpc":"2.0","id":9,"method":"initialize","params":{}}');
373
+ await flush();
374
+ expect(t.ws().data()).toContain('{"jsonrpc":"2.0","id":9,"result":{"protocolVersion":1}}');
375
+ });
376
+ it("rewrites params.requestId across epochs for $/cancel_request and elicitation/create", async () => {
377
+ const t = await initialized();
334
378
  freshReconnect(t);
335
- t.ws().receive('{"jsonrpc":"2.0","id":5,"method":"initialize","params":{}}');
336
379
  await flush();
337
- expect(t.ws().data()).toEqual(['{"jsonrpc":"2.0","id":5,"result":{"protocolVersion":1}}']);
380
+ t.children[0].written.length = 0;
381
+ t.ws().receive('{"jsonrpc":"2.0","id":4,"method":"session/prompt","params":{"sessionId":"S1"}}');
382
+ await flush();
383
+ expect(JSON.parse(t.children[0].written[0]).id).toBe("1:4");
384
+ // Agent sends elicitation/create scoped to request "1:4" -> AgentRQ sees requestId 4.
385
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":20,"method":"elicitation/create","params":{"requestId":"1:4","mode":"url","message":"hi","url":"https://x"}}\n');
386
+ await flush();
387
+ expect(JSON.parse(t.ws().data()[0]).params.requestId).toBe(4);
388
+ // AgentRQ sends $/cancel_request for requestId 4 -> agent receives "1:4".
389
+ t.ws().receive('{"jsonrpc":"2.0","method":"$/cancel_request","params":{"requestId":4}}');
390
+ await flush();
391
+ expect(JSON.parse(t.children[0].written[1])).toEqual({
392
+ jsonrpc: "2.0",
393
+ method: "$/cancel_request",
394
+ params: { requestId: "1:4" },
395
+ });
338
396
  });
339
397
  it("carries a task's conversation on in its own session after the agent is kept", async () => {
340
398
  const t = await connected();
@@ -358,18 +416,40 @@ describe("runNative", () => {
358
416
  t.children[0].stdout.write('{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"S3","update":{}}}\n');
359
417
  await flush();
360
418
  expect(t.ws().data().map((f) => JSON.parse(f).params.sessionId)).toEqual(["S2", "S3"]);
361
- // Once AgentRQ closes it, the task starts afresh in whatever session it opens next.
419
+ // Once AgentRQ closes it, the task starts afresh in whatever session it opens next,
420
+ // and the closed session is not cancelled on a later reconnect.
421
+ // Trailing session/update frames emitted before the agent responds to session/close
422
+ // still have their sessionId translated back to S2 until the close response arrives.
362
423
  t.children[0].written.length = 0;
424
+ t.ws().sent.length = 0;
363
425
  t.ws().receive('{"jsonrpc":"2.0","id":5,"method":"session/close","params":{"sessionId":"S2"}}');
364
- t.ws().receive(prompt(6, "S5", "[Task T1] again"));
426
+ t.ws().receive('{"jsonrpc":"2.0","id":6,"method":"session/delete","params":{"sessionId":"S4"}}');
427
+ t.ws().receive(prompt(7, "S5", "[Task T1] again"));
428
+ await flush();
429
+ expect(sent().map((m) => m.params.sessionId)).toEqual(["S1", "S4", "S5"]);
430
+ // Trailing notification on S1 while session/close ("1:5") is still in flight -> mapped to S2.
431
+ t.children[0].stdout.write('{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"S1","update":{"trailing":1}}}\n');
432
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":"1:5","result":{}}\n');
433
+ t.children[0].stdout.write('{"jsonrpc":"2.0","id":"1:6","result":{}}\n');
434
+ // After session/close has completed, S1 is no longer mapped to S2.
435
+ t.children[0].stdout.write('{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"S1","update":{"afterClose":1}}}\n');
365
436
  await flush();
366
- expect(sent().map((m) => m.params.sessionId)).toEqual(["S1", "S5"]);
437
+ expect(t.ws().data().map((f) => JSON.parse(f))).toEqual([
438
+ { jsonrpc: "2.0", method: "session/update", params: { sessionId: "S2", update: { trailing: 1 } } },
439
+ { jsonrpc: "2.0", id: 5, result: {} },
440
+ { jsonrpc: "2.0", id: 6, result: {} },
441
+ { jsonrpc: "2.0", method: "session/update", params: { sessionId: "S1", update: { afterClose: 1 } } },
442
+ ]);
443
+ t.children[0].written.length = 0;
444
+ freshReconnect(t);
445
+ await flush();
446
+ expect(sent().map((m) => m.params.sessionId)).toEqual(["S3", "S5"]);
367
447
  });
368
- it("forgets the agent's requests AgentRQ has answered", async () => {
448
+ it("forgets the agent's requests AgentRQ has answered or the agent has cancelled", async () => {
369
449
  const t = await initialized();
370
450
  t.ws().receive('{"jsonrpc":"2.0","id":7,"result":{"outcome":{"outcome":"selected","optionId":"allow"}}}');
371
451
  t.ws().receive('{"jsonrpc":"2.0","id":8,"result":{"action":"accept"}}');
372
- t.ws().receive('{"jsonrpc":"2.0","id":9,"result":{"content":""}}');
452
+ t.children[0].stdout.write('{"jsonrpc":"2.0","method":"$/cancel_request","params":{"requestId":9}}\n');
373
453
  await flush();
374
454
  t.children[0].written.length = 0;
375
455
  freshReconnect(t);
@@ -949,5 +1029,236 @@ describe("runNative", () => {
949
1029
  await flush();
950
1030
  expect(t.exit).not.toHaveBeenCalled();
951
1031
  });
1032
+ it("prints non-verbose summaries for connection, agent readiness, tasks, permissions, elicitations, and turn completion", async () => {
1033
+ const t = await connected();
1034
+ expect(printed()).toContain("[acp-gateway] Connected to AgentRQ; waiting for tasks...");
1035
+ // 1. Agent initialization summary
1036
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", id: "init-1", method: "initialize", params: {} }));
1037
+ t.children[0].stdout.write(JSON.stringify({
1038
+ jsonrpc: "2.0",
1039
+ id: "init-1",
1040
+ result: { agentInfo: { title: "Gemini CLI", version: "1.2.3" } },
1041
+ }) + "\n");
1042
+ await flush();
1043
+ expect(printed()).toContain("[acp-gateway] Agent ready: Gemini CLI v1.2.3");
1044
+ // 2. Incoming task prompt announcement
1045
+ t.ws().receive(JSON.stringify({
1046
+ jsonrpc: "2.0",
1047
+ id: 10,
1048
+ method: "session/prompt",
1049
+ params: {
1050
+ sessionId: "s-1",
1051
+ prompt: [{ type: "text", text: "[Task 0lzWBUu5TV2]\nShare summaries when not in verbose mode" }],
1052
+ },
1053
+ }));
1054
+ expect(printed()).toContain("[task 0lzWBUu5TV2] Share summaries when not in verbose mode");
1055
+ // 3. Tool call title remembered from session/update + permission request + verdict
1056
+ t.children[0].stdout.write(JSON.stringify({
1057
+ jsonrpc: "2.0",
1058
+ method: "session/update",
1059
+ params: {
1060
+ sessionId: "s-1",
1061
+ update: { sessionUpdate: "tool_call", toolCallId: "tc-1", title: "Run npm test" },
1062
+ },
1063
+ }) + "\n");
1064
+ t.children[0].stdout.write(JSON.stringify({
1065
+ jsonrpc: "2.0",
1066
+ id: "perm-1",
1067
+ method: "session/request_permission",
1068
+ params: {
1069
+ sessionId: "s-1",
1070
+ toolCall: { toolCallId: "tc-1" },
1071
+ options: [{ optionId: "allow-once", kind: "allow_once", name: "Allow" }],
1072
+ },
1073
+ }) + "\n");
1074
+ await flush();
1075
+ expect(printed()).toContain("🔐 ACP Permission requested: Run npm test (ID: tc-1)");
1076
+ expect(printed()).toContain("⌛ Waiting for human approval in the agentrq dashboard...");
1077
+ t.ws().receive(JSON.stringify({
1078
+ jsonrpc: "2.0",
1079
+ id: "perm-1",
1080
+ result: { outcome: { outcome: "selected", optionId: "allow-once" } },
1081
+ }));
1082
+ expect(printed()).toContain("✅ Permission verdict received: allow-once");
1083
+ // Permission request with explicit title, explicit name, no toolCallId, and cancelled outcome
1084
+ t.children[0].stdout.write(JSON.stringify({
1085
+ jsonrpc: "2.0",
1086
+ id: "perm-title",
1087
+ method: "session/request_permission",
1088
+ params: {
1089
+ sessionId: "s-1",
1090
+ toolCall: { title: "Explicit Title" },
1091
+ },
1092
+ }) + "\n");
1093
+ t.children[0].stdout.write(JSON.stringify({
1094
+ jsonrpc: "2.0",
1095
+ id: "perm-2",
1096
+ method: "session/request_permission",
1097
+ params: {
1098
+ sessionId: "s-1",
1099
+ toolCall: { name: "WriteFile" },
1100
+ },
1101
+ }) + "\n");
1102
+ await flush();
1103
+ expect(printed()).toContain("🔐 ACP Permission requested: Explicit Title");
1104
+ expect(printed()).toContain("🔐 ACP Permission requested: WriteFile");
1105
+ t.ws().receive(JSON.stringify({
1106
+ jsonrpc: "2.0",
1107
+ id: "perm-2",
1108
+ result: { outcome: { outcome: "cancelled" } },
1109
+ }));
1110
+ expect(printed()).toContain("✅ Permission verdict received: cancelled");
1111
+ // Permission request with fallback Unknown Tool and response without outcome
1112
+ t.children[0].stdout.write(JSON.stringify({
1113
+ jsonrpc: "2.0",
1114
+ id: "perm-3",
1115
+ method: "session/request_permission",
1116
+ params: { sessionId: "s-1" },
1117
+ }) + "\n");
1118
+ await flush();
1119
+ expect(printed()).toContain("🔐 ACP Permission requested: Unknown Tool");
1120
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", id: "perm-3", result: {} }));
1121
+ // 4. Elicitation request (with explicit mode/message and with defaults)
1122
+ t.children[0].stdout.write(JSON.stringify({
1123
+ jsonrpc: "2.0",
1124
+ id: "elicit-1",
1125
+ method: "elicitation/create",
1126
+ params: { sessionId: "s-1", mode: "url", message: "Authenticate with GitHub" },
1127
+ }) + "\n");
1128
+ t.children[0].stdout.write(JSON.stringify({
1129
+ jsonrpc: "2.0",
1130
+ id: "elicit-2",
1131
+ method: "elicitation/create",
1132
+ params: { sessionId: "s-1" },
1133
+ }) + "\n");
1134
+ await flush();
1135
+ expect(printed()).toContain("[acp] Elicitation requested (mode=url): Authenticate with GitHub");
1136
+ expect(printed()).toContain("[acp] Elicitation requested (mode=form): ");
1137
+ // 5. Turn completion summary
1138
+ t.children[0].stdout.write(JSON.stringify({
1139
+ jsonrpc: "2.0",
1140
+ id: 10,
1141
+ result: { stopReason: "end_turn" },
1142
+ }) + "\n");
1143
+ await flush();
1144
+ expect(printed()).toContain("[task 0lzWBUu5TV2] Finished: end_turn");
1145
+ });
1146
+ it("handles prompt error responses, cancelled prompts, and fallback agent labels", async () => {
1147
+ const t = await start({ agentId: "", acpCmdArgs: [""] });
1148
+ t.ws().open();
1149
+ t.ws().welcome({ connectionId: "AB" });
1150
+ // Initialize with empty agentInfo and empty agentId/acpCmdArgs fallback
1151
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: {} }));
1152
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: 1, result: {} }) + "\n");
1153
+ await flush();
1154
+ expect(printed()).toContain("[acp-gateway] Agent ready");
1155
+ // Prompt that errors with message, code, or unknown
1156
+ t.ws().receive(JSON.stringify({
1157
+ jsonrpc: "2.0",
1158
+ id: 20,
1159
+ method: "session/prompt",
1160
+ params: { sessionId: "s-1", prompt: [{ type: "text", text: "plain task" }] },
1161
+ }));
1162
+ t.children[0].stdout.write(JSON.stringify({
1163
+ jsonrpc: "2.0",
1164
+ id: 20,
1165
+ error: { code: -32603, message: "rate limited" },
1166
+ }) + "\n");
1167
+ t.ws().receive(JSON.stringify({
1168
+ jsonrpc: "2.0",
1169
+ id: 21,
1170
+ method: "session/prompt",
1171
+ params: { sessionId: "s-1", prompt: [{ type: "text", text: "second task" }] },
1172
+ }));
1173
+ t.children[0].stdout.write(JSON.stringify({
1174
+ jsonrpc: "2.0",
1175
+ id: 21,
1176
+ error: { code: -32001 },
1177
+ }) + "\n");
1178
+ t.ws().receive(JSON.stringify({
1179
+ jsonrpc: "2.0",
1180
+ id: 22,
1181
+ method: "session/prompt",
1182
+ params: { sessionId: "s-1", prompt: [{ type: "text", text: "third task" }] },
1183
+ }));
1184
+ t.children[0].stdout.write(JSON.stringify({
1185
+ jsonrpc: "2.0",
1186
+ id: 22,
1187
+ error: {},
1188
+ }) + "\n");
1189
+ // Prompt that returns neither stopReason nor error, or is cancelled via $/cancel_request
1190
+ t.ws().receive(JSON.stringify({
1191
+ jsonrpc: "2.0",
1192
+ id: 23,
1193
+ method: "session/prompt",
1194
+ params: { sessionId: "s-1", prompt: [{ type: "text", text: "fourth task" }] },
1195
+ }));
1196
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: 23, result: {} }) + "\n");
1197
+ t.ws().receive(JSON.stringify({
1198
+ jsonrpc: "2.0",
1199
+ id: 24,
1200
+ method: "session/prompt",
1201
+ params: { sessionId: "s-1", prompt: [{ type: "text", text: "cancelled task" }] },
1202
+ }));
1203
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", method: "$/cancel_request", params: { requestId: 24 } }));
1204
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: 24, result: { stopReason: "cancelled" } }) + "\n");
1205
+ await flush();
1206
+ expect(printed()).toContain("[task] Finished: error (rate limited)");
1207
+ expect(printed()).toContain("[task] Finished: error (-32001)");
1208
+ expect(printed()).toContain("[task] Finished: error (unknown)");
1209
+ expect(printed()).not.toContain("Finished: cancelled");
1210
+ });
1211
+ it("streams the agent's stderr during authenticate and relays a loopback OAuth redirect when interactive", async () => {
1212
+ const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
1213
+ let resolveAsk;
1214
+ const askUntil = vi.fn((_q, signal) => new Promise((resolve, reject) => {
1215
+ resolveAsk = resolve;
1216
+ signal.addEventListener("abort", () => reject(new Error("aborted")));
1217
+ }));
1218
+ const t = await start({ askUntil });
1219
+ t.ws().open();
1220
+ t.ws().welcome({ connectionId: "AB" });
1221
+ // AgentRQ sends an authenticate request
1222
+ t.ws().receive(JSON.stringify({
1223
+ jsonrpc: "2.0",
1224
+ id: "auth-1",
1225
+ method: "authenticate",
1226
+ params: { methodId: "google-login" },
1227
+ }));
1228
+ expect(printed()).toContain("[auth] The agent requires authentication (google-login); waiting for sign-in...");
1229
+ // While authenticate is in flight, agent stderr is forwarded even without --verbose
1230
+ const oauthLine = "Open the following link: https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=http%3A%2F%2F127.0.0.1%3A43123%2Foauth-callback\n";
1231
+ t.children[0].stderr.write(oauthLine);
1232
+ await flush();
1233
+ expect(write).toHaveBeenCalledWith(oauthLine);
1234
+ expect(printed()).toContain("Signing in from a browser on another computer?");
1235
+ expect(askUntil).toHaveBeenCalledTimes(1);
1236
+ // Additional stderr chunk while already relaying does not start a second relay
1237
+ t.children[0].stderr.write("still waiting...\n");
1238
+ await flush();
1239
+ expect(askUntil).toHaveBeenCalledTimes(1);
1240
+ // Agent finishes authentication on its own (or after redirect), aborting askUntil
1241
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: "auth-1", result: {} }) + "\n");
1242
+ await flush();
1243
+ expect(printed()).toContain('[auth] Logged in with "google-login".');
1244
+ // Once authenticate is complete, stderr is no longer forwarded in non-verbose mode
1245
+ write.mockClear();
1246
+ t.children[0].stderr.write("quiet again\n");
1247
+ await flush();
1248
+ expect(write).not.toHaveBeenCalled();
1249
+ // Authenticate with default methodId and error responses (message, code, unknown)
1250
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", id: "auth-2", method: "authenticate", params: {} }));
1251
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", id: "auth-3", method: "authenticate", params: { methodId: "oauth" } }));
1252
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: "auth-2", error: { code: -32000, message: "denied" } }) + "\n");
1253
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: "auth-3", error: { code: -32001 } }) + "\n");
1254
+ t.ws().receive(JSON.stringify({ jsonrpc: "2.0", id: "auth-4", method: "authenticate", params: { methodId: "oauth" } }));
1255
+ t.children[0].stdout.write(JSON.stringify({ jsonrpc: "2.0", id: "auth-4", error: {} }) + "\n");
1256
+ await flush();
1257
+ expect(printed()).toContain("[auth] Authentication failed (default): denied");
1258
+ expect(printed()).toContain("[auth] Authentication failed (oauth): -32001");
1259
+ expect(printed()).toContain("[auth] Authentication failed (oauth): unknown");
1260
+ write.mockRestore();
1261
+ void resolveAsk;
1262
+ });
952
1263
  });
953
1264
  //# sourceMappingURL=native.test.js.map