Skip to content

Apply a platform gem's own patch without rebuilding over its binaries, and repair copies 5.2.0 left stuck - #2499

Open
Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
mainfrom
fix/agent-gem-own-platform-binary
Open

Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
mainfrom
fix/agent-gem-own-platform-binary

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

A RubyGems platform gem (nokogiri-1.18.9-x86_64-linux-gnu) has its own patch, and that patch carries the gem's compiled libraries, one per Ruby ABI, next to the changed C sources. In agent mode 5.2.0 took the C sources for build inputs. It wrote the record's files, then built the source gem and put that build's library over the record's own for the Ruby that ran apply.

The file it replaced holds neither the original nor the patched content of the record, so every later command failed on the copy:

  • apply --check exits 1 ("matches neither the original nor the patched bytes")
  • apply fails on the hash mismatch
  • vex cannot attest the gem
  • rollback refuses the copy

The build also cost a compiler, the headers and several minutes, for a patch that already brought the binary.

The rule

A record brings its own build when its key names the copy's platform, one of its files is a compiled library, and it has that library for every Ruby the copy ships one for. Such a record is applied to a precompiled copy as it is: files written, nothing built, nothing grafted. The decision is made in InstalledGem::scope, the one place that sees the record's key together with the copy.

Everything else is unchanged: a ruby record on a precompiled copy still builds and grafts, and so does a platform record with sources only.

A platform record that leaves out the binary of a Ruby the gem ships one for is also handled as in 5.2.0, failure included: apply builds for the Ruby that runs it, and when that build lands on a binary the record brings, the copy ends in the state described above and is not repaired. The production record for nokogiri holds all four binaries the gem ships, so it is not such a record. The alternative, applying a partial record with no build, would report a copy patched while one of its Rubies loads unpatched code.

Copies 5.2.0 left stuck

apply and rollback put the binary that the build replaced back from the graft's backup, then go on. They do it only when that makes the copy one they can finish:

  • apply: with the replaced binaries left out, the rest of the record passes a dry run under the run's own mismatch policy, and every blob the record writes is stored and holds the content the record names.
  • rollback: every other file of the record holds its original or its patched content.
  • Neither acts when the backup holds content the record does not know or a file that is not the record's, or when it lists files the earlier patch added.

Any other copy is left as it is, with the build that runs the patched source still in place, and fails as it did before.

A dry run puts nothing back. apply --dry-run counts the copy as one that can be patched. A put-back that fails partway is reported as native_extension_restore_failed and names gem pristine. Once the record is written whole, the graft's marker and backup are dropped.

Tests

Unit tests in patch/gem_ext.rs cover the rule, a clean apply, the recovery by apply and by rollback, the cases where the build is kept (edited source, missing file, missing blob, damaged blob, unknown backup content, a backup with a file the record does not write, a backup that lists added files), the dry run, a patch published again since the build, and the leftovers.

Run locally: the core library tests, the CLI library tests, and the gem, native build, rollback and vex integration targets.

Checked against production

With a Linux build of the third commit (8d32d98c0; the fourth adds a test and contract text only), on nokogiri 1.18.9 for x86_64-linux-gnu. The first two rows ran in ruby:3.3 and ruby:3.2 containers, the rest in ruby:3.3:

Step Result
get <patch> --mode agent exit 0, no build, all five files patched
apply --check, apply, vex, rollback after it exit 0 each
Copy patched by 5.2.0 first: apply --check exit 1 (5.2.0 and this branch agree)
That copy: apply --dry-run exit 0, "1 package can be patched", nothing changed on disk
That copy: apply exit 0, all five files patched, marker and backup gone
Then apply --check, vex, rollback exit 0 each, all files original after rollback
That copy: rollback --dry-run, then rollback exit 0 each, all files original

Not run: Ruby 3.1 and 3.4 containers.

Review

/code-review high ran twice: on the first commit, and again on the branch after the second commit. The second, third and fourth commits answer the findings.

First review:

Finding Handling
A missing file made the recovery undo a build the record could not replace The recovery runs only when the rest of the record passes a dry run
A copy holding an earlier publication of the patch was not repaired The dry run uses the run's own mismatch policy, which overwrites content a Socket patch wrote
--dry-run reported something other than what the run does A dry run puts nothing back and reports the replaced binary as a file to patch
A backup that lists added files lost that list Such a backup is left alone
rollback swallowed a failed put-back It fails the package with native_extension_restore_failed
The marker and backup stayed after the record was applied Dropped when every file they name is one the record wrote
A failed put-back was reported under an unrelated code Own code, and the message names gem pristine
A build an earlier run left unfinished was not swept recover_interrupted_rebuild runs before the record is applied

