Repository navigation
Fix agent apply patching gem homes Bundler never loads (#2313) - #2402
Mikola Lysenko (mikolalysenko) merged 24 commits into
Conversation
Agent apply writes gem copies in gem env homes that Bundler never loads for a project with an explicit install path. Work in progress for #2313. 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_011f9aePyMvbD6rKimV9v2pL
`find_manifest_package_copies_reusing` dropped the gem copies Bundler never loads for the project inline: a copy in a `gem env` home under an explicit or deployment `path`, unless it is a default gem. Move that into `retain_bundler_loaded_gem_copies`, beside the Cargo twin, so the forward writer can use the same rule (#2313). The helper returns before the `gem env` probe when every copy sits in one of the project's own Bundler stores. No behaviour change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Under an explicit Bundler `path` (app config, `BUNDLE_PATH`) or deployment mode, Bundler installs every non-default gem into that path and never loads one from a `gem env` home (the system gems, an rbenv or asdf home, `~/.gem`). Agent `apply`, and the apply that `get` and `scan --mode agent` run, still wrote every copy of the patched version it found in those homes: a gem that belongs to other projects on the machine. On a fresh clone, with nothing in the path yet, it printed "N of N targeted patches applied" while the project had no patched gem. `find_all_packages_for_purls` now drops those copies by the rule `vex` and `apply --check` already follow: a copy in a `gem env` home Bundler does not load from is a write target only when it is a default gem. A gem left with no copy is `package_not_installed`, as any package that is not installed: a calm skip when the lock resolves it, the all-miss error when nothing does. `vendor` asks the same resolver whether a gem already holds its patch in agent mode, so it keeps agreeing with what `apply` writes. `rollback` and `remove` resolve without the rule and still restore such a copy, which an earlier release may have patched. Unchanged: a populated `vendor/bundle`, Bundler using system gems, default gems, `--global` runs, and the agent scan's discovery crawl. The fallback-home tests pinned the old write scope. They now pin the new one, and keep the best-effort failure semantics on the copies that are still written (a default gem beside a store copy). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CLI_CONTRACT.md's "Copy classes" paragraph said a copy in a `gem env` fallback home is patched whenever it matches and is writable. Say when it is a write target (Bundler using system gems, or a default gem), what a gem with no copy left reports, and that `rollback`, `remove` and `--global` runs are unchanged. The best-effort failure rule now reads on the copies that are still written. Both texts also say what was already true and unwritten: once the default `vendor/bundle` holds the project's gems, no local run searches the `gem env` homes, so a default gem there is not patched and `rollback` names `rollback <purl> -g` for a copy left patched there. Add the agent-mode rule to "Ruby gems: gems already installed" in docs/ecosystems.md, beside the hosted-mode rule for shared gem homes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Agent `apply` no longer writes a non-default gem in a `gem env` home that Bundler does not load for the project. The scan's crawl still found such a gem, so `scan --mode agent` selected its patch, downloaded and recorded it, and its apply step then reported it as not installed. With no other patch matched the scan exited 1 where it exited 0 before, and so did every later `apply`, over a gem the project does not use. The scan now drops such a gem from its candidates when the project's lock does not list it either: it is not counted, queried or shown, and nothing is downloaded or recorded for it. The question is put to `apply`'s own resolver (`gems_apply_never_writes`), so the scan cannot select what `apply` will not write. The filter runs after the prune universe is captured: a prune still counts the copy as installed and keeps a record an earlier release wrote for it, which `rollback` restores. Unchanged: a gem the lock lists (a fresh clone before `bundle install`) is still selected and recorded, and the apply step reports it as not installed on this host, exit 0. So are default gems, Bundler using system gems, a populated `vendor/bundle`, and hosted and vendored scans. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The "Copy classes" paragraph of CLI_CONTRACT.md said the scan's discovery was not narrowed by the write rule. Say that it selects by the same rule: a gem only another project's gem home holds, and that the lock does not list, is no candidate, while a gem the lock lists is still recorded. docs/ecosystems.md says the same for users, and what to do with a record an earlier release wrote for such a gem. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Bundler is never installed into a project's `path`. A Bundler updated with `gem install bundler` lives in the system gem home with its spec outside `specifications/default/`, and `bundle exec` loads it from there under any path (checked with Bundler 2.6.9 on Ruby 3.4 under `path vendor/gems`). The rule that drops the gem copies Bundler does not load treated it as another project's gem, so `apply` stopped patching it and `vex` and `apply --check` stopped judging it. `retain_bundler_loaded_gem_copies` now keeps a `bundler-<version>` copy in a `gem env` home, as it keeps a default gem, so every command that uses the rule agrees. Every installed version is kept: which one runs also depends on the lock's `BUNDLED WITH`, on `BUNDLER_VERSION` and on Bundler's own switch to the locked version, and a wrong guess would leave the running Bundler unpatched. `bundler-audit` and the like are ordinary gems. Two crawler comments gave rexml as an example of a default gem; it has been a bundled gem since Ruby 3.0, so they now name json and uri. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A manifest record for a gem whose only copy sits in a `gem env` home that Bundler does not load for the project made `apply` exit 1 with "matched no installed package" and "Check that the packages are installed and --cwd points to the right directory". Both are wrong: the gem is installed, for another project, and the record is one an earlier release wrote when it patched that copy, or one `get` of the exact purl wrote. The remedy is `socket-patch rollback <purl>`. Such a record is now a skip of its own. For the gems `apply` found no copy to write and no lock lists, it asks `rollback`'s resolver, which does not apply the rule, whether a copy exists. One that does is a `skipped` event with the existing `gem_fallback_home_skipped` code: the detail names the copy, says why it was left, and gives the `rollback` command, which is true whether or not the copy was ever patched. The human run prints it as a warning and counts the patch as "not applicable". It never flips the exit code and is not an all-miss. A gem with no copy anywhere and no lock entry keeps the all-miss exit 1, and one the lock lists keeps the lockfile-only skip. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…n and get Two gaps were left after the scan stopped selecting another project's gems. A gem the lock lists, with a copy only in a `gem env` home Bundler does not load, was shown as installed, downloaded and recorded, then reported as not installed by the apply step of the same run, and its recorded variant was picked by hashing the other project's copy. The same gem with no copy at all is lockfile-only: flagged `notInstalled` and skipped before any download. An agent-mode scan now takes the gems `apply` has no copy to write out of the crawl before the lockfile supplement, so both fresh-clone states go down the lockfile-only path and a gem the lock does not list is no candidate. Their purls stay in the prune universe, so a record an earlier release wrote is kept until `rollback` drops it. `get` in agent mode still took such a gem for an installed package: `get <name>` resolved it from the crawl, and `get <CVE|GHSA>` kept its version when narrowing to what is installed. Both now ask the same question as the scan. An exact purl is not narrowed for any package and is left alone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`rollback` and `remove` resolve gem copies without the rule `apply` follows, so that a copy an earlier release patched in a `gem env` home stays restorable. That also made them judge a copy `apply` never wrote: one holding neither side of the patch (another project's edit, or another build) failed the rollback with `hash_mismatch`, and `apply` no longer says anything about such a copy. It also meant the `rollback` that `apply` now names for another project's gem failed on exactly those copies. A copy that Bundler does not load for the project and that holds nothing of the patch is now left out of the rollback targets, whatever it holds. One that holds the patch stays, under every record of its release, so the variant narrowing still sees it. With no copy left the record is dropped as for a package that is not installed. The patched-content test is the one `patched_in_other_scope` already used, lifted into `copy_holds_patch`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CLI_CONTRACT.md "Copy classes" and the Ruby section of docs/ecosystems.md now say: Bundler and default gems are patched in the gem home they are installed in; a record for another project's gem is a `gem_fallback_home_skipped` skip that names `rollback`, exit 0; a gem the lock lists with no copy `apply` writes is lockfile-only in a scan, with nothing recorded; `get` by name or advisory follows the scan; `rollback` and `remove` leave out a copy that holds nothing of the patch. The contract's code table gains the `gem_fallback_home_skipped` row it never had. Both also state that a copy another project patched with the same patch in system-gems mode cannot be told from one this project patched, so `rollback` and `remove` restore it too. `rexml` was given as an example of a default gem. Since Ruby 3.0 it is a bundled gem with an ordinary spec, so it follows the ordinary rule: the examples are now `json`, `uri` and `set`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… skip The skip for another project's gem asks whether the lock lists the gem through the lock inventory, which leaves out a gem `Gemfile.lock` holds only as precompiled platform builds (`nokogiri (1.18.10-x86_64-linux)`). On a fresh clone with the same version in a gem home, `apply` then said the gem belongs to another project and named the `rollback` of a patch the project needs. Such a gem is the project's. It is taken out of the candidates, so it keeps the not-installed result it had. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
An agent-mode scan takes the gems `apply` has no copy to write out of its crawl. Their purls must stay in the prune universe: the copy is on disk and may hold a patch an earlier release wrote, and a prune that dropped the record and its blobs would leave nothing for `rollback` to restore it with. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PR SummaryMedium Risk Overview Unwritable shared copies surface as non-fatal Reviewed by Cursor Bugbot for commit 7c2d9e7. Configure here. |
`is_bundler_copy` told Bundler by its gem dir: the prefix `bundler-` followed by a digit. A gem named like `bundler-2fa` passes that test, so its copy in a `gem env` home would have been kept as one the project loads and patched there. `retain_bundler_loaded_gem_copies` has the purl as its map key, so it now compares the gem name in it to `bundler`, and the directory test is gone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`--save-only` forces agent mode, so `get <name> --save-only` ran the filter that leaves out a gem `apply` has no copy to write, and printed "No packages matching" for a gem that only a `gem env` home holds. It applies nothing, and `get <GHSA> --save-only` and an exact purl still recorded the patch. The name path skips the filter under `--save-only`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`apply` took "not in the lock inventory" as proof that a gem whose only copy sits in a `gem env` home is another project's. The inventory is also empty when no lock was read: a project with no lock yet (a gem library that commits none), and the layouts reported as `gem_lock_unsupported` (an unsupported `BUNDLE_GEMFILE` or `BUNDLE_LOCKFILE`, a `Gemfile` + `gems.rb` twin). There `apply` said the lock does not list the gem and advised a `rollback` that removes a patch the project may need. The skip now needs a Bundler lock that was read (`gem_lock_read_in`) and does not list the gem. Without one the gem keeps the `package_not_installed` result. The fixtures of the other-project cases get a lock, and the lockless case is pinned. `scan` and `get` do not use the lock to drop such a gem: to them a gem `apply` has no copy to write is not installed, with or without a lock, and the lock only decides whether it is reported as lockfile-only. They are unchanged. The skip also gets its own code, `gem_other_project_copy_skipped`. It shared `gem_fallback_home_skipped` with the best-effort skip, which is keyed by the base purl and means the patch is in effect on the copy bundler loads; this one is keyed by the manifest key and means nothing was applied, and a consumer could not tell them apart. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CLI_CONTRACT.md and docs/ecosystems.md follow the code: a gem is called another project's only under a lock that was read, the skip is `gem_other_project_copy_skipped` with its own table row, and `get --save-only` records by name. Two things are stated and not changed. A copy in a shared gem home that a release up to 5.1.0 patched beside the project's own copy stays patched after an upgrade, and `apply` does not mention it: `rollback <purl> --preserve-state` restores both copies and keeps the record, and the `apply` after it patches the project's copy alone. And an agent-mode scan does not report a gem the lock holds only as precompiled platform builds as not installed, with or without a copy in a `gem env` home. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
With a `gems.rb` beside the `Gemfile`, which pair loads depends on the Bundler that runs, so the `Gemfile.lock` on disk is not read. A gem whose only copy sits in a `gem env` home then keeps the not-installed result, as in a project with no lock: nothing calls it another project's, and no `rollback` is advised. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
One conflict, in the re-exports of vendor/lock_inventory/mod.rs: this branch adds gem_lock_read_in, main adds gem_resolved_builds_in. Both are kept. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
On Windows the in-process test `remove_base_purl_clears_all_platforms_and_rolls_back` overflowed its 2 MiB test thread stack on this branch and not on main. The branch awaited its new gem checks inline in `scan_project`, `rollback_patches_inner`, `apply_patches_inner` and `get`, and inside `find_all_packages_for_purls`, whose future those callers build in their own frames. A debug build keeps room in the caller's poll frame for each such future, also behind an inline `Box::pin(..)`, and those frames stay on the stack for the whole run, under the deepest chain. Each of those awaits now goes through `boxed`, which builds the future in its own frame so the caller holds a pointer: the candidate filter in scan and get, the rollback target filter, the other-project skip in apply, and the three calls of the shared rule in the resolvers. Measured on macOS as the smallest `RUST_MIN_STACK` at which a test passes (KiB; main, branch before, branch after): the remove test and an in-process rollback test 1972, 1988, 1972; an in-process scan and apply 1892, 1908, 1892. The CLI's main thread (`ulimit -s`) for a gem rollback: 1412, 1428, 1412; for a gem apply 1668 throughout. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The Windows run of this branch still overflowed the 2 MiB test thread in `remove_base_purl_clears_all_platforms_and_rolls_back`. The overflow is in the test's first step, the in-process scan that downloads and applies, not in the remove: scan -> download -> nested apply -> apply loop -> gem patch is the deepest chain of the CLI, and a debug build keeps each callee's future in its caller's poll frame. That chain had no room left before this branch. Measured on Linux as the smallest `RUST_MIN_STACK` at which the test passes, main needs 1996 KiB of the 2048 and this branch needed 1999, which is enough to tip a Windows debug build over. The four hand-offs of the chain now go through `boxed`, so each caller's frame holds a pointer: the download and apply in scan, the nested apply in the download engine, `apply_patches_inner`, and the gem patch in the apply loop. The same test now needs 1816 KiB, 180 below main. Nothing else changes: the same futures run in the same order. The shipped binary was never at risk, its main thread has 8 MiB on every platform. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeRtYGshMQUt3iHo5jY Assisted-by: Claude Code:claude-fable-5-2
…s-dir' into fix/vex-pnpm-store-windows The base branch took in main, which holds #2430 (the rule this branch extends) and the two fixes for the stack that the in-process gem tests overflow on Windows (#2402, #2432). Without them the Windows test shard fails on a test this branch does not touch. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Fixes #2313
Root cause
Agent
applytakes its gem write targets from the crawler's gem paths. Those keep everygem envhome whenever the defaultvendor/bundleroot holds no store, so that default gems (which only live there) can be patched. Nothing then limited the copies found in those homes to default gems. Under an explicit or deployment Bundlerpath, Bundler never loads a non-default gem from agem envhome, soapplywrote a copy that belongs to other projects on the machine, and on a fresh clone reported success while the project had no patched gem. The read-only side (vex,apply --check) already applied the right rule.The maintainer's decision (2026-12-01): yes
The behavior #2313 reports was the documented contract ("Copy classes" in
CLI_CONTRACT.md) and was pinned bytests/apply/in_process_gem_fallback_home.rs, so the fix waited for a ruling. The ruling: agentapplystops writing non-default gem copies ingem envhomes that Bundler does not load from for the project;rollbackandremovekeep finding them, since an earlier release may have patched them.What changes
retain_bundler_loaded_gem_copies(ecosystem_dispatch.rs) is the rulevexandapply --checkalready used, lifted out of them.apply's resolver now calls it too. Under apathor deployment mode, a copy in agem envhome is a write target only when it is a default gem (json,uri,set) or Bundler itself, which the project loads from the gem home under any path. A bundled gem (rexml,rake) follows the ordinary rule.Gemfile.locklists is reported as not installed (1 not found on disk, exit 0), and the shared copy is not touched.getselect by the same rule. This goes one step past the ruling, which left the scan's discovery alone: measured on a real project, leaving it alone turned a scan that exits 0 onmaininto exit 1, because the scan selected a patch thatapplythen refused to write. An agent-mode scan, andgetby name or by advisory, no longer select a gem that only another project's gem home holds. A gem the lock lists whose only copy is in such a home takes the existing not-installed path, as it does when no gem home holds it.getwith an exact purl is unchanged.applyskips such a record with agem_other_project_copy_skippedwarning that names the copy andsocket-patch rollback <purl>, counts it as not applicable and exits 0. Without a lock to go by (noGemfile.lock, or a layout the CLI does not read), the gem may be the project's own, so it is reported as not installed, as any package is.scan --synckeeps the record, so thatrollbackcan still restore the copy.rollbackleaves a copy that holds nothing of the patch. A copy in a gem home the project does not load is restored only when it holds the patch. Before, a diverged copy there failed the rollback withhash_mismatch.What it does not change
path(system gems): Bundler loads the project's gems from the shared homes, andapplypatches them there, as before.vendor/bundle:applylooks only there, as before.apply --globalpatches the shared homes whatever the project's Bundler settings are.rollbackandremoverestore a patched copy in a shared home even when another project, with nopathof its own, patched it with the same patch: the two cannot be told apart. The contract and the Ruby guide now say so.Measured (real Bundler 4.0.15, Ruby 3.4.10, rack 2.2.8 only in the gem home,
path vendor/gems)mainscan --mode agent, lock does not list rackscan --mode agent, lock lists rack, path emptypackage_not_installed, nothing downloaded or recordedapply,scan,rollbackover a record 5.1.0 wrote for the other project's rackrollbackrestores the copyget rackapply, gem installed in the path and in the gem homeapplied: 2)applied: 1), the shared copy untouchedapplyover a record, noGemfile.lock, rack only in the gem home, nothing else matchedpackage_not_installedThe first four rows were measured with real runs before the last round of review fixes, and the scan and
getrows again after the first round; the fifth is from the in-process tests and the sixth from a test of the last round. The warning in the third row isgem_other_project_copy_skippedon the final head.For a maintainer
getfollow the rule, beyond the ruling, for the reason above. The alternative was a scan that fails on another project's gem.BUNDLER_VERSIONand Bundler's own version switch, and a wrong guess would leave the running Bundler unpatched.get pkg:gem/<name>@<version>for another project's gem records the patch and takes the skip (exit 0); its variant is still picked by hashing that copy.pathand nothing installed in it yet gets exit 1 fromapplywheremainpatched the shared copy and exited 0 (last row of the table). Without a lock the CLI cannot tell the project's gem from another project's, so it says not installed and gives no rollback advice.applydoes not mention it. The guide names the remedy:socket-patch rollback <purl> --preserve-state, thensocket-patch apply.Review
/code-review highon the first version returned seven findings. Fixed:getdid not follow the rule; an old record madeapplyexit 1 with the wrong remedy; an updated Bundler was treated as another project's gem; a locked gem whose only copy is in a shared home was recorded and then reported not installed in the same run;rollbackfailed on a diverged shared copy; the docs namedrexmlas a default gem. Not changed, by the ruling:rollbackandremoverestore a shared copy another project patched with the same patch (stated in the contract).A second
/code-review high, on the head with those fixes, returned six. Fixed: the other-project skip was also taken by a project with no lock the CLI reads, and advised a rollback of a patch the project needs;get <name> --save-onlyran the filter although it applies nothing; Bundler was identified by a directory-name prefix thatbundler-2fawould match; one warning code carried two meanings, so the other-project skip has its own,gem_other_project_copy_skipped. Documented and not changed: items 6 and 7 above.Tests
tests/scan/scan_gem_unloaded_homes.rs,tests/get/get_gem_unloaded_homes.rs;tests/apply/in_process_gem_fallback_home.rsis rewritten (its best-effort skips now use a default-gem copy).mainwas merged in:cargo fmt --check,cargo clippy --workspace --all-features -- -D warnings, and the CLI lib (1086),apply(225),scan(274),get(167),rollback(142),remove(90),vendor(146) andvex(29) suites, the core Ruby crawler tests, and the stale-install and native-extension gem suites (with their real-Ruby capstones), all passing. Not run locally: the suites that need the network, Docker or the live API.remove_base_purl_clears_all_platforms_and_rolls_back(in_process_gem_multi_platform), whichmainhas too since0a1709bb2(run onmain). This branch boxes the checks it added and the four hand-offs of the scan to gem apply chain (bad4acf47,1c74732b2), and passes: run 46788431120 on1c74732b2, all 48 jobs. Run each copy's patch engine behind a pointer, so the in-process gem tests fit their stack on Windows #2432 fixes the same overflow in the core engines formain.Windows stack overflow in the in-process gem test
The dispatched Windows run failed twice on this branch in
remove_base_purl_clears_all_platforms_and_rolls_back("has overflowed its stack"). The overflow is in the test's first step, the in-process scan that downloads and applies. That chain (scan, download, nested apply, apply loop, gem patch) is the CLI's deepest, and onmainit already takes 1996 KiB of the 2048 KiB test thread (smallest passingRUST_MIN_STACK, Linux debug build). This branch took 1999.Commit 1c74732 boxes the four hand-offs of that chain, so each caller's poll frame holds a pointer instead of the callee's future. The same test now needs 1816 KiB, 180 below
main. The futures and their order are unchanged, and the shipped binary was never affected (its main thread has 8 MiB on every platform).Checked locally on 1c74732:
cargo fmt --checkon the three files,cargo clippy --locked -p socket-patch-cli --all-features -- -D warnings, the CLI lib tests (1090),apply(225),scan(274),get(167),in_process_agent_patch_replace(14) andin_process_gem_multi_platform(9), all passing. The Windows-only CI run dispatched on 1c74732 passed, both test shards included. The same test now also overflows onmainat 0a1709b in its own Windows run (test (windows-latest, 2)), so this commit clears that failure formainas well once it merges.The other failure in that Windows run,
composer_crawler::tests::equivalence::randomized_vendor_trees_match_the_async_oracleintest (windows-latest, 1), is in code this branch does not touch and passed in the earlier Windows run of this branch.Generated by Claude Code