feat(sdk,surface): journaled agent artifacts, artifact_exists gate, predicate gates

Merged
KH
2mo ago
+974-99

Summary

Makes "the agent must have written this file" a journal-honest, replayable check in authored flows. Three pieces, one contract: the worker that spawned the agent measures the artifacts and journals them; gates and AgentResult read the journal, never the disk.

1. Artifacts move into the worker (what changed vs #434)

#434 diffed the cwd inside the authored step, after the journal entry existed — so a gate couldn't read the list and a resume couldn't reproduce it. Now runAgentCli (worker-cli.ts, the one process provably sharing the agent's filesystem) snapshots the cwd before the spawn, content-diffs after, and WorkerCliResult.artifacts lands in the step's journaled output. authored-worker-step.ts reads output.artifacts back; no second scan. agent-artifacts.ts + its unit tests are unchanged; #434's loopback integration test is adapted to the journaled contract.

Honest edges, documented in SURFACE.md and AgentResult: relay transport journals none (agent ran elsewhere); an agent whose final message is a JSON object owns its output shape — it is left byte-identical, so such a step journals no artifacts (gate it on a deterministic check). Injecting a key into author-owned JSON broke summary and json_schema contracts in the existing suite, so that path is deliberately untouched.

2. artifact_exists named gate

.gate({ type: 'artifact_exists', path: 'review/security.md' }) end to end: surface NamedGate, SDK NamedDataGate, validation (gate_path_invalid: relative POSIX, no empty/./.. segments, no NUL), lowering to a deterministic <step>.gate step whose command judges only input.output.artifacts from FLOWS_INPUT, unknown_gate_kind message, preflight coverage, regenerated flows.schema.json. flows check prints it as a kernel exit_code gate.

3. Predicate gates, per SURFACE.md §6

.gate(fn, because?) was refused unsupported_gate although §6 already specified running it as runtime control flow. The executor now runs the closure once, after the step, on the journaled value, and journals the verdict as a lowered <step>.gate deterministic step — so resume/replay read the verdict and never re-run author code. False or throwing → run fails gate_failed naming the step and the reason (flows run/resume report it as a run failure, not protocol_error). One .gate() per step; the function is never serialized; flows check prints no line for it (runtime-only by construction — documented, see "could not do" below).

Examples/docs

examples/pr-review-pipeline uses artifact_exists per lens and a predicate for consensus (workspace annotations dropped — still refused, unchanged); examples/README.md no longer says BLOCKED on the budget header (accepted since #306). docs/SURFACE.md §6 rewritten to describe what now happens.

Evidence — journal of a real run (built CLI, real daemon, --local-agent, wrapper CLI that writes review/security.md)

{"step":"agent-1","completionReason":"success","output":{"artifacts":["review/security.md"],"exit_code":0,"stderr_tail":"","stdout_tail":"reviewed\n"}}
{"step":"agent-1.gate","completionReason":"success","output":{"exit_code":0,"stderr_tail":"","stdout_tail":""}}
{"step":"run-2","completionReason":"success","output":{"exit_code":0,"stderr_tail":"","stdout_tail":""}}
{"step":"run-2.gate","completionReason":"success","output":{"exit_code":0,"stderr_tail":"","stdout_tail":"{\"gate\":\"predicate\",\"step\":\"run-2\",\"verdict\":\"pass\",\"because\":\"exactly one file\"}"}}
{"step":"complete-3","completionReason":"success", ...}

Test plan

  • tests/artifact-gates.test.ts (5): worker artifacts via mocked spawn (created + changed, dotdirs/node_modules skipped, empty list, none for llm); gate validation/refusal kinds; lowering with the command executed against FLOWS_INPUT (present/absent/no-list/JSON-owned output); flows check inspection.
  • tests/agent-artifacts-live.test.ts (3): Cloud's exact argv through the built CLI + real daemon; asserts output.artifacts in the journal, both gate forms pass, artifact_exists miss → step_failed naming agent-1.gate, predicate false → gate_failed with the author reason and "verdict":"fail" journaled.
  • authored-agent-artifacts.test.ts adapted; authored-flow.test.ts now asserts predicate acceptance + one-gate-per-step; preflight.test.ts covers gate_path_invalid.
  • Full packages/sdk suite against a freshly built kernel: 1799 passed, 0 failed (one cli-watch 5 s timeout under load, passes alone; authored-node-runtime skipped: Bun 1.3.14 vs pinned 1.4.0). Surface 34/34. Examples typecheck.
  • packages/schema parity: 76/77 — step-memory.flow.yaml hits bun's 5 s timeout, identically on an untouched main checkout; pre-existing, not this change.

🤖 Generated with Claude Code


[!NOTE]
Medium Risk
Changes authored execution, worker output shape, gate lowering, and Node result verification—core run/resume/replay paths—with broad test coverage but non-trivial behavioral surface area.

Overview
Journal-honest agent file checks and predicate .gate(fn) for authored flows: the worker that spawns the agent now measures cwd changes and journals output.artifacts; AgentResult and gates read that journal entry instead of re-scanning disk.

Adds .gate({ type: 'artifact_exists', path }) end to end (surface, schema, validation, lowering to a deterministic step that checks FLOWS_INPUT.output.artifacts). Predicate .gate(fn, because?) is no longer unsupported_gate: the closure runs once on the completed value, the verdict is appended to a root predicate-gates stream and lowered as <step>.gate, and resume/replay reuse the recorded verdict. Failures surface as gate_failed on run/resume; one gate per step. Concurrent agent runs in the same cwd are serialized around the artifact snapshot window.

Verifier and IPC treat <step>.gate children as non-ordinal journal steps (named gates may live inside the parent spec). pr-review-pipeline is updated to artifact_exists + predicate gates; docs/examples reflect runnable budget headers and §6 behavior.

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


Summary by cubic

Makes "the agent must have written this file" a journal-honest, replayable check: the worker that spawned the agent measures and journals artifacts, and gates read that journal, never the disk. Predicate .gate(fn) gates were previously refused as unsupported_gate; they now run and their verdict is journaled, and the standalone result verifier accepts gate child runs so gated flows no longer fail verification.

Artifacts and artifact_exists

  • Worker snapshots the agent's cwd before the run and journals changed paths in the step output; AgentResult.artifacts reads that entry instead of re-scanning.
  • New .gate({ type: 'artifact_exists', path }) named gate lowers to a deterministic step that checks the journaled list, so flows check can preflight it.
  • Executions sharing a cwd are serialized around the snapshot interval, so one agent's writes are never attributed to a concurrent one.
  • Wrapper CLIs now spawn in the requested cwd, matching the directory that gets scanned.
  • Relay transport and agents whose final message is a JSON object journal no artifacts (documented).

Predicate gates

  • The closure runs once on the journaled value; the verdict is journaled as a lowered <step>.gate step so resume/replay never re-run author code.
  • Verdicts are recorded on the root run before the gate run opens; concurrent gates share one stream read, so a resumed body reuses the recorded verdict and no gate re-runs an already-recorded closure.
  • The gate applies to every operation kind via the lifecycle, including helpers, MCP, and plugin steps.
  • A false or throwing predicate fails the run as gate_failed, naming the step and the author's reason.
  • One .gate() per step; the function is never serialized.
  • Gate runs are verified like any child but do not consume a complete-N ordinal; a named gate lowered inside a child spec must complete too.

Agent Relay sessions

  • claude session a6fc54db · opened via gh pr create · last active 2026-09-17