@@ -415,6 +415,173 @@ function makeThread(
415415}
416416
417417describe ( "buildThreadFeed" , ( ) => {
418+ it ( "reuses unchanged feed and presentation rows during an assistant text update" , ( ) => {
419+ const completedTurnId = TurnId . make ( "completed-turn" ) ;
420+ const activeTurnId = TurnId . make ( "active-turn" ) ;
421+ const thread = makeThread ( {
422+ id : ThreadId . make ( "feed-reuse" ) ,
423+ projectId : ProjectId . make ( "project-1" ) ,
424+ title : "Feed reuse" ,
425+ messages : [
426+ {
427+ id : MessageId . make ( "completed-message" ) ,
428+ role : "assistant" ,
429+ text : "Completed response" ,
430+ turnId : completedTurnId ,
431+ streaming : false ,
432+ createdAt : "2026-04-01T00:00:01.000Z" ,
433+ updatedAt : "2026-04-01T00:00:01.000Z" ,
434+ } ,
435+ {
436+ id : MessageId . make ( "streaming-message" ) ,
437+ role : "assistant" ,
438+ text : "Current response" ,
439+ turnId : activeTurnId ,
440+ streaming : true ,
441+ createdAt : "2026-04-01T00:00:04.000Z" ,
442+ updatedAt : "2026-04-01T00:00:04.000Z" ,
443+ } ,
444+ ] ,
445+ activities : [
446+ makeActivity ( {
447+ id : EventId . make ( "completed-tool" ) ,
448+ kind : "tool.completed" ,
449+ summary : "Read files" ,
450+ createdAt : "2026-04-01T00:00:02.000Z" ,
451+ turnId : completedTurnId ,
452+ payload : { itemType : "file_read" , status : "completed" } ,
453+ } ) ,
454+ makeActivity ( {
455+ id : EventId . make ( "active-tool" ) ,
456+ kind : "tool.updated" ,
457+ summary : "Run checks" ,
458+ createdAt : "2026-04-01T00:00:03.000Z" ,
459+ turnId : activeTurnId ,
460+ payload : { itemType : "command_execution" , command : "vp test" , status : "inProgress" } ,
461+ } ) ,
462+ ] ,
463+ } ) ;
464+ const latestTurn = {
465+ turnId : activeTurnId ,
466+ state : "running" as const ,
467+ startedAt : "2026-04-01T00:00:03.000Z" ,
468+ completedAt : null ,
469+ } ;
470+ const expandedTurns = new Set ( [ completedTurnId ] ) ;
471+ const expandedGroups = new Set ( [ "work-group:completed-tool" , "work-group:active-tool" ] ) ;
472+ const previousFeed = buildThreadFeed ( thread ) ;
473+ const previousRows = deriveThreadFeedPresentation (
474+ previousFeed ,
475+ latestTurn ,
476+ expandedTurns ,
477+ expandedGroups ,
478+ latestTurn . startedAt ,
479+ ) ;
480+ const updatedMessage = {
481+ ...thread . messages [ 1 ] ! ,
482+ text : "Current response with more text" ,
483+ updatedAt : "2026-04-01T00:00:05.000Z" ,
484+ } ;
485+ const nextFeed = buildThreadFeed ( {
486+ ...thread ,
487+ messages : [ thread . messages [ 0 ] ! , updatedMessage ] ,
488+ } ) ;
489+ const nextRows = deriveThreadFeedPresentation (
490+ nextFeed ,
491+ latestTurn ,
492+ expandedTurns ,
493+ expandedGroups ,
494+ latestTurn . startedAt ,
495+ ) ;
496+
497+ expect ( nextFeed ) . toHaveLength ( previousFeed . length ) ;
498+ expect ( nextRows ) . toHaveLength ( previousRows . length ) ;
499+ for ( const [ before , after ] of [
500+ [ previousFeed , nextFeed ] ,
501+ [ previousRows , nextRows ] ,
502+ ] as const ) {
503+ for ( const [ index , row ] of after . entries ( ) ) {
504+ if ( row . id === updatedMessage . id ) {
505+ expect ( row ) . not . toBe ( before [ index ] ) ;
506+ expect ( row ) . toMatchObject ( { message : updatedMessage } ) ;
507+ } else {
508+ expect ( row ) . toBe ( before [ index ] ) ;
509+ }
510+ }
511+ }
512+ expect ( nextRows . some ( ( row ) => row . type === "turn-fold" ) ) . toBe ( true ) ;
513+ expect ( nextRows . some ( ( row ) => row . type === "activity-group" ) ) . toBe ( true ) ;
514+ } ) ;
515+
516+ it ( "regroups cached activities for message changes and pagination" , ( ) => {
517+ const messages = [ 2 , 4 ] . map ( ( second ) => ( {
518+ id : MessageId . make ( `message-${ second } ` ) ,
519+ role : "assistant" as const ,
520+ text : second === 2 ? "" : "Response" ,
521+ streaming : false ,
522+ turnId : null ,
523+ createdAt : `2026-04-01T00:00:0${ second } .000Z` ,
524+ updatedAt : `2026-04-01T00:00:0${ second } .000Z` ,
525+ } ) ) ;
526+ const thread = makeThread ( {
527+ id : ThreadId . make ( "feed-regroup" ) ,
528+ projectId : ProjectId . make ( "project-1" ) ,
529+ title : "Feed grouping" ,
530+ messages,
531+ activities : [ 1 , 3 , 5 ] . map ( ( second ) =>
532+ makeActivity ( {
533+ id : EventId . make ( `work-${ second } ` ) ,
534+ kind : "runtime.warning" ,
535+ summary : `Notice ${ second } ` ,
536+ createdAt : `2026-04-01T00:00:0${ second } .000Z` ,
537+ } ) ,
538+ ) ,
539+ } ) ;
540+ const initial = buildThreadFeed ( thread ) ;
541+ expect ( initial . map ( ( row ) => row . id ) ) . toEqual ( [ "work-1" , "message-4" , "work-5" ] ) ;
542+ const split = buildThreadFeed ( {
543+ ...thread ,
544+ messages : [ { ...messages [ 0 ] ! , text : "Now visible" } , messages [ 1 ] ! ] ,
545+ } ) ;
546+ expect ( split . map ( ( row ) => row . id ) ) . toEqual ( [
547+ "work-1" ,
548+ "message-2" ,
549+ "work-3" ,
550+ "message-4" ,
551+ "work-5" ,
552+ ] ) ;
553+ expect ( split [ 0 ] ) . not . toBe ( initial [ 0 ] ) ;
554+ expect ( split . at ( - 1 ) ) . toBe ( initial . at ( - 1 ) ) ;
555+ expect ( initial [ 0 ] ) . toMatchObject ( { activities : [ { id : "work-1" } , { id : "work-3" } ] } ) ;
556+
557+ const reordered = buildThreadFeed ( {
558+ ...thread ,
559+ messages : [ messages [ 0 ] ! , { ...messages [ 1 ] ! , createdAt : "2026-04-01T00:00:06.000Z" } ] ,
560+ } ) ;
561+ expect ( reordered . map ( ( row ) => row . id ) ) . toEqual ( [ "work-1" , "message-4" ] ) ;
562+ expect ( reordered [ 0 ] ) . toMatchObject ( {
563+ activities : [ { id : "work-1" } , { id : "work-3" } , { id : "work-5" } ] ,
564+ } ) ;
565+ const olderMessage = {
566+ ...messages [ 1 ] ! ,
567+ id : MessageId . make ( "older-message" ) ,
568+ createdAt : "2026-04-01T00:00:00.000Z" ,
569+ } ;
570+ const page = buildThreadFeed ( thread , {
571+ loadedMessages : [ messages [ 1 ] ! ] ,
572+ localMessages : [ olderMessage ] ,
573+ } ) ;
574+ expect ( page . map ( ( row ) => row . id ) ) . toEqual ( [ "older-message" , "message-4" , "work-5" ] ) ;
575+ const prepended = buildThreadFeed ( thread , { loadedMessages : [ olderMessage , ...messages ] } ) ;
576+ expect ( prepended . map ( ( row ) => row . id ) ) . toEqual ( [
577+ "older-message" ,
578+ "work-1" ,
579+ "message-4" ,
580+ "work-5" ,
581+ ] ) ;
582+ expect ( prepended . at ( - 1 ) ) . toBe ( page . at ( - 1 ) ) ;
583+ } ) ;
584+
418585 it ( "keeps context compaction as a standalone timeline row" , ( ) => {
419586 const thread = makeThread ( {
420587 id : ThreadId . make ( "thread-context-compaction" ) ,
@@ -696,7 +863,8 @@ describe("buildThreadFeed", () => {
696863 } ;
697864
698865 for ( const currentTurn of [ null , latestTurn ] ) {
699- const feed = buildThreadFeed ( { ...thread , latestTurn : currentTurn } ) ;
866+ const currentThread = { ...thread , latestTurn : currentTurn } ;
867+ const feed = buildThreadFeed ( currentThread ) ;
700868 expect ( feed ) . toMatchObject ( [
701869 {
702870 type : "activity-group" ,
@@ -1618,6 +1786,30 @@ describe("buildThreadFeed", () => {
16181786 "work-toggle:work-group:tool-completed" ,
16191787 "assistant-final" ,
16201788 ] ) ;
1789+
1790+ const interrupted = deriveThreadFeedPresentation (
1791+ feed ,
1792+ { ...thread . latestTurn ! , state : "interrupted" , completedAt : "2026-04-01T00:00:20.000Z" } ,
1793+ new Set ( ) ,
1794+ ) ;
1795+ expect ( interrupted [ 1 ] ) . toMatchObject ( {
1796+ type : "turn-fold" ,
1797+ label : "You stopped after 19s" ,
1798+ expanded : false ,
1799+ } ) ;
1800+ const retimed = deriveThreadFeedPresentation (
1801+ buildThreadFeed ( {
1802+ ...thread ,
1803+ messages : [
1804+ thread . messages [ 0 ] ! ,
1805+ { ...thread . messages [ 1 ] ! , updatedAt : "2026-04-01T00:00:25.000Z" } ,
1806+ ] ,
1807+ } ) ,
1808+ null ,
1809+ new Set ( ) ,
1810+ ) ;
1811+ expect ( retimed [ 1 ] ) . toMatchObject ( { type : "turn-fold" , label : "Worked for 23s" } ) ;
1812+ expect ( collapsed [ 1 ] ) . toMatchObject ( { type : "turn-fold" , label : "Worked for 17s" } ) ;
16211813 } ) ;
16221814
16231815 it ( "folds assistant messages between the first and terminal messages" , ( ) => {
@@ -1916,6 +2108,15 @@ describe("buildThreadFeed", () => {
19162108 { id : "activity-3" , groupedToolDetail : true , live : false } ,
19172109 ] ,
19182110 } ) ;
2111+ const unchanged = deriveThreadFeedPresentation (
2112+ feed ,
2113+ null ,
2114+ new Set ( ) ,
2115+ new Set ( [ "work-group:activity-1" , "unrelated-group" ] ) ,
2116+ ) ;
2117+ expect ( unchanged [ 0 ] ) . toBe ( expanded [ 0 ] ) ;
2118+ expect ( unchanged [ 1 ] ) . toBe ( expanded [ 1 ] ) ;
2119+ expect ( deriveThreadFeedPresentation ( feed , null , new Set ( ) ) ) . toEqual ( collapsed ) ;
19192120 } ) ;
19202121
19212122 it . each (
@@ -2272,6 +2473,43 @@ describe("buildThreadFeed", () => {
22722473 { id : "call-b-2" , lifecycleStatus : "completed" , groupedToolDetail : true , live : false } ,
22732474 ] ,
22742475 } ) ;
2476+
2477+ const correctedFeed = buildThreadFeed ( {
2478+ ...thread ,
2479+ activities : thread . activities . map ( ( activity ) =>
2480+ activity . id === "call-a-3"
2481+ ? {
2482+ ...activity ,
2483+ tone : "error" ,
2484+ payload : {
2485+ toolCallId : "call-a" ,
2486+ itemType : "command_execution" ,
2487+ status : "failed" ,
2488+ detail : "Corrected failure output" ,
2489+ } ,
2490+ }
2491+ : activity ,
2492+ ) ,
2493+ } ) ;
2494+ const correctedGroup = correctedFeed . find ( ( entry ) => entry . type === "activity-group" ) ;
2495+ expect ( correctedGroup ) . toMatchObject ( {
2496+ activities : [
2497+ { id : "call-a-1" , lifecycleStatus : "failed" , detail : "Corrected failure output" } ,
2498+ { id : "call-b-2" , lifecycleStatus : "completed" , detail : "second output" } ,
2499+ ] ,
2500+ } ) ;
2501+ expect ( correctedGroup ?. activities [ 0 ] ?. getCopyText ( ) ) . toContain ( "Corrected failure output" ) ;
2502+ expect ( activityGroup ?. activities [ 0 ] ?. getCopyText ( ) ) . toContain ( "first output" ) ;
2503+ const correctedRows = deriveThreadFeedPresentation (
2504+ correctedFeed ,
2505+ null ,
2506+ new Set ( [ turnId ] ) ,
2507+ new Set ( [ groupId ] ) ,
2508+ ) ;
2509+ expect ( correctedRows . find ( ( entry ) => entry . type === "activity-group" ) ) . toMatchObject ( {
2510+ id : "call-a-1" ,
2511+ activities : [ { status : "failure" , workEntry : { tone : "error" } } ] ,
2512+ } ) ;
22752513 } ) ;
22762514} ) ;
22772515
0 commit comments