fix(studio-server): an agent step waits for history's first copies, so undo brings old files back (#8375)

This commit is contained in:
Miguel Ángel
2026-11-21 14:14:14 +00:00
committed by GitHub
parent d5881e8aa6
commit fbbf5ee67e
3 changed files with 78 additions and 7 deletions
@@ -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",
);
});
});
@@ -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"]), {
@@ -729,6 +729,10 @@ class Engine {
);
}
async untilFirstCopiesLandBeforeAnyWrite(): Promise<void> {
await Promise.all([...this.adopting.values()].map((adoption) => adoption.firstTry));
}
adoptMediaInBackground(files: ListedFile[], sweptAt: number): void {
let turn: Promise<unknown> = 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()));