@nimiplatform/nimi-coding 0.2.8 → 0.3.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 (206) hide show
  1. package/CHANGELOG.md +10 -90
  2. package/CONTRIBUTING.md +5 -34
  3. package/README.md +47 -350
  4. package/README.zh-CN.md +33 -287
  5. package/cli/commands/start.mjs +48 -688
  6. package/cli/commands/validate-spec-audit.mjs +1 -5
  7. package/cli/commands/validate-spec-governance.mjs +20 -0
  8. package/cli/constants.mjs +9 -417
  9. package/cli/help.mjs +28 -145
  10. package/cli/index.mjs +5 -33
  11. package/cli/lib/blueprint-audit.mjs +5 -12
  12. package/cli/lib/bootstrap.mjs +0 -9
  13. package/cli/lib/contracts.mjs +0 -163
  14. package/cli/lib/doctor.mjs +155 -10
  15. package/cli/lib/entrypoints.mjs +63 -167
  16. package/cli/lib/internal/contracts-loaders.mjs +0 -85
  17. package/cli/lib/internal/contracts-parse.mjs +84 -520
  18. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +13 -8
  19. package/cli/lib/internal/surface-taxonomy-validators.mjs +316 -66
  20. package/cli/lib/internal/validators-shared.mjs +0 -16
  21. package/cli/lib/internal/validators-spec-helpers.mjs +7 -140
  22. package/cli/lib/internal/validators-spec.mjs +103 -342
  23. package/cli/lib/shared.mjs +1 -42
  24. package/cli/lib/validators.mjs +0 -48
  25. package/cli/seeds/seed-policy.yaml +0 -3
  26. package/config/bootstrap.yaml +4 -4
  27. package/config/spec-generation-inputs.yaml +6 -12
  28. package/contracts/domain-admission.schema.yaml +3 -3
  29. package/contracts/migration-inventory.schema.yaml +12 -75
  30. package/contracts/negative-fixtures.yaml +19 -99
  31. package/contracts/placement-contract.schema.yaml +49 -78
  32. package/contracts/projection-edge.schema.yaml +37 -121
  33. package/contracts/shared-enums.yaml +4 -8
  34. package/contracts/spec-generation-inputs.schema.yaml +15 -112
  35. package/contracts/spec-layout.schema.yaml +32 -0
  36. package/contracts/surface-taxonomy.schema.yaml +43 -94
  37. package/contracts/table-family.schema.yaml +1 -1
  38. package/contracts/tracked-output-admission.schema.yaml +13 -66
  39. package/methodology/core.yaml +33 -25
  40. package/methodology/four-closure-policy.yaml +18 -26
  41. package/methodology/role-separation-policy.yaml +21 -27
  42. package/methodology/spec-reconstruction.yaml +17 -34
  43. package/package.json +2 -4
  44. package/spec/_meta/spec-tree-model.yaml +5 -101
  45. package/spec/product-scope.yaml +25 -57
  46. package/adapters/README.md +0 -25
  47. package/adapters/claude/README.md +0 -89
  48. package/adapters/claude/profile.yaml +0 -70
  49. package/adapters/codex/README.md +0 -53
  50. package/adapters/codex/profile.yaml +0 -78
  51. package/adapters/oh-my-codex/README.md +0 -184
  52. package/adapters/oh-my-codex/profile.yaml +0 -46
  53. package/cli/commands/admit-high-risk-decision.mjs +0 -108
  54. package/cli/commands/audit-sweep.mjs +0 -364
  55. package/cli/commands/closeout.mjs +0 -186
  56. package/cli/commands/decide-high-risk-execution.mjs +0 -124
  57. package/cli/commands/handoff.mjs +0 -123
  58. package/cli/commands/ingest-high-risk-execution.mjs +0 -95
  59. package/cli/commands/review-high-risk-execution.mjs +0 -95
  60. package/cli/commands/sweep-design.mjs +0 -295
  61. package/cli/commands/sweep.mjs +0 -22
  62. package/cli/commands/topic-formatters.mjs +0 -382
  63. package/cli/commands/topic-goal.mjs +0 -33
  64. package/cli/commands/topic-options-shared.mjs +0 -27
  65. package/cli/commands/topic-options-workflow.mjs +0 -767
  66. package/cli/commands/topic-options.mjs +0 -626
  67. package/cli/commands/topic-runner.mjs +0 -169
  68. package/cli/commands/topic.mjs +0 -795
  69. package/cli/commands/validate-acceptance.mjs +0 -5
  70. package/cli/commands/validate-execution-packet.mjs +0 -5
  71. package/cli/commands/validate-orchestration-state.mjs +0 -5
  72. package/cli/commands/validate-prompt.mjs +0 -5
  73. package/cli/commands/validate-worker-output.mjs +0 -5
  74. package/cli/lib/adapter-profiles.mjs +0 -403
  75. package/cli/lib/audit-execution.mjs +0 -52
  76. package/cli/lib/audit-sweep-runtime/admissions.mjs +0 -508
  77. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +0 -356
  78. package/cli/lib/audit-sweep-runtime/chunks.mjs +0 -697
  79. package/cli/lib/audit-sweep-runtime/claude-auditor.mjs +0 -658
  80. package/cli/lib/audit-sweep-runtime/closeout.mjs +0 -144
  81. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +0 -654
  82. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +0 -527
  83. package/cli/lib/audit-sweep-runtime/common.mjs +0 -341
  84. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +0 -172
  85. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +0 -155
  86. package/cli/lib/audit-sweep-runtime/format.mjs +0 -57
  87. package/cli/lib/audit-sweep-runtime/ingest.mjs +0 -486
  88. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +0 -425
  89. package/cli/lib/audit-sweep-runtime/inventory.mjs +0 -798
  90. package/cli/lib/audit-sweep-runtime/ledger.mjs +0 -315
  91. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +0 -101
  92. package/cli/lib/audit-sweep-runtime/remediation.mjs +0 -349
  93. package/cli/lib/audit-sweep-runtime/rerun.mjs +0 -129
  94. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +0 -300
  95. package/cli/lib/audit-sweep-runtime/status.mjs +0 -62
  96. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +0 -215
  97. package/cli/lib/audit-sweep-runtime/validators.mjs +0 -797
  98. package/cli/lib/audit-sweep.mjs +0 -19
  99. package/cli/lib/authority-convergence.mjs +0 -720
  100. package/cli/lib/closeout.mjs +0 -732
  101. package/cli/lib/codex-sdk-runner.mjs +0 -76
  102. package/cli/lib/external-execution.mjs +0 -101
  103. package/cli/lib/handoff.mjs +0 -812
  104. package/cli/lib/high-risk-admission.mjs +0 -512
  105. package/cli/lib/high-risk-decision.mjs +0 -353
  106. package/cli/lib/high-risk-ingest.mjs +0 -321
  107. package/cli/lib/high-risk-review.mjs +0 -267
  108. package/cli/lib/internal/contracts-parse-high-risk.mjs +0 -131
  109. package/cli/lib/internal/contracts-validators.mjs +0 -399
  110. package/cli/lib/internal/doctor-bootstrap-surface.mjs +0 -406
  111. package/cli/lib/internal/doctor-delegated-surface.mjs +0 -256
  112. package/cli/lib/internal/doctor-finalize.mjs +0 -383
  113. package/cli/lib/internal/doctor-format.mjs +0 -286
  114. package/cli/lib/internal/doctor-inspectors.mjs +0 -327
  115. package/cli/lib/internal/doctor-state.mjs +0 -205
  116. package/cli/lib/internal/validators-artifacts.mjs +0 -515
  117. package/cli/lib/sweep-design-runtime/common.mjs +0 -246
  118. package/cli/lib/sweep-design-runtime/engine.mjs +0 -733
  119. package/cli/lib/sweep-design-runtime/fix-topic.mjs +0 -414
  120. package/cli/lib/sweep-design-runtime/lifecycle.mjs +0 -54
  121. package/cli/lib/sweep-design-runtime/results.mjs +0 -324
  122. package/cli/lib/sweep-design.mjs +0 -8
  123. package/cli/lib/topic-artifacts.mjs +0 -186
  124. package/cli/lib/topic-authority-coverage.mjs +0 -73
  125. package/cli/lib/topic-closeout.mjs +0 -560
  126. package/cli/lib/topic-common.mjs +0 -404
  127. package/cli/lib/topic-decisions.mjs +0 -332
  128. package/cli/lib/topic-draft-packets.mjs +0 -167
  129. package/cli/lib/topic-execution.mjs +0 -533
  130. package/cli/lib/topic-goal.mjs +0 -440
  131. package/cli/lib/topic-ledger.mjs +0 -281
  132. package/cli/lib/topic-lifecycle-artifacts.mjs +0 -173
  133. package/cli/lib/topic-root-validation.mjs +0 -288
  134. package/cli/lib/topic-runner-commands.mjs +0 -174
  135. package/cli/lib/topic-runner-deferral.mjs +0 -532
  136. package/cli/lib/topic-runner-stale-gates.mjs +0 -114
  137. package/cli/lib/topic-runner-validation.mjs +0 -138
  138. package/cli/lib/topic-runner.mjs +0 -727
  139. package/cli/lib/topic-scaffold.mjs +0 -252
  140. package/cli/lib/topic-waves.mjs +0 -403
  141. package/cli/lib/topic.mjs +0 -81
  142. package/config/audit-execution-artifacts.yaml +0 -20
  143. package/config/external-execution-artifacts.yaml +0 -16
  144. package/config/host-adapter.yaml +0 -30
  145. package/config/host-profile.yaml +0 -29
  146. package/config/installer-evidence.yaml +0 -31
  147. package/config/skill-installer.yaml +0 -23
  148. package/config/skill-manifest.yaml +0 -48
  149. package/config/skills.yaml +0 -30
  150. package/contracts/acceptance.schema.yaml +0 -16
  151. package/contracts/admission-checklist.schema.yaml +0 -15
  152. package/contracts/audit-chunk.schema.yaml +0 -123
  153. package/contracts/audit-closeout.schema.yaml +0 -51
  154. package/contracts/audit-finding.schema.yaml +0 -61
  155. package/contracts/audit-ledger.schema.yaml +0 -138
  156. package/contracts/audit-plan.schema.yaml +0 -138
  157. package/contracts/audit-remediation-map.schema.yaml +0 -52
  158. package/contracts/audit-rerun.schema.yaml +0 -31
  159. package/contracts/audit-sweep-result.yaml +0 -53
  160. package/contracts/authority-convergence-audit.schema.yaml +0 -19
  161. package/contracts/closeout.schema.yaml +0 -25
  162. package/contracts/decision-review.schema.yaml +0 -16
  163. package/contracts/doc-spec-audit-result.yaml +0 -19
  164. package/contracts/execution-packet.schema.yaml +0 -49
  165. package/contracts/external-host-compatibility.yaml +0 -22
  166. package/contracts/forbidden-shortcuts.catalog.yaml +0 -23
  167. package/contracts/high-risk-admission.schema.yaml +0 -23
  168. package/contracts/high-risk-execution-result.yaml +0 -20
  169. package/contracts/orchestration-state.schema.yaml +0 -41
  170. package/contracts/overflow-continuation.schema.yaml +0 -12
  171. package/contracts/packet.schema.yaml +0 -30
  172. package/contracts/pending-note.schema.yaml +0 -17
  173. package/contracts/prompt.schema.yaml +0 -12
  174. package/contracts/remediation.schema.yaml +0 -16
  175. package/contracts/result.schema.yaml +0 -24
  176. package/contracts/spec-reconstruction-result.yaml +0 -41
  177. package/contracts/sweep-design-result.yaml +0 -349
  178. package/contracts/topic-goal.schema.yaml +0 -87
  179. package/contracts/topic-run-ledger.schema.yaml +0 -72
  180. package/contracts/topic-step-decision.schema.yaml +0 -45
  181. package/contracts/topic.schema.yaml +0 -65
  182. package/contracts/true-close.schema.yaml +0 -15
  183. package/contracts/wave.schema.yaml +0 -29
  184. package/contracts/worker-output.schema.yaml +0 -15
  185. package/contracts/workflow-consumer.schema.yaml +0 -110
  186. package/methodology/audit-sweep-p0p1-recall.yaml +0 -45
  187. package/methodology/authority-convergence-policy.yaml +0 -42
  188. package/methodology/overflow-continuation-policy.yaml +0 -14
  189. package/methodology/skill-exchange-projection.yaml +0 -114
  190. package/methodology/skill-handoff.yaml +0 -34
  191. package/methodology/skill-installer-result.yaml +0 -27
  192. package/methodology/skill-installer-summary-projection.yaml +0 -181
  193. package/methodology/skill-runtime.yaml +0 -23
  194. package/methodology/spec-target-truth-profile.yaml +0 -46
  195. package/methodology/topic-lifecycle-report.yaml +0 -144
  196. package/methodology/topic-lifecycle.yaml +0 -37
  197. package/methodology/topic-naming-ontology.yaml +0 -21
  198. package/methodology/topic-ontology.yaml +0 -38
  199. package/methodology/topic-validation-policy.yaml +0 -9
  200. package/methodology/wave-dag-policy.yaml +0 -14
  201. package/spec/_meta/command-gating-matrix.yaml +0 -143
  202. package/spec/_meta/generate-drift-migration-checklist.yaml +0 -137
  203. package/spec/_meta/governance-routing-cutover-checklist.yaml +0 -35
  204. package/spec/_meta/phase2-impacted-surface-matrix.yaml +0 -44
  205. package/spec/_meta/spec-authority-cutover-readiness.yaml +0 -102
  206. package/spec/bootstrap-state.yaml +0 -99
