Skip to content

codex_runtime.py unconditionally sets CODEX_API_KEY, which makes the Codex CLI discard a valid subscription auth.json #1971

Closed

Description

@rservers

Summary

codex_runtime.py (_execute_codex) sets both OPENAI_API_KEY and CODEX_API_KEY unconditionally from the same resolved value (source comment: "some Codex builds also read CODEX_API_KEY... Defensive"). However, the mere presence of CODEX_API_KEY flips the Codex CLI into API-key auth mode, silently discarding a valid subscription auth.json. Result: subscription-authenticated Codex agents cannot complete any /task dispatch through Trinity, even though the same auth.json works perfectly when the CLI is invoked directly.

Environment

  • Trinity OSS (self-hosted evaluation deployment, Docker Compose)
  • Codex CLI inside the agent container, authenticated via a valid subscription auth.json mounted under CODEX_HOME
  • No real OPENAI_API_KEY available by design (subscription auth only)

Repro (environment isolation)

With CODEX_HOME pointing at the mounted config:

  1. Without CODEX_API_KEY (only a placeholder OPENAI_API_KEY set): the Codex CLI uses the auth.json subscription token and succeeds — real completion returned.
  2. With CODEX_API_KEY set (with or without OPENAI_API_KEY): the CLI ignores auth.json entirely, attempts true API-key auth against api.openai.com, receives 401 Unauthorized: Incorrect API key provided, retries 5x, then fails the turn.

Since Trinity's dispatch path always injects CODEX_API_KEY, case 2 is the only reachable behavior through /task — the agent 503s on every dispatch. The only successful Codex completion we obtained was via a manual docker exec invocation that deliberately omitted CODEX_API_KEY, bypassing Trinity's dispatch entirely.

Suggested fix

Only set CODEX_API_KEY when a real API key is actually configured (or drop the defensive duplicate entirely). A one-line conditional around the injection restores subscription-auth operation without affecting API-key users.

Secondary observation (non-fatal)

Trinity's relocation of CODEX_HOME under $TMPDIR trips a Codex CLI safety heuristic: WARNING: ... Refusing to create helper binaries under temporary dir. Execution continues, so this is cosmetic — noted only because it surfaces in the same code path.

Happy to provide further detail from our evaluation notes if useful.

Activity

  1. added
    type-bug
    priority-p2
    theme-reliability
    status-ready
    complexity-low
    on Aug 4, 2026
  2. added
    status-in-progress
    and removed
    status-ready
    on Aug 4, 2026
  3. self-assigned this
    on Aug 4, 2026
  4. dolho commented on Aug 4, 2026

    @dolho
    Contributor

    /claim — picking this up. Branch: fix/1971-codex-api-key, off dev.

  5. github-actions commented on Aug 4, 2026

    @github-actions

    @dolho This issue is already assigned to dolho. Use /unclaim first if you want to take over.

  6. dolho commented on Aug 4, 2026

    @dolho
    Contributor

    PR: #1980

    Took the CODEX_API_KEY fix as filed, and also fixed a second defect your repro exposes but that isn't in the issue: your working case is "only a placeholder OPENAI_API_KEY set" — that placeholder is load-bearing, because _execute_codex raised 503 before invoking the CLI whenever no API key was found, and a ChatGPT-plan container has no API key by design. So fixing only the reported half would have left subscription agents working provided they knew to set a fake key, which isn't the goal you described. The gate now accepts a subscription auth.json under CODEX_HOME, and the 503 text names both remedies instead of only "inject OPENAI_API_KEY" — that wording is what pointed you at the placeholder in the first place.

    Detection is deliberately existence + non-empty only: validating the token is the CLI's job, and a stale auth.json should surface as the CLI's own auth error rather than a Trinity 503 falsely claiming no credentials are configured.

    The secondary observation (Refusing to create helper binaries under temporary dir) is not fixed here — you're right that it's cosmetic, and moving CODEX_HOME off $TMPDIR would unpick the #1098 arrangement that keeps Codex state out of the git-tracked workspace. Worth its own issue; happy to file one if you'd like it tracked.

    Your evaluation notes on the environment isolation were the decisive detail — the presence-vs-value distinction is not something the code comment suggested, and it's what made this diagnosable. Thank you.

  7. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 4, 2026
  8. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions