@nimiplatform/nimi-coding 0.1.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 (186) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +348 -0
  3. package/adapters/README.md +25 -0
  4. package/adapters/claude/README.md +89 -0
  5. package/adapters/claude/profile.yaml +70 -0
  6. package/adapters/codex/README.md +53 -0
  7. package/adapters/codex/profile.yaml +78 -0
  8. package/adapters/oh-my-codex/README.md +185 -0
  9. package/adapters/oh-my-codex/profile.yaml +46 -0
  10. package/bin/nimicoding.mjs +6 -0
  11. package/cli/commands/admit-high-risk-decision.mjs +108 -0
  12. package/cli/commands/audit-sweep.mjs +341 -0
  13. package/cli/commands/blueprint-audit.mjs +91 -0
  14. package/cli/commands/clear.mjs +168 -0
  15. package/cli/commands/closeout.mjs +183 -0
  16. package/cli/commands/decide-high-risk-execution.mjs +124 -0
  17. package/cli/commands/doctor.mjs +53 -0
  18. package/cli/commands/generate-spec-derived-docs.mjs +131 -0
  19. package/cli/commands/handoff.mjs +123 -0
  20. package/cli/commands/ingest-high-risk-execution.mjs +95 -0
  21. package/cli/commands/review-high-risk-execution.mjs +95 -0
  22. package/cli/commands/start.mjs +717 -0
  23. package/cli/commands/topic-formatters.mjs +382 -0
  24. package/cli/commands/topic-goal.mjs +33 -0
  25. package/cli/commands/topic-options-shared.mjs +27 -0
  26. package/cli/commands/topic-options-workflow.mjs +767 -0
  27. package/cli/commands/topic-options.mjs +626 -0
  28. package/cli/commands/topic-runner.mjs +169 -0
  29. package/cli/commands/topic.mjs +795 -0
  30. package/cli/commands/validate-acceptance.mjs +5 -0
  31. package/cli/commands/validate-ai-governance.mjs +214 -0
  32. package/cli/commands/validate-execution-packet.mjs +5 -0
  33. package/cli/commands/validate-orchestration-state.mjs +5 -0
  34. package/cli/commands/validate-prompt.mjs +5 -0
  35. package/cli/commands/validate-spec-audit.mjs +27 -0
  36. package/cli/commands/validate-spec-governance.mjs +124 -0
  37. package/cli/commands/validate-spec-tree.mjs +27 -0
  38. package/cli/commands/validate-worker-output.mjs +5 -0
  39. package/cli/constants.mjs +489 -0
  40. package/cli/help.mjs +134 -0
  41. package/cli/index.mjs +103 -0
  42. package/cli/lib/adapter-profiles.mjs +403 -0
  43. package/cli/lib/audit-execution.mjs +52 -0
  44. package/cli/lib/audit-sweep-runtime/admissions.mjs +381 -0
  45. package/cli/lib/audit-sweep-runtime/audit-validity.mjs +333 -0
  46. package/cli/lib/audit-sweep-runtime/chunks.mjs +697 -0
  47. package/cli/lib/audit-sweep-runtime/closeout.mjs +144 -0
  48. package/cli/lib/audit-sweep-runtime/codex-auditor-evidence.mjs +639 -0
  49. package/cli/lib/audit-sweep-runtime/codex-auditor.mjs +515 -0
  50. package/cli/lib/audit-sweep-runtime/common.mjs +329 -0
  51. package/cli/lib/audit-sweep-runtime/coverage-quality.mjs +172 -0
  52. package/cli/lib/audit-sweep-runtime/evidence-assignment.mjs +152 -0
  53. package/cli/lib/audit-sweep-runtime/format.mjs +57 -0
  54. package/cli/lib/audit-sweep-runtime/ingest.mjs +486 -0
  55. package/cli/lib/audit-sweep-runtime/inventory-spec-chunks.mjs +198 -0
  56. package/cli/lib/audit-sweep-runtime/inventory.mjs +728 -0
  57. package/cli/lib/audit-sweep-runtime/ledger.mjs +315 -0
  58. package/cli/lib/audit-sweep-runtime/p0p1-profile.mjs +101 -0
  59. package/cli/lib/audit-sweep-runtime/remediation.mjs +349 -0
  60. package/cli/lib/audit-sweep-runtime/rerun.mjs +129 -0
  61. package/cli/lib/audit-sweep-runtime/risk-budget.mjs +300 -0
  62. package/cli/lib/audit-sweep-runtime/status.mjs +62 -0
  63. package/cli/lib/audit-sweep-runtime/validators-ledger.mjs +215 -0
  64. package/cli/lib/audit-sweep-runtime/validators.mjs +758 -0
  65. package/cli/lib/audit-sweep.mjs +18 -0
  66. package/cli/lib/authority-convergence.mjs +309 -0
  67. package/cli/lib/blueprint-audit.mjs +370 -0
  68. package/cli/lib/bootstrap.mjs +228 -0
  69. package/cli/lib/closeout.mjs +623 -0
  70. package/cli/lib/codex-sdk-runner.mjs +76 -0
  71. package/cli/lib/contracts.mjs +180 -0
  72. package/cli/lib/doctor.mjs +18 -0
  73. package/cli/lib/entrypoints.mjs +274 -0
  74. package/cli/lib/external-execution.mjs +101 -0
  75. package/cli/lib/fs-helpers.mjs +33 -0
  76. package/cli/lib/handoff.mjs +785 -0
  77. package/cli/lib/high-risk-admission.mjs +442 -0
  78. package/cli/lib/high-risk-decision.mjs +324 -0
  79. package/cli/lib/high-risk-ingest.mjs +317 -0
  80. package/cli/lib/high-risk-review.mjs +263 -0
  81. package/cli/lib/internal/contracts-loaders.mjs +132 -0
  82. package/cli/lib/internal/contracts-parse-high-risk.mjs +131 -0
  83. package/cli/lib/internal/contracts-parse.mjs +457 -0
  84. package/cli/lib/internal/contracts-validators.mjs +398 -0
  85. package/cli/lib/internal/doctor-bootstrap-surface.mjs +359 -0
  86. package/cli/lib/internal/doctor-delegated-surface.mjs +256 -0
  87. package/cli/lib/internal/doctor-finalize.mjs +385 -0
  88. package/cli/lib/internal/doctor-format.mjs +286 -0
  89. package/cli/lib/internal/doctor-inspectors.mjs +294 -0
  90. package/cli/lib/internal/doctor-state.mjs +205 -0
  91. package/cli/lib/internal/governance/ai/ai-context-budget-core.mjs +315 -0
  92. package/cli/lib/internal/governance/ai/ai-structure-budget-core.mjs +358 -0
  93. package/cli/lib/internal/governance/ai/check-agents-freshness.mjs +155 -0
  94. package/cli/lib/internal/governance/ai/check-high-risk-doc-metadata-core.mjs +173 -0
  95. package/cli/lib/internal/governance/config.mjs +150 -0
  96. package/cli/lib/internal/governance/runner.mjs +35 -0
  97. package/cli/lib/internal/governance/shared/read-yaml-with-fragments.mjs +49 -0
  98. package/cli/lib/internal/validators-artifacts.mjs +515 -0
  99. package/cli/lib/internal/validators-shared.mjs +28 -0
  100. package/cli/lib/internal/validators-spec-helpers.mjs +186 -0
  101. package/cli/lib/internal/validators-spec.mjs +410 -0
  102. package/cli/lib/shared.mjs +83 -0
  103. package/cli/lib/topic-draft-packets.mjs +48 -0
  104. package/cli/lib/topic-goal.mjs +361 -0
  105. package/cli/lib/topic-runner.mjs +772 -0
  106. package/cli/lib/topic.mjs +93 -0
  107. package/cli/lib/ui.mjs +178 -0
  108. package/cli/lib/validators.mjs +78 -0
  109. package/cli/lib/value-helpers.mjs +24 -0
  110. package/cli/lib/yaml-helpers.mjs +133 -0
  111. package/cli/nimicoding.mjs +1 -0
  112. package/cli/seeds/bootstrap.mjs +47 -0
  113. package/config/audit-execution-artifacts.yaml +20 -0
  114. package/config/bootstrap.yaml +6 -0
  115. package/config/external-execution-artifacts.yaml +16 -0
  116. package/config/host-adapter.yaml +30 -0
  117. package/config/host-profile.yaml +29 -0
  118. package/config/installer-evidence.yaml +31 -0
  119. package/config/skill-installer.yaml +23 -0
  120. package/config/skill-manifest.yaml +46 -0
  121. package/config/skills.yaml +30 -0
  122. package/config/spec-generation-inputs.yaml +25 -0
  123. package/contracts/acceptance.schema.yaml +16 -0
  124. package/contracts/admission-checklist.schema.yaml +15 -0
  125. package/contracts/audit-chunk.schema.yaml +110 -0
  126. package/contracts/audit-closeout.schema.yaml +51 -0
  127. package/contracts/audit-finding.schema.yaml +61 -0
  128. package/contracts/audit-ledger.schema.yaml +138 -0
  129. package/contracts/audit-plan.schema.yaml +123 -0
  130. package/contracts/audit-remediation-map.schema.yaml +51 -0
  131. package/contracts/audit-rerun.schema.yaml +31 -0
  132. package/contracts/audit-sweep-result.yaml +49 -0
  133. package/contracts/authority-convergence-audit.schema.yaml +19 -0
  134. package/contracts/closeout.schema.yaml +25 -0
  135. package/contracts/decision-review.schema.yaml +16 -0
  136. package/contracts/doc-spec-audit-result.yaml +19 -0
  137. package/contracts/execution-packet.schema.yaml +49 -0
  138. package/contracts/external-host-compatibility.yaml +22 -0
  139. package/contracts/forbidden-shortcuts.catalog.yaml +23 -0
  140. package/contracts/high-risk-admission.schema.yaml +23 -0
  141. package/contracts/high-risk-execution-result.yaml +20 -0
  142. package/contracts/orchestration-state.schema.yaml +41 -0
  143. package/contracts/overflow-continuation.schema.yaml +12 -0
  144. package/contracts/packet.schema.yaml +30 -0
  145. package/contracts/pending-note.schema.yaml +17 -0
  146. package/contracts/prompt.schema.yaml +12 -0
  147. package/contracts/remediation.schema.yaml +16 -0
  148. package/contracts/result.schema.yaml +24 -0
  149. package/contracts/spec-generation-audit.schema.yaml +31 -0
  150. package/contracts/spec-generation-inputs.schema.yaml +39 -0
  151. package/contracts/spec-reconstruction-result.yaml +37 -0
  152. package/contracts/topic-goal.schema.yaml +78 -0
  153. package/contracts/topic-run-ledger.schema.yaml +72 -0
  154. package/contracts/topic-step-decision.schema.yaml +45 -0
  155. package/contracts/topic.schema.yaml +65 -0
  156. package/contracts/true-close.schema.yaml +15 -0
  157. package/contracts/wave.schema.yaml +29 -0
  158. package/contracts/worker-output.schema.yaml +15 -0
  159. package/methodology/audit-sweep-p0p1-recall.yaml +45 -0
  160. package/methodology/authority-convergence-policy.yaml +42 -0
  161. package/methodology/core.yaml +25 -0
  162. package/methodology/four-closure-policy.yaml +28 -0
  163. package/methodology/overflow-continuation-policy.yaml +14 -0
  164. package/methodology/role-separation-policy.yaml +28 -0
  165. package/methodology/skill-exchange-projection.yaml +114 -0
  166. package/methodology/skill-handoff.yaml +34 -0
  167. package/methodology/skill-installer-result.yaml +27 -0
  168. package/methodology/skill-installer-summary-projection.yaml +181 -0
  169. package/methodology/skill-runtime.yaml +23 -0
  170. package/methodology/spec-reconstruction.yaml +63 -0
  171. package/methodology/spec-target-truth-profile.yaml +53 -0
  172. package/methodology/topic-lifecycle-report.yaml +144 -0
  173. package/methodology/topic-lifecycle.yaml +37 -0
  174. package/methodology/topic-naming-ontology.yaml +21 -0
  175. package/methodology/topic-ontology.yaml +38 -0
  176. package/methodology/topic-validation-policy.yaml +9 -0
  177. package/methodology/wave-dag-policy.yaml +14 -0
  178. package/package.json +50 -0
  179. package/spec/_meta/command-gating-matrix.yaml +110 -0
  180. package/spec/_meta/generate-drift-migration-checklist.yaml +155 -0
  181. package/spec/_meta/governance-routing-cutover-checklist.yaml +35 -0
  182. package/spec/_meta/phase2-impacted-surface-matrix.yaml +44 -0
  183. package/spec/_meta/spec-authority-cutover-readiness.yaml +104 -0
  184. package/spec/_meta/spec-tree-model.yaml +72 -0
  185. package/spec/bootstrap-state.yaml +99 -0
  186. package/spec/product-scope.yaml +56 -0
