Skip to content

fix(agent): recover truncated tool calls - #2018

Merged
yyhhyyyyyy merged 3 commits into
devfrom
fix/truncated-tool-call-recovery
Jul 24, 2026
Merged

fix(agent): recover truncated tool calls#2018
yyhhyyyyyy merged 3 commits into
devfrom
fix/truncated-tool-call-recovery

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Atomically reject DeepChat-owned tool calls when provider output is truncated by max_tokens
  • Generate matching error tool results without executing potentially incomplete arguments
  • Retry the provider once after settling the rejected batch, then terminate safely if truncation repeats
  • Track DeepChat-owned and provider-owned tool calls to prevent local execution of provider-managed tools
  • Preserve tool-call protocol integrity across persistence, context rebuilding, and Tape facts

Implementation

  • Add optional per-call execution ownership with backward-compatible DeepChat ownership by default
  • Map AI SDK providerExecuted calls and ACP tool calls to provider ownership
  • Extend tool batch settlement with explicit execute and truncated-rejection dispositions
  • Collect completed and pending DeepChat-owned calls in source order for atomic rejection
  • Mark rejected and structurally incomplete blocks with distinct max_tokens reasons
  • Keep rejected calls outside tool execution limits, metadata accounting, permissions, hooks, and no-progress detection
  • Scope block updates to the current provider round when call IDs are reused
  • Preserve existing normal tool_use, abort, pending-input, output fitting, and provider-round-limit behavior

Safety

Truncated tool arguments are never executed, even when some calls appear complete. The rejection path does not invoke permission prechecks, auto-approval reviewers, tool executors, question tools, or skill activation.

Automatic recovery is limited to one additional provider request per run.

Summary by CodeRabbit

  • New Features

    • Added recovery for tool calls interrupted by output-length limits.
    • Preserves incomplete tool-call details and records clear skipped or incomplete statuses.
    • Distinguishes tool calls already handled by the provider from those requiring local execution.
    • Maintains tool-call ordering and context across recovery attempts.
  • Bug Fixes

    • Prevents truncated tool calls from remaining unresolved or being executed with incomplete arguments.
    • Improves tool-result persistence and status reporting after interrupted responses.
  • Documentation

    • Added a specification describing truncated tool-call recovery behavior and acceptance criteria.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 96326198-ff24-426d-8b2f-3cd8f3305d1a

📥 Commits

Reviewing files that changed from the base of the PR and between 38e8d66 and 71bc743.

📒 Files selected for processing (22)
  • docs/issues/truncated-tool-call-recovery/spec.md
  • src/main/agent/acp/runtime/acpContentMapper.ts
  • src/main/agent/deepchat/loop/deepChatLoopEngine.ts
  • src/main/agent/deepchat/runtime/accumulator.ts
  • src/main/agent/deepchat/runtime/dispatch.ts
  • src/main/agent/deepchat/runtime/imageGenerationBlocks.ts
  • src/main/agent/deepchat/runtime/process.ts
  • src/main/agent/deepchat/runtime/types.ts
  • src/main/provider/aiSdk/streamAdapter.ts
  • src/shared/chat.d.ts
  • src/shared/types/agent-interface.d.ts
  • src/shared/types/core/chat.ts
  • src/shared/types/core/llm-events.ts
  • test/main/agent/acp/runtime/acpContentMapper.test.ts
  • test/main/agent/deepchat/loop/deepChatLoopEngine.test.ts
  • test/main/agent/deepchat/runtime/accumulator.test.ts
  • test/main/agent/deepchat/runtime/contextBuilder.test.ts
  • test/main/agent/deepchat/runtime/dispatch.test.ts
  • test/main/agent/deepchat/runtime/process.test.ts
  • test/main/evals/nativeAgent/nativeAgentBehavior.eval.test.ts
  • test/main/evals/nativeAgent/scenarios.ts
  • test/main/provider/aiSdkStreamAdapter.test.ts

📝 Walkthrough

Walkthrough

Adds execution-owner metadata for tool calls, refactors tool execution into settlement, and introduces bounded recovery for max_tokens-truncated DeepChat-owned calls with ordered error results and persistence coverage.

Changes

Truncated tool-call recovery

Layer / File(s) Summary
Execution ownership contracts
src/shared/types/core/llm-events.ts, src/main/provider/aiSdk/streamAdapter.ts, src/main/agent/acp/runtime/acpContentMapper.ts, src/main/agent/deepchat/runtime/accumulator.ts, src/shared/...
Tool-call start events identify provider-owned calls; accumulated state excludes provider-owned calls from local execution; assistant metadata includes skipped and incomplete reasons.
Tool-batch settlement
src/main/agent/deepchat/runtime/dispatch.ts, src/main/agent/deepchat/runtime/imageGenerationBlocks.ts
settleToolBatch handles execute or reject dispositions, commits staged results centrally, and scopes block updates to the current batch.
Recovery loop control
src/main/agent/deepchat/runtime/process.ts, src/main/agent/deepchat/loop/deepChatLoopEngine.ts
max_tokens rounds derive ordered DeepChat-owned truncated calls, reject them without execution, allow one recovery round, mark other unresolved blocks incomplete, and terminate after the recovery limit.
Recovery specification and validation
docs/issues/truncated-tool-call-recovery/spec.md, test/main/agent/deepchat/..., test/main/provider/..., test/main/evals/nativeAgent/*
Specification, unit tests, context reconstruction tests, provider-ownership tests, ordering coverage, and a native-agent recovery scenario validate the new behavior.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Provider
  participant processStream
  participant settleToolBatch
  participant ContextBuilder
  Provider->>processStream: Emit truncated tool-call round
  processStream->>processStream: Select DeepChat-owned calls
  processStream->>settleToolBatch: Reject output_truncated batch
  settleToolBatch->>ContextBuilder: Commit ordered error results
  processStream->>Provider: Request one recovery round
Loading

Possibly related PRs

  • ThinkInAIXYZ/deepchat#1959: Adds the native-agent evaluation harness and scenario infrastructure used by the new recovery scenario.

Suggested reviewers: zerob13, zhangmo8

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: recovering truncated tool calls in the agent.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/truncated-tool-call-recovery

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@yyhhyyyyyy
yyhhyyyyyy merged commit 44bdb0e into dev Jul 24, 2026
12 checks passed
@zhangmo8
zhangmo8 deleted the fix/truncated-tool-call-recovery branch July 27, 2026 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant