@@ -128,6 +128,137 @@ describe("realtime Talk conversation", () => {
128128 ] ) ;
129129 } ) ;
130130
131+ it ( "bounds streamed assistant delta growth while retaining useful context" , ( ) => {
132+ let state = createRealtimeTalkConversationState ( ) ;
133+ const opening = "Opening context stays visible. " ;
134+
135+ state = updateRealtimeTalkConversation ( state , {
136+ role : "assistant" ,
137+ text : `${ opening } ${ "a" . repeat ( 7_900 ) } ` ,
138+ final : false ,
139+ nowMs : 1 ,
140+ } ) ;
141+ state = updateRealtimeTalkConversation ( state , {
142+ role : "assistant" ,
143+ text : "b" . repeat ( 500 ) ,
144+ final : false ,
145+ nowMs : 2 ,
146+ } ) ;
147+ state = updateRealtimeTalkConversation ( state , {
148+ role : "assistant" ,
149+ text : `${ "c" . repeat ( 500 ) } NEWEST` ,
150+ final : false ,
151+ nowMs : 3 ,
152+ } ) ;
153+
154+ expect ( state . entries [ 0 ] ?. text . length ) . toBeLessThanOrEqual ( 8_000 ) ;
155+ expect ( state . entries [ 0 ] ?. text . startsWith ( opening ) ) . toBe ( true ) ;
156+ expect ( state . entries [ 0 ] ?. text ) . toContain ( "\n…\n" ) ;
157+ expect ( state . entries [ 0 ] ?. text . split ( "\n…\n" ) ) . toHaveLength ( 2 ) ;
158+ expect ( state . entries [ 0 ] ?. text . endsWith ( "NEWEST" ) ) . toBe ( true ) ;
159+ } ) ;
160+
161+ it ( "replaces a bounded assistant stream with the authoritative final transcript" , ( ) => {
162+ let state = createRealtimeTalkConversationState ( ) ;
163+ const opening = "Original opening context. " ;
164+
165+ state = updateRealtimeTalkConversation ( state , {
166+ role : "assistant" ,
167+ text : `${ opening } ${ "draft " . repeat ( 1_600 ) } ` ,
168+ final : false ,
169+ nowMs : 1 ,
170+ } ) ;
171+ expect ( state . entries [ 0 ] ?. text ) . toContain ( "\n…\n" ) ;
172+
173+ state = updateRealtimeTalkConversation ( state , {
174+ role : "assistant" ,
175+ text : `${ opening } corrected ${ "final " . repeat ( 1_600 ) } DONE` ,
176+ final : true ,
177+ nowMs : 2 ,
178+ } ) ;
179+
180+ expect ( state . entries [ 0 ] ?. text . length ) . toBeLessThanOrEqual ( 8_000 ) ;
181+ expect ( state . entries [ 0 ] ?. text . startsWith ( `${ opening } corrected ` ) ) . toBe ( true ) ;
182+ expect ( state . entries [ 0 ] ?. text ) . not . toContain ( "draft " ) ;
183+ expect ( state . entries [ 0 ] ?. text . endsWith ( "DONE" ) ) . toBe ( true ) ;
184+ expect ( state . entries [ 0 ] ?. isStreaming ) . toBe ( false ) ;
185+ } ) ;
186+
187+ it ( "does not expose dangling surrogates at a bounded transcript edge" , ( ) => {
188+ let state = createRealtimeTalkConversationState ( ) ;
189+ const transcript = `${ "a" . repeat ( 8_000 ) } 🚀${ "b" . repeat ( 7_740 ) } ` ;
190+
191+ state = updateRealtimeTalkConversation ( state , {
192+ role : "assistant" ,
193+ text : transcript ,
194+ final : true ,
195+ nowMs : 1 ,
196+ } ) ;
197+
198+ const text = state . entries [ 0 ] ?. text ?? "" ;
199+ expect ( text . length ) . toBeLessThanOrEqual ( 8_000 ) ;
200+ expect ( text ) . not . toMatch (
201+ / (?: [ \uD800 - \uDBFF ] (? ! [ \uDC00 - \uDFFF ] ) | (?< ! [ \uD800 - \uDBFF ] ) [ \uDC00 - \uDFFF ] ) / ,
202+ ) ;
203+ } ) ;
204+
205+ it ( "does not trust a natural truncation marker outside the bounded prefix" , ( ) => {
206+ let state = createRealtimeTalkConversationState ( ) ;
207+
208+ state = updateRealtimeTalkConversation ( state , {
209+ role : "assistant" ,
210+ text : `${ "a" . repeat ( 7_998 ) } \n…\n${ "b" . repeat ( 500 ) } NEWEST` ,
211+ final : true ,
212+ nowMs : 1 ,
213+ } ) ;
214+
215+ expect ( state . entries [ 0 ] ?. text . length ) . toBeLessThanOrEqual ( 8_000 ) ;
216+ expect ( state . entries [ 0 ] ?. text . startsWith ( "a" . repeat ( 256 ) ) ) . toBe ( true ) ;
217+ expect ( state . entries [ 0 ] ?. text . endsWith ( "NEWEST" ) ) . toBe ( true ) ;
218+ } ) ;
219+
220+ it . each ( [ 255 , 256 ] ) (
221+ "does not retain a lone high surrogate before a natural marker at offset %i" ,
222+ ( markerOffset ) => {
223+ let state = createRealtimeTalkConversationState ( ) ;
224+ const retainedText = "a" . repeat ( markerOffset - 1 ) ;
225+
226+ state = updateRealtimeTalkConversation ( state , {
227+ role : "assistant" ,
228+ text : `${ retainedText } \uD800\n…\n${ "b" . repeat ( 8_000 ) } NEWEST` ,
229+ final : true ,
230+ nowMs : 1 ,
231+ } ) ;
232+
233+ const text = state . entries [ 0 ] ?. text ?? "" ;
234+ expect ( text . length ) . toBeLessThanOrEqual ( 8_000 ) ;
235+ expect ( text . startsWith ( `${ retainedText } \n…\n` ) ) . toBe ( true ) ;
236+ expect ( text . endsWith ( "NEWEST" ) ) . toBe ( true ) ;
237+ expect ( text ) . not . toMatch (
238+ / (?: [ \uD800 - \uDBFF ] (? ! [ \uDC00 - \uDFFF ] ) | (?< ! [ \uD800 - \uDBFF ] ) [ \uDC00 - \uDFFF ] ) / ,
239+ ) ;
240+ } ,
241+ ) ;
242+
243+ it . each ( [ "user" , "assistant" ] as const ) (
244+ "bounds oversized final %s entries while retaining the newest text" ,
245+ ( role ) => {
246+ let state = createRealtimeTalkConversationState ( ) ;
247+
248+ state = updateRealtimeTalkConversation ( state , {
249+ role,
250+ text : `Useful opening. ${ "x" . repeat ( 9_000 ) } NEWEST` ,
251+ final : true ,
252+ nowMs : 1 ,
253+ } ) ;
254+
255+ expect ( state . entries [ 0 ] ?. text . length ) . toBeLessThanOrEqual ( 8_000 ) ;
256+ expect ( state . entries [ 0 ] ?. text . startsWith ( "Useful opening. " ) ) . toBe ( true ) ;
257+ expect ( state . entries [ 0 ] ?. text . endsWith ( "NEWEST" ) ) . toBe ( true ) ;
258+ expect ( state . entries [ 0 ] ?. isStreaming ) . toBe ( false ) ;
259+ } ,
260+ ) ;
261+
131262 it ( "keeps alternating realtime turns as separate bubbles" , ( ) => {
132263 let state = createRealtimeTalkConversationState ( ) ;
133264
0 commit comments