@agentrq/acp-gateway 0.3.1 → 0.4.0
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 +59 -169
- package/dist/__tests__/agentInfo.test.js +2 -31
- package/dist/__tests__/agentInfo.test.js.map +1 -1
- package/dist/__tests__/concurrency.test.js +1 -74
- package/dist/__tests__/concurrency.test.js.map +1 -1
- package/dist/__tests__/index.test.js +190 -2672
- package/dist/__tests__/index.test.js.map +1 -1
- package/dist/__tests__/localClient.test.js +100 -0
- package/dist/__tests__/localClient.test.js.map +1 -0
- package/dist/__tests__/log.test.js +1 -42
- package/dist/__tests__/log.test.js.map +1 -1
- package/dist/__tests__/mcpServers.test.js +62 -0
- package/dist/__tests__/mcpServers.test.js.map +1 -0
- package/dist/__tests__/models.test.js +2 -18
- package/dist/__tests__/models.test.js.map +1 -1
- package/dist/__tests__/native.test.js +294 -29
- package/dist/__tests__/native.test.js.map +1 -1
- package/dist/__tests__/nativeFrames.test.js +132 -4
- package/dist/__tests__/nativeFrames.test.js.map +1 -1
- package/dist/__tests__/nativeHeaders.test.js +34 -0
- package/dist/__tests__/nativeHeaders.test.js.map +1 -1
- package/dist/agentInfo.js +0 -31
- package/dist/agentInfo.js.map +1 -1
- package/dist/concurrency.js +6 -64
- package/dist/concurrency.js.map +1 -1
- package/dist/index.js +114 -1729
- package/dist/index.js.map +1 -1
- package/dist/localClient.js +91 -0
- package/dist/localClient.js.map +1 -0
- package/dist/log.js +0 -22
- package/dist/log.js.map +1 -1
- package/dist/mcpServers.js +60 -0
- package/dist/mcpServers.js.map +1 -0
- package/dist/models.js +0 -10
- package/dist/models.js.map +1 -1
- package/dist/native.js +243 -30
- package/dist/native.js.map +1 -1
- package/dist/nativeFrames.js +114 -12
- package/dist/nativeFrames.js.map +1 -1
- package/package.json +4 -6
- package/dist/__tests__/acpClient.test.js +0 -2346
- package/dist/__tests__/acpClient.test.js.map +0 -1
- package/dist/__tests__/collections.test.js +0 -116
- package/dist/__tests__/collections.test.js.map +0 -1
- package/dist/__tests__/commands.test.js +0 -92
- package/dist/__tests__/commands.test.js.map +0 -1
- package/dist/__tests__/mcpClient.test.js +0 -558
- package/dist/__tests__/mcpClient.test.js.map +0 -1
- package/dist/__tests__/taskIdentity.test.js +0 -63
- package/dist/__tests__/taskIdentity.test.js.map +0 -1
- package/dist/__tests__/telemetry.test.js +0 -151
- package/dist/__tests__/telemetry.test.js.map +0 -1
- package/dist/acpClient.js +0 -1112
- package/dist/acpClient.js.map +0 -1
- package/dist/collections.js +0 -105
- package/dist/collections.js.map +0 -1
- package/dist/commands.js +0 -60
- package/dist/commands.js.map +0 -1
- package/dist/mcpClient.js +0 -361
- package/dist/mcpClient.js.map +0 -1
- package/dist/taskIdentity.js +0 -41
- package/dist/taskIdentity.js.map +0 -1
- package/dist/telemetry.js +0 -106
- package/dist/telemetry.js.map +0 -1
|
@@ -1,2346 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
-
import { setVerbose } from "../log.js";
|
|
3
|
-
import { EventEmitter } from "node:events";
|
|
4
|
-
import { AgentRQACPClient, lastModelsForSession } from "../acpClient.js";
|
|
5
|
-
import * as fs from "node:fs/promises";
|
|
6
|
-
import * as path from "node:path";
|
|
7
|
-
// Mock dependencies
|
|
8
|
-
vi.mock("node:fs/promises");
|
|
9
|
-
vi.mock("node:path");
|
|
10
|
-
describe("AgentRQACPClient", () => {
|
|
11
|
-
afterEach(() => setVerbose(false));
|
|
12
|
-
let mcpBridge;
|
|
13
|
-
let client;
|
|
14
|
-
/**
|
|
15
|
-
* Answers the tool call that is waiting, the way agentrq does: by echoing
|
|
16
|
-
* back the request id the gateway actually sent.
|
|
17
|
-
*/
|
|
18
|
-
function answerWith(behavior) {
|
|
19
|
-
setTimeout(() => {
|
|
20
|
-
const sent = mcpBridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
21
|
-
mcpBridge.emit("verdict", { requestId: sent?.request_id, behavior });
|
|
22
|
-
}, 10);
|
|
23
|
-
}
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
vi.clearAllMocks();
|
|
26
|
-
// A real emitter: the client registers one shared verdict listener when it
|
|
27
|
-
// is constructed, so a mocked `on` would never see a verdict at all.
|
|
28
|
-
mcpBridge = Object.assign(new EventEmitter(), {
|
|
29
|
-
getSessionId: vi.fn().mockReturnValue("test-session"),
|
|
30
|
-
sendNotification: vi.fn().mockResolvedValue(undefined),
|
|
31
|
-
callTool: vi.fn(),
|
|
32
|
-
});
|
|
33
|
-
client = new AgentRQACPClient(mcpBridge);
|
|
34
|
-
});
|
|
35
|
-
describe("requestPermission", () => {
|
|
36
|
-
it("should send a notification and wait for a verdict", async () => {
|
|
37
|
-
const params = {
|
|
38
|
-
toolCall: {
|
|
39
|
-
toolCallId: "req-123",
|
|
40
|
-
title: "Test Tool",
|
|
41
|
-
rawInput: { foo: "bar" },
|
|
42
|
-
},
|
|
43
|
-
options: [
|
|
44
|
-
{ optionId: "opt-1", kind: "allow", name: "Allow Once" },
|
|
45
|
-
{ optionId: "opt-2", kind: "deny", name: "Deny" },
|
|
46
|
-
],
|
|
47
|
-
};
|
|
48
|
-
answerWith("allow");
|
|
49
|
-
const response = await client.requestPermission(params);
|
|
50
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
51
|
-
});
|
|
52
|
-
it("should cancel (not throw) when sendNotification rejects, e.g. on network outage", async () => {
|
|
53
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
54
|
-
mcpBridge.sendNotification.mockRejectedValue(new Error("MCP not connected after 10s timeout"));
|
|
55
|
-
const params = {
|
|
56
|
-
toolCall: {
|
|
57
|
-
toolCallId: "req-123",
|
|
58
|
-
title: "Test Tool",
|
|
59
|
-
rawInput: { foo: "bar" },
|
|
60
|
-
},
|
|
61
|
-
options: [
|
|
62
|
-
{ optionId: "opt-1", kind: "allow", name: "Allow Once" },
|
|
63
|
-
{ optionId: "opt-2", kind: "deny", name: "Deny" },
|
|
64
|
-
],
|
|
65
|
-
};
|
|
66
|
-
// Must resolve with a cancelled outcome rather than reject — a rejection
|
|
67
|
-
// here would surface as an unhandled rejection and crash the gateway.
|
|
68
|
-
const response = await client.requestPermission(params);
|
|
69
|
-
expect(response.outcome.outcome).toBe("cancelled");
|
|
70
|
-
// Nothing may be left waiting on a verdict that will never come.
|
|
71
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
72
|
-
consoleSpy.mockRestore();
|
|
73
|
-
});
|
|
74
|
-
it("should include task_id in the payload when available", async () => {
|
|
75
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
76
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
77
|
-
const params = {
|
|
78
|
-
sessionId: "sess-1",
|
|
79
|
-
toolCall: {
|
|
80
|
-
toolCallId: "req-123",
|
|
81
|
-
title: "Test Tool",
|
|
82
|
-
rawInput: { foo: "bar" },
|
|
83
|
-
},
|
|
84
|
-
options: [
|
|
85
|
-
{ optionId: "opt-1", kind: "allow", name: "Allow Once" },
|
|
86
|
-
{ optionId: "opt-2", kind: "deny", name: "Deny" },
|
|
87
|
-
],
|
|
88
|
-
};
|
|
89
|
-
answerWith("allow");
|
|
90
|
-
await clientWithTaskId.requestPermission(params);
|
|
91
|
-
expect(getTaskId).toHaveBeenCalledWith("sess-1");
|
|
92
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/permission_request", expect.objectContaining({
|
|
93
|
-
task_id: "task-123",
|
|
94
|
-
}));
|
|
95
|
-
});
|
|
96
|
-
it("should auto-allow tools matching agentrq-<11 chars> pattern in title", async () => {
|
|
97
|
-
const params = {
|
|
98
|
-
toolCall: {
|
|
99
|
-
toolCallId: "req-123",
|
|
100
|
-
title: "updateTaskStatus (agentrq-0aleR6CbZBp MCP Server)",
|
|
101
|
-
},
|
|
102
|
-
options: [
|
|
103
|
-
{ optionId: "opt-1", kind: "allow", name: "Allow" },
|
|
104
|
-
{ optionId: "opt-2", kind: "deny", name: "Deny" },
|
|
105
|
-
],
|
|
106
|
-
};
|
|
107
|
-
const response = await client.requestPermission(params);
|
|
108
|
-
expect(mcpBridge.sendNotification).not.toHaveBeenCalled();
|
|
109
|
-
expect(response.outcome.outcome).toBe("selected");
|
|
110
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
111
|
-
});
|
|
112
|
-
describe("tools served by the configured workspace server", () => {
|
|
113
|
-
/** A client bridged to a workspace MCP server configured under `name`. */
|
|
114
|
-
const clientFor = (name) => {
|
|
115
|
-
const bridge = Object.assign(new EventEmitter(), {
|
|
116
|
-
getSessionId: vi.fn().mockReturnValue("test-session"),
|
|
117
|
-
getServerName: vi.fn().mockReturnValue(name),
|
|
118
|
-
// Unasked by default, so the built-in floor is what applies.
|
|
119
|
-
getAdvertisedTools: vi.fn().mockReturnValue(undefined),
|
|
120
|
-
sendNotification: vi.fn().mockResolvedValue(undefined),
|
|
121
|
-
callTool: vi.fn(),
|
|
122
|
-
});
|
|
123
|
-
return {
|
|
124
|
-
bridge,
|
|
125
|
-
client: new AgentRQACPClient(bridge),
|
|
126
|
-
};
|
|
127
|
-
};
|
|
128
|
-
const ask = (title) => ({
|
|
129
|
-
toolCall: { toolCallId: "req-1", title },
|
|
130
|
-
options: [
|
|
131
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
132
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
133
|
-
],
|
|
134
|
-
});
|
|
135
|
-
// The server this repo is configured with: "agentrq-" plus a word, which
|
|
136
|
-
// the bare-workspace-id pattern never matched, so every workspace tool
|
|
137
|
-
// was going to the human for approval.
|
|
138
|
-
it.each([
|
|
139
|
-
"saveMemory (agentrq-workspace MCP Server)",
|
|
140
|
-
"mcp__agentrq-workspace__loadMemory",
|
|
141
|
-
"mcp.agentrq-workspace.deleteMemory",
|
|
142
|
-
"reply (agentrq-workspace MCP Server)",
|
|
143
|
-
"mcp__agentrq-workspace__task",
|
|
144
|
-
"mcp__agentrq-workspace__memory",
|
|
145
|
-
"mcp__agentrq-workspace__skills",
|
|
146
|
-
"mcp__agentrq-workspace__workspace",
|
|
147
|
-
"mcp__agentrq-workspace__siteTools",
|
|
148
|
-
"mcp__agentrq-workspace__pano",
|
|
149
|
-
"mcp__agentrq-workspace__help",
|
|
150
|
-
])("auto-allows %s", async (title) => {
|
|
151
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
152
|
-
const response = await c.requestPermission(ask(title));
|
|
153
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
154
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
155
|
-
});
|
|
156
|
-
it("auto-allows the memory tools on a server named after a bare workspace id", async () => {
|
|
157
|
-
const { bridge, client: c } = clientFor("agentrq-0an2BXTfpGj");
|
|
158
|
-
const response = await c.requestPermission(ask("mcp__agentrq-0an2BXTfpGj__saveMemory"));
|
|
159
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
160
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
161
|
-
});
|
|
162
|
-
it("still asks the human about another server's tools", async () => {
|
|
163
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
164
|
-
setTimeout(() => {
|
|
165
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
166
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
167
|
-
}, 10);
|
|
168
|
-
await c.requestPermission(ask("mcp__github__create_issue"));
|
|
169
|
-
expect(bridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: "mcp__github__create_issue" }));
|
|
170
|
-
});
|
|
171
|
-
// A path that happens to sit under a directory of the same name is not a
|
|
172
|
-
// call to the workspace, and must not be approved on the human's behalf.
|
|
173
|
-
it("still asks the human about a tool that merely mentions the server name", async () => {
|
|
174
|
-
const { bridge, client: c } = clientFor("agentrq");
|
|
175
|
-
const title = "Read (/src/github.com/agentrq/acp-gateway/src/index.ts)";
|
|
176
|
-
setTimeout(() => {
|
|
177
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
178
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
179
|
-
}, 10);
|
|
180
|
-
await c.requestPermission(ask(title));
|
|
181
|
-
expect(bridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: title }));
|
|
182
|
-
});
|
|
183
|
-
// The workspace is free to rename what it hands out — a slug instead of
|
|
184
|
-
// a bare id, say — without waiting on a release of the gateway.
|
|
185
|
-
it.each([
|
|
186
|
-
"mcp__agentrq-my-team-slug__saveMemory",
|
|
187
|
-
"mcp__agentrq__loadMemory",
|
|
188
|
-
"deleteMemory (agentrq-workspace-0an2BXTfpGj MCP Server)",
|
|
189
|
-
])("auto-allows an agentrq server named some other way: %s", async (title) => {
|
|
190
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
191
|
-
const response = await c.requestPermission(ask(title));
|
|
192
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
193
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
194
|
-
});
|
|
195
|
-
// Being generous about the name is only safe because the name alone
|
|
196
|
-
// never grants approval: an agentrq-looking server cannot have its whole
|
|
197
|
-
// surface waved through.
|
|
198
|
-
it("still asks the human about a tool the workspace does not advertise", async () => {
|
|
199
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
200
|
-
setTimeout(() => {
|
|
201
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
202
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
203
|
-
}, 10);
|
|
204
|
-
await c.requestPermission(ask("mcp__agentrq-workspace__deleteRepository"));
|
|
205
|
-
expect(bridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
|
|
206
|
-
tool_name: "mcp__agentrq-workspace__deleteRepository",
|
|
207
|
-
}));
|
|
208
|
-
});
|
|
209
|
-
// What the server says it has beats any built-in list, so a tool added
|
|
210
|
-
// to the workspace needs no change here.
|
|
211
|
-
it("trusts the tools the server says it advertises", async () => {
|
|
212
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
213
|
-
bridge.getAdvertisedTools.mockReturnValue(new Set(["reply", "summariseWorkspace"]));
|
|
214
|
-
const added = await c.requestPermission(ask("mcp__agentrq-workspace__summariseWorkspace"));
|
|
215
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
216
|
-
expect(added.outcome.optionId).toBe("opt-1");
|
|
217
|
-
// And a tool it no longer lists goes back to the human.
|
|
218
|
-
setTimeout(() => {
|
|
219
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
220
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
221
|
-
}, 10);
|
|
222
|
-
await c.requestPermission(ask("mcp__agentrq-workspace__saveMemory"));
|
|
223
|
-
expect(bridge.sendNotification).toHaveBeenCalled();
|
|
224
|
-
});
|
|
225
|
-
it("compares a server name with punctuation in it literally", async () => {
|
|
226
|
-
const { bridge, client: c } = clientFor("agentrq.workspace");
|
|
227
|
-
const allowed = await c.requestPermission(ask("mcp__agentrq.workspace__saveMemory"));
|
|
228
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
229
|
-
expect(allowed.outcome.optionId).toBe("opt-1");
|
|
230
|
-
// `agentrqXworkspace` is neither the configured name nor an
|
|
231
|
-
// agentrq-prefixed one, so it is somebody else's server.
|
|
232
|
-
setTimeout(() => {
|
|
233
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
234
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
235
|
-
}, 10);
|
|
236
|
-
await c.requestPermission(ask("mcp__agentrqXworkspace__saveMemory"));
|
|
237
|
-
expect(bridge.sendNotification).toHaveBeenCalled();
|
|
238
|
-
});
|
|
239
|
-
// A title that echoes its arguments can carry an agentrq-looking path
|
|
240
|
-
// without being a call to agentrq at all.
|
|
241
|
-
it("still asks the human when the server name only appears in an argument", async () => {
|
|
242
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
243
|
-
const title = "mcp__filesystem__read_file (/home/me/agentrq-notes/x.md)";
|
|
244
|
-
setTimeout(() => {
|
|
245
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
246
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
247
|
-
}, 10);
|
|
248
|
-
await c.requestPermission(ask(title));
|
|
249
|
-
expect(bridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: title }));
|
|
250
|
-
});
|
|
251
|
-
// Some agents announce the call with no `mcp` prefix at all, leaving a
|
|
252
|
-
// title with nothing to anchor the split on but the names themselves.
|
|
253
|
-
// Every one of these used to go to the human.
|
|
254
|
-
it.each([
|
|
255
|
-
"agentrq-workspace_updateTaskStatus",
|
|
256
|
-
"agentrq-workspace__reply",
|
|
257
|
-
"agentrq-workspace.loadMemory",
|
|
258
|
-
"agentrq-workspace-saveMemory",
|
|
259
|
-
])("auto-allows a bare title: %s", async (title) => {
|
|
260
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
261
|
-
const response = await c.requestPermission(ask(title));
|
|
262
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
263
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
264
|
-
});
|
|
265
|
-
// The split is guessed, so both halves have to confirm it. Neither a
|
|
266
|
-
// tool the workspace does not advertise nor another server's tool may
|
|
267
|
-
// ride in on a division that merely happens to be possible.
|
|
268
|
-
it.each([
|
|
269
|
-
"agentrq-workspace_deleteRepository",
|
|
270
|
-
"github_create_issue",
|
|
271
|
-
"write_file",
|
|
272
|
-
"_reply",
|
|
273
|
-
"saveMemory_",
|
|
274
|
-
])("still asks the human about a bare title: %s", async (title) => {
|
|
275
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
276
|
-
setTimeout(() => {
|
|
277
|
-
const sent = bridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
278
|
-
bridge.emit("verdict", { requestId: sent?.request_id, behavior: "allow" });
|
|
279
|
-
}, 10);
|
|
280
|
-
await c.requestPermission(ask(title));
|
|
281
|
-
expect(bridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: title }));
|
|
282
|
-
});
|
|
283
|
-
it("reads a bare title against the tools the server advertises", async () => {
|
|
284
|
-
const { bridge, client: c } = clientFor("agentrq-workspace");
|
|
285
|
-
bridge.getAdvertisedTools.mockReturnValue(new Set(["summariseWorkspace"]));
|
|
286
|
-
const response = await c.requestPermission(ask("agentrq-workspace_summariseWorkspace"));
|
|
287
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
288
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
289
|
-
});
|
|
290
|
-
it("falls back to the bare-workspace-id pattern when the name is unavailable", async () => {
|
|
291
|
-
const bridge = Object.assign(new EventEmitter(), {
|
|
292
|
-
getSessionId: vi.fn().mockReturnValue("test-session"),
|
|
293
|
-
sendNotification: vi.fn().mockResolvedValue(undefined),
|
|
294
|
-
callTool: vi.fn(),
|
|
295
|
-
});
|
|
296
|
-
const c = new AgentRQACPClient(bridge);
|
|
297
|
-
const response = await c.requestPermission(ask("saveMemory (agentrq-0an2BXTfpGj MCP Server)"));
|
|
298
|
-
expect(bridge.sendNotification).not.toHaveBeenCalled();
|
|
299
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
300
|
-
});
|
|
301
|
-
it("skips a duplicate reply the agent sent through the configured server", async () => {
|
|
302
|
-
const { bridge } = clientFor("agentrq-workspace");
|
|
303
|
-
bridge.callTool.mockResolvedValue({ isError: false, content: [] });
|
|
304
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
305
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
306
|
-
const withTask = new AgentRQACPClient(bridge, () => "task-123");
|
|
307
|
-
await withTask.sessionUpdate({
|
|
308
|
-
sessionId: "sess-1",
|
|
309
|
-
update: {
|
|
310
|
-
sessionUpdate: "agent_message_chunk",
|
|
311
|
-
content: { type: "text", text: "Hello world" },
|
|
312
|
-
},
|
|
313
|
-
});
|
|
314
|
-
await withTask.sessionUpdate({
|
|
315
|
-
sessionId: "sess-1",
|
|
316
|
-
update: {
|
|
317
|
-
sessionUpdate: "tool_call",
|
|
318
|
-
title: "reply (agentrq-workspace MCP Server)",
|
|
319
|
-
status: "completed",
|
|
320
|
-
toolCallId: "tc-1",
|
|
321
|
-
rawInput: { chatId: "task-123", text: "Hello world" },
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
await withTask.flushReply("sess-1");
|
|
325
|
-
expect(bridge.callTool).not.toHaveBeenCalledWith("reply", expect.anything());
|
|
326
|
-
consoleSpy.mockRestore();
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
it("should handle missing tool title", async () => {
|
|
330
|
-
const consoleSpy = vi
|
|
331
|
-
.spyOn(console, "error")
|
|
332
|
-
.mockImplementation(() => { });
|
|
333
|
-
const params = {
|
|
334
|
-
toolCall: { toolCallId: "req-123" },
|
|
335
|
-
options: [{ optionId: "opt-1", kind: "allow", name: "Allow" }],
|
|
336
|
-
};
|
|
337
|
-
answerWith("allow");
|
|
338
|
-
const response = await client.requestPermission(params);
|
|
339
|
-
// Permission matching still works based on behavior, independent of title presence
|
|
340
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
341
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Unknown Tool"));
|
|
342
|
-
consoleSpy.mockRestore();
|
|
343
|
-
});
|
|
344
|
-
it("should handle missing rawInput and missing session ID", async () => {
|
|
345
|
-
setVerbose(true);
|
|
346
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
347
|
-
mcpBridge.getSessionId.mockReturnValue(undefined);
|
|
348
|
-
const params = {
|
|
349
|
-
toolCall: { toolCallId: "req-123", title: "Test Tool" },
|
|
350
|
-
options: [{ optionId: "opt-1", kind: "allow", name: "Allow" }],
|
|
351
|
-
};
|
|
352
|
-
answerWith("allow");
|
|
353
|
-
const response = await client.requestPermission(params);
|
|
354
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
355
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Bridge Session ID: unknown"));
|
|
356
|
-
// Verify payload had empty object for rawInput
|
|
357
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ input_preview: "{}" }));
|
|
358
|
-
consoleSpy.mockRestore();
|
|
359
|
-
});
|
|
360
|
-
// codex-acp emits a `tool_call` session update naming the MCP tool, then
|
|
361
|
-
// requests permission for that same toolCallId with no title and no
|
|
362
|
-
// rawInput. Without correlating the two, agentrq's own calls reach the
|
|
363
|
-
// human as "Unknown Tool" instead of being auto-allowed.
|
|
364
|
-
it("should auto-allow an agentrq MCP call whose permission request carries no title", async () => {
|
|
365
|
-
await client.sessionUpdate({
|
|
366
|
-
sessionId: "sess-1",
|
|
367
|
-
update: {
|
|
368
|
-
sessionUpdate: "tool_call",
|
|
369
|
-
toolCallId: "call_QDSxAGK3Qagv",
|
|
370
|
-
title: "mcp.agentrq-0cHiaZsOGMj.updateTaskStatus",
|
|
371
|
-
status: "pending",
|
|
372
|
-
rawInput: { server: "agentrq-0cHiaZsOGMj", tool: "updateTaskStatus", arguments: {} },
|
|
373
|
-
},
|
|
374
|
-
});
|
|
375
|
-
const params = {
|
|
376
|
-
sessionId: "sess-1",
|
|
377
|
-
toolCall: { toolCallId: "call_QDSxAGK3Qagv", kind: "execute", status: "pending" },
|
|
378
|
-
options: [
|
|
379
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
380
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
381
|
-
],
|
|
382
|
-
};
|
|
383
|
-
const response = await client.requestPermission(params);
|
|
384
|
-
expect(mcpBridge.sendNotification).not.toHaveBeenCalled();
|
|
385
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
386
|
-
});
|
|
387
|
-
it("should recover the title and input from an earlier tool_call_update", async () => {
|
|
388
|
-
await client.sessionUpdate({
|
|
389
|
-
sessionId: "sess-1",
|
|
390
|
-
update: {
|
|
391
|
-
sessionUpdate: "tool_call_update",
|
|
392
|
-
toolCallId: "call_abc",
|
|
393
|
-
title: "mcp.other-server.doThing",
|
|
394
|
-
status: "pending",
|
|
395
|
-
rawInput: { server: "other-server", tool: "doThing" },
|
|
396
|
-
},
|
|
397
|
-
});
|
|
398
|
-
const params = {
|
|
399
|
-
sessionId: "sess-1",
|
|
400
|
-
toolCall: { toolCallId: "call_abc", kind: "execute", status: "pending" },
|
|
401
|
-
options: [
|
|
402
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
403
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
404
|
-
],
|
|
405
|
-
};
|
|
406
|
-
answerWith("allow");
|
|
407
|
-
await client.requestPermission(params);
|
|
408
|
-
// A non-agentrq tool still goes to the human, but now with a meaningful
|
|
409
|
-
// name and input rather than "Unknown Tool" / "{}".
|
|
410
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
|
|
411
|
-
tool_name: "mcp.other-server.doThing",
|
|
412
|
-
input_preview: JSON.stringify({ server: "other-server", tool: "doThing" }),
|
|
413
|
-
}));
|
|
414
|
-
});
|
|
415
|
-
it("should prefer the permission request's own title over the remembered one", async () => {
|
|
416
|
-
await client.sessionUpdate({
|
|
417
|
-
sessionId: "sess-1",
|
|
418
|
-
update: {
|
|
419
|
-
sessionUpdate: "tool_call",
|
|
420
|
-
toolCallId: "call_cmd",
|
|
421
|
-
title: "stale title",
|
|
422
|
-
status: "pending",
|
|
423
|
-
rawInput: { command: "stale" },
|
|
424
|
-
},
|
|
425
|
-
});
|
|
426
|
-
const params = {
|
|
427
|
-
sessionId: "sess-1",
|
|
428
|
-
toolCall: {
|
|
429
|
-
toolCallId: "call_cmd",
|
|
430
|
-
title: "Run command",
|
|
431
|
-
rawInput: { command: "rm index" },
|
|
432
|
-
},
|
|
433
|
-
options: [
|
|
434
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
435
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
436
|
-
],
|
|
437
|
-
};
|
|
438
|
-
answerWith("allow");
|
|
439
|
-
await client.requestPermission(params);
|
|
440
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
|
|
441
|
-
tool_name: "Run command",
|
|
442
|
-
input_preview: JSON.stringify({ command: "rm index" }),
|
|
443
|
-
}));
|
|
444
|
-
});
|
|
445
|
-
it("should still report Unknown Tool when nothing was ever recorded", async () => {
|
|
446
|
-
const params = {
|
|
447
|
-
sessionId: "sess-1",
|
|
448
|
-
toolCall: { toolCallId: "call_unseen", kind: "execute", status: "pending" },
|
|
449
|
-
options: [
|
|
450
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
451
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
452
|
-
],
|
|
453
|
-
};
|
|
454
|
-
answerWith("allow");
|
|
455
|
-
await client.requestPermission(params);
|
|
456
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: "Unknown Tool", input_preview: "{}" }));
|
|
457
|
-
});
|
|
458
|
-
it("should forget a tool call once it has completed, so the map does not grow unbounded", async () => {
|
|
459
|
-
const record = { sessionId: "sess-1", toolCallId: "call_done", title: "mcp.agentrq-0cHiaZsOGMj.reply" };
|
|
460
|
-
await client.sessionUpdate({
|
|
461
|
-
sessionId: "sess-1",
|
|
462
|
-
update: { ...record, sessionUpdate: "tool_call", status: "pending" },
|
|
463
|
-
});
|
|
464
|
-
await client.sessionUpdate({
|
|
465
|
-
sessionId: "sess-1",
|
|
466
|
-
update: { ...record, sessionUpdate: "tool_call_update", status: "completed" },
|
|
467
|
-
});
|
|
468
|
-
const params = {
|
|
469
|
-
sessionId: "sess-1",
|
|
470
|
-
toolCall: { toolCallId: "call_done", kind: "execute", status: "pending" },
|
|
471
|
-
options: [
|
|
472
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
473
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
474
|
-
],
|
|
475
|
-
};
|
|
476
|
-
answerWith("allow");
|
|
477
|
-
await client.requestPermission(params);
|
|
478
|
-
// Entry was dropped on completion, so it is no longer auto-allowed by
|
|
479
|
-
// the remembered title — it goes to the human as an unknown tool.
|
|
480
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: "Unknown Tool" }));
|
|
481
|
-
});
|
|
482
|
-
it("should not retain details for a consumed permission request", async () => {
|
|
483
|
-
await client.sessionUpdate({
|
|
484
|
-
sessionId: "sess-1",
|
|
485
|
-
update: {
|
|
486
|
-
sessionUpdate: "tool_call",
|
|
487
|
-
toolCallId: "call_once",
|
|
488
|
-
title: "mcp.agentrq-0cHiaZsOGMj.reply",
|
|
489
|
-
status: "pending",
|
|
490
|
-
},
|
|
491
|
-
});
|
|
492
|
-
const params = {
|
|
493
|
-
sessionId: "sess-1",
|
|
494
|
-
toolCall: { toolCallId: "call_once", kind: "execute", status: "pending" },
|
|
495
|
-
options: [
|
|
496
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
497
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
498
|
-
],
|
|
499
|
-
};
|
|
500
|
-
// First request is auto-allowed from the remembered agentrq title.
|
|
501
|
-
const first = await client.requestPermission(params);
|
|
502
|
-
expect(first.outcome.optionId).toBe("opt-1");
|
|
503
|
-
expect(mcpBridge.sendNotification).not.toHaveBeenCalled();
|
|
504
|
-
// A replay of the same id no longer resolves, so it reaches the human.
|
|
505
|
-
answerWith("allow");
|
|
506
|
-
await client.requestPermission(params);
|
|
507
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: "Unknown Tool" }));
|
|
508
|
-
});
|
|
509
|
-
// codex-acp follows an accepted MCP approval with a bare
|
|
510
|
-
// {toolCallId, status: "in_progress"} update carrying no title.
|
|
511
|
-
it("should retain a known title when a later update omits it", async () => {
|
|
512
|
-
await client.sessionUpdate({
|
|
513
|
-
sessionId: "sess-1",
|
|
514
|
-
update: {
|
|
515
|
-
sessionUpdate: "tool_call",
|
|
516
|
-
toolCallId: "call_keep",
|
|
517
|
-
title: "mcp.agentrq-0cHiaZsOGMj.reply",
|
|
518
|
-
status: "pending",
|
|
519
|
-
},
|
|
520
|
-
});
|
|
521
|
-
await client.sessionUpdate({
|
|
522
|
-
sessionId: "sess-1",
|
|
523
|
-
update: {
|
|
524
|
-
sessionUpdate: "tool_call_update",
|
|
525
|
-
toolCallId: "call_keep",
|
|
526
|
-
status: "in_progress",
|
|
527
|
-
},
|
|
528
|
-
});
|
|
529
|
-
const params = {
|
|
530
|
-
sessionId: "sess-1",
|
|
531
|
-
toolCall: { toolCallId: "call_keep", kind: "execute", status: "pending" },
|
|
532
|
-
options: [
|
|
533
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
534
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
535
|
-
],
|
|
536
|
-
};
|
|
537
|
-
const response = await client.requestPermission(params);
|
|
538
|
-
expect(mcpBridge.sendNotification).not.toHaveBeenCalled();
|
|
539
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
540
|
-
});
|
|
541
|
-
it("should record nothing for an update with neither title nor input", async () => {
|
|
542
|
-
await client.sessionUpdate({
|
|
543
|
-
sessionId: "sess-1",
|
|
544
|
-
update: {
|
|
545
|
-
sessionUpdate: "tool_call_update",
|
|
546
|
-
toolCallId: "call_bare",
|
|
547
|
-
status: "in_progress",
|
|
548
|
-
},
|
|
549
|
-
});
|
|
550
|
-
const params = {
|
|
551
|
-
sessionId: "sess-1",
|
|
552
|
-
toolCall: { toolCallId: "call_bare", kind: "execute", status: "pending" },
|
|
553
|
-
options: [
|
|
554
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
555
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject" },
|
|
556
|
-
],
|
|
557
|
-
};
|
|
558
|
-
answerWith("allow");
|
|
559
|
-
await client.requestPermission(params);
|
|
560
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ tool_name: "Unknown Tool", input_preview: "{}" }));
|
|
561
|
-
});
|
|
562
|
-
it("should ignore session updates that carry no tool call id", async () => {
|
|
563
|
-
await expect(client.sessionUpdate({
|
|
564
|
-
sessionId: "sess-1",
|
|
565
|
-
update: { sessionUpdate: "tool_call_update", title: "no id here" },
|
|
566
|
-
})).resolves.toBeUndefined();
|
|
567
|
-
});
|
|
568
|
-
it("should match options with 'yes' or 'approve'", async () => {
|
|
569
|
-
const params = {
|
|
570
|
-
toolCall: { toolCallId: "req-123" },
|
|
571
|
-
options: [
|
|
572
|
-
{ optionId: "opt-1", kind: "other", name: "Yes, proceed" },
|
|
573
|
-
{ optionId: "opt-2", kind: "other", name: "No" },
|
|
574
|
-
],
|
|
575
|
-
};
|
|
576
|
-
answerWith("allow");
|
|
577
|
-
const response = await client.requestPermission(params);
|
|
578
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
579
|
-
});
|
|
580
|
-
it("should match options with 'deny' in the name", async () => {
|
|
581
|
-
const params = {
|
|
582
|
-
toolCall: { toolCallId: "req-123" },
|
|
583
|
-
options: [
|
|
584
|
-
{ optionId: "opt-1", kind: "allow", name: "Allow" },
|
|
585
|
-
{ optionId: "opt-2", kind: "other", name: "Deny this" },
|
|
586
|
-
],
|
|
587
|
-
};
|
|
588
|
-
answerWith("deny");
|
|
589
|
-
const response = await client.requestPermission(params);
|
|
590
|
-
expect(response.outcome.optionId).toBe("opt-2");
|
|
591
|
-
});
|
|
592
|
-
it("should fall back to first option if 'deny' verdict has no specific match", async () => {
|
|
593
|
-
const params = {
|
|
594
|
-
toolCall: { toolCallId: "req-123" },
|
|
595
|
-
options: [
|
|
596
|
-
{ optionId: "opt-1", kind: "other", name: "Other 1" },
|
|
597
|
-
{ optionId: "opt-2", kind: "other", name: "Other 2" },
|
|
598
|
-
],
|
|
599
|
-
};
|
|
600
|
-
answerWith("deny");
|
|
601
|
-
const response = await client.requestPermission(params);
|
|
602
|
-
expect(response.outcome.optionId).toBe("opt-1");
|
|
603
|
-
});
|
|
604
|
-
it("should match spec-compliant reject_once/reject_always kinds on deny (not just a literal 'deny' kind prefix)", async () => {
|
|
605
|
-
const params = {
|
|
606
|
-
toolCall: { toolCallId: "req-123" },
|
|
607
|
-
options: [
|
|
608
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow Once" },
|
|
609
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Reject Once" },
|
|
610
|
-
],
|
|
611
|
-
};
|
|
612
|
-
answerWith("deny");
|
|
613
|
-
const response = await client.requestPermission(params);
|
|
614
|
-
// A spec-compliant "reject_once" kind must never be missed and fall
|
|
615
|
-
// through to the first (allow) option — that would silently approve a
|
|
616
|
-
// tool call the human explicitly denied.
|
|
617
|
-
expect(response.outcome.optionId).toBe("opt-2");
|
|
618
|
-
});
|
|
619
|
-
it("should never resolve a deny verdict to an allow-kind option, even with no matching option", async () => {
|
|
620
|
-
const params = {
|
|
621
|
-
toolCall: { toolCallId: "req-123" },
|
|
622
|
-
options: [
|
|
623
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Proceed" },
|
|
624
|
-
{ optionId: "opt-2", kind: "other", name: "Skip" },
|
|
625
|
-
],
|
|
626
|
-
};
|
|
627
|
-
answerWith("deny");
|
|
628
|
-
const response = await client.requestPermission(params);
|
|
629
|
-
expect(response.outcome.optionId).toBe("opt-2");
|
|
630
|
-
});
|
|
631
|
-
it("should cancel a deny verdict rather than select an allow option when every option is allow-kind", async () => {
|
|
632
|
-
const params = {
|
|
633
|
-
toolCall: { toolCallId: "req-123" },
|
|
634
|
-
options: [{ optionId: "opt-1", kind: "allow_once", name: "Proceed" }],
|
|
635
|
-
};
|
|
636
|
-
answerWith("deny");
|
|
637
|
-
const response = await client.requestPermission(params);
|
|
638
|
-
expect(response.outcome.outcome).toBe("cancelled");
|
|
639
|
-
});
|
|
640
|
-
it("should select the once option on allow, never the always option, so future calls still require agentrq approval", async () => {
|
|
641
|
-
const params = {
|
|
642
|
-
toolCall: { toolCallId: "req-123" },
|
|
643
|
-
options: [
|
|
644
|
-
{ optionId: "opt-always", kind: "allow_always", name: "Always Allow" },
|
|
645
|
-
{ optionId: "opt-once", kind: "allow_once", name: "Allow Once" },
|
|
646
|
-
],
|
|
647
|
-
};
|
|
648
|
-
answerWith("allow");
|
|
649
|
-
const response = await client.requestPermission(params);
|
|
650
|
-
// Selecting "allow_always" would make the spawned agent remember this
|
|
651
|
-
// decision and stop asking for matching future tool calls, bypassing
|
|
652
|
-
// agentrq entirely for those. Must always pick the once-only variant.
|
|
653
|
-
expect(response.outcome.optionId).toBe("opt-once");
|
|
654
|
-
});
|
|
655
|
-
it("should select the once option on deny, never the always option", async () => {
|
|
656
|
-
const params = {
|
|
657
|
-
toolCall: { toolCallId: "req-123" },
|
|
658
|
-
options: [
|
|
659
|
-
{ optionId: "opt-always", kind: "reject_always", name: "Always Reject" },
|
|
660
|
-
{ optionId: "opt-once", kind: "reject_once", name: "Reject Once" },
|
|
661
|
-
],
|
|
662
|
-
};
|
|
663
|
-
answerWith("deny");
|
|
664
|
-
const response = await client.requestPermission(params);
|
|
665
|
-
expect(response.outcome.optionId).toBe("opt-once");
|
|
666
|
-
});
|
|
667
|
-
it("should cancel an allow verdict rather than select an always-kind option when no once option exists", async () => {
|
|
668
|
-
const params = {
|
|
669
|
-
toolCall: { toolCallId: "req-123" },
|
|
670
|
-
options: [{ optionId: "opt-always", kind: "allow_always", name: "Always Allow" }],
|
|
671
|
-
};
|
|
672
|
-
answerWith("allow");
|
|
673
|
-
const response = await client.requestPermission(params);
|
|
674
|
-
expect(response.outcome.outcome).toBe("cancelled");
|
|
675
|
-
});
|
|
676
|
-
it("should fall back to first option if no match", async () => {
|
|
677
|
-
const params = {
|
|
678
|
-
toolCall: { toolCallId: "req-123" },
|
|
679
|
-
options: [{ optionId: "opt-default", kind: "other", name: "Maybe" }],
|
|
680
|
-
};
|
|
681
|
-
answerWith("allow");
|
|
682
|
-
const response = await client.requestPermission(params);
|
|
683
|
-
expect(response.outcome.optionId).toBe("opt-default");
|
|
684
|
-
});
|
|
685
|
-
});
|
|
686
|
-
describe("createElicitation", () => {
|
|
687
|
-
it("should delegate form mode to the elicit tool and return an accept result", async () => {
|
|
688
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
689
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
690
|
-
mcpBridge.callTool.mockResolvedValue({
|
|
691
|
-
content: [{ type: "text", text: JSON.stringify({ action: "accept", content: { strategy: "balanced" } }) }],
|
|
692
|
-
});
|
|
693
|
-
const params = {
|
|
694
|
-
sessionId: "sess-1",
|
|
695
|
-
mode: "form",
|
|
696
|
-
message: "How should I proceed?",
|
|
697
|
-
requestedSchema: { type: "object", properties: { strategy: { type: "string" } } },
|
|
698
|
-
};
|
|
699
|
-
const response = await clientWithTaskId.createElicitation(params);
|
|
700
|
-
expect(getTaskId).toHaveBeenCalledWith("sess-1");
|
|
701
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("elicit", {
|
|
702
|
-
taskId: "task-123",
|
|
703
|
-
message: "How should I proceed?",
|
|
704
|
-
mode: "form",
|
|
705
|
-
requestedSchema: params.requestedSchema,
|
|
706
|
-
});
|
|
707
|
-
expect(response).toEqual({ action: "accept", content: { strategy: "balanced" } });
|
|
708
|
-
});
|
|
709
|
-
it("should delegate url mode to the elicit tool", async () => {
|
|
710
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
711
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
712
|
-
mcpBridge.callTool.mockResolvedValue({
|
|
713
|
-
content: [{ type: "text", text: JSON.stringify({ action: "accept" }) }],
|
|
714
|
-
});
|
|
715
|
-
const params = {
|
|
716
|
-
sessionId: "sess-1",
|
|
717
|
-
mode: "url",
|
|
718
|
-
elicitationId: "oauth-1",
|
|
719
|
-
url: "https://example.com/connect",
|
|
720
|
-
message: "Please authorize access.",
|
|
721
|
-
};
|
|
722
|
-
const response = await clientWithTaskId.createElicitation(params);
|
|
723
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("elicit", {
|
|
724
|
-
taskId: "task-123",
|
|
725
|
-
message: "Please authorize access.",
|
|
726
|
-
mode: "url",
|
|
727
|
-
url: "https://example.com/connect",
|
|
728
|
-
});
|
|
729
|
-
expect(response).toEqual({ action: "accept" });
|
|
730
|
-
});
|
|
731
|
-
it("should map decline and cancel actions through", async () => {
|
|
732
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
733
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
734
|
-
const params = {
|
|
735
|
-
sessionId: "sess-1",
|
|
736
|
-
mode: "form",
|
|
737
|
-
message: "Name?",
|
|
738
|
-
requestedSchema: { type: "object", properties: { name: { type: "string" } } },
|
|
739
|
-
};
|
|
740
|
-
mcpBridge.callTool.mockResolvedValueOnce({
|
|
741
|
-
content: [{ type: "text", text: JSON.stringify({ action: "decline" }) }],
|
|
742
|
-
});
|
|
743
|
-
expect(await clientWithTaskId.createElicitation(params)).toEqual({ action: "decline" });
|
|
744
|
-
mcpBridge.callTool.mockResolvedValueOnce({
|
|
745
|
-
content: [{ type: "text", text: JSON.stringify({ action: "cancel" }) }],
|
|
746
|
-
});
|
|
747
|
-
expect(await clientWithTaskId.createElicitation(params)).toEqual({ action: "cancel" });
|
|
748
|
-
});
|
|
749
|
-
it("should create and start a task when there is no associated task, then delegate to it", async () => {
|
|
750
|
-
const params = {
|
|
751
|
-
mode: "form",
|
|
752
|
-
message: "Please provide your workspace name to continue setup.",
|
|
753
|
-
requestedSchema: { type: "object", properties: { name: { type: "string" } } },
|
|
754
|
-
};
|
|
755
|
-
mcpBridge.callTool.mockImplementation(async (name) => {
|
|
756
|
-
if (name === "createTask") {
|
|
757
|
-
return { content: [{ type: "text", text: "task created with id=T-new123" }] };
|
|
758
|
-
}
|
|
759
|
-
if (name === "updateTaskStatus") {
|
|
760
|
-
return { content: [{ type: "text", text: "ok" }] };
|
|
761
|
-
}
|
|
762
|
-
if (name === "elicit") {
|
|
763
|
-
return { content: [{ type: "text", text: JSON.stringify({ action: "accept", content: { name: "acme" } }) }] };
|
|
764
|
-
}
|
|
765
|
-
throw new Error(`unexpected tool ${name}`);
|
|
766
|
-
});
|
|
767
|
-
const response = await client.createElicitation(params);
|
|
768
|
-
expect(mcpBridge.callTool).toHaveBeenNthCalledWith(1, "createTask", {
|
|
769
|
-
title: params.message,
|
|
770
|
-
body: params.message,
|
|
771
|
-
assignee: "human",
|
|
772
|
-
});
|
|
773
|
-
expect(mcpBridge.callTool).toHaveBeenNthCalledWith(2, "updateTaskStatus", {
|
|
774
|
-
taskId: "T-new123",
|
|
775
|
-
status: "ongoing",
|
|
776
|
-
});
|
|
777
|
-
expect(mcpBridge.callTool).toHaveBeenNthCalledWith(3, "elicit", {
|
|
778
|
-
taskId: "T-new123",
|
|
779
|
-
message: params.message,
|
|
780
|
-
mode: "form",
|
|
781
|
-
requestedSchema: params.requestedSchema,
|
|
782
|
-
});
|
|
783
|
-
expect(response).toEqual({ action: "accept", content: { name: "acme" } });
|
|
784
|
-
});
|
|
785
|
-
it("should truncate long messages for the created task's title", async () => {
|
|
786
|
-
const longMessage = "x".repeat(120);
|
|
787
|
-
const params = { mode: "form", message: longMessage, requestedSchema: { type: "object", properties: {} } };
|
|
788
|
-
mcpBridge.callTool.mockImplementation(async (name) => {
|
|
789
|
-
if (name === "createTask")
|
|
790
|
-
return { content: [{ type: "text", text: "task created with id=T-1" }] };
|
|
791
|
-
if (name === "updateTaskStatus")
|
|
792
|
-
return { content: [{ type: "text", text: "ok" }] };
|
|
793
|
-
return { content: [{ type: "text", text: JSON.stringify({ action: "cancel" }) }] };
|
|
794
|
-
});
|
|
795
|
-
await client.createElicitation(params);
|
|
796
|
-
const [, createTaskArgs] = mcpBridge.callTool.mock.calls[0];
|
|
797
|
-
expect(createTaskArgs.title).toBe(`${"x".repeat(77)}...`);
|
|
798
|
-
expect(createTaskArgs.title.length).toBe(80);
|
|
799
|
-
expect(createTaskArgs.body).toBe(longMessage);
|
|
800
|
-
});
|
|
801
|
-
it("should cancel when the created task's ID cannot be parsed", async () => {
|
|
802
|
-
const params = { sessionId: undefined, mode: "form", message: "Name?", requestedSchema: {} };
|
|
803
|
-
mcpBridge.callTool.mockResolvedValue({ content: [{ type: "text", text: "something went wrong" }] });
|
|
804
|
-
const response = await client.createElicitation(params);
|
|
805
|
-
expect(mcpBridge.callTool).toHaveBeenCalledTimes(1);
|
|
806
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("createTask", expect.any(Object));
|
|
807
|
-
expect(response).toEqual({ action: "cancel" });
|
|
808
|
-
});
|
|
809
|
-
it("should cancel when task creation throws", async () => {
|
|
810
|
-
const params = { mode: "form", message: "Name?", requestedSchema: {} };
|
|
811
|
-
mcpBridge.callTool.mockRejectedValue(new Error("MCP not connected"));
|
|
812
|
-
const response = await client.createElicitation(params);
|
|
813
|
-
expect(response).toEqual({ action: "cancel" });
|
|
814
|
-
});
|
|
815
|
-
it("should cancel for an unsupported elicitation mode", async () => {
|
|
816
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
817
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
818
|
-
const params = { sessionId: "sess-1", mode: "_custom", message: "?" };
|
|
819
|
-
const response = await clientWithTaskId.createElicitation(params);
|
|
820
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
821
|
-
expect(response).toEqual({ action: "cancel" });
|
|
822
|
-
});
|
|
823
|
-
it("should cancel when the elicit tool call errors", async () => {
|
|
824
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
825
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
826
|
-
const params = {
|
|
827
|
-
sessionId: "sess-1",
|
|
828
|
-
mode: "form",
|
|
829
|
-
message: "Name?",
|
|
830
|
-
requestedSchema: { type: "object", properties: { name: { type: "string" } } },
|
|
831
|
-
};
|
|
832
|
-
mcpBridge.callTool.mockResolvedValue({
|
|
833
|
-
isError: true,
|
|
834
|
-
content: [{ type: "text", text: "taskId and message are required" }],
|
|
835
|
-
});
|
|
836
|
-
const response = await clientWithTaskId.createElicitation(params);
|
|
837
|
-
expect(response).toEqual({ action: "cancel" });
|
|
838
|
-
});
|
|
839
|
-
it("should cancel when the elicit tool call throws", async () => {
|
|
840
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
841
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
842
|
-
const params = {
|
|
843
|
-
sessionId: "sess-1",
|
|
844
|
-
mode: "form",
|
|
845
|
-
message: "Name?",
|
|
846
|
-
requestedSchema: { type: "object", properties: { name: { type: "string" } } },
|
|
847
|
-
};
|
|
848
|
-
mcpBridge.callTool.mockRejectedValue(new Error("MCP not connected"));
|
|
849
|
-
const response = await clientWithTaskId.createElicitation(params);
|
|
850
|
-
expect(response).toEqual({ action: "cancel" });
|
|
851
|
-
});
|
|
852
|
-
});
|
|
853
|
-
describe("url elicitations at the terminal", () => {
|
|
854
|
-
const urlParams = {
|
|
855
|
-
mode: "url",
|
|
856
|
-
elicitationId: "oauth-1",
|
|
857
|
-
url: "https://auth.openai.com/codex/device",
|
|
858
|
-
message: "Sign in to ChatGPT and enter this code: N8TX-6RKPJ",
|
|
859
|
-
};
|
|
860
|
-
it("should log the URL itself, not just the message", async () => {
|
|
861
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
862
|
-
const clientWithTaskId = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
863
|
-
mcpBridge.callTool.mockResolvedValue({
|
|
864
|
-
content: [{ type: "text", text: JSON.stringify({ action: "accept" }) }],
|
|
865
|
-
});
|
|
866
|
-
const logged = [];
|
|
867
|
-
const spy = vi.spyOn(console, "error").mockImplementation((...args) => {
|
|
868
|
-
logged.push(args.join(" "));
|
|
869
|
-
});
|
|
870
|
-
try {
|
|
871
|
-
await clientWithTaskId.createElicitation({ ...urlParams, sessionId: "sess-1" });
|
|
872
|
-
}
|
|
873
|
-
finally {
|
|
874
|
-
spy.mockRestore();
|
|
875
|
-
}
|
|
876
|
-
const banner = logged.join("\n");
|
|
877
|
-
expect(banner).toContain("https://auth.openai.com/codex/device");
|
|
878
|
-
expect(banner).toContain("N8TX-6RKPJ");
|
|
879
|
-
});
|
|
880
|
-
it("should ask at the terminal instead of creating a task when there is no session", async () => {
|
|
881
|
-
const promptUrlElicitation = vi.fn().mockResolvedValue({ action: "accept" });
|
|
882
|
-
const terminalClient = new AgentRQACPClient(mcpBridge, () => undefined, { promptUrlElicitation });
|
|
883
|
-
const response = await terminalClient.createElicitation(urlParams);
|
|
884
|
-
expect(response).toEqual({ action: "accept" });
|
|
885
|
-
expect(promptUrlElicitation).toHaveBeenCalledWith(expect.objectContaining({
|
|
886
|
-
url: urlParams.url,
|
|
887
|
-
message: urlParams.message,
|
|
888
|
-
signal: expect.any(AbortSignal),
|
|
889
|
-
}));
|
|
890
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
891
|
-
});
|
|
892
|
-
it("should still use the workspace when a session already has a task", async () => {
|
|
893
|
-
const promptUrlElicitation = vi.fn().mockResolvedValue({ action: "accept" });
|
|
894
|
-
const sessionClient = new AgentRQACPClient(mcpBridge, () => "task-123", { promptUrlElicitation });
|
|
895
|
-
mcpBridge.callTool.mockResolvedValue({
|
|
896
|
-
content: [{ type: "text", text: JSON.stringify({ action: "accept" }) }],
|
|
897
|
-
});
|
|
898
|
-
await sessionClient.createElicitation({ ...urlParams, sessionId: "sess-1" });
|
|
899
|
-
expect(promptUrlElicitation).not.toHaveBeenCalled();
|
|
900
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("elicit", {
|
|
901
|
-
taskId: "task-123",
|
|
902
|
-
message: urlParams.message,
|
|
903
|
-
mode: "url",
|
|
904
|
-
url: urlParams.url,
|
|
905
|
-
});
|
|
906
|
-
});
|
|
907
|
-
it("should create a task when there is no session and no terminal to ask at", async () => {
|
|
908
|
-
mcpBridge.callTool.mockImplementation(async (name) => {
|
|
909
|
-
if (name === "createTask")
|
|
910
|
-
return { content: [{ type: "text", text: "id=T-1" }] };
|
|
911
|
-
if (name === "updateTaskStatus")
|
|
912
|
-
return { content: [{ type: "text", text: "ok" }] };
|
|
913
|
-
return { content: [{ type: "text", text: JSON.stringify({ action: "accept" }) }] };
|
|
914
|
-
});
|
|
915
|
-
const response = await client.createElicitation(urlParams);
|
|
916
|
-
expect(response).toEqual({ action: "accept" });
|
|
917
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("createTask", expect.any(Object));
|
|
918
|
-
});
|
|
919
|
-
it("should cancel when the terminal prompt itself fails", async () => {
|
|
920
|
-
const promptUrlElicitation = vi.fn().mockRejectedValue(new Error("stdin is gone"));
|
|
921
|
-
const terminalClient = new AgentRQACPClient(mcpBridge, () => undefined, { promptUrlElicitation });
|
|
922
|
-
const response = await terminalClient.createElicitation(urlParams);
|
|
923
|
-
expect(response).toEqual({ action: "cancel" });
|
|
924
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
925
|
-
});
|
|
926
|
-
it("should prompt even for an elicitation the agent gave no id to", async () => {
|
|
927
|
-
const promptUrlElicitation = vi.fn().mockResolvedValue({ action: "accept" });
|
|
928
|
-
const terminalClient = new AgentRQACPClient(mcpBridge, () => undefined, { promptUrlElicitation });
|
|
929
|
-
const { elicitationId: _omitted, ...withoutId } = urlParams;
|
|
930
|
-
const response = await terminalClient.createElicitation(withoutId);
|
|
931
|
-
expect(response).toEqual({ action: "accept" });
|
|
932
|
-
expect(promptUrlElicitation).toHaveBeenCalled();
|
|
933
|
-
});
|
|
934
|
-
it("should cancel a form-mode elicitation at the terminal rather than prompting for a URL", async () => {
|
|
935
|
-
const promptUrlElicitation = vi.fn();
|
|
936
|
-
const terminalClient = new AgentRQACPClient(mcpBridge, () => undefined, { promptUrlElicitation });
|
|
937
|
-
mcpBridge.callTool.mockImplementation(async (name) => {
|
|
938
|
-
if (name === "createTask")
|
|
939
|
-
return { content: [{ type: "text", text: "id=T-1" }] };
|
|
940
|
-
if (name === "updateTaskStatus")
|
|
941
|
-
return { content: [{ type: "text", text: "ok" }] };
|
|
942
|
-
return { content: [{ type: "text", text: JSON.stringify({ action: "accept" }) }] };
|
|
943
|
-
});
|
|
944
|
-
await terminalClient.createElicitation({
|
|
945
|
-
mode: "form",
|
|
946
|
-
message: "Name?",
|
|
947
|
-
requestedSchema: { type: "object", properties: {} },
|
|
948
|
-
});
|
|
949
|
-
expect(promptUrlElicitation).not.toHaveBeenCalled();
|
|
950
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("createTask", expect.any(Object));
|
|
951
|
-
});
|
|
952
|
-
});
|
|
953
|
-
describe("completeElicitation", () => {
|
|
954
|
-
it("should resolve without side effects", async () => {
|
|
955
|
-
await expect(client.completeElicitation({ elicitationId: "oauth-1" })).resolves.toBeUndefined();
|
|
956
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
957
|
-
});
|
|
958
|
-
it("should abort a terminal prompt the agent no longer needs an answer to", async () => {
|
|
959
|
-
let seenSignal;
|
|
960
|
-
const promptUrlElicitation = vi.fn(({ signal }) => new Promise((resolve) => {
|
|
961
|
-
seenSignal = signal;
|
|
962
|
-
signal.addEventListener("abort", () => resolve({ action: "cancel" }));
|
|
963
|
-
}));
|
|
964
|
-
const terminalClient = new AgentRQACPClient(mcpBridge, () => undefined, { promptUrlElicitation });
|
|
965
|
-
const pending = terminalClient.createElicitation({
|
|
966
|
-
mode: "url",
|
|
967
|
-
elicitationId: "oauth-1",
|
|
968
|
-
url: "https://example.com/device",
|
|
969
|
-
message: "Enter code ABCD",
|
|
970
|
-
});
|
|
971
|
-
await vi.waitFor(() => expect(seenSignal).toBeDefined());
|
|
972
|
-
await terminalClient.completeElicitation({ elicitationId: "oauth-1" });
|
|
973
|
-
await expect(pending).resolves.toEqual({ action: "cancel" });
|
|
974
|
-
});
|
|
975
|
-
it("should ignore a completion for an elicitation it is not waiting on", async () => {
|
|
976
|
-
const promptUrlElicitation = vi.fn().mockResolvedValue({ action: "accept" });
|
|
977
|
-
const terminalClient = new AgentRQACPClient(mcpBridge, () => undefined, { promptUrlElicitation });
|
|
978
|
-
await expect(terminalClient.completeElicitation({ elicitationId: "unknown" })).resolves.toBeUndefined();
|
|
979
|
-
});
|
|
980
|
-
});
|
|
981
|
-
describe("sessionUpdate", () => {
|
|
982
|
-
it("should write text chunks to stdout", async () => {
|
|
983
|
-
setVerbose(true);
|
|
984
|
-
const writeSpy = vi
|
|
985
|
-
.spyOn(process.stdout, "write")
|
|
986
|
-
.mockImplementation(() => true);
|
|
987
|
-
const params = {
|
|
988
|
-
sessionId: "sess-1",
|
|
989
|
-
update: {
|
|
990
|
-
sessionUpdate: "agent_message_chunk",
|
|
991
|
-
content: { type: "text", text: "hello" },
|
|
992
|
-
},
|
|
993
|
-
};
|
|
994
|
-
await client.sessionUpdate(params);
|
|
995
|
-
expect(writeSpy).toHaveBeenCalledWith("hello");
|
|
996
|
-
writeSpy.mockRestore();
|
|
997
|
-
});
|
|
998
|
-
it("should keep the agent's streamed answer off the terminal unless verbose", async () => {
|
|
999
|
-
const writeSpy = vi
|
|
1000
|
-
.spyOn(process.stdout, "write")
|
|
1001
|
-
.mockImplementation(() => true);
|
|
1002
|
-
const params = {
|
|
1003
|
-
sessionId: "sess-1",
|
|
1004
|
-
update: {
|
|
1005
|
-
sessionUpdate: "agent_message_chunk",
|
|
1006
|
-
content: { type: "text", text: "hello" },
|
|
1007
|
-
},
|
|
1008
|
-
};
|
|
1009
|
-
await client.sessionUpdate(params);
|
|
1010
|
-
expect(writeSpy).not.toHaveBeenCalled();
|
|
1011
|
-
writeSpy.mockRestore();
|
|
1012
|
-
});
|
|
1013
|
-
it("should not print tool calls unless verbose", async () => {
|
|
1014
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1015
|
-
await client.sessionUpdate({
|
|
1016
|
-
sessionId: "sess-1",
|
|
1017
|
-
update: { sessionUpdate: "tool_call", toolCallId: "t1", title: "Read file", status: "pending" },
|
|
1018
|
-
});
|
|
1019
|
-
expect(consoleSpy).not.toHaveBeenCalledWith(expect.stringContaining("Tool call: Read file"));
|
|
1020
|
-
consoleSpy.mockRestore();
|
|
1021
|
-
});
|
|
1022
|
-
it("should ignore non-text chunks", async () => {
|
|
1023
|
-
const writeSpy = vi
|
|
1024
|
-
.spyOn(process.stdout, "write")
|
|
1025
|
-
.mockImplementation(() => true);
|
|
1026
|
-
const params = {
|
|
1027
|
-
sessionId: "sess-1",
|
|
1028
|
-
update: {
|
|
1029
|
-
sessionUpdate: "agent_message_chunk",
|
|
1030
|
-
content: { type: "other", text: "ignored" },
|
|
1031
|
-
},
|
|
1032
|
-
};
|
|
1033
|
-
await client.sessionUpdate(params);
|
|
1034
|
-
expect(writeSpy).not.toHaveBeenCalled();
|
|
1035
|
-
writeSpy.mockRestore();
|
|
1036
|
-
});
|
|
1037
|
-
it("should log tool calls", async () => {
|
|
1038
|
-
setVerbose(true);
|
|
1039
|
-
const consoleSpy = vi
|
|
1040
|
-
.spyOn(console, "error")
|
|
1041
|
-
.mockImplementation(() => { });
|
|
1042
|
-
const params = {
|
|
1043
|
-
update: {
|
|
1044
|
-
sessionUpdate: "tool_call",
|
|
1045
|
-
title: "test-tool",
|
|
1046
|
-
status: "pending",
|
|
1047
|
-
},
|
|
1048
|
-
};
|
|
1049
|
-
await client.sessionUpdate(params);
|
|
1050
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Tool call: test-tool (pending)"));
|
|
1051
|
-
consoleSpy.mockRestore();
|
|
1052
|
-
});
|
|
1053
|
-
it("should skip logging for auto-allowed tool calls", async () => {
|
|
1054
|
-
const consoleSpy = vi
|
|
1055
|
-
.spyOn(console, "error")
|
|
1056
|
-
.mockImplementation(() => { });
|
|
1057
|
-
const params = {
|
|
1058
|
-
update: {
|
|
1059
|
-
sessionUpdate: "tool_call",
|
|
1060
|
-
title: "updateTaskStatus (agentrq-0aleR6CbZBp MCP Server)",
|
|
1061
|
-
status: "pending",
|
|
1062
|
-
},
|
|
1063
|
-
};
|
|
1064
|
-
await client.sessionUpdate(params);
|
|
1065
|
-
expect(consoleSpy).not.toHaveBeenCalled();
|
|
1066
|
-
consoleSpy.mockRestore();
|
|
1067
|
-
});
|
|
1068
|
-
it("should handle unknown update types", async () => {
|
|
1069
|
-
const params = { update: { sessionUpdate: "unknown" } };
|
|
1070
|
-
await expect(client.sessionUpdate(params)).resolves.toBeUndefined();
|
|
1071
|
-
});
|
|
1072
|
-
});
|
|
1073
|
-
describe("flushReply", () => {
|
|
1074
|
-
it("should call reply tool with accumulated text and clear the buffer", async () => {
|
|
1075
|
-
mcpBridge.callTool = vi.fn().mockResolvedValue({ isError: false, content: [] });
|
|
1076
|
-
const getTaskId = vi.fn().mockReturnValue("task-123");
|
|
1077
|
-
const c = new AgentRQACPClient(mcpBridge, getTaskId);
|
|
1078
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
1079
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello " } } });
|
|
1080
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "world" } } });
|
|
1081
|
-
await c.flushReply("sess-1");
|
|
1082
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("reply", { chatId: "task-123", text: "Hello world" });
|
|
1083
|
-
// Buffer cleared — second flush should not call reply again
|
|
1084
|
-
mcpBridge.callTool.mockClear();
|
|
1085
|
-
await c.flushReply("sess-1");
|
|
1086
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
1087
|
-
});
|
|
1088
|
-
it("should skip reply if buffer is empty", async () => {
|
|
1089
|
-
mcpBridge.callTool = vi.fn();
|
|
1090
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
1091
|
-
await c.flushReply("sess-empty");
|
|
1092
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
1093
|
-
});
|
|
1094
|
-
it("should skip reply if no task ID is found for the session", async () => {
|
|
1095
|
-
setVerbose(true);
|
|
1096
|
-
mcpBridge.callTool = vi.fn();
|
|
1097
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1098
|
-
const c = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
1099
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
1100
|
-
await c.sessionUpdate({ sessionId: "sess-unknown", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "hi" } } });
|
|
1101
|
-
await c.flushReply("sess-unknown");
|
|
1102
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
1103
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("No task ID for session"));
|
|
1104
|
-
consoleSpy.mockRestore();
|
|
1105
|
-
});
|
|
1106
|
-
it("should skip reply when agent already sent identical reply via tool call", async () => {
|
|
1107
|
-
setVerbose(true);
|
|
1108
|
-
mcpBridge.callTool = vi.fn().mockResolvedValue({ isError: false, content: [] });
|
|
1109
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1110
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
1111
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
1112
|
-
// Agent buffers text
|
|
1113
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello world" } } });
|
|
1114
|
-
// Agent also called reply via MCP tool with identical params
|
|
1115
|
-
await c.sessionUpdate({
|
|
1116
|
-
sessionId: "sess-1",
|
|
1117
|
-
update: {
|
|
1118
|
-
sessionUpdate: "tool_call",
|
|
1119
|
-
title: "reply (agentrq-0an2BXTfpGj MCP Server)",
|
|
1120
|
-
status: "completed",
|
|
1121
|
-
toolCallId: "tc-1",
|
|
1122
|
-
rawInput: { chatId: "task-123", text: "Hello world" },
|
|
1123
|
-
},
|
|
1124
|
-
});
|
|
1125
|
-
await c.flushReply("sess-1");
|
|
1126
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalledWith("reply", expect.anything());
|
|
1127
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("agent already sent identical reply"));
|
|
1128
|
-
consoleSpy.mockRestore();
|
|
1129
|
-
});
|
|
1130
|
-
it("should still send reply when agent called reply with different text", async () => {
|
|
1131
|
-
mcpBridge.callTool = vi.fn().mockResolvedValue({ isError: false, content: [] });
|
|
1132
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
1133
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
1134
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Full verbose output" } } });
|
|
1135
|
-
// Agent sent a different message via reply tool
|
|
1136
|
-
await c.sessionUpdate({
|
|
1137
|
-
sessionId: "sess-1",
|
|
1138
|
-
update: {
|
|
1139
|
-
sessionUpdate: "tool_call",
|
|
1140
|
-
title: "reply (agentrq-0an2BXTfpGj MCP Server)",
|
|
1141
|
-
status: "completed",
|
|
1142
|
-
toolCallId: "tc-1",
|
|
1143
|
-
rawInput: { chatId: "task-123", text: "Summary only" },
|
|
1144
|
-
},
|
|
1145
|
-
});
|
|
1146
|
-
await c.flushReply("sess-1");
|
|
1147
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("reply", { chatId: "task-123", text: "Full verbose output" });
|
|
1148
|
-
});
|
|
1149
|
-
it("should not skip reply if agent tool call was not completed", async () => {
|
|
1150
|
-
mcpBridge.callTool = vi.fn().mockResolvedValue({ isError: false, content: [] });
|
|
1151
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
1152
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
1153
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello world" } } });
|
|
1154
|
-
// Tool call is in_progress, not completed
|
|
1155
|
-
await c.sessionUpdate({
|
|
1156
|
-
sessionId: "sess-1",
|
|
1157
|
-
update: {
|
|
1158
|
-
sessionUpdate: "tool_call",
|
|
1159
|
-
title: "reply (agentrq-0an2BXTfpGj MCP Server)",
|
|
1160
|
-
status: "in_progress",
|
|
1161
|
-
toolCallId: "tc-1",
|
|
1162
|
-
rawInput: { chatId: "task-123", text: "Hello world" },
|
|
1163
|
-
},
|
|
1164
|
-
});
|
|
1165
|
-
await c.flushReply("sess-1");
|
|
1166
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("reply", { chatId: "task-123", text: "Hello world" });
|
|
1167
|
-
});
|
|
1168
|
-
});
|
|
1169
|
-
describe("waiting for a verdict", () => {
|
|
1170
|
-
const params = (overrides = {}) => ({
|
|
1171
|
-
sessionId: "sess-1",
|
|
1172
|
-
toolCall: { toolCallId: "call-1", title: "Bash", rawInput: { command: "ls" } },
|
|
1173
|
-
options: [
|
|
1174
|
-
{ optionId: "opt-1", kind: "allow_once", name: "Allow" },
|
|
1175
|
-
{ optionId: "opt-2", kind: "reject_once", name: "Deny" },
|
|
1176
|
-
],
|
|
1177
|
-
...overrides,
|
|
1178
|
-
});
|
|
1179
|
-
it("should scope the request id to the session it came from", async () => {
|
|
1180
|
-
answerWith("allow");
|
|
1181
|
-
await client.requestPermission(params());
|
|
1182
|
-
// agentrq keys its bookkeeping on the request id alone, workspace-wide,
|
|
1183
|
-
// while tool call ids are only unique within one session.
|
|
1184
|
-
expect(mcpBridge.sendNotification.mock.calls[0][1].request_id).toBe("sess-1:call-1");
|
|
1185
|
-
});
|
|
1186
|
-
it("should fall back to the bare tool call id when there is no session", async () => {
|
|
1187
|
-
answerWith("allow");
|
|
1188
|
-
await client.requestPermission(params({ sessionId: undefined }));
|
|
1189
|
-
expect(mcpBridge.sendNotification.mock.calls[0][1].request_id).toBe("call-1");
|
|
1190
|
-
});
|
|
1191
|
-
it("should keep one verdict listener however many calls are waiting", async () => {
|
|
1192
|
-
const waiting = [
|
|
1193
|
-
client.requestPermission(params()),
|
|
1194
|
-
client.requestPermission(params({ toolCall: { toolCallId: "call-2", title: "Bash" } })),
|
|
1195
|
-
client.requestPermission(params({ toolCall: { toolCallId: "call-3", title: "Bash" } })),
|
|
1196
|
-
];
|
|
1197
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(3));
|
|
1198
|
-
// A listener per request was only ever removed on a matching verdict, so
|
|
1199
|
-
// every unanswered request leaked one for the life of the process.
|
|
1200
|
-
expect(mcpBridge.listenerCount("verdict")).toBe(1);
|
|
1201
|
-
client.cancelPendingPermissions("test");
|
|
1202
|
-
await Promise.all(waiting);
|
|
1203
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
1204
|
-
});
|
|
1205
|
-
it("should ignore a verdict for a call that is not waiting", async () => {
|
|
1206
|
-
answerWith("allow");
|
|
1207
|
-
await client.requestPermission(params());
|
|
1208
|
-
expect(() => mcpBridge.emit("verdict", { requestId: "gone", behavior: "allow" })).not.toThrow();
|
|
1209
|
-
});
|
|
1210
|
-
it("should give up on a call nobody answers, and stop the turn", async () => {
|
|
1211
|
-
vi.useFakeTimers();
|
|
1212
|
-
const cancel = vi.fn();
|
|
1213
|
-
const bounded = new AgentRQACPClient(mcpBridge, () => "task-1", {
|
|
1214
|
-
permissionTimeoutMs: 60_000,
|
|
1215
|
-
});
|
|
1216
|
-
bounded.setSessionCanceller(cancel);
|
|
1217
|
-
const waiting = bounded.requestPermission(params());
|
|
1218
|
-
await vi.waitFor(() => expect(bounded.pendingPermissionCount).toBe(1));
|
|
1219
|
-
await vi.advanceTimersByTimeAsync(60_000);
|
|
1220
|
-
// Cancelling as well as answering: the spec treats "cancelled" as the
|
|
1221
|
-
// answer a client gives because it cancelled the turn. Answering alone
|
|
1222
|
-
// would leave the agent free to carry on and ask again.
|
|
1223
|
-
expect((await waiting).outcome.outcome).toBe("cancelled");
|
|
1224
|
-
expect(cancel).toHaveBeenCalledWith("sess-1");
|
|
1225
|
-
expect(bounded.pendingPermissionCount).toBe(0);
|
|
1226
|
-
vi.useRealTimers();
|
|
1227
|
-
});
|
|
1228
|
-
it("should survive a turn that cannot be cancelled", async () => {
|
|
1229
|
-
vi.useFakeTimers();
|
|
1230
|
-
const bounded = new AgentRQACPClient(mcpBridge, () => "task-1", {
|
|
1231
|
-
permissionTimeoutMs: 60_000,
|
|
1232
|
-
});
|
|
1233
|
-
bounded.setSessionCanceller(() => {
|
|
1234
|
-
throw new Error("connection gone");
|
|
1235
|
-
});
|
|
1236
|
-
const waiting = bounded.requestPermission(params());
|
|
1237
|
-
await vi.waitFor(() => expect(bounded.pendingPermissionCount).toBe(1));
|
|
1238
|
-
await vi.advanceTimersByTimeAsync(60_000);
|
|
1239
|
-
expect((await waiting).outcome.outcome).toBe("cancelled");
|
|
1240
|
-
vi.useRealTimers();
|
|
1241
|
-
});
|
|
1242
|
-
it("should wait indefinitely when the timeout is switched off", async () => {
|
|
1243
|
-
vi.useFakeTimers();
|
|
1244
|
-
const unbounded = new AgentRQACPClient(mcpBridge, () => "task-1", {
|
|
1245
|
-
permissionTimeoutMs: 0,
|
|
1246
|
-
});
|
|
1247
|
-
const waiting = unbounded.requestPermission(params());
|
|
1248
|
-
await vi.waitFor(() => expect(unbounded.pendingPermissionCount).toBe(1));
|
|
1249
|
-
await vi.advanceTimersByTimeAsync(24 * 60 * 60_000);
|
|
1250
|
-
expect(unbounded.pendingPermissionCount).toBe(1);
|
|
1251
|
-
unbounded.cancelPendingPermissions("test over");
|
|
1252
|
-
expect((await waiting).outcome.outcome).toBe("cancelled");
|
|
1253
|
-
vi.useRealTimers();
|
|
1254
|
-
});
|
|
1255
|
-
it("should answer everything still waiting when the agent is gone", async () => {
|
|
1256
|
-
const waiting = client.requestPermission(params());
|
|
1257
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(1));
|
|
1258
|
-
client.cancelPendingPermissions("agent process exited");
|
|
1259
|
-
expect((await waiting).outcome.outcome).toBe("cancelled");
|
|
1260
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
1261
|
-
});
|
|
1262
|
-
it("should re-send waiting calls when the workspace reconnects", async () => {
|
|
1263
|
-
const waiting = client.requestPermission(params());
|
|
1264
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(1));
|
|
1265
|
-
const sentFirst = mcpBridge.sendNotification.mock.calls.length;
|
|
1266
|
-
mcpBridge.emit("reconnected");
|
|
1267
|
-
await vi.waitFor(() => expect(mcpBridge.sendNotification.mock.calls.length).toBe(sentFirst + 1));
|
|
1268
|
-
// Same request id: the workspace has to recognise this as the decision it
|
|
1269
|
-
// is already showing, not a new one to ask about again.
|
|
1270
|
-
const [first, resent] = mcpBridge.sendNotification.mock.calls.map((c) => c[1]);
|
|
1271
|
-
expect(resent).toEqual(first);
|
|
1272
|
-
expect(client.pendingPermissionCount).toBe(1);
|
|
1273
|
-
answerWith("allow");
|
|
1274
|
-
expect((await waiting).outcome.outcome).toBe("selected");
|
|
1275
|
-
});
|
|
1276
|
-
it("should not talk to the workspace on reconnect when nothing is waiting", () => {
|
|
1277
|
-
mcpBridge.emit("reconnected");
|
|
1278
|
-
expect(mcpBridge.sendNotification).not.toHaveBeenCalled();
|
|
1279
|
-
});
|
|
1280
|
-
it("should keep waiting when the re-send itself fails", async () => {
|
|
1281
|
-
const waiting = client.requestPermission(params());
|
|
1282
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(1));
|
|
1283
|
-
mcpBridge.sendNotification.mockRejectedValueOnce(new Error("still down"));
|
|
1284
|
-
mcpBridge.emit("reconnected");
|
|
1285
|
-
await vi.waitFor(() => expect(mcpBridge.sendNotification.mock.calls.length).toBeGreaterThan(1));
|
|
1286
|
-
// The next reconnect gets another go; giving up here would lose the turn.
|
|
1287
|
-
expect(client.pendingPermissionCount).toBe(1);
|
|
1288
|
-
client.cancelPendingPermissions("test over");
|
|
1289
|
-
await waiting;
|
|
1290
|
-
});
|
|
1291
|
-
it("should cancel only the pending permissions for the specified session", async () => {
|
|
1292
|
-
const waitSess1 = client.requestPermission(params({ sessionId: "sess-1", toolCall: { toolCallId: "call-1", title: "Bash" } }));
|
|
1293
|
-
const waitSess2 = client.requestPermission(params({ sessionId: "sess-2", toolCall: { toolCallId: "call-2", title: "Bash" } }));
|
|
1294
|
-
const waitNoSess = client.requestPermission(params({ sessionId: undefined, toolCall: { toolCallId: "call-3", title: "Bash" } }));
|
|
1295
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(3));
|
|
1296
|
-
client.cancelPendingPermissions("task cancelled", "sess-1");
|
|
1297
|
-
expect((await waitSess1).outcome.outcome).toBe("cancelled");
|
|
1298
|
-
expect(client.pendingPermissionCount).toBe(2);
|
|
1299
|
-
// Requests with undefined sessionId should NOT be cancelled by a session-scoped cancel
|
|
1300
|
-
mcpBridge.emit("verdict", { requestId: "sess-2:call-2", behavior: "allow" });
|
|
1301
|
-
expect((await waitSess2).outcome.outcome).toBe("selected");
|
|
1302
|
-
mcpBridge.emit("verdict", { requestId: "call-3", behavior: "allow" });
|
|
1303
|
-
expect((await waitNoSess).outcome.outcome).toBe("selected");
|
|
1304
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
1305
|
-
});
|
|
1306
|
-
it("should do nothing if sessionId does not match any waiting permissions", async () => {
|
|
1307
|
-
const waiting = client.requestPermission(params({ sessionId: "sess-1" }));
|
|
1308
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(1));
|
|
1309
|
-
client.cancelPendingPermissions("task cancelled", "sess-999");
|
|
1310
|
-
expect(client.pendingPermissionCount).toBe(1);
|
|
1311
|
-
answerWith("allow");
|
|
1312
|
-
expect((await waiting).outcome.outcome).toBe("selected");
|
|
1313
|
-
});
|
|
1314
|
-
it("should send session/cancel RPC before settling pending permissions", async () => {
|
|
1315
|
-
const callOrder = [];
|
|
1316
|
-
const cancelSession = vi.fn().mockImplementation(async () => {
|
|
1317
|
-
callOrder.push("cancelRPC");
|
|
1318
|
-
});
|
|
1319
|
-
client.setSessionCanceller(cancelSession);
|
|
1320
|
-
const waiting = client.requestPermission(params({ sessionId: "sess-1" })).then((res) => {
|
|
1321
|
-
callOrder.push("settledPermission");
|
|
1322
|
-
return res;
|
|
1323
|
-
});
|
|
1324
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(1));
|
|
1325
|
-
await client.cancelTurn("sess-1");
|
|
1326
|
-
const res = await waiting;
|
|
1327
|
-
expect(callOrder).toEqual(["cancelRPC", "settledPermission"]);
|
|
1328
|
-
expect(res.outcome.outcome).toBe("cancelled");
|
|
1329
|
-
expect(cancelSession).toHaveBeenCalledWith("sess-1");
|
|
1330
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
1331
|
-
});
|
|
1332
|
-
it("should settle waiting permissions even if session/cancel never returns", async () => {
|
|
1333
|
-
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1334
|
-
// An agent wedged on its stdin: the write goes out, nothing comes back.
|
|
1335
|
-
// The permissions must still settle or their queue slots are held forever.
|
|
1336
|
-
client.setSessionCanceller(() => new Promise(() => { }));
|
|
1337
|
-
const waiting = client.requestPermission(params({ sessionId: "sess-1" }));
|
|
1338
|
-
await vi.waitFor(() => expect(client.pendingPermissionCount).toBe(1));
|
|
1339
|
-
vi.useFakeTimers();
|
|
1340
|
-
try {
|
|
1341
|
-
const cancelling = client.cancelTurn("sess-1");
|
|
1342
|
-
await vi.advanceTimersByTimeAsync(5000);
|
|
1343
|
-
await cancelling;
|
|
1344
|
-
}
|
|
1345
|
-
finally {
|
|
1346
|
-
vi.useRealTimers();
|
|
1347
|
-
}
|
|
1348
|
-
expect((await waiting).outcome.outcome).toBe("cancelled");
|
|
1349
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
1350
|
-
errorSpy.mockRestore();
|
|
1351
|
-
});
|
|
1352
|
-
it("should handle cancelTurn with undefined sessionId or no canceller gracefully", async () => {
|
|
1353
|
-
await expect(client.cancelTurn(undefined)).resolves.toBeUndefined();
|
|
1354
|
-
const freshClient = new AgentRQACPClient(mcpBridge, () => "task-1");
|
|
1355
|
-
await expect(freshClient.cancelTurn("sess-1")).resolves.toBeUndefined();
|
|
1356
|
-
});
|
|
1357
|
-
it("should log error if cancelSession rejects during cancelTurn", async () => {
|
|
1358
|
-
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1359
|
-
client.setSessionCanceller(() => {
|
|
1360
|
-
throw new Error("cancel RPC failed");
|
|
1361
|
-
});
|
|
1362
|
-
await client.cancelTurn("sess-1");
|
|
1363
|
-
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to cancel turn for session sess-1:"), expect.any(Error));
|
|
1364
|
-
errorSpy.mockRestore();
|
|
1365
|
-
});
|
|
1366
|
-
it("should say nothing when there is nothing waiting to cancel", () => {
|
|
1367
|
-
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1368
|
-
client.cancelPendingPermissions("nothing doing");
|
|
1369
|
-
expect(errorSpy).not.toHaveBeenCalled();
|
|
1370
|
-
errorSpy.mockRestore();
|
|
1371
|
-
});
|
|
1372
|
-
});
|
|
1373
|
-
describe("session mode changes", () => {
|
|
1374
|
-
it("should hand a mode change to whoever is watching for it", async () => {
|
|
1375
|
-
const onMode = vi.fn();
|
|
1376
|
-
client.setModeChangeHandler(onMode);
|
|
1377
|
-
await client.sessionUpdate({
|
|
1378
|
-
sessionId: "sess-1",
|
|
1379
|
-
update: { sessionUpdate: "current_mode_update", currentModeId: "auto" },
|
|
1380
|
-
});
|
|
1381
|
-
expect(onMode).toHaveBeenCalledWith("sess-1", "auto");
|
|
1382
|
-
});
|
|
1383
|
-
it("should not mind a mode change nobody is watching for", async () => {
|
|
1384
|
-
await expect(client.sessionUpdate({
|
|
1385
|
-
sessionId: "sess-1",
|
|
1386
|
-
update: { sessionUpdate: "current_mode_update", currentModeId: "auto" },
|
|
1387
|
-
})).resolves.toBeUndefined();
|
|
1388
|
-
});
|
|
1389
|
-
});
|
|
1390
|
-
describe("reportStopReason", () => {
|
|
1391
|
-
function clientForTask(taskId) {
|
|
1392
|
-
return new AgentRQACPClient(mcpBridge, () => taskId);
|
|
1393
|
-
}
|
|
1394
|
-
it("should say nothing when the agent simply finished", async () => {
|
|
1395
|
-
await clientForTask("task-1").reportStopReason("sess-1", "end_turn");
|
|
1396
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
1397
|
-
});
|
|
1398
|
-
it("should tell the workspace when a turn was refused or cut short", async () => {
|
|
1399
|
-
const client = clientForTask("task-1");
|
|
1400
|
-
await client.reportStopReason("sess-1", "refusal");
|
|
1401
|
-
await client.reportStopReason("sess-1", "max_tokens");
|
|
1402
|
-
await client.reportStopReason("sess-1", "max_turn_requests");
|
|
1403
|
-
await client.reportStopReason("sess-1", "cancelled");
|
|
1404
|
-
const texts = mcpBridge.callTool.mock.calls.map((c) => c[1].text);
|
|
1405
|
-
expect(mcpBridge.callTool.mock.calls.every((c) => c[0] === "reply")).toBe(true);
|
|
1406
|
-
expect(texts[0]).toContain("refused");
|
|
1407
|
-
expect(texts[1]).toContain("ran out of output tokens");
|
|
1408
|
-
expect(texts[2]).toContain("model requests");
|
|
1409
|
-
expect(texts[3]).toContain("cancelled");
|
|
1410
|
-
});
|
|
1411
|
-
it("should still report a stop reason it does not recognise", async () => {
|
|
1412
|
-
await clientForTask("task-1").reportStopReason("sess-1", "something_new");
|
|
1413
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("reply", {
|
|
1414
|
-
chatId: "task-1",
|
|
1415
|
-
text: expect.stringContaining("something_new"),
|
|
1416
|
-
});
|
|
1417
|
-
});
|
|
1418
|
-
it("should skip a session with no task behind it", async () => {
|
|
1419
|
-
await clientForTask(undefined).reportStopReason("sess-1", "refusal");
|
|
1420
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
1421
|
-
});
|
|
1422
|
-
it("should survive a workspace that cannot be reached", async () => {
|
|
1423
|
-
mcpBridge.callTool.mockRejectedValue(new Error("offline"));
|
|
1424
|
-
await expect(clientForTask("task-1").reportStopReason("sess-1", "refusal")).resolves.toBeUndefined();
|
|
1425
|
-
});
|
|
1426
|
-
});
|
|
1427
|
-
describe("file operations", () => {
|
|
1428
|
-
it("should read text files", async () => {
|
|
1429
|
-
vi.mocked(path.resolve).mockReturnValue("/mock/path/file.txt");
|
|
1430
|
-
vi.mocked(fs.readFile).mockResolvedValue("file content");
|
|
1431
|
-
const response = await client.readTextFile({
|
|
1432
|
-
path: "file.txt",
|
|
1433
|
-
sessionId: "test-session",
|
|
1434
|
-
});
|
|
1435
|
-
expect(response.content).toBe("file content");
|
|
1436
|
-
});
|
|
1437
|
-
describe("line and limit windows", () => {
|
|
1438
|
-
const file = "one\ntwo\nthree\nfour\n";
|
|
1439
|
-
beforeEach(() => {
|
|
1440
|
-
vi.mocked(path.resolve).mockReturnValue("/mock/path/file.txt");
|
|
1441
|
-
vi.mocked(fs.readFile).mockResolvedValue(file);
|
|
1442
|
-
});
|
|
1443
|
-
const read = (line, limit) => client
|
|
1444
|
-
.readTextFile({ path: "file.txt", sessionId: "test-session", line, limit })
|
|
1445
|
-
.then((r) => r.content);
|
|
1446
|
-
it("returns the whole file when neither bound is given", async () => {
|
|
1447
|
-
await expect(read()).resolves.toBe(file);
|
|
1448
|
-
await expect(read(null, null)).resolves.toBe(file);
|
|
1449
|
-
});
|
|
1450
|
-
it("starts at the 1-based line the agent asked for", async () => {
|
|
1451
|
-
await expect(read(1)).resolves.toBe(file);
|
|
1452
|
-
await expect(read(3)).resolves.toBe("three\nfour\n");
|
|
1453
|
-
});
|
|
1454
|
-
it("stops after `limit` lines", async () => {
|
|
1455
|
-
await expect(read(null, 2)).resolves.toBe("one\ntwo\n");
|
|
1456
|
-
});
|
|
1457
|
-
it("honours both bounds together", async () => {
|
|
1458
|
-
await expect(read(2, 2)).resolves.toBe("two\nthree\n");
|
|
1459
|
-
});
|
|
1460
|
-
it("keeps the last line intact when the file has no trailing newline", async () => {
|
|
1461
|
-
vi.mocked(fs.readFile).mockResolvedValue("one\ntwo\nthree");
|
|
1462
|
-
await expect(read(3, 1)).resolves.toBe("three");
|
|
1463
|
-
await expect(read(2, 5)).resolves.toBe("two\nthree");
|
|
1464
|
-
});
|
|
1465
|
-
it("returns nothing when the window starts past the end of the file", async () => {
|
|
1466
|
-
await expect(read(99)).resolves.toBe("");
|
|
1467
|
-
await expect(read(99, 10)).resolves.toBe("");
|
|
1468
|
-
});
|
|
1469
|
-
it("returns nothing for a limit of zero or less", async () => {
|
|
1470
|
-
await expect(read(1, 0)).resolves.toBe("");
|
|
1471
|
-
await expect(read(1, -5)).resolves.toBe("");
|
|
1472
|
-
});
|
|
1473
|
-
it("clamps a start line below one back to the first line", async () => {
|
|
1474
|
-
await expect(read(0)).resolves.toBe(file);
|
|
1475
|
-
await expect(read(-3, 1)).resolves.toBe("one\n");
|
|
1476
|
-
});
|
|
1477
|
-
it("truncates fractional bounds instead of misaligning the window", async () => {
|
|
1478
|
-
await expect(read(2.9, 1.9)).resolves.toBe("two\n");
|
|
1479
|
-
});
|
|
1480
|
-
it("ignores bounds that are not usable numbers", async () => {
|
|
1481
|
-
await expect(read(Number.NaN, Number.NaN)).resolves.toBe(file);
|
|
1482
|
-
await expect(read(Number.POSITIVE_INFINITY, 2)).resolves.toBe("one\ntwo\n");
|
|
1483
|
-
});
|
|
1484
|
-
it("reads an empty file without complaint", async () => {
|
|
1485
|
-
vi.mocked(fs.readFile).mockResolvedValue("");
|
|
1486
|
-
await expect(read(1, 10)).resolves.toBe("");
|
|
1487
|
-
});
|
|
1488
|
-
});
|
|
1489
|
-
it("should throw error when reading file fails", async () => {
|
|
1490
|
-
vi.mocked(fs.readFile).mockRejectedValue(new Error("read failed"));
|
|
1491
|
-
await expect(client.readTextFile({ path: "fail.txt", sessionId: "test-session" })).rejects.toThrow("read failed");
|
|
1492
|
-
});
|
|
1493
|
-
it("should write text files", async () => {
|
|
1494
|
-
vi.mocked(path.resolve).mockReturnValue("/mock/path/file.txt");
|
|
1495
|
-
vi.mocked(path.dirname).mockReturnValue("/mock/path");
|
|
1496
|
-
vi.mocked(fs.mkdir).mockResolvedValue(undefined);
|
|
1497
|
-
vi.mocked(fs.writeFile).mockResolvedValue(undefined);
|
|
1498
|
-
await client.writeTextFile({
|
|
1499
|
-
path: "file.txt",
|
|
1500
|
-
content: "new content",
|
|
1501
|
-
sessionId: "test-session",
|
|
1502
|
-
});
|
|
1503
|
-
expect(fs.writeFile).toHaveBeenCalledWith("/mock/path/file.txt", "new content", "utf8");
|
|
1504
|
-
});
|
|
1505
|
-
it("should throw error when writing file fails", async () => {
|
|
1506
|
-
vi.mocked(fs.writeFile).mockRejectedValue(new Error("write failed"));
|
|
1507
|
-
await expect(client.writeTextFile({
|
|
1508
|
-
path: "fail.txt",
|
|
1509
|
-
content: "content",
|
|
1510
|
-
sessionId: "test-session",
|
|
1511
|
-
})).rejects.toThrow("write failed");
|
|
1512
|
-
});
|
|
1513
|
-
});
|
|
1514
|
-
describe("streaming telemetry", () => {
|
|
1515
|
-
/** Every telemetry notification the bridge was asked to send, in order. */
|
|
1516
|
-
function telemetrySent() {
|
|
1517
|
-
return mcpBridge.sendNotification.mock.calls
|
|
1518
|
-
.filter((c) => c[0] === "notifications/claude/channel/telemetry")
|
|
1519
|
-
.map((c) => c[1]);
|
|
1520
|
-
}
|
|
1521
|
-
function newClient(taskId = "task-123") {
|
|
1522
|
-
mcpBridge.callTool = vi
|
|
1523
|
-
.fn()
|
|
1524
|
-
.mockResolvedValue({ isError: false, content: [] });
|
|
1525
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
1526
|
-
return new AgentRQACPClient(mcpBridge, () => taskId);
|
|
1527
|
-
}
|
|
1528
|
-
function thought(text) {
|
|
1529
|
-
return {
|
|
1530
|
-
sessionId: "sess-1",
|
|
1531
|
-
update: {
|
|
1532
|
-
sessionUpdate: "agent_thought_chunk",
|
|
1533
|
-
content: { type: "text", text },
|
|
1534
|
-
},
|
|
1535
|
-
};
|
|
1536
|
-
}
|
|
1537
|
-
it("batches thought chunks into one block per boundary", async () => {
|
|
1538
|
-
const c = newClient();
|
|
1539
|
-
await c.sessionUpdate(thought("Let me "));
|
|
1540
|
-
await c.sessionUpdate(thought("check the config."));
|
|
1541
|
-
// Nothing goes out mid-block: one notification per token is unusable.
|
|
1542
|
-
expect(telemetrySent()).toHaveLength(0);
|
|
1543
|
-
await c.sessionUpdate({
|
|
1544
|
-
sessionId: "sess-1",
|
|
1545
|
-
update: {
|
|
1546
|
-
sessionUpdate: "tool_call",
|
|
1547
|
-
title: "read_file",
|
|
1548
|
-
status: "pending",
|
|
1549
|
-
toolCallId: "tc-1",
|
|
1550
|
-
},
|
|
1551
|
-
});
|
|
1552
|
-
await c.flushReply("sess-1");
|
|
1553
|
-
const thoughts = telemetrySent().filter((p) => p.kind === "thought");
|
|
1554
|
-
expect(thoughts).toHaveLength(1);
|
|
1555
|
-
expect(thoughts[0]).toMatchObject({
|
|
1556
|
-
task_id: "task-123",
|
|
1557
|
-
session_id: "sess-1",
|
|
1558
|
-
kind: "thought",
|
|
1559
|
-
text: "Let me check the config.",
|
|
1560
|
-
});
|
|
1561
|
-
});
|
|
1562
|
-
it("closes off a reasoning block when the agent starts answering", async () => {
|
|
1563
|
-
const c = newClient();
|
|
1564
|
-
await c.sessionUpdate(thought("Thinking first."));
|
|
1565
|
-
await c.sessionUpdate({
|
|
1566
|
-
sessionId: "sess-1",
|
|
1567
|
-
update: {
|
|
1568
|
-
sessionUpdate: "agent_message_chunk",
|
|
1569
|
-
content: { type: "text", text: "The answer." },
|
|
1570
|
-
},
|
|
1571
|
-
});
|
|
1572
|
-
await c.sessionUpdate(thought("Second thought."));
|
|
1573
|
-
await c.flushReply("sess-1");
|
|
1574
|
-
expect(telemetrySent()
|
|
1575
|
-
.filter((p) => p.kind === "thought")
|
|
1576
|
-
.map((p) => p.text)).toEqual(["Thinking first.", "Second thought."]);
|
|
1577
|
-
});
|
|
1578
|
-
it("keeps reasoning out of the reply the human sees", async () => {
|
|
1579
|
-
const c = newClient();
|
|
1580
|
-
await c.sessionUpdate(thought("Internal reasoning."));
|
|
1581
|
-
await c.sessionUpdate({
|
|
1582
|
-
sessionId: "sess-1",
|
|
1583
|
-
update: {
|
|
1584
|
-
sessionUpdate: "agent_message_chunk",
|
|
1585
|
-
content: { type: "text", text: "Done." },
|
|
1586
|
-
},
|
|
1587
|
-
});
|
|
1588
|
-
await c.flushReply("sess-1");
|
|
1589
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("reply", {
|
|
1590
|
-
chatId: "task-123",
|
|
1591
|
-
text: "Done.",
|
|
1592
|
-
});
|
|
1593
|
-
});
|
|
1594
|
-
it("ignores non-text thought chunks", async () => {
|
|
1595
|
-
const c = newClient();
|
|
1596
|
-
await c.sessionUpdate({
|
|
1597
|
-
sessionId: "sess-1",
|
|
1598
|
-
update: {
|
|
1599
|
-
sessionUpdate: "agent_thought_chunk",
|
|
1600
|
-
content: { type: "image", data: "…" },
|
|
1601
|
-
},
|
|
1602
|
-
});
|
|
1603
|
-
await c.flushReply("sess-1");
|
|
1604
|
-
expect(telemetrySent()).toHaveLength(0);
|
|
1605
|
-
});
|
|
1606
|
-
it("does not report whitespace-only reasoning", async () => {
|
|
1607
|
-
const c = newClient();
|
|
1608
|
-
await c.sessionUpdate(thought(" \n "));
|
|
1609
|
-
await c.flushReply("sess-1");
|
|
1610
|
-
expect(telemetrySent()).toHaveLength(0);
|
|
1611
|
-
});
|
|
1612
|
-
it("reports a legacy plan update as soon as it arrives", async () => {
|
|
1613
|
-
const c = newClient();
|
|
1614
|
-
await c.sessionUpdate({
|
|
1615
|
-
sessionId: "sess-1",
|
|
1616
|
-
update: {
|
|
1617
|
-
sessionUpdate: "plan",
|
|
1618
|
-
entries: [
|
|
1619
|
-
{ content: "Read the config", priority: "high", status: "completed" },
|
|
1620
|
-
{ content: "Add tests", priority: "low", status: "pending" },
|
|
1621
|
-
],
|
|
1622
|
-
},
|
|
1623
|
-
});
|
|
1624
|
-
// Plans are queued, not awaited on the stream's hot path.
|
|
1625
|
-
await c.flushReply("sess-1");
|
|
1626
|
-
expect(telemetrySent()).toEqual([
|
|
1627
|
-
{
|
|
1628
|
-
task_id: "task-123",
|
|
1629
|
-
session_id: "sess-1",
|
|
1630
|
-
kind: "plan",
|
|
1631
|
-
text: "- ✅ Read the config\n- ⬜ Add tests",
|
|
1632
|
-
data: {
|
|
1633
|
-
planId: "default",
|
|
1634
|
-
planType: "items",
|
|
1635
|
-
entries: [
|
|
1636
|
-
{
|
|
1637
|
-
content: "Read the config",
|
|
1638
|
-
priority: "high",
|
|
1639
|
-
status: "completed",
|
|
1640
|
-
},
|
|
1641
|
-
{ content: "Add tests", priority: "low", status: "pending" },
|
|
1642
|
-
],
|
|
1643
|
-
},
|
|
1644
|
-
},
|
|
1645
|
-
]);
|
|
1646
|
-
});
|
|
1647
|
-
it("reports an ID-keyed plan update", async () => {
|
|
1648
|
-
const c = newClient();
|
|
1649
|
-
await c.sessionUpdate({
|
|
1650
|
-
sessionId: "sess-1",
|
|
1651
|
-
update: {
|
|
1652
|
-
sessionUpdate: "plan_update",
|
|
1653
|
-
plan: {
|
|
1654
|
-
type: "markdown",
|
|
1655
|
-
planId: "plan-7",
|
|
1656
|
-
content: "## Steps\n1. Ship it",
|
|
1657
|
-
},
|
|
1658
|
-
},
|
|
1659
|
-
});
|
|
1660
|
-
await c.flushReply("sess-1");
|
|
1661
|
-
expect(telemetrySent()[0]).toMatchObject({
|
|
1662
|
-
kind: "plan",
|
|
1663
|
-
text: "## Steps\n1. Ship it",
|
|
1664
|
-
data: {
|
|
1665
|
-
planId: "plan-7",
|
|
1666
|
-
planType: "markdown",
|
|
1667
|
-
content: "## Steps\n1. Ship it",
|
|
1668
|
-
},
|
|
1669
|
-
});
|
|
1670
|
-
});
|
|
1671
|
-
it("reports a withdrawn plan so the human sees it was dropped", async () => {
|
|
1672
|
-
const c = newClient();
|
|
1673
|
-
await c.sessionUpdate({
|
|
1674
|
-
sessionId: "sess-1",
|
|
1675
|
-
update: { sessionUpdate: "plan_removed", planId: "plan-7" },
|
|
1676
|
-
});
|
|
1677
|
-
await c.flushReply("sess-1");
|
|
1678
|
-
expect(telemetrySent()[0]).toMatchObject({
|
|
1679
|
-
kind: "plan",
|
|
1680
|
-
text: "Plan withdrawn.",
|
|
1681
|
-
data: { planId: "plan-7", removed: true },
|
|
1682
|
-
});
|
|
1683
|
-
});
|
|
1684
|
-
it("puts reasoning in front of the plan it explains", async () => {
|
|
1685
|
-
const c = newClient();
|
|
1686
|
-
await c.sessionUpdate(thought("I should plan this out."));
|
|
1687
|
-
await c.sessionUpdate({
|
|
1688
|
-
sessionId: "sess-1",
|
|
1689
|
-
update: { sessionUpdate: "plan", entries: [] },
|
|
1690
|
-
});
|
|
1691
|
-
await c.sessionUpdate({
|
|
1692
|
-
sessionId: "sess-1",
|
|
1693
|
-
update: { sessionUpdate: "plan_removed", planId: "default" },
|
|
1694
|
-
});
|
|
1695
|
-
await c.flushReply("sess-1");
|
|
1696
|
-
expect(telemetrySent().map((p) => p.kind)).toEqual([
|
|
1697
|
-
"thought",
|
|
1698
|
-
"plan",
|
|
1699
|
-
"plan",
|
|
1700
|
-
]);
|
|
1701
|
-
});
|
|
1702
|
-
it("reports only the last usage snapshot, once the turn is over", async () => {
|
|
1703
|
-
const c = newClient();
|
|
1704
|
-
await c.sessionUpdate({
|
|
1705
|
-
sessionId: "sess-1",
|
|
1706
|
-
update: { sessionUpdate: "usage_update", used: 100, size: 200_000 },
|
|
1707
|
-
});
|
|
1708
|
-
await c.sessionUpdate({
|
|
1709
|
-
sessionId: "sess-1",
|
|
1710
|
-
update: {
|
|
1711
|
-
sessionUpdate: "usage_update",
|
|
1712
|
-
used: 50_000,
|
|
1713
|
-
size: 200_000,
|
|
1714
|
-
cost: { amount: 0.42, currency: "USD" },
|
|
1715
|
-
},
|
|
1716
|
-
});
|
|
1717
|
-
// Each snapshot supersedes the last, so none go out mid-turn.
|
|
1718
|
-
expect(telemetrySent()).toHaveLength(0);
|
|
1719
|
-
await c.flushReply("sess-1");
|
|
1720
|
-
expect(telemetrySent()).toEqual([
|
|
1721
|
-
{
|
|
1722
|
-
task_id: "task-123",
|
|
1723
|
-
session_id: "sess-1",
|
|
1724
|
-
kind: "usage",
|
|
1725
|
-
text: "Context 50,000 / 200,000 tokens (25%) · 0.42 USD",
|
|
1726
|
-
data: {
|
|
1727
|
-
used: 50_000,
|
|
1728
|
-
size: 200_000,
|
|
1729
|
-
percent: 25,
|
|
1730
|
-
cost: { amount: 0.42, currency: "USD" },
|
|
1731
|
-
},
|
|
1732
|
-
},
|
|
1733
|
-
]);
|
|
1734
|
-
});
|
|
1735
|
-
it("does not report the same usage snapshot on a later turn", async () => {
|
|
1736
|
-
const c = newClient();
|
|
1737
|
-
await c.sessionUpdate({
|
|
1738
|
-
sessionId: "sess-1",
|
|
1739
|
-
update: { sessionUpdate: "usage_update", used: 100, size: 200_000 },
|
|
1740
|
-
});
|
|
1741
|
-
await c.flushReply("sess-1");
|
|
1742
|
-
mcpBridge.sendNotification.mockClear();
|
|
1743
|
-
await c.flushReply("sess-1");
|
|
1744
|
-
expect(telemetrySent()).toHaveLength(0);
|
|
1745
|
-
});
|
|
1746
|
-
it("orders reasoning, the reply, then the usage footer", async () => {
|
|
1747
|
-
const c = newClient();
|
|
1748
|
-
const order = [];
|
|
1749
|
-
mcpBridge.sendNotification = vi.fn(async (_m, p) => {
|
|
1750
|
-
order.push(p.kind);
|
|
1751
|
-
});
|
|
1752
|
-
mcpBridge.callTool = vi.fn(async () => {
|
|
1753
|
-
order.push("reply");
|
|
1754
|
-
return { isError: false, content: [] };
|
|
1755
|
-
});
|
|
1756
|
-
await c.sessionUpdate(thought("Reasoning."));
|
|
1757
|
-
await c.sessionUpdate({
|
|
1758
|
-
sessionId: "sess-1",
|
|
1759
|
-
update: {
|
|
1760
|
-
sessionUpdate: "agent_message_chunk",
|
|
1761
|
-
content: { type: "text", text: "Answer." },
|
|
1762
|
-
},
|
|
1763
|
-
});
|
|
1764
|
-
await c.sessionUpdate({
|
|
1765
|
-
sessionId: "sess-1",
|
|
1766
|
-
update: { sessionUpdate: "usage_update", used: 1, size: 100 },
|
|
1767
|
-
});
|
|
1768
|
-
await c.flushReply("sess-1");
|
|
1769
|
-
expect(order).toEqual(["thought", "reply", "usage"]);
|
|
1770
|
-
});
|
|
1771
|
-
it("drops telemetry when the session has no task to attach it to", async () => {
|
|
1772
|
-
setVerbose(true);
|
|
1773
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1774
|
-
const c = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
1775
|
-
await c.sessionUpdate(thought("Nowhere to put this."));
|
|
1776
|
-
await c.flushReply("sess-1");
|
|
1777
|
-
expect(telemetrySent()).toHaveLength(0);
|
|
1778
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("dropping thought telemetry"));
|
|
1779
|
-
consoleSpy.mockRestore();
|
|
1780
|
-
});
|
|
1781
|
-
it("still delivers the reply when telemetry cannot be sent", async () => {
|
|
1782
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1783
|
-
const c = newClient();
|
|
1784
|
-
mcpBridge.sendNotification.mockRejectedValue(new Error("offline"));
|
|
1785
|
-
await c.sessionUpdate(thought("Reasoning."));
|
|
1786
|
-
await c.sessionUpdate({
|
|
1787
|
-
sessionId: "sess-1",
|
|
1788
|
-
update: {
|
|
1789
|
-
sessionUpdate: "agent_message_chunk",
|
|
1790
|
-
content: { type: "text", text: "Answer." },
|
|
1791
|
-
},
|
|
1792
|
-
});
|
|
1793
|
-
await c.flushReply("sess-1");
|
|
1794
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("reply", {
|
|
1795
|
-
chatId: "task-123",
|
|
1796
|
-
text: "Answer.",
|
|
1797
|
-
});
|
|
1798
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to send thought telemetry"), expect.anything());
|
|
1799
|
-
consoleSpy.mockRestore();
|
|
1800
|
-
});
|
|
1801
|
-
it("keeps sending telemetry after one send has failed", async () => {
|
|
1802
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1803
|
-
const c = newClient();
|
|
1804
|
-
mcpBridge.sendNotification
|
|
1805
|
-
.mockRejectedValueOnce(new Error("offline"))
|
|
1806
|
-
.mockResolvedValue(undefined);
|
|
1807
|
-
await c.sessionUpdate(thought("First block."));
|
|
1808
|
-
await c.sessionUpdate({
|
|
1809
|
-
sessionId: "sess-1",
|
|
1810
|
-
update: {
|
|
1811
|
-
sessionUpdate: "tool_call",
|
|
1812
|
-
title: "read_file",
|
|
1813
|
-
status: "pending",
|
|
1814
|
-
toolCallId: "tc-1",
|
|
1815
|
-
},
|
|
1816
|
-
});
|
|
1817
|
-
await c.sessionUpdate(thought("Second block."));
|
|
1818
|
-
await c.flushReply("sess-1");
|
|
1819
|
-
expect(telemetrySent().map((p) => p.text)).toEqual([
|
|
1820
|
-
"First block.",
|
|
1821
|
-
"Second block.",
|
|
1822
|
-
]);
|
|
1823
|
-
consoleSpy.mockRestore();
|
|
1824
|
-
});
|
|
1825
|
-
it("survives a queued send that throws outright", async () => {
|
|
1826
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1827
|
-
// Looking up the task is the gateway's own callback, and it is the one
|
|
1828
|
-
// step outside sendTelemetry's own error handling.
|
|
1829
|
-
const c = new AgentRQACPClient(mcpBridge, () => {
|
|
1830
|
-
throw new Error("task lookup blew up");
|
|
1831
|
-
});
|
|
1832
|
-
await c.sessionUpdate(thought("Reasoning."));
|
|
1833
|
-
await expect(c.flushReply("sess-1")).resolves.toBeUndefined();
|
|
1834
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Telemetry send failed"), expect.anything());
|
|
1835
|
-
consoleSpy.mockRestore();
|
|
1836
|
-
});
|
|
1837
|
-
it("keeps each session's telemetry to itself", async () => {
|
|
1838
|
-
const c = new AgentRQACPClient(mcpBridge, (sessionId) => `task-${sessionId}`);
|
|
1839
|
-
await c.sessionUpdate(thought("Session one."));
|
|
1840
|
-
await c.sessionUpdate({
|
|
1841
|
-
sessionId: "sess-2",
|
|
1842
|
-
update: {
|
|
1843
|
-
sessionUpdate: "agent_thought_chunk",
|
|
1844
|
-
content: { type: "text", text: "Session two." },
|
|
1845
|
-
},
|
|
1846
|
-
});
|
|
1847
|
-
await c.flushReply("sess-2");
|
|
1848
|
-
expect(telemetrySent()).toHaveLength(1);
|
|
1849
|
-
expect(telemetrySent()[0]).toMatchObject({
|
|
1850
|
-
task_id: "task-sess-2",
|
|
1851
|
-
text: "Session two.",
|
|
1852
|
-
});
|
|
1853
|
-
});
|
|
1854
|
-
});
|
|
1855
|
-
describe("models support", () => {
|
|
1856
|
-
it("forwards models to workspace on config_option_update", async () => {
|
|
1857
|
-
const c = new AgentRQACPClient(mcpBridge, (sessionId) => `task-${sessionId}`);
|
|
1858
|
-
await c.sessionUpdate({
|
|
1859
|
-
sessionId: "sess-1",
|
|
1860
|
-
update: {
|
|
1861
|
-
sessionUpdate: "config_option_update",
|
|
1862
|
-
configOptions: [
|
|
1863
|
-
{
|
|
1864
|
-
id: "model",
|
|
1865
|
-
name: "Model",
|
|
1866
|
-
type: "select",
|
|
1867
|
-
currentValue: "claude-3-7-sonnet",
|
|
1868
|
-
options: [
|
|
1869
|
-
{ value: "claude-3-7-sonnet", name: "Claude 3.7 Sonnet" },
|
|
1870
|
-
{ value: "claude-3-5-haiku", name: "Claude 3.5 Haiku" },
|
|
1871
|
-
],
|
|
1872
|
-
},
|
|
1873
|
-
],
|
|
1874
|
-
},
|
|
1875
|
-
});
|
|
1876
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/models", expect.objectContaining({
|
|
1877
|
-
task_id: "task-sess-1",
|
|
1878
|
-
session_id: "sess-1",
|
|
1879
|
-
config_id: "model",
|
|
1880
|
-
current_model: "claude-3-7-sonnet",
|
|
1881
|
-
models: [
|
|
1882
|
-
expect.objectContaining({
|
|
1883
|
-
id: "claude-3-7-sonnet",
|
|
1884
|
-
model_id: "claude-3-7-sonnet",
|
|
1885
|
-
name: "Claude 3.7 Sonnet",
|
|
1886
|
-
current: true,
|
|
1887
|
-
}),
|
|
1888
|
-
expect.objectContaining({
|
|
1889
|
-
id: "claude-3-5-haiku",
|
|
1890
|
-
model_id: "claude-3-5-haiku",
|
|
1891
|
-
name: "Claude 3.5 Haiku",
|
|
1892
|
-
current: false,
|
|
1893
|
-
}),
|
|
1894
|
-
],
|
|
1895
|
-
available_models: expect.any(Array),
|
|
1896
|
-
can_set: true,
|
|
1897
|
-
}));
|
|
1898
|
-
});
|
|
1899
|
-
it("includes wire format aliases (model_id, modelId, value, available_models) in models payload", async () => {
|
|
1900
|
-
const c = new AgentRQACPClient(mcpBridge, (sessionId) => `task-${sessionId}`);
|
|
1901
|
-
await c.sendModelsToWorkspace("sess-test", {
|
|
1902
|
-
configId: "model",
|
|
1903
|
-
currentModelId: "gpt-4",
|
|
1904
|
-
models: [
|
|
1905
|
-
{ id: "gpt-4", name: "GPT-4", current: true, description: "Smart" },
|
|
1906
|
-
{ id: "gpt-3.5", name: "GPT-3.5", current: false },
|
|
1907
|
-
],
|
|
1908
|
-
});
|
|
1909
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/models", expect.objectContaining({
|
|
1910
|
-
task_id: "task-sess-test",
|
|
1911
|
-
session_id: "sess-test",
|
|
1912
|
-
config_id: "model",
|
|
1913
|
-
current_model: "gpt-4",
|
|
1914
|
-
current_model_id: "gpt-4",
|
|
1915
|
-
can_set: true,
|
|
1916
|
-
models: [
|
|
1917
|
-
expect.objectContaining({
|
|
1918
|
-
id: "gpt-4",
|
|
1919
|
-
model_id: "gpt-4",
|
|
1920
|
-
modelId: "gpt-4",
|
|
1921
|
-
value: "gpt-4",
|
|
1922
|
-
name: "GPT-4",
|
|
1923
|
-
label: "GPT-4",
|
|
1924
|
-
title: "GPT-4",
|
|
1925
|
-
current: true,
|
|
1926
|
-
is_current: true,
|
|
1927
|
-
isCurrent: true,
|
|
1928
|
-
}),
|
|
1929
|
-
expect.objectContaining({
|
|
1930
|
-
id: "gpt-3.5",
|
|
1931
|
-
model_id: "gpt-3.5",
|
|
1932
|
-
modelId: "gpt-3.5",
|
|
1933
|
-
value: "gpt-3.5",
|
|
1934
|
-
name: "GPT-3.5",
|
|
1935
|
-
label: "GPT-3.5",
|
|
1936
|
-
title: "GPT-3.5",
|
|
1937
|
-
current: false,
|
|
1938
|
-
is_current: false,
|
|
1939
|
-
isCurrent: false,
|
|
1940
|
-
}),
|
|
1941
|
-
],
|
|
1942
|
-
available_models: expect.any(Array),
|
|
1943
|
-
availableModels: expect.any(Array),
|
|
1944
|
-
}));
|
|
1945
|
-
});
|
|
1946
|
-
it("ignores config_option_update if no model options exist", async () => {
|
|
1947
|
-
const c = new AgentRQACPClient(mcpBridge, (sessionId) => `task-${sessionId}`);
|
|
1948
|
-
await c.sessionUpdate({
|
|
1949
|
-
sessionId: "sess-1",
|
|
1950
|
-
update: {
|
|
1951
|
-
sessionUpdate: "config_option_update",
|
|
1952
|
-
configOptions: [
|
|
1953
|
-
{
|
|
1954
|
-
id: "theme",
|
|
1955
|
-
name: "Theme",
|
|
1956
|
-
type: "select",
|
|
1957
|
-
currentValue: "dark",
|
|
1958
|
-
options: [{ value: "dark", name: "Dark" }],
|
|
1959
|
-
},
|
|
1960
|
-
],
|
|
1961
|
-
},
|
|
1962
|
-
});
|
|
1963
|
-
expect(mcpBridge.sendNotification).not.toHaveBeenCalledWith("notifications/claude/channel/models", expect.anything());
|
|
1964
|
-
});
|
|
1965
|
-
it("sends the models even when no task is attached to the session", async () => {
|
|
1966
|
-
// The session opened at startup has no task, and it is the one that lets
|
|
1967
|
-
// a workspace say what its agent offers before any work arrives. The
|
|
1968
|
-
// workspace keys this to the connection and ignores the task id.
|
|
1969
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1970
|
-
const c = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
1971
|
-
await c.sendModelsToWorkspace("sess-notask", {
|
|
1972
|
-
configId: "model",
|
|
1973
|
-
currentModelId: "gpt-4",
|
|
1974
|
-
models: [{ id: "gpt-4", name: "GPT-4", current: true }],
|
|
1975
|
-
});
|
|
1976
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/models", expect.objectContaining({ task_id: "", session_id: "sess-notask", config_id: "model" }));
|
|
1977
|
-
consoleSpy.mockRestore();
|
|
1978
|
-
});
|
|
1979
|
-
it("handles error when sending models notification fails", async () => {
|
|
1980
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1981
|
-
mcpBridge.sendNotification.mockRejectedValue(new Error("network error"));
|
|
1982
|
-
const c = new AgentRQACPClient(mcpBridge, (sessionId) => `task-${sessionId}`);
|
|
1983
|
-
await c.sendModelsToWorkspace("sess-1", {
|
|
1984
|
-
configId: "model",
|
|
1985
|
-
currentModelId: "gpt-4",
|
|
1986
|
-
models: [{ id: "gpt-4", name: "GPT-4", current: true }],
|
|
1987
|
-
});
|
|
1988
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to send models notification for session sess-1:"), expect.anything());
|
|
1989
|
-
consoleSpy.mockRestore();
|
|
1990
|
-
});
|
|
1991
|
-
it("reports a list with nothing selected yet", async () => {
|
|
1992
|
-
setVerbose(true);
|
|
1993
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
1994
|
-
const c = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
1995
|
-
await c.sendModelsToWorkspace("sess-unset", {
|
|
1996
|
-
configId: "model",
|
|
1997
|
-
models: [{ id: "gpt-4", name: "GPT-4", current: false }],
|
|
1998
|
-
});
|
|
1999
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/models", expect.objectContaining({ current_model: undefined }));
|
|
2000
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("current: none"));
|
|
2001
|
-
consoleSpy.mockRestore();
|
|
2002
|
-
});
|
|
2003
|
-
it("remembers what a session was on after the client that served it is gone", async () => {
|
|
2004
|
-
// A set_model can arrive for a session whose agent has already exited. The
|
|
2005
|
-
// record has to survive that client, or the one case that most needs an
|
|
2006
|
-
// answer — a picker left pending on a session that no longer exists — is
|
|
2007
|
-
// the one case that cannot be answered.
|
|
2008
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
2009
|
-
const models = {
|
|
2010
|
-
configId: "model",
|
|
2011
|
-
currentModelId: "gpt-4",
|
|
2012
|
-
models: [{ id: "gpt-4", name: "GPT-4", current: true }],
|
|
2013
|
-
};
|
|
2014
|
-
let dead = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
2015
|
-
await dead.sendModelsToWorkspace("sess-outlives", models);
|
|
2016
|
-
dead = undefined;
|
|
2017
|
-
expect(lastModelsForSession("sess-outlives")).toEqual(models);
|
|
2018
|
-
// And any later client sees it too, which is what lets a live session
|
|
2019
|
-
// answer for one that has ended.
|
|
2020
|
-
const fresh = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
2021
|
-
expect(fresh.lastModelsFor("sess-outlives")).toEqual(models);
|
|
2022
|
-
consoleSpy.mockRestore();
|
|
2023
|
-
});
|
|
2024
|
-
});
|
|
2025
|
-
describe("available_commands_update", () => {
|
|
2026
|
-
/** Silences the informational logging these paths write to stderr. */
|
|
2027
|
-
function quiet() {
|
|
2028
|
-
return vi.spyOn(console, "error").mockImplementation(() => { });
|
|
2029
|
-
}
|
|
2030
|
-
// No default parameter on purpose: `clientWithTask()` would take
|
|
2031
|
-
// the default rather than the no-task case the test is asking for.
|
|
2032
|
-
function clientWithTask(taskId) {
|
|
2033
|
-
return new AgentRQACPClient(mcpBridge, () => taskId);
|
|
2034
|
-
}
|
|
2035
|
-
async function advertise(client, availableCommands, sessionId = "sess-1") {
|
|
2036
|
-
await client.sessionUpdate({
|
|
2037
|
-
sessionId,
|
|
2038
|
-
update: { sessionUpdate: "available_commands_update", availableCommands },
|
|
2039
|
-
});
|
|
2040
|
-
}
|
|
2041
|
-
it("forwards the agent's commands to the workspace", async () => {
|
|
2042
|
-
const consoleSpy = quiet();
|
|
2043
|
-
await advertise(clientWithTask("task-123"), [
|
|
2044
|
-
{ name: "compact", description: "Shorten the context" },
|
|
2045
|
-
{ name: "web", description: "Search the web", input: { hint: "query" } },
|
|
2046
|
-
]);
|
|
2047
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/commands", {
|
|
2048
|
-
task_id: "task-123",
|
|
2049
|
-
session_id: "sess-1",
|
|
2050
|
-
commands: [
|
|
2051
|
-
{ name: "compact", description: "Shorten the context" },
|
|
2052
|
-
{ name: "web", description: "Search the web", hint: "query" },
|
|
2053
|
-
],
|
|
2054
|
-
});
|
|
2055
|
-
consoleSpy.mockRestore();
|
|
2056
|
-
});
|
|
2057
|
-
it("forwards an empty list, which is how an agent withdraws its commands", async () => {
|
|
2058
|
-
// Dropping this would leave the workspace offering a menu of commands
|
|
2059
|
-
// the agent has stopped accepting.
|
|
2060
|
-
const consoleSpy = quiet();
|
|
2061
|
-
await advertise(clientWithTask("task-123"), []);
|
|
2062
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/commands", { task_id: "task-123", session_id: "sess-1", commands: [] });
|
|
2063
|
-
consoleSpy.mockRestore();
|
|
2064
|
-
});
|
|
2065
|
-
it("sends the commands even when the session has no task attached", async () => {
|
|
2066
|
-
// Same reason as the models: the startup session has no task, and it is
|
|
2067
|
-
// what lets the composer offer commands before any work has run.
|
|
2068
|
-
const consoleSpy = quiet();
|
|
2069
|
-
await advertise(clientWithTask(), [{ name: "init", description: "d" }]);
|
|
2070
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/commands", expect.objectContaining({
|
|
2071
|
-
task_id: "",
|
|
2072
|
-
session_id: "sess-1",
|
|
2073
|
-
commands: [{ name: "init", description: "d" }],
|
|
2074
|
-
}));
|
|
2075
|
-
consoleSpy.mockRestore();
|
|
2076
|
-
});
|
|
2077
|
-
it("does not let a failed send stall the agent's turn", async () => {
|
|
2078
|
-
const consoleSpy = quiet();
|
|
2079
|
-
mcpBridge.sendNotification.mockRejectedValueOnce(new Error("workspace unreachable"));
|
|
2080
|
-
await expect(advertise(clientWithTask("task-123"), [{ name: "init", description: "d" }])).resolves.toBeUndefined();
|
|
2081
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to send commands notification for session sess-1:"), expect.anything());
|
|
2082
|
-
consoleSpy.mockRestore();
|
|
2083
|
-
});
|
|
2084
|
-
});
|
|
2085
|
-
describe("sendAgentToWorkspace", () => {
|
|
2086
|
-
function quiet() {
|
|
2087
|
-
return vi.spyOn(console, "error").mockImplementation(() => { });
|
|
2088
|
-
}
|
|
2089
|
-
it("tells the workspace which agent is behind the gateway", async () => {
|
|
2090
|
-
const consoleSpy = quiet();
|
|
2091
|
-
const client = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2092
|
-
await client.sendAgentToWorkspace("sess-1", {
|
|
2093
|
-
name: "codex",
|
|
2094
|
-
title: "Codex CLI",
|
|
2095
|
-
version: "1.10.0",
|
|
2096
|
-
});
|
|
2097
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/agent", {
|
|
2098
|
-
task_id: "task-123",
|
|
2099
|
-
session_id: "sess-1",
|
|
2100
|
-
name: "codex",
|
|
2101
|
-
title: "Codex CLI",
|
|
2102
|
-
version: "1.10.0",
|
|
2103
|
-
});
|
|
2104
|
-
consoleSpy.mockRestore();
|
|
2105
|
-
});
|
|
2106
|
-
it("sends the name alone when that is all the agent gave", async () => {
|
|
2107
|
-
const consoleSpy = quiet();
|
|
2108
|
-
const client = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2109
|
-
await client.sendAgentToWorkspace("sess-1", { name: "gemini" });
|
|
2110
|
-
const payload = mcpBridge.sendNotification.mock.calls.at(-1)?.[1];
|
|
2111
|
-
expect(payload).toMatchObject({ name: "gemini" });
|
|
2112
|
-
expect(payload.title).toBeUndefined();
|
|
2113
|
-
expect(payload.version).toBeUndefined();
|
|
2114
|
-
consoleSpy.mockRestore();
|
|
2115
|
-
});
|
|
2116
|
-
it("sends even when the session has no task to attach to", async () => {
|
|
2117
|
-
// Unlike the models and commands beside it, this notification is not
|
|
2118
|
-
// about a task: the workspace keys it to the connection and ignores the
|
|
2119
|
-
// task id. Requiring one would keep the workspace ignorant of its agent
|
|
2120
|
-
// for exactly as long as nobody had given it work.
|
|
2121
|
-
const consoleSpy = quiet();
|
|
2122
|
-
const client = new AgentRQACPClient(mcpBridge, () => undefined);
|
|
2123
|
-
await client.sendAgentToWorkspace("sess-1", { name: "codex" });
|
|
2124
|
-
expect(mcpBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/agent", expect.objectContaining({ task_id: "", session_id: "sess-1", name: "codex" }));
|
|
2125
|
-
consoleSpy.mockRestore();
|
|
2126
|
-
});
|
|
2127
|
-
it("does not let a failed send stall the agent's turn", async () => {
|
|
2128
|
-
const consoleSpy = quiet();
|
|
2129
|
-
mcpBridge.sendNotification.mockRejectedValueOnce(new Error("workspace unreachable"));
|
|
2130
|
-
const client = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2131
|
-
await expect(client.sendAgentToWorkspace("sess-1", { name: "codex" })).resolves.toBeUndefined();
|
|
2132
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Failed to send agent notification:"), expect.anything());
|
|
2133
|
-
consoleSpy.mockRestore();
|
|
2134
|
-
});
|
|
2135
|
-
});
|
|
2136
|
-
describe("dispose and resource management", () => {
|
|
2137
|
-
it("removes verdict and reconnected listeners from mcpBridge on dispose", () => {
|
|
2138
|
-
const bridge = Object.assign(new EventEmitter(), {
|
|
2139
|
-
getSessionId: vi.fn().mockReturnValue("test-session"),
|
|
2140
|
-
sendNotification: vi.fn().mockResolvedValue(undefined),
|
|
2141
|
-
callTool: vi.fn(),
|
|
2142
|
-
});
|
|
2143
|
-
expect(bridge.listenerCount("verdict")).toBe(0);
|
|
2144
|
-
expect(bridge.listenerCount("reconnected")).toBe(0);
|
|
2145
|
-
const client = new AgentRQACPClient(bridge);
|
|
2146
|
-
expect(bridge.listenerCount("verdict")).toBe(1);
|
|
2147
|
-
expect(bridge.listenerCount("reconnected")).toBe(1);
|
|
2148
|
-
client.dispose();
|
|
2149
|
-
expect(bridge.listenerCount("verdict")).toBe(0);
|
|
2150
|
-
expect(bridge.listenerCount("reconnected")).toBe(0);
|
|
2151
|
-
});
|
|
2152
|
-
it("cancels pending permissions and aborts pending elicitations on dispose", async () => {
|
|
2153
|
-
const bridge = Object.assign(new EventEmitter(), {
|
|
2154
|
-
getSessionId: vi.fn().mockReturnValue("test-session"),
|
|
2155
|
-
sendNotification: vi.fn().mockResolvedValue(undefined),
|
|
2156
|
-
callTool: vi.fn(),
|
|
2157
|
-
});
|
|
2158
|
-
let promptSignal;
|
|
2159
|
-
const client = new AgentRQACPClient(bridge, () => undefined, {
|
|
2160
|
-
promptUrlElicitation: ({ signal }) => {
|
|
2161
|
-
promptSignal = signal;
|
|
2162
|
-
return new Promise(() => { });
|
|
2163
|
-
},
|
|
2164
|
-
});
|
|
2165
|
-
const permPromise = client.requestPermission({
|
|
2166
|
-
sessionId: "sess-1",
|
|
2167
|
-
toolCall: { toolCallId: "call-1" },
|
|
2168
|
-
options: [
|
|
2169
|
-
{ optionId: "allow", name: "Allow", kind: "allow" },
|
|
2170
|
-
{ optionId: "reject", name: "Reject", kind: "reject" },
|
|
2171
|
-
],
|
|
2172
|
-
});
|
|
2173
|
-
void client.createElicitation({
|
|
2174
|
-
mode: "url",
|
|
2175
|
-
url: "https://example.com/login",
|
|
2176
|
-
elicitationId: "elicit-1",
|
|
2177
|
-
message: "Sign in",
|
|
2178
|
-
});
|
|
2179
|
-
await vi.waitFor(() => {
|
|
2180
|
-
expect(client.pendingPermissionCount).toBe(1);
|
|
2181
|
-
expect(promptSignal).toBeDefined();
|
|
2182
|
-
});
|
|
2183
|
-
expect(promptSignal?.aborted).toBe(false);
|
|
2184
|
-
client.dispose();
|
|
2185
|
-
expect(client.pendingPermissionCount).toBe(0);
|
|
2186
|
-
expect(promptSignal?.aborted).toBe(true);
|
|
2187
|
-
const result = await permPromise;
|
|
2188
|
-
expect(result.outcome).toEqual({ outcome: "cancelled" });
|
|
2189
|
-
});
|
|
2190
|
-
it("bounds replyBuffers to prevent unbounded memory growth", async () => {
|
|
2191
|
-
const bridge = Object.assign(new EventEmitter(), {
|
|
2192
|
-
getSessionId: vi.fn().mockReturnValue("test-session"),
|
|
2193
|
-
sendNotification: vi.fn().mockResolvedValue(undefined),
|
|
2194
|
-
callTool: vi.fn(),
|
|
2195
|
-
});
|
|
2196
|
-
const client = new AgentRQACPClient(bridge, (sid) => "task-" + sid);
|
|
2197
|
-
for (let i = 0; i < 300; i++) {
|
|
2198
|
-
await client.sessionUpdate({
|
|
2199
|
-
sessionId: "sess-" + i,
|
|
2200
|
-
update: {
|
|
2201
|
-
sessionUpdate: "agent_message_chunk",
|
|
2202
|
-
content: { type: "text", text: "hello from " + i },
|
|
2203
|
-
},
|
|
2204
|
-
});
|
|
2205
|
-
}
|
|
2206
|
-
// sess-0 should have been evicted by BoundedMap(256)
|
|
2207
|
-
await client.flushReply("sess-0");
|
|
2208
|
-
expect(bridge.callTool).not.toHaveBeenCalledWith("reply", expect.objectContaining({ chatId: "task-sess-0" }));
|
|
2209
|
-
// sess-299 should still be present
|
|
2210
|
-
await client.flushReply("sess-299");
|
|
2211
|
-
expect(bridge.callTool).toHaveBeenCalledWith("reply", expect.objectContaining({
|
|
2212
|
-
chatId: "task-sess-299",
|
|
2213
|
-
text: "hello from 299",
|
|
2214
|
-
}));
|
|
2215
|
-
});
|
|
2216
|
-
});
|
|
2217
|
-
describe("the single task tool", () => {
|
|
2218
|
-
const TASK_SERVER = new Set(["task", "pano", "getWorkspace"]);
|
|
2219
|
-
beforeEach(() => {
|
|
2220
|
-
mcpBridge.getAdvertisedTools = vi.fn().mockReturnValue(TASK_SERVER);
|
|
2221
|
-
});
|
|
2222
|
-
function okResult(text = "ok") {
|
|
2223
|
-
return { isError: false, content: [{ type: "text", text }] };
|
|
2224
|
-
}
|
|
2225
|
-
it("forwards the agent's reply through task", async () => {
|
|
2226
|
-
mcpBridge.callTool.mockResolvedValue(okResult());
|
|
2227
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2228
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
2229
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello" } } });
|
|
2230
|
-
await c.flushReply("sess-1");
|
|
2231
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("task", {
|
|
2232
|
-
action: "reply",
|
|
2233
|
-
payload: { taskId: "task-123", text: "Hello" },
|
|
2234
|
-
});
|
|
2235
|
-
});
|
|
2236
|
-
it("reports a stop reason through task", async () => {
|
|
2237
|
-
mcpBridge.callTool.mockResolvedValue(okResult());
|
|
2238
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-1");
|
|
2239
|
-
await c.reportStopReason("sess-1", "refusal");
|
|
2240
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("task", {
|
|
2241
|
-
action: "reply",
|
|
2242
|
-
payload: { taskId: "task-1", text: expect.any(String) },
|
|
2243
|
-
});
|
|
2244
|
-
});
|
|
2245
|
-
it("creates, starts and asks through task when an elicitation has no task", async () => {
|
|
2246
|
-
mcpBridge.callTool.mockImplementation(async (_name, args) => {
|
|
2247
|
-
if (args.action === "create")
|
|
2248
|
-
return okResult("task created with id=T-new");
|
|
2249
|
-
if (args.action === "elicit")
|
|
2250
|
-
return okResult(JSON.stringify({ action: "decline" }));
|
|
2251
|
-
return okResult();
|
|
2252
|
-
});
|
|
2253
|
-
const params = {
|
|
2254
|
-
mode: "form",
|
|
2255
|
-
message: "Name?",
|
|
2256
|
-
requestedSchema: { type: "object", properties: { name: { type: "string" } } },
|
|
2257
|
-
};
|
|
2258
|
-
const response = await client.createElicitation(params);
|
|
2259
|
-
expect(mcpBridge.callTool.mock.calls).toEqual([
|
|
2260
|
-
["task", { action: "create", payload: { title: "Name?", body: "Name?", assignee: "human" } }],
|
|
2261
|
-
["task", { action: "updateStatus", payload: { taskId: "T-new", status: "ongoing" } }],
|
|
2262
|
-
["task", {
|
|
2263
|
-
action: "elicit",
|
|
2264
|
-
payload: { taskId: "T-new", message: "Name?", mode: "form", requestedSchema: params.requestedSchema },
|
|
2265
|
-
}],
|
|
2266
|
-
]);
|
|
2267
|
-
expect(response).toEqual({ action: "decline" });
|
|
2268
|
-
});
|
|
2269
|
-
it.each([
|
|
2270
|
-
["task (agentrq-0an2BXTfpGj MCP Server)", { action: "reply", payload: { taskId: "task-123", text: "Hello world" } }],
|
|
2271
|
-
["mcp__agentrq-workspace__task", { action: "reply", payload: { taskId: "task-123", text: "Hello world" } }],
|
|
2272
|
-
["task (agentrq-0an2BXTfpGj MCP Server)", { action: "reply", taskId: "task-123", text: "Hello world" }],
|
|
2273
|
-
["mcp__agentrq-workspace__reply", { chatId: "task-123", text: "Hello world" }],
|
|
2274
|
-
["agentrq-workspace_task", { action: "reply", payload: { taskId: "task-123", text: "Hello world" } }],
|
|
2275
|
-
])("does not repeat a reply the agent already sent as %s %j", async (title, rawInput) => {
|
|
2276
|
-
mcpBridge.callTool.mockResolvedValue(okResult());
|
|
2277
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2278
|
-
mcpBridge.getAdvertisedTools.mockReturnValue(new Set([...TASK_SERVER, "reply"]));
|
|
2279
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
2280
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello world" } } });
|
|
2281
|
-
await c.sessionUpdate({
|
|
2282
|
-
sessionId: "sess-1",
|
|
2283
|
-
update: { sessionUpdate: "tool_call", title, status: "completed", toolCallId: "tc-1", rawInput },
|
|
2284
|
-
});
|
|
2285
|
-
await c.flushReply("sess-1");
|
|
2286
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
2287
|
-
});
|
|
2288
|
-
it.each([
|
|
2289
|
-
["another action", { action: "get", payload: { taskId: "task-123" } }],
|
|
2290
|
-
["no task id", { action: "reply", payload: { text: "Hello world" } }],
|
|
2291
|
-
["a payload that is not an object", { action: "reply", payload: "Hello world" }],
|
|
2292
|
-
["no input", undefined],
|
|
2293
|
-
])("still replies when the agent's task call has %s", async (_case, rawInput) => {
|
|
2294
|
-
mcpBridge.callTool.mockResolvedValue(okResult());
|
|
2295
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2296
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
2297
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello world" } } });
|
|
2298
|
-
await c.sessionUpdate({
|
|
2299
|
-
sessionId: "sess-1",
|
|
2300
|
-
update: { sessionUpdate: "tool_call", title: "mcp__agentrq-workspace__task", status: "completed", toolCallId: "tc-1", rawInput },
|
|
2301
|
-
});
|
|
2302
|
-
await c.flushReply("sess-1");
|
|
2303
|
-
expect(mcpBridge.callTool).toHaveBeenCalledWith("task", {
|
|
2304
|
-
action: "reply",
|
|
2305
|
-
payload: { taskId: "task-123", text: "Hello world" },
|
|
2306
|
-
});
|
|
2307
|
-
});
|
|
2308
|
-
it.each([
|
|
2309
|
-
["mcp__agentrq-workspace__task", { action: "reply", payload: { taskId: "task-123" } }],
|
|
2310
|
-
["mcp__agentrq-workspace__reply", { chatId: "task-123" }],
|
|
2311
|
-
])("treats a %s reply with no text as empty, not as the agent's text", async (title, rawInput) => {
|
|
2312
|
-
mcpBridge.getAdvertisedTools.mockReturnValue(new Set([...TASK_SERVER, "reply"]));
|
|
2313
|
-
mcpBridge.callTool.mockResolvedValue(okResult());
|
|
2314
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2315
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
2316
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hello world" } } });
|
|
2317
|
-
await c.sessionUpdate({
|
|
2318
|
-
sessionId: "sess-1",
|
|
2319
|
-
update: { sessionUpdate: "tool_call", title, status: "completed", toolCallId: "tc-1", rawInput },
|
|
2320
|
-
});
|
|
2321
|
-
await c.flushReply("sess-1");
|
|
2322
|
-
expect(mcpBridge.callTool).toHaveBeenCalledTimes(1);
|
|
2323
|
-
});
|
|
2324
|
-
it("recognises task as the workspace's own when tools/list failed", async () => {
|
|
2325
|
-
mcpBridge.getAdvertisedTools.mockReturnValue(undefined);
|
|
2326
|
-
mcpBridge.callTool.mockResolvedValue(okResult());
|
|
2327
|
-
const c = new AgentRQACPClient(mcpBridge, () => "task-123");
|
|
2328
|
-
vi.spyOn(process.stdout, "write").mockImplementation(() => true);
|
|
2329
|
-
await c.sessionUpdate({ sessionId: "sess-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hi" } } });
|
|
2330
|
-
await c.sessionUpdate({
|
|
2331
|
-
sessionId: "sess-1",
|
|
2332
|
-
update: {
|
|
2333
|
-
sessionUpdate: "tool_call",
|
|
2334
|
-
title: "mcp__agentrq-workspace__task",
|
|
2335
|
-
status: "completed",
|
|
2336
|
-
toolCallId: "tc-1",
|
|
2337
|
-
rawInput: { action: "reply", payload: { taskId: "task-123", text: "Hi" } },
|
|
2338
|
-
},
|
|
2339
|
-
});
|
|
2340
|
-
await c.flushReply("sess-1");
|
|
2341
|
-
// Were task not on the known list, the call would go unrecognised and the reply repeat.
|
|
2342
|
-
expect(mcpBridge.callTool).not.toHaveBeenCalled();
|
|
2343
|
-
});
|
|
2344
|
-
});
|
|
2345
|
-
});
|
|
2346
|
-
//# sourceMappingURL=acpClient.test.js.map
|