@@ -1550,6 +1550,25 @@ function itemCountThroughRun(input: {
15501550 return count ;
15511551}
15521552
1553+ /**
1554+ * Threads that some shell row forks from at a run. Only these need run
1555+ * ordinals and per-run item counts, to count the inherited prefix, so a thread
1556+ * nobody forked from never pays for a scan of its run history.
1557+ */
1558+ function shellForkSourceIds (
1559+ rows : ReadonlyArray < { readonly forked_from_run_source_thread_id : string | null } > ,
1560+ ) : ReadonlyArray < ThreadId > {
1561+ return [
1562+ ...new Set (
1563+ rows . flatMap ( ( row ) =>
1564+ row . forked_from_run_source_thread_id === null
1565+ ? [ ]
1566+ : [ ThreadId . make ( row . forked_from_run_source_thread_id ) ] ,
1567+ ) ,
1568+ ) ,
1569+ ] ;
1570+ }
1571+
15531572function visibleItemCountForShell ( input : {
15541573 readonly threadId : ThreadId ;
15551574 readonly statesByThreadId : ReadonlyMap < ThreadId , ShellThreadState > ;
@@ -4938,13 +4957,19 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
49384957 AND plan.kind = 'proposed_plan'
49394958 AND plan.status = 'active'
49404959 ) AS has_actionable_proposed_plan,
4960+ -- Count per run on the covering (thread_id, run_id) index, then
4961+ -- look up each run once, instead of one run lookup per item.
49414962 (
4942- SELECT COUNT(*)
4943- FROM orchestration_v2_projection_turn_items i
4963+ SELECT COALESCE(SUM(per_run.item_count), 0)
4964+ FROM (
4965+ SELECT i.run_id, COUNT(*) AS item_count
4966+ FROM orchestration_v2_projection_turn_items i
4967+ WHERE i.thread_id = t.thread_id
4968+ GROUP BY i.run_id
4969+ ) per_run
49444970 LEFT JOIN orchestration_v2_projection_runs r
4945- ON r.run_id = i.run_id
4946- WHERE i.thread_id = t.thread_id
4947- AND (i.run_id IS NULL OR r.status <> 'rolled_back')
4971+ ON r.run_id = per_run.run_id
4972+ WHERE per_run.run_id IS NULL OR r.status <> 'rolled_back'
49484973 ) AS item_count,
49494974 (
49504975 SELECT COUNT(*)
@@ -5426,14 +5451,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54265451 }
54275452 const threadRows = [ ...rowsByThreadId . values ( ) ] ;
54285453 const threadIds = [ ...rowsByThreadId . keys ( ) ] ;
5454+ const forkSourceIds = shellForkSourceIds ( threadRows ) ;
54295455 const readForThreadIds = < A > (
54305456 read : ( ids : ReadonlyArray < ThreadId > ) => Effect . Effect < ReadonlyArray < A > , unknown > ,
5431- ) =>
5432- threadIds . length === 0 ? Effect . succeed ( [ ] as ReadonlyArray < A > ) : read ( threadIds ) ;
5457+ ids : ReadonlyArray < ThreadId > = threadIds ,
5458+ ) => ( ids . length === 0 ? Effect . succeed ( [ ] as ReadonlyArray < A > ) : read ( ids ) ) ;
54335459 const [ runRows , itemCountRows , sequenceRows , providerThreadRows , pendingTurnItemRows ] =
54345460 yield * Effect . all ( [
5435- readForThreadIds ( selectShellRunRows ) ,
5436- readForThreadIds ( selectShellRunItemCounts ) ,
5461+ readForThreadIds ( selectShellRunRows , forkSourceIds ) ,
5462+ readForThreadIds ( selectShellRunItemCounts , forkSourceIds ) ,
54375463 sql < { readonly snapshot_sequence : number | null } > `
54385464 SELECT MAX(sequence) AS snapshot_sequence
54395465 FROM orchestration_events
@@ -5520,10 +5546,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55205546 }
55215547
55225548 const threadIds = [ ...rowsByThreadId . keys ( ) ] ;
5549+ const forkSourceIds = shellForkSourceIds ( [ ...rowsByThreadId . values ( ) ] ) ;
55235550 const [ runRows , itemCountRows , providerThreadRows , pendingTurnItemRows ] =
55245551 yield * Effect . all ( [
5525- selectShellRunRows ( threadIds ) ,
5526- selectShellRunItemCounts ( threadIds ) ,
5552+ forkSourceIds . length === 0
5553+ ? Effect . succeed ( [ ] as ReadonlyArray < ShellRunRow > )
5554+ : selectShellRunRows ( forkSourceIds ) ,
5555+ forkSourceIds . length === 0
5556+ ? Effect . succeed ( [ ] as ReadonlyArray < ShellRunItemCountRow > )
5557+ : selectShellRunItemCounts ( forkSourceIds ) ,
55275558 selectShellProviderThreadRows ( threadIds ) ,
55285559 selectShellPendingTurnItemRows ( threadIds ) ,
55295560 ] ) ;
0 commit comments