acpx 0.7.0 → 0.9.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.
Files changed (50) hide show
  1. package/README.md +14 -6
  2. package/dist/{cli-T-Z-9x6a.js → cli-Bf3yjqzE.js} +35 -10
  3. package/dist/cli-Bf3yjqzE.js.map +1 -0
  4. package/dist/cli.d.ts +1 -1
  5. package/dist/cli.d.ts.map +1 -1
  6. package/dist/cli.js +724 -241
  7. package/dist/cli.js.map +1 -1
  8. package/dist/{client-COPilhO_.d.ts → client-BssohYqM.d.ts} +35 -4
  9. package/dist/client-BssohYqM.d.ts.map +1 -0
  10. package/dist/flags-C-rwARqg.js +260 -0
  11. package/dist/flags-C-rwARqg.js.map +1 -0
  12. package/dist/{flows-CF8w1rPI.js → flows-WLs26_5Y.js} +405 -337
  13. package/dist/flows-WLs26_5Y.js.map +1 -0
  14. package/dist/flows.d.ts +23 -2
  15. package/dist/flows.d.ts.map +1 -1
  16. package/dist/flows.js +1 -1
  17. package/dist/{prompt-turn-CVPMWdj1.js → live-checkpoint-D5d-K9s1.js} +2487 -609
  18. package/dist/live-checkpoint-D5d-K9s1.js.map +1 -0
  19. package/dist/output-DPg20dvn.js +4146 -0
  20. package/dist/output-DPg20dvn.js.map +1 -0
  21. package/dist/runtime.d.ts +56 -4
  22. package/dist/runtime.d.ts.map +1 -1
  23. package/dist/runtime.js +676 -393
  24. package/dist/runtime.js.map +1 -1
  25. package/dist/{types-CVBeQyi3.d.ts → session-options-CFudjdkU.d.ts} +62 -3
  26. package/dist/session-options-CFudjdkU.d.ts.map +1 -0
  27. package/package.json +30 -25
  28. package/skills/acpx/SKILL.md +211 -13
  29. package/dist/cli-T-Z-9x6a.js.map +0 -1
  30. package/dist/client-COPilhO_.d.ts.map +0 -1
  31. package/dist/flags-Dj-IXgo9.js +0 -163
  32. package/dist/flags-Dj-IXgo9.js.map +0 -1
  33. package/dist/flows-CF8w1rPI.js.map +0 -1
  34. package/dist/ipc-ABXlXzGP.js +0 -1290
  35. package/dist/ipc-ABXlXzGP.js.map +0 -1
  36. package/dist/jsonrpc-DSxh2w5R.js +0 -68
  37. package/dist/jsonrpc-DSxh2w5R.js.map +0 -1
  38. package/dist/output-DmHvT8vm.js +0 -807
  39. package/dist/output-DmHvT8vm.js.map +0 -1
  40. package/dist/perf-metrics-C2pXfxvR.js +0 -598
  41. package/dist/perf-metrics-C2pXfxvR.js.map +0 -1
  42. package/dist/prompt-turn-CVPMWdj1.js.map +0 -1
  43. package/dist/render-N5YwotCy.js +0 -172
  44. package/dist/render-N5YwotCy.js.map +0 -1
  45. package/dist/rolldown-runtime-CiIaOW0V.js +0 -13
  46. package/dist/session-CDaQe6BH.js +0 -1538
  47. package/dist/session-CDaQe6BH.js.map +0 -1
  48. package/dist/session-options-pCbHn_n7.d.ts +0 -13
  49. package/dist/session-options-pCbHn_n7.d.ts.map +0 -1
  50. package/dist/types-CVBeQyi3.d.ts.map +0 -1
