@@ -59,6 +59,14 @@ import {
5959 resolveSendEnvMode ,
6060 threadShellHasStarted ,
6161 resolveDraftHeroState ,
62+ isPaintOnlyThreadTimeline ,
63+ peekHeldThreadTimeline ,
64+ peekRememberedThreadTimeline ,
65+ rememberReadyThreadTimeline ,
66+ resetHeldThreadTimeline ,
67+ resolveThreadSwitchTimeline ,
68+ threadKeysShareEnvironment ,
69+ timelineHasEphemeralPreviewUrls ,
6270 scheduleEnvironmentReconnectWarning ,
6371 startNewThreadForProject ,
6472 codexArtifactTemplatePromptToAppend ,
@@ -565,6 +573,179 @@ describe("draft hero submission transition", () => {
565573 } ) ;
566574} ) ;
567575
576+ describe ( "resolveThreadSwitchTimeline" , ( ) => {
577+ afterEach ( ( ) => {
578+ resetHeldThreadTimeline ( ) ;
579+ } ) ;
580+
581+ const held = { threadKey : "env-1:thread-a" , entries : [ "a1" , "a2" ] } ;
582+
583+ it ( "keeps the previous thread's entries while the next thread is loading" , ( ) => {
584+ expect (
585+ resolveThreadSwitchTimeline ( {
586+ loading : true ,
587+ activeThreadKey : "env-1:thread-b" ,
588+ nextEntries : [ ] ,
589+ lastReady : held ,
590+ } ) ,
591+ ) . toEqual ( { entries : [ "a1" , "a2" ] , displayThreadKey : "env-1:thread-a" } ) ;
592+ } ) ;
593+
594+ it ( "shows the new thread once its detail is ready" , ( ) => {
595+ expect (
596+ resolveThreadSwitchTimeline ( {
597+ loading : false ,
598+ activeThreadKey : "env-1:thread-b" ,
599+ nextEntries : [ "b1" ] ,
600+ lastReady : held ,
601+ } ) ,
602+ ) . toEqual ( { entries : [ "b1" ] , displayThreadKey : "env-1:thread-b" } ) ;
603+ } ) ;
604+
605+ it ( "does not invent a timeline on the first open of a thread" , ( ) => {
606+ expect (
607+ resolveThreadSwitchTimeline ( {
608+ loading : true ,
609+ activeThreadKey : "env-1:thread-a" ,
610+ nextEntries : [ ] ,
611+ lastReady : null ,
612+ } ) ,
613+ ) . toEqual ( { entries : [ ] , displayThreadKey : "env-1:thread-a" } ) ;
614+ } ) ;
615+
616+ it ( "keeps the held thread workspace cwd with the snapshot" , ( ) => {
617+ rememberReadyThreadTimeline ( {
618+ ...held ,
619+ markdownCwd : "/repo/a" ,
620+ workspaceRoot : "/repo/a" ,
621+ } ) ;
622+ expect ( peekHeldThreadTimeline < string [ ] > ( ) ) . toEqual ( {
623+ ...held ,
624+ markdownCwd : "/repo/a" ,
625+ workspaceRoot : "/repo/a" ,
626+ } ) ;
627+ } ) ;
628+
629+ it ( "survives a ChatView remount by remembering the last ready timeline" , ( ) => {
630+ rememberReadyThreadTimeline ( held ) ;
631+ expect ( peekHeldThreadTimeline < string [ ] > ( ) ) . toEqual ( held ) ;
632+ expect (
633+ resolveThreadSwitchTimeline ( {
634+ loading : true ,
635+ activeThreadKey : "env-1:thread-b" ,
636+ nextEntries : [ ] ,
637+ } ) ,
638+ ) . toEqual ( { entries : [ "a1" , "a2" ] , displayThreadKey : "env-1:thread-a" } ) ;
639+ } ) ;
640+
641+ it ( "paints a remembered destination instead of the last-viewed thread" , ( ) => {
642+ rememberReadyThreadTimeline ( held ) ;
643+ rememberReadyThreadTimeline ( { threadKey : "env-1:thread-b" , entries : [ "b1" , "b2" ] } ) ;
644+ expect ( peekRememberedThreadTimeline < string [ ] > ( "env-1:thread-a" ) ) . toEqual ( [ "a1" , "a2" ] ) ;
645+ expect (
646+ resolveThreadSwitchTimeline ( {
647+ loading : true ,
648+ activeThreadKey : "env-1:thread-a" ,
649+ nextEntries : [ ] ,
650+ } ) ,
651+ ) . toEqual ( { entries : [ "a1" , "a2" ] , displayThreadKey : "env-1:thread-a" } ) ;
652+ } ) ;
653+
654+ it ( "prefers live entries over a remembered snapshot" , ( ) => {
655+ rememberReadyThreadTimeline ( { threadKey : "env-1:thread-b" , entries : [ "stale-b" ] } ) ;
656+ expect (
657+ resolveThreadSwitchTimeline ( {
658+ loading : false ,
659+ activeThreadKey : "env-1:thread-b" ,
660+ nextEntries : [ "fresh-b" ] ,
661+ } ) ,
662+ ) . toEqual ( { entries : [ "fresh-b" ] , displayThreadKey : "env-1:thread-b" } ) ;
663+ } ) ;
664+
665+ it ( "does not keep a remembered snapshot on a resolved empty thread" , ( ) => {
666+ rememberReadyThreadTimeline ( held ) ;
667+ expect (
668+ resolveThreadSwitchTimeline ( {
669+ loading : false ,
670+ activeThreadKey : "env-1:thread-a" ,
671+ nextEntries : [ ] ,
672+ } ) ,
673+ ) . toEqual ( { entries : [ ] , displayThreadKey : "env-1:thread-a" } ) ;
674+ } ) ;
675+
676+ it ( "does not hold another environment's timeline across a jump" , ( ) => {
677+ expect ( threadKeysShareEnvironment ( "env-1:thread-a" , "env-2:thread-b" ) ) . toBe ( false ) ;
678+ expect (
679+ resolveThreadSwitchTimeline ( {
680+ loading : true ,
681+ activeThreadKey : "env-2:thread-b" ,
682+ nextEntries : [ ] ,
683+ lastReady : held ,
684+ } ) ,
685+ ) . toEqual ( { entries : [ ] , displayThreadKey : "env-2:thread-b" } ) ;
686+ } ) ;
687+
688+ it ( "treats a foreign held timeline as paint-only" , ( ) => {
689+ expect ( isPaintOnlyThreadTimeline ( "env-1:thread-a" , "env-1:thread-b" ) ) . toBe ( true ) ;
690+ expect ( isPaintOnlyThreadTimeline ( "env-1:thread-b" , "env-1:thread-b" ) ) . toBe ( false ) ;
691+ } ) ;
692+
693+ it ( "does not remember a timeline that still has handoff blob previews" , ( ) => {
694+ expect (
695+ timelineHasEphemeralPreviewUrls ( [
696+ {
697+ kind : "message" ,
698+ message : {
699+ id : MessageId . make ( "preview-message" ) ,
700+ role : "user" ,
701+ text : "Preview" ,
702+ turnId : null ,
703+ streaming : false ,
704+ createdAt : "2026-09-10T12:00:00.000Z" ,
705+ updatedAt : "2026-09-10T12:00:00.000Z" ,
706+ attachments : [
707+ {
708+ type : "image" ,
709+ id : "preview" ,
710+ name : "preview.png" ,
711+ mimeType : "image/png" ,
712+ sizeBytes : 1 ,
713+ previewUrl : "blob:handoff" ,
714+ } ,
715+ ] ,
716+ } ,
717+ } ,
718+ ] ) ,
719+ ) . toBe ( true ) ;
720+ expect (
721+ timelineHasEphemeralPreviewUrls ( [
722+ {
723+ kind : "message" ,
724+ message : {
725+ id : MessageId . make ( "preview-message" ) ,
726+ role : "user" ,
727+ text : "Preview" ,
728+ turnId : null ,
729+ streaming : false ,
730+ createdAt : "2026-09-10T12:00:00.000Z" ,
731+ updatedAt : "2026-09-10T12:00:00.000Z" ,
732+ attachments : [
733+ {
734+ type : "image" ,
735+ id : "preview" ,
736+ name : "preview.png" ,
737+ mimeType : "image/png" ,
738+ sizeBytes : 1 ,
739+ previewUrl : "https://cdn.example/a.png" ,
740+ } ,
741+ ] ,
742+ } ,
743+ } ,
744+ ] ) ,
745+ ) . toBe ( false ) ;
746+ } ) ;
747+ } ) ;
748+
568749describe ( "shouldReleaseTimelineAnchorForToolActivity" , ( ) => {
569750 const activeTurnId = TurnId . make ( "active-turn" ) ;
570751 const anchorMessageId = MessageId . make ( "anchored-message" ) ;
0 commit comments