@@ -393,6 +393,15 @@ export interface ShellSnapshotOptions {
393393 readonly threadIds ?: ReadonlyArray < ThreadId > ;
394394}
395395
396+ /** What a shell subscriber's trims read from an active thread, without building its shell. */
397+ export interface ActiveShellIndexEntry extends Pick <
398+ OrchestrationV2ThreadShell ,
399+ "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pinnedAt" | "snoozedUntil"
400+ > {
401+ /** False only when no run, request, work item, or watch can make the shell live. */
402+ readonly mayBeLive : boolean ;
403+ }
404+
396405export interface OlderSettledThreadsQuery {
397406 readonly before : OrchestrationV2OlderSettledCursor ;
398407 readonly projectIds ?: ReadonlyArray < ProjectId > | undefined ;
@@ -450,6 +459,13 @@ export interface ProjectionStoreV2Shape {
450459 ReadonlyArray < ThreadId > ,
451460 ProjectionStoreV2Error
452461 > ;
462+ /** Threads that are not deleted, archived ones included. */
463+ readonly countThreads : ( ) => Effect . Effect < number , ProjectionStoreV2Error > ;
464+ /** Every active thread, in shell order. */
465+ readonly listActiveShellIndex : ( ) => Effect . Effect <
466+ ReadonlyArray < ActiveShellIndexEntry > ,
467+ ProjectionStoreV2Error
468+ > ;
453469 /** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
454470 readonly listOlderSettledThreads : (
455471 query : OlderSettledThreadsQuery ,
@@ -6614,6 +6630,98 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66146630 Effect . mapError ( shellSnapshotReadError ) ,
66156631 ) ;
66166632
6633+ const countThreads = Effect . fn ( "ProjectionStore.countThreads" ) ( function * ( ) {
6634+ const rows = yield * sql < { readonly count : number } > `
6635+ SELECT COUNT(*) AS count FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL
6636+ ` ;
6637+ return rows [ 0 ] ?. count ?? 0 ;
6638+ } , Effect . mapError ( shellSnapshotReadError ) ) ;
6639+
6640+ const listActiveShellIndex = Effect . fn ( "ProjectionStore.listActiveShellIndex" ) ( function * ( ) {
6641+ const rows = yield * sql < {
6642+ readonly thread_id : string ;
6643+ readonly project_id : string ;
6644+ readonly relationship_to_parent : string | null ;
6645+ readonly parent_thread_id : string | null ;
6646+ readonly root_thread_id : string ;
6647+ readonly settled_override : string | null ;
6648+ readonly settled_at : string | null ;
6649+ readonly pinned_at : string | null ;
6650+ readonly snoozed_until : string | null ;
6651+ readonly may_be_live : number ;
6652+ } > `
6653+ SELECT
6654+ t.thread_id,
6655+ t.project_id,
6656+ json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent,
6657+ json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id,
6658+ json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id,
6659+ json_extract(t.payload_json, '$.settledOverride') AS settled_override,
6660+ json_extract(t.payload_json, '$.settledAt') AS settled_at,
6661+ json_extract(t.payload_json, '$.pinnedAt') AS pinned_at,
6662+ json_extract(t.payload_json, '$.snoozedUntil') AS snoozed_until,
6663+ -- A superset of isLiveSubagentShell: the sources of its run status, pending request,
6664+ -- and pending background work.
6665+ (
6666+ EXISTS (
6667+ SELECT 1 FROM orchestration_v2_projection_runs r
6668+ WHERE r.thread_id = t.thread_id
6669+ AND r.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
6670+ )
6671+ OR EXISTS (
6672+ SELECT 1 FROM orchestration_v2_projection_runtime_requests request
6673+ WHERE request.thread_id = t.thread_id AND request.status = 'pending'
6674+ )
6675+ -- Same predicates as the shell's pending item and link reads, for their partial indexes.
6676+ OR EXISTS (
6677+ SELECT 1 FROM orchestration_v2_projection_turn_items item
6678+ WHERE item.thread_id = t.thread_id
6679+ AND item.type IN ('command_execution', 'dynamic_tool', 'subagent')
6680+ AND item.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')
6681+ )
6682+ OR EXISTS (
6683+ SELECT 1 FROM orchestration_v2_projection_turn_items link_request
6684+ WHERE link_request.thread_id = t.thread_id
6685+ AND link_request.type = 'link_request' AND link_request.status = 'waiting'
6686+ )
6687+ OR EXISTS (
6688+ SELECT 1 FROM orchestration_v2_projection_provider_threads provider_thread
6689+ WHERE provider_thread.thread_id = t.thread_id
6690+ AND json_array_length(provider_thread.payload_json, '$.pendingBackgroundTasks') > 0
6691+ )
6692+ OR EXISTS (
6693+ SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
6694+ WHERE json_type(link.value, '$.watch') IS NOT NULL
6695+ )
6696+ ) AS may_be_live
6697+ FROM orchestration_v2_projection_threads t
6698+ WHERE t.deleted_at IS NULL
6699+ AND json_extract(t.payload_json, '$.archivedAt') IS NULL
6700+ ORDER BY t.updated_at ASC, t.thread_id ASC
6701+ ` ;
6702+ return rows . map ( ( row ) : ActiveShellIndexEntry => ( {
6703+ id : ThreadId . make ( row . thread_id ) ,
6704+ projectId : ProjectId . make ( row . project_id ) ,
6705+ lineage : {
6706+ parentThreadId :
6707+ row . parent_thread_id === null ? null : ThreadId . make ( row . parent_thread_id ) ,
6708+ relationshipToParent :
6709+ row . relationship_to_parent === "fork" || row . relationship_to_parent === "subagent"
6710+ ? row . relationship_to_parent
6711+ : null ,
6712+ rootThreadId : ThreadId . make ( row . root_thread_id ) ,
6713+ } ,
6714+ settledOverride :
6715+ row . settled_override === "settled" || row . settled_override === "active"
6716+ ? row . settled_override
6717+ : null ,
6718+ settledAt : row . settled_at === null ? null : DateTime . makeUnsafe ( row . settled_at ) ,
6719+ pinnedAt : row . pinned_at === null ? null : DateTime . makeUnsafe ( row . pinned_at ) ,
6720+ snoozedUntil : row . snoozed_until === null ? null : DateTime . makeUnsafe ( row . snoozed_until ) ,
6721+ mayBeLive : row . may_be_live === 1 ,
6722+ } ) ) ;
6723+ } , Effect . mapError ( shellSnapshotReadError ) ) ;
6724+
66176725 const findGuestShareThreadId = Effect . fn ( "ProjectionStore.findGuestShareThreadId" ) ( function * (
66186726 shareId : GuestShareId ,
66196727 ) {
@@ -6768,6 +6876,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67686876 apply,
67696877 getShellSnapshot : readOnReader ( getShellSnapshot ) ,
67706878 listActiveSubagentThreadIds : readOnReader ( listActiveSubagentThreadIds ) ,
6879+ countThreads : readOnReader ( countThreads ) ,
6880+ listActiveShellIndex : readOnReader ( listActiveShellIndex ) ,
67716881 readShellSnapshot : readOnReader ( readShellSnapshot ) ,
67726882 listOlderSettledThreads : readOnReader ( listOlderSettledThreads ) ,
67736883 getThreadShell : readOnReader ( getThreadShell ) ,
0 commit comments