@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,639 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
|
|
4
|
+
import { writeJsonRef } from "./common.mjs";
|
|
5
|
+
import { validateEvidenceEnvelope } from "./ingest.mjs";
|
|
6
|
+
import { buildAuditValidityForEvidence } from "./audit-validity.mjs";
|
|
7
|
+
import { isPlainObject } from "../value-helpers.mjs";
|
|
8
|
+
|
|
9
|
+
const ALLOWED_TOP_LEVEL_FIELDS = new Set(["chunk_id", "auditor", "coverage", "findings"]);
|
|
10
|
+
export const P0P1_RULE_CHECK_IDS = [
|
|
11
|
+
"fail_open_or_pseudo_success",
|
|
12
|
+
"partial_coverage_misrepresented_as_complete",
|
|
13
|
+
"authority_boundary_or_private_import_bypass",
|
|
14
|
+
"permission_or_capability_bypass",
|
|
15
|
+
"ungated_destructive_action",
|
|
16
|
+
"provider_or_model_hardcoding",
|
|
17
|
+
"app_local_shadow_truth",
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
function hasUnexpectedTopLevelFields(evidence) {
|
|
21
|
+
return Object.keys(evidence).filter((key) => !ALLOWED_TOP_LEVEL_FIELDS.has(key));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function firstJsonObjectPrefix(rawText) {
|
|
25
|
+
const start = rawText.indexOf("{");
|
|
26
|
+
if (start < 0) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
let depth = 0;
|
|
30
|
+
let inString = false;
|
|
31
|
+
let escaped = false;
|
|
32
|
+
for (let index = start; index < rawText.length; index += 1) {
|
|
33
|
+
const char = rawText[index];
|
|
34
|
+
if (inString) {
|
|
35
|
+
if (escaped) {
|
|
36
|
+
escaped = false;
|
|
37
|
+
} else if (char === "\\") {
|
|
38
|
+
escaped = true;
|
|
39
|
+
} else if (char === "\"") {
|
|
40
|
+
inString = false;
|
|
41
|
+
}
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (char === "\"") {
|
|
45
|
+
inString = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (char === "{") {
|
|
49
|
+
depth += 1;
|
|
50
|
+
}
|
|
51
|
+
if (char === "}") {
|
|
52
|
+
depth -= 1;
|
|
53
|
+
if (depth === 0) {
|
|
54
|
+
return {
|
|
55
|
+
jsonText: rawText.slice(start, index + 1),
|
|
56
|
+
trailing: rawText.slice(index + 1),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (depth < 0) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseCodexRawJson(rawText) {
|
|
68
|
+
try {
|
|
69
|
+
return { ok: true, value: JSON.parse(rawText) };
|
|
70
|
+
} catch {
|
|
71
|
+
const singleBraceRepair = parseWithUniqueDroppedClosingBrace(rawText);
|
|
72
|
+
if (singleBraceRepair.ok) {
|
|
73
|
+
return singleBraceRepair;
|
|
74
|
+
}
|
|
75
|
+
const prefix = firstJsonObjectPrefix(rawText);
|
|
76
|
+
const trailing = prefix?.trailing?.trim() ?? "";
|
|
77
|
+
if (prefix && /^}*$/.test(trailing)) {
|
|
78
|
+
try {
|
|
79
|
+
return { ok: true, value: JSON.parse(prefix.jsonText), repaired: trailing ? "ignored_trailing_closing_braces" : null };
|
|
80
|
+
} catch {
|
|
81
|
+
// Fall through to the contract error below.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (prefix && /^,\s*"findings_count"\s*:\s*\d+\s*[}]\s*$/u.test(trailing)) {
|
|
85
|
+
try {
|
|
86
|
+
const value = JSON.parse(prefix.jsonText);
|
|
87
|
+
if (isPlainObject(value) && hasUnexpectedTopLevelFields(value).length === 0) {
|
|
88
|
+
return { ok: true, value, repaired: "ignored_trailing_findings_count_metadata" };
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
// Fall through to the contract error below.
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const duplicateTrailingFieldRepair = parseWithDuplicateTrailingTopLevelFields(prefix, trailing);
|
|
95
|
+
if (duplicateTrailingFieldRepair.ok) {
|
|
96
|
+
return duplicateTrailingFieldRepair;
|
|
97
|
+
}
|
|
98
|
+
return { ok: false, error: "Codex auditor raw output must be exact JSON, without markdown or transcript prose" };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parseWithDuplicateTrailingTopLevelFields(prefix, trailing) {
|
|
103
|
+
if (!prefix || !trailing.startsWith(",") || !trailing.endsWith("}")) {
|
|
104
|
+
return { ok: false };
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const value = JSON.parse(prefix.jsonText);
|
|
108
|
+
if (!isPlainObject(value) || hasUnexpectedTopLevelFields(value).length > 0) {
|
|
109
|
+
return { ok: false };
|
|
110
|
+
}
|
|
111
|
+
const trailingFields = JSON.parse(`{${trailing.slice(1)}`);
|
|
112
|
+
if (!isPlainObject(trailingFields)) {
|
|
113
|
+
return { ok: false };
|
|
114
|
+
}
|
|
115
|
+
const keys = Object.keys(trailingFields);
|
|
116
|
+
if (keys.length === 0 || keys.some((key) => !ALLOWED_TOP_LEVEL_FIELDS.has(key))) {
|
|
117
|
+
return { ok: false };
|
|
118
|
+
}
|
|
119
|
+
if (!keys.every((key) => isDeepStrictEqual(trailingFields[key], value[key]))) {
|
|
120
|
+
return { ok: false };
|
|
121
|
+
}
|
|
122
|
+
return { ok: true, value, repaired: "ignored_duplicate_trailing_top_level_fields" };
|
|
123
|
+
} catch {
|
|
124
|
+
return { ok: false };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function parseWithUniqueDroppedClosingBrace(rawText) {
|
|
129
|
+
const candidates = [];
|
|
130
|
+
const candidateKeys = new Set();
|
|
131
|
+
for (let index = 0; index < rawText.length; index += 1) {
|
|
132
|
+
if (rawText[index] !== "}") {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
const value = JSON.parse(`${rawText.slice(0, index)}${rawText.slice(index + 1)}`);
|
|
137
|
+
if (isPlainObject(value) && hasUnexpectedTopLevelFields(value).length === 0) {
|
|
138
|
+
const key = JSON.stringify(value);
|
|
139
|
+
if (!candidateKeys.has(key)) {
|
|
140
|
+
candidateKeys.add(key);
|
|
141
|
+
candidates.push(value);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} catch {
|
|
145
|
+
// Keep searching for an unambiguous single-token structural repair.
|
|
146
|
+
}
|
|
147
|
+
if (candidates.length > 1) {
|
|
148
|
+
return { ok: false };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (candidates.length === 1) {
|
|
152
|
+
return { ok: true, value: candidates[0], repaired: "dropped_single_extra_closing_brace" };
|
|
153
|
+
}
|
|
154
|
+
return { ok: false };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function normalizeRefs(refs) {
|
|
158
|
+
return Array.isArray(refs)
|
|
159
|
+
? refs.map((ref) => typeof ref === "string" ? ref.replace(/\\/g, "/") : ref)
|
|
160
|
+
: [];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function uniqueRefs(refs) {
|
|
164
|
+
return [...new Set(refs)].sort();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function refsOutsideSet(refs, allowedSet) {
|
|
168
|
+
return refs.filter((ref) => typeof ref !== "string" || !allowedSet.has(ref));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isNonImplementationContextRef(ref) {
|
|
172
|
+
if (typeof ref !== "string") {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
const normalized = ref.replace(/\\/g, "/");
|
|
176
|
+
return /(^|\/)AGENTS\.md$/u.test(normalized)
|
|
177
|
+
|| /(^|\/)README\.md$/u.test(normalized)
|
|
178
|
+
|| normalized.startsWith(".nimi/spec/")
|
|
179
|
+
|| normalized.startsWith(".nimi/contracts/")
|
|
180
|
+
|| normalized.startsWith(".nimi/methodology/")
|
|
181
|
+
|| normalized.startsWith("nimi-coding/methodology/")
|
|
182
|
+
|| normalized.startsWith("nimi-coding/spec/");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function stripNonImplementationContextRefs(refs, evidenceInventorySet) {
|
|
186
|
+
return refs.filter((ref) => evidenceInventorySet.has(ref) || !isNonImplementationContextRef(ref));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function normalizeFindingEnvelope(finding, evidenceInventorySet, authorityRefSet = new Set()) {
|
|
190
|
+
if (!isPlainObject(finding)) {
|
|
191
|
+
return finding;
|
|
192
|
+
}
|
|
193
|
+
const severityInput = typeof finding.severity === "string" && finding.severity.trim()
|
|
194
|
+
? finding.severity
|
|
195
|
+
: typeof finding.priority === "string" && finding.priority.trim()
|
|
196
|
+
? finding.priority
|
|
197
|
+
: typeof finding.impact === "string" && finding.impact.trim()
|
|
198
|
+
? finding.impact
|
|
199
|
+
: null;
|
|
200
|
+
const severity = typeof severityInput === "string" ? severityInput.trim().toLowerCase() : "";
|
|
201
|
+
const patch = {};
|
|
202
|
+
if (severity === "p0") {
|
|
203
|
+
patch.severity = "critical";
|
|
204
|
+
}
|
|
205
|
+
if (severity === "p1") {
|
|
206
|
+
patch.severity = "high";
|
|
207
|
+
}
|
|
208
|
+
if (["critical", "high", "medium", "low"].includes(severity)) {
|
|
209
|
+
patch.severity = severity;
|
|
210
|
+
}
|
|
211
|
+
const actionability = typeof finding.actionability === "string"
|
|
212
|
+
? finding.actionability.trim().toLowerCase().replace(/_/g, "-")
|
|
213
|
+
: "";
|
|
214
|
+
if (!actionability) {
|
|
215
|
+
patch.actionability = "needs-decision";
|
|
216
|
+
}
|
|
217
|
+
if (["needs-fix", "manual-fix", "manual-review", "fix-required", "needs-remediation"].includes(actionability)) {
|
|
218
|
+
patch.actionability = "needs-decision";
|
|
219
|
+
}
|
|
220
|
+
if (actionability === "auto-fix" || actionability === "autofix") {
|
|
221
|
+
patch.actionability = "auto-fix";
|
|
222
|
+
}
|
|
223
|
+
if (actionability === "deferred" || actionability === "backlog" || actionability === "deferred-backlog") {
|
|
224
|
+
patch.actionability = "deferred-backlog";
|
|
225
|
+
}
|
|
226
|
+
const confidence = typeof finding.confidence === "string"
|
|
227
|
+
? finding.confidence.trim().toLowerCase()
|
|
228
|
+
: "";
|
|
229
|
+
if (!confidence) {
|
|
230
|
+
patch.confidence = "high";
|
|
231
|
+
}
|
|
232
|
+
if (["high", "medium", "low"].includes(confidence)) {
|
|
233
|
+
patch.confidence = confidence;
|
|
234
|
+
}
|
|
235
|
+
if (typeof finding.category !== "string" || !finding.category.trim()) {
|
|
236
|
+
const ruleId = Array.isArray(finding.rule_ids)
|
|
237
|
+
? finding.rule_ids.find((entry) => typeof entry === "string" && entry.trim())
|
|
238
|
+
: null;
|
|
239
|
+
if (ruleId) {
|
|
240
|
+
patch.category = ruleId.trim();
|
|
241
|
+
} else if (typeof finding.rule_check_id === "string" && finding.rule_check_id.trim()) {
|
|
242
|
+
patch.category = finding.rule_check_id.trim();
|
|
243
|
+
} else if (typeof finding.rule_id === "string" && finding.rule_id.trim()) {
|
|
244
|
+
patch.category = finding.rule_id.trim();
|
|
245
|
+
} else if (typeof finding.defect_class === "string" && finding.defect_class.trim()) {
|
|
246
|
+
patch.category = finding.defect_class.trim();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (typeof finding.title !== "string" || !finding.title.trim()) {
|
|
250
|
+
const summary = typeof finding.summary === "string" && finding.summary.trim() ? finding.summary.trim() : null;
|
|
251
|
+
if (summary) {
|
|
252
|
+
patch.title = summary;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (typeof finding.description !== "string" || !finding.description.trim()) {
|
|
256
|
+
const reasoning = typeof finding.reasoning === "string" && finding.reasoning.trim()
|
|
257
|
+
? finding.reasoning.trim()
|
|
258
|
+
: typeof finding.details === "string" && finding.details.trim()
|
|
259
|
+
? finding.details.trim()
|
|
260
|
+
: typeof finding.summary === "string" && finding.summary.trim()
|
|
261
|
+
? finding.summary.trim()
|
|
262
|
+
: null;
|
|
263
|
+
if (reasoning) {
|
|
264
|
+
patch.description = reasoning;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (!isPlainObject(finding.location) || typeof finding.location.file !== "string" || !finding.location.file.trim()) {
|
|
268
|
+
const location = Array.isArray(finding.locations)
|
|
269
|
+
? finding.locations.find((entry) => isPlainObject(entry)
|
|
270
|
+
&& typeof entry.ref === "string"
|
|
271
|
+
&& evidenceInventorySet.has(entry.ref.replace(/\\/g, "/")))
|
|
272
|
+
: null;
|
|
273
|
+
if (location) {
|
|
274
|
+
patch.location = {
|
|
275
|
+
file: location.ref.replace(/\\/g, "/"),
|
|
276
|
+
...(Number.isInteger(location.line) && location.line > 0 ? { line: location.line } : {}),
|
|
277
|
+
...(typeof location.symbol === "string" && location.symbol.trim() ? { symbol: location.symbol.trim() } : {}),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
const implementationRefs = normalizeRefs(finding.implementation_refs);
|
|
281
|
+
const locationFile = implementationRefs.find((ref) => evidenceInventorySet.has(ref));
|
|
282
|
+
if (!patch.location && locationFile) {
|
|
283
|
+
patch.location = {
|
|
284
|
+
file: locationFile,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
const authorityRefs = normalizeRefs(finding.authority_refs);
|
|
288
|
+
const authorityLocationFile = authorityRefs.find((ref) => authorityRefSet.has(ref));
|
|
289
|
+
if (!patch.location && authorityLocationFile) {
|
|
290
|
+
patch.location = {
|
|
291
|
+
file: authorityLocationFile,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (!isPlainObject(finding.evidence)) {
|
|
296
|
+
const summary = typeof finding.description === "string" && finding.description.trim()
|
|
297
|
+
? finding.description.trim()
|
|
298
|
+
: typeof finding.impact === "string" && finding.impact.trim()
|
|
299
|
+
? finding.impact.trim()
|
|
300
|
+
: typeof finding.title === "string" && finding.title.trim()
|
|
301
|
+
? finding.title.trim()
|
|
302
|
+
: "";
|
|
303
|
+
const auditorReasoning = typeof finding.recommendation === "string" && finding.recommendation.trim()
|
|
304
|
+
? finding.recommendation.trim()
|
|
305
|
+
: summary;
|
|
306
|
+
if (summary && auditorReasoning) {
|
|
307
|
+
patch.evidence = {
|
|
308
|
+
summary,
|
|
309
|
+
auditor_reasoning: auditorReasoning,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return Object.keys(patch).length > 0 ? { ...finding, ...patch } : finding;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function validateCodexProvenance(evidence, expectedPacketRef) {
|
|
317
|
+
const provenance = evidence?.auditor?.provenance;
|
|
318
|
+
if (!isPlainObject(provenance)) {
|
|
319
|
+
return { ok: false, error: "auditor.provenance is required" };
|
|
320
|
+
}
|
|
321
|
+
if (provenance.kind !== "semantic_audit") {
|
|
322
|
+
return { ok: false, error: "auditor.provenance.kind must be semantic_audit" };
|
|
323
|
+
}
|
|
324
|
+
if (provenance.packet_ref !== expectedPacketRef) {
|
|
325
|
+
return { ok: false, error: "auditor.provenance.packet_ref must match the auditor packet" };
|
|
326
|
+
}
|
|
327
|
+
const tracePresent = ["session_ref", "transcript_ref", "review_ref"]
|
|
328
|
+
.some((field) => typeof provenance[field] === "string" && provenance[field].trim().length > 0);
|
|
329
|
+
if (!tracePresent) {
|
|
330
|
+
return { ok: false, error: "auditor.provenance requires session_ref, transcript_ref, or review_ref" };
|
|
331
|
+
}
|
|
332
|
+
return { ok: true };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function validateCodexAuditorEvidence(evidence, chunk, expectedPacketRef) {
|
|
336
|
+
if (!isPlainObject(evidence)) {
|
|
337
|
+
return { ok: false, error: "Codex auditor output must be a JSON object" };
|
|
338
|
+
}
|
|
339
|
+
if (Object.prototype.hasOwnProperty.call(evidence, "p0p1_rule_checks")) {
|
|
340
|
+
return { ok: false, error: "Codex auditor output must place p0p1_rule_checks under coverage" };
|
|
341
|
+
}
|
|
342
|
+
const unexpectedFields = hasUnexpectedTopLevelFields(evidence);
|
|
343
|
+
if (unexpectedFields.length > 0) {
|
|
344
|
+
return { ok: false, error: `Codex auditor output has unexpected top-level fields: ${unexpectedFields.join(", ")}` };
|
|
345
|
+
}
|
|
346
|
+
if (!isPlainObject(evidence.auditor)
|
|
347
|
+
|| typeof evidence.auditor.mode !== "string"
|
|
348
|
+
|| !evidence.auditor.mode.trim()
|
|
349
|
+
|| typeof evidence.auditor.methodology_ref !== "string"
|
|
350
|
+
|| !evidence.auditor.methodology_ref.trim()) {
|
|
351
|
+
return { ok: false, error: "Codex auditor output requires auditor.mode and auditor.methodology_ref" };
|
|
352
|
+
}
|
|
353
|
+
const provenance = validateCodexProvenance(evidence, expectedPacketRef);
|
|
354
|
+
if (!provenance.ok) {
|
|
355
|
+
return provenance;
|
|
356
|
+
}
|
|
357
|
+
const envelope = validateEvidenceEnvelope(evidence, chunk);
|
|
358
|
+
if (!envelope.ok) {
|
|
359
|
+
return envelope;
|
|
360
|
+
}
|
|
361
|
+
const auditValidity = buildAuditValidityForEvidence(chunk, evidence);
|
|
362
|
+
if (auditValidity.posture === "invalid") {
|
|
363
|
+
return {
|
|
364
|
+
ok: false,
|
|
365
|
+
error: `audit evidence is invalid (${auditValidity.blockers.map((blocker) => blocker.id).join(", ")})`,
|
|
366
|
+
auditValidity,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
return { ok: true, auditValidity };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function semanticOutputRef(rawOutput, chunk, expectedAuthorityRefs) {
|
|
373
|
+
if (rawOutput?.chunk_id !== chunk.chunk_id) {
|
|
374
|
+
return { ok: false, error: "Codex auditor raw output chunk_id must match the chunk" };
|
|
375
|
+
}
|
|
376
|
+
if (Object.prototype.hasOwnProperty.call(rawOutput, "p0p1_rule_checks")) {
|
|
377
|
+
return { ok: false, error: "Codex auditor output must place p0p1_rule_checks under coverage" };
|
|
378
|
+
}
|
|
379
|
+
const unexpectedFields = hasUnexpectedTopLevelFields(rawOutput);
|
|
380
|
+
if (unexpectedFields.length > 0) {
|
|
381
|
+
return { ok: false, error: `Codex auditor output has unexpected top-level fields: ${unexpectedFields.join(", ")}` };
|
|
382
|
+
}
|
|
383
|
+
if (!isPlainObject(rawOutput.coverage)) {
|
|
384
|
+
return { ok: false, error: "Codex auditor raw output coverage is required" };
|
|
385
|
+
}
|
|
386
|
+
if (!Array.isArray(rawOutput.findings)) {
|
|
387
|
+
return { ok: false, error: "Codex auditor raw output findings must be an array" };
|
|
388
|
+
}
|
|
389
|
+
const outcomes = rawOutput.coverage.authority_outcomes;
|
|
390
|
+
if (!Array.isArray(outcomes)) {
|
|
391
|
+
return { ok: false, error: "Codex auditor raw output coverage.authority_outcomes is required" };
|
|
392
|
+
}
|
|
393
|
+
if (outcomes.length !== expectedAuthorityRefs.length) {
|
|
394
|
+
return { ok: false, error: "Codex auditor raw output must contain one authority_outcome per authority ref" };
|
|
395
|
+
}
|
|
396
|
+
return { ok: true };
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function normalizeOutcome(rawOutcome, index, authorityRef, evidenceInventorySet) {
|
|
400
|
+
if (!isPlainObject(rawOutcome)) {
|
|
401
|
+
return { ok: false, error: `authority_outcomes[${index}] must be an object` };
|
|
402
|
+
}
|
|
403
|
+
const rawAuthorityRef = typeof rawOutcome.authority_ref === "string" ? rawOutcome.authority_ref.replace(/\\/g, "/") : authorityRef;
|
|
404
|
+
if (rawAuthorityRef !== authorityRef) {
|
|
405
|
+
return { ok: false, error: `authority_outcomes[${index}].authority_ref must match ${authorityRef}` };
|
|
406
|
+
}
|
|
407
|
+
const status = rawOutcome.status ?? "audited";
|
|
408
|
+
if (!["audited", "blocked", "not_applicable"].includes(status)) {
|
|
409
|
+
return { ok: false, error: `authority_outcomes[${index}].status must be audited, blocked, or not_applicable` };
|
|
410
|
+
}
|
|
411
|
+
const inspectedImplementationRefs = uniqueRefs([
|
|
412
|
+
...normalizeRefs(rawOutcome.inspected_implementation_refs),
|
|
413
|
+
...normalizeRefs(rawOutcome.implementation_evidence_refs),
|
|
414
|
+
]);
|
|
415
|
+
const implementationRefs = stripNonImplementationContextRefs(inspectedImplementationRefs, evidenceInventorySet);
|
|
416
|
+
const invalidImplementationRefs = refsOutsideSet(implementationRefs, evidenceInventorySet);
|
|
417
|
+
if (invalidImplementationRefs.length > 0) {
|
|
418
|
+
return {
|
|
419
|
+
ok: false,
|
|
420
|
+
error: `authority_outcomes[${index}] inspected implementation refs must belong to chunk evidence inventory: ${invalidImplementationRefs.join(", ")}`,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
const normalized = {
|
|
424
|
+
authority_ref: authorityRef,
|
|
425
|
+
status,
|
|
426
|
+
evidence_refs: uniqueRefs([authorityRef, ...implementationRefs]),
|
|
427
|
+
implementation_evidence_refs: implementationRefs,
|
|
428
|
+
};
|
|
429
|
+
if (typeof rawOutcome.negative_reasoning === "string" && rawOutcome.negative_reasoning.trim()) {
|
|
430
|
+
normalized.negative_reasoning = rawOutcome.negative_reasoning.trim();
|
|
431
|
+
}
|
|
432
|
+
if (!normalized.negative_reasoning && typeof rawOutcome.reasoning === "string" && rawOutcome.reasoning.trim()) {
|
|
433
|
+
normalized.negative_reasoning = rawOutcome.reasoning.trim();
|
|
434
|
+
}
|
|
435
|
+
if (typeof rawOutcome.reason === "string" && rawOutcome.reason.trim()) {
|
|
436
|
+
normalized.reason = rawOutcome.reason.trim();
|
|
437
|
+
}
|
|
438
|
+
if (typeof rawOutcome.implementation_not_applicable_reason === "string" && rawOutcome.implementation_not_applicable_reason.trim()) {
|
|
439
|
+
normalized.implementation_not_applicable_reason = rawOutcome.implementation_not_applicable_reason.trim();
|
|
440
|
+
}
|
|
441
|
+
if (!normalized.reason && status === "not_applicable" && normalized.implementation_not_applicable_reason) {
|
|
442
|
+
normalized.reason = normalized.implementation_not_applicable_reason;
|
|
443
|
+
}
|
|
444
|
+
if (!normalized.reason && ["blocked", "not_applicable"].includes(status) && normalized.negative_reasoning) {
|
|
445
|
+
normalized.reason = normalized.negative_reasoning;
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
ok: true,
|
|
449
|
+
outcome: normalized,
|
|
450
|
+
inspectedImplementationRefs: implementationRefs,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function normalizeRuleChecks(rawRuleChecks, evidenceInventorySet, authorityRefSet) {
|
|
455
|
+
if (rawRuleChecks === undefined) {
|
|
456
|
+
return { ok: true, ruleChecks: [], implementationRefs: [] };
|
|
457
|
+
}
|
|
458
|
+
if (!Array.isArray(rawRuleChecks)) {
|
|
459
|
+
return { ok: false, error: "coverage.p0p1_rule_checks must be an array when present" };
|
|
460
|
+
}
|
|
461
|
+
const ruleChecks = [];
|
|
462
|
+
const implementationRefs = [];
|
|
463
|
+
const checkedIds = [];
|
|
464
|
+
for (const [index, rawCheck] of rawRuleChecks.entries()) {
|
|
465
|
+
if (!isPlainObject(rawCheck)) {
|
|
466
|
+
return { ok: false, error: `coverage.p0p1_rule_checks[${index}] must be an object` };
|
|
467
|
+
}
|
|
468
|
+
const id = typeof rawCheck.id === "string" ? rawCheck.id : "";
|
|
469
|
+
if (!P0P1_RULE_CHECK_IDS.includes(id)) {
|
|
470
|
+
return { ok: false, error: `coverage.p0p1_rule_checks[${index}].id is not a required P0/P1 rule id` };
|
|
471
|
+
}
|
|
472
|
+
checkedIds.push(id);
|
|
473
|
+
if (!["checked", "not_applicable"].includes(rawCheck.status)) {
|
|
474
|
+
return { ok: false, error: `coverage.p0p1_rule_checks[${index}].status must be checked or not_applicable` };
|
|
475
|
+
}
|
|
476
|
+
if (typeof rawCheck.negative_reasoning !== "string" || !rawCheck.negative_reasoning.trim()) {
|
|
477
|
+
return { ok: false, error: `coverage.p0p1_rule_checks[${index}].negative_reasoning is required` };
|
|
478
|
+
}
|
|
479
|
+
const rawRefs = stripNonImplementationContextRefs(uniqueRefs(normalizeRefs(rawCheck.implementation_refs)), evidenceInventorySet);
|
|
480
|
+
const refs = rawRefs.filter((ref) => evidenceInventorySet.has(ref));
|
|
481
|
+
const invalidRawRefs = rawRefs.filter((ref) => !evidenceInventorySet.has(ref) && !authorityRefSet.has(ref));
|
|
482
|
+
if (rawCheck.status === "checked" && refs.length === 0) {
|
|
483
|
+
return { ok: false, error: `coverage.p0p1_rule_checks[${index}].implementation_refs is required when status is checked` };
|
|
484
|
+
}
|
|
485
|
+
if (invalidRawRefs.length > 0) {
|
|
486
|
+
return {
|
|
487
|
+
ok: false,
|
|
488
|
+
error: `coverage.p0p1_rule_checks[${index}].implementation_refs must belong to chunk evidence inventory: ${invalidRawRefs.join(", ")}`,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
implementationRefs.push(...refs);
|
|
492
|
+
ruleChecks.push({
|
|
493
|
+
id,
|
|
494
|
+
status: rawCheck.status,
|
|
495
|
+
implementation_refs: refs,
|
|
496
|
+
negative_reasoning: rawCheck.negative_reasoning.trim(),
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
const missingRuleCheckIds = P0P1_RULE_CHECK_IDS.filter((id) => !checkedIds.includes(id));
|
|
500
|
+
if (missingRuleCheckIds.length > 0) {
|
|
501
|
+
return { ok: false, error: `coverage.p0p1_rule_checks must include every required P0/P1 rule id: missing ${missingRuleCheckIds.join(", ")}` };
|
|
502
|
+
}
|
|
503
|
+
return { ok: true, ruleChecks, implementationRefs: uniqueRefs(implementationRefs) };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function deriveP0P1NegativeReasoningFromRuleChecks(ruleChecks) {
|
|
507
|
+
const reasons = uniqueRefs(ruleChecks
|
|
508
|
+
.map((check) => typeof check.negative_reasoning === "string" ? check.negative_reasoning.trim() : "")
|
|
509
|
+
.filter(Boolean));
|
|
510
|
+
if (reasons.length === 0) {
|
|
511
|
+
return null;
|
|
512
|
+
}
|
|
513
|
+
return reasons.join(" ");
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function normalizeCodexSemanticOutput(rawOutput, chunk, options) {
|
|
517
|
+
if (!isPlainObject(rawOutput)) {
|
|
518
|
+
return { ok: false, error: "Codex auditor output must be a JSON object" };
|
|
519
|
+
}
|
|
520
|
+
const authorityRefs = chunk.authority_refs ?? chunk.files;
|
|
521
|
+
const semanticShape = semanticOutputRef(rawOutput, chunk, authorityRefs);
|
|
522
|
+
if (!semanticShape.ok) {
|
|
523
|
+
return semanticShape;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const evidenceInventory = chunk.planning_basis === "spec_authority" ? (chunk.evidence_inventory ?? []) : (chunk.files ?? []);
|
|
527
|
+
const evidenceInventorySet = new Set(evidenceInventory);
|
|
528
|
+
const authorityRefSet = new Set(authorityRefs);
|
|
529
|
+
const outcomes = [];
|
|
530
|
+
const inspectedRefs = [];
|
|
531
|
+
for (const [index, authorityRef] of authorityRefs.entries()) {
|
|
532
|
+
const rawOutcome = rawOutput.coverage.authority_outcomes[index];
|
|
533
|
+
const normalized = normalizeOutcome(rawOutcome, index, authorityRef, evidenceInventorySet);
|
|
534
|
+
if (!normalized.ok) {
|
|
535
|
+
return normalized;
|
|
536
|
+
}
|
|
537
|
+
outcomes.push(normalized.outcome);
|
|
538
|
+
inspectedRefs.push(...normalized.inspectedImplementationRefs);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const ruleChecks = normalizeRuleChecks(rawOutput.coverage.p0p1_rule_checks, evidenceInventorySet, authorityRefSet);
|
|
542
|
+
if (!ruleChecks.ok) {
|
|
543
|
+
return ruleChecks;
|
|
544
|
+
}
|
|
545
|
+
const p0p1EvidenceRefs = uniqueRefs([
|
|
546
|
+
...inspectedRefs,
|
|
547
|
+
...ruleChecks.implementationRefs,
|
|
548
|
+
...normalizeRefs(rawOutput.coverage.p0p1_evidence_refs),
|
|
549
|
+
...normalizeRefs(rawOutput.coverage.inspected_implementation_refs),
|
|
550
|
+
]);
|
|
551
|
+
const normalizedP0P1EvidenceRefs = stripNonImplementationContextRefs(p0p1EvidenceRefs, evidenceInventorySet);
|
|
552
|
+
const invalidP0P1EvidenceRefs = refsOutsideSet(normalizedP0P1EvidenceRefs, evidenceInventorySet);
|
|
553
|
+
if (invalidP0P1EvidenceRefs.length > 0) {
|
|
554
|
+
return {
|
|
555
|
+
ok: false,
|
|
556
|
+
error: `coverage inspected implementation refs must belong to chunk evidence inventory: ${invalidP0P1EvidenceRefs.join(", ")}`,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const evidence = {
|
|
561
|
+
chunk_id: chunk.chunk_id,
|
|
562
|
+
auditor: {
|
|
563
|
+
id: typeof rawOutput.auditor?.id === "string" && rawOutput.auditor.id.trim() ? rawOutput.auditor.id : options.auditorId,
|
|
564
|
+
mode: "codex_semantic_audit",
|
|
565
|
+
methodology_ref: "nimi-coding/methodology/audit-sweep-p0p1-recall.yaml",
|
|
566
|
+
provenance: {
|
|
567
|
+
kind: "semantic_audit",
|
|
568
|
+
packet_ref: options.packetRef,
|
|
569
|
+
session_ref: options.sessionRef,
|
|
570
|
+
transcript_ref: options.transcriptRef,
|
|
571
|
+
},
|
|
572
|
+
},
|
|
573
|
+
coverage: {
|
|
574
|
+
files: chunk.files,
|
|
575
|
+
authority_refs: authorityRefs,
|
|
576
|
+
evidence_files: evidenceInventory,
|
|
577
|
+
authority_outcomes: outcomes,
|
|
578
|
+
p0p1_evidence_refs: normalizedP0P1EvidenceRefs,
|
|
579
|
+
p0p1_rule_checks: ruleChecks.ruleChecks,
|
|
580
|
+
},
|
|
581
|
+
findings: rawOutput.findings.map((finding) => normalizeFindingEnvelope(finding, evidenceInventorySet, authorityRefSet)),
|
|
582
|
+
};
|
|
583
|
+
if (typeof rawOutput.coverage.p0p1_negative_reasoning === "string" && rawOutput.coverage.p0p1_negative_reasoning.trim()) {
|
|
584
|
+
evidence.coverage.p0p1_negative_reasoning = rawOutput.coverage.p0p1_negative_reasoning.trim();
|
|
585
|
+
} else if (evidence.findings.length === 0) {
|
|
586
|
+
const derivedReasoning = deriveP0P1NegativeReasoningFromRuleChecks(ruleChecks.ruleChecks);
|
|
587
|
+
if (derivedReasoning) {
|
|
588
|
+
evidence.coverage.p0p1_negative_reasoning = derivedReasoning;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (typeof rawOutput.coverage.p0p1_implementation_not_applicable_reason === "string" && rawOutput.coverage.p0p1_implementation_not_applicable_reason.trim()) {
|
|
592
|
+
evidence.coverage.p0p1_implementation_not_applicable_reason = rawOutput.coverage.p0p1_implementation_not_applicable_reason.trim();
|
|
593
|
+
}
|
|
594
|
+
if (!evidence.coverage.p0p1_implementation_not_applicable_reason && evidenceInventory.length === 0) {
|
|
595
|
+
const outcomeReasons = outcomes
|
|
596
|
+
.map((outcome) => outcome.implementation_not_applicable_reason)
|
|
597
|
+
.filter((reason) => typeof reason === "string" && reason.trim().length > 0);
|
|
598
|
+
if (outcomeReasons.length > 0) {
|
|
599
|
+
evidence.coverage.p0p1_implementation_not_applicable_reason = uniqueRefs(outcomeReasons).join(" ");
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return { ok: true, evidence };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
export async function extractCodexAuditorEvidenceFile(projectRoot, options) {
|
|
606
|
+
let rawText = "";
|
|
607
|
+
try {
|
|
608
|
+
rawText = await readFile(options.rawOutputPath, "utf8");
|
|
609
|
+
} catch {
|
|
610
|
+
return { ok: false, error: "Codex auditor raw output file is missing" };
|
|
611
|
+
}
|
|
612
|
+
const parsedRaw = parseCodexRawJson(rawText);
|
|
613
|
+
if (!parsedRaw.ok) {
|
|
614
|
+
return parsedRaw;
|
|
615
|
+
}
|
|
616
|
+
const parsed = parsedRaw.value;
|
|
617
|
+
|
|
618
|
+
const normalized = normalizeCodexSemanticOutput(parsed, options.chunk, {
|
|
619
|
+
packetRef: options.packetRef,
|
|
620
|
+
sessionRef: options.sessionRef,
|
|
621
|
+
transcriptRef: options.transcriptRef,
|
|
622
|
+
auditorId: options.auditorId,
|
|
623
|
+
});
|
|
624
|
+
if (!normalized.ok) {
|
|
625
|
+
return normalized;
|
|
626
|
+
}
|
|
627
|
+
const validation = validateCodexAuditorEvidence(normalized.evidence, options.chunk, options.packetRef);
|
|
628
|
+
if (!validation.ok) {
|
|
629
|
+
return validation;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
await writeJsonRef(projectRoot, options.evidenceRef, normalized.evidence);
|
|
633
|
+
return {
|
|
634
|
+
ok: true,
|
|
635
|
+
evidence: normalized.evidence,
|
|
636
|
+
evidenceRef: options.evidenceRef,
|
|
637
|
+
auditValidity: validation.auditValidity,
|
|
638
|
+
};
|
|
639
|
+
}
|