@nimiplatform/nimi-coding 0.2.8 → 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 (205) hide show
  1. package/CHANGELOG.md +8 -92
  2. package/CONTRIBUTING.md +5 -34
  3. package/README.md +47 -350
  4. package/README.zh-CN.md +33 -287
  5. package/cli/commands/start.mjs +48 -688
  6. package/cli/commands/validate-spec-audit.mjs +1 -5
  7. package/cli/constants.mjs +9 -417
  8. package/cli/help.mjs +28 -145
  9. package/cli/index.mjs +5 -33
  10. package/cli/lib/blueprint-audit.mjs +5 -12
  11. package/cli/lib/bootstrap.mjs +0 -9
  12. package/cli/lib/contracts.mjs +0 -163
  13. package/cli/lib/doctor.mjs +155 -10
  14. package/cli/lib/entrypoints.mjs +63 -167
  15. package/cli/lib/internal/contracts-loaders.mjs +0 -85
  16. package/cli/lib/internal/contracts-parse.mjs +84 -520
  17. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +13 -8
  18. package/cli/lib/internal/surface-taxonomy-validators.mjs +316 -66
  19. package/cli/lib/internal/validators-shared.mjs +0 -16
  20. package/cli/lib/internal/validators-spec-helpers.mjs +7 -140
  21. package/cli/lib/internal/validators-spec.mjs +103 -342
  22. package/cli/lib/shared.mjs +1 -42
  23. package/cli/lib/validators.mjs +0 -48
  24. package/cli/seeds/seed-policy.yaml +0 -3
  25. package/config/bootstrap.yaml +4 -4
  26. package/config/spec-generation-inputs.yaml +6 -12
  27. package/contracts/domain-admission.schema.yaml +3 -3
  28. package/contracts/migration-inventory.schema.yaml +12 -75
  29. package/contracts/negative-fixtures.yaml +19 -99
  30. package/contracts/placement-contract.schema.yaml +49 -78
  31. package/contracts/projection-edge.schema.yaml +37 -121
  32. package/contracts/shared-enums.yaml +4 -8
  33. package/contracts/spec-generation-inputs.schema.yaml +15 -112
  34. package/contracts/spec-layout.schema.yaml +32 -0
  35. package/contracts/surface-taxonomy.schema.yaml +43 -94
  36. package/contracts/table-family.schema.yaml +1 -1
  37. package/contracts/tracked-output-admission.schema.yaml +13 -66
  38. package/methodology/core.yaml +33 -25
  39. package/methodology/four-closure-policy.yaml +18 -26
  40. package/methodology/role-separation-policy.yaml +21 -27
  41. package/methodology/spec-reconstruction.yaml +17 -34
  42. package/package.json +2 -4
  43. package/spec/_meta/spec-tree-model.yaml +5 -101
  44. package/spec/product-scope.yaml +25 -57
  45. package/adapters/README.md +0 -25
  46. package/adapters/claude/README.md +0 -89
  47. package/adapters/claude/profile.yaml +0 -70
  48. package/adapters/codex/README.md +0 -53
  49. package/adapters/codex/profile.yaml +0 -78
  50. package/adapters/oh-my-codex/README.md +0 -184
  51. package/adapters/oh-my-codex/profile.yaml +0 -46
  52. package/cli/commands/admit-high-risk-decision.mjs +0 -108
  53. package/cli/commands/audit-sweep.mjs +0 -364
  54. package/cli/commands/closeout.mjs +0 -186
  55. package/cli/commands/decide-high-risk-execution.mjs +0 -124
  56. package/cli/commands/handoff.mjs +0 -123
  57. package/cli/commands/ingest-high-risk-execution.mjs +0 -95
  58. package/cli/commands/review-high-risk-execution.mjs +0 -95
  59. package/cli/commands/sweep-design.mjs +0 -295
  60. package/cli/commands/sweep.mjs +0 -22
  61. package/cli/commands/topic-formatters.mjs +0 -382
  62. package/cli/commands/topic-goal.mjs +0 -33
  63. package/cli/commands/topic-options-shared.mjs +0 -27
  64. package/cli/commands/topic-options-workflow.mjs +0 -767
  65. package/cli/commands/topic-options.mjs +0 -626
  66. package/cli/commands/topic-runner.mjs +0 -169
  67. package/cli/commands/topic.mjs +0 -795
  68. package/cli/commands/validate-acceptance.mjs +0 -5
  69. package/cli/commands/validate-execution-packet.mjs +0 -5
  70. package/cli/commands/validate-orchestration-state.mjs +0 -5
  71. package/cli/commands/validate-prompt.mjs +0 -5
  72. package/cli/commands/validate-worker-output.mjs +0 -5
  73. package/cli/lib/adapter-profiles.mjs +0 -403
  74. package/cli/lib/audit-execution.mjs +0 -52
  75. package/cli/lib/audit-sweep-runtime/admissions.mjs +0 -508
  76. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +0 -356
  77. package/cli/lib/audit-sweep-runtime/chunks.mjs +0 -697
  78. package/cli/lib/audit-sweep-runtime/claude-auditor.mjs +0 -658
  79. package/cli/lib/audit-sweep-runtime/closeout.mjs +0 -144
  80. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +0 -654
  81. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +0 -527
  82. package/cli/lib/audit-sweep-runtime/common.mjs +0 -341
  83. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +0 -172
  84. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +0 -155
  85. package/cli/lib/audit-sweep-runtime/format.mjs +0 -57
  86. package/cli/lib/audit-sweep-runtime/ingest.mjs +0 -486
  87. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +0 -425
  88. package/cli/lib/audit-sweep-runtime/inventory.mjs +0 -798
  89. package/cli/lib/audit-sweep-runtime/ledger.mjs +0 -315
  90. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +0 -101
  91. package/cli/lib/audit-sweep-runtime/remediation.mjs +0 -349
  92. package/cli/lib/audit-sweep-runtime/rerun.mjs +0 -129
  93. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +0 -300
  94. package/cli/lib/audit-sweep-runtime/status.mjs +0 -62
  95. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +0 -215
  96. package/cli/lib/audit-sweep-runtime/validators.mjs +0 -797
  97. package/cli/lib/audit-sweep.mjs +0 -19
  98. package/cli/lib/authority-convergence.mjs +0 -720
  99. package/cli/lib/closeout.mjs +0 -732
  100. package/cli/lib/codex-sdk-runner.mjs +0 -76
  101. package/cli/lib/external-execution.mjs +0 -101
  102. package/cli/lib/handoff.mjs +0 -812
  103. package/cli/lib/high-risk-admission.mjs +0 -512
  104. package/cli/lib/high-risk-decision.mjs +0 -353
  105. package/cli/lib/high-risk-ingest.mjs +0 -321
  106. package/cli/lib/high-risk-review.mjs +0 -267
  107. package/cli/lib/internal/contracts-parse-high-risk.mjs +0 -131
  108. package/cli/lib/internal/contracts-validators.mjs +0 -399
  109. package/cli/lib/internal/doctor-bootstrap-surface.mjs +0 -406
  110. package/cli/lib/internal/doctor-delegated-surface.mjs +0 -256
  111. package/cli/lib/internal/doctor-finalize.mjs +0 -383
  112. package/cli/lib/internal/doctor-format.mjs +0 -286
  113. package/cli/lib/internal/doctor-inspectors.mjs +0 -327
  114. package/cli/lib/internal/doctor-state.mjs +0 -205
  115. package/cli/lib/internal/validators-artifacts.mjs +0 -515
  116. package/cli/lib/sweep-design-runtime/common.mjs +0 -246
  117. package/cli/lib/sweep-design-runtime/engine.mjs +0 -733
  118. package/cli/lib/sweep-design-runtime/fix-topic.mjs +0 -414
  119. package/cli/lib/sweep-design-runtime/lifecycle.mjs +0 -54
  120. package/cli/lib/sweep-design-runtime/results.mjs +0 -324
  121. package/cli/lib/sweep-design.mjs +0 -8
  122. package/cli/lib/topic-artifacts.mjs +0 -186
  123. package/cli/lib/topic-authority-coverage.mjs +0 -73
  124. package/cli/lib/topic-closeout.mjs +0 -560
  125. package/cli/lib/topic-common.mjs +0 -404
  126. package/cli/lib/topic-decisions.mjs +0 -332
  127. package/cli/lib/topic-draft-packets.mjs +0 -167
  128. package/cli/lib/topic-execution.mjs +0 -533
  129. package/cli/lib/topic-goal.mjs +0 -440
  130. package/cli/lib/topic-ledger.mjs +0 -281
  131. package/cli/lib/topic-lifecycle-artifacts.mjs +0 -173
  132. package/cli/lib/topic-root-validation.mjs +0 -288
  133. package/cli/lib/topic-runner-commands.mjs +0 -174
  134. package/cli/lib/topic-runner-deferral.mjs +0 -532
  135. package/cli/lib/topic-runner-stale-gates.mjs +0 -114
  136. package/cli/lib/topic-runner-validation.mjs +0 -138
  137. package/cli/lib/topic-runner.mjs +0 -727
  138. package/cli/lib/topic-scaffold.mjs +0 -252
  139. package/cli/lib/topic-waves.mjs +0 -403
  140. package/cli/lib/topic.mjs +0 -81
  141. package/config/audit-execution-artifacts.yaml +0 -20
  142. package/config/external-execution-artifacts.yaml +0 -16
  143. package/config/host-adapter.yaml +0 -30
  144. package/config/host-profile.yaml +0 -29
  145. package/config/installer-evidence.yaml +0 -31
  146. package/config/skill-installer.yaml +0 -23
  147. package/config/skill-manifest.yaml +0 -48
  148. package/config/skills.yaml +0 -30
  149. package/contracts/acceptance.schema.yaml +0 -16
  150. package/contracts/admission-checklist.schema.yaml +0 -15
  151. package/contracts/audit-chunk.schema.yaml +0 -123
  152. package/contracts/audit-closeout.schema.yaml +0 -51
  153. package/contracts/audit-finding.schema.yaml +0 -61
  154. package/contracts/audit-ledger.schema.yaml +0 -138
  155. package/contracts/audit-plan.schema.yaml +0 -138
  156. package/contracts/audit-remediation-map.schema.yaml +0 -52
  157. package/contracts/audit-rerun.schema.yaml +0 -31
  158. package/contracts/audit-sweep-result.yaml +0 -53
  159. package/contracts/authority-convergence-audit.schema.yaml +0 -19
  160. package/contracts/closeout.schema.yaml +0 -25
  161. package/contracts/decision-review.schema.yaml +0 -16
  162. package/contracts/doc-spec-audit-result.yaml +0 -19
  163. package/contracts/execution-packet.schema.yaml +0 -49
  164. package/contracts/external-host-compatibility.yaml +0 -22
  165. package/contracts/forbidden-shortcuts.catalog.yaml +0 -23
  166. package/contracts/high-risk-admission.schema.yaml +0 -23
  167. package/contracts/high-risk-execution-result.yaml +0 -20
  168. package/contracts/orchestration-state.schema.yaml +0 -41
  169. package/contracts/overflow-continuation.schema.yaml +0 -12
  170. package/contracts/packet.schema.yaml +0 -30
  171. package/contracts/pending-note.schema.yaml +0 -17
  172. package/contracts/prompt.schema.yaml +0 -12
  173. package/contracts/remediation.schema.yaml +0 -16
  174. package/contracts/result.schema.yaml +0 -24
  175. package/contracts/spec-reconstruction-result.yaml +0 -41
  176. package/contracts/sweep-design-result.yaml +0 -349
  177. package/contracts/topic-goal.schema.yaml +0 -87
  178. package/contracts/topic-run-ledger.schema.yaml +0 -72
  179. package/contracts/topic-step-decision.schema.yaml +0 -45
  180. package/contracts/topic.schema.yaml +0 -65
  181. package/contracts/true-close.schema.yaml +0 -15
  182. package/contracts/wave.schema.yaml +0 -29
  183. package/contracts/worker-output.schema.yaml +0 -15
  184. package/contracts/workflow-consumer.schema.yaml +0 -110
  185. package/methodology/audit-sweep-p0p1-recall.yaml +0 -45
  186. package/methodology/authority-convergence-policy.yaml +0 -42
  187. package/methodology/overflow-continuation-policy.yaml +0 -14
  188. package/methodology/skill-exchange-projection.yaml +0 -114
  189. package/methodology/skill-handoff.yaml +0 -34
  190. package/methodology/skill-installer-result.yaml +0 -27
  191. package/methodology/skill-installer-summary-projection.yaml +0 -181
  192. package/methodology/skill-runtime.yaml +0 -23
  193. package/methodology/spec-target-truth-profile.yaml +0 -46
  194. package/methodology/topic-lifecycle-report.yaml +0 -144
  195. package/methodology/topic-lifecycle.yaml +0 -37
  196. package/methodology/topic-naming-ontology.yaml +0 -21
  197. package/methodology/topic-ontology.yaml +0 -38
  198. package/methodology/topic-validation-policy.yaml +0 -9
  199. package/methodology/wave-dag-policy.yaml +0 -14
  200. package/spec/_meta/command-gating-matrix.yaml +0 -143
  201. package/spec/_meta/generate-drift-migration-checklist.yaml +0 -137
  202. package/spec/_meta/governance-routing-cutover-checklist.yaml +0 -35
  203. package/spec/_meta/phase2-impacted-surface-matrix.yaml +0 -44
  204. package/spec/_meta/spec-authority-cutover-readiness.yaml +0 -102
  205. package/spec/bootstrap-state.yaml +0 -99
