fix(observer): project each run into its channel and scope the link to it

Merged
KH
2mo ago
+1213-49

Problem

The Observer: link flows run printed opened an arbitrary workspace channel with nothing about the flow:

  1. No run published anything to Relaycast — only agent communication was projected.
  2. The token was workspace-wide, so the dashboard opened whichever channel listed first.
  3. The link was minted and printed only after the run finished.

Reproduced before the change: flows run testdata/hello-deterministic.flow.yaml completed and printed a link; listing the workspace's channels afterwards showed no channel for the run.

Change

  • kernel — run.start {watch: true} streams the new run's entries on the starting connection from run.spawned on. The watcher is registered before the first append (Engine::start_observed), so every entry arrives once and there is nothing to replay. This is the only way to observe a run whose id the caller does not yet know (YAML runs block in run.start). Opt-in; unchanged without the flag. kernel/DESIGN.md updated.
  • sdk — run-projection.ts publishes run start, every step transition and the terminal outcome into wf-<runId> (the channel agent communication already uses). Each message carries the run snapshot under metadata.relayflow (version 1) for AgentWorkforce/relaycast#450 to draw. journal-projection.ts folds the journal stream for YAML runs; authored runs use the executor's progress events plus a new onRunStarted hook. Resume uses run.watch, and replayed history updates the snapshot without re-posting.
  • cli — cli/observer-session.ts scopes the observer token to the run's channel (channel_names, no DMs) and prints Observer: on stderr as soon as the run exists, then on stdout after RUN as before.
  • Fails open (RFC-0001 decision 7 — a projection, not the record). A projection or mint failure produces one [observer] line and never touches the run. A daemon that predates watch refuses the field while decoding, before any run exists, so the CLI starts again without it and says the run was not projected. A refused run.watch on resume is ignored.
  • tests — tests/isolate-workspace.ts (vitest setup) points AGENT_RELAY_HOME at an empty dir and clears RELAYCAST_WORKSPACE_KEY. Without it, any developer logged in with agent-relay had SDK tests minting real tokens — and, with this change, publishing into their real workspace. That also pushed two live-CLI test files past vitest's 5s timeout.

Evidence

Live run against production cast.agentrelay.com. The channel afterwards (GET /v1/channels/wf-01m37ws2v3pbxb7y4zc4qx7hv1/messages):

▶ hello-deterministic started · 2 steps · run 01M37WS2V3PBXB7Y4ZC4QX7HV1 | running [('greet','pending'),('shout','pending')]
○ greet (deterministic) 0.00s | running [('greet','running'),('shout','pending')]
✓ greet (deterministic) 0.01s completionReason: success | running [('greet','completed'),('shout','pending')]
○ shout (deterministic) 0.00s | ...
✓ shout (deterministic) 0.00s completionReason: success | ...
■ hello-deterministic completed · completionReason: success | completed [...]

(Start lines now read started.) The minted token lists exactly one channel: ['wf-01m37ws2v3pbxb7y4zc4qx7hv1'].

Link timing. A roughly 15s fan-out/fan-in flow printed Observer: on stderr at 1.78s.

Live dashboard. Local engine and dashboard from relaycast#450, plus this branch: one page session went 0/4 → 1/4 → 2/4 → 3/4 → 4/4 steps.

Mutation checks (each: revert the change, run the test, capture the failure, restore byte-identically with cmp, re-run):

  • watch sent as false → streams the run into wf-<runId>…: AssertionError: expected undefined to be true / restored: 1 passed
  • old-daemon fallback removed → starts again without watch…: AssertionError: expected 1 to be +0 / restored: 1 passed
  • kernel if false && params.watch → run_start_with_watch_streams_every_entry_once_before_the_result: test result: FAILED. 0 passed; 1 failed / restored: 2 passed

