diff --git a/python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py b/python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py index 84e79c29ecc1..b14bc837c8b7 100644 --- a/python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py +++ b/python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py @@ -205,14 +205,13 @@ def extract_range( sliced = list(range(start, end)) # If we need to preserve call->result pairs, gather them - pair_map = {} + call_to_results: dict[int, list[int]] = {} + result_to_call: dict[int, int] = {} if preserve_pairs: pairs = get_call_result_pairs(history) - # store in a dict for quick membership checking - # call_idx -> result_idx, and also result_idx -> call_idx for cidx, ridx in pairs: - pair_map[cidx] = ridx - pair_map[ridx] = cidx + call_to_results.setdefault(cidx, []).append(ridx) + result_to_call[ridx] = cidx extracted: list[ChatMessageContent] = [] i = 0 @@ -220,53 +219,56 @@ def extract_range( idx = sliced[i] msg = history[idx] - # If filter_func excludes it, skip it - if filter_func and filter_func(msg): - i += 1 - continue - # skipping system/developer message if msg.role in (AuthorRole.DEVELOPER, AuthorRole.SYSTEM): i += 1 continue - # If preserve_pairs is on, and there's a paired index, skip or include them both - if preserve_pairs and idx in pair_map: - paired_idx = pair_map[idx] - # If the pair is within [start, end), we must keep or skip them together - if start <= paired_idx < end: - # Check if the pair or itself fails filter_func - if filter_func and (filter_func(history[paired_idx]) or filter_func(msg)): - # skip both - i += 1 - # Also skip the paired index if it's in our current slice - if paired_idx in sliced: - # remove it from the slice so we don't process it again - sliced.remove(paired_idx) + if preserve_pairs and idx in call_to_results: + results = call_to_results[idx] + if filter_func and filter_func(msg): + for r in set(results): + if start <= r < end and r > idx and r in sliced: + sliced.remove(r) + i += 1 + continue + + kept_in_range: list[int] = [] + for r in results: + if not (start <= r < end): continue - # keep both - extracted.append(msg) - if paired_idx > idx: - # We'll skip the pair in the normal iteration by removing from slice - # but add it to extracted right now - extracted.append(history[paired_idx]) - if paired_idx in sliced: - sliced.remove(paired_idx) - else: - # if paired_idx < idx, it might appear later, so skip for now - # but we may have already processed it if i was the 2nd item - # either way, do not add duplicates - pass + if filter_func and filter_func(history[r]): + if r > idx and r in sliced: + sliced.remove(r) + elif r > idx: + kept_in_range.append(r) + + if not kept_in_range: i += 1 continue - # If the paired_idx is outside [start, end), there's no conflict - # so we can just do normal logic - extracted.append(msg) + + unique_kept = sorted(set(kept_in_range)) + extracted.append(_filter_call_message_items(msg, [history[r] for r in unique_kept])) + for r in unique_kept: + extracted.append(history[r]) + if r in sliced: + sliced.remove(r) i += 1 - else: - # keep it if filter_func not triggered - extracted.append(msg) + continue + + if preserve_pairs and idx in result_to_call: + cidx = result_to_call[idx] + if start <= cidx < end and filter_func and filter_func(history[cidx]): + i += 1 + continue + + # Normal message (not a call, or a result whose paired call is outside [start, end)) + if filter_func and filter_func(msg): i += 1 + continue + + extracted.append(msg) + i += 1 return extracted @@ -282,3 +284,27 @@ def contains_function_call_or_result(msg: ChatMessageContent) -> bool: if msg.role == AuthorRole.TOOL: return True return any(isinstance(item, (FunctionCallContent, FunctionResultContent)) for item in msg.items) + + +def _filter_call_message_items( + call_msg: ChatMessageContent, + kept_results: list[ChatMessageContent], +) -> ChatMessageContent: + """Keep only FunctionCallContent items in call_msg whose ID matches a kept result. + + Non-call items are preserved, and a shallow copy with the updated items list + is returned when any FunctionCallContent is stripped so the original history + message is never mutated. + """ + kept_result_ids = { + item.id + for res_msg in kept_results + for item in res_msg.items + if isinstance(item, FunctionResultContent) and item.id is not None + } + filtered_items = [ + item for item in call_msg.items if not isinstance(item, FunctionCallContent) or item.id in kept_result_ids + ] + if len(filtered_items) == len(call_msg.items): + return call_msg + return call_msg.model_copy(update={"items": filtered_items}) diff --git a/python/tests/unit/contents/test_chat_history_reducer_utils.py b/python/tests/unit/contents/test_chat_history_reducer_utils.py index d46340915b8a..061fe6528d10 100644 --- a/python/tests/unit/contents/test_chat_history_reducer_utils.py +++ b/python/tests/unit/contents/test_chat_history_reducer_utils.py @@ -230,3 +230,299 @@ def test_locate_safe_reduction_index_tool_role_without_function_result_content() f"Tool result at index 2 was kept but tool call at index 1 was dropped. " f"Kept indices: {kept_indices}, reduction index: {idx}" ) + + +def _assert_every_call_has_later_result(messages: list[ChatMessageContent]) -> None: + """Assert that every FunctionCallContent in messages has a matching FunctionResultContent later in messages.""" + for idx, msg in enumerate(messages): + later_result_ids = { + item.id + for later_msg in messages[idx + 1 :] + for item in later_msg.items + if isinstance(item, FunctionResultContent) and item.id is not None + } + for item in msg.items: + if isinstance(item, FunctionCallContent): + assert item.id is not None and item.id in later_result_ids, ( + f"FunctionCallContent(id={item.id!r}) at index {idx} has no matching FunctionResultContent later" + ) + + +def test_extract_range_parallel_tool_calls_filter_partial_result(): + """Regression test for #14485: filtering out only one of several parallel tool results + must keep the kept pair and strip only the excluded FunctionCallContent on a copy. + """ + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_paris_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_paris", name="get_weather", result="15C")], + ) + msg_tokyo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_tokyo", name="get_weather", result="22C")], + ) + msg_done = ChatMessageContent(role=AuthorRole.ASSISTANT, content="done") + history = [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result, msg_done] + + # Filter out only the Tokyo result (e.g. moderation or specific error filter) + filter_func = lambda m: m is msg_tokyo_result # noqa: E731 + + extracted = extract_range(history, start=0, end=len(history), filter_func=filter_func, preserve_pairs=True) + + assert len(extracted) == 4 + assert extracted[0] is msg_user + assert extracted[1] is not msg_assistant + assert extracted[1].role == AuthorRole.ASSISTANT + assert [item.id for item in extracted[1].items] == ["call_paris"] + assert [item.id for item in msg_assistant.items] == ["call_paris", "call_tokyo"] + assert extracted[2] is msg_paris_result + assert msg_tokyo_result not in extracted, "Filtered Tokyo tool result must be excluded" + assert extracted[3] is msg_done + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_parallel_tool_calls_preserves_chronological_order(): + """Regression test: parallel tool call results must preserve their natural chronological order.""" + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_paris_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_paris", name="get_weather", result="15C")], + ) + msg_tokyo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_tokyo", name="get_weather", result="22C")], + ) + msg_done = ChatMessageContent(role=AuthorRole.ASSISTANT, content="done") + history = [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result, msg_done] + + extracted = extract_range(history, start=0, end=len(history), preserve_pairs=True) + + assert extracted == [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result, msg_done] + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_parallel_tool_calls_filter_call_message_drops_all_results(): + """Regression test: filtering the call message drops all of its paired parallel results.""" + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_paris_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_paris", name="get_weather", result="15C")], + ) + msg_tokyo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_tokyo", name="get_weather", result="22C")], + ) + msg_done = ChatMessageContent(role=AuthorRole.ASSISTANT, content="done") + history = [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result, msg_done] + + filter_func = lambda m: m is msg_assistant # noqa: E731 + + extracted = extract_range(history, start=0, end=len(history), filter_func=filter_func, preserve_pairs=True) + + assert msg_assistant not in extracted + assert msg_paris_result not in extracted + assert msg_tokyo_result not in extracted + assert extracted == [msg_user, msg_done] + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_parallel_tool_calls_filter_all_results_drops_call_message(): + """Regression test: filtering all parallel results drops the call message to preserve pairs.""" + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_paris_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_paris", name="get_weather", result="15C")], + ) + msg_tokyo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_tokyo", name="get_weather", result="22C")], + ) + msg_done = ChatMessageContent(role=AuthorRole.ASSISTANT, content="done") + history = [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result, msg_done] + + filter_func = lambda m: m in (msg_paris_result, msg_tokyo_result) # noqa: E731 + + extracted = extract_range(history, start=0, end=len(history), filter_func=filter_func, preserve_pairs=True) + + assert msg_assistant not in extracted + assert msg_paris_result not in extracted + assert msg_tokyo_result not in extracted + assert extracted == [msg_user, msg_done] + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_multiple_results_in_single_message(): + """Regression test: when a single TOOL message holds multiple FunctionResultContent items, + extract_range preserves them without duplicate entries. + """ + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_tool_multi = ChatMessageContent( + role=AuthorRole.TOOL, + items=[ + FunctionResultContent(id="call_paris", name="get_weather", result="15C"), + FunctionResultContent(id="call_tokyo", name="get_weather", result="22C"), + ], + ) + msg_done = ChatMessageContent(role=AuthorRole.ASSISTANT, content="done") + history = [msg_user, msg_assistant, msg_tool_multi, msg_done] + + extracted = extract_range(history, start=0, end=len(history), preserve_pairs=True) + + assert extracted == [msg_user, msg_assistant, msg_tool_multi, msg_done] + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_parallel_tool_calls_filter_partial_preserves_non_call_items_and_metadata(): + """When stripping filtered calls from an assistant message copy, non-call items and metadata stay intact.""" + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + content="Checking both cities.", + name="weather_bot", + metadata={"trace": "abc"}, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_paris_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_paris", name="get_weather", result="15C")], + ) + msg_tokyo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_tokyo", name="get_weather", result="22C")], + ) + history = [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result] + + def filter_func(m: ChatMessageContent) -> bool: + return m is msg_tokyo_result + + extracted = extract_range(history, start=0, end=len(history), filter_func=filter_func, preserve_pairs=True) + + assert len(extracted) == 3 + copied_assistant = extracted[1] + assert copied_assistant is not msg_assistant + assert copied_assistant.content == "Checking both cities." + assert copied_assistant.name == "weather_bot" + assert copied_assistant.metadata == {"trace": "abc"} + kept_call_ids = [item.id for item in copied_assistant.items if isinstance(item, FunctionCallContent)] + assert kept_call_ids == ["call_paris"] + orig_call_ids = [item.id for item in msg_assistant.items if isinstance(item, FunctionCallContent)] + assert orig_call_ids == ["call_paris", "call_tokyo"] + assert extracted[2] is msg_paris_result + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_multiple_results_in_single_message_with_sibling_filtered(): + """Filtering one tool result message keeps all kept calls from a multi-result sibling message once.""" + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + FunctionCallContent(id="call_oslo", name="get_weather", arguments='{"city":"Oslo"}'), + ], + ) + msg_tool_multi = ChatMessageContent( + role=AuthorRole.TOOL, + items=[ + FunctionResultContent(id="call_paris", name="get_weather", result="15C"), + FunctionResultContent(id="call_tokyo", name="get_weather", result="22C"), + ], + ) + msg_oslo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_oslo", name="get_weather", result="10C")], + ) + history = [msg_user, msg_assistant, msg_tool_multi, msg_oslo_result] + + def filter_func(m: ChatMessageContent) -> bool: + return m is msg_oslo_result + + extracted = extract_range(history, start=0, end=len(history), filter_func=filter_func, preserve_pairs=True) + + assert len(extracted) == 3 + assert [item.id for item in extracted[1].items] == ["call_paris", "call_tokyo"] + assert extracted[2] is msg_tool_multi + _assert_every_call_has_later_result(extracted) + + +def test_extract_range_parallel_tool_calls_with_results_past_end(): + """Calls whose results are filtered or past end must not leave unresponded FunctionCallContent items.""" + msg_user = ChatMessageContent(role=AuthorRole.USER, content="weather?") + msg_assistant = ChatMessageContent( + role=AuthorRole.ASSISTANT, + items=[ + FunctionCallContent(id="call_paris", name="get_weather", arguments='{"city":"Paris"}'), + FunctionCallContent(id="call_tokyo", name="get_weather", arguments='{"city":"Tokyo"}'), + ], + ) + msg_paris_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_paris", name="get_weather", result="15C")], + ) + msg_tokyo_result = ChatMessageContent( + role=AuthorRole.TOOL, + items=[FunctionResultContent(id="call_tokyo", name="get_weather", result="22C")], + ) + history = [msg_user, msg_assistant, msg_paris_result, msg_tokyo_result] + + def filter_paris(m: ChatMessageContent) -> bool: + return m is msg_paris_result + + # Every in-range result filtered while call_tokyo result lies past end: drop the call message. + extracted_all_in_range_filtered = extract_range( + history, start=0, end=3, filter_func=filter_paris, preserve_pairs=True + ) + assert extracted_all_in_range_filtered == [msg_user] + _assert_every_call_has_later_result(extracted_all_in_range_filtered) + + # Partial slice ending before call_tokyo result: strip call_tokyo on copy, keep call_paris pair. + extracted_partial_slice = extract_range(history, start=0, end=3, preserve_pairs=True) + assert len(extracted_partial_slice) == 3 + assert extracted_partial_slice[0] is msg_user + assert [item.id for item in extracted_partial_slice[1].items] == ["call_paris"] + assert extracted_partial_slice[2] is msg_paris_result + _assert_every_call_has_later_result(extracted_partial_slice) + + # Slice ending before all results: drop the call message. + extracted_no_results_in_slice = extract_range(history, start=0, end=2, preserve_pairs=True) + assert extracted_no_results_in_slice == [msg_user] + _assert_every_call_has_later_result(extracted_no_results_in_slice)