Skip to content

Fix hosted scan leaving the upstream .gem beside an agent-patched gem (#2089) - #2103

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-hosted-scan-agent-patched-gem-archive
Nov 17, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-hosted-scan-agent-patched-gem-archive

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

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

Fixes #2089

Summary

A hosted scan over a gem that agent mode had patched in place left the
upstream .gem in the gem home's cache/, under a lock that now pins the
patched gem. bundle pristine and bundle install --redownload reinstall
from that archive: with a CHECKSUMS section Bundler removes the gem and
exits 37 (and every later bundle install fails too), without one it
silently reinstalls the unpatched gem. The run printed no warning.

The scan now replaces that archive with the verified patched one. The gem
dir is not touched: its files are already the patched ones, and agent mode
keeps a native gem's original build inside it. Where the run may not write
(a shared gem home, --no-gem-install-refresh, an archive it cannot fetch
or verify) it warns and names the archive.

Root cause

gem_stale_install_warnings (commands/scan/hosted.rs) judged an installed
dir by the agent-mode manifest record when one existed and skipped the
<home>/cache/<leaf>.gem comparison in that case. The record proves the
files, but says nothing about the archive RubyGems reinstalls from.

The change

  • Probe (scan/hosted.rs): files a record proves patched, beside a
    readable archive whose sha256 is not a patched artifact's, are reported as
    stale with StaleGem::in_place. With the patched archive, no readable
    archive, or no patched sha256 to compare, the record's verdict stands as
    before. The sha256 comparison ignores case, as the refresh's already did.
  • Refresh (commands/gem_refresh.rs): an in_place gem's target is its
    home archive (Target::CacheArchive, the existing staged-and-renamed
    replacement), under the same policy as an installed copy: project-local
    by default, a shared home only with --gem-refresh-shared. That archive
    is not listed with the files to commit.
  • Messages: the note, progress line, warning and headline for these
    gems say "patched in place" and name the archive. They never call the
    install unpatched, and the delete list is the archive alone.
  • CLI_CONTRACT.md ("Gem stale-install guard") and docs/ecosystems.md
    ("Ruby gems: gems already installed") describe the new rule.

No core crate change, and nothing in the npm/, pypi/ or gem/ wrappers
depends on this.

Evidence

Red first: commit 81b1165 holds the tests only; the unit test and both
hermetic e2e tests failed there, and the two real-Bundler capstones failed
with the fix stashed.

Test Covers
scan::hosted::tests::gem_stale_probe_flags_an_upstream_archive_beside_an_in_place_patch the probe rule, incl. patched archive, no archive, upper-case sha256
gem_refresh::tests::in_place_patched_gems_are_never_called_unpatched dry-run note, warnings, headlines, remedies
e2e_redirect_gem_stale_install::refresh_replaces_the_upstream_archive_beside_an_agent_patched_install dry run, refresh, gem dir untouched, quiet re-scan, rollback restores upstream files and archive
e2e_redirect_gem_stale_install::an_agent_patched_install_the_run_may_not_replace_is_warned_about --no-gem-install-refresh, shared home, --gem-refresh-shared
e2e_redirect_gem_build::gem_hosted_scan_refreshes_an_agent_patched_warm_install (and _with_a_checksums_lock) real Bundler: install, --redownload, pristine and a frozen install all succeed and keep the patch

Run locally (Ruby 3.3.6, Bundler 4.0.18):

  • cargo test -p socket-patch-cli --lib: 948 passed
  • --test e2e_redirect_gem_stale_install: 33 passed
  • --test e2e_redirect_gem_build -- --include-ignored: 46 passed
  • --test e2e_vendor_gem_build, e2e_gem_native_ext_build (--include-ignored), in_process_rollback_hosted, cli: passed
  • cargo clippy --locked --workspace --all-features -- -D warnings (the CI command): clean.
    --all-targets reports 37 errors in test helpers on main as well
    (tests/prebuilt_common, duplicate common modules); none is in a file
    this PR touches.

CI on d41f8cb: ci-ok and clippy green, Cursor Bugbot passed with no findings, no open review thread.

Review (/code-review high)

The first version replaced the whole gem dir. The review showed that this
would drop agent mode's native-build marker and original-build backup, so
the fix was reworked to replace the archive only. Also fixed from it: the
case-sensitive sha256 comparison, hashing the archive only when the result
is used, the human bullet and mixed headline wording, stale comments, and
the duplicated test fixture (now common::write_in_place_patch_record).

Not changed:

  • stale_detail keeps a separate branch for in-place gems. Its sentences
    have another subject and delete list than the two arms below it; folding
    them would thread four more variables through those arms.
  • After the scan the home archive is the patched one, so a rollback that
    cannot fetch the upstream gem (--offline, a gem from a private remote)
    restores the files from blobs but leaves that archive, and warns
    upstream_gem_stale_install. This is what every hosted refresh already
    does for a warm install.

Follow-ups (not in this PR)

From the issue's last paragraph: the agent manifest record stays after the
switch, so socket-patch list shows the gem twice. That is a separate
question (should the hosted switch migrate the record out?).

https://claude.ai/code/session_011f9aePQWkJCgXswur7SmP6


Generated by Claude Code

Hosted scan over a gem agent mode patched in place leaves the
upstream .gem in the gem home's cache under a lock that pins the
patched checksum. Claim commit; the fix follows.

Assisted-by: Claude Code:claude-fable-5-2
A hosted scan over a gem agent mode patched in place leaves the
upstream .gem in the gem home's cache under a lock that pins the
patched checksum (#2089). The probe unit test and the two e2e tests
fail until the next commit.

Assisted-by: Claude Code:claude-fable-5-2
The hosted scan's stale-install probe judged an installed gem by an
agent-mode manifest record when one existed and never looked at the
archive RubyGems reinstalls from. An in-place patch keeps the
upstream archive, so once the lock pinned the patched checksum,
bundle pristine and bundle install --redownload removed the gem and
failed on the checksum (exit 37), or reinstalled the unpatched gem
on a lock without CHECKSUMS.

Files a record proves patched beside a readable archive that is not
a patched artifact's now count as stale. The existing refresh then
replaces the dir and the archive with the verified patched gem, or
the run warns where it may not (shared gem home, refresh off). The
messages say the copy is patched in place and name the archive
instead of calling the install unpatched.

Fixes #2089

Assisted-by: Claude Code:claude-fable-5-2
Review of the first fix: swapping the whole gem dir would drop what
agent mode keeps inside it (a native gem's build marker and its
original-build backup), so a later rollback could not restore the
prebuilt binaries. The files are already patched; only the archive
RubyGems reinstalls from is wrong.

The refresh now replaces <home>/cache/<leaf>.gem alone for such a
copy and leaves the gem dir as it is. The manual remedy is that one
archive, not the specifications entry. The sha256 comparison ignores
case and the archive is hashed only when the verdict needs it. The
shared-home bullet and the mixed headline no longer call a copy
patched in place unpatched.

Refs #2089

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

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes gem stale detection and on-disk refresh behavior during hosted scan; incorrect logic could wrongly refresh or skip gems, but scope is limited to Ruby gem refresh and messaging with broad test coverage.

Overview
Fixes #2089: after switching to hosted patches, gems that agent mode patched in place could still have the upstream .gem in the gem home’s cache/. Bundler operations like bundle pristine and bundle install --redownload reinstall from that archive, which either fails on lock checksums or brings back unpatched code.

The stale-install probe now treats “files proved patched by an agent manifest, but the sidecar archive is not a patched artifact” as stale (StaleGem::in_place). The refresh replaces only <home>/cache/<leaf>.gem, leaving the installed gem directory untouched. Warnings and progress text distinguish in-place cases (archive-only delete list, dedicated headlines) from fully unpatched installs. CLI_CONTRACT.md, ecosystems.md, unit tests, and Bundler e2e tests document and lock in the behavior.

Reviewed by Cursor Bugbot for commit d41f8cb. Configure here.

The contract listed the plain 'still unpatched' headline as the one for
gems patched in place. It is the headline when none was; the in-place
and mixed headlines follow.

Assisted-by: Claude Code:claude-opus-5-5
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief

What it does
Fixes #2089. After scan --mode agent patched a gem in place, a later scan --mode hosted left the upstream .gem in <gem home>/cache/. The lock now pins the patched gem, so bundle pristine / install --redownload either failed with exit 37 after removing the gem, or quietly reinstalled the unpatched one. The hosted scan now counts that archive as stale and replaces it with the verified patched .gem, without touching the gem dir. Where it may not write (a shared gem home, --no-gem-install-refresh, a fetch or verify failure), it warns and names the archive.

Risk: medium. On the agent→hosted path the scan now writes a file in the gem home that it used to leave alone. The write uses the existing staged, sha256-checked archive replacement, and the same policy as before applies: project-local by default, a shared home only with --gem-refresh-shared. No core-crate or wrapper change.

Look here

  • scan/hosted.rs:461: the probe. A gem the agent record proves patched, beside a readable archive whose sha256 isn't a patched artifact's (case-insensitive), is flagged in_place.
  • scan/hosted.rs:523: an in_place dir skips the patched/positive short-circuits.
  • gem_refresh.rs:244: the target is the home archive (Target::CacheArchive), never the gem dir. It goes through the same may_replace policy and is kept out of the files-to-commit list.
  • gem_refresh.rs:355: the manual delete list is the archive only, never the specifications entry.
  • gem_refresh.rs:643: the new and mixed headlines.

Verified

Changes I made: none.

Open questions: none.


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 17, 2026
Merged via the queue into main with commit 6eb0a19 Nov 17, 2026
49 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-hosted-scan-agent-patched-gem-archive branch November 17, 2026 15:21
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Nov 17, 2026
Main gained the in-tree build products fix (#2108) and the hosted scan's
refresh of the archive beside an agent-patched gem (#2103). Both touch
files this branch changes; three hunks conflicted, each resolved by
keeping both sides.

crates/socket-patch-core/src/patch/gem_ext.rs,
`recover_interrupted_rebuild_sync`: this branch's `refuse_link(&ext, ..)`
(a link, or an extension dir that cannot be probed, is refused) in place
of main's unchanged `is_symlink` check, followed by main's new restore of
the gem's tree from the snapshot of an interrupted in-place build.

crates/socket-patch-cli/src/commands/gem_refresh.rs, `report`: main's two
headlines for gems patched in place become arms of this branch's `known`
headline, which the count of copies that could not be checked is then
added to. The words "the switch to hosted patches" come from `switch`.

crates/socket-patch-cli/src/commands/gem_refresh.rs,
`stale_after_restore`: this branch's listing of a copy that cannot be
judged whether or not its archive is cached, with main's new
`in_place: false` field.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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