ductor icon indicating copy to clipboard operation
ductor copied to clipboard

[Bug]: Codex usage-limit / turn.failed errors are masked by misleading "thread not found" stderr

Open Ilinvic opened this issue 7 months ago • 1 comments

What happened?

When the Codex CLI returns a turn.failed event (e.g. usage limit reached, model error), ductor shows a misleading user-facing message:

Session Error
───
[gpt-5.5] Something went wrong.
Your session is preserved — just send the message again.
Use /new to start fresh.

Detail: 2026-04-26T13:05:20.009848Z ERROR codex_core::session: failed to record rollout items: thread 019dc906-… not found

The thread … not found line is a downstream artifact of the failed turn (Codex tries to write a rollout for a session that was never registered because the turn failed first). The real cause — for example "You've hit your usage limit. Try again at 6:00 PM." — is delivered by Codex on stdout in the JSON stream, but ductor never surfaces it.

Steps to Reproduce

  1. Drive any Codex usage account up to its limit (or otherwise force a turn.failed event with a clear error message).
  2. Send a message via Telegram on the affected provider/model.
  3. Observe the user-facing "Session Error" with Detail: … thread … not found.

Manual reproduction confirms Codex itself emits the proper error:

$ codex exec --json --color never --dangerously-bypass-approvals-and-sandbox \
    --skip-git-repo-check --model gpt-5.5 -- "test"
{"type":"thread.started","thread_id":"019dc9ee-…"}
{"type":"turn.started"}
{"type":"error","message":"You've hit your usage limit. … try again at 6:00 PM."}
{"type":"turn.failed","error":{"message":"You've hit your usage limit. …"}}
2026-04-26T13:16:06Z ERROR codex_core::session: failed to record rollout items: thread 019dc9ee-… not found

Expected Behavior

User sees the actual cause from Codex, e.g.:

[gpt-5.5] Usage limit reached. Try again at 6:00 PM.

…or at minimum has the real error.message shown as Detail: instead of the rollout-recording artifact.

Root cause (analysis)

Two compounding issues:

1. _StreamState drops the parsed error event — ductor_bot/cli/codex_provider.py:42-56

parse_codex_stream_event already converts turn.failed into a ResultEvent(result=msg, is_error=True) (codex_events.py:200-204). But _StreamState.track() only accumulates AssistantTextDelta, ignoring ResultEvent. So the real message is yielded once and then forgotten.

Then _codex_final_result (codex_provider.py:266-272) builds the final user-facing payload as:

if result.process.returncode != 0:
    error_detail = stderr_text or "\n".join(accumulated_text) or "(no output)"

Since stderr_text always contains the thread … not found line in this scenario, it wins — even though a clean error message was already in the stream.

2. classify_cli_error doesn't recognize Codex's wording — ductor_bot/text/response_format.py:31

_RATE_PATTERNS = ("429", "rate limit", "too many requests", "quota exceeded")

Codex says "You've hit your usage limit" / "Upgrade to Pro" — neither pattern matches, so even after fix #1 the user wouldn't get the friendly localized hint, only the raw English line as Detail:.

Suggested Fix

  1. In _StreamState, also record the latest ResultEvent.result when is_error=True, e.g. as last_error_message.
  2. In _codex_final_result, prefer that captured message: last_error_message → "\n".join(accumulated_text) → stderr_text → "(no output)".
  3. Extend _RATE_PATTERNS with "usage limit", "upgrade to pro", "hit your" so the rate-limit hint fires for Codex's exact wording.

Happy to send a PR if helpful.

Operating System

Linux

Environment

Local (pip install)

Which LLM provider?

Codex

ductor version

0.16.1

Before submitting

  • [x] I have searched existing issues for duplicates.
  • [x] I am running the latest version.

Ilinvic avatar Apr 26 '26 14:04 Ilinvic

Thanks @Ilinvic — your root-cause analysis was spot on and made this trivial to fix. All three points implemented exactly as suggested and shipped in v0.16.2:

  1. _StreamState now captures the most recent ResultEvent(is_error=True) message into last_error_message.
  2. _codex_final_result prefers it: last_error_message → accumulated_text → stderr_text → "(no output)" (also bumped accumulated above stderr so partial output beats stderr noise).
  3. _RATE_PATTERNS extended with "usage limit", "upgrade to pro", "hit your" so the rate-limit hint fires for Codex's wording.

11 new tests added (6 for _StreamState, 2 for the priority chain, 3 for the new rate patterns). Closing — thanks again for the great writeup. 🙏

PleasePrompto avatar Apr 29 '26 05:04 PleasePrompto