Skip to content

Report an unreadable vendor ledger and a still-wired orphan in scan --prune - #2261

Open
Mikola Lysenko (mikolalysenko) wants to merge 8 commits into
mainfrom
arch-refactor/2253-prune-unreadable-ledger
Open

Mikola Lysenko (mikolalysenko) wants to merge 8 commits into
mainfrom
arch-refactor/2253-prune-unreadable-ledger

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code

Fixes #2253. Takes the still-wired orphan bullet of #1910 (that issue stays open for its Go undo warning).

Problem

scan --prune reconciles vendored state: it reverts entries the project no longer uses and sweeps vendor directories nothing owns. In two states it did none of that and reported a clean pass, so a CI job that runs it stays green while the project stays unreconciled:

  1. The vendor ledger cannot be read. A merge conflict left in the committed .socket/vendor/state.json is enough. rollback, list, apply and vendor --check all name the ledger on the same project.
  2. A lockfile installs from a vendored directory that no ledger entry owns (a merge that kept one side of state.json). vendor --revert, rollback and remove report vendor_orphan_still_wired or vendor_ledger_missing. The prune pass ran the same sweep and dropped that part of its result.

Plain vendor with no manifest had the first gap under --json only: human mode warned on stderr, and the envelope was a bare noManifest.

Evidence on main (cfc870f)

The built binary, against a local stand-in for the patch API that answers "no patches":

$ printf '{\n<<<<<<< HEAD\n' > .socket/vendor/state.json
$ socket-patch scan --prune --json     # status, warnings, removedVendorOrphanDirs
{"status": "success", "removedVendorOrphanDirs": 0}
$ socket-patch list --json
{"status": "success", "warnings": [{"code": "vendor_state_unreadable", "detail": "unreadable vendor ledger (corrupt ./.socket/vendor/state.json: key must be a string at line 2 column 1); its vendored patches are not listed"}]}
$ socket-patch rollback --dry-run --json --offline      # exit 1
{"status": "partialFailure", "warnings": [{"code": "vendor_state_unreadable", …}, {"code": "gc_skipped", …}]}
$ socket-patch vendor --json           # no manifest; human mode prints a warning
{"status": "noManifest"}
$ cat requirements.txt                 # no ledger entry owns this directory
./.socket/vendor/pypi/1a2b3c4d-5e6f-4a1b-8c2d-9e0f1a2b3c4d/six-1.16.0-py2.py3-none-any.whl
$ socket-patch scan --prune --json
{"status": "success", "removedVendorOrphanDirs": 0}
$ socket-patch vendor --revert --dry-run --json
{"status": "success", "events": [{"action": "skipped", "purl": "pkg:pypi/six@1.16.0", "errorCode": "vendor_orphan_still_wired", "reason": "a project lockfile still points at .socket/vendor/pypi/1a2b3c4d-…, which no ledger entry owns; the artifacts were kept (…)"}]}

The tests this PR adds fail on main's production code. Unit tests in scan/gc.rs (cargo test -p socket-patch-cli --lib -- scan::gc::tests::gc_):

test commands::scan::gc::tests::gc_takes_only_a_missing_ledger_for_no_ledger ... FAILED
test commands::scan::gc::tests::gc_reports_a_vendor_ledger_it_could_not_read ... FAILED
test commands::scan::gc::tests::gc_reports_an_orphan_dir_a_project_file_still_installs_from ... FAILED
  dry_run=true: the vendored half did not run, and the pass must say why: {"command":"scan","status":"success",…}
  left: []
 right: ["vendor_state_unreadable"]
test result: FAILED. 1 passed; 3 failed

CLI tests, through the built binary (--test scan -- scan_prune_corrupt_vendor_ledger, --test vendor -- missing_manifest_with_an_unreadable):

test scan_prune_corrupt_vendor_ledger::corrupt_ledger_scan_prune_of_an_empty_crawl_names_the_ledger ... FAILED
test scan_prune_corrupt_vendor_ledger::corrupt_ledger_scan_prune_keeps_vendored_manifest_entry ... FAILED
  the pass could not read the ledger and must say so; warnings=null
test vendor_render::missing_manifest_with_an_unreadable_ledger_says_so_in_both_modes ... FAILED

(Two of these were renamed in review: the first unit test is gc_takes_only_a_missing_vendor_dir_for_nothing_vendored on this branch, and the first CLI test is scan_prune_of_an_empty_crawl_says_what_it_could_not_reconcile.)

