Skip to content

Fix in-place gem rebuild ignoring in-tree build products (#2104, #2105) - #2108

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-in-tree-build-products
Nov 17, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
agent/fix-gem-in-tree-build-products

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Fixes #2104
Fixes #2105

Summary

An agent-mode patch to a native build input of a gem that builds a library into its own tree and loads it from there (scrypt via ffi-compiler, digest-crc via a Rakefile) is rebuilt in the installed tree. That in-place rebuild only knew about the RubyGems extension dir, which such a gem leaves empty. This PR makes it track what the build does to the gem's own tree:

Staged rebuilds from the source gem (the normal path) and precompiled gems are not changed.

Root cause

rebuild in crates/socket-patch-core/src/patch/gem_ext.rs backed up, recorded and restored artifact_rels(extension_dir) and their lib/ copies only. For an in-tree gem that set is empty: the marker was written with "extArtifacts": {}, "libArtifacts": {}, so nothing was backed up, nothing could be verified, and a build that exited 0 was trusted whatever it produced.

How it works

  1. After the current build is parked (and under its lock), the gem's tree is copied to extensions/<platform>/<abi>/.<gem>.socket-patch-tree, beside the parked build and outside the gem dir. The record's own files are left out: the patch engine writes and reverts those.
  2. The files the staged source-gem build was seen to create in the gem's tree (StageFailure::InTree) are removed, as a fresh install has none of them.
  3. After the build, the tree is compared with the snapshot. Replaced and removed files go into the existing backup (.socket-patch-native-orig/lib/, listed in libFiles); added files are listed in a new createdFiles field. Loadable ones are hashed into the marker's libArtifacts.
  4. On failure the tree is restored from the snapshot before the parked build is moved back. recover_interrupted_rebuild does the same for a run that died.

The backup and marker formats only gain entries: an older release reads the new libFiles and libArtifacts entries with its existing code and ignores createdFiles.

Evidence

Real gems, Ruby 3.3.6, Linux (the repros from the issues):

Gem Before After
digest-crc 0.6.5 apply exit 0, patched=false, vex not_affected patched=true after apply; rollback gives patched=false; diff -rq of the whole vendor/ against the pre-apply copy is empty
scrypt 3.0.8 patched .so left after rollback marker lists ext/scrypt/x86_64-linux/libscrypt_ext.so; after rollback nm -D shows no patched symbol and diff -rq is empty

Tests (all failed on main, commit 1d18d1e adds them before the fix):

Local runs:

cargo clippy --locked --workspace --all-features -- -D warnings      ok
cargo test -p socket-patch-core --lib                                7780 passed
cargo test -p socket-patch-cli --test e2e_gem_native_ext_build -- --include-ignored   32 passed
  ... --test e2e_redirect_gem_build   44 passed
  ... --test e2e_vendor_gem_build     14 passed
  ... --test in_process_gem_apply --test in_process_gem_multi_platform   9 passed

cargo clippy --all-targets fails locally on main too (44 errors in test code of files this PR does not touch); CI's clippy command does not use --all-targets.

Review

/code-review high ran on the diff. Fixed in f8e88d0: snapshot moved out of the gem dir, parked build kept when the tree can't be restored, the "compiled nothing" refusal limited to gems that keep a library in their tree, rollback's in-place rebuild given the known products, identical rebuilt products attested and backed up, one read per snapshotted file, shared helpers.

Not fixed, with reasons:

  • Copies an older release already built. A backup or marker written by 5.1.0 for an in-tree gem does not record the tree. Such a marker still attests (it lists no artifact to check), and a re-apply over it backs up the 5.1.0-built library as the original. Both are the behavior before this PR, not a regression. Rejecting a marker with no artifacts would also stop attesting gems whose build legitimately compiles nothing on a platform, so that needs a maintainer's decision.
  • Symlinks and directories. The snapshot covers regular files, like the rest of this module's tree handling. A symlink a build removes or creates in the gem's tree is not restored. No gem in the reports does this.
  • Cost. An in-place rebuild now copies the gem's tree once and hashes it once more. It only runs when the staged build can't be used, and it is small next to the compile.

Cursor Bugbot found one issue on f8e88d0: recovery after a crash between writing the marker and dropping the parked build undid a committed build's tree. Fixed in 82ed2bf with a_committed_in_place_build_is_not_undone_by_the_next_run.

Follow-ups

https://claude.ai/code/session_011f9aePpTeM1ZTDzN1jcpr1


Generated by Claude Code

Claim commit for #2104 and #2105: the in-place rebuild of a gem's
native extension neither backs up nor records the libraries the
build leaves in the gem's own tree.

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_011f9aePpTeM1ZTDzN1jcpr1
Three host capstones over a gem that builds its library into its
own tree and loads it from there. Each fails on main:

- rollback leaves the patched library in place, and nothing
  records it (#2104)
- a build that takes the old library for current passes for a
  rebuild, with and without the source gem to compare (#2105)

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_011f9aePpTeM1ZTDzN1jcpr1
A gem that builds a library into its own tree and loads it from
there (scrypt, digest-crc) has no artifact in the RubyGems
extension dir, which is all the in-place rebuild backed up,
recorded and put back.

The rebuild now snapshots the gem's tree before the build. What a
finished build replaced or added there is backed up with the
original build and attested by the marker, so rollback restores it
byte for byte and vex notices it changing (#2104). A failed or
interrupted build is undone from the snapshot.

The products the source gem's build was seen to make are removed
before the in-place build, as a fresh install has none, so a build
script that takes an existing library for current compiles the
patched source. With none known, a build of a changed input that
leaves no compiled code changed fails instead of passing for a
rebuild (#2105).

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_011f9aePpTeM1ZTDzN1jcpr1
From the review of the previous commit:

- Keep the tree snapshot beside the parked extension dir, not in
  the gem dir, where a build script that globs for sources would
  find a second copy of them.
- Leave the parked build in place when the tree cannot be put
  back, so the next run retries from the snapshot instead of
  dropping it.
- Refuse a build that compiled nothing only for a gem that keeps
  a library in its own tree. A gem with no library anywhere
  compiles nothing on this platform, and passes as before.
- Hand the products the source gem's build makes to a rollback's
  in-place rebuild too, and hold that rebuild to the same check.
- Attest and back up a product that was rebuilt with the bytes it
  had, so the restore does not take it for one to remove.
- Hash a snapshotted file from the bytes already read.

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_011f9aePpTeM1ZTDzN1jcpr1
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 17, 2026 06:28
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches gem native extension rebuild, backup/marker formats, and rollback paths where incorrect file handling could leave gems half-patched or falsely attested; behavior is heavily tested but affects installed gem trees on disk.

Overview
In-place native gem rebuilds now snapshot, backup, record, and restore files the build writes inside the gem directory (e.g. FFI/Rake gems like scrypt, digest-crc), not only the RubyGems extension dir.

Before running the build, known in-tree build products from a staged source-gem build are removed so scripts that reuse an existing library cannot skip compiling patched sources. Builds that exit successfully but produce no new compiled artifacts are rejected as native_extension_build_failed. libArtifacts in the build marker and rollback backup cover libraries loaded from the gem tree; apply --check, vex, and rollback honor those bytes.

Failed or interrupted in-place rebuilds restore the gem tree from the snapshot (alongside extension-dir recovery). Docs and e2e/unit tests cover attestation, stale-library forcing, and compile-nothing refusal.

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

A run that died after it wrote the marker of an in-place build,
before it dropped the parked build and the tree snapshot, left all
three on disk. The next run restored the gem's tree from the
snapshot, putting the old in-tree library back under a finished
build.

Recovery now tells that state apart: the extension dir is a
finished build, and the marker was written after the snapshot was
taken and matches the tree. Such a build stands, and the snapshot
is dropped.

Found by Cursor Bugbot on the PR.

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_011f9aePpTeM1ZTDzN1jcpr1
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief

What it does
Fixes #2104 and #2105. Some gems (scrypt, digest-crc) build their native library into their own tree rather than into RubyGems' extension dir. When apply patches a build input, it rebuilds the gem in its installed tree. Before this PR, rollback left the patched library in place, and a build that just reused the old library still counted as patched. Now the rebuild snapshots the gem's tree first and backs up and attests whatever the build replaced or added. If the build fails, is interrupted, or exits without compiling anything, the tree is restored from the snapshot.

Risk: medium. Only the in-place gem rebuild path changes. Staged builds from the source gem and precompiled gems are untouched, but this new path writes, deletes and restores files inside a user's installed gem. The backup and marker formats only gain entries, and older releases ignore createdFiles. A gem that keeps a prebuilt library in its tree, whose build compiles nothing and whose extension dir is empty, is now refused with native_extension_build_failed. In that case the install is left as it was, so it is never broken.

Look here

  • gem_ext.rs:2818: the snapshot is taken under the parked build's lock, and known products are removed before the build.
  • gem_ext.rs:2568 restore_tree: the undo path. It deletes files the snapshot doesn't hold, leaving the patched record files and the .socket-patch-native* entries alone.
  • gem_ext.rs:2598 build_committed_since: crash recovery tells a committed build apart from an unfinished one by comparing the marker's mtime with the snapshot's and checking the marker's hashes.
  • gem_ext.rs:2719 stale_build: the new "compiled nothing" refusal.
  • gem_ext.rs:5133: rollback removes the files the build added.

Verified

Changes I made
None.

Open questions (non-blocking)

  • When the gem has no extension dir yet, set_aside takes no lock. A second socket-patch run on the same gem at the same moment could then drop the live snapshot in recover_interrupted_rebuild's drop_stale_tree_snapshots (gem_ext.rs:1643). The first run would then fail and could not restore the tree. In-tree gems normally have an extension dir with gem.build_complete, so I consider this an unlikely corner case, not a blocker.
  • The PR lists the things it deliberately leaves unfixed: markers written by older releases, symlinks in the gem's tree, and the cost of the copy. Each has a reason in the description.

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 17, 2026
Merged via the queue into main with commit 4441393 Nov 17, 2026
49 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-in-tree-build-products branch November 17, 2026 15:21
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Nov 17, 2026
Main gained the in-tree build products fix (#2108) and the hosted scan's
refresh of the archive beside an agent-patched gem (#2103). Both touch
files this branch changes; three hunks conflicted, each resolved by
keeping both sides.

crates/socket-patch-core/src/patch/gem_ext.rs,
`recover_interrupted_rebuild_sync`: this branch's `refuse_link(&ext, ..)`
(a link, or an extension dir that cannot be probed, is refused) in place
of main's unchanged `is_symlink` check, followed by main's new restore of
the gem's tree from the snapshot of an interrupted in-place build.

crates/socket-patch-cli/src/commands/gem_refresh.rs, `report`: main's two
headlines for gems patched in place become arms of this branch's `known`
headline, which the count of copies that could not be checked is then
added to. The words "the switch to hosted patches" come from `switch`.

crates/socket-patch-cli/src/commands/gem_refresh.rs,
`stale_after_restore`: this branch's listing of a copy that cannot be
judged whether or not its archive is cached, with main's new
`in_place: false` field.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Nov 17, 2026
The in-tree build products fix (#2108) acts on what a listing lacks: a
file the tree snapshot does not show is removed when a build is undone,
and a rollback removes what the marker lists and the backup does not
hold. Its probes read an error as "nothing there":

- `restore_tree` listed the snapshot's copies with a walk that skips
  what it cannot read. A snapshot whose copies could not be listed was
  read as holding none, and every file of the gem's own tree was removed.
  `snapshot_tree` listed the gem's tree the same way, so a directory that
  could not be read was left out and its files were the next undo's to
  remove. Both now use `try_tree_files`, which fails instead.
- Recovery took a snapshot dir it could not probe for no snapshot and
  settled the extension dir without putting the tree back; so did the
  failed-snapshot path of `rebuild`. `build_committed_since` took a marker
  it could not probe for no marker. Each is an error now.
- `back_up_tree_changes` and `note_absent_files` took a backup
  description that is there and cannot be read for no backup, recorded
  nothing, and let the build be committed. `try_read_backup_info` fails
  instead, and the build is undone.

The marker and backup formats are as #2108 left them, and
`read_marker_strict` and the restore read them unchanged.

The contract still said an in-place rebuild leaves a library in the gem's
own tree as last built, next to #2108's account of backing it up. That
sentence now says what the code does.

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

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

3 participants