Skip to content

feat: turn-level timeout — bound Codex turn await (port from openai#376, closes #25)#26

Merged
axisrow merged 1 commit into
mainfrom
feat/turn-timeout-376
Jul 20, 2026
Merged

feat: turn-level timeout — bound Codex turn await (port from openai#376, closes #25)#26
axisrow merged 1 commit into
mainfrom
feat/turn-timeout-376

Conversation

@axisrow

@axisrow axisrow commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Closes fork#25. Port of @russjhammond's openai#376.

If Codex CLI can't reach the OpenAI API, captureTurn hangs forever — rejectCompletion was dead code. This PR wires it to exitPromise + adds a deadline (Promise.race). Foreground budget 110s (below Bash ceiling), background 600s, configurable via CODEX_TURN_TIMEOUT_MS or --turn-timeout-ms. 166 tests, 162 pass / 4 pre-existing.

Closes fork#25. If Codex CLI can't reach the OpenAI API (network down, DNS
misconfigured), captureTurn's 'await state.completion' hangs forever — no
turn-level timeout, rejectCompletion was dead code (zero call sites).

Ported from @russjhammond's openai#376:
- Wire rejectCompletion to client.exitPromise: app-server death after
  startRequest rejects the await immediately instead of hanging.
- Add deadline via Promise.race: hard per-turn budget (default 600s bg,
  110s foreground — just below Claude Code Bash tool ceiling).
- resolveTurnTimeoutMs reads CODEX_TURN_TIMEOUT_MS at CALL time, not import
  time (companion sets env after import — import-time read was inert).
- applyForegroundTurnBudget: foreground commands get 110s budget so a
  stalled turn returns a structured error instead of being SIGKILLed.
- --turn-timeout-ms flag on review/adversarial-review/task for manual override.

Verified: 166 tests, 162 pass / 4 pre-existing (unchanged).

Refs fork#25, openai#49, openai#489, openai#376 (@russjhammond).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@axisrow

axisrow commented Jul 20, 2026

Copy link
Copy Markdown
Owner Author

Cycle 1 triage: Codex found 3 extensions (not bugs — the port is faithful and correct). Claude APPROVE (all 5 concerns verified: exitPromise wiring no race, deadline no leak, env parsing correct, fg/bg split correct, edge cases handled).

Codex findings are real but all are FUTURE improvements:

  1. HIGH: timed-out turn keeps running in broker (no turn/interrupt on deadline) — follow-up
  2. HIGH: deadline armed after turn/start response (stalled turn/start not covered) — follow-up
  3. MEDIUM: --turn-timeout-ms ignored for background — follow-up

The port solves the stated problem (hang forever → bounded timeout with structured error). Extensions tracked separately. Merge ready.

@axisrow
axisrow merged commit 4f1ec56 into main Jul 20, 2026
@axisrow
axisrow deleted the feat/turn-timeout-376 branch July 20, 2026 12:39
axisrow added a commit that referenced this pull request Jul 20, 2026
…y guards)

Simplify review of PR #26 found exitRaceSettled redundant: JS promises silently
ignore any settle after the first, so rejectCompletion on an already-settled
promise is a no-op. state.completed is the load-bearing guard (protects against
late-arriving exit after normal completion); exitRaceSettled was the weaker half.

Skipped: S1 (options.turnTimeoutMs dead code — altitude-AGNT recommends threading
the value through options instead of env side-channel, but that's a refactor
changing 4 signatures, not a simplify); S3 (handleTask missing background guard —
already safe via early-return at line 882); S4 (Number.isFinite duplication —
3 lines, not worth a lib/timing.mjs module).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
axisrow added a commit that referenced this pull request Jul 20, 2026
…st, thread through options (closes #27)

Three gaps from Codex cycle-review of PR #26:

1. Timed-out turn not interrupted: deadline rejected the await but
   turn/interrupt was never sent. The broker kept executing a write-capable
   turn after it was reported failed. Fix: catch deadline rejection,
   best-effort interruptAppServerTurn(cwd, {threadId, turnId}), re-throw.

2. Deadline armed after turn/start response: a stalled turn/start (app-server
   alive but not responding) was unbounded. Fix: arm the deadline BEFORE
   startRequest, race the entire lifecycle (startRequest + completion) against
   it. If the deadline fires during startRequest, there's no turnId to
   interrupt, but the process no longer hangs.

3. --turn-timeout-ms ignored for background: applyForegroundTurnBudget was
   foreground-only (env side-channel gated by if(!options.background)).
   Replaced with options threading: resolveTurnTimeoutMsFromOptions resolves
   the value from the flag/env/default (foreground=110s, background=600s),
   passes it through buildTaskRequest → handleTaskWorker → executeTaskRun →
   runAppServerTurn → captureTurn → resolveTurnTimeoutMs. Deleted
   applyForegroundTurnBudget (env side-channel gone). Env fallback
   (CODEX_TURN_TIMEOUT_MS) remains for operator-configured overrides.

Also threads cwd into captureTurn options (needed by the interrupt path).

Verified: 166 tests, 162 pass / 4 pre-existing (unchanged).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
axisrow added a commit that referenced this pull request Jul 20, 2026
…st, thread through options (closes #27) (#28)

Three gaps from Codex cycle-review of PR #26:

1. Timed-out turn not interrupted: deadline rejected the await but
   turn/interrupt was never sent. The broker kept executing a write-capable
   turn after it was reported failed. Fix: catch deadline rejection,
   best-effort interruptAppServerTurn(cwd, {threadId, turnId}), re-throw.

2. Deadline armed after turn/start response: a stalled turn/start (app-server
   alive but not responding) was unbounded. Fix: arm the deadline BEFORE
   startRequest, race the entire lifecycle (startRequest + completion) against
   it. If the deadline fires during startRequest, there's no turnId to
   interrupt, but the process no longer hangs.

3. --turn-timeout-ms ignored for background: applyForegroundTurnBudget was
   foreground-only (env side-channel gated by if(!options.background)).
   Replaced with options threading: resolveTurnTimeoutMsFromOptions resolves
   the value from the flag/env/default (foreground=110s, background=600s),
   passes it through buildTaskRequest → handleTaskWorker → executeTaskRun →
   runAppServerTurn → captureTurn → resolveTurnTimeoutMs. Deleted
   applyForegroundTurnBudget (env side-channel gone). Env fallback
   (CODEX_TURN_TIMEOUT_MS) remains for operator-configured overrides.

Also threads cwd into captureTurn options (needed by the interrupt path).

Verified: 166 tests, 162 pass / 4 pre-existing (unchanged).

Co-authored-by: axisrow <axisrow@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant