Repository navigation
Description
Two issues flagged in PR review by @aponcedeleonch on the thv-llm-token branch.
1. withPreemptiveRefresh composition is broken
File: pkg/llm/tokensource.go
Both callers of withPreemptiveRefresh pass an src that is already an oauth2.ReuseTokenSource:
flow.TokenSource()returnsoauth2.ReuseTokenSource(token, base)(flow.go:511)remote.CreateTokenSourceFromCachedreturnsoauth2.ReuseTokenSource(token, base)(persisting_token_source.go:104), then wrapped inPersistingTokenSource
The current stacking is:
outer ReuseTokenSource(nil, preemptiveTokenSource{inner: PersistingTokenSource{source: inner ReuseTokenSource{source: rawRefresher}}})
The bug: when the outer ReuseTokenSource's cached token hits the shifted expiry, it calls preemptiveTokenSource, which calls the inner chain. The inner ReuseTokenSource sees the real expiry is still valid and returns the same cached token unchanged. preemptiveTokenSource shifts the expiry again and returns the same shifted value. The outer caches it — but it immediately appears expired on the next call because time.Now() has already crossed real_expiry - 40s. Every Token() call inside the 30-second preemptive window triggers a full inner round-trip instead of one refresh per window.
Fix: wrap the raw refresher with preemptiveTokenSource before oauth2.ReuseTokenSource, not after. Target stacking:
ReuseTokenSource(nil, preemptiveTokenSource{inner: PersistingTokenSource{source: rawRefresher}})
Requires:
- In
tryRestoreFromCache: build the raw token source directly instead of usingCreateTokenSourceFromCached(which adds an innerReuseTokenSource) - In
performBrowserFlow: build raw token source fromflowCfgfields directly instead of callingflow.TokenSource()(which is aReuseTokenSource) - Note:
oauth2.Config.TokenSourcealso returns areuseTokenSourceinternally, soNewResourceTokenSource(which does its own validity check + storestokenfield) is the correct stateless refresher for the resource path - Add a regression test with a fake refresher that counts calls, verifying that multiple
Token()calls within the preemptive window produce exactly one innerToken()call
2. Stale _AT cache entry after refresh token rotation
File: pkg/llm/tokensource.go — makeTokenPersister
When the persister callback fires (a rotated refresh token is written to the secrets store), the associated _AT access token cache entry is left unchanged. Between that write and the next Token() call, tier 1.5 could serve an access token that was minted against the now-invalidated refresh token. The preemptive window reduces the window in practice, but it is not zero.
Fix: inside makeTokenPersister, after SetSecret succeeds, overwrite or delete the _AT entry so the next Token() call falls through to tier 2 and gets a fresh access token:
// Invalidate the access-token cache so the next call does not serve a token
// minted against the now-rotated refresh token.
_ = t.secretsProvider.SetSecret(ctx, t.accessTokenCacheKey(), "")Or delete it if the provider supports no-op deletes for missing keys.
Both issues are on pkg/llm/tokensource.go introduced in the thv-llm-token branch / PR.
Activity
pkg/llm: fix withPreemptiveRefresh composition and stale _AT on rotation
pkg/llm: fix withPreemptiveRefresh composition and stale _AT on rotation
pkg/llm: fix withPreemptiveRefresh composition and stale _AT on rotat…