kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

fix(agent-core-v2): observe aborted source rejections

Open morluto opened this issue 4 months ago • 1 comments

Related Issue

No linked issue. This is a focused fix for deterministic promise ownership behavior in the internal v2 engine.

Problem

When abortable() receives an already-aborted signal, it rejects its returned promise before attaching handlers to the supplied source promise. If that source rejects later, Node emits unhandledRejection; with the default throw policy, that can terminate the process.

The MCP reconnect path can create reconnect work before wrapping it with the tool execution signal, making it the clearest affected caller.

already-aborted signal ──> caller receives AbortError
                                  │
source work continues ────────────┴──> later rejection
                                          │
                              before: unhandled
                              after:  observed by abortable

What changed

  • Attach source settlement handlers before checking whether cancellation has already won.
  • Remove the abort listener on either source settlement path without creating a detached cleanup promise.
  • Add a regression test that observes the actual Node unhandledRejection boundary for the supplied source promise.

This does not attempt to cancel the source operation; it preserves the existing contract while retaining ownership of its eventual rejection.

Test plan

  • pnpm --filter @moonshot-ai/agent-core-v2 exec vitest run test/_base/utils/abort.test.ts --reporter=verbose — 9 passed
  • pnpm --filter @moonshot-ai/agent-core-v2 test — 4,082 passed
  • pnpm --filter @moonshot-ai/agent-core-v2 typecheck — passed
  • pnpm --filter @moonshot-ai/agent-core-v2 lint:domain — passed for 938 files
  • pnpm exec oxlint --type-aware packages/agent-core-v2/src/_base/utils/abort.ts packages/agent-core-v2/test/_base/utils/abort.test.ts — 0 warnings, 0 errors

Checklist

  • [x] I have read the CONTRIBUTING document.
  • [x] I have linked a related issue, or explained the problem above.
  • [x] I have added tests that prove the fix works.
  • [x] Ran gen-changesets; no changeset is needed for an internal-only agent-core-v2 change.
  • [x] No user documentation update is needed for this internal utility fix.

morluto avatar Jul 24 '26 17:07 morluto

⚠️ No Changeset found

Latest commit: 338d4bf728d660fd765804c74629a3478222f10f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

changeset-bot[bot] avatar Jul 24 '26 17:07 changeset-bot[bot]