fix(auth): prime models cache after device-flow sign-in#177
Merged
Conversation
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.
Problem
On a fresh install, signing in via the GUI device-code flow leaves the models list empty — the catalog is never pulled down until something forces a refresh. Reported on Windows (a fresh install signs in via device flow for the first time), but the bug is cross-platform logic, not Windows-specific code.
Root cause
Two paths can prime the models cache, and the sign-in path was missing the call:
src/lib/start/bootstrap.ts):logUser()→setupCopilotToken()→cacheModels()✅src/lib/auth-controller.ts):addAccount()→setupCopilotToken()→setAuthState(signed-in)— nocacheModels()❌The lazy
staleRefreshMiddleware(src/lib/refresh-models.ts) can't cover the gap: it's stale-while-revalidate and explicitly no-ops on an unprimed cache (loadedAtMs === null → "not_primed"). So the chain on a fresh box is:if (state.githubToken)block →cacheModels()never runs → cacheloadedAtMs === null.null.not_primed→ never fires.GET /settings/api/modelsis read-only (only POST forces a refresh) → shows empty.macOS users rarely hit it because they usually boot with a token (boot primes), so the device-flow-on-fresh-install case is unusual there.
Fix
Prime the models cache right after the Copilot token is minted in the device-flow sign-in path, mirroring
bootstrap.ts. Best-effort + isolatedtry/catch(same pattern as thesetupCopilotTokencall beside it) so a/modelshiccup never blocks reaching the signed-in state.Tests
tests/auth-controller.test.ts:cacheModelscalled once +authenticated).cacheModelsrejects, sign-in still reachesauthenticated).cacheModelsviamock.module("~/lib/utils", …)(spreads the real namespace) so the success path no longer makes a real Copilot/modelsfetch.31 pass / 0 fail;
tsc --noEmitclean; eslint clean.