@@ -372,6 +372,12 @@ export interface ProjectionRecordFilter {
372372 * run is active or `except` names them.
373373 */
374374 readonly omitSettledSubagents ?: true | { readonly except : ReadonlyArray < NodeId > } ;
375+ /**
376+ * Also skips the provider thread of each subagent that `omitSettledSubagents: true` skips, and
377+ * with `omitSettledLeafNodes` its ended node. A provider thread that shares a provider session
378+ * with the thread stays.
379+ */
380+ readonly omitSettledSubagentRecords ?: boolean ;
375381}
376382
377383// A fork inherits its source's history before it has messages of its own,
@@ -2098,6 +2104,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
20982104 )
20992105 )` ;
21002106
2107+ // The settled index holds every column the check reads, so the settled tasks are dropped
2108+ // there and their rows, each with a full prompt, stay unread.
2109+ const unsettledSubagentRowids = ( threadId : ThreadId ) => sql `
2110+ SELECT candidate.rowid
2111+ FROM orchestration_v2_projection_subagents AS candidate
2112+ INDEXED BY orchestration_v2_projection_subagents_settled_idx
2113+ WHERE candidate.thread_id = ${ threadId }
2114+ AND NOT ${ settledSubagent ( sql `candidate` ) }
2115+ ` ;
2116+
21012117 // A todo list is never closed when its run ends, so a long thread holds an active one per
21022118 // old run. Counting those as live work drags each run's nodes and subagents into every window.
21032119 const todoListOfSettledRun = ( row : Statement . Fragment ) => sql `(
@@ -3738,6 +3754,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
37383754 AND (kind IN ('tool_call', 'assistant_message', 'reasoning')
37393755 AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
37403756 AND run_id IS NOT NULL) = 0
3757+ ${
3758+ filter . omitSettledSubagentRecords === true
3759+ ? // Drops the ended subagent nodes inside migration 084's index, whose
3760+ // predicate this must match. The last branch brings back the node of
3761+ // each subagent the read keeps.
3762+ sql `EXCEPT
3763+ SELECT rowid FROM orchestration_v2_projection_nodes
3764+ INDEXED BY orchestration_v2_projection_nodes_ended_subagent_idx
3765+ WHERE thread_id = ${ threadId }
3766+ AND kind = 'subagent'
3767+ AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3768+ AND run_id IS NOT NULL`
3769+ : sql ``
3770+ }
37413771 UNION ALL
37423772 SELECT rowid FROM orchestration_v2_projection_nodes
37433773 WHERE thread_id = ${ threadId }
@@ -3748,6 +3778,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
37483778 )
37493779 AND kind IN ('tool_call', 'assistant_message', 'reasoning')
37503780 AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3781+ ${
3782+ filter . omitSettledSubagentRecords === true
3783+ ? sql `UNION ALL
3784+ SELECT kept.rowid
3785+ FROM orchestration_v2_projection_subagents AS owner
3786+ CROSS JOIN orchestration_v2_projection_nodes AS kept
3787+ ON kept.node_id = owner.subagent_id
3788+ WHERE owner.rowid IN (${ unsettledSubagentRowids ( threadId ) } )`
3789+ : sql ``
3790+ }
37513791 )`
37523792 : sql `${ threadColumn ( filter ?. nodeIds , "node.thread_id" ) } = ${ threadId } `
37533793 }
@@ -3780,15 +3820,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
37803820 ${ filter ?. subagentRunIds === undefined ? sql `` : sql `AND run_id IN (SELECT value FROM json_each(${ encodeIdList ( filter . subagentRunIds ) } ))` }
37813821 ${
37823822 filter ?. omitSettledSubagents === true
3783- ? // The settled index holds every column this check reads, so the settled
3784- // tasks are dropped there and their rows, each with a full prompt, stay unread.
3785- sql `AND subagent.rowid IN (
3786- SELECT candidate.rowid
3787- FROM orchestration_v2_projection_subagents AS candidate
3788- INDEXED BY orchestration_v2_projection_subagents_settled_idx
3789- WHERE candidate.thread_id = ${ threadId }
3790- AND NOT ${ settledSubagent ( sql `candidate` ) }
3791- )`
3823+ ? sql `AND subagent.rowid IN (${ unsettledSubagentRowids ( threadId ) } )`
37923824 : filter ?. omitSettledSubagents !== undefined
37933825 ? sql `AND (
37943826 subagent_id IN (SELECT value FROM json_each(${ encodeIdList ( filter . omitSettledSubagents . except ) } ))
@@ -3838,12 +3870,35 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
38383870 FROM orchestration_v2_projection_provider_threads
38393871 WHERE (thread_id = ${ threadId }
38403872 OR owner_node_id IN (${ nodesOwningProviderThreads ( threadId ) } )
3841- OR provider_thread_id IN (
3873+ OR ${
3874+ filter ?. omitSettledSubagentRecords === true
3875+ ? // A dispatcher thread holds thousands of settled tasks. Their provider
3876+ // threads belong to the child threads; only a session this thread also
3877+ // uses is probed for background work when the thread detaches it.
3878+ sql `provider_thread_id IN (
3879+ SELECT provider_thread_id
3880+ FROM orchestration_v2_projection_subagents
3881+ WHERE rowid IN (${ unsettledSubagentRowids ( threadId ) } )
3882+ AND provider_thread_id IS NOT NULL
3883+ )
3884+ OR (provider_session_id IN (
3885+ SELECT provider_session_id
3886+ FROM orchestration_v2_projection_provider_session_bindings
3887+ WHERE thread_id = ${ threadId }
3888+ )
3889+ AND EXISTS (
3890+ SELECT 1 FROM orchestration_v2_projection_subagents AS owner
3891+ WHERE owner.provider_thread_id =
3892+ orchestration_v2_projection_provider_threads.provider_thread_id
3893+ AND owner.thread_id = ${ threadId }
3894+ ))`
3895+ : sql `provider_thread_id IN (
38423896 SELECT provider_thread_id
38433897 FROM orchestration_v2_projection_subagents
38443898 WHERE thread_id = ${ threadId }
38453899 AND provider_thread_id IS NOT NULL
3846- ))
3900+ )`
3901+ } )
38473902 ${ filter ?. providerThreadIds === undefined ? sql `` : sql `AND provider_thread_id IN (SELECT value FROM json_each(${ encodeIdList ( filter . providerThreadIds ) } ))` }
38483903 ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
38493904 `
0 commit comments