@@ -382,6 +382,89 @@ const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
382382 ] ) ;
383383} ) ;
384384
385+ const commandReadsSkipSettledLeafNodes = Effect . gen ( function * ( ) {
386+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
387+ const now = yield * DateTime . now ;
388+ const threadId = yield * addRolledBackRecoveryCandidate ( "settled-leaf-nodes" ) ;
389+ const settledRun = ( yield * store . getThreadProjection ( threadId ) ) . runs [ 0 ] ! ;
390+ const activeRun = {
391+ ...settledRun ,
392+ id : RunId . make ( "run:settled-leaf-nodes:active" ) ,
393+ ordinal : 2 ,
394+ userMessageId : MessageId . make ( "message:settled-leaf-nodes:active" ) ,
395+ rootNodeId : NodeId . make ( "node:settled-leaf-nodes:active-root" ) ,
396+ status : "running" as const ,
397+ completedAt : null ,
398+ } ;
399+ yield * store . apply ( {
400+ id : EventId . make ( "event:settled-leaf-nodes:active-run" ) ,
401+ type : "run.created" ,
402+ threadId,
403+ runId : activeRun . id ,
404+ nodeId : activeRun . rootNodeId ,
405+ driver,
406+ providerInstanceId,
407+ occurredAt : now ,
408+ payload : activeRun ,
409+ } ) ;
410+ const nodes = [
411+ [ "settled-root" , settledRun , "root_turn" , "completed" ] ,
412+ [ "settled-tool" , settledRun , "tool_call" , "completed" ] ,
413+ [ "settled-message" , settledRun , "assistant_message" , "failed" ] ,
414+ [ "settled-reasoning" , settledRun , "reasoning" , "cancelled" ] ,
415+ [ "settled-subagent" , settledRun , "subagent" , "completed" ] ,
416+ [ "settled-run-live-tool" , settledRun , "tool_call" , "running" ] ,
417+ [ "active-run-done-tool" , activeRun , "tool_call" , "completed" ] ,
418+ ] as const ;
419+ for ( const [ name , run , kind , status ] of nodes ) {
420+ const id = NodeId . make ( `node:settled-leaf-nodes:${ name } ` ) ;
421+ yield * store . apply ( {
422+ id : EventId . make ( `event:settled-leaf-nodes:${ name } ` ) ,
423+ type : "node.updated" ,
424+ threadId,
425+ runId : run . id ,
426+ nodeId : id ,
427+ driver,
428+ occurredAt : now ,
429+ payload : {
430+ id,
431+ threadId,
432+ runId : run . id ,
433+ rootNodeId : kind === "root_turn" ? id : run . rootNodeId ! ,
434+ parentNodeId : kind === "root_turn" ? null : run . rootNodeId ,
435+ kind,
436+ status,
437+ countsForRun : kind === "root_turn" ,
438+ providerThreadId : null ,
439+ providerTurnId : null ,
440+ nativeItemRef : null ,
441+ runtimeRequestId : null ,
442+ checkpointScopeId : null ,
443+ startedAt : now ,
444+ completedAt : status === "running" ? null : now ,
445+ } ,
446+ } ) ;
447+ }
448+ const nodeNames = ( records : { readonly nodes : ReadonlyArray < { readonly id : string } > } ) =>
449+ records . nodes
450+ . map ( ( node ) => node . id . replace ( "node:settled-leaf-nodes:" , "" ) )
451+ . filter ( ( name ) => nodes . some ( ( [ candidate ] ) => candidate === name ) )
452+ . toSorted ( ) ;
453+
454+ assert . deepEqual (
455+ nodeNames ( yield * store . getThreadRecords ( threadId , [ "nodes" ] , { omitSettledLeafNodes : true } ) ) ,
456+ [ "active-run-done-tool" , "settled-root" , "settled-run-live-tool" , "settled-subagent" ] ,
457+ ) ;
458+ assert . deepEqual (
459+ nodeNames ( yield * store . getThreadRecords ( threadId , [ "nodes" ] ) ) ,
460+ nodes . map ( ( [ name ] ) => name ) . toSorted ( ) ,
461+ ) ;
462+ } ) ;
463+
464+ it . effect ( "memory command reads skip finished leaf nodes of finished runs" , ( ) =>
465+ commandReadsSkipSettledLeafNodes . pipe ( Effect . provide ( ProjectionStore . layerMemory ) ) ,
466+ ) ;
467+
385468it . effect ( "memory worktree path lookup reads only active threads" , ( ) =>
386469 threadWorktreePathsReadActiveThreads . pipe ( Effect . provide ( ProjectionStore . layerMemory ) ) ,
387470) ;
@@ -657,6 +740,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
657740 assert . deepEqual ( recovery ?. restartCancelledBackgroundWork , work ) ;
658741 } ) ,
659742 ) ;
743+ it . effect (
744+ "command reads skip finished leaf nodes of finished runs" ,
745+ ( ) => commandReadsSkipSettledLeafNodes ,
746+ ) ;
660747 it . effect ( "limits turn-start history to the requested runs, including an empty selection" , ( ) =>
661748 Effect . gen ( function * ( ) {
662749 const store = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments