Skip to content

fix(session): reclaim untracked agent runtimes this install spawned - #12612

Closed
DcXiaoBing wants to merge 1 commit into
kirodotdev:mainfrom
DcXiaoBing:fix/reclaim-untracked-runtime-orphans
Closed

DcXiaoBing wants to merge 1 commit into
kirodotdev:mainfrom
DcXiaoBing:fix/reclaim-untracked-runtime-orphans

Conversation

@DcXiaoBing

@DcXiaoBing DcXiaoBing commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Reclaim leaked agent runtimes this install provably spawned, and leave every other process alone.

The periodic orphan sweep detects untracked agent runtimes that are reparented to init, but its report-only path leaves each runtime tree resident. Since #11309, #11565 and #11720 the tracked teardown path kills the whole group and records descendants, so the runtimes that still reach the report-only arm are those whose root died before the first PID-file snapshot, and every runtime on hosts without cgroup v2 scope delegation (where session_scope_reap has nothing to reclaim). A leaked kiro-cli acp tree can retain its chat process and MCP servers, consuming about 1.2 GB until an operator intervenes.

Why it matters

Repeated leaks can exhaust a long-running gateway host. One observed dev desktop accumulated 42 trees over four days, consuming 48 GB of a 61 GB host. Reclaiming them safely requires proof that this install spawned the runtime and that no live session owns it.

Not a goal

  • Reclaiming runtimes spawned by a predecessor gateway (no minted-token record; report-only)
  • Any change to the kill gate, lease, or registry behavior landed on main

What changed (motivation → approach → change)

The sweep reclaims only runtime trees whose ownership and process identities remain provable at signal time, and whose owning session is gone.

Piece Change
Spawn ownership src/kiro_crew/constants.py, src/kiro_crew/acp/runtime.py, and src/kiro_crew/acp/client.py stamp runtimes with KIROCREW_SPAWN_HOME and KIROCREW_SPAWN_INSTANCE; session_pid.mint_spawn_instance is the one minting point and remembers every token it hands out.
Reclaim eligibility _is_reclaimable_untracked_runtime in src/kiro_crew/session_pid.py requires ALL of: _is_untracked_managed_agent_orphan (reparented, harness argv0, KIROCREW_SPAWNED, in neither PID file), a complete tracked-PID snapshot, KIROCREW_SPAWN_HOME equal to this install's data home, a KIROCREW_SPAWN_INSTANCE in this gateway's minted set, an age of at least _UNTRACKED_RUNTIME_MIN_AGE_SECONDS, and _work_orphan_session_leader_alive false (the root's session leader is gone). The sid is a refusal input only, never reclaim authority: a live leader, a self-led root (setsid-detached) or an unreadable sid keeps the root report-only; a dead launcher passes.
Identity-bound signaling _kill_orphan_runtime_tree revalidates the root start token before group enumeration, refuses a root that does not lead its group while the group's leader is alive (a live runtime's backgrounded child), drops vouched members present on a fresh tracked-PID snapshot or in the protected set (an incomplete snapshot enumerates nothing), vouches the rest by process group and instance, rechecks each member token at signal time, and signals the root last. Recycled or unreadable identities fail closed.
Shared orphan path Work-orphan subtree enumeration stays outside the ACP boundary, each signal is identity-bound, and process-controlled argv0 values are quoted in logs and SEL resources.
Environ decoding _spawn_env_value decodes KIROCREW_SPAWN_HOME / KIROCREW_SPAWN_INSTANCE as UTF-8 (errors="replace"), not ASCII, so a non-ASCII data home compares equal to peek_data_home().
Boundary and specification .github/agent-sdk-boundary-baseline.txt records the reduced ACP import count. docs/system-specs/modules/session.md names the age-floor constant and documents the ownership, leader-refusal and revalidation contract.

test/acp_launch_capture.py and test/fixtures/acp_launch_goldens.json normalize and pin the runtime ownership environment. Runtimes without complete proof remain report-only, including pre-stamp processes and non-Linux environments where the required process evidence is unavailable. macOS is a follow-up: KIROCREW_SPAWN_HOME/INSTANCE are readable there via KERN_PROCARGS2, but the leader refusal reads the target's SID through _linux_pid_sid, which no darwin helper exposes yet; it needs its own ownership proof rather than a port.

Backwards compatibility

