@driftsys/markspec 0.1.0 → 0.3.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 (242) hide show
  1. package/README.md +32 -8
  2. package/esm/_dnt.polyfills.d.ts +101 -0
  3. package/esm/_dnt.polyfills.d.ts.map +1 -0
  4. package/esm/_dnt.polyfills.js +127 -0
  5. package/esm/compiler/inverses.d.ts +22 -0
  6. package/esm/compiler/inverses.d.ts.map +1 -0
  7. package/esm/compiler/inverses.js +147 -0
  8. package/esm/compiler/link_target.d.ts +19 -0
  9. package/esm/compiler/link_target.d.ts.map +1 -0
  10. package/esm/compiler/link_target.js +66 -0
  11. package/esm/compiler/mod.d.ts +43 -0
  12. package/esm/compiler/mod.d.ts.map +1 -0
  13. package/esm/compiler/mod.js +163 -0
  14. package/esm/compiler/schema.d.ts +38 -0
  15. package/esm/compiler/schema.d.ts.map +1 -0
  16. package/esm/compiler/schema.js +44 -0
  17. package/esm/config/markspec.d.ts +51 -0
  18. package/esm/config/markspec.d.ts.map +1 -0
  19. package/esm/config/markspec.js +216 -0
  20. package/esm/config/mod.d.ts +48 -0
  21. package/esm/config/mod.d.ts.map +1 -0
  22. package/esm/config/mod.js +243 -0
  23. package/esm/formatter/mod.d.ts +65 -0
  24. package/esm/formatter/mod.d.ts.map +1 -0
  25. package/esm/formatter/mod.js +306 -0
  26. package/esm/mod.d.ts +19 -0
  27. package/esm/mod.d.ts.map +1 -1
  28. package/esm/mod.js +18 -0
  29. package/esm/model/attributes.d.ts +47 -0
  30. package/esm/model/attributes.d.ts.map +1 -0
  31. package/esm/model/attributes.js +97 -0
  32. package/esm/model/mod.d.ts +381 -0
  33. package/esm/model/mod.d.ts.map +1 -0
  34. package/esm/model/mod.js +73 -0
  35. package/esm/model/profile.d.ts +189 -0
  36. package/esm/model/profile.d.ts.map +1 -0
  37. package/esm/model/profile.js +31 -0
  38. package/esm/parser/attributes.d.ts +48 -0
  39. package/esm/parser/attributes.d.ts.map +1 -0
  40. package/esm/parser/attributes.js +131 -0
  41. package/esm/parser/captions.d.ts +25 -0
  42. package/esm/parser/captions.d.ts.map +1 -0
  43. package/esm/parser/captions.js +148 -0
  44. package/esm/parser/directives.d.ts +29 -0
  45. package/esm/parser/directives.d.ts.map +1 -0
  46. package/esm/parser/directives.js +88 -0
  47. package/esm/parser/frontmatter.d.ts +53 -0
  48. package/esm/parser/frontmatter.d.ts.map +1 -0
  49. package/esm/parser/frontmatter.js +185 -0
  50. package/esm/parser/grammars.d.ts +22 -0
  51. package/esm/parser/grammars.d.ts.map +1 -0
  52. package/esm/parser/grammars.js +70 -0
  53. package/esm/parser/markdown.d.ts +32 -0
  54. package/esm/parser/markdown.d.ts.map +1 -0
  55. package/esm/parser/markdown.js +225 -0
  56. package/esm/parser/mod.d.ts +73 -0
  57. package/esm/parser/mod.d.ts.map +1 -0
  58. package/esm/parser/mod.js +89 -0
  59. package/esm/parser/references.d.ts +24 -0
  60. package/esm/parser/references.d.ts.map +1 -0
  61. package/esm/parser/references.js +71 -0
  62. package/esm/parser/remark.d.ts +9 -0
  63. package/esm/parser/remark.d.ts.map +1 -0
  64. package/esm/parser/remark.js +11 -0
  65. package/esm/parser/source.d.ts +33 -0
  66. package/esm/parser/source.d.ts.map +1 -0
  67. package/esm/parser/source.js +192 -0
  68. package/esm/profile/cache.d.ts +29 -0
  69. package/esm/profile/cache.d.ts.map +1 -0
  70. package/esm/profile/cache.js +45 -0
  71. package/esm/profile/chain.d.ts +42 -0
  72. package/esm/profile/chain.d.ts.map +1 -0
  73. package/esm/profile/chain.js +193 -0
  74. package/esm/profile/git-cache.d.ts +73 -0
  75. package/esm/profile/git-cache.d.ts.map +1 -0
  76. package/esm/profile/git-cache.js +96 -0
  77. package/esm/profile/load.d.ts +28 -0
  78. package/esm/profile/load.d.ts.map +1 -0
  79. package/esm/profile/load.js +50 -0
  80. package/esm/profile/manifest.d.ts +21 -0
  81. package/esm/profile/manifest.d.ts.map +1 -0
  82. package/esm/profile/manifest.js +830 -0
  83. package/esm/profile/merge.d.ts +26 -0
  84. package/esm/profile/merge.d.ts.map +1 -0
  85. package/esm/profile/merge.js +518 -0
  86. package/esm/profile/mod.d.ts +23 -0
  87. package/esm/profile/mod.d.ts.map +1 -0
  88. package/esm/profile/mod.js +14 -0
  89. package/esm/profile/npm.d.ts +51 -0
  90. package/esm/profile/npm.d.ts.map +1 -0
  91. package/esm/profile/npm.js +186 -0
  92. package/esm/profile/resolver.d.ts +60 -0
  93. package/esm/profile/resolver.d.ts.map +1 -0
  94. package/esm/profile/resolver.js +132 -0
  95. package/esm/reporter/mod.d.ts +31 -0
  96. package/esm/reporter/mod.d.ts.map +1 -0
  97. package/esm/reporter/mod.js +173 -0
  98. package/esm/validator/attributes.d.ts +43 -0
  99. package/esm/validator/attributes.d.ts.map +1 -0
  100. package/esm/validator/attributes.js +169 -0
  101. package/esm/validator/mod.d.ts +44 -0
  102. package/esm/validator/mod.d.ts.map +1 -0
  103. package/esm/validator/mod.js +214 -0
  104. package/esm/validator/normalize.d.ts +21 -0
  105. package/esm/validator/normalize.d.ts.map +1 -0
  106. package/esm/validator/normalize.js +58 -0
  107. package/esm/validator/pattern.d.ts +23 -0
  108. package/esm/validator/pattern.d.ts.map +1 -0
  109. package/esm/validator/pattern.js +44 -0
  110. package/esm/validator/pipeline.d.ts +30 -0
  111. package/esm/validator/pipeline.d.ts.map +1 -0
  112. package/esm/validator/pipeline.js +115 -0
  113. package/esm/validator/traceability.d.ts +48 -0
  114. package/esm/validator/traceability.d.ts.map +1 -0
  115. package/esm/validator/traceability.js +141 -0
  116. package/esm/validator/types.d.ts +48 -0
  117. package/esm/validator/types.d.ts.map +1 -0
  118. package/esm/validator/types.js +140 -0
  119. package/esm/validator/value_types.d.ts +20 -0
  120. package/esm/validator/value_types.d.ts.map +1 -0
  121. package/esm/validator/value_types.js +126 -0
  122. package/package.json +8 -1
  123. package/script/_dnt.polyfills.d.ts +101 -0
  124. package/script/_dnt.polyfills.d.ts.map +1 -0
  125. package/script/_dnt.polyfills.js +130 -0
  126. package/script/compiler/inverses.d.ts +22 -0
  127. package/script/compiler/inverses.d.ts.map +1 -0
  128. package/script/compiler/inverses.js +150 -0
  129. package/script/compiler/link_target.d.ts +19 -0
  130. package/script/compiler/link_target.d.ts.map +1 -0
  131. package/script/compiler/link_target.js +69 -0
  132. package/script/compiler/mod.d.ts +43 -0
  133. package/script/compiler/mod.d.ts.map +1 -0
  134. package/script/compiler/mod.js +169 -0
  135. package/script/compiler/schema.d.ts +38 -0
  136. package/script/compiler/schema.d.ts.map +1 -0
  137. package/script/compiler/schema.js +47 -0
  138. package/script/config/markspec.d.ts +51 -0
  139. package/script/config/markspec.d.ts.map +1 -0
  140. package/script/config/markspec.js +222 -0
  141. package/script/config/mod.d.ts +48 -0
  142. package/script/config/mod.d.ts.map +1 -0
  143. package/script/config/mod.js +250 -0
  144. package/script/formatter/mod.d.ts +65 -0
  145. package/script/formatter/mod.d.ts.map +1 -0
  146. package/script/formatter/mod.js +313 -0
  147. package/script/mod.d.ts +19 -0
  148. package/script/mod.d.ts.map +1 -1
  149. package/script/mod.js +65 -2
  150. package/script/model/attributes.d.ts +47 -0
  151. package/script/model/attributes.d.ts.map +1 -0
  152. package/script/model/attributes.js +101 -0
  153. package/script/model/mod.d.ts +381 -0
  154. package/script/model/mod.d.ts.map +1 -0
  155. package/script/model/mod.js +83 -0
  156. package/script/model/profile.d.ts +189 -0
  157. package/script/model/profile.d.ts.map +1 -0
  158. package/script/model/profile.js +34 -0
  159. package/script/parser/attributes.d.ts +48 -0
  160. package/script/parser/attributes.d.ts.map +1 -0
  161. package/script/parser/attributes.js +137 -0
  162. package/script/parser/captions.d.ts +25 -0
  163. package/script/parser/captions.d.ts.map +1 -0
  164. package/script/parser/captions.js +151 -0
  165. package/script/parser/directives.d.ts +29 -0
  166. package/script/parser/directives.d.ts.map +1 -0
  167. package/script/parser/directives.js +91 -0
  168. package/script/parser/frontmatter.d.ts +53 -0
  169. package/script/parser/frontmatter.d.ts.map +1 -0
  170. package/script/parser/frontmatter.js +188 -0
  171. package/script/parser/grammars.d.ts +22 -0
  172. package/script/parser/grammars.d.ts.map +1 -0
  173. package/script/parser/grammars.js +77 -0
  174. package/script/parser/markdown.d.ts +32 -0
  175. package/script/parser/markdown.d.ts.map +1 -0
  176. package/script/parser/markdown.js +228 -0
  177. package/script/parser/mod.d.ts +73 -0
  178. package/script/parser/mod.d.ts.map +1 -0
  179. package/script/parser/mod.js +101 -0
  180. package/script/parser/references.d.ts +24 -0
  181. package/script/parser/references.d.ts.map +1 -0
  182. package/script/parser/references.js +74 -0
  183. package/script/parser/remark.d.ts +9 -0
  184. package/script/parser/remark.d.ts.map +1 -0
  185. package/script/parser/remark.js +17 -0
  186. package/script/parser/source.d.ts +33 -0
  187. package/script/parser/source.d.ts.map +1 -0
  188. package/script/parser/source.js +198 -0
  189. package/script/profile/cache.d.ts +29 -0
  190. package/script/profile/cache.d.ts.map +1 -0
  191. package/script/profile/cache.js +48 -0
  192. package/script/profile/chain.d.ts +42 -0
  193. package/script/profile/chain.d.ts.map +1 -0
  194. package/script/profile/chain.js +229 -0
  195. package/script/profile/git-cache.d.ts +73 -0
  196. package/script/profile/git-cache.d.ts.map +1 -0
  197. package/script/profile/git-cache.js +104 -0
  198. package/script/profile/load.d.ts +28 -0
  199. package/script/profile/load.d.ts.map +1 -0
  200. package/script/profile/load.js +53 -0
  201. package/script/profile/manifest.d.ts +21 -0
  202. package/script/profile/manifest.d.ts.map +1 -0
  203. package/script/profile/manifest.js +833 -0
  204. package/script/profile/merge.d.ts +26 -0
  205. package/script/profile/merge.d.ts.map +1 -0
  206. package/script/profile/merge.js +521 -0
  207. package/script/profile/mod.d.ts +23 -0
  208. package/script/profile/mod.d.ts.map +1 -0
  209. package/script/profile/mod.js +31 -0
  210. package/script/profile/npm.d.ts +51 -0
  211. package/script/profile/npm.d.ts.map +1 -0
  212. package/script/profile/npm.js +191 -0
  213. package/script/profile/resolver.d.ts +60 -0
  214. package/script/profile/resolver.d.ts.map +1 -0
  215. package/script/profile/resolver.js +136 -0
  216. package/script/reporter/mod.d.ts +31 -0
  217. package/script/reporter/mod.d.ts.map +1 -0
  218. package/script/reporter/mod.js +176 -0
  219. package/script/validator/attributes.d.ts +43 -0
  220. package/script/validator/attributes.d.ts.map +1 -0
  221. package/script/validator/attributes.js +173 -0
  222. package/script/validator/mod.d.ts +44 -0
  223. package/script/validator/mod.d.ts.map +1 -0
  224. package/script/validator/mod.js +231 -0
  225. package/script/validator/normalize.d.ts +21 -0
  226. package/script/validator/normalize.d.ts.map +1 -0
  227. package/script/validator/normalize.js +61 -0
  228. package/script/validator/pattern.d.ts +23 -0
  229. package/script/validator/pattern.d.ts.map +1 -0
  230. package/script/validator/pattern.js +47 -0
  231. package/script/validator/pipeline.d.ts +30 -0
  232. package/script/validator/pipeline.d.ts.map +1 -0
  233. package/script/validator/pipeline.js +118 -0
  234. package/script/validator/traceability.d.ts +48 -0
  235. package/script/validator/traceability.d.ts.map +1 -0
  236. package/script/validator/traceability.js +146 -0
  237. package/script/validator/types.d.ts +48 -0
  238. package/script/validator/types.d.ts.map +1 -0
  239. package/script/validator/types.js +144 -0
  240. package/script/validator/value_types.d.ts +20 -0
  241. package/script/validator/value_types.d.ts.map +1 -0
  242. package/script/validator/value_types.js +129 -0
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @module compiler
3
+ *
4
+ * Compiler pipeline. Takes file paths, parses all entries, validates them,
5
+ * builds a bidirectional traceability graph, and returns the compiled model.
6
+ */
7
+ import { parseFile } from "../parser/mod.js";
8
+ import { classifyEntriesStage, validate } from "../validator/mod.js";
9
+ import { generateInverses } from "./inverses.js";
10
+ import { checkLinkTargets } from "./link_target.js";
11
+ /**
12
+ * Compile MarkSpec files from the given paths into a resolved
13
+ * traceability graph.
14
+ *
15
+ * @param paths - File paths to compile
16
+ * @param options - Compile options
17
+ * @returns Compiled entries, links, and diagnostics
18
+ */
19
+ export async function compile(paths, options) {
20
+ const read = options.readFile;
21
+ const allEntries = [];
22
+ const parseDiagnostics = [];
23
+ const documents = new Map();
24
+ // Phase 1: Read and parse all files.
25
+ for (const filePath of paths) {
26
+ let content;
27
+ try {
28
+ content = await read(filePath);
29
+ }
30
+ catch {
31
+ parseDiagnostics.push({
32
+ code: "MSL-E000",
33
+ severity: "error",
34
+ message: `failed to read file: ${filePath}`,
35
+ location: undefined,
36
+ });
37
+ continue;
38
+ }
39
+ const result = await parseFile(content, { file: filePath });
40
+ allEntries.push(...result.entries);
41
+ parseDiagnostics.push(...result.diagnostics);
42
+ if (result.document)
43
+ documents.set(filePath, result.document);
44
+ }
45
+ // Phase 2: Validate all entries.
46
+ const validationResult = validate(allEntries);
47
+ // Phase 2.5: Classify entries when a profile is loaded.
48
+ let classifiedEntries = allEntries;
49
+ if (options.profile) {
50
+ const stage2 = classifyEntriesStage(allEntries, options.profile);
51
+ classifiedEntries = stage2.entries;
52
+ parseDiagnostics.push(...stage2.diagnostics);
53
+ }
54
+ // Phase 3: Build traceability graph.
55
+ // Keep first occurrence of each display ID (validator catches duplicates).
56
+ const entries = new Map();
57
+ for (const entry of classifiedEntries) {
58
+ if (!entries.has(entry.displayId)) {
59
+ entries.set(entry.displayId, entry);
60
+ }
61
+ }
62
+ // Phase 3.5: Generate inverse attributes from profile declarations.
63
+ if (options.profile) {
64
+ const inverseResult = generateInverses([...entries.values()], options.profile);
65
+ parseDiagnostics.push(...inverseResult.diagnostics);
66
+ entries.clear();
67
+ for (const entry of inverseResult.entries) {
68
+ if (!entries.has(entry.displayId)) {
69
+ entries.set(entry.displayId, entry);
70
+ }
71
+ }
72
+ }
73
+ const links = extractLinks([...entries.values()]);
74
+ // MSL-T013: check link targets for draft/retired state.
75
+ const linkTargetDiags = checkLinkTargets(entries, links);
76
+ const forward = buildAdjacency(links, (l) => l.from);
77
+ const reverse = buildAdjacency(links, (l) => l.to);
78
+ const diagnostics = [
79
+ ...parseDiagnostics,
80
+ ...validationResult.diagnostics,
81
+ ...linkTargetDiags,
82
+ ];
83
+ return { entries, links, forward, reverse, documents, diagnostics };
84
+ }
85
+ /** Extract traceability links from entry attributes. */
86
+ function extractLinks(entries) {
87
+ const links = [];
88
+ for (const entry of entries) {
89
+ for (const attr of entry.rawAttributes) {
90
+ const extracted = extractLinksFromAttribute(entry.displayId, attr.key, attr.value, entry.location);
91
+ links.push(...extracted);
92
+ }
93
+ }
94
+ return links;
95
+ }
96
+ /**
97
+ * Attributes whose value is `slug + optional free-text locator` per
98
+ * ADR-002 §2.6 (citation type + legacy Derived-from tolerance). The link
99
+ * target is the leading non-whitespace token only.
100
+ */
101
+ const LOCATOR_BEARING_ATTRS = new Set([
102
+ "Derived-from",
103
+ "References",
104
+ ]);
105
+ /** Map an attribute key+value to zero or more links. */
106
+ function extractLinksFromAttribute(from, key, value, location) {
107
+ const kind = ATTR_TO_LINK_KIND[key];
108
+ if (!kind)
109
+ return [];
110
+ if (LOCATOR_BEARING_ATTRS.has(key)) {
111
+ // Format: "ID §section" — extract ID part only.
112
+ const idPart = value.split(/\s/)[0];
113
+ if (idPart) {
114
+ return [{ from, to: idPart, kind, location }];
115
+ }
116
+ return [];
117
+ }
118
+ // Comma-separated targets for id-list attributes.
119
+ return value
120
+ .split(",")
121
+ .map((s) => s.trim())
122
+ .filter((s) => s.length > 0)
123
+ .map((to) => ({ from, to, kind, location }));
124
+ }
125
+ /**
126
+ * Attribute keys that produce traceability links per ADR-002.
127
+ *
128
+ * Spec attributes: Satisfies, Derived-from, References, Allocated-to.
129
+ * Test attributes: Verifies, Tests.
130
+ * Element attributes: Realizes, Depends-on, Part-of, Generated-from.
131
+ * Universal: Supersedes (same-family).
132
+ */
133
+ const ATTR_TO_LINK_KIND = {
134
+ "Satisfies": "satisfies",
135
+ "Derived-from": "derived-from",
136
+ "References": "references",
137
+ "Allocated-to": "allocated-to",
138
+ "Realizes": "realizes",
139
+ "Verifies": "verifies",
140
+ "Tests": "tests",
141
+ "Depends-on": "depends-on",
142
+ "Part-of": "part-of",
143
+ "Generated-from": "generated-from",
144
+ "Supersedes": "supersedes",
145
+ };
146
+ /** Build an adjacency map from links using a key selector. */
147
+ function buildAdjacency(links, keyFn) {
148
+ const map = new Map();
149
+ for (const link of links) {
150
+ const key = keyFn(link);
151
+ let list = map.get(key);
152
+ if (!list) {
153
+ list = [];
154
+ map.set(key, list);
155
+ }
156
+ list.push(link);
157
+ }
158
+ return map;
159
+ }
160
+ // Re-export serialization helper.
161
+ export { serializeCompileResult } from "./schema.js";
162
+ // Re-export inverse generation.
163
+ export { generateInverses } from "./inverses.js";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @module schema
3
+ *
4
+ * Serialization helper that converts {@linkcode CompileResult} (which uses
5
+ * Maps) to a plain JSON-serializable object suitable for export and
6
+ * interchange.
7
+ */
8
+ import type { CompileResult } from "./mod.js";
9
+ import type { Diagnostic, Entry, Link } from "../model/mod.js";
10
+ /**
11
+ * Serialized form of {@linkcode CompileResult}.
12
+ *
13
+ * All `ReadonlyMap` fields are converted to plain objects keyed by display ID.
14
+ * Arrays and scalar fields are passed through unchanged.
15
+ */
16
+ export interface SerializedCompileResult {
17
+ /** Entries keyed by display ID. */
18
+ readonly entries: Record<string, Entry>;
19
+ /** All traceability links. */
20
+ readonly links: readonly Link[];
21
+ /** Outgoing links per entry (entry -> targets). */
22
+ readonly forward: Record<string, readonly Link[]>;
23
+ /** Incoming links per entry (entry -> sources pointing to it). */
24
+ readonly reverse: Record<string, readonly Link[]>;
25
+ /** Diagnostics from parsing and validation. */
26
+ readonly diagnostics: readonly Diagnostic[];
27
+ }
28
+ /**
29
+ * Convert a {@linkcode CompileResult} to a plain JSON-serializable object.
30
+ *
31
+ * Maps are converted to `Record` objects keyed by display ID; arrays and
32
+ * scalars pass through unchanged.
33
+ *
34
+ * @param result - The compiled project output
35
+ * @returns A plain object safe for `JSON.stringify`
36
+ */
37
+ export declare function serializeCompileResult(result: CompileResult): SerializedCompileResult;
38
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/compiler/schema.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE/D;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,mCAAmC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACxC,8BAA8B;IAC9B,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC;IAChC,mDAAmD;IACnD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,IAAI,EAAE,CAAC,CAAC;IAClD,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,IAAI,EAAE,CAAC,CAAC;IAClD,+CAA+C;IAC/C,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;CAC7C;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,aAAa,GACpB,uBAAuB,CAazB"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @module schema
3
+ *
4
+ * Serialization helper that converts {@linkcode CompileResult} (which uses
5
+ * Maps) to a plain JSON-serializable object suitable for export and
6
+ * interchange.
7
+ */
8
+ /**
9
+ * Convert a {@linkcode CompileResult} to a plain JSON-serializable object.
10
+ *
11
+ * Maps are converted to `Record` objects keyed by display ID; arrays and
12
+ * scalars pass through unchanged.
13
+ *
14
+ * @param result - The compiled project output
15
+ * @returns A plain object safe for `JSON.stringify`
16
+ */
17
+ export function serializeCompileResult(result) {
18
+ const rawEntries = Object.fromEntries(result.entries);
19
+ const entries = {};
20
+ for (const [key, entry] of Object.entries(rawEntries)) {
21
+ entries[key] = serializeEntry(entry);
22
+ }
23
+ return {
24
+ entries,
25
+ links: result.links,
26
+ forward: Object.fromEntries(result.forward),
27
+ reverse: Object.fromEntries(result.reverse),
28
+ diagnostics: result.diagnostics,
29
+ };
30
+ }
31
+ /**
32
+ * Convert an {@linkcode Entry} to a JSON-safe form by replacing
33
+ * `typedAttributes` (a `ReadonlyMap`) with a plain object.
34
+ */
35
+ function serializeEntry(entry) {
36
+ if (!entry.typedAttributes || entry.typedAttributes.size === 0) {
37
+ return entry;
38
+ }
39
+ const typedObj = Object.fromEntries(entry.typedAttributes);
40
+ return {
41
+ ...entry,
42
+ typedAttributes: typedObj,
43
+ };
44
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * @module core/config/markspec
3
+ *
4
+ * Load and validate `.markspec.yaml` — the consumer-project binding that
5
+ * declares which profiles the project uses.
6
+ *
7
+ * Emits diagnostics (MARKSPEC-YAML-*) on parse or schema errors. See
8
+ * [spec §7.6](../../../../docs/superpowers/specs/2026-04-21-adr-008-profile-system-v1-design.md).
9
+ */
10
+ import type { Diagnostic, ProfileSpecifier } from "../model/mod.js";
11
+ import type { ReadFile } from "./mod.js";
12
+ /** The consumer-binding config filename, placed next to `project.yaml`. */
13
+ export declare const MARKSPEC_YAML_FILENAME = ".markspec.yaml";
14
+ /**
15
+ * Read a `.markspec.yaml` file at the given project root.
16
+ *
17
+ * @param projectRoot - Absolute path to the directory containing `project.yaml`
18
+ * @param readFile - File reader (returns `undefined` when missing)
19
+ * @returns Raw file contents, or `null` when the file is absent
20
+ */
21
+ export declare function readMarkspecYaml(projectRoot: string, readFile: ReadFile): Promise<string | null>;
22
+ /** The parsed content of a `.markspec.yaml`. */
23
+ export interface MarkspecYaml {
24
+ readonly profiles: readonly ProfileSpecifier[];
25
+ }
26
+ /** Result of parsing a `.markspec.yaml` string. */
27
+ export interface ParseMarkspecYamlResult {
28
+ readonly config: MarkspecYaml | null;
29
+ readonly diagnostics: readonly Diagnostic[];
30
+ }
31
+ /**
32
+ * Parse and validate a `.markspec.yaml` string.
33
+ *
34
+ * - `MARKSPEC-YAML-002` — YAML parse error.
35
+ * - `MARKSPEC-YAML-003` — schema error (wrong type, bad specifier).
36
+ * - `MARKSPEC-YAML-001` — unknown top-level key (warning; config still produced).
37
+ */
38
+ export declare function parseMarkspecYaml(rawYaml: string, sourcePath: string): ParseMarkspecYamlResult;
39
+ /** Injectable file writer for addProfileSpecifier. */
40
+ export type WriteFile = (path: string, content: string) => Promise<void>;
41
+ /**
42
+ * Append a profile specifier to `.markspec.yaml`.
43
+ *
44
+ * - File absent: creates with `profiles:\n - <specifier>\n`.
45
+ * - File exists with `profiles:` key: appends after the last list entry.
46
+ * - File exists without `profiles:` key: appends the block at end of file.
47
+ *
48
+ * Uses raw string manipulation to preserve user comments and formatting.
49
+ */
50
+ export declare function addProfileSpecifier(specifier: string, readFileFn: ReadFile, writeFileFn: WriteFile, projectRoot: string): Promise<void>;
51
+ //# sourceMappingURL=markspec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markspec.d.ts","sourceRoot":"","sources":["../../src/config/markspec.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACpE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB,mBAAmB,CAAC;AAEvD;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CACpC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,QAAQ,GACjB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAIxB;AAMD,gDAAgD;AAChD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,QAAQ,EAAE,SAAS,gBAAgB,EAAE,CAAC;CAChD;AAED,mDAAmD;AACnD,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IACrC,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;CAC7C;AAID;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,GACjB,uBAAuB,CA8EzB;AAgFD,sDAAsD;AACtD,MAAM,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzE;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,QAAQ,EACpB,WAAW,EAAE,SAAS,EACtB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC,CAiDf"}
@@ -0,0 +1,216 @@
1
+ /**
2
+ * @module core/config/markspec
3
+ *
4
+ * Load and validate `.markspec.yaml` — the consumer-project binding that
5
+ * declares which profiles the project uses.
6
+ *
7
+ * Emits diagnostics (MARKSPEC-YAML-*) on parse or schema errors. See
8
+ * [spec §7.6](../../../../docs/superpowers/specs/2026-04-21-adr-008-profile-system-v1-design.md).
9
+ */
10
+ import { parse as parseYaml } from "@std/yaml";
11
+ import { join } from "@std/path";
12
+ /** The consumer-binding config filename, placed next to `project.yaml`. */
13
+ export const MARKSPEC_YAML_FILENAME = ".markspec.yaml";
14
+ /**
15
+ * Read a `.markspec.yaml` file at the given project root.
16
+ *
17
+ * @param projectRoot - Absolute path to the directory containing `project.yaml`
18
+ * @param readFile - File reader (returns `undefined` when missing)
19
+ * @returns Raw file contents, or `null` when the file is absent
20
+ */
21
+ export async function readMarkspecYaml(projectRoot, readFile) {
22
+ const path = join(projectRoot, MARKSPEC_YAML_FILENAME);
23
+ const content = await readFile(path);
24
+ return content ?? null;
25
+ }
26
+ const ALLOWED_MARKSPEC_YAML_KEYS = new Set(["profiles"]);
27
+ /**
28
+ * Parse and validate a `.markspec.yaml` string.
29
+ *
30
+ * - `MARKSPEC-YAML-002` — YAML parse error.
31
+ * - `MARKSPEC-YAML-003` — schema error (wrong type, bad specifier).
32
+ * - `MARKSPEC-YAML-001` — unknown top-level key (warning; config still produced).
33
+ */
34
+ export function parseMarkspecYaml(rawYaml, sourcePath) {
35
+ const diagnostics = [];
36
+ // Empty file is equivalent to `profiles: []`
37
+ const trimmed = rawYaml.trim();
38
+ if (trimmed.length === 0) {
39
+ return { config: { profiles: [] }, diagnostics };
40
+ }
41
+ let parsed;
42
+ try {
43
+ parsed = parseYaml(rawYaml);
44
+ }
45
+ catch (err) {
46
+ const message = err instanceof Error ? err.message : String(err);
47
+ diagnostics.push({
48
+ code: "MARKSPEC-YAML-002",
49
+ severity: "error",
50
+ message: `.markspec.yaml: YAML parse error: ${message}`,
51
+ location: { file: sourcePath, line: 1, column: 1 },
52
+ });
53
+ return { config: null, diagnostics };
54
+ }
55
+ if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) {
56
+ diagnostics.push({
57
+ code: "MARKSPEC-YAML-003",
58
+ severity: "error",
59
+ message: ".markspec.yaml must be a YAML mapping",
60
+ location: { file: sourcePath, line: 1, column: 1 },
61
+ });
62
+ return { config: null, diagnostics };
63
+ }
64
+ const root = parsed;
65
+ // Unknown keys — warn, but continue parsing.
66
+ for (const key of Object.keys(root)) {
67
+ if (!ALLOWED_MARKSPEC_YAML_KEYS.has(key)) {
68
+ diagnostics.push({
69
+ code: "MARKSPEC-YAML-001",
70
+ severity: "warning",
71
+ message: `.markspec.yaml: unknown top-level key '${key}'`,
72
+ location: { file: sourcePath, line: 1, column: 1 },
73
+ });
74
+ }
75
+ }
76
+ const rawProfiles = root.profiles;
77
+ if (rawProfiles !== undefined && !Array.isArray(rawProfiles)) {
78
+ diagnostics.push({
79
+ code: "MARKSPEC-YAML-003",
80
+ severity: "error",
81
+ message: ".markspec.yaml: 'profiles' must be a list",
82
+ location: { file: sourcePath, line: 1, column: 1 },
83
+ });
84
+ return { config: null, diagnostics };
85
+ }
86
+ const profiles = [];
87
+ if (Array.isArray(rawProfiles)) {
88
+ for (let i = 0; i < rawProfiles.length; i++) {
89
+ const spec = parseProfileSpecifier(rawProfiles[i], `.markspec.yaml: profiles[${i}]`, sourcePath, diagnostics);
90
+ if (spec)
91
+ profiles.push(spec);
92
+ }
93
+ }
94
+ // If any specifier failed to parse, treat the whole file as invalid.
95
+ const hasErrors = diagnostics.some((d) => d.severity === "error");
96
+ if (hasErrors) {
97
+ return { config: null, diagnostics };
98
+ }
99
+ return { config: { profiles }, diagnostics };
100
+ }
101
+ /**
102
+ * Parse a single specifier string into a {@linkcode ProfileSpecifier}.
103
+ * Emits `MARKSPEC-YAML-003` on malformed input.
104
+ */
105
+ function parseProfileSpecifier(raw, context, sourcePath, diagnostics) {
106
+ if (typeof raw !== "string" || raw.length === 0) {
107
+ diagnostics.push({
108
+ code: "MARKSPEC-YAML-003",
109
+ severity: "error",
110
+ message: `${context}: specifier must be a non-empty string`,
111
+ location: { file: sourcePath, line: 1, column: 1 },
112
+ });
113
+ return undefined;
114
+ }
115
+ if (raw.startsWith("./") || raw.startsWith("../")) {
116
+ return { kind: "local", path: raw };
117
+ }
118
+ if (raw.startsWith("git+")) {
119
+ const m = /^git\+((?:https?|file):\/\/[^#]+?\.git)(\/[^#]+)?#(.+)$/.exec(raw);
120
+ if (!m) {
121
+ diagnostics.push({
122
+ code: "MARKSPEC-YAML-003",
123
+ severity: "error",
124
+ message: `${context}: git specifier malformed; expected git+<https|file>://host/.git[/subpath]#<tag>`,
125
+ location: { file: sourcePath, line: 1, column: 1 },
126
+ });
127
+ return undefined;
128
+ }
129
+ const [, repo, rawSubpath, tag] = m;
130
+ const subpath = rawSubpath ? rawSubpath.slice(1) : undefined;
131
+ return { kind: "git", repo, subpath, tag };
132
+ }
133
+ if (raw.startsWith("npm:")) {
134
+ // npm:@scope/name@range or npm:name@range
135
+ const body = raw.slice(4); // strip "npm:"
136
+ const scopedMatch = /^(@[a-z0-9-]+\/[a-z0-9-]+)@(.+)$/.exec(body);
137
+ if (scopedMatch) {
138
+ const [, fullName, range] = scopedMatch;
139
+ const slashIdx = fullName.indexOf("/");
140
+ return {
141
+ kind: "npm",
142
+ scope: fullName.slice(0, slashIdx),
143
+ name: fullName.slice(slashIdx + 1),
144
+ range,
145
+ };
146
+ }
147
+ const unscopedMatch = /^([a-z0-9-]+)@(.+)$/.exec(body);
148
+ if (unscopedMatch) {
149
+ const [, name, range] = unscopedMatch;
150
+ return { kind: "npm", name, range };
151
+ }
152
+ diagnostics.push({
153
+ code: "MARKSPEC-YAML-003",
154
+ severity: "error",
155
+ message: `${context}: npm specifier malformed; expected npm:[@scope/]name@<version-range>`,
156
+ location: { file: sourcePath, line: 1, column: 1 },
157
+ });
158
+ return undefined;
159
+ }
160
+ diagnostics.push({
161
+ code: "MARKSPEC-YAML-003",
162
+ severity: "error",
163
+ message: `${context}: unsupported specifier scheme (use './path', 'git+<https|file>://…#<tag>', or 'npm:[@scope/]name@<range>')`,
164
+ location: { file: sourcePath, line: 1, column: 1 },
165
+ });
166
+ return undefined;
167
+ }
168
+ /**
169
+ * Append a profile specifier to `.markspec.yaml`.
170
+ *
171
+ * - File absent: creates with `profiles:\n - <specifier>\n`.
172
+ * - File exists with `profiles:` key: appends after the last list entry.
173
+ * - File exists without `profiles:` key: appends the block at end of file.
174
+ *
175
+ * Uses raw string manipulation to preserve user comments and formatting.
176
+ */
177
+ export async function addProfileSpecifier(specifier, readFileFn, writeFileFn, projectRoot) {
178
+ const filePath = join(projectRoot, MARKSPEC_YAML_FILENAME);
179
+ const existing = await readFileFn(filePath);
180
+ if (existing === undefined) {
181
+ await writeFileFn(filePath, `profiles:\n - "${specifier}"\n`);
182
+ return;
183
+ }
184
+ const profilesIdx = existing.indexOf("profiles:");
185
+ if (profilesIdx === -1) {
186
+ const trailing = existing.endsWith("\n") ? "" : "\n";
187
+ await writeFileFn(filePath, existing + trailing + `\nprofiles:\n - "${specifier}"\n`);
188
+ return;
189
+ }
190
+ const lines = existing.split("\n");
191
+ let lastEntryLine = -1;
192
+ let inProfiles = false;
193
+ for (let i = 0; i < lines.length; i++) {
194
+ if (lines[i].trimStart().startsWith("profiles:")) {
195
+ inProfiles = true;
196
+ continue;
197
+ }
198
+ if (inProfiles) {
199
+ if (/^\s+-\s/.test(lines[i])) {
200
+ lastEntryLine = i;
201
+ }
202
+ else if (lines[i].trim().length > 0 && !lines[i].startsWith(" ") &&
203
+ !lines[i].startsWith("#")) {
204
+ break;
205
+ }
206
+ }
207
+ }
208
+ if (lastEntryLine >= 0) {
209
+ lines.splice(lastEntryLine + 1, 0, ` - "${specifier}"`);
210
+ }
211
+ else {
212
+ const profilesLine = lines.findIndex((l) => l.trimStart().startsWith("profiles:"));
213
+ lines.splice(profilesLine + 1, 0, ` - "${specifier}"`);
214
+ }
215
+ await writeFileFn(filePath, lines.join("\n"));
216
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @module config
3
+ *
4
+ * Project discovery and configuration loading. Walks up from CWD to
5
+ * find `project.yaml`, parses the YAML, and validates the schema.
6
+ */
7
+ import { type ProjectConfig } from "../model/mod.js";
8
+ /** The config file name used as the project root marker. */
9
+ export declare const CONFIG_FILE_NAME = "project.yaml";
10
+ /** Result of loading a project configuration. */
11
+ export interface LoadConfigResult {
12
+ /** The parsed and validated configuration. */
13
+ readonly config: ProjectConfig;
14
+ /** Absolute path to the project root directory. */
15
+ readonly projectRoot: string;
16
+ /** Absolute path to the `project.yaml` file. */
17
+ readonly configPath: string;
18
+ }
19
+ /** A function that reads a file and returns its content, or `undefined` if not found. */
20
+ export type ReadFile = (path: string) => Promise<string | undefined>;
21
+ /**
22
+ * Walk up from `startDir` to the filesystem root looking for `project.yaml`.
23
+ *
24
+ * @returns Absolute path to the directory containing `project.yaml`,
25
+ * or `undefined` if not found.
26
+ */
27
+ export declare function discoverProjectRoot(startDir: string, readFile: ReadFile): Promise<string | undefined>;
28
+ /**
29
+ * Parse a YAML string and validate it as a MarkSpec project configuration.
30
+ *
31
+ * @param yaml - Raw YAML content
32
+ * @param filePath - Path to the file (for error messages)
33
+ * @returns Validated `ProjectConfig`
34
+ * @throws {ConfigError} on invalid content
35
+ */
36
+ export declare function parseProjectConfig(yaml: string, filePath: string): ProjectConfig;
37
+ /**
38
+ * Derive a 3-6 letter project domain abbreviation from the project name.
39
+ */
40
+ export declare function deriveDomain(projectName: string): string;
41
+ /**
42
+ * Discover `project.yaml` by walking up from `startDir`, load, and validate.
43
+ *
44
+ * @returns `LoadConfigResult` if found and valid, `undefined` if not found.
45
+ * @throws {ConfigError} if `project.yaml` exists but is invalid.
46
+ */
47
+ export declare function loadConfig(startDir: string, readFile: ReadFile): Promise<LoadConfigResult | undefined>;
48
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/config/mod.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAIL,KAAK,aAAa,EACnB,MAAM,iBAAiB,CAAC;AAEzB,4DAA4D;AAC5D,eAAO,MAAM,gBAAgB,iBAAiB,CAAC;AAE/C,iDAAiD;AACjD,MAAM,WAAW,gBAAgB;IAC/B,8CAA8C;IAC9C,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,mDAAmD;IACnD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,gDAAgD;IAChD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,yFAAyF;AACzF,MAAM,MAAM,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AAMrE;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,QAAQ,GACjB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAU7B;AAsBD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GACf,aAAa,CAwIf;AAMD;;GAEG;AACH,wBAAgB,YAAY,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAiBxD;AAMD;;;;;GAKG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,QAAQ,GACjB,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAUvC"}