|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from semantic_kernel.contents.chat_message_content import ChatMessageContent |
| 4 | +from semantic_kernel.contents.function_call_content import FunctionCallContent |
| 5 | +from semantic_kernel.contents.function_result_content import FunctionResultContent |
| 6 | +from semantic_kernel.contents.history_reducer.chat_history_reducer_utils import locate_safe_reduction_index |
| 7 | +from semantic_kernel.contents.history_reducer.chat_history_truncation_reducer import ChatHistoryTruncationReducer |
| 8 | +from semantic_kernel.contents.utils.author_role import AuthorRole |
| 9 | + |
| 10 | + |
| 11 | +def _call(cid: str) -> ChatMessageContent: |
| 12 | + m = ChatMessageContent(role=AuthorRole.ASSISTANT, content="") |
| 13 | + m.items.append(FunctionCallContent(id=cid, function_name=f"f{cid}", plugin_name="p", arguments={})) |
| 14 | + return m |
| 15 | + |
| 16 | + |
| 17 | +def _has_call(history: list[ChatMessageContent], cid: str) -> bool: |
| 18 | + return any(isinstance(item, FunctionCallContent) and item.id == cid for msg in history for item in msg.items) |
| 19 | + |
| 20 | + |
| 21 | +def _has_result(history: list[ChatMessageContent], cid: str) -> bool: |
| 22 | + return any(isinstance(item, FunctionResultContent) and item.id == cid for msg in history for item in msg.items) |
| 23 | + |
| 24 | + |
| 25 | +async def test_public_reducer_keeps_text_tool_without_orphaning_earlier_call(): |
| 26 | + """Copilot r4415468227 case through the public reducer API. |
| 27 | +
|
| 28 | + [user, call A, call B, TOOL(text result A), assistant, TOOL(result B)] |
| 29 | + target_count=2. On base the cut lands at idx=2, dropping call A but keeping |
| 30 | + the TOOL message carrying text for A. After the fix the cut moves to idx=1 |
| 31 | + so both call A and its text-result TOOL are retained. |
| 32 | + """ |
| 33 | + history = [ |
| 34 | + ChatMessageContent(role=AuthorRole.USER, content="u0"), |
| 35 | + _call("A"), |
| 36 | + _call("B"), |
| 37 | + ChatMessageContent(role=AuthorRole.TOOL, content="result A as plain text"), |
| 38 | + ChatMessageContent(role=AuthorRole.ASSISTANT, content="text"), |
| 39 | + ChatMessageContent( |
| 40 | + role=AuthorRole.TOOL, |
| 41 | + items=[FunctionResultContent(id="B", function_name="fB", plugin_name="p", result="r-B")], |
| 42 | + ), |
| 43 | + ] |
| 44 | + reducer = ChatHistoryTruncationReducer(target_count=2, threshold_count=0) |
| 45 | + reducer.messages = history |
| 46 | + result = await reducer.reduce() |
| 47 | + |
| 48 | + assert result is not None |
| 49 | + kept = result.messages |
| 50 | + assert _has_call(kept, "B"), "call B should be retained" |
| 51 | + assert _has_result(kept, "B"), "result B should be retained" |
| 52 | + assert _has_call(kept, "A"), "call A must be retained alongside its text TOOL result" |
| 53 | + assert any(msg.role == AuthorRole.TOOL and msg.content == "result A as plain text" for msg in kept), ( |
| 54 | + "the text-only TOOL result for A must be retained alongside call A" |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_chained_closure_requires_multiple_adjacency_id_rounds(): |
| 59 | + """n=10 history with two ID pairs and a text-only TOOL block. |
| 60 | +
|
| 61 | + Initial cut at 8. ID closure for (B,resultB) moves to 5; ID closure for |
| 62 | + (A,resultA) moves to 2; adjacency re-walk from 2 moves to 1 (call X). |
| 63 | + On base (no adjacency re-walk) the cut stops at 2, orphaning the text-only |
| 64 | + TOOL for X at msg3. |
| 65 | + """ |
| 66 | + history = [ |
| 67 | + ChatMessageContent(role=AuthorRole.USER, content="u0"), # 0 |
| 68 | + _call("X"), # 1 |
| 69 | + _call("A"), # 2 |
| 70 | + ChatMessageContent(role=AuthorRole.TOOL, content="text X"), # 3 (no FunctionResult) |
| 71 | + ChatMessageContent(role=AuthorRole.ASSISTANT, content="text"), # 4 |
| 72 | + _call("B"), # 5 |
| 73 | + ChatMessageContent( |
| 74 | + role=AuthorRole.TOOL, |
| 75 | + items=[FunctionResultContent(id="A", function_name="fA", plugin_name="p", result="r-A")], |
| 76 | + ), # 6 |
| 77 | + ChatMessageContent(role=AuthorRole.TOOL, content="text B"), # 7 (no FunctionResult) |
| 78 | + ChatMessageContent(role=AuthorRole.ASSISTANT, content="text"), # 8 |
| 79 | + ChatMessageContent( |
| 80 | + role=AuthorRole.TOOL, |
| 81 | + items=[FunctionResultContent(id="B", function_name="fB", plugin_name="p", result="r-B")], |
| 82 | + ), # 9 |
| 83 | + ] |
| 84 | + idx = locate_safe_reduction_index(history, target_count=2, threshold_count=0) |
| 85 | + assert idx is not None |
| 86 | + assert idx == 1, f"expected cut at call X (idx=1), got {idx}" |
0 commit comments