@nimiplatform/nimi-coding 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +348 -0
  3. package/adapters/README.md +25 -0
  4. package/adapters/claude/README.md +89 -0
  5. package/adapters/claude/profile.yaml +70 -0
  6. package/adapters/codex/README.md +53 -0
  7. package/adapters/codex/profile.yaml +78 -0
  8. package/adapters/oh-my-codex/README.md +185 -0
  9. package/adapters/oh-my-codex/profile.yaml +46 -0
  10. package/bin/nimicoding.mjs +6 -0
  11. package/cli/commands/admit-high-risk-decision.mjs +108 -0
  12. package/cli/commands/audit-sweep.mjs +341 -0
  13. package/cli/commands/blueprint-audit.mjs +91 -0
  14. package/cli/commands/clear.mjs +168 -0
  15. package/cli/commands/closeout.mjs +183 -0
  16. package/cli/commands/decide-high-risk-execution.mjs +124 -0
  17. package/cli/commands/doctor.mjs +53 -0
  18. package/cli/commands/generate-spec-derived-docs.mjs +131 -0
  19. package/cli/commands/handoff.mjs +123 -0
  20. package/cli/commands/ingest-high-risk-execution.mjs +95 -0
  21. package/cli/commands/review-high-risk-execution.mjs +95 -0
  22. package/cli/commands/start.mjs +717 -0
  23. package/cli/commands/topic-formatters.mjs +382 -0
  24. package/cli/commands/topic-goal.mjs +33 -0
  25. package/cli/commands/topic-options-shared.mjs +27 -0
  26. package/cli/commands/topic-options-workflow.mjs +767 -0
  27. package/cli/commands/topic-options.mjs +626 -0
  28. package/cli/commands/topic-runner.mjs +169 -0
  29. package/cli/commands/topic.mjs +795 -0
  30. package/cli/commands/validate-acceptance.mjs +5 -0
  31. package/cli/commands/validate-ai-governance.mjs +214 -0
  32. package/cli/commands/validate-execution-packet.mjs +5 -0
  33. package/cli/commands/validate-orchestration-state.mjs +5 -0
  34. package/cli/commands/validate-prompt.mjs +5 -0
  35. package/cli/commands/validate-spec-audit.mjs +27 -0
  36. package/cli/commands/validate-spec-governance.mjs +124 -0
  37. package/cli/commands/validate-spec-tree.mjs +27 -0
  38. package/cli/commands/validate-worker-output.mjs +5 -0
  39. package/cli/constants.mjs +489 -0
  40. package/cli/help.mjs +134 -0
  41. package/cli/index.mjs +103 -0
  42. package/cli/lib/adapter-profiles.mjs +403 -0
  43. package/cli/lib/audit-execution.mjs +52 -0
  44. package/cli/lib/audit-sweep-runtime/admissions.mjs +381 -0
  45. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +333 -0
  46. package/cli/lib/audit-sweep-runtime/chunks.mjs +697 -0
  47. package/cli/lib/audit-sweep-runtime/closeout.mjs +144 -0
  48. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +639 -0
  49. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +515 -0
  50. package/cli/lib/audit-sweep-runtime/common.mjs +329 -0
  51. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +172 -0
  52. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +152 -0
  53. package/cli/lib/audit-sweep-runtime/format.mjs +57 -0
  54. package/cli/lib/audit-sweep-runtime/ingest.mjs +486 -0
  55. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +198 -0
  56. package/cli/lib/audit-sweep-runtime/inventory.mjs +728 -0
  57. package/cli/lib/audit-sweep-runtime/ledger.mjs +315 -0
  58. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +101 -0
  59. package/cli/lib/audit-sweep-runtime/remediation.mjs +349 -0
  60. package/cli/lib/audit-sweep-runtime/rerun.mjs +129 -0
  61. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +300 -0
  62. package/cli/lib/audit-sweep-runtime/status.mjs +62 -0
  63. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +215 -0
  64. package/cli/lib/audit-sweep-runtime/validators.mjs +758 -0
  65. package/cli/lib/audit-sweep.mjs +18 -0
  66. package/cli/lib/authority-convergence.mjs +309 -0
  67. package/cli/lib/blueprint-audit.mjs +370 -0
  68. package/cli/lib/bootstrap.mjs +228 -0
  69. package/cli/lib/closeout.mjs +623 -0
  70. package/cli/lib/codex-sdk-runner.mjs +76 -0
  71. package/cli/lib/contracts.mjs +180 -0
  72. package/cli/lib/doctor.mjs +18 -0
  73. package/cli/lib/entrypoints.mjs +274 -0
  74. package/cli/lib/external-execution.mjs +101 -0
  75. package/cli/lib/fs-helpers.mjs +33 -0
  76. package/cli/lib/handoff.mjs +785 -0
  77. package/cli/lib/high-risk-admission.mjs +442 -0
  78. package/cli/lib/high-risk-decision.mjs +324 -0
  79. package/cli/lib/high-risk-ingest.mjs +317 -0
  80. package/cli/lib/high-risk-review.mjs +263 -0
  81. package/cli/lib/internal/contracts-loaders.mjs +132 -0
  82. package/cli/lib/internal/contracts-parse-high-risk.mjs +131 -0
  83. package/cli/lib/internal/contracts-parse.mjs +457 -0
  84. package/cli/lib/internal/contracts-validators.mjs +398 -0
  85. package/cli/lib/internal/doctor-bootstrap-surface.mjs +359 -0
  86. package/cli/lib/internal/doctor-delegated-surface.mjs +256 -0
  87. package/cli/lib/internal/doctor-finalize.mjs +385 -0
  88. package/cli/lib/internal/doctor-format.mjs +286 -0
  89. package/cli/lib/internal/doctor-inspectors.mjs +294 -0
  90. package/cli/lib/internal/doctor-state.mjs +205 -0
  91. package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +315 -0
  92. package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +358 -0
  93. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +155 -0
  94. package/cli/lib/internal/governance/ai/check-high-risk-doc-metadata-core.mjs +173 -0
  95. package/cli/lib/internal/governance/config.mjs +150 -0
  96. package/cli/lib/internal/governance/runner.mjs +35 -0
  97. package/cli/lib/internal/governance/shared/read-yaml-with-fragments.mjs +49 -0
  98. package/cli/lib/internal/validators-artifacts.mjs +515 -0
  99. package/cli/lib/internal/validators-shared.mjs +28 -0
  100. package/cli/lib/internal/validators-spec-helpers.mjs +186 -0
  101. package/cli/lib/internal/validators-spec.mjs +410 -0
  102. package/cli/lib/shared.mjs +83 -0
  103. package/cli/lib/topic-draft-packets.mjs +48 -0
  104. package/cli/lib/topic-goal.mjs +361 -0
  105. package/cli/lib/topic-runner.mjs +772 -0
  106. package/cli/lib/topic.mjs +93 -0
  107. package/cli/lib/ui.mjs +178 -0
  108. package/cli/lib/validators.mjs +78 -0
  109. package/cli/lib/value-helpers.mjs +24 -0
  110. package/cli/lib/yaml-helpers.mjs +133 -0
  111. package/cli/nimicoding.mjs +1 -0
  112. package/cli/seeds/bootstrap.mjs +47 -0
  113. package/config/audit-execution-artifacts.yaml +20 -0
  114. package/config/bootstrap.yaml +6 -0
  115. package/config/external-execution-artifacts.yaml +16 -0
  116. package/config/host-adapter.yaml +30 -0
  117. package/config/host-profile.yaml +29 -0
  118. package/config/installer-evidence.yaml +31 -0
  119. package/config/skill-installer.yaml +23 -0
  120. package/config/skill-manifest.yaml +46 -0
  121. package/config/skills.yaml +30 -0
  122. package/config/spec-generation-inputs.yaml +25 -0
  123. package/contracts/acceptance.schema.yaml +16 -0
  124. package/contracts/admission-checklist.schema.yaml +15 -0
  125. package/contracts/audit-chunk.schema.yaml +110 -0
  126. package/contracts/audit-closeout.schema.yaml +51 -0
  127. package/contracts/audit-finding.schema.yaml +61 -0
  128. package/contracts/audit-ledger.schema.yaml +138 -0
  129. package/contracts/audit-plan.schema.yaml +123 -0
  130. package/contracts/audit-remediation-map.schema.yaml +51 -0
  131. package/contracts/audit-rerun.schema.yaml +31 -0
  132. package/contracts/audit-sweep-result.yaml +49 -0
  133. package/contracts/authority-convergence-audit.schema.yaml +19 -0
  134. package/contracts/closeout.schema.yaml +25 -0
  135. package/contracts/decision-review.schema.yaml +16 -0
  136. package/contracts/doc-spec-audit-result.yaml +19 -0
  137. package/contracts/execution-packet.schema.yaml +49 -0
  138. package/contracts/external-host-compatibility.yaml +22 -0
  139. package/contracts/forbidden-shortcuts.catalog.yaml +23 -0
  140. package/contracts/high-risk-admission.schema.yaml +23 -0
  141. package/contracts/high-risk-execution-result.yaml +20 -0
  142. package/contracts/orchestration-state.schema.yaml +41 -0
  143. package/contracts/overflow-continuation.schema.yaml +12 -0
  144. package/contracts/packet.schema.yaml +30 -0
  145. package/contracts/pending-note.schema.yaml +17 -0
  146. package/contracts/prompt.schema.yaml +12 -0
  147. package/contracts/remediation.schema.yaml +16 -0
  148. package/contracts/result.schema.yaml +24 -0
  149. package/contracts/spec-generation-audit.schema.yaml +31 -0
  150. package/contracts/spec-generation-inputs.schema.yaml +39 -0
  151. package/contracts/spec-reconstruction-result.yaml +37 -0
  152. package/contracts/topic-goal.schema.yaml +78 -0
  153. package/contracts/topic-run-ledger.schema.yaml +72 -0
  154. package/contracts/topic-step-decision.schema.yaml +45 -0
  155. package/contracts/topic.schema.yaml +65 -0
  156. package/contracts/true-close.schema.yaml +15 -0
  157. package/contracts/wave.schema.yaml +29 -0
  158. package/contracts/worker-output.schema.yaml +15 -0
  159. package/methodology/audit-sweep-p0p1-recall.yaml +45 -0
  160. package/methodology/authority-convergence-policy.yaml +42 -0
  161. package/methodology/core.yaml +25 -0
  162. package/methodology/four-closure-policy.yaml +28 -0
  163. package/methodology/overflow-continuation-policy.yaml +14 -0
  164. package/methodology/role-separation-policy.yaml +28 -0
  165. package/methodology/skill-exchange-projection.yaml +114 -0
  166. package/methodology/skill-handoff.yaml +34 -0
  167. package/methodology/skill-installer-result.yaml +27 -0
  168. package/methodology/skill-installer-summary-projection.yaml +181 -0
  169. package/methodology/skill-runtime.yaml +23 -0
  170. package/methodology/spec-reconstruction.yaml +63 -0
  171. package/methodology/spec-target-truth-profile.yaml +53 -0
  172. package/methodology/topic-lifecycle-report.yaml +144 -0
  173. package/methodology/topic-lifecycle.yaml +37 -0
  174. package/methodology/topic-naming-ontology.yaml +21 -0
  175. package/methodology/topic-ontology.yaml +38 -0
  176. package/methodology/topic-validation-policy.yaml +9 -0
  177. package/methodology/wave-dag-policy.yaml +14 -0
  178. package/package.json +50 -0
  179. package/spec/_meta/command-gating-matrix.yaml +110 -0
  180. package/spec/_meta/generate-drift-migration-checklist.yaml +155 -0
  181. package/spec/_meta/governance-routing-cutover-checklist.yaml +35 -0
  182. package/spec/_meta/phase2-impacted-surface-matrix.yaml +44 -0
  183. package/spec/_meta/spec-authority-cutover-readiness.yaml +104 -0
  184. package/spec/_meta/spec-tree-model.yaml +72 -0
  185. package/spec/bootstrap-state.yaml +99 -0
  186. package/spec/product-scope.yaml +56 -0