Fix

  • run_vendor_gc returned an empty summary for a ledger that failed to load (_ => return out). It now returns a vendor_state_unreadable warning: nothing is reverted or swept, and the manifest half of the prune still runs, as before.
  • The still-wired set of the orphan sweep becomes vendor_orphan_still_wired warnings, also when the ledger holds no entry at all. That case still deletes nothing: it only reports.
  • The pass's existence gate took any stat error on the ledger for "no ledger" (the probe Report an unreadable vendor ledger instead of "No patch found" in remove and rollback #2250 fixed in remove, rollback and repair), and read --cwd where the pass reads the manifest's project. It now asks whether .socket/vendor/ of the manifest's project is known to be missing. A wet pass therefore reports a still-wired directory in a project with no manifest and no ledger, as --dry-run does.
  • An empty crawl runs the vendored half only for ledger entries the lock no longer wires, and a lost or unreadable ledger names none. That path now reports the same two warnings read-only (no lock, nothing written), the way it already reports a recorded PyPI patch that is still on disk.
  • vendor --json with no manifest carries the warning human mode prints.

One test repair rides along: corrupt_ledger_scan_prune_keeps_vendored_manifest_entry checked "not pruned" by reading gc.prunedManifestEntries, a key the envelope does not have, so that assertion could not fail. It now reads the events. The test's on-disk checks were sound.

After the fix:

$ socket-patch scan --prune --json
{"status": "success", "warnings": [{"code": "vendor_state_unreadable", "detail": "cannot read .socket/vendor/state.json: corrupt ./.socket/vendor/state.json: key must be a string at line 2 column 1; the prune did not check the vendored entries or sweep orphan vendor dirs (restore the ledger from version control)"}], "removedVendorOrphanDirs": 0}
$ socket-patch scan --prune
No patches available for installed packages.
GC: cannot read .socket/vendor/state.json: corrupt ./.socket/vendor/state.json: key must be a string at line 2 column 1; the prune did not check the vendored entries or sweep orphan vendor dirs (restore the ledger from version control).
$ socket-patch scan --prune --json     # the requirements project
{"status": "success", "warnings": [{"code": "vendor_orphan_still_wired", "detail": "a project lockfile still points at .socket/vendor/pypi/1a2b3c4d-…, which no ledger entry owns; the artifacts were kept (restore the missing entries in .socket/vendor/state.json from version control (if a git merge kept one side of state.json, merge both sides' `entries`), or point the lockfile back at the registry release and delete the directory)"}], "removedVendorOrphanDirs": 0}
$ socket-patch vendor --json
{"status": "noManifest", "warnings": [{"code": "vendor_state_unreadable", "detail": "no manifest to vendor from, and the vendor ledger could not be read: …; restore .socket/vendor/state.json from version control"}]}

Deleted

The catch-all arm that turned a ledger load error into an empty summary, and the has_ledger stat probe. No copy was left behind.

Behavior change

All additive, and no exit code or status changes:

  • scan --prune gains two entries in warnings[] (human: GC: … lines, or Warning: lines on stderr when the crawl found nothing), in the two states above. Both codes already exist on other commands.
  • vendor --json with no manifest gains a warnings[] entry for an unreadable ledger.
  • Plain vendor's human warning for that state ended with "Run socket-patch repair to check the vendored artifacts". repair stops at the same ledger (Error: Could not read the vendor ledger, exit 1), so it now says "Restore .socket/vendor/state.json from version control", and strips control characters from the ledger's error.
  • A wet prune of a project with no manifest and no ledger file, but a .socket/vendor/ directory, now takes the apply lock and runs the vendored half, where it returned at the gate. With nothing to report it prints what it printed before.
  • The prune's still-wired text leaves out the "then revert again" of the reverting commands: a prune was not asked to revert, and the phrase reads as vendor --revert, which un-vendors the project.

CLI_CONTRACT.md describes all of this: the scan --prune paragraph, the vendor_state_unreadable row, and a new row for vendor_orphan_still_wired, which three commands emitted and the contract never listed.

Risk

Low. Nothing new is deleted or reverted: every new branch only reports. The one path that does more work than before is the gate (a lock taken where the pass used to return), and run_apply_gc_creates_no_socket_dir_on_a_pristine_project still holds. The existing test apply_gc_says_why_a_vendored_revert_failed gained a second expected line: its fixture hand-edits a ledger entry's ecosystem, which disowns that entry's directory.

No cfg(unix) or cfg(windows) production code changed. One new test is #[cfg(unix)] (it needs a symlink loop).

Review

