11import {
2+ OrchestrationV2AppThread ,
23 OrchestrationV2TurnItem ,
34 type OrchestrationV2ThreadProjection ,
45 type ScopedThreadRef ,
@@ -21,6 +22,7 @@ import type {
2122 EnvironmentThread ,
2223 EnvironmentThreadStructure ,
2324 ThreadStructure ,
25+ ThreadStructureThread ,
2426 ThreadStructureTurnItem ,
2527 ThreadStructureVisibleTurnItem ,
2628} from "./models.ts" ;
@@ -41,6 +43,7 @@ const EMPTY_CHECKPOINT_SUMMARIES: ReadonlyArray<ThreadCheckpointSummary> = Objec
4143
4244type TurnItem = OrchestrationV2ThreadProjection [ "turnItems" ] [ number ] ;
4345type VisibleTurnItem = OrchestrationV2ThreadProjection [ "visibleTurnItems" ] [ number ] ;
46+ type AppThread = OrchestrationV2ThreadProjection [ "thread" ] ;
4447
4548// Items are stripped once each: unchanged items keep their identity across
4649// structure changes, which the incremental timeline relies on.
@@ -76,45 +79,83 @@ export function isCompleteTurnItemRow(row: ThreadStructureVisibleTurnItem): row
7679 return row . item . type !== "assistant_message" && row . item . type !== "reasoning" ;
7780}
7881
82+ // The open thread is marked visited several times while a reply streams. Visit
83+ // state is read from the shell, or from lastVisitedAtAtom while the thread is
84+ // not in the shell index yet, so a mark is not structure.
85+ function toThreadStructureThread ( thread : AppThread ) : ThreadStructureThread {
86+ const { updatedAt : _updatedAt , lastVisitedAt : _lastVisitedAt , ...structure } = thread ;
87+ return structure ;
88+ }
89+
7990function toThreadStructure ( projection : OrchestrationV2ThreadProjection ) : ThreadStructure {
8091 const { updatedAt : _updatedAt , ...structure } = projection ;
8192 return {
8293 ...structure ,
94+ thread : toThreadStructureThread ( projection . thread ) ,
8395 turnItems : projection . turnItems . map ( toThreadStructureTurnItem ) ,
8496 visibleTurnItems : projection . visibleTurnItems . map ( toThreadStructureVisibleTurnItem ) ,
8597 } ;
8698}
8799
88100const sameTurnItem = Schema . toEquivalence ( OrchestrationV2TurnItem ) ;
101+ const sameAppThread = Schema . toEquivalence ( OrchestrationV2AppThread ) ;
102+
103+ // Overlays previous's structure on next, so the fields the strip functions
104+ // remove are exactly the ones ignored here.
105+ function sameApartFromStripped < T extends object > (
106+ same : ( left : T , right : T ) => boolean ,
107+ previous : T ,
108+ previousStructure : object ,
109+ next : T ,
110+ ) : boolean {
111+ return (
112+ Object . keys ( previous ) . length === Object . keys ( next ) . length &&
113+ same ( Object . assign ( { } , next , previousStructure ) , next )
114+ ) ;
115+ }
89116
90- // Compares through toThreadStructureTurnItem, so the fields it strips are
91- // exactly the ones ignored here.
92117function sameTurnItemStructure ( previous : TurnItem , next : TurnItem ) : boolean {
93118 if ( previous === next ) return true ;
94119 const structure = toThreadStructureTurnItem ( previous ) ;
95120 if ( previous . id !== next . id || previous . type !== next . type || structure === previous ) {
96121 return false ;
97122 }
98- return (
99- Object . keys ( previous ) . length === Object . keys ( next ) . length &&
100- sameTurnItem ( Object . assign ( { } , next , structure ) , next )
101- ) ;
123+ return sameApartFromStripped ( sameTurnItem , previous , structure , next ) ;
102124}
103125
104126/**
105127 * Whether `previous` and `next` have the same ThreadStructure: they differ
106- * only by text streamed into existing assistant and reasoning items. The same
107- * items exist, with the same status, and every other field is unchanged.
128+ * only by text streamed into existing assistant and reasoning items or by a
129+ * visit mark. The same items exist, with the same status, and every other
130+ * field is unchanged.
108131 */
109132export function sameThreadStructure (
110133 previous : OrchestrationV2ThreadProjection ,
111134 next : OrchestrationV2ThreadProjection ,
112135) : boolean {
113136 if ( previous === next ) return true ;
114137 for ( const key of Object . keys ( next ) as Array < keyof OrchestrationV2ThreadProjection > ) {
115- if ( key === "turnItems" || key === "visibleTurnItems" || key === "updatedAt" ) continue ;
138+ if (
139+ key === "thread" ||
140+ key === "turnItems" ||
141+ key === "visibleTurnItems" ||
142+ key === "updatedAt"
143+ ) {
144+ continue ;
145+ }
116146 if ( previous [ key ] !== next [ key ] ) return false ;
117147 }
148+ if (
149+ previous . thread !== next . thread &&
150+ ! sameApartFromStripped (
151+ sameAppThread ,
152+ previous . thread ,
153+ toThreadStructureThread ( previous . thread ) ,
154+ next . thread ,
155+ )
156+ ) {
157+ return false ;
158+ }
118159 if (
119160 previous . turnItems . length !== next . turnItems . length ||
120161 previous . visibleTurnItems . length !== next . visibleTurnItems . length
@@ -304,6 +345,12 @@ export function createEnvironmentThreadDetailAtoms<E>(
304345 } ) . pipe ( Atom . setIdleTTL ( 0 ) , Atom . withLabel ( `environment-thread-checkpoint-summaries:${ key } ` ) ) ;
305346 } ) ;
306347
348+ const lastVisitedAtAtomFamily = Atom . family ( ( key : string ) =>
349+ Atom . make (
350+ ( get ) =>
351+ Option . getOrNull ( get ( threadStateValueAtomFamily ( key ) ) . data ) ?. thread . lastVisitedAt ?? null ,
352+ ) . pipe ( Atom . setIdleTTL ( 0 ) , Atom . withLabel ( `environment-thread-last-visited:${ key } ` ) ) ,
353+ ) ;
307354 const updatedAtAtomFamily = Atom . family ( ( key : string ) =>
308355 Atom . make (
309356 ( get ) => Option . getOrNull ( get ( threadStateValueAtomFamily ( key ) ) . data ) ?. updatedAt ?? null ,
@@ -372,6 +419,7 @@ export function createEnvironmentThreadDetailAtoms<E>(
372419 return {
373420 worktreePathAtom : ( ref : ScopedThreadRef ) => worktreePathAtomFamily ( threadKey ( ref ) ) ,
374421 updatedAtAtom : ( ref : ScopedThreadRef ) => updatedAtAtomFamily ( threadKey ( ref ) ) ,
422+ lastVisitedAtAtom : ( ref : ScopedThreadRef ) => lastVisitedAtAtomFamily ( threadKey ( ref ) ) ,
375423 pendingRequestsAtom : ( ref : ScopedThreadRef ) => pendingRequestsAtomFamily ( threadKey ( ref ) ) ,
376424 queueWorkflowAtom : ( ref : ScopedThreadRef ) => queueWorkflowAtomFamily ( threadKey ( ref ) ) ,
377425 queuedCountAtom : ( ref : ScopedThreadRef ) => queuedCountAtomFamily ( threadKey ( ref ) ) ,
0 commit comments