Skip to content

Commit 276ac45

Browse files
committed
fix(studio-server): keep an older history whose drive may only be unmounted
1 parent f00f8d9 commit 276ac45

2 files changed

Lines changed: 55 additions & 10 deletions

File tree

‎packages/studio-server/src/history/pruneHistories.test.ts‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ import { tmpdir } from "node:os";
1111
import { dirname, join } from "node:path";
1212
import { describe, expect, it, onTestFinished, vi } from "vitest";
1313
import { ID_PATH, readId } from "./historyId.js";
14+
import { takeHistoryOwnership } from "./ownerLock.js";
1415
import { openProjectHistory } from "./projectHistory.js";
1516
import { KEEP_GONE_PROJECT_HISTORY_MS, pruneGoneProjectHistories } from "./pruneHistories.js";
1617

18+
vi.mock("./ownerLock.js", async (importOriginal) => {
19+
const actual = await importOriginal<typeof import("./ownerLock.js")>();
20+
return { ...actual, takeHistoryOwnership: vi.fn(actual.takeHistoryOwnership) };
21+
});
22+
1723
function tempDir(prefix: string): string {
1824
const dir = mkdtempSync(join(tmpdir(), prefix));
1925
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
@@ -92,8 +98,12 @@ describe("pruneGoneProjectHistories", () => {
9298
const elsewhere = { tempDir: historyRoot };
9399

94100
expect(await prunedIds(historyRoot, elsewhere)).toEqual([]);
95-
const later = Date.now() + KEEP_GONE_PROJECT_HISTORY_MS;
96-
expect(await prunedIds(historyRoot, { ...elsewhere, now: later })).toEqual([gone.id]);
101+
expect(await prunedIds(historyRoot, { ...elsewhere, now: Date.now() + 13 * DAY_MS })).toEqual(
102+
[],
103+
);
104+
expect(await prunedIds(historyRoot, { ...elsewhere, now: Date.now() + 14 * DAY_MS })).toEqual([
105+
gone.id,
106+
]);
97107
});
98108

99109
it("prunes when a history opens a day after the last full prune", async () => {
@@ -198,24 +208,58 @@ describe("pruneGoneProjectHistories", () => {
198208
]);
199209
});
200210

201-
it("keeps a history recorded without its disk once the project's parent is gone too", async () => {
211+
it("prunes a history recorded without its disk only when its folder is there without the project", async () => {
202212
const historyRoot = tempDir("hf-prune-root-");
213+
const emptied = await projectWithHistory(historyRoot);
214+
const folderGone = await projectWithHistory(historyRoot);
203215
const parentGone = await projectWithHistory(historyRoot, {
204216
projectDir: join(tempDir("hf-prune-parent-"), "project"),
205217
});
206-
const parentKept = await projectWithHistory(historyRoot);
207-
await Promise.all([parentGone.history.close(), parentKept.history.close()]);
208-
for (const { id } of [parentGone, parentKept])
209-
editRecord(historyRoot, id, ({ dev: _dev, ...record }) => record);
218+
const all = [emptied, folderGone, parentGone];
219+
await Promise.all(all.map(({ history }) => history.close()));
220+
for (const { id } of all) editRecord(historyRoot, id, ({ dev: _dev, ...record }) => record);
221+
rmSync(join(emptied.projectDir, ".hyperframes"), { recursive: true, force: true });
222+
// An unmounted disk leaves its mount point, so a missing folder under it may just be unplugged.
223+
rmSync(folderGone.projectDir, { recursive: true, force: true });
210224
rmSync(dirname(parentGone.projectDir), { recursive: true, force: true });
211-
rmSync(parentKept.projectDir, { recursive: true, force: true });
212225

213226
const later = Date.now() + KEEP_GONE_PROJECT_HISTORY_MS;
214227
expect(await prunedIds(historyRoot, { tempDir: historyRoot, now: later })).toEqual([
215-
parentKept.id,
228+
emptied.id,
216229
]);
217230
});
218231

232+
it("prunes a history recorded without its disk under the temp folder once its project is gone", async () => {
233+
const historyRoot = tempDir("hf-prune-root-");
234+
const scratch = await projectWithHistory(historyRoot, {
235+
projectDir: join(tempDir("hf-prune-scratch-"), "project"),
236+
});
237+
await scratch.history.close();
238+
editRecord(historyRoot, scratch.id, ({ dev: _dev, ...record }) => record);
239+
rmSync(dirname(scratch.projectDir), { recursive: true, force: true });
240+
241+
expect(await prunedIds(historyRoot)).toEqual([scratch.id]);
242+
});
243+
244+
it("keeps a history whose project comes back while the prune takes its lock", async () => {
245+
const historyRoot = tempDir("hf-prune-root-");
246+
const back = await projectWithHistory(historyRoot);
247+
await back.history.close();
248+
const idFile = join(back.projectDir, ID_PATH);
249+
const id = readFileSync(idFile, "utf-8");
250+
rmSync(dirname(idFile), { recursive: true, force: true });
251+
const real = await vi.importActual<typeof import("./ownerLock.js")>("./ownerLock.js");
252+
vi.mocked(takeHistoryOwnership).mockImplementationOnce(async (home, waitMs) => {
253+
mkdirSync(dirname(idFile), { recursive: true });
254+
writeFileSync(idFile, id);
255+
return real.takeHistoryOwnership(home, waitMs);
256+
});
257+
258+
expect(await prunedIds(historyRoot)).toEqual([]);
259+
expect(existsSync(idFile)).toBe(true);
260+
expect(existsSync(join(historyRoot, back.id))).toBe(true);
261+
});
262+
219263
it("removes the history of a folder that now holds another project", async () => {
220264
const historyRoot = tempDir("hf-prune-root-");
221265
const replaced = await projectWithHistory(historyRoot);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ function diskStillHere(dir: string, dev: unknown, tempDir: string): boolean {
158158
stat = statSync(at, { throwIfNoEntry: false });
159159
}
160160
if (typeof dev === "number") return stat.dev === dev;
161-
return at === dir || at === dirname(dir) || isSafePath(tempDir, at);
161+
// Recorded before `dev`: a missing folder may sit on an unmounted disk, so only an emptied folder or temp counts.
162+
return at === dir || isSafePath(tempDir, at);
162163
}
163164

164165
function lastUsed(home: string): number {

0 commit comments

Comments
 (0)