Skip to content

Commit 893b4f6

Browse files
authored
feat(studio): record video state, edit targets and artifact cleanup in project history (#5524)
* feat(studio): record video state, edit targets and artifact cleanup * fix(studio): clear video-state CI gates * fix(studio-server): pin cleanup reads to file descriptors * chore: account for cleanup descriptor code in package budget
1 parent f31b3c9 commit 893b4f6

24 files changed

Lines changed: 1403 additions & 38 deletions

‎packages/studio-server/src/helpers/projectSignature.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { readRenderOutputsOrNone, isRenderOutput } from "./renderOutputs.js";
12
import { createHash } from "node:crypto";
2-
import { lstatSync, readFileSync, readdirSync } from "node:fs";
3+
import { lstatSync, readFileSync, readdirSync, type Stats } from "node:fs";
34
import { extname, isAbsolute, relative, resolve, sep } from "node:path";
45
import { isAtomicTempPath } from "@hyperframes/core/atomic-file";
56
import type { ResolvedProject, StudioApiAdapter } from "../types.js";
@@ -68,6 +69,7 @@ export function affectsProjectSignature(projectDir: string, changedPath: string)
6869
) {
6970
return false;
7071
}
72+
if (isRenderOutput(projectDir, changedPath, readRenderOutputsOrNone(projectDir))) return false;
7173
const segments = relativePath.split(sep);
7274
if (STUDIO_SIGNATURE_MANIFEST_PATHS.includes(segments.join("/") as never)) return true;
7375
return !segments.some((segment) => SIGNATURE_EXCLUDED_DIRS.has(segment));
@@ -106,10 +108,27 @@ function isTextContentEligible(file: string, size: number): boolean {
106108
const isSkippedEntry = (entry: string) =>
107109
SIGNATURE_EXCLUDED_DIRS.has(entry) || isAtomicTempPath(entry);
108110

111+
function signatureEntryStat(
112+
projectDir: string,
113+
entry: string,
114+
file: string,
115+
outputs: readonly string[],
116+
): Stats | null {
117+
if (isSkippedEntry(entry) || !isPathWithin(projectDir, file)) return null;
118+
if (isRenderOutput(projectDir, file, outputs)) return null;
119+
try {
120+
const stat = lstatSync(file);
121+
return stat.isSymbolicLink() ? null : stat;
122+
} catch {
123+
return null;
124+
}
125+
}
126+
109127
function collectProjectSignatureFiles(
110128
projectDir: string,
111129
dir: string,
112130
files: ProjectSignatureFile[],
131+
outputs = readRenderOutputsOrNone(projectDir),
113132
): void {
114133
let entries: string[];
115134
try {
@@ -119,18 +138,11 @@ function collectProjectSignatureFiles(
119138
}
120139

121140
for (const entry of entries) {
122-
if (isSkippedEntry(entry)) continue;
123141
const file = resolve(dir, entry);
124-
if (!isPathWithin(projectDir, file)) continue;
125-
let stat: ReturnType<typeof lstatSync>;
126-
try {
127-
stat = lstatSync(file);
128-
} catch {
129-
continue;
130-
}
131-
if (stat.isSymbolicLink()) continue;
142+
const stat = signatureEntryStat(projectDir, entry, file, outputs);
143+
if (!stat) continue;
132144
if (stat.isDirectory()) {
133-
collectProjectSignatureFiles(projectDir, file, files);
145+
collectProjectSignatureFiles(projectDir, file, files, outputs);
134146
} else if (stat.isFile()) {
135147
files.push({
136148
file,
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { expect, it } from "vitest";
5+
import { registerRenderOutput, readRenderOutputs } from "./renderOutputs.js";
6+
import { listProjectFiles, affectsProjectSignature } from "./projectSignature.js";
7+
import { openProjectHistory } from "../history/projectHistory.js";
8+
import { beginArtifactCapture, recordRenderSuccess } from "../history/videoState.js";
9+
10+
it("keeps root render outputs out of revisions while tracking source video edits", async () => {
11+
const root = mkdtempSync(join(tmpdir(), "hf-render-history-"));
12+
const project = join(root, "project");
13+
mkdirSync(project);
14+
writeFileSync(join(project, "index.html"), "initial");
15+
writeFileSync(join(project, "input.mp4"), "source video");
16+
const history = await openProjectHistory({
17+
projectDir: project,
18+
historyRoot: join(root, "history"),
19+
});
20+
const state = () =>
21+
JSON.parse(readFileSync(join(project, ".hyperframes/video-state.json"), "utf8"));
22+
try {
23+
const window = await history.beginWindow({ kind: "agent", name: "test" }, "edit");
24+
writeFileSync(join(project, "index.html"), "edited");
25+
await history.checkpoint();
26+
const revision = state().currentRevision;
27+
const output = join(project, "benefits.mp4");
28+
await registerRenderOutput(project, output);
29+
const capture = await beginArtifactCapture(project);
30+
expect(capture.revision).toBe(revision);
31+
for (const bytes of ["first render", "second render"]) {
32+
writeFileSync(output, bytes);
33+
const render = await recordRenderSuccess(project, output, "completed", capture);
34+
expect(render.revision).toBe(revision);
35+
await history.checkpoint();
36+
expect(state().currentRevision).toBe(revision);
37+
expect(state().pendingChanges).toBe(false);
38+
expect(state().files["benefits.mp4"]).toBeUndefined();
39+
}
40+
expect(affectsProjectSignature(project, output)).toBe(false);
41+
expect(affectsProjectSignature(project, join(project, "input.mp4"))).toBe(true);
42+
writeFileSync(join(project, "input.mp4"), "updated source video");
43+
await window.close();
44+
expect(state().currentRevision).not.toBe(revision);
45+
expect(Object.keys(state().revisions[state().currentRevision].files)).toEqual(["input.mp4"]);
46+
const nextCapture = await beginArtifactCapture(project);
47+
writeFileSync(join(project, "index.html"), "changed during rendering");
48+
writeFileSync(output, "third render");
49+
expect(
50+
(await recordRenderSuccess(project, output, "completed", nextCapture)).revision,
51+
).toBeNull();
52+
} finally {
53+
await history.close();
54+
rmSync(root, { recursive: true, force: true });
55+
}
56+
});
57+
it("registers concurrent custom outputs and directory renders without excluding other media", async () => {
58+
const root = mkdtempSync(join(tmpdir(), "hf-render-registry-"));
59+
try {
60+
await Promise.all([
61+
registerRenderOutput(root, join(root, "a.webm")),
62+
registerRenderOutput(root, join(root, "hls")),
63+
]);
64+
expect(readRenderOutputs(root)).toEqual(["a.webm", "hls"]);
65+
mkdirSync(join(root, "hls"));
66+
writeFileSync(join(root, "hls", "chunk.ts"), "output");
67+
writeFileSync(join(root, "a.webm"), "output");
68+
writeFileSync(join(root, "b.webm"), "input");
69+
expect(listProjectFiles(root).map((file) => file.path)).toEqual(["b.webm"]);
70+
await registerRenderOutput(root, join(root, "..", "external.mp4"));
71+
expect(readRenderOutputs(root)).toEqual(["a.webm", "hls"]);
72+
await expect(registerRenderOutput(root, root)).rejects.toThrow("project directory");
73+
} finally {
74+
rmSync(root, { recursive: true, force: true });
75+
}
76+
});
77+
78+
it("lists project files when the render output registry is unreadable", () => {
79+
const project = mkdtempSync(join(tmpdir(), "hf-render-registry-"));
80+
try {
81+
writeFileSync(join(project, "index.html"), "x");
82+
writeFileSync(join(project, ".hyperframes"), "a file, not a folder");
83+
expect(readRenderOutputs(project)).toEqual([]);
84+
rmSync(join(project, ".hyperframes"));
85+
mkdirSync(join(project, ".hyperframes"));
86+
writeFileSync(join(project, ".hyperframes", "render-outputs.json"), "{not json");
87+
expect(() => readRenderOutputs(project)).toThrow();
88+
expect(listProjectFiles(project).length).toBeGreaterThan(0);
89+
expect(affectsProjectSignature(project, join(project, "index.html"))).toBe(true);
90+
} finally {
91+
rmSync(project, { recursive: true, force: true });
92+
}
93+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { readFileSync } from "node:fs";
2+
import { isAbsolute, relative, resolve, sep } from "node:path";
3+
import { replaceFileAtomically } from "@hyperframes/core/atomic-file";
4+
import { takeHistoryOwnership } from "../history/ownerLock.js";
5+
import { pinProjectRecord } from "./safePath.js";
6+
7+
const manifest = ".hyperframes/render-outputs.json";
8+
9+
export function readRenderOutputs(projectDir: string): string[] {
10+
const path = pinProjectRecord(projectDir, manifest);
11+
if (!path) return [];
12+
let text: string;
13+
try {
14+
text = readFileSync(path, "utf8");
15+
} catch (error) {
16+
const code = (error as NodeJS.ErrnoException).code;
17+
if (code === "ENOENT" || code === "ENOTDIR") return [];
18+
throw error;
19+
}
20+
const value = JSON.parse(text);
21+
if (
22+
value.version !== 1 ||
23+
!Array.isArray(value.paths) ||
24+
!value.paths.every(
25+
(p: unknown) =>
26+
typeof p === "string" &&
27+
p !== "" &&
28+
!isAbsolute(p) &&
29+
!p.split(/[\\/]/).some((part) => part === ".." || part === "."),
30+
)
31+
)
32+
throw new Error("Invalid render output registry.");
33+
return value.paths;
34+
}
35+
36+
export function readRenderOutputsOrNone(projectDir: string): string[] {
37+
try {
38+
return readRenderOutputs(projectDir);
39+
} catch {
40+
return [];
41+
}
42+
}
43+
44+
export function isRenderOutput(
45+
projectDir: string,
46+
path: string,
47+
outputs: readonly string[],
48+
): boolean {
49+
const candidate = relative(resolve(projectDir), resolve(path)).split(sep).join("/");
50+
return outputs.some((output) => candidate === output || candidate.startsWith(`${output}/`));
51+
}
52+
53+
export async function registerRenderOutput(projectDir: string, output: string): Promise<void> {
54+
const path = relative(resolve(projectDir), resolve(output)).split(sep).join("/");
55+
if (path === "") throw new Error("Render output cannot replace the project directory.");
56+
if (isAbsolute(path) || path === ".." || path.startsWith("../")) return;
57+
const registry = pinProjectRecord(projectDir, manifest);
58+
const lock = pinProjectRecord(projectDir, ".hyperframes/render-outputs-lock");
59+
if (!registry || !lock) throw new Error("Render output registry must stay inside the project.");
60+
const release = await takeHistoryOwnership(lock, 5000);
61+
try {
62+
const paths = [...new Set([...readRenderOutputs(projectDir), path])].sort();
63+
replaceFileAtomically(registry, JSON.stringify({ version: 1, paths }) + "\n", 0o644);
64+
} finally {
65+
release();
66+
}
67+
}

‎packages/studio-server/src/helpers/safePath.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ export function isPrivateProjectFile(projectDir: string, filePath: string): bool
5959
* file checked here even if a link on the way is retargeted before the write.
6060
*/
6161
export function pinWithinProject(base: string, relativePath: string): string | null {
62-
const checked = resolveWithinProject(base, relativePath);
62+
return pinResolved(base, resolveWithinProject(base, relativePath));
63+
}
64+
65+
/** `pinWithinProject` for the app's and the CLI's own records under `.hyperframes/`, which no route serves. */
66+
export function pinProjectRecord(base: string, relativePath: string): string | null {
67+
return pinResolved(base, resolveInProject(base, relativePath));
68+
}
69+
70+
function pinResolved(base: string, checked: string | null): string | null {
6371
if (!checked) return null;
6472
const inside = relative(realFilePath(base), realFilePath(checked));
6573
const escapes = inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside);

0 commit comments

Comments
 (0)