Skip to content

Vendor a platform-locked gem once one platform has a patched build, with upstream's gem for the rest - #2342

Draft
Mikola Lysenko (mikolalysenko) wants to merge 14 commits into
feat/gem-complete-registryfrom
feat/gem-vendored-partial
Draft

Mikola Lysenko (mikolalysenko) wants to merge 14 commits into
feat/gem-complete-registryfrom
feat/gem-vendored-partial

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

Stacked on #2273. Offered for the maintainer to take or leave: #2273 does not depend on it.

What this does

A gem that Gemfile.lock names as precompiled platform gems is vendored as soon as one locked platform has a patched build. Vendored mode then mirrors what hosted mode already does for such a lock:

  • A locked platform with a patched build gets that build's tree, as in Patch gems locked as platform gems: patched builds where they exist, upstream's gem elsewhere #2273.
  • A locked platform with no patched build gets the tree of upstream's own gem, unchanged. The .gem is the one the gem patch's registry lists for the platform. It is downloaded from Socket's patch server (the registry's gems/<name>-<version>-<platform>.gem), never from the upstream registry: the download follows no redirect, and one the registry answers is the gem not being served. It is checked against the sha256 the list names. Its tree carries the gemspec the gem itself holds (metadata.gz), which Bundler reads the way it reads a Ruby gemspec.
  • One line per such gem, in hosted mode's words and from hosted mode's code:
    nokogiri 1.19.3: patched for arm64-darwin; upstream gem kept for x86_64-linux-gnu (no patched build yet)
  • Counting as in hosted mode: one applied event per patched build under its own patch, and the uncounted advisory event gem_upstream_build_kept (no uuid) for a tree of upstream's gem.
  • vex attests the patched builds from the ledger, offline, as before, and states a platform whose tree is upstream's gem affected, with what changes that.
  • A later scan follows the list. It asks for the references of all the gems vendored this way in one request and vendors a copy again when the list names another build for a locked platform: a patched build published since (now also patched for …), or upstream's gem where the patched build was withdrawn (upstream gem back for … (its patched build was withdrawn)). The Gemfile and the lock are not touched.
  • It never moves to a newer patch that has no patched build for a platform the vendored copy of an earlier patch holds one for. The earlier copy stays in force, as a hosted lock stays on the patch it is on.

Why

#2273 vendors such a gem only when every locked platform has a patched build. Bundler's default lock of nokogiri names eight platforms, so vendored mode would not patch it until all eight have builds, while hosted mode patches the lock as soon as one has.

The ledger

A tree of upstream's gem is a gemBuilds entry with its platform and gemIntegrity (the sha256-… of the gem the list named) and nothing else: no uuid, no gemspecIntegrity, no files. That is enough because:

  • vendor --check and vex already verify a tree that holds no patched file by the copy's file inventory, which covers every file of it. A changed, missing or added file fails the check.
  • repair fetches the same gem again from the gem patch's registry and requires the recorded sha256. It refuses (vendor_prebuilt_replaced) when the list names a patched build for the platform now, or no gem.

No new mechanism: one optional field in an existing entry, the registry URL the reference already carries, and the artifact download the client already has.

What a default lock commits

Bundler's default lock of nokogiri 1.19.3 names eight platforms. Measured with the real gems, the .socket/ directory plus the Gemfile and the lock in a fresh repository:

files bytes on disk git objects, loose git pack (gc --aggressive)
all eight patched 1,620 86,895,326 (du 88,708 KiB) 36,904 KiB 29,280 KiB
one patched, seven upstream 1,620 86,877,825 (du 88,648 KiB) 36,880 KiB 29,279 KiB

The two cases are the same size: a tree of upstream's gem is the same tree without the patch. One tree is 7.0 to 14.9 MB. The first scan of the mixed case downloads the seven upstream gems through the patch server (36.5 MB in 15 requests against a local run of the patch service).

