Skip to content

Commit c6478e4

Browse files
fix(studio): undo takes back the first edit of a film whose media is still being copied (#5043)
* fix(studio): undo takes back the first edit of a film whose media is still being copied * fix(studio): the first undo of a film repaints the preview in place * fix(studio): background media copies keep the media ledger and survive a history prune * fix(studio): a busy project history never makes writes wait for its lock again * refactor(studio): name the restore stamping instead of commenting it * perf(studio): undo stamps only a restore side that has no stamps * fix(studio): closing a project history finishes its media copy first * fix(studio): media copies stay resumable until recorded and skip a replaced folder * fix(studio): the history lock wait comes back after a successful open
1 parent 967752e commit c6478e4

10 files changed

Lines changed: 489 additions & 23 deletions

File tree

‎packages/cli/src/server/studioServer.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,44 @@ describe("createStudioServer project history (D-491)", () => {
185185
},
186186
);
187187

188+
it("never makes a request wait on another process's history lock again once it was busy", async () => {
189+
const waits: unknown[] = [];
190+
historyState.open = async (options) => {
191+
waits.push((options as { ownerWaitMs?: number }).ownerWaitMs);
192+
throw new HistoryBusyError(1);
193+
};
194+
const projectDir = tmpProject();
195+
server = createStudioServer({ projectDir, historyRoot: tmpProject() });
196+
const historyUrl = `/api/projects/${encodeURIComponent(basename(projectDir))}/history`;
197+
await server.app.request(historyUrl);
198+
await server.app.request(historyUrl);
199+
historyState.open = null;
200+
expect(waits).toEqual([undefined, 0]);
201+
await server.shutdown();
202+
});
203+
204+
it("waits for the history lock again once a history opened after a busy one", async () => {
205+
const waits: unknown[] = [];
206+
const reopenable = { replacedAtPath: () => true, close: async () => {} };
207+
let opens = 0;
208+
historyState.open = async (options) => {
209+
waits.push((options as { ownerWaitMs?: number }).ownerWaitMs);
210+
opens += 1;
211+
if (opens === 1) throw new HistoryBusyError(1);
212+
if (opens === 2) return reopenable;
213+
historyState.open = null;
214+
throw new HistoryBusyError(1);
215+
};
216+
const projectDir = tmpProject();
217+
server = createStudioServer({ projectDir, historyRoot: tmpProject() });
218+
const historyUrl = `/api/projects/${encodeURIComponent(basename(projectDir))}/history`;
219+
await server.app.request(historyUrl);
220+
await server.app.request(historyUrl);
221+
historyState.open = null;
222+
expect(waits).toEqual([undefined, 0, undefined]);
223+
await server.shutdown();
224+
});
225+
188226
it("opens a new project's own history once it takes the folder's path", async () => {
189227
const projectDir = tmpProject();
190228
writeFileSync(join(projectDir, "index.html"), "<html>before</html>");

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,25 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
436436
};
437437

438438
// Opened on first use, so a server that never serves Studio's history never writes one. A failed open stays off
439-
// for this run; one another process was holding is tried again on the next request.
439+
// for this run; one another process was holding is tried again on the next request, which no write waits for.
440+
let ownerWaitMs: number | undefined;
440441
const histories = historyCache(() =>
441442
openProjectHistory({
442443
projectDir,
443444
historyRoot: options.historyRoot ?? DEFAULT_HISTORY_ROOT,
444-
}).catch((error: unknown) => {
445-
console.warn(`[studio] Project history is off: ${String(error)}`);
446-
if (error instanceof HistoryBusyError || error instanceof HistoryClosedError)
447-
histories.forget(projectDir);
448-
return null;
449-
}),
445+
ownerWaitMs,
446+
})
447+
.then((history) => {
448+
ownerWaitMs = undefined;
449+
return history;
450+
})
451+
.catch((error: unknown) => {
452+
console.warn(`[studio] Project history is off: ${String(error)}`);
453+
if (error instanceof HistoryBusyError) ownerWaitMs = 0;
454+
if (error instanceof HistoryBusyError || error instanceof HistoryClosedError)
455+
histories.forget(projectDir);
456+
return null;
457+
}),
450458
);
451459
const projectHistory = () => histories.get(projectDir);
452460
watcher.addListener((changedPath) => {

‎packages/studio-server/src/createStudioApi.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ export function createStudioApi(adapter: StudioApiAdapter): Hono {
5353
.catch(() => null);
5454
if (project) adapter.invalidateProjectSignature?.(project.dir);
5555
});
56+
api.use("/projects/:id/*", async function openHistorySoNoWriteBecomesItsBaseline(c, next) {
57+
if (c.req.method !== "GET" && c.req.method !== "HEAD" && adapter.history) {
58+
await Promise.resolve()
59+
.then(() => adapter.resolveProject(c.req.param("id")))
60+
.then((project) => project && adapter.history?.(project))
61+
.catch(() => null);
62+
}
63+
await next();
64+
});
5665

5766
registerProjectRoutes(api, adapter);
5867
registerFileRoutes(api, adapter);

‎packages/studio-server/src/history/historyLog.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ export function saveRecord(file: string, log: HistoryLog, record: LogRecord): vo
100100
}
101101
}
102102

