@nimiplatform/nimi-coding 0.1.0 → 0.2.1

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 (126) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/CODE_OF_CONDUCT.md +28 -0
  3. package/CONTRIBUTING.md +45 -0
  4. package/README.md +371 -344
  5. package/README.zh-CN.md +307 -0
  6. package/SECURITY.md +26 -0
  7. package/adapters/oh-my-codex/README.md +8 -9
  8. package/cli/commands/audit-sweep.mjs +10 -10
  9. package/cli/commands/classify-spec-tree.mjs +5 -0
  10. package/cli/commands/closeout.mjs +3 -0
  11. package/cli/commands/generate-spec-derived-docs.mjs +20 -0
  12. package/cli/commands/generate-spec-migration-plan.mjs +30 -0
  13. package/cli/commands/start.mjs +5 -1
  14. package/cli/commands/surface-validator-command.mjs +49 -0
  15. package/cli/commands/sweep-design.mjs +295 -0
  16. package/cli/commands/sweep.mjs +22 -0
  17. package/cli/commands/sync.mjs +132 -0
  18. package/cli/commands/topic-formatters.mjs +8 -8
  19. package/cli/commands/validate-ai-governance.mjs +167 -46
  20. package/cli/commands/validate-domain-admission.mjs +5 -0
  21. package/cli/commands/validate-guidance-bodies.mjs +5 -0
  22. package/cli/commands/validate-placement.mjs +5 -0
  23. package/cli/commands/validate-projection-edges.mjs +5 -0
  24. package/cli/commands/validate-spec-audit.mjs +5 -1
  25. package/cli/commands/validate-table-family.mjs +5 -0
  26. package/cli/commands/validate-tracked-output-admission.mjs +5 -0
  27. package/cli/constants.mjs +5 -49
  28. package/cli/help.mjs +33 -11
  29. package/cli/index.mjs +20 -2
  30. package/cli/lib/audit-sweep-runtime/admissions.mjs +38 -29
  31. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +8 -0
  32. package/cli/lib/audit-sweep-runtime/chunks.mjs +11 -11
  33. package/cli/lib/audit-sweep-runtime/closeout.mjs +8 -8
  34. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +3 -3
  35. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +10 -10
  36. package/cli/lib/audit-sweep-runtime/common.mjs +7 -7
  37. package/cli/lib/audit-sweep-runtime/format.mjs +3 -3
  38. package/cli/lib/audit-sweep-runtime/ingest.mjs +8 -8
  39. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +24 -27
  40. package/cli/lib/audit-sweep-runtime/inventory.mjs +58 -18
  41. package/cli/lib/audit-sweep-runtime/ledger.mjs +1 -1
  42. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +2 -2
  43. package/cli/lib/audit-sweep-runtime/remediation.mjs +6 -6
  44. package/cli/lib/audit-sweep-runtime/rerun.mjs +6 -6
  45. package/cli/lib/audit-sweep-runtime/status.mjs +1 -1
  46. package/cli/lib/audit-sweep-runtime/validators.mjs +2 -2
  47. package/cli/lib/authority-convergence.mjs +397 -2
  48. package/cli/lib/blueprint-audit.mjs +5 -5
  49. package/cli/lib/closeout.mjs +126 -3
  50. package/cli/lib/contracts.mjs +21 -17
  51. package/cli/lib/handoff.mjs +29 -11
  52. package/cli/lib/high-risk-admission.mjs +60 -11
  53. package/cli/lib/high-risk-decision.mjs +31 -2
  54. package/cli/lib/high-risk-ingest.mjs +5 -1
  55. package/cli/lib/high-risk-review.mjs +5 -1
  56. package/cli/lib/internal/contracts-parse.mjs +195 -24
  57. package/cli/lib/internal/contracts-validators.mjs +3 -2
  58. package/cli/lib/internal/doctor-bootstrap-surface.mjs +82 -35
  59. package/cli/lib/internal/doctor-delegated-surface.mjs +1 -1
  60. package/cli/lib/internal/doctor-finalize.mjs +12 -8
  61. package/cli/lib/internal/doctor-inspectors.mjs +34 -1
  62. package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +74 -12
  63. package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +24 -6
  64. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +18 -23
  65. package/cli/lib/internal/surface-taxonomy-validators.mjs +931 -0
  66. package/cli/lib/internal/validators-spec.mjs +229 -20
  67. package/cli/lib/sweep-design-runtime/common.mjs +246 -0
  68. package/cli/lib/sweep-design-runtime/engine.mjs +733 -0
  69. package/cli/lib/sweep-design-runtime/fix-topic.mjs +414 -0
  70. package/cli/lib/sweep-design-runtime/lifecycle.mjs +54 -0
  71. package/cli/lib/sweep-design-runtime/results.mjs +324 -0
  72. package/cli/lib/sweep-design.mjs +8 -0
  73. package/cli/lib/sync.mjs +143 -0
  74. package/cli/lib/topic-artifacts.mjs +186 -0
  75. package/cli/lib/topic-authority-coverage.mjs +73 -0
  76. package/cli/lib/topic-closeout.mjs +560 -0
  77. package/cli/lib/topic-common.mjs +404 -0
  78. package/cli/lib/topic-decisions.mjs +332 -0
  79. package/cli/lib/topic-draft-packets.mjs +126 -7
  80. package/cli/lib/topic-execution.mjs +515 -0
  81. package/cli/lib/topic-goal.mjs +112 -33
  82. package/cli/lib/topic-ledger.mjs +281 -0
  83. package/cli/lib/topic-lifecycle-artifacts.mjs +173 -0
  84. package/cli/lib/topic-root-validation.mjs +288 -0
  85. package/cli/lib/topic-runner-commands.mjs +174 -0
  86. package/cli/lib/topic-runner-deferral.mjs +532 -0
  87. package/cli/lib/topic-runner-stale-gates.mjs +114 -0
  88. package/cli/lib/topic-runner-validation.mjs +138 -0
  89. package/cli/lib/topic-runner.mjs +109 -154
  90. package/cli/lib/topic-scaffold.mjs +252 -0
  91. package/cli/lib/topic-waves.mjs +403 -0
  92. package/cli/lib/topic.mjs +81 -93
  93. package/cli/lib/value-helpers.mjs +6 -1
  94. package/cli/seeds/bootstrap.mjs +96 -20
  95. package/cli/seeds/seed-policy.yaml +67 -0
  96. package/config/bootstrap.yaml +1 -1
  97. package/config/skill-manifest.yaml +4 -2
  98. package/config/spec-generation-inputs.yaml +41 -19
  99. package/contracts/audit-remediation-map.schema.yaml +1 -0
  100. package/contracts/audit-sweep-result.yaml +4 -0
  101. package/contracts/domain-admission.schema.yaml +56 -0
  102. package/contracts/migration-inventory.schema.yaml +80 -0
  103. package/contracts/negative-fixtures.yaml +91 -0
  104. package/contracts/placement-contract.schema.yaml +163 -0
  105. package/contracts/projection-edge.schema.yaml +130 -0
  106. package/contracts/shared-enums.yaml +68 -0
  107. package/contracts/spec-generation-audit.schema.yaml +19 -4
  108. package/contracts/spec-generation-inputs.schema.yaml +130 -29
  109. package/contracts/spec-reconstruction-result.yaml +9 -5
  110. package/contracts/surface-taxonomy.schema.yaml +201 -0
  111. package/contracts/sweep-design-result.yaml +349 -0
  112. package/contracts/table-family.schema.yaml +121 -0
  113. package/contracts/topic-goal.schema.yaml +10 -1
  114. package/contracts/tracked-output-admission.schema.yaml +70 -0
  115. package/contracts/workflow-consumer.schema.yaml +112 -0
  116. package/methodology/audit-sweep-p0p1-recall.yaml +1 -1
  117. package/methodology/spec-reconstruction.yaml +53 -30
  118. package/package.json +19 -4
  119. package/spec/_meta/command-gating-matrix.yaml +33 -0
  120. package/spec/_meta/generate-drift-migration-checklist.yaml +44 -62
  121. package/spec/_meta/governance-routing-cutover-checklist.yaml +3 -3
  122. package/spec/_meta/phase2-impacted-surface-matrix.yaml +14 -14
  123. package/spec/_meta/spec-authority-cutover-readiness.yaml +3 -5
  124. package/spec/_meta/spec-tree-model.yaml +104 -36
  125. package/spec/bootstrap-state.yaml +36 -36
  126. package/spec/product-scope.yaml +13 -10
@@ -0,0 +1,324 @@
1
+ import path from "node:path";
2
+
3
+ import {
4
+ assertDesignArtifact,
5
+ FINAL_OUTCOME_STATES,
6
+ inputError,
7
+ loadYamlRef,
8
+ nowIso,
9
+ requireRunId,
10
+ safeDesignId,
11
+ TRANSIENT_STATES,
12
+ writeYamlRef,
13
+ } from "./common.mjs";
14
+ import {
15
+ decisionQueueRef,
16
+ finalOutcomeState,
17
+ finalStateReportRef,
18
+ isLlmAuditorResult,
19
+ isSyntheticAuditorResult,
20
+ ledgerRef,
21
+ loadInventory,
22
+ loadOrCreateLedger,
23
+ normalizeRevisionEntry,
24
+ packetRef,
25
+ resultRef,
26
+ stopClassForResult,
27
+ updateLedgerHashes,
28
+ validateAuditorResult,
29
+ validateLedger,
30
+ validateOutcome,
31
+ validatePacket,
32
+ validateWave,
33
+ wavePlanRef,
34
+ } from "./engine.mjs";
35
+
36
+ export async function runResultIngest(projectRoot, options) {
37
+ const run = requireRunId(options);
38
+ if (!run.ok) return run;
39
+ if (!options.from) return inputError("nimicoding sweep design refused: result-ingest requires --from.\n");
40
+ const result = await loadYamlRef(projectRoot, options.from) ?? await import("node:fs/promises").then(async ({ readFile }) => {
41
+ const YAML = await import("yaml");
42
+ return YAML.parse(await readFile(path.resolve(projectRoot, options.from), "utf8"));
43
+ }).catch(() => null);
44
+ const errors = validateAuditorResult(result);
45
+ if (errors.length > 0) return inputError(`nimicoding sweep design refused: ${errors.join("; ")}.\n`);
46
+ if (isSyntheticAuditorResult(result) && options.allowSyntheticTrial !== true) {
47
+ return inputError("nimicoding sweep design refused: synthetic_trial results require --allow-synthetic-trial and do not satisfy true LLM closeout.\n");
48
+ }
49
+ if (!safeDesignId(result.packet_id)) return inputError("nimicoding sweep design refused: result packet_id must be a stable id.\n");
50
+ if (!safeDesignId(result.result_id)) return inputError("nimicoding sweep design refused: result_id must be a stable id.\n");
51
+ if (result.run_id !== run.runId) return inputError("nimicoding sweep design refused: result run_id does not match --run-id.\n");
52
+ const packet = await assertDesignArtifact(projectRoot, packetRef(run.runId, result.packet_id), "sweep-design-design-auditor-packet", "design auditor packet");
53
+ if (!packet.ok) return packet;
54
+ if (result.packet_ref !== packet.ref) return inputError("nimicoding sweep design refused: result packet_ref does not match canonical packet artifact.\n");
55
+ const packetErrors = validatePacket(packet.value);
56
+ if (packetErrors.length > 0) return inputError(`nimicoding sweep design refused: ${packetErrors.join("; ")}.\n`);
57
+ const timestamp = options.verifiedAt ?? nowIso();
58
+ const ledger = await loadOrCreateLedger(projectRoot, run.runId, timestamp);
59
+ if (!ledger.ok) return ledger;
60
+ const previousSnapshotHash = ledger.value.ledger_snapshot_hash;
61
+ const normalizedRevisionEntries = [];
62
+ const revisionErrors = [];
63
+ const context = {
64
+ ledger: ledger.value,
65
+ normalized: normalizedRevisionEntries,
66
+ errors: revisionErrors,
67
+ resultId: result.result_id,
68
+ resultArtifactRef: resultRef(run.runId, result.result_id),
69
+ timestamp,
70
+ auditorProvenance: {
71
+ auditor: result.auditor,
72
+ auditor_family: result.auditor_family,
73
+ auditor_mode: result.auditor_mode,
74
+ auditor_result_origin: result.auditor_result_origin,
75
+ session_ref: result.session_ref,
76
+ transcript_ref: result.transcript_ref,
77
+ llm_session_ref: result.llm_session_ref ?? null,
78
+ llm_transcript_ref: result.llm_transcript_ref ?? null,
79
+ llm_prompt_ref: result.llm_prompt_ref ?? null,
80
+ packet_ref: result.packet_ref,
81
+ result_ref: resultRef(run.runId, result.result_id),
82
+ },
83
+ };
84
+ for (const entry of result.revision_entries) {
85
+ normalizedRevisionEntries.push(normalizeRevisionEntry(entry, context));
86
+ }
87
+ if (revisionErrors.length > 0) return inputError(`nimicoding sweep design refused: ${revisionErrors.join("; ")}.\n`);
88
+ const outcomeErrors = [];
89
+ for (const outcome of result.finding_outcomes) {
90
+ validateOutcome(outcome, { errors: outcomeErrors });
91
+ }
92
+ const includedFindingIds = new Set(packet.value.included_finding_ids);
93
+ const outcomeFindingIds = result.finding_outcomes.map((outcome) => outcome.finding_id);
94
+ const uniqueOutcomeFindingIds = new Set(outcomeFindingIds);
95
+ if (uniqueOutcomeFindingIds.size !== outcomeFindingIds.length) {
96
+ outcomeErrors.push("finding_outcomes[] contains duplicate finding_id entries");
97
+ }
98
+ const missingOutcomeIds = packet.value.included_finding_ids.filter((findingId) => !uniqueOutcomeFindingIds.has(findingId));
99
+ if (missingOutcomeIds.length > 0) {
100
+ outcomeErrors.push(`design auditor result is missing final outcomes for included findings: ${missingOutcomeIds.join(", ")}`);
101
+ }
102
+ const outOfPacketOutcomeIds = outcomeFindingIds.filter((findingId) => !includedFindingIds.has(findingId));
103
+ if (outOfPacketOutcomeIds.length > 0) {
104
+ outcomeErrors.push(`design auditor result includes outcomes outside the packet: ${[...new Set(outOfPacketOutcomeIds)].join(", ")}`);
105
+ }
106
+ const normalizedRevisionRefs = new Set(normalizedRevisionEntries.map((entry) => `${ledger.ref}#${entry.revision_entry_id}`));
107
+ for (const outcome of result.finding_outcomes) {
108
+ for (const ref of outcome.revision_ledger_entry_refs ?? []) {
109
+ if (!normalizedRevisionRefs.has(ref)) {
110
+ outcomeErrors.push(`finding_outcomes[] revision_ledger_entry_refs must reference revision entries from this ingest: ${ref}`);
111
+ }
112
+ }
113
+ }
114
+ if (outcomeErrors.length > 0) return inputError(`nimicoding sweep design refused: ${outcomeErrors.join("; ")}.\n`);
115
+ const storedResult = {
116
+ ...result,
117
+ closeout_eligible: isLlmAuditorResult(result),
118
+ ingested_at: timestamp,
119
+ normalized_revision_entry_refs: normalizedRevisionEntries.map((entry) => `${ledger.ref}#${entry.revision_entry_id}`),
120
+ };
121
+ const storedResultRef = await writeYamlRef(projectRoot, resultRef(run.runId, result.result_id), storedResult);
122
+ ledger.value.entries.push(...normalizedRevisionEntries);
123
+ updateLedgerHashes(ledger.value, previousSnapshotHash, timestamp);
124
+ await writeYamlRef(projectRoot, ledger.ref, ledger.value);
125
+ const decisionItems = result.finding_outcomes
126
+ .filter((outcome) => finalOutcomeState(outcome) === "needs_user_decision")
127
+ .map((outcome) => ({
128
+ decision_queue_item_ref: outcome.decision_queue_item_ref,
129
+ finding_id: outcome.finding_id,
130
+ decision_packet_ref: outcome.decision_packet_ref,
131
+ recommended_decision: outcome.recommended_decision,
132
+ queue_status: outcome.queue_status,
133
+ blocked_downstream_wave_refs: outcome.blocked_downstream_wave_refs,
134
+ }));
135
+ if (decisionItems.length > 0 || result.human_decision_requests.length > 0) {
136
+ await writeYamlRef(projectRoot, decisionQueueRef(run.runId), {
137
+ version: 2,
138
+ kind: "sweep-design-decision-queue",
139
+ run_id: run.runId,
140
+ source_design_auditor_result_ref: storedResultRef,
141
+ queue_policy: "focused_mode_stops_immediately_all_mode_batches_until_audit_complete",
142
+ original_findings_mutation_policy: "read_only_never_update_from_sweep_design",
143
+ pending_decision_count: decisionItems.length + result.human_decision_requests.length,
144
+ items: decisionItems,
145
+ human_decision_requests: result.human_decision_requests,
146
+ created_at: timestamp,
147
+ });
148
+ }
149
+ const stopClass = stopClassForResult(result);
150
+ const mode = options.mode ?? result.auditor_mode;
151
+ const focusedStop = mode === "focused" && stopClass;
152
+ return {
153
+ ok: true,
154
+ exitCode: focusedStop ? 2 : 0,
155
+ runId: run.runId,
156
+ resultRef: storedResultRef,
157
+ ledgerRef: ledger.ref,
158
+ revisionEntryCount: normalizedRevisionEntries.length,
159
+ findingOutcomeCount: result.finding_outcomes.length,
160
+ closeoutEligible: isLlmAuditorResult(result),
161
+ stopped: Boolean(focusedStop),
162
+ stopClass: focusedStop ? stopClass : null,
163
+ stopReason: focusedStop ? "focused_mode_requires_manager_or_human_resolution" : null,
164
+ };
165
+ }
166
+
167
+ export async function runLedgerValidate(projectRoot, options) {
168
+ const run = requireRunId(options);
169
+ if (!run.ok) return run;
170
+ const ledger = await assertDesignArtifact(projectRoot, ledgerRef(run.runId), "sweep-design-revision-ledger", "revision ledger");
171
+ if (!ledger.ok) return ledger;
172
+ const errors = validateLedger(ledger.value);
173
+ if (errors.length > 0) return inputError(`nimicoding sweep design refused: ${errors.join("; ")}.\n`);
174
+ return {
175
+ ok: true,
176
+ exitCode: 0,
177
+ runId: run.runId,
178
+ ledgerRef: ledger.ref,
179
+ revisionEntryCount: ledger.value.entries.length,
180
+ ledgerSnapshotHash: ledger.value.ledger_snapshot_hash,
181
+ };
182
+ }
183
+
184
+ export async function runFinalize(projectRoot, options) {
185
+ const run = requireRunId(options);
186
+ if (!run.ok) return run;
187
+ const inventory = await loadInventory(projectRoot, run.runId);
188
+ if (!inventory.ok) return inventory;
189
+ const ledger = await assertDesignArtifact(projectRoot, ledgerRef(run.runId), "sweep-design-revision-ledger", "revision ledger");
190
+ if (!ledger.ok) return ledger;
191
+ const ledgerErrors = validateLedger(ledger.value);
192
+ if (ledgerErrors.length > 0) return inputError(`nimicoding sweep design refused: ${ledgerErrors.join("; ")}.\n`);
193
+ const outcomes = new Map();
194
+ const resultRefs = new Set();
195
+ const nonLlmResultRefs = [];
196
+ for (const entry of ledger.value.entries) {
197
+ for (const ref of entry.replacement_artifact_refs ?? []) {
198
+ if (ref.includes("/design-auditor-results/")) resultRefs.add(ref);
199
+ }
200
+ }
201
+ for (const ref of resultRefs) {
202
+ const result = await loadYamlRef(projectRoot, ref);
203
+ if (result?.kind !== "sweep-design-design-auditor-result") continue;
204
+ if (!isLlmAuditorResult(result)) nonLlmResultRefs.push(ref);
205
+ for (const outcome of result.finding_outcomes ?? []) {
206
+ outcomes.set(outcome.finding_id, { ...outcome, source_result_ref: ref });
207
+ }
208
+ }
209
+ const findings = inventory.value.findings.map((finding) => {
210
+ const outcome = outcomes.get(finding.finding_id);
211
+ if (!outcome) {
212
+ return {
213
+ finding_id: finding.finding_id,
214
+ state: "raw",
215
+ final_outcome: false,
216
+ transient: true,
217
+ reason: "missing_design_auditor_final_outcome",
218
+ };
219
+ }
220
+ const state = finalOutcomeState(outcome);
221
+ return {
222
+ finding_id: finding.finding_id,
223
+ state,
224
+ final_outcome: FINAL_OUTCOME_STATES.has(state),
225
+ transient: TRANSIENT_STATES.has(state),
226
+ source_result_ref: outcome.source_result_ref,
227
+ design_auditor_packet_ref: outcome.design_auditor_packet_ref,
228
+ revision_ledger_entry_refs: outcome.revision_ledger_entry_refs,
229
+ };
230
+ });
231
+ const transient = findings.filter((finding) => finding.transient || !finding.final_outcome);
232
+ const finalOutcomeComplete = transient.length === 0;
233
+ const llmCloseoutEligible = nonLlmResultRefs.length === 0;
234
+ const complete = finalOutcomeComplete && (llmCloseoutEligible || options.allowSyntheticCloseout === true);
235
+ const artifact = {
236
+ version: 2,
237
+ kind: "sweep-design-final-state-report",
238
+ run_id: run.runId,
239
+ source_inventory_ref: inventory.ref,
240
+ source_revision_ledger_ref: ledger.ref,
241
+ final_state_policy: "every_finding_must_have_final_sweep_design_outcome_with_llm_packet_result_ledger_provenance",
242
+ source_findings_mutation_policy: "read_only_never_update_from_sweep_design",
243
+ complete,
244
+ final_outcome_complete: finalOutcomeComplete,
245
+ llm_closeout_eligible: llmCloseoutEligible,
246
+ non_llm_result_refs: nonLlmResultRefs,
247
+ total_finding_count: findings.length,
248
+ final_finding_count: findings.length - transient.length,
249
+ transient_finding_count: transient.length,
250
+ final_outcome_states: [...FINAL_OUTCOME_STATES].sort(),
251
+ transient_states: [...TRANSIENT_STATES].sort(),
252
+ blocking_findings: transient.map((finding) => ({
253
+ finding_id: finding.finding_id,
254
+ state: finding.state,
255
+ reason: finding.reason ?? "transient_or_missing_final_outcome",
256
+ })),
257
+ findings,
258
+ created_at: options.verifiedAt ?? nowIso(),
259
+ };
260
+ const ref = await writeYamlRef(projectRoot, finalStateReportRef(run.runId), artifact);
261
+ return {
262
+ ok: true,
263
+ exitCode: complete ? 0 : 2,
264
+ runId: run.runId,
265
+ finalStateReportRef: ref,
266
+ finalComplete: complete,
267
+ finalOutcomeComplete,
268
+ llmCloseoutEligible,
269
+ nonLlmResultCount: nonLlmResultRefs.length,
270
+ totalFindingCount: artifact.total_finding_count,
271
+ finalFindingCount: artifact.final_finding_count,
272
+ transientFindingCount: artifact.transient_finding_count,
273
+ stopped: !complete,
274
+ stopClass: complete ? null : finalOutcomeComplete ? "non_llm_result_provenance" : "incomplete_final_state",
275
+ stopReason: complete ? null : finalOutcomeComplete ? "synthetic_or_non_llm_results_do_not_satisfy_true_llm_closeout" : "missing_or_transient_final_outcomes_remaining",
276
+ };
277
+ }
278
+
279
+ export async function runWavePlan(projectRoot, options) {
280
+ const run = requireRunId(options);
281
+ if (!run.ok) return run;
282
+ if (!options.topicId) return inputError("nimicoding sweep design refused: --topic-id is required.\n");
283
+ const ledger = await assertDesignArtifact(projectRoot, ledgerRef(run.runId), "sweep-design-revision-ledger", "revision ledger");
284
+ if (!ledger.ok) return ledger;
285
+ const ledgerErrors = validateLedger(ledger.value);
286
+ if (ledgerErrors.length > 0) return inputError(`nimicoding sweep design refused: ${ledgerErrors.join("; ")}.\n`);
287
+ const waves = [];
288
+ const errors = [];
289
+ const resultRefs = new Set();
290
+ for (const entry of ledger.value.entries) {
291
+ for (const ref of entry.replacement_artifact_refs ?? []) {
292
+ if (ref.includes("/design-auditor-results/")) resultRefs.add(ref);
293
+ }
294
+ }
295
+ for (const ref of resultRefs) {
296
+ const result = await loadYamlRef(projectRoot, ref);
297
+ if (result?.kind !== "sweep-design-design-auditor-result") continue;
298
+ if (!isLlmAuditorResult(result) && options.allowSyntheticTrial !== true) {
299
+ errors.push(`wave-plan result ${ref} is not closeout-eligible LLM provenance; pass --allow-synthetic-trial only for load tests`);
300
+ continue;
301
+ }
302
+ for (const wave of result.wave_changes ?? []) {
303
+ if (wave.state === "ready_for_implementation" || wave.ready_for_implementation_wave === true) {
304
+ validateWave(wave, errors);
305
+ waves.push({ ...wave, source_design_auditor_result_ref: ref });
306
+ }
307
+ }
308
+ }
309
+ if (errors.length > 0) return inputError(`nimicoding sweep design refused: ${errors.join("; ")}.\n`);
310
+ const artifact = {
311
+ version: 2,
312
+ kind: "sweep-design-wave-plan",
313
+ run_id: run.runId,
314
+ topic_id: options.topicId,
315
+ source_revision_ledger_ref: ledger.ref,
316
+ mutates_topic_state: false,
317
+ worker_dispatch_allowed: false,
318
+ wave_count: waves.length,
319
+ waves,
320
+ created_at: options.verifiedAt ?? nowIso(),
321
+ };
322
+ const ref = await writeYamlRef(projectRoot, wavePlanRef(run.runId), artifact);
323
+ return { ok: true, exitCode: 0, runId: run.runId, wavePlanRef: ref, waveCount: waves.length };
324
+ }
@@ -0,0 +1,8 @@
1
+ export {
2
+ runAuditorPrompt,
3
+ runIntake,
4
+ runPacketBuild,
5
+ runPacketBuildBatch,
6
+ } from "./sweep-design-runtime/engine.mjs";
7
+ export { runFixTopic } from "./sweep-design-runtime/fix-topic.mjs";
8
+ export { runFinalize, runLedgerValidate, runResultIngest, runWavePlan } from "./sweep-design-runtime/results.mjs";
@@ -0,0 +1,143 @@
1
+ import { mkdir, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import { getBootstrapSeedEntries } from "../seeds/bootstrap.mjs";
5
+ import { pathExists, readTextIfFile } from "./fs-helpers.mjs";
6
+
7
+ export const SYNC_MODE = {
8
+ DRY_RUN: "dry_run",
9
+ APPLY: "apply",
10
+ CHECK: "check",
11
+ };
12
+
13
+ const STATUS = {
14
+ IN_SYNC: "in_sync",
15
+ CREATED: "created",
16
+ UPDATED: "updated",
17
+ WOULD_CREATE: "would_create",
18
+ WOULD_UPDATE: "would_update",
19
+ DRIFTED_PRESERVED: "drifted_preserved",
20
+ MISSING_PACKAGE_CANONICAL: "missing_package_canonical",
21
+ MISSING_HOST_STATE_SEED: "missing_host_state_seed",
22
+ DRIFTED_PACKAGE_CANONICAL: "drifted_package_canonical",
23
+ };
24
+
25
+ const HOST_OWNED_SEED_OWNERSHIPS = new Set(["host_state_seed", "host_profile_override"]);
26
+
27
+ async function evaluateSeedEntry(projectRoot, entry, mode) {
28
+ const absolutePath = path.join(projectRoot, entry.outputRelativePath);
29
+ const info = await pathExists(absolutePath);
30
+ const fileExists = Boolean(info && info.isFile());
31
+
32
+ if (!fileExists) {
33
+ if (mode === SYNC_MODE.APPLY) {
34
+ await mkdir(path.dirname(absolutePath), { recursive: true });
35
+ await writeFile(absolutePath, entry.content, "utf8");
36
+ return {
37
+ outputRelativePath: entry.outputRelativePath,
38
+ ownership: entry.ownership,
39
+ status: STATUS.CREATED,
40
+ detail: "missing host file was seeded from package source",
41
+ };
42
+ }
43
+ return {
44
+ outputRelativePath: entry.outputRelativePath,
45
+ ownership: entry.ownership,
46
+ status: HOST_OWNED_SEED_OWNERSHIPS.has(entry.ownership)
47
+ ? STATUS.MISSING_HOST_STATE_SEED
48
+ : STATUS.MISSING_PACKAGE_CANONICAL,
49
+ detail: mode === SYNC_MODE.CHECK
50
+ ? "host file is missing"
51
+ : "host file is missing and would be seeded on --apply",
52
+ };
53
+ }
54
+
55
+ const actual = await readTextIfFile(absolutePath);
56
+ if (actual === entry.content) {
57
+ return {
58
+ outputRelativePath: entry.outputRelativePath,
59
+ ownership: entry.ownership,
60
+ status: STATUS.IN_SYNC,
61
+ detail: "host file matches package source byte-for-byte",
62
+ };
63
+ }
64
+
65
+ if (HOST_OWNED_SEED_OWNERSHIPS.has(entry.ownership)) {
66
+ return {
67
+ outputRelativePath: entry.outputRelativePath,
68
+ ownership: entry.ownership,
69
+ status: STATUS.DRIFTED_PRESERVED,
70
+ detail: `${entry.ownership}: host owns canonical content; sync preserves host copy`,
71
+ };
72
+ }
73
+
74
+ if (mode === SYNC_MODE.APPLY) {
75
+ await writeFile(absolutePath, entry.content, "utf8");
76
+ return {
77
+ outputRelativePath: entry.outputRelativePath,
78
+ ownership: entry.ownership,
79
+ status: STATUS.UPDATED,
80
+ detail: "drifted package_canonical file rewritten to package source",
81
+ };
82
+ }
83
+
84
+ return {
85
+ outputRelativePath: entry.outputRelativePath,
86
+ ownership: entry.ownership,
87
+ status: mode === SYNC_MODE.CHECK
88
+ ? STATUS.DRIFTED_PACKAGE_CANONICAL
89
+ : STATUS.WOULD_UPDATE,
90
+ detail: "host file diverges from package_canonical source",
91
+ };
92
+ }
93
+
94
+ export async function runSeedSync(projectRoot, mode = SYNC_MODE.DRY_RUN) {
95
+ const entries = await getBootstrapSeedEntries();
96
+ const results = [];
97
+ for (const entry of entries) {
98
+ results.push(await evaluateSeedEntry(projectRoot, entry, mode));
99
+ }
100
+
101
+ const summary = {
102
+ total: results.length,
103
+ in_sync: 0,
104
+ created: 0,
105
+ updated: 0,
106
+ would_create: 0,
107
+ would_update: 0,
108
+ drifted_preserved: 0,
109
+ missing_host_state_seed: 0,
110
+ missing_package_canonical: 0,
111
+ drifted_package_canonical: 0,
112
+ };
113
+
114
+ for (const result of results) {
115
+ summary[result.status] = (summary[result.status] ?? 0) + 1;
116
+ }
117
+
118
+ // Re-derive dry-run status counters when no apply happened.
119
+ if (mode === SYNC_MODE.DRY_RUN) {
120
+ summary.would_create = results.filter((r) =>
121
+ r.status === STATUS.MISSING_HOST_STATE_SEED || r.status === STATUS.MISSING_PACKAGE_CANONICAL,
122
+ ).length;
123
+ summary.would_update = results.filter((r) => r.status === STATUS.WOULD_UPDATE).length;
124
+ }
125
+
126
+ const checkFailures = mode === SYNC_MODE.CHECK
127
+ ? results.filter((r) =>
128
+ r.status === STATUS.MISSING_PACKAGE_CANONICAL
129
+ || r.status === STATUS.MISSING_HOST_STATE_SEED
130
+ || r.status === STATUS.DRIFTED_PACKAGE_CANONICAL,
131
+ )
132
+ : [];
133
+
134
+ return {
135
+ mode,
136
+ summary,
137
+ results,
138
+ ok: mode === SYNC_MODE.CHECK ? checkFailures.length === 0 : true,
139
+ checkFailures,
140
+ };
141
+ }
142
+
143
+ export const SYNC_RESULT_STATUS = STATUS;
@@ -0,0 +1,186 @@
1
+ import { readdir, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import YAML from "yaml";
4
+
5
+ import { readTextIfFile } from "./fs-helpers.mjs";
6
+ import { packetAuthorityCoverage, packetAuthorityCoverageError } from "./topic-authority-coverage.mjs";
7
+ import { isRecognizedLifecycleArtifactName } from "./topic-lifecycle-artifacts.mjs";
8
+ import { parseYamlText } from "./yaml-helpers.mjs";
9
+ import { loadTopicRuntimeAuthority, toPortableRelativePath } from "./topic-common.mjs";
10
+ import { getTopicWaves, loadTopicReport, topicHasEnrichedShape } from "./topic-scaffold.mjs";
11
+
12
+ export function readFrontmatterObject(text) {
13
+ const parsed = parsePacketDraft(text);
14
+ return parsed && typeof parsed == "object" ? parsed : null;
15
+ }
16
+ export function parsePacketDraft(text) {
17
+ if (!text) return null;
18
+ if (
19
+ text.startsWith(`---
20
+ `)
21
+ ) {
22
+ const closing = text.indexOf(
23
+ `
24
+ ---
25
+ `,
26
+ 4,
27
+ );
28
+ if (closing !== -1) {
29
+ const frontmatter = text.slice(4, closing);
30
+ return parseYamlText(frontmatter);
31
+ }
32
+ }
33
+ return parseYamlText(text);
34
+ }
35
+ export function packetFilenameFromId(packetId) {
36
+ return `packet-${packetId}.md`;
37
+ }
38
+ export function resultFilename(waveId, slug, resultKind) {
39
+ return `result-${waveId}-${resultKind}.md`;
40
+ }
41
+ export function decisionReviewFilename(slug) {
42
+ return `decision-review-${slug}.md`;
43
+ }
44
+ export function remediationFilename(waveId, kind, reason) {
45
+ return `packet-${waveId}-remediation-${kind}-${reason}.md`;
46
+ }
47
+ export function overflowContinuationFilename(waveId, continuationPacketId) {
48
+ return `overflow-continuation-${waveId}-${continuationPacketId}.md`;
49
+ }
50
+ export function waveCloseoutFilename(waveId) {
51
+ return `closeout-${waveId}.md`;
52
+ }
53
+ export function topicCloseoutFilename() {
54
+ return "closeout-topic.md";
55
+ }
56
+ export function topicTrueCloseAuditFilename() {
57
+ return "topic-true-close-audit.md";
58
+ }
59
+ export function topicTrueCloseJudgementFilename() {
60
+ return "result-topic-true-close-audit.md";
61
+ }
62
+ export function topicTrueCloseRecordFilename() {
63
+ return "result-topic-true-close.md";
64
+ }
65
+ export function pendingNoteFilename() {
66
+ return "pending-note.md";
67
+ }
68
+ export function pendingNoteMarkdown(note) {
69
+ return `---
70
+ ${YAML.stringify(note).trimEnd()}
71
+ ---
72
+
73
+ # Pending Note
74
+
75
+ Recorded by \`nimicoding topic hold\`.
76
+ `;
77
+ }
78
+ export function packetMarkdown(packet) {
79
+ return `---
80
+ ${YAML.stringify(packet).trimEnd()}
81
+ ---
82
+
83
+ # Packet ${packet.packet_id}
84
+
85
+ Frozen by \`nimicoding topic packet freeze\`.
86
+ `;
87
+ }
88
+ export async function freezePacketForTopic(projectRoot, input, draftPath) {
89
+ const loaded = await loadTopicReport(projectRoot, input);
90
+ if (!loaded.ok) return loaded;
91
+ const authority = await loadTopicRuntimeAuthority(projectRoot);
92
+ if (!topicHasEnrichedShape(loaded.topic, authority))
93
+ return { ok: false, error: "Packet freeze requires an enriched topic root." };
94
+ const draftText = await readTextIfFile(path.resolve(projectRoot, draftPath));
95
+ if (draftText === null) return { ok: false, error: `Draft packet not found: ${draftPath}` };
96
+ const packet = parsePacketDraft(draftText);
97
+ if (!packet || typeof packet != "object")
98
+ return { ok: false, error: `Draft packet is not valid YAML/frontmatter: ${draftPath}` };
99
+ const missingFields = authority.packetRequiredFields.filter((field) => {
100
+ const value = packet[field];
101
+ return value == null || value === "" || (Array.isArray(value) && value.length === 0);
102
+ });
103
+ if (missingFields.length > 0)
104
+ return {
105
+ ok: false,
106
+ error: `Draft packet is missing required fields: ${missingFields.join(", ")}`,
107
+ };
108
+ if (packet.topic_id !== loaded.topicId)
109
+ return {
110
+ ok: false,
111
+ error: `Draft packet topic_id does not match topic (${packet.topic_id} vs ${loaded.topicId})`,
112
+ };
113
+ if (!getTopicWaves(loaded.topic).find((entry) => entry.wave_id === packet.wave_id))
114
+ return {
115
+ ok: false,
116
+ error: `Draft packet wave_id does not resolve inside the topic: ${packet.wave_id}`,
117
+ };
118
+ const wave = getTopicWaves(loaded.topic).find((entry) => entry.wave_id === packet.wave_id);
119
+ const coverage = packetAuthorityCoverage(packet, wave);
120
+ if (!coverage.ok)
121
+ return {
122
+ ok: false,
123
+ error: packetAuthorityCoverageError(coverage),
124
+ missingAuthorityOwnerRefs: coverage.missingAuthorityOwnerRefs,
125
+ missingCanonicalSeamRefs: coverage.missingCanonicalSeamRefs,
126
+ };
127
+ if (!authority.packetFreezeAllowedStatuses.includes(packet.status))
128
+ return { ok: false, error: `Draft packet status is not freezeable: ${packet.status}` };
129
+ const packetFileName = packetFilenameFromId(packet.packet_id);
130
+ if (!isRecognizedLifecycleArtifactName(packetFileName))
131
+ return {
132
+ ok: false,
133
+ error: `Draft packet packet_id would create an ambiguous lifecycle artifact name: ${packetFileName}. Use a wave-prefixed packet id such as ${packet.wave_id}-${packet.packet_id}.`,
134
+ };
135
+ packet.status = "candidate";
136
+ const packetPath = path.join(loaded.topicDir, packetFileName);
137
+ return (
138
+ await writeFile(packetPath, packetMarkdown(packet), "utf8"),
139
+ {
140
+ ok: true,
141
+ topicId: loaded.topicId,
142
+ topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
143
+ packetId: packet.packet_id,
144
+ packetRef: toPortableRelativePath(path.relative(projectRoot, packetPath)),
145
+ waveId: packet.wave_id,
146
+ status: packet.status,
147
+ }
148
+ );
149
+ }
150
+ export async function loadTopicPacket(projectRoot, input, packetId) {
151
+ const loaded = await loadTopicReport(projectRoot, input);
152
+ if (!loaded.ok) return loaded;
153
+ const packetPath = path.join(loaded.topicDir, packetFilenameFromId(packetId)),
154
+ packetText = await readTextIfFile(packetPath);
155
+ if (packetText === null) return { ok: false, error: `Packet not found: ${packetId}` };
156
+ const packet = parsePacketDraft(packetText);
157
+ return !packet || typeof packet != "object"
158
+ ? { ok: false, error: `Packet is not valid YAML/frontmatter: ${packetId}` }
159
+ : { ok: true, ...loaded, packetPath, packet };
160
+ }
161
+ export async function listWavePackets(topicDir, waveId) {
162
+ const entries = await readdir(topicDir, { withFileTypes: true }),
163
+ packets = [];
164
+ for (const entry of entries) {
165
+ if (!entry.isFile() || !entry.name.startsWith("packet-") || !entry.name.endsWith(".md"))
166
+ continue;
167
+ const packetPath = path.join(topicDir, entry.name),
168
+ packetText = await readTextIfFile(packetPath),
169
+ packet = readFrontmatterObject(packetText ?? "");
170
+ packet?.wave_id === waveId && packets.push({ packet, packetPath, packetRefName: entry.name });
171
+ }
172
+ return packets.sort((left, right) => left.packetRefName.localeCompare(right.packetRefName));
173
+ }
174
+ export async function listWaveResults(topicDir, waveId) {
175
+ const entries = await readdir(topicDir, { withFileTypes: true }),
176
+ results = [];
177
+ for (const entry of entries) {
178
+ if (!entry.isFile() || !entry.name.startsWith("result-") || !entry.name.endsWith(".md"))
179
+ continue;
180
+ const resultPath = path.join(topicDir, entry.name),
181
+ resultText = await readTextIfFile(resultPath),
182
+ result = readFrontmatterObject(resultText ?? "");
183
+ result?.wave_id === waveId && results.push({ result, resultPath, resultRefName: entry.name });
184
+ }
185
+ return results.sort((left, right) => left.resultRefName.localeCompare(right.resultRefName));
186
+ }