pi-tian-background-terminals 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/manager.ts ADDED
@@ -0,0 +1,1070 @@
1
+ /**
2
+ * TerminalManager — owns the registry of running/settled background
3
+ * terminals.
4
+ *
5
+ * Each terminal is a raw `node:child_process` spawn (own process group on
6
+ * POSIX, no interactive stdin) whose stdout/stderr 'data' callbacks fold into two
7
+ * bounded OutputBuffers. Closing a terminal's scope kills the whole process
8
+ * tree (SIGTERM → SIGKILL escalation).
9
+ *
10
+ * The manager also exposes a synchronous `TerminalReadModel` so the
11
+ * imperative TUI components (which render synchronously) can read snapshots
12
+ * and issue fire-and-forget kills without touching the Effect runtime.
13
+ */
14
+
15
+ import { spawn, type ChildProcess } from "node:child_process";
16
+ import * as fs from "node:fs";
17
+ import * as os from "node:os";
18
+ import * as path from "node:path";
19
+ import { getShellConfig } from "@earendil-works/pi-coding-agent";
20
+ import {
21
+ Context,
22
+ Deferred,
23
+ Effect,
24
+ Exit,
25
+ FiberSet,
26
+ Layer,
27
+ Scope,
28
+ } from "effect";
29
+ import {
30
+ ConcurrencyLimitError,
31
+ formatExit,
32
+ SpawnError,
33
+ UnknownTerminalError,
34
+ type TerminalSnapshot,
35
+ type TerminalStatus,
36
+ } from "./domain.ts";
37
+ import { OutputBuffer } from "./output.ts";
38
+
39
+ export const MAX_RUNNING = 8;
40
+ export const MAX_TRACKED = 32;
41
+ export const DEFAULT_YIELD_TIME_MS = 10_000;
42
+ export const MIN_YIELD_TIME_MS = 250;
43
+ export const MAX_YIELD_TIME_MS = 30_000;
44
+ /** Same upper bound as Pi's built-in bash timeout (Node timer maximum). */
45
+ export const MAX_RUNTIME_TIMEOUT_MS = 2_147_483_647;
46
+ export const MAX_RUNTIME_TIMEOUT_SECONDS = MAX_RUNTIME_TIMEOUT_MS / 1000;
47
+ const MAX_SETTLED_HISTORY = MAX_TRACKED * 4;
48
+ /** In-memory retained cap per stream. */
49
+ export const RETAINED_PER_STREAM = 2 * 1024 * 1024;
50
+ /** Stable startup prefix within the retained cap; the remainder is rolling tail. */
51
+ export const HEAD_RETAINED_PER_STREAM = 256 * 1024;
52
+ /** Private full-log spills are bounded so a firehose cannot fill the temp disk. */
53
+ export const MAX_SPILL_BYTES_PER_STREAM = 256 * 1024 * 1024;
54
+ const STOP_TIMEOUT_MS = 5_000;
55
+ /** SIGTERM is normally enough; the second deadline covers a wedged process. */
56
+ const FORCE_KILL_AFTER_MS = 2_000;
57
+ /** After termination, how long to wait for the natural close→flush→settle
58
+ * path before force-settling (a grandchild can hold the stdio pipes open). */
59
+ const SETTLE_GRACE_MS = 1_000;
60
+ /** Bound on waiting for spill WriteStreams to flush before settling; a hung
61
+ * filesystem must not leave an exited entry "running" (and kill() waiting).
62
+ * Terminate (≤2.5s) + settle grace (1s) + flush (1.5s) stays inside the 5s
63
+ * scope-close bound, so teardown remains bounded end to end. */
64
+ const SPILL_FLUSH_TIMEOUT_MS = 1_500;
65
+ const ERROR_TEXT_MAX_LENGTH = 4_096;
66
+
67
+ function bounded(text: string) {
68
+ return text.slice(0, ERROR_TEXT_MAX_LENGTH);
69
+ }
70
+
71
+ function boundedError(error: unknown) {
72
+ return bounded(error instanceof Error ? error.message : String(error));
73
+ }
74
+
75
+ // --- Internal state -----------------------------------------------------------
76
+
77
+ /** Mutable snapshot; exposed to readers via the readonly TerminalSnapshot type.
78
+ * stdout/stderr are getters over the live OutputBuffers. */
79
+ interface MutableSnapshot extends TerminalSnapshot {
80
+ status: TerminalStatus;
81
+ pid?: number;
82
+ settledAt?: number;
83
+ exitCode?: number;
84
+ signal?: string;
85
+ errorText?: string;
86
+ }
87
+
88
+ interface Entry {
89
+ snapshot: MutableSnapshot;
90
+ child: ChildProcess;
91
+ scope: Scope.Closeable;
92
+ stdoutBuf: OutputBuffer;
93
+ stderrBuf: OutputBuffer;
94
+ spillStreams: fs.WriteStream[];
95
+ /** Set in the same synchronous effect that sends SIGTERM so a natural exit
96
+ * before signaling keeps its truthful status. */
97
+ killSignaled: boolean;
98
+ /** The child emitted 'error' (spawn failure etc.); settles as "failed".
99
+ * Kept separate from errorText, which also carries non-fatal notes
100
+ * (spill failures) that must not flip a clean exit to "failed". */
101
+ processErrored: boolean;
102
+ /** 'exit' event observed (code/signal recorded). */
103
+ exited: boolean;
104
+ /** 'close' event observed (stdio flushed; the settle trigger). */
105
+ stdioClosed: boolean;
106
+ /** A settle-after-spill-flush is in flight; don't start a second one. */
107
+ settling: boolean;
108
+ /** Hard runtime deadline won before a natural process exit. */
109
+ timedOut: boolean;
110
+ /** Cleared on every settle/disposal path. */
111
+ timeoutHandle?: NodeJS.Timeout;
112
+ /** The shell exited without stdio closing; a bounded scope close is queued
113
+ * to reap descendants that still hold the inherited pipes open. */
114
+ exitCleanupStarted: boolean;
115
+ /** Completed exactly once when the entry settles. Kill callers and the scope
116
+ * finalizer can all await the same result without missing a notification. */
117
+ settled: Deferred.Deferred<void>;
118
+ }
119
+
120
+ export interface StartOptions {
121
+ readonly command: string;
122
+ readonly title: string;
123
+ readonly cwd: string;
124
+ /** Exact script sent to Bash after Pi's configured command prefix. */
125
+ readonly executionCommand?: string;
126
+ /** Pi's configured Bash path, when present. */
127
+ readonly shellPath?: string;
128
+ /** Environment resolved at the tool boundary, including current PI_* state. */
129
+ readonly env?: NodeJS.ProcessEnv;
130
+ /** Optional hard total runtime timeout. The yield wait is independent. */
131
+ readonly timeoutMs?: number;
132
+ }
133
+
134
+ export interface SettlementWaitResult {
135
+ readonly snapshot: TerminalSnapshot;
136
+ /** True when the process settled before this wait yielded. */
137
+ readonly settled: boolean;
138
+ }
139
+
140
+ export interface KillResult {
141
+ readonly id: string;
142
+ readonly title: string;
143
+ readonly status: TerminalStatus;
144
+ /** True when the entry was still running when this kill began. */
145
+ readonly wasRunning: boolean;
146
+ /** True when this call initiated the termination AND the entry settled as
147
+ * killed (a natural exit that won the race reports killed: false). */
148
+ readonly killed: boolean;
149
+ /** Final exit rendering ("exit 0", "SIGTERM", ...) captured at settle time,
150
+ * so reports stay accurate even if the entry is pruned afterwards. */
151
+ readonly exit: string;
152
+ }
153
+
154
+ // --- Read model ----------------------------------------------------------------
155
+
156
+ /** Synchronous bridge for the TUI. Snapshots are live objects; do not mutate. */
157
+ export interface TerminalReadModel {
158
+ list(): ReadonlyArray<TerminalSnapshot>;
159
+ get(id: string): TerminalSnapshot | undefined;
160
+ size(): number;
161
+ /** Any-change notification (widget, /ps list). */
162
+ subscribe(listener: () => void): () => void;
163
+ /** Per-terminal notification (/ps detail view). */
164
+ subscribeTo(id: string, listener: () => void): () => void;
165
+ /** Fire-and-forget kill (dashboard/detail `x`). Not marked consumed: the
166
+ * settle still flows back to the model as a follow-up message. */
167
+ requestKill(id: string): void;
168
+ /**
169
+ * Register the settle hook. `consumed` is true when a manager operation is
170
+ * returning that same final state, so it must not also become a follow-up.
171
+ */
172
+ setOnSettled(
173
+ hook: ((snap: TerminalSnapshot, consumed: boolean) => void) | undefined,
174
+ ): void;
175
+ }
176
+
177
+ // --- Service --------------------------------------------------------------------
178
+
179
+ export interface TerminalManagerShape {
180
+ start(
181
+ options: StartOptions,
182
+ ): Effect.Effect<TerminalSnapshot, SpawnError | ConcurrencyLimitError>;
183
+ waitForSettlement(
184
+ id: string,
185
+ timeoutMs: number,
186
+ ): Effect.Effect<SettlementWaitResult, UnknownTerminalError>;
187
+ status(id: string): Effect.Effect<TerminalSnapshot, UnknownTerminalError>;
188
+ /** Kill running terminals; resolves only after they have settled. */
189
+ kill(ids: ReadonlyArray<string>): Effect.Effect<ReadonlyArray<KillResult>>;
190
+ readonly list: Effect.Effect<ReadonlyArray<TerminalSnapshot>>;
191
+ readonly disposeAll: Effect.Effect<void>;
192
+ readonly view: TerminalReadModel;
193
+ }
194
+
195
+ export class TerminalManager extends Context.Service<
196
+ TerminalManager,
197
+ TerminalManagerShape
198
+ >()("background-terminals/TerminalManager") {}
199
+
200
+ // --- Process helpers ------------------------------------------------------------
201
+
202
+ function shellInvocation(command: string, shellPath?: string) {
203
+ // Match Pi's built-in bash resolution: installations expect Bash syntax on
204
+ // every platform (Git Bash/MSYS/Cygwin on Windows, Bash→sh fallback on
205
+ // POSIX). Legacy WSL bash accepts the script over a one-shot stdin
206
+ // transport; it is closed immediately and never becomes interactive.
207
+ const config = getShellConfig(shellPath);
208
+ const commandFromStdin = config.commandTransport === "stdin";
209
+ return {
210
+ shell: config.shell,
211
+ args: commandFromStdin ? config.args : [...config.args, command],
212
+ commandInput: commandFromStdin ? command : undefined,
213
+ };
214
+ }
215
+
216
+ /** Signal the whole process group on POSIX so descendants (servers a shell
217
+ * command spawned) die with it; a wedged child must not orphan its tree. */
218
+ function killTree(child: ChildProcess, signal: NodeJS.Signals) {
219
+ if (process.platform === "win32" && child.pid) {
220
+ try {
221
+ const killer = spawn(
222
+ "taskkill",
223
+ [
224
+ "/pid",
225
+ String(child.pid),
226
+ "/T",
227
+ ...(signal === "SIGKILL" ? ["/F"] : []),
228
+ ],
229
+ { stdio: "ignore", windowsHide: true },
230
+ );
231
+ killer.once("error", () => {
232
+ try {
233
+ child.kill(signal);
234
+ } catch {
235
+ // Process may already be gone.
236
+ }
237
+ });
238
+ killer.once("exit", (code) => {
239
+ if (code === 0) return;
240
+ try {
241
+ child.kill(signal);
242
+ } catch {
243
+ // Process may already be gone.
244
+ }
245
+ });
246
+ killer.unref();
247
+ return;
248
+ } catch {
249
+ // Fall through to the direct signal when taskkill cannot be launched.
250
+ }
251
+ }
252
+ if (process.platform !== "win32" && child.pid) {
253
+ try {
254
+ process.kill(-child.pid, signal);
255
+ return;
256
+ } catch {
257
+ // Group may already be gone; fall through to the direct signal.
258
+ }
259
+ }
260
+ try {
261
+ child.kill(signal);
262
+ } catch {
263
+ // Process may already be gone.
264
+ }
265
+ }
266
+
267
+ /** Await stdio closure without retaining a listener after interruption. */
268
+ function awaitChildClose(child: ChildProcess, closed: () => boolean) {
269
+ return Effect.callback<void>((resume) => {
270
+ if (closed()) {
271
+ resume(Effect.void);
272
+ return;
273
+ }
274
+ const onClose = () => resume(Effect.void);
275
+ child.once("close", onClose);
276
+ return Effect.sync(() => child.off("close", onClose));
277
+ });
278
+ }
279
+
280
+ /** SIGTERM → deadline → SIGKILL; waits for stdio closure rather than only the
281
+ * shell's exit because descendants can keep the inherited pipes and process
282
+ * group alive after the shell itself is gone. */
283
+ function terminateChild(
284
+ child: ChildProcess,
285
+ closed: () => boolean,
286
+ onSignal: () => void,
287
+ ) {
288
+ return Effect.suspend(() => {
289
+ if (closed()) return Effect.void;
290
+ return Effect.gen(function* () {
291
+ yield* Effect.sync(() => {
292
+ onSignal();
293
+ killTree(child, "SIGTERM");
294
+ });
295
+ yield* awaitChildClose(child, closed).pipe(
296
+ Effect.timeout(FORCE_KILL_AFTER_MS),
297
+ Effect.ignore,
298
+ );
299
+ if (closed()) return;
300
+ yield* Effect.sync(() => killTree(child, "SIGKILL"));
301
+ yield* awaitChildClose(child, closed).pipe(
302
+ Effect.timeout(500),
303
+ Effect.ignore,
304
+ );
305
+ });
306
+ });
307
+ }
308
+
309
+ // --- Implementation --------------------------------------------------------------
310
+
311
+ const makeManager = Effect.gen(function* () {
312
+ // Scoped detached forker for sync contexts (read-model kills, process-event
313
+ // settlement, pruning). Completed fibers remove themselves; manager scope
314
+ // close interrupts any work that outlives the bounded disposeAll wait.
315
+ const cleanupFibers = yield* FiberSet.make();
316
+ const runCleanup = yield* FiberSet.runtime(cleanupFibers)();
317
+
318
+ const entries = new Map<string, Entry>();
319
+ /** Small immutable tombstones preserve truthful kill reports if pruning
320
+ * races the tool boundary after an id was validated. */
321
+ const settledHistory = new Map<
322
+ string,
323
+ Pick<KillResult, "title" | "status" | "exit">
324
+ >();
325
+ /** Internal kill() callers collecting a result (settle → consumed). */
326
+ const killInterest = new Map<string, number>();
327
+ /** Initial bash waits currently entitled to return a settlement. */
328
+ const settlementWaiters = new Map<string, Set<{ consumed: boolean }>>();
329
+ const listeners = new Set<() => void>();
330
+ const idListeners = new Map<string, Set<() => void>>();
331
+ let counter = 0;
332
+ let reserved = 0;
333
+ let disposed = false;
334
+ let spillDir: string | undefined | null;
335
+ let onSettled:
336
+ ((snap: TerminalSnapshot, consumed: boolean) => void) | undefined;
337
+
338
+ const notify = (id?: string) => {
339
+ for (const listener of [...listeners]) {
340
+ try {
341
+ listener();
342
+ } catch {
343
+ // A failed widget/render listener must not corrupt lifecycle state.
344
+ }
345
+ }
346
+ if (id) {
347
+ for (const listener of idListeners.get(id) ?? []) {
348
+ try {
349
+ listener();
350
+ } catch {
351
+ // Same.
352
+ }
353
+ }
354
+ }
355
+ };
356
+
357
+ const runningCount = () =>
358
+ [...entries.values()].filter((e) => e.snapshot.status === "running").length;
359
+
360
+ const addKillInterest = (ids: ReadonlyArray<string>) => {
361
+ for (const id of ids) killInterest.set(id, (killInterest.get(id) ?? 0) + 1);
362
+ };
363
+ const releaseKillInterest = (ids: ReadonlyArray<string>) => {
364
+ for (const id of ids) {
365
+ const count = (killInterest.get(id) ?? 1) - 1;
366
+ if (count <= 0) killInterest.delete(id);
367
+ else killInterest.set(id, count);
368
+ }
369
+ };
370
+
371
+ const closeEntryScope = (entry: Entry) =>
372
+ Scope.close(entry.scope, Exit.void).pipe(Effect.ignore);
373
+
374
+ const pruneSettled = () => {
375
+ if (entries.size <= MAX_TRACKED) return;
376
+ const candidates = [...entries.values()]
377
+ .filter(
378
+ (e) =>
379
+ e.snapshot.status !== "running" && !killInterest.has(e.snapshot.id),
380
+ )
381
+ .sort(
382
+ (a, b) =>
383
+ (a.snapshot.settledAt ?? a.snapshot.createdAt) -
384
+ (b.snapshot.settledAt ?? b.snapshot.createdAt),
385
+ );
386
+ for (const entry of candidates) {
387
+ if (entries.size <= MAX_TRACKED) break;
388
+ entries.delete(entry.snapshot.id);
389
+ runCleanup(closeEntryScope(entry));
390
+ }
391
+ };
392
+
393
+ /** End all spill streams; resolves when their buffers are flushed to disk
394
+ * (bounded), so a settle notification never points at a partial file. */
395
+ const flushSpillStreams = (entry: Entry) => {
396
+ const streams = entry.spillStreams;
397
+ entry.spillStreams = [];
398
+ return Effect.forEach(
399
+ streams,
400
+ (stream) =>
401
+ Effect.callback<void>((resume) => {
402
+ const done = () => resume(Effect.void);
403
+ try {
404
+ stream.end(done);
405
+ } catch {
406
+ // Best effort; tmpdir contents are disposable.
407
+ done();
408
+ }
409
+ }),
410
+ { concurrency: "unbounded", discard: true },
411
+ ).pipe(
412
+ Effect.timeoutOrElse({
413
+ duration: SPILL_FLUSH_TIMEOUT_MS,
414
+ orElse: () =>
415
+ Effect.sync(() => {
416
+ entry.stdoutBuf.spillPath = undefined;
417
+ entry.stderrBuf.spillPath = undefined;
418
+ entry.snapshot.errorText ??=
419
+ "Full-log spill flush timed out; full output may be incomplete";
420
+ }),
421
+ }),
422
+ );
423
+ };
424
+
425
+ /** Single settle path — idempotent; kill vs natural exit vs error races are
426
+ * resolved by whichever lands first (the second call is a no-op). */
427
+ const settle = (entry: Entry) => {
428
+ const s = entry.snapshot;
429
+ if (s.status !== "running") return;
430
+ if (entry.timeoutHandle) {
431
+ clearTimeout(entry.timeoutHandle);
432
+ entry.timeoutHandle = undefined;
433
+ }
434
+ s.settledAt = Date.now();
435
+ s.status = entry.timedOut
436
+ ? "timed_out"
437
+ : entry.killSignaled
438
+ ? "killed"
439
+ : entry.processErrored
440
+ ? "failed"
441
+ : s.exitCode === 0
442
+ ? "done"
443
+ : "failed";
444
+ settledHistory.set(s.id, {
445
+ title: s.title,
446
+ status: s.status,
447
+ exit: formatExit(s),
448
+ });
449
+ while (settledHistory.size > MAX_SETTLED_HISTORY) {
450
+ const oldest = settledHistory.keys().next().value;
451
+ if (oldest === undefined) break;
452
+ settledHistory.delete(oldest);
453
+ }
454
+ // Mark bash waiters before completing the Deferred: whichever side of
455
+ // the yield/settle race wins owns the result without a duplicate follow-up.
456
+ const waiters = settlementWaiters.get(s.id);
457
+ for (const waiter of waiters ?? []) waiter.consumed = true;
458
+ const consumed =
459
+ (killInterest.get(s.id) ?? 0) > 0 || (waiters?.size ?? 0) > 0;
460
+ Deferred.doneUnsafe(entry.settled, Effect.void);
461
+ notify(s.id);
462
+ try {
463
+ // During teardown, don't queue results into a shutting-down session.
464
+ if (!disposed) onSettled?.(s, consumed);
465
+ } catch {
466
+ // The parent session may be unavailable; settlement stays final.
467
+ }
468
+ pruneSettled();
469
+ };
470
+
471
+ /** Flush the spill files, then settle: the completion follow-up (and the
472
+ * kill() resolution) reference the spill path, so the full capture must be
473
+ * on disk before anyone is told about it. Idempotent via `settling`. */
474
+ const settleAfterFlush = (entry: Entry) => {
475
+ if (entry.settling || entry.snapshot.status !== "running") return;
476
+ entry.settling = true;
477
+ runCleanup(
478
+ flushSpillStreams(entry).pipe(
479
+ Effect.andThen(Effect.sync(() => settle(entry))),
480
+ ),
481
+ );
482
+ };
483
+
484
+ const scheduleExitCleanup = (entry: Entry) => {
485
+ if (entry.exitCleanupStarted) return;
486
+ entry.exitCleanupStarted = true;
487
+ runCleanup(
488
+ Effect.sleep(SETTLE_GRACE_MS).pipe(
489
+ Effect.andThen(
490
+ Effect.suspend(() =>
491
+ entry.snapshot.status === "running" && !entry.stdioClosed
492
+ ? closeEntryScope(entry).pipe(
493
+ Effect.timeout(STOP_TIMEOUT_MS),
494
+ Effect.ignore,
495
+ )
496
+ : Effect.void,
497
+ ),
498
+ ),
499
+ ),
500
+ );
501
+ };
502
+
503
+ const resolveSpillDir = () => {
504
+ if (spillDir !== undefined) return spillDir ?? undefined;
505
+ try {
506
+ const base = path.join(os.tmpdir(), "pi-background-terminals");
507
+ fs.mkdirSync(base, { recursive: true, mode: 0o700 });
508
+ fs.chmodSync(base, 0o700);
509
+ spillDir = fs.mkdtempSync(path.join(base, "session-"));
510
+ fs.chmodSync(spillDir, 0o700);
511
+ } catch {
512
+ spillDir = null;
513
+ }
514
+ return spillDir ?? undefined;
515
+ };
516
+
517
+ const makeSpill = (
518
+ entry: () => Entry | undefined,
519
+ id: string,
520
+ stream: "stdout" | "stderr",
521
+ resumeSource: () => void,
522
+ ) => {
523
+ const dir = resolveSpillDir();
524
+ if (!dir) return undefined;
525
+ const spillPath = path.join(dir, `${id}.${stream}.log`);
526
+ try {
527
+ const file = fs.createWriteStream(spillPath, {
528
+ flags: "a",
529
+ mode: 0o600,
530
+ });
531
+ let broken = false;
532
+ let capped = false;
533
+ let writtenBytes = 0;
534
+ file.on("error", (error) => {
535
+ broken = true;
536
+ resumeSource();
537
+ const current = entry();
538
+ if (current) {
539
+ const buf =
540
+ stream === "stdout" ? current.stdoutBuf : current.stderrBuf;
541
+ buf.spillPath = undefined;
542
+ current.snapshot.errorText ??= bounded(
543
+ `Full-log spill to ${spillPath} failed: ${boundedError(error)}`,
544
+ );
545
+ }
546
+ });
547
+ return {
548
+ spillPath,
549
+ file,
550
+ write: (chunk: string) => {
551
+ // writableEnded guard: late 'data' after the settle flush must not
552
+ // error the ended stream (and falsely report the spill as broken).
553
+ if (broken || capped || file.writableEnded) return true;
554
+ const chunkBytes = Buffer.byteLength(chunk, "utf8");
555
+ if (writtenBytes + chunkBytes > MAX_SPILL_BYTES_PER_STREAM) {
556
+ capped = true;
557
+ const current = entry();
558
+ if (current) {
559
+ const buf =
560
+ stream === "stdout" ? current.stdoutBuf : current.stderrBuf;
561
+ buf.spillPath = undefined;
562
+ current.snapshot.errorText ??= bounded(
563
+ `${stream} full-log spill reached the ${MAX_SPILL_BYTES_PER_STREAM}-byte safety limit`,
564
+ );
565
+ }
566
+ return true;
567
+ }
568
+ writtenBytes += chunkBytes;
569
+ const accepted = file.write(chunk);
570
+ if (!accepted) file.once("drain", resumeSource);
571
+ return accepted;
572
+ },
573
+ };
574
+ } catch {
575
+ return undefined;
576
+ }
577
+ };
578
+
579
+ const start = (options: StartOptions) =>
580
+ Effect.gen(function* () {
581
+ // Reserve synchronously (before the first yield inside doStart) so
582
+ // parallel tool calls cannot race past the cap.
583
+ yield* Effect.suspend(
584
+ (): Effect.Effect<void, SpawnError | ConcurrencyLimitError> => {
585
+ if (disposed) {
586
+ return new SpawnError({
587
+ message: "Background terminal manager is shutting down.",
588
+ fallbackSafe: false,
589
+ });
590
+ }
591
+ if (runningCount() + reserved >= MAX_RUNNING) {
592
+ return new ConcurrencyLimitError({
593
+ message: `Max ${MAX_RUNNING} background terminals can run concurrently. Stop one from /ps before starting another.`,
594
+ });
595
+ }
596
+ reserved++;
597
+ return Effect.void;
598
+ },
599
+ );
600
+
601
+ const doStart = Effect.gen(function* () {
602
+ const invocation = yield* Effect.try({
603
+ try: () =>
604
+ shellInvocation(
605
+ options.executionCommand ?? options.command,
606
+ options.shellPath,
607
+ ),
608
+ catch: (error) =>
609
+ new SpawnError({
610
+ message: boundedError(error),
611
+ fallbackSafe: true,
612
+ }),
613
+ });
614
+ const child = yield* Effect.try({
615
+ try: () => {
616
+ const spawned = spawn(invocation.shell, invocation.args, {
617
+ cwd: options.cwd,
618
+ env: options.env ?? process.env,
619
+ // No interactive stdin. The sole exception is legacy WSL Bash's
620
+ // one-shot script transport, which is closed immediately below.
621
+ stdio: [
622
+ invocation.commandInput === undefined ? "ignore" : "pipe",
623
+ "pipe",
624
+ "pipe",
625
+ ],
626
+ // Own process group on POSIX → group kill takes the whole tree.
627
+ detached: process.platform !== "win32",
628
+ windowsHide: true,
629
+ });
630
+ if (invocation.commandInput !== undefined) {
631
+ spawned.stdin?.on("error", () => {});
632
+ spawned.stdin?.end(invocation.commandInput);
633
+ }
634
+ return spawned;
635
+ },
636
+ catch: (error) =>
637
+ new SpawnError({
638
+ message: boundedError(error),
639
+ fallbackSafe: true,
640
+ }),
641
+ });
642
+
643
+ const id = `bt-${++counter}`;
644
+ const entryRef = () => entries.get(id);
645
+ const stdoutSpill = makeSpill(entryRef, id, "stdout", () =>
646
+ child.stdout?.resume(),
647
+ );
648
+ const stderrSpill = makeSpill(entryRef, id, "stderr", () =>
649
+ child.stderr?.resume(),
650
+ );
651
+ const stdoutBuf = new OutputBuffer(
652
+ RETAINED_PER_STREAM,
653
+ stdoutSpill?.write,
654
+ HEAD_RETAINED_PER_STREAM,
655
+ );
656
+ const stderrBuf = new OutputBuffer(
657
+ RETAINED_PER_STREAM,
658
+ stderrSpill?.write,
659
+ HEAD_RETAINED_PER_STREAM,
660
+ );
661
+ stdoutBuf.spillPath = stdoutSpill?.spillPath;
662
+ stderrBuf.spillPath = stderrSpill?.spillPath;
663
+
664
+ const snapshot: MutableSnapshot = {
665
+ id,
666
+ command: options.command,
667
+ title: options.title,
668
+ cwd: options.cwd,
669
+ pid: child.pid,
670
+ status: "running",
671
+ createdAt: Date.now(),
672
+ timeoutMs: options.timeoutMs,
673
+ get stdout() {
674
+ return stdoutBuf.view();
675
+ },
676
+ get stderr() {
677
+ return stderrBuf.view();
678
+ },
679
+ };
680
+
681
+ const scope = yield* Scope.make();
682
+ const settled = yield* Deferred.make<void>();
683
+ const entry: Entry = {
684
+ snapshot,
685
+ child,
686
+ scope,
687
+ stdoutBuf,
688
+ stderrBuf,
689
+ spillStreams: [stdoutSpill?.file, stderrSpill?.file].filter(
690
+ (file): file is fs.WriteStream => file !== undefined,
691
+ ),
692
+ killSignaled: false,
693
+ processErrored: false,
694
+ exited: false,
695
+ stdioClosed: false,
696
+ settling: false,
697
+ timedOut: false,
698
+ exitCleanupStarted: false,
699
+ settled,
700
+ };
701
+
702
+ // Plain-callback stream plumbing (the codex-backend precedent):
703
+ // setEncoding's internal StringDecoder is multibyte-safe across
704
+ // chunk boundaries.
705
+ child.stdout?.setEncoding("utf8");
706
+ child.stdout?.on("data", (chunk: string) => {
707
+ if (!stdoutBuf.push(chunk)) child.stdout?.pause();
708
+ notify(id);
709
+ });
710
+ child.stderr?.setEncoding("utf8");
711
+ child.stderr?.on("data", (chunk: string) => {
712
+ if (!stderrBuf.push(chunk)) child.stderr?.pause();
713
+ notify(id);
714
+ });
715
+ // Spawn failures (ENOENT etc.) arrive via 'error', not a throw. Node
716
+ // still emits 'close' afterwards (with a bogus errno as code), so
717
+ // record the failure here and let the close path do the one settle.
718
+ child.once("error", (error) => {
719
+ entry.processErrored = true;
720
+ snapshot.errorText ??= boundedError(error);
721
+ entry.exited = true;
722
+ settleAfterFlush(entry);
723
+ });
724
+ // Record code/signal on 'exit'; settle on 'close' so the completion
725
+ // notification always carries the final flushed output.
726
+ child.once("exit", (code, signal) => {
727
+ entry.exited = true;
728
+ snapshot.exitCode = code ?? undefined;
729
+ snapshot.signal = signal ?? undefined;
730
+ // A descendant can keep the pipes open after the shell exits. Give
731
+ // close a short natural grace, then close the scope to terminate
732
+ // the surviving process group and force a bounded settlement.
733
+ scheduleExitCleanup(entry);
734
+ });
735
+ child.once("close", (code, signal) => {
736
+ entry.exited = true;
737
+ entry.stdioClosed = true;
738
+ // Only trust close's code/signal when 'exit' never fired (a spawn
739
+ // 'error' close reports the errno, e.g. -2, as its code).
740
+ if (!entry.processErrored) {
741
+ snapshot.exitCode ??= code ?? undefined;
742
+ snapshot.signal ??= signal ?? undefined;
743
+ }
744
+ settleAfterFlush(entry);
745
+ });
746
+
747
+ // One teardown path: kill(), requestKill, pruning, disposeAll, and
748
+ // runtime.dispose() all converge on closing this scope.
749
+ yield* Scope.provide(
750
+ Effect.addFinalizer(() =>
751
+ Effect.gen(function* () {
752
+ // Only claim "killed" when we are actually about to signal a
753
+ // live process; a natural exit that already happened (still
754
+ // waiting on 'close') keeps its truthful done/failed status.
755
+ yield* terminateChild(
756
+ child,
757
+ () => entry.stdioClosed,
758
+ () => {
759
+ entry.killSignaled ||=
760
+ !entry.exited && entry.snapshot.status === "running";
761
+ },
762
+ );
763
+ // Give the natural close→flush→settle path a bounded grace,
764
+ // then force the settle: a grandchild holding the pipe open
765
+ // (detached into a new group) must not leave the entry
766
+ // "running" forever.
767
+ if (entry.snapshot.status === "running") {
768
+ yield* Deferred.await(entry.settled).pipe(
769
+ Effect.timeout(SETTLE_GRACE_MS),
770
+ Effect.ignore,
771
+ );
772
+ }
773
+ if (entry.snapshot.status === "running" && !entry.settling) {
774
+ // Force the settle ourselves. When `settling` is set, the
775
+ // close path's flush→settle is already in flight (bounded by
776
+ // SPILL_FLUSH_TIMEOUT_MS) — settling here first would cite a
777
+ // spill file that is still being flushed.
778
+ if (!entry.stdioClosed) {
779
+ entry.snapshot.errorText ??=
780
+ "stdio did not close after termination; output may be incomplete";
781
+ }
782
+ entry.settling = true;
783
+ yield* flushSpillStreams(entry);
784
+ settle(entry);
785
+ }
786
+ }),
787
+ ),
788
+ scope,
789
+ );
790
+
791
+ // disposeAll may have swept the entries map while we were setting up;
792
+ // an entry added after the sweep would never be torn down. Close our
793
+ // own scope (kills the child) and fail instead (subagents precedent).
794
+ if (disposed) {
795
+ yield* closeEntryScope(entry);
796
+ return yield* new SpawnError({
797
+ message: "Background terminal manager shut down while starting.",
798
+ fallbackSafe: false,
799
+ });
800
+ }
801
+ entries.set(id, entry);
802
+ if (options.timeoutMs !== undefined) {
803
+ entry.timeoutHandle = setTimeout(() => {
804
+ if (entry.snapshot.status !== "running") return;
805
+ // Preserve a natural exit that already won but whose descendants
806
+ // still hold stdout/stderr open; the timeout may reap that tree,
807
+ // but must not rewrite the truthful final status.
808
+ entry.timedOut ||= !entry.exited;
809
+ if (entry.timedOut) {
810
+ entry.snapshot.errorText ??=
811
+ `Command exceeded its ${options.timeoutMs}-ms runtime timeout`;
812
+ }
813
+ runCleanup(
814
+ closeEntryScope(entry).pipe(
815
+ Effect.timeout(STOP_TIMEOUT_MS),
816
+ Effect.ignore,
817
+ ),
818
+ );
819
+ }, options.timeoutMs);
820
+ entry.timeoutHandle.unref();
821
+ }
822
+ notify(id);
823
+ return snapshot as TerminalSnapshot;
824
+ });
825
+
826
+ // Uninterruptible: between spawn() and entries.set there must be no
827
+ // window where an interrupt (tool abort, runtime dispose) leaves a
828
+ // live child that no scope/registry knows about. All steps are sync.
829
+ return yield* doStart.pipe(
830
+ Effect.uninterruptible,
831
+ Effect.ensuring(
832
+ Effect.sync(() => {
833
+ reserved--;
834
+ notify();
835
+ }),
836
+ ),
837
+ );
838
+ });
839
+
840
+ const waitForSettlement = (id: string, timeoutMs: number) =>
841
+ Effect.suspend(
842
+ (): Effect.Effect<SettlementWaitResult, UnknownTerminalError> => {
843
+ const entry = entries.get(id);
844
+ if (!entry) {
845
+ const known = [...entries.keys()];
846
+ return new UnknownTerminalError({
847
+ message: `Unknown terminal id "${id}". Known: ${known.join(", ") || "none"}.`,
848
+ });
849
+ }
850
+ if (entry.snapshot.status !== "running") {
851
+ return Effect.succeed({
852
+ snapshot: entry.snapshot as TerminalSnapshot,
853
+ settled: true,
854
+ });
855
+ }
856
+
857
+ const waiter = { consumed: false };
858
+ const removeWaiter = () => {
859
+ const current = settlementWaiters.get(id);
860
+ current?.delete(waiter);
861
+ if (current?.size === 0) settlementWaiters.delete(id);
862
+ };
863
+ const finish = Effect.sync((): SettlementWaitResult => {
864
+ // This synchronous removal linearizes timeout vs settle: a later
865
+ // settle sees no waiter and is therefore delivered as a follow-up.
866
+ removeWaiter();
867
+ return {
868
+ snapshot: entry.snapshot as TerminalSnapshot,
869
+ settled:
870
+ waiter.consumed || entry.snapshot.status !== "running",
871
+ };
872
+ });
873
+ const waitMs = Math.max(
874
+ MIN_YIELD_TIME_MS,
875
+ Math.min(MAX_YIELD_TIME_MS, timeoutMs),
876
+ );
877
+
878
+ return Effect.sync(() => {
879
+ let waiters = settlementWaiters.get(id);
880
+ if (!waiters) {
881
+ waiters = new Set();
882
+ settlementWaiters.set(id, waiters);
883
+ }
884
+ waiters.add(waiter);
885
+ }).pipe(
886
+ Effect.andThen(
887
+ Effect.raceFirst(
888
+ Deferred.await(entry.settled),
889
+ Effect.sleep(waitMs),
890
+ ).pipe(Effect.andThen(finish)),
891
+ ),
892
+ Effect.ensuring(Effect.sync(removeWaiter)),
893
+ );
894
+ },
895
+ );
896
+
897
+ const status = (id: string) =>
898
+ Effect.suspend(
899
+ (): Effect.Effect<TerminalSnapshot, UnknownTerminalError> => {
900
+ const entry = entries.get(id);
901
+ if (!entry) {
902
+ const known = [...entries.keys()];
903
+ return new UnknownTerminalError({
904
+ message: `Unknown terminal id "${id}". Known: ${known.join(", ") || "none"}.`,
905
+ });
906
+ }
907
+ return Effect.succeed(entry.snapshot as TerminalSnapshot);
908
+ },
909
+ );
910
+
911
+ /** Kill one running entry: close the scope — whose finalizer marks the kill
912
+ * at the signal point, terminates the tree, and force-settles —
913
+ * in a DETACHED fiber. Once the flag is set the termination must actually
914
+ * happen; a tool abort interrupting the caller cannot cancel it (this is
915
+ * what makes "termination continues in the background" truthful). */
916
+ const killEntry = (entry: Entry) =>
917
+ Effect.sync(() => {
918
+ if (entry.snapshot.status !== "running") return;
919
+ runCleanup(
920
+ closeEntryScope(entry).pipe(
921
+ Effect.timeout(STOP_TIMEOUT_MS),
922
+ Effect.ignore,
923
+ ),
924
+ );
925
+ });
926
+
927
+ const kill = (ids: ReadonlyArray<string>) =>
928
+ Effect.suspend(() => {
929
+ const unique = [...new Set(ids)];
930
+ const byId = new Map(
931
+ unique
932
+ .map((id) => entries.get(id))
933
+ .filter((entry): entry is Entry => entry !== undefined)
934
+ .map((entry) => [entry.snapshot.id, entry]),
935
+ );
936
+ const running = [...byId.values()].filter(
937
+ (entry) => entry.snapshot.status === "running",
938
+ );
939
+ const runningIds = running.map((entry) => entry.snapshot.id);
940
+ // Mark consumed before signaling so this kill's settlements are not
941
+ // ALSO queued as automatic follow-up messages to the model.
942
+ addKillInterest(runningIds);
943
+ const work = Effect.gen(function* () {
944
+ yield* Effect.forEach(running, killEntry, {
945
+ concurrency: "unbounded",
946
+ });
947
+ // Every caller waits on the entries that were running when its kill
948
+ // began. Deferred completion cannot be missed and supports concurrent
949
+ // overlapping/multi-id kill calls.
950
+ yield* Effect.forEach(
951
+ running,
952
+ (entry) => Deferred.await(entry.settled),
953
+ { concurrency: "unbounded", discard: true },
954
+ );
955
+ // Capture the report BEFORE the ensuring below releases interest and
956
+ // prunes — a just-settled entry must not vanish out from under it.
957
+ return unique.map((id): KillResult => {
958
+ const snapshot = byId.get(id)?.snapshot;
959
+ const history = settledHistory.get(id);
960
+ const status = snapshot?.status ?? history?.status ?? "killed";
961
+ const wasRunning = runningIds.includes(id);
962
+ return {
963
+ id,
964
+ title: snapshot?.title ?? history?.title ?? "?",
965
+ status,
966
+ wasRunning,
967
+ // A natural exit can win the race with our SIGTERM; report what
968
+ // actually happened rather than claiming the kill did it.
969
+ killed: wasRunning && status === "killed",
970
+ exit: snapshot
971
+ ? formatExit(snapshot)
972
+ : (history?.exit ?? "unknown"),
973
+ };
974
+ });
975
+ });
976
+ return work.pipe(
977
+ Effect.ensuring(
978
+ Effect.sync(() => {
979
+ releaseKillInterest(runningIds);
980
+ pruneSettled();
981
+ }),
982
+ ),
983
+ );
984
+ });
985
+
986
+ const disposeAll = Effect.gen(function* () {
987
+ disposed = true;
988
+ const all = [...entries.values()];
989
+ entries.clear();
990
+ for (const entry of all) {
991
+ if (entry.timeoutHandle) {
992
+ clearTimeout(entry.timeoutHandle);
993
+ entry.timeoutHandle = undefined;
994
+ }
995
+ }
996
+ yield* Effect.forEach(
997
+ all,
998
+ (entry) =>
999
+ closeEntryScope(entry).pipe(
1000
+ Effect.timeout(STOP_TIMEOUT_MS),
1001
+ Effect.ignore,
1002
+ ),
1003
+ { concurrency: "unbounded" },
1004
+ );
1005
+ // Detached kill/prune/flush work is scoped to the manager. Wait for it
1006
+ // within the shutdown bound; the FiberSet finalizer interrupts anything
1007
+ // still live when the manager scope closes, so cleanup cannot leak.
1008
+ yield* FiberSet.awaitEmpty(cleanupFibers).pipe(
1009
+ Effect.timeout(STOP_TIMEOUT_MS),
1010
+ Effect.ignore,
1011
+ );
1012
+ yield* Effect.sync(() => {
1013
+ const dir = spillDir;
1014
+ spillDir = null;
1015
+ if (dir) fs.rmSync(dir, { recursive: true, force: true });
1016
+ });
1017
+ yield* Effect.sync(() => notify());
1018
+ });
1019
+
1020
+ const view: TerminalReadModel = {
1021
+ list: () => [...entries.values()].map((entry) => entry.snapshot),
1022
+ get: (id) => entries.get(id)?.snapshot,
1023
+ size: () => entries.size,
1024
+ subscribe: (listener) => {
1025
+ listeners.add(listener);
1026
+ return () => listeners.delete(listener);
1027
+ },
1028
+ subscribeTo: (id, listener) => {
1029
+ let set = idListeners.get(id);
1030
+ if (!set) {
1031
+ set = new Set();
1032
+ idListeners.set(id, set);
1033
+ }
1034
+ set.add(listener);
1035
+ return () => {
1036
+ set.delete(listener);
1037
+ if (set.size === 0) idListeners.delete(id);
1038
+ };
1039
+ },
1040
+ requestKill: (id) => {
1041
+ const entry = entries.get(id);
1042
+ if (!entry) return;
1043
+ // UI-initiated kills are not "consumed": the killed result still flows
1044
+ // back to the model as a follow-up message (subagents precedent).
1045
+ runCleanup(killEntry(entry).pipe(Effect.ignore));
1046
+ },
1047
+ setOnSettled: (hook) => {
1048
+ onSettled = hook;
1049
+ },
1050
+ };
1051
+
1052
+ // Safety net: disposing the ManagedRuntime tears everything down even if
1053
+ // the extension forgot to call disposeAll explicitly.
1054
+ yield* Effect.addFinalizer(() => disposeAll);
1055
+
1056
+ return TerminalManager.of({
1057
+ start,
1058
+ waitForSettlement,
1059
+ status,
1060
+ kill,
1061
+ list: Effect.sync(() => [...entries.values()].map((e) => e.snapshot)),
1062
+ disposeAll,
1063
+ view,
1064
+ });
1065
+ });
1066
+
1067
+ export const TerminalManagerLive: Layer.Layer<TerminalManager> = Layer.effect(
1068
+ TerminalManager,
1069
+ makeManager,
1070
+ );