Skip to content

Fix stale files left by a replaced agent patch (#2096) - #2119

Merged
Mikola Lysenko (mikolalysenko) merged 7 commits into
mainfrom
agent/fix-agent-update-stale-patched-files
Nov 18, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 7 commits into
mainfrom
agent/fix-agent-update-stale-patched-files

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Fixes #2096

Summary

In agent mode, a scan or get that 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 on main. 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 --check and vex passed on a tree holding two patches' edits, and rollback left 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 by scan --mode agent and get's search path) and save_patch_record (get.rs, the get <uuid> path) both did manifest.patches.insert(purl, new_record) for a PatchAction::Updated patch 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_patches runs the in-place rollback engine (rollback_patches_inner, the one remove delegates 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.
  • A copy that holds none of the old patch's bytes has nothing to roll back (the patch was never applied to it, it is another release variant's distribution, or its patched files were edited since). It is left alone with a replaced_patch_not_on_copy run warning. A copy that holds some of the old patch and cannot be rolled back refuses the replacement (replaced_patch_not_restored).
  • Both entry points call it before the manifest write. The get <uuid> path writes its blobs first, so a patch that cannot be saved changes nothing on disk.
  • A copy that a sibling release variant's record accounts for whole (--all-releases manifests) is left to that record.
  • When the engine stops because an original blob is missing, only the packages that need it are refused; the rest of the run's replacements go on.
  • rollback.rs: two small pub(crate) helpers (copy_holds_patched_bytes, a wrapper over the existing check, and copy_carries_record).
  • tests/command_module_layering.rs: the new agent_download -> rollback edge is on the allowlist, next to remove -> rollback.
  • CLI_CONTRACT.md: the new step, the error code and the warning code.

Behavior that changes for users:

  • --save-only on a replacement that drops a file now leaves the package original and recorded (as a --save-only of a new patch does). Before, it left the old patch's bytes in place under the new record.
  • A human run prints [restore] <purl> (N files of the replaced patch).
  • scan --strict on 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.
  • A replacement that drops a file needs the old patch's original blobs (in .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/main sources and on this branch:

Test file On origin/main On this branch
tests/in_process_agent_patch_replace.rs (new, npm fixture, 14 tests) 10 fail 14 pass
tests/in_process_gem_apply.rs, gem_rescan_replacing_a_patch_restores_the_file_the_new_patch_dropped (real gem install) fails passes
tests/in_process_gem_multi_platform.rs, broad_rescan_replaces_the_uninstalled_variants_patch and broad_rescan_leaves_a_file_the_installed_sibling_variant_shares pass pass (they guard the rollback against a sibling release variant's copy; the second failed on this PR's earlier head)

#2096 is covered by scan_replacing_a_patch_restores_the_files_only_the_old_patch_changed and get_by_uuid_replacing_... (the two entry points), and by the gem test above. The refusal is covered by replacement_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_had and refused_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 for lib/rack.rb, then patch B for lib/rack/version.rb), with this branch's binary:

  [update] pkg:gem/rack@2.2.8 (replacing 44444444)
  [restore] pkg:gem/rack@2.2.8 (1 file of the replaced patch)
Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk
lib/rack.rb:0            <- patch A's edit is gone
lib/rack/version.rb:1    <- patch B's edit
apply --check: Patches are in sync (1 patch checked).
rollback, then sha256sum -c .before: both files OK
bundle exec ruby -e 'require "rack"; ...'   -> plain

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 warnings clean, plus clippy on the three test targets; cargo test -p socket-patch-cli for --lib (949), get (162), scan (257), hot_patch_e2e, and the in_process_* suites for get, gem, cargo, edge cases and agent re-apply all pass. in_process_redirect has 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_layering needed the new agent_download -> rollback edge on its allowlist, and scan_rollout_e2e::agent_cap_rolls_forward_and_upgrades_ignore_the_cap showed 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.0 failed on corepack yarn@4.0.2 being unavailable on the runner, in a test this PR does not touch. It passes on the current head, as do ci-ok, clippy and both coverage legs.

Bugbot