What is unchanged

  • No locked platform has a patched build: the one-line skip, exit 0, nothing written.
  • A platform the registry does not list: the skip with its remedy.
  • A patched build the list names whose patch is not served yet: the skip. The registry then lists neither a build that can be vendored nor upstream's gem for that platform.
  • A rebuild: an intact copy is untouched, and a miss is worded by the service's answer.
  • Locks that name the gem for the ruby platform, and the refusal of a native gem locked as its source gem.
  • A gem whose every tree is a patched build prints what it printed (no new line).
  • vendor --revert and rollback restore the Gemfile and the lock byte for byte.

What changes for a copy that is already vendored

A run over an intact copy of a platform-locked gem made no request before. A run now makes one request for all such gems together, the references of their fixes, which is how it sees a published or withdrawn build. A service that does not answer is asked once, not once per gem.

An intact copy is never made worse, and a run never fails over one:

  • Offline, when the request fails, or when the list cannot be read, the copy stands as it is and nothing is said.
  • When the list names another build but the copy cannot be vendored again from it (a download that fails, a gem that fails its digest, a stub that is not served, an upstream gem that does not unpack), nothing of the copy is touched. The run exits 0, counts the copy as it counts one in step, and prints one note: Note: nokogiri@1.19.3 could not be brought in step with the builds the patch registry lists (<why>); its vendored copy stays in force as it is. (vendor_gem_list_not_followed in --json).
  • A ledger entry that names no build of its trees is not asked about.

When the list has moved, the whole copy is vendored again, every tree, not only the one that changed.

A copy that is damaged after the list moved is vendored again from the current list by the next vendor or scan run. The exact restore of it is refused, because the list names another build, and the run then goes on instead of failing. repair still refuses and names the scan.

What the maintainer must know

A copy with a tree of upstream's gem needs a CLI that reads the gemBuilds entry of that tree:

So a release that can produce such a copy has to contain a reader that knows it (#2273 plus this change), and a project vendored this way must not be attested (vex, vendor --check) with an older CLI.

How it was tested

  • Core unit tests: the mixed case, a gain and a withdrawal step by step with dry runs, upstream gems that are not the listed one, restore of an upstream tree and its refusals, the request-less preview.
  • CLI-level tests against a stand-in service that serves upstream's gem from the registry: vendor, scan and get, dry and wet, JSON and human output, the ledger, vendor --check, an offline vex, revert, a hosted-to-vendored takeover and an eject.
  • Real Bundler (4.0.15): the platform-gem capstone gains the mixed case. A fresh checkout installs frozen with no request to the patch service, Ruby loads the patched build of the running platform, bundle install and bundle lock regenerate the lock byte for byte, revert restores both files.
  • tests/nokogiri/nokogiri_e2e.rb with real nokogiri 1.18.10, eight locked platforms: vendored with every build patched, with the other platforms on upstream's gem, and with this machine's own platform on upstream's gem (Ruby loads upstream's nokogiri from the committed tree, then the patched one after the stand-in publishes the build). The compatibility workflow gains that last row.
  • Against a local run of Socket's backend with a scratch database, nokogiri 1.19.3: a two-platform lock and the default eight-platform lock with one patched build (frozen install with zero service requests, Ruby loads the patched build), repair of a removed upstream tree and of a removed patched tree, a second platform build published later, that build withdrawn, and the all-patched, rebuild-window and hosted runs as regression checks.

