From f4433409646f20afa5b3c3a5e96b91c16105b7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 29 Nov 2026 12:58:58 +0000 Subject: [PATCH 1/2] fix(studio-server): history never drops an edit to make room for bytes it must keep --- .../src/history/projectHistory.saving.test.ts | 28 +++++++++++++++++-- .../src/history/projectHistory.ts | 14 +++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/packages/studio-server/src/history/projectHistory.saving.test.ts b/packages/studio-server/src/history/projectHistory.saving.test.ts index d6bda3712a9..254c9401fe2 100644 --- a/packages/studio-server/src/history/projectHistory.saving.test.ts +++ b/packages/studio-server/src/history/projectHistory.saving.test.ts @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; import { basename, join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { fileContentVersion, hashOfVersion, recordFileWriteReceipt } from "../helpers/fileVersion"; -import { SETTLE_MS } from "./blobStore"; +import { SETTLE_MS, blobPath } from "./blobStore"; import { START, type HistoryWho } from "./historyLog"; import { openProjectHistory, type ProjectHistory } from "./projectHistory"; @@ -51,7 +51,7 @@ async function project(options = {}) { cleanup.push(() => history.close()); const write = (content: string) => writeFileSync(join(projectDir, "index.html"), content); const read = () => readFileSync(join(projectDir, "index.html"), "utf-8"); - return { projectDir, history, write, read, index: join(projectDir, "index.html") }; + return { projectDir, historyRoot, history, write, read, index: join(projectDir, "index.html") }; } describe("a file an outside save has emptied but not yet written", () => { @@ -215,3 +215,27 @@ describe("a file still being written when the history first opens", () => { expect(readFileSync(join(projectDir, "media.txt"), "utf-8")).toBe("recorded"); }); }); + +describe("bytes a save replaced that history has not read back yet", () => { + // fails without the fix: the 200 replaced bytes counted against the budget, so the edit's own entry was dropped. + it("stay kept, and never push an edit out of a history within its budget", async () => { + const { projectDir, historyRoot, history, write } = await project({ budgetBytes: 64 }); + const replaced = "x".repeat(200); + // Nothing is on disk there, so no sweep reads it and its note stays. + recordFileWriteReceipt(join(projectDir, "away.html"), { + path: "away.html", + version: fileContentVersion("saved"), + writeToken: "studio", + overwrote: replaced, + }); + write("

Edited

"); + await history.claim(you, "Edit", ["index.html"]); + expect(history.list().map((entry) => entry.label)).toEqual(["Edit"]); + expect(history.peek(START)!["index.html"]).toBe( + hashOfVersion(fileContentVersion("

Hello

")), + ); + const blobs = join(historyRoot, history.projectId, "blobs"); + const kept = blobPath(blobs, hashOfVersion(fileContentVersion(replaced))!); + expect(readFileSync(kept, "utf-8")).toBe(replaced); + }); +}); diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts index c2ec4fbd005..e85ddaed0a5 100644 --- a/packages/studio-server/src/history/projectHistory.ts +++ b/packages/studio-server/src/history/projectHistory.ts @@ -1596,12 +1596,19 @@ class Engine { return entry; } + /** Bytes saves replaced, kept until history takes those saves in: dropping old entries frees none of them. */ + replacedHashes(): string[] { + return [ + ...[...this.overwritten.values()].flatMap((byAfter) => [...byAfter.values()]), + ...[...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]), + ]; + } + /** Stored bytes beyond what the current files need: a project larger than the budget still keeps its history. */ historyBytes(): number { let current = 0; const baseline = [...this.tracked.values()].map((file) => file.hash); - const owedStarts = [...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]); - for (const hash of new Set([...baseline, ...this.blobs.pinned(), ...owedStarts])) + for (const hash of new Set([...baseline, ...this.blobs.pinned(), ...this.replacedHashes()])) current += this.blobs.size(hash); return this.blobs.bytes() - current; } @@ -1614,8 +1621,7 @@ class Engine { new Set([ ...referencedHashes(this.log, this.manifest()), ...this.pendingHashes(), - ...[...this.overwritten.values()].flatMap((byAfter) => [...byAfter.values()]), - ...[...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]), + ...this.replacedHashes(), ]), ); if (this.historyBytes() <= budget || !foldOldest(this.log)) break; From e48efe205928b9c9b48239cceb2b8f5e976df218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 29 Nov 2026 13:13:44 +0000 Subject: [PATCH 2/2] fix(studio-server): name the kept replaced bytes instead of commenting them --- packages/studio-server/src/history/projectHistory.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/studio-server/src/history/projectHistory.ts b/packages/studio-server/src/history/projectHistory.ts index e85ddaed0a5..396ee363529 100644 --- a/packages/studio-server/src/history/projectHistory.ts +++ b/packages/studio-server/src/history/projectHistory.ts @@ -1596,8 +1596,7 @@ class Engine { return entry; } - /** Bytes saves replaced, kept until history takes those saves in: dropping old entries frees none of them. */ - replacedHashes(): string[] { + keptReplacedHashes(): string[] { return [ ...[...this.overwritten.values()].flatMap((byAfter) => [...byAfter.values()]), ...[...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]), @@ -1608,7 +1607,7 @@ class Engine { historyBytes(): number { let current = 0; const baseline = [...this.tracked.values()].map((file) => file.hash); - for (const hash of new Set([...baseline, ...this.blobs.pinned(), ...this.replacedHashes()])) + for (const hash of new Set([...baseline, ...this.blobs.pinned(), ...this.keptReplacedHashes()])) current += this.blobs.size(hash); return this.blobs.bytes() - current; } @@ -1621,7 +1620,7 @@ class Engine { new Set([ ...referencedHashes(this.log, this.manifest()), ...this.pendingHashes(), - ...this.replacedHashes(), + ...this.keptReplacedHashes(), ]), ); if (this.historyBytes() <= budget || !foldOldest(this.log)) break;