quick-acp 0.1.0 → 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/dist/acp/agent.js CHANGED
@@ -91,7 +91,20 @@ export class QuickAcpAgent {
91
91
  thinkingEffort: 'high',
92
92
  });
93
93
  await chat.open();
94
- const id = `quick-${++this.sessionSeq}`;
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();
95
108
  this.sessions.set(id, {
96
109
  id,
97
110
  chat,
@@ -80,6 +80,20 @@ export class QuickChatSession {
80
80
  get threadId() {
81
81
  return this.state?.threadId ?? null;
82
82
  }
83
+ /**
84
+ * Create this session's thread now, if it does not have one.
85
+ *
86
+ * The thread id is only a UUID until the first turn — the server accepts a
87
+ * fresh one and creates the conversation. But callers need a stable identity
88
+ * for the session BEFORE the first turn: an ACP client binds the session id to
89
+ * a conversation the moment the session is created, and an id that only
90
+ * appears after the first message is no id at all.
91
+ */
92
+ ensureThread() {
93
+ if (!this.state)
94
+ this.state = { threadId: randomUUID(), sentTurns: [] };
95
+ return this.state.threadId;
96
+ }
83
97
  /** Forget the thread, so the next turn starts a fresh conversation. */
84
98
  resetThread() {
85
99
  this.state = null;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Regression: session ids must not collide across agent restarts.
3
+ *
4
+ * The bug this locks down: ids were `quick-1`, `quick-2`, … from a counter that
5
+ * reset to 1 on every process start. codeg binds a session id to a conversation
6
+ * and refuses to rebind it, so the second conversation ever opened got
7
+ * "agent session quick-1 is already bound to conversation 770" and could never
8
+ * send a message.
9
+ *
10
+ * Runs the REAL agent twice (two separate processes, like two app restarts) and
11
+ * asserts the ids differ.
12
+ */
13
+ import { spawn } from 'node:child_process';
14
+ import { fileURLToPath } from 'node:url';
15
+ import { dirname, join } from 'node:path';
16
+ const here = dirname(fileURLToPath(import.meta.url));
17
+ const agentPath = join(here, 'main.js');
18
+ /** Start an agent, create one session, return its id, and stop the agent. */
19
+ async function oneRun(label) {
20
+ const child = spawn(process.execPath, [agentPath], {
21
+ stdio: ['pipe', 'pipe', 'pipe'],
22
+ env: { ...process.env, QUICK_ACP_WORKSPACE: process.cwd() },
23
+ });
24
+ child.stderr.on('data', () => { });
25
+ let buf = '';
26
+ const waiters = new Map();
27
+ child.stdout.on('data', (c) => {
28
+ buf += c.toString('utf8');
29
+ let i;
30
+ while ((i = buf.indexOf('\n')) >= 0) {
31
+ const line = buf.slice(0, i).trim();
32
+ buf = buf.slice(i + 1);
33
+ if (!line)
34
+ continue;
35
+ let msg;
36
+ try {
37
+ msg = JSON.parse(line);
38
+ }
39
+ catch {
40
+ continue;
41
+ }
42
+ if (typeof msg.id === 'number' && waiters.has(msg.id)) {
43
+ const fn = waiters.get(msg.id);
44
+ waiters.delete(msg.id);
45
+ fn(msg);
46
+ }
47
+ }
48
+ });
49
+ const send = (id, method, params) => {
50
+ child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, method, params })}\n`);
51
+ return new Promise((res, rej) => {
52
+ waiters.set(id, res);
53
+ setTimeout(() => rej(new Error(`timeout: ${method}`)), 120_000);
54
+ });
55
+ };
56
+ await send(1, 'initialize', { protocolVersion: 1, clientCapabilities: {} });
57
+ const ns = await send(2, 'session/new', { cwd: process.cwd(), mcpServers: [] });
58
+ if (ns.error)
59
+ throw new Error(`${label}: session/new failed: ${ns.error.message}`);
60
+ const id = String(ns.result?.['sessionId'] ?? '');
61
+ child.kill();
62
+ await new Promise((r) => setTimeout(r, 400));
63
+ return id;
64
+ }
65
+ let failures = 0;
66
+ const check = (name, ok, detail = '') => {
67
+ if (ok)
68
+ console.log(` ok ${name}`);
69
+ else {
70
+ failures++;
71
+ console.log(` FAIL ${name}${detail ? ` — ${detail}` : ''}`);
72
+ }
73
+ };
74
+ console.log('quick-acp session id uniqueness');
75
+ console.log('');
76
+ const ids = [];
77
+ for (const label of ['run 1', 'run 2', 'run 3']) {
78
+ const id = await oneRun(label);
79
+ ids.push(id);
80
+ console.log(` ${label}: ${id}`);
81
+ }
82
+ console.log('');
83
+ check('every session got an id', ids.every((i) => i.length > 0));
84
+ check('ids are unique across separate agent processes', new Set(ids).size === ids.length, `got ${JSON.stringify(ids)}`);
85
+ // The old scheme produced quick-1 / quick-1 / quick-1 here.
86
+ check('ids are not the old process-local counter form', !ids.every((i) => /^quick-\d+$/.test(i)) || new Set(ids).size === ids.length);
87
+ check('ids look like Quick thread ids (UUID)', ids.every((i) => /^[0-9a-f-]{36}$/i.test(i)));
88
+ console.log('');
89
+ if (failures === 0)
90
+ console.log('all session-id checks passed');
91
+ else {
92
+ console.log(`${failures} check(s) FAILED`);
93
+ process.exitCode = 1;
94
+ }
95
+ //# sourceMappingURL=session-id-test.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
- {
1
+ {
2
2
  "name": "quick-acp",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "description": "ACP agent that bridges AWS Quick (Quickwork) Claude quota to standard ACP clients",
6
6
  "license": "MIT",