@@ -25,6 +25,9 @@ export type TranscriptDisplayPosition = z.infer<typeof positionSchema>;
2525export function readTranscriptDisplayPosition (
2626 value : unknown ,
2727) : TranscriptDisplayPosition | undefined {
28+ if ( ! asOptionalRecord ( value ) ) {
29+ return undefined ;
30+ }
2831 const parsed = positionSchema . safeParse ( value ) ;
2932 return parsed . success ? parsed . data : undefined ;
3033}
@@ -34,71 +37,86 @@ export function composeTranscriptDisplay<T>(
3437 values : T [ ] ,
3538 messageFor : ( value : T ) => unknown = ( value ) => value ,
3639) : T [ ] {
37- type Row = { value : T ; position : TranscriptDisplayPosition } ;
3840 const output : T [ ] = [ ] ;
39- let segment : Row [ ] = [ ] ;
41+ let start = 0 ;
42+ let positions : TranscriptDisplayPosition [ ] = [ ] ;
43+ let activities : number [ ] = [ ] ;
44+ let previousStableSeq : number | undefined ;
45+ let monotonic = true ;
4046 const flush = ( ) => {
41- const rows = segment ;
42- segment = [ ] ;
43- const stable = rows . filter ( ( row ) => ! row . position . activity ) ;
44- const monotonic = stable . every ( ( row , index ) => {
45- const previous = stable [ index - 1 ] ;
46- return ! previous || previous . position . rawSeq <= row . position . rawSeq ;
47- } ) ;
48- const activities = rows . flatMap ( ( row ) =>
49- row . position . activity ? [ { ...row , activity : row . position . activity } ] : [ ] ,
50- ) ;
51- if ( ! monotonic || activities . length === 0 ) {
52- for ( const row of rows ) {
53- output . push ( row . value ) ;
54- }
47+ if ( positions . length === 0 ) {
48+ return ;
49+ }
50+ const current = positions ;
51+ const activityIndexes = activities ;
52+ const recompose = monotonic && activityIndexes . length > 0 ;
53+ positions = [ ] ;
54+ activities = [ ] ;
55+ previousStableSeq = undefined ;
56+ monotonic = true ;
57+ if ( ! recompose ) {
5558 return ;
5659 }
57- const ordered = activities . toSorted ( ( left , right ) => {
58- const a = left . activity . afterRawSeq ;
59- const b = right . activity . afterRawSeq ;
60+ // Positions index the collected output tail. Detach only segments that need
61+ // recomposition, preserving each value captured by the message selector.
62+ const selected = output . splice ( start ) ;
63+ const ordered = activityIndexes . toSorted ( ( left , right ) => {
64+ const a = current [ left ] ! . activity ! . afterRawSeq ;
65+ const b = current [ right ] ! . activity ! . afterRawSeq ;
6066 if ( a === b ) {
61- return left . position . rawSeq - right . position . rawSeq ;
67+ return current [ left ] ! . rawSeq - current [ right ] ! . rawSeq ;
6268 }
6369 if ( a === null ) {
6470 return - 1 ;
6571 }
6672 return b === null ? 1 : a - b ;
6773 } ) ;
68- const iterator = ordered . values ( ) ;
69- let next = iterator . next ( ) ;
74+ let next = 0 ;
7075 const emitGap = ( beforeRawSeq ?: number ) => {
71- const cohorts = new Map < string , { firstSeq : number ; rows : typeof activities } > ( ) ;
72- while ( ! next . done ) {
73- const row = next . value ;
74- const afterRawSeq = row . activity . afterRawSeq ;
75- if ( beforeRawSeq !== undefined && afterRawSeq !== null && afterRawSeq >= beforeRawSeq ) {
76+ let cohorts : Map < string , { firstSeq : number ; rows : number [ ] } > | undefined ;
77+ while ( next < ordered . length ) {
78+ const index = ordered [ next ] ! ;
79+ const position = current [ index ] ! ;
80+ const activity = position . activity ! ;
81+ if (
82+ beforeRawSeq !== undefined &&
83+ activity . afterRawSeq !== null &&
84+ activity . afterRawSeq >= beforeRawSeq
85+ ) {
7686 break ;
7787 }
78- let cohort = cohorts . get ( row . activity . scopeId ) ;
88+ cohorts ??= new Map ( ) ;
89+ let cohort = cohorts . get ( activity . scopeId ) ;
7990 if ( ! cohort ) {
80- cohort = { firstSeq : row . position . rawSeq , rows : [ ] } ;
81- cohorts . set ( row . activity . scopeId , cohort ) ;
91+ cohort = { firstSeq : position . rawSeq , rows : [ ] } ;
92+ cohorts . set ( activity . scopeId , cohort ) ;
8293 }
83- cohort . firstSeq = Math . min ( cohort . firstSeq , row . position . rawSeq ) ;
84- cohort . rows . push ( row ) ;
85- next = iterator . next ( ) ;
94+ cohort . firstSeq = Math . min ( cohort . firstSeq , position . rawSeq ) ;
95+ cohort . rows . push ( index ) ;
96+ next += 1 ;
97+ }
98+ if ( ! cohorts ) {
99+ return ;
86100 }
87101 // Scope cohorts keep a total order; comparing ordinals only for matching
88102 // scopes inside one comparator would be non-transitive across attempts.
89103 for ( const cohort of [ ...cohorts . values ( ) ] . toSorted ( ( a , b ) => a . firstSeq - b . firstSeq ) ) {
90104 const sorted = cohort . rows . toSorted (
91105 ( a , b ) =>
92- a . activity . startOrder - b . activity . startOrder || a . position . rawSeq - b . position . rawSeq ,
106+ current [ a ] ! . activity ! . startOrder - current [ b ] ! . activity ! . startOrder ||
107+ current [ a ] ! . rawSeq - current [ b ] ! . rawSeq ,
93108 ) ;
94- for ( const row of sorted ) {
95- output . push ( row . value ) ;
109+ for ( const index of sorted ) {
110+ output . push ( selected [ index ] ! ) ;
96111 }
97112 }
98113 } ;
99- for ( const row of stable ) {
100- emitGap ( row . position . rawSeq ) ;
101- output . push ( row . value ) ;
114+ for ( let index = 0 ; index < current . length ; index += 1 ) {
115+ const position = current [ index ] ! ;
116+ if ( ! position . activity ) {
117+ emitGap ( position . rawSeq ) ;
118+ output . push ( selected [ index ] ! ) ;
119+ }
102120 }
103121 emitGap ( ) ;
104122 } ;
@@ -110,12 +128,21 @@ export function composeTranscriptDisplay<T>(
110128 if ( ! position ) {
111129 flush ( ) ;
112130 output . push ( value ) ;
131+ start = output . length ;
113132 continue ;
114133 }
115- if ( segment . at ( - 1 ) ?. position . source !== position . source ) {
134+ if ( positions . at ( - 1 ) ?. source !== position . source ) {
116135 flush ( ) ;
136+ start = output . length ;
137+ }
138+ if ( position . activity ) {
139+ activities . push ( positions . length ) ;
140+ } else {
141+ monotonic &&= previousStableSeq === undefined || previousStableSeq <= position . rawSeq ;
142+ previousStableSeq = position . rawSeq ;
117143 }
118- segment . push ( { value, position } ) ;
144+ positions . push ( position ) ;
145+ output . push ( value ) ;
119146 }
120147 flush ( ) ;
121148 return output . every ( ( value , index ) => value === values [ index ] ) ? values : output ;
0 commit comments