128 lines
4.5 KiB
TypeScript
128 lines
4.5 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: the routing-hub charter as hard numbers (size budget) and routing
|
|
* integrity (every reference file on disk is routed; every routed reference
|
|
* exists on disk). Not checked here: wording quality, table formatting.
|
|
*
|
|
* 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,
|
|
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"
|
|
|
|
/**
|
|
* The charter says SKILL.md is a lightweight routing hub, never a feature
|
|
* billboard. These budgets turn that into hard numbers; raising them must be
|
|
* a deliberate, reviewable diff, not silent growth.
|
|
*/
|
|
export const SKILL_MAX_LINES = 200
|
|
export const SKILL_MAX_H2_SECTIONS = 12
|
|
|
|
/** 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 lineCount = content.split("\n").length
|
|
if (lineCount > SKILL_MAX_LINES) {
|
|
issues.push({
|
|
file,
|
|
rule: "skill-budget-lines",
|
|
message: `SKILL.md is ${lineCount} lines (budget ${SKILL_MAX_LINES}); move detail into references/ per the routing-hub charter`
|
|
})
|
|
}
|
|
const h2Count = headings.filter((heading) => heading.level === 2).length
|
|
if (h2Count > SKILL_MAX_H2_SECTIONS) {
|
|
issues.push({
|
|
file,
|
|
rule: "skill-budget-h2",
|
|
message: `SKILL.md has ${h2Count} H2 sections (budget ${SKILL_MAX_H2_SECTIONS}); consolidate before adding more`
|
|
})
|
|
}
|
|
|
|
const diskReferences = new Set(referenceFiles)
|
|
const mentioned = new Set<string>()
|
|
for (const match of content.matchAll(REFERENCE_MENTION)) {
|
|
mentioned.add(match[1]!)
|
|
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.
|
|
if (issues.length > 0) {
|
|
const summary = issues.map((issue) => `${issue.rule}: ${issue.message}`).join("; ")
|
|
console.log(
|
|
JSON.stringify({
|
|
additionalContext: `SKILL.md 路由中枢校验未通过(${issues.length} 项):${summary}。本地运行 bun run verify:skill-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(`SKILL.md routing-hub checks failed (${issues.length} issue(s)).`)
|
|
process.exitCode = 1
|
|
return
|
|
}
|
|
console.log("SKILL.md routing-hub checks passed.")
|
|
}
|
|
|
|
if (import.meta.main) main()
|