@@ -639,35 +639,44 @@ const make = Effect.gen(function* () {
639639 }
640640
641641 // Re-read the run only when the thread records a run update or its
642- // deletion, instead of polling the projection for up to an hour.
643- const wait = orchestrator
644- . streamStoredEventsFrom ( { threadId : input . threadId , afterSequence } )
645- . pipe (
646- Stream . mapError ( loadError ) ,
647- Stream . filter (
648- ( stored ) =>
649- ( stored . event . type === "run.updated" && stored . event . payload . id === selectedRun . id ) ||
650- stored . event . type === "thread.deleted" ,
651- ) ,
652- Stream . mapEffect ( ( ) =>
653- getProjectThreadRecords ( input , [ "runs" ] , { runIds : [ selectedRun . id ] } ) . pipe (
654- Effect . flatMap ( ( current ) => {
655- const run = current . runs . find ( ( candidate ) => candidate . id === selectedRun . id ) ;
656- return run === undefined
657- ? Effect . fail (
658- new ThreadManagementRunNotFoundError ( {
659- threadId : input . threadId ,
660- runId : selectedRun . id ,
661- } ) ,
662- )
663- : Effect . succeed ( run ) ;
664- } ) ,
665- ) ,
642+ // deletion, instead of polling the projection for up to an hour. Each
643+ // stream keeps one event type, so transcript events never fill its buffer.
644+ const wait = Stream . merge (
645+ orchestrator . streamStoredEventsFrom ( {
646+ threadId : input . threadId ,
647+ afterSequence,
648+ eventType : "run.updated" ,
649+ } ) ,
650+ orchestrator . streamStoredEventsFrom ( {
651+ threadId : input . threadId ,
652+ afterSequence,
653+ eventType : "thread.deleted" ,
654+ } ) ,
655+ ) . pipe (
656+ Stream . mapError ( loadError ) ,
657+ Stream . filter (
658+ ( stored ) =>
659+ stored . event . type !== "run.updated" || stored . event . payload . id === selectedRun . id ,
660+ ) ,
661+ Stream . mapEffect ( ( ) =>
662+ getProjectThreadRecords ( input , [ "runs" ] , { runIds : [ selectedRun . id ] } ) . pipe (
663+ Effect . flatMap ( ( current ) => {
664+ const run = current . runs . find ( ( candidate ) => candidate . id === selectedRun . id ) ;
665+ return run === undefined
666+ ? Effect . fail (
667+ new ThreadManagementRunNotFoundError ( {
668+ threadId : input . threadId ,
669+ runId : selectedRun . id ,
670+ } ) ,
671+ )
672+ : Effect . succeed ( run ) ;
673+ } ) ,
666674 ) ,
667- Stream . filter ( ( run ) => isTerminalRunStatus ( run . status ) ) ,
668- Stream . runHead ,
669- Effect . timeoutOption ( Duration . millis ( Math . max ( 1 , input . timeoutMs ) ) ) ,
670- ) ;
675+ ) ,
676+ Stream . filter ( ( run ) => isTerminalRunStatus ( run . status ) ) ,
677+ Stream . runHead ,
678+ Effect . timeoutOption ( Duration . millis ( Math . max ( 1 , input . timeoutMs ) ) ) ,
679+ ) ;
671680 const waited = Option . flatten ( yield * wait ) ;
672681 if ( Option . isSome ( waited ) ) {
673682 return { threadId : input . threadId , run : waited . value , timedOut : false } ;
0 commit comments