1
0

agent-note-tree.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Shared structural source of truth for the Agent Note tree. Lifecycle and class
  3. * sets are closed under `.agents/notes/README.md`; importing this module is pure.
  4. */
  5. import { globSync, readdirSync } from 'node:fs'
  6. import { resolve, sep } from 'node:path'
  7. export const agentNoteRoot = resolve(import.meta.dirname, '../.agents/notes')
  8. /** The closed set of active Agent Note lifecycles (top-level folders under .agents/notes/). */
  9. const AGENT_NOTE_LIFECYCLES = ['proposed', 'implemented', 'rejected'] as const
  10. /**
  11. * The closed set of Agent Note classes (nested folder under each lifecycle). Adding a
  12. * class is a deliberate act: extend this list AND the README's Classification
  13. * section. The gate rejects any folder not listed here.
  14. */
  15. export const AGENT_NOTE_CLASSES = ['feature', 'bug-fix', 'simplification', 'architecture', 'process', 'testing'] as const
  16. /** Historical implemented notes live outside the active lifecycle tree. */
  17. const AGENT_NOTE_ARCHIVE = 'archived'
  18. /** Non-Agent Note Markdown allowed to sit directly at a lifecycle root. */
  19. const ROOT_ALLOWLIST = new Set(['AGENTS.md', 'CLAUDE.md'])
  20. /** One Agent Note file, as discovered by the walker. */
  21. export interface AgentNote {
  22. lifecycle: string
  23. /** Path relative to .agents/notes. */
  24. rel: string
  25. /** `yyyy-mm-dd` from the filename. */
  26. date: string
  27. }
  28. /**
  29. * Walk the Agent Note tree, enforcing the structure rules. Returns every valid Agent Note
  30. * plus one error string per violation (unknown lifecycle or class folder, bad
  31. * depth, or bad filename). Callers treat a non-empty error list as fatal.
  32. */
  33. export function walkAgentNoteTree(): { notes: AgentNote[]; errors: string[] } {
  34. const notes: AgentNote[] = []
  35. const errors: string[] = []
  36. // The lifecycle set is closed too: any directory under .agents/notes/ that is not
  37. // a known lifecycle would otherwise hold Agent Notes invisible to the walk below.
  38. for (const entry of readdirSync(agentNoteRoot, { withFileTypes: true })) {
  39. if (entry.name === 'INDEX.md') {
  40. errors.push('structure: INDEX.md — centralized Agent Note indexes are forbidden; browse the lifecycle/class tree or search the repository')
  41. continue
  42. }
  43. if (entry.isDirectory()
  44. && entry.name !== AGENT_NOTE_ARCHIVE
  45. && !(AGENT_NOTE_LIFECYCLES as readonly string[]).includes(entry.name)) {
  46. errors.push(`structure: ${entry.name}/ — unknown lifecycle folder (allowed: ${AGENT_NOTE_LIFECYCLES.join(', ')}, plus ${AGENT_NOTE_ARCHIVE}/)`)
  47. }
  48. }
  49. for (const lifecycle of AGENT_NOTE_LIFECYCLES) {
  50. for (const match of globSync(`${lifecycle}/**/*.md`, { cwd: agentNoteRoot }).map(path => path.split(sep).join('/')).sort()) {
  51. const segs = match.split('/')
  52. // Allowlisted file directly at the lifecycle root (e.g. implemented/AGENTS.md).
  53. if (segs.length === 2 && ROOT_ALLOWLIST.has(segs[1] ?? '')) continue
  54. // A Chinese counterpart (foo.zh.md, docs/i18n/README.md) is the SAME Agent Note,
  55. // indexed via its English filename; the pairing gate owns its consistency.
  56. if (match.endsWith('.zh.md')) continue
  57. const cls = segs[1]
  58. const base = segs[2]
  59. if (segs.length !== 3 || cls === undefined || base === undefined) {
  60. errors.push(`structure: ${match} — expected {lifecycle}/{class}/file.md (got depth ${segs.length})`)
  61. continue
  62. }
  63. if (!(AGENT_NOTE_CLASSES as readonly string[]).includes(cls)) {
  64. errors.push(`structure: ${match} — unknown class folder "${cls}" (allowed: ${AGENT_NOTE_CLASSES.join(', ')})`)
  65. continue
  66. }
  67. if (!/^\d{4}-\d{2}-\d{2}-.+\.md$/.test(base)) {
  68. errors.push(`structure: ${match} — filename must be yyyy-mm-dd-topic.md`)
  69. continue
  70. }
  71. notes.push({ lifecycle, rel: match, date: base.slice(0, 10) })
  72. }
  73. }
  74. return { notes, errors }
  75. }