Skip to content

Commit 41bd742

Browse files
fix(webview): restore both source definitions of a custom mode on rollback
The batch rollback snapshotted getCustomModes(), which hides a global definition behind the project mode of the same slug. A batch that overwrote the global copy through that slug then restored only the project copy, leaving the global definition permanently replaced. The snapshot now reads both source files (getCustomModeDefinitions) and restores each entry to its own file. New test: a slug that exists as a project and a global mode must come back as both, and the merged read must not be enough to satisfy it. ClineProvider.spec 383 + CustomModesManager.spec 50 passed, tsc and eslint clean.
1 parent 610b6de commit 41bd742

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎src/core/config/CustomModesManager.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,28 @@ export class CustomModesManager {
353353
}
354354
}
355355

356+
/**
357+
* Every definition of a mode, per source file, without the project-over-global hiding that
358+
* `getCustomModes` applies. A rollback has to restore both: a batch that overwrites a global
359+
* definition through a slug that also exists as a project mode would otherwise leave the global
360+
* copy permanently replaced by the batch's version.
361+
*/
362+
public async getCustomModeDefinitions(slugs: readonly string[]): Promise<ModeConfig[]> {
363+
const wanted = new Set(slugs)
364+
const settingsPath = await this.getCustomModesFilePath()
365+
const settingsModes = await this.loadModesFromFile(settingsPath)
366+
const roomodesPath = await this.getWorkspaceRoomodes()
367+
const roomodesModes = roomodesPath ? await this.loadModesFromFile(roomodesPath) : []
368+
369+
return [
370+
...roomodesModes
371+
.filter((mode) => wanted.has(mode.slug))
372+
.map((mode) => ({ ...mode, source: "project" as const })),
373+
...settingsModes
374+
.filter((mode) => wanted.has(mode.slug))
375+
.map((mode) => ({ ...mode, source: "global" as const })),
376+
]
377+
}
356378
public async getCustomModes(): Promise<ModeConfig[]> {
357379
// Check if we have a valid cached result.
358380
const now = Date.now()

‎src/core/webview/ClineProvider.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5002,9 +5002,12 @@ export class ClineProvider
50025002
if (configuration) {
50035003
// Capture the modes this batch overrides before registering them: a later failure
50045004
// must restore a previously installed mode instead of leaving the batch's copy.
5005+
// Snapshot every definition of every slug this batch touches, one per source file. Filtering
5006+
// the merged list would keep only the project copy of a slug that exists in both files, and
5007+
// the rollback would then restore that copy while the global definition stayed overwritten.
50055008
const savedCustomModes = configuration.customModes?.length
5006-
? (await this.customModesManager.getCustomModes()).filter((mode) =>
5007-
configuration.customModes!.some((candidate) => candidate.slug === mode.slug),
5009+
? await this.customModesManager.getCustomModeDefinitions(
5010+
configuration.customModes.map((candidate) => candidate.slug),
50085011
)
50095012
: []
50105013

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6936,6 +6936,56 @@ describe("ClineProvider", () => {
69366936
mockContext.globalState.update = originalUpdate
69376937
await provider.dispose()
69386938
})
6939+
it("restores both definitions of a slug that exists as a project and a global mode", async () => {
6940+
const provider = new ClineProvider(
6941+
mockContext,
6942+
mockOutputChannel,
6943+
"sidebar",
6944+
new ContextProxy(mockContext),
6945+
new WebviewFocusTracker(),
6946+
)
6947+
const projectCopy = {
6948+
slug: "shared-slug",
6949+
name: "Project copy",
6950+
roleDefinition: "r",
6951+
groups: ["read" as const],
6952+
source: "project" as const,
6953+
}
6954+
const globalCopy = {
6955+
slug: "shared-slug",
6956+
name: "Global copy",
6957+
roleDefinition: "r",
6958+
groups: ["read" as const],
6959+
source: "global" as const,
6960+
}
6961+
const manager = provider["customModesManager"]
6962+
Object.assign(manager, {
6963+
// The merged read hides the global copy behind the project one; the rollback snapshot must
6964+
// not, or restoring it would write the project definition to the global file as well.
6965+
getCustomModes: vi.fn().mockResolvedValue([projectCopy]),
6966+
getCustomModeDefinitions: vi.fn().mockResolvedValue([projectCopy, globalCopy]),
6967+
updateCustomMode: vi.fn().mockResolvedValue(undefined),
6968+
removeCustomModeEntry: vi.fn().mockResolvedValue(true),
6969+
})
6970+
vi.spyOn(provider, "setValues").mockRejectedValue(new Error("configuration write failed"))
6971+
6972+
await expect(
6973+
provider.createTask(
6974+
"text",
6975+
undefined,
6976+
undefined,
6977+
{},
6978+
{ customModes: [{ ...globalCopy, name: "Batch copy" }] },
6979+
),
6980+
).rejects.toThrow("configuration write failed")
6981+
6982+
// Both copies are restored, each to its own file, so the batch's version cannot outlive the
6983+
// rollback in the global file while the project file gets the pre-batch definition back.
6984+
expect(manager.updateCustomMode).toHaveBeenCalledWith("shared-slug", projectCopy)
6985+
expect(manager.updateCustomMode).toHaveBeenCalledWith("shared-slug", globalCopy)
6986+
6987+
await provider.dispose()
6988+
})
69396989
it("should remove a custom mode the batch registered when the batch fails", async () => {
69406990
const provider = new ClineProvider(
69416991
mockContext,
@@ -6952,6 +7002,8 @@ describe("ClineProvider", () => {
69527002
const manager = provider["customModesManager"]
69537003
Object.assign(manager, {
69547004
getCustomModes: vi.fn().mockResolvedValue([savedMode]),
7005+
// The rollback snapshot reads both source files, so the double answers that reader too.
7006+
getCustomModeDefinitions: vi.fn().mockResolvedValue([savedMode]),
69557007
updateCustomMode: vi.fn().mockResolvedValue(undefined),
69567008
removeCustomModeEntry: vi.fn().mockResolvedValue(true),
69577009
})
@@ -6986,6 +7038,8 @@ describe("ClineProvider", () => {
69867038
const manager = provider["customModesManager"]
69877039
Object.assign(manager, {
69887040
getCustomModes: vi.fn().mockResolvedValue([savedMode]),
7041+
// The rollback snapshot reads both source files, so the double answers that reader too.
7042+
getCustomModeDefinitions: vi.fn().mockResolvedValue([savedMode]),
69897043
updateCustomMode: vi.fn((slug: string, mode: typeof savedMode) => {
69907044
calls.push([slug, mode.name])
69917045
// The batch's second registration is the one that throws.

0 commit comments

Comments
 (0)