#!/usr/bin/env node /** * check.mjs — enforce `.debug/` workspace hygiene: * 1. Validate `.debug/work/` work orders (passes cleanly when absent, e.g. in CI) * 2. Validate `.debug/tmp/` session ownership records (passes cleanly when absent) * 3. Reject stray top-level entries under `.debug/` outside DEBUG_HOMES * 4. Reject concrete `.debug/work/` or `.debug/tmp/` references in durable docs */ import fs from 'node:fs' import path from 'node:path' import { DEBUG_HOMES, debugPath, resolveStorageRoot } from './paths.mjs' import { createTemporaryFiles } from './temporary-files.mjs' import { collectDurableDocs, createWorkStore } from './work-store.mjs' const CONCRETE_DEBUG_REF = /\.debug\/(?:work\/\d{8}-[a-z0-9]+(?:-[a-z0-9]+)*|tmp\/[a-z0-9-]+-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i export function checkDebugHygiene(root = resolveStorageRoot()) { const storage = path.resolve(root) const problems = [] const warnings = [] const workResult = createWorkStore(storage).checkWorkOrders() const tmpResult = createTemporaryFiles(storage).check() problems.push(...workResult.problems, ...tmpResult.problems) warnings.push(...workResult.warnings, ...tmpResult.warnings) const debugDir = debugPath(storage) if (fs.existsSync(debugDir)) { const strays = fs.readdirSync(debugDir).filter((name) => !DEBUG_HOMES.includes(name)) if (strays.length > 0) { problems.push( `.debug: ${strays.length} entry/entries outside declared homes (${DEBUG_HOMES.join(', ')}): ` + `${strays.slice(0, 5).join(', ')}${strays.length > 5 ? ', …' : ''} — ` + 'use `node scripts/debug/tmp.mjs` for scratch or `scripts/debug/paths.mjs` for tool outputs', ) } } for (const file of collectDurableDocs(storage)) { const full = path.join(storage, file) if (!fs.existsSync(full)) continue const text = fs.readFileSync(full, 'utf8') const match = text.match(CONCRETE_DEBUG_REF) if (match !== null) { problems.push(`${file}: cites temporary debug instance "${match[0]}" — durable documents never reference concrete .debug/ instances`) } } return { problems, warnings, orderCount: workResult.count } } if (path.resolve(process.argv[1] ?? '') === path.resolve(import.meta.dirname, 'check.mjs')) { const { problems, warnings, orderCount } = checkDebugHygiene() for (const warning of warnings) console.warn(`debug-check: ${warning}`) for (const problem of problems) console.error(`debug-check: ${problem}`) if (problems.length > 0) { console.error(`debug-check: ${problems.length} problem(s) found`) process.exitCode = 1 } else { console.log(`debug-check: clean (${orderCount} work order(s), homes: ${DEBUG_HOMES.join(', ')})`) } }