intent-format 0.0.0-stage → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/.github/ISSUE_TEMPLATE/1-something-wrong.yml +18 -0
  2. package/.github/ISSUE_TEMPLATE/2-idea.yml +11 -0
  3. package/.github/ISSUE_TEMPLATE/config.yml +5 -0
  4. package/AGENTS.md +11 -0
  5. package/CHANGELOG.md +287 -0
  6. package/CLAUDE.md +1 -0
  7. package/CODE_OF_CONDUCT.md +128 -0
  8. package/CONTRIBUTING.md +19 -0
  9. package/LICENSE +21 -0
  10. package/README.md +71 -2
  11. package/SECURITY.md +10 -0
  12. package/VERSION +1 -0
  13. package/examples/example-app/AGENTS.md +19 -0
  14. package/examples/example-app/CLAUDE.md +1 -0
  15. package/examples/example-app/V1.0/apis/_index.md +14 -0
  16. package/examples/example-app/V1.0/apis/ap000001-list-items.md +20 -0
  17. package/examples/example-app/V1.0/apis/ap000002-save-item.md +28 -0
  18. package/examples/example-app/V1.0/backend/_index.md +15 -0
  19. package/examples/example-app/V1.0/backend/bn000001-app-server.md +24 -0
  20. package/examples/example-app/V1.0/backend/bn000002-main-database.md +22 -0
  21. package/examples/example-app/V1.0/backend/bn000003-file-storage.md +22 -0
  22. package/examples/example-app/V1.0/comments/_index.md +13 -0
  23. package/examples/example-app/V1.0/comments/cm000001-empty-list-message.md +15 -0
  24. package/examples/example-app/V1.0/components/_index.md +16 -0
  25. package/examples/example-app/V1.0/components/cp000001-button.md +32 -0
  26. package/examples/example-app/V1.0/components/cp000002-text-field.md +31 -0
  27. package/examples/example-app/V1.0/components/cp000003-item-row.md +34 -0
  28. package/examples/example-app/V1.0/components/cp000004-top-bar.md +26 -0
  29. package/examples/example-app/V1.0/decisions/_index.md +13 -0
  30. package/examples/example-app/V1.0/decisions/dc000001-where-do-item-images-live.md +26 -0
  31. package/examples/example-app/V1.0/edge-cases/_index.md +13 -0
  32. package/examples/example-app/V1.0/edge-cases/ec000001-save-fails.md +24 -0
  33. package/examples/example-app/V1.0/features/_index.md +17 -0
  34. package/examples/example-app/V1.0/features/ft000001-items.md +22 -0
  35. package/examples/example-app/V1.0/features/ft000002-account.md +21 -0
  36. package/examples/example-app/V1.0/features/ft000003-browse-items.md +26 -0
  37. package/examples/example-app/V1.0/features/ft000004-edit-an-item.md +33 -0
  38. package/examples/example-app/V1.0/features/ft000005-change-settings.md +21 -0
  39. package/examples/example-app/V1.0/ia/_index.md +13 -0
  40. package/examples/example-app/V1.0/ia/ia000001-main-map.md +23 -0
  41. package/examples/example-app/V1.0/intent-format.md +12 -0
  42. package/examples/example-app/V1.0/project.md +51 -0
  43. package/examples/example-app/V1.0/roles/_index.md +14 -0
  44. package/examples/example-app/V1.0/roles/ro000001-member.md +22 -0
  45. package/examples/example-app/V1.0/roles/ro000002-admin.md +19 -0
  46. package/examples/example-app/V1.0/rules/_index.md +13 -0
  47. package/examples/example-app/V1.0/rules/rl000001-item-name-is-required.md +20 -0
  48. package/examples/example-app/V1.0/screens/_index.md +17 -0
  49. package/examples/example-app/V1.0/screens/sc000001-home.md +18 -0
  50. package/examples/example-app/V1.0/screens/sc000002-item-list.md +43 -0
  51. package/examples/example-app/V1.0/screens/sc000003-item-detail.md +24 -0
  52. package/examples/example-app/V1.0/screens/sc000004-edit-item.md +35 -0
  53. package/examples/example-app/V1.0/screens/sc000005-settings.md +19 -0
  54. package/examples/example-app/V1.0/shells/_index.md +8 -0
  55. package/examples/example-app/V1.0/tech/_index.md +14 -0
  56. package/examples/example-app/V1.0/tech/tc000001-ui-framework.md +23 -0
  57. package/examples/example-app/V1.0/tech/tc000002-relational-database.md +25 -0
  58. package/examples/example-app/V1.0/userflows/_index.md +14 -0
  59. package/examples/example-app/V1.0/userflows/uf000001-create-an-item.md +66 -0
  60. package/examples/example-app/V1.0/userflows/uf000002-edit-an-item.md +47 -0
  61. package/examples/example-app/V1.1/CHANGES.md +12 -0
  62. package/examples/example-app/V1.1/apis/_index.md +14 -0
  63. package/examples/example-app/V1.1/apis/ap000001-list-items.md +20 -0
  64. package/examples/example-app/V1.1/apis/ap000002-save-item.md +28 -0
  65. package/examples/example-app/V1.1/backend/_index.md +15 -0
  66. package/examples/example-app/V1.1/backend/bn000001-app-server.md +24 -0
  67. package/examples/example-app/V1.1/backend/bn000002-main-database.md +22 -0
  68. package/examples/example-app/V1.1/backend/bn000003-file-storage.md +22 -0
  69. package/examples/example-app/V1.1/comments/_index.md +14 -0
  70. package/examples/example-app/V1.1/comments/cm000001-empty-list-message.md +15 -0
  71. package/examples/example-app/V1.1/comments/cm000002-filter-placement.md +12 -0
  72. package/examples/example-app/V1.1/components/_index.md +16 -0
  73. package/examples/example-app/V1.1/components/cp000001-button.md +32 -0
  74. package/examples/example-app/V1.1/components/cp000002-text-field.md +32 -0
  75. package/examples/example-app/V1.1/components/cp000003-item-row.md +34 -0
  76. package/examples/example-app/V1.1/components/cp000004-top-bar.md +26 -0
  77. package/examples/example-app/V1.1/decisions/_index.md +13 -0
  78. package/examples/example-app/V1.1/decisions/dc000001-where-do-item-images-live.md +26 -0
  79. package/examples/example-app/V1.1/edge-cases/_index.md +13 -0
  80. package/examples/example-app/V1.1/edge-cases/ec000001-save-fails.md +24 -0
  81. package/examples/example-app/V1.1/features/_index.md +17 -0
  82. package/examples/example-app/V1.1/features/ft000001-items.md +22 -0
  83. package/examples/example-app/V1.1/features/ft000002-account.md +21 -0
  84. package/examples/example-app/V1.1/features/ft000003-browse-items.md +26 -0
  85. package/examples/example-app/V1.1/features/ft000004-edit-an-item.md +33 -0
  86. package/examples/example-app/V1.1/features/ft000005-change-settings.md +21 -0
  87. package/examples/example-app/V1.1/ia/_index.md +13 -0
  88. package/examples/example-app/V1.1/ia/ia000001-main-map.md +23 -0
  89. package/examples/example-app/V1.1/intent-format.md +13 -0
  90. package/examples/example-app/V1.1/project.md +51 -0
  91. package/examples/example-app/V1.1/roles/_index.md +14 -0
  92. package/examples/example-app/V1.1/roles/ro000001-member.md +22 -0
  93. package/examples/example-app/V1.1/roles/ro000002-admin.md +19 -0
  94. package/examples/example-app/V1.1/rules/_index.md +13 -0
  95. package/examples/example-app/V1.1/rules/rl000001-item-name-is-required.md +20 -0
  96. package/examples/example-app/V1.1/screens/_index.md +17 -0
  97. package/examples/example-app/V1.1/screens/sc000001-home.md +18 -0
  98. package/examples/example-app/V1.1/screens/sc000002-item-list.md +44 -0
  99. package/examples/example-app/V1.1/screens/sc000003-item-detail.md +24 -0
  100. package/examples/example-app/V1.1/screens/sc000004-edit-item.md +35 -0
  101. package/examples/example-app/V1.1/screens/sc000005-settings.md +19 -0
  102. package/examples/example-app/V1.1/shells/_index.md +8 -0
  103. package/examples/example-app/V1.1/tech/_index.md +14 -0
  104. package/examples/example-app/V1.1/tech/tc000001-ui-framework.md +23 -0
  105. package/examples/example-app/V1.1/tech/tc000002-relational-database.md +25 -0
  106. package/examples/example-app/V1.1/userflows/_index.md +14 -0
  107. package/examples/example-app/V1.1/userflows/uf000001-create-an-item.md +66 -0
  108. package/examples/example-app/V1.1/userflows/uf000002-edit-an-item.md +47 -0
  109. package/examples/example-app/_ids.md +78 -0
  110. package/examples/example-app/_index.md +14 -0
  111. package/examples/example-app/inputs/brief.md +3 -0
  112. package/examples/example-app/inputs/links.md +3 -0
  113. package/kit/cli/README.md +128 -0
  114. package/kit/cli/intent-format.mjs +342 -0
  115. package/kit/cli/lib/agents.mjs +39 -0
  116. package/kit/cli/lib/check.mjs +412 -0
  117. package/kit/cli/lib/create.mjs +190 -0
  118. package/kit/cli/lib/frame.mjs +182 -0
  119. package/kit/cli/lib/generate.mjs +418 -0
  120. package/kit/cli/lib/kit.mjs +203 -0
  121. package/kit/cli/lib/migrate.mjs +97 -0
  122. package/kit/cli/lib/model.mjs +409 -0
  123. package/kit/cli/lib/next.mjs +134 -0
  124. package/kit/cli/lib/open.mjs +34 -0
  125. package/kit/cli/lib/parse.mjs +260 -0
  126. package/kit/cli/lib/project.mjs +120 -0
  127. package/kit/cli/lib/regen.mjs +112 -0
  128. package/kit/cli/lib/relations.mjs +380 -0
  129. package/kit/cli/lib/rename.mjs +23 -0
  130. package/kit/cli/lib/schema.mjs +185 -0
  131. package/kit/cli/lib/templates.mjs +41 -0
  132. package/kit/cli/lib/util.mjs +165 -0
  133. package/kit/cli/test/nav.mjs +145 -0
  134. package/kit/cli/test/roles.mjs +85 -0
  135. package/kit/cli/test/smoke.mjs +716 -0
  136. package/kit/skills/intent-format/SKILL.md +91 -0
  137. package/kit/skills/intent-format/links.md +142 -0
  138. package/kit/skills/intent-format/types/_overview.md +73 -0
  139. package/kit/skills/intent-format/types/api.md +111 -0
  140. package/kit/skills/intent-format/types/backend.md +110 -0
  141. package/kit/skills/intent-format/types/comment.md +66 -0
  142. package/kit/skills/intent-format/types/component.md +139 -0
  143. package/kit/skills/intent-format/types/decision.md +91 -0
  144. package/kit/skills/intent-format/types/edge-case.md +107 -0
  145. package/kit/skills/intent-format/types/feature.md +154 -0
  146. package/kit/skills/intent-format/types/ia.md +106 -0
  147. package/kit/skills/intent-format/types/project.md +131 -0
  148. package/kit/skills/intent-format/types/role.md +98 -0
  149. package/kit/skills/intent-format/types/rule.md +100 -0
  150. package/kit/skills/intent-format/types/screen.md +153 -0
  151. package/kit/skills/intent-format/types/shell.md +156 -0
  152. package/kit/skills/intent-format/types/tech.md +109 -0
  153. package/kit/skills/intent-format/types/userflow.md +197 -0
  154. package/kit/skills/kb-cold-start/SKILL.md +124 -0
  155. package/kit/skills/kb-cold-start/kickoff-questionnaire.md +42 -0
  156. package/kit/skills/kb-cold-start/templates.md +108 -0
  157. package/package.json +30 -4
  158. package/rationale.md +180 -0
  159. package/schema.json +5895 -0
  160. package/spec.md +1173 -0
  161. package/templates/api.md +16 -0
  162. package/templates/backend.md +15 -0
  163. package/templates/comment.md +11 -0
  164. package/templates/component.md +15 -0
  165. package/templates/decision.md +23 -0
  166. package/templates/edge-case.md +14 -0
  167. package/templates/feature.md +14 -0
  168. package/templates/ia.md +14 -0
  169. package/templates/project.md +34 -0
  170. package/templates/role.md +14 -0
  171. package/templates/rule.md +14 -0
  172. package/templates/screen.md +14 -0
  173. package/templates/shell.md +24 -0
  174. package/templates/tech.md +18 -0
  175. package/templates/userflow.md +20 -0
