@@ -2295,6 +2295,251 @@ describe("buildThreadFeed", () => {
22952295 } ,
22962296 ) ;
22972297
2298+ it ( "groups ordered reasoning blocks, keeps the live slot, and restores the group after unfolding" , ( ) => {
2299+ const turnId = TurnId . make ( "reasoning-group" ) ;
2300+ const messages : OrchestrationThread [ "messages" ] = [ 1 , 2 , 3 , 4 ] . map ( ( second ) => ( {
2301+ id : MessageId . make ( `reasoning-${ second } ` ) ,
2302+ role : "reasoning" ,
2303+ text : `**Step ${ second } **\n\nCheck ${ second } .` ,
2304+ turnId,
2305+ streaming : second === 4 ,
2306+ createdAt : `2026-04-01T00:00:0${ second } .000Z` ,
2307+ updatedAt : `2026-04-01T00:00:0${ second } .000Z` ,
2308+ } ) ) ;
2309+ const thread = makeThread ( {
2310+ id : ThreadId . make ( "reasoning-group" ) ,
2311+ projectId : ProjectId . make ( "project-1" ) ,
2312+ title : "Reasoning" ,
2313+ messages,
2314+ latestTurn : {
2315+ turnId,
2316+ state : "running" ,
2317+ requestedAt : "2026-04-01T00:00:00.000Z" ,
2318+ startedAt : "2026-04-01T00:00:00.000Z" ,
2319+ completedAt : null ,
2320+ assistantMessageId : null ,
2321+ } ,
2322+ } ) ;
2323+ const feed = buildThreadFeed ( thread ) ;
2324+ const rows = deriveThreadFeedPresentation ( feed , thread . latestTurn , new Set ( ) , new Set ( ) , "now" ) ;
2325+ expect ( rows ) . toMatchObject ( [
2326+ { type : "work-toggle" , id : "live-activity-row" , summary : "Thinking" , hiddenCount : 4 } ,
2327+ ] ) ;
2328+ expect ( rows ) . toHaveLength ( 1 ) ;
2329+ expect (
2330+ deriveThreadFeedPresentation ( feed , thread . latestTurn , new Set ( ) , new Set ( ) , "now" ) [ 0 ] ,
2331+ ) . toBe ( rows [ 0 ] ) ;
2332+
2333+ const settledTurn = {
2334+ ...thread . latestTurn ! ,
2335+ state : "completed" as const ,
2336+ completedAt : "2026-04-01T00:00:06.000Z" ,
2337+ } ;
2338+ // Reasoning alone stays visible, including a streaming flag left behind on settlement.
2339+ expect ( deriveThreadFeedPresentation ( feed , settledTurn , new Set ( ) ) ) . toMatchObject ( [
2340+ { type : "work-toggle" , summary : "Thought (×4)" , hiddenCount : 4 } ,
2341+ ] ) ;
2342+ const completedMessages = messages . map ( ( message ) => ( { ...message , streaming : false } ) ) ;
2343+ const waiting = deriveThreadFeedPresentation (
2344+ buildThreadFeed ( { ...thread , messages : completedMessages } ) ,
2345+ thread . latestTurn ,
2346+ new Set ( ) ,
2347+ new Set ( ) ,
2348+ "now" ,
2349+ ) ;
2350+ expect ( waiting ) . toMatchObject ( [
2351+ { type : "work-toggle" , id : "live-activity-row" , summary : "Thinking" , hiddenCount : 4 } ,
2352+ ] ) ;
2353+ const feedWithWork = buildThreadFeed ( {
2354+ ...thread ,
2355+ activities : [
2356+ makeActivity ( {
2357+ id : EventId . make ( "reasoning-tool" ) ,
2358+ kind : "tool.completed" ,
2359+ tone : "tool" ,
2360+ summary : "Read files" ,
2361+ createdAt : "2026-04-01T00:00:05.000Z" ,
2362+ turnId,
2363+ payload : { itemType : "file_read" , status : "completed" } ,
2364+ } ) ,
2365+ ] ,
2366+ } ) ;
2367+ const toolRunning = deriveThreadFeedPresentation (
2368+ feedWithWork ,
2369+ thread . latestTurn ,
2370+ new Set ( ) ,
2371+ new Set ( ) ,
2372+ "now" ,
2373+ ) ;
2374+ expect ( toolRunning ) . toMatchObject ( [
2375+ { type : "work-toggle" , id : "live-activity-row" , shimmer : true , hiddenCount : 5 } ,
2376+ ] ) ;
2377+ expect ( toolRunning [ 0 ] ) . not . toMatchObject ( { summary : "Thinking" } ) ;
2378+ const nextThought = {
2379+ ...messages [ 3 ] ! ,
2380+ id : MessageId . make ( "reasoning-after-tool" ) ,
2381+ createdAt : "2026-04-01T00:00:06.000Z" ,
2382+ updatedAt : "2026-04-01T00:00:06.000Z" ,
2383+ } ;
2384+ const reasoningAgainFeed : ThreadFeedEntry [ ] = [
2385+ ...feedWithWork ,
2386+ {
2387+ type : "message" ,
2388+ id : nextThought . id ,
2389+ createdAt : nextThought . createdAt ,
2390+ message : nextThought ,
2391+ } ,
2392+ ] ;
2393+ const reasoningAgain = deriveThreadFeedPresentation (
2394+ reasoningAgainFeed ,
2395+ thread . latestTurn ,
2396+ new Set ( ) ,
2397+ new Set ( ) ,
2398+ "now" ,
2399+ ) ;
2400+ expect ( reasoningAgain . filter ( ( row ) => row . id === "live-activity-row" ) ) . toMatchObject ( [
2401+ { type : "work-toggle" , summary : "Thinking" , hiddenCount : 6 } ,
2402+ ] ) ;
2403+ expect ( reasoningAgain ) . toHaveLength ( 1 ) ;
2404+ const expandedLive = deriveThreadFeedPresentation (
2405+ reasoningAgainFeed ,
2406+ thread . latestTurn ,
2407+ new Set ( ) ,
2408+ new Set ( [ `activity-run:${ messages [ 0 ] ! . id } ` ] ) ,
2409+ "now" ,
2410+ ) ;
2411+ expect ( expandedLive . map ( ( entry ) => entry . type ) ) . toEqual ( [
2412+ "work-toggle" ,
2413+ "message" ,
2414+ "activity-group" ,
2415+ "message" ,
2416+ ] ) ;
2417+ expect ( expandedLive [ 1 ] ) . toMatchObject ( { reasoningMessages : messages } ) ;
2418+ expect ( expandedLive [ 3 ] ) . toMatchObject ( { message : nextThought } ) ;
2419+ expect ( deriveThreadFeedPresentation ( feedWithWork , settledTurn , new Set ( ) ) ) . toMatchObject ( [
2420+ { type : "turn-fold" , expanded : false } ,
2421+ ] ) ;
2422+ const reopened = deriveThreadFeedPresentation (
2423+ feedWithWork ,
2424+ settledTurn ,
2425+ new Set ( [ turnId ] ) ,
2426+ new Set ( [ `activity-run:${ messages [ 0 ] ! . id } ` ] ) ,
2427+ ) ;
2428+ expect ( reopened . map ( ( entry ) => entry . type ) ) . toEqual ( [
2429+ "turn-fold" ,
2430+ "work-toggle" ,
2431+ "message" ,
2432+ "activity-group" ,
2433+ ] ) ;
2434+ expect ( reopened [ 2 ] ) . toMatchObject ( { id : messages [ 0 ] ! . id , reasoningMessages : messages } ) ;
2435+ const toolFirstFeed = feedWithWork . filter ( ( entry ) => entry . type === "activity-group" ) ;
2436+ const toolFirst = deriveThreadFeedPresentation (
2437+ toolFirstFeed ,
2438+ thread . latestTurn ,
2439+ new Set ( ) ,
2440+ new Set ( ) ,
2441+ "now" ,
2442+ ) [ 0 ] ;
2443+ expect ( toolFirst ?. type ) . toBe ( "work-toggle" ) ;
2444+ if ( toolFirst ?. type !== "work-toggle" ) return ;
2445+ const preservedExpansion = deriveThreadFeedPresentation (
2446+ [ ...toolFirstFeed , reasoningAgainFeed . at ( - 1 ) ! ] ,
2447+ thread . latestTurn ,
2448+ new Set ( ) ,
2449+ new Set ( [ toolFirst . groupId ] ) ,
2450+ "now" ,
2451+ ) ;
2452+ expect ( preservedExpansion . map ( ( entry ) => entry . type ) ) . toEqual ( [
2453+ "work-toggle" ,
2454+ "activity-group" ,
2455+ "message" ,
2456+ ] ) ;
2457+ expect ( preservedExpansion [ 0 ] ) . toMatchObject ( {
2458+ id : "live-activity-row" ,
2459+ groupId : toolFirst . groupId ,
2460+ expanded : true ,
2461+ summary : "Thinking" ,
2462+ } ) ;
2463+ } ) ;
2464+
2465+ it . each ( [ "tool" , "failed-tool" , "assistant" , "turn" , "unknown-turn" ] as const ) (
2466+ "preserves a %s boundary in expanded activity history" ,
2467+ ( boundary ) => {
2468+ const turnId = TurnId . make ( "reasoning-boundary" ) ;
2469+ const messages : OrchestrationThread [ "messages" ] = [ 1 , 3 ] . map ( ( second ) => ( {
2470+ id : MessageId . make ( `reasoning-${ second } ` ) ,
2471+ role : "reasoning" ,
2472+ text : `Step ${ second } ` ,
2473+ turnId :
2474+ boundary === "unknown-turn"
2475+ ? null
2476+ : boundary === "turn" && second === 3
2477+ ? TurnId . make ( "other-turn" )
2478+ : turnId ,
2479+ streaming : false ,
2480+ createdAt : `2026-04-01T00:00:0${ second } .000Z` ,
2481+ updatedAt : `2026-04-01T00:00:0${ second } .000Z` ,
2482+ } ) ) ;
2483+ const thread = makeThread ( {
2484+ id : ThreadId . make ( "reasoning-boundary" ) ,
2485+ projectId : ProjectId . make ( "project-1" ) ,
2486+ title : "Reasoning" ,
2487+ messages :
2488+ boundary === "assistant"
2489+ ? [
2490+ messages [ 0 ] ! ,
2491+ {
2492+ ...messages [ 0 ] ! ,
2493+ id : MessageId . make ( "assistant-between" ) ,
2494+ role : "assistant" ,
2495+ text : "Checking the next file." ,
2496+ createdAt : "2026-04-01T00:00:02.000Z" ,
2497+ } ,
2498+ messages [ 1 ] ! ,
2499+ ]
2500+ : messages ,
2501+ activities :
2502+ boundary === "tool" || boundary === "failed-tool"
2503+ ? [
2504+ makeActivity ( {
2505+ id : EventId . make ( "tool-between" ) ,
2506+ kind : "tool.completed" ,
2507+ tone : "tool" ,
2508+ summary : "Read files" ,
2509+ createdAt : "2026-04-01T00:00:02.000Z" ,
2510+ turnId,
2511+ payload : {
2512+ itemType : "file_read" ,
2513+ status : boundary === "failed-tool" ? "failed" : "completed" ,
2514+ } ,
2515+ } ) ,
2516+ ]
2517+ : [ ] ,
2518+ } ) ;
2519+ const rows = deriveThreadFeedPresentation (
2520+ buildThreadFeed ( thread ) ,
2521+ null ,
2522+ new Set ( [ turnId ] ) ,
2523+ new Set ( messages . map ( ( message ) => `activity-run:${ message . id } ` ) ) ,
2524+ ) ;
2525+ const reasoningRows = rows . filter (
2526+ ( entry ) => entry . type === "message" && entry . message . role === "reasoning" ,
2527+ ) ;
2528+ if ( boundary === "failed-tool" ) {
2529+ expect ( rows . filter ( ( entry ) => entry . type === "work-toggle" ) ) . toHaveLength ( 3 ) ;
2530+ expect ( rows . some ( ( entry ) => entry . type === "work-toggle" && entry . hasFailure ) ) . toBe ( true ) ;
2531+ }
2532+ expect ( reasoningRows ) . toEqual (
2533+ messages . map ( ( message ) => ( {
2534+ type : "message" ,
2535+ id : message . id ,
2536+ createdAt : message . createdAt ,
2537+ message,
2538+ } ) ) ,
2539+ ) ;
2540+ } ,
2541+ ) ;
2542+
22982543 it ( "shows one Thinking row while a turn works without live tool activity" , ( ) => {
22992544 const turnId = TurnId . make ( "turn-thinking" ) ;
23002545 const latestTurn = {
0 commit comments