@@ -5221,7 +5221,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52215221 } ) ,
52225222 ) ;
52235223
5224- it . effect ( "looks up shell stop requests and active runs through their indexes " , ( ) =>
5224+ it . effect ( "reads shell runs and item counts without walking a thread's history " , ( ) =>
52255225 Effect . gen ( function * ( ) {
52265226 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
52275227 const sql = yield * SqlClient . SqlClient ;
@@ -5230,14 +5230,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52305230 yield * projectionStore
52315231 . getShellSnapshot ( { projectId, location : "active" } )
52325232 . pipe ( Effect . withTracer ( tracer ) ) ;
5233+ const planOf = ( statement : string ) =>
5234+ sql
5235+ . unsafe < { readonly detail : string } > ( `EXPLAIN QUERY PLAN ${ statement } ` )
5236+ . pipe ( Effect . map ( ( rows ) => rows . map ( ( row ) => row . detail ) ) ) ;
52335237
52345238 const threadQuery = statements . find ( ( statement ) =>
52355239 statement . includes ( "AS latest_run_stop_requested" ) ,
52365240 ) ;
52375241 assert . isDefined ( threadQuery ) ;
5238- const plan = ( yield * sql . unsafe < { readonly detail : string } > (
5239- `EXPLAIN QUERY PLAN ${ threadQuery } ` ,
5240- ) ) . map ( ( row ) => row . detail ) ;
5242+ const plan = yield * planOf ( threadQuery ) ;
52415243 const stopRequestLookups = plan . filter ( ( detail ) => detail . startsWith ( "SEARCH stop_request " ) ) ;
52425244 assert . lengthOf ( stopRequestLookups , 2 ) ;
52435245 for ( const lookup of stopRequestLookups ) {
@@ -5252,32 +5254,20 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52525254 for ( const lookup of activeRunLookups ) {
52535255 assert . include ( lookup , "orchestration_v2_projection_runs_thread_status_idx" ) ;
52545256 }
5255- } ) ,
5256- ) ;
5257-
5258- it . effect ( "reads a thread's last executed run through the last executed run index" , ( ) =>
5259- Effect . gen ( function * ( ) {
5260- const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
5261- const sql = yield * SqlClient . SqlClient ;
5262- const { projectId } = yield * assertProjectScopedShells ( "sql-shell-last-executed-index" ) ;
5263- const { statements, tracer } = traceSqlStatements ( ) ;
5264- yield * projectionStore
5265- . getShellSnapshot ( { projectId, location : "active" } )
5266- . pipe ( Effect . withTracer ( tracer ) ) ;
5267-
5268- const threadQuery = statements . find ( ( statement ) =>
5269- statement . includes ( "AS latest_run_stop_requested" ) ,
5270- ) ;
5271- assert . isDefined ( threadQuery ) ;
5272- const plan = ( yield * sql . unsafe < { readonly detail : string } > (
5273- `EXPLAIN QUERY PLAN ${ threadQuery } ` ,
5274- ) ) . map ( ( row ) => row . detail ) ;
52755257 const lastExecutedIndex = plan . findIndex ( ( detail ) =>
52765258 detail . includes ( "orchestration_v2_projection_runs_last_executed_idx" ) ,
52775259 ) ;
52785260 assert . notStrictEqual ( lastExecutedIndex , - 1 ) ;
52795261 // The lookup that follows the search is the sort it no longer needs.
52805262 assert . notInclude ( plan [ lastExecutedIndex + 1 ] ?? "" , "USE TEMP B-TREE" ) ;
5263+
5264+ const countQueries = statements . filter ( ( statement ) => statement . includes ( "item_count" ) ) ;
5265+ assert . isNotEmpty ( countQueries ) ;
5266+ for ( const countQuery of countQueries ) {
5267+ for ( const detail of yield * planOf ( countQuery ) ) {
5268+ assert . notInclude ( detail , "orchestration_v2_projection_turn_items" ) ;
5269+ }
5270+ }
52815271 } ) ,
52825272 ) ;
52835273
@@ -5532,6 +5522,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55325522 } ,
55335523 } ) ;
55345524 }
5525+ let runWrites = 0 ;
55355526 const applyRun = (
55365527 threadId : ThreadId ,
55375528 suffix : string ,
@@ -5540,9 +5531,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55405531 ) => {
55415532 const runId = RunId . make ( `run:shell-item-counts:${ suffix } ` ) ;
55425533 const rootNodeId = NodeId . make ( `node:shell-item-counts:${ suffix } ` ) ;
5534+ runWrites += 1 ;
55435535 return projectionStore
55445536 . apply ( {
5545- id : EventId . make ( `event:shell-item-counts:${ suffix } :run` ) ,
5537+ id : EventId . make ( `event:shell-item-counts:${ suffix } :run: ${ runWrites } ` ) ,
55465538 type : "run.created" ,
55475539 threadId,
55485540 runId,
@@ -5569,11 +5561,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
55695561 } )
55705562 . pipe ( Effect . as ( runId ) ) ;
55715563 } ;
5572- let ordinal = 0 ;
5573- const applyItem = ( threadId : ThreadId , runId : RunId | null ) => {
5574- ordinal += 1 ;
5564+ let items = 0 ;
5565+ let writes = 0 ;
5566+ // Passing `rewrite` writes that earlier item again, possibly under another run.
5567+ const applyItem = ( threadId : ThreadId , runId : RunId | null , rewrite ?: number ) => {
5568+ const ordinal = rewrite ?? ( items += 1 ) ;
5569+ writes += 1 ;
55755570 return projectionStore . apply ( {
5576- id : EventId . make ( `event:shell-item-counts:item :${ ordinal } ` ) ,
5571+ id : EventId . make ( `event:shell-item-counts:write :${ writes } ` ) ,
55775572 type : "turn-item.updated" ,
55785573 threadId,
55795574 ...( runId === null ? { } : { runId } ) ,
@@ -5626,6 +5621,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
56265621 ) ;
56275622 assert . equal ( shell ?. itemCount , itemCount ) ;
56285623 }
5624+
5625+ // Writing an item again keeps its count; moving it to another run moves the count.
5626+ const itemCountOf = ( threadId : ThreadId ) =>
5627+ projectionStore . getThreadShell ( threadId ) . pipe ( Effect . map ( ( shell ) => shell ?. itemCount ) ) ;
5628+ yield * applyItem ( busyThreadId , completedRunId , 3 ) ;
5629+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 4 ) ;
5630+ yield * applyItem ( busyThreadId , rolledBackRunId , 3 ) ;
5631+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 3 ) ;
5632+ yield * applyItem ( busyThreadId , null , 3 ) ;
5633+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 4 ) ;
5634+
5635+ // A run's rollback uncounts its items, and writing the run again counts them back.
5636+ yield * applyRun ( busyThreadId , "busy-completed" , 1 , true ) ;
5637+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 3 ) ;
5638+ yield * applyRun ( busyThreadId , "busy-completed" , 1 , false ) ;
5639+ assert . equal ( yield * itemCountOf ( busyThreadId ) , 4 ) ;
5640+ // Items written before their run exist count once it does.
5641+ const lateRunId = RunId . make ( "run:shell-item-counts:late" ) ;
5642+ yield * applyItem ( otherThreadId , lateRunId ) ;
5643+ assert . equal ( yield * itemCountOf ( otherThreadId ) , 1 ) ;
5644+ yield * applyRun ( otherThreadId , "late" , 2 , false ) ;
5645+ assert . equal ( yield * itemCountOf ( otherThreadId ) , 2 ) ;
56295646 } ) ,
56305647 ) ;
56315648
0 commit comments