Skip to content

Fix hosted gem unwind behind a private mirror (#2230) - #2233

Open
Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
agent/fix-gem-mirror-unwind
Open

Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
agent/fix-gem-mirror-unwind

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

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

Fixes #2230

Summary

A project whose Gemfile names a private gem server as its source
(source "https://gems.example.com") could not get a hosted gem pin undone
by socket-patch. After this PR:

  • Lock with CHECKSUMS: rollback / remove restore the pair byte for
    byte, and put the upstream gem back in the gem home, when
    SOCKET_RUBYGEMS_URL names that server. Without the setting they still
    refuse and change nothing, and the refusal now names the setting.
  • Lock without CHECKSUMS: the pair was already restored, but the
    patched build left in the gem home was not reported. It is now named by
    upstream_gem_stale_install with the files to remove.
  • The gem registry is no longer asked about a private server's gem when the
    unwind checks an archive in Bundler's cache dir.

No new network destination: the unwind contacts only the registry the user
names with SOCKET_RUBYGEMS_URL, as before.

Root cause

  1. restore_one (core, redirect/upstream/gem.rs) re-pinned a
    CHECKSUMS entry only when the upstream remote was the literal
    rubygems.org. The refresh, the source-gem lookup and the contract's
    SOCKET_RUBYGEMS_URL row ("a lock remote: equal to it counts as the
    registry") all use is_rubygems_remote, which accepts the setting's
    server. The restore now uses it too.
  2. PinJudge::probe (CLI, gem_refresh.rs) left every installed copy with
    an archive in its gem home to the archive check. That check needs the
    upstream sha256. With none (another remote under a lock without
    CHECKSUMS, or --offline / a registry error) nothing judged the copy.
    Such a copy is now judged by its pinned patch records, like a copy whose
    gem home has no archive.
  3. Found in review: stale_cache_warning asked the registry's compact index
    for any gem with no CHECKSUMS entry, whatever remote the lock resolves
    it from. A private gem's name went to the registry, and a public gem of
    the same name would have made the archive look patched. It is now gated
    on rubygems_locked, and the archive is named as unchecked.

Behavior changes to review

  • upstream_gem_stale_install now also appears after an unwind for a
    rubygems.org gem under a lock without CHECKSUMS when the run is
    --offline or the registry fails and the gem home holds an archive. The
    copy is judged by the patch record (proven patched: reported as stale;
    proven unpatched: silent; no record: "could not be checked"). Before, the
    copy was passed over. This is the rule the contract already had for a gem
    home without an archive.
  • The refusal text for a foreign remote changed (it strips credentials from
    the remote and names the setting). For a remote reached over plain http
    on another host the setting is not named, because it would be refused.

Evidence

Red before the fix, green after:

Test Covers Without the fix
upstream_restore_golden::gem_mirror_named_by_socket_rubygems_url_round_trips (core) restore with the setting naming the mirror, with a trailing slash, with credentials refused
upstream_restore_golden::gem_refusals_leave_everything_hosted (core) refusal names the setting; not for plain http failed
gem_refresh::tests::pin_judge_judges_an_archiveless_copy_by_any_of_its_pins (CLI unit) a copy with an archive and no upstream sha256 n/a (new arms)
e2e_redirect_gem_stale_install::hosted_unwind_restores_a_mirror_gem_when_the_registry_setting_names_the_mirror rollback and remove, CHECKSUMS lock: refusal, then exact restore and upstream install rollback_failed
e2e_redirect_gem_stale_install::hosted_unwind_names_a_patched_install_from_a_mirror_without_checksums lock without CHECKSUMS, online and --offline; cache-dir archive; registry not asked no warning; registry asked
e2e_redirect_gem_stale_install::hosted_unwind_leaves_an_upstream_install_from_a_mirror_alone no false warning for an unpatched copy passed (control)
e2e_redirect_gem_build::gem_hosted_unwind_restores_a_gem_from_a_private_gem_server (real Bundler, --ignored) CHECKSUMS lock from a real bundle install; after the unwind bundle install and bundle pristine pass and keep the upstream gem new

Local runs (Ruby 3.3.6, Bundler 4.0.18):

cargo test -p socket-patch-core --test upstream_restore_golden      65 passed
cargo test -p socket-patch-core --lib gem                           614 passed
cargo test -p socket-patch-cli gem_refresh                          11 passed
cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install 65 passed
cargo test -p socket-patch-cli --test in_process_rollback_hosted    41 passed
cargo test -p socket-patch-cli --test in_process_rollback_vendored  27 passed
cargo test -p socket-patch-cli --test e2e_redirect_gem_build -- --ignored   55 passed
cargo test -p socket-patch-cli --test e2e_vendor_gem_build -- --ignored     13 passed
cargo clippy --locked --workspace --all-features -- -D warnings     clean

CLI_CONTRACT.md and docs/ecosystems.md describe the new behavior. The
wrappers under npm/, pypi/ and gem/ need no change.

Code review

/code-review high ran on the diff. Fixed: the cache-dir registry lookup
(root cause 3), the refusal's remedy for a plain-http remote, a missing blank
line in the docs, and the wording of the SOCKET_RUBYGEMS_URL contract row.

Not fixed, with reasons:

  • The setting has two meanings. With SOCKET_RUBYGEMS_URL exported for
    every project, a lock that resolves from rubygems.org itself is also
    restored from the setting's server. That is the setting's existing
    stand-in meaning and predates this PR. The refusal asks for the setting on
    that re-run only. Separating "stand-in for rubygems.org" from "this
    private server" would be a new setting, which is a maintainer's call.
  • The wider offline warning (first item under "Behavior changes") is
    intended: the copy may hold the patched build and nothing else reports it.

Follow-ups (not in this PR)

  • A hosted scan still wires a mirror gem under a CHECKSUMS lock without
    saying that the unwind needs SOCKET_RUBYGEMS_URL, and its hold note
    offers a plain socket-patch rollback pkg:gem/.... The refusal now gives
    the way out, so this is a wording improvement.
  • Whether the unwind should read a private server's compact index on its
    own (with Bundler's configured credentials) is a design decision for a
    maintainer.

https://claude.ai/code/session_011f9aeRPXFzq8PM8WQb75Ke


Generated by Claude Code

Assisted-by: Claude Code:claude-fable-5-2
The hosted gem unwind re-pins a CHECKSUMS entry only for a gem whose
upstream remote is the literal rubygems.org. A project whose Gemfile
names a private mirror as its source locks its gems under that
remote, so rollback and remove always refused, also with
SOCKET_RUBYGEMS_URL set to that very server, which the rest of the
unwind already treats as the registry's stand-in.

Accept the stand-in in the restore too: the sha256 is read from that
server's compact index. The refusal that remains names the setting.

Refs #2230

Assisted-by: Claude Code:claude-fable-5-2
After a hosted unwind an installed copy with an archive in its gem
home was judged only against the upstream sha256. With none to judge
by (a gem from another remote under a lock without CHECKSUMS, or an
offline run), nothing judged it, so the patched build the scan had
put there was left with no upstream_gem_stale_install warning.

Judge such a copy by its pinned patch records, like a copy whose gem
home has no archive.

Refs #2230

Assisted-by: Claude Code:claude-fable-5-2
Refs #2230

Assisted-by: Claude Code:claude-fable-5-2
Refs #2230

Assisted-by: Claude Code:claude-fable-5-2
Review follow-ups for the gem mirror unwind:

- The cache-dir archive check asked the gem registry for the sha256
  of any gem with no CHECKSUMS entry, also one the lock resolves from
  another gem server. It names that archive as unchecked instead.
- The refusal no longer names SOCKET_RUBYGEMS_URL for a server
  reached over plain http, which the setting refuses.
- Doc wording.

Refs #2230

Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 06:04
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes rollback/remove behavior for private gem sources and stale-install detection; mistakes could refuse restores or mis-report patched on-disk gems, but scope is limited to gem hosted unwind/refresh paths with extensive new tests.

Overview
Fixes hosted gem unwind when the lock resolves gems from a private mirror instead of rubygems.org.

Upstream restore now treats SOCKET_RUBYGEMS_URL like the rest of the gem stack: CHECKSUMS re-pinning accepts the configured registry (not only literal rubygems.org), refusals name that setting as the remedy, and Bundler cache-dir stale checks no longer query the default registry for gems from another remote.

Installed-copy refresh (PinJudge::probe) no longer skips copies that have a .gem archive when there is no upstream sha256 (e.g. lock without CHECKSUMS, --offline, or private remote). Those copies are judged from patch records like archiveless installs, with clearer upstream_gem_stale_install / upstream_gem_stale_cache messaging.

CLI contract and ecosystems docs were updated to match; golden, unit, and e2e tests cover mirror restore, refusal text, and stale reporting.

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

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. Lets rollback / remove undo a hosted gem pin for a project whose Gemfile uses a private gem server: under a lock with CHECKSUMS, the restore now accepts the server that SOCKET_RUBYGEMS_URL names (the same rule the refresh and the contract already used), and the refusal without it names the setting. Under a lock without CHECKSUMS, an installed copy that has an archive but no known upstream sha256 is now judged by its patch records, so a leftover patched build gets reported. The registry is no longer asked about a private server's gem when a cached archive is checked.

Risk: low. It only touches the gem unwind path. The restore accepts one more remote, and only the one the user names explicitly. The other two changes add a warning or skip a registry lookup, and neither changes any files. The refusal text now strips credentials from the remote URL, where before it printed it raw.

Look here

  • [gem.rs:1426](https://github.com/SocketDev/socket-patch/blob/c55705d08e123361fc911d654fecc68cf363141b/crates/socket-patch-core/src/patch/redirect/upstream/gem.rs#L1426): the restore uses is_rubygems_remote, and the refusal leaves out the setting for a plain-http server (the setting would be refused there).
  • [gem.rs:1086](https://github.com/SocketDev/socket-patch/blob/c55705d08e123361fc911d654fecc68cf363141b/crates/socket-patch-core/src/patch/redirect/upstream/gem.rs#L1086): stale_cache_warning asks the registry only about a gem the lock resolves from it.
  • gem_refresh.rs:1187 and :1505: PinJudge::probe now skips a copy only when the archive check holds an upstream sha256 for its leaf.
  • [e2e_redirect_gem_stale_install.rs:3860](https://github.com/SocketDev/socket-patch/blob/c55705d08e123361fc911d654fecc68cf363141b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs#L3860) and [:3977](https://github.com/SocketDev/socket-patch/blob/c55705d08e123361fc911d654fecc68cf363141b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs#L3977): the end-to-end tests, CHECKSUMS and no-CHECKSUMS, online and --offline.

Verified. I read the full diff of the Rust sources, CLI_CONTRACT.md and docs/ecosystems.md. rubygems_locked and the restore share is_rubygems_remote, so a mirror named by the setting counts as the registry in both. by_archive is the leaves the archive check holds a sha256 for, or reports as unverified itself, so no copy is judged twice by archive and record. The new unit arms in pin_judge_judges_an_archiveless_copy_by_any_of_its_pins would fail on main, because the old probe skipped any archived copy. CHANGELOG.md is untouched and the branch merges into main cleanly. CI at c55705d: all 127 check runs passed or were skipped, ci-ok and clippy passed, and Bugbot found no issues. There are no open review threads.

Changes I made. None.

Open questions. The PR's own "Not fixed" note, which predates this PR: with SOCKET_RUBYGEMS_URL exported for every project, a lock resolving from rubygems.org is also restored from the setting's server. Separating the two meanings would take a new setting, which is a maintainer's call. It doesn't block this PR.


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 25, 2026
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

2 participants