Skip to content

fix(agent-core-v2): keep swarm cancellation from crashing the CLI - #3779

Merged
RealKai42 merged 1 commit into
MoonshotAI:mainfrom
huangzheng2016:fix/swarm-cancel-unhandled-rejection
Sep 15, 2026
Merged

RealKai42 merged 1 commit into
MoonshotAI:mainfrom
huangzheng2016:fix/swarm-cancel-unhandled-rejection

Conversation

@huangzheng2016

Copy link
Copy Markdown
Contributor

Related Issue

No separate issue was filed for this bug; the problem and its root cause are described in detail below.

Problem

Pressing Ctrl+C in the TUI while a subagent swarm is running terminates the entire CLI process instead of just interrupting the swarm.

The root cause is an unhandled promise rejection, through the following chain:

  1. Ctrl+C cancels the current turn, and the turn-abort path stops the tool scope with a default AbortError that carries no cancellation reason.
  2. That same default AbortError propagates into the swarm controller. The run batch's user-cancellation check does not recognize a reason-less AbortError as a user cancellation, so the batch fails with it.
  3. The rejection of batch.run() itself is awaited and handled by the caller. However, SessionSwarmService derived a second promise via void promise.finally(...) purely for cleanup bookkeeping (unlinking abort signals and clearing the in-flight entry). That derived promise rejects with the same error and nothing observes it, producing an unhandled rejection.
  4. The CLI's shell treats unhandled rejections as fatal and performs an emergency exit, taking down the whole process — the user loses the session instead of merely cancelling the swarm.

What changed

This is a defensive fix at the exact point where the unhandled rejection is created:

  • In SessionSwarmService.run, attach .catch(() => {}) to the promise derived from promise.finally(...) used for cleanup. A failing batch no longer surfaces as an unhandled rejection. The original batch.run() promise is still returned to and awaited by the caller, so the failure itself continues to be reported and handled through the normal path.
  • Added a regression test that cancels a running swarm whose batch fails with a non-user abort and asserts that no unhandledRejection is emitted.

Follow-up work (not part of this PR): the turn-abort path should propagate the original cancellation reason so that user cancellations can be distinguished from system-initiated aborts at the source, rather than relying on downstream defensive handling. That is being tracked on the branch behind PR #3778.

How this was verified

  • New regression test passes; the full sessionSwarm.test.ts suite passes 32/32.
  • End-to-end: with a mock provider, started a swarm of 16 subagents in the CLI and pressed Ctrl+C mid-run — the swarm was interrupted and the CLI stayed alive.
  • Full repository test suite: 6583/6583 passing.
  • Root lint (check-no-comments + oxlint --type-aware): 0 errors.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: the issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7b06dd3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Sep 14, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@7b06dd3
npx https://pkg.pr.new/@moonshot-ai/kimi-code@7b06dd3

commit: 7b06dd3

@huangzheng2016

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 7b06dd3a96

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@RealKai42
RealKai42 merged commit 82ec469 into MoonshotAI:main Sep 15, 2026
16 checks passed
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Oct 5, 2026
Step 5 of the incremental sync to 2.1.1: 14 upstream commits over 230
files. The bulk is the pi-tui re-baseline onto upstream v0.85.1.

Upstream's changes in this step, all absorbed:
- pi-tui re-baselined on upstream v0.85.1 (MoonshotAI#3780): the native platform
  modules were consolidated and renamed (darwin-modifiers ->
  darwin-platform, win32-console-mode -> win32-platform), a Linux/X11
  clipboard backend with prebuilds was added, and alt-screen search,
  autocomplete, editor, input, markdown and the render path were
  reworked; the native-asset copy scripts and flake.nix follow
- the session event bus is sharded by agent id (MoonshotAI#3775)
- disposed agent scopes are released from the DI dependency graph (MoonshotAI#3776)
- swarm cancellation no longer crashes the CLI (MoonshotAI#3779)
- tower mode no longer mistakes newly spawned agents for the previous
  name (MoonshotAI#3752)
- [secondary_model].default_effort fails fast when unsupported (MoonshotAI#3785)
- the managed usage payload switched to the quota model (MoonshotAI#3787)
- kap-server verifies indexed search results against session sources (MoonshotAI#3631)
- web dist synced from code-app (MoonshotAI#3792); docs for the web slash-command
  table and flat-form skill naming (MoonshotAI#3772, MoonshotAI#3774)

Fork behaviour preserved through the re-baseline (pi-tui/UPSTREAM.md):
- every `keep` intent card still has a matching per-file diff: the
  width/grapheme clamps, editor history hooks, multi-root @-completion,
  inline slash autocomplete, CJK autolink, fullscreen nav fall-through,
  mouse dispatch (cropped rows, prompt-width clicks, search-field
  forwarding), and sixel graphics with the startup probe
- the fork's image/terminal-image, latex and layout extensions ride on
  upstream's v0.85.1 baseline
- the fork's i18n layer covers the new user-facing strings

Verification and the follow-up fixes are recorded in the next commit:
the fork's auth-facade test still asserted the pre-quota managed-usage
shape, and the node-sdk dts build had lost its i18n specifier rewriting
in the 0.42.0 merge (found by this step's root typecheck).
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.

2 participants