@@ -33,18 +33,15 @@ import {
3333 type EnvironmentThreadStatus ,
3434} from "./threadState.ts" ;
3535
36- // Coalesce one render-sized burst so web and mobile derive their large thread
37- // views once instead of once per streamed event. The size cap bounds catch-up
38- // memory and prevents a quiet connection from waiting indefinitely.
3936const THREAD_EVENT_BATCH_WINDOW = Duration . millis ( 16 ) ;
4037const THREAD_EVENT_BATCH_MAX_SIZE = 64 ;
4138
4239interface ThreadStreamBatchReduction {
4340 readonly state : EnvironmentThreadState ;
4441 readonly lastSequence : number ;
4542 readonly awaitingCompletion : boolean ;
46- readonly threadChanged : boolean ;
4743 readonly threadDeleted : boolean ;
44+ readonly persistableSnapshot : OrchestrationThreadDetailSnapshot | null ;
4845}
4946
5047export interface EnvironmentThreadStateOptions {
@@ -61,8 +58,8 @@ function reduceThreadStreamItems(
6158 let lastSequence = currentSequence ;
6259 let awaitingCompletion = currentAwaitingCompletion ;
6360 let thread = Option . getOrNull ( currentState . data ) ;
64- let threadChanged = false ;
6561 let threadDeleted = false ;
62+ let persistableSnapshot : OrchestrationThreadDetailSnapshot | null = null ;
6663
6764 for ( const item of items ) {
6865 if ( item . kind === "synchronized" ) {
@@ -80,8 +77,8 @@ function reduceThreadStreamItems(
8077 if ( item . kind === "snapshot" ) {
8178 lastSequence = item . snapshot . snapshotSequence ;
8279 thread = item . snapshot . thread ;
83- threadChanged = true ;
8480 threadDeleted = false ;
81+ persistableSnapshot = shouldPersistThread ( thread ) ? item . snapshot : null ;
8582 state = {
8683 data : Option . some ( thread ) ,
8784 status : awaitingCompletion ? "synchronizing" : "live" ,
@@ -99,6 +96,7 @@ function reduceThreadStreamItems(
9996 if ( item . event . type === "thread.deleted" ) {
10097 awaitingCompletion = false ;
10198 threadDeleted = true ;
99+ persistableSnapshot = null ;
102100 state = {
103101 data : Option . none ( ) ,
104102 status : "deleted" ,
@@ -111,7 +109,9 @@ function reduceThreadStreamItems(
111109 const result = applyThreadDetailEvent ( thread , item . event ) ;
112110 if ( result . kind === "updated" ) {
113111 thread = result . thread ;
114- threadChanged = true ;
112+ if ( shouldPersistThread ( thread ) ) {
113+ persistableSnapshot = { snapshotSequence : lastSequence , thread } ;
114+ }
115115 state = {
116116 data : Option . some ( thread ) ,
117117 status : awaitingCompletion ? "synchronizing" : "live" ,
@@ -121,6 +121,7 @@ function reduceThreadStreamItems(
121121 awaitingCompletion = false ;
122122 thread = null ;
123123 threadDeleted = true ;
124+ persistableSnapshot = null ;
124125 state = {
125126 data : Option . none ( ) ,
126127 status : "deleted" ,
@@ -133,8 +134,8 @@ function reduceThreadStreamItems(
133134 state,
134135 lastSequence,
135136 awaitingCompletion,
136- threadChanged,
137137 threadDeleted,
138+ persistableSnapshot,
138139 } ;
139140}
140141
@@ -281,15 +282,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
281282 return ;
282283 }
283284
284- const thread = Option . getOrNull ( reduction . state . data ) ;
285- // Active threads can update many times per second and retain large tool
286- // payloads. The server remains the source of truth while a turn is active;
287- // persist once it settles so cache encoding stays off the streaming path.
288- if ( reduction . threadChanged && thread !== null && shouldPersistThread ( thread ) ) {
289- yield * Queue . offer ( persistence , {
290- snapshotSequence : reduction . lastSequence ,
291- thread,
292- } ) ;
285+ if ( reduction . persistableSnapshot !== null ) {
286+ yield * Queue . offer ( persistence , reduction . persistableSnapshot ) ;
293287 }
294288 } ) ;
295289
0 commit comments