140 lines
5.0 KiB
TypeScript
140 lines
5.0 KiB
TypeScript
#!/usr/bin/env bun
|
|
/**
|
|
* Governance checks for skills/seal-use/SKILL.md (the user/agent-facing
|
|
* traffic-routing hub, per .agents/rules/skill-documentation.md).
|
|
*
|
|
* Scope: structure and routing integrity — every reference file on disk is
|
|
* routed from SKILL.md; every routed reference exists on disk. Size is
|
|
* deliberately NOT gated: where detail belongs (hub vs references/) is a
|
|
* review-time judgment, and an arbitrary numeric cap would erode the gate's
|
|
* credibility with false positives.
|
|
*
|
|
* Invoked as `bun run verify:skill-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, statSync } from "node:fs"
|
|
import { join } from "node:path"
|
|
import {
|
|
checkHeadingHierarchy,
|
|
checkHygiene,
|
|
checkSingleLeadingH1,
|
|
extractHeadings,
|
|
formatIssue,
|
|
partitionBySeverity,
|
|
type DocIssue
|
|
} from "./doc-md-common.js"
|
|
|
|
export const SKILL_DOC_RELATIVE_PATH = "skills/seal-use/SKILL.md"
|
|
export const REFERENCES_DIR_RELATIVE_PATH = "skills/seal-use/references"
|
|
export const REQUIRED_ROUTING_SECTION_TITLE = "参考文档路由"
|
|
|
|
/** Backticked skill-relative references only: `references/<name>.md`. */
|
|
const REFERENCE_MENTION = /`(references\/[A-Za-z0-9._-]+\.md)`/g
|
|
|
|
export function validateSkillDoc(content: string, referenceFiles: readonly string[]): DocIssue[] {
|
|
const file = SKILL_DOC_RELATIVE_PATH
|
|
const issues: DocIssue[] = []
|
|
|
|
const headings = extractHeadings(content)
|
|
issues.push(...checkSingleLeadingH1(headings, file))
|
|
issues.push(...checkHeadingHierarchy(headings, file))
|
|
|
|
const h2Headings = headings.filter((heading) => heading.level === 2)
|
|
const hasRoutingSection = h2Headings.some((heading) => heading.text === REQUIRED_ROUTING_SECTION_TITLE)
|
|
if (!hasRoutingSection) {
|
|
issues.push({
|
|
file,
|
|
rule: "skill-routing-section",
|
|
message: `missing required routing hub section "## ${REQUIRED_ROUTING_SECTION_TITLE}"`
|
|
})
|
|
} else {
|
|
const lastH2 = h2Headings[h2Headings.length - 1]!
|
|
if (lastH2.text !== REQUIRED_ROUTING_SECTION_TITLE) {
|
|
issues.push({
|
|
file,
|
|
line: lastH2.line,
|
|
rule: "skill-routing-terminal",
|
|
message: `"## ${REQUIRED_ROUTING_SECTION_TITLE}" must be the terminal H2 section of the routing hub, but found trailing H2 section "## ${lastH2.text}"`
|
|
})
|
|
}
|
|
}
|
|
|
|
const diskReferences = new Set(referenceFiles)
|
|
for (const match of content.matchAll(REFERENCE_MENTION)) {
|
|
if (!diskReferences.has(match[1]!)) {
|
|
issues.push({
|
|
file,
|
|
rule: "skill-reference-exists",
|
|
message: `routing points to ${match[1]} which does not exist on disk`
|
|
})
|
|
}
|
|
}
|
|
for (const reference of diskReferences) {
|
|
if (!content.includes(reference)) {
|
|
issues.push({
|
|
file,
|
|
rule: "skill-reference-routed",
|
|
message: `${reference} exists in references/ but is never routed from SKILL.md (undiscoverable per the charter)`
|
|
})
|
|
}
|
|
}
|
|
|
|
issues.push(...checkHygiene(content, file))
|
|
return issues
|
|
}
|
|
|
|
function main(): void {
|
|
const root = join(import.meta.dirname, "../..")
|
|
const content = readFileSync(join(root, SKILL_DOC_RELATIVE_PATH), "utf8")
|
|
const referencesDir = join(root, REFERENCES_DIR_RELATIVE_PATH)
|
|
const referenceFiles: string[] = []
|
|
for (const entry of readdirSync(referencesDir, { withFileTypes: true })) {
|
|
if (entry.isFile() && entry.name.endsWith(".md") && statSync(join(referencesDir, entry.name)).isFile()) {
|
|
referenceFiles.push(`references/${entry.name}`)
|
|
}
|
|
}
|
|
const issues = validateSkillDoc(content, referenceFiles)
|
|
|
|
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.
|
|
const { errors, warnings } = partitionBySeverity(issues)
|
|
const parts: string[] = []
|
|
if (errors.length > 0) {
|
|
parts.push(`❌ ${errors.length} 项错误:${errors.map((i) => `${i.rule}: ${i.message}`).join("; ")}`)
|
|
}
|
|
if (warnings.length > 0) {
|
|
parts.push(`⚠️ ${warnings.length} 项警告:${warnings.map((i) => `${i.rule}: ${i.message}`).join("; ")}`)
|
|
}
|
|
if (parts.length > 0) {
|
|
console.log(
|
|
JSON.stringify({
|
|
additionalContext: `SKILL.md 路由中枢校验:${parts.join("。")}。本地运行 bun run verify:skill-md 查看详情。`
|
|
})
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
const { errors, warnings } = partitionBySeverity(issues)
|
|
for (const warning of warnings) {
|
|
console.error(formatIssue(warning))
|
|
}
|
|
if (errors.length > 0) {
|
|
for (const error of errors) {
|
|
console.error(formatIssue(error))
|
|
}
|
|
console.error(`SKILL.md routing-hub checks failed (${errors.length} error(s), ${warnings.length} warning(s)).`)
|
|
process.exitCode = 1
|
|
return
|
|
}
|
|
if (warnings.length > 0) {
|
|
console.log(`SKILL.md routing-hub checks passed with ${warnings.length} warning(s).`)
|
|
return
|
|
}
|
|
console.log("SKILL.md routing-hub checks passed.")
|
|
}
|
|
|
|
if (import.meta.main) main()
|