From d606c2a531e0f60a258677d36f764a5b29f4c5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 07:55:02 +0000 Subject: [PATCH] feat(studio-server): history names the bytes its next restore point would copy in (#8120) * feat(studio-server): history names the bytes its next restore point would copy in * fix(studio-server): count each unsaved blob once, and name the method for what it counts * refactor(studio-server): a file the next restore point copies nothing of is its own check --- .../studio-server/src/history/blobStore.ts | 2 +- .../src/history/projectHistory.ts | 35 +++++++++++++- .../history/projectHistory.unsaved.test.ts | 47 +++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 packages/studio-server/src/history/projectHistory.unsaved.test.ts diff --git a/packages/studio-server/src/history/blobStore.ts b/packages/studio-server/src/history/blobStore.ts index f8da22ae1c..8e902aa60b 100644 --- a/packages/studio-server/src/history/blobStore.ts +++ b/packages/studio-server/src/history/blobStore.ts @@ -70,7 +70,7 @@ async function readChecked(path: string, hash: string): Promise { export const readBlob = (dir: string, hash: string) => readChecked(blobPath(dir, hash), hash); -async function hashFile(path: string): Promise { +export async function hashFile(path: string): Promise { const hash = createHash("sha256"); for await (const chunk of createReadStream(path)) hash.update(chunk); return hash.digest("hex"); diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts index 1d5d45170c..28b6bda371 100644 --- a/packages/studio-server/src/history/projectHistory.ts +++ b/packages/studio-server/src/history/projectHistory.ts @@ -30,7 +30,14 @@ import { listProjectFilesAsync, type ListedProjectFile, } from "../helpers/projectSignature.js"; -import { isSettling, openBlobStore, readBlob, SETTLE_MS, type BlobStore } from "./blobStore.js"; +import { + hashFile, + isSettling, + openBlobStore, + readBlob, + SETTLE_MS, + type BlobStore, +} from "./blobStore.js"; import { isUncopied, uncopiedOnlyHashes } from "./uncopied.js"; import { assertStillWithin, @@ -155,6 +162,7 @@ export interface ProjectHistory { label: string, options?: { idleMs?: number; exclusive?: boolean }, ): Promise; + unsavedBlobBytes(): Promise; /** * For a writer that records after writing (Studio): moves the uncommitted changes to `paths` into one entry of * `who`'s, each cut at the `overwrote` version so earlier parts stay their writer's. Same-key claims merge until @@ -289,6 +297,12 @@ function withoutHiddenPaths(log: HistoryLog, ignores?: (path: string) => boolean return log; } +const hashIfPresent = (path: string) => + hashFile(path).catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT") return null; + throw error; + }); + function dropKeptOutside(entry: HistoryEntry, ignores?: (path: string) => boolean): boolean { const kept = Object.entries(entry.keptInPlace ?? {}); const left = kept.filter(([path]) => isHistoryPath(path, ignores)); @@ -830,6 +844,24 @@ class Engine { replaceFileAtomically(join(this.home, "stat.json"), files, 0o644); } + copiesNothing(file: ListedFile, now: number): boolean { + if (this.adopting.has(file.path) || isUncopied(file.path, file.size)) return true; + const stat = statKey(file, now); + return stat !== "" && this.tracked.get(file.path)?.stat === stat; + } + + async unsavedBlobBytes(): Promise { + if (this.whereFolder() !== "here") return 0; + const now = this.now(); + const missing = new Map(); + for (const file of await this.historyFiles()) { + if (this.copiesNothing(file, now)) continue; + const hash = await hashIfPresent(join(this.dir, file.path)); + if (hash && !this.blobs.has(hash)) missing.set(hash, file.size); + } + return [...missing.values()].reduce((sum, size) => sum + size, 0); + } + /** * Takes in every write since the last sweep and files each change to its writer. ponytail: always the whole * project (a stat per file, a hash only when the stat moved); per-path sweeps if projects reach tens of thousands. @@ -1611,6 +1643,7 @@ class Engine { 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())); return this.queue(() => this.claimNow(who, label, paths, options)); diff --git a/packages/studio-server/src/history/projectHistory.unsaved.test.ts b/packages/studio-server/src/history/projectHistory.unsaved.test.ts new file mode 100644 index 0000000000..782a641234 --- /dev/null +++ b/packages/studio-server/src/history/projectHistory.unsaved.test.ts @@ -0,0 +1,47 @@ +// @vitest-environment node +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, expect, it } from "vitest"; +import { openProjectHistory } from "./projectHistory"; + +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; +} + +it("names the bytes the next restore point would copy in: changed files the store lacks, no media", async () => { + const projectDir = tempDir("hf-history-project-"); + const historyRoot = tempDir("hf-history-root-"); + writeFileSync(join(projectDir, "index.html"), "

Hello

"); + const history = await openProjectHistory({ + projectDir, + historyRoot, + ignores: (path) => path.startsWith("private/"), + }); + cleanup.push(() => history.close()); + await (await history.beginWindow({ kind: "person", name: "You" }, "first")).close(); + expect(await history.unsavedBlobBytes()).toBe(0); + + writeFileSync(join(projectDir, "scene.html"), "x".repeat(1000)); + // The same bytes as a stored file are already in the store. + writeFileSync(join(projectDir, "copy.html"), "

Hello

"); + writeFileSync(join(projectDir, "clip.mp4"), "v".repeat(5000)); + expect(await history.unsavedBlobBytes()).toBe(1000); + + // Two new files with the same bytes are one blob. + writeFileSync(join(projectDir, "b1.html"), "y".repeat(2000)); + writeFileSync(join(projectDir, "b2.html"), "y".repeat(2000)); + expect(await history.unsavedBlobBytes()).toBe(3000); + + // A path history ignores is never copied in. + mkdirSync(join(projectDir, "private")); + writeFileSync(join(projectDir, "private", "notes.html"), "z".repeat(4000)); + expect(await history.unsavedBlobBytes()).toBe(3000); +});