fix(agents): validate tool name against declared tools list before dispatch #64
No reviewers
Labels
No labels
auto/blocked-by-deps
auto/ci-timeout
auto/claimed-implementer
auto/claimed-merge
auto/claimed-reviewer
auto/driver-down
auto/invariant-violation
auto/last-attempt-tier-0
auto/last-attempt-tier-1
auto/last-attempt-tier-2
auto/last-attempt-tier-min
Automation Tracking
auto/needs-conflict-resolution
auto/needs-implementer
auto/postmortem
auto/ready-to-merge
auto/restart-throttled
auto/revert
auto/sentinel
auto/stale-inactivity
auto/unstable
Blocked
Bounty
$100
Bounty
$1000
Bounty
$10000
Bounty
$20
Bounty
$2000
Bounty
$250
Bounty
$50
Bounty
$500
Bounty
$5000
Bounty
$750
MoSCoW
Could have
MoSCoW
Must have
MoSCoW
Should have
Needs Feedback
Points
1
Points
13
Points
2
Points
21
Points
3
Points
34
Points
5
Points
55
Points
8
Points
88
Priority
Backlog
Priority
CI Blocker
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Signed-off: Owner
Signed-off: Scrum Master
Signed-off: Tech Lead
Spike
State
Completed
State
Duplicate
State
In Progress
State
In Review
State
Paused
State
Unverified
State
Verified
State
Wont Do
Type
Automation
Type
Bug
Type
Discussion
Type
Documentation
Type
Epic
Type
Feature
Type
Legendary
Type
Refactor
Type
Support
Type
Task
Type
Testing
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Blocks
#63 fix(agents): validate LLMAgent tool dispatch against declared tools list
cleveragents/cleveractors-core
Reference
cleveragents/cleveractors-core!64
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "bugfix/m2-llmagent-tool-dispatch-validation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Validates that the model-synthesized tool names in the LLM synthesis round match the declared tools list, preventing crashes or silent failures when the model hallucinates an undeclared tool name.
Fixes an additional bug in
ToolAgent._execute_tool()where the containment checktool_name not in self.toolsalways failed becauseself.toolsis a heterogeneous list of strings and dicts — a string tool name like"file_read"would never match. Fixed by extracting only string entries for the comparison.Changes
src/cleveractors/agents/llm.py_declared_namesset fromself._lc_tools; reject undeclared tool calls withToolMessageerrorsrc/cleveractors/agents/tool.pystring_tool_namesfrom mixed list for correct dispatch checkCloses #63
Code Review — PR #64
Fixes look correct and well-scoped. A few notes:
llm.py— synthesis-round validation ✅Building
_declared_namesas a set fromself._lc_toolsis the right pattern: O(1) lookup, and gracefully handles the hallucinated-name case by returning aToolMessageerror rather than crashing. Thet.get("function", {}).get("name", "")extraction matches the standard OpenAI/LangChain tool schema correctly.Minor nit:
_declared_namesis built unconditionally, before theif isinstance(synth_tool_calls, list) and synth_tool_calls and _has_tools:gate. It runs even when there are no tool calls — not a bug, but a small waste. Could move it inside theifblock. Low priority.tool.py—_execute_tooldispatch check ✅The fix (splitting
string_tool_namesfrom the heterogeneousself.toolslist) is cleaner and more explicit than the originaltool_name not in self.toolscheck. The original actually did work correctly in Python (a string==a dict is alwaysFalse, so theand dict_tool_namesfallback caught the dict-defined tools), but the refactored version makes the intent unambiguous. Good call.Micro nit:
string_tool_namesis alist, sotool_name not in string_tool_namesis O(n). For typical tool list sizes this is negligible — just flagging it in case the list ever grows large.Blank-line additions in
tool.pyPure style — no functional change.
CI
All checks still pending at time of review. Nothing to block on from the code side.
Verdict: Logic is sound, both fixes are correct, CHANGELOG entries are accurate. Ready to merge once CI is green.
cc @rui.hu
PR Review: !64 (Ticket #63)
Verdict: Approve
The synthesis-branch guard is implemented correctly, and the
tool.pyrefactor is a reasonable clarity improvement. The PR author has explained (in a comment on ticket #63) why they did not extend the guard to the other two tool-dispatch branches: the_execute_toolsafeguard already rejects undeclared tool names, and the surroundingexcept (ExecutionError, ConfigurationError)block inllm.pyalready converts that into aToolMessagefor the LLM. The new_declared_namescheck is therefore a UX refinement for clearer LLM feedback, applied where it matters most. This reasoning is technically sound and should be reflected back into the ticket (see Summary).Critical Issues
None.
Major Issues
None.
Minor Issues
tool.pychange as a bug fix.src/cleveractors/agents/tool.py, lines 256–259tool_name not in self.toolsalways failed becauseself.toolsis a heterogeneous list of strings and dicts — a string tool name like"file_read"would never match." This claim is not quite accurate — the original checktool_name not in self.tools and tool_name not in dict_tool_namesdid function correctly, because Python'sinoperator returnsFalsefor any string-vs-dict equality, causing thedict_tool_namesfallback to handle dict-typed tools. The author's own ticket comment ("it just needs to be adjusted to...") implicitly acknowledges this — they propose simplifying toif tool_name not in dict_tool_names:, but the actual PR keeps both checks. Graa's review on the PR also notes the original code worked.Nits
_declared_namesbuilt outside its gate inllm.py(src/cleveractors/agents/llm.py:1058-1060): the set comprehension runs even when there are nosynth_tool_calls. Cost is trivial; could be moved inside theif isinstance(synth_tool_calls, list) and synth_tool_calls and _has_tools:gate for symmetry with the rest of the block. (Already flagged by Graa.)string_tool_namesis alist, not aset(src/cleveractors/agents/tool.py:256):tool_name not in string_tool_namesis O(n). Negligible at typical tool counts (≤10); a set would also make the two checks symmetric in style.tool.py(lines 94, 200): cosmetic only.Summary
The synthesis-branch fix is correct: it builds
_declared_namesfromself._lc_toolsusing the OpenAI/LangChain tool-schema convention (t.get("function", {}).get("name", "")), appends aToolMessagewith the same error wording the ticket suggests, and usescontinueto keep the loop alive so the LLM can recover. Thetool.pyrefactor splits the heterogeneousself.toolslist intostring_tool_namesanddict_tool_names, making the dispatch check explicit.The PR does not strictly satisfy ticket AC #3 ("the guard is applied consistently in all three tool-dispatch branches"), but the PR author has provided a sound technical justification in the ticket comments for limiting the new check to the synthesis branch: the existing
ToolAgent._execute_toolsafeguard + the surroundingexcept ExecutionErrorhandler already prevent undeclared tool execution and deliver error feedback to the LLM in the other two branches. The new check is therefore a UX refinement for clearer LLM feedback, applied where it provides the most value (the synthesis round, where the model is being explicitly prompted to recover).Recommended follow-up (not a blocker): Reconcile the ticket with the implementation. Either:
tool.pyalready prevents the bug in the other branches, and the new check is a UX refinement), or_declared_namesguard in the main loop and budget-exhausted branches for error-message consistency.The existing review from Graa on the PR approves the fix and concurs on the nits above.
bb3decbd964642d49a864642d49a86bcce5dc42a