@nimiplatform/nimi-coding 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +19 -20
  2. package/adapters/oh-my-codex/README.md +8 -9
  3. package/cli/commands/audit-sweep.mjs +10 -10
  4. package/cli/commands/classify-spec-tree.mjs +5 -0
  5. package/cli/commands/closeout.mjs +3 -0
  6. package/cli/commands/generate-spec-derived-docs.mjs +20 -0
  7. package/cli/commands/generate-spec-migration-plan.mjs +30 -0
  8. package/cli/commands/start.mjs +5 -1
  9. package/cli/commands/surface-validator-command.mjs +49 -0
  10. package/cli/commands/sweep-design.mjs +295 -0
  11. package/cli/commands/sweep.mjs +22 -0
  12. package/cli/commands/sync.mjs +132 -0
  13. package/cli/commands/topic-formatters.mjs +8 -8
  14. package/cli/commands/validate-ai-governance.mjs +167 -46
  15. package/cli/commands/validate-domain-admission.mjs +5 -0
  16. package/cli/commands/validate-guidance-bodies.mjs +5 -0
  17. package/cli/commands/validate-placement.mjs +5 -0
  18. package/cli/commands/validate-projection-edges.mjs +5 -0
  19. package/cli/commands/validate-spec-audit.mjs +5 -1
  20. package/cli/commands/validate-table-family.mjs +5 -0
  21. package/cli/commands/validate-tracked-output-admission.mjs +5 -0
  22. package/cli/constants.mjs +5 -49
  23. package/cli/help.mjs +33 -11
  24. package/cli/index.mjs +20 -2
  25. package/cli/lib/audit-sweep-runtime/admissions.mjs +38 -29
  26. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +8 -0
  27. package/cli/lib/audit-sweep-runtime/chunks.mjs +11 -11
  28. package/cli/lib/audit-sweep-runtime/closeout.mjs +8 -8
  29. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +3 -3
  30. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +10 -10
  31. package/cli/lib/audit-sweep-runtime/common.mjs +7 -7
  32. package/cli/lib/audit-sweep-runtime/format.mjs +3 -3
  33. package/cli/lib/audit-sweep-runtime/ingest.mjs +8 -8
  34. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +24 -27
  35. package/cli/lib/audit-sweep-runtime/inventory.mjs +58 -18
  36. package/cli/lib/audit-sweep-runtime/ledger.mjs +1 -1
  37. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +2 -2
  38. package/cli/lib/audit-sweep-runtime/remediation.mjs +6 -6
  39. package/cli/lib/audit-sweep-runtime/rerun.mjs +6 -6
  40. package/cli/lib/audit-sweep-runtime/status.mjs +1 -1
  41. package/cli/lib/audit-sweep-runtime/validators.mjs +2 -2
  42. package/cli/lib/authority-convergence.mjs +397 -2
  43. package/cli/lib/blueprint-audit.mjs +5 -5
  44. package/cli/lib/closeout.mjs +126 -3
  45. package/cli/lib/contracts.mjs +21 -17
  46. package/cli/lib/handoff.mjs +29 -11
  47. package/cli/lib/high-risk-admission.mjs +60 -11
  48. package/cli/lib/high-risk-decision.mjs +31 -2
  49. package/cli/lib/high-risk-ingest.mjs +5 -1
  50. package/cli/lib/high-risk-review.mjs +5 -1
  51. package/cli/lib/internal/contracts-parse.mjs +195 -24
  52. package/cli/lib/internal/contracts-validators.mjs +3 -2
  53. package/cli/lib/internal/doctor-bootstrap-surface.mjs +82 -35
  54. package/cli/lib/internal/doctor-delegated-surface.mjs +1 -1
  55. package/cli/lib/internal/doctor-finalize.mjs +12 -8
  56. package/cli/lib/internal/doctor-inspectors.mjs +34 -1
  57. package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +74 -12
  58. package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +24 -6
  59. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +18 -23
  60. package/cli/lib/internal/surface-taxonomy-validators.mjs +931 -0
  61. package/cli/lib/internal/validators-spec.mjs +229 -20
  62. package/cli/lib/sweep-design-runtime/common.mjs +246 -0
  63. package/cli/lib/sweep-design-runtime/engine.mjs +733 -0
  64. package/cli/lib/sweep-design-runtime/fix-topic.mjs +414 -0
  65. package/cli/lib/sweep-design-runtime/lifecycle.mjs +54 -0
  66. package/cli/lib/sweep-design-runtime/results.mjs +324 -0
  67. package/cli/lib/sweep-design.mjs +8 -0
  68. package/cli/lib/sync.mjs +143 -0
  69. package/cli/lib/topic-artifacts.mjs +186 -0
  70. package/cli/lib/topic-authority-coverage.mjs +73 -0
  71. package/cli/lib/topic-closeout.mjs +560 -0
  72. package/cli/lib/topic-common.mjs +404 -0
  73. package/cli/lib/topic-decisions.mjs +332 -0
  74. package/cli/lib/topic-draft-packets.mjs +126 -7
  75. package/cli/lib/topic-execution.mjs +515 -0
  76. package/cli/lib/topic-goal.mjs +112 -33
  77. package/cli/lib/topic-ledger.mjs +281 -0
  78. package/cli/lib/topic-lifecycle-artifacts.mjs +173 -0
  79. package/cli/lib/topic-root-validation.mjs +288 -0
  80. package/cli/lib/topic-runner-commands.mjs +174 -0
  81. package/cli/lib/topic-runner-deferral.mjs +532 -0
  82. package/cli/lib/topic-runner-stale-gates.mjs +114 -0
  83. package/cli/lib/topic-runner-validation.mjs +138 -0
  84. package/cli/lib/topic-runner.mjs +109 -154
  85. package/cli/lib/topic-scaffold.mjs +252 -0
  86. package/cli/lib/topic-waves.mjs +403 -0
  87. package/cli/lib/topic.mjs +81 -93
  88. package/cli/lib/value-helpers.mjs +6 -1
  89. package/cli/seeds/bootstrap.mjs +96 -20
  90. package/cli/seeds/seed-policy.yaml +67 -0
  91. package/config/bootstrap.yaml +1 -1
  92. package/config/skill-manifest.yaml +4 -2
  93. package/config/spec-generation-inputs.yaml +41 -19
  94. package/contracts/audit-remediation-map.schema.yaml +1 -0
  95. package/contracts/audit-sweep-result.yaml +4 -0
  96. package/contracts/domain-admission.schema.yaml +56 -0
  97. package/contracts/migration-inventory.schema.yaml +80 -0
  98. package/contracts/negative-fixtures.yaml +91 -0
  99. package/contracts/placement-contract.schema.yaml +163 -0
  100. package/contracts/projection-edge.schema.yaml +130 -0
  101. package/contracts/shared-enums.yaml +68 -0
  102. package/contracts/spec-generation-audit.schema.yaml +19 -4
  103. package/contracts/spec-generation-inputs.schema.yaml +130 -29
  104. package/contracts/spec-reconstruction-result.yaml +9 -5
  105. package/contracts/surface-taxonomy.schema.yaml +201 -0
  106. package/contracts/sweep-design-result.yaml +349 -0
  107. package/contracts/table-family.schema.yaml +114 -0
  108. package/contracts/topic-goal.schema.yaml +10 -1
  109. package/contracts/tracked-output-admission.schema.yaml +70 -0
  110. package/contracts/workflow-consumer.schema.yaml +112 -0
  111. package/methodology/audit-sweep-p0p1-recall.yaml +1 -1
  112. package/methodology/spec-reconstruction.yaml +53 -30
  113. package/package.json +5 -4
  114. package/spec/_meta/command-gating-matrix.yaml +33 -0
  115. package/spec/_meta/generate-drift-migration-checklist.yaml +44 -62
  116. package/spec/_meta/governance-routing-cutover-checklist.yaml +3 -3
  117. package/spec/_meta/phase2-impacted-surface-matrix.yaml +14 -14
  118. package/spec/_meta/spec-authority-cutover-readiness.yaml +3 -5
  119. package/spec/_meta/spec-tree-model.yaml +104 -36
  120. package/spec/bootstrap-state.yaml +36 -36
  121. package/spec/product-scope.yaml +13 -10
