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

Ouverte
#2,301 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
54/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
rust
Domaine
backend, cli

Piste de recherche

Start with crates/socket-patch-core/src/manifest/cleanup_blobs.rs, where ArtifactReferences::sweep and cleanup_dir handle artifact cleanup, then inspect its callers in repair.rs, remove.rs, rollback.rs and scan/gc.rs. Run the described sweep_spares_user_files_beside_a_manifest_outside_dot_socket test and add CLI coverage for the listed commands. Done means cleanup preserves ordinary user files beside a relocated manifest while retaining existing .socket/ cleanup behavior.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

agent:triaged arch-audit bug priority:p3

[agent] Found by the architecture refactor routine (ledger discussion #2244, lead L43), on main at cfc870f.

Problem

With --manifest-path (or SOCKET_MANIFEST_PATH) naming a manifest that is not inside a .socket/ directory, the directory the manifest sits in becomes the artifact directory. For a bare file name that is the project root. The cleanup that repair, scan --prune, scan --sync, rollback and remove run then treats <that directory>/blobs, <that directory>/diffs and <that directory>/packages as socket-patch's own stores and deletes the files in them:

  • every regular, non-hidden file directly in packages/ and in diffs/;
  • every regular, non-hidden file directly in blobs/ that is not a blob the manifest references;
  • the directory itself, when that leaves it empty.

The run exits 0 and reports the files as "unused package archives", "unused diff archives" or "orphan files". Subdirectories are left alone.

A manifest outside .socket/ is a layout the contract documents: "a manifest file elsewhere (state/patches.json, /etc/socket/manifest.json) relocates only the manifest and its lock/artifact directory, not the project" (crates/socket-patch-cli/CLI_CONTRACT.md, line 96), and docs/migrating-to-v5.md (line 50) gives --manifest-path state/patches.json as the example.

Who it hurts

  • A monorepo that keeps its manifest at the repository root, next to the lockfile, and has a packages/ directory. The first scan --mode agent --prune deletes packages/README.md, packages/tsconfig.base.json, a shared jest.config.js: whatever sits directly in packages/. The next build fails, in CI too.
  • Anyone whose manifest directory has a diffs/ or blobs/ directory of their own.
  • Files that are not committed are gone for good. Only repair --dry-run names the files; every other run gives a count.

Evidence on main

The binary is built from cfc870f. No network or patch API is needed.

$ mkdir -p app/packages/web && cd app && git init -q
$ echo '{"compilerOptions":{"strict":true}}' > packages/tsconfig.base.json
$ echo '# Packages' > packages/README.md
$ echo '{"patches":{}}' > socket-patches.json
$ ls packages
README.md  tsconfig.base.json  web

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

Would remove 2 unused package archives (47 B freed)
Unused package archives:
  - README.md
  - tsconfig.base.json

Dry run: no changes made.                                   (exit 0)

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

Removed 2 unused package archives (47 B freed)

Repair complete.                                            (exit 0)
$ ls packages
web

rollback says there is nothing to roll back and deletes the file anyway:

$ echo '# Packages' > packages/README.md
$ SOCKET_MANIFEST_PATH=socket-patches.json socket-patch rollback --offline
No patches in this project; nothing to roll back (no manifest at ./socket-patches.json).

Freed 11 B of unused blobs and archives                     (exit 0)
$ ls packages
web

The contract's own example path loses its files the same way, and the emptied directory goes with them:

$ mkdir -p app2/state/packages && cd app2 && git init -q
$ echo keep > state/packages/index.json
$ echo '{"patches":{}}' > state/patches.json
$ socket-patch repair --offline --manifest-path state/patches.json
No patches in manifest; nothing to download.

Removed 1 unused package archive (5 B freed)

Repair complete.                                            (exit 0)
$ ls state
patches.json

With a patch API (an npm tree with six patched packages and a stand-in API; the scripts are in the routine's ledger entry of 2026-11-26 22:56Z), the first agent-mode run of a project that has no manifest yet does it:

$ socket-patch scan --mode agent --prune --manifest-path socket-patches.json
Applied 6 patches.
GC: pruned 0 manifest entries and removed 2 orphan files (47 B).      (exit 0)
$ ls packages
web

$ socket-patch remove lodash --yes --manifest-path socket-patches.json
Removed 1 unused blob (41 B freed)
Removed 1 unused package archive (11 B freed)                         (exit 0)
Command, with a manifest at the project root packages/*, diffs/*, stray blobs/* files
repair deleted
scan --prune (hosted, the default mode, with the manifest file present; agent) deleted
scan --sync deleted, on a run that exits 1 too
rollback deleted, with an empty manifest too
remove <target> deleted
repair --dry-run, scan --prune --dry-run kept; repair names them, scan counts them
scan, apply, list, vex kept

The default layout is not affected: with the manifest at .socket/manifest.json the swept directories are .socket/blobs, .socket/diffs and .socket/packages.

A unit test for core manifest/cleanup_blobs.rs that hands the sweep a project root, as socket_dir_of does for a bare file name, fails on main:

#[tokio::test]
async fn sweep_spares_user_files_beside_a_manifest_outside_dot_socket() {
    let tmp = tempfile::tempdir().unwrap();
    let project = tmp.path().join("app");
    for (dir, file) in [
        ("packages", "tsconfig.base.json"),
        ("packages", "README.md"),
        ("diffs", "review.diff"),
        ("blobs", "logo.png"),
    ] {
        std::fs::create_dir_all(project.join(dir)).unwrap();
        std::fs::write(project.join(dir).join(file), "the user's own file").unwrap();
    }
    std::fs::create_dir_all(project.join("packages/web")).unwrap();

    // What `socket_dir_of("socket-patches.json", cwd)` hands the sweep.
    let sweep = ArtifactReferences::active(&PatchManifest::new())
        .sweep(&project, false)
        .await;

    let removed: Vec<String> = [sweep.blobs, sweep.diffs, sweep.packages]
        .into_iter()
        .flat_map(|pass| pass.map(|r| r.removed_blobs).unwrap_or_default())
        .collect();
    assert!(
        removed.is_empty(),
        "the sweep deleted files socket-patch never wrote: {removed:?}"
    );
    assert!(project.join("packages/tsconfig.base.json").exists());
}
$ cargo test -p socket-patch-core --lib sweep_spares_user_files
---- manifest::cleanup_blobs::tests::sweep_spares_user_files_beside_a_manifest_outside_dot_socket stdout ----
the sweep deleted files socket-patch never wrote: ["logo.png", "review.diff", "README.md", "tsconfig.base.json"]

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 8642 filtered out

Cause

Two rules that are each right for .socket/ meet in a directory that is not socket-patch's:

  • socket_dir_of (CLI args.rs, line 767) returns the manifest's parent directory, and --cwd for a bare file name. That is where apply.lock and blobs/ go.
  • ArtifactReferences::sweep (core manifest/cleanup_blobs.rs, line 86) sweeps blobs, diffs and packages under that directory, and cleanup_dir (line 134) "treats it as authoritative socket-patch state (so any regular non-hidden file is considered for removal)". The diffs and packages passes keep nothing: "every file in either directory is an orphan" (line 99), since v5 writes neither store. The pre-v5 stores named their archives <patch uuid>.tar.gz.

The four callers are repair.rs line 814, remove.rs line 1392, rollback.rs line 2798 and scan/gc.rs line 258.

The contract states the premise the sweep relies on: "socket-patch creates .socket/ and everything under it itself" (line 1576). That holds for a .socket/ directory. It does not hold for state/ or for the project root.

The same class was fixed twice already, each time for one site:

  • The lock guard removes an emptied .socket/ and is "gated to a directory literally named .socket: --manifest-path can point the lock at an arbitrary user directory, and the lock must never delete one of those just because it happened to be empty" (prune_empty_socket_dir, core patch/apply_lock.rs, line 315, since #247). The sweep runs under that same lock, in that same directory, with no such gate.
  • The sweep refuses an artifact directory that is a link: "socket-patch never creates a link there, so its target is someone else's tree, and every file in it would read as an orphan" (the comment above sweep, pinned by sweep_refuses_a_linked_artifact_dir_and_spares_its_target). A directory that is someone else's by its location was not covered.

Planned fix

One rule in ArtifactReferences::sweep: a pass deletes only what socket-patch can have written there.

  • When the artifact directory is not a directory named .socket, a pass removes only files that carry socket-patch's own artifact names: a 64-character lowercase hex name in blobs/, and <uuid>.tar.gz in diffs/ and packages/ (the names the pre-v5 stores used). Every other file is left and not counted. The directory is removed afterwards only when the pass itself emptied it.
  • A .socket/ directory keeps today's behavior, which its tests pin.

That changes no flag, default, exit code or JSON shape. The counts in gc and the Removed … lines get smaller for a relocated manifest, because the user's files are no longer in them. Tests: the unit test above, and a CLI test over repair, scan --prune, rollback and remove with a manifest at the project root beside a packages/ directory.

Not planned, because each changes a layout or refuses a documented one: refusing a manifest outside .socket/, or moving the artifact directory of such a manifest to a .socket/ beside it.

Seen beside this, and not part of it: rollback over an empty manifest at a non-default path says "no manifest at ./socket-patches.json" for a file that exists.


Generated by Claude Code

Langage dominant
Rust
Étoiles
9
Forks
2
Merge moyen
1 j 8 h
PR mergées (30 j)
313

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de SocketDev/socket-patch

Toutes les issues de SocketDev/socket-patch

Issues similaires

Plus d'issues Rust

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.