feat(sdk): flows eval — re-run a flow version against a frozen suite as a deploy gate

Merged
KH
2mo ago
+2398-1

Why

flows replay is a read-only journal dump, not a re-run (see discovery gap #1, "replay-as-gate"). A self-improving flow needs to re-execute a candidate flow version against fixed inputs and get back a verdict it can gate a deploy on, and it has to be able to do that from inside a step.

What

  • evaluateFlow(options), exported from @relayflows/sdk. Given a flow version and a frozen suite of N cases, it re-executes the version once per case. It returns a flows.eval.report containing:
    • per case: pass/fail/error, failure reasons, run id, latency, cost, tokens, per-step status and duration, and caller scorer metrics;
    • summary: pass rate, total cost, and p50/p95/max latency;
    • gate.pass: the result of the minPassRate (default 1), maxTotalCostUsd and maxP95LatencyMs checks, plus --baseline regressions. Errored cases always fail the gate (fail closed).
  • Frozen inputs. Every report pins flow.version (sha256 of the source bytes) and suite.sha256. expectVersion / --expect-version refuse to evaluate any other bytes. Suite parsing is strict: an unknown field is refused, so a typo cannot weaken a gate.
  • Executors
    • local: one fresh flows run process per case. This is deliberate: an authored .flow.ts is loaded with import(), and the ESM cache would otherwise keep running the first version a long-lived caller loaded. The live test caught this. Cost comes from the journal plus the authored child-run journals.
    • cloud: runInCloud, then wait, then getCloudRunSteps spend.
    • custom: any function the caller supplies.
    • Memoization (--reuse-from) is never used.
  • flows eval <flow> --cases suite.json [--json] [--cloud] [--report f] [--baseline r] [--expect-version v] [--concurrency n]. The exit code is the verdict: 0 pass, 1 fail, 2 refused.
  • docs/EVAL.md and the regenerated docs/CLI.md.

Callable from a step?

Yes, two ways:

  • f.run('flows eval … --report r.json', { onNonZero: 'record' }) and branch on ok;
  • import { evaluateFlow, localFlowEvalExecutor } from '@relayflows/sdk' in a step script or an authored body.

flow-eval-live.test.ts proves the first way end to end against a real daemon: a deploy-gate flow's step runs flows eval and completes success only on a passing gate.

Tests

  • tests/flow-eval.test.ts (26 tests): suite validation, scoring, thresholds, errors failing closed, scorers, baselines and regressions, version/suite pins, concurrency, abort, local/cloud outcome mapping, and journal spend including child runs. It also covers the CLI exit codes.
  • tests/flow-eval-live.test.ts (2 tests, real relayflowd):
    • fresh re-execution: 6 distinct run ids across 2 evaluations;
    • an edited candidate is detected as a regression against the baseline;
    • the step-invoked gate.
  • relay-cli-surface invocations and the authoring-reference verb count are updated.
  • typecheck, typecheck:tests and build are clean. Two authoring-reference failures (AgentOptions resume/sessionId missing from AUTHORING.md) already fail on main and are not touched here.

Out of scope

  • Deploy after a pass: deployToCloud still needs an interactive cli:auth session.
  • The post-merge trigger.
  • A token for the Cloud executor inside a Cloud sandbox: a run-scoped token can only read its own run.

Full local SDK suite: 4289 passed and 12 failed. The same 12 also fail on main in this environment (webhook-live, provider-trigger-executor, event-await-cli, live-kernel daemon race, mcp release binary, cli-watch, communication-mixed-resume), so this PR adds no failures.

Do not merge without human review.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


[!NOTE]
Medium Risk
New execution path that runs real flow cases (local subprocesses or cloud) and influences deploy gating; version sealing reduces drift risk but evaluated code still runs with caller privileges and linked node_modules.

Overview
Adds flows eval and the evaluateFlow API so a candidate flow version is re-executed once per frozen suite case (unlike read-only flows replay) and scored into a flows.eval.report whose gate.pass is the deploy decision.

The SDK pins flow.version (manifest over transitive local sources, lockfiles, and locked extensions) and suite.sha256, seals each case in a fresh read-only snapshot, and gates on per-case expectations, suite thresholds, optional baseline regressions, and fail-closed handling for errored cases and unknown costs. Local runs use one flows run process per case; cloud runs hosted runs and refuses flows that depend on local modules beyond the entry.

CLI exit codes are 0/1/2 (pass/fail/refused); docs/EVAL.md and docs/CLI.md document the feature, with broad unit and live daemon tests.

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


Summary by cubic

Adds flows eval and evaluateFlow to re-execute a flow version against a frozen suite of input cases and gate a deploy on the score report. Previously flows replay only dumped a journal; a candidate version is now run once per case and scored.

Key details

  • The report carries per-case outcomes, latency, cost, tokens, step status, caller metrics, and a summary; gate.pass reflects minPassRate, cost and p95 latency thresholds, and baseline regressions. Errors always fail the gate, and negative, non-finite, or unknown costs never pass a cost ceiling.
  • Reports pin the flow version (a manifest hash over the entry, its transitive imports and used flows, the project lockfiles, and locked plugin extensions) and suite sha256; --expect-version refuses other bytes. Each case executes a fresh, read-only sealed copy of exactly the judged bytes with per-file package.json "type" scoping and ancestor node_modules linked at relative paths; in-memory specs are structured-cloned and deep-frozen before hashing. Mid-evaluation edits (even edit-and-restore) fail instead of running unjudged; modules the manifest cannot see are absent from the snapshot and fail. Local runs start in the snapshot directory so relative runtime reads resolve inside the sealed copy. Baselines are validated before any case runs and must cover exactly the suite's case ids; parsed suites are deep-frozen so scorers cannot rename cases or loosen expectations. Evaluated code runs as the caller with lockfile-pinned node_modules linked; hostile flows need a sandbox.
  • Executors: local (one flows run process per case, avoiding ESM caching a stale .flow.ts; spend sums step budgets across root and authored children), cloud (reads the hosted run's stored report for verdicts consistent with local, then steps for spend; refuses a flow spanning local modules — sealed extension payloads are submitted as extensions and exempt — since Cloud runs the entry source alone), and custom caller functions. worker_unavailable is distinguished from needs_human.
  • Case inputs are bounded like flows run --input (1 MiB, depth 64, max nodes), and MAX_FLOW_EVAL_SOURCES caps manifests and extension payloads. Callable from a step via f.run('flows eval …', { onNonZero: 'record' }) or programmatically from @relayflows/sdk. The CLI exit code is the verdict: 0 pass, 1 fail, 2 refused; --json refusals are still emitted as JSON.

Agent Relay sessions

  • claude session d3ee38b7 · opened via gh pr create · last active 2026-10-10
  • claude session 5863fddb · contributor · ran gh pr commands · last active 2026-10-10