quick-acp 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +405 -2
- package/dist/acp/agent.js +384 -0
- package/dist/acp-smoke.js +163 -0
- package/dist/concurrent-test.js +101 -0
- package/dist/doctor.js +86 -0
- package/dist/e2e.js +206 -0
- package/dist/fresh-machine-e2e.js +151 -0
- package/dist/inject-turn-test.js +119 -0
- package/dist/login-cli.js +86 -0
- package/dist/main.js +77 -0
- package/dist/no-outpost-e2e.js +86 -0
- package/dist/probe.js +308 -0
- package/dist/quick/auth.js +371 -0
- package/dist/quick/codec.js +499 -0
- package/dist/quick/credstore.js +439 -0
- package/dist/quick/gateway-contract.js +104 -0
- package/dist/quick/login.js +522 -0
- package/dist/quick/oidc.js +152 -0
- package/dist/quick/session-direct.js +200 -0
- package/dist/quick/session.js +399 -0
- package/dist/quick/transport.js +343 -0
- package/dist/session-id-test.js +95 -0
- package/dist/tools/registry.js +411 -0
- package/dist/tools/strategies.js +188 -0
- package/dist/wss-smoke.js +230 -0
- package/package.json +54 -5
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP agent implementation backed by the Quick gateway.
|
|
3
|
+
*
|
|
4
|
+
* This is the adaptor T3Code / codeg / any ACP client talks to. It speaks
|
|
5
|
+
* standard ACP over stdio and forwards turns to the Quick session.
|
|
6
|
+
*
|
|
7
|
+
* Mapping notes (each one is a real semantic loss, stated rather than hidden):
|
|
8
|
+
*
|
|
9
|
+
* - ACP `prompt` gives the full `content` of ONE new user turn, not history.
|
|
10
|
+
* That matches Quick's thread model exactly: each prompt is an increment.
|
|
11
|
+
* - ACP `newSession` maps to a fresh Quick thread.
|
|
12
|
+
* - ACP `setSessionMode` maps to Quick `agent_mode`; the mode list is
|
|
13
|
+
* Quick's ('fast'/'balanced'/'smart'), not a generic ask/code/architect set.
|
|
14
|
+
* - ACP `setSessionModel` is refused: Quick does not let a caller pick the
|
|
15
|
+
* model. Reporting a successful model switch would be a lie.
|
|
16
|
+
* - Tool calls are executed by this process and reported through
|
|
17
|
+
* `session/update` so the client renders them like any other agent's.
|
|
18
|
+
*/
|
|
19
|
+
import { RequestError } from '@zed-industries/agent-client-protocol';
|
|
20
|
+
import { resolveSession } from '../quick/auth.js';
|
|
21
|
+
import { QuickTransport } from '../quick/transport.js';
|
|
22
|
+
import { QuickChatSession, } from '../quick/session.js';
|
|
23
|
+
import { defaultPolicy, toolDefs, ToolExecutor } from '../tools/registry.js';
|
|
24
|
+
import { resolveToolMode, runWithInjectedTools } from '../tools/strategies.js';
|
|
25
|
+
/** ACP protocol revision this agent implements. */
|
|
26
|
+
const PROTOCOL_VERSION = 1;
|
|
27
|
+
export class QuickAcpAgent {
|
|
28
|
+
sessions = new Map();
|
|
29
|
+
sessionSeq = 0;
|
|
30
|
+
quickSession = null;
|
|
31
|
+
toolMode;
|
|
32
|
+
tools;
|
|
33
|
+
executor;
|
|
34
|
+
workspaceRoot;
|
|
35
|
+
policy;
|
|
36
|
+
conn = null;
|
|
37
|
+
constructor(opts = {}) {
|
|
38
|
+
this.workspaceRoot = opts.workspaceRoot ?? process.cwd();
|
|
39
|
+
const policy = defaultPolicy(this.workspaceRoot);
|
|
40
|
+
this.toolMode = resolveToolMode();
|
|
41
|
+
this.tools = toolDefs(policy);
|
|
42
|
+
this.executor = new ToolExecutor(policy);
|
|
43
|
+
this.policy = policy;
|
|
44
|
+
}
|
|
45
|
+
/** Called by main once the connection exists, so we can emit updates. */
|
|
46
|
+
attach(conn) {
|
|
47
|
+
this.conn = conn;
|
|
48
|
+
}
|
|
49
|
+
// -------------------------------------------------------------------------
|
|
50
|
+
// ACP: lifecycle
|
|
51
|
+
// -------------------------------------------------------------------------
|
|
52
|
+
async initialize(_params) {
|
|
53
|
+
return {
|
|
54
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
55
|
+
agentCapabilities: {
|
|
56
|
+
// We stream incremental text, so loadSession stays false: replaying a
|
|
57
|
+
// full history is not something the Quick thread model supports.
|
|
58
|
+
loadSession: false,
|
|
59
|
+
promptCapabilities: {
|
|
60
|
+
image: true,
|
|
61
|
+
audio: false,
|
|
62
|
+
embeddedContext: true,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
authMethods: [],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async newSession(params) {
|
|
69
|
+
const cwd = typeof params.cwd === 'string' && params.cwd.length > 0 ? params.cwd : this.workspaceRoot;
|
|
70
|
+
// Each ACP session gets its OWN gateway socket and its own Quick thread, so
|
|
71
|
+
// two conversations in the client map to two independent Quick threads.
|
|
72
|
+
// Turns within one session are serialized by the gateway's thread model;
|
|
73
|
+
// sessions run independently of each other.
|
|
74
|
+
const quick = await this.quick();
|
|
75
|
+
const transport = new QuickTransport(quick, {
|
|
76
|
+
onTokenRefreshNeeded: async () => {
|
|
77
|
+
try {
|
|
78
|
+
const fresh = await resolveSession();
|
|
79
|
+
this.quickSession = fresh;
|
|
80
|
+
return fresh.jwt;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
const chat = new QuickChatSession({
|
|
88
|
+
transport,
|
|
89
|
+
actorArn: quick.actorArn,
|
|
90
|
+
agentMode: 'smart',
|
|
91
|
+
thinkingEffort: 'high',
|
|
92
|
+
});
|
|
93
|
+
await chat.open();
|
|
94
|
+
// The session id must be unique ACROSS PROCESS LIFETIMES, not just within
|
|
95
|
+
// this one.
|
|
96
|
+
//
|
|
97
|
+
// It used to be `quick-${++this.sessionSeq}`, which restarts at 1 on every
|
|
98
|
+
// agent start. codeg remembers which conversation a session is bound to, so
|
|
99
|
+
// the second conversation to ever use the agent was told:
|
|
100
|
+
//
|
|
101
|
+
// conflict: agent session quick-1 is already bound to conversation 770;
|
|
102
|
+
// refusing to move it onto conversation 771
|
|
103
|
+
//
|
|
104
|
+
// and could never send a message. The Quick thread id is a UUID that this
|
|
105
|
+
// session owns for its whole life, so reusing it gives an id that is unique
|
|
106
|
+
// for real (and correlates with the thread in any server-side logs).
|
|
107
|
+
const id = chat.ensureThread();
|
|
108
|
+
this.sessions.set(id, {
|
|
109
|
+
id,
|
|
110
|
+
chat,
|
|
111
|
+
transport,
|
|
112
|
+
agentMode: 'smart',
|
|
113
|
+
thinkingEffort: 'high',
|
|
114
|
+
cancelled: false,
|
|
115
|
+
cwd,
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
sessionId: id,
|
|
119
|
+
modes: {
|
|
120
|
+
currentModeId: 'smart',
|
|
121
|
+
availableModes: [
|
|
122
|
+
{
|
|
123
|
+
id: 'fast',
|
|
124
|
+
name: 'Fast',
|
|
125
|
+
description: 'Quickest responses, no extended thinking. Maps to Quick mode "fast".',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
id: 'balanced',
|
|
129
|
+
name: 'Balanced',
|
|
130
|
+
description: 'Balances quality and speed. Maps to Quick mode "balanced".',
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
id: 'smart',
|
|
134
|
+
name: 'Smart',
|
|
135
|
+
description: 'Highest quality with extended thinking. Maps to Quick mode "smart", which is ' +
|
|
136
|
+
'the tier that routes to the strongest available model.',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async setSessionMode(params) {
|
|
143
|
+
const session = this.requireSession(params.sessionId);
|
|
144
|
+
const mode = String(params.modeId ?? '');
|
|
145
|
+
if (mode !== 'fast' && mode !== 'balanced' && mode !== 'smart') {
|
|
146
|
+
throw RequestError.invalidParams({ modeId: mode });
|
|
147
|
+
}
|
|
148
|
+
session.agentMode = mode;
|
|
149
|
+
// Fast has no thinking control in Quick; the others keep the current effort.
|
|
150
|
+
if (mode === 'fast')
|
|
151
|
+
session.thinkingEffort = 'off';
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Quick does not let a caller choose the model, so this is refused rather
|
|
155
|
+
* than silently accepted. The resolved model is reported per turn instead.
|
|
156
|
+
*/
|
|
157
|
+
async setSessionModel(params) {
|
|
158
|
+
throw new RequestError(-32601, 'Quick does not expose model selection. The gateway routes the model ' +
|
|
159
|
+
'server-side; per-turn usage reports `resolved_model_id`.', { requestedModelId: params.modelId });
|
|
160
|
+
}
|
|
161
|
+
async authenticate() {
|
|
162
|
+
// No ACP-level auth: the Quick session comes from the desktop app.
|
|
163
|
+
}
|
|
164
|
+
// -------------------------------------------------------------------------
|
|
165
|
+
// ACP: prompt turn
|
|
166
|
+
// -------------------------------------------------------------------------
|
|
167
|
+
async prompt(params) {
|
|
168
|
+
const session = this.requireSession(params.sessionId);
|
|
169
|
+
session.cancelled = false;
|
|
170
|
+
const { text: userText, attachments } = extractContent(params.prompt);
|
|
171
|
+
if (!userText.trim() && attachments.length === 0) {
|
|
172
|
+
return { stopReason: 'end_turn' };
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const result = await this.runTurn(session, userText, attachments);
|
|
176
|
+
if (result.cancelled) {
|
|
177
|
+
return { stopReason: 'cancelled' };
|
|
178
|
+
}
|
|
179
|
+
return { stopReason: 'end_turn' };
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
183
|
+
// Surface protocol-level failures as a refusal the client can render,
|
|
184
|
+
// with the reason intact instead of a generic internal error.
|
|
185
|
+
throw new RequestError(-32000, message, { sessionId: params.sessionId });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async cancel(params) {
|
|
189
|
+
const session = this.sessions.get(params.sessionId);
|
|
190
|
+
if (!session)
|
|
191
|
+
return;
|
|
192
|
+
session.cancelled = true;
|
|
193
|
+
session.chat.cancel();
|
|
194
|
+
}
|
|
195
|
+
async extMethod(method) {
|
|
196
|
+
throw new RequestError(-32601, `unsupported extension method: ${method}`);
|
|
197
|
+
}
|
|
198
|
+
async extNotification() {
|
|
199
|
+
// Ignored by design.
|
|
200
|
+
}
|
|
201
|
+
// -------------------------------------------------------------------------
|
|
202
|
+
// turn execution
|
|
203
|
+
// -------------------------------------------------------------------------
|
|
204
|
+
async runTurn(session, userText, attachments) {
|
|
205
|
+
// Tools are advertised in the prompt and driven by this loop. One extra
|
|
206
|
+
// gateway turn per tool call is the price of needing nothing but the
|
|
207
|
+
// gateway; the native MCP alternative lives on the `mcp-native` branch.
|
|
208
|
+
const result = await runWithInjectedTools({
|
|
209
|
+
userText,
|
|
210
|
+
tools: this.tools,
|
|
211
|
+
workspaceRoot: this.workspaceRoot,
|
|
212
|
+
executor: this.executor,
|
|
213
|
+
maxToolRounds: Number.parseInt(process.env.QUICK_ACP_MAX_TOOL_ROUNDS ?? '12', 10) || 12,
|
|
214
|
+
turn: (text) => this.oneGatewayTurn(session, text, []),
|
|
215
|
+
onAssistantText: () => {
|
|
216
|
+
/* streamed to the client already */
|
|
217
|
+
},
|
|
218
|
+
onToolCall: (call, result) => {
|
|
219
|
+
void this.emitToolUpdate(session, call.name, call.arguments, result.content, result.ok);
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
return { cancelled: result.cancelled, text: result.finalText };
|
|
223
|
+
}
|
|
224
|
+
/** One gateway turn: send text, stream deltas as ACP updates, return full text. */
|
|
225
|
+
async oneGatewayTurn(session, text, attachments) {
|
|
226
|
+
let full = '';
|
|
227
|
+
const stream = session.chat.sendTurn([{ role: 'user', text }], {
|
|
228
|
+
agentMode: session.agentMode,
|
|
229
|
+
thinkingEffort: session.thinkingEffort,
|
|
230
|
+
attachments,
|
|
231
|
+
});
|
|
232
|
+
for await (const ev of stream) {
|
|
233
|
+
if (session.cancelled) {
|
|
234
|
+
session.chat.cancel();
|
|
235
|
+
return { text: full, cancelled: true };
|
|
236
|
+
}
|
|
237
|
+
switch (ev.type) {
|
|
238
|
+
case 'text':
|
|
239
|
+
full += ev.delta;
|
|
240
|
+
await this.emitText(session.id, ev.delta);
|
|
241
|
+
break;
|
|
242
|
+
case 'thinking':
|
|
243
|
+
// ACP has agent_thought_chunk for exactly this; forward it so the
|
|
244
|
+
// client can render reasoning like it does for any other agent.
|
|
245
|
+
await this.emitThought(session.id, ev.delta);
|
|
246
|
+
break;
|
|
247
|
+
case 'tool_start':
|
|
248
|
+
await this.emitToolUpdate(session, ev.toolName, ev.args, undefined, true, ev.toolId);
|
|
249
|
+
break;
|
|
250
|
+
case 'tool_end':
|
|
251
|
+
break;
|
|
252
|
+
case 'turn_end':
|
|
253
|
+
break;
|
|
254
|
+
case 'error':
|
|
255
|
+
throw new Error(`${ev.code}: ${ev.message}`);
|
|
256
|
+
default:
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return { text: full, cancelled: session.cancelled };
|
|
261
|
+
}
|
|
262
|
+
// -------------------------------------------------------------------------
|
|
263
|
+
// ACP notifications
|
|
264
|
+
// -------------------------------------------------------------------------
|
|
265
|
+
async emitText(sessionId, delta) {
|
|
266
|
+
if (!this.conn || !delta)
|
|
267
|
+
return;
|
|
268
|
+
const notification = {
|
|
269
|
+
sessionId,
|
|
270
|
+
update: {
|
|
271
|
+
sessionUpdate: 'agent_message_chunk',
|
|
272
|
+
content: { type: 'text', text: delta },
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
try {
|
|
276
|
+
await this.conn.sessionUpdate(notification);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
/* client went away */
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Forward reasoning to the client.
|
|
284
|
+
*
|
|
285
|
+
* ACP distinguishes agent_thought_chunk from agent_message_chunk precisely so
|
|
286
|
+
* a client can render thinking separately instead of mistaking it for the
|
|
287
|
+
* answer. Quick emits thinking_delta, so it maps straight across.
|
|
288
|
+
*/
|
|
289
|
+
async emitThought(sessionId, delta) {
|
|
290
|
+
if (!this.conn || !delta)
|
|
291
|
+
return;
|
|
292
|
+
const notification = {
|
|
293
|
+
sessionId,
|
|
294
|
+
update: {
|
|
295
|
+
sessionUpdate: 'agent_thought_chunk',
|
|
296
|
+
content: { type: 'text', text: delta },
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
try {
|
|
300
|
+
await this.conn.sessionUpdate(notification);
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
/* client went away */
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
async emitToolUpdate(session, toolName, args, resultText, ok, toolCallId) {
|
|
307
|
+
if (!this.conn)
|
|
308
|
+
return;
|
|
309
|
+
const id = toolCallId ?? `tool-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
310
|
+
const notification = {
|
|
311
|
+
sessionId: session.id,
|
|
312
|
+
update: {
|
|
313
|
+
sessionUpdate: 'tool_call',
|
|
314
|
+
toolCallId: id,
|
|
315
|
+
title: toolName,
|
|
316
|
+
kind: 'other',
|
|
317
|
+
status: resultText === undefined ? 'in_progress' : ok ? 'completed' : 'failed',
|
|
318
|
+
rawInput: args,
|
|
319
|
+
...(resultText === undefined
|
|
320
|
+
? {}
|
|
321
|
+
: { content: [{ type: 'content', content: { type: 'text', text: resultText } }] }),
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
try {
|
|
325
|
+
await this.conn.sessionUpdate(notification);
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
/* client went away */
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// -------------------------------------------------------------------------
|
|
332
|
+
requireSession(id) {
|
|
333
|
+
const s = this.sessions.get(id);
|
|
334
|
+
if (!s)
|
|
335
|
+
throw RequestError.invalidParams({ sessionId: id });
|
|
336
|
+
return s;
|
|
337
|
+
}
|
|
338
|
+
async quick() {
|
|
339
|
+
if (this.quickSession)
|
|
340
|
+
return this.quickSession;
|
|
341
|
+
this.quickSession = await resolveSession();
|
|
342
|
+
return this.quickSession;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/** Flatten ACP prompt content blocks into text + attachments. */
|
|
346
|
+
function extractContent(blocks) {
|
|
347
|
+
const parts = [];
|
|
348
|
+
const attachments = [];
|
|
349
|
+
if (!Array.isArray(blocks))
|
|
350
|
+
return { text: '', attachments };
|
|
351
|
+
for (const block of blocks) {
|
|
352
|
+
if (!block || typeof block !== 'object')
|
|
353
|
+
continue;
|
|
354
|
+
const b = block;
|
|
355
|
+
const type = b['type'];
|
|
356
|
+
if (type === 'text' && typeof b['text'] === 'string') {
|
|
357
|
+
parts.push(b['text']);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (type === 'resource' && typeof b['resource'] === 'object') {
|
|
361
|
+
const r = b['resource'];
|
|
362
|
+
if (typeof r['text'] === 'string')
|
|
363
|
+
parts.push(r['text']);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
// ACP image blocks carry base64 in `data` with `mimeType`; that maps
|
|
367
|
+
// straight onto Quick's attachment shape.
|
|
368
|
+
if (type === 'image' && typeof b['data'] === 'string') {
|
|
369
|
+
const mimeType = typeof b['mimeType'] === 'string' ? b['mimeType'] : 'image/png';
|
|
370
|
+
const ext = mimeType.split('/')[1] ?? 'png';
|
|
371
|
+
const data = b['data'];
|
|
372
|
+
attachments.push({
|
|
373
|
+
type: 'image',
|
|
374
|
+
name: `image_${attachments.length + 1}.${ext}`,
|
|
375
|
+
size: Math.floor((data.length * 3) / 4), // base64 -> bytes, approximate
|
|
376
|
+
mimeType,
|
|
377
|
+
data,
|
|
378
|
+
});
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return { text: parts.join('\n\n'), attachments };
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* ACP handshake smoke test.
|
|
4
|
+
*
|
|
5
|
+
* Launches the built agent over stdio and drives the ACP methods a client calls
|
|
6
|
+
* before it ever reaches a prompt turn. This proves the adaptor is wire-correct
|
|
7
|
+
* WITHOUT a Quick account, because none of these methods touch the gateway.
|
|
8
|
+
*
|
|
9
|
+
* What it does NOT prove: that `newSession`/`prompt` reach the gateway. Those
|
|
10
|
+
* need a live Quick session and are the documented next step.
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import { dirname, join } from 'node:path';
|
|
15
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const agentPath = join(here, 'main.js');
|
|
17
|
+
let nextId = 1;
|
|
18
|
+
const pending = new Map();
|
|
19
|
+
function send(child, method, params) {
|
|
20
|
+
const id = nextId++;
|
|
21
|
+
const msg = { jsonrpc: '2.0', id, method, params };
|
|
22
|
+
child.stdin?.write(`${JSON.stringify(msg)}\n`);
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
pending.set(id, resolve);
|
|
25
|
+
setTimeout(() => {
|
|
26
|
+
if (!pending.has(id))
|
|
27
|
+
return;
|
|
28
|
+
pending.delete(id);
|
|
29
|
+
reject(new Error(`timeout waiting for ${method}`));
|
|
30
|
+
}, 10_000);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async function main() {
|
|
34
|
+
console.log('ACP handshake smoke test');
|
|
35
|
+
console.log('');
|
|
36
|
+
const child = spawn(process.execPath, [agentPath], {
|
|
37
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
38
|
+
env: { ...process.env, QUICK_ACP_WORKSPACE: process.cwd() },
|
|
39
|
+
});
|
|
40
|
+
let buffer = '';
|
|
41
|
+
child.stdout?.on('data', (chunk) => {
|
|
42
|
+
buffer += chunk.toString('utf8');
|
|
43
|
+
let idx;
|
|
44
|
+
while ((idx = buffer.indexOf('\n')) >= 0) {
|
|
45
|
+
const line = buffer.slice(0, idx).trim();
|
|
46
|
+
buffer = buffer.slice(idx + 1);
|
|
47
|
+
if (!line)
|
|
48
|
+
continue;
|
|
49
|
+
let msg;
|
|
50
|
+
try {
|
|
51
|
+
msg = JSON.parse(line);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
console.error(' (non-JSON line on stdout:', line.slice(0, 120), ')');
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (typeof msg.id === 'number' && pending.has(msg.id)) {
|
|
58
|
+
const resolve = pending.get(msg.id);
|
|
59
|
+
pending.delete(msg.id);
|
|
60
|
+
resolve(msg);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
let stderr = '';
|
|
65
|
+
child.stderr?.on('data', (c) => {
|
|
66
|
+
stderr += c.toString('utf8');
|
|
67
|
+
});
|
|
68
|
+
let failures = 0;
|
|
69
|
+
const check = async (name, fn) => {
|
|
70
|
+
try {
|
|
71
|
+
await fn();
|
|
72
|
+
console.log(` ok ${name}`);
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
failures++;
|
|
76
|
+
console.log(` FAIL ${name}`);
|
|
77
|
+
console.log(` ${e instanceof Error ? e.message : String(e)}`);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
await check('initialize negotiates protocol version and capabilities', async () => {
|
|
81
|
+
const resp = await send(child, 'initialize', {
|
|
82
|
+
protocolVersion: 1,
|
|
83
|
+
clientCapabilities: { fs: { readTextFile: false, writeTextFile: false } },
|
|
84
|
+
});
|
|
85
|
+
if (resp.error)
|
|
86
|
+
throw new Error(`error: ${JSON.stringify(resp.error)}`);
|
|
87
|
+
const result = resp.result;
|
|
88
|
+
if (!result)
|
|
89
|
+
throw new Error('no result');
|
|
90
|
+
if (typeof result['protocolVersion'] !== 'number')
|
|
91
|
+
throw new Error('protocolVersion missing');
|
|
92
|
+
const caps = result['agentCapabilities'];
|
|
93
|
+
if (!caps)
|
|
94
|
+
throw new Error('agentCapabilities missing');
|
|
95
|
+
if (caps['loadSession'] !== false) {
|
|
96
|
+
throw new Error('loadSession should be false: the thread model cannot replay history');
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
await check('session/set_model is refused with a real reason, not a fake success', async () => {
|
|
100
|
+
const resp = await send(child, 'session/set_model', {
|
|
101
|
+
sessionId: 'nope',
|
|
102
|
+
modelId: 'claude-opus-5.5',
|
|
103
|
+
});
|
|
104
|
+
if (!resp.error)
|
|
105
|
+
throw new Error('expected an error, got success');
|
|
106
|
+
const msg = String(resp.error.message ?? '');
|
|
107
|
+
if (!/model selection/i.test(msg)) {
|
|
108
|
+
throw new Error(`unexpected refusal reason: ${msg}`);
|
|
109
|
+
}
|
|
110
|
+
// Must be OUR refusal, not the SDK's generic "Method not found".
|
|
111
|
+
if (/method not found/i.test(msg)) {
|
|
112
|
+
throw new Error('handler was not routed: SDK reported Method not found');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
await check('session/set_mode validates the mode id', async () => {
|
|
116
|
+
const resp = await send(child, 'session/set_mode', {
|
|
117
|
+
sessionId: 'nope',
|
|
118
|
+
modeId: 'smart',
|
|
119
|
+
});
|
|
120
|
+
// Unknown session must be refused before mode validation.
|
|
121
|
+
if (!resp.error)
|
|
122
|
+
throw new Error('expected an error for an unknown session');
|
|
123
|
+
if (/method not found/i.test(String(resp.error.message))) {
|
|
124
|
+
throw new Error('handler was not routed');
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
await check('unknown method returns method-not-found, not a hang', async () => {
|
|
128
|
+
const resp = await send(child, 'no/such/method', {});
|
|
129
|
+
if (!resp.error)
|
|
130
|
+
throw new Error('expected an error');
|
|
131
|
+
});
|
|
132
|
+
await check('prompt on an unknown session is refused cleanly', async () => {
|
|
133
|
+
const resp = await send(child, 'session/prompt', {
|
|
134
|
+
sessionId: 'does-not-exist',
|
|
135
|
+
prompt: [{ type: 'text', text: 'hi' }],
|
|
136
|
+
});
|
|
137
|
+
if (!resp.error)
|
|
138
|
+
throw new Error('expected an error');
|
|
139
|
+
});
|
|
140
|
+
child.kill();
|
|
141
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
142
|
+
console.log('');
|
|
143
|
+
if (failures === 0) {
|
|
144
|
+
console.log('ACP adaptor is wire-correct for the pre-session methods.');
|
|
145
|
+
console.log('Next: provide a Quick session and run a real prompt.');
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
console.log(`${failures} check(s) FAILED`);
|
|
149
|
+
process.exitCode = 1;
|
|
150
|
+
}
|
|
151
|
+
if (stderr.trim()) {
|
|
152
|
+
console.log('');
|
|
153
|
+
console.log('agent stderr (first lines):');
|
|
154
|
+
console.log(stderr
|
|
155
|
+
.trim()
|
|
156
|
+
.split('\n')
|
|
157
|
+
.slice(0, 6)
|
|
158
|
+
.map((l) => ` ${l}`)
|
|
159
|
+
.join('\n'));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
void main();
|
|
163
|
+
//# sourceMappingURL=acp-smoke.js.map
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concurrency: two ACP sessions at once, each with its own gateway socket,
|
|
3
|
+
* driven through the real agent over stdio.
|
|
4
|
+
*
|
|
5
|
+
* This is the "two conversations in codeg at the same time" case. Each session
|
|
6
|
+
* must answer its OWN prompt — the failure mode to catch is crossed replies or
|
|
7
|
+
* one session starving the other.
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import { dirname, join } from 'node:path';
|
|
12
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const agentPath = join(here, 'main.js');
|
|
14
|
+
const child = spawn(process.execPath, [agentPath], {
|
|
15
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
16
|
+
env: { ...process.env, QUICK_ACP_WORKSPACE: process.cwd(), QUICK_ACP_ALLOW_SHELL: '1' },
|
|
17
|
+
});
|
|
18
|
+
child.stderr.on('data', () => { });
|
|
19
|
+
let nextId = 1;
|
|
20
|
+
const pending = new Map();
|
|
21
|
+
const chunks = new Map(); // sessionId -> text
|
|
22
|
+
let buf = '';
|
|
23
|
+
child.stdout.on('data', (c) => {
|
|
24
|
+
buf += c.toString('utf8');
|
|
25
|
+
let i;
|
|
26
|
+
while ((i = buf.indexOf('\n')) >= 0) {
|
|
27
|
+
const line = buf.slice(0, i).trim();
|
|
28
|
+
buf = buf.slice(i + 1);
|
|
29
|
+
if (!line)
|
|
30
|
+
continue;
|
|
31
|
+
let msg;
|
|
32
|
+
try {
|
|
33
|
+
msg = JSON.parse(line);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (typeof msg.id === 'number' && pending.has(msg.id)) {
|
|
39
|
+
const fn = pending.get(msg.id);
|
|
40
|
+
pending.delete(msg.id);
|
|
41
|
+
fn(msg);
|
|
42
|
+
}
|
|
43
|
+
else if (msg.method === 'session/update') {
|
|
44
|
+
const p = (msg.params ?? {});
|
|
45
|
+
const u = (p['update'] ?? {});
|
|
46
|
+
const content = (u['content'] ?? {});
|
|
47
|
+
const sessionId = typeof p['sessionId'] === 'string' ? p['sessionId'] : '';
|
|
48
|
+
if (u['sessionUpdate'] === 'agent_message_chunk' && typeof content['text'] === 'string') {
|
|
49
|
+
chunks.set(sessionId, (chunks.get(sessionId) ?? '') + content['text']);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
function send(method, params) {
|
|
55
|
+
const id = nextId++;
|
|
56
|
+
child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, method, params })}\n`);
|
|
57
|
+
return new Promise((res, rej) => {
|
|
58
|
+
pending.set(id, res);
|
|
59
|
+
setTimeout(() => {
|
|
60
|
+
if (pending.delete(id))
|
|
61
|
+
rej(new Error(`timeout: ${method}`));
|
|
62
|
+
}, 300_000);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
await send('initialize', { protocolVersion: 1, clientCapabilities: {} });
|
|
66
|
+
const aRes = await send('session/new', { cwd: process.cwd(), mcpServers: [] });
|
|
67
|
+
const bRes = await send('session/new', { cwd: process.cwd(), mcpServers: [] });
|
|
68
|
+
const a = String(aRes.result?.['sessionId'] ?? '');
|
|
69
|
+
const b = String(bRes.result?.['sessionId'] ?? '');
|
|
70
|
+
console.log('session A:', a);
|
|
71
|
+
console.log('session B:', b);
|
|
72
|
+
console.log('');
|
|
73
|
+
console.log('prompting both concurrently...');
|
|
74
|
+
const t0 = Date.now();
|
|
75
|
+
const [ra, rb] = await Promise.all([
|
|
76
|
+
send('session/prompt', { sessionId: a, prompt: [{ type: 'text', text: 'Reply with exactly: SESSIONA' }] }),
|
|
77
|
+
send('session/prompt', { sessionId: b, prompt: [{ type: 'text', text: 'Reply with exactly: SESSIONB' }] }),
|
|
78
|
+
]);
|
|
79
|
+
const ms = Date.now() - t0;
|
|
80
|
+
await new Promise((r) => setTimeout(r, 800));
|
|
81
|
+
const textA = (chunks.get(a) ?? '').trim();
|
|
82
|
+
const textB = (chunks.get(b) ?? '').trim();
|
|
83
|
+
console.log('');
|
|
84
|
+
console.log('elapsed :', ms, 'ms');
|
|
85
|
+
console.log('A stopReason :', ra.result?.['stopReason']);
|
|
86
|
+
console.log('B stopReason :', rb.result?.['stopReason']);
|
|
87
|
+
console.log('A text :', JSON.stringify(textA));
|
|
88
|
+
console.log('B text :', JSON.stringify(textB));
|
|
89
|
+
console.log('');
|
|
90
|
+
const okA = textA.toUpperCase().includes('SESSIONA');
|
|
91
|
+
const okB = textB.toUpperCase().includes('SESSIONB');
|
|
92
|
+
const crossed = textA.toUpperCase().includes('SESSIONB') || textB.toUpperCase().includes('SESSIONA');
|
|
93
|
+
if (okA && okB && !crossed) {
|
|
94
|
+
console.log('*** CONCURRENT SESSIONS WORK — no crossed replies ***');
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
console.log('FAILED A_ok=' + okA + ' B_ok=' + okB + ' crossed=' + crossed);
|
|
98
|
+
process.exitCode = 1;
|
|
99
|
+
}
|
|
100
|
+
child.kill();
|
|
101
|
+
//# sourceMappingURL=concurrent-test.js.map
|