@nimiplatform/nimi-coding 0.1.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 (186) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +348 -0
  3. package/adapters/README.md +25 -0
  4. package/adapters/claude/README.md +89 -0
  5. package/adapters/claude/profile.yaml +70 -0
  6. package/adapters/codex/README.md +53 -0
  7. package/adapters/codex/profile.yaml +78 -0
  8. package/adapters/oh-my-codex/README.md +185 -0
  9. package/adapters/oh-my-codex/profile.yaml +46 -0
  10. package/bin/nimicoding.mjs +6 -0
  11. package/cli/commands/admit-high-risk-decision.mjs +108 -0
  12. package/cli/commands/audit-sweep.mjs +341 -0
  13. package/cli/commands/blueprint-audit.mjs +91 -0
  14. package/cli/commands/clear.mjs +168 -0
  15. package/cli/commands/closeout.mjs +183 -0
  16. package/cli/commands/decide-high-risk-execution.mjs +124 -0
  17. package/cli/commands/doctor.mjs +53 -0
  18. package/cli/commands/generate-spec-derived-docs.mjs +131 -0
  19. package/cli/commands/handoff.mjs +123 -0
  20. package/cli/commands/ingest-high-risk-execution.mjs +95 -0
  21. package/cli/commands/review-high-risk-execution.mjs +95 -0
  22. package/cli/commands/start.mjs +717 -0
  23. package/cli/commands/topic-formatters.mjs +382 -0
  24. package/cli/commands/topic-goal.mjs +33 -0
  25. package/cli/commands/topic-options-shared.mjs +27 -0
  26. package/cli/commands/topic-options-workflow.mjs +767 -0
  27. package/cli/commands/topic-options.mjs +626 -0
  28. package/cli/commands/topic-runner.mjs +169 -0
  29. package/cli/commands/topic.mjs +795 -0
  30. package/cli/commands/validate-acceptance.mjs +5 -0
  31. package/cli/commands/validate-ai-governance.mjs +214 -0
  32. package/cli/commands/validate-execution-packet.mjs +5 -0
  33. package/cli/commands/validate-orchestration-state.mjs +5 -0
  34. package/cli/commands/validate-prompt.mjs +5 -0
  35. package/cli/commands/validate-spec-audit.mjs +27 -0
  36. package/cli/commands/validate-spec-governance.mjs +124 -0
  37. package/cli/commands/validate-spec-tree.mjs +27 -0
  38. package/cli/commands/validate-worker-output.mjs +5 -0
  39. package/cli/constants.mjs +489 -0
  40. package/cli/help.mjs +134 -0
  41. package/cli/index.mjs +103 -0
  42. package/cli/lib/adapter-profiles.mjs +403 -0
  43. package/cli/lib/audit-execution.mjs +52 -0
  44. package/cli/lib/audit-sweep-runtime/admissions.mjs +381 -0
  45. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +333 -0
  46. package/cli/lib/audit-sweep-runtime/chunks.mjs +697 -0
  47. package/cli/lib/audit-sweep-runtime/closeout.mjs +144 -0
  48. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +639 -0
  49. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +515 -0
  50. package/cli/lib/audit-sweep-runtime/common.mjs +329 -0
  51. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +172 -0
  52. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +152 -0
  53. package/cli/lib/audit-sweep-runtime/format.mjs +57 -0
  54. package/cli/lib/audit-sweep-runtime/ingest.mjs +486 -0
  55. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +198 -0
  56. package/cli/lib/audit-sweep-runtime/inventory.mjs +728 -0
  57. package/cli/lib/audit-sweep-runtime/ledger.mjs +315 -0
  58. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +101 -0
  59. package/cli/lib/audit-sweep-runtime/remediation.mjs +349 -0
  60. package/cli/lib/audit-sweep-runtime/rerun.mjs +129 -0
  61. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +300 -0
  62. package/cli/lib/audit-sweep-runtime/status.mjs +62 -0
  63. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +215 -0
  64. package/cli/lib/audit-sweep-runtime/validators.mjs +758 -0
  65. package/cli/lib/audit-sweep.mjs +18 -0
  66. package/cli/lib/authority-convergence.mjs +309 -0
  67. package/cli/lib/blueprint-audit.mjs +370 -0
  68. package/cli/lib/bootstrap.mjs +228 -0
  69. package/cli/lib/closeout.mjs +623 -0
  70. package/cli/lib/codex-sdk-runner.mjs +76 -0
  71. package/cli/lib/contracts.mjs +180 -0
  72. package/cli/lib/doctor.mjs +18 -0
  73. package/cli/lib/entrypoints.mjs +274 -0
  74. package/cli/lib/external-execution.mjs +101 -0
  75. package/cli/lib/fs-helpers.mjs +33 -0
  76. package/cli/lib/handoff.mjs +785 -0
  77. package/cli/lib/high-risk-admission.mjs +442 -0
  78. package/cli/lib/high-risk-decision.mjs +324 -0
  79. package/cli/lib/high-risk-ingest.mjs +317 -0
  80. package/cli/lib/high-risk-review.mjs +263 -0
  81. package/cli/lib/internal/contracts-loaders.mjs +132 -0
  82. package/cli/lib/internal/contracts-parse-high-risk.mjs +131 -0
  83. package/cli/lib/internal/contracts-parse.mjs +457 -0
  84. package/cli/lib/internal/contracts-validators.mjs +398 -0
  85. package/cli/lib/internal/doctor-bootstrap-surface.mjs +359 -0
  86. package/cli/lib/internal/doctor-delegated-surface.mjs +256 -0
  87. package/cli/lib/internal/doctor-finalize.mjs +385 -0
  88. package/cli/lib/internal/doctor-format.mjs +286 -0
  89. package/cli/lib/internal/doctor-inspectors.mjs +294 -0
  90. package/cli/lib/internal/doctor-state.mjs +205 -0
  91. package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +315 -0
  92. package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +358 -0
  93. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +155 -0
  94. package/cli/lib/internal/governance/ai/check-high-risk-doc-metadata-core.mjs +173 -0
  95. package/cli/lib/internal/governance/config.mjs +150 -0
  96. package/cli/lib/internal/governance/runner.mjs +35 -0
  97. package/cli/lib/internal/governance/shared/read-yaml-with-fragments.mjs +49 -0
  98. package/cli/lib/internal/validators-artifacts.mjs +515 -0
  99. package/cli/lib/internal/validators-shared.mjs +28 -0
  100. package/cli/lib/internal/validators-spec-helpers.mjs +186 -0
  101. package/cli/lib/internal/validators-spec.mjs +410 -0
  102. package/cli/lib/shared.mjs +83 -0
  103. package/cli/lib/topic-draft-packets.mjs +48 -0
  104. package/cli/lib/topic-goal.mjs +361 -0
  105. package/cli/lib/topic-runner.mjs +772 -0
  106. package/cli/lib/topic.mjs +93 -0
  107. package/cli/lib/ui.mjs +178 -0
  108. package/cli/lib/validators.mjs +78 -0
  109. package/cli/lib/value-helpers.mjs +24 -0
  110. package/cli/lib/yaml-helpers.mjs +133 -0
  111. package/cli/nimicoding.mjs +1 -0
  112. package/cli/seeds/bootstrap.mjs +47 -0
  113. package/config/audit-execution-artifacts.yaml +20 -0
  114. package/config/bootstrap.yaml +6 -0
  115. package/config/external-execution-artifacts.yaml +16 -0
  116. package/config/host-adapter.yaml +30 -0
  117. package/config/host-profile.yaml +29 -0
  118. package/config/installer-evidence.yaml +31 -0
  119. package/config/skill-installer.yaml +23 -0
  120. package/config/skill-manifest.yaml +46 -0
  121. package/config/skills.yaml +30 -0
  122. package/config/spec-generation-inputs.yaml +25 -0
  123. package/contracts/acceptance.schema.yaml +16 -0
  124. package/contracts/admission-checklist.schema.yaml +15 -0
  125. package/contracts/audit-chunk.schema.yaml +110 -0
  126. package/contracts/audit-closeout.schema.yaml +51 -0
  127. package/contracts/audit-finding.schema.yaml +61 -0
  128. package/contracts/audit-ledger.schema.yaml +138 -0
  129. package/contracts/audit-plan.schema.yaml +123 -0
  130. package/contracts/audit-remediation-map.schema.yaml +51 -0
  131. package/contracts/audit-rerun.schema.yaml +31 -0
  132. package/contracts/audit-sweep-result.yaml +49 -0
  133. package/contracts/authority-convergence-audit.schema.yaml +19 -0
  134. package/contracts/closeout.schema.yaml +25 -0
  135. package/contracts/decision-review.schema.yaml +16 -0
  136. package/contracts/doc-spec-audit-result.yaml +19 -0
  137. package/contracts/execution-packet.schema.yaml +49 -0
  138. package/contracts/external-host-compatibility.yaml +22 -0
  139. package/contracts/forbidden-shortcuts.catalog.yaml +23 -0
  140. package/contracts/high-risk-admission.schema.yaml +23 -0
  141. package/contracts/high-risk-execution-result.yaml +20 -0
  142. package/contracts/orchestration-state.schema.yaml +41 -0
  143. package/contracts/overflow-continuation.schema.yaml +12 -0
  144. package/contracts/packet.schema.yaml +30 -0
  145. package/contracts/pending-note.schema.yaml +17 -0
  146. package/contracts/prompt.schema.yaml +12 -0
  147. package/contracts/remediation.schema.yaml +16 -0
  148. package/contracts/result.schema.yaml +24 -0
  149. package/contracts/spec-generation-audit.schema.yaml +31 -0
  150. package/contracts/spec-generation-inputs.schema.yaml +39 -0
  151. package/contracts/spec-reconstruction-result.yaml +37 -0
  152. package/contracts/topic-goal.schema.yaml +78 -0
  153. package/contracts/topic-run-ledger.schema.yaml +72 -0
  154. package/contracts/topic-step-decision.schema.yaml +45 -0
  155. package/contracts/topic.schema.yaml +65 -0
  156. package/contracts/true-close.schema.yaml +15 -0
  157. package/contracts/wave.schema.yaml +29 -0
  158. package/contracts/worker-output.schema.yaml +15 -0
  159. package/methodology/audit-sweep-p0p1-recall.yaml +45 -0
  160. package/methodology/authority-convergence-policy.yaml +42 -0
  161. package/methodology/core.yaml +25 -0
  162. package/methodology/four-closure-policy.yaml +28 -0
  163. package/methodology/overflow-continuation-policy.yaml +14 -0
  164. package/methodology/role-separation-policy.yaml +28 -0
  165. package/methodology/skill-exchange-projection.yaml +114 -0
  166. package/methodology/skill-handoff.yaml +34 -0
  167. package/methodology/skill-installer-result.yaml +27 -0
  168. package/methodology/skill-installer-summary-projection.yaml +181 -0
  169. package/methodology/skill-runtime.yaml +23 -0
  170. package/methodology/spec-reconstruction.yaml +63 -0
  171. package/methodology/spec-target-truth-profile.yaml +53 -0
  172. package/methodology/topic-lifecycle-report.yaml +144 -0
  173. package/methodology/topic-lifecycle.yaml +37 -0
  174. package/methodology/topic-naming-ontology.yaml +21 -0
  175. package/methodology/topic-ontology.yaml +38 -0
  176. package/methodology/topic-validation-policy.yaml +9 -0
  177. package/methodology/wave-dag-policy.yaml +14 -0
  178. package/package.json +50 -0
  179. package/spec/_meta/command-gating-matrix.yaml +110 -0
  180. package/spec/_meta/generate-drift-migration-checklist.yaml +155 -0
  181. package/spec/_meta/governance-routing-cutover-checklist.yaml +35 -0
  182. package/spec/_meta/phase2-impacted-surface-matrix.yaml +44 -0
  183. package/spec/_meta/spec-authority-cutover-readiness.yaml +104 -0
  184. package/spec/_meta/spec-tree-model.yaml +72 -0
  185. package/spec/bootstrap-state.yaml +99 -0
  186. package/spec/product-scope.yaml +56 -0
