@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.
- package/LICENSE +21 -0
- package/README.md +348 -0
- package/adapters/README.md +25 -0
- package/adapters/claude/README.md +89 -0
- package/adapters/claude/profile.yaml +70 -0
- package/adapters/codex/README.md +53 -0
- package/adapters/codex/profile.yaml +78 -0
- package/adapters/oh-my-codex/README.md +185 -0
- package/adapters/oh-my-codex/profile.yaml +46 -0
- package/bin/nimicoding.mjs +6 -0
- package/cli/commands/admit-high-risk-decision.mjs +108 -0
- package/cli/commands/audit-sweep.mjs +341 -0
- package/cli/commands/blueprint-audit.mjs +91 -0
- package/cli/commands/clear.mjs +168 -0
- package/cli/commands/closeout.mjs +183 -0
- package/cli/commands/decide-high-risk-execution.mjs +124 -0
- package/cli/commands/doctor.mjs +53 -0
- package/cli/commands/generate-spec-derived-docs.mjs +131 -0
- package/cli/commands/handoff.mjs +123 -0
- package/cli/commands/ingest-high-risk-execution.mjs +95 -0
- package/cli/commands/review-high-risk-execution.mjs +95 -0
- package/cli/commands/start.mjs +717 -0
- package/cli/commands/topic-formatters.mjs +382 -0
- package/cli/commands/topic-goal.mjs +33 -0
- package/cli/commands/topic-options-shared.mjs +27 -0
- package/cli/commands/topic-options-workflow.mjs +767 -0
- package/cli/commands/topic-options.mjs +626 -0
- package/cli/commands/topic-runner.mjs +169 -0
- package/cli/commands/topic.mjs +795 -0
- package/cli/commands/validate-acceptance.mjs +5 -0
- package/cli/commands/validate-ai-governance.mjs +214 -0
- package/cli/commands/validate-execution-packet.mjs +5 -0
- package/cli/commands/validate-orchestration-state.mjs +5 -0
- package/cli/commands/validate-prompt.mjs +5 -0
- package/cli/commands/validate-spec-audit.mjs +27 -0
- package/cli/commands/validate-spec-governance.mjs +124 -0
- package/cli/commands/validate-spec-tree.mjs +27 -0
- package/cli/commands/validate-worker-output.mjs +5 -0
- package/cli/constants.mjs +489 -0
- package/cli/help.mjs +134 -0
- package/cli/index.mjs +103 -0
- package/cli/lib/adapter-profiles.mjs +403 -0
- package/cli/lib/audit-execution.mjs +52 -0
- package/cli/lib/audit-sweep-runtime/admissions.mjs +381 -0
- package/cli/lib/audit-sweep-runtime/audit-validity.mjs +333 -0
- package/cli/lib/audit-sweep-runtime/chunks.mjs +697 -0
- package/cli/lib/audit-sweep-runtime/closeout.mjs +144 -0
- package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +639 -0
- package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +515 -0
- package/cli/lib/audit-sweep-runtime/common.mjs +329 -0
- package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +172 -0
- package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +152 -0
- package/cli/lib/audit-sweep-runtime/format.mjs +57 -0
- package/cli/lib/audit-sweep-runtime/ingest.mjs +486 -0
- package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +198 -0
- package/cli/lib/audit-sweep-runtime/inventory.mjs +728 -0
- package/cli/lib/audit-sweep-runtime/ledger.mjs +315 -0
- package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +101 -0
- package/cli/lib/audit-sweep-runtime/remediation.mjs +349 -0
- package/cli/lib/audit-sweep-runtime/rerun.mjs +129 -0
- package/cli/lib/audit-sweep-runtime/risk-budget.mjs +300 -0
- package/cli/lib/audit-sweep-runtime/status.mjs +62 -0
- package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +215 -0
- package/cli/lib/audit-sweep-runtime/validators.mjs +758 -0
- package/cli/lib/audit-sweep.mjs +18 -0
- package/cli/lib/authority-convergence.mjs +309 -0
- package/cli/lib/blueprint-audit.mjs +370 -0
- package/cli/lib/bootstrap.mjs +228 -0
- package/cli/lib/closeout.mjs +623 -0
- package/cli/lib/codex-sdk-runner.mjs +76 -0
- package/cli/lib/contracts.mjs +180 -0
- package/cli/lib/doctor.mjs +18 -0
- package/cli/lib/entrypoints.mjs +274 -0
- package/cli/lib/external-execution.mjs +101 -0
- package/cli/lib/fs-helpers.mjs +33 -0
- package/cli/lib/handoff.mjs +785 -0
- package/cli/lib/high-risk-admission.mjs +442 -0
- package/cli/lib/high-risk-decision.mjs +324 -0
- package/cli/lib/high-risk-ingest.mjs +317 -0
- package/cli/lib/high-risk-review.mjs +263 -0
- package/cli/lib/internal/contracts-loaders.mjs +132 -0
- package/cli/lib/internal/contracts-parse-high-risk.mjs +131 -0
- package/cli/lib/internal/contracts-parse.mjs +457 -0
- package/cli/lib/internal/contracts-validators.mjs +398 -0
- package/cli/lib/internal/doctor-bootstrap-surface.mjs +359 -0
- package/cli/lib/internal/doctor-delegated-surface.mjs +256 -0
- package/cli/lib/internal/doctor-finalize.mjs +385 -0
- package/cli/lib/internal/doctor-format.mjs +286 -0
- package/cli/lib/internal/doctor-inspectors.mjs +294 -0
- package/cli/lib/internal/doctor-state.mjs +205 -0
- package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +315 -0
- package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +358 -0
- package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +155 -0
- package/cli/lib/internal/governance/ai/check-high-risk-doc-metadata-core.mjs +173 -0
- package/cli/lib/internal/governance/config.mjs +150 -0
- package/cli/lib/internal/governance/runner.mjs +35 -0
- package/cli/lib/internal/governance/shared/read-yaml-with-fragments.mjs +49 -0
- package/cli/lib/internal/validators-artifacts.mjs +515 -0
- package/cli/lib/internal/validators-shared.mjs +28 -0
- package/cli/lib/internal/validators-spec-helpers.mjs +186 -0
- package/cli/lib/internal/validators-spec.mjs +410 -0
- package/cli/lib/shared.mjs +83 -0
- package/cli/lib/topic-draft-packets.mjs +48 -0
- package/cli/lib/topic-goal.mjs +361 -0
- package/cli/lib/topic-runner.mjs +772 -0
- package/cli/lib/topic.mjs +93 -0
- package/cli/lib/ui.mjs +178 -0
- package/cli/lib/validators.mjs +78 -0
- package/cli/lib/value-helpers.mjs +24 -0
- package/cli/lib/yaml-helpers.mjs +133 -0
- package/cli/nimicoding.mjs +1 -0
- package/cli/seeds/bootstrap.mjs +47 -0
- package/config/audit-execution-artifacts.yaml +20 -0
- package/config/bootstrap.yaml +6 -0
- package/config/external-execution-artifacts.yaml +16 -0
- package/config/host-adapter.yaml +30 -0
- package/config/host-profile.yaml +29 -0
- package/config/installer-evidence.yaml +31 -0
- package/config/skill-installer.yaml +23 -0
- package/config/skill-manifest.yaml +46 -0
- package/config/skills.yaml +30 -0
- package/config/spec-generation-inputs.yaml +25 -0
- package/contracts/acceptance.schema.yaml +16 -0
- package/contracts/admission-checklist.schema.yaml +15 -0
- package/contracts/audit-chunk.schema.yaml +110 -0
- package/contracts/audit-closeout.schema.yaml +51 -0
- package/contracts/audit-finding.schema.yaml +61 -0
- package/contracts/audit-ledger.schema.yaml +138 -0
- package/contracts/audit-plan.schema.yaml +123 -0
- package/contracts/audit-remediation-map.schema.yaml +51 -0
- package/contracts/audit-rerun.schema.yaml +31 -0
- package/contracts/audit-sweep-result.yaml +49 -0
- package/contracts/authority-convergence-audit.schema.yaml +19 -0
- package/contracts/closeout.schema.yaml +25 -0
- package/contracts/decision-review.schema.yaml +16 -0
- package/contracts/doc-spec-audit-result.yaml +19 -0
- package/contracts/execution-packet.schema.yaml +49 -0
- package/contracts/external-host-compatibility.yaml +22 -0
- package/contracts/forbidden-shortcuts.catalog.yaml +23 -0
- package/contracts/high-risk-admission.schema.yaml +23 -0
- package/contracts/high-risk-execution-result.yaml +20 -0
- package/contracts/orchestration-state.schema.yaml +41 -0
- package/contracts/overflow-continuation.schema.yaml +12 -0
- package/contracts/packet.schema.yaml +30 -0
- package/contracts/pending-note.schema.yaml +17 -0
- package/contracts/prompt.schema.yaml +12 -0
- package/contracts/remediation.schema.yaml +16 -0
- package/contracts/result.schema.yaml +24 -0
- package/contracts/spec-generation-audit.schema.yaml +31 -0
- package/contracts/spec-generation-inputs.schema.yaml +39 -0
- package/contracts/spec-reconstruction-result.yaml +37 -0
- package/contracts/topic-goal.schema.yaml +78 -0
- package/contracts/topic-run-ledger.schema.yaml +72 -0
- package/contracts/topic-step-decision.schema.yaml +45 -0
- package/contracts/topic.schema.yaml +65 -0
- package/contracts/true-close.schema.yaml +15 -0
- package/contracts/wave.schema.yaml +29 -0
- package/contracts/worker-output.schema.yaml +15 -0
- package/methodology/audit-sweep-p0p1-recall.yaml +45 -0
- package/methodology/authority-convergence-policy.yaml +42 -0
- package/methodology/core.yaml +25 -0
- package/methodology/four-closure-policy.yaml +28 -0
- package/methodology/overflow-continuation-policy.yaml +14 -0
- package/methodology/role-separation-policy.yaml +28 -0
- package/methodology/skill-exchange-projection.yaml +114 -0
- package/methodology/skill-handoff.yaml +34 -0
- package/methodology/skill-installer-result.yaml +27 -0
- package/methodology/skill-installer-summary-projection.yaml +181 -0
- package/methodology/skill-runtime.yaml +23 -0
- package/methodology/spec-reconstruction.yaml +63 -0
- package/methodology/spec-target-truth-profile.yaml +53 -0
- package/methodology/topic-lifecycle-report.yaml +144 -0
- package/methodology/topic-lifecycle.yaml +37 -0
- package/methodology/topic-naming-ontology.yaml +21 -0
- package/methodology/topic-ontology.yaml +38 -0
- package/methodology/topic-validation-policy.yaml +9 -0
- package/methodology/wave-dag-policy.yaml +14 -0
- package/package.json +50 -0
- package/spec/_meta/command-gating-matrix.yaml +110 -0
- package/spec/_meta/generate-drift-migration-checklist.yaml +155 -0
- package/spec/_meta/governance-routing-cutover-checklist.yaml +35 -0
- package/spec/_meta/phase2-impacted-surface-matrix.yaml +44 -0
- package/spec/_meta/spec-authority-cutover-readiness.yaml +104 -0
- package/spec/_meta/spec-tree-model.yaml +72 -0
- package/spec/bootstrap-state.yaml +99 -0
- package/spec/product-scope.yaml +56 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { SEVERITY_RANK, sha256Object } from "./common.mjs";
|
|
4
|
+
import { isPlainObject } from "../value-helpers.mjs";
|
|
5
|
+
|
|
6
|
+
const RISK_BUDGET_LIMIT_FIELDS = [
|
|
7
|
+
"maxSweepFindings",
|
|
8
|
+
"maxDomainFindings",
|
|
9
|
+
"maxSweepHighRiskFindings",
|
|
10
|
+
"maxDomainHighRiskFindings",
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
function positiveIntegerOrNull(value) {
|
|
14
|
+
return Number.isInteger(value) && value > 0 ? value : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function nonEmptyString(value) {
|
|
18
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function buildRiskBudgetPolicy(options = {}) {
|
|
22
|
+
const hasLimit = RISK_BUDGET_LIMIT_FIELDS.some((field) => positiveIntegerOrNull(options[field]) !== null);
|
|
23
|
+
if (!hasLimit) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
mode: "root_cause_aware",
|
|
29
|
+
duplicate_symptoms_count_as_remediation_obligations: false,
|
|
30
|
+
high_risk_unique_root_causes_are_canonical: true,
|
|
31
|
+
accepted_cluster_resume_skip: true,
|
|
32
|
+
max_sweep_findings: positiveIntegerOrNull(options.maxSweepFindings),
|
|
33
|
+
max_domain_findings: positiveIntegerOrNull(options.maxDomainFindings),
|
|
34
|
+
max_sweep_high_risk_findings: positiveIntegerOrNull(options.maxSweepHighRiskFindings),
|
|
35
|
+
max_domain_high_risk_findings: positiveIntegerOrNull(options.maxDomainHighRiskFindings),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function highRiskFinding(finding) {
|
|
40
|
+
return finding?.severity === "critical" || finding?.severity === "high";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function evidenceRootForFile(chunk, fileRef) {
|
|
44
|
+
for (const rootRef of chunk.evidence_roots ?? []) {
|
|
45
|
+
const normalized = String(rootRef).replace(/\\/g, "/").replace(/\/$/, "");
|
|
46
|
+
if (fileRef === normalized || fileRef.startsWith(`${normalized}/`)) {
|
|
47
|
+
return normalized;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return path.posix.dirname(fileRef) || ".";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function normalizeRootCauseField(rawFinding, name) {
|
|
54
|
+
const rootCause = isPlainObject(rawFinding.root_cause) ? rawFinding.root_cause : {};
|
|
55
|
+
const value = rootCause[name] ?? rawFinding[name];
|
|
56
|
+
return nonEmptyString(value) ? String(value).trim().replace(/\\/g, "/") : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function normalizeEvidenceRootForChunk(chunk, evidenceRoot) {
|
|
60
|
+
if (!evidenceRoot) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
if (evidenceRoot === "packet:evidence_inventory") {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
for (const rootRef of chunk.evidence_roots ?? []) {
|
|
67
|
+
const normalized = String(rootRef).replace(/\\/g, "/").replace(/\/$/, "");
|
|
68
|
+
if (evidenceRoot === normalized || evidenceRoot.startsWith(`${normalized}/`)) {
|
|
69
|
+
return normalized;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function deriveFindingCluster(rawFinding, finding, chunk, plan) {
|
|
76
|
+
const authorityRef = normalizeRootCauseField(rawFinding, "authority_ref")
|
|
77
|
+
?? chunk.authority_refs?.[0]
|
|
78
|
+
?? chunk.files?.[0]
|
|
79
|
+
?? finding.location.file;
|
|
80
|
+
const allowedAuthorityRefs = new Set([...(chunk.authority_refs ?? []), ...(chunk.files ?? [])]);
|
|
81
|
+
if (chunk.planning_basis === "spec_authority" && !allowedAuthorityRefs.has(authorityRef)) {
|
|
82
|
+
return { ok: false, error: "root_cause.authority_ref must belong to chunk authority refs" };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const explicitEvidenceRoot = normalizeRootCauseField(rawFinding, "evidence_root");
|
|
86
|
+
const normalizedExplicitEvidenceRoot = normalizeEvidenceRootForChunk(chunk, explicitEvidenceRoot);
|
|
87
|
+
const packetInventoryRootSentinel = explicitEvidenceRoot === "packet:evidence_inventory";
|
|
88
|
+
if (explicitEvidenceRoot && !packetInventoryRootSentinel && chunk.planning_basis === "spec_authority" && !normalizedExplicitEvidenceRoot) {
|
|
89
|
+
return { ok: false, error: "root_cause.evidence_root must belong to chunk evidence roots or a descendant of one" };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const rootCauseKey = normalizeRootCauseField(rawFinding, "key") ?? normalizeRootCauseField(rawFinding, "id");
|
|
93
|
+
const evidenceRoot = normalizedExplicitEvidenceRoot ?? evidenceRootForFile(chunk, finding.location.file);
|
|
94
|
+
const contractSeam = normalizeRootCauseField(rawFinding, "contract_seam") ?? finding.category;
|
|
95
|
+
const repairTarget = normalizeRootCauseField(rawFinding, "repair_target") ?? finding.location.file;
|
|
96
|
+
const fallbackUniqueKey = `${finding.title}:${finding.description}:${finding.location.file}`;
|
|
97
|
+
const seed = {
|
|
98
|
+
sweep_id: finding.sweep_id,
|
|
99
|
+
authority_context: {
|
|
100
|
+
inventory_hash: plan.inventory_hash,
|
|
101
|
+
evidence_inventory_hash: plan.evidence_inventory_hash ?? null,
|
|
102
|
+
},
|
|
103
|
+
owner_domain: finding.owner_domain,
|
|
104
|
+
category: finding.category,
|
|
105
|
+
actionability: finding.actionability,
|
|
106
|
+
authority_ref: authorityRef,
|
|
107
|
+
evidence_root: evidenceRoot,
|
|
108
|
+
contract_seam: contractSeam,
|
|
109
|
+
repair_target: repairTarget,
|
|
110
|
+
root_cause_key: rootCauseKey ?? fallbackUniqueKey,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
ok: true,
|
|
115
|
+
cluster: {
|
|
116
|
+
cluster_id: `cluster-${sha256Object(seed).slice(0, 16)}`,
|
|
117
|
+
cluster_key: sha256Object(seed),
|
|
118
|
+
root_cause_key: rootCauseKey,
|
|
119
|
+
authority_ref: authorityRef,
|
|
120
|
+
evidence_root: evidenceRoot,
|
|
121
|
+
contract_seam: contractSeam,
|
|
122
|
+
repair_target: repairTarget,
|
|
123
|
+
authority_context: seed.authority_context,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function ensureClusterStore(store) {
|
|
129
|
+
if (!Array.isArray(store.clusters)) {
|
|
130
|
+
store.clusters = [];
|
|
131
|
+
}
|
|
132
|
+
if (!Number.isInteger(store.clustered_symptom_count)) {
|
|
133
|
+
store.clustered_symptom_count = 0;
|
|
134
|
+
}
|
|
135
|
+
if (!Number.isInteger(store.accepted_cluster_skip_count)) {
|
|
136
|
+
store.accepted_cluster_skip_count = 0;
|
|
137
|
+
}
|
|
138
|
+
store.remediation_obligation_count = store.findings.length;
|
|
139
|
+
return store;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function severityRank(value) {
|
|
143
|
+
return SEVERITY_RANK[value] ?? 99;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function findingRequiresCanonicalInCluster(finding, cluster) {
|
|
147
|
+
const currentRank = severityRank(cluster.max_severity);
|
|
148
|
+
const incomingRank = severityRank(finding.severity);
|
|
149
|
+
return incomingRank < currentRank;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function clusterAcceptanceMatchesPlan(cluster, plan) {
|
|
153
|
+
const acceptance = cluster.acceptance;
|
|
154
|
+
if (!isPlainObject(acceptance)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return acceptance.source_inventory_hash === plan.inventory_hash
|
|
158
|
+
&& (acceptance.source_evidence_inventory_hash ?? null) === (plan.evidence_inventory_hash ?? null);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function buildDuplicateSymptom(finding, fingerprint, classification) {
|
|
162
|
+
return {
|
|
163
|
+
fingerprint,
|
|
164
|
+
classification,
|
|
165
|
+
chunk_id: finding.chunk_id,
|
|
166
|
+
evidence_ref: finding.evidence_ref,
|
|
167
|
+
severity: finding.severity,
|
|
168
|
+
title: finding.title,
|
|
169
|
+
location: finding.location,
|
|
170
|
+
detected_at: finding.detected_at,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function updateClusterWithCanonical(cluster, finding) {
|
|
175
|
+
if (!cluster.canonical_finding_ids.includes(finding.id)) {
|
|
176
|
+
cluster.canonical_finding_ids.push(finding.id);
|
|
177
|
+
cluster.canonical_finding_ids.sort();
|
|
178
|
+
}
|
|
179
|
+
if (severityRank(finding.severity) < severityRank(cluster.max_severity)) {
|
|
180
|
+
cluster.max_severity = finding.severity;
|
|
181
|
+
cluster.representative_finding_id = finding.id;
|
|
182
|
+
}
|
|
183
|
+
cluster.source_chunks = [...new Set([...cluster.source_chunks, finding.chunk_id])].sort();
|
|
184
|
+
cluster.files = [...new Set([...cluster.files, finding.location.file])].sort();
|
|
185
|
+
cluster.updated_at = finding.detected_at;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function createCluster(clusterSeed, finding) {
|
|
189
|
+
return {
|
|
190
|
+
...clusterSeed,
|
|
191
|
+
representative_finding_id: finding.id,
|
|
192
|
+
canonical_finding_ids: [finding.id],
|
|
193
|
+
owner_domain: finding.owner_domain,
|
|
194
|
+
category: finding.category,
|
|
195
|
+
actionability: finding.actionability,
|
|
196
|
+
max_severity: finding.severity,
|
|
197
|
+
source_chunks: [finding.chunk_id],
|
|
198
|
+
files: [finding.location.file],
|
|
199
|
+
duplicate_symptom_count: 0,
|
|
200
|
+
duplicate_symptoms: [],
|
|
201
|
+
created_at: finding.detected_at,
|
|
202
|
+
updated_at: finding.detected_at,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function buildRiskBudgetStatus(plan, store, verifiedAt) {
|
|
207
|
+
const policy = plan.risk_budget_policy;
|
|
208
|
+
if (!isPlainObject(policy)) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const findings = Array.isArray(store.findings) ? store.findings : [];
|
|
213
|
+
const clusters = Array.isArray(store.clusters) ? store.clusters : [];
|
|
214
|
+
const domains = new Map();
|
|
215
|
+
for (const finding of findings) {
|
|
216
|
+
const ownerDomain = finding.owner_domain ?? "root";
|
|
217
|
+
const domain = domains.get(ownerDomain) ?? {
|
|
218
|
+
owner_domain: ownerDomain,
|
|
219
|
+
finding_count: 0,
|
|
220
|
+
high_risk_finding_count: 0,
|
|
221
|
+
cluster_count: 0,
|
|
222
|
+
clustered_symptom_count: 0,
|
|
223
|
+
state: "within_budget",
|
|
224
|
+
reasons: [],
|
|
225
|
+
};
|
|
226
|
+
domain.finding_count += 1;
|
|
227
|
+
if (highRiskFinding(finding)) {
|
|
228
|
+
domain.high_risk_finding_count += 1;
|
|
229
|
+
}
|
|
230
|
+
domains.set(ownerDomain, domain);
|
|
231
|
+
}
|
|
232
|
+
for (const cluster of clusters) {
|
|
233
|
+
const ownerDomain = cluster.owner_domain ?? "root";
|
|
234
|
+
const domain = domains.get(ownerDomain) ?? {
|
|
235
|
+
owner_domain: ownerDomain,
|
|
236
|
+
finding_count: 0,
|
|
237
|
+
high_risk_finding_count: 0,
|
|
238
|
+
cluster_count: 0,
|
|
239
|
+
clustered_symptom_count: 0,
|
|
240
|
+
state: "within_budget",
|
|
241
|
+
reasons: [],
|
|
242
|
+
};
|
|
243
|
+
domain.cluster_count += 1;
|
|
244
|
+
domain.clustered_symptom_count += cluster.duplicate_symptom_count ?? 0;
|
|
245
|
+
domains.set(ownerDomain, domain);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const sweep = {
|
|
249
|
+
finding_count: findings.length,
|
|
250
|
+
high_risk_finding_count: findings.filter(highRiskFinding).length,
|
|
251
|
+
cluster_count: clusters.length,
|
|
252
|
+
clustered_symptom_count: store.clustered_symptom_count ?? 0,
|
|
253
|
+
state: "within_budget",
|
|
254
|
+
reasons: [],
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
if (policy.max_sweep_findings && sweep.finding_count >= policy.max_sweep_findings) {
|
|
258
|
+
sweep.state = "paused";
|
|
259
|
+
sweep.reasons.push(`max_sweep_findings:${policy.max_sweep_findings}`);
|
|
260
|
+
}
|
|
261
|
+
if (policy.max_sweep_high_risk_findings && sweep.high_risk_finding_count >= policy.max_sweep_high_risk_findings) {
|
|
262
|
+
sweep.state = "paused";
|
|
263
|
+
sweep.reasons.push(`max_sweep_high_risk_findings:${policy.max_sweep_high_risk_findings}`);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
for (const domain of domains.values()) {
|
|
267
|
+
if (policy.max_domain_findings && domain.finding_count >= policy.max_domain_findings) {
|
|
268
|
+
domain.state = "paused";
|
|
269
|
+
domain.reasons.push(`max_domain_findings:${policy.max_domain_findings}`);
|
|
270
|
+
}
|
|
271
|
+
if (policy.max_domain_high_risk_findings && domain.high_risk_finding_count >= policy.max_domain_high_risk_findings) {
|
|
272
|
+
domain.state = "paused";
|
|
273
|
+
domain.reasons.push(`max_domain_high_risk_findings:${policy.max_domain_high_risk_findings}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const domainStatuses = [...domains.values()].sort((left, right) => left.owner_domain.localeCompare(right.owner_domain));
|
|
278
|
+
return {
|
|
279
|
+
policy,
|
|
280
|
+
state: sweep.state === "paused" || domainStatuses.some((domain) => domain.state === "paused") ? "paused" : "within_budget",
|
|
281
|
+
sweep,
|
|
282
|
+
domains: domainStatuses,
|
|
283
|
+
updated_at: verifiedAt,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function budgetBlockForChunk(plan, chunk) {
|
|
288
|
+
const status = plan.risk_budget_status;
|
|
289
|
+
if (!isPlainObject(status)) {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
if (status.sweep?.state === "paused") {
|
|
293
|
+
return `sweep risk budget paused (${(status.sweep.reasons ?? []).join(", ") || "budget reached"})`;
|
|
294
|
+
}
|
|
295
|
+
const domain = (status.domains ?? []).find((entry) => entry.owner_domain === chunk.owner_domain);
|
|
296
|
+
if (domain?.state === "paused") {
|
|
297
|
+
return `domain risk budget paused for ${chunk.owner_domain} (${(domain.reasons ?? []).join(", ") || "budget reached"})`;
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ACTIVE_CHUNK_STATES,
|
|
3
|
+
inputError,
|
|
4
|
+
loadFindings,
|
|
5
|
+
loadLatestLedger,
|
|
6
|
+
loadPlan,
|
|
7
|
+
safeSweepId,
|
|
8
|
+
} from "./common.mjs";
|
|
9
|
+
import { buildCoverageQuality } from "./coverage-quality.mjs";
|
|
10
|
+
import { ensureClusterStore } from "./risk-budget.mjs";
|
|
11
|
+
|
|
12
|
+
export async function getAuditSweepStatus(projectRoot, options) {
|
|
13
|
+
const sweepId = safeSweepId(options.sweepId);
|
|
14
|
+
if (!sweepId) {
|
|
15
|
+
return inputError("nimicoding audit-sweep refused: --sweep-id is required.\n");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const planResult = await loadPlan(projectRoot, sweepId);
|
|
19
|
+
if (!planResult.ok) {
|
|
20
|
+
return inputError(planResult.error);
|
|
21
|
+
}
|
|
22
|
+
const { findingsRef, store } = await loadFindings(projectRoot, sweepId);
|
|
23
|
+
ensureClusterStore(store);
|
|
24
|
+
const latestLedger = await loadLatestLedger(projectRoot, sweepId);
|
|
25
|
+
const chunks = Array.isArray(planResult.plan.chunks) ? planResult.plan.chunks : [];
|
|
26
|
+
const coverageQuality = latestLedger.ok
|
|
27
|
+
? latestLedger.ledger.coverage_quality ?? buildCoverageQuality(planResult.plan, chunks, planResult.plan.coverage)
|
|
28
|
+
: buildCoverageQuality(planResult.plan, chunks, planResult.plan.coverage);
|
|
29
|
+
const auditValidity = latestLedger.ok ? latestLedger.ledger.audit_validity ?? null : null;
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
ok: true,
|
|
33
|
+
exitCode: 0,
|
|
34
|
+
sweepId,
|
|
35
|
+
planRef: planResult.planRef,
|
|
36
|
+
findingsRef,
|
|
37
|
+
latestLedgerRef: latestLedger.ok ? latestLedger.ledgerRef : null,
|
|
38
|
+
coverage: {
|
|
39
|
+
totalFiles: planResult.plan.coverage?.total_files ?? 0,
|
|
40
|
+
includedFiles: planResult.plan.coverage?.included_files ?? 0,
|
|
41
|
+
...(planResult.plan.planning_basis?.mode === "spec_authority" ? {
|
|
42
|
+
authorityFiles: planResult.plan.coverage?.authority_files ?? 0,
|
|
43
|
+
evidenceFiles: planResult.plan.coverage?.evidence_files ?? 0,
|
|
44
|
+
unmappedEvidenceFiles: planResult.plan.coverage?.unmapped_evidence_files ?? 0,
|
|
45
|
+
} : {}),
|
|
46
|
+
frozenChunks: chunks.filter((chunk) => chunk.state === "frozen").length,
|
|
47
|
+
activeChunks: chunks.filter((chunk) => ACTIVE_CHUNK_STATES.has(chunk.state)).length,
|
|
48
|
+
chunks: chunks.reduce((acc, chunk) => {
|
|
49
|
+
acc[chunk.state] = (acc[chunk.state] ?? 0) + 1;
|
|
50
|
+
return acc;
|
|
51
|
+
}, {}),
|
|
52
|
+
},
|
|
53
|
+
findingCount: store.findings.length,
|
|
54
|
+
findingClusterCount: store.clusters.length,
|
|
55
|
+
clusteredSymptomCount: store.clustered_symptom_count ?? 0,
|
|
56
|
+
remediationObligationCount: store.remediation_obligation_count ?? store.findings.length,
|
|
57
|
+
unresolvedFindingCount: store.findings.filter((finding) => finding.disposition === "open").length,
|
|
58
|
+
riskBudgetStatus: planResult.plan.risk_budget_status ?? null,
|
|
59
|
+
coverageQuality,
|
|
60
|
+
auditValidity,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ACTIVE_CHUNK_STATES,
|
|
3
|
+
artifactPath,
|
|
4
|
+
ledgerRef,
|
|
5
|
+
loadLatestLedger,
|
|
6
|
+
loadYamlRef,
|
|
7
|
+
remediationMapRef,
|
|
8
|
+
sha256Object,
|
|
9
|
+
} from "./common.mjs";
|
|
10
|
+
import { pathExists } from "../fs-helpers.mjs";
|
|
11
|
+
import { isPlainObject } from "../value-helpers.mjs";
|
|
12
|
+
|
|
13
|
+
function check(checks, id, ok, reason) {
|
|
14
|
+
checks.push({ id, ok, reason });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function nonEmptyString(value) {
|
|
18
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function refExists(projectRoot, ref) {
|
|
22
|
+
const info = await pathExists(artifactPath(projectRoot, ref));
|
|
23
|
+
return Boolean(info?.isFile());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function validateEvidenceRefs(projectRoot, refs, checks, prefix) {
|
|
27
|
+
for (const ref of refs.filter((entry) => typeof entry === "string" && entry.trim())) {
|
|
28
|
+
check(checks, `${prefix}_${ref.replace(/[^a-zA-Z0-9]+/g, "_")}_exists`, await refExists(projectRoot, ref), `referenced artifact exists: ${ref}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function deriveLedgerSnapshotId(sweepId, plan, chunks, findings, clusters = []) {
|
|
33
|
+
const snapshotSeed = {
|
|
34
|
+
sweepId,
|
|
35
|
+
inventoryHash: plan.inventory_hash,
|
|
36
|
+
evidenceInventoryHash: plan.evidence_inventory_hash ?? null,
|
|
37
|
+
chunkStates: chunks.map((chunk) => ({
|
|
38
|
+
chunk_id: chunk.chunk_id,
|
|
39
|
+
state: chunk.state,
|
|
40
|
+
evidence_ref: chunk.evidence_ref ?? null,
|
|
41
|
+
finding_count: chunk.finding_count ?? 0,
|
|
42
|
+
})),
|
|
43
|
+
findings: findings.map((finding) => ({
|
|
44
|
+
id: finding.id,
|
|
45
|
+
fingerprint: finding.fingerprint,
|
|
46
|
+
disposition: finding.disposition,
|
|
47
|
+
resolution_evidence_ref: finding.resolution?.evidence_ref ?? null,
|
|
48
|
+
})),
|
|
49
|
+
clusters: clusters.map((cluster) => ({
|
|
50
|
+
cluster_id: cluster.cluster_id,
|
|
51
|
+
canonical_finding_ids: cluster.canonical_finding_ids,
|
|
52
|
+
duplicate_symptom_count: cluster.duplicate_symptom_count ?? 0,
|
|
53
|
+
})),
|
|
54
|
+
};
|
|
55
|
+
return `ledger-${sha256Object(snapshotSeed).slice(0, 16)}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function validateClusterShape(cluster, findingIds, checks) {
|
|
59
|
+
check(checks, `cluster_${cluster?.cluster_id ?? "unknown"}_required_fields`, isPlainObject(cluster)
|
|
60
|
+
&& nonEmptyString(cluster.cluster_id)
|
|
61
|
+
&& nonEmptyString(cluster.cluster_key)
|
|
62
|
+
&& nonEmptyString(cluster.representative_finding_id)
|
|
63
|
+
&& Array.isArray(cluster.canonical_finding_ids)
|
|
64
|
+
&& Array.isArray(cluster.duplicate_symptoms), "audit finding cluster has required fields");
|
|
65
|
+
if (!isPlainObject(cluster)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
check(checks, `cluster_${cluster.cluster_id}_findings_known`, findingIds.has(cluster.representative_finding_id)
|
|
69
|
+
&& cluster.canonical_finding_ids.every((findingId) => findingIds.has(findingId)), "audit finding cluster references known canonical findings");
|
|
70
|
+
check(checks, `cluster_${cluster.cluster_id}_symptom_count_matches`, (cluster.duplicate_symptom_count ?? 0) === cluster.duplicate_symptoms.length, "audit finding cluster duplicate symptom count matches symptoms");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildLedgerExpectedCounts(plan, chunks, findings, clusters = []) {
|
|
74
|
+
const frozenChunks = chunks.filter((chunk) => chunk.state === "frozen");
|
|
75
|
+
const lifecycleCoverage = {
|
|
76
|
+
frozen_chunks: frozenChunks.length,
|
|
77
|
+
failed_chunks: chunks.filter((chunk) => chunk.state === "failed").length,
|
|
78
|
+
skipped_chunks: chunks.filter((chunk) => chunk.state === "skipped").length,
|
|
79
|
+
active_chunks: chunks.filter((chunk) => ACTIVE_CHUNK_STATES.has(chunk.state)).length,
|
|
80
|
+
};
|
|
81
|
+
const coverage = plan.planning_basis?.mode === "spec_authority"
|
|
82
|
+
? buildSpecAuthorityCoverage(plan, frozenChunks, lifecycleCoverage)
|
|
83
|
+
: buildFileCoverage(plan, frozenChunks, lifecycleCoverage);
|
|
84
|
+
const findingPosture = {
|
|
85
|
+
open: findings.filter((finding) => finding.disposition === "open").length,
|
|
86
|
+
remediated: findings.filter((finding) => finding.disposition === "remediated").length,
|
|
87
|
+
accepted_risk: findings.filter((finding) => finding.disposition === "accepted-risk").length,
|
|
88
|
+
false_positive: findings.filter((finding) => finding.disposition === "false-positive").length,
|
|
89
|
+
deferred_backlog: findings.filter((finding) => finding.disposition === "deferred-backlog").length,
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
coverage,
|
|
93
|
+
findingPosture,
|
|
94
|
+
findingClusterCount: clusters.length,
|
|
95
|
+
clusteredSymptomCount: clusters.reduce((total, cluster) => total + (cluster.duplicate_symptom_count ?? 0), 0),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function buildSpecAuthorityCoverage(plan, frozenChunks, lifecycleCoverage) {
|
|
100
|
+
const authorityTotal = plan.coverage?.authority_files ?? plan.coverage?.included_files ?? 0;
|
|
101
|
+
const evidenceTotal = plan.coverage?.evidence_files ?? plan.evidence_inventory?.length ?? 0;
|
|
102
|
+
const emptyEvidenceChunks = plan.coverage?.authority_chunks_without_evidence_inventory
|
|
103
|
+
?? (Array.isArray(plan.chunks) ? plan.chunks.filter((chunk) => (chunk.evidence_inventory ?? []).length === 0).length : 0)
|
|
104
|
+
?? 0;
|
|
105
|
+
const auditedAuthorityFiles = new Set(frozenChunks.flatMap((chunk) => chunk.files));
|
|
106
|
+
const auditedEvidenceFiles = new Set(frozenChunks.flatMap((chunk) => chunk.evidence_inventory ?? []));
|
|
107
|
+
return {
|
|
108
|
+
total_files: authorityTotal + evidenceTotal,
|
|
109
|
+
included_files: authorityTotal + evidenceTotal,
|
|
110
|
+
audited_files: auditedAuthorityFiles.size + auditedEvidenceFiles.size,
|
|
111
|
+
authority_coverage: {
|
|
112
|
+
total_files: authorityTotal,
|
|
113
|
+
audited_files: auditedAuthorityFiles.size,
|
|
114
|
+
chunks_without_evidence_inventory: emptyEvidenceChunks,
|
|
115
|
+
},
|
|
116
|
+
evidence_coverage: {
|
|
117
|
+
total_files: evidenceTotal,
|
|
118
|
+
audited_files: auditedEvidenceFiles.size,
|
|
119
|
+
unmapped_files: plan.coverage?.unmapped_evidence_files ?? plan.unmapped_evidence_files?.length ?? 0,
|
|
120
|
+
},
|
|
121
|
+
...lifecycleCoverage,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function buildFileCoverage(plan, frozenChunks, lifecycleCoverage) {
|
|
126
|
+
const auditedFiles = new Set(frozenChunks.flatMap((chunk) => chunk.files));
|
|
127
|
+
return {
|
|
128
|
+
total_files: plan.coverage.total_files,
|
|
129
|
+
included_files: plan.coverage.included_files,
|
|
130
|
+
audited_files: auditedFiles.size,
|
|
131
|
+
...lifecycleCoverage,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function validateLatestLedger(projectRoot, sweepId, plan, chunks, findings, clusters, checks) {
|
|
136
|
+
const latest = await loadLatestLedger(projectRoot, sweepId);
|
|
137
|
+
check(checks, "latest_ledger_loadable", latest.ok, "latest ledger pointer and ledger are loadable");
|
|
138
|
+
if (!latest.ok) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const ledger = latest.ledger;
|
|
142
|
+
const expectedSnapshotId = deriveLedgerSnapshotId(sweepId, plan, chunks, findings, clusters);
|
|
143
|
+
check(checks, "ledger_snapshot_id_content_hash", ledger.snapshot_id === expectedSnapshotId && latest.ledgerRef === ledgerRef(sweepId, expectedSnapshotId), "ledger snapshot id is content-derived and current");
|
|
144
|
+
const expected = buildLedgerExpectedCounts(plan, chunks, findings, clusters);
|
|
145
|
+
check(checks, "ledger_coverage_counts_match", JSON.stringify(ledger.coverage) === JSON.stringify(expected.coverage), "ledger coverage counts match plan and chunks");
|
|
146
|
+
check(checks, "ledger_finding_counts_match", ledger.finding_count === findings.length
|
|
147
|
+
&& ledger.unresolved_finding_count === expected.findingPosture.open
|
|
148
|
+
&& JSON.stringify(ledger.finding_posture) === JSON.stringify(expected.findingPosture), "ledger finding counts match findings store");
|
|
149
|
+
check(checks, "ledger_cluster_counts_match", (ledger.finding_cluster_count ?? 0) === expected.findingClusterCount
|
|
150
|
+
&& (ledger.clustered_symptom_count ?? 0) === expected.clusteredSymptomCount
|
|
151
|
+
&& (ledger.remediation_obligation_count ?? findings.length) === findings.length, "ledger cluster counts match findings store");
|
|
152
|
+
check(checks, "ledger_latest_pointer_matches", latest.ledgerRef === ledgerRef(sweepId, ledger.snapshot_id), "latest pointer references the immutable ledger snapshot");
|
|
153
|
+
check(checks, "ledger_status_valid", ["candidate_ready", "partial", "blocked", "blocked_evidence_incomplete", "partial_authority_only"].includes(ledger.status), "ledger status is valid");
|
|
154
|
+
checkLedgerSpecCoverage(plan, ledger, checks);
|
|
155
|
+
await validateEvidenceRefs(projectRoot, [
|
|
156
|
+
ledger.plan_ref,
|
|
157
|
+
...(Array.isArray(ledger.chunk_refs) ? ledger.chunk_refs : []),
|
|
158
|
+
ledger.findings_ref,
|
|
159
|
+
...(Array.isArray(ledger.evidence_refs) ? ledger.evidence_refs : []),
|
|
160
|
+
ledger.report_ref,
|
|
161
|
+
ledger.run_ledger_ref,
|
|
162
|
+
], checks, "ledger_ref");
|
|
163
|
+
return { ledger, ledger_ref: latest.ledgerRef, snapshot_id: ledger.snapshot_id };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function checkLedgerSpecCoverage(plan, ledger, checks) {
|
|
167
|
+
check(checks, "ledger_candidate_ready_strict", ledger.status !== "candidate_ready"
|
|
168
|
+
|| (ledger.coverage.included_files > 0 && ledger.coverage.audited_files === ledger.coverage.included_files && ledger.coverage.active_chunks === 0 && ledger.coverage.failed_chunks === 0 && ledger.coverage.skipped_chunks === 0), "candidate_ready requires all included files audited and all chunks frozen");
|
|
169
|
+
if (plan.planning_basis?.mode !== "spec_authority") {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const authorityFull = ledger.coverage.authority_coverage?.total_files > 0
|
|
173
|
+
&& ledger.coverage.authority_coverage.audited_files === ledger.coverage.authority_coverage.total_files;
|
|
174
|
+
const evidenceFull = ledger.coverage.evidence_coverage?.audited_files === ledger.coverage.evidence_coverage?.total_files
|
|
175
|
+
&& ledger.coverage.evidence_coverage?.unmapped_files === 0;
|
|
176
|
+
check(checks, "ledger_spec_coverage_split_present", isPlainObject(ledger.coverage.authority_coverage)
|
|
177
|
+
&& isPlainObject(ledger.coverage.evidence_coverage), "spec-authority ledger splits authority and evidence coverage");
|
|
178
|
+
check(checks, "ledger_spec_coverage_quality_present", isPlainObject(ledger.coverage_quality), "spec-authority ledger exposes coverage_quality");
|
|
179
|
+
check(checks, "ledger_spec_audit_validity_present", isPlainObject(ledger.audit_validity), "spec-authority ledger exposes audit_validity");
|
|
180
|
+
check(checks, "ledger_spec_candidate_ready_requires_evidence", ledger.status !== "candidate_ready"
|
|
181
|
+
|| (authorityFull && evidenceFull), "candidate_ready requires full authority and evidence coverage");
|
|
182
|
+
check(checks, "ledger_spec_no_full_with_unmapped_evidence", ledger.status !== "candidate_ready"
|
|
183
|
+
|| ledger.coverage.evidence_coverage?.unmapped_files === 0, "candidate_ready requires zero unmapped evidence files");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export async function validateRemediationMap(projectRoot, sweepId, ledgerInfo, findings, clusters, checks) {
|
|
187
|
+
if (!ledgerInfo) {
|
|
188
|
+
check(checks, "remediation_map_ledger_available", false, "remediation map validation requires latest ledger");
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const mapRef = remediationMapRef(sweepId, ledgerInfo.snapshot_id);
|
|
192
|
+
const remediationMap = await loadYamlRef(projectRoot, mapRef);
|
|
193
|
+
const openFindings = findings.filter((finding) => finding.disposition === "open");
|
|
194
|
+
if (!isPlainObject(remediationMap)) {
|
|
195
|
+
check(checks, "remediation_map_required", false, "remediation map exists for the latest ledger");
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
check(checks, "remediation_map_identity", remediationMap.kind === "audit-remediation-map" && remediationMap.sweep_id === sweepId && remediationMap.source_ledger_ref === ledgerInfo.ledger_ref, "remediation map references latest ledger");
|
|
199
|
+
const findingIds = new Set(findings.map((finding) => finding.id));
|
|
200
|
+
const clusterIds = new Set(clusters.map((cluster) => cluster.cluster_id));
|
|
201
|
+
const mappedIds = new Set();
|
|
202
|
+
const wavesOk = Array.isArray(remediationMap.waves) && remediationMap.waves.every((wave) => {
|
|
203
|
+
if (!isPlainObject(wave) || !nonEmptyString(wave.wave_id) || !Array.isArray(wave.finding_ids) || !Array.isArray(wave.write_set)) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
for (const findingId of wave.finding_ids) {
|
|
207
|
+
mappedIds.add(findingId);
|
|
208
|
+
}
|
|
209
|
+
return wave.finding_ids.every((findingId) => findingIds.has(findingId))
|
|
210
|
+
&& (!Array.isArray(wave.cluster_ids) || wave.cluster_ids.every((clusterId) => clusterIds.has(clusterId)));
|
|
211
|
+
});
|
|
212
|
+
check(checks, "remediation_map_waves_valid", wavesOk, "remediation map waves reference known findings");
|
|
213
|
+
check(checks, "remediation_map_open_findings_covered", openFindings.every((finding) => mappedIds.has(finding.id)), "all open findings are covered by remediation map waves");
|
|
214
|
+
return { remediationMap, remediation_map_ref: mapRef };
|
|
215
|
+
}
|