Numbers on this Mac (arm64, Bundler 4.0.15), at the head of the branch. The Rust suites ran on the last commit that changes Rust code; the two commits after it change the docs and the Ruby script.

  • cargo fmt --all -- --check and cargo clippy --locked --workspace --all-features -- -D warnings: clean.
  • cargo test -p socket-patch-core: 9,445 passed, 0 failed, 3 ignored.
  • cargo test -p socket-patch-cli: 6,001 passed, 2 failed, 538 ignored. The two failures are e2e_vendor_cargo_build cases that need an x86_64 toolchain this machine lacks, and fail on the base branch too.
  • The two real-Bundler suites (e2e_redirect_gem_build, e2e_vendor_gem_build, --ignored): 72 and 13 passed.
  • nokogiri_e2e.rb: vendored 26, vendored with the other platforms on upstream's gem 44, vendored with this machine's platform on upstream's gem 45, the same against a registry that redirects upstream's gem 29, hosted 58 checks, all pass.
  • Local backend runs: 147 checks in eight runs, all pass. They include an intact copy whose new build fails its digest (exit 0, every committed file unchanged) and a copy damaged after the list moved (vendored again from the list).

Review

/code-review high of this branch's own commits returned ten findings, all fixed in the last five commits:

  1. A copy damaged after the list moved failed on every re-run until a rollback. The run now vendors it again from the list.
  2. An intact copy whose list moved failed the run when the new build could not be vendored. It now stays in force, with one note and exit 0.
  3. The follow check made one request per vendored gem, and two for the fix when the list had moved. It is one request for all gems, read again by the rebuild.
  4. The human summary under-counted real skips beside a gem with a tree of upstream's gem.
  5. The ledger's doc comment was wrong about older readers. It is corrected, and the consequence is stated above.
  6. The download of upstream's gem followed redirects. It follows none.
  7. vex could state one product both ways when the view already attested the purl of a tree of upstream's gem.
  8. A scan's dry run dropped the gem's line and per-tree events for a copy in step.
  9. A lock that would have been skipped could fail on an upstream gem the list did not pin. The skips are judged first.
  10. Two advisory texts were wrong after a full withdrawal and for upstream trees, and the registry URL of an upstream gem was built in three places.

What was not run

  • CI on this branch (the PR is a draft), and so no Linux, Windows or older-Bundler run of the new cases. The workflow row for the nokogiri script has not run on a runner.
  • /code-review has not been run on this diff.
  • A production deployment of the patch service: the proofs use a local run of it and stand-ins.

Decisions a reviewer may want to change

  • The line is printed only for a gem with a tree of upstream's gem, or one a run brought in step with the list. Printing it for every platform-locked gem, as hosted mode does, is a one-line change.
  • A moved list vendors the whole copy again. Replacing only the changed tree would save the downloads of the others.
  • The line of a copy that could not follow its list prints as a note, not a warning: nothing in the project is wrong, and the next run tries again.
  • With a registry that answers upstream's gem with a redirect, vendored mode does not vendor a lock that needs that gem (hosted mode follows the redirect).
  • The sentence Vendored mode needs every locked platform is kept for an unlisted platform and for a named build that is not served.

🤖 Generated with Claude Code

…atched build

A gem `Gemfile.lock` names as precompiled platform gems was vendored only
when every locked platform had a patched build. Bundler's default lock
names eight platforms, so such a gem stayed unpatched in vendored mode
until all eight had one.

It is now vendored when the fix's list names a patched build for at least
one locked platform. Each of the others gets the tree of the gem the patch
registry lists for it: upstream's own gem, unchanged. That gem is
downloaded from the patch server, checked against the sha256 the list
names, and its tree carries the gemspec the gem itself holds (its
`metadata.gz`, which Bundler reads like a Ruby gemspec). Hosted mode
installs the same gem for such a platform.

- The ledger records such a tree as a `gemBuilds` entry with no `uuid`
  and the sha256 of its gem. It records no files: the copy's file
  inventory vouches for every byte of the tree, as it does for the files
  of a patched tree that the patch does not name, and the digest is what
  a restore fetches the gem again by.
- A copy that is intact asks for the fix's list (one request) and is
  vendored again when the list names another build for a locked platform:
  a patched build that was published since, or upstream's gem where the
  patched build was withdrawn.
- A newer fix that has no patched build for a platform the vendored copy
  of an earlier patch holds one for is a skip: the earlier copy stays in
  force.