@@ -0,0 +1,381 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import YAML from "yaml";
5
+
6
+ import { artifactPath, toPosix } from "./common.mjs";
7
+ import { pathExists } from "../fs-helpers.mjs";
8
+
9
+ export const AUDIT_SWEEP_PROJECT_CONFIG_REF = ".nimi/config/audit-sweep.yaml";
10
+ export const APP_SLICE_ADMISSION_REF = ".nimi/spec/platform/kernel/tables/app-slice-admissions.yaml";
11
+ const AUDIT_EVIDENCE_ROOT_TABLE_BASENAME = "audit-evidence-roots.yaml";
12
+ const PACKAGE_AUTHORITY_ADMISSION_BASENAME = "package-authority-admissions.yaml";
13
+
14
+ export function refInsideRoot(fileRef, rootRef) {
15
+ const normalizedRoot = rootRef.replace(/\\/g, "/").replace(/\/$/, "");
16
+ return fileRef === normalizedRoot || fileRef.startsWith(`${normalizedRoot}/`);
17
+ }
18
+
19
+ function safeProjectRef(value) {
20
+ return typeof value === "string"
21
+ && value.trim().length > 0
22
+ && !path.isAbsolute(value)
23
+ && !value.split("/").includes("..");
24
+ }
25
+
26
+ export async function loadAuditSweepProjectConfig(projectRoot) {
27
+ const configPath = artifactPath(projectRoot, AUDIT_SWEEP_PROJECT_CONFIG_REF);
28
+ const info = await pathExists(configPath);
29
+ if (!info?.isFile()) {
30
+ return { ok: true, found: false, excludePatterns: [], ignorePatterns: [], ignoreOwnerDomains: [], ignoreReason: null };
31
+ }
32
+
33
+ let parsed;
34
+ try {
35
+ parsed = YAML.parse(await readFile(configPath, "utf8"));
36
+ } catch (error) {
37
+ return {
38
+ ok: false,
39
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} must contain valid YAML (${error.message}).\n`,
40
+ };
41
+ }
42
+
43
+ const rawExcludePatterns = parsed?.audit_sweep?.exclude_patterns ?? parsed?.exclude_patterns ?? [];
44
+ const rawIgnorePatterns = parsed?.audit_sweep?.ignore_patterns ?? parsed?.ignore_patterns ?? [];
45
+ const rawIgnoreOwnerDomains = parsed?.audit_sweep?.ignore_owner_domains ?? parsed?.ignore_owner_domains ?? [];
46
+ const rawIgnoreReason = parsed?.audit_sweep?.ignore_reason ?? parsed?.ignore_reason ?? null;
47
+ if (!Array.isArray(rawExcludePatterns)) {
48
+ return {
49
+ ok: false,
50
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} exclude_patterns must be an array.\n`,
51
+ };
52
+ }
53
+ if (!Array.isArray(rawIgnorePatterns)) {
54
+ return {
55
+ ok: false,
56
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} ignore_patterns must be an array.\n`,
57
+ };
58
+ }
59
+ if (!Array.isArray(rawIgnoreOwnerDomains)) {
60
+ return {
61
+ ok: false,
62
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} ignore_owner_domains must be an array.\n`,
63
+ };
64
+ }
65
+ if (rawIgnoreReason !== null && (typeof rawIgnoreReason !== "string" || rawIgnoreReason.trim().length === 0)) {
66
+ return {
67
+ ok: false,
68
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} ignore_reason must be a non-empty string when present.\n`,
69
+ };
70
+ }
71
+
72
+ const excludePatterns = [];
73
+ for (const pattern of rawExcludePatterns) {
74
+ if (typeof pattern !== "string" || pattern.trim().length === 0) {
75
+ return {
76
+ ok: false,
77
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} exclude_patterns entries must be non-empty strings.\n`,
78
+ };
79
+ }
80
+ excludePatterns.push(pattern.trim());
81
+ }
82
+ const ignorePatterns = [];
83
+ for (const pattern of rawIgnorePatterns) {
84
+ if (typeof pattern !== "string" || pattern.trim().length === 0) {
85
+ return {
86
+ ok: false,
87
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} ignore_patterns entries must be non-empty strings.\n`,
88
+ };
89
+ }
90
+ ignorePatterns.push(pattern.trim());
91
+ }
92
+ const ignoreOwnerDomains = [];
93
+ for (const ownerDomain of rawIgnoreOwnerDomains) {
94
+ if (typeof ownerDomain !== "string" || ownerDomain.trim().length === 0) {
95
+ return {
96
+ ok: false,
97
+ error: `nimicoding audit-sweep refused: ${AUDIT_SWEEP_PROJECT_CONFIG_REF} ignore_owner_domains entries must be non-empty strings.\n`,
98
+ };
99
+ }
100
+ ignoreOwnerDomains.push(ownerDomain.trim());
101
+ }
102
+
103
+ return {
104
+ ok: true,
105
+ found: true,
106
+ excludePatterns,
107
+ ignorePatterns,
108
+ ignoreOwnerDomains,
109
+ ignoreReason: rawIgnoreReason?.trim() ?? null,
110
+ };
111
+ }
112
+
113
+ export async function loadAppSliceAdmissions(projectRoot) {
114
+ const configPath = artifactPath(projectRoot, APP_SLICE_ADMISSION_REF);
115
+ const info = await pathExists(configPath);
116
+ if (!info?.isFile()) {
117
+ return { ok: true, found: false, admissions: [] };
118
+ }
119
+
120
+ let parsed;
121
+ try {
122
+ parsed = YAML.parse(await readFile(configPath, "utf8"));
123
+ } catch (error) {
124
+ return {
125
+ ok: false,
126
+ error: `nimicoding audit-sweep refused: ${APP_SLICE_ADMISSION_REF} must contain valid YAML (${error.message}).\n`,
127
+ };
128
+ }
129
+
130
+ const rows = Array.isArray(parsed?.admissions) ? parsed.admissions : null;
131
+ if (!rows) {
132
+ return {
133
+ ok: false,
134
+ error: `nimicoding audit-sweep refused: ${APP_SLICE_ADMISSION_REF} must declare admissions as an array.\n`,
135
+ };
136
+ }
137
+
138
+ const admissions = [];
139
+ const seenAppIds = new Set();
140
+ for (const row of rows) {
141
+ const appId = String(row?.app_id ?? "").trim();
142
+ const status = String(row?.status ?? "").trim();
143
+ const ownerDomain = String(row?.owner_domain ?? "").trim();
144
+ const authorityRoot = String(row?.authority_root ?? "").trim().replace(/\\/g, "/").replace(/\/$/, "");
145
+ const evidenceRoots = Array.isArray(row?.evidence_roots)
146
+ ? row.evidence_roots.map((entry) => String(entry ?? "").trim().replace(/\\/g, "/").replace(/\/$/, "")).filter(Boolean)
147
+ : null;
148
+ if (!appId || seenAppIds.has(appId)) {
149
+ return { ok: false, error: `nimicoding audit-sweep refused: ${APP_SLICE_ADMISSION_REF} has missing or duplicate app_id.\n` };
150
+ }
151
+ seenAppIds.add(appId);
152
+ if (appId === "avatar") {
153
+ return { ok: false, error: `nimicoding audit-sweep refused: avatar is promoted to .nimi/spec/avatar and must not be admitted through ${APP_SLICE_ADMISSION_REF}.\n` };
154
+ }
155
+ if (status !== "active") {
156
+ continue;
157
+ }
158
+ if (!ownerDomain || !safeProjectRef(authorityRoot) || !authorityRoot.startsWith(`apps/${appId}/spec`)) {
159
+ return { ok: false, error: `nimicoding audit-sweep refused: ${APP_SLICE_ADMISSION_REF} ${appId} has invalid owner_domain or authority_root.\n` };
160
+ }
161
+ if (!evidenceRoots || evidenceRoots.length === 0 || !evidenceRoots.every((rootRef) => safeProjectRef(rootRef) && refInsideRoot(rootRef, `apps/${appId}`))) {
162
+ return { ok: false, error: `nimicoding audit-sweep refused: ${APP_SLICE_ADMISSION_REF} ${appId} has invalid evidence_roots.\n` };
163
+ }
164
+ admissions.push({
165
+ app_id: appId,
166
+ owner_domain: ownerDomain,
167
+ status,
168
+ authority_root: authorityRoot,
169
+ evidence_roots: evidenceRoots,
170
+ admission_ref: `${APP_SLICE_ADMISSION_REF}#${appId}`,
171
+ });
172
+ }
173
+
174
+ return { ok: true, found: true, admissions };
175
+ }
176
+
177
+ async function listSpecTableRefs(projectRoot, basename, listGitFiles, listFallbackFiles) {
178
+ const specRootInfo = await pathExists(artifactPath(projectRoot, ".nimi/spec"));
179
+ if (!specRootInfo?.isDirectory()) {
180
+ return null;
181
+ }
182
+ const gitSpecFiles = await listGitFiles(projectRoot, ".nimi/spec");
183
+ const specFiles = gitSpecFiles.length > 0
184
+ ? gitSpecFiles
185
+ : await listFallbackFiles(projectRoot, ".nimi/spec", []);
186
+ return specFiles
187
+ .map((entry) => toPosix(entry))
188
+ .filter((entry) => entry.endsWith(`/kernel/tables/${basename}`))
189
+ .sort();
190
+ }
191
+
192
+ export async function loadAuditEvidenceRootAdmissions(projectRoot, listGitFiles, listFallbackFiles) {
193
+ const tableRefs = await listSpecTableRefs(projectRoot, AUDIT_EVIDENCE_ROOT_TABLE_BASENAME, listGitFiles, listFallbackFiles);
194
+ if (!tableRefs) {
195
+ return { ok: true, tableRefs: [], admissions: [] };
196
+ }
197
+ const admissions = [];
198
+
199
+ for (const tableRef of tableRefs) {
200
+ let parsed;
201
+ try {
202
+ parsed = YAML.parse(await readFile(artifactPath(projectRoot, tableRef), "utf8"));
203
+ } catch (error) {
204
+ return {
205
+ ok: false,
206
+ error: `nimicoding audit-sweep refused: ${tableRef} must contain valid YAML (${error.message}).\n`,
207
+ };
208
+ }
209
+ const rows = Array.isArray(parsed?.roots) ? parsed.roots : null;
210
+ if (!rows) {
211
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} must declare roots as an array.\n` };
212
+ }
213
+ for (const row of rows) {
214
+ const id = String(row?.id ?? "").trim();
215
+ const ownerDomain = String(row?.owner_domain ?? "").trim();
216
+ const authorityRefs = Array.isArray(row?.authority_refs)
217
+ ? row.authority_refs.map((entry) => String(entry ?? "").trim().replace(/\\/g, "/")).filter(Boolean)
218
+ : null;
219
+ const evidenceRoots = Array.isArray(row?.evidence_roots)
220
+ ? row.evidence_roots.map((entry) => String(entry ?? "").trim().replace(/\\/g, "/").replace(/\/$/, "")).filter(Boolean)
221
+ : null;
222
+ if (!id || !ownerDomain || !authorityRefs?.length || !evidenceRoots?.length) {
223
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} root rows require id, owner_domain, authority_refs, and evidence_roots.\n` };
224
+ }
225
+ if (!authorityRefs.every((ref) => safeProjectRef(ref) && ref.startsWith(".nimi/spec/"))) {
226
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} authority_refs must stay under .nimi/spec.\n` };
227
+ }
228
+ if (!evidenceRoots.every((ref) => safeProjectRef(ref) && !ref.startsWith(".nimi/spec/"))) {
229
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} evidence_roots must be project evidence roots outside .nimi/spec.\n` };
230
+ }
231
+ admissions.push({
232
+ id,
233
+ owner_domain: ownerDomain,
234
+ authority_refs: authorityRefs,
235
+ evidence_roots: evidenceRoots,
236
+ admission_ref: `${tableRef}#${id}`,
237
+ });
238
+ }
239
+ }
240
+
241
+ return { ok: true, tableRefs, admissions };
242
+ }
243
+
244
+ export async function loadPackageAuthorityAdmissions(projectRoot, listGitFiles, listFallbackFiles) {
245
+ const tableRefs = await listSpecTableRefs(projectRoot, PACKAGE_AUTHORITY_ADMISSION_BASENAME, listGitFiles, listFallbackFiles);
246
+ if (!tableRefs) {
247
+ return { ok: true, tableRefs: [], admissions: [] };
248
+ }
249
+ const admissions = [];
250
+ const seenIds = new Set();
251
+
252
+ for (const tableRef of tableRefs) {
253
+ let parsed;
254
+ try {
255
+ parsed = YAML.parse(await readFile(artifactPath(projectRoot, tableRef), "utf8"));
256
+ } catch (error) {
257
+ return {
258
+ ok: false,
259
+ error: `nimicoding audit-sweep refused: ${tableRef} must contain valid YAML (${error.message}).\n`,
260
+ };
261
+ }
262
+ const rows = Array.isArray(parsed?.admissions) ? parsed.admissions : null;
263
+ if (!rows) {
264
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} must declare admissions as an array.\n` };
265
+ }
266
+ for (const row of rows) {
267
+ const id = String(row?.id ?? "").trim();
268
+ const status = String(row?.status ?? "").trim();
269
+ const ownerDomain = String(row?.owner_domain ?? "").trim();
270
+ const authorityRoot = String(row?.authority_root ?? "").trim().replace(/\\/g, "/").replace(/\/$/, "");
271
+ const evidenceRoots = Array.isArray(row?.evidence_roots)
272
+ ? row.evidence_roots.map((entry) => String(entry ?? "").trim().replace(/\\/g, "/").replace(/\/$/, "")).filter(Boolean)
273
+ : null;
274
+ const hostAuthorityProjectionRefs = Array.isArray(row?.projection_boundary?.host_authority_projection_refs)
275
+ ? row.projection_boundary.host_authority_projection_refs.map((entry) => ({
276
+ host_ref: String(entry?.host_ref ?? "").trim().replace(/\\/g, "/"),
277
+ package_ref: String(entry?.package_ref ?? "").trim().replace(/\\/g, "/"),
278
+ })).filter((entry) => entry.host_ref || entry.package_ref)
279
+ : [];
280
+ const admissionKey = `${tableRef}#${id}`;
281
+ if (!id || seenIds.has(admissionKey)) {
282
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} has missing or duplicate package authority id.\n` };
283
+ }
284
+ seenIds.add(admissionKey);
285
+ if (status !== "active") {
286
+ continue;
287
+ }
288
+ if (!ownerDomain || !safeProjectRef(authorityRoot) || authorityRoot.startsWith(".nimi/spec/") || !authorityRoot.endsWith("/spec")) {
289
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} has invalid owner_domain or authority_root.\n` };
290
+ }
291
+ if (!evidenceRoots || evidenceRoots.length === 0 || !evidenceRoots.every((rootRef) => safeProjectRef(rootRef) && !rootRef.startsWith(".nimi/spec/") && refInsideRoot(authorityRoot, rootRef))) {
292
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} has invalid evidence_roots.\n` };
293
+ }
294
+ const seenProjectionHostRefs = new Set();
295
+ for (const projection of hostAuthorityProjectionRefs) {
296
+ if (!safeProjectRef(projection.host_ref) || !projection.host_ref.startsWith(".nimi/spec/")) {
297
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} host_authority_projection_refs host_ref must stay under .nimi/spec.\n` };
298
+ }
299
+ if (!safeProjectRef(projection.package_ref) || !refInsideRoot(projection.package_ref, authorityRoot)) {
300
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} host_authority_projection_refs package_ref must stay under authority_root.\n` };
301
+ }
302
+ if (seenProjectionHostRefs.has(projection.host_ref)) {
303
+ return { ok: false, error: `nimicoding audit-sweep refused: ${tableRef} ${id} host_authority_projection_refs contains duplicate host_ref.\n` };
304
+ }
305
+ seenProjectionHostRefs.add(projection.host_ref);
306
+ }
307
+ admissions.push({
308
+ id,
309
+ owner_domain: ownerDomain,
310
+ status,
311
+ authority_root: authorityRoot,
312
+ evidence_roots: evidenceRoots,
313
+ host_authority_projection_refs: hostAuthorityProjectionRefs,
314
+ admission_ref: admissionKey,
315
+ });
316
+ }
317
+ }
318
+
319
+ return { ok: true, tableRefs, admissions };
320
+ }
321
+
322
+ function appSliceAdmissionForFile(fileRef, appSliceAdmissions = []) {
323
+ return appSliceAdmissions.find((admission) => refInsideRoot(fileRef, admission.authority_root)) ?? null;
324
+ }
325
+
326
+ function packageAuthorityAdmissionForFile(fileRef, packageAuthorityAdmissions = []) {
327
+ return packageAuthorityAdmissions.find((admission) => refInsideRoot(fileRef, admission.authority_root)) ?? null;
328
+ }
329
+
330
+ export function specSurfaceForFile(fileRef, appSliceAdmissions = [], packageAuthorityAdmissions = []) {
331
+ const packageAdmission = packageAuthorityAdmissionForFile(fileRef, packageAuthorityAdmissions);
332
+ if (packageAdmission) {
333
+ const relative = fileRef.slice(packageAdmission.authority_root.length + 1);
334
+ const parts = relative.split("/");
335
+ if (parts[0] === "_meta") {
336
+ return { ownerDomain: packageAdmission.owner_domain, surface: "package-meta", packageAdmission };
337
+ }
338
+ if (parts[0] === "kernel" && parts[1] === "tables") {
339
+ return { ownerDomain: packageAdmission.owner_domain, surface: "package-kernel-tables", packageAdmission };
340
+ }
341
+ if (parts[0] === "kernel") {
342
+ return { ownerDomain: packageAdmission.owner_domain, surface: "package-kernel-contracts", packageAdmission };
343
+ }
344
+ return { ownerDomain: packageAdmission.owner_domain, surface: "package-root", packageAdmission };
345
+ }
346
+
347
+ const appAdmission = appSliceAdmissionForFile(fileRef, appSliceAdmissions);
348
+ if (appAdmission) {
349
+ const relative = fileRef.slice(appAdmission.authority_root.length + 1);
350
+ const parts = relative.split("/");
351
+ if (parts[0] === "kernel" && parts[1] === "tables") {
352
+ return { ownerDomain: appAdmission.owner_domain, surface: "app-kernel-tables", appAdmission };
353
+ }
354
+ if (parts[0] === "kernel") {
355
+ return { ownerDomain: appAdmission.owner_domain, surface: "app-kernel-contracts", appAdmission };
356
+ }
357
+ return { ownerDomain: appAdmission.owner_domain, surface: "app-domain-guides", appAdmission };
358
+ }
359
+
360
+ const withoutRoot = fileRef.startsWith(".nimi/spec/")
361
+ ? fileRef.slice(".nimi/spec/".length)
362
+ : fileRef;
363
+ const parts = withoutRoot.split("/");
364
+ if (parts[0] === "_meta") {
365
+ return { ownerDomain: "spec-meta", surface: path.posix.basename(fileRef, path.posix.extname(fileRef)) };
366
+ }
367
+ if (parts.length === 1) {
368
+ return { ownerDomain: "spec-root", surface: path.posix.basename(fileRef, path.posix.extname(fileRef)) };
369
+ }
370
+ const domain = parts[0];
371
+ if (parts[1] === "kernel" && parts[2] === "tables") {
372
+ return { ownerDomain: domain, surface: "kernel-tables" };
373
+ }
374
+ if (parts[1] === "kernel" && parts[2] === "generated") {
375
+ return { ownerDomain: domain, surface: "kernel-generated" };
376
+ }
377
+ if (parts[1] === "kernel") {
378
+ return { ownerDomain: domain, surface: "kernel-contracts" };
379
+ }
380
+ return { ownerDomain: domain, surface: "domain-guides" };
381
+ }