11import {
22 EnvironmentId ,
3+ EventId ,
34 MessageId ,
45 RunId ,
56 TurnItemId ,
@@ -12,7 +13,9 @@ import * as Option from "effect/Option";
1213import { AsyncResult , Atom , AtomRegistry } from "effect/reactivity" ;
1314
1415import { v2Now , v2Projection , v2ThreadId } from "./orchestrationV2TestFixtures.ts" ;
16+ import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts" ;
1517import { createEnvironmentThreadDetailAtoms , sameThreadStructure } from "./threadDetail.ts" ;
18+ import { mergeOlderHistoryIntoProjection } from "./threadHistoryMerge.ts" ;
1619import { EMPTY_ENVIRONMENT_THREAD_STATE } from "./threadState.ts" ;
1720
1821const later = DateTime . makeUnsafe ( "2026-06-20T00:00:01.000Z" ) ;
@@ -132,10 +135,14 @@ describe("quietThreadAtom", () => {
132135 registry . mount ( quietAtom ) ;
133136 return {
134137 read : ( ) => registry . get ( quietAtom ) ,
135- push : ( projection : OrchestrationV2ThreadProjection ) =>
138+ [ Symbol . dispose ] : ( ) => registry . dispose ( ) ,
139+ push : ( projection : OrchestrationV2ThreadProjection | null ) =>
136140 registry . set (
137141 stateAtom ,
138- AsyncResult . success ( { ...EMPTY_ENVIRONMENT_THREAD_STATE , data : Option . some ( projection ) } ) ,
142+ AsyncResult . success ( {
143+ ...EMPTY_ENVIRONMENT_THREAD_STATE ,
144+ data : Option . fromNullishOr ( projection ) ,
145+ } ) ,
139146 ) ,
140147 } ;
141148 }
@@ -158,6 +165,22 @@ describe("quietThreadAtom", () => {
158165 expect ( next ?. projection . turnItems [ 0 ] ) . toMatchObject ( { status : "completed" , streaming : false } ) ;
159166 } ) ;
160167
168+ it ( "discards structure held before the thread resets" , ( ) => {
169+ const initial = withItem ( assistant ) ;
170+ using thread = setup ( initial ) ;
171+ const first = thread . read ( ) ;
172+ thread . push ( replaceItem ( initial , { ...assistant , text : "Hello" } ) ) ;
173+ expect ( thread . read ( ) ) . toBe ( first ) ;
174+ thread . push ( null ) ;
175+ expect ( thread . read ( ) ) . toBeNull ( ) ;
176+ const reset = withItem ( { ...assistant , title : "Restored" , text : "New snapshot" } ) ;
177+ thread . push ( reset ) ;
178+ expect ( thread . read ( ) ) . not . toBe ( first ) ;
179+ expect ( thread . read ( ) ?. projection . turnItems [ 0 ] ) . toMatchObject ( { title : "Restored" } ) ;
180+ thread . push ( replaceItem ( reset , { ...assistant , title : "Restored" , text : "More text" } ) ) ;
181+ expect ( thread . read ( ) ?. projection . turnItems [ 0 ] ) . toMatchObject ( { title : "Restored" } ) ;
182+ } ) ;
183+
161184 it ( "leaves out the fields that streaming moves" , ( ) => {
162185 const thread = setup ( withItem ( assistant ) ) ;
163186 const projection = thread . read ( ) ?. projection ;
@@ -173,3 +196,120 @@ describe("quietThreadAtom", () => {
173196 } ) ;
174197 } ) ;
175198} ) ;
199+
200+ it ( "only compares replaced entries in a streamed reducer delta" , ( ) => {
201+ const size = 100 ;
202+ let slotReads = 0 ;
203+ const countReads = < T > ( items : ReadonlyArray < T > ) : ReadonlyArray < T > =>
204+ new Proxy ( items , {
205+ get ( target , key , receiver ) {
206+ if ( typeof key === "string" && / ^ \d + $ / . test ( key ) ) slotReads ++ ;
207+ return Reflect . get ( target , key , receiver ) ;
208+ } ,
209+ } ) ;
210+ const items = Array . from ( { length : size } , ( _ , index ) => ( {
211+ ...assistant ,
212+ id : TurnItemId . make ( `item-${ index } ` ) ,
213+ ordinal : index ,
214+ } ) ) ;
215+ const before = {
216+ ...v2Projection ,
217+ turnItems : countReads ( items ) ,
218+ visibleTurnItems : countReads (
219+ items . map ( ( item , position ) => ( {
220+ item,
221+ position,
222+ visibility : "local" as const ,
223+ sourceThreadId : v2ThreadId ,
224+ sourceItemId : item . id ,
225+ } ) ) ,
226+ ) ,
227+ } ;
228+ const next = applyOrchestrationV2ProjectionEvent ( before , {
229+ id : EventId . make ( "text-delta" ) ,
230+ type : "turn-item.updated" ,
231+ threadId : v2ThreadId ,
232+ occurredAt : later ,
233+ payload : { ...items . at ( - 1 ) ! , text : "Hello" , updatedAt : later } ,
234+ } ) ;
235+ slotReads = 0 ;
236+
237+ expect ( sameThreadStructure ( before , next ! ) ) . toBe ( true ) ;
238+ expect ( slotReads ) . toBeLessThanOrEqual ( 2 ) ;
239+ } ) ;
240+
241+ function replaceItem ( projection : OrchestrationV2ThreadProjection , item : OrchestrationV2TurnItem ) {
242+ return applyOrchestrationV2ProjectionEvent ( projection , {
243+ id : EventId . make ( `event-${ item . id } ` ) ,
244+ type : "turn-item.updated" ,
245+ threadId : v2ThreadId ,
246+ occurredAt : item . updatedAt ,
247+ payload : item ,
248+ } ) ! ;
249+ }
250+
251+ it ( "matches full structure comparisons across streamed, batched, and reordered updates" , ( ) => {
252+ const message = { ...assistant , ordinal : 2 } ;
253+ const reasoning = {
254+ ...assistant ,
255+ id : TurnItemId . make ( "reasoning" ) ,
256+ type : "reasoning" as const ,
257+ ordinal : 1 ,
258+ } ;
259+ const initial = mergeOlderHistoryIntoProjection ( withItem ( message ) , [
260+ withItem ( reasoning ) . visibleTurnItems [ 0 ] ! ,
261+ ] ) ;
262+ const updates = [
263+ { ...message , text : "Hello" , updatedAt : later } ,
264+ { ...reasoning , text : "Thinking" , updatedAt : later } ,
265+ { ...reasoning , text : "Done" , status : "completed" as const , streaming : false } ,
266+ { ...message , text : "Hello there" , updatedAt : later } ,
267+ { ...message , text : "Hello there" , ordinal : 0 } ,
268+ { ...message , text : "More text" , ordinal : 0 } ,
269+ { ...message , text : "More text" , ordinal : 2 } ,
270+ { ...message , text : "More text" , runId : RunId . make ( "another-run" ) } ,
271+ ] ;
272+ const projections = [ initial ] ;
273+ const withoutHints = ( projection : OrchestrationV2ThreadProjection ) => ( {
274+ ...projection ,
275+ turnItems : projection . turnItems . slice ( ) ,
276+ visibleTurnItems : projection . visibleTurnItems . slice ( ) ,
277+ } ) ;
278+ const consecutive : boolean [ ] = [ ] ;
279+ for ( const item of updates ) {
280+ const previous = projections . at ( - 1 ) ! ;
281+ const next = replaceItem ( previous , item ) ;
282+ consecutive . push ( sameThreadStructure ( previous , next ) ) ;
283+ // All earlier snapshots include readers that skip one or several updates in a batch.
284+ for ( const before of projections ) {
285+ expect ( sameThreadStructure ( before , next ) ) . toBe (
286+ sameThreadStructure ( withoutHints ( before ) , withoutHints ( next ) ) ,
287+ ) ;
288+ }
289+ projections . push ( next ) ;
290+ }
291+
292+ // A resumed snapshot can reuse item ids while changing a different row.
293+ const snapshot = withoutHints ( projections [ 1 ] ! ) ;
294+ const resumed = replaceItem ( snapshot , { ...reasoning , title : "Restored reasoning" } ) ;
295+ const sibling = replaceItem ( initial , { ...message , text : "Another stream" } ) ;
296+ const reconnected = replaceItem ( resumed , { ...message , text : "Resumed stream" } ) ;
297+ for ( const before of [ initial , sibling , projections [ 1 ] ! , resumed ] ) {
298+ expect ( sameThreadStructure ( before , reconnected ) ) . toBe (
299+ sameThreadStructure ( withoutHints ( before ) , withoutHints ( reconnected ) ) ,
300+ ) ;
301+ }
302+ expect ( sameThreadStructure ( sibling , reconnected ) ) . toBe ( false ) ;
303+ expect ( sameThreadStructure ( resumed , reconnected ) ) . toBe ( true ) ;
304+
305+ expect ( consecutive ) . toEqual ( [ true , true , false , true , false , true , false , false ] ) ;
306+ expect ( sameThreadStructure ( initial , projections [ 4 ] ! ) ) . toBe ( false ) ;
307+ expect ( projections [ 5 ] ! . visibleTurnItems . map ( ( row ) => [ row . item . id , row . position ] ) ) . toEqual ( [
308+ [ assistant . id , 0 ] ,
309+ [ reasoning . id , 1 ] ,
310+ ] ) ;
311+ expect ( projections [ 7 ] ! . visibleTurnItems . map ( ( row ) => [ row . item . id , row . position ] ) ) . toEqual ( [
312+ [ reasoning . id , 0 ] ,
313+ [ assistant . id , 1 ] ,
314+ ] ) ;
315+ } ) ;
0 commit comments