Skip to content

Commit 9e20cc9

Browse files
committed
fix(webview): announce aborted mode switches only while their commit is current
1 parent 1759c8e commit 9e20cc9

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2548,7 +2548,13 @@ export class ClineProvider
25482548
}
25492549

25502550
// The switch has committed: notify listeners only now, so a reconciled failure
2551-
// never leaves listeners tracking a mode the stores rolled back from.
2551+
// never leaves listeners tracking a mode the stores rolled back from. After an
2552+
// abort the mutation queue has advanced: announce only while this switch's
2553+
// committed mode is still current - a superseded commit stays silent because
2554+
// the newer mutation already announced its own mode.
2555+
if (signal?.aborted && this.getValues().mode !== newMode) {
2556+
return
2557+
}
25522558
if (viewScopedSwitch) {
25532559
this.emit(RooCodeEventName.ModeChanged, newMode)
25542560
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,59 @@ describe("ClineProvider - Sticky Mode", () => {
12031203
expect(postSpy).toHaveBeenCalled()
12041204
})
12051205

1206+
it("stays silent when a newer mutation supersedes the aborted switch", async () => {
1207+
// Same typed double as the activation-failure rollback test above.
1208+
const mockTask = Object.assign(
1209+
{} as Task,
1210+
{
1211+
taskId: "test-task-id",
1212+
taskMode: "code",
1213+
_taskMode: "code",
1214+
emit: vi.fn(),
1215+
saveClineMessages: vi.fn(),
1216+
clineMessages: [],
1217+
apiConversationHistory: [],
1218+
updateApiConfiguration: vi.fn(),
1219+
} as Partial<Task>,
1220+
)
1221+
const historyItem: HistoryItem = {
1222+
id: "test-task-id",
1223+
ts: Date.now(),
1224+
task: "Test task",
1225+
mode: "code",
1226+
number: 1,
1227+
tokensIn: 0,
1228+
tokensOut: 0,
1229+
cacheWrites: 0,
1230+
cacheReads: 0,
1231+
totalCost: 0,
1232+
}
1233+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
1234+
vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
1235+
await provider.saveViewState("mode", "code")
1236+
await provider.contextProxy.setValue("mode", "code")
1237+
1238+
// The abort lands after this switch's mode writes, and a newer mutation
1239+
// immediately moves the mode stores to its own mode: this switch's commit
1240+
// is superseded, so announcing it would clobber the newer owner - the
1241+
// emit/post tail must stay silent.
1242+
const emitSpy = vi.spyOn(provider, "emit")
1243+
const postSpy = vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
1244+
const controller = new AbortController()
1245+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockImplementation(async () => {
1246+
controller.abort()
1247+
await provider.setValue("mode", "ask")
1248+
return undefined
1249+
})
1250+
vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue([])
1251+
await provider.addClineToStack(mockTask)
1252+
1253+
await provider["handleModeSwitchUnlocked"]("architect", mockTask, controller.signal)
1254+
1255+
expect(provider.getValue("mode")).toBe("ask")
1256+
expect(emitSpy).not.toHaveBeenCalledWith("modeChanged", "architect")
1257+
expect(postSpy).not.toHaveBeenCalled()
1258+
})
12061259
it("keeps the cancellation result when the abort rollback write itself fails", async () => {
12071260
const mockTask = Object.assign(
12081261
{} as Task,

0 commit comments

Comments
 (0)