Second review:

Finding Handling
A damaged blob failed the write after the build was undone Fixed: every blob is read and hashed before anything is put back
apply --dry-run printed a mismatch-overwrite warning the run never prints Fixed: the replaced binary is reported as verify reports a file ready to patch
A marker that could not be read lost its backup Fixed: only a missing marker file counts as no marker
A record that leaves out one Ruby's binary was applied with no build Partly. Such a record is no longer applied with no build, so the left-out Ruby is never reported patched on the record's files alone. It falls back to 5.2.0's handling, which fails with the hash mismatch when the build lands on a binary the record brings. A record whose only library is not kept per Ruby (libfoo.so.1) is still applied with no build
The put-back could write backup content the record does not know Fixed: every file the backup holds must be the record's, with content the record knows
The recovery is not reached when the replaced binary is the record's first file in sort order Not fixed. The check that matches a record to a copy by one file is older than this change and is shared with every ecosystem. The miss needs the replaced binary to be the smallest key among the record's files that exist in the gem. For nokogiri that key is ext/nokogiri/xml_node.c, and the recovery runs. A record with no changed file that sorts before its binaries stays as it was in 5.2.0
rollback --offline asks for the original of the replaced binary, which the backup already holds Not fixed. The blob check runs before the gem is read. Online, the blob is downloaded and the rollback succeeds, as in the production run above

🤖 Generated with Claude Code


Generated by Claude Code

…s binaries

A patch made for a precompiled platform gem itself
(`pkg:gem/nokogiri@1.18.9?platform=x86_64-linux-gnu`) carries the
sources that gem packs and the binaries built from them, one per Ruby
the gem supports. In agent mode `apply` took the C source in it for a
build input: it wrote the record's files, then built the ruby gem's
source archive and put that build over the record's binary for the Ruby
that ran it. That file then held neither content of the record, so the
run reported the patch applied and every later `apply`, `apply --check`,
`vex` and `rollback` failed on the copy until `bundle pristine`.

A record for the copy's own platform that replaces or adds a shared
library now has no build inputs on that copy. `apply` writes its files
and starts no build, leaves no marker and no backup, and the copy holds
the files of the patched platform gem, which is what hosted mode
installs. `apply --check`, `vex` and `rollback` judge it by the record's
files alone, through the one scope every reader asks. A record for the
platform that changes a build input and brings no library is still
built, as the ruby gem's record is.

A copy 5.2.0 left in that state is put right: `apply` and `rollback`
first put back the binary that build replaced, from the backup that run
took, and drop its marker and backup. They do so only when that leaves
every file of the record with content the record knows; a copy with a
file edited by hand keeps the build over it.

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

A copy that 5.2.0 patched with a platform record holds a build of the
source gem over one of the record's own binaries, so every later command
failed on it. apply and rollback put the replaced binary back first, and
now do so only when that makes the copy one they can finish:

- apply: with the replaced binaries left out, the rest of the record
  passes a dry run under the run's own mismatch policy, and every blob
  the record writes is stored. A patch published again since is covered
  by the policy that overwrites content a Socket patch wrote.
- rollback: every other file of the record holds its original or its
  patched content.
- Neither acts when the backup holds content the record does not know,
  or lists files the earlier patch added.

A dry run puts nothing back and reports the copy as one the run patches.
A put-back that fails partway is reported as
native_extension_restore_failed and names `gem pristine`; rollback no
longer swallows that error. A build an earlier run left unfinished is
swept before the record is applied, and a graft's marker and backup are
dropped once the record is written whole.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Findings of a second review of the branch:

- A record is the copy's own build only when it brings a binary for
  every Ruby the copy ships one for. One that leaves a Ruby out is built
  as before, so that Ruby is not left on unpatched code under a copy
  reported as patched.
- The build over a record's binary is undone only when every blob the
  record writes is stored and holds the content the record names. A
  blob cut short would fail the write after the build was gone.
- The backup is put back only when every file it holds is a file of the
  record with content the record knows, since putting it back writes
  all of them.
- `apply --dry-run` reports the replaced binary as a file ready to
  patch, without the warning of a mismatch overwrite the run never
  makes.
- A marker that is there and cannot be read keeps its backup.

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

cursor Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes gem native-extension apply/rollback paths and on-disk backup/marker handling; mistakes could corrupt installed gems or skip needed rebuilds, though recovery is gated on strict preconditions and covered by many new tests.

Overview
Fixes 5.2.0 agent-mode behavior where a patch keyed to a precompiled platform gem (e.g. ?platform=x86_64-linux-gnu) that already ships per-Ruby .so/.bundle files was still treated as a native rebuild: after writing the record, apply built from C sources and grafted a local binary over the patch’s library, leaving that path in a hash_mismatch state for apply, apply --check, vex, and rollback.

