Repository navigation
Description
[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).
Summary
On a project whose gems were patched in agent mode (scan --mode agent / apply), scan --mode hosted rewrites Gemfile and Gemfile.lock (with the patched gem's sha256 in CHECKSUMS) but leaves the installed copy as it is: the agent-patched files, next to the upstream .gem in <gem home>/cache/. The run prints no warning. From then on the lock's checksum and the archive RubyGems reinstalls from disagree:
- With a
CHECKSUMSsection (the default on Bundler 4,bundle lock --add-checksumson 2.6):bundle pristineandbundle install --redownloadexit 37 ("Bundler found mismatched checksums"), after Bundler has removed the installed gem. The app no longer loads (cannot load such file -- rack), and a plainbundle installexits 37 too. Nothing works again until the user deletesvendor/bundle/ruby/*/cache/rack-3.2.8.gemby hand. - Without a
CHECKSUMSsection (Bundler 2.5, 2.6 by default): the same two commands exit 0 and reinstall the unpatched upstream gem, although the lock names the patch registry as the gem's source. (vexandapply --checkdo notice it afterwards.)
The same scan on a project that was never agent-patched replaces the installed copy and its archive, and all of these commands succeed and keep the patch.
Impact
Do no harm: before the hosted scan, bundle pristine / --redownload worked on this project. After it, on a current Bundler, they take the gem away and every later bundle install fails. Moving from agent mode to hosted mode is the upgrade path the tool itself suggests (agent-mode users are told a fresh install removes the patches). The run's own next step says "Reinstall from the updated lockfile so the installed packages pick up the patched artifacts": the reinstall that actually re-fetches (bundle install --redownload) is the command that breaks the project.
Found on a Rails 7.1 app (6 gems switched); the minimal repro below uses one gem.
Repro
Needs a patch for rack 3.2.8 (I used a local mock of the patch API and patch registry; any gem with a hosted patch will do).
mkdir repro && cd repro && git init -q .
printf 'source "https://rubygems.org"\ngem "rack", "3.2.8"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle install # Bundler 4.0.18, Ruby 3.3.6
socket-patch scan --mode agent # 1 of 1 targeted patch applied
socket-patch scan --mode hosted # Switched 1 package to hosted patches; rewrote 2 files.
sha256sum vendor/bundle/ruby/3.3.0/cache/rack-3.2.8.gem # still the upstream archive
grep 'rack (3.2.8) sha256' Gemfile.lock # the patched gem's sha256
bundle install # Bundle complete! (Using rack)
bundle pristine # exit 37
ruby -e 'require "bundler/setup"; require "rack"' # LoadError
bundle install # exit 37Output of the hosted scan (no Replaced the installed copy … line, no warning):
Switched 1 package to hosted patches; rewrote 2 files.
Added `source:` to 1 gem declaration in Gemfile (rack).
Next steps:
1. Commit Gemfile and Gemfile.lock to keep the hosted patches.
2. Reinstall from the updated lockfile so the installed packages pick up the patched artifacts, then run `socket-patch vex` to verify the installed patches.
bundle pristine (and bundle install --redownload, and the bundle install after either):
Bundler found mismatched checksums. This is a potential security risk.
rack (3.2.8)
sha256=0152ef9c2080a82492e65e3920f8e233ec8967ea5d6aa7205bea7d823cca3453
from the lockfile CHECKSUMS at Gemfile.lock:388:16
rack (3.2.8)
sha256=1fecc85e463ebc7a2a945fff829ffcd549babeb1556384796fad946c28dc4ec7
from the gem at
…/vendor/bundle/ruby/3.3.0/cache/rack-3.2.8.gem
$ ruby -e 'require "bundler/setup"; require "rack"'
…/bundled_gems.rb:69:in `require': cannot load such file -- rack (LoadError)
After the failed pristine, socket-patch apply --check prints "Patches are in sync (0 patches checked; 1 patch not installed, skipped)" and socket-patch vex emits a statement for the gem from the lock pin.
Control (skip the scan --mode agent line): the hosted scan prints "Replaced the installed copy of 1 gem with the patched build: rack 3.2.8.", the archive in cache/ has the patched sha256, and bundle pristine and bundle install --redownload exit 0 with the patched code loaded.
Expected
docs/ecosystems.md, "Ruby gems: gems already installed": hosted mode "replaces those copies itself, right after it rewrites the lock: an installed gem whose gem home is inside the project …, together with the .gem archive RubyGems reinstalls it from", and "After the scan bundle exec runs the patched code, and every kind of reinstall keeps it."
CLI_CONTRACT.md, "Gem stale-install guard": "After the run, with no further command, bundle exec loads the patched code, and bundle install, bundle install --redownload, bundle pristine, and a frozen, deployment or --local install all succeed and keep it."
The contract also says "An agent-mode manifest record of the same patch judges the dir by its afterHash file map instead (an in-place patch keeps the upstream archive), so an agent→hosted migration stays quiet." Staying quiet about the files is fine, since they are patched. But the upstream archive beside them is exactly what the guard exists to replace, and it is left in place without a word. Either the archive (with the gem dir) should be refreshed as for any other copy, or the run should warn and name the file to remove.
Actual
The copy counts as patched because of the agent record, so nothing is refreshed and nothing is warned. The upstream archive stays in cache/ under a lock that pins the patched checksum (or, without CHECKSUMS, under a lock that can't tell the two apart).
Versions
| Bundler | Ruby | Lock | Result |
|---|---|---|---|
| 4.0.18 | 3.3.6 | CHECKSUMS (default) |
pristine / --redownload exit 37, gem removed, later bundle install exits 37 (reproduced 3 times, and on a Rails 7.1 app) |
| 2.6.9 | 3.3.6 | after bundle lock --add-checksums |
same |
| 2.6.9 | 3.3.6 | no CHECKSUMS |
pristine / --redownload exit 0 and reinstall the unpatched gem |
| 2.5.22 | 3.3.6 | no CHECKSUMS |
same as the row above |
| 4.0.18 | 3.3.6 | hosted scan with no agent patch before it | does not reproduce |
Tested on main 63f791d (5.1.0), Linux. Nothing here is platform-specific, so macOS and Windows should behave the same. First bad release not searched.
Not affected: socket-patch rollback after the switch restores the pair byte for byte, puts the upstream files back and empties the manifest.
Two related things seen on the same project, in case they share the fix: the agent manifest keeps its records after the switch, so socket-patch list shows each gem twice (once as a manifest patch, once as "Mode: hosted"); and step 2 of the next steps tells the user to reinstall although the installed files are already patched.
Suspect code
crates/socket-patch-cli/src/commands/scan/hosted.rs:1717(local_records, the agent-mode records handed to the stale-install probe) and the rule documented atcrates/socket-patch-cli/src/commands/scan/hosted.rs:319: a dir an agent record proves patched is never passed torefresh_stale_gems, so itscache/<leaf>.gemis never compared with the lock's checksum.
Generated by Claude Code
Activity
bundler: 2026-11-15 run 2 (#2089, #2090, #2091)
mikolalysenko commentedon Nov 17, 2026
[agent] Claiming this issue. Branch: agent/fix-hosted-scan-agent-patched-gem-archive.
Generated by Claude Code
Add failing tests for agent-patched gem archive
Refresh the archive beside an agent-patched gem
Replace only the archive beside an in-place patch