Files
orca/config/scripts/test-runtime/module-cache/vitest-local-module-cache.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

50 lines
1.9 KiB
JavaScript

import { mkdirSync, readdirSync, rmSync, statSync, utimesSync } from 'node:fs'
import { join } from 'node:path'
// A run that started under an older policy may still be reading its directory.
const STALE_AFTER_MS = 30 * 60_000
export function localModuleCacheParent(root) {
return join(root, 'node_modules', '.cache', 'orca-vitest-modules')
}
/** Retire directories from earlier policies so one worktree holds a single cache. */
export function pruneLocalModuleCaches(parent, keep, now = Date.now()) {
let names
try {
names = readdirSync(parent)
} catch {
return
}
for (const name of names) {
const path = join(parent, name)
try {
if (name !== keep && now - statSync(path).mtimeMs > STALE_AFTER_MS) {
rmSync(path, { recursive: true, force: true, maxRetries: 3 })
}
} catch {}
}
}
/**
* Persist Vitest transforms between local runs. The policy hash covers what Vitest's
* per-module key misses (lockfile, tsconfig, package.json, runner scripts), so any such
* change starts a fresh directory. CI keeps its own shard-keyed cache.
*/
export async function localModuleCacheEnv(root, env = process.env) {
if (env.CI || env.ORCA_UNIT_MODULE_CACHE_PATH || env.ORCA_LOCAL_MODULE_CACHE === '0') {
return {}
}
const parent = localModuleCacheParent(root)
// Lazy: the policy hash loads TypeScript sources that reclaim, which only needs the parent, avoids.
const { moduleCachePolicyHash } = await import('../../ci/unit-cache/ci-unit-module-cache.mjs')
const key = `${(await moduleCachePolicyHash(root)).slice(0, 16)}-node${process.versions.node}`
pruneLocalModuleCaches(parent, key)
const path = join(parent, key)
// Reclaim (reclaim-vitest-module-caches.mjs) reads this mtime as the last run.
mkdirSync(path, { recursive: true })
const now = new Date()
utimesSync(path, now, now)
return { ORCA_UNIT_MODULE_CACHE_PATH: path }
}