Repository navigation
Fix sync-path saturation with bounded wait and 503 Retry-After - #69
Conversation
Implement spec §4.3 sync-path saturation handling. Previously a saturated
pool surfaced `SandboxPoolSaturatedError`, which `runLeaf` folded into a
generic `{status:"failed", reason:"error"}` and the sync `/runs` path
returned as HTTP 200 — no bounded wait, no `503 Retry-After`.
- run-leaf: add a distinct `reason:"saturated"` and map
`SandboxPoolSaturatedError` to it, keeping every other throw as `"error"`.
- classify-outcome: treat `"saturated"` as retryable (like `"error"`) so the
async path is unchanged — work stays queued and drains as leases free.
- knative-server `/runs`: bound-wait with exponential backoff around pool
acquisition, then `503 Retry-After` on timeout (no result record written,
since 503 means "retry", not a terminal failure). Budget/backoff/Retry-After
are env-configurable (`KAGENTI_SYNC_SATURATION_*`).
Closes #55
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
| // request so overrides take effect without a restart (and so tests can shrink the budget). | ||
| function saturationWaitConfig() { | ||
| return { | ||
| waitMs: parseInt(process.env.KAGENTI_SYNC_SATURATION_WAIT_MS ?? "30000", 10), |
There was a problem hiding this comment.
nit (non-blocking): parseInt here has no NaN guard. A malformed env value (e.g. KAGENTI_SYNC_SATURATION_WAIT_MS=abc) makes deadline NaN, so the Date.now() < deadline guard is always false — the bounded wait is skipped and the response is an immediate 503 with Retry-After: NaN. Behavior stays fail-safe (503, no lease leak), so this is not blocking, but a Number.isFinite(...) fallback to the defaults would harden these operator knobs against typos.
Assisted-By: Claude Code
There was a problem hiding this comment.
Good catch — fixed in 619c968. Added an intEnv helper that falls back to the default when a knob is unset or not a finite, non-negative integer, and routed all four KAGENTI_SYNC_SATURATION_* values through it. So a typo like WAIT_MS=abc now reverts to the default budget (bounded wait still runs) instead of yielding a NaN deadline + Retry-After: NaN. Covered by two new tests (malformed budget still bound-waits; malformed Retry-After defaults, never NaN).
Assisted-By: Claude Code
Address PR review nit: parseInt had no NaN guard. A malformed operator knob (e.g. KAGENTI_SYNC_SATURATION_WAIT_MS=abc) produced NaN, poisoning the `Date.now() < deadline` guard (always false) so the bounded wait was skipped and the response was an immediate 503 with `Retry-After: NaN`. Add an `intEnv` helper that falls back to the default when the value is unset or not a finite, non-negative integer, and route all four `KAGENTI_SYNC_SATURATION_*` knobs through it. Tests cover the malformed-budget (still bound-waits) and malformed-Retry-After (defaults, never NaN) cases. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Summary
Implements spec §4.3 sync-path saturation handling (follow-up from P2 — #46, PR #53). Closes #55.
Previously, when every pool pod was at the soft cap,
selectPoolSandboxthrewSandboxPoolSaturatedError, whichrunLeaffolded into a generic{status:"failed", reason:"error"}. The sync/runspath returned that as HTTP 200 — no bounded wait, no503 Retry-After. (The throw is fail-safe: it happens before any lease/converge/agent work, so there's no leak.)Change
flowchart LR A["POST /runs (sync)"] --> B["runLeaf → selectPoolSandbox"] B -->|saturated| C{"within wait budget?"} C -->|yes| D["backoff sleep, retry acquisition"] D --> B C -->|no| E["503 + Retry-After<br/>(no result record)"] B -->|acquired| F["200 + verdict"]harness/src/run-leaf.ts— add a distinctreason:"saturated"toLeafResultand mapSandboxPoolSaturatedErrorto it; every other throw stays"error".harness/src/classify-outcome.ts— treat"saturated"as retryable (like"error"), so the async path is unchanged: work stays queued and drains as leases free (spec §4.3).packages/knative-server/src/server.ts— in the sync/runshandler, bounded-wait with exponential backoff around pool acquisition, then503+Retry-Afteron timeout. No result record is written on 503 (it means "retry", not a terminal failure, so/runs/statuswon't report it as one). Retrying wholerunLeafon saturation is safe —selectPoolSandboxthrows before any lease/converge/agent work, and the preceding steps are idempotent.Config (env, sync path only):
KAGENTI_SYNC_SATURATION_WAIT_MS(30000),_BACKOFF_MS(250),_MAX_BACKOFF_MS(5000),_RETRY_AFTER_S(5).Tests
runLeafmaps saturation →reason:"saturated"(not"error").classifyOutcomekeeps"saturated"retryable (async requeue unchanged)./runsreturns503+Retry-Afterafter the budget, re-attempting acquisition first, and writes no result record./runsretries and returns the verdict once a pod frees.Full suites green locally: harness 134 passed / 3 skipped, knative-server 32 passed; all four packages typecheck clean. No dependency changes.
Assisted-By: Claude Code