Skip to content

Say why an apply failed in its telemetry and --json events - #2247

Merged
Mikola Lysenko (mikolalysenko) merged 6 commits into
mainfrom
feat/apply-failure-telemetry
Nov 25, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 6 commits into
mainfrom
feat/apply-failure-telemetry

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

Why

A failed in-place (agent mode) apply reports error_code: apply_failed and, per patch, code: apply_failed whatever went wrong. The patch-lifecycle events therefore cannot tell a hash mismatch (a different build of the version, or a local edit) from a missing blob, a write error, a release variant that does not match, or a manifest patch whose package is not installed. Several of those fail the run with no per-package result at all, so the event names no patch either.

What changes

Commit 1: why an apply failed

  • core: ApplyResult.cause (ApplyFailureCause, #[non_exhaustive]) is set where a result is failed:

    • the in-place engine: hash_mismatch, file_not_found, unsafe_path, shared_store, conda_build, blob_missing, blob_corrupt, write_failed, sidecar_failed;
    • refusals and native rebuilds under their own codes: the Maven/Gradle refusals, gem refusals, native_extension_build_failed;
    • a failed go.mod edit (write_failed).

    A path that sets none is unclassified. The cause is never guessed from the verified files, which a later step can leave describing something else, and the unexplained share stays measurable. A failed pnpm/vlt store copy hands its cause to the package.

  • --json: a failed apply event keeps errorCode: apply_failed (consumers route on it) and adds cause. This is additive; nested get / scan envelopes carry it too.

  • telemetry:

    • each failed patch's code is its cause;
    • failures with no package result are named: no release variant matched (once per record, never a record another copy applied, and never misreported as not installed), a Maven base refused as a whole, and a manifest patch whose package is not installed (package_not_installed). These appear in telemetry only; --json output is unchanged for them;
    • failure_codes counts failed patches per code over the whole run, so a run over 50 patches is not cut off;
    • a run stopped before patching anything reports the stage (sources_download_failed, offline_missing_sources) as its error_code.
  • Exit codes, human output and what counts as a failure are unchanged.

Commit 2: context.ci (separate on purpose: it's a new environment signal, so it's the maintainer's call whether to keep it)

  • true when CI is set to anything but empty, 0 or false, or GITHUB_ACTIONS is set. This is the rule the update notifier already used, now shared from core. It names no provider, repository or job. Drop the commit to ship without it.

Tests

  • e2e (tests/cli/telemetry_e2e.rs): hash mismatch, missing file, corrupt blob, package not installed, failed blob download. Each asserts the telemetry code, failure_codes, error_code, the envelope's cause, and that no package name leaks.
  • unit: the cause fallback, a failed store copy's cause, distinct codes, failures with no result, the run-level stage code, failure_codes counting, CI detection.
  • cargo test -p socket-patch-core -p socket-patch-cli passes locally, except two old-toolchain Cargo e2e tests that need x86 binaries this arm64 Mac can't run. cargo clippy --workspace --all-features -D warnings and cargo fmt --check are clean.

Not in this PR

  • Paths not listed above (for example the jar swap's own write failures) report unclassified. That share is now visible and can be narrowed later.
  • Semver: ApplyResult gains a public field, so code outside this workspace that builds it with a struct literal must add cause. Nothing in the workspace is affected; the enum is #[non_exhaustive] so later causes don't break matches.
  • Counting how many of a package's files mismatch would mean verifying past the first failing file, which changes apply's behaviour, so it's left out.

🤖 Generated with Claude Code

A failed in-place apply reported the same `apply_failed` code for every
cause, so the patch-lifecycle events could not tell a hash mismatch from
a missing blob, a write error or an uninstalled package.

- core: `ApplyResult.cause` (`ApplyFailureCause`), set at each failure of
  the in-place engine: hash_mismatch, file_not_found, unsafe_path,
  shared_store, conda_build, blob_missing, blob_corrupt, write_failed,
  sidecar_failed; Maven refusals keep their own codes. A failure that
  sets none falls back to the file that stopped it, else `unclassified`,
  so the unexplained share stays measurable. A failed store copy hands
  its cause to the package.
- --json: a failed apply event keeps `errorCode: apply_failed` and adds
  `cause` (additive; nested get/scan envelopes carry it too).
- telemetry: each failed patch's `code` is its cause; failures with no
  package result (no release variant matched, a Maven base refused as a
  whole, a manifest patch whose package is not installed) are named too;
  `failure_codes` counts failed patches per code over the whole run; a
  run stopped before patching reports `sources_download_failed` /
  `offline_missing_sources` as its `error_code`.

Exit codes and what counts as a failure are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`context.ci` is `true` when `CI` is set to anything but empty, `0` or
`false`, or `GITHUB_ACTIONS` is set: the same rule the update notifier
already used, now shared from core. It tells CI runs from a developer's
machine without naming the provider, repository or job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review fixes:
- Drop the fallback that guessed a cause from the verified files: a step
  after a successful engine run (a failed native rebuild, a `--force`
  skip) could leave the last file describing something else. A failure
  with no cause set is `unclassified`.
- Set the cause where results are failed outside the engine: gem
  refusals and `native_extension_build_failed`, Gradle's stale transform
  copies, a failed `go.mod` edit (`write_failed`). `Refused` becomes
  `Coded` and the enum is `#[non_exhaustive]`.
- A copy no release variant matches is reported as `no_matching_variant`,
  not `package_not_installed`; once per record, never a record another
  copy applied; Maven entries come from the manifest-filtered variants.
- A failed store copy's cause is read before its files move.
- The contract says which causes reach `--json` and which only telemetry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 17:08
@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes apply failure reporting and telemetry shape (new cause, failure_codes, context.ci); ApplyResult gains a public field and behavioral reporting paths, but apply success/exit semantics are intended to stay the same.

Overview
Failed apply runs now expose a stable cause on --json events (alongside unchanged errorCode: apply_failed) and use that cause as each patch’s telemetry code instead of generic apply_failed.

The patch engine records ApplyFailureCause on ApplyResult for hash mismatch, missing files/blobs, write/sidecar errors, variant mismatches, refusals, and similar cases; failures with no per-package result (no matching variant, Maven refusal, not installed) are still reported in telemetry via unreported / reported_patches. Run-level apply telemetry prefers stage codes like sources_download_failed when nothing could be patched, and adds failure_codes counts. pnpm/vlt multi-copy fold propagates the failing copy’s cause.

Separately, telemetry context.ci is set via shared running_in_ci() (also used by the update notifier). CLI_CONTRACT and configuration docs describe the new fields.

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

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

Reviewed by Cursor Bugbot for commit b7a8965. Configure here.

…lemetry

# Conflicts:
#	crates/socket-patch-cli/src/commands/apply.rs
`gradle_ro_cache_shadows` fails the run while the writable copies are
patched; only a record no copy matched is left out when another copy
applied it. Drop the configuration and contract doc edits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged via the queue into main with commit da012af Nov 25, 2026
89 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the feat/apply-failure-telemetry branch November 25, 2026 18:19
Mikola Lysenko (mikolalysenko) added a commit that referenced this pull request Nov 25, 2026
Main gained the PyPI and RubyGems vendored refusals (#2208) and the
reason of a failed apply in its telemetry and `--json` events (#2247).

#2208 is the squash of the branch that the merge before this one
brought in. Main's tree is that branch merged with #2247, so the seven
files that conflicted there conflict here again, with the same two
sides: `vendor/refusals.rs`, `bazel/built.rs`, `bazel/maven.rs`,
`args.rs`, `CLI_CONTRACT.md`, `docs/ecosystems.md` and
`scripts/loc-budget.json`. Each is what the merge before this one made
of it, and #2247 touches none of them. So this tree is the last one with
#2247 added and nothing else: 16 files, none of which this branch
changes.

The cap of the `bazel` group in `scripts/loc-budget.json` stays 5300,
the one value every open change of this series sets so that they stop
conflicting on that line; the count of this tree is 4638.

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.

2 participants