Skip to content

Fix WorktreeCreatedTaskDispatcher overfiring (#318241)#318880

Merged
connor4312 merged 2 commits into
mainfrom
fix/worktree-created-task-dispatcher-issue-318241
May 29, 2026
Merged

Fix WorktreeCreatedTaskDispatcher overfiring (#318241)#318880
connor4312 merged 2 commits into
mainfrom
fix/worktree-created-task-dispatcher-issue-318241

Conversation

@connor4312
Copy link
Copy Markdown
Member

Problem

WorktreeCreatedTaskDispatcher fires 'Install & Watch' worktree tasks excessively for restored/synced sessions instead of only for newly-started ones.

Root Cause

The dispatcher listened to onDidChangeSessions and tried to filter on status === Untitled to identify newly started sessions. This was fundamentally wrong:

  1. Agent-host sessions are never Untitled when arriving via change events:

    • Skeletons get setStatus(InProgress) before the change event fires (baseAgentHostSessionsProvider.ts:1854-1859)
    • Real sessions from _refreshSessions are constructed with server metadata status (baseAgentHostSessionsProvider.ts:2106-2147)
  2. onDidChangeSessions is a catalog-change event, not a 'we started this' event:

    • Fires whenever any provider updates the catalog: added/removed/changed
    • Includes cloud-synced sessions, restored sessions, sessions from other devices
    • This caused tasks to run for existing sessions on every catalog update

Solution

Introduce an explicit onDidStartSession event that fires only from sendNewChatRequest after provider.sendRequest() resolves with the committed session.

Changes

  • ISessionsManagementService: Add readonly onDidStartSession: Event<ISession> declaration
  • SessionsManagementService: Implement emitter + fire call in sendNewChatRequest (after session is committed)
  • WorktreeCreatedTaskDispatcher: Rewrite to listen ONLY to onDidStartSession instead of added/changed events
  • Tests: Comprehensive rewrite with regression test for AgentHost: "Install & Watch" runs on existing sessions #318241
  • Docs: Update SESSIONS.md spec

Why This is Correct

Sessions that flow through sendNewChatRequest are exactly the ones 'we just started locally'. Cloud-synced / restored / refreshed sessions appear via provider._onDidChangeSessions from _refreshSessions, which the dispatcher no longer listens to.

This correctly distinguishes:

  • 'Sessions we just started' → runs worktree tasks ✓
  • 'Sessions in the catalog from any other reason' → ignored ✓

Fixes #318241

…18241)

Introduce onDidStartSession event to distinguish sessions we just started from sessions that appear in the catalog (via cloud sync, refresh, etc).

Problem:
WorktreeCreatedTaskDispatcher listened to onDidChangeSessions and tried to filter on status=Untitled to identify newly started sessions. This was wrong because:
- Agent-host sessions are never Untitled when arriving via change events (skeleton gets setStatus(InProgress) before firing)
- onDidChangeSessions fires for any catalog change including cloud-synced/restored sessions from other devices
- This caused tasks to run for existing sessions on every sync, not just newly started ones

Solution:
- Add onDidStartSession: Event<ISession> to ISessionsManagementService
- Fire it from sendNewChatRequest after provider.sendRequest() resolves with committed session
- Rewrite WorktreeCreatedTaskDispatcher to listen ONLY to onDidStartSession instead of added/changed events
- This correctly identifies 'sessions we just started locally' vs 'sessions in the catalog'

Changes:
- ISessionsManagementService: add onDidStartSession event
- SessionsManagementService: implement emitter + fire in sendNewChatRequest
- WorktreeCreatedTaskDispatcher: rewrite to use onDidStartSession exclusively
- Tests: comprehensive rewrite with regression test for #318241
- Docs: update SESSIONS.md spec

Fixes #318241

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 29, 2026 01:44
@connor4312 connor4312 enabled auto-merge (squash) May 29, 2026 01:46
@vs-code-engineering
Copy link
Copy Markdown
Contributor

vs-code-engineering Bot commented May 29, 2026

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@sandy081

Matched files:

  • src/vs/sessions/services/sessions/browser/sessionsManagementService.ts
  • src/vs/sessions/services/sessions/common/sessionsManagement.ts
  • src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts

@lszomoru

Matched files:

  • src/vs/sessions/services/sessions/browser/sessionsManagementService.ts
  • src/vs/sessions/services/sessions/common/sessionsManagement.ts
  • src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes overfiring of worktreeCreated session tasks by distinguishing locally started sessions from restored/synced catalog updates in the Agents window session flow.

Changes:

  • Adds ISessionsManagementService.onDidStartSession and fires it after sendNewChatRequest commits a session.
  • Reworks WorktreeCreatedTaskDispatcher to track only newly started sessions and use catalog changes only for removal cleanup.
  • Updates related tests, configuration default, and session documentation.
Show a summary per file
File Description
src/vs/sessions/SESSIONS.md Documents the new onDidStartSession-based worktree task dispatch flow.
src/vs/sessions/services/sessions/common/sessionsManagement.ts Adds the new service event contract.
src/vs/sessions/services/sessions/browser/sessionsManagementService.ts Implements and fires onDidStartSession after session commit.
src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts Updates the session management service test mock.
src/vs/sessions/contrib/chat/browser/worktreeCreatedTaskDispatcher.ts Rewrites dispatch tracking to listen to started sessions instead of catalog additions/changes.
src/vs/sessions/contrib/chat/browser/chat.contribution.ts Changes the agent-host worktree-created task setting default to enabled.
src/vs/sessions/contrib/chat/test/browser/worktreeCreatedTaskDispatcher.test.ts Reworks dispatcher tests around the new event and adds a restored-session regression case.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 4

Comment thread src/vs/sessions/contrib/chat/browser/chat.contribution.ts
Comment thread src/vs/sessions/contrib/chat/test/browser/worktreeCreatedTaskDispatcher.test.ts Outdated
Comment thread src/vs/sessions/contrib/chat/browser/worktreeCreatedTaskDispatcher.ts Outdated
…318880)

Fix 4 issues identified in review:

1. **Autorun synchronous-dispatch leak** (line 79-91):
   - autorun was running synchronously before store.add() completed
   - when conditions were met immediately, it would dispose the store before the autorun was registered
   - Switch to registerAutorunSelfDisposable to ensure the autorun is registered before it can dispose itself

2. **Config default documentation mismatch** (line 18-21):
   - JSDoc claimed setting defaults to false, but chat.contribution.ts sets true
   - Update JSDoc to say 'Defaults to `true`'

3. **Test doesn't reflect new default** (line 258-268):
   - Test 'skips agent host sessions when the setting is disabled (default)' relied on default
   - Now that default is true, explicitly set config to false
   - Remove '(default)' from test name

4. **Stale comment** (line 74-77):
   - Comment still referred to removed 'dispatched' flag
   - Update to describe actual guard (disposing store removes subscriptions)
@connor4312 connor4312 merged commit 9fd5436 into main May 29, 2026
25 checks passed
@connor4312 connor4312 deleted the fix/worktree-created-task-dispatcher-issue-318241 branch May 29, 2026 05:57
@vs-code-engineering vs-code-engineering Bot added this to the 1.123.0 milestone May 29, 2026
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.

AgentHost: "Install & Watch" runs on existing sessions

3 participants