@nimiplatform/nimi-coding 0.2.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/CHANGELOG.md +8 -80
  2. package/CONTRIBUTING.md +5 -34
  3. package/README.md +47 -350
  4. package/README.zh-CN.md +33 -287
  5. package/cli/commands/start.mjs +48 -688
  6. package/cli/commands/validate-spec-audit.mjs +1 -5
  7. package/cli/constants.mjs +9 -417
  8. package/cli/dev/run-cli-smoke.mjs +24 -0
  9. package/cli/dev/run-suite.mjs +17 -0
  10. package/cli/help.mjs +28 -145
  11. package/cli/index.mjs +5 -33
  12. package/cli/lib/blueprint-audit.mjs +5 -12
  13. package/cli/lib/bootstrap.mjs +0 -9
  14. package/cli/lib/contracts.mjs +0 -163
  15. package/cli/lib/doctor.mjs +155 -10
  16. package/cli/lib/entrypoints.mjs +63 -167
  17. package/cli/lib/internal/contracts-loaders.mjs +0 -85
  18. package/cli/lib/internal/contracts-parse.mjs +84 -520
  19. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +13 -8
  20. package/cli/lib/internal/surface-taxonomy-validators.mjs +316 -66
  21. package/cli/lib/internal/validators-shared.mjs +0 -16
  22. package/cli/lib/internal/validators-spec-helpers.mjs +7 -140
  23. package/cli/lib/internal/validators-spec.mjs +103 -342
  24. package/cli/lib/shared.mjs +1 -42
  25. package/cli/lib/validators.mjs +0 -48
  26. package/cli/seeds/seed-policy.yaml +0 -3
  27. package/config/bootstrap.yaml +4 -4
  28. package/config/spec-generation-inputs.yaml +6 -12
  29. package/contracts/domain-admission.schema.yaml +3 -3
  30. package/contracts/migration-inventory.schema.yaml +12 -75
  31. package/contracts/negative-fixtures.yaml +19 -99
  32. package/contracts/placement-contract.schema.yaml +49 -78
  33. package/contracts/projection-edge.schema.yaml +37 -121
  34. package/contracts/shared-enums.yaml +4 -8
  35. package/contracts/spec-generation-inputs.schema.yaml +15 -112
  36. package/contracts/spec-layout.schema.yaml +32 -0
  37. package/contracts/surface-taxonomy.schema.yaml +43 -94
  38. package/contracts/table-family.schema.yaml +1 -1
  39. package/contracts/tracked-output-admission.schema.yaml +13 -66
  40. package/methodology/core.yaml +33 -25
  41. package/methodology/four-closure-policy.yaml +18 -26
  42. package/methodology/role-separation-policy.yaml +21 -27
  43. package/methodology/spec-reconstruction.yaml +17 -34
  44. package/package.json +4 -6
  45. package/spec/_meta/spec-tree-model.yaml +5 -101
  46. package/spec/product-scope.yaml +25 -57
  47. package/adapters/README.md +0 -25
  48. package/adapters/claude/README.md +0 -89
  49. package/adapters/claude/profile.yaml +0 -70
  50. package/adapters/codex/README.md +0 -53
  51. package/adapters/codex/profile.yaml +0 -78
  52. package/adapters/oh-my-codex/README.md +0 -184
  53. package/adapters/oh-my-codex/profile.yaml +0 -46
  54. package/cli/commands/admit-high-risk-decision.mjs +0 -108
  55. package/cli/commands/audit-sweep.mjs +0 -364
  56. package/cli/commands/closeout.mjs +0 -186
  57. package/cli/commands/decide-high-risk-execution.mjs +0 -124
  58. package/cli/commands/handoff.mjs +0 -123
  59. package/cli/commands/ingest-high-risk-execution.mjs +0 -95
  60. package/cli/commands/review-high-risk-execution.mjs +0 -95
  61. package/cli/commands/sweep-design.mjs +0 -295
  62. package/cli/commands/sweep.mjs +0 -22
  63. package/cli/commands/topic-formatters.mjs +0 -382
  64. package/cli/commands/topic-goal.mjs +0 -33
  65. package/cli/commands/topic-options-shared.mjs +0 -27
  66. package/cli/commands/topic-options-workflow.mjs +0 -767
  67. package/cli/commands/topic-options.mjs +0 -626
  68. package/cli/commands/topic-runner.mjs +0 -169
  69. package/cli/commands/topic.mjs +0 -795
  70. package/cli/commands/validate-acceptance.mjs +0 -5
  71. package/cli/commands/validate-execution-packet.mjs +0 -5
  72. package/cli/commands/validate-orchestration-state.mjs +0 -5
  73. package/cli/commands/validate-prompt.mjs +0 -5
  74. package/cli/commands/validate-worker-output.mjs +0 -5
  75. package/cli/lib/adapter-profiles.mjs +0 -403
  76. package/cli/lib/audit-execution.mjs +0 -52
  77. package/cli/lib/audit-sweep-runtime/admissions.mjs +0 -508
  78. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +0 -356
  79. package/cli/lib/audit-sweep-runtime/chunks.mjs +0 -697
  80. package/cli/lib/audit-sweep-runtime/claude-auditor.mjs +0 -647
  81. package/cli/lib/audit-sweep-runtime/closeout.mjs +0 -144
  82. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +0 -654
  83. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +0 -516
  84. package/cli/lib/audit-sweep-runtime/common.mjs +0 -341
  85. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +0 -172
  86. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +0 -155
  87. package/cli/lib/audit-sweep-runtime/format.mjs +0 -57
  88. package/cli/lib/audit-sweep-runtime/ingest.mjs +0 -486
  89. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +0 -425
  90. package/cli/lib/audit-sweep-runtime/inventory.mjs +0 -798
  91. package/cli/lib/audit-sweep-runtime/ledger.mjs +0 -315
  92. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +0 -101
  93. package/cli/lib/audit-sweep-runtime/remediation.mjs +0 -349
  94. package/cli/lib/audit-sweep-runtime/rerun.mjs +0 -129
  95. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +0 -300
  96. package/cli/lib/audit-sweep-runtime/status.mjs +0 -62
  97. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +0 -215
  98. package/cli/lib/audit-sweep-runtime/validators.mjs +0 -797
  99. package/cli/lib/audit-sweep.mjs +0 -19
  100. package/cli/lib/authority-convergence.mjs +0 -704
  101. package/cli/lib/closeout.mjs +0 -732
  102. package/cli/lib/codex-sdk-runner.mjs +0 -76
  103. package/cli/lib/external-execution.mjs +0 -101
  104. package/cli/lib/handoff.mjs +0 -812
  105. package/cli/lib/high-risk-admission.mjs +0 -512
  106. package/cli/lib/high-risk-decision.mjs +0 -353
  107. package/cli/lib/high-risk-ingest.mjs +0 -321
  108. package/cli/lib/high-risk-review.mjs +0 -267
  109. package/cli/lib/internal/contracts-parse-high-risk.mjs +0 -131
  110. package/cli/lib/internal/contracts-validators.mjs +0 -399
  111. package/cli/lib/internal/doctor-bootstrap-surface.mjs +0 -406
  112. package/cli/lib/internal/doctor-delegated-surface.mjs +0 -256
  113. package/cli/lib/internal/doctor-finalize.mjs +0 -383
  114. package/cli/lib/internal/doctor-format.mjs +0 -286
  115. package/cli/lib/internal/doctor-inspectors.mjs +0 -327
  116. package/cli/lib/internal/doctor-state.mjs +0 -205
  117. package/cli/lib/internal/validators-artifacts.mjs +0 -515
  118. package/cli/lib/sweep-design-runtime/common.mjs +0 -246
  119. package/cli/lib/sweep-design-runtime/engine.mjs +0 -733
  120. package/cli/lib/sweep-design-runtime/fix-topic.mjs +0 -414
  121. package/cli/lib/sweep-design-runtime/lifecycle.mjs +0 -54
  122. package/cli/lib/sweep-design-runtime/results.mjs +0 -324
  123. package/cli/lib/sweep-design.mjs +0 -8
  124. package/cli/lib/topic-artifacts.mjs +0 -186
  125. package/cli/lib/topic-authority-coverage.mjs +0 -73
  126. package/cli/lib/topic-closeout.mjs +0 -560
  127. package/cli/lib/topic-common.mjs +0 -404
  128. package/cli/lib/topic-decisions.mjs +0 -332
  129. package/cli/lib/topic-draft-packets.mjs +0 -167
  130. package/cli/lib/topic-execution.mjs +0 -515
  131. package/cli/lib/topic-goal.mjs +0 -440
  132. package/cli/lib/topic-ledger.mjs +0 -281
  133. package/cli/lib/topic-lifecycle-artifacts.mjs +0 -173
  134. package/cli/lib/topic-root-validation.mjs +0 -288
  135. package/cli/lib/topic-runner-commands.mjs +0 -174
  136. package/cli/lib/topic-runner-deferral.mjs +0 -532
  137. package/cli/lib/topic-runner-stale-gates.mjs +0 -114
  138. package/cli/lib/topic-runner-validation.mjs +0 -138
  139. package/cli/lib/topic-runner.mjs +0 -727
  140. package/cli/lib/topic-scaffold.mjs +0 -252
  141. package/cli/lib/topic-waves.mjs +0 -403
  142. package/cli/lib/topic.mjs +0 -81
  143. package/config/audit-execution-artifacts.yaml +0 -20
  144. package/config/external-execution-artifacts.yaml +0 -16
  145. package/config/host-adapter.yaml +0 -30
  146. package/config/host-profile.yaml +0 -29
  147. package/config/installer-evidence.yaml +0 -31
  148. package/config/skill-installer.yaml +0 -23
  149. package/config/skill-manifest.yaml +0 -48
  150. package/config/skills.yaml +0 -30
  151. package/contracts/acceptance.schema.yaml +0 -16
  152. package/contracts/admission-checklist.schema.yaml +0 -15
  153. package/contracts/audit-chunk.schema.yaml +0 -123
  154. package/contracts/audit-closeout.schema.yaml +0 -51
  155. package/contracts/audit-finding.schema.yaml +0 -61
  156. package/contracts/audit-ledger.schema.yaml +0 -138
  157. package/contracts/audit-plan.schema.yaml +0 -138
  158. package/contracts/audit-remediation-map.schema.yaml +0 -52
  159. package/contracts/audit-rerun.schema.yaml +0 -31
  160. package/contracts/audit-sweep-result.yaml +0 -53
  161. package/contracts/authority-convergence-audit.schema.yaml +0 -19
  162. package/contracts/closeout.schema.yaml +0 -25
  163. package/contracts/decision-review.schema.yaml +0 -16
  164. package/contracts/doc-spec-audit-result.yaml +0 -19
  165. package/contracts/execution-packet.schema.yaml +0 -49
  166. package/contracts/external-host-compatibility.yaml +0 -22
  167. package/contracts/forbidden-shortcuts.catalog.yaml +0 -23
  168. package/contracts/high-risk-admission.schema.yaml +0 -23
  169. package/contracts/high-risk-execution-result.yaml +0 -20
  170. package/contracts/orchestration-state.schema.yaml +0 -41
  171. package/contracts/overflow-continuation.schema.yaml +0 -12
  172. package/contracts/packet.schema.yaml +0 -30
  173. package/contracts/pending-note.schema.yaml +0 -17
  174. package/contracts/prompt.schema.yaml +0 -12
  175. package/contracts/remediation.schema.yaml +0 -16
  176. package/contracts/result.schema.yaml +0 -24
  177. package/contracts/spec-reconstruction-result.yaml +0 -41
  178. package/contracts/sweep-design-result.yaml +0 -349
  179. package/contracts/topic-goal.schema.yaml +0 -87
  180. package/contracts/topic-run-ledger.schema.yaml +0 -72
  181. package/contracts/topic-step-decision.schema.yaml +0 -45
  182. package/contracts/topic.schema.yaml +0 -65
  183. package/contracts/true-close.schema.yaml +0 -15
  184. package/contracts/wave.schema.yaml +0 -29
  185. package/contracts/worker-output.schema.yaml +0 -15
  186. package/contracts/workflow-consumer.schema.yaml +0 -110
  187. package/methodology/audit-sweep-p0p1-recall.yaml +0 -45
  188. package/methodology/authority-convergence-policy.yaml +0 -42
  189. package/methodology/overflow-continuation-policy.yaml +0 -14
  190. package/methodology/skill-exchange-projection.yaml +0 -114
  191. package/methodology/skill-handoff.yaml +0 -34
  192. package/methodology/skill-installer-result.yaml +0 -27
  193. package/methodology/skill-installer-summary-projection.yaml +0 -181
  194. package/methodology/skill-runtime.yaml +0 -23
  195. package/methodology/spec-target-truth-profile.yaml +0 -46
  196. package/methodology/topic-lifecycle-report.yaml +0 -144
  197. package/methodology/topic-lifecycle.yaml +0 -37
  198. package/methodology/topic-naming-ontology.yaml +0 -21
  199. package/methodology/topic-ontology.yaml +0 -38
  200. package/methodology/topic-validation-policy.yaml +0 -9
  201. package/methodology/wave-dag-policy.yaml +0 -14
  202. package/spec/_meta/command-gating-matrix.yaml +0 -143
  203. package/spec/_meta/generate-drift-migration-checklist.yaml +0 -137
  204. package/spec/_meta/governance-routing-cutover-checklist.yaml +0 -35
  205. package/spec/_meta/phase2-impacted-surface-matrix.yaml +0 -44
  206. package/spec/_meta/spec-authority-cutover-readiness.yaml +0 -102
  207. package/spec/bootstrap-state.yaml +0 -99
