Skip to content

fix(runtime-core): avoid MCP tool discovery after agent cancellation (#450) - #457

Draft
tkislan wants to merge 2 commits into
mainfrom
cursor/avoid-mcp-tool-discovery-after-cancel-7f81
Draft

fix(runtime-core): avoid MCP tool discovery after agent cancellation (#450)#457
tkislan wants to merge 2 commits into
mainfrom
cursor/avoid-mcp-tool-discovery-after-cancel-7f81

Conversation

@tkislan

@tkislan tkislan commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #450

Problem

When an agent run is cancelled while MCP clients are still being created or while client.tools() is in flight, the handler continued into tool discovery and/or model streaming after the abort signal had already fired.

Changes

agent-handler.ts

  • Add context.signal?.throwIfAborted() after retaining fulfilled MCP clients from Promise.allSettled, before checking for creation failures (abort wins over spawn errors).
  • Add a second guard after client.tools() completes, before constructing the agent and starting the model stream.
  • Existing finally cleanup is unchanged.

execution-engine.ts

  • Add optional ExecutionOptions.signal forwarded only to AgentBlockContext.signal.
  • Does not interrupt code/SQL/kernel blocks; runProject / runFile still resolve with a failed agent block when the signal aborts.

Tests

  • Race tests for abort during pending createMCPClient and pending client.tools().
  • Engine forwarding tests and behavioral test that abort resolves with failedBlocks === 1.

Docs

  • Document signal in packages/runtime-core/README.md.

Verification

  • pnpm exec vitest run packages/runtime-core/src/execute-agent-block.test.ts packages/runtime-core/src/execution-engine.test.ts
  • pnpm test
  • pnpm typecheck
  • pnpm biome:check

Summary by CodeRabbit

  • New Features

    • Added optional cancellation support for agent block execution.
    • Aborted agent runs now stop setup promptly and report the cancellation reason.
    • Aborted agent blocks are handled as failed blocks without causing the overall project run to reject.
  • Documentation

    • Documented signal forwarding, scope, and cancellation behavior.

Add abort checks after MCP client creation and tool discovery so
cancelled agent runs skip merging tool sets. Expose optional signal
on ExecutionOptions and document the agent-only contract in README.
…450

Add MCP cancellation race tests in execute-agent-block.test.ts and
ExecutionEngine signal pass-through / failed-block summary tests.
@coderabbitai

coderabbitai Bot commented Aug 14, 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f09fa8f8-2a43-4adc-8326-0416a0d0b247

📥 Commits

Reviewing files that changed from the base of the PR and between d20bc24 and b1ec9db.

📒 Files selected for processing (5)
  • packages/runtime-core/README.md
  • packages/runtime-core/src/agent-handler.ts
  • packages/runtime-core/src/execute-agent-block.test.ts
  • packages/runtime-core/src/execution-engine.test.ts
  • packages/runtime-core/src/execution-engine.ts

📝 Walkthrough

Walkthrough

The execution engine now accepts an optional AbortSignal and forwards it to agent blocks. Agent setup checks for cancellation after MCP client creation and tool discovery. Aborted agent runs preserve client cleanup, skip tool or model execution, and are recorded as failed blocks without rejecting runProject. Documentation and tests cover signal propagation, cancellation timing, cleanup, and outcomes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to b1ec9

The PR changes runtime cancellation behavior and adds related tests and documentation; no actionable merge-blocking risk remains based on the supplied evidence.

Possibly related PRs

Suggested reviewers: m1so, dinohamzic

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary fix: preventing MCP tool discovery after agent cancellation.
Linked Issues check ✅ Passed The changes satisfy the cancellation, cleanup, signal propagation, and regression-test requirements in [#450].
Out of Scope Changes check ✅ Passed The README, signal forwarding, implementation, and tests directly support the cancellation behavior in [#450].
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Updates Docs ✅ Passed The PR adds runtime-core README documentation for signal, scope, forwarding, and abort results. The private roadmap repo is not visible here; update its landing page separately.

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

@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.30%. Comparing base (3a3a15c) to head (b1ec9db).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #457   +/-   ##
=======================================
  Coverage   88.30%   88.30%           
=======================================
  Files         191      191           
  Lines       10697    10699    +2     
  Branches     2996     2996           
=======================================
+ Hits         9446     9448    +2     
  Misses       1249     1249           
  Partials        2        2           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

Avoid MCP tool discovery after agent cancellation

1 participant