fix(ui): a workflow draft changed elsewhere is never overwritten silently (#1944)

This commit is contained in:
sam committed 2026-11-13 14:01:35 +11:00
1 parent 1def83a43f
commit f4b7652ae7
2 files changed
+68 -18

No files matched your search

+2
View File
@@ -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}`,
},
+66 -18
View File
@@ -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<string | undefined>();
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<typeof setTimeout> | undefined;
async function persistDraft(target: string, value: string, attempt = 0): Promise<void> {
async function writeDraft(target: string, next: string | undefined, force = false): Promise<boolean> {
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<void> {
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<void> {
async function dropDraft(target: string): Promise<boolean> {
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<void> {
}
// Back to the published definition: the draft is dropped, not merged.
async function restorePublished(): Promise<void> {
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<boolean> {
@@ -278,10 +317,10 @@ async function publish(): Promise<void> {
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());
</div>
</TabsContent>
</Tabs>
<Alert v-if="localError || workflows.draftState.error || state.error" variant="destructive"><AlertDescription>
<Alert v-if="draftConflict" variant="destructive">
<AlertDescription class="flex flex-wrap items-center justify-between gap-2">
{{ errors.workflows.editor.draftChangedElsewhere }}
<span class="flex gap-2">
<Button type="button" variant="outline" size="sm" @click="loadLatestDraft">Load latest</Button>
<Button type="button" variant="outline" size="sm" @click="keepMyDraft">Replace with mine</Button>
</span>
</AlertDescription>
</Alert>
<Alert v-else-if="localError || workflows.draftState.error || state.error" variant="destructive"><AlertDescription>
{{ localError || workflows.draftState.error || state.error }}
</AlertDescription></Alert>
<Dialog :open="draftOpen && changes.length > 0" @update:open="(open) => (draftOpen = open)">