Skip to content

Commit 5c35f78

Browse files
committed
fix: prune stale clientCache entries in refreshIndexList()
Agent-Id: agent-81feb144-5dc6-4a1f-9cd1-7e74cd26e3d1
1 parent c9c4ea1 commit 5c35f78

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/clients/multi-index-runner.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,52 @@ describe("MultiIndexRunner.refreshIndexList", () => {
236236
// In fixed mode, the list should remain unchanged even though pytorch was deleted
237237
expect(runner.indexNames).toEqual(["pytorch", "react"]);
238238
});
239+
240+
it("in discovery mode, refreshIndexList prunes stale cache entries", async () => {
241+
const store = createMockStoreWithIndexes(["pytorch", "react", "docs"]);
242+
243+
const runner = await MultiIndexRunner.create({
244+
store,
245+
// No indexNames = discovery mode
246+
});
247+
248+
expect(runner.indexNames).toEqual(["pytorch", "react", "docs"]);
249+
250+
// Manually populate cache with mock clients (avoid initialization)
251+
const mockClient = { initialized: true } as any;
252+
(runner as any).clientCache.set("pytorch", mockClient);
253+
(runner as any).clientCache.set("react", mockClient);
254+
(runner as any).clientCache.set("docs", mockClient);
255+
256+
// Verify cache has all three entries
257+
expect((runner as any).clientCache.size).toBe(3);
258+
259+
// Simulate store losing the "docs" index
260+
(store.list as any).mockResolvedValue(["pytorch", "react"]);
261+
(store.loadSearch as any).mockImplementation((name: string) => {
262+
if (["pytorch", "react"].includes(name)) {
263+
return Promise.resolve({
264+
version: 1,
265+
contextState: { version: 1 } as any,
266+
source: {
267+
type: "github",
268+
config: { owner: "test", repo: name },
269+
syncedAt: new Date().toISOString(),
270+
},
271+
});
272+
}
273+
return Promise.resolve(null);
274+
});
275+
276+
await runner.refreshIndexList();
277+
278+
// Index list should be updated
279+
expect(runner.indexNames).toEqual(["pytorch", "react"]);
280+
281+
// Cache should be pruned - only pytorch and react remain
282+
expect((runner as any).clientCache.size).toBe(2);
283+
expect((runner as any).clientCache.has("pytorch")).toBe(true);
284+
expect((runner as any).clientCache.has("react")).toBe(true);
285+
expect((runner as any).clientCache.has("docs")).toBe(false);
286+
});
239287
});

src/clients/multi-index-runner.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ export class MultiIndexRunner {
244244

245245
this.indexNames = newIndexNames;
246246
this.indexes = newIndexes;
247+
248+
// Prune stale cache entries for removed indexes
249+
for (const cachedName of this.clientCache.keys()) {
250+
if (!newIndexNames.includes(cachedName)) {
251+
this.clientCache.delete(cachedName);
252+
}
253+
}
247254
}
248255

249256
/**

0 commit comments

Comments
 (0)