Skip to content

Commit 7812d3a

Browse files
committed
fix(memory): refuse a brain file whose page or registry rows name another brain
Brain.importFromSqlite checked for a single brain in brain_meta alone, then copied every memory_pages and memory_scopes row of the file under the receiving brain. A file whose meta names one brain and whose page or registry rows name another now throws as a file of several brains does, before anything is copied. Signed-off-by: jddunn <[email protected]>
1 parent 014cd9d commit 7812d3a

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

  • src/cognition/memory/retrieval/store

‎src/cognition/memory/retrieval/store/Brain.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,18 @@ export class Brain {
17581758
`SELECT DISTINCT brain_id FROM brain_meta WHERE brain_id IS NOT NULL`,
17591759
);
17601760
}
1761+
// The registry of conversation scopes and the pages name their brain too, and the copy reads them whole: a file
1762+
// whose rows of the two tables name another brain than its meta is a file of two brains.
1763+
const named = new Set(sourceBrainIds.map((row) => row.brain_id));
1764+
for (const table of CONVERSATION_SCOPE_TABLES) {
1765+
const columns = await peekAdapter.all<{ name: string }>(`PRAGMA table_info(${table})`);
1766+
if (!columns.some((column) => column.name === 'brain_id')) continue;
1767+
const rows = await peekAdapter.all<{ brain_id: string }>(
1768+
`SELECT DISTINCT brain_id FROM ${table} WHERE brain_id IS NOT NULL`,
1769+
);
1770+
for (const row of rows) named.add(row.brain_id);
1771+
}
1772+
sourceBrainIds = [...named].sort().map((brain_id) => ({ brain_id }));
17611773
} finally {
17621774
await peekAdapter.close();
17631775
}

0 commit comments

Comments
 (0)