@nimiplatform/nimi-coding 0.1.0 → 0.2.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 (121) hide show
  1. package/README.md +19 -20
  2. package/adapters/oh-my-codex/README.md +8 -9
  3. package/cli/commands/audit-sweep.mjs +10 -10
  4. package/cli/commands/classify-spec-tree.mjs +5 -0
  5. package/cli/commands/closeout.mjs +3 -0
  6. package/cli/commands/generate-spec-derived-docs.mjs +20 -0
  7. package/cli/commands/generate-spec-migration-plan.mjs +30 -0
  8. package/cli/commands/start.mjs +5 -1
  9. package/cli/commands/surface-validator-command.mjs +49 -0
  10. package/cli/commands/sweep-design.mjs +295 -0
  11. package/cli/commands/sweep.mjs +22 -0
  12. package/cli/commands/sync.mjs +132 -0
  13. package/cli/commands/topic-formatters.mjs +8 -8
  14. package/cli/commands/validate-ai-governance.mjs +167 -46
  15. package/cli/commands/validate-domain-admission.mjs +5 -0
  16. package/cli/commands/validate-guidance-bodies.mjs +5 -0
  17. package/cli/commands/validate-placement.mjs +5 -0
  18. package/cli/commands/validate-projection-edges.mjs +5 -0
  19. package/cli/commands/validate-spec-audit.mjs +5 -1
  20. package/cli/commands/validate-table-family.mjs +5 -0
  21. package/cli/commands/validate-tracked-output-admission.mjs +5 -0
  22. package/cli/constants.mjs +5 -49
  23. package/cli/help.mjs +33 -11
  24. package/cli/index.mjs +20 -2
  25. package/cli/lib/audit-sweep-runtime/admissions.mjs +38 -29
  26. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +8 -0
  27. package/cli/lib/audit-sweep-runtime/chunks.mjs +11 -11
  28. package/cli/lib/audit-sweep-runtime/closeout.mjs +8 -8
  29. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +3 -3
  30. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +10 -10
  31. package/cli/lib/audit-sweep-runtime/common.mjs +7 -7
  32. package/cli/lib/audit-sweep-runtime/format.mjs +3 -3
  33. package/cli/lib/audit-sweep-runtime/ingest.mjs +8 -8
  34. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +24 -27
  35. package/cli/lib/audit-sweep-runtime/inventory.mjs +58 -18
  36. package/cli/lib/audit-sweep-runtime/ledger.mjs +1 -1
  37. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +2 -2
  38. package/cli/lib/audit-sweep-runtime/remediation.mjs +6 -6
  39. package/cli/lib/audit-sweep-runtime/rerun.mjs +6 -6
  40. package/cli/lib/audit-sweep-runtime/status.mjs +1 -1
  41. package/cli/lib/audit-sweep-runtime/validators.mjs +2 -2
  42. package/cli/lib/authority-convergence.mjs +397 -2
  43. package/cli/lib/blueprint-audit.mjs +5 -5
  44. package/cli/lib/closeout.mjs +126 -3
  45. package/cli/lib/contracts.mjs +21 -17
  46. package/cli/lib/handoff.mjs +29 -11
  47. package/cli/lib/high-risk-admission.mjs +60 -11
  48. package/cli/lib/high-risk-decision.mjs +31 -2
  49. package/cli/lib/high-risk-ingest.mjs +5 -1
  50. package/cli/lib/high-risk-review.mjs +5 -1
  51. package/cli/lib/internal/contracts-parse.mjs +195 -24
  52. package/cli/lib/internal/contracts-validators.mjs +3 -2
  53. package/cli/lib/internal/doctor-bootstrap-surface.mjs +82 -35
  54. package/cli/lib/internal/doctor-delegated-surface.mjs +1 -1
  55. package/cli/lib/internal/doctor-finalize.mjs +12 -8
  56. package/cli/lib/internal/doctor-inspectors.mjs +34 -1
  57. package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +74 -12
  58. package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +24 -6
  59. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +18 -23
  60. package/cli/lib/internal/surface-taxonomy-validators.mjs +931 -0
  61. package/cli/lib/internal/validators-spec.mjs +229 -20
  62. package/cli/lib/sweep-design-runtime/common.mjs +246 -0
  63. package/cli/lib/sweep-design-runtime/engine.mjs +733 -0
  64. package/cli/lib/sweep-design-runtime/fix-topic.mjs +414 -0
  65. package/cli/lib/sweep-design-runtime/lifecycle.mjs +54 -0
  66. package/cli/lib/sweep-design-runtime/results.mjs +324 -0
  67. package/cli/lib/sweep-design.mjs +8 -0
  68. package/cli/lib/sync.mjs +143 -0
  69. package/cli/lib/topic-artifacts.mjs +186 -0
  70. package/cli/lib/topic-authority-coverage.mjs +73 -0
  71. package/cli/lib/topic-closeout.mjs +560 -0
  72. package/cli/lib/topic-common.mjs +404 -0
  73. package/cli/lib/topic-decisions.mjs +332 -0
  74. package/cli/lib/topic-draft-packets.mjs +126 -7
  75. package/cli/lib/topic-execution.mjs +515 -0
  76. package/cli/lib/topic-goal.mjs +112 -33
  77. package/cli/lib/topic-ledger.mjs +281 -0
  78. package/cli/lib/topic-lifecycle-artifacts.mjs +173 -0
  79. package/cli/lib/topic-root-validation.mjs +288 -0
  80. package/cli/lib/topic-runner-commands.mjs +174 -0
  81. package/cli/lib/topic-runner-deferral.mjs +532 -0
  82. package/cli/lib/topic-runner-stale-gates.mjs +114 -0
  83. package/cli/lib/topic-runner-validation.mjs +138 -0
  84. package/cli/lib/topic-runner.mjs +109 -154
  85. package/cli/lib/topic-scaffold.mjs +252 -0
  86. package/cli/lib/topic-waves.mjs +403 -0
  87. package/cli/lib/topic.mjs +81 -93
  88. package/cli/lib/value-helpers.mjs +6 -1
  89. package/cli/seeds/bootstrap.mjs +96 -20
  90. package/cli/seeds/seed-policy.yaml +67 -0
  91. package/config/bootstrap.yaml +1 -1
  92. package/config/skill-manifest.yaml +4 -2
  93. package/config/spec-generation-inputs.yaml +41 -19
  94. package/contracts/audit-remediation-map.schema.yaml +1 -0
  95. package/contracts/audit-sweep-result.yaml +4 -0
  96. package/contracts/domain-admission.schema.yaml +56 -0
  97. package/contracts/migration-inventory.schema.yaml +80 -0
  98. package/contracts/negative-fixtures.yaml +91 -0
  99. package/contracts/placement-contract.schema.yaml +163 -0
  100. package/contracts/projection-edge.schema.yaml +130 -0
  101. package/contracts/shared-enums.yaml +68 -0
  102. package/contracts/spec-generation-audit.schema.yaml +19 -4
  103. package/contracts/spec-generation-inputs.schema.yaml +130 -29
  104. package/contracts/spec-reconstruction-result.yaml +9 -5
  105. package/contracts/surface-taxonomy.schema.yaml +201 -0
  106. package/contracts/sweep-design-result.yaml +349 -0
  107. package/contracts/table-family.schema.yaml +114 -0
  108. package/contracts/topic-goal.schema.yaml +10 -1
  109. package/contracts/tracked-output-admission.schema.yaml +70 -0
  110. package/contracts/workflow-consumer.schema.yaml +112 -0
  111. package/methodology/audit-sweep-p0p1-recall.yaml +1 -1
  112. package/methodology/spec-reconstruction.yaml +53 -30
  113. package/package.json +5 -4
  114. package/spec/_meta/command-gating-matrix.yaml +33 -0
  115. package/spec/_meta/generate-drift-migration-checklist.yaml +44 -62
  116. package/spec/_meta/governance-routing-cutover-checklist.yaml +3 -3
  117. package/spec/_meta/phase2-impacted-surface-matrix.yaml +14 -14
  118. package/spec/_meta/spec-authority-cutover-readiness.yaml +3 -5
  119. package/spec/_meta/spec-tree-model.yaml +104 -36
  120. package/spec/bootstrap-state.yaml +36 -36
  121. package/spec/product-scope.yaml +13 -10
@@ -0,0 +1,414 @@
1
+ import { mkdir, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import YAML from "yaml";
5
+
6
+ import { addWaveToTopic, admitWaveInTopic, createTopic, selectWaveInTopic } from "../topic.mjs";
7
+ import { sweepDesignWaveAuthorityRefs } from "../topic-authority-coverage.mjs";
8
+ import { assertDesignArtifact, designRef, inputError, nowIso, requireRunId } from "./common.mjs";
9
+
10
+ function inventoryRef(runId) {
11
+ return designRef(runId, "inventory.yaml");
12
+ }
13
+
14
+ function ledgerRef(runId) {
15
+ return designRef(runId, "revision-ledger.yaml");
16
+ }
17
+
18
+ function finalStateReportRef(runId) {
19
+ return designRef(runId, "final-state-report.yaml");
20
+ }
21
+
22
+ function wavePlanRef(runId) {
23
+ return designRef(runId, "wave-plan.yaml");
24
+ }
25
+
26
+ function toPortableRelativePath(filePath) {
27
+ return filePath.split(path.sep).join("/");
28
+ }
29
+
30
+ function stableSlug(value, fallback = "sweep-fix") {
31
+ const slug = String(value ?? "")
32
+ .toLowerCase()
33
+ .replace(/[^a-z0-9]+/g, "-")
34
+ .replace(/^-+|-+$/g, "")
35
+ .replace(/-{2,}/g, "-");
36
+ return slug || fallback;
37
+ }
38
+
39
+ function titleFromSlug(value) {
40
+ return stableSlug(value)
41
+ .split("-")
42
+ .map((part) => `${part.charAt(0).toUpperCase()}${part.slice(1)}`)
43
+ .join(" ");
44
+ }
45
+
46
+ function topicWaveFromSweepDesignWave(wave, context) {
47
+ const authorityRefs = sweepDesignWaveAuthorityRefs(wave);
48
+ return {
49
+ wave_id: wave.wave_id,
50
+ slug: stableSlug(wave.wave_id.replace(/^wave-/, ""), wave.wave_id),
51
+ state: "candidate",
52
+ primary_closure_goal: wave.scope,
53
+ deps: Array.isArray(wave.dependencies) ? wave.dependencies : [],
54
+ owner_domain: wave.owner_domain,
55
+ parallelizable_after: [],
56
+ selected: false,
57
+ source_sweep_design: {
58
+ run_id: context.runId,
59
+ wave_plan_ref: context.wavePlanRef,
60
+ final_state_report_ref: context.finalStateReportRef,
61
+ source_revision_ledger_ref: context.sourceRevisionLedgerRef,
62
+ source_inventory_ref: context.sourceInventoryRef,
63
+ source_design_packet_refs: wave.source_design_packet_refs ?? [],
64
+ design_auditor_result_refs: wave.design_auditor_result_refs ?? [],
65
+ revision_ledger_entry_refs: wave.revision_ledger_entry_refs ?? [],
66
+ finding_ids: wave.finding_ids ?? [],
67
+ merged_cluster_ids: wave.merged_cluster_ids ?? [],
68
+ merged_root_cause_keys: wave.merged_root_cause_keys ?? [],
69
+ authority_owner: authorityRefs.length > 0 ? authorityRefs : wave.authority_owner,
70
+ source_authority_refs: authorityRefs,
71
+ source_authority_coverage_policy: "authority_owner_and_canonical_seams_must_cover_union_of_source_sweep_design_authority_refs",
72
+ preflight_ref: wave.preflight_ref,
73
+ validation_commands: wave.validation_commands ?? [],
74
+ negative_checks: wave.negative_checks ?? [],
75
+ drift_resistance_checks: wave.drift_resistance_checks ?? [],
76
+ closeout_criteria: wave.closeout_criteria ?? [],
77
+ blocked_gate_refs: wave.blocked_gate_refs ?? [],
78
+ non_goals: wave.non_goals ?? [],
79
+ consolidation_rationale: wave.consolidation_rationale ?? null,
80
+ isolation_justification: wave.isolation_justification ?? null,
81
+ source_design_auditor_result_ref: wave.source_design_auditor_result_ref ?? null,
82
+ },
83
+ };
84
+ }
85
+
86
+ function buildSweepFixReadme(topicId, title, context) {
87
+ return `# ${title}
88
+
89
+ State: generated sweep-fix topic
90
+
91
+ This topic was materialized from a completed sweep design run. It is the implementation carrier for the deterministic waves produced by the LLM sweep-design process.
92
+
93
+ ## Source
94
+
95
+ - Topic id: ${topicId}
96
+ - Sweep design run: ${context.runId}
97
+ - Final state report: ${context.finalStateReportRef}
98
+ - Wave plan: ${context.wavePlanRef}
99
+ - Wave count: ${context.waveCount}
100
+ - Source findings mutation: forbidden
101
+
102
+ ## Contract
103
+
104
+ The source audit findings remain immutable evidence. This topic owns implementation planning and execution state only. Each wave in topic.yaml carries a source_sweep_design block, and sweep-fix-wave-catalog.yaml preserves the full wave-plan payload for review, packet creation, validation, and closeout.
105
+ `;
106
+ }
107
+
108
+ function buildSweepFixDesign(topicId, context) {
109
+ return `# Sweep Fix Design
110
+
111
+ Topic: ${topicId}
112
+
113
+ ## Product Shape
114
+
115
+ This topic is generated from sweep design, not hand-authored from a prose summary. The source run has already produced final finding outcomes and deterministic implementation waves.
116
+
117
+ ## Authority Boundary
118
+
119
+ - Source findings are read-only input evidence.
120
+ - Implementation state belongs to this topic.
121
+ - Wave execution must use the wave's source_sweep_design provenance, validation commands, negative checks, and closeout criteria.
122
+ - No compatibility or legacy migration surface is implied by this generated topic.
123
+
124
+ ## Source Artifacts
125
+
126
+ - Inventory: ${context.sourceInventoryRef}
127
+ - Final state report: ${context.finalStateReportRef}
128
+ - Revision ledger: ${context.sourceRevisionLedgerRef}
129
+ - Wave plan: ${context.wavePlanRef}
130
+ `;
131
+ }
132
+
133
+ function buildSweepFixPreflight(topicId, context) {
134
+ return `# Preflight
135
+
136
+ Topic: ${topicId}
137
+
138
+ ## Spec Status
139
+
140
+ - preflight-required
141
+
142
+ ## Authority Owner
143
+
144
+ - Selected-wave authority owner is read from topic.yaml waves[].source_sweep_design.authority_owner.
145
+ - Topic-wide authority input is .nimi/spec/** plus each generated wave's source_sweep_design block.
146
+
147
+ ## Work Type
148
+
149
+ - redesign
150
+
151
+ ## Status
152
+
153
+ This topic contains ${context.waveCount} generated sweep-fix waves from sweep design run ${context.runId}.
154
+
155
+ ## Admission Rule
156
+
157
+ Wave admission is handled by nimicoding topic. A generated wave may move from candidate to preflight_admitted only when its dependencies are closed and its packet/preflight evidence has been frozen from source_sweep_design.
158
+
159
+ ## Stop Line
160
+
161
+ - Do not mutate source audit findings.
162
+ - Do not dispatch implementation from the sweep design run directory.
163
+ - Do not treat sweep-fix-wave-catalog.yaml as a parallel topic state; topic.yaml is the lifecycle registry.
164
+ - Do not drop validation commands, negative checks, drift checks, or closeout criteria when freezing packets.
165
+
166
+ ## Human Gates
167
+
168
+ - Product, authority, semantic, or evidence forks must stop for human decision before the affected wave can close.
169
+ - All-mode execution may queue independent gates, but gated waves remain pending or blocked until the decision is recorded.
170
+
171
+ ## Validation Commands
172
+
173
+ - pnpm exec nimicoding topic validate ${topicId} --json
174
+ - pnpm exec nimicoding topic validate graph ${topicId} --json
175
+ - pnpm exec nimicoding topic goal ${topicId} --json is expected to emit a goal once a selected wave reaches preflight_admitted or a later execution-stage state.
176
+ `;
177
+ }
178
+
179
+ function buildSweepFixWaves(topicId, waves) {
180
+ const rows = waves
181
+ .map((wave) => {
182
+ const deps = Array.isArray(wave.dependencies) && wave.dependencies.length > 0 ? wave.dependencies.join(", ") : "none";
183
+ const findings = Array.isArray(wave.finding_ids) ? wave.finding_ids.length : 0;
184
+ return `| ${wave.wave_id} | ${wave.owner_domain} | ${findings} | ${deps} |`;
185
+ })
186
+ .join("\n");
187
+ return `# Waves
188
+
189
+ Topic: ${topicId}
190
+
191
+ The machine lifecycle registry lives in topic.yaml. Full source design context lives in sweep-fix-wave-catalog.yaml.
192
+
193
+ | Wave | Owner | Findings | Dependencies |
194
+ |---|---|---:|---|
195
+ ${rows}
196
+ `;
197
+ }
198
+
199
+ function buildSweepFixCandidateWavePlan(topicId, context) {
200
+ return `# Candidate Wave Plan
201
+
202
+ Topic: ${topicId}
203
+
204
+ The candidate wave set is generated from ${context.wavePlanRef}. There are ${context.waveCount} candidate implementation waves. The first executable wave should be selected by nimicoding topic after dependency checks and packet freeze, not by editing the source sweep-design artifacts.
205
+ `;
206
+ }
207
+
208
+ function buildSweepFixCloseout(topicId) {
209
+ return `# Closeout
210
+
211
+ Topic: ${topicId}
212
+
213
+ ## Wave-1 Closeout Requirements
214
+
215
+ - complete: selected wave has implementation result lineage, validation evidence, negative-check evidence, drift-resistance evidence, and source_sweep_design provenance.
216
+ - partial: selected wave has bounded residual work recorded with explicit downstream wave refs.
217
+ - blocked: selected wave has unresolved product, authority, semantic, or evidence gates.
218
+ - pending: selected wave is waiting on external evidence or human decision.
219
+
220
+ ## Wave Closeout
221
+
222
+ Each wave must close with packet lineage, implementation result lineage, validation command evidence, negative check evidence, drift resistance evidence, and source_sweep_design provenance retained.
223
+
224
+ ## Topic Closeout
225
+
226
+ The topic may close only after all generated waves are closed, retired by explicit supersession, or blocked with a recorded human decision.
227
+ `;
228
+ }
229
+
230
+ function buildSweepFixImplementationDoctrine(topicId) {
231
+ return `# Implementation Doctrine
232
+
233
+ Topic: ${topicId}
234
+
235
+ Implementation must be hard-cut, complete, and authority-aligned. No pseudo-success, compatibility shim, legacy alias, app-local shadow truth, or partial closure is acceptable. Runtime/SDK/Desktop/Web boundaries remain governed by repository authority and by each wave's validation and closeout criteria.
236
+ `;
237
+ }
238
+
239
+ function buildSweepFixAdmissionChecklists(topicId) {
240
+ return `# Admission Checklists
241
+
242
+ Topic: ${topicId}
243
+
244
+ ## Before Admitting A Wave
245
+
246
+ - The wave exists in topic.yaml.
247
+ - All dependencies are closed.
248
+ - source_sweep_design includes design packet refs, auditor result refs, revision ledger refs, validation commands, negative checks, drift checks, and closeout criteria.
249
+ - Any blocked gate refs have recorded human decisions.
250
+ - A packet/preflight artifact has been frozen for this wave.
251
+ `;
252
+ }
253
+
254
+ function buildSweepFixManagerSessionProtocol(topicId) {
255
+ return `# Manager Session Protocol
256
+
257
+ Topic: ${topicId}
258
+
259
+ 1. Use nimicoding topic to select, admit, run, and close waves.
260
+ 2. Read sweep-fix-wave-catalog.yaml and the selected wave's source_sweep_design before freezing a packet.
261
+ 3. Stop for human confirmation only on product, authority, semantic, or evidence forks.
262
+ 4. In all-mode execution, queue human decisions and continue independent waves; resolve queued decisions before closing affected waves.
263
+ `;
264
+ }
265
+
266
+ function buildSweepFixManagerPrompts(topicId) {
267
+ return `# Manager Prompts
268
+
269
+ Topic: ${topicId}
270
+
271
+ ## Packet Freeze Prompt
272
+
273
+ Use the selected topic wave and its source_sweep_design block to produce an implementation packet. Preserve the source design packet refs, auditor result refs, revision ledger refs, validation commands, negative checks, drift checks, closeout criteria, non-goals, and blocked gates. Do not mutate source findings.
274
+ `;
275
+ }
276
+
277
+ async function writeSweepFixTopicArtifacts(topicDir, topicId, title, context, waves) {
278
+ await mkdir(path.join(topicDir, "sweep-fix"), { recursive: true });
279
+ const catalog = {
280
+ version: 1,
281
+ kind: "sweep-fix-wave-catalog",
282
+ topic_id: topicId,
283
+ source_sweep_design_run_id: context.runId,
284
+ source_inventory_ref: context.sourceInventoryRef,
285
+ source_final_state_report_ref: context.finalStateReportRef,
286
+ source_revision_ledger_ref: context.sourceRevisionLedgerRef,
287
+ source_wave_plan_ref: context.wavePlanRef,
288
+ source_findings_mutation_policy: "read_only_never_update_from_sweep_fix_topic",
289
+ wave_count: waves.length,
290
+ waves,
291
+ created_at: context.createdAt,
292
+ };
293
+ const source = {
294
+ version: 1,
295
+ kind: "sweep-fix-topic-source",
296
+ topic_id: topicId,
297
+ source_sweep_design_run_id: context.runId,
298
+ source_inventory_ref: context.sourceInventoryRef,
299
+ source_final_state_report_ref: context.finalStateReportRef,
300
+ source_revision_ledger_ref: context.sourceRevisionLedgerRef,
301
+ source_wave_plan_ref: context.wavePlanRef,
302
+ source_findings_mutation_policy: "read_only_never_update_from_sweep_fix_topic",
303
+ final_state_complete: true,
304
+ wave_count: waves.length,
305
+ created_at: context.createdAt,
306
+ };
307
+ const files = new Map([
308
+ ["README.md", buildSweepFixReadme(topicId, title, context)],
309
+ ["design.md", buildSweepFixDesign(topicId, context)],
310
+ ["preflight.md", buildSweepFixPreflight(topicId, context)],
311
+ ["waves.md", buildSweepFixWaves(topicId, waves)],
312
+ ["candidate-wave-plan.md", buildSweepFixCandidateWavePlan(topicId, context)],
313
+ ["closeout.md", buildSweepFixCloseout(topicId)],
314
+ ["implementation-doctrine.md", buildSweepFixImplementationDoctrine(topicId)],
315
+ ["admission-checklists.md", buildSweepFixAdmissionChecklists(topicId)],
316
+ ["manager-session-protocol.md", buildSweepFixManagerSessionProtocol(topicId)],
317
+ ["manager-prompts.md", buildSweepFixManagerPrompts(topicId)],
318
+ ["sweep-fix/source.yaml", YAML.stringify(source)],
319
+ ["sweep-fix/wave-catalog.yaml", YAML.stringify(catalog)],
320
+ ]);
321
+ for (const [fileName, contents] of files.entries()) {
322
+ await writeFile(path.join(topicDir, fileName), contents.endsWith("\n") ? contents : `${contents}\n`, "utf8");
323
+ }
324
+ }
325
+
326
+ async function loadInventory(projectRoot, runId) {
327
+ return assertDesignArtifact(projectRoot, inventoryRef(runId), "sweep-design-inventory", "inventory");
328
+ }
329
+
330
+ export async function runFixTopic(projectRoot, options) {
331
+ const run = requireRunId(options);
332
+ if (!run.ok) return run;
333
+ const inventory = await loadInventory(projectRoot, run.runId);
334
+ if (!inventory.ok) return inventory;
335
+ const finalReport = await assertDesignArtifact(projectRoot, finalStateReportRef(run.runId), "sweep-design-final-state-report", "final state report");
336
+ if (!finalReport.ok) return finalReport;
337
+ if (finalReport.value.complete !== true) {
338
+ return inputError("nimicoding sweep design fix-topic refused: final-state-report is not complete.\n");
339
+ }
340
+ const wavePlan = await assertDesignArtifact(projectRoot, wavePlanRef(run.runId), "sweep-design-wave-plan", "wave plan");
341
+ if (!wavePlan.ok) return wavePlan;
342
+ const waves = Array.isArray(wavePlan.value.waves) ? wavePlan.value.waves : [];
343
+ if (waves.length === 0) return inputError("nimicoding sweep design fix-topic refused: wave-plan contains no implementation waves.\n");
344
+ if (wavePlan.value.wave_count !== waves.length) {
345
+ return inputError("nimicoding sweep design fix-topic refused: wave-plan wave_count does not match waves[].\n");
346
+ }
347
+ const waveIds = new Set();
348
+ for (const wave of waves) {
349
+ if (waveIds.has(wave.wave_id)) return inputError(`nimicoding sweep design fix-topic refused: duplicate wave id ${wave.wave_id}.\n`);
350
+ waveIds.add(wave.wave_id);
351
+ }
352
+ const slug = stableSlug(options.slug ?? `sweep-fix-${run.runId}`, `sweep-fix-${run.runId}`);
353
+ const title = options.title ?? titleFromSlug(slug);
354
+ const now = options.verifiedAt ? new Date(options.verifiedAt) : new Date();
355
+ if (Number.isNaN(now.getTime())) return inputError("nimicoding sweep design fix-topic refused: --verified-at must be an ISO timestamp.\n");
356
+ const created = await createTopic(projectRoot, {
357
+ slug,
358
+ title,
359
+ now,
360
+ mode: "landed",
361
+ posture: "no_legacy_hard_cut",
362
+ designPolicy: "complete_contract_first",
363
+ parallelTruth: "forbidden",
364
+ layering: "ontology",
365
+ risk: "high",
366
+ applicability: "complex_remediation",
367
+ justification: `sweep design run ${run.runId} produced a complete final-state report and ${waves.length} deterministic implementation waves`,
368
+ executionMode: "manager_worker_auditor",
369
+ });
370
+ if (!created.ok) return created;
371
+ const context = {
372
+ runId: run.runId,
373
+ sourceInventoryRef: inventory.ref,
374
+ finalStateReportRef: finalReport.ref,
375
+ sourceRevisionLedgerRef: wavePlan.value.source_revision_ledger_ref ?? finalReport.value.source_revision_ledger_ref ?? ledgerRef(run.runId),
376
+ wavePlanRef: wavePlan.ref,
377
+ waveCount: waves.length,
378
+ createdAt: options.verifiedAt ?? nowIso(),
379
+ };
380
+ const topicWaves = waves.map((wave) => topicWaveFromSweepDesignWave(wave, context));
381
+ for (const topicWave of topicWaves) {
382
+ const added = await addWaveToTopic(projectRoot, created.topicId, topicWave);
383
+ if (!added.ok) return added;
384
+ }
385
+ await writeSweepFixTopicArtifacts(created.topicDir, created.topicId, title, context, waves);
386
+ let admittedWaveId = null;
387
+ let finalTopicRef = created.topicRef;
388
+ let finalTopicState = created.state;
389
+ const admitWaveId = options.admitWaveId ?? (options.admitFirstWave ? topicWaves.find((wave) => wave.deps.length === 0)?.wave_id : null);
390
+ if (admitWaveId) {
391
+ if (!waveIds.has(admitWaveId)) return inputError(`nimicoding sweep design fix-topic refused: --admit-wave-id does not exist in the generated wave plan: ${admitWaveId}.\n`);
392
+ const selected = await selectWaveInTopic(projectRoot, created.topicId, admitWaveId);
393
+ if (!selected.ok) return selected;
394
+ const admitted = await admitWaveInTopic(projectRoot, created.topicId, admitWaveId);
395
+ if (!admitted.ok) return admitted;
396
+ admittedWaveId = admitted.waveId;
397
+ finalTopicRef = admitted.topicRef;
398
+ finalTopicState = admitted.state;
399
+ }
400
+ return {
401
+ ok: true,
402
+ exitCode: 0,
403
+ runId: run.runId,
404
+ topicId: created.topicId,
405
+ topicRef: finalTopicRef,
406
+ state: finalTopicState,
407
+ sourceRef: toPortableRelativePath(path.join(finalTopicRef, "sweep-fix", "source.yaml")),
408
+ waveCatalogRef: toPortableRelativePath(path.join(finalTopicRef, "sweep-fix", "wave-catalog.yaml")),
409
+ wavePlanRef: wavePlan.ref,
410
+ waveCount: waves.length,
411
+ materializedWaveIds: topicWaves.map((wave) => wave.wave_id),
412
+ admittedWaveId,
413
+ };
414
+ }
@@ -0,0 +1,54 @@
1
+ import { DESIGN_STATES, TERMINAL_STATES } from "./common.mjs";
2
+
3
+ const TRANSITIONS = new Map([
4
+ ["raw", new Set(["confirmed", "duplicate", "superseded", "false_positive", "needs_more_audit"])],
5
+ ["confirmed", new Set([
6
+ "needs_user_decision",
7
+ "needs_authority_alignment",
8
+ "needs_design",
9
+ "ready_for_implementation_wave",
10
+ "blocked",
11
+ ])],
12
+ ["needs_more_audit", new Set(["confirmed", "false_positive", "superseded", "blocked"])],
13
+ ["needs_user_decision", new Set(["confirmed", "ready_for_implementation_wave", "blocked"])],
14
+ ["needs_authority_alignment", new Set(["confirmed", "ready_for_implementation_wave", "blocked"])],
15
+ ["needs_design", new Set(["confirmed", "ready_for_implementation_wave", "blocked"])],
16
+ ["ready_for_implementation_wave", new Set(["blocked"])],
17
+ ]);
18
+
19
+ function needsUserGate(toState) {
20
+ return toState === "false_positive";
21
+ }
22
+
23
+ function needsNextArtifact(fromState, toState) {
24
+ return fromState.startsWith("needs_") || [
25
+ "needs_more_audit",
26
+ "needs_user_decision",
27
+ "needs_authority_alignment",
28
+ "needs_design",
29
+ "ready_for_implementation_wave",
30
+ ].includes(toState);
31
+ }
32
+
33
+ export function validateLifecycleTransition({ fromState, toState, userGateRef, nextArtifactRef }) {
34
+ if (!DESIGN_STATES.has(fromState) || !DESIGN_STATES.has(toState)) {
35
+ return `unsupported lifecycle transition ${fromState} -> ${toState}`;
36
+ }
37
+ if (TERMINAL_STATES.has(fromState)) {
38
+ return `terminal finding state cannot transition: ${fromState}`;
39
+ }
40
+ if (!TRANSITIONS.get(fromState)?.has(toState)) {
41
+ return `illegal lifecycle transition ${fromState} -> ${toState}`;
42
+ }
43
+ if (needsUserGate(toState) && !userGateRef) {
44
+ return `${toState} requires --user-gate-ref`;
45
+ }
46
+ if (needsNextArtifact(fromState, toState) && !nextArtifactRef) {
47
+ return `${fromState} -> ${toState} requires --next-artifact-ref`;
48
+ }
49
+ return null;
50
+ }
51
+
52
+ export function terminalStates() {
53
+ return [...TERMINAL_STATES].sort();
54
+ }