Skip to content

Commit c33d7e4

Browse files
committed
Python: fix(ag-ui): keep one-chunk annotated duplicates like unannotated ones
A duplicate equal to the whole accumulated text was still suppressed after a single prior chunk whenever its annotations re-sent the recorded occurrences, which dropped a genuine repeated token carrying the same citation. Suppression now needs more than one prior chunk for annotated duplicates too, matching the unannotated policy from the issue. The genuine path emits the delta with its annotations, spans rebased to the new chunk position, and the existing occurrence reconciliation still applies on the suppression path. Follow-up to the review on #9339. Signed-off-by: Manohar Paturi <[email protected]>
1 parent 6f1f174 commit c33d7e4

2 files changed

Lines changed: 56 additions & 42 deletions

File tree

‎python/packages/ag-ui/agent_framework_ag_ui/_run_common.py‎

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -581,33 +581,6 @@ def _text_segment_for(flow: FlowState, message_id: str) -> dict[str, Any] | None
581581
return None
582582

583583

584-
def _annotations_replay_recorded_occurrences(flow: FlowState, annotations: Any) -> bool:
585-
"""Whether an equal-text duplicate's annotations re-send every recorded occurrence.
586-
587-
A cumulative replay re-sends the annotations the message already carries; a genuine
588-
repeated delta brings new annotations. Comparing against the segment's recorded
589-
annotations separates the two without guessing from the delta alone.
590-
"""
591-
if not annotations:
592-
return False
593-
segment = _text_segment_for(flow, flow.message_id) if flow.message_id else None
594-
if segment is None:
595-
return False
596-
recorded = list(segment.get("annotations", []))
597-
if not recorded:
598-
return False
599-
incoming = cast(
600-
"list[dict[str, Any]]",
601-
make_json_safe(
602-
[
603-
{key: value for key, value in annotation.items() if key != "raw_representation"}
604-
for annotation in annotations
605-
]
606-
),
607-
)
608-
return all(annotation in incoming for annotation in recorded)
609-
610-
611584
def _new_tool_call_segment_id(flow: FlowState) -> str:
612585
"""Allocate a snapshot ID, reusing a tool-only opening ID at most once."""
613586
segment_ids = {segment.get("id") for segment in flow.snapshot_segments if segment.get("id")}
@@ -676,14 +649,14 @@ def _emit_text(content: Content, flow: FlowState, skip_text: bool = False) -> li
676649
_open_text_segment(flow, flow.message_id)
677650
events.append(TextMessageStartEvent(message_id=flow.message_id, role="assistant"))
678651
elif flow.accumulated_text and content.text == flow.accumulated_text and (
679-
flow.text_delta_count > 1 or _annotations_replay_recorded_occurrences(flow, content.annotations)
652+
flow.text_delta_count > 1
680653
):
681654
# Guard against full-message replay chunks that can appear after streaming deltas.
682655
# A duplicate equal to the whole accumulated text is only a safe replay signal when
683-
# the text already streamed in more than one chunk, or when its annotations are
684-
# cumulative: a replay re-sends the annotations already recorded for the message,
685-
# while a genuine repeated delta carries new ones. Otherwise a single prior chunk
686-
# leaves the two indistinguishable and dropping it would lose text (issue #9336).
656+
# the text already streamed in more than one chunk. With a single prior chunk the
657+
# duplicate is indistinguishable from a genuine repeated token, annotated or not,
658+
# so the one-chunk policy stays the same as the unannotated path: keep the text
659+
# (issue #9336) and let the annotation emission reconcile occurrences.
687660
logger.debug("Skipping duplicate full-text delta for message_id=%s", flow.message_id)
688661
if not content.annotations:
689662
return []

‎python/packages/ag-ui/tests/ag_ui/test_run.py‎

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,12 @@ def test_emit_text_annotation_only_update_starts_a_message_without_an_empty_delt
788788

789789

790790
def 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

807816
def 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

38433856
def 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

Comments
 (0)