@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,329 @@
1
+ import { createHash } from "node:crypto";
2
+ import { appendFile, mkdir, open, readFile, rm, writeFile } from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ import YAML from "yaml";
6
+
7
+ import { AUDIT_SWEEP_ARTIFACT_ROOTS } from "../../constants.mjs";
8
+ import { pathExists } from "../fs-helpers.mjs";
9
+ import { loadYamlFile } from "../yaml-helpers.mjs";
10
+ import { isIsoUtcTimestamp, isPlainObject } from "../value-helpers.mjs";
11
+
12
+ export const DEFAULT_MAX_FILES_PER_CHUNK = 60;
13
+ export const DEFAULT_CRITERIA = ["quality"];
14
+ export const AUDITABLE_EXTENSIONS = new Set([
15
+ ".cjs",
16
+ ".css",
17
+ ".go",
18
+ ".js",
19
+ ".jsx",
20
+ ".json",
21
+ ".md",
22
+ ".mjs",
23
+ ".proto",
24
+ ".py",
25
+ ".rs",
26
+ ".ts",
27
+ ".tsx",
28
+ ".yaml",
29
+ ".yml",
30
+ ]);
31
+ export const DEFAULT_EXCLUDE_PATTERNS = [
32
+ ".git/",
33
+ ".next/",
34
+ ".nimi/local/",
35
+ ".turbo/",
36
+ "archive/",
37
+ "dist/",
38
+ "generated/",
39
+ "node_modules/",
40
+ "pnpm-lock.yaml",
41
+ "package-lock.json",
42
+ "yarn.lock",
43
+ ];
44
+ export const FINDING_SEVERITY = new Set(["critical", "high", "medium", "low"]);
45
+ export const FINDING_ACTIONABILITY = new Set(["auto-fix", "needs-decision", "deferred-backlog"]);
46
+ export const FINDING_CONFIDENCE = new Set(["high", "medium", "low"]);
47
+ export const FINDING_DISPOSITION = new Set(["open", "remediated", "accepted-risk", "false-positive", "deferred-backlog"]);
48
+ export const RERUN_VERDICT = new Set(["not_reproduced", "still_reproduced", "manager_accepted", "deferred"]);
49
+ export const CHUNK_STATES = new Set(["planned", "dispatched", "ingested", "reviewed", "frozen", "failed", "skipped"]);
50
+ export const ACTIVE_CHUNK_STATES = new Set(["planned", "dispatched", "ingested", "reviewed"]);
51
+ export const SEVERITY_RANK = {
52
+ critical: 0,
53
+ high: 1,
54
+ medium: 2,
55
+ low: 3,
56
+ };
57
+
58
+ export function nowIso() {
59
+ return new Date().toISOString();
60
+ }
61
+
62
+ export function toPosix(filePath) {
63
+ return filePath.split(path.sep).join(path.posix.sep);
64
+ }
65
+
66
+ export function stableJson(value) {
67
+ if (Array.isArray(value)) {
68
+ return `[${value.map(stableJson).join(",")}]`;
69
+ }
70
+
71
+ if (isPlainObject(value)) {
72
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
73
+ }
74
+
75
+ return JSON.stringify(value);
76
+ }
77
+
78
+ export function sha256Text(text) {
79
+ return createHash("sha256").update(text).digest("hex");
80
+ }
81
+
82
+ export function sha256Object(value) {
83
+ return sha256Text(stableJson(value));
84
+ }
85
+
86
+ export function relPath(projectRoot, absolutePath) {
87
+ return toPosix(path.relative(projectRoot, absolutePath));
88
+ }
89
+
90
+ export function isPathInside(parent, child) {
91
+ const relative = path.relative(parent, child);
92
+ return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
93
+ }
94
+
95
+ export function resolveInsideProject(projectRoot, inputPath, label) {
96
+ const absolutePath = path.resolve(projectRoot, inputPath);
97
+ if (!isPathInside(projectRoot, absolutePath)) {
98
+ return {
99
+ ok: false,
100
+ error: `nimicoding audit-sweep refused: ${label} must stay inside the project root.\n`,
101
+ };
102
+ }
103
+
104
+ return {
105
+ ok: true,
106
+ absolutePath,
107
+ ref: relPath(projectRoot, absolutePath),
108
+ };
109
+ }
110
+
111
+ export function normalizeCsv(value, fallback = []) {
112
+ if (!value) {
113
+ return [...fallback];
114
+ }
115
+
116
+ return String(value)
117
+ .split(",")
118
+ .map((entry) => entry.trim())
119
+ .filter(Boolean);
120
+ }
121
+
122
+ export function safeSweepId(value) {
123
+ if (typeof value !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9._-]{2,120}$/.test(value)) {
124
+ return null;
125
+ }
126
+
127
+ return value;
128
+ }
129
+
130
+ export function deriveSweepId(targetRootRef, date = new Date()) {
131
+ const day = date.toISOString().slice(0, 10);
132
+ const slug = targetRootRef
133
+ .replace(/[^a-zA-Z0-9]+/g, "-")
134
+ .replace(/^-+|-+$/g, "")
135
+ .toLowerCase() || "project";
136
+ return `audit-sweep-${day}-${slug}`;
137
+ }
138
+
139
+ export function artifactRef(kind, ...parts) {
140
+ return path.posix.join(AUDIT_SWEEP_ARTIFACT_ROOTS[kind], ...parts);
141
+ }
142
+
143
+ export function artifactPath(projectRoot, ref) {
144
+ return path.join(projectRoot, ...ref.split("/"));
145
+ }
146
+
147
+ export async function writeYamlRef(projectRoot, ref, value) {
148
+ const destination = artifactPath(projectRoot, ref);
149
+ await mkdir(path.dirname(destination), { recursive: true });
150
+ await writeFile(destination, YAML.stringify(value, { aliasDuplicateObjects: false }), "utf8");
151
+ }
152
+
153
+ export async function writeJsonRef(projectRoot, ref, value) {
154
+ const destination = artifactPath(projectRoot, ref);
155
+ await mkdir(path.dirname(destination), { recursive: true });
156
+ await writeFile(destination, `${JSON.stringify(value, null, 2)}\n`, "utf8");
157
+ }
158
+
159
+ function auditSweepLockPath(projectRoot, sweepId) {
160
+ return path.join(projectRoot, ".nimi", "local", "audit", "locks", `${sweepId}.lock`);
161
+ }
162
+
163
+ export async function withAuditSweepMutationLock(projectRoot, sweepId, label, fn) {
164
+ const lockPath = auditSweepLockPath(projectRoot, sweepId);
165
+ await mkdir(path.dirname(lockPath), { recursive: true });
166
+ let handle = null;
167
+ try {
168
+ handle = await open(lockPath, "wx");
169
+ } catch (error) {
170
+ if (error?.code === "EEXIST") {
171
+ return inputError(`nimicoding audit-sweep refused: ${label} mutation already in progress for ${sweepId}; retry after the current command finishes.\n`);
172
+ }
173
+ throw error;
174
+ }
175
+
176
+ try {
177
+ await handle.writeFile(`${JSON.stringify({
178
+ sweep_id: sweepId,
179
+ label,
180
+ pid: process.pid,
181
+ locked_at: nowIso(),
182
+ })}\n`, "utf8");
183
+ await handle.close();
184
+ handle = null;
185
+ return await fn();
186
+ } finally {
187
+ if (handle) {
188
+ await handle.close();
189
+ }
190
+ await rm(lockPath, { force: true });
191
+ }
192
+ }
193
+
194
+ export async function loadYamlRef(projectRoot, ref) {
195
+ return loadYamlFile(artifactPath(projectRoot, ref));
196
+ }
197
+
198
+ export async function loadJsonFile(filePath) {
199
+ try {
200
+ return { ok: true, value: JSON.parse(await readFile(filePath, "utf8")) };
201
+ } catch {
202
+ return { ok: false, error: "must contain valid JSON" };
203
+ }
204
+ }
205
+
206
+ export async function assertExistingFile(filePath, message) {
207
+ const info = await pathExists(filePath);
208
+ if (!info || !info.isFile()) {
209
+ return { ok: false, error: message };
210
+ }
211
+ return { ok: true };
212
+ }
213
+
214
+ export function planRef(sweepId) {
215
+ return artifactRef("plan_ref", `${sweepId}.yaml`);
216
+ }
217
+
218
+ export function chunkRef(sweepId, chunkId) {
219
+ return artifactRef("chunk_refs", sweepId, `${chunkId}.yaml`);
220
+ }
221
+
222
+ export function findingsRef(sweepId) {
223
+ return artifactRef("evidence_refs", sweepId, "findings.yaml");
224
+ }
225
+
226
+ export function runLedgerRef(sweepId) {
227
+ return artifactRef("run_ledger_ref", `${sweepId}.jsonl`);
228
+ }
229
+
230
+ export function reportRef(sweepId, snapshotId) {
231
+ return artifactRef("report_ref", sweepId, `${snapshotId}.md`);
232
+ }
233
+
234
+ export function ledgerRef(sweepId, snapshotId) {
235
+ return artifactRef("ledger_ref", sweepId, `${snapshotId}.yaml`);
236
+ }
237
+
238
+ export function remediationMapRef(sweepId, snapshotId) {
239
+ return artifactRef("remediation_map_ref", sweepId, `${snapshotId}.yaml`);
240
+ }
241
+
242
+ export function auditCloseoutRef(sweepId, snapshotId) {
243
+ return artifactRef("audit_closeout_ref", sweepId, `${snapshotId}.yaml`);
244
+ }
245
+
246
+ export function packetRef(sweepId, chunkId) {
247
+ return artifactRef("packet_ref", sweepId, `${chunkId}.auditor-packet.yaml`);
248
+ }
249
+
250
+ export async function appendRunEvent(projectRoot, sweepId, event) {
251
+ const ref = runLedgerRef(sweepId);
252
+ const destination = artifactPath(projectRoot, ref);
253
+ await mkdir(path.dirname(destination), { recursive: true });
254
+ await appendFile(destination, `${JSON.stringify({
255
+ event_id: sha256Object({ sweepId, event, ordinal_hint: Date.now() }).slice(0, 16),
256
+ sweep_id: sweepId,
257
+ recorded_at: nowIso(),
258
+ ...event,
259
+ })}\n`, "utf8");
260
+ return ref;
261
+ }
262
+
263
+ export async function loadPlan(projectRoot, sweepId) {
264
+ const ref = planRef(sweepId);
265
+ const plan = await loadYamlRef(projectRoot, ref);
266
+ if (!isPlainObject(plan) || plan.kind !== "audit-plan" || plan.sweep_id !== sweepId) {
267
+ return { ok: false, error: `nimicoding audit-sweep refused: plan not found for ${sweepId}.\n` };
268
+ }
269
+ return { ok: true, plan, planRef: ref };
270
+ }
271
+
272
+ export async function loadChunk(projectRoot, sweepId, chunkId) {
273
+ const ref = chunkRef(sweepId, chunkId);
274
+ const chunk = await loadYamlRef(projectRoot, ref);
275
+ if (!isPlainObject(chunk) || chunk.kind !== "audit-chunk" || chunk.sweep_id !== sweepId || chunk.chunk_id !== chunkId) {
276
+ return { ok: false, error: `nimicoding audit-sweep refused: chunk not found for ${sweepId}/${chunkId}.\n` };
277
+ }
278
+ return { ok: true, chunk, chunkRef: ref };
279
+ }
280
+
281
+ export async function loadFindings(projectRoot, sweepId) {
282
+ const ref = findingsRef(sweepId);
283
+ const existing = await loadYamlRef(projectRoot, ref);
284
+ if (isPlainObject(existing) && existing.kind === "audit-findings" && existing.sweep_id === sweepId && Array.isArray(existing.findings)) {
285
+ return { findingsRef: ref, store: existing };
286
+ }
287
+
288
+ return {
289
+ findingsRef: ref,
290
+ store: {
291
+ version: 1,
292
+ kind: "audit-findings",
293
+ sweep_id: sweepId,
294
+ findings: [],
295
+ duplicate_count: 0,
296
+ clusters: [],
297
+ clustered_symptom_count: 0,
298
+ accepted_cluster_skip_count: 0,
299
+ remediation_obligation_count: 0,
300
+ updated_at: nowIso(),
301
+ },
302
+ };
303
+ }
304
+
305
+ export async function loadLatestLedger(projectRoot, sweepId) {
306
+ const latestRef = artifactRef("ledger_ref", sweepId, "latest.yaml");
307
+ const pointer = await loadYamlRef(projectRoot, latestRef);
308
+ if (!isPlainObject(pointer) || typeof pointer.ledger_ref !== "string") {
309
+ return { ok: false, error: `nimicoding audit-sweep refused: latest ledger not found for ${sweepId}.\n` };
310
+ }
311
+
312
+ const ledger = await loadYamlRef(projectRoot, pointer.ledger_ref);
313
+ if (!isPlainObject(ledger) || ledger.kind !== "audit-ledger" || ledger.sweep_id !== sweepId) {
314
+ return { ok: false, error: `nimicoding audit-sweep refused: latest ledger is malformed for ${sweepId}.\n` };
315
+ }
316
+
317
+ return { ok: true, ledger, ledgerRef: pointer.ledger_ref, pointerRef: latestRef };
318
+ }
319
+
320
+ export function inputError(error) {
321
+ return { ok: false, inputError: true, exitCode: 2, error };
322
+ }
323
+
324
+ export function ensureIsoTimestamp(value, label = "--verified-at") {
325
+ if (!isIsoUtcTimestamp(value)) {
326
+ return inputError(`nimicoding audit-sweep refused: ${label} must be an ISO-8601 UTC timestamp.\n`);
327
+ }
328
+ return null;
329
+ }
@@ -0,0 +1,172 @@
1
+ export const COVERAGE_SCOPE_LABEL = "declared_authority_and_evidence_inventory";
2
+ export const FILE_INVENTORY_SCOPE_LABEL = "file_inventory";
3
+
4
+ function makeDiagnostic(id, message, details = {}) {
5
+ return { id, message, ...details };
6
+ }
7
+
8
+ function ownerDomainCoverage(chunks) {
9
+ const byOwner = {};
10
+ for (const chunk of chunks) {
11
+ const ownerDomain = chunk.owner_domain ?? "unknown";
12
+ const evidenceCount = Array.isArray(chunk.evidence_inventory) ? chunk.evidence_inventory.length : 0;
13
+ const entry = byOwner[ownerDomain] ?? {
14
+ authority_chunks: 0,
15
+ chunks_with_evidence_inventory: 0,
16
+ chunks_without_evidence_inventory: 0,
17
+ evidence_file_refs: new Set(),
18
+ posture: "strong",
19
+ };
20
+ entry.authority_chunks += 1;
21
+ for (const fileRef of Array.isArray(chunk.evidence_inventory) ? chunk.evidence_inventory : []) {
22
+ entry.evidence_file_refs.add(fileRef);
23
+ }
24
+ if (evidenceCount > 0) {
25
+ entry.chunks_with_evidence_inventory += 1;
26
+ } else {
27
+ entry.chunks_without_evidence_inventory += 1;
28
+ }
29
+ byOwner[ownerDomain] = entry;
30
+ }
31
+ for (const entry of Object.values(byOwner)) {
32
+ entry.evidence_files = entry.evidence_file_refs.size;
33
+ delete entry.evidence_file_refs;
34
+ entry.posture = entry.authority_chunks > 0 && entry.evidence_files === 0 ? "warning" : "strong";
35
+ }
36
+ return byOwner;
37
+ }
38
+
39
+ export function buildCoverageQuality(plan, chunks, coverage = plan.coverage ?? {}) {
40
+ if (plan?.planning_basis?.mode !== "spec_authority") {
41
+ return {
42
+ scope_label: FILE_INVENTORY_SCOPE_LABEL,
43
+ posture: "strong",
44
+ authority_chunk_count: chunks.length,
45
+ chunks_with_evidence_inventory: 0,
46
+ chunks_without_evidence_inventory: 0,
47
+ empty_evidence_chunk_ratio: 0,
48
+ evidence_file_count: 0,
49
+ max_evidence_files_per_chunk: 0,
50
+ max_evidence_chunk_id: null,
51
+ evidence_concentration_ratio: 0,
52
+ owner_domain_coverage: ownerDomainCoverage(chunks),
53
+ warnings: [],
54
+ blockers: [],
55
+ };
56
+ }
57
+
58
+ const authorityChunkCount = chunks.length;
59
+ const evidenceCounts = chunks.map((chunk) => Array.isArray(chunk.evidence_inventory) ? chunk.evidence_inventory.length : 0);
60
+ const chunksWithEvidenceInventory = evidenceCounts.filter((count) => count > 0).length;
61
+ const chunksWithoutEvidenceInventory = authorityChunkCount - chunksWithEvidenceInventory;
62
+ const evidenceFileCount = coverage.evidence_coverage?.total_files
63
+ ?? coverage.evidence_files
64
+ ?? plan.evidence_inventory?.length
65
+ ?? 0;
66
+ const maxEvidenceFilesPerChunk = evidenceCounts.length > 0 ? Math.max(...evidenceCounts) : 0;
67
+ const maxEvidenceChunkIndex = evidenceCounts.findIndex((count) => count === maxEvidenceFilesPerChunk);
68
+ const maxEvidenceChunkId = maxEvidenceChunkIndex >= 0 ? chunks[maxEvidenceChunkIndex]?.chunk_id ?? null : null;
69
+ const evidenceConcentrationRatio = evidenceFileCount > 0 ? maxEvidenceFilesPerChunk / evidenceFileCount : 0;
70
+ const emptyEvidenceChunkRatio = authorityChunkCount > 0 ? chunksWithoutEvidenceInventory / authorityChunkCount : 0;
71
+ const ownerCoverage = ownerDomainCoverage(chunks);
72
+ const warnings = [];
73
+ const blockers = [];
74
+
75
+ if (chunksWithoutEvidenceInventory > 0) {
76
+ warnings.push(makeDiagnostic("sparse_evidence_inventory", "Some authority chunks have no mapped evidence inventory.", {
77
+ chunks_without_evidence_inventory: chunksWithoutEvidenceInventory,
78
+ authority_chunk_count: authorityChunkCount,
79
+ }));
80
+ }
81
+
82
+ for (const [owner_domain, entry] of Object.entries(ownerCoverage)) {
83
+ if (entry.authority_chunks > 0 && entry.evidence_files === 0) {
84
+ warnings.push(makeDiagnostic("owner_domain_authority_only", "Owner domain has authority chunks but no mapped evidence files.", {
85
+ owner_domain,
86
+ authority_chunks: entry.authority_chunks,
87
+ }));
88
+ }
89
+ }
90
+
91
+ if (evidenceFileCount > 0 && evidenceConcentrationRatio >= 0.5 && chunksWithEvidenceInventory > 1) {
92
+ warnings.push(makeDiagnostic("evidence_fan_in_concentrated", "One chunk owns a concentrated share of the mapped evidence inventory.", {
93
+ chunk_id: maxEvidenceChunkId,
94
+ evidence_concentration_ratio: evidenceConcentrationRatio,
95
+ }));
96
+ }
97
+
98
+ const unresolvedChunks = chunks
99
+ .filter((chunk) => Array.isArray(chunk.declared_evidence_unresolved) && chunk.declared_evidence_unresolved.length > 0)
100
+ .map((chunk) => chunk.chunk_id);
101
+ if (unresolvedChunks.length > 0) {
102
+ blockers.push(makeDiagnostic("declared_evidence_target_unresolved", "Declared evidence targets remain unresolved.", {
103
+ chunk_ids: unresolvedChunks,
104
+ }));
105
+ }
106
+
107
+ const unmappedEvidenceFiles = coverage.evidence_coverage?.unmapped_files
108
+ ?? coverage.unmapped_evidence_files
109
+ ?? plan.unmapped_evidence_files?.length
110
+ ?? 0;
111
+ if (unmappedEvidenceFiles > 0) {
112
+ blockers.push(makeDiagnostic("unmapped_evidence_files", "Evidence inventory contains files that no authority chunk can accept.", {
113
+ unmapped_evidence_files: unmappedEvidenceFiles,
114
+ }));
115
+ }
116
+
117
+ return {
118
+ scope_label: COVERAGE_SCOPE_LABEL,
119
+ posture: blockers.length > 0 ? "blocked" : warnings.length > 0 ? "warning" : "strong",
120
+ authority_chunk_count: authorityChunkCount,
121
+ chunks_with_evidence_inventory: chunksWithEvidenceInventory,
122
+ chunks_without_evidence_inventory: chunksWithoutEvidenceInventory,
123
+ empty_evidence_chunk_ratio: emptyEvidenceChunkRatio,
124
+ evidence_file_count: evidenceFileCount,
125
+ max_evidence_files_per_chunk: maxEvidenceFilesPerChunk,
126
+ max_evidence_chunk_id: maxEvidenceChunkId,
127
+ evidence_concentration_ratio: evidenceConcentrationRatio,
128
+ owner_domain_coverage: ownerCoverage,
129
+ warnings,
130
+ blockers,
131
+ };
132
+ }
133
+
134
+ export function withFullScopeWarning(coverageQuality) {
135
+ if (!coverageQuality) {
136
+ return coverageQuality;
137
+ }
138
+ if (coverageQuality.scope_label !== COVERAGE_SCOPE_LABEL) {
139
+ return coverageQuality;
140
+ }
141
+ if (coverageQuality.warnings?.some((warning) => warning.id === "full_status_scope_is_declared_inventory")) {
142
+ return coverageQuality;
143
+ }
144
+ return {
145
+ ...coverageQuality,
146
+ posture: coverageQuality.posture === "strong" ? "warning" : coverageQuality.posture,
147
+ warnings: [
148
+ ...(coverageQuality.warnings ?? []),
149
+ makeDiagnostic("full_status_scope_is_declared_inventory", "Full spec-authority coverage is scoped to declared authority and admitted evidence inventory."),
150
+ ],
151
+ };
152
+ }
153
+
154
+ export function deriveCoverageStatus(ledgerStatus) {
155
+ if (ledgerStatus === "candidate_ready") {
156
+ return "full";
157
+ }
158
+ if (ledgerStatus === "blocked") {
159
+ return "blocked";
160
+ }
161
+ return "partial";
162
+ }
163
+
164
+ export function deriveCoverageCloseoutPosture({ coverageStatus, openFindingCount }) {
165
+ if (coverageStatus === "blocked") {
166
+ return "blocked";
167
+ }
168
+ if (coverageStatus === "partial") {
169
+ return openFindingCount > 0 ? "partial_coverage_findings_open" : "partial_coverage_all_findings_postured";
170
+ }
171
+ return openFindingCount > 0 ? "audit_complete_findings_open" : "audit_complete_all_findings_postured";
172
+ }
@@ -0,0 +1,152 @@
1
+ function chunkMatchesEvidenceFile(chunk, fileRef) {
2
+ const roots = Array.isArray(chunk.admitted_evidence_roots) && chunk.admitted_evidence_roots.length > 0
3
+ ? chunk.admitted_evidence_roots
4
+ : chunk.evidence_roots;
5
+ return Array.isArray(roots)
6
+ && roots.some((rootRef) => {
7
+ const normalizedRoot = rootRef.replace(/\\/g, "/").replace(/\/$/, "");
8
+ return fileRef === normalizedRoot || fileRef.startsWith(`${normalizedRoot}/`);
9
+ });
10
+ }
11
+
12
+ function normalizedRef(value) {
13
+ return String(value ?? "").replace(/\\/g, "/").replace(/\/$/, "");
14
+ }
15
+
16
+ function refInsideRoot(fileRef, rootRef) {
17
+ const normalizedRoot = normalizedRef(rootRef);
18
+ return Boolean(normalizedRoot) && (fileRef === normalizedRoot || fileRef.startsWith(`${normalizedRoot}/`));
19
+ }
20
+
21
+ function chunkHasExactAdmittedEvidenceRef(chunk, fileRef) {
22
+ return Array.isArray(chunk.evidence_root_admission_refs)
23
+ && Array.isArray(chunk.admitted_evidence_roots)
24
+ && chunk.admitted_evidence_roots.some((rootRef) => normalizedRef(rootRef) === fileRef);
25
+ }
26
+
27
+ function topLevelEvidenceDocForRoot(rootRef, fileRef) {
28
+ const normalizedRoot = normalizedRef(rootRef);
29
+ if (!normalizedRoot || !refInsideRoot(fileRef, normalizedRoot)) {
30
+ return false;
31
+ }
32
+ const relative = fileRef === normalizedRoot ? "" : fileRef.slice(normalizedRoot.length + 1);
33
+ return !relative.includes("/") && /^(README|AGENTS)(?:\.[^.]+)?$/i.test(relative);
34
+ }
35
+
36
+ function chunkDeclaredEvidenceRoots(chunk) {
37
+ if (!Array.isArray(chunk.declared_evidence_targets)) {
38
+ return [];
39
+ }
40
+ return chunk.declared_evidence_targets
41
+ .flatMap((target) => Array.isArray(target.candidates) ? target.candidates : [])
42
+ .map((candidate) => normalizedRef(candidate))
43
+ .filter(Boolean);
44
+ }
45
+
46
+ function chunkAcceptsEvidenceFile(chunk, fileRef) {
47
+ const surface = String(chunk.spec_surface ?? "");
48
+ if (chunkDeclaredEvidenceRoots(chunk).some((rootRef) => refInsideRoot(fileRef, rootRef))) {
49
+ return true;
50
+ }
51
+ const roots = Array.isArray(chunk.admitted_evidence_roots) && chunk.admitted_evidence_roots.length > 0
52
+ ? chunk.admitted_evidence_roots
53
+ : chunk.evidence_roots;
54
+ const isTopLevelDoc = (roots ?? []).some((rootRef) => topLevelEvidenceDocForRoot(rootRef, fileRef));
55
+ if (isTopLevelDoc) {
56
+ return surface === "domain-guides" || surface === "app-domain-guides" || surface === "package-root" || surface === "INDEX";
57
+ }
58
+ if (
59
+ surface === "kernel-contracts"
60
+ || surface === "app-kernel-contracts"
61
+ || surface === "kernel-tables"
62
+ || surface === "app-kernel-tables"
63
+ || surface === "package-kernel-tables"
64
+ ) {
65
+ return true;
66
+ }
67
+ if (surface === "spec-generation-audit" || surface === "high-risk-admissions" || surface === "package-meta" || surface === "package-root") {
68
+ return true;
69
+ }
70
+ return false;
71
+ }
72
+
73
+ function unresolvedDeclaredEvidenceTargets(chunk, evidenceEntries) {
74
+ if (!Array.isArray(chunk.declared_evidence_targets)) {
75
+ return [];
76
+ }
77
+ return chunk.declared_evidence_targets
78
+ .map((target) => {
79
+ const candidates = Array.isArray(target.candidates)
80
+ ? target.candidates.map((candidate) => normalizedRef(candidate)).filter(Boolean)
81
+ : [];
82
+ const resolved = candidates.some((candidate) => evidenceEntries.some((entry) => refInsideRoot(entry.file_ref, candidate)));
83
+ return resolved ? null : {
84
+ source_path: target.source_path,
85
+ candidates,
86
+ };
87
+ })
88
+ .filter(Boolean);
89
+ }
90
+
91
+ function deriveEmptyEvidenceInventoryReason(chunk) {
92
+ if (chunk.spec_surface === "kernel-generated" || String(chunk.spec_surface ?? "").includes("generated")) {
93
+ return "generated_projection_authority_no_direct_implementation_evidence";
94
+ }
95
+ if (chunk.spec_surface === "kernel-tables" || String(chunk.spec_surface ?? "").includes("tables")) {
96
+ return "structured_fact_authority_no_direct_implementation_evidence";
97
+ }
98
+ if (chunk.spec_surface === "domain-guides" || String(chunk.spec_surface ?? "").endsWith("guides")) {
99
+ return "domain_guide_authority_no_direct_implementation_evidence";
100
+ }
101
+ return "no_matching_evidence_files_after_spec_authority_assignment";
102
+ }
103
+
104
+ export function assignEvidenceInventory(evidenceEntries, chunks, options = {}) {
105
+ const chunksById = new Map(chunks.map((chunk) => [chunk.chunk_id, { ...chunk, evidence_inventory: [] }]));
106
+ const unmapped = [];
107
+
108
+ for (const entry of evidenceEntries.sort((left, right) => left.file_ref.localeCompare(right.file_ref))) {
109
+ const candidates = chunks.filter((chunk) => chunkMatchesEvidenceFile(chunk, entry.file_ref));
110
+ if (candidates.length === 0) {
111
+ unmapped.push(entry.file_ref);
112
+ continue;
113
+ }
114
+ const exactAdmissionCandidates = candidates.filter((chunk) => chunkHasExactAdmittedEvidenceRef(chunk, entry.file_ref));
115
+ const selectedChunks = (exactAdmissionCandidates.length > 0 ? exactAdmissionCandidates : candidates.filter((chunk) => chunkAcceptsEvidenceFile(chunk, entry.file_ref)))
116
+ .sort((left, right) => left.chunk_id.localeCompare(right.chunk_id));
117
+ if (selectedChunks.length === 0) {
118
+ unmapped.push(entry.file_ref);
119
+ continue;
120
+ }
121
+ for (const selected of selectedChunks) {
122
+ chunksById.get(selected.chunk_id).evidence_inventory.push(entry.file_ref);
123
+ }
124
+ }
125
+
126
+ return {
127
+ chunks: chunks.map((chunk) => {
128
+ const enriched = chunksById.get(chunk.chunk_id);
129
+ const evidenceInventory = enriched.evidence_inventory.sort();
130
+ const evidenceInventoryEmpty = evidenceInventory.length === 0;
131
+ const declaredEvidenceUnresolved = unresolvedDeclaredEvidenceTargets(chunk, evidenceEntries);
132
+ return {
133
+ ...chunk,
134
+ evidence_inventory: evidenceInventory,
135
+ evidence_inventory_status: evidenceInventoryEmpty ? "empty" : "mapped",
136
+ ...(declaredEvidenceUnresolved.length > 0 ? {
137
+ declared_evidence_unresolved: declaredEvidenceUnresolved,
138
+ } : {}),
139
+ ...(evidenceInventoryEmpty ? {
140
+ evidence_inventory_empty_reason: deriveEmptyEvidenceInventoryReason(chunk),
141
+ } : {}),
142
+ coverage_contract: {
143
+ authority_refs_required: true,
144
+ evidence_inventory_required: true,
145
+ evidence_files_must_cover_inventory: true,
146
+ empty_evidence_inventory_requires_reason: true,
147
+ },
148
+ };
149
+ }),
150
+ unmappedEvidenceFiles: unmapped.sort(),
151
+ };
152
+ }