fix: trace tool forwarding decisions

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
mmc
2026-04-29 06:12:21 +08:00
parent 3c9d419726
commit d9fec3fd74
4 changed files with 168 additions and 0 deletions

View File

@@ -45,6 +45,31 @@ def _resolve_ask_mode(model: str, has_tooling_context: bool, *, default_ask_mode
return default_ask_mode
def _tool_config_summary(tool_config: dict[str, Any] | None) -> dict[str, Any]:
if not isinstance(tool_config, dict):
return {"present": False, "provider": None, "tool_names": [], "tool_choice": None}
tools = tool_config.get("tools")
tool_names: list[str] = []
if isinstance(tools, list):
for tool in tools:
if not isinstance(tool, dict):
continue
if tool.get("type") == "function":
fn = tool.get("function")
if isinstance(fn, dict) and isinstance(fn.get("name"), str) and fn.get("name").strip():
tool_names.append(fn.get("name").strip())
continue
name = tool.get("name")
if isinstance(name, str) and name.strip():
tool_names.append(name.strip())
return {
"present": True,
"provider": tool_config.get("provider"),
"tool_names": tool_names,
"tool_choice": tool_config.get("tool_choice"),
}
async def _apply_cached_instance_or_invalidate(
*,
protocol: str,
@@ -91,6 +116,14 @@ async def prepare_execution_context(
has_tooling_context,
default_ask_mode=default_ask_mode,
)
logger.info(
"%s.prepare requested_model=%s ask_mode=%s tooling=%s tool_config=%s",
protocol,
requested_model,
ask_mode,
has_tooling_context,
_tool_config_summary(tool_config),
)
reuse_eligible = (
session_cache.enabled
@@ -152,6 +185,17 @@ async def prepare_execution_context(
prompt = messages_to_prompt(messages_dump)
is_reply = False
logger.info(
"%s.context inst=%s model=%s ask_mode=%s reuse_eligible=%s reused_session=%s affinity=%s",
protocol,
inst.name,
model,
ask_mode,
reuse_eligible,
bool(cached_session_id),
affinity,
)
return ExecutionContext(
ask_mode=ask_mode,
lookup_key=lookup_key,
@@ -219,6 +263,13 @@ async def complete_execution(
estimate_tokens: Callable[[str], int],
) -> CompletedExecution:
try:
logger.info(
"%s.complete inst=%s ask_mode=%s tool_config=%s",
protocol,
execution.inst.name,
execution.ask_mode,
_tool_config_summary(tool_config),
)
result = await execution.inst.client.chat_complete(
execution.prompt,
execution.model,