@@ -189,6 +189,25 @@ export type ProjectionThreadPullRequests = Pick<
189189 "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pullRequests"
190190> ;
191191
192+ export interface ProjectionPullRequestSyncSnapshot {
193+ readonly snapshotSequence : number ;
194+ readonly threads : ReadonlyArray <
195+ Pick <
196+ OrchestrationV2ThreadShell ,
197+ | "id"
198+ | "projectId"
199+ | "branch"
200+ | "worktreePath"
201+ | "pullRequests"
202+ | "linkedPullRequest"
203+ | "branchPullRequest"
204+ | "archivedAt"
205+ | "settledOverride"
206+ | "settledAt"
207+ >
208+ > ;
209+ }
210+
192211export type ProjectionSettlementCandidate = Pick <
193212 OrchestrationV2ThreadShell ,
194213 | "id"
@@ -512,6 +531,12 @@ export interface ProjectionStoreV2Shape {
512531 readonly getThreadsWithPullRequests : (
513532 threadId ?: ThreadId ,
514533 ) => Effect . Effect < ReadonlyArray < ProjectionThreadPullRequests > , ProjectionStoreV2Error > ;
534+ /** The shell snapshot's threads and sequence without the run, item and message reads. */
535+ readonly getPullRequestSyncSnapshot : (
536+ options : Pick < ShellSnapshotOptions , "unsettledOnly" | "threadIds" > & {
537+ readonly location ?: "active" ;
538+ } ,
539+ ) => Effect . Effect < ProjectionPullRequestSyncSnapshot , ProjectionStoreV2Error > ;
515540 readonly getThreadWorktreePaths : ( options ?: {
516541 readonly includeArchived ?: boolean ;
517542 } ) => Effect . Effect <
@@ -610,6 +635,10 @@ export interface ProjectionStoreV2Shape {
610635 threadId : ThreadId ,
611636 runId : RunId ,
612637 ) => Effect . Effect < OrchestrationV2ConversationMessage | undefined , ProjectionStoreV2Error > ;
638+ readonly getRun : (
639+ threadId : ThreadId ,
640+ runId : RunId ,
641+ ) => Effect . Effect < OrchestrationV2Run | undefined , ProjectionStoreV2Error > ;
613642 readonly canStartQueuedRun : (
614643 threadId : ThreadId ,
615644 ) => Effect . Effect < boolean , ProjectionStoreV2Error > ;
@@ -5265,6 +5294,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52655294 )
52665295 . pipe ( Effect . mapError ( controlReadError ( threadId ) ) ) ;
52675296
5297+ const getRun : ProjectionStoreV2Shape [ "getRun" ] = ( threadId , runId ) =>
5298+ sql
5299+ . withTransaction (
5300+ Effect . gen ( function * ( ) {
5301+ yield * requireThread ( threadId ) ;
5302+ const rows = yield * sql < PayloadRow > `
5303+ SELECT payload_json FROM orchestration_v2_projection_runs
5304+ WHERE run_id = ${ runId } AND thread_id = ${ threadId } ` ;
5305+ return rows [ 0 ] === undefined
5306+ ? undefined
5307+ : yield * decodeRunPayload ( rows [ 0 ] . payload_json ) ;
5308+ } ) ,
5309+ )
5310+ . pipe ( Effect . mapError ( controlReadError ( threadId ) ) ) ;
5311+
52685312 const canStartQueuedRun : ProjectionStoreV2Shape [ "canStartQueuedRun" ] = ( threadId ) =>
52695313 sql
52705314 . withTransaction (
@@ -6474,6 +6518,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64746518 const shellSnapshotReadError = ( cause : unknown ) =>
64756519 new ProjectionStoreReadError ( { threadId : ThreadId . make ( "thread:shell" ) , cause } ) ;
64766520
6521+ const selectSnapshotSequence = sql < { readonly snapshot_sequence : number | null } > `
6522+ SELECT MAX(sequence) AS snapshot_sequence
6523+ FROM orchestration_events
6524+ WHERE application_event_version = 2
6525+ AND aggregate_kind = 'thread'
6526+ ` ;
6527+
64776528 const readShellSnapshotRows = Effect . fn ( "ProjectionStore.readShellSnapshotRows" ) ( function * (
64786529 options : ShellSnapshotOptions | undefined ,
64796530 ) {
@@ -6515,12 +6566,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
65156566 readForThreadIds ( selectShellRunRows , forkSourceIds ) ,
65166567 readForThreadIds ( selectShellRunItemCounts , forkSourceIds ) ,
65176568 readForThreadIds ( selectShellItemCounts ) ,
6518- sql < { readonly snapshot_sequence : number | null } > `
6519- SELECT MAX(sequence) AS snapshot_sequence
6520- FROM orchestration_events
6521- WHERE application_event_version = 2
6522- AND aggregate_kind = 'thread'
6523- ` ,
6569+ selectSnapshotSequence ,
65246570 readForThreadIds ( selectShellProviderThreadRows ) ,
65256571 readForThreadIds ( ( ids ) =>
65266572 selectShellPendingTurnItemRows (
@@ -6605,6 +6651,50 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66056651 . withTransaction ( readShellSnapshotRows ( options ) )
66066652 . pipe ( Effect . mapError ( shellSnapshotReadError ) , Effect . flatMap ( decodeShellSnapshot ) ) ;
66076653
6654+ const getPullRequestSyncSnapshot : ProjectionStoreV2Shape [ "getPullRequestSyncSnapshot" ] =
6655+ Effect . fn ( "ProjectionStore.getPullRequestSyncSnapshot" ) ( function * ( {
6656+ location,
6657+ unsettledOnly = false ,
6658+ threadIds,
6659+ } ) {
6660+ const [ rows , sequenceRows ] = yield * sql . withTransaction (
6661+ Effect . all ( [
6662+ sql < PayloadRow > `
6663+ SELECT payload_json
6664+ FROM orchestration_v2_projection_threads
6665+ WHERE deleted_at IS NULL${ location === "active" ? sql ` AND archived_at IS NULL` : sql `` } ${
6666+ unsettledOnly
6667+ ? sql ` AND json_extract(payload_json, '$.settledAt') IS NULL AND json_extract(payload_json, '$.settledOverride') IS NOT 'settled'`
6668+ : sql ``
6669+ } ${ threadIds === undefined ? sql `` : sql ` AND thread_id IN ${ sql . in ( threadIds ) } ` }
6670+ ORDER BY updated_at ASC, thread_id ASC
6671+ ` ,
6672+ selectSnapshotSequence ,
6673+ ] ) ,
6674+ ) ;
6675+ const threads = yield * Effect . forEach ( rows , ( row ) =>
6676+ decodeThreadPayload ( row . payload_json ) . pipe (
6677+ Effect . map ( ( thread ) => ( {
6678+ id : thread . id ,
6679+ projectId : thread . projectId ,
6680+ branch : thread . branch ,
6681+ worktreePath : thread . worktreePath ,
6682+ pullRequests : threadPullRequestsOf ( thread ) ,
6683+ ...( thread . linkedPullRequest === undefined
6684+ ? { }
6685+ : { linkedPullRequest : thread . linkedPullRequest } ) ,
6686+ ...( thread . branchPullRequest === undefined
6687+ ? { }
6688+ : { branchPullRequest : thread . branchPullRequest } ) ,
6689+ archivedAt : thread . archivedAt ,
6690+ settledOverride : thread . settledOverride ,
6691+ settledAt : thread . settledAt ,
6692+ } ) ) ,
6693+ ) ,
6694+ ) ;
6695+ return { snapshotSequence : sequenceRows [ 0 ] ?. snapshot_sequence ?? 0 , threads } ;
6696+ } , Effect . mapError ( shellSnapshotReadError ) ) ;
6697+
66086698 const listActiveSubagentThreadIds = Effect . fn ( "ProjectionStore.listActiveSubagentThreadIds" ) (
66096699 function * ( ) {
66106700 const rows = yield * sql < { readonly thread_id : string } > `
@@ -6780,6 +6870,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67806870 getThread : readOnReader ( getThread ) ,
67816871 getSettlementCandidates : readOnReader ( getSettlementCandidates ) ,
67826872 getThreadsWithPullRequests : readOnReader ( getThreadsWithPullRequests ) ,
6873+ getPullRequestSyncSnapshot : readOnReader ( getPullRequestSyncSnapshot ) ,
67836874 getThreadWorktreePaths : readOnReader ( getThreadWorktreePaths ) ,
67846875 getThreadProjection,
67856876 getTurnStartContext : readOnReader ( getTurnStartContext ) ,
@@ -6792,6 +6883,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67926883 getCheckpointCaptureContext : readOnReader ( getCheckpointCaptureContext ) ,
67936884 getCheckpointRollbackContext : readOnReader ( getCheckpointRollbackContext ) ,
67946885 getRunMessage : readOnReader ( getRunMessage ) ,
6886+ getRun : readOnReader ( getRun ) ,
67956887 canStartQueuedRun : readOnReader ( canStartQueuedRun ) ,
67966888 getPendingNativeUserInputs : readOnReader ( getPendingNativeUserInputs ) ,
67976889 hasUnpairedRunInterruptRequest : readOnReader ( hasUnpairedRunInterruptRequest ) ,
@@ -7060,6 +7152,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
70607152 } ) ) ,
70617153 ) ,
70627154 ) ,
7155+ getPullRequestSyncSnapshot : ( options ) =>
7156+ service . getShellSnapshot ( options ) . pipe (
7157+ Effect . map ( ( snapshot ) => ( {
7158+ snapshotSequence : snapshot . snapshotSequence ,
7159+ threads : [ ...snapshot . threads , ...snapshot . archivedThreads ] ,
7160+ } ) ) ,
7161+ ) ,
70637162 getLimitRecoveryCandidates : ( options ) =>
70647163 Ref . get ( replayState ) . pipe (
70657164 Effect . map ( ( state ) =>
@@ -7500,6 +7599,12 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
75007599 const run = projection . runs . find ( ( candidate ) => candidate . id === runId ) ;
75017600 return projection . messages . find ( ( message ) => message . id === run ?. userMessageId ) ;
75027601 } ) ,
7602+ getRun : Effect . fn ( "ProjectionStore.getRun" ) ( function * ( threadId , runId ) {
7603+ const projection = ( yield * Ref . get ( replayState ) ) . projections . get ( threadId ) ;
7604+ if ( projection === undefined )
7605+ return yield * new ProjectionStoreThreadNotFoundError ( { threadId } ) ;
7606+ return projection . runs . find ( ( candidate ) => candidate . id === runId ) ;
7607+ } ) ,
75037608 canStartQueuedRun : Effect . fn ( "ProjectionStore.canStartQueuedRun" ) ( function * ( threadId ) {
75047609 const projection = ( yield * Ref . get ( replayState ) ) . projections . get ( threadId ) ;
75057610 if ( projection === undefined )
0 commit comments