fix(ui): let settings saves recover from version conflicts

Keep the conflict visible after reloading the resource so the draft rebases onto the latest version. Require another Save to submit it, and keep the draft against its original version if the refresh fails.

(cherry picked from commit 0d4b63c906363c4343e8f8a8f4163c96b0c3378f)
This commit is contained in:
sam committed 2026-11-27 15:51:34 +11:00
1 parent 90e58a67e9
commit a2141ed4bb
2 files changed
+65 -4

No files matched your search

+9 -4
View File
@@ -401,7 +401,7 @@ export const useSettingsStore = defineStore("settings", () => {
return saveInOrder(dirtyKinds.value, saveDraft);
}
async function loadResource(kind: string): Promise<void> {
async function loadResource(kind: string): Promise<boolean> {
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);
}
+56
View File
@@ -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();