Skip to content

Keep the user's files when cleanup runs beside a manifest outside .socket/ - #2390

Open
Mikola Lysenko (mikolalysenko) wants to merge 7 commits into
mainfrom
arch-refactor/2301-sweep-spares-user-files
Open

Mikola Lysenko (mikolalysenko) wants to merge 7 commits into
mainfrom
arch-refactor/2301-sweep-spares-user-files

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

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 holds blobs/, diffs/ and packages/. The cleanup that repair, rollback, remove, scan --prune and scan --sync end with treated those three directories as socket-patch's own stores and deleted every regular file directly in them: a monorepo's packages/README.md and packages/tsconfig.base.json, anything in a diffs/ or blobs/ 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.

$ ls packages blobs
blobs:     2cf24dba…938b9824  cc66f07c…a6854055
packages:  README.md  tsconfig.base.json  web
$ socket-patch repair --offline --manifest-path socket-patches.json
No patches in manifest; nothing to download.

Removed 2 unused blobs (13 B freed)
Removed 2 unused package archives (47 B freed)

Repair complete.
(exit 0)
$ ls packages blobs
ls: cannot access 'blobs': No such file or directory
packages:  web

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:

repair --manifest-path socket-patches.json: deleted the user's ["blobs/logo.png", "diffs/review.diff", "packages/README.md", "packages/tsconfig.base.json", "blobs/ffff…ffff", "diffs/33333333-….tar.gz", "packages/22222222-….tar.gz"]
rollback --manifest-path socket-patches.json: deleted the user's [the same seven]
remove --manifest-path socket-patches.json: deleted the user's [the same seven]
scan --prune --manifest-path socket-patches.json: deleted the user's [the same seven]
scan --mode agent --prune --manifest-path socket-patches.json: deleted the user's [the same seven]
scan --sync --prune --manifest-path socket-patches.json: deleted the user's [the same seven]
(and the same six lines for --manifest-path state/patches.json)
test result: FAILED. 0 passed; 1 failed

The unit test from the issue fails on main the 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 .socket is 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 check repair already 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/ and packages/: left alone. No v5 command writes either, and nothing in a pre-v5 archive shows whose it is.
  • The directory is removed only when the sweep took a file out of it and that emptied it.
  • A blobs that is a link or a plain file is skipped, and a link above it is followed as the blob writes follow it. On main, --cwd through a symlink with a bare manifest name made all three passes fail with a cleanup_failed warning on every run.

With the fix, the same commands:

$ socket-patch repair --offline --manifest-path socket-patches.json
No patches in manifest; nothing to download.

Removed 1 unused blob (8 B freed)

Repair complete.
(exit 0)
$ ls packages blobs
blobs:     2cf24dba…938b9824
packages:  README.md  tsconfig.base.json  web

Deleted

Nothing is removed. get_corrupt_blobs and the sweep now share one blob_file_is_intact instead of the sweep growing a second copy of that check.

Behavior change

  • Default layout (.socket/manifest.json): none.
  • A manifest outside .socket/: cleanup removes less, as above. gc, details.checked and the Removed … lines no longer count the project's files. No flag, default, exit code, JSON shape or file format changes.
  • Two odd spellings of .socket/: --manifest-path manifest.json run from inside .socket/, and a .socket/sub/../manifest.json detour, do not end in the name, so they get the narrower sweep: unreferenced blobs go, other debris stays. Nothing main kept is deleted.

CLI_CONTRACT.md (the GC paragraph) and docs/migrating-to-v5.md say this.

