Skip to content

Commit 555a550

Browse files
committed
fix brain sessions indexing
1 parent c9eb6d3 commit 555a550

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/memory/index.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe("memory index", () => {
4040
let indexExtraPath = "";
4141
let indexStatusPath = "";
4242
let indexSourceChangePath = "";
43+
let indexSessionStartPath = "";
4344
let indexModelPath = "";
4445
let sourceChangeStateDir = "";
4546
const sourceChangeSessionLogLines = [
@@ -73,6 +74,7 @@ describe("memory index", () => {
7374
indexExtraPath = path.join(workspaceDir, "index-extra.sqlite");
7475
indexStatusPath = path.join(workspaceDir, "index-status.sqlite");
7576
indexSourceChangePath = path.join(workspaceDir, "index-source-change.sqlite");
77+
indexSessionStartPath = path.join(workspaceDir, "index-session-start.sqlite");
7678
indexModelPath = path.join(workspaceDir, "index-model-change.sqlite");
7779
sourceChangeStateDir = path.join(fixtureRoot, "state-source-change");
7880

@@ -321,6 +323,44 @@ describe("memory index", () => {
321323
}
322324
});
323325

326+
it("includes sessions in a full reindex triggered from session-start", async () => {
327+
const stateDir = path.join(fixtureRoot, "state-session-start");
328+
const sessionDir = path.join(stateDir, "agents", "main", "sessions");
329+
await fs.rm(stateDir, { recursive: true, force: true });
330+
await fs.mkdir(sessionDir, { recursive: true });
331+
await fs.writeFile(
332+
path.join(sessionDir, "session-start-reindex.jsonl"),
333+
`${sourceChangeSessionLogLines}\n`,
334+
);
335+
336+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
337+
process.env.OPENCLAW_STATE_DIR = stateDir;
338+
339+
try {
340+
const cfg = createCfg({
341+
storePath: indexSessionStartPath,
342+
sources: ["memory", "sessions"],
343+
sessionMemory: true,
344+
});
345+
const result = await getMemorySearchManager({ cfg, agentId: "main" });
346+
const manager = requireManager(result);
347+
await manager.sync?.({ reason: "session-start" });
348+
const status = manager.status();
349+
expect(status.sourceCounts?.find((entry) => entry.source === "sessions")?.files).toBe(1);
350+
expect(
351+
status.sourceCounts?.find((entry) => entry.source === "sessions")?.chunks ?? 0,
352+
).toBeGreaterThan(0);
353+
await manager.close?.();
354+
} finally {
355+
if (previousStateDir === undefined) {
356+
delete process.env.OPENCLAW_STATE_DIR;
357+
} else {
358+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
359+
}
360+
await fs.rm(stateDir, { recursive: true, force: true });
361+
}
362+
});
363+
324364
it("reindexes when the embedding model changes", async () => {
325365
const base = createCfg({ storePath: indexModelPath });
326366
const baseAgents = base.agents!;

src/memory/manager-sync-ops.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,13 @@ export abstract class MemoryManagerSyncOps {
699699
if (params?.force) {
700700
return true;
701701
}
702+
if (needsFullReindex) {
703+
return true;
704+
}
702705
const reason = params?.reason;
703706
if (reason === "session-start" || reason === "watch") {
704707
return false;
705708
}
706-
if (needsFullReindex) {
707-
return true;
708-
}
709709
return this.sessionsDirty && this.sessionsDirtyFiles.size > 0;
710710
}
711711

0 commit comments

Comments
 (0)