Skip to content

Commit 00ac4ef

Browse files
feat(studio-server): list the projects the history cache knows, with their folders (#4639)
1 parent f39b179 commit 00ac4ef

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import { describe, expect, it, onTestFinished, vi } from "vitest";
1313
import { ID_PATH, readId } from "./historyId.js";
1414
import { takeHistoryOwnership } from "./ownerLock.js";
1515
import { openProjectHistory } from "./projectHistory.js";
16-
import { KEEP_GONE_PROJECT_HISTORY_MS, pruneGoneProjectHistories } from "./pruneHistories.js";
16+
import {
17+
KEEP_GONE_PROJECT_HISTORY_MS,
18+
listProjectHistories,
19+
pruneGoneProjectHistories,
20+
} from "./pruneHistories.js";
1721

1822
vi.mock("./ownerLock.js", async (importOriginal) => {
1923
const actual = await importOriginal<typeof import("./ownerLock.js")>();
@@ -280,3 +284,27 @@ describe("pruneGoneProjectHistories", () => {
280284
expect(existsSync(join(historyRoot, unreadable.id))).toBe(true);
281285
});
282286
});
287+
288+
describe("listProjectHistories", () => {
289+
it("lists only histories whose project is still in its folder, and skips other folders", async () => {
290+
const historyRoot = tempDir("hf-prune-root-");
291+
const live = await projectWithHistory(historyRoot);
292+
const gone = await projectWithHistory(historyRoot);
293+
const replaced = await projectWithHistory(historyRoot);
294+
await Promise.all([live, gone, replaced].map(({ history }) => history.close()));
295+
rmSync(gone.projectDir, { recursive: true, force: true });
296+
writeFileSync(join(replaced.projectDir, ID_PATH), "00000000-0000-4000-8000-000000000000\n");
297+
mkdirSync(join(historyRoot, "backup"));
298+
299+
const listed = listProjectHistories(historyRoot);
300+
expect(listed.map(({ id, projectDir }) => ({ id, projectDir }))).toEqual([
301+
{ id: live.id, projectDir: live.projectDir },
302+
]);
303+
expect(listed[0]!.lastUsedMs).toBeGreaterThan(0);
304+
expect(existsSync(join(historyRoot, gone.id))).toBe(true);
305+
});
306+
307+
it("lists nothing for a history root that does not exist", () => {
308+
expect(listProjectHistories(join(tmpdir(), "hf-no-such-history-root"))).toEqual([]);
309+
});
310+
});

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ interface PruneOptions {
2525
budgetMs?: number;
2626
}
2727

28+
export interface ProjectHistoryRecord {
29+
id: string;
30+
projectDir: string;
31+
lastUsedMs: number;
32+
}
33+
34+
/** Every history whose project is still in the folder it was recorded for; reads only. */
35+
export function listProjectHistories(historyRoot: string): ProjectHistoryRecord[] {
36+
return namesIn(historyRoot)
37+
.filter(isHistoryId)
38+
.flatMap((id) => {
39+
const home = join(historyRoot, id);
40+
const projectDir = readRecord(home)?.dir;
41+
if (typeof projectDir !== "string" || readId(projectDir) !== id) return [];
42+
return [{ id, projectDir, lastUsedMs: lastUsed(home) }];
43+
});
44+
}
45+
46+
function namesIn(historyRoot: string): string[] {
47+
try {
48+
return readdirSync(historyRoot);
49+
} catch (error) {
50+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
51+
throw error;
52+
}
53+
}
54+
2855
export interface PrunedHistory {
2956
id: string;
3057
projectDir: string;
@@ -51,13 +78,7 @@ async function pruneWithin(
5178
}: PruneOptions & { measure?: boolean },
5279
): Promise<{ pruned: PrunedHistory[]; finished: boolean }> {
5380
const started = performance.now();
54-
let names: string[];
55-
try {
56-
names = readdirSync(historyRoot);
57-
} catch (error) {
58-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return { pruned: [], finished: true };
59-
throw error;
60-
}
81+
const names = namesIn(historyRoot);
6182
const pruned: PrunedHistory[] = [];
6283
for (const name of names) {
6384
if (performance.now() - started >= budgetMs) return { pruned, finished: false };

‎packages/studio-server/src/index.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ export {
1919
export { HistoryBusyError } from "./history/ownerLock.js";
2020
export { HistoryIdError } from "./history/historyId.js";
2121
export { historyCache } from "./history/historyCache.js";
22-
export { pruneGoneProjectHistories, type PrunedHistory } from "./history/pruneHistories.js";
22+
export {
23+
listProjectHistories,
24+
pruneGoneProjectHistories,
25+
type ProjectHistoryRecord,
26+
type PrunedHistory,
27+
} from "./history/pruneHistories.js";
2328
export {
2429
START as HISTORY_START,
2530
type HistoryEntry,

0 commit comments

Comments
 (0)