Skip to content

Fix vendored unwind leaving an agent-patched gem (#2093) - #2122

Open
Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
mainfrom
agent/fix-vendored-unwind-agent-patched-gem
Open

Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
mainfrom
agent/fix-vendored-unwind-agent-patched-gem

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 #2093

Summary

A gem that agent mode patched in place and scan --mode vendored (or
get --mode vendored) then took over stayed patched after rollback,
remove or vendor --revert: the pair was back on rubygems.org, Bundler
loaded the installed copy again, that copy still held the patched files,
and no record was left that any command could act on.

The vendored unwind now puts the upstream gem back in that copy, the same
way it already does for a copy a hosted scan had replaced (#2083). A copy
it may not or cannot replace is named by upstream_gem_stale_install
with the files to remove.

Root cause

The takeover moves the manifest record into the vendor ledger and keeps
the patched files (by design: Bundler loads the vendored copy from then
on, and vex compares the installed copy with the vendored artifact).
The unwind's gem refresh judged an installed copy only by the archive in
its gem home's cache/. For an agent-patched copy that archive is the
upstream one, so the copy passed as the upstream build.

The change

  • gem_refresh.rs: the vendored unwind's refresh also judges each
    installed copy against the patch record of the ledger entry being
    removed. A copy whose files all hash to the record's afterHash values
    is stale whatever archive its gem home holds, and is reinstalled from
    the upstream gem. That is the archive RubyGems kept beside it, so
    nothing is downloaded unless cache/ was pruned.
  • vendored_backend/mod.rs: revert hands the refresh the record of each
    unwound gem, under two limits. Only a detached entry's record is used
    (scan / get --mode vendored, where the ledger is the only owner).
    A gem .socket/manifest.json still has a record for is not judged: its
    in-place patch is agent mode's and on record there.
  • With no sha256 for the upstream gem (--offline or a registry error
    under a lock without CHECKSUMS), a copy the record proves patched is
    reported, not refreshed, and only when the lock resolves the gem from
    rubygems.org (gem_resolves_from_rubygems in core). A gem from another
    remote stays unjudged, as in every unwind refresh.
  • CLI_CONTRACT.md ("Gem stale-install guard") and docs/ecosystems.md
    describe the new judgment. No new flag, code or output field.

The existing limits of the refresh apply unchanged: a project-local gem
home only unless --gem-refresh-shared, --no-gem-install-refresh,
--offline, ruby-platform gems only. The hosted unwind is not touched
(it passes no records).

Evidence

Each new test failed before the fix commit (46d8a30, 3dc990f are the
red commits) and passes after it.

e2e_redirect_gem_stale_install (mock API, no Ruby needed), each over
rollback, remove and vendor --revert:

Test Covers
vendored_unwind_restores_an_install_agent_mode_patched the issue's repro: upstream files back, no download, upstream_gem_install_refreshed
vendored_unwind_restores_an_agent_patched_install_without_its_archive cache/ pruned: one download of the upstream gem
vendored_unwind_names_an_agent_patched_install_it_leaves --no-gem-install-refresh, and --offline with cache/ pruned: gem home untouched, warning names the files
vendored_unwind_reports_the_agent_patched_refresh_in_a_human_run progress line, no "keeps its patched bytes" note
vendored_unwind_leaves_an_install_the_manifest_still_records vendor --revert leaves a copy whose record is in the manifest
vendored_unwind_does_not_judge_an_agent_patched_gem_from_another_remote another remote: untouched, rubygems.org not asked

e2e_redirect_gem_build (real Bundler):
gem_vendored_unwind_restores_an_install_agent_mode_patched runs the
three unwinds, then bundle install, --redownload and pristine, and
checks bundle exec loads the upstream code after each.

Local runs (Ruby 3.3.6, Bundler 4.0.18, Linux):

cargo clippy --locked --workspace --all-features -- -D warnings      clean
cargo test -p socket-patch-cli --lib                                 949 passed
cargo test -p socket-patch-core --lib redirect::upstream             185 passed
e2e_redirect_gem_stale_install                                       47 passed
e2e_redirect_gem_build / e2e_vendor_gem_build / e2e_gem_native_ext_build
  (--include-ignored)                                                48 / 18 / 32 passed
e2e_gem, scan_vendor_e2e, command_module_layering                    passed

cargo clippy --all-targets reports errors in test code this PR does
not touch (tests/common, tests/prebuilt_common, older lines of the
two gem suites); they are on main too, and CI's clippy job does not
build test targets.

Review findings not fixed here

/code-review high ran twice. Fixed: the judgment for a gem from another
remote, PurlKey matching, and an earlier attempt to cover manifest
records that could reinstall a copy whose record then stayed. Left, with
the reason:

  • rollback / remove after standalone vendor from an agent
    manifest.
    They drop the manifest record of a vendored gem without
    restoring its files, so the copy stays patched (unchanged from main).
    The right fix is in the in-place leg, which still has the record's
    blobs and can restore exactly, offline and in a shared gem home. It is a
    different path from this one and deserves its own issue.
  • A takeover that vendors a newer patch than the one applied in
    place.
    The takeover drops the older manifest record, and the ledger
    record's hashes do not match the installed files, so the copy is not
    detected. The fix belongs in the takeover (migrate_legacy_manifest_records).
  • A shared gem home. The copy is reported, not replaced, and the
    warning still offers a re-run with --gem-refresh-shared, which is
    Hosted gem rollback / remove tell the user to re-run with --gem-refresh-shared, but the re-run exits 1 with "nothing to roll back" and the patched gem stays in the shared gem home #2086.
  • An unreadable manifest judges no gem: it cannot be told whether the
    manifest records the patch.
  • Non-detached entries (vendor's reconcile after
    remove --skip-rollback) are not judged: the user chose to keep the
    patched files.
  • A refused registry still words a record-proven copy as "could not
    be checked"; the remedy it prints is the same.
  • Repeated crawls and hashing in the unwind: a few files per unwound
    gem, on a path that already crawled twice.
  • gem_resolves_from_rubygems re-reads the lock instead of a new
    UpstreamArchive variant: that keeps the return contract the hosted
    unwind matches on.

https://claude.ai/code/session_011f9aeQy1d8enbEzCjPrQua


Generated by Claude Code

Refs #2093

Assisted-by: Claude Code:claude-fable-5-2
Four tests fail: rollback, remove and vendor --revert of a gem that
agent mode patched in place and a vendored scan then took over leave
the installed copy on the patched files and say nothing, with or
without the upstream archive in the gem home.

Refs #2093

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_011f9aeQy1d8enbEzCjPrQua
Fails on rollback, remove and vendor --revert: the installed copy
keeps agent mode's patched files after the vendored gem is unwound.

Refs #2093

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_011f9aeQy1d8enbEzCjPrQua
A vendored takeover of a gem that agent mode patched in place moves
the manifest record into the vendor ledger and keeps the patched
files, because Bundler loads the vendored copy from then on. The
vendored unwind judged an installed copy only by the archive in its
gem home, which for such a copy is the upstream one. So rollback,
remove and vendor --revert left the patched files in the copy
Bundler loads again, with no record left to undo them.

The unwind's gem refresh now also judges each installed copy against
the patch record the unwind ends: a copy whose files hash to the
record's afterHash values is reinstalled from the upstream gem. That
is the archive RubyGems kept beside it, so nothing is downloaded
unless the gem home's cache was pruned. A copy the run may not or
cannot replace is named by upstream_gem_stale_install.

The record is the detached ledger entry's. For a gem standalone
vendor took from an agent manifest it is the manifest record, and
only in rollback and remove, which drop it without restoring a
file. vendor --revert keeps that record, so the copy stays patched.

Fixes #2093

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_011f9aeQy1d8enbEzCjPrQua
Review follow-ups for the vendored unwind's record judgment.

A gem the manifest still has a record for is not judged: its
in-place patch is agent mode's and on record there (standalone
vendor keeps the record), so vendor --revert leaves the installed
copy as it is. Only a detached ledger entry's record is used, and
the manifest is matched by PurlKey as the takeover does.

With no sha256 for the upstream gem (an offline run or a registry
error under a lock without CHECKSUMS), a copy is judged by its
record only when the lock resolves the gem from rubygems.org. A gem
from another remote stays unjudged, as in every unwind refresh.

Refs #2093

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_011f9aeQy1d8enbEzCjPrQua
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 17, 2026 21:58
@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 gem install refresh during vendored unwind with filesystem and optional registry I/O; scope is Ruby gems on unwind paths and existing stale-install flags apply, but wrong record matching could refresh or skip the wrong copy.

Overview
Fixes a gap where vendored unwind (rollback, remove, vendor --revert) could leave an agent in-place–patched gem running patched code after the lock was back on rubygems.org. Staleness was judged only by the cached .gem archive, which still matched upstream for agent patches, so the refresh skipped the install.

The vendored unwind refresh now also compares installed files to the detached vendor ledger patch record (afterHash via judge_installed_record). When they match, the copy is treated as stale and reinstalled from the upstream gem (often from disk beside the install; download only if cache/ was pruned). vendored_backend passes those ledger records on revert but does not refresh gems that still have a manifest record (agent patch stays intentional). Offline / unverifiable upstream sha256 copies proven patched are warned (upstream_gem_stale_install), not silently assumed upstream.

gem_resolves_from_rubygems limits record-based judgment to locks that resolve the gem from rubygems.org. CLI contract, ecosystems docs, and e2e tests cover the new behavior and edge cases (--no-gem-install-refresh, --offline, other remotes, manifest-held patches).

Reviewed by Cursor Bugbot for commit 9389ca9. Configure here.

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

Labels

None yet

Projects

None yet

2 participants