@asterxsk/kiln 0.1.0 → 0.2.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 (203) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +170 -170
  3. package/agent/AGENTS.md +67 -67
  4. package/agent/README.md +5 -5
  5. package/agent/extensions/AGENTS.md +68 -68
  6. package/agent/extensions/ask-user/index.ts +418 -418
  7. package/agent/extensions/ask-user/package-lock.json +769 -769
  8. package/agent/extensions/ask-user/package.json +19 -19
  9. package/agent/extensions/ask-user/prompt.ts +45 -45
  10. package/agent/extensions/ask-user/tsconfig.json +7 -7
  11. package/agent/extensions/background-terminals/docs/implementation-guide.md +942 -942
  12. package/agent/extensions/background-terminals/index.ts +627 -627
  13. package/agent/extensions/background-terminals/manager.test.ts +735 -735
  14. package/agent/extensions/background-terminals/output.test.ts +109 -109
  15. package/agent/extensions/background-terminals/package-lock.json +769 -769
  16. package/agent/extensions/background-terminals/package.json +17 -17
  17. package/agent/extensions/background-terminals/prompt.test.ts +125 -125
  18. package/agent/extensions/background-terminals/ps.test.ts +82 -82
  19. package/agent/extensions/background-terminals/result-delivery.test.ts +44 -44
  20. package/agent/extensions/background-terminals/src/domain.ts +87 -87
  21. package/agent/extensions/background-terminals/src/manager.ts +907 -907
  22. package/agent/extensions/background-terminals/src/output.ts +84 -84
  23. package/agent/extensions/background-terminals/src/prompt.ts +142 -142
  24. package/agent/extensions/background-terminals/src/result-delivery.ts +27 -27
  25. package/agent/extensions/background-terminals/src/runtime.ts +36 -36
  26. package/agent/extensions/background-terminals/src/ui/output-view.ts +79 -79
  27. package/agent/extensions/background-terminals/src/ui/ps.ts +621 -621
  28. package/agent/extensions/background-terminals/tsconfig.json +7 -7
  29. package/agent/extensions/file-search/index.spec.ts +443 -443
  30. package/agent/extensions/file-search/index.ts +459 -459
  31. package/agent/extensions/file-search/package-lock.json +2253 -2253
  32. package/agent/extensions/file-search/package.json +23 -23
  33. package/agent/extensions/file-search/src/args.ts +122 -122
  34. package/agent/extensions/file-search/src/binaries.ts +422 -422
  35. package/agent/extensions/file-search/src/output.ts +126 -126
  36. package/agent/extensions/file-search/src/process.ts +146 -146
  37. package/agent/extensions/file-search/src/prompt.ts +52 -52
  38. package/agent/extensions/file-search/tsconfig.json +7 -7
  39. package/agent/extensions/modelconf/PLAN.md +915 -915
  40. package/agent/extensions/modelconf/index.ts +296 -296
  41. package/agent/extensions/modelconf/src/ui/ModelConfView.ts +1101 -1101
  42. package/agent/extensions/pi-web-access/CHANGELOG.md +690 -690
  43. package/agent/extensions/pi-web-access/LICENSE +21 -21
  44. package/agent/extensions/pi-web-access/README.md +470 -470
  45. package/agent/extensions/pi-web-access/SECURITY.md +5 -5
  46. package/agent/extensions/pi-web-access/activity.ts +101 -101
  47. package/agent/extensions/pi-web-access/auth-fetch.ts +148 -148
  48. package/agent/extensions/pi-web-access/brightdata-unlocker.ts +272 -272
  49. package/agent/extensions/pi-web-access/chrome-cookies.ts +669 -669
  50. package/agent/extensions/pi-web-access/content-find.ts +139 -139
  51. package/agent/extensions/pi-web-access/credential-source.ts +191 -191
  52. package/agent/extensions/pi-web-access/data-uri-sanitize.ts +406 -406
  53. package/agent/extensions/pi-web-access/datalab-pdf-extract.ts +568 -568
  54. package/agent/extensions/pi-web-access/declared-web-links.ts +173 -173
  55. package/agent/extensions/pi-web-access/evidence/CONTRACT-EVIDENCE.md +496 -496
  56. package/agent/extensions/pi-web-access/evidence/contract-probe.mjs +140 -140
  57. package/agent/extensions/pi-web-access/exa.ts +526 -526
  58. package/agent/extensions/pi-web-access/extract.ts +1196 -1196
  59. package/agent/extensions/pi-web-access/feature-config.ts +29 -29
  60. package/agent/extensions/pi-web-access/fetch-params.ts +111 -111
  61. package/agent/extensions/pi-web-access/gemini-adc.ts +298 -298
  62. package/agent/extensions/pi-web-access/gemini-api.ts +353 -353
  63. package/agent/extensions/pi-web-access/gemini-pdf-extract.ts +108 -108
  64. package/agent/extensions/pi-web-access/gemini-url-context.ts +128 -128
  65. package/agent/extensions/pi-web-access/gemini-web-config.ts +101 -101
  66. package/agent/extensions/pi-web-access/gemini-web.ts +487 -487
  67. package/agent/extensions/pi-web-access/github-api.ts +197 -197
  68. package/agent/extensions/pi-web-access/github-extract.ts +746 -746
  69. package/agent/extensions/pi-web-access/github-issue-pr.ts +700 -700
  70. package/agent/extensions/pi-web-access/index.ts +1737 -1737
  71. package/agent/extensions/pi-web-access/package-lock.json +5808 -5808
  72. package/agent/extensions/pi-web-access/package.json +64 -64
  73. package/agent/extensions/pi-web-access/page-query.ts +96 -96
  74. package/agent/extensions/pi-web-access/pdf-extract.ts +409 -409
  75. package/agent/extensions/pi-web-access/promise-try.d.ts +7 -7
  76. package/agent/extensions/pi-web-access/query-rewrite.ts +51 -51
  77. package/agent/extensions/pi-web-access/render-search-error.ts +170 -170
  78. package/agent/extensions/pi-web-access/rsc-extract.ts +338 -338
  79. package/agent/extensions/pi-web-access/source-check.ts +282 -282
  80. package/agent/extensions/pi-web-access/ssrf-protection.ts +526 -526
  81. package/agent/extensions/pi-web-access/storage.ts +521 -521
  82. package/agent/extensions/pi-web-access/summary-model-scope.ts +125 -125
  83. package/agent/extensions/pi-web-access/test/auth-fetch.test.mjs +208 -208
  84. package/agent/extensions/pi-web-access/test/brightdata-unlocker.test.mjs +840 -840
  85. package/agent/extensions/pi-web-access/test/chrome-cookie-extraction.test.mjs +441 -441
  86. package/agent/extensions/pi-web-access/test/config-path.test.mjs +283 -283
  87. package/agent/extensions/pi-web-access/test/content-find.test.mjs +25 -25
  88. package/agent/extensions/pi-web-access/test/credential-source.test.mjs +118 -118
  89. package/agent/extensions/pi-web-access/test/data-uri-sanitize.test.mjs +210 -210
  90. package/agent/extensions/pi-web-access/test/datalab-pdf-extract.test.mjs +552 -552
  91. package/agent/extensions/pi-web-access/test/declared-web-links.test.mjs +212 -212
  92. package/agent/extensions/pi-web-access/test/fetch-answer-storage.test.mjs +40 -40
  93. package/agent/extensions/pi-web-access/test/fetch-cache-storage.test.mjs +334 -334
  94. package/agent/extensions/pi-web-access/test/fetch-content-domain-policy.test.mjs +95 -95
  95. package/agent/extensions/pi-web-access/test/fetch-modes.test.mjs +53 -53
  96. package/agent/extensions/pi-web-access/test/fetch-not-found-guidance.test.mjs +92 -92
  97. package/agent/extensions/pi-web-access/test/fetch-params.test.mjs +86 -86
  98. package/agent/extensions/pi-web-access/test/fetch-render-call.test.mjs +34 -34
  99. package/agent/extensions/pi-web-access/test/fetch-routing.test.mjs +173 -173
  100. package/agent/extensions/pi-web-access/test/gemini-adc-auth.test.mjs +257 -257
  101. package/agent/extensions/pi-web-access/test/gemini-api-transport.test.mjs +170 -170
  102. package/agent/extensions/pi-web-access/test/gemini-pdf-extract.test.mjs +133 -133
  103. package/agent/extensions/pi-web-access/test/gemini-web-cookie-opt-in.test.mjs +178 -178
  104. package/agent/extensions/pi-web-access/test/gemini-web-header-overflow.test.mjs +148 -148
  105. package/agent/extensions/pi-web-access/test/get-search-content.test.mjs +223 -223
  106. package/agent/extensions/pi-web-access/test/github-extract.test.mjs +378 -378
  107. package/agent/extensions/pi-web-access/test/github-issue-pr.test.mjs +565 -565
  108. package/agent/extensions/pi-web-access/test/inline-content-config.test.mjs +99 -99
  109. package/agent/extensions/pi-web-access/test/lazy-extract-load.test.mjs +118 -118
  110. package/agent/extensions/pi-web-access/test/local-video-oversize.test.mjs +52 -52
  111. package/agent/extensions/pi-web-access/test/package-typebox-dependency.test.mjs +50 -50
  112. package/agent/extensions/pi-web-access/test/page-query.test.mjs +51 -51
  113. package/agent/extensions/pi-web-access/test/pdf-config.test.mjs +140 -140
  114. package/agent/extensions/pi-web-access/test/pdf-extract.test.mjs +500 -500
  115. package/agent/extensions/pi-web-access/test/proxy-transport.test.mjs +286 -286
  116. package/agent/extensions/pi-web-access/test/query-rewrite.test.mjs +52 -52
  117. package/agent/extensions/pi-web-access/test/rsc-fallback.test.mjs +102 -102
  118. package/agent/extensions/pi-web-access/test/search-error-render.test.mjs +152 -152
  119. package/agent/extensions/pi-web-access/test/search-providers.test.mjs +274 -274
  120. package/agent/extensions/pi-web-access/test/source-check.test.mjs +179 -179
  121. package/agent/extensions/pi-web-access/test/ssrf-allow-ranges-config.test.mjs +205 -205
  122. package/agent/extensions/pi-web-access/test/ssrf-protection.test.mjs +456 -456
  123. package/agent/extensions/pi-web-access/test/tool-registration-config.test.mjs +182 -182
  124. package/agent/extensions/pi-web-access/test/youtube-extract-errors.test.mjs +64 -64
  125. package/agent/extensions/pi-web-access/tsconfig.json +11 -11
  126. package/agent/extensions/pi-web-access/utils.ts +451 -451
  127. package/agent/extensions/pi-web-access/video-extract.ts +392 -392
  128. package/agent/extensions/pi-web-access/youtube-extract.ts +328 -328
  129. package/agent/extensions/shared/activity-status.ts +31 -31
  130. package/agent/extensions/shared/child-session.test.ts +270 -270
  131. package/agent/extensions/shared/child-session.ts +148 -148
  132. package/agent/extensions/shared/context-utilization.test.ts +48 -48
  133. package/agent/extensions/shared/context-utilization.ts +47 -47
  134. package/agent/extensions/shared/dashboard-state.ts +99 -99
  135. package/agent/extensions/shared/tool-call-timeout.test.ts +117 -117
  136. package/agent/extensions/shared/tool-call-timeout.ts +104 -104
  137. package/agent/extensions/subagents/by-the-way.test.ts +29 -29
  138. package/agent/extensions/subagents/claude.test.ts +119 -119
  139. package/agent/extensions/subagents/codex.test.ts +102 -102
  140. package/agent/extensions/subagents/context-usage.test.ts +107 -107
  141. package/agent/extensions/subagents/docs/design-plan.md +568 -568
  142. package/agent/extensions/subagents/docs/effect-v4-extension-guide.md +354 -354
  143. package/agent/extensions/subagents/docs/effect-v4-notes.md +571 -571
  144. package/agent/extensions/subagents/index.ts +779 -779
  145. package/agent/extensions/subagents/manager.test.ts +276 -276
  146. package/agent/extensions/subagents/package-lock.json +2244 -2244
  147. package/agent/extensions/subagents/package.json +19 -19
  148. package/agent/extensions/subagents/result-delivery.test.ts +27 -27
  149. package/agent/extensions/subagents/src/backend.ts +73 -73
  150. package/agent/extensions/subagents/src/backends/claude.ts +701 -701
  151. package/agent/extensions/subagents/src/backends/codex.ts +1060 -1060
  152. package/agent/extensions/subagents/src/backends/pi.ts +575 -575
  153. package/agent/extensions/subagents/src/backends/stub.ts +300 -300
  154. package/agent/extensions/subagents/src/by-the-way.ts +21 -21
  155. package/agent/extensions/subagents/src/domain.ts +253 -253
  156. package/agent/extensions/subagents/src/format.ts +74 -74
  157. package/agent/extensions/subagents/src/manager.ts +736 -736
  158. package/agent/extensions/subagents/src/prompt.ts +92 -92
  159. package/agent/extensions/subagents/src/result-delivery.ts +20 -20
  160. package/agent/extensions/subagents/src/runtime.ts +53 -53
  161. package/agent/extensions/subagents/src/ui/takeover.ts +583 -583
  162. package/agent/extensions/subagents/src/ui/transcript.ts +201 -201
  163. package/agent/extensions/subagents/takeover.test.ts +29 -29
  164. package/agent/extensions/subagents/tsconfig.json +7 -7
  165. package/agent/extensions/todo/AGENTS.md +38 -38
  166. package/agent/extensions/todo/LICENSE +21 -21
  167. package/agent/extensions/todo/config.ts +55 -55
  168. package/agent/extensions/todo/index.ts +151 -151
  169. package/agent/extensions/todo/locales/de.json +17 -17
  170. package/agent/extensions/todo/locales/en.json +15 -15
  171. package/agent/extensions/todo/locales/es.json +17 -17
  172. package/agent/extensions/todo/locales/fr.json +17 -17
  173. package/agent/extensions/todo/locales/pt-BR.json +17 -17
  174. package/agent/extensions/todo/locales/pt.json +17 -17
  175. package/agent/extensions/todo/locales/ru.json +17 -17
  176. package/agent/extensions/todo/locales/uk.json +17 -17
  177. package/agent/extensions/todo/locales/zh.json +17 -17
  178. package/agent/extensions/todo/package-lock.json +3358 -3358
  179. package/agent/extensions/todo/package.json +67 -67
  180. package/agent/extensions/todo/state/i18n-bridge.ts +64 -64
  181. package/agent/extensions/todo/state/invariants.ts +20 -20
  182. package/agent/extensions/todo/state/replay.ts +38 -38
  183. package/agent/extensions/todo/state/selectors.ts +107 -107
  184. package/agent/extensions/todo/state/state-reducer.ts +326 -326
  185. package/agent/extensions/todo/state/state.ts +18 -18
  186. package/agent/extensions/todo/state/store.ts +82 -82
  187. package/agent/extensions/todo/state/task-graph.ts +57 -57
  188. package/agent/extensions/todo/todo-overlay.ts +200 -200
  189. package/agent/extensions/todo/todo.ts +155 -155
  190. package/agent/extensions/todo/tool/response-envelope.ts +109 -109
  191. package/agent/extensions/todo/tool/types.ts +206 -206
  192. package/agent/extensions/todo/view/format.ts +177 -177
  193. package/agent/install.ps1 +637 -527
  194. package/agent/install.sh +620 -511
  195. package/agent/keybindings.json +7 -7
  196. package/bin/kiln.js +124 -11
  197. package/package.json +8 -2
  198. package/agent/extensions/taste/index.ts +0 -443
  199. package/agent/extensions/taste/install.ps1 +0 -23
  200. package/agent/extensions/taste/install.sh +0 -21
  201. /package/agent/extensions/{status line → statusline}/index.ts +0 -0
  202. /package/agent/extensions/{status line → statusline}/install.ps1 +0 -0
  203. /package/agent/extensions/{status line → statusline}/install.sh +0 -0
