Skip to content

fix: remove duplicate command registration for agent-host-copilotcli (fixes #318223)#318231

Open
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/duplicate-command-agent-host-copilotcli-45d6028ab7f7bb9c
Open

fix: remove duplicate command registration for agent-host-copilotcli (fixes #318223)#318231
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/duplicate-command-agent-host-copilotcli-45d6028ab7f7bb9c

Conversation

@vs-code-engineering
Copy link
Copy Markdown
Contributor

🔧 Error Fix

Summary

The error Cannot register two commands with the same id: workbench.action.chat.openNewChatSessionInPlace.agent-host-copilotcli occurs because the command is registered twice: once statically via CommandsRegistry.registerCommand in electron-browser/chat.contribution.ts, and once dynamically via registerAction2 in the ChatSessionsService autorun (triggered by registerChatSessionContribution). The registerAction2 function throws when a command with the same ID already exists.

Fixes #318223
Recommended reviewer: @meganrogge

Culprit Commit

The static registration was introduced by 66ba7394 ("Add static commands for opening Agent Host chat sessions (#314187)") by @meganrogge on May 4. The conflict became observable in the regression window (b51c2a1...859cf8d, May 21-22) when the registerChatSessionContribution code path started firing _onDidChangeAvailability to trigger the autorun that dynamically registers the same command via registerAction2.

Code Flow

sequenceDiagram
    participant AH as AgentHostContrib
    participant CSS as ChatSessionsService
    participant OBS as contributedSessionProviders
    participant AR as Autorun
    participant CR as CommandsRegistry
    participant RA as registerAction2

    Note over CR: Static registration at startup (electron-browser)
    CR->>CR: registerCommand(openNewChatSessionInPlace.agent-host-copilotcli)

    AH->>AH: handleSnapshot fires rootState.onDidChange
    AH->>AH: _handleRootStateChange
    AH->>AH: _registerAgent(copilotcli)
    AH->>CSS: registerChatSessionContribution(type: agent-host-copilotcli)
    CSS->>CSS: _contributions.set, _contributionDisposables.set
    CSS->>OBS: _onDidChangeAvailability.fire()
    OBS->>OBS: recompute: includes agent-host-copilotcli
    OBS->>AR: trigger autorun
    AR->>AR: dispose old reader.store
    AR->>RA: registerNewSessionInPlaceAction(agent-host-copilotcli)
    RA->>CR: getCommand(openNewChatSessionInPlace.agent-host-copilotcli)
    CR-->>RA: command exists!
    RA->>RA: throw Error(Cannot register two commands with same id)
Loading

Affected Files

File Role
src/vs/workbench/contrib/chat/electron-browser/chat.contribution.ts Static command registration (the producer of the conflict)
src/vs/workbench/contrib/chat/browser/chatSessions/chatSessions.contribution.ts Dynamic autorun-based registration via registerAction2
src/vs/platform/actions/common/actions.ts:720 Crash site — registerAction2 throws on duplicate
src/vs/workbench/contrib/chat/browser/agentSessions/agentHost/agentHostChatContribution.ts Triggers registerChatSessionContribution when agent host starts

Repro Steps

  1. Launch VS Code Insiders (Electron/desktop) with a local agent host configured
  2. The agent host starts and surfaces an AgentInfo with provider: 'copilotcli'
  3. agentHostChatContribution._handleRootStateChange registers the agent
  4. registerChatSessionContribution fires _onDidChangeAvailability
  5. The autorun tries to call registerAction2 for the command that was already statically registered
  6. Error thrown: "Cannot register two commands with the same id"

How the Fix Works

Chosen approach (src/vs/workbench/contrib/chat/electron-browser/chat.contribution.ts): Remove the static CommandsRegistry.registerCommand for openNewChatSessionInPlace.agent-host-copilotcli. This is the data producer — it creates the pre-existing command entry that causes registerAction2 to throw. The dynamic autorun-based registration (via registerChatSessionContribution + the autorun in ChatSessionsService) now handles this command, and stable automation commands (openNewSessionSidebar.agent-host-copilotcli, openNewSessionEditor.agent-host-copilotcli) remain registered for the pre-agent-host-start use case.

Alternatives considered: Adding a guard in registerNewSessionInPlaceAction to skip registration when the command exists — rejected because this would lose the full Action2 metadata (title, category, precondition, menu items) and is a consumer-side fallback that hides the conflict rather than fixing the producer.

Recommended Owner

@meganrogge — authored the static command registration in PR #314187.

Generated by errors-fix · ● 128.8M ·

…utorun-based registration (fixes #318223)

The static CommandsRegistry.registerCommand for
openNewChatSessionInPlace.agent-host-copilotcli conflicted with the
dynamic registerAction2 call in the ChatSessionsService autorun. The
autorun (triggered by registerChatSessionContribution) attempts to
register the same command via registerAction2, which throws if the
command already exists. Remove the static registration since the autorun
now handles it, and the stable openNewSessionSidebar/openNewSessionEditor
commands remain for automation needs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 25, 2026 15:58
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot marked this pull request as ready for review May 25, 2026 16:00
@vs-code-engineering vs-code-engineering Bot enabled auto-merge (squash) May 25, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Error] unhandlederror-Cannot register two commands with the same id: workbench.action.chat.openNewChatSessi...

2 participants