Skip to content

Vendoring a gem that agent mode already patched drops its manifest record but keeps the in-place patch, so after rollback / remove / vendor --revert the app still loads patched code and no command can undo it #2093

Closed

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:

  • Gemfile and Gemfile.lock are 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 --check says "No patches to check.", list says "No patches in this project.", and the original blobs were freed, so no socket-patch command can undo it;
  • rollback prints "1 unwired package keeps its patched bytes in installed trees until the next package-manager install", and the next bundle install changes 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions