@@ -785,6 +785,123 @@ describe("ProviderCommandReactor", () => {
785785 expect ( thread ?. titleRegeneration ) . toBeNull ( ) ;
786786 } ) ;
787787
788+ it ( "pins the first user message when regeneration context is truncated" , async ( ) => {
789+ const harness = await createHarness ( ) ;
790+ const now = "2026-01-01T00:00:00.000Z" ;
791+ const firstUserMessage = `Review subagent monitoring risks. ${ "Opening context. " . repeat ( 200 ) } ` ;
792+ const recentUserMessage = `LATEST FINDING: ${ "implementation detail " . repeat ( 320 ) } ` ;
793+ harness . generateThreadTitle . mockReturnValue (
794+ Effect . succeed ( { title : "Review subagent monitoring risks" } ) ,
795+ ) ;
796+
797+ await harness . runEffect (
798+ harness . engine . dispatch ( {
799+ type : "thread.meta.update" ,
800+ commandId : CommandId . make ( "cmd-thread-title-existing-long" ) ,
801+ threadId : ThreadId . make ( "thread-1" ) ,
802+ title : "Generic PR review" ,
803+ } ) ,
804+ ) ;
805+ await harness . runEffect (
806+ harness . engine . dispatch ( {
807+ type : "thread.turn.start" ,
808+ commandId : CommandId . make ( "cmd-turn-start-before-long-title-regeneration" ) ,
809+ threadId : ThreadId . make ( "thread-1" ) ,
810+ message : {
811+ messageId : asMessageId ( "user-message-before-long-title-regeneration" ) ,
812+ role : "user" ,
813+ text : firstUserMessage ,
814+ attachments : [
815+ {
816+ type : "image" ,
817+ id : "opening-context-image" ,
818+ name : "image.png" ,
819+ mimeType : "image/png" ,
820+ sizeBytes : 5 ,
821+ } ,
822+ ] ,
823+ } ,
824+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
825+ runtimeMode : "approval-required" ,
826+ createdAt : now ,
827+ } ) ,
828+ ) ;
829+ await harness . runEffect (
830+ harness . engine . dispatch ( {
831+ type : "thread.turn.start" ,
832+ commandId : CommandId . make ( "cmd-middle-turn-before-long-title-regeneration" ) ,
833+ threadId : ThreadId . make ( "thread-1" ) ,
834+ message : {
835+ messageId : asMessageId ( "middle-message-before-long-title-regeneration" ) ,
836+ role : "user" ,
837+ text : "Temporary handoff details." ,
838+ attachments : [
839+ {
840+ type : "image" ,
841+ id : "middle-context-image" ,
842+ name : "image.png" ,
843+ mimeType : "image/png" ,
844+ sizeBytes : 5 ,
845+ } ,
846+ ] ,
847+ } ,
848+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
849+ runtimeMode : "approval-required" ,
850+ createdAt : "2026-01-01T00:00:01.000Z" ,
851+ } ) ,
852+ ) ;
853+ await harness . runEffect (
854+ harness . engine . dispatch ( {
855+ type : "thread.turn.start" ,
856+ commandId : CommandId . make ( "cmd-recent-turn-before-long-title-regeneration" ) ,
857+ threadId : ThreadId . make ( "thread-1" ) ,
858+ message : {
859+ messageId : asMessageId ( "recent-message-before-long-title-regeneration" ) ,
860+ role : "user" ,
861+ text : recentUserMessage ,
862+ attachments : [
863+ {
864+ type : "image" ,
865+ id : "recent-context-image" ,
866+ name : "image.png" ,
867+ mimeType : "image/png" ,
868+ sizeBytes : 5 ,
869+ } ,
870+ ] ,
871+ } ,
872+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
873+ runtimeMode : "approval-required" ,
874+ createdAt : "2026-01-01T00:00:02.000Z" ,
875+ } ) ,
876+ ) ;
877+ await harness . runEffect (
878+ harness . engine . dispatch ( {
879+ type : "thread.meta.update" ,
880+ commandId : CommandId . make ( "cmd-thread-title-regenerate-long" ) ,
881+ threadId : ThreadId . make ( "thread-1" ) ,
882+ regenerateTitle : true ,
883+ } ) ,
884+ ) ;
885+
886+ await harness . drain ( ) ;
887+
888+ expect ( harness . generateThreadTitle ) . toHaveBeenCalledTimes ( 1 ) ;
889+ const input = harness . generateThreadTitle . mock . calls [ 0 ] ?. [ 0 ] ;
890+ if ( ! input ) {
891+ throw new Error ( "Expected a title generation input" ) ;
892+ }
893+ const message = input . message ;
894+ expect ( message . startsWith ( "USER:\nReview subagent monitoring risks." ) ) . toBe ( true ) ;
895+ expect ( message ) . toContain ( "[First user message truncated]" ) ;
896+ expect ( message ) . toContain ( "[Earlier content truncated]" ) ;
897+ expect ( message ) . toContain ( "image.png" ) ;
898+ expect ( message ) . toHaveLength ( 8_000 ) ;
899+ expect ( input . attachments ?. map ( ( attachment ) => attachment . id ) ) . toEqual ( [
900+ "opening-context-image" ,
901+ "recent-context-image" ,
902+ ] ) ;
903+ } ) ;
904+
788905 it ( "clears title regeneration state left pending across reactor startup" , async ( ) => {
789906 const harness = await createHarness ( {
790907 titleRegenerationBeforeStart : "one" ,
@@ -972,10 +1089,14 @@ describe("ProviderCommandReactor", () => {
9721089 expect ( thread ?. titleRegeneration ) . toBeNull ( ) ;
9731090 } ) ;
9741091
975- it ( "keeps the full retained context and excludes attachments outside it " , async ( ) => {
1092+ it ( "pins the first user context and attachment before the retained tail " , async ( ) => {
9761093 const harness = await createHarness ( ) ;
9771094 const now = "2026-01-01T00:00:00.000Z" ;
978- const retainedContext = "x" . repeat ( 8_000 ) ;
1095+ const firstUserContext = "USER:\nOld visual issue\n[Attachments: old-issue.png]" ;
1096+ const truncationMarker = "[Earlier content truncated]\n\n" ;
1097+ const retainedContext = "x" . repeat (
1098+ 8_000 - firstUserContext . length - "\n\n" . length - truncationMarker . length ,
1099+ ) ;
9791100
9801101 await harness . runEffect (
9811102 harness . engine . dispatch ( {
@@ -1040,9 +1161,14 @@ describe("ProviderCommandReactor", () => {
10401161 await harness . drain ( ) ;
10411162
10421163 expect ( harness . generateThreadTitle . mock . calls [ 0 ] ?. [ 0 ] . message ) . toBe (
1043- `[Earlier content truncated] \n\n${ retainedContext } ` ,
1164+ `${ firstUserContext } \n\n${ truncationMarker } ${ retainedContext } ` ,
10441165 ) ;
1045- expect ( harness . generateThreadTitle . mock . calls [ 0 ] ?. [ 0 ] . attachments ) . toBeUndefined ( ) ;
1166+ expect ( harness . generateThreadTitle . mock . calls [ 0 ] ?. [ 0 ] . attachments ) . toEqual ( [
1167+ expect . objectContaining ( {
1168+ id : "old-title-context-image" ,
1169+ name : "old-issue.png" ,
1170+ } ) ,
1171+ ] ) ;
10461172 } ) ;
10471173
10481174 it ( "does not overwrite a manual rename while title regeneration is running" , async ( ) => {
0 commit comments