From 9ef2308f173d39e8603be0c9a6b0a85b79af9626 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 23:04:54 +0000 Subject: [PATCH 1/8] Start prune-unreadable-ledger Refs #2253. Assisted-by: Claude Code From 8f32a8a63cb5f576c64fb8d94cd4b6f704abfdc1 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 23:15:39 +0000 Subject: [PATCH 2/8] Report what scan --prune could not reconcile The vendored half of the prune returned an empty summary in two cases where the other commands speak up, so the scan reported a clean pass. A vendor ledger that could not be read (a merge conflict left in the committed state.json) read as an empty one. The pass now carries a vendor_state_unreadable warning, as rollback, list and apply do: no entry was reverted and no orphan dir swept, and the manifest half still runs. A ledger that cannot be stat'd no longer counts as absent at the pass's existence gate, which also reads the manifest's project instead of --cwd, and a scan whose crawl found nothing runs the vendored half for an unreadable ledger too. A vendored dir that no ledger entry owns and a project file still installs from was kept without a word, where vendor --revert, rollback and remove report vendor_orphan_still_wired. The pass reports it too, also when the ledger holds no entry at all, where it still deletes nothing. Its remedy leaves out "then revert again": a prune was not asked to revert, and the phrase reads as vendor --revert. The scan's status and exit code are unchanged. Refs #2253, #1910. Assisted-by: Claude Code --- crates/socket-patch-cli/CLI_CONTRACT.md | 5 +- .../socket-patch-cli/src/commands/scan/gc.rs | 226 +++++++++++++++++- .../socket-patch-cli/src/commands/scan/mod.rs | 7 +- .../socket-patch-cli/src/commands/vendor.rs | 103 +++++++- .../src/commands/vendor_orphans.rs | 16 +- .../scan/scan_prune_corrupt_vendor_ledger.rs | 73 +++++- 6 files changed, 398 insertions(+), 32 deletions(-) diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 10b3aecec..3624ee5de 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -406,7 +406,7 @@ The route is taken only when **all** of these hold; otherwise the run takes the **Agent-flow run-level warnings (additive).** An agent-mode apply (`--mode agent` / `--sync`, `--json`) may add a top-level `warnings[]` array of `{code, detail}` entries to the scan envelope (absent when none fired; each is also mirrored to stderr unless `--silent`). They surface cross-mode state the apply cannot change — never a status or exit-code change (hosted refusals set the precedent: exit 0 + warning). Codes (stable; new codes are additive/MINOR): `vendored_ownership_retained` — vendor-owned package(s) were skipped before download (each is also a `skipped` / `vendored` event); the detail names the purls and the migration path (`remove `, or `vendor --revert` which unwinds every vendored package, then re-run). `hosted_wiring_retained` — the lockfiles still pin scanned package(s) to a hosted patch (the agent run does not unwind hosted wiring — as of v5.0 that is `socket-patch rollback`'s job, which restores the upstream registry entries, or `remove ` per package); the detail names the purls and the options (stay `--mode hosted`, migrate via `scan --mode vendored`, or `socket-patch rollback`). The warning keys on the hosted pins lockfile discovery finds at scan time, so a flow that restored the upstream entries retires it. The human path prints the same `hosted_wiring_retained` text to stderr after an apply; the vendored counterpart is already covered by its per-package `[skip] … (vendored …)` lines. `ownership_not_restored` (v5.0; `apply` and `rollback` `warnings[]` alike) — a file WAS patched (or restored) but could not be given its original uid/gid (its mode is kept; the group is kept when the caller belongs to it); the detail is `: : patched, but ownership could not be restored to uid N gid M: ` and the human line `Warning: ` (stderr, muted by `--silent`); never a status or exit change. `go_installed_tool_stale` (v5.0; `apply`, dry runs included) — a `--global` Go apply patched a module cache copy that a tool installed with `go install` was built from before the patch; the detail names the binary and the `go install @` remedy (or, when they can't be inspected, what could not be checked); never a status or exit change. `stale_bytecode_not_removed` (`apply` and `rollback` `warnings[]` alike) — a `.py` file WAS patched (or restored) but CPython bytecode compiled from its previous bytes (`__pycache__/.[.opt-N].pyc`) could not be deleted, so Python may keep running the old code until it is removed; the detail is `: could not remove stale Python bytecode (: , …); …` and the human line `Warning: `; never a status or exit change. `patch_files_skipped` (`apply --force`) — a file of the patch is not on disk and was skipped; the detail is `: N patch file(s) not found on disk and skipped (--force): , …` and the human line `Warning: `. The package's event is `applied` with the files that were written, or, when the run wrote none and the files that exist are patched, `skipped` / `already_patched`; never a status or exit change. `cargo_build_cache_stale` (v5.0; `apply` and `rollback` `warnings[]` alike) — a cargo crate's bytes changed but its compiled copy in a project build directory could not be invalidated (a fingerprint directory that could not be removed, or a `build.build-dir` with a `{workspace-path-hash}` template); the detail names the crates to `cargo clean -p` before the next build, which may otherwise link the stale code; never a status or exit change. -`scan --prune` opts into garbage collection. When set, `scan` removes manifest entries for packages no longer present in the crawl, then deletes orphan blob files, and every obsolete diff and package archive, from `.socket/`. A blob is an orphan when no patch left in the manifest references it: the afterHash and beforeHash blobs of every remaining patch are kept, the same retention policy `repair` uses (the beforeHash blobs are an offline rollback's only restore data, and `repair` downloads afterHash blobs only). Diff and legacy package archives are never kept, even for a remaining patch: nothing reads them any more. Off by default (v3.0) so a temporary uninstall doesn't silently destroy manifest state. Under `--global`/`--global-prefix` the crawl covers only the global installs while the manifest is still `--cwd`'s, so the prune also keeps every entry the `--cwd` project accounts for (its installed packages and its lockfile-listed ones, exactly what the project's own `--prune` keeps): a global prune removes only entries neither scope has. Only entries whose ecosystem this run actually crawled are eligible: a `pkg:/` with no crawler in this build (a newer CLI's ecosystem in the committed manifest) is exempt — the crawl never looked for them, so their absence is not evidence of removal (same fail-safe as the `--ecosystems` filter, which narrows the query but never the prune's installed set). A Cargo entry is also exempt while its agent-mode copy is still patched: the project crawl looks up only the crates `Cargo.lock` resolves, but a crate the lock bumped or dropped keeps its patched copy in the machine-wide `$CARGO_HOME/registry/src` cache (nothing deletes it), and the entry holds the only blobs that can restore that copy. The wet pass keeps it with a `cargo_cache_patch_kept` warning naming the `socket-patch rollback ` that restores the copy and drops the entry; the `--dry-run` preview reports no event (and no warning) for it. A PyPI entry is exempt the same way: a project with no environment of its own is crawled only for the packages it declares, in the one interpreter it installs into, so a package it stopped declaring, or one an earlier release patched in another interpreter or a tool's environment, drops out of the crawl with its copy still patched. The copies `rollback ` restores are looked up by PURL, and for a project that installs into a shared interpreter so are the ones `rollback --global` restores (a project with an environment of its own never patched a copy outside it). The wet pass keeps an entry with a still-patched copy with a `pypi_installed_patch_kept` warning. A prune that an empty crawl skips drops no record, and reports the same warning for each recorded PyPI patch that is still on disk. The pass also reconciles vendored state (runs FIRST, under ONE apply-lock acquisition shared with the manifest prune — lock contention skips the whole pass without failing the scan; `--lock-timeout` is honored and a lock I/O error is reported rather than swallowed; the existence gate — a manifest file OR a vendor ledger file, both cheap stats; an emptied ledger is deleted on save, so its presence is its content proxy — runs BEFORE the lock, so a bare project never gets a `.socket/`; in the vendored scan arms the pass runs AFTER the vendor step): (a) ledger entries still tracked by a manifest record (manifest-mode entries written by standalone `vendor`) whose patch is gone from the manifest are reverted — `detached` entries (every `scan`/`get --mode vendored` entry, v5.0) have no manifest record to lose and are exempt from this leg; (b) EVERY ledger entry whose dependency is no longer in the lockfile graph is reverted and any manifest entry it still had dropped (v5.0: the check is about the lockfile, not the manifest, so embedded-record entries are no longer exempt; a missing or undeterminable lockfile keeps the entry, fail-safe); and (c) orphan `.socket/vendor//` dirs with no ledger entry are swept. The pass also takes out the entries of hosted mode's pnpm registry route that no lockfile uses any more (see "pnpm registry route", *Cleanup*): from pnpm-workspace.yaml and the pnpm locks alone, under the same lock, for a project that holds something of a route (which then passes the existence gate without a manifest or a ledger; the lock's `.socket/` is removed again when it stays empty); never under `--global`. A run whose crawl found nothing still does this half and the vendored one. The prune never deletes a zero-patch `.socket/manifest.json` (its `{"patches": {}}` + `setup` block stay). Under `--json` (v5.0, MAJOR) the pass is recorded into the scan envelope: each pruned manifest entry is a `removed` event with `details.manifest: true`, each reverted vendored entry a `removed` event (`errorCode: "vendor_reverted"`, `details.mode: "vendored"`), each kept entry a `skipped` / `vendor_revert_kept` event whose reason says why it was kept (a drift, or a project file that still installs from the artifact) and each entry whose revert failed a `skipped` / `vendor_revert_failed` event whose reason carries the revert's own (a GC revert failure never fails the run); a `--dry-run` preview reports the removals as `verified`. The artifact sweep is the envelope's `gc` (`summary.bytesFreed` mirrors it), the orphan vendor dirs swept are the top-level `removedVendorOrphanDirs` count, and each registry entry taken out is a `registry_entry_unused` event counted in `gc.removedRegistryEntries`. The pass's warnings join the top-level `warnings[]`: `vendor_state_write_failed` (entries were reverted but the ledger rewrite failed), `manifest_write_failed` (the pruned manifest could not be written: no entry was pruned and no blob, diff or package archive was swept, since the manifest on disk still references them), `cleanup_failed` (an orphan sweep failed mid-way, or `.socket/` or one of its `blobs/`, `diffs/`, `packages/` directories is a symlink, which is never swept), `cargo_cache_patch_kept` (see above), `registry_cleanup_skipped` / `registry_cleanup_failed` (the registry-route half could not read or write the pnpm files), the reinstall advisories of the vendored reverts (`vendor_bun_reinstall_required`, `vendor_vlt_reinstall_required`, `vendor_pypi_reinstall_required`), and `vendor_revert_residual_reference` for an entry kept because a project file still installs from its artifact: its detail starts with the entry's purl and names that file (a revert's other warnings, such as `vendor_lock_entry_removed` or a drift keep's, are not repeated). A pass skipped at the lock (`lock_held` | `lock_io`) has no `gc` and no events, only a `gc_skipped` warning whose detail ends with the code. Human mode prints `GC: skipped: .`, one `GC: .` line per warning, and `GC: failed to revert N vendored entries: …` (singular for one), each purl followed by the revert's reason in parentheses. A drift keep is an entry the revert deliberately preserved (`vendor_artifact_kept` — undo the drift and re-run `vendor --revert` to finish). A keep for a project file that still installs from the artifact is not a drift (#1184): the revert restored its recorded wiring, and the way out is to point the file named by `vendor_revert_residual_reference` back at the registry release (or re-export it from the restored lock) and prune again. The preview runs no revert, so it sees neither keep and its `verified` reverts may over-promise what a wet run will actually reclaim. +`scan --prune` opts into garbage collection. When set, `scan` removes manifest entries for packages no longer present in the crawl, then deletes orphan blob files, and every obsolete diff and package archive, from `.socket/`. A blob is an orphan when no patch left in the manifest references it: the afterHash and beforeHash blobs of every remaining patch are kept, the same retention policy `repair` uses (the beforeHash blobs are an offline rollback's only restore data, and `repair` downloads afterHash blobs only). Diff and legacy package archives are never kept, even for a remaining patch: nothing reads them any more. Off by default (v3.0) so a temporary uninstall doesn't silently destroy manifest state. Under `--global`/`--global-prefix` the crawl covers only the global installs while the manifest is still `--cwd`'s, so the prune also keeps every entry the `--cwd` project accounts for (its installed packages and its lockfile-listed ones, exactly what the project's own `--prune` keeps): a global prune removes only entries neither scope has. Only entries whose ecosystem this run actually crawled are eligible: a `pkg:/` with no crawler in this build (a newer CLI's ecosystem in the committed manifest) is exempt — the crawl never looked for them, so their absence is not evidence of removal (same fail-safe as the `--ecosystems` filter, which narrows the query but never the prune's installed set). A Cargo entry is also exempt while its agent-mode copy is still patched: the project crawl looks up only the crates `Cargo.lock` resolves, but a crate the lock bumped or dropped keeps its patched copy in the machine-wide `$CARGO_HOME/registry/src` cache (nothing deletes it), and the entry holds the only blobs that can restore that copy. The wet pass keeps it with a `cargo_cache_patch_kept` warning naming the `socket-patch rollback ` that restores the copy and drops the entry; the `--dry-run` preview reports no event (and no warning) for it. A PyPI entry is exempt the same way: a project with no environment of its own is crawled only for the packages it declares, in the one interpreter it installs into, so a package it stopped declaring, or one an earlier release patched in another interpreter or a tool's environment, drops out of the crawl with its copy still patched. The copies `rollback ` restores are looked up by PURL, and for a project that installs into a shared interpreter so are the ones `rollback --global` restores (a project with an environment of its own never patched a copy outside it). The wet pass keeps an entry with a still-patched copy with a `pypi_installed_patch_kept` warning. A prune that an empty crawl skips drops no record, and reports the same warning for each recorded PyPI patch that is still on disk. The pass also reconciles vendored state (runs FIRST, under ONE apply-lock acquisition shared with the manifest prune — lock contention skips the whole pass without failing the scan; `--lock-timeout` is honored and a lock I/O error is reported rather than swallowed; the existence gate — a manifest file OR a vendor ledger file, both cheap stats; an emptied ledger is deleted on save, so its presence is its content proxy; a ledger that cannot be stat'd is not absent, and the pass goes on to the read that reports it — runs BEFORE the lock, so a bare project never gets a `.socket/`; in the vendored scan arms the pass runs AFTER the vendor step): (a) ledger entries still tracked by a manifest record (manifest-mode entries written by standalone `vendor`) whose patch is gone from the manifest are reverted — `detached` entries (every `scan`/`get --mode vendored` entry, v5.0) have no manifest record to lose and are exempt from this leg; (b) EVERY ledger entry whose dependency is no longer in the lockfile graph is reverted and any manifest entry it still had dropped (v5.0: the check is about the lockfile, not the manifest, so embedded-record entries are no longer exempt; a missing or undeterminable lockfile keeps the entry, fail-safe); and (c) orphan `.socket/vendor//` dirs with no ledger entry are swept, except one a project file still installs from, which is kept with a `vendor_orphan_still_wired` warning (with no ledger entry at all the pass deletes no vendor dir, and only warns). A vendor ledger that cannot be read stops the vendored half before (a): nothing is reverted or swept, the pass carries a `vendor_state_unreadable` warning, and the manifest half still runs (the scan recovers the vendored purls it exempts from the committed artifacts). The pass also takes out the entries of hosted mode's pnpm registry route that no lockfile uses any more (see "pnpm registry route", *Cleanup*): from pnpm-workspace.yaml and the pnpm locks alone, under the same lock, for a project that holds something of a route (which then passes the existence gate without a manifest or a ledger; the lock's `.socket/` is removed again when it stays empty); never under `--global`. A run whose crawl found nothing still does this half and the vendored one (for the entries the lock no longer wires, and to report a ledger it could not read). The prune never deletes a zero-patch `.socket/manifest.json` (its `{"patches": {}}` + `setup` block stay). Under `--json` (v5.0, MAJOR) the pass is recorded into the scan envelope: each pruned manifest entry is a `removed` event with `details.manifest: true`, each reverted vendored entry a `removed` event (`errorCode: "vendor_reverted"`, `details.mode: "vendored"`), each kept entry a `skipped` / `vendor_revert_kept` event whose reason says why it was kept (a drift, or a project file that still installs from the artifact) and each entry whose revert failed a `skipped` / `vendor_revert_failed` event whose reason carries the revert's own (a GC revert failure never fails the run); a `--dry-run` preview reports the removals as `verified`. The artifact sweep is the envelope's `gc` (`summary.bytesFreed` mirrors it), the orphan vendor dirs swept are the top-level `removedVendorOrphanDirs` count, and each registry entry taken out is a `registry_entry_unused` event counted in `gc.removedRegistryEntries`. The pass's warnings join the top-level `warnings[]`: `vendor_state_unreadable` (the vendor ledger could not be read, so the vendored half did nothing; `status` and the exit code are the scan's own), `vendor_orphan_still_wired` (a vendor dir no ledger entry owns was kept because a project file still installs from it; the detail names the dir and the remedy, restoring the missing ledger entries from version control), `vendor_state_write_failed` (entries were reverted but the ledger rewrite failed), `manifest_write_failed` (the pruned manifest could not be written: no entry was pruned and no blob, diff or package archive was swept, since the manifest on disk still references them), `cleanup_failed` (an orphan sweep failed mid-way, or `.socket/` or one of its `blobs/`, `diffs/`, `packages/` directories is a symlink, which is never swept), `cargo_cache_patch_kept` (see above), `registry_cleanup_skipped` / `registry_cleanup_failed` (the registry-route half could not read or write the pnpm files), the reinstall advisories of the vendored reverts (`vendor_bun_reinstall_required`, `vendor_vlt_reinstall_required`, `vendor_pypi_reinstall_required`), and `vendor_revert_residual_reference` for an entry kept because a project file still installs from its artifact: its detail starts with the entry's purl and names that file (a revert's other warnings, such as `vendor_lock_entry_removed` or a drift keep's, are not repeated). A pass skipped at the lock (`lock_held` | `lock_io`) has no `gc` and no events, only a `gc_skipped` warning whose detail ends with the code. Human mode prints `GC: skipped: .`, one `GC: .` line per warning, and `GC: failed to revert N vendored entries: …` (singular for one), each purl followed by the revert's reason in parentheses. A drift keep is an entry the revert deliberately preserved (`vendor_artifact_kept` — undo the drift and re-run `vendor --revert` to finish). A keep for a project file that still installs from the artifact is not a drift (#1184): the revert restored its recorded wiring, and the way out is to point the file named by `vendor_revert_residual_reference` back at the registry release (or re-export it from the restored lock) and prune again. The preview runs no revert, so it sees neither keep and its `verified` reverts may over-promise what a wet run will actually reclaim. `scan` queries the patch API in `--batch-size` chunks. Authenticated runs POST `/v0/orgs/{slug}/patches/batch`; token-less runs POST `{proxy}/patch/batch` on the public proxy and degrade to per-package `GET /patch/by-package/:purl` requests in two cases: the deployed proxy predates the batch endpoint (legacy proxies answer the POST with their `400 "Unsupported endpoint"` catch-all), or the all-or-nothing batch validation rejects the chunk (e.g. a crawled PURL type the server doesn't recognize, such as `pkg:jsr/…` — the per-package path tolerates those individually, preserving the pre-batch scan semantics). Rate limits and over-capacity 503s surface instead of silently degrading. @@ -2498,7 +2498,7 @@ command's integration tests check its output. | `vendor_commit_recovered` / `vendor_commit_set_aside` | `warnings[]` of the lock-taking command that found the journal (`apply`, `rollback`, `remove`, `repair`, `vendor`, `scan`, `get`) | group commit recovery: taking the apply lock found an interrupted run's `.socket/vendor/.commit-journal.json`. `vendor_commit_recovered`: the commit was finished, and the detail names every file rolled forward (none when they all already held their new bytes and only the journal was removed). `vendor_commit_set_aside`: the journal could not be replayed as written (a file edited since, an unreadable journal, a path it must not write) and was renamed aside; the detail says what was done instead and where the journal went. Never changes the status or exit code. The same sentence is a `Warning:` line on stderr in every output mode. | | `redirect_symlinked_file_unsupported` (vendored) | top-level error (`vendor`, and the nested vendor envelope of `scan` / `get --mode vendored`) | v5.0 group commit (#627): a file the run would rewrite — a lockfile, `package.json`, `pnpm-workspace.yaml`, `nuget.config`, … — is a symbolic link. The commit stages each file and renames it over the path, which would replace the link with a detached copy and leave its target (the lock other checkouts read) unpatched, so it refuses before writing anything — the same code and message as the hosted guard. Exit 1; the link, its target and `.socket/vendor/state.json` are left as they were, the artifact dirs the run added under `.socket/vendor/` are removed (any that cannot be are named in the message, with `vendor --revert` as the remedy), and each package the run vendored is a `failed` event with this code, not `applied` (`summary.applied` does not count it); the `skipped` advisories the run recorded for it (`vendor_prebuilt_downloaded`, `vendor_artifact_reused`, …) are dropped, since they describe the vendoring that was refused. The human run prints no "Vendored N packages" count for them and no "Next steps:" (#898). Backends that check their own targets first (Hatch, uv, Poetry, PDM, Pipenv, requirements, PEP 751 locks, Cargo) refuse per package with this same code (see the per-package row below); a symlinked `bun.lockb` keeps `vendor_bun_lockb_invalid`. | | `vendor_would_refuse_symlinked_file` | `skipped` (advisory event) under `vendor --dry-run`; a top-level `warnings[]` entry (detail prefixed `: `) for each package the `scan` / `get --mode vendored --dry-run` preview would vendor (human: an `[warning] (): ` line) | dry run (#627): a dry run captures no writes, so for each package it would vendor (not one previewed as in sync, whose re-run writes nothing) it names every symlinked file of that package's ecosystem a vendored run may rewrite (the registry's vendored rewrite targets plus `pnpm-workspace.yaml`, `nuget.config`, `packages.lock.json`, the root `pom.xml`, `.mvn/maven.config` and `hatch.toml`; for PyPI also every variable-name lock in the project root — `pylock.toml`, `pylock..toml`, a PEP 723 `