Skip to content

Commit d0e3c3e

Browse files
fix(commands): keep the palette path off a hidden sidebar provider
CodeRabbit filed src/activate/registerCommands.ts:167 (root comment 4503339692): the sidebar plusButtonClicked evicts the registered provider's running task even when the sidebar is hidden, because the commandPalette rows this branch added keep the command reachable whenever a tab panel is not the active webview panel. Checked against the tree rather than the finding text: src/package.json gains a menus.commandPalette section (8 rows) in which zoo-code.plusButtonClicked sits under "activeWebviewPanelId != zoo-code.TabPanelProvider"; with a text editor focused and the sidebar collapsed that context key is unset, so the clause passes and the palette runs the sidebar handler. The rows restrict an already-listed command rather than introduce it - what changed is that these handlers dropped the guard upstream's copy had: at merge-base a4f774b plusButtonClicked opened with getVisibleProviderOrLog and returned when it found no visible instance (registerCommands.ts:90-104), so the same palette invocation logged and did nothing instead of aborting a task behind an invisible view. sidebarCommandTarget keeps the click-origin targeting while the registered provider's own view is visible, which a view/title click always is, and otherwise falls back to the visible instance - what these handlers did before the click-origin split: act on whichever surface is on screen, and do nothing when none is. Applied to plusButtonClicked, the destructive one, and to settingsButtonClicked, historyButtonClicked and marketplaceButtonClicked, which only post, so the four stay consistent about where a palette invocation lands. Telemetry still fires only once a target exists. registerCommands.spec.ts: the registered provider double states the view/title precondition (isViewVisible: true), and five cases pin the palette origin - plusButton click targeting the visible provider instead of evicting behind the hidden sidebar, plusButtonClicked doing nothing when no surface is visible (asserting the log line and that no plus telemetry fired), and an it.each over the three post-only commands. With the guard deleted the lane fails exactly those five and passes the other 62; with the guard in place the lane is 67 tests green. tsc --noEmit 0 errors, eslint --max-warnings=0 clean on both files, prettier --check clean, and src/eslint-suppressions.json is untouched, so no count moves. The pre-commit hook ran lint-staged on this commit (PATH prepended with C:/Users/eason/AppData/Roaming/npm so the hook resolves pnpm 10.8.1 instead of falling through to npx). No --no-verify was used.
1 parent 9ab5356 commit d0e3c3e

2 files changed

Lines changed: 111 additions & 8 deletions

File tree

