feat(sdk): record a named step DAG for authored flows
What / why
Agent Relay Cloud can't draw a TypeScript (authored) flow's run as a graph today. Step ids like agent-4 carry no name and no dependency edges. A prospect wants to watch a DAG of parallel agents run, so every authored step now records:
label: thef.agentname (it was discarded before withvoid name), the first 60 characters of anf.runcommand on one line, or thef.hookname.f.llmandf.humanhave no label.after: the step's causal predecessors, transitively reduced.
Unchanged: step ids, admission keys and resume identity. This is observational metadata only and never gates execution.
How the edges are found (src/authored-step-graph.ts)
The edges are captured synchronously in the AuthoredFlowOperation constructor, from executionAsyncId() at the moment f.* is called. From there:
- The walk goes backward over the existing promise graph (trigger, resolutionCause and creationContext edges; see the new
AuthoredPromiseGraph.causesOf). - It stops when it reaches a step's own promise.
Promise.allandPromise.allSettledaggregates expand to every member. The runtime only links an aggregate to the member that settled last. The member lists come from the combinator interception that already exists; aStepthenable maps to its operation's promise.raceandanyfollow only the winner.- The result is transitively reduced: a candidate that is an ancestor of another candidate is dropped.
- Cost stays bounded. The frontier is memoized per invocation context, and a walk that reaches an already-answered context reuses its answer. Measured: about 44 edge lookups per step at both 100 and 1000 sequential steps. The walk is capped at 10k nodes, and past the cap the record says
afterTruncated: truerather than guessing.
Helper, MCP and plugin effects write no index record. The walk passes through them, so an after entry never names a step the index doesn't contain.
Record schema change (optional, backward compatible)
relayflows.authored-step.v1, on both admitted and completed records:
{
index: 'relayflows.authored-step.v1',
step: string, runId: string, state: 'admitted' | 'completed',
completionReason?: string, kernelStep?: string,
label?: string, // bounded to 256 chars like other fields
after?: string[], // step ids, at most 32
afterTruncated?: true, // present only when `after` is incomplete
}
The root completion journalSteps entries get the same three optional fields. A step with no label and no predecessors writes exactly the old shape. isAuthoredStepRecord validates the new fields and skips malformed records, as it already does. The current Cloud reader (packages/core/src/bootstrap/lib/relayflow-v2-authored-index.ts) ignores unknown fields, and authored-root.ts / authored-node-runner.ts already tolerate extra journalSteps fields. The Cloud PR that draws the graph consumes these fields.
Tests
New:
tests/authored-step-graph.test.ts(13 tests), against a real lifecycle:- parallel fan-out draws no sibling edges
a→b→cstays a chainawait Promise.all(deps)depends on all deps, whichever settled last; same forallSettledracefollows only the winner- diamond reduction
- async wrapper functions
- the examples/task-graph pattern: a map of promises, each awaiting
Promise.allof its deps, thenrun → agent → run - a fan-in of 40 is capped at 32 and marked
afterTruncated - 300 sequential steps each list only their predecessor
- the node cap stops a 50k-node walk and marks it truncated
- the
f.runlabel is on one line and truncated at 60 chars
tests/authored-step-graph-live.test.ts: through the live kernel and local agent. Labels andafterappear on the admitted and completed stream records, onjournalSteps, and after areadAuthoredStepIndexround-trip. Ids are unchanged.tests/authored-step-index.test.ts(+4): round-trip, old shape when there are no edges, bounds, and malformed records skipped.
Updated, because they pin the journalSteps or index shape and an f.run/f.agent step now carries its label: authored-declined.test.ts, authored-flow.test.ts, agent-transcript-live.test.ts.
Results, run locally in the worktree with RELAYFLOWD_BIN set to a kernel built by ops/cargo.sh:
npm run typecheck: exit 0npm run build && npm run typecheck:tests: exit 0vitest run(full):Tests 28 failed | 2605 passed | 17 skipped (2650). None of the failures come from this change:authored-node-runtime.test.ts: needs bun 1.4.0, but the machine has 1.3.14. Also fails on main.canonical-software-factory.test.ts(3) andstuck-run-triage.test.ts(22):expected an @relayflows/surface flow handle, a module-resolution issue in this worktree's local install. Unchanged main in the same worktree gives the same 25 failures.cli-watch.test.ts(2) andcli.test.ts(1): 5s timeouts. They pass when rerun, and on main a differentcli.test.tstest times out the same way.
- The step-graph, index, lifecycle, operation, flow and declined files together:
Tests 127 passed (127). agent-transcript-live.test.ts:4 passed.
Review round 1 (cf3b3ea1)
- Combinator edges depend on the outcome. Fulfilled
all, rejectedanyand everyallSettledexpand to all members. A rejectedall, a fulfilledanyand everyracekeep only the member that settled them. The outcome is read withutil.inspect; no handler is attached, so an author's unhandled rejection stays visible. - Truncation is carried through the reduction. A step's node keeps whether its own
afterwas incomplete, and a join that reduces against it is markedafterTruncated. afteris bounded on read.isAuthoredStepRecordrejectsafterwith more than 32 entries.- Long-running steps. A step in flight for 30 000 polls (3 times the walk limit) still gives its dependents exact, untruncated
afterlists. The walk stops at the step's own promise and never enters its internal chain; the dependents' walks visit 7 nodes at most.
Overlap with fix/sdk-parallel-local-agents
That branch reworks AuthoredPromiseGraph and authored-flow-lifecycle.ts to fix a CPU stall in the completion gate.
- It adds a
causesOf(asyncId): number[]toauthored-promise-graph.tsthat is identical to the one this PR adds, with the same signature, body and position. When rebasing, keep a single copy. - In
authored-flow-lifecycle.ts, this PR changes onlyregisterCombinator(collecting member ids and onestepGraph.registerAggregatecall),registerOperation/stepEdges, and two lines inclose(). The step-graph logic itself lives in the newauthored-step-graph.ts.
🤖 Generated with Claude Code
[!NOTE]
Medium Risk
Changes promise-lifecycle instrumentation and extends durable journal/index schema, but metadata is optional and does not affect execution or admission; incorrect edges would only mislead graph UI.
Overview
Authored TypeScript flows now emit observational DAG metadata so Cloud can draw named graphs: each indexed step and matching journalSteps entry may include optional label (f.agent / f.hook names, kept whole up to 256 chars or omitted—not f.run commands) and after (transitively reduced causal predecessors, capped at 32 with afterTruncated when incomplete).
The SDK adds AuthoredStepGraph, wired through AuthoredFlowLifecycle and AuthoredFlowOperation at step invocation: it walks the existing promise graph backward (stopping at step frontiers, expanding Promise.all / allSettled / any / race by settlement outcome) and threads the resulting edges into admitted/completed relayflows.authored-step.v1 records. Step ids, admission keys, and resume identity are unchanged; steps with no graph fields keep the old record shape. Docs and unit/live tests cover combinator behavior, walk limits, label safety, and index round-trips.
Reviewed by Cursor Bugbot for commit ea36f070d666ee93f2a7db8e0e692522262d4f01. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Records a human label and causal predecessors for every authored step so Agent Relay Cloud can draw a TypeScript flow's run as a named DAG of parallel agents. Step ids, admission keys, and resume identity are unchanged; the new fields are observational metadata and never gate execution.
Edges and labels
labelis thef.agentname (previously discarded) or thef.hookname, kept whole and omitted — never cut — past 256 characters;f.run,f.llm, andf.humanhave none.afterlists the step's causal predecessors, computed by walking the promise graph back from the invocation context.Promise.all/allSettledexpand to every member only when the aggregate had to wait for all of them (a fulfilledall, a rejectedany);race, a rejectedall, and a fulfilledanyfollow only the member whose settlement settled them.- An aggregate's settlement is read with
inspectwithout attaching a handler, so a rejected aggregate stays an unhandled rejection; the read pinscolors,compact, andsortedexplicitly, which process-wideinspectdefaults could otherwise change. - Walks are memoized per invocation context and capped at 10k nodes; every predecessor entry inspected during reduction counts toward the cap, duplicates included, so a wide fan-in stays bounded. Past the cap, or when a join reduces against an incomplete list, the record says
afterTruncated: trueinstead of guessing. relayflows.authored-step.v1records and rootjournalStepsentries gain optionallabel,after(at most 32), andafterTruncated; a step with neither writes the old shape, and existing readers ignore the new fields.
Diagnostics
- A failed local agent worker closes the journal client the flow shares; subsequent journal requests now name the worker's failure (or the socket's error/close) as the cause instead of failing with a bare "not connected".
Agent Relay sessions
claudesessiona41ce5ec· opened viagh pr create· last active 2026-09-22