|
4 | 4 |
|
5 | 5 | from __future__ import annotations |
6 | 6 |
|
| 7 | +from copy import deepcopy |
7 | 8 | from datetime import datetime, timezone |
8 | 9 | from typing import Any, cast |
9 | 10 | from unittest.mock import Mock |
@@ -687,6 +688,130 @@ async def test_workflow_hitl_resume_keeps_yes_when_messages_replay_prior_yes() - |
687 | 688 | assert len(yes_turns) >= 2 |
688 | 689 |
|
689 | 690 |
|
| 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 | + |
690 | 815 | def test_snapshot_messages_from_resume_keeps_conversational_approval_text() -> None: |
691 | 816 | from agent_framework_ag_ui._workflow import _snapshot_messages_from_resume_value |
692 | 817 |
|
@@ -930,6 +1055,22 @@ def test_append_unique_snapshot_messages_keeps_intentional_repeated_replies() -> |
930 | 1055 | assert [m["id"] for m in merged] == ["u0", "r1", "r2"] |
931 | 1056 |
|
932 | 1057 |
|
| 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 | + |
933 | 1074 | def test_append_unique_snapshot_messages_keeps_second_hitl_yes_without_client_replay() -> None: |
934 | 1075 | """messages:[] HITL resumes must not collapse a later identical reply against history.""" |
935 | 1076 | from agent_framework_ag_ui._workflow import _append_unique_snapshot_messages |
|
0 commit comments