@@ -0,0 +1,532 @@
1
+ import { readdir, readFile, writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import YAML from "yaml";
4
+
5
+ function toPortablePath(value) {
6
+ return value.split(path.sep).join("/");
7
+ }
8
+
9
+ function projectRef(projectRoot, absolutePath) {
10
+ return toPortablePath(path.relative(projectRoot, absolutePath));
11
+ }
12
+
13
+ function safeSegment(value) {
14
+ return String(value).replace(/[^a-zA-Z0-9._-]+/g, "-");
15
+ }
16
+
17
+ function isTerminalWave(wave) {
18
+ return ["closed", "retired", "superseded"].includes(wave?.state);
19
+ }
20
+
21
+ function getTopicWaves(topic) {
22
+ return Array.isArray(topic?.waves) ? topic.waves : [];
23
+ }
24
+
25
+ function findDeferredBlockerNextWave(topic, blockedWaveId) {
26
+ const waves = getTopicWaves(topic);
27
+ const terminalIds = new Set(waves.filter(isTerminalWave).map((wave) => wave.wave_id));
28
+ const ready = waves.filter((wave) => {
29
+ if (wave.wave_id === blockedWaveId) return false;
30
+ if (isTerminalWave(wave)) return false;
31
+ if (!["candidate", "preflight_draft"].includes(wave.state)) return false;
32
+ const deps = Array.isArray(wave.deps) ? wave.deps : [];
33
+ if (deps.includes(blockedWaveId)) return false;
34
+ return deps.every((dep) => terminalIds.has(dep));
35
+ });
36
+ return ready.length > 0 ? ready[0] : null;
37
+ }
38
+
39
+ function decisionText(decision) {
40
+ return JSON.stringify({
41
+ reason_code: decision?.reason_code,
42
+ recommended_action: decision?.recommended_action,
43
+ recommended_decision: decision?.recommended_decision,
44
+ recommendation_rationale: decision?.recommendation_rationale,
45
+ blocking_checks: decision?.blocking_checks ?? [],
46
+ }).toLowerCase();
47
+ }
48
+
49
+ function isDisallowedGlobalBlocker(decision) {
50
+ const text = decisionText(decision);
51
+ return [
52
+ "global topic",
53
+ "global_topic",
54
+ "topic contract",
55
+ "topic_contract",
56
+ "contract-changing",
57
+ "contract changing",
58
+ "source audit",
59
+ "source_audit",
60
+ "source sweep",
61
+ "source_sweep_design",
62
+ "sweep-design artifact",
63
+ "lowered gate",
64
+ "lower gate",
65
+ "destructive evidence deletion",
66
+ "evidence deletion",
67
+ "product semantics",
68
+ "semantic fork",
69
+ "authority/scope decision",
70
+ "explicit human decision",
71
+ ].some((pattern) => text.includes(pattern));
72
+ }
73
+
74
+ function isDisallowedWaveBlocker(wave) {
75
+ const text = JSON.stringify({
76
+ owner_domain: wave?.owner_domain,
77
+ goal: wave?.goal,
78
+ blocker_scope: wave?.blocker_scope,
79
+ source_sweep_design: wave?.source_sweep_design,
80
+ }).toLowerCase();
81
+ if (
82
+ Array.isArray(wave?.source_sweep_design?.blocked_gate_refs) &&
83
+ wave.source_sweep_design.blocked_gate_refs.length > 0
84
+ ) {
85
+ return true;
86
+ }
87
+ return [
88
+ "global topic",
89
+ "global_topic",
90
+ "topic contract",
91
+ "topic_contract",
92
+ "contract-changing",
93
+ "contract changing",
94
+ "lowered gate",
95
+ "lower gate",
96
+ "destructive evidence deletion",
97
+ "product semantics",
98
+ "semantic fork",
99
+ ].some((pattern) => text.includes(pattern));
100
+ }
101
+
102
+ function isDeferrableLocalWaveDecision(topic, decision) {
103
+ if (!decision || decision.stop_class !== "blocked") {
104
+ return { ok: false, reason: "decision_not_blocked" };
105
+ }
106
+ if (decision.recommended_action !== "open_remediation") {
107
+ return { ok: false, reason: "not_open_remediation" };
108
+ }
109
+ const wave = getTopicWaves(topic).find((entry) => entry.wave_id === decision.wave_id);
110
+ if (!wave) {
111
+ return { ok: false, reason: "blocked_wave_not_found" };
112
+ }
113
+ if (wave.state !== "needs_revision") {
114
+ return { ok: false, reason: "blocked_wave_not_needs_revision" };
115
+ }
116
+ if (isDisallowedGlobalBlocker(decision) || isDisallowedWaveBlocker(wave)) {
117
+ return { ok: false, reason: "global_or_contract_blocker" };
118
+ }
119
+ const nextWave = findDeferredBlockerNextWave(topic, wave.wave_id);
120
+ if (!nextWave) {
121
+ return { ok: false, reason: "no_independent_ready_wave" };
122
+ }
123
+ return { ok: true, wave, nextWave };
124
+ }
125
+
126
+ function evidenceFlagPattern(name, value) {
127
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
128
+ return new RegExp(`${escaped}\\s*:\\s*${value}`, "i");
129
+ }
130
+
131
+ function hasFalseEvidenceFlag(text, names) {
132
+ return names.some((name) => evidenceFlagPattern(name, "false").test(text));
133
+ }
134
+
135
+ function hasTrueEvidenceFlag(text, names) {
136
+ return names.some((name) => evidenceFlagPattern(name, "true").test(text));
137
+ }
138
+
139
+ function cleanStructuredListItem(value) {
140
+ return String(value ?? "")
141
+ .replace(/\s+#.*$/u, "")
142
+ .trim()
143
+ .replace(/^[-\s]+/u, "")
144
+ .replace(/^["'`]+|["'`]+$/gu, "")
145
+ .trim();
146
+ }
147
+
148
+ function parseInlineStructuredList(value) {
149
+ const trimmed = value.trim();
150
+ if (!trimmed) return [];
151
+ if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
152
+ return trimmed
153
+ .slice(1, -1)
154
+ .split(",")
155
+ .map(cleanStructuredListItem)
156
+ .filter(Boolean);
157
+ }
158
+ return [cleanStructuredListItem(trimmed)].filter(Boolean);
159
+ }
160
+
161
+ function extractStructuredListValues(text, fieldNames) {
162
+ const escapedNames = fieldNames.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
163
+ const fieldPattern = new RegExp(`^\\s*(?:${escapedNames.join("|")})\\s*:\\s*(.*)$`, "iu");
164
+ const lines = text.split(/\r?\n/u);
165
+ const values = [];
166
+ for (let index = 0; index < lines.length; index += 1) {
167
+ const line = lines[index];
168
+ const match = line.match(fieldPattern);
169
+ if (!match) continue;
170
+ const inline = match[1].trim();
171
+ if (inline) {
172
+ values.push(...parseInlineStructuredList(inline));
173
+ continue;
174
+ }
175
+ const baseIndent = line.match(/^\s*/u)?.[0].length ?? 0;
176
+ for (let scan = index + 1; scan < lines.length; scan += 1) {
177
+ const nextLine = lines[scan];
178
+ if (!nextLine.trim()) continue;
179
+ const nextIndent = nextLine.match(/^\s*/u)?.[0].length ?? 0;
180
+ const item = nextLine.match(/^\s*-\s+(.+)$/u);
181
+ if (item) {
182
+ values.push(cleanStructuredListItem(item[1]));
183
+ continue;
184
+ }
185
+ if (nextIndent <= baseIndent && /^\s*\S/u.test(nextLine)) break;
186
+ if (/^\s*[a-zA-Z0-9_.-]+\s*:/u.test(nextLine)) break;
187
+ }
188
+ }
189
+ return [...new Set(values.filter(Boolean))];
190
+ }
191
+
192
+ function isConcreteAuthorityRef(value) {
193
+ const ref = cleanStructuredListItem(value);
194
+ if (!ref) return false;
195
+ if (path.isAbsolute(ref)) return false;
196
+ if (/[<>{}]|\.\.\./u.test(ref)) return false;
197
+ if (/\s/u.test(ref)) return false;
198
+ if (!ref.includes("/")) return false;
199
+ if (/(^|[/_.-])(tbd|todo|placeholder|unknown|none|null)([/_.-]|$)/iu.test(ref)) return false;
200
+ if (ref.startsWith(".nimi/local/audit/") || ref.startsWith(".nimi/local/sweep-design/")) return false;
201
+ return /\.(?:md|ya?ml|json|toml)$/iu.test(ref);
202
+ }
203
+
204
+ function extractConcreteMissingAuthorityRefs(text) {
205
+ return extractStructuredListValues(text, [
206
+ "missing_authority_refs",
207
+ "missing_authority_owner_refs",
208
+ "missing_canonical_seam_refs",
209
+ ]).filter(isConcreteAuthorityRef);
210
+ }
211
+
212
+ function evidenceDeclaresDisallowedBlocker(text) {
213
+ const normalized = text.toLowerCase();
214
+ if (
215
+ hasTrueEvidenceFlag(normalized, [
216
+ "source_audit_mutated",
217
+ "source_audit_findings_mutated",
218
+ "source_sweep_design_mutated",
219
+ "source_sweep_design_artifacts_mutated",
220
+ "lowered_gate",
221
+ "lowered_gates",
222
+ "requires_lowered_gate",
223
+ "topic_global_contract_change_required",
224
+ "topic_contract_change_required",
225
+ "global_contract_change_required",
226
+ "requires_global_contract_change",
227
+ "source_evidence_mutated",
228
+ "source_evidence_change_required",
229
+ "requires_source_evidence_change",
230
+ "product_semantic_ambiguity",
231
+ "unresolved_authority_scope_gate_product_semantic_ambiguity",
232
+ "unresolved_authority_conflict",
233
+ "authority_conflict",
234
+ "implementation_conflict",
235
+ "destructive_evidence_deletion",
236
+ "destructive_evidence_deletion_required",
237
+ "requires_destructive_evidence_deletion",
238
+ "explicit_human_product_decision_required",
239
+ "explicit_human_decision_packet",
240
+ "product_semantic_decision_required",
241
+ "requires_product_semantic_decision",
242
+ "explicit_human_decision_required",
243
+ "requires_explicit_human_decision",
244
+ ])
245
+ ) {
246
+ return true;
247
+ }
248
+ const disallowedPatterns = [
249
+ "lowered gate required",
250
+ "lowered validation gate",
251
+ "global topic contract change",
252
+ "topic contract change required",
253
+ "source evidence change required",
254
+ "source audit finding mutation required",
255
+ "source sweep-design artifact mutation required",
256
+ "destructive evidence deletion",
257
+ "product semantics fork",
258
+ "semantic fork",
259
+ "unresolved authority conflict",
260
+ "authority conflict",
261
+ "explicit human decision",
262
+ ];
263
+ return normalized.split(/\r?\n\s*\r?\n/u).some((block) => {
264
+ if (!disallowedPatterns.some((pattern) => block.includes(pattern))) {
265
+ return false;
266
+ }
267
+ return !/\b(no|not|false|without)\b|does not|do not|isn't|is not|aren't|are not/u.test(block);
268
+ });
269
+ }
270
+
271
+ function evidenceDeclaresBroadAmbiguity(text) {
272
+ return hasTrueEvidenceFlag(text.toLowerCase(), [
273
+ "unresolved_authority_scope_gate_product_semantic_ambiguity",
274
+ ]);
275
+ }
276
+
277
+ function evidenceHasPositiveLocalOnlyProof(text) {
278
+ return hasTrueEvidenceFlag(text.toLowerCase(), [
279
+ "local_packet_authority_scope_remediation_only",
280
+ ]);
281
+ }
282
+
283
+ function evidenceHasNoProductSemanticAmbiguityProof(text) {
284
+ return hasFalseEvidenceFlag(text.toLowerCase(), [
285
+ "product_semantic_ambiguity",
286
+ ]);
287
+ }
288
+
289
+ function missingRequiredFalseEvidenceFlagReason(text) {
290
+ const normalized = text.toLowerCase();
291
+ const required = [
292
+ {
293
+ names: ["source_audit_mutated", "source_audit_findings_mutated"],
294
+ reason: "missing_source_audit_non_mutation_evidence",
295
+ },
296
+ {
297
+ names: ["source_sweep_design_mutated", "source_sweep_design_artifacts_mutated"],
298
+ reason: "missing_source_sweep_design_non_mutation_evidence",
299
+ },
300
+ {
301
+ names: ["lowered_gate", "lowered_gates", "requires_lowered_gate"],
302
+ reason: "missing_no_lowered_gate_evidence",
303
+ },
304
+ {
305
+ names: [
306
+ "topic_global_contract_change_required",
307
+ "topic_contract_change_required",
308
+ "global_contract_change_required",
309
+ "requires_global_contract_change",
310
+ ],
311
+ reason: "missing_no_global_contract_change_evidence",
312
+ },
313
+ {
314
+ names: ["source_evidence_mutated", "source_evidence_change_required", "requires_source_evidence_change"],
315
+ reason: "missing_no_source_evidence_change_evidence",
316
+ },
317
+ {
318
+ names: [
319
+ "destructive_evidence_deletion",
320
+ "destructive_evidence_deletion_required",
321
+ "requires_destructive_evidence_deletion",
322
+ ],
323
+ reason: "missing_no_destructive_evidence_deletion_evidence",
324
+ },
325
+ {
326
+ names: [
327
+ "explicit_human_product_decision_required",
328
+ "explicit_human_decision_required",
329
+ "explicit_human_decision_packet",
330
+ "requires_explicit_human_decision",
331
+ "product_semantic_decision_required",
332
+ "requires_product_semantic_decision",
333
+ ],
334
+ reason: "missing_no_explicit_human_product_decision_evidence",
335
+ },
336
+ ];
337
+ const missing = required.find((entry) => !hasFalseEvidenceFlag(normalized, entry.names));
338
+ return missing?.reason ?? null;
339
+ }
340
+
341
+ function evidenceDeclaresLocalPacketAuthorityScopeRemediation(text) {
342
+ const normalized = text.toLowerCase();
343
+ return [
344
+ "required_remediation: local wave packet authority/scope remediation only",
345
+ "local wave packet authority/scope remediation only",
346
+ "local packet authority/scope remediation",
347
+ "authority/scope mismatch in the packet metadata",
348
+ "authority/scope mismatch in packet metadata",
349
+ "packet authority omission",
350
+ "packet metadata omission",
351
+ "regenerate or remediate the topic-local implementation packet",
352
+ "regenerate or remediate the topic-local",
353
+ ].some((pattern) => normalized.includes(pattern));
354
+ }
355
+
356
+ function resultVerifiedAtMs(text) {
357
+ const match = text.match(/\bverified_at\s*:\s*["']?([^"'\n\r]+)["']?/iu);
358
+ if (!match) return Number.NaN;
359
+ const parsed = Date.parse(match[1].trim());
360
+ return Number.isFinite(parsed) ? parsed : Number.NaN;
361
+ }
362
+
363
+ function isNonDeferrableEvidenceReason(reason) {
364
+ return [
365
+ "blocking_result_declares_non_deferrable_gate",
366
+ "unresolved_ambiguity_not_local_deferrable",
367
+ ].includes(reason);
368
+ }
369
+
370
+ async function evaluateDeferrableLocalWaveEvidence(projectRoot, resultRefs) {
371
+ if (resultRefs.length === 0) {
372
+ return { ok: false, reason: "missing_blocking_result_evidence" };
373
+ }
374
+ const candidates = [];
375
+ for (const [index, ref] of resultRefs.entries()) {
376
+ const text = await readFile(path.join(projectRoot, ref), "utf8");
377
+ if (!/verdict:\s*NEEDS_REVISION/i.test(text)) continue;
378
+ const evidence = evaluateDeferrableLocalWaveEvidenceText(text);
379
+ candidates.push({
380
+ evidence,
381
+ index,
382
+ ref,
383
+ verifiedAtMs: resultVerifiedAtMs(text),
384
+ });
385
+ }
386
+ if (candidates.length === 0) {
387
+ return { ok: false, reason: "missing_blocking_result_evidence" };
388
+ }
389
+ const nonDeferrable = candidates.find((candidate) => (
390
+ !candidate.evidence.ok && isNonDeferrableEvidenceReason(candidate.evidence.reason)
391
+ ));
392
+ if (nonDeferrable) {
393
+ return nonDeferrable.evidence;
394
+ }
395
+ candidates.sort((left, right) => {
396
+ const leftTime = Number.isFinite(left.verifiedAtMs) ? left.verifiedAtMs : -Infinity;
397
+ const rightTime = Number.isFinite(right.verifiedAtMs) ? right.verifiedAtMs : -Infinity;
398
+ return rightTime - leftTime || right.index - left.index;
399
+ });
400
+ const latest = candidates[0];
401
+ return latest.evidence.ok
402
+ ? { ...latest.evidence, resultRefs: [latest.ref] }
403
+ : latest.evidence;
404
+ }
405
+
406
+ function evaluateDeferrableLocalWaveEvidenceText(text) {
407
+ if (!/verdict:\s*NEEDS_REVISION/i.test(text)) {
408
+ return { ok: false, reason: "blocking_result_not_needs_revision" };
409
+ }
410
+ const missingFalseFlagReason = missingRequiredFalseEvidenceFlagReason(text);
411
+ if (missingFalseFlagReason) {
412
+ return { ok: false, reason: missingFalseFlagReason };
413
+ }
414
+ if (evidenceDeclaresDisallowedBlocker(text)) {
415
+ return { ok: false, reason: "blocking_result_declares_non_deferrable_gate" };
416
+ }
417
+ if (evidenceDeclaresBroadAmbiguity(text)) {
418
+ return { ok: false, reason: "unresolved_ambiguity_not_local_deferrable" };
419
+ }
420
+ if (!evidenceHasPositiveLocalOnlyProof(text)) {
421
+ return { ok: false, reason: "missing_structured_local_only_evidence" };
422
+ }
423
+ if (!evidenceHasNoProductSemanticAmbiguityProof(text)) {
424
+ return { ok: false, reason: "missing_product_semantic_non_ambiguity_evidence" };
425
+ }
426
+ if (!evidenceDeclaresLocalPacketAuthorityScopeRemediation(text)) {
427
+ return { ok: false, reason: "missing_local_wave_remediation_evidence" };
428
+ }
429
+ const missingAuthorityRefs = extractConcreteMissingAuthorityRefs(text);
430
+ if (missingAuthorityRefs.length === 0) {
431
+ return { ok: false, reason: "missing_concrete_missing_authority_refs" };
432
+ }
433
+ return { ok: true, missingAuthorityRefs };
434
+ }
435
+
436
+ async function collectWaveResultRefs(projectRoot, loaded, waveId) {
437
+ const files = await readdir(loaded.topicDir, { withFileTypes: true });
438
+ return files
439
+ .filter((entry) => entry.isFile())
440
+ .map((entry) => entry.name)
441
+ .filter((name) => name.startsWith("result-") && name.includes(waveId))
442
+ .sort()
443
+ .map((name) => projectRef(projectRoot, path.join(loaded.topicDir, name)));
444
+ }
445
+
446
+ async function writeDeferredBlockerArtifact(
447
+ projectRoot,
448
+ loaded,
449
+ decision,
450
+ decisionRef,
451
+ nextWave,
452
+ resultRefs,
453
+ evidence,
454
+ recordedAt,
455
+ ) {
456
+ const waveId = decision.wave_id;
457
+ const reasonCode = decision.reason_code ?? "local_wave_blocker";
458
+ const blockerPath = path.join(
459
+ loaded.topicDir,
460
+ `deferred-blocker-${safeSegment(waveId)}-${safeSegment(reasonCode)}.md`,
461
+ );
462
+ const blocker = {
463
+ deferred_blocker_id: `deferred-${waveId}-${safeSegment(reasonCode)}`,
464
+ topic_id: loaded.topicId,
465
+ wave_id: waveId,
466
+ status: "active",
467
+ deferrable_scope: "local_wave",
468
+ reason_code: reasonCode,
469
+ stop_class: decision.stop_class,
470
+ recommended_action: decision.recommended_action,
471
+ decision_ref: decisionRef,
472
+ blocking_result_refs: resultRefs,
473
+ missing_authority_refs: evidence.missingAuthorityRefs,
474
+ next_wave_id: nextWave.wave_id,
475
+ required_manager_decision: decision.recommended_decision ?? "remediate selected wave before true-close",
476
+ remediation_summary:
477
+ decision.recommendation_rationale ??
478
+ "Local wave remediation is deferred so independent dependency-ready waves can continue.",
479
+ deferral_rationale:
480
+ "The blocker is scoped to this wave, no global contract/source-evidence/lowered-gate change is requested, and an independent dependency-ready wave exists.",
481
+ source_audit_findings_mutated: false,
482
+ source_sweep_design_artifacts_mutated: false,
483
+ product_semantic_ambiguity: false,
484
+ local_packet_authority_scope_remediation_only: true,
485
+ recorded_at: recordedAt,
486
+ };
487
+ await writeFile(
488
+ blockerPath,
489
+ `---\n${YAML.stringify(blocker).trimEnd()}\n---\n\n# Deferred Blocker ${waveId}\n`,
490
+ "utf8",
491
+ );
492
+ return projectRef(projectRoot, blockerPath);
493
+ }
494
+
495
+ export async function deferLocalWaveBlocker(projectRoot, loaded, decision, decisionRef, recordedAt) {
496
+ const deferrable = isDeferrableLocalWaveDecision(loaded.topic, decision);
497
+ if (!deferrable.ok) {
498
+ return deferrable;
499
+ }
500
+ const resultRefs = await collectWaveResultRefs(projectRoot, loaded, deferrable.wave.wave_id);
501
+ const evidence = await evaluateDeferrableLocalWaveEvidence(projectRoot, resultRefs);
502
+ if (!evidence.ok) {
503
+ return evidence;
504
+ }
505
+
506
+ const blockerRef = await writeDeferredBlockerArtifact(
507
+ projectRoot,
508
+ loaded,
509
+ decision,
510
+ decisionRef,
511
+ deferrable.nextWave,
512
+ evidence.resultRefs,
513
+ evidence,
514
+ recordedAt,
515
+ );
516
+ const waves = getTopicWaves(loaded.topic).map((wave) => ({
517
+ ...wave,
518
+ selected: wave.wave_id === deferrable.nextWave.wave_id,
519
+ }));
520
+ loaded.topic.waves = waves;
521
+ loaded.topic.selected_next_target = deferrable.nextWave.wave_id;
522
+ loaded.topic.last_transition_at = recordedAt.slice(0, 10);
523
+ loaded.topic.last_transition_reason = `deferred_local_blocker_${deferrable.wave.wave_id}`;
524
+ await writeFile(loaded.topicYamlPath, YAML.stringify(loaded.topic), "utf8");
525
+
526
+ return {
527
+ ok: true,
528
+ blockerRef,
529
+ wave: deferrable.wave,
530
+ nextWave: deferrable.nextWave,
531
+ };
532
+ }
@@ -0,0 +1,114 @@
1
+ import { readFile, stat } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import YAML from "yaml";
4
+
5
+ import { recordTopicRunEvent } from "./topic.mjs";
6
+
7
+ function toPortablePath(value) {
8
+ return value.split(path.sep).join("/");
9
+ }
10
+
11
+ function projectRef(projectRoot, absolutePath) {
12
+ return toPortablePath(path.relative(projectRoot, absolutePath));
13
+ }
14
+
15
+ function hasPlaceholder(value) {
16
+ return /<[^>]+>/.test(value);
17
+ }
18
+
19
+ function isConcreteTopicExpectedRef(ref) {
20
+ return typeof ref === "string"
21
+ && ref.length > 0
22
+ && !hasPlaceholder(ref)
23
+ && !path.isAbsolute(ref)
24
+ && !ref.includes("..");
25
+ }
26
+
27
+ async function refExists(projectRoot, ref) {
28
+ try {
29
+ return (await stat(path.join(projectRoot, ref))).isFile();
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ async function loadRunEventByRef(loaded, eventRef) {
36
+ if (!eventRef || path.isAbsolute(eventRef) || eventRef.includes("..")) {
37
+ return null;
38
+ }
39
+ try {
40
+ return YAML.parse(await readFile(path.join(loaded.topicDir, eventRef), "utf8"));
41
+ } catch {
42
+ return null;
43
+ }
44
+ }
45
+
46
+ async function loadDecisionByRef(projectRoot, decisionRef) {
47
+ if (!decisionRef || path.isAbsolute(decisionRef) || decisionRef.includes("..")) {
48
+ return null;
49
+ }
50
+ try {
51
+ return JSON.parse(await readFile(path.join(projectRoot, decisionRef), "utf8"));
52
+ } catch {
53
+ return null;
54
+ }
55
+ }
56
+
57
+ async function concreteExpectedArtifactRefs(projectRoot, loaded, expectedArtifacts) {
58
+ const refs = [];
59
+ for (const artifact of expectedArtifacts ?? []) {
60
+ if (!isConcreteTopicExpectedRef(artifact)) {
61
+ return null;
62
+ }
63
+ const absolutePath = path.join(loaded.topicDir, artifact);
64
+ const ref = projectRef(projectRoot, absolutePath);
65
+ if (!await refExists(projectRoot, ref)) {
66
+ return null;
67
+ }
68
+ refs.push(ref);
69
+ }
70
+ return refs.length > 0 ? refs : null;
71
+ }
72
+
73
+ function staleGateCanBeResolvedByEvidence(previousDecision, currentDecision) {
74
+ if (!previousDecision || !currentDecision) return false;
75
+ if (currentDecision.stop_class !== "continue") return false;
76
+ if ((currentDecision.blocking_checks ?? []).length > 0) return false;
77
+ if (previousDecision.stop_class !== "require_human_confirmation") return false;
78
+ if (previousDecision.recommended_action !== "record_result") return false;
79
+ return [
80
+ "implementation_admission_result_required",
81
+ "spec_update_review_required",
82
+ ].includes(previousDecision.reason_code);
83
+ }
84
+
85
+ export async function maybeResolveStaleHumanGate(projectRoot, options, loaded, ledgerReport, currentDecision, recordedAt) {
86
+ const gate = ledgerReport?.ledger?.current_human_gate;
87
+ if (!gate) {
88
+ return { ok: true, ledger: ledgerReport, resolved: false };
89
+ }
90
+ const gateEvent = await loadRunEventByRef(loaded, gate.event_ref);
91
+ const previousDecision = await loadDecisionByRef(projectRoot, gateEvent?.artifact_refs?.decision_ref);
92
+ if (!staleGateCanBeResolvedByEvidence(previousDecision, currentDecision)) {
93
+ return { ok: true, ledger: ledgerReport, resolved: false };
94
+ }
95
+ const expectedRefs = await concreteExpectedArtifactRefs(projectRoot, loaded, previousDecision.expected_artifacts);
96
+ if (!expectedRefs) {
97
+ return { ok: true, ledger: ledgerReport, resolved: false };
98
+ }
99
+ const resultRef = expectedRefs.find((ref) => path.basename(ref).startsWith("result-")) ?? expectedRefs[0];
100
+ const report = await recordTopicRunEvent(projectRoot, options.topicInput, {
101
+ runId: options.runId,
102
+ eventKind: "human_gate_resolved",
103
+ stopClass: "continue",
104
+ recommendedAction: previousDecision.recommended_action,
105
+ sourceRef: resultRef,
106
+ summary: `${previousDecision.reason_code}_resolved_by_existing_evidence`,
107
+ recordedAt,
108
+ waveId: previousDecision.wave_id,
109
+ artifactRefs: resultRef ? { result_ref: resultRef } : {},
110
+ });
111
+ return report.ok
112
+ ? { ok: true, ledger: report, resolved: true, resultRef }
113
+ : report;
114
+ }