fix(memory): avoid caching status-only managers

This commit is contained in:
Vincent Koc 2026-03-24 10:20:26 -07:00
parent f47549c5f6
commit a1c91bdb75
2 changed files with 33 additions and 11 deletions

View File

@ -432,6 +432,30 @@ describe("memory index", () => {
await statusManager.close?.();
});
it("does not cache builtin status-only managers across repeated requests", async () => {
const cfg = createCfg({
storePath: path.join(workspaceDir, `index-status-${randomUUID()}.sqlite`),
});
const first = await getMemorySearchManager({
cfg,
agentId: "main",
purpose: "status",
});
const second = await getMemorySearchManager({
cfg,
agentId: "main",
purpose: "status",
});
const firstManager = requireManager(first, "first status manager missing");
const secondManager = requireManager(second, "second status manager missing");
expect(secondManager).not.toBe(firstManager);
await firstManager.close?.();
await secondManager.close?.();
});
it("reindexes sessions when source config adds sessions to an existing index", async () => {
const stateDir = sourceChangeStateDir;
const sessionDir = path.join(stateDir, "agents", "main", "sessions");

View File

@ -174,16 +174,8 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
const purpose = params.purpose === "status" ? "status" : "default";
const key = `${agentId}:${workspaceDir}:${JSON.stringify(settings)}:${purpose}`;
const statusOnly = params.purpose === "status";
const existing = INDEX_CACHE.get(key);
if (existing) {
return existing;
}
const pending = INDEX_CACHE_PENDING.get(key);
if (pending) {
return pending;
}
if (statusOnly) {
const manager = new MemoryIndexManager({
return new MemoryIndexManager({
cacheKey: key,
cfg,
agentId,
@ -191,8 +183,14 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
settings,
purpose: params.purpose,
});
INDEX_CACHE.set(key, manager);
return manager;
}
const existing = INDEX_CACHE.get(key);
if (existing) {
return existing;
}
const pending = INDEX_CACHE_PENDING.get(key);
if (pending) {
return pending;
}
const createPromise = (async () => {
const providerResult = await MemoryIndexManager.loadProviderResult({