Repository navigation
Skip a stale patch instead of failing apply - #2515
Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
Conversation
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>
PR SummaryMedium Risk Overview User-visible behavior: stale records are skipped with warnings naming the version in use and scoped remedies ( Wiring: new 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>
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 applyexited 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:
applyskips 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,-gand--ecosystemsso it can be pasted. The summary has astalebucket, shown only when non-zero, in place of counting the record as "not found on disk".apply --json: askippedevent witherrorCode: "patch_stale". Itsreasonnames both versions and the remedy.apply --checksays1 patch stale, skippedin place of1 patch not installed, skipped, prints the same warning, and reports the same event under--json. Still exit 0.scanwithout--prunenames stale records and the remedy where it used to say only "No patches available for installed packages". Under--jsonit is apatch_staleentry inwarnings[]. A scan that prunes removes the records and says nothing about them.getrunsapplyover the manifest after it saves its patch. A stale record beside the fetched patch is named once (apatch_stalewarning under--json). A patch fetched for a version the project moved off is saved, reported as its ownskipped/patch_staleevent, and the run exits 0. Before, thatgetexited 1 withpackage_not_installed.--silenthides the notice everywhere, like the lockfile-only note. It is a warning, never an error.A record
scan --prunekeepsscan --prunekeeps 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'sreasonand the run warning name therollbackthat restores the copy and removes the record, with-gwhen 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. When other stale records can be pruned, a last line says
Runsocket-patch scan --pruneto 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
-grun 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
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.--cwdguard and it is unchanged.Telemetry
{outcome: "stale", code: "patch_stale"}and counted underoutcomes.stale. It is never infailure_codesand never makes the eventpatch_apply_failed.stale, orskipped/package_not_installedfor 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.getandscan --mode agentrun reports the same outcomes under the caller's command, once. Their--jsonenvelope carries apatch_stalewarning for a stale record the run did not select.Tests
--checklines, the telemetry mapping, andreported_patchesfor 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,--checkin text and--json, and a Cargo record with its old cache copy still patched (namesrollback; 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--jsonwarning,--pruneremoving the record, and agent and--syncruns 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 namesrollback <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 oneskippedevent, 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.scan --prune, andscan --prune --cwd <dir> --ecosystems npm, each removed the record without--yes, and the nextapplyhad nothing to skip.Ran:
cargo test -p socket-patch-cli --lib(1120 pass), theapply(251),get(169),cli(127) andscansuites, the in-process get and scan suites,e2e_cargo,e2e_pypi_multi_copy, and the remedy, help-text and layering hygiene suites. Onescantest 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:
scanfollows the same rule. Test: a Cargo project with no lockfile and another version in the registry cache still exits 1.apply,apply --checkandscan.scan --jsonadded 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.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 reorderingapply, and it overlaps separate work on where patch data is read from.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
applyandapply --check,scan --pruneremoves the record, and the nextapplyreports 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_tokenin the scan suite, which fails onmainon Windows too and is in code this PR does not touch.🤖 Generated with Claude Code