Repository navigation
perf(vault): cached credentials and safe singleflight refresh - #46
Merged
Merged
Conversation
Prevent concurrent OAuth refreshes from invalidating rotating tokens. Keep decrypted tokens in a bounded, expiry-aware local LRU. Signed-off-by: Shreyansh Sancheti <43677304+shreyanshjain7174@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Apply a separate 15s deadline to shared refreshes. Use conditional writes so deletes and re-links beat stale tokens. Retry rotated-token writes without logging credentials. Escape OAuth callback HTML to satisfy the expanded Semgrep scan. Signed-off-by: Shreyansh Sancheti <43677304+shreyanshjain7174@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Always release waiters and clear flights after a panic. Use fixed errors and logs so panic values cannot expose credentials. Persist successful rotations even after the exchange deadline. Signed-off-by: Shreyansh Sancheti <43677304+shreyanshjain7174@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Plan
Phase 0, item 7 of the AgentGate scale-out prompt: vault credential cache and singleflight refresh.
Every request did a SQLite read plus AES decrypt to fetch a token. Concurrent requests for an expiring OAuth token all refreshed at once. Providers that rotate refresh tokens then invalidate each other, which can lock the user out.
What changed
CachedStore. Caches decrypted tokens per (user, service).PutandDeleteupdate the DB first, and a DB error leaves the entry invalidated.AGENTGATE_VAULT_CACHE_TTLsets the TTL (0disables).RefreshFunctakes a context.exchangeTokenusesNewRequestWithContextand a client with a 15s timeout. Before,http.PostFormon the default client could hang forever.CompareAndPut(user, service, expectedRefreshToken, tok)on every store. SQLite compares inside a transaction and the UPDATE matches the old ciphertext.vault_refresh_timeout, nottoken_expired.html/template(found by Semgrep).Numbers
BenchmarkVaultGetValidation
go build ./...: passedgo vet ./...: passedgo test -race ./... -count=2: passedsemgrep --config auto --error --quiet internal/vault internal/oauth internal/gateway cmd/agentgw: passed, no findingsTests cover:
Review
Three rounds by a second model (Claude Opus 5.5). It found:
All are fixed with tests.
Known limits