- Unchanged: a lock none of whose platforms has a patched build, a
  platform the registry does not list, and a build the list names whose
  patch is not served are the skips they were.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…a build

A gem vendored as platform gems is reported per tree. A tree of
upstream's own gem (a locked platform with no patched build) now gets
what hosted mode gives a locked build the patch registry serves as
upstream's gem:

- the advisory `skipped` event `gem_upstream_build_kept`, with no `uuid`,
  which `summary` does not count; `withdrawn: true` and the matching
  reason where the run put the tree in place of a patched build, and
  `newlyPatched: true` on a patched build the run put in place of
  upstream's gem;
- one line per such gem under the summary line, in hosted mode's words
  (`nokogiri 1.19.3: patched for arm64-darwin; upstream gem kept for
  x86_64-linux-gnu (no patched build yet)`). A gem whose every tree is a
  patched build prints no line, as before;
- in `vex`, the `affected` statement of that platform gem with what
  changes it, read from the ledger with no request. The patched trees are
  attested as before.

`vendor --check` fails every tree of a copy that does not verify, the one
of upstream's gem included, so a copy that holds upstream's gems alone
still fails its check. Telemetry takes such a tree as about no patch.

A dry run of `scan --mode vendored` asks the fix's list for a gem already
vendored as platform gems, as the wet run does, and reports the trees the
wet run would put in place when the list names other builds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The stand-in patch service of the platform-gem tests now serves
upstream's own gem for a platform it lists with no patched build, from
the gem patch's registry and with the sha256 the list names.

- A lock with one patched platform and one that has none: vendored, the
  one line, the counted and the advisory event, the ledger, a later run
  in step (one request), `vendor --check` and a change to the upstream
  tree, an offline `vex` (the patched build attested, the other stated
  `affected`), and revert byte for byte.
- A later run that takes a published build and one that follows a
  withdrawal, each with its dry run first.
- Every dry run (`vendor`, `scan`, `get`) says what the wet run does.
- A takeover of a hosted gem with upstream's gem for a platform, by a
  scan and by an eject.
- The tests of a gem that is left alone or stays hosted because of one
  platform now use a build whose patch is still being built, and a lock
  none of whose platforms has a patched build keeps the one-line skip.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The platform-gem capstone of the real-Bundler suite gains the mixed case:
