Skip to content

Commit ca59e87

Browse files
committed
fix(ag-ui): normalize current-turn snapshot deduplication
1 parent 429cae2 commit ca59e87

2 files changed

Lines changed: 148 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ def _append_unique_snapshot_messages(
164164
"""
165165
seen_ids = {message.get("id") for message in existing if message.get("id")}
166166
content_source = content_dedupe_against if content_dedupe_against is not None else []
167-
remaining_content = Counter(_message_content_identity(message) for message in content_source)
167+
# Reserve exact-id overlap before content matching, regardless of incoming order.
168+
# A client turn matched by id must not also consume a distinct identical reply.
169+
matched_ids = seen_ids.intersection(message.get("id") for message in incoming if message.get("id"))
170+
remaining_content = Counter(
171+
_message_content_identity(message) for message in content_source if message.get("id") not in matched_ids
172+
)
168173
merged = list(existing)
169174
for message in incoming:
170175
message_id = message.get("id")
@@ -812,7 +817,7 @@ def append_conversational_responses(values: list[Any]) -> None:
812817
]
813818
snapshot_builder.append_resume_messages(
814819
hitl_messages,
815-
content_dedupe_against=current_turn_client_messages,
820+
content_dedupe_against=agui_messages_to_snapshot_format(current_turn_client_messages),
816821
)
817822

818823
if snapshot_builder is not None and effective_state:

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
from copy import deepcopy
78
from datetime import datetime, timezone
89
from typing import Any, cast
910
from unittest.mock import Mock
@@ -687,6 +688,130 @@ async def test_workflow_hitl_resume_keeps_yes_when_messages_replay_prior_yes() -
687688
assert len(yes_turns) >= 2
688689

689690

691+
@pytest.mark.parametrize(
692+
"client_id",
693+
[None, "client-reply", "resume-1", "resume-2"],
694+
ids=["without-id", "remapped-id", "matching-first-id", "matching-last-id"],
695+
)
696+
@pytest.mark.parametrize(
697+
("parts", "expected_content"),
698+
[
699+
pytest.param([{"type": "input_text", "text": "yes"}], "yes", id="text-parts"),
700+
pytest.param(
701+
[
702+
{"type": "input_text", "text": "Before"},
703+
{
704+
"type": "image",
705+
"source": {"type": "url", "value": "https://example.com/image.png", "mimeType": "image/png"},
706+
},
707+
{"type": "text", "text": "After"},
708+
],
709+
[
710+
{"type": "text", "text": "Before"},
711+
{"type": "binary", "url": "https://example.com/image.png", "mimeType": "image/png"},
712+
{"type": "text", "text": "After"},
713+
],
714+
id="mixed-image",
715+
),
716+
pytest.param(
717+
[{"type": "audio", "source": {"type": "base64", "value": "YWJj", "mimeType": "audio/wav"}}],
718+
[{"type": "binary", "data": "YWJj", "mimeType": "audio/wav"}],
719+
id="media-only-audio",
720+
),
721+
],
722+
)
723+
async def test_workflow_hitl_resume_dedupes_normalized_current_turn_content(
724+
client_id: str | None, parts: list[dict[str, Any]], expected_content: Any
725+
) -> None:
726+
"""Only current-turn overlap consumes one matching reply after content normalization."""
727+
from agent_framework_ag_ui import InMemoryAGUIThreadSnapshotStore
728+
from agent_framework_ag_ui._snapshots import _SNAPSHOT_SCOPE_INPUT_KEY, AGUIThreadSnapshot
729+
730+
handled: list[list[dict[str, Any]]] = []
731+
732+
class MessageRequestExecutor(Executor):
733+
@handler
734+
async def start(self, message: Any, ctx: WorkflowContext[Any, str]) -> None:
735+
del message
736+
await ctx.request_info("Send replies", list[dict[str, Any]], request_id="user-input")
737+
738+
@response_handler
739+
async def capture(
740+
self, original_request: str, response: list[dict[str, Any]], ctx: WorkflowContext[Any, str]
741+
) -> None:
742+
del original_request
743+
handled.append(response)
744+
await ctx.yield_output("Captured replies")
745+
746+
store = InMemoryAGUIThreadSnapshotStore()
747+
workflow = WorkflowBuilder(start_executor=MessageRequestExecutor(id="message-request")).build()
748+
agent = AgentFrameworkWorkflow(workflow=workflow, snapshot_store=store)
749+
start_message = {"id": "start", "role": "user", "content": "start"}
750+
first_events = await _run(
751+
agent,
752+
{
753+
"thread_id": "overlap-thread",
754+
"messages": [start_message],
755+
_SNAPSHOT_SCOPE_INPUT_KEY: "tenant-a",
756+
},
757+
)
758+
assert not [event.model_dump() for event in first_events if event.type == "RUN_ERROR"]
759+
first_finished = next(event for event in first_events if event.type == "RUN_FINISHED")
760+
assert _interrupts_from_finished(first_finished)[0]["id"] == "user-input"
761+
762+
# A prior identical reply must not consume either of this interrupt's replies.
763+
await store.save(
764+
scope="tenant-a",
765+
thread_id="overlap-thread",
766+
snapshot=AGUIThreadSnapshot(
767+
messages=[start_message, {"id": "prior-reply", "role": "user", "content": expected_content}],
768+
state=None,
769+
interrupt=None,
770+
),
771+
)
772+
client_reply: dict[str, Any] = {"role": "user", "content": deepcopy(parts)}
773+
if client_id is not None:
774+
client_reply["id"] = client_id
775+
resume_replies = [
776+
{"id": "resume-1", "role": "user", "contents": deepcopy(parts)},
777+
{"id": "resume-2", "role": "user", "contents": deepcopy(parts)},
778+
]
779+
payload = {
780+
"thread_id": "overlap-thread",
781+
"messages": [
782+
start_message,
783+
{"id": "prior-reply", "role": "user", "content": deepcopy(parts)},
784+
client_reply,
785+
],
786+
"resume": {"interrupts": [{"id": "user-input", "value": resume_replies}]},
787+
_SNAPSHOT_SCOPE_INPUT_KEY: "tenant-a",
788+
}
789+
original_payload = deepcopy(payload)
790+
resumed_events = await _run(agent, payload)
791+
assert not [event.model_dump() for event in resumed_events if event.type == "RUN_ERROR"]
792+
assert handled == [resume_replies]
793+
assert payload == original_payload
794+
795+
snapshot = await store.get(scope="tenant-a", thread_id="overlap-thread")
796+
assert snapshot is not None
797+
user_messages = [message for message in snapshot.messages if message.get("role") == "user"]
798+
assert [message["content"] for message in user_messages] == ["start", *[expected_content] * 3]
799+
assert user_messages[1]["id"] == "prior-reply"
800+
if client_id is not None:
801+
assert user_messages[2]["id"] == client_id
802+
assert user_messages[3]["id"] == ("resume-1" if client_id == "resume-2" else "resume-2")
803+
804+
hydrated = await _run(agent, {"thread_id": "overlap-thread", "messages": [], _SNAPSHOT_SCOPE_INPUT_KEY: "tenant-a"})
805+
replay = next(event for event in hydrated if event.type == "MESSAGES_SNAPSHOT")
806+
replay_users = [
807+
message.model_dump(by_alias=True, exclude_none=True)["content"]
808+
for message in replay.messages
809+
if message.role == "user"
810+
]
811+
assert replay_users == [message["content"] for message in user_messages]
812+
assert handled == [resume_replies]
813+
814+
690815
def test_snapshot_messages_from_resume_keeps_conversational_approval_text() -> None:
691816
from agent_framework_ag_ui._workflow import _snapshot_messages_from_resume_value
692817

@@ -930,6 +1055,22 @@ def test_append_unique_snapshot_messages_keeps_intentional_repeated_replies() ->
9301055
assert [m["id"] for m in merged] == ["u0", "r1", "r2"]
9311056

9321057

1058+
def test_append_unique_snapshot_messages_keeps_current_overlap_after_historical_id_match() -> None:
1059+
"""Matching historical ids must not consume a current client turn's content allowance."""
1060+
from agent_framework_ag_ui._workflow import _append_unique_snapshot_messages
1061+
1062+
history = [{"id": "prior-reply", "role": "user", "content": "yes"}]
1063+
current = [{"id": "client-reply", "role": "user", "content": "yes"}]
1064+
incoming = [
1065+
{"id": "prior-reply", "role": "user", "content": "yes"},
1066+
{"id": "resume-reply", "role": "user", "content": "yes"},
1067+
]
1068+
1069+
assert _append_unique_snapshot_messages(history + current, incoming, content_dedupe_against=current) == (
1070+
history + current
1071+
)
1072+
1073+
9331074
def test_append_unique_snapshot_messages_keeps_second_hitl_yes_without_client_replay() -> None:
9341075
"""messages:[] HITL resumes must not collapse a later identical reply against history."""
9351076
from agent_framework_ag_ui._workflow import _append_unique_snapshot_messages

0 commit comments

Comments
 (0)