@nimiplatform/nimi-coding 0.2.8 → 0.3.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 (205) hide show
  1. package/CHANGELOG.md +8 -92
  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/constants.mjs +9 -417
  8. package/cli/help.mjs +28 -145
  9. package/cli/index.mjs +5 -33
  10. package/cli/lib/blueprint-audit.mjs +5 -12
  11. package/cli/lib/bootstrap.mjs +0 -9
  12. package/cli/lib/contracts.mjs +0 -163
  13. package/cli/lib/doctor.mjs +155 -10
  14. package/cli/lib/entrypoints.mjs +63 -167
  15. package/cli/lib/internal/contracts-loaders.mjs +0 -85
  16. package/cli/lib/internal/contracts-parse.mjs +84 -520
  17. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +13 -8
  18. package/cli/lib/internal/surface-taxonomy-validators.mjs +316 -66
  19. package/cli/lib/internal/validators-shared.mjs +0 -16
  20. package/cli/lib/internal/validators-spec-helpers.mjs +7 -140
  21. package/cli/lib/internal/validators-spec.mjs +103 -342
  22. package/cli/lib/shared.mjs +1 -42
  23. package/cli/lib/validators.mjs +0 -48
  24. package/cli/seeds/seed-policy.yaml +0 -3
  25. package/config/bootstrap.yaml +4 -4
  26. package/config/spec-generation-inputs.yaml +6 -12
  27. package/contracts/domain-admission.schema.yaml +3 -3
  28. package/contracts/migration-inventory.schema.yaml +12 -75
  29. package/contracts/negative-fixtures.yaml +19 -99
  30. package/contracts/placement-contract.schema.yaml +49 -78
  31. package/contracts/projection-edge.schema.yaml +37 -121
  32. package/contracts/shared-enums.yaml +4 -8
  33. package/contracts/spec-generation-inputs.schema.yaml +15 -112
  34. package/contracts/spec-layout.schema.yaml +32 -0
  35. package/contracts/surface-taxonomy.schema.yaml +43 -94
  36. package/contracts/table-family.schema.yaml +1 -1
  37. package/contracts/tracked-output-admission.schema.yaml +13 -66
  38. package/methodology/core.yaml +33 -25
  39. package/methodology/four-closure-policy.yaml +18 -26
  40. package/methodology/role-separation-policy.yaml +21 -27
  41. package/methodology/spec-reconstruction.yaml +17 -34
  42. package/package.json +2 -4
  43. package/spec/_meta/spec-tree-model.yaml +5 -101
  44. package/spec/product-scope.yaml +25 -57
  45. package/adapters/README.md +0 -25
  46. package/adapters/claude/README.md +0 -89
  47. package/adapters/claude/profile.yaml +0 -70
  48. package/adapters/codex/README.md +0 -53
  49. package/adapters/codex/profile.yaml +0 -78
  50. package/adapters/oh-my-codex/README.md +0 -184
  51. package/adapters/oh-my-codex/profile.yaml +0 -46
  52. package/cli/commands/admit-high-risk-decision.mjs +0 -108
  53. package/cli/commands/audit-sweep.mjs +0 -364
  54. package/cli/commands/closeout.mjs +0 -186
  55. package/cli/commands/decide-high-risk-execution.mjs +0 -124
  56. package/cli/commands/handoff.mjs +0 -123
  57. package/cli/commands/ingest-high-risk-execution.mjs +0 -95
  58. package/cli/commands/review-high-risk-execution.mjs +0 -95
  59. package/cli/commands/sweep-design.mjs +0 -295
  60. package/cli/commands/sweep.mjs +0 -22
  61. package/cli/commands/topic-formatters.mjs +0 -382
  62. package/cli/commands/topic-goal.mjs +0 -33
  63. package/cli/commands/topic-options-shared.mjs +0 -27
  64. package/cli/commands/topic-options-workflow.mjs +0 -767
  65. package/cli/commands/topic-options.mjs +0 -626
  66. package/cli/commands/topic-runner.mjs +0 -169
  67. package/cli/commands/topic.mjs +0 -795
  68. package/cli/commands/validate-acceptance.mjs +0 -5
  69. package/cli/commands/validate-execution-packet.mjs +0 -5
  70. package/cli/commands/validate-orchestration-state.mjs +0 -5
  71. package/cli/commands/validate-prompt.mjs +0 -5
  72. package/cli/commands/validate-worker-output.mjs +0 -5
  73. package/cli/lib/adapter-profiles.mjs +0 -403
  74. package/cli/lib/audit-execution.mjs +0 -52
  75. package/cli/lib/audit-sweep-runtime/admissions.mjs +0 -508
  76. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +0 -356
  77. package/cli/lib/audit-sweep-runtime/chunks.mjs +0 -697
  78. package/cli/lib/audit-sweep-runtime/claude-auditor.mjs +0 -658
  79. package/cli/lib/audit-sweep-runtime/closeout.mjs +0 -144
  80. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +0 -654
  81. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +0 -527
  82. package/cli/lib/audit-sweep-runtime/common.mjs +0 -341
  83. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +0 -172
  84. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +0 -155
  85. package/cli/lib/audit-sweep-runtime/format.mjs +0 -57
  86. package/cli/lib/audit-sweep-runtime/ingest.mjs +0 -486
  87. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +0 -425
  88. package/cli/lib/audit-sweep-runtime/inventory.mjs +0 -798
  89. package/cli/lib/audit-sweep-runtime/ledger.mjs +0 -315
  90. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +0 -101
  91. package/cli/lib/audit-sweep-runtime/remediation.mjs +0 -349
  92. package/cli/lib/audit-sweep-runtime/rerun.mjs +0 -129
  93. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +0 -300
  94. package/cli/lib/audit-sweep-runtime/status.mjs +0 -62
  95. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +0 -215
  96. package/cli/lib/audit-sweep-runtime/validators.mjs +0 -797
  97. package/cli/lib/audit-sweep.mjs +0 -19
  98. package/cli/lib/authority-convergence.mjs +0 -720
  99. package/cli/lib/closeout.mjs +0 -732
  100. package/cli/lib/codex-sdk-runner.mjs +0 -76
  101. package/cli/lib/external-execution.mjs +0 -101
  102. package/cli/lib/handoff.mjs +0 -812
  103. package/cli/lib/high-risk-admission.mjs +0 -512
  104. package/cli/lib/high-risk-decision.mjs +0 -353
  105. package/cli/lib/high-risk-ingest.mjs +0 -321
  106. package/cli/lib/high-risk-review.mjs +0 -267
  107. package/cli/lib/internal/contracts-parse-high-risk.mjs +0 -131
  108. package/cli/lib/internal/contracts-validators.mjs +0 -399
  109. package/cli/lib/internal/doctor-bootstrap-surface.mjs +0 -406
  110. package/cli/lib/internal/doctor-delegated-surface.mjs +0 -256
  111. package/cli/lib/internal/doctor-finalize.mjs +0 -383
  112. package/cli/lib/internal/doctor-format.mjs +0 -286
  113. package/cli/lib/internal/doctor-inspectors.mjs +0 -327
  114. package/cli/lib/internal/doctor-state.mjs +0 -205
  115. package/cli/lib/internal/validators-artifacts.mjs +0 -515
  116. package/cli/lib/sweep-design-runtime/common.mjs +0 -246
  117. package/cli/lib/sweep-design-runtime/engine.mjs +0 -733
  118. package/cli/lib/sweep-design-runtime/fix-topic.mjs +0 -414
  119. package/cli/lib/sweep-design-runtime/lifecycle.mjs +0 -54
  120. package/cli/lib/sweep-design-runtime/results.mjs +0 -324
  121. package/cli/lib/sweep-design.mjs +0 -8
  122. package/cli/lib/topic-artifacts.mjs +0 -186
  123. package/cli/lib/topic-authority-coverage.mjs +0 -73
  124. package/cli/lib/topic-closeout.mjs +0 -560
  125. package/cli/lib/topic-common.mjs +0 -404
  126. package/cli/lib/topic-decisions.mjs +0 -332
  127. package/cli/lib/topic-draft-packets.mjs +0 -167
  128. package/cli/lib/topic-execution.mjs +0 -533
  129. package/cli/lib/topic-goal.mjs +0 -440
  130. package/cli/lib/topic-ledger.mjs +0 -281
  131. package/cli/lib/topic-lifecycle-artifacts.mjs +0 -173
  132. package/cli/lib/topic-root-validation.mjs +0 -288
  133. package/cli/lib/topic-runner-commands.mjs +0 -174
  134. package/cli/lib/topic-runner-deferral.mjs +0 -532
  135. package/cli/lib/topic-runner-stale-gates.mjs +0 -114
  136. package/cli/lib/topic-runner-validation.mjs +0 -138
  137. package/cli/lib/topic-runner.mjs +0 -727
  138. package/cli/lib/topic-scaffold.mjs +0 -252
  139. package/cli/lib/topic-waves.mjs +0 -403
  140. package/cli/lib/topic.mjs +0 -81
  141. package/config/audit-execution-artifacts.yaml +0 -20
  142. package/config/external-execution-artifacts.yaml +0 -16
  143. package/config/host-adapter.yaml +0 -30
  144. package/config/host-profile.yaml +0 -29
  145. package/config/installer-evidence.yaml +0 -31
  146. package/config/skill-installer.yaml +0 -23
  147. package/config/skill-manifest.yaml +0 -48
  148. package/config/skills.yaml +0 -30
  149. package/contracts/acceptance.schema.yaml +0 -16
  150. package/contracts/admission-checklist.schema.yaml +0 -15
  151. package/contracts/audit-chunk.schema.yaml +0 -123
  152. package/contracts/audit-closeout.schema.yaml +0 -51
  153. package/contracts/audit-finding.schema.yaml +0 -61
  154. package/contracts/audit-ledger.schema.yaml +0 -138
  155. package/contracts/audit-plan.schema.yaml +0 -138
  156. package/contracts/audit-remediation-map.schema.yaml +0 -52
  157. package/contracts/audit-rerun.schema.yaml +0 -31
  158. package/contracts/audit-sweep-result.yaml +0 -53
  159. package/contracts/authority-convergence-audit.schema.yaml +0 -19
  160. package/contracts/closeout.schema.yaml +0 -25
  161. package/contracts/decision-review.schema.yaml +0 -16
  162. package/contracts/doc-spec-audit-result.yaml +0 -19
  163. package/contracts/execution-packet.schema.yaml +0 -49
  164. package/contracts/external-host-compatibility.yaml +0 -22
  165. package/contracts/forbidden-shortcuts.catalog.yaml +0 -23
  166. package/contracts/high-risk-admission.schema.yaml +0 -23
  167. package/contracts/high-risk-execution-result.yaml +0 -20
  168. package/contracts/orchestration-state.schema.yaml +0 -41
  169. package/contracts/overflow-continuation.schema.yaml +0 -12
  170. package/contracts/packet.schema.yaml +0 -30
  171. package/contracts/pending-note.schema.yaml +0 -17
  172. package/contracts/prompt.schema.yaml +0 -12
  173. package/contracts/remediation.schema.yaml +0 -16
  174. package/contracts/result.schema.yaml +0 -24
  175. package/contracts/spec-reconstruction-result.yaml +0 -41
  176. package/contracts/sweep-design-result.yaml +0 -349
  177. package/contracts/topic-goal.schema.yaml +0 -87
  178. package/contracts/topic-run-ledger.schema.yaml +0 -72
  179. package/contracts/topic-step-decision.schema.yaml +0 -45
  180. package/contracts/topic.schema.yaml +0 -65
  181. package/contracts/true-close.schema.yaml +0 -15
  182. package/contracts/wave.schema.yaml +0 -29
  183. package/contracts/worker-output.schema.yaml +0 -15
  184. package/contracts/workflow-consumer.schema.yaml +0 -110
  185. package/methodology/audit-sweep-p0p1-recall.yaml +0 -45
  186. package/methodology/authority-convergence-policy.yaml +0 -42
  187. package/methodology/overflow-continuation-policy.yaml +0 -14
  188. package/methodology/skill-exchange-projection.yaml +0 -114
  189. package/methodology/skill-handoff.yaml +0 -34
  190. package/methodology/skill-installer-result.yaml +0 -27
  191. package/methodology/skill-installer-summary-projection.yaml +0 -181
  192. package/methodology/skill-runtime.yaml +0 -23
  193. package/methodology/spec-target-truth-profile.yaml +0 -46
  194. package/methodology/topic-lifecycle-report.yaml +0 -144
  195. package/methodology/topic-lifecycle.yaml +0 -37
  196. package/methodology/topic-naming-ontology.yaml +0 -21
  197. package/methodology/topic-ontology.yaml +0 -38
  198. package/methodology/topic-validation-policy.yaml +0 -9
  199. package/methodology/wave-dag-policy.yaml +0 -14
  200. package/spec/_meta/command-gating-matrix.yaml +0 -143
  201. package/spec/_meta/generate-drift-migration-checklist.yaml +0 -137
  202. package/spec/_meta/governance-routing-cutover-checklist.yaml +0 -35
  203. package/spec/_meta/phase2-impacted-surface-matrix.yaml +0 -44
  204. package/spec/_meta/spec-authority-cutover-readiness.yaml +0 -102
  205. package/spec/bootstrap-state.yaml +0 -99
@@ -1,532 +0,0 @@
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
- }
@@ -1,114 +0,0 @@
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
- }