Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/activate/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ export function getVisibleProviderOrLog(outputChannel: vscode.OutputChannel): Cl
let sidebarPanel: vscode.WebviewView | undefined = undefined
let tabPanel: vscode.WebviewPanel | undefined = undefined

// Callback invoked when a tab provider is created via openClineInNewTab.
// This allows the API to register event listeners on dynamically created tab providers.
let onTabProviderCreatedCallback: ((provider: ClineProvider) => void) | undefined

/**
* Register a callback that will be invoked whenever a new tab provider is
* created via `openClineInNewTab`. Used by the API to forward events from
* tab providers to the `RooCodeAPI` EventEmitter.
*/
export function setOnTabProviderCreated(callback: (provider: ClineProvider) => void): void {
onTabProviderCreatedCallback = callback
}

/**
* Get the currently active panel
* @returns WebviewPanel或WebviewView
Expand Down Expand Up @@ -270,5 +283,8 @@ export const openClineInNewTab = async ({ context, outputChannel }: Omit<Registe
await delay(100)
await vscode.commands.executeCommand("workbench.action.lockEditorGroup")

// Notify the API (if registered) so it can forward events from this tab provider.
onTabProviderCreatedCallback?.(tabProvider)

return tabProvider
}
68 changes: 68 additions & 0 deletions src/extension/__tests__/api-tab-events.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, it, expect, vi, beforeEach } from "vitest"
import * as vscode from "vscode"

import { API } from "../api"
import { ClineProvider } from "../../core/webview/ClineProvider"
import { setOnTabProviderCreated } from "../../activate/registerCommands"

vi.mock("vscode")
vi.mock("../../core/webview/ClineProvider")

// Capture the callback registered by the API constructor.
let capturedCallback: ((provider: ClineProvider) => void) | undefined

vi.mock("../../activate/registerCommands", () => ({
openClineInNewTab: vi.fn(),
setOnTabProviderCreated: vi.fn((cb: (provider: ClineProvider) => void) => {
capturedCallback = cb
}),
}))

describe("API - Tab Provider Event Registration", () => {
let mockOutputChannel: vscode.OutputChannel
let mockSidebarProvider: ClineProvider
let api: API

beforeEach(() => {
capturedCallback = undefined

mockOutputChannel = {
appendLine: vi.fn(),
} as unknown as vscode.OutputChannel

mockSidebarProvider = {
context: {} as vscode.ExtensionContext,
on: vi.fn(),
postMessageToWebview: vi.fn(),
getCurrentTaskStack: vi.fn().mockReturnValue([]),
getCurrentTask: vi.fn().mockReturnValue(undefined),
viewLaunched: true,
} as unknown as ClineProvider

api = new API(mockOutputChannel, mockSidebarProvider, undefined, false)
})

it("should call setOnTabProviderCreated during construction", () => {
expect(setOnTabProviderCreated).toHaveBeenCalledWith(expect.any(Function))
expect(capturedCallback).toBeDefined()
})

it("should register listeners on tab providers created via commands", () => {
const mockTabProvider = {
on: vi.fn(),
context: {} as vscode.ExtensionContext,
} as unknown as ClineProvider

// Simulate a tab provider being created via command
capturedCallback!(mockTabProvider)

// registerListeners calls provider.on(RooCodeEventName.TaskCreated, ...)
// so we verify that on() was called on the tab provider
expect(mockTabProvider.on).toHaveBeenCalled()
})

it("should register listeners on the sidebar provider during construction", () => {
// The sidebar provider should also have listeners registered
expect(mockSidebarProvider.on).toHaveBeenCalled()
})
})
6 changes: 4 additions & 2 deletions src/extension/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { CloudService } from "@roo-code/cloud"

import { Package } from "../shared/package"
import { ClineProvider } from "../core/webview/ClineProvider"
import { openClineInNewTab } from "../activate/registerCommands"
import { openClineInNewTab, setOnTabProviderCreated } from "../activate/registerCommands"
import { getCommands } from "../services/command/commands"
import { getModels } from "../api/providers/fetchers/modelCache"

Expand Down Expand Up @@ -62,6 +62,9 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {

this.registerListeners(this.sidebarProvider)

// Ensure tab providers created via commands also get event listeners.
setOnTabProviderCreated((provider) => this.registerListeners(provider))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This callback now registers listeners on every tab provider created via openClineInNewTab, but startNewTask({ newTab: true }) (line ~188-189) also explicitly calls this.registerListeners(provider) after openClineInNewTab returns. The result is double-registration: every event from tab providers created through startNewTask will be emitted twice through the API EventEmitter. The explicit this.registerListeners(provider) call in startNewTask should be removed now that the callback handles it.

Fix it with Roo Code or mention @roomote and request a fix.

Copy link

Choose a reason for hiding this comment

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

@roomote fix this

Copy link
Contributor Author

@roomote roomote bot Feb 17, 2026

Choose a reason for hiding this comment

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

Fixaroo Clock   See task

Removed the duplicate registerListeners call in startNewTask since the setOnTabProviderCreated callback already handles it. All tests and checks pass.

View commit | Revert commit


if (socketPath) {
const ipc = (this.ipc = new IpcServer(socketPath, this.log))

Expand Down Expand Up @@ -183,7 +186,6 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
await vscode.commands.executeCommand("workbench.action.closeAllEditors")

provider = await openClineInNewTab({ context: this.context, outputChannel: this.outputChannel })
this.registerListeners(provider)
} else {
await vscode.commands.executeCommand(`${Package.name}.SidebarProvider.focus`)

Expand Down
Loading