/** * Declared homes under `.debug/`. Every debug tool writes through one of these, * and `node scripts/debug/check.mjs` refuses any other entry at the top of `.debug/`. * * tmp/ one-off session scratch, owned and collected by `scripts/debug/tmp.mjs` * work/ cross-session work orders, owned by `scripts/debug/work.mjs` * cache/ rebuildable local tool caches * runs/ run outputs: logs, captures, reports, traces, isolated test runs * worktrees/ scratch git worktrees, owned by `scripts/debug/worktree.mjs` */ import { execFileSync } from 'node:child_process' import path from 'node:path' export const REPO_ROOT = path.resolve(import.meta.dirname, '..', '..') export const DEBUG_HOMES = ['tmp', 'work', 'cache', 'runs', 'worktrees'] /** * Resolve the main repository tree so linked worktrees share `.debug/`. * Falls back to `root` when executed outside a git repository (e.g. isolated tests). */ export function resolveStorageRoot(root = REPO_ROOT) { const resolved = path.resolve(root) try { const common = execFileSync( 'git', ['rev-parse', '--path-format=absolute', '--git-common-dir'], { cwd: resolved, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }, ).trim() if (common !== '') return path.dirname(path.resolve(resolved, common)) } catch { // Non-git directories (e.g. synthetic test sandboxes) use the directory directly. } return resolved } export const debugPath = (root, ...parts) => path.join(root, '.debug', ...parts) export const tmpPath = (root, ...parts) => debugPath(root, 'tmp', ...parts) export const workPath = (root, ...parts) => debugPath(root, 'work', ...parts) export const cachePath = (root, ...parts) => debugPath(root, 'cache', ...parts) export const runsPath = (root, ...parts) => debugPath(root, 'runs', ...parts) export const worktreesPath = (root, ...parts) => debugPath(root, 'worktrees', ...parts)