fix(reply): stop models answering past an available tool
M0 on Strata caught both v2 models skipping a tool they had been given: the recommended model made up "24 degrees, sunny" instead of calling the weather tool, and the fast model said "timer set" without calling the timer tool (3 of 3 each). qwen2.5:7b does the same through the engine. - Prompt: TOOL_HONESTY_RULES in the persona (live facts only from a tool result this turn, actions only claimed after a tool reports success, no answer text next to a tool call), restated at the end of the system message by tool_honesty_reminder() with the turn's real tool names. - Engine: skipped-tool guard. When a native-tool model answers before any tool has run and the plan names a callable tool as a concrete call, the answer is dropped and the plan steps are direct-executed, then the model answers from the results. Only literal, schema-valid steps run (regex fast path, no LLM repair); otherwise the held-back answer is delivered. - Planner: the AVAILABLE TOOLS lines now carry argument names (planner_tool_catalog), so concrete steps use the tool's real keys. - Eval evals/test_tool_skip_confabulation.py; on qwen2.5:7b, 3 runs each, available-tool cases went from 0/12 to 9/12 calling the tool. - M0 kit: tools_fast / tools_recommended stages (run_m0_tools.bat) rerun tool calling before and after the fix on the real models, loading the production system_prompt.py and planner.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ from ..system_prompt import (
|
||||
build_system_prompt,
|
||||
load_agent_instructions,
|
||||
reply_language_directive,
|
||||
tool_honesty_reminder,
|
||||
)
|
||||
from ..tools.registry import run_tool_with_retries, generate_tools_description, generate_tools_json_schema, BUILTIN_TOOLS
|
||||
from ..tools.builtin.stop import STOP_SIGNAL
|
||||
@@ -33,6 +34,9 @@ from .planner import (
|
||||
progress_nudge,
|
||||
tool_steps_of,
|
||||
tool_names_in_plan,
|
||||
named_tool_steps,
|
||||
parse_concrete_tool_step,
|
||||
planner_tool_catalog,
|
||||
plan_has_unresolved_tool_steps,
|
||||
plan_requires_memory,
|
||||
strip_memory_directives,
|
||||
@@ -1241,15 +1245,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
debug_log("screen-share: site named — dropping webSearch so controlBrowser wins", "tools")
|
||||
|
||||
_planner_schema = generate_tools_json_schema(routed_tools, mcp_tools)
|
||||
_planner_tool_catalog: list[tuple[str, str]] = []
|
||||
for _schema in (_planner_schema or []):
|
||||
_fn = _schema.get("function", {}) if isinstance(_schema, dict) else {}
|
||||
if isinstance(_fn, dict):
|
||||
_nm = _fn.get("name")
|
||||
_desc = (_fn.get("description") or "").strip().splitlines()
|
||||
_first = _desc[0] if _desc else ""
|
||||
if _nm:
|
||||
_planner_tool_catalog.append((str(_nm), _first[:120]))
|
||||
_planner_tool_catalog = planner_tool_catalog(_planner_schema or [])
|
||||
|
||||
action_plan: list[str] = []
|
||||
try:
|
||||
@@ -1824,6 +1820,11 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
if _agent_instructions:
|
||||
guidance.append("Additional instructions from the operator:\n" + _agent_instructions)
|
||||
|
||||
# Recency reinforcement for the tool honesty rules (see
|
||||
# tool_honesty_reminder): placed after every context block so it is
|
||||
# the last instruction before the language lock.
|
||||
guidance.append(tool_honesty_reminder(list(allowed_tools)))
|
||||
|
||||
# Recency reinforcement: repeat the language lock at the very END too.
|
||||
# In a ~5k-token prompt the front-placed rule gets "lost in the middle";
|
||||
# bigger models (qwen2.5:7b) otherwise leak Chinese/Cyrillic mid-reply.
|
||||
@@ -2048,6 +2049,15 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
# to the steps of the current plan.
|
||||
_plan_steps_baseline = sum(1 for m in messages if m.get("tool_name"))
|
||||
|
||||
# Skipped-tool guard (native tool calling). Capable models sometimes answer
|
||||
# without calling a tool the planner already chose: a made-up "24 degrees
|
||||
# and sunny" instead of the weather tool (M0, 2026-11-10). When that happens
|
||||
# the engine drops the answer and direct-executes the plan steps that name
|
||||
# a callable tool (the same path small models use), then lets the model
|
||||
# synthesise from the real result. Fires at most once per reply; a
|
||||
# non-empty list means it fired. See planner.spec.md.
|
||||
_forced_plan_steps: list[str] = []
|
||||
|
||||
while turn < max_turns:
|
||||
turn += 1
|
||||
debug_log(f"🔁 messages loop turn {turn}", "planning")
|
||||
@@ -2065,12 +2075,12 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
# all plan tool steps are exhausted, at which point it synthesises
|
||||
# a final reply from the accumulated results.
|
||||
# See planner.spec.md.
|
||||
if (
|
||||
if _forced_plan_steps or (
|
||||
use_text_tools
|
||||
and len(action_plan) > 1
|
||||
and not _plan_under_specified
|
||||
):
|
||||
_plan_tool_steps = tool_steps_of(action_plan)
|
||||
_plan_tool_steps = _forced_plan_steps or tool_steps_of(action_plan)
|
||||
_tool_results_so_far = (
|
||||
sum(1 for m in messages if m.get("tool_name"))
|
||||
- _plan_steps_baseline
|
||||
@@ -2079,12 +2089,18 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
_plan_exec_handled = False
|
||||
try:
|
||||
_prior = list(invoked_tools_history)
|
||||
_resolved = _resolve_plan_step(
|
||||
cfg=cfg,
|
||||
next_step_text=_plan_tool_steps[_tool_results_so_far],
|
||||
prior_results=_prior,
|
||||
tools_schema=tools_json_schema or [],
|
||||
)
|
||||
if _forced_plan_steps:
|
||||
_resolved = parse_concrete_tool_step(
|
||||
_plan_tool_steps[_tool_results_so_far],
|
||||
tools_json_schema or [],
|
||||
)
|
||||
else:
|
||||
_resolved = _resolve_plan_step(
|
||||
cfg=cfg,
|
||||
next_step_text=_plan_tool_steps[_tool_results_so_far],
|
||||
prior_results=_prior,
|
||||
tools_schema=tools_json_schema or [],
|
||||
)
|
||||
if _resolved is not None:
|
||||
_name, _args = _resolved
|
||||
try:
|
||||
@@ -2107,6 +2123,20 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
and _name != "toolSearchTool"
|
||||
and _cand_sig not in recent_tool_signatures
|
||||
)
|
||||
# The skipped-tool guard overrides the model's own
|
||||
# choice, so it only runs a call whose arguments fit
|
||||
# the tool: a planner that mapped "timer" onto an
|
||||
# unrelated tool must not trigger that tool.
|
||||
_plan_args_err = (
|
||||
_validate_tool_args_against_schema(_name, _args, mcp_tools)
|
||||
if _forced_plan_steps and _plan_exec_ok else None
|
||||
)
|
||||
if _plan_args_err:
|
||||
debug_log(
|
||||
f"skipped-tool guard: {_name} args rejected ({_plan_args_err})",
|
||||
"planning",
|
||||
)
|
||||
_plan_exec_ok = False
|
||||
if _plan_exec_ok:
|
||||
debug_log(
|
||||
f"planner: direct-executing plan step "
|
||||
@@ -2179,22 +2209,33 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
)
|
||||
_plan_text = f"Error: {_plan_err}"
|
||||
_plan_tool_results_after = _tool_results_so_far + 1
|
||||
if action_plan:
|
||||
if action_plan and use_text_tools:
|
||||
_plan_hint = progress_nudge(
|
||||
action_plan,
|
||||
_plan_tool_results_after,
|
||||
)
|
||||
else:
|
||||
_plan_hint = ""
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": (
|
||||
f"[Tool result: {_name}]\n"
|
||||
f"{_plan_text}{_plan_hint}"
|
||||
),
|
||||
"tool_name": _name,
|
||||
"tool_failed": not _plan_result.success,
|
||||
})
|
||||
if use_text_tools:
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": (
|
||||
f"[Tool result: {_name}]\n"
|
||||
f"{_plan_text}{_plan_hint}"
|
||||
),
|
||||
"tool_name": _name,
|
||||
"tool_failed": not _plan_result.success,
|
||||
})
|
||||
else:
|
||||
# Native protocol: answer the assistant
|
||||
# tool_calls message with a role=tool result.
|
||||
messages.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": _plan_call_id,
|
||||
"tool_name": _name,
|
||||
"content": f"{_plan_text}{_plan_hint}",
|
||||
"tool_failed": not _plan_result.success,
|
||||
})
|
||||
recent_tool_signatures.append(_cand_sig)
|
||||
if len(recent_tool_signatures) > 5:
|
||||
recent_tool_signatures = (
|
||||
@@ -2218,6 +2259,16 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
)
|
||||
if _plan_exec_handled:
|
||||
continue
|
||||
if _forced_plan_steps and _tool_results_so_far == 0:
|
||||
# The guard could not run the planned tool after all
|
||||
# (unresolvable step or arguments that do not fit). Fail
|
||||
# open: deliver the answer it held back rather than ask
|
||||
# the model again on the same messages.
|
||||
debug_log("skipped-tool guard: planned tool not runnable, keeping the model answer", "planning")
|
||||
messages.append(_guard_held_message)
|
||||
reply = _guard_held_reply
|
||||
last_candidate_reply = _guard_held_reply
|
||||
break
|
||||
|
||||
# Update the system message with fresh context (time/location) before each LLM call
|
||||
# Note: We update the first system message rather than appending a new one because
|
||||
@@ -2693,6 +2744,30 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
candidate_reply = content
|
||||
malformed_fallback = False
|
||||
|
||||
_guard_steps = [] if (use_text_tools or _forced_plan_steps or malformed_fallback) else (
|
||||
named_tool_steps(
|
||||
action_plan,
|
||||
[n for n in allowed_tools if n not in ("stop", "toolSearchTool")],
|
||||
)
|
||||
)
|
||||
if (
|
||||
_guard_steps
|
||||
and sum(1 for m in messages if m.get("tool_name")) == _plan_steps_baseline
|
||||
):
|
||||
# The model answered without calling the tool the plan chose, so
|
||||
# the answer can only be a guess. Drop it (the synthesis turn must
|
||||
# not anchor on it) and run the planned steps directly.
|
||||
_guard_held_message = messages.pop()
|
||||
_guard_held_reply = candidate_reply
|
||||
_forced_plan_steps = _guard_steps
|
||||
debug_log(
|
||||
f"skipped-tool guard: model answered without the planned tool; "
|
||||
f"direct-executing {len(_guard_steps)} plan step(s)",
|
||||
"planning",
|
||||
)
|
||||
print(" 🧭 Planned tool skipped by the model, running it directly", flush=True)
|
||||
continue
|
||||
|
||||
reply = candidate_reply
|
||||
last_candidate_reply = candidate_reply
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user