Skip to content

fix(lifecycle): overlap refinement drain with kernel teardown - #1265

Closed
sethkarten wants to merge 16 commits into
core06-observable-mcp-cleanupfrom
core07-lifecycle-teardown
Closed

fix(lifecycle): overlap refinement drain with kernel teardown#1265
sethkarten wants to merge 16 commits into
core06-observable-mcp-cleanupfrom
core07-lifecycle-teardown

Conversation

@sethkarten

@sethkarten sethkarten commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • start the pending refinement drain before awaiting IPython kernel-provisioner teardown
  • retain the existing kernel-first await order while allowing final serialized refinement work to progress concurrently
  • cover the ordering with a deferred-provisioner disposal test

Validation

  • biome check (changed files)
  • focused RLM/daemon lifecycle and prior MCP tests: 115 passing
  • daemon monitor and ENG-4685 regression tests with RLM/daemon environment variables unset: 94 passing
  • root tsgo --noEmit remains blocked by pre-existing Core06 mismatch: sdk-mcp-boundary.test.ts imports missing invokeHostRequest from host-request-context.ts

Note

Run refinement drain and kernel teardown concurrently in AgentSession.disposeAsync

  • disposeAsync now starts _drainPendingRefinementForDisposal() and _ipythonKernelProvisioner?.dispose() concurrently using Promise.allSettled, then aggregates failures into an AggregateError if more than one occurs.
  • All concurrent and late callers (including those arriving after _disposed is already true) receive the same shared teardown promise result, including any rejection.
  • _buildRuntime and reload now throw immediately if called after async teardown has started, guarded by the new _asyncTeardownStarted flag set before any await.
  • Kernel disposal error handling is removed from _disposeAsyncOnce; errors are now surfaced rather than swallowed.
  • Behavioral Change: kernel disposal errors that were previously ignored in _disposeAsyncOnce are now propagated to callers of disposeAsync.

Macroscope summarized c954d4d.


Note

Medium Risk
Changes session teardown ordering and error semantics for IPython kernel snapshots and refinement drain; mistakes could affect graceful quit or concurrent disposal behavior.

Overview
AgentSession.disposeAsync now runs the pending refinement drain and IPython kernel provisioner teardown in parallel (Promise.allSettled) instead of awaiting the kernel only after the drain finishes. Final session cleanup still runs afterward, with a finally that calls dispose() and awaits dispose callbacks even when drain or kernel steps fail; callers get a single thrown error or an AggregateError when both paths fail.

Concurrent disposeAsync callers always join the same in-flight promise (including after _disposed is set), so late callers see the same terminal rejection instead of resolving early.

A new _asyncTeardownStarted flag blocks reload and _buildRuntime during async teardown so the runtime cannot replace the kernel mid-disposal. Kernel dispose is invoked from disposeAsync rather than _disposeAsyncOnce.

Tests cover parallel start ordering, shared failures across concurrent/late callers, reload blocking, and aggregated errors.

Reviewed by Cursor Bugbot for commit c954d4d. Bugbot is set up for automated code reviews on this repo. Configure here.

@sethkarten
sethkarten marked this pull request as ready for review August 12, 2026 06:00
Comment thread packages/coding-agent/src/core/agent-session.ts Outdated
@sethkarten
sethkarten force-pushed the core07-lifecycle-teardown branch from 1c5b9c0 to 034c229 Compare August 12, 2026 18:34
Comment thread packages/coding-agent/src/core/agent-session.ts Outdated
Comment thread packages/coding-agent/src/core/agent-session.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ed8324b. Configure here.

Comment thread packages/coding-agent/src/core/agent-session.ts
@sethkarten

Copy link
Copy Markdown
Contributor Author

Superseded by #1335 as part of the physical Core/MCP stack split. The replacement preserves this PR’s reviewed semantic delta on the corrected shared #1243 foundation. Closing this PR to avoid duplicate review; branch retained for provenance.

@sethkarten sethkarten closed this Aug 13, 2026
sethkarten added a commit that referenced this pull request Aug 13, 2026
Reconstruct the unique net delta from PR #1265, excluding propagation 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.

1 participant