Repository navigation
Keep the user's files when cleanup runs beside a manifest outside .socket/ - #2390
Mikola Lysenko (mikolalysenko) wants to merge 7 commits into
Conversation
Refs #2301. Assisted-by: Claude Code
A manifest outside a .socket/ directory makes its own directory the artifact directory: the project root for a bare file name, state/ for state/patches.json. The cleanup of repair, scan --prune, rollback and remove treated blobs/, diffs/ and packages/ there as socket-patch's own stores, so it deleted a monorepo's packages/README.md and packages/tsconfig.base.json as "unused package archives" and exited 0. The sweep now asks whose directory it walks. Under a directory named .socket, which socket-patch creates and fills itself, nothing changes. Anywhere else a pass considers only the files that carry a name socket-patch gives an artifact: a 64-digit hex name in blobs/, and <uuid>.tar.gz in diffs/ and packages/ (the names the pre-v5 stores used). Any other file is the user's: it is neither removed nor counted. Such a directory is removed only when the pass took a file out of it and that left it empty, so an empty packages/ the user made stays. Refs #2301. Assisted-by: Claude Code
One CLI test runs repair, rollback, remove and the three scan --prune forms with the manifest at the project root and at state/patches.json, over a project that keeps files of its own in blobs/, diffs/ and packages/. Each run must leave those files and still take out what socket-patch can have written there. On main's sweep every one of the twelve runs deletes the user's files. The contract's GC paragraph now says what a sweep takes outside a .socket/ directory. Fixes #2301. Assisted-by: Claude Code
The first cut went by the name alone, and the review found three ways that falls short beside a manifest outside .socket/. A file a project keeps under a 64-digit hex name (a store keyed by plain sha256) was still deleted from blobs/. A blob is named for the git-sha256 of its bytes, so the sweep now removes an unreferenced one only when its bytes hash to its name, with the check repair already uses for a corrupt blob. An archive is taken only when it is named for a patch of the manifest the command loaded, or one the run itself just removed: no other <uuid>.tar.gz is shown to be this project's. The .socket rule read the path's last component, which `.` from inside the directory and a sub/.. detour do not have, so those runs left debris main removes. A path that does not end in the name is resolved first. A packages that is a link or a plain file raised a cleanup_failed warning on every run, as if a store had failed to sweep. Outside .socket/ it is the user's, and the pass is empty. Refs #2301. Assisted-by: Claude Code
A second review showed the archive rule of the last commit did not hold together: scan --prune sweeps against the manifest it has already pruned, so the archives of the patches it drops were never taken, and a record's uuid is a free string, so a manifest with "uuid": "release" made packages/release.tar.gz an archive. Nothing in a pre-v5 archive shows whose it is, and no v5 run writes one. So beside a manifest outside .socket/ the sweep now takes blobs alone (a file whose bytes hash to its name) and leaves diffs/ and packages/ as they are. A pre-v5 archive left beside such a manifest stays until it is deleted by hand. That also drops the uuid set from the reference type and gives the production change back its shape: one early branch in sweep, one condition in the pass. A linked level above blobs/ is no longer refused there. With a bare manifest name the level above is the project directory, so --cwd through a symlink made every pass fail with a cleanup_failed warning on each run, on main too. The write path never checked that level. Refs #2301. Assisted-by: Claude Code
The last commit resolved the artifact directory before reading its name, so `.` from inside .socket/ and a sub/.. detour got the full sweep. A third review showed what that costs: a linked .socket reached by a detour passes the link check, which only walks the levels of the path as given, and resolving it handed that directory the full sweep its plain spelling is refused. The rule is the last component of the path again, as the apply lock reads it. A spelling that does not end in .socket gets the narrower sweep: unreferenced blobs go, and what only a .socket/ sweep may take stays. That leaves less cleaned up than main for those two spellings, and deletes nothing main kept. The contract says which spelling counts and that a link above blobs/ is followed, not skipped. The v5 migration notes name the archives the cleanup now leaves beside a relocated manifest. One test binding moves into its cfg(unix) block. Fixes #2301. Assisted-by: Claude Code
|
bugbot run Generated by Claude Code |
PR SummaryMedium Risk Overview Sweep behavior in Edge cases: non-directory Docs ( Tests: integration across repair/rollback/remove/scan prune variants for two manifest layouts; extensive unit tests for shared vs own scope. Reviewed by Cursor Bugbot for commit 40da0e2. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ 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 40da0e2. Configure here.
Outside .socket/ the sweep now reads files in a directory the user owns, to tell a blob by its bytes. That read was a plain open after an lstat: a pipe swapped in between the two would block the cleanup for good, waiting for a writer. The blob check goes through the workspace's regular-file opener, as every other read of a project file does: the open does not block, the handle is checked to be a regular file, and the size comes off the handle that is hashed. repair's corrupt-blob check shares the function and gets the same open. The new test fails against the plain open after its five-second deadline. Refs #2301. Assisted-by: Claude Code
|
[agent] PR shepherd: review brief for the approver What it does Risk Look here
Verified
Changes I made Open questions Generated by Claude Code |

LLM Description written by Claude Code
Fixes #2301.
Problem
A manifest outside a
.socket/directory is a documented layout (--manifest-path state/patches.json; a bare file name puts it at the project root). Its own directory then holdsblobs/,diffs/andpackages/. The cleanup thatrepair,rollback,remove,scan --pruneandscan --syncend with treated those three directories as socket-patch's own stores and deleted every regular file directly in them: a monorepo'spackages/README.mdandpackages/tsconfig.base.json, anything in adiffs/orblobs/the project keeps. The run exits 0 and reports them as "unused package archives". Files that were not committed are gone for good.Evidence on
main(1b3f422)The built binary, no network.
cc66…is a real orphan blob (the git-sha256 of its bytes);2cf2…is the project's own file, named by the plain sha256 of its bytes.The CLI test this PR adds runs six commands over two layouts. Against
main's sweep (cargo test -p socket-patch-cli --test manifest_path_ledger_root cleanup_beside) all twelve runs fail:The unit test from the issue fails on
mainthe same way (cargo test -p socket-patch-core --lib sweep_spares_user_files):the sweep deleted files socket-patch never wrote (dry_run=true): ["logo.png", "review.diff", "README.md", "tsconfig.base.json"].Fix
All four callers share core's
ArtifactReferences::sweep, so the rule lives there. Only a directory named.socketis socket-patch's own; that is the gate the apply lock already uses before it removes an emptied one. Anywhere else:blobs/: an unreferenced file goes only when it is a blob by its bytes: a 64-digit hex name that is the git-sha256 of the file's contents. This is the checkrepairalready runs for a corrupt blob, now one function both use, and it opens the file through the FIFO-safe regular-file opener. Any other file is the project's: neither removed nor counted.diffs/andpackages/: left alone. No v5 command writes either, and nothing in a pre-v5 archive shows whose it is.blobsthat is a link or a plain file is skipped, and a link above it is followed as the blob writes follow it. Onmain,--cwdthrough a symlink with a bare manifest name made all three passes fail with acleanup_failedwarning on every run.With the fix, the same commands:
Deleted
Nothing is removed.
get_corrupt_blobsand the sweep now share oneblob_file_is_intactinstead of the sweep growing a second copy of that check.Behavior change
.socket/manifest.json): none..socket/: cleanup removes less, as above.gc,details.checkedand theRemoved …lines no longer count the project's files. No flag, default, exit code, JSON shape or file format changes..socket/:--manifest-path manifest.jsonrun from inside.socket/, and a.socket/sub/../manifest.jsondetour, do not end in the name, so they get the narrower sweep: unreferenced blobs go, other debris stays. Nothingmainkept is deleted.CLI_CONTRACT.md(the GC paragraph) anddocs/migrating-to-v5.mdsay this.Risk
diffs/orpackages/archive is no longer cleaned up, and neither is an unreferenced blob whose bytes no longer hash to its name (a CRLF checkout). Both are left in silence; the migration notes name the archives.blobs/is read once per cleanup to tell a blob from the project's file.cfgbranch. One new test has acfg(unix)block for its symlink cases.Review
Three passes of
/code-review high, then Cursor Bugbot. Each confirmed finding in this change is fixed in the later commits (the commit messages say which); Bugbot's one finding, a plain open of a file in a user-ownedblobs/, is fixed in the last commit, with a test that fails against the plain open. Left as they are:blobs/, and one's cleanup takes the other's blobs. Reproduced onmain, unchanged here. Whether one directory may serve two manifests is a decision, not a fix for this PR.blobs/the user made beside a relocated manifest (the CLI's download unwind). Read, not run; only an already-empty directory is at stake..socket-dl-*stage file left by a killed download is never swept. Onmaintoo, in every layout..socketreached by a detour the full sweep its plain spelling is refused. The name check stays.Checks
cargo clippy --locked --workspace --all-features -- -D warnings: clean.--all-targetson core fails onmainin files this PR does not touch.manifest_path_ledger_root(11),remove(90),rollback(140),scan(268),cli(125),in_process_vendor(156),in_process_remove_repair_lifecycle,in_process_scan, the threein_process_rollback_*,in_process_get_manifest_path,in_process_pnpm_registry_cleanup,in_process_edge_cases,cli_remove_silent,cli_scan_silent: all passed.repair: 116 passed. Its two chmod tests fail as root in the sandbox, onmaintoo, and pass as uid 1000 (repair_cleanup_failure_is_reported_in_json_and_silent_modes,repair_exits_zero_and_stays_quiet_when_lock_file_unremovable).Diff
5 files, +473 −22: production +84 −14 (core
manifest/cleanup_blobs.rs+67 −7,api/blob_fetcher.rs+17 −7), tests +386 −7 (unit +240 −7, CLI +146), docs +3 −1.Generated by Claude Code