Three findings on 512c71f, all confirmed and fixed in bba4bcd, 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 high ran twice. What it confirmed is fixed in the last two commits. Left as they are:

  • apply from 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 runs socket-patch apply reaches the same stale-file state. apply only sees record B. Fixing it needs apply to know which patch an installed copy carries, which is a separate change.
  • --dry-run still previews a replacement as [would-update] in the case the wet run refuses. The wet run changes nothing when it refuses.
  • Two engine passes. A replacement crawls the package twice, and a missing original blob is downloaded for the dry run and again for the rollback. Only replacements pay this.
  • A stale warning. Replacing a local-mode Go patch in a project with a committed vendor/modules.txt can carry the rollback's "modules.txt is out of step" warning into the run's output although the nested apply restores the replace.
  • The human output prints [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

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
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 17, 2026 18:16
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes agent download/apply behavior by mutating installed packages and manifest records during patch replacement, though failures are fail-closed after verification and refusals leave state unchanged.

Overview
Agent-mode get and scan now treat patch replacement as roll back the recorded patch → update the manifest → apply the new patch, instead of overwriting the manifest record and applying only the new patch’s files. That prevents files touched only by the old patch from staying patched with nothing left in the manifest to track or roll them back (#2096).

roll_back_replaced_patches in agent_download.rs runs the existing in-place rollback engine in a dry verify pass, then a wet rollback, under the apply lock. Copies that never had the old patch bytes are skipped with replaced_patch_not_on_copy; if rollback cannot complete on a copy that still carries the old patch, the replacement is refused with replaced_patch_not_restored (exit 1, old record and tree unchanged). The shared download engine and get’s single-uuid save_patch_record both call this before manifest write; human runs print [restore] lines. --save-only replacements still roll back on disk so the package matches the new record semantics.

Docs and tests: CLI_CONTRACT.md documents the step and codes; new in_process_agent_patch_replace.rs plus gem/npm multi-variant cases cover success, refusal, partial rollback, and JSON events.

Reviewed by Cursor Bugbot for commit 512c71f. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.

Fix All in Cursor

❌ 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
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for bba4bcd

What it does: In agent mode, when scan or get replaces a recorded patch with a new patch that drops some of the old patch's files, the old patch is now rolled back first. A dry run checks every copy, then the real rollback runs, all under the lock already held. If a copy that holds the old patch's bytes can't be rolled back, the replacement is refused with replaced_patch_not_restored and the run exits 1. Copies holding none of the old patch's bytes are left alone with a replaced_patch_not_on_copy warning. Fixes #2096.

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:

  • [agent_download.rs:1055](https://github.com/SocketDev/socket-patch/blob/bba4bcdfa0365bee27cd3fa8b1351db1fac93b84/crates/socket-patch-cli/src/commands/agent_download.rs#L1055): roll_back_replaced_patches (dry pass, then wet pass at :1143)
  • agent_download.rs:928: rollback_pass decides per copy whether it is failed, patched or not-on-copy (holds_patch at :1009)
  • [agent_download.rs:2153](https://github.com/SocketDev/socket-patch/blob/bba4bcdfa0365bee27cd3fa8b1351db1fac93b84/crates/socket-patch-cli/src/commands/agent_download.rs#L2153): refused patches have their blobs unwound and updated becomes failed
  • get.rs:2016: the get <uuid> path writes blobs, then rolls back, then writes the manifest
  • rollback.rs:4004: copy_carries_record, the check for copies a sibling release variant owns

Verified:

  • Reviewed the full diff.
  • cargo test -p socket-patch-cli --test in_process_agent_patch_replace: 14/14 passed.
  • The new tests assert the dropped files are restored, so they would fail on main.
  • The engine is never reached under --dry-run.
  • Path components in copy_carries_record are checked.
  • CHANGELOG.md is untouched.
  • ci-ok and clippy are green on this head, and all 3 Bugbot threads are resolved.

Changes I made: None.

Open questions: None blocking. These are low-risk notes for a follow-up:

  1. rollback_pass checks bytes only at the primary package_path of a folded pnpm/vlt peer-variant result. A failure on a peer copy could be classed not_on_copy (warn) rather than refused.
  2. A Go or Maven copy that fails rollback for an unrelated reason always refuses the replacement, never just warns.
  3. CLI_CONTRACT doesn't describe the partial case, where the wet pass fails after the dry pass passed.

Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

4 participants