Skip to content

Commit c2ee9dc

Browse files
committed
fix(webview): announce aborted mode switches only while their commit is current
1 parent 94c5c76 commit c2ee9dc

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
@@ -2546,7 +2546,13 @@ export class ClineProvider
25462546
}
25472547

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

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

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

1253+
it("stays silent when a newer mutation supersedes the aborted switch", async () => {
1254+
// Same typed double as the activation-failure rollback test above.
1255+
const mockTask = Object.assign(
1256+
{} as Task,
1257+
{
1258+
taskId: "test-task-id",
1259+
taskMode: "code",
1260+
_taskMode: "code",
1261+
emit: vi.fn(),
1262+
saveClineMessages: vi.fn(),
1263+
clineMessages: [],
1264+
apiConversationHistory: [],
1265+
updateApiConfiguration: vi.fn(),
1266+
} as Partial<Task>,
1267+
)
1268+
const historyItem: HistoryItem = {
1269+
id: "test-task-id",
1270+
ts: Date.now(),
1271+
task: "Test task",
1272+
mode: "code",
1273+
number: 1,
1274+
tokensIn: 0,
1275+
tokensOut: 0,
1276+
cacheWrites: 0,
1277+
cacheReads: 0,
1278+
totalCost: 0,
1279+
}
1280+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
1281+
vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
1282+
await provider.saveViewState("mode", "code")
1283+
await provider.contextProxy.setValue("mode", "code")
1284+
1285+
// The abort lands after this switch's mode writes, and a newer mutation
1286+
// immediately moves the mode stores to its own mode: this switch's commit
1287+
// is superseded, so announcing it would clobber the newer owner - the
1288+
// emit/post tail must stay silent.
1289+
const emitSpy = vi.spyOn(provider, "emit")
1290+
const postSpy = vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
1291+
const controller = new AbortController()
1292+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockImplementation(async () => {
1293+
controller.abort()
1294+
await provider.setValue("mode", "ask")
1295+
return undefined
1296+
})
1297+
vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue([])
1298+
await provider.addClineToStack(mockTask)
1299+
1300+
await provider["handleModeSwitchUnlocked"]("architect", mockTask, controller.signal)
1301+
1302+
expect(provider.getValue("mode")).toBe("ask")
1303+
expect(emitSpy).not.toHaveBeenCalledWith("modeChanged", "architect")
1304+
expect(postSpy).not.toHaveBeenCalled()
1305+
})
12531306
it("keeps the cancellation result when the abort rollback write itself fails", async () => {
12541307
const mockTask = Object.assign(
12551308
{} as Task,

0 commit comments

Comments
 (0)