@@ -788,7 +788,12 @@ def test_emit_text_annotation_only_update_starts_a_message_without_an_empty_delt
788788
789789
790790def test_emit_text_replay_emits_only_new_annotations () -> None :
791- """Suppress repeated text and citations without dropping final-only citations."""
791+ """A one-chunk duplicate keeps its text and emits only new citation occurrences.
792+
793+ After a single streamed chunk a duplicate equal to the whole text is treated as a
794+ genuine delta, matching the unannotated policy from issue #9336. The annotation
795+ emission still reconciles occurrences, so a re-sent citation is not duplicated.
796+ """
792797 flow = FlowState ()
793798 first = Annotation (type = "citation" , url = "https://example.com/first" )
794799 second = Annotation (type = "citation" , url = "https://example.com/second" )
@@ -797,11 +802,15 @@ def test_emit_text_replay_emits_only_new_annotations() -> None:
797802 events = _emit_text (Content .from_text ("Answer" , annotations = [first , second ]), flow )
798803 replay_events = _emit_text (Content .from_text ("Answer" , annotations = [first , second ]), flow )
799804
800- assert len (events ) == 1
801- assert isinstance (events [0 ], CustomEvent )
802- assert events [0 ].value == {"messageId" : flow .message_id , "annotations" : [second ]}
803- assert replay_events == []
804- assert flow .accumulated_text == "Answer"
805+ text_events = [event for event in events if isinstance (event , TextMessageContentEvent )]
806+ citation_events = [event for event in events if isinstance (event , CustomEvent )]
807+ assert [event .delta for event in text_events ] == ["Answer" ]
808+ assert [event .value ["annotations" ] for event in citation_events ] == [[first , second ]]
809+ replay_text_events = [event for event in replay_events if isinstance (event , TextMessageContentEvent )]
810+ assert [event .delta for event in replay_text_events ] == ["Answer" ]
811+ replay_citation_events = [event for event in replay_events if isinstance (event , CustomEvent )]
812+ assert [event .value ["annotations" ] for event in replay_citation_events ] == [[first , second ]]
813+ assert flow .accumulated_text == "AnswerAnswerAnswer"
805814
806815
807816def test_emit_text_preserves_identical_annotation_occurrences () -> None :
@@ -816,7 +825,11 @@ def test_emit_text_preserves_identical_annotation_occurrences() -> None:
816825
817826 assert len (first_events ) == len (second_events ) == 1
818827 assert all (isinstance (event , CustomEvent ) for event in [* first_events , * second_events ])
819- assert replay_events == []
828+ # One prior text chunk: the duplicate streams as genuine text with its annotations.
829+ replay_text_events = [event for event in replay_events if isinstance (event , TextMessageContentEvent )]
830+ assert [event .delta for event in replay_text_events ] == ["Answer" ]
831+ replay_citation_events = [event for event in replay_events if isinstance (event , CustomEvent )]
832+ assert [event .value ["annotations" ] for event in replay_citation_events ] == [[annotation , annotation ]]
820833
821834
822835@pytest .mark .parametrize ("suppression" , ["skip_text" , "waiting_for_approval" ])
@@ -3841,7 +3854,11 @@ def test_emit_text_keeps_repeated_annotated_single_chunk_delta() -> None:
38413854
38423855
38433856def test_emit_text_suppresses_cumulative_annotated_replay_after_one_chunk () -> None :
3844- """An equal delta whose annotations re-send the recorded set is still a replay."""
3857+ """A one-chunk cumulative duplicate keeps its text and emits only the new citation.
3858+
3859+ Suppression of an equal duplicate needs more than one prior chunk; with one chunk
3860+ the policy matches the unannotated path and the text streams as a genuine delta.
3861+ """
38453862 flow = FlowState ()
38463863 first_citation = Annotation (type = "citation" , url = "https://example.com/first" )
38473864 _emit_text (Content .from_text ("Answer" , annotations = [first_citation ]), flow )
@@ -3857,7 +3874,31 @@ def test_emit_text_suppresses_cumulative_annotated_replay_after_one_chunk() -> N
38573874 flow ,
38583875 )
38593876
3860- assert flow .accumulated_text == "Answer"
3877+ assert flow .accumulated_text == "AnswerAnswer"
3878+ text_events = [event for event in replay if isinstance (event , TextMessageContentEvent )]
3879+ assert [event .delta for event in text_events ] == ["Answer" ]
38613880 citation_events = [event for event in replay if isinstance (event , CustomEvent )]
38623881 assert len (citation_events ) == 1
3863- assert citation_events [0 ].value ["annotations" ][0 ]["url" ] == "https://example.com/second"
3882+ assert [a ["url" ] for a in citation_events [0 ].value ["annotations" ]] == [
3883+ "https://example.com/first" ,
3884+ "https://example.com/second" ,
3885+ ]
3886+
3887+ # A later multi-chunk message still suppresses full-text replays.
3888+ multi_flow = FlowState ()
3889+ _emit_text (Content .from_text ("Pre " ), multi_flow )
3890+ _emit_text (Content .from_text ("fix" ), multi_flow )
3891+ replay_events = _emit_text (Content .from_text ("Pre fix" ), multi_flow )
3892+ assert replay_events == []
3893+ assert multi_flow .accumulated_text == "Pre fix"
3894+
3895+ # The exact case from the review: two genuine identical annotated tokens.
3896+ same_flow = FlowState ()
3897+ same = Annotation (type = "citation" , url = "https://example.com/same" )
3898+ _emit_text (Content .from_text ("ha" , annotations = [same ]), same_flow )
3899+ repeat = _emit_text (Content .from_text ("ha" , annotations = [same ]), same_flow )
3900+ assert same_flow .accumulated_text == "haha"
3901+ text_events = [event for event in repeat if isinstance (event , TextMessageContentEvent )]
3902+ assert [event .delta for event in text_events ] == ["ha" ]
3903+ repeat_citations = [event for event in repeat if isinstance (event , CustomEvent )]
3904+ assert [event .value ["annotations" ] for event in repeat_citations ] == [[same ]]
0 commit comments