Repository navigation
Expand file tree
/
Copy pathtest-session-manager.ts
More file actions
717 lines (667 loc) · 25.1 KB
/
Copy pathtest-session-manager.ts
File metadata and controls
717 lines (667 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import assert from "node:assert/strict"
import { EventEmitter, once } from "node:events"
import { setTimeout as delay } from "node:timers/promises"
import { test } from "node:test"
import { spawn, type ChildProcess } from "node:child_process"
import {
buildCliArgs,
DEFAULT_IDLE_PROCESS_TIMEOUT_MS,
deleteActiveProcess,
deleteActiveProcessAndWait,
deleteActiveProcessesForSession,
deleteClaudeSessionId,
describeChildCrash,
ensureProcessExitCleanup,
evictIfNeeded,
getActiveProcess,
getClaudeSessionId,
isIdleProcessEvictionScheduled,
killAllActiveProcesses,
MAX_ACTIVE_PROCESSES,
MAX_CLAUDE_SESSION_ENTRIES,
resolveIdleProcessTimeoutMs,
retainStderr,
scheduleIdleProcessEviction,
noteTurnStarted,
noteTurnLine,
isTurnInFlight,
awaitTurnIdle,
interruptTurn,
setActiveProcess,
setClaudeSessionId,
spawnClaudeProcess,
type ActiveProcess,
} from "./src/session-manager.js"
import { getPendingProxyCalls, queuePendingProxyCall } from "./src/proxy-broker.js"
import {
applyTaskCreateToolResult,
applyTaskCreateToolUse,
getLedger,
} from "./src/todo-ledger.js"
import {
createProxyMcpServer,
DEFAULT_PROXY_TOOLS,
SERVER_CLOSED_MESSAGE,
type ProxyToolCall,
} from "./src/proxy-mcp.js"
function fakeActiveProcess(options: { exitOn: NodeJS.Signals; delayMs: number }): {
activeProcess: ActiveProcess
signals: NodeJS.Signals[]
} {
const proc = new EventEmitter() as ChildProcess
const signals: NodeJS.Signals[] = []
Object.assign(proc, {
exitCode: null,
signalCode: null,
kill(signal: NodeJS.Signals = "SIGTERM") {
signals.push(signal)
if (signal === options.exitOn) {
setTimeout(() => {
Object.defineProperty(proc, "signalCode", {
configurable: true,
value: signal,
})
proc.emit("exit", null, signal)
}, options.delayMs)
}
return true
},
})
return {
activeProcess: {
proc,
lineEmitter: new EventEmitter(),
proxyServer: null,
},
signals,
}
}
test("deleteActiveProcessAndWait waits for the old session owner", async () => {
const key = "wait-for-session-owner"
const { activeProcess, signals } = fakeActiveProcess({
exitOn: "SIGTERM",
delayMs: 25,
})
setActiveProcess(key, activeProcess)
setClaudeSessionId(key, "claude-session")
let settled = false
const pending = deleteActiveProcessAndWait(key, {
exitTimeoutMs: 200,
forceExitTimeoutMs: 100,
}).then((result) => {
settled = true
return result
})
await new Promise((resolve) => setTimeout(resolve, 5))
assert.equal(settled, false)
assert.equal(await pending, true)
assert.deepEqual(signals, ["SIGTERM"])
assert.equal(getActiveProcess(key), undefined)
assert.equal(getClaudeSessionId(key), "claude-session")
deleteClaudeSessionId(key)
})
test("deleteActiveProcessAndWait escalates before reusing a session ID", async () => {
const key = "force-session-owner-exit"
const { activeProcess, signals } = fakeActiveProcess({
exitOn: "SIGKILL",
delayMs: 5,
})
setActiveProcess(key, activeProcess)
assert.equal(
await deleteActiveProcessAndWait(key, {
exitTimeoutMs: 5,
forceExitTimeoutMs: 100,
}),
true,
)
assert.deepEqual(signals, ["SIGTERM", "SIGKILL"])
})
test("buildCliArgs resumes a remembered session with --resume", () => {
const key = "resume-args"
setClaudeSessionId(key, "11111111-1111-4111-8111-111111111111")
try {
const args = buildCliArgs({ sessionKey: key, skipPermissions: true })
assert.equal(
args[args.indexOf("--resume") + 1],
"11111111-1111-4111-8111-111111111111",
)
assert.equal(args.includes("--session-id"), false)
} finally {
deleteClaudeSessionId(key)
}
})
test("buildCliArgs skips --resume while the session owner is alive", () => {
const key = "resume-args-live"
setClaudeSessionId(key, "22222222-2222-4222-8222-222222222222")
const { activeProcess } = fakeActiveProcess({ exitOn: "SIGTERM", delayMs: 0 })
setActiveProcess(key, activeProcess)
try {
const args = buildCliArgs({ sessionKey: key, skipPermissions: true })
assert.equal(args.includes("--resume"), false)
assert.equal(args.includes("--session-id"), false)
} finally {
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
})
test("a resume failure on stderr clears the remembered session ID", async () => {
const key = "resume-error-stderr"
setClaudeSessionId(key, "purged-session")
spawnClaudeProcess(
process.execPath,
[
"-e",
"console.error('No conversation found with session ID: purged-session'); setInterval(() => {}, 1000)",
],
process.cwd(),
key,
)
try {
const deadline = Date.now() + 2000
while (getClaudeSessionId(key) !== undefined && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 10))
}
assert.equal(getClaudeSessionId(key), undefined)
} finally {
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
})
test("an exiting stale process cannot delete its replacement", async () => {
const key = "stale-process-exit"
const first = spawnClaudeProcess(
process.execPath,
["-e", "setInterval(() => {}, 1000)"],
process.cwd(),
key,
)
const replacementProc = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"])
const replacement: ActiveProcess = {
proc: replacementProc,
lineEmitter: new EventEmitter(),
proxyServer: null,
}
try {
setActiveProcess(key, replacement)
first.proc.kill()
await once(first.proc, "exit")
assert.equal(getActiveProcess(key), replacement)
} finally {
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
})
// Idle timeout tests by @bernardofortes (a5f723a).
function fakeIdleProcess(onKill: () => void): ActiveProcess {
return {
proc: {
kill() {
onKill()
return true
},
} as ActiveProcess["proc"],
lineEmitter: new EventEmitter(),
}
}
test("idle process is evicted after the configured timeout", async () => {
const key = `idle-eviction-${Date.now()}`
const sessionId = "f8dccdd4-4785-4bd9-8520-7a5993a71f78"
let kills = 0
setActiveProcess(key, fakeIdleProcess(() => kills++))
setClaudeSessionId(key, sessionId)
scheduleIdleProcessEviction(key, 10)
await delay(30)
assert.equal(kills, 1)
assert.equal(getActiveProcess(key), undefined)
assert.deepEqual(
buildCliArgs({ sessionKey: key, skipPermissions: false }).slice(-2),
["--resume", sessionId],
)
deleteClaudeSessionId(key)
})
test("reusing a process cancels its idle eviction", async () => {
const key = `idle-reuse-${Date.now()}`
let kills = 0
const process = fakeIdleProcess(() => kills++)
setActiveProcess(key, process)
scheduleIdleProcessEviction(key, 10)
assert.equal(getActiveProcess(key), process)
await delay(30)
assert.equal(kills, 0)
assert.equal(getActiveProcess(key), process)
deleteActiveProcess(key)
})
test("idle eviction is off unless set, and an explicit value arms it", () => {
assert.equal(DEFAULT_IDLE_PROCESS_TIMEOUT_MS, 0)
assert.equal(resolveIdleProcessTimeoutMs(undefined), 0)
assert.equal(resolveIdleProcessTimeoutMs(0), 0)
assert.equal(resolveIdleProcessTimeoutMs(900_000), 900_000)
const key = `idle-default-${Date.now()}`
setActiveProcess(key, fakeIdleProcess(() => {}))
try {
scheduleIdleProcessEviction(key, resolveIdleProcessTimeoutMs(undefined))
assert.equal(isIdleProcessEvictionScheduled(key), false, "unset arms nothing")
scheduleIdleProcessEviction(key, resolveIdleProcessTimeoutMs(900_000))
assert.equal(isIdleProcessEvictionScheduled(key), true)
scheduleIdleProcessEviction(key, resolveIdleProcessTimeoutMs(0))
assert.equal(isIdleProcessEvictionScheduled(key), false, "0 disarms")
} finally {
deleteActiveProcess(key)
}
})
// A recovered continuation, an auto-continue or a late tool result can put a
// process back to work after the turn that armed the timer completed.
test("the idle timer spares a process that is mid-turn and re-arms instead", async () => {
const key = `idle-in-flight-${Date.now()}`
let kills = 0
const ap = fakeIdleProcess(() => kills++)
setActiveProcess(key, ap)
try {
scheduleIdleProcessEviction(key, 10)
noteTurnStarted(ap)
await delay(30)
assert.equal(kills, 0, "a busy process is never evicted by the clock")
assert.equal(isIdleProcessEvictionScheduled(key), true, "re-armed for the next window")
noteTurnLine(ap, JSON.stringify({ type: "result", subtype: "success" }))
await delay(30)
assert.equal(kills, 1, "evicted once the turn settled and the window lapsed")
assert.equal(isIdleProcessEvictionScheduled(key), false)
} finally {
deleteActiveProcess(key)
}
})
test("timeouts above Node's maximum delay do not evict immediately", async () => {
const key = `idle-overflow-${Date.now()}`
let kills = 0
const process = fakeIdleProcess(() => kills++)
setActiveProcess(key, process)
scheduleIdleProcessEviction(key, 2_147_483_648)
await delay(10)
assert.equal(kills, 0)
assert.equal(getActiveProcess(key), process)
deleteActiveProcess(key)
})
// Turn lifecycle and abort interrupt (adapted from @broskees' 68ed142).
function fakeTurnProcess(): { ap: ActiveProcess; writes: string[] } {
const writes: string[] = []
const ap: ActiveProcess = {
proc: {
stdin: {
writable: true,
write(chunk: string) {
writes.push(chunk)
return true
},
},
} as unknown as ActiveProcess["proc"],
lineEmitter: new EventEmitter(),
}
return { ap, writes }
}
test("a turn is in flight from the envelope write until the terminal result line", async () => {
const { ap } = fakeTurnProcess()
assert.equal(isTurnInFlight(ap), false)
noteTurnStarted(ap)
assert.equal(isTurnInFlight(ap), true)
noteTurnLine(ap, JSON.stringify({ type: "assistant", message: { content: [{ type: "text", text: "result" }] } }))
assert.equal(isTurnInFlight(ap), true, "a content line that merely mentions result does not settle")
noteTurnLine(ap, "not json \"result\"")
assert.equal(isTurnInFlight(ap), true)
const idle = awaitTurnIdle(ap, 1_000)
noteTurnLine(ap, JSON.stringify({ type: "result", subtype: "success" }))
assert.equal(isTurnInFlight(ap), false)
assert.equal(await idle, true)
})
test("interruptTurn writes an interrupt control request and waits for the result", async () => {
const { ap, writes } = fakeTurnProcess()
assert.equal(await interruptTurn(ap), true, "nothing in flight is already idle, nothing written")
assert.deepEqual(writes, [])
noteTurnStarted(ap)
const pending = interruptTurn(ap, 1_000)
assert.equal(writes.length, 1)
const request = JSON.parse(writes[0]!)
assert.equal(request.type, "control_request")
assert.equal(request.request.subtype, "interrupt")
assert.ok(request.request_id)
noteTurnLine(ap, JSON.stringify({ type: "result", subtype: "error_during_execution", is_error: true }))
assert.equal(await pending, true)
})
test("interruptTurn reports false when the CLI never answers", async () => {
const { ap } = fakeTurnProcess()
noteTurnStarted(ap)
assert.equal(await interruptTurn(ap, 20), false)
assert.equal(isTurnInFlight(ap), true, "still in flight; the next turn's guard will retry")
})
test("the interactive transport is never marked in flight", () => {
const { ap } = fakeTurnProcess()
ap.asideTransport = { cliPath: "claude", interactive: true }
noteTurnStarted(ap)
assert.equal(isTurnInFlight(ap), false)
})
function captureStderr(): { lines: string[]; restore: () => void } {
const lines: string[] = []
const original = console.error
console.error = (line: unknown) => {
lines.push(String(line))
}
return { lines, restore: () => { console.error = original } }
}
// The child's stdin is its own emitter, so `proc.on("error", ...)` does not
// cover it. A write that lands after the child died raises EPIPE there, and
// an 'error' event on a stream with no listener throws: inside opencode's own
// process, not ours. The EPIPE itself is delivered whenever libuv gets around
// to failing the queued write, so the event is emitted here directly; the
// contract under test is that something is listening for it.
test("an error on a dead child's stdin is logged, not thrown", async () => {
const key = `stdin-error-${Date.now()}`
const captured = captureStderr()
const ap = spawnClaudeProcess(
process.execPath,
["-e", "process.stdin.destroy(); setInterval(() => {}, 1000)"],
process.cwd(),
key,
)
const stdin = ap.proc.stdin!
try {
await delay(100)
noteTurnStarted(ap)
// The write a real turn makes. It must not throw synchronously either.
stdin.write(JSON.stringify({ type: "user", pad: "x".repeat(100_000) }) + "\n")
stdin.emit("error", Object.assign(new Error("write EPIPE"), { code: "EPIPE" }))
} finally {
captured.restore()
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
assert.ok(
captured.lines.some((line) => line.includes("claude process stdin error")),
`expected a logged stdin error, got: ${captured.lines.join(" | ")}`,
)
assert.ok(
captured.lines.some((line) => line.includes('"code":"EPIPE"')),
"the logged error should name the errno the write failed with",
)
assert.equal(
isTurnInFlight(ap),
false,
"a write that never reached the CLI leaves no turn to wait for",
)
})
function fillActiveProcesses(prefix: string, killed: string[]): {
keys: string[]
processes: ActiveProcess[]
} {
const keys: string[] = []
const processes: ActiveProcess[] = []
for (let index = 0; index < MAX_ACTIVE_PROCESSES; index++) {
const key = `${prefix}-${index}`
const ap = fakeIdleProcess(() => killed.push(key))
keys.push(key)
processes.push(ap)
setActiveProcess(key, ap)
}
return { keys, processes }
}
// Killing a process mid-turn truncates that answer silently: the close
// handler finishes the stream and the operator sees half a reply.
test("LRU eviction picks the oldest idle process, not the oldest process", () => {
const killed: string[] = []
const { keys, processes } = fillActiveProcesses(`lru-guard-${Date.now()}`, killed)
try {
noteTurnStarted(processes[0]!)
noteTurnStarted(processes[1]!)
evictIfNeeded()
assert.deepEqual(killed, [keys[2]])
assert.equal(getActiveProcess(keys[0]!), processes[0])
assert.equal(getActiveProcess(keys[1]!), processes[1])
assert.equal(getActiveProcess(keys[2]!), undefined)
} finally {
for (const key of keys) deleteActiveProcess(key)
}
})
test("LRU eviction kills nothing while every process is mid-turn", () => {
const killed: string[] = []
const { keys, processes } = fillActiveProcesses(`lru-busy-${Date.now()}`, killed)
const captured = captureStderr()
let killedDuringEviction: string[] = []
try {
for (const ap of processes) noteTurnStarted(ap)
evictIfNeeded()
killedDuringEviction = [...killed]
} finally {
captured.restore()
for (const key of keys) deleteActiveProcess(key)
}
assert.deepEqual(killedDuringEviction, [])
assert.ok(
captured.lines.some((line) => line.includes("every claude process is mid-turn")),
`expected a warning about the skipped eviction, got: ${captured.lines.join(" | ")}`,
)
})
test("the process cap is 16 and the LRU never exceeds it while an idle victim exists", () => {
assert.equal(MAX_ACTIVE_PROCESSES, 16)
})
// A `task` call has no deadline, so once its proxy server is gone nothing
// else would ever reap its broker entry.
test("deleting a process rejects the broker calls its proxy server can no longer answer", async () => {
const key = `detach-rejects-${Date.now()}`
let serverClosed = false
const ap: ActiveProcess = {
...fakeIdleProcess(() => {}),
proxyServer: { async close() { serverClosed = true } } as unknown as ActiveProcess["proxyServer"],
}
setActiveProcess(key, ap)
let rejection: Error | undefined
const settled = new Promise<void>((resolve) => {
queuePendingProxyCall(key, {
id: `call-${key}`,
toolName: "task",
input: {},
resolve: () => resolve(),
reject: (error) => { rejection = error; resolve() },
})
})
assert.equal(getPendingProxyCalls(key).length, 1)
deleteActiveProcess(key)
await settled
assert.equal(serverClosed, true)
assert.equal(getPendingProxyCalls(key).length, 0)
assert.equal(rejection?.message, SERVER_CLOSED_MESSAGE)
})
test("deleteActiveProcessesForSession releases every process and remembered id of one session only", async () => {
const stamp = Date.now()
const keyFor = (session: string, model = "claude-opus-5", scope = "tools") =>
scope === "compaction"
? `/tmp/proj-${stamp}::${model}::compaction::${session}`
: `/tmp/proj-${stamp}::${model}::${scope}::${session}::context=["claude-code",null]`
const killed: string[] = []
const register = (key: string, opencodeSessionID?: string) => {
const { activeProcess } = fakeActiveProcess({ exitOn: "SIGTERM", delayMs: 0 })
activeProcess.proc.kill = ((signal?: NodeJS.Signals) => {
killed.push(key)
Object.defineProperty(activeProcess.proc, "exitCode", { configurable: true, value: 0 })
activeProcess.proc.emit("exit", 0, signal ?? null)
return true
}) as typeof activeProcess.proc.kill
if (opencodeSessionID) activeProcess.opencodeSessionID = opencodeSessionID
setActiveProcess(key, activeProcess)
}
const a1 = keyFor("ses_A")
const a2 = keyFor("ses_A", "claude-haiku-4-5", "compaction")
const aEffort = `${keyFor("ses_A")}::effort=high`
const b = keyFor("ses_B")
const shared = keyFor("default")
register(a1, "ses_A")
register(a2)
register(aEffort, "ses_A")
register(b, "ses_B")
register(shared)
setClaudeSessionId(a1, "claude-a1")
setClaudeSessionId(b, "claude-b")
// An idle-evicted process keeps its session id for a resume; a deleted
// session must drop that too.
const aEvicted = keyFor("ses_A", "claude-sonnet-5")
setClaudeSessionId(aEvicted, "claude-a-evicted")
try {
assert.deepEqual(deleteActiveProcessesForSession("default"), [], "the shared bucket is never matched")
assert.deepEqual(deleteActiveProcessesForSession(""), [])
const released = deleteActiveProcessesForSession("ses_A")
assert.deepEqual(released.sort(), [a1, a2, aEffort, aEvicted].sort())
assert.deepEqual(killed.sort(), [a1, a2, aEffort].sort())
assert.equal(getActiveProcess(a1), undefined)
assert.equal(getActiveProcess(a2), undefined)
assert.equal(getActiveProcess(aEffort), undefined)
assert.ok(getActiveProcess(b), "another session's process survives")
assert.ok(getActiveProcess(shared), "the shared bucket survives")
assert.equal(getClaudeSessionId(a1), undefined)
assert.equal(getClaudeSessionId(aEvicted), undefined)
assert.equal(getClaudeSessionId(b), "claude-b")
assert.deepEqual(deleteActiveProcessesForSession("ses_A"), [], "idempotent")
} finally {
for (const key of [a1, a2, aEffort, b, shared, aEvicted]) {
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
}
})
test("killAllActiveProcesses is synchronous, releases parked calls on both sides, and the exit hook is armed once", async () => {
const stamp = Date.now()
const killed: string[] = []
const keys = [`exit-a-${stamp}`, `exit-b-${stamp}`]
for (const key of keys) setActiveProcess(key, fakeIdleProcess(() => killed.push(key)))
setClaudeSessionId(keys[0]!, "claude-exit-a")
// A real proxy server holding a real `task` request, wired to the broker
// the way the language model wires it. opencode going away must release
// the HTTP side and the broker entry, not just kill the child.
const server = await createProxyMcpServer(DEFAULT_PROXY_TOOLS.filter((t) => t.name === "task"))
server.calls.on("call", (call: ProxyToolCall) => queuePendingProxyCall(keys[1]!, call))
const parked: ActiveProcess = { ...fakeIdleProcess(() => killed.push(keys[1]!)), proxyServer: server }
setActiveProcess(keys[1]!, parked)
const queued = new Promise<void>((resolve) => server.calls.once("call", () => resolve()))
const request = fetch(server.url, {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${server.authToken}` },
body: JSON.stringify({
jsonrpc: "2.0", id: "parked", method: "tools/call",
params: { name: "task", arguments: { description: "d", prompt: "p", subagent_type: "general" } },
}),
}).then((response) => response.json() as Promise<any>)
await queued
assert.deepEqual(server.pendingCallIds().length, 1)
assert.equal(getPendingProxyCalls(keys[1]!).length, 1)
try {
assert.deepEqual(killAllActiveProcesses().sort(), keys.sort())
assert.deepEqual(killed.sort(), keys.sort(), "killed before the call returned")
assert.equal(getPendingProxyCalls(keys[1]!).length, 0, "broker entry released synchronously")
const answer = await request
assert.equal(answer.result.isError, true)
assert.equal(answer.result.content[0].text, SERVER_CLOSED_MESSAGE)
assert.deepEqual(server.pendingCallIds(), [], "HTTP entry released")
assert.equal(getActiveProcess(keys[0]!), undefined)
assert.equal(getClaudeSessionId(keys[0]!), "claude-exit-a", "ids are left alone at exit")
assert.deepEqual(killAllActiveProcesses(), [])
const before = process.listenerCount("exit")
const armed = ensureProcessExitCleanup()
const afterFirst = process.listenerCount("exit")
assert.equal(ensureProcessExitCleanup(), false, "a second call never adds a listener")
assert.equal(process.listenerCount("exit"), afterFirst)
assert.equal(afterFirst - before, armed ? 1 : 0)
} finally {
for (const key of keys) {
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
}
})
test("retained stderr keeps the newest 2 KB", () => {
const ap = fakeIdleProcess(() => {})
retainStderr(ap, "x".repeat(3_000))
retainStderr(ap, "the tail that matters")
assert.equal(ap.lastStderr!.length, 2 * 1024)
assert.ok(ap.lastStderr!.endsWith("the tail that matters"))
})
test("describeChildCrash names the exit code, the signal, and the stderr tail", () => {
const exited = describeChildCrash(3, null, " fatal: out of memory\n")
assert.match(exited, /exited with code 3/)
assert.match(exited, /fatal: out of memory/)
assert.match(describeChildCrash(null, "SIGKILL", undefined), /killed by SIGKILL/)
assert.doesNotMatch(
describeChildCrash(null, "SIGKILL", undefined),
/Last stderr/,
"no stderr, no empty section",
)
assert.match(describeChildCrash(null, null, undefined), /closed its output/)
})
test("a child that dies keeps its stderr for the crash report", async () => {
const key = `crash-stderr-${Date.now()}`
const ap = spawnClaudeProcess(
process.execPath,
[
"-e",
"process.stderr.write('fatal: claude ran out of memory\\n'); setTimeout(() => process.exit(3), 30)",
],
process.cwd(),
key,
)
try {
await once(ap.proc, "exit")
await delay(20)
assert.match(ap.lastStderr ?? "", /fatal: claude ran out of memory/)
const message = describeChildCrash(
ap.proc.exitCode,
ap.proc.signalCode,
ap.lastStderr,
)
assert.match(message, /exited with code 3/)
assert.match(message, /fatal: claude ran out of memory/)
} finally {
deleteActiveProcess(key)
deleteClaudeSessionId(key)
}
})
test("the claude session store is capped, and eviction takes the ledger with it", () => {
const total = MAX_CLAUDE_SESSION_ENTRIES + 10
const keys = Array.from({ length: total }, (_, i) => `cap-session-${i}`)
// The first key's ledger must go when the id does: an orphaned ledger is
// exactly the leak the cap exists to stop.
applyTaskCreateToolUse("cap-claude-0", "tu-1", { subject: "Write tests" })
applyTaskCreateToolResult("cap-claude-0", "tu-1", "Task #1 created")
assert.equal(getLedger("cap-claude-0").length, 1)
try {
for (const [index, key] of keys.entries()) {
setClaudeSessionId(key, `cap-claude-${index}`)
}
// Oldest first. Earlier tests may leave their own idle keys ahead of
// these, which only evicts more of the early ones, never the recent ones.
assert.equal(getClaudeSessionId(keys[0]), undefined)
assert.equal(getClaudeSessionId(keys[5]), undefined)
assert.deepEqual(getLedger("cap-claude-0"), [])
for (const key of keys.slice(-20)) {
assert.ok(getClaudeSessionId(key), `${key} should have survived the cap`)
}
} finally {
for (const key of keys) deleteClaudeSessionId(key)
}
})
test("the claude session cap never takes a key that still has a process", () => {
const busyKey = "cap-session-busy"
const { activeProcess } = fakeActiveProcess({ exitOn: "SIGTERM", delayMs: 0 })
setActiveProcess(busyKey, activeProcess)
setClaudeSessionId(busyKey, "cap-claude-busy")
const keys = Array.from(
{ length: MAX_CLAUDE_SESSION_ENTRIES + 10 },
(_, i) => `cap-session-after-${i}`,
)
try {
for (const [index, key] of keys.entries()) {
setClaudeSessionId(key, `cap-claude-after-${index}`)
}
// It is the oldest key in the map and would be the first to go on age
// alone; the busy check is the only thing keeping it.
assert.equal(getClaudeSessionId(busyKey), "cap-claude-busy")
} finally {
for (const key of keys) deleteClaudeSessionId(key)
deleteActiveProcess(busyKey)
deleteClaudeSessionId(busyKey)
}
})