Skip to content

[PIR #1915] Windows: session jobs keep everything a session starts; Tower refuses to run inside one - #1959

Merged
amrmelsayed merged 38 commits into
mainfrom
builder/pir-1915
Nov 24, 2026
Merged

amrmelsayed merged 38 commits into
mainfrom
builder/pir-1915

Conversation

@amrmelsayed

@amrmelsayed amrmelsayed commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

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 find burning 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 running afx cleanup ends 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 status and codev doctor, with a command to end them. By human decision at dev-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-approval the human made two changes:

  1. They dropped the manual ending path (afx orphans, POST /api/orphans/end), because afx cleanup and closing the session already end everything.
  2. They dropped the listing and replaced the teardown guard with prevention. Instead of a guard deciding at teardown what may be killed, Tower refuses to run inside any session job, so a session's job never holds Tower or another session's shellper.

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)

  • Anything started from a Codev terminal on Windows now ends with its session. That includes a GUI program (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 in DEPENDENCIES.md.
  • This applies to sessions created after Tower restarts on the new version. A session created before keeps its old job, which still allows breakaway, until it ends.
  • Tower started by hand inside a session (not through afx tower start) exits with code 1 and a message naming the supported launch paths.
  • Tower start-up cost of the refusal check: about 0.6s with 9 live shellpers on a warm machine; a few seconds cold, while PowerShell starts.
  • macOS/Linux: no runtime change (documentation text only).

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.ts
    • Session job limit flags change from JOB_OBJECT_LIMIT_BREAKAWAY_OK (0x800) to 0. This is commit 6b7f81940, kept alone so it can be reverted by itself.
    • The helper gains a read-only in-job request (IsProcessInJob against a named job).
  • tower-lifecycle.ts
    • sessionJobNamesToCheck returns the job named by the session marker, plus the job of every live shellper found in the process census.
    • towerSessionJobRefusal checks 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):

aad9c00a1 reverts the listing, the API, afx orphans and the teardown guard back to main'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 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:
      • Closing a real session ends a leftover whose parent exited, while a process started outside the session survives.
      • Tower started inside a session refuses to run, with the marker set and with it cleared (exit 1 plus the log line).
    • 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.
    • Unit tests:
      • Flag values and the in-job request.
      • The helper program fits under the 32,767-character command line (it encodes to 21,076).
      • sessionJobNamesToCheck and 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.exe and a timed-out find.exe whose parent was gone both ran inside the builder's job.
    • Both afx cleanup paths ended the builder, its orphaned find and its whole tree: Tower killing the terminal, and, with Tower down, killing the shellper by worktree.
    • The architect and Tower survived.
    • A Tower restart kept the architect's shellper, and a new session worked afterwards.
    • The main Tower was untouched.
  • Survey of programs started inside a job with no breakaway: git, gh, node→cmd, cmd, powershell, python, codex, code and claude --version all ran normally, as did a real claude -p tool call. No program was found that needs to leave the job.

  • Untested:

    • agy (not installed on this machine).
    • Codex running as a full agent session (only codex --version was run).
    • Browsers launched by Playwright or start <url> from a session. This includes the dashboard browser that afx workspace start, afx attach and afx tower cloud login open through openBrowser (agent-farm/utils/shell.ts).
    • GUI apps launched from a terminal, beyond 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)

  1. Test worker death
    • Run 45133587326 (aad9c00a1), Node 22 unit: every test that ran passed, plus one unhandled "Worker exited unexpectedly".
    • It happened in windows-job.windows.test.ts, during "a stopped helper (Tower restart) does not kill a job member". That test is unchanged from main and terminates a job holding only its own idle child.
    • Precedent: spir-1658 run 39927082964 (Node 22), a dead worker in 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.
    • To rule out the worker being inside a job being ended: terminateChecked (3b4066839, kept permanently) checks in-job for the worker and its parent before each of the file's 6 terminates, logs JOB-WORKER-CHECK, and throws if either is a member.
    • Results of that check:
      • Locally, 32 runs of the file all passed; 384 checks, all member=false.
      • In CI, every later run logged 12 checks per job, with 0 member=true and 0 worker deaths.
  2. Lost runner
    • Run 45135042446 (3b4066839), Node 22 unit: "The hosted runner lost communication with the server", 07:59→08:47 (48 min).
    • The same signature, about 47 minutes against a normal 5–7, appears on spir-1658 Node 22 unit runs 42547114468 (22:52→23:39) and 42715696142 (01:44→02:31).
    • The architect accepted it as a pre-existing Node 22 runner pattern.
  3. Tower lifecycle Phase 4(b): see Flaky Tests.

Architecture Updates