Compatible: no accepted command, configuration, or API input is rejected. Spawned runtimes receive ownership metadata, while any runtime that lacks the full proof remains report-only.

Tests

test/test_pid_lifecycle.py covers every reclaim predicate, the leader refusal (live leader, self-led root, unreadable sid refuse; dead launcher passes), incomplete tracking snapshots, root and member PID reuse, a root whose group leader is alive (nothing enumerated), owned members dropped before signalling, identity-bound group signaling, kill ordering and caps, work-orphan subtree handling, hostile argv0 logging, and fail-closed behavior.

test/test_acp_spawn_offload.py, test/acp_launch_capture.py and test/fixtures/acp_launch_goldens.json verify that both spawn paths stamp a minted instance and the ownership environment across all captured ACP launch paths. test/test_per_runtime_process_resources.py pins that the cgroup scope is named after the same minted token; test/test_kill_chokepoint_ratchet.py lowers BYPASS_BASELINE to 66 for the kill site this change routes through the identity-bound path.

Manual verification

Live-host evidence confirmed the target shape: leaked runtime roots retained a dead launcher SID and sat in the dead launcher's process group rather than becoming their own session or group leaders, so the leader refusal and the group-leader refusal both pass for them. The related test suite exercises the reclaim and refusal paths; no live orphan was staged for destructive verification.

Screenshots / video

N/A — this change has no user-visible interface.

Related Issues

Fixes #11899

Pattern harvest

Not generalizable: the report-only behavior was a deliberate safety boundary pending an ownership proof; this change supplies that proof and its process-identity checks.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@DcXiaoBing
DcXiaoBing requested review from a team and bolichen97 September 21, 2026 20:04
@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

Intent: Let the orphan sweep reclaim an untracked agent runtime once it can prove this install spawned it, so leaked kiro-cli acp trees stop accumulating for the life of the host.
Not a goal: Reaping runtimes that carry no ownership proof (older spawns, other installs, non-Linux hosts); fixing the kiro-cli SIGTERM forwarding defect that creates the leak; changing how tracked runtimes are torn down.

@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Sep 21, 2026
@DcXiaoBing
DcXiaoBing force-pushed the fix/reclaim-untracked-runtime-orphans branch from 72c8875 to 648d0d1 Compare September 21, 2026 20:12
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 21, 2026
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🔴 BLOCK (blocking)

Design-level review of 951af8da9386d82df6dd678b045018703b572c4c via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: BLOCK

Description promises a "no live owning session" reclaim conjunct the code deliberately drops and pins the opposite of — the approved contract is not the shipped one.

Blockers

The kill path's safety contract in the description is not the one implemented.
The description states reclaim requires "no live owning session" and that "a self-led session or unreadable owner state remains report-only", and its macOS paragraph describes an "owning-session check [that] reads the target's SID" → the diff's _is_reclaimable_untracked_runtime decides only on stamps, minted token, snapshot completeness and age, documents "The root's session id and parent are NOT inputs", and test_pid_lifecycle.py::test_session_shape_does_not_decide pins that a self-led or live-leader root "is reclaimed all the same" → a human approving this revision authorizes SIGKILL for exactly the shape the description says is spared: a ≥30-minute-old runtime whose tracking append permanently failed but whose session is still live is killed mid-task, irreversibly. That matters doubly here because this arm's report-only behavior was explicitly reserved — commit a59bdd7: "Whether an untracked runtime may be reaped at all is a maintainer's ruling and is left out of this change" — so the contract being ruled on must be the contract shipped.
Fix: reconcile the two — either implement the owning-session conjunct the description names, or revise the description (author edit, noted in a PR comment) to state that session shape is deliberately not an input and why the remaining conjuncts spare a live-but-untracked session.
Clears when: the description's reclaim predicate and _is_reclaimable_untracked_runtime name the same conjunct set, and a maintainer's approval stands in for the ruling a59bdd7 reserved.

[DESIGN-REVIEWED] 951af8d

Description read

[DESCRIPTION-READ] b41573a6d1a6b7bf2cf7530092c27bc6cc1317b6ae5d233b2e524dd915092d6c

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — 🔴 changes requested (blocking)

Reviewed 951af8da9386d82df6dd678b045018703b572c4c via the fork AI-review pipeline; updated in place on each push.

1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

BLOCKING -- src/kiro_crew/session_pid.py:4561 -- Reclaim ignores a live owning session

