fix: stop a finished subagent's leftover processes and its parent's spinner (#201157) Related: #199079 ## What Problem This Solves When a hidden subagent finishes, fails, times out or is stopped, the servers and `nohup … &` processes it started keep running, with nothing left to manage them. Its parent's sidebar row keeps spinning, because the leftover processes still count on the dead child. Archiving a chat had the same gap. On 2026-11-18 a stopped worker left an isolated Gateway, a mock server and an HTTP server running until someone killed them by hand. The process-lifetime audit reproduced this on every subagent end path and on archive. ## User Impact - A hidden subagent's `yieldMs` commands, `background: true` services and leftover processes (`cmd &`, `nohup`) stop when its task ends: it finishes, fails, times out or is stopped. A subagent waiting in `sessions_yield` keeps the command it waits on, and that command's exit still wakes it. - Archiving a chat stops its processes, as reset and delete already do. If the stop cannot be confirmed, the archive fails with a retryable error and the chat stays unarchived. - The parent row and the agent header stop spinning once the child's own run ends. A child's processes still show on its own row. - **Behavior change:** to keep a service after a subagent ends, use `durable: true` or start it from the parent. Main chats, visible spawned sessions and the one-shot `agent --local` hand-off are unchanged. ## Why This Change Was Made The chat stays the only owner of its processes (#199079, #200097), and `stopSessionExecProcesses` stays the only stop. Two chat ends never called it: - **Subagent task end:** one subscriber on the registry's persistence publication derives "task ended" from the stored rows. All rows ended; none yielded, steer-restart or a visible session. It then stops the child's scope keys (key and session id, the same keys the row counts). Every end path writes its terminal row through that bus, so a new end path is covered automatically. - **Archive:** calls the same stop after the lifecycle drain. - **Sidebar:** one helper, `childShowsParentWorking`, replaces the two duplicated fold rules (session tree and team header). A child folds into its parent only while its run is visible, or while it is a yielded subagent waiting on its own command. No protocol change. - **Text:** the `background` exec description and the background-process and subagent docs. ## Evidence - **Contract table** (`subagent-task-end-processes.lifecycle.test.ts`, real processes: a service, a hand-off and a survivor per row). Complete, error, timeout, killed, and parent reset of a yielded child: base fails all 5, the branch passes. The yielded child and the visible-session child keep their processes on both. About 20 s of test time. - **Archive** (`server.sessions.archive-lifecycle.test.ts -t "archive stops the chat"`): both processes stop; an unconfirmed stop returns retryable `UNAVAILABLE` and does not archive. Base fails, the branch passes. - **Sidebar** (`app-sidebar-session-tree.test.ts`): a `historical` child with processes does not spin its parent or the team header; `paused` and `active` children do. Base fails, the branch passes. - tsgo core/ui/core-test, oxlint, format and `agent-tools.prefix-stability.test.ts`: green. **Fault matrix rerun** (isolated Gateways, `env -i`, service marker set, scripted model; base `c8da4b7fbc40` → this branch). R2 = `yieldMs`, R3 = `background:true`, R4 = `nohup &`. L = alive and counted, C = clean. | Cell | Base R2/R3/R4 | Head R2/R3/R4 | |---|---|---| | Subagent normal completion | L/L/L | **C/C/C** | | Subagent model failure (400) | L/L/L | **C/C/C** | | Subagent kill | C/L/L | **C/C/C** | | Parent delete, child running | C/L/L | **C/C/C** | | Parent delete, child completed | C/L/L | **C/C/C** | | Archive | C/L/L | **C/C/C** | | Subagent yielded on its hand-off | kept | kept (`paused`, count 1; exit resumes it) | | Main chat: completion / abort / failure | L/L/L | unchanged by design | A `setsid` double fork stays untracked on macOS, unchanged (follow-up). **Real model** (`openai/gpt-6.2-sol`, this branch): a hidden subagent started a `background:true` HTTP server and a `nohup` process and was then killed. Both processes were gone afterwards, and the child row went from count 2 to no count. **Control UI**, screenshot taken 12 s after the kill without a reload (base used the scripted model): | Before (base) | After (this branch) | | --- | --- | |  |  | |  |  | ## Merge danger and blast radius - Two-way door: no stored rows, schema, config or protocol change; a revert fully restores the old lifetime. - Hidden subagents that start services meant to outlive them now lose them at task end. Archive stops a chat's dev server. - The stop touches only an ended hidden child's scope, and its `assertCurrent` re-derives the condition, so a resumed child is not stopped. An unconfirmed stop is logged and retried on the next publication for that child; a publication never throws. - Cost: one map lookup per changed run per registry publication. - Size: production +134/−18. The design estimated about +60 of logic; the rest is import formatting, the hoisted archive key list and the review fix that bounds the stop by the task's end time (a reset child key keeps its later processes). Tests: +376/−4. ## Left for later PR 2: group kill for adopted survivors, the relay on macOS without the marker, a required `scopeKey` with a single factory and a teardown census, cron run sessions, `canStop: false` on killed children. PR 3: crash recovery for adopted survivors (needs approval) and Linux `cgroup.kill`. Co-authored-by: Ayaan Zaidi <hi@obviy.us>
fix: stop a finished subagent's leftover processes and its parent's s…
Find Your Interest
- When your AI agent publishes internal data
- The Roundup by ByteByteGo
- Developers, Your Next Customer Is an AI Agent
- Defining “Agentic”: Why We Need a Shared Taxonomy for AI Agents
- How to detect Instinct, the invite-only AI agent you text
- Authenticated vs verified vs authorized: What these terms actually mean for AI agents
- Fragments: October 21
- WebMCP: Optimize Your Website for AI Agents
- Adapting to AI penetration testing orchestrators: The Modern Security Model
- How to detect Claude Duet AI agent traffic





