Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/studio-server/src/history/projectHistory.saving.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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("<h1>Edited</h1>");
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("<h1>Hello</h1>")),
);
const blobs = join(historyRoot, history.projectId, "blobs");
const kept = blobPath(blobs, hashOfVersion(fileContentVersion(replaced))!);
expect(readFileSync(kept, "utf-8")).toBe(replaced);
});
});
13 changes: 9 additions & 4 deletions packages/studio-server/src/history/projectHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,12 +1596,18 @@ class Engine {
return entry;
}

keptReplacedHashes(): 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.keptReplacedHashes()]))
current += this.blobs.size(hash);
return this.blobs.bytes() - current;
}
Expand All @@ -1614,8 +1620,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.keptReplacedHashes(),
]),
);
if (this.historyBytes() <= budget || !foldOldest(this.log)) break;
Expand Down
Loading