mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 01:39:51 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Google Antigravity agents can run Google's native `/swarm` on the native Runner (experimental flag, #22733, #22865) > - In a live run, the `agy acp` CLI process was killed mid-swarm. Its helper shell commands kept running for about 14 s, and one wrote a file in the task folder 14 s after the kill > - The failure was recorded only when those helpers exited, because they held the CLI's output pipe open. Automatic recovery then starts a fresh swarm in the same folder, so old helpers can race it > - Recovery must just work, and nothing may write in a task folder after its run ended > - This pull request records every process the CLI starts and stops all of them before a failed or stopped swarm turn is reported and on shutdown, and detects a lost CLI on exit > - The benefit is that a crashed, stopped, timed-out or shut-down swarm leaves no process behind and recovery never races old helpers ## Linked Issues or Issue Description Refs #22733, #22865 **What happened?** After a `kill -9` of the swarm's `agy acp` process, three helper commands outlived it by about 14 s. One wrote a file in the task folder 14 s after the kill. The run failure was recorded about 15 s after the kill, right after the last helper exited. On a user Stop the helpers were gone in 0.2 s, because the CLI stops them itself. **What did you expect?** When the CLI exits for any reason, every process it or its helpers started stops within about 2 s, before Paperclip records the outcome and before recovery starts in the same folder. **Steps to reproduce** Start `/swarm` on a task with helpers that run shell commands. Kill the `agy acp` process. Watch the helper processes and the task folder. **Why it happened** - `retireAntigravityProcess` signals only the launcher's process group. The CLI starts each helper command as its own process-group leader, and when the CLI dies those commands are re-parented. - The swarm connection detected a lost CLI on the child's `close` event. `close` waits until every copy of stdout is closed, and the helpers inherited that pipe. - runnerd already records the full process lineage of the sidecar (`process_lineage.rs`) and stops it when the sidecar is torn down. That happens after the turn outcome is published, so it cannot keep the helpers from writing first. ## What Changed - New `antigravity-process-lineage.ts`: records the lineage of the CLI launcher while it runs, with runnerd's `LineageTracker` membership rules, scoped to the CLI's subtree. A process belongs when its parent belongs, or when it is in a group or session that a member leads and started no earlier than that leader (sessions on Linux; macOS `ps` reports no session id). Identity is PID plus start time, re-read just before each signal, so a reused PID is never signalled. A recorded member that exists but cannot be read stays unresolved, and a table read that does not list this process is rejected; neither proves the lineage gone. Membership never depends on a command name, so other `agy` processes of the same user are never touched. It reads `/proc` on Linux and `/bin/ps` (numbers only) on macOS, every 200 ms while the CLI runs. `terminate()` re-reads the table each round, sends SIGKILL to every live member (the first round before any deadline), and proves none remain within 1.5 s. - Why not runnerd's tracker directly: it is rooted at the sidecar and stops the whole lineage, the sidecar included, only at sidecar teardown. It has no operation for one subtree. The swarm must stop the CLI subtree while the sidecar keeps serving the run, also on Stop and helper timeout, when the CLI is still alive. Reusing it needs a new sidecar-to-runnerd operation and a Rust change, which is out of scope here. The macOS retirement check in `antigravity-process.ts` now uses the same process-table reader. - `antigravity-swarm-runtime.ts`: the CLI's retirement now also terminates that lineage. A swarm turn that did not complete (CLI lost, Stop, helper timeout, refusal) retires the CLI and its lineage before the outcome is returned. `close()` (runner shutdown) does the same. A swarm turn is the port's only turn, so nothing else uses the CLI after it. - Cleanup fails closed. A failed or incomplete process-table read is retried. If the helpers still cannot be proven stopped after three attempts, the turn fails as not retryable with plain text, so Paperclip does not continue the task on its own in that folder. A failed retirement is not cached, so `close()` tries again. - A lost CLI is detected on the child's `exit` event. Output already written is read for up to 200 ms after exit; the inherited pipe no longer delays the failure. - Fake CLI scenario `orphan-helpers`: a helper that leads its own process group, holds the CLI's output pipe, and writes `late.txt` after a delay. The CLI never stops it itself. - Qualification ledger: the live re-check of #22865 (Stop usage 36,910 in / 869 out; killed-run usage still 0 with CLI 1.5.15; kill text and automatic recovery; receipt event; clean thread text) and this fix (fake-CLI proof, live proof pending). Investigated and not changed here: an ordinary (non-swarm) Antigravity run read the instance's run logs and the CLI's private files outside its task folder. No existing Runner policy confines reads for this harness; confining them would be a new policy, so it is reported separately. ## Verification - Red/green: with only the runtime change reverted, the new swarm tests fail (the failure waited on the pipe; helper alive after Stop; helper alive after shutdown; unproven cleanup reported as recoverable). With the change they pass. - Swarm tests kill the fake CLI with SIGKILL and check that the helper is dead when the failure is reported, that `late.txt` is never written (the helper writes it after 3 s, so a report that waited on its pipe would fail), and that an unrelated process of the same user survives. They also cover Stop, runtime close mid-swarm, and a failing process listing (not retryable; the helper stays alive past the cleanup budget and the later close stops it). - Lineage unit tests: re-parented helpers and their children are kept; session members are kept; a reused PID, a reused group id and an earlier-started process are never claimed; zombies are dropped; unreadable members stay unresolved; failed, empty, self-less and unreadable reads never prove the lineage gone and a later call retries; a PID whose start time changed is never signalled; the first kill round runs even when its read exceeds the deadline; pure macOS `ps` and Linux `/proc` parsers; a detached grandchild of a dead root is stopped and an unrelated process survives. Each review-driven rule was checked by reverting it and seeing its test fail. - All Antigravity driver suites (packages/paperclip-runner): 20 files, 363 tests passed. `tsc --noEmit` for the Runner package passed. - `pnpm verify` exited 2 (the fake CLI fixture is a broad input), so CI supplies the broad coverage. `tests/runner-e2e/source-sdk-public-inputs.mjs --write` reported no change. - Not run: a live swarm (live proof is pending and recorded as pending in the ledger). Linux ran only in CI. ## Risks - A sampler reads the process table every 200 ms while a swarm CLI runs (`ps` on macOS, about 25 ms wall time; `/proc` on Linux). Only swarm turns run it. - A helper that starts and leaves its group and session within one 200 ms sample, before it is seen, can escape this tracker. runnerd's sidecar lineage still stops it at sidecar teardown. - A swarm turn that ended without completing now stops the CLI before reporting. The port already refuses a second swarm turn, so no later turn depended on that CLI. - If cleanup cannot be proven (for example the process table cannot be read), the swarm fails without automatic recovery instead of risking a race. - macOS start times have one-second resolution, so a PID reused within the same second as the original start could match. The group and parent rules also need a recorded member, which keeps this narrow. - On macOS there is no session rule, because `ps` reports no session id. A helper that calls `setsid` is still recorded as a group leader when a sample sees it. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use (shell, file edits, test runs). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>