@@ -562,6 +562,23 @@ describe("multiAgentGuidanceText", () => {
562562} ) ;
563563
564564describe ( "injectDeveloperMessage" , ( ) => {
565+ const guidance = "guidance text" ;
566+ const generatedItem = ( text = guidance ) => ( {
567+ type : "message" ,
568+ role : "developer" ,
569+ content : [ { type : "input_text" , text } ] ,
570+ } ) ;
571+ const countExact = ( input : unknown [ ] , text = guidance ) : number => input . filter ( item => {
572+ if ( ! item || typeof item !== "object" || Array . isArray ( item ) ) return false ;
573+ const record = item as Record < string , unknown > ;
574+ if ( record . type !== "message" || record . role !== "developer" || ! Array . isArray ( record . content ) ) return false ;
575+ if ( record . content . length !== 1 ) return false ;
576+ const part = record . content [ 0 ] ;
577+ return ! ! part && typeof part === "object" && ! Array . isArray ( part )
578+ && ( part as Record < string , unknown > ) . type === "input_text"
579+ && ( part as Record < string , unknown > ) . text === text ;
580+ } ) . length ;
581+
565582 test ( "appends to both the parsed messages and the raw passthrough input" , ( ) => {
566583 const parsed = parsedFixture ( { reasoning : "max" } ) ;
567584 injectDeveloperMessage ( parsed , "hello there" ) ;
@@ -597,6 +614,51 @@ describe("injectDeveloperMessage", () => {
597614 expect ( ( input [ 1 ] as { role : string } ) . role ) . toBe ( "developer" ) ;
598615 expect ( ( input [ 2 ] as { type : string } ) . type ) . toBe ( "compaction_trigger" ) ;
599616 } ) ;
617+
618+ test ( "exact-guidance predicate rejects every near-match replay-prefix shape (#326)" , ( ) => {
619+ const nearMatches : Array < [ string , unknown ] > = [
620+ [ "non-record item" , null ] ,
621+ [ "wrong type" , { ...generatedItem ( ) , type : "other" } ] ,
622+ [ "wrong role" , { ...generatedItem ( ) , role : "user" } ] ,
623+ [ "non-array content" , { type : "message" , role : "developer" , content : "guidance text" } ] ,
624+ [ "wrong content length" , { type : "message" , role : "developer" , content : [ ] } ] ,
625+ [ "non-record part" , { type : "message" , role : "developer" , content : [ null ] } ] ,
626+ [ "wrong part type" , { type : "message" , role : "developer" , content : [ { type : "output_text" , text : guidance } ] } ] ,
627+ [ "different text" , generatedItem ( "different guidance" ) ] ,
628+ [ "extra content part" , {
629+ type : "message" ,
630+ role : "developer" ,
631+ content : [ { type : "input_text" , text : guidance } , { type : "input_text" , text : "extra" } ] ,
632+ } ] ,
633+ ] ;
634+
635+ for ( const [ label , nearMatch ] of nearMatches ) {
636+ const parsed = parsedFixture ( { reasoning : "max" , rawInput : [ nearMatch ] } ) ;
637+ parsed . _replayPrefixLen = 1 ;
638+ injectDeveloperMessage ( parsed , guidance ) ;
639+ const rawInput = ( parsed . _rawBody as { input : unknown [ ] } ) . input ;
640+ expect ( countExact ( rawInput ) , label ) . toBe ( 1 ) ;
641+ expect ( parsed . context . messages . filter ( message => message . role === "developer" && message . content === guidance ) , label )
642+ . toHaveLength ( 1 ) ;
643+ }
644+ } ) ;
645+
646+ test ( "matching guidance in the current suffix does not suppress replay-prefix injection (#326)" , ( ) => {
647+ const parsed = parsedFixture ( {
648+ reasoning : "max" ,
649+ rawInput : [
650+ { type : "message" , role : "user" , content : "replayed" } ,
651+ generatedItem ( ) ,
652+ ] ,
653+ } ) ;
654+ parsed . _replayPrefixLen = 1 ;
655+ injectDeveloperMessage ( parsed , guidance ) ;
656+
657+ const rawInput = ( parsed . _rawBody as { input : unknown [ ] } ) . input ;
658+ expect ( countExact ( rawInput ) ) . toBe ( 2 ) ;
659+ expect ( parsed . context . messages . filter ( message => message . role === "developer" && message . content === guidance ) )
660+ . toHaveLength ( 1 ) ;
661+ } ) ;
600662} ) ;
601663
602664describe ( "sanitizeEncryptedContentInPlace" , ( ) => {
0 commit comments