@@ -1562,7 +1562,9 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
15621562 [ "settled-reasoning" , settledRun , "reasoning" , "cancelled" ] ,
15631563 [ "settled-subagent" , settledRun , "subagent" , "completed" ] ,
15641564 [ "settled-run-live-tool" , settledRun , "tool_call" , "running" ] ,
1565+ [ "settled-run-todo" , settledRun , "todo_list" , "running" ] ,
15651566 [ "active-run-done-tool" , activeRun , "tool_call" , "completed" ] ,
1567+ [ "active-run-todo" , activeRun , "todo_list" , "running" ] ,
15661568 ] as const ;
15671569 for ( const [ name , run , kind , status ] of nodes ) {
15681570 const id = NodeId . make ( `node:settled-leaf-nodes:${ name } ` ) ;
@@ -1592,7 +1594,49 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
15921594 completedAt : status === "running" ? null : now ,
15931595 } ,
15941596 } ) ;
1597+ if ( kind === "todo_list" ) {
1598+ yield * store . apply ( {
1599+ id : EventId . make ( `event:settled-leaf-nodes:${ name } :plan` ) ,
1600+ type : "plan.updated" ,
1601+ threadId,
1602+ runId : run . id ,
1603+ nodeId : id ,
1604+ driver,
1605+ occurredAt : now ,
1606+ payload : {
1607+ id : PlanId . make ( `plan:settled-leaf-nodes:${ name } ` ) ,
1608+ threadId,
1609+ runId : run . id ,
1610+ nodeId : id ,
1611+ status : "active" ,
1612+ kind : "todo_list" ,
1613+ steps : [ ] ,
1614+ } ,
1615+ } ) ;
1616+ }
15951617 }
1618+ // No row or node of this run is in the window.
1619+ const settledCheckpointId = CheckpointId . make ( "checkpoint:settled-leaf-nodes:earlier-run" ) ;
1620+ yield * store . apply ( {
1621+ id : EventId . make ( "event:settled-leaf-nodes:earlier-run-checkpoint" ) ,
1622+ type : "checkpoint.captured" ,
1623+ threadId,
1624+ occurredAt : now ,
1625+ payload : {
1626+ id : settledCheckpointId ,
1627+ threadId,
1628+ scopeId : CheckpointScopeId . make ( "scope:settled-leaf-nodes:root" ) ,
1629+ runId : RunId . make ( "run:settled-leaf-nodes:earlier" ) ,
1630+ nodeId : NodeId . make ( "node:settled-leaf-nodes:earlier-root" ) ,
1631+ parentCheckpointId : null ,
1632+ ordinalWithinScope : 1 ,
1633+ appRunOrdinal : 1 ,
1634+ ref : CheckpointRef . make ( "refs/t3/settled-leaf-nodes/1" ) ,
1635+ status : "ready" ,
1636+ files : [ ] ,
1637+ capturedAt : now ,
1638+ } ,
1639+ } ) ;
15961640 const nodeNames = ( records : { readonly nodes : ReadonlyArray < { readonly id : string } > } ) =>
15971641 records . nodes
15981642 . map ( ( node ) => node . id . replace ( "node:settled-leaf-nodes:" , "" ) )
@@ -1601,12 +1645,32 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
16011645
16021646 assert . deepEqual (
16031647 nodeNames ( yield * store . getThreadRecords ( threadId , [ "nodes" ] , { omitSettledLeafNodes : true } ) ) ,
1604- [ "active-run-done-tool" , "settled-root" , "settled-run-live-tool" , "settled-subagent" ] ,
1648+ [
1649+ "active-run-done-tool" ,
1650+ "active-run-todo" ,
1651+ "settled-root" ,
1652+ "settled-run-live-tool" ,
1653+ "settled-run-todo" ,
1654+ "settled-subagent" ,
1655+ ] ,
16051656 ) ;
16061657 assert . deepEqual (
16071658 nodeNames ( yield * store . getThreadRecords ( threadId , [ "nodes" ] ) ) ,
16081659 nodes . map ( ( [ name ] ) => name ) . toSorted ( ) ,
16091660 ) ;
1661+ // A todo list stays running after its run ends. Counted as live work, every old run of a
1662+ // long thread would ride along with each window, with its nodes and subagents.
1663+ const liveWindow = ( yield * store . getThreadSnapshotWindow ( threadId , { rowLimit : 1 } ) ) . projection ;
1664+ assert . deepEqual ( nodeNames ( liveWindow ) , [ "active-run-todo" , "settled-run-live-tool" ] ) ;
1665+ assert . deepEqual (
1666+ liveWindow . plans . map ( ( plan ) => plan . id ) ,
1667+ [ PlanId . make ( "plan:settled-leaf-nodes:active-run-todo" ) ] ,
1668+ ) ;
1669+ // Clients show the diff of the last finished turn from this record.
1670+ assert . deepEqual (
1671+ liveWindow . checkpoints . map ( ( checkpoint ) => checkpoint . id ) ,
1672+ [ settledCheckpointId ] ,
1673+ ) ;
16101674
16111675 const subagents = [
16121676 [ "delivered" , settledRun , "completed" , "delivered" ] ,
@@ -3663,6 +3727,92 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
36633727 SET type = 'user_message', payload_json = json_set(payload_json, '$.type', 'user_message')
36643728 WHERE thread_id = ${ threadId } AND ordinal = 1
36653729 ` ;
3730+ // A running run folds nothing, so its newest rows are the window. Reading them must not
3731+ // walk the rest of the turn: 999 commands here, tens of thousands after a day of work.
3732+ const setSharedRunStatus = ( status : "running" | "completed" ) => sql `
3733+ UPDATE orchestration_v2_projection_runs
3734+ SET status = ${ status } , payload_json = json_set(payload_json, '$.status', ${ status } )
3735+ WHERE run_id = ${ sharedRunId } ` ;
3736+ yield * setSharedRunStatus ( "running" ) ;
3737+ // Another thread's rows that carry this run's id must not count toward the tail.
3738+ const foreignThreadId = "thread:bounded-sql-history:foreign" ;
3739+ yield * sql `
3740+ INSERT INTO orchestration_v2_projection_turn_items (
3741+ turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
3742+ parent_item_id, ordinal, type, status, updated_at, payload_json
3743+ )
3744+ SELECT turn_item_id || ':foreign', ${ foreignThreadId } , run_id, NULL, NULL, NULL,
3745+ NULL, ordinal + 5000, type, status, updated_at, payload_json
3746+ FROM orchestration_v2_projection_turn_items
3747+ WHERE thread_id = ${ threadId } AND ordinal > 400` ;
3748+ const liveStatements : Array < readonly [ string , ReadonlyArray < unknown > ] > = [ ] ;
3749+ const liveWindow = yield * projectionStore
3750+ . getThreadSnapshotWindow ( threadId , {
3751+ rowLimit : sqlPageLimit ,
3752+ userTurnLimit : 10 ,
3753+ deferWork : true ,
3754+ } )
3755+ . pipe (
3756+ Effect . provideService ( Statement . CurrentTransformer , ( statement ) => {
3757+ liveStatements . push ( statement . compile ( ) ) ;
3758+ return Effect . succeed ( statement ) ;
3759+ } ) ,
3760+ ) ;
3761+ assert . deepEqual (
3762+ liveWindow . projection . turnItems . map ( ( item ) => item . ordinal ) ,
3763+ Array . from ( { length : sqlPageLimit } , ( _ , index ) => 1001 - sqlPageLimit + index ) ,
3764+ ) ;
3765+ {
3766+ const livePath = "file:/thread-window-live-steps?vfs=memdb" ;
3767+ using copy = new NodeSqlite . DatabaseSync ( livePath ) ;
3768+ yield * sql `VACUUM INTO ${ livePath } ` ;
3769+ const liveSteps = ( statements = liveStatements ) =>
3770+ statements . reduce ( ( steps , [ query , params ] ) => {
3771+ const statement = copy . prepare ( query ) ;
3772+ statement . all ( ...( params as ReadonlyArray < NodeSqlite . SQLInputValue > ) ) ;
3773+ return steps + statement . stat ( "vmStep" ) ;
3774+ } , 0 ) ;
3775+ const deleteOrdinals = ( from : number , to : number ) =>
3776+ copy . exec (
3777+ `DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN ${ from } AND ${ to } ` ,
3778+ ) ;
3779+ // Nothing below the newest 502 rows is read to find the tail.
3780+ const fullSteps = liveSteps ( ) ;
3781+ assert . isAbove ( fullSteps , 0 ) ;
3782+ deleteOrdinals ( 2 , 498 ) ;
3783+ assert . isAtMost ( fullSteps , liveSteps ( ) * 1.1 ) ;
3784+ // The rows themselves are read only from the newest page, not from all 502.
3785+ const rowStatements = liveStatements . filter ( ( [ query ] ) => query . includes ( "work_segments" ) ) ;
3786+ assert . lengthOf ( rowStatements , 1 ) ;
3787+ const pageSteps = liveSteps ( rowStatements ) ;
3788+ deleteOrdinals ( 499 , 900 ) ;
3789+ assert . isAtMost ( pageSteps , liveSteps ( rowStatements ) * 1.1 ) ;
3790+ // The tail search reads conversation rows from their own index, not past the work rows.
3791+ const tailSearch = liveStatements . find ( ( [ query ] ) => query . includes ( "visible_tail" ) ) ;
3792+ assert . isDefined ( tailSearch ) ;
3793+ const plan = copy
3794+ . prepare ( `EXPLAIN QUERY PLAN ${ tailSearch ! [ 0 ] } ` )
3795+ . all ( ...( tailSearch ! [ 1 ] as ReadonlyArray < NodeSqlite . SQLInputValue > ) )
3796+ . map ( ( row ) => String ( row . detail ) ) ;
3797+ assert . isTrue (
3798+ plan . some ( ( detail ) => detail . includes ( "turn_items_conversation_idx" ) ) ,
3799+ plan . join ( "\n" ) ,
3800+ ) ;
3801+ }
3802+ const olderLivePage = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
3803+ rowLimit : sqlPageLimit ,
3804+ userTurnLimit : 10 ,
3805+ deferWork : true ,
3806+ anchorItemId : TurnItemId . make ( "turn-item:bounded-sql-history:600" ) ,
3807+ } ) ;
3808+ // Rows above the anchor must not set the tail, or the page would start above its end.
3809+ assert . deepEqual (
3810+ olderLivePage . projection . turnItems . map ( ( item ) => item . ordinal ) ,
3811+ Array . from ( { length : sqlPageLimit } , ( _ , index ) => 601 - sqlPageLimit + index ) ,
3812+ ) ;
3813+ yield * sql `DELETE FROM orchestration_v2_projection_turn_items
3814+ WHERE thread_id = ${ foreignThreadId } ` ;
3815+ yield * setSharedRunStatus ( "completed" ) ;
36663816 yield * sql `
36673817 UPDATE orchestration_v2_projection_turn_items
36683818 SET run_id = 'run:bounded-sql-history:' || ordinal,
0 commit comments