@@ -1,942 +1,942 @@
1
- # background-terminals — Implementation Guide
2
-
3
- > Research phase output. Updated 2026-07-24 against:
4
- > - `effect@4.0.0-beta.101` (verified installed in this package's `node_modules/effect`; the
5
- > `unstable/process` module exists there but we deliberately do NOT use it — see §6)
6
- > - `@earendil-works/pi-coding-agent@^0.82.0` docs at
7
- > `/Users/davis/.vite-plus/js_runtime/node/24.18.0/lib/node_modules/@earendil-works/pi-coding-agent/docs/`
8
- > - Reference implementations: `extensions/subagents` (Effect v4 service/manager/read-model/tools)
9
- > and `extensions/workflows` (dashboard UI, status line, background completion follow-ups).
10
- >
11
- > Read alongside `extensions/subagents/docs/effect-v4-notes.md` (API cheat sheet) and
12
- > `extensions/subagents/docs/effect-v4-extension-guide.md` (toolchain + ManagedRuntime boundary).
13
- > Those two documents are authoritative for Effect v4 API names — do not use v3 APIs
14
- > (`Effect.fork`, `Effect.async`, `Either`, `Context.Tag`, `Mailbox`, `ServiceMap` are all
15
- > wrong; use `forkChild`/`forkDetach`, `Effect.callback`, `Result`, `Context.Service`, `Queue`).
16
-
17
- ## 1. What this extension is
18
-
19
- The model can start long-running shell processes ("background terminals"), keep working while
20
- they run, check on them, and stop them. It can **never** write to a running process's stdin —
21
- processes are launched with `stdin: "ignore"`; there is no send/steer surface at all (this is
22
- the key simplification vs. subagents' `send()`).
23
-
24
- - Full stdout and stderr are captured **separately and completely** in private spill files;
25
- bounded in-memory tails keep `/ps` responsive (§7.4).
26
- - Tool responses to the model are **always truncated** with the pi truncation utilities.
27
- - When a process exits, the model is woken **exactly once** via `pi.sendMessage(...,
28
- { deliverAs: "followUp", triggerTurn: true })` — no polling — using the same
29
- deferred-delivery/consumed dance as subagents (§9).
30
- - While ≥1 process is running, a one-line widget renders **directly above the editor**:
31
- `N background terminal(s) running • /ps to view` (§10).
32
- - `/ps` opens a two-stage full-screen overlay (list → detail with scrollable stdout/stderr),
33
- modeled on `extensions/subagents/src/ui/takeover.ts` and
34
- `extensions/workflows/dashboard.ts` (§11).
35
-
36
- ## 2. Directory / file architecture
37
-
38
- Mirror the subagents layout exactly (it is the known-green reference; `npm run check` passes
39
- there against the pinned toolchain):
40
-
41
- ```
42
- extensions/background-terminals/
43
- ├── package.json # exact pins, see §3
44
- ├── tsconfig.json # extends ../../tsconfig.json + effect LS plugin
45
- ├── index.ts # extension edge: tools, command, widget, events (plain TS + runTool)
46
- ├── docs/
47
- │ └── implementation-guide.md (this file)
48
- ├── src/
49
- │ ├── domain.ts # types, status union, errors, formatting helpers
50
- │ ├── manager.ts # TerminalManager Context.Service + Layer (the Effect core)
51
- │ ├── output.ts # OutputBuffer: bounded decoded text + byte counters (plain TS class)
52
- │ ├── runtime.ts # ManagedRuntime factory + runTool helper (copy of subagents')
53
- │ ├── prompt.ts # all model-facing strings (tool descriptions, result builders)
54
- │ ├── result-delivery.ts # deferred one-shot delivery map (copy of subagents')
55
- │ └── ui/
56
- │ ├── ps.ts # /ps picker + detail view components
57
- │ └── output-view.ts # stdout/stderr → wrapped display lines
58
- ├── manager.test.ts # node:test end-to-end through a real ManagedRuntime
59
- ├── output.test.ts # OutputBuffer truncation/decoding unit tests
60
- ├── result-delivery.test.ts # (copied semantics, tiny)
61
- └── ps.test.ts # selection-reconciliation tests (like takeover.test.ts)
62
- ```
63
-
64
- Tests live at the package root, plain `node --test --experimental-strip-types`, exactly like
65
- `extensions/subagents/package.json`'s `test` script. Note the repo-root `package.json` test
66
- script (`node --test --experimental-strip-types extensions/*/*.test.ts`) will automatically
67
- pick these up.
68
-
69
- ## 3. Toolchain (copy exactly, per effect-v4-extension-guide.md §1)
70
-
71
- `package.json`:
72
-
73
- ```jsonc
74
- {
75
- "name": "background-terminals",
76
- "private": true,
77
- "type": "module",
78
- "scripts": {
79
- "check": "tsc --noEmit -p .",
80
- "prepare": "effect-tsgo patch",
81
- "test": "node --test --experimental-strip-types manager.test.ts output.test.ts result-delivery.test.ts ps.test.ts"
82
- },
83
- "dependencies": {
84
- "effect": "^4.0.0-beta.99"
85
- },
86
- "devDependencies": {
87
- "@effect/tsgo": "^0.24.2",
88
- "typescript": "^7.0.2"
89
- }
90
- }
91
- ```
92
-
93
- `tsconfig.json` — identical to `extensions/subagents/tsconfig.json`:
94
-
95
- ```jsonc
96
- {
97
- "extends": "../../tsconfig.json",
98
- "compilerOptions": { "plugins": [{ "name": "@effect/language-service" }] },
99
- "include": ["index.ts", "src/**/*.ts", "*.test.ts"]
100
- }
101
- ```
102
-
103
- Per AGENTS.md: add deps with an install command (`npm install effect@^4.0.0-beta.99`),
104
- run `npm run check` when done, avoid explicit return types unless needed, no `as any`.
105
- Verification runs from inside `extensions/background-terminals/` only — never root scripts
106
- (house rule, effect-v4-extension-guide.md §7/§8).
107
-
108
- Note: we do **not** need `@effect/platform-node`. Subagents' codex backend uses raw
109
- `node:child_process` `spawn` inside Effect and that is the right model here too (§6).
110
-
111
- ## 4. Domain model (`src/domain.ts`)
112
-
113
- Follow `extensions/subagents/src/domain.ts` (readonly interfaces, `Data.TaggedError`, status
114
- string union, mutable-snapshot-behind-readonly-view trick lives in the manager).
115
-
116
- ```ts
117
- import { Data } from "effect";
118
-
119
- export type TerminalStatus = "running" | "done" | "failed" | "killed";
120
- // "done" = exited with code 0
121
- // "failed" = exited non-zero, or spawn-level runtime error after start
122
- // "killed" = terminated by bg_kill, UI kill, or session teardown
123
-
124
- export interface TerminalSnapshot {
125
- readonly id: string; // "bt-1", "bt-2", ... (manager counter, like "sa-N")
126
- readonly command: string; // exactly what the model asked to run (display string)
127
- readonly title: string; // short model-provided name, shown in UI (<=80 chars)
128
- readonly cwd: string; // resolved absolute cwd the process runs in
129
- readonly pid?: number; // undefined only if spawn itself failed
130
- readonly status: TerminalStatus;
131
- readonly createdAt: number; // Date.now() at spawn
132
- readonly settledAt?: number; // Date.now() at exit/kill
133
- readonly exitCode?: number; // null-safe: only set when exited via exit code
134
- readonly signal?: string; // e.g. "SIGTERM" when terminated by signal
135
- readonly errorText?: string; // spawn error / kill-escalation notes, bounded
136
- // Live output views (see src/output.ts):
137
- readonly stdout: OutputView;
138
- readonly stderr: OutputView;
139
- }
140
-
141
- export interface OutputView {
142
- readonly text: string; // decoded, possibly head-trimmed text (bounded)
143
- readonly totalBytes: number; // true total bytes ever received
144
- readonly truncatedBytes: number; // bytes dropped from the head (0 = complete)
145
- readonly spillPath?: string; // on-disk full capture, when spilling engaged (§7.6)
146
- }
147
-
148
- export class SpawnError extends Data.TaggedError("SpawnError")<{
149
- readonly message: string;
150
- }> {}
151
- export class ConcurrencyLimitError extends Data.TaggedError("ConcurrencyLimitError")<{
152
- readonly message: string;
153
- }> {}
154
- export class UnknownTerminalError extends Data.TaggedError("UnknownTerminalError")<{
155
- readonly message: string;
156
- }> {}
157
-
158
- export function formatElapsed(snap: TerminalSnapshot) { /* copy from subagents domain.ts */ }
159
- ```
160
-
161
- ### State transitions
162
-
163
- ```
164
- spawn ok exit code 0
165
- (none) ────────► running ───────────────────────► done
166
- │ exit code ≠0 / 'error' event
167
- ├─────────────────────────────► failed
168
- │ bg_kill / UI x / session_shutdown
169
- └─────────────────────────────► killed
170
- spawn throws (ENOENT etc.) → tool call fails; NO entry is tracked (SpawnError to the model)
171
- ```
172
-
173
- Terminal states are final; there is no restart (unlike subagents' `send()` restart). A killed
174
- process that raced an exit event keeps whichever settle landed first — settle must be
175
- idempotent (`if (s.status !== "running") return;`, exactly like `settle()` in
176
- `extensions/subagents/src/manager.ts`).
177
-
178
- Timestamps: `createdAt`/`settledAt` are `Date.now()` millis (matches subagents; `formatElapsed`
179
- consumes them). Exit status: record **both** `exitCode` (number | undefined) and `signal`
180
- (string | undefined) from Node's `exit (code, signal)` callback — exactly one is non-null per
181
- Node semantics; render "exit 0", "exit 137", or "SIGKILL" accordingly.
182
-
183
- ## 5. Effect architecture (`src/runtime.ts`, `src/manager.ts`)
184
-
185
- ### 5.1 Runtime boundary
186
-
187
- Copy `extensions/subagents/src/runtime.ts` nearly verbatim (it is only 53 lines):
188
-
189
- ```ts
190
- import { Cause, Exit, ManagedRuntime, type Effect } from "effect";
191
- import { TerminalManagerLive } from "./manager.ts";
192
-
193
- export function createTerminalRuntime() {
194
- return ManagedRuntime.make(TerminalManagerLive);
195
- }
196
- export type TerminalRuntime = ReturnType<typeof createTerminalRuntime>;
197
-
198
- export async function runTool<A, E>(
199
- runtime: TerminalRuntime,
200
- effect: Effect.Effect<A, E>,
201
- options: { signal?: AbortSignal; interruptMessage?: string } = {},
202
- ) {
203
- const exit = await runtime.runPromiseExit(
204
- effect,
205
- options.signal ? { signal: options.signal } : undefined,
206
- );
207
- if (Exit.isSuccess(exit)) return exit.value;
208
- if (Cause.hasInterruptsOnly(exit.cause)) {
209
- throw new Error(options.interruptMessage ?? "Operation was aborted.");
210
- }
211
- const [first] = Cause.prettyErrors(exit.cause);
212
- throw new Error(first?.message ?? Cause.pretty(exit.cause));
213
- }
214
- ```
215
-
216
- No `BackendRegistry` layer is needed — there is exactly one "backend" (node spawn), so
217
- `AppLayer` is just `TerminalManagerLive`.
218
-
219
- `index.ts` builds the runtime lazily and disposes it on `session_shutdown`, exactly like
220
- `extensions/subagents/index.ts` lines 128–222:
221
-
222
- ```ts
223
- let runtime: TerminalRuntime | undefined;
224
- let managerPromise: Promise<TerminalManagerShape> | undefined;
225
- const getRuntime = () => (runtime ??= createTerminalRuntime());
226
- const getManager = () => {
227
- managerPromise ??= getRuntime().runPromise(TerminalManager).then((manager) => {
228
- manager.view.setOnSettled(onSettled);
229
- unsubStatus?.();
230
- unsubStatus = manager.view.subscribe(() => updateWidget(manager));
231
- updateWidget(manager);
232
- return manager;
233
- });
234
- return managerPromise;
235
- };
236
- ```
237
-
238
- ### 5.2 TerminalManager service (`src/manager.ts`)
239
-
240
- One `Context.Service` holding a plain `Map<string, Entry>` plus the synchronous read model
241
- (the exact structure of `SubagentManager` — see `extensions/subagents/src/manager.ts`, which
242
- is the single most important file to imitate):
243
-
244
- ```ts
245
- export interface TerminalManagerShape {
246
- start(options: StartOptions): Effect.Effect<TerminalSnapshot, SpawnError | ConcurrencyLimitError>;
247
- status(id: string): Effect.Effect<TerminalSnapshot, UnknownTerminalError>;
248
- readonly list: Effect.Effect<ReadonlyArray<TerminalSnapshot>>;
249
- kill(ids: ReadonlyArray<string>): Effect.Effect<ReadonlyArray<KillResult>>; // resolves when settled
250
- readonly disposeAll: Effect.Effect<void>;
251
- readonly view: TerminalReadModel; // synchronous bridge for the TUI + widget
252
- }
253
-
254
- export class TerminalManager extends Context.Service<TerminalManager, TerminalManagerShape>()(
255
- "background-terminals/TerminalManager",
256
- ) {}
257
-
258
- export const TerminalManagerLive: Layer.Layer<TerminalManager> =
259
- Layer.effect(TerminalManager, makeManager);
260
- ```
261
-
262
- `makeManager = Effect.gen(function* () { ... })` closes over:
263
-
264
- - `const entries = new Map<string, Entry>()` — mutable snapshot per entry (readonly view out).
265
- - `const listeners = new Set<() => void>()` + `notify()` — any-change subscription for the
266
- widget and `/ps` list, with try/catch around each UI listener.
267
- - One `Deferred<void>` per entry, completed synchronously and exactly once by `settle()`.
268
- Every `kill()` caller awaits the Deferreds for entries that were running when it began.
269
- - A scoped `FiberSet.runtime` bridge for fire-and-forget UI kills, process-event settlement,
270
- and pruning. Completed fibers remove themselves; disposal waits for the set within a bound,
271
- and scope close interrupts cleanup still live after that bound.
272
- - `let counter = 0` for ids; `let disposed = false`; `waitInterest` is NOT needed (there is no
273
- `bg_wait` tool in v1 — see §8 note), but the "consumed" concept still applies to `bg_kill`
274
- and `bg_status` so a settle isn't double-announced (§9.3).
275
- - `yield* Effect.addFinalizer(() => disposeAll)` — the safety net so `runtime.dispose()` in
276
- `session_shutdown` kills every process even if the extension forgot (subagents manager.ts
277
- line 657).
278
-
279
- Concurrency cap: subagents caps at `MAX_RUNNING = 4` with a synchronous reservation
280
- (`Effect.suspend` before the first yield so parallel tool calls cannot race the check —
281
- manager.ts lines 364–383). For terminals use `MAX_RUNNING = 8` (processes are cheaper than
282
- agents) and the same reservation pattern; and `MAX_TRACKED = 32` completed entries retained,
283
- pruned oldest-settled-first exactly like `pruneSettled()` (never prune running entries).
284
-
285
- ### 5.3 Where Effect fibers/queues/etc. do and don't earn their keep
286
-
287
- Per effect-v4-extension-guide.md §0 the async core is Effect; per the codex backend precedent
288
- the Node stream plumbing stays plain callbacks. Concretely:
289
-
290
- - **Yes Effect:** the manager service/layer, `start` reservation, per-entry `Deferred`,
291
- `kill` (timeout + escalation + Deferred wait), scoped `FiberSet` cleanup, `disposeAll`
292
- (parallel bounded teardown), `runTool` boundary, and `Effect.addFinalizer`.
293
- - **Plain TS callbacks:** `child.stdout.on("data")`, `child.on("exit")` handlers mutate the
294
- entry snapshot and call `notify()` directly. This is exactly what the codex backend does with
295
- its JSON-RPC stdout pump (`codex.ts` lines ~820–860). Do NOT build a
296
- `Queue<SubagentEvent>`/pump-fiber pipeline here — subagents needs that because three
297
- heterogeneous backends normalize into one event stream; a single spawn does not.
298
-
299
- ## 6. Node child_process design (the core of `start`)
300
-
301
- Model on `makeCodexSession` in `extensions/subagents/src/backends/codex.ts` (spawn options,
302
- kill-tree, terminate-with-escalation), minus the JSON-RPC machinery:
303
-
304
- ```ts
305
- import { spawn } from "node:child_process";
306
-
307
- const child = yield* Effect.try({
308
- try: () =>
309
- spawn(shellPath, ["-c", options.command], {
310
- cwd: options.cwd,
311
- env: process.env,
312
- stdio: ["ignore", "pipe", "pipe"], // ← stdin IGNORED: no input surface, ever
313
- detached: process.platform !== "win32", // own process group on POSIX → group kill
314
- }),
315
- catch: (error) => new SpawnError({ message: boundedError(error) }),
316
- });
317
- ```
318
-
319
- Decisions and rationale:
320
-
321
- - **Shell execution.** The model supplies one `command` string; run it through the platform
322
- shell (`/bin/sh -c` on POSIX, `cmd.exe /d /s /c` on Windows) so pipes/redirection work. Honor the
323
- user's configured shell if convenient (`~/.pi/agent/settings.json` has
324
- `"shellPath": ".../zsh-with-rc"`), but `/bin/sh` is an acceptable v1 — document which you
325
- pick in the tool description. Never `shell: true` with an args array (double-parse trap).
326
- - **`stdin: "ignore"`** enforces the "no subsequent input" requirement at the OS level. A
327
- process that tries to read stdin gets EOF immediately, which is the honest contract (and the
328
- tool description must say so — interactive commands will exit or hang, and `bg_kill` is the
329
- remedy).
330
- - **`detached: true` on POSIX** gives the child its own process group, so kill can signal
331
- `-pid` and take down the whole tree (grandchildren from `npm run dev` etc.). `killTree`
332
- keeps the direct-signal fallback when the group is gone; Windows uses `taskkill /T` and
333
- adds `/F` for the force-kill phase. `terminateChild` uses Effect
334
- callbacks/timeouts: SIGTERM now, SIGKILL after 2s if needed, then a final 500ms bound.
335
- Do NOT call `child.unref()` — we want the exit event, and pi owns the lifetime anyway.
336
- - **Spawn failure semantics.** `spawn()` itself rarely throws; ENOENT arrives via
337
- `child.once("error", ...)`. Wire the error handler *before* returning from `start`, and treat
338
- an error event pre-exit as settling the entry to `failed` with `errorText` (mirror
339
- `failForProcessExit` in codex.ts). To catch instant failures, you may optionally wait one
340
- tick for `spawn` event vs `error` event, but simplest correct behavior: register the entry
341
- immediately as `running` and let the near-instant `error`/`exit` settle it; the model gets
342
- the settle notification milliseconds later.
343
- - **Exit handling** (single source of truth for settling):
344
-
345
- ```ts
346
- child.once("exit", (code, signal) => {
347
- finishOutput(entry); // flush any pending partial decode
348
- settle(entry, {
349
- status: entry.killSignaled ? "killed" : code === 0 ? "done" : "failed",
350
- exitCode: code ?? undefined,
351
- signal: signal ?? undefined,
352
- });
353
- });
354
- ```
355
-
356
- `killSignaled` is set in the same synchronous effect that sends SIGTERM, so a process that
357
- exits before signaling keeps its natural status while a signaled process reports `killed`.
358
- Settle is idempotent (§4).
359
- - **cwd semantics.** The tool takes optional `working_dir`; resolve with
360
- `path.resolve(ctx.cwd, params.working_dir ?? ".")` and validate
361
- `fs.existsSync(cwd) && fs.statSync(cwd).isDirectory()` in the tool handler *before* touching
362
- the runtime — throw a plain Error otherwise. This is copied from `subagent_spawn`'s handler
363
- (`extensions/subagents/index.ts` lines 262–265). No trust-store logic is needed (we are not
364
- spawning an agent in another project; a shell command in another directory is equivalent to
365
- what the bash tool already allows).
366
-
367
- ### 6.1 Why not `effect/unstable/process` yet?
368
-
369
- `ChildProcess.make` + `ChildProcessHandle` is the eventual target, but the current Effect beta
370
- cannot preserve the current process contract yet:
371
-
372
- 1. `forceKillAfter` does not correctly wait before SIGKILL on POSIX in this pin.
373
- 2. `ChildProcessHandle.exitCode` does not expose the actual terminating signal, while the
374
- public snapshot and model-facing output distinguish `SIGTERM` from `SIGKILL`.
375
-
376
- This first pass therefore keeps raw spawn and stream callbacks, while moving termination
377
- waits, escalation deadlines, settlement coordination, and cleanup ownership into Effect.
378
- Do not add `@effect/platform-node` until both blockers can be resolved.
379
-
380
- ## 7. Output capture (`src/output.ts`)
381
-
382
- ### 7.1 Requirements recap
383
-
384
- Capture stdout and stderr **separately** and **completely** (the user's "full stdout/stderr"),
385
- viewable in `/ps`; tool responses truncated; memory must be bounded.
386
-
387
- ### 7.2 Decoding — do it right
388
-
389
- Do NOT use `child.stdout.setEncoding("utf8")` naïvely-per-chunk... actually `setEncoding`
390
- internally uses a StringDecoder and *is* multibyte-safe across chunk boundaries, which is why
391
- codex.ts can use it. Two acceptable options; pick (a):
392
-
393
- - (a) `child.stdout.setEncoding("utf8")` and receive `string` chunks (Node handles split
394
- UTF-8 sequences). Simplest, matches codex.ts line ~824.
395
- - (b) accumulate `Buffer`s and decode with `new (await import("node:string_decoder")).StringDecoder("utf8")`.
396
-
397
- Either way, strip nothing at capture time — raw text goes into the buffer; ANSI/control
398
- sanitization happens at *render* time using `sanitizeText` (copy from
399
- `extensions/subagents/src/ui/transcript.ts` lines 15–29; it exists precisely because raw ANSI
400
- desyncs the TUI renderer).
401
-
402
- ### 7.3 OutputBuffer (bounded ring with head-drop + optional spill)
403
-
404
- ```ts
405
- export class OutputBuffer {
406
- private chunks: string[] = [];
407
- private bytes = 0; // bytes currently retained (Buffer.byteLength of chunks)
408
- totalBytes = 0; // true total ever received
409
- truncatedBytes = 0; // dropped from the head
410
- spillPath?: string;
411
-
412
- constructor(private maxRetainedBytes: number, private spill?: (chunk: string) => void) {}
413
-
414
- push(chunk: string) {
415
- /* Count and spill the complete chunk first. If the chunk alone exceeds
416
- maxRetainedBytes, discard older retained chunks and UTF-8-safely trim
417
- this chunk to its newest cap-sized tail. Otherwise append it and evict
418
- older whole chunks until retained bytes fit. Every discarded byte
419
- increments truncatedBytes; totalBytes counts the original input. */
420
- }
421
- view(): OutputView { /* { text: this.chunks.join(""), totalBytes, truncatedBytes, spillPath } */ }
422
- }
423
- ```
424
-
425
- Cache the `join("")` and invalidate on push so the 1Hz UI tick doesn't re-join megabytes.
426
-
427
- ### 7.4 Memory bounds vs "full inspection" — the honest tradeoff
428
-
429
- Unbounded retention of a `yes`-style firehose is a hard memory leak (codex.ts caps its stderr
430
- retain at 4 KiB and treats an unbounded protocol buffer as session-fatal for exactly this
431
- reason). Resolution:
432
-
433
- - **In-memory retained cap: 2 MiB per stream per process** (so ≤ 8 procs × 2 streams × 2 MiB =
434
- 32 MiB worst case). The newest output is always retained; the head is dropped.
435
- - **Spill-to-disk for the full capture** (this is what makes "full stdout/stderr" true even
436
- past the cap): create the shared/session directories with owner-only `0700` permissions,
437
- then open two `0600` append-mode `WriteStream`s under
438
- ``path.join(os.tmpdir(), "pi-background-terminals", sessionId, `${id}.stdout.log`)`` (and
439
- `.stderr.log`). A `WriteStream` serializes writes per stream; settlement ends and awaits
440
- both streams behind a bounded flush barrier before publishing the result. A stream error or
441
- flush timeout clears the affected full-log pointer and surfaces a bounded `errorText` note.
442
- The `/ps` detail view shows the in-memory tail and, when `truncatedBytes > 0`, a header line
443
- "first N KiB dropped from view — full log: <spillPath>"; model-facing results reference the
444
- same path. `disposeAll` removes the private session spill directory after all entry scopes
445
- and spill flushes complete, so secret-bearing logs do not outlive the owning pi session.
446
- - Precedent for "truncate + point at the full file": docs/extensions.md "Output Truncation"
447
- section recommends exactly this shape for tool results.
448
-
449
- ### 7.5 Entry wiring inside `start`
450
-
451
- Per entry, like subagents' `spawn` (manager.ts lines 385–466):
452
-
453
- ```ts
454
- const scope = yield* Scope.make();
455
- const settled = yield* Deferred.make<void>();
456
- // finalizer kills the tree; registered in the scope so BOTH kill() and disposeAll()
457
- // and runtime.dispose() converge on one teardown path:
458
- yield* Scope.provide(
459
- Effect.addFinalizer(() =>
460
- Effect.gen(function* () {
461
- yield* terminateChild(child, () => entry.stdioClosed, markKillSignaled);
462
- yield* Deferred.await(settled).pipe(
463
- Effect.timeout(SETTLE_GRACE_MS),
464
- Effect.ignore,
465
- );
466
- // If still running, flush output within its bound and settle here.
467
- }),
468
- ),
469
- scope,
470
- );
471
- entries.set(id, { snapshot, child, scope, stdoutBuf, stderrBuf, settled });
472
- ```
473
-
474
- `kill(ids)` then is: `Scope.close(entry.scope, Exit.void)` (bounded with
475
- `Effect.timeout(STOP_TIMEOUT_MS)` + `Effect.ignore`) in the scoped cleanup `FiberSet`, then
476
- await every captured entry's `Deferred`. Return per-id `{ id, status, killed: boolean }`
477
- results and treat already-settled ids as no-ops rather than errors.
478
-
479
- `disposeAll`: set `disposed = true`, snapshot `[...entries.values()]`, close every scope with
480
- `{ concurrency: "unbounded" }` and a 5s timeout each — verbatim subagents `disposeAll`
481
- (manager.ts lines 596–618).
482
-
483
- ### 7.6 Race conditions checklist (each has a subagents precedent)
484
-
485
- - **Spawn vs concurrent spawn past the cap** → synchronous reservation before first yield
486
- (`reserved++` inside `Effect.suspend`; decrement in `Effect.ensuring`).
487
- - **Kill vs natural exit** → idempotent `settle` with one authoritative precedence rule. If
488
- kill reaches a live shell, set `killSignaled` in the same effect that signals it and report
489
- `killed`. If the shell's `exit` event was already observed, preserve its natural
490
- `done`/`failed` status even when cleanup must still signal descendants holding stdio open.
491
- A missing `close` after `exit` starts a bounded grace, then closes the entry scope so the
492
- surviving process group is terminated and the entry cannot occupy a running slot forever.
493
- - **Exit event vs scope close ("stream ended unexpectedly")** → we have no pump, so this class
494
- disappears; the only settle source is the `exit`/`error` listener.
495
- - **Settle during teardown** → `if (!disposed) onSettled?.(...)` so a result is never queued
496
- into a shutting-down session (subagents `settle`, manager.ts line 280).
497
- - **Tool AbortSignal during `bg_kill`'s wait** → interruption stops only that caller's
498
- `Deferred.await`; the detached scope-close stays owned by the manager `FiberSet`, and
499
- `Effect.ensuring` still releases bookkeeping.
500
- - **Late output after exit** → Node may still flush 'data' after 'exit' is observed in rare
501
- orderings; buffers accept pushes until `close` — harmless because settle doesn't freeze the
502
- buffer, and the UI just shows more text. (Optionally listen on `close` instead of `exit` to
503
- be strictly after stdio flush; `close` fires when stdio streams end — prefer `close` for
504
- settling to guarantee complete output at notification time, and keep `exit` only to record
505
- code/signal. This is the one place we improve on codex.ts, which doesn't need output
506
- completeness.)
507
-
508
- **Recommended:** record `{code, signal}` on `exit`, settle + notify on `close`. This
509
- guarantees the completion follow-up message contains the final output tail.
510
-
511
- ## 8. Tools (`index.ts` + `src/prompt.ts`)
512
-
513
- All model-facing strings live in `src/prompt.ts` (subagents convention). Register with
514
- `pi.registerTool`; parameters via `typebox` `Type.Object`; use `StringEnum` from
515
- `@earendil-works/pi-ai` if any enum appears (Google-compat rule, docs/extensions.md
516
- "Tool Definition"). Throw plain `Error` for failures (that is what sets `isError`).
517
-
518
- ### 8.1 `bg_start`
519
-
520
- ```ts
521
- parameters: Type.Object({
522
- command: Type.String({ description: "Shell command line to run in the background (sh -c on POSIX, cmd.exe /d /s /c on Windows). It receives no stdin (EOF immediately); interactive commands will not work." }),
523
- title: Type.String({ description: "Short human-readable name shown in listings and the UI" }),
524
- working_dir: Type.Optional(Type.String({ description: "Working directory (default: current working directory)" })),
525
- })
526
- ```
527
-
528
- Handler: validate cwd (§6), `title.trim().slice(0, 80) || "terminal"`, then
529
- `runTool(getRuntime(), manager.start({ command, title, cwd }))`. Result text (build in
530
- prompt.ts, like `buildSubagentSpawnResult`):
531
-
532
- ```
533
- Started background terminal bt-3 "dev server" (pid 12345, /Users/davis/project).
534
- It runs in the background with no stdin. You'll get a message when it exits, or use
535
- bg_status(id: "bt-3") to peek, bg_kill to stop it, bg_list to see all.
536
- ```
537
-
538
- `promptSnippet`: "Run a long-lived shell command in the background (dev servers, builds,
539
- watchers); output is captured and you're notified on exit".
540
- `promptGuidelines` (name the tool explicitly — docs warn "this tool" is ambiguous):
541
- - "Use bg_start for commands expected to run long or indefinitely (servers, watch modes); use the regular bash tool for quick commands."
542
- - "bg_start processes receive no stdin — never start a command that requires interactive input."
543
- - "After bg_start, keep working; the exit result arrives automatically. Use bg_status only when you need current output before continuing."
544
-
545
- Description documents the truncation limits (docs requirement) and the no-stdin contract.
546
-
547
- ### 8.2 `bg_status`
548
-
549
- ```ts
550
- parameters: Type.Object({ id: Type.String({ description: 'Terminal id, e.g. "bt-1"' }) })
551
- ```
552
-
553
- Unknown id → throw with the known-ids list (copy the exact error style from `subagent_check`:
554
- `Unknown terminal id "x". Known: bt-1, bt-2.`). Result: one metadata line
555
- (`bt-1 [running] "dev server" (pid 12345, 3m12s, exit -, /path)`) then **tail-truncated**
556
- stdout and stderr sections:
557
-
558
- ```ts
559
- const stdout = truncateTail(snap.stdout.text, { maxBytes: 16 * 1024, maxLines: 400 });
560
- const stderr = truncateTail(snap.stderr.text, { maxBytes: 8 * 1024, maxLines: 200 });
561
- ```
562
-
563
- `truncateTail` (not head) because for process logs the end matters — this is the documented
564
- guidance in docs/extensions.md Output Truncation. When truncated, append
565
- `[stdout truncated: showing last X of Y. Full log: <spillPath or "in /ps viewer">]` using
566
- `formatSize` + the truncation result fields (see `truncatedOutput()` in subagents index.ts for
567
- the message shape). If `bg_status` observes a settled entry whose completion message is still
568
- pending delivery, mark it consumed (§9.3).
569
-
570
- ### 8.3 `bg_list`
571
-
572
- No parameters. One line per entry via a `describeTerminal(snap)` helper (mirror
573
- `describeSubagent`): id, status, title, pid, elapsed, exit code/signal, cwd, and total output
574
- sizes (`formatSize(stdout.totalBytes)`). "No background terminals." when empty. Include both
575
- running and completed (completed entries are retained up to `MAX_TRACKED`).
576
-
577
- ### 8.4 `bg_kill`
578
-
579
- ```ts
580
- parameters: Type.Object({ ids: Type.Array(Type.String(), { description: 'Terminal ids to stop, e.g. ["bt-1"]' }) })
581
- ```
582
-
583
- Validate all ids known first (throw listing unknowns, copy `subagent_cancel`). Then
584
- `runTool(getRuntime(), manager.kill(ids), { signal, interruptMessage: "Kill wait aborted; termination continues in the background." })`.
585
- Report per id: `Killed bt-1 "dev server" (SIGTERM).` or `bt-2 "build" was already done (exit 0).`
586
- Killing marks the settle consumed so the model doesn't also get the async completion message
587
- (§9.3) — same reason subagents' `cancel` calls `addInterest` before interrupting.
588
-
589
- **No `bg_wait` and no `bg_send`.** No stdin is a hard requirement. Blocking wait is
590
- deliberately omitted in v1: completion notification makes it redundant, and it would drag in
591
- subagents' full `waitInterest` machinery. If it's ever wanted, each entry already has a
592
- settlement `Deferred` and the subagents `waitFor` result shaping is the template.
593
-
594
- ## 9. Completion notification — exactly once, no polling, no turn races
595
-
596
- This is the subtlest requirement. Copy the subagents solution wholesale; it exists precisely
597
- to solve this problem (see comments in `extensions/subagents/index.ts` lines 168–222 and
598
- `result-delivery.ts`).
599
-
600
- ### 9.1 Mechanism
601
-
602
- On settle, the manager invokes a hook `onSettled(snap, consumed)` registered by `index.ts`
603
- (same `view.setOnSettled` bridge). The hook:
604
-
605
- ```ts
606
- const resultDelivery = createDeferredResultDelivery<TerminalSnapshot>(); // copy the 20-line module
607
-
608
- const onSettled = (snap: TerminalSnapshot, consumed: boolean) => {
609
- if (consumed) { resultDelivery.consume([snap.id]); return; }
610
- // Defer a deep-enough copy: the live snapshot keeps mutating (late output flushes).
611
- resultDelivery.defer({ ...snap, stdout: { ...snap.stdout }, stderr: { ...snap.stderr } });
612
- if (sessionContext?.isIdle()) flushResults();
613
- };
614
-
615
- pi.on("agent_settled", flushResults);
616
-
617
- const flushResults = () => {
618
- for (const snap of resultDelivery.drain()) {
619
- pi.sendMessage({
620
- customType: "background-terminal-result",
621
- content: buildTerminalResultMessage(snap), // prompt.ts; truncateTail'd output inside
622
- display: true,
623
- details: { id: snap.id, title: snap.title, status: snap.status, exitCode: snap.exitCode, signal: snap.signal },
624
- }, { deliverAs: "followUp", triggerTurn: true });
625
- }
626
- };
627
- ```
628
-
629
- ### 9.2 Why this is race-free (the reasoning to preserve in code comments)
630
-
631
- - `deliverAs: "followUp"` queues the message until the agent has no more tool calls; it never
632
- interrupts a mid-turn stream (docs/extensions.md § pi.sendMessage).
633
- - `triggerTurn: true` wakes the model immediately **iff idle**; if busy, the queued follow-up
634
- is delivered when the current run settles — either way exactly one delivery.
635
- - The `Map`-keyed `resultDelivery` (keyed by id, `drain()` clears) makes double-delivery
636
- structurally impossible even if both the `isIdle()` fast-path and the `agent_settled` event
637
- fire: whoever drains first wins, the second drain sees an empty map.
638
- - The `consumed` flag closes the remaining hole: if the model is *currently inside*
639
- `bg_kill` (which returns the final state itself), the settle must not ALSO queue a message.
640
- Manager computes `consumed` = "a kill/status collection is in flight for this id" at settle
641
- time (subagents: `waitInterest`; here: the `kill()`-marked id set).
642
- - `if (!disposed)` in `settle` prevents queueing into a shutting-down session.
643
-
644
- ### 9.3 Consumed-set details
645
-
646
- Keep a `Map<string, number> killInterest` in the manager; `kill()` adds interest before
647
- signaling and releases in `Effect.ensuring` (identical to `addInterest`/`releaseInterest`).
648
- `settle` computes `consumed = (killInterest.get(id) ?? 0) > 0`. Additionally, `bg_kill`'s tool
649
- handler calls `resultDelivery.consume(ids)` after `runTool` returns, mirroring
650
- `subagent_wait`'s "settlement may have happened before this wait began" comment (index.ts
651
- line 352) — belt and suspenders for the settled-before-kill-started ordering.
652
-
653
- ### 9.4 Result message content
654
-
655
- `buildTerminalResultMessage` (prompt.ts): first line
656
- `Background terminal bt-3 "dev server" exited (exit 1) after 4m12s.` (or `(SIGTERM)` /
657
- `was killed`), then tail-truncated stdout (≤ 16 KiB) and, if non-empty, stderr (≤ 8 KiB) in
658
- labeled sections, with truncation notes pointing at the spill file. Register a
659
- `pi.registerMessageRenderer("background-terminal-result", ...)` for a collapsed preview —
660
- copy the subagent-result renderer (index.ts lines 514–561: icon by status, header line,
661
- 8-line preview, "ctrl+o to expand").
662
-
663
- ## 10. Widget above the editor
664
-
665
- Requirement: visible **only while ≥1 process is running**, directly above editor, text
666
- `N background terminal(s) running • /ps to view`.
667
-
668
- API: `ctx.ui.setWidget(key, linesOrFactory)` — default placement is already **above the
669
- editor** (docs/extensions.md "Widgets, Status, and Footer" + tui.md Pattern 5); do NOT pass
670
- `placement: "belowEditor"`. Clear with `setWidget(key, undefined)`.
671
-
672
- ```ts
673
- const updateWidget = (manager: TerminalManagerShape) => {
674
- if (!ui) return; // captured from session_start ctx.hasUI
675
- const running = manager.view.list().filter((s) => s.status === "running").length;
676
- if (running === 0) { ui.setWidget("background-terminals", undefined); return; }
677
- ui.setWidget("background-terminals", (_tui, theme) => {
678
- const line =
679
- theme.fg("warning", "■ ") +
680
- theme.fg("text", `${running} background terminal${running === 1 ? "" : "s"} running`) +
681
- theme.fg("dim", " • ") + theme.fg("accent", "/ps") + theme.fg("dim", " to view");
682
- return { render: () => [line], invalidate: () => {} };
683
- });
684
- };
685
- ```
686
-
687
- Drive it from `manager.view.subscribe(...)` exactly like subagents drives `setStatus`
688
- (index.ts lines 139–166) — the subscription fires on every state change, including settles, so
689
- the widget disappears the moment the last process exits. Guard `ctx.hasUI`; wrap in try/catch
690
- like workflows' `updateIndicator` ("UI may be unavailable"). Clear the widget in
691
- `session_shutdown` before disposing the runtime.
692
-
693
- (Singular/plural: render `1 background terminal running`, `2 background terminals running` —
694
- implement the requested "terminal(s)" sense as proper pluralization.)
695
-
696
- ## 11. `/ps` command + two-stage UI (`src/ui/ps.ts`, `src/ui/output-view.ts`)
697
-
698
- Register `pi.registerCommand("ps", { description: "List and inspect background terminals", handler })`.
699
- Handler: TUI-mode guard + empty-state notify + open picker — copy the `/subagents` command
700
- skeleton (index.ts lines 565–587). Non-TUI (`ctx.mode !== "tui"`): print a plain-text listing
701
- via `ctx.ui.notify` like workflows' non-TUI fallback, or just the notify error like subagents —
702
- prefer the listing (cheap and useful in RPC mode).
703
-
704
- ### 11.1 Stage 1 — list (dashboard)
705
-
706
- Copy `SubagentDashboard` (`src/ui/takeover.ts` lines 109–344) with terminal rows:
707
-
708
- - Entry point loop `openTerminalPicker(ctx, view)` — the `while (true)` pick→detail→back loop
709
- of `openSubagentPicker` (lines 52–86), full-screen overlay
710
- (`{ overlay: true, overlayOptions: { anchor: "center", width: "100%", maxHeight: "100%" } }`).
711
- - Row left: selection marker, status glyph (`■` warning/success/error — reuse `statusGlyph`
712
- pattern; map `killed` to muted/error), title, dim id.
713
- - Row right: `pid 12345 · 3m12s · exit 0` (or `running` / `SIGTERM`), dim separators — the
714
- `split(left, right, width)` helper from workflows' dashboard is the cleanest to copy.
715
- - Keys: up/down/j/k select, enter open, `x` kill selected (only when running →
716
- `view.requestKill(id)` fire-and-forget, precedent: dashboard `x` → `requestAbort`), esc
717
- close. Hint line built from `keybindings.getKeys(...)` via the `configuredKeys` helper.
718
- - 1Hz `setInterval` ticker for elapsed times + `view.subscribe` re-render, both cleaned up in
719
- `dispose()`/`cleanup()` (idempotent closed-flag pattern — copy it exactly; overlay components
720
- are disposed on close and must not be reused, tui.md "Overlay Lifecycle").
721
- - Keep list selection stable across refreshes with `reconcileDashboardSelection` (takeover.ts
722
- lines 95–107) — copy it and its test (`takeover.test.ts`).
723
-
724
- ### 11.2 Stage 2 — detail (read-only inspector)
725
-
726
- Copy `TakeoverView` (takeover.ts lines 350–563) **minus the Input line** (read-only: no
727
- `Focusable`, no `Input`, no `requestSend`). Layout:
728
-
729
- ```
730
- ────────────────────────────────────────────────────────────
731
- ■ bt-3 · dev server · running · 4m12s · pid 12345 · ~/project
732
- $ npm run dev
733
- ────────────────────────────────────────────────────────────
734
- [ tab: stdout (1.2MB) | stderr (4KB) ] ← `t` toggles streams
735
- ...scrollable output lines (sanitized, wrapped, tail-pinned)...
736
- ... 120 lines below · ↓/pgdn
737
- ────────────────────────────────────────────────────────────
738
- esc back · t stdout/stderr · x kill · ↑/↓ scroll · pgup/pgdn page · g/G top/bottom
739
- ────────────────────────────────────────────────────────────
740
- ```
741
-
742
- - Metadata header: status glyph, id, title, status word, elapsed (`formatElapsed`), pid, cwd,
743
- exit code/signal when settled, total sizes (`formatSize`), truncation note when
744
- `truncatedBytes > 0` (with spill path).
745
- - **stdout/stderr shown separately** (requirement): a `t` key toggles the active stream;
746
- header tab shows both sizes. (Alternative side-by-side split like workflows' phases/agents
747
- panels is more code for less readability of wide log lines — use the toggle.)
748
- - Output rendering (`src/ui/output-view.ts`): split buffer text on `\n`, `sanitizeText` each
749
- line (copy from transcript.ts — ANSI strip is mandatory or the overlay smears), wrap with
750
- `wrapTextWithAnsi`, `truncateToWidth`. Scroll state = offset-from-bottom, 0 = pinned to
751
- bottom so a running process live-tails; clamp `scrollOffset` to `maxOffset` each render
752
- (TakeoverView lines 510–543 is exactly this fixed-height-viewport math — copy it, including
753
- the "scroll status consumes a viewport row" trick so height never jumps).
754
- - Live updates: `view.subscribeTo(id, ...)` per-entry subscription + the 50ms
755
- `scheduleRender` debounce (TakeoverView lines 406–414 — a chatty process emits a chunk per
756
- write; do not repaint per chunk).
757
- - Keys: esc/left back to list (loop re-opens dashboard), `x` kill (running only), scroll keys
758
- via `keybindings.matches(data, "tui.editor.cursorUp"/"cursorDown"/"pageUp"/"pageDown")` plus
759
- j/k and g/G (workflows transcript view precedent).
760
- - Big-buffer perf: with the 2 MiB cap, worst case ~30k lines; recompute wrapped lines only when
761
- the buffer version or width changed (cache `(version, width) → lines`), not per render tick.
762
-
763
- ### 11.3 Read model
764
-
765
- ```ts
766
- export interface TerminalReadModel {
767
- list(): ReadonlyArray<TerminalSnapshot>;
768
- get(id: string): TerminalSnapshot | undefined;
769
- size(): number;
770
- subscribe(listener: () => void): () => void;
771
- subscribeTo(id: string, listener: () => void): () => void;
772
- requestKill(id: string): void; // fire-and-forget via the scoped FiberSet runtime
773
- setOnSettled(hook?: (snap: TerminalSnapshot, consumed: boolean) => void): void;
774
- }
775
- ```
776
-
777
- Verbatim shape of `SubagentReadModel` minus `requestSend`. Snapshots are live objects; the UI
778
- must not mutate them (same doc comment as manager.ts line 89).
779
-
780
- ## 12. Lifecycle: reload / new / resume / fork / shutdown
781
-
782
- pi's session replacement flow (docs/extensions.md "Lifecycle Overview" + session_shutdown):
783
- `/new`, `/resume`, `/fork`, `/reload`, and quit all emit `session_shutdown` (with `event.reason`)
784
- for the old extension instance, then re-instantiate extensions and emit `session_start`.
785
- Consequences:
786
-
787
- - **Processes do not survive any session transition.** In `session_shutdown`: clear
788
- `resultDelivery`, unsubscribe, clear widget, null the ui/context refs, then
789
- `await closing?.dispose()` — the ManagedRuntime close runs the manager finalizer →
790
- `disposeAll` → every entry scope → `terminateChild` (SIGTERM→SIGKILL tree kill). This is
791
- the identical teardown in subagents index.ts lines 210–222; each scope close is bounded
792
- (5s timeout) so a wedged process cannot hang shutdown, and SIGKILL covers it anyway.
793
- - **Spill files do not survive the session either.** `disposeAll` first closes every entry
794
- scope and awaits bounded spill flushes, then recursively removes its owner-only session
795
- directory. Paths shown in the old transcript are intentionally session-lifetime pointers.
796
- - **No persistence / no resurrection.** Unlike workflows (which persists `workflow.json` and
797
- marks stale "running" runs as aborted on reload — dashboard.ts lines 286–297), v1 keeps no
798
- cross-session record: killed-on-shutdown processes simply disappear. Optionally append a
799
- `pi.appendEntry("background-terminals-note", {...})` breadcrumb ("bt-2 'dev server' was
800
- killed by session shutdown") so a resumed session's transcript explains the vanished
801
- terminal — cheap and worth doing; entries don't enter LLM context (docs: appendEntry).
802
- The model-facing story stays consistent because tool results always describe terminals as
803
- session-scoped ("killed when the session ends" in `bg_start`'s description).
804
- - **Do not spawn from stale contexts.** All spawning goes through tool handlers with a live
805
- `ctx`; the manager rejects `start` when `disposed` (SpawnError "shutting down", subagents
806
- manager.ts lines 370–374 precedent).
807
- - **Fork/clone:** nothing special — same shutdown+start pair; the new instance starts empty.
808
-
809
- ## 13. Truncation constants (single place, `index.ts` top)
810
-
811
- ```ts
812
- const STATUS_STDOUT_MAX = 16 * 1024; // bg_status stdout tail
813
- const STATUS_STDERR_MAX = 8 * 1024; // bg_status stderr tail
814
- const RESULT_STDOUT_MAX = 16 * 1024; // completion follow-up stdout tail
815
- const RESULT_STDERR_MAX = 8 * 1024;
816
- const RETAINED_PER_STREAM = 2 * 1024 * 1024; // in-memory cap per stream (spill keeps the rest)
817
- ```
818
-
819
- All clamped by `Math.min(..., DEFAULT_MAX_BYTES)` and `DEFAULT_MAX_LINES` (imports from
820
- `@earendil-works/pi-coding-agent`, verified exported in `dist/index.d.ts`) — same defensive
821
- clamp as `truncatedOutput` in subagents index.ts. Always `truncateTail` for process output.
822
-
823
- ## 14. Test plan
824
-
825
- Follow the house style: `node:test` + `assert/strict`, end-to-end through a real
826
- `ManagedRuntime`, minimal count, deterministic (subagents `manager.test.ts` is the template,
827
- including the `withManager` fixture that guarantees `runtime.dispose()` in `finally`).
828
-
829
- **`output.test.ts`** (pure, no processes)
830
- 1. push/view roundtrip; totalBytes/truncatedBytes accounting when the cap evicts head chunks.
831
- 2. multibyte boundary: feeding split UTF-8 via setEncoding path is Node's job, but verify the
832
- buffer never splits what it was given and byte counts use `Buffer.byteLength`.
833
- 3. spill callback receives every chunk in order even after eviction.
834
-
835
- **`manager.test.ts`** (real processes — use `node -e` one-liners for portability, no shell
836
- tricks; they exist on any machine running pi)
837
- 1. happy path: `start` node printing to stdout+stderr then exiting 0 → status transitions
838
- running→done, exitCode 0, both buffers correct and separate, settle hook fired once with
839
- `consumed: false`.
840
- 2. non-zero exit → `failed`, exitCode captured.
841
- 3. `kill` on a `setInterval` never-exiting script → `killed`, signal recorded, `kill()` only
842
- resolves after settle; second `kill` of same id reports already-settled, no error.
843
- 4. process-tree termination: spawn a grandchild that updates a unique heartbeat sentinel,
844
- kill, then use bounded polling with an explicit timeout to confirm both that the process is
845
- gone and that its unique sentinel stopped changing. The sentinel ties the assertion to the
846
- spawned child so PID reuse cannot create a false pass.
847
- 5. concurrency cap: cap+1 concurrent starts → last fails with ConcurrencyLimitError;
848
- reservation released on spawn failure (start a bogus binary → SpawnError → slot free).
849
- 6. consumed semantics: settle during an in-flight `kill` reports `consumed: true`.
850
- 7. `disposeAll` (via `runtime.dispose()`) kills a running process and settles it as killed;
851
- no settle hook fires after dispose (`disposed` guard).
852
- 8. pruning: exceed MAX_TRACKED with settled entries → oldest pruned, running never pruned.
853
- 9. SIGTERM-resistant process → SIGKILL after the 2s grace, within the 5s close bound.
854
- 10. aborted `bg_kill` wait → detached escalation still reaches SIGKILL and settles.
855
- 11. overlapping multi-id kills → every caller observes every captured settlement; each
856
- settle hook fires once and consumed state remains true.
857
- 12. shell `exit` without stdio `close` → bounded cleanup reaps the descendant holding the
858
- pipes, preserves the shell's natural exit status, and releases the running slot.
859
-
860
- **`result-delivery.test.ts`** — consume-before-drain, drain-once (copy subagents' file).
861
-
862
- **`ps.test.ts`** — `reconcileTerminalSelection` behavior (copy `takeover.test.ts` cases).
863
-
864
- **Manual validation (must actually run pi):**
865
- - `pi` → ask the model to `bg_start` a dev-server-like command → widget appears above editor
866
- with correct count/pluralization → `/ps` list → enter detail → live tail scrolls, `t`
867
- toggles stderr, ANSI-heavy output (e.g. `npm run dev`) renders without smearing → back →
868
- `x` kills → widget disappears when last settles → completion message arrives exactly once,
869
- rendered collapsed, expands with ctrl+o.
870
- - Race check: start a 2s `sleep`-then-echo while the model is mid-long-turn → result arrives
871
- as follow-up after the turn, not mid-stream, and only once.
872
- - `/new` and `/reload` with a running process → process is dead afterwards (`ps aux | grep`),
873
- no orphan, widget cleared.
874
- - `npm run check` green; `npm test` green; repo-root `npm run format:check` clean for the new
875
- files (prettier covers `extensions/**/*.ts`).
876
-
877
- ## 15. Pitfalls (each burned someone in the reference code)
878
-
879
- 1. **Effect v3 API names don't exist** — `Effect.fork`, `Effect.async`, `Either`,
880
- `Layer.scoped`, `Context.Tag`. Check every API against effect-v4-notes.md before writing it.
881
- 2. **`Queue.end` needs `Cause.Done` in the error type** — only relevant if you add a queue;
882
- this design avoids queues entirely.
883
- 3. **Don't render raw process output** — ANSI/tabs/control chars desync the TUI
884
- (transcript.ts's `sanitizeText` comment). Sanitize at render, never at capture.
885
- 4. **Don't repaint per data chunk** — 50ms debounce (TakeoverView) or the UI starves input.
886
- 5. **Overlay components are disposed on close** — never cache and re-show; re-invoke
887
- `ctx.ui.custom` (tui.md Overlay Lifecycle). Make `cleanup()` idempotent with a `closed`
888
- flag and clear every timer in it.
889
- 6. **`detached` + group kill or you orphan grandchildren** — `sh -c "npm run dev"` without
890
- process-group SIGTERM leaves node servers running after pi exits (codex.ts `killTree`
891
- comment).
892
- 7. **Settle must be idempotent and single-sourced** — kill vs exit vs error events race;
893
- `if (status !== "running") return` in settle. Set `killSignaled` atomically with SIGTERM
894
- only while the shell is live; an already-observed natural exit keeps `done`/`failed` even
895
- if its surviving process group still needs cleanup.
896
- 8. **Never queue messages into a dying session** — `disposed` guard around `onSettled`, and
897
- try/catch around `pi.sendMessage` (workflows wraps its follow-up send in try/catch:
898
- "Session may be shutting down").
899
- 9. **Defer a copy, not the live snapshot** — the buffer keeps mutating after settle (late
900
- flushes); subagents defers `{ ...snap, meta: { ...snap.meta } }` for the same reason.
901
- 10. **Synchronous reservation for the cap** — an `await` between check and increment lets
902
- parallel tool calls race past it (manager.ts spawn comment).
903
- 11. **Bound every teardown wait** — 5s timeout on scope closes, or a wedged child hangs
904
- `session_shutdown` (subagents `disposeAll` + `abortEntry` comments).
905
- 12. **Snapshot kill interest before Deferred completion** — Effect can resume kill waiters
906
- immediately; compute `consumed` before `Deferred.doneUnsafe` so their `ensuring`
907
- blocks cannot release interest first.
908
- 13. **Tool output limits are a hard requirement** — unbounded stdout in a tool result causes
909
- context overflow/compaction failures (docs Output Truncation). Truncate *everything* the
910
- model sees, including the completion message.
911
- 14. **`prepareArguments` is not needed v1** — but never rename/retype `bg_*` parameters later
912
- without adding it (resumed sessions replay old tool calls; docs Tool Definition).
913
- 15. **`hasUI`/`mode` guards** — widget + `/ps` must no-op gracefully in print/RPC modes.
914
-
915
- ## 16. Acceptance checklist
916
-
917
- - [ ] `npm install && npm run check` green in `extensions/background-terminals` (TS7 + Effect LS).
918
- - [ ] `npm test` green (manager, output, result-delivery, ps selection).
919
- - [ ] Tools registered: `bg_start`, `bg_status`, `bg_list`, `bg_kill`; descriptions document
920
- no-stdin, session-scoped lifetime, and truncation limits; no stdin/steer surface exists.
921
- - [ ] stdout and stderr captured separately and completely (in-memory tail + spill file);
922
- `/ps` detail can inspect both, read-only, scrollable, ANSI-sanitized, live-tailing.
923
- - [ ] Every model-visible output path truncated (`truncateTail` + clamps) with pointers to the
924
- full log.
925
- - [ ] Exactly-once async completion notification via `sendMessage followUp + triggerTurn`,
926
- deferred-delivery map, consumed-set for kill, `agent_settled` flush, `isIdle()` fast
927
- path, `disposed` guard. No polling anywhere.
928
- - [ ] Widget above editor only while ≥1 running, text `N background terminals running • /ps to
929
- view`, cleared on last settle and on shutdown.
930
- - [ ] `/ps` two-stage overlay: list (select/kill/open) → detail (metadata, stdout/stderr
931
- toggle, scroll, back), matching subagents/workflows interaction conventions and hint
932
- lines from `keybindings.getKeys`.
933
- - [ ] Kill terminates the whole process tree (SIGTERM → 2s → SIGKILL), records exit
934
- code/signal, resolves only after settle.
935
- - [ ] `session_shutdown` (quit/reload/new/resume/fork) kills all processes within bounded
936
- time via `runtime.dispose()`; no orphans; no messages sent during teardown.
937
- - [ ] Completed entries retained (≤ MAX_TRACKED, pruned oldest-settled) and visible in
938
- `bg_list` + `/ps`; running entries never pruned.
939
- - [ ] Concurrency cap enforced race-free; ids are `bt-N`; cwd resolved against `ctx.cwd` and
940
- validated; timestamps and elapsed rendering consistent with subagents.
941
- - [ ] Code style: model strings in `prompt.ts`, Effect only in the async core, plain TS
942
- callbacks for stream plumbing, no `as any`, prettier-clean.
1
+ # background-terminals — Implementation Guide
2
+
3
+ > Research phase output. Updated 2026-07-24 against:
4
+ > - `effect@4.0.0-beta.101` (verified installed in this package's `node_modules/effect`; the
5
+ > `unstable/process` module exists there but we deliberately do NOT use it — see §6)
6
+ > - `@earendil-works/pi-coding-agent@^0.82.0` docs at
7
+ > `/Users/davis/.vite-plus/js_runtime/node/24.18.0/lib/node_modules/@earendil-works/pi-coding-agent/docs/`
8
+ > - Reference implementations: `extensions/subagents` (Effect v4 service/manager/read-model/tools)
9
+ > and `extensions/workflows` (dashboard UI, status line, background completion follow-ups).
10
+ >
11
+ > Read alongside `extensions/subagents/docs/effect-v4-notes.md` (API cheat sheet) and
12
+ > `extensions/subagents/docs/effect-v4-extension-guide.md` (toolchain + ManagedRuntime boundary).
13
+ > Those two documents are authoritative for Effect v4 API names — do not use v3 APIs
14
+ > (`Effect.fork`, `Effect.async`, `Either`, `Context.Tag`, `Mailbox`, `ServiceMap` are all
15
+ > wrong; use `forkChild`/`forkDetach`, `Effect.callback`, `Result`, `Context.Service`, `Queue`).
16
+
17
+ ## 1. What this extension is
18
+
19
+ The model can start long-running shell processes ("background terminals"), keep working while
20
+ they run, check on them, and stop them. It can **never** write to a running process's stdin —
21
+ processes are launched with `stdin: "ignore"`; there is no send/steer surface at all (this is
22
+ the key simplification vs. subagents' `send()`).
23
+
24
+ - Full stdout and stderr are captured **separately and completely** in private spill files;
25
+ bounded in-memory tails keep `/ps` responsive (§7.4).
26
+ - Tool responses to the model are **always truncated** with the pi truncation utilities.
27
+ - When a process exits, the model is woken **exactly once** via `pi.sendMessage(...,
28
+ { deliverAs: "followUp", triggerTurn: true })` — no polling — using the same
29
+ deferred-delivery/consumed dance as subagents (§9).
30
+ - While ≥1 process is running, a one-line widget renders **directly above the editor**:
31
+ `N background terminal(s) running • /ps to view` (§10).
32
+ - `/ps` opens a two-stage full-screen overlay (list → detail with scrollable stdout/stderr),
33
+ modeled on `extensions/subagents/src/ui/takeover.ts` and
34
+ `extensions/workflows/dashboard.ts` (§11).
35
+
36
+ ## 2. Directory / file architecture
37
+
38
+ Mirror the subagents layout exactly (it is the known-green reference; `npm run check` passes
39
+ there against the pinned toolchain):
40
+
41
+ ```
42
+ extensions/background-terminals/
43
+ ├── package.json # exact pins, see §3
44
+ ├── tsconfig.json # extends ../../tsconfig.json + effect LS plugin
45
+ ├── index.ts # extension edge: tools, command, widget, events (plain TS + runTool)
46
+ ├── docs/
47
+ │ └── implementation-guide.md (this file)
48
+ ├── src/
49
+ │ ├── domain.ts # types, status union, errors, formatting helpers
50
+ │ ├── manager.ts # TerminalManager Context.Service + Layer (the Effect core)
51
+ │ ├── output.ts # OutputBuffer: bounded decoded text + byte counters (plain TS class)
52
+ │ ├── runtime.ts # ManagedRuntime factory + runTool helper (copy of subagents')
53
+ │ ├── prompt.ts # all model-facing strings (tool descriptions, result builders)
54
+ │ ├── result-delivery.ts # deferred one-shot delivery map (copy of subagents')
55
+ │ └── ui/
56
+ │ ├── ps.ts # /ps picker + detail view components
57
+ │ └── output-view.ts # stdout/stderr → wrapped display lines
58
+ ├── manager.test.ts # node:test end-to-end through a real ManagedRuntime
59
+ ├── output.test.ts # OutputBuffer truncation/decoding unit tests
60
+ ├── result-delivery.test.ts # (copied semantics, tiny)
61
+ └── ps.test.ts # selection-reconciliation tests (like takeover.test.ts)
62
+ ```
63
+
64
+ Tests live at the package root, plain `node --test --experimental-strip-types`, exactly like
65
+ `extensions/subagents/package.json`'s `test` script. Note the repo-root `package.json` test
66
+ script (`node --test --experimental-strip-types extensions/*/*.test.ts`) will automatically
67
+ pick these up.
68
+
69
+ ## 3. Toolchain (copy exactly, per effect-v4-extension-guide.md §1)
70
+
71
+ `package.json`:
72
+
73
+ ```jsonc
74
+ {
75
+ "name": "background-terminals",
76
+ "private": true,
77
+ "type": "module",
78
+ "scripts": {
79
+ "check": "tsc --noEmit -p .",
80
+ "prepare": "effect-tsgo patch",
81
+ "test": "node --test --experimental-strip-types manager.test.ts output.test.ts result-delivery.test.ts ps.test.ts"
82
+ },
83
+ "dependencies": {
84
+ "effect": "^4.0.0-beta.99"
85
+ },
86
+ "devDependencies": {
87
+ "@effect/tsgo": "^0.24.2",
88
+ "typescript": "^7.0.2"
89
+ }
90
+ }
91
+ ```
92
+
93
+ `tsconfig.json` — identical to `extensions/subagents/tsconfig.json`:
94
+
95
+ ```jsonc
96
+ {
97
+ "extends": "../../tsconfig.json",
98
+ "compilerOptions": { "plugins": [{ "name": "@effect/language-service" }] },
99
+ "include": ["index.ts", "src/**/*.ts", "*.test.ts"]
100
+ }
101
+ ```
102
+
103
+ Per AGENTS.md: add deps with an install command (`npm install effect@^4.0.0-beta.99`),
104
+ run `npm run check` when done, avoid explicit return types unless needed, no `as any`.
105
+ Verification runs from inside `extensions/background-terminals/` only — never root scripts
106
+ (house rule, effect-v4-extension-guide.md §7/§8).
107
+
108
+ Note: we do **not** need `@effect/platform-node`. Subagents' codex backend uses raw
109
+ `node:child_process` `spawn` inside Effect and that is the right model here too (§6).
110
+
111
+ ## 4. Domain model (`src/domain.ts`)
112
+
113
+ Follow `extensions/subagents/src/domain.ts` (readonly interfaces, `Data.TaggedError`, status
114
+ string union, mutable-snapshot-behind-readonly-view trick lives in the manager).
115
+
116
+ ```ts
117
+ import { Data } from "effect";
118
+
119
+ export type TerminalStatus = "running" | "done" | "failed" | "killed";
120
+ // "done" = exited with code 0
121
+ // "failed" = exited non-zero, or spawn-level runtime error after start
122
+ // "killed" = terminated by bg_kill, UI kill, or session teardown
123
+
124
+ export interface TerminalSnapshot {
125
+ readonly id: string; // "bt-1", "bt-2", ... (manager counter, like "sa-N")
126
+ readonly command: string; // exactly what the model asked to run (display string)
127
+ readonly title: string; // short model-provided name, shown in UI (<=80 chars)
128
+ readonly cwd: string; // resolved absolute cwd the process runs in
129
+ readonly pid?: number; // undefined only if spawn itself failed
130
+ readonly status: TerminalStatus;
131
+ readonly createdAt: number; // Date.now() at spawn
132
+ readonly settledAt?: number; // Date.now() at exit/kill
133
+ readonly exitCode?: number; // null-safe: only set when exited via exit code
134
+ readonly signal?: string; // e.g. "SIGTERM" when terminated by signal
135
+ readonly errorText?: string; // spawn error / kill-escalation notes, bounded
136
+ // Live output views (see src/output.ts):
137
+ readonly stdout: OutputView;
138
+ readonly stderr: OutputView;
139
+ }
140
+
141
+ export interface OutputView {
142
+ readonly text: string; // decoded, possibly head-trimmed text (bounded)
143
+ readonly totalBytes: number; // true total bytes ever received
144
+ readonly truncatedBytes: number; // bytes dropped from the head (0 = complete)
145
+ readonly spillPath?: string; // on-disk full capture, when spilling engaged (§7.6)
146
+ }
147
+
148
+ export class SpawnError extends Data.TaggedError("SpawnError")<{
149
+ readonly message: string;
150
+ }> {}
151
+ export class ConcurrencyLimitError extends Data.TaggedError("ConcurrencyLimitError")<{
152
+ readonly message: string;
153
+ }> {}
154
+ export class UnknownTerminalError extends Data.TaggedError("UnknownTerminalError")<{
155
+ readonly message: string;
156
+ }> {}
157
+
158
+ export function formatElapsed(snap: TerminalSnapshot) { /* copy from subagents domain.ts */ }
159
+ ```
160
+
161
+ ### State transitions
162
+
163
+ ```
164
+ spawn ok exit code 0
165
+ (none) ────────► running ───────────────────────► done
166
+ │ exit code ≠0 / 'error' event
167
+ ├─────────────────────────────► failed
168
+ │ bg_kill / UI x / session_shutdown
169
+ └─────────────────────────────► killed
170
+ spawn throws (ENOENT etc.) → tool call fails; NO entry is tracked (SpawnError to the model)
171
+ ```
172
+
173
+ Terminal states are final; there is no restart (unlike subagents' `send()` restart). A killed
174
+ process that raced an exit event keeps whichever settle landed first — settle must be
175
+ idempotent (`if (s.status !== "running") return;`, exactly like `settle()` in
176
+ `extensions/subagents/src/manager.ts`).
177
+
178
+ Timestamps: `createdAt`/`settledAt` are `Date.now()` millis (matches subagents; `formatElapsed`
179
+ consumes them). Exit status: record **both** `exitCode` (number | undefined) and `signal`
180
+ (string | undefined) from Node's `exit (code, signal)` callback — exactly one is non-null per
181
+ Node semantics; render "exit 0", "exit 137", or "SIGKILL" accordingly.
182
+
183
+ ## 5. Effect architecture (`src/runtime.ts`, `src/manager.ts`)
184
+
185
+ ### 5.1 Runtime boundary
186
+
187
+ Copy `extensions/subagents/src/runtime.ts` nearly verbatim (it is only 53 lines):
188
+
189
+ ```ts
190
+ import { Cause, Exit, ManagedRuntime, type Effect } from "effect";
191
+ import { TerminalManagerLive } from "./manager.ts";
192
+
193
+ export function createTerminalRuntime() {
194
+ return ManagedRuntime.make(TerminalManagerLive);
195
+ }
196
+ export type TerminalRuntime = ReturnType<typeof createTerminalRuntime>;
197
+
198
+ export async function runTool<A, E>(
199
+ runtime: TerminalRuntime,
200
+ effect: Effect.Effect<A, E>,
201
+ options: { signal?: AbortSignal; interruptMessage?: string } = {},
202
+ ) {
203
+ const exit = await runtime.runPromiseExit(
204
+ effect,
205
+ options.signal ? { signal: options.signal } : undefined,
206
+ );
207
+ if (Exit.isSuccess(exit)) return exit.value;
208
+ if (Cause.hasInterruptsOnly(exit.cause)) {
209
+ throw new Error(options.interruptMessage ?? "Operation was aborted.");
210
+ }
211
+ const [first] = Cause.prettyErrors(exit.cause);
212
+ throw new Error(first?.message ?? Cause.pretty(exit.cause));
213
+ }
214
+ ```
215
+
216
+ No `BackendRegistry` layer is needed — there is exactly one "backend" (node spawn), so
217
+ `AppLayer` is just `TerminalManagerLive`.
218
+
219
+ `index.ts` builds the runtime lazily and disposes it on `session_shutdown`, exactly like
220
+ `extensions/subagents/index.ts` lines 128–222:
221
+
222
+ ```ts
223
+ let runtime: TerminalRuntime | undefined;
224
+ let managerPromise: Promise<TerminalManagerShape> | undefined;
225
+ const getRuntime = () => (runtime ??= createTerminalRuntime());
226
+ const getManager = () => {
227
+ managerPromise ??= getRuntime().runPromise(TerminalManager).then((manager) => {
228
+ manager.view.setOnSettled(onSettled);
229
+ unsubStatus?.();
230
+ unsubStatus = manager.view.subscribe(() => updateWidget(manager));
231
+ updateWidget(manager);
232
+ return manager;
233
+ });
234
+ return managerPromise;
235
+ };
236
+ ```
237
+
238
+ ### 5.2 TerminalManager service (`src/manager.ts`)
239
+
240
+ One `Context.Service` holding a plain `Map<string, Entry>` plus the synchronous read model
241
+ (the exact structure of `SubagentManager` — see `extensions/subagents/src/manager.ts`, which
242
+ is the single most important file to imitate):
243
+
244
+ ```ts
245
+ export interface TerminalManagerShape {
246
+ start(options: StartOptions): Effect.Effect<TerminalSnapshot, SpawnError | ConcurrencyLimitError>;
247
+ status(id: string): Effect.Effect<TerminalSnapshot, UnknownTerminalError>;
248
+ readonly list: Effect.Effect<ReadonlyArray<TerminalSnapshot>>;
249
+ kill(ids: ReadonlyArray<string>): Effect.Effect<ReadonlyArray<KillResult>>; // resolves when settled
250
+ readonly disposeAll: Effect.Effect<void>;
251
+ readonly view: TerminalReadModel; // synchronous bridge for the TUI + widget
252
+ }
253
+
254
+ export class TerminalManager extends Context.Service<TerminalManager, TerminalManagerShape>()(
255
+ "background-terminals/TerminalManager",
256
+ ) {}
257
+
258
+ export const TerminalManagerLive: Layer.Layer<TerminalManager> =
259
+ Layer.effect(TerminalManager, makeManager);
260
+ ```
261
+
262
+ `makeManager = Effect.gen(function* () { ... })` closes over:
263
+
264
+ - `const entries = new Map<string, Entry>()` — mutable snapshot per entry (readonly view out).
265
+ - `const listeners = new Set<() => void>()` + `notify()` — any-change subscription for the
266
+ widget and `/ps` list, with try/catch around each UI listener.
267
+ - One `Deferred<void>` per entry, completed synchronously and exactly once by `settle()`.
268
+ Every `kill()` caller awaits the Deferreds for entries that were running when it began.
269
+ - A scoped `FiberSet.runtime` bridge for fire-and-forget UI kills, process-event settlement,
270
+ and pruning. Completed fibers remove themselves; disposal waits for the set within a bound,
271
+ and scope close interrupts cleanup still live after that bound.
272
+ - `let counter = 0` for ids; `let disposed = false`; `waitInterest` is NOT needed (there is no
273
+ `bg_wait` tool in v1 — see §8 note), but the "consumed" concept still applies to `bg_kill`
274
+ and `bg_status` so a settle isn't double-announced (§9.3).
275
+ - `yield* Effect.addFinalizer(() => disposeAll)` — the safety net so `runtime.dispose()` in
276
+ `session_shutdown` kills every process even if the extension forgot (subagents manager.ts
277
+ line 657).
278
+
279
+ Concurrency cap: subagents caps at `MAX_RUNNING = 4` with a synchronous reservation
280
+ (`Effect.suspend` before the first yield so parallel tool calls cannot race the check —
281
+ manager.ts lines 364–383). For terminals use `MAX_RUNNING = 8` (processes are cheaper than
282
+ agents) and the same reservation pattern; and `MAX_TRACKED = 32` completed entries retained,
283
+ pruned oldest-settled-first exactly like `pruneSettled()` (never prune running entries).
284
+
285
+ ### 5.3 Where Effect fibers/queues/etc. do and don't earn their keep
286
+
287
+ Per effect-v4-extension-guide.md §0 the async core is Effect; per the codex backend precedent
288
+ the Node stream plumbing stays plain callbacks. Concretely:
289
+
290
+ - **Yes Effect:** the manager service/layer, `start` reservation, per-entry `Deferred`,
291
+ `kill` (timeout + escalation + Deferred wait), scoped `FiberSet` cleanup, `disposeAll`
292
+ (parallel bounded teardown), `runTool` boundary, and `Effect.addFinalizer`.
293
+ - **Plain TS callbacks:** `child.stdout.on("data")`, `child.on("exit")` handlers mutate the
294
+ entry snapshot and call `notify()` directly. This is exactly what the codex backend does with
295
+ its JSON-RPC stdout pump (`codex.ts` lines ~820–860). Do NOT build a
296
+ `Queue<SubagentEvent>`/pump-fiber pipeline here — subagents needs that because three
297
+ heterogeneous backends normalize into one event stream; a single spawn does not.
298
+
299
+ ## 6. Node child_process design (the core of `start`)
300
+
301
+ Model on `makeCodexSession` in `extensions/subagents/src/backends/codex.ts` (spawn options,
302
+ kill-tree, terminate-with-escalation), minus the JSON-RPC machinery:
303
+
304
+ ```ts
305
+ import { spawn } from "node:child_process";
306
+
307
+ const child = yield* Effect.try({
308
+ try: () =>
309
+ spawn(shellPath, ["-c", options.command], {
310
+ cwd: options.cwd,
311
+ env: process.env,
312
+ stdio: ["ignore", "pipe", "pipe"], // ← stdin IGNORED: no input surface, ever
313
+ detached: process.platform !== "win32", // own process group on POSIX → group kill
314
+ }),
315
+ catch: (error) => new SpawnError({ message: boundedError(error) }),
316
+ });
317
+ ```
318
+
319
+ Decisions and rationale:
320
+
321
+ - **Shell execution.** The model supplies one `command` string; run it through the platform
322
+ shell (`/bin/sh -c` on POSIX, `cmd.exe /d /s /c` on Windows) so pipes/redirection work. Honor the
323
+ user's configured shell if convenient (`~/.pi/agent/settings.json` has
324
+ `"shellPath": ".../zsh-with-rc"`), but `/bin/sh` is an acceptable v1 — document which you
325
+ pick in the tool description. Never `shell: true` with an args array (double-parse trap).
326
+ - **`stdin: "ignore"`** enforces the "no subsequent input" requirement at the OS level. A
327
+ process that tries to read stdin gets EOF immediately, which is the honest contract (and the
328
+ tool description must say so — interactive commands will exit or hang, and `bg_kill` is the
329
+ remedy).
330
+ - **`detached: true` on POSIX** gives the child its own process group, so kill can signal
331
+ `-pid` and take down the whole tree (grandchildren from `npm run dev` etc.). `killTree`
332
+ keeps the direct-signal fallback when the group is gone; Windows uses `taskkill /T` and
333
+ adds `/F` for the force-kill phase. `terminateChild` uses Effect
334
+ callbacks/timeouts: SIGTERM now, SIGKILL after 2s if needed, then a final 500ms bound.
335
+ Do NOT call `child.unref()` — we want the exit event, and pi owns the lifetime anyway.
336
+ - **Spawn failure semantics.** `spawn()` itself rarely throws; ENOENT arrives via
337
+ `child.once("error", ...)`. Wire the error handler *before* returning from `start`, and treat
338
+ an error event pre-exit as settling the entry to `failed` with `errorText` (mirror
339
+ `failForProcessExit` in codex.ts). To catch instant failures, you may optionally wait one
340
+ tick for `spawn` event vs `error` event, but simplest correct behavior: register the entry
341
+ immediately as `running` and let the near-instant `error`/`exit` settle it; the model gets
342
+ the settle notification milliseconds later.
343
+ - **Exit handling** (single source of truth for settling):
344
+
345
+ ```ts
346
+ child.once("exit", (code, signal) => {
347
+ finishOutput(entry); // flush any pending partial decode
348
+ settle(entry, {
349
+ status: entry.killSignaled ? "killed" : code === 0 ? "done" : "failed",
350
+ exitCode: code ?? undefined,
351
+ signal: signal ?? undefined,
352
+ });
353
+ });
354
+ ```
355
+
356
+ `killSignaled` is set in the same synchronous effect that sends SIGTERM, so a process that
357
+ exits before signaling keeps its natural status while a signaled process reports `killed`.
358
+ Settle is idempotent (§4).
359
+ - **cwd semantics.** The tool takes optional `working_dir`; resolve with
360
+ `path.resolve(ctx.cwd, params.working_dir ?? ".")` and validate
361
+ `fs.existsSync(cwd) && fs.statSync(cwd).isDirectory()` in the tool handler *before* touching
362
+ the runtime — throw a plain Error otherwise. This is copied from `subagent_spawn`'s handler
363
+ (`extensions/subagents/index.ts` lines 262–265). No trust-store logic is needed (we are not
364
+ spawning an agent in another project; a shell command in another directory is equivalent to
365
+ what the bash tool already allows).
366
+
367
+ ### 6.1 Why not `effect/unstable/process` yet?
368
+
369
+ `ChildProcess.make` + `ChildProcessHandle` is the eventual target, but the current Effect beta
370
+ cannot preserve the current process contract yet:
371
+
372
+ 1. `forceKillAfter` does not correctly wait before SIGKILL on POSIX in this pin.
373
+ 2. `ChildProcessHandle.exitCode` does not expose the actual terminating signal, while the
374
+ public snapshot and model-facing output distinguish `SIGTERM` from `SIGKILL`.
375
+
376
+ This first pass therefore keeps raw spawn and stream callbacks, while moving termination
377
+ waits, escalation deadlines, settlement coordination, and cleanup ownership into Effect.
378
+ Do not add `@effect/platform-node` until both blockers can be resolved.
379
+
380
+ ## 7. Output capture (`src/output.ts`)
381
+
382
+ ### 7.1 Requirements recap
383
+
384
+ Capture stdout and stderr **separately** and **completely** (the user's "full stdout/stderr"),
385
+ viewable in `/ps`; tool responses truncated; memory must be bounded.
386
+
387
+ ### 7.2 Decoding — do it right
388
+
389
+ Do NOT use `child.stdout.setEncoding("utf8")` naïvely-per-chunk... actually `setEncoding`
390
+ internally uses a StringDecoder and *is* multibyte-safe across chunk boundaries, which is why
391
+ codex.ts can use it. Two acceptable options; pick (a):
392
+
393
+ - (a) `child.stdout.setEncoding("utf8")` and receive `string` chunks (Node handles split
394
+ UTF-8 sequences). Simplest, matches codex.ts line ~824.
395
+ - (b) accumulate `Buffer`s and decode with `new (await import("node:string_decoder")).StringDecoder("utf8")`.
396
+
397
+ Either way, strip nothing at capture time — raw text goes into the buffer; ANSI/control
398
+ sanitization happens at *render* time using `sanitizeText` (copy from
399
+ `extensions/subagents/src/ui/transcript.ts` lines 15–29; it exists precisely because raw ANSI
400
+ desyncs the TUI renderer).
401
+
402
+ ### 7.3 OutputBuffer (bounded ring with head-drop + optional spill)
403
+
404
+ ```ts
405
+ export class OutputBuffer {
406
+ private chunks: string[] = [];
407
+ private bytes = 0; // bytes currently retained (Buffer.byteLength of chunks)
408
+ totalBytes = 0; // true total ever received
409
+ truncatedBytes = 0; // dropped from the head
410
+ spillPath?: string;
411
+
412
+ constructor(private maxRetainedBytes: number, private spill?: (chunk: string) => void) {}
413
+
414
+ push(chunk: string) {
415
+ /* Count and spill the complete chunk first. If the chunk alone exceeds
416
+ maxRetainedBytes, discard older retained chunks and UTF-8-safely trim
417
+ this chunk to its newest cap-sized tail. Otherwise append it and evict
418
+ older whole chunks until retained bytes fit. Every discarded byte
419
+ increments truncatedBytes; totalBytes counts the original input. */
420
+ }
421
+ view(): OutputView { /* { text: this.chunks.join(""), totalBytes, truncatedBytes, spillPath } */ }
422
+ }
423
+ ```
424
+
425
+ Cache the `join("")` and invalidate on push so the 1Hz UI tick doesn't re-join megabytes.
426
+
427
+ ### 7.4 Memory bounds vs "full inspection" — the honest tradeoff
428
+
429
+ Unbounded retention of a `yes`-style firehose is a hard memory leak (codex.ts caps its stderr
430
+ retain at 4 KiB and treats an unbounded protocol buffer as session-fatal for exactly this
431
+ reason). Resolution:
432
+
433
+ - **In-memory retained cap: 2 MiB per stream per process** (so ≤ 8 procs × 2 streams × 2 MiB =
434
+ 32 MiB worst case). The newest output is always retained; the head is dropped.
435
+ - **Spill-to-disk for the full capture** (this is what makes "full stdout/stderr" true even
436
+ past the cap): create the shared/session directories with owner-only `0700` permissions,
437
+ then open two `0600` append-mode `WriteStream`s under
438
+ ``path.join(os.tmpdir(), "pi-background-terminals", sessionId, `${id}.stdout.log`)`` (and
439
+ `.stderr.log`). A `WriteStream` serializes writes per stream; settlement ends and awaits
440
+ both streams behind a bounded flush barrier before publishing the result. A stream error or
441
+ flush timeout clears the affected full-log pointer and surfaces a bounded `errorText` note.
442
+ The `/ps` detail view shows the in-memory tail and, when `truncatedBytes > 0`, a header line
443
+ "first N KiB dropped from view — full log: <spillPath>"; model-facing results reference the
444
+ same path. `disposeAll` removes the private session spill directory after all entry scopes
445
+ and spill flushes complete, so secret-bearing logs do not outlive the owning pi session.
446
+ - Precedent for "truncate + point at the full file": docs/extensions.md "Output Truncation"
447
+ section recommends exactly this shape for tool results.
448
+
449
+ ### 7.5 Entry wiring inside `start`
450
+
451
+ Per entry, like subagents' `spawn` (manager.ts lines 385–466):
452
+
453
+ ```ts
454
+ const scope = yield* Scope.make();
455
+ const settled = yield* Deferred.make<void>();
456
+ // finalizer kills the tree; registered in the scope so BOTH kill() and disposeAll()
457
+ // and runtime.dispose() converge on one teardown path:
458
+ yield* Scope.provide(
459
+ Effect.addFinalizer(() =>
460
+ Effect.gen(function* () {
461
+ yield* terminateChild(child, () => entry.stdioClosed, markKillSignaled);
462
+ yield* Deferred.await(settled).pipe(
463
+ Effect.timeout(SETTLE_GRACE_MS),
464
+ Effect.ignore,
465
+ );
466
+ // If still running, flush output within its bound and settle here.
467
+ }),
468
+ ),
469
+ scope,
470
+ );
471
+ entries.set(id, { snapshot, child, scope, stdoutBuf, stderrBuf, settled });
472
+ ```
473
+
474
+ `kill(ids)` then is: `Scope.close(entry.scope, Exit.void)` (bounded with
475
+ `Effect.timeout(STOP_TIMEOUT_MS)` + `Effect.ignore`) in the scoped cleanup `FiberSet`, then
476
+ await every captured entry's `Deferred`. Return per-id `{ id, status, killed: boolean }`
477
+ results and treat already-settled ids as no-ops rather than errors.
478
+
479
+ `disposeAll`: set `disposed = true`, snapshot `[...entries.values()]`, close every scope with
480
+ `{ concurrency: "unbounded" }` and a 5s timeout each — verbatim subagents `disposeAll`
481
+ (manager.ts lines 596–618).
482
+
483
+ ### 7.6 Race conditions checklist (each has a subagents precedent)
484
+
485
+ - **Spawn vs concurrent spawn past the cap** → synchronous reservation before first yield
486
+ (`reserved++` inside `Effect.suspend`; decrement in `Effect.ensuring`).
487
+ - **Kill vs natural exit** → idempotent `settle` with one authoritative precedence rule. If
488
+ kill reaches a live shell, set `killSignaled` in the same effect that signals it and report
489
+ `killed`. If the shell's `exit` event was already observed, preserve its natural
490
+ `done`/`failed` status even when cleanup must still signal descendants holding stdio open.
491
+ A missing `close` after `exit` starts a bounded grace, then closes the entry scope so the
492
+ surviving process group is terminated and the entry cannot occupy a running slot forever.
493
+ - **Exit event vs scope close ("stream ended unexpectedly")** → we have no pump, so this class
494
+ disappears; the only settle source is the `exit`/`error` listener.
495
+ - **Settle during teardown** → `if (!disposed) onSettled?.(...)` so a result is never queued
496
+ into a shutting-down session (subagents `settle`, manager.ts line 280).
497
+ - **Tool AbortSignal during `bg_kill`'s wait** → interruption stops only that caller's
498
+ `Deferred.await`; the detached scope-close stays owned by the manager `FiberSet`, and
499
+ `Effect.ensuring` still releases bookkeeping.
500
+ - **Late output after exit** → Node may still flush 'data' after 'exit' is observed in rare
501
+ orderings; buffers accept pushes until `close` — harmless because settle doesn't freeze the
502
+ buffer, and the UI just shows more text. (Optionally listen on `close` instead of `exit` to
503
+ be strictly after stdio flush; `close` fires when stdio streams end — prefer `close` for
504
+ settling to guarantee complete output at notification time, and keep `exit` only to record
505
+ code/signal. This is the one place we improve on codex.ts, which doesn't need output
506
+ completeness.)
507
+
508
+ **Recommended:** record `{code, signal}` on `exit`, settle + notify on `close`. This
509
+ guarantees the completion follow-up message contains the final output tail.
510
+
511
+ ## 8. Tools (`index.ts` + `src/prompt.ts`)
512
+
513
+ All model-facing strings live in `src/prompt.ts` (subagents convention). Register with
514
+ `pi.registerTool`; parameters via `typebox` `Type.Object`; use `StringEnum` from
515
+ `@earendil-works/pi-ai` if any enum appears (Google-compat rule, docs/extensions.md
516
+ "Tool Definition"). Throw plain `Error` for failures (that is what sets `isError`).
517
+
518
+ ### 8.1 `bg_start`
519
+
520
+ ```ts
521
+ parameters: Type.Object({
522
+ command: Type.String({ description: "Shell command line to run in the background (sh -c on POSIX, cmd.exe /d /s /c on Windows). It receives no stdin (EOF immediately); interactive commands will not work." }),
523
+ title: Type.String({ description: "Short human-readable name shown in listings and the UI" }),
524
+ working_dir: Type.Optional(Type.String({ description: "Working directory (default: current working directory)" })),
525
+ })
526
+ ```
527
+
528
+ Handler: validate cwd (§6), `title.trim().slice(0, 80) || "terminal"`, then
529
+ `runTool(getRuntime(), manager.start({ command, title, cwd }))`. Result text (build in
530
+ prompt.ts, like `buildSubagentSpawnResult`):
531
+
532
+ ```
533
+ Started background terminal bt-3 "dev server" (pid 12345, /Users/davis/project).
534
+ It runs in the background with no stdin. You'll get a message when it exits, or use
535
+ bg_status(id: "bt-3") to peek, bg_kill to stop it, bg_list to see all.
536
+ ```
537
+
538
+ `promptSnippet`: "Run a long-lived shell command in the background (dev servers, builds,
539
+ watchers); output is captured and you're notified on exit".
540
+ `promptGuidelines` (name the tool explicitly — docs warn "this tool" is ambiguous):
541
+ - "Use bg_start for commands expected to run long or indefinitely (servers, watch modes); use the regular bash tool for quick commands."
542
+ - "bg_start processes receive no stdin — never start a command that requires interactive input."
543
+ - "After bg_start, keep working; the exit result arrives automatically. Use bg_status only when you need current output before continuing."
544
+
545
+ Description documents the truncation limits (docs requirement) and the no-stdin contract.
546
+
547
+ ### 8.2 `bg_status`
548
+
549
+ ```ts
550
+ parameters: Type.Object({ id: Type.String({ description: 'Terminal id, e.g. "bt-1"' }) })
551
+ ```
552
+
553
+ Unknown id → throw with the known-ids list (copy the exact error style from `subagent_check`:
554
+ `Unknown terminal id "x". Known: bt-1, bt-2.`). Result: one metadata line
555
+ (`bt-1 [running] "dev server" (pid 12345, 3m12s, exit -, /path)`) then **tail-truncated**
556
+ stdout and stderr sections:
557
+
558
+ ```ts
559
+ const stdout = truncateTail(snap.stdout.text, { maxBytes: 16 * 1024, maxLines: 400 });
560
+ const stderr = truncateTail(snap.stderr.text, { maxBytes: 8 * 1024, maxLines: 200 });
561
+ ```
562
+
563
+ `truncateTail` (not head) because for process logs the end matters — this is the documented
564
+ guidance in docs/extensions.md Output Truncation. When truncated, append
565
+ `[stdout truncated: showing last X of Y. Full log: <spillPath or "in /ps viewer">]` using
566
+ `formatSize` + the truncation result fields (see `truncatedOutput()` in subagents index.ts for
567
+ the message shape). If `bg_status` observes a settled entry whose completion message is still
568
+ pending delivery, mark it consumed (§9.3).
569
+
570
+ ### 8.3 `bg_list`
571
+
572
+ No parameters. One line per entry via a `describeTerminal(snap)` helper (mirror
573
+ `describeSubagent`): id, status, title, pid, elapsed, exit code/signal, cwd, and total output
574
+ sizes (`formatSize(stdout.totalBytes)`). "No background terminals." when empty. Include both
575
+ running and completed (completed entries are retained up to `MAX_TRACKED`).
576
+
577
+ ### 8.4 `bg_kill`
578
+
579
+ ```ts
580
+ parameters: Type.Object({ ids: Type.Array(Type.String(), { description: 'Terminal ids to stop, e.g. ["bt-1"]' }) })
581
+ ```
582
+
583
+ Validate all ids known first (throw listing unknowns, copy `subagent_cancel`). Then
584
+ `runTool(getRuntime(), manager.kill(ids), { signal, interruptMessage: "Kill wait aborted; termination continues in the background." })`.
585
+ Report per id: `Killed bt-1 "dev server" (SIGTERM).` or `bt-2 "build" was already done (exit 0).`
586
+ Killing marks the settle consumed so the model doesn't also get the async completion message
587
+ (§9.3) — same reason subagents' `cancel` calls `addInterest` before interrupting.
588
+
589
+ **No `bg_wait` and no `bg_send`.** No stdin is a hard requirement. Blocking wait is
590
+ deliberately omitted in v1: completion notification makes it redundant, and it would drag in
591
+ subagents' full `waitInterest` machinery. If it's ever wanted, each entry already has a
592
+ settlement `Deferred` and the subagents `waitFor` result shaping is the template.
593
+
594
+ ## 9. Completion notification — exactly once, no polling, no turn races
595
+
596
+ This is the subtlest requirement. Copy the subagents solution wholesale; it exists precisely
597
+ to solve this problem (see comments in `extensions/subagents/index.ts` lines 168–222 and
598
+ `result-delivery.ts`).
599
+
600
+ ### 9.1 Mechanism
601
+
602
+ On settle, the manager invokes a hook `onSettled(snap, consumed)` registered by `index.ts`
603
+ (same `view.setOnSettled` bridge). The hook:
604
+
605
+ ```ts
606
+ const resultDelivery = createDeferredResultDelivery<TerminalSnapshot>(); // copy the 20-line module
607
+
608
+ const onSettled = (snap: TerminalSnapshot, consumed: boolean) => {
609
+ if (consumed) { resultDelivery.consume([snap.id]); return; }
610
+ // Defer a deep-enough copy: the live snapshot keeps mutating (late output flushes).
611
+ resultDelivery.defer({ ...snap, stdout: { ...snap.stdout }, stderr: { ...snap.stderr } });
612
+ if (sessionContext?.isIdle()) flushResults();
613
+ };
614
+
615
+ pi.on("agent_settled", flushResults);
616
+
617
+ const flushResults = () => {
618
+ for (const snap of resultDelivery.drain()) {
619
+ pi.sendMessage({
620
+ customType: "background-terminal-result",
621
+ content: buildTerminalResultMessage(snap), // prompt.ts; truncateTail'd output inside
622
+ display: true,
623
+ details: { id: snap.id, title: snap.title, status: snap.status, exitCode: snap.exitCode, signal: snap.signal },
624
+ }, { deliverAs: "followUp", triggerTurn: true });
625
+ }
626
+ };
627
+ ```
628
+
629
+ ### 9.2 Why this is race-free (the reasoning to preserve in code comments)
630
+
631
+ - `deliverAs: "followUp"` queues the message until the agent has no more tool calls; it never
632
+ interrupts a mid-turn stream (docs/extensions.md § pi.sendMessage).
633
+ - `triggerTurn: true` wakes the model immediately **iff idle**; if busy, the queued follow-up
634
+ is delivered when the current run settles — either way exactly one delivery.
635
+ - The `Map`-keyed `resultDelivery` (keyed by id, `drain()` clears) makes double-delivery
636
+ structurally impossible even if both the `isIdle()` fast-path and the `agent_settled` event
637
+ fire: whoever drains first wins, the second drain sees an empty map.
638
+ - The `consumed` flag closes the remaining hole: if the model is *currently inside*
639
+ `bg_kill` (which returns the final state itself), the settle must not ALSO queue a message.
640
+ Manager computes `consumed` = "a kill/status collection is in flight for this id" at settle
641
+ time (subagents: `waitInterest`; here: the `kill()`-marked id set).
642
+ - `if (!disposed)` in `settle` prevents queueing into a shutting-down session.
643
+
644
+ ### 9.3 Consumed-set details
645
+
646
+ Keep a `Map<string, number> killInterest` in the manager; `kill()` adds interest before
647
+ signaling and releases in `Effect.ensuring` (identical to `addInterest`/`releaseInterest`).
648
+ `settle` computes `consumed = (killInterest.get(id) ?? 0) > 0`. Additionally, `bg_kill`'s tool
649
+ handler calls `resultDelivery.consume(ids)` after `runTool` returns, mirroring
650
+ `subagent_wait`'s "settlement may have happened before this wait began" comment (index.ts
651
+ line 352) — belt and suspenders for the settled-before-kill-started ordering.
652
+
653
+ ### 9.4 Result message content
654
+
655
+ `buildTerminalResultMessage` (prompt.ts): first line
656
+ `Background terminal bt-3 "dev server" exited (exit 1) after 4m12s.` (or `(SIGTERM)` /
657
+ `was killed`), then tail-truncated stdout (≤ 16 KiB) and, if non-empty, stderr (≤ 8 KiB) in
658
+ labeled sections, with truncation notes pointing at the spill file. Register a
659
+ `pi.registerMessageRenderer("background-terminal-result", ...)` for a collapsed preview —
660
+ copy the subagent-result renderer (index.ts lines 514–561: icon by status, header line,
661
+ 8-line preview, "ctrl+o to expand").
662
+
663
+ ## 10. Widget above the editor
664
+
665
+ Requirement: visible **only while ≥1 process is running**, directly above editor, text
666
+ `N background terminal(s) running • /ps to view`.
667
+
668
+ API: `ctx.ui.setWidget(key, linesOrFactory)` — default placement is already **above the
669
+ editor** (docs/extensions.md "Widgets, Status, and Footer" + tui.md Pattern 5); do NOT pass
670
+ `placement: "belowEditor"`. Clear with `setWidget(key, undefined)`.
671
+
672
+ ```ts
673
+ const updateWidget = (manager: TerminalManagerShape) => {
674
+ if (!ui) return; // captured from session_start ctx.hasUI
675
+ const running = manager.view.list().filter((s) => s.status === "running").length;
676
+ if (running === 0) { ui.setWidget("background-terminals", undefined); return; }
677
+ ui.setWidget("background-terminals", (_tui, theme) => {
678
+ const line =
679
+ theme.fg("warning", "■ ") +
680
+ theme.fg("text", `${running} background terminal${running === 1 ? "" : "s"} running`) +
681
+ theme.fg("dim", " • ") + theme.fg("accent", "/ps") + theme.fg("dim", " to view");
682
+ return { render: () => [line], invalidate: () => {} };
683
+ });
684
+ };
685
+ ```
686
+
687
+ Drive it from `manager.view.subscribe(...)` exactly like subagents drives `setStatus`
688
+ (index.ts lines 139–166) — the subscription fires on every state change, including settles, so
689
+ the widget disappears the moment the last process exits. Guard `ctx.hasUI`; wrap in try/catch
690
+ like workflows' `updateIndicator` ("UI may be unavailable"). Clear the widget in
691
+ `session_shutdown` before disposing the runtime.
692
+
693
+ (Singular/plural: render `1 background terminal running`, `2 background terminals running` —
694
+ implement the requested "terminal(s)" sense as proper pluralization.)
695
+
696
+ ## 11. `/ps` command + two-stage UI (`src/ui/ps.ts`, `src/ui/output-view.ts`)
697
+
698
+ Register `pi.registerCommand("ps", { description: "List and inspect background terminals", handler })`.
699
+ Handler: TUI-mode guard + empty-state notify + open picker — copy the `/subagents` command
700
+ skeleton (index.ts lines 565–587). Non-TUI (`ctx.mode !== "tui"`): print a plain-text listing
701
+ via `ctx.ui.notify` like workflows' non-TUI fallback, or just the notify error like subagents —
702
+ prefer the listing (cheap and useful in RPC mode).
703
+
704
+ ### 11.1 Stage 1 — list (dashboard)
705
+
706
+ Copy `SubagentDashboard` (`src/ui/takeover.ts` lines 109–344) with terminal rows:
707
+
708
+ - Entry point loop `openTerminalPicker(ctx, view)` — the `while (true)` pick→detail→back loop
709
+ of `openSubagentPicker` (lines 52–86), full-screen overlay
710
+ (`{ overlay: true, overlayOptions: { anchor: "center", width: "100%", maxHeight: "100%" } }`).
711
+ - Row left: selection marker, status glyph (`■` warning/success/error — reuse `statusGlyph`
712
+ pattern; map `killed` to muted/error), title, dim id.
713
+ - Row right: `pid 12345 · 3m12s · exit 0` (or `running` / `SIGTERM`), dim separators — the
714
+ `split(left, right, width)` helper from workflows' dashboard is the cleanest to copy.
715
+ - Keys: up/down/j/k select, enter open, `x` kill selected (only when running →
716
+ `view.requestKill(id)` fire-and-forget, precedent: dashboard `x` → `requestAbort`), esc
717
+ close. Hint line built from `keybindings.getKeys(...)` via the `configuredKeys` helper.
718
+ - 1Hz `setInterval` ticker for elapsed times + `view.subscribe` re-render, both cleaned up in
719
+ `dispose()`/`cleanup()` (idempotent closed-flag pattern — copy it exactly; overlay components
720
+ are disposed on close and must not be reused, tui.md "Overlay Lifecycle").
721
+ - Keep list selection stable across refreshes with `reconcileDashboardSelection` (takeover.ts
722
+ lines 95–107) — copy it and its test (`takeover.test.ts`).
723
+
724
+ ### 11.2 Stage 2 — detail (read-only inspector)
725
+
726
+ Copy `TakeoverView` (takeover.ts lines 350–563) **minus the Input line** (read-only: no
727
+ `Focusable`, no `Input`, no `requestSend`). Layout:
728
+
729
+ ```
730
+ ────────────────────────────────────────────────────────────
731
+ ■ bt-3 · dev server · running · 4m12s · pid 12345 · ~/project
732
+ $ npm run dev
733
+ ────────────────────────────────────────────────────────────
734
+ [ tab: stdout (1.2MB) | stderr (4KB) ] ← `t` toggles streams
735
+ ...scrollable output lines (sanitized, wrapped, tail-pinned)...
736
+ ... 120 lines below · ↓/pgdn
737
+ ────────────────────────────────────────────────────────────
738
+ esc back · t stdout/stderr · x kill · ↑/↓ scroll · pgup/pgdn page · g/G top/bottom
739
+ ────────────────────────────────────────────────────────────
740
+ ```
741
+
742
+ - Metadata header: status glyph, id, title, status word, elapsed (`formatElapsed`), pid, cwd,
743
+ exit code/signal when settled, total sizes (`formatSize`), truncation note when
744
+ `truncatedBytes > 0` (with spill path).
745
+ - **stdout/stderr shown separately** (requirement): a `t` key toggles the active stream;
746
+ header tab shows both sizes. (Alternative side-by-side split like workflows' phases/agents
747
+ panels is more code for less readability of wide log lines — use the toggle.)
748
+ - Output rendering (`src/ui/output-view.ts`): split buffer text on `\n`, `sanitizeText` each
749
+ line (copy from transcript.ts — ANSI strip is mandatory or the overlay smears), wrap with
750
+ `wrapTextWithAnsi`, `truncateToWidth`. Scroll state = offset-from-bottom, 0 = pinned to
751
+ bottom so a running process live-tails; clamp `scrollOffset` to `maxOffset` each render
752
+ (TakeoverView lines 510–543 is exactly this fixed-height-viewport math — copy it, including
753
+ the "scroll status consumes a viewport row" trick so height never jumps).
754
+ - Live updates: `view.subscribeTo(id, ...)` per-entry subscription + the 50ms
755
+ `scheduleRender` debounce (TakeoverView lines 406–414 — a chatty process emits a chunk per
756
+ write; do not repaint per chunk).
757
+ - Keys: esc/left back to list (loop re-opens dashboard), `x` kill (running only), scroll keys
758
+ via `keybindings.matches(data, "tui.editor.cursorUp"/"cursorDown"/"pageUp"/"pageDown")` plus
759
+ j/k and g/G (workflows transcript view precedent).
760
+ - Big-buffer perf: with the 2 MiB cap, worst case ~30k lines; recompute wrapped lines only when
761
+ the buffer version or width changed (cache `(version, width) → lines`), not per render tick.
762
+
763
+ ### 11.3 Read model
764
+
765
+ ```ts
766
+ export interface TerminalReadModel {
767
+ list(): ReadonlyArray<TerminalSnapshot>;
768
+ get(id: string): TerminalSnapshot | undefined;
769
+ size(): number;
770
+ subscribe(listener: () => void): () => void;
771
+ subscribeTo(id: string, listener: () => void): () => void;
772
+ requestKill(id: string): void; // fire-and-forget via the scoped FiberSet runtime
773
+ setOnSettled(hook?: (snap: TerminalSnapshot, consumed: boolean) => void): void;
774
+ }
775
+ ```
776
+
777
+ Verbatim shape of `SubagentReadModel` minus `requestSend`. Snapshots are live objects; the UI
778
+ must not mutate them (same doc comment as manager.ts line 89).
779
+
780
+ ## 12. Lifecycle: reload / new / resume / fork / shutdown
781
+
782
+ pi's session replacement flow (docs/extensions.md "Lifecycle Overview" + session_shutdown):
783
+ `/new`, `/resume`, `/fork`, `/reload`, and quit all emit `session_shutdown` (with `event.reason`)
784
+ for the old extension instance, then re-instantiate extensions and emit `session_start`.
785
+ Consequences:
786
+
787
+ - **Processes do not survive any session transition.** In `session_shutdown`: clear
788
+ `resultDelivery`, unsubscribe, clear widget, null the ui/context refs, then
789
+ `await closing?.dispose()` — the ManagedRuntime close runs the manager finalizer →
790
+ `disposeAll` → every entry scope → `terminateChild` (SIGTERM→SIGKILL tree kill). This is
791
+ the identical teardown in subagents index.ts lines 210–222; each scope close is bounded
792
+ (5s timeout) so a wedged process cannot hang shutdown, and SIGKILL covers it anyway.
793
+ - **Spill files do not survive the session either.** `disposeAll` first closes every entry
794
+ scope and awaits bounded spill flushes, then recursively removes its owner-only session
795
+ directory. Paths shown in the old transcript are intentionally session-lifetime pointers.
796
+ - **No persistence / no resurrection.** Unlike workflows (which persists `workflow.json` and
797
+ marks stale "running" runs as aborted on reload — dashboard.ts lines 286–297), v1 keeps no
798
+ cross-session record: killed-on-shutdown processes simply disappear. Optionally append a
799
+ `pi.appendEntry("background-terminals-note", {...})` breadcrumb ("bt-2 'dev server' was
800
+ killed by session shutdown") so a resumed session's transcript explains the vanished
801
+ terminal — cheap and worth doing; entries don't enter LLM context (docs: appendEntry).
802
+ The model-facing story stays consistent because tool results always describe terminals as
803
+ session-scoped ("killed when the session ends" in `bg_start`'s description).
804
+ - **Do not spawn from stale contexts.** All spawning goes through tool handlers with a live
805
+ `ctx`; the manager rejects `start` when `disposed` (SpawnError "shutting down", subagents
806
+ manager.ts lines 370–374 precedent).
807
+ - **Fork/clone:** nothing special — same shutdown+start pair; the new instance starts empty.
808
+
809
+ ## 13. Truncation constants (single place, `index.ts` top)
810
+
811
+ ```ts
812
+ const STATUS_STDOUT_MAX = 16 * 1024; // bg_status stdout tail
813
+ const STATUS_STDERR_MAX = 8 * 1024; // bg_status stderr tail
814
+ const RESULT_STDOUT_MAX = 16 * 1024; // completion follow-up stdout tail
815
+ const RESULT_STDERR_MAX = 8 * 1024;
816
+ const RETAINED_PER_STREAM = 2 * 1024 * 1024; // in-memory cap per stream (spill keeps the rest)
817
+ ```
818
+
819
+ All clamped by `Math.min(..., DEFAULT_MAX_BYTES)` and `DEFAULT_MAX_LINES` (imports from
820
+ `@earendil-works/pi-coding-agent`, verified exported in `dist/index.d.ts`) — same defensive
821
+ clamp as `truncatedOutput` in subagents index.ts. Always `truncateTail` for process output.
822
+
823
+ ## 14. Test plan
824
+
825
+ Follow the house style: `node:test` + `assert/strict`, end-to-end through a real
826
+ `ManagedRuntime`, minimal count, deterministic (subagents `manager.test.ts` is the template,
827
+ including the `withManager` fixture that guarantees `runtime.dispose()` in `finally`).
828
+
829
+ **`output.test.ts`** (pure, no processes)
830
+ 1. push/view roundtrip; totalBytes/truncatedBytes accounting when the cap evicts head chunks.
831
+ 2. multibyte boundary: feeding split UTF-8 via setEncoding path is Node's job, but verify the
832
+ buffer never splits what it was given and byte counts use `Buffer.byteLength`.
833
+ 3. spill callback receives every chunk in order even after eviction.
834
+
835
+ **`manager.test.ts`** (real processes — use `node -e` one-liners for portability, no shell
836
+ tricks; they exist on any machine running pi)
837
+ 1. happy path: `start` node printing to stdout+stderr then exiting 0 → status transitions
838
+ running→done, exitCode 0, both buffers correct and separate, settle hook fired once with
839
+ `consumed: false`.
840
+ 2. non-zero exit → `failed`, exitCode captured.
841
+ 3. `kill` on a `setInterval` never-exiting script → `killed`, signal recorded, `kill()` only
842
+ resolves after settle; second `kill` of same id reports already-settled, no error.
843
+ 4. process-tree termination: spawn a grandchild that updates a unique heartbeat sentinel,
844
+ kill, then use bounded polling with an explicit timeout to confirm both that the process is
845
+ gone and that its unique sentinel stopped changing. The sentinel ties the assertion to the
846
+ spawned child so PID reuse cannot create a false pass.
847
+ 5. concurrency cap: cap+1 concurrent starts → last fails with ConcurrencyLimitError;
848
+ reservation released on spawn failure (start a bogus binary → SpawnError → slot free).
849
+ 6. consumed semantics: settle during an in-flight `kill` reports `consumed: true`.
850
+ 7. `disposeAll` (via `runtime.dispose()`) kills a running process and settles it as killed;
851
+ no settle hook fires after dispose (`disposed` guard).
852
+ 8. pruning: exceed MAX_TRACKED with settled entries → oldest pruned, running never pruned.
853
+ 9. SIGTERM-resistant process → SIGKILL after the 2s grace, within the 5s close bound.
854
+ 10. aborted `bg_kill` wait → detached escalation still reaches SIGKILL and settles.
855
+ 11. overlapping multi-id kills → every caller observes every captured settlement; each
856
+ settle hook fires once and consumed state remains true.
857
+ 12. shell `exit` without stdio `close` → bounded cleanup reaps the descendant holding the
858
+ pipes, preserves the shell's natural exit status, and releases the running slot.
859
+
860
+ **`result-delivery.test.ts`** — consume-before-drain, drain-once (copy subagents' file).
861
+
862
+ **`ps.test.ts`** — `reconcileTerminalSelection` behavior (copy `takeover.test.ts` cases).
863
+
864
+ **Manual validation (must actually run pi):**
865
+ - `pi` → ask the model to `bg_start` a dev-server-like command → widget appears above editor
866
+ with correct count/pluralization → `/ps` list → enter detail → live tail scrolls, `t`
867
+ toggles stderr, ANSI-heavy output (e.g. `npm run dev`) renders without smearing → back →
868
+ `x` kills → widget disappears when last settles → completion message arrives exactly once,
869
+ rendered collapsed, expands with ctrl+o.
870
+ - Race check: start a 2s `sleep`-then-echo while the model is mid-long-turn → result arrives
871
+ as follow-up after the turn, not mid-stream, and only once.
872
+ - `/new` and `/reload` with a running process → process is dead afterwards (`ps aux | grep`),
873
+ no orphan, widget cleared.
874
+ - `npm run check` green; `npm test` green; repo-root `npm run format:check` clean for the new
875
+ files (prettier covers `extensions/**/*.ts`).
876
+
877
+ ## 15. Pitfalls (each burned someone in the reference code)
878
+
879
+ 1. **Effect v3 API names don't exist** — `Effect.fork`, `Effect.async`, `Either`,
880
+ `Layer.scoped`, `Context.Tag`. Check every API against effect-v4-notes.md before writing it.
881
+ 2. **`Queue.end` needs `Cause.Done` in the error type** — only relevant if you add a queue;
882
+ this design avoids queues entirely.
883
+ 3. **Don't render raw process output** — ANSI/tabs/control chars desync the TUI
884
+ (transcript.ts's `sanitizeText` comment). Sanitize at render, never at capture.
885
+ 4. **Don't repaint per data chunk** — 50ms debounce (TakeoverView) or the UI starves input.
886
+ 5. **Overlay components are disposed on close** — never cache and re-show; re-invoke
887
+ `ctx.ui.custom` (tui.md Overlay Lifecycle). Make `cleanup()` idempotent with a `closed`
888
+ flag and clear every timer in it.
889
+ 6. **`detached` + group kill or you orphan grandchildren** — `sh -c "npm run dev"` without
890
+ process-group SIGTERM leaves node servers running after pi exits (codex.ts `killTree`
891
+ comment).
892
+ 7. **Settle must be idempotent and single-sourced** — kill vs exit vs error events race;
893
+ `if (status !== "running") return` in settle. Set `killSignaled` atomically with SIGTERM
894
+ only while the shell is live; an already-observed natural exit keeps `done`/`failed` even
895
+ if its surviving process group still needs cleanup.
896
+ 8. **Never queue messages into a dying session** — `disposed` guard around `onSettled`, and
897
+ try/catch around `pi.sendMessage` (workflows wraps its follow-up send in try/catch:
898
+ "Session may be shutting down").
899
+ 9. **Defer a copy, not the live snapshot** — the buffer keeps mutating after settle (late
900
+ flushes); subagents defers `{ ...snap, meta: { ...snap.meta } }` for the same reason.
901
+ 10. **Synchronous reservation for the cap** — an `await` between check and increment lets
902
+ parallel tool calls race past it (manager.ts spawn comment).
903
+ 11. **Bound every teardown wait** — 5s timeout on scope closes, or a wedged child hangs
904
+ `session_shutdown` (subagents `disposeAll` + `abortEntry` comments).
905
+ 12. **Snapshot kill interest before Deferred completion** — Effect can resume kill waiters
906
+ immediately; compute `consumed` before `Deferred.doneUnsafe` so their `ensuring`
907
+ blocks cannot release interest first.
908
+ 13. **Tool output limits are a hard requirement** — unbounded stdout in a tool result causes
909
+ context overflow/compaction failures (docs Output Truncation). Truncate *everything* the
910
+ model sees, including the completion message.
911
+ 14. **`prepareArguments` is not needed v1** — but never rename/retype `bg_*` parameters later
912
+ without adding it (resumed sessions replay old tool calls; docs Tool Definition).
913
+ 15. **`hasUI`/`mode` guards** — widget + `/ps` must no-op gracefully in print/RPC modes.
914
+
915
+ ## 16. Acceptance checklist
916
+
917
+ - [ ] `npm install && npm run check` green in `extensions/background-terminals` (TS7 + Effect LS).
918
+ - [ ] `npm test` green (manager, output, result-delivery, ps selection).
919
+ - [ ] Tools registered: `bg_start`, `bg_status`, `bg_list`, `bg_kill`; descriptions document
920
+ no-stdin, session-scoped lifetime, and truncation limits; no stdin/steer surface exists.
921
+ - [ ] stdout and stderr captured separately and completely (in-memory tail + spill file);
922
+ `/ps` detail can inspect both, read-only, scrollable, ANSI-sanitized, live-tailing.
923
+ - [ ] Every model-visible output path truncated (`truncateTail` + clamps) with pointers to the
924
+ full log.
925
+ - [ ] Exactly-once async completion notification via `sendMessage followUp + triggerTurn`,
926
+ deferred-delivery map, consumed-set for kill, `agent_settled` flush, `isIdle()` fast
927
+ path, `disposed` guard. No polling anywhere.
928
+ - [ ] Widget above editor only while ≥1 running, text `N background terminals running • /ps to
929
+ view`, cleared on last settle and on shutdown.
930
+ - [ ] `/ps` two-stage overlay: list (select/kill/open) → detail (metadata, stdout/stderr
931
+ toggle, scroll, back), matching subagents/workflows interaction conventions and hint
932
+ lines from `keybindings.getKeys`.
933
+ - [ ] Kill terminates the whole process tree (SIGTERM → 2s → SIGKILL), records exit
934
+ code/signal, resolves only after settle.
935
+ - [ ] `session_shutdown` (quit/reload/new/resume/fork) kills all processes within bounded
936
+ time via `runtime.dispose()`; no orphans; no messages sent during teardown.
937
+ - [ ] Completed entries retained (≤ MAX_TRACKED, pruned oldest-settled) and visible in
938
+ `bg_list` + `/ps`; running entries never pruned.
939
+ - [ ] Concurrency cap enforced race-free; ids are `bt-N`; cwd resolved against `ctx.cwd` and
940
+ validated; timestamps and elapsed rendering consistent with subagents.
941
+ - [ ] Code style: model strings in `prompt.ts`, Effect only in the async core, plain TS
942
+ callbacks for stream plumbing, no `as any`, prettier-clean.