Repository navigation
fix/53317-gateway-overwrites-oauth-token - #53754
luoxiao6645 wants to merge 18 commits into
Conversation
…file is newer Problem: Gateway overwrites fresh OAuth tokens with stale cached state on startup, burning single-use refresh tokens and blocking all Codex requests. Root cause: resolveRuntimeAuthProfileStore() returns in-memory snapshots without checking if on-disk auth-profiles.json was modified after snapshot load. Fix: - Add runtimeSnapshotsLoadedAtMs timestamp to track snapshot load time - In replaceRuntimeAuthProfileStoreSnapshots(), record timestamp BEFORE populating the map to eliminate race window - In resolveRuntimeAuthProfileStore(), compare disk file mtime against snapshot load timestamp; invalidate on stale - Only clear runtimeAuthStoreSnapshots (not loadedAuthStoreCache, which already has mtime-based invalidation) Fixes openclaw#53317
|
@codex review |
Greptile SummaryThis PR adds mtime-based staleness detection to Changes:
Potential concern: Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/auth-profiles/store.ts
Line: 104-107
Comment:
**Misleading comment about what the pre-population timestamp prevents**
The comment says: *"any disk writes that happen between 'load from disk' and 'stamp here' are still detected as stale (the mtime will be >= this timestamp)"* — but this is the opposite of what actually happens.
The disk reads occur in `prepareSecretsRuntimeSnapshot` (via `loadAuthStore(agentDir)` for each agent dir), which completes well before `activateSecretsRuntimeSnapshot` calls `replaceRuntimeAuthProfileStoreSnapshots`. Any write with `mtime < runtimeSnapshotsLoadedAtMs` (i.e. writes between the disk read and this timestamp) will **not** be detected as stale — the condition `mainMtime > runtimeSnapshotsLoadedAtMs` will be `false`.
What setting the timestamp **before** map population actually achieves is: any write that occurs **during or after** the map-population loop (steps between `Date.now()` and the end of the `for` loop) will have `mtime >= timestamp` and will be detected on the next check. If the timestamp were set *after* populating, writes during population would be missed.
A more accurate comment:
```suggestion
// Record timestamp BEFORE populating the map so that any disk writes
// that occur during or after map population are detected as stale on
// the next resolveRuntimeAuthProfileStore() call (mtime will be >= this timestamp).
// Note: writes between the upstream disk read and this point are NOT caught —
// that is an inherent limitation of mtime-based invalidation without holding a
// file lock across the entire prepare→activate sequence.
runtimeSnapshotsLoadedAtMs = Date.now();
```
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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4b475cfa66
ℹ️ 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".
Read the earliest mtime across all auth store files at activation time instead of using Date.now(). This ensures that any file update that landed before replaceRuntimeAuthProfileStoreSnapshots() is detected as stale, closing the window between prepareSecretsRuntimeSnapshot's disk reads and this function call.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ddc182ea6b
ℹ️ 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".
|
I dug through this one again because the user-visible symptom is real and severe, but I don't think it's ready to land as-is. Two blockers:
I think the right next step is:
I would keep the PR open, but I would not merge this revision. |
- Track mtime per auth store path instead of global earliest mtime - Prevents incorrect invalidation when different agents have different mtimes - Add regression tests for startup race condition (openclaw#53317)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 59eb171b9e
ℹ️ 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".
Clear runtimeSnapshotMtimes before populating fresh entries to prevent removed agent paths from causing false-positive staleness detection.
- Test stale mtime key cleanup when agent is removed - Test handling of non-existent auth-profiles.json - Covers 5 test scenarios for per-path mtime tracking
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3aeef80b7f
ℹ️ 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".
- Add authStoreMtimes to PreparedSecretsRuntimeSnapshot type - Record mtime when loadAuthStore() reads the store in prepareSecretsRuntimeSnapshot() - Pass snapshotMtimes to replaceRuntimeAuthProfileStoreSnapshots() to close the prepare->activate race window where external file modifications could pair stale in-memory stores with newer on-disk mtimes, causing invalid credentials to be saved
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3aeef80b7f
ℹ️ 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".
After saveAuthProfileStore() writes to disk, update runtimeSnapshotMtimes to the new mtime. This prevents self-induced file updates (e.g. usage-stat writes) from triggering false-positive staleness detection that would clear the runtime snapshot and lose runtime-resolved secret values.
…Store Also update runtimeAuthStoreSnapshots when saveAuthProfileStore writes to disk to ensure subsequent ensureAuthProfileStore() calls return the newly saved content rather than stale in-memory data. Combined with the mtime update, this prevents self-induced writes from triggering false staleness detection.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 229fc1ff58
ℹ️ 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".
Write original store (with resolved key/token) to runtimeAuthStoreSnapshots, not the sanitized payload (which strips keyRef/tokenRef values). This preserves runtime-resolved secret values after save operations.
|
@steipete Thanks for the review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 38c4ffd601
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d8e30cec9f
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 03485b9c00
ℹ️ 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".
|
@steipete please review again |
…ain-sync # Conflicts: # src/agents/auth-profiles.markauthprofilefailure.test.ts # src/agents/auth-profiles/store.ts # src/secrets/runtime.integration.test.ts
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3a839b68ac
ℹ️ 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 (requestedMtime !== null && requestedMtime > requestedLoadedMtime) { | ||
| invalidateRuntimeAuthProfileStoreSnapshot(agentDir); | ||
| return null; |
There was a problem hiding this comment.
Preserve main runtime secrets when agent snapshot turns stale
When a subagent auth file has a newer mtime, this path invalidates only that agent snapshot and returns null, which forces ensureAuthProfileStore(agentDir) down the disk-load path for both agent and main stores. In an active secrets runtime, main credentials resolved from keyRef/tokenRef can exist only in the in-memory runtime snapshot, so this fallback can drop usable main credentials for that agent until a full runtime snapshot rebuild, causing auth failures after an external subagent auth write.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fa17899d10
ℹ️ 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".
| invalidateRuntimeAuthProfileStoreSnapshot(agentDir); | ||
| return null; |
There was a problem hiding this comment.
Preserve main runtime secrets after stale-agent invalidation
When an agent-specific auth file is detected as stale, this branch invalidates that agent snapshot and immediately returns null, which forces ensureAuthProfileStore(agentDir) down the disk-load path even if the main runtime snapshot is still valid. In secrets-runtime flows where main credentials are resolved only in memory (for example keyRef/tokenRef profiles whose plaintext value is intentionally not persisted), that fallback drops usable main credentials for the agent after any external edit to the agent store. Fresh evidence in this commit is the new early return after invalidateRuntimeAuthProfileStoreSnapshot(agentDir), together with the stale-key guard that prevents main-snapshot fallback.
Useful? React with 👍 / 👎.
|
Related work from PRtags group Title: auth: stale runtime snapshots overwrite OAuth tokens
|
|
Closing this as implemented after Codex review. Current main already fixes the stale-auth overwrite behavior through locked disk reloads and runtime-snapshot refreshes, with direct regression coverage for the stale-in-memory/fresh-on-disk case this PR targets. What I checked:
So I’m closing this as already implemented rather than keeping a duplicate issue open. Review notes: reviewed against 8f64cd3e4d83. |
Summary
Problem: Gateway overwrites fresh OAuth tokens with stale cached state on startup, burning single-use refresh tokens and blocking all Codex requests.
Why it matters: 100% reproducible regression that completely breaks openai-codex provider usage on v2026.3.22+.
What changed: Added mtime-based staleness detection to
resolveRuntimeAuthProfileStore()— before returning a cached snapshot, compare disk file mtime against snapshot load timestamp; invalidate on stale. Includes multiple fixes for edge cases:Change Type
Scope
Linked Issue
Fixes #53317
User-visible / Behavior Changes
OAuth tokens written by
openclaw models auth loginwhile gateway is stopped are no longer overwritten on startup.Root Cause
resolveRuntimeAuthProfileStore()returns the in-memory snapshot populated byreplaceRuntimeAuthProfileStoreSnapshots()on gateway startup, without checking if the on-disk file was modified since. The existingloadedAuthStoreCachehas mtime-based invalidation, butruntimeAuthStoreSnapshots(a separate cache layer) does not — and it takes priority.When
openclaw models auth loginwrites a fresh token toauth-profiles.jsonwhile the gateway is stopped, the next gateway startup loads the old snapshot, and any subsequent write-back (usage stats, profile sync) overwrites the fresh token.Additional Fixes (Reviewer Feedback)
prepareSecretsRuntimeSnapshot(), not during activation, closing the race window between prepare and activate.saveAuthProfileStore()to prevent false staleness detection from gateway's own writes.Repro + Verification
Steps:
openclaw models auth login --provider openai-codex(fresh token written)auth-profiles.json— previously: old token restored; now: fresh token preservedVerification
oauth.test.ts✓,runtime-snapshot-save.test.ts✓,secrets/runtime.test.ts✓,runtime-snapshot-staleness.test.ts✓Compatibility / Migration
Failure Recovery
Revert the commit to restore previous behavior.
Risks and Mitigations
Risk: Additional
statSynccall on everyresolveRuntimeAuthProfileStore()invocation.Mitigation:
statSyncis sub-millisecond (kernel dentry cache), and this function is not called in tight loops. The performance cost is negligible compared to the security benefit.