Repository navigation
Fix stale files left by a replaced agent patch (#2096) - #2119
Mikola Lysenko (mikolalysenko) merged 7 commits into
Conversation
Refs #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
Patch A changes two files and patch B one of them. Replacing A's record with B's must put back the file only A changed, through scan and through get by uuid, or keep A's record when it cannot. A gem variant runs the issue's repro on a real installed gem. Six of the twelve tests in the two files fail until the fix lands. Refs #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
An agent scan or get that replaced a manifest record with another patch for the same purl overwrote the record and applied the new patch. Files only the old patch changed stayed patched with no record: apply --check and vex passed on a tree holding two patches' edits, and rollback could not restore them. Before the record is replaced, roll back the files the recorded patch changed and the new one does not list, through the rollback engine remove delegates to, under the same lock. Files both patches list are left to the nested apply, so a replacement that drops no file behaves as before. When such a file cannot be restored, refuse the replacement: keep the old record, drop the new patch's blobs, and fail the patch with replaced_patch_not_restored and a remedy. Fixes #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
Review of the first cut found that restoring only the files the new patch drops ran the rollback engine on a partial record: a Go redirect, a swapped jar or a rebuilt gem extension was undone as a whole anyway, and under --strict the nested apply still met the old patch's bytes in a shared file. A replacement now rolls the recorded patch back whole, records the new one and applies it. Every installed copy is verified with a dry run before any is written, so a refusal changes nothing. The single-uuid get path writes its blobs before the rollback and carries the rollback's warnings into its envelope. A refused patch's blobs are kept when another patch of the run lists them. A release variant whose distribution is on no installed copy is not rolled back against its sibling's copy. Refs #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
A second review found three gaps in the two-pass rollback. A pass that stopped at the before-blob gate left the other replaced purls without a result, and they were replaced as if rolled back. A recorded patch that was never applied to the installed copy (its hashes match no file there) was refused on every run, and no reinstall could clear it. A failure after the verification read as if the old patch were still applied. Each pass now fails every purl an aborted run did not report. A copy that holds none of the recorded patch's bytes has nothing to roll back: it is left alone with a replaced_patch_not_on_copy warning, which also covers a release variant whose distribution is not installed, so the separate variant check is gone. A late failure says some files were restored and names apply. A manifest write that fails after a rollback says the same. The hot-patch mark is set before the rollback writes, and get prints through the engine's reporter. Refs #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
|
[agent] bugbot run Generated by Claude Code |
PR SummaryMedium Risk Overview
Docs and tests: Reviewed by Cursor Bugbot for commit 512c71f. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 512c71f. Configure here.
CI showed two things. The command-module layering test needs the new agent_download -> rollback edge on its allowlist. And scan_rollout_e2e's agent upgrade was refused: its mock serves no original blobs, so the old patch could not be rolled back, although the new patch lists the same file and nothing would be left behind. A new patch that lists every file of the recorded one now needs no rollback, as on main: the nested apply moves each file to it and reads no original blob. Only a replacement that drops a file rolls the recorded patch back first, which is the case #2096 is about. Refs #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
A replaced release variant was judged against the installed copy of a sibling variant the run does not replace. Where the two share a file, the replacement was refused on every run, or the sibling's file was restored. A copy whose files a sibling's record accounts for whole is now left to that record. A failure in the second pass was always reported as partial. It is now partial only when that pass wrote to the package; a blob that could not be downloaded a second time leaves it applied, and the message says so. A pass that stopped at the before-blob gate failed every package of the run. It now fails the packages the gate named and runs again over the rest. Refs #2096 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
|
[agent] bugbot run Generated by Claude Code |
|
[agent] PR shepherd: review brief for What it does: In agent mode, when Risk: Medium. This is a new write path into installed packages that runs before the manifest write. It only runs when a new patch drops files the old one changed, and a refusal is decided before anything is written. Look here:
Verified:
Changes I made: None. Open questions: None blocking. These are low-risk notes for a follow-up:
Generated by Claude Code |

LLM Description written by Claude Code:claude-fable-5-2
Fixes #2096
Summary
In agent mode, a
scanorgetthat replaces a recorded patch with another patch for the same package now rolls the recorded patch back first when the new patch does not list every file the old one changed. That replacement is "roll back the old patch, record the new one, apply it". A new patch that lists every file of the old one is handled exactly as onmain. Before, the record was overwritten and only the new patch's files were written, so a file only the old patch changed stayed patched with no record:apply --checkandvexpassed on a tree holding two patches' edits, androllbackleft that file patched.When the old patch cannot be rolled back, the replacement is refused: the old record and the installed files stay as they were, the new patch is not applied, and the run exits 1 with a remedy.
Root cause
download_and_apply_patches_into(agent_download.rs, used byscan --mode agentandget's search path) andsave_patch_record(get.rs, theget <uuid>path) both didmanifest.patches.insert(purl, new_record)for aPatchAction::Updatedpatch and went straight to the nested apply. Nothing undid the old record's files, and the old record was the only thing that listed them. The code is not gem-specific, so every ecosystem's agent mode was affected.The change
agent_download.rs:roll_back_replaced_patchesruns the in-place rollback engine (rollback_patches_inner, the oneremovedelegates to) over the records about to be replaced by a patch that drops one of their files, muted, under the lock the caller already holds. It runs twice: a dry run that verifies every installed copy, then the rollback of the packages the dry run found the patch on. A refusal is therefore decided before anything is written.replaced_patch_not_on_copyrun warning. A copy that holds some of the old patch and cannot be rolled back refuses the replacement (replaced_patch_not_restored).get <uuid>path writes its blobs first, so a patch that cannot be saved changes nothing on disk.--all-releasesmanifests) is left to that record.rollback.rs: two smallpub(crate)helpers (copy_holds_patched_bytes, a wrapper over the existing check, andcopy_carries_record).tests/command_module_layering.rs: the newagent_download->rollbackedge is on the allowlist, next toremove->rollback.CLI_CONTRACT.md: the new step, the error code and the warning code.Behavior that changes for users:
--save-onlyon a replacement that drops a file now leaves the package original and recorded (as a--save-onlyof a new patch does). Before, it left the old patch's bytes in place under the new record.[restore] <purl> (N files of the replaced patch).scan --stricton a replacement that drops a file now succeeds where both patches also change a common file: the nested apply meets the original bytes, not the old patch's..socket/blobs, or downloadable). If they cannot be had, it is refused. A replacement that drops no file needs none, as before.Evidence
New tests, run on
origin/mainsources and on this branch:origin/maintests/in_process_agent_patch_replace.rs(new, npm fixture, 14 tests)tests/in_process_gem_apply.rs,gem_rescan_replacing_a_patch_restores_the_file_the_new_patch_dropped(realgem install)tests/in_process_gem_multi_platform.rs,broad_rescan_replaces_the_uninstalled_variants_patchandbroad_rescan_leaves_a_file_the_installed_sibling_variant_shares#2096 is covered by
scan_replacing_a_patch_restores_the_files_only_the_old_patch_changedandget_by_uuid_replacing_...(the two entry points), and by the gem test above. The refusal is covered byreplacement_is_refused_when_a_dropped_file_cannot_be_restored(both entry points),refused_replacement_leaves_every_copy_as_it_was(two installed copies),replacement_is_refused_when_an_original_blob_cannot_be_hadandrefused_replacement_is_a_failed_event_with_its_own_code.The issue's repro under Bundler 4.0.18 / Ruby 3.3.6 (rack 2.2.8 in
vendor/bundle, a local mock of the patch API serving patch A forlib/rack.rb, then patch B forlib/rack/version.rb), with this branch's binary:With a two-file patch A and one of its files edited by hand, the scan exits 1, keeps record A and both files, and prints the remedy (
bundle pristine rack); after running that remedy the same scan succeeds.Local checks:
cargo clippy --locked --workspace --all-features -- -D warningsclean, plus clippy on the three test targets;cargo test -p socket-patch-clifor--lib(949),get(162),scan(257),hot_patch_e2e, and thein_process_*suites for get, gem, cargo, edge cases and agent re-apply all pass.in_process_redirecthas 3 failures in this sandbox with and without this change (it runs as root, which those read-only-file tests do not expect).CI on the first ready commit
Two failures were this PR's and are fixed in the last commit:
command_module_layeringneeded the newagent_download->rollbackedge on its allowlist, andscan_rollout_e2e::agent_cap_rolls_forward_and_upgrades_ignore_the_capshowed that rolling back on every replacement refused an ordinary upgrade when the original blob was not served. That is why the rollback is now limited to replacements that drop a file.yarn-berry 4.18.0failed oncorepack yarn@4.0.2being unavailable on the runner, in a test this PR does not touch. It passes on the current head, as doci-ok,clippyand both coverage legs.Bugbot
Three findings on
512c71f, all confirmed and fixed inbba4bcd, each thread answered and resolved: a replaced release variant judged against its sibling's installed copy, a late failure always worded as partial, and a missing blob refusing unrelated packages of the same run.Review findings not applied
/code-review highran twice. What it confirmed is fixed in the last two commits. Left as they are:applyfrom a stale tree. If a teammate's scan replaces A with B and commits the manifest, a checkout that still has A applied in a cached install and runssocket-patch applyreaches the same stale-file state.applyonly sees record B. Fixing it needsapplyto know which patch an installed copy carries, which is a separate change.--dry-runstill previews a replacement as[would-update]in the case the wet run refuses. The wet run changes nothing when it refuses.vendor/modules.txtcan carry the rollback's "modules.txt is out of step" warning into the run's output although the nested apply restores thereplace.[update] <purl> (replacing ...)and then[fail]for a refused replacement, because the first line is printed while fetching.Follow-up for a maintainer
A single-file patch whose one file was edited by hand after patching is replaced with a warning, not refused: the copy holds none of the old patch's bytes, which is also what a never-applied patch looks like, and refusing that case would block a corrected patch on every run with no reinstall able to clear it. If you would rather refuse there too, it is one condition in
rollback_pass.https://claude.ai/code/session_011f9aeNq467hYuFGH6gine5
Generated by Claude Code