[Bug]: Tool calls truncated by the output token limit are still executed in the non-chat agentic loops (Deep Research, Question Generator, Explore Context, PageIndex)
Do you need to file an issue?
- [x] I have searched the existing issues and this bug is not already filed.
- [x] I believe this is a legitimate bug, not just a question or feature request.
Related Module
API/Backend (affects Deep Research, Question Generator, Explore Context, and the PageIndex RAG pipeline)
Describe the bug
Follow-up to #2713. That issue's chat-loop half is fixed by PR #2719 (open as of this filing, one commit on dev tip 4c1e000d8): AgentLoopHost.dispatch_tools now refuses a tool batch when the round ended in a truncation finish (length / max_tokens / max_output_tokens). I verified that PR locally (review comment).
However, AgentLoopHost.dispatch_tools is only one of six production call sites that execute tool calls. The other five dispatch without any truncation check, so a tool call the model never finished writing can still be repaired and executed there — the same hazard #2713 describes for chat.
The five unguarded sites (all on dev tip 4c1e000d8):
| Site | How it gets there | Truncation check |
|---|---|---|
runtime/agentic/loop.py:322 (run_agentic_loop) |
Generic loop calls host.dispatch_tools(...) with step.tool_calls |
None — step.finish_reason is available on the LabeledStepResult in hand but never consulted |
agents/research/pipeline.py:2780 and :3168 |
Deep Research's dispatch_tools host impls (called from the generic loop at pipeline.py:815 / :1017) |
Only MAX_PARALLEL_TOOL_CALLS; truncation is validated for report parts (:2435) but not for tool rounds |
agents/question/pipeline.py:2099 |
Question Generator's own loop → wrapper at :2174 → base dispatch |
None |
capabilities/explore_context/explorer.py:363 |
Explorer's own loop: dispatch_tool_calls(tool_calls=result.tool_calls, ...) |
None |
services/rag/pipelines/pageindex/reasoning.py:90 |
PageIndex reasoning's dispatch_tools host impl, driven by the generic loop |
None |
The underlying layer does not protect them: dispatch_tool_calls (runtime/agentic/tool_dispatch.py) deliberately repairs malformed JSON arguments and runs the tool. The stream reader (runtime/agentic/labeled_step.py) accumulates tool-call deltas verbatim and hands finish_reason to the caller — its docstring says report writers use it, i.e. truncation handling is left to the layer above, which only chat has.
Steps to reproduce
The failure mode is provider-independent (any model that can hit an output cap mid-tool-call). Minimal repro of the repair-then-run behavior at the shared dispatch layer, on dev tip 4c1e000d8:
- Register a real tool (
obsidian_appendworks; any tool with structured args) in a freshToolRegistry. - Call
dispatch_tool_callswith one call whoseargumentsstring is cut mid-JSON (e.g.'{"note": "Calculus/Week 7", "content": "## Integration\\n- line one\\n- line two\\n- line three cut o'), simulating a round that hitfinish_reason: lengthmid-arguments. - The tool runs against the repaired (partial) arguments and returns success. In my run the vault note was mutated with the half-written content and the tool message reported
{"status": "appended"}— the model never wrote that text.
To reach the five sites end to end, run Deep Research / Question Generator / an Explore Context turn / PageIndex indexing with a max_tokens low enough that a model truncates mid-tool-call: the truncated batch dispatches, the (fabricated) tool result is appended to the loop history as role: "tool", and downstream rounds reason over it. For contrast, the same scenario in chat on PR #2719 is refused with "Model output was truncated. No tools in this batch were executed."
Expected Behavior
Same policy as the chat loop gets from #2719: when a round's finish reason is a truncation reason, the tool batch from that round must not be executed. The flow should retry the round (or degrade gracefully per flow), instead of executing tool calls with arguments the model never completed writing and appending their results to the loop history.
Notes on impact and fix shape
- Tool sets differ per flow, and Deep Research / Explore Context / PageIndex tools are predominantly retrieval/read — so the concrete cost is usually not data mutation but fabricated tool results (repaired partial args → successful-looking result) entering the history that later rounds synthesize from.
- The natural choke point is
run_agentic_loopitself (runtime/agentic/loop.py:322): checkfinish_was_truncated(step.finish_reason)beforehost.dispatch_tools(...)and return the same style of refusal notice +role: "tool"messages PR #2719 uses. That single check covers Deep Research (both sites) and PageIndex; Question Generator (:2099) and Explore Context (explorer.py:363) drive their own loops and need the same check at their dispatch points. finish_was_truncated/TRUNCATED_FINISH_REASONS(services/llm/types.py:17) is already the shared table the research report validator reads, so no new detection logic is needed.
Happy to open a PR against dev for this once the issue is confirmed.
Runtime repro added — this confirms the whole chain end to end on dev tip 4c1e000d8, not just the dispatch layer. The setup drives the real run_agentic_loop with a scripted provider whose round 1 emits TOOL plus one call with a cut arguments string and finish_reason: "length"; the host forwards every round to the real dispatch_tool_calls with a real ToolRegistry + real ObsidianAppendTool + a real temp vault (same seam Deep Research and PageIndex use, since both are driven by the generic loop).
Output:
dispatch happened: True
arguments dispatched verbatim: '{"note": "Calculus/Week 7", "content": "## Integration\\n- line one\\n- line two\\n- line three cut o'
arguments valid JSON: NO (Unterminated string starting at at char 39)
loop completed: True
note content after the run:
'# Week 7\n\n## Integration\n- line one\n- line two\n- line three cut o'
vault was mutated by the truncated round: True
half-written line persisted: True
fabricated tool results in history: 1
-> {"status": "appended", "path": "Calculus/Week 7.md"}
So: the loop dispatched a batch whose round ended in a truncation finish, the dispatch layer repaired the cut JSON (never a full tool call), the vault got the half-written content, and the fabricated {"status": "appended"} entered the loop history as role: "tool" for the next round to reason over. Under the policy PR #2719 gives the chat loop, the expected result is the refusal path — no dispatch, refusal notice, role: "tool" messages asking the model to retry.
The repro script lives in my workspace; the test-shaped version (asserting the refusal, mirroring tests/agents/loop/test_truncated_tool_dispatch.py) will go into the PR if the issue is confirmed.