@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,515 @@
1
+ import { spawn } from "node:child_process";
2
+ import { mkdir } from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ import {
6
+ appendRunEvent,
7
+ artifactPath,
8
+ artifactRef,
9
+ chunkRef,
10
+ ensureIsoTimestamp,
11
+ inputError,
12
+ loadChunk,
13
+ loadPlan,
14
+ packetRef,
15
+ resolveInsideProject,
16
+ safeSweepId,
17
+ withAuditSweepMutationLock,
18
+ writeYamlRef,
19
+ } from "./common.mjs";
20
+ import { buildAuditorPacket, reviewAuditSweepChunk, updatePlanChunk } from "./chunks.mjs";
21
+ import { extractCodexAuditorEvidenceFile, P0P1_RULE_CHECK_IDS } from "./codex-auditor-evidence.mjs";
22
+ import { ingestAuditSweepChunk } from "./ingest.mjs";
23
+ import { budgetBlockForChunk } from "./risk-budget.mjs";
24
+ import { validateAuditSweepArtifacts } from "./validators.mjs";
25
+
26
+ const CODEX_AUDITOR_DEFAULT = "codex_semantic_auditor";
27
+ const DEFAULT_CODEX_TIMEOUT_MS = 10 * 60 * 1000;
28
+ const CODEX_TIMEOUT_KILL_GRACE_MS = 3000;
29
+ const CODEX_RAW_SUFFIX = ".codex-raw.json";
30
+ const CODEX_EVIDENCE_SUFFIX = ".codex-evidence.json";
31
+
32
+ function codexOutputRef(sweepId, chunkId, suffix) {
33
+ return artifactRef("evidence_refs", sweepId, "codex-output", `${chunkId}${suffix}`);
34
+ }
35
+
36
+ function codexRunToken(timestamp) {
37
+ return timestamp.replace(/[^0-9A-Za-z]+/g, "-").replace(/^-+|-+$/g, "");
38
+ }
39
+
40
+ function projectRefForPath(projectRoot, absolutePath) {
41
+ return path.relative(projectRoot, absolutePath).replace(/\\/g, "/");
42
+ }
43
+ function codexPrompt({ packet, auditorPacketRef, rawRef, sessionRef }) {
44
+ return [
45
+ "You are the Codex semantic auditor for a nimicoding audit-sweep chunk.",
46
+ "Run in read-only, audit-only mode. Do not edit files. Do not implement product fixes.",
47
+ `Read the auditor packet from ${auditorPacketRef} and inspect the chunk authority refs and implementation evidence semantically.`,
48
+ "Do not rely on this prompt as the chunk inventory; the packet file is the source for files, authority_refs, selected_implementation_refs, audit_depth, retrieval_prepass, and the raw semantic output contract.",
49
+ "Scripts may not generate findings or no-findings; your conclusions must come from your own inspection.",
50
+ "The packet is compact: evidence_inventory/selected_implementation_refs is the manager-selected implementation slice, not the full manager-owned inventory.",
51
+ "Do not ask for, reconstruct, or echo the omitted full evidence_inventory. audit-codex will mechanically fill coverage.files, coverage.authority_refs, and full coverage.evidence_files from manager-owned chunk state.",
52
+ "You only author semantic audit content: authority_outcomes reasoning/status, inspected_implementation_refs, P0/P1 rule checks, p0p1_negative_reasoning when applicable, and findings.",
53
+ "For each authority outcome, set authority_ref to the packet authority_ref and put inspected implementation refs in inspected_implementation_refs or implementation_evidence_refs.",
54
+ "Every implementation ref you cite must be an exact file ref from packet.selected_implementation_refs / packet.evidence_inventory.",
55
+ "Never put AGENTS.md, README.md, spec files, authority refs, methodology docs, or governance docs in inspected_implementation_refs, implementation_evidence_refs, coverage.p0p1_evidence_refs, findings[].implementation_refs, or coverage.p0p1_rule_checks[].implementation_refs unless that exact file appears in packet.selected_implementation_refs.",
56
+ "If a governance or authority document influenced reasoning but is not in packet.selected_implementation_refs, mention it only in negative_reasoning/description text, not in any implementation_refs array.",
57
+ "Use packet.audit_depth to size your inspection: deep means inspect the selected slice thoroughly, normal means focused semantic inspection, shallow means audit generated/table/index invariants from the selected slice without expanding the omitted inventory.",
58
+ "Return exactly one JSON object and nothing else. Do not wrap it in markdown.",
59
+ "The JSON object must have exactly these top-level fields: chunk_id, auditor, coverage, findings.",
60
+ `Set auditor.id to ${JSON.stringify(packet.auditor)}.`,
61
+ `Set auditor.mode to "codex_semantic_audit".`,
62
+ `Set auditor.methodology_ref to "nimi-coding/methodology/audit-sweep-p0p1-recall.yaml".`,
63
+ "Put P0/P1 rule checks only at coverage.p0p1_rule_checks.",
64
+ `Set auditor.provenance.kind to "semantic_audit".`,
65
+ `Set auditor.provenance.packet_ref to ${JSON.stringify(packetRef(packet.sweep_id, packet.chunk_id))}.`,
66
+ `Set auditor.provenance.session_ref to ${JSON.stringify(sessionRef)}.`,
67
+ `Set auditor.provenance.transcript_ref to ${JSON.stringify(rawRef)}.`,
68
+ "coverage.authority_outcomes must contain one outcome per authority_ref.",
69
+ `coverage.p0p1_rule_checks must contain exactly these ids and no aliases: ${P0P1_RULE_CHECK_IDS.join(", ")}.`,
70
+ "Each coverage.authority_outcomes[] object must include negative_reasoning when no critical/high finding is emitted for the chunk.",
71
+ "Each coverage.p0p1_rule_checks[] object must include id, status, implementation_refs, and negative_reasoning.",
72
+ "Use status=\"checked\" when implementation evidence was inspected; checked rules must cite at least one in-scope implementation ref.",
73
+ "Use status=\"not_applicable\" only when the rule truly has no implementation surface, and explain that in negative_reasoning.",
74
+ "When the packet evidence_inventory is empty and no critical/high finding is emitted, include coverage.p0p1_implementation_not_applicable_reason with the chunk-specific reason implementation refs are not applicable.",
75
+ "Do not use priority defect class aliases such as authority_boundary_bypass, security_or_permission_bypass, destructive_action_without_gate, package_boundary_violation, or unadmitted_truth_or_evidence_source as rule check ids.",
76
+ "Do not emit coverage.files, coverage.authority_refs, or coverage.evidence_files; those fields are manager-owned and will be populated from the packet.",
77
+ "Do not emit authority_outcomes[].evidence_refs; it is manager-owned and will be built from authority_ref plus inspected implementation refs.",
78
+ "Every finding must include severity, category, impact, title, description, and location.file. Set severity to critical or high for P0/P1 findings. Set finding.category to one of the exact P0/P1 rule ids when the finding maps to a P0/P1 rule; do not use rule_id as the primary finding category field.",
79
+ "Set findings[].location.file to an exact packet.selected_implementation_refs file for implementation findings. For authority-only findings with no implementation surface, set findings[].location.file to the in-scope authority_ref that contains the defect.",
80
+ "authority_outcomes[].status is an audit-process enum only: audited, blocked, or not_applicable.",
81
+ "Use status=audited when the authority/evidence was inspected, even if you discovered violations.",
82
+ "When an authority outcome uses status=blocked or status=not_applicable, include reason with the chunk-specific blocker or not-applicable explanation.",
83
+ "Do not use compliance verdicts such as violated, pass, fail, compliant, or non_compliant in authority_outcomes[].status; put violations in findings.",
84
+ "For no-finding chunks, include chunk-specific inspected implementation refs, P0/P1 rule checks, and negative reasoning.",
85
+ ].join("\n");
86
+ }
87
+
88
+ function terminateProcess(child, signal) {
89
+ try {
90
+ if (process.platform !== "win32" && child.pid) {
91
+ process.kill(-child.pid, signal);
92
+ return;
93
+ }
94
+ } catch {
95
+ // Fall through to direct child termination.
96
+ }
97
+ try {
98
+ child.kill(signal);
99
+ } catch {
100
+ // Process may already have exited.
101
+ }
102
+ }
103
+
104
+ function runCodexExec({ projectRoot, codexBin, rawOutputPath, prompt, timeoutMs }) {
105
+ return new Promise((resolve) => {
106
+ const boundedTimeoutMs = Number.isInteger(timeoutMs) && timeoutMs > 0 ? timeoutMs : DEFAULT_CODEX_TIMEOUT_MS;
107
+ let timedOut = false;
108
+ let settled = false;
109
+ let killTimer = null;
110
+ const child = spawn(codexBin, [
111
+ "exec",
112
+ "-C",
113
+ projectRoot,
114
+ "-s",
115
+ "read-only",
116
+ "--output-last-message",
117
+ rawOutputPath,
118
+ "-",
119
+ ], {
120
+ cwd: projectRoot,
121
+ stdio: ["pipe", "pipe", "pipe"],
122
+ detached: process.platform !== "win32",
123
+ });
124
+
125
+ const timeoutTimer = setTimeout(() => {
126
+ timedOut = true;
127
+ terminateProcess(child, "SIGTERM");
128
+ killTimer = setTimeout(() => terminateProcess(child, "SIGKILL"), CODEX_TIMEOUT_KILL_GRACE_MS);
129
+ }, boundedTimeoutMs);
130
+
131
+ let stdout = "";
132
+ let stderr = "";
133
+ child.stdout.on("data", (chunk) => {
134
+ stdout += chunk.toString();
135
+ });
136
+ child.stderr.on("data", (chunk) => {
137
+ stderr += chunk.toString();
138
+ });
139
+ child.on("error", (error) => {
140
+ if (settled) {
141
+ return;
142
+ }
143
+ settled = true;
144
+ clearTimeout(timeoutTimer);
145
+ if (killTimer) {
146
+ clearTimeout(killTimer);
147
+ }
148
+ resolve({ ok: false, exitCode: 1, timedOut, timeoutMs: boundedTimeoutMs, stdout, stderr: `${stderr}${error.message}` });
149
+ });
150
+ child.on("close", (exitCode, signal) => {
151
+ if (settled) {
152
+ return;
153
+ }
154
+ settled = true;
155
+ clearTimeout(timeoutTimer);
156
+ if (killTimer) {
157
+ clearTimeout(killTimer);
158
+ }
159
+ resolve({ ok: exitCode === 0 && !timedOut, exitCode, signal, timedOut, timeoutMs: boundedTimeoutMs, stdout, stderr });
160
+ });
161
+ child.stdin.end(prompt);
162
+ });
163
+ }
164
+
165
+ async function prepareCodexAuditPacket(projectRoot, options) {
166
+ return withAuditSweepMutationLock(projectRoot, options.sweepId, "chunk codex audit prepare", async () => {
167
+ const planResult = await loadPlan(projectRoot, options.sweepId);
168
+ if (!planResult.ok) {
169
+ return inputError(planResult.error);
170
+ }
171
+ const chunkResult = await loadChunk(projectRoot, options.sweepId, options.chunkId);
172
+ if (!chunkResult.ok) {
173
+ return inputError(chunkResult.error);
174
+ }
175
+ if (chunkResult.chunk.state === "skipped") {
176
+ return inputError("nimicoding audit-sweep refused: skipped chunks cannot be audited through Codex.\n");
177
+ }
178
+ const budgetBlock = budgetBlockForChunk(planResult.plan, chunkResult.chunk);
179
+ if (budgetBlock && chunkResult.chunk.state !== "frozen") {
180
+ return inputError(`nimicoding audit-sweep refused: ${budgetBlock}; build or admit remediation bundles before continuing discovery.\n`);
181
+ }
182
+
183
+ const dispatch = {
184
+ auditor: options.auditor ?? CODEX_AUDITOR_DEFAULT,
185
+ criteria: chunkResult.chunk.criteria,
186
+ files: chunkResult.chunk.files,
187
+ authority_refs: chunkResult.chunk.authority_refs ?? chunkResult.chunk.files,
188
+ host_authority_projection_refs: chunkResult.chunk.host_authority_projection_refs ?? [],
189
+ evidence_roots: chunkResult.chunk.evidence_roots ?? [],
190
+ admitted_evidence_roots: chunkResult.chunk.admitted_evidence_roots ?? [],
191
+ evidence_inventory: chunkResult.chunk.evidence_inventory ?? [],
192
+ evidence_inventory_status: chunkResult.chunk.evidence_inventory_status ?? null,
193
+ evidence_inventory_empty_reason: chunkResult.chunk.evidence_inventory_empty_reason ?? null,
194
+ execution_owner: "nimicoding_codex_auditor_path",
195
+ };
196
+ const packet = buildAuditorPacket(options.sweepId, chunkResult.chunk, dispatch.auditor, options.dispatchedAt, planResult.plan, { projectRoot });
197
+ packet.execution_owner = "nimicoding_codex_auditor_path";
198
+ packet.raw_output_contract = {
199
+ raw_output_is_transcript_ref: true,
200
+ raw_output_must_be_exact_json: true,
201
+ schema_drift_rejected_fail_closed: true,
202
+ scripts_may_only_extract_schema_conformant_evidence: true,
203
+ };
204
+
205
+ const auditorPacketRef = packetRef(options.sweepId, options.chunkId);
206
+ const updatedChunk = {
207
+ ...chunkResult.chunk,
208
+ state: "dispatched",
209
+ lifecycle: {
210
+ ...chunkResult.chunk.lifecycle,
211
+ dispatched_at: options.dispatchedAt,
212
+ ingested_at: null,
213
+ reviewed_at: null,
214
+ frozen_at: null,
215
+ failed_at: null,
216
+ skipped_at: null,
217
+ },
218
+ dispatch,
219
+ evidence_ref: null,
220
+ finding_count: 0,
221
+ audit_validity: null,
222
+ review: null,
223
+ failure: null,
224
+ updated_at: options.dispatchedAt,
225
+ };
226
+
227
+ await writeYamlRef(projectRoot, auditorPacketRef, packet);
228
+ await writeYamlRef(projectRoot, chunkResult.chunkRef, updatedChunk);
229
+ await writeYamlRef(projectRoot, planResult.planRef, {
230
+ ...updatePlanChunk(planResult.plan, options.chunkId, {
231
+ state: "dispatched",
232
+ evidence_ref: null,
233
+ finding_count: 0,
234
+ audit_validity: null,
235
+ failure: null,
236
+ }),
237
+ updated_at: options.dispatchedAt,
238
+ });
239
+ const runLedgerRef = await appendRunEvent(projectRoot, options.sweepId, {
240
+ event_type: "chunk_codex_audit_prepared",
241
+ chunk_id: options.chunkId,
242
+ chunk_ref: chunkRef(options.sweepId, options.chunkId),
243
+ packet_ref: auditorPacketRef,
244
+ auditor: dispatch.auditor,
245
+ });
246
+ return {
247
+ ok: true,
248
+ chunk: updatedChunk,
249
+ packet,
250
+ packetRef: auditorPacketRef,
251
+ chunkRef: chunkResult.chunkRef,
252
+ runLedgerRef,
253
+ };
254
+ });
255
+ }
256
+
257
+ async function markCodexAuditFailed(projectRoot, options) {
258
+ return withAuditSweepMutationLock(projectRoot, options.sweepId, "chunk codex audit fail", async () => {
259
+ const planResult = await loadPlan(projectRoot, options.sweepId);
260
+ if (!planResult.ok) {
261
+ return inputError(planResult.error);
262
+ }
263
+ const chunkResult = await loadChunk(projectRoot, options.sweepId, options.chunkId);
264
+ if (!chunkResult.ok) {
265
+ return inputError(chunkResult.error);
266
+ }
267
+ const failure = {
268
+ reason: options.reason,
269
+ failed_at: options.failedAt,
270
+ packet_ref: options.packetRef,
271
+ transcript_ref: options.transcriptRef,
272
+ phase: options.phase,
273
+ };
274
+ const updatedChunk = {
275
+ ...chunkResult.chunk,
276
+ state: "failed",
277
+ lifecycle: {
278
+ ...chunkResult.chunk.lifecycle,
279
+ failed_at: options.failedAt,
280
+ skipped_at: null,
281
+ },
282
+ failure,
283
+ updated_at: options.failedAt,
284
+ };
285
+ await writeYamlRef(projectRoot, chunkResult.chunkRef, updatedChunk);
286
+ await writeYamlRef(projectRoot, planResult.planRef, {
287
+ ...updatePlanChunk(planResult.plan, options.chunkId, {
288
+ state: "failed",
289
+ failure,
290
+ }),
291
+ updated_at: options.failedAt,
292
+ });
293
+ const runLedgerRef = await appendRunEvent(projectRoot, options.sweepId, {
294
+ event_type: "chunk_failed",
295
+ chunk_id: options.chunkId,
296
+ chunk_ref: chunkResult.chunkRef,
297
+ packet_ref: options.packetRef,
298
+ transcript_ref: options.transcriptRef,
299
+ summary: options.reason,
300
+ phase: options.phase,
301
+ });
302
+ return {
303
+ ok: true,
304
+ state: "failed",
305
+ chunkRef: chunkResult.chunkRef,
306
+ runLedgerRef,
307
+ };
308
+ });
309
+ }
310
+
311
+ export async function runCodexAuditSweepChunk(projectRoot, options) {
312
+ const sweepId = safeSweepId(options.sweepId);
313
+ if (!sweepId || typeof options.chunkId !== "string") {
314
+ return inputError("nimicoding audit-sweep refused: --sweep-id and --chunk-id are required.\n");
315
+ }
316
+ const dispatchedAtError = ensureIsoTimestamp(options.dispatchedAt, "--dispatched-at");
317
+ if (dispatchedAtError) {
318
+ return dispatchedAtError;
319
+ }
320
+ const verifiedAtError = ensureIsoTimestamp(options.verifiedAt, "--verified-at");
321
+ if (verifiedAtError) {
322
+ return verifiedAtError;
323
+ }
324
+ const reviewedAtError = ensureIsoTimestamp(options.reviewedAt, "--reviewed-at");
325
+ if (reviewedAtError) {
326
+ return reviewedAtError;
327
+ }
328
+
329
+ const prepare = await prepareCodexAuditPacket(projectRoot, {
330
+ ...options,
331
+ sweepId,
332
+ });
333
+ if (!prepare.ok) {
334
+ return prepare;
335
+ }
336
+
337
+ const outputSuffix = `.${codexRunToken(options.dispatchedAt)}`;
338
+ let rawRef = codexOutputRef(sweepId, options.chunkId, `${outputSuffix}${CODEX_RAW_SUFFIX}`);
339
+ const evidenceCandidateRef = codexOutputRef(sweepId, options.chunkId, `${outputSuffix}${CODEX_EVIDENCE_SUFFIX}`);
340
+ let rawOutputPath = artifactPath(projectRoot, rawRef);
341
+ let sessionRef = `codex-exec:${sweepId}:${options.chunkId}:${options.dispatchedAt}`;
342
+ if (options.fromRawOutput) {
343
+ const replaySource = resolveInsideProject(projectRoot, options.fromRawOutput, "--from-raw-output");
344
+ if (!replaySource.ok) {
345
+ await markCodexAuditFailed(projectRoot, {
346
+ sweepId,
347
+ chunkId: options.chunkId,
348
+ failedAt: options.verifiedAt,
349
+ packetRef: prepare.packetRef,
350
+ transcriptRef: rawRef,
351
+ phase: "raw_output_replay",
352
+ reason: replaySource.error.trim(),
353
+ });
354
+ return inputError(replaySource.error);
355
+ }
356
+ rawOutputPath = replaySource.absolutePath;
357
+ rawRef = projectRefForPath(projectRoot, rawOutputPath);
358
+ sessionRef = `codex-replay:${sweepId}:${options.chunkId}:${options.dispatchedAt}`;
359
+ } else {
360
+ await mkdir(path.dirname(rawOutputPath), { recursive: true });
361
+ const runResult = await runCodexExec({
362
+ projectRoot,
363
+ codexBin: options.codexBin ?? "codex",
364
+ rawOutputPath,
365
+ prompt: codexPrompt({
366
+ packet: prepare.packet,
367
+ auditorPacketRef: prepare.packetRef,
368
+ rawRef,
369
+ sessionRef,
370
+ }),
371
+ timeoutMs: options.timeoutMs,
372
+ });
373
+ if (!runResult.ok) {
374
+ const failureReason = runResult.timedOut
375
+ ? `Codex auditor execution timed out after ${runResult.timeoutMs}ms.`
376
+ : `Codex auditor execution failed with exit code ${runResult.exitCode ?? "unknown"}.`;
377
+ await markCodexAuditFailed(projectRoot, {
378
+ sweepId,
379
+ chunkId: options.chunkId,
380
+ failedAt: options.verifiedAt,
381
+ packetRef: prepare.packetRef,
382
+ transcriptRef: rawRef,
383
+ phase: "codex_execution",
384
+ reason: failureReason,
385
+ });
386
+ await appendRunEvent(projectRoot, sweepId, {
387
+ event_type: "chunk_codex_audit_failed",
388
+ chunk_id: options.chunkId,
389
+ chunk_ref: prepare.chunkRef,
390
+ packet_ref: prepare.packetRef,
391
+ transcript_ref: rawRef,
392
+ exit_code: runResult.exitCode,
393
+ timed_out: runResult.timedOut,
394
+ timeout_ms: runResult.timeoutMs,
395
+ stderr_tail: runResult.stderr.slice(-2000),
396
+ });
397
+ return inputError(`nimicoding audit-sweep refused: ${failureReason}\n`);
398
+ }
399
+ }
400
+
401
+ const extracted = await extractCodexAuditorEvidenceFile(projectRoot, {
402
+ rawOutputPath,
403
+ evidenceRef: evidenceCandidateRef,
404
+ chunk: prepare.chunk,
405
+ packetRef: prepare.packetRef,
406
+ sessionRef,
407
+ transcriptRef: rawRef,
408
+ auditorId: options.auditor ?? CODEX_AUDITOR_DEFAULT,
409
+ });
410
+ if (!extracted.ok) {
411
+ await markCodexAuditFailed(projectRoot, {
412
+ sweepId,
413
+ chunkId: options.chunkId,
414
+ failedAt: options.verifiedAt,
415
+ packetRef: prepare.packetRef,
416
+ transcriptRef: rawRef,
417
+ phase: "auditor_output_validation",
418
+ reason: `Codex auditor output rejected: ${extracted.error}.`,
419
+ });
420
+ await appendRunEvent(projectRoot, sweepId, {
421
+ event_type: "chunk_codex_auditor_output_rejected",
422
+ chunk_id: options.chunkId,
423
+ chunk_ref: prepare.chunkRef,
424
+ packet_ref: prepare.packetRef,
425
+ transcript_ref: rawRef,
426
+ reason: extracted.error,
427
+ });
428
+ return inputError(`nimicoding audit-sweep refused: Codex auditor output rejected for ${options.chunkId}: ${extracted.error}.\n`);
429
+ }
430
+
431
+ await appendRunEvent(projectRoot, sweepId, {
432
+ event_type: "chunk_codex_auditor_output_accepted",
433
+ chunk_id: options.chunkId,
434
+ chunk_ref: prepare.chunkRef,
435
+ packet_ref: prepare.packetRef,
436
+ transcript_ref: rawRef,
437
+ evidence_candidate_ref: evidenceCandidateRef,
438
+ audit_validity: extracted.auditValidity,
439
+ });
440
+
441
+ const ingest = await ingestAuditSweepChunk(projectRoot, {
442
+ sweepId,
443
+ chunkId: options.chunkId,
444
+ fromPath: evidenceCandidateRef,
445
+ verifiedAt: options.verifiedAt,
446
+ });
447
+ if (!ingest.ok) {
448
+ await markCodexAuditFailed(projectRoot, {
449
+ sweepId,
450
+ chunkId: options.chunkId,
451
+ failedAt: options.verifiedAt,
452
+ packetRef: prepare.packetRef,
453
+ transcriptRef: rawRef,
454
+ phase: "chunk_ingest",
455
+ reason: `Codex auditor evidence ingest rejected: ${ingest.error ?? "unknown ingest failure"}.`,
456
+ });
457
+ return inputError(`nimicoding audit-sweep refused: Codex auditor evidence ingest rejected for ${options.chunkId}: ${ingest.error ?? "unknown ingest failure"}.\n`);
458
+ }
459
+
460
+ const review = await reviewAuditSweepChunk(projectRoot, {
461
+ sweepId,
462
+ chunkId: options.chunkId,
463
+ verdict: "pass",
464
+ reviewedAt: options.reviewedAt,
465
+ reviewer: options.reviewer ?? "nimicoding_codex_auditor_path",
466
+ summary: options.summary ?? `Codex semantic audit accepted from ${rawRef}.`,
467
+ });
468
+ if (!review.ok) {
469
+ await markCodexAuditFailed(projectRoot, {
470
+ sweepId,
471
+ chunkId: options.chunkId,
472
+ failedAt: options.reviewedAt,
473
+ packetRef: prepare.packetRef,
474
+ transcriptRef: rawRef,
475
+ phase: "chunk_review",
476
+ reason: `Codex auditor evidence review rejected: ${review.error ?? "unknown review failure"}.`,
477
+ });
478
+ return inputError(`nimicoding audit-sweep refused: Codex auditor evidence review rejected for ${options.chunkId}: ${review.error ?? "unknown review failure"}.\n`);
479
+ }
480
+
481
+ const validation = await validateAuditSweepArtifacts(projectRoot, {
482
+ sweepId,
483
+ scope: "chunks",
484
+ });
485
+ if (!validation.ok) {
486
+ await markCodexAuditFailed(projectRoot, {
487
+ sweepId,
488
+ chunkId: options.chunkId,
489
+ failedAt: options.reviewedAt,
490
+ packetRef: prepare.packetRef,
491
+ transcriptRef: rawRef,
492
+ phase: "post_chunk_validation",
493
+ reason: "Post-Codex chunk validation failed.",
494
+ });
495
+ return inputError(`nimicoding audit-sweep refused: post-Codex chunk validation failed for ${options.chunkId}.\n`);
496
+ }
497
+
498
+ return {
499
+ ok: true,
500
+ exitCode: 0,
501
+ sweepId,
502
+ chunkId: options.chunkId,
503
+ state: "frozen",
504
+ packetRef: prepare.packetRef,
505
+ transcriptRef: rawRef,
506
+ extractedEvidenceRef: evidenceCandidateRef,
507
+ evidenceRef: ingest.evidenceRef,
508
+ findingsRef: ingest.findingsRef,
509
+ findingCount: ingest.findingCount,
510
+ addedCount: ingest.addedCount,
511
+ duplicateCount: ingest.duplicateCount,
512
+ reviewRef: review.runLedgerRef,
513
+ validationScope: "chunks",
514
+ };
515
+ }