Skip to content

Fix gem dir with no gemspec counted as installed (#2087) - #2124

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-dir-without-gemspec-counted-installed
Nov 18, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-dir-without-gemspec-counted-installed

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 #2087

Summary

socket-patch vex and apply --check no longer count the leftover directory of a gem build that is not installed. After a project moves a gem from its precompiled build to the ruby platform build (the path hosted mode sends people down) and runs the scan's own rm -f <gemspec> <archive> && bundle install remedy, gems/nokogiri-1.16.0-x86_64-linux/ is left with no gemspec beside the patched gems/nokogiri-1.16.0/. vex judged it as an installed copy, found the original bytes and exited 1 with not_applied. It now attests the gem.

The change is read-only: apply, rollback, remove and the scan resolve gem dirs exactly as before.

Root cause

The Ruby crawler returns every gems/<name>-<version>[-<platform>]/ dir that has a lib/ as a copy (#1092, so that a platform build installed beside the ruby one is patched and verified). It never asks whether the gem home has a gemspec for the dir. RubyGems and Bundler load a gem by specifications/<leaf>.gemspec alone, so a dir without one is not installed, and the verification gates were refusing a patched gem over files nothing can load.

The rule

retain_loaded_gem_builds (ruby_crawler.rs) takes the copies of one gem version and drops a dir when its gem home has no gemspec for it (specifications/ or specifications/default/) and another of those copies in the same home has its own. find_manifest_package_copies_reusing (ecosystem_dispatch.rs), the one function vex and apply --check collect copies through, applies it next to the existing #1098 and #599 filters.

Everything short of proof keeps the dir a copy, so a gem is never attested on a guess:

Evidence

Test Covers
e2e_vex_redirect::gem_hosted_ref_is_attested_beside_a_leftover_platform_dir_with_no_gemspec The issue's state in hosted mode (lock wired to the patch registry, patched ruby build, unpatched platform dir with no gemspec), for a bundle path inside the project and one outside it. vex attests; with the gemspec back it exits 1.
apply::in_process_gem_multicopy::the_leftover_dir_of_an_unlisted_platform_build_is_not_a_copy The same for an agent-mode record: vex and apply --check pass, both refuse once the gemspec is back, and apply / rollback still write and restore the dir.
ruby_crawler::tests::retain_loaded_gem_builds_* (4) The rule itself: both directions, default gems, both builds installed, no listed copy, a listed copy in another home or with no dir, a dir outside gems/, a gemspec that cannot be probed, and a standalone setup.rb (scoped and flat homes, with and without a require path).

Both CLI tests fail without the filter (vex exits 1, not_applied) and pass with it. The first commit holds the apply-suite test alone, failing.

Run locally on Ruby 3.3.6 / Bundler 4.0.18, all passing (CI: all 88 checks pass on 640b52b, Bugbot found no issues):

  • cargo clippy --locked --workspace --all-features -- -D warnings (CI's command)
  • cargo test -p socket-patch-core
  • cargo test -p socket-patch-cli --lib --test apply --test e2e_vex --test e2e_vex_redirect --test e2e_redirect_gem_build --test e2e_vendor_gem_build --test e2e_gem_native_ext_build --test e2e_redirect_gem_stale_install --test in_process_gem_apply --test in_process_gem_multi_platform -- --include-ignored

cargo clippy -p <crate> --all-targets reports lints in test code on main too (CI's clippy does not lint test targets); none are in lines this PR adds.

Review

/code-review high ran twice. The first pass was on a version that filtered inside the crawler:

The second pass was on the verify-only filter:

  • A listed gemspec whose own dir is missing or not taken for a gem could leave the purl with no copy, and the hosted lock pin would then attest with nothing hashed. Fixed: the listed build must itself be one of the copies. Tested.
  • setup.rb was looked for in the scoped layout only, and matched only with a trailing slash. Fixed: flat homes are covered and an entry for a gem loaded from its root matches. Tested.
  • Blocking file calls on the async executor. Fixed: one run_blocking per gem with more than one copy.
  • Share the <home>/specifications/<leaf>.gemspec derivation with is_default_gem_copy and home_spec. Not done: those use is_file(), which is the stat-error behavior this rule must avoid, and unifying the callers is a refactor of its own. The gems/ parent guard the finding asked for is in.
  • apply still walks the leftover dir, so it can fail with no matching variant found where apply --check now passes; the hosted scan's stale-install guard looks at the first dir only. Both are how main behaves and neither is what Standalone vex refuses a hosted gem as not_applied because of a leftover <gem>-<version>-x86_64-linux directory that has no gemspec, after the scan's own rm -f … && bundle install remedy #2087 reports. Listed below.

Not in this PR

  • If the user runs a plain bundle install instead of the remedy, the old platform build keeps its gemspec. RubyGems can load it, so vex still refuses it (Gem agent apply patches only the ruby-platform x-1.0/ dir when a x-1.0-x86_64-linux-gnu/ sibling is also installed, so Bundler loads the unpatched platform gem while vex attests not_affected #1092), and the warning still does not name the directory or bundle clean --force. Naming the path in the vex omission is a message change worth its own PR.
  • The scan's remedy still leaves the directory on disk (bundle clean --force removes it; docs/ecosystems.md now says so).
  • Agent apply still treats a leftover dir as a copy: it patches it when the bytes match and fails with no matching variant found when they match no manifest variant. Changing what apply writes needs its own look at rollback (see the first review finding).
  • The hosted scan's stale-install guard resolves each gem to its first dir (find_each_by_purl), so in the reverse case (a leftover ruby dir beside an installed platform build) it judges the leftover.

https://claude.ai/code/session_011f9aeSRKgiJ4152hMeivxR


Generated by Claude Code

Refs #2087.

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_011f9aeSRKgiJ4152hMeivxR
Failing tests for #2087. A gem home lists the ruby-platform build
of a gem version and holds the dir of a precompiled build it has no
gemspec for (what the stale-install remedy leaves behind). Nothing
loads that dir, yet the crawler returns it as an installed copy and
vex refuses the patched gem over it (not_applied).

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_011f9aeSRKgiJ4152hMeivxR
The Ruby crawler counted every gems/<name>-<version>[-<platform>]
dir with a lib/ as an installed copy. RubyGems and Bundler load a
gem by its specifications entry alone, so the dir of a build whose
gemspec is gone is one nothing loads. The hosted scan's remedy for
a stale precompiled build (rm its gemspec and archive, then bundle
install) leaves exactly that beside the patched ruby-platform
build, and vex then refused the gem as not_applied.

A build's dir is now skipped when its gem home has no gemspec for
it but has one for another build of the same version. A version
the home lists no build of is not judged, so nothing changes where
the home does not say what is installed. The scan's discovery
follows the same rule, so it never names a dir the lookups skip.

Fixes #2087.

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_011f9aeSRKgiJ4152hMeivxR
The first fix skipped a leftover build dir in the crawler, which
every mode resolves through. Review found that too wide: rollback
and remove would stop restoring such a dir after agent mode had
patched it, a standalone bundle can still load it through
bundler/setup.rb, and is_file() took any stat error for an absent
gemspec.

The crawler is back to what main has. is_leftover_gem_build is now
applied where vex and apply --check collect copies, beside the
filters of #1098 and #599, so the write paths see every dir as
before. It calls a dir a leftover only on proof: the gemspec is
not found (no other stat error counts), another build of the
version is listed, and no standalone setup.rb names the dir.

Adds the hosted-mode vex test for the state #2087 reports, for a
bundle path inside the project and one outside it.

Fixes #2087.

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_011f9aeSRKgiJ4152hMeivxR
Second review pass on the verify-only filter:

- The listed build had to exist only as a gemspec. With its dir
  missing, a gem could lose every copy and the hosted lock pin
  would attest with nothing hashed. The filter now takes the
  copies of one gem version and drops a dir only when another of
  them in the same home has its gemspec.
- A standalone setup.rb was looked for two levels above the home
  only, and matched only with a slash after the dir. A flat home
  keeps it in <home>/bundler, and a gem loaded from its root has
  no slash.
- A dir whose parent is not gems/ is not judged.
- The probes run in one blocking task per gem.

Refs #2087.

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_011f9aeSRKgiJ4152hMeivxR
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 18, 2026 03: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 Ruby gem copy resolution for read-only verification (vex, apply --check) with nuanced filesystem and Bundler standalone rules; write paths are intentionally untouched but incorrect filtering could attest unpatched gems or miss real installs.

Overview
Fixes #2087: socket-patch vex and apply --check no longer treat a leftover gems/<leaf>/ directory as an installed copy when its gem home has no specifications/ entry for that build but does list another copy of the same version (typical after the stale-install remedy leaves an old platform tree beside a ruby platform install).

The core adds retain_loaded_gem_builds in the Ruby crawler, which drops only those “unlisted” dirs when a listed sibling exists in the same home. Apply, rollback, and remove are unchanged — they still resolve and write every physical dir; the filter runs only in find_manifest_package_copies_reusing (verification), alongside the existing gem multi-copy filters.

Edge cases stay conservative: dirs are kept when they are the last copy in a home, when gemspec presence cannot be proven (non–NotFound stat errors), or when bundler/setup.rb in a standalone bundle may still load them. Docs (CLI_CONTRACT.md, ecosystems.md) describe the rule; in-process and hosted vex e2e tests plus unit tests cover the behavior.

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

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. Fixes #2087. After a project switches a gem from a precompiled build to the ruby platform build and runs the scan's own cleanup command, the old gems/<name>-<ver>-<platform>/ dir is left on disk with no gemspec. vex and apply --check used to count it as an installed copy and fail with not_applied. They now skip such a dir, but only when another copy of that version in the same gem home has a gemspec. apply, rollback and remove work as before.

Risk: low. The change only affects the two read-only checks. It drops a copy only when there is proof: the gemspec stat must return "not found" (any other error keeps the dir), the gem keeps at least one listed copy in that home, and a standalone bundler/setup.rb that names the dir (or can't be read) keeps it. In the worst case vex attests a gem while a gemspec-less, unpatched dir sits next to the patched one. RubyGems and Bundler can't load that dir.

Look here

  • ruby_crawler.rs:1529: retain_loaded_gem_builds, the rule itself
  • ruby_crawler.rs:1555: home_lists_copy. Only NotFound counts as missing
  • ruby_crawler.rs:1580: standalone_setup_may_load, the guard for standalone bundles
  • ecosystem_dispatch.rs:1317: where the filter is applied. It runs after the verification-only bundle paths are merged, so out-of-tree bundles are covered too
  • [in_process_gem_multicopy.rs:359](https://github.com/SocketDev/socket-patch/blob/640b52bbf9f9c96e552417afe4c03d42d64492b2/crates/socket-patch-cli/tests/apply/in_process_gem_multicopy.rs#L359): end-to-end test of vex, check, apply and rollback

Verified

  • Read the full diff against Standalone vex refuses a hosted gem as not_applied because of a leftover <gem>-<version>-x86_64-linux directory that has no gemspec, after the scan's own rm -f … && bundle install remedy #2087 and the description; they match.
  • The filter is verify-only: find_manifest_package_copies_reusing is the path vex and apply --check use, and the write paths don't call it.
  • The empty-entry retain copies the existing one a few lines above it and can't remove a purl, because the rule never drops a gem's last listed copy.
  • The bundler/setup.rb match uses forward slashes, which is also how Bundler writes paths on Windows. A Gems/gems case mismatch only makes the filter keep the dir, so it fails safe.
  • No CHANGELOG.md change, no debug leftovers.
  • Ran locally on this head:
    • cargo test -p socket-patch-core --lib retain_loaded_gem_builds (4 tests)
    • --test apply in_process_gem_multicopy
    • --test e2e_vex_redirect gem_hosted_ref
    • Results: all 15 pass (4 + 9 + 2)
  • CI: ci-ok and clippy are green on 640b52b. Bugbot found no issues.

Changes I made. None.

Open questions. None. The follow-ups listed under "Not in this PR" (apply still walks the leftover dir, and the stale-install guard looks at the first dir only) behave the same on main and belong in their own PRs.


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 9df29b2 Nov 18, 2026
89 of 92 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-dir-without-gemspec-counted-installed branch November 18, 2026 15:41
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