Repository navigation
Classify Cloudflare challenges on Codex requests as transient - #2159
Merged
Merged
Conversation
With a ChatGPT subscription login the host daemon posts voice transcription to https://chatgpt.com/backend-api/transcribe, which sits behind Cloudflare bot management. When Cloudflare answers a managed challenge (HTTP 403, cf-mitigated: challenge) even after the cookie retry, the daemon threw the permanent code codex_request_failed with the first 400 characters of the challenge HTML in the message. The server treats that code as non-transient, so it neither retried nor mapped it to a friendly error, and the user saw "HTTP 502: Codex transcription request failed with HTTP 403: <html> <head> ...". Map a Cloudflare challenge to codex_service_unavailable with a clean, actionable message so the server retries once and then returns its 503 transcription_unavailable error. Never append text/html error bodies to Codex error messages. Document the workaround. Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
With a ChatGPT subscription login (
~/.codex/auth.jsonauth_mode: "chatgpt"), the host daemon postscodex/voice transcription tohttps://chatgpt.com/backend-api/transcribe. That endpoint sits behind Cloudflare bot management and answers some networks with a managed challenge (HTTP 403,cf-mitigated: challenge, a "Just a moment..." HTML page). The daemon already retries once with the Cloudflare cookies, but when the retry is challenged too,createCodexHttpErrorfalls through to the permanent codecodex_request_failedand appends the first 400 characters of the challenge HTML to the message. The server'sisTransientInferenceErrordoes not recognise that code, so it makes one attempt out of two and re-throws the daemon error as-is. The user seesHTTP 502: Codex transcription request failed with HTTP 403: <html> <head> <meta name="viewport" ....Issue: #1587. Report: https://get-bb.github.io/reports/issues/1587.html
What changed
apps/host-daemon/src/codex-chatgpt-client.ts:createCodexHttpErrornow maps a Cloudflare challenge response (isCloudflareChallenge) tocodex_service_unavailablewith a clean, actionable message ("chatgpt.com answered with a Cloudflare challenge that bb cannot solve. Retry, or set BB_TRANSCRIPTION to an openai/ model with OPENAI_API_KEY."; the hint namesBB_INFERENCEfor the inference route). It also never appendstext/htmlerror bodies to Codex error messages, so other HTML error pages (Cloudflare block pages and the like) stay out of user-facing errors.codex_service_unavailableis already transient forinferenceCompleteWithFallback(one retry after 250 ms, so a challenged network gets four fetches instead of two) and already maps to the 503transcription_unavailableerror "Voice transcription is temporarily unavailable. Please try again in a moment." The daemon's message with the Cloudflare detail lands in the server's "Voice transcription failed" log entry.docs/configuration.md: documents that subscription-modecodex/transcription can be challenged by Cloudflare and how to route through an API key (codex login --with-api-key, orBB_TRANSCRIPTION=openai/gpt-transcribewithOPENAI_API_KEY).codex_service_unavailableis a code the server already handles, soHOST_DAEMON_PROTOCOL_VERSIONis unchanged.openai/<model>on the server whenOPENAI_API_KEYhappens to be configured. I left that out. Silently spending API credit for a user who chose a subscription route is a product decision, anddocs/configuration.mdsaysOPENAI_API_KEYonly affects explicitopenai/routes.How you verified
New tests in
apps/host-daemon/src/codex-chatgpt-client.test.ts:expected ExpectedCommandDispatchError ... "code": "codex_request_failed"against the expected"code": "codex_service_unavailable"; passes with the fix (two fetches, clean message).: <html><body>Access denied (error 1020)</body></html>; passes with the fix.Commands (run from the committed tree,
git status --porcelainempty):pnpm exec turbo run typecheck test --filter=@bb/host-daemon:Tasks: 6 successful, 6 total;Test Files 46 passed (46),Tests 553 passed (553).Manual repro on my own dev instance using the report's
cf-challenge-preload.mjs(a Node--importpreload that feeds the daemon the real captured Cloudflare challenge response forPOST https://chatgpt.com/backend-api/transcribe):bb voice transcribe tone.mp3printedError: HTTP 502: Codex transcription request failed with HTTP 403: <html> <head> <meta name="viewport" ..., exit 1; preload log showed 2 fetches; server log"attempts":1,"maxAttempts":2.Error: HTTP 503: Voice transcription is temporarily unavailable. Please try again in a moment., exit 1; preload log showed 4 fetches (2 per server attempt); server logVoice transcription failed transiently; using fallback model {"attempt":1,"errorCode":"codex_service_unavailable",...}then"attempts":2, with the Cloudflare message in the logged error. Baseline (preload disarmed, real network) still transcribes, exit 0.Fixes #1587
Independent verification
Verified on a fresh worktree at
065ade267(PR head),origin/mainis an ancestor,mergeable: MERGEABLE.Commands:
git fetch origin main && git fetch origin bb/fix-1587-codex-transcription-403 && git checkout -b verify-1587-r1 FETCH_HEADpnpm install --frozen-lockfile --prefer-offline && pnpm exec turbo run build(18/18 tasks)git checkout origin/main -- apps/host-daemon/src/codex-chatgpt-client.ts docs/configuration.md, thencd apps/host-daemon && pnpm exec vitest run src/codex-chatgpt-client.test.ts -t "Cloudflare challenge as transient|omits HTML error pages"-> 2 failed:expected ... "code": "codex_service_unavailable"received"code": "codex_request_failed"(line 741), andmessage: "Codex transcription request failed with HTTP 403"did not match because the HTML body was appended (line 777).git checkout HEAD -- <same files>, same vitest run -> 16 passed (16).pnpm exec turbo run typecheck test --filter=@bb/host-daemon --force->Tasks: 6 successful, 6 total;Test Files 46 passed (46),Tests 553 passed (553).cf-challenge-preload.mjs(CF_PRELOAD_DIRisolated under~/.bb-dev/scratch/1587-verify/preload) and codexauth_mode: chatgpt:bb voice transcribe tone.mp3exit=0Error: HTTP 503: Voice transcription is temporarily unavailable. Please try again in a moment.exit=1, 4 intercepted daemon fetches (2 server attempts x 1 cookie retry each); server logVoice transcription failed transiently; using fallback model {"attempt":1,"errorCode":"codex_service_unavailable",...}thenVoice transcription failed {"attempts":2,...}with the clean Cloudflare message (no HTML) in the logged error.Review notes: the fix sits in the daemon (response classification is a host-local primitive), reuses the existing
codex_service_unavailablecode that the server already treats as transient and maps to the 503transcription_unavailableerror, so no wire shape or meaning changes and noHOST_DAEMON_PROTOCOL_VERSIONbump is needed.BB_INFERENCE/BB_TRANSCRIPTION,openai/gpt-transcribe, andcodex login --with-api-keyin the message and docs all exist.Residual risks (not blocking): on a network that is challenged every time the user sees only the generic "temporarily unavailable" 503; the actionable hint (API-key route) lives in server logs and
docs/configuration.md. The Cloudflare/HTML error paths now return without consuming the response body, matching the pre-existing discard of the first challenged response infetchChatGpt.