Skip to content

fix(openai-codex): normalize stale /backend-api/v1 baseUrl to canonical - #67159

Closed
hclsys wants to merge 1 commit into
openclaw:mainfrom
hclsys:fix/codex-base-url-v1-legacy-67131
Closed

hclsys wants to merge 1 commit into
openclaw:mainfrom
hclsys:fix/codex-base-url-v1-legacy-67131

Conversation

@hclsys

@hclsys hclsys commented Apr 15, 2026

Copy link
Copy Markdown

What

Two-line logic change plus tests:

  1. extensions/openai/base-url.ts — isOpenAICodexBaseUrl regex now matches both https://chatgpt.com/backend-api and the legacy https://chatgpt.com/backend-api/v1:

    -return /^https?:\/\/chatgpt\.com\/backend-api\/?$/i.test(trimmed);
    +return /^https?:\/\/chatgpt\.com\/backend-api(?:\/v1)?\/?$/i.test(trimmed);
  2. extensions/openai/openai-codex-provider.ts — normalizeCodexTransport now includes isOpenAICodexBaseUrl(model.baseUrl) in the baseUrl-rewrite condition, so the widened matcher rewrites legacy values to the canonical root:

     const baseUrl =
       api === 'openai-codex-responses' &&
    -  (!model.baseUrl || isOpenAIApiBaseUrl(model.baseUrl))
    +  (!model.baseUrl || isOpenAIApiBaseUrl(model.baseUrl) || isOpenAICodexBaseUrl(model.baseUrl))
         ? OPENAI_CODEX_BASE_URL
         : model.baseUrl;

Why

Fixes #67131.

Older OpenClaw releases persisted https://chatgpt.com/backend-api/v1 as the Codex provider's baseUrl in agent-scoped ~/.openclaw/agents/main/agent/models.json. The Codex responses endpoint is actually /backend-api/codex/responses; requests to /backend-api/v1/codex/responses get Cloudflare-blocked with HTTP 403 + cf-mitigated: challenge, which OpenClaw surfaces as misleading downstream errors (DNS lookup failed, raw HTML challenge, rate-limit style).

PR #66969 already landed a partial fix for the related model.api === undefined regression, but stale /v1 baseUrls survive #66969 because normalizeCodexTransport only rewrites baseUrl when !model.baseUrl || isOpenAIApiBaseUrl(model.baseUrl) — and /backend-api/v1 matches neither: it is NOT api.openai.com/v1 (that matcher), and it was NOT previously recognised as a codex URL (this PR adds that).