@@ -1,281 +0,0 @@
1
- import { readdir, writeFile } from "node:fs/promises";
2
- import path from "node:path";
3
- import YAML from "yaml";
4
-
5
- import { pathExists, readTextIfFile } from "./fs-helpers.mjs";
6
- import { parseYamlText } from "./yaml-helpers.mjs";
7
- import { loadTopicRuntimeAuthority, toPortableRelativePath } from "./topic-common.mjs";
8
- import { isIsoUtcTimestamp, loadTopicReport } from "./topic-scaffold.mjs";
9
- import { readFrontmatterObject } from "./topic-artifacts.mjs";
10
-
11
- const RUN_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
12
-
13
- export function validateRunId(value) {
14
- return typeof value == "string" && RUN_ID_PATTERN.test(value);
15
- }
16
- export function runLedgerFilename(runId) {
17
- return `run-ledger-${runId}.yaml`;
18
- }
19
- export function runEventFilename(runId, eventIndex, eventKind) {
20
- return `run-event-${runId}-${String(eventIndex).padStart(4, "0")}-${eventKind}.yaml`;
21
- }
22
- export function stopClassToRunStatus(stopClass) {
23
- return stopClass === "continue"
24
- ? "running"
25
- : stopClass === "require_human_confirmation"
26
- ? "awaiting_human_confirmation"
27
- : stopClass === "await_external_evidence"
28
- ? "awaiting_external_evidence"
29
- : stopClass === "blocked"
30
- ? "blocked"
31
- : stopClass === "completed"
32
- ? "completed"
33
- : "blocked";
34
- }
35
- export function retryPostureForEvent(event) {
36
- return event.stop_class === "require_human_confirmation"
37
- ? "retry_forbidden_until_human_gate"
38
- : event.stop_class === "blocked"
39
- ? "retry_requires_new_packet"
40
- : event.stop_class === "await_external_evidence"
41
- ? "retry_allowed_same_command"
42
- : "not_applicable";
43
- }
44
- export function normalizeArtifactRefs(input) {
45
- const refs = {};
46
- for (const [key, value] of Object.entries(input ?? {}))
47
- typeof key == "string" &&
48
- key.length > 0 &&
49
- typeof value == "string" &&
50
- value.length > 0 &&
51
- (refs[key] = value);
52
- return refs;
53
- }
54
- export async function validatePortableRefExists(projectRoot, ref, label) {
55
- return path.isAbsolute(ref)
56
- ? `${label} must be project-relative: ${ref}`
57
- : (await pathExists(path.join(projectRoot, ref)))?.isFile()
58
- ? null
59
- : `${label} does not resolve to a file: ${ref}`;
60
- }
61
- export async function loadTopicRunEvents(topicDir, runId) {
62
- const entries = await readdir(topicDir, { withFileTypes: true }),
63
- events = [];
64
- for (const entry of entries) {
65
- if (
66
- !entry.isFile() ||
67
- !entry.name.startsWith(`run-event-${runId}-`) ||
68
- !entry.name.endsWith(".yaml")
69
- )
70
- continue;
71
- const eventPath = path.join(topicDir, entry.name),
72
- eventText = await readTextIfFile(eventPath),
73
- event = parseYamlText(eventText ?? "");
74
- !event ||
75
- typeof event != "object" ||
76
- event.run_id !== runId ||
77
- events.push({ event, eventRef: entry.name, eventPath });
78
- }
79
- return events.sort((left, right) => {
80
- const leftIndex = Number(left.event.event_index ?? 0),
81
- rightIndex = Number(right.event.event_index ?? 0);
82
- return leftIndex - rightIndex || left.eventRef.localeCompare(right.eventRef);
83
- });
84
- }
85
- export function latestArtifactRef(events, key) {
86
- for (const entry of [...events].reverse()) {
87
- const value = entry.event.artifact_refs?.[key];
88
- if (typeof value == "string" && value.length > 0) return value;
89
- }
90
- return null;
91
- }
92
- export function buildCurrentHumanGate(events) {
93
- const gateClosingEvents = new Set(["human_gate_resolved", "wave_closed", "topic_closed"]);
94
- for (const entry of [...events].reverse()) {
95
- const event = entry.event;
96
- if (gateClosingEvents.has(event.event_kind)) return null;
97
- if (
98
- event.stop_class === "require_human_confirmation" ||
99
- event.event_kind === "human_gate_opened"
100
- )
101
- return {
102
- event_ref: entry.eventRef,
103
- wave_id: event.wave_id ?? null,
104
- recommended_action: event.recommended_action,
105
- summary: event.summary,
106
- source_ref: event.source_ref,
107
- };
108
- }
109
- return null;
110
- }
111
- export function buildTopicRunLedgerProjection(topic, runId, events, updatedAt) {
112
- const latest = events.at(-1) ?? null,
113
- latestEvent = latest?.event ?? null;
114
- return {
115
- ledger_id: `${topic.topic_id}:${runId}`,
116
- topic_id: topic.topic_id,
117
- run_id: runId,
118
- kind: "topic-run-ledger",
119
- run_status: latestEvent ? stopClassToRunStatus(latestEvent.stop_class) : "running",
120
- event_count: events.length,
121
- event_refs: events.map((entry) => entry.eventRef),
122
- latest_event_ref: latest?.eventRef ?? null,
123
- current_wave_id: latestEvent?.wave_id ?? topic.selected_next_target ?? null,
124
- latest_decision_ref: latestArtifactRef(events, "decision_ref"),
125
- latest_packet_ref: latestArtifactRef(events, "packet_ref"),
126
- latest_prompt_ref: latestArtifactRef(events, "prompt_ref"),
127
- latest_result_ref: latestArtifactRef(events, "result_ref"),
128
- latest_closeout_ref: latestArtifactRef(events, "closeout_ref"),
129
- current_human_gate: buildCurrentHumanGate(events),
130
- retry_posture: latestEvent ? retryPostureForEvent(latestEvent) : "not_applicable",
131
- updated_at: updatedAt,
132
- };
133
- }
134
- export async function writeTopicRunLedger(projectRoot, loaded, runId, events, updatedAt) {
135
- const ledger = buildTopicRunLedgerProjection(loaded.topic, runId, events, updatedAt),
136
- ledgerPath = path.join(loaded.topicDir, runLedgerFilename(runId));
137
- return (
138
- await writeFile(ledgerPath, YAML.stringify(ledger), "utf8"),
139
- {
140
- ok: true,
141
- topicId: loaded.topicId,
142
- topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
143
- runId,
144
- ledgerRef: toPortableRelativePath(path.relative(projectRoot, ledgerPath)),
145
- ledger,
146
- }
147
- );
148
- }
149
- export async function initTopicRunLedger(projectRoot, input, runId, startedAt = new Date().toISOString()) {
150
- if (!validateRunId(runId))
151
- return { ok: false, error: `Topic run ledger refused: invalid run id ${runId}` };
152
- const loaded = await loadTopicReport(projectRoot, input);
153
- if (!loaded.ok) return loaded;
154
- const ledgerPath = path.join(loaded.topicDir, runLedgerFilename(runId));
155
- if ((await readTextIfFile(ledgerPath)) !== null)
156
- return { ok: false, error: `Topic run ledger already exists: ${runId}` };
157
- const report = await writeTopicRunLedger(projectRoot, loaded, runId, [], startedAt);
158
- return { ...report, runStatus: report.ledger.run_status, eventCount: report.ledger.event_count };
159
- }
160
- export async function recordTopicRunEvent(projectRoot, input, options) {
161
- if (!validateRunId(options.runId))
162
- return { ok: false, error: `Topic run event refused: invalid run id ${options.runId}` };
163
- const loaded = await loadTopicReport(projectRoot, input);
164
- if (!loaded.ok) return loaded;
165
- const authority = await loadTopicRuntimeAuthority(projectRoot);
166
- if (!authority.topicRunLedger.eventKinds.includes(options.eventKind))
167
- return {
168
- ok: false,
169
- error: `Topic run event refused: unsupported event kind ${options.eventKind}`,
170
- };
171
- if (!authority.topicStepDecision.stopClasses.includes(options.stopClass))
172
- return {
173
- ok: false,
174
- error: `Topic run event refused: unsupported stop class ${options.stopClass}`,
175
- };
176
- if (!authority.topicStepDecision.recommendedActions.includes(options.recommendedAction))
177
- return {
178
- ok: false,
179
- error: `Topic run event refused: unsupported recommended action ${options.recommendedAction}`,
180
- };
181
- if (!isIsoUtcTimestamp(options.recordedAt))
182
- return {
183
- ok: false,
184
- error: `Topic run event refused: --verified-at must be an ISO-8601 UTC timestamp: ${options.recordedAt}`,
185
- };
186
- if (!options.sourceRef || !options.summary)
187
- return { ok: false, error: "Topic run event refused: --source and --summary are required" };
188
- const sourceError = await validatePortableRefExists(projectRoot, options.sourceRef, "source_ref");
189
- if (sourceError) return { ok: false, error: `Topic run event refused: ${sourceError}` };
190
- const ledgerPath = path.join(loaded.topicDir, runLedgerFilename(options.runId));
191
- if ((await readTextIfFile(ledgerPath)) === null)
192
- return {
193
- ok: false,
194
- error: `Topic run event refused: run ledger does not exist: ${options.runId}`,
195
- };
196
- const artifactRefs = normalizeArtifactRefs(options.artifactRefs),
197
- invalidArtifactKeys = Object.keys(artifactRefs).filter(
198
- (key) => !authority.topicRunLedger.artifactRefKeys.includes(key),
199
- );
200
- if (invalidArtifactKeys.length > 0)
201
- return {
202
- ok: false,
203
- error: `Topic run event refused: unsupported artifact ref keys: ${invalidArtifactKeys.join(", ")}`,
204
- };
205
- for (const [key, ref] of Object.entries(artifactRefs)) {
206
- const artifactError = await validatePortableRefExists(projectRoot, ref, key);
207
- if (artifactError) return { ok: false, error: `Topic run event refused: ${artifactError}` };
208
- }
209
- const eventIndex = (await loadTopicRunEvents(loaded.topicDir, options.runId)).length + 1,
210
- event = {
211
- event_id: `${options.runId}:${String(eventIndex).padStart(4, "0")}:${options.eventKind}`,
212
- topic_id: loaded.topicId,
213
- run_id: options.runId,
214
- event_index: eventIndex,
215
- event_kind: options.eventKind,
216
- stop_class: options.stopClass,
217
- recommended_action: options.recommendedAction,
218
- wave_id: options.waveId ?? loaded.topic.selected_next_target ?? null,
219
- source_ref: options.sourceRef,
220
- summary: options.summary,
221
- recorded_at: options.recordedAt,
222
- artifact_refs: artifactRefs,
223
- },
224
- eventPath = path.join(
225
- loaded.topicDir,
226
- runEventFilename(options.runId, eventIndex, options.eventKind),
227
- );
228
- await writeFile(eventPath, YAML.stringify(event), "utf8");
229
- const updatedEvents = await loadTopicRunEvents(loaded.topicDir, options.runId),
230
- report = await writeTopicRunLedger(
231
- projectRoot,
232
- loaded,
233
- options.runId,
234
- updatedEvents,
235
- options.recordedAt,
236
- );
237
- return {
238
- ...report,
239
- eventId: event.event_id,
240
- eventRef: toPortableRelativePath(path.relative(projectRoot, eventPath)),
241
- runStatus: report.ledger.run_status,
242
- eventCount: report.ledger.event_count,
243
- };
244
- }
245
- export async function buildTopicRunLedger(
246
- projectRoot,
247
- input,
248
- runId,
249
- updatedAt = new Date().toISOString(),
250
- ) {
251
- if (!validateRunId(runId))
252
- return { ok: false, error: `Topic run ledger refused: invalid run id ${runId}` };
253
- const loaded = await loadTopicReport(projectRoot, input);
254
- if (!loaded.ok) return loaded;
255
- const ledgerPath = path.join(loaded.topicDir, runLedgerFilename(runId));
256
- if ((await readTextIfFile(ledgerPath)) === null)
257
- return { ok: false, error: `Topic run ledger not found: ${runId}` };
258
- const events = await loadTopicRunEvents(loaded.topicDir, runId),
259
- report = await writeTopicRunLedger(projectRoot, loaded, runId, events, updatedAt);
260
- return { ...report, runStatus: report.ledger.run_status, eventCount: report.ledger.event_count };
261
- }
262
- export async function readTopicRunLedger(projectRoot, input, runId) {
263
- if (!validateRunId(runId))
264
- return { ok: false, error: `Topic run ledger refused: invalid run id ${runId}` };
265
- const loaded = await loadTopicReport(projectRoot, input);
266
- if (!loaded.ok) return loaded;
267
- const ledgerPath = path.join(loaded.topicDir, runLedgerFilename(runId)),
268
- ledgerText = await readTextIfFile(ledgerPath);
269
- if (ledgerText === null) return { ok: false, error: `Topic run ledger not found: ${runId}` };
270
- const ledger = parseYamlText(ledgerText);
271
- return {
272
- ok: true,
273
- topicId: loaded.topicId,
274
- topicRef: toPortableRelativePath(path.relative(projectRoot, loaded.topicDir)),
275
- runId,
276
- ledgerRef: toPortableRelativePath(path.relative(projectRoot, ledgerPath)),
277
- ledger,
278
- runStatus: ledger.run_status,
279
- eventCount: ledger.event_count,
280
- };
281
- }
@@ -1,173 +0,0 @@
1
- import { readdir } from "node:fs/promises";
2
-
3
- function getTopicWaves(topic) {
4
- return Array.isArray(topic.waves) ? topic.waves.map((entry) => ({ ...entry })) : [];
5
- }
6
-
7
- function fileReferencesWave(fileName, waveId) {
8
- return fileName.includes(waveId);
9
- }
10
-
11
- function buildObservedLineage(entry) {
12
- if (entry.closeouts > 0) return "closed_lineage";
13
- if (entry.results > 0) return "result_lineage";
14
- if (entry.packets > 0) return "packet_lineage";
15
- if (entry.remediations > 0 || entry.exec_packs > 0 || entry.decision_reviews > 0) {
16
- return "auxiliary_lineage";
17
- }
18
- return "declared_only";
19
- }
20
-
21
- function isRecognizedLifecycleArtifactName(fileName) {
22
- if (!fileName.endsWith(".md")) return true;
23
- if (fileName.startsWith("packet-")) {
24
- return /^packet-wave-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/.test(fileName)
25
- || /^packet-true-close(?:-[a-z0-9]+)*\.md$/.test(fileName);
26
- }
27
- if (fileName.startsWith("result-")) {
28
- return /^result-wave-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/.test(fileName)
29
- || /^result-topic-true-close(?:-[a-z0-9]+)*\.md$/.test(fileName)
30
- || /^result-true-close(?:-[a-z0-9]+)*\.md$/.test(fileName);
31
- }
32
- if (fileName.startsWith("closeout-")) {
33
- return /^closeout-wave-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/.test(fileName)
34
- || /^closeout-topic(?:-[a-z0-9]+)*\.md$/.test(fileName)
35
- || /^closeout-true-close(?:-[a-z0-9]+)*\.md$/.test(fileName);
36
- }
37
- if (fileName.startsWith("decision-review-")) {
38
- return /^decision-review-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/.test(fileName);
39
- }
40
- if (fileName.startsWith("prompt-")) {
41
- return /^prompt-[a-z0-9-]+-(worker|audit)\.md$/.test(fileName);
42
- }
43
- if (fileName.startsWith("overflow-continuation-")) {
44
- return /^overflow-continuation-wave-[a-z0-9]+(?:-[a-z0-9]+)*\.md$/.test(fileName);
45
- }
46
- return true;
47
- }
48
-
49
- function resolveDeclaredWaveArtifactLineage(fileNames, topicWaveIds) {
50
- const declared = Array.from(topicWaveIds);
51
- const resolved = [];
52
- const unresolved = [];
53
- const ambiguous = [];
54
-
55
- for (const fileName of fileNames) {
56
- const matches = declared.filter((waveId) => fileReferencesWave(fileName, waveId));
57
- if (matches.length === 1) {
58
- resolved.push({ fileName, waveId: matches[0] });
59
- } else if (matches.length === 0) {
60
- unresolved.push(fileName);
61
- } else {
62
- ambiguous.push({ fileName, waveIds: matches });
63
- }
64
- }
65
-
66
- return { resolved, unresolved, ambiguous };
67
- }
68
-
69
- function formatLifecycleLineageFailures(lineage) {
70
- return [
71
- ...lineage.unresolved.map((fileName) => `${fileName}:unresolved`),
72
- ...lineage.ambiguous.map((entry) => `${entry.fileName}:ambiguous:${entry.waveIds.join(",")}`),
73
- ];
74
- }
75
-
76
- async function analyzeTopicArtifacts(topicDir, topic) {
77
- const files = (await readdir(topicDir, { withFileTypes: true }))
78
- .filter((entry) => entry.isFile())
79
- .map((entry) => entry.name);
80
- const packetFiles = files.filter((name) => name.startsWith("packet-") && name.endsWith(".md"));
81
- const resultFiles = files.filter((name) => name.startsWith("result-") && name.endsWith(".md"));
82
- const closeoutFiles = files.filter((name) => name.startsWith("closeout-") && name.endsWith(".md"));
83
- const decisionReviewFiles = files.filter((name) => name.startsWith("decision-review-") && name.endsWith(".md"));
84
- const remediationFiles = files.filter((name) => name.includes("remediation") && name.endsWith(".md"));
85
- const overflowFiles = files.filter((name) => name.includes("overflow-continuation") || name.includes("remediation-continuation"));
86
- const execPackFiles = files.filter((name) => name.includes("exec-pack-") && name.endsWith(".md"));
87
- const trueCloseFiles = files.filter((name) => name === "topic-true-close-audit.md"
88
- || name.startsWith("result-topic-true-close")
89
- || name === "closeout-topic-true-close.md");
90
- const ambiguousLifecycleFiles = files.filter((name) => /^(packet|result|closeout|decision-review|prompt|overflow-continuation)-/.test(name)
91
- && !isRecognizedLifecycleArtifactName(name));
92
- const topicWaveIds = new Set(getTopicWaves(topic).map((entry) => entry.wave_id));
93
- const packetWaveFiles = packetFiles.filter((name) => !name.startsWith("packet-true-close"));
94
- const resultWaveFiles = resultFiles.filter((name) => !name.startsWith("result-topic-true-close") && !name.startsWith("result-true-close"));
95
- const closeoutWaveFiles = closeoutFiles.filter((name) => !name.startsWith("closeout-topic") && !name.startsWith("closeout-true-close"));
96
- const packetLineage = resolveDeclaredWaveArtifactLineage(packetWaveFiles, topicWaveIds);
97
- const resultLineage = resolveDeclaredWaveArtifactLineage(resultWaveFiles, topicWaveIds);
98
- const closeoutLineage = resolveDeclaredWaveArtifactLineage(closeoutWaveFiles, topicWaveIds);
99
- const packetWaveIds = new Set(packetLineage.resolved.map((entry) => entry.waveId));
100
- const resultWaveIds = resultLineage.resolved.map((entry) => entry.waveId);
101
- const closeoutWaveIds = new Set(closeoutLineage.resolved.map((entry) => entry.waveId));
102
- const closeoutWaveIdsArray = Array.from(closeoutWaveIds);
103
- const unresolvedPacketWaveRefs = formatLifecycleLineageFailures(packetLineage);
104
- const unresolvedResultWaveIds = formatLifecycleLineageFailures(resultLineage);
105
- const unresolvedCloseoutWaveRefs = formatLifecycleLineageFailures(closeoutLineage);
106
- const activeWaveCloseoutConflicts = getTopicWaves(topic)
107
- .filter((entry) => !["closed", "retired", "superseded"].includes(entry.state)
108
- && closeoutFiles.some((name) => fileReferencesWave(name, entry.wave_id) || closeoutWaveIds.has(entry.wave_id)))
109
- .map((entry) => `${entry.wave_id}:${entry.state}`);
110
- const topicHasOpenBlockers = getTopicWaves(topic).some((entry) => !["closed", "retired", "superseded"].includes(entry.state))
111
- || (typeof topic.selected_next_target === "string"
112
- && topic.selected_next_target.length > 0
113
- && topic.selected_next_target !== "topic_design_baseline");
114
- const prematureTrueClose = trueCloseFiles.length > 0 && topicHasOpenBlockers;
115
- const observedWaveIds = Array.from(new Set([
116
- ...Array.from(topicWaveIds),
117
- ...Array.from(packetWaveIds),
118
- ...closeoutWaveIdsArray,
119
- ...resultWaveIds,
120
- ])).sort();
121
- const observedWaves = observedWaveIds.map((waveId) => {
122
- const observed = {
123
- wave_id: waveId,
124
- packets: packetFiles.filter((name) => fileReferencesWave(name, waveId)).length,
125
- results: resultFiles.filter((name) => fileReferencesWave(name, waveId)).length,
126
- closeouts: closeoutFiles.filter((name) => fileReferencesWave(name, waveId)).length,
127
- decision_reviews: decisionReviewFiles.filter((name) => fileReferencesWave(name, waveId)).length,
128
- remediations: remediationFiles.filter((name) => fileReferencesWave(name, waveId)).length,
129
- overflow_continuations: overflowFiles.filter((name) => fileReferencesWave(name, waveId)).length,
130
- exec_packs: execPackFiles.filter((name) => fileReferencesWave(name, waveId)).length,
131
- declared_in_topic_yaml: topicWaveIds.has(waveId),
132
- };
133
- return { ...observed, observed_lineage: buildObservedLineage(observed) };
134
- });
135
-
136
- return {
137
- files,
138
- counts: {
139
- files: files.length,
140
- packets: packetFiles.length,
141
- results: resultFiles.length,
142
- closeouts: closeoutFiles.length,
143
- decision_reviews: decisionReviewFiles.length,
144
- remediations: remediationFiles.length,
145
- overflow_continuations: overflowFiles.length,
146
- exec_packs: execPackFiles.length,
147
- true_close_artifacts: trueCloseFiles.length,
148
- },
149
- waveIds: observedWaveIds,
150
- observedWaves,
151
- featureFlags: {
152
- decision_review_lineage: decisionReviewFiles.length > 0,
153
- remediation_lineage: remediationFiles.length > 0,
154
- overflow_lineage: overflowFiles.length > 0,
155
- true_close_lineage: trueCloseFiles.length >= 2,
156
- exec_pack_lineage: execPackFiles.length > 0,
157
- },
158
- unresolvedPacketWaveRefs,
159
- unresolvedResultWaveIds,
160
- unresolvedCloseoutWaveRefs,
161
- closeoutWaveIds: closeoutWaveIdsArray,
162
- ambiguousLifecycleFiles,
163
- activeWaveCloseoutConflicts,
164
- prematureTrueClose,
165
- };
166
- }
167
-
168
- export {
169
- analyzeTopicArtifacts,
170
- fileReferencesWave,
171
- isRecognizedLifecycleArtifactName,
172
- resolveDeclaredWaveArtifactLineage,
173
- };