Symptom: on Bedrock Converse a turn fails with "ValidationException: Expected toolResult blocks at messages.N.content for the following Ids" right after "Removed duplicate tool call: <tool>". Retries and every fallback model fail the same way because they replay the same history. The delegate_task concurrency cap hits the same path with any model. Root cause: run_tool_round passes the batch through _cap_delegate_task_calls and _deduplicate_tool_calls, which remove entries from tool_calls only. The ordered native sidecars (bedrock_content_blocks, anthropic_content_blocks) still hold the dropped call's toolUse/tool_use block, and both converters replay the sidecar instead of rebuilding blocks from tool_calls. The dropped call never gets a result, so the next request carries an unanswered toolUse. Fix: prune_native_tool_blocks (next to replace_native_tool_blocks) removes only the blocks whose id belongs to a call the guardrails dropped. Thinking/text blocks, signatures and block order are untouched, as are blocks it cannot attribute. run_tool_round calls it only when the guardrails returned fewer calls, before id normalization so the sidecar ids still match. Mixed invalid-name calls stay in tool_calls (they get an error result) and keep their blocks. Tests: tests/agent/test_guardrail_sidecar_prune.py drives the real transports and run_tool_round up to staging, then builds the Converse request with bedrock_adapter and asserts every toolUse has a matching toolResult: duplicate call in a parallel Bedrock batch, capped delegate_task, mixed invalid batch plus a duplicate, Anthropic sidecar, and the no-drop path leaving the sidecar object identical. 4 fail on main, 5 pass with the fix.
117 lines
5.5 KiB
Python
117 lines
5.5 KiB
Python
"""Normalize local bridge batches before persistence and per-tool execution."""
|
|
|
|
from copy import deepcopy
|
|
import json
|
|
|
|
from agent.message_sanitization import deterministic_call_id, uniquify_tool_call_ids
|
|
from session_rules.turns import coalesce_tool_call_id
|
|
from agent.transports.types import ToolCall
|
|
from tools.connectors import is_connector_name
|
|
from tools.connectors.gateway.config import MAX_CALLS_PER_DISPATCH
|
|
from tools.tool_search_catalog import TOOL_CALL_NAME
|
|
from tools.tool_search_validation import normalize_tool_call_entries
|
|
|
|
|
|
def expand_local_tool_batches(tool_calls: list, *, provider_data: dict | None = None) -> list:
|
|
"""Give every local entry the ordinary agent execution and persistence path.
|
|
|
|
Registry-only batch dispatch loses the live agent's inline tools, guardrails,
|
|
and per-server concurrency policy. Split the *new* assistant turn instead,
|
|
before it is persisted; never rewrite an earlier conversation message.
|
|
Connector-only batches keep their existing dispatcher and wire shape.
|
|
"""
|
|
expanded, replacements = [], {}
|
|
for call in tool_calls:
|
|
if call.function.name != TOOL_CALL_NAME:
|
|
expanded.append(call)
|
|
continue
|
|
try:
|
|
args = call.function.arguments
|
|
entries, error = normalize_tool_call_entries(json.loads(args) if isinstance(args, str) else args)
|
|
except (TypeError, ValueError, AttributeError):
|
|
entries, error = [], True
|
|
if (error or not 1 < len(entries) <= MAX_CALLS_PER_DISPATCH
|
|
or all(is_connector_name(entry["name"]) for entry in entries)):
|
|
# Leave malformed/oversized requests intact for the dispatcher's error.
|
|
expanded.append(call)
|
|
continue
|
|
parent_id = coalesce_tool_call_id(call)
|
|
output_index = getattr(call, "_codex_output_index", None)
|
|
children = []
|
|
for index, entry in enumerate(entries):
|
|
arguments = json.dumps({"calls": [entry]}, ensure_ascii=False)
|
|
if index == 0:
|
|
child = deepcopy(call)
|
|
child.function.arguments = arguments
|
|
else:
|
|
# A new entry must not inherit the parent's Responses item id or
|
|
# provider signature, only its response-local output position.
|
|
# IDs are deterministic for stable replay.
|
|
child = ToolCall(
|
|
id=deterministic_call_id(TOOL_CALL_NAME, parent_id + arguments, index),
|
|
name=TOOL_CALL_NAME, arguments=arguments,
|
|
provider_data={"_codex_output_index": output_index} if type(output_index) is int else None,
|
|
)
|
|
expanded.append(child)
|
|
children.append(child)
|
|
replacements[parent_id] = children
|
|
uniquify_tool_call_ids(expanded)
|
|
if provider_data and replacements:
|
|
replace_native_tool_blocks(provider_data, replacements)
|
|
return expanded
|
|
|
|
|
|
def replace_native_tool_blocks(provider_data: dict, replacements: dict) -> None:
|
|
"""Native providers replay ordered blocks instead of rebuilding them from
|
|
tool_calls. Replace the matching tool block in place, preserving signed
|
|
thinking/text blocks and their order exactly."""
|
|
for key, replace in (("anthropic_content_blocks", _anthropic_children),
|
|
("bedrock_content_blocks", _bedrock_children)):
|
|
if blocks := provider_data.get(key):
|
|
provider_data[key] = [child for block in blocks for child in replace(block, replacements)]
|
|
|
|
|
|
def prune_native_tool_blocks(provider_data: dict, emitted: list, kept: list) -> None:
|
|
"""Drop the native tool blocks of calls in ``emitted`` but not in ``kept``.
|
|
|
|
A guardrail that drops a call (duplicate, delegate cap) leaves it without a
|
|
result, and the native sidecars replay ahead of tool_calls: Converse then
|
|
rejects the turn ("Expected toolResult blocks ... for the following Ids").
|
|
Only blocks naming a dropped call go; thinking/text blocks, signatures,
|
|
unattributable tool blocks and block order are kept exactly.
|
|
"""
|
|
kept_ids = {coalesce_tool_call_id(tc) for tc in kept}
|
|
dropped = {coalesce_tool_call_id(tc) for tc in emitted} - kept_ids - {""}
|
|
if not dropped:
|
|
return
|
|
for key, tool_id in (("anthropic_content_blocks", _anthropic_tool_id),
|
|
("bedrock_content_blocks", _bedrock_tool_id)):
|
|
if blocks := provider_data.get(key):
|
|
provider_data[key] = [block for block in blocks if tool_id(block) not in dropped]
|
|
|
|
|
|
def _anthropic_tool_id(block):
|
|
return block.get("id") if isinstance(block, dict) and block.get("type") == "tool_use" else None
|
|
|
|
|
|
def _bedrock_tool_id(block):
|
|
tool = block.get("toolUse") if isinstance(block, dict) else None
|
|
return tool.get("toolUseId") if isinstance(tool, dict) else None
|
|
|
|
|
|
def _anthropic_children(block, replacements):
|
|
children = replacements.get(block.get("id")) if block.get("type") == "tool_use" else None
|
|
if children is None:
|
|
return [block]
|
|
return [{**block, "id": coalesce_tool_call_id(child), "name": child.function.name,
|
|
"input": json.loads(child.function.arguments)} for child in children]
|
|
|
|
|
|
def _bedrock_children(block, replacements):
|
|
tool = block.get("toolUse")
|
|
children = replacements.get(tool.get("toolUseId")) if isinstance(tool, dict) else None
|
|
if children is None:
|
|
return [block]
|
|
return [{**block, "toolUse": {**tool, "toolUseId": coalesce_tool_call_id(child), "name": child.function.name,
|
|
"input": json.loads(child.function.arguments)}} for child in children]
|