@@ -0,0 +1,623 @@
1
+ import { readdir, realpath } from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import {
5
+ CLOSEOUT_PAYLOAD_CONTRACT_VERSION,
6
+ SKILL_RESULT_CONTRACT_REFS,
7
+ } from "../constants.mjs";
8
+ import {
9
+ loadAuditSweepContract,
10
+ loadDocSpecAuditContract,
11
+ loadHighRiskExecutionContract,
12
+ loadSpecReconstructionContract,
13
+ validateAuditSweepSummary,
14
+ validateDocSpecAuditSummary,
15
+ validateHighRiskExecutionSummary,
16
+ validateSpecReconstructionSummary,
17
+ } from "./contracts.mjs";
18
+ import { inspectDoctorState } from "./doctor.mjs";
19
+ import {
20
+ loadExternalExecutionArtifactsConfig,
21
+ validateHighRiskExecutionArtifactRefs,
22
+ } from "./external-execution.mjs";
23
+ import { validateAuditSweepArtifacts } from "./audit-sweep-runtime/validators.mjs";
24
+ import { readTextIfFile } from "./fs-helpers.mjs";
25
+ import {
26
+ localize,
27
+ styleCommand,
28
+ styleHeading,
29
+ styleLabel,
30
+ styleStatus,
31
+ } from "./ui.mjs";
32
+ import { isIsoUtcTimestamp, isPlainObject } from "./value-helpers.mjs";
33
+ import { parseSkillSection } from "./yaml-helpers.mjs";
34
+
35
+ function translateCloseoutReason(reason) {
36
+ const translations = new Map([
37
+ ["spec_reconstruction result contract is missing or malformed", "spec_reconstruction 结果契约缺失或格式错误"],
38
+ ["doc_spec_audit result contract is missing or malformed", "doc_spec_audit 结果契约缺失或格式错误"],
39
+ ["audit_sweep result contract is missing or malformed", "audit_sweep 结果契约缺失或格式错误"],
40
+ ["high_risk_execution result contract is missing or malformed", "high_risk_execution 结果契约缺失或格式错误"],
41
+ ["Bootstrap or handoff validation is failing; repair doctor errors before projecting closeout results", "bootstrap 或 handoff 校验失败;请先修复 doctor 报错,再投影 closeout 结果"],
42
+ ["Non-completed outcomes may be projected as local-only closeout artifacts", "非 completed 的 outcome 可以仅投影为本地 closeout 产物"],
43
+ ["Completed closeout is not allowed in the current lifecycle state", "当前生命周期状态不允许完成该 closeout"],
44
+ ["Completed closeout requires declared canonical tree files to be valid", "完成 closeout 需要声明的 canonical tree 文件有效"],
45
+ ["Completed closeout requires a valid `.nimi/spec/_meta/spec-generation-audit.yaml` artifact", "完成 closeout 需要一个有效的 `.nimi/spec/_meta/spec-generation-audit.yaml` 产物"],
46
+ ["Completed doc_spec_audit closeout must compare against `.nimi/spec`", "完成 doc_spec_audit closeout 时必须对 `.nimi/spec` 进行比较"],
47
+ ["Completed high_risk_execution closeout requires canonical admissions truth to remain `.nimi/spec/high-risk-admissions.yaml`", "完成 high_risk_execution closeout 需要 canonical admissions truth 继续落在 `.nimi/spec/high-risk-admissions.yaml`"],
48
+ ["Completed closeout is consistent with the current canonical tree state", "completed closeout 与当前 canonical tree 状态一致"],
49
+ ]);
50
+
51
+ if (translations.has(reason)) {
52
+ return translations.get(reason);
53
+ }
54
+
55
+ const statusPrefix = " summary.status must be ";
56
+ if (reason.includes(statusPrefix)) {
57
+ const [skillId, suffix] = reason.split(statusPrefix);
58
+ const [expectedStatus, outcomePart] = suffix.split(" when outcome is ");
59
+ return `当 outcome 为 ${outcomePart} 时,${skillId} 的 summary.status 必须为 ${expectedStatus}`;
60
+ }
61
+
62
+ const noSummarySuffix = " does not accept summary when outcome is failed";
63
+ if (reason.endsWith(noSummarySuffix)) {
64
+ return `${reason.slice(0, -noSummarySuffix.length)} 在 outcome 为 failed 时不得携带 summary`;
65
+ }
66
+
67
+ const summaryImportPrefix = "summary import is not supported for skill ";
68
+ if (reason.startsWith(summaryImportPrefix)) {
69
+ return `当前不支持为该 skill 导入 summary:${reason.slice(summaryImportPrefix.length)}`;
70
+ }
71
+
72
+ return reason;
73
+ }
74
+
75
+ function inferAuditSweepIdFromSummary(summary) {
76
+ if (typeof summary?.ledger_ref !== "string") {
77
+ return null;
78
+ }
79
+ const match = summary.ledger_ref.match(/^\.nimi\/local\/audit\/ledgers\/([^/]+)\/ledger-[a-f0-9]{16}\.yaml$/);
80
+ return match ? match[1] : null;
81
+ }
82
+
83
+ async function validateCloseoutSummaryForSkill(projectRoot, skillId, summary, verifiedAt) {
84
+ if (summary === undefined) {
85
+ return { ok: true };
86
+ }
87
+
88
+ if (skillId === "spec_reconstruction") {
89
+ const contract = await loadSpecReconstructionContract(projectRoot);
90
+ if (!contract.ok) {
91
+ return {
92
+ ok: false,
93
+ reason: "spec_reconstruction result contract is missing or malformed",
94
+ };
95
+ }
96
+
97
+ return validateSpecReconstructionSummary(summary, contract, verifiedAt);
98
+ }
99
+
100
+ if (skillId === "doc_spec_audit") {
101
+ const contract = await loadDocSpecAuditContract(projectRoot);
102
+ if (!contract.ok) {
103
+ return {
104
+ ok: false,
105
+ reason: "doc_spec_audit result contract is missing or malformed",
106
+ };
107
+ }
108
+
109
+ return validateDocSpecAuditSummary(summary, contract, verifiedAt);
110
+ }
111
+
112
+ if (skillId === "audit_sweep") {
113
+ const contract = await loadAuditSweepContract(projectRoot);
114
+ if (!contract.ok) {
115
+ return {
116
+ ok: false,
117
+ reason: "audit_sweep result contract is missing or malformed",
118
+ };
119
+ }
120
+
121
+ const summaryValidation = validateAuditSweepSummary(summary, contract, verifiedAt);
122
+ if (!summaryValidation.ok) {
123
+ return summaryValidation;
124
+ }
125
+ const sweepId = inferAuditSweepIdFromSummary(summary);
126
+ if (!sweepId) {
127
+ return {
128
+ ok: false,
129
+ reason: "audit_sweep summary.ledger_ref must identify a local audit sweep ledger",
130
+ };
131
+ }
132
+ const artifactValidation = await validateAuditSweepArtifacts(projectRoot, { sweepId, scope: "closeout" });
133
+ if (!artifactValidation.ok) {
134
+ const failed = artifactValidation.checks.find((entry) => !entry.ok);
135
+ return {
136
+ ok: false,
137
+ reason: `audit_sweep artifact validation failed: ${failed?.reason ?? "unknown failure"}`,
138
+ };
139
+ }
140
+ return { ok: true };
141
+ }
142
+
143
+ if (skillId === "high_risk_execution") {
144
+ const contract = await loadHighRiskExecutionContract(projectRoot);
145
+ if (!contract.ok) {
146
+ return {
147
+ ok: false,
148
+ reason: "high_risk_execution result contract is missing or malformed",
149
+ };
150
+ }
151
+
152
+ const summaryValidation = validateHighRiskExecutionSummary(summary, contract, verifiedAt);
153
+ if (!summaryValidation.ok) {
154
+ return summaryValidation;
155
+ }
156
+
157
+ const externalExecutionArtifacts = await loadExternalExecutionArtifactsConfig(projectRoot);
158
+ return validateHighRiskExecutionArtifactRefs(summary, externalExecutionArtifacts);
159
+ }
160
+
161
+ return {
162
+ ok: false,
163
+ reason: `summary import is not supported for skill ${skillId}`,
164
+ };
165
+ }
166
+
167
+ function validateOutcomeStatusConsistency(skillId, outcome, summary) {
168
+ const expectedStatusBySkillAndOutcome = {
169
+ spec_reconstruction: {
170
+ completed: ["reconstructed", "partial"],
171
+ blocked: ["blocked"],
172
+ failed: [],
173
+ },
174
+ doc_spec_audit: {
175
+ completed: ["aligned", "drift_detected"],
176
+ blocked: ["blocked"],
177
+ failed: [],
178
+ },
179
+ audit_sweep: {
180
+ completed: ["candidate_ready", "partial"],
181
+ blocked: ["blocked"],
182
+ failed: [],
183
+ },
184
+ high_risk_execution: {
185
+ completed: ["candidate_ready"],
186
+ blocked: ["blocked"],
187
+ failed: ["failed"],
188
+ },
189
+ };
190
+
191
+ const expectedStatuses = expectedStatusBySkillAndOutcome[skillId]?.[outcome];
192
+ if (!expectedStatuses) {
193
+ return { ok: true };
194
+ }
195
+
196
+ if (outcome === "failed") {
197
+ if (summary !== undefined) {
198
+ return {
199
+ ok: false,
200
+ reason: `${skillId} does not accept summary when outcome is failed`,
201
+ };
202
+ }
203
+ return { ok: true };
204
+ }
205
+
206
+ if (summary === undefined) {
207
+ return { ok: true };
208
+ }
209
+
210
+ if (!expectedStatuses.includes(summary.status)) {
211
+ return {
212
+ ok: false,
213
+ reason: `${skillId} summary.status must be ${expectedStatuses.join("|")} when outcome is ${outcome}`,
214
+ };
215
+ }
216
+
217
+ return { ok: true };
218
+ }
219
+
220
+ async function collectSpecPaths(rootPath, relativePrefix) {
221
+ let entries;
222
+ try {
223
+ entries = await readdir(rootPath, { withFileTypes: true });
224
+ } catch {
225
+ return [];
226
+ }
227
+
228
+ const files = [];
229
+ for (const entry of entries) {
230
+ const absolutePath = path.join(rootPath, entry.name);
231
+ const relativePath = path.posix.join(relativePrefix, entry.name);
232
+ if (entry.isDirectory()) {
233
+ files.push(...await collectSpecPaths(absolutePath, relativePath));
234
+ } else if (entry.isFile()) {
235
+ files.push(relativePath);
236
+ }
237
+ }
238
+
239
+ return files.sort();
240
+ }
241
+
242
+ async function synthesizeSpecReconstructionSummary(projectRoot, doctorResult, verifiedAt) {
243
+ const generatedPaths = await collectSpecPaths(path.join(projectRoot, ".nimi", "spec"), ".nimi/spec");
244
+ const auditSummary = doctorResult.specGenerationAudit?.summary ?? {};
245
+ const unresolvedFileCount = Number.isInteger(auditSummary.unresolvedFiles) ? auditSummary.unresolvedFiles : 0;
246
+ const inferredFileCount = Number.isInteger(auditSummary.inferredFiles) ? auditSummary.inferredFiles : 0;
247
+ const placeholderFiles = Number.isInteger(auditSummary.placeholderFiles) ? auditSummary.placeholderFiles : 0;
248
+ const partialFiles = Number.isInteger(auditSummary.partialFiles) ? auditSummary.partialFiles : unresolvedFileCount;
249
+ const shouldBePartial = partialFiles > 0 || unresolvedFileCount > 0 || inferredFileCount > 0;
250
+
251
+ return {
252
+ generated_paths: generatedPaths,
253
+ audit_ref: ".nimi/spec/_meta/spec-generation-audit.yaml",
254
+ coverage_summary: {
255
+ complete_files: Math.max(generatedPaths.length - partialFiles - placeholderFiles, 0),
256
+ partial_files: partialFiles,
257
+ placeholder_files: placeholderFiles,
258
+ },
259
+ unresolved_file_count: unresolvedFileCount,
260
+ inferred_file_count: inferredFileCount,
261
+ status: doctorResult.specGenerationAudit?.ok && !shouldBePartial ? "reconstructed" : "partial",
262
+ summary: doctorResult.specGenerationAudit?.ok && !shouldBePartial
263
+ ? "Canonical spec generation completed with file-level audit coverage."
264
+ : "Canonical spec generation is valid, but file-level audit still records inferred or unresolved coverage.",
265
+ verified_at: verifiedAt,
266
+ };
267
+ }
268
+
269
+ function evaluateCloseoutReadiness(skillId, outcome, doctorResult, summary) {
270
+ if (outcome !== "completed") {
271
+ if (!doctorResult.ok || !doctorResult.handoffReadiness.ok) {
272
+ return {
273
+ ok: false,
274
+ reason: "Bootstrap or handoff validation is failing; repair doctor errors before projecting closeout results",
275
+ };
276
+ }
277
+ return {
278
+ ok: true,
279
+ reason: "Non-completed outcomes may be projected as local-only closeout artifacts",
280
+ };
281
+ }
282
+
283
+ const rule = (doctorResult.commandGating?.entries ?? []).find((entry) => entry.command === "closeout" && entry.skill === skillId) ?? null;
284
+ if (!rule?.completedRequires) {
285
+ return {
286
+ ok: false,
287
+ reason: "Completed closeout is not allowed in the current lifecycle state",
288
+ };
289
+ }
290
+
291
+ const treeState = doctorResult.lifecycleState?.treeState;
292
+ if (rule.completedRequires.tree_state && rule.completedRequires.tree_state !== treeState) {
293
+ return {
294
+ ok: false,
295
+ reason: "Completed closeout is not allowed in the current lifecycle state",
296
+ };
297
+ }
298
+
299
+ if (rule.completedRequires.canonical_required_files_valid === true && doctorResult.canonicalTree?.requiredFilesValid !== true) {
300
+ return {
301
+ ok: false,
302
+ reason: "Completed closeout requires declared canonical tree files to be valid",
303
+ };
304
+ }
305
+
306
+ if (rule.completedRequires.spec_generation_audit_valid === true && doctorResult.specGenerationAudit?.ok !== true) {
307
+ return {
308
+ ok: false,
309
+ reason: "Completed closeout requires a valid `.nimi/spec/_meta/spec-generation-audit.yaml` artifact",
310
+ };
311
+ }
312
+
313
+ if (rule.completedRequires.audit_references_canonical_root === true) {
314
+ const comparedPaths = Array.isArray(summary?.compared_paths) ? summary.compared_paths : [];
315
+ if (!comparedPaths.includes(".nimi/spec")) {
316
+ return {
317
+ ok: false,
318
+ reason: "Completed doc_spec_audit closeout must compare against `.nimi/spec`",
319
+ };
320
+ }
321
+ }
322
+
323
+ if (rule.completedRequires.high_risk_admissions_truth_ref) {
324
+ const admissionsTruthPath = rule.completedRequires.high_risk_admissions_truth_ref;
325
+ const expectedAdmissionsPath = doctorResult.commandGating.entries.find(
326
+ (entry) => entry.command === "admit-high-risk-decision" && entry.requires?.canonical_admissions_truth,
327
+ )?.requires?.canonical_admissions_truth ?? ".nimi/spec/high-risk-admissions.yaml";
328
+ if (admissionsTruthPath !== expectedAdmissionsPath) {
329
+ return {
330
+ ok: false,
331
+ reason: "Completed high_risk_execution closeout requires canonical admissions truth to remain `.nimi/spec/high-risk-admissions.yaml`",
332
+ };
333
+ }
334
+ }
335
+
336
+ if (!doctorResult.ok || !doctorResult.handoffReadiness.ok) {
337
+ return {
338
+ ok: false,
339
+ reason: "Bootstrap or handoff validation is failing; repair doctor errors before projecting closeout results",
340
+ };
341
+ }
342
+
343
+ return {
344
+ ok: true,
345
+ reason: "Completed closeout is consistent with the current canonical tree state",
346
+ };
347
+ }
348
+
349
+ export async function validateImportedCloseoutShape(raw, projectRoot) {
350
+ if (!isPlainObject(raw)) {
351
+ return {
352
+ ok: false,
353
+ error: `${localize(
354
+ "nimicoding closeout refused: imported closeout JSON must be an object.",
355
+ "nimicoding closeout 已拒绝:导入的 closeout JSON 必须是对象。",
356
+ )}\n`,
357
+ };
358
+ }
359
+
360
+ if (raw.projectRoot) {
361
+ let importedProjectRoot;
362
+ let currentProjectRoot;
363
+ try {
364
+ importedProjectRoot = await realpath(raw.projectRoot);
365
+ currentProjectRoot = await realpath(projectRoot);
366
+ } catch {
367
+ return {
368
+ ok: false,
369
+ error: `${localize(
370
+ "nimicoding closeout refused: imported closeout projectRoot could not be resolved.",
371
+ "nimicoding closeout 已拒绝:无法解析导入 closeout 的 projectRoot。",
372
+ )}\n`,
373
+ };
374
+ }
375
+
376
+ if (importedProjectRoot !== currentProjectRoot) {
377
+ return {
378
+ ok: false,
379
+ error: `${localize(
380
+ "nimicoding closeout refused: imported closeout projectRoot does not match the current project.",
381
+ "nimicoding closeout 已拒绝:导入 closeout 的 projectRoot 与当前项目不匹配。",
382
+ )}\n`,
383
+ };
384
+ }
385
+ }
386
+
387
+ const skillId = typeof raw.skill === "string"
388
+ ? raw.skill
389
+ : raw.skill && typeof raw.skill === "object" && typeof raw.skill.id === "string"
390
+ ? raw.skill.id
391
+ : null;
392
+
393
+ if (!skillId) {
394
+ return {
395
+ ok: false,
396
+ error: `${localize(
397
+ "nimicoding closeout refused: imported closeout JSON must declare `skill.id`.",
398
+ "nimicoding closeout 已拒绝:导入的 closeout JSON 必须声明 `skill.id`。",
399
+ )}\n`,
400
+ };
401
+ }
402
+
403
+ if (typeof raw.outcome !== "string" || !["completed", "blocked", "failed"].includes(raw.outcome)) {
404
+ return {
405
+ ok: false,
406
+ error: `${localize(
407
+ "nimicoding closeout refused: imported closeout JSON must declare a supported `outcome`.",
408
+ "nimicoding closeout 已拒绝:导入的 closeout JSON 必须声明受支持的 `outcome`。",
409
+ )}\n`,
410
+ };
411
+ }
412
+
413
+ if (!isIsoUtcTimestamp(raw.verifiedAt)) {
414
+ return {
415
+ ok: false,
416
+ error: `${localize(
417
+ "nimicoding closeout refused: imported `verifiedAt` must be an ISO-8601 UTC timestamp.",
418
+ "nimicoding closeout 已拒绝:导入的 `verifiedAt` 必须是 ISO-8601 UTC 时间戳。",
419
+ )}\n`,
420
+ };
421
+ }
422
+
423
+ if ("localOnly" in raw && raw.localOnly !== true) {
424
+ return {
425
+ ok: false,
426
+ error: `${localize(
427
+ "nimicoding closeout refused: imported closeout JSON cannot claim non-local semantic promotion.",
428
+ "nimicoding closeout 已拒绝:导入的 closeout JSON 不能声明非本地语义提升。",
429
+ )}\n`,
430
+ };
431
+ }
432
+
433
+ if ("summary" in raw && !isPlainObject(raw.summary)) {
434
+ return {
435
+ ok: false,
436
+ error: `${localize(
437
+ "nimicoding closeout refused: imported closeout JSON `summary` must be an object when present.",
438
+ "nimicoding closeout 已拒绝:导入的 closeout JSON 中 `summary` 如果存在,必须是对象。",
439
+ )}\n`,
440
+ };
441
+ }
442
+
443
+ return {
444
+ ok: true,
445
+ options: {
446
+ skill: skillId,
447
+ outcome: raw.outcome,
448
+ verifiedAt: raw.verifiedAt,
449
+ summary: raw.summary,
450
+ },
451
+ };
452
+ }
453
+
454
+ export async function loadImportedCloseoutOptions(projectRoot, fromPath) {
455
+ const absolutePath = path.resolve(projectRoot, fromPath);
456
+ const rawText = await readTextIfFile(absolutePath);
457
+
458
+ if (rawText === null) {
459
+ return {
460
+ ok: false,
461
+ error: `${localize(
462
+ `nimicoding closeout refused: cannot read imported closeout JSON at ${absolutePath}.`,
463
+ `nimicoding closeout 已拒绝:无法读取 ${absolutePath} 处的导入 closeout JSON。`,
464
+ )}\n`,
465
+ };
466
+ }
467
+
468
+ let parsed;
469
+ try {
470
+ parsed = JSON.parse(rawText);
471
+ } catch {
472
+ return {
473
+ ok: false,
474
+ error: `${localize(
475
+ `nimicoding closeout refused: imported closeout JSON at ${absolutePath} is invalid JSON.`,
476
+ `nimicoding closeout 已拒绝:${absolutePath} 处的导入 closeout JSON 不是合法 JSON。`,
477
+ )}\n`,
478
+ };
479
+ }
480
+
481
+ return validateImportedCloseoutShape(parsed, projectRoot);
482
+ }
483
+
484
+ export async function buildCloseoutPayload(projectRoot, options) {
485
+ const doctorResult = await inspectDoctorState(projectRoot);
486
+ const manifestText = await readTextIfFile(path.join(projectRoot, ".nimi", "config", "skill-manifest.yaml"));
487
+ const skillsConfigText = await readTextIfFile(path.join(projectRoot, ".nimi", "config", "skills.yaml"));
488
+ const manifestSkills = parseSkillSection(manifestText, "skills");
489
+ const expectedSkills = parseSkillSection(skillsConfigText, "expected_skill_surfaces");
490
+ const manifestSkill = manifestSkills.find((skill) => skill.id === options.skill) ?? null;
491
+ const expectedSkill = expectedSkills.find((skill) => skill.id === options.skill) ?? null;
492
+
493
+ if (!manifestSkill || !expectedSkill) {
494
+ return {
495
+ ok: false,
496
+ exitCode: 1,
497
+ error: localize(
498
+ `Unknown or undeclared skill id: ${options.skill}`,
499
+ `未知或未声明的 skill id:${options.skill}`,
500
+ ),
501
+ availableSkills: manifestSkills.map((skill) => skill.id),
502
+ doctor: doctorResult,
503
+ };
504
+ }
505
+
506
+ const resultContractRef = manifestSkill.result_contract_ref ?? SKILL_RESULT_CONTRACT_REFS[options.skill] ?? null;
507
+ let effectiveSummary = options.summary;
508
+ if (!effectiveSummary && options.skill === "spec_reconstruction" && options.outcome === "completed") {
509
+ effectiveSummary = await synthesizeSpecReconstructionSummary(projectRoot, doctorResult, options.verifiedAt);
510
+ }
511
+ const summaryValidation = await validateCloseoutSummaryForSkill(
512
+ projectRoot,
513
+ options.skill,
514
+ effectiveSummary,
515
+ options.verifiedAt,
516
+ );
517
+
518
+ if (!summaryValidation.ok) {
519
+ return {
520
+ ok: false,
521
+ exitCode: 2,
522
+ inputError: true,
523
+ error: `${localize(
524
+ `nimicoding closeout refused: ${summaryValidation.reason}.`,
525
+ `nimicoding closeout 已拒绝:${translateCloseoutReason(summaryValidation.reason)}。`,
526
+ )}\n`,
527
+ };
528
+ }
529
+
530
+ const statusConsistency = validateOutcomeStatusConsistency(
531
+ options.skill,
532
+ options.outcome,
533
+ effectiveSummary,
534
+ );
535
+ if (!statusConsistency.ok) {
536
+ return {
537
+ ok: false,
538
+ exitCode: 2,
539
+ inputError: true,
540
+ error: `${localize(
541
+ `nimicoding closeout refused: ${statusConsistency.reason}.`,
542
+ `nimicoding closeout 已拒绝:${translateCloseoutReason(statusConsistency.reason)}。`,
543
+ )}\n`,
544
+ };
545
+ }
546
+
547
+ const readiness = evaluateCloseoutReadiness(options.skill, options.outcome, doctorResult, effectiveSummary);
548
+ const localArtifactPath = path.join(projectRoot, ".nimi", "local", "handoff-results", `${options.skill}.json`);
549
+ const payload = {
550
+ contractVersion: CLOSEOUT_PAYLOAD_CONTRACT_VERSION,
551
+ ok: readiness.ok,
552
+ exitCode: readiness.ok ? 0 : 1,
553
+ projectRoot,
554
+ skill: {
555
+ id: options.skill,
556
+ required: expectedSkill.required === "true",
557
+ purpose: expectedSkill.purpose ?? null,
558
+ source: manifestSkill.source ?? "external",
559
+ resultContractRef,
560
+ },
561
+ outcome: options.outcome,
562
+ verifiedAt: options.verifiedAt,
563
+ localOnly: true,
564
+ artifactPath: localArtifactPath,
565
+ summary: effectiveSummary,
566
+ contracts: {
567
+ exchangeProjectionContractRef: ".nimi/methodology/skill-exchange-projection.yaml",
568
+ handoffRef: ".nimi/methodology/skill-handoff.yaml",
569
+ resultContractRef,
570
+ },
571
+ readiness,
572
+ doctor: {
573
+ ok: doctorResult.ok,
574
+ handoffReadiness: doctorResult.handoffReadiness,
575
+ delegatedContracts: doctorResult.delegatedContracts,
576
+ specGenerationAudit: doctorResult.specGenerationAudit,
577
+ auditArtifact: doctorResult.auditArtifact,
578
+ },
579
+ nextAction: readiness.ok
580
+ ? options.writeLocal
581
+ ? `Write the closeout artifact to ${localArtifactPath}.`
582
+ : "Review the projected closeout payload or write it locally with `--write-local`."
583
+ : readiness.reason,
584
+ };
585
+
586
+ return payload;
587
+ }
588
+
589
+ export function formatCloseoutPayload(payload) {
590
+ const nextAction = !payload.readiness.ok
591
+ ? translateCloseoutReason(payload.nextAction)
592
+ : payload.nextAction.startsWith("Write the closeout artifact to ")
593
+ ? localize(payload.nextAction, `将 closeout 产物写入 ${payload.artifactPath}。`)
594
+ : localize(
595
+ payload.nextAction,
596
+ `检查投影后的 closeout payload,或使用 ${styleCommand("--write-local")} 将其写入本地。`,
597
+ );
598
+ const lines = [
599
+ styleHeading(`nimicoding closeout: ${payload.projectRoot}`),
600
+ "",
601
+ styleLabel(localize("Skill:", "Skill:")),
602
+ ` - id: ${payload.skill.id}`,
603
+ ` - required: ${payload.skill.required ? "true" : "false"}`,
604
+ ` - source: ${payload.skill.source}`,
605
+ ` - purpose: ${payload.skill.purpose ?? localize("unknown", "未知")}`,
606
+ ` - result_contract_ref: ${payload.skill.resultContractRef ?? "none"}`,
607
+ "",
608
+ styleLabel(localize("Result:", "结果:")),
609
+ ` - outcome: ${payload.outcome}`,
610
+ ` - verified_at: ${payload.verifiedAt}`,
611
+ ` - ready: ${styleStatus(payload.readiness.ok ? "ready" : "needs_attention")}`,
612
+ ` - local_only: ${payload.localOnly ? "true" : "false"}`,
613
+ "",
614
+ styleLabel(localize("Next:", "下一步:")),
615
+ ` - ${nextAction}`,
616
+ ];
617
+
618
+ if (payload.summary?.status) {
619
+ lines.splice(lines.length - 3, 0, "", styleLabel(localize("Summary:", "摘要:")), ` - status: ${payload.summary.status}`);
620
+ }
621
+
622
+ return `${lines.join("\n")}\n`;
623
+ }
@@ -0,0 +1,76 @@
1
+ function requireNonEmptyString(value, field) {
2
+ if (typeof value !== "string" || value.trim().length === 0) {
3
+ return `${field} must be a non-empty string`;
4
+ }
5
+ return null;
6
+ }
7
+
8
+ function normalizeCodexResult(result) {
9
+ return {
10
+ raw: result,
11
+ finalResponse: typeof result?.final_response === "string"
12
+ ? result.final_response
13
+ : typeof result?.finalResponse === "string"
14
+ ? result.finalResponse
15
+ : typeof result === "string"
16
+ ? result
17
+ : null,
18
+ };
19
+ }
20
+
21
+ async function loadCodexSdk() {
22
+ try {
23
+ return await import("@openai/codex-sdk");
24
+ } catch (error) {
25
+ return {
26
+ loadError: error,
27
+ };
28
+ }
29
+ }
30
+
31
+ export async function runNativeCodexSdkPrompt(options) {
32
+ const promptError = requireNonEmptyString(options?.prompt, "prompt");
33
+ if (promptError) {
34
+ return {
35
+ ok: false,
36
+ error: `native Codex SDK dispatch refused: ${promptError}`,
37
+ };
38
+ }
39
+
40
+ if (options.threadId !== undefined && options.threadId !== null) {
41
+ const threadError = requireNonEmptyString(options.threadId, "threadId");
42
+ if (threadError) {
43
+ return {
44
+ ok: false,
45
+ error: `native Codex SDK dispatch refused: ${threadError}`,
46
+ };
47
+ }
48
+ }
49
+
50
+ let codex = options.codex ?? null;
51
+ if (!codex) {
52
+ const sdk = await loadCodexSdk();
53
+ if (!sdk.Codex) {
54
+ return {
55
+ ok: false,
56
+ error: "native Codex SDK dispatch refused: @openai/codex-sdk is not installed or does not export Codex",
57
+ };
58
+ }
59
+ codex = new sdk.Codex();
60
+ }
61
+ const thread = options.threadId
62
+ ? codex.resumeThread(options.threadId)
63
+ : codex.startThread();
64
+ const result = await thread.run(options.prompt);
65
+ const normalized = normalizeCodexResult(result);
66
+
67
+ return {
68
+ ok: true,
69
+ adapterId: "codex",
70
+ sdkPackage: "@openai/codex-sdk",
71
+ mode: options.threadId ? "resume_thread" : "start_thread",
72
+ threadId: options.threadId ?? thread.id ?? result?.thread_id ?? null,
73
+ finalResponse: normalized.finalResponse,
74
+ result: normalized.raw,
75
+ };
76
+ }