Skip to content

Report an unreadable vendor ledger instead of "No patch found" in remove and rollback - #2250

Merged
Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
arch-refactor/2249-unreadable-ledger-not-found
Nov 25, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 5 commits into
mainfrom
arch-refactor/2249-unreadable-ledger-not-found

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code

Fixes #2249.

Problem

A vendored project records its patches only in .socket/vendor/state.json: vendored mode keeps no manifest. When that file cannot be read, remove <target> and rollback <target> say the patch does not exist. A merge that leaves conflict markers in the ledger is enough:

$ socket-patch remove pkg:pypi/six@1.16.0
Error: No patch found matching identifier: pkg:pypi/six@1.16.0
$ socket-patch rollback pkg:pypi/six@1.16.0
Error: No patch found matching identifier: pkg:pypi/six@1.16.0
$ socket-patch report pkg:pypi/six@1.16.0 --dry-run
Error: Could not read the vendor ledger: corrupt ./.socket/vendor/state.json: key must be a string at line 4 column 1. To report a patch anyway, name it by its UUID

The lockfiles still install the vendored artifact, and the user has been told there is nothing to take off. report and a bare rollback already name the ledger, and CLI_CONTRACT.md defines not_found / patch_not_found as "no manifest entry, vendor-ledger entry or hosted pin matches", which cannot be said of a ledger that was never read.

Evidence on main (0a391c1)

