Add tool-call support to LLMAgent.stream_message #67
Closed
opened 2026-07-02 06:42:57 +00:00 by hurui200320
·
1 comment
No Branch/Tag Specified
Labels
Clear 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
Needs Feedback
Signed-off: Owner
Signed-off: Scrum Master
Signed-off: Tech Lead
Spike
PR blocked by an open issue dependency. Operator must close the dep (or remove the dependency link) before the merge driver can act. Auto-cleared by merge_drive when no open deps remain.
Most recent merge cycle hit CI timeout. Driver excludes this PR while last merge_cycle row is < 30 min old; label persists thereafter as visible history.
Currently being processed by an implementer worker.
Currently being processed by the merge driver.
Currently being processed by a reviewer worker.
Merge driver heartbeat stale; pipeline halted. Closed automatically on next clean tick.
Detected master commit violating the strict merge invariant. Tracked as an issue (not a PR label); kept here for label completeness.
In-cycle escalation: most recent attempt ran at the Tier 0 slot (`tier-0`). Slot's model defined in .opencode/models/tiers.yaml.
In-cycle escalation: most recent attempt ran at the Tier 1 slot (`tier-1`). Slot's model defined in .opencode/models/tiers.yaml.
In-cycle escalation: most recent attempt ran at the Tier 2 slot (`tier-2`). Slot's model defined in .opencode/models/tiers.yaml. Gated behind IMPLEMENTER_ESCALATION_TIER2_ENABLED.
In-cycle escalation: most recent attempt ran at the Tier -1 slot (`tier-min`). Slot's model defined in .opencode/models/tiers.yaml. Suffix is ``-min`` (not ``--1``) so the Forgejo UI reads naturally.
Tracking issues used by the AI Automation system for agents to communicate and report.
Rebase conflict needs LLM conflict-resolver.
Failing CI needs implementer attention.
Documenting a driver incident or rollback.
Reviewer has APPROVED this PR and no later REQUEST_CHANGES is outstanding. The merge driver requires this label to even consider a PR for merging. Set by the reviewer worker on APPROVE; cleared on REQUEST_CHANGES.
Train repeatedly lost master-tempo races. Driver excludes via merge_cycle until cooldown elapses; label persists as visible history.
Revert PR backing out an invariant violation. Fast-tracked through the merge driver.
Sentinel PR duplicated from upstream into a personal fork by tools/duplicate_prs_to_fork.py for pipeline testing. Lives only in the fork; the canonical pipeline never sees it.
No implementer activity for N days. Flagged for human review. Auto-cleared on next push to head branch.
Repeatedly fails on current master (>= 3 ci-fail-on-rebased-sha releases in 12 h). Excluded from driver until human triage.
A ticket in a blocked state and unable to complete until some other task is completed first.
Bounty
$100
A bounty of $100 for any open-source contributor who provides a MR that solves this issue
Bounty
$1000
A bounty of $1000 for any open-source contributor who provides a MR that solves this issue
Bounty
$10000
A bounty of $10000 for any open-source contributor who provides a MR that solves this issue
Bounty
$20
A bounty of $20 for any open-source contributor who provides a MR that solves this issue
Bounty
$2000
A bounty of $2000 for any open-source contributor who provides a MR that solves this issue
Bounty
$250
A bounty of $250 for any open-source contributor who provides a MR that solves this issue
Bounty
$50
A bounty of $50 for any open-source contributor who provides a MR that solves this issue
Bounty
$500
A bounty of $500 for any open-source contributor who provides a MR that solves this issue
Bounty
$5000
A bounty of $5000 for any open-source contributor who provides a MR that solves this issue
Bounty
$750
A bounty of $750 for any open-source contributor who provides a MR that solves this issue
MoSCoW
Could have
Could have feature in order to satisfy the epic/legendary.
MoSCoW
Must have
Must have feature in order to satisfy the epic/legendary.
MoSCoW
Should have
Should have feature in order to satisfy the epic/legendary.
There are questions in the ticket that can not be completed until the project owner provides clarity.
Points
1
1 man-hours worth of work for an expert with no learning curve.
Points
13
13 man-hours worth of work for an expert with no learning curve.
Points
2
2 man-hours worth of work for an expert with no learning curve.
Points
21
21 man-hours worth of work for an expert with no learning curve.
Points
3
3 man-hours worth of work for an expert with no learning curve.
Points
34
34 man-hours worth of work for an expert with no learning curve.
Points
5
5 man-hours worth of work for an expert with no learning curve.
Points
55
55 man-hours worth of work for an expert with no learning curve.
Points
8
8 man-hours worth of work for an expert with no learning curve.
Points
88
88 man-hours worth of work for an expert with no learning curve.
Priority
Backlog
This ticket has backlogged priority and is not to be worked on yet
Priority
CI Blocker
Critical priority issue that blocks CI/CD pipeline and prevents PR merges
Priority
Critical
The priority is critical
Priority
High
The priority is high
Priority
Low
The priority is low
Priority
Medium
The priority is medium
When an epic or legendary is in review it must be signed off by owner, tech lead, and scrum master before being marked as completed.
When an epic or legendary is in review it must be signed off by owner, tech lead, and scrum master before being marked as completed.
When an epic or legendary is in review it must be signed off by owner, tech lead, and scrum master before being marked as completed.
A ticket for learning a tool or technology that is needed to be able to do future planning and design.
State
Completed
The ticket has been fully implemented, completed, and merged with the source code. This label should only be applied once a ticket is closed.
State
Duplicate
A ticket that represents the same content as an existing ticket.
State
In Progress
A ticket that is actively being developed.
State
In Review
A ticket that has had some code completed to implement but is waiting to pass peer review and is not yet merged in.
State
Paused
This ticket's work started but wasn't finished. It's on hold (likely in a feature branch) and will be resumed later, either due to a blocker or a delay.
State
Unverified
All new tickets start in this state. A developer may set it to show the ticket is unverified. This means we haven't agreed to work on it. It will either move to a verified state or be closed as wontdo.
State
Verified
The issue has been verified by a developer as legitimate. It will be worked on and verified tickets are now considered part of the backlog.
State
Wont Do
This ticket has been decided it wont be done. This may mean the bug has been determined to not be real (cant verify) or the feature is one we have decided we dont want to adopt.
Type
Automation
Any edits or discussion about the AI automated coding system.
Type
Bug
Something that doesnt work as intended.
Type
Discussion
Anytime a ticket represents a discussion about a subject and doesnt fall into one of the other categories.
Type
Documentation
An error or improvement needed in the documentation.
Type
Epic
Any first tier epic. That is, an epic which contains only issues as children and will not have sub-epics.
Type
Feature
Some new functionality not present.
Type
Legendary
A type of Epic which will contain other Epics.
Type
Refactor
A code change that restructures existing code without changing its external behavior.
Type
Support
Someone needs help using the project.
Type
Task
A generic task that doesnt fit into the other type categories.
Type
Testing
Work exclusively focusing on fixing or expanding testing.
Projects
Clear projects
No project
Assignees
aditya (Aditya Chhabra)
aleenaumair (Aleena Umair)
brent.edwards (Brent Edwards)
CoreRasurae (Luis Mendes)
drew (Drew Morris)
eugen.thaci (Eugen Thaci)
freemo (Jeffrey Phillips Freeman)
HAL9000 (HAL 9000)
HAL9001 (HAL9001)
hamza.khyari (Hamza Khyari)
hurui200320 (Rui Hu)
justin.morris
khird (Kyle Hird)
org.cleveragents
Clear assignees
No Assignees
hurui200320
Notifications
Due Date
No due date set.
Depends on
#68 feat(agents): add tool-call support to LLMAgent.stream_message
cleveragents/cleveractors-core
Reference: cleveragents/cleveractors-core#67
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Delete Branch "%!s()"
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?
Metadata
feat(agents): add tool-call support to LLMAgent.stream_messagefeature/m2-llm-agent-stream-tool-callsBackground
LLMAgentexposes two execution paths:process_message()(blocking, usesainvoke()) andstream_message()(streaming, usesastream()). These two paths have diverged in capability:process_message()has a fully working multi-turn tool-call loop — it passestoolsto the LLM, detectstool_callson every response, dispatches to ephemeralToolAgentinstances, appendsToolMessages, re-invokes up totool_max_rounds, handles token-budget exhaustion, runs a synthesis prompt when the model is stuck in tool-only mode, and supports tool-output pruning.stream_message()has none of this.The fix is not to duplicate the tool-call logic into
stream_message()— that would split one authoritative implementation into two and create a permanent maintenance burden. Instead, the tool-call loop should be extracted fromprocess_message()into a shared private helper,_execute_tool_loop(), that both paths call.process_message()is refactored to delegate to the helper (no change in observable behaviour).stream_message()calls the same helper for tool-using actors (yielding the complete response as a single content chunk) and retains its existingastream()path unchanged for actors that declare no tools.The single-chunk behaviour for tool-using actors is an accepted trade-off: when tools are involved, the user already waits for tool execution to complete before the final answer begins, so yielding that answer as a single chunk rather than token-by-token has minimal UX impact. The alternative — calling the LLM a second time with
astream()after the helper has already produced the final response withainvoke()— would double the LLM cost of every tool-using request and is not acceptable in a production billing context.The cleveragents-webapp has a temporary router-side workaround in PR #328 (
actor_config_has_tools+ fallback toexecutor.execute()in_execute_via_cleveractors_streamand_generate_actor_sse) that silently degrades to non-streaming for tool-declaring actors. That workaround can be removed from the webapp once this issue is resolved.Current Behaviour
Calling
executor.execute_stream(message)on an actor whose LLM agent declarestoolsyields an empty response or plain-text hallucination.stream_message()callsself.chat_model.astream(lc_messages)without passingtools, so the LLM receives no tool schemas and emits no tool calls. The stream completes immediately with no useful output.Additionally,
process_message()contains the entire multi-turn tool-call loop inline (~200 lines), with no separation between the "tool orchestration" concern and the "final text retrieval" concern, making the method hard to unit-test in isolation and impossible to reuse from other callers.Expected Behaviour
A new private async method
_execute_tool_loop()onLLMAgentencapsulates the complete tool-call orchestration: passingtoolsto the LLM, running the multi-turn loop viaainvoke(), dispatching tool calls through ephemeralToolAgentinstances, accumulating token counts across all rounds, handling token-budget exhaustion and the synthesis prompt, and running the tool-output pruning pass. It returns a result object containing the finalAIMessageresponse and the accumulated(prompt_tokens, completion_tokens).process_message()delegates to_execute_tool_loop()for the tool-call rounds and extractsstr(final_response.content)from the result. Observable behaviour is identical to today.stream_message()branches on whether the actor declares tools:astream()path runs unchanged — real token-by-token streaming is preserved._execute_tool_loop()runs all rounds viaainvoke();stream_message()then yields the final response text as a single content chunk and sets_last_token_usage/last_token_usage_varfrom the accumulated token counts returned by the helper.All billing-integrity guarantees (populating
last_token_usage_varand the_captured_prompt/_captured_completionsentinels on exception paths) are met for both callers of_execute_tool_loop().Acceptance Criteria
executor.execute_stream(message)on a tool-declaring single-LLM actor yields at least one non-empty string token andexecutor.last_resultreports non-zero accumulated token counts covering all tool-call rounds.execute_streamyields the final answer andexecutor.last_result.prompt_tokensequals the sum across all three invocations (two tool rounds + final).tool_max_roundsis exhausted, the generator terminates cleanly andexecutor.last_resultis populated.stream_message()on an actor with no tools configured behaves identically to the current implementation — realastream()token-by-token streaming, same token counts.process_message()behaviour is unchanged for both tool-using and non-tool actors.ConfigurationError, LangChain error),executor.last_resultis populated with accumulated token counts before the exception propagates._execute_tool_loop()has dedicated Behave coverage for each of its internal branches, independently ofprocess_message()andstream_message().nox -s typecheckreports zero errors and zero warnings on all changed files.nox -s coverage_reportreports ≥ 97% coverage.Supporting Information
src/cleveractors/agents/llm.py.process_message()(lines ~831–1303). It is tightly coupled to several local variables (_accumulated_prompt,_captured_prompt,_budget_exhausted,messages,response); the extraction must design a clean return type (a private dataclass or named tuple) that surfaces all state needed by both callers without leaking internal implementation details._captured_prompt/_captured_completion) inprocess_message()andstream_message()must be updated to read accumulated counts from the helper's return value rather than re-reading local variables.actor_config_has_tools+stream_fallback_to_non_streambranch in PR cleveragents/cleveragents-webapp#328 (src/cleverthis/api/routes/v1_chat.pyandsrc/cleverthis/services/chat_stream.py).process_message()refactor ~2d,stream_message()integration ~0.5d, Behave scenarios ~1.5d, Robot test ~0.5d, coverage + PR ~0.5d).Subtasks
_execute_tool_loop(): a private dataclass (e.g._ToolLoopResult) carryingfinal_response,accumulated_prompt,accumulated_completion, and any flags needed by callers (budget_exhausted,synthesis_was_run)._execute_tool_loop()fromprocess_message(): move the multi-turnainvoke()loop, per-toolToolAgentdispatch, token accumulation, budget exhaustion path, synthesis prompt flow, and tool-output pruning pass into the new method._execute_tool_loop()must populate its result correctly on both success and exception paths.process_message()to call_execute_tool_loop()and deriveresponse_textfromresult.final_response.content. All existing tests must continue to pass without modification.stream_message()to call_execute_tool_loop()whenself._lc_tools is not None, yieldstr(result.final_response.content)as a single content chunk, and set_last_token_usage/last_token_usage_varfromresult.accumulated_prompt/result.accumulated_completion. The existing no-toolsastream()branch must remain untouched._captured_prompt/_captured_completionbilling-integrity sentinels in both callers to correctly reflect accumulated counts from the helper on exception paths._execute_tool_loop()internals: basic single tool round; multi-round (two consecutive tool-call rounds);tool_max_roundsexhaustion; token accumulation spanning all rounds; token-budget exhaustion (synthesis path runs); model stuck in tool-only mode (synthesis prompt flow); tool-output pruning (pass runs and token costs included); tool dispatch error (ExecutionErrorcaptured asToolMessage, loop continues); exception path (partial token counts preserved in result).stream_message()integration: tool-using actor yields one non-empty chunk andlast_resulttoken counts equal helper's accumulated totals; no-tools actor behaviour unchanged (real astream tokens).process_message()regression: confirm existing tool-calling scenarios continue to pass after refactor (no new scenarios required if existing coverage is sufficient).execute_stream()on an actor with a tool config (stub provider; no real API key required).nox -s coverage_reportand confirm ≥ 97%.nox(all default sessions) and confirm all pass with zero errors.Definition of Done
This issue is complete when:
master, reviewed, and merged.Implementation Notes — commit
306f114Design decisions
_ToolLoopErrorfor billing integrity across the extraction boundaryThe key challenge in extracting
_execute_tool_loop()was preserving billing integrity: the callers (process_messageandstream_message) need to know the partial accumulated token counts if the helper raises before returning. Since Python has no output parameters, I defined a private_ToolLoopErrorexception that wraps the original exception and carriesaccumulated_prompt,accumulated_completion, andany_invocation_made. Callers catch this, set their_captured_prompt/_captured_completionsentinels, then re-raisecause from None. The outer exception handlers (except ConfigurationError,except LangChainException,except Exception) then process the re-raised original exception normally. This preserves all billing-integrity guarantees from the original implementation.Single-chunk output for
stream_message()with toolsWhen tools are configured,
stream_message()awaits_execute_tool_loop()and yields the complete final response as one chunk. The alternative (callingastream()again afterainvoke()produced the final answer) would double LLM cost for every tool-using request. The accepted trade-off is documented in the issue background and in the commit message. Real token-by-tokenastream()is preserved unchanged for no-tool actors.messageslist modified in-place_execute_tool_loop()takesmessages: list[Any]and appendsAIMessage+ToolMessageobjects as the conversation progresses. This mirrors the original inline behavior. Both callers build the initial[SystemMessage, HumanMessage]list and pass it to the helper, which grows it through tool rounds.tool_max_roundsparsing moved into the helperPreviously
tool_max_roundswas parsed at the top ofprocess_message()before theif _has_toolscheck (so it would raiseConfigurationErrorfor invalid values even for no-tool agents). After extraction, parsing happens inside_execute_tool_loop(), which is only called for tool-configured agents. This is a minor behavior change (no-tool agents with invalidtool_max_roundsno longer raise at call time), but there are no tests for that case and it's not a specified behavior.Key code locations
_ToolLoopResult:src/cleveractors/agents/llm.py, module level, afterChatOpenAIglobals_ToolLoopError: same file, module level, after_ToolLoopResult_execute_tool_loop():LLMAgent._execute_tool_loop, after_run_pruning_passprocess_message()delegation:LLMAgent.process_message, ~15-line block replacing 490+ linesstream_message()tools branch:LLMAgent.stream_message, before the existingastream()loopTests summary
features/llm_agent_tool_loop.feature+features/steps/llm_agent_tool_loop_steps.py: 10 Behave scenarios covering_execute_tool_loop()independentlyfeatures/llm_agent_stream_tool_calls.feature+features/steps/llm_agent_stream_tool_calls_steps.py: 7 Behave scenarios forstream_message()with toolsrobot/llm_tool_calling.robot: 2 new Robot integration tests (stream loop + multi-round token accumulation)llm_agent_tool_calling.featurescenarios pass unmodifiedQuality gates (commit
306f114)Follow-up note (out of scope)
The webapp workaround
actor_config_has_tools+stream_fallback_to_non_streamincleveragents/cleveragents-webapp#328can now be removed. This is webapp work tracked in the issue description under Supporting Information.