diff --git a/ui/src/stores/settings.ts b/ui/src/stores/settings.ts index c81bf8bc2..75cf91c09 100644 --- a/ui/src/stores/settings.ts +++ b/ui/src/stores/settings.ts @@ -401,7 +401,7 @@ export const useSettingsStore = defineStore("settings", () => { return saveInOrder(dirtyKinds.value, saveDraft); } - async function loadResource(kind: string): Promise { + async function loadResource(kind: string): Promise { const state = stateFor(kind); state.loading = true; try { @@ -412,8 +412,10 @@ export const useSettingsStore = defineStore("settings", () => { ) ).resource, ); + return true; } catch (error) { state.error = describeError(error, errors.common.unknown); + return false; } finally { state.loading = false; } @@ -493,9 +495,12 @@ export const useSettingsStore = defineStore("settings", () => { return true; } catch (error) { if (error instanceof ControlPlaneError && error.status === 409) { - state.conflict = true; - state.error = errors.stores.settings.conflict; - await loadResource(kind); + // Loading the resource clears its last error. Mark the conflict after + // that read so the review can show the latest version before retrying. + if (await loadResource(kind)) { + state.conflict = true; + state.error = errors.stores.settings.conflict; + } } else { state.error = describeError(error, errors.common.unknown); } diff --git a/ui/test/control-plane.test.ts b/ui/test/control-plane.test.ts index 9c0ab8096..ace4b2f7e 100644 --- a/ui/test/control-plane.test.ts +++ b/ui/test/control-plane.test.ts @@ -128,6 +128,62 @@ test("save errors appear on the page only when not presented in the review drawe } }); +// A rejected save must not silently retry against the same stale version. +test("a conflicting settings save keeps the draft for review and retries with the latest version", async (t) => { + for (const [kind, value] of [ + ["workflow-execution-settings", { max_model_tool_steps: 5 }], + ["scheduling-policy", { poll_interval: "1m" }], + ] as const) { + await t.test(kind, async (t) => { + setActivePinia(createPinia()); + const store = useSettingsStore(); + const resourcePath = `/api/control-plane/resources/${kind}`; + const initial = { kind, version: 7, value: {} }; + let resource = initial; + let failRead = false; + const sent: unknown[] = []; + t.mock.method(globalThis, "fetch", async (path: string, init: RequestInit) => { + if (path === "/api/capabilities") return Response.json({ sections: { settings: false } }); + if (path === "/api/control-plane/catalog") { + return Response.json({ resources: [{ kind, commands: ["replace"] }] }); + } + if (path === resourcePath) { + return failRead ? new Response("Could not read the latest version", { status: 503 }) : Response.json({ resource }); + } + assert.equal(path, `${resourcePath}/commands/replace`); + assert.equal(init.method, "POST"); + const command = JSON.parse(String(init.body)); + sent.push(command); + if (command.expected_version !== resource.version) { + return new Response("Changed elsewhere", { status: 409 }); + } + resource = { ...resource, version: resource.version + 1, value: command.value }; + return Response.json({ resource }); + }); + + await store.load(); + store.setDraftValue(kind, value); + resource = { ...resource, version: 8 }; + + failRead = true; + assert.equal((await store.saveDrafts()).failed, kind); + assert.match(store.pageErrorFor(kind) ?? "", /Could not read the latest version/); + assert.deepEqual(store.draftValue(kind), value); + + failRead = false; + assert.equal((await store.saveDrafts()).failed, kind); + assert.match(store.pageErrorFor(kind) ?? "", /Changed elsewhere/); + assert.deepEqual(store.draftValue(kind), value); + assert.deepEqual(sent, [{ value, expected_version: 7 }, { value, expected_version: 7 }], "no automatic overwrite"); + + assert.ok(!(await store.saveDrafts()).failed); + assert.deepEqual(sent.at(-1), { value, expected_version: 8 }); + assert.deepEqual(store.dirtyKinds, []); + assert.equal(store.pageErrorFor(kind), undefined); + }); + } +}); + test("an unset resource opens without changes and an untouched save keeps it unset", async () => { setActivePinia(createPinia()); const store = useSettingsStore();