Repository navigation
Description
[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).
Summary
When a gem that agent mode patched in place is then vendored (scan --mode vendored), the scan moves its record out of .socket/manifest.json ("1 manifest record moved to the vendor ledger") but leaves the in-place patch in the installed copy under vendor/bundle. While the gem is vendored that copy is not loaded, so nothing shows. Once the vendored gem is unwound with rollback, remove <purl> or vendor --revert, Bundler loads the installed copy again, and it is still patched:
GemfileandGemfile.lockare the originals byte for byte, and the command exits 0;- the app loads the patched code after
bundle install("Bundle complete!"); - no record of the patch is left:
apply --checksays "No patches to check.",listsays "No patches in this project.", and the original blobs were freed, so no socket-patch command can undo it; rollbackprints "1 unwired package keeps its patched bytes in installed trees until the next package-manager install", and the nextbundle installchanges nothing.
This is the agent-mode counterpart of #2083 (hosted to vendored). The cause is different: there the hosted refresh is not undone, here the manifest record that could undo the in-place patch is dropped at the takeover.
Impact
Trying agent mode first and moving to vendored mode later is an ordinary path (agent mode needs apply after every install; vendored mode is the portable one). A user who then rolls back keeps running patched code with nothing reporting it, and the note names a step that does not help. bundle pristine does restore the gem, because the cached .gem is still the upstream one, but nothing says so.
Repro
Needs a patch for rack 2.2.8 (the run used a mock patch API: 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
printf 'vendor/bundle/\n.bundle/\n' > .gitignore
bundle config set --local path vendor/bundle && bundle install
git init -q . && git add -A && git commit -qm init
socket-patch scan --mode agent --yes # 1 of 1 targeted patch applied
socket-patch scan --mode vendored --yes # Vendored 1 package.
# Warning: 1 manifest record moved to the vendor ledger (vendored mode is manifest-free): pkg:gem/rack@2.2.8
bundle install # rack now loads from .socket/vendor/gem/<uuid>/rack-2.2.8
socket-patch rollback --yes # or: remove pkg:gem/rack@2.2.8, or: vendor --revert
# Reverted vendoring for pkg:gem/rack@2.2.8
# Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.
git diff --exit-code HEAD -- Gemfile Gemfile.lock # identical to the original
bundle install # Bundle complete!
bundle exec ruby -e 'require "rack"; p defined?(Rack::SOCKET_PATCHED)'
# "constant" <- loaded from vendor/bundle/ruby/3.3.0/gems/rack-2.2.8, still patched
socket-patch apply --check # No patches to check.
socket-patch list # No patches in this project. Run `socket-patch scan`.Expected
rollback gives the project back as it was before socket-patch touched it. CLI_CONTRACT.md describes the takeover as a migration ("a legacy manifest record for a vendored purl is migrated out of .socket/manifest.json"); it does not say the in-place patch becomes permanent. Either the takeover restores the installed copy when it drops the record (the gem is loaded from .socket/vendor from then on), or the vendored unwind restores it, or at least the unwind warns that the installed copy still holds the patch and names bundle pristine <gem>.
Actual
The record is dropped, the patched files stay, and the unwind neither restores them nor names a remedy that works.
Versions
| Bundler | Ruby | Undo command | Result |
|---|---|---|---|
| 4.1.0 | 3.3.6 | vendor --revert |
reproduces |
| 4.1.0 | 3.3.6 | rollback (4-gem project, 2 gems vendored) |
reproduces for both vendored gems |
| 4.0.18 | 3.3.6 | rollback |
reproduces |
| 2.6.9 | 3.3.6 | remove pkg:gem/rack@2.2.8 |
reproduces |
| any of the above | 3.3.6 | vendored only, no agent scan first | does not reproduce (the installed copy was never patched) |
Tested on main 63f791d (5.1.0), Linux. Nothing here is platform-specific.
Suspect code
crates/socket-patch-cli/src/commands/scan/vendor_flow.rs:672-712: the loop removes the manifest records of the purls that were just vendored and writes the manifest (VENDOR_MANIFEST_RECORD_MIGRATED), without rolling back the files those records patched in the installed tree. The same migration runs for get --mode vendored. Other ecosystems that keep an installed copy beside the vendored one may be affected the same way; only gems were tested.
Generated by Claude Code
Activity
bundler: 2026-11-16 run (Bundler 4.1.0; #2092, #2093)
mikolalysenko commentedon Nov 17, 2026
[agent] Claiming this issue. Branch: agent/fix-vendored-unwind-agent-patched-gem.
Generated by Claude Code
Test vendored unwind of an agent-patched gem
Test the agent-patched unwind on real Bundler
Restore an agent-patched gem on vendored unwind
Judge only ledger-owned gems from rubygems.org