Browse Source

[fix] train_agent: distinguish unknown tools from tool execution failures

rollout_single() built the model-visible observation as

    result_str = json.dumps(result) if result else '{"error": "tool not found"}'

so every falsy return from execute_tool() -- which included every exception
raised by a tool body -- was reported to the policy model as "tool not found".

CHECK_ARGS already guarantees the tool name and argument shape before execution,
so a raise here is a real execution failure (division by zero, math domain
error, non-numeric value), not a lookup miss, and the model was never shown the
actual reason. execute_tool() now returns the error object itself, matching the
observation format the repo already ships in scripts/eval_toolcall.py
({"error": "未知工具: ..."} / {"error": "工具执行失败: ..."}), and rollout_single()
serialises the returned payload verbatim.

Falsy-valued payloads from a tool (e.g. {"value": 0}) are no longer replaced by
the error string either.

Verified on the unmodified base 3eee569 and on this branch with the same
16-check script: 4 checks fail before, all pass after. Successful calls,
the six-tool registry, the falsy-payload path and calculate_rewards() output
are unchanged (reward +0.467 both before and after).
dajiaohuang 2 days ago
parent
commit
b9cfefc111
1 changed files with 4 additions and 4 deletions
  1. 4 4
      trainer/train_agent.py

+ 4 - 4
trainer/train_agent.py

@@ -85,11 +85,11 @@ def parse_tool_calls(text):
 
 def execute_tool(name, args):
     fn = MOCK_RESULTS.get(name)
-    if not fn: return None
+    if not fn: return {"error": f"未知工具: {name}"}
     try:
         return fn(args)
-    except Exception:
-        return None
+    except Exception as e:
+        return {"error": f"工具执行失败: {str(e)[:80]}"}
 
 # ======== 多轮 Rollout ========
 def rollout_single(rollout_engine, tokenizer, messages, tools, max_turns=3, max_new_tokens=256, thinking_ratio=0.5, device="cuda"):
@@ -137,7 +137,7 @@ def rollout_single(rollout_engine, tokenizer, messages, tools, max_turns=3, max_
                 except: raw = {}
             if not isinstance(raw, dict): raw = {}
             result = execute_tool(name, raw)
-            result_str = (json.dumps(result, ensure_ascii=False) if result else '{"error": "tool not found"}')[:2048]  # 防止天文数字撑爆tokenizer
+            result_str = json.dumps(result, ensure_ascii=False)[:2048]  # 防止天文数字撑爆tokenizer
             messages.append({"role": "tool", "content": result_str})
 
         marker = f"<|agent_observation_{id(messages)}_{len(response_ids)}|>"