@@ -621,6 +621,32 @@ describe.each(STORES)('the compile per scope on %s', (store) => {
621621 expect ( compiledAt . get ( B ) ) . toBe ( b1 . createdAt ) ;
622622 } ) ;
623623
624+ it ( 'two compiles at once send the traces of a scope to the model once' , async ( ) => {
625+ const { memory } = await open ( store ) ;
626+ let release ! : ( ) => void ;
627+ const gate = new Promise < void > ( ( resolve ) => {
628+ release = resolve ;
629+ } ) ;
630+ const prompts = attachRows ( memory , {
631+ llm : async ( ) => {
632+ await gate ;
633+ return 'Merged notes.' ;
634+ } ,
635+ } ) ;
636+ await memory . remember ( 'Alice likes tea.' , { scope : 'conversation' , scopeId : A } ) ;
637+ await memory . remember ( 'Alice has a cat.' , { scope : 'conversation' , scopeId : A } ) ;
638+
639+ // The second compile reads the scopes to compile while the first one's model call is held.
640+ const first = memory . compileWiki ( ) ;
641+ const second = memory . compileWiki ( ) ;
642+ for ( let i = 0 ; i < 400 && prompts . length < 1 ; i += 1 ) await pause ( 5 ) ;
643+ await pause ( 50 ) ;
644+ release ( ) ;
645+ await Promise . all ( [ first , second ] ) ;
646+
647+ expect ( prompts ) . toHaveLength ( 1 ) ;
648+ } ) ;
649+
624650 it ( 'compiles nothing a second time, and only the scope with a new trace the third' , async ( ) => {
625651 const { memory } = await open ( store ) ;
626652 const prompts = attachRows ( memory ) ;
@@ -1217,6 +1243,75 @@ describe.each(STORES)('scopes kept apart on every path, on %s', (store) => {
12171243 expect ( await traceIdsUnder ( brain , A ) ) . toEqual ( chunkIdsOf ( rows [ 0 ] ! ) ) ;
12181244 } ) ;
12191245
1246+ /** A brain that holds alice's page, one message of hers and a compile watermark, exported as JSON text. */
1247+ const exportedJson = async ( ) : Promise < { json : string ; scopes : Awaited < ReturnType < Memory [ 'listScopes' ] > > } > => {
1248+ const { memory, brain } = await open ( store ) ;
1249+ await new UserWikiStore ( { scopeId : A , pages : memory , chunk } ) . writePage ( P ( 'Alice likes tea.\n\nAlice has a cat.' , [ 't1' ] ) ) ;
1250+ await memory . remember ( 'Alice said she likes tea.' , { scope : 'conversation' , scopeId : A } ) ;
1251+ await brain . run ( 'UPDATE memory_scopes SET compiled_at = ? WHERE brain_id = ? AND scope_id = ?' , [ 1_700_000_000_000 , brain . brainId , A ] ) ;
1252+ return { json : await memory . exportToString ( ) , scopes : await memory . listScopes ( ) } ;
1253+ } ;
1254+
1255+ it ( "a JSON export carries each subject's pages and the scope registry, and an import brings them back" , async ( ) => {
1256+ const { json, scopes } = await exportedJson ( ) ;
1257+ const { memory, brain } = await open ( store ) ;
1258+
1259+ const result = await memory . importFromString ( json , 'json' ) ;
1260+
1261+ expect ( result . errors ) . toEqual ( [ ] ) ;
1262+ const pages = new UserWikiStore ( { scopeId : A , pages : memory , chunk } ) ;
1263+ const page = await pages . readPage ( 'concepts/notes' ) ;
1264+ expect ( bodyOf ( page ) ) . toBe ( 'Alice likes tea.\n\nAlice has a cat.' ) ;
1265+ expect ( page ?. sources ) . toEqual ( [ 't1' ] ) ;
1266+ // The row lists the chunks the import wrote, so no chunk is left that no row lists.
1267+ const rows = await pageRows ( brain ) ;
1268+ expect ( rows ) . toHaveLength ( 1 ) ;
1269+ expect ( chunkIdsOf ( rows [ 0 ] ! ) ) . toEqual ( await chunkTraceIdsUnder ( brain , A ) ) ;
1270+ expect ( chunkIdsOf ( rows [ 0 ] ! ) ) . toHaveLength ( 2 ) ;
1271+ expect ( await contentsUnder ( brain , A ) ) . toEqual ( [ 'Alice has a cat.' , 'Alice likes tea.' , 'Alice said she likes tea.' ] ) ;
1272+ // The scope comes with its watermark, so its next compile does not read the same traces again.
1273+ expect ( await memory . listScopes ( ) ) . toEqual ( scopes ) ;
1274+ expect ( scopes . map ( ( row ) => row . compiledAt ) ) . toEqual ( [ 1_700_000_000_000 ] ) ;
1275+
1276+ // The page's next rewrite takes the chunks the import wrote, and leaves what alice said.
1277+ await pages . writePage ( P ( 'Alice drinks espresso.' ) ) ;
1278+ expect ( await contentsUnder ( brain , A ) ) . toEqual ( [ 'Alice drinks espresso.' , 'Alice said she likes tea.' ] ) ;
1279+ } ) ;
1280+
1281+ it ( "a JSON import keeps a page the brain already holds, and brings none of the file's chunks for it" , async ( ) => {
1282+ const { json } = await exportedJson ( ) ;
1283+ const { memory, brain } = await open ( store ) ;
1284+ const pages = new UserWikiStore ( { scopeId : A , pages : memory , chunk } ) ;
1285+ await pages . writePage ( P ( 'Alice drinks espresso.' ) ) ;
1286+ const held = await chunkTraceIdsUnder ( brain , A ) ;
1287+
1288+ const result = await memory . importFromString ( json , 'json' ) ;
1289+
1290+ expect ( result . errors ) . toEqual ( [ ] ) ;
1291+ expect ( bodyOf ( await pages . readPage ( 'concepts/notes' ) ) ) . toBe ( 'Alice drinks espresso.' ) ;
1292+ expect ( await chunkTraceIdsUnder ( brain , A ) ) . toEqual ( held ) ;
1293+ expect ( chunkIdsOf ( ( await pageRows ( brain ) ) [ 0 ] ! ) ) . toEqual ( held ) ;
1294+ expect ( await contentsUnder ( brain , A ) ) . toEqual ( [ 'Alice drinks espresso.' , 'Alice said she likes tea.' ] ) ;
1295+ // The brain keeps its own watermark for a scope it already holds.
1296+ expect ( ( await memory . listScopes ( ) ) . map ( ( row ) => [ row . scopeId , row . compiledAt ] ) ) . toEqual ( [ [ A , null ] ] ) ;
1297+ } ) ;
1298+
1299+ it ( "a JSON-imported message with the text of one of the brain's page chunks is stored as a trace of its own" , async ( ) => {
1300+ const source = await open ( store ) ;
1301+ await source . memory . remember ( 'Alice likes tea.' , { scope : 'conversation' , scopeId : A } ) ;
1302+ const json = await source . memory . exportToString ( ) ;
1303+ const { memory, brain } = await open ( store ) ;
1304+ const pages = new UserWikiStore ( { scopeId : A , pages : memory , chunk } ) ;
1305+ await pages . writePage ( P ( 'Alice likes tea.' ) ) ;
1306+
1307+ const result = await memory . importFromString ( json , 'json' ) ;
1308+
1309+ expect ( result . errors ) . toEqual ( [ ] ) ;
1310+ expect ( await contentsUnder ( brain , A ) ) . toEqual ( [ 'Alice likes tea.' , 'Alice likes tea.' ] ) ;
1311+ await pages . writePage ( P ( 'Alice drinks espresso.' ) ) ;
1312+ expect ( await contentsUnder ( brain , A ) ) . toEqual ( [ 'Alice drinks espresso.' , 'Alice likes tea.' ] ) ;
1313+ } ) ;
1314+
12201315 // These run the consolidation loop on a store of its own, or write and read a SQLite export file.
12211316 if ( store === 'sqlite' ) {
12221317 /** A facade on a fresh SQLite file, with the options given. */
@@ -1359,6 +1454,31 @@ describe.each(STORES)('scopes kept apart on every path, on %s', (store) => {
13591454 expect ( await pageRows ( brain ) ) . toEqual ( [ ] ) ;
13601455 } ) ;
13611456
1457+ it ( 'a merge of a brain file whose pages and registry rows name another brain than its meta is refused' , async ( ) => {
1458+ const { memory : source , dir } = await openWith ( { selfImprove : false } ) ;
1459+ await source . writePageRow ( A , { pageId : 'concepts/notes' , content : 'Alice likes tea.\n' , contentHash : 'h-alice' } ) ;
1460+ const file = path . join ( dir , 'pages.sqlite' ) ;
1461+ await source . export ( file , { format : 'sqlite' } ) ;
1462+ await editFile ( file , async ( db ) => {
1463+ await db . run (
1464+ `INSERT INTO memory_scopes (brain_id, scope, scope_id, kind, agent_id, created_at)
1465+ VALUES ('another-brain', 'conversation', ?, 'user', 'aria-1', 1)` ,
1466+ [ B ] ,
1467+ ) ;
1468+ await db . run (
1469+ `INSERT INTO memory_pages (brain_id, scope, scope_id, page_id, content, content_hash, chunk_ids, updated_at)
1470+ VALUES ('another-brain', 'conversation', ?, 'concepts/notes', 'Bob plays cello.', 'h-bob', '[]', 1)` ,
1471+ [ B ] ,
1472+ ) ;
1473+ } ) ;
1474+ const { brain } = await openWith ( { selfImprove : false } ) ;
1475+
1476+ await expect ( brain . importFromSqlite ( file , { strategy : 'merge' } ) ) . rejects . toThrow ( 'multiple brain_ids' ) ;
1477+
1478+ expect ( await pageRows ( brain ) ) . toEqual ( [ ] ) ;
1479+ expect ( await scopeRows ( brain ) ) . toEqual ( [ ] ) ;
1480+ } ) ;
1481+
13621482 it ( "an imported message with the text of one of the brain's page chunks is stored as a trace of its own" , async ( ) => {
13631483 const source = await openWith ( { selfImprove : false } ) ;
13641484 await source . memory . remember ( 'Alice likes tea.' , { scope : 'conversation' , scopeId : A } ) ;
0 commit comments