Skip to content

Commit 3f0a20b

Browse files
fix(studio-server): history never drops an edit to make room for bytes it must keep (#9096)
* fix(studio-server): history never drops an edit to make room for bytes it must keep * fix(studio-server): name the kept replaced bytes instead of commenting them
1 parent da8c087 commit 3f0a20b

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
55
import { basename, join } from "node:path";
66
import { afterEach, describe, expect, it, vi } from "vitest";
77
import { fileContentVersion, hashOfVersion, recordFileWriteReceipt } from "../helpers/fileVersion";
8-
import { SETTLE_MS } from "./blobStore";
8+
import { SETTLE_MS, blobPath } from "./blobStore";
99
import { START, type HistoryWho } from "./historyLog";
1010
import { openProjectHistory, type ProjectHistory } from "./projectHistory";
1111

@@ -51,7 +51,7 @@ async function project(options = {}) {
5151
cleanup.push(() => history.close());
5252
const write = (content: string) => writeFileSync(join(projectDir, "index.html"), content);
5353
const read = () => readFileSync(join(projectDir, "index.html"), "utf-8");
54-
return { projectDir, history, write, read, index: join(projectDir, "index.html") };
54+
return { projectDir, historyRoot, history, write, read, index: join(projectDir, "index.html") };
5555
}
5656

5757
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", () => {
215215
expect(readFileSync(join(projectDir, "media.txt"), "utf-8")).toBe("recorded");
216216
});
217217
});
218+
219+
describe("bytes a save replaced that history has not read back yet", () => {
220+
// fails without the fix: the 200 replaced bytes counted against the budget, so the edit's own entry was dropped.
221+
it("stay kept, and never push an edit out of a history within its budget", async () => {
222+
const { projectDir, historyRoot, history, write } = await project({ budgetBytes: 64 });
223+
const replaced = "x".repeat(200);
224+
// Nothing is on disk there, so no sweep reads it and its note stays.
225+
recordFileWriteReceipt(join(projectDir, "away.html"), {
226+
path: "away.html",
227+
version: fileContentVersion("saved"),
228+
writeToken: "studio",
229+
overwrote: replaced,
230+
});
231+
write("<h1>Edited</h1>");
232+
await history.claim(you, "Edit", ["index.html"]);
233+
expect(history.list().map((entry) => entry.label)).toEqual(["Edit"]);
234+
expect(history.peek(START)!["index.html"]).toBe(
235+
hashOfVersion(fileContentVersion("<h1>Hello</h1>")),
236+
);
237+
const blobs = join(historyRoot, history.projectId, "blobs");
238+
const kept = blobPath(blobs, hashOfVersion(fileContentVersion(replaced))!);
239+
expect(readFileSync(kept, "utf-8")).toBe(replaced);
240+
});
241+
});

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,12 +1596,18 @@ class Engine {
15961596
return entry;
15971597
}
15981598

1599+
keptReplacedHashes(): string[] {
1600+
return [
1601+
...[...this.overwritten.values()].flatMap((byAfter) => [...byAfter.values()]),
1602+
...[...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]),
1603+
];
1604+
}
1605+
15991606
/** Stored bytes beyond what the current files need: a project larger than the budget still keeps its history. */
16001607
historyBytes(): number {
16011608
let current = 0;
16021609
const baseline = [...this.tracked.values()].map((file) => file.hash);
1603-
const owedStarts = [...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]);
1604-
for (const hash of new Set([...baseline, ...this.blobs.pinned(), ...owedStarts]))
1610+
for (const hash of new Set([...baseline, ...this.blobs.pinned(), ...this.keptReplacedHashes()]))
16051611
current += this.blobs.size(hash);
16061612
return this.blobs.bytes() - current;
16071613
}
@@ -1614,8 +1620,7 @@ class Engine {
16141620
new Set([
16151621
...referencedHashes(this.log, this.manifest()),
16161622
...this.pendingHashes(),
1617-
...[...this.overwritten.values()].flatMap((byAfter) => [...byAfter.values()]),
1618-
...[...this.heardBeforeCopy.values()].flatMap((replaced) => [...replaced]),
1623+
...this.keptReplacedHashes(),
16191624
]),
16201625
);
16211626
if (this.historyBytes() <= budget || !foldOldest(this.log)) break;

0 commit comments

Comments
 (0)