two locked platforms with a patched build (this machine's among them) and
one the registry serves as upstream's own gem. The stand-in registry now
serves that gem's file.

The gem is vendored: two counted `applied` events and the advisory one,
upstream's gem downloaded from the patch server, its tree with the YAML
gemspec the gem carries. A fresh checkout installs frozen with no request
to the patch service, Ruby loads the patched build of the running
platform, `bundle install` and `bundle lock` regenerate the lock byte for
byte, `vendor --check` passes and revert restores both files.

A platform the list leaves out and a build whose patch is still being
built stay the skips they were. An in-sync re-run now makes one request,
and a newer patch that lacks a build the vendored copy holds is worded
as hosted mode words it.

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

`CLI_CONTRACT.md`: the gem row of the vendored layouts (the tree of
upstream's gem, where it comes from, the gem's line, how a later run
follows the list, the ledger entry of such a tree, its events and its
`vex` statement, the dry runs), and the rows of
`vendor_prebuilt_platform_pending`, `vendor_prebuilt_replaced` and
`gem_upstream_build_kept`.

`docs/ecosystems.md`: the vendored row of the native-extensions table and
the support matrix cell.

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

The vendored scenario of `tests/nokogiri/nokogiri_e2e.rb` no longer waits
for every platform. With `--pass-through` naming some of the locked
platforms, the scan vendors nokogiri with the patched build of the others
and upstream's own gem, served by the stand-in's registry, for those. The
script checks the events, the trees and their gemspecs, where upstream's
gem was asked for, the line of a later scan, the `vex` statements, and a
frozen install of a fresh checkout with the service unreachable: Ruby
loads the patched nokogiri where this machine's platform has a patched
build, and upstream's nokogiri from the committed tree where it has none
(`--pass-through own`). The next scan then takes the builds the stand-in
publishes. With no patched build at all nothing is vendored, as before.

The compatibility workflow runs the vendored scenario with the runner's
own platform passed through as well.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…he list

The line that says which platform builds of a gem are patched is printed
by hosted mode and now by vendored mode. It moves, with the filter
vendored mode applies to it, into the helper module
`commands/gem_build_lines.rs`, so that `vendor` does not import `scan`
(the command modules' layering test).

The core test of the request-less preview covers a lock already wired to
the patch's copy: one request (the fix's list) while the copy holds what
the list names, the trees the wet run would put in place when the list
names another build, and no promise when a patched build it names is not
served.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review findings on the gem backend, with the rule that an intact
committed copy is never made worse and a run never fails because of
something it could have left alone.

- An intact copy whose list moved went through the rebuild of a missing
  or stale copy, where any failure but the pending skip ended the run.
  When the list cannot be followed (a request that fails, a build whose
  gem fails its digest or whose stub is not served, an upstream gem that
  does not unpack) the copy now stays in force: the run ends as it does
  for a copy in step, with one line (`vendor_gem_list_not_followed`). A
  build the list names that is not served yet keeps its skip. A dry run
  says the same. The advisory of a copy that did follow its list has a
  code of its own (`vendor_gem_list_followed`).
- A ledger entry that names no build of its trees says nothing of a
  moved list: no request is made for it and the copy stands.
- The package references of several patches can be asked for in one
  request and remembered by the client: a later read of one of them makes
  no request, and a request that brought no answer is not made again per
  patch. A run asks for the lists of the gems it finds vendored this way,
  and the fix's reference is read once whether or not the list moved.
- Upstream's own gem is downloaded with a client that follows no
  redirect: a redirect the registry answers is the gem not being served.
- The skips of `locked_build_sources` (a platform the list leaves out, no
  patched build at all, a newer fix with fewer builds) are judged before
  an upstream gem the list does not pin fails the gem.
- The download advisory names a copy of upstream's gems alone as such and
  says where it came from, the `.gitignore` advisory names the gems as
  they are, and the registry URL of an upstream gem is built in one place.
- The ledger's doc comment says what each older reader does with a tree
  of upstream's gem.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…t real skips

Review findings on the CLI side of a gem vendored as platform gems.

- The vendor loop restores a damaged copy before it reaches the backend
  and stopped at a restore error. A restore that is refused only because
  the list of the gem's fix names other builds now
  (`vendor_prebuilt_replaced`) goes on to the backend, which vendors the
  copy again from that list, as it does for an intact copy. `repair`
  still refuses and names the scan. A ledger edited to name another
  patch for a tree looks the same and is settled the same way: the list
  chooses what is unpacked.
- The lists of the gems already vendored as platform gems are asked for
  in one request before the loop, and by the dry-run preview of a scan.
- `VendorTally` no longer subtracts the uncounted advisory event of a
  tree of upstream's gem, so a package skipped beside such a gem is said.
- `vex`: a tree of upstream's gem under a purl the view already attests
  leaves that purl alone, as a hosted build does.
- The dry-run preview of a scan reports an in-step copy per tree, with
  the gem's line, as the real run does.
- The line of a copy that could not follow its list prints as a note.

Tests: a damaged copy after the list moved (either tree), an intact copy
whose rebuild fails with every file compared byte for byte (dry and wet),
the dry run of an in-step copy, the tally, the vex view, and the edited
ledger settled by the list.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`CLI_CONTRACT.md`: the one request for the lists of all the gems vendored
as platform gems, the download of upstream's gem that follows no
redirect, what a run does when the list cannot be read or cannot be
followed (`vendor_gem_list_not_followed`, a note, exit 0, the copy
untouched), the advisory of a copy that followed its list
(`vendor_gem_list_followed`), and that a vendor run whose exact restore
is refused because the list moved vendors the copy again from the list
while `repair` still refuses.

`docs/ecosystems.md`: the same in the vendored row of the
native-extensions table.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
With `--pass-through-redirect` the stand-in registry answers the file of
upstream's gem with a redirect to its upstream stand-in. Vendored mode
takes that gem from the patch service alone, so the vendored scenario now
checks that the scan does not vendor the gem, asks nothing of the
upstream stand-in and writes nothing, and then goes on once the stand-in
publishes the builds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… the client memo

Review findings on the gem backend and the API client.

- A run that could not vendor an intact copy again said the copy "stays
  in force as it is" without looking at it. Two failures happen after the
  copy was changed: the new trees are in place and the uuid dir's git
  metadata cannot be written, or the swap of the stage fails and cannot
  be undone. One function, `copy_in_force`, now answers whether the
  recorded copy stands with its metadata in place. The in-step path and
  every outcome that says the copy stays in force go through it
  (`wired_copy_failed`); a copy that no longer stands is the failure it
  is for any damaged copy. A copy left in force gets its `.gitignore`
  and `.gitattributes` put back, as a copy in step does.
- The client-wide memo of batched references is gone, and with it the
  remembered "no answer" that failed later reads of the same patch. The
  batch goes through the vendor prefetch plan: a planned download can be
  a position whose reference alone is read
  (`PlannedDownload::reference`), which shares the plan's one reference
  request and is never fetched ahead.
- The rebuild of a copy whose list moved takes the gems that list was
  read to name (`BuildsFrom::Listed`), so the fix's reference is asked
  for once with or without a plan.
- A redirect is "not found" only for the client that follows none. The
  client that follows redirects answers one it cannot follow as it did.
- `restore` returns a structured reason (`RestoreError::list_moved`),
  true only when the list of the gem's fix names another build than the
  entry records; a reference that does not pin upstream's gem is a miss.
  `restore_follows_list` asks the same without downloading, for a dry
  run.
- `follows_list` is the one rule for which copies follow a list.
- Failure points for the stage swap, its undo and the uuid dir's
  metadata, for the tests of the two failures above (debug builds only).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…restore by its reason

Review findings on the CLI side of a gem vendored as platform gems.

- The collect, sort, dedup and remember block is gone from the vendor
  loop and from the dry-run preview of a scan. One helper,
  `list_follower`, says which packages follow the list of their fix; the
  vendor loop plans each as a position whose reference alone is read (a
  dry run plans nothing else), and the preview attaches the same plan for
  the rows in step. The plan's one reference request serves them, under
  its own guard.
- The fall-through after a refused restore is decided by the restore's
  structured reason (`list_moved`), not by the prefix of a formatted
  error, so a reference that does not pin upstream's gem is the failure
  it is.
- The dry run of a damaged copy whose list moved no longer reports
  `wouldRedownload` and stops: it asks whether the restore would be
  refused for a moved list and, as the wet run does, goes on to preview
  the copy vendored from the list.
- `vex`: every tree of a vendored copy whose purl the view already
  attests is left to that record, as the hosted loop does, the patched
  ones included.

Tests: each of the two failures that change the copy (the uuid dir's
metadata after the trees are in place; a stage swap that cannot be
undone) forced with a failure point, with what is printed and the exit
code; the dry run of a damaged copy; the metadata put back by a run that
leaves the copy in force; the vex view for a patched tree.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…n of a damaged copy

`CLI_CONTRACT.md`: the references of the gems vendored as platform gems
are positions of the run's download plan and share its one request; what
an unreachable service costs; a run says a copy stays in force only after
checking that it stands, with its git metadata put back; a rebuild that
fails after it changed the copy is a failure; the fall-through after a
refused restore is for a moved list alone, and its dry run previews the
copy vendored from the list.

`docs/ecosystems.md`: the same, briefly.

The planner shim the tests call is compiled for tests only.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant