From f4b7652ae74453aa1a6c23e9a2aae71210bdeab9 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 13 Nov 2026 14:01:35 +1100 Subject: [PATCH] fix(ui): a workflow draft changed elsewhere is never overwritten silently (#1944) --- ui/src/copy/errors/workflows.ts | 2 + ui/src/workflows/WorkflowEditor.vue | 84 ++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/ui/src/copy/errors/workflows.ts b/ui/src/copy/errors/workflows.ts index 432b8f750..ee429e388 100644 --- a/ui/src/copy/errors/workflows.ts +++ b/ui/src/copy/errors/workflows.ts @@ -32,6 +32,8 @@ export const workflows = { editor: { restartFailed: "Restart failed", draftNotSaved: "Draft not saved. Your edit is still in the editor.", + draftChangedElsewhere: "This draft changed in another tab or by Archie. Your edit is still in the editor.", + draftNotRemoved: "Published, but the old draft wasn't removed. Discard it from the menu.", recentRunsNotLoaded: (detail: string) => `Could not load recent runs: ${detail}`, watchedStagesNotLoaded: (detail: string) => `Could not load the watched run's stages: ${detail}`, }, diff --git a/ui/src/workflows/WorkflowEditor.vue b/ui/src/workflows/WorkflowEditor.vue index 85ca5b46f..54259c4f6 100644 --- a/ui/src/workflows/WorkflowEditor.vue +++ b/ui/src/workflows/WorkflowEditor.vue @@ -164,12 +164,19 @@ function renameAt(path: StepPath, name: string): void { yaml.value = replaceStep(yaml.value, path, { id: name || undefined, type, ...rest }); } +// The stored draft of the open workflow as this editor last saw it. A write +// that finds anything else stored would overwrite another tab's or Archie's +// edit, so it stops and says so instead. +const seenDraft = ref(); +const draftConflict = ref(false); function load(value: string): void { const entry = collection.value.definitions.find( (candidate) => candidate.id === value, ); id.value = entry?.id ?? value; - loadedYaml.value = workflows.drafts.find((draft) => draft.id === value)?.yaml ?? entry?.yaml ?? `id: ${value}\nidentity: ${SEEDED_AGENT_ID}\nsteps: []\n`; + seenDraft.value = workflows.drafts.find((draft) => draft.id === value)?.yaml; + draftConflict.value = false; + loadedYaml.value = seenDraft.value ?? entry?.yaml ?? `id: ${value}\nidentity: ${SEEDED_AGENT_ID}\nsteps: []\n`; yaml.value = normalizeStepIDs(loadedYaml.value); open.value = null; localError.value = ""; @@ -189,21 +196,46 @@ watch([collection, () => workflows.drafts], () => { // survives a reload and nothing reaches the running workflow until Publish. // A draft equal to the published definition is removed. let pending: ReturnType | undefined; -async function persistDraft(target: string, value: string, attempt = 0): Promise { +async function writeDraft(target: string, next: string | undefined, force = false): Promise { + for (let attempt = 0; attempt < 5; attempt++) { + const stored = workflows.drafts.find((draft) => draft.id === target)?.yaml; + if (!force && target === selected.value && stored !== seenDraft.value) { + draftConflict.value = true; + return false; + } + if (await workflows.putDraft(target, next)) { + if (target === selected.value) { + seenDraft.value = next; + draftConflict.value = false; + } + return true; + } + if (workflows.draftState.conflict) { + if (target === selected.value) draftConflict.value = true; + return false; + } + if (workflows.draftState.error) return false; + // Another write was in flight; try again once it settles. + await new Promise((resolve) => setTimeout(resolve, 300)); + } + return false; +} +// Every edit lands in the workflow's draft shortly after it is made, so work +// survives a reload and nothing reaches the running workflow until Publish. +// A draft equal to the published definition is removed. +async function persistDraft(target: string, value: string, force = false): Promise { if (!target) return; const published = collection.value.definitions.find((entry) => entry.id === target)?.yaml ?? ""; const current = workflows.drafts.find((draft) => draft.id === target)?.yaml; const next = value === published ? undefined : value; - if (next === current) return; - if (await workflows.putDraft(target, next)) { + if (next === current) { + if (target === selected.value) seenDraft.value = current; + return; + } + if (await writeDraft(target, next, force)) { if (target === selected.value && value === yaml.value) loadedYaml.value = value; - } else if (attempt < 4) { - // replace() clears saving before it resolves, so the failure is retried - // here on a bounded schedule rather than only while a save is in flight. - pending = setTimeout(() => void persistDraft(target, value, attempt + 1), 500); - } else if (target === selected.value) { - // The write failed for good: the edit stays in the editor, and the alert - // says the draft was not saved rather than dropping it silently. + } else if (target === selected.value && !draftConflict.value) { + // The edit stays in the editor; the alert says the draft was not saved. localError.value = workflows.draftState.error || errors.workflows.editor.draftNotSaved; } } @@ -225,10 +257,17 @@ onBeforeUnmount(() => { clearTimeout(pending); void persistDraft(selected.value, yaml.value); }); -async function dropDraft(target: string): Promise { +async function dropDraft(target: string): Promise { clearTimeout(pending); unsaved = undefined; - await workflows.putDraft(target, undefined); + return writeDraft(target, undefined); +} +// The conflict's two ways out: take what is stored, or write this edit over it. +function loadLatestDraft(): void { + load(selected.value); +} +function keepMyDraft(): void { + void persistDraft(selected.value, yaml.value, true); } // Edits made while Archie works would be overwritten by its draft, so the // canvas and YAML go read-only until the turn ends; viewing stays open. @@ -251,8 +290,8 @@ async function takeProposal(): Promise { } // Back to the published definition: the draft is dropped, not merged. async function restorePublished(): Promise { - await dropDraft(selected.value); - load(selected.value); + if (await dropDraft(selected.value)) load(selected.value); + else if (!draftConflict.value) localError.value = workflows.draftState.error || errors.workflows.editor.draftNotSaved; } async function save(next: WorkflowDefinitionCollection): Promise { @@ -278,10 +317,10 @@ async function publish(): Promise { if ( await save(upsertWorkflowDefinition(next, { id: nextID, yaml: yaml.value })) ) { - await dropDraft(previous); - if (previous !== nextID) await dropDraft(nextID); + const dropped = (await dropDraft(previous)) && (previous === nextID || (await dropDraft(nextID))); selected.value = nextID; loadedYaml.value = yaml.value; + if (!dropped && !draftConflict.value) localError.value = errors.workflows.editor.draftNotRemoved; } } @@ -585,7 +624,16 @@ onBeforeUnmount(() => yamlObserver?.disconnect()); - + + + {{ errors.workflows.editor.draftChangedElsewhere }} + + + + + + + {{ localError || workflows.draftState.error || state.error }}