Skip to content

Commit ed3ff3f

Browse files
committed
fix(webview): keep committed mode switches out of the reconcile rollback
The mode-switch reconcile rolled the mode back even when the mode-linked activation had already written shared state (profile, provider settings, mode mapping, sticky task profile): a later failure then paired the old mode with the new profile, the very mismatch the reconcile exists to prevent. The catch now keeps the switch once activation has started writing, and also when the mutation signal has aborted - the queue has advanced past the timed-out switch, so a newer mutation may own the mode stores. The trailing state post moved outside the reconciled region: a UI-refresh failure after a fully committed switch is logged, not rolled back. Three regression tests included.
1 parent f76a524 commit ed3ff3f

2 files changed

Lines changed: 207 additions & 5 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,10 @@ export class ClineProvider
24072407
}
24082408
}
24092409

2410+
// Once the mode-linked activation below has started writing shared state, a
2411+
// mode-only rollback would pair the old mode with the new profile: the catch must
2412+
// then keep the switch instead of reconciling.
2413+
let activationStarted = false
24102414
try {
24112415
if (viewScopedSwitch) {
24122416
// Pin the view's durable per-view mode first: the pin is this view's own
@@ -2466,6 +2470,7 @@ export class ClineProvider
24662470
const hasActualSettings = !!fullProfile.apiProvider
24672471

24682472
if (hasActualSettings) {
2473+
activationStarted = true
24692474
await this.activateProviderProfileUnlocked(
24702475
{ name: profile.name },
24712476
targetTask === null ? { skipCurrentTaskRebuild: true } : undefined,
@@ -2489,17 +2494,36 @@ export class ClineProvider
24892494
}
24902495
}
24912496
}
2492-
2493-
if (targetTask !== null) {
2494-
await this.postStateToWebview()
2495-
}
24962497
} catch (error) {
2497-
await reconcileSwitchWrites()
2498+
// Two reasons to keep the switch instead of reconciling: once the mode-linked
2499+
// activation has started writing shared state, a mode-only rollback would pair
2500+
// the old mode with the new profile; and after a timeout abort the queue has
2501+
// advanced, so a newer mutation may own these fields and replaying the
2502+
// pre-switch values would clobber it.
2503+
if (!activationStarted && !signal?.aborted) {
2504+
await reconcileSwitchWrites()
2505+
} else if (signal?.aborted) {
2506+
this.log(`[handleModeSwitch] Skipping rollback for aborted switch to "${newMode}"`)
2507+
}
24982508
this.log(
24992509
`[handleModeSwitch] Failed to persist mode "${newMode}": ${error instanceof Error ? error.message : String(error)}`,
25002510
)
25012511
throw error
25022512
}
2513+
2514+
// The switch has committed: a UI-refresh failure must not roll it back, so the
2515+
// trailing post runs outside the reconciled region and its failure is only logged.
2516+
if (targetTask !== null) {
2517+
try {
2518+
await this.postStateToWebview()
2519+
} catch (postError: unknown) {
2520+
this.log(
2521+
`[handleModeSwitch] Mode switch to "${newMode}" committed but the state post failed: ${
2522+
postError instanceof Error ? postError.message : String(postError)
2523+
}`,
2524+
)
2525+
}
2526+
}
25032527
}
25042528

25052529
// Provider Profile Management

‎src/core/webview/__tests__/ClineProvider.sticky-mode.spec.ts‎

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,184 @@ describe("ClineProvider - Sticky Mode", () => {
740740
expect(provider.getValue("mode")).toBe("code")
741741
})
742742

