Repository navigation
[PIR #1915] Windows: session jobs keep everything a session starts; Tower refuses to run inside one - #1959
Merged
Conversation
…r Tower/other sessions, real-session dev-approval
The MSYS runtime asks for job breakaway on every native program Git Bash starts. With BREAKAWAY_OK set (Spec 1658 Phase 3b, ruling (c)), a session's job held only the shellper, its console host and the launch loop's bash.exe; the agent and every tool-call process ran outside it, so teardown and any listing of the session's processes could not see them (measured on the owner's host). Without it, git, gh, node, cmd, powershell, python, codex, code, claude and a Claude tool call all start normally and stay in the job. Kept alone so it can be reverted by itself. New Windows test: a node started by Git Bash inside a session job ends with the job (fails with the old flag).
With no breakaway, a Tower started from inside a session by a plain spawn is a member of that session's job, with its helper and every shellper it creates; terminating the job ended all of them (measured). Three guards: - Tower refuses to boot when the job its session marker names really holds it (the marker alone can be stale, so the job is asked). - Teardown lists the job first. If the root's tree or job holds a Tower or another shellper (node running tower-server / shellper-main), the job is not terminated and taskkill /T is not run; the session's own processes are killed one by one, the protected ones and their descendants spared. - New helper requests the listing builds on: snapshot (Toolhelp pid/ppid/ name, ~12ms warm, shaped for the liveness sweep, which is unchanged), job-members, in-job and end-process (terminates only a job member with the expected creation time, never the root). The helper program outgrew the 32,767-character command line, so -EncodedCommand now carries a bootstrap that reads the program from stdin. Still in memory only.
A member of a session's job is an orphan when it is alive, its parent has exited (or its parent pid was reused by a later process), and it is not protected: Tower, its Job Object helper and IDE server, every session root Tower knows, any Tower or shellper found in the job by command line (with its job's members; a husk is left to sweep-husks), members of a nested session's job, and the descendants of all of these. The session's console host, launch loop and agent have live parents back to the root, so they can never match. A parent that cannot be opened never makes an orphan. - GET /api/orphans[?workspace=] lists per session: name, pid, age, CPU, never command lines. The census is read only when a job has a candidate. - POST /api/orphans/end ends only the named processes still orphaned with the listed creation time, through the helper's end-process. Local-only. - afx orphans [agent] [--all] [--apply] [-y]: read-only by default; --apply needs an agent or --all, lists, asks, ends. - afx status (Windows) shows the section when there is something to show, and gains --port; --json carries orphans. codev doctor warns per session. - DEPENDENCIES.md (both trees, identical), the afx skill and the command reference: sessions keep everything they start; afx orphans. Nothing is ever ended automatically.
Measured: a Tower started by `cmd start /b` keeps the console host of the cmd that started it, whose parent is gone. A console host lives exactly as long as something is attached to its console and exits on its own, so it is never listed, as an orphan or a descendant. Also measured: in Git Bash the process whose parent is gone is often the bash.exe stub MSYS keeps as the parent of a native program, with the program as its descendant; listing and ending take both. New Windows tests (real shellper sessions, real helper, isolated Tower): an orphan is listed and ended while the shellper and shell keep running; a process outside every job is never listed and its end is refused; a Tower inside a session job refuses to start with the marker set; without it, the Tower and its session are never listed and survive the outer session's teardown.
Owner decision at dev-approval: with session jobs allowing no breakaway, closing a session or `afx cleanup` ends everything it started, orphans included (checked on real sessions), so no command should be needed to end them by hand. Removed: `afx orphans`, POST /api/orphans/end, the helper's end-process request, and their docs (both skill trees, both command references). Kept, read-only: the per-session list in `afx status` and `codev doctor` (name, pid, age, CPU, never command lines), whose hint now says the processes end with their session. The real-session Windows test now checks that closing the session ends the orphan and that a process outside the job survives.
…per teardown Windows run 45099192117 found two faults in the guard (5c06db9): - It failed open. When the job listing failed, the guard fell back to the tree and still terminated the job, ending a Tower inside it (Node 22, session-orphans.windows). Now a failed listing never terminates the job; nor does a member job without a census to check it. The tree teardown still runs, and the job error says why the job was left. - It added a second cold one-shot PowerShell helper to every teardown run without Tower's warm helper (Node 24, process-tree.windows scenario 6, which had never failed in 102 earlier logs). Now one helper serves the listing and the terminate, and a job that does not exist, is not ours, or is disabled gets no terminate call and no root re-check, so such a teardown does less work than before this branch. Teardown only terminates a job that the listing shows holds the root and the census shows holds no Tower or other session.
…ly their own pids Windows run 45100689541: - In guarded mode (the job holds a Tower or another session), teardown also killed other job members one by one, and killed an inner Tower's shellper the census check had not flagged on that run. A guarded teardown now ends only what hangs off the root by live parent links, minus the protected; other job members are left (this session's orphans in such a job stay too). - tower-lifecycle Phase 4(b) found its launcher's job gone before teardown, never seen on another branch. This branch's test files SIGKILLed every pid they had recorded, including long-exited ones, so a reused pid in a parallel file could be killed. New helpers/tracked-pids.ts kills a pid only when its process was created before the test recorded it; used by session-orphans.windows and windows-job.windows. Audit of every new or touched test: no other unchecked pid kill.
… session job; drop the listing Decision at dev-approval (the human, relayed to the architect): - Remove the orphan listing entirely. With session jobs allowing no breakaway, everything a session starts, a timed-out command's leftovers included, ends when the session is closed or `afx cleanup` runs, and Windows Tower has never shipped, so no recovery code for older sessions is needed. Gone: GET /api/orphans, the afx status section, --port and JSON key, the codev doctor check, the client method, the helper's snapshot and job-members requests, and their docs and tests. - Prevent the Tower-inside-a-session-job case rather than guard teardown against it. At boot Tower asks the job of every live shellper on the machine (from its command line), plus the one its marker names, whether it holds Tower, and refuses to run if any does, marker or not. Teardown is main's again (no guard). The helper gains only an in-job request; the program fits a command line again, so it launches as on main. - Test harnesses start their Towers the supported way: `tower start` keeps the session marker when run inside a session, so afx uses CIM. New Windows tests (real shellper sessions): closing a session ends a child whose parent exited, a process outside every job survives; a Tower started inside a session refuses to run with the marker set and with it cleared.
…st worker or its parent Architect check after CI run 45133587326 (a vitest worker died in this file): every terminate first asks the helper's in-job whether the worker or its parent is a member, logs JOB-WORKER-CHECK, and fails loudly if so.
… recorded process (process-tree, tower-lifecycle, launchers)
amrmelsayed
added a commit
that referenced
this pull request
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PIR Review: Windows session jobs keep everything a session starts; Tower refuses to run inside one
Fixes #1915
Summary
On Windows, a program left behind by an agent command that timed out (typically a root-wide
findburning a CPU) kept running after its session ended. The cause: session Job Objects allowed breakaway, and the MSYS runtime behind Git Bash asks for breakaway on every native program it starts. As a result, the agent and all its tool-call processes were never in their session's job. This PR removes breakaway from session jobs, so closing a session or runningafx cleanupends everything it started. Because nothing can now leave a session job, Tower refuses at boot to run inside any session's job.The issue asked for orphans to be listed in
afx statusandcodev doctor, with a command to end them. By human decision atdev-approval, neither shipped: with no breakaway there are no orphans to list or end by hand.Course change from the plan
The plan (rev 2) had three parts: drop breakaway, guard teardown so it never ends Tower or another session, and list and end orphans per session. Over
dev-approvalthe human made two changes:afx orphans,POST /api/orphans/end), becauseafx cleanupand closing the session already end everything.The guard went through three CI rounds and each found a real defect (fails open on a failed listing, two cold helpers per teardown, killing job members one by one). Prevention removed that whole class of problem. The plan file still describes rev 2; this section records the change.
Behaviour change (Windows only)
code .when VS Code is not already running) and a background server started with&. To keep such a program running after the session ends, start it from outside Codev (a Windows Terminal or PowerShell window). This is documented inDEPENDENCIES.md.afx tower start) exits with code 1 and a message naming the supported launch paths.Files Changed
codev-skeleton/DEPENDENCIES.md(+11 / -0)codev/DEPENDENCIES.md(+11 / -0)codev/plans/1915-windows-show-and-end-orphaned-.md(+326 / -0)codev/projects/1915-windows-show-and-end-orphaned-/status.yaml(+22 / -0)codev/resources/arch.md(+8 / -3)codev/resources/lessons-learned.md(+3 / -1)codev/reviews/1915-windows-show-and-end-orphaned-.md(new)codev/state/pir-1915_thread.md(+119 / -0)packages/codev/src/__tests__/helpers/tracked-pids.ts(+52 / -0)packages/codev/src/__tests__/launchers.windows.test.ts(+16 / -9)packages/codev/src/__tests__/session-job-containment.windows.test.ts(+153 / -0)packages/codev/src/__tests__/process-tree.windows.test.ts(+5 / -9)packages/codev/src/__tests__/tower-lifecycle.windows.test.ts(+18 / -10)packages/codev/src/__tests__/windows-job.test.ts(+17 / -1)packages/codev/src/__tests__/windows-job.windows.test.ts(+59 / -15)packages/codev/src/agent-farm/__tests__/tower-stop-windows.test.ts(+32 / -1)packages/codev/src/agent-farm/commands/windows/tower-lifecycle.ts(+50 / -4)packages/codev/src/agent-farm/servers/tower-server.ts(+17 / -2)packages/codev/src/lib/platform/windows/job-objects.ts(+47 / -10)packages/codev/src/lib/platform/windows/process-tree.ts(+4 / -4, comments only)What each change does:
job-objects.tsJOB_OBJECT_LIMIT_BREAKAWAY_OK(0x800) to0. This is commit6b7f81940, kept alone so it can be reverted by itself.in-jobrequest (IsProcessInJobagainst a named job).tower-lifecycle.tssessionJobNamesToCheckreturns the job named by the session marker, plus the job of every live shellper found in the process census.towerSessionJobRefusalchecks Tower's pid against each of those jobs.tower-server.ts: runs that check once at Windows boot. If Tower is inside a session job, it logs an ERROR, stops the helper, releases the owner lock and exits with code 1. If the census fails, it logs a WARN and checks only the marker's job.Commits
Implementation commits on top of the merge-base (porch bookkeeping and thread-only commits omitted):
6b7f81940[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Session jobs allow no breakaway5c06db9bf[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Keep Tower and other sessions out of a session's teardown74419b02a[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Show and end orphaned processes per session (Windows)9f69b1568[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Never list a console host; real-session Windows testse3546f923[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Mirror the afx skill into the Codex trees; regenerate skill hashes148092136[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Drop the manual ending path: orphans end with their session4f4a5fca2[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Teardown guard fails closed; one job listing, one helper per teardown67015c575[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Guarded teardown ends only the root's tree; tests kill only their own pidsaad9c00a1[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Prevent instead of guard: Tower refuses to run inside any session job; drop the listing3b4066839[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] windows-job.windows: never terminate a job holding the test worker or its parent72e0e2f42Merge remote-tracking branch 'origin/main' into builder/pir-1915 (clean; onlyarch.mdchanged on both sides and auto-merged)99e244a0e[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Consultation follow-ups: exact flag assertion, openBrowser documented, full untested listcbd1cf763[PIR Windows: show and end orphaned processes left in a session after an agent command times out #1915] Windows tests kill a leftover pid only if it is still the recorded process (process-tree, tower-lifecycle, launchers)aad9c00a1reverts the listing, the API,afx orphansand the teardown guard back tomain's code, so the net diff is the file list above.Test Results
Build: ✓ pass (after the merge with
origin/main)Local full suite (Windows 11, Node 22, on
aad9c00a1): 8135 passed, 0 failed. The main Tower kept PID 53132 before and after.Local after the merge: every test file this branch changed, plus the tests merged in from
main(air-1893, bugfix-1910, bugfix-1952): 109 passed, 7 skipped, 0 failed.Windows CI on the final head
cbd1cf763(tracked-pids in all Windows test cleanup): https://github.com/cluesmith/codev/actions/runs/45298573627 (suites plus.windows.targeted); the merge was gated on this run being green.Windows CI on the merged code:
.windows.run https://github.com/cluesmith/codev/actions/runs/45279040879: 9 files passed, 1 skipped; all probes green.Windows CI before the merge: two consecutive fully green runs on
3b4066839, https://github.com/cluesmith/codev/actions/runs/45142055516 and https://github.com/cluesmith/codev/actions/runs/45143011938.New tests:
session-job-containment.windows:windows-job.windows: a native program started by Git Bash stays in the job and ends with it. This test fails with the old flag.in-jobrequest.sessionJobNamesToCheckand the refusal.Real-session check (manual, scripted): an isolated Tower started through CIM from inside a session, in a throwaway workspace, with a real Claude architect and builders (haiku). 17/17:
claude.exeand a timed-outfind.exewhose parent was gone both ran inside the builder's job.afx cleanuppaths ended the builder, its orphanedfindand its whole tree: Tower killing the terminal, and, with Tower down, killing the shellper by worktree.Survey of programs started inside a job with no breakaway: git, gh, node→cmd, cmd, powershell, python, codex, code and
claude --versionall ran normally, as did a realclaude -ptool call. No program was found that needs to leave the job.Untested:
codex --versionwas run).start <url>from a session. This includes the dashboard browser thatafx workspace start,afx attachandafx tower cloud loginopen throughopenBrowser(agent-farm/utils/shell.ts).code, which was surveyed.These come from the plan's risk list. Any of them failing would be a reason to revert
6b7f81940.CI evidence trail (all judged pre-existing)
aad9c00a1), Node 22 unit: every test that ran passed, plus one unhandled "Worker exited unexpectedly".windows-job.windows.test.ts, during "a stopped helper (Tower restart) does not kill a job member". That test is unchanged frommainand terminates a job holding only its own idle child.agy-lane-model.test.ts. tests: load-dependent failures in the packages/codev suite on a native Windows dev machine (agy-auth-cache EPERM, metrics-model-id barrier, windows-job census, worker exit) #1923 lists the symptom.terminateChecked(3b4066839, kept permanently) checks in-job for the worker and its parent before each of the file's 6 terminates, logsJOB-WORKER-CHECK, and throws if either is a member.member=false.member=trueand 0 worker deaths.3b4066839), Node 22 unit: "The hosted runner lost communication with the server", 07:59→08:47 (48 min).Architecture Updates
COLD
codev/resources/arch.md, Platform Layer section:afx tower startcreates it outside through CIM.Nothing went to the HOT tier. The fact is Windows-only, and the hot file is for cross-cutting facts that change behaviour.
Lessons Learned Updates
COLD
codev/resources/lessons-learned.md:detached-is-not-setsidlesson.)__tests__/helpers/tracked-pids.ts). vitest 4 ignorespoolOptions.forks.singleFork, so test files run in parallel workers, and one file's cleanup can run inside another file's assertions.Things to Look At During PR Review
tower-server.ts,towerSessionJobRefusal):found: false) is not a refusal. Neither is a helper error on a job (ok: false): the check fails open, so it can never stop Tower from booting by mistake, at the cost of possibly missing a Tower that really is inside a session job.6b7f81940stands alone so it can be reverted if a program turns up that must leave the job. Agy and Codex as full sessions are the untested candidates.tower-lifecycle.windows.test.ts(towerStartEnv) andlaunchers.windows.test.ts(envFor) keepCODEV_SHELLPER_SESSIONonly fortower start. That makes afx use CIM, as it does for a real user inside a session; every other command still runs without the marker.windows-job.windowsand the since-removedsession-orphans.windowshad that pattern; both now usetracked-pids.tracked-pids(commitcbd1cf763, test code only). That coversprocess-tree.windows,tower-lifecycle.windowsandlaunchers.windows; my first audit missed the last one although this branch touches it. A grep of all Windows test files found no others:ide-server-windowsandide-server-stub.windowskill nothing by pid, andprocess-tree.windows:146kills a root the test just started and is still alive.singleFork, so Windows test files still run in parallel.Consultation (single pass)
Verdicts:
Disposition of Claude's findings:
0x800. Fixed.windows-job.test.tsnow matches the whole line$SESSION_JOB_LIMIT_FLAGS = 0and rejects= 0x…. It is the only check of the flag that runs on the Linux PR CI, because.windows.tests skip themselves there.process-tree.windows.test.ts:75is the suspected killer in the Phase 4(b) flake. Fixed in this PR by owner decision. It and the two other Windows test files with the same cleanup pattern now usetracked-pids(see the audit above).openBrowserstarts a browser inside the session. Documented:DEPENDENCIES.md(both trees) now names the dashboard browser thatafx workspace start,afx attachandafx tower cloud loginopen. It ends with the session if no browser was already running. Launching it through CIM is a possible follow-up.tracked-pidscomment now states the 1s clock slack.arch.mdline wrap is fixed.How to Test Locally
afx dev pir-1915pnpm exec vitest run .windows.inpackages/codev.(find / -name x >/dev/null 2>&1 &)). Close the session (orafx cleanup) and confirm the program is gone.CODEV_AGENT_FARM_DIR="$(mktemp -d)" node dist/agent-farm/servers/tower-server.js 4199. It should exit 1 with the refusal message. WithoutCODEV_AGENT_FARM_DIRit collides with the main Tower's state and exits with a different error.afx tower startshould still work.Flaky Tests
All pre-existing; each was left running, not skipped.
packages/codev/src/__tests__/tower-lifecycle.windows.test.ts:510, Phase 4(b), "Tower N died with the session job".inJob=False, created through CIM, parent = WMI). Its log ended normally with no shutdown or refusal line, so it was ended from outside.process-tree.windows.test.ts:75's uncheckedafterAllSIGKILLs every pid it recorded. Those were all confirmed dead mid-test and so free for reuse. That file finished at 22:09:00.87, after the pre-teardown check (22:08:59.41) and before the assertion (about 22:09:01.5). Unproven, because that file doesn't log its pids.cbd1cf763: that cleanup, and the same pattern intower-lifecycle.windowsandlaunchers.windows, now kills a pid only if the live process was created no later than the recording. Still not proven to be the cause; if Phase 4(b) fails again with this in place, the cause is elsewhere.windows.yml(141 runs scanned): spir-1658 39657896647 and 39907783124 (the "once in nine runs" note), bugfix-1922 44237325105 (commite034d9b94, now onmain, without this branch's code), and this branch 45100689541 (an earlier teardown-guard design) and 45276143657. So it fails without this branch.packages/codev/src/agent-farm/__tests__/launch-loop-exit-code.test.ts:94, "still auto-restarts after a crash": failed once in a full local run under load, passed 3/3 alone (tests: load-dependent failures in the packages/codev suite on a native Windows dev machine (agy-auth-cache EPERM, metrics-model-id barrier, windows-job census, worker exit) #1923 family).windows-job.windows.test.ts(Node 22, run 45133587326): see the evidence trail above (tests: load-dependent failures in the packages/codev suite on a native Windows dev machine (agy-auth-cache EPERM, metrics-model-id barrier, windows-job census, worker exit) #1923 family).tower-lifecycle.windowsscenario 5:afx tower stopexited 0xC0000142 (DLL init failed) once, on Node 24, run 45093824937. pir-1233-session-launch-loop (Git Bash launch failed) failed on the same run. Both passed on Node 22 and on later runs.