@@ -1122,6 +1122,141 @@ describe("deriveMessagesTimelineRows", () => {
11221122 ] ) ;
11231123 } ) ;
11241124
1125+ it ( "folds a settled subagent spawn row and keeps a live one outside the fold" , ( ) => {
1126+ const firstMessage : ChatMessage = {
1127+ id : MessageId . make ( "assistant-first-entry" ) ,
1128+ role : "assistant" ,
1129+ text : "Fanning out." ,
1130+ turnId : TurnId . make ( "turn-1" ) ,
1131+ createdAt : "2026-01-01T00:00:01Z" ,
1132+ updatedAt : "2026-01-01T00:00:02Z" ,
1133+ streaming : false ,
1134+ } ;
1135+ const entriesWith = ( agentSpawn : { workflowId : string | null ; agentTaskIds : string [ ] } ) =>
1136+ deriveTimelineEntries (
1137+ [
1138+ firstMessage ,
1139+ {
1140+ ...firstMessage ,
1141+ id : MessageId . make ( "assistant-final-entry" ) ,
1142+ text : "Done." ,
1143+ createdAt : "2026-01-01T00:00:05Z" ,
1144+ updatedAt : "2026-01-01T00:00:06Z" ,
1145+ } ,
1146+ ] ,
1147+ [ ] ,
1148+ [
1149+ {
1150+ id : "spawn-entry" ,
1151+ createdAt : "2026-01-01T00:00:03Z" ,
1152+ turnId : firstMessage . turnId ,
1153+ label : "Ran 2 subagents" ,
1154+ tone : "tool" ,
1155+ agentSpawn,
1156+ } ,
1157+ ] ,
1158+ ) ;
1159+ const direct = entriesWith ( { workflowId : null , agentTaskIds : [ "agent-a" , "agent-b" ] } ) ;
1160+ const workflow = entriesWith ( { workflowId : "wf-1" , agentTaskIds : [ "wf-1" , "agent-a" ] } ) ;
1161+ const derive = (
1162+ timelineEntries : typeof direct ,
1163+ liveAgentTaskIds : ReadonlySet < string > | undefined ,
1164+ expandedSpawnEntryIds ?: ReadonlySet < string > ,
1165+ expandedTurnIds ?: ReadonlySet < TurnId > ,
1166+ ) =>
1167+ deriveMessagesTimelineRows ( {
1168+ timelineEntries,
1169+ isWorking : false ,
1170+ activeTurnStartedAt : null ,
1171+ turnDiffSummaries : [ ] ,
1172+ supportsConversationRollback : false ,
1173+ liveAgentTaskIds,
1174+ expandedSpawnEntryIds,
1175+ ...( expandedTurnIds ? { expandedTurnIds } : { } ) ,
1176+ } ) . map ( ( row ) => row . id ) ;
1177+ const folded = [ "turn-fold:turn-1" , "assistant-final-entry" ] ;
1178+ const unfolded = [ "turn-fold:turn-1" , "spawn-entry" , "assistant-final-entry" ] ;
1179+
1180+ const activeRows = (
1181+ timelineEntries : typeof direct ,
1182+ liveAgentTaskIds : ReadonlySet < string > ,
1183+ runningTurnId = "turn-1" ,
1184+ ) =>
1185+ deriveMessagesTimelineRows ( {
1186+ timelineEntries : timelineEntries . slice ( 0 , 2 ) ,
1187+ isWorking : true ,
1188+ runningTurnId : runningTurnId as TurnId ,
1189+ activeTurnStartedAt : "2026-01-01T00:00:00Z" ,
1190+ turnDiffSummaries : [ ] ,
1191+ supportsConversationRollback : false ,
1192+ liveAgentTaskIds,
1193+ } ) . map ( ( row ) => row . kind ) ;
1194+ expect ( activeRows ( direct , new Set ( [ "agent-b" ] ) ) ) . not . toContain ( "thinking" ) ;
1195+ expect ( activeRows ( workflow , new Set ( [ "wf-1" ] ) ) ) . not . toContain ( "thinking" ) ;
1196+ expect ( activeRows ( direct , new Set ( ) ) ) . toContain ( "thinking" ) ;
1197+ expect ( activeRows ( direct , new Set ( [ "agent-b" ] ) , "turn-2" ) ) . toContain ( "thinking" ) ;
1198+
1199+ for ( const [ toolLifecycleStatus , tone , sourceActivityKind ] of [
1200+ [ "inProgress" , "tool" , "tool.updated" ] ,
1201+ [ "completed" , "tool" , "tool.completed" ] ,
1202+ // Claude background Bash completions arrive without a command or item type.
1203+ [ "completed" , "info" , "task.completed" ] ,
1204+ ] as const ) {
1205+ const laterTool = {
1206+ id : "later-tool" ,
1207+ kind : "work" as const ,
1208+ createdAt : "2026-01-01T00:00:04Z" ,
1209+ entry : {
1210+ id : "later-tool" ,
1211+ turnId : "turn-1" as TurnId ,
1212+ createdAt : "2026-01-01T00:00:04Z" ,
1213+ label : "Read file" ,
1214+ tone,
1215+ sourceActivityKind,
1216+ toolLifecycleStatus,
1217+ } ,
1218+ } ;
1219+ for ( const trailingEntries of [ [ ] , [ laterTool ] ] ) {
1220+ const rows = deriveMessagesTimelineRows ( {
1221+ timelineEntries : [ ...direct . slice ( 0 , 2 ) , ...trailingEntries ] ,
1222+ isWorking : true ,
1223+ runningTurnId : "turn-1" as TurnId ,
1224+ activeTurnStartedAt : "2026-01-01T00:00:00Z" ,
1225+ turnDiffSummaries : [ ] ,
1226+ supportsConversationRollback : false ,
1227+ liveAgentTaskIds : new Set ( [ "agent-b" ] ) ,
1228+ } ) ;
1229+ expect ( rows . filter ( ( row ) => row . kind === "work-live" ) ) . toMatchObject ( [
1230+ {
1231+ id : "live-activity-row" ,
1232+ entry : { id : trailingEntries . length ? "later-tool" : "spawn-entry" } ,
1233+ active : true ,
1234+ } ,
1235+ ] ) ;
1236+ expect ( rows . some ( ( row ) => row . kind === "work" || row . kind === "thinking" ) ) . toBe ( false ) ;
1237+ }
1238+ }
1239+
1240+ expect ( derive ( direct , new Set ( ) ) ) . toEqual ( folded ) ;
1241+ expect ( derive ( direct , new Set ( [ "agent-b" ] ) ) ) . toEqual ( unfolded ) ;
1242+ // A workflow coordinator between phases keeps its batch out of the fold.
1243+ expect ( derive ( workflow , new Set ( [ "wf-1" ] ) ) ) . toEqual ( unfolded ) ;
1244+ expect ( derive ( workflow , new Set ( ) ) ) . toEqual ( folded ) ;
1245+ // No live set is known.
1246+ expect ( derive ( direct , undefined ) ) . toEqual ( unfolded ) ;
1247+ // The user has it open: it stays visible under the collapsed fold and
1248+ // keeps its place when the fold is expanded.
1249+ expect ( derive ( direct , new Set ( ) , new Set ( [ "spawn-entry" ] ) ) ) . toEqual ( unfolded ) ;
1250+ expect (
1251+ derive ( direct , new Set ( ) , new Set ( [ "spawn-entry" ] ) , new Set ( [ "turn-1" as TurnId ] ) ) ,
1252+ ) . toEqual ( [
1253+ "turn-fold:turn-1" ,
1254+ "assistant-first-entry" ,
1255+ "spawn-entry" ,
1256+ "assistant-final-entry" ,
1257+ ] ) ;
1258+ } ) ;
1259+
11251260 it ( "only enables assistant copy for the terminal assistant message in a turn" , ( ) => {
11261261 const rows = deriveMessagesTimelineRows ( {
11271262 timelineEntries : [
@@ -2286,7 +2421,7 @@ describe("deriveMessagesTimelineRows", () => {
22862421 it . each ( [
22872422 [ undefined , true ] ,
22882423 [ "inProgress" , true ] ,
2289- [ "completed" , false ] ,
2424+ [ "completed" , true ] ,
22902425 [ "failed" , null ] ,
22912426 [ "declined" , false ] ,
22922427 [ "stopped" , false ] ,
@@ -2329,6 +2464,7 @@ describe("deriveMessagesTimelineRows", () => {
23292464 expect ( rows . at ( - 1 ) ) . toMatchObject ( { kind : "thinking" , id : "live-activity-row" } ) ;
23302465 } else {
23312466 expect ( workLiveRow ) . toMatchObject ( { active } ) ;
2467+ if ( active ) expect ( rows . some ( ( row ) => row . kind === "thinking" ) ) . toBe ( false ) ;
23322468 }
23332469 } ,
23342470 ) ;
0 commit comments