✨ feat(handoff): handoff --detect names the stage nearest the repeated-failure trigger (#588)

Merged
AT
3d ago
+72-12

Slice: #588

niyam handoff --detect now names the stage closest to the repeated-failure trigger before it fires, with its counts and the threshold. Part of https://github.com/veda-hq/niyam/issues/394 (spec v1, approved 2026-11-24).

Why. Today repeatedFailure sets a stage only once one fires (3 failures over 2 trees), so a branch with 2 real failures prints failures 0. The athya-rules lesson docs/lessons/push-and-prepush.md#repeated-failure-trigger says that invites one more blind retry, which fires the trigger.

What changes (src/handoff.ts).

  • Repeated carries threshold: { failures, trees }, from MIN_FAILURES and MIN_TREES.
  • A stage that fired is still the one named, picked as before (most failures; the first seen on a tie). It wins over a stage that failed more without firing, so fired is computed as before and nothing fires earlier.
  • When none fired, the nearest stage is named with fired: false: the one with the most failures, then the most trees, then the first seen.
  • With no failure on the branch it prints stage: null, failures: 0, as before.
  • fired(), the gate's refusal text and the run log are unchanged: they read only .fired.
  • The README's hand-off section says so.

Checked: pnpm check (provenance, typecheck, lint, test, test:scripts, pins, pariksha, hooks:check, build, smoke), exit 0:

pnpm test:          tests 2193, pass 2193, fail 0
  handoff.ts 100.00 lines / 98.53 branches / 98.15 funcs
pnpm test:scripts:  tests 138, pass 138, fail 0

The built CLI, node dist/cli.js handoff --detect, on a scratch repository with a push history (.triggers.repeated and .fired shown):

== no failure
{"repeated":{"fired":false,"stage":null,"failures":0,"trees":0,"threshold":{"failures":3,"trees":2}},"fired":[]}
== two failures, one tree (near miss)
{"repeated":{"fired":false,"stage":"test","failures":2,"trees":1,"threshold":{"failures":3,"trees":2}},"fired":[]}
== three failures, two trees (fires)
{"repeated":{"fired":true,"stage":"test","failures":3,"trees":2,"threshold":{"failures":3,"trees":2}},"fired":["repeated-stage-failure"]}

New and changed tests in src/handoff.test.ts:

  • A near miss (2 failures in 1 tree), a fire, and no failures, both in repeatedFailure and through handoff --detect's JSON.
  • The noise case: two real test failures among other branches' and kinds' runs are named as a near miss with only their own counts.
  • The nearest-stage order: most failures (3 in one tree beats 2), then most trees, then first seen.
  • A stage that fired is named over one that failed more without firing, in either order of the history.

No reader in athya_mono uses repeated.stage or repeated.failures without fired; its ledger tracks this very gap (detect-reports-near-threshold).

Guards: none.

🤖 Generated with Claude Code


Summary by cubic

niyam handoff --detect now names the stage nearest the repeated-failure trigger before it fires, with its counts and the threshold, so a branch with two real failures no longer reads as failures 0. The README's hand-off section documents the new output.

Changes

  • When none fires, the nearest stage is the one with the most failures, then the most trees, then the first seen.
  • A stage that fired is still named as before and wins over one that failed more without firing.
  • Repeated now carries the threshold; the gate's refusal text and run log are unchanged.