Skip to content

Fix sync-path saturation with bounded wait and 503 Retry-After - #69

Merged
pdettori merged 2 commits into
mainfrom
fix/sync-saturation-503-55
Jul 7, 2026
Merged

pdettori merged 2 commits into
mainfrom
fix/sync-saturation-503-55

Conversation

@pdettori

@pdettori pdettori commented Jul 6, 2026

Copy link
Copy Markdown
Member

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, selectPoolSandbox threw SandboxPoolSaturatedError, which runLeaf folded into a generic {status:"failed", reason:"error"}. The sync /runs path returned that as HTTP 200 — no bounded wait, no 503 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"]
Loading
  • harness/src/run-leaf.ts — add a distinct reason:"saturated" to LeafResult and map SandboxPoolSaturatedError to 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 /runs handler, bounded-wait with exponential backoff around pool acquisition, then 503 + Retry-After on timeout. No result record is written on 503 (it means "retry", not a terminal failure, so /runs/status won't report it as one). Retrying whole runLeaf on saturation is safe — selectPoolSandbox throws 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

  • runLeaf maps saturation → reason:"saturated" (not "error").
  • classifyOutcome keeps "saturated" retryable (async requeue unchanged).
  • /runs returns 503 + Retry-After after the budget, re-attempting acquisition first, and writes no result record.
  • /runs retries 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

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]>
Comment thread packages/knative-server/src/server.ts Outdated
// 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),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@pdettori
pdettori merged commit 314f6c6 into main Jul 7, 2026
9 checks passed
@pdettori
pdettori deleted the fix/sync-saturation-503-55 branch July 7, 2026 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync-path saturation: bounded wait + 503 Retry-After per spec 4.3

1 participant