Skip to content

Fix vendored gem unwind leaving a hosted-patched install (#2083) - #2113

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-vendored-unwind-stale-hosted-gem
Nov 17, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-vendored-unwind-stale-hosted-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 #2083

Summary

A gem that was patched in hosted mode and later vendored keeps the patched installed copy in the project's vendor/bundle (the hosted scan put it there, and the takeover leaves it because Bundler loads the vendored copy instead). When the vendored gem was unwound with rollback, remove or vendor --revert, the Gemfile pair went back to upstream but the installed copy stayed patched. The app kept running the patch, and on a lock with CHECKSUMS bundle pristine and bundle install --redownload failed with exit 37.

The vendored unwind now does what the hosted unwind already does for installed copies: it replaces a project-local installed copy whose gem-home archive is not the upstream gem with the sha256-verified upstream gem, and warns upstream_gem_stale_install (with the files to remove) for a copy it may not or cannot replace.

Root cause

VendoredBackend::revert (the one revert primitive behind rollback, remove, vendor --revert and vendor's reconcile) put a gem back on its upstream entry and only checked Bundler's cache dir (upstream_gem_stale_cache). The installed-copy refresh in gem_refresh::after_restore ran only from the hosted unwind. CLI_CONTRACT.md documented both halves ("the takeover leaves unused leftovers", "a vendored unwind only names the archive"), and together they left the gap: after the unwind the leftover is in use again.

What changed

  • gem_refresh.rs: after_vendored_unwind runs the existing upstream refresh for installed copies only (Scope::Installed). It looks up the upstream sha256 only for a gem that has an installed copy with its archive, so unwinding a gem that is not installed still needs no network. The archive in Bundler's cache dir (vendor/cache) is not replaced, as before: the vendored unwind still names it.
  • vendored_backend/mod.rs: revert calls it once per run for the gems a wet revert unwired. --json gets a note or warning per copy on the gem's entry. A human run gets one progress line and one consolidated warning.
  • rollback.rs, remove.rs, vendor.rs: print the progress line after the gem's own line. Rollback's "keeps its patched bytes" note no longer counts a gem whose installed copy was refreshed.
  • retry_hint: an unwind's upstream_gem_stale_install warning no longer says "Re-run without --no-gem-install-refresh". A second unwind finds nothing to unwind (the same trap 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 reports for --gem-refresh-shared), so the warning now gives only the rm -f … && bundle install remedy. This also changes the hosted unwind's wording for that one case.
  • CLI_CONTRACT.md ("Gem stale-install guard", flag and warning tables) and docs/ecosystems.md describe the new behavior.

Limits kept from the hosted unwind: only gem homes inside the project are written unless --gem-refresh-shared; --no-gem-install-refresh turns it off; --offline uses only an archive already on disk; a failed download or verification changes nothing and warns. A gem that was only ever vendored has the upstream archive in its gem home, so nothing is downloaded or changed for it.

Evidence

Each test below failed before the fix commit and passes after it.

Test Covers
e2e_redirect_gem_stale_install::vendored_unwind_restores_an_install_a_hosted_scan_patched #2083 for rollback, remove, vendor --revert: installed copy and gem-home archive are upstream again, upstream_gem_install_refreshed in the envelope
…::vendored_unwind_reports_the_refresh_in_a_human_run human output of all three; rollback's reinstall note is gone
…::vendored_unwind_names_an_install_it_leaves_on_the_patched_build --no-gem-install-refresh and a failed upstream download: gem home byte-identical, upstream_gem_stale_install names the dir, gemspec and archive
…::vendored_unwind_warns_once_in_a_human_run one human warning with the rm -f … && bundle install remedy and no "Re-run"
…::vendored_unwind_leaves_an_upstream_install_alone control (passed before too): a never-hosted vendored gem's unwind changes nothing and downloads no gem
e2e_redirect_gem_build::gem_vendored_unwind_restores_an_install_the_hosted_scan_patched (real Bundler, --ignored) the issue's repro: hosted install, takeover, vendored install, unwind; then bundle install, bundle install --redownload and bundle pristine all succeed and bundle exec loads the upstream code

Local runs (Ruby 3.3.6, Bundler 4.0.18):

cargo clippy --locked --workspace --all-features -- -D warnings      clean (CI's command)
cargo test -p socket-patch-cli --lib                                 946 passed
cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install 36 passed
  --test in_process_rollback_vendored 19, in_process_vendor 148, remove 85, rollback 130,
  vendor 133, scan_vendor_e2e 37, vendor_revert_tamper_e2e 21, in_process_remove_repair_lifecycle 17: all passed
cargo test -p socket-patch-cli --test e2e_redirect_gem_build -- --ignored   44 passed (real Bundler)
cargo test -p socket-patch-cli --test e2e_vendor_gem_build -- --ignored     11 passed (real Bundler)

With the src/ changes stashed, the real-Bundler capstone and the four new hermetic tests fail on the installed copy still holding the patched build.

Review (/code-review high)

Fixed:

  • An entry vendored over hosted wiring before v5 reverts to its hosted pin, whose CHECKSUMS sha256 is the patched gem's, so a correct upstream install would have been judged stale. The refresh now covers only a gem the lock resolves from rubygems.org. I have no test for this legacy state: staging a pre-v5 ledger entry by hand was more fixture than the rule deserves, so a reviewer may want to look at this hunk in refresh_restored.
  • remove now prints the refresh's progress line before it aborts on a failed ledger save.
  • The contract sentence about "no re-run" was narrowed to the cases it is true for.

Not fixed, with reasons:

  • No warning when the upstream sha256 can't be proven (--offline or a failed compact-index lookup on a lock without CHECKSUMS). The hosted unwind behaves the same way. Warning without evidence would fire on every offline unwind of a never-hosted gem.
  • scan --prune and --dry-run do not refresh or preview. Stated in the contract. The prune leg that could hit this is the pre-v5 manifest reconcile.
  • Shared-gem-home warning still offers --gem-refresh-shared on a re-run. That 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, unchanged here.
  • rollback --json still carries reinstall_required next to upstream_gem_install_refreshed. The hosted unwind does the same; changing it for one leg would make the two disagree.
  • JSON notes are matched to their gem by the purl that starts the detail. A mismatch falls back to the first unwound gem's entry, so nothing is lost; carrying the purl in Report would change the hosted path's types too.
  • Progress and summary ride on per-entry fields, and the gem homes are crawled twice: design and efficiency notes I left for a follow-up to keep this diff small.

Not in this PR

https://claude.ai/code/session_011f9aeQ4raW21EPjpN9cP36


Generated by Claude Code

Refs #2083

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_011f9aeQ4raW21EPjpN9cP36
Three of the four tests fail: rollback, remove and vendor --revert
of a gem that went hosted, then vendored, leave the installed copy
and its gem-home archive on the patched build and say nothing.

Refs #2083

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_011f9aeQ4raW21EPjpN9cP36
A hosted scan replaces the project's installed copy of a gem with
the patched build. A hosted to vendored takeover leaves that copy,
because Bundler loads the vendored one instead. Once rollback,
remove or vendor --revert unwinds the vendored gem, Bundler loads
the installed copy again, and it was still the patched build: the
app kept running the patch under the upstream lock, and bundle
pristine failed on the upstream checksum (exit 37).

The shared vendored revert now runs the hosted unwind's upstream
refresh for the installed copies of the gems it unwired, under the
same limits (project-local gem homes, --no-gem-install-refresh,
--offline), and warns upstream_gem_stale_install for a copy it
leaves. A gem that was only ever vendored has the upstream archive
in its gem home, so nothing is downloaded or changed for it.

An unwind's stale-install warning no longer suggests a re-run
without --no-gem-install-refresh: a second unwind has nothing left
to unwind.

Fixes #2083

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_011f9aeQ4raW21EPjpN9cP36
Review follow-ups for the vendored unwind's gem refresh.

An entry vendored over hosted wiring before v5 reverts to its
hosted pin, whose CHECKSUMS sha256 is the patched gem's. Judging
the installed copy against it would call a correct upstream
install stale. The refresh now covers only a gem the lock resolves
from rubygems.org; the hosted unwind that follows restores such a
pin and refreshes then.

remove prints the refresh's progress line before it aborts on a
failed ledger save, since the gem home was already changed.

Refs #2083

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

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches vendored revert, Bundler install trees, and optional registry fetches during unwind; behavior is gated by existing flags and limited to gems with stale installed copies, but incorrect upstream SHA lookup could refresh the wrong artifact.

Overview
Fixes #2083: when a gem was hosted-patched and later vendored, rollback, remove, and vendor --revert restored the lock to upstream but left the patched installed gem in place—so the app still ran patched code and Bundler reinstall/pristine could fail.

Vendored unwinds now run the same upstream gem stale-install refresh as hosted unwinds, but only for project-local installed copies (after_vendored_unwind / Scope::Installed). Cache-dir archives are still only warned (upstream_gem_stale_cache). Refresh applies only when the lock resolves the gem from rubygems.org (avoids misjudging pre-v5 hosted revert pins). VendoredBackend::revert batches unwound gems, emits upstream_gem_install_refreshed / upstream_gem_stale_install, prints one consolidated human warning and progress lines on rollback / remove / vendor, and rollback no longer counts gems whose installs were refreshed in “keeps patched bytes.”

Unwind warnings with --no-gem-install-refresh no longer suggest re-running the unwind; remedy is rm -f … && bundle install only (hosted and vendored upstream path).

Contract and docs (CLI_CONTRACT.md, ecosystems.md) and e2e tests cover restore, human output, opt-out/failed refresh, and never-hosted control cases.

Reviewed by Cursor Bugbot for commit 1fb6e83. 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1fb6e83. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for 1fb6e83

What it does: Fixes #2083. Sometimes a hosted scan patches a gem's installed copy before that gem is vendored. When rollback, remove, vendor --revert or vendor's reconcile later unwinds the vendored gem, the installed copy is now put back on the sha256-verified upstream build. This happens only in project-local gem homes, and only when the lock now resolves the gem from rubygems.org. A copy that can't or may not be refreshed is named in one warning with an rm -f … && bundle install remedy.

Risk: low. It reuses the existing, tested hosted-unwind refresh. It writes nothing on a dry run, and never fails the unwind: a failed lookup or download only skips the gem or adds a warning, and the exit code stays 0.

Look here

  • [vendored_backend/mod.rs:230-262](https://github.com/SocketDev/socket-patch/blob/1fb6e8355954857ddd1034628b3e7d3a91a0869b/crates/socket-patch-cli/src/commands/vendored_backend/mod.rs#L230-L262): which gems are collected (only after a wet unwire), and how notes and warnings are routed to entries
  • gem_refresh.rs:763 after_vendored_unwind, and :805, the rubygems.org-only filter
  • gem_refresh.rs:344: the retry_hint change, which also rewords hosted unwinds (correctly: a re-run can't redo them)
  • rollback.rs:2861: refreshed gems are no longer counted in "keeps its patched bytes"

Verified

  • I read the whole diff against every caller of .revert( and gem_upstream_archive.
  • The 5 new e2e_redirect_gem_stale_install tests pass at head. With src reset to main, 4 of them fail; the 5th covers a no-op case and passes either way.
  • These suites also pass: the full stale-install suite (36), the CLI lib unit tests (946), in_process_rollback_vendored/_hosted, e2e_gem and vendor_revert_tamper_e2e.
  • ci-ok and clippy are green. CHANGELOG.md is untouched.

Changes I made: none.

Open questions (minor, a possible follow-up)

  • After an unwind, the shared-gem-home advice still says to "re-run with --gem-refresh-shared" (gem_refresh.rs:384, :589), but a re-run has nothing left to act on. This predates the PR for hosted unwinds and is now reachable from vendored ones too.
  • If the lock has no CHECKSUMS (bundler < 2.6), a wet vendored unwind now asks the rubygems.org compact index for the sha256, unless --offline. A failure skips the gem.

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
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 17, 2026
Conflicts: gem_refresh.rs (StaleGem gained in_place on main; the
installed copies of an unwound gem are never patched in place) and
the stale-install suite (both sides appended tests; kept both).

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_011f9aeQ4raW21EPjpN9cP36
Merged via the queue into main with commit affe508 Nov 17, 2026
49 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-vendored-unwind-stale-hosted-gem branch November 17, 2026 16:19
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Nov 17, 2026
Main gained the vendored unwind's refresh of an install a hosted scan
patched (#2113). It reworks the unwind refresh this branch changed, and
two files conflicted. Each hunk keeps both sides.

crates/socket-patch-cli/src/commands/gem_refresh.rs:

- `stale_after_restore` takes main's `installed_with_archives` walk and
  its `Scope`, and keeps this branch's `Option<sha256>` map: a copy with
  no upstream sha256 to judge it by is listed whether or not its archive
  is cached, one with a sha256 is judged by the archive as before.
- `after_restore` and main's new `after_vendored_unwind` both run
  `refresh_restored`, which keeps this branch's handling of
  `UpstreamArchive` (a refused registry is reported, not dropped) and its
  one `SecureRegistryClient` per run, with main's rule that a vendored
  unwind judges only a gem the lock resolves from rubygems.org. A refused
  lookup only comes back for such a gem, so that rule holds for the
  unchecked copies too.

crates/socket-patch-cli/CLI_CONTRACT.md:

- The unwind direction paragraph keeps this branch's account of a copy
  that could not be checked and ends with main's sentence on a copy left
  by `--no-gem-install-refresh`.
- The `upstream_gem_stale_install` row takes main's vendored unwind
  columns and keeps this branch's "could not be checked" meaning.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Nov 17, 2026
…used registry

The vendored unwind (#2113) now runs the same refresh as the hosted one,
so it reports an installed copy it could not judge when the lock pins no
sha256 and `SOCKET_RUBYGEMS_URL` is refused. An e2e holds that for
`rollback`, `remove` and `vendor --revert`: the gem home is unchanged, no
registry is asked, and the copy is named with the reason. The contract's
paragraph on a vendored unwind says so.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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

3 participants