@@ -185,7 +185,10 @@ const EventReplayStatsRowSchema = Schema.Struct({
185185 eventCount : Schema . Number ,
186186 payloadBytes : Schema . Number ,
187187} ) ;
188- const ActiveThreadRowsRequest = Schema . Struct ( { unsettledOnly : Schema . Boolean } ) ;
188+ const ActiveThreadRowsRequest = Schema . Struct ( {
189+ unsettledOnly : Schema . Boolean ,
190+ settleWhenIdleOnly : Schema . Boolean ,
191+ } ) ;
189192const ProjectionThreadSearchRequest = Schema . Struct ( {
190193 pattern : Schema . String ,
191194 limit : Schema . Int ,
@@ -602,11 +605,11 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
602605 ` ,
603606 } ) ;
604607
605- // Background sweeps skip settled threads, the same check PR discovery makes.
606- const unsettledThreadsFilter = ( unsettledOnly : boolean ) =>
607- unsettledOnly
608- ? sql `AND threads.settled_at IS NULL AND threads.settled_override IS NOT 'settled'`
609- : sql ` `;
608+ /** Apply sweep filters to threads and every joined row, keeping recovery reads narrow. */
609+ const activeThreadsFilter = ( request : typeof ActiveThreadRowsRequest . Type ) => sql `
610+ ${ request . unsettledOnly ? sql `AND threads.settled_at IS NULL AND threads.settled_override IS NOT 'settled'` : sql `` }
611+ ${ request . settleWhenIdleOnly ? sql `AND threads.settle_when_idle_at IS NOT NULL` : sql `` }
612+ ` ;
610613
611614 const listActiveThreadRows = SqlSchema . findAll ( {
612615 Request : ActiveThreadRowsRequest ,
@@ -649,7 +652,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
649652 FROM projection_threads threads
650653 WHERE deleted_at IS NULL
651654 AND archived_at IS NULL
652- ${ unsettledThreadsFilter ( request . unsettledOnly ) }
655+ ${ activeThreadsFilter ( request ) }
653656 ORDER BY project_id ASC, created_at ASC, thread_id ASC
654657 ` ,
655658 } ) ;
@@ -809,7 +812,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
809812 ON threads.thread_id = links.thread_id
810813 WHERE threads.deleted_at IS NULL
811814 AND threads.archived_at IS NULL
812- ${ unsettledThreadsFilter ( request . unsettledOnly ) }
815+ ${ activeThreadsFilter ( request ) }
813816 ORDER BY links.thread_id ASC, links.linked_at ASC, links.number ASC
814817 ` ,
815818 } ) ;
@@ -939,7 +942,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
939942 ON threads.thread_id = sessions.thread_id
940943 WHERE threads.deleted_at IS NULL
941944 AND threads.archived_at IS NULL
942- ${ unsettledThreadsFilter ( request . unsettledOnly ) }
945+ ${ activeThreadsFilter ( request ) }
943946 ORDER BY sessions.thread_id ASC
944947 ` ,
945948 } ) ;
@@ -1035,7 +1038,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
10351038 WHERE threads.deleted_at IS NULL
10361039 AND threads.archived_at IS NULL
10371040 AND threads.latest_turn_id IS NOT NULL
1038- ${ unsettledThreadsFilter ( request . unsettledOnly ) }
1041+ ${ activeThreadsFilter ( request ) }
10391042 ORDER BY turns.thread_id ASC
10401043 ` ,
10411044 } ) ;
@@ -2111,6 +2114,7 @@ pending_approval_requests AS (
21112114 ` ,
21122115 } ) ;
21132116
2117+ /** Read complete persisted orchestration state for consumers that need thread bodies. */
21142118 const getSnapshot : ProjectionSnapshotQueryShape [ "getSnapshot" ] = ( ) =>
21152119 sql
21162120 . withTransaction (
@@ -2671,8 +2675,12 @@ pending_approval_requests AS (
26712675 } ) ,
26722676 ) ;
26732677
2678+ /** Read navigation state or a filtered recovery snapshot with a consistent sequence guard. */
26742679 const getShellSnapshot : ProjectionSnapshotQueryShape [ "getShellSnapshot" ] = ( options ) => {
2675- const unsettledOnly = options ?. unsettledOnly === true ;
2680+ const request = {
2681+ unsettledOnly : options ?. unsettledOnly === true ,
2682+ settleWhenIdleOnly : options ?. settleWhenIdleOnly === true ,
2683+ } ;
26762684 return sql
26772685 . withTransaction (
26782686 Effect . all ( [
@@ -2684,31 +2692,31 @@ pending_approval_requests AS (
26842692 ) ,
26852693 ) ,
26862694 ) ,
2687- listActiveThreadRows ( { unsettledOnly } ) . pipe (
2695+ listActiveThreadRows ( request ) . pipe (
26882696 Effect . mapError (
26892697 toPersistenceSqlOrDecodeError (
26902698 "ProjectionSnapshotQuery.getShellSnapshot:listThreads:query" ,
26912699 "ProjectionSnapshotQuery.getShellSnapshot:listThreads:decodeRows" ,
26922700 ) ,
26932701 ) ,
26942702 ) ,
2695- listActiveThreadSessionRows ( { unsettledOnly } ) . pipe (
2703+ listActiveThreadSessionRows ( request ) . pipe (
26962704 Effect . mapError (
26972705 toPersistenceSqlOrDecodeError (
26982706 "ProjectionSnapshotQuery.getShellSnapshot:listThreadSessions:query" ,
26992707 "ProjectionSnapshotQuery.getShellSnapshot:listThreadSessions:decodeRows" ,
27002708 ) ,
27012709 ) ,
27022710 ) ,
2703- listActiveThreadPullRequestRows ( { unsettledOnly } ) . pipe (
2711+ listActiveThreadPullRequestRows ( request ) . pipe (
27042712 Effect . mapError (
27052713 toPersistenceSqlOrDecodeError (
27062714 "ProjectionSnapshotQuery.getShellSnapshot:listThreadPullRequests:query" ,
27072715 "ProjectionSnapshotQuery.getShellSnapshot:listThreadPullRequests:decodeRows" ,
27082716 ) ,
27092717 ) ,
27102718 ) ,
2711- listActiveLatestTurnRows ( { unsettledOnly } ) . pipe (
2719+ listActiveLatestTurnRows ( request ) . pipe (
27122720 Effect . mapError (
27132721 toPersistenceSqlOrDecodeError (
27142722 "ProjectionSnapshotQuery.getShellSnapshot:listLatestTurns:query" ,
@@ -2862,6 +2870,7 @@ pending_approval_requests AS (
28622870 ) ,
28632871 ) ;
28642872
2873+ /** Read archived navigation rows separately from active-client and recovery snapshots. */
28652874 const getArchivedShellSnapshot : ProjectionSnapshotQueryShape [ "getArchivedShellSnapshot" ] = ( ) =>
28662875 sql
28672876 . withTransaction (
@@ -3270,6 +3279,7 @@ pending_approval_requests AS (
32703279 } ) ;
32713280 } ) ;
32723281
3282+ /** Read one thread's lifecycle and live-work state without loading its conversation history. */
32733283 const getThreadShellById : ProjectionSnapshotQueryShape [ "getThreadShellById" ] = ( threadId ) =>
32743284 Effect . gen ( function * ( ) {
32753285 const [ threadRow , latestTurnRow , sessionRow , pullRequestRows ] = yield * Effect . all ( [
0 commit comments