import type { BriefFile } from "./review-brief.ts"; import { parseSpecsConfig, SpecsEngine, type SpecsConfig, type SpecsCheckReport, type SpecsCoverReport } from "./specs.ts"; import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "node:fs"; import { join } from "node:path"; import { realPath } from "./project.ts"; export interface SpecAck { id: string; reason: string } export interface SpecRef { id: string; path: string; title: string; reason?: string } export interface SpecTaskReport { head: string; complete: boolean; errors: number; unchanged: SpecRef[]; changed: SpecRef[]; truncated?: boolean; reason?: string; } export interface SpecsStatus { specCount: number; documents: number; errors: number; stale: number; truncated: boolean } const UNAVAILABLE = new Set(["discovery-unavailable", "snapshot-unavailable", "coverage-unavailable"]); /** Host-bound repository inspection only. Tasks owns caller/privacy projection and every board write. */ export class SpecTaskInspector { private readonly engines = new Map(); private readonly root: string; constructor(root: string, private readonly config: () => SpecsConfig) { this.root = realPath(root); } /** A positive absence only; unsafe or incomplete reads must not claim the block is missing. */ missingBlock(): boolean | undefined { const file = join(this.root, "AGENTS.md"); let fd: number | undefined; try { const before = lstatSync(file); if (!before.isFile() || before.isSymbolicLink() || before.size > 256 * 1024) return undefined; fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW); const opened = fstatSync(fd); if (opened.dev !== before.dev || opened.ino !== before.ino) return undefined; const bytes = Buffer.alloc(before.size + 1); let count = 0; while (count < bytes.length) { const got = readSync(fd, bytes, count, bytes.length - count, count); if (!got) break; count += got; } const text = bytes.subarray(0, count).toString("utf8"), after = lstatSync(file); if (after.dev !== before.dev || after.ino !== before.ino || after.size !== before.size || after.mtimeMs !== before.mtimeMs) return undefined; const begin = text.indexOf("