COLD codev/resources/arch.md, Platform Layer section:

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:

  • Architecture: a job that allows breakaway loses everything Git Bash starts. Measure the job membership of a real session before trusting "the job holds the session". (Appended to the windows: native (non-WSL) support — tracking map and go/no-go; WSL2 is the supported path today #1658 detached-is-not-setsid lesson.)
  • Testing: a test cleanup that kills every recorded pid can kill an unrelated process, because Windows reuses pids quickly. Kill a recorded pid only if the live process was created no later than the recording (__tests__/helpers/tracked-pids.ts). vitest 4 ignores poolOptions.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

  • The refusal check at boot (tower-server.ts, towerSessionJobRefusal):
    • It checks every live shellper's job, not only the marker's, because a Tower launched with the marker cleared would otherwise go undetected. A test covers that case.
    • It degrades rather than blocking boot. If the census fails, only the marker's job is checked. A shellper job that no longer exists (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.
  • Commit 6b7f81940 stands 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.
  • Session marker in two test harnesses: tower-lifecycle.windows.test.ts (towerStartEnv) and launchers.windows.test.ts (envFor) keep CODEV_SHELLPER_SESSION only for tower start. That makes afx use CIM, as it does for a real user inside a session; every other command still runs without the marker.
  • Audit of pid kills in tests (architect request): I checked every new or touched test file for cleanup that kills recorded pids without checking identity.
    • windows-job.windows and the since-removed session-orphans.windows had that pattern; both now use tracked-pids.
    • The rest use fakes, mocks, or kills that check identity.
    • By owner decision on this PR, every Windows test file whose cleanup killed by bare pid now uses tracked-pids (commit cbd1cf763, test code only). That covers process-tree.windows, tower-lifecycle.windows and launchers.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-windows and ide-server-stub.windows kill nothing by pid, and process-tree.windows:146 kills a root the test just started and is still alive.
    • Worker and parallelism settings are unchanged. vitest 4 ignores singleFork, so Windows test files still run in parallel.

Consultation (single pass)

Verdicts:

  • Claude: COMMENT, high confidence.
  • Codex: COMMENT, low confidence. Its sandbox rejected every file read, so it reviewed no code.
  • Gemini: COMMENT, skipped. agy is not installed here.

Disposition of Claude's findings:

  1. The always-on flag test could pass with 0x800. Fixed. windows-job.test.ts now matches the whole line $SESSION_JOB_LIMIT_FLAGS = 0 and rejects = 0x…. It is the only check of the flag that runs on the Linux PR CI, because .windows. tests skip themselves there.
  2. process-tree.windows.test.ts:75 is 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 use tracked-pids (see the audit above).
  3. openBrowser starts a browser inside the session. Documented: DEPENDENCIES.md (both trees) now names the dashboard browser that afx workspace start, afx attach and afx tower cloud login open. It ends with the session if no browser was already running. Launching it through CIM is a possible follow-up.
  4. The untested list was narrower than the plan's risks. Fixed; see Test Results.
  5. Nits.
    • The tracked-pids comment now states the 1s clock slack.
    • The arch.md line wrap is fixed.

How to Test Locally

  • View diff: VSCode sidebar → right-click builder pir-1915 → View Diff
  • Run dev: VSCode sidebar → Run Dev, or afx dev pir-1915
  • What to verify (Windows):
    • Run pnpm exec vitest run .windows. in packages/codev.
    • In a Codev session, start a long-running native program from Git Bash and let its parent exit (e.g. (find / -name x >/dev/null 2>&1 &)). Close the session (or afx cleanup) and confirm the program is gone.
    • From inside a session, start a throwaway Tower plainly, with its own state folder and a spare port, so it never touches the main Tower: CODEV_AGENT_FARM_DIR="$(mktemp -d)" node dist/agent-farm/servers/tower-server.js 4199. It should exit 1 with the refusal message. Without CODEV_AGENT_FARM_DIR it collides with the main Tower's state and exits with a different error. afx tower start should 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".
    • Seen on run 45276143657, Node 24; passed on re-run.
    • Before teardown, Tower 2136 was not in the launcher's job (inJob=False, created through CIM, parent = WMI). Its log ended normally with no shutdown or refusal line, so it was ended from outside.
    • Likely cause: process-tree.windows.test.ts:75's unchecked afterAll SIGKILLs 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.
    • Addressed at its likely cause in cbd1cf763: that cleanup, and the same pattern in tower-lifecycle.windows and launchers.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.
    • This branch cannot put a CIM-created process into a session job:
      • A process joins a job only through its creator or an explicit assign, and the helper assigns only shellpers it just spawned.
      • The no-breakaway flag only stops members from leaving.
      • The boot check passed, so Tower was in no session job at start-up.
    • History in windows.yml (141 runs scanned): spir-1658 39657896647 and 39907783124 (the "once in nine runs" note), bugfix-1922 44237325105 (commit e034d9b94, now on main, 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).
  • Test worker death in 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.windows scenario 5: afx tower stop exited 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.

…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.
@amrmelsayed
amrmelsayed merged commit 77e2d82 into main Nov 24, 2026
8 checks passed
amrmelsayed added a commit that referenced this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: show and end orphaned processes left in a session after an agent command times out

1 participant