Skip to content

Hosted mode: patch gems locked as precompiled platform gems (nokogiri), on MRI and JRuby - #2150

Merged
Mikola Lysenko (mikolalysenko) merged 10 commits into
mainfrom
feat/gem-hosted-platform-prebuilds
Nov 19, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 10 commits into
mainfrom
feat/gem-hosted-platform-prebuilds

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

Summary

Hosted mode now patches a gem that Gemfile.lock pins as precompiled
platform gems. nokogiri is the gem this is for: Bundler installs it as
nokogiri-1.18.10-x86_64-linux-gnu.gem and friends, the lock has one entry
per platform and often no plain nokogiri (1.18.10), and until now a hosted
scan skipped such a lock with redirect_gem_platform_unsupported, leaving
the gem unpatched. On JRuby the platform is java, and the same path
covers it.

It needs Socket's backend to serve a patched build of each platform gem (a
"prebuild"). Where it serves none, the behaviour is what it was: the lock is
left alone and the skip is reported.

How it works

  • What the service says. A patch's package reference can list
    registryOverride.identifiers.gemPlatforms: [{platform, gemChecksumSha256}], one per platform gem the patch registry serves. The
    field is additive and read leniently: null, a non-list or an entry
    without a platform or checksum reads as "no prebuild for that platform"
    and never fails the batch it arrives in.
  • The move. When every platform entry of the gem in the lock has a
    prebuild, the scan moves all of them (blocks and dependency lines
    verbatim) into the patch registry's GEM section, pins the dependency
    with !, and writes each platform's sha256 into CHECKSUMS where the
    lock has that section. A plain name (version) entry beside them moves
    too. The Gemfile edit is the one hosted mode already makes. One platform
    without a prebuild and nothing moves.
  • Restore. rollback, remove and a withdrawn patch put every platform
    entry back on its upstream entry and its upstream sha256 (the lock's own
    record, else the registry's compact index, one request per gem), in every
    wiring state.
  • Installed copies. The stale-install guard and the unwind refresh judge
    and refresh the directory of every build the lock names, each against its
    own platform's sha256. A gem home that holds both the ruby-platform and a
    platform build no longer has the first one found judged in place of the
    one Bundler loads.
  • VEX discovers one hosted reference per gem version, content-pinned
    only when every build has a sha256.
  • Discovery. A platform-only gem with no installed copy is read from the
    lock, so a hosted scan of a fresh checkout sees it.

CLI_CONTRACT.md and docs/ecosystems.md describe it.

What a reviewer should weigh

  • Bundler's default lock keeps the ruby-platform entry beside the platform
    ones
    for a gem that also ships a source gem (Bundler 4.0.15 adds ruby
    to PLATFORMS). Both shapes are handled and tested: platform entries
    alone, and with the ruby entry beside them.
  • A platform named only under CHECKSUMS counts as locked and needs a
    prebuild.
  • A lock from Bundler before 2.2 (merged layout) keeps the skip.
  • A mixed lock (the gem's builds spread over sections, or a lock that
    cannot be converged) gets no CHECKSUMS edit at all.
  • Refreshing an installed copy needs the prebuild's gemspec metadata to
    match the upstream platform gem's byte for byte, the rule the ruby gem
    already has. If the backend regenerates it, the refresh is refused and the
    stale warning stays; fresh installs are unaffected.
  • Released CLIs ignore the new fields: they pick artifacts by exact kind
    and drop unknown fields, and keep skipping a platform lock.

Code review

/code-review high raised ten findings, all fixed: the stale-install guard
judging only the first installed directory per gem home; a null or
incomplete gemPlatforms failing the whole batch response; the unwind
judging platform copies the restored lock does not name; prebuild checksums
written into a lock that then stays mixed, which rollback did not undo; a
platform-only hosted pin not recorded as a manifest pin; and five
efficiency or duplication points (a crawl per restored gem, a registry
client per call, a second lock read from disk, three readers of "which
builds does the lock name", a lock parse per dependency).

Two partial points remain: the vendored unwind's cache advisory still builds
its own registry client per gem, and the unwind still crawls once per
judging step, as on main.

Testing

  • Real nokogiri 1.18.10, the new hosted scenario of
    tests/nokogiri/nokogiri_e2e.rb
    , against a stand-in for the patch
    service that repacks the real platform gems:
    • Ruby 3.4.10, Bundler 4.0.15, macOS arm64: 25 checks pass. The scan moves
      all eight platform entries to the patch registry with their checksums; a
      fresh checkout installs with the lock frozen and compiles nothing; Ruby
      loads the patched gem there and in the scanned project (its installed
      copy is refreshed); vex attests; rollback leaves the lock and the
      Gemfile byte for byte as they were.
    • JRuby 9.4.15.0, Bundler 2.6.3, in a Linux container: 17 checks pass, the
      same sequence on the java platform gem (a lock without CHECKSUMS).
    • Four CI legs run it: Ruby 3.4 and 3.1 on Linux, Ruby 3.4 on macOS,
      JRuby 9.4.
  • Real Bundler capstones in e2e_redirect_gem_build (52 pass, three new):
    a CHECKSUMS lock with platform entries alone, the same without
    CHECKSUMS, and a lock that keeps the ruby-platform entry beside them.
    Each compares the scan's lock byte for byte with what bundle lock
    writes, then checks the frozen install, a no-op re-scan, rollback and
    standalone vex.
  • Before the last rebase onto main: core lib 7,972, CLI lib 955,
    redirect_golden and upstream_restore_golden 1 + 64,
    e2e_redirect_gem_stale_install 55, in_process_rollback_hosted 41,
    hosted_memory_parity 44. After it: cargo fmt --check and CI's clippy
    command pass, and the two real-nokogiri runs above; the suites run again in
    CI.
  • Not run: Windows; Bundler older than 2.6 other than through the capstones'
    gates; a real patch service (the backend change that serves prebuilds is
    in review).

The nokogiri script also carries a vendored scenario, which this branch's
CLI does not pass; its legs come with the vendored change.

🤖 Generated with Claude Code


Generated by Claude Code

Bundler installs a native gem such as nokogiri as a precompiled platform
gem, and the lock then has one entry per platform
(`nokogiri (1.18.10-arm64-darwin)`) and usually no ruby-platform
`nokogiri (1.18.10)`. Hosted mode skipped such a lock with
`redirect_gem_platform_unsupported`, because the patch registry served
the ruby gem alone. The registry can now serve a patched platform gem (a
"prebuild") per platform, and the patch API lists them.

API: a package artifact may carry `platform`, and the registry override's
identifiers may carry `gemPlatforms` (`[{platform, gemChecksumSha256}]`).
Both default to none. A grant's platform names and digests are held to
the shape the lock takes before a grant becomes a candidate.

Rewrite: when every platform entry of the gem at the patched version has
a prebuild, every entry of that version (spec line and sub-lines,
verbatim, plus a ruby-platform entry beside them) moves to the patch
registry's `GEM` section, `DEPENDENCIES` is pinned as before, and each
`CHECKSUMS` entry takes the sha256 of its own build. A lock with no
`CHECKSUMS` section gets none. A locked platform with no prebuild keeps
the skip, and the warning now names the missing builds and what the
registry serves. A second scan of a moved lock is a no-op: the agreement
check and the converge step look for the builds of the version, not for
exactly one `name (version)` spec.

Restore: every platform entry goes back to the upstream section where
Bundler sorts it (by full name), and each `CHECKSUMS` entry is re-pinned
to its own build's upstream sha256, the compact index's
`<version>-<platform>` line. One `info/<name>` document is read per gem.

Discovery: the platform specs of one gem version under a Socket `GEM`
section of its own are one hosted pin, content-pinned only when
`CHECKSUMS` holds a sha256 for every build. The engine drops a rewrite
discovery cannot attribute, so this lands with the rewrite.

Stale installs: an installed platform copy
(`gems/nokogiri-1.18.10-arm64-darwin`) and its archive in Bundler's cache
dir are judged against the patched artifacts, prebuilds included, and
refreshed from `<index>/gems/<leaf>.gem` against that platform's own
sha256. A platform gem declares no extensions, so nothing is built. After
a rollback the same copy and archive go back to the upstream platform
gem; a patched archive left in the cache dir would fail every install on
the restored checksum.

Lock-only discovery: the lock inventory lists a gem by its ruby-platform
entry, so a gem locked only as platform builds was found only through an
installed copy. A hosted run (the disk scan, `get --mode hosted` and the
in-memory engine) now also reads those gems from the lock. Agent and
vendored runs are unchanged.

Nothing changes for a lock that names a gem by its ruby-platform entry
alone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Three real-Bundler capstones in e2e_redirect_gem_build (`#[ignore]`, like
their neighbours). The fixture gem is published as a ruby gem and as
platform gems for this machine and for a foreign platform; a platform gem
needs no binary, a pure-Ruby gem with `s.platform` set is one. The real
Bundler locks the platform builds, the hosted scan moves them, and the
lock is compared byte for byte with what `bundle lock` writes for the
redirected Gemfile. A fresh checkout's frozen `bundle install` fetches
exactly this machine's prebuild, leaves the lock untouched, and Ruby
loads the patched file from the platform build. A re-scan changes
nothing, `rollback` restores the pair byte for byte and the installed
copy, and standalone `vex` attests the installed platform build.

The arms: a `CHECKSUMS` lock holding the platform builds alone, the same
without `CHECKSUMS`, and a lock that keeps the ruby-platform entry beside
them (what Bundler 4 writes by default for a gem that also ships a ruby
gem).

Two hermetic tests need no Ruby: a stale platform install and its
committed cache archive are replaced with the prebuild and put back by
`rollback`; and a fresh checkout is pinned from the lock alone, or left
untouched when a locked platform has no prebuild. The in-memory and disk
engines are compared on the new golden fixture.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The CLI contract and the ecosystems guide said a gem locked as
precompiled platform builds is always skipped in hosted mode. They now
say when it is redirected (a prebuild for every locked platform), what
the lock looks like afterwards, how an installed platform copy is judged
and refreshed in both directions, how the lock is restored, and that the
skip is left for a locked platform with no prebuild.
`redirect_gem_platform_unsupported` gets its row in the warning table.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The rollout's recorded view reads a hosted gem pin from the manifest's
`source:` option and takes the version from the lock. It read the version
from ruby-platform specs only, so a gem the lock names as precompiled
platform builds alone was never a recorded manifest pin, and a capped
re-scan spent a new-patch slot on it every time. Any build of the gem now
names the version; builds of two versions still name none.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The hosted lock supplement read the Bundler lock from disk a second time,
beside the snapshot the lock inventory is taken from, and cloned the whole
inventory to append the platform-only gems to it. It now reads the lock
through the project context's snapshot and walks the two lists of purls
without copying either.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review fixes for hosted platform prebuilds.

Installed copies are judged by leaf. The stale-install guard and the
unwind's refresh took the first installed dir of a gem version in each
gem home, and `<name>-<version>` sorts before the platform dirs. In a
home holding both builds, the ruby-platform dir stood in for the platform
build the lock names: the guard called the gem patched (or stale) by the
wrong copy and left the build Bundler loads unpatched and unreported, and
a rollback did not put it back. Both now look for the dir of every build
the project's lock names, under its own leaf, and judge each one found.
A build the lock does not name (another project's copy in a shared home)
is never judged, reported or replaced, by the archive check, the record
check or the pinned-patch check. Where no lock names the gem (a global
scan) the first copy is judged, as before; an unwind whose lock cannot be
read keeps to the ruby-platform gem.

One reader says which builds a lock names. The restore's own platform
list disagreed with `GemfileLock::locked_builds` about a platform named
only under `CHECKSUMS`; it is gone, and the rewrite, the restore and the
refresh all ask `locked_builds`. A build's lock token and file name come
from `GemId` everywhere, and the cache-dir listing that re-implemented
the crawler's platform-dir rule is replaced by a look at the archive of
each named build.

A prebuild sha256 is written only on entries that moved. The rewriter
pinned `CHECKSUMS` before it knew the lock converges, so a lock it keeps
MIXED (a 5.0 / 5.1 `source … do` block over a lock that cannot be
converged) got prebuild sha256s on platform entries that still resolve
from rubygems.org, and every `bundle install` failed on the checksum. The
`CHECKSUMS` edits are now decided after the source attribution, and a
gem locked as platform builds gets none while the lock stays mixed. The
restore re-pins a platform entry to its upstream sha256 in every wiring
state, not only when the gem has a section of its own.

`gemPlatforms` cannot fail a reference response. One response answers for
every package of a batch, and the list was read strictly: `null`, or an
entry with a `null` checksum, failed the whole response and cost every
package its grant. `null` (or anything that is no list) now reads as no
prebuild, and an entry without a platform or a checksum string is
dropped, which leaves that platform without a prebuild. An artifact's
`platform` that is no string reads as absent. An entry that is two
strings in the wrong shape still fails its own grant, and only that one.

The unwind's refresh reads the restored lock once and uses one registry
client (`GemUpstream`), so a gem's compact-index document is fetched once
for all of its builds and is no longer cloned on a cache hit; it crawls
the gem homes once for the platform builds of all restored gems instead
of once per gem. `rewrite_gem` parses the lock once per call instead of
once per dependency.

Tests: a shared gem home holding both builds through scan, a refresh on
request and rollback; the mixed lock through rewrite and unwind, byte for
byte; a response whose prebuild lists are `null`, not lists, or full of
holes; a wrong prebuild failing only its own grant.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The stale-install guard and the unwind's refresh judge the installed
copy of every build the lock names, each under its own leaf, and no copy
of a build it does not name. The contract described the first installed
copy of the version.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The nokogiri script gains a hosted scenario. It scans a real install
whose lock pins nokogiri as platform gems against a stand-in for the
patch service, which serves a patched build of each real platform gem
(repacked the way the service repacks one: the gemspec member byte for
byte, the files and checksums rewritten). It then installs from a fresh
checkout with the lock frozen, checks that the scanned project's own
copy was refreshed, runs vex, and rolls back to a byte-identical lock.

Four legs run it: Ruby 3.4 and 3.1 on Linux, Ruby 3.4 on macOS, and
JRuby 9.4, where the platform is java.

The script also carries the vendored scenario; its legs come with the
vendored change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes hosted gem lock rewrites, on-disk refresh, and rollback for multi-platform locks (high install impact), but behavior is fail-closed when prebuilds are missing and is covered by expanded nokogiri/Bundler CI scenarios.

Overview
Hosted gem patching now covers locks that pin only precompiled platform builds (e.g. nokogiri (1.18.10-x86_64-linux-gnu)). When the patch grant lists a prebuild per platform (gemPlatforms), hosted mode rewrites Gemfile/Gemfile.lock like other gems and moves every platform spec to the patch registry; missing prebuilds still skip with redirect_gem_platform_unsupported.

Discovery and staleness follow the lock per build, not a single ruby-platform .gem: hosted scan/get add platform-only gems to the lockfile supplement; the stale-install guard and upstream unwind refresh locate installs by leaf (find_builds), judge/refresh each named platform archive (including prebuild checksums), and avoid treating an unrelated ruby-platform dir as the loaded build.

CI and contract add nokogiri hosted matrix legs and path filters for gem redirect/refresh code; CLI_CONTRACT.md documents platform wiring, rollback, and VEX pinning. A hosted-memory parity fixture covers platform-prebuild redirects.

Reviewed by Cursor Bugbot for commit e311a74. 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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit e311a74. Configure here.

The hosted gem rewrite refused a gem with redirect_gem_missing_sha256
before it looked at the lock's platform builds. A lock that names only
platform builds pins no ruby-platform entry, so a grant whose prebuilds
cover every locked platform needs no ruby gem checksum: the builds now
move with their prebuilds' sha256. A lock with the ruby entry beside
them still needs it and still warns.

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

Copy link
Copy Markdown
Collaborator Author

One more run, beyond what the description lists, on a local build of this branch merged with the other two open gem changes (#2150, #2154, #2155).

The stand-in patch service in tests/nokogiri repacks the real platform gems with a Ruby file changed. For this run it instead served patched platform gems that Socket's backend cross-build produced for nokogiri 1.18.10 (arm64-darwin, x86_64-linux-gnu, aarch64-linux-gnu, x86_64-linux-musl), each with a C source change and a new libxml2 patch compiled in, and the repack for the other four platforms in the lock.

Ruby 3.4.10, Bundler 4.0.15, macOS arm64: 45 checks pass across the hosted and vendored scenarios.

  • A fresh checkout installs with the lock frozen and loads the binary the service built: the C change and the libxml2 patch are both in what Ruby loads.
  • The scanned project's already-installed copy is refreshed to that same binary. This is the path that depends on a prebuild carrying the upstream gemspec byte for byte.
  • vex attests, and rollback puts the lock, the Gemfile and the upstream binary back.

The option that serves a prebuilt gem (--prebuilt-gem) is not in this PR; I will send it as a follow-up to the test script once these land.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
Hosted mode can now patch gems that Gemfile.lock pins as precompiled platform gems (nokogiri on MRI, java on JRuby), but only when Socket's backend serves a patched build for every platform the lock names. In that case it moves every platform entry to the patch registry's GEM section and pins each build's own sha256 in CHECKSUMS. Rollback, remove, the stale-install refresh and VEX all handle each build separately. With no prebuilds nothing changes: the gem is still skipped with redirect_gem_platform_unsupported.

Risk: medium. It edits users' lockfiles across a large surface (~5k lines, 47 files). Several things limit the risk:

  • It does nothing unless the backend sends the new gemPlatforms field.
  • It fails closed: if one platform is missing, nothing moves.
  • The bundler tests compare the result byte for byte with what real bundle lock writes.
  • Four new real-nokogiri hosted legs are green: Ruby 3.4 on Linux and macOS, Ruby 3.1 and JRuby 9.4.

The Windows and macOS e2e jobs were skipped on this PR.

Look here

  • redirect/mod.rs#L8716: the Bugbot fix; a ruby sha256 is needed only when the lock has a ruby entry
  • redirect/mod.rs#L8778: every locked platform must have a prebuild, otherwise skip
  • redirect/mod.rs#L8523: no prebuild checksums on a lock that stays mixed
  • redirect/grant.rs#L274: validation of prebuild platforms and sha256s
  • [redirect/upstream/gem.rs#L470](https://github.com/SocketDev/socket-patch/blob/50d6b28729873d788e338bbf27a8cbd8653d6a18/crates/socket-patch-core/src/patch/redirect/upstream/gem.rs#L470): rollback re-pins each build's upstream checksum

Verified

  • cargo test -p socket-patch-core --lib filtered to gem, redirect, platform and prebuild: all pass. upstream_restore_golden passes too.
  • The Bugbot regression test gem_platform_only_lock_needs_no_ruby_checksum fails with 50d6b28's fix reverted and passes with it.
  • Checked with no defect found:
    • All-or-nothing platform coverage, including a platform named only under CHECKSUMS.
    • Exact matching of CHECKSUMS tokens (1.18.1 does not match 1.18.10-…).
    • Bundler <2.2 merged locks keep the skip.
    • CRLF locks.
    • Platform directory names read from disk are checked as one safe path segment.
    • Grant validation rejects ruby, injected lock text, duplicates and non-hex digests.
  • All 85 check runs on 50d6b28 succeeded or were skipped, including ci-ok, clippy and hosted-e2e. CHANGELOG.md is untouched and every review thread is resolved.

Changes I made
None in this review. The previous run pushed 50d6b28 (the fix for the Bugbot ruby-checksum finding).

Open questions (neither blocks merging)

  1. Take a lock that names only platform builds, with no ruby gem. After the redirect, an unfrozen bundle install (or bundle lock --add-platform) on a platform the lock doesn't list will fail to resolve unless the patch registry also serves the ruby-platform gem or a prebuild for that platform. Before this PR, such a project kept resolving from rubygems.org. Does the registry always serve the ruby gem? If not, should the scan warn?
  2. Minor: the grant check accepts uppercase hex and writes the prebuild sha256 into CHECKSUMS as sent, but Bundler compares digests case-sensitively. Lowercase it, or reject uppercase? The existing ruby sha256 path behaves the same way.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[agent] Reviewed at 50d6b28. The platform move is all-or-nothing (one locked platform without a prebuild, or a Bundler < 2.2 merged lock, skips the gem with redirect_gem_platform_unsupported), a lock that stays mixed gets no prebuild CHECKSUMS edits, grant validation rejects ruby, injected lock text, non-hex digests and duplicate platforms, and rollback re-pins each build from its own compact-index line. The Bugbot finding on the ruby checksum requirement is fixed by the platform_only gate. Non-blocking: (1) in scan/hosted.rs wanted, when no prebuild matches the stale copy's platform the accepted sha256 set is every artifact of the gem, which is looser than that build's own sha256; (2) on a platform-only lock, an unfrozen bundle install or --add-platform for an unlisted platform fails after the redirect unless the registry also serves the ruby gem, and no warning says so; (3) uppercase hex is written into CHECKSUMS as sent.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 19, 2026
…orm-prebuilds

# Conflicts:
#	crates/socket-patch-cli/CLI_CONTRACT.md
#	crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs
Merged via the queue into main with commit f297a90 Nov 19, 2026
85 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the feat/gem-hosted-platform-prebuilds branch November 19, 2026 15:22
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 19, 2026
#2114, which this branch was stacked on, landed on main as a squash
(6014caf). Merged against its tip (7bdc058) as the base, so the
reactor/property work is taken once and #2150 (f297a90) comes in
unchanged. Two conflicts kept both sides: the MAVEN_ARGS and REPIN
entries in the Bazel harness, and the mirror and declared_maven
modules in formats/maven. CHANGELOG.md is main's.

Assisted-by: Claude Code:claude-opus-5-5
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

Development

Successfully merging this pull request may close these issues.

3 participants