a_target_is_never_not_found_while_the_vendor_ledger_is_unreadable in tests/manifest_load_error_matrix.rs gives one project to each command that takes a target: 9 commands over 3 projects (ledger only; beside an empty manifest; beside a manifest holding another package's patch). On main, report passes its 6 runs and remove / rollback fail their 21:

the vendor ledger could not be read, so these targets were never looked up in it:
  remove pkg:npm/x@1.0.0 --json --yes (manifest: none): not_found: No patch found matching identifier: pkg:npm/x@1.0.0
  remove x --json --yes --dry-run (manifest: none): not_found: No patch found matching identifier: x
  remove 11111111-1111-4111-8111-111111111111 --json --yes (manifest: none): not_found: No patch found matching identifier: 11111111-1111-4111-8111-111111111111
  rollback pkg:npm/x@1.0.0 --json --offline (manifest: none): patch_not_found: No patch found matching identifier: pkg:npm/x@1.0.0
  rollback x --json --offline --dry-run (manifest: none): patch_not_found: No patch found matching identifier: x (to target a directory instead, use ./x or x/**)
  rollback 11111111-1111-4111-8111-111111111111 --json --offline (manifest: none): patch_not_found: No patch found matching identifier: 11111111-1111-4111-8111-111111111111
  rollback ./node_modules/x --json --offline (manifest: none): path_glob_no_match: path pattern matched no patched packages: ./node_modules/x (path targets select installed copies; patches for uninstalled packages are reachable by identifier or an unscoped rollback)
  ... the same 7 lines for each of the other two projects

Review of the first commit found the same input mishandled in three more places. Each has a test that fails before its fix:

  • remove changed the install before refusing. With the target in the manifest, remove restored the patched file and then failed with vendor_state_unreadable, events: [] and rolledBack: 0. The contract says "a hard top-level error before any mutation". remove_corrupt_vendor_ledger_restores_no_installed_file failed with left: "original bytes\n", right: "patched bytes\n".
  • A ledger that cannot be stat'd read as no ledger. With no manifest and state.json a symlink to itself, remove x and rollback x returned manifest_not_found while report x returned vendor_state_unreadable (a_target_is_never_not_found_while_the_vendor_ledger_cannot_be_stat). The manifest's own probe was fixed the same way earlier (apply, apply --check and vendor report noManifest (exit 0) when .socket/manifest.json exists but can't be stat'd #998).
  • repair had the same probe and returned manifest_not_found (repair_reports_a_vendor_ledger_it_cannot_stat).

Fix

  • remove <target> and rollback <target> (an identifier or a path target) fail with vendor_state_unreadable when the target selects nothing in the stores that could be read and the ledger could not be. The message names the file and the target:
    Error: Cannot read .socket/vendor/state.json to look up pkg:pypi/six@1.16.0, which matches nothing else in the project: corrupt ./.socket/vendor/state.json: key must be a string at line 4 column 1
  • remove refuses an unreadable ledger before its in-place rollback, so nothing is restored first.
  • remove, rollback and repair treat only a missing ledger file as "no ledger".
  • One mapping, json_envelope::vendor_ledger_load_error, gives the code and message for remove and rollback, next to manifest_load_error.

Deleted

  • remove's "unreadable ledger degrades to nothing vendored" path: the Result no longer reaches the in-place rollback, the prompt count or the vendored leg.
  • The comment in a test that recorded the wrong behavior ("an unmatched identifier is not_found whatever the ledger holds"); remove joins that file's ledger-root matrix instead.

Behavior change

All exit codes stay 1. With an unreadable vendor ledger:

Run Before After
remove <target>, target in no readable store not_found, status: "notFound" vendor_state_unreadable, status: "error"
rollback <identifier>, same patch_not_found vendor_state_unreadable (the patched_projects_below note is kept, and so is the directory hint of a bare name, placed ahead of the ledger's own error)
rollback <path>, same path_glob_no_match vendor_state_unreadable
remove <target>, target in the manifest files restored, then vendor_state_unreadable vendor_state_unreadable, nothing restored
no manifest, ledger cannot be stat'd: remove, rollback, repair manifest_not_found the ledger's error. Where .socket/vendor/ itself cannot be entered, the apply lock's journal check fails first with lock_io, as it already does when a manifest is present

Unchanged: every "not found" code with a readable ledger (the control test pins them), a target the manifest or a hosted pin holds in rollback, a remove that only restores hosted pins, a bare rollback, and report.

CLI_CONTRACT.md: 7 rows updated to say the above.

Not fixed here

Risk

Low. The change is on error paths that already exit 1. The one run that now does less is remove with a corrupt ledger and a manifest match: it used to restore files before failing, and now restores nothing, which is what the contract promised. No cfg(windows) or cfg(unix) production code changed; the two stat tests are cfg(unix).

Verification

  • cargo clippy --locked -p socket-patch-cli --all-features -- -D warnings: clean.
  • 15 suites, 1,615 tests pass: the CLI unit tests, remove, rollback, report_e2e, cli_parse_remove, cli_parse_rollback, cli_parse_repair, cli_remove_silent, in_process_remove_repair_lifecycle, in_process_rollback_vendored, in_process_rollback_hosted, remove_rollback_api_overrides, manifest_load_error_matrix, manifest_path_ledger_root, and repair except the two below.
  • Fail in the root sandbox only (they rely on chmod, which root ignores; unrelated to this change): repair_invariants::repair_cleanup_failure_is_reported_in_json_and_silent_modes, repair_render::repair_exits_zero_and_stays_quiet_when_lock_file_unremovable, and in in_process_redirect: partial_lockfile_write_failure_exits_1_and_writes_no_ledger, redirect_json_mode_write_failures_emit_error_envelope, vlt::scan_redirect_vlt_heal_invalidation_failure_warns.
  • CI on 26a0f4a: all 83 checks pass. Bugbot found no issues.
  • /code-review high ran twice. Its 8 findings are fixed in the second and third commits, except the scan --prune probe above.
  • After approval, the review thread on rollback.rs asked for the directory hint to come before the ledger's error instead of after it, where it read as part of the parse error. 8579eb6 does that: ... to look up x, which matches nothing else in the project (to target a directory instead, use ./x or x/**): corrupt ./.socket/vendor/state.json: .... rollback_puts_the_directory_hint_ahead_of_the_ledger_error failed on 26a0f4a and passes now. An independent review of that commit found nothing to fix; the CLI unit tests and the 10 suites that cover remove, rollback and report pass (1,451 tests), and clippy is clean. CI on 8579eb6: all 51 checks pass, and Bugbot found no issues.

git diff --stat origin/main: production +150 −80 (4 files), tests +342 −3 (3 files), contract +7 −7.


Generated by Claude Code

Refs #2249.

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
A vendored project records its patches only in the vendor ledger. When
that file could not be parsed, remove <target> and rollback <target>
answered "No patch found matching identifier" for every target the
manifest and the hosted pins did not hold. The ledger may hold it, and
the lockfiles still install its artifact. report and a bare rollback
already named the ledger.

A target that selects nothing in the stores that could be read now
fails with vendor_state_unreadable in both commands, for an identifier
and for a rollback path target, in place of not_found, patch_not_found
and path_glob_no_match. The exit code stays 1. remove takes the error
it already raised before a vendored revert; the two share one mapping.

With every store readable the "not found" codes are unchanged, and so
is a target that the manifest or a hosted pin does hold.

Fixes #2249.

Assisted-by: Claude Code
Review of the previous commit found the same input mishandled in three
more places.

remove <target> for a manifest record restored the installed files in
place and only then failed with vendor_state_unreadable, reporting no
event and rolledBack 0. The contract says the refusal comes before any
mutation. It now comes before the in-place rollback, so nothing is
restored; the ledger no longer reaches that rollback as "nothing
vendored".

With no manifest, remove and rollback probed for the ledger with a stat
and took any error for absence, so a ledger that could not be stat'd (a
symlink loop, an unsearchable directory) was "No patches in this
project". Only a missing file is absence now, as for the manifest.

The error of an unmatched target names that target, so a run given
several can tell which one stopped it, and rollback keeps the note that
names the project directories below holding it.

Refs #2249.

Assisted-by: Claude Code
A second review of the branch.

repair probed for the vendor ledger with the same stat as remove and
rollback did: with no manifest, a ledger it could not stat was
manifest_not_found. Only a missing file is absence there too now, and
the run reports the ledger.

The contract said a ledger under an unsearchable .socket/vendor/ fails
as vendor_state_unreadable. It does not: the apply lock's journal check
reads that directory first and fails as lock_io, as it already did with
a manifest present. The contract and the comments say that now.

rollback <name> keeps its "to target a directory instead" hint when
the ledger is what failed. The contract's patched_projects_below row
covers an identifier, not a path target, which never carried the note.

Refs #2249.

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

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Changes are confined to error paths (still exit 1); the behavioral fix is refusing remove before mutation when the ledger is corrupt, which reduces accidental partial restores.

Overview
When .socket/vendor/state.json exists but cannot be read, remove and rollback no longer report not_found, patch_not_found, or path_glob_no_match for targets that only match in unreadable stores. They now fail with vendor_state_unreadable and name the target and ledger error—aligned with report and the contract that “not found” requires every store to have been read.

remove refuses an unreadable ledger before the in-place rollback, so patched install trees are not restored and then failed on.

remove, rollback, and repair treat only a missing ledger file as “no ledger”; paths that cannot be stat’d (e.g. symlink loop) are treated as unreadable ledger, not an empty project.

Shared messaging lives in json_envelope::vendor_ledger_load_error; CLI_CONTRACT.md and integration tests document and lock the behavior.

Reviewed by Cursor Bugbot for commit 8579eb6. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

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

Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 25, 2026
The second run under the real-problems charter: #2249 filed and fixed in
PR #2250, #1963 rejected with evidence and closed, L23 and L24 added.

Assisted-by: Claude Code
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
When .socket/vendor/state.json exists but can't be read (for example, merge conflict markers or a symlink loop), remove <target> and rollback <target> used to say "No patch found". In a vendored project the ledger is the only record of its patches, so that was wrong. Now they fail with vendor_state_unreadable and name the file. remove also refuses before it restores any file. Before, it restored files first and then failed. remove, rollback and repair now count only a missing ledger as "no ledger".

Risk: low. Only error paths change, and every exit code stays 1. One visible change: a remove --dry-run that matches a manifest entry now errors when the ledger is unreadable, where before it previewed as if nothing were vendored. This matches the contract ("a hard top-level error before any mutation").

Look here

  • remove.rs:751: a ledger-only lookup keeps the load error, and hosted-only pins are still tried before the refusal
  • remove.rs:807: the refusal moved ahead of the in-place rollback (it used to be at the vendored leg)
  • rollback.rs:2298 and rollback.rs:2374: identifier and path targets map to the ledger error
  • json_envelope.rs:674: the one shared mapping
  • [manifest_load_error_matrix.rs:390](https://github.com/SocketDev/socket-patch/blob/26a0f4ab97a18e71ea6649da3fa0818e6bb6618d/crates/socket-patch-cli/tests/manifest_load_error_matrix.rs#L390): 9 commands × 3 projects. The description shows it failing on main

Verified

  • Read the whole diff against main.
  • The new try_exists probes treat Err as "present". The old metadata().is_ok() treated it as "absent". That is the fix.
  • The vendor_state that moved up is the one used later by the dry-run count, the in-place exclusion and the vendored leg, so no path still reads the Result.
  • CHANGELOG.md is untouched.
  • CI on 26a0f4a: ci-ok and clippy are green, mergeable, no open review threads, and the Bugbot pass has no findings.

Changes I made: none.

Open questions: none.


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[agent] Reviewed the refusal order in remove, the missing-file probe and both rollback early returns; nothing is changed on disk before the refusal. One nit inline, not blocking.

A review comment on the branch: with the vendor ledger unreadable,
rollback <name> appended "(to target a directory instead, use ./x or
x/**)" after the ledger's own error, so the hint read as part of the
parse error:

  ... key must be a string at line 4 column 1 (to target a directory
  instead, use ./x or x/**)

The hint now ends the clause about the target, ahead of that error:

  cannot read .socket/vendor/state.json to look up x, which matches
  nothing else in the project (to target a directory instead, use ./x
  or x/**): corrupt ./.socket/vendor/state.json: key must be ...

patch_not_found reads as before, and a PURL or UUID target still
carries no hint. A test pins where the hint lands.

Refs #2249.

Assisted-by: Claude Code
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@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 8579eb6. Configure here.

Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 25, 2026
The fourth run answered the one open review thread on #2250, which
was holding an approved PR out of the merge queue: rollback's
directory hint now comes before the ledger's error. The PR is in the
queue.

Leads run on main at 9a2a946: L06 is filed as #2259 (the Hatch
uv-installer check reads environments hatch.toml replaces), L21 is
confirmed on #1910 (scan --prune drops the still-wired orphans), L16
and L20 are rejected, #1014 is closed, and #1950, L09 and L15 are out
of charter. The next-PR list is ordered by harm.

Assisted-by: Claude Code
Merged via the queue into main with commit ff949a3 Nov 25, 2026
51 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the arch-refactor/2249-unreadable-ledger-not-found branch November 25, 2026 21:30
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Nov 25, 2026
#2250 merged from the queue as ff949a3 a minute after the run's
entry was written, so it moves from In flight to Merged. No routine
PR is open; the next-PR list stands.

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

remove and rollback <target> answer "No patch found" when the vendor ledger cannot be read

4 participants