@nimiplatform/nimi-coding 0.2.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/CHANGELOG.md +8 -92
  2. package/CONTRIBUTING.md +5 -34
  3. package/README.md +47 -350
  4. package/README.zh-CN.md +33 -287
  5. package/cli/commands/start.mjs +48 -688
  6. package/cli/commands/validate-spec-audit.mjs +1 -5
  7. package/cli/constants.mjs +9 -417
  8. package/cli/help.mjs +28 -145
  9. package/cli/index.mjs +5 -33
  10. package/cli/lib/blueprint-audit.mjs +5 -12
  11. package/cli/lib/bootstrap.mjs +0 -9
  12. package/cli/lib/contracts.mjs +0 -163
  13. package/cli/lib/doctor.mjs +155 -10
  14. package/cli/lib/entrypoints.mjs +63 -167
  15. package/cli/lib/internal/contracts-loaders.mjs +0 -85
  16. package/cli/lib/internal/contracts-parse.mjs +84 -520
  17. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +13 -8
  18. package/cli/lib/internal/surface-taxonomy-validators.mjs +316 -66
  19. package/cli/lib/internal/validators-shared.mjs +0 -16
  20. package/cli/lib/internal/validators-spec-helpers.mjs +7 -140
  21. package/cli/lib/internal/validators-spec.mjs +103 -342
  22. package/cli/lib/shared.mjs +1 -42
  23. package/cli/lib/validators.mjs +0 -48
  24. package/cli/seeds/seed-policy.yaml +0 -3
  25. package/config/bootstrap.yaml +4 -4
  26. package/config/spec-generation-inputs.yaml +6 -12
  27. package/contracts/domain-admission.schema.yaml +3 -3
  28. package/contracts/migration-inventory.schema.yaml +12 -75
  29. package/contracts/negative-fixtures.yaml +19 -99
  30. package/contracts/placement-contract.schema.yaml +49 -78
  31. package/contracts/projection-edge.schema.yaml +37 -121
  32. package/contracts/shared-enums.yaml +4 -8
  33. package/contracts/spec-generation-inputs.schema.yaml +15 -112
  34. package/contracts/spec-layout.schema.yaml +32 -0
  35. package/contracts/surface-taxonomy.schema.yaml +43 -94
  36. package/contracts/table-family.schema.yaml +1 -1
  37. package/contracts/tracked-output-admission.schema.yaml +13 -66
  38. package/methodology/core.yaml +33 -25
  39. package/methodology/four-closure-policy.yaml +18 -26
  40. package/methodology/role-separation-policy.yaml +21 -27
  41. package/methodology/spec-reconstruction.yaml +17 -34
  42. package/package.json +2 -4
  43. package/spec/_meta/spec-tree-model.yaml +5 -101
  44. package/spec/product-scope.yaml +25 -57
  45. package/adapters/README.md +0 -25
  46. package/adapters/claude/README.md +0 -89
  47. package/adapters/claude/profile.yaml +0 -70
  48. package/adapters/codex/README.md +0 -53
  49. package/adapters/codex/profile.yaml +0 -78
  50. package/adapters/oh-my-codex/README.md +0 -184
  51. package/adapters/oh-my-codex/profile.yaml +0 -46
  52. package/cli/commands/admit-high-risk-decision.mjs +0 -108
  53. package/cli/commands/audit-sweep.mjs +0 -364
  54. package/cli/commands/closeout.mjs +0 -186
  55. package/cli/commands/decide-high-risk-execution.mjs +0 -124
  56. package/cli/commands/handoff.mjs +0 -123
  57. package/cli/commands/ingest-high-risk-execution.mjs +0 -95
  58. package/cli/commands/review-high-risk-execution.mjs +0 -95
  59. package/cli/commands/sweep-design.mjs +0 -295
  60. package/cli/commands/sweep.mjs +0 -22
  61. package/cli/commands/topic-formatters.mjs +0 -382
  62. package/cli/commands/topic-goal.mjs +0 -33
  63. package/cli/commands/topic-options-shared.mjs +0 -27
  64. package/cli/commands/topic-options-workflow.mjs +0 -767
  65. package/cli/commands/topic-options.mjs +0 -626
  66. package/cli/commands/topic-runner.mjs +0 -169
  67. package/cli/commands/topic.mjs +0 -795
  68. package/cli/commands/validate-acceptance.mjs +0 -5
  69. package/cli/commands/validate-execution-packet.mjs +0 -5
  70. package/cli/commands/validate-orchestration-state.mjs +0 -5
  71. package/cli/commands/validate-prompt.mjs +0 -5
  72. package/cli/commands/validate-worker-output.mjs +0 -5
  73. package/cli/lib/adapter-profiles.mjs +0 -403
  74. package/cli/lib/audit-execution.mjs +0 -52
  75. package/cli/lib/audit-sweep-runtime/admissions.mjs +0 -508
  76. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +0 -356
  77. package/cli/lib/audit-sweep-runtime/chunks.mjs +0 -697
  78. package/cli/lib/audit-sweep-runtime/claude-auditor.mjs +0 -658
  79. package/cli/lib/audit-sweep-runtime/closeout.mjs +0 -144
  80. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +0 -654
  81. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +0 -527
  82. package/cli/lib/audit-sweep-runtime/common.mjs +0 -341
  83. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +0 -172
  84. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +0 -155
  85. package/cli/lib/audit-sweep-runtime/format.mjs +0 -57
  86. package/cli/lib/audit-sweep-runtime/ingest.mjs +0 -486
  87. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +0 -425
  88. package/cli/lib/audit-sweep-runtime/inventory.mjs +0 -798
  89. package/cli/lib/audit-sweep-runtime/ledger.mjs +0 -315
  90. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +0 -101
  91. package/cli/lib/audit-sweep-runtime/remediation.mjs +0 -349
  92. package/cli/lib/audit-sweep-runtime/rerun.mjs +0 -129
  93. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +0 -300
  94. package/cli/lib/audit-sweep-runtime/status.mjs +0 -62
  95. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +0 -215
  96. package/cli/lib/audit-sweep-runtime/validators.mjs +0 -797
  97. package/cli/lib/audit-sweep.mjs +0 -19
  98. package/cli/lib/authority-convergence.mjs +0 -720
  99. package/cli/lib/closeout.mjs +0 -732
  100. package/cli/lib/codex-sdk-runner.mjs +0 -76
  101. package/cli/lib/external-execution.mjs +0 -101
  102. package/cli/lib/handoff.mjs +0 -812
  103. package/cli/lib/high-risk-admission.mjs +0 -512
  104. package/cli/lib/high-risk-decision.mjs +0 -353
  105. package/cli/lib/high-risk-ingest.mjs +0 -321
  106. package/cli/lib/high-risk-review.mjs +0 -267
  107. package/cli/lib/internal/contracts-parse-high-risk.mjs +0 -131
  108. package/cli/lib/internal/contracts-validators.mjs +0 -399
  109. package/cli/lib/internal/doctor-bootstrap-surface.mjs +0 -406
  110. package/cli/lib/internal/doctor-delegated-surface.mjs +0 -256
  111. package/cli/lib/internal/doctor-finalize.mjs +0 -383
  112. package/cli/lib/internal/doctor-format.mjs +0 -286
  113. package/cli/lib/internal/doctor-inspectors.mjs +0 -327
  114. package/cli/lib/internal/doctor-state.mjs +0 -205
  115. package/cli/lib/internal/validators-artifacts.mjs +0 -515
  116. package/cli/lib/sweep-design-runtime/common.mjs +0 -246
  117. package/cli/lib/sweep-design-runtime/engine.mjs +0 -733
  118. package/cli/lib/sweep-design-runtime/fix-topic.mjs +0 -414
  119. package/cli/lib/sweep-design-runtime/lifecycle.mjs +0 -54
  120. package/cli/lib/sweep-design-runtime/results.mjs +0 -324
  121. package/cli/lib/sweep-design.mjs +0 -8
  122. package/cli/lib/topic-artifacts.mjs +0 -186
  123. package/cli/lib/topic-authority-coverage.mjs +0 -73
  124. package/cli/lib/topic-closeout.mjs +0 -560
  125. package/cli/lib/topic-common.mjs +0 -404
  126. package/cli/lib/topic-decisions.mjs +0 -332
  127. package/cli/lib/topic-draft-packets.mjs +0 -167
  128. package/cli/lib/topic-execution.mjs +0 -533
  129. package/cli/lib/topic-goal.mjs +0 -440
  130. package/cli/lib/topic-ledger.mjs +0 -281
  131. package/cli/lib/topic-lifecycle-artifacts.mjs +0 -173
  132. package/cli/lib/topic-root-validation.mjs +0 -288
  133. package/cli/lib/topic-runner-commands.mjs +0 -174
  134. package/cli/lib/topic-runner-deferral.mjs +0 -532
  135. package/cli/lib/topic-runner-stale-gates.mjs +0 -114
  136. package/cli/lib/topic-runner-validation.mjs +0 -138
  137. package/cli/lib/topic-runner.mjs +0 -727
  138. package/cli/lib/topic-scaffold.mjs +0 -252
  139. package/cli/lib/topic-waves.mjs +0 -403
  140. package/cli/lib/topic.mjs +0 -81
  141. package/config/audit-execution-artifacts.yaml +0 -20
  142. package/config/external-execution-artifacts.yaml +0 -16
  143. package/config/host-adapter.yaml +0 -30
  144. package/config/host-profile.yaml +0 -29
  145. package/config/installer-evidence.yaml +0 -31
  146. package/config/skill-installer.yaml +0 -23
  147. package/config/skill-manifest.yaml +0 -48
  148. package/config/skills.yaml +0 -30
  149. package/contracts/acceptance.schema.yaml +0 -16
  150. package/contracts/admission-checklist.schema.yaml +0 -15
  151. package/contracts/audit-chunk.schema.yaml +0 -123
  152. package/contracts/audit-closeout.schema.yaml +0 -51
  153. package/contracts/audit-finding.schema.yaml +0 -61
  154. package/contracts/audit-ledger.schema.yaml +0 -138
  155. package/contracts/audit-plan.schema.yaml +0 -138
  156. package/contracts/audit-remediation-map.schema.yaml +0 -52
  157. package/contracts/audit-rerun.schema.yaml +0 -31
  158. package/contracts/audit-sweep-result.yaml +0 -53
  159. package/contracts/authority-convergence-audit.schema.yaml +0 -19
  160. package/contracts/closeout.schema.yaml +0 -25
  161. package/contracts/decision-review.schema.yaml +0 -16
  162. package/contracts/doc-spec-audit-result.yaml +0 -19
  163. package/contracts/execution-packet.schema.yaml +0 -49
  164. package/contracts/external-host-compatibility.yaml +0 -22
  165. package/contracts/forbidden-shortcuts.catalog.yaml +0 -23
  166. package/contracts/high-risk-admission.schema.yaml +0 -23
  167. package/contracts/high-risk-execution-result.yaml +0 -20
  168. package/contracts/orchestration-state.schema.yaml +0 -41
  169. package/contracts/overflow-continuation.schema.yaml +0 -12
  170. package/contracts/packet.schema.yaml +0 -30
  171. package/contracts/pending-note.schema.yaml +0 -17
  172. package/contracts/prompt.schema.yaml +0 -12
  173. package/contracts/remediation.schema.yaml +0 -16
  174. package/contracts/result.schema.yaml +0 -24
  175. package/contracts/spec-reconstruction-result.yaml +0 -41
  176. package/contracts/sweep-design-result.yaml +0 -349
  177. package/contracts/topic-goal.schema.yaml +0 -87
  178. package/contracts/topic-run-ledger.schema.yaml +0 -72
  179. package/contracts/topic-step-decision.schema.yaml +0 -45
  180. package/contracts/topic.schema.yaml +0 -65
  181. package/contracts/true-close.schema.yaml +0 -15
  182. package/contracts/wave.schema.yaml +0 -29
  183. package/contracts/worker-output.schema.yaml +0 -15
  184. package/contracts/workflow-consumer.schema.yaml +0 -110
  185. package/methodology/audit-sweep-p0p1-recall.yaml +0 -45
  186. package/methodology/authority-convergence-policy.yaml +0 -42
  187. package/methodology/overflow-continuation-policy.yaml +0 -14
  188. package/methodology/skill-exchange-projection.yaml +0 -114
  189. package/methodology/skill-handoff.yaml +0 -34
  190. package/methodology/skill-installer-result.yaml +0 -27
  191. package/methodology/skill-installer-summary-projection.yaml +0 -181
  192. package/methodology/skill-runtime.yaml +0 -23
  193. package/methodology/spec-target-truth-profile.yaml +0 -46
  194. package/methodology/topic-lifecycle-report.yaml +0 -144
  195. package/methodology/topic-lifecycle.yaml +0 -37
  196. package/methodology/topic-naming-ontology.yaml +0 -21
  197. package/methodology/topic-ontology.yaml +0 -38
  198. package/methodology/topic-validation-policy.yaml +0 -9
  199. package/methodology/wave-dag-policy.yaml +0 -14
  200. package/spec/_meta/command-gating-matrix.yaml +0 -143
  201. package/spec/_meta/generate-drift-migration-checklist.yaml +0 -137
  202. package/spec/_meta/governance-routing-cutover-checklist.yaml +0 -35
  203. package/spec/_meta/phase2-impacted-surface-matrix.yaml +0 -44
  204. package/spec/_meta/spec-authority-cutover-readiness.yaml +0 -102
  205. package/spec/bootstrap-state.yaml +0 -99
