Skip to content

refactor(agent): type tool execution contract - #2022

Merged
yyhhyyyyyy merged 4 commits into
devfrom
refactor/tool-execution-contract
Jul 25, 2026
Merged

refactor(agent): type tool execution contract#2022
yyhhyyyyyy merged 4 commits into
devfrom
refactor/tool-execution-contract

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Introduce a required, typed execution contract for tool definitions.
  • Centralize tool batch scheduling in a fail-closed execution policy.
  • Preserve existing execution behavior while replacing name-based dispatch checks.
  • Add architecture documentation and focused regression coverage.

Design

Each executable tool now declares:

  • effect: read or write
  • executionMode: sequential or parallel

The type contract makes write + parallel unrepresentable.

A batch runs in parallel only when:

  • permission mode is full_access;
  • the batch contains at least two calls;
  • every tool definition is present and unambiguous;
  • every tool is explicitly declared as read + parallel.

Missing, malformed, duplicate, mixed, or write-capable definitions fall back to whole-batch sequential execution.

External MCP and plugin tools remain write + sequential. Their advisory metadata is not trusted as an execution-safety boundary. Currently, only the filesystem read tool opts into parallel execution.

Summary by CodeRabbit

  • New Features
    • Added explicit execution contracts for tools, defining read/write effects and allowed parallel vs sequential execution.
    • Enabled parallel execution for eligible read tool batches in full-access sessions; otherwise falls back to sequential.
  • Bug Fixes
    • Prevented execution metadata from affecting provider tool schemas, token estimation, or manifest hash verification.
    • Improved MCP tool parameter rendering for enums and required fields.
  • Documentation
    • Added architecture docs and task tracking for agent harness execution-safety boundaries.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds typed MCP tool execution contracts, classifies built-in and external tools, centralizes fail-closed batch scheduling, excludes execution metadata from token estimates, provider schemas, and tape manifest hashes, and hardens MCP parameter-schema rendering.

Changes

Execution Contract and Scheduling

