Skip to content

Commit 4b446bb

Browse files
committed
fix(zoo-gateway): address CodeRabbit review on session cache lifecycle
Bound and prune auth-session catalogs, clear Kimi Code cache on sign-out, keep flushModels refresh non-throwing, and tighten regression tests.
1 parent 41b81f1 commit 4b446bb

5 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎src/api/providers/fetchers/__tests__/modelCache.spec.ts‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,12 +1273,13 @@ describe("auth session cache", () => {
12731273
expect(freshMockGetZooGatewayModels).toHaveBeenCalledTimes(2)
12741274
})
12751275

1276-
it("clears session cache on sign-out helper so the next account refetches", async () => {
1276+
it("clears session cache on sign-out helper so the same identity refetches", async () => {
12771277
freshMockGetZooGatewayModels.mockResolvedValue(zooGatewayOk(zooModels))
1278+
const options = { provider: providerIdentifiers.zooGateway, apiKey: "session-token" }
12781279

1279-
await freshGetModels({ provider: providerIdentifiers.zooGateway, apiKey: "account-a" })
1280+
await freshGetModels(options)
12801281
freshClearAuthSessionModelsForProvider(providerIdentifiers.zooGateway)
1281-
await freshGetModels({ provider: providerIdentifiers.zooGateway, apiKey: "account-b" })
1282+
await freshGetModels(options)
12821283

12831284
expect(freshMockGetZooGatewayModels).toHaveBeenCalledTimes(2)
12841285
})
@@ -1455,4 +1456,35 @@ describe("auth session cache", () => {
14551456

14561457
expect(freshMockGetZooGatewayModels).toHaveBeenCalledTimes(4)
14571458
})
1459+
1460+
it("flushModels with refresh logs and does not throw when the provider fetch fails", async () => {
1461+
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
1462+
freshMockGetZooGatewayModels.mockRejectedValue(new Error("network down"))
1463+
const options = { provider: providerIdentifiers.zooGateway, apiKey: "session-token" }
1464+
1465+
await expect(freshFlushModels(options, true)).resolves.toBeUndefined()
1466+
expect(consoleSpy).toHaveBeenCalled()
1467+
consoleSpy.mockRestore()
1468+
})
1469+
1470+
it("prunes session entries older than twice the TTL when maintaining the cache", async () => {
1471+
vi.useFakeTimers()
1472+
try {
1473+
freshMockGetZooGatewayModels.mockResolvedValue(zooGatewayOk(zooModels))
1474+
const first = { provider: providerIdentifiers.zooGateway, apiKey: "session-a" }
1475+
const second = { provider: providerIdentifiers.zooGateway, apiKey: "session-b" }
1476+
1477+
await freshGetModels(first)
1478+
vi.advanceTimersByTime(5 * 60 * 1000 * 2 + 1)
1479+
await freshGetModels(second)
1480+
1481+
freshMockGetZooGatewayModels.mockClear()
1482+
freshMockGetZooGatewayModels.mockResolvedValueOnce({ kind: "not_modified" })
1483+
const result = await freshGetModels(first)
1484+
1485+
expect(result).toEqual({})
1486+
} finally {
1487+
vi.useRealTimers()
1488+
}
1489+
})
14581490
})

‎src/api/providers/fetchers/__tests__/zoo-gateway.spec.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ describe("Zoo Gateway Fetchers", () => {
142142
}),
143143
)
144144
expect(result).toEqual({ kind: "not_modified" })
145+
146+
const validateStatus = mockAxiosGet.mock.calls[0]?.[1]?.validateStatus
147+
expect(validateStatus?.(304)).toBe(true)
148+
expect(validateStatus?.(200)).toBe(true)
149+
expect(validateStatus?.(500)).toBe(false)
145150
})
146151

147152
it("skips the request and returns {} when no token is available", async () => {

‎src/api/providers/fetchers/modelCache.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ type AuthScopedProvider = typeof providerIdentifiers.zooGateway | typeof provide
113113
type AuthScopedGetModelsOptions = Extract<GetModelsOptions, { provider: AuthScopedProvider }>
114114

115115
const AUTH_SESSION_TTL_MS = 5 * 60 * 1000
116+
const AUTH_SESSION_MAX_ENTRIES = 64
116117

117118
type AuthSessionCacheEntry = {
118119
models: ModelRecord
@@ -140,6 +141,34 @@ function isAuthSessionFresh(entry: AuthSessionCacheEntry): boolean {
140141
return Date.now() - entry.fetchedAt < AUTH_SESSION_TTL_MS
141142
}
142143

144+
function pruneExpiredAuthSessionEntries(): void {
145+
const staleCutoffMs = AUTH_SESSION_TTL_MS * 2
146+
const now = Date.now()
147+
for (const [key, entry] of authSessionCache) {
148+
if (now - entry.fetchedAt >= staleCutoffMs) {
149+
authSessionCache.delete(key)
150+
}
151+
}
152+
}
153+
154+
function enforceAuthSessionCacheBound(): void {
155+
pruneExpiredAuthSessionEntries()
156+
while (authSessionCache.size > AUTH_SESSION_MAX_ENTRIES) {
157+
let oldestKey: string | undefined
158+
let oldestFetchedAt = Infinity
159+
for (const [key, entry] of authSessionCache) {
160+
if (entry.fetchedAt < oldestFetchedAt) {
161+
oldestFetchedAt = entry.fetchedAt
162+
oldestKey = key
163+
}
164+
}
165+
if (!oldestKey) {
166+
break
167+
}
168+
authSessionCache.delete(oldestKey)
169+
}
170+
}
171+
143172
function getAuthSessionEntry(cacheKey: string): AuthSessionCacheEntry | undefined {
144173
return authSessionCache.get(cacheKey)
145174
}
@@ -149,10 +178,12 @@ function setAuthSessionEntry(cacheKey: string, models: ModelRecord, etag?: strin
149178
return
150179
}
151180
authSessionCache.set(cacheKey, { models, etag, fetchedAt: Date.now() })
181+
enforceAuthSessionCacheBound()
152182
}
153183

154184
function touchAuthSessionEntry(cacheKey: string, entry: AuthSessionCacheEntry): void {
155185
authSessionCache.set(cacheKey, { ...entry, fetchedAt: Date.now() })
186+
enforceAuthSessionCacheBound()
156187
}
157188

158189
function deleteAuthSessionEntry(cacheKey: string): void {
@@ -634,7 +665,11 @@ export const flushModels = async (options: GetModelsOptions, refresh: boolean =
634665
if (isAuthScopedProvider(options.provider)) {
635666
deleteAuthSessionEntry(getCacheKey(options))
636667
if (refresh) {
637-
await resolveAuthScopedModels(options, { forceRefresh: true })
668+
try {
669+
await resolveAuthScopedModels(options, { forceRefresh: true })
670+
} catch (error) {
671+
console.error(`[flushModels] Failed to refresh auth-scoped ${getCacheKey(options)} models:`, error)
672+
}
638673
}
639674
return
640675
}

‎src/integrations/kimi-code/__tests__/oauth.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { providerIdentifiers } from "@roo-code/types"
2+
3+
import * as modelCache from "../../../api/providers/fetchers/modelCache"
14
import { KIMI_CODE_OAUTH_CONFIG, KimiCodeOAuthManager } from "../oauth"
25

36
const createContext = () => {
@@ -95,6 +98,7 @@ describe("KimiCodeOAuthManager", () => {
9598
})
9699

97100
it("clears credentials and cancels authorization", async () => {
101+
const clearSessionSpy = vi.spyOn(modelCache, "clearAuthSessionModelsForProvider")
98102
const { context, values } = createContext()
99103
values.set(
100104
"kimi-code-oauth-credentials",
@@ -105,6 +109,8 @@ describe("KimiCodeOAuthManager", () => {
105109
await manager.clearCredentials()
106110
expect(values.has("kimi-code-oauth-credentials")).toBe(false)
107111
expect(await manager.isAuthenticated()).toBe(false)
112+
expect(clearSessionSpy).toHaveBeenCalledWith(providerIdentifiers.kimiCode)
113+
clearSessionSpy.mockRestore()
108114
})
109115

110116
it("does not restore credentials when sign-out races an in-flight refresh", async () => {

‎src/integrations/kimi-code/oauth.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ export class KimiCodeOAuthManager {
232232
await this.context?.secrets.delete(KIMI_CODE_CREDENTIALS_KEY)
233233
this.credentials = null
234234
this.state = { status: "idle" }
235+
const { clearAuthSessionModelsForProvider } = await import("../../api/providers/fetchers/modelCache")
236+
const { providerIdentifiers } = await import("@roo-code/types")
237+
clearAuthSessionModelsForProvider(providerIdentifiers.kimiCode)
235238
}
236239

237240
async isAuthenticated(): Promise<boolean> {

0 commit comments

Comments
 (0)