The reporter (#67131) independently confirmed the fix by manually editing the persisted baseUrl to /backend-api, which restored working codex turns.

Relationship to #66969

#66969 is OPEN and touches normalizeCodexTransport in the same file but fixes a different symptom (api undefined for users without explicit api: 'openai-responses' in user config). The two fixes compose cleanly:

Neither fix makes the other obsolete; both reach the same useCodexTransport && model.api === 'openai-responses' code path only under mutually-exclusive conditions.

Behavior

baseUrl (input) Before After
"" / undefined rewritten to /backend-api unchanged
https://api.openai.com rewritten to /backend-api unchanged
https://api.openai.com/v1 rewritten to /backend-api unchanged
https://chatgpt.com/backend-api passed through (no-op) passed through (no-op, reference-equal)
https://chatgpt.com/backend-api/v1 kept (broken) rewritten to /backend-api
https://chatgpt.com/backend-api/v2 kept kept
https://chatgpt.com/backend-api/codex kept kept
arbitrary proxy URL kept kept

Test

Added 4 new test cases:

extensions/openai/base-url.test.ts

  • recognizes /backend-api/v1 and /backend-api/v1/ forms (http + https)
  • rejects /backend-api/v2, /backend-api/codex, /api to guard against over-broadening

extensions/openai/openai-codex-provider.test.ts

  • rewrites stale /backend-api/v1 baseUrl to canonical /backend-api, keeps api: 'openai-codex-responses'
  • leaves canonical /backend-api baseUrl unchanged (reference-equal short-circuit in normalizeCodexTransport)

pnpm test -- extensions/openai/base-url.test.ts currently fails on an unrelated pre-existing infra bug (test/non-isolated-runner.ts → Class extends value undefined) that reproduces on main with no edits. Regex logic independently verified against 10 URL variants via isolated node -e.

Risk

Low. Single regex widening + one additional clause in an existing rewrite condition. Canonical and unrelated-path cases preserved. The only behavior change is that stale /backend-api/v1 values — which today cause 403s and misleading errors — get healed on the next model resolve.

@greptile-apps

greptile-apps Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a regression where users who had https://chatgpt.com/backend-api/v1 persisted as their Codex provider baseUrl in ~/.openclaw/agents/main/agent/models.json would hit Cloudflare 403s, because requests were routing to /backend-api/v1/codex/responses instead of /backend-api/codex/responses. The fix widens isOpenAICodexBaseUrl to recognise the legacy /v1 suffix, and adds it to the normalizeCodexTransport baseUrl-rewrite condition so stale values are healed on the next model resolve.

Confidence Score: 5/5

Safe to merge — minimal two-line logic change with correct regex, good test coverage, and no regressions to existing paths.

All findings are P2 or lower. The regex change is narrowly scoped and correctly anchored. The baseUrl-rewrite condition change closes a pre-existing asymmetry with useCodexTransport. The no-op short-circuit for the canonical URL is preserved by value-equality. Tests cover the new paths and negative guards.

No files require special attention.

Reviews (1): Last reviewed commit: "fix(openai-codex): normalize stale /back..." | Re-trigger Greptile

Older OpenClaw releases persisted `https://chatgpt.com/backend-api/v1`
as the Codex provider's baseUrl in agent-scoped
`~/.openclaw/agents/main/agent/models.json`. The Codex responses
endpoint is `/backend-api/codex/responses`; requests to
`/backend-api/v1/codex/responses` are mitigated by Cloudflare with a
403 challenge (`cf-mitigated: challenge`), which OpenClaw surfaces as
misleading downstream errors (DNS lookup failure, rate-limit style).

`openclaw#66969` fixed the `model.api === undefined` regression, but a stale
`/v1` baseUrl survives that fix because `normalizeCodexTransport`
only rewrites baseUrl when `!model.baseUrl || isOpenAIApiBaseUrl`
matches — and the stale `/v1` variant matches neither.

Fix:
- Extend `isOpenAICodexBaseUrl` to accept both the canonical
  `/backend-api` and the legacy `/backend-api/v1` form (and their
  trailing-slash variants).
- In `normalizeCodexTransport`, rewrite baseUrl to the canonical
  `OPENAI_CODEX_BASE_URL` when the stored value matches either form,
  so the stale `/v1` value is healed on the next model resolve.

Behavior preserved for:
- canonical `/backend-api` baseUrl: rewrite is a no-op (returned model
  is reference-equal to the input when no fields change).
- unrelated paths (`/backend-api/v2`, `/backend-api/codex`, `/api`,
  arbitrary proxies): still rejected.
- non-openai-codex providers: unaffected.

Fixes openclaw#67131
@hclsys

This comment was marked as low quality.

ngutman added a commit that referenced this pull request Apr 16, 2026
…nd discovery (#67635)

Merged via squash.

Supersedes:
- #66969 by @saamuelng601-pixel
- #67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
@ngutman

ngutman commented Apr 16, 2026

Copy link
Copy Markdown
Member

Thanks for the original fix work here — this was helpful in pinning down the stale Codex /backend-api/v1 normalization issue.

We ended up landing the consolidated follow-up in #67635, which merged this fix together with the related missing-api transport normalization path so both failure modes are covered together.

Also credited you in the merged PR / merge commit. Thank you again.

ayteks pushed a commit to ayteks/openclaw that referenced this pull request Apr 16, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
xudaiyanzi pushed a commit to xudaiyanzi/openclaw that referenced this pull request Apr 17, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
kvnkho pushed a commit to kvnkho/openclaw that referenced this pull request Apr 17, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
Mquarmoc pushed a commit to Mquarmoc/openclaw that referenced this pull request Apr 20, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
Heetisk pushed a commit to Heetisk/openclaw that referenced this pull request Sep 28, 2026
…nd discovery (openclaw#67635)

Merged via squash.

Supersedes:
- openclaw#66969 by @saamuelng601-pixel
- openclaw#67159 by @hclsys

Co-authored-by: saamuelng601-pixel <[email protected]>
Co-authored-by: hclsys <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: penai-codex can still fail after #66969 when models.json persists chatgpt baseUrl with /backend-api/v1

2 participants