103+
export function baselineRecord(log: HistoryLog): LogRecord {
104+
return {
105+
type: "baseline",
106+
files: Object.fromEntries(log.baseline),
107+
...(log.keepsLedger && { keepsLedger: true }),
108+
};
109+
}
110+
103111
export function writeLog(file: string, log: HistoryLog): void {
104112
const records: LogRecord[] = [
105-
{
106-
type: "baseline",
107-
files: Object.fromEntries(log.baseline),
108-
...(log.keepsLedger && { keepsLedger: true }),
109-
},
113+
baselineRecord(log),
110114
...log.entries.map((entry) => ({ type: "entry" as const, entry })),
111115
...[...log.pins].map((id) => ({ type: "pin" as const, id, pinned: true })),
112116
];

‎packages/studio-server/src/history/projectHistory.test.ts‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,44 @@ import { HistoryIdError } from "./historyId";
2424
import { HistoryClosedError, openProjectHistory, type ProjectHistory } from "./projectHistory";
2525
import { START, type HistoryWho } from "./historyLog";
2626

27+
// A test can hold a media copy, and run work after its blob is stored but before history records it.
28+
const mediaCopy = vi.hoisted(() => ({
29+
held: null as Promise<void> | null,
30+
stored: null as (() => Promise<unknown>) | null,
31+
copies: [] as string[],
32+
fails: null as ((copy: number) => boolean) | null,
33+
}));
34+
vi.mock("./blobStore", async (importOriginal) => {
35+
const real = await importOriginal<typeof import("./blobStore")>();
36+
return {
37+
...real,
38+
openBlobStore: async (dir: string) => {
39+
const store = await real.openBlobStore(dir);
40+
return {
41+
...store,
42+
put: async (path: string) => {
43+
if (path.endsWith(".mp4")) {
44+
await mediaCopy.held;
45+
mediaCopy.copies.push(basename(path));
46+
if (mediaCopy.fails?.(mediaCopy.copies.length)) throw new Error("The disk is full.");
47+
}
48+
const hash = await store.put(path);
49+
if (path.endsWith(".mp4")) await mediaCopy.stored?.();
50+
return hash;
51+
},
52+
};
53+
},
54+
};
55+
});
56+
2757
const you: HistoryWho = { kind: "person", name: "You" };
2858
const pause = (ms: number) => new Promise((settle) => setTimeout(settle, ms));
2959
const agent: HistoryWho = { kind: "agent", name: "Agent" };
3060
const cleanup: Array<() => unknown> = [];
3161

3262
afterEach(async () => {
3363
for (const step of cleanup.splice(0).reverse()) await step();
64+
Object.assign(mediaCopy, { held: null, stored: null, copies: [], fails: null });
3465
});
3566

3667
const inside = (dir: string, path: string) => readFileSync(join(dir, path), "utf-8");
@@ -265,6 +296,138 @@ describe("openProjectHistory", () => {
265296
expect([read("index.html"), has(".media/manifest.jsonl")]).toEqual(["A", false]);
266297
});
267298

299+
it("a ledger made while closed is still a change after the project's media was copied in the background", async () => {
300+
const { history, write, has, projectDir, historyRoot } = await project(
301+
{ "index.html": "A", "clip.mp4": Buffer.alloc(2 * 1024 ** 2, 7) },
302+
{ quietMs: 30 },
303+
);
304+
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
305+
timeout: 10_000,
306+
});
307+
await history.close();
308+
write("b.png", "png");
309+
write(".media/manifest.jsonl", '{"path":"b.png"}\n');
310+
const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
311+
await reopened.flush();
312+
const [outside] = reopened.list();
313+
expect(outside!.files.map((file) => file.path).sort()).toEqual([
314+
".media/manifest.jsonl",
315+
"b.png",
316+
]);
317+
expect((await reopened.undo(outside!.id, { who: you })).ok).toBe(true);
318+
expect([has("b.png"), has(".media/manifest.jsonl")]).toEqual([false, false]);
319+
});
320+
321+
it("names media in adopting.json until its copy is recorded, so an exit mid-copy resumes it", async () => {
322+
let release = () => {};
323+
mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
324+
const { history, historyRoot } = await project({
325+
"index.html": "A",
326+
"clip.mp4": Buffer.alloc(2 * 1024 ** 2, 7),
327+
});
328+
const adopting = join(historyRoot, history.projectId, "adopting.json");
329+
expect(JSON.parse(readFileSync(adopting, "utf-8"))).toEqual(["clip.mp4"]);
330+
release();
331+
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
332+
timeout: 10_000,
333+
});
334+
expect(existsSync(adopting)).toBe(false);
335+
});
336+
337+
it("keeps media whose recording failed for the next open, rather than log it as added", async () => {
338+
const { history, write, historyRoot } = await project(
339+
{ "index.html": "A", "clip.mp4": Buffer.alloc(2 * 1024 ** 2, 7) },
340+
{ budgetBytes: 1, quietMs: 30 },
341+
);
342+
// The edit's prune takes the first copy; storing it again then fails.
343+
mediaCopy.fails = (copy) => copy === 2;
344+
mediaCopy.stored = () => {
345+
mediaCopy.stored = null;
346+
return change(history, you, "Color", () => write("index.html", "B"));
347+
};
348+
await vi.waitFor(() => expect(mediaCopy.copies).toHaveLength(2), { timeout: 10_000 });
349+
await history.flush();
350+
expect(history.list().flatMap((entry) => entry.files.map((file) => file.path))).not.toContain(
351+
"clip.mp4",
352+
);
353+
const adopting = join(historyRoot, history.projectId, "adopting.json");
354+
expect(JSON.parse(readFileSync(adopting, "utf-8"))).toEqual(["clip.mp4"]);
355+
});
356+
357+
it("never copies the media of a folder that replaced the project", async () => {
358+
let release = () => {};
359+
mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
360+
const media = {
361+
"a.mp4": Buffer.alloc(2 * 1024 ** 2, 1),
362+
"b.mp4": Buffer.alloc(2 * 1024 ** 2, 2),
363+
};
364+
const { history, write, projectDir } = await project({ "index.html": "A", ...media });
365+
renameSync(projectDir, `${projectDir}-moved`);
366+
cleanup.push(() => rmSync(`${projectDir}-moved`, { recursive: true, force: true }));
367+
mkdirSync(projectDir);
368+
for (const [path, bytes] of Object.entries(media)) write(path, bytes);
369+
release();
370+
await history.close();
371+
// Only the copy already running when the folder changed may finish.
372+
expect(mediaCopy.copies).toEqual(["a.mp4"]);
373+
});
374+
375+
it("undoes a clip replaced while closed, though the history closed before its copy finished", async () => {
376+
const clip = Buffer.alloc(2 * 1024 ** 2, 7);
377+
let release = () => {};
378+
mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
379+
const { history, write, projectDir, historyRoot } = await project(
380+
{ "index.html": "A", "clip.mp4": clip },
381+
{ quietMs: 30 },
382+
);
383+
const closed = history.close();
384+
release();
385+
await closed;
386+
write("clip.mp4", Buffer.alloc(2 * 1024 ** 2, 9));
387+
const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
388+
await reopened.flush();
389+
expect(reopened.list().map((entry) => entry.files.map((file) => file.path))).toEqual([
390+
["clip.mp4"],
391+
]);
392+
expect(await reopened.step("back", you)).toMatchObject({ ok: true });
393+
expect(readFileSync(join(projectDir, "clip.mp4")).equals(clip)).toBe(true);
394+
});
395+
396+
it("adopts media whose copy a process exit cut short, rather than log it as added", async () => {
397+
const { history, write, projectDir, historyRoot } = await project({ "index.html": "A" });
398+
await history.close();
399+
// What an exit mid-copy leaves: the clip on disk, named as still being copied, not in the baseline.
400+
write("clip.mp4", Buffer.alloc(2 * 1024 ** 2, 7));
401+
writeFileSync(join(historyRoot, history.projectId, "adopting.json"), '["clip.mp4"]');
402+
const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
403+
await vi.waitFor(() => expect(reopened.peek(START)).toHaveProperty(["clip.mp4"]), {
404+
timeout: 10_000,
405+
});
406+
await reopened.flush();
407+
expect(reopened.list()).toEqual([]);
408+
});
409+
410+
it("keeps a media copy that a budget prune ran into before history recorded it", async () => {
411+
const clip = Buffer.alloc(2 * 1024 ** 2, 7);
412+
let release = () => {};
413+
mediaCopy.held = new Promise<void>((resolve) => (release = resolve));
414+
const { history, write } = await project(
415+
{ "index.html": "A", "clip.mp4": clip },
416+
{ budgetBytes: 1, quietMs: 30 },
417+
);
418+
// The copy is stored; an edit commits and prunes before history records the copy.
419+
mediaCopy.stored = () => {
420+
mediaCopy.stored = null;
421+
return change(history, you, "Color", () => write("index.html", "B"));
422+
};
423+
release();
424+
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
425+
timeout: 10_000,
426+
});
427+
const start = history.peek(START)["clip.mp4"]!;
428+
expect((await history.readBlob(start)).equals(clip)).toBe(true);
429+
});
430+
268431
it("a log 0.8.123 wrote without a ledger takes none in, and one made later while closed is a change", async () => {
269432
const { history, write, projectDir, historyRoot } = await project(
270433
{ "index.html": "A" },

0 commit comments

Comments
 (0)