Skip to content

Write HTML. Render video. Built for agents.

fix(studio): undo takes back the first edit of a film whose media is still being copied (#5043)

Miguel Ángelc6478e404c66
diff --git a/packages/cli/src/server/studioServer.test.ts b/packages/cli/src/server/studioServer.test.ts
index 3af11315fe..538b3203f4 100644
--- a/packages/cli/src/server/studioServer.test.ts
+++ b/packages/cli/src/server/studioServer.test.ts
@@ -185,6 +185,44 @@ describe("createStudioServer project history (D-491)", () => {
},
);
+ it("never makes a request wait on another process's history lock again once it was busy", async () => {
+ const waits: unknown[] = [];
+ historyState.open = async (options) => {
+ waits.push((options as { ownerWaitMs?: number }).ownerWaitMs);
+ throw new HistoryBusyError(1);
+ };
+ const projectDir = tmpProject();
+ server = createStudioServer({ projectDir, historyRoot: tmpProject() });
+ const historyUrl = `/api/projects/${encodeURIComponent(basename(projectDir))}/history`;
+ await server.app.request(historyUrl);
+ await server.app.request(historyUrl);
+ historyState.open = null;
+ expect(waits).toEqual([undefined, 0]);
+ await server.shutdown();
+ });
+
+ it("waits for the history lock again once a history opened after a busy one", async () => {
+ const waits: unknown[] = [];
+ const reopenable = { replacedAtPath: () => true, close: async () => {} };
+ let opens = 0;
+ historyState.open = async (options) => {
+ waits.push((options as { ownerWaitMs?: number }).ownerWaitMs);
+ opens += 1;
+ if (opens === 1) throw new HistoryBusyError(1);
+ if (opens === 2) return reopenable;
+ historyState.open = null;
+ throw new HistoryBusyError(1);
+ };
+ const projectDir = tmpProject();
+ server = createStudioServer({ projectDir, historyRoot: tmpProject() });
+ const historyUrl = `/api/projects/${encodeURIComponent(basename(projectDir))}/history`;
+ await server.app.request(historyUrl);
+ await server.app.request(historyUrl);
+ historyState.open = null;
+ expect(waits).toEqual([undefined, 0, undefined]);
+ await server.shutdown();
+ });
+
it("opens a new project's own history once it takes the folder's path", async () => {
const projectDir = tmpProject();
writeFileSync(join(projectDir, "index.html"), "<html>before</html>");
diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts
index 0c65c6b41b..54a3ef1151 100644
--- a/packages/cli/src/server/studioServer.ts
+++ b/packages/cli/src/server/studioServer.ts
@@ -436,17 +436,25 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
};
// Opened on first use, so a server that never serves Studio's history never writes one. A failed open stays off
- // for this run; one another process was holding is tried again on the next request.
+ // for this run; one another process was holding is tried again on the next request, which no write waits for.
+ let ownerWaitMs: number | undefined;
const histories = historyCache(() =>
openProjectHistory({
projectDir,
historyRoot: options.historyRoot ?? DEFAULT_HISTORY_ROOT,
- }).catch((error: unknown) => {
- console.warn(`[studio] Project history is off: ${String(error)}`);
- if (error instanceof HistoryBusyError || error instanceof HistoryClosedError)
- histories.forget(projectDir);
- return null;
- }),
+ ownerWaitMs,
+ })
+ .then((history) => {
+ ownerWaitMs = undefined;
+ return history;
+ })
+ .catch((error: unknown) => {
+ console.warn(`[studio] Project history is off: ${String(error)}`);
+ if (error instanceof HistoryBusyError) ownerWaitMs = 0;
+ if (error instanceof HistoryBusyError || error instanceof HistoryClosedError)
+ histories.forget(projectDir);
+ return null;
+ }),
);
const projectHistory = () => histories.get(projectDir);
watcher.addListener((changedPath) => {
diff --git a/packages/studio-server/src/createStudioApi.ts b/packages/studio-server/src/createStudioApi.ts
index 3c1e3cde3e..3ca2db4d9a 100644
--- a/packages/studio-server/src/createStudioApi.ts
+++ b/packages/studio-server/src/createStudioApi.ts
@@ -53,6 +53,15 @@ export function createStudioApi(adapter: StudioApiAdapter): Hono {
.catch(() => null);
if (project) adapter.invalidateProjectSignature?.(project.dir);
});
+ api.use("/projects/:id/*", async function openHistorySoNoWriteBecomesItsBaseline(c, next) {
+ if (c.req.method !== "GET" && c.req.method !== "HEAD" && adapter.history) {
+ await Promise.resolve()
+ .then(() => adapter.resolveProject(c.req.param("id")))
+ .then((project) => project && adapter.history?.(project))
+ .catch(() => null);
+ }
+ await next();
+ });
registerProjectRoutes(api, adapter);
registerFileRoutes(api, adapter);
diff --git a/packages/studio-server/src/history/historyLog.ts b/packages/studio-server/src/history/historyLog.ts
index ff78e737c0..ee5c3bf996 100644
--- a/packages/studio-server/src/history/historyLog.ts
+++ b/packages/studio-server/src/history/historyLog.ts
@@ -100,13 +100,17 @@ export function saveRecord(file: string, log: HistoryLog, record: LogRecord): vo
}
}
+export function baselineRecord(log: HistoryLog): LogRecord {
+ return {
+ type: "baseline",
+ files: Object.fromEntries(log.baseline),
+ ...(log.keepsLedger && { keepsLedger: true }),
+ };
+}
+
export function writeLog(file: string, log: HistoryLog): void {
const records: LogRecord[] = [
- {
- type: "baseline",
- files: Object.fromEntries(log.baseline),
- ...(log.keepsLedger && { keepsLedger: true }),
- },
+ baselineRecord(log),
...log.entries.map((entry) => ({ type: "entry" as const, entry })),
...[...log.pins].map((id) => ({ type: "pin" as const, id, pinned: true })),
];
diff --git a/packages/studio-server/src/history/projectHistory.test.ts b/packages/studio-server/src/history/projectHistory.test.ts
index ca5a839ed5..9f2eb29593 100644
--- a/packages/studio-server/src/history/projectHistory.test.ts
+++ b/packages/studio-server/src/history/projectHistory.test.ts
@@ -24,6 +24,36 @@ import { HistoryIdError } from "./historyId";
import { HistoryClosedError, openProjectHistory, type ProjectHistory } from "./projectHistory";
import { START, type HistoryWho } from "./historyLog";
+// A test can hold a media copy, and run work after its blob is stored but before history records it.
+const mediaCopy = vi.hoisted(() => ({
+ held: null as Promise<void> | null,
+ stored: null as (() => Promise<unknown>) | null,
+ copies: [] as string[],
+ fails: null as ((copy: number) => boolean) | null,
+}));
+vi.mock("./blobStore", async (importOriginal) => {
+ const real = await importOriginal<typeof import("./blobStore")>();
+ return {
+ ...real,
+ openBlobStore: async (dir: string) => {
+ const store = await real.openBlobStore(dir);
+ return {
+ ...store,
+ put: async (path: string) => {
+ if (path.endsWith(".mp4")) {
+ await mediaCopy.held;
+ mediaCopy.copies.push(basename(path));
+ if (mediaCopy.fails?.(mediaCopy.copies.length)) throw new Error("The disk is full.");
+ }
+ const hash = await store.put(path);
+ if (path.endsWith(".mp4")) await mediaCopy.stored?.();
+ return hash;
+ },
+ };
+ },
+ };
+});
+
const you: HistoryWho = { kind: "person", name: "You" };
const pause = (ms: number) => new Promise((settle) => setTimeout(settle, ms));
const agent: HistoryWho = { kind: "agent", name: "Agent" };
@@ -31,6 +61,7 @@ const cleanup: Array<() => unknown> = [];
afterEach(async () => {
for (const step of cleanup.splice(0).reverse()) await step();
+ Object.assign(mediaCopy, { held: null, stored: null, copies: [], fails: null });
});
const inside = (dir: string, path: string) => readFileSync(join(dir, path), "utf-8");
@@ -265,6 +296,138 @@ describe("openProjectHistory", () => {
expect([read("index.html"), has(".media/manifest.jsonl")]).toEqual(["A", false]);
});
+ it("a ledger made while closed is still a change after the project's media was copied in the background", async () => {
+ const { history, write, has, projectDir, historyRoot } = await project(
+ { "index.html": "A", "clip.mp4": Buffer.alloc(2 * 1024 ** 2, 7) },
+ { quietMs: 30 },
+ );
+ await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
+ timeout: 10_000,
+ });
+ await history.close();
+ write("b.png", "png");
+ write(".media/manifest.jsonl", '{"path":"b.png"}\n');
+ const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
+ await reopened.flush();
+ const [outside] = reopened.list();
+ expect(outside!.files.map((file) => file.path).sort()).toEqual([
+ ".media/manifest.jsonl",
+ "b.png",
+ ]);
+ expect((await reopened.undo(outside!.id, { who: you })).ok).toBe(true);
+ expect([has("b.png"), has(".media/manifest.jsonl")]).toEqual([false, false]);
+ });
+
+ it("names media in adopting.json until its copy is recorded, so an exit mid-copy resumes it", async () => {
+ let release = () => {};
+ mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
+ const { history, historyRoot } = await project({
+ "index.html": "A",
+ "clip.mp4": Buffer.alloc(2 * 1024 ** 2, 7),
+ });
+ const adopting = join(historyRoot, history.projectId, "adopting.json");
+ expect(JSON.parse(readFileSync(adopting, "utf-8"))).toEqual(["clip.mp4"]);
+ release();
+ await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
+ timeout: 10_000,
+ });
+ expect(existsSync(adopting)).toBe(false);
+ });
+
+ it("keeps media whose recording failed for the next open, rather than log it as added", async () => {
+ const { history, write, historyRoot } = await project(
+ { "index.html": "A", "clip.mp4": Buffer.alloc(2 * 1024 ** 2, 7) },
+ { budgetBytes: 1, quietMs: 30 },
+ );
+ // The edit's prune takes the first copy; storing it again then fails.
+ mediaCopy.fails = (copy) => copy === 2;
+ mediaCopy.stored = () => {
+ mediaCopy.stored = null;
+ return change(history, you, "Color", () => write("index.html", "B"));
+ };
+ await vi.waitFor(() => expect(mediaCopy.copies).toHaveLength(2), { timeout: 10_000 });
+ await history.flush();
+ expect(history.list().flatMap((entry) => entry.files.map((file) => file.path))).not.toContain(
+ "clip.mp4",
+ );
+ const adopting = join(historyRoot, history.projectId, "adopting.json");
+ expect(JSON.parse(readFileSync(adopting, "utf-8"))).toEqual(["clip.mp4"]);
+ });
+
+ it("never copies the media of a folder that replaced the project", async () => {
+ let release = () => {};
+ mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
+ const media = {
+ "a.mp4": Buffer.alloc(2 * 1024 ** 2, 1),
+ "b.mp4": Buffer.alloc(2 * 1024 ** 2, 2),
+ };
+ const { history, write, projectDir } = await project({ "index.html": "A", ...media });
+ renameSync(projectDir, `${projectDir}-moved`);
+ cleanup.push(() => rmSync(`${projectDir}-moved`, { recursive: true, force: true }));
+ mkdirSync(projectDir);
+ for (const [path, bytes] of Object.entries(media)) write(path, bytes);
+ release();
+ await history.close();
+ // Only the copy already running when the folder changed may finish.
+ expect(mediaCopy.copies).toEqual(["a.mp4"]);
+ });
+
+ it("undoes a clip replaced while closed, though the history closed before its copy finished", async () => {
+ const clip = Buffer.alloc(2 * 1024 ** 2, 7);
+ let release = () => {};
+ mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
+ const { history, write, projectDir, historyRoot } = await project(
+ { "index.html": "A", "clip.mp4": clip },
+ { quietMs: 30 },
+ );
+ const closed = history.close();
+ release();
+ await closed;
+ write("clip.mp4", Buffer.alloc(2 * 1024 ** 2, 9));
+ const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
+ await reopened.flush();
+ expect(reopened.list().map((entry) => entry.files.map((file) => file.path))).toEqual([
+ ["clip.mp4"],
+ ]);
+ expect(await reopened.step("back", you)).toMatchObject({ ok: true });
+ expect(readFileSync(join(projectDir, "clip.mp4")).equals(clip)).toBe(true);
+ });
+
+ it("adopts media whose copy a process exit cut short, rather than log it as added", async () => {
+ const { history, write, projectDir, historyRoot } = await project({ "index.html": "A" });
+ await history.close();
+ // What an exit mid-copy leaves: the clip on disk, named as still being copied, not in the baseline.
+ write("clip.mp4", Buffer.alloc(2 * 1024 ** 2, 7));
+ writeFileSync(join(historyRoot, history.projectId, "adopting.json"), '["clip.mp4"]');
+ const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
+ await vi.waitFor(() => expect(reopened.peek(START)).toHaveProperty(["clip.mp4"]), {
+ timeout: 10_000,
+ });
+ await reopened.flush();
+ expect(reopened.list()).toEqual([]);
+ });
+
+ it("keeps a media copy that a budget prune ran into before history recorded it", async () => {
+ const clip = Buffer.alloc(2 * 1024 ** 2, 7);
+ let release = () => {};
+ mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
+ const { history, write } = await project(
+ { "index.html": "A", "clip.mp4": clip },
+ { budgetBytes: 1, quietMs: 30 },
+ );
+ // The copy is stored; an edit commits and prunes before history records the copy.
+ mediaCopy.stored = () => {
+ mediaCopy.stored = null;
+ return change(history, you, "Color", () => write("index.html", "B"));
+ };
+ release();
+ await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
+ timeout: 10_000,
+ });
+ const start = history.peek(START)["clip.mp4"]!;
+ expect((await history.readBlob(start)).equals(clip)).toBe(true);
+ });
+
it("a log 0.8.123 wrote without a ledger takes none in, and one made later while closed is a change", async () => {
const { history, write, projectDir, historyRoot } = await project(
{ "index.html": "A" },
diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts
index 0ba9409a6b..c894c7510e 100644
--- a/packages/studio-server/src/history/projectHistory.ts
+++ b/packages/studio-server/src/history/projectHistory.ts
@@ -43,6 +43,7 @@ import {
import { takeHistoryOwnership } from "./ownerLock.js";
import {
START,
+ baselineRecord,
foldOldest,
manifestAround,
manifestAt,
@@ -221,6 +222,8 @@ function withoutHiddenPaths(log: HistoryLog): HistoryLog {
const historyFiles = (dir: string) =>
listProjectFiles(dir).filter((file) => isHistoryPath(file.path));
+type ListedFile = ReturnType<typeof listProjectFiles>[number];
+const COPY_LATER_ABOVE_BYTES = 1024 ** 2;
const sameWho = (a: HistoryWho, b: HistoryWho) => a.kind === b.kind && a.name === b.name;
@@ -319,6 +322,7 @@ class Engine {
outside: Group | null = null;
/** A coalescing claim, open until another key, its idle timer, an operation, a window, or another write. */
claimed: { group: Group; key: string; timer?: NodeJS.Timeout } | null = null;
+ adopting = new Map<string, Promise<void>>();
/** Per path and hash an API write left, the hash of the bytes it replaced, until walked or written past. */
overwritten = new Map<string, Map<string, string>>();
stopHearing: (() => void) | undefined;
@@ -413,6 +417,10 @@ class Engine {
const cached = cache.get(path);
this.tracked.set(path, { hash, stat: cached?.hash === hash ? cached.stat : "" });
}
+ const left = this.readAdopting();
+ const unadopted = (file: { path: string }) =>
+ left.has(file.path) && !this.tracked.has(file.path);
+ this.adoptMediaInBackground(historyFiles(this.dir).filter(unadopted), this.now());
// What changed while the project was closed is one outside entry, or the closed window's.
this.reopenClosedWindow();
await this.settleAll();
@@ -481,7 +489,8 @@ class Engine {
async firstOpen(): Promise<void> {
const sweptAt = this.now();
- for (const file of historyFiles(this.dir)) {
+ const files = historyFiles(this.dir);
+ for (const file of files.filter((file) => file.size <= COPY_LATER_ABOVE_BYTES)) {
const hash = await this.storeIfPresent(file.path);
if (this.whereFolder() !== "here") throw this.replaced();
if (hash !== null) this.tracked.set(file.path, { hash, stat: statKey(file, sweptAt) });
@@ -490,6 +499,56 @@ class Engine {
this.log.keepsLedger = true;
this.persistLog();
this.saveStatCache();
+ this.adoptMediaInBackground(
+ files.filter((file) => file.size > COPY_LATER_ABOVE_BYTES),
+ sweptAt,
+ );
+ }
+
+ adoptMediaInBackground(files: ListedFile[], sweptAt: number): void {
+ let copied = Promise.resolve();
+ for (const file of files) {
+ copied = copied.then(() => this.adopt(file, sweptAt));
+ this.adopting.set(file.path, copied);
+ }
+ this.saveAdopting();
+ }
+
+ saveAdopting(): void {
+ const file = join(this.home, "adopting.json");
+ if (!this.adopting.size) return rmSync(file, { force: true });
+ mkdirSync(this.home, { recursive: true });
+ replaceFileAtomically(file, JSON.stringify([...this.adopting.keys()]), 0o644);
+ }
+
+ async adopt(file: ListedFile, sweptAt: number) {
+ try {
+ if (this.whereFolder() !== "here") return;
+ const stored = await this.storeIfPresent(file.path);
+ await this.queue(async () => {
+ // Still named for the next open until recorded, so a failed record is never logged as added.
+ await this.recordAdopted(file, sweptAt, stored);
+ this.adopting.delete(file.path);
+ this.saveAdopting();
+ });
+ } catch (error) {
+ if (!(error instanceof HistoryClosedError)) this.options.onError?.(error);
+ }
+ }
+
+ async recordAdopted(file: ListedFile, sweptAt: number, stored: string | null) {
+ if (stored === null || this.whereFolder() !== "here") return;
+ // A prune that ran while this copy waited for the queue may have taken its bytes.
+ const hash = this.blobs.has(stored) ? stored : await this.storeIfPresent(file.path);
+ if (hash === null) return;
+ const change = { path: file.path, before: null, after: hash };
+ const found = this.overwrittenBy(change, undefined, new Set());
+ const start = found && this.blobs.has(found) ? found : hash;
+ this.tracked.set(file.path, { hash, stat: statKey(file, sweptAt) });
+ this.log.baseline.set(file.path, start);
+ if (start !== hash) addChange(this.outsideGroup(), file.path, start, hash);
+ this.persistLog(baselineRecord(this.log));
+ this.saveStatCache();
}
manifest(): Manifest {
@@ -505,6 +564,16 @@ class Engine {
}
}
+ readAdopting(): Set<string> {
+ try {
+ return new Set(
+ JSON.parse(readFileSync(join(this.home, "adopting.json"), "utf-8")) as string[],
+ );
+ } catch {
+ return new Set();
+ }
+ }
+
saveStatCache(): void {
const files = JSON.stringify(Object.fromEntries(this.tracked));
mkdirSync(this.home, { recursive: true });
@@ -521,7 +590,7 @@ class Engine {
const sweptAt = this.now();
const changedAt = (file: { mtimeMs: number; ctimeMs: number }) =>
Math.min(sweptAt, Math.max(file.mtimeMs, file.ctimeMs));
- const seen = historyFiles(this.dir);
+ const seen = historyFiles(this.dir).filter((file) => !this.adopting.has(file.path));
const heard = new Map(this.overwritten);
const present = new Set(seen.map((file) => file.path));
const removed = [...this.tracked.keys()]
@@ -1092,8 +1161,10 @@ class Engine {
projectId: this.projectId,
beginWindow: (who, label, options = {}) =>
this.beginWindow(who, label, options.idleMs ?? this.options.maxGroupMs ?? 30_000),
- claim: (who, label, paths, options = {}) =>
- this.queue(() => this.claimNow(who, label, paths, options)),
+ claim: async (who, label, paths, options = {}) => {
+ await Promise.all(paths.map((path) => this.adopting.get(this.logPath(path))));
+ return this.queue(() => this.claimNow(who, label, paths, options));
+ },
noteChange: (path) => {
if (!this.closed) this.noteChange(path);
},
@@ -1163,6 +1234,8 @@ class Engine {
replacedAtPath: () => this.whereFolder() === "replaced",
close: () =>
(this.closing ??= (async () => {
+ // A copy cut short would leave its media out of the baseline, so a change made while closed is lost.
+ await Promise.all(this.adopting.values());
if (this.notedTimer) clearTimeout(this.notedTimer);
this.stopHearing?.();
const settled = this.queue(() => this.settleAll());
diff --git a/packages/studio-server/src/routes/history.test.ts b/packages/studio-server/src/routes/history.test.ts
index ba55a1cafb..44f2390086 100644
--- a/packages/studio-server/src/routes/history.test.ts
+++ b/packages/studio-server/src/routes/history.test.ts
@@ -7,12 +7,34 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { createStudioApi } from "../createStudioApi";
import { DELETED_VERSION, fileContentVersion, identifyFileWrite } from "../helpers/fileVersion";
import { openProjectHistory, type ProjectHistory } from "../history/projectHistory";
+import { START } from "../history/historyLog";
+import { historyCache } from "../history/historyCache";
import { createProjectSignature, resolveProjectSignature } from "../helpers/projectSignature";
import type { StudioApiAdapter } from "../types";
+// Media copies wait on `held`, so a test can keep the first open's media copy running.
+const mediaCopy = vi.hoisted(() => ({ held: null as Promise<void> | null }));
+vi.mock("../history/blobStore", async (importOriginal) => {
+ const real = await importOriginal<typeof import("../history/blobStore")>();
+ return {
+ ...real,
+ openBlobStore: async (dir: string) => {
+ const store = await real.openBlobStore(dir);
+ return {
+ ...store,
+ put: async (path: string) => {
+ if (path.endsWith(".mp4")) await mediaCopy.held;
+ return store.put(path);
+ },
+ };
+ },
+ };
+});
+
const cleanup: Array<() => unknown> = [];
afterEach(async () => {
+ mediaCopy.held = null;
for (const step of cleanup.splice(0).reverse()) await step();
});
@@ -36,6 +58,39 @@ function apiFor(projectDir: string, history?: ProjectHistory) {
);
}
+/** A project whose index.html reads "A", its history opened on first use as both hosts do. */
+function lazyHistoryProject() {
+ const projectDir = tempDir("hf-history-first-write-");
+ const file = join(projectDir, "index.html");
+ writeFileSync(file, "A");
+ const historyRoot = tempDir("hf-history-first-write-root-");
+ const histories = historyCache((dir) => openProjectHistory({ projectDir: dir, historyRoot }));
+ cleanup.push(() => histories.closeAll());
+ const api = createStudioApi({
+ listProjects: () => [],
+ resolveProject: (id: string) => (id === "demo" ? { id, dir: projectDir } : null),
+ history: (project: { dir: string }) => histories.get(project.dir),
+ } as unknown as StudioApiAdapter);
+ const post = (path: string, body: object) =>
+ api.request(`/projects/demo/history${path}`, { method: "POST", body: JSON.stringify(body) });
+ const put = (content: string) =>
+ api.request("/projects/demo/files/index.html", {
+ method: "PUT",
+ body: content,
+ headers: { "If-Match": fileContentVersion("A") },
+ });
+ return { projectDir, file, histories, api, post, put };
+}
+
+async function claimFirstWrite(post: ReturnType<typeof lazyHistoryProject>["post"]) {
+ const reply = await post("/claim", {
+ label: "Color",
+ paths: ["index.html"],
+ overwrote: { "index.html": fileContentVersion("A") },
+ });
+ return ((await reply.json()) as { claimed: unknown }).claimed;
+}
+
/** A project whose index.html reads "A", with its history and the routes over it. */
async function demoProject({ quietMs }: { quietMs?: number } = {}) {
const projectDir = tempDir("hf-history-routes-");
@@ -340,6 +395,63 @@ describe("history routes", () => {
expect(back).toMatchObject({ id: claimed.id, label: "Moved Title" });
});
+ it("undo Studio's first write when that write is what opens the project's history", async () => {
+ const { file, put, post } = lazyHistoryProject();
+
+ expect((await put("B")).status).toBe(200);
+ expect(await claimFirstWrite(post)).not.toBeNull();
+ expect(await (await post("/step", { direction: "back" })).json()).toMatchObject({ ok: true });
+ expect(readFileSync(file, "utf-8")).toBe("A");
+ });
+
+ it("save and undo the first edit while the project's media is still being copied in", async () => {
+ let finishCopy = () => {};
+ mediaCopy.held = new Promise<void>((resolve) => (finishCopy = resolve));
+ const { projectDir, file, put, post, histories } = lazyHistoryProject();
+ writeFileSync(join(projectDir, "clip.mp4"), Buffer.alloc(4 * 1024 ** 2, 7));
+
+ // The copy stays held until finishCopy, so a save or claim that waited on it never returns.
+ expect((await put("B")).status).toBe(200);
+ expect(await claimFirstWrite(post)).not.toBeNull();
+ await post("/step", { direction: "back" });
+ expect(readFileSync(file, "utf-8")).toBe("A");
+
+ finishCopy();
+ const history = (await histories.get(projectDir))!;
+ await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
+ timeout: 10_000,
+ });
+ await history.flush();
+ expect(history.list().map((entry) => entry.label)).toEqual(["Color", "Undid: Color"]);
+ });
+
+ it("undo Studio's overwrite of media whose first copy was still running", async () => {
+ let finishCopy = () => {};
+ mediaCopy.held = new Promise<void>((resolve) => (finishCopy = resolve));
+ const { projectDir, api, post } = lazyHistoryProject();
+ const clip = join(projectDir, "clip.mp4");
+ const original = Buffer.alloc(4 * 1024 ** 2, 7);
+ writeFileSync(clip, original);
+ await post("/claim", { label: "Nothing", paths: [] });
+
+ const written = await api.request("/projects/demo/files/clip.mp4", {
+ method: "PUT",
+ body: Buffer.alloc(4 * 1024 ** 2, 9),
+ headers: { "If-Match": fileContentVersion(original) },
+ });
+ expect(written.status).toBe(200);
+ finishCopy();
+ const claimed = await post("/claim", {
+ label: "Replace clip",
+ paths: ["clip.mp4"],
+ overwrote: { "clip.mp4": fileContentVersion(original) },
+ });
+ expect(((await claimed.json()) as { claimed: unknown }).claimed).not.toBeNull();
+
+ await post("/step", { direction: "back" });
+ expect(readFileSync(clip).equals(original)).toBe(true);
+ });
+
it("end a window given idleMs by itself once its writes stop", async () => {
const { projectDir, call } = await demoProject();
@@ -350,7 +462,7 @@ describe("history routes", () => {
await vi.waitFor(
async () =>
expect((await (await call("")).json()).entries).toMatchObject([{ label: "Dragged Title" }]),
- { timeout: 2_000, interval: 50 },
+ { timeout: 10_000, interval: 50 },
);
const { entry } = await (await call(`/window/${windowId}/close`, {})).json();
expect(entry).toMatchObject({ id: windowId, label: "Dragged Title" });
diff --git a/packages/studio/src/utils/gsapUndoRestore.test.ts b/packages/studio/src/utils/gsapUndoRestore.test.ts
index cd1003878e..34f0599a18 100644
--- a/packages/studio/src/utils/gsapUndoRestore.test.ts
+++ b/packages/studio/src/utils/gsapUndoRestore.test.ts
@@ -1,6 +1,19 @@
// @vitest-environment happy-dom
import { describe, it, expect, vi } from "vitest";
+import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
+
+const stamping = vi.hoisted(() => ({ calls: 0 }));
+vi.mock("@hyperframes/parsers/hf-ids", async (importOriginal) => {
+ const real = await importOriginal<typeof import("@hyperframes/parsers/hf-ids")>();
+ return {
+ ...real,
+ ensureHfIds: (html: string) => {
+ stamping.calls += 1;
+ return real.ensureHfIds(html);
+ },
+ };
+});
import {
applyUndoRestoreToPreview,
diffSoftReloadableRestore,
@@ -100,6 +113,36 @@ function buildLiveIframe(bodyHtml: string) {
describe("applyUndoRestoreToPreview", () => {
const ROOT = "index.html";
+ it("never stamps an undo whose sides are both already stamped", () => {
+ const film = (color: string) =>
+ `<div id="root" data-composition-id="root"><div id="target" style="background-color: ${color}">T</div><p>caption</p></div>`;
+ const blue = ensureHfIds(film("blue"));
+ const red = blue.replace("background-color: blue", "background-color: red");
+ const { iframe } = buildLiveIframe(red);
+ stamping.calls = 0;
+
+ const files = { [ROOT]: { previous: wrap(red), restored: wrap(blue) } };
+ expect(applyUndoRestoreToPreview(iframe, ROOT, files, 3, vi.fn())).toBe("soft");
+ expect(stamping.calls).toBe(0);
+ });
+
+ it("repaints in place an undo back to the film before Studio first stamped it", () => {
+ const film = (color: string) =>
+ `<div id="root" data-composition-id="root"><div id="target" style="background-color: ${color}">T</div><p>caption</p></div>`;
+ // The preview stamped the film as it loaded; the first edit wrote those stamps with its red.
+ const edited = ensureHfIds(film("blue")).replace(
+ "background-color: blue",
+ "background-color: red",
+ );
+ const { iframe, doc } = buildLiveIframe(edited);
+ const reloadPreview = vi.fn();
+ const files = { [ROOT]: { previous: wrap(edited), restored: wrap(film("blue")) } };
+
+ expect(applyUndoRestoreToPreview(iframe, ROOT, files, 3, reloadPreview)).toBe("soft");
+ expect(reloadPreview).not.toHaveBeenCalled();
+ expect((doc.getElementById("target") as HTMLElement).style.backgroundColor).toBe("blue");
+ });
+
it("soft-applies an attribute/style-only restore: syncs the live element, no full reload", () => {
const { iframe, contentWindow, doc } = buildLiveIframe(
`<div id="a" style="translate: 10px 10px" data-hf-path-offset="true">t</div>`,
diff --git a/packages/studio/src/utils/gsapUndoRestore.ts b/packages/studio/src/utils/gsapUndoRestore.ts
index 5814a59afc..186fde3662 100644
--- a/packages/studio/src/utils/gsapUndoRestore.ts
+++ b/packages/studio/src/utils/gsapUndoRestore.ts
@@ -9,7 +9,7 @@ import {
findGsapScriptElements,
readNestedFiles,
} from "./gsapSoftReload";
-import { isCompositionTemplate } from "@hyperframes/parsers/hf-ids";
+import { ensureHfIds, isCompositionTemplate } from "@hyperframes/parsers/hf-ids";
import { findAuthoredElement, parseSavedSource } from "./authoredSource";
import { STUDIO_EDIT_ATTRS } from "../components/editor/manualEditsSeekReapply";
import { markScenesStale } from "../player/sceneSwap";
@@ -252,8 +252,11 @@ function fileTargets(
isActive: boolean,
file: UndoRestoreFile,
): RestorePlan | null {
- const prevDoc = parseRestoreSource(file.previous);
- const nextDoc = parseRestoreSource(file.restored);
+ const previewStamped = doc.querySelector("[data-hf-id]") !== null;
+ const stampedLikeThePreview = (html: string) =>
+ previewStamped && !html.includes("data-hf-id") ? ensureHfIds(html) : html;
+ const prevDoc = parseRestoreSource(stampedLikeThePreview(file.previous));
+ const nextDoc = parseRestoreSource(stampedLikeThePreview(file.restored));
const scripted = restoreScripted(prevDoc, nextDoc, isActive);
if (scripted === null) return null;
const keys = diffRestoreDocs(prevDoc, nextDoc);
diff --git a/packages/studio/tests/e2e/undo-socket-pool.mjs b/packages/studio/tests/e2e/undo-socket-pool.mjs
index fda8c841a7..5566c08036 100644
--- a/packages/studio/tests/e2e/undo-socket-pool.mjs
+++ b/packages/studio/tests/e2e/undo-socket-pool.mjs
@@ -122,8 +122,19 @@ try {
throw new Error(`studio_look found no #target: ${JSON.stringify(look).slice(0, 400)}`);
const colors = ["#ff0000", "#00ff00", "#0000ff", "#ff00ff", "#00ffff"];
+ // What the preview paints for #target: an undo must show the edit taken back, not only write the file.
+ const previewColor = async () => {
+ for (const frame of page.frames()) {
+ const color = await frame
+ .$eval("#target", (el) => getComputedStyle(el).backgroundColor)
+ .catch(() => null);
+ if (color) return color;
+ }
+ return null;
+ };
for (let round = 0; round < ROUNDS; round++) {
const before = readIndex();
+ const shownBefore = await previewColor();
await call("studio_set_style", {
handle: target.handle,
styles: { backgroundColor: colors[round % colors.length] },
@@ -152,10 +163,12 @@ try {
await until(() => step()?.done, 15_000);
const undo = step();
const reverted = await until(() => readIndex() === before, 5_000);
+ const repainted = await until(async () => (await previewColor()) === shownBefore, 3_000);
const stallMs = undo?.timing ? Math.round(undo.timing.sendStart) : null;
- rounds.push({ round, pinned, stallMs, reverted });
- // Round 0 is exempt until the fix for the first undo of a GSAP-free film, which takes back only part, lands.
- if (!reverted && round > 0) failures.push(`round ${round}: the undo did not restore the file`);
+ rounds.push({ round, pinned, stallMs, reverted, repainted });
+ if (!reverted) failures.push(`round ${round}: the undo did not restore the file`);
+ if (!repainted)
+ failures.push(`round ${round}: the preview still showed the edit 3 s after the undo`);
if (stallMs == null)
failures.push(`round ${round}: the undo's POST history/step never answered`);
else if (stallMs > STALL_LIMIT_MS)