fix(core): retry transient failures when provisioning the run workspace

Merged
KH
2mo ago
+524-10

The failure

A real run died today with:

{"status":"failed","error":"Service Unavailable","steps":0}

Zero steps. Workspace provisioning runs before step one, so a single 503 from Relaycast killed the whole run and reported an error that tells the user nothing about their flow. The flow was fine — the identical flow passed on the next attempt with no changes.

This is the failure a prospect hits on their first try. It was hit on the Altek demo flow.

Why 503 specifically

Relaycast sheds load with 503 database_overloaded under database pressure — ensureWorkspaceKey returns it via d1OverloadResponse(). That is a transient upstream condition, not a caller error, and it is exactly the shape worth retrying.

POST /v1/workspaces had no retry at all: one blip, whole run dead.

Change

ensureRelaycastApiKey now retries on 5xx and network errors, 4 attempts with linear backoff.

  • 4xx is never retried — that's the caller's fault (bad key, bad request) and retrying only delays a real error.
  • Retry-After is honoured when present, in both the delay-seconds and HTTP-date forms, clamped to 15s so a large or malformed value can't stall a run.
  • The exhausted message now says the service is shedding load rather than implying a problem with the flow — so the next person doesn't start debugging their YAML.

Verification

97 tests pass (file baseline is 92). Five new regression tests, four red against the parent commit:

  • retries a 503 then succeeds
  • retries a dropped connection, not just an HTTP status
  • does not retry a 4xx
  • honours Retry-After, capped
  • gives up after a bounded number of attempts with an actionable message

One note for reviewers

The new tests restore only their own fetch spy. This file has no global mock cleanup, and vi.restoreAllMocks() here tears down the module-level mocks that later describes depend on — that mistake caused 28 unrelated failures before I caught it. Worth knowing if you add fetch-spying tests to this file.

Not covered here

HarnessDriverClient.spawn() in startOrReuseSharedBroker is the other pre-step-one path with no retry. It's a larger change (broker lifecycle, lease handling) and deserves its own PR rather than being bundled into a hotfix for the failure actually observed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YFC87QQoACaBWwonRBWTEb


[!NOTE]
Medium Risk
Changes critical pre-step-one setup paths (Relaycast provisioning and broker spawn) with new retry/backoff behavior; mistakes could delay failures, mask auth errors, or leave runs hanging, though 4xx and abort paths are explicitly guarded.

Overview
Workflow runs were failing with zero steps when Relaycast returned a transient 503 or network blip during pre-step setup (workspace provisioning or broker startup). This change adds bounded retries and clearer failure attribution so a load-shedding blip does not kill an otherwise valid flow.

Workspace provisioning (POST /v1/workspaces) now retries 5xx and network errors (up to 4 attempts) with linear backoff, honors Retry-After (seconds or HTTP-date) clamped to 15s, parses the JSON body inside each attempt (so mid-body drops retry), passes the run abort signal to fetch, and uses abortableDelay so cancellation does not sleep through backoff. 4xx is not retried; exhausted 5xx errors explain that Relaycast is shedding load, not that the flow is wrong.

Broker startup retries transient spawn failures (up to 3 attempts) but not obvious misconfigurations (e.g. missing binary). Failures from both paths are wrapped with [setup:workspace-provisioning] or [setup:broker-startup] via new describeSetupFailure.

Tests add regression coverage for 503/network/4xx, Retry-After clamping, body-drop and stale Retry-After edge cases, abort during delay, stage tags, and broker retry vs no-retry — with per-block fetch spy cleanup so module mocks are not torn down globally.

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


Summary by cubic

Retries transient failures in both pre-step-one Relaycast calls — workspace provisioning and broker startup — so a single 503 or network blip no longer fails a run before it executes a step. Pre-step setup failures now carry a stage tag, so a failed run record says which call actually failed instead of an opaque Service Unavailable.

  • Workspace provisioning retries 5xx and network errors, up to 4 attempts with linear backoff; 4xx is never retried.
  • Honors Retry-After in both delay-seconds and HTTP-date forms, clamped to 15s so a large value cannot stall a run; the header is read only from the current attempt's response, and the body read sits inside the attempt so a mid-transfer drop is retried too.
  • Broker startup retries transient protocol and network failures, up to 3 attempts with linear backoff; misconfigurations are not retried.
  • Backoff delays abort as soon as the run is cancelled, and no further request starts after an abort.
  • Exhausted messages now say Relaycast is shedding load, not that the flow is broken.
  • Twelve regression tests; new fetch-spying tests restore only their own spy — vi.restoreAllMocks() breaks module-level mocks later tests depend on.