@@ -194,3 +194,190 @@ test("buildTranscript categorizes explicit Buzz tool calls for the activity bar"
194194 assert . deepEqual ( item . args , { limit : 20 } ) ;
195195 assert . equal ( item . status , "completed" ) ;
196196} ) ;
197+
198+ function sessionUpdate ( seq , update , overrides = { } ) {
199+ return {
200+ ...baseEvent ,
201+ ...overrides ,
202+ seq,
203+ kind : "acp_read" ,
204+ payload : {
205+ method : "session/update" ,
206+ params : {
207+ sessionId : overrides . sessionId ?? baseEvent . sessionId ,
208+ update,
209+ } ,
210+ } ,
211+ } ;
212+ }
213+
214+ function assistantChunk ( seq , messageId , text , overrides = { } ) {
215+ return sessionUpdate (
216+ seq ,
217+ {
218+ sessionUpdate : "agent_message_chunk" ,
219+ messageId,
220+ content : { type : "text" , text } ,
221+ } ,
222+ overrides ,
223+ ) ;
224+ }
225+
226+ test ( "buildTranscript de-duplicates repeated tool updates into one canonical row" , ( ) => {
227+ const items = toolItems ( [
228+ acpToolUpdate ( 40 , {
229+ sessionUpdate : "tool_call" ,
230+ toolCallId : "call-dupe" ,
231+ status : "executing" ,
232+ title : "shell" ,
233+ kind : "shell" ,
234+ rawInput : { command : "echo hi" } ,
235+ } ) ,
236+ acpToolUpdate ( 41 , {
237+ sessionUpdate : "tool_call_update" ,
238+ toolCallId : "call-dupe" ,
239+ status : "completed" ,
240+ title : "shell" ,
241+ kind : "shell" ,
242+ rawOutput : "hi" ,
243+ } ) ,
244+ acpToolUpdate ( 42 , {
245+ sessionUpdate : "tool_call_update" ,
246+ toolCallId : "call-dupe" ,
247+ status : "completed" ,
248+ title : "shell" ,
249+ kind : "shell" ,
250+ rawOutput : "hi" ,
251+ } ) ,
252+ ] ) ;
253+
254+ assert . equal ( items . length , 1 ) ;
255+ assert . equal ( items [ 0 ] . id , `tool:${ baseEvent . channelId } :call-dupe` ) ;
256+ assert . equal ( items [ 0 ] . status , "completed" ) ;
257+ assert . equal ( items [ 0 ] . result , "hi" ) ;
258+ } ) ;
259+
260+ test ( "buildTranscript keeps a completed tool terminal when a late executing call arrives" , ( ) => {
261+ const [ item ] = toolItems ( [
262+ acpToolUpdate ( 50 , {
263+ sessionUpdate : "tool_call_update" ,
264+ toolCallId : "call-regression" ,
265+ status : "completed" ,
266+ title : "shell" ,
267+ kind : "shell" ,
268+ rawOutput : "done" ,
269+ } ) ,
270+ acpToolUpdate ( 51 , {
271+ sessionUpdate : "tool_call" ,
272+ toolCallId : "call-regression" ,
273+ status : "executing" ,
274+ title : "shell" ,
275+ kind : "shell" ,
276+ rawInput : { command : "echo done" } ,
277+ } ) ,
278+ ] ) ;
279+
280+ assert . equal ( item . status , "completed" ) ;
281+ assert . equal ( item . completedAt , baseEvent . timestamp ) ;
282+ assert . deepEqual ( item . args , { command : "echo done" } ) ;
283+ assert . equal ( item . result , "done" ) ;
284+ } ) ;
285+
286+ test ( "buildTranscript rebuilds out-of-order tool frames as one canonical row with retained ids" , ( ) => {
287+ const [ item ] = toolItems ( [
288+ sessionUpdate (
289+ 60 ,
290+ {
291+ sessionUpdate : "tool_call_update" ,
292+ toolCallId : "call-out-of-order" ,
293+ status : "completed" ,
294+ title : "read_file" ,
295+ kind : "read_file" ,
296+ rawOutput : "file contents" ,
297+ } ,
298+ {
299+ channelId : "22222222-2222-2222-2222-222222222222" ,
300+ sessionId : "sess-2" ,
301+ turnId : "turn-2" ,
302+ timestamp : "2026-06-18T00:00:05Z" ,
303+ } ,
304+ ) ,
305+ sessionUpdate (
306+ 61 ,
307+ {
308+ sessionUpdate : "tool_call" ,
309+ toolCallId : "call-out-of-order" ,
310+ status : "executing" ,
311+ title : "read_file" ,
312+ kind : "read_file" ,
313+ rawInput : { path : "AGENTS.md" } ,
314+ } ,
315+ {
316+ channelId : "22222222-2222-2222-2222-222222222222" ,
317+ sessionId : "sess-2" ,
318+ turnId : "turn-2" ,
319+ timestamp : "2026-06-18T00:00:04Z" ,
320+ } ,
321+ ) ,
322+ ] ) ;
323+
324+ assert . equal (
325+ item . id ,
326+ "tool:22222222-2222-2222-2222-222222222222:call-out-of-order" ,
327+ ) ;
328+ assert . equal ( item . status , "completed" ) ;
329+ assert . deepEqual ( item . args , { path : "AGENTS.md" } ) ;
330+ assert . equal ( item . channelId , "22222222-2222-2222-2222-222222222222" ) ;
331+ assert . equal ( item . turnId , "turn-2" ) ;
332+ assert . equal ( item . sessionId , "sess-2" ) ;
333+ } ) ;
334+
335+ test ( "buildTranscript coalesces assistant chunks until the message is sealed" , ( ) => {
336+ const messages = buildTranscript ( [
337+ assistantChunk ( 70 , "msg-1" , "Hello " ) ,
338+ assistantChunk ( 71 , "msg-1" , "world" ) ,
339+ ] ) . filter ( ( item ) => item . type === "message" && item . role === "assistant" ) ;
340+
341+ assert . equal ( messages . length , 1 ) ;
342+ assert . equal ( messages [ 0 ] . text , "Hello world" ) ;
343+ assert . equal ( messages [ 0 ] . id , `assistant:${ baseEvent . channelId } :msg-1` ) ;
344+ } ) ;
345+
346+ test ( "buildTranscript starts a continuation for same-message chunks after sealing" , ( ) => {
347+ const messages = buildTranscript ( [
348+ assistantChunk ( 80 , "msg-2" , "First" ) ,
349+ acpToolUpdate ( 81 , {
350+ sessionUpdate : "tool_call" ,
351+ toolCallId : "call-seal" ,
352+ status : "executing" ,
353+ title : "shell" ,
354+ kind : "shell" ,
355+ } ) ,
356+ assistantChunk ( 82 , "msg-2" , "Second" ) ,
357+ ] ) . filter ( ( item ) => item . type === "message" && item . role === "assistant" ) ;
358+
359+ assert . equal ( messages . length , 2 ) ;
360+ assert . equal ( messages [ 0 ] . text , "First" ) ;
361+ assert . equal ( messages [ 1 ] . text , "Second" ) ;
362+ assert . match ( messages [ 1 ] . id , / : c \d + $ / ) ;
363+ } ) ;
364+
365+ test ( "buildTranscript preserves channel, turn, and session ids through message updates" , ( ) => {
366+ const [ message ] = buildTranscript ( [
367+ assistantChunk ( 90 , "msg-identity" , "One " , {
368+ channelId : "33333333-3333-3333-3333-333333333333" ,
369+ sessionId : "sess-identity" ,
370+ turnId : "turn-identity" ,
371+ } ) ,
372+ assistantChunk ( 91 , "msg-identity" , "Two" , {
373+ channelId : "33333333-3333-3333-3333-333333333333" ,
374+ sessionId : null ,
375+ turnId : null ,
376+ } ) ,
377+ ] ) . filter ( ( item ) => item . type === "message" && item . role === "assistant" ) ;
378+
379+ assert . equal ( message . text , "One Two" ) ;
380+ assert . equal ( message . channelId , "33333333-3333-3333-3333-333333333333" ) ;
381+ assert . equal ( message . turnId , "turn-identity" ) ;
382+ assert . equal ( message . sessionId , "sess-identity" ) ;
383+ } ) ;
0 commit comments