@@ -1,349 +0,0 @@
1
- import {
2
- SEVERITY_RANK,
3
- appendRunEvent,
4
- ensureIsoTimestamp,
5
- inputError,
6
- loadFindings,
7
- loadLatestLedger,
8
- loadPlan,
9
- loadYamlRef,
10
- remediationMapRef,
11
- safeSweepId,
12
- nowIso,
13
- writeYamlRef,
14
- } from "./common.mjs";
15
- import { ensureClusterStore } from "./risk-budget.mjs";
16
- import {
17
- addWaveToTopic,
18
- admitWaveInTopic,
19
- selectWaveInTopic,
20
- } from "../topic.mjs";
21
-
22
- function priorityFor(findings) {
23
- const rank = Math.min(...findings.map((finding) => SEVERITY_RANK[finding.severity] ?? 99));
24
- if (rank <= 1) {
25
- return "P0";
26
- }
27
- if (rank === 2) {
28
- return "P1";
29
- }
30
- return "P2";
31
- }
32
-
33
- function buildAdmissionChecklist(actionability) {
34
- return {
35
- authority_closed: false,
36
- semantic_closed: false,
37
- consumer_closed: false,
38
- drift_resistance_closed: false,
39
- manager_decision_required: actionability === "needs-decision",
40
- re_audit_required: true,
41
- };
42
- }
43
-
44
- function clusterForFinding(clustersByFindingId, findingId) {
45
- return clustersByFindingId.get(findingId) ?? null;
46
- }
47
-
48
- function remediationBundleForWave(waveId, waveFindings, clustersByFindingId) {
49
- const clusters = [];
50
- const seenClusters = new Set();
51
- for (const finding of waveFindings) {
52
- const cluster = clusterForFinding(clustersByFindingId, finding.id);
53
- if (cluster && !seenClusters.has(cluster.cluster_id)) {
54
- clusters.push(cluster);
55
- seenClusters.add(cluster.cluster_id);
56
- }
57
- }
58
- return {
59
- bundle_id: `bundle-${waveId.replace(/^remediation-wave-/, "")}`,
60
- cluster_ids: clusters.map((cluster) => cluster.cluster_id),
61
- representative_finding_ids: clusters.map((cluster) => cluster.representative_finding_id),
62
- canonical_finding_ids: [...new Set(waveFindings.map((finding) => finding.id))].sort(),
63
- duplicate_symptom_count: clusters.reduce((total, cluster) => total + (cluster.duplicate_symptom_count ?? 0), 0),
64
- source_chunks: [...new Set([
65
- ...waveFindings.map((finding) => finding.chunk_id),
66
- ...clusters.flatMap((cluster) => cluster.source_chunks ?? []),
67
- ])].sort(),
68
- authority_refs: [...new Set(clusters.map((cluster) => cluster.authority_ref).filter(Boolean))].sort(),
69
- evidence_roots: [...new Set(clusters.map((cluster) => cluster.evidence_root).filter(Boolean))].sort(),
70
- contract_seams: [...new Set(clusters.map((cluster) => cluster.contract_seam).filter(Boolean))].sort(),
71
- repair_targets: [...new Set(clusters.map((cluster) => cluster.repair_target).filter(Boolean))].sort(),
72
- };
73
- }
74
-
75
- function groupOpenFindings(findings, clusters, maxFindingsPerWave) {
76
- const groups = new Map();
77
- const clustersByFindingId = new Map();
78
- for (const cluster of clusters) {
79
- for (const findingId of cluster.canonical_finding_ids ?? []) {
80
- clustersByFindingId.set(findingId, cluster);
81
- }
82
- }
83
- const openFindings = findings
84
- .filter((finding) => finding.disposition === "open")
85
- .sort((left, right) => {
86
- const severityDiff = (SEVERITY_RANK[left.severity] ?? 99) - (SEVERITY_RANK[right.severity] ?? 99);
87
- if (severityDiff !== 0) {
88
- return severityDiff;
89
- }
90
- return left.id.localeCompare(right.id);
91
- });
92
-
93
- for (const finding of openFindings) {
94
- const fileParts = finding.location.file.split("/");
95
- const ownerDomain = finding.owner_domain ?? (fileParts.length > 1 ? fileParts[0] : "root");
96
- const key = `${ownerDomain}:${finding.actionability}`;
97
- const group = groups.get(key) ?? {
98
- ownerDomain,
99
- actionability: finding.actionability,
100
- findings: [],
101
- };
102
- group.findings.push(finding);
103
- groups.set(key, group);
104
- }
105
-
106
- const waves = [];
107
- for (const group of [...groups.values()].sort((left, right) => left.ownerDomain.localeCompare(right.ownerDomain))) {
108
- for (let index = 0; index < group.findings.length; index += maxFindingsPerWave) {
109
- const waveFindings = group.findings.slice(index, index + maxFindingsPerWave);
110
- const waveId = `remediation-wave-${String(waves.length + 1).padStart(3, "0")}`;
111
- const writeSet = [...new Set(waveFindings.map((finding) => finding.location.file))].sort();
112
- const remediationBundle = remediationBundleForWave(waveId, waveFindings, clustersByFindingId);
113
- waves.push({
114
- wave_id: waveId,
115
- status: "proposed",
116
- owner_domain: group.ownerDomain,
117
- priority: priorityFor(waveFindings),
118
- actionability: group.actionability,
119
- finding_ids: waveFindings.map((finding) => finding.id),
120
- cluster_ids: remediationBundle.cluster_ids,
121
- clustered_symptom_count: remediationBundle.duplicate_symptom_count,
122
- source_chunks: [...new Set(waveFindings.map((finding) => finding.chunk_id))].sort(),
123
- files: writeSet,
124
- write_set: writeSet,
125
- depends_on: [],
126
- remediation_bundle: remediationBundle,
127
- admission_checklist: buildAdmissionChecklist(group.actionability),
128
- });
129
- }
130
- }
131
-
132
- return waves;
133
- }
134
-
135
- export async function buildAuditSweepRemediationMap(projectRoot, options) {
136
- const sweepId = safeSweepId(options.sweepId);
137
- if (!sweepId) {
138
- return inputError("nimicoding sweep audit refused: --sweep-id is required.\n");
139
- }
140
- const timestampError = options.verifiedAt ? ensureIsoTimestamp(options.verifiedAt) : null;
141
- if (timestampError) {
142
- return timestampError;
143
- }
144
- const verifiedAt = options.verifiedAt ?? new Date().toISOString();
145
-
146
- const ledgerResult = await loadLatestLedger(projectRoot, sweepId);
147
- if (!ledgerResult.ok) {
148
- return inputError(ledgerResult.error);
149
- }
150
- const { findingsRef, store } = await loadFindings(projectRoot, sweepId);
151
- ensureClusterStore(store);
152
- const maxFindingsPerWave = Number.isInteger(options.maxFindingsPerWave) && options.maxFindingsPerWave > 0
153
- ? options.maxFindingsPerWave
154
- : 10;
155
- const waves = groupOpenFindings(store.findings, store.clusters, maxFindingsPerWave);
156
- const mappedFindingIds = new Set(waves.flatMap((wave) => wave.finding_ids));
157
- const mapRef = remediationMapRef(sweepId, ledgerResult.ledger.snapshot_id);
158
- const remediationMap = {
159
- version: 1,
160
- kind: "audit-remediation-map",
161
- sweep_id: sweepId,
162
- source_ledger_ref: ledgerResult.ledgerRef,
163
- source_findings_ref: findingsRef,
164
- grouping_policy: {
165
- owner_domain: "finding_owner_domain_or_first_two_path_segments",
166
- cluster_policy: "root_cause_authority_evidence_repair_target",
167
- split_by_actionability: true,
168
- split_by_write_set: true,
169
- max_findings_per_wave: maxFindingsPerWave,
170
- duplicate_symptoms_count_as_remediation_obligations: false,
171
- preserve_source_ledger: true,
172
- },
173
- remediation_bundles: waves.map((wave) => wave.remediation_bundle),
174
- waves,
175
- unmapped_findings: store.findings
176
- .filter((finding) => finding.disposition === "open" && !mappedFindingIds.has(finding.id))
177
- .map((finding) => finding.id),
178
- status: waves.length > 0 ? "proposed" : "empty",
179
- created_at: verifiedAt,
180
- updated_at: verifiedAt,
181
- };
182
-
183
- await writeYamlRef(projectRoot, mapRef, remediationMap);
184
- const runRef = await appendRunEvent(projectRoot, sweepId, {
185
- event_type: "remediation_map_created",
186
- remediation_map_ref: mapRef,
187
- source_ledger_ref: ledgerResult.ledgerRef,
188
- wave_count: waves.length,
189
- });
190
-
191
- return {
192
- ok: true,
193
- exitCode: 0,
194
- sweepId,
195
- ledgerRef: ledgerResult.ledgerRef,
196
- findingsRef,
197
- remediationMapRef: mapRef,
198
- runLedgerRef: runRef,
199
- waveCount: waves.length,
200
- mappedFindingCount: mappedFindingIds.size,
201
- remediationBundleCount: remediationMap.remediation_bundles.length,
202
- clusteredSymptomCount: remediationMap.remediation_bundles.reduce((total, bundle) => total + bundle.duplicate_symptom_count, 0),
203
- unmappedFindingCount: remediationMap.unmapped_findings.length,
204
- waves,
205
- };
206
- }
207
-
208
- function topicWaveIdForRemediationWave(wave) {
209
- const suffix = String(wave.wave_id ?? "")
210
- .replace(/^remediation-wave-/, "")
211
- .replace(/[^a-z0-9]+/g, "-")
212
- .replace(/^-+|-+$/g, "") || "001";
213
- return `wave-audit-remediation-${suffix}`;
214
- }
215
-
216
- function topicWaveFromRemediationWave(wave, ledgerRef, remediationMapRefValue) {
217
- const waveId = topicWaveIdForRemediationWave(wave);
218
- return {
219
- wave_id: waveId,
220
- slug: waveId.replace(/^wave-/, ""),
221
- state: "candidate",
222
- primary_closure_goal: `Resolve audit findings: ${wave.finding_ids.join(", ")}`,
223
- deps: Array.isArray(wave.depends_on) ? wave.depends_on.map((dep) => topicWaveIdForRemediationWave({ wave_id: dep })) : [],
224
- owner_domain: wave.owner_domain,
225
- parallelizable_after: [],
226
- selected: false,
227
- source_audit_sweep: {
228
- source_remediation_wave_id: wave.wave_id,
229
- source_ledger_ref: ledgerRef,
230
- remediation_map_ref: remediationMapRefValue,
231
- finding_ids: wave.finding_ids,
232
- cluster_ids: wave.cluster_ids ?? [],
233
- source_chunks: wave.source_chunks,
234
- write_set: wave.write_set,
235
- remediation_bundle: wave.remediation_bundle ?? null,
236
- actionability: wave.actionability,
237
- priority: wave.priority,
238
- admission_checklist: wave.admission_checklist,
239
- },
240
- };
241
- }
242
-
243
- export async function admitAuditSweepRemediationMap(projectRoot, options) {
244
- const sweepId = safeSweepId(options.sweepId);
245
- if (!sweepId || typeof options.topicId !== "string" || !options.topicId.trim()) {
246
- return inputError("nimicoding sweep audit refused: --sweep-id and --topic-id are required.\n");
247
- }
248
-
249
- const ledgerResult = await loadLatestLedger(projectRoot, sweepId);
250
- if (!ledgerResult.ok) {
251
- return inputError(ledgerResult.error);
252
- }
253
- const mapRef = remediationMapRef(sweepId, ledgerResult.ledger.snapshot_id);
254
- const remediationMap = await loadYamlRef(projectRoot, mapRef);
255
- if (!remediationMap || remediationMap.kind !== "audit-remediation-map" || remediationMap.source_ledger_ref !== ledgerResult.ledgerRef || !Array.isArray(remediationMap.waves)) {
256
- return inputError("nimicoding sweep audit refused: latest remediation map is missing or malformed.\n");
257
- }
258
- const planResult = await loadPlan(projectRoot, sweepId);
259
- if (!planResult.ok) {
260
- return inputError(planResult.error);
261
- }
262
- const { findingsRef, store } = await loadFindings(projectRoot, sweepId);
263
- ensureClusterStore(store);
264
-
265
- const materialized = [];
266
- const admitted = [];
267
- const managerDecisionRequired = [];
268
- for (const remediationWave of remediationMap.waves) {
269
- const topicWave = topicWaveFromRemediationWave(remediationWave, ledgerResult.ledgerRef, mapRef);
270
- const addResult = await addWaveToTopic(projectRoot, options.topicId, topicWave);
271
- if (!addResult.ok) {
272
- return {
273
- ok: false,
274
- inputError: true,
275
- exitCode: 1,
276
- error: `nimicoding sweep audit refused: remediation wave admission failed: ${addResult.error}\n`,
277
- };
278
- }
279
- materialized.push(topicWave.wave_id);
280
-
281
- if (remediationWave.admission_checklist?.manager_decision_required === true || remediationWave.actionability === "needs-decision") {
282
- managerDecisionRequired.push(topicWave.wave_id);
283
- continue;
284
- }
285
-
286
- const selectResult = await selectWaveInTopic(projectRoot, options.topicId, topicWave.wave_id);
287
- if (!selectResult.ok) {
288
- return {
289
- ok: false,
290
- inputError: true,
291
- exitCode: 1,
292
- error: `nimicoding sweep audit refused: remediation wave selection failed: ${selectResult.error}\n`,
293
- };
294
- }
295
- const admitResult = await admitWaveInTopic(projectRoot, options.topicId, topicWave.wave_id);
296
- if (!admitResult.ok) {
297
- return {
298
- ok: false,
299
- inputError: true,
300
- exitCode: 1,
301
- error: `nimicoding sweep audit refused: remediation wave admission failed: ${admitResult.error}\n`,
302
- };
303
- }
304
- admitted.push(topicWave.wave_id);
305
- }
306
-
307
- const acceptedClusterIds = new Set(remediationMap.waves.flatMap((wave) => wave.cluster_ids ?? []));
308
- const acceptedAt = nowIso();
309
- for (const cluster of store.clusters) {
310
- if (!acceptedClusterIds.has(cluster.cluster_id)) {
311
- continue;
312
- }
313
- cluster.acceptance = {
314
- topic_id: options.topicId,
315
- remediation_map_ref: mapRef,
316
- source_ledger_ref: ledgerResult.ledgerRef,
317
- source_inventory_hash: planResult.plan.inventory_hash,
318
- source_evidence_inventory_hash: planResult.plan.evidence_inventory_hash ?? null,
319
- accepted_at: acceptedAt,
320
- };
321
- cluster.updated_at = acceptedAt;
322
- }
323
- store.updated_at = acceptedAt;
324
- await writeYamlRef(projectRoot, findingsRef, store);
325
-
326
- const runRef = await appendRunEvent(projectRoot, sweepId, {
327
- event_type: "remediation_map_admitted",
328
- remediation_map_ref: mapRef,
329
- source_ledger_ref: ledgerResult.ledgerRef,
330
- topic_id: options.topicId,
331
- materialized_wave_ids: materialized,
332
- admitted_wave_ids: admitted,
333
- manager_decision_required_wave_ids: managerDecisionRequired,
334
- accepted_cluster_ids: [...acceptedClusterIds].sort(),
335
- });
336
-
337
- return {
338
- ok: true,
339
- exitCode: 0,
340
- sweepId,
341
- topicId: options.topicId,
342
- ledgerRef: ledgerResult.ledgerRef,
343
- remediationMapRef: mapRef,
344
- runLedgerRef: runRef,
345
- materializedWaveIds: materialized,
346
- admittedWaveIds: admitted,
347
- managerDecisionRequiredWaveIds: managerDecisionRequired,
348
- };
349
- }
@@ -1,129 +0,0 @@
1
- import { copyFile, mkdir } from "node:fs/promises";
2
- import path from "node:path";
3
-
4
- import {
5
- FINDING_DISPOSITION,
6
- RERUN_VERDICT,
7
- appendRunEvent,
8
- artifactPath,
9
- artifactRef,
10
- ensureIsoTimestamp,
11
- findingsRef,
12
- inputError,
13
- loadFindings,
14
- loadJsonFile,
15
- resolveInsideProject,
16
- safeSweepId,
17
- writeYamlRef,
18
- } from "./common.mjs";
19
- import { isPlainObject } from "../value-helpers.mjs";
20
- import { pathExists } from "../fs-helpers.mjs";
21
-
22
- function validateRerunEvidence(evidence, finding, disposition) {
23
- if (!isPlainObject(evidence)) {
24
- return { ok: false, error: "rerun evidence must be a JSON object" };
25
- }
26
- if (evidence.finding_id !== finding.id) {
27
- return { ok: false, error: "rerun evidence finding_id must match --finding-id" };
28
- }
29
- if (evidence.source_fingerprint !== finding.fingerprint) {
30
- return { ok: false, error: "rerun evidence source_fingerprint must match original finding" };
31
- }
32
- if (evidence.disposition !== disposition) {
33
- return { ok: false, error: "rerun evidence disposition must match --disposition" };
34
- }
35
- if (!isPlainObject(evidence.rerun) || !Array.isArray(evidence.rerun.covered_files)) {
36
- return { ok: false, error: "rerun.covered_files is required" };
37
- }
38
- if (!evidence.rerun.covered_files.includes(finding.location.file)) {
39
- return { ok: false, error: "rerun.covered_files must include original finding file" };
40
- }
41
- if (!RERUN_VERDICT.has(evidence.rerun.verdict)) {
42
- return { ok: false, error: "rerun.verdict is invalid" };
43
- }
44
- if (disposition === "remediated" && evidence.rerun.verdict !== "not_reproduced") {
45
- return { ok: false, error: "remediated disposition requires not_reproduced rerun verdict" };
46
- }
47
- if (["accepted-risk", "false-positive"].includes(disposition) && !isPlainObject(evidence.manager_acceptance)) {
48
- return { ok: false, error: `${disposition} disposition requires manager_acceptance evidence` };
49
- }
50
- if (disposition === "deferred-backlog" && typeof evidence.backlog_ref !== "string") {
51
- return { ok: false, error: "deferred-backlog disposition requires backlog_ref" };
52
- }
53
- if (typeof evidence.evidence_summary !== "string" || !evidence.evidence_summary.trim()) {
54
- return { ok: false, error: "evidence_summary is required" };
55
- }
56
- return { ok: true };
57
- }
58
-
59
- export async function resolveAuditSweepFinding(projectRoot, options) {
60
- const sweepId = safeSweepId(options.sweepId);
61
- if (!sweepId || typeof options.findingId !== "string") {
62
- return inputError("nimicoding sweep audit refused: --sweep-id and --finding-id are required.\n");
63
- }
64
- if (!FINDING_DISPOSITION.has(options.disposition) || options.disposition === "open") {
65
- return inputError("nimicoding sweep audit refused: --disposition must be one of remediated, accepted-risk, false-positive, deferred-backlog.\n");
66
- }
67
- const timestampError = ensureIsoTimestamp(options.verifiedAt);
68
- if (timestampError) {
69
- return timestampError;
70
- }
71
- const source = resolveInsideProject(projectRoot, options.fromPath ?? "", "--from");
72
- if (!source.ok) {
73
- return inputError(source.error);
74
- }
75
- const sourceInfo = await pathExists(source.absolutePath);
76
- if (!sourceInfo || !sourceInfo.isFile()) {
77
- return inputError("nimicoding sweep audit refused: --from must point to an existing JSON evidence file.\n");
78
- }
79
-
80
- const { findingsRef: aggregateFindingsRef, store } = await loadFindings(projectRoot, sweepId);
81
- const findingIndex = store.findings.findIndex((finding) => finding.id === options.findingId);
82
- if (findingIndex === -1) {
83
- return inputError(`nimicoding sweep audit refused: finding not found for ${options.findingId}.\n`);
84
- }
85
- const finding = store.findings[findingIndex];
86
- const evidenceJson = await loadJsonFile(source.absolutePath);
87
- if (!evidenceJson.ok) {
88
- return inputError("nimicoding sweep audit refused: --from must contain valid JSON.\n");
89
- }
90
- const validation = validateRerunEvidence(evidenceJson.value, finding, options.disposition);
91
- if (!validation.ok) {
92
- return inputError(`nimicoding sweep audit refused: ${validation.error}.\n`);
93
- }
94
-
95
- const evidenceRef = artifactRef("evidence_refs", sweepId, `resolution-${options.findingId}.json`);
96
- await mkdir(path.dirname(artifactPath(projectRoot, evidenceRef)), { recursive: true });
97
- await copyFile(source.absolutePath, artifactPath(projectRoot, evidenceRef));
98
-
99
- store.findings[findingIndex] = {
100
- ...finding,
101
- disposition: options.disposition,
102
- resolution: {
103
- disposition: options.disposition,
104
- evidence_ref: evidenceRef,
105
- rerun: evidenceJson.value.rerun,
106
- resolved_at: options.verifiedAt,
107
- },
108
- };
109
- store.updated_at = options.verifiedAt;
110
- await writeYamlRef(projectRoot, aggregateFindingsRef, store);
111
- const runRef = await appendRunEvent(projectRoot, sweepId, {
112
- event_type: "finding_resolved",
113
- finding_id: options.findingId,
114
- disposition: options.disposition,
115
- evidence_ref: evidenceRef,
116
- findings_ref: findingsRef(sweepId),
117
- });
118
-
119
- return {
120
- ok: true,
121
- exitCode: 0,
122
- sweepId,
123
- findingId: options.findingId,
124
- disposition: options.disposition,
125
- findingsRef: aggregateFindingsRef,
126
- evidenceRef,
127
- runLedgerRef: runRef,
128
- };
129
- }