Skip to content

feat(install): inject GH_TOKEN into Codex via [shell_environment_policy.set] — workaround openai/codex#10695 - #368

Merged
joelteply merged 1 commit into
canaryfrom
feat/codex-gh-token-injection
Apr 30, 2026
Merged

joelteply merged 1 commit into
canaryfrom
feat/codex-gh-token-injection

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Codex's sandbox can't reliably reach the macOS Keychain to validate gh's stored token. Symptom from Joel's QA pass: gh auth status flakes between ✓ and X within a single Codex session, airc join trips on the X path even though the token is real and valid. This is the upstream bug at openai/codex#10695 — patch in flight from a contributor (shell handlers don't merge dependency_env into spawned processes).

OpenAI's own maintainer guidance on #10695: "If echo $GH_TOKEN is defined at app launch it's visible to sandboxed tools." And Codex docs confirm [shell_environment_policy.set] is "explicit environment overrides injected into every subprocess" — exactly the right knob.

What this PR does

install.sh now writes a marker-bracketed block to ~/.codex/config.toml after the existing permission-profile setup:

# AIRC-GH-TOKEN-START — managed by install.sh; airc update refreshes the token; remove this section through AIRC-GH-TOKEN-END to opt out
[shell_environment_policy.set]
GH_TOKEN = "<gh auth token output>"
# AIRC-GH-TOKEN-END

After Codex restart, sandboxed gh and airc invocations see GH_TOKEN in env and never depend on the keychain probe. The flake disappears.

Idempotent + self-refreshing: every install.sh run (incl airc update) strips any prior airc-managed block via sed range-delete and rewrites with the current gh auth token output. Token rotation just works.

Composes with user config: uses [shell_environment_policy.set] sub-table form (not inline set = {...}) so it doesn't conflict with user-defined parent keys like inherit or include_only.

Opt-out: AIRC_SKIP_CODEX_TOKEN=1 skips the injection (for users who prefer to manage GH_TOKEN via shell alias / launcher / CI secret).

uninstall.sh: matching cleanup strips the marker-bracketed block when airc is removed.

Trade-offs (documented in the codex README)

Joel's preferences ruled out

  • PATH-shadowing codex ("major interference to man-in-the-middle codex")
  • Manual launch one-liner ("yuck")
  • Shellrc-exporting GH_TOKEN to every shell (security leak)

Tested on this Mac

  • Live install run → block written with real token, Codex starts cleanly
  • Re-run → prior block replaced (no duplicate), token refreshed
  • Uninstall flow → strips block cleanly without touching other config

Test plan

  • Syntax check: bash -n install.sh && bash -n uninstall.sh
  • Live: install.sh on Codex-installed Mac → marker block lands with current token, codex exec starts (rc=0)
  • Idempotency: second install.sh run → block replaced, no duplicate
  • Token rotation: gh auth refresh → next install.sh picks up new token (verified by SHA-comparing the GH_TOKEN value before/after)
  • End-to-end: Joel restarts Codex post-merge, runs /join in airc tab, expect successful mesh join (no token-invalid flake)
  • CI: clean-install jobs pass (no Codex on runners; no-op for them)

Related

🤖 Generated with Claude Code

…cy.set] (workaround openai/codex#10695)

Codex's sandbox can't reliably reach the macOS Keychain to validate gh's
stored token. Symptom: gh auth status flakes between ✓ and X within a
single Codex session, airc join trips on the X path even though the
token is real and valid (Joel hit this on the codex first-encounter
QA pass; openai/codex#10695 is the upstream tracking bug, with a
contributor analysis confirming Codex's shell handlers don't merge
session dependency_env into spawned processes; patch in flight).

OpenAI's own maintainer guidance on #10695: "If echo $GH_TOKEN is
defined at app launch it's visible to sandboxed tools." Codex's
[shell_environment_policy.set] is documented as "explicit environment
overrides injected into every subprocess" — exactly what's needed to
inject GH_TOKEN at session start and bypass the keychain flake.

install.sh now writes a marker-bracketed block to ~/.codex/config.toml:

  # AIRC-GH-TOKEN-START — managed by install.sh; airc update refreshes...
  [shell_environment_policy.set]
  GH_TOKEN = "<gh-auth-token-output>"
  # AIRC-GH-TOKEN-END

Idempotent + token-refreshing: every install.sh run (including airc
update) strips any prior airc-managed block via sed range-delete and
rewrites with the current `gh auth token` output. If gh auth rotates
keys, the next airc update propagates the new token.

Composes cleanly with user-defined [shell_environment_policy] keys at
the parent level (inherit, include_only, etc.) because we use the
sub-table form [shell_environment_policy.set] rather than inline
`set = { ... }` syntax that would conflict.

AIRC_SKIP_CODEX_TOKEN=1 escape hatch for users who'd rather manage
GH_TOKEN themselves (shell alias, separate launcher, etc).

uninstall.sh: matching cleanup strips the marker-bracketed block from
~/.codex/config.toml when airc is removed. Same sed range-delete pattern.

Trade-offs explicitly documented in the codex README:
- Token is plaintext on disk in ~/.codex/config.toml (similar trust
  posture to ~/.codex/auth.json which already holds OpenAI creds).
- When openai/codex#10695 lands a fix, this injection becomes a
  no-op safety net rather than load-bearing workaround.

Joel's stated preferences ruled out:
- PATH-shadowing codex (too invasive — "major interference to
  man-in-the-middle codex")
- Asking the user to type the launch one-liner each time ("yuck")
- Shellrc-exporting GH_TOKEN to every shell (security leak beyond
  airc's scope)

Tested on this Mac: live install run wrote the block with current
gh token, Codex starts cleanly, second install.sh run replaced the
block (no duplicate), uninstall flow strips cleanly.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@joelteply
joelteply merged commit acc7f46 into canary Apr 30, 2026
6 checks passed
@joelteply
joelteply deleted the feat/codex-gh-token-injection branch April 30, 2026 03:18
@joelteply

Copy link
Copy Markdown
Contributor Author

🤖 post-merge live test result: GH_TOKEN injection alone is insufficient — deeper sandbox flake

Joel restarted Codex post-merge, ran `/join`. Outcome:

```
gh CLI is installed but the GitHub token is invalid.
github.com
X Failed to log in to github.com using token (GH_TOKEN)
```

The injected GH_TOKEN IS the same valid keychain token that Claude Code on the same Mac uses successfully right now. So gh saying "Failed to log in using token (GH_TOKEN)" is misreporting a Codex-sandbox network failure as auth failure — same root issue we hit pre-#368, just rebadged from "keyring invalid" to "GH_TOKEN invalid."

Verdict: the token mechanism (keychain vs env) is moot when Codex's sandbox flakily mangles gh's `/user` validation call. Both paths hit the same broken network layer.

What would actually unblock: airc's preflight needs to skip gh-auth-status entirely when Codex env is detected and attempt the gist operation directly. If gh can't validate the token via /user but the actual gist API call works (or at least returns a useful error), proceed instead of dying at preflight. That's an airc-binary change, not a skill text change. Tracked as a follow-up to #367 + #368.

This PR (#368) still ships value as a no-op safety net for when openai/codex#10695 lands the upstream fix — once `dependency_env` propagation works, the env-injection mechanism here will become load-bearing without needing the airc-side skip-preflight. Leaving merged.

For Joel's Carl QA pass: pausing Codex+airc here. Claude Code path remains fully shippable. Distributed mesh research already converged on Codex sandbox flakiness as the root cause; further iteration on our side won't change the upstream constraint.

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