Skip to content

Commit cef38f5

Browse files
fix(webview): keep a view's mode pin when a different mode is deleted
The routed default-mode reset fired for every view. provider.setValue writes the shared store and this view's durable pin together, so deleting mode A moved a view pinned to mode B onto the default as well. Measured on 9347e16: a view pinned to keep-mode served keep-mode before the deletion and code after it, and code was persisted into viewStates for that view. The reset now keys off the mode this view actually resolves to - its pin, or the shared selection when it has no pin of its own - and only that write goes through the provider. When the shared selection is the deleted slug but this view pins another mode, only the shared layer moves, so the views that follow the shared selection stop resolving to a slug that no longer exists while this view keeps its own. pinnedModeName exposes the view-local slug the way pinnedProfileName already exposes the profile name. Tests: "keeps a view pinned to another mode on that mode when a different mode is deleted" and "resets the shared selection without moving a view pinned to another mode"; the unpinned case now seeds the shared selection with the deleted slug, which is the selection this reset exists for. Control with only the production change reverted: both new tests fail with "expected code to be keep-mode". With the fix: ClineProvider.spec.ts 484 passed, webviewMessageHandler 180, sticky-mode 46, parallelMode 55, the core/webview folder 35 files / 1178 passed, tsc 0 and eslint 0 on the touched files.
1 parent 71d7b57 commit cef38f5

3 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ export class ClineProvider
530530
return this.viewLocalState.currentApiConfigName
531531
}
532532

533+
/**
534+
* This view's pinned mode slug from the view-local buffer, or undefined when the view
535+
* follows the shared selection. Exposed so a custom-mode deletion can key the reset
536+
* onto the view it actually applies to instead of moving every view's pin.
537+
*/
538+
get pinnedModeName(): string | undefined {
539+
return this.viewLocalState.mode
540+
}
533541
public isViewLaunched = false
534542
public settingsImportedAt?: number
535543
public readonly latestAnnouncementId = "oct-2026-v3.94.0-models-oauth-chat" // v3.94.0 model support, MCP OAuth, and chat reliability

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10547,6 +10547,26 @@ describe("ClineProvider", () => {
1054710547
// getState() is what the fan-out serializes for this view: it merges the view's pin
1054810548
// over the shared store, so it is the mode the webview actually renders.
1054910549
const servedMode = async () => (await provider.getState()).mode
10550+
// Both slugs the cases below name, so the mode the provider validates against always
10551+
// carries the mode the view under test is pinned to.
10552+
const seedTwoModes = () => {
10553+
Object.assign(provider.customModesManager, {
10554+
getCustomModes: vi.fn().mockResolvedValue([
10555+
{
10556+
slug: "gone-mode",
10557+
name: "Gone Mode",
10558+
roleDefinition: "A mode that is about to be deleted",
10559+
groups: ["read"] as const,
10560+
},
10561+
{
10562+
slug: "keep-mode",
10563+
name: "Keep Mode",
10564+
roleDefinition: "A mode this view is pinned to",
10565+
groups: ["read"] as const,
10566+
},
10567+
]),
10568+
})
10569+
}
1055010570

1055110571
it("moves a view pinned to the deleted mode onto the default mode", async () => {
1055210572
const messageHandler = await handlerForView("pinned-deleted-view")
@@ -10567,6 +10587,9 @@ describe("ClineProvider", () => {
1056710587

1056810588
it("reports the default mode to a view that has no pin of its own", async () => {
1056910589
const messageHandler = await handlerForView("unpinned-deleted-view")
10590+
// The shared selection is the slug being deleted - the selection this reset exists
10591+
// for - and the view has no pin of its own to keep it away from it.
10592+
await provider.contextProxy.setValue("mode", "gone-mode")
1057010593

1057110594
await messageHandler({ type: "deleteCustomMode", slug: "gone-mode" })
1057210595

@@ -10580,6 +10603,38 @@ describe("ClineProvider", () => {
1058010603
})
1058110604
})
1058210605

10606+
// Deleting one mode must not move a view that is pinned to a different one: the reset
10607+
// keys off the mode this view actually resolves to - its pin, or the shared selection
10608+
// when it has no pin of its own.
10609+
it("keeps a view pinned to another mode on that mode when a different mode is deleted", async () => {
10610+
const messageHandler = await handlerForView("pinned-other-view")
10611+
seedTwoModes()
10612+
await provider.saveViewState("mode", "keep-mode")
10613+
10614+
await messageHandler({ type: "deleteCustomMode", slug: "gone-mode" })
10615+
10616+
expect(await servedMode()).toBe("keep-mode")
10617+
expect(provider["viewLocalState"].mode).toBe("keep-mode")
10618+
// The shared selection was never the deleted slug, so nothing writes over it here.
10619+
expect(provider.contextProxy.getValue("mode")).toBeUndefined()
10620+
})
10621+
10622+
it("resets the shared selection without moving a view pinned to another mode", async () => {
10623+
const messageHandler = await handlerForView("pinned-other-shared-view")
10624+
seedTwoModes()
10625+
// The shared selection is the mode being deleted while this view pins another one:
10626+
// the shared layer must move so unpinned views stop resolving to the deleted slug,
10627+
// while this view keeps its own pin.
10628+
await provider.contextProxy.setValue("mode", "gone-mode")
10629+
await provider.saveViewState("mode", "keep-mode")
10630+
10631+
await messageHandler({ type: "deleteCustomMode", slug: "gone-mode" })
10632+
10633+
expect(provider.contextProxy.getValue("mode")).toBe("code")
10634+
expect(provider["viewLocalState"].mode).toBe("keep-mode")
10635+
expect(await servedMode()).toBe("keep-mode")
10636+
})
10637+
1058310638
it("moves the pin onto the mode a pinned view just saved", async () => {
1058410639
const messageHandler = await handlerForView("pinned-saved-view")
1058510640
await provider.saveViewState("mode", "gone-mode")

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,10 +2907,21 @@ export const webviewMessageHandler = async (
29072907
}
29082908
}
29092909

2910-
// Switch back to default mode after deletion, through the provider so this view's
2911-
// durable pin follows the slug being written: a direct ContextProxy write would move
2912-
// the shared store and leave a view pinned to the mode it just deleted pinning it.
2913-
await provider.setValue("mode", defaultModeSlug)
2910+
// Move onto the default mode only for the view this call applies to: the one whose
2911+
// effective mode - its durable pin, or the shared selection when it has no pin of
2912+
// its own - is the slug being deleted. Routing that write through the provider keeps
2913+
// this view's pin and the shared store on the same slug. A view pinned to a mode this
2914+
// deletion did not touch keeps its pin: moving it here used to overwrite an unrelated
2915+
// selection with the default. When the shared selection is the deleted slug but this
2916+
// view pins something else, only the shared layer moves, so the views that follow it
2917+
// do not resolve to a slug that no longer exists while this view keeps its own.
2918+
const pinnedMode = provider.pinnedModeName
2919+
const sharedMode = getGlobalState("mode")
2920+
if ((pinnedMode ?? sharedMode) === message.slug) {
2921+
await provider.setValue("mode", defaultModeSlug)
2922+
} else if (sharedMode === message.slug) {
2923+
await updateGlobalState("mode", defaultModeSlug)
2924+
}
29142925
await provider.postStateToWebview()
29152926
}
29162927
break

0 commit comments

Comments
 (0)