fix(agent): refresh model catalog on auth change without full reauth#3655
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix(agent): refresh model catalog on auth change without full reauth#3655posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
The gateway model caches (fetchGatewayModels / fetchModelsList) are module-level singletons keyed by (gatewayUrl, authToken) with a 10-minute TTL and no explicit invalidation. They were only busted by TTL expiry or a change of the token half of the key — which is exactly what a full sign-out/sign-in does. So when a model's availability changed for a user mid-session (e.g. Codex), the stale catalog could keep hiding it until the user fully signed out and back in. Add resetGatewayModelCaches() and wire it to auth-state changes via the AgentAuth port, so any auth transition drops the cache and the next picker/config read re-reads the gateway — a model that becomes available surfaces without a full reauthentication. Mirrors the existing StateChanged-driven cache reset in LlmGatewayService / UsageMonitor. Adds a regression test proving the catalog refreshes for the same token after a reset (i.e. without a token change). Generated-By: PostHog Code Task-Id: f2a3c85e-350e-4ec6-b482-3b663cab9d2c
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
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
Two users reported that Codex disappeared from the model list mid-session, and it only came back after signing out and back in. Another user still had it available at the same time, pointing to a user-session-specific stale-state problem rather than a platform-wide removal.
The model catalog is fetched from the AI gateway
/v1/modelsbyfetchGatewayModels/fetchModelsListin@posthog/agent. Both hold module-level in-memory caches keyed by(gatewayUrl, authToken)with a 10-minute TTL and no explicit invalidation. The cache is therefore only busted by TTL expiry or a change of the token half of the key — and a full sign-out/sign-in is precisely what rotates the token. So once a stale (or entitlement-restricted) catalog was cached for a user's token, nothing short of the TTL lapsing or a full reauthentication would refresh it. That matches the reported symptom and the sign-out/in workaround exactly.Changes
resetGatewayModelCaches()to@posthog/agent/gateway-models, which drops both in-process model caches.AgentServicesubscribes via a newonAuthStateChangedmethod on theAgentAuthport (backed byAuthService's existingStateChangedevent), so any auth transition (login, logout, org switch, token refresh) drops the cache. The next picker/config read re-reads the gateway, so a model that becomes available surfaces without a full reauthentication. This mirrors the existingStateChanged-driven cache resets inLlmGatewayServiceandUsageMonitor.AgentServiceis a process-lifetime singleton, so the subscription never needs tearing down.How did you test this?
gateway-models.test.tsproving that, for the same token (no reauth), a model marked restricted is served from cache, then reappears as allowed afterresetGatewayModelCaches()forces a re-read. Runs for bothfetchGatewayModelsandfetchModelsList.pnpm --filter @posthog/agent exec vitest run src/gateway-models.test.ts— 34 passed.pnpm --filter @posthog/workspace-server exec vitest runforagent.test.ts+auth-adapter.test.ts— 46 passed.pnpm --filter @posthog/agent --filter @posthog/workspace-server --filter @posthog/core typecheckandpnpm --filter code typecheck— clean.Automatic notifications
Created with PostHog Code from this inbox report.