@@ -421,6 +421,7 @@ export interface ProjectionStoreV2Shape {
421421 ) => Effect . Effect < ReadonlyArray < ProjectionSettlementCandidate > , ProjectionStoreV2Error > ;
422422 readonly getThreadsWithPullRequests : (
423423 threadId ?: ThreadId ,
424+ options ?: { readonly watchedOnly ?: boolean } ,
424425 ) => Effect . Effect < ReadonlyArray < ProjectionThreadPullRequests > , ProjectionStoreV2Error > ;
425426 readonly getThreadWorktreePaths : ( ) => Effect . Effect <
426427 ReadonlyArray < { readonly threadId : ThreadId ; readonly worktreePath : string } > ,
@@ -1207,6 +1208,20 @@ const decodeRows =
12071208 ) ,
12081209 ) ;
12091210
1211+ // Full thread schemas can exhaust the fiber budget slowly; yield between small
1212+ // batches so background sweeps let other event-loop work run during bulk decoding.
1213+ const decodeBackgroundRows = Effect . fnUntraced ( function * < A , B , E , R > (
1214+ rows : ReadonlyArray < A > ,
1215+ decode : ( row : A ) => Effect . Effect < B , E , R > ,
1216+ ) {
1217+ const values : Array < B > = [ ] ;
1218+ for ( let index = 0 ; index < rows . length ; index ++ ) {
1219+ if ( index > 0 && index % 64 === 0 ) yield * Effect . yieldNow ;
1220+ values . push ( yield * decode ( rows [ index ] ! ) ) ;
1221+ }
1222+ return values ;
1223+ } ) ;
1224+
12101225function messageIdForTurnItem ( item : OrchestrationV2TurnItem ) : string | null {
12111226 switch ( item . type ) {
12121227 case "user_message" :
@@ -5650,71 +5665,83 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56505665 )
56515666 ORDER BY t.updated_at ASC, t.thread_id ASC
56525667 ` ;
5653- if ( rows . length === 0 ) return [ ] ;
5668+ if ( rows . length === 0 ) {
5669+ return { rows, providerThreadRows : [ ] , pendingTurnItemRows : [ ] } ;
5670+ }
56545671 const threadIds = rows . map ( ( row ) => ThreadId . make ( row . thread_id ) ) ;
5655- const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
5656- yield * pendingBackgroundDataByThreadId ( {
5657- providerThreadRows : yield * selectShellProviderThreadRows ( threadIds ) ,
5658- pendingTurnItemRows : yield * selectShellPendingTurnItemRows ( threadIds ) ,
5659- } ) ;
5660- return yield * Effect . forEach ( rows , ( row ) =>
5661- Effect . gen ( function * ( ) {
5662- const thread = yield * decodeThreadPayload ( row . payload_json ) ;
5663- const status = shellStatusFromStoredRunStatus ( row . latest_run_status ) ;
5664- const latestRunId =
5665- row . latest_run_id === null ? null : RunId . make ( row . latest_run_id ) ;
5666- return {
5667- ...thread ,
5668- pinnedAt : thread . pinnedAt ?? null ,
5669-
5670- autoSettleDisabledAt : thread . autoSettleDisabledAt ?? null ,
5671- snoozedUntil : thread . snoozedUntil ?? null ,
5672- snoozedAt : thread . snoozedAt ?? null ,
5673- status,
5674- latestRunId,
5675- latestRunRequestedAt :
5676- row . latest_run_requested_at === null
5677- ? null
5678- : DateTime . makeUnsafe ( row . latest_run_requested_at ) ,
5679- latestRunStartedAt :
5680- row . latest_run_started_at === null
5681- ? null
5682- : DateTime . makeUnsafe ( row . latest_run_started_at ) ,
5683- latestRunCompletedAt :
5684- row . latest_run_completed_at === null
5685- ? null
5686- : DateTime . makeUnsafe ( row . latest_run_completed_at ) ,
5687- latestUserMessageAt :
5688- row . latest_user_message_at === null
5689- ? null
5690- : DateTime . makeUnsafe ( row . latest_user_message_at ) ,
5691- latestUserAuthoredMessageAt :
5692- row . latest_user_authored_message_at === null
5693- ? null
5694- : DateTime . makeUnsafe ( row . latest_user_authored_message_at ) ,
5695- activityRunStatus : null ,
5696- activityRunStartedAt : null ,
5697- pendingRuntimeRequest : null ,
5698- pendingBackgroundTasks : derivePendingBackgroundWork ( {
5699- latestRun :
5700- latestRunId === null || status === "idle"
5701- ? null
5702- : { id : latestRunId , ordinal : 0 , status } ,
5703- providerThreads : providerThreadsByThreadId . get ( thread . id ) ?? [ ] ,
5704- turnItems : pendingTurnItemsByThreadId . get ( thread . id ) ?? [ ] ,
5705- activeProviderThreadId : thread . activeProviderThreadId ,
5706- hasActiveRun : false ,
5707- pullRequests : thread . pullRequests ,
5708- } ) ,
5709- } satisfies ProjectionSettlementCandidate ;
5710- } ) ,
5711- ) ;
5672+ return {
5673+ rows,
5674+ providerThreadRows : yield * selectShellProviderThreadRows ( threadIds ) ,
5675+ pendingTurnItemRows : yield * selectShellPendingTurnItemRows ( threadIds ) ,
5676+ } ;
57125677 } ) ,
57135678 )
5714- . pipe ( Effect . mapError ( ( cause ) => new ProjectionStoreSetupError ( { cause } ) ) ) ;
5679+ . pipe (
5680+ // Release the shared connection before schema decoding and background derivation.
5681+ Effect . flatMap ( ( { rows, providerThreadRows, pendingTurnItemRows } ) =>
5682+ Effect . gen ( function * ( ) {
5683+ const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
5684+ yield * pendingBackgroundDataByThreadId ( { providerThreadRows, pendingTurnItemRows } ) ;
5685+ return yield * decodeBackgroundRows ( rows , ( row ) =>
5686+ Effect . gen ( function * ( ) {
5687+ const thread = yield * decodeThreadPayload ( row . payload_json ) ;
5688+ const status = shellStatusFromStoredRunStatus ( row . latest_run_status ) ;
5689+ const latestRunId =
5690+ row . latest_run_id === null ? null : RunId . make ( row . latest_run_id ) ;
5691+ return {
5692+ ...thread ,
5693+ pinnedAt : thread . pinnedAt ?? null ,
5694+
5695+ autoSettleDisabledAt : thread . autoSettleDisabledAt ?? null ,
5696+ snoozedUntil : thread . snoozedUntil ?? null ,
5697+ snoozedAt : thread . snoozedAt ?? null ,
5698+ status,
5699+ latestRunId,
5700+ latestRunRequestedAt :
5701+ row . latest_run_requested_at === null
5702+ ? null
5703+ : DateTime . makeUnsafe ( row . latest_run_requested_at ) ,
5704+ latestRunStartedAt :
5705+ row . latest_run_started_at === null
5706+ ? null
5707+ : DateTime . makeUnsafe ( row . latest_run_started_at ) ,
5708+ latestRunCompletedAt :
5709+ row . latest_run_completed_at === null
5710+ ? null
5711+ : DateTime . makeUnsafe ( row . latest_run_completed_at ) ,
5712+ latestUserMessageAt :
5713+ row . latest_user_message_at === null
5714+ ? null
5715+ : DateTime . makeUnsafe ( row . latest_user_message_at ) ,
5716+ latestUserAuthoredMessageAt :
5717+ row . latest_user_authored_message_at === null
5718+ ? null
5719+ : DateTime . makeUnsafe ( row . latest_user_authored_message_at ) ,
5720+ activityRunStatus : null ,
5721+ activityRunStartedAt : null ,
5722+ pendingRuntimeRequest : null ,
5723+ pendingBackgroundTasks : derivePendingBackgroundWork ( {
5724+ latestRun :
5725+ latestRunId === null || status === "idle"
5726+ ? null
5727+ : { id : latestRunId , ordinal : 0 , status } ,
5728+ providerThreads : providerThreadsByThreadId . get ( thread . id ) ?? [ ] ,
5729+ turnItems : pendingTurnItemsByThreadId . get ( thread . id ) ?? [ ] ,
5730+ activeProviderThreadId : thread . activeProviderThreadId ,
5731+ hasActiveRun : false ,
5732+ pullRequests : thread . pullRequests ,
5733+ } ) ,
5734+ } satisfies ProjectionSettlementCandidate ;
5735+ } ) ,
5736+ ) ;
5737+ } ) ,
5738+ ) ,
5739+ Effect . mapError ( ( cause ) => new ProjectionStoreSetupError ( { cause } ) ) ,
5740+ ) ;
57155741
57165742 const getThreadsWithPullRequests : ProjectionStoreV2Shape [ "getThreadsWithPullRequests" ] = (
57175743 threadId ,
5744+ options ,
57185745 ) =>
57195746 Effect . gen ( function * ( ) {
57205747 const rows = yield * sql < PayloadRow > `
@@ -5723,9 +5750,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57235750 WHERE deleted_at IS NULL${ threadId === undefined ? sql `` : sql ` AND thread_id = ${ threadId } ` }
57245751 AND json_extract(payload_json, '$.archivedAt') IS NULL
57255752 AND json_array_length(payload_json, '$.pullRequests') > 0
5753+ ${
5754+ options ?. watchedOnly
5755+ ? sql `AND EXISTS (
5756+ SELECT 1 FROM json_each(payload_json, '$.pullRequests') AS link
5757+ WHERE json_type(link.value, '$.watch') IS NOT NULL
5758+ )`
5759+ : sql ``
5760+ }
57265761 ORDER BY updated_at ASC, thread_id ASC
57275762 ` ;
5728- return yield * Effect . forEach ( rows , ( row ) =>
5763+ return yield * decodeBackgroundRows ( rows , ( row ) =>
57295764 decodeThreadPayload ( row . payload_json ) . pipe (
57305765 Effect . map ( ( thread ) : ProjectionThreadPullRequests => ( {
57315766 id : thread . id ,
@@ -6252,7 +6287,7 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
62526287 ) ,
62536288 ) ,
62546289 ) ,
6255- getThreadsWithPullRequests : ( threadId ) =>
6290+ getThreadsWithPullRequests : ( threadId , options ) =>
62566291 Ref . get ( replayState ) . pipe (
62576292 Effect . map ( ( state ) =>
62586293 [ ...state . projections . values ( ) ]
@@ -6262,7 +6297,9 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
62626297 ( threadId === undefined || thread . id === threadId ) &&
62636298 thread . deletedAt === null &&
62646299 thread . archivedAt === null &&
6265- ( thread . pullRequests ?? [ ] ) . length > 0 ,
6300+ ( thread . pullRequests ?? [ ] ) . length > 0 &&
6301+ ( ! options ?. watchedOnly ||
6302+ thread . pullRequests ?. some ( ( link ) => link . watch !== undefined ) ) ,
62666303 )
62676304 . toSorted (
62686305 ( left , right ) =>
0 commit comments