@@ -1,727 +0,0 @@
1
- import { mkdir, readFile, writeFile } from "node:fs/promises";
2
- import path from "node:path";
3
-
4
- import { runNativeCodexSdkPrompt } from "./codex-sdk-runner.mjs";
5
- import { parseMechanicalCommandRef } from "./topic-runner-commands.mjs";
6
- export { parseMechanicalCommandRef } from "./topic-runner-commands.mjs";
7
- import { deferLocalWaveBlocker } from "./topic-runner-deferral.mjs";
8
- import { maybeResolveStaleHumanGate } from "./topic-runner-stale-gates.mjs";
9
- export {
10
- classifyValidationCommandResult,
11
- runValidationCommandEvidence,
12
- } from "./topic-runner-validation.mjs";
13
- import {
14
- admitWaveInTopic,
15
- buildTopicRunLedger,
16
- closeoutWaveInTopic,
17
- decideTopicNextStep,
18
- dispatchTopicPacket,
19
- freezePacketForTopic,
20
- initTopicRunLedger,
21
- loadTopicReport,
22
- readTopicRunLedger,
23
- recordTopicResult,
24
- recordTopicRunEvent,
25
- } from "./topic.mjs";
26
-
27
- const ADAPTER_IDS = new Set(["codex", "oh_my_codex", "claude"]);
28
-
29
- function utcNowNoMillis() {
30
- return new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
31
- }
32
-
33
- function toPortablePath(value) {
34
- return value.split(path.sep).join("/");
35
- }
36
-
37
- function projectRef(projectRoot, absolutePath) {
38
- return toPortablePath(path.relative(projectRoot, absolutePath));
39
- }
40
-
41
- function hasPlaceholder(value) {
42
- return /<[^>]+>/.test(value);
43
- }
44
-
45
- function safeSegment(value) {
46
- return String(value).replace(/[^a-zA-Z0-9._-]+/g, "-");
47
- }
48
-
49
- function remapTopicRef(ref, fromTopicRef, toTopicRef) {
50
- if (!ref || !fromTopicRef || !toTopicRef || fromTopicRef === toTopicRef) {
51
- return ref;
52
- }
53
- return ref.startsWith(`${fromTopicRef}/`)
54
- ? `${toTopicRef}/${ref.slice(fromTopicRef.length + 1)}`
55
- : ref;
56
- }
57
-
58
- function buildGate(decision) {
59
- if (!decision || decision.stop_class === "continue") {
60
- return null;
61
- }
62
- return {
63
- stopClass: decision.stop_class,
64
- reasonCode: decision.reason_code,
65
- recommendedAction: decision.recommended_action,
66
- recommendedDecision: decision.recommended_decision,
67
- recommendationRationale: decision.recommendation_rationale,
68
- nextCommandRef: decision.next_command_ref,
69
- blockingChecks: decision.blocking_checks ?? [],
70
- };
71
- }
72
-
73
- function isTerminalWave(wave) {
74
- return ["closed", "retired", "superseded"].includes(wave?.state);
75
- }
76
-
77
- function getTopicWaves(topic) {
78
- return Array.isArray(topic?.waves) ? topic.waves : [];
79
- }
80
-
81
- function findDeterministicNextWave(topic) {
82
- const waves = getTopicWaves(topic);
83
- const terminalIds = new Set(waves.filter(isTerminalWave).map((wave) => wave.wave_id));
84
- const ready = waves.filter((wave) => {
85
- if (isTerminalWave(wave)) return false;
86
- if (!["candidate", "preflight_draft", "needs_revision"].includes(wave.state)) return false;
87
- const deps = Array.isArray(wave.deps) ? wave.deps : [];
88
- return deps.every((dep) => terminalIds.has(dep));
89
- });
90
- return ready.length > 0 ? ready[0] : null;
91
- }
92
-
93
- function normalizePhaseTransitionDecision(decisionReport, topic) {
94
- const decision = decisionReport?.decision;
95
- if (!decision || decision.stop_class !== "require_human_confirmation") {
96
- return decisionReport;
97
- }
98
-
99
- if (
100
- decision.recommended_action === "closeout_wave" &&
101
- typeof decision.next_command_ref === "string" &&
102
- !hasPlaceholder(decision.next_command_ref)
103
- ) {
104
- return {
105
- ...decisionReport,
106
- decision: {
107
- ...decision,
108
- stop_class: "continue",
109
- requires_human_confirmation: false,
110
- recommended_decision: "closeout_wave",
111
- recommendation_rationale: "Wave closeout is a deterministic phase transition once lineage-backed result evidence exists.",
112
- },
113
- };
114
- }
115
-
116
- if (decision.reason_code === "no_selected_next_target") {
117
- const nextWave = findDeterministicNextWave(topic);
118
- if (!nextWave) {
119
- return decisionReport;
120
- }
121
- return {
122
- ...decisionReport,
123
- decision: {
124
- ...decision,
125
- wave_id: nextWave.wave_id,
126
- stop_class: "continue",
127
- recommended_action: "admit_wave",
128
- reason_code: "deterministic_next_wave_ready",
129
- requires_human_confirmation: false,
130
- recommended_decision: "admit_wave",
131
- recommendation_rationale: "The first dependency-ready non-terminal wave in topic.yaml waves[] order is selected mechanically.",
132
- expected_artifacts: [],
133
- next_command_ref: `nimicoding topic wave admit ${topic.topic_id} ${nextWave.wave_id}`,
134
- blocking_checks: [],
135
- },
136
- };
137
- }
138
-
139
- return decisionReport;
140
- }
141
-
142
- function blockedResult(base, error, extra = {}) {
143
- return {
144
- ok: false,
145
- ...base,
146
- runnerStatus: "blocked",
147
- stopClass: "blocked",
148
- recommendedAction: "no_action",
149
- error,
150
- circuitBreaker: {
151
- state: "open",
152
- reason: error,
153
- },
154
- ...extra,
155
- };
156
- }
157
-
158
- async function ensureLedger(projectRoot, topicInput, runId, startedAt) {
159
- const existing = await readTopicRunLedger(projectRoot, topicInput, runId);
160
- if (existing.ok) {
161
- return existing;
162
- }
163
- if (!String(existing.error ?? "").includes("not found")) {
164
- return existing;
165
- }
166
- return initTopicRunLedger(projectRoot, topicInput, runId, startedAt);
167
- }
168
-
169
- async function writeDecisionArtifact(projectRoot, loaded, runId, decision, eventIndex) {
170
- const decisionPath = path.join(
171
- loaded.topicDir,
172
- `runner-decision-${safeSegment(runId)}-${String(eventIndex).padStart(4, "0")}.json`,
173
- );
174
- await writeFile(decisionPath, `${JSON.stringify(decision, null, 2)}\n`, "utf8");
175
- return projectRef(projectRoot, decisionPath);
176
- }
177
-
178
- async function recordRunnerBlocked(projectRoot, options, fields) {
179
- return recordTopicRunEvent(projectRoot, options.topicInput, {
180
- runId: options.runId,
181
- eventKind: "runner_blocked",
182
- stopClass: "blocked",
183
- recommendedAction: "no_action",
184
- sourceRef: fields.sourceRef,
185
- summary: fields.summary,
186
- recordedAt: fields.recordedAt,
187
- waveId: fields.waveId,
188
- artifactRefs: fields.artifactRefs ?? {},
189
- });
190
- }
191
-
192
- async function rewriteMovedRunEventRef(projectRoot, eventRef, fromTopicRef, toTopicRef, fromRef, toRef) {
193
- if (fromRef === toRef) {
194
- return;
195
- }
196
- const movedEventRef = remapTopicRef(eventRef, fromTopicRef, toTopicRef);
197
- const eventPath = path.join(projectRoot, movedEventRef);
198
- const eventText = await readFile(eventPath, "utf8");
199
- await writeFile(eventPath, eventText.split(fromRef).join(toRef), "utf8");
200
- }
201
-
202
- async function executeMechanicalCommand(projectRoot, options, parsedCommand) {
203
- if (parsedCommand.action === "admit_wave") {
204
- const report = await admitWaveInTopic(projectRoot, options.topicInput, parsedCommand.waveId);
205
- return {
206
- ok: report.ok,
207
- action: parsedCommand.action,
208
- report,
209
- eventKind: "wave_admitted",
210
- eventSourceRef: report.ok ? `${report.topicRef}/topic.yaml` : null,
211
- summary: report.ok ? "wave_admit_completed" : "runner_wave_admit_failed",
212
- artifactRefs: {},
213
- waveId: report.ok ? report.waveId : parsedCommand.waveId,
214
- error: report.ok ? null : report.error,
215
- };
216
- }
217
-
218
- if (parsedCommand.action === "freeze_packet") {
219
- const report = await freezePacketForTopic(projectRoot, options.topicInput, parsedCommand.draftPath);
220
- return {
221
- ok: report.ok,
222
- action: parsedCommand.action,
223
- report,
224
- eventKind: "packet_frozen",
225
- eventSourceRef: report.ok ? report.packetRef : null,
226
- summary: report.ok ? "packet_freeze_completed" : "runner_packet_freeze_failed",
227
- artifactRefs: report.ok ? { packet_ref: report.packetRef } : {},
228
- waveId: report.ok ? report.waveId : null,
229
- error: report.ok ? null : report.error,
230
- };
231
- }
232
-
233
- if (parsedCommand.action === "closeout_wave") {
234
- const report = await closeoutWaveInTopic(projectRoot, options.topicInput, parsedCommand.waveId, {
235
- authorityClosure: parsedCommand.authorityClosure,
236
- semanticClosure: parsedCommand.semanticClosure,
237
- consumerClosure: parsedCommand.consumerClosure,
238
- driftResistanceClosure: parsedCommand.driftResistanceClosure,
239
- disposition: parsedCommand.disposition,
240
- });
241
- return {
242
- ok: report.ok,
243
- action: parsedCommand.action,
244
- report,
245
- eventKind: "wave_closed",
246
- eventSourceRef: report.ok ? report.closeoutRef : null,
247
- summary: report.ok ? "wave_closeout_completed" : "runner_wave_closeout_failed",
248
- artifactRefs: report.ok ? { closeout_ref: report.closeoutRef } : {},
249
- waveId: report.ok ? report.waveId : parsedCommand.waveId,
250
- error: report.ok ? null : report.error,
251
- };
252
- }
253
-
254
- if (parsedCommand.action === "record_result") {
255
- const report = await recordTopicResult(
256
- projectRoot,
257
- options.topicInput,
258
- parsedCommand.resultKind,
259
- parsedCommand.verdict,
260
- parsedCommand.fromPath,
261
- parsedCommand.verifiedAt,
262
- );
263
- return {
264
- ok: report.ok,
265
- action: parsedCommand.action,
266
- report,
267
- eventKind: "result_recorded",
268
- eventSourceRef: report.ok ? report.resultRef : null,
269
- summary: report.ok ? `${parsedCommand.resultKind}_result_recorded` : "runner_result_record_failed",
270
- artifactRefs: report.ok ? { result_ref: report.resultRef } : {},
271
- waveId: report.ok ? report.waveId : null,
272
- recordedAt: parsedCommand.verifiedAt,
273
- error: report.ok ? null : report.error,
274
- };
275
- }
276
-
277
- return {
278
- ok: false,
279
- action: parsedCommand.action,
280
- report: null,
281
- eventKind: null,
282
- eventSourceRef: null,
283
- summary: "runner_unsupported_mechanical_command",
284
- artifactRefs: {},
285
- waveId: null,
286
- error: `topic-runner refused: unsupported mechanical action ${parsedCommand.action}`,
287
- };
288
- }
289
-
290
- async function writeHostOutput(projectRoot, options, dispatchReport, hostReport, recordedAt) {
291
- const outputDir = path.join(projectRoot, ".nimi", "local", "outputs", safeSegment(options.runId));
292
- await mkdir(outputDir, { recursive: true });
293
- const outputPath = path.join(
294
- outputDir,
295
- `${safeSegment(dispatchReport.role)}-${safeSegment(dispatchReport.packetId)}.codex-output.json`,
296
- );
297
- await writeFile(
298
- outputPath,
299
- `${JSON.stringify({
300
- contract: "nimicoding.topic-runner.host-output.v1",
301
- topic_id: dispatchReport.topicId,
302
- run_id: options.runId,
303
- adapter_id: options.adapter,
304
- role: dispatchReport.role,
305
- packet_id: dispatchReport.packetId,
306
- prompt_ref: dispatchReport.promptRef,
307
- recorded_at: recordedAt,
308
- host_report: hostReport,
309
- }, null, 2)}\n`,
310
- "utf8",
311
- );
312
- return projectRef(projectRoot, outputPath);
313
- }
314
-
315
- async function maybeExecuteHost(projectRoot, options, dispatchReport, deps) {
316
- if (!options.executeHost) {
317
- return {
318
- ok: true,
319
- executed: false,
320
- hostOutputRef: null,
321
- hostReport: null,
322
- };
323
- }
324
- if (options.adapter !== "codex") {
325
- return {
326
- ok: false,
327
- error: `topic-runner refused: --execute-host is currently supported only for adapter codex, not ${options.adapter}`,
328
- };
329
- }
330
-
331
- const prompt = await readFile(path.join(projectRoot, dispatchReport.promptRef), "utf8");
332
- const hostReport = await runNativeCodexSdkPrompt({
333
- prompt,
334
- threadId: options.threadId,
335
- codex: deps.codex,
336
- });
337
- if (!hostReport.ok) {
338
- return hostReport;
339
- }
340
- const hostOutputRef = await writeHostOutput(projectRoot, options, dispatchReport, hostReport, options.recordedAt);
341
- return {
342
- ok: true,
343
- executed: true,
344
- hostOutputRef,
345
- hostReport,
346
- };
347
- }
348
-
349
- export async function runTopicRunnerStep(projectRoot, options, deps = {}) {
350
- if (!ADAPTER_IDS.has(options.adapter)) {
351
- return {
352
- ok: false,
353
- error: `topic-runner refused: unsupported adapter ${options.adapter}`,
354
- };
355
- }
356
-
357
- const recordedAt = options.verifiedAt ?? utcNowNoMillis();
358
- let ledger = await ensureLedger(projectRoot, options.topicInput, options.runId, recordedAt);
359
- if (!ledger.ok) {
360
- return ledger;
361
- }
362
-
363
- const loaded = await loadTopicReport(projectRoot, options.topicInput);
364
- if (!loaded.ok) {
365
- return loaded;
366
- }
367
-
368
- const rawDecisionReport = await decideTopicNextStep(projectRoot, options.topicInput);
369
- if (!rawDecisionReport.ok) {
370
- return rawDecisionReport;
371
- }
372
- const decisionReport = normalizePhaseTransitionDecision(rawDecisionReport, loaded.topic);
373
-
374
- const staleGateSync = await maybeResolveStaleHumanGate(
375
- projectRoot,
376
- options,
377
- loaded,
378
- ledger,
379
- decisionReport.decision,
380
- recordedAt,
381
- );
382
- if (!staleGateSync.ok) {
383
- return staleGateSync;
384
- }
385
- ledger = staleGateSync.ledger;
386
-
387
- const decisionRef = await writeDecisionArtifact(
388
- projectRoot,
389
- loaded,
390
- options.runId,
391
- decisionReport.decision,
392
- (ledger.eventCount ?? 0) + 1,
393
- );
394
- const decisionEvent = await recordTopicRunEvent(projectRoot, options.topicInput, {
395
- runId: options.runId,
396
- eventKind: "decision_emitted",
397
- stopClass: decisionReport.decision.stop_class,
398
- recommendedAction: decisionReport.decision.recommended_action,
399
- sourceRef: decisionRef,
400
- summary: decisionReport.decision.reason_code,
401
- recordedAt,
402
- waveId: decisionReport.decision.wave_id,
403
- artifactRefs: {
404
- decision_ref: decisionRef,
405
- },
406
- });
407
- if (!decisionEvent.ok) {
408
- return decisionEvent;
409
- }
410
-
411
- if (decisionReport.decision.stop_class !== "continue") {
412
- if (options.allowDeferredLocalBlockers === true) {
413
- const deferred = await deferLocalWaveBlocker(
414
- projectRoot,
415
- loaded,
416
- decisionReport.decision,
417
- decisionRef,
418
- recordedAt,
419
- );
420
- if (deferred.ok) {
421
- const deferredEvent = await recordTopicRunEvent(projectRoot, options.topicInput, {
422
- runId: options.runId,
423
- eventKind: "runner_blocked",
424
- stopClass: "continue",
425
- recommendedAction: "admit_wave",
426
- sourceRef: deferred.blockerRef,
427
- summary: "deferred_local_wave_blocker",
428
- recordedAt,
429
- waveId: deferred.wave.wave_id,
430
- artifactRefs: {
431
- decision_ref: decisionRef,
432
- evidence_ref: deferred.blockerRef,
433
- },
434
- });
435
- if (!deferredEvent.ok) {
436
- return deferredEvent;
437
- }
438
- return {
439
- ok: true,
440
- topicId: decisionReport.topicId,
441
- topicRef: decisionReport.topicRef,
442
- runId: options.runId,
443
- adapter: options.adapter,
444
- runnerStatus: "continued",
445
- executed: true,
446
- stopClass: "continue",
447
- recommendedAction: "defer_local_wave_blocker",
448
- decision: decisionReport.decision,
449
- gate: buildGate(decisionReport.decision),
450
- decisionRef,
451
- deferredBlocker: {
452
- waveId: deferred.wave.wave_id,
453
- reasonCode: decisionReport.decision.reason_code,
454
- blockerRef: deferred.blockerRef,
455
- nextWaveId: deferred.nextWave.wave_id,
456
- },
457
- ledgerRef: deferredEvent.ledgerRef,
458
- eventCount: deferredEvent.eventCount,
459
- };
460
- }
461
- }
462
- return {
463
- ok: true,
464
- topicId: decisionReport.topicId,
465
- topicRef: decisionReport.topicRef,
466
- runId: options.runId,
467
- adapter: options.adapter,
468
- runnerStatus: "stopped",
469
- executed: false,
470
- stopClass: decisionReport.decision.stop_class,
471
- recommendedAction: decisionReport.decision.recommended_action,
472
- decision: decisionReport.decision,
473
- gate: buildGate(decisionReport.decision),
474
- decisionRef,
475
- ledgerRef: decisionEvent.ledgerRef,
476
- eventCount: decisionEvent.eventCount,
477
- };
478
- }
479
-
480
- const parsedCommand = parseMechanicalCommandRef(
481
- decisionReport.decision.next_command_ref,
482
- decisionReport.topicId,
483
- );
484
- if (!parsedCommand.ok) {
485
- const blockedEvent = await recordRunnerBlocked(projectRoot, { ...options, verifiedAt: recordedAt }, {
486
- sourceRef: decisionRef,
487
- summary: "runner_refused_next_command",
488
- recordedAt,
489
- waveId: decisionReport.decision.wave_id,
490
- artifactRefs: { decision_ref: decisionRef },
491
- });
492
- return blockedResult({
493
- topicId: decisionReport.topicId,
494
- topicRef: decisionReport.topicRef,
495
- runId: options.runId,
496
- adapter: options.adapter,
497
- decision: decisionReport.decision,
498
- decisionRef,
499
- ledgerRef: blockedEvent.ok ? blockedEvent.ledgerRef : decisionEvent.ledgerRef,
500
- eventCount: blockedEvent.ok ? blockedEvent.eventCount : decisionEvent.eventCount,
501
- }, parsedCommand.error);
502
- }
503
-
504
- if (["admit_wave", "freeze_packet", "closeout_wave", "record_result"].includes(parsedCommand.action)) {
505
- const commandExecution = await executeMechanicalCommand(projectRoot, { ...options, recordedAt }, parsedCommand);
506
- if (!commandExecution.ok) {
507
- const blockedEvent = await recordRunnerBlocked(projectRoot, { ...options, verifiedAt: recordedAt }, {
508
- sourceRef: decisionRef,
509
- summary: commandExecution.summary,
510
- recordedAt,
511
- waveId: commandExecution.waveId ?? decisionReport.decision.wave_id,
512
- artifactRefs: { decision_ref: decisionRef },
513
- });
514
- return blockedResult({
515
- topicId: decisionReport.topicId,
516
- topicRef: decisionReport.topicRef,
517
- runId: options.runId,
518
- adapter: options.adapter,
519
- decision: decisionReport.decision,
520
- decisionRef,
521
- ledgerRef: blockedEvent.ok ? blockedEvent.ledgerRef : decisionEvent.ledgerRef,
522
- eventCount: blockedEvent.ok ? blockedEvent.eventCount : decisionEvent.eventCount,
523
- }, commandExecution.error ?? "topic-runner mechanical command failed");
524
- }
525
-
526
- const effectiveDecisionRef = remapTopicRef(
527
- decisionRef,
528
- decisionReport.topicRef,
529
- commandExecution.report.topicRef,
530
- );
531
- await rewriteMovedRunEventRef(
532
- projectRoot,
533
- decisionEvent.eventRef,
534
- decisionReport.topicRef,
535
- commandExecution.report.topicRef,
536
- decisionRef,
537
- effectiveDecisionRef,
538
- );
539
- const artifactRefs = {
540
- decision_ref: effectiveDecisionRef,
541
- ...commandExecution.artifactRefs,
542
- };
543
- const commandEvent = await recordTopicRunEvent(projectRoot, options.topicInput, {
544
- runId: options.runId,
545
- eventKind: commandExecution.eventKind,
546
- stopClass: "continue",
547
- recommendedAction: parsedCommand.action,
548
- sourceRef: commandExecution.eventSourceRef,
549
- summary: commandExecution.summary,
550
- recordedAt: commandExecution.recordedAt ?? recordedAt,
551
- waveId: commandExecution.waveId,
552
- artifactRefs,
553
- });
554
- if (!commandEvent.ok) {
555
- return commandEvent;
556
- }
557
-
558
- return {
559
- ok: true,
560
- topicId: commandExecution.report.topicId,
561
- topicRef: commandExecution.report.topicRef,
562
- runId: options.runId,
563
- adapter: options.adapter,
564
- runnerStatus: "continued",
565
- executed: true,
566
- stopClass: "continue",
567
- recommendedAction: decisionReport.decision.recommended_action,
568
- decision: decisionReport.decision,
569
- gate: buildGate(decisionReport.decision),
570
- decisionRef: effectiveDecisionRef,
571
- command: commandExecution.report,
572
- ledgerRef: commandEvent.ledgerRef,
573
- eventCount: commandEvent.eventCount,
574
- };
575
- }
576
-
577
- const dispatchReport = await dispatchTopicPacket(
578
- projectRoot,
579
- options.topicInput,
580
- parsedCommand.packetId,
581
- parsedCommand.role,
582
- );
583
- if (!dispatchReport.ok) {
584
- const blockedEvent = await recordRunnerBlocked(projectRoot, { ...options, verifiedAt: recordedAt }, {
585
- sourceRef: decisionRef,
586
- summary: "runner_dispatch_failed",
587
- recordedAt,
588
- waveId: decisionReport.decision.wave_id,
589
- artifactRefs: { decision_ref: decisionRef },
590
- });
591
- return blockedResult({
592
- topicId: decisionReport.topicId,
593
- topicRef: decisionReport.topicRef,
594
- runId: options.runId,
595
- adapter: options.adapter,
596
- decision: decisionReport.decision,
597
- decisionRef,
598
- ledgerRef: blockedEvent.ok ? blockedEvent.ledgerRef : decisionEvent.ledgerRef,
599
- eventCount: blockedEvent.ok ? blockedEvent.eventCount : decisionEvent.eventCount,
600
- }, dispatchReport.error);
601
- }
602
-
603
- const hostExecution = await maybeExecuteHost(
604
- projectRoot,
605
- { ...options, recordedAt },
606
- dispatchReport,
607
- deps,
608
- );
609
- if (!hostExecution.ok) {
610
- const blockedEvent = await recordRunnerBlocked(projectRoot, { ...options, verifiedAt: recordedAt }, {
611
- sourceRef: dispatchReport.promptRef,
612
- summary: "runner_host_execution_failed",
613
- recordedAt,
614
- waveId: dispatchReport.waveId,
615
- artifactRefs: {
616
- decision_ref: decisionRef,
617
- packet_ref: dispatchReport.packetRef,
618
- prompt_ref: dispatchReport.promptRef,
619
- },
620
- });
621
- return blockedResult({
622
- topicId: decisionReport.topicId,
623
- topicRef: decisionReport.topicRef,
624
- runId: options.runId,
625
- adapter: options.adapter,
626
- decision: decisionReport.decision,
627
- decisionRef,
628
- dispatch: dispatchReport,
629
- ledgerRef: blockedEvent.ok ? blockedEvent.ledgerRef : decisionEvent.ledgerRef,
630
- eventCount: blockedEvent.ok ? blockedEvent.eventCount : decisionEvent.eventCount,
631
- }, hostExecution.error);
632
- }
633
-
634
- const artifactRefs = {
635
- packet_ref: dispatchReport.packetRef,
636
- prompt_ref: dispatchReport.promptRef,
637
- };
638
- if (hostExecution.hostOutputRef) {
639
- artifactRefs[dispatchReport.role === "audit" ? "audit_output_ref" : "worker_output_ref"] = hostExecution.hostOutputRef;
640
- }
641
-
642
- const dispatchEvent = await recordTopicRunEvent(projectRoot, options.topicInput, {
643
- runId: options.runId,
644
- eventKind: dispatchReport.role === "audit" ? "audit_dispatched" : "worker_dispatched",
645
- stopClass: "continue",
646
- recommendedAction: "record_result",
647
- sourceRef: dispatchReport.promptRef,
648
- summary: `${dispatchReport.role}_dispatch_completed`,
649
- recordedAt,
650
- waveId: dispatchReport.waveId,
651
- artifactRefs,
652
- });
653
- if (!dispatchEvent.ok) {
654
- return dispatchEvent;
655
- }
656
-
657
- return {
658
- ok: true,
659
- topicId: decisionReport.topicId,
660
- topicRef: decisionReport.topicRef,
661
- runId: options.runId,
662
- adapter: options.adapter,
663
- runnerStatus: "continued",
664
- executed: true,
665
- stopClass: "continue",
666
- recommendedAction: decisionReport.decision.recommended_action,
667
- decision: decisionReport.decision,
668
- gate: buildGate(decisionReport.decision),
669
- decisionRef,
670
- dispatch: dispatchReport,
671
- hostExecution,
672
- ledgerRef: dispatchEvent.ledgerRef,
673
- eventCount: dispatchEvent.eventCount,
674
- };
675
- }
676
-
677
- export async function runTopicRunner(projectRoot, options, deps = {}) {
678
- const maxSteps = Number.isInteger(options.maxSteps) && options.maxSteps > 0
679
- ? options.maxSteps
680
- : 20;
681
- const steps = [];
682
- for (let index = 0; index < maxSteps; index += 1) {
683
- const step = await runTopicRunnerStep(projectRoot, {
684
- ...options,
685
- allowDeferredLocalBlockers: true,
686
- }, deps);
687
- steps.push(step);
688
- if (!step.ok || step.runnerStatus !== "continued") {
689
- return {
690
- ...step,
691
- mode: "run",
692
- steps,
693
- stepCount: steps.length,
694
- };
695
- }
696
- }
697
-
698
- const recordedAt = options.verifiedAt ?? utcNowNoMillis();
699
- const latestStep = steps.at(-1) ?? null;
700
- const blockedEvent = latestStep?.decisionRef
701
- ? await recordRunnerBlocked(projectRoot, { ...options, verifiedAt: recordedAt }, {
702
- sourceRef: latestStep.decisionRef,
703
- summary: "runner_max_steps_exhausted",
704
- recordedAt,
705
- waveId: latestStep.decision?.wave_id ?? null,
706
- artifactRefs: { decision_ref: latestStep.decisionRef },
707
- })
708
- : null;
709
- const ledger = await buildTopicRunLedger(projectRoot, options.topicInput, options.runId, recordedAt);
710
- return blockedResult({
711
- mode: "run",
712
- topicId: latestStep?.topicId ?? null,
713
- topicRef: latestStep?.topicRef ?? null,
714
- runId: options.runId,
715
- adapter: options.adapter,
716
- steps,
717
- stepCount: steps.length,
718
- ledgerRef: blockedEvent?.ok ? blockedEvent.ledgerRef : ledger.ok ? ledger.ledgerRef : null,
719
- eventCount: blockedEvent?.ok ? blockedEvent.eventCount : ledger.ok ? ledger.eventCount : null,
720
- }, `topic-runner refused: max steps exhausted (${maxSteps})`, {
721
- circuitBreaker: {
722
- state: "open",
723
- reason: "max_steps_exhausted",
724
- maxSteps,
725
- },
726
- });
727
- }