Repository navigation
Description
[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).
Summary
When the gem home's download cache is missing (vendor/bundle/ruby/<abi>/cache/), a hosted scan still replaces the installed gem with the patched build, but rollback / remove do not put the upstream build back. They exit 0, restore Gemfile and Gemfile.lock, and print:
Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.
That remedy does not work for Bundler. The next bundle install (plain or frozen) prints "Bundle complete!" and keeps the patched copy, and bundle doctor reports no issues. The app keeps loading patched code under a lock that pins the upstream checksum. No upstream_gem_stale_install warning is raised, and remove --json has no warnings at all.
The cache dir is missing in ordinary setups:
- Bundler 4.1.0 (released 2026-11-16) adds the
prunesetting (bundle config set prune true,BUNDLE_PRUNE=1), which deletes that dir after every install. - The Dockerfile Rails generates runs
rm -rf "${BUNDLE_PATH}"/ruby/*/cache. gem cleanup, or a CI cache that leaves the dir out.
Impact
rollback is supposed to undo the scan. Here it restores the two files but leaves every hosted-patched gem installed, and names a remedy that changes nothing. A user who rolls back because a patch broke the app still runs the patched code, and nothing they are told to do fixes it. vex correctly says there is nothing to attest, so the project is in a state no command reports.
Repro
Needs a patch for rack 2.2.8 (the run used a mock patch API and registry: the upstream .gem with one line appended to lib/rack/version.rb that defines Rack::SOCKET_PATCHED).
mkdir repro && cd repro
printf 'source "https://rubygems.org"\ngem "rack", "2.2.8"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle config set --local prune true # Bundler 4.1.0; on older Bundlers: rm -r vendor/bundle/ruby/*/cache after the install
bundle install
git init -q . && git add -A && git commit -qm init
socket-patch scan --mode hosted --yes
# Replaced the installed copy of 1 gem with the patched build: rack 2.2.8.
bundle exec ruby -e 'require "rack"; p defined?(Rack::SOCKET_PATCHED)' # "constant"
socket-patch rollback --yes
# Restored pkg:gem/rack@2.2.8 to its upstream registry entry
# Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.
git status --short # clean: the pair is restored byte for byte
bundle install # Bundle complete! 1 Gemfile dependency, 1 gem now installed.
bundle exec ruby -e 'require "rack"; p defined?(Rack::SOCKET_PATCHED)' # "constant" <- still patchedRemoving the cache dir after the scan instead of before it gives the same result.
What does bring the upstream gem back: bundle install --redownload (Bundler 4.1.0 with prune), or rm vendor/bundle/ruby/*/specifications/rack-2.2.8.gemspec && bundle install. bundle pristine fails ("Cached gem … does not exist").
Expected
docs/ecosystems.md, "Ruby gems: gems already installed": after the scan replaces the installed copies, "rollback and remove do the same with the upstream gem." CLI_CONTRACT.md, "Gem stale-install guard", says the unwind "does the same in the other direction" and that an installed copy left on the patched build is reported as upstream_gem_stale_install. The same section says the scan direction handles "a pruned cache/" by judging the gem dir against the patch record, which is why the scan replaces the copy here.
So either the unwind replaces the copy as the scan did, or it warns upstream_gem_stale_install with a remedy that works. The "until the next package-manager install" note should not be printed for a gem, since Bundler keeps a gem it has already installed.
Actual
The unwind judges an installed copy only by <home>/cache/<leaf>.gem. With no archive the copy is skipped without a warning, and the generic reinstall_required note is all the user sees.
Versions
| Bundler | Ruby | Setup | Result |
|---|---|---|---|
| 4.1.0 | 3.3.6 | prune true |
reproduces |
| 4.0.18 | 3.3.6 | cache dir removed before the scan | reproduces |
| 4.0.18 | 3.3.6 | cache dir removed after the scan | reproduces |
| 4.0.18 | 3.3.6 | remove <purl> instead of rollback |
reproduces |
| 2.6.9 | 3.3.6 | cache dir removed before the scan | reproduces |
| 4.1.0, 4.0.18 | 3.3.6 | cache dir present | does not reproduce ("Replaced the installed copy of … with the upstream build") |
Tested on main 63f791d (5.1.0), Linux. Nothing here is platform-specific, so macOS and Windows should behave the same.
Suspect code
crates/socket-patch-cli/src/commands/gem_refresh.rs:645(stale_after_restore):let Some(archive) = home_cache_archive(&pkg.path) else { continue; }skips a copy with no archive. The scan-side probe falls back to the patch record'safterHashmap in that case; the unwind has no fallback (the record'sbeforeHashmap, or the restored lock's checksum against a downloaded upstream gem, could judge it).crates/socket-patch-cli/src/commands/rollback.rs:2573: thereinstall_requirednote says "until the next package-manager install", which is not true for Bundler.
Generated by Claude Code
Activity
bundler: 2026-11-16 run (Bundler 4.1.0; #2092, #2093)
mikolalysenko commentedon Nov 18, 2026
[agent] Not claimed this run, on purpose. The fix belongs in
stale_after_restore(gem_refresh.rs): judge an installed copy with no cached archive by the patch record'safterHashmap. #2122 (open, awaiting review) adds exactly that mechanism to the same function for the vendored unwind (records+holds_patch). A second PR frommainwould duplicate it and conflict, so this should be built on top of #2122 once it merges, by passing the hosted unwind's patch records toafter_restore.Generated by Claude Code
mikolalysenko commentedon Nov 18, 2026
[agent] Claiming this issue. Branch: agent/fix-hosted-gem-unwind-pruned-cache. #2122 has merged, so this builds on its
records/holds_patchmechanism as planned above.Generated by Claude Code
Test hosted gem unwind in a home with no cache dir
Test pruned-home gem unwind with a real Bundler
Judge a gem with no cached archive by its patch
Judge each archive-less gem copy on its own files
Report an archive-less gem copy no record judged