Repository navigation
Description
[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).
Summary
Some gems build a library into their own directory and load it from there (FFI gems built with ffi-compiler, such as scrypt). An agent-mode patch to a C source of such a gem is rebuilt in the installed tree, as docs/ecosystems.md describes. But the compiled library that the rebuild overwrites is not backed up and not recorded: the marker is written with "extArtifacts": {}, "libArtifacts": {} and .socket-patch-native-orig/ holds only gem.build_complete and gem_make.out.
So rollback restores the C source, removes the record, exits 0, and leaves the patched binary in place. The app keeps loading the patched code. apply --check says "No patches to check", list says "No patches in this project", and bundle install keeps the copy. Only bundle pristine scrypt gets the original back.
Impact
Rollback is the user's way out of a patch that broke something. Here it reports success and changes nothing that Ruby loads, and no command shows that the gem still runs patched code. The same gap means a later tamper or a make in the tree is invisible to apply --check and vex, because no artifact hash is recorded.
Repro
Linux, Ruby 3.3.6, a compiler toolchain. No Socket API needed: the record is written by hand.
mkdir scrypt-repro && cd scrypt-repro
printf 'source "https://rubygems.org"\ngem "scrypt", "3.0.8"\n' > Gemfile
bundle config set --local path vendor/bundle && bundle install
G=vendor/bundle/ruby/3.3.0/gems/scrypt-3.0.8
SO=$G/ext/scrypt/x86_64-linux/libscrypt_ext.so
sha256sum $SO # original build
# mkrec.py: write an agent-mode record (.socket/manifest.json + blobs) for one changed file
cat > mkrec.py <<'PY'
import sys, json, hashlib, os
purl, key, before, after = sys.argv[1:5]
os.makedirs(".socket/blobs", exist_ok=True)
def gh(b): return hashlib.sha256(b"blob %d\0" % len(b) + b).hexdigest()
b, a = open(before, "rb").read(), open(after, "rb").read()
for x in (b, a): open(".socket/blobs/" + gh(x), "wb").write(x)
json.dump({"patches": {purl: {"uuid": "77777777-7777-4777-8777-777777777772",
"exportedAt": "2026-01-01T00:00:00Z",
"files": {key: {"beforeHash": gh(b), "afterHash": gh(a)}},
"vulnerabilities": {"GHSA-aaaa-bbbb-cccc": {"cves": ["CVE-2026-0001"], "summary": "s", "severity": "HIGH", "description": "d"}},
"description": "synthetic test patch", "license": "MIT", "tier": "free"}}},
open(".socket/manifest.json", "w"), indent=2)
PY
cp $G/ext/scrypt/scrypt_ext.c before.c
(cat before.c; printf '\nint socket_patched_marker(void) { return 1; }\n') > after.c
python3 mkrec.py pkg:gem/scrypt@3.0.8 ext/scrypt/scrypt_ext.c before.c after.c
socket-patch apply # 1 of 1 applied
nm -D $SO | grep -c socket_patched_marker # 1: the patched library is loaded from the gem's tree
cat $G/.socket-patch-native-build.json # "extArtifacts": {}, "libArtifacts": {}
socket-patch rollback # exit 0, "Removed 1 patch from manifest"
cmp $G/ext/scrypt/scrypt_ext.c before.c # source restored
nm -D $SO | grep -c socket_patched_marker # still 1
sha256sum $SO # not the original hash
socket-patch apply --check # "No patches to check."
bundle install # keeps the copy
nm -D $SO | grep -c socket_patched_marker # still 1Expected
docs/ecosystems.md, "Ruby gems: native extensions": "apply keeps the original compiled code in .socket-patch-native-orig/ inside the gem directory [...], so rollback restores it byte for byte without recompiling, and records what it built in .socket-patch-native-build.json." And: "A gem that builds a library into its own directory and loads it from there (an FFI gem such as argon2) is always rebuilt from its installed tree."
After rollback the gem directory is byte for byte what bundle install left, including ext/scrypt/x86_64-linux/libscrypt_ext.so and scrypt_ext.o.
Actual
After rollback, two files differ from the pre-patch tree:
gems/scrypt-3.0.8/ext/scrypt/x86_64-linux/libscrypt_ext.so (patched build, exports socket_patched_marker)
gems/scrypt-3.0.8/ext/scrypt/x86_64-linux/scrypt_ext.o
The record is gone, so nothing lists or checks the gem any more.
Versions
| socket-patch | Ruby | Bundler | Result |
|---|---|---|---|
main 3b14afc |
3.3.6 | 4.0.18 | reproduces |
main 3b14afc |
3.3.6 | 2.6.9 | reproduces |
| 5.1.0 release | 3.3.6 | 4.0.18 | reproduces |
Gems that RubyGems builds into the extension dir (26 tried on the same main, among them puma, nio4r, bcrypt, json, msgpack, bootsnap, oj, prism, openssl, psych) roll back byte for byte, and so do precompiled nokogiri, ffi, sqlite3 and google-protobuf.
Linux only was tested. Nothing here is platform-specific, so macOS (.bundle / .dylib) should behave the same.
First bad version
Present in 5.1.0, the first release with the agent-mode rebuild (#1998). Not a regression of #2098.
Suspect code
crates/socket-patch-core/src/patch/gem_ext.rs:2372 (rebuild, the in-place path): the backup and the marker (gem_ext.rs:2326-2346) cover only artifact_rels(extension_dir) and their lib/ copies. in_tree_products (gem_ext.rs:3010) already knows how to find the loadable files a build leaves in the gem's own tree, but it is used only to decide against a staged build, not to back up or record them.
Generated by Claude Code
Activity
bundler: 2026-11-17 run (agent native rebuild; #2104, #2105, #2106)
mikolalysenko commentedon Nov 17, 2026
[agent] Claiming this issue. Branch: agent/fix-gem-in-tree-build-products.
Generated by Claude Code
Test in-tree gem build products end to end
Track in-tree products of in-place gem builds