#!/usr/bin/env bun /** * Governance checks for the root AGENTS.md (the developer-level contract). * * Scope: only mechanically decidable rules — document structure, the required * section set, and reference integrity for the .agents/rules/ registry. * Deliberately excluded: prose style, tone, wording (subjective, noisy). * * Invoked as `bun run verify:agents-md` (CI + pre-commit) and with `--soft` * from the ZCode SessionStart hook, which emits hook-safe JSON and never * blocks the session. */ import { readdirSync, readFileSync } from "node:fs" import { join } from "node:path" import { checkHeadingHierarchy, checkHygiene, checkSingleLeadingH1, extractHeadings, type DocIssue } from "./doc-md-common.js" export const AGENTS_DOC_FILE = "AGENTS.md" /** * Load-bearing H2 sections of the developer contract. Deleting one must be a * conscious edit to this list, not an accident of a large refactor or merge. */ export const REQUIRED_H2_SECTIONS = [ "Project Structure & Module Organization", "Build, Test, and Development Commands", "Coding Style & Naming Conventions", "CLI & Agent Harness Protocol Guidelines", "Testing Guidelines", "Commit & Pull Request Guidelines", "Release & Publishing Guidelines", "Security & Configuration Tips", "Agent-Specific Instructions" ] /** Backticked registry references only: `.agents/rules/.md`. */ const RULES_REFERENCE = /`(\.agents\/rules\/[A-Za-z0-9._-]+\.md)`/g export function validateAgentsDoc(content: string, existingRuleFiles: ReadonlySet): DocIssue[] { const file = AGENTS_DOC_FILE const issues: DocIssue[] = [] const headings = extractHeadings(content) issues.push(...checkSingleLeadingH1(headings, file)) issues.push(...checkHeadingHierarchy(headings, file)) const h2Texts = new Set(headings.filter((heading) => heading.level === 2).map((heading) => heading.text)) for (const required of REQUIRED_H2_SECTIONS) { if (!h2Texts.has(required)) { issues.push({ file, rule: "required-section", message: `missing required H2 section "${required}"` }) } } for (const match of content.matchAll(RULES_REFERENCE)) { const ruleFile = match[1]!.split("/").pop()! if (!existingRuleFiles.has(ruleFile)) { issues.push({ file, rule: "rules-reference", message: `referenced rules file ${match[1]} does not exist in .agents/rules/` }) } } issues.push(...checkHygiene(content, file)) return issues } function main(): void { const root = join(import.meta.dirname, "..") const content = readFileSync(join(root, AGENTS_DOC_FILE), "utf8") const ruleFiles = new Set( readdirSync(join(root, ".agents", "rules"), { withFileTypes: true }) .filter((entry) => entry.isFile() && entry.name.endsWith(".md")) .map((entry) => entry.name) ) const issues = validateAgentsDoc(content, ruleFiles) if (process.argv.includes("--soft")) { // SessionStart hook mode: empty stdout passes silently; the strict hook // schema accepts a single additionalContext key. Never block the session. if (issues.length > 0) { const summary = issues.map((issue) => `${issue.rule}: ${issue.message}`).join("; ") console.log( JSON.stringify({ additionalContext: `AGENTS.md 治理校验未通过(${issues.length} 项):${summary}。本地运行 bun run verify:agents-md 查看详情。` }) ) } return } if (issues.length > 0) { for (const issue of issues) { const at = issue.line === undefined ? "" : `:${issue.line}` console.error(`${issue.file}${at} [${issue.rule}] ${issue.message}`) } console.error(`AGENTS.md governance checks failed (${issues.length} issue(s)).`) process.exitCode = 1 return } console.log("AGENTS.md governance checks passed.") } if (import.meta.main) main()