Risk

  • Beside a relocated manifest, a pre-v5 diffs/ or packages/ 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.
  • There, each unreferenced 64-hex-named file in blobs/ is read once per cleanup to tell a blob from the project's file.
  • Production code has no cfg branch. One new test has a cfg(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-owned blobs/, is fixed in the last commit, with a test that fails against the plain open. Left as they are:

  • Two manifests in one directory share blobs/, and one's cleanup takes the other's blobs. Reproduced on main, unchanged here. Whether one directory may serve two manifests is a decision, not a fix for this PR.
  • A failed download can remove an empty blobs/ the user made beside a relocated manifest (the CLI's download unwind). Read, not run; only an already-empty directory is at stake.
  • A .socket-dl-* stage file left by a killed download is never swept. On main too, in every layout.
  • Resolving the path to recognise the two odd spellings. The first pass asked for it; the third showed it hands a linked .socket reached by a detour the full sweep its plain spelling is refused. The name check stays.
  • A runtime hint for the archives left behind. The contract and migration notes say it; no new warning code.

Checks

  • cargo clippy --locked --workspace --all-features -- -D warnings: clean. --all-targets on core fails on main in files this PR does not touch.
  • Core lib: 8670 passed. CLI lib: 1083 passed.
  • CLI suites 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 three in_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, on main too, 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

Refs #2301.

Assisted-by: Claude Code
@mikolalysenko Mikola Lysenko (mikolalysenko) added arch-refactor PR opened by the scheduled architecture refactor routine refactor Structural change: duplicated code or logic, missing abstraction, layering, dead code labels Dec 1, 2026
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
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 1, 2026 02:28
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes filesystem cleanup semantics for non-.socket manifest layouts—incorrect blob identification could still delete user data or leave orphans; default .socket/ layout is unchanged.

Overview
Fixes #2301: artifact cleanup no longer treats every file under co-located blobs/, diffs/, and packages/ as socket-patch state when the manifest lives outside a .socket/ directory (e.g. --manifest-path state/patches.json).

Sweep behavior in ArtifactReferences::sweep now keys off whether the artifact directory’s last path component is .socket. Under .socket/, behavior is unchanged (full sweep of blobs, diffs, and packages). Beside a relocated manifest (Scope::Shared), only blobs/ is considered; an unreferenced file is removed only if it is a 64-hex git-sha256 blob whose contents match the name (via shared blob_file_is_intact). Project files—including monorepo packages/, diffs/, misnamed hex files, and legacy archives—are left untouched and excluded from GC counts. diffs/ and packages/ are not swept in shared layout. Empty blobs/ is removed only after the pass actually deleted something (or under .socket/).

Edge cases: non-directory blobs (file/symlink) is skipped without cleanup_failed; project roots reached through symlinks are followed for blob cleanup.

Docs (CLI_CONTRACT.md, migrating-to-v5.md) document the narrower GC beside relocated manifests (pre-v5 archives in diffs//packages/ must be removed manually).

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.

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

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
When the manifest lives outside a .socket/ directory (--manifest-path state/patches.json or a bare file name), the cleanup at the end of repair, rollback, remove and scan --prune/--sync no longer deletes the project's own files in blobs/, diffs/ and packages/ (for example a monorepo's packages/README.md). There it removes only unreferenced files in blobs/ whose bytes hash (git-sha256) to their 64-hex name, and leaves diffs/ and packages/ alone. The default .socket/ layout behaves as before.

Risk
Low. The change only narrows what gets deleted outside .socket/, and the .socket/ path takes the same code as before. The cost: pre-v5 archives and CRLF-mangled blobs beside a relocated manifest are no longer cleaned up. The migration notes and CLI_CONTRACT say so.

Look here

  • cleanup_blobs.rs:101: the .socket gate (last path component only) and the early return for the shared layout
  • cleanup_blobs.rs:112: a blobs that is a link or file is skipped without a warning
  • cleanup_blobs.rs:228: shared scope only takes a file that is a blob by its bytes
  • cleanup_blobs.rs:253: a shared blobs/ is removed only if this pass emptied it
  • blob_fetcher.rs:98: blob_file_is_intact, now shared with repair's corrupt-blob check and using the FIFO-safe opener (callers lstat first, so symlinked blobs are handled as before)

Verified

Changes I made
None.

Open questions
None.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-refactor PR opened by the scheduled architecture refactor routine Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review refactor Structural change: duplicated code or logic, missing abstraction, layering, dead code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

With the manifest outside .socket/, repair, scan --prune, rollback and remove delete the project's own files in packages/, diffs/ and blobs/

2 participants