fix: quarantine invalid tool results

This commit is contained in:
hamb1y authored and Xubin Ren committed 2026-07-22 01:59:09 +08:00
1 parent c6a4d46a2a
commit a9867a5a4e
4 files changed
+113 -5

No files matched your search

+5 -2
View File
@@ -232,8 +232,9 @@ class ContextGovernor:
def drop_orphan_tool_results(
messages: list[dict[str, Any]],
) -> list[dict[str, Any]]:
"""Drop tool results that have no matching assistant tool_call earlier in history."""
"""Drop invalid tool results before history is sent back to providers."""
declared: set[str] = set()
fulfilled: set[str] = set()
updated: list[dict[str, Any]] | None = None
for idx, msg in enumerate(messages):
role = msg.get("role")
@@ -243,10 +244,12 @@ class ContextGovernor:
declared.add(str(tc["id"]))
if role == "tool":
tid = msg.get("tool_call_id")
if tid and str(tid) not in declared:
tid_str = str(tid) if tid else ""
if not tid_str or tid_str not in declared or tid_str in fulfilled:
if updated is None:
updated = [dict(m) for m in messages[:idx]]
continue
fulfilled.add(tid_str)
if updated is not None:
updated.append(dict(msg))