Layer / File(s) Summary
Canonical contract and architecture boundaries
docs/architecture/deepchat-agent-harness-boundaries/*, src/shared/types/core/mcp.ts, src/shared/types/mcp.ts, src/main/tool/index.ts
MCP definitions now combine base metadata with a discriminated execution contract, while prompt-only paths use base definitions.
Catalog execution classification
src/main/mcp/*, src/main/tool/agentTools/*, src/main/tool/browser/definitions.ts, test/main/mcp/toolManager.test.ts, test/main/tool/agentTools/agentToolManagerRead.test.ts
Built-in tools receive explicit execution contracts, while discovered MCP tools default to sequential writes regardless of server hints.
Fail-closed batch scheduling
src/main/agent/deepchat/runtime/toolExecutionPolicy.ts, src/main/agent/deepchat/runtime/dispatch.ts, test/main/agent/deepchat/runtime/*
Parallel execution is selected only for multi-call, full-access batches of uniquely resolved parallel-read tools.
Provider, context, and fixture compatibility
src/main/agent/deepchat/runtime/contextBuilder.ts, src/main/tape/domain/viewManifest.ts, test/main/provider/*, test/main/session/data/tapeViewManifest.test.ts, test/main/agent/*
Execution metadata is excluded from token estimation, provider mappings, and tape hashes, with updated fixtures and regression tests.

MCP Tool Schema Rendering

Layer / File(s) Summary
Defensive parameter schema display
src/renderer/src/components/mcp-config/components/McpToolPanel.vue, test/renderer/components/McpToolPanel.test.ts
Parameter schema parsing handles required values, enums, array enums, and stable rendering keys more defensively.

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

Sequence Diagram(s)

sequenceDiagram
  participant AgentRuntime
  participant BatchPolicy
  participant ToolCatalog
  participant ToolExecutor
  AgentRuntime->>BatchPolicy: submit permission mode and tool calls
  BatchPolicy->>ToolCatalog: resolve execution contracts
  ToolCatalog-->>BatchPolicy: return tool effect and execution mode
  BatchPolicy-->>AgentRuntime: select parallel or sequential mode
  AgentRuntime->>ToolExecutor: settle the selected batch
Loading

Possibly related PRs

Suggested reviewers: zerob13

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 and concisely captures the main change: introducing a typed tool execution contract.
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 refactor/tool-execution-contract

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/main/agent/deepchat/runtime/dispatch.test.ts (1)

103-109: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Keep execution metadata explicit in test fixtures.

makeAgentTool still infers parallel execution from the name read, so tests can omit the contract and preserve the name-based behavior this PR is removing. Default to sequential write and pass PARALLEL_READ_TOOL_EXECUTION explicitly where needed.

Suggested adjustment
 function makeAgentTool(
   name: string,
-  execution: ToolExecutionContract =
-    name === 'read' ? PARALLEL_READ_TOOL_EXECUTION : SEQUENTIAL_WRITE_TOOL_EXECUTION
+  execution: ToolExecutionContract = SEQUENTIAL_WRITE_TOOL_EXECUTION
 ): MCPToolDefinition {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/main/agent/deepchat/runtime/dispatch.test.ts` around lines 103 - 109,
Update the makeAgentTool test fixture to always default its execution contract
to SEQUENTIAL_WRITE_TOOL_EXECUTION instead of inferring it from the tool name.
Add PARALLEL_READ_TOOL_EXECUTION explicitly at each read-tool fixture call site
that requires parallel execution.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/renderer/src/components/mcp-config/components/McpToolPanel.vue`:
- Around line 181-185: Update the item-enum rendering condition near line 380 to
also accept parameters whose mapped type is “array[enum]”, matching the type
assigned by the enumValues mapper. Preserve the existing “array” behavior so
enum badges render for both array parameter variants.

---

Nitpick comments:
In `@test/main/agent/deepchat/runtime/dispatch.test.ts`:
- Around line 103-109: Update the makeAgentTool test fixture to always default
its execution contract to SEQUENTIAL_WRITE_TOOL_EXECUTION instead of inferring
it from the tool name. Add PARALLEL_READ_TOOL_EXECUTION explicitly at each
read-tool fixture call site that requires parallel execution.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c4c7d988-fde0-4680-aa04-a6fd453f8b1e

📥 Commits

Reviewing files that changed from the base of the PR and between 3ed1ff5 and f4f7473.

📒 Files selected for processing (38)
  • docs/architecture/deepchat-agent-harness-boundaries/plan.md
  • docs/architecture/deepchat-agent-harness-boundaries/spec.md
  • docs/architecture/deepchat-agent-harness-boundaries/tasks.md
  • src/main/agent/deepchat/runtime/contextBuilder.ts
  • src/main/agent/deepchat/runtime/dispatch.ts
  • src/main/agent/deepchat/runtime/toolExecutionPolicy.ts
  • src/main/mcp/index.ts
  • src/main/mcp/toolManager.ts
  • src/main/tape/domain/viewManifest.ts
  • src/main/tool/agentTools/agentImageGenerationTool.ts
  • src/main/tool/agentTools/agentMemoryTools.ts
  • src/main/tool/agentTools/agentPlanTool.ts
  • src/main/tool/agentTools/agentTapeTools.ts
  • src/main/tool/agentTools/agentToolManager.ts
  • src/main/tool/agentTools/chatSettingsTools.ts
  • src/main/tool/agentTools/cronJobTool.ts
  • src/main/tool/agentTools/subagentOrchestratorTool.ts
  • src/main/tool/browser/definitions.ts
  • src/main/tool/index.ts
  • src/renderer/src/components/mcp-config/components/McpToolPanel.vue
  • src/shared/types/core/mcp.ts
  • src/shared/types/mcp.ts
  • test/main/agent/acp/runtime/acpCompatibilityPromptBuilder.test.ts
  • test/main/agent/deepchat/instance/deepChatAgentRuntime.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/agent/deepchat/runtime/toolAdapters.test.ts
  • test/main/agent/deepchat/runtime/toolExecutionPolicy.test.ts
  • test/main/agent/deepchat/runtime/toolOutputGuard.test.ts
  • test/main/evals/nativeAgent/harness.ts
  • test/main/mcp/toolManager.test.ts
  • test/main/provider/aiSdkToolMapper.test.ts
  • test/main/provider/baseProvider.test.ts
  • test/main/session/data/tapeViewManifest.test.ts
  • test/main/tool/agentTools/agentToolManagerRead.test.ts
  • test/main/tool/toolService.test.ts
  • test/renderer/composables/useChatInputMentions.test.ts

Comment thread src/renderer/src/components/mcp-config/components/McpToolPanel.vue

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
test/renderer/components/McpToolPanel.test.ts (1)

25-109: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Move this test into the mirrored component directory.

Place it under test/renderer/components/mcp-config/components/McpToolPanel.test.ts to mirror src/renderer/src/components/mcp-config/components/McpToolPanel.vue.

As per coding guidelines, “Place tests in directories mirroring their source under test/main/** and test/renderer/**.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/renderer/components/McpToolPanel.test.ts` around lines 25 - 109, Move
the McpToolPanel test suite from the current renderer components test location
into the mirrored mcp-config/components directory, preserving the existing
describe block and test behavior unchanged.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@test/renderer/components/McpToolPanel.test.ts`:
- Around line 25-109: Move the McpToolPanel test suite from the current renderer
components test location into the mirrored mcp-config/components directory,
preserving the existing describe block and test behavior unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c1d848e2-6e7e-4c8b-9964-71995aa27338

📥 Commits

Reviewing files that changed from the base of the PR and between f4f7473 and 1ec01f0.

📒 Files selected for processing (35)
  • docs/architecture/deepchat-agent-harness-boundaries/plan.md
  • docs/architecture/deepchat-agent-harness-boundaries/spec.md
  • docs/architecture/deepchat-agent-harness-boundaries/tasks.md
  • src/main/agent/deepchat/runtime/toolExecutionPolicy.ts
  • src/main/mcp/index.ts
  • src/main/mcp/toolManager.ts
  • src/main/tool/agentTools/agentImageGenerationTool.ts
  • src/main/tool/agentTools/agentMemoryTools.ts
  • src/main/tool/agentTools/agentPlanTool.ts
  • src/main/tool/agentTools/agentTapeTools.ts
  • src/main/tool/agentTools/agentToolManager.ts
  • src/main/tool/agentTools/chatSettingsTools.ts
  • src/main/tool/agentTools/cronJobTool.ts
  • src/main/tool/agentTools/subagentOrchestratorTool.ts
  • src/main/tool/browser/definitions.ts
  • src/renderer/src/components/mcp-config/components/McpToolPanel.vue
  • src/shared/types/core/mcp.ts
  • src/shared/types/mcp.ts
  • test/main/agent/acp/runtime/acpCompatibilityPromptBuilder.test.ts
  • test/main/agent/deepchat/instance/deepChatAgentRuntime.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/agent/deepchat/runtime/toolAdapters.test.ts
  • test/main/agent/deepchat/runtime/toolExecutionPolicy.test.ts
  • test/main/agent/deepchat/runtime/toolOutputGuard.test.ts
  • test/main/evals/nativeAgent/harness.ts
  • test/main/mcp/toolManager.test.ts
  • test/main/provider/aiSdkToolMapper.test.ts
  • test/main/provider/baseProvider.test.ts
  • test/main/session/data/tapeViewManifest.test.ts
  • test/main/tool/agentTools/agentToolManagerRead.test.ts
  • test/main/tool/toolService.test.ts
  • test/renderer/components/McpToolPanel.test.ts
  • test/renderer/composables/useChatInputMentions.test.ts
🚧 Files skipped from review as they are similar to previous changes (12)
  • test/main/mcp/toolManager.test.ts
  • test/main/agent/deepchat/runtime/toolOutputGuard.test.ts
  • docs/architecture/deepchat-agent-harness-boundaries/tasks.md
  • src/main/agent/deepchat/runtime/toolExecutionPolicy.ts
  • src/main/tool/agentTools/agentTapeTools.ts
  • src/renderer/src/components/mcp-config/components/McpToolPanel.vue
  • test/main/session/data/tapeViewManifest.test.ts
  • test/main/agent/deepchat/runtime/toolExecutionPolicy.test.ts
  • docs/architecture/deepchat-agent-harness-boundaries/spec.md
  • docs/architecture/deepchat-agent-harness-boundaries/plan.md
  • test/main/agent/deepchat/runtime/contextBuilder.test.ts
  • test/main/agent/deepchat/runtime/dispatch.test.ts

@yyhhyyyyyy
yyhhyyyyyy merged commit a7565c9 into dev Jul 25, 2026
12 checks passed
@zhangmo8
zhangmo8 deleted the refactor/tool-execution-contract 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