Files
orca/config/scripts/worktree-storage/reclaim-vite-caches.mjs
Neil 9bd6272fdf refactor(tooling): organize build, CI, and test owners (#38130)
* refactor(tooling): organize test runtime and maintenance owners

* test(tooling): preserve relocated runtime fixture dependency edges

* refactor(tooling): preserve xterm routing owner checks

* ci(tooling): include routing owner in sparse checkout

* test(tooling): preserve copied workflow dependency closures

* fix(tooling): normalize hashed JSON assets in release parity

* fix(tooling): distinguish asset aliases from literal references

* fix(tooling): frame normalized asset reference payloads

* fix(tooling): normalize decoded JSON asset references

* refactor(tooling): organize build, CI and release owners

* fix(tooling): align moved generator and workflow path contracts

* fix(tooling): update generated plugin bundle owner banners

* test(tooling): create relocated mobile verifier fixture directories

* test(tooling): copy headless detector dependency closure

* test(tooling): align native batch policy fixtures with owner path

* fix(tooling): link moved reliability command to historical evidence

* fix(tooling): preserve nested sandbox owners and canonical guide source

* fix(tooling): target release version owners in checkout fixture
2026-11-04 21:08:19 -08:00

132 lines
3.8 KiB
JavaScript

import { lstat, readdir, realpath, rm } from 'node:fs/promises'
import { join, resolve } from 'node:path'
import { localRepositoryWorktrees } from './local-repository-worktrees.mjs'
import { isProcessAlive } from '../development/remote-session-cleanup/remote-agent-session-process-cleanup.mjs'
import { parseReclaimArguments } from './reclaim-worktree-command.mjs'
import {
VITE_CACHE_HOST_ID,
VITE_CACHE_USAGE_DIRECTORY,
withViteCacheLock
} from './vite-cache-activity.mjs'
const OWNER = /^([\da-f]{64})-([1-9]\d*)-[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/
async function directory(path) {
try {
return (await lstat(path)).isDirectory()
} catch (error) {
if (error.code === 'ENOENT') {
return false
}
throw error
}
}
async function cacheDirectory(worktree) {
if (!(await directory(join(worktree, '.cache')))) {
return null
}
const root = join(worktree, '.cache', 'vite')
return (await directory(root)) ? root : null
}
async function unusedCache(cacheRoot, cutoff, isAlive) {
try {
const marker = join(cacheRoot, VITE_CACHE_USAGE_DIRECTORY)
const info = await lstat(marker)
if (!info.isDirectory() || info.mtimeMs > cutoff) {
return false
}
for (const name of await readdir(marker)) {
const match = OWNER.exec(name)
if (!match || match[1] !== VITE_CACHE_HOST_ID) {
return false
}
const pid = Number(match[2])
const info = await lstat(join(marker, name))
if (!Number.isSafeInteger(pid) || !info.isFile() || info.size !== 0 || isAlive(pid)) {
return false
}
}
return true
} catch (error) {
if (error.code === 'ENOENT') {
return false
}
throw error
}
}
export async function reclaimViteCaches({
repoRoot = process.cwd(),
apply = false,
excludeWorktree,
minAgeDays = 30,
nowMs = Date.now(),
worktrees,
isAlive = isProcessAlive,
report = console.log
} = {}) {
if (!Number.isFinite(minAgeDays) || minAgeDays < 0) {
throw new Error('Vite cache age must be a non-negative number')
}
const excluded = excludeWorktree ? await realpath(excludeWorktree) : null
const roots = worktrees ?? (await localRepositoryWorktrees(repoRoot)).worktrees
const result = { entries: 0, protected: 0 }
for (const worktree of new Set(roots)) {
if (!(await directory(worktree))) {
continue
}
const canonical = await realpath(worktree)
if (canonical === excluded) {
continue
}
const cacheRoot = await cacheDirectory(canonical)
if (!cacheRoot) {
continue
}
try {
await withViteCacheLock(
cacheRoot,
async (verify) => {
if (
(await cacheDirectory(canonical)) !== cacheRoot ||
!(await unusedCache(cacheRoot, nowMs - minAgeDays * 86_400_000, isAlive))
) {
result.protected += 1
return
}
verify()
if (apply) {
await rm(cacheRoot, { recursive: true, force: true })
}
result.entries += 1
report(`${apply ? 'removed' : 'would remove'} Vite cache ${JSON.stringify(cacheRoot)}`)
},
0
)
} catch (error) {
if (error.code !== 'ELOCKED') {
throw error
}
result.protected += 1
}
}
report(
`${apply ? 'Removed' : 'Would remove'} ${result.entries} Vite cache(s); retained ${result.protected} recent, active, or unverifiable cache(s).`
)
return result
}
if (process.argv[1] && resolve(process.argv[1]) === resolve(import.meta.filename)) {
const options = parseReclaimArguments(process.argv.slice(2))
if (options.help) {
console.log('Usage: pnpm reclaim-space [--apply] [--repo DIRECTORY]')
} else {
reclaimViteCaches(options).catch((error) => {
console.error(error.message)
process.exitCode = 1
})
}
}