@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,728 @@
1
+ import { execFile as execFileCallback } from "node:child_process";
2
+ import { readdir, readFile, stat } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { promisify } from "node:util";
5
+
6
+ import {
7
+ AUDITABLE_EXTENSIONS,
8
+ DEFAULT_CRITERIA,
9
+ DEFAULT_EXCLUDE_PATTERNS,
10
+ DEFAULT_MAX_FILES_PER_CHUNK,
11
+ appendRunEvent,
12
+ artifactPath,
13
+ chunkRef,
14
+ deriveSweepId,
15
+ inputError,
16
+ loadPlan,
17
+ normalizeCsv,
18
+ planRef,
19
+ relPath,
20
+ resolveInsideProject,
21
+ runLedgerRef,
22
+ safeSweepId,
23
+ sha256Object,
24
+ sha256Text,
25
+ toPosix,
26
+ writeYamlRef,
27
+ } from "./common.mjs";
28
+ import {
29
+ APP_SLICE_ADMISSION_REF,
30
+ AUDIT_SWEEP_PROJECT_CONFIG_REF,
31
+ loadAppSliceAdmissions,
32
+ loadAuditEvidenceRootAdmissions,
33
+ loadAuditSweepProjectConfig,
34
+ loadPackageAuthorityAdmissions,
35
+ } from "./admissions.mjs";
36
+ import { buildCoverageQuality } from "./coverage-quality.mjs";
37
+ import { assignEvidenceInventory } from "./evidence-assignment.mjs";
38
+ import { buildSpecChunks } from "./inventory-spec-chunks.mjs";
39
+ import { buildRiskBudgetPolicy } from "./risk-budget.mjs";
40
+ import { pathExists } from "../fs-helpers.mjs";
41
+
42
+ const execFile = promisify(execFileCallback);
43
+ async function listGitFiles(projectRoot, targetRootRef) {
44
+ try {
45
+ const result = await execFile(
46
+ "git",
47
+ ["ls-files", "--cached", "--others", "--exclude-standard", "--", targetRootRef],
48
+ { cwd: projectRoot },
49
+ );
50
+ return result.stdout.split(/\r?\n/).filter(Boolean).sort();
51
+ } catch {
52
+ return [];
53
+ }
54
+ }
55
+
56
+ function isExcluded(fileRef, excludePatterns) {
57
+ return excludePatterns.some((pattern) => {
58
+ const normalized = pattern.replace(/\\/g, "/");
59
+ if (!normalized) {
60
+ return false;
61
+ }
62
+ if (normalized.includes("*")) {
63
+ let patternText = "";
64
+ for (let index = 0; index < normalized.length; index += 1) {
65
+ const char = normalized[index];
66
+ if (char === "*" && normalized[index + 1] === "*") {
67
+ patternText += ".*";
68
+ index += 1;
69
+ } else if (char === "*") {
70
+ patternText += "[^/]*";
71
+ } else {
72
+ patternText += char.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
73
+ }
74
+ }
75
+ return new RegExp(`^${patternText}$`).test(fileRef);
76
+ }
77
+ if (normalized.endsWith("/")) {
78
+ return fileRef === normalized.slice(0, -1) || fileRef.startsWith(normalized);
79
+ }
80
+ return fileRef === normalized || fileRef.includes(normalized);
81
+ });
82
+ }
83
+
84
+ async function listFallbackFiles(projectRoot, targetRootRef, excludePatterns) {
85
+ const targetRoot = path.resolve(projectRoot, targetRootRef);
86
+ const files = [];
87
+
88
+ async function visit(currentPath) {
89
+ const entries = await readdir(currentPath, { withFileTypes: true });
90
+ for (const entry of entries) {
91
+ const absolutePath = path.join(currentPath, entry.name);
92
+ const fileRef = relPath(projectRoot, absolutePath);
93
+ if (isExcluded(entry.isDirectory() ? `${fileRef}/` : fileRef, excludePatterns)) {
94
+ continue;
95
+ }
96
+ if (entry.isDirectory()) {
97
+ await visit(absolutePath);
98
+ } else if (entry.isFile()) {
99
+ files.push(fileRef);
100
+ }
101
+ }
102
+ }
103
+
104
+ await visit(targetRoot);
105
+ return files.sort();
106
+ }
107
+
108
+ function classifyFile(fileRef) {
109
+ const extension = path.posix.extname(fileRef);
110
+ if ([".md", ".yaml", ".yml", ".json"].includes(extension)) {
111
+ return "contract-or-doc";
112
+ }
113
+ if ([".test.ts", ".test.js", ".spec.ts", ".spec.js"].some((suffix) => fileRef.endsWith(suffix))) {
114
+ return "test";
115
+ }
116
+ return "implementation";
117
+ }
118
+
119
+ function ownerDomainForFile(fileRef, targetRootRef) {
120
+ const normalizedTarget = targetRootRef === "." ? "" : `${targetRootRef.replace(/\/$/, "")}/`;
121
+ const withoutTarget = normalizedTarget && fileRef.startsWith(normalizedTarget)
122
+ ? fileRef.slice(normalizedTarget.length)
123
+ : fileRef;
124
+ const parts = withoutTarget.split("/");
125
+ if (parts.length <= 1) {
126
+ return targetRootRef === "." ? "root" : targetRootRef;
127
+ }
128
+ return path.posix.join(targetRootRef === "." ? "" : targetRootRef, parts[0]) || parts[0];
129
+ }
130
+
131
+ function isSpecAuthorityRoot(ref) {
132
+ return ref === ".nimi/spec" || ref === ".nimi/spec/";
133
+ }
134
+
135
+ async function hasSpecAuthorityRoot(projectRoot) {
136
+ const info = await pathExists(path.join(projectRoot, ".nimi", "spec"));
137
+ return info?.isDirectory() === true;
138
+ }
139
+
140
+ function resolveChunkBasis(targetRootRef, requested, specRootPresent) {
141
+ const normalized = requested ? String(requested).trim() : "auto";
142
+ if (!["auto", "files", "spec"].includes(normalized)) {
143
+ return { ok: false, error: "nimicoding audit-sweep refused: --chunk-basis must be auto, files, or spec.\n" };
144
+ }
145
+ if (normalized === "files") {
146
+ return { ok: true, basis: "files" };
147
+ }
148
+ if (normalized === "spec") {
149
+ return specRootPresent
150
+ ? { ok: true, basis: "spec" }
151
+ : { ok: false, error: "nimicoding audit-sweep refused: --chunk-basis spec requires .nimi/spec.\n" };
152
+ }
153
+ return { ok: true, basis: (targetRootRef === "." || isSpecAuthorityRoot(targetRootRef)) && specRootPresent ? "spec" : "files" };
154
+ }
155
+
156
+ async function buildInventoryEntry(projectRoot, fileRef, targetRootRef, excludePatterns, options = {}) {
157
+ const extension = path.posix.extname(fileRef);
158
+ const excluded = !options.forceAuthority && !fileRef.startsWith(".nimi/spec/") && isExcluded(fileRef, excludePatterns);
159
+ const auditable = AUDITABLE_EXTENSIONS.has(extension);
160
+ const absolutePath = artifactPath(projectRoot, fileRef);
161
+ const fileStat = await stat(absolutePath);
162
+ const contents = await readFile(absolutePath);
163
+ const included = !excluded && auditable;
164
+
165
+ return {
166
+ file_ref: fileRef,
167
+ sha256: sha256Text(contents),
168
+ bytes: fileStat.size,
169
+ extension: extension || "none",
170
+ owner_domain: options.ownerDomain ?? ownerDomainForFile(fileRef, targetRootRef),
171
+ classification: classifyFile(fileRef),
172
+ included,
173
+ exclusion_reason: included
174
+ ? null
175
+ : (excluded ? "matched_exclude_pattern" : "extension_not_auditable"),
176
+ };
177
+ }
178
+
179
+ function buildFileChunks(includedInventory, options) {
180
+ const byOwner = new Map();
181
+ for (const entry of includedInventory) {
182
+ const files = byOwner.get(entry.owner_domain) ?? [];
183
+ files.push(entry);
184
+ byOwner.set(entry.owner_domain, files);
185
+ }
186
+
187
+ const chunks = [];
188
+ for (const [ownerDomain, entries] of [...byOwner.entries()].sort(([left], [right]) => left.localeCompare(right))) {
189
+ const sortedEntries = entries.sort((left, right) => left.file_ref.localeCompare(right.file_ref));
190
+ for (let index = 0; index < sortedEntries.length; index += options.maxFilesPerChunk) {
191
+ const chunkEntries = sortedEntries.slice(index, index + options.maxFilesPerChunk);
192
+ chunks.push({
193
+ chunk_id: `chunk-${String(chunks.length + 1).padStart(3, "0")}`,
194
+ state: "planned",
195
+ owner_domain: ownerDomain,
196
+ criteria: options.criteria,
197
+ files: chunkEntries.map((entry) => entry.file_ref),
198
+ file_count: chunkEntries.length,
199
+ finding_count: 0,
200
+ });
201
+ }
202
+ }
203
+
204
+ return chunks;
205
+ }
206
+
207
+ function buildAuditIgnorePolicy(projectConfig, options) {
208
+ const patterns = [
209
+ ...(projectConfig.ignorePatterns ?? []),
210
+ ...normalizeCsv(options.ignore),
211
+ ];
212
+ const ownerDomains = [
213
+ ...(projectConfig.ignoreOwnerDomains ?? []),
214
+ ...normalizeCsv(options.ignoreOwner),
215
+ ];
216
+ const reason = typeof options.ignoreReason === "string" && options.ignoreReason.trim()
217
+ ? options.ignoreReason.trim()
218
+ : projectConfig.ignoreReason;
219
+ if (patterns.length === 0 && ownerDomains.length === 0) {
220
+ return null;
221
+ }
222
+ if (!reason) {
223
+ return {
224
+ ok: false,
225
+ error: "nimicoding audit-sweep refused: --ignore or --ignore-owner requires --ignore-reason, or .nimi/config/audit-sweep.yaml audit_sweep.ignore_reason.\n",
226
+ };
227
+ }
228
+ return {
229
+ ok: true,
230
+ policy: {
231
+ mode: "explicit_scope_omission",
232
+ patterns,
233
+ owner_domains: ownerDomains,
234
+ reason,
235
+ },
236
+ };
237
+ }
238
+
239
+ function chunkIgnoreMatches(chunk, ignorePolicy) {
240
+ if (!ignorePolicy) {
241
+ return [];
242
+ }
243
+ const matches = [];
244
+ if (ignorePolicy.owner_domains.includes(chunk.owner_domain)) {
245
+ matches.push(`owner:${chunk.owner_domain}`);
246
+ }
247
+ const refs = [
248
+ ...(chunk.files ?? []),
249
+ ...(chunk.authority_refs ?? []),
250
+ ...(chunk.evidence_roots ?? []),
251
+ ...(chunk.evidence_inventory ?? []),
252
+ ];
253
+ for (const pattern of ignorePolicy.patterns) {
254
+ if (refs.some((ref) => isExcluded(ref, [pattern]))) {
255
+ matches.push(`pattern:${pattern}`);
256
+ }
257
+ }
258
+ return [...new Set(matches)].sort();
259
+ }
260
+
261
+ function applyAuditIgnorePolicy(chunks, ignorePolicy, ignoredAt) {
262
+ if (!ignorePolicy) {
263
+ return { chunks, ignoredChunks: [] };
264
+ }
265
+ const ignoredChunks = [];
266
+ const nextChunks = chunks.map((chunk) => {
267
+ const matches = chunkIgnoreMatches(chunk, ignorePolicy);
268
+ if (matches.length === 0) {
269
+ return chunk;
270
+ }
271
+ ignoredChunks.push({
272
+ chunk_id: chunk.chunk_id,
273
+ owner_domain: chunk.owner_domain,
274
+ matches,
275
+ files: chunk.files ?? [],
276
+ authority_refs: chunk.authority_refs ?? chunk.files ?? [],
277
+ evidence_roots: chunk.evidence_roots ?? [],
278
+ });
279
+ return {
280
+ ...chunk,
281
+ state: "skipped",
282
+ skip: {
283
+ reason: ignorePolicy.reason,
284
+ ignored_by_policy: true,
285
+ matches,
286
+ skipped_at: ignoredAt,
287
+ },
288
+ };
289
+ });
290
+ return { chunks: nextChunks, ignoredChunks };
291
+ }
292
+
293
+ async function listAdmittedPackageAuthorityEntries(projectRoot, packageAuthorityAdmissions, excludePatterns) {
294
+ const entries = [];
295
+ for (const admission of packageAuthorityAdmissions) {
296
+ const rootInfo = await pathExists(artifactPath(projectRoot, admission.authority_root));
297
+ if (!rootInfo?.isDirectory()) {
298
+ return {
299
+ ok: false,
300
+ error: `nimicoding audit-sweep refused: package authority admission ${admission.id} authority_root is missing: ${admission.authority_root}.\n`,
301
+ };
302
+ }
303
+ const gitFiles = await listGitFiles(projectRoot, admission.authority_root);
304
+ const allFileRefs = gitFiles.length > 0
305
+ ? gitFiles.map((entry) => toPosix(entry))
306
+ : await listFallbackFiles(projectRoot, admission.authority_root, excludePatterns);
307
+ for (const fileRef of allFileRefs) {
308
+ const normalizedRef = toPosix(fileRef);
309
+ const extension = path.posix.extname(normalizedRef);
310
+ if (!AUDITABLE_EXTENSIONS.has(extension)) {
311
+ continue;
312
+ }
313
+ entries.push(await buildInventoryEntry(projectRoot, normalizedRef, admission.authority_root, excludePatterns, {
314
+ forceAuthority: true,
315
+ ownerDomain: admission.owner_domain,
316
+ }));
317
+ }
318
+ }
319
+ return { ok: true, entries };
320
+ }
321
+
322
+ async function listAdmittedAppAuthorityEntries(projectRoot, appSliceAdmissions, excludePatterns) {
323
+ const entries = [];
324
+ for (const admission of appSliceAdmissions) {
325
+ const rootInfo = await pathExists(artifactPath(projectRoot, admission.authority_root));
326
+ if (!rootInfo?.isDirectory()) {
327
+ return {
328
+ ok: false,
329
+ error: `nimicoding audit-sweep refused: app-slice admission ${admission.app_id} authority_root is missing: ${admission.authority_root}.\n`,
330
+ };
331
+ }
332
+ const gitFiles = await listGitFiles(projectRoot, admission.authority_root);
333
+ const allFileRefs = gitFiles.length > 0
334
+ ? gitFiles.map((entry) => toPosix(entry))
335
+ : await listFallbackFiles(projectRoot, admission.authority_root, excludePatterns);
336
+ for (const fileRef of allFileRefs) {
337
+ const normalizedRef = toPosix(fileRef);
338
+ const extension = path.posix.extname(normalizedRef);
339
+ if (!AUDITABLE_EXTENSIONS.has(extension)) {
340
+ continue;
341
+ }
342
+ entries.push(await buildInventoryEntry(projectRoot, normalizedRef, admission.authority_root, excludePatterns, {
343
+ forceAuthority: true,
344
+ ownerDomain: admission.owner_domain,
345
+ }));
346
+ }
347
+ }
348
+ return { ok: true, entries };
349
+ }
350
+
351
+ async function listAuditableEntriesForRoot(projectRoot, rootRef, excludePatterns) {
352
+ const rootInfo = await pathExists(artifactPath(projectRoot, rootRef));
353
+ if (!rootInfo) {
354
+ return [];
355
+ }
356
+ const gitFiles = await listGitFiles(projectRoot, rootRef);
357
+ const allFileRefs = gitFiles.length > 0
358
+ ? gitFiles.map((entry) => toPosix(entry))
359
+ : (rootInfo.isDirectory() ? await listFallbackFiles(projectRoot, rootRef, excludePatterns) : [rootRef]);
360
+ const entries = [];
361
+ for (const fileRef of allFileRefs) {
362
+ const normalizedRef = toPosix(fileRef);
363
+ if (isExcluded(normalizedRef, excludePatterns)) {
364
+ continue;
365
+ }
366
+ const extension = path.posix.extname(normalizedRef);
367
+ if (!AUDITABLE_EXTENSIONS.has(extension)) {
368
+ continue;
369
+ }
370
+ entries.push(await buildInventoryEntry(projectRoot, normalizedRef, ".", excludePatterns));
371
+ }
372
+ return entries.filter((entry) => entry.included);
373
+ }
374
+
375
+ export async function createAuditSweepPlan(projectRoot, options) {
376
+ const targetRoot = resolveInsideProject(projectRoot, options.root ?? ".", "--root");
377
+ if (!targetRoot.ok) {
378
+ return inputError(targetRoot.error);
379
+ }
380
+ const targetRootRef = targetRoot.ref || ".";
381
+
382
+ const targetInfo = await pathExists(targetRoot.absolutePath);
383
+ if (!targetInfo || !targetInfo.isDirectory()) {
384
+ return inputError("nimicoding audit-sweep refused: --root must point to an existing directory.\n");
385
+ }
386
+
387
+ const sweepId = options.sweepId ? safeSweepId(options.sweepId) : deriveSweepId(targetRootRef);
388
+ if (!sweepId) {
389
+ return inputError("nimicoding audit-sweep refused: --sweep-id must be a safe id.\n");
390
+ }
391
+
392
+ const specRootPresent = await hasSpecAuthorityRoot(projectRoot);
393
+ const chunkBasis = resolveChunkBasis(targetRootRef, options.chunkBasis, specRootPresent);
394
+ if (!chunkBasis.ok) {
395
+ return inputError(chunkBasis.error);
396
+ }
397
+ const inventoryRootRef = chunkBasis.basis === "spec" ? ".nimi/spec" : targetRootRef;
398
+ const criteria = normalizeCsv(options.criteria, DEFAULT_CRITERIA);
399
+ const projectConfig = await loadAuditSweepProjectConfig(projectRoot);
400
+ if (!projectConfig.ok) {
401
+ return inputError(projectConfig.error);
402
+ }
403
+ const auditIgnorePolicyResult = buildAuditIgnorePolicy(projectConfig, options);
404
+ if (auditIgnorePolicyResult?.ok === false) {
405
+ return inputError(auditIgnorePolicyResult.error);
406
+ }
407
+ const auditIgnorePolicy = auditIgnorePolicyResult?.policy ?? null;
408
+ const excludePatterns = [
409
+ ...DEFAULT_EXCLUDE_PATTERNS,
410
+ ...projectConfig.excludePatterns,
411
+ ...normalizeCsv(options.exclude),
412
+ ];
413
+ const maxFilesPerChunk = Number.isInteger(options.maxFilesPerChunk) && options.maxFilesPerChunk > 0
414
+ ? options.maxFilesPerChunk
415
+ : DEFAULT_MAX_FILES_PER_CHUNK;
416
+ const riskBudgetPolicy = buildRiskBudgetPolicy(options);
417
+ let appSliceAdmissions = [];
418
+ let auditEvidenceRootAdmissions = [];
419
+ let auditEvidenceRootAdmissionRefs = [];
420
+ let packageAuthorityAdmissions = [];
421
+ let packageAuthorityAdmissionRefs = [];
422
+ if (chunkBasis.basis === "spec") {
423
+ const loadedAdmissions = await loadAppSliceAdmissions(projectRoot);
424
+ if (!loadedAdmissions.ok) {
425
+ return inputError(loadedAdmissions.error);
426
+ }
427
+ appSliceAdmissions = loadedAdmissions.admissions;
428
+ const loadedEvidenceRootAdmissions = await loadAuditEvidenceRootAdmissions(projectRoot, listGitFiles, listFallbackFiles);
429
+ if (!loadedEvidenceRootAdmissions.ok) {
430
+ return inputError(loadedEvidenceRootAdmissions.error);
431
+ }
432
+ auditEvidenceRootAdmissions = loadedEvidenceRootAdmissions.admissions;
433
+ auditEvidenceRootAdmissionRefs = loadedEvidenceRootAdmissions.tableRefs;
434
+ const loadedPackageAuthorityAdmissions = await loadPackageAuthorityAdmissions(projectRoot, listGitFiles, listFallbackFiles);
435
+ if (!loadedPackageAuthorityAdmissions.ok) {
436
+ return inputError(loadedPackageAuthorityAdmissions.error);
437
+ }
438
+ packageAuthorityAdmissions = loadedPackageAuthorityAdmissions.admissions;
439
+ packageAuthorityAdmissionRefs = loadedPackageAuthorityAdmissions.tableRefs;
440
+ }
441
+ const gitFiles = await listGitFiles(projectRoot, inventoryRootRef);
442
+ const allFileRefs = gitFiles.length > 0
443
+ ? gitFiles.map((entry) => toPosix(entry))
444
+ : await listFallbackFiles(projectRoot, inventoryRootRef, excludePatterns);
445
+ const inventory = [];
446
+ for (const fileRef of allFileRefs) {
447
+ inventory.push(await buildInventoryEntry(projectRoot, fileRef, inventoryRootRef, excludePatterns, { forceAuthority: true }));
448
+ }
449
+ if (chunkBasis.basis === "spec" && appSliceAdmissions.length > 0) {
450
+ const appAuthorityEntries = await listAdmittedAppAuthorityEntries(projectRoot, appSliceAdmissions, excludePatterns);
451
+ if (!appAuthorityEntries.ok) {
452
+ return inputError(appAuthorityEntries.error);
453
+ }
454
+ const seenAuthorityRefs = new Set(inventory.map((entry) => entry.file_ref));
455
+ for (const entry of appAuthorityEntries.entries) {
456
+ if (!seenAuthorityRefs.has(entry.file_ref)) {
457
+ inventory.push(entry);
458
+ seenAuthorityRefs.add(entry.file_ref);
459
+ }
460
+ }
461
+ }
462
+ if (chunkBasis.basis === "spec" && packageAuthorityAdmissions.length > 0) {
463
+ const packageAuthorityEntries = await listAdmittedPackageAuthorityEntries(projectRoot, packageAuthorityAdmissions, excludePatterns);
464
+ if (!packageAuthorityEntries.ok) {
465
+ return inputError(packageAuthorityEntries.error);
466
+ }
467
+ const seenAuthorityRefs = new Set(inventory.map((entry) => entry.file_ref));
468
+ for (const entry of packageAuthorityEntries.entries) {
469
+ if (!seenAuthorityRefs.has(entry.file_ref)) {
470
+ inventory.push(entry);
471
+ seenAuthorityRefs.add(entry.file_ref);
472
+ }
473
+ }
474
+ }
475
+
476
+ const includedInventory = inventory.filter((entry) => entry.included);
477
+ const authorityFileRefs = new Set(includedInventory.map((entry) => entry.file_ref));
478
+ const authorityTextByRef = new Map();
479
+ if (chunkBasis.basis === "spec") {
480
+ for (const entry of includedInventory) {
481
+ if ([".md", ".markdown"].includes(entry.extension)) {
482
+ authorityTextByRef.set(entry.file_ref, await readFile(artifactPath(projectRoot, entry.file_ref), "utf8"));
483
+ }
484
+ }
485
+ }
486
+ let chunks = chunkBasis.basis === "spec"
487
+ ? buildSpecChunks(includedInventory, { criteria, targetRootRef, appSliceAdmissions, auditEvidenceRootAdmissions, packageAuthorityAdmissions, authorityTextByRef })
488
+ : buildFileChunks(includedInventory, { criteria, maxFilesPerChunk });
489
+ let evidenceInventory = [];
490
+ let unmappedEvidenceFiles = [];
491
+ let evidenceInventoryHash = null;
492
+ if (chunkBasis.basis === "spec") {
493
+ const evidenceRoots = [...new Set(chunks.flatMap((chunk) => chunk.evidence_roots ?? []))].sort();
494
+ const evidenceByFile = new Map();
495
+ for (const rootRef of evidenceRoots) {
496
+ const entries = await listAuditableEntriesForRoot(projectRoot, rootRef, excludePatterns);
497
+ for (const entry of entries) {
498
+ if (!authorityFileRefs.has(entry.file_ref)) {
499
+ evidenceByFile.set(entry.file_ref, entry);
500
+ }
501
+ }
502
+ }
503
+ evidenceInventory = [...evidenceByFile.values()].sort((left, right) => left.file_ref.localeCompare(right.file_ref));
504
+ const assigned = assignEvidenceInventory(evidenceInventory, chunks, {
505
+ maxEvidenceFilesPerChunk: maxFilesPerChunk,
506
+ });
507
+ chunks = assigned.chunks;
508
+ unmappedEvidenceFiles = assigned.unmappedEvidenceFiles;
509
+ evidenceInventoryHash = sha256Object(evidenceInventory.map((entry) => ({
510
+ file_ref: entry.file_ref,
511
+ sha256: entry.sha256,
512
+ included: entry.included,
513
+ exclusion_reason: entry.exclusion_reason,
514
+ })));
515
+ }
516
+ const createdAt = options.createdAt ?? new Date().toISOString();
517
+ const ignoreResult = applyAuditIgnorePolicy(chunks, auditIgnorePolicy, createdAt);
518
+ chunks = ignoreResult.chunks;
519
+ const inventoryHash = sha256Object(inventory.map((entry) => ({
520
+ file_ref: entry.file_ref,
521
+ sha256: entry.sha256,
522
+ included: entry.included,
523
+ exclusion_reason: entry.exclusion_reason,
524
+ })));
525
+ const plan = {
526
+ version: 1,
527
+ kind: "audit-plan",
528
+ sweep_id: sweepId,
529
+ target_root: targetRootRef,
530
+ planning_basis: {
531
+ mode: chunkBasis.basis === "spec" ? "spec_authority" : "file_inventory",
532
+ authority_root: chunkBasis.basis === "spec" ? ".nimi/spec" : null,
533
+ inventory_root: inventoryRootRef,
534
+ evidence_root: targetRootRef,
535
+ files_are_evidence_only: chunkBasis.basis === "spec",
536
+ },
537
+ criteria,
538
+ max_files_per_chunk: maxFilesPerChunk,
539
+ ...(auditIgnorePolicy ? {
540
+ audit_ignore_policy: {
541
+ ...auditIgnorePolicy,
542
+ ignored_chunk_count: ignoreResult.ignoredChunks.length,
543
+ ignored_chunks: ignoreResult.ignoredChunks,
544
+ },
545
+ } : {}),
546
+ ...(riskBudgetPolicy ? { risk_budget_policy: riskBudgetPolicy } : {}),
547
+ risk_budget_status: null,
548
+ audit_sweep_config_ref: projectConfig.found ? AUDIT_SWEEP_PROJECT_CONFIG_REF : null,
549
+ ...(chunkBasis.basis === "spec" && appSliceAdmissions.length > 0 ? {
550
+ app_slice_admission_ref: APP_SLICE_ADMISSION_REF,
551
+ app_slice_admissions: appSliceAdmissions.map((admission) => ({
552
+ app_id: admission.app_id,
553
+ owner_domain: admission.owner_domain,
554
+ status: admission.status,
555
+ authority_root: admission.authority_root,
556
+ evidence_roots: admission.evidence_roots,
557
+ admission_ref: admission.admission_ref,
558
+ })),
559
+ } : {}),
560
+ ...(chunkBasis.basis === "spec" && auditEvidenceRootAdmissionRefs.length > 0 ? {
561
+ audit_evidence_root_refs: auditEvidenceRootAdmissionRefs,
562
+ } : {}),
563
+ ...(chunkBasis.basis === "spec" && packageAuthorityAdmissions.length > 0 ? {
564
+ package_authority_admission_refs: packageAuthorityAdmissionRefs,
565
+ package_authority_admissions: packageAuthorityAdmissions.map((admission) => ({
566
+ id: admission.id,
567
+ owner_domain: admission.owner_domain,
568
+ status: admission.status,
569
+ authority_root: admission.authority_root,
570
+ evidence_roots: admission.evidence_roots,
571
+ host_authority_projection_refs: admission.host_authority_projection_refs ?? [],
572
+ admission_ref: admission.admission_ref,
573
+ })),
574
+ } : {}),
575
+ exclude_patterns: excludePatterns,
576
+ inventory_hash: inventoryHash,
577
+ ...(evidenceInventoryHash ? { evidence_inventory_hash: evidenceInventoryHash } : {}),
578
+ inventory,
579
+ ...(chunkBasis.basis === "spec" ? {
580
+ evidence_inventory: evidenceInventory.map((entry) => ({
581
+ file_ref: entry.file_ref,
582
+ sha256: entry.sha256,
583
+ bytes: entry.bytes,
584
+ extension: entry.extension,
585
+ owner_domain: entry.owner_domain,
586
+ classification: entry.classification,
587
+ included: entry.included,
588
+ exclusion_reason: entry.exclusion_reason,
589
+ })),
590
+ unmapped_evidence_files: unmappedEvidenceFiles,
591
+ } : {}),
592
+ chunks,
593
+ coverage: {
594
+ total_files: inventory.length,
595
+ included_files: includedInventory.length,
596
+ excluded_files: inventory.length - includedInventory.length,
597
+ ...(chunkBasis.basis === "spec" ? {
598
+ authority_files: includedInventory.length,
599
+ evidence_files: evidenceInventory.length,
600
+ unmapped_evidence_files: unmappedEvidenceFiles.length,
601
+ authority_chunks_without_evidence_inventory: chunks.filter((chunk) => (chunk.evidence_inventory ?? []).length === 0).length,
602
+ } : {}),
603
+ ...(auditIgnorePolicy ? {
604
+ ignored_chunks: ignoreResult.ignoredChunks.length,
605
+ } : {}),
606
+ chunk_count: chunks.length,
607
+ },
608
+ run_ledger_ref: runLedgerRef(sweepId),
609
+ created_at: createdAt,
610
+ updated_at: createdAt,
611
+ };
612
+ const coverageQuality = buildCoverageQuality(plan, chunks, plan.coverage);
613
+ if (coverageQuality) {
614
+ plan.coverage_quality = coverageQuality;
615
+ }
616
+
617
+ await writeYamlRef(projectRoot, planRef(sweepId), plan);
618
+ for (const chunk of chunks) {
619
+ const chunkInventory = includedInventory.filter((entry) => chunk.files.includes(entry.file_ref));
620
+ const evidenceByFile = new Map(evidenceInventory.map((entry) => [entry.file_ref, entry]));
621
+ await writeYamlRef(projectRoot, chunkRef(sweepId, chunk.chunk_id), {
622
+ version: 1,
623
+ kind: "audit-chunk",
624
+ sweep_id: sweepId,
625
+ chunk_id: chunk.chunk_id,
626
+ state: chunk.state,
627
+ owner_domain: chunk.owner_domain,
628
+ criteria,
629
+ files: chunk.files,
630
+ ...(chunk.planning_basis ? { planning_basis: chunk.planning_basis } : {}),
631
+ ...(chunk.spec_surface ? { spec_surface: chunk.spec_surface } : {}),
632
+ ...(chunk.authority_refs ? { authority_refs: chunk.authority_refs } : {}),
633
+ ...(chunk.authority_kind ? { authority_kind: chunk.authority_kind } : {}),
634
+ ...(chunk.app_id ? { app_id: chunk.app_id } : {}),
635
+ ...(chunk.package_authority_id ? { package_authority_id: chunk.package_authority_id } : {}),
636
+ ...(chunk.admission_ref ? { admission_ref: chunk.admission_ref } : {}),
637
+ ...(chunk.authority_root ? { authority_root: chunk.authority_root } : {}),
638
+ ...(chunk.evidence_root_admission_refs ? { evidence_root_admission_refs: chunk.evidence_root_admission_refs } : {}),
639
+ ...(chunk.admitted_evidence_roots ? { admitted_evidence_roots: chunk.admitted_evidence_roots } : {}),
640
+ ...(chunk.host_authority_projection_refs ? { host_authority_projection_refs: chunk.host_authority_projection_refs } : {}),
641
+ ...(chunk.declared_evidence_targets ? { declared_evidence_targets: chunk.declared_evidence_targets } : {}),
642
+ ...(chunk.evidence_roots ? { evidence_roots: chunk.evidence_roots } : {}),
643
+ ...(chunk.declared_evidence_unresolved ? { declared_evidence_unresolved: chunk.declared_evidence_unresolved } : {}),
644
+ ...(chunk.evidence_inventory ? { evidence_inventory: chunk.evidence_inventory } : {}),
645
+ ...(chunk.evidence_inventory_status ? { evidence_inventory_status: chunk.evidence_inventory_status } : {}),
646
+ ...(chunk.evidence_inventory_empty_reason ? { evidence_inventory_empty_reason: chunk.evidence_inventory_empty_reason } : {}),
647
+ ...(chunk.coverage_contract ? { coverage_contract: chunk.coverage_contract } : {}),
648
+ file_count: chunk.files.length,
649
+ file_hashes: Object.fromEntries(chunkInventory.map((entry) => [entry.file_ref, entry.sha256])),
650
+ ...(chunk.evidence_inventory ? {
651
+ evidence_file_hashes: Object.fromEntries(chunk.evidence_inventory.map((fileRef) => [fileRef, evidenceByFile.get(fileRef)?.sha256]).filter(([, hash]) => Boolean(hash))),
652
+ } : {}),
653
+ lifecycle: {
654
+ planned_at: createdAt,
655
+ dispatched_at: null,
656
+ ingested_at: null,
657
+ reviewed_at: null,
658
+ frozen_at: null,
659
+ failed_at: null,
660
+ skipped_at: chunk.state === "skipped" ? createdAt : null,
661
+ },
662
+ evidence_ref: null,
663
+ review: null,
664
+ failure: null,
665
+ ...(chunk.skip ? { skip: chunk.skip } : {}),
666
+ finding_count: 0,
667
+ created_at: createdAt,
668
+ updated_at: createdAt,
669
+ });
670
+ }
671
+
672
+ let runRef = await appendRunEvent(projectRoot, sweepId, {
673
+ event_type: "plan_created",
674
+ plan_ref: planRef(sweepId),
675
+ inventory_hash: inventoryHash,
676
+ included_files: includedInventory.length,
677
+ chunk_count: chunks.length,
678
+ ignored_chunk_count: ignoreResult.ignoredChunks.length,
679
+ });
680
+ for (const ignoredChunk of ignoreResult.ignoredChunks) {
681
+ runRef = await appendRunEvent(projectRoot, sweepId, {
682
+ event_type: "chunk_skipped",
683
+ chunk_id: ignoredChunk.chunk_id,
684
+ chunk_ref: chunkRef(sweepId, ignoredChunk.chunk_id),
685
+ reason: auditIgnorePolicy.reason,
686
+ ignored_by_policy: true,
687
+ matches: ignoredChunk.matches,
688
+ });
689
+ }
690
+
691
+ return {
692
+ ok: true,
693
+ exitCode: 0,
694
+ sweepId,
695
+ planRef: planRef(sweepId),
696
+ chunkRefs: chunks.map((chunk) => chunkRef(sweepId, chunk.chunk_id)),
697
+ runLedgerRef: runRef,
698
+ chunkCount: chunks.length,
699
+ totalFiles: inventory.length,
700
+ includedFiles: includedInventory.length,
701
+ excludedFiles: inventory.length - includedInventory.length,
702
+ ...(chunkBasis.basis === "spec" ? {
703
+ evidenceFiles: evidenceInventory.length,
704
+ unmappedEvidenceFiles: unmappedEvidenceFiles.length,
705
+ evidenceInventoryHash,
706
+ } : {}),
707
+ inventoryHash,
708
+ criteria,
709
+ maxFilesPerChunk,
710
+ auditIgnorePolicy: auditIgnorePolicy ? {
711
+ ...auditIgnorePolicy,
712
+ ignored_chunk_count: ignoreResult.ignoredChunks.length,
713
+ } : null,
714
+ riskBudgetPolicy,
715
+ chunkBasis: plan.planning_basis.mode,
716
+ };
717
+ }
718
+
719
+ export async function getPlannedChunkRefs(projectRoot, sweepId) {
720
+ const loaded = await loadPlan(projectRoot, sweepId);
721
+ if (!loaded.ok) {
722
+ return loaded;
723
+ }
724
+ return {
725
+ ok: true,
726
+ chunkRefs: loaded.plan.chunks.map((chunk) => chunkRef(sweepId, chunk.chunk_id)),
727
+ };
728
+ }