Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
959edbc
fix(studio-server): a file the drive never answers no longer holds a …
miguel-heygen Nov 18, 2026
29ee475
fix(studio-server): keep history's comment share at or below its base
miguel-heygen Nov 18, 2026
e718969
fix(studio-server): a first copy that lands past its deadline is kept…
miguel-heygen Nov 19, 2026
4892c12
fix(studio-server): a late first copy is adopted and kept while anoth…
miguel-heygen Nov 19, 2026
68a04bb
fix(studio-server): a waiting late first copy counts as baseline and …
miguel-heygen Nov 19, 2026
4b65fe7
fix(studio-server): every reread of an owed file takes a copy slot an…
miguel-heygen Nov 19, 2026
065757f
fix(studio-server): history pins a copy as it is filed and keeps a re…
miguel-heygen Nov 19, 2026
59d9710
fix(studio-server): a history pin is taken and released by its owner,…
miguel-heygen Nov 19, 2026
9e2378b
Merge remote-tracking branch 'origin/main' into fix/history-copy-dead…
miguel-heygen Nov 21, 2026
27631d4
fix(studio-server): an agent step waits at most a deadline for a file…
miguel-heygen Nov 21, 2026
397d3bf
fix(studio-server): undo refuses a file whose first copy landed after…
miguel-heygen Nov 21, 2026
031671c
chore(studio-server): give the packed size budget its usual 5% headroom
miguel-heygen Nov 21, 2026
33eafbf
fix(studio-server): an agent step that goes ahead of a first copy own…
miguel-heygen Nov 21, 2026
6ff4d4e
fix(studio-server): undo of a step that went ahead of a pending first…
miguel-heygen Nov 21, 2026
dfe957d
Merge remote-tracking branch 'origin/main' into fix/history-copy-dead…
miguel-heygen Nov 21, 2026
a30cf5b
fix(studio-server): an undo after a step that rewrote a pending file …
miguel-heygen Nov 21, 2026
a2032ba
fix(studio-server): a step ahead of a pending file refuses undo after…
miguel-heygen Nov 21, 2026
b910bd6
fix(studio-server): a step that went ahead of a pending file never un…
miguel-heygen Nov 21, 2026
021580d
fix(studio-server): a recovered step keeps its guard through a second…
miguel-heygen Nov 21, 2026
2e293fc
fix(studio-server): a start keeps receipts heard before its read; reo…
miguel-heygen Nov 21, 2026
59ee378
fix(studio-server): a first copy's start follows only receipts heard …
miguel-heygen Nov 21, 2026
5a0b3a6
fix(studio-server): bytes kept for a pending first copy count as star…
miguel-heygen Nov 21, 2026
46e78b1
Merge remote-tracking branch 'origin/main' into fix/history-copy-dead…
miguel-heygen Nov 21, 2026
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
48 changes: 48 additions & 0 deletions packages/studio-server/src/history/__fixtures__/stepThenDie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Reads of `stuck` (comma-separated) never answer; a step rewrites or deletes them, then the process dies: "open"
// mid-step, "reopen"/"reopen-list" recovering the JSON window, at its first sweep/went-ahead.json write.
import fs, { unlinkSync, writeFileSync } from "node:fs";
import fsp from "node:fs/promises";
import { syncBuiltinESMExports } from "node:module";
import { join } from "node:path";

const [projectDir, historyRoot, stuck, mode = "close", closedWindow] = process.argv.slice(2);
const stuckFiles = stuck!.split(",");
const realOpen = fsp.open;
fsp.open = (async (...args: Parameters<typeof realOpen>) => {
if (stuckFiles.includes(String(args[0]))) await new Promise(() => {});
if (mode === "reopen" && String(args[0]) === join(projectDir!, "index.html"))
process.kill(process.pid, "SIGKILL");
return realOpen(...args);
}) as typeof realOpen;
const realRename = fs.renameSync;
fs.renameSync = ((from: fs.PathLike, to: fs.PathLike) => {
realRename(from, to);
if (mode === "reopen-list" && String(to).endsWith("went-ahead.json"))
process.kill(process.pid, "SIGKILL");
}) as typeof realRename;
syncBuiltinESMExports();