/code-review high ran three times. The first pass found the wet-versus-dry gap at the gate, the repair remedy and an unsanitized error; the second found the incomplete "point the lockfile back" remedy, the unsanitized human vendor line and a gate test that no longer covered a failed stat. The third, after Bugbot's fix, found that a pass holding no apply lock (a --dry-run, or the empty crawl's read-only report) would call a directory deletable while an interrupted vendored run's commit is pending: that commit replaces the lockfiles before the ledger. Such a pass now reports no still-wired directory while a commit journal is pending; a locked pass has finished the commit before it looks. All are fixed, and the gate test fails with the gate's error arm inverted.

Bugbot found one more (medium): on an empty crawl the still-wired report never ran when the ledger was simply missing. It reproduces: a package-lock.json that resolves from an orphan vendored tarball, with nothing installed, scans as zero packages. Fixed in 587d443, and the thread is answered and resolved.

Three findings are left as they are:

  • When the wet pass cannot take the apply lock it reports gc_skipped and not the new warnings. A skipped pass says it was skipped, and the next run reports.
  • With .socket/vendor a plain file and no manifest, --dry-run reports vendor_state_unreadable and the wet run gc_skipped (lock_io, from the lock's own check of that directory). Both name the path. The contract already gives lock_io for a .socket/vendor/ that cannot be entered on remove, rollback and repair; the prune's preview takes no lock, so it has no such check.
  • On an empty crawl with an unreadable ledger, human mode prints the new Warning: line and then the existing "--prune skipped: no installed packages were found … run socket-patch repair" line. The second line is about the manifest half, which the empty crawl did skip, and its remedy holds once the ledger is restored, which the first line asks for.

Verification

  • CI on d5a77284: 48 of 50 checks pass. hosted-e2e and its aggregate ci-ok fail because the production patch service answers HTTP 500 to hosted artifact downloads; the comment below has the evidence. It needs a re-run once the service recovers.
  • cargo clippy --locked -p socket-patch-cli --all-features -- -D warnings: clean.
  • cargo test -p socket-patch-cli --lib: 1081 passed.
  • cli (125), the suite with the test-tree ratchets, passes. CI's first run on this PR failed coverage (1): the scan suite's new test defined its own warning_codes, which the ratchet in tests/cli/envelope_helper_copies.rs forbids. It now uses common::envelope's reader.
  • Suites scan (268), vendor (146), in_process_scan (31), scan_vendor_e2e (43), scan_vendor_requirements_unwired (4), cli_scan_silent (12), in_process_rollback_vendored (27), manifest_load_error_matrix (10), cli_remove_silent (10): all passed.

Diff

7 files, +647 −43: production +163 −26 (four files under src/commands/), tests +481 −15 (unit +305 −7, CLI suites +176 −8), contract +3 −2.

CLI_CONTRACT.md is also changed by #2258, one line in the gem section about 100 lines from the nearest hunk here, so the two merge cleanly in either order.


Generated by Claude Code

Refs #2253.

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 Nov 25, 2026
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
Plain vendor with no manifest and a vendor ledger it could not read
warned on stderr in human mode and printed a bare noManifest envelope
under --json, the shape of a project with nothing vendored. The
envelope now carries the same finding as a vendor_state_unreadable
warning. The run is still the exit-0 no-op.

Fixes #2253.

Assisted-by: Claude Code
A wet prune of a project with neither a manifest nor a ledger file
stopped at its existence gate, so a vendored dir a lockfile still
installs from was reported by --dry-run and not by the run itself. The
gate now asks for the ledger's directory, .socket/vendor/, which the
artifacts share, and the wet pass reports what the preview does.

The still-wired warning of a prune names a second way out, pointing
the lockfile back at the registry release: the ledger need not have
lost an entry.

The unreadable-ledger warning strips control characters from the
ledger's error, which a human run prints.

Plain vendor with no manifest and an unreadable ledger pointed at
repair, which stops at the same ledger. Both modes now say to restore
the file from version control.

Refs #2253.

Assisted-by: Claude Code
From the second review.

The still-wired warning of a prune ends its second way out with
deleting the directory: with no ledger entry to judge ownership by the
prune never deletes one, so pointing the lockfile at the registry
alone left a committed directory nothing would mention again.

Plain vendor's human warning for an unreadable ledger strips control
characters from the ledger's error, as the prune's warning does.

The gate test now loops .socket/vendor/ itself as well as the ledger,
so the "cannot be stat'd" arm of the gate is covered: reading that arm
as "nothing vendored" fails the test.

Refs #2253.

Assisted-by: Claude Code
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 23:49
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Additive reporting only; exit codes unchanged. The main behavioral expansion is wet prune taking the apply lock when .socket/vendor/ exists without a ledger file, with no new deletes when there is nothing to reconcile.

Overview
scan --prune no longer looks like a clean pass when vendored state cannot be reconciled. If .socket/vendor/state.json is unreadable, the vendored GC half stops before any revert or orphan sweep, emits vendor_state_unreadable (warnings[] / human GC: lines), and the manifest half still runs as before. If a lockfile still installs from a .socket/vendor/<eco>/<uuid>/ dir with no ledger owner, the sweep keeps that directory and reports vendor_orphan_still_wired (prune-specific remedy text: restore ledger or repoint the lockfile and delete the dir—no “revert again”).

The prune existence gate now treats .socket/vendor/ (not only the ledger file) as “may have vendored state,” so projects without a manifest or ledger still enter the pass when artifacts or wiring remain. Empty crawls add a read-only path that surfaces the same unreconciled warnings via unreconciled_vendor_warnings when there is nothing to revert from the ledger.

Plain vendor with no manifest now puts the unreadable-ledger case in --json as vendor_state_unreadable (stderr already warned). The human hint switches from repair to restore state.json from version control, with sanitized error text.

CLI_CONTRACT.md documents prune behavior, extends the vendor_state_unreadable row, and adds vendor_orphan_still_wired for prune/rollback.

Reviewed by Cursor Bugbot for commit d5a7728. 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.

Stale Bugbot comment from a previous run.

The corrupt-ledger scan suite defined its own warning_codes, which the
ratchet in tests/cli/envelope_helper_copies.rs forbids: CI's coverage
job failed on it. The suite now uses common::envelope's reader.

Also separates two unrelated comment paragraphs in vendor's no-manifest
branch.

Refs #2253.

Assisted-by: Claude Code
From Bugbot. A scan whose crawl finds nothing runs the vendored half
of the prune only for ledger entries the lock no longer wires. A lost
ledger names none, so a lockfile that installs from a vendored dir no
entry owns went unreported there: with nothing installed, a
package-lock file: entry is no package of the crawl.

That path now reports what the half could not reconcile, read-only, as
it already does for a recorded PyPI patch still on disk: no lock is
taken and nothing is written. The unreadable ledger takes the same
route, in place of running the half for it, so both are a Warning line
on stderr in human mode and a warnings[] entry under --json.

Refs #2253.

Assisted-by: Claude Code
From the third review. A vendored run's group commit replaces the
lockfiles before the ledger. If it is interrupted, a lockfile is ahead
of its ledger entry until the next run that takes the apply lock
finishes the commit from its journal. A prune that holds no lock (a
--dry-run, or the read-only report of an empty crawl) read that state
as a lost ledger entry and said the directory could be deleted.

Such a pass now reports no still-wired directory while a commit journal
is pending, or cannot be checked. A locked pass has finished the commit
before it looks, and reports as before.

Also: the vendor suite's new test reads the envelope through
common::envelope, and the contract says when the empty-crawl warning is
a stderr line.

Refs #2253.

Assisted-by: Claude Code
@mikolalysenko

Mikola Lysenko (mikolalysenko) commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

[agent] hosted-e2e (and ci-ok, which aggregates it) is red on this head, and the cause is not in this PR. The other 48 checks pass.

What fails. Nine tests of e2e_hosted_production (npm_package_lock, pnpm, yarn_classic, yarn_berry, bun, vlt, pypi_uv_lock, pypi_requirements_txt, gem_bundler): each pins a hosted patch from the production patch service and installs it with the real package manager, and the service answers the artifact downloads with HTTP 500:

npm error 500 Internal Server Error - GET https://patch.socket.dev/patch/npm/minimist/1.2.2/***/***/minimist-1.2.2.tgz - internal_error
"reason": "cannot fetch the hosted tarball: vendor package download failed with status 500: internal_error"

Why it isn't this PR's.

  • The diff changes scan --prune's vendored GC and plain vendor's no-manifest branch. It touches no hosted-mode code, and none of these tests runs either path.
  • The job passed on main (cfc870f, this branch's base) at 22:40Z. It has not passed on this branch: it is skipped on a draft's runs and was cancelled on the first ready run, so these two are its only results here.
  • It failed the same way on two heads in two separate runs (587d443b at 00:09Z, d5a77284 at 00:31Z), so it is not a one-off. Every failure traces to that 500: seven tests fail in the package manager's download, and two (pypi_uv_lock, yarn_classic) because the scan's own fetch of the hosted artifact got it and skipped the package (npm_tarball_unavailable, python_metadata_unavailable: "download failed with status 500: internal_error").

Nothing in this repository can fix it. The job needs a re-run once the patch service recovers; I have not re-run it, since a third run against the same outage would only fail again.


Generated by Claude Code

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 26, 2026

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit d5a7728. Configure here.

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 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.

scan --prune reports a clean pass over a vendor ledger it could not read

2 participants