‎src/activate/__tests__/registerCommands.spec.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ describe("registerCommands handlers", () => {
160160
refreshWorkspace: Mock
161161
webviewFocusTracker: WebviewFocusTracker
162162
retryWebviewRecovery: Mock<() => Promise<void>>
163+
isViewVisible: boolean
163164
}
164165
let mockVisibleProviderInstance: ClineProvider
165166
let handlers: Record<string, (...args: unknown[]) => unknown>
@@ -194,6 +195,10 @@ describe("registerCommands handlers", () => {
194195
evictCurrentTask: vi.fn().mockResolvedValue(undefined),
195196
refreshWorkspace: vi.fn().mockResolvedValue(undefined),
196197
webviewFocusTracker: new WebviewFocusTracker(),
198+
// The registered provider stands in for a visible sidebar: the view/title
199+
// origin of these commands always has one. The palette cases below flip this
200+
// flag to model a collapsed sidebar.
201+
isViewVisible: true,
197202
}
198203
;(ClineProvider.getVisibleInstance as Mock).mockReturnValue(mockVisibleProvider)
199204
;(vscode.commands.registerCommand as Mock).mockImplementation(
@@ -336,6 +341,64 @@ describe("registerCommands handlers", () => {
336341
expect(mockVisibleProvider.postMessageToWebview).not.toHaveBeenCalled()
337342
})
338343

344+
// The `commandPalette` rows sit beside the `view/title` rows in src/package.json and keep
345+
// these commands reachable from the palette whenever a tab panel is not the active webview
346+
// panel. That includes a text editor focused with the sidebar collapsed: there
347+
// `activeWebviewPanelId` is unset, so the `activeWebviewPanelId != zoo-code.TabPanelProvider`
348+
// clause passes and the handler runs against a provider whose view is not on screen.
349+
it("plusButtonClicked targets the visible provider when the registered sidebar provider is hidden", async () => {
350+
const collapsedSidebarTabProvider = {
351+
postMessageToWebview: vi.fn().mockResolvedValue(undefined),
352+
evictCurrentTask: vi.fn().mockResolvedValue(undefined),
353+
refreshWorkspace: vi.fn().mockResolvedValue(undefined),
354+
}
355+
mockProvider.isViewVisible = false
356+
;(ClineProvider.getVisibleInstance as Mock).mockReturnValue(collapsedSidebarTabProvider)
357+
358+
await handlers["zoo-code.plusButtonClicked"]()
359+
360+
// The registered provider holds a running task behind a view nobody is looking at: targeting
361+
// it would abort that task and post into a webview that is not visible.
362+
expect(mockProvider.evictCurrentTask).not.toHaveBeenCalled()
363+
expect(mockProvider.refreshWorkspace).not.toHaveBeenCalled()
364+
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
365+
expect(collapsedSidebarTabProvider.evictCurrentTask).toHaveBeenCalledTimes(1)
366+
expect(collapsedSidebarTabProvider.postMessageToWebview).toHaveBeenNthCalledWith(1, {
367+
type: "action",
368+
action: "chatButtonClicked",
369+
})
370+
expect(collapsedSidebarTabProvider.postMessageToWebview).toHaveBeenNthCalledWith(2, {
371+
type: "action",
372+
action: "focusInput",
373+
})
374+
})
375+
376+
it("plusButtonClicked leaves the registered task alone when no surface is visible", async () => {
377+
mockProvider.isViewVisible = false
378+
;(ClineProvider.getVisibleInstance as Mock).mockReturnValue(undefined)
379+
380+
await handlers["zoo-code.plusButtonClicked"]()
381+
382+
expect(mockProvider.evictCurrentTask).not.toHaveBeenCalled()
383+
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
384+
expect(TelemetryService.instance.captureTitleButtonClicked).not.toHaveBeenCalledWith("plus")
385+
expect(mockOutputChannel.appendLine).toHaveBeenCalledWith("Cannot find any visible Roo Code instances.")
386+
})
387+
388+
it.each(["settingsButtonClicked", "historyButtonClicked", "marketplaceButtonClicked"])(
389+
"%s posts on the visible provider when the registered sidebar provider is hidden",
390+
(action) => {
391+
const collapsedSidebarTabProvider = { postMessageToWebview: vi.fn().mockResolvedValue(undefined) }
392+
mockProvider.isViewVisible = false
393+
;(ClineProvider.getVisibleInstance as Mock).mockReturnValue(collapsedSidebarTabProvider)
394+
395+
handlers[`zoo-code.${action}`]()
396+
397+
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
398+
expect(collapsedSidebarTabProvider.postMessageToWebview).toHaveBeenCalledWith({ type: "action", action })
399+
},
400+
)
401+
339402
// The `*InTab` handlers serve the `editor/title` menu: they target the
340403
// instance that owns the tracked tab panel, resolved via
341404
// ClineProvider.getInstanceForView.

‎src/activate/registerCommands.ts‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ const postActions = (
137137
}
138138
}
139139

140+
/**
141+
* Resolve the provider a sidebar-scoped command should act on.
142+
*
143+
* The `view/title` rows scope these commands to the sidebar view, so a title-bar
144+
* click always has a visible sidebar and the registered provider is the right
145+
* target. The `commandPalette` rows added beside them do not: with a text editor
146+
* focused and the sidebar collapsed, `activeWebviewPanelId` is unset, the
147+
* `activeWebviewPanelId != zoo-code.TabPanelProvider` clause passes, and the
148+
* palette runs the same handler. Falling back to the visible instance keeps the
149+
* palette behaviour these handlers had before the click-origin split - act on
150+
* whichever surface is on screen, and do nothing when none is - instead of
151+
* evicting a task behind a webview nobody is looking at.
152+
*/
153+
const sidebarCommandTarget = (
154+
provider: ClineProvider,
155+
outputChannel: vscode.OutputChannel,
156+
): ClineProvider | undefined => (provider.isViewVisible ? provider : getVisibleProviderOrLog(outputChannel))
157+
140158
const getCommandsMap = ({
141159
context,
142160
outputChannel,
@@ -155,16 +173,23 @@ const getCommandsMap = ({
155173
// visible-instance heuristic, which would follow the user's focus to a
156174
// tab instance when both surfaces are open side-by-side. The `*InTab`
157175
// variants serve the `editor/title` menu and target the tab instance
158-
// through `getTabProvider()` instead.
176+
// through `getTabProvider()` instead. `sidebarCommandTarget` keeps that
177+
// targeting while the sidebar is visible and hands the palette path back to
178+
// the visible instance.
159179
plusButtonClicked: async () => {
180+
const target = sidebarCommandTarget(provider, outputChannel)
181+
if (!target) {
182+
return
183+
}
184+
160185
TelemetryService.instance.captureTitleButtonClicked("plus")
161186

162-
await provider.evictCurrentTask()
163-
await provider.refreshWorkspace()
164-
await provider.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
187+
await target.evictCurrentTask()
188+
await target.refreshWorkspace()
189+
await target.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
165190
// Send focusInput action immediately after chatButtonClicked
166191
// This ensures the focus happens after the view has switched
167-
await provider.postMessageToWebview({ type: "action", action: "focusInput" })
192+
await target.postMessageToWebview({ type: "action", action: "focusInput" })
168193
},
169194
plusButtonClickedInTab: async () => {
170195
const tabProvider = getTabProvider()
@@ -187,10 +212,15 @@ const getCommandsMap = ({
187212
openInNewTab: () =>
188213
openClineInNewTab({ context, outputChannel, webviewFocusTracker: provider.webviewFocusTracker }),
189214
settingsButtonClicked: () => {
215+
const target = sidebarCommandTarget(provider, outputChannel)
216+
if (!target) {
217+
return
218+
}
219+
190220
TelemetryService.instance.captureTitleButtonClicked("settings")
191221

192222
// Also explicitly post the visibility message to trigger scroll reliably.
193-
postActions(outputChannel, provider, ["settingsButtonClicked", "didBecomeVisible"], "settingsButtonClicked")
223+
postActions(outputChannel, target, ["settingsButtonClicked", "didBecomeVisible"], "settingsButtonClicked")
194224
},
195225
settingsButtonClickedInTab: () => {
196226
const tabProvider = getTabProvider()
@@ -208,9 +238,14 @@ const getCommandsMap = ({
208238
)
209239
},
210240
historyButtonClicked: () => {
241+
const target = sidebarCommandTarget(provider, outputChannel)
242+
if (!target) {
243+
return
244+
}
245+
211246
TelemetryService.instance.captureTitleButtonClicked("history")
212247

213-
postActions(outputChannel, provider, ["historyButtonClicked"], "historyButtonClicked")
248+
postActions(outputChannel, target, ["historyButtonClicked"], "historyButtonClicked")
214249
},
215250
historyButtonClickedInTab: () => {
216251
const tabProvider = getTabProvider()
@@ -223,7 +258,12 @@ const getCommandsMap = ({
223258
postActions(outputChannel, tabProvider, ["historyButtonClicked"], "historyButtonClickedInTab")
224259
},
225260
marketplaceButtonClicked: () => {
226-
postActions(outputChannel, provider, ["marketplaceButtonClicked"], "marketplaceButtonClicked")
261+
const target = sidebarCommandTarget(provider, outputChannel)
262+
if (!target) {
263+
return
264+
}
265+
266+
postActions(outputChannel, target, ["marketplaceButtonClicked"], "marketplaceButtonClicked")
227267
},
228268
marketplaceButtonClickedInTab: () => {
229269
const tabProvider = getTabProvider()

0 commit comments

Comments
 (0)