fix(sdk): reuse authored CLI probes for each run so concurrent f.llm never outlives its lease (#561)
Closes #561.
The stale dispatch
authoredWorkerRunner runs the declarative preflight (checkAuthoredFlow)
per authored f.llm/f.agent call, and that preflight real-probes the CLI —
claude auth status and friends — synchronously. Nine concurrent calls
therefore spawned nine blocking probes on the one Node event loop.
WorkerSlots was never the problem: it bounds admission correctly, and
overflow calls do wait before run.start. The damage is done to the calls that
were admitted. While the remaining probes block the loop, the worker cannot
service its dispatch or send a heartbeat, so the kernel sees a lease started and
never renewed. At +30s it journals lease_expired, retries, and the worker's
withWorkerLease throws Agent lease is already expired on the stale dispatch
it finally picks up.
The fix
Give each authored run one cliProbeCache (and one readProjectConfig) shared
across its calls — exactly what declarative preflight already does for a whole
flow, where a single preflight() call caches probes across every step. The
cache is per run and holds refusals as well as passes, keyed on CLI, model and
resolution source (packages/sdk/tests/preflight-run-cache.test.ts pins that).
One probe is paid; the other eight calls resolve from cache and reach the worker
well inside the lease.
Three files change, 22 lines net:
packages/sdk/src/preflight.ts— accept an optionalcliProbeCache; export
CliProbeOutcome.packages/sdk/src/cli/check.ts— thread it throughcheckAuthoredFlow.packages/sdk/src/authored-worker-step.ts— one cache per runner.
Also in this branch: tests/artifact-gates.test.ts declared an absolute
mkdtempSync cwd, which #566 turned into a compile refusal one commit before
#517 added the fixture. main is red at that merge, on this branch and any
other cut from it. The fixture now makes its directory inside the run root and
declares the relative name; no assertion changed.
Acceptance
- A test that runs 9+ concurrent
f.llmcalls against the real kernel
with a fake CLI, nolease_expiredin any child journal, at capacity 1 and
the default —packages/sdk/tests/authored-probe-cache.test.ts. It reads
every child journal throughjournalReadand asserts exactly one
step.attempt.startedper run, and that peak concurrent sessions never
exceedcapacity. - Mutation-verified, re-run at this branch's head. Removing only the
fifthcheckAuthoredFlowargument (evidence/561/mutation.patch) fails all
five cases, and the journal shows the reported shape verbatim — attempt 1
"completionReason":"lease_expired"with"wallclock_ms":30011, a
retry_backoffsleep, attempt 2"completionReason":"success". Restoring
(git diff --exit-codeclean, sha256
b9cd463d9f1ee274175a0b659119262c0ba5b18f1afc11b866c3e0f7f34023b7) returns
5 passed. Captures:evidence/561/mutation-red-at-head.txt,
evidence/561/mutation-green-at-head.txt, walked through in
evidence/561/README.md. - The repro passes locally —
evidence/561/live-default.txtand
evidence/561/live-one.txt, both exit 0, journals asserted by
evidence/561/check-live-journals.py. Captured while this environment had
authenticated Claude. It no longer has it, so this was not re-confirmed
at head;evidence/561/live-one-recheck-unauthenticated.txtis the
unauthenticated re-run, which still shows the mechanism from outside (one
1.04s probe, then eight refusals in 0.01–0.12s). The kernel-backed regression
covers the same behaviour with a fake CLI and needs no credentials. - examples/prompt-lab restores
Promise.all— not done here.
examples/prompt-labdoes not exist in this repository; it lives on
feat/examples-prompt-lab. It needs a follow-up there once this ships
(evidence/561/prompt-lab.txt).
Check status
.relayflow/check.sh is green except for 22 cases in
tests/hosted-extension-{isolation,protocol}.test.ts, which need a real
bubblewrap sandbox. This container denies unprivileged user namespaces and the
AppArmor sysctl cannot be relaxed from inside it, so installing bubblewrap only
changes the error. Unrelated to this change and classified in full, with the
commands, in .relayflow/repair-notes.md.
🤖 Generated with Claude Code
[!NOTE]
Medium Risk
Changes the preflight path on every authored LLM/agent worker step and timing-sensitive lease behavior; mitigated by kernel-backed regression tests and mutation verification, but still core execution infrastructure.
Overview
Fixes worker lease expiry when many concurrent f.llm / f.agent calls each ran synchronous CLI auth/model probes before dispatch. Nine probes on one Node loop blocked heartbeats; admitted steps hit lease_expired (~30s) and stale WorkerLeaseLostError.
Per authored run, a new authoredPreflight path shares one probe cache (and project config read), dedupes in-flight probes, and runs live checks via probeCliAsync so the event loop can serve worker I/O. Probe implementation moves to cli/cli-probe.ts; checkAuthoredFlow accepts an optional probe cache / options bag for the same facts declarative preflight already caches per flow.
Adds authored-probe-cache (and related) tests plus evidence/561/ transcripts (baseline, mutation patch, live repro) documenting the failure shape and verification.
Reviewed by Cursor Bugbot for commit 2dab6ae47d2ae5ef0e6bc2c1b64cdb8b69ede4a8. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Fixes concurrent f.llm/f.agent runs each paying a synchronous CLI auth probe before worker admission. Slow probes blocked the worker's event loop, so issued 30-second leases were never heartbeated and expired, raising Agent lease is already expired on a stale dispatch.
Each authored run now probes once and shares results across its calls. A new authoredPreflight keeps a per-run probe cache (keyed on CLI, model, and resolution source; failures cached too) and reads project config once. Probe logic moved into cli/cli-probe.ts; live-flow probes run asynchronously via probeCliAsync so the loop yields, and asynchronous probes isolate stdin from concurrent calls. checkAuthoredFlow now takes an options bag, and authored option bags without a projectConfig key are still detected and never become config. The synchronous public API is unchanged.
Bug Fixes
artifact-gatesdeclared an absolute tempcwd, which is now a compile refusal; the fixture's directory is created inside the run root with a relative name. The merge withmainresolved the conflict with the revert of thesummary.mdworking file.
Tests
authored-probe-cacheruns 9 concurrent LLM calls against the real kernel with a fake CLI at capacities 1 and 4, asserting nolease_expiredand one probe per run.authored-parallel-llm,authored-preflight,cli-probe, andpreflight-run-cachecover the new cache, sync/async probe drivers, and cache key shape.- Mutation-verified: dropping the cache argument reproduces
lease_expiredon attempt 1 across all five cases;evidence/561/holds the transcripts.