@@ -180,6 +180,12 @@ async function insertInBatches(engine: ObjectQL, object: string, rows: Row[]): P
180180 }
181181}
182182
183+ // [#23181] Pay the first kernel boot at module scope, never in a hook: vitest
184+ // clocks a hook as it clocks a test, and the first boot is the slow one. The
185+ // throwaway kernel registers no fixture object, writes nothing and is shut down
186+ // before any hook runs.
187+ await ( await bootKernel ( new SqliteWasmDriver ( { filename : ':memory:' } ) ) ) . kernel . shutdown ( ) ;
188+
183189describe ( '[ADR-0139 E5] the parent-record read gates on the leaf path' , ( ) => {
184190 let kernel : ObjectKernel ;
185191 let engine : ObjectQL ;
@@ -201,12 +207,30 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
201207 const ledgerOrganizationBound : boolean [ ] = [ ] ;
202208 const ids : Record < string , string > = { } ;
203209
210+ /**
211+ * [#23181] Every row of an object at rest, as the system reads it — read once
212+ * and reused. No case in this describe writes after `beforeAll`, so each such
213+ * read answered the same rows: the cases made about sixty of them, over tables
214+ * of 2,600 to 7,000 rows. Each reuse counts the object first and re-reads on a
215+ * moved count, so a case that adds or removes rows is never judged against an
216+ * older ground truth.
217+ */
218+ const atRestCache = new Map < string , { count : number ; rows : Row [ ] } > ( ) ;
219+ const atRestOf = async ( object : string ) : Promise < Row [ ] > => {
220+ const count = await engine . count ( object , { } , { context : SYS } ) ;
221+ const hit = atRestCache . get ( object ) ;
222+ if ( hit && hit . count === count ) return hit . rows ;
223+ const rows : Row [ ] = await engine . find ( object , { limit : 100_000 , context : SYS } ) ;
224+ atRestCache . set ( object , { count, rows } ) ;
225+ return rows ;
226+ } ;
227+
204228 /** Every row at rest, and the ground truth of who may read which, judged by the stand-in's own rules. */
205229 const truth = async ( object : string , reader : any ) : Promise < string [ ] > => {
206- const atRest : Row [ ] = await engine . find ( object , { limit : 100_000 , context : SYS } ) ;
230+ const atRest = await atRestOf ( object ) ;
207231 const live = new Map < string , Map < string , Row > > ( ) ;
208232 for ( const parent of [ DOC , VAULT , GATED , FAR ] ) {
209- const records : Row [ ] = await engine . find ( parent , { limit : 100_000 , context : SYS } ) ;
233+ const records = await atRestOf ( parent ) ;
210234 live . set ( parent , new Map ( records . map ( ( r ) => [ String ( r . id ) , r ] ) ) ) ;
211235 }
212236 const holder = Array . isArray ( reader ?. systemPermissions ) && reader . systemPermissions . includes ( LEDGER_AUDIT_CAPABILITY ) ;
@@ -298,12 +322,12 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
298322 } , 30_000 ) ;
299323
300324 it ( 'control: the fixture is past the window, and every row came from its producer' , async ( ) => {
301- const activity : Row [ ] = await engine . find ( ACTIVITY , { limit : 100_000 , context : SYS } ) ;
325+ const activity = await atRestOf ( ACTIVITY ) ;
302326 expect ( activity . length ) . toBeGreaterThan ( PARENT_GATE_SCAN_LIMIT ) ;
303327 expect ( activity . filter ( ( r ) => r . object_name === DOC && r . type === 'created' ) . length ) . toBe ( DOCS_OF_A + DOCS_OF_B + 1 ) ;
304328 expect ( activity . some ( ( r ) => r . object_name === DOC && r . record_id === ids . gone && r . type === 'deleted' ) ) . toBe ( true ) ;
305329 expect ( ( await truth ( ACTIVITY , MEMBER_A ) ) . length ) . toBeGreaterThan ( PARENT_GATE_SCAN_LIMIT ) ;
306- const ledger : Row [ ] = await engine . find ( LEDGER , { limit : 100_000 , context : SYS } ) ;
330+ const ledger = await atRestOf ( LEDGER ) ;
307331 expect ( ledger . filter ( ( r ) => r . action === 'config_change' ) . length ) . toBe ( ROWS_ABOUT_NO_RECORD ) ;
308332 } ) ;
309333
@@ -328,7 +352,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
328352 } ) ;
329353
330354 it ( 'no row is served whose parent the reader may not read, or that no longer exists' , async ( ) => {
331- const all : Row [ ] = await engine . find ( ACTIVITY , { limit : 100_000 , context : SYS } ) ;
355+ const all = await atRestOf ( ACTIVITY ) ;
332356 const byId = new Map ( all . map ( ( r ) => [ String ( r . id ) , r ] ) ) ;
333357 for ( const id of await served ( ACTIVITY , MEMBER_A ) ) {
334358 const row = byId . get ( id ) ! ;
@@ -379,7 +403,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
379403 const named = { object_name : { $in : NAMED } } ;
380404 // The same rows, in a spelling the gate does not read as naming its parents: it keeps every branch.
381405 const spelledOut = { $or : NAMED . map ( ( object_name ) => ( { object_name } ) ) } ;
382- const atRest : Row [ ] = await engine . find ( ACTIVITY , { limit : 100_000 , context : SYS } ) ;
406+ const atRest = await atRestOf ( ACTIVITY ) ;
383407 const parentOf = new Map ( atRest . map ( ( r ) => [ String ( r . id ) , r . object_name ] ) ) ;
384408 const gateConjunct = ( where : any ) => where . $and [ 1 ] ;
385409 seenWhere [ ACTIVITY ] . length = 0 ;
@@ -431,7 +455,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
431455 // per parent object through the engine middleware chain, with the
432456 // delegated context. Asked here for every parent pair at rest, so no
433457 // window cuts it; the leaf must serve exactly the rows it admits.
434- const atRest : Row [ ] = await engine . find ( ACTIVITY , { limit : 100_000 , context : SYS } ) ;
458+ const atRest = await atRestOf ( ACTIVITY ) ;
435459 const pairs = new Map < string , Set < string > > ( ) ;
436460 for ( const row of atRest ) {
437461 const parent = parseParentRecord ( row , ACTIVITY ) ;
@@ -488,7 +512,7 @@ describe('[ADR-0139 E5] the parent-record read gates on the leaf path', () => {
488512 return Math . round ( ( performance . now ( ) - t0 ) * 10 ) / 10 ;
489513 } ;
490514 const reading = {
491- rowsAtRest : ( await engine . find ( ACTIVITY , { limit : 100_000 , context : SYS } ) ) . length ,
515+ rowsAtRest : ( await atRestOf ( ACTIVITY ) ) . length ,
492516 leafMs : {
493517 memberFind : await time ( ( ) => served ( ACTIVITY , MEMBER_A ) ) ,
494518 memberCount : await time ( ( ) => engine . count ( ACTIVITY , { } , { context : MEMBER_A } ) ) ,
0 commit comments