@@ -27,6 +27,7 @@ import * as DateTime from "effect/DateTime";
2727import * as Effect from "effect/Effect" ;
2828import * as Layer from "effect/Layer" ;
2929import * as Schema from "effect/Schema" ;
30+ import * as Tracer from "effect/Tracer" ;
3031import * as SqlClient from "effect/sql/SqlClient" ;
3132import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness" ;
3233
@@ -588,12 +589,26 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
588589 } ) ;
589590 yield * sql `INSERT INTO orchestration_v2_projection_turn_items ${ sql . insert ( rows ) } ` ;
590591 }
592+ const statements : Array < string > = [ ] ;
593+ const tracer = Tracer . make ( {
594+ span ( options ) {
595+ const span = new Tracer . NativeSpan ( options ) ;
596+ const end = span . end . bind ( span ) ;
597+ span . end = ( endTime , exit ) => {
598+ end ( endTime , exit ) ;
599+ const query = span . attributes . get ( "db.query.text" ) ;
600+ if ( typeof query === "string" ) statements . push ( query ) ;
601+ } ;
602+ return span ;
603+ } ,
604+ } ) ;
591605 const initial = yield * Effect . acquireUseRelease (
592606 Effect . sync ( ( ) => vi . spyOn ( JSON , "parse" ) ) ,
593607 ( parse ) =>
594608 projectionStore
595609 . getThreadSnapshotWindow ( threadId , { rowLimit : 77 , userTurnLimit : 10 } )
596610 . pipe (
611+ Effect . withTracer ( tracer ) ,
597612 Effect . tap ( ( ) =>
598613 Effect . sync ( ( ) => {
599614 // Tool outputs must not be allocated again just to collect cohort IDs.
@@ -606,6 +621,35 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
606621 ) ,
607622 ( parse ) => Effect . sync ( ( ) => parse . mockRestore ( ) ) ,
608623 ) ;
624+ // Large threads blocked the server for over a second here. The boundary
625+ // must come from the user-message index rather than a scan of every tool
626+ // row, and payloads must not go through a sort after they are fetched.
627+ const windowStatement = statements . find ( ( statement ) => statement . includes ( "turn_anchors" ) ) ;
628+ assert . isDefined ( windowStatement ) ;
629+ const windowPlan = yield * sql . unsafe < { readonly parent : number ; readonly detail : string } > (
630+ `EXPLAIN QUERY PLAN ${ windowStatement } ` ,
631+ ) ;
632+ assert . match (
633+ windowPlan . map ( ( row ) => row . detail ) . join ( "\n" ) ,
634+ / S E A R C H i t e m U S I N G I N D E X o r c h e s t r a t i o n _ v 2 _ p r o j e c t i o n _ t u r n _ i t e m s _ u s e r _ m e s s a g e _ i d x \( t h r e a d _ i d = \? A N D o r d i n a l < \? \) / ,
635+ ) ;
636+ const topLevel = windowPlan . filter ( ( row ) => row . parent === 0 ) . map ( ( row ) => row . detail ) ;
637+ assert . include (
638+ topLevel ,
639+ "SEARCH item USING INDEX sqlite_autoindex_orchestration_v2_projection_turn_items_1 (turn_item_id=?)" ,
640+ ) ;
641+ assert . notInclude ( topLevel , "USE TEMP B-TREE FOR ORDER BY" ) ;
642+ const nodeStatement = statements . find ( ( statement ) =>
643+ statement . includes ( "WITH RECURSIVE retained" ) ,
644+ ) ;
645+ assert . isDefined ( nodeStatement ) ;
646+ const nodePlan = yield * sql . unsafe < { readonly detail : string } > (
647+ `EXPLAIN QUERY PLAN ${ nodeStatement } ` ,
648+ ) ;
649+ assert . include (
650+ nodePlan . map ( ( row ) => row . detail ) ,
651+ "SEARCH orchestration_v2_projection_nodes USING INDEX orchestration_v2_projection_nodes_live_idx (thread_id=?)" ,
652+ ) ;
609653 // Only the selected turn cohort and two lookahead anchors are decoded.
610654 assert . lengthOf ( initial . projection . turnItems , 12 * 102 ) ;
611655 const bounded = buildBoundedThreadProjection ( {
0 commit comments