@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,515 @@
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import YAML from "yaml";
4
+
5
+ import { buildDispatchPrompt } from "./authority-convergence.mjs";
6
+ import { readTextIfFile } from "./fs-helpers.mjs";
7
+ import { loadTopicRuntimeAuthority, toPortableRelativePath } from "./topic-common.mjs";
8
+ import {
9
+ buildTopicNow,
10
+ getTopicWaves,
11
+ isIsoUtcTimestamp,
12
+ loadTopicReport,
13
+ moveTopicDirectoryForState,
14
+ validateTopicSlug,
15
+ topicHasEnrichedShape,
16
+ writeTopicYaml,
17
+ } from "./topic-scaffold.mjs";
18
+ import {
19
+ loadTopicPacket,
20
+ decisionReviewFilename,
21
+ overflowContinuationFilename,
22
+ packetMarkdown,
23
+ pendingNoteFilename,
24
+ pendingNoteMarkdown,
25
+ remediationFilename,
26
+ resultFilename,
27
+ } from "./topic-artifacts.mjs";
28
+ import {
29
+ collectWaveArtifactEvidence,
30
+ getPendingEntryBlockers,
31
+ loadPendingNote,
32
+ validateWaveId,
33
+ } from "./topic-waves.mjs";
34
+
35
+ export function promptFilename(packetId, role) {
36
+ return `prompt-${packetId}-${role}.md`;
37
+ }
38
+ export async function dispatchTopicPacket(projectRoot, input, packetId, role) {
39
+ const loaded = await loadTopicPacket(projectRoot, input, packetId);
40
+ if (!loaded.ok) return loaded;
41
+ const wave = getTopicWaves(loaded.topic).find((entry) => entry.wave_id === loaded.packet.wave_id);
42
+ if (!wave)
43
+ return {
44
+ ok: false,
45
+ error: `Packet wave_id does not resolve inside topic: ${loaded.packet.wave_id}`,
46
+ };
47
+ if (["retired", "superseded", "closed"].includes(wave.state))
48
+ return { ok: false, error: `Wave is not dispatchable: ${wave.wave_id} (${wave.state})` };
49
+ if (!["candidate", "admitted", "preflight", "dispatched"].includes(loaded.packet.status))
50
+ return { ok: false, error: `Packet is not dispatchable from status ${loaded.packet.status}` };
51
+ const promptPath = path.join(loaded.topicDir, promptFilename(packetId, role));
52
+ return (
53
+ await writeFile(promptPath, buildDispatchPrompt(loaded.packet, loaded.topicId, role), "utf8"),
54
+ (loaded.packet.status = "dispatched"),
55
+ await writeFile(loaded.packetPath, packetMarkdown(loaded.packet), "utf8"),
56
+ role === "worker" &&
57
+ ["preflight_admitted", "implementation_admitted", "continuation_packet_open"].includes(
58
+ wave.state,
59
+ ) &&
60
+ ((wave.state = "implementation_active"),
61
+ (loaded.topic.waves = getTopicWaves(loaded.topic).map((entry) =>
62
+ entry.wave_id === wave.wave_id ? wave : entry,
63
+ )),
64
+ (loaded.topic.last_transition_at = buildTopicNow()),
65
+ (loaded.topic.last_transition_reason = `packet_${packetId}_worker_dispatched`),
66
+ await writeTopicYaml(loaded.topicYamlPath, loaded.topic)),
67
+ {
68
+ ok: true,
69
+ topicId: loaded.topicId,
70
+ topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
71
+ packetId,
72
+ packetRef: toPortableRelativePath(path.relative(projectRoot, loaded.packetPath)),
73
+ promptRef: toPortableRelativePath(path.relative(projectRoot, promptPath)),
74
+ waveId: wave.wave_id,
75
+ waveState: wave.state,
76
+ role,
77
+ }
78
+ );
79
+ }
80
+ export function resultMarkdown(result, sourceText) {
81
+ return (
82
+ `---
83
+ ${YAML.stringify(result).trimEnd()}
84
+ ---
85
+
86
+ # Result ${result.result_id}
87
+
88
+ ${sourceText ?? ""}`.trimEnd() +
89
+ `
90
+ `
91
+ );
92
+ }
93
+ export async function recordTopicResult(projectRoot, input, resultKind, verdict, fromPath, verifiedAt) {
94
+ const loaded = await loadTopicReport(projectRoot, input);
95
+ if (!loaded.ok) return loaded;
96
+ const authority = await loadTopicRuntimeAuthority(projectRoot),
97
+ sourcePath = path.resolve(projectRoot, fromPath),
98
+ sourceText = await readTextIfFile(sourcePath);
99
+ if (sourceText === null) return { ok: false, error: `Result source not found: ${fromPath}` };
100
+ if (!authority.resultKinds.includes(resultKind))
101
+ return { ok: false, error: `Unsupported result kind: ${resultKind}` };
102
+ if (!authority.resultVerdicts.includes(verdict))
103
+ return { ok: false, error: `Unsupported result verdict: ${verdict}` };
104
+ if (
105
+ authority.resultVerifiedAtFormat === "iso8601_utc_timestamp" &&
106
+ !isIsoUtcTimestamp(verifiedAt)
107
+ )
108
+ return { ok: false, error: `Result verified_at must be an ISO-8601 UTC timestamp: ${verifiedAt}` };
109
+ const waveId = loaded.topic.selected_next_target,
110
+ wave = getTopicWaves(loaded.topic).find((entry) => entry.wave_id === waveId) ?? null;
111
+ if (!wave)
112
+ return {
113
+ ok: false,
114
+ error: "Result recording requires a selected wave in topic.selected_next_target",
115
+ };
116
+ if ((await collectWaveArtifactEvidence(loaded.topicDir, wave.wave_id)).packetRefs.length === 0)
117
+ return {
118
+ ok: false,
119
+ error: `Result recording requires at least one packet lineage for ${wave.wave_id}`,
120
+ };
121
+ const resultId = `${wave.wave_id}-${resultKind}`,
122
+ result = {
123
+ result_id: resultId,
124
+ topic_id: loaded.topicId,
125
+ wave_id: wave.wave_id,
126
+ result_kind: resultKind,
127
+ verdict,
128
+ verified_at: verifiedAt,
129
+ source_ref: toPortableRelativePath(path.relative(projectRoot, sourcePath)),
130
+ },
131
+ resultPath = path.join(loaded.topicDir, resultFilename(wave.wave_id, wave.slug, resultKind));
132
+ return (
133
+ await writeFile(resultPath, resultMarkdown(result, sourceText), "utf8"),
134
+ verdict === "OVERFLOW"
135
+ ? (wave.state = "overflowed")
136
+ : verdict === "NEEDS_REVISION" || verdict === "FAIL"
137
+ ? (wave.state = "needs_revision")
138
+ : verdict === "PASS" &&
139
+ resultKind[0] === "p" &&
140
+ wave.state === "preflight_admitted" &&
141
+ (wave.state = "implementation_admitted"),
142
+ (loaded.topic.waves = getTopicWaves(loaded.topic).map((entry) =>
143
+ entry.wave_id === wave.wave_id ? wave : entry,
144
+ )),
145
+ (loaded.topic.last_transition_at = buildTopicNow()),
146
+ (loaded.topic.last_transition_reason = `recorded_${resultKind}_${verdict}_for_${wave.wave_id}`),
147
+ await writeTopicYaml(loaded.topicYamlPath, loaded.topic),
148
+ {
149
+ ok: true,
150
+ topicId: loaded.topicId,
151
+ topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
152
+ resultId,
153
+ resultRef: toPortableRelativePath(path.relative(projectRoot, resultPath)),
154
+ waveId: wave.wave_id,
155
+ waveState: wave.state,
156
+ verdict,
157
+ resultKind,
158
+ }
159
+ );
160
+ }
161
+ export function remediationMarkdown(remediation) {
162
+ return `---
163
+ ${YAML.stringify(remediation).trimEnd()}
164
+ ---
165
+
166
+ # Remediation ${remediation.remediation_id}
167
+
168
+ Opened by \`nimicoding topic remediation open\`.
169
+ `;
170
+ }
171
+ export async function openTopicRemediation(projectRoot, input, options) {
172
+ const loaded = await loadTopicReport(projectRoot, input);
173
+ if (!loaded.ok) return loaded;
174
+ const authority = await loadTopicRuntimeAuthority(projectRoot);
175
+ if (!topicHasEnrichedShape(loaded.topic, authority))
176
+ return { ok: false, error: "Remediation commands require an enriched topic root." };
177
+ if (!authority.remediationKinds.includes(options.kind))
178
+ return { ok: false, error: `Unsupported remediation kind: ${options.kind}` };
179
+ const waveId = loaded.topic.selected_next_target,
180
+ wave = getTopicWaves(loaded.topic).find((entry) => entry.wave_id === waveId) ?? null;
181
+ if (!wave)
182
+ return {
183
+ ok: false,
184
+ error: "Remediation open requires a selected wave in topic.selected_next_target",
185
+ };
186
+ if (["retired", "superseded", "closed"].includes(wave.state))
187
+ return { ok: false, error: `Wave is not remediation-eligible: ${wave.wave_id} (${wave.state})` };
188
+ if (options.kind === "continuation" && wave.state !== "overflowed")
189
+ return {
190
+ ok: false,
191
+ error: `Continuation remediation requires an overflowed wave, found ${wave.state}`,
192
+ };
193
+ if (options.kind === "continuation" && !options.overflowedPacketId)
194
+ return { ok: false, error: "Continuation remediation requires --overflowed-packet lineage" };
195
+ if (options.overflowedPacketId) {
196
+ const overflowedPacket = await loadTopicPacket(projectRoot, input, options.overflowedPacketId);
197
+ if (!overflowedPacket.ok)
198
+ return {
199
+ ok: false,
200
+ error: `Overflowed packet lineage could not be loaded: ${options.overflowedPacketId}`,
201
+ };
202
+ if (overflowedPacket.packet.wave_id !== wave.wave_id)
203
+ return {
204
+ ok: false,
205
+ error: `Overflowed packet does not belong to the selected wave (${overflowedPacket.packet.wave_id} vs ${wave.wave_id})`,
206
+ };
207
+ }
208
+ const remediationId = `${wave.wave_id}-remediation-${options.kind}-${options.reason}`,
209
+ remediation = {
210
+ remediation_id: remediationId,
211
+ topic_id: loaded.topicId,
212
+ wave_id: wave.wave_id,
213
+ kind: options.kind,
214
+ reason: options.reason,
215
+ };
216
+ options.overflowedPacketId && (remediation.overflowed_packet_id = options.overflowedPacketId);
217
+ const remediationPath = path.join(
218
+ loaded.topicDir,
219
+ remediationFilename(wave.wave_id, options.kind, options.reason),
220
+ );
221
+ return (
222
+ await writeFile(remediationPath, remediationMarkdown(remediation), "utf8"),
223
+ options.kind !== "continuation" &&
224
+ wave.state !== "needs_revision" &&
225
+ ((wave.state = "needs_revision"),
226
+ (loaded.topic.waves = getTopicWaves(loaded.topic).map((entry) =>
227
+ entry.wave_id === wave.wave_id ? wave : entry,
228
+ ))),
229
+ (loaded.topic.last_transition_at = buildTopicNow()),
230
+ (loaded.topic.last_transition_reason = `opened_remediation_${options.kind}_${options.reason}_for_${wave.wave_id}`),
231
+ await writeTopicYaml(loaded.topicYamlPath, loaded.topic),
232
+ {
233
+ ok: true,
234
+ topicId: loaded.topicId,
235
+ topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
236
+ remediationId,
237
+ remediationRef: toPortableRelativePath(path.relative(projectRoot, remediationPath)),
238
+ waveId: wave.wave_id,
239
+ waveState: wave.state,
240
+ kind: options.kind,
241
+ reason: options.reason,
242
+ }
243
+ );
244
+ }
245
+ export function overflowContinuationMarkdown(continuation) {
246
+ return `---
247
+ ${YAML.stringify(continuation).trimEnd()}
248
+ ---
249
+
250
+ # Overflow Continuation ${continuation.continuation_packet_id}
251
+
252
+ Recorded by \`nimicoding topic overflow continue\`.
253
+ `;
254
+ }
255
+ export async function continueTopicOverflow(projectRoot, input, options) {
256
+ const loaded = await loadTopicReport(projectRoot, input);
257
+ if (!loaded.ok) return loaded;
258
+ const authority = await loadTopicRuntimeAuthority(projectRoot);
259
+ if (!topicHasEnrichedShape(loaded.topic, authority))
260
+ return { ok: false, error: "Overflow continuation requires an enriched topic root." };
261
+ const waveId = loaded.topic.selected_next_target,
262
+ wave = getTopicWaves(loaded.topic).find((entry) => entry.wave_id === waveId) ?? null;
263
+ if (!wave)
264
+ return {
265
+ ok: false,
266
+ error: "Overflow continuation requires a selected wave in topic.selected_next_target",
267
+ };
268
+ if (wave.state !== "overflowed")
269
+ return {
270
+ ok: false,
271
+ error: `Overflow continuation requires an overflowed wave, found ${wave.state}`,
272
+ };
273
+ if (options.sameOwnerDomain !== true)
274
+ return {
275
+ ok: false,
276
+ error: "Overflow continuation requires explicit same-owner-domain acknowledgement",
277
+ };
278
+ const overflowedPacket = await loadTopicPacket(projectRoot, input, options.overflowedPacketId);
279
+ if (!overflowedPacket.ok)
280
+ return {
281
+ ok: false,
282
+ error: `Overflowed packet lineage could not be loaded: ${options.overflowedPacketId}`,
283
+ };
284
+ if (overflowedPacket.packet.wave_id !== wave.wave_id)
285
+ return {
286
+ ok: false,
287
+ error: `Overflowed packet does not belong to the selected wave (${overflowedPacket.packet.wave_id} vs ${wave.wave_id})`,
288
+ };
289
+ const continuationPacket = await loadTopicPacket(
290
+ projectRoot,
291
+ input,
292
+ options.continuationPacketId,
293
+ );
294
+ if (!continuationPacket.ok)
295
+ return {
296
+ ok: false,
297
+ error: `Continuation packet could not be loaded: ${options.continuationPacketId}`,
298
+ };
299
+ if (continuationPacket.packet.wave_id !== wave.wave_id)
300
+ return {
301
+ ok: false,
302
+ error: `Continuation packet does not belong to the selected wave (${continuationPacket.packet.wave_id} vs ${wave.wave_id})`,
303
+ };
304
+ const continuation = {
305
+ topic_id: loaded.topicId,
306
+ wave_id: wave.wave_id,
307
+ overflowed_packet_id: options.overflowedPacketId,
308
+ manager_judgement: options.managerJudgement,
309
+ continuation_packet_id: options.continuationPacketId,
310
+ same_owner_domain: true,
311
+ },
312
+ continuationPath = path.join(
313
+ loaded.topicDir,
314
+ overflowContinuationFilename(wave.wave_id, options.continuationPacketId),
315
+ );
316
+ return (
317
+ await writeFile(continuationPath, overflowContinuationMarkdown(continuation), "utf8"),
318
+ (wave.state = "continuation_packet_open"),
319
+ (loaded.topic.waves = getTopicWaves(loaded.topic).map((entry) =>
320
+ entry.wave_id === wave.wave_id ? wave : entry,
321
+ )),
322
+ (loaded.topic.last_transition_at = buildTopicNow()),
323
+ (loaded.topic.last_transition_reason = `continued_overflow_for_${wave.wave_id}_via_${options.continuationPacketId}`),
324
+ await writeTopicYaml(loaded.topicYamlPath, loaded.topic),
325
+ {
326
+ ok: true,
327
+ topicId: loaded.topicId,
328
+ topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
329
+ waveId: wave.wave_id,
330
+ waveState: wave.state,
331
+ overflowedPacketId: options.overflowedPacketId,
332
+ continuationPacketId: options.continuationPacketId,
333
+ continuationRef: toPortableRelativePath(path.relative(projectRoot, continuationPath)),
334
+ }
335
+ );
336
+ }
337
+ export async function createDecisionReview(projectRoot, input, slug, options) {
338
+ const loaded = await loadTopicReport(projectRoot, input);
339
+ if (!loaded.ok) return loaded;
340
+ const authority = await loadTopicRuntimeAuthority(projectRoot);
341
+ if (!validateTopicSlug(slug))
342
+ return { ok: false, error: `Decision review slug must be lowercase kebab-case: ${slug}` };
343
+ if (!authority.decisionDispositions.includes(options.disposition))
344
+ return { ok: false, error: `Unsupported decision disposition: ${options.disposition}` };
345
+ const review = {
346
+ decision_review_id: slug,
347
+ topic_id: loaded.topicId,
348
+ date: options.date,
349
+ decision: options.decision,
350
+ replaced_scope: options.replacedScope,
351
+ active_replacement_scope: options.activeReplacementScope,
352
+ disposition: options.disposition,
353
+ };
354
+ if (
355
+ options.targetWaveId &&
356
+ !getTopicWaves(loaded.topic).find((entry) => entry.wave_id === options.targetWaveId)
357
+ )
358
+ return { ok: false, error: `Decision review target wave does not exist: ${options.targetWaveId}` };
359
+ if (
360
+ options.activeReplacementScope !== "topic_design_baseline" &&
361
+ options.activeReplacementScope !== null &&
362
+ !getTopicWaves(loaded.topic).some((entry) => entry.wave_id === options.activeReplacementScope)
363
+ )
364
+ return {
365
+ ok: false,
366
+ error: `Decision review active replacement scope must be machine-identifiable: ${options.activeReplacementScope}`,
367
+ };
368
+ const reviewPath = path.join(loaded.topicDir, decisionReviewFilename(slug));
369
+ if (
370
+ (await writeFile(
371
+ reviewPath,
372
+ `---
373
+ ${YAML.stringify(review).trimEnd()}
374
+ ---
375
+
376
+ # Decision Review ${slug}
377
+ `,
378
+ "utf8",
379
+ ),
380
+ options.targetWaveId)
381
+ ) {
382
+ const waves = getTopicWaves(loaded.topic).map((entry) =>
383
+ entry.wave_id === options.targetWaveId
384
+ ? options.disposition === "retired"
385
+ ? { ...entry, state: "retired", selected: false }
386
+ : options.disposition === "superseded"
387
+ ? { ...entry, state: "superseded", selected: false }
388
+ : entry
389
+ : entry.wave_id === options.activeReplacementScope
390
+ ? { ...entry, selected: true }
391
+ : loaded.topic.selected_next_target === options.targetWaveId
392
+ ? { ...entry, selected: false }
393
+ : entry,
394
+ );
395
+ ((loaded.topic.waves = waves),
396
+ loaded.topic.selected_next_target === options.targetWaveId &&
397
+ (loaded.topic.selected_next_target = options.activeReplacementScope),
398
+ (loaded.topic.last_transition_at = buildTopicNow()),
399
+ (loaded.topic.last_transition_reason = `decision_review_${slug}`),
400
+ await writeTopicYaml(loaded.topicYamlPath, loaded.topic));
401
+ }
402
+ return {
403
+ ok: true,
404
+ topicId: loaded.topicId,
405
+ topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
406
+ decisionReviewId: slug,
407
+ decisionReviewRef: toPortableRelativePath(path.relative(projectRoot, reviewPath)),
408
+ disposition: options.disposition,
409
+ targetWaveId: options.targetWaveId ?? null,
410
+ };
411
+ }
412
+ export async function holdTopicInPending(projectRoot, input, options) {
413
+ const loaded = await loadTopicReport(projectRoot, input);
414
+ if (!loaded.ok) return loaded;
415
+ const authority = await loadTopicRuntimeAuthority(projectRoot);
416
+ if (!topicHasEnrichedShape(loaded.topic, authority))
417
+ return { ok: false, error: "Topic hold requires an enriched topic root." };
418
+ if (loaded.topic.state !== "ongoing")
419
+ return { ok: false, error: `Topic hold requires ongoing state, found ${loaded.topic.state}` };
420
+ const blockers = getPendingEntryBlockers(loaded.topic);
421
+ if (blockers.length > 0)
422
+ return {
423
+ ok: false,
424
+ error: `Topic hold requires no active implementation wave, found ${blockers.join(", ")}`,
425
+ };
426
+ if (!options.reopenCriteria && !options.closeTrigger)
427
+ return { ok: false, error: "Topic hold requires explicit reopen criteria or close trigger." };
428
+ const pendingNote = {
429
+ pending_note_id: `pending-${loaded.topicId}`,
430
+ topic_id: loaded.topicId,
431
+ entered_from_state: loaded.topic.state,
432
+ reason: options.reason,
433
+ summary: options.summary,
434
+ status: "active",
435
+ };
436
+ (options.reopenCriteria && (pendingNote.reopen_criteria = options.reopenCriteria),
437
+ options.closeTrigger && (pendingNote.close_trigger = options.closeTrigger));
438
+ const notePath = path.join(loaded.topicDir, pendingNoteFilename());
439
+ (await writeFile(notePath, pendingNoteMarkdown(pendingNote), "utf8"),
440
+ (loaded.topic.state = "pending"),
441
+ (loaded.topic.last_transition_at = buildTopicNow()),
442
+ (loaded.topic.last_transition_reason = `entered_pending_${options.reason}`));
443
+ const moved = await moveTopicDirectoryForState(
444
+ projectRoot,
445
+ loaded.topicDir,
446
+ loaded.topicId,
447
+ "pending",
448
+ );
449
+ return (
450
+ await writeTopicYaml(moved.topicYamlPath, loaded.topic),
451
+ {
452
+ ok: true,
453
+ topicId: loaded.topicId,
454
+ topicRef: toPortableRelativePath(path.relative(projectRoot, moved.topicDir)),
455
+ state: loaded.topic.state,
456
+ pendingNoteRef: toPortableRelativePath(
457
+ path.relative(projectRoot, path.join(moved.topicDir, pendingNoteFilename())),
458
+ ),
459
+ reason: options.reason,
460
+ }
461
+ );
462
+ }
463
+ export async function resumePendingTopic(projectRoot, input, options) {
464
+ const loaded = await loadTopicReport(projectRoot, input);
465
+ if (!loaded.ok) return loaded;
466
+ const authority = await loadTopicRuntimeAuthority(projectRoot);
467
+ if (!topicHasEnrichedShape(loaded.topic, authority))
468
+ return { ok: false, error: "Topic resume requires an enriched topic root." };
469
+ if (loaded.topic.state !== "pending")
470
+ return { ok: false, error: `Topic resume requires pending state, found ${loaded.topic.state}` };
471
+ const pendingNoteLoaded = await loadPendingNote(loaded.topicDir);
472
+ if (!pendingNoteLoaded.ok) return { ok: false, error: pendingNoteLoaded.error };
473
+ const pendingNote = pendingNoteLoaded.note;
474
+ if (!pendingNote.reopen_criteria)
475
+ return { ok: false, error: "Topic resume requires pending note reopen criteria." };
476
+ const selectedWave = getTopicWaves(loaded.topic).find((entry) => entry.selected === true) ?? null;
477
+ if (
478
+ !(
479
+ typeof loaded.topic.selected_next_target == "string" &&
480
+ loaded.topic.selected_next_target !== "topic_design_baseline" &&
481
+ selectedWave !== null &&
482
+ selectedWave.wave_id === loaded.topic.selected_next_target
483
+ )
484
+ )
485
+ return {
486
+ ok: false,
487
+ error: "Topic resume requires exactly one selected next execution target before reopening.",
488
+ };
489
+ ((pendingNote.status = "resumed"),
490
+ (pendingNote.last_resumed_at = buildTopicNow()),
491
+ (pendingNote.last_resume_reason = options.criteriaMet),
492
+ await writeFile(pendingNoteLoaded.notePath, pendingNoteMarkdown(pendingNote), "utf8"),
493
+ (loaded.topic.state = "ongoing"),
494
+ (loaded.topic.last_transition_at = buildTopicNow()),
495
+ (loaded.topic.last_transition_reason = "resumed_from_pending_after_reopen_criteria_met"));
496
+ const moved = await moveTopicDirectoryForState(
497
+ projectRoot,
498
+ loaded.topicDir,
499
+ loaded.topicId,
500
+ "ongoing",
501
+ );
502
+ return (
503
+ await writeTopicYaml(moved.topicYamlPath, loaded.topic),
504
+ {
505
+ ok: true,
506
+ topicId: loaded.topicId,
507
+ topicRef: toPortableRelativePath(path.relative(projectRoot, moved.topicDir)),
508
+ state: loaded.topic.state,
509
+ pendingNoteRef: toPortableRelativePath(
510
+ path.relative(projectRoot, path.join(moved.topicDir, pendingNoteFilename())),
511
+ ),
512
+ criteriaMet: options.criteriaMet,
513
+ }
514
+ );
515
+ }