Repository navigation
fix(auth): invalidate stale runtime auth snapshots when auth files change - #68866
alithnar-bot wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes a long-lived gateway process serving stale in-memory auth snapshots after a browser OAuth re-login rotates credentials on disk. It fingerprints each The logic correctly handles missing files (both sides returning Confidence Score: 5/5Safe to merge — the fix is minimal, well-targeted, and covered by new regression tests. No P0 or P1 findings. The sole comment is a P2 naming suggestion for a private helper. The mtime-invalidation logic is correct, error handling is sound, and the tests exercise both invalidation paths with a reliable mtime bump strategy. No files require special attention. Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/auth-profiles/runtime-snapshots.ts
Line: 21-27
Comment:
**Misleading helper name**
`readAuthStoreMtimeMs` is used for both `auth-profiles.json` (auth store) and `auth-state.json` (auth state) paths, but the name implies it's specific to the auth store. A more generic name like `readFileMtimeMs` or `readMtimeMs` would prevent confusion for future readers.
```suggestion
function readFileMtimeMs(pathname: string): number | null {
try {
return fs.statSync(pathname).mtimeMs;
} catch {
return null;
}
}
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(auth): invalidate stale runtime auth..." | Re-trigger Greptile |
| function readAuthStoreMtimeMs(pathname: string): number | null { | ||
| try { | ||
| return fs.statSync(pathname).mtimeMs; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
There was a problem hiding this comment.
readAuthStoreMtimeMs is used for both auth-profiles.json (auth store) and auth-state.json (auth state) paths, but the name implies it's specific to the auth store. A more generic name like readFileMtimeMs or readMtimeMs would prevent confusion for future readers.
| function readAuthStoreMtimeMs(pathname: string): number | null { | |
| try { | |
| return fs.statSync(pathname).mtimeMs; | |
| } catch { | |
| return null; | |
| } | |
| } | |
| function readFileMtimeMs(pathname: string): number | null { | |
| try { | |
| return fs.statSync(pathname).mtimeMs; | |
| } catch { | |
| return null; | |
| } | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/auth-profiles/runtime-snapshots.ts
Line: 21-27
Comment:
**Misleading helper name**
`readAuthStoreMtimeMs` is used for both `auth-profiles.json` (auth store) and `auth-state.json` (auth state) paths, but the name implies it's specific to the auth store. A more generic name like `readFileMtimeMs` or `readMtimeMs` would prevent confusion for future readers.
```suggestion
function readFileMtimeMs(pathname: string): number | null {
try {
return fs.statSync(pathname).mtimeMs;
} catch {
return null;
}
}
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2f8dbabe26
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| export function hasRuntimeAuthProfileStoreSnapshot(agentDir?: string): boolean { | ||
| return runtimeAuthStoreSnapshots.has(resolveRuntimeStoreKey(agentDir)); | ||
| return Boolean(getFreshRuntimeAuthProfileStoreSnapshot(agentDir)); |
There was a problem hiding this comment.
Preserve runtime snapshots when persisting updated stores
hasRuntimeAuthProfileStoreSnapshot now calls the freshness check, so it returns false (and evicts the entry) as soon as auth-profiles.json/auth-state.json mtimes change. In saveAuthProfileStore (src/agents/auth-profiles/store.ts), the files are written before this check, so a previously-present runtime snapshot is treated as stale and skipped instead of being refreshed via setRuntimeAuthProfileStoreSnapshot. That means runtime-only in-memory auth data (for example external OAuth overlays or pre-resolved secret refs) can disappear after a normal save path and stay missing until a full runtime snapshot rebuild.
Useful? React with 👍 / 👎.
0cc450f to
591cd59
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 591cd59b20
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (snapshot.authMtimeMs !== authMtimeMs || snapshot.stateMtimeMs !== stateMtimeMs) { | ||
| runtimeAuthStoreSnapshots.delete(key); | ||
| return undefined; |
There was a problem hiding this comment.
Reload subagent store when runtime snapshot goes stale
When getFreshRuntimeAuthProfileStoreSnapshot evicts and returns undefined on mtime drift, subagent calls now look like “no requested snapshot.” In that case resolveRuntimeAuthProfileStore (src/agents/auth-profiles/store.ts) falls back to the main-agent runtime snapshot and returns early, so ensureAuthProfileStore(subagentDir) can skip reloading the changed subagent auth-profiles.json/auth-state.json from disk. This leaves subagent auth updates unapplied (and can incorrectly use main-agent credentials) whenever a main snapshot is present.
Useful? React with 👍 / 👎.
|
Related work from PRtags group Title: auth: stale runtime snapshots overwrite OAuth tokens
|
|
Closing this as implemented after Codex automated review. Current main, shipped in v2026.4.24, already fixes the stale Codex OAuth credential failure through safer disk reloads in locked auth writes and OAuth refresh recovery. The unmerged PR's runtime-snapshot mtime approach is now stale and had review comments flagging credential/snapshot regressions, so it should not be merged as the fix. Best possible solution: Close this PR and keep the shipped v2026.4.24 implementation. If maintainers still want broader runtime auth snapshot mtime invalidation later, it should be designed as a new change with tests for subagent fallback, save-path refresh, auth-state changes, and runtime-only secret overlays. What I checked:
So I’m closing this as already implemented rather than keeping a duplicate issue open. Codex Review notes: model gpt-5.5, reasoning high; reviewed against f0b327cf686e; fix evidence: release v2026.4.24, commit cbcfdf62c729. |
Summary
auth-profiles.jsonchanges on diskauth-state.jsonchanges on diskProblem
A long-lived gateway process can keep serving a stale in-memory runtime auth snapshot even after a fresh OAuth login rotates credentials on disk.
In the Codex OAuth path, that leaves the running process reusing an already-rotated refresh token until the gateway restarts, which can surface as repeated refresh failures like:
401refresh_token_reusedYour refresh token has already been used to generate a new access token. Please try signing in again.Root cause
src/agents/auth-profiles/runtime-snapshots.tscached runtime auth stores by agent path, but it did not verify whether the underlyingauth-profiles.jsonorauth-state.jsonfiles had changed since the snapshot was created.That meant external disk updates, including browser re-login flows and other auth-store writes, could leave a stale runtime snapshot active indefinitely inside the current gateway process.
Fix
This PR fingerprints runtime snapshots with the current mtimes of:
auth-profiles.jsonauth-state.jsonWhen either file changes, the snapshot is evicted before reuse, so the next auth-store load comes from fresh persisted state instead of stale in-memory credentials.
Tests
npx --yes [email protected] exec vitest run src/agents/auth-profiles.store-cache.test.ts src/agents/auth-profiles.store.save.test.tsnpx --yes [email protected] exec vitest run src/agents/auth-profiles/oauth.openai-codex-refresh-fallback.test.ts