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 > - ACPX chat turns (Claude, Codex, and the other ACP agents) run through runnerd. runnerd reads the ACPX sidecar output in order and keeps the run's provider state in a durable file > - For each sidecar output that it read, runnerd rewrote the full provider state file. Each save did a JSON rewrite, a file `fsync`, a rename, and a directory `fsync` > - runnerd reads the sidecar again only after the controller acknowledges the events that it sent. Streamed text collects in the sidecar pipe during that round trip, so one poll usually reads several outputs > - #20217 made the Codex provider save once per poll. ACPX still saved once per output > - This pull request makes the ACPX executor save the outputs of one poll once, before anything that depends on them leaves the executor > - The benefit is fewer full state rewrites and `fsync` pairs during a streamed turn, on any disk. In a replay of a real Claude stream, runnerd made 49% fewer ACPX state saves ## Linked Issues or Issue Description Refs #20217 (the same change for the Codex provider) Related: #22829 (the same change for runnerd's own state file) Related: #16817 (a broader change. It also merges text deltas into fewer events, changes the sidecar contract, and records expired tool results. This pull request changes only when the ACPX executor saves, and does not change the event stream or the contract.) **What existing behavior does this improve?** The number of durable ACPX provider state saves during a streamed turn. **Subsystem affected** Runner (`paperclip-runner-core`, ACPX provider backend). **Current behavior** `poll_provider_events` calls `save_state` after each sidecar output that returns events. One ACP update, for example one streamed text chunk, becomes one sidecar output. Each save rewrites the full `acpx-provider-state.json` file and does two `fsync` calls. **Proposed behavior** The executor keeps the effects of ordinary outputs in memory and saves them once at the end of the poll. Outputs that drive an effect outside the stream save at once, as before. **Reason and benefit** The save count during a streamed turn no longer grows with the number of chunks in a poll. This removes disk writes and `fsync` calls on every platform. **Breaking changes** None. The state file format does not change. ## What Changed - The ACPX executor marks state as unsaved after an ordinary sidecar output. It saves once when the poll ends, also when the poll stops with an error. A poll returns events only after that save, so no event, command result, or reply to the sidecar leaves before the state that it depends on is on disk. - Each unsaved output still applies the state transition that a save applies (`leave_finished_provider_turn_wait`). The order of the events does not change. - These outputs still save at once: a turn terminal, a tool call, a lost provider turn, and a provider transport failure. The goal read at a terminal keeps its own save. - The session holds a tool rejection that it sends to the sidecar inside `poll_output` (the "too many tool calls in flight" case). The executor saves the outputs read before the rejection, then sends it. - The event acknowledgement (`acknowledge_events`) still saves at once. Recovery reads an empty queue on disk as fully delivered, so that save must not wait. - The transport-failure handling moves into one helper, `fail_polled_provider`. The poll loop and the held-rejection path both use it. - `runner/DURABLE_TRANSPORT.md` now describes when the ACPX executor saves. ## Verification Measurement (macOS, APFS, debug runnerd). I recorded the arrival times of one real Claude ACP turn from `claude-agent-acp` (323 updates in 15.4 s, median gap 33 ms). A temporary build of the fake sidecar replayed that timing in `runnerd-acpx-source-reconciliation.test.ts` ("claude" case, two turns). That test runs a real runnerd against the in-process controller. Temporary counters in the backend recorded the outputs per poll and each state save. These counters are not in this pull request. | | Polls with output | Outputs per poll (mean) | ACPX state saves | Time in saves | | --- | --- | --- | --- | --- | | Before | 84 | 7.7 | 749 | 8.1 s | | After | 182 | 3.6 | 379 | 4.3 s | - One save took about 10 ms (median) in both runs. - Before the change, 642 of 650 outputs came in polls with more than one output. - After the change, the remaining saves are about one per poll plus one per acknowledgement. Polls come more often because each cycle spends less time in saves. Tests: - New lib tests in `src/tests/acpx_poll_state_saves.rs`: - 48 outputs in one poll need one state write. The file on disk then equals the state in memory. - Polled outputs survive a restart once: a new executor retains exactly the unacknowledged suffix, with no duplicate event ids. - An output that ends the turn, or calls a tool, is saved at once. - The sidecar receives a held tool rejection only after the outputs read before it are on disk. - With the old per-output save restored, the tests fail with 48, 26, and 21 writes. Without the hold, the rejection test fails. - `cargo test -p paperclip-runner-core --lib acpx` (122 passed) and `--lib durable` (162 passed). - `cargo test -p paperclip-runner-core --test acpx_park_preparation --test acpx_park_readiness --test acpx_warm_attachment --test durable_recovery` (47, 8, 7, 4 passed). - `cargo test -p paperclip-runner-core --test acpx_provider_session --test acpx_provider_turns --test acpx_quiet_turn_continuation --test acpx_provider_resolutions` (13, 31, 14, 12 passed). - With a freshly built runnerd: `vitest run src/live/runnerd-acpx-source-reconciliation.test.ts` (17 passed). - `cargo fmt --check` is clean. `cargo clippy` shows no new warnings in the changed files. - I did not run `pnpm verify` or the full Rust suite locally. CI supplies that coverage. ## Risks - Crash window: if runnerd stops before the end-of-poll save, it loses the unsaved outputs of that poll. Nothing derived from them has left the executor, so recovery is the same as a stop before runnerd read them. Turn terminals, tool calls, lost provider turns, and failures keep their immediate save. - The executor adds no save to `retained_events`, `execute`, `host_park_ready`, or `prepare_for_park`. Unsaved state does not outlive one poll call. If the end-of-poll save fails, the existing persistence latch blocks all later work. - A held tool rejection reaches the sidecar after one more state save. Its order relative to other sidecar requests does not change. - Low risk overall. The state file format and the acknowledgement path do not change. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, through Claude Code with tool use (shell, file edits, local builds and tests). ## 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 - [x] All Paperclip CI gates are green - [x] 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>