Skip to content

Fix the vendored unwind of a platform gem (#2174) - #2175

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-vendored-unwind-platform-builds
Nov 20, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-vendored-unwind-platform-builds

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Fixes #2174

Summary

A gem locked as precompiled platform gems (nokogiri) that a hosted scan
patched and a vendored run then took over kept its patched installed
copy after rollback, remove and vendor --revert. The Gemfile
pair came back byte for byte, but the app kept loading the patch, and on a
lock with CHECKSUMS the next bundle pristine failed on the restored
checksum after removing the installed gem.

The unwind now brings that copy back to the upstream platform gem, as it
already did for a ruby-platform gem, and names a patched platform archive
left in Bundler's cache dir.

Root cause

A vendored unwind has two follow-ups for a gem it put back on its upstream
entry, and both assumed the gem is the ruby-platform one:

  1. after_vendored_unwind (crates/socket-patch-cli/src/commands/gem_refresh.rs)
    looked for installed copies under <name>-<version> only. A platform
    build is installed as <name>-<version>-<platform>, so nothing was
    found and the refresh never started. The refresh itself
    (refresh_restored) already works by the builds the restored lock names.
  2. unwound_stale_cache (crates/socket-patch-core/src/patch/redirect/upstream/gem.rs)
    judged only <name>-<version>.gem in Bundler's cache dir, and returned
    early when the lock has no ruby-platform entry, which is always the case
    for a gem vendored from prebuilt platform gems. The review of the first
    fix found this one.

Vendoring a platform gem is new on main, so no release has the bug.

The change

  • after_vendored_unwind looks for the builds the restored lock names
    (read from disk, no registry request), and falls back to the
    ruby-platform gem when the lock names none. The code that derives those
    builds moved out of refresh_restored into named_builds, unchanged.
  • unwound_stale_cache judges the archive of the ruby-platform gem and of
    each platform build the lock names, with one warning per archive, the way
    the hosted restore does. A gem none of whose builds the lock pins a
    sha256 for or resolves from rubygems.org is still not judged. It now returns a list; its one caller
    (push_gem_stale_cache) pushes each warning.
  • CLI_CONTRACT.md ("Gem stale-install guard", the
    upstream_gem_stale_cache row) and docs/ecosystems.md ("Ruby gems:
    gems already installed") say that platform builds are covered.

No output format, exit code or warning code changes.

Evidence

Ruby 3.3.6, Bundler 4.0.18, Linux x86_64.

Check Before After
e2e_redirect_gem_build: gem_vendored_unwind_restores_a_platform_install_the_hosted_scan_patched (CHECKSUMS lock) fails: installed copy still patched after remove passes
e2e_redirect_gem_build: gem_vendored_unwind_restores_a_platform_install_without_checksums fails the same way passes, including a real bundle install, bundle pristine and bundle exec after rollback
core unit test vendored_unwind_names_a_patched_platform_archive fails: no warning passes
The issue's repro with the real nokogiri 1.18.10 and tests/nokogiri/patch_service.rb defined?(SOCKET_PATCHED) is "constant", bundle pristine fails rollback prints Replaced the installed copy of 1 gem with the upstream build: nokogiri 1.18.10., defined?(SOCKET_PATCHED) is nil, bundle pristine passes and nokogiri loads

The two e2e tests are a takeover leg of the existing platform prebuild
capstone: hosted scan, get --mode vendored, then remove and
vendor --revert over copies of the project (each with a patched platform
archive in vendor/cache) and rollback on the project itself. They
cover all three commands the issue names.

Local runs: cargo clippy --locked -p socket-patch-core -p socket-patch-cli --all-features -- -D warnings
is clean. e2e_redirect_gem_build (55), e2e_vendor_gem_build (20),
e2e_redirect_gem_stale_install (58) and e2e_gem_native_ext_build (52)
pass with --include-ignored, as do the core redirect::upstream unit
tests (203) and the CLI gem_refresh and vendored_backend unit tests.

Notes for the reviewer

  • cargo clippy --all-targets reports duplicate_mod and
    type_complexity in test targets on main as well; CI's clippy job does
    not lint test targets, and none of the reports are in lines this PR
    touches.
  • CI on 123e56f: ci-ok and clippy are green, Bugbot found no
    issues. The nokogiri and Bazel (RubyGems) compatibility workflows were
    cancelled by the trigger race when the PR left draft; re-run once, both
    pass.
  • A lock that also holds the ruby-platform entry beside the platform builds
    cannot be vendored (platform_gem_unsupported), so there is no takeover
    leg for it.
  • /code-review high ran on the diff, twice. Fixed: the cache-dir archive
    check above, a stale test comment, a loose e2e assertion, the contract
    row's wording, and the ruby-platform archive of a gem whose lock names
    platform builds alone (now judged, as the hosted restore judges it).
  • One finding is not fixed: with no readable lock the cache-dir check
    cannot learn the platform builds and judges the ruby-platform archive
    alone. A vendored gem unwind rewrites the lock, so it has one; guessing
    builds from file names in the cache dir would name archives of builds
    the project does not lock.

https://claude.ai/code/session_011f9aeQwRPCRjnzyQ3rqcZr


Generated by Claude Code

Claims #2174: the vendored unwind judges only the ruby-platform
build of an unwound gem, so a precompiled platform build that a
hosted scan replaced stays patched.

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_011f9aeQwRPCRjnzyQ3rqcZr
A gem locked as precompiled platform gems that a hosted scan
patched and a vendored run then took over keeps its patched
installed copy after rollback, remove and vendor --revert
(#2174). The takeover leg of the platform prebuild capstone
fails on that until the unwind follows the lock's builds.

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_011f9aeQwRPCRjnzyQ3rqcZr
After a vendored unwind put a gem back on its upstream entry,
the refresh looked only for the ruby-platform installed copy
(<name>-<version>). A gem vendored from prebuilt platform gems
is installed as <name>-<version>-<platform>, so a copy a hosted
scan had replaced with the patched build stayed in the gem
home: the app kept loading the patch, and bundle pristine
failed on the restored checksum.

Look for the builds the restored lock names instead, as the
refresh that follows already does, and fall back to the
ruby-platform gem when the lock names none.

Fixes #2174

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_011f9aeQwRPCRjnzyQ3rqcZr
The vendored unwind's check of Bundler's cache dir judged only
<name>-<version>.gem, and stopped when the lock had no
ruby-platform entry: always, for a gem vendored from prebuilt
platform gems. A patched <name>-<version>-<platform>.gem that
a bundle cache took under the hosted pin went unnamed, and the
next install took it first.

Judge the archive of every build the restored lock names, as
the hosted restore does, with one warning per archive.

Refs #2174

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_011f9aeQwRPCRjnzyQ3rqcZr
Review follow-ups to the vendored unwind's cache dir check:

- A gem whose lock names platform builds alone still has its
  <name>-<version>.gem judged, as the hosted restore judges
  it. A gem none of whose builds can be judged stays silent.
- The takeover e2e leg counts the stale-cache warnings and
  asserts the archive was judged patched, not unchecked.
- CLI_CONTRACT.md states the archive shapes once for the
  whole upstream_gem_stale_cache row.

Refs #2174

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_011f9aeQwRPCRjnzyQ3rqcZr
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 20, 2026 06:04
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes gem install refresh and stale-cache detection on vendored unwind paths; incorrect build selection could leave patched gems on disk or miss cache archives, but behavior is scoped to Ruby/Bundler unwind and is heavily covered by new e2e and unit tests.

Overview
Fixes #2174: when a hosted scan patched a platform gem and vendoring later took over, rollback / remove / vendor --revert still left the patched install under <name>-<version>-<platform> and missed stale vendor/cache archives for those builds.

Vendored unwind refresh (after_vendored_unwind in gem_refresh.rs) no longer assumes only the ruby-platform leaf. It reads the restored lock via gem_upstream, derives builds with shared named_builds, and refreshes installed copies for every build the lock names (ruby fallback if the lock can’t be read).

Stale cache warnings (unwound_stale_cache / gem_stale_cache_after_unwind) now check the ruby-platform archive plus each locked platform build, emitting one upstream_gem_stale_cache advisory per bad archive; push_gem_stale_cache pushes all of them.

Contract text (CLI_CONTRACT.md, docs/ecosystems.md) and tests (core unit test + e2e takeover legs for hosted→vendored→unwind) document and lock in the new behavior. No new warning codes or exit semantics.

Reviewed by Cursor Bugbot for commit 123e56f. 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 123e56f. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. Fixes #2174. When a precompiled platform gem (nokogiri) went hosted → vendored and was then unwound (rollback, remove, vendor --revert), the installed <name>-<version>-<platform> copy stayed patched, and bundle pristine then failed on the restored checksum. The unwind now refreshes the platform builds the restored lock names, as it already did for ruby-platform gems, and warns about each patched platform archive left in Bundler's cache dir.

Risk: low. The change only touches the vendored gem unwind's follow-up steps. It reuses the build derivation and the per-token archive check that the hosted restore already ships. Output formats, exit codes and warning codes are unchanged. Vendoring platform gems is new on main and not in any release.

Look here

  • gem_refresh.rs:1251: the unwind reads the restored lock (offline, no registry request) and looks for every named build's installed copy.
  • gem_refresh.rs:1277: named_builds was moved out of refresh_restored unchanged. It falls back to the ruby-platform gem when the lock names no build.
  • [upstream/gem.rs:1120](https://github.com/SocketDev/socket-patch/blob/123e56f28896129e0fd39bd709ad03e44576ab4b/crates/socket-patch-core/src/patch/redirect/upstream/gem.rs#L1120): unwound_stale_cache now judges the ruby archive plus each locked platform archive, and skips the gem when none of its builds has a CHECKSUMS sha or a rubygems.org entry ([L1157](https://github.com/SocketDev/socket-patch/blob/123e56f28896129e0fd39bd709ad03e44576ab4b/crates/socket-patch-core/src/patch/redirect/upstream/gem.rs#L1157)).
  • [vendored_backend/mod.rs:406](https://github.com/SocketDev/socket-patch/blob/123e56f28896129e0fd39bd709ad03e44576ab4b/crates/socket-patch-cli/src/commands/vendored_backend/mod.rs#L406): the one caller now pushes one warning per archive.
  • [e2e_redirect_gem_build.rs:7454](https://github.com/SocketDev/socket-patch/blob/123e56f28896129e0fd39bd709ad03e44576ab4b/crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs#L7454): the two new takeover e2e tests, with and without CHECKSUMS.

Verified

  • Read the whole diff against the issue and the description; they match.
  • stale_cache_warning, lock_checksum and rubygems_locked already take a platform token, so the per-build check is sound.
  • The new unit test vendored_unwind_names_a_patched_platform_archive would fail on main: the old code returned early when the lock had no ruby-platform entry.
  • Ran cargo test -p socket-patch-core --lib redirect::upstream::gem locally: 33 passed.
  • CRLF locks are normalised (to_lf) before parsing.
  • CHANGELOG.md is untouched. No unrelated edits and no debug code.
  • CI on 123e56f: ci-ok and clippy are green, and Bugbot found no issues.

Changes I made: none.

Open questions: none. One known limit is described in the PR: with no readable lock, the cache-dir check judges only the ruby-platform archive. A vendored unwind always rewrites the lock, so this case should not come up.


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 20, 2026
Merged via the queue into main with commit 6af8cd6 Nov 20, 2026
128 of 134 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-vendored-unwind-platform-builds branch November 20, 2026 15:10
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