Repository navigation
Description
[agent] Filed by the scheduled architecture refactor routine. Register: L149.
Kind: bug (a class fixed in one command and live in its twin).
Problem
The agent manifest records no scope, so a record says nothing about whether its patch went into the project or into a global install. #450 was that hazard for rollback: run in the wrong scope, it found an untouched copy, counted the patch as rolled back, and deleted the record and its blobs while the patched copy stayed patched. The fix (cfc1d12, #1714) made rollback look in the other scope before it drops a record (patched_in_other_scope).
remove runs the same restore and then drops the record itself, and it never asks. Run in the wrong scope, it:
- reports
1 package already in original stateandRemoved 1 patch from manifest, statussuccess, exit 0, with no warning; - deletes the blobs, the before-blob among them;
- leaves the patched copy patched.
After that nothing on the machine knows about the patched copy: rollback in the right scope says No patches in this project; nothing to roll back. and apply --check says No patches to check.
It happens in both directions, and the wrong scope is one forgotten or extra -g.
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; the project holds the same release; the record changes index.js from module.exports = 1; to module.exports = 2;.
| Applied with | Then | Says | Left |
|---|---|---|---|
apply -g |
remove <purl> -g (control) |
Rolled back 1 package, Removed 1 patch from manifest |
both copies original |
apply -g |
remove <purl> |
1 package already in original state, Removed 1 patch from manifest, Removed 2 unused blobs; exit 0 |
the global copy patched, no record, no blobs |
apply |
remove <purl> -g |
the same | the project's copy patched, no record, no blobs |
apply |
rollback -g (for comparison) |
Warning: … its copy in the project (./node_modules/left-pad) is still patched …, so its manifest record was kept; run socket-patch rollback pkg:npm/left-pad@1.3.0 to restore it |
the record and both blobs |
The second row, in full:
$ socket-patch apply -g --offline
Using global npm packages at: <prefix>/lib/node_modules
pkg:npm/left-pad@1.3.0 (via blob)
$ socket-patch remove pkg:npm/left-pad@1.3.0 --offline --yes # exit 0
The following patch will be removed:
- pkg:npm/left-pad@1.3.0 (UUID: 11111111, 1 file)
1 package already in original state
Removed 1 patch from manifest:
- pkg:npm/left-pad@1.3.0
Manifest updated at ./.socket/manifest.json
Removed 2 unused blobs (40 B freed)
$ cat <prefix>/lib/node_modules/left-pad/index.js
module.exports = 2;
$ socket-patch rollback -g --offline
No patches in this project; nothing to roll back.
$ socket-patch apply --check -g --offline
No patches to check.
Under --json the envelope is "status": "success" with two removed events and no warnings.
A failing test, beside global_rollback_keeps_a_record_patched_in_the_project in crates/socket-patch-cli/tests/global_scope_project_state.rs, with that file's helpers:
/// Project apply, then `remove --global-prefix`: the global copy is
/// already original and the project's copy is still patched. `rollback`
/// keeps the record there (#450); `remove` must as well.
#[test]
fn global_remove_keeps_a_record_patched_in_the_project() {
let project = agent_project(PATCHED_INDEX);
let root = project.path();
let prefix = empty_prefix();
write_left_pad(&prefix.path().join("left-pad"), ORIG_INDEX);
let args = [
"remove", V_PURL, "--json", "--yes", "--offline",
"--global-prefix", prefix.path().to_str().unwrap(),
];
let (code, stdout, stderr) = run(root, &args);
assert_eq!(code, 0, "stdout={stdout}\nstderr={stderr}");
let v = parse(&stdout, &stderr);
assert_eq!(
std::fs::read(root.join("node_modules/left-pad/index.js")).unwrap(),
PATCHED_INDEX,
"a global remove leaves the project's copy alone"
);
assert!(
manifest_of(root)["patches"].get(V_PURL).is_some(),
"the project's copy is still patched, so its record must stay; {v}"
);
assert!(
root.join(".socket/blobs").join(git_sha256(ORIG_INDEX)).is_file(),
"the blob that restores the project's copy must survive; {v}"
);
}$ cargo test -p socket-patch-cli --test global_scope_project_state -- global_r
test global_rollback_keeps_a_record_patched_in_the_project ... ok
test global_remove_keeps_a_record_patched_in_the_project ... FAILED
the project's copy is still patched, so its record must stay; {"command":"remove","status":"success","dryRun":false,"events":[{"action":"removed","purl":"pkg:npm/left-pad@1.3.0"},{"action":"removed","bytes":65,"details":{"count":2,"checked":2}}],"summary":{…,"removed":1,…,"bytesFreed":65},"gc":{"removedBlobs":2,…}}
Where
removehands the restore to rollback's engine and then drops the record with no look at the other scope: remove.rs L964-L973.rollbackchecks first, in its own flow only: [rollback.rs L2844-L2876](https://github.com/SocketDev/socket-patch/blob/a95f10c8882415ee6d7b8441d653797c4772e609/crates/socket-patch-cli/src/commands/rollback.rs#L2844-L2876).- The earlier fix:
git log -S'fn patched_in_other_scope'gives cfc1d12, "Fix rollback dropping records patched in the other scope (Agent-mode rollback in the other scope (rollback -g after a project apply, or rollback after a -g apply) deletes the manifest entry and blobs while the patched copy stays patched, and exits 0 #450) (Fix rollback dropping records patched in the other scope (#450) #1714)". Its four commits namerollbackonly.
Planned fix
Make the check part of what both commands share: before a record leaves the manifest because its copy here was restored, already original or not installed, ask the other scope, and keep the record with its variant group and its blobs when a copy there still holds the patch. remove then reports it as rollback does: the patched_in_other_scope warning, naming the purl, the copy and the command for the right scope, with the exit code unchanged (the contract's row for rollback: "Never flips the exit (#450)").
remove --skip-rollback asks for the record to go without a restore, so it stays as it is. The contract's patched_in_other_scope row needs remove added.
Related: #2286 (a failed npm root -g probe reads as "no global install", so the check rollback has finds nothing), and #2465, the same check under a patch entry that only fixes a file's permissions.
Not run: PyPI, gems and the other ecosystems (the code path is the shared one); remove by UUID or by name; Windows.
Generated by Claude Code
Activity
refactor: L148, L149 filed as #2465, #2466