@@ -66,7 +66,6 @@ import {
6666import {
6767 createOrchestrationV2TurnItemVisibility ,
6868 isOrchestrationV2SupersededInterrupt ,
69- isOrchestrationV2TurnItemVisible ,
7069} from "@t3tools/shared/orchestrationV2Timeline" ;
7170import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork" ;
7271import * as Context from "effect/Context" ;
@@ -1182,22 +1181,18 @@ function sortMessagesByTurnItemOrder(
11821181function activeLocalTurnItems (
11831182 projection : OrchestrationV2ThreadProjection ,
11841183) : Array < OrchestrationV2ProjectedTurnItem > {
1185- return projection . turnItems
1186- . filter ( ( item ) =>
1187- isOrchestrationV2TurnItemVisible ( {
1188- item,
1189- runs : projection . runs ,
1190- attempts : projection . attempts ,
1191- items : projection . turnItems ,
1192- } ) ,
1193- )
1194- . map ( ( item , position ) => ( {
1195- position,
1196- visibility : "local" as const ,
1197- sourceThreadId : item . threadId ,
1198- sourceItemId : item . id ,
1199- item,
1200- } ) ) ;
1184+ const isVisible = createOrchestrationV2TurnItemVisibility ( {
1185+ runs : projection . runs ,
1186+ attempts : projection . attempts ,
1187+ items : projection . turnItems ,
1188+ } ) ;
1189+ return projection . turnItems . filter ( isVisible ) . map ( ( item , position ) => ( {
1190+ position,
1191+ visibility : "local" as const ,
1192+ sourceThreadId : item . threadId ,
1193+ sourceItemId : item . id ,
1194+ item,
1195+ } ) ) ;
12011196}
12021197
12031198function localVisibleTurnItems (
@@ -2722,7 +2717,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
27222717 `
27232718 : yield * sql < PayloadRow > `
27242719 WITH eligible AS NOT MATERIALIZED (
2725- SELECT item.payload_json, item.ordinal, item.turn_item_id,
2720+ SELECT item.rowid AS item_rowid, item. payload_json, item.ordinal, item.turn_item_id,
27262721 item.run_id, item.node_id, item.type
27272722 FROM orchestration_v2_projection_turn_items AS item
27282723 LEFT JOIN orchestration_v2_projection_runs AS run
@@ -2811,7 +2806,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28112806 -- Choose and sort rows by ID, then fetch payloads in that
28122807 -- order. A turn window can hold megabytes of tool output, and
28132808 -- carrying it through the union and sort cost about a second.
2814- SELECT ordinal, turn_item_id, run_id, type
2809+ SELECT item_rowid, ordinal, turn_item_id, run_id, type
28152810 FROM eligible
28162811 WHERE ordinal >= (SELECT ordinal FROM boundary)
28172812 ORDER BY ordinal DESC, turn_item_id DESC
@@ -2821,19 +2816,19 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28212816 ELSE ${ window . rowLimit }
28222817 END
28232818 ), retained AS MATERIALIZED (
2824- SELECT ordinal, turn_item_id FROM selected
2819+ SELECT item_rowid, ordinal, turn_item_id FROM selected
28252820 UNION
2826- SELECT request.ordinal, request.turn_item_id
2821+ SELECT request.rowid AS item_rowid, request. ordinal, request.turn_item_id
28272822 FROM orchestration_v2_projection_turn_items AS request
28282823 WHERE request.run_id IN (
28292824 SELECT run_id FROM selected
28302825 WHERE type = 'run_interrupt_result' AND run_id IS NOT NULL
28312826 )
28322827 AND request.type = 'run_interrupt_request'
28332828 UNION
2834- SELECT latest.ordinal, latest.turn_item_id
2829+ SELECT latest.item_rowid, latest. ordinal, latest.turn_item_id
28352830 FROM (
2836- SELECT ordinal, turn_item_id
2831+ SELECT rowid AS item_rowid, ordinal, turn_item_id
28372832 FROM orchestration_v2_projection_turn_items
28382833 WHERE thread_id = ${ threadId }
28392834 AND ${ window . anchorItemId ?? null } IS NULL
@@ -2844,11 +2839,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28442839 ORDER BY ordinal ASC, turn_item_id ASC
28452840 )
28462841 -- CROSS JOIN keeps the sorted IDs as the outer loop, so SQLite
2847- -- skips sorting again once the payloads are attached.
2842+ -- skips sorting again once the payloads are attached. Carry the
2843+ -- rowid through selection to avoid a second string-key index lookup.
28482844 SELECT item.payload_json
28492845 FROM retained
28502846 CROSS JOIN orchestration_v2_projection_turn_items AS item
2851- ON item.turn_item_id = retained.turn_item_id
2847+ ON item.rowid = retained.item_rowid
28522848 ORDER BY retained.ordinal ASC, retained.turn_item_id ASC
28532849 ` ;
28542850 // Reuse the decoded items for cohort IDs and the resulting projection.
@@ -2865,7 +2861,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28652861 // A run can contain thousands of completed nodes. Load the visible
28662862 // nodes and live control dependencies, then walk only their ancestors.
28672863 const cohortNodeIds =
2868- window === undefined
2864+ window === undefined ||
2865+ ( fields !== undefined &&
2866+ fields . every (
2867+ ( field ) => field === "runs" || field === "attempts" || field === "turnItems" ,
2868+ ) )
28692869 ? cohortJson ( "nodeId" )
28702870 : encodeIdList (
28712871 ( yield * sql < { readonly node_id : string } > `
@@ -2900,7 +2900,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
29002900 INNER JOIN retained ON parent.node_id = retained.node_id
29012901 WHERE parent.thread_id = ${ threadId } AND parent.parent_node_id IS NOT NULL
29022902 )
2903- SELECT node_id FROM retained WHERE node_id IS NOT NULL
2903+ -- Sort IDs before fetching bodies, so large payloads never enter
2904+ -- SQLite's temporary sort. The node read restores this order by key.
2905+ SELECT retained.node_id
2906+ FROM retained
2907+ LEFT JOIN orchestration_v2_projection_nodes AS node
2908+ ON node.node_id = retained.node_id AND node.thread_id = ${ threadId }
2909+ WHERE retained.node_id IS NOT NULL
2910+ ORDER BY COALESCE(node.started_at, ''), retained.node_id ASC
29042911 ` ) . map ( ( row ) => row . node_id ) ,
29052912 ) ;
29062913 const cohortProviderThreadIds = cohortJson ( "providerThreadId" ) ;
@@ -2970,13 +2977,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
29702977 FROM orchestration_v2_projection_nodes
29712978 WHERE thread_id = ${ threadId }
29722979 ORDER BY COALESCE(started_at, ''), node_id ASC
2973- `
2974- : sql < PayloadRow > `
2975- SELECT payload_json FROM orchestration_v2_projection_nodes
2976- WHERE thread_id = ${ threadId }
2977- AND node_id IN (SELECT value FROM json_each(${ cohortNodeIds } ))
2978- ORDER BY COALESCE(started_at, ''), node_id ASC
2979- ` ,
2980+ `
2981+ : sql < PayloadRow & { readonly position : number } > `
2982+ -- Keep retained IDs outermost so SQLite looks up each node by its
2983+ -- primary key instead of scanning every historical node in the thread.
2984+ SELECT node.payload_json, retained.key AS position
2985+ FROM json_each(${ cohortNodeIds } ) AS retained
2986+ CROSS JOIN orchestration_v2_projection_nodes AS node
2987+ ON node.node_id = retained.value
2988+ WHERE node.thread_id = ${ threadId }
2989+ ` . pipe (
2990+ Effect . map ( ( rows ) =>
2991+ rows . toSorted ( ( left , right ) => left . position - right . position ) ,
2992+ ) ,
2993+ ) ,
29802994 fields !== undefined && ! fields . includes ( "subagents" )
29812995 ? Effect . succeed ( [ ] )
29822996 : window === undefined
@@ -3257,7 +3271,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
32573271 visibleTurnItems : [ ] ,
32583272 updatedAt : thread . updatedAt ,
32593273 } satisfies OrchestrationV2ThreadProjection ;
3260- return fields === undefined ? withLocalVisibleTurnItems ( projection ) : projection ;
3274+ return projection ;
32613275 } ) . pipe (
32623276 Effect . mapError ( ( cause ) =>
32633277 isProjectionStoreThreadNotFoundError ( cause )
@@ -3289,7 +3303,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
32893303 ( window ?. historyAnchor !== undefined && window . historyAnchor . threadId !== threadId )
32903304 ? { ...window , rowLimit : 0 , anchorItemId : undefined }
32913305 : window ;
3292- const projection = yield * readCanonicalProjection ( threadId , localWindow ) ;
3306+ // Ancestors contribute timeline items, not their provider/control state.
3307+ // Keep their runs and attempts for fork boundaries and visibility.
3308+ const projection = yield * readCanonicalProjection (
3309+ threadId ,
3310+ localWindow ,
3311+ window !== undefined && seenThreadIds . size > 0
3312+ ? [ "runs" , "attempts" , "turnItems" ]
3313+ : undefined ,
3314+ ) ;
32933315 const forkedFrom = projection . thread . forkedFrom ;
32943316 if ( forkedFrom ?. type !== "run" || seenThreadIds . has ( forkedFrom . threadId ) ) {
32953317 return withLocalVisibleTurnItems ( projection ) ;
0 commit comments