fix(sdk): judge fenced llm replies by their content; resume past a predicate gate
Two defects found while proving the Prompt Lab example end to end on relayflows 2.0.29. Each one stopped a local authored run.
1. A fenced structured f.llm reply failed verification
f.llm(prompt, { output }) with a reply of the form ```json\n{…}\n``` completed verification_failed, even though the value inside matched the schema. The worker already tells the model not to use fences, and it still does sometimes: twice in about 40 calls with claude-sonnet-5. A failed run can't be resumed, so each occurrence costs the whole run.
Fix: llm-worker.ts parses the value inside exactly one surrounding fence (unfenced). A fence is three or more backticks or tildes, closed by a run of the same character at least as long. The schema still judges that value, and both the worker and the kernel still check it. Prose around the JSON still fails, and so do two fenced values. SURFACE.md now says so.
2. A resumed body couldn't get past a predicate .gate(fn)
applyPredicateGate lowers the verdict as printf '%s' '<JSON.stringify(record)>'. The first run stringifies the record as the SDK wrote it: gate, step, verdict, because. On resume, the record comes back from the predicate-gates stream with its keys sorted. The <step>.gate command then differs, and the admission key refuses it:
FAILED [protocol_error] relayflowd could not complete the resume request: run_admission_conflict: run admission key "authored-child:…" is already bound to a different spec
So any authored flow with a predicate gate before an f.human couldn't resume. The existing resume test replayed the record in the order it was written, so it couldn't catch this.
Fix: build the command in a fixed field order. The f.human and f.hook records beside it are built literally in code, so they aren't affected.
Evidence
Every file in evidence/llm-fence-predicate-resume/ comes from verify.sh, run from packages/sdk. The script echoes each literal command and then its complete output and exit code.
mutation-1-fence.txt,mutation-2-predicate.txt: mutation-verified, per AGENTS.md.cpthe source aside →sedrevert the one fix → the named test fails →cpback →cmp(exit 0) → the same test passes.$ npx vitest run tests/worker-transcript.test.ts -t 'fenced reply' # fence fix reverted × … accepts a fenced reply whose content matches the schema … Tests 1 failed | 7 skipped (8) exit=1 $ cmp src/llm-worker.ts …/llm-worker.ts exit=0 $ npx vitest run tests/worker-transcript.test.ts -t 'fenced reply' Tests 1 passed | 7 skipped (8) exit=0 $ npx vitest run tests/authored-agent-artifacts.test.ts -t 'sorted keys' # predicate fix reverted × … a resumed verdict read back with sorted keys lowers the same gate command … Tests 1 failed | 4 skipped (5) exit=1 $ cmp src/authored-flow-executor.ts … exit=0 $ npx vitest run tests/authored-agent-artifacts.test.ts -t 'sorted keys' Tests 1 passed | 4 skipped (5) exit=0thirteen-files-at-main.txtvsthirteen-files-on-branch.txt: the 13 test files that fail on this machine, run first with this change's two source files atmain(git show main:…, restored andcmp'd afterwards), then on the branch. Both runs giveTests 60 failed | 114 passed | 64 skipped (238), so this change adds no failures there. They're environmental: a missingkernel/target/release/relayflowd(ENOENT), the hosted-extension sandbox, and similar.full-suite-on-branch.txt:npm testgivesTests 61 failed | 2759 passed | 72 skipped (2892). That's the same 60, plus one incli-watch.test.ts, a file that passes in both 13-file runs above. It's a load flake in the full run.
npm run typecheck and npm run typecheck:tests pass. CI (packed-consumer, linux-x64-artifact, validate) is green.
Not in this PR
The lease problems from the same proof are tracked separately, because they need diagnosis rather than a one-line fix:
- More than a few concurrent
f.llmcalls lose the run. - A late or stale lease error from the LLM worker is fatal to the whole local run.
🤖 Generated with Claude Code
[!NOTE]
Medium Risk
Touches LLM verification and authored resume admission paths; behavior is narrower (accept one fence) and more deterministic (canonical gate JSON), with targeted tests and mutation evidence.
Overview
Fixes two resume-blocking bugs in local authored runs: structured f.llm output wrapped in a single markdown fence, and predicate .gate(fn) steps after resume.
The LLM worker now strips exactly one surrounding fence (backticks or tildes) via unfenced before JSON.parse and schema checks; prose or multiple fences still fail verification. Predicate gate lowering builds a canonical PredicateRecord with fixed field order so a verdict replayed from the journal with sorted keys produces the same printf gate command as the first run, avoiding run_admission_conflict.
docs/SURFACE.md documents the fence rule. evidence/llm-fence-predicate-resume/ adds mutation runs and suite logs for review.
Reviewed by Cursor Bugbot for commit 2d356ca7fcfb510b7d5a058f5fc773051490d24b. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Fixes two defects that stop authored flows on relayflows 2.0.29: fenced structured f.llm replies failing verification, and resumed runs not getting past a predicate .gate(fn).
Bug Fixes
f.llm(prompt, { output })now judges a reply that is exactly one markdown code fence by the value inside it. A fence opens with three or more backticks or tildes and closes with a run of the same character at least as long; prose around the JSON or two fenced values still fail.- The
<step>.gatecommand is now built in a fixed field order, so a resumed verdict read back with sorted keys lowers the same command as the first run instead of hitting arun_admission_conflict.
Refactors
- Extracts the fence-stripping logic into an
unfencedhelper exported fromllm-worker.ts. - Documents the fence behavior in
docs/SURFACE.mdand adds mutation evidence underevidence/llm-fence-predicate-resume/, regenerated byverify.shwith literal commands.
Agent Relay sessions
claudesessiondf229951· opened viagh pr create· last active 2026-09-23