return _env_spawn_instance(pid) in _minted_spawn_instances
Active agent runs setsid -f kiro-cli acp -> inherited stamps pass reclaim after 1800s -> sweep SIGKILLs the live runtime.
Anchor: residual/crash-data-loss-corruption
Fix: Return false when _work_orphan_session_leader_alive(pid) is true.
[BLOCK-MERGE] 951af8d
[GPT-REVIEWED] 951af8d

Adjudication (Opus 5) — is blocking on each finding proportionate?

F1 — harm rung: UNBOUNDED-adjacent (silent SIGKILL of a live process tree and its in-flight work, unrecoverable).
Conditions I confirmed in the reclaim path: reparenting to init only (session_pid.py:3794, _our_orphan_pids accepts any same-uid pid whose ppid is init/systemd --user), harness basename + inherited KIROCREW_SPAWNED + absence from both PID files (session_pid.py:4441-4451), then only the inherited KIROCREW_SPAWN_HOME/KIROCREW_SPAWN_INSTANCE stamps and the 1800s floor (patch _is_reclaimable_untracked_runtime); the root is signalled unconditionally as the last target even when _marked_group_members (session_pid.py:1720) vouches nobody, and the sibling work arm at session_pid.py:4278-4285 deliberately requires a test-runner argv shape and a dead session leader precisely because "marked-but-detached processes an agent may deliberately leave running … MUST NOT be swept" (session_pid.py:4243-4246) — the new arm keeps neither guard for harness-named roots.
Rarity record cannot be completed: an agent backgrounding a nested harness that outlives 30 minutes under the still-live gateway that minted the token is an ordinary shape, not a contradictory one, and the file's own existing invariant treats such survivors as intentional; no recovery path exists after SIGKILL.

[ADJUDICATION] 951af8d total=0 uphold=0 downgrade=0
[GPT-ADJUDICATED] 951af8d

[ADJUDICATION-FENCED] 951af8d fenced=1 flagged=0
UPHOLD-FENCED F1 src/kiro_crew/session_pid.py:4561 -- The reclaim's only ownership proof is two environment stamps that every descendant inherits, so an agent's deliberately detached harness process older than the 1800s floor is SIGKILLed with its group while live, and the file's sibling work arm already treats such survivors as intentional and off-limits.
[GPT-ADJUDICATED-FENCED] 951af8d

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 21, 2026
@DcXiaoBing
DcXiaoBing force-pushed the fix/reclaim-untracked-runtime-orphans branch from 648d0d1 to b35a6dc Compare September 22, 2026 19:10
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 22, 2026
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — 🔴 BLOCK

Premise-level review of 951af8da9386d82df6dd678b045018703b572c4c via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: BLOCK

The sweep now SIGKILLs by default what base code says awaits "a maintainer's ruling on whether an untracked runtime may be reaped at all" — no RFC records that ruling.

Not justified as shipped

  • Item 1 — product-shape change without accepted RFC: the diff rewrites the base pin (session_pid.py:4436) "safe to ship ahead of a maintainer's ruling on whether an untracked runtime may be reaped at all", while the description recasts it as "pending an ownership proof; this change supplies that proof".
    Clears when: an RFC covering untracked-runtime reclaim is merged non-draft on the base branch and the author pushes or rebases; or a maintainer pushes this branch to kirodotdev/KiroCrew and records /ai-review override first-principles 951af8da9386d82df6dd678b045018703b572c4c: <decision>.
  • Item 5 — rides along: work-orphan kills re-routed through identity-bound signaling; declared, closes a named PID-recycle kill.
  • Item 6 — rides along: log/SEL argv0 quoting; declared.
  • Item 8 — undeclared: env values now decoded UTF-8 not ASCII; required for the non-ASCII data-home compare.

What this change ships

Inventory (8 items) — 4 justified

