@@ -15,7 +15,7 @@ import {
1515 chatItemStartsUserTurn ,
1616 safeNormalizeMessage ,
1717} from "./chat-turn-boundary.ts" ;
18- import { persistedSteerTargetRunId } from "./stream-causal-boundary.ts" ;
18+ import { indexTurnContinuations } from "./stream-causal-boundary.ts" ;
1919
2020export function isKeyedAssistantStreamFallbackMessage ( message : unknown ) : boolean {
2121 const record = asRecord ( message ) ;
@@ -554,18 +554,17 @@ function isFinalReplyGroup(item: TurnRenderItem): boolean {
554554 return item . kind === "group" && ! item . isStreaming && assistantGroupCanOwnActiveRunStatus ( item ) ;
555555}
556556
557- function turnStartsWithSteer ( turn : TurnRenderItem [ ] ) : boolean {
557+ function turnUserMessages ( turn : TurnRenderItem [ ] ) : unknown [ ] {
558558 const boundary = turn [ 0 ] ;
559559 if ( ! boundary || boundary . kind === "stream-run" ) {
560- return false ;
560+ return [ ] ;
561561 }
562562 if ( boundary . kind === "group" ) {
563- return (
564- boundary . role . toLowerCase ( ) === "user" &&
565- boundary . messages . some ( ( { message } ) => persistedSteerTargetRunId ( message ) !== null )
566- ) ;
563+ return boundary . role . toLowerCase ( ) === "user"
564+ ? boundary . messages . map ( ( { message } ) => message )
565+ : [ ] ;
567566 }
568- return boundary . kind === "message" && persistedSteerTargetRunId ( boundary . message ) !== null ;
567+ return boundary . kind === "message" && chatItemStartsUserTurn ( boundary ) ? [ boundary . message ] : [ ] ;
569568}
570569
571570/**
@@ -605,11 +604,12 @@ export function collapseCompletedTurnWork(
605604 turns . push ( currentTurn ) ;
606605 }
607606
608- const continuesIntoSteer = turns . map ( ( _ , turnIndex ) =>
609- turnStartsWithSteer ( turns [ turnIndex + 1 ] ?? [ ] ) ,
607+ const { continuationTurnIndexes, precedingContinuationTurnIndexes } = indexTurnContinuations (
608+ turns ,
609+ turnUserMessages ,
610610 ) ;
611611 const finalReplyIndexes = turns . map ( ( turn , turnIndex ) => {
612- if ( continuesIntoSteer [ turnIndex ] ) {
612+ if ( continuationTurnIndexes . has ( turnIndex ) ) {
613613 return - 1 ;
614614 }
615615 for ( let index = turn . length - 1 ; index >= 0 ; index -= 1 ) {
@@ -624,14 +624,22 @@ export function collapseCompletedTurnWork(
624624 index >= 0 ? ( turns [ turnIndex ] ?. [ index ] as MessageGroup ) : undefined ,
625625 ) ;
626626 for ( let turnIndex = turns . length - 2 ; turnIndex >= 0 ; turnIndex -= 1 ) {
627- if ( ! terminalReplies [ turnIndex ] && continuesIntoSteer [ turnIndex ] ) {
628- terminalReplies [ turnIndex ] = terminalReplies [ turnIndex + 1 ] ;
627+ const continuationTurnIndex = continuationTurnIndexes . get ( turnIndex ) ;
628+ if ( ! terminalReplies [ turnIndex ] && continuationTurnIndex !== undefined ) {
629+ terminalReplies [ turnIndex ] = terminalReplies [ continuationTurnIndex ] ;
629630 }
630631 }
631- let activeRunStartIndex = turns . length - 1 ;
632+ const liveTurnIndexes = new Set < number > ( ) ;
632633 if ( opts . runWorking ) {
633- while ( activeRunStartIndex > 0 && turnStartsWithSteer ( turns [ activeRunStartIndex ] ?? [ ] ) ) {
634- activeRunStartIndex -= 1 ;
634+ let liveTurnIndex = turns . length - 1 ;
635+ liveTurnIndexes . add ( liveTurnIndex ) ;
636+ for ( ; ; ) {
637+ const precedingTurnIndex = precedingContinuationTurnIndexes . get ( liveTurnIndex ) ;
638+ if ( precedingTurnIndex === undefined ) {
639+ break ;
640+ }
641+ liveTurnIndex = precedingTurnIndex ;
642+ liveTurnIndexes . add ( liveTurnIndex ) ;
635643 }
636644 }
637645
@@ -641,7 +649,7 @@ export function collapseCompletedTurnWork(
641649 // While the run works, the trailing turn also stays expanded so activity
642650 // is watchable until the terminal rebuild collapses it.
643651 const isLive =
644- ( opts . runWorking && turnIndex >= activeRunStartIndex ) ||
652+ liveTurnIndexes . has ( turnIndex ) ||
645653 turn . some (
646654 ( item ) => item . kind === "stream-run" || ( item . kind === "group" && item . isStreaming ) ,
647655 ) ;
@@ -683,12 +691,14 @@ export function collapseCompletedTurnWork(
683691 const startTimestamp = boundaryTimestamp == null ? firstGroup . timestamp : boundaryTimestamp ;
684692 const endTimestamp = terminalReply . timestamp ;
685693 const durationMs = endTimestamp > startTimestamp ? endTimestamp - startTimestamp : null ;
686- const nextBoundary = turns [ turnIndex + 1 ] ?. [ 0 ] ;
694+ const continuationBoundary = turns [ continuationTurnIndexes . get ( turnIndex ) ?? - 1 ] ?. [ 0 ] ;
687695 result . push ( ...turn . slice ( 0 , segmentStart ) ) ;
688696 result . push ( {
689697 kind : "work-group" ,
690698 // The final reply survives older-history prepends; the first work row does not.
691- key : `work:${ finalReplyIndex >= 0 || ! nextBoundary ? terminalReply . key : nextBoundary . key } ` ,
699+ key : `work:${
700+ finalReplyIndex >= 0 || ! continuationBoundary ? terminalReply . key : continuationBoundary . key
701+ } `,
692702 groups,
693703 durationMs,
694704 } ) ;
0 commit comments