docs: prune obsolete docs and enforce declarative doc hygiene
This commit is contained in:
1 parent
bc11fd8ac6
commit
53627e5e1a
18 files changed
+414
-1359
No files matched your search
+124
-11
@@ -1,18 +1,139 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* check.mjs — enforce `.debug/` workspace hygiene:
|
||||
* check.mjs — enforce `.debug/` workspace and durable documentation 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/<id>` or `.debug/tmp/<session>` references in durable docs
|
||||
* 5. Reject broken relative links/anchors and `- **Title**: description` list slogans 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'
|
||||
import { collectDurableDocs, createWorkStore, isInside } 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
|
||||
const BOLD_COLON_BULLET = /^\s*(?:[-*+]|\d+\.)\s+\*\*[^*\r\n]+\*\*\s*[::]/
|
||||
const EXTERNAL_TARGET = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i
|
||||
|
||||
function stripFencedBlocks(markdown) {
|
||||
const lines = markdown.replaceAll('\r\n', '\n').split('\n')
|
||||
let fence = null
|
||||
return lines.map((line) => {
|
||||
const match = line.match(/^\s*(`{3,}|~{3,})/)
|
||||
if (match) {
|
||||
const marker = match[1]
|
||||
if (fence === null) {
|
||||
fence = marker[0]
|
||||
return ''
|
||||
}
|
||||
if (marker[0] === fence) {
|
||||
fence = null
|
||||
return ''
|
||||
}
|
||||
}
|
||||
return fence === null ? line : ''
|
||||
})
|
||||
}
|
||||
|
||||
function collectAnchors(markdown) {
|
||||
const anchors = new Set()
|
||||
const counts = new Map()
|
||||
for (const m of markdown.matchAll(/\bid=["']([^"']+)["']/g)) {
|
||||
anchors.add(m[1])
|
||||
}
|
||||
for (const line of stripFencedBlocks(markdown)) {
|
||||
const h = line.match(/^#{1,6}\s+(.+?)\s*#*\s*$/)
|
||||
if (!h) continue
|
||||
const plain = h[1]
|
||||
.replace(/<[^>]+>/g, '')
|
||||
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
|
||||
.replace(/`([^`]+)`/g, '$1')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^\p{L}\p{N}\s_-]/gu, '')
|
||||
.trim()
|
||||
.replace(/\s+/g, '-')
|
||||
if (!plain) continue
|
||||
const seen = counts.get(plain) ?? 0
|
||||
counts.set(plain, seen + 1)
|
||||
anchors.add(seen === 0 ? plain : `${plain}-${seen}`)
|
||||
}
|
||||
return anchors
|
||||
}
|
||||
|
||||
function extractRelativeTargets(lines) {
|
||||
const targets = []
|
||||
for (let i = 0; i < lines.length; i += 1) {
|
||||
const withoutCode = lines[i].replace(/`[^`]*`/g, '')
|
||||
for (const m of withoutCode.matchAll(/!?\[[^\]]*\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g)) {
|
||||
targets.push({ line: i + 1, raw: m[1] })
|
||||
}
|
||||
for (const m of withoutCode.matchAll(/\b(?:href|src|srcset)=["']([^"']+)["']/g)) {
|
||||
const first = m[1].split(',')[0].trim().split(/\s+/)[0]
|
||||
if (first) targets.push({ line: i + 1, raw: first })
|
||||
}
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
function checkDocHygiene(storage, problems) {
|
||||
const anchorCache = new Map()
|
||||
const getAnchors = (absPath) => {
|
||||
if (!anchorCache.has(absPath)) {
|
||||
anchorCache.set(absPath, collectAnchors(fs.readFileSync(absPath, 'utf8')))
|
||||
}
|
||||
return anchorCache.get(absPath)
|
||||
}
|
||||
|
||||
const durable = collectDurableDocs(storage)
|
||||
const linkDocs = [...durable]
|
||||
if (fs.existsSync(path.join(storage, 'NOTICE.md')) && !linkDocs.includes('NOTICE.md')) {
|
||||
linkDocs.push('NOTICE.md')
|
||||
}
|
||||
|
||||
for (const file of linkDocs) {
|
||||
const full = path.join(storage, file)
|
||||
if (!fs.existsSync(full)) continue
|
||||
const text = fs.readFileSync(full, 'utf8')
|
||||
const lines = stripFencedBlocks(text)
|
||||
|
||||
if (durable.includes(file)) {
|
||||
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`)
|
||||
}
|
||||
for (let i = 0; i < lines.length; i += 1) {
|
||||
if (BOLD_COLON_BULLET.test(lines[i])) {
|
||||
problems.push(`${file}:${i + 1}: uses "- **Title**: description" list style — write a plain declarative sentence instead`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const { line, raw } of extractRelativeTargets(lines)) {
|
||||
if (!raw || EXTERNAL_TARGET.test(raw)) continue
|
||||
const [rawPath, rawFrag = ''] = raw.split('#')
|
||||
const decodedPath = decodeURIComponent(rawPath)
|
||||
const targetPath = decodedPath === '' ? full : path.resolve(path.dirname(full), decodedPath)
|
||||
if (!isInside(storage, targetPath)) {
|
||||
problems.push(`${file}:${line}: relative link "${raw}" leaves repository root`)
|
||||
continue
|
||||
}
|
||||
if (!fs.existsSync(targetPath)) {
|
||||
problems.push(`${file}:${line}: broken relative link "${raw}"`)
|
||||
continue
|
||||
}
|
||||
if (rawFrag !== '' && targetPath.toLowerCase().endsWith('.md')) {
|
||||
const frag = decodeURIComponent(rawFrag).toLowerCase()
|
||||
const anchors = getAnchors(targetPath)
|
||||
if (!anchors.has(frag) && !anchors.has(decodeURIComponent(rawFrag))) {
|
||||
problems.push(`${file}:${line}: broken heading anchor "#${rawFrag}" in "${raw}"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function checkDebugHygiene(root = resolveStorageRoot()) {
|
||||
const storage = path.resolve(root)
|
||||
@@ -36,15 +157,7 @@ export function checkDebugHygiene(root = resolveStorageRoot()) {
|
||||
}
|
||||
}
|
||||
|
||||
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`)
|
||||
}
|
||||
}
|
||||
checkDocHygiene(storage, problems)
|
||||
|
||||
return { problems, warnings, orderCount: workResult.count }
|
||||
}
|
||||
|
||||
@@ -139,6 +139,13 @@ test('checkDebugHygiene: rejects stray .debug entries and concrete debug citatio
|
||||
fs.writeFileSync(path.join(sandbox, 'README.md'), 'See .debug/work/20261129-sample-task for details.\n')
|
||||
const docRes = checkDebugHygiene(sandbox)
|
||||
assert.ok(docRes.problems.some((p) => p.includes('durable documents never reference concrete .debug/ instances')))
|
||||
|
||||
// Broken local relative link and "- **Title**: description" slogan bullet must fail.
|
||||
fs.writeFileSync(path.join(sandbox, 'README.md'), '# Title\n\n- **Slogan**: prose after colon.\n\nSee [missing](./docs/nope.md) and [bad anchor](#missing).\n')
|
||||
const rotRes = checkDebugHygiene(sandbox)
|
||||
assert.ok(rotRes.problems.some((p) => p.includes('plain declarative sentence')))
|
||||
assert.ok(rotRes.problems.some((p) => p.includes('broken relative link "./docs/nope.md"')))
|
||||
assert.ok(rotRes.problems.some((p) => p.includes('broken heading anchor "#missing"')))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user