Skip to content

fix(agent-core): release filesystem watchers on session close#1279

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

fix(agent-core): release filesystem watchers on session close#1279
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:fix/session-fs-watcher-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

Filesystem watchers are session-scoped resources. Each session watcher owns path references, connection/session reference maps, pending change buffers, debounce timers, and an underlying chokidar watcher.

Today, FsWatcherService can create watcher state for a session, but it does not release that state when ISessionService.onDidClose fires. As a result, when a session closes while the service remains alive, watcher state for that session can remain until paths are explicitly removed or the whole service is disposed.

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

Code-path proof:

  1. FsWatcherService.addPaths() creates or reuses a SessionEntry in sessions.
  2. Each SessionEntry owns path references, connection references, pending change state, timers, and a chokidar watcher.
  3. FsWatcherService.removePaths() and service disposal clean entries, but session close did not clean entries.
  4. ISessionService exposes onDidClose, but FsWatcherService did not subscribe to it.
  5. Therefore, closing a session did not release filesystem watcher state owned by that session.

What changed

FsWatcherService now subscribes to ISessionService.onDidClose and disposes watcher state for the closed session.

The cleanup removes the session from each connection map, drops empty connection maps, and disposes the session watcher entry.

A focused regression test verifies that closing a session releases the watcher, clears watched paths for each connection, and resets connection path counts.

Validation

  • pnpm --filter @moonshot-ai/server exec vitest run test/services.test.ts passes: 1 file, 22 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: 187e78e

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