@@ -0,0 +1,4146 @@
1
+ import { $ as getPerfMetricsSnapshot, A as parsePromptStopReason, B as normalizeName, Bt as QueueProtocolError, C as applyConversation, Ct as formatErrorMessage, D as extractSessionUpdateNotification, E as AcpClient, F as findSession, H as resolveSessionRecord, I as findSessionByDirectoryWalk, J as sessionEventActivePath, K as defaultSessionEventLog, L as isoNow, Mt as OUTPUT_ERROR_CODES, N as absolutePath, Nt as OUTPUT_ERROR_ORIGINS, O as isAcpJsonRpcMessage, Ot as toAcpErrorPayload, P as findGitRepositoryRoot, Q as formatPerfMetric, R as listSessions, S as sessionOptionsFromRecord, Tt as normalizeOutputError, U as writeSessionRecord, V as pruneSessions, X as sessionEventSegmentPath, Y as sessionEventLockPath, _ as recordPromptSubmission, _t as withTimeout, a as applyRequestedModelIfAdvertised, at as startPerfTimer, b as mergeSessionOptions, c as setDesiredConfigOption, d as syncAdvertisedModelState, et as incrementPerfCounter, f as applyConfigOptionsToRecord, ft as promptToDisplayText, g as recordClientOperation, gt as withInterrupt, h as createSessionConversation, ht as TimeoutError, i as connectAndLoadSession, it as setPerfGauge, k as parseJsonRpcErrorMessage, l as setDesiredModeId, lt as isPromptInput, m as cloneSessionConversation, mt as InterruptedError, n as runPromptTurn, nt as recordPerfDuration, o as assertRequestedModelSupported, p as cloneSessionAcpxState, pt as textPrompt, q as sessionBaseDir, r as withConnectedSession, rt as resetPerfMetrics, s as setCurrentModelId, st as normalizeRuntimeSessionId, t as LiveSessionCheckpoint, tt as measurePerf, u as setDesiredModelId, v as recordSessionUpdate, w as applyLifecycleSnapshotToRecord, wt as isRetryablePromptError, x as persistSessionOptions, y as trimConversationForRuntime, z as listSessionsForAgent, zt as QueueConnectionError } from "./live-checkpoint-D5d-K9s1.js";
2
+ import fs, { realpathSync } from "node:fs";
3
+ import path from "node:path";
4
+ import fs$1 from "node:fs/promises";
5
+ import os from "node:os";
6
+ import { spawn } from "node:child_process";
7
+ import { createHash, randomInt, randomUUID } from "node:crypto";
8
+ import net from "node:net";
9
+ //#region \0rolldown/runtime.js
10
+ var __defProp = Object.defineProperty;
11
+ var __exportAll = (all, no_symbols) => {
12
+ let target = {};
13
+ for (var name in all) __defProp(target, name, {
14
+ get: all[name],
15
+ enumerable: true
16
+ });
17
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
18
+ return target;
19
+ };
20
+ //#endregion
21
+ //#region src/cli/session/contracts.ts
22
+ const DEFAULT_QUEUE_OWNER_TTL_MS = 3e5;
23
+ function normalizeQueueOwnerTtlMs(ttlMs) {
24
+ if (ttlMs == null) return DEFAULT_QUEUE_OWNER_TTL_MS;
25
+ if (!Number.isFinite(ttlMs) || ttlMs < 0) return DEFAULT_QUEUE_OWNER_TTL_MS;
26
+ return Math.round(ttlMs);
27
+ }
28
+ //#endregion
29
+ //#region src/process-liveness.ts
30
+ function isProcessAlive(pid) {
31
+ if (!pid || !Number.isInteger(pid) || pid <= 0 || pid === process.pid) return false;
32
+ try {
33
+ process.kill(pid, 0);
34
+ return true;
35
+ } catch {
36
+ return false;
37
+ }
38
+ }
39
+ //#endregion
40
+ //#region src/cli/queue/paths.ts
41
+ function shortHash(value, length) {
42
+ return createHash("sha256").update(value).digest("hex").slice(0, length);
43
+ }
44
+ function queueKeyForSession(sessionId) {
45
+ return shortHash(sessionId, 24);
46
+ }
47
+ function queueBaseDir(homeDir = os.homedir()) {
48
+ return path.join(homeDir, ".acpx", "queues");
49
+ }
50
+ function queueSocketBaseDir(homeDir = os.homedir()) {
51
+ if (process.platform === "win32") return;
52
+ return path.join("/tmp", `acpx-${shortHash(homeDir, 10)}`);
53
+ }
54
+ function queueLockFilePath(sessionId, homeDir = os.homedir()) {
55
+ return path.join(queueBaseDir(homeDir), `${queueKeyForSession(sessionId)}.lock`);
56
+ }
57
+ function queueSocketPath(sessionId, homeDir = os.homedir()) {
58
+ const key = queueKeyForSession(sessionId);
59
+ if (process.platform === "win32") return `\\\\.\\pipe\\acpx-${key}`;
60
+ return path.join(queueSocketBaseDir(homeDir) ?? "/tmp", `${key}.sock`);
61
+ }
62
+ //#endregion
63
+ //#region src/cli/queue/lease-store.ts
64
+ const PROCESS_EXIT_GRACE_MS = 1500;
65
+ const PROCESS_POLL_MS = 50;
66
+ const QUEUE_OWNER_STALE_HEARTBEAT_MS = 15e3;
67
+ function parseQueueOwnerRecord(raw) {
68
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
69
+ const record = raw;
70
+ if (!hasValidQueueOwnerRecordFields(record)) return null;
71
+ return {
72
+ pid: record.pid,
73
+ sessionId: record.sessionId,
74
+ socketPath: record.socketPath,
75
+ createdAt: record.createdAt,
76
+ heartbeatAt: record.heartbeatAt,
77
+ ownerGeneration: record.ownerGeneration,
78
+ queueDepth: record.queueDepth
79
+ };
80
+ }
81
+ function hasValidQueueOwnerRecordFields(record) {
82
+ return isPositiveInteger(record.pid) && typeof record.sessionId === "string" && typeof record.socketPath === "string" && typeof record.createdAt === "string" && typeof record.heartbeatAt === "string" && isPositiveInteger(record.ownerGeneration) && isNonNegativeInteger(record.queueDepth);
83
+ }
84
+ function isPositiveInteger(value) {
85
+ return Number.isInteger(value) && value > 0;
86
+ }
87
+ function isNonNegativeInteger(value) {
88
+ return Number.isInteger(value) && value >= 0;
89
+ }
90
+ function createOwnerGeneration() {
91
+ return randomInt(1, 2 ** 48);
92
+ }
93
+ function nowIso() {
94
+ return (/* @__PURE__ */ new Date()).toISOString();
95
+ }
96
+ function isQueueOwnerHeartbeatStale(owner) {
97
+ const heartbeatMs = Date.parse(owner.heartbeatAt);
98
+ if (!Number.isFinite(heartbeatMs)) return true;
99
+ return Date.now() - heartbeatMs > QUEUE_OWNER_STALE_HEARTBEAT_MS;
100
+ }
101
+ async function ensureQueueDir() {
102
+ const baseDir = queueBaseDir();
103
+ await fs$1.mkdir(baseDir, {
104
+ recursive: true,
105
+ mode: 448
106
+ });
107
+ await fs$1.chmod(baseDir, 448);
108
+ const socketDir = queueSocketBaseDir();
109
+ if (socketDir) {
110
+ await fs$1.mkdir(socketDir, {
111
+ recursive: true,
112
+ mode: 448
113
+ });
114
+ await fs$1.chmod(socketDir, 448);
115
+ }
116
+ }
117
+ async function removeSocketFile(socketPath) {
118
+ if (process.platform === "win32") return;
119
+ try {
120
+ await fs$1.unlink(socketPath);
121
+ } catch (error) {
122
+ if (error.code !== "ENOENT") throw error;
123
+ }
124
+ }
125
+ async function waitForProcessExit(pid, timeoutMs) {
126
+ const deadline = Date.now() + Math.max(0, timeoutMs);
127
+ while (Date.now() <= deadline) {
128
+ if (!isProcessAlive(pid)) return true;
129
+ await waitMs(PROCESS_POLL_MS);
130
+ }
131
+ return !isProcessAlive(pid);
132
+ }
133
+ async function cleanupStaleQueueOwner(sessionId, owner) {
134
+ const lockPath = queueLockFilePath(sessionId);
135
+ await removeSocketFile(owner?.socketPath ?? queueSocketPath(sessionId)).catch(() => {});
136
+ await fs$1.unlink(lockPath).catch((error) => {
137
+ if (error.code !== "ENOENT") throw error;
138
+ });
139
+ }
140
+ async function readQueueOwnerRecord(sessionId) {
141
+ const lockPath = queueLockFilePath(sessionId);
142
+ try {
143
+ const payload = await fs$1.readFile(lockPath, "utf8");
144
+ return parseQueueOwnerRecord(JSON.parse(payload)) ?? void 0;
145
+ } catch {
146
+ return;
147
+ }
148
+ }
149
+ async function terminateProcess(pid) {
150
+ if (!isProcessAlive(pid)) return false;
151
+ try {
152
+ process.kill(pid, "SIGTERM");
153
+ } catch {
154
+ return false;
155
+ }
156
+ if (await waitForProcessExit(pid, PROCESS_EXIT_GRACE_MS)) return true;
157
+ try {
158
+ process.kill(pid, "SIGKILL");
159
+ } catch {
160
+ return false;
161
+ }
162
+ await waitForProcessExit(pid, PROCESS_EXIT_GRACE_MS);
163
+ return true;
164
+ }
165
+ async function ensureOwnerIsUsable(sessionId, owner) {
166
+ const alive = isProcessAlive(owner.pid);
167
+ const stale = isQueueOwnerHeartbeatStale(owner);
168
+ if (alive && !stale) return true;
169
+ if (alive) await terminateProcess(owner.pid).catch(() => {});
170
+ await cleanupStaleQueueOwner(sessionId, owner);
171
+ return false;
172
+ }
173
+ async function readQueueOwnerStatus(sessionId) {
174
+ const owner = await readQueueOwnerRecord(sessionId);
175
+ if (!owner) return;
176
+ const alive = await ensureOwnerIsUsable(sessionId, owner);
177
+ if (!alive) return;
178
+ return {
179
+ pid: owner.pid,
180
+ socketPath: owner.socketPath,
181
+ heartbeatAt: owner.heartbeatAt,
182
+ ownerGeneration: owner.ownerGeneration,
183
+ queueDepth: owner.queueDepth,
184
+ alive,
185
+ stale: isQueueOwnerHeartbeatStale(owner)
186
+ };
187
+ }
188
+ async function tryAcquireQueueOwnerLease(sessionId, nowIsoFactory = nowIso) {
189
+ await ensureQueueDir();
190
+ const lockPath = queueLockFilePath(sessionId);
191
+ const socketPath = queueSocketPath(sessionId);
192
+ const createdAt = nowIsoFactory();
193
+ const ownerGeneration = createOwnerGeneration();
194
+ const payload = JSON.stringify({
195
+ pid: process.pid,
196
+ sessionId,
197
+ socketPath,
198
+ createdAt,
199
+ heartbeatAt: createdAt,
200
+ ownerGeneration,
201
+ queueDepth: 0
202
+ }, null, 2);
203
+ try {
204
+ await fs$1.writeFile(lockPath, `${payload}\n`, {
205
+ encoding: "utf8",
206
+ flag: "wx"
207
+ });
208
+ await removeSocketFile(socketPath).catch(() => {});
209
+ return {
210
+ sessionId,
211
+ lockPath,
212
+ socketPath,
213
+ createdAt,
214
+ ownerGeneration
215
+ };
216
+ } catch (error) {
217
+ if (error.code !== "EEXIST") throw error;
218
+ const owner = await readQueueOwnerRecord(sessionId);
219
+ if (!owner) {
220
+ await cleanupStaleQueueOwner(sessionId, owner);
221
+ return;
222
+ }
223
+ if (!isProcessAlive(owner.pid) || isQueueOwnerHeartbeatStale(owner)) {
224
+ if (isProcessAlive(owner.pid)) await terminateProcess(owner.pid).catch(() => {});
225
+ await cleanupStaleQueueOwner(sessionId, owner);
226
+ }
227
+ return;
228
+ }
229
+ }
230
+ async function refreshQueueOwnerLease(lease, options, nowIsoFactory = nowIso) {
231
+ const payload = JSON.stringify({
232
+ pid: process.pid,
233
+ sessionId: lease.sessionId,
234
+ socketPath: lease.socketPath,
235
+ createdAt: lease.createdAt,
236
+ heartbeatAt: nowIsoFactory(),
237
+ ownerGeneration: lease.ownerGeneration,
238
+ queueDepth: Math.max(0, Math.round(options.queueDepth))
239
+ }, null, 2);
240
+ await fs$1.writeFile(lease.lockPath, `${payload}\n`, { encoding: "utf8" });
241
+ }
242
+ async function releaseQueueOwnerLease(lease) {
243
+ await removeSocketFile(lease.socketPath).catch(() => {});
244
+ await fs$1.unlink(lease.lockPath).catch((error) => {
245
+ if (error.code !== "ENOENT") throw error;
246
+ });
247
+ }
248
+ async function terminateQueueOwnerForSession(sessionId) {
249
+ const owner = await readQueueOwnerRecord(sessionId);
250
+ if (!owner) return;
251
+ if (isProcessAlive(owner.pid)) await terminateProcess(owner.pid);
252
+ await cleanupStaleQueueOwner(sessionId, owner);
253
+ }
254
+ async function waitMs(ms) {
255
+ await new Promise((resolve) => {
256
+ setTimeout(resolve, ms);
257
+ });
258
+ }
259
+ //#endregion
260
+ //#region src/cli/queue/ipc-transport.ts
261
+ const QUEUE_CONNECT_ATTEMPTS = 40;
262
+ const SOCKET_CONNECTION_TIMEOUT_MS = 5e3;
263
+ function shouldRetryQueueConnect(error) {
264
+ const code = error.code;
265
+ return code === "ENOENT" || code === "ECONNREFUSED";
266
+ }
267
+ async function connectToSocket(socketPath, timeoutMs = SOCKET_CONNECTION_TIMEOUT_MS) {
268
+ return await new Promise((resolve, reject) => {
269
+ const socket = net.createConnection(socketPath);
270
+ let settled = false;
271
+ const timeout = setTimeout(() => {
272
+ if (settled) return;
273
+ settled = true;
274
+ socket.destroy();
275
+ reject(/* @__PURE__ */ new Error(`Connection to ${socketPath} timed out after ${timeoutMs}ms`));
276
+ }, timeoutMs);
277
+ const onConnect = () => {
278
+ if (settled) return;
279
+ settled = true;
280
+ clearTimeout(timeout);
281
+ socket.off("error", onError);
282
+ resolve(socket);
283
+ };
284
+ const onError = (error) => {
285
+ if (settled) return;
286
+ settled = true;
287
+ clearTimeout(timeout);
288
+ socket.off("connect", onConnect);
289
+ reject(error);
290
+ };
291
+ socket.once("connect", onConnect);
292
+ socket.once("error", onError);
293
+ });
294
+ }
295
+ async function connectToQueueOwner(owner, maxAttempts = QUEUE_CONNECT_ATTEMPTS) {
296
+ let lastError;
297
+ const attempts = Math.max(1, Math.trunc(maxAttempts));
298
+ for (let attempt = 0; attempt < attempts; attempt += 1) try {
299
+ return await measurePerf("queue.connect", async () => await connectToSocket(owner.socketPath));
300
+ } catch (error) {
301
+ lastError = error;
302
+ if (!shouldRetryQueueConnect(error)) throw error;
303
+ await waitMs(50);
304
+ }
305
+ if (lastError && !shouldRetryQueueConnect(lastError)) throw lastError;
306
+ }
307
+ //#endregion
308
+ //#region src/cli/queue/ipc-health.ts
309
+ async function probeQueueOwnerHealth(sessionId) {
310
+ const ownerRecord = await readQueueOwnerRecord(sessionId);
311
+ if (!ownerRecord) return {
312
+ sessionId,
313
+ hasLease: false,
314
+ healthy: false,
315
+ socketReachable: false,
316
+ pidAlive: false
317
+ };
318
+ const owner = await readQueueOwnerStatus(sessionId);
319
+ if (!owner) return {
320
+ sessionId,
321
+ hasLease: false,
322
+ healthy: false,
323
+ socketReachable: false,
324
+ pidAlive: false
325
+ };
326
+ const pidAlive = owner.alive;
327
+ let socketReachable = false;
328
+ try {
329
+ const socket = await connectToQueueOwner(ownerRecord, 2);
330
+ if (socket) {
331
+ socketReachable = true;
332
+ if (!socket.destroyed) socket.end();
333
+ }
334
+ } catch {
335
+ socketReachable = false;
336
+ }
337
+ return {
338
+ sessionId,
339
+ hasLease: true,
340
+ healthy: socketReachable,
341
+ socketReachable,
342
+ pidAlive,
343
+ pid: owner.pid,
344
+ socketPath: owner.socketPath,
345
+ ownerGeneration: owner.ownerGeneration,
346
+ queueDepth: owner.queueDepth
347
+ };
348
+ }
349
+ //#endregion
350
+ //#region src/cli/queue/messages.ts
351
+ function asRecord$2(value) {
352
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
353
+ return value;
354
+ }
355
+ function isPermissionMode(value) {
356
+ return value === "approve-all" || value === "approve-reads" || value === "deny-all";
357
+ }
358
+ function isSessionResumePolicy(value) {
359
+ return value === "allow-new" || value === "same-session-only";
360
+ }
361
+ function isNonInteractivePermissionPolicy(value) {
362
+ return value === "deny" || value === "fail";
363
+ }
364
+ function isPermissionPolicy(value) {
365
+ const record = asRecord$2(value);
366
+ if (!record) return false;
367
+ return hasValidPermissionRuleLists(record) && hasValidPermissionDefaultAction(record);
368
+ }
369
+ function hasValidPermissionRuleLists(record) {
370
+ for (const key of [
371
+ "autoApprove",
372
+ "autoDeny",
373
+ "escalate"
374
+ ]) if (!isOptionalStringList(record[key])) return false;
375
+ return true;
376
+ }
377
+ function isOptionalStringList(value) {
378
+ return value == null || Array.isArray(value) && value.every((item) => typeof item === "string");
379
+ }
380
+ function hasValidPermissionDefaultAction(record) {
381
+ return record.defaultAction == null || record.defaultAction === "approve" || record.defaultAction === "deny" || record.defaultAction === "escalate";
382
+ }
383
+ function isOutputErrorCode(value) {
384
+ return typeof value === "string" && OUTPUT_ERROR_CODES.includes(value);
385
+ }
386
+ function isOutputErrorOrigin(value) {
387
+ return typeof value === "string" && OUTPUT_ERROR_ORIGINS.includes(value);
388
+ }
389
+ function isPermissionEscalationEvent(value) {
390
+ const event = asRecord$2(value);
391
+ return !!event && hasRequiredPermissionEscalationFields(event) && hasOptionalStringFields(event, [
392
+ "toolName",
393
+ "toolKind",
394
+ "matchedRule"
395
+ ]);
396
+ }
397
+ function hasRequiredPermissionEscalationFields(event) {
398
+ return event.type === "permission_escalation" && typeof event.sessionId === "string" && typeof event.toolCallId === "string" && typeof event.toolTitle === "string" && event.action === "escalate" && typeof event.message === "string" && typeof event.timestamp === "string";
399
+ }
400
+ function hasOptionalStringFields(record, keys) {
401
+ return keys.every((key) => record[key] == null || typeof record[key] === "string");
402
+ }
403
+ function parseSessionOptions(value) {
404
+ if (value == null) return;
405
+ const record = asRecord$2(value);
406
+ if (!record) return null;
407
+ const sessionOptions = {};
408
+ if (!assignSessionModel(sessionOptions, record.model)) return null;
409
+ if (!assignSessionAllowedTools(sessionOptions, record.allowedTools)) return null;
410
+ if (!assignSessionMaxTurns(sessionOptions, record.maxTurns)) return null;
411
+ if (!assignSessionSystemPrompt(sessionOptions, record.systemPrompt)) return null;
412
+ return sessionOptions;
413
+ }
414
+ function assignSessionModel(options, value) {
415
+ if (value == null) return true;
416
+ if (typeof value !== "string" || value.trim().length === 0) return false;
417
+ options.model = value;
418
+ return true;
419
+ }
420
+ function assignSessionAllowedTools(options, value) {
421
+ if (value == null) return true;
422
+ if (!Array.isArray(value) || value.some((tool) => typeof tool !== "string")) return false;
423
+ options.allowedTools = value;
424
+ return true;
425
+ }
426
+ function assignSessionMaxTurns(options, value) {
427
+ if (value == null) return true;
428
+ if (typeof value !== "number" || !Number.isFinite(value)) return false;
429
+ options.maxTurns = Math.max(1, Math.round(value));
430
+ return true;
431
+ }
432
+ function assignSessionSystemPrompt(options, value) {
433
+ if (value == null) return true;
434
+ if (typeof value === "string") {
435
+ options.systemPrompt = value;
436
+ return true;
437
+ }
438
+ const systemPrompt = asRecord$2(value);
439
+ if (!systemPrompt || typeof systemPrompt.append !== "string") return false;
440
+ options.systemPrompt = { append: systemPrompt.append };
441
+ return true;
442
+ }
443
+ function parseOwnerGeneration(value) {
444
+ if (value == null) return;
445
+ if (typeof value !== "number" || !Number.isInteger(value) || value <= 0) return null;
446
+ return value;
447
+ }
448
+ function parseNonNegativeInteger(value) {
449
+ if (value == null) return;
450
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) return null;
451
+ return Math.round(value);
452
+ }
453
+ function parseQueueRequest(raw) {
454
+ const request = asRecord$2(raw);
455
+ if (!request || typeof request.type !== "string" || typeof request.requestId !== "string") return null;
456
+ const ownerGeneration = parseOwnerGeneration(request.ownerGeneration);
457
+ if (ownerGeneration === null) return null;
458
+ return parseTypedQueueRequest(request, parseQueueRequestContext(request.requestId, ownerGeneration, request.timeoutMs));
459
+ }
460
+ function parseQueueRequestContext(requestId, ownerGeneration, timeoutRaw) {
461
+ return {
462
+ requestId,
463
+ ownerGeneration,
464
+ timeoutMs: parsePositiveTimeout(timeoutRaw)
465
+ };
466
+ }
467
+ function parsePositiveTimeout(value) {
468
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return;
469
+ return Math.round(value);
470
+ }
471
+ function parseTypedQueueRequest(request, context) {
472
+ switch (request.type) {
473
+ case "submit_prompt": return parseSubmitRequest(request, context);
474
+ case "cancel_prompt": return {
475
+ type: "cancel_prompt",
476
+ requestId: context.requestId,
477
+ ownerGeneration: context.ownerGeneration
478
+ };
479
+ case "close_session": return {
480
+ type: "close_session",
481
+ ...context
482
+ };
483
+ case "set_mode": return parseStringFieldRequest(request, context, "set_mode", "modeId");
484
+ case "set_model": return parseStringFieldRequest(request, context, "set_model", "modelId");
485
+ case "set_config_option": return parseSetConfigOptionRequest(request, context);
486
+ default: return null;
487
+ }
488
+ }
489
+ function parseSubmitRequest(request, context) {
490
+ const parsed = parseSubmitRequestFields(request);
491
+ if (!parsed) return null;
492
+ return {
493
+ type: "submit_prompt",
494
+ requestId: context.requestId,
495
+ ownerGeneration: context.ownerGeneration,
496
+ message: parsed.message,
497
+ prompt: parsed.prompt ?? textPrompt(parsed.message),
498
+ permissionMode: parsed.permissionMode,
499
+ ...parsed.resumePolicy !== void 0 ? { resumePolicy: parsed.resumePolicy } : {},
500
+ nonInteractivePermissions: parsed.nonInteractivePermissions,
501
+ ...parsed.permissionPolicy !== void 0 ? { permissionPolicy: parsed.permissionPolicy } : {},
502
+ timeoutMs: context.timeoutMs,
503
+ ...parsed.suppressSdkConsoleErrors !== void 0 ? { suppressSdkConsoleErrors: parsed.suppressSdkConsoleErrors } : {},
504
+ ...parsed.promptRetries !== void 0 ? { promptRetries: parsed.promptRetries } : {},
505
+ waitForCompletion: parsed.waitForCompletion,
506
+ ...parsed.sessionOptions !== void 0 ? { sessionOptions: parsed.sessionOptions } : {}
507
+ };
508
+ }
509
+ function parseSubmitRequestFields(request) {
510
+ const parsed = {
511
+ message: typeof request.message === "string" ? request.message : null,
512
+ prompt: parseOptionalValue(request.prompt, isPromptInput),
513
+ permissionMode: parseRequiredValue(request.permissionMode, isPermissionMode),
514
+ resumePolicy: parseOptionalValue(request.resumePolicy, isSessionResumePolicy),
515
+ nonInteractivePermissions: parseOptionalValue(request.nonInteractivePermissions, isNonInteractivePermissionPolicy),
516
+ permissionPolicy: parseOptionalValue(request.permissionPolicy, isPermissionPolicy),
517
+ suppressSdkConsoleErrors: parseOptionalBoolean(request.suppressSdkConsoleErrors),
518
+ promptRetries: parseNonNegativeInteger(request.promptRetries),
519
+ waitForCompletion: typeof request.waitForCompletion === "boolean" ? request.waitForCompletion : null,
520
+ sessionOptions: parseSessionOptions(request.sessionOptions)
521
+ };
522
+ if (Object.values(parsed).some((value) => value === null)) return null;
523
+ return parsed;
524
+ }
525
+ function parseOptionalValue(value, guard) {
526
+ if (value == null) return;
527
+ return guard(value) ? value : null;
528
+ }
529
+ function parseRequiredValue(value, guard) {
530
+ return guard(value) ? value : null;
531
+ }
532
+ function parseOptionalBoolean(value) {
533
+ if (value == null) return;
534
+ return typeof value === "boolean" ? value : null;
535
+ }
536
+ function parseStringFieldRequest(request, context, type, field) {
537
+ const value = parseNonEmptyString(request[field]);
538
+ if (!value) return null;
539
+ return {
540
+ type,
541
+ ...context,
542
+ [field]: value
543
+ };
544
+ }
545
+ function parseSetConfigOptionRequest(request, context) {
546
+ const configId = parseNonEmptyString(request.configId);
547
+ const value = parseNonEmptyString(request.value);
548
+ if (!configId || !value) return null;
549
+ return {
550
+ type: "set_config_option",
551
+ ...context,
552
+ configId,
553
+ value
554
+ };
555
+ }
556
+ function parseNonEmptyString(value) {
557
+ if (typeof value !== "string" || value.trim().length === 0) return null;
558
+ return value;
559
+ }
560
+ function parseSessionSendResult(raw) {
561
+ const result = asRecord$2(raw);
562
+ if (!result || !hasValidSessionSendResultCore(result)) return null;
563
+ const permissionStats = asRecord$2(result.permissionStats);
564
+ const record = asRecord$2(result.record);
565
+ if (!permissionStats || !record) return null;
566
+ if (!hasValidPermissionStats(permissionStats)) return null;
567
+ if (!hasValidSessionRecordShape(record)) return null;
568
+ return result;
569
+ }
570
+ function hasValidSessionSendResultCore(result) {
571
+ return typeof result.stopReason === "string" && typeof result.sessionId === "string" && typeof result.resumed === "boolean";
572
+ }
573
+ function hasValidPermissionStats(permissionStats) {
574
+ return [
575
+ "requested",
576
+ "approved",
577
+ "denied",
578
+ "cancelled"
579
+ ].every((key) => typeof permissionStats[key] === "number");
580
+ }
581
+ function hasValidSessionRecordShape(record) {
582
+ return hasStringFields(record, [
583
+ "acpxRecordId",
584
+ "acpSessionId",
585
+ "agentCommand",
586
+ "cwd",
587
+ "createdAt",
588
+ "lastUsedAt",
589
+ "updated_at"
590
+ ]) && Array.isArray(record.messages) && typeof record.lastSeq === "number" && Number.isInteger(record.lastSeq) && !!record.eventLog && typeof record.eventLog === "object";
591
+ }
592
+ function hasStringFields(record, keys) {
593
+ return keys.every((key) => typeof record[key] === "string");
594
+ }
595
+ function parseQueueOwnerMessage(raw) {
596
+ const message = asRecord$2(raw);
597
+ if (!message || typeof message.type !== "string" || typeof message.requestId !== "string") return null;
598
+ const ownerGeneration = parseOwnerGeneration(message.ownerGeneration);
599
+ if (ownerGeneration === null) return null;
600
+ return parseTypedQueueOwnerMessage(message, {
601
+ requestId: message.requestId,
602
+ ownerGeneration
603
+ });
604
+ }
605
+ function parseTypedQueueOwnerMessage(message, context) {
606
+ const parser = queueOwnerMessageParser(message.type);
607
+ return parser ? parser(message, context) : null;
608
+ }
609
+ const QUEUE_OWNER_MESSAGE_PARSERS = {
610
+ accepted: (_message, context) => ({
611
+ type: "accepted",
612
+ ...context
613
+ }),
614
+ event: parseEventOwnerMessage,
615
+ permission_escalation: parsePermissionEscalationOwnerMessage,
616
+ result: parseResultOwnerMessage,
617
+ cancel_result: (message, context) => parseBooleanResultOwnerMessage(message, context, "cancel_result", "cancelled"),
618
+ close_session_result: (message, context) => parseBooleanResultOwnerMessage(message, context, "close_session_result", "closed"),
619
+ set_mode_result: (message, context) => parseStringResultOwnerMessage(message, context, "set_mode_result", "modeId"),
620
+ set_model_result: (message, context) => parseStringResultOwnerMessage(message, context, "set_model_result", "modelId"),
621
+ set_config_option_result: parseSetConfigOptionOwnerMessage,
622
+ error: parseErrorOwnerMessage
623
+ };
624
+ function queueOwnerMessageParser(type) {
625
+ return Object.hasOwn(QUEUE_OWNER_MESSAGE_PARSERS, type) ? QUEUE_OWNER_MESSAGE_PARSERS[type] : void 0;
626
+ }
627
+ function parseEventOwnerMessage(message, context) {
628
+ if (!isAcpJsonRpcMessage(message.message)) return null;
629
+ return {
630
+ type: "event",
631
+ ...context,
632
+ message: message.message
633
+ };
634
+ }
635
+ function parsePermissionEscalationOwnerMessage(message, context) {
636
+ if (!isPermissionEscalationEvent(message.event)) return null;
637
+ return {
638
+ type: "permission_escalation",
639
+ ...context,
640
+ event: message.event
641
+ };
642
+ }
643
+ function parseResultOwnerMessage(message, context) {
644
+ const result = parseSessionSendResult(message.result);
645
+ if (!result) return null;
646
+ return {
647
+ type: "result",
648
+ ...context,
649
+ result
650
+ };
651
+ }
652
+ function parseBooleanResultOwnerMessage(message, context, type, field) {
653
+ if (typeof message[field] !== "boolean") return null;
654
+ return {
655
+ type,
656
+ ...context,
657
+ [field]: message[field]
658
+ };
659
+ }
660
+ function parseStringResultOwnerMessage(message, context, type, field) {
661
+ if (typeof message[field] !== "string") return null;
662
+ return {
663
+ type,
664
+ ...context,
665
+ [field]: message[field]
666
+ };
667
+ }
668
+ function parseSetConfigOptionOwnerMessage(message, context) {
669
+ const response = asRecord$2(message.response);
670
+ if (!response || !Array.isArray(response.configOptions)) return null;
671
+ return {
672
+ type: "set_config_option_result",
673
+ ...context,
674
+ response
675
+ };
676
+ }
677
+ function parseErrorOwnerMessage(message, context) {
678
+ if (!isValidOwnerErrorCore(message)) return null;
679
+ const outputAlreadyEmitted = typeof message.outputAlreadyEmitted === "boolean" ? message.outputAlreadyEmitted : void 0;
680
+ return {
681
+ type: "error",
682
+ ...context,
683
+ code: message.code,
684
+ detailCode: parseNonEmptyString(message.detailCode) ?? void 0,
685
+ origin: message.origin,
686
+ message: message.message,
687
+ retryable: typeof message.retryable === "boolean" ? message.retryable : void 0,
688
+ acp: toAcpErrorPayload(message.acp),
689
+ ...outputAlreadyEmitted === void 0 ? {} : { outputAlreadyEmitted }
690
+ };
691
+ }
692
+ function isValidOwnerErrorCore(message) {
693
+ return typeof message.message === "string" && isOutputErrorCode(message.code) && isOutputErrorOrigin(message.origin);
694
+ }
695
+ //#endregion
696
+ //#region src/cli/queue/ipc-server.ts
697
+ function makeQueueOwnerError(requestId, message, detailCode, options = {}) {
698
+ return {
699
+ type: "error",
700
+ requestId,
701
+ ownerGeneration: void 0,
702
+ code: "RUNTIME",
703
+ detailCode,
704
+ origin: "queue",
705
+ retryable: options.retryable,
706
+ message
707
+ };
708
+ }
709
+ function makeQueueOwnerErrorFromUnknown(requestId, error, detailCode, options = {}) {
710
+ const normalized = normalizeOutputError(error, {
711
+ defaultCode: "RUNTIME",
712
+ origin: "queue",
713
+ detailCode,
714
+ retryable: options.retryable
715
+ });
716
+ return {
717
+ type: "error",
718
+ requestId,
719
+ code: normalized.code,
720
+ detailCode: normalized.detailCode,
721
+ origin: normalized.origin,
722
+ message: normalized.message,
723
+ retryable: normalized.retryable,
724
+ acp: normalized.acp
725
+ };
726
+ }
727
+ function writeQueueMessage(socket, message) {
728
+ if (socket.destroyed || !socket.writable) return;
729
+ socket.write(`${JSON.stringify(message)}\n`);
730
+ }
731
+ var SessionQueueOwner = class SessionQueueOwner {
732
+ server;
733
+ controlHandlers;
734
+ ownerGeneration;
735
+ maxQueueDepth;
736
+ onQueueDepthChanged;
737
+ pending = [];
738
+ waiters = [];
739
+ closed = false;
740
+ constructor(server, controlHandlers, lease, options) {
741
+ this.server = server;
742
+ this.controlHandlers = controlHandlers;
743
+ this.ownerGeneration = lease.ownerGeneration;
744
+ this.maxQueueDepth = Math.max(1, Math.round(options.maxQueueDepth));
745
+ this.onQueueDepthChanged = options.onQueueDepthChanged;
746
+ }
747
+ static async start(lease, controlHandlers, options = { maxQueueDepth: 16 }) {
748
+ const ownerRef = { current: void 0 };
749
+ const server = net.createServer((socket) => {
750
+ ownerRef.current?.handleConnection(socket);
751
+ });
752
+ ownerRef.current = new SessionQueueOwner(server, controlHandlers, lease, options);
753
+ await new Promise((resolve, reject) => {
754
+ const onListening = () => {
755
+ server.off("error", onError);
756
+ resolve();
757
+ };
758
+ const onError = (error) => {
759
+ server.off("listening", onListening);
760
+ reject(error);
761
+ };
762
+ server.once("listening", onListening);
763
+ server.once("error", onError);
764
+ server.listen(lease.socketPath);
765
+ });
766
+ return ownerRef.current;
767
+ }
768
+ async close() {
769
+ if (this.closed) return;
770
+ this.closed = true;
771
+ for (const waiter of this.waiters.splice(0)) waiter(void 0);
772
+ for (const task of this.pending.splice(0)) {
773
+ if (task.waitForCompletion) task.send(makeQueueOwnerError(task.requestId, "Queue owner shutting down before prompt execution", "QUEUE_OWNER_SHUTTING_DOWN", { retryable: true }));
774
+ task.close();
775
+ }
776
+ this.emitQueueDepth();
777
+ await new Promise((resolve) => {
778
+ this.server.close(() => resolve());
779
+ });
780
+ }
781
+ async nextTask(timeoutMs) {
782
+ if (this.pending.length > 0) {
783
+ const task = this.pending.shift();
784
+ this.emitQueueDepth();
785
+ if (task) recordPerfDuration("queue.owner.wait_ms", Date.now() - task.enqueuedAt);
786
+ return task;
787
+ }
788
+ if (this.closed) return;
789
+ return await new Promise((resolve) => {
790
+ const timer = timeoutMs != null && setTimeout(() => {
791
+ const index = this.waiters.indexOf(waiter);
792
+ if (index >= 0) this.waiters.splice(index, 1);
793
+ resolve(void 0);
794
+ }, Math.max(0, timeoutMs));
795
+ const waiter = (task) => {
796
+ if (timer) clearTimeout(timer);
797
+ resolve(task);
798
+ };
799
+ this.waiters.push(waiter);
800
+ });
801
+ }
802
+ queueDepth() {
803
+ return this.pending.length;
804
+ }
805
+ emitQueueDepth() {
806
+ this.onQueueDepthChanged?.(this.pending.length);
807
+ }
808
+ enqueue(task) {
809
+ if (this.closed) {
810
+ if (task.waitForCompletion) task.send(makeQueueOwnerError(task.requestId, "Queue owner is shutting down", "QUEUE_OWNER_SHUTTING_DOWN", { retryable: true }));
811
+ task.close();
812
+ return;
813
+ }
814
+ const waiter = this.waiters.shift();
815
+ if (waiter) {
816
+ waiter(task);
817
+ return;
818
+ }
819
+ if (this.pending.length >= this.maxQueueDepth) {
820
+ if (task.waitForCompletion) task.send({
821
+ ...makeQueueOwnerError(task.requestId, `Queue owner is overloaded (${this.pending.length}/${this.maxQueueDepth} queued)`, "QUEUE_OWNER_OVERLOADED", { retryable: true }),
822
+ ownerGeneration: this.ownerGeneration
823
+ });
824
+ task.close();
825
+ return;
826
+ }
827
+ this.pending.push(task);
828
+ this.emitQueueDepth();
829
+ }
830
+ handleControlRequest(options) {
831
+ writeQueueMessage(options.socket, {
832
+ type: "accepted",
833
+ requestId: options.requestId,
834
+ ownerGeneration: this.ownerGeneration
835
+ });
836
+ options.run().then((message) => {
837
+ writeQueueMessage(options.socket, {
838
+ ...message,
839
+ ownerGeneration: this.ownerGeneration
840
+ });
841
+ }).catch((error) => {
842
+ writeQueueMessage(options.socket, {
843
+ ...makeQueueOwnerErrorFromUnknown(options.requestId, error, "QUEUE_CONTROL_REQUEST_FAILED"),
844
+ ownerGeneration: this.ownerGeneration
845
+ });
846
+ }).finally(() => {
847
+ if (!options.socket.destroyed) options.socket.end();
848
+ });
849
+ }
850
+ failRequest(socket, requestId, message, detailCode) {
851
+ writeQueueMessage(socket, {
852
+ ...makeQueueOwnerError(requestId, message, detailCode, { retryable: false }),
853
+ ownerGeneration: this.ownerGeneration
854
+ });
855
+ socket.end();
856
+ }
857
+ parseRequestLine(socket, line) {
858
+ let parsed;
859
+ try {
860
+ parsed = JSON.parse(line);
861
+ } catch {
862
+ this.failRequest(socket, "unknown", "Invalid queue request payload", "QUEUE_REQUEST_PAYLOAD_INVALID_JSON");
863
+ return;
864
+ }
865
+ const request = parseQueueRequest(parsed);
866
+ if (!request) this.failRequest(socket, "unknown", "Invalid queue request", "QUEUE_REQUEST_INVALID");
867
+ return request ?? void 0;
868
+ }
869
+ rejectStaleOwnerGeneration(socket, request) {
870
+ if (request.ownerGeneration === void 0 || this.ownerGeneration === void 0 || request.ownerGeneration === this.ownerGeneration) return false;
871
+ this.failRequest(socket, request.requestId, "Queue request targeted a stale queue owner generation", "QUEUE_OWNER_GENERATION_MISMATCH");
872
+ return true;
873
+ }
874
+ handleControlQueueRequest(socket, request) {
875
+ if (request.type === "cancel_prompt") {
876
+ this.handleControlRequest({
877
+ socket,
878
+ requestId: request.requestId,
879
+ run: async () => ({
880
+ type: "cancel_result",
881
+ requestId: request.requestId,
882
+ cancelled: await this.controlHandlers.cancelPrompt()
883
+ })
884
+ });
885
+ return true;
886
+ }
887
+ if (request.type === "close_session") {
888
+ this.handleControlRequest({
889
+ socket,
890
+ requestId: request.requestId,
891
+ run: async () => ({
892
+ type: "close_session_result",
893
+ requestId: request.requestId,
894
+ closed: await this.controlHandlers.closeSession(request.timeoutMs)
895
+ })
896
+ });
897
+ return true;
898
+ }
899
+ return this.handleSessionControlQueueRequest(socket, request);
900
+ }
901
+ handleSessionControlQueueRequest(socket, request) {
902
+ if (request.type === "set_mode") {
903
+ this.handleControlRequest({
904
+ socket,
905
+ requestId: request.requestId,
906
+ run: async () => {
907
+ await this.controlHandlers.setSessionMode(request.modeId, request.timeoutMs);
908
+ return {
909
+ type: "set_mode_result",
910
+ requestId: request.requestId,
911
+ modeId: request.modeId
912
+ };
913
+ }
914
+ });
915
+ return true;
916
+ }
917
+ if (request.type === "set_model") {
918
+ this.handleControlRequest({
919
+ socket,
920
+ requestId: request.requestId,
921
+ run: async () => {
922
+ await this.controlHandlers.setSessionModel(request.modelId, request.timeoutMs);
923
+ return {
924
+ type: "set_model_result",
925
+ requestId: request.requestId,
926
+ modelId: request.modelId
927
+ };
928
+ }
929
+ });
930
+ return true;
931
+ }
932
+ if (request.type === "set_config_option") {
933
+ this.handleControlRequest({
934
+ socket,
935
+ requestId: request.requestId,
936
+ run: async () => ({
937
+ type: "set_config_option_result",
938
+ requestId: request.requestId,
939
+ response: await this.controlHandlers.setSessionConfigOption(request.configId, request.value, request.timeoutMs)
940
+ })
941
+ });
942
+ return true;
943
+ }
944
+ return false;
945
+ }
946
+ enqueuePromptRequest(socket, request) {
947
+ const task = {
948
+ requestId: request.requestId,
949
+ message: request.message,
950
+ prompt: request.prompt ?? textPrompt(request.message),
951
+ permissionMode: request.permissionMode,
952
+ resumePolicy: request.resumePolicy,
953
+ nonInteractivePermissions: request.nonInteractivePermissions,
954
+ permissionPolicy: request.permissionPolicy,
955
+ timeoutMs: request.timeoutMs,
956
+ suppressSdkConsoleErrors: request.suppressSdkConsoleErrors,
957
+ promptRetries: request.promptRetries,
958
+ sessionOptions: request.sessionOptions,
959
+ waitForCompletion: request.waitForCompletion,
960
+ enqueuedAt: Date.now(),
961
+ send: (message) => {
962
+ writeQueueMessage(socket, {
963
+ ...message,
964
+ ownerGeneration: this.ownerGeneration
965
+ });
966
+ },
967
+ close: () => {
968
+ if (!socket.destroyed) socket.end();
969
+ }
970
+ };
971
+ writeQueueMessage(socket, {
972
+ type: "accepted",
973
+ requestId: request.requestId,
974
+ ownerGeneration: this.ownerGeneration
975
+ });
976
+ if (!request.waitForCompletion) task.close();
977
+ this.enqueue(task);
978
+ }
979
+ handleConnection(socket) {
980
+ socket.setEncoding("utf8");
981
+ if (this.closed) {
982
+ writeQueueMessage(socket, makeQueueOwnerError("unknown", "Queue owner is closed", "QUEUE_OWNER_CLOSED", { retryable: true }));
983
+ socket.end();
984
+ return;
985
+ }
986
+ let buffer = "";
987
+ let handled = false;
988
+ const processLine = (line) => {
989
+ if (handled) return;
990
+ handled = true;
991
+ const request = this.parseRequestLine(socket, line);
992
+ if (!request || this.rejectStaleOwnerGeneration(socket, request)) return;
993
+ if (this.handleControlQueueRequest(socket, request)) return;
994
+ if (request.type !== "submit_prompt") {
995
+ this.failRequest(socket, request.requestId, "Invalid queue request", "QUEUE_REQUEST_INVALID");
996
+ return;
997
+ }
998
+ this.enqueuePromptRequest(socket, request);
999
+ };
1000
+ socket.on("data", (chunk) => {
1001
+ buffer += chunk;
1002
+ let index = buffer.indexOf("\n");
1003
+ while (index >= 0) {
1004
+ const line = buffer.slice(0, index).trim();
1005
+ buffer = buffer.slice(index + 1);
1006
+ if (line.length > 0) processLine(line);
1007
+ index = buffer.indexOf("\n");
1008
+ }
1009
+ });
1010
+ socket.on("error", () => {});
1011
+ }
1012
+ };
1013
+ //#endregion
1014
+ //#region src/cli/queue/ipc.ts
1015
+ const MAX_MESSAGE_BUFFER_SIZE = 10 * 1024 * 1024;
1016
+ const STALE_OWNER_PROTOCOL_DETAIL_CODES = new Set(["QUEUE_PROTOCOL_MALFORMED_MESSAGE", "QUEUE_PROTOCOL_UNEXPECTED_RESPONSE"]);
1017
+ async function maybeRecoverStaleOwnerAfterProtocolMismatch(params) {
1018
+ if (!(params.error instanceof QueueProtocolError)) return false;
1019
+ const detailCode = params.error.detailCode;
1020
+ if (!detailCode || !STALE_OWNER_PROTOCOL_DETAIL_CODES.has(detailCode)) return false;
1021
+ await terminateQueueOwnerForSession(params.sessionId).catch(() => {});
1022
+ incrementPerfCounter("queue.owner.stale_recovered");
1023
+ if (params.verbose) process.stderr.write(`[acpx] dropped stale queue owner metadata after protocol mismatch for session ${params.sessionId} (${detailCode})\n`);
1024
+ return true;
1025
+ }
1026
+ function assertOwnerGeneration(owner, message) {
1027
+ if (owner.ownerGeneration !== void 0 && message.ownerGeneration !== void 0 && message.ownerGeneration !== owner.ownerGeneration) throw new QueueProtocolError("Queue owner returned mismatched generation", {
1028
+ detailCode: "QUEUE_OWNER_GENERATION_MISMATCH",
1029
+ origin: "queue",
1030
+ retryable: true
1031
+ });
1032
+ return message;
1033
+ }
1034
+ function queueConnectionErrorFromOwner(message, outputAlreadyEmitted) {
1035
+ return new QueueConnectionError(message.message, {
1036
+ outputCode: message.code,
1037
+ detailCode: message.detailCode,
1038
+ origin: message.origin ?? "queue",
1039
+ retryable: message.retryable,
1040
+ acp: message.acp,
1041
+ ...outputAlreadyEmitted ? { outputAlreadyEmitted: true } : {}
1042
+ });
1043
+ }
1044
+ function makeMalformedQueueMessageError() {
1045
+ return new QueueProtocolError("Queue owner sent malformed message", {
1046
+ detailCode: "QUEUE_PROTOCOL_MALFORMED_MESSAGE",
1047
+ origin: "queue",
1048
+ retryable: true
1049
+ });
1050
+ }
1051
+ function emitQueueOwnerError(formatter, policy, sessionId, message) {
1052
+ formatter.setContext({ sessionId });
1053
+ const queueErrorAlreadyEmitted = policy?.queueErrorAlreadyEmitted ?? true;
1054
+ if (message.outputAlreadyEmitted !== true || !queueErrorAlreadyEmitted) {
1055
+ formatter.onError({
1056
+ code: message.code ?? "RUNTIME",
1057
+ detailCode: message.detailCode,
1058
+ origin: message.origin ?? "queue",
1059
+ message: message.message,
1060
+ retryable: message.retryable,
1061
+ acp: message.acp
1062
+ });
1063
+ formatter.flush();
1064
+ }
1065
+ return queueConnectionErrorFromOwner(message, queueErrorAlreadyEmitted);
1066
+ }
1067
+ function parseQueueOwnerResponseLine(owner, requestId, line) {
1068
+ let parsed;
1069
+ try {
1070
+ parsed = JSON.parse(line);
1071
+ } catch {
1072
+ throw new QueueProtocolError("Queue owner sent invalid JSON payload", {
1073
+ detailCode: "QUEUE_PROTOCOL_INVALID_JSON",
1074
+ origin: "queue",
1075
+ retryable: true
1076
+ });
1077
+ }
1078
+ const parsedMessage = parseQueueOwnerMessage(parsed);
1079
+ if (!parsedMessage) throw makeMalformedQueueMessageError();
1080
+ const message = assertOwnerGeneration(owner, parsedMessage);
1081
+ if (message.requestId !== requestId) throw makeMalformedQueueMessageError();
1082
+ return message;
1083
+ }
1084
+ async function runQueueOwnerRequest(options) {
1085
+ const socket = await connectToQueueOwner(options.owner);
1086
+ if (!socket) return;
1087
+ socket.setEncoding("utf8");
1088
+ return await new Promise((resolve, reject) => {
1089
+ let settled = false;
1090
+ let buffer = "";
1091
+ const state = { acknowledged: false };
1092
+ const finishResolve = (result) => {
1093
+ if (settled) return;
1094
+ settled = true;
1095
+ socket.removeAllListeners();
1096
+ if (!socket.destroyed) socket.end();
1097
+ resolve(result);
1098
+ };
1099
+ const finishReject = (error) => {
1100
+ if (settled) return;
1101
+ settled = true;
1102
+ socket.removeAllListeners();
1103
+ if (!socket.destroyed) socket.destroy();
1104
+ reject(error);
1105
+ };
1106
+ const controls = {
1107
+ state,
1108
+ resolve: finishResolve,
1109
+ reject: finishReject
1110
+ };
1111
+ const processLine = (line) => {
1112
+ let message;
1113
+ try {
1114
+ message = parseQueueOwnerResponseLine(options.owner, options.request.requestId, line);
1115
+ } catch (error) {
1116
+ finishReject(error);
1117
+ return;
1118
+ }
1119
+ if (message.type === "accepted") {
1120
+ state.acknowledged = true;
1121
+ options.onAccepted?.(controls);
1122
+ return;
1123
+ }
1124
+ options.onMessage(message, controls);
1125
+ };
1126
+ socket.on("data", (chunk) => {
1127
+ buffer += chunk;
1128
+ if (buffer.length > 10485760) {
1129
+ socket.destroy();
1130
+ finishReject(/* @__PURE__ */ new Error(`Message buffer exceeded ${MAX_MESSAGE_BUFFER_SIZE} bytes`));
1131
+ return;
1132
+ }
1133
+ let index = buffer.indexOf("\n");
1134
+ while (index >= 0) {
1135
+ const line = buffer.slice(0, index).trim();
1136
+ buffer = buffer.slice(index + 1);
1137
+ if (line.length > 0) processLine(line);
1138
+ index = buffer.indexOf("\n");
1139
+ }
1140
+ });
1141
+ socket.once("error", (error) => {
1142
+ finishReject(error);
1143
+ });
1144
+ socket.once("close", () => {
1145
+ if (settled) return;
1146
+ options.onClose(controls);
1147
+ });
1148
+ socket.write(`${JSON.stringify(options.request)}\n`);
1149
+ });
1150
+ }
1151
+ function missingQueueAckError() {
1152
+ return new QueueConnectionError("Queue owner did not acknowledge request", {
1153
+ detailCode: "QUEUE_ACK_MISSING",
1154
+ origin: "queue",
1155
+ retryable: true
1156
+ });
1157
+ }
1158
+ function unexpectedQueueResponseError() {
1159
+ return new QueueProtocolError("Queue owner returned unexpected response", {
1160
+ detailCode: "QUEUE_PROTOCOL_UNEXPECTED_RESPONSE",
1161
+ origin: "queue",
1162
+ retryable: true
1163
+ });
1164
+ }
1165
+ function handleAcknowledgedSubmitMessage(message, controls, formatter) {
1166
+ if (message.type === "event") {
1167
+ formatter.onAcpMessage(message.message);
1168
+ return;
1169
+ }
1170
+ if (message.type === "permission_escalation") {
1171
+ formatter.onPermissionEscalation(message.event);
1172
+ return;
1173
+ }
1174
+ if (message.type === "result") {
1175
+ formatter.flush();
1176
+ controls.resolve(message.result);
1177
+ return;
1178
+ }
1179
+ controls.reject(unexpectedQueueResponseError());
1180
+ }
1181
+ function handleSubmitQueueOwnerMessage(message, controls, options) {
1182
+ if (message.type === "error") {
1183
+ controls.reject(emitQueueOwnerError(options.outputFormatter, options.errorEmissionPolicy, options.sessionId, message));
1184
+ return;
1185
+ }
1186
+ if (!controls.state.acknowledged) {
1187
+ controls.reject(missingQueueAckError());
1188
+ return;
1189
+ }
1190
+ handleAcknowledgedSubmitMessage(message, controls, options.outputFormatter);
1191
+ }
1192
+ async function submitToQueueOwner(owner, options) {
1193
+ const requestId = randomUUID();
1194
+ const request = {
1195
+ type: "submit_prompt",
1196
+ requestId,
1197
+ ownerGeneration: owner.ownerGeneration,
1198
+ message: options.message,
1199
+ prompt: options.prompt,
1200
+ permissionMode: options.permissionMode,
1201
+ resumePolicy: options.resumePolicy,
1202
+ nonInteractivePermissions: options.nonInteractivePermissions,
1203
+ permissionPolicy: options.permissionPolicy,
1204
+ timeoutMs: options.timeoutMs,
1205
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
1206
+ promptRetries: options.promptRetries ?? 0,
1207
+ waitForCompletion: options.waitForCompletion,
1208
+ sessionOptions: options.sessionOptions
1209
+ };
1210
+ options.outputFormatter.setContext({ sessionId: options.sessionId });
1211
+ return await runQueueOwnerRequest({
1212
+ owner,
1213
+ request,
1214
+ onAccepted: ({ resolve }) => {
1215
+ options.outputFormatter.setContext({ sessionId: options.sessionId });
1216
+ if (!options.waitForCompletion) resolve({
1217
+ queued: true,
1218
+ sessionId: options.sessionId,
1219
+ requestId
1220
+ });
1221
+ },
1222
+ onMessage: (message, controls) => {
1223
+ handleSubmitQueueOwnerMessage(message, controls, options);
1224
+ },
1225
+ onClose: ({ state, resolve, reject }) => {
1226
+ if (!state.acknowledged) {
1227
+ reject(new QueueConnectionError("Queue owner disconnected before acknowledging request", {
1228
+ detailCode: "QUEUE_DISCONNECTED_BEFORE_ACK",
1229
+ origin: "queue",
1230
+ retryable: true
1231
+ }));
1232
+ return;
1233
+ }
1234
+ if (!options.waitForCompletion) {
1235
+ resolve({
1236
+ queued: true,
1237
+ sessionId: options.sessionId,
1238
+ requestId
1239
+ });
1240
+ return;
1241
+ }
1242
+ reject(new QueueConnectionError("Queue owner disconnected before prompt completion", {
1243
+ detailCode: "QUEUE_DISCONNECTED_BEFORE_COMPLETION",
1244
+ origin: "queue",
1245
+ retryable: true
1246
+ }));
1247
+ }
1248
+ });
1249
+ }
1250
+ async function submitControlToQueueOwner(owner, request, isExpectedResponse) {
1251
+ return await runQueueOwnerRequest({
1252
+ owner,
1253
+ request,
1254
+ onMessage: (message, { state, resolve, reject }) => {
1255
+ if (message.type === "error") {
1256
+ reject(new QueueConnectionError(message.message, {
1257
+ outputCode: message.code,
1258
+ detailCode: message.detailCode,
1259
+ origin: message.origin ?? "queue",
1260
+ retryable: message.retryable,
1261
+ acp: message.acp
1262
+ }));
1263
+ return;
1264
+ }
1265
+ if (!state.acknowledged) {
1266
+ reject(new QueueConnectionError("Queue owner did not acknowledge request", {
1267
+ detailCode: "QUEUE_ACK_MISSING",
1268
+ origin: "queue",
1269
+ retryable: true
1270
+ }));
1271
+ return;
1272
+ }
1273
+ if (!isExpectedResponse(message)) {
1274
+ reject(new QueueProtocolError("Queue owner returned unexpected response", {
1275
+ detailCode: "QUEUE_PROTOCOL_UNEXPECTED_RESPONSE",
1276
+ origin: "queue",
1277
+ retryable: true
1278
+ }));
1279
+ return;
1280
+ }
1281
+ resolve(message);
1282
+ },
1283
+ onClose: ({ state, reject }) => {
1284
+ if (!state.acknowledged) {
1285
+ reject(new QueueConnectionError("Queue owner disconnected before acknowledging request", {
1286
+ detailCode: "QUEUE_DISCONNECTED_BEFORE_ACK",
1287
+ origin: "queue",
1288
+ retryable: true
1289
+ }));
1290
+ return;
1291
+ }
1292
+ reject(new QueueConnectionError("Queue owner disconnected before responding", {
1293
+ detailCode: "QUEUE_DISCONNECTED_BEFORE_COMPLETION",
1294
+ origin: "queue",
1295
+ retryable: true
1296
+ }));
1297
+ }
1298
+ });
1299
+ }
1300
+ async function submitCancelToQueueOwner(owner) {
1301
+ const request = {
1302
+ type: "cancel_prompt",
1303
+ requestId: randomUUID(),
1304
+ ownerGeneration: owner.ownerGeneration
1305
+ };
1306
+ const response = await submitControlToQueueOwner(owner, request, (message) => message.type === "cancel_result");
1307
+ if (!response) return;
1308
+ if (response.requestId !== request.requestId) throw new QueueProtocolError("Queue owner returned mismatched cancel response", {
1309
+ detailCode: "QUEUE_PROTOCOL_MALFORMED_MESSAGE",
1310
+ origin: "queue",
1311
+ retryable: true
1312
+ });
1313
+ return response.cancelled;
1314
+ }
1315
+ async function submitSetModeToQueueOwner(owner, modeId, timeoutMs) {
1316
+ const request = {
1317
+ type: "set_mode",
1318
+ requestId: randomUUID(),
1319
+ ownerGeneration: owner.ownerGeneration,
1320
+ modeId,
1321
+ timeoutMs
1322
+ };
1323
+ const response = await submitControlToQueueOwner(owner, request, (message) => message.type === "set_mode_result");
1324
+ if (!response) return;
1325
+ if (response.requestId !== request.requestId) throw new QueueProtocolError("Queue owner returned mismatched set_mode response", {
1326
+ detailCode: "QUEUE_PROTOCOL_MALFORMED_MESSAGE",
1327
+ origin: "queue",
1328
+ retryable: true
1329
+ });
1330
+ return true;
1331
+ }
1332
+ async function submitSetModelToQueueOwner(owner, modelId, timeoutMs) {
1333
+ const request = {
1334
+ type: "set_model",
1335
+ requestId: randomUUID(),
1336
+ ownerGeneration: owner.ownerGeneration,
1337
+ modelId,
1338
+ timeoutMs
1339
+ };
1340
+ const response = await submitControlToQueueOwner(owner, request, (message) => message.type === "set_model_result");
1341
+ if (!response) return;
1342
+ if (response.requestId !== request.requestId) throw new QueueProtocolError("Queue owner returned mismatched set_model response", {
1343
+ detailCode: "QUEUE_PROTOCOL_MALFORMED_MESSAGE",
1344
+ origin: "queue",
1345
+ retryable: true
1346
+ });
1347
+ return true;
1348
+ }
1349
+ async function submitSetConfigOptionToQueueOwner(owner, configId, value, timeoutMs) {
1350
+ const request = {
1351
+ type: "set_config_option",
1352
+ requestId: randomUUID(),
1353
+ ownerGeneration: owner.ownerGeneration,
1354
+ configId,
1355
+ value,
1356
+ timeoutMs
1357
+ };
1358
+ const response = await submitControlToQueueOwner(owner, request, (message) => message.type === "set_config_option_result");
1359
+ if (!response) return;
1360
+ if (response.requestId !== request.requestId) throw new QueueProtocolError("Queue owner returned mismatched set_config_option response", {
1361
+ detailCode: "QUEUE_PROTOCOL_MALFORMED_MESSAGE",
1362
+ origin: "queue",
1363
+ retryable: true
1364
+ });
1365
+ return response.response;
1366
+ }
1367
+ async function submitCloseSessionToQueueOwner(owner, timeoutMs) {
1368
+ const request = {
1369
+ type: "close_session",
1370
+ requestId: randomUUID(),
1371
+ ownerGeneration: owner.ownerGeneration,
1372
+ timeoutMs
1373
+ };
1374
+ const response = await submitControlToQueueOwner(owner, request, (message) => message.type === "close_session_result");
1375
+ if (!response) return;
1376
+ if (response.requestId !== request.requestId) throw new QueueProtocolError("Queue owner returned mismatched close_session response", {
1377
+ detailCode: "QUEUE_PROTOCOL_MALFORMED_MESSAGE",
1378
+ origin: "queue",
1379
+ retryable: true
1380
+ });
1381
+ return response.closed;
1382
+ }
1383
+ async function trySubmitToRunningOwner(options) {
1384
+ const owner = await readQueueOwnerRecord(options.sessionId);
1385
+ if (!owner) return;
1386
+ let submitted;
1387
+ try {
1388
+ submitted = await submitToQueueOwner(owner, options);
1389
+ } catch (error) {
1390
+ if (await maybeRecoverStaleOwnerAfterProtocolMismatch({
1391
+ sessionId: options.sessionId,
1392
+ owner,
1393
+ error,
1394
+ verbose: options.verbose
1395
+ })) return;
1396
+ throw error;
1397
+ }
1398
+ if (submitted) {
1399
+ if (options.verbose) process.stderr.write(`[acpx] queued prompt on active owner pid ${owner.pid} for session ${options.sessionId}\n`);
1400
+ return submitted;
1401
+ }
1402
+ if (!(await probeQueueOwnerHealth(options.sessionId)).hasLease) return;
1403
+ throw new QueueConnectionError("Session queue owner is running but not accepting queue requests", {
1404
+ detailCode: "QUEUE_NOT_ACCEPTING_REQUESTS",
1405
+ origin: "queue",
1406
+ retryable: true
1407
+ });
1408
+ }
1409
+ async function tryCloseSessionOnRunningOwner(options) {
1410
+ const owner = await readQueueOwnerRecord(options.sessionId);
1411
+ if (!owner) return;
1412
+ const closed = await submitCloseSessionToQueueOwner(owner, options.timeoutMs);
1413
+ if (closed !== void 0) {
1414
+ if (options.verbose) process.stderr.write(`[acpx] requested session/close on active owner pid ${owner.pid} for session ${options.sessionId}\n`);
1415
+ return closed;
1416
+ }
1417
+ if (!(await probeQueueOwnerHealth(options.sessionId)).hasLease) return;
1418
+ throw new QueueConnectionError("Session queue owner is running but not accepting close_session requests", {
1419
+ detailCode: "QUEUE_NOT_ACCEPTING_REQUESTS",
1420
+ origin: "queue",
1421
+ retryable: true
1422
+ });
1423
+ }
1424
+ async function tryCancelOnRunningOwner(options) {
1425
+ const owner = await readQueueOwnerRecord(options.sessionId);
1426
+ if (!owner) return;
1427
+ const cancelled = await submitCancelToQueueOwner(owner);
1428
+ if (cancelled !== void 0) {
1429
+ if (options.verbose) process.stderr.write(`[acpx] requested cancel on active owner pid ${owner.pid} for session ${options.sessionId}\n`);
1430
+ return cancelled;
1431
+ }
1432
+ if (!(await probeQueueOwnerHealth(options.sessionId)).hasLease) return;
1433
+ throw new QueueConnectionError("Session queue owner is running but not accepting cancel requests", {
1434
+ detailCode: "QUEUE_NOT_ACCEPTING_REQUESTS",
1435
+ origin: "queue",
1436
+ retryable: true
1437
+ });
1438
+ }
1439
+ async function trySetModeOnRunningOwner(sessionId, modeId, timeoutMs, verbose) {
1440
+ const owner = await readQueueOwnerRecord(sessionId);
1441
+ if (!owner) return;
1442
+ if (await submitSetModeToQueueOwner(owner, modeId, timeoutMs)) {
1443
+ if (verbose) process.stderr.write(`[acpx] requested session/set_mode on owner pid ${owner.pid} for session ${sessionId}\n`);
1444
+ return true;
1445
+ }
1446
+ if (!(await probeQueueOwnerHealth(sessionId)).hasLease) return;
1447
+ throw new QueueConnectionError("Session queue owner is running but not accepting set_mode requests", {
1448
+ detailCode: "QUEUE_NOT_ACCEPTING_REQUESTS",
1449
+ origin: "queue",
1450
+ retryable: true
1451
+ });
1452
+ }
1453
+ async function trySetModelOnRunningOwner(sessionId, modelId, timeoutMs, verbose) {
1454
+ const owner = await readQueueOwnerRecord(sessionId);
1455
+ if (!owner) return;
1456
+ if (await submitSetModelToQueueOwner(owner, modelId, timeoutMs)) {
1457
+ if (verbose) process.stderr.write(`[acpx] requested session/set_model on owner pid ${owner.pid} for session ${sessionId}\n`);
1458
+ return true;
1459
+ }
1460
+ if (!(await probeQueueOwnerHealth(sessionId)).hasLease) return;
1461
+ throw new QueueConnectionError("Session queue owner is running but not accepting set_model requests", {
1462
+ detailCode: "QUEUE_NOT_ACCEPTING_REQUESTS",
1463
+ origin: "queue",
1464
+ retryable: true
1465
+ });
1466
+ }
1467
+ async function trySetConfigOptionOnRunningOwner(sessionId, configId, value, timeoutMs, verbose) {
1468
+ const owner = await readQueueOwnerRecord(sessionId);
1469
+ if (!owner) return;
1470
+ const response = await submitSetConfigOptionToQueueOwner(owner, configId, value, timeoutMs);
1471
+ if (response) {
1472
+ if (verbose) process.stderr.write(`[acpx] requested session/set_config_option on owner pid ${owner.pid} for session ${sessionId}\n`);
1473
+ return response;
1474
+ }
1475
+ if (!(await probeQueueOwnerHealth(sessionId)).hasLease) return;
1476
+ throw new QueueConnectionError("Session queue owner is running but not accepting set_config_option requests", {
1477
+ detailCode: "QUEUE_NOT_ACCEPTING_REQUESTS",
1478
+ origin: "queue",
1479
+ retryable: true
1480
+ });
1481
+ }
1482
+ //#endregion
1483
+ //#region src/cli/session/prompt-runner.ts
1484
+ function buildDirectConnectedSessionOptions(options, run) {
1485
+ return {
1486
+ sessionRecordId: options.sessionRecordId,
1487
+ loadRecord: resolveSessionRecord,
1488
+ saveRecord: writeSessionRecord,
1489
+ mcpServers: options.mcpServers,
1490
+ nonInteractivePermissions: options.nonInteractivePermissions,
1491
+ authCredentials: options.authCredentials,
1492
+ authPolicy: options.authPolicy,
1493
+ terminal: options.terminal,
1494
+ timeoutMs: options.timeoutMs,
1495
+ verbose: options.verbose,
1496
+ onClientAvailable: (controller) => {
1497
+ options.onClientAvailable?.(controller);
1498
+ },
1499
+ onClientClosed: options.onClientClosed,
1500
+ run
1501
+ };
1502
+ }
1503
+ function toSessionMutationResult(result) {
1504
+ return {
1505
+ record: result.record,
1506
+ resumed: result.resumed,
1507
+ loadError: result.loadError
1508
+ };
1509
+ }
1510
+ async function runSessionSetModeDirect(options) {
1511
+ return toSessionMutationResult(await withConnectedSession(buildDirectConnectedSessionOptions(options, async ({ client, sessionId, record }) => {
1512
+ await withTimeout(client.setSessionMode(sessionId, options.modeId), options.timeoutMs);
1513
+ setDesiredModeId(record, options.modeId);
1514
+ })));
1515
+ }
1516
+ async function runSessionSetModelDirect(options) {
1517
+ return toSessionMutationResult(await withConnectedSession(buildDirectConnectedSessionOptions(options, async ({ client, sessionId, record }) => {
1518
+ await withTimeout(client.setSessionModel(sessionId, options.modelId), options.timeoutMs);
1519
+ setDesiredModelId(record, options.modelId);
1520
+ setCurrentModelId(record, options.modelId);
1521
+ })));
1522
+ }
1523
+ async function runSessionSetConfigOptionDirect(options) {
1524
+ const result = await withConnectedSession(buildDirectConnectedSessionOptions(options, async ({ client, sessionId, record }) => {
1525
+ const response = await withTimeout(client.setSessionConfigOption(sessionId, options.configId, options.value), options.timeoutMs);
1526
+ if (options.configId === "mode") setDesiredModeId(record, options.value);
1527
+ else setDesiredConfigOption(record, options.configId, options.value);
1528
+ return response;
1529
+ }));
1530
+ return {
1531
+ record: result.record,
1532
+ response: result.value,
1533
+ resumed: result.resumed,
1534
+ loadError: result.loadError
1535
+ };
1536
+ }
1537
+ //#endregion
1538
+ //#region src/cli/session/session-control.ts
1539
+ async function cancelSessionPrompt(options) {
1540
+ const cancelled = await tryCancelOnRunningOwner(options);
1541
+ return {
1542
+ sessionId: options.sessionId,
1543
+ cancelled: cancelled === true
1544
+ };
1545
+ }
1546
+ async function setSessionMode(options) {
1547
+ if (await trySetModeOnRunningOwner(options.sessionId, options.modeId, options.timeoutMs, options.verbose)) {
1548
+ const record = await resolveSessionRecord(options.sessionId);
1549
+ setDesiredModeId(record, options.modeId);
1550
+ await writeSessionRecord(record);
1551
+ return {
1552
+ record,
1553
+ resumed: false
1554
+ };
1555
+ }
1556
+ return await runSessionSetModeDirect({
1557
+ sessionRecordId: options.sessionId,
1558
+ modeId: options.modeId,
1559
+ mcpServers: options.mcpServers,
1560
+ nonInteractivePermissions: options.nonInteractivePermissions,
1561
+ authCredentials: options.authCredentials,
1562
+ authPolicy: options.authPolicy,
1563
+ terminal: options.terminal,
1564
+ timeoutMs: options.timeoutMs,
1565
+ verbose: options.verbose
1566
+ });
1567
+ }
1568
+ async function setSessionModel(options) {
1569
+ if (await trySetModelOnRunningOwner(options.sessionId, options.modelId, options.timeoutMs, options.verbose)) {
1570
+ const record = await resolveSessionRecord(options.sessionId);
1571
+ setDesiredModelId(record, options.modelId);
1572
+ setCurrentModelId(record, options.modelId);
1573
+ await writeSessionRecord(record);
1574
+ return {
1575
+ record,
1576
+ resumed: false
1577
+ };
1578
+ }
1579
+ return await runSessionSetModelDirect({
1580
+ sessionRecordId: options.sessionId,
1581
+ modelId: options.modelId,
1582
+ mcpServers: options.mcpServers,
1583
+ nonInteractivePermissions: options.nonInteractivePermissions,
1584
+ authCredentials: options.authCredentials,
1585
+ authPolicy: options.authPolicy,
1586
+ terminal: options.terminal,
1587
+ timeoutMs: options.timeoutMs,
1588
+ verbose: options.verbose
1589
+ });
1590
+ }
1591
+ async function setSessionConfigOption(options) {
1592
+ const ownerResponse = await trySetConfigOptionOnRunningOwner(options.sessionId, options.configId, options.value, options.timeoutMs, options.verbose);
1593
+ if (ownerResponse) {
1594
+ const record = await resolveSessionRecord(options.sessionId);
1595
+ if (options.configId === "mode") setDesiredModeId(record, options.value);
1596
+ else setDesiredConfigOption(record, options.configId, options.value);
1597
+ await writeSessionRecord(record);
1598
+ return {
1599
+ record,
1600
+ response: ownerResponse,
1601
+ resumed: false
1602
+ };
1603
+ }
1604
+ return await runSessionSetConfigOptionDirect({
1605
+ sessionRecordId: options.sessionId,
1606
+ configId: options.configId,
1607
+ value: options.value,
1608
+ mcpServers: options.mcpServers,
1609
+ nonInteractivePermissions: options.nonInteractivePermissions,
1610
+ authCredentials: options.authCredentials,
1611
+ authPolicy: options.authPolicy,
1612
+ terminal: options.terminal,
1613
+ timeoutMs: options.timeoutMs,
1614
+ verbose: options.verbose
1615
+ });
1616
+ }
1617
+ function firstAgentCommandToken(command) {
1618
+ const trimmed = command.trim();
1619
+ if (!trimmed) return;
1620
+ const token = trimmed.split(/\s+/, 1)[0];
1621
+ return token.length > 0 ? token : void 0;
1622
+ }
1623
+ async function isLikelyMatchingProcess(pid, agentCommand) {
1624
+ const expectedToken = firstAgentCommandToken(agentCommand);
1625
+ if (!expectedToken) return false;
1626
+ const procCmdline = `/proc/${pid}/cmdline`;
1627
+ try {
1628
+ const argv = (await fs$1.readFile(procCmdline, "utf8")).split("\0").map((entry) => entry.trim()).filter((entry) => entry.length > 0);
1629
+ if (argv.length === 0) return false;
1630
+ const executableBase = path.basename(argv[0]);
1631
+ const expectedBase = path.basename(expectedToken);
1632
+ return executableBase === expectedBase || argv.some((entry) => path.basename(entry) === expectedBase);
1633
+ } catch {
1634
+ return true;
1635
+ }
1636
+ }
1637
+ async function closeSession(sessionId) {
1638
+ const record = await resolveSessionRecord(sessionId);
1639
+ await tryCloseSessionOnRunningOwner({ sessionId: record.acpxRecordId }).catch(() => {});
1640
+ await terminateQueueOwnerForSession(record.acpxRecordId);
1641
+ if (record.pid != null && isProcessAlive(record.pid) && await isLikelyMatchingProcess(record.pid, record.agentCommand)) await terminateProcess(record.pid);
1642
+ record.pid = void 0;
1643
+ record.closed = true;
1644
+ record.closedAt = isoNow();
1645
+ await writeSessionRecord(record);
1646
+ return record;
1647
+ }
1648
+ //#endregion
1649
+ //#region src/cli/session/session-management.ts
1650
+ async function createSessionRecordWithClient(client, options) {
1651
+ const cwd = absolutePath(options.cwd);
1652
+ await withTimeout(client.start(), options.timeoutMs);
1653
+ let sessionId;
1654
+ let agentSessionId;
1655
+ let sessionResult;
1656
+ let sessionModels;
1657
+ let requestedModelApplied = false;
1658
+ if (options.resumeSessionId) {
1659
+ const resumed = await resumeSessionRecordWithClient(client, options, cwd);
1660
+ sessionId = resumed.sessionId;
1661
+ agentSessionId = resumed.agentSessionId;
1662
+ sessionResult = resumed.sessionResult;
1663
+ sessionModels = resumed.sessionModels;
1664
+ requestedModelApplied = resumed.requestedModelApplied;
1665
+ } else {
1666
+ const createdSession = await withTimeout(client.createSession(cwd), options.timeoutMs);
1667
+ sessionId = createdSession.sessionId;
1668
+ agentSessionId = normalizeRuntimeSessionId(createdSession.agentSessionId);
1669
+ sessionResult = createdSession;
1670
+ sessionModels = createdSession.models;
1671
+ requestedModelApplied = await applyRequestedModelIfAdvertised({
1672
+ client,
1673
+ sessionId,
1674
+ requestedModel: options.sessionOptions?.model,
1675
+ models: sessionModels,
1676
+ agentCommand: options.agentCommand,
1677
+ timeoutMs: options.timeoutMs
1678
+ });
1679
+ }
1680
+ const lifecycle = client.getAgentLifecycleSnapshot();
1681
+ const now = isoNow();
1682
+ const record = {
1683
+ schema: "acpx.session.v1",
1684
+ acpxRecordId: sessionId,
1685
+ acpSessionId: sessionId,
1686
+ agentSessionId,
1687
+ agentCommand: options.agentCommand,
1688
+ cwd,
1689
+ name: normalizeName(options.name),
1690
+ createdAt: now,
1691
+ lastUsedAt: now,
1692
+ lastSeq: 0,
1693
+ lastRequestId: void 0,
1694
+ eventLog: defaultSessionEventLog(sessionId),
1695
+ closed: false,
1696
+ closedAt: void 0,
1697
+ pid: lifecycle.running ? lifecycle.pid : void 0,
1698
+ agentStartedAt: lifecycle.startedAt,
1699
+ protocolVersion: client.initializeResult?.protocolVersion,
1700
+ agentCapabilities: client.initializeResult?.agentCapabilities,
1701
+ ...createSessionConversation(now),
1702
+ acpx: {}
1703
+ };
1704
+ persistSessionOptions(record, options.sessionOptions);
1705
+ applyConfigOptionsToRecord(record, sessionResult);
1706
+ syncAdvertisedModelState(record, sessionModels);
1707
+ if (requestedModelApplied) setCurrentModelId(record, options.sessionOptions?.model);
1708
+ await writeSessionRecord(record);
1709
+ return record;
1710
+ }
1711
+ async function resumeSessionRecordWithClient(client, options, cwd) {
1712
+ if (!options.resumeSessionId) throw new Error("resumeSessionId is required");
1713
+ const resumeMethod = client.supportsResumeSession() ? "session/resume" : client.supportsLoadSession() ? "session/load" : void 0;
1714
+ if (!resumeMethod) throw new Error(`Agent command "${options.agentCommand}" does not support session/resume or session/load; cannot resume session ${options.resumeSessionId}`);
1715
+ try {
1716
+ const resumedSession = await withTimeout(resumeMethod === "session/resume" ? client.resumeSession(options.resumeSessionId, cwd) : client.loadSession(options.resumeSessionId, cwd), options.timeoutMs);
1717
+ const sessionModels = resumedSession.models;
1718
+ return {
1719
+ sessionId: options.resumeSessionId,
1720
+ agentSessionId: normalizeRuntimeSessionId(resumedSession.agentSessionId),
1721
+ sessionResult: resumedSession,
1722
+ sessionModels,
1723
+ requestedModelApplied: await applyRequestedModelIfAdvertised({
1724
+ client,
1725
+ sessionId: options.resumeSessionId,
1726
+ requestedModel: options.sessionOptions?.model,
1727
+ models: sessionModels,
1728
+ agentCommand: options.agentCommand,
1729
+ timeoutMs: options.timeoutMs
1730
+ })
1731
+ };
1732
+ } catch (error) {
1733
+ throw new Error(`Failed to resume ACP session ${options.resumeSessionId}: ${formatErrorMessage(error)}`, { cause: error });
1734
+ }
1735
+ }
1736
+ async function createSessionWithClient(options) {
1737
+ const client = new AcpClient({
1738
+ agentCommand: options.agentCommand,
1739
+ cwd: absolutePath(options.cwd),
1740
+ mcpServers: options.mcpServers,
1741
+ permissionMode: options.permissionMode,
1742
+ nonInteractivePermissions: options.nonInteractivePermissions,
1743
+ permissionPolicy: options.permissionPolicy,
1744
+ authCredentials: options.authCredentials,
1745
+ authPolicy: options.authPolicy,
1746
+ terminal: options.terminal,
1747
+ verbose: options.verbose,
1748
+ sessionOptions: options.sessionOptions
1749
+ });
1750
+ try {
1751
+ return {
1752
+ record: await withInterrupt(async () => await createSessionRecordWithClient(client, options), async () => {
1753
+ await client.close();
1754
+ }),
1755
+ client
1756
+ };
1757
+ } catch (error) {
1758
+ await client.close();
1759
+ throw error;
1760
+ }
1761
+ }
1762
+ async function createSession(options) {
1763
+ const { record, client } = await createSessionWithClient(options);
1764
+ try {
1765
+ return record;
1766
+ } finally {
1767
+ await client.close();
1768
+ applyLifecycleSnapshotToRecord(record, client.getAgentLifecycleSnapshot());
1769
+ await writeSessionRecord(record);
1770
+ }
1771
+ }
1772
+ async function listAgentSessions(options) {
1773
+ const client = new AcpClient({
1774
+ agentCommand: options.agentCommand,
1775
+ cwd: absolutePath(options.cwd),
1776
+ mcpServers: options.mcpServers,
1777
+ permissionMode: options.permissionMode,
1778
+ nonInteractivePermissions: options.nonInteractivePermissions,
1779
+ permissionPolicy: options.permissionPolicy,
1780
+ authCredentials: options.authCredentials,
1781
+ authPolicy: options.authPolicy,
1782
+ terminal: options.terminal,
1783
+ verbose: options.verbose
1784
+ });
1785
+ try {
1786
+ return await withInterrupt(async () => {
1787
+ await withTimeout(client.start(), options.timeoutMs);
1788
+ if (!client.supportsListSessions()) return;
1789
+ const cwd = options.filterCwd ? absolutePath(options.filterCwd) : void 0;
1790
+ const response = await withTimeout(client.listSessions({
1791
+ ...cwd ? { cwd } : {},
1792
+ ...options.cursor ? { cursor: options.cursor } : {}
1793
+ }), options.timeoutMs);
1794
+ return {
1795
+ _meta: response._meta,
1796
+ source: "agent",
1797
+ sessions: response.sessions,
1798
+ cursor: options.cursor,
1799
+ cwd,
1800
+ nextCursor: response.nextCursor
1801
+ };
1802
+ }, async () => {
1803
+ await client.close();
1804
+ });
1805
+ } finally {
1806
+ await client.close();
1807
+ }
1808
+ }
1809
+ async function ensureSession(options) {
1810
+ const cwd = absolutePath(options.cwd);
1811
+ const gitRoot = findGitRepositoryRoot(cwd);
1812
+ const walkBoundary = options.walkBoundary ?? gitRoot ?? cwd;
1813
+ const existing = await findSessionByDirectoryWalk({
1814
+ agentCommand: options.agentCommand,
1815
+ cwd,
1816
+ name: options.name,
1817
+ boundary: walkBoundary
1818
+ });
1819
+ if (existing) {
1820
+ const requestedModel = options.sessionOptions?.model;
1821
+ if (requestedModel) return {
1822
+ record: (await setSessionModel({
1823
+ sessionId: existing.acpxRecordId,
1824
+ modelId: requestedModel,
1825
+ mcpServers: options.mcpServers,
1826
+ nonInteractivePermissions: options.nonInteractivePermissions,
1827
+ authCredentials: options.authCredentials,
1828
+ authPolicy: options.authPolicy,
1829
+ terminal: options.terminal,
1830
+ timeoutMs: options.timeoutMs,
1831
+ verbose: options.verbose
1832
+ })).record,
1833
+ created: false
1834
+ };
1835
+ return {
1836
+ record: existing,
1837
+ created: false
1838
+ };
1839
+ }
1840
+ return {
1841
+ record: await createSession({
1842
+ agentCommand: options.agentCommand,
1843
+ cwd,
1844
+ name: options.name,
1845
+ resumeSessionId: options.resumeSessionId,
1846
+ mcpServers: options.mcpServers,
1847
+ permissionMode: options.permissionMode,
1848
+ nonInteractivePermissions: options.nonInteractivePermissions,
1849
+ permissionPolicy: options.permissionPolicy,
1850
+ authCredentials: options.authCredentials,
1851
+ authPolicy: options.authPolicy,
1852
+ terminal: options.terminal,
1853
+ timeoutMs: options.timeoutMs,
1854
+ verbose: options.verbose,
1855
+ sessionOptions: options.sessionOptions
1856
+ }),
1857
+ created: true
1858
+ };
1859
+ }
1860
+ //#endregion
1861
+ //#region src/perf-metrics-capture.ts
1862
+ const PERF_METRICS_FILE_ENV = "ACPX_PERF_METRICS_FILE";
1863
+ let installed = false;
1864
+ let flushed = false;
1865
+ let captureFilePath;
1866
+ let captureRole = "cli";
1867
+ let captureArgv = [];
1868
+ let captureSequence = 0;
1869
+ function shouldCapture() {
1870
+ return typeof captureFilePath === "string" && captureFilePath.trim().length > 0;
1871
+ }
1872
+ function buildPayload(reason) {
1873
+ return {
1874
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1875
+ pid: process.pid,
1876
+ ppid: process.ppid,
1877
+ role: captureRole,
1878
+ argv: captureArgv,
1879
+ cwd: process.cwd(),
1880
+ sequence: captureSequence,
1881
+ reason,
1882
+ metrics: getPerfMetricsSnapshot()
1883
+ };
1884
+ }
1885
+ function payloadHasMetrics(payload) {
1886
+ const metrics = payload.metrics;
1887
+ return [
1888
+ metrics.counters,
1889
+ metrics.gauges,
1890
+ metrics.timings
1891
+ ].some((entries) => Object.keys(entries ?? {}).length > 0);
1892
+ }
1893
+ function appendPerfMetricsPayload(payload) {
1894
+ fs.mkdirSync(path.dirname(captureFilePath), { recursive: true });
1895
+ fs.appendFileSync(captureFilePath, `${JSON.stringify(payload)}\n`, "utf8");
1896
+ captureSequence += 1;
1897
+ }
1898
+ function writePerfMetricsCapture(reason, resetAfterWrite) {
1899
+ if (!shouldCapture()) return false;
1900
+ const payload = buildPayload(reason);
1901
+ if (!payloadHasMetrics(payload)) return false;
1902
+ try {
1903
+ appendPerfMetricsPayload(payload);
1904
+ if (resetAfterWrite) resetPerfMetrics();
1905
+ return true;
1906
+ } catch {
1907
+ return false;
1908
+ }
1909
+ }
1910
+ function checkpointPerfMetricsCapture() {
1911
+ flushed = false;
1912
+ writePerfMetricsCapture("checkpoint", true);
1913
+ }
1914
+ function flushPerfMetricsCapture(reason = "exit") {
1915
+ if (flushed || !shouldCapture()) return;
1916
+ flushed = true;
1917
+ writePerfMetricsCapture(reason, false);
1918
+ }
1919
+ function installPerfMetricsCapture(options = {}) {
1920
+ captureFilePath = options.filePath ?? process.env[PERF_METRICS_FILE_ENV];
1921
+ if (!shouldCapture()) return;
1922
+ resetPerfMetrics();
1923
+ captureRole = options.role ?? captureRole;
1924
+ captureArgv = options.argv ?? [];
1925
+ captureSequence = 0;
1926
+ flushed = false;
1927
+ if (installed) return;
1928
+ installed = true;
1929
+ process.once("exit", () => {
1930
+ flushPerfMetricsCapture("exit");
1931
+ });
1932
+ for (const signal of ["SIGINT", "SIGTERM"]) {
1933
+ const handler = () => {
1934
+ flushPerfMetricsCapture("signal");
1935
+ process.removeListener(signal, handler);
1936
+ process.kill(process.pid, signal);
1937
+ };
1938
+ process.once(signal, handler);
1939
+ }
1940
+ }
1941
+ //#endregion
1942
+ //#region src/cli/queue/owner-turn-controller.ts
1943
+ var QueueOwnerTurnController = class {
1944
+ options;
1945
+ state = "idle";
1946
+ pendingCancel = false;
1947
+ activeController;
1948
+ constructor(options) {
1949
+ this.options = options;
1950
+ }
1951
+ get lifecycleState() {
1952
+ return this.state;
1953
+ }
1954
+ get hasPendingCancel() {
1955
+ return this.pendingCancel;
1956
+ }
1957
+ beginTurn() {
1958
+ this.state = "starting";
1959
+ this.pendingCancel = false;
1960
+ }
1961
+ markPromptActive() {
1962
+ if (this.state === "starting" || this.state === "active") this.state = "active";
1963
+ }
1964
+ endTurn() {
1965
+ this.state = "idle";
1966
+ this.pendingCancel = false;
1967
+ }
1968
+ beginClosing() {
1969
+ this.state = "closing";
1970
+ this.pendingCancel = false;
1971
+ this.activeController = void 0;
1972
+ }
1973
+ setActiveController(controller) {
1974
+ this.activeController = controller;
1975
+ }
1976
+ clearActiveController() {
1977
+ this.activeController = void 0;
1978
+ }
1979
+ assertCanHandleControlRequest() {
1980
+ if (this.state === "closing") throw new QueueConnectionError("Queue owner is closing", {
1981
+ detailCode: "QUEUE_OWNER_SHUTTING_DOWN",
1982
+ origin: "queue",
1983
+ retryable: true
1984
+ });
1985
+ }
1986
+ async requestCancel() {
1987
+ const activeController = this.activeController;
1988
+ if (activeController?.hasActivePrompt()) {
1989
+ const cancelled = await activeController.requestCancelActivePrompt();
1990
+ if (cancelled) this.pendingCancel = false;
1991
+ return cancelled;
1992
+ }
1993
+ if (this.state === "starting" || this.state === "active") {
1994
+ this.pendingCancel = true;
1995
+ return true;
1996
+ }
1997
+ return false;
1998
+ }
1999
+ async applyPendingCancel() {
2000
+ const activeController = this.activeController;
2001
+ if (!this.pendingCancel || !activeController || !activeController.hasActivePrompt()) return false;
2002
+ const cancelled = await activeController.requestCancelActivePrompt();
2003
+ if (cancelled) this.pendingCancel = false;
2004
+ return cancelled;
2005
+ }
2006
+ async setSessionMode(modeId, timeoutMs) {
2007
+ this.assertCanHandleControlRequest();
2008
+ const activeController = this.activeController;
2009
+ if (activeController) {
2010
+ await this.options.withTimeout(async () => await activeController.setSessionMode(modeId), timeoutMs);
2011
+ return;
2012
+ }
2013
+ await this.options.setSessionModeFallback(modeId, timeoutMs);
2014
+ }
2015
+ async setSessionModel(modelId, timeoutMs) {
2016
+ this.assertCanHandleControlRequest();
2017
+ const activeController = this.activeController;
2018
+ if (activeController) {
2019
+ await this.options.withTimeout(async () => await activeController.setSessionModel(modelId), timeoutMs);
2020
+ return;
2021
+ }
2022
+ await this.options.setSessionModelFallback(modelId, timeoutMs);
2023
+ }
2024
+ async setSessionConfigOption(configId, value, timeoutMs) {
2025
+ this.assertCanHandleControlRequest();
2026
+ const activeController = this.activeController;
2027
+ if (activeController) return await this.options.withTimeout(async () => await activeController.setSessionConfigOption(configId, value), timeoutMs);
2028
+ return await this.options.setSessionConfigOptionFallback(configId, value, timeoutMs);
2029
+ }
2030
+ };
2031
+ //#endregion
2032
+ //#region src/cli/session/queue-owner-process.ts
2033
+ function isNonEmptyStringArray(value) {
2034
+ return Array.isArray(value) && value.length > 0 && value.every((entry) => typeof entry === "string" && entry.length > 0);
2035
+ }
2036
+ const NODE_TEST_FLAGS = new Set([
2037
+ "--experimental-test-coverage",
2038
+ "--test",
2039
+ "--test-name-pattern",
2040
+ "--test-reporter",
2041
+ "--test-reporter-destination"
2042
+ ]);
2043
+ const NODE_TEST_FLAGS_WITH_VALUE = new Set([
2044
+ "--test-name-pattern",
2045
+ "--test-reporter",
2046
+ "--test-reporter-destination"
2047
+ ]);
2048
+ const INSPECTOR_FLAGS_WITH_VALUE = new Set([
2049
+ "--inspect",
2050
+ "--inspect-brk",
2051
+ "--inspect-port",
2052
+ "--inspect-publish-uid",
2053
+ "--debug-port"
2054
+ ]);
2055
+ const INSPECTOR_FLAG_PREFIXES = [
2056
+ "--inspect=",
2057
+ "--inspect-brk=",
2058
+ "--inspect-port=",
2059
+ "--inspect-publish-uid=",
2060
+ "--debug-port="
2061
+ ];
2062
+ function classifyExecArgv(value) {
2063
+ if (value === void 0) return "drop";
2064
+ if (NODE_TEST_FLAGS_WITH_VALUE.has(value) || INSPECTOR_FLAGS_WITH_VALUE.has(value)) return "drop-with-value";
2065
+ return dropSingleExecArgv(value) ? "drop" : "keep";
2066
+ }
2067
+ function dropSingleExecArgv(value) {
2068
+ return NODE_TEST_FLAGS.has(value) || value.startsWith("--test-") || INSPECTOR_FLAG_PREFIXES.some((prefix) => value.startsWith(prefix));
2069
+ }
2070
+ function sanitizeQueueOwnerExecArgv(execArgv = process.execArgv) {
2071
+ const sanitized = [];
2072
+ for (let index = 0; index < execArgv.length; index += 1) {
2073
+ const value = execArgv[index] ?? "";
2074
+ const decision = classifyExecArgv(value);
2075
+ if (decision === "drop") continue;
2076
+ if (decision === "drop-with-value") {
2077
+ index += 1;
2078
+ continue;
2079
+ }
2080
+ sanitized.push(value);
2081
+ }
2082
+ return sanitized;
2083
+ }
2084
+ function buildQueueOwnerArgOverride(entryPath, execArgv = process.execArgv) {
2085
+ const sanitized = sanitizeQueueOwnerExecArgv(execArgv);
2086
+ if (sanitized.length === 0) return null;
2087
+ return JSON.stringify([
2088
+ ...sanitized,
2089
+ entryPath,
2090
+ "__queue-owner"
2091
+ ]);
2092
+ }
2093
+ function resolveQueueOwnerSpawnArgs(argv = process.argv) {
2094
+ const override = process.env.ACPX_QUEUE_OWNER_ARGS;
2095
+ if (override) {
2096
+ const parsed = JSON.parse(override);
2097
+ if (isNonEmptyStringArray(parsed)) return [...parsed];
2098
+ throw new Error("acpx self-spawn failed: invalid ACPX_QUEUE_OWNER_ARGS");
2099
+ }
2100
+ const entry = argv[1];
2101
+ if (!entry || entry.trim().length === 0) throw new Error("acpx self-spawn failed: missing CLI entry path");
2102
+ return [realpathSync(entry), "__queue-owner"];
2103
+ }
2104
+ function queueOwnerRuntimeOptionsFromSend(options) {
2105
+ return {
2106
+ sessionId: options.sessionId,
2107
+ mcpServers: options.mcpServers,
2108
+ permissionMode: options.permissionMode,
2109
+ nonInteractivePermissions: options.nonInteractivePermissions,
2110
+ authCredentials: options.authCredentials,
2111
+ authPolicy: options.authPolicy,
2112
+ terminal: options.terminal,
2113
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2114
+ verbose: options.verbose,
2115
+ ttlMs: options.ttlMs,
2116
+ maxQueueDepth: options.maxQueueDepth,
2117
+ promptRetries: options.promptRetries,
2118
+ sessionOptions: options.sessionOptions
2119
+ };
2120
+ }
2121
+ function buildQueueOwnerSpawnOptions(payload) {
2122
+ return {
2123
+ detached: true,
2124
+ stdio: "ignore",
2125
+ env: {
2126
+ ...process.env,
2127
+ ACPX_QUEUE_OWNER_PAYLOAD: payload
2128
+ },
2129
+ windowsHide: true
2130
+ };
2131
+ }
2132
+ function spawnQueueOwnerProcess(options) {
2133
+ const payload = JSON.stringify(options);
2134
+ spawn(process.execPath, resolveQueueOwnerSpawnArgs(), buildQueueOwnerSpawnOptions(payload)).unref();
2135
+ }
2136
+ //#endregion
2137
+ //#region src/session/events.ts
2138
+ const LOCK_RETRY_MS = 15;
2139
+ const EVENT_LOCK_STALE_MS = 15e3;
2140
+ async function ensureSessionDir() {
2141
+ await fs$1.mkdir(sessionBaseDir(), { recursive: true });
2142
+ }
2143
+ async function pathExists(filePath) {
2144
+ try {
2145
+ await fs$1.access(filePath);
2146
+ return true;
2147
+ } catch {
2148
+ return false;
2149
+ }
2150
+ }
2151
+ async function statSize(filePath) {
2152
+ try {
2153
+ return (await fs$1.stat(filePath)).size;
2154
+ } catch {
2155
+ return 0;
2156
+ }
2157
+ }
2158
+ async function countExistingSegments(sessionId, maxSegments) {
2159
+ let count = 0;
2160
+ for (let segment = 1; segment <= maxSegments; segment += 1) if (await pathExists(sessionEventSegmentPath(sessionId, segment))) count += 1;
2161
+ if (await pathExists(sessionEventActivePath(sessionId))) count += 1;
2162
+ return count;
2163
+ }
2164
+ async function resolveInitialSegmentCount(record, maxSegments) {
2165
+ if (Number.isInteger(record.eventLog.segment_count) && record.eventLog.segment_count > 0) return record.eventLog.segment_count;
2166
+ return await countExistingSegments(record.acpxRecordId, maxSegments) || 1;
2167
+ }
2168
+ async function rotateSegments(sessionId, maxSegments) {
2169
+ const active = sessionEventActivePath(sessionId);
2170
+ const overflow = sessionEventSegmentPath(sessionId, maxSegments);
2171
+ await fs$1.unlink(overflow).catch((error) => {
2172
+ if (error.code !== "ENOENT") throw error;
2173
+ });
2174
+ for (let segment = maxSegments - 1; segment >= 1; segment -= 1) {
2175
+ const from = sessionEventSegmentPath(sessionId, segment);
2176
+ const to = sessionEventSegmentPath(sessionId, segment + 1);
2177
+ if (!await pathExists(from)) continue;
2178
+ await fs$1.rename(from, to);
2179
+ }
2180
+ if (await pathExists(active)) await fs$1.rename(active, sessionEventSegmentPath(sessionId, 1));
2181
+ }
2182
+ function parseEventLockPayload(raw) {
2183
+ try {
2184
+ const parsed = JSON.parse(raw);
2185
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
2186
+ const record = parsed;
2187
+ return {
2188
+ pid: typeof record.pid === "number" ? record.pid : void 0,
2189
+ created_at: typeof record.created_at === "string" ? record.created_at : void 0
2190
+ };
2191
+ } catch {
2192
+ return {};
2193
+ }
2194
+ }
2195
+ async function removeStaleEventLock(lockPath) {
2196
+ try {
2197
+ const parsed = parseEventLockPayload(await fs$1.readFile(lockPath, "utf8"));
2198
+ const createdAtMs = parsed.created_at ? Date.parse(parsed.created_at) : NaN;
2199
+ const lockAgeMs = Number.isFinite(createdAtMs) ? Date.now() - createdAtMs : Number.POSITIVE_INFINITY;
2200
+ if (isProcessAlive(parsed.pid) && lockAgeMs <= EVENT_LOCK_STALE_MS) return false;
2201
+ await fs$1.unlink(lockPath);
2202
+ incrementPerfCounter("session.events.stale_lock_recovered");
2203
+ return true;
2204
+ } catch (error) {
2205
+ if (error.code === "ENOENT") return true;
2206
+ return false;
2207
+ }
2208
+ }
2209
+ async function acquireEventsLock(sessionId) {
2210
+ await ensureSessionDir();
2211
+ const lockPath = sessionEventLockPath(sessionId);
2212
+ const payload = JSON.stringify({
2213
+ pid: process.pid,
2214
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
2215
+ }, null, 2);
2216
+ for (;;) try {
2217
+ await fs$1.writeFile(lockPath, `${payload}\n`, {
2218
+ encoding: "utf8",
2219
+ flag: "wx"
2220
+ });
2221
+ return { filePath: lockPath };
2222
+ } catch (error) {
2223
+ if (error.code !== "EEXIST") throw error;
2224
+ if (await removeStaleEventLock(lockPath)) continue;
2225
+ await new Promise((resolve) => {
2226
+ setTimeout(resolve, LOCK_RETRY_MS);
2227
+ });
2228
+ }
2229
+ }
2230
+ async function releaseEventsLock(lock) {
2231
+ await fs$1.unlink(lock.filePath).catch((error) => {
2232
+ if (error.code !== "ENOENT") throw error;
2233
+ });
2234
+ }
2235
+ var SessionEventWriter = class SessionEventWriter {
2236
+ record;
2237
+ lock;
2238
+ maxSegmentBytes;
2239
+ maxSegments;
2240
+ activePath;
2241
+ activeSizeBytes;
2242
+ segmentCount;
2243
+ closed = false;
2244
+ constructor(record, lock, options, state) {
2245
+ this.record = record;
2246
+ this.lock = lock;
2247
+ this.maxSegmentBytes = options.maxSegmentBytes;
2248
+ this.maxSegments = options.maxSegments;
2249
+ this.activePath = state.activePath;
2250
+ this.activeSizeBytes = state.activeSizeBytes;
2251
+ this.segmentCount = state.segmentCount;
2252
+ }
2253
+ static async open(record, options = {}) {
2254
+ const lock = await acquireEventsLock(record.acpxRecordId);
2255
+ const maxSegmentBytes = options.maxSegmentBytes ?? record.eventLog.max_segment_bytes ?? 67108864;
2256
+ const maxSegments = options.maxSegments ?? record.eventLog.max_segments ?? 5;
2257
+ const activePath = sessionEventActivePath(record.acpxRecordId);
2258
+ const activeSizeBytes = await statSize(activePath);
2259
+ const segmentCount = await resolveInitialSegmentCount(record, maxSegments);
2260
+ return new SessionEventWriter(record, lock, {
2261
+ maxSegmentBytes,
2262
+ maxSegments
2263
+ }, {
2264
+ activePath,
2265
+ activeSizeBytes,
2266
+ segmentCount
2267
+ });
2268
+ }
2269
+ getRecord() {
2270
+ return this.record;
2271
+ }
2272
+ async appendMessage(message, options = {}) {
2273
+ await this.appendMessages([message], options);
2274
+ }
2275
+ async appendMessages(messages, options = {}) {
2276
+ if (this.closed) throw new Error("SessionEventWriter is closed");
2277
+ if (messages.length === 0) return;
2278
+ await ensureSessionDir();
2279
+ await measurePerf("session.events.append_batch", async () => {
2280
+ for (const message of messages) {
2281
+ if (!isAcpJsonRpcMessage(message)) throw new Error("Attempted to persist invalid ACP JSON-RPC payload");
2282
+ const line = `${JSON.stringify(message)}\n`;
2283
+ const lineBytes = Buffer.byteLength(line);
2284
+ if (this.activeSizeBytes > 0 && this.activeSizeBytes + lineBytes > this.maxSegmentBytes) {
2285
+ await rotateSegments(this.record.acpxRecordId, this.maxSegments);
2286
+ this.activePath = sessionEventActivePath(this.record.acpxRecordId);
2287
+ this.activeSizeBytes = 0;
2288
+ this.segmentCount = Math.min(this.segmentCount + 1, this.maxSegments);
2289
+ incrementPerfCounter("session.events.rotate");
2290
+ }
2291
+ await fs$1.appendFile(this.activePath, line, "utf8");
2292
+ this.activeSizeBytes += lineBytes;
2293
+ this.record.lastSeq += 1;
2294
+ if (Object.hasOwn(message, "id")) {
2295
+ const id = message.id;
2296
+ if (typeof id === "string" || typeof id === "number") this.record.lastRequestId = String(id);
2297
+ }
2298
+ const writeTs = (/* @__PURE__ */ new Date()).toISOString();
2299
+ this.record.lastUsedAt = writeTs;
2300
+ this.record.eventLog = {
2301
+ active_path: this.activePath,
2302
+ segment_count: this.segmentCount,
2303
+ max_segment_bytes: this.maxSegmentBytes,
2304
+ max_segments: this.maxSegments,
2305
+ last_write_at: writeTs,
2306
+ last_write_error: null
2307
+ };
2308
+ }
2309
+ });
2310
+ if (options.checkpoint === true) await writeSessionRecord(this.record);
2311
+ }
2312
+ async checkpoint() {
2313
+ if (this.closed) throw new Error("SessionEventWriter is closed");
2314
+ await writeSessionRecord(this.record);
2315
+ }
2316
+ async close(options = {}) {
2317
+ if (this.closed) return;
2318
+ try {
2319
+ if (options.checkpoint !== false) await writeSessionRecord(this.record);
2320
+ } finally {
2321
+ this.closed = true;
2322
+ await releaseEventsLock(this.lock);
2323
+ }
2324
+ }
2325
+ };
2326
+ //#endregion
2327
+ //#region src/cli/session/runtime.ts
2328
+ const INTERRUPT_CANCEL_WAIT_MS = 2500;
2329
+ var QueueTaskOutputFormatter = class {
2330
+ requestId;
2331
+ send;
2332
+ constructor(task) {
2333
+ this.requestId = task.requestId;
2334
+ this.send = task.send;
2335
+ }
2336
+ setContext(_context) {}
2337
+ onAcpMessage(message) {
2338
+ this.send({
2339
+ type: "event",
2340
+ requestId: this.requestId,
2341
+ message
2342
+ });
2343
+ }
2344
+ onError(params) {
2345
+ this.send({
2346
+ type: "error",
2347
+ requestId: this.requestId,
2348
+ code: params.code,
2349
+ detailCode: params.detailCode,
2350
+ origin: params.origin,
2351
+ message: params.message,
2352
+ retryable: params.retryable,
2353
+ acp: params.acp
2354
+ });
2355
+ }
2356
+ onPermissionEscalation(event) {
2357
+ this.send({
2358
+ type: "permission_escalation",
2359
+ requestId: this.requestId,
2360
+ event
2361
+ });
2362
+ }
2363
+ flush() {}
2364
+ };
2365
+ const DISCARD_OUTPUT_FORMATTER = {
2366
+ setContext() {},
2367
+ onAcpMessage() {},
2368
+ onError() {},
2369
+ onPermissionEscalation() {},
2370
+ flush() {}
2371
+ };
2372
+ function toPromptResult(stopReason, sessionId, client) {
2373
+ return {
2374
+ stopReason,
2375
+ sessionId,
2376
+ permissionStats: client.getPermissionStats()
2377
+ };
2378
+ }
2379
+ function requestedModelId(value) {
2380
+ return typeof value === "string" ? value.trim() : "";
2381
+ }
2382
+ function advertisedModelsForRecord(record) {
2383
+ const availableModels = record.acpx?.available_models;
2384
+ if (!Array.isArray(availableModels)) return;
2385
+ return {
2386
+ currentModelId: record.acpx?.current_model_id ?? "",
2387
+ availableModels: availableModels.map((modelId) => ({
2388
+ modelId,
2389
+ name: modelId
2390
+ }))
2391
+ };
2392
+ }
2393
+ async function applyPromptModelIfAdvertised(params) {
2394
+ const requestedModel = requestedModelId(params.requestedModel);
2395
+ if (!requestedModel) return;
2396
+ const models = advertisedModelsForRecord(params.record);
2397
+ assertRequestedModelSupported({
2398
+ requestedModel,
2399
+ models,
2400
+ agentCommand: params.record.agentCommand,
2401
+ context: "apply"
2402
+ });
2403
+ if (!models) return;
2404
+ if (params.record.acpx?.current_model_id === requestedModel) {
2405
+ setDesiredModelId(params.record, requestedModel);
2406
+ return;
2407
+ }
2408
+ await withTimeout(params.client.setSessionModel(params.sessionId, requestedModel), params.timeoutMs);
2409
+ setDesiredModelId(params.record, requestedModel);
2410
+ setCurrentModelId(params.record, requestedModel);
2411
+ }
2412
+ function jsonRpcIdKey$1(value) {
2413
+ if (typeof value === "string") return `s:${value}`;
2414
+ if (typeof value === "number" && Number.isFinite(value)) return `n:${value}`;
2415
+ }
2416
+ function extractJsonRpcRequestInfo(message) {
2417
+ const candidate = message;
2418
+ if (typeof candidate.method !== "string") return;
2419
+ const idKey = jsonRpcIdKey$1(candidate.id);
2420
+ if (!idKey) return;
2421
+ return {
2422
+ idKey,
2423
+ method: candidate.method
2424
+ };
2425
+ }
2426
+ function extractJsonRpcResponseInfo(message) {
2427
+ const candidate = message;
2428
+ const idKey = jsonRpcIdKey$1(candidate.id);
2429
+ if (!idKey) return;
2430
+ const hasError = Object.hasOwn(candidate, "error");
2431
+ if (!hasError && !Object.hasOwn(candidate, "result")) return;
2432
+ return {
2433
+ idKey,
2434
+ hasError
2435
+ };
2436
+ }
2437
+ const SESSION_RECONNECT_METHODS = new Set(["session/load", "session/resume"]);
2438
+ function filterRecoverableLoadFallbackOutput(messages) {
2439
+ const requestMethodById = /* @__PURE__ */ new Map();
2440
+ const failedLoadRequestIds = /* @__PURE__ */ new Set();
2441
+ for (const message of messages) {
2442
+ const request = extractJsonRpcRequestInfo(message);
2443
+ if (request) {
2444
+ requestMethodById.set(request.idKey, request.method);
2445
+ continue;
2446
+ }
2447
+ const response = extractJsonRpcResponseInfo(message);
2448
+ if (!response || !response.hasError) continue;
2449
+ const requestMethod = requestMethodById.get(response.idKey);
2450
+ if (requestMethod && SESSION_RECONNECT_METHODS.has(requestMethod)) failedLoadRequestIds.add(response.idKey);
2451
+ }
2452
+ if (failedLoadRequestIds.size === 0) return messages;
2453
+ return messages.filter((message) => {
2454
+ const request = extractJsonRpcRequestInfo(message);
2455
+ if (request && SESSION_RECONNECT_METHODS.has(request.method) && failedLoadRequestIds.has(request.idKey)) return false;
2456
+ const response = extractJsonRpcResponseInfo(message);
2457
+ if (response && failedLoadRequestIds.has(response.idKey)) return false;
2458
+ return true;
2459
+ });
2460
+ }
2461
+ function emitPromptRetryNotice(params) {
2462
+ if (params.suppressSdkConsoleErrors) return;
2463
+ process.stderr.write(`[acpx] prompt failed (${formatErrorMessage(params.error)}), retrying in ${params.delayMs}ms (attempt ${params.attempt}/${params.maxRetries})\n`);
2464
+ }
2465
+ function emitConnectPerfMetric(startedAt, verbose) {
2466
+ if (!verbose) return;
2467
+ process.stderr.write(`[acpx] ${formatPerfMetric("prompt.connect_and_load", Date.now() - startedAt)}\n`);
2468
+ }
2469
+ function emitPromptPerfMetric(startedAt, verbose) {
2470
+ if (!verbose) return;
2471
+ process.stderr.write(`[acpx] ${formatPerfMetric("prompt.agent_turn", Date.now() - startedAt)}\n`);
2472
+ }
2473
+ function emitPromptHookError(error, verbose) {
2474
+ if (!verbose) return;
2475
+ process.stderr.write("[acpx] onPromptActive hook failed: " + formatErrorMessage(error) + "\n");
2476
+ }
2477
+ function emitPromptDisconnectNotice(snapshot, verbose) {
2478
+ const lastExit = snapshot.lastExit;
2479
+ if (!lastExit?.unexpectedDuringPrompt || !verbose) return;
2480
+ process.stderr.write("[acpx] agent disconnected during prompt (" + lastExit.reason + ", exit=" + lastExit.exitCode + ", signal=" + (lastExit.signal ?? "none") + ")\n");
2481
+ }
2482
+ function shouldRetryRuntimePrompt(error, attempt, maxRetries, snapshot, hasSideEffects) {
2483
+ if (!shouldRetryPromptAttempt(error, attempt, maxRetries, hasSideEffects)) return false;
2484
+ return snapshot.lastExit?.unexpectedDuringPrompt !== true;
2485
+ }
2486
+ function shouldRetryPromptAttempt(error, attempt, maxRetries, hasSideEffects) {
2487
+ return attempt < maxRetries && !hasSideEffects() && isRetryablePromptError(error);
2488
+ }
2489
+ async function waitBeforePromptRetry(error, attempt, maxRetries, suppressSdkConsoleErrors) {
2490
+ const delayMs = Math.min(1e3 * 2 ** attempt, 1e4);
2491
+ emitPromptRetryNotice({
2492
+ error,
2493
+ delayMs,
2494
+ attempt: attempt + 1,
2495
+ maxRetries,
2496
+ suppressSdkConsoleErrors
2497
+ });
2498
+ await waitMs(delayMs);
2499
+ }
2500
+ function buildQueuedTaskRunOptions(sessionRecordId, task, options, outputFormatter) {
2501
+ return {
2502
+ sessionRecordId,
2503
+ mcpServers: options.mcpServers,
2504
+ prompt: task.prompt ?? textPrompt(task.message),
2505
+ permissionMode: task.permissionMode,
2506
+ resumePolicy: task.resumePolicy,
2507
+ nonInteractivePermissions: task.nonInteractivePermissions ?? options.nonInteractivePermissions,
2508
+ permissionPolicy: task.permissionPolicy,
2509
+ authCredentials: options.authCredentials,
2510
+ authPolicy: options.authPolicy,
2511
+ outputFormatter,
2512
+ timeoutMs: task.timeoutMs,
2513
+ suppressSdkConsoleErrors: task.suppressSdkConsoleErrors ?? options.suppressSdkConsoleErrors,
2514
+ verbose: options.verbose,
2515
+ promptRetries: task.promptRetries ?? options.promptRetries ?? 0,
2516
+ sessionOptions: mergeSessionOptions(task.sessionOptions, options.sessionOptions),
2517
+ onClientAvailable: options.onClientAvailable,
2518
+ onClientClosed: options.onClientClosed,
2519
+ onPromptActive: options.onPromptActive,
2520
+ client: options.sharedClient
2521
+ };
2522
+ }
2523
+ function sendQueuedTaskResult(task, result) {
2524
+ if (!task.waitForCompletion) return;
2525
+ task.send({
2526
+ type: "result",
2527
+ requestId: task.requestId,
2528
+ result
2529
+ });
2530
+ }
2531
+ function sendQueuedTaskError(task, error) {
2532
+ if (!task.waitForCompletion) return;
2533
+ const normalizedError = normalizeOutputError(error, {
2534
+ origin: "runtime",
2535
+ detailCode: "QUEUE_RUNTIME_PROMPT_FAILED"
2536
+ });
2537
+ const alreadyEmitted = error.outputAlreadyEmitted === true;
2538
+ task.send({
2539
+ type: "error",
2540
+ requestId: task.requestId,
2541
+ code: normalizedError.code,
2542
+ detailCode: normalizedError.detailCode,
2543
+ origin: normalizedError.origin,
2544
+ message: normalizedError.message,
2545
+ retryable: normalizedError.retryable,
2546
+ acp: normalizedError.acp,
2547
+ outputAlreadyEmitted: alreadyEmitted
2548
+ });
2549
+ }
2550
+ async function runQueuedTask(sessionRecordId, task, options) {
2551
+ const outputFormatter = task.waitForCompletion ? new QueueTaskOutputFormatter(task) : DISCARD_OUTPUT_FORMATTER;
2552
+ try {
2553
+ sendQueuedTaskResult(task, await runSessionPrompt(buildQueuedTaskRunOptions(sessionRecordId, task, options, outputFormatter)));
2554
+ } catch (error) {
2555
+ sendQueuedTaskError(task, error);
2556
+ if (error instanceof InterruptedError) throw error;
2557
+ } finally {
2558
+ task.close();
2559
+ }
2560
+ }
2561
+ async function runSessionPrompt(options) {
2562
+ const stopTotalTimer = startPerfTimer("runtime.prompt.total");
2563
+ const output = options.outputFormatter;
2564
+ const record = await measurePerf("session.resolve_prompt_record", async () => {
2565
+ return await resolveSessionRecord(options.sessionRecordId);
2566
+ });
2567
+ const conversation = cloneSessionConversation(record);
2568
+ let acpxState = cloneSessionAcpxState(record.acpx);
2569
+ const promptStartedAt = isoNow();
2570
+ const promptMessageId = recordPromptSubmission(conversation, options.prompt, promptStartedAt);
2571
+ record.lastPromptAt = promptStartedAt;
2572
+ record.lastUsedAt = promptStartedAt;
2573
+ applyConversation(record, conversation);
2574
+ record.acpx = acpxState;
2575
+ await writeSessionRecord(record);
2576
+ output.setContext({ sessionId: record.acpxRecordId });
2577
+ const eventWriter = await measurePerf("session.events.open", async () => {
2578
+ return await SessionEventWriter.open(record);
2579
+ });
2580
+ const pendingMessages = [];
2581
+ const pendingConnectOutputMessages = [];
2582
+ const sessionOptions = mergeSessionOptions(options.sessionOptions, sessionOptionsFromRecord(record));
2583
+ let bufferingConnectOutput = true;
2584
+ let promptTurnActive = false;
2585
+ let promptTurnHadSideEffects = false;
2586
+ let sawAcpMessage = false;
2587
+ let eventWriterClosed = false;
2588
+ const closeEventWriter = async (checkpoint) => {
2589
+ if (eventWriterClosed) return;
2590
+ eventWriterClosed = true;
2591
+ await eventWriter.close({ checkpoint });
2592
+ };
2593
+ const flushPendingMessages = async (checkpoint = false) => {
2594
+ if (pendingMessages.length === 0) return;
2595
+ const batch = pendingMessages.splice(0);
2596
+ await measurePerf("session.events.flush_pending", async () => {
2597
+ await eventWriter.appendMessages(batch, { checkpoint });
2598
+ });
2599
+ };
2600
+ const preserveClosedState = async () => {
2601
+ const latest = await resolveSessionRecord(record.acpxRecordId).catch(() => void 0);
2602
+ if (!latest?.closed) return;
2603
+ record.closed = true;
2604
+ record.closedAt = latest.closedAt ?? record.closedAt ?? isoNow();
2605
+ record.pid = latest.pid;
2606
+ if (latest.acpx) record.acpx = {
2607
+ ...record.acpx,
2608
+ ...latest.acpx
2609
+ };
2610
+ };
2611
+ const liveCheckpoint = new LiveSessionCheckpoint({
2612
+ save: async () => {
2613
+ await flushPendingMessages(false);
2614
+ record.lastUsedAt = isoNow();
2615
+ applyConversation(record, conversation);
2616
+ record.acpx = acpxState;
2617
+ await preserveClosedState();
2618
+ await eventWriter.checkpoint();
2619
+ },
2620
+ onError: (error) => {
2621
+ if (options.verbose) process.stderr.write("[acpx] live session checkpoint failed: " + formatErrorMessage(error) + "\n");
2622
+ }
2623
+ });
2624
+ const ownClient = options.client == null;
2625
+ const client = options.client ?? new AcpClient({
2626
+ agentCommand: record.agentCommand,
2627
+ cwd: absolutePath(record.cwd),
2628
+ mcpServers: options.mcpServers,
2629
+ permissionMode: options.permissionMode,
2630
+ nonInteractivePermissions: options.nonInteractivePermissions,
2631
+ permissionPolicy: options.permissionPolicy,
2632
+ authCredentials: options.authCredentials,
2633
+ authPolicy: options.authPolicy,
2634
+ terminal: options.terminal,
2635
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2636
+ verbose: options.verbose,
2637
+ sessionOptions
2638
+ });
2639
+ client.updateRuntimeOptions({
2640
+ permissionMode: options.permissionMode,
2641
+ nonInteractivePermissions: options.nonInteractivePermissions,
2642
+ permissionPolicy: options.permissionPolicy,
2643
+ terminal: options.terminal,
2644
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2645
+ verbose: options.verbose
2646
+ });
2647
+ client.setEventHandlers({
2648
+ onAcpMessage: (direction, message) => {
2649
+ sawAcpMessage = true;
2650
+ pendingMessages.push(message);
2651
+ options.onAcpMessage?.(direction, message);
2652
+ },
2653
+ onAcpOutputMessage: (_direction, message) => {
2654
+ if (bufferingConnectOutput) {
2655
+ pendingConnectOutputMessages.push(message);
2656
+ return;
2657
+ }
2658
+ output.onAcpMessage(message);
2659
+ },
2660
+ onSessionUpdate: (notification) => {
2661
+ if (promptTurnActive) promptTurnHadSideEffects = true;
2662
+ acpxState = recordSessionUpdate(conversation, acpxState, notification);
2663
+ trimConversationForRuntime(conversation);
2664
+ liveCheckpoint.request();
2665
+ options.onSessionUpdate?.(notification);
2666
+ },
2667
+ onClientOperation: (operation) => {
2668
+ if (promptTurnActive) promptTurnHadSideEffects = true;
2669
+ acpxState = recordClientOperation(conversation, acpxState, operation);
2670
+ trimConversationForRuntime(conversation);
2671
+ liveCheckpoint.request();
2672
+ options.onClientOperation?.(operation);
2673
+ },
2674
+ onPermissionEscalation: (event) => {
2675
+ output.onPermissionEscalation(event);
2676
+ options.onPermissionEscalation?.(event);
2677
+ }
2678
+ });
2679
+ let activeSessionIdForControl = record.acpSessionId;
2680
+ let notifiedClientAvailable = false;
2681
+ const activeController = {
2682
+ hasActivePrompt: () => client.hasActivePrompt(),
2683
+ requestCancelActivePrompt: async () => await client.requestCancelActivePrompt(),
2684
+ setSessionMode: async (modeId) => {
2685
+ await client.setSessionMode(activeSessionIdForControl, modeId);
2686
+ },
2687
+ setSessionModel: async (modelId) => {
2688
+ await client.setSessionModel(activeSessionIdForControl, modelId);
2689
+ },
2690
+ setSessionConfigOption: async (configId, value) => {
2691
+ return await client.setSessionConfigOption(activeSessionIdForControl, configId, value);
2692
+ }
2693
+ };
2694
+ const flushConnectOutput = (loadError) => {
2695
+ bufferingConnectOutput = false;
2696
+ const messages = loadError == null ? pendingConnectOutputMessages : filterRecoverableLoadFallbackOutput(pendingConnectOutputMessages);
2697
+ for (const message of messages) output.onAcpMessage(message);
2698
+ pendingConnectOutputMessages.length = 0;
2699
+ };
2700
+ const connectForPrompt = async () => {
2701
+ const connectStartedAt = Date.now();
2702
+ try {
2703
+ const connected = await measurePerf("runtime.connect_and_load", async () => {
2704
+ return await connectAndLoadSession({
2705
+ client,
2706
+ record,
2707
+ resumePolicy: options.resumePolicy,
2708
+ timeoutMs: options.timeoutMs,
2709
+ verbose: options.verbose,
2710
+ activeController,
2711
+ onClientAvailable: (controller) => {
2712
+ options.onClientAvailable?.(controller);
2713
+ notifiedClientAvailable = true;
2714
+ },
2715
+ onConnectedRecord: (connectedRecord) => {
2716
+ connectedRecord.lastPromptAt = isoNow();
2717
+ },
2718
+ onSessionIdResolved: (sessionId) => {
2719
+ activeSessionIdForControl = sessionId;
2720
+ }
2721
+ });
2722
+ });
2723
+ flushConnectOutput(connected.loadError);
2724
+ emitConnectPerfMetric(connectStartedAt, options.verbose);
2725
+ return connected;
2726
+ } catch (error) {
2727
+ flushConnectOutput();
2728
+ throw error;
2729
+ }
2730
+ };
2731
+ const buildPromptStartedHook = (attempt) => {
2732
+ if (attempt !== 0 || !options.onPromptActive) return;
2733
+ return async () => {
2734
+ try {
2735
+ await options.onPromptActive?.();
2736
+ } catch (error) {
2737
+ emitPromptHookError(error, options.verbose);
2738
+ }
2739
+ };
2740
+ };
2741
+ const runPromptAttempt = async (sessionId, attempt) => {
2742
+ const promptStartedAt = Date.now();
2743
+ const response = await measurePerf("runtime.prompt.agent_turn", async () => {
2744
+ return await runPromptTurn({
2745
+ client,
2746
+ sessionId,
2747
+ prompt: options.prompt,
2748
+ timeoutMs: options.timeoutMs,
2749
+ conversation,
2750
+ promptMessageId,
2751
+ onPromptStarted: buildPromptStartedHook(attempt)
2752
+ });
2753
+ });
2754
+ emitPromptPerfMetric(promptStartedAt, options.verbose);
2755
+ return response;
2756
+ };
2757
+ const handlePromptFailure = async (error, attempt) => {
2758
+ const snapshot = client.getAgentLifecycleSnapshot();
2759
+ if (shouldRetryRuntimePrompt(error, attempt, options.promptRetries ?? 0, snapshot, () => promptTurnHadSideEffects)) {
2760
+ await waitBeforePromptRetry(error, attempt, options.promptRetries ?? 0, options.suppressSdkConsoleErrors);
2761
+ return promptTurnHadSideEffects ? await failRuntimePrompt(error, snapshot) : "retry";
2762
+ }
2763
+ return await failRuntimePrompt(error, snapshot);
2764
+ };
2765
+ const failRuntimePrompt = async (error, snapshot) => {
2766
+ promptTurnActive = false;
2767
+ applyLifecycleSnapshotToRecord(record, snapshot);
2768
+ emitPromptDisconnectNotice(snapshot, options.verbose);
2769
+ const normalizedError = normalizeOutputError(error, { origin: "runtime" });
2770
+ await flushPendingMessages(false).catch(() => {});
2771
+ output.flush();
2772
+ record.lastUsedAt = isoNow();
2773
+ applyConversation(record, conversation);
2774
+ record.acpx = acpxState;
2775
+ const propagated = error instanceof Error ? error : new Error(formatErrorMessage(error));
2776
+ propagated.outputAlreadyEmitted = sawAcpMessage;
2777
+ propagated.normalizedOutputError = normalizedError;
2778
+ throw propagated;
2779
+ };
2780
+ const runPromptWithRetries = async (sessionId) => {
2781
+ promptTurnActive = true;
2782
+ for (let attempt = 0;; attempt++) try {
2783
+ return await runPromptAttempt(sessionId, attempt);
2784
+ } catch (error) {
2785
+ if (await handlePromptFailure(error, attempt) === "retry") continue;
2786
+ }
2787
+ };
2788
+ const savePromptSuccess = async (response) => {
2789
+ await flushPendingMessages(false);
2790
+ output.flush();
2791
+ record.lastUsedAt = isoNow();
2792
+ record.closed = false;
2793
+ record.closedAt = void 0;
2794
+ record.protocolVersion = client.initializeResult?.protocolVersion;
2795
+ record.agentCapabilities = client.initializeResult?.agentCapabilities;
2796
+ applyConversation(record, conversation);
2797
+ record.acpx = acpxState;
2798
+ applyLifecycleSnapshotToRecord(record, client.getAgentLifecycleSnapshot());
2799
+ stopTotalTimer();
2800
+ return response;
2801
+ };
2802
+ try {
2803
+ return await withInterrupt(async () => {
2804
+ const { sessionId: activeSessionId, resumed, loadError } = await connectForPrompt();
2805
+ await applyPromptModelIfAdvertised({
2806
+ client,
2807
+ sessionId: activeSessionId,
2808
+ requestedModel: sessionOptions?.model,
2809
+ record,
2810
+ timeoutMs: options.timeoutMs
2811
+ });
2812
+ output.setContext({ sessionId: record.acpxRecordId });
2813
+ await liveCheckpoint.checkpoint();
2814
+ const response = await savePromptSuccess(await runPromptWithRetries(activeSessionId));
2815
+ promptTurnActive = false;
2816
+ return {
2817
+ ...toPromptResult(response.stopReason, record.acpxRecordId, client),
2818
+ record,
2819
+ resumed,
2820
+ loadError
2821
+ };
2822
+ }, async () => {
2823
+ await client.cancelActivePrompt(INTERRUPT_CANCEL_WAIT_MS);
2824
+ applyLifecycleSnapshotToRecord(record, client.getAgentLifecycleSnapshot());
2825
+ record.lastUsedAt = isoNow();
2826
+ applyConversation(record, conversation);
2827
+ record.acpx = acpxState;
2828
+ await flushPendingMessages(false).catch(() => {});
2829
+ if (ownClient) await client.close();
2830
+ });
2831
+ } finally {
2832
+ if (options.verbose) process.stderr.write(`[acpx] ${formatPerfMetric("prompt.total", stopTotalTimer())}\n`);
2833
+ else stopTotalTimer();
2834
+ if (notifiedClientAvailable) options.onClientClosed?.();
2835
+ client.clearEventHandlers();
2836
+ if (ownClient) await client.close();
2837
+ applyLifecycleSnapshotToRecord(record, client.getAgentLifecycleSnapshot());
2838
+ applyConversation(record, conversation);
2839
+ record.acpx = acpxState;
2840
+ await liveCheckpoint.flush().catch(() => {});
2841
+ await flushPendingMessages(false).catch(() => {});
2842
+ await preserveClosedState().catch(() => {});
2843
+ await closeEventWriter(true).catch(() => {});
2844
+ }
2845
+ }
2846
+ async function runOnce(options) {
2847
+ const output = options.outputFormatter;
2848
+ let promptTurnActive = false;
2849
+ let promptTurnHadSideEffects = false;
2850
+ const client = new AcpClient({
2851
+ agentCommand: options.agentCommand,
2852
+ cwd: absolutePath(options.cwd),
2853
+ mcpServers: options.mcpServers,
2854
+ permissionMode: options.permissionMode,
2855
+ nonInteractivePermissions: options.nonInteractivePermissions,
2856
+ permissionPolicy: options.permissionPolicy,
2857
+ authCredentials: options.authCredentials,
2858
+ authPolicy: options.authPolicy,
2859
+ terminal: options.terminal,
2860
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2861
+ verbose: options.verbose,
2862
+ onAcpMessage: options.onAcpMessage,
2863
+ onAcpOutputMessage: (_direction, message) => output.onAcpMessage(message),
2864
+ onSessionUpdate: (notification) => {
2865
+ if (promptTurnActive) promptTurnHadSideEffects = true;
2866
+ options.onSessionUpdate?.(notification);
2867
+ },
2868
+ onClientOperation: (operation) => {
2869
+ if (promptTurnActive) promptTurnHadSideEffects = true;
2870
+ options.onClientOperation?.(operation);
2871
+ },
2872
+ onPermissionEscalation: (event) => {
2873
+ output.onPermissionEscalation(event);
2874
+ options.onPermissionEscalation?.(event);
2875
+ },
2876
+ sessionOptions: options.sessionOptions
2877
+ });
2878
+ const runExecPromptAttempt = async (sessionId) => {
2879
+ return await measurePerf("runtime.exec.prompt", async () => {
2880
+ return await withTimeout(client.prompt(sessionId, options.prompt), options.timeoutMs);
2881
+ });
2882
+ };
2883
+ const runExecPromptWithRetries = async (sessionId) => {
2884
+ const maxRetries = options.promptRetries ?? 0;
2885
+ promptTurnActive = true;
2886
+ for (let attempt = 0;; attempt++) try {
2887
+ return await runExecPromptAttempt(sessionId);
2888
+ } catch (error) {
2889
+ if (shouldRetryPromptAttempt(error, attempt, maxRetries, () => promptTurnHadSideEffects)) {
2890
+ await waitBeforePromptRetry(error, attempt, maxRetries, options.suppressSdkConsoleErrors);
2891
+ if (!promptTurnHadSideEffects) continue;
2892
+ }
2893
+ promptTurnActive = false;
2894
+ throw error;
2895
+ }
2896
+ };
2897
+ try {
2898
+ return await withInterrupt(async () => {
2899
+ await measurePerf("runtime.exec.start", async () => {
2900
+ await withTimeout(client.start(), options.timeoutMs);
2901
+ });
2902
+ const createdSession = await measurePerf("runtime.exec.create_session", async () => {
2903
+ return await withTimeout(client.createSession(absolutePath(options.cwd)), options.timeoutMs);
2904
+ });
2905
+ const sessionId = createdSession.sessionId;
2906
+ await applyRequestedModelIfAdvertised({
2907
+ client,
2908
+ sessionId,
2909
+ requestedModel: options.sessionOptions?.model,
2910
+ models: createdSession.models,
2911
+ agentCommand: options.agentCommand,
2912
+ timeoutMs: options.timeoutMs
2913
+ });
2914
+ output.setContext({ sessionId });
2915
+ const response = await runExecPromptWithRetries(sessionId);
2916
+ promptTurnActive = false;
2917
+ output.flush();
2918
+ return toPromptResult(response.stopReason, sessionId, client);
2919
+ }, async () => {
2920
+ await client.cancelActivePrompt(INTERRUPT_CANCEL_WAIT_MS);
2921
+ await client.close();
2922
+ });
2923
+ } finally {
2924
+ await client.close();
2925
+ }
2926
+ }
2927
+ async function sendSessionDirect(options) {
2928
+ return await runSessionPrompt({
2929
+ sessionRecordId: options.sessionId,
2930
+ prompt: options.prompt,
2931
+ mcpServers: options.mcpServers,
2932
+ permissionMode: options.permissionMode,
2933
+ resumePolicy: options.resumePolicy,
2934
+ nonInteractivePermissions: options.nonInteractivePermissions,
2935
+ permissionPolicy: options.permissionPolicy,
2936
+ authCredentials: options.authCredentials,
2937
+ authPolicy: options.authPolicy,
2938
+ terminal: options.terminal,
2939
+ outputFormatter: options.outputFormatter,
2940
+ onAcpMessage: options.onAcpMessage,
2941
+ onSessionUpdate: options.onSessionUpdate,
2942
+ onClientOperation: options.onClientOperation,
2943
+ onPermissionEscalation: options.onPermissionEscalation,
2944
+ timeoutMs: options.timeoutMs,
2945
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2946
+ verbose: options.verbose,
2947
+ client: options.client
2948
+ });
2949
+ }
2950
+ //#endregion
2951
+ //#region src/cli/session/queue-owner-runtime.ts
2952
+ const QUEUE_OWNER_STARTUP_MAX_ATTEMPTS = 120;
2953
+ const QUEUE_OWNER_HEARTBEAT_INTERVAL_MS = 5e3;
2954
+ async function submitToRunningOwner(options, waitForCompletion) {
2955
+ return await trySubmitToRunningOwner({
2956
+ sessionId: options.sessionId,
2957
+ message: promptToDisplayText(options.prompt),
2958
+ prompt: options.prompt,
2959
+ permissionMode: options.permissionMode,
2960
+ nonInteractivePermissions: options.nonInteractivePermissions,
2961
+ permissionPolicy: options.permissionPolicy,
2962
+ outputFormatter: options.outputFormatter,
2963
+ errorEmissionPolicy: options.errorEmissionPolicy,
2964
+ timeoutMs: options.timeoutMs,
2965
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2966
+ promptRetries: options.promptRetries,
2967
+ waitForCompletion,
2968
+ verbose: options.verbose,
2969
+ sessionOptions: options.sessionOptions
2970
+ });
2971
+ }
2972
+ function createQueueOwnerSharedClient(options, sessionRecord) {
2973
+ return new AcpClient({
2974
+ agentCommand: sessionRecord.agentCommand,
2975
+ cwd: absolutePath(sessionRecord.cwd),
2976
+ mcpServers: options.mcpServers,
2977
+ permissionMode: options.permissionMode,
2978
+ nonInteractivePermissions: options.nonInteractivePermissions,
2979
+ authCredentials: options.authCredentials,
2980
+ authPolicy: options.authPolicy,
2981
+ terminal: options.terminal,
2982
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
2983
+ verbose: options.verbose,
2984
+ sessionOptions: mergeSessionOptions(options.sessionOptions, sessionOptionsFromRecord(sessionRecord))
2985
+ });
2986
+ }
2987
+ function createQueueOwnerTurnController(options) {
2988
+ return new QueueOwnerTurnController({
2989
+ withTimeout: async (run, timeoutMs) => await withTimeout(run(), timeoutMs),
2990
+ setSessionModeFallback: async (modeId, timeoutMs) => {
2991
+ await runSessionSetModeDirect({
2992
+ sessionRecordId: options.sessionId,
2993
+ modeId,
2994
+ mcpServers: options.mcpServers,
2995
+ nonInteractivePermissions: options.nonInteractivePermissions,
2996
+ authCredentials: options.authCredentials,
2997
+ authPolicy: options.authPolicy,
2998
+ terminal: options.terminal,
2999
+ timeoutMs,
3000
+ verbose: options.verbose
3001
+ });
3002
+ },
3003
+ setSessionModelFallback: async (modelId, timeoutMs) => {
3004
+ await runSessionSetModelDirect({
3005
+ sessionRecordId: options.sessionId,
3006
+ modelId,
3007
+ mcpServers: options.mcpServers,
3008
+ nonInteractivePermissions: options.nonInteractivePermissions,
3009
+ authCredentials: options.authCredentials,
3010
+ authPolicy: options.authPolicy,
3011
+ terminal: options.terminal,
3012
+ timeoutMs,
3013
+ verbose: options.verbose
3014
+ });
3015
+ },
3016
+ setSessionConfigOptionFallback: async (configId, value, timeoutMs) => {
3017
+ return (await runSessionSetConfigOptionDirect({
3018
+ sessionRecordId: options.sessionId,
3019
+ configId,
3020
+ value,
3021
+ mcpServers: options.mcpServers,
3022
+ nonInteractivePermissions: options.nonInteractivePermissions,
3023
+ authCredentials: options.authCredentials,
3024
+ authPolicy: options.authPolicy,
3025
+ terminal: options.terminal,
3026
+ timeoutMs,
3027
+ verbose: options.verbose
3028
+ })).response;
3029
+ }
3030
+ });
3031
+ }
3032
+ function logDeferredCancelFailure(error, verbose) {
3033
+ if (!verbose) return;
3034
+ process.stderr.write(`[acpx] failed to apply deferred cancel: ${formatErrorMessage(error)}\n`);
3035
+ }
3036
+ function logQueueOwnerReady(params) {
3037
+ if (!params.verbose) return;
3038
+ process.stderr.write(`[acpx] queue owner ready for session ${params.sessionId} (ttlMs=${params.ttlMs}, maxQueueDepth=${params.maxQueueDepth})\n`);
3039
+ }
3040
+ async function closeQueueOwnerRuntime(params) {
3041
+ if (params.heartbeatTimer) clearInterval(params.heartbeatTimer);
3042
+ params.turnController.beginClosing();
3043
+ await params.owner?.close();
3044
+ await params.sharedClient.close().catch(() => {});
3045
+ await writeQueueOwnerLifecycleSnapshot(params.sessionId, params.sharedClient);
3046
+ await releaseQueueOwnerLease(params.lease);
3047
+ if (params.verbose) process.stderr.write(`[acpx] queue owner stopped for session ${params.sessionId}\n`);
3048
+ }
3049
+ async function writeQueueOwnerLifecycleSnapshot(sessionId, sharedClient) {
3050
+ try {
3051
+ const record = await resolveSessionRecord(sessionId);
3052
+ applyLifecycleSnapshotToRecord(record, sharedClient.getAgentLifecycleSnapshot());
3053
+ await writeSessionRecord(record);
3054
+ } catch {}
3055
+ }
3056
+ async function runSessionQueueOwner(options) {
3057
+ const lease = await tryAcquireQueueOwnerLease(options.sessionId);
3058
+ if (!lease) return;
3059
+ const sessionRecord = await resolveSessionRecord(options.sessionId);
3060
+ let owner;
3061
+ let heartbeatTimer;
3062
+ const sharedClient = createQueueOwnerSharedClient(options, sessionRecord);
3063
+ const ttlMs = normalizeQueueOwnerTtlMs(options.ttlMs);
3064
+ const maxQueueDepth = Math.max(1, Math.round(options.maxQueueDepth ?? 16));
3065
+ const taskPollTimeoutMs = ttlMs === 0 ? void 0 : ttlMs;
3066
+ const initialTaskPollTimeoutMs = taskPollTimeoutMs == null ? void 0 : Math.max(taskPollTimeoutMs, 1e3);
3067
+ const turnController = createQueueOwnerTurnController(options);
3068
+ const applyPendingCancel = async () => {
3069
+ return await turnController.applyPendingCancel();
3070
+ };
3071
+ const scheduleApplyPendingCancel = () => {
3072
+ applyPendingCancel().catch((error) => {
3073
+ logDeferredCancelFailure(error, options.verbose);
3074
+ });
3075
+ };
3076
+ const setActiveController = (controller) => {
3077
+ turnController.setActiveController(controller);
3078
+ scheduleApplyPendingCancel();
3079
+ };
3080
+ const clearActiveController = () => {
3081
+ turnController.clearActiveController();
3082
+ };
3083
+ const closeActiveBackendSession = async (timeoutMs) => {
3084
+ const latestRecord = await resolveSessionRecord(options.sessionId);
3085
+ if (!sharedClient.supportsCloseSession()) return false;
3086
+ await withTimeout(sharedClient.closeSession(latestRecord.acpSessionId), timeoutMs);
3087
+ return true;
3088
+ };
3089
+ const runPromptTurn = async (run) => {
3090
+ turnController.beginTurn();
3091
+ try {
3092
+ return await run();
3093
+ } finally {
3094
+ turnController.endTurn();
3095
+ }
3096
+ };
3097
+ try {
3098
+ owner = await SessionQueueOwner.start(lease, {
3099
+ cancelPrompt: async () => {
3100
+ if (!await turnController.requestCancel()) return false;
3101
+ await applyPendingCancel();
3102
+ return true;
3103
+ },
3104
+ closeSession: async (timeoutMs) => await closeActiveBackendSession(timeoutMs),
3105
+ setSessionMode: async (modeId, timeoutMs) => {
3106
+ await turnController.setSessionMode(modeId, timeoutMs);
3107
+ },
3108
+ setSessionModel: async (modelId, timeoutMs) => {
3109
+ await turnController.setSessionModel(modelId, timeoutMs);
3110
+ },
3111
+ setSessionConfigOption: async (configId, value, timeoutMs) => {
3112
+ return await turnController.setSessionConfigOption(configId, value, timeoutMs);
3113
+ }
3114
+ }, {
3115
+ maxQueueDepth,
3116
+ onQueueDepthChanged: (queueDepth) => {
3117
+ setPerfGauge("queue.owner.depth", queueDepth);
3118
+ refreshQueueOwnerLease(lease, { queueDepth }).catch(() => {});
3119
+ }
3120
+ });
3121
+ logQueueOwnerReady({
3122
+ sessionId: options.sessionId,
3123
+ ttlMs,
3124
+ maxQueueDepth,
3125
+ verbose: options.verbose
3126
+ });
3127
+ await refreshQueueOwnerLease(lease, { queueDepth: owner.queueDepth() }).catch(() => {});
3128
+ heartbeatTimer = setInterval(() => {
3129
+ refreshQueueOwnerLease(lease, { queueDepth: owner?.queueDepth() ?? 0 }).catch(() => {});
3130
+ }, QUEUE_OWNER_HEARTBEAT_INTERVAL_MS);
3131
+ let isFirstTask = true;
3132
+ while (true) {
3133
+ const pollTimeoutMs = isFirstTask ? initialTaskPollTimeoutMs : taskPollTimeoutMs;
3134
+ const task = await owner.nextTask(pollTimeoutMs);
3135
+ if (!task) break;
3136
+ isFirstTask = false;
3137
+ await runPromptTurn(async () => {
3138
+ try {
3139
+ await runQueuedTask(options.sessionId, task, {
3140
+ sharedClient,
3141
+ verbose: options.verbose,
3142
+ mcpServers: options.mcpServers,
3143
+ nonInteractivePermissions: options.nonInteractivePermissions,
3144
+ authCredentials: options.authCredentials,
3145
+ authPolicy: options.authPolicy,
3146
+ suppressSdkConsoleErrors: options.suppressSdkConsoleErrors,
3147
+ promptRetries: task.promptRetries ?? 0,
3148
+ sessionOptions: options.sessionOptions,
3149
+ onClientAvailable: setActiveController,
3150
+ onClientClosed: clearActiveController,
3151
+ onPromptActive: async () => {
3152
+ turnController.markPromptActive();
3153
+ await applyPendingCancel();
3154
+ }
3155
+ });
3156
+ } finally {
3157
+ checkpointPerfMetricsCapture();
3158
+ }
3159
+ });
3160
+ }
3161
+ } finally {
3162
+ await closeQueueOwnerRuntime({
3163
+ lease,
3164
+ owner,
3165
+ heartbeatTimer,
3166
+ turnController,
3167
+ sharedClient,
3168
+ sessionId: options.sessionId,
3169
+ verbose: options.verbose
3170
+ });
3171
+ }
3172
+ }
3173
+ async function sendSession(options) {
3174
+ const waitForCompletion = options.waitForCompletion !== false;
3175
+ const queuedToOwner = await submitToRunningOwner(options, waitForCompletion);
3176
+ if (queuedToOwner) return queuedToOwner;
3177
+ spawnQueueOwnerProcess(queueOwnerRuntimeOptionsFromSend(options));
3178
+ for (let attempt = 0; attempt < QUEUE_OWNER_STARTUP_MAX_ATTEMPTS; attempt += 1) {
3179
+ const queued = await submitToRunningOwner(options, waitForCompletion);
3180
+ if (queued) return queued;
3181
+ await waitMs(50);
3182
+ }
3183
+ throw new Error(`Session queue owner failed to start for session ${options.sessionId}`);
3184
+ }
3185
+ //#endregion
3186
+ //#region src/session/session.ts
3187
+ var session_exports = /* @__PURE__ */ __exportAll({
3188
+ DEFAULT_HISTORY_LIMIT: () => 20,
3189
+ DEFAULT_QUEUE_OWNER_TTL_MS: () => DEFAULT_QUEUE_OWNER_TTL_MS,
3190
+ InterruptedError: () => InterruptedError,
3191
+ TimeoutError: () => TimeoutError,
3192
+ cancelSessionPrompt: () => cancelSessionPrompt,
3193
+ closeSession: () => closeSession,
3194
+ createSession: () => createSession,
3195
+ createSessionWithClient: () => createSessionWithClient,
3196
+ ensureSession: () => ensureSession,
3197
+ findGitRepositoryRoot: () => findGitRepositoryRoot,
3198
+ findSession: () => findSession,
3199
+ findSessionByDirectoryWalk: () => findSessionByDirectoryWalk,
3200
+ isProcessAlive: () => isProcessAlive,
3201
+ listAgentSessions: () => listAgentSessions,
3202
+ listSessions: () => listSessions,
3203
+ listSessionsForAgent: () => listSessionsForAgent,
3204
+ normalizeQueueOwnerTtlMs: () => normalizeQueueOwnerTtlMs,
3205
+ pruneSessions: () => pruneSessions,
3206
+ runOnce: () => runOnce,
3207
+ runQueuedTask: () => runQueuedTask,
3208
+ runSessionQueueOwner: () => runSessionQueueOwner,
3209
+ sendSession: () => sendSession,
3210
+ sendSessionDirect: () => sendSessionDirect,
3211
+ setSessionConfigOption: () => setSessionConfigOption,
3212
+ setSessionMode: () => setSessionMode,
3213
+ setSessionModel: () => setSessionModel
3214
+ });
3215
+ //#endregion
3216
+ //#region src/acp/jsonrpc-error.ts
3217
+ const OUTPUT_ERROR_JSONRPC_CODES = {
3218
+ NO_SESSION: -32002,
3219
+ TIMEOUT: -32070,
3220
+ PERMISSION_DENIED: -32071,
3221
+ PERMISSION_PROMPT_UNAVAILABLE: -32072,
3222
+ RUNTIME: -32603,
3223
+ USAGE: -32602
3224
+ };
3225
+ function hasValidAcpError(acp) {
3226
+ return Boolean(acp && Number.isFinite(acp.code) && typeof acp.message === "string" && acp.message.trim().length > 0);
3227
+ }
3228
+ function buildFallbackData(params) {
3229
+ const data = {
3230
+ acpxCode: params.outputCode,
3231
+ detailCode: params.detailCode,
3232
+ origin: params.origin,
3233
+ retryable: params.retryable,
3234
+ timestamp: params.timestamp,
3235
+ sessionId: params.sessionId
3236
+ };
3237
+ for (const [key, value] of Object.entries(data)) if (value === void 0) delete data[key];
3238
+ return data;
3239
+ }
3240
+ function mergeAcpErrorData(acpData, fallbackData) {
3241
+ if (Object.keys(fallbackData).length === 0) return acpData;
3242
+ if (acpData === void 0) return fallbackData;
3243
+ if (acpData && typeof acpData === "object" && !Array.isArray(acpData)) return {
3244
+ ...fallbackData,
3245
+ ...acpData
3246
+ };
3247
+ return acpData;
3248
+ }
3249
+ function buildErrorObject(params) {
3250
+ const fallbackData = buildFallbackData(params);
3251
+ if (hasValidAcpError(params.acp)) {
3252
+ const data = mergeAcpErrorData(params.acp.data, fallbackData);
3253
+ return {
3254
+ code: params.acp.code,
3255
+ message: params.acp.message,
3256
+ ...data !== void 0 ? { data } : {}
3257
+ };
3258
+ }
3259
+ const data = fallbackData;
3260
+ return {
3261
+ code: OUTPUT_ERROR_JSONRPC_CODES[params.outputCode] ?? -32603,
3262
+ message: params.message,
3263
+ ...Object.keys(data).length > 0 ? { data } : {}
3264
+ };
3265
+ }
3266
+ function buildJsonRpcErrorResponse(params) {
3267
+ return {
3268
+ jsonrpc: "2.0",
3269
+ id: params.id ?? null,
3270
+ error: buildErrorObject(params)
3271
+ };
3272
+ }
3273
+ //#endregion
3274
+ //#region src/cli/output/read-suppression.ts
3275
+ const SUPPRESSED_READ_OUTPUT = "[read output suppressed]";
3276
+ function inferToolKindFromTitle(title) {
3277
+ const normalized = title?.trim().toLowerCase();
3278
+ if (!normalized) return;
3279
+ const head = normalized.split(":", 1)[0]?.trim();
3280
+ if (!head) return;
3281
+ if ([
3282
+ "read",
3283
+ "cat",
3284
+ "open",
3285
+ "view"
3286
+ ].some((needle) => head.includes(needle))) return "read";
3287
+ }
3288
+ function isReadLikeTool(tool) {
3289
+ return tool.kind?.trim().toLowerCase() === "read" || inferToolKindFromTitle(tool.title) === "read";
3290
+ }
3291
+ //#endregion
3292
+ //#region src/cli/output/json-formatter.ts
3293
+ const DEFAULT_JSON_SESSION_ID = "unknown";
3294
+ function asRecord$1(value) {
3295
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
3296
+ return value;
3297
+ }
3298
+ function jsonRpcIdKey(value) {
3299
+ if (typeof value === "string") return `s:${value}`;
3300
+ if (typeof value === "number" && Number.isFinite(value)) return `n:${value}`;
3301
+ }
3302
+ function sanitizeReadResult(result) {
3303
+ const record = asRecord$1(result);
3304
+ if (!record || typeof record.content !== "string") return result;
3305
+ return {
3306
+ ...record,
3307
+ content: SUPPRESSED_READ_OUTPUT
3308
+ };
3309
+ }
3310
+ function sanitizeToolContent(content) {
3311
+ if (!Array.isArray(content)) return content;
3312
+ return [{
3313
+ type: "content",
3314
+ content: {
3315
+ type: "text",
3316
+ text: SUPPRESSED_READ_OUTPUT
3317
+ }
3318
+ }];
3319
+ }
3320
+ function sanitizeToolMessage(message) {
3321
+ const root = asRecord$1(message);
3322
+ const params = asRecord$1(root?.params);
3323
+ const update = asRecord$1(params?.update);
3324
+ if (!root || !params || !update) return message;
3325
+ return {
3326
+ ...root,
3327
+ params: {
3328
+ ...params,
3329
+ update: sanitizeToolUpdate(update)
3330
+ }
3331
+ };
3332
+ }
3333
+ function sanitizeToolUpdate(update) {
3334
+ return {
3335
+ ...update,
3336
+ rawOutput: sanitizedRawOutput(update),
3337
+ content: sanitizedContent(update)
3338
+ };
3339
+ }
3340
+ function sanitizedRawOutput(update) {
3341
+ if (Object.prototype.hasOwnProperty.call(update, "rawOutput") && update.rawOutput !== void 0) return { content: SUPPRESSED_READ_OUTPUT };
3342
+ return update.rawOutput;
3343
+ }
3344
+ function sanitizedContent(update) {
3345
+ if (Object.prototype.hasOwnProperty.call(update, "content") && update.content !== void 0) return sanitizeToolContent(update.content);
3346
+ return update.content;
3347
+ }
3348
+ var JsonOutputFormatter = class {
3349
+ stdout;
3350
+ suppressReads;
3351
+ sessionId;
3352
+ requestMethodById = /* @__PURE__ */ new Map();
3353
+ toolStateById = /* @__PURE__ */ new Map();
3354
+ constructor(stdout, suppressReads, context) {
3355
+ this.stdout = stdout;
3356
+ this.suppressReads = suppressReads;
3357
+ this.sessionId = context?.sessionId?.trim() || DEFAULT_JSON_SESSION_ID;
3358
+ }
3359
+ setContext(context) {
3360
+ this.sessionId = context.sessionId?.trim() || this.sessionId || DEFAULT_JSON_SESSION_ID;
3361
+ }
3362
+ onAcpMessage(message) {
3363
+ this.stdout.write(`${JSON.stringify(this.sanitizeMessage(message))}\n`);
3364
+ }
3365
+ sanitizeMessage(message) {
3366
+ if (!this.suppressReads) return message;
3367
+ const sanitizedResponse = this.sanitizeReadResponse(message);
3368
+ if (sanitizedResponse !== message) return sanitizedResponse;
3369
+ const sanitizedToolMessage = this.sanitizeReadToolMessage(message);
3370
+ if (sanitizedToolMessage !== message) return sanitizedToolMessage;
3371
+ this.trackRequestMethod(message);
3372
+ return message;
3373
+ }
3374
+ trackRequestMethod(message) {
3375
+ const candidate = message;
3376
+ if (typeof candidate.method !== "string") return;
3377
+ const idKey = jsonRpcIdKey(candidate.id);
3378
+ if (!idKey) return;
3379
+ this.requestMethodById.set(idKey, candidate.method);
3380
+ }
3381
+ sanitizeReadResponse(message) {
3382
+ const candidate = message;
3383
+ const idKey = jsonRpcIdKey(candidate.id);
3384
+ if (!idKey || !Object.hasOwn(candidate, "result")) return message;
3385
+ const method = this.requestMethodById.get(idKey);
3386
+ this.requestMethodById.delete(idKey);
3387
+ if (method !== "fs/read_text_file") return message;
3388
+ const root = asRecord$1(message);
3389
+ if (!root) return message;
3390
+ return {
3391
+ ...root,
3392
+ result: sanitizeReadResult(candidate.result)
3393
+ };
3394
+ }
3395
+ sanitizeReadToolMessage(message) {
3396
+ const update = this.readToolUpdate(message);
3397
+ if (!update) return message;
3398
+ const toolCallId = typeof update.toolCallId === "string" ? update.toolCallId : void 0;
3399
+ if (!toolCallId) return message;
3400
+ const current = this.mergeToolState(toolCallId, update);
3401
+ this.toolStateById.set(toolCallId, current);
3402
+ return isReadLikeTool(current) ? sanitizeToolMessage(message) : message;
3403
+ }
3404
+ readToolUpdate(message) {
3405
+ const root = asRecord$1(message);
3406
+ if (root?.method !== "session/update") return;
3407
+ const params = asRecord$1(root.params);
3408
+ const update = asRecord$1(params?.update);
3409
+ if (!params || !update) return;
3410
+ const sessionUpdate = update.sessionUpdate;
3411
+ if (sessionUpdate !== "tool_call" && sessionUpdate !== "tool_call_update") return;
3412
+ return update;
3413
+ }
3414
+ mergeToolState(toolCallId, update) {
3415
+ const previous = this.toolStateById.get(toolCallId) ?? {};
3416
+ return {
3417
+ title: typeof update.title === "string" ? update.title : previous.title,
3418
+ kind: typeof update.kind === "string" || update.kind === null ? update.kind : previous.kind
3419
+ };
3420
+ }
3421
+ onError(params) {
3422
+ this.stdout.write(`${JSON.stringify(buildJsonRpcErrorResponse({
3423
+ outputCode: params.code,
3424
+ detailCode: params.detailCode,
3425
+ origin: params.origin,
3426
+ message: params.message,
3427
+ retryable: params.retryable,
3428
+ timestamp: params.timestamp,
3429
+ sessionId: this.sessionId,
3430
+ acp: params.acp
3431
+ }))}\n`);
3432
+ }
3433
+ onPermissionEscalation() {}
3434
+ flush() {}
3435
+ };
3436
+ function createJsonOutputFormatter(stdout, suppressReads = false, context) {
3437
+ return new JsonOutputFormatter(stdout, suppressReads, context);
3438
+ }
3439
+ //#endregion
3440
+ //#region src/cli/output/output.ts
3441
+ var output_exports = /* @__PURE__ */ __exportAll({
3442
+ createOutputFormatter: () => createOutputFormatter,
3443
+ getTextErrorRemediationHints: () => getTextErrorRemediationHints
3444
+ });
3445
+ const MAX_THOUGHT_CHARS = 900;
3446
+ const MAX_INLINE_CHARS = 220;
3447
+ const MAX_OUTPUT_CHARS = 2e3;
3448
+ const MAX_OUTPUT_LINES = 28;
3449
+ const MAX_LOCATION_ITEMS = 5;
3450
+ const OUTPUT_PRIORITY_KEYS = [
3451
+ "stdout",
3452
+ "stderr",
3453
+ "output",
3454
+ "content",
3455
+ "text",
3456
+ "message",
3457
+ "result",
3458
+ "response",
3459
+ "value"
3460
+ ];
3461
+ function asStatus(status) {
3462
+ return status ?? "unknown";
3463
+ }
3464
+ function isFinalStatus(status) {
3465
+ return status === "completed" || status === "failed";
3466
+ }
3467
+ function toStatusLabel(status) {
3468
+ switch (status) {
3469
+ case "in_progress": return "running";
3470
+ case "pending": return "pending";
3471
+ case "completed": return "completed";
3472
+ case "failed": return "failed";
3473
+ default: return "running";
3474
+ }
3475
+ }
3476
+ function asRecord(value) {
3477
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
3478
+ return value;
3479
+ }
3480
+ function extractJsonRpcMethod(message) {
3481
+ return Object.hasOwn(message, "method") ? message.method?.toString() : void 0;
3482
+ }
3483
+ function collapseWhitespace(value) {
3484
+ return value.replace(/\s+/g, " ").trim();
3485
+ }
3486
+ function normalizeLineEndings(value) {
3487
+ return value.replace(/\r\n?/g, "\n");
3488
+ }
3489
+ function truncate(value, maxChars) {
3490
+ if (value.length <= maxChars) return value;
3491
+ if (maxChars <= 3) return value.slice(0, maxChars);
3492
+ return `${value.slice(0, maxChars - 3)}...`;
3493
+ }
3494
+ function toInline(value, maxChars = MAX_INLINE_CHARS) {
3495
+ return truncate(collapseWhitespace(value), maxChars);
3496
+ }
3497
+ function indentBlock(value, prefix) {
3498
+ return value.split("\n").map((line) => `${prefix}${line}`).join("\n");
3499
+ }
3500
+ function dedupeStrings(values) {
3501
+ const seen = /* @__PURE__ */ new Set();
3502
+ const result = [];
3503
+ for (const value of values) {
3504
+ if (seen.has(value)) continue;
3505
+ seen.add(value);
3506
+ result.push(value);
3507
+ }
3508
+ return result;
3509
+ }
3510
+ function safeJson(value, spacing) {
3511
+ const seen = /* @__PURE__ */ new WeakSet();
3512
+ try {
3513
+ return JSON.stringify(value, (_key, entry) => {
3514
+ if (typeof entry === "bigint") return `${entry}n`;
3515
+ if (typeof entry === "function") return `[Function ${entry.name || "anonymous"}]`;
3516
+ if (typeof entry === "symbol") return entry.toString();
3517
+ if (entry && typeof entry === "object") {
3518
+ if (seen.has(entry)) return "[Circular]";
3519
+ seen.add(entry);
3520
+ }
3521
+ return entry;
3522
+ }, spacing);
3523
+ } catch {
3524
+ return;
3525
+ }
3526
+ }
3527
+ function readFirstString(source, keys) {
3528
+ for (const key of keys) {
3529
+ const value = source[key];
3530
+ if (typeof value === "string" && value.trim()) return value.trim();
3531
+ }
3532
+ }
3533
+ function readFirstFiniteNumber(source, keys) {
3534
+ for (const key of keys) {
3535
+ const value = source[key];
3536
+ if (typeof value === "number" && Number.isFinite(value)) return value;
3537
+ }
3538
+ }
3539
+ function formatMetadataNumber(value) {
3540
+ return Number.isInteger(value) ? String(value) : String(Number(value.toFixed(8)));
3541
+ }
3542
+ function readFirstStringArray(source, keys) {
3543
+ for (const key of keys) {
3544
+ const value = source[key];
3545
+ if (!Array.isArray(value)) continue;
3546
+ const entries = value.map((entry) => typeof entry === "string" ? entry.trim() : "").filter((entry) => entry.length > 0);
3547
+ if (entries.length > 0) return entries;
3548
+ }
3549
+ }
3550
+ function formatDisjunction(values) {
3551
+ if (values.length <= 1) return values[0] ?? "";
3552
+ if (values.length === 2) return `${values[0]} or ${values[1]}`;
3553
+ return `${values.slice(0, -1).join(", ")}, or ${values.at(-1)}`;
3554
+ }
3555
+ function parseAuthMethodIdsFromMessage(message) {
3556
+ const methods = [];
3557
+ const methodListMatch = message.match(/auth methods \[([^\]]+)\]/iu);
3558
+ if (methodListMatch) methods.push(...methodListMatch[1].split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0));
3559
+ const singleMethodMatch = message.match(/auth method ([\w.-]+)/iu);
3560
+ if (singleMethodMatch) methods.push(singleMethodMatch[1]);
3561
+ return dedupeStrings(methods);
3562
+ }
3563
+ function parseAuthMethodIdsFromAcpData(data) {
3564
+ const record = asRecord(data);
3565
+ if (!record) return [];
3566
+ const methodIds = [];
3567
+ if (typeof record.methodId === "string" && record.methodId.trim().length > 0) methodIds.push(record.methodId.trim());
3568
+ if (Array.isArray(record.methods)) for (const entry of record.methods) {
3569
+ const id = parseAuthMethodIdEntry(entry);
3570
+ if (id) methodIds.push(id);
3571
+ }
3572
+ return dedupeStrings(methodIds);
3573
+ }
3574
+ function parseAuthMethodIdEntry(entry) {
3575
+ if (typeof entry === "string" && entry.trim().length > 0) return entry.trim();
3576
+ const id = asRecord(entry)?.id;
3577
+ return typeof id === "string" && id.trim().length > 0 ? id.trim() : void 0;
3578
+ }
3579
+ function renderAuthRequiredHint(params) {
3580
+ const methodIds = dedupeStrings([...parseAuthMethodIdsFromAcpData(params.acp?.data), ...parseAuthMethodIdsFromMessage(params.message)]);
3581
+ if (methodIds.length === 0) return "hint: run `acpx config show` to locate the active config, then add the required credential under `auth` and retry.";
3582
+ return `hint: run \`acpx config show\` to locate the active config, then add ${formatDisjunction(methodIds.map((methodId) => `\`auth.${methodId}\``))} and retry.`;
3583
+ }
3584
+ function getTextErrorRemediationHints(params) {
3585
+ const lowerMessage = params.message.toLowerCase();
3586
+ if (params.detailCode === "AUTH_REQUIRED") return [renderAuthRequiredHint(params)];
3587
+ if (params.code === "TIMEOUT") return ["hint: increase `--timeout <seconds>` for long-running prompts, or check whether the agent/provider is stalled."];
3588
+ if (params.code === "NO_SESSION") return noSessionHints(lowerMessage);
3589
+ return matchingTextErrorRule(params, lowerMessage)?.hints ?? [];
3590
+ }
3591
+ const TEXT_ERROR_HINT_RULES = [
3592
+ {
3593
+ matches: (_params, lowerMessage) => isUnsupportedSessionLoadError(lowerMessage),
3594
+ hints: ["hint: this adapter cannot resume saved ACP sessions; create a fresh one with `acpx <agent> sessions new` instead of reusing `--resume-session`."]
3595
+ },
3596
+ {
3597
+ matches: (_params, lowerMessage) => isSessionLoadError(lowerMessage),
3598
+ hints: ["hint: rerun with `--verbose` to capture the ACP load failure details.", "hint: if you do not need the old backend session, start a fresh one with `acpx <agent> sessions new` and retry."]
3599
+ },
3600
+ {
3601
+ matches: (params, lowerMessage) => isRateLimitError(params.message, lowerMessage),
3602
+ hints: ["hint: the provider appears rate-limited; retry later, switch model, or check provider quota/billing."]
3603
+ },
3604
+ {
3605
+ matches: (_params, lowerMessage) => isModelLookupError(lowerMessage),
3606
+ hints: ["hint: check the configured model name for this agent, then retry with `--model <model>` or `sessions set-model <model>`."]
3607
+ },
3608
+ {
3609
+ matches: (_params, lowerMessage) => isSessionConfigMethodError(lowerMessage),
3610
+ hints: ["hint: rerun with `--verbose` to capture the ACP method/error details before retrying."]
3611
+ },
3612
+ {
3613
+ matches: isRuntimeAcpProtocolError,
3614
+ hints: ["hint: rerun with `--verbose` to capture the underlying ACP error details."]
3615
+ }
3616
+ ];
3617
+ function matchingTextErrorRule(params, lowerMessage) {
3618
+ return TEXT_ERROR_HINT_RULES.find((rule) => rule.matches(params, lowerMessage));
3619
+ }
3620
+ function noSessionHints(lowerMessage) {
3621
+ if (lowerMessage.includes("create one:")) return [];
3622
+ return ["hint: the saved ACP session is missing or stale; start a fresh session with `acpx <agent> sessions new`, then retry."];
3623
+ }
3624
+ function isUnsupportedSessionLoadError(lowerMessage) {
3625
+ return lowerMessage.includes("does not support session/resume") || lowerMessage.includes("does not support session/load");
3626
+ }
3627
+ function isSessionLoadError(lowerMessage) {
3628
+ return lowerMessage.includes("failed to resume acp session") || lowerMessage.includes("session/resume") || lowerMessage.includes("session/load");
3629
+ }
3630
+ function isRateLimitError(message, lowerMessage) {
3631
+ return /\b429\b/u.test(message) || ["rate limit", "quota exceeded"].some((text) => lowerMessage.includes(text));
3632
+ }
3633
+ function isModelLookupError(lowerMessage) {
3634
+ return [
3635
+ "model not found",
3636
+ "unknown model",
3637
+ "invalid model"
3638
+ ].some((text) => lowerMessage.includes(text));
3639
+ }
3640
+ function isSessionConfigMethodError(lowerMessage) {
3641
+ return [
3642
+ "session/set_mode",
3643
+ "session/set_model",
3644
+ "session/set_config_option"
3645
+ ].some((text) => lowerMessage.includes(text));
3646
+ }
3647
+ function isRuntimeAcpProtocolError(params, lowerMessage) {
3648
+ return params.origin === "acp" && params.code === "RUNTIME" && (params.acp?.code === -32602 || params.acp?.code === -32603 || lowerMessage.includes("internal error"));
3649
+ }
3650
+ function summarizeToolInput(rawInput) {
3651
+ if (rawInput == null) return;
3652
+ if (isScalarToolInput(rawInput)) return toInline(String(rawInput));
3653
+ const record = asRecord(rawInput);
3654
+ if (record) return summarizeToolInputRecord(record) ?? summarizeToolInputJson(rawInput);
3655
+ return summarizeToolInputJson(rawInput);
3656
+ }
3657
+ function isScalarToolInput(value) {
3658
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
3659
+ }
3660
+ function summarizeToolInputRecord(record) {
3661
+ const command = readFirstString(record, [
3662
+ "command",
3663
+ "cmd",
3664
+ "program"
3665
+ ]);
3666
+ const args = readFirstStringArray(record, ["args", "arguments"]);
3667
+ if (command) return toInline([command, ...args ?? []].join(" "));
3668
+ const location = readFirstString(record, [
3669
+ "path",
3670
+ "file",
3671
+ "filePath",
3672
+ "filepath",
3673
+ "target",
3674
+ "uri",
3675
+ "url"
3676
+ ]);
3677
+ const query = readFirstString(record, [
3678
+ "query",
3679
+ "pattern",
3680
+ "text",
3681
+ "search"
3682
+ ]);
3683
+ return location || query ? toInline(location ?? query ?? "") : void 0;
3684
+ }
3685
+ function summarizeToolInputJson(rawInput) {
3686
+ const json = safeJson(rawInput, 0);
3687
+ return json ? toInline(json) : void 0;
3688
+ }
3689
+ function formatLocations(locations) {
3690
+ if (!locations || locations.length === 0) return;
3691
+ const unique = /* @__PURE__ */ new Set();
3692
+ for (const location of locations) {
3693
+ const formatted = formatLocation(location);
3694
+ if (formatted) unique.add(formatted);
3695
+ }
3696
+ const items = [...unique];
3697
+ if (items.length === 0) return;
3698
+ const visible = items.slice(0, MAX_LOCATION_ITEMS);
3699
+ const hidden = items.length - visible.length;
3700
+ if (hidden <= 0) return visible.join(", ");
3701
+ return `${visible.join(", ")}, +${hidden} more`;
3702
+ }
3703
+ function formatLocation(location) {
3704
+ const path = location.path?.trim();
3705
+ if (!path) return;
3706
+ return `${path}${typeof location.line === "number" && Number.isFinite(location.line) ? `:${Math.max(1, Math.trunc(location.line))}` : ""}`;
3707
+ }
3708
+ function summarizeDiff(path, oldText, newText) {
3709
+ const oldLines = oldText ? oldText.split("\n").length : 0;
3710
+ const delta = newText.split("\n").length - oldLines;
3711
+ if (delta === 0) return `diff ${path} (line count unchanged)`;
3712
+ return `diff ${path} (${`${delta > 0 ? "+" : ""}${delta}`} lines)`;
3713
+ }
3714
+ function textFromContentBlock(content) {
3715
+ switch (content.type) {
3716
+ case "text": return content.text;
3717
+ case "resource_link": return content.title ?? content.name ?? content.uri;
3718
+ case "resource": return textFromResourceBlock(content);
3719
+ case "image": return `[image] ${content.mimeType}`;
3720
+ case "audio": return `[audio] ${content.mimeType}`;
3721
+ default: return;
3722
+ }
3723
+ }
3724
+ function textFromResourceBlock(content) {
3725
+ if ("text" in content.resource && typeof content.resource.text === "string") return content.resource.text;
3726
+ const uri = content.resource.uri;
3727
+ const mimeType = content.resource.mimeType;
3728
+ return `[resource] ${uri}${mimeType ? ` (${mimeType})` : ""}`;
3729
+ }
3730
+ function summarizeToolContent(content) {
3731
+ if (!content || content.length === 0) return;
3732
+ const fragments = [];
3733
+ for (const entry of content) {
3734
+ const fragment = summarizeToolContentEntry(entry);
3735
+ if (fragment) fragments.push(fragment);
3736
+ }
3737
+ const unique = dedupeStrings(fragments.map((fragment) => fragment.trim()).filter((fragment) => fragment.length > 0));
3738
+ if (unique.length === 0) return;
3739
+ return unique.join("\n\n");
3740
+ }
3741
+ function summarizeToolContentEntry(entry) {
3742
+ if (entry.type === "content") {
3743
+ const text = textFromContentBlock(entry.content);
3744
+ return text && text.trim() ? text.trimEnd() : void 0;
3745
+ }
3746
+ if (entry.type === "diff") return summarizeDiff(entry.path, entry.oldText, entry.newText);
3747
+ if (entry.type === "terminal") return `[terminal] ${entry.terminalId}`;
3748
+ }
3749
+ function extractOutputText(value, depth = 0, seen = /* @__PURE__ */ new Set()) {
3750
+ if (value == null) return;
3751
+ const scalar = extractScalarOutputText(value);
3752
+ if (scalar !== null) return scalar;
3753
+ if (depth >= 4) return;
3754
+ if (Array.isArray(value)) return extractOutputTextArray(value, depth, seen);
3755
+ return extractOutputTextRecord(value, depth, seen);
3756
+ }
3757
+ function extractOutputTextRecord(value, depth, seen) {
3758
+ const record = asRecord(value);
3759
+ if (!record || seen.has(record)) return;
3760
+ seen.add(record);
3761
+ return extractPreferredOutputText(record, depth, seen) ?? extractJsonOutputText(record);
3762
+ }
3763
+ function extractScalarOutputText(value) {
3764
+ if (typeof value === "string") {
3765
+ const trimmed = value.trimEnd();
3766
+ return trimmed.length > 0 ? trimmed : void 0;
3767
+ }
3768
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
3769
+ return null;
3770
+ }
3771
+ function extractOutputTextArray(value, depth, seen) {
3772
+ const parts = value.map((entry) => extractOutputText(entry, depth + 1, seen)).filter((entry) => Boolean(entry));
3773
+ return parts.length > 0 ? dedupeStrings(parts).join("\n") : void 0;
3774
+ }
3775
+ function extractPreferredOutputText(record, depth, seen) {
3776
+ const uniquePreferred = dedupeStrings(OUTPUT_PRIORITY_KEYS.flatMap((key) => {
3777
+ if (!(key in record)) return [];
3778
+ const extracted = extractOutputText(record[key], depth + 1, seen);
3779
+ return extracted ? [extracted] : [];
3780
+ }));
3781
+ return uniquePreferred.length > 0 ? uniquePreferred.join("\n") : void 0;
3782
+ }
3783
+ function extractJsonOutputText(record) {
3784
+ const json = safeJson(record, 2);
3785
+ return !json || json === "{}" ? void 0 : json;
3786
+ }
3787
+ function summarizeToolOutput(rawOutput, content) {
3788
+ const fragments = dedupeStrings([extractOutputText(rawOutput), summarizeToolContent(content)].map((fragment) => fragment?.trim()).filter((fragment) => Boolean(fragment)));
3789
+ if (fragments.length === 0) return;
3790
+ return fragments.join("\n\n");
3791
+ }
3792
+ function renderToolOutput(state, suppressReads) {
3793
+ if (suppressReads && isReadLikeTool(state)) return SUPPRESSED_READ_OUTPUT;
3794
+ return summarizeToolOutput(state.rawOutput, state.content);
3795
+ }
3796
+ function limitOutputBlock(value) {
3797
+ const normalized = value.replace(/\r\n/g, "\n").trim();
3798
+ if (!normalized) return "";
3799
+ const lines = normalized.split("\n");
3800
+ const visible = lines.slice(0, MAX_OUTPUT_LINES);
3801
+ let result = visible.join("\n");
3802
+ if (lines.length > visible.length) {
3803
+ const hidden = lines.length - visible.length;
3804
+ result += `\n... (${hidden} more lines)`;
3805
+ }
3806
+ if (result.length > MAX_OUTPUT_CHARS) result = `${result.slice(0, MAX_OUTPUT_CHARS - 3)}...`;
3807
+ return result;
3808
+ }
3809
+ var TextOutputFormatter = class {
3810
+ stdout;
3811
+ useColor;
3812
+ suppressReads;
3813
+ toolStates = /* @__PURE__ */ new Map();
3814
+ thoughtBuffer = "";
3815
+ wroteAny = false;
3816
+ atLineStart = true;
3817
+ section = null;
3818
+ constructor(stdout, suppressReads) {
3819
+ this.stdout = stdout;
3820
+ this.useColor = Boolean(stdout.isTTY);
3821
+ this.suppressReads = suppressReads;
3822
+ }
3823
+ setContext(_context) {}
3824
+ onAcpMessage(message) {
3825
+ const notification = extractSessionUpdateNotification(message);
3826
+ if (notification) {
3827
+ this.renderSessionUpdate(notification);
3828
+ return;
3829
+ }
3830
+ const method = extractJsonRpcMethod(message);
3831
+ if (method && method !== "session/prompt" && method !== "session/cancel") {
3832
+ this.onClientOperation({
3833
+ method,
3834
+ status: "running",
3835
+ summary: method,
3836
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
3837
+ });
3838
+ return;
3839
+ }
3840
+ const stopReason = parsePromptStopReason(message);
3841
+ if (stopReason) {
3842
+ this.renderDone(stopReason);
3843
+ return;
3844
+ }
3845
+ const errorMessage = parseJsonRpcErrorMessage(message);
3846
+ if (errorMessage) this.onError({
3847
+ code: "RUNTIME",
3848
+ origin: "acp",
3849
+ message: errorMessage
3850
+ });
3851
+ }
3852
+ renderSessionUpdate(notification) {
3853
+ const update = notification.update;
3854
+ if (update.sessionUpdate !== "agent_thought_chunk") this.flushThoughtBuffer();
3855
+ this.renderSessionUpdateBody(update);
3856
+ }
3857
+ renderSessionUpdateBody(update) {
3858
+ switch (update.sessionUpdate) {
3859
+ case "agent_message_chunk":
3860
+ if (update.content.type === "text") this.writeAssistantChunk(update.content.text);
3861
+ return;
3862
+ case "agent_thought_chunk":
3863
+ if (update.content.type === "text") this.thoughtBuffer += update.content.text;
3864
+ return;
3865
+ case "tool_call":
3866
+ this.renderToolUpdate(update);
3867
+ return;
3868
+ case "tool_call_update":
3869
+ this.renderToolUpdate(update);
3870
+ return;
3871
+ case "plan":
3872
+ this.renderPlanUpdate(update.entries);
3873
+ return;
3874
+ default: return;
3875
+ }
3876
+ }
3877
+ renderPlanUpdate(entries) {
3878
+ this.beginSection("plan");
3879
+ this.writeLine(this.bold("[plan]"));
3880
+ for (const entry of entries) this.writeLine(` - [${entry.status}] ${entry.content}`);
3881
+ }
3882
+ renderDone(stopReason) {
3883
+ this.flushThoughtBuffer();
3884
+ this.beginSection("done");
3885
+ this.writeLine(this.dim(`[done] ${stopReason}`));
3886
+ }
3887
+ onError(params) {
3888
+ this.flushThoughtBuffer();
3889
+ this.beginSection("done");
3890
+ this.writeLine(this.formatAnsi(`[error] ${params.code}: ${params.message}`, "31"));
3891
+ for (const hint of getTextErrorRemediationHints(params)) this.writeLine(this.dim(hint));
3892
+ }
3893
+ onClientOperation(operation) {
3894
+ this.flushThoughtBuffer();
3895
+ this.beginSection("client");
3896
+ const normalizedStatus = operation.status === "completed" ? "completed" : operation.status === "failed" ? "failed" : "in_progress";
3897
+ const statusText = this.colorStatus(operation.status, normalizedStatus);
3898
+ this.writeLine(`${this.bold("[client]")} ${operation.summary} (${statusText})`);
3899
+ if (operation.details && operation.details.trim().length > 0) {
3900
+ this.writeLine(" details:");
3901
+ this.writeLine(indentBlock(operation.details, " "));
3902
+ }
3903
+ }
3904
+ onPermissionEscalation(event) {
3905
+ this.flushThoughtBuffer();
3906
+ this.beginSection("client");
3907
+ this.writeLine(`${this.bold("[permission]")} ${event.message}`);
3908
+ const details = [
3909
+ `sessionId: ${event.sessionId}`,
3910
+ `toolCallId: ${event.toolCallId}`,
3911
+ event.toolName ? `toolName: ${event.toolName}` : void 0,
3912
+ `toolTitle: ${event.toolTitle}`,
3913
+ event.toolInput !== void 0 ? `toolInput: ${summarizeToolInput(event.toolInput) ?? "(structured input)"}` : void 0,
3914
+ event.toolKind ? `toolKind: ${event.toolKind}` : void 0,
3915
+ event.matchedRule ? `matchedRule: ${event.matchedRule}` : void 0
3916
+ ].filter((line) => Boolean(line));
3917
+ this.writeLine(indentBlock(details.join("\n"), " "));
3918
+ }
3919
+ flush() {
3920
+ this.flushThoughtBuffer();
3921
+ if (!this.atLineStart) this.write("\n");
3922
+ }
3923
+ write(chunk) {
3924
+ if (!chunk) return;
3925
+ this.stdout.write(chunk);
3926
+ this.wroteAny = true;
3927
+ this.atLineStart = chunk.endsWith("\n");
3928
+ }
3929
+ writeLine(line) {
3930
+ this.write(`${line}\n`);
3931
+ }
3932
+ beginSection(next) {
3933
+ if (!this.atLineStart) this.write("\n");
3934
+ if (this.wroteAny) this.write("\n");
3935
+ this.section = next;
3936
+ }
3937
+ writeAssistantChunk(text) {
3938
+ if (!text) return;
3939
+ this.section = "assistant";
3940
+ this.write(text);
3941
+ }
3942
+ flushThoughtBuffer() {
3943
+ const thought = truncate(normalizeLineEndings(this.thoughtBuffer).trim(), MAX_THOUGHT_CHARS);
3944
+ this.thoughtBuffer = "";
3945
+ if (!thought) return;
3946
+ this.beginSection("thought");
3947
+ const [firstLine, ...restLines] = thought.split("\n");
3948
+ this.writeLine(this.dim(`[thinking] ${firstLine}`));
3949
+ for (const line of restLines) this.writeLine(this.dim(` ${line}`));
3950
+ }
3951
+ renderToolUpdate(update) {
3952
+ const state = this.getOrCreateToolState(update.toolCallId);
3953
+ this.mergeToolState(state, update);
3954
+ const status = asStatus(state.status);
3955
+ if (isFinalStatus(status)) {
3956
+ const signature = this.toolSignature(state);
3957
+ if (signature !== state.finalSignature) {
3958
+ state.finalSignature = signature;
3959
+ this.renderFinalToolState(state, status);
3960
+ }
3961
+ return;
3962
+ }
3963
+ if (state.startedPrinted) return;
3964
+ state.startedPrinted = true;
3965
+ this.renderStartingToolState(state, status);
3966
+ }
3967
+ getOrCreateToolState(toolCallId) {
3968
+ const existing = this.toolStates.get(toolCallId);
3969
+ if (existing) return existing;
3970
+ const created = {
3971
+ id: toolCallId,
3972
+ startedPrinted: false
3973
+ };
3974
+ this.toolStates.set(toolCallId, created);
3975
+ return created;
3976
+ }
3977
+ mergeToolState(state, update) {
3978
+ this.mergeToolTitle(state, update.title);
3979
+ this.mergeToolPayloadState(state, update);
3980
+ }
3981
+ mergeToolTitle(state, title) {
3982
+ if (typeof title === "string" && title.trim().length > 0) state.title = title;
3983
+ }
3984
+ mergeToolPayloadState(state, update) {
3985
+ if (update.status !== void 0) state.status = update.status;
3986
+ if (update.kind !== void 0) state.kind = update.kind;
3987
+ if (update.locations !== void 0) state.locations = update.locations;
3988
+ if (update.rawInput !== void 0) state.rawInput = update.rawInput;
3989
+ if (update.rawOutput !== void 0) state.rawOutput = update.rawOutput;
3990
+ if (update.content !== void 0) state.content = update.content;
3991
+ }
3992
+ toolSignature(state) {
3993
+ const signaturePayload = {
3994
+ title: state.title,
3995
+ status: state.status,
3996
+ kind: state.kind,
3997
+ input: summarizeToolInput(state.rawInput),
3998
+ files: formatLocations(state.locations),
3999
+ output: renderToolOutput(state, this.suppressReads)
4000
+ };
4001
+ return safeJson(signaturePayload, 0) ?? JSON.stringify(signaturePayload);
4002
+ }
4003
+ renderStartingToolState(state, status) {
4004
+ this.beginSection("tool");
4005
+ const title = state.title ?? state.id;
4006
+ const label = status === "pending" ? "pending" : "running";
4007
+ const statusText = this.colorStatus(label, status);
4008
+ this.writeLine(`${this.bold("[tool]")} ${title} (${statusText})`);
4009
+ const input = summarizeToolInput(state.rawInput);
4010
+ if (input) this.writeLine(` input: ${input}`);
4011
+ const files = formatLocations(state.locations);
4012
+ if (files) this.writeLine(` files: ${files}`);
4013
+ }
4014
+ renderFinalToolState(state, status) {
4015
+ this.beginSection("tool");
4016
+ const title = state.title ?? state.id;
4017
+ const statusText = this.colorStatus(toStatusLabel(status), status);
4018
+ this.writeLine(`${this.bold("[tool]")} ${title} (${statusText})`);
4019
+ if (state.kind) this.writeLine(` kind: ${state.kind}`);
4020
+ const input = summarizeToolInput(state.rawInput);
4021
+ if (input) this.writeLine(` input: ${input}`);
4022
+ const files = formatLocations(state.locations);
4023
+ if (files) this.writeLine(` files: ${files}`);
4024
+ const output = renderToolOutput(state, this.suppressReads);
4025
+ if (output) {
4026
+ this.writeLine(" output:");
4027
+ this.writeLine(indentBlock(limitOutputBlock(output), " "));
4028
+ }
4029
+ }
4030
+ formatAnsi(text, code) {
4031
+ if (!this.useColor) return text;
4032
+ return `\u001b[${code}m${text}\u001b[0m`;
4033
+ }
4034
+ bold(text) {
4035
+ return this.formatAnsi(text, "1");
4036
+ }
4037
+ dim(text) {
4038
+ return this.formatAnsi(text, "2");
4039
+ }
4040
+ colorStatus(text, status) {
4041
+ if (!this.useColor) return text;
4042
+ switch (status) {
4043
+ case "completed": return this.formatAnsi(text, "32");
4044
+ case "failed": return this.formatAnsi(text, "31");
4045
+ default: return this.formatAnsi(text, "33");
4046
+ }
4047
+ }
4048
+ };
4049
+ var QuietOutputFormatter = class {
4050
+ stdout;
4051
+ stderr;
4052
+ chunks = [];
4053
+ flushed = false;
4054
+ metadataFlushed = false;
4055
+ constructor(stdout, stderr) {
4056
+ this.stdout = stdout;
4057
+ this.stderr = stderr;
4058
+ }
4059
+ setContext(_context) {}
4060
+ onAcpMessage(message) {
4061
+ const update = extractSessionUpdateNotification(message);
4062
+ if (update?.update.sessionUpdate === "agent_message_chunk" && update.update.content.type === "text") {
4063
+ this.chunks.push(update.update.content.text);
4064
+ return;
4065
+ }
4066
+ if (parsePromptStopReason(message)) {
4067
+ this.flushBufferedOutput();
4068
+ this.flushMetadata(message);
4069
+ }
4070
+ }
4071
+ onError(_params) {}
4072
+ onPermissionEscalation(_event) {}
4073
+ flush() {}
4074
+ flushBufferedOutput() {
4075
+ if (this.flushed) return;
4076
+ this.flushed = true;
4077
+ const text = this.chunks.join("");
4078
+ this.stdout.write(text.endsWith("\n") ? text : `${text}\n`);
4079
+ }
4080
+ flushMetadata(message) {
4081
+ if (this.metadataFlushed) return;
4082
+ this.metadataFlushed = true;
4083
+ const result = asRecord(message.result);
4084
+ if (!result) return;
4085
+ const usageLine = this.formatUsageLine(asRecord(result.usage));
4086
+ if (usageLine) this.stderr.write(`${usageLine}\n`);
4087
+ const costLine = this.formatCostLine(result.cost);
4088
+ if (costLine) this.stderr.write(`${costLine}\n`);
4089
+ }
4090
+ formatUsageLine(usage) {
4091
+ if (!usage) return;
4092
+ const parts = [];
4093
+ for (const [label, keys] of [
4094
+ ["input", ["inputTokens", "input_tokens"]],
4095
+ ["output", ["outputTokens", "output_tokens"]],
4096
+ ["cache_read", [
4097
+ "cachedReadTokens",
4098
+ "cacheReadInputTokens",
4099
+ "cache_read_input_tokens"
4100
+ ]],
4101
+ ["cache_write", [
4102
+ "cachedWriteTokens",
4103
+ "cacheCreationInputTokens",
4104
+ "cache_creation_input_tokens"
4105
+ ]],
4106
+ ["total", ["totalTokens", "total_tokens"]]
4107
+ ]) {
4108
+ const value = readFirstFiniteNumber(usage, keys);
4109
+ if (value !== void 0) parts.push(`${label}=${formatMetadataNumber(value)}`);
4110
+ }
4111
+ return parts.length > 0 ? `[acpx] tokens: ${parts.join(" ")}` : void 0;
4112
+ }
4113
+ formatCostLine(cost) {
4114
+ const scalar = this.formatScalarCostLine(cost);
4115
+ if (scalar) return scalar;
4116
+ const record = asRecord(cost);
4117
+ if (!record) return;
4118
+ const amount = readFirstFiniteNumber(record, [
4119
+ "amount",
4120
+ "value",
4121
+ "total"
4122
+ ]);
4123
+ if (amount === void 0) return;
4124
+ const currency = typeof record.currency === "string" && record.currency.trim() ? ` ${record.currency.trim()}` : "";
4125
+ return `[acpx] cost: ${formatMetadataNumber(amount)}${currency}`;
4126
+ }
4127
+ formatScalarCostLine(cost) {
4128
+ if (typeof cost === "number" && Number.isFinite(cost)) return `[acpx] cost: ${formatMetadataNumber(cost)}`;
4129
+ if (typeof cost === "string" && cost.trim()) return `[acpx] cost: ${cost.trim()}`;
4130
+ }
4131
+ };
4132
+ function createOutputFormatter(format, options = {}) {
4133
+ const stdout = options.stdout ?? process.stdout;
4134
+ const stderr = options.stderr ?? process.stderr;
4135
+ const suppressReads = options.suppressReads === true;
4136
+ switch (format) {
4137
+ case "text": return new TextOutputFormatter(stdout, suppressReads);
4138
+ case "json": return createJsonOutputFormatter(stdout, suppressReads, options.jsonContext);
4139
+ case "quiet": return new QuietOutputFormatter(stdout, stderr);
4140
+ default: throw new Error("Unsupported output format");
4141
+ }
4142
+ }
4143
+ //#endregion
4144
+ export { runSessionQueueOwner as a, buildQueueOwnerArgOverride as c, createSessionWithClient as d, cancelSessionPrompt as f, __exportAll as h, session_exports as i, flushPerfMetricsCapture as l, DEFAULT_QUEUE_OWNER_TTL_MS as m, getTextErrorRemediationHints as n, runOnce as o, probeQueueOwnerHealth as p, output_exports as r, sendSessionDirect as s, createOutputFormatter as t, installPerfMetricsCapture as u };
4145
+
4146
+ //# sourceMappingURL=output-DPg20dvn.js.map