Skip to content

Write HTML. Render video. Built for agents.

feat(studio-server): list the projects the history cache knows, with their folders (#4639)

Miguel Ángel00ac4ef571c6
diff --git a/packages/studio-server/src/history/pruneHistories.test.ts b/packages/studio-server/src/history/pruneHistories.test.ts
index 1e65e8f446..ccd83cce21 100644
--- a/packages/studio-server/src/history/pruneHistories.test.ts
+++ b/packages/studio-server/src/history/pruneHistories.test.ts
@@ -13,7 +13,11 @@ import { describe, expect, it, onTestFinished, vi } from "vitest";
import { ID_PATH, readId } from "./historyId.js";
import { takeHistoryOwnership } from "./ownerLock.js";
import { openProjectHistory } from "./projectHistory.js";
-import { KEEP_GONE_PROJECT_HISTORY_MS, pruneGoneProjectHistories } from "./pruneHistories.js";
+import {
+ KEEP_GONE_PROJECT_HISTORY_MS,
+ listProjectHistories,
+ pruneGoneProjectHistories,
+} from "./pruneHistories.js";
vi.mock("./ownerLock.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("./ownerLock.js")>();
@@ -280,3 +284,27 @@ describe("pruneGoneProjectHistories", () => {
expect(existsSync(join(historyRoot, unreadable.id))).toBe(true);
});
});
+
+describe("listProjectHistories", () => {
+ it("lists only histories whose project is still in its folder, and skips other folders", async () => {
+ const historyRoot = tempDir("hf-prune-root-");
+ const live = await projectWithHistory(historyRoot);
+ const gone = await projectWithHistory(historyRoot);
+ const replaced = await projectWithHistory(historyRoot);
+ await Promise.all([live, gone, replaced].map(({ history }) => history.close()));
+ rmSync(gone.projectDir, { recursive: true, force: true });
+ writeFileSync(join(replaced.projectDir, ID_PATH), "00000000-0000-4000-8000-000000000000\n");
+ mkdirSync(join(historyRoot, "backup"));
+
+ const listed = listProjectHistories(historyRoot);
+ expect(listed.map(({ id, projectDir }) => ({ id, projectDir }))).toEqual([
+ { id: live.id, projectDir: live.projectDir },
+ ]);
+ expect(listed[0]!.lastUsedMs).toBeGreaterThan(0);
+ expect(existsSync(join(historyRoot, gone.id))).toBe(true);
+ });
+
+ it("lists nothing for a history root that does not exist", () => {
+ expect(listProjectHistories(join(tmpdir(), "hf-no-such-history-root"))).toEqual([]);
+ });
+});
diff --git a/packages/studio-server/src/history/pruneHistories.ts b/packages/studio-server/src/history/pruneHistories.ts
index de2de44259..d00fbd7a4c 100644
--- a/packages/studio-server/src/history/pruneHistories.ts
+++ b/packages/studio-server/src/history/pruneHistories.ts
@@ -25,6 +25,33 @@ interface PruneOptions {
budgetMs?: number;
}
+export interface ProjectHistoryRecord {
+ id: string;
+ projectDir: string;
+ lastUsedMs: number;
+}
+
+/** Every history whose project is still in the folder it was recorded for; reads only. */
+export function listProjectHistories(historyRoot: string): ProjectHistoryRecord[] {
+ return namesIn(historyRoot)
+ .filter(isHistoryId)
+ .flatMap((id) => {
+ const home = join(historyRoot, id);
+ const projectDir = readRecord(home)?.dir;
+ if (typeof projectDir !== "string" || readId(projectDir) !== id) return [];
+ return [{ id, projectDir, lastUsedMs: lastUsed(home) }];
+ });
+}
+
+function namesIn(historyRoot: string): string[] {
+ try {
+ return readdirSync(historyRoot);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
+ throw error;
+ }
+}
+
export interface PrunedHistory {
id: string;
projectDir: string;
@@ -51,13 +78,7 @@ async function pruneWithin(
}: PruneOptions & { measure?: boolean },
): Promise<{ pruned: PrunedHistory[]; finished: boolean }> {
const started = performance.now();
- let names: string[];
- try {
- names = readdirSync(historyRoot);
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code === "ENOENT") return { pruned: [], finished: true };
- throw error;
- }
+ const names = namesIn(historyRoot);
const pruned: PrunedHistory[] = [];
for (const name of names) {
if (performance.now() - started >= budgetMs) return { pruned, finished: false };
diff --git a/packages/studio-server/src/index.ts b/packages/studio-server/src/index.ts
index 931e341e56..4de4906bdb 100644
--- a/packages/studio-server/src/index.ts
+++ b/packages/studio-server/src/index.ts
@@ -19,7 +19,12 @@ export {
export { HistoryBusyError } from "./history/ownerLock.js";
export { HistoryIdError } from "./history/historyId.js";
export { historyCache } from "./history/historyCache.js";
-export { pruneGoneProjectHistories, type PrunedHistory } from "./history/pruneHistories.js";
+export {
+ listProjectHistories,
+ pruneGoneProjectHistories,
+ type ProjectHistoryRecord,
+ type PrunedHistory,
+} from "./history/pruneHistories.js";
export {
START as HISTORY_START,
type HistoryEntry,