@@ -5359,7 +5359,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
53595359 } ) ,
53605360 ) ;
53615361
5362- it . effect ( "looks up shell stop requests through the interrupt request index " , ( ) =>
5362+ it . effect ( "reads shell runs and item counts without walking a thread's history " , ( ) =>
53635363 Effect . gen ( function * ( ) {
53645364 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
53655365 const sql = yield * SqlClient . SqlClient ;
@@ -5368,20 +5368,44 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
53685368 yield * projectionStore
53695369 . getShellSnapshot ( { projectId, location : "active" } )
53705370 . pipe ( Effect . withTracer ( tracer ) ) ;
5371+ const planOf = ( statement : string ) =>
5372+ sql
5373+ . unsafe < { readonly detail : string } > ( `EXPLAIN QUERY PLAN ${ statement } ` )
5374+ . pipe ( Effect . map ( ( rows ) => rows . map ( ( row ) => row . detail ) ) ) ;
53715375
53725376 const threadQuery = statements . find ( ( statement ) =>
53735377 statement . includes ( "AS latest_run_stop_requested" ) ,
53745378 ) ;
53755379 assert . isDefined ( threadQuery ) ;
5376- const stopRequestLookups = ( yield * sql . unsafe < { readonly detail : string } > (
5377- `EXPLAIN QUERY PLAN ${ threadQuery } ` ,
5378- ) )
5379- . map ( ( row ) => row . detail )
5380- . filter ( ( detail ) => detail . startsWith ( "SEARCH stop_request " ) ) ;
5380+ const plan = yield * planOf ( threadQuery ) ;
5381+ const stopRequestLookups = plan . filter ( ( detail ) => detail . startsWith ( "SEARCH stop_request " ) ) ;
53815382 assert . lengthOf ( stopRequestLookups , 2 ) ;
53825383 for ( const lookup of stopRequestLookups ) {
53835384 assert . include ( lookup , "orchestration_v2_projection_turn_items_stop_request_idx" ) ;
53845385 }
5386+ // The active, activity, and pending-secret lookups; walking the ordinal index instead
5387+ // reads every run of an idle thread.
5388+ const activeRunLookups = plan . filter (
5389+ ( detail ) => detail . startsWith ( "SEARCH r " ) && detail . includes ( "(thread_id=?" ) ,
5390+ ) ;
5391+ assert . lengthOf ( activeRunLookups , 4 ) ;
5392+ for ( const lookup of activeRunLookups ) {
5393+ assert . include ( lookup , "orchestration_v2_projection_runs_thread_status_idx" ) ;
5394+ }
5395+ const lastExecutedIndex = plan . findIndex ( ( detail ) =>
5396+ detail . includes ( "orchestration_v2_projection_runs_last_executed_idx" ) ,
5397+ ) ;
5398+ assert . notStrictEqual ( lastExecutedIndex , - 1 ) ;
5399+ // The lookup that follows the search is the sort it no longer needs.
5400+ assert . notInclude ( plan [ lastExecutedIndex + 1 ] ?? "" , "USE TEMP B-TREE" ) ;
5401+
5402+ const countQueries = statements . filter ( ( statement ) => statement . includes ( "item_count" ) ) ;
5403+ assert . isNotEmpty ( countQueries ) ;
5404+ for ( const countQuery of countQueries ) {
5405+ for ( const detail of yield * planOf ( countQuery ) ) {
5406+ assert . notInclude ( detail , "orchestration_v2_projection_turn_items" ) ;
5407+ }
5408+ }
53855409 } ) ,
53865410 ) ;
53875411
@@ -5636,6 +5660,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
56365660 } ,
56375661 } ) ;
56385662 }
5663+ let runWrites = 0 ;
56395664 const applyRun = (
56405665 threadId : ThreadId ,
56415666 suffix : string ,
@@ -5644,9 +5669,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
56445669 ) => {
56455670 const runId = RunId . make ( `run:shell-item-counts:${ suffix } ` ) ;
56465671 const rootNodeId = NodeId . make ( `node:shell-item-counts:${ suffix } ` ) ;
5672+ runWrites += 1 ;
56475673 return projectionStore
56485674 . apply ( {
5649- id : EventId . make ( `event:shell-item-counts:${ suffix } :run` ) ,
5675+ id : EventId . make ( `event:shell-item-counts:${ suffix } :run: ${ runWrites } ` ) ,
56505676 type : "run.created" ,
56515677 threadId,
56525678 runId,
@@ -5673,11 +5699,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
56735699 } )
56745700 . pipe ( Effect . as ( runId ) ) ;
56755701 } ;
5676- let ordinal = 0 ;
5677- const applyItem = ( threadId : ThreadId , runId : RunId | null ) => {
5678- ordinal += 1 ;
5702+ let items = 0 ;
5703+ let writes = 0 ;
5704+ // Passing `rewrite` writes that earlier item again, possibly under another run.
5705+ const applyItem = ( threadId : ThreadId , runId : RunId | null , rewrite ?: number ) => {
5706+ const ordinal = rewrite ?? ( items += 1 ) ;
5707+ writes += 1 ;
56795708 return projectionStore . apply ( {
5680- id : EventId . make ( `event:shell-item-counts:item :${ ordinal } ` ) ,
5709+ id : EventId . make ( `event:shell-item-counts:write :${ writes } ` ) ,
56815710 type : "turn-item.updated" ,
56825711 threadId,
56835712 ...( runId === null ? { } : { runId } ) ,
@@ -5730,6 +5759,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
57305759 ) ;
57315760 assert . equal ( shell ?. itemCount , itemCount ) ;
57325761 }
5762+
5763+ // Writing an item again keeps its count; moving it to another run moves the count.
5764+ const itemCountOf = ( threadId : ThreadId ) =>
5765+ projectionStore . getThreadShell ( threadId ) . pipe ( Effect . map ( ( shell ) => shell ?. itemCount ) ) ;
5766+ yield * applyItem ( busyThreadId , completedRunId , 3 ) ;
5767+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 4 ) ;
5768+ yield * applyItem ( busyThreadId , rolledBackRunId , 3 ) ;
5769+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 3 ) ;
5770+ yield * applyItem ( busyThreadId , null , 3 ) ;
5771+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 4 ) ;
5772+
5773+ // A run's rollback uncounts its items, and writing the run again counts them back.
5774+ yield * applyRun ( busyThreadId , "busy-completed" , 1 , true ) ;
5775+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 3 ) ;
5776+ yield * applyRun ( busyThreadId , "busy-completed" , 1 , false ) ;
5777+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 4 ) ;
5778+ // Items written before their run exist count once it does.
5779+ const lateRunId = RunId . make ( "run:shell-item-counts:late" ) ;
5780+ yield * applyItem ( otherThreadId , lateRunId ) ;
5781+ assert . equal ( yield * itemCountOf ( otherThreadId ) , 1 ) ;
5782+ yield * applyRun ( otherThreadId , "late" , 2 , false ) ;
5783+ assert . equal ( yield * itemCountOf ( otherThreadId ) , 2 ) ;
57335784 } ) ,
57345785 ) ;
57355786
0 commit comments