Skip to content

feat(embeddings): add token refresh to CodexOAuthEmbeddings; extract CodexAuthManager#1712

Merged
nicoloboschi merged 2 commits into
mainfrom
fix/codex-oauth-embeddings-refresh
May 28, 2026
Merged

feat(embeddings): add token refresh to CodexOAuthEmbeddings; extract CodexAuthManager#1712
nicoloboschi merged 2 commits into
mainfrom
fix/codex-oauth-embeddings-refresh

Conversation

@DK09876
Copy link
Copy Markdown
Contributor

@DK09876 DK09876 commented May 22, 2026

Summary

Builds on #1704 (Codex OAuth embeddings) with three improvements:

  • Token refresh for CodexOAuthEmbeddings: the original implementation read the OAuth token once at construction time and never refreshed it. Long-running processes would silently keep using an expired token until the process was restarted. This adds the same proactive (JWT expiry detection) and reactive (401 retry) refresh logic that CodexLLM already has.

  • CodexAuthManager extraction: rather than duplicating the refresh logic, the shared auth management (token loading, JWT decode, staleness check, atomic file persistence, single-flight refresh) is extracted into a new codex_auth.py. Both CodexLLM and CodexOAuthEmbeddings delegate to it. CodexLLM wraps the sync manager via asyncio.to_thread; CodexOAuthEmbeddings calls it directly from encode().

  • openrouter branch missing dimensions: the new HINDSIGHT_API_EMBEDDINGS_OPENAI_DIMENSIONS env var was wired into the openai provider branch but not openrouter, so it was silently ignored. One-line fix.

Test plan

  • 36 unit tests pass (test_codex_oauth_refresh.py, test_embeddings_openai_batch_size.py) including 2 new tests for proactive and reactive refresh in CodexOAuthEmbeddings
  • End-to-end smoke test confirmed against live OpenAI embeddings API: real vectors returned, proactive refresh fires on stale JWT, reactive refresh fires on 401 and retries successfully
  • openrouter dimensions fix covered by updated test_openrouter_provider_uses_configured_batch_size

🤖 Generated with Claude Code

…hEmbeddings

Extract Codex OAuth auth management into a shared CodexAuthManager class
(codex_auth.py) used by both CodexLLM and CodexOAuthEmbeddings. This gives
CodexOAuthEmbeddings the same token-refresh capability that CodexLLM already
has: proactive refresh (JWT expiry detection before each encode call) and
reactive refresh (401 retry with rotated token).

Also fix the openrouter branch in create_embeddings_from_env() which was
silently ignoring HINDSIGHT_API_EMBEDDINGS_OPENAI_DIMENSIONS.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@DK09876 DK09876 force-pushed the fix/codex-oauth-embeddings-refresh branch from 5ccd020 to 6f0690a Compare May 26, 2026 14:25
@nicoloboschi nicoloboschi merged commit ffa6fbf into main May 28, 2026
2 checks passed
@nicoloboschi nicoloboschi deleted the fix/codex-oauth-embeddings-refresh branch May 28, 2026 09:44
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.

2 participants