743+
it("keeps the committed mode switch when the post-activation state refresh fails", async () => {
744+
// Same typed double as the activation-failure rollback test above.
745+
const mockTask = Object.assign(
746+
{} as Task,
747+
{
748+
taskId: "test-task-id",
749+
taskMode: "code",
750+
_taskMode: "code",
751+
emit: vi.fn(),
752+
saveClineMessages: vi.fn(),
753+
clineMessages: [],
754+
apiConversationHistory: [],
755+
updateApiConfiguration: vi.fn(),
756+
} as Partial<Task>,
757+
)
758+
const historyItem: HistoryItem = {
759+
id: "test-task-id",
760+
ts: Date.now(),
761+
task: "Test task",
762+
mode: "code",
763+
number: 1,
764+
tokensIn: 0,
765+
tokensOut: 0,
766+
cacheWrites: 0,
767+
cacheReads: 0,
768+
totalCost: 0,
769+
}
770+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
771+
const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
772+
await provider.saveViewState("mode", "code")
773+
await provider.contextProxy.setValue("mode", "code")
774+
775+
// The mode-linked profile resolves and its activation lands (writing the
776+
// shared profile like the real activation does); only the trailing state
777+
// post rejects. By then the switch has fully committed - ModeChanged was
778+
// emitted and the activation persisted the new profile - so a UI refresh
779+
// failure must not roll the mode back: that would pair the old mode with
780+
// the new profile, the very mismatch the reconcile exists to prevent.
781+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockResolvedValue("architect-profile-id")
782+
vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue([
783+
{ id: "architect-profile-id", name: "architect-profile", apiProvider: providerIdentifiers.openrouter },
784+
])
785+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
786+
name: "architect-profile",
787+
id: "architect-profile-id",
788+
apiProvider: providerIdentifiers.openrouter,
789+
})
790+
provider["activateProviderProfileUnlocked"] = vi.fn().mockImplementation(async () => {
791+
await provider.contextProxy.setValue("currentApiConfigName", "architect-profile")
792+
})
793+
vi.spyOn(provider, "postStateToWebview").mockRejectedValue(new Error("post failed"))
794+
await provider.addClineToStack(mockTask)
795+
796+
await provider["handleModeSwitchUnlocked"]("architect", mockTask)
797+
798+
expect(provider.getValue("mode")).toBe("architect")
799+
expect(provider["viewLocalState"].mode).toBe("architect")
800+
expect(mockTask["_taskMode"]).toBe("architect")
801+
expect(provider.contextProxy.getValue("currentApiConfigName")).toBe("architect-profile")
802+
expect(updateTaskHistorySpy).not.toHaveBeenCalledWith(expect.objectContaining({ mode: "code" }))
803+
})
804+
805+
it("keeps the switched mode when the mode-linked activation fails after writing", async () => {
806+
// Same typed double as the activation-failure rollback test above.
807+
const mockTask = Object.assign(
808+
{} as Task,
809+
{
810+
taskId: "test-task-id",
811+
taskMode: "code",
812+
_taskMode: "code",
813+
emit: vi.fn(),
814+
saveClineMessages: vi.fn(),
815+
clineMessages: [],
816+
apiConversationHistory: [],
817+
updateApiConfiguration: vi.fn(),
818+
} as Partial<Task>,
819+
)
820+
const historyItem: HistoryItem = {
821+
id: "test-task-id",
822+
ts: Date.now(),
823+
task: "Test task",
824+
mode: "code",
825+
number: 1,
826+
tokensIn: 0,
827+
tokensOut: 0,
828+
cacheWrites: 0,
829+
cacheReads: 0,
830+
totalCost: 0,
831+
}
832+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
833+
const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
834+
await provider.saveViewState("mode", "code")
835+
await provider.contextProxy.setValue("mode", "code")
836+
837+
// The mode-linked activation lands its shared writes and only then rejects
838+
// (e.g. its own state post or setModeConfig step failing). A mode-only rollback
839+
// would pair the old mode with the profile the activation already switched to,
840+
// the mismatch the reconcile exists to prevent: the switch must stand and the
841+
// error must still surface.
842+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockResolvedValue("architect-profile-id")
843+
vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue([
844+
{ id: "architect-profile-id", name: "architect-profile", apiProvider: providerIdentifiers.openrouter },
845+
])
846+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
847+
name: "architect-profile",
848+
id: "architect-profile-id",
849+
apiProvider: providerIdentifiers.openrouter,
850+
})
851+
provider["activateProviderProfileUnlocked"] = vi.fn().mockImplementation(async () => {
852+
await provider.contextProxy.setValue("currentApiConfigName", "architect-profile")
853+
throw new Error("activation step failed")
854+
})
855+
await provider.addClineToStack(mockTask)
856+
857+
await expect(provider["handleModeSwitchUnlocked"]("architect", mockTask)).rejects.toThrow(
858+
"activation step failed",
859+
)
860+
861+
expect(provider.getValue("mode")).toBe("architect")
862+
expect(provider["viewLocalState"].mode).toBe("architect")
863+
expect(mockTask["_taskMode"]).toBe("architect")
864+
expect(provider.contextProxy.getValue("currentApiConfigName")).toBe("architect-profile")
865+
expect(updateTaskHistorySpy).not.toHaveBeenCalledWith(expect.objectContaining({ mode: "code" }))
866+
})
867+
868+
it("keeps the switched mode when the mutation signal aborted before the failure", async () => {
869+
// Same typed double as the activation-failure rollback test above.
870+
const mockTask = Object.assign(
871+
{} as Task,
872+
{
873+
taskId: "test-task-id",
874+
taskMode: "code",
875+
_taskMode: "code",
876+
emit: vi.fn(),
877+
saveClineMessages: vi.fn(),
878+
clineMessages: [],
879+
apiConversationHistory: [],
880+
updateApiConfiguration: vi.fn(),
881+
} as Partial<Task>,
882+
)
883+
const historyItem: HistoryItem = {
884+
id: "test-task-id",
885+
ts: Date.now(),
886+
task: "Test task",
887+
mode: "code",
888+
number: 1,
889+
tokensIn: 0,
890+
tokensOut: 0,
891+
cacheWrites: 0,
892+
cacheReads: 0,
893+
totalCost: 0,
894+
}
895+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
896+
const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
897+
await provider.saveViewState("mode", "code")
898+
await provider.contextProxy.setValue("mode", "code")
899+
900+
// The pin and shared writes land; then the mutation timeout aborts this
901+
// switch and a later step rejects. The queue has already advanced past the
902+
// timed-out switch, so a newer mutation may own the mode stores: the late
903+
// rollback must not replay the pre-switch values over them.
904+
const controller = new AbortController()
905+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockImplementation(async () => {
906+
controller.abort()
907+
throw new Error("late activation read failure")
908+
})
909+
await provider.addClineToStack(mockTask)
910+
911+
await expect(
912+
provider["handleModeSwitchUnlocked"]("architect", mockTask, controller.signal),
913+
).rejects.toThrow("late activation read failure")
914+
915+
expect(provider.getValue("mode")).toBe("architect")
916+
expect(provider["viewLocalState"].mode).toBe("architect")
917+
expect(mockTask["_taskMode"]).toBe("architect")
918+
expect(updateTaskHistorySpy).not.toHaveBeenCalledWith(expect.objectContaining({ mode: "code" }))
919+
})
920+
743921
it("keeps the cancellation result when the abort rollback write itself fails", async () => {
744922
const mockTask = Object.assign(
745923
{} as Task,

0 commit comments

Comments
 (0)