Suites

  • sh ../ops/cargo.sh test --workspace --no-fail-fast → cargo_exit=0, 26 test binaries all ok.
  • SDK npx vitest run: Tests 34 failed | 2928 passed | 64 skipped. All 34 are in authored-node-runtime, babysitter-native-extension, hosted-base-snapshot, hosted-extension-isolation and hosted-extension-protocol. Those same files fail on a clean main worktree on this machine (Tests 35 failed | 61 passed across the set), so they predate this PR. live-kernel and direct-input pass with the isolation fix.
  • Cost: hello-deterministic exits in 1.32s with the observer on vs 0.52s with FLOWS_NO_OBSERVER=1, while the last messages are sent (bounded at 5s).

Found, not fixed here

  • Kernel: the independent steps lint and test ran serially (test routed at 18.06s, after lint completed). test's step.attempt.started is stamped 8.03s, so its wallclock_ms is 24055 for a sleep 14.
  • scripts/run-workflow.sh drives the v1 relayflows runner and still prints a workspace-wide link.

🤖 Generated with Claude Code


[!NOTE]
Medium Risk
Adds an opt-in kernel protocol field and best-effort external Relaycast publication, but execution and admission remain unchanged when observation fails or is disabled.

Overview
flows run / flows resume now push a live run snapshot into Relaycast channel wf-<runId> and mint an ot_live_ link scoped to that channel (not the whole workspace). The link can appear on stderr as soon as the run id exists; --no-observer-link / FLOWS_NO_OBSERVER=1 still disable projection and minting.

Kernel: run.start accepts optional watch: true, streaming journal entry events on the starting connection from run.spawned onward. Engine::start_observed registers a pre-append hook so new runs need no replay; idempotent admission replays then watches the same run. Failed starts unwind the watcher; observation errors do not affect admission or execution.

SDK / CLI: run-projection and journal-projection publish step transitions and terminal status under metadata.relayflow (v1). YAML runs use run.start {watch} (with a fallback when an old daemon rejects watch); resume attaches run.watch and folds replayed history without re-posting stale terminal facts. Authored roots use onRunStarted plus executor progress. createObserverSession replaces the pre-run workspace-wide mint with channel-scoped tokens and bounded drain/cleanup of the session publisher agent.

Tests / docs: Vitest isolate-workspace prevents tests from hitting real Relaycast workspaces; new kernel and projection tests cover watch ordering, admission replay, and fail-open behavior. docs/OBSERVER-RUN-PROJECTION.md documents the producer contract.

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


Summary by cubic

Fixes the observer link flows run printed, which opened an arbitrary workspace channel with nothing about the flow: the token was workspace-wide, nothing projected the run, and the link only appeared after the run finished. Now each run projects its lifecycle into its wf-<runId> channel and the link is scoped to that channel, printed on stderr as soon as the run exists.

  • Kernel: run.start {watch: true} streams the new run's entries on the starting connection from run.spawned, registered before the first append so there is nothing to replay. This is the only way to observe a run whose id the caller does not yet know.
  • SDK: a run projection publishes run start, step transitions, and terminal outcome into wf-<runId>, each message carrying the run snapshot under metadata.relayflow (v1). YAML runs fold the journal stream (epoch summaries reset step state); authored runs use the executor's progress events plus a new onRunStarted hook. Publication goes through a per-session publisher agent, retired history-preserving after its queue drains — including when the create response was lost.
  • CLI: the observer token is scoped to the run's channel (no DMs) and the link prints on stderr as soon as the run exists, then on stdout after RUN as before.
  • Resume keeps the watcher attached through a parked initial response; replayed history folds into the snapshot without re-publishing, and an old terminal fact cannot close the resumed projection.
  • Fails open: a projection or mint failure produces one [observer] line and never touches the run. A daemon that predates watch refuses the field while decoding, before any run exists, so the CLI starts again without it and prints that the run was not projected. A refused run.watch on resume is ignored.
  • Tests: tests/isolate-workspace.ts points AGENT_RELAY_HOME at an empty dir and clears RELAYCAST_WORKSPACE_KEY, so SDK tests never mint real tokens or publish into a developer's real workspace. docs/OBSERVER-RUN-PROJECTION.md records the design and its fail-open boundaries.

Agent Relay sessions

  • claude session 110a8f49 · opened via gh pr create · last active 2026-09-24
  • codex session 01a0cb2f · contributor · pushed commits · last active 2026-09-24