Repository navigation
Conversation
A kiro-cli backend runs in its own session, so it outlives a gateway that is SIGKILLed. The next boot's reap skips any backend inside the 120 s spawn grace, and the periodic sweep took only its own gateway's entries, so a backend spawned shortly before a crash ran for the whole life of the next gateway. The periodic sweep, both its scan phase and its kill-phase index, now also takes the entries of a gateway whose PID names no process, using the same pid_exists probe the boot reap uses. A live gateway's entries are still skipped, and the spawn grace, the active-session and start-token identity checks and the argv gate apply unchanged.
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Design Review (Opus 5.5, fork) — 🔴 BLOCK (blocking)Design-level review of Design-Verdict: BLOCK The problem isn't shown to exist: an existing reaper already runs on every periodic tick and kills a dead gateway's backends, with no spawn grace. BlockersThe stated root cause is contradicted by
Watch
[DESIGN-REVIEWED] e36ca2f Description read[DESCRIPTION-READ] f6e6e6e16a67a62763777af1f37cc47676a12a89bbe5d7210b15706ad4d008af That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (5 items) — 5 justifiedIntent: when a gateway crashes, the backends it spawned in its last 120 s are stopped soon after the next gateway starts, instead of running for that gateway's whole life. FIX.
[FIRST-PRINCIPLES-REVIEWED] e36ca2f Description read[DESCRIPTION-READ] 1957d6fa03175d230162fa3fb47728df9452afd663d4572cba4674cbc5e2a8a3 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
|
Closing this one. The Design review is right: the problem it fixes does not exist on main.
I ran that case against main with this PR's own test harness. The boot reap left the backend alive, and the next real cleanup tick killed it and removed its tracking entry, in 4 runs out of 4. On this branch the roots sweep still runs first in the tick, so the new periodic path never reaches that backend. The description's premise, that nothing else reaps it until the next boot, is wrong, and the change would only add a second reaper. |
Problem / Motivation
Goal: A backend left running by a gateway that crashed inside the spawn grace is reaped by the next gateway's first periodic sweep after the grace, instead of living for the whole life of the new gateway.
A kiro-cli backend is started in its own session (
start_new_session=True,acp/launch.py), so it outlives a gateway that is SIGKILLed, OOM-killed or loses power. The next boot's reap,cleanup_orphaned_sessions, then skips any backend younger thanSWEEP_SPAWN_GRACE_SECONDS(120 s). The grace's recorded reason (session_pid.py, above the constant) is that "a missed kill self-heals next cycle". For a crashed gateway's backend there is no next cycle:should_skip_tagged=lambda gw, _p: gw != my_gw_pid)._session_pid_entry_index).cleanup_orphaned_sessionsruns again only at the next clean shutdown or the next boot.Why it matters
A supervisor restarts a crashed gateway within seconds, so every backend spawned in the 120 s before the crash keeps running for the whole life of the new gateway. It keeps its memory, and possibly its turn: whether a real kiro-cli keeps working after its stdin closes was not driven here, because that would spend real model quota.
Not a goal
PR_SET_PDEATHSIGon the spawn. It is Linux-only, it fires on the spawning thread's exit rather than the process's, and it defeats the separate sessionsetsidsets up on purpose.docs/request-for-change/rfc-leaked-runtime-reclaim.md, the closed fix(session): reclaim untracked agent runtimes this install spawned #12612): these rows are tracked runtimes, each a line_track_session_pidwrote in this data home's session PID file with its start token, which the boot reap already ends on main, so this is tracked teardown, which the RFC's Non-goals leave as it is.What changed (motivation → approach → change)
src/kiro_crew/session_pid.py:_owning_gateway_may_be_alive(gw_pid)is the one probe both sweeps use to decide whose entries they may take. It isplatform_compat.pid_exists, exactly what the boot path's_skip_taggedalready called, and_skip_taggednow calls it, so the boot reap is unchanged. A gateway PID that now names another process, or one this user cannot signal, reads as alive. That is the safe direction: an entry counts as a dead gateway's only when its gateway PID names no process._session_pid_entry_index) covers the same set, so a candidate from a dead gateway's entry is re-judged against its own recorded line and token rather than skipped. This gateway's own entry wins when both name one PID._kill_pid_tree; the session cleaner's ownership gate and teardown barrier.asyncio.to_thread(session_cleanup.py,_sweep_periodic_pids), and so does the kill phase. The added probe is onekill(pid, 0)per entry of another gateway, inside those threads.test/test_pid_sweep_dead_gateway.py(new, four tests, listed below). Intest/test_pid_sweep_helpers.py,test_only_sweeps_own_gateway_entriesis renamedtest_sweeps_own_entries_and_skips_a_live_gateways_entries: its fake made every PID dead, the other gateway's too, which under this change correctly makes that gateway's dead entry prunable. The fake now keeps the other gateway alive, so the test still checks what it was for: a live gateway's entries are preserved.Backwards compatibility
Compatible: the boot reap, the grace, the identity checks and the argv gate behave as before; the only new behavior is that a periodic sweep also judges the entries of a gateway whose PID names no process.
Removes nothing: no function, constant, flag or file format is removed. One test is renamed and its fake adjusted, as described above.
Tests
test/test_pid_sweep_dead_gateway.pydrives real processes.kiro-clithe wayacp/launch.pydoes (own session, theKIROCREW_SPAWNEDmarker, pipes) and records it with the real_track_session_pid. The fake is the interpreter reached through a symlink namedkiro-cli, writing a line every 0.1 s. The test process plays the next gateway and runs the real boot reap and both real phases of the periodic sweep.tmp_path. The boot reap's host-wide MCP sweep and its prunes are replaced by recorders. Only PIDs the test started are signalled, by recorded PID, and teardown checks that every one is gone. The one text-mode subprocess call passesencoding="utf-8", and every child that writes a file starts in a directory undertmp_path(cwd=).test_a_crashed_gateways_backend_is_reaped_by_the_first_periodic_sweep_after_its_gracetest_a_live_gateways_backend_is_left_alone_young_or_old(control)test_an_entry_whose_pid_now_names_another_process_is_not_signalled(control)test_the_boot_reap_still_kills_a_crashed_gateways_backend_past_its_grace(control: boot unchanged)Red on origin/main d32c084 (the test file copied onto it, 3 of 3 serial runs): 1 failed, 3 passed each.
Green on this branch (rebased onto origin/main d32c084): the new file 4 passed in 3 of 3 serial runs. The 79 test files that name
session_pid, the new file first: 5,397 passed, 41 skipped, 5 xfailed, 1 failed. The failure,test_browser_launcher.py::TestOpenUrlArgv::test_missing_cli_is_an_honest_error, fails the same way onmainalone on this host, which hasplaywright-cliinstalled outside the managed location.test_acp_client.pyandtest_ephemeral_sessions.pywere left out because they can write under the real home directory. mypy (session_pid.py), flake8, isort, black (no changed file is baselined),scripts/check_black_formatting.py,scripts/check_subprocess_encoding.py,COMMENT_HISTORY_BASE_REF=origin/main scripts/check_comment_history.pyand./scripts/docs-lint.sh: all exit 0.Manual verification
The tests run the real sweep code against real child processes that stand in for a gateway and its kiro-cli. Not run against a real kiro-cli, and not run with a real supervisor restart.
Related Issues
Open #17765 (apply the orphan-sweep spawn grace on Windows) changes the grace itself at different lines and merges cleanly with this change. Merged #767 and #8039 added the identity checks this change relies on.
Pattern harvest
Rule candidate: review-prompt
Pattern: a periodic cleaner that scopes itself to its own owner's records, with the only cross-owner cleanup at boot; a record whose owner died inside a grace window is then never revisited.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)