const { openProjectHistory } = await import("../projectHistory.js");
const history = await openProjectHistory({
projectDir: projectDir!,
historyRoot: historyRoot!,
firstCopyFileMs: 100,
now: () => Date.now() + 60_000,
...(closedWindow && { closedWindow: JSON.parse(closedWindow) }),
});
const agent = { kind: "agent", name: "Framey" } as const;
const index = join(projectDir!, "index.html");
if (mode === "earlier") {
writeFileSync(index, "<html>first</html>");
await history.claim({ kind: "person", name: "You" }, "First", [index]);
}
const step = await history.beginWindow(agent, "Step");
await new Promise((sent) => process.send!({ id: step.id }, sent));
for (const path of stuckFiles)
if (mode === "delete") unlinkSync(path);
else writeFileSync(path, "// agent\n");
writeFileSync(index, "<html>agent</html>");
for (const path of stuckFiles) history.noteChange(path);
history.noteChange(index);
if (mode !== "open") await step.close();
process.kill(process.pid, "SIGKILL");
107 changes: 104 additions & 3 deletions packages/studio-server/src/history/firstCopy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ vi.mock("node:fs/promises", async (importOriginal) => {
import * as fsp from "node:fs/promises";
import { fileContentVersion, recordFileWriteReceipt } from "../helpers/fileVersion.js";
import { blobPath } from "./blobStore.js";
import { START } from "./historyLog.js";
import { START, type HistoryWho } from "./historyLog.js";
import { openProjectHistory } from "./projectHistory.js";

const { writeFile: realWriteFile, open: realOpen } =
Expand Down Expand Up @@ -54,6 +54,29 @@ function projectOf(count: number, tag: string): string {
for (let i = 0; i < count; i++) writeFileSync(join(dir, "parts", `p${i}.js`), `// ${tag} ${i}\n`);
return dir;
}
/** A project with `count` 2 MiB files, copied in the background after the open, one after another. */
function mediaProject(count: number): string {
const dir = tempDir("hf-first-copy-");
writeFileSync(join(dir, "index.html"), "<html></html>");
for (let i = 0; i < count; i++)
writeFileSync(join(dir, `m${i}.bin`), Buffer.alloc(2 * 1024 ** 2, i + 1));
return dir;
}
const agent: HistoryWho = { kind: "agent", name: "Framey" };
/** The first open of `path` waits until an answer is called, at the latest before the test's histories close. */
function stallFirstOpenOf(path: string): (() => void)[] {
const releases: (() => void)[] = [];
let stalled = false;
vi.mocked(fsp.open).mockImplementation(async (...args: Parameters<typeof realOpen>) => {
if (String(args[0]) === path && !stalled) {
stalled = true;
await new Promise<void>((answer) => releases.push(answer));
}
return realOpen(...args);
});
closes.push(async () => releases.forEach((answer) => answer()));
return releases;
}
const homeIn = (historyRoot: string) => {
const homes = readdirSync(historyRoot, { withFileTypes: true }).filter((e) => e.isDirectory());
return join(historyRoot, homes.find((e) => existsSync(join(historyRoot, e.name, "blobs")))!.name);
Expand Down Expand Up @@ -223,11 +246,32 @@ describe("a project's first history copy", () => {
expect(existsSync(join(homeIn(historyRoot), "adopting.json"))).toBe(false);
});

it("arms no deadline for a read already owed when its slot comes", async () => {
const projectDir = projectOf(4, "owed-queued");
const releases: (() => void)[] = [];
closes.push(async () => releases.forEach((answer) => answer()));
let opened = 0;
vi.mocked(fsp.open).mockImplementation(async (...args: Parameters<typeof realOpen>) => {
if (String(args[0]).startsWith(projectDir) && ++opened <= 4)
await new Promise<void>((answer) => releases.push(answer));
return realOpen(...args);
});
const armed = vi.spyOn(globalThis, "setTimeout");
closes.push(async () => armed.mockRestore());
const historyRoot = tempDir("hf-first-copy-root-");
const history = await openProjectHistory({ projectDir, historyRoot, firstCopyFileMs: 777 });
closes.push(() => history.close());
const deadlines = () => armed.mock.calls.filter(([, ms]) => ms === 777).length;
const before = deadlines();
releases.shift()!();
await vi.waitFor(() => expect(opened).toBeGreaterThanOrEqual(5));
expect(deadlines()).toBe(before);
});

it("does not stretch a stalled read's deadline while other first copies finish", async () => {
const projectDir = projectOf(1, "stretch");
const stuck = join(projectDir, "parts", "p0.js");
const releases: (() => void)[] = [];
closes.push(async () => releases.forEach((answer) => answer()));
const releases = stallFirstOpenOf(stuck);
vi.mocked(fsp.open).mockImplementation(async (...args: Parameters<typeof realOpen>) => {
if (String(args[0]) === stuck) await new Promise<void>((answer) => releases.push(answer));
if (String(args[0]) === join(projectDir, "index.html"))
Expand All @@ -244,6 +288,63 @@ describe("a project's first history copy", () => {
]);
});

it("begins an agent step with no second deadline while a file's read never answers", async () => {
const projectDir = projectOf(6, "stuck");
const stuck = join(projectDir, "parts", "p1.js");
const releases: (() => void)[] = [];
vi.mocked(fsp.open).mockImplementation(async (...args: Parameters<typeof realOpen>) => {
if (String(args[0]) === stuck) await new Promise<void>((answer) => releases.push(answer));
return realOpen(...args);
});
try {
const openedAt = Date.now();
const history = await openProjectHistory({
projectDir,
historyRoot: tempDir("hf-first-copy-root-"),
firstCopyFileMs: 1000,
});
closes.push(() => history.close());
await history.beginWindow(agent, "Step");
expect(Date.now() - openedAt).toBeGreaterThanOrEqual(990);
expect(Date.now() - openedAt).toBeLessThan(1800);
} finally {
for (const answer of releases) answer();
}
});

it("begins an agent step after one deadline when media waits behind a read that never answers", async () => {
const projectDir = mediaProject(6);
stallFirstOpenOf(join(projectDir, "m0.bin"));
const history = await openProjectHistory({
projectDir,
historyRoot: tempDir("hf-first-copy-root-"),
firstCopyFileMs: 500,
});
closes.push(() => history.close());
const askedAt = Date.now();
await history.beginWindow(agent, "Step");
expect(Date.now() - askedAt).toBeLessThan(1500);
});

it("holds an agent step for media that copies slowly, each file within its deadline", async () => {
const projectDir = mediaProject(3);
const delayed = new Set([0, 1, 2].map((i) => join(projectDir, `m${i}.bin`)));
vi.mocked(fsp.open).mockImplementation(async (...args: Parameters<typeof realOpen>) => {
if (delayed.delete(String(args[0]))) await new Promise((later) => setTimeout(later, 300));
return realOpen(...args);
});
const history = await openProjectHistory({
projectDir,
historyRoot: tempDir("hf-first-copy-root-"),
firstCopyFileMs: 600,
});
closes.push(() => history.close());
await history.beginWindow(agent, "Step");
expect(Object.keys(history.peek(START) ?? {}).sort()).toEqual(
["index.html", "m0.bin", "m1.bin", "m2.bin"].sort(),
);
});

it("keeps the original as the start of a late copy when a save writes it back over a later change", async () => {
const projectDir = projectOf(1, "late");
const p0 = join(projectDir, "parts", "p0.js");
Expand Down
1 change: 1 addition & 0 deletions packages/studio-server/src/history/historyLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface HistoryEntry {
/** Set on a restore: the point (an entry id, or START) the files were made equal to. */
restoredTo?: string;
keptInPlace?: Record<string, string>;
wentAheadOf?: string[];
}

/** The point before the first kept entry. */
Expand Down
13 changes: 11 additions & 2 deletions packages/studio-server/src/history/projectHistory.saving.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,16 @@ describe("a file still being written when the history first opens", () => {
changing.set("media.txt", 4);
const history = await openProjectHistory({ projectDir, historyRoot });
cleanup.push(() => history.close());
expect(await history.claim(you, "Edited", ["media.txt"])).toBeNull();
expect(history.peek(START)).not.toHaveProperty("media.txt");
const edit = await history.claim(you, "Edited", ["media.txt"]);
// The start names the file, as a version not kept until its copy lands.
expect(history.peek(START)!["media.txt"]).toMatch(/^[0-9a-f]{64}$/);
expect(history.peek(START)!["media.txt"]).not.toBe(
hashOfVersion(fileContentVersion("recorded")),
);
// The claim goes ahead of the copy, so its entry refuses an undo until the copy is saved.
await expect(history.undo(edit!.id, { who: you })).rejects.toThrow(
"The project's starting copy is still being saved",
);
expect(readFileSync(join(projectDir, "media.txt"), "utf-8")).toBe("recorded");
});
});
Loading
Loading