@@ -1,425 +0,0 @@
1
- import path from "node:path";
2
-
3
- import { specSurfaceForFile } from "./admissions.mjs";
4
-
5
- function evidenceRootsForSpecOwner(ownerDomain, targetRootRef) {
6
- if (targetRootRef !== ".") {
7
- return [targetRootRef];
8
- }
9
- const owner = String(ownerDomain ?? "").trim().replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
10
- if (!owner || owner === "spec-meta" || owner === "spec-root") {
11
- return [];
12
- }
13
- if (owner === "project") {
14
- return ["src", "lib", "packages", "apps", "tools", "services"];
15
- }
16
- return [
17
- owner,
18
- `nimi-${owner}`,
19
- `src/${owner}`,
20
- `lib/${owner}`,
21
- `packages/${owner}`,
22
- `packages/nimi-${owner}`,
23
- `apps/${owner}`,
24
- `tools/${owner}`,
25
- `services/${owner}`,
26
- ];
27
- }
28
-
29
- const DECLARED_EVIDENCE_REF_PATTERN = /(?:^|[\s"'`([{:;,])((?:\.\/)?(?:[A-Za-z0-9_.@+-]+\/)+[A-Za-z0-9_@+.-]+\.(?:cjs|css|go|js|jsx|json|md|mjs|prisma|proto|py|rs|ts|tsx|yaml|yml))(?:[#:)\\\],;."'`]|\s|$)/gu;
30
-
31
- function looksLikeSpecAuthorityRelativeRef(normalized) {
32
- const extension = path.posix.extname(normalized);
33
- if (![".md", ".yaml", ".yml"].includes(extension)) {
34
- return false;
35
- }
36
- const parts = normalized.split("/");
37
- const firstSegment = parts[0];
38
- if (["tables", "generated", "kernel"].includes(firstSegment)) {
39
- return true;
40
- }
41
- if (parts[1] === "kernel") {
42
- return true;
43
- }
44
- const specDomainLike = /^(backend|dashboard|realm|runtime|v[0-9]+|vision|workers)$/u.test(firstSegment);
45
- return specDomainLike && parts.length <= 2;
46
- }
47
-
48
- function normalizeDeclaredEvidenceRef(value) {
49
- const raw = String(value ?? "")
50
- .trim()
51
- .replace(/\\/g, "/")
52
- .replace(/[),.;:]+$/u, "");
53
- if (!raw || raw.startsWith("../") || raw.includes("/../") || raw.startsWith("http:") || raw.startsWith("https:")) {
54
- return null;
55
- }
56
- if (raw.startsWith("@") || raw.startsWith("./")) {
57
- return null;
58
- }
59
- const normalized = raw.replace(/^\.\//, "");
60
- if (!normalized || normalized.startsWith("../") || normalized.includes("/../") || normalized.startsWith("http:") || normalized.startsWith("https:")) {
61
- return null;
62
- }
63
- if (!normalized.includes("/")) {
64
- return null;
65
- }
66
- const firstSegment = normalized.split("/")[0];
67
- if (looksLikeSpecAuthorityRelativeRef(normalized)) {
68
- return null;
69
- }
70
- if (
71
- normalized.startsWith(".nimi/spec/")
72
- || normalized.startsWith(".nimi/contracts/")
73
- || normalized.startsWith(".nimi/methodology/")
74
- || normalized.startsWith(".nimi/local/")
75
- || normalized.startsWith(".agents/")
76
- || normalized.startsWith(".claude/")
77
- || normalized.startsWith(".openclaw/")
78
- || normalized.includes("/.nimi/spec/")
79
- || normalized.includes("/.nimi/contracts/")
80
- || normalized.includes("/.nimi/methodology/")
81
- ) {
82
- return null;
83
- }
84
- const basename = path.posix.basename(normalized).toLowerCase();
85
- if (basename === "agents.md" || basename === "readme.md") {
86
- return null;
87
- }
88
- return normalized;
89
- }
90
-
91
- function candidateEvidenceRefsForDeclaredRef(declaredRef, evidenceRoots) {
92
- const normalized = String(declaredRef ?? "").replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/$/, "");
93
- if (!normalized) {
94
- return [];
95
- }
96
- const candidates = [normalized];
97
- for (const rootRef of evidenceRoots ?? []) {
98
- const root = String(rootRef ?? "").replace(/\\/g, "/").replace(/\/$/, "");
99
- if (!root || root === "." || root.startsWith(".nimi/spec") || path.posix.extname(root)) {
100
- continue;
101
- }
102
- if (normalized === root || normalized.startsWith(`${root}/`)) {
103
- candidates.push(normalized);
104
- } else {
105
- candidates.push(`${root}/${normalized}`);
106
- }
107
- }
108
- return [...new Set(candidates)].sort();
109
- }
110
-
111
- function normalizedRef(value) {
112
- return String(value ?? "").replace(/\\/g, "/").replace(/\/$/, "");
113
- }
114
-
115
- function refInsideRoot(fileRef, rootRef) {
116
- const root = normalizedRef(rootRef);
117
- return Boolean(root) && (fileRef === root || fileRef.startsWith(`${root}/`));
118
- }
119
-
120
- function delegatedProjectionAdmissionForFile(fileRef, admissions = []) {
121
- return admissions.find((admission) => {
122
- if (!refInsideRoot(fileRef, admission.authority_root)) {
123
- return false;
124
- }
125
- const relative = fileRef === admission.authority_root
126
- ? ""
127
- : fileRef.slice(admission.authority_root.length + 1);
128
- return !(admission.host_owned_relative_paths ?? []).some((hostOwnedPath) => (
129
- relative === hostOwnedPath || relative.startsWith(`${hostOwnedPath}/`)
130
- ));
131
- }) ?? null;
132
- }
133
-
134
- function declaredRefDelegated(ref, prefixes = []) {
135
- const normalized = normalizedRef(ref);
136
- return prefixes.some((prefix) => {
137
- const normalizedPrefix = normalizedRef(prefix);
138
- return normalizedPrefix && (normalized === normalizedPrefix || normalized.startsWith(`${normalizedPrefix}/`));
139
- });
140
- }
141
-
142
- function extractDeclaredEvidenceRefs(text) {
143
- const refs = [];
144
- for (const match of String(text ?? "").matchAll(DECLARED_EVIDENCE_REF_PATTERN)) {
145
- const normalized = normalizeDeclaredEvidenceRef(match[1]);
146
- if (normalized) {
147
- refs.push(normalized);
148
- }
149
- }
150
- return [...new Set(refs)].sort();
151
- }
152
-
153
- function slugPart(value) {
154
- return String(value)
155
- .replace(/[^a-zA-Z0-9]+/g, "-")
156
- .replace(/^-+|-+$/g, "")
157
- .toLowerCase() || "spec";
158
- }
159
-
160
- function extractModuleMapRefs(markdownText) {
161
- const lines = String(markdownText ?? "").split(/\r?\n/);
162
- const refs = [];
163
- let inModuleMap = false;
164
- for (const line of lines) {
165
- const heading = /^(#{1,6})\s+(.+?)\s*$/.exec(line);
166
- if (heading) {
167
- const headingText = heading[2].trim().toLowerCase();
168
- if (headingText === "module map") {
169
- inModuleMap = true;
170
- continue;
171
- }
172
- if (inModuleMap && heading[1].length <= 2) {
173
- break;
174
- }
175
- }
176
- if (!inModuleMap) {
177
- continue;
178
- }
179
- const match = /^\s*[-*]\s+`([^`]+)`/.exec(line);
180
- if (match) {
181
- refs.push(match[1].trim());
182
- }
183
- }
184
- return [...new Set(refs)].sort();
185
- }
186
-
187
- function buildDelegatedProjectionChunk({
188
- admission,
189
- entries,
190
- chunkIndex,
191
- criteria,
192
- authorityTextByRef,
193
- }) {
194
- const authorityRefs = entries.map((entry) => entry.file_ref).sort();
195
- const authorityText = authorityRefs
196
- .map((authorityRef) => authorityTextByRef?.get(authorityRef) ?? "")
197
- .join("\n");
198
- const declaredEvidenceRefs = extractDeclaredEvidenceRefs(authorityText);
199
- const delegatedDeclaredEvidenceRefs = declaredEvidenceRefs
200
- .filter((ref) => declaredRefDelegated(ref, admission.delegated_declared_evidence_prefixes))
201
- .sort();
202
- const localDeclaredEvidenceRefs = declaredEvidenceRefs
203
- .filter((ref) => !declaredRefDelegated(ref, admission.delegated_declared_evidence_prefixes))
204
- .sort();
205
- const evidenceRoots = [...new Set(admission.local_projection_evidence_roots ?? [])].sort();
206
- const declaredEvidenceTargets = localDeclaredEvidenceRefs
207
- .map((evidenceRef) => ({
208
- source_path: evidenceRef,
209
- candidates: candidateEvidenceRefsForDeclaredRef(evidenceRef, evidenceRoots),
210
- }))
211
- .filter((target) => target.candidates.length > 0);
212
- return {
213
- chunk_id: [
214
- `chunk-${String(chunkIndex).padStart(3, "0")}`,
215
- slugPart(admission.owner_domain),
216
- "delegated-projection",
217
- slugPart(admission.id),
218
- ].join("-"),
219
- state: "planned",
220
- owner_domain: admission.owner_domain,
221
- planning_basis: "spec_authority",
222
- spec_surface: "delegated-projection",
223
- criteria,
224
- files: authorityRefs,
225
- authority_refs: authorityRefs,
226
- authority_kind: "delegated_projection",
227
- delegated_projection_id: admission.id,
228
- admission_ref: admission.admission_ref,
229
- authority_root: admission.authority_root,
230
- source_authority_root: admission.source_authority_root,
231
- admitted_evidence_roots: evidenceRoots,
232
- delegated_evidence_roots: admission.delegated_evidence_roots,
233
- delegated_declared_evidence_prefixes: admission.delegated_declared_evidence_prefixes,
234
- ...(delegatedDeclaredEvidenceRefs.length > 0 ? {
235
- delegated_declared_evidence_refs: delegatedDeclaredEvidenceRefs,
236
- declared_evidence_delegation: {
237
- status: "delegated_to_source_authority",
238
- delegated_ref_count: delegatedDeclaredEvidenceRefs.length,
239
- source_authority_root: admission.source_authority_root,
240
- delegated_evidence_roots: admission.delegated_evidence_roots,
241
- },
242
- } : {}),
243
- ...(admission.required_verification_commands?.length > 0 ? {
244
- required_verification_commands: admission.required_verification_commands,
245
- } : {}),
246
- ...(declaredEvidenceTargets.length > 0 ? {
247
- declared_evidence_targets: declaredEvidenceTargets,
248
- } : {}),
249
- evidence_roots: evidenceRoots,
250
- file_count: authorityRefs.length,
251
- finding_count: 0,
252
- };
253
- }
254
-
255
- function candidateEvidenceRefsForModuleMapPath(modulePath, evidenceRoots) {
256
- const normalized = String(modulePath ?? "").replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/$/, "");
257
- if (!normalized || normalized.startsWith("http:") || normalized.startsWith("https:")) {
258
- return [];
259
- }
260
- const firstSegment = normalized.split("/")[0];
261
- const genericDirectRoots = new Set([".github", ".nimi", "apps", "config", "lib", "packages", "scripts", "services", "src", "tools"]);
262
- const directRoot = genericDirectRoots.has(firstSegment)
263
- || (evidenceRoots ?? []).some((rootRef) => {
264
- const root = String(rootRef ?? "").replace(/\\/g, "/").replace(/\/$/, "");
265
- return root !== "." && (normalized === root || normalized.startsWith(`${root}/`));
266
- });
267
- const candidates = [];
268
- if (directRoot) {
269
- candidates.push(normalized);
270
- }
271
- for (const rootRef of evidenceRoots ?? []) {
272
- const root = String(rootRef ?? "").replace(/\\/g, "/").replace(/\/$/, "");
273
- if (!root || root.startsWith(".nimi/spec")) {
274
- continue;
275
- }
276
- candidates.push(`${root}/${normalized}`);
277
- if (root.startsWith("apps/")) {
278
- candidates.push(`${root}/src/${normalized}`);
279
- candidates.push(`${root}/src/shell/renderer/${normalized}`);
280
- candidates.push(`${root}/src-tauri/src/${normalized}`);
281
- }
282
- }
283
- return [...new Set(candidates)].sort();
284
- }
285
-
286
- export function buildSpecChunks(includedInventory, options) {
287
- const sortedEntries = [...includedInventory].sort((left, right) => left.file_ref.localeCompare(right.file_ref));
288
- const includedRefs = new Set(sortedEntries.map((entry) => entry.file_ref));
289
- const delegatedEntriesByAdmissionRef = new Map();
290
- for (const entry of sortedEntries) {
291
- const admission = delegatedProjectionAdmissionForFile(entry.file_ref, options.delegatedProjectionAdmissions);
292
- if (!admission) {
293
- continue;
294
- }
295
- const entries = delegatedEntriesByAdmissionRef.get(admission.admission_ref) ?? [];
296
- entries.push(entry);
297
- delegatedEntriesByAdmissionRef.set(admission.admission_ref, entries);
298
- }
299
- const hostProjectionByHostRef = new Map();
300
- const hostProjectionsByPackageRef = new Map();
301
- for (const admission of options.packageAuthorityAdmissions ?? []) {
302
- for (const projection of admission.host_authority_projection_refs ?? []) {
303
- if (!includedRefs.has(projection.package_ref)) {
304
- continue;
305
- }
306
- const enrichedProjection = {
307
- host_ref: projection.host_ref,
308
- package_ref: projection.package_ref,
309
- package_authority_id: admission.id,
310
- admission_ref: admission.admission_ref,
311
- };
312
- hostProjectionByHostRef.set(projection.host_ref, enrichedProjection);
313
- const projections = hostProjectionsByPackageRef.get(projection.package_ref) ?? [];
314
- projections.push(enrichedProjection);
315
- hostProjectionsByPackageRef.set(projection.package_ref, projections);
316
- }
317
- }
318
- let chunkIndex = 0;
319
- const chunks = [];
320
- const emittedDelegatedAdmissions = new Set();
321
- for (const entry of sortedEntries) {
322
- const delegatedAdmission = delegatedProjectionAdmissionForFile(entry.file_ref, options.delegatedProjectionAdmissions);
323
- if (delegatedAdmission) {
324
- if (emittedDelegatedAdmissions.has(delegatedAdmission.admission_ref)) {
325
- continue;
326
- }
327
- emittedDelegatedAdmissions.add(delegatedAdmission.admission_ref);
328
- chunkIndex += 1;
329
- chunks.push(buildDelegatedProjectionChunk({
330
- admission: delegatedAdmission,
331
- entries: delegatedEntriesByAdmissionRef.get(delegatedAdmission.admission_ref) ?? [entry],
332
- chunkIndex,
333
- criteria: options.criteria,
334
- authorityTextByRef: options.authorityTextByRef,
335
- }));
336
- continue;
337
- }
338
- if (hostProjectionByHostRef.has(entry.file_ref)) {
339
- continue;
340
- }
341
- const surface = specSurfaceForFile(entry.file_ref, options.appSliceAdmissions, options.packageAuthorityAdmissions);
342
- const appAdmission = surface.appAdmission;
343
- const packageAdmission = surface.packageAdmission;
344
- const hostAuthorityProjectionRefs = (hostProjectionsByPackageRef.get(entry.file_ref) ?? [])
345
- .sort((left, right) => left.host_ref.localeCompare(right.host_ref));
346
- const authorityRefs = [
347
- entry.file_ref,
348
- ...hostAuthorityProjectionRefs.map((projection) => projection.host_ref),
349
- ];
350
- const rootAdmissions = (options.auditEvidenceRootAdmissions ?? [])
351
- .filter((admission) => admission.owner_domain === surface.ownerDomain && admission.authority_refs.includes(entry.file_ref));
352
- const admittedEvidenceRoots = rootAdmissions.flatMap((admission) => admission.evidence_roots);
353
- const authorityText = authorityRefs
354
- .map((authorityRef) => options.authorityTextByRef?.get(authorityRef) ?? "")
355
- .join("\n");
356
- const declaredEvidenceRefs = packageAdmission || appAdmission
357
- ? []
358
- : extractDeclaredEvidenceRefs(authorityText);
359
- const evidenceRoots = packageAdmission
360
- ? packageAdmission.evidence_roots
361
- : appAdmission
362
- ? appAdmission.evidence_roots
363
- : [...new Set([
364
- ...evidenceRootsForSpecOwner(surface.ownerDomain, options.targetRootRef),
365
- ...declaredEvidenceRefs,
366
- ...admittedEvidenceRoots,
367
- ])].sort();
368
- const moduleMapRefs = surface.surface === "domain-guides" || surface.surface === "app-domain-guides"
369
- ? extractModuleMapRefs(options.authorityTextByRef?.get(entry.file_ref) ?? "")
370
- : [];
371
- const declaredEvidenceTargets = [
372
- ...moduleMapRefs.map((moduleRef) => ({
373
- source_path: moduleRef,
374
- candidates: candidateEvidenceRefsForModuleMapPath(moduleRef, evidenceRoots),
375
- })),
376
- ...declaredEvidenceRefs.map((evidenceRef) => ({
377
- source_path: evidenceRef,
378
- candidates: candidateEvidenceRefsForDeclaredRef(evidenceRef, evidenceRoots),
379
- })),
380
- ]
381
- .filter((target) => target.candidates.length > 0);
382
- chunkIndex += 1;
383
- const chunkId = [
384
- `chunk-${String(chunkIndex).padStart(3, "0")}`,
385
- slugPart(surface.ownerDomain),
386
- slugPart(surface.surface),
387
- slugPart(path.posix.basename(entry.file_ref, path.posix.extname(entry.file_ref))),
388
- ].join("-");
389
- chunks.push({
390
- chunk_id: chunkId,
391
- state: "planned",
392
- owner_domain: surface.ownerDomain,
393
- planning_basis: "spec_authority",
394
- spec_surface: surface.surface,
395
- criteria: options.criteria,
396
- files: authorityRefs,
397
- authority_refs: authorityRefs,
398
- authority_kind: packageAdmission ? "admitted_package_authority" : (appAdmission ? "admitted_app_slice" : "nimi_spec"),
399
- ...(packageAdmission ? {
400
- package_authority_id: packageAdmission.id,
401
- admission_ref: packageAdmission.admission_ref,
402
- authority_root: packageAdmission.authority_root,
403
- } : {}),
404
- ...(appAdmission ? {
405
- app_id: appAdmission.app_id,
406
- admission_ref: appAdmission.admission_ref,
407
- authority_root: appAdmission.authority_root,
408
- } : {}),
409
- ...(rootAdmissions.length > 0 ? {
410
- evidence_root_admission_refs: rootAdmissions.map((admission) => admission.admission_ref),
411
- admitted_evidence_roots: admittedEvidenceRoots,
412
- } : {}),
413
- ...(hostAuthorityProjectionRefs.length > 0 ? {
414
- host_authority_projection_refs: hostAuthorityProjectionRefs,
415
- } : {}),
416
- ...(declaredEvidenceTargets.length > 0 ? {
417
- declared_evidence_targets: declaredEvidenceTargets,
418
- } : {}),
419
- evidence_roots: evidenceRoots,
420
- file_count: authorityRefs.length,
421
- finding_count: 0,
422
- });
423
- }
424
- return chunks;
425
- }