@@ -0,0 +1,333 @@
1
+ import { criteriaEnableP0P1Recall } from "./p0p1-profile.mjs";
2
+
3
+ function diagnostic(id, message, details = {}) {
4
+ return { id, message, ...details };
5
+ }
6
+
7
+ function normalizeRefs(refs) {
8
+ return Array.isArray(refs) ? refs.map((ref) => typeof ref === "string" ? ref.replace(/\\/g, "/") : ref) : [];
9
+ }
10
+
11
+ function normalizeFileRef(value) {
12
+ return typeof value === "string" ? value.replace(/\\/g, "/") : null;
13
+ }
14
+
15
+ const REQUIRED_P0P1_RULE_CHECK_IDS = [
16
+ "fail_open_or_pseudo_success",
17
+ "partial_coverage_misrepresented_as_complete",
18
+ "authority_boundary_or_private_import_bypass",
19
+ "permission_or_capability_bypass",
20
+ "ungated_destructive_action",
21
+ "provider_or_model_hardcoding",
22
+ "app_local_shadow_truth",
23
+ ];
24
+
25
+ function validateP0P1RuleChecks(evidence, implementationRefSet) {
26
+ const ruleChecks = evidence?.coverage?.p0p1_rule_checks;
27
+ if (!Array.isArray(ruleChecks) || ruleChecks.length === 0) {
28
+ return {
29
+ ok: false,
30
+ missing: REQUIRED_P0P1_RULE_CHECK_IDS,
31
+ invalid: [],
32
+ checkedIds: [],
33
+ };
34
+ }
35
+
36
+ const checkedIds = [];
37
+ const invalid = [];
38
+ for (const [index, check] of ruleChecks.entries()) {
39
+ const id = typeof check?.id === "string" ? check.id : "";
40
+ if (id) {
41
+ checkedIds.push(id);
42
+ }
43
+ const status = check?.status;
44
+ if (!["checked", "not_applicable"].includes(status)) {
45
+ invalid.push({ index, id, reason: "status must be checked or not_applicable" });
46
+ continue;
47
+ }
48
+ const reasoning = typeof check?.negative_reasoning === "string" && check.negative_reasoning.trim().length > 0;
49
+ if (!reasoning) {
50
+ invalid.push({ index, id, reason: "negative_reasoning is required" });
51
+ }
52
+ const refs = normalizeRefs(check?.implementation_refs);
53
+ if (status === "checked" && refs.length === 0) {
54
+ invalid.push({ index, id, reason: "checked rule must cite implementation_refs" });
55
+ }
56
+ const outOfScopeRefs = refs.filter((ref) => !implementationRefSet.has(ref));
57
+ if (outOfScopeRefs.length > 0) {
58
+ invalid.push({ index, id, reason: "implementation_refs must belong to chunk implementation surface", invalid_refs: outOfScopeRefs });
59
+ }
60
+ }
61
+
62
+ const checkedIdSet = new Set(checkedIds);
63
+ const missing = REQUIRED_P0P1_RULE_CHECK_IDS.filter((id) => !checkedIdSet.has(id));
64
+ return {
65
+ ok: missing.length === 0 && invalid.length === 0,
66
+ missing,
67
+ invalid,
68
+ checkedIds,
69
+ };
70
+ }
71
+
72
+ function hasSemanticAuditorProvenance(evidence) {
73
+ const provenance = evidence?.auditor?.provenance;
74
+ if (!provenance || typeof provenance !== "object") {
75
+ return false;
76
+ }
77
+ const kind = typeof provenance.kind === "string" ? provenance.kind : "";
78
+ const packetRef = typeof provenance.packet_ref === "string" && provenance.packet_ref.trim().length > 0;
79
+ const semanticTrace = [
80
+ provenance.session_ref,
81
+ provenance.transcript_ref,
82
+ provenance.review_ref,
83
+ ].some((ref) => typeof ref === "string" && ref.trim().length > 0);
84
+ return kind === "semantic_audit" && packetRef && semanticTrace;
85
+ }
86
+
87
+ function looksSyntheticNoFindingEvidence(evidence) {
88
+ const auditorMode = typeof evidence?.auditor?.mode === "string" ? evidence.auditor.mode : "";
89
+ const generatedByScript = evidence?.auditor?.generated_by_script === true;
90
+ const negativeReasoning = typeof evidence?.coverage?.p0p1_negative_reasoning === "string"
91
+ ? evidence.coverage.p0p1_negative_reasoning
92
+ : "";
93
+ const templatedFragments = [
94
+ "reading the complete chunk corpus through the local audit cache",
95
+ "Lower-severity cleanup was intentionally summarized so the sweep preserved P0/P1 recall focus",
96
+ "No critical/high fail-open, authority-boundary bypass, unadmitted truth promotion, partial coverage success",
97
+ ];
98
+ return generatedByScript
99
+ || auditorMode.includes("script")
100
+ || auditorMode.includes("local_full_sweep")
101
+ || templatedFragments.some((fragment) => negativeReasoning.includes(fragment));
102
+ }
103
+
104
+ export function p0p1ImplementationRefsForChunk(chunk) {
105
+ if (chunk?.planning_basis === "spec_authority") {
106
+ return normalizeRefs(chunk?.evidence_inventory);
107
+ }
108
+ return normalizeRefs(chunk?.files);
109
+ }
110
+
111
+ function expectedDefectMatchesFinding(expected, finding) {
112
+ if (typeof expected?.root_cause_key === "string") {
113
+ const findingRootCauseKey = finding?.root_cause?.key ?? finding?.root_cause_key ?? null;
114
+ if (findingRootCauseKey !== expected.root_cause_key) {
115
+ return false;
116
+ }
117
+ }
118
+ if (typeof expected?.location_file === "string") {
119
+ if (normalizeFileRef(finding?.location?.file) !== normalizeFileRef(expected.location_file)) {
120
+ return false;
121
+ }
122
+ }
123
+ if (typeof expected?.severity === "string" && finding?.severity !== expected.severity) {
124
+ return false;
125
+ }
126
+ if (typeof expected?.category === "string" && finding?.category !== expected.category) {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+
132
+ function findMissedCalibrationDefects(chunk, findings) {
133
+ const expectedDefects = Array.isArray(chunk?.calibration_expected_defects)
134
+ ? chunk.calibration_expected_defects.filter((entry) => typeof entry?.id === "string" && entry.id.trim().length > 0)
135
+ : [];
136
+ const missed = expectedDefects.filter((expected) => !findings.some((finding) => expectedDefectMatchesFinding(expected, finding)));
137
+ return { expectedDefects, missed };
138
+ }
139
+
140
+ export function buildAuditValidityForEvidence(chunk, evidence) {
141
+ const warnings = [];
142
+ const blockers = [];
143
+ const outcomes = Array.isArray(evidence?.coverage?.authority_outcomes) ? evidence.coverage.authority_outcomes : [];
144
+ const findings = Array.isArray(evidence?.findings) ? evidence.findings : [];
145
+ const evidenceInventory = Array.isArray(chunk?.evidence_inventory) ? chunk.evidence_inventory : [];
146
+ const p0p1ImplementationRefs = p0p1ImplementationRefsForChunk(chunk);
147
+ const evidenceInventorySet = new Set(evidenceInventory);
148
+ const p0p1ImplementationRefSet = new Set(p0p1ImplementationRefs);
149
+ const hasImplementationInventory = evidenceInventory.length > 0;
150
+ const findingsEmpty = findings.length === 0;
151
+ const p0p1RecallRequired = criteriaEnableP0P1Recall(chunk?.criteria);
152
+ const hasP0P1Finding = findings.some((finding) => ["critical", "high"].includes(finding?.severity));
153
+ let auditedWithImplementationEvidenceRefs = 0;
154
+ let auditedWithoutImplementationEvidenceRefs = 0;
155
+ let missingNegativeReasoning = 0;
156
+ let authorityOnlyAuditedOutcomes = 0;
157
+
158
+ for (const outcome of outcomes.filter((entry) => entry?.status === "audited")) {
159
+ const explicitImplementationRefs = normalizeRefs(outcome.implementation_evidence_refs);
160
+ const implementationRefs = explicitImplementationRefs.filter((ref) => evidenceInventorySet.has(ref));
161
+ const notApplicableReason = typeof outcome.implementation_not_applicable_reason === "string"
162
+ && outcome.implementation_not_applicable_reason.trim().length > 0;
163
+ const evidenceRefs = normalizeRefs(outcome.evidence_refs);
164
+ const evidenceRefsIncludeImplementation = evidenceRefs.some((ref) => evidenceInventorySet.has(ref));
165
+ const negativeReasoning = typeof outcome.negative_reasoning === "string"
166
+ && outcome.negative_reasoning.trim().length > 0;
167
+
168
+ if (implementationRefs.length > 0) {
169
+ auditedWithImplementationEvidenceRefs += 1;
170
+ } else {
171
+ auditedWithoutImplementationEvidenceRefs += 1;
172
+ }
173
+
174
+ if (hasImplementationInventory && !evidenceRefsIncludeImplementation && implementationRefs.length === 0 && !notApplicableReason) {
175
+ authorityOnlyAuditedOutcomes += 1;
176
+ }
177
+ if (findingsEmpty && !negativeReasoning) {
178
+ missingNegativeReasoning += 1;
179
+ }
180
+ }
181
+
182
+ if (findingsEmpty && hasImplementationInventory && authorityOnlyAuditedOutcomes > 0) {
183
+ blockers.push(diagnostic(
184
+ "audited_outcome_authority_only_evidence_refs",
185
+ "Audited no-finding outcomes cite only authority refs while implementation evidence inventory exists.",
186
+ { outcome_count: authorityOnlyAuditedOutcomes },
187
+ ));
188
+ blockers.push(diagnostic(
189
+ "no_finding_evidence_invalid",
190
+ "No-finding evidence cannot prove that declared implementation evidence was reviewed.",
191
+ ));
192
+ }
193
+
194
+ if (findingsEmpty && hasImplementationInventory && missingNegativeReasoning > 0) {
195
+ blockers.push(diagnostic(
196
+ "no_finding_negative_reasoning_missing",
197
+ "No-finding evidence lacks per-outcome negative implementation reasoning.",
198
+ { outcome_count: missingNegativeReasoning },
199
+ ));
200
+ }
201
+
202
+ if (findingsEmpty && !hasImplementationInventory) {
203
+ warnings.push(diagnostic(
204
+ "empty_inventory_no_finding_weak",
205
+ "Empty-inventory no-finding evidence is weak unless the empty-evidence reason and negative reasoning support it.",
206
+ ));
207
+ }
208
+
209
+ if (p0p1RecallRequired && !hasP0P1Finding) {
210
+ const p0p1NegativeReasoning = typeof evidence?.coverage?.p0p1_negative_reasoning === "string"
211
+ && evidence.coverage.p0p1_negative_reasoning.trim().length > 0;
212
+ const p0p1EvidenceRefs = normalizeRefs(evidence?.coverage?.p0p1_evidence_refs);
213
+ const p0p1ImplementationNotApplicable = !hasImplementationInventory
214
+ && typeof evidence?.coverage?.p0p1_implementation_not_applicable_reason === "string"
215
+ && evidence.coverage.p0p1_implementation_not_applicable_reason.trim().length > 0;
216
+ const invalidP0P1EvidenceRefs = p0p1EvidenceRefs.filter((ref) => !p0p1ImplementationRefSet.has(ref));
217
+ const hasP0P1ImplementationRef = p0p1EvidenceRefs.length > 0 && invalidP0P1EvidenceRefs.length === 0;
218
+ const syntheticNoFindingEvidence = looksSyntheticNoFindingEvidence(evidence);
219
+ if (invalidP0P1EvidenceRefs.length > 0) {
220
+ blockers.push(diagnostic(
221
+ "p0p1_evidence_refs_out_of_scope",
222
+ "P0/P1 evidence refs must all belong to the chunk implementation surface.",
223
+ { invalid_refs: invalidP0P1EvidenceRefs },
224
+ ));
225
+ }
226
+ if (!p0p1NegativeReasoning || (!hasP0P1ImplementationRef && !p0p1ImplementationNotApplicable)) {
227
+ blockers.push(diagnostic(
228
+ "p0p1_negative_reasoning_missing",
229
+ "P0/P1 recall evidence without critical/high findings must include P0/P1 negative reasoning and implementation evidence refs, or an explicit not-applicable reason when no implementation inventory exists.",
230
+ {
231
+ p0p1_negative_reasoning_present: p0p1NegativeReasoning,
232
+ p0p1_implementation_evidence_refs_present: hasP0P1ImplementationRef,
233
+ p0p1_implementation_not_applicable_reason_present: p0p1ImplementationNotApplicable,
234
+ },
235
+ ));
236
+ }
237
+ if (syntheticNoFindingEvidence) {
238
+ blockers.push(diagnostic(
239
+ "synthetic_no_finding_evidence",
240
+ "P0/P1 no-finding evidence appears to be generated by a script or bulk template rather than a semantic audit.",
241
+ ));
242
+ }
243
+ if (hasImplementationInventory) {
244
+ if (!hasSemanticAuditorProvenance(evidence)) {
245
+ blockers.push(diagnostic(
246
+ "auditor_provenance_missing",
247
+ "P0/P1 no-finding evidence with implementation inventory must cite semantic auditor provenance.",
248
+ {
249
+ required_kind: "semantic_audit",
250
+ required_fields: ["auditor.provenance.packet_ref", "auditor.provenance.session_ref|transcript_ref|review_ref"],
251
+ },
252
+ ));
253
+ }
254
+ const ruleCheckValidity = validateP0P1RuleChecks(evidence, p0p1ImplementationRefSet);
255
+ if (!ruleCheckValidity.ok) {
256
+ blockers.push(diagnostic(
257
+ "p0p1_rule_checks_missing_or_invalid",
258
+ "P0/P1 no-finding evidence with implementation inventory must record rule-specific checks over the chunk implementation surface.",
259
+ {
260
+ missing_rule_check_ids: ruleCheckValidity.missing,
261
+ invalid_rule_checks: ruleCheckValidity.invalid,
262
+ present_rule_check_ids: ruleCheckValidity.checkedIds,
263
+ },
264
+ ));
265
+ }
266
+ }
267
+ }
268
+
269
+ const calibration = findMissedCalibrationDefects(chunk, findings);
270
+ if (calibration.missed.length > 0) {
271
+ blockers.push(diagnostic(
272
+ "calibration_known_defect_missed",
273
+ "Calibration evidence missed one or more expected known defects.",
274
+ {
275
+ expected_defect_count: calibration.expectedDefects.length,
276
+ missed_defect_ids: calibration.missed.map((defect) => defect.id),
277
+ },
278
+ ));
279
+ }
280
+
281
+ return {
282
+ posture: blockers.length > 0 ? "invalid" : warnings.length > 0 ? "warning" : "trusted",
283
+ no_finding_posture: findings.length > 0 ? "not_applicable" : blockers.length > 0 ? "invalid" : warnings.length > 0 ? "weak" : "explained",
284
+ audited_outcomes_with_implementation_evidence_refs: auditedWithImplementationEvidenceRefs,
285
+ audited_outcomes_without_implementation_evidence_refs: auditedWithoutImplementationEvidenceRefs,
286
+ zero_finding_chunk_count: findingsEmpty ? 1 : 0,
287
+ large_zero_finding_chunk_count: findingsEmpty && evidenceInventory.length >= 10 ? 1 : 0,
288
+ negative_reasoning_present: !findingsEmpty || (outcomes.length > 0 && missingNegativeReasoning === 0),
289
+ p0p1_recall_required: p0p1RecallRequired,
290
+ p0p1_recall_posture: !p0p1RecallRequired ? "not_applicable" : hasP0P1Finding ? "p0p1_finding_present" : blockers.length > 0 ? "invalid" : "explained",
291
+ p0p1_rule_check_count: Array.isArray(evidence?.coverage?.p0p1_rule_checks) ? evidence.coverage.p0p1_rule_checks.length : 0,
292
+ auditor_provenance_present: !findingsEmpty || hasSemanticAuditorProvenance(evidence),
293
+ calibration_expected_defect_count: calibration.expectedDefects.length,
294
+ calibration_missed_defect_count: calibration.missed.length,
295
+ warnings,
296
+ blockers,
297
+ };
298
+ }
299
+
300
+ export function combineAuditValidity(entries) {
301
+ const validEntries = entries.filter(Boolean);
302
+ const warnings = validEntries.flatMap((entry) => entry.warnings ?? []);
303
+ const blockers = validEntries.flatMap((entry) => entry.blockers ?? []);
304
+ const zeroFindingChunkCount = validEntries.reduce((total, entry) => total + (entry.zero_finding_chunk_count ?? 0), 0);
305
+ const largeZeroFindingChunkCount = validEntries.reduce((total, entry) => total + (entry.large_zero_finding_chunk_count ?? 0), 0);
306
+ const withImplementation = validEntries.reduce((total, entry) => total + (entry.audited_outcomes_with_implementation_evidence_refs ?? 0), 0);
307
+ const withoutImplementation = validEntries.reduce((total, entry) => total + (entry.audited_outcomes_without_implementation_evidence_refs ?? 0), 0);
308
+ const calibrationExpectedDefectCount = validEntries.reduce((total, entry) => total + (entry.calibration_expected_defect_count ?? 0), 0);
309
+ const calibrationMissedDefectCount = validEntries.reduce((total, entry) => total + (entry.calibration_missed_defect_count ?? 0), 0);
310
+ const p0p1RecallRequiredCount = validEntries.reduce((total, entry) => total + (entry.p0p1_recall_required ? 1 : 0), 0);
311
+ const p0p1InvalidCount = validEntries.reduce((total, entry) => total + (entry.p0p1_recall_posture === "invalid" ? 1 : 0), 0);
312
+ const p0p1RuleCheckCount = validEntries.reduce((total, entry) => total + (entry.p0p1_rule_check_count ?? 0), 0);
313
+ const anyInvalid = validEntries.some((entry) => entry.posture === "invalid");
314
+ const anyWarning = validEntries.some((entry) => entry.posture === "warning");
315
+
316
+ return {
317
+ posture: anyInvalid ? "invalid" : anyWarning ? "warning" : "trusted",
318
+ no_finding_posture: anyInvalid ? "invalid" : anyWarning ? "weak" : zeroFindingChunkCount > 0 ? "explained" : "not_applicable",
319
+ audited_outcomes_with_implementation_evidence_refs: withImplementation,
320
+ audited_outcomes_without_implementation_evidence_refs: withoutImplementation,
321
+ zero_finding_chunk_count: zeroFindingChunkCount,
322
+ large_zero_finding_chunk_count: largeZeroFindingChunkCount,
323
+ negative_reasoning_present: validEntries.every((entry) => entry.negative_reasoning_present !== false),
324
+ p0p1_recall_required_count: p0p1RecallRequiredCount,
325
+ p0p1_recall_invalid_count: p0p1InvalidCount,
326
+ p0p1_rule_check_count: p0p1RuleCheckCount,
327
+ auditor_provenance_present: validEntries.every((entry) => entry.auditor_provenance_present !== false),
328
+ calibration_expected_defect_count: calibrationExpectedDefectCount,
329
+ calibration_missed_defect_count: calibrationMissedDefectCount,
330
+ warnings,
331
+ blockers,
332
+ };
333
+ }