New rule: when the manifest record names the copy’s platform, includes shared libraries, and covers every Ruby ABI the gem ships, it is classified as brings_own_build. Those records are applied whole via OwnBuildApply—no extension build, marker, or backup—matching hosted installs of the patched platform gem. Ruby-platform records and platform records with sources-only or incomplete per-Ruby binaries still rebuild/graft as before.

Recovery for stuck copies: apply and rollback can restore pre-graft binaries from the 5.2.0 backup first (replaced_binaries / put_back_replaced_build), but only when the rest of the record would succeed (dry-run + whole blobs for apply; known content on other files for rollback). Failed restore surfaces native_extension_restore_failed and suggests gem pristine. Successful whole apply drops stale graft marker/backup when the record overwrote every file they tracked. Docs in CLI_CONTRACT.md and docs/ecosystems.md describe the contract; extensive tests in gem_ext.rs cover apply, rollback, dry-run, mismatch policy, and edge cases where the build is left in place.

Reviewed by Cursor Bugbot for commit 8d32d98. 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 3 potential issues.

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 8d32d98. Configure here.

Such a record is not the copy's own build: its source stays a build
input, `apply` builds for the Ruby that runs it, and a build 5.2.0 put
over one of its binaries is not undone. That is 5.2.0's handling of
every platform record, kept here so that a Ruby the record has no
binary for is never reported patched on the record's files alone. The
contract says so, and a test holds it.

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

Three fixes to the recovery of a copy 5.2.0 left with a build over a
platform record's own binary:

- rollback put that build's backup back, and dropped the backup, once
  the other files held content the record knows, without checking the
  before blobs it then reads. A missing or truncated blob failed the
  rollback after the backup was gone, leaving the binary the build
  replaced (the unpatched one) with no way back to the build.
  replaced_build_files now also requires every before blob the rollback
  reads to be stored and to hash to the record, so such a copy keeps its
  build, marker and backup and fails as before.
- rollback --dry-run left the binaries it puts back out of the files it
  checked and reported nothing for them, so a copy whose other files
  were already original showed no work while the real run rewrote the
  library. The gem rollback now runs through gem_ext::rollback_gem_patch,
  which reports each such binary as a file to restore on a dry run.
- covers_every_ruby read a gem's library dir that could not be read as
  one that ships no other Ruby, so a platform record could be applied
  with no build while it left out another Ruby's binary. An unreadable
  dir, entry or sibling now counts as not covered, and the record is
  built as before.

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

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: final review of a75a977

What it does
In agent mode, a patch made for a precompiled platform gem (such as nokogiri-…-x86_64-linux-gnu) that already includes its compiled libraries, one for every Ruby the gem ships, is now written as it is. Nothing is built. Before this, 5.2.0 rebuilt the source gem and put that build over the patch's own binary, so apply --check, apply, vex and rollback all failed on the copy afterwards. apply and rollback now also repair copies 5.2.0 left that way, by putting the replaced binary back from the graft's backup. They do this only when the rest of the record can then be applied or rolled back.

Risk: medium. It changes the gem apply and rollback paths for precompiled platform gems, and moves files on disk during recovery. The new path is narrow: the record must name the copy's own platform, include a shared library, and cover every Ruby minor the gem ships. The recovery checks every blob and every file's content before it touches anything. Any other record goes through the 5.2.0 code unchanged.

Look here

Verified

  • Read the whole non-test diff. Checked that backup paths go through is_safe_relative_subpath before they are joined, that the files[…] indexing only uses keys taken from files, and that the dry runs write nothing.
  • Checked that rollback still calls restore_original_build afterwards, and that it finds no marker once the put-back has dropped it.
  • cargo test -p socket-patch-core --lib gem_ext passes locally (89 tests).
  • ci-ok and clippy are green on a75a977. All three Bugbot threads are fixed and resolved.
  • CHANGELOG.md is untouched. The PR also updates CLI_CONTRACT.md and docs/ecosystems.md.
  • The PR description includes a production run against nokogiri 1.18.9 x86_64-linux-gnu.

Changes I made
None in this review. The previous shepherd run pushed a75a977, which fixes the three Bugbot findings.

Open questions
The PR description lists two known gaps it leaves as they are, and both are older than this change:

  • The recovery is not reached when the replaced binary sorts first among the record's files. This does not happen with nokogiri.
  • rollback --offline needs the original blob of the replaced binary.

Decide whether you accept them.


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 4, 2026
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.

2 participants