Intent: FIX — stop leaked kiro-cli runtime trees from holding ~1.2 GB each by letting the periodic sweep reclaim ones this install provably spawned (Fixes #11899).

  1. The periodic sweep now SIGKILLs untracked runtime trees it previously only logged — product-shape change without accepted RFC
  2. Every spawned runtime carries a KIROCREW_SPAWN_HOME install stamp — justified
  3. Client-path spawns now also carry KIROCREW_SPAWN_INSTANCE — justified
  4. Spawn tokens minted through one remembered registry, so only this gateway's tokens prove ownership — justified
  5. Work-orphan kills now identity-bound via pidfd; recycled PIDs skipped — rides along (declared)
  6. Leak report and SEL records quote process-controlled argv0 and name the refusal reason — rides along (declared)
  7. 30-minute age floor before any reclaim, documented in session.md — justified
  8. /proc env values decoded UTF-8 instead of ASCII — undeclared (needed by item 2's path compare)

Blockers

product-shape change without accepted RFC — the untracked-runtime arm of a first-class monitor (runs on every cleanup tick, session_cleanup.py:1238, no flag) flips from report-only to SIGKILL for everyone. The base pin it rewrites states the missing decision in its own words: "safe to ship ahead of a maintainer's ruling on whether an untracked runtime may be reaped at all"; the description offers only "Fixes #11899" and "pending an ownership proof". The base RFC status list (all 64 entries read) names no RFC covering orphan-runtime reclaim. The missing record, not a subtraction, resolves this.
Clears when: an RFC covering untracked-runtime reclaim is merged non-draft on the base branch and the author pushes or rebases; or a maintainer pushes this branch to kirodotdev/KiroCrew and records /ai-review override first-principles 951af8da9386d82df6dd678b045018703b572c4c: <decision>.

[FIRST-PRINCIPLES-REVIEWED] 951af8d

Description read

[DESCRIPTION-READ] 1371826a0b8db52271ef245d3cb9d211309ae764a86c962c28f52e76e35e9467

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 22, 2026
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review (fork) — 🔴 changes requested (blocking)

Reviewed 951af8da9386d82df6dd678b045018703b572c4c via the fork AI-review pipeline; updated in place on each push.

The new runtime reclaim resolves the tree from os.getpgid(orphan), so an orphan that is merely a member of a live runtime's group makes the sweep SIGKILL that live, tracked runtime and its MCP servers.

[BLOCK-MERGE] 951af8d

BLOCKING — src/kiro_crew/session_pid.py:5366

    vouched = _marked_group_members(pgid, instance)
    members = sorted((member, token) for member, token in vouched.items() if member != pid)

An agent backgrounds a kiro-cli from its shell tool (bash -c has job control off, so the child keeps the runtime root's pgid); the wrapper shell exits, the child reparents to init, is in neither PID file, and inherits the root's KIROCREW_SPAWNED/KIROCREW_SPAWN_HOME/KIROCREW_SPAWN_INSTANCE → after 1800s _is_reclaimable_untracked_runtime passes for that child alone → _kill_orphan_runtime_tree reads pgid = os.getpgid(child), which is the live root R (a session leader via start_new_session=IS_POSIX), and _marked_group_members(R, instance) vouches R itself plus every MCP server in its group (same inherited instance, marker, and _tracked_child_has_runtime_identity) → _kill_marked_orphan_tree SIGKILLs them all; its only per-target gates are target <= 0 or target == my_pid and the start token, so nothing consults tracked_pids, the caller's active_pids, _protected_pids() or authorize_runtime_kill for a member, and the user's in-flight session dies. The sibling teardown this path cites resolves the group from the session-leader contract for a root it spawned; here the group comes from an arbitrary orphan that need not lead it.
Fix: inside _kill_orphan_runtime_tree, either refuse a root that does not lead its own group (if pgid != pid: return 0) or drop any vouched member present in kill_orphan_mcps's fresh _read_tracked_agent_pids() snapshot / _protected_pids() before signalling.

[OPUS-REVIEWED] 951af8d

@DcXiaoBing
DcXiaoBing force-pushed the fix/reclaim-untracked-runtime-orphans branch 2 times, most recently from 9392949 to 1d98bd0 Compare September 22, 2026 22:04
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 22, 2026
@DcXiaoBing
DcXiaoBing force-pushed the fix/reclaim-untracked-runtime-orphans branch from 1d98bd0 to eec5c23 Compare September 22, 2026 23:37
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Sep 22, 2026
@DcXiaoBing

Copy link
Copy Markdown
Contributor Author
  • Product-shape change lacks an accepted RFC

disposition: needs-a-decision
Maintainer: please choose an accepted RFC path, or push this fork branch to the upstream repository and record /ai-review override first-principles 4a13d97551b985893af229fd18a6136aad3322d5: Approve reclaiming provably-owned untracked agent runtime trees under the documented ownership and identity checks.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author
  • Reclaim authority needs a maintainer ruling

disposition: needs-a-decision
Maintainer: please provide the accepted RFC that authorizes this default behavior, or push the branch upstream and record /ai-review override first-principles 4a13d97551b985893af229fd18a6136aad3322d5: Approve reclaiming provably-owned untracked agent runtime trees under the documented ownership and identity checks.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author
  • Root identity re-verification rides along

disposition: rebutted
This is a declared, harm-named fix in the same reclaim path: the root start token is rechecked before member enumeration so a recycled PID cannot authorize signals to an unrelated process tree.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author
  • Local subtree enumeration rides along

disposition: rebutted
This is a declared fix in the same work-orphan path: _orphan_descendants() supplies the required subtree walk without importing the ACP layer, and the only-shrink boundary baseline records that reduction.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author
  • Quoted process name rides along

disposition: rebutted
This is a declared, harm-named fix in the same kill path: process-controlled argv0 is quoted in the kill log and SEL resources so embedded control characters cannot forge log lines.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 25, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Maintainer ruling on the First Principles question: Crew may reclaim an untracked runtime tree it can prove this install spawned. Proof required per root, all of: KIROCREW_SPAWN_HOME equal to this install's home, and the spawn instance token (KIROCREW_SPAWN_INSTANCE or equivalent) matching a token this gateway minted, read from /proc with a start-time re-check against pid reuse. sid/ppid shape alone is not proof and must not decide reclaim. Anything without that proof is report-only. Please also post one live sid measurement from a leaked root (the 42 ppid=1 roots) so we can see the fix actually reclaims the reported leak.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 27, 2026
@DcXiaoBing
DcXiaoBing force-pushed the fix/reclaim-untracked-runtime-orphans branch from 4a13d97 to 951af8d Compare September 29, 2026 19:35
@DcXiaoBing
DcXiaoBing requested a review from a team as a code owner September 29, 2026 19:35
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Sep 29, 2026
Give the untracked-MCP sweep kill authority over a leaked agent runtime tree only when the root proves this install spawned it and no live session owns it: its KIROCREW_SPAWN_HOME equals this install's data home, its KIROCREW_SPAWN_INSTANCE is a token this gateway minted (both read from the process environment), and the session leader it was spawned under is gone. The sid is a refusal input only: a live leader, a self-led root or an unreadable sid keeps the root report-only, and the sid never grants the kill.

Both spawn paths stamp KIROCREW_SPAWN_HOME beside the instance and mint the instance through session_pid.mint_spawn_instance, which remembers every token it hands out. The kill phase captures the root's start identity before any other read, re-checks it before enumerating the tree by process group and instance, refuses a root whose group is led by a live process, drops vouched members a reaper already owns (fresh tracked snapshot, protected set), and binds every signal to the enumerated identity through a pidfd, so a recycled pid is never signalled on stale evidence. The work-orphan arm shares that identity-bound kill path, enumerates through _orphan_descendants instead of the ACP layer, and quotes process-controlled argv0 in logs.
@DcXiaoBing
DcXiaoBing force-pushed the fix/reclaim-untracked-runtime-orphans branch from 951af8d to cc81de9 Compare September 29, 2026 22:32
@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=5a79f647cc9c

  • Description promises a "no live owning session" reclaim conjunct the code deliberately drops -- fixed

_is_reclaimable_untracked_runtime (src/kiro_crew/session_pid.py:4568) now refuses when _work_orphan_session_leader_alive(pid) is true; the sid is a refusal input only and never grants the kill.
Code and description name the same conjunct set: untracked identity, complete tracked snapshot, SPAWN_HOME == this data home, SPAWN_INSTANCE in this gateway's minted set, age >= _UNTRACKED_RUNTIME_MIN_AGE_SECONDS, session leader gone (live leader, self-led root, unreadable sid all report-only).
This comment records the author edit of the description (What changed / Manual verification / macOS paragraph rewritten to that list) for cc81de9; docs/system-specs/modules/session.md says the same.
Tests: test_pid_lifecycle.py::TestIsReclaimableUntrackedRuntime::test_live_session_leader_forbids and ::test_session_shape_refuses_but_never_grants (fail with the branch removed).

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=99fb501fe250

  • Reclaim ignores a live owning session -- fixed

_is_reclaimable_untracked_runtime (src/kiro_crew/session_pid.py:4568) returns False when _work_orphan_session_leader_alive(pid) is true -- the same fail-closed gate the work-orphan arm uses (sid unreadable -> alive; sid == pid -> alive).
A setsid -f kiro-cli acp detached harness is its own session leader, so it is report-only regardless of age or inherited stamps; a root whose launcher-leader still lives is report-only too. Only a root whose session leader is gone (the dead-launcher leak shape) passes.
Regression tests: test_pid_lifecycle.py::TestIsReclaimableUntrackedRuntime::test_live_session_leader_forbids and ::test_session_shape_refuses_but_never_grants (dead launcher passes; live leader, self-led, unreadable refuse). Mutation-verified: removing the branch fails both.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=afe7cf520521

  • Reclaim resolves the tree from os.getpgid(orphan), so an orphan that is merely a member of a live runtime's group makes the sweep SIGKILL that live runtime -- fixed

_kill_orphan_runtime_tree (src/kiro_crew/session_pid.py:5394) returns 0 when pgid != pid and the group leader still has a start token -- the group then belongs to a live process (a tracked runtime whose backgrounded child reparented), not a leaked tree. A dead launcher has no token and passes.
Before enumerating, it re-reads _read_tracked_agent_pids() (incomplete snapshot -> 0), unions _protected_pids(), refuses a root in that set and drops every vouched member in it (session_pid.py:5403-5413).
Tests: TestUntrackedRuntimeReclaimIntegration::test_kill_refuses_a_root_whose_group_leader_is_alive, ::test_kill_reclaims_a_root_that_leads_its_own_group, ::test_kill_drops_members_a_reaper_owns_before_signalling, ::test_kill_phase_enumerates_nothing_off_an_incomplete_snapshot. Mutation-verified: each refusal removed fails its test.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=0eefc344f534

  • product-shape change without accepted RFC -- needs-a-decision

The 2026-09-26 ruling is implemented as written: KIROCREW_SPAWN_HOME stamped at acp/client.py:10267 and acp/runtime.py:2807, compared to this install's data home at session_pid.py:4570; KIROCREW_SPAWN_INSTANCE minted via mint_spawn_instance (session_pid.py:3946) and compared to the minted set at session_pid.py:4572; start-time recheck at session_pid.py:5383 before enumeration and pidfd-bound per signal at session_pid.py:5473; sid is never a reclaim basis -- it can only refuse (session_pid.py:4568).
A fork cannot consume /ai-review override first-principles. Question for the maintainer: will you record the override for cc81de9, or push this branch to kirodotdev/KiroCrew so it can be recorded there?
Question: does "a token this gateway minted" mean this gateway PROCESS or this INSTALL? Minted tokens are held in-process, so a runtime from a predecessor gateway is report-only; persisting them would need a new record in the root registry.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=aa5608c75f1e

  • Item 1 -- product-shape change without accepted RFC -- needs-a-decision

Same decision as span 0eefc344f534 (see that disposition comment on head cc81de9): the ruling is implemented; the override or a maintainer push of the branch is the only way a fork PR clears this.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=750436447eed

  • Item 5 -- rides along: work-orphan kills re-routed through identity-bound signaling -- rebutted

Declared in the description (What changed, "Shared orphan path"). It closes a named PID-recycle kill on the same kill path this PR hardens: _kill_orphan_work_tree and _kill_orphan_runtime_tree both signal through _kill_marked_orphan_tree -> _signal_pid_by_identity (session_pid.py:5473), so leaving the work arm on the bare-pid path would ship two kill contracts for one primitive.

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=c61715228c1e

  • Item 6 -- rides along: log/SEL argv0 quoting -- rebutted

Declared in the description (What changed, "Shared orphan path"). argv0 is process-controlled text logged on the same reclaim path this PR adds; quoting it (%r) is what keeps the new leak-report and kill log lines from being forged by a newline in argv0 (test_pid_lifecycle.py::test_argv0_control_characters_cannot_forge_log_lines).

@DcXiaoBing

Copy link
Copy Markdown
Contributor Author

span=8e4b7db3a8f0

  • Item 8 -- undeclared: env values now decoded UTF-8 not ASCII -- fixed

Declared in the description for cc81de9: What changed row "Environ decoding" -- _spawn_env_value (session_pid.py:3993) decodes KIROCREW_SPAWN_HOME / KIROCREW_SPAWN_INSTANCE as UTF-8 with errors="replace" so a non-ASCII data home compares equal to peek_data_home().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leaked agent runtimes are detected but never reclaimed, exhausting host memory and restarting the gateway

3 participants