From 773aedef46403607c4d59639eb10a7ec790bd5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 23 Nov 2026 08:16:40 +0000 Subject: [PATCH] fix(studio-server): a save made while an import is taken in keeps its own undo step (#8561) --- .../src/history/projectHistory.test.ts | 47 +++++++++++++++++++ .../src/history/projectHistory.ts | 21 ++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/studio-server/src/history/projectHistory.test.ts b/packages/studio-server/src/history/projectHistory.test.ts index 1aa2ca75d8..cc94d4bbf1 100644 --- a/packages/studio-server/src/history/projectHistory.test.ts +++ b/packages/studio-server/src/history/projectHistory.test.ts @@ -1971,6 +1971,53 @@ describe("openProjectHistory", () => { }); describe("claim: a writer that records after writing", () => { + /** A save the history has seen but not yet logged: the change is pending outside when the next claim runs. */ + async function pendingOutside(history: Awaited>, path: string) { + history.noteChange(path); + await vi.waitFor(() => + expect(history.next("back", you)?.files.map((file) => file.path)).toContain(path), + ); + } + const backdate = (file: string) => + utimesSync(file, new Date(Date.now() - 60_000), new Date(Date.now() - 60_000)); + + // fails without the fix: the import's claim logged the save as a change from outside, the save's own claim then + // found nothing, and Cmd+Z took back the import instead of the edit. + it("a save made while an import is still taken in keeps its own step: Cmd+Z undoes the edit, the media stays", async () => { + const { history, write, read, has, projectDir } = await project( + { "index.html": "

Hello

" }, + { quietMs: 60_000 }, + ); + write("assets/logo.png", "png"); + backdate(join(projectDir, "assets/logo.png")); + write("index.html", "

Two

"); + await pendingOutside(history, "index.html"); + await history.claim(you, "Imported media", ["assets/logo.png"]); + await history.claim(you, "Edited", ["index.html"]); + expect(history.list().map((entry) => entry.label)).toEqual(["Imported media", "Edited"]); + expect(await history.step("back", you)).toMatchObject({ ok: true }); + expect(read("index.html")).toBe("

Hello

"); + expect(has("assets/logo.png")).toBe(true); + }); + + it("a change made before a claim's own files is still logged ahead of the claim", async () => { + const { history, write, projectDir } = await project( + { "index.html": "v1" }, + { quietMs: 60_000 }, + ); + write("style.css", "h1 {}"); + backdate(join(projectDir, "style.css")); + await pendingOutside(history, "style.css"); + write("index.html", "v2"); + await history.claim(you, "Edited", ["index.html"]); + expect( + history.list().map((entry) => [entry.who.kind, entry.files.map((file) => file.path)]), + ).toEqual([ + ["outside", ["style.css"]], + ["person", ["index.html"]], + ]); + }); + it("files the claimed paths' writes as the claimer's entry; other outside writes stay outside, logged first", async () => { const { history, write, read } = await project({ "index.html": "A", "notes.md": "n" }); write("index.html", "B"); diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts index 6e402dcb8f..8bee733bca 100644 --- a/packages/studio-server/src/history/projectHistory.ts +++ b/packages/studio-server/src/history/projectHistory.ts @@ -1256,7 +1256,7 @@ class Engine { if (!taken.length) return this.claimNothing(coalesceKey); const held = this.heldFor(coalesceKey, taken); if (!held) await this.commitClaim(); - await this.commitPending(); + await this.commitPendingWrittenBefore(taken); const group = held ?? this.newGroup(who, label); for (const change of taken) addChange(group, change.path, change.before, change.after); this.markWentAhead(pending, group); @@ -1291,6 +1291,25 @@ class Engine { return null; } + async commitPendingWrittenBefore(taken: readonly HistoryFileChange[]): Promise { + const writtenAt = Math.max( + ...(await Promise.all(taken.map((change) => this.mtimeOf(change.path)))), + ); + const later: HistoryFileChange[] = []; + if (Number.isFinite(writtenAt)) + for (const change of [...(this.outside?.changes.values() ?? [])]) + if ((await this.mtimeOf(change.path)) > writtenAt) later.push(change); + for (const change of later) this.outside!.changes.delete(change.path); + await this.commitPending(); + if (!later.length) return; + const outside = this.outsideGroup(); + for (const change of later) outside.changes.set(change.path, change); + } + + async mtimeOf(path: string): Promise { + return (await stat(join(this.dir, path)).catch(() => null))?.mtimeMs ?? -Infinity; + } + /** Commits every other writer's pending changes, oldest first; open windows stay open. */ async commitPending(): Promise { await this.commitOutside();