verify-agent-note-format.ts 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Enforce Agent Note headers, lifecycle-specific sections, alternatives, and retired
  3. * marker rules. Classification and filenames belong to the sibling tree gate;
  4. * translation structure belongs to the pairing gate. Exact format and
  5. * grandfathering rules live in `.agents/notes/README.md`.
  6. */
  7. import { readFileSync } from 'node:fs'
  8. import { resolve } from 'node:path'
  9. import { agentNoteRoot, walkAgentNoteTree } from './agent-note-tree.ts'
  10. /** The date these format rules took effect; the grandfather comment is valid only before it. */
  11. const FORMAT_ADOPTED = '2026-07-05'
  12. /** The exact comment a pre-format Agent Note carries in place of `## Alternatives considered`. */
  13. const GRANDFATHER = '<!-- agent-note-format: alternatives-not-recorded (pre-format Agent Note) -->'
  14. /** The retired debt marker that flagged pre-format bodies; banned so it cannot creep back. */
  15. const LEGACY_MARKERS = ['XXX: legacy ADR/RFC body format', 'XXX: legacy ADR/Agent Note body format']
  16. /** Status-line grammar per lifecycle folder. */
  17. const STATUS: Record<string, RegExp> = {
  18. proposed: /^Status: proposed$/,
  19. implemented: /^Status: implemented$/,
  20. rejected: /^Status: rejected — .+$/,
  21. }
  22. /** Required `##` headings per lifecycle, beyond the universal `## Problem` opener. */
  23. const REQUIRED: Record<string, string[]> = {
  24. proposed: ['## Proposal', '## Acceptance criteria', '## Risks'],
  25. implemented: ['## Decision', '## Consequences'],
  26. rejected: ['## Proposal'],
  27. }
  28. /** Headings banned in `implemented/` — proposal-era spec-speak per the slop checklist. */
  29. const BANNED_IMPLEMENTED = /^## (?:Proposal\b|Plan\b|Migration plan\b|Acceptance criteria\b)/i
  30. const { notes, errors } = walkAgentNoteTree()
  31. for (const note of notes) {
  32. const fail = (msg: string): void => {
  33. errors.push(`format: ${note.rel} — ${msg}`)
  34. }
  35. const lines = readFileSync(resolve(agentNoteRoot, note.rel), 'utf8').split('\n')
  36. // Format tokens inside fenced examples are not document structure.
  37. let inFence = false
  38. const prose = lines.filter((l) => {
  39. if (l.startsWith('```')) {
  40. inFence = !inFence
  41. return false
  42. }
  43. return !inFence
  44. })
  45. if (!/^# Agent Note: \S/.test(lines[0] ?? '')) fail('line 1 must be `# Agent Note: <title>`')
  46. if (lines[1] !== '') fail('line 2 must be blank')
  47. const status = STATUS[note.lifecycle]
  48. if (status !== undefined && !status.test(lines[2] ?? '')) {
  49. fail(`line 3 must match the ${note.lifecycle} status grammar (${String(status)})`)
  50. }
  51. if (lines[3] !== '') fail('line 4 must be blank')
  52. const statusLines = prose.filter(l => l.startsWith('Status:') && l !== lines[2])
  53. if (statusLines.length > 0 || prose.filter(l => l === lines[2]).length > 1) {
  54. fail('the line-3 `Status:` line must be the only one in the file')
  55. }
  56. const h2s = prose.filter(l => l.startsWith('## ')).map(l => l.trimEnd())
  57. if (h2s[0] !== '## Problem') fail(`the first section must be \`## Problem\` (got ${JSON.stringify(h2s[0] ?? '<none>')})`)
  58. for (const required of REQUIRED[note.lifecycle] ?? []) {
  59. if (!h2s.includes(required)) fail(`missing the required \`${required}\` section`)
  60. }
  61. if (note.lifecycle === 'implemented') {
  62. for (const h2 of h2s.filter(h => BANNED_IMPLEMENTED.test(h))) {
  63. fail(`\`${h2}\` is a proposal-era heading; an implemented Agent Note states what is (fold it into Decision/Consequences/Testing)`)
  64. }
  65. }
  66. const hasSection = h2s.includes('## Alternatives considered')
  67. const hasGrandfather = prose.includes(GRANDFATHER)
  68. if (hasSection && hasGrandfather) fail('carries both `## Alternatives considered` and the grandfather comment — drop the comment')
  69. if (!hasSection && !hasGrandfather) fail('missing `## Alternatives considered` (a pre-format Agent Note whose alternatives are not reconstructible carries the grandfather comment instead — see .agents/notes/README.md § The file format)')
  70. if (hasGrandfather && note.date >= FORMAT_ADOPTED) fail(`the grandfather comment is only valid for Agent Notes dated before ${FORMAT_ADOPTED}`)
  71. if (prose.some(line => LEGACY_MARKERS.some(marker => line.includes(marker)))) fail('carries the retired legacy-format debt marker')
  72. }
  73. if (errors.length === 0) {
  74. console.log(`verify-agent-note-format: ${notes.length} Agent Note(s) checked, all conform to .agents/notes/README.md § The file format.`)
  75. process.exit(0)
  76. }
  77. console.error('verify-agent-note-format: violations found:')
  78. for (const e of errors) console.error(` ${e}`)
  79. process.exit(1)