Skip to content

Fix hosted gem unwind skipping a copy with no cached archive (#2092) - #2130

Merged
Mikola Lysenko (mikolalysenko) merged 6 commits into
mainfrom
agent/fix-hosted-gem-unwind-pruned-cache
Nov 18, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 6 commits into
mainfrom
agent/fix-hosted-gem-unwind-pruned-cache

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

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

Fixes #2092

Summary

After a hosted scan replaces an installed gem with the patched build,
rollback / remove are meant to put the upstream gem back. In a gem
home with no cache/ dir (Bundler 4.1's prune setting, the
rm -rf "${BUNDLE_PATH}"/ruby/*/cache of the Dockerfile Rails
generates, gem cleanup) they restored the Gemfile pair, left the
patched gem installed, and said nothing about it. No later
bundle install replaces that copy, so the app kept loading patched
code under the upstream lock.

Now the unwind judges such a copy by the record of the patch its gem was
pinned to, which is how the scan direction already judges a copy in a
pruned home, and:

Installed copy with no archive in its gem home Before Now
holds the pinned patch left patched, no warning replaced with the sha256-verified upstream gem (upstream_gem_install_refreshed)
holds the pinned patch, nothing proves the upstream sha256 (no CHECKSUMS, registry error) left patched, no warning left, named by upstream_gem_stale_install with the files to remove
no record to judge it by (--offline, API error, a patch the API no longer has) left, no warning left, named by upstream_gem_stale_install as "could not be checked", with the reason and the files to remove
is the upstream gem (the scan never replaced it) left left, no warning, no gem download

Nothing changes for a copy whose gem home still has its archive, or for
any other ecosystem. An unwind with no archive-less copy fetches no
patch record.

Root cause

stale_after_restore in
crates/socket-patch-cli/src/commands/gem_refresh.rs judged an
installed copy only by <home>/cache/<leaf>.gem. With no archive to
read there was no evidence, so the copy was skipped. The hosted unwind
keeps no patch record of its own (v5 hosted mode has no ledger), so the
record mechanism #2122 added for the vendored unwind had nothing to
work from here.

How it works

  • PinJudge (new, gem_refresh.rs) looks at each installed copy of a
    restored gem that has no readable archive, fetches the record of each
    patch that gem was pinned to (/patches/view/<uuid>, once per uuid),
    and calls the copy stale when every file a record lists hashes to its
    afterHash. Stale copies join the existing refresh, which already
    reinstalls from the upstream archive and creates no cache/ dir.
  • The pins' uuids come from the restore outcome
    (hosted_unwind.rs). run_hosted_leg now takes the run's
    RunApiClient, so rollback and remove build no second client and
    none at all when no record is needed. Its four callers pass theirs.
  • view_record is the one rule for "a view response that can judge",
    shared with the scan's ViewFallback.
  • report's headline counts a gem with one stale and one unchecked
    copy as stale (it could not occur before).

Evidence

Red first: f392882 and 383e7e9 add the end-to-end tests; on those
commits 3 of 4 mock-registry tests and the real-Bundler test fail with
the installed file still PATCHED. They pass from 131f1bc on.

At 10a4c86 (Ruby 3.3.6, Bundler 4.0.18):

  • cargo test -p socket-patch-cli --lib: 953 passed.
  • cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install --test e2e_redirect_gem_build --test e2e_vendor_gem_build --test e2e_gem_native_ext_build -- --include-ignored:
    52, 49, 18 and 34 passed.
  • cargo clippy --locked -p socket-patch-cli --all-features -- -D warnings
    (CI's command): clean. --all-targets still reports the lints
    main has in tests/common and tests/prebuilt_common; none is in
    a file this PR touches.
  • cargo fmt --check on the touched files: clean.

Tests that cover Fixes #2092:

  • gem_hosted_unwind_restores_an_install_whose_home_cache_was_pruned
    (e2e_redirect_gem_build.rs, real Bundler): rollback and remove
    leave the upstream gem installed, and bundle install keeps it.
  • e2e_redirect_gem_stale_install.rs (mock registry):
    hosted_unwind_restores_an_install_whose_home_cache_was_pruned
    (both commands, one record fetched, no cache/ created),
    hosted_unwind_reports_the_pruned_home_refresh_in_a_human_run
    (dry run, progress line, no "keeps its patched bytes" note),
    hosted_unwind_names_a_pruned_home_install_no_record_can_judge
    (API error: nothing changed, warning with reason and files),
    hosted_unwind_names_a_pruned_home_install_with_no_upstream_sha256
    (--offline fetches nothing; registry error),
    hosted_unwind_leaves_an_upstream_install_in_a_pruned_home_alone.
  • Unit, gem_refresh.rs:
    pin_judge_judges_an_archiveless_copy_by_any_of_its_pins (two pins,
    a missing record, unreadable record files, a copy with its archive),
    a_gem_with_a_stale_copy_and_an_unchecked_one_counts_as_stale.

Docs: the "Gem stale-install guard" section and the
upstream_gem_stale_install row of CLI_CONTRACT.md, and "Ruby gems:
gems already installed" in docs/ecosystems.md.

Review (/code-review high, two passes)

Fixed (1de2023, 10a4c86): a gem two pins named was judged by one
record; a fetched record judged every copy of the gem, not only the
archive-less one; any API error stopped later fetches, and an
unreachable API cost a timeout per gem; a copy missing a record's file
was taken as unpatched; no judgment when nothing proved the upstream
sha256, or for a gem on another remote; a refreshed-plus-unchecked gem
was reported as needing no reinstall; a second API client; a reason
that said "could not be fetched" when the API had answered.

Not fixed, on purpose:

  • Withdrawn patches. Scan's withdrawn-patch restore runs the same
    judge, but the API may no longer serve that patch's record. The copy
    is then reported as not checked, with the files to remove, where it
    was passed over before. Replacing it would mean judging against the
    upstream archive's own files in place of a patch record: a larger
    change to the refresh, better as its own PR.
  • A copy with its archive when nothing proves the upstream sha256
    (rollback --offline on a lock without CHECKSUMS). It is still
    not judged, as on main. The record judge could cover it, but that
    changes what every offline rollback on Bundler 2.5 and older prints,
    and it is not this issue.
  • ViewFallback and PinJudge stay two types. They share
    view_record; the scan's holds a built client and retries after an
    error, the unwind's builds the client lazily and stops after a
    network error. Merging them would change the scan path.
  • One more crawl of the gem homes per hosted gem unwind (two when
    an archive-less copy was refreshed). The archive check and the judge
    each walk the homes; folding them into one walk is a refactor of
    stale_after_restore.

CI and Bugbot

All 88 checks pass on 10a4c86, ci-ok and clippy among them. The
three ruby / bazel jobs were cancelled by a duplicate run the
ready-for-review event started, and passed on one re-run. Bugbot found
no issues.

Follow-ups

https://claude.ai/code/session_011f9aeQv3najd5px4XRiFzQ


Generated by Claude Code

Refs #2092. Opens the draft PR that claims the issue.

Assisted-by: Claude Code:claude-fable-5-2
Four end-to-end tests for #2092, in a gem home whose cache/ dir is
gone: rollback and remove put the upstream gem back, a human run
reports it, a copy no patch record can judge is named, and an
upstream copy is left alone. The first three fail: the unwind judges
an installed copy by its cached archive alone.

Assisted-by: Claude Code:claude-fable-5-2
A capstone for #2092 in the real-Bundler suite: after a hosted
install whose gem home lost its cache/ dir, rollback and remove must
leave the upstream gem installed and bundle install must keep it. It
fails: the patched copy stays. The mock-registry test of a copy no
record can judge drops its offline arm, which no hosted unwind
reaches (see the fix).

Assisted-by: Claude Code:claude-fable-5-2
A hosted unwind (rollback, remove, a withdrawn-patch restore) judged
an installed gem only by <home>/cache/<leaf>.gem. In a gem home with
no cache dir (Bundler 4.1 prune, a slim image, gem cleanup) the copy
the hosted scan had replaced with the patched build was passed over:
no refresh and no warning, and no later bundle install replaced it.

The unwind now fetches the record of the patch such a gem was pinned
to, as the scan direction does, and judges the copy by its afterHash
map through the records mechanism the vendored unwind uses. A copy
holding the patch is replaced with the upstream gem. When the record
cannot be fetched the copy is left alone and reported as one that
could not be checked, with the files to remove. An unwind with no
such copy fetches nothing.

Fixes #2092

Assisted-by: Claude Code:claude-fable-5-2
Review of the #2092 fix found that the record judgment was keyed by
purl and bolted on beside the archive check:

- a gem two pins named was judged by the first pin's record only;
- the fetched record then judged every copy of the gem, so whether a
  copy with its archive was replaced depended on a sibling gem home;
- an unreachable API cost one timeout per gem;
- a copy was still passed over when nothing proved the upstream
  sha256 (a lock without CHECKSUMS, offline or a registry error);
- a gem with one refreshed and one unchecked copy was reported as
  needing no reinstall, and one with a stale and an unchecked copy
  lost its place in the warning's headline count;
- the leg built an API client of its own.

PinJudge now judges each installed copy that has no readable archive
by the record of any patch its gem was pinned to, stops asking after
the first fetch the API does not answer, and uses the run's API
client, which run_hosted_leg now takes. Without an upstream sha256 a
copy proven patched is reported as left on the patched build.

Refs #2092

Assisted-by: Claude Code:claude-fable-5-2
A second review of the #2092 fix:

- any API error stopped every later record fetch. Only a request
  the API did not answer (a network error) does now;
- a copy missing a file its record lists was taken as free of the
  patch. A record judges only files it could read, so such a copy is
  reported as not checked;
- a gem restored to a remote other than rubygems.org was not judged
  when nothing proved its upstream sha256. The restore that ran is
  what says the gem is back on its upstream entry;
- the reason told a user to expect a fetch failure when the API had
  answered that it has no record of the patch;
- the judge and the archive check now use one test for a readable
  archive, and after_restore takes the pins alone.

Refs #2092

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

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes rollback/remove gem refresh behavior and adds patch API fetches during unwind; incorrect judgment could leave patched bytes on disk or mis-report staleness, but scope is limited to Ruby hosted unwinds with pruned gem caches.

Overview
Fixes #2092: hosted gem unwinds (rollback, remove, withdrawn-patch restore, etc.) no longer ignore installed gems when the gem home has no cache/ (Bundler prune, slim images, gem cleanup). Previously only <home>/cache/<leaf>.gem decided staleness, so patched copies stayed on disk with no warning under a restored upstream lock.

The unwind path adds PinJudge, which for archive-less copies fetches each pin’s patch record via the run’s RunApiClient (/patches/view/<uuid>, once per UUID) and treats copies whose files match the record’s afterHash as still patched—then refreshes them like other stale installs or emits upstream_gem_stale_install when the record can’t be fetched (--offline, API errors, withdrawn patches). after_restore now takes a purl → patch UUID map instead of a flat restored list; run_hosted_leg and remove / rollback / vendor / scan withdrawn thread the shared API client through. view_record is shared with the hosted scan path for consistent “judgeable” records.

Reporting treats a gem with one known-stale copy and one uncheckable copy in another home as stale in the human headline. Contract docs and ecosystem notes describe the pruned-cache behavior. E2E and unit tests cover refresh, warnings, offline/API failure, and upstream-only installs left alone.

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

@mikolalysenko

Mikola Lysenko (mikolalysenko) commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for 10a4c86

What it does: After a hosted scan replaces an installed gem with the patched build, rollback and remove restore the Gemfile pair. In a gem home with no cache/ dir (Bundler prune, the Dockerfile Rails generates, gem cleanup) they used to leave the patched copy in place without a word. Now a new PinJudge fetches the record of the patch that gem was pinned to. When the copy's files match that record, the copy is reinstalled from the sha256-verified upstream gem. When nothing can judge the copy (--offline, an API error, a withdrawn patch), it is left as it is and reported as upstream_gem_stale_install with the files to remove.

Risk: Low.

  • It only acts on archive-less copies, which were silently skipped before. Copies that keep their archive, and every other ecosystem, run the same path as before.
  • A copy is replaced only when the upstream sha256 is known.
  • No patch record is fetched unless such a copy exists, and fetching stops after the first network error.
  • The visible change is new warnings. Exit codes are unchanged.

Look here

  • gem_refresh.rs:1015: PinJudge, and :1063 probe, which decides how a copy is judged by its pins
  • gem_refresh.rs:1042: only ApiError::Network stops further fetches
  • [gem_refresh.rs:1242-1310](https://github.com/SocketDev/socket-patch/blob/10a4c863ba17a25dc41cc037b40e430df65d6937/crates/socket-patch-cli/src/commands/gem_refresh.rs#L1242-L1310): refresh_restored merges stale and unjudged copies, then re-probes after the refresh
  • gem_refresh.rs:668: the headline counts a gem with one stale and one unchecked copy as stale
  • hosted_unwind.rs:104: how the pins and the run's API client are passed in

Verified

Changes I made: none.

Open questions: none. The description lists four limits left on purpose, including a new "could not be checked" warning when scan restores a withdrawn patch in a pruned home. I agree with leaving them.


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 18, 2026
Merged via the queue into main with commit 73b2029 Nov 18, 2026
89 of 92 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-hosted-gem-unwind-pruned-cache branch November 18, 2026 21:51
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

4 participants