Repository navigation
Apply a platform gem's own patch without rebuilding over its binaries, and repair copies 5.2.0 left stuck - #2499
Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
Conversation
…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>
PR SummaryMedium Risk Overview 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 Recovery for stuck copies: Reviewed by Cursor Bugbot for commit 8d32d98. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.
❌ 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
|
[agent] PR shepherd: final review of What it does 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
Changes I made Open questions
Decide whether you accept them. Generated by Claude Code |

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 ranapply.The file it replaced holds neither the original nor the patched content of the record, so every later command failed on the copy:
apply --checkexits 1 ("matches neither the original nor the patched bytes")applyfails on the hash mismatchvexcannot attest the gemrollbackrefuses the copyThe 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
rubyrecord 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:
applybuilds 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
applyandrollbackput 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.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-runcounts the copy as one that can be patched. A put-back that fails partway is reported asnative_extension_restore_failedand namesgem pristine. Once the record is written whole, the graft's marker and backup are dropped.Tests
Unit tests in
patch/gem_ext.rscover the rule, a clean apply, the recovery byapplyand byrollback, 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 forx86_64-linux-gnu. The first two rows ran inruby:3.3andruby:3.2containers, the rest inruby:3.3:get <patch> --mode agentapply --check,apply,vex,rollbackafter itapply --checkapply --dry-runapplyapply --check,vex,rollbackrollback --dry-run, thenrollbackNot run: Ruby 3.1 and 3.4 containers.
Review
/code-review highran 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:
--dry-runreported something other than what the run doesrollbackswallowed a failed put-backnative_extension_restore_failedgem pristinerecover_interrupted_rebuildruns before the record is appliedSecond review:
apply --dry-runprinted a mismatch-overwrite warning the run never printslibfoo.so.1) is still applied with no buildext/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.0rollback --offlineasks for the original of the replaced binary, which the backup already holds🤖 Generated with Claude Code
Generated by Claude Code