agent-note-tree.ts 3.5 KB

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