From f04b91d4322ffa2d6a3806c0c6ae0dc7f7d6300d Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Wed, 11 Nov 2026 04:55:40 +0530 Subject: [PATCH 1/7] fix(contents): build separate call-to-results and result-to-call mappings in extract_range Signed-off-by: Hardik Kaurani --- .../chat_history_reducer_utils.py | 91 +++++++++++-------- 1 file changed, 53 insertions(+), 38 deletions(-) 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..8138a3a9570b 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,69 @@ 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 + if preserve_pairs and idx in call_to_results: + results = call_to_results[idx] + in_range_results = [r for r in results if start <= r < end] + if in_range_results: + if filter_func and filter_func(msg): + for r in in_range_results: + if r in sliced: + sliced.remove(r) + i += 1 + continue + + if filter_func and all(filter_func(history[r]) for r in in_range_results): + for r in in_range_results: + if r in sliced: + sliced.remove(r) 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) 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 + for r in in_range_results: + if r > idx: + extracted.append(history[r]) + if r in sliced: + sliced.remove(r) + i += 1 + continue + + if filter_func and filter_func(msg): 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) i += 1 - else: - # keep it if filter_func not triggered + continue + + if preserve_pairs and idx in result_to_call: + cidx = result_to_call[idx] + if start <= cidx < end: + if filter_func and filter_func(msg): + i += 1 + continue + extracted.append(msg) + i += 1 + continue + + if filter_func and filter_func(msg): + i += 1 + continue extracted.append(msg) i += 1 + continue + + # Normal message (not a call or result under preserve_pairs) + if filter_func and filter_func(msg): + i += 1 + continue + + extracted.append(msg) + i += 1 return extracted From 9b7467e03e31df9bf2d7b3fb75c9208d99b0d7c9 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Wed, 11 Nov 2026 04:56:09 +0530 Subject: [PATCH 2/7] fix(contents): handle partial parallel result filtering without orphaning assistant calls Signed-off-by: Hardik Kaurani --- .../history_reducer/chat_history_reducer_utils.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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 8138a3a9570b..1f132f9dec00 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 @@ -227,6 +227,7 @@ def extract_range( if preserve_pairs and idx in call_to_results: results = call_to_results[idx] in_range_results = [r for r in results if start <= r < end] + out_of_range_results = [r for r in results if not (start <= r < end)] if in_range_results: if filter_func and filter_func(msg): for r in in_range_results: @@ -235,16 +236,20 @@ def extract_range( i += 1 continue - if filter_func and all(filter_func(history[r]) for r in in_range_results): - for r in in_range_results: - if r in sliced: - sliced.remove(r) + kept_in_range = [r for r in in_range_results if not (filter_func and filter_func(history[r]))] + skipped_in_range = [r for r in in_range_results if filter_func and filter_func(history[r])] + + for r in skipped_in_range: + if r in sliced: + sliced.remove(r) + + if not kept_in_range and not out_of_range_results: i += 1 continue extracted.append(msg) for r in in_range_results: - if r > idx: + if r in kept_in_range and r > idx: extracted.append(history[r]) if r in sliced: sliced.remove(r) From ed43e75b1ae584badfe01c06a13bc9554948ddd6 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Wed, 11 Nov 2026 04:56:29 +0530 Subject: [PATCH 3/7] fix(contents): sort and deduplicate kept in-range tool results in extract_range Signed-off-by: Hardik Kaurani --- .../history_reducer/chat_history_reducer_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 1f132f9dec00..59d1ac6caf77 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 @@ -230,7 +230,7 @@ def extract_range( out_of_range_results = [r for r in results if not (start <= r < end)] if in_range_results: if filter_func and filter_func(msg): - for r in in_range_results: + for r in set(in_range_results): if r in sliced: sliced.remove(r) i += 1 @@ -239,7 +239,7 @@ def extract_range( kept_in_range = [r for r in in_range_results if not (filter_func and filter_func(history[r]))] skipped_in_range = [r for r in in_range_results if filter_func and filter_func(history[r])] - for r in skipped_in_range: + for r in set(skipped_in_range): if r in sliced: sliced.remove(r) @@ -248,8 +248,8 @@ def extract_range( continue extracted.append(msg) - for r in in_range_results: - if r in kept_in_range and r > idx: + for r in sorted(set(kept_in_range)): + if r > idx: extracted.append(history[r]) if r in sliced: sliced.remove(r) From cd462fe732e4075d6f5b3914644821c1faf35abf Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Wed, 11 Nov 2026 04:56:56 +0530 Subject: [PATCH 4/7] test(contents): add regression coverage for parallel tool call pair filtering (#14485) Signed-off-by: Hardik Kaurani --- .../test_chat_history_reducer_utils.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) 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..9f7eff862769 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,63 @@ 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 test_extract_range_parallel_tool_calls_filter_partial_result(): + """Regression test for #14485: filtering out only one of several parallel tool results + must not drop the assistant message that made the tool calls. + """ + 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 msg_assistant in extracted, "Assistant message with tool calls must not be dropped" + assert msg_paris_result in extracted, "Unfiltered Paris tool result must be kept" + assert msg_tokyo_result not in extracted, "Filtered Tokyo tool result must be excluded" + assert extracted == [msg_user, msg_assistant, msg_paris_result, msg_done] + + +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] From 93397ca58b047ad59679c98b29b0dbf620c7aecd Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Wed, 11 Nov 2026 04:57:23 +0530 Subject: [PATCH 5/7] test(contents): add regression coverage for call filtering and multi-result messages Signed-off-by: Hardik Kaurani --- .../test_chat_history_reducer_utils.py | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) 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 9f7eff862769..e7aefb060477 100644 --- a/python/tests/unit/contents/test_chat_history_reducer_utils.py +++ b/python/tests/unit/contents/test_chat_history_reducer_utils.py @@ -290,3 +290,92 @@ def test_extract_range_parallel_tool_calls_preserves_chronological_order(): 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] + + +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] + + +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] + + +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] From eddad6db462c5f03a504c7d4e2fd305c35504b4f Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Thu, 12 Nov 2026 03:35:23 +0530 Subject: [PATCH 6/7] fix(contents): strip unkept FunctionCallContent items and drop calls without kept results in extract_range Signed-off-by: Hardik Kaurani --- .../chat_history_reducer_utils.py | 86 ++++++++++--------- 1 file changed, 46 insertions(+), 40 deletions(-) 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 59d1ac6caf77..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 @@ -226,61 +226,43 @@ def extract_range( if preserve_pairs and idx in call_to_results: results = call_to_results[idx] - in_range_results = [r for r in results if start <= r < end] - out_of_range_results = [r for r in results if not (start <= r < end)] - if in_range_results: - if filter_func and filter_func(msg): - for r in set(in_range_results): - if r in sliced: - sliced.remove(r) - i += 1 - continue - - kept_in_range = [r for r in in_range_results if not (filter_func and filter_func(history[r]))] - skipped_in_range = [r for r in in_range_results if filter_func and filter_func(history[r])] - - for r in set(skipped_in_range): - if r in sliced: + 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 - if not kept_in_range and not out_of_range_results: - i += 1 + kept_in_range: list[int] = [] + for r in results: + if not (start <= r < end): continue + 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) - extracted.append(msg) - for r in sorted(set(kept_in_range)): - if r > idx: - extracted.append(history[r]) - if r in sliced: - sliced.remove(r) + if not kept_in_range: i += 1 continue - if filter_func and filter_func(msg): - i += 1 - continue - 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 continue if preserve_pairs and idx in result_to_call: cidx = result_to_call[idx] - if start <= cidx < end: - if filter_func and filter_func(msg): - i += 1 - continue - extracted.append(msg) + if start <= cidx < end and filter_func and filter_func(history[cidx]): i += 1 continue - if filter_func and filter_func(msg): - i += 1 - continue - extracted.append(msg) - i += 1 - continue - - # Normal message (not a call or result under preserve_pairs) + # 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 @@ -302,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}) From 2920cea0d30fb6e700ebcda8827ba771f3539721 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Thu, 12 Nov 2026 03:35:23 +0530 Subject: [PATCH 7/7] test(contents): verify call-result invariant on partial and out-of-range tool filtering Signed-off-by: Hardik Kaurani --- .../test_chat_history_reducer_utils.py | 155 +++++++++++++++++- 1 file changed, 151 insertions(+), 4 deletions(-) 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 e7aefb060477..061fe6528d10 100644 --- a/python/tests/unit/contents/test_chat_history_reducer_utils.py +++ b/python/tests/unit/contents/test_chat_history_reducer_utils.py @@ -232,9 +232,25 @@ def test_locate_safe_reduction_index_tool_role_without_function_result_content() ) +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 not drop the assistant message that made the tool calls. + 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( @@ -260,10 +276,16 @@ def test_extract_range_parallel_tool_calls_filter_partial_result(): extracted = extract_range(history, start=0, end=len(history), filter_func=filter_func, preserve_pairs=True) - assert msg_assistant in extracted, "Assistant message with tool calls must not be dropped" - assert msg_paris_result in extracted, "Unfiltered Paris tool result must be kept" + 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 == [msg_user, msg_assistant, msg_paris_result, msg_done] + assert extracted[3] is msg_done + _assert_every_call_has_later_result(extracted) def test_extract_range_parallel_tool_calls_preserves_chronological_order(): @@ -290,6 +312,7 @@ def test_extract_range_parallel_tool_calls_preserves_chronological_order(): 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(): @@ -321,6 +344,7 @@ def test_extract_range_parallel_tool_calls_filter_call_message_drops_all_results 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(): @@ -352,6 +376,7 @@ def test_extract_range_parallel_tool_calls_filter_all_results_drops_call_message 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(): @@ -379,3 +404,125 @@ def test_extract_range_multiple_results_in_single_message(): 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)