Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio-server): history saved by 0.8.78 survives the update with every entry (#4601)
Miguel Ángel3db3da7bb284
diff --git a/packages/cli/src/commands/history.ts b/packages/cli/src/commands/history.tsindex 1395fe3640..3b88a4be8f 100644--- a/packages/cli/src/commands/history.ts+++ b/packages/cli/src/commands/history.ts@@ -6,6 +6,7 @@ import { defineCommand, type ArgsDef } from "citty";import {HISTORY_START,HistoryBusyError,+ HistoryIdError,type HistoryEntry,type HistoryListItem,type HistoryResult,@@ -277,7 +278,8 @@ function guarded<A>(run: (args: A) => Promise<void>) {const refused =error instanceof Refusal ||error instanceof AmbiguousPreviewServerError ||- error instanceof HistoryBusyError;+ error instanceof HistoryBusyError ||+ error instanceof HistoryIdError;if (!refused) throw error;setCommandExitCode(2);const { message } = error as Error;diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/history-id b/packages/studio-server/src/history/__fixtures__/history-0.8.78/history-idnew file mode 100644index 0000000000..766205a626--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/history-id@@ -0,0 +1 @@+aaf9077d-99e4-4c62-a4a9-e3fecd03e537diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/index.html b/packages/studio-server/src/history/__fixtures__/history-0.8.78/index.htmlnew file mode 100644index 0000000000..915cbbeeb9--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/index.html@@ -0,0 +1 @@+<p>v3</p>diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/17/17447ef1cecb59829280af0f5fb3057c92916ff42c77e23e3b3d0ded67e2052d b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/17/17447ef1cecb59829280af0f5fb3057c92916ff42c77e23e3b3d0ded67e2052dnew file mode 100644index 0000000000..7ad32bdbee--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/17/17447ef1cecb59829280af0f5fb3057c92916ff42c77e23e3b3d0ded67e2052d@@ -0,0 +1 @@+<p>v1</p>diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/5b/5b342709000bfc0340dca6033d65a3db160e63e6561235afb834b82227ea9ef6 b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/5b/5b342709000bfc0340dca6033d65a3db160e63e6561235afb834b82227ea9ef6new file mode 100644index 0000000000..915cbbeeb9--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/5b/5b342709000bfc0340dca6033d65a3db160e63e6561235afb834b82227ea9ef6@@ -0,0 +1 @@+<p>v3</p>diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/f0/f0bb9a80dfd9312e4d8c3229a955eec8a5e7e86a310be74f6a81fcd93480c3c3 b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/f0/f0bb9a80dfd9312e4d8c3229a955eec8a5e7e86a310be74f6a81fcd93480c3c3new file mode 100644index 0000000000..c8b328ed21--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/blobs/f0/f0bb9a80dfd9312e4d8c3229a955eec8a5e7e86a310be74f6a81fcd93480c3c3@@ -0,0 +1 @@+<p>v2</p>diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/log.jsonl b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/log.jsonlnew file mode 100644index 0000000000..1b6ab7c31d--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/log.jsonl@@ -0,0 +1,3 @@+{"type":"baseline","files":{"index.html":"17447ef1cecb59829280af0f5fb3057c92916ff42c77e23e3b3d0ded67e2052d"}}+{"type":"entry","entry":{"id":"40c67f66-b8bd-4a20-b813-d5de7c8443f1","who":{"kind":"person","name":"You"},"label":"Moved Title","startedAt":1790516955951,"endedAt":1790516955951,"files":[{"path":"index.html","before":"17447ef1cecb59829280af0f5fb3057c92916ff42c77e23e3b3d0ded67e2052d","after":"f0bb9a80dfd9312e4d8c3229a955eec8a5e7e86a310be74f6a81fcd93480c3c3"}]}}+{"type":"entry","entry":{"id":"41486d91-d85c-459d-8449-3a126729e1d2","who":{"kind":"agent","name":"Agent"},"label":"Agent turn","startedAt":1790516955954,"endedAt":1790516955954,"files":[{"path":"index.html","before":"f0bb9a80dfd9312e4d8c3229a955eec8a5e7e86a310be74f6a81fcd93480c3c3","after":"5b342709000bfc0340dca6033d65a3db160e63e6561235afb834b82227ea9ef6"}]}}diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/project.json b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/project.jsonnew file mode 100644index 0000000000..959bb945e9--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/project.json@@ -0,0 +1 @@+{ "dir": "/tmp/project" }diff --git a/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/stat.json b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/stat.jsonnew file mode 100644index 0000000000..efb764c538--- /dev/null+++ b/packages/studio-server/src/history/__fixtures__/history-0.8.78/root/aaf9077d-99e4-4c62-a4a9-e3fecd03e537/stat.json@@ -0,0 +1,6 @@+{+ "index.html": {+ "hash": "5b342709000bfc0340dca6033d65a3db160e63e6561235afb834b82227ea9ef6",+ "stat": ""+ }+}diff --git a/packages/studio-server/src/history/historyId.ts b/packages/studio-server/src/history/historyId.tsindex 089c612f79..e09a6ae41a 100644--- a/packages/studio-server/src/history/historyId.ts+++ b/packages/studio-server/src/history/historyId.ts@@ -1,6 +1,6 @@import { randomUUID } from "node:crypto";import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";-import { join, resolve } from "node:path";+import { basename, join, resolve } from "node:path";import { replaceFileAtomically } from "../helpers/atomicFile.js";export const ID_PATH = join(".hyperframes", "history-id");@@ -22,22 +22,44 @@ export const sameFolder = (a: FolderIdentity, b: FolderIdentity) =>a.ino === b.ino && a.birthtimeMs === b.birthtimeMs;export function isRecordedFolder(historyDir: string, folder: FolderIdentity): boolean {+ const was = readRecord(historyDir);+ if (typeof was?.ino === "number")+ return sameFolder({ ino: was.ino, birthtimeMs: was.born ?? NaN }, folder);+ const unreadableSo0878KeptTheId = typeof was?.dir !== "string";+ if (unreadableSo0878KeptTheId) return true;+ return !isCopyOf0878Folder(was!.dir as string, basename(historyDir), folder);+}++function readRecord(historyDir: string): { dir?: unknown; ino?: unknown; born?: number } | null {try {- const was = JSON.parse(readFileSync(join(historyDir, "project.json"), "utf-8"));- return sameFolder({ ino: was.ino, birthtimeMs: was.born }, folder);+ return JSON.parse(readFileSync(join(historyDir, "project.json"), "utf-8"));+ } catch {+ return null;+ }+}++function isCopyOf0878Folder(recordedDir: string, id: string, folder: FolderIdentity): boolean {+ let there;+ try {+ there = statSync(recordedDir, { throwIfNoEntry: false });} catch {return false;}+ return !!there && !sameFolder(there, folder) && readId(recordedDir) === id;}/**- * The project's history id, kept in the project so a rename or move keeps its history. Where history exists under it,- * only the recorded folder keeps it: same inode and creation time, since an inode alone is reused after a delete.+ * The project's history id, kept in the project so a rename or move keeps its history; only the folder its history+ * was recorded for keeps it (isRecordedFolder). An id file that cannot be read is refused, never replaced.*/export function projectHistoryId(projectDir: string, historyRoot: string): string {const dir = resolve(projectDir);const folder = statSync(dir);let id = readId(dir);+ if (!id && existsSync(join(dir, ID_PATH)))+ throw new HistoryIdError(+ `${join(dir, ID_PATH)} holds no history id this version can read; move it aside to start anew.`,+ );if (!id ||(existsSync(join(historyRoot, id)) && !isRecordedFolder(join(historyRoot, id), folder))@@ -50,6 +72,13 @@ export function projectHistoryId(projectDir: string, historyRoot: string): strinreturn id;}+export class HistoryIdError extends Error {+ constructor(message: string) {+ super(message);+ this.name = "HistoryIdError";+ }+}+export function recordProject(historyDir: string, dir: string, folder: FolderIdentity): void {const record = { dir, ino: folder.ino, born: folder.birthtimeMs };mkdirSync(historyDir, { recursive: true });diff --git a/packages/studio-server/src/history/projectHistory.test.ts b/packages/studio-server/src/history/projectHistory.test.tsindex f2eefe4081..deafb1ce7a 100644--- a/packages/studio-server/src/history/projectHistory.test.ts+++ b/packages/studio-server/src/history/projectHistory.test.ts@@ -20,6 +20,7 @@ import { basename, dirname, join, relative } from "node:path";import { afterEach, describe, expect, it, vi } from "vitest";import { fileContentVersion, recordFileWriteReceipt } from "../helpers/fileVersion";import { HistoryBusyError } from "./ownerLock";+import { HistoryIdError } from "./historyId";import { HistoryClosedError, openProjectHistory, type ProjectHistory } from "./projectHistory";import { START, type HistoryWho } from "./historyLog";@@ -632,17 +633,96 @@ describe("openProjectHistory", () => {);});- it("gives a folder whose history record cannot prove it is that folder a history of its own", async () => {+ it("keeps the history of a record 0.8.78 wrote for this folder, and records the folder's identity", async () => {const { history, write, projectDir, historyRoot } = await project({ "index.html": "v1" });await change(history, you, "Old change", () => write("index.html", "v2"));await history.close();- writeFileSync(- join(historyRoot, history.projectId, "project.json"),- JSON.stringify({ dir: projectDir }),- );+ const record = join(historyRoot, history.projectId, "project.json");+ writeFileSync(record, JSON.stringify({ dir: projectDir }));const again = await open(projectDir, historyRoot);- expect([again.projectId === history.projectId, again.list()]).toEqual([false, []]);+ expect(again.projectId).toBe(history.projectId);+ expect(again.list().map((entry) => entry.label)).toEqual(["Old change"]);+ expect(JSON.parse(readFileSync(record, "utf-8"))).toMatchObject({+ dir: projectDir,+ ino: expect.any(Number),+ });+ });++ describe("a history 0.8.78 wrote", () => {+ const fixture = join(import.meta.dirname, "__fixtures__", "history-0.8.78");+ const fixtureId = readFileSync(join(fixture, "history-id"), "utf-8").trim();++ /** The fixture's project and history root, laid out as 0.8.78 left them. */+ function legacyProject(recordedDir: (projectDir: string) => string) {+ const projectDir = tempDir("hf-history-legacy-");+ const historyRoot = tempDir("hf-history-legacy-root-");+ cpSync(join(fixture, "root"), historyRoot, { recursive: true });+ mkdirSync(join(projectDir, ".hyperframes"));+ cpSync(join(fixture, "history-id"), join(projectDir, ".hyperframes", "history-id"));+ cpSync(join(fixture, "index.html"), join(projectDir, "index.html"));+ const record = join(historyRoot, fixtureId, "project.json");+ writeFileSync(record, JSON.stringify({ dir: recordedDir(projectDir) }));+ return { projectDir, historyRoot };+ }++ it.each([+ ["at the path it was recorded for", (projectDir: string) => projectDir],+ ["moved since", (projectDir: string) => `${projectDir}-before-the-move`],+ ])("opens %s with every entry, and can undo them", async (_, recordedDir) => {+ const { projectDir, historyRoot } = legacyProject(recordedDir);++ const history = await open(projectDir, historyRoot);+ expect(history.projectId).toBe(fixtureId);+ expect(inside(projectDir, ".hyperframes/history-id").trim()).toBe(fixtureId);+ const entries = history.list();+ expect(entries.map((entry) => [entry.who.name, entry.label])).toEqual([+ ["You", "Moved Title"],+ ["Agent", "Agent turn"],+ ]);+ expect(await history.undo(entries[1]!.id, { who: you })).toMatchObject({ ok: true });+ expect(inside(projectDir, "index.html")).toBe("<p>v2</p>\n");+ });++ it.each([+ ["empty", ""],+ ["null", "null"],+ ])("keeps the id when the record is %s, as 0.8.78 did", async (_, record) => {+ const { projectDir, historyRoot } = legacyProject((projectDir) => projectDir);+ writeFileSync(join(historyRoot, fixtureId, "project.json"), record);++ const history = await open(projectDir, historyRoot);+ expect([history.projectId, history.list().length]).toEqual([fixtureId, 2]);+ });++ it("keeps the history when the record names the folder by another path to it", async () => {+ const { projectDir, historyRoot } = legacyProject((projectDir) => `${projectDir}-link`);+ symlinkSync(projectDir, `${projectDir}-link`, "junction");+ cleanup.push(() => rmSync(`${projectDir}-link`, { force: true }));++ const history = await open(projectDir, historyRoot);+ expect([history.projectId, history.list().length]).toEqual([fixtureId, 2]);+ });++ it("gives a copy its own history while the recorded folder still carries the id", async () => {+ const original = legacyProject((projectDir) => projectDir);+ const copy = tempDir("hf-history-legacy-copy-");+ cpSync(original.projectDir, copy, { recursive: true });++ const copied = await open(copy, original.historyRoot);+ expect([copied.projectId === fixtureId, copied.list()]).toEqual([false, []]);+ expect((await open(original.projectDir, original.historyRoot)).list()).toHaveLength(2);+ });+ });++ it("refuses to open, and leaves the file alone, when the history id cannot be read", async () => {+ const projectDir = tempDir("hf-history-bad-id-");+ writeFileSync(join(projectDir, "index.html"), "v1");+ mkdirSync(join(projectDir, ".hyperframes"));+ writeFileSync(join(projectDir, ".hyperframes", "history-id"), "not-an-id\n");++ await expect(open(projectDir, tempDir("hf-history-root-"))).rejects.toThrow(HistoryIdError);+ expect(inside(projectDir, ".hyperframes/history-id")).toBe("not-an-id\n");});it("leaves the id alone for a history root that has no history under it, so the open one keeps recording", async () => {@@ -976,7 +1056,7 @@ describe("openProjectHistory", () => {expect(reopened.list().map((kept) => kept.label)).toEqual(["Second", "Third"]);});- it("mints its own id when the project's history-id is anything else, so a project cannot pick where history is written", async () => {+ it("refuses a history-id that is anything else, so a project cannot pick where history is written", async () => {const projectDir = tempDir("hf-history-project-");const historyRoot = tempDir("hf-history-root-");const victim = tempDir("hf-history-victim-");@@ -985,12 +1065,11 @@ describe("openProjectHistory", () => {mkdirSync(join(projectDir, ".hyperframes"));writeFileSync(join(projectDir, ".hyperframes", "history-id"), relative(historyRoot, victim));- const history = await open(projectDir, historyRoot);- expect(history.projectId).toMatch(/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/);+ await expect(open(projectDir, historyRoot)).rejects.toThrow(/history-id/);expect(readdirSync(victim)).toEqual(["project.json"]);expect(readFileSync(join(victim, "project.json"), "utf-8")).toBe('{"precious":true}');const idFile = join(projectDir, ".hyperframes", "history-id");- expect(readFileSync(idFile, "utf-8").trim()).toBe(history.projectId);+ expect(readFileSync(idFile, "utf-8")).toBe(relative(historyRoot, victim));});it("commits a window that is never closed when the history flushes, so an undo still reaches its writes", async () => {diff --git a/packages/studio-server/src/index.ts b/packages/studio-server/src/index.tsindex fc7b2cc8dd..a6b9adb11e 100644--- a/packages/studio-server/src/index.ts+++ b/packages/studio-server/src/index.ts@@ -17,6 +17,7 @@ export {HistoryClosedError,} from "./history/projectHistory.js";export { HistoryBusyError } from "./history/ownerLock.js";+export { HistoryIdError } from "./history/historyId.js";export { historyCache } from "./history/historyCache.js";export {START as HISTORY_START,diff --git a/packages/studio-server/src/types.ts b/packages/studio-server/src/types.tsindex 12c041b942..ac0769e51e 100644--- a/packages/studio-server/src/types.ts+++ b/packages/studio-server/src/types.ts@@ -109,8 +109,8 @@ export interface StudioApiAdapter {resolveProject(id: string): Promise<ResolvedProject | null> | ResolvedProject | null;/**- * Optional: the project's history (openProjectHistory), one per project for the host's lifetime. Without it- * the history routes answer 404.+ * Optional: the project's current history. A history refuses every call once its folder is replaced (a+ * deleted `.hyperframes`, a new project there), so keep them in `historyCache`, which reopens. Else routes 404.*/history?: (project: ResolvedProject) => Promise<ProjectHistory | null> | ProjectHistory | null;