fix: recover Codex OAuth refresh after shared auth rotation - #3804
Conversation
Strix Security ReviewNo security issues found. Updated for Reviewed by Strix |
|
I ran into this race condition in hindsight where it got hung up and failed to auth due to consumed refresh token, so I tasked my agent with tracking down and fixing the cause. It found this race condition problem and patched it, so I thought I would share it. It happened to find a couple of little regression issues in a couple of places where My Hermes agent is sharing codex auth with hindsight by bind mounting .codex directory into hindsight container so that I don’t have to install and auth codex separately in the hindsight container. I think this is also why I had the fun of running into this race condition in the first place, but it had been working great for a while until I hit this snag, so this is not a particularly high probability race but can/does occur. |
Summary
This hardens Codex OAuth refresh handling for long-lived Hindsight processes that share Codex CLI auth state with other processes.
When a refresh request fails because the refresh token was already rotated elsewhere, Hindsight now re-reads
auth.jsonunder the shared lock and recovers when possible:This also adds regression coverage for the refresh-token reuse race and ensures Codex OAuth embedding tests are isolated from developer or CI
CODEX_HOMEenvironment settings.Test Plan
uv run pytest -n0 tests/test_codex_oauth_refresh.py tests/test_codex_home_env.py tests/test_codex_request_headers.py tests/test_embeddings_openai_batch_size.py -quv run ruff check hindsight_api/engine/providers/codex_auth.py tests/test_codex_oauth_refresh.pyuv run ruff format --check hindsight_api/engine/providers/codex_auth.py tests/test_codex_oauth_refresh.pyuv run ty check hindsight_api/engine/providers/codex_auth.pygit diff --check