@@ -2028,6 +2028,24 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
20282028 `(${ DEFERRED_WORK_ITEM_TYPES . map ( ( type ) => `'${ type } '` ) . join ( ", " ) } )` ,
20292029 ) ;
20302030
2031+ // The shell counts a thread's items from these rows, so it never reads the thread's items.
2032+ // Items without a run count under run_id ''. `counted` mirrors whether the run exists and was
2033+ // not rolled back; run events keep it current.
2034+ const bumpRunItemCount = ( threadId : ThreadId , runId : RunId | null , delta : 1 | - 1 ) => sql `
2035+ INSERT INTO orchestration_v2_projection_run_item_counts (thread_id, run_id, item_count, counted)
2036+ VALUES (
2037+ ${ threadId } , ${ runId ?? "" } , ${ delta } ,
2038+ ${
2039+ runId === null
2040+ ? sql `1`
2041+ : sql `COALESCE(
2042+ (SELECT status <> 'rolled_back' FROM orchestration_v2_projection_runs
2043+ WHERE run_id = ${ runId } ), 0)`
2044+ }
2045+ )
2046+ ON CONFLICT(thread_id, run_id) DO UPDATE SET item_count = item_count + excluded.item_count
2047+ ` ;
2048+
20312049 // A todo list is never closed when its run ends, so a long thread holds an active one per
20322050 // old run. Counting those as live work drags each run's nodes and subagents into every window.
20332051 const todoListOfSettledRun = ( row : Statement . Fragment ) => sql `(
@@ -2230,6 +2248,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
22302248 keepRecordedRunField ( sql `excluded.payload_json` , "$.delegatedCompletion" ) ,
22312249 "$.restartCancelledBackgroundWork" ,
22322250 ) }
2251+ ` ;
2252+ yield * sql `
2253+ UPDATE orchestration_v2_projection_run_item_counts
2254+ SET counted = ${ event . payload . status === "rolled_back" ? 0 : 1 }
2255+ WHERE thread_id = ${ event . payload . threadId } AND run_id = ${ event . payload . id }
2256+ AND counted <> ${ event . payload . status === "rolled_back" ? 0 : 1 }
22332257 ` ;
22342258 break ;
22352259 }
@@ -2724,6 +2748,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
27242748 case "turn-item.updated" : {
27252749 const payloadJson = yield * encodeTurnItemPayload ( event . payload ) ;
27262750 const updatedAt = yield * encodePayloadTimestamp ( event . payload . updatedAt ) ;
2751+ const [ previous ] = yield * sql < {
2752+ readonly thread_id : ThreadId ;
2753+ readonly run_id : RunId | null ;
2754+ } > `
2755+ SELECT thread_id, run_id FROM orchestration_v2_projection_turn_items
2756+ WHERE turn_item_id = ${ event . payload . id }
2757+ ` ;
27272758 yield * sql `
27282759 INSERT INTO orchestration_v2_projection_turn_items (
27292760 turn_item_id,
@@ -2767,6 +2798,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
27672798 updated_at = excluded.updated_at,
27682799 payload_json = excluded.payload_json
27692800 ` ;
2801+ if (
2802+ previous ?. thread_id !== event . payload . threadId ||
2803+ previous . run_id !== event . payload . runId
2804+ ) {
2805+ if ( previous !== undefined ) {
2806+ yield * bumpRunItemCount ( previous . thread_id , previous . run_id , - 1 ) ;
2807+ }
2808+ yield * bumpRunItemCount ( event . payload . threadId , event . payload . runId , 1 ) ;
2809+ }
27702810 break ;
27712811 }
27722812 case "checkpoint-scope.created" : {
@@ -6719,28 +6759,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67196759
67206760 const selectShellRunItemCounts = ( threadIds : ReadonlyArray < ThreadId > ) =>
67216761 sql < ShellRunItemCountRow > `
6722- SELECT thread_id, run_id, COUNT(*) AS item_count
6723- FROM orchestration_v2_projection_turn_items
6724- WHERE run_id IS NOT NULL AND thread_id IN (SELECT value FROM json_each( ${ encodeIdList ( threadIds ) } ))
6725- GROUP BY thread_id, run_id
6762+ SELECT thread_id, run_id, item_count
6763+ FROM orchestration_v2_projection_run_item_counts
6764+ WHERE run_id <> '' AND item_count > 0
6765+ AND thread_id IN (SELECT value FROM json_each( ${ encodeIdList ( threadIds ) } ))
67266766 ` ;
67276767
6728- // Count all selected threads in one index walk; fork sources still need per-run counts.
6768+ // Fork sources still need per-run counts.
67296769 const selectShellItemCounts = ( threadIds : ReadonlyArray < ThreadId > ) =>
67306770 sql < ShellItemCountRow > `
6731- SELECT counts.thread_id,
6732- SUM(CASE WHEN counts.run_id IS NULL OR r.status <> 'rolled_back'
6733- THEN counts.item_count ELSE 0 END) AS item_count,
6734- SUM(CASE WHEN counts.run_id IS NULL THEN counts.item_count ELSE 0 END)
6735- AS runless_item_count
6736- FROM (
6737- SELECT thread_id, run_id, COUNT(*) AS item_count
6738- FROM orchestration_v2_projection_turn_items
6739- WHERE thread_id IN (SELECT value FROM json_each(${ encodeIdList ( threadIds ) } ))
6740- GROUP BY thread_id, run_id
6741- ) counts
6742- LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_id
6743- GROUP BY counts.thread_id
6771+ SELECT thread_id,
6772+ SUM(CASE WHEN counted = 1 THEN item_count ELSE 0 END) AS item_count,
6773+ SUM(CASE WHEN run_id = '' THEN item_count ELSE 0 END) AS runless_item_count
6774+ FROM orchestration_v2_projection_run_item_counts
6775+ WHERE thread_id IN (SELECT value FROM json_each(${ encodeIdList ( threadIds ) } ))
6776+ GROUP BY thread_id
67446777 ` ;
67456778
67466779 const selectShellProviderThreadRows = ( threadIds ?: ReadonlyArray < ThreadId > ) =>
0 commit comments