fix(sdk): make a failed step diagnosable instead of a protocol_error

Merged
KJ
3mo ago
+391-101

The report that started this

A local flows run of an authored .mts flow (relayflows 2.0.12). agent-1
succeeded in 59s; agent-2 died in 15s and produced this, in full:

✗ agent-2 (agent) [agent: failed] 15.09s
FAILED [protocol_error] relayflowd could not complete the run request: step_failed: Run "01M2NDS2RYK3MH6YBSB1CHA9SE" failed with completionReason: step_failed.
RUN 01M2NDS2RYK3MH6YBSB1CHA9SE unknown

No exit code, no output, no step id, no path to the journal, no hint that
.relayflowd/ exists. Not self-servable, and not diagnosable by anyone else
either without hand-searching the journal directory.

What the journal actually held

Everything needed, the whole time.

relayflowd-core/src/machine.rs nulls output on a failed completion for
every step type except deterministic (preserve_failure_output). But
relayflowd/src/engine/remote.rs runs worker_failure_detail(&completion.output)
before that, capturing the worker's {exit_code, stdout_tail, stderr_tail}
as a bounded render into verification.detail. For agent-2 that render
contained the exit code and the agent's own stderr. Nothing in the SDK read it.

Where the detail was dropped — three defects

  1. deterministicFailureDetails bailed on line 2. It returned undefined
    for any run without a deterministic step. Every f.agent and f.llm step
    runs as its own single-step kernel run, so that is every authored worker
    call. Renamed to stepFailureDetails: reads any step type, prefers output,
    falls back to trajectory_tail, then re-parses verification.detail when it
    carries the same shape. Its old predicate also required a non-zero
    exit_code, which silently skipped a deterministic step that exits 0 and
    then fails its gate; the predicate is now "terminal and not success".

  2. direct-run.ts called a known failure a protocol error. An
    AuthoredFlowExecutionError('step_failed') fell through to
    protocolFailure, which says the daemon could not complete the request —
    about a daemon that had driven the run correctly — and builds a report with
    no status. That is the entire meaning of RUN <id> unknown: not a
    status the renderer failed to recognise, but report.status ?? 'unknown'
    with nothing there. It now reports step_failed / status: failed,
    mirroring the McpStepError branch immediately above it, which had this
    shape all along.

  3. Nothing named flows replay. The premise that there is no command to
    inspect a finished run turned out to be false — flows replay <run-id> [--at <step-id>] exists and prints the whole journal. It was simply never named at
    the moment of failure. So: no new command. Every step_failed report now
    ends with the replay invocation and the journal path, and that footer is
    derived from the run id alone, so it appears even when the journal could not
    be read or the failure shape was not recognised.

Before / after

Captured from code on both trees (pristine origin/main vs this branch), not
hand-written. The "before" reproduces the operator's transcript exactly.

Before — authored .mts, f.agent step failed:

FAILED [protocol_error] relayflowd could not complete the run request: step_failed: Run "01M2NDS2RYK3MH6YBSB1CHA9SE" failed with completionReason: step_failed.
RUN 01M2NDS2RYK3MH6YBSB1CHA9SE unknown
exit 1

After — same run, same journal:

FAILED [step_failed] Run "01M2NDS2RYK3MH6YBSB1CHA9SE" failed with completionReason: step_failed. Step "agent-2" (agent) completionReason: worker_error exit=1.
Stdout (last 1,024 bytes):
Analyzing repository...
Error: unable to write to /work/out
Stderr (last 1,024 bytes):
claude: permission denied: /work/out
Inspect: flows replay 01M2NDS2RYK3MH6YBSB1CHA9SE --at agent-2
Journal: .relayflowd/runs/01M2NDS2RYK3MH6YBSB1CHA9SE.sqlite3
RUN 01M2NDS2RYK3MH6YBSB1CHA9SE failed completionReason: step_failed
exit 1

A declarative flows run with a failing agent step gains the same body; before,
it printed only the first sentence.

Paired negatives

  • A successful run is not noisier. The evidence path is reached only inside
    the step_failed branch, after if (report.ok) return. Pinned by does not
    inspect the journal or change diagnostics on success
    (asserts runGet and
    journalRead are never called).
  • An unknown failure does not pretend to know. A completion carrying only a
    reason reports that reason, omits exitCode/stderrTail/detail entirely
    rather than printing exit=undefined (which the old renderer did), and still
    prints the replay hint. Pinned by says where to look without inventing
    evidence it does not have
    .
  • A journal that cannot be read still says where it is. Pinned by
    preserves step_failed, explains failed inspection, and still says where to
    look
    .

The 429 — established, not assumed

[observer] token mint failed: mint API returned HTTP 429; skipping observer link

Non-fatal, and it could not have contributed to the step failure. The mint
is issued once per invocation, at cli.ts:209, before the run — not per
f.agent step. Its promise is .catch()-wrapped into a warning, and every
terminal use is io.stdout/io.stderr. It touches nothing in worker attach,
step dispatch, auth or budget; the exit code comes solely from
execution.exitCode. The correlation with agent-2 starting ~60s in is an
artifact of printing order: finalizeObserverLine emits after the RUN
summary, so a t≈0 failure surfaces at the end of the output.

The limit is real, and it is not this client retrying hot. The endpoint is
POST https://cast.agentrelay.com/v1/observer-tokens, handled by
@relaycast/engine. Its limiter keys on <workspaceId>:global:<minute> — a
per-workspace, fixed one-minute bucket, free plan 300/min — and
POST /v1/observer-tokens has no route multiplier, so it shares that bucket
with ~27 other route modules, including the /v1/actions/*/invoke and
/v1/agent calls a transport: 'relay' agent step makes. One mint per run is a
negligible consumer; a 429 here is a symptom that something else drained the
workspace's minute, not a cause. (The engine repo has an unmerged branch adding
'POST:/observer-tokens': 1.0 to ROUTE_MULTIPLIERS to split the bucket; its
test comment records a node-token draining the shared bucket in ~3.5 minutes.)

So it is cosmetic here, and the wording was the problem. skipping observer link next to a real failure reads like a cause — it cost attention in this
investigation. Now: continuing without an observer link (the run is unaffected). Kept on stderr under [observer] so it stays greppable, and
documented in SURFACE.md. No severity escalation, no new failure kind.

Tests

npm test in packages/sdk (tsc + tsc tests + vitest), exit 1:

Test Files  3 failed | 101 passed | 2 skipped (106)
     Tests  9 failed | 1668 passed | 14 skipped (1691)

The 9 failures are pre-existing and environmental, not from this diff. Run
on a pristine origin/main worktree in the same environment, the same three
files fail the same way — 3 failed (3) / 9 failed | 1 passed | 10 skipped —
with spawnSync .../kernel/target/debug/relayflowd ENOENT (these live tests
hardcode a target path that ops/cargo.sh no longer writes to, since it
relocates CARGO_TARGET_DIR outside the repo) plus one unrelated
expected '1.3.14' to be '1.4.0'. Files: webhook-live,
provider-trigger-executor, authored-node-runtime.

Every file this PR touches passes: step-failure-diagnostic 16 ✓,
direct-run-failure 7 ✓, observer-link 39 ✓, classify-outcome 2 ✓.

🤖 Generated with Claude Code


[!NOTE]
Medium Risk
Changes CLI failure classification and journal inspection for all run paths (declarative, authored direct, resume); behavior is well-tested but affects operator-facing exit reports and diagnostics.

Overview
Fixes flows run / flows resume so a known step failure is reported as step_failed with status: failed, not protocol_error and a RUN <id> unknown summary (authored .ts direct runs get an explicit direct-run branch mirroring McpStepError).

Replaces deterministicFailureDetails with stepFailureDetails: journal inspection now covers all step types, pulling evidence from output, trajectory_tail, and parsed verification.detail for agent/llm failures. Every step_failed diagnostic gains flows replay plus an optional journal path when --data-dir is known (DEFAULT_DATA_DIR centralized in daemon-connection.ts).

Observer mint warnings on run/resume now say the run is unaffected (documented in SURFACE.md). Tests expanded for agent/llm evidence, replay hints, and authored step_failed classification.

Reviewed by Cursor Bugbot for commit d03092184a1b65b9ba90f6b7a6751fe2a2128a29. Bugbot is set up for automated code reviews on this repo. Configure here.