import { terminalLines, terminalText } from "@hyperframes/core"; import { existsSync, readFileSync, realpathSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { isAbsolute, join, relative, resolve } from "node:path"; import { defineCommand } from "../utils/defineCommand.js"; import { EXTRACT_CACHE_MIN_AGE_MS, directorySizeBytes, gcExtractionCache, resolveExtractCacheDir, } from "@hyperframes/engine"; import { DEFAULT_HISTORY_ROOT, PROXY_CACHE_DIR_NAME, cleanupProxyCache, listProjectHistories, pruneGoneProjectHistories, reclaimUncopiedBlobs, type ProjectHistoryRecord, } from "@hyperframes/studio-server"; import type { Example } from "./_examples.js"; import { c } from "../ui/colors.js"; import { formatBytes } from "../ui/format.js"; import { setCommandExitCode } from "../utils/commandResult.js"; import { loadProducer } from "../utils/producer.js"; import { redactHome } from "./doctor.js"; import { withMeta } from "../utils/updateCheck.js"; import { resolveProject } from "../utils/project.js"; export const examples: Example[] = [ ["See what this project left on disk, remove nothing", "hyperframes clean --dry-run"], ["Remove it", "hyperframes clean"], ["Also remove snapshots and saved check reports", "hyperframes clean --snapshots"], [ "Also sweep machine-wide caches (other projects, temp renders, frame cache, history)", "hyperframes clean --global --dry-run", ], ["For agents", "hyperframes clean --json"], ]; /** A render dir with no owner record (made before owners were recorded) counts as abandoned after this long idle. */ const OWNERLESS_IDLE_MS = 6 * 60 * 60 * 1000; /** A video proxy nobody has used for this long is regenerable; a Studio still using one keeps touching it. */ const PROXY_IDLE_MS = 60 * 60 * 1000; export type CleanScope = "project" | "global"; export interface Leftover { what: string; path: string; bytes: number; scope: CleanScope; } export interface CleanOptions { projectDir: string; dryRun: boolean; snapshots: boolean; global?: boolean; /** Where to look; each defaults to the real location (tests point them at scratch dirs). */ historyRoot?: string; tempDir?: string; debugDir?: string; } export interface CleanResult { removed: Leftover[]; alsoReclaimable: Leftover[]; errors: string[]; } interface Sweep { dryRun: boolean; result: CleanResult; } function recordError(sweep: Sweep, error: unknown): void { sweep.result.errors.push(error instanceof Error ? error.message : String(error)); } /** Runs one kind of sweep; a failure is reported and the rest of the clean goes on. */ async function attempt(sweep: Sweep, step: () => unknown): Promise { try { await step(); } catch (error) { recordError(sweep, error); } } /** A HyperFrames project: its index.html holds a composition root, not just any website. */ function isProject(dir: string): boolean { try { return readFileSync(join(dir, "index.html"), "utf-8").includes("data-composition-id"); } catch { return false; } } function realDir(dir: string): string { try { return realpathSync(dir); } catch { return resolve(dir); } } function knownProjectHistories(sweep: Sweep, historyRoot: string): ProjectHistoryRecord[] { try { return listProjectHistories(historyRoot); } catch (error) { recordError(sweep, error); return []; } } function drop(sweep: Sweep, what: string, path: string, scope: CleanScope): void { const bytes = directorySizeBytes(path); try { if (!sweep.dryRun) rmSync(path, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); sweep.result.removed.push({ what, path, bytes, scope }); } catch (error) { recordError(sweep, error); } } function sweepProxies(sweep: Sweep, project: string, scope: CleanScope): void { const proxies = join(project, PROXY_CACHE_DIR_NAME); const swept = cleanupProxyCache(proxies, { maxBytes: Number.POSITIVE_INFINITY, maxIdleMs: PROXY_IDLE_MS, minSweepIntervalMs: 0, dryRun: sweep.dryRun, }); if (swept.removed.length === 0) return; sweep.result.removed.push({ what: "Video proxies", path: proxies, bytes: swept.bytesBefore - swept.bytesAfter, scope, }); } const SNAPSHOT_FOLDERS = [ { what: "Snapshots", path: ["snapshots"] }, { what: "Snapshots", path: [".hyperframes", "artifacts", "snapshots"] }, { what: "Check reports", path: [".hyperframes", "artifacts", "checks"] }, ]; function staysUnderArtifacts(project: string, path: string, folder: readonly string[]): boolean { try { const allowed = join(realpathSync(project), ...folder); const target = realpathSync(path); const fromAllowed = relative(allowed, target); return fromAllowed === "" || (!fromAllowed.startsWith("..") && !isAbsolute(fromAllowed)); } catch { return false; } } function sweepSnapshots(sweep: Sweep, project: string, remove: boolean): void { for (const folder of SNAPSHOT_FOLDERS) { const path = join(project, ...folder.path); if (!existsSync(path) || !staysUnderArtifacts(project, path, folder.path)) continue; if (remove) drop(sweep, folder.what, path, "project"); else sweep.result.alsoReclaimable.push({ what: folder.what, path, bytes: directorySizeBytes(path), scope: "project", }); } } async function sweepGoneHistories( sweep: Sweep, historyRoot: string, tempDir: string, ): Promise { const pruned = await pruneGoneProjectHistories(historyRoot, { dryRun: sweep.dryRun, onError: (error) => recordError(sweep, error), tempDir, }); for (const history of pruned) { sweep.result.removed.push({ what: "Project history", path: join(historyRoot, history.id), bytes: history.bytes, scope: "global", }); } } async function sweepHistoryCopies( sweep: Sweep, historyRoot: string, ownIds: readonly string[], global: boolean, ): Promise { if (!global && ownIds.length === 0) return; const reclaimed = await reclaimUncopiedBlobs(historyRoot, { dryRun: sweep.dryRun, onError: (error) => recordError(sweep, error), ...(global ? {} : { ids: ownIds }), }); for (const history of reclaimed) { sweep.result.removed.push({ what: "Video and audio copies in project history", path: join(historyRoot, history.id, "blobs"), bytes: history.bytes, scope: ownIds.includes(history.id) ? "project" : "global", }); } } function sweepExtractCache(sweep: Sweep): void { const extractCache = resolveExtractCacheDir().dir; if (!extractCache) return; const swept = gcExtractionCache(extractCache, { maxBytes: 0, minAgeMs: EXTRACT_CACHE_MIN_AGE_MS, dryRun: sweep.dryRun, }); if (swept.evictedEntries + swept.agedPartialsRemoved === 0) return; sweep.result.removed.push({ what: "Extracted video frames", path: extractCache, bytes: swept.evictedBytes, scope: "global", }); } /** What HyperFrames left in the project, or machine-wide with `global`, that nothing running uses. */ export async function cleanLeftovers(options: CleanOptions): Promise { const historyRoot = options.historyRoot ?? DEFAULT_HISTORY_ROOT; const tempDir = options.tempDir ?? tmpdir(); const global = options.global ?? false; const { RENDER_JOB_DIR, listAbandonedRenderDirs, resolveRenderDebugDir } = await loadProducer(); const sweep: Sweep = { dryRun: options.dryRun, result: { removed: [], alsoReclaimable: [], errors: [] }, }; const dropAbandoned = (what: string, parent: string, scope: CleanScope, names?: RegExp) => { for (const dir of listAbandonedRenderDirs(parent, { ownerlessIdleMs: OWNERLESS_IDLE_MS, names, })) drop(sweep, what, dir, scope); }; const project = realDir(options.projectDir); const histories = knownProjectHistories(sweep, historyRoot); const ownHistoryIds = histories .filter((history) => realDir(history.projectDir) === project) .map((history) => history.id); const otherProjects = global ? new Set(histories.map((history) => realDir(history.projectDir)).filter((p) => p !== project)) : new Set(); dropAbandoned("Render leftovers", project, "project"); dropAbandoned("Render leftovers", join(project, "renders"), "project"); if (isProject(project)) { await attempt(sweep, () => sweepProxies(sweep, project, "project")); sweepSnapshots(sweep, project, options.snapshots); } await attempt(sweep, () => sweepHistoryCopies(sweep, historyRoot, ownHistoryIds, global)); if (!global) return sweep.result; for (const other of otherProjects) { dropAbandoned("Render leftovers", other, "global"); dropAbandoned("Render leftovers", join(other, "renders"), "global"); if (isProject(other)) await attempt(sweep, () => sweepProxies(sweep, other, "global")); } const temp = realDir(tempDir); if (temp !== project && !otherProjects.has(temp)) dropAbandoned("Render leftovers", temp, "global"); dropAbandoned( "Debug renders", options.debugDir ?? resolveRenderDebugDir(), "global", RENDER_JOB_DIR, ); await attempt(sweep, () => sweepGoneHistories(sweep, historyRoot, tempDir)); await attempt(sweep, () => sweepExtractCache(sweep)); return sweep.result; } function printTable(rows: Leftover[]): void { const whatWidth = Math.max(...rows.map((row) => row.what.length)); for (const row of rows) { console.log( ` ${row.scope.padEnd(7)} ${row.what.padEnd(whatWidth)} ${formatBytes(row.bytes).padStart(9)} ${c.dim(terminalText(row.path))}`, ); } } const total = (rows: Leftover[]) => rows.reduce((sum, row) => sum + row.bytes, 0); const redactPaths = (rows: Leftover[]) => rows.map((row) => ({ ...row, path: redactHome(row.path) })); function printReport(result: CleanResult, dryRun: boolean, global: boolean): void { if (result.removed.length === 0) { console.log(`${c.success("◇")} Nothing to clean`); } else { console.log( `${c.success("◇")} ${dryRun ? "Would remove" : "Removed"} ${formatBytes(total(result.removed))}`, ); printTable(result.removed); } if (result.alsoReclaimable.length > 0) { console.log( `\n Also reclaimable with --snapshots: ${formatBytes(total(result.alsoReclaimable))}`, ); printTable(result.alsoReclaimable); } if (!global) { console.log( c.dim( "\n Only this project was searched. Add --global for other projects and machine-wide caches.", ), ); } for (const error of result.errors) console.log(c.warn(` ${terminalLines(error)}`)); } export default defineCommand({ meta: { name: "clean", description: "Find and remove what HyperFrames left in one project (render leftovers, video proxies, its history's media copies); --global also sweeps machine-wide caches", }, args: { dir: { type: "positional", description: "Project directory (default: the project holding the current folder); its subfolders are not searched", required: false, }, global: { type: "boolean", description: "Also sweep outside the project: other projects' render leftovers and proxies, temp and debug renders, the extracted-frame cache, gone projects' history", default: false, }, "dry-run": { type: "boolean", description: "List what would be removed, remove nothing", default: false, }, snapshots: { type: "boolean", description: "Also remove snapshots/ and .hyperframes/artifacts/{snapshots,checks}/ folders", default: false, }, json: { type: "boolean", description: "Output as JSON", default: false }, }, async run({ args }) { const dryRun = args["dry-run"]; const result = await cleanLeftovers({ projectDir: resolveProject(args.dir, { requireIndex: false, searchParents: true }).dir, dryRun, snapshots: args.snapshots, global: args.global, }); if (result.errors.length > 0) setCommandExitCode(1); if (args.json) { const report = withMeta({ dryRun, scope: args.global ? "global" : "project", removed: redactPaths(result.removed), alsoReclaimable: redactPaths(result.alsoReclaimable), errors: result.errors.map(redactHome), totalBytes: total(result.removed), }); console.log(JSON.stringify(report, null, 2)); return; } printReport(result, dryRun, args.global); }, });