@@ -0,0 +1,260 @@
1
+ // Low-level parsing: front matter, card skeleton (title, status line, sections), relation lines.
2
+ import { splitTop, unquote, quote } from './util.mjs';
3
+
4
+ /* ---------------- front matter ---------------- */
5
+
6
+ export function parseFrontMatter(lines, R) {
7
+ if (lines[0] !== '---') return null;
8
+ const entries = [], errors = [];
9
+ let end = -1;
10
+ for (let i = 1; i < lines.length; i++) {
11
+ if (lines[i] === '---') { end = i; break; }
12
+ const m = R.fmLine.exec(lines[i]);
13
+ if (!m) { errors.push({ idx: i, msg: `not a "key: value" line: ${lines[i]}` }); continue; }
14
+ const raw = m.groups.value ?? '';
15
+ const e = { key: m.groups.key, raw, idx: i, kind: 'plain', value: raw, items: null };
16
+ if (raw.trim() === '') { e.kind = 'empty'; e.value = ''; }
17
+ else if (raw === '—') e.kind = 'blind';
18
+ else if (raw.startsWith('"')) {
19
+ const q = R.quoted.exec(raw);
20
+ if (!q) errors.push({ idx: i, msg: `unbalanced quotes in ${e.key}:` });
21
+ else { e.kind = 'quoted'; e.value = unquote(q.groups.inner); }
22
+ } else if (raw.startsWith('[')) {
23
+ const l = R.list.exec(raw);
24
+ if (!l) errors.push({ idx: i, msg: `unbalanced list in ${e.key}:` });
25
+ else {
26
+ e.kind = 'list';
27
+ e.items = l.groups.items.trim() === '' ? [] : l.groups.items.split(',').map((x) => {
28
+ const t = x.trim();
29
+ return t.startsWith('"') && t.endsWith('"') && t.length > 1 ? unquote(t.slice(1, -1)) : t;
30
+ });
31
+ e.value = e.items.join(', ');
32
+ }
33
+ }
34
+ entries.push(e);
35
+ }
36
+ if (end < 0) errors.push({ idx: 0, msg: 'front matter is not closed by ---' });
37
+ const map = new Map();
38
+ for (const e of entries) if (!map.has(e.key)) map.set(e.key, e);
39
+ return { entries, end, errors, map };
40
+ }
41
+
42
+ /** Render one front-matter value following D3.4/D3.7. enumKey: value comes from a closed list. */
43
+ export function fmValue(S, key, value, enumKey = false) {
44
+ if (Array.isArray(value)) return '[' + value.join(', ') + ']';
45
+ const v = String(value);
46
+ if (v === '—') return v;
47
+ if (S.alwaysQuoted.has(key)) return quote(v);
48
+ if (S.R.needsQuotes.test(v) || (!enumKey && S.R.readsAsNonString.test(v))) return quote(v);
49
+ return v;
50
+ }
51
+
52
+ /* ---------------- skeleton ---------------- */
53
+
54
+ const FENCE = /^(```|~~~)/;
55
+
56
+ /** Marks lines inside fenced code blocks (never parsed as structure). */
57
+ export function fenceMask(lines, from = 0) {
58
+ const mask = new Array(lines.length).fill(false);
59
+ let open = null;
60
+ for (let i = from; i < lines.length; i++) {
61
+ const m = FENCE.exec(lines[i]);
62
+ if (open) { mask[i] = true; if (m && lines[i].startsWith(open)) open = null; }
63
+ else if (m) { open = m[1]; mask[i] = true; }
64
+ }
65
+ return mask;
66
+ }
67
+
68
+ /**
69
+ * Card skeleton after the front matter: title, status line, preamble, ## sections.
70
+ * Indices are 0-based line indices; ranges are [start, end).
71
+ */
72
+ export function parseSkeleton(lines, bodyStart, R, mask) {
73
+ const sk = { titleIdx: -1, statusIdx: -1, preStart: bodyStart, preEnd: lines.length, sections: [] };
74
+ let i = bodyStart;
75
+ while (i < lines.length && lines[i].trim() === '') i++;
76
+ if (i < lines.length && /^# /.test(lines[i])) {
77
+ sk.titleIdx = i;
78
+ let j = i + 1;
79
+ while (j < lines.length && lines[j].trim() === '') j++;
80
+ if (j < lines.length && /^> ⚙/.test(lines[j])) sk.statusIdx = j;
81
+ sk.preStart = (sk.statusIdx >= 0 ? sk.statusIdx : i) + 1;
82
+ }
83
+ for (let k = sk.preStart; k < lines.length; k++) {
84
+ if (mask[k]) continue;
85
+ const m = R.section.exec(lines[k]);
86
+ if (!m || !/^## /.test(lines[k])) continue;
87
+ const gear = !!m.groups.gear;
88
+ sk.sections.push({ heading: m.groups.heading, gear, idx: k, start: k + 1, end: lines.length });
89
+ }
90
+ for (let s = 0; s < sk.sections.length; s++) {
91
+ if (s + 1 < sk.sections.length) sk.sections[s].end = sk.sections[s + 1].idx;
92
+ }
93
+ if (sk.sections.length) sk.preEnd = sk.sections[0].idx;
94
+ return sk;
95
+ }
96
+
97
+ /** Split a range at ### headings: { intro: [start,end), subs: [{idx, text, start, end}] }. */
98
+ export function splitSub(lines, start, end, mask) {
99
+ const subs = [];
100
+ let introEnd = end;
101
+ for (let i = start; i < end; i++) {
102
+ if (mask[i] || !/^### \S/.test(lines[i])) continue;
103
+ if (!subs.length) introEnd = i;
104
+ if (subs.length) subs[subs.length - 1].end = i;
105
+ subs.push({ idx: i, text: lines[i].slice(4).trim(), start: i + 1, end });
106
+ }
107
+ return { intro: [start, introEnd], subs };
108
+ }
109
+
110
+ /** Non-blank, non-fenced line indices of a range. */
111
+ export function contentIdx(lines, start, end, mask) {
112
+ const out = [];
113
+ for (let i = start; i < end; i++) if (!mask[i] && lines[i].trim() !== '') out.push(i);
114
+ return out;
115
+ }
116
+
117
+ /** Body state of a range: 'blind' | 'none' | 'forgive' | 'empty' | null (content). */
118
+ export function bodyState(lines, idxs, R) {
119
+ if (!idxs.length) return 'empty';
120
+ if (idxs.length > 1) return null;
121
+ const t = lines[idxs[0]].trim();
122
+ if (R.blind.test(t)) return 'blind';
123
+ if (R.none.test(t)) return 'none';
124
+ if (R.forgiveness.test(t)) return 'forgive';
125
+ return null;
126
+ }
127
+
128
+ /* ---------------- atoms and relation lines ---------------- */
129
+
130
+ export function parseAtom(text, R, linkMode = false) {
131
+ const t = text;
132
+ if (linkMode) {
133
+ const l = R.link.exec(t);
134
+ return l ? { kind: 'link', title: l.groups.title, target: l.groups.target } : null;
135
+ }
136
+ let m;
137
+ if ((m = R.bound.exec(t))) return { kind: 'bound', core: m.groups.core, name: m.groups.name ?? null, path: m.groups.path, anchor: m.groups.anchor ?? null };
138
+ if ((m = R.hand.exec(t))) return { kind: 'hand', core: m.groups.core };
139
+ if ((m = R.placeholder.exec(t))) return { kind: 'placeholder', name: m.groups.name };
140
+ if (t === '—') return { kind: 'unknown' };
141
+ return null;
142
+ }
143
+
144
+ function parseQualifiers(segs, base, R) {
145
+ const quals = [];
146
+ for (const seg of segs) {
147
+ const raw = seg.text;
148
+ const m = R.qualifier.exec(raw);
149
+ if (!m) { quals.push({ key: null, raw, start: base + seg.start, bad: true }); continue; }
150
+ let value = m.groups.value ?? null, quoted = false;
151
+ if (value && value.startsWith('"') && value.endsWith('"') && value.length > 1) { value = unquote(value.slice(1, -1)); quoted = true; }
152
+ quals.push({ key: m.groups.key, value, quoted, raw, start: base + seg.start });
153
+ }
154
+ return quals;
155
+ }
156
+
157
+ function atomAt(text, base, R, linkMode, glyphOk) {
158
+ let t = text, off = base;
159
+ const lead = t.length - t.trimStart().length;
160
+ t = t.trim(); off += lead;
161
+ let glyph = null;
162
+ if (glyphOk) {
163
+ const g = R.stepGlyph.exec(t);
164
+ if (g) { glyph = g.groups.glyph; off += g[0].length; t = t.slice(g[0].length); }
165
+ }
166
+ const atom = parseAtom(t, R, linkMode);
167
+ if (atom) Object.assign(atom, { text: t, start: off, end: off + t.length, glyph });
168
+ return atom;
169
+ }
170
+
171
+ /**
172
+ * Parse one line in a relation context.
173
+ * cx: { relations, defaultRel, glyphOk }
174
+ * Returns { kind: 'nonlist' | 'generated' | 'unknown-word' | 'prose' | 'ext' | 'rel', ... }.
175
+ */
176
+ export function parseRelLine(line, cx, R) {
177
+ const m = R.listLine.exec(line);
178
+ if (!m) return { kind: 'nonlist' };
179
+ const indent = m.groups.indent.length;
180
+ const ordered = m.groups.num != null;
181
+ let rest = m.groups.rest;
182
+ let off = line.length - rest.length;
183
+ if (R.generatedLine.exec(line.slice(indent))) return { kind: 'generated', indent, ordered };
184
+ let rel = null, explicit = false;
185
+ const pm = R.relationPrefix.exec(rest);
186
+ if (pm) {
187
+ const w = pm.groups.rel;
188
+ if (w.startsWith('x-')) return { kind: 'ext', indent, ordered };
189
+ if (cx.relations[w]) {
190
+ rel = w; explicit = true;
191
+ off += w.length + 2; rest = pm.groups.body;
192
+ } else if (!cx.defaultRel || !parseAtom(rest.trim(), R, cx.defaultRel === 'Link')) {
193
+ return { kind: 'unknown-word', word: w, indent, ordered };
194
+ }
195
+ }
196
+ if (!rel) {
197
+ if (!cx.defaultRel) return { kind: 'prose', indent, ordered };
198
+ rel = cx.defaultRel;
199
+ }
200
+ const def = cx.relations[rel];
201
+ const r = { kind: 'rel', rel, explicit, indent, ordered, num: ordered ? +m.groups.num : null, atoms: [], quals: [], note: null, ok: true };
202
+ if (def.textSlot) {
203
+ r.text = rest; r.textStart = off;
204
+ return r;
205
+ }
206
+ const noteSplit = splitTop(rest, ' — ', 1);
207
+ if (noteSplit.length > 1) r.note = noteSplit[1].text;
208
+ let main = noteSplit[0].text, mainOff = off;
209
+ if (def.twoSlot || def.labelSlot) {
210
+ const lr = splitTop(main, ' → ', 1);
211
+ if (lr.length < 2) { r.ok = false; r.why = 'missing " → "'; return r; }
212
+ if (def.twoSlot) {
213
+ const a = atomAt(lr[0].text, mainOff + lr[0].start, R, false, false);
214
+ if (!a) { r.ok = false; r.why = `"${lr[0].text.trim()}" is not a reference`; return r; }
215
+ r.atoms.push(a);
216
+ } else {
217
+ let label = lr[0].text.trim();
218
+ if (label.startsWith('"') && label.endsWith('"') && label.length > 1) label = unquote(label.slice(1, -1));
219
+ r.label = label;
220
+ }
221
+ main = lr[1].text; mainOff += lr[1].start;
222
+ }
223
+ const segs = splitTop(main, ' · ');
224
+ const a = atomAt(segs[0].text, mainOff + segs[0].start, R, rel === 'Link', !!cx.glyphOk);
225
+ if (!a) { r.ok = false; r.why = `"${segs[0].text.trim()}" is not a ${rel === 'Link' ? '[title](target) link' : 'reference'}`; }
226
+ else r.atoms.push(a);
227
+ r.quals = parseQualifiers(segs.slice(1), mainOff, R);
228
+ return r;
229
+ }
230
+
231
+ /** Passport line → [{key, value, raw}] or null when the line is not a passport line. */
232
+ export function parsePassport(line, R) {
233
+ if (!R.passportLine.test(line)) return null;
234
+ const pairs = [];
235
+ for (const seg of splitTop(line, ' · ')) {
236
+ const m = R.passportPair.exec(seg.text);
237
+ if (!m) return null;
238
+ let v = m.groups.value;
239
+ if (v.startsWith('"') && v.endsWith('"') && v.length > 1) v = unquote(v.slice(1, -1));
240
+ pairs.push({ key: m.groups.key, value: v });
241
+ }
242
+ return pairs;
243
+ }
244
+
245
+ /** Markdown table rows of a range → { header: [..], rows: [{idx, cells}] , bad: [idx] }. */
246
+ export function parseTable(lines, idxs, R) {
247
+ const out = { header: null, rows: [], bad: [] };
248
+ let sawSep = false;
249
+ for (const i of idxs) {
250
+ const t = lines[i].trim();
251
+ const m = R.tableRow.exec(t);
252
+ if (!m) { out.bad.push(i); continue; }
253
+ if (R.tableSep.test(t)) { sawSep = true; continue; }
254
+ const cells = m.groups.cells.split('|').map((c) => c.trim());
255
+ if (!out.header) out.header = cells;
256
+ else out.rows.push({ idx: i, cells });
257
+ }
258
+ if (out.header && !sawSep) out.bad.push(idxs[0]);
259
+ return out;
260
+ }
@@ -0,0 +1,120 @@
1
+ // Loads a project root: workspaces (V<x.y>/), their manifests and cards, the core index and _ids.md.
2
+ import path from 'node:path';
3
+ import { listDir, walkMd, exists, readText, toPosix, compareVersions } from './util.mjs';
4
+ import { buildFile } from './model.mjs';
5
+
6
+ export function emptyVocab() {
7
+ return { declared: new Set(), values: {}, stateCodes: new Map(), roleStates: [] };
8
+ }
9
+
10
+ export function loadProject(S, root) {
11
+ root = path.resolve(root);
12
+ const P = { root, workspaces: [], byVersion: new Map(), ids: null, rootFiles: [] };
13
+ for (const e of listDir(root)) {
14
+ if (e.isDirectory() && S.R.workspaceFolder.test(e.name)) P.workspaces.push(loadWorkspace(S, P, e.name));
15
+ }
16
+ P.workspaces.sort((a, b) => compareVersions(a.version, b.version));
17
+ for (const ws of P.workspaces) P.byVersion.set(ws.version, ws);
18
+ for (const ws of P.workspaces) ws.base = ws.basedOn ? P.byVersion.get(ws.basedOn) || null : null;
19
+ for (const n of ['_index.md', '_ids.md']) {
20
+ const f = path.join(root, n);
21
+ if (exists(f)) P.rootFiles.push(buildFile(S, f, n));
22
+ }
23
+ const idsFile = path.join(root, '_ids.md');
24
+ P.ids = exists(idsFile) ? parseIds(readText(idsFile)) : { resolved: [], retired: [], marks: new Map() };
25
+ return P;
26
+ }
27
+
28
+ function loadWorkspace(S, P, folder) {
29
+ const dir = path.join(P.root, folder);
30
+ const rel = (f) => toPosix(path.relative(P.root, f));
31
+ const ws = {
32
+ folder, dir, version: folder.slice(1), files: [], cards: [], byCore: new Map(), dupCores: [],
33
+ manifest: null, project: null, vocab: emptyVocab(), modules: [...S.modules], lifecycle: null,
34
+ basedOn: null, kb: null, name: null, subfolders: [],
35
+ };
36
+ const mf = path.join(dir, 'intent-format.md');
37
+ if (exists(mf)) {
38
+ ws.manifest = buildFile(S, mf, rel(mf));
39
+ const m = ws.manifest.fm?.map;
40
+ if (m) {
41
+ ws.kb = m.get('kb')?.value ?? null;
42
+ ws.name = m.get('name')?.value ?? null;
43
+ ws.lifecycle = m.get('lifecycle')?.value ?? null;
44
+ ws.basedOn = m.get('based-on')?.value || null;
45
+ const mods = m.get('modules');
46
+ if (mods && mods.kind === 'list') ws.modules = mods.items;
47
+ }
48
+ }
49
+ for (const f of walkMd(dir)) {
50
+ if (f === mf) continue;
51
+ const c = buildFile(S, f, rel(f), { modules: ws.modules });
52
+ c.ws = ws;
53
+ ws.files.push(c);
54
+ }
55
+ for (const e of listDir(dir)) {
56
+ if (!e.isDirectory() || !S.modules.includes(e.name)) continue;
57
+ for (const s of listDir(path.join(dir, e.name))) if (s.isDirectory()) ws.subfolders.push(`${folder}/${e.name}/${s.name}`);
58
+ }
59
+ ws.cards = ws.files.filter((c) => c.kind === 'card');
60
+ ws.project = ws.cards.find((c) => c.type === 'project' && path.basename(c.file) === 'project.md' && path.dirname(c.file) === dir)
61
+ || ws.cards.find((c) => c.type === 'project') || null;
62
+ if (ws.project && ws.project.vocab) ws.vocab = ws.project.vocab;
63
+ // core index
64
+ const put = (core, entry) => {
65
+ if (ws.byCore.has(core)) ws.dupCores.push({ core, a: ws.byCore.get(core), b: entry });
66
+ else ws.byCore.set(core, entry);
67
+ };
68
+ for (const c of ws.files) {
69
+ if ((c.kind === 'card' || c.kind === 'x') && c.id) put(c.id, { card: c, sub: null });
70
+ for (const sd of c.subs) put(sd.core, { card: c, sub: sd });
71
+ }
72
+ return ws;
73
+ }
74
+
75
+ /** Type of a resolved target: card type or sub-document kind. */
76
+ export const targetType = (t) => (t.sub ? t.sub.kind : t.card.type);
77
+
78
+ /** Name of a resolved target. */
79
+ export const targetName = (t) => (t.sub ? t.sub.name : t.card.name);
80
+
81
+ /** Absolute file + anchor of a resolved target. */
82
+ export const targetAnchor = (t) => (t.sub ? t.sub.core.toLowerCase() : null);
83
+
84
+ /* ---------------- _ids.md ---------------- */
85
+
86
+ function tableAfter(lines, heading) {
87
+ const i = lines.findIndex((l) => l.replace('⚙️', '⚙').trim() === `## ${heading}`);
88
+ if (i < 0) return [];
89
+ const rows = [];
90
+ for (let j = i + 1; j < lines.length && !/^## /.test(lines[j]); j++) {
91
+ const t = lines[j].trim();
92
+ if (!/^\|.*\|$/.test(t) || /^\|(\s*:?-{3,}:?\s*\|)+$/.test(t)) continue;
93
+ rows.push(t.slice(1, -1).split('|').map((x) => x.trim()));
94
+ }
95
+ return rows.slice(1); // drop header
96
+ }
97
+
98
+ export function parseIds(text) {
99
+ const lines = text.split('\n');
100
+ const resolved = tableAfter(lines, '⚙ Resolved').map((r) => ({ core: r[0], type: r[1], name: r[2], versions: (r[3] || '').split(',').map((x) => x.trim()).filter(Boolean), path: r[4] }));
101
+ const retired = tableAfter(lines, '⚙ Retired').map((r) => ({ core: r[0], type: r[1], name: r[2], last: r[3], merged: r[4] || '—' }));
102
+ const marks = new Map();
103
+ for (const r of tableAfter(lines, '⚙ High-water marks')) if (/^[A-Z]{2}$/.test(r[0]) && /^\d+$/.test(r[1])) marks.set(r[0], +r[1]);
104
+ return { resolved, retired, marks };
105
+ }
106
+
107
+ /** Highest number per prefix: files of every workspace, Retired, High-water marks. */
108
+ export function highWater(P) {
109
+ const hw = new Map();
110
+ const bump = (core) => {
111
+ const p = core.slice(0, 2), n = +core.slice(2);
112
+ if (!/^[A-Z]{2}$/.test(p) || !Number.isFinite(n)) return;
113
+ if (!hw.has(p) || hw.get(p) < n) hw.set(p, n);
114
+ };
115
+ for (const ws of P.workspaces) for (const core of ws.byCore.keys()) bump(core);
116
+ for (const ws of P.workspaces) for (const d of ws.dupCores) bump(d.core);
117
+ for (const r of P.ids.retired) bump(r.core);
118
+ for (const [p, n] of P.ids.marks) if (!hw.has(p) || hw.get(p) < n) hw.set(p, n);
119
+ return hw;
120
+ }
@@ -0,0 +1,112 @@
1
+ // intent-format regen: rewrite machine-owned tokens and generated zones. Idempotent; no dates.
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { writeIfChanged, exists, toPosix } from './util.mjs';
5
+ import { targetType } from './project.mjs';
6
+ import { ref, makeContext, generatedFor, changeOf, statusLine, renderFolderIndex, renderRootIndex, renderIds, renderChanges } from './generate.mjs';
7
+
8
+ /** New text of one card: title, status line, refreshed authored zone, generated sections last. */
9
+ export function rewriteCard(S, ws, card, gen, status) {
10
+ const { lines, sk, mask } = card;
11
+ const res = (core) => ws.byCore.get(core);
12
+ const skip = new Set([sk.titleIdx, sk.statusIdx].filter((i) => i >= 0));
13
+ const untouched = new Set();
14
+ for (const sec of sk.sections) {
15
+ const range = [];
16
+ for (let i = sec.idx; i < sec.end; i++) range.push(i);
17
+ if (sec.gear && S.generated[sec.full]) range.forEach((i) => skip.add(i));
18
+ else if (sec.ext) range.forEach((i) => untouched.add(i));
19
+ }
20
+ const ups = new Map();
21
+ for (const r of card.rels) {
22
+ const def = S.relations[r.rel];
23
+ for (const a of r.atoms) {
24
+ if (a.kind !== 'hand') continue;
25
+ const t = res(a.core);
26
+ if (!t || (def.targetTypes.length && !def.targetTypes.includes(targetType(t)))) continue;
27
+ if (!ups.has(r.idx)) ups.set(r.idx, []);
28
+ ups.get(r.idx).push({ start: a.start, end: a.end, text: ref(card.file, t) });
29
+ }
30
+ }
31
+ const renumber = new Map();
32
+ for (const g of card.groups) g.forEach((idx, k) => renumber.set(idx, k + 1));
33
+ const forgive = new Set(card.forgive);
34
+ const confirmed = new Set(card.confirmedIdx || []);
35
+ const anchorAfter = new Map(), anchorFix = new Map();
36
+ for (const sd of card.subs) {
37
+ const want = `<a id="${sd.core.toLowerCase()}"></a>`;
38
+ if (sd.anchorIdx < 0) anchorAfter.set(sd.idx, want);
39
+ else if (lines[sd.anchorIdx] !== want) anchorFix.set(sd.anchorIdx, want);
40
+ }
41
+ const refresh = (l) => l.replace(S.R.boundG, (...a) => {
42
+ const t = res(a[a.length - 1].core);
43
+ return t ? ref(card.file, t) : a[0];
44
+ });
45
+ const body = [];
46
+ for (let i = card.fm.end + 1; i < lines.length; i++) {
47
+ if (skip.has(i)) continue;
48
+ let l = lines[i];
49
+ if (!untouched.has(i) && !mask[i] && !card.extLines.has(i)) {
50
+ if (anchorFix.has(i)) l = anchorFix.get(i);
51
+ else if (forgive.has(i)) l = l.replace(l.trim(), '—');
52
+ else if (confirmed.has(i)) l = l.replace(/ \[confirmed\]\s*$/, '');
53
+ else {
54
+ const u = ups.get(i);
55
+ if (u) for (const x of [...u].sort((p, q) => q.start - p.start)) l = l.slice(0, x.start) + x.text + l.slice(x.end);
56
+ if (renumber.has(i)) l = l.replace(/^(\s*)\d+\./, `$1${renumber.get(i)}.`);
57
+ l = refresh(l);
58
+ }
59
+ }
60
+ body.push(l);
61
+ if (anchorAfter.has(i)) {
62
+ body.push(anchorAfter.get(i));
63
+ const nx = i + 1 < lines.length ? lines[i + 1] : '';
64
+ if (nx.trim() !== '' && !/^#{2,3} /.test(nx) && !S.R.passportLine.test(nx)) body.push('');
65
+ }
66
+ }
67
+ while (body.length && body[0].trim() === '') body.shift();
68
+ while (body.length && body[body.length - 1].trim() === '') body.pop();
69
+ const out = lines.slice(0, card.fm.end + 1);
70
+ out.push(`# ${card.name} · #${card.id}`, status);
71
+ if (body.length) out.push('', ...body);
72
+ for (const g of gen) out.push('', `## ${g.heading}`, ...g.lines);
73
+ return out.join('\n') + '\n';
74
+ }
75
+
76
+ /** Compute every file regen would write. Returns [{file, text|null}] (null = delete). */
77
+ export function plan(S, P) {
78
+ const writes = [];
79
+ for (const ws of P.workspaces) {
80
+ if (S.raw.lifecycles.readOnly.includes(ws.lifecycle)) continue;
81
+ const ctx = makeContext(S, ws);
82
+ const changes = new Map();
83
+ for (const c of ws.cards) if (c.id) changes.set(c, changeOf(S, ws, c));
84
+ for (const c of ws.cards) {
85
+ if (!c.id || !c.name) continue;
86
+ writes.push({ file: c.file, text: rewriteCard(S, ws, c, generatedFor(ctx, c), statusLine(S, ws, c, changes.get(c))) });
87
+ }
88
+ for (const folder of S.modules) {
89
+ if (ws.modules.includes(folder) || exists(path.join(ws.dir, folder))) {
90
+ writes.push({ file: path.join(ws.dir, folder, '_index.md'), text: renderFolderIndex(S, ws, folder) });
91
+ }
92
+ }
93
+ const ch = renderChanges(S, ws, changes);
94
+ const cf = path.join(ws.dir, 'CHANGES.md');
95
+ if (ch) writes.push({ file: cf, text: ch });
96
+ else if (exists(cf)) writes.push({ file: cf, text: null });
97
+ }
98
+ writes.push({ file: path.join(P.root, '_index.md'), text: renderRootIndex(S, P) });
99
+ writes.push({ file: path.join(P.root, '_ids.md'), text: renderIds(S, P) });
100
+ return writes;
101
+ }
102
+
103
+ /** Apply a plan; returns the project-relative paths that changed. */
104
+ export function apply(P, writes) {
105
+ const changed = [];
106
+ for (const w of writes) {
107
+ const rel = toPosix(path.relative(P.root, w.file));
108
+ if (w.text === null) { fs.rmSync(w.file, { force: true }); changed.push(rel + ' (removed)'); continue; }
109
+ if (writeIfChanged(w.file, w.text)) changed.push(rel);
110
+ }
111
+ return changed;
112
+ }