@@ -21,6 +21,8 @@ import {
2121 type OrchestrationV2ConversationMessage ,
2222 type OrchestrationV2CreationSource ,
2323 type OrchestrationV2Run ,
24+ type OrchestrationV2SubscribeThreadInput ,
25+ type OrchestrationV2ThreadStreamItem ,
2426 type OrchestrationV2ThreadShellSnapshot ,
2527 type OrchestrationV2ThreadProjection ,
2628 type OrchestrationV2ThreadTranscript ,
@@ -49,11 +51,30 @@ import * as Stream from "effect/Stream";
4951import * as CommandReceiptStore from "./CommandReceiptStore.ts" ;
5052import * as Orchestrator from "./Orchestrator.ts" ;
5153import * as ServerEnvironment from "../environment/ServerEnvironment.ts" ;
52- import { projectedRowEncodedBytes } from "./threadHistoryPaging.ts" ;
54+ import {
55+ projectedRowEncodedBytes ,
56+ THREAD_HISTORY_PAGE_POLICY ,
57+ THREAD_HISTORY_SNAPSHOT_ROW_LIMIT ,
58+ } from "./threadHistoryPaging.ts" ;
5359import * as ProviderSessionManager from "./ProviderSessionManager.ts" ;
5460import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts" ;
5561import { DispatchModeLimit , exceededDispatchModeLimit } from "./DispatchModeLimit.ts" ;
56- import { projectTurnItemForDetail } from "./WireProjection.ts" ;
62+ import {
63+ projectTurnItemForDetail ,
64+ projectDomainEventForWire ,
65+ projectThreadProjectionForWire ,
66+ } from "./WireProjection.ts" ;
67+ import { rpcInitialItems } from "../rpcInitialItems.ts" ;
68+ import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts" ;
69+ import { coalesceThreadLiveStream } from "./ThreadLiveEventCoalescer.ts" ;
70+ import {
71+ buildBoundedThreadStreamSnapshot ,
72+ decideThreadResume ,
73+ isThreadReplayRawPayloadSafe ,
74+ shouldUseBoundedThreadSnapshot ,
75+ threadReplayEncodedBytes ,
76+ THREAD_RESUME_MAX_REPLAY_EVENTS ,
77+ } from "./ThreadStream.ts" ;
5778import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts" ;
5879import * as ServerSettings from "../serverSettings.ts" ;
5980
@@ -310,7 +331,50 @@ export type ThreadManagementError = typeof ThreadManagementError.Type;
310331
311332type ThreadManagementFailure = ThreadManagementError | Orchestrator . OrchestratorV2Error ;
312333
334+ export class ThreadManagementThreadStreamError extends Schema . TaggedError < ThreadManagementThreadStreamError > ( ) (
335+ "ThreadManagementThreadStreamError" ,
336+ {
337+ threadId : ThreadId ,
338+ operation : Schema . Literals ( [
339+ "hydrate" ,
340+ "stream" ,
341+ "replay" ,
342+ "snapshot" ,
343+ "prepareReplay" ,
344+ "measureReplay" ,
345+ "locateRecreated" ,
346+ ] ) ,
347+ cause : Schema . Defect ( ) ,
348+ } ,
349+ ) {
350+ override get message ( ) : string {
351+ switch ( this . operation ) {
352+ case "hydrate" :
353+ return `Failed to hydrate migrated thread ${ this . threadId } ` ;
354+ case "stream" :
355+ return `Failed while streaming orchestration V2 thread ${ this . threadId } ` ;
356+ case "replay" :
357+ return `Failed while replaying orchestration V2 thread ${ this . threadId } ` ;
358+ case "snapshot" :
359+ return `Failed to load orchestration V2 thread ${ this . threadId } ` ;
360+ case "prepareReplay" :
361+ return `Failed to prepare orchestration V2 thread ${ this . threadId } replay` ;
362+ case "measureReplay" :
363+ return `Failed to measure orchestration V2 thread ${ this . threadId } replay` ;
364+ case "locateRecreated" :
365+ return `Failed to locate recreated orchestration V2 thread ${ this . threadId } ` ;
366+ }
367+ }
368+ }
369+
313370export interface ThreadManagementServiceShape {
371+ readonly streamThreadEventsForClient : (
372+ input : OrchestrationV2SubscribeThreadInput ,
373+ ) => Effect . Effect <
374+ Stream . Stream < OrchestrationV2ThreadStreamItem , ThreadManagementThreadStreamError > ,
375+ ThreadManagementThreadStreamError ,
376+ OrchestrationEventStore . OrchestrationEventStore
377+ > ;
314378 readonly searchThreadStream : (
315379 input : OrchestrationV2SearchThreadInput ,
316380 ) => Stream . Stream < OrchestrationV2SearchThreadResult , Orchestrator . OrchestratorV2Error > ;
@@ -642,6 +706,232 @@ const make = Effect.gen(function* () {
642706 Effect . andThen ( orchestrator . getThreadSnapshotWindow ( threadId , options ) ) ,
643707 ) ;
644708
709+ const streamThreadEventsForClient = Effect . fn (
710+ "orchestrationV2.threadManagement.streamThreadEventsForClient" ,
711+ ) ( function * ( input : OrchestrationV2SubscribeThreadInput ) {
712+ const applicationEvents = yield * OrchestrationEventStore . OrchestrationEventStore ;
713+
714+ yield * ensureLegacyTranscript ( input . threadId ) . pipe (
715+ Effect . mapError (
716+ ( cause ) =>
717+ new ThreadManagementThreadStreamError ( {
718+ threadId : input . threadId ,
719+ operation : "hydrate" ,
720+ cause,
721+ } ) ,
722+ ) ,
723+ ) ;
724+
725+ const eventStreamFrom = ( afterSequence : number ) =>
726+ orchestrator
727+ . streamStoredEventsFrom ( {
728+ threadId : input . threadId ,
729+ afterSequence,
730+ } )
731+ . pipe (
732+ Stream . mapEffect ( ( stored ) =>
733+ Effect . gen ( function * ( ) {
734+ const event = stored . event ;
735+ if (
736+ input . acceptThreadFieldEvents !== true &&
737+ ( event . type === "thread.pull-request-link-synced" ||
738+ event . type === "thread.visit-recorded" )
739+ ) {
740+ // Older clients need the legacy full-thread event. The live stream reads
741+ // committed metadata only; historical compact events use a fresh snapshot below.
742+ const { thread } = yield * ensureProjectionTranscript ( input . threadId ) . pipe (
743+ Effect . andThen ( orchestrator . getThreadRecords ( input . threadId , [ ] ) ) ,
744+ ) ;
745+ return {
746+ kind : "event" as const ,
747+ sequence : stored . sequence ,
748+ event : projectDomainEventForWire ( {
749+ ...event ,
750+ type :
751+ event . type === "thread.visit-recorded"
752+ ? "thread.visited"
753+ : "thread.pull-request-synced" ,
754+ payload : thread ,
755+ } ) ,
756+ } ;
757+ }
758+ return {
759+ kind : "event" as const ,
760+ sequence : stored . sequence ,
761+ event : projectDomainEventForWire ( event ) ,
762+ } ;
763+ } ) ,
764+ ) ,
765+ coalesceThreadLiveStream ,
766+ Stream . mapError (
767+ ( cause ) =>
768+ new ThreadManagementThreadStreamError ( {
769+ threadId : input . threadId ,
770+ operation : "stream" ,
771+ cause,
772+ } ) ,
773+ ) ,
774+ ) ;
775+
776+ const loadReplayThrough = ( afterSequence : number , throughSequence : number ) =>
777+ applicationEvents
778+ . readAgentEvents ( {
779+ threadId : input . threadId ,
780+ afterSequence,
781+ throughSequence,
782+ limit : THREAD_RESUME_MAX_REPLAY_EVENTS + 1 ,
783+ // A row a newer build wrote (e.g. after a downgrade) must not fail
784+ // every resubscribe. The live tail resumes from `highWater`, so a
785+ // dropped row never hides later events.
786+ skipUnknownEventTypes : true ,
787+ } )
788+ . pipe (
789+ Stream . map ( ( stored ) => ( {
790+ kind : "event" as const ,
791+ sequence : stored . sequence ,
792+ event : projectDomainEventForWire ( stored . event ) ,
793+ } ) ) ,
794+ Stream . runCollect ,
795+ Effect . map ( ( items ) => Array . from ( items ) ) ,
796+ Effect . mapError (
797+ ( cause ) =>
798+ new ThreadManagementThreadStreamError ( {
799+ threadId : input . threadId ,
800+ operation : "replay" ,
801+ cause,
802+ } ) ,
803+ ) ,
804+ ) ;
805+
806+ const completionMarker =
807+ input . requestCompletionMarker === true
808+ ? Stream . make ( { kind : "synchronized" as const } )
809+ : Stream . empty ;
810+
811+ const snapshotThenLive = Effect . fn ( "orchestrationV2.threadManagement.threadSnapshotThenLive" ) (
812+ function * ( ) {
813+ const useBoundedSnapshot = shouldUseBoundedThreadSnapshot ( input ) ;
814+ const snapshot = yield * (
815+ useBoundedSnapshot
816+ ? getThreadSnapshotWindow ( input . threadId , {
817+ rowLimit : THREAD_HISTORY_SNAPSHOT_ROW_LIMIT ,
818+ userTurnLimit : THREAD_HISTORY_PAGE_POLICY . maxUserTurns ,
819+ } )
820+ : getThreadSnapshot ( input . threadId )
821+ ) . pipe (
822+ Effect . mapError (
823+ ( cause ) =>
824+ new ThreadManagementThreadStreamError ( {
825+ threadId : input . threadId ,
826+ operation : "snapshot" ,
827+ cause,
828+ } ) ,
829+ ) ,
830+ ) ;
831+ const { snapshotSequence } = snapshot ;
832+ const snapshotItem = useBoundedSnapshot
833+ ? buildBoundedThreadStreamSnapshot ( {
834+ ...snapshot ,
835+ compactTurnItems : input . acceptCompactTurnItems === true ,
836+ } )
837+ : {
838+ kind : "snapshot" as const ,
839+ snapshotSequence,
840+ projection : projectThreadProjectionForWire ( snapshot . projection ) ,
841+ } ;
842+ return Stream . concat (
843+ Stream . concat ( rpcInitialItems ( [ snapshotItem ] ) , completionMarker ) ,
844+ eventStreamFrom ( snapshotSequence ) ,
845+ ) ;
846+ } ,
847+ ) ;
848+
849+ // The event sink subscribes to live events before reading the persisted
850+ // tail, so no event published during the replay window is lost; overlapping
851+ // events are deduped by sequence on the client.
852+ if ( input . afterSequence !== undefined ) {
853+ const highWater = yield * applicationEvents . latestAgentSequence ( input . threadId ) . pipe (
854+ Effect . mapError (
855+ ( cause ) =>
856+ new ThreadManagementThreadStreamError ( {
857+ threadId : input . threadId ,
858+ operation : "prepareReplay" ,
859+ cause,
860+ } ) ,
861+ ) ,
862+ ) ;
863+ if ( input . afterSequence > highWater ) {
864+ return yield * snapshotThenLive ( ) ;
865+ }
866+ const stats = yield * applicationEvents
867+ . getAgentReplayStats ( {
868+ threadId : input . threadId ,
869+ afterSequence : input . afterSequence ,
870+ throughSequence : highWater ,
871+ maxEvents : THREAD_RESUME_MAX_REPLAY_EVENTS ,
872+ } )
873+ . pipe (
874+ Effect . mapError (
875+ ( cause ) =>
876+ new ThreadManagementThreadStreamError ( {
877+ threadId : input . threadId ,
878+ operation : "measureReplay" ,
879+ cause,
880+ } ) ,
881+ ) ,
882+ ) ;
883+ // Bound stored JSON before decoding, then check projected event
884+ // size separately. Neither byte count is a bound on process memory.
885+ if (
886+ stats . eventCount > THREAD_RESUME_MAX_REPLAY_EVENTS ||
887+ ! isThreadReplayRawPayloadSafe ( stats . rawPayloadBytes )
888+ ) {
889+ return yield * snapshotThenLive ( ) ;
890+ }
891+ if ( stats . hasCreateEvent ) {
892+ const shell = yield * orchestrator . getThreadShell ( input . threadId ) . pipe (
893+ Effect . mapError (
894+ ( cause ) =>
895+ new ThreadManagementThreadStreamError ( {
896+ threadId : input . threadId ,
897+ operation : "locateRecreated" ,
898+ cause,
899+ } ) ,
900+ ) ,
901+ ) ;
902+ // A retained creation can belong to a thread already deleted.
903+ // Only replace its bounded replay when a snapshot can exist.
904+ if ( shell !== null ) return yield * snapshotThenLive ( ) ;
905+ }
906+ const replay = yield * loadReplayThrough ( input . afterSequence , highWater ) ;
907+ if (
908+ input . acceptThreadFieldEvents !== true &&
909+ replay . some (
910+ ( item ) =>
911+ item . event . type === "thread.pull-request-link-synced" ||
912+ item . event . type === "thread.visit-recorded" ,
913+ )
914+ ) {
915+ return yield * snapshotThenLive ( ) ;
916+ }
917+ const plan = decideThreadResume ( {
918+ afterSequence : input . afterSequence ,
919+ highWater,
920+ replayEventCount : replay . length ,
921+ replayEncodedBytes : threadReplayEncodedBytes ( replay ) ,
922+ } ) ;
923+ if ( plan . mode === "snapshot" ) {
924+ return yield * snapshotThenLive ( ) ;
925+ }
926+ return Stream . concat (
927+ Stream . concat ( rpcInitialItems ( replay ) , completionMarker ) ,
928+ eventStreamFrom ( highWater ) ,
929+ ) ;
930+ }
931+
932+ return yield * snapshotThenLive ( ) ;
933+ } ) ;
934+
645935 const assertDispatchTargetEnabled = ( command : OrchestrationV2ServerCommand ) => {
646936 switch ( command . type ) {
647937 case "message.dispatch" :
@@ -1119,6 +1409,7 @@ const make = Effect.gen(function* () {
11191409 } ) ;
11201410
11211411 return ThreadManagementService . of ( {
1412+ streamThreadEventsForClient,
11221413 ensureLegacyTranscript,
11231414 searchThreadStream : ( input ) =>
11241415 Stream . unwrap (
0 commit comments