Repository navigation
Description
[agent] Filed by the scheduled architecture refactor routine. Register: L148.
Kind: bug (one rule read two ways; a warning that names the wrong remedy). Found by running the file-permission entries of #2433 through the binary.
Problem
A file entry of a patch record may now carry a mode and leave the content as it is (beforeHash equal to afterHash): a permissions fix. apply, apply --check and vex judge such an entry by the file's bits. The contract says a rollback has nothing to restore for it.
rollback's other-scope check (#450) does not know this. Before it drops a record, it asks whether a copy in the other scope "still holds the patch", and answers yes for any file whose content hashes to afterHash:
copy_holds_patch, rollback.rs L434-L452
For an entry with unchanged content, every copy of the release as published hashes to afterHash. So when the other scope holds the same release, untouched:
rollbackrestores what it can, keeps the record and its blobs, and warns that the other copy "is still patched", namingsocket-patch rollback <purl> -g;- that command finds nothing to restore, keeps the record again, and names
socket-patch rollback <purl>; - neither run can ever drop the record, and both exit 0;
- the next
apply(a postinstall hook, CI) puts the patch back, content changes included.
It takes one such entry in a record, also beside entries that do change content. remove is the only way out, and nothing says so.
Who it reaches. No patch carries a mode yet: #2433 says the patch service has to send them first. From then on, any project whose patched release is also in the other scope: the same six or urllib3 in a virtualenv and in the system Python (the case #450 was reported with), a gem in vendor/bundle and in the system gems, an npm package that the global npm bundles.
Evidence on main (a95f10c)
Built binary, files only, --offline. The global install is a temp prefix (npm_config_prefix=<prefix>) holding left-pad 1.3.0 as published. The project holds the same release.
Control, a patch that changes index.js: the record goes.
$ socket-patch apply --offline
pkg:npm/left-pad@1.3.0 (via blob)
$ socket-patch rollback --offline
Rolled back 1 package:
pkg:npm/left-pad@1.3.0
Removed 1 patch from manifest.
A record with one entry, beforeHash = afterHash, "mode": "0644", the project's file installed 0666:
$ socket-patch apply --offline
pkg:npm/left-pad@1.3.0 (via permissions)
$ socket-patch rollback --offline # exit 0
Rolled back 1 package:
pkg:npm/left-pad@1.3.0 (already original)
Warning: pkg:npm/left-pad@1.3.0: its copy in the global install (<prefix>/lib/node_modules/left-pad) is still patched or could not be verified, so its manifest record was kept; run `socket-patch rollback pkg:npm/left-pad@1.3.0 -g` to restore it
$ socket-patch rollback pkg:npm/left-pad@1.3.0 -g --offline # exit 0
Rolled back 1 package:
pkg:npm/left-pad@1.3.0 (already original)
Warning: pkg:npm/left-pad@1.3.0: its copy in the project (./node_modules/left-pad) is still patched or could not be verified, so its manifest record was kept; run `socket-patch rollback pkg:npm/left-pad@1.3.0` to restore it
$ socket-patch rollback pkg:npm/left-pad@1.3.0 --offline # exit 0
Warning: pkg:npm/left-pad@1.3.0: its copy in the global install (…) is still patched … run `socket-patch rollback pkg:npm/left-pad@1.3.0 -g` to restore it
The global copy was never written by anything.
A record that changes index.js and fixes the permissions of lib/x.js: the rollback does not last.
$ socket-patch apply --offline
pkg:npm/left-pad@1.3.0 (via blob+permissions)
$ socket-patch rollback --offline # exit 0; index.js is restored
Rolled back 1 package:
pkg:npm/left-pad@1.3.0
Warning: … its copy in the global install (…) is still patched … so its manifest record was kept …
$ socket-patch apply --check --offline # exit 1
Error: Patches are OUT OF SYNC:
pkg:npm/left-pad@1.3.0: patch not applied (an installed copy is still unpatched)
$ socket-patch apply --offline # the next install's hook
pkg:npm/left-pad@1.3.0 (via blob) # index.js is patched again
$ socket-patch remove pkg:npm/left-pad@1.3.0 --offline --yes # the one command that drops it
Removed 1 patch from manifest:
The same first row came up unprompted on this machine with a real pip virtualenv: the system Python ships six 1.16.0, and the rollback of a permissions-only record for the virtualenv's six kept the record over /usr/lib/python3/dist-packages.
A failing test, beside global_rollback_drops_a_record_original_in_both_scopes in crates/socket-patch-cli/tests/global_scope_project_state.rs, with that file's helpers:
/// A record whose only entry leaves the content as it is (a permissions
/// fix), every copy original in both scopes: nothing is left to restore
/// anywhere, so the record goes, as it does for a content patch.
#[test]
fn global_rollback_drops_a_permissions_only_record() {
let project = agent_project(ORIG_INDEX);
let root = project.path();
let mut manifest = manifest_of(root);
manifest["patches"][V_PURL]["files"]["package/index.js"] = json!({
"beforeHash": git_sha256(ORIG_INDEX),
"afterHash": git_sha256(ORIG_INDEX),
"mode": "0644",
});
std::fs::write(
root.join(".socket/manifest.json"),
serde_json::to_vec_pretty(&manifest).unwrap(),
)
.unwrap();
let prefix = empty_prefix();
write_left_pad(&prefix.path().join("left-pad"), ORIG_INDEX);
let (code, stdout, stderr) = run(
root,
&["rollback", "--json", "--yes", "--offline", "--global-prefix", prefix.path().to_str().unwrap()],
);
assert_eq!(code, 0, "stdout={stdout}\nstderr={stderr}");
let v = parse(&stdout, &stderr);
assert!(
!v["warnings"]
.as_array()
.is_some_and(|w| w.iter().any(|w| w["code"] == "patched_in_other_scope")),
"no copy holds anything a rollback restores; {}",
v["warnings"]
);
assert!(manifest_of(root)["patches"].get(V_PURL).is_none(), "{v}");
}$ cargo test -p socket-patch-cli --test global_scope_project_state -- global_rollback_
test global_rollback_drops_a_record_original_in_both_scopes ... ok
test global_rollback_keeps_a_record_patched_in_the_project ... ok
test global_rollback_drops_a_permissions_only_record ... FAILED
(the other six global_rollback_ tests pass)
no copy holds anything a rollback restores; [{"code":"patched_in_other_scope","detail":"pkg:npm/left-pad@1.3.0: its copy in the project (./node_modules/left-pad) is still patched or could not be verified, so its manifest record was kept; run `socket-patch rollback pkg:npm/left-pad@1.3.0` to restore it"}]
Planned fix
An entry whose content the patch does not change holds nothing a rollback restores, so it cannot be what keeps a record. Tried on main: skipping such entries at the top of the loop in copy_holds_patch (if info.before_hash == info.after_hash { continue; }) makes the test above pass, keeps the eight other global_rollback_* tests of that file green, drops the record in both rows above, and still keeps it when a content file of the other copy is patched (apply -g, then rollback from the project).
The same reading, "content at afterHash means patched", is written in three more places that predate mode, and each should take the rule from one place:
holds_patched_bytesandcopy_carries_recordincommands/rollback.rs(the Gradle superseded-record check, and the patch-update path incommands/agent_download.rs);- the cargo twin in
ecosystem_dispatch.rsthat The "copy may still hold the patch" rule is written twice and drifted: cargo GC keeps a missing-blob modified copy, rollback's other-scope check drops it #1926 already asks to merge withcopy_holds_patchinto one core predicate. That predicate is the place for this rule.
drop_untouched_unloaded_gem_copies also calls copy_holds_patch: for a gem record with a permissions-only entry, a copy Bundler never loads counts as holding the patch. I read that and did not run it.
Not run: the three other sites, a gem, Windows (where a mode changes nothing).
Generated by Claude Code
Activity
refactor: L148, L149 filed as #2465, #2466