fix(sdk): lower done("step_failed") in the authored executor
Merging this does not reach the operator's CLI. publish.yml is
workflow_dispatch-only — no push or tag trigger — and its package input
states single-package selections are dry-run only, so a real publish needs a
deliberate run with package: all and dry_run: false. Both packages/sdk
and packages/surface sit at 2.0.14, which npm already has, so a version
bump is required before any of this reaches a user, followed by a kit pin
bump in agentrelay.com. Please do not read a merge here as "the flow can be
re-run."
Why this exists
A Shortcut-triggered software-factory run (544df8ea-8392-5ca0-a207-7efde94a4c12)
did all of its work correctly — 15 agent steps, pushed a branch, opened
https://github.com/AgentWorkforce/cloud-e2e-sandbox/pull/25 — and then died at
the final step:
FAILED [protocol_error] relayflowd could not complete the run request:
unsupported_completion: the initial authored executor cannot lower done("step_failed")
The generated flow ends if (!clean) return f.done("step_failed") when the
adversary review finds problems. step_failed is a legitimate
FlowCompletionReason (packages/surface/src/completion.ts:27), and done()'s
first gate accepted it — then a second, hand-written gate refused everything
except success and needs_human.
The type system said valid; the runtime said no. That gap is the defect. A
flow that can express failure could not execute.
This is not a new limitation being lifted. web/app/flows/flow-examples.ts in
agentrelay.com already ships f.done("step_failed") as a public authoring
example, so the surface has been advertising a completion the runtime refused.
The fix
1. How it lowers. step_failed follows the needs_human pattern: a
deterministic terminal marker that succeeds and writes
{"completionReason":"step_failed"} to stdout. success stays :, because
success needs no marker — the marker run's own kernel success already is the
record.
It is deliberately not lowered as a failing command. The kernel's per-step
vocabulary cannot carry an authored verdict on a step that succeeded, so the
verdict travels as data. Lowering it as exit 1 would have made
lowerDeterministic throw step_failed and put a fabricated failing step named
complete-N into the journal of a flow whose steps all ran correctly.
2. Exit code: 1, not 3. Exit 3 means parked — resumable, waiting for a
human, and the local kit's START-HERE text documents it as the manual-approval
stop. A step_failed flow is terminal; there is nothing to resume. Exit 1 is
already documented as "the run failed with a declared completionReason", which
is exactly what this is.
This is also forced by an existing contract rather than chosen freely:
getCloudFlowRun (packages/sdk/src/cloud-run.ts:156) rejects a completed
terminal whose reason is not success, and accepts step_failed only under
status: failed. Exit 0 or exit 3 would make Cloud refuse the record as
invalid_response.
3. Other reasons stay refused — with a reason. canceled and
budget_exceeded are kernel outcomes, not authored verdicts: the kernel records
them when it cancels a run or exhausts its budget, so a body declaring one would
assert a kernel fact that never happened. canceled additionally has no terminal
shape to lower into — there is no cancelled RunStatus locally, and Cloud
accepts that reason only under a cancelled status this CLI never reports. The
refusal message now says this instead of "the initial authored executor cannot
lower", which read as a temporary limitation.
4. One answer, not four. Four gates each hand-wrote ['success', 'needs_human'] — done(), verifyAuthoredNodeResult, isCompletedRootOutput,
and the terminal-marker string (duplicated verbatim in the executor and the node
runner). They now share isLoweredCompletion and completionMarker, so a
reason cannot be accepted in one place and rejected in another again. That
duplication is the bug class, not a tidiness concern.
runDirectFlow and resumeFlow likewise now share one authoredCompletion
helper, for the reason #431 gave when it shared authoredStepFailure: a copy is
how they drifted. The needs_human report had been duplicated verbatim across
both files.
authoredCompletion is deliberately not authoredStepFailure, though both
land on exit 1. That function describes a step that ran and failed and carries
its evidence; here every step succeeded and the body declared the outcome, so
there is no failing step to name. Routing through it would invent one.
On the error code of the same name
AuthoredFlowExecutionErrorCode already contains 'step_failed'. It does not
interact with this change. The error code means "a lowered step ran and failed"
(thrown by authored-worker-step, authored-helper-effect, authored-mcp,
authored-plugin-effect, authored-budget). The completion reason means "the
body declared its checks did not pass". They converge only in the report —
both produce exit 1 / status: failed / completionReason: step_failed, which
is correct, because the CLI and Cloud have one shape for "this run failed". The
paths stay distinct: the error path throws and carries step evidence; the
completion path returns normally with an all-successful journal and says so.
Tests
tests/authored-step-failed.test.ts (10) and
tests/authored-step-failed-exit.test.ts (3):
done("step_failed")lowers without throwing, and every journal step —
terminal marker included — issuccess.- the terminal spec is the
printfmarker, asserted as an exact command. - a PR URL captured before the verdict survives: the
f.runthat printed it
is still a recorded, successful journal step with its own run id, and its
stdout still reaches the body. An adverse verdict truncates nothing before it. - exit 1 /
status: failed/completionReason: step_failed, with the
diagnostic asserted not to containprotocol_error,unsupported_completion
orcannot lower— the exact strings the production run died with. - the verdict names no fabricated failing step.
success(exit 0,:) andneeds_human(exit 3, its marker) assert
unchanged, including through the shared report helper on the resume path.canceledandbudget_exceededstill refuse.
The resume path is covered by unit-testing authoredCompletion directly rather
than by mocking authored-root.js, which sidesteps the trap #431 documented:
vi.mock of that module does not reach run.ts's own import of it.
And one live end-to-end test, in tests/direct-input.test.ts beside the
needs_human exit-3 test that already lives there: the real built CLI against a
real relayflowd, running tests/fixtures/step-failed.flow.ts. It asserts the
exact production shape — exit 1, FAILED [step_failed] on stderr, and no
protocol_error — then reads the journal back and pins the durability claim the
fix rests on: the authored root's step succeeded with
output.completionReason: step_failed, all three lowered steps are success,
the terminal marker's stdout is {"completionReason":"step_failed"}, and the
pull request URL printed before the verdict is still readable in its own child
journal.
The transport is mocked nowhere in that test. It is the failing production
invocation, run for real.
Review response
The maintainability lens passed with no blockers and three concerns. All three
are addressed in e86118c2 rather than argued with:
authoredCompletionhad no exhaustiveness gate. Correct, and the sharpest
point in the review: it took the wideFlowCompletionReasonand let anything
that was notsuccess/needs_humanfall into the failure branch, so a stray
canceledwould have reporteddeclared done("canceled"): its own checks did not pass— something the body never said. That re-opened this PR's own defect
on the reporting side.AuthoredFlowExecutionResult.completionReasonis now
LoweredCompletionReason(the truth —done()cannot store a reason the
executor does not lower), and the helper switches exhaustively and ends in a
neverguard. A fourth lowered completion is now a compile error, not a wrong
report. The authoring surface is untouched and stays wide.- The repeated
isLoweredCompletionguard was a narrowing workaround defended
only by prose. The narrowing above dissolves it: the duplicate is deleted,
and the top-level check still validates the untrusted IPC frame. A future
"simplify duplicate guard" pass can no longer break the marker comparison. - Exported helpers vs the module's "deliberately not exported" docstring.
Noted on the exports that they are internal cross-module helpers, not public
surface. Verifiedsrc/index.tsreferences this module nowhere, so nothing
leaks today.
The history lens reported FAIL and the structure lens MISSING. Both are
infrastructure, not findings: the history transcript ends in
ERROR: You've hit your usage limit from the Codex provider, and structure
produced no transcript at all. The same MISSING/FAILED swarm result appears on
PRs #430, #431 and #433, all of which merged.
Measured, on this PR's base commit (c8c68315)
Same machine, same pinned relayflowd, kernel built in both worktrees:
| Test Files | Tests | |
|---|---|---|
Baseline (c8c68315) | 6 failed, 99 passed, 2 skipped (107) | 11 failed, 1664 passed, 18 skipped (1693) |
This branch (d1cfbd11) | 6 failed, 101 passed, 2 skipped (109) | 11 failed, 1678 passed, 18 skipped (1707) |
+14 passed, +14 total — exactly the 14 new tests. Failure count unchanged.
The two runs fail on the same 6 files and the same 11 test names — an
identical set, not merely an equal count. They are environment-dependent:
live-daemon tests (webhook-live, live-kernel, mcp,
provider-trigger-executor), bundle, and authored-node-runtime — the last
from a bun version mismatch (1.3.14 installed, 1.4.0 required), verified
by running that file on the base commit and on this branch and getting the same
assertion. None are touched by this change.
The first baseline I took was not trustworthy and was discarded: that worktree
had no relayflowd built, so its live tests failed for a different reason than
the branch's. The numbers above come from a re-measured baseline with the kernel
built in both worktrees and the same pinned binary.
npm run typecheck, npm run typecheck:tests and npm run build are clean.
tests/direct-input.test.ts — which drives a real daemon — is not in the
failing set and passes 6/6, including the new live step_failed test.
What a release requires
- Merge this.
publish.ymlviaworkflow_dispatchwithpackage: all,dry_run: false,
and a version bump (2.0.14→2.0.15).package: allis mandatory — the
workflow refuses a real release otherwise, so surface/sdk/runtime/relayflows
versions and internal pins advance together.- Bump the kit's pin in
agentrelay.comto the published version.
Not done here, and not to be inferred from a merge.
Cloud-side gap this does not fix (named, not touched)
cloud's relayflow-v2-executor.ts attaches the captured pullRequestUrl to
the report on a completed run, or to primaryError only when it is a
RelayflowV2ParkedError — and parkedReportDetail builds one only for
exitCode === 3 with status: "parked". A step_failed run exits 1, so it
takes the generic-Error branch and that attachment is skipped.
The URL still reaches Cloud: it is scraped per step from output.stdout_tail
during journal replay and carried on the telemetry payload, which this change
preserves intact. But the report/error field that reporter.ts forwards
would be absent for a step_failed run. That is a Cloud-repo change and is
not made here.
🤖 Generated with Claude Code