Files
loki/scripts/test
f043fe323a fix(orchestration): a run nobody delivered can no longer report success (#341)
* fix(orchestration): a run nobody delivered can no longer report success

Yesterday's fix made the reaper announce failure. Retrying the five stranded
feedback fixes against it exposed something worse one layer down: three runs
that were NEVER DELIVERED closed as success/partial, and two visitor reports
were auto-resolved as shipped without an agent ever seeing them.

The chain, traced in prod:

- Five dispatches, FIFO-serialized. One was injected; four sat queued.
- At T+60min purgeStalePendingCommands deleted the four queued commands (the
  gate-hold grace expires at STALE_RUN_MINUTES) and the close sweep ran in the
  SAME tick.
- Guard 1, hasUndeliveredCommandForRun, infers non-delivery from a
  pending_commands row — which had just been deleted. Absent read as delivered.
- Guard 2, runEffectiveStartMs, falls back to startedAt when deliveredAt is
  missing. So the one delivered run's ready handoff post-dated all four.
- All four closed off ONE sibling's work. Three non-failing verdicts, and
  success funnels into resolveFeedbackForRun -> "shipped".

Both guards fail open on missing data, and the comment on guard 2 describes
this exact bug as already fixed ("that is how run B used to get closed and its
feedback auto-resolved off run A's work"). It recurred because the fallback
reintroduces it precisely when deliveredAt is absent — which is precisely when
the run was never delivered.

The fix makes absence mean something definite:

- deliveredAt is now stamped on BOTH transports — runner ack for queued
  dispatches (unchanged) and at inject time for direct ones (new, inject-core
  + both tab-inject direct paths). One field, one meaning, no third state.
- closeRunFromSession refuses any run without it. Undelivered runs belong to
  the reaper, which stamps the honest timeout.
- hasUndeliveredCommandForRun stays as a cheap early-out, with a comment
  recording why a row another process garbage-collects cannot be the only
  guard.

Test pins the invariant (scripts/test/undelivered-run-close.ts): a ready
handoff post-dating dispatch must NOT close an undelivered run, and must still
close a delivered one. Proven by mutation — removing the guard fails
"a ready handoff does NOT close an undelivered run"; green when restored.
Auto-discovered by test-unit, so it cannot be born dead.

handoff-evidence.ts's fixture gains deliveredAt: it encoded the fail-open
assumption and tests evidence plumbing, not delivery.

Prod data repaired in the same pass: 2 feedback items reopened, 3 verdicts
corrected to timeout. Scoped to the traced batch only — for older runs a
missing stamp is ambiguous (direct injects were never stamped before this),
so they are deliberately left alone rather than rewritten on a weaker
inference.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(orchestration): the state machine encodes the delivery rule too

orchestration-state-machine.ts shared handoff-evidence.ts's fixture
assumption — an open run with no deliveredAt — so it went red on the new
guard. Its openRun is now a delivered one, and the suite gains an explicit
case for the undelivered path, which is where the transition gate belongs.

Two suites having frozen the fail-open as the expected result is the honest
signal that the guard was missing, not that the guard is wrong.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: t <t@t.t>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-24 19:57:15 +00:00
..