From fbbf5ee67e86e41e10536e5088baaf886330a60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 14:14:14 +0000 Subject: [PATCH] fix(studio-server): an agent step waits for history's first copies, so undo brings old files back (#8375) --- .../src/history/oldBytesKept.test.ts | 63 +++++++++++++++++++ .../src/history/projectHistory.test.ts | 10 +-- .../src/history/projectHistory.ts | 12 +++- 3 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 packages/studio-server/src/history/oldBytesKept.test.ts diff --git a/packages/studio-server/src/history/oldBytesKept.test.ts b/packages/studio-server/src/history/oldBytesKept.test.ts new file mode 100644 index 0000000000..6caec867c9 --- /dev/null +++ b/packages/studio-server/src/history/oldBytesKept.test.ts @@ -0,0 +1,63 @@ +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { openProjectHistory, type ProjectHistory } from "./projectHistory"; +import type { HistoryWho } from "./historyLog"; + +const agent: HistoryWho = { kind: "agent", name: "Framey" }; +const MiB = 1024 ** 2; +const cleanup: Array<() => unknown> = []; +afterEach(async () => { + for (const step of cleanup.splice(0).reverse()) await step(); +}); + +function tempDir(prefix: string): string { + const dir = mkdtempSync(join(tmpdir(), prefix)); + cleanup.push(() => rmSync(dir, { recursive: true, force: true })); + return dir; +} + +/** A project whose `name` was already on disk, `size` bytes of 1s, when its history first opened. */ +async function projectWith(name: string, size: number) { + const projectDir = tempDir("hf-old-bytes-project-"); + const historyRoot = tempDir("hf-old-bytes-root-"); + writeFileSync(join(projectDir, "index.html"), "A"); + const file = join(projectDir, name); + const old = Buffer.alloc(size, 1); + writeFileSync(file, old); + const history = await openProjectHistory({ projectDir, historyRoot, quietMs: 30 }); + cleanup.push(() => history.close()); + return { history, file, old }; +} + +/** An agent step that replaces `file` right after it begins, as Desktop's watcher reports it. */ +async function replaced(history: ProjectHistory, file: string, size: number) { + const window = await history.beginWindow(agent, "Replace it"); + writeFileSync(file, Buffer.alloc(size, 2)); + history.noteChange(file); + return window.close(); +} + +// fails without the fix: the step began before the file's first copy landed, so the replace was never recorded +// and its old bytes were lost. +describe("an agent step that replaces a file the project had when its history opened", () => { + it("brings back a 40 MB image replaced before its first copy would have landed", async () => { + const { history, file, old } = await projectWith("cover.png", 40 * MiB); + const entry = await replaced(history, file, 40 * MiB); + expect(entry).toMatchObject({ label: "Replace it", who: agent }); + expect(await history.undo(entry!.id, { who: agent, mode: "back-to-before" })).toMatchObject({ + ok: true, + }); + expect(readFileSync(file).equals(old)).toBe(true); + }); + + it("records a replaced video as the agent's change, whose old version is not kept", async () => { + const { history, file } = await projectWith("talk.mp4", 2 * MiB); + const entry = await replaced(history, file, 2 * MiB); + expect(entry).toMatchObject({ label: "Replace it", who: agent }); + await expect(history.undo(entry!.id, { who: agent, mode: "back-to-before" })).rejects.toThrow( + "An earlier version of talk.mp4 is not kept", + ); + }); +}); diff --git a/packages/studio-server/src/history/projectHistory.test.ts b/packages/studio-server/src/history/projectHistory.test.ts index 7c7b6b130e..49c07df0b2 100644 --- a/packages/studio-server/src/history/projectHistory.test.ts +++ b/packages/studio-server/src/history/projectHistory.test.ts @@ -357,11 +357,12 @@ describe("openProjectHistory", () => { { "index.html": "A", "clip.bin": Buffer.alloc(2 * 1024 ** 2, 7) }, { budgetBytes: 1, quietMs: 30 }, ); - // The edit's prune takes the first copy; storing it again then fails. + // Studio's edit, whose prune takes the first copy; storing it again then fails. A step would wait for the copy. mediaCopy.fails = (copy) => copy === 2; mediaCopy.stored = () => { mediaCopy.stored = null; - return change(history, you, "Color", () => write("index.html", "B")); + write("index.html", "B"); + return history.claim(you, "Color", ["index.html"]); }; await vi.waitFor(() => expect(mediaCopy.copies).toHaveLength(2), { timeout: 10_000 }); await history.flush(); @@ -435,10 +436,11 @@ describe("openProjectHistory", () => { { "index.html": "A", "clip.bin": clip }, { budgetBytes: 1, quietMs: 30 }, ); - // The copy is stored; an edit commits and prunes before history records the copy. + // The copy is stored; Studio's edit commits and prunes before history records it. A step would wait for it. mediaCopy.stored = () => { mediaCopy.stored = null; - return change(history, you, "Color", () => write("index.html", "B")); + write("index.html", "B"); + return history.claim(you, "Color", ["index.html"]); }; release(); await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.bin"]), { diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts index d1da23211b..deddca889d 100644 --- a/packages/studio-server/src/history/projectHistory.ts +++ b/packages/studio-server/src/history/projectHistory.ts @@ -729,6 +729,10 @@ class Engine { ); } + async untilFirstCopiesLandBeforeAnyWrite(): Promise { + await Promise.all([...this.adopting.values()].map((adoption) => adoption.firstTry)); + } + adoptMediaInBackground(files: ListedFile[], sweptAt: number): void { let turn: Promise = Promise.resolve(); for (const file of files) { @@ -1639,13 +1643,15 @@ class Engine { api(): ProjectHistory { return { projectId: this.projectId, - beginWindow: (who, label, options = {}) => - this.beginWindow( + beginWindow: async (who, label, options = {}) => { + await this.untilFirstCopiesLandBeforeAnyWrite(); + return this.beginWindow( who, label, options.idleMs ?? this.options.maxGroupMs ?? 30_000, options.exclusive === true, - ), + ); + }, unsavedBlobBytes: () => this.unsavedBlobBytes(), claim: async (who, label, paths, options = {}) => { await Promise.all(paths.map((path) => this.adopting.get(this.logPath(path))?.retryNow()));