Skip to content

fix(agent-core): dispose session terminal records on close#1277

Open
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:fix/session-terminal-record-cleanup
Open

fix(agent-core): dispose session terminal records on close#1277
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:fix/session-terminal-record-cleanup

Conversation

@morluto

@morluto morluto commented Jul 1, 2026

Copy link
Copy Markdown

Related Issue

No linked issue. This is a focused follow-up to #1276 for one session-owned resource type.

Problem

Terminal records are session-scoped resources: each record holds a backend terminal process, output listeners, attached sinks, and buffered output.

Today, TerminalService creates those records for a session but does not subscribe to ISessionService.onDidClose. As a result, when a session is closed while the service remains alive, terminal records for that session can remain in memory until the whole service is disposed.

This breaks the expected ownership model and can leak memory: closing a session should release resources owned by that session.

Code-path proof:

  1. TerminalService.create() stores each terminal in records.
  2. records entries hold the process, output listeners, sinks, and output buffer.
  3. TerminalService removes records during whole-service disposal, but did not respond to session close events.
  4. ISessionService exposes onDidClose, but TerminalService did not subscribe to it.
  5. Therefore, closing a session did not release terminal records owned by that session.

This is separate from TUI terminal restoration in #1272. This change concerns agent-core terminal records owned by server sessions.

What changed

TerminalService now subscribes to ISessionService.onDidClose and releases terminal records for the closed session.

For each matching terminal record, the cleanup path:

  • kills the backend terminal process best-effort;
  • marks the terminal as exited;
  • clears attached sinks and buffered output;
  • removes the record from the service map.

A focused regression test covers the session-close behavior through the public terminal service seam.

Validation

  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/services/terminal-service.test.ts passes: 1 file, 8 tests.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • 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 Jul 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 62cf523

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

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