Skip to content

Skip a stale patch instead of failing apply - #2515

Open
Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
fix/stale-patch-skip
Open

Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
fix/stale-patch-skip

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

What changes for users

A manifest record is for one version of a package. When a project moved to another version and nothing pruned the record, every socket-patch apply exited 1 with "The targeted manifest patch matched no installed package", so the install hook failed on every install. Nothing is wrong with the installed tree in that state: the record has nothing left to patch.

Such a record is now a stale patch, a skip in a category of its own:

Warning: 1 manifest patch is stale (the project uses another version of the package; skipped):
  - pkg:npm/widget@1.0.0 (the project lockfile resolves 2.0.0)
Run `socket-patch scan --prune` to remove it from the manifest.

Summary: 0 of 1 targeted patch applied, 0 already patched, 1 stale, 0 not found on disk
  • apply skips it and warns. The warning names the version the project uses and the command that removes the record, with the run's own --cwd, --manifest-path, -g and --ecosystems so it can be pasted. The summary has a stale bucket, shown only when non-zero, in place of counting the record as "not found on disk".
  • apply --json: a skipped event with errorCode: "patch_stale". Its reason names both versions and the remedy.
  • apply --check says 1 patch stale, skipped in place of 1 patch not installed, skipped, prints the same warning, and reports the same event under --json. Still exit 0.
  • scan without --prune names stale records and the remedy where it used to say only "No patches available for installed packages". Under --json it is a patch_stale entry in warnings[]. A scan that prunes removes the records and says nothing about them.
  • get runs apply over the manifest after it saves its patch. A stale record beside the fetched patch is named once (a patch_stale warning under --json). A patch fetched for a version the project moved off is saved, reported as its own skipped / patch_stale event, and the run exits 0. Before, that get exited 1 with package_not_installed.
  • --silent hides the notice everywhere, like the lockfile-only note. It is a warning, never an error.

A record scan --prune keeps

scan --prune keeps a Cargo or PyPI record while a copy of the record's version is still patched on the machine: a crate in the shared registry cache, a Python package in another interpreter or a tool's environment. The record holds what restores that copy. The prune is no remedy for such a stale record, so the warning, the event's reason and the run warning name the rollback that restores the copy and removes the record, with -g when the copy is one only a global rollback reaches:

Warning: 1 manifest patch is stale (the project uses another version of the package; skipped):
  - pkg:cargo/gadget@1.0.0 (the project lockfile resolves 1.1.0)
A copy of pkg:cargo/gadget@1.0.0 on this machine is still patched: run `socket-patch rollback pkg:cargo/gadget@1.0.0` to restore it and remove the patch from the manifest.

The rule is the one the prune keeps the record by, read from the same copies. When other stale records can be pruned, a last line says Run socket-patch scan --prune to remove the rest from the manifest.

When a record is stale

Only on evidence: the record has no installed copy, no lockfile resolves its version, and the project's lockfiles or its installed tree hold the same package at another version. Packages are compared as canonical purls, so a name's spelling, a qualifier or an equal PEP 440 spelling is not another version. The lockfiles are asked first. The installed tree is crawled only for the records no lockfile names, and only for npm, PyPI and Composer, whose packages are installed in a tree of the project's own. A gem home, the Cargo registry, the Go module cache, a Maven or Gradle repository and the NuGet packages folder are shared by every project on the machine, so another version found there is not evidence. There, only a lockfile is, which leaves Maven and NuGet records never stale (they have no lockfile view). A -g run has no project to ask and reports no stale patch.

A record for a package the project shows no sign of is not stale. It stays package_not_installed.

Exit code change

  • A run whose unmatched records are all stale now exits 0 (was 1). One exception, below: apply --vex <path> when nothing was applied. That holds for a manifest with one stale record and for a stale record beside patches that applied.
  • A run in which no patch matched and some record has no evidence still exits 1 with the same error, which now lists only those records. This is the wrong---cwd guard and it is unchanged.

Telemetry

  • A stale patch is reported per patch as {outcome: "stale", code: "patch_stale"} and counted under outcomes.stale. It is never in failure_codes and never makes the event patch_apply_failed.
  • A run that succeeds now also reports the records no installed copy matched (stale, or skipped / package_not_installed for a lockfile-only one or one that is not installed beside a patch that applied). Before, such records reached telemetry only through the failures of a failed run, so a successful run left them out.
  • The apply that get and scan --mode agent run reports the same outcomes under the caller's command, once. Their --json envelope carries a patch_stale warning for a stale record the run did not select.

Tests

  • Unit: the classifier (another version, no evidence, the record's own version, spelling), the reason, notice and remedy text, the summary and --check lines, the telemetry mapping, and reported_patches for a successful run. That test was not written before the code, as it was meant to be: the apply wiring went in first and the test followed in the same commit.
  • tests/apply/stale_patch_skip.rs: a stale record alone (lockfile evidence and installed-tree evidence), --silent, beside a live patch, beside a record with no evidence (still exit 1), a scoped remedy, --check in text and --json, and a Cargo record with its old cache copy still patched (names rollback; the test runs it as printed and checks the copy and the manifest), pristine, and absent (names the prune).
  • tests/scan/scan_stale_patches.rs: the notice, the --json warning, --prune removing the record, and agent and --sync runs naming it once or not at all.
  • tests/cli/telemetry_e2e.rs: the event of a stale-only apply, and of an agent scan with a stale record.
  • tests/e2e_pypi_multi_copy.rs: a stale PyPI record whose old copy is still patched in a tool's environment names rollback <purl> -g, and running it as printed restores the copy and removes the record.
  • tests/get/get_stale_patches.rs: a stale record beside the fetched patch is named once, and a fetched patch that is itself stale is one skipped event, no warning, exit 0.
  • tests/apply/in_process_gem_fallback_home.rs: a stale gem record beside a record for another project's gem no longer fails the run.
  • I also ran the remedy on a stale project: scan --prune, and scan --prune --cwd <dir> --ecosystems npm, each removed the record without --yes, and the next apply had nothing to skip.

Ran: cargo test -p socket-patch-cli --lib (1120 pass), the apply (251), get (169), cli (127) and scan suites, the in-process get and scan suites, e2e_cargo, e2e_pypi_multi_copy, and the remedy, help-text and layering hygiene suites. One scan test fails on my machine, quoted_url_credentials::a_hosted_warning_quotes_a_lock_entry_without_its_token (280 pass, 1 fail): its hosted PyPI case stops at "No packages found: the 11 packages installed in the Python on PATH are not ones this project declares". It fails the same way on the base commit of this branch, so it is this machine's Python, not this change.

Not covered by an integration test: the second all-miss branch of apply (a copy was found, no record matched) with only stale records left. I found no small fixture that reaches it with nothing else failing; the gem fixture above turned out to take the first branch. The second branch shares its three lines of stale handling with the mixed-run test, which fails when that handling is disabled.

Code review (/code-review high)

Fixed:

  • A needed patch could be called stale from a shared store. Another version in a machine-wide cache made a record stale, so a project whose own version was not fetched yet got exit 0 and advice to prune a record it needs. Installed evidence now counts only for npm, PyPI and Composer; elsewhere a lockfile is required. scan follows the same rule. Test: a Cargo project with no lockfile and another version in the registry cache still exits 1.
  • Global runs reported the project's own records as stale, with a remedy that does not remove them. A global run now reports no stale patch, in apply, apply --check and scan.
  • scan --json added its stale warnings all or none. They are now added per record, and a scan that vendors or redirects something names the stale records too (text and --json).

Not fixed:

  • apply --vex <path> with nothing applied still exits 1 (no_applicable_patches), stale records or not. That is how every run that applied nothing behaves today, such as one whose records are all lockfile-only. Whether such a run should skip the attestation or write an empty one is a decision about --vex, not about stale patches. The contract row names the exception.
  • A stale record with no local blob still fails apply --offline, and an online run still downloads the blob. The source check runs over every in-scope record before any package is looked up, as it did before this change for a record that is not installed. Moving stale detection ahead of it means reordering apply, and it overlaps separate work on where patch data is read from.
  • Detection costs a second read of the lockfiles and, for npm, PyPI and Composer, a crawl, on runs that have an unmatched record no lockfile resolves. Such a run already warns or fails, and the crawl is now limited to three ecosystems. A by-name lookup would be the cheaper form; no crawler offers one yet.

Checked by hand

On a project with one recorded patch after the package moved to another version: the 5.2.0 release exits 1 ("not found on disk"); this build warns and exits 0 for apply and apply --check, scan --prune removes the record, and the next apply reports nothing to apply.

A Windows test run was dispatched by hand on this branch (PR runs have no Windows job), on the commit before the last one. Every test this PR adds passed there. Two tests failed: the core crate's guard against an inline purl type check, which the last commit fixes (it passes in PR CI now), and quoted_url_credentials::a_hosted_warning_quotes_a_lock_entry_without_its_token in the scan suite, which fails on main on Windows too and is in code this PR does not touch.

🤖 Generated with Claude Code

A manifest record for a version of a package the project no longer uses
(the project moved to another version and the record was never pruned)
made every `apply` exit 1 with "matched no installed package". Nothing
is wrong with the installed tree in that state: the record has nothing
left to patch.

Such a record is now a skip in a category of its own:

- `apply` skips it with a warning that names the version the project
  uses and the command that removes the record, and exits 0. In `--json`
  it is a `skipped` event with `errorCode: "patch_stale"`.
- A record is stale only on evidence: the project's lockfiles or its
  installed tree hold the same package at another version, and neither
  holds the record's own. With no evidence the run still fails as before.
- `apply --check` and `scan` name stale records and the remedy.
- Telemetry reports the per-patch outcome `stale` with the code
  `patch_stale`, counted under `outcomes.stale` and never under
  `failure_codes`. A run that succeeds now also reports the records no
  installed copy matched, which it left out before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- A scan that prunes after its apply (`--prune` / `--sync`) removes the
  stale records in the same run, so the apply it runs no longer asks for
  them to be pruned.
- The remedy an apply run by `get` or `scan` prints leaves out the
  absolute path of the default manifest it was handed.
- Integration tests: apply (alone, beside a live patch, beside a record
  with no evidence, `--silent`, `--check`, a scoped remedy), scan (the
  notice, its `--json` warning, pruning, agent and sync runs) and the
  telemetry of an apply and of an agent scan.
- CLI contract, usage and configuration docs describe the `patch_stale`
  skip, the run warning and the `stale` telemetry outcome.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…atched

`scan --prune` keeps a Cargo or PyPI record while a copy of the record's
version is still patched on the machine: a crate in the shared registry
cache, a Python package in another interpreter or a tool's environment.
For such a stale record the prune is not a remedy, so the warning, the
event's reason and the run warning name `socket-patch rollback <purl>`
instead, with `-g` when the copy is one only a global rollback reaches.
The rule is the one the prune keeps the record by, read from the same
copies.

Tests run the command as printed on a Cargo and a PyPI project: the copy
is restored and the record is gone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`get` runs apply over the manifest after it saves its patch. A stale
record beside the fetched patch is named once (a `patch_stale` warning
under `--json`). A patch fetched for a version the project moved off
is stale as soon as it is saved: it is its own `skipped` event, so the
warning for it is left out of the envelope, and the run exits 0.

`run_nested_apply` takes the caller's facts as one `NestedApply`
instead of four arguments.

Also a test of a stale gem record beside a record for another project's
gem: the run no longer fails.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review fixes.

A version found in a store every project on the machine shares (a gem
home, the Cargo registry, the Go module cache, a Maven or Gradle
repository, the NuGet packages folder) may be another project's, so it
no longer makes a record stale: there, only a lockfile does. Installed
evidence is kept for npm, PyPI and Composer, whose packages are in a
tree of the project's own. A global run has no project to ask and
reports no stale patch; its unmatched records stay not installed.
`scan` follows the same two rules.

`scan --json` adds its stale warnings per record instead of all or
none, and a scan that vendors or redirects something now names the stale
records too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 4, 2026 20:45
@cursor

cursor Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes exit codes and apply/scan/get semantics for manifest entries that previously failed CI and postinstall hooks; mis-detection could skip needed patches or advise wrong remediation, though detection is constrained by ecosystem and lockfile rules.

Overview
Manifest records for package versions the project no longer uses are now classified as stale (patch_stale) instead of failing runs as package_not_installed. apply, nested applies from get / agent scan, and apply --check detect staleness from lockfiles first, then from npm/PyPI/Composer install trees only (shared caches never count as “the project moved on” without lock evidence). Global runs do not report stale project records.

User-visible behavior: stale records are skipped with warnings naming the version in use and scoped remedies (scan --prune, or rollback / -g when an old copy is still patched). Summaries gain a stale bucket; --json emits skipped + patch_stale and optionally patch_stale run warnings (deduped when the nested apply already emitted the event). Runs that only had stale unmatched entries exit 0 (install hooks stop failing on leftover manifest noise). Telemetry adds outcome stale and documents it in CLI_CONTRACT.md.

Wiring: new stale_patch module; scan surfaces stale records when not pruning (with prune_follows suppressing prune nag when --prune/--sync runs next); extensive unit/e2e tests cover apply, scan, get, telemetry, and rollback remedies.

Reviewed by Cursor Bugbot for commit 0829752. Configure here.

The release comparison tested the purl type with an inline prefix
check, which the guard test in the core crate rejects.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant