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,133 @@ describe("quietThreadAtom", () => {
173196 } ) ;
174197 } ) ;
175198} ) ;
199+
200+ it . each ( [ 100 , 1000 , 10000 ] ) (
201+ "only compares replaced entries in a reducer delta among %i items" ,
202+ ( size ) => {
203+ let slotReads = 0 ;
204+ const countReads = < T > ( items : ReadonlyArray < T > ) : ReadonlyArray < T > =>
205+ new Proxy ( items , {
206+ get ( target , key , receiver ) {
207+ if ( typeof key === "string" && / ^ \d + $ / . test ( key ) ) slotReads ++ ;
208+ return Reflect . get ( target , key , receiver ) ;
209+ } ,
210+ } ) ;
211+ const items = Array . from ( { length : size } , ( _ , index ) => ( {
212+ ...assistant ,
213+ id : TurnItemId . make ( `item-${ index } ` ) ,
214+ ordinal : index ,
215+ } ) ) ;
216+ const before = {
217+ ...v2Projection ,
218+ turnItems : countReads ( items ) ,
219+ visibleTurnItems : countReads (
220+ items . map ( ( item , position ) => ( {
221+ item,
222+ position,
223+ visibility : "local" as const ,
224+ sourceThreadId : v2ThreadId ,
225+ sourceItemId : item . id ,
226+ } ) ) ,
227+ ) ,
228+ } ;
229+ const next = applyOrchestrationV2ProjectionEvent ( before , {
230+ id : EventId . make ( "text-delta" ) ,
231+ type : "turn-item.updated" ,
232+ threadId : v2ThreadId ,
233+ occurredAt : later ,
234+ payload : { ...items . at ( - 1 ) ! , text : "Hello" , updatedAt : later } ,
235+ } ) ;
236+ slotReads = 0 ;
237+
238+ expect ( sameThreadStructure ( before , next ! ) ) . toBe ( true ) ;
239+ expect ( slotReads ) . toBeLessThanOrEqual ( 2 ) ;
240+ } ,
241+ ) ;
242+
243+ function replaceItem ( projection : OrchestrationV2ThreadProjection , item : OrchestrationV2TurnItem ) {
244+ return applyOrchestrationV2ProjectionEvent ( projection , {
245+ id : EventId . make ( `event-${ item . id } ` ) ,
246+ type : "turn-item.updated" ,
247+ threadId : v2ThreadId ,
248+ occurredAt : item . updatedAt ,
249+ payload : item ,
250+ } ) ! ;
251+ }
252+
253+ describe ( "reducer structure comparisons" , ( ) => {
254+ it . each ( [ "assistant_message" , "reasoning" ] as const ) (
255+ "keeps %s text quiet but observes completion and run changes" ,
256+ ( type ) => {
257+ const item = { ...assistant , type } ;
258+ const initial = withItem ( item ) ;
259+ const streamed = replaceItem ( initial , { ...item , text : "Hello" , updatedAt : later } ) ;
260+ const completed = replaceItem ( streamed , {
261+ ...item ,
262+ status : "completed" ,
263+ streaming : false ,
264+ text : "Hello there" ,
265+ } ) ;
266+ const movedRun = replaceItem ( completed , { ...item , runId : RunId . make ( "another-run" ) } ) ;
267+
268+ expect ( sameThreadStructure ( initial , streamed ) ) . toBe ( true ) ;
269+ expect ( sameThreadStructure ( streamed , completed ) ) . toBe ( false ) ;
270+ expect ( sameThreadStructure ( completed , movedRun ) ) . toBe ( false ) ;
271+ expect ( streamed . visibleTurnItems [ 0 ] ?. item ) . toMatchObject ( { text : "Hello" } ) ;
272+ expect ( initial . turnItems [ 0 ] ) . toBe ( item ) ;
273+ expect ( initial . visibleTurnItems [ 0 ] ?. item ) . toMatchObject ( { text : "Hel" } ) ;
274+ } ,
275+ ) ;
276+
277+ it ( "compares all changes when the reader skips events in a batch" , ( ) => {
278+ const initial = mergeOlderHistoryIntoProjection ( withItem ( assistant ) , [
279+ {
280+ ...withItem ( assistant ) . visibleTurnItems [ 0 ] ! ,
281+ sourceItemId : TurnItemId . make ( "older" ) ,
282+ item : { ...assistant , id : TurnItemId . make ( "older" ) , ordinal : 0 } ,
283+ } ,
284+ ] ) ;
285+ const older = initial . turnItems . find ( ( item ) => item . id === "older" ) ! ;
286+ const completed = replaceItem ( initial , { ...older , status : "completed" } ) ;
287+ const streamed = replaceItem ( completed , { ...assistant , text : "Hello" , updatedAt : later } ) ;
288+
289+ expect ( sameThreadStructure ( completed , streamed ) ) . toBe ( true ) ;
290+ expect ( sameThreadStructure ( initial , streamed ) ) . toBe ( false ) ;
291+ expect (
292+ sameThreadStructure ( initial , {
293+ ...streamed ,
294+ turnItems : initial . turnItems ,
295+ visibleTurnItems : streamed . visibleTurnItems . slice ( ) ,
296+ } ) ,
297+ ) . toBe ( false ) ;
298+ } ) ;
299+
300+ it ( "compares snapshot resets and history merges without a replacement hint" , ( ) => {
301+ const initial = withItem ( assistant ) ;
302+ const streamed = replaceItem ( initial , { ...assistant , text : "Hello" , updatedAt : later } ) ;
303+ const snapshot = {
304+ ...streamed ,
305+ turnItems : streamed . turnItems . map ( ( item ) => ( { ...item , title : "Restored" } ) ) ,
306+ visibleTurnItems : streamed . visibleTurnItems . map ( ( row ) => ( { ...row } ) ) ,
307+ } ;
308+ const older = { ...assistant , id : TurnItemId . make ( "older" ) , ordinal : 0 } ;
309+ const history = mergeOlderHistoryIntoProjection ( streamed , [
310+ {
311+ position : 0 ,
312+ visibility : "local" ,
313+ sourceThreadId : v2ThreadId ,
314+ sourceItemId : older . id ,
315+ item : older ,
316+ } ,
317+ ] ) ;
318+ const next = replaceItem ( history , { ...assistant , text : "Hello there" , updatedAt : later } ) ;
319+
320+ expect ( sameThreadStructure ( initial , snapshot ) ) . toBe ( false ) ;
321+ expect ( sameThreadStructure ( streamed , history ) ) . toBe ( false ) ;
322+ expect ( sameThreadStructure ( history , next ) ) . toBe ( true ) ;
323+ expect ( next . visibleTurnItems . map ( ( row ) => [ row . item . id , row . position ] ) ) . toEqual ( [
324+ [ older . id , 0 ] ,
325+ [ assistant . id , 1 ] ,
326+ ] ) ;
327+ } ) ;
328+ } ) ;
0 commit comments