@tiangong-lca/foundry 0.1.2

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 (323) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +532 -0
  3. package/docs/architecture.md +450 -0
  4. package/docs/foundry-task-contracts.md +147 -0
  5. package/docs/import-profiles/bafu/constraints.md +343 -0
  6. package/docs/import-profiles/bafu/leaf-process-classification-authoring.md +153 -0
  7. package/docs/import-profiles/bafu/profile.md +183 -0
  8. package/docs/import-profiles/uslci/constraints.md +29 -0
  9. package/docs/import-profiles/uslci/profile.md +43 -0
  10. package/docs/import-profiles/worldsteel/constraints.md +77 -0
  11. package/docs/import-profiles/worldsteel/import-coverage.md +93 -0
  12. package/docs/import-profiles/worldsteel/import-plan.md +375 -0
  13. package/docs/import-profiles/worldsteel/profile.md +78 -0
  14. package/docs/package-distribution-contract.md +287 -0
  15. package/docs/public-runtime-contract.md +218 -0
  16. package/docs/runtime-context-contract.md +138 -0
  17. package/docs/safety-policy.md +164 -0
  18. package/docs/task-authorization-contract.md +131 -0
  19. package/docs/uslci-import-runbook.md +336 -0
  20. package/docs/workspace-migration-contract.md +127 -0
  21. package/package-dist/assets/foundry-package-descriptor.json +1661 -0
  22. package/package-dist/scripts/foundry-facade.d.ts +78 -0
  23. package/package-dist/scripts/foundry-facade.js +1317 -0
  24. package/package-dist/scripts/foundry-runtime.d.ts +180 -0
  25. package/package-dist/scripts/foundry-runtime.js +190 -0
  26. package/package-dist/scripts/lib/batch-orchestration/scope-attempt-ledger.d.ts +27 -0
  27. package/package-dist/scripts/lib/batch-orchestration/scope-attempt-ledger.js +85 -0
  28. package/package-dist/scripts/lib/batch-orchestration/scope-resume-contract.d.ts +38 -0
  29. package/package-dist/scripts/lib/batch-orchestration/scope-resume-contract.js +90 -0
  30. package/package-dist/scripts/lib/bundle-row-types.d.ts +44 -0
  31. package/package-dist/scripts/lib/bundle-row-types.js +46 -0
  32. package/package-dist/scripts/lib/bundle-sample-utils.d.ts +183 -0
  33. package/package-dist/scripts/lib/bundle-sample-utils.js +526 -0
  34. package/package-dist/scripts/lib/bundle-source-context.d.ts +31 -0
  35. package/package-dist/scripts/lib/bundle-source-context.js +142 -0
  36. package/package-dist/scripts/lib/canonical-description.d.ts +12 -0
  37. package/package-dist/scripts/lib/canonical-description.js +72 -0
  38. package/package-dist/scripts/lib/canonical-support-rewrites.d.ts +127 -0
  39. package/package-dist/scripts/lib/canonical-support-rewrites.js +527 -0
  40. package/package-dist/scripts/lib/decision-owners/classification.d.ts +245 -0
  41. package/package-dist/scripts/lib/decision-owners/classification.js +754 -0
  42. package/package-dist/scripts/lib/decision-owners/identity-preflight.d.ts +333 -0
  43. package/package-dist/scripts/lib/decision-owners/identity-preflight.js +1353 -0
  44. package/package-dist/scripts/lib/decision-owners/identity-task.d.ts +130 -0
  45. package/package-dist/scripts/lib/decision-owners/identity-task.js +494 -0
  46. package/package-dist/scripts/lib/decision-owners/identity.d.ts +74 -0
  47. package/package-dist/scripts/lib/decision-owners/identity.js +443 -0
  48. package/package-dist/scripts/lib/decision-owners/location.d.ts +239 -0
  49. package/package-dist/scripts/lib/decision-owners/location.js +704 -0
  50. package/package-dist/scripts/lib/decision-task-utils.d.ts +484 -0
  51. package/package-dist/scripts/lib/decision-task-utils.js +735 -0
  52. package/package-dist/scripts/lib/finalize-owners/cli-wrappers.d.ts +52 -0
  53. package/package-dist/scripts/lib/finalize-owners/cli-wrappers.js +146 -0
  54. package/package-dist/scripts/lib/finalize-owners/commit-handoff.d.ts +79 -0
  55. package/package-dist/scripts/lib/finalize-owners/commit-handoff.js +515 -0
  56. package/package-dist/scripts/lib/finalize-owners/post-authoring.d.ts +234 -0
  57. package/package-dist/scripts/lib/finalize-owners/post-authoring.js +1720 -0
  58. package/package-dist/scripts/lib/finalize-owners/post-write-closeout.d.ts +140 -0
  59. package/package-dist/scripts/lib/finalize-owners/post-write-closeout.js +656 -0
  60. package/package-dist/scripts/lib/foundry-args.d.ts +7 -0
  61. package/package-dist/scripts/lib/foundry-args.js +38 -0
  62. package/package-dist/scripts/lib/foundry-authentication-environment.d.ts +17 -0
  63. package/package-dist/scripts/lib/foundry-authentication-environment.js +55 -0
  64. package/package-dist/scripts/lib/foundry-authorization-input.d.ts +31 -0
  65. package/package-dist/scripts/lib/foundry-authorization-input.js +151 -0
  66. package/package-dist/scripts/lib/foundry-command-metadata.d.ts +32 -0
  67. package/package-dist/scripts/lib/foundry-command-metadata.js +1012 -0
  68. package/package-dist/scripts/lib/foundry-command-registry.d.ts +34 -0
  69. package/package-dist/scripts/lib/foundry-command-registry.js +241 -0
  70. package/package-dist/scripts/lib/foundry-command-spec.d.ts +1 -0
  71. package/package-dist/scripts/lib/foundry-command-spec.js +1 -0
  72. package/package-dist/scripts/lib/foundry-decision-owners.d.ts +17 -0
  73. package/package-dist/scripts/lib/foundry-decision-owners.js +68 -0
  74. package/package-dist/scripts/lib/foundry-decision-routing.d.ts +3 -0
  75. package/package-dist/scripts/lib/foundry-decision-routing.js +38 -0
  76. package/package-dist/scripts/lib/foundry-execution-admission.d.ts +33 -0
  77. package/package-dist/scripts/lib/foundry-execution-admission.js +225 -0
  78. package/package-dist/scripts/lib/foundry-execution-attempt.d.ts +11 -0
  79. package/package-dist/scripts/lib/foundry-execution-attempt.js +33 -0
  80. package/package-dist/scripts/lib/foundry-execution-context-format.d.ts +18 -0
  81. package/package-dist/scripts/lib/foundry-execution-context-format.js +83 -0
  82. package/package-dist/scripts/lib/foundry-facade-store.d.ts +25 -0
  83. package/package-dist/scripts/lib/foundry-facade-store.js +449 -0
  84. package/package-dist/scripts/lib/foundry-finalize-owners.d.ts +21 -0
  85. package/package-dist/scripts/lib/foundry-finalize-owners.js +203 -0
  86. package/package-dist/scripts/lib/foundry-identity-owners.d.ts +13 -0
  87. package/package-dist/scripts/lib/foundry-identity-owners.js +58 -0
  88. package/package-dist/scripts/lib/foundry-managed-host.d.ts +7 -0
  89. package/package-dist/scripts/lib/foundry-managed-host.js +173 -0
  90. package/package-dist/scripts/lib/foundry-migration-adoption-plan.d.ts +39 -0
  91. package/package-dist/scripts/lib/foundry-migration-adoption-plan.js +467 -0
  92. package/package-dist/scripts/lib/foundry-migration-adoption.d.ts +31 -0
  93. package/package-dist/scripts/lib/foundry-migration-adoption.js +187 -0
  94. package/package-dist/scripts/lib/foundry-migration-authority.d.ts +42 -0
  95. package/package-dist/scripts/lib/foundry-migration-authority.js +280 -0
  96. package/package-dist/scripts/lib/foundry-migration-inventory.d.ts +31 -0
  97. package/package-dist/scripts/lib/foundry-migration-inventory.js +148 -0
  98. package/package-dist/scripts/lib/foundry-migration-plan.d.ts +48 -0
  99. package/package-dist/scripts/lib/foundry-migration-plan.js +234 -0
  100. package/package-dist/scripts/lib/foundry-migration-replay.d.ts +3 -0
  101. package/package-dist/scripts/lib/foundry-migration-replay.js +134 -0
  102. package/package-dist/scripts/lib/foundry-migration-stage.d.ts +15 -0
  103. package/package-dist/scripts/lib/foundry-migration-stage.js +152 -0
  104. package/package-dist/scripts/lib/foundry-migration-transfer-io.d.ts +13 -0
  105. package/package-dist/scripts/lib/foundry-migration-transfer-io.js +226 -0
  106. package/package-dist/scripts/lib/foundry-migration-transfer.d.ts +36 -0
  107. package/package-dist/scripts/lib/foundry-migration-transfer.js +350 -0
  108. package/package-dist/scripts/lib/foundry-operation-result.d.ts +71 -0
  109. package/package-dist/scripts/lib/foundry-operation-result.js +264 -0
  110. package/package-dist/scripts/lib/foundry-owner-execution-store.d.ts +319 -0
  111. package/package-dist/scripts/lib/foundry-owner-execution-store.js +293 -0
  112. package/package-dist/scripts/lib/foundry-owner-readback.d.ts +22 -0
  113. package/package-dist/scripts/lib/foundry-owner-readback.js +189 -0
  114. package/package-dist/scripts/lib/foundry-owner-trace-acceptance.d.ts +15 -0
  115. package/package-dist/scripts/lib/foundry-owner-trace-acceptance.js +68 -0
  116. package/package-dist/scripts/lib/foundry-package-contract.d.ts +56 -0
  117. package/package-dist/scripts/lib/foundry-package-contract.js +392 -0
  118. package/package-dist/scripts/lib/foundry-private-path.d.ts +2 -0
  119. package/package-dist/scripts/lib/foundry-private-path.js +20 -0
  120. package/package-dist/scripts/lib/foundry-runtime-cache.d.ts +3 -0
  121. package/package-dist/scripts/lib/foundry-runtime-cache.js +25 -0
  122. package/package-dist/scripts/lib/foundry-runtime-command-policy.d.ts +20 -0
  123. package/package-dist/scripts/lib/foundry-runtime-command-policy.js +173 -0
  124. package/package-dist/scripts/lib/foundry-runtime-context-types.d.ts +59 -0
  125. package/package-dist/scripts/lib/foundry-runtime-context-types.js +1 -0
  126. package/package-dist/scripts/lib/foundry-runtime-context.d.ts +27 -0
  127. package/package-dist/scripts/lib/foundry-runtime-context.js +668 -0
  128. package/package-dist/scripts/lib/foundry-runtime-environment.d.ts +9 -0
  129. package/package-dist/scripts/lib/foundry-runtime-environment.js +90 -0
  130. package/package-dist/scripts/lib/foundry-runtime-error.d.ts +4 -0
  131. package/package-dist/scripts/lib/foundry-runtime-error.js +8 -0
  132. package/package-dist/scripts/lib/foundry-runtime-identity.d.ts +18 -0
  133. package/package-dist/scripts/lib/foundry-runtime-identity.js +127 -0
  134. package/package-dist/scripts/lib/foundry-runtime-paths.d.ts +16 -0
  135. package/package-dist/scripts/lib/foundry-runtime-paths.js +159 -0
  136. package/package-dist/scripts/lib/foundry-runtime-qualification.d.ts +66 -0
  137. package/package-dist/scripts/lib/foundry-runtime-qualification.js +223 -0
  138. package/package-dist/scripts/lib/foundry-runtime-selection-record.d.ts +16 -0
  139. package/package-dist/scripts/lib/foundry-runtime-selection-record.js +32 -0
  140. package/package-dist/scripts/lib/foundry-runtime-selection.d.ts +14 -0
  141. package/package-dist/scripts/lib/foundry-runtime-selection.js +123 -0
  142. package/package-dist/scripts/lib/foundry-runtime-utils.d.ts +178 -0
  143. package/package-dist/scripts/lib/foundry-runtime-utils.js +603 -0
  144. package/package-dist/scripts/lib/foundry-semantic-input.d.ts +30 -0
  145. package/package-dist/scripts/lib/foundry-semantic-input.js +132 -0
  146. package/package-dist/scripts/lib/foundry-task-authorization.d.ts +68 -0
  147. package/package-dist/scripts/lib/foundry-task-authorization.js +350 -0
  148. package/package-dist/scripts/lib/foundry-task-io.d.ts +18 -0
  149. package/package-dist/scripts/lib/foundry-task-io.js +92 -0
  150. package/package-dist/scripts/lib/foundry-task-registration.d.ts +6 -0
  151. package/package-dist/scripts/lib/foundry-task-registration.js +384 -0
  152. package/package-dist/scripts/lib/foundry-task-start-spec.d.ts +34 -0
  153. package/package-dist/scripts/lib/foundry-task-start-spec.js +161 -0
  154. package/package-dist/scripts/lib/foundry-task-store.d.ts +18 -0
  155. package/package-dist/scripts/lib/foundry-task-store.js +466 -0
  156. package/package-dist/scripts/lib/foundry-task-types.d.ts +99 -0
  157. package/package-dist/scripts/lib/foundry-task-types.js +1 -0
  158. package/package-dist/scripts/lib/foundry-workflow-approval-continuation.d.ts +6 -0
  159. package/package-dist/scripts/lib/foundry-workflow-approval-continuation.js +136 -0
  160. package/package-dist/scripts/lib/foundry-workflow-assessment.d.ts +3 -0
  161. package/package-dist/scripts/lib/foundry-workflow-assessment.js +231 -0
  162. package/package-dist/scripts/lib/foundry-workflow-authorization.d.ts +22 -0
  163. package/package-dist/scripts/lib/foundry-workflow-authorization.js +174 -0
  164. package/package-dist/scripts/lib/foundry-workflow-context.d.ts +3 -0
  165. package/package-dist/scripts/lib/foundry-workflow-context.js +99 -0
  166. package/package-dist/scripts/lib/foundry-workflow-decisions.d.ts +18 -0
  167. package/package-dist/scripts/lib/foundry-workflow-decisions.js +123 -0
  168. package/package-dist/scripts/lib/foundry-workflow-execution.d.ts +8 -0
  169. package/package-dist/scripts/lib/foundry-workflow-execution.js +248 -0
  170. package/package-dist/scripts/lib/foundry-workflow-finalize.d.ts +13 -0
  171. package/package-dist/scripts/lib/foundry-workflow-finalize.js +264 -0
  172. package/package-dist/scripts/lib/foundry-workflow-identity-apply.d.ts +14 -0
  173. package/package-dist/scripts/lib/foundry-workflow-identity-apply.js +159 -0
  174. package/package-dist/scripts/lib/foundry-workflow-identity.d.ts +6 -0
  175. package/package-dist/scripts/lib/foundry-workflow-identity.js +153 -0
  176. package/package-dist/scripts/lib/foundry-workflow-import.d.ts +4 -0
  177. package/package-dist/scripts/lib/foundry-workflow-import.js +70 -0
  178. package/package-dist/scripts/lib/foundry-workflow-io.d.ts +11 -0
  179. package/package-dist/scripts/lib/foundry-workflow-io.js +77 -0
  180. package/package-dist/scripts/lib/foundry-workflow-reference-verify.d.ts +41 -0
  181. package/package-dist/scripts/lib/foundry-workflow-reference-verify.js +266 -0
  182. package/package-dist/scripts/lib/foundry-workflow-rows.d.ts +2 -0
  183. package/package-dist/scripts/lib/foundry-workflow-rows.js +47 -0
  184. package/package-dist/scripts/lib/foundry-workflow-semantic.d.ts +5 -0
  185. package/package-dist/scripts/lib/foundry-workflow-semantic.js +367 -0
  186. package/package-dist/scripts/lib/foundry-workflow-state.d.ts +37 -0
  187. package/package-dist/scripts/lib/foundry-workflow-state.js +158 -0
  188. package/package-dist/scripts/lib/full-context-proof.d.ts +51 -0
  189. package/package-dist/scripts/lib/full-context-proof.js +932 -0
  190. package/package-dist/scripts/lib/identity-preflight-artifacts.d.ts +184 -0
  191. package/package-dist/scripts/lib/identity-preflight-artifacts.js +573 -0
  192. package/package-dist/scripts/lib/identity-preflight-proof.d.ts +117 -0
  193. package/package-dist/scripts/lib/identity-preflight-proof.js +341 -0
  194. package/package-dist/scripts/lib/identity-reference-rewrite-utils.d.ts +147 -0
  195. package/package-dist/scripts/lib/identity-reference-rewrite-utils.js +817 -0
  196. package/package-dist/scripts/lib/import-curation/authoring-packages.d.ts +28 -0
  197. package/package-dist/scripts/lib/import-curation/authoring-packages.js +92 -0
  198. package/package-dist/scripts/lib/import-curation/curation-cleanup.d.ts +32 -0
  199. package/package-dist/scripts/lib/import-curation/curation-cleanup.js +203 -0
  200. package/package-dist/scripts/lib/import-curation/curation-gate.d.ts +47 -0
  201. package/package-dist/scripts/lib/import-curation/curation-gate.js +528 -0
  202. package/package-dist/scripts/lib/import-curation/internal/artifact-inputs.d.ts +24 -0
  203. package/package-dist/scripts/lib/import-curation/internal/artifact-inputs.js +152 -0
  204. package/package-dist/scripts/lib/import-curation/internal/authoring-patch-workflow.d.ts +3 -0
  205. package/package-dist/scripts/lib/import-curation/internal/authoring-patch-workflow.js +3 -0
  206. package/package-dist/scripts/lib/import-curation/internal/authoring-task-workflow.d.ts +1 -0
  207. package/package-dist/scripts/lib/import-curation/internal/authoring-task-workflow.js +1 -0
  208. package/package-dist/scripts/lib/import-curation/internal/context-inputs.d.ts +56 -0
  209. package/package-dist/scripts/lib/import-curation/internal/context-inputs.js +206 -0
  210. package/package-dist/scripts/lib/import-curation/internal/curation-gate-workflow.d.ts +14 -0
  211. package/package-dist/scripts/lib/import-curation/internal/curation-gate-workflow.js +14 -0
  212. package/package-dist/scripts/lib/import-curation/internal/dataset-payload.d.ts +70 -0
  213. package/package-dist/scripts/lib/import-curation/internal/dataset-payload.js +141 -0
  214. package/package-dist/scripts/lib/import-curation/internal/dataset-types.d.ts +25 -0
  215. package/package-dist/scripts/lib/import-curation/internal/dataset-types.js +45 -0
  216. package/package-dist/scripts/lib/import-curation/internal/full-context-proof.d.ts +129 -0
  217. package/package-dist/scripts/lib/import-curation/internal/full-context-proof.js +552 -0
  218. package/package-dist/scripts/lib/import-curation/internal/hash-utils.d.ts +2 -0
  219. package/package-dist/scripts/lib/import-curation/internal/hash-utils.js +10 -0
  220. package/package-dist/scripts/lib/import-curation/internal/mutation-manifest-workflow.d.ts +15 -0
  221. package/package-dist/scripts/lib/import-curation/internal/mutation-manifest-workflow.js +15 -0
  222. package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.d.ts +31 -0
  223. package/package-dist/scripts/lib/import-curation/internal/prewrite-cleanup.js +467 -0
  224. package/package-dist/scripts/lib/import-curation/internal/profiles-config.d.ts +47 -0
  225. package/package-dist/scripts/lib/import-curation/internal/profiles-config.js +88 -0
  226. package/package-dist/scripts/lib/import-curation/internal/runtime-io.d.ts +23 -0
  227. package/package-dist/scripts/lib/import-curation/internal/runtime-io.js +132 -0
  228. package/package-dist/scripts/lib/import-curation/internal/trace-summary.d.ts +53 -0
  229. package/package-dist/scripts/lib/import-curation/internal/trace-summary.js +114 -0
  230. package/package-dist/scripts/lib/import-curation/internal/workflow-authoring-tasks.d.ts +141 -0
  231. package/package-dist/scripts/lib/import-curation/internal/workflow-authoring-tasks.js +854 -0
  232. package/package-dist/scripts/lib/import-curation/internal/workflow-decision-apply-context.d.ts +20 -0
  233. package/package-dist/scripts/lib/import-curation/internal/workflow-decision-apply-context.js +46 -0
  234. package/package-dist/scripts/lib/import-curation/internal/workflow-decision-full-context.d.ts +110 -0
  235. package/package-dist/scripts/lib/import-curation/internal/workflow-decision-full-context.js +782 -0
  236. package/package-dist/scripts/lib/import-curation/internal/workflow-dry-run-context.d.ts +24 -0
  237. package/package-dist/scripts/lib/import-curation/internal/workflow-dry-run-context.js +191 -0
  238. package/package-dist/scripts/lib/import-curation/internal/workflow-evidence-scope.d.ts +80 -0
  239. package/package-dist/scripts/lib/import-curation/internal/workflow-evidence-scope.js +359 -0
  240. package/package-dist/scripts/lib/import-curation/internal/workflow-identity-decision-context.d.ts +146 -0
  241. package/package-dist/scripts/lib/import-curation/internal/workflow-identity-decision-context.js +381 -0
  242. package/package-dist/scripts/lib/import-curation/internal/workflow-identity-preflight.d.ts +223 -0
  243. package/package-dist/scripts/lib/import-curation/internal/workflow-identity-preflight.js +1079 -0
  244. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-collect.d.ts +41 -0
  245. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-collect.js +412 -0
  246. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-evidence-context.d.ts +117 -0
  247. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-evidence-context.js +278 -0
  248. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-evidence.d.ts +59 -0
  249. package/package-dist/scripts/lib/import-curation/internal/workflow-patch-evidence.js +593 -0
  250. package/package-dist/scripts/lib/import-curation/internal/workflow-queue-context.d.ts +58 -0
  251. package/package-dist/scripts/lib/import-curation/internal/workflow-queue-context.js +263 -0
  252. package/package-dist/scripts/lib/import-curation/internal/workflow-reference-closure.d.ts +177 -0
  253. package/package-dist/scripts/lib/import-curation/internal/workflow-reference-closure.js +812 -0
  254. package/package-dist/scripts/lib/import-curation/internal/workflow-row-transform-context.d.ts +159 -0
  255. package/package-dist/scripts/lib/import-curation/internal/workflow-row-transform-context.js +466 -0
  256. package/package-dist/scripts/lib/import-curation/internal/workflow-semantic-actions.d.ts +152 -0
  257. package/package-dist/scripts/lib/import-curation/internal/workflow-semantic-actions.js +1377 -0
  258. package/package-dist/scripts/lib/import-curation/internal/workflow-source-reference-context.d.ts +38 -0
  259. package/package-dist/scripts/lib/import-curation/internal/workflow-source-reference-context.js +116 -0
  260. package/package-dist/scripts/lib/import-curation/mutation-manifest.d.ts +51 -0
  261. package/package-dist/scripts/lib/import-curation/mutation-manifest.js +542 -0
  262. package/package-dist/scripts/lib/import-curation/patch-collect.d.ts +18 -0
  263. package/package-dist/scripts/lib/import-curation/patch-collect.js +172 -0
  264. package/package-dist/scripts/lib/import-curation/profiles.d.ts +1 -0
  265. package/package-dist/scripts/lib/import-curation/profiles.js +1 -0
  266. package/package-dist/scripts/lib/import-curation/trace-summary.d.ts +1 -0
  267. package/package-dist/scripts/lib/import-curation/trace-summary.js +1 -0
  268. package/package-dist/scripts/lib/oauth-session-reference.d.ts +1 -0
  269. package/package-dist/scripts/lib/oauth-session-reference.js +20 -0
  270. package/package-dist/scripts/lib/post-authoring-finalize-utils.d.ts +95 -0
  271. package/package-dist/scripts/lib/post-authoring-finalize-utils.js +548 -0
  272. package/package-dist/scripts/lib/post-write-root-proof.d.ts +44 -0
  273. package/package-dist/scripts/lib/post-write-root-proof.js +169 -0
  274. package/package-dist/scripts/lib/reference-description-text.d.ts +2 -0
  275. package/package-dist/scripts/lib/reference-description-text.js +12 -0
  276. package/package-dist/scripts/lib/remote-verification-accepted-diff.d.ts +42 -0
  277. package/package-dist/scripts/lib/remote-verification-accepted-diff.js +424 -0
  278. package/package-dist/scripts/lib/same-identity-commit-recovery.d.ts +8 -0
  279. package/package-dist/scripts/lib/same-identity-commit-recovery.js +52 -0
  280. package/package-dist/scripts/lib/source-semantics.d.ts +255 -0
  281. package/package-dist/scripts/lib/source-semantics.js +888 -0
  282. package/package-dist/scripts/lib/stage-contract.d.ts +36 -0
  283. package/package-dist/scripts/lib/stage-contract.js +25 -0
  284. package/package-dist/scripts/lib/task-authorization.d.ts +84 -0
  285. package/package-dist/scripts/lib/task-authorization.js +210 -0
  286. package/package-dist/scripts/lib/tidas-adapter.d.ts +113 -0
  287. package/package-dist/scripts/lib/tidas-adapter.js +525 -0
  288. package/package-dist/scripts/lib/tidas-language-utils.d.ts +8 -0
  289. package/package-dist/scripts/lib/tidas-language-utils.js +206 -0
  290. package/package-dist/scripts/lib/tidas-row-utils.d.ts +70 -0
  291. package/package-dist/scripts/lib/tidas-row-utils.js +225 -0
  292. package/package-dist/scripts/lib/trace-coverage.d.ts +53 -0
  293. package/package-dist/scripts/lib/trace-coverage.js +178 -0
  294. package/package-dist/scripts/package-entry.d.ts +2 -0
  295. package/package-dist/scripts/package-entry.js +4 -0
  296. package/package-dist/scripts/public-api.d.ts +80 -0
  297. package/package-dist/scripts/public-api.js +28 -0
  298. package/package-dist/scripts/runtime-entry.d.ts +23 -0
  299. package/package-dist/scripts/runtime-entry.js +424 -0
  300. package/package.json +93 -0
  301. package/specs/canonical-support/flow-properties-unit-groups.json +2692 -0
  302. package/specs/import-profiles.json +118 -0
  303. package/specs/prewrite-content-policy.json +105 -0
  304. package/specs/schemas/authorization-derivation.schema.json +34 -0
  305. package/specs/schemas/execution-context.schema.json +68 -0
  306. package/specs/schemas/foundry-authorization-input.schema.json +120 -0
  307. package/specs/schemas/foundry-facade-request-index.schema.json +78 -0
  308. package/specs/schemas/foundry-managed-runtime.schema.json +122 -0
  309. package/specs/schemas/foundry-migration-activation.schema.json +213 -0
  310. package/specs/schemas/foundry-migration-adoption-plan.schema.json +548 -0
  311. package/specs/schemas/foundry-migration-transfer-receipt.schema.json +93 -0
  312. package/specs/schemas/foundry-operation-result.schema.json +147 -0
  313. package/specs/schemas/foundry-package-descriptor.schema.json +134 -0
  314. package/specs/schemas/foundry-runtime-selection.schema.json +59 -0
  315. package/specs/schemas/foundry-semantic-input.schema.json +36 -0
  316. package/specs/schemas/foundry-task-start.schema.json +123 -0
  317. package/specs/schemas/foundry-workspace-migration-pending.schema.json +17 -0
  318. package/specs/schemas/foundry-workspace-migration-plan.schema.json +59 -0
  319. package/specs/schemas/foundry-workspace-migration-transfer-plan.schema.json +357 -0
  320. package/specs/schemas/foundry-workspace-v2.schema.json +70 -0
  321. package/specs/schemas/runtime-qualification.schema.json +63 -0
  322. package/specs/schemas/task-authorization.schema.json +154 -0
  323. package/specs/schemas/tidas-runtime-expectation.schema.json +54 -0
@@ -0,0 +1,1720 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { taskAuthorizationAllows } from "../task-authorization.js";
4
+ import { readOnlyStageContract } from "../stage-contract.js";
5
+ const postAuthoringFinalizeStageContract = readOnlyStageContract([
6
+ {
7
+ stage: "prepare_scope",
8
+ phase: "prepare",
9
+ purpose: "Resolve dataset type, input rows, profile requirements, output directory, and full-context identity-preflight requirement.",
10
+ inputs: ["rows file", "profile options", "queue/context options"],
11
+ outputs: ["finalize scope"],
12
+ side_effects: [],
13
+ },
14
+ {
15
+ stage: "rewrite_and_cleanup",
16
+ phase: "rewrite_cleanup",
17
+ purpose: "Apply identity reference rewrites, unresolved exchange externalization, BAFU source/contact rewrites, canonical support rewrites, and deterministic curation cleanup.",
18
+ inputs: [
19
+ "input rows",
20
+ "identity/preflight indexes",
21
+ "BAFU source/contact policy",
22
+ "canonical support cache",
23
+ ],
24
+ outputs: ["rewritten rows", "cleaned rows", "rewrite reports"],
25
+ side_effects: ["writes local .foundry artifact files"],
26
+ },
27
+ {
28
+ stage: "gate_and_validate",
29
+ phase: "gate_validate",
30
+ purpose: "Run curation queue preparation, native Rust tidas validation, deterministic QA, location audit, curation gate, dry-run write planning, and optional remote reference verification.",
31
+ inputs: ["cleaned rows", "context files", "queue artifacts"],
32
+ outputs: ["validation/QA/gate/dry-run/remote-verify reports"],
33
+ side_effects: [
34
+ "runs native tidas and published CLI read-only checks",
35
+ "writes local .foundry artifact files",
36
+ ],
37
+ },
38
+ {
39
+ stage: "mutation_manifest",
40
+ phase: "gate_validate",
41
+ purpose: "Build exact-scope mutation evidence and aggregate prewrite blockers before any explicit commit handoff.",
42
+ inputs: ["cleaned rows", "all prewrite reports"],
43
+ outputs: ["dataset-mutation-manifest report"],
44
+ side_effects: ["writes local .foundry artifact files"],
45
+ },
46
+ {
47
+ stage: "report",
48
+ phase: "report",
49
+ purpose: "Emit the post-authoring finalize report with runtime stages, counts, files, and blockers.",
50
+ inputs: ["runtime stage reports", "mutation manifest"],
51
+ outputs: ["dataset-post-authoring-finalize-report.json"],
52
+ side_effects: ["writes local .foundry artifact files"],
53
+ },
54
+ ]);
55
+ const postCleanupArtifactDirectories = [
56
+ "source-contact-support-finalize",
57
+ "identity-preflight-run",
58
+ "identity-preflight-run-parallel",
59
+ "identity-preflight-current-scope",
60
+ "curation-queue",
61
+ "curation-queue-inputs",
62
+ "schema",
63
+ "qa",
64
+ "location-audit",
65
+ "curation-gate",
66
+ "dry-run",
67
+ "precommit-verify-remote",
68
+ "mutation-manifest",
69
+ "commit-handoff",
70
+ ];
71
+ const postCleanupArtifactFiles = [
72
+ "identity-reference-rewrite-external-flow-refs.jsonl",
73
+ "process-reference-external-flow-refs.jsonl",
74
+ ];
75
+ function verifiedReferenceLedgerFilesForDir(ledgerDir) {
76
+ if (!ledgerDir)
77
+ return [];
78
+ return [
79
+ "ok.flows.verified.jsonl",
80
+ "ok.processes.verified.jsonl",
81
+ "ok.sources.verified.jsonl",
82
+ "ok.contacts.verified.jsonl",
83
+ "ok.unitgroups.verified.jsonl",
84
+ "ok.flowproperties.verified.jsonl",
85
+ ]
86
+ .map((name) => path.join(ledgerDir, name))
87
+ .filter((filePath) => fs.existsSync(filePath) && fs.statSync(filePath).isFile());
88
+ }
89
+ export function createPostAuthoringFinalizeCommands({ appendOption, applyCanonicalSupportRewrites, applyIdentityReferenceRewrites, asText, profileFor, blockersFromLocationAuditStage, buildLibraryContactPayload, booleanOption, compactStageReport, cloneJson, contactGlobalReference, datasetIdentity, datasetRowsFileStem, ensureArray, externalizeUnresolvedProcessFlowExchanges, fileExists, normalizedList, nowIso, postAuthoringPrewriteGateBlockers, processSourceReferenceRows, profileFullContextRequirement, repoRelativeMaybe, loadCanonicalSupportCache, repoRelativePath, repoRoot, reportFileFromCliStage, readRowsFile, repairTrueSourceClassification, repairTrueSourceDescription, repairTrueSourceIdentity, resolveRepoPath, rewriteCanonicalSourceReferences, rewriteContactReferences, rewriteTrueSourceReferenceDescriptions, runDatasetCommitHandoffPlan, runDatasetCurationCleanup, runDatasetCurationGate, runDatasetMutationManifest, runFinalizeAutoCurationQueue, runFinalizeIdentityPreflightStage, runTidasRowsValidation, runTiangongJsonStage, skippedPrewriteStage, sourceReferenceSemanticBlockers, sourceReferenceRewritesFileForRowsFile, sourceSemanticSummary, unique, writeFinalizeImportLedger, writeJson, writeJsonLines, }) {
90
+ function stageStatus(value) {
91
+ if (!value || typeof value !== "object")
92
+ return "completed";
93
+ return value.status ?? value.report?.status ?? "completed";
94
+ }
95
+ function timedFinalizeStage(timings, stage, fn) {
96
+ const startedAtUtc = nowIso();
97
+ const startedAtMs = Date.now();
98
+ try {
99
+ const result = fn();
100
+ timings.push({
101
+ stage,
102
+ status: stageStatus(result),
103
+ started_at_utc: startedAtUtc,
104
+ finished_at_utc: nowIso(),
105
+ duration_ms: Date.now() - startedAtMs,
106
+ });
107
+ return result;
108
+ }
109
+ catch (error) {
110
+ timings.push({
111
+ stage,
112
+ status: "failed",
113
+ started_at_utc: startedAtUtc,
114
+ finished_at_utc: nowIso(),
115
+ duration_ms: Date.now() - startedAtMs,
116
+ error: String(error instanceof Error ? error.message : error),
117
+ });
118
+ throw error;
119
+ }
120
+ }
121
+ function compactStageReportWithTiming(stageReport, timingsByStage) {
122
+ const compacted = compactStageReport(stageReport);
123
+ const timing = compacted.stage ? timingsByStage.get(compacted.stage) : undefined;
124
+ if (!timing)
125
+ return compacted;
126
+ return {
127
+ ...compacted,
128
+ started_at_utc: timing.started_at_utc,
129
+ finished_at_utc: timing.finished_at_utc,
130
+ duration_ms: timing.duration_ms,
131
+ };
132
+ }
133
+ function rowDatasetType(payload, fallbackType) {
134
+ if (payload?.processDataSet)
135
+ return "process";
136
+ if (payload?.flowDataSet)
137
+ return "flow";
138
+ if (payload?.lifeCycleModelDataSet)
139
+ return "lifecyclemodel";
140
+ if (payload?.contactDataSet)
141
+ return "contact";
142
+ if (payload?.sourceDataSet)
143
+ return "source";
144
+ if (payload?.flowPropertyDataSet)
145
+ return "flowproperty";
146
+ if (payload?.unitGroupDataSet)
147
+ return "unitgroup";
148
+ return fallbackType;
149
+ }
150
+ // P1a (historical BAFU-cleanup backlog): under the explicit --mint-unmatched-fp-ug-support
151
+ // flag, lift the scope's UNMATCHED Unit Groups + Flow Properties into the support
152
+ // commit set, so the existing source/contact "support" sub-finalize + handoff
153
+ // mints them ONCE as account-local My Data and commits them BEFORE the flows that
154
+ // reference them (deduped across scopes by the runner's verified-support cache).
155
+ // "Unmatched" = a materialized FP/UG whose UUID is not already a canonical-cache
156
+ // id; reusable canonical FP/UG (e.g. Mass 93a60a56) keep their public reference
157
+ // and are never minted. UGs precede FPs so a minted FP's reference unit group is
158
+ // in scope. This generic support-mint flag is enabled by the USLCI and Worldsteel
159
+ // adapters; BAFU private incubation uses its separate candidate registry and guarded
160
+ // owner-draft path.
161
+ // Read a Flow Property row's referenceToReferenceUnitGroup id + version.
162
+ function flowPropertyReferenceUnitGroup(fpRow) {
163
+ const referenceUnitGroup = fpRow?.flowPropertyDataSet?.flowPropertiesInformation?.quantitativeReference
164
+ ?.referenceToReferenceUnitGroup;
165
+ if (!referenceUnitGroup)
166
+ return { reference: null, id: "", version: "" };
167
+ return {
168
+ reference: referenceUnitGroup,
169
+ id: asText(referenceUnitGroup["@refObjectId"] ??
170
+ referenceUnitGroup.refObjectId ??
171
+ referenceUnitGroup.ref_object_id),
172
+ version: asText(referenceUnitGroup["@version"]),
173
+ };
174
+ }
175
+ // Coherent model — REUSED (canonical) dependency proof. Given a set of support rows
176
+ // (the exact rows a SUPPORT scope writes), derive the canonical Unit Group
177
+ // id@published-version proof keys for every minted Flow Property whose reference Unit
178
+ // Group is a public canonical dataset (present in the canonical-support cache) and is
179
+ // therefore NOT written as account-local My Data. The SUPPORT sub-finalize writes the
180
+ // minted FP but must prove the reused canonical UG as a remote reference in its OWN
181
+ // mutation manifest — otherwise closure (verify-remote OFF) cannot prove the FP->UG
182
+ // edge. Only UGs present in the canonical cache are surfaced; account-local UGs in the
183
+ // same support set are written and proven via plannedRootKeys, never here. Empty unless
184
+ // the account-local override is active; the frozen profile determines which candidate
185
+ // path may consume the proof.
186
+ function deriveCanonicalUnitGroupProofKeysFromSupportRows(supportRows, options) {
187
+ const { index } = loadCanonicalSupportCache(options);
188
+ const writtenUnitGroupIds = new Set(supportRows
189
+ .filter((row) => row?.unitGroupDataSet)
190
+ .map((row) => datasetIdentity(row, "unitgroup").id)
191
+ .filter(Boolean));
192
+ const proofKeys = [];
193
+ const seen = new Set();
194
+ for (const row of supportRows) {
195
+ if (!row?.flowPropertyDataSet)
196
+ continue;
197
+ const { id: ugId } = flowPropertyReferenceUnitGroup(row);
198
+ if (!ugId)
199
+ continue;
200
+ // A UG actually written in this support set is a planned root, not a reused remote
201
+ // reference; never prove it here.
202
+ if (writtenUnitGroupIds.has(ugId))
203
+ continue;
204
+ const canonicalUnitGroup = index.unitGroupById.get(ugId);
205
+ if (!canonicalUnitGroup)
206
+ continue;
207
+ const canonicalVersion = asText(canonicalUnitGroup.version);
208
+ if (!canonicalVersion)
209
+ continue;
210
+ const proofKey = `unitgroups:${ugId}@${canonicalVersion}`;
211
+ if (seen.has(proofKey))
212
+ continue;
213
+ seen.add(proofKey);
214
+ proofKeys.push({ id: ugId, version: canonicalVersion });
215
+ }
216
+ return proofKeys;
217
+ }
218
+ function collectAccountLocalFpUgSupportRows(options) {
219
+ if (!booleanOption(options.mintUnmatchedFpUgSupport))
220
+ return { rows: [], canonicalUnitGroupProofKeys: [] };
221
+ const { index } = loadCanonicalSupportCache(options);
222
+ const ugFile = resolveRepoPath(options.supportUnitgroupRowsFile);
223
+ const fpFile = resolveRepoPath(options.supportFlowpropertyRowsFile);
224
+ const ugRows = ugFile && fileExists(ugFile) ? readRowsFile(ugFile) : [];
225
+ const fpRows = fpFile && fileExists(fpFile) ? readRowsFile(fpFile) : [];
226
+ // Only genuinely account-local UGs/FPs are minted. A UG/FP whose UUID is already a
227
+ // canonical-cache id (e.g. Units of energy 93a60a57@03.00.003) is a public dataset
228
+ // and must NOT be written as account-local My Data @00.00.001 — doing so triggers a
229
+ // remote `version_outdated` blocker (root role) because the database already
230
+ // publishes it at a higher version.
231
+ // Account-local minted support is written as My Data at 00.00.001. The converter emits
232
+ // each minted FP/UG with its NATIVE EF3.1 own dataSetVersion (e.g. FP 33.00.000, UG
233
+ // 20.24.002 / 20.20.002) but leaves every reference TO it (FP->UG, flow->FP) at the
234
+ // @00.00.001 placeholder. Two consequences force the 00.00.001 normalization:
235
+ // (a) those native (id, version) slots are already occupied by another account in this
236
+ // DB, so committing at the native version fails with HTTP 409 / 23505; 00.00.001 is
237
+ // free (verified) — the same My Data convention used for the account-local flows.
238
+ // (b) plannedRootReferenceKeys keys the in-scope UG/FP root by its OWN version; the
239
+ // references are already @00.00.001, so aligning the roots DOWN to 00.00.001 makes
240
+ // the FP->UG (and flow->FP) edges prove in-scope with no remote/proof key needed.
241
+ // USLCI's minted FEDEFL FP/UG are already @00.00.001, so this is a no-op for it. The
242
+ // function is behind the separate mintUnmatchedFpUgSupport flag; BAFU private
243
+ // incubation must use its profile-specific guarded owner-draft path unless that
244
+ // generic mint route is explicitly reviewed and enabled later.
245
+ const ACCOUNT_LOCAL_MINT_VERSION = "00.00.001";
246
+ const setSupportOwnVersion = (row, rootKey) => {
247
+ const next = cloneJson(row);
248
+ const admin = next[rootKey]?.administrativeInformation;
249
+ if (admin) {
250
+ admin.publicationAndOwnership = admin.publicationAndOwnership || {};
251
+ admin.publicationAndOwnership["common:dataSetVersion"] = ACCOUNT_LOCAL_MINT_VERSION;
252
+ }
253
+ return next;
254
+ };
255
+ const mintUnitGroups = ugRows
256
+ .filter((row) => {
257
+ const id = datasetIdentity(row, "unitgroup").id;
258
+ return id && !index.unitGroupById.has(id);
259
+ })
260
+ .map((row) => setSupportOwnVersion(row, "unitGroupDataSet"));
261
+ const mintFlowProperties = fpRows.filter((row) => {
262
+ const id = datasetIdentity(row, "flowproperty").id;
263
+ return id && !index.flowPropertyById.has(id);
264
+ });
265
+ // FIX C/D (corrected): a minted (account-local) Flow Property references its reference
266
+ // Unit Group. That UG is one of two cases:
267
+ // 1. Account-local/minted (NOT in the canonical cache): it is already in
268
+ // mintUnitGroups above and written as support, so the FP->UG edge is in scope.
269
+ // 2. Canonical (IN the canonical cache, published e.g. @03.00.003): it must NOT be
270
+ // written. Instead the minted FP's referenceToReferenceUnitGroup must be
271
+ // rewritten to the canonical PUBLISHED version (the converter materialized it at
272
+ // @00.00.001), and the canonical UG id@published-version proven as a reusable
273
+ // remote reference (provenReferenceKeys) so closure passes without writing it.
274
+ // Without (2) the FP keeps referencing the canonical UG @00.00.001, which the remote
275
+ // verify reports as `version_outdated` (reference role); the earlier FIX D instead
276
+ // force-wrote the canonical UG @00.00.001 and added a `version_outdated` (root role).
277
+ // Entirely within the separately gated mintUnmatchedFpUgSupport branch; enabling the
278
+ // broader account-local profile policy alone does not enter this generic mint path.
279
+ const canonicalUnitGroupProofKeys = [];
280
+ const seenCanonicalProofKeys = new Set();
281
+ const rewrittenFlowProperties = mintFlowProperties.map((fpRow) => {
282
+ const { id: ugId, version: currentVersion } = flowPropertyReferenceUnitGroup(fpRow);
283
+ const canonicalUnitGroup = ugId ? index.unitGroupById.get(ugId) : null;
284
+ let out = fpRow;
285
+ if (canonicalUnitGroup) {
286
+ // Case 2: the FP references a CANONICAL (published) Unit Group. Do NOT write that UG;
287
+ // rewrite the FP reference to the canonical published version and prove it as a
288
+ // reusable remote reference so closure passes without writing the UG.
289
+ const canonicalVersion = asText(canonicalUnitGroup.version);
290
+ if (canonicalVersion) {
291
+ const proofKey = `unitgroups:${ugId}@${canonicalVersion}`;
292
+ if (!seenCanonicalProofKeys.has(proofKey)) {
293
+ seenCanonicalProofKeys.add(proofKey);
294
+ canonicalUnitGroupProofKeys.push({ id: ugId, version: canonicalVersion });
295
+ }
296
+ if (currentVersion !== canonicalVersion) {
297
+ out = cloneJson(fpRow);
298
+ out.flowPropertyDataSet.flowPropertiesInformation.quantitativeReference.referenceToReferenceUnitGroup["@version"] = canonicalVersion;
299
+ }
300
+ }
301
+ }
302
+ // Case 1 (account-local reference UG): leave the FP->UG reference at its @00.00.001
303
+ // placeholder — it now matches the account-local UG minted at 00.00.001 above. In BOTH
304
+ // cases the minted FP itself is account-local My Data, so normalise its OWN version to
305
+ // 00.00.001 (its native version, e.g. 33.00.000, is likewise occupied -> 23505).
306
+ return setSupportOwnVersion(out, "flowPropertyDataSet");
307
+ });
308
+ return {
309
+ rows: [...mintUnitGroups, ...rewrittenFlowProperties],
310
+ canonicalUnitGroupProofKeys,
311
+ };
312
+ }
313
+ function applySourceContactRewrites({ datasetType, rowsFile, outDir, options, }) {
314
+ const profile = String(options.profile || "generic")
315
+ .trim()
316
+ .toLowerCase();
317
+ // Libraries that use the one-shared-library-contact model: the deterministic
318
+ // source/contact rewrite folds every row's ownership + data-entry contact onto
319
+ // a single library contact and lifts referenced true sources into the support
320
+ // commit set. BAFU was first; USLCI (NREL) reuses the same machinery via its
321
+ // profile-driven library contact identity; worldsteel reuses it with the
322
+ // worldsteel attribution contact carried in its runner config.
323
+ const supportedForProfile = profile === "bafu" || profile === "uslci" || profile === "worldsteel";
324
+ const outputRowsFile = path.join(outDir, `${datasetRowsFileStem(datasetType)}.source-contact-rewritten.jsonl`);
325
+ const reportPath = path.join(outDir, "source-contact-rewrites-report.json");
326
+ const sourceReferenceRewritesPath = path.join(outDir, "source-reference-rewrites.jsonl");
327
+ const supportRowsPath = path.join(outDir, "support.jsonl");
328
+ fs.mkdirSync(outDir, { recursive: true });
329
+ if (!supportedForProfile || booleanOption(options.skipSourceContactRewrites)) {
330
+ // Coherent model — REUSED (canonical) dependency proof for the SUPPORT sub-finalize.
331
+ // The SUPPORT sub-finalize runs with --skip-source-contact-rewrites, so the parent's
332
+ // source/contact rewrite (which computed canonical_support) does not re-run here. But
333
+ // the minted Flow Properties this scope WRITES still reference public canonical Unit
334
+ // Groups that this scope does NOT write; closure (verify-remote OFF) cannot prove the
335
+ // FP->canonical-UG edge unless the canonical UG id@published-version is in THIS
336
+ // sub-finalize's own mutation-manifest provenReferenceKeys. Re-derive the
337
+ // canonical_support block from the exact support rows being finalized so the support
338
+ // sub-finalize proves its own reused canonical UGs through the same channel the
339
+ // dependent finalize uses. This proof is gated on the account-local profile
340
+ // override and remains read-only; it does not mint or publish FP/UG rows.
341
+ const skippedCanonicalUnitGroupProofKeys = fileExists(rowsFile)
342
+ ? deriveCanonicalUnitGroupProofKeysFromSupportRows(readRowsFile(rowsFile), options)
343
+ : [];
344
+ const report = {
345
+ schema_version: 1,
346
+ status: "skipped",
347
+ reason: supportedForProfile
348
+ ? "Skipped by --skip-source-contact-rewrites."
349
+ : "Source/contact deterministic rewrites are currently profile-specific and only required for BAFU.",
350
+ profile,
351
+ rows_file: repoRelativePath(rowsFile),
352
+ counts: {
353
+ input_rows: 0,
354
+ output_rows: 0,
355
+ contact_reference_rewrites: 0,
356
+ source_reference_rewrites: 0,
357
+ support_rows: 0,
358
+ },
359
+ // Empty when the adapter's mint flag is off; populated for a flag-enabled support
360
+ // sub-finalize so its mutation manifest can prove the FP->canonical-UG edge it writes.
361
+ canonical_support: {
362
+ canonical_unit_group_reference_keys: skippedCanonicalUnitGroupProofKeys,
363
+ },
364
+ files: {},
365
+ };
366
+ writeJson(reportPath, report);
367
+ return {
368
+ ...report,
369
+ output_rows_file: repoRelativePath(rowsFile),
370
+ files: {
371
+ report: repoRelativePath(reportPath),
372
+ },
373
+ };
374
+ }
375
+ const rows = readRowsFile(rowsFile);
376
+ const sourceReferenceRewriteRows = [];
377
+ const stats = {
378
+ source_reference_rewrites: 0,
379
+ true_source_identity_repairs: 0,
380
+ true_source_description_repairs: 0,
381
+ true_source_classification_repairs: 0,
382
+ true_source_reference_description_repairs: 0,
383
+ };
384
+ const sourceSupportRepairRows = [];
385
+ const sourceSupportSemanticsRows = [];
386
+ const libraryContact = buildLibraryContactPayload(options, null, {
387
+ rewriteRows: sourceReferenceRewriteRows,
388
+ stats,
389
+ });
390
+ const libraryContactIdentity = datasetIdentity(libraryContact, "contact");
391
+ const libraryContactName = asText(libraryContact.contactDataSet.contactInformation.dataSetInformation["common:name"]?.["#text"]);
392
+ const libraryContactRef = contactGlobalReference({
393
+ id: libraryContactIdentity.id,
394
+ version: libraryContactIdentity.version,
395
+ shortDescription: libraryContactName,
396
+ language: asText(options.language || options.lang || "en") || "en",
397
+ });
398
+ const sourceSupportRowsFile = resolveRepoPath(options.sourceSupportRowsFile || options.supportSourceRowsFile || options.sourceSupportRows);
399
+ const sourceSupportPayloads = new Map();
400
+ const sourceLookup = new Map();
401
+ if (fileExists(sourceSupportRowsFile)) {
402
+ const supportContactRewriteStats = {
403
+ rewritten: 0,
404
+ previous_ids: new Set(),
405
+ previous_descriptions: new Set(),
406
+ };
407
+ for (const sourceRow of readRowsFile(sourceSupportRowsFile)) {
408
+ if (!sourceRow?.sourceDataSet)
409
+ continue;
410
+ const payload = cloneJson(sourceRow);
411
+ const identity = datasetIdentity(payload, "source");
412
+ if (!identity.id)
413
+ continue;
414
+ rewriteContactReferences(payload, libraryContactRef, supportContactRewriteStats);
415
+ repairTrueSourceIdentity(payload, {
416
+ sourceFile: sourceSupportRowsFile,
417
+ stats,
418
+ repairRows: sourceSupportRepairRows,
419
+ });
420
+ repairTrueSourceDescription(payload, {
421
+ sourceFile: sourceSupportRowsFile,
422
+ stats,
423
+ repairRows: sourceSupportRepairRows,
424
+ });
425
+ repairTrueSourceClassification(payload, {
426
+ sourceFile: sourceSupportRowsFile,
427
+ stats,
428
+ repairRows: sourceSupportRepairRows,
429
+ });
430
+ rewriteCanonicalSourceReferences(payload, {
431
+ datasetType: "source",
432
+ sourceFile: sourceSupportRowsFile,
433
+ stats,
434
+ rewriteRows: sourceReferenceRewriteRows,
435
+ datasetIdentityCache: datasetIdentity(payload, "source"),
436
+ });
437
+ const repairedIdentity = datasetIdentity(payload, "source");
438
+ const summary = sourceSemanticSummary(payload, sourceSupportRowsFile);
439
+ sourceSupportSemanticsRows.push(summary);
440
+ sourceLookup.set(summary.dataset_id, summary);
441
+ sourceSupportPayloads.set(`${repairedIdentity.id}::${repairedIdentity.version || "00.00.001"}`, payload);
442
+ }
443
+ }
444
+ const contactRewriteStats = {
445
+ rewritten: 0,
446
+ previous_ids: new Set(),
447
+ previous_descriptions: new Set(),
448
+ };
449
+ const rewrittenRows = rows.map((row) => {
450
+ const payload = cloneJson(row);
451
+ const type = rowDatasetType(payload, datasetType);
452
+ rewriteContactReferences(payload, libraryContactRef, contactRewriteStats);
453
+ rewriteCanonicalSourceReferences(payload, {
454
+ datasetType: type,
455
+ sourceFile: rowsFile,
456
+ stats,
457
+ rewriteRows: sourceReferenceRewriteRows,
458
+ datasetIdentityCache: datasetIdentity(payload, type),
459
+ // CLASS 2 fix (gated by the frozen account-local profile policy): also rewrite a
460
+ // format/compliance support source
461
+ // referenced OUTSIDE its format/compliance slot (e.g. a process
462
+ // modellingAndValidation/validation/review/common:referenceToCompleteReviewReport
463
+ // pointing at an "ILCD format" source) to the public canonical source, using the
464
+ // referenced source's semantic kind. Without the lookup the rewrite only fires on
465
+ // path-relations, leaving the review-report ref stale and unprovable. Passing the
466
+ // lookup only under the override keeps default public-only profiles unchanged.
467
+ sourceLookup: sourceLookup.size > 0 ? sourceLookup : null,
468
+ });
469
+ if (type === "process" && sourceLookup.size > 0) {
470
+ rewriteTrueSourceReferenceDescriptions(payload.processDataSet, {
471
+ sourceLookup,
472
+ sourceFile: rowsFile,
473
+ stats,
474
+ rewriteRows: sourceReferenceRewriteRows,
475
+ datasetIdentityCache: datasetIdentity(payload, type),
476
+ });
477
+ }
478
+ return payload;
479
+ });
480
+ const allProcessSourceReferenceRowsForScope = sourceLookup.size > 0
481
+ ? rewrittenRows.flatMap((payload) => {
482
+ const type = rowDatasetType(payload, datasetType);
483
+ return type === "process"
484
+ ? processSourceReferenceRows(payload, sourceLookup, rowsFile)
485
+ : [];
486
+ })
487
+ : [];
488
+ // The process_data_source semantic check stays scoped to referenceToDataSource.
489
+ const processSourceReferenceRowsForScope = allProcessSourceReferenceRowsForScope.filter((row) => row.relation === "process_data_source");
490
+ const sourceSemanticBlockers = typeof sourceReferenceSemanticBlockers === "function"
491
+ ? sourceReferenceSemanticBlockers(processSourceReferenceRowsForScope)
492
+ : [];
493
+ // But the support commit must carry EVERY referenced true source, regardless of
494
+ // which field points at it (e.g. validation/review/referenceToCompleteReviewReport),
495
+ // so the process passes reference closure when it commits.
496
+ const referencedTrueSourceKeys = new Set(allProcessSourceReferenceRowsForScope
497
+ .filter((row) => row.referenced_source_kind === "true_source" && row.ref_object_id)
498
+ .map((row) => `${row.ref_object_id}::${row.version || "00.00.001"}`));
499
+ // FIX C (corrected): a review-report source (modellingAndValidation/validation/review
500
+ // -> common:referenceToCompleteReviewReport) can exist in the materialized source
501
+ // support bundle yet not be classified as kind="true_source" by the process-level
502
+ // referenced_source_kind (e.g. missing/weak citation). Defensively harvest such
503
+ // validation/review refs into the support commit, BUT ONLY when the resolved support
504
+ // payload is itself a true source (materializable). A format/compliance/placeholder
505
+ // support source (e.g. an "ILCD format" / "Data set formats" source landing in a
506
+ // referenceToCompleteReviewReport slot) is NOT a writable BAFU-owned source — its
507
+ // reference is rewritten to the public canonical source elsewhere and it must never
508
+ // be committed, otherwise the support sub-finalize blocks on
509
+ // source_identity_not_true_source / source_classification_not_true_source
510
+ // (mutation_manifest_not_ready -> handoff_plan_not_ready). Restricting the harvest to
511
+ // true-source kinds keeps closure correct while never minting a non-true source.
512
+ const trueSourceSupportIds = new Set(sourceSupportSemanticsRows
513
+ .filter((summary) => summary?.kind === "true_source")
514
+ .map((summary) => summary?.dataset_id)
515
+ .filter(Boolean));
516
+ for (const row of allProcessSourceReferenceRowsForScope) {
517
+ if (!row.ref_object_id)
518
+ continue;
519
+ const pathText = String(row.path || "");
520
+ const isReviewReportRef = pathText.includes("validation") && pathText.includes("review");
521
+ if (!isReviewReportRef)
522
+ continue;
523
+ if (!trueSourceSupportIds.has(row.ref_object_id))
524
+ continue;
525
+ const key = `${row.ref_object_id}::${row.version || "00.00.001"}`;
526
+ if (sourceSupportPayloads.has(key))
527
+ referencedTrueSourceKeys.add(key);
528
+ }
529
+ const referencedTrueSourceRows = [...referencedTrueSourceKeys]
530
+ .map((key) => sourceSupportPayloads.get(key))
531
+ .filter((row) => Boolean(row));
532
+ writeJsonLines(outputRowsFile, rewrittenRows);
533
+ writeJsonLines(sourceReferenceRewritesPath, sourceReferenceRewriteRows);
534
+ const sourceSupportSemanticsPath = path.join(outDir, "source-support-semantics.jsonl");
535
+ const sourceSupportRepairsPath = path.join(outDir, "source-support-repairs.jsonl");
536
+ writeJsonLines(sourceSupportSemanticsPath, sourceSupportSemanticsRows);
537
+ writeJsonLines(sourceSupportRepairsPath, sourceSupportRepairRows);
538
+ const { rows: accountLocalFpUgSupportRows, canonicalUnitGroupProofKeys } = collectAccountLocalFpUgSupportRows(options);
539
+ writeJsonLines(supportRowsPath, [
540
+ ...accountLocalFpUgSupportRows,
541
+ libraryContact,
542
+ ...referencedTrueSourceRows,
543
+ ]);
544
+ const totalRewrites = Number(contactRewriteStats.rewritten ?? 0) + Number(stats.source_reference_rewrites ?? 0);
545
+ const report = {
546
+ schema_version: 1,
547
+ status: sourceSemanticBlockers.length > 0
548
+ ? "blocked"
549
+ : totalRewrites > 0
550
+ ? "completed"
551
+ : "completed_no_rewrites",
552
+ profile,
553
+ rows_file: repoRelativePath(rowsFile),
554
+ output_rows_file: repoRelativePath(outputRowsFile),
555
+ policy: {
556
+ contact: "BAFU imports use one database-level FOEN/BAFU ownership contact for every row in the library.",
557
+ source: "Data set format and compliance source references are canonical public support references, not BAFU source rows.",
558
+ },
559
+ counts: {
560
+ input_rows: rows.length,
561
+ output_rows: rewrittenRows.length,
562
+ contact_reference_rewrites: contactRewriteStats.rewritten,
563
+ source_reference_rewrites: sourceReferenceRewriteRows.length,
564
+ support_rows: 1 + referencedTrueSourceRows.length + accountLocalFpUgSupportRows.length,
565
+ support_contact_rows: 1,
566
+ support_source_rows: referencedTrueSourceRows.length,
567
+ support_account_local_fp_ug_rows: accountLocalFpUgSupportRows.length,
568
+ support_unitgroup_rows: accountLocalFpUgSupportRows.filter((row) => row?.unitGroupDataSet)
569
+ .length,
570
+ support_flowproperty_rows: accountLocalFpUgSupportRows.filter((row) => row?.flowPropertyDataSet).length,
571
+ source_support_candidate_rows: sourceSupportSemanticsRows.length,
572
+ true_source_identity_repairs: stats.true_source_identity_repairs,
573
+ true_source_description_repairs: stats.true_source_description_repairs,
574
+ true_source_classification_repairs: stats.true_source_classification_repairs,
575
+ true_source_reference_description_repairs: stats.true_source_reference_description_repairs,
576
+ },
577
+ contact: {
578
+ id: libraryContactIdentity.id,
579
+ version: libraryContactIdentity.version,
580
+ name: libraryContactName,
581
+ previous_ids: [...contactRewriteStats.previous_ids].sort(),
582
+ previous_descriptions: [...contactRewriteStats.previous_descriptions].sort(),
583
+ },
584
+ source_support: {
585
+ source_support_rows_file: fileExists(sourceSupportRowsFile)
586
+ ? repoRelativePath(sourceSupportRowsFile)
587
+ : null,
588
+ referenced_true_source_ids: referencedTrueSourceRows.map((payload) => datasetIdentity(payload, "source").id),
589
+ // FIX C: id+version keys for every committed true source (including
590
+ // defensively-harvested review-report sources), so the process.finalize
591
+ // reference-closure proof can include them by exact reference key.
592
+ referenced_true_source_keys: referencedTrueSourceRows.map((payload) => {
593
+ const identity = datasetIdentity(payload, "source");
594
+ return { id: identity.id, version: identity.version || "00.00.001" };
595
+ }),
596
+ },
597
+ // CLASS 1 fix: minted (account-local) Flow Properties whose reference Unit Group is
598
+ // a public canonical UG are NOT written; the FP's referenceToReferenceUnitGroup is
599
+ // rewritten to the canonical published version and the canonical UG id@version is
600
+ // surfaced here so the mutation-manifest reference-closure proof can mark it as a
601
+ // reusable remote reference (proven, not written). Empty unless the separate
602
+ // --mint-unmatched-fp-ug-support path is explicitly enabled.
603
+ canonical_support: {
604
+ canonical_unit_group_reference_keys: canonicalUnitGroupProofKeys,
605
+ },
606
+ files: {
607
+ output_rows: repoRelativePath(outputRowsFile),
608
+ source_reference_rewrites: repoRelativePath(sourceReferenceRewritesPath),
609
+ support_rows: repoRelativePath(supportRowsPath),
610
+ source_support_semantics: repoRelativePath(sourceSupportSemanticsPath),
611
+ source_support_repairs: repoRelativePath(sourceSupportRepairsPath),
612
+ report: repoRelativePath(reportPath),
613
+ },
614
+ blockers: sourceSemanticBlockers,
615
+ };
616
+ writeJson(reportPath, report);
617
+ return {
618
+ ...report,
619
+ output_rows_file: repoRelativePath(outputRowsFile),
620
+ };
621
+ }
622
+ function runDatasetPostAuthoringFinalize(options) {
623
+ const finalizeStartedAtUtc = nowIso();
624
+ const finalizeStartedAtMs = Date.now();
625
+ const finalizeTimings = [];
626
+ const timeStage = (stage, fn) => timedFinalizeStage(finalizeTimings, stage, fn);
627
+ const datasetType = String(options.type || options.datasetType || "process")
628
+ .trim()
629
+ .toLowerCase();
630
+ const taskAuthorization = typeof profileFor === "function"
631
+ ? profileFor(repoRoot, String(options.profile || "generic")
632
+ .trim()
633
+ .toLowerCase(), options)?.authorization
634
+ : null;
635
+ const supportTypes = ["contact", "source"];
636
+ const mixedSupportTypes = ["support"];
637
+ const authoredTypes = ["process", "flow", "lifecyclemodel"];
638
+ const curationGateTypes = [...supportTypes, ...mixedSupportTypes, ...authoredTypes];
639
+ const supportedTypes = [...supportTypes, ...mixedSupportTypes, ...authoredTypes];
640
+ const requiresDeterministicQa = authoredTypes.includes(datasetType);
641
+ const requiresCurationGate = curationGateTypes.includes(datasetType);
642
+ if (options.help) {
643
+ return {
644
+ schema_version: 1,
645
+ status: "help",
646
+ command: "dataset-post-authoring-finalize",
647
+ usage: [
648
+ "node scripts/foundry.ts dataset-post-authoring-finalize --type <support|contact|source|process|flow|lifecyclemodel> --rows-file <patched-or-classified-rows.jsonl> --out-dir <finalize-dir> --profile <profile> --queue-dir <queue-dir> --classification-queue <classification-authoring-queue.jsonl> --location-queue <location-authoring-queue.jsonl> --identity-preflight-index <identity-preflight-requests.jsonl> --run-identity-preflight --schema-file <schema.json> --yaml-file <methodology.yaml> --ruleset-file <ruleset.json> --classification-decision-apply-report <classification-decisions-apply-report.json> --location-decision-apply-report <location-decisions-apply-report.json> --patch-collect-report <authoring-patch-collect-report.json> --patch-apply-report <dataset-patch-apply-report.json> --require-patch-collect-report --target-user-id <uuid> --verify-remote --finalize-source-contact-support --ledger-dir <task-import-ledger-dir>",
649
+ ],
650
+ purpose: "Run the post-AI authoring prewrite chain for support, process, flow, or lifecyclemodel rows: cleanup, native Rust tidas validation, location audit, post-authoring curation gate, dry-run publish/save, optional remote reference verification, and mutation manifest. Process/flow/lifecyclemodel rows additionally run deterministic QA. This command never commits rows.",
651
+ ...postAuthoringFinalizeStageContract,
652
+ supported_types: supportedTypes,
653
+ };
654
+ }
655
+ if (!supportedTypes.includes(datasetType)) {
656
+ throw new Error(`dataset-post-authoring-finalize supports support, contact, source, process, flow, and lifecyclemodel rows. Unsupported type: ${datasetType}.`);
657
+ }
658
+ const rowsFile = resolveRepoPath(options.rowsFile || options.input || options.rows);
659
+ if (!rowsFile || !fileExists(rowsFile)) {
660
+ throw new Error("--rows-file is required and must point to patched or authored TIDAS rows.");
661
+ }
662
+ const outDir = resolveRepoPath(options.outDir || `.foundry/workspaces/${datasetType}-post-authoring-finalize`);
663
+ fs.mkdirSync(outDir, { recursive: true });
664
+ const fullContextRequirement = profileFullContextRequirement(options.profile, datasetType);
665
+ const identityPreflightRequired = ["flow", "process"].includes(datasetType) &&
666
+ (booleanOption(options.requireIdentityPreflight) || Boolean(fullContextRequirement));
667
+ const identityReferenceRewriteStage = timeStage("identity_reference_rewrites", () => applyIdentityReferenceRewrites({
668
+ datasetType,
669
+ rowsFile,
670
+ outFile: path.join(outDir, "identity-reference-rewrites", `${datasetRowsFileStem(datasetType)}.identity-rewritten.jsonl`),
671
+ outDir: path.join(outDir, "identity-reference-rewrites"),
672
+ options,
673
+ allowMissingIndex: true,
674
+ }));
675
+ const identityReferenceRewriteFile = resolveRepoPath(identityReferenceRewriteStage.rewrite_file);
676
+ const identityRewrittenRowsFile = Number(identityReferenceRewriteStage.counts?.flow_reference_rewrites ?? 0) > 0
677
+ ? resolveRepoPath(identityReferenceRewriteStage.output_rows_file)
678
+ : rowsFile;
679
+ const unresolvedExchangeExternalizeStage = timeStage("unresolved_exchange_externalization", () => externalizeUnresolvedProcessFlowExchanges({
680
+ datasetType,
681
+ rowsFile: identityRewrittenRowsFile,
682
+ outFile: path.join(outDir, "unresolved-exchange-externalization", `${datasetRowsFileStem(datasetType)}.unresolved-exchanges-externalized.jsonl`),
683
+ outDir: path.join(outDir, "unresolved-exchange-externalization"),
684
+ options,
685
+ }));
686
+ const preCleanupRowsFile = Number(unresolvedExchangeExternalizeStage.counts?.externalized_exchanges ?? 0) > 0
687
+ ? resolveRepoPath(unresolvedExchangeExternalizeStage.output_rows_file)
688
+ : identityRewrittenRowsFile;
689
+ const sourceContactRewriteStage = timeStage("source_contact_rewrites", () => applySourceContactRewrites({
690
+ datasetType,
691
+ rowsFile: preCleanupRowsFile,
692
+ outDir: path.join(outDir, "source-contact-rewrites"),
693
+ options,
694
+ }));
695
+ const sourceContactRowsFile = resolveRepoPath(sourceContactRewriteStage.files?.output_rows || sourceContactRewriteStage.output_rows_file);
696
+ const sourceContactSupportRowsFile = resolveRepoPath(sourceContactRewriteStage.files?.support_rows);
697
+ const sourceContactSupportFinalizeRequested = authoredTypes.includes(datasetType) &&
698
+ fileExists(sourceContactSupportRowsFile) &&
699
+ booleanOption(options.finalizeSourceContactSupport ||
700
+ options.prepareSourceContactSupport ||
701
+ options.autoFinalizeSourceContactSupport);
702
+ // "support" is the mixed-type (auto per-row) finalize; "contact" assumes
703
+ // contact-only rows. Force "support" whenever true sources OR account-local
704
+ // FP/UG (P1a) are in the set so the unit-group/flow-property rows finalize and
705
+ // commit under their own types instead of being treated as contacts.
706
+ const sourceContactSupportFinalizeType = Number(sourceContactRewriteStage.counts?.support_source_rows ?? 0) > 0 ||
707
+ Number(sourceContactRewriteStage.counts?.support_account_local_fp_ug_rows ?? 0) > 0
708
+ ? "support"
709
+ : "contact";
710
+ let sourceContactSupportFinalize = {
711
+ status: sourceContactSupportRowsFile ? "available_not_requested" : "not_required",
712
+ counts: { blockers: 0, write_candidates: 0 },
713
+ files: {
714
+ report: null,
715
+ final_rows: sourceContactSupportRowsFile
716
+ ? repoRelativePath(sourceContactSupportRowsFile)
717
+ : null,
718
+ },
719
+ commit_handoff: { status: "not_requested", blockers: [] },
720
+ blockers: [],
721
+ };
722
+ const finalizeSourceContactSupport = () => runDatasetPostAuthoringFinalize({
723
+ ...options,
724
+ type: sourceContactSupportFinalizeType,
725
+ rowsFile: sourceContactSupportRowsFile,
726
+ outDir: path.join(outDir, "source-contact-support-finalize"),
727
+ cleanedRowsFile: null,
728
+ cleanedRows: null,
729
+ outFile: null,
730
+ out: null,
731
+ skipSourceContactRewrites: true,
732
+ finalizeSourceContactSupport: false,
733
+ prepareSourceContactSupport: false,
734
+ autoFinalizeSourceContactSupport: false,
735
+ // The FP/UG are already in this support rows file; don't re-collect
736
+ // them in the nested finalize (skipSourceContactRewrites already
737
+ // early-returns before the collector, but be explicit).
738
+ mintUnmatchedFpUgSupport: false,
739
+ requireIdentityPreflight: false,
740
+ runIdentityPreflight: false,
741
+ identityPreflightIndex: null,
742
+ identityPreflightRequests: null,
743
+ identityPreflightRequestsIndex: null,
744
+ classificationDecisionApplyReport: null,
745
+ classificationDecisionsApplyReport: null,
746
+ locationDecisionApplyReport: null,
747
+ locationDecisionsApplyReport: null,
748
+ identityDecisionApplyReport: null,
749
+ identityDecisionsApplyReport: null,
750
+ identityDecisionApplyReports: [],
751
+ identityDecisionsApplyReports: [],
752
+ patchCollectReport: null,
753
+ authoringPatchCollectReport: null,
754
+ patchApplyReport: null,
755
+ verifyRemote: false,
756
+ precommitVerifyRemote: false,
757
+ });
758
+ const canonicalSupportRewriteStage = timeStage("canonical_support_rewrites", () => applyCanonicalSupportRewrites({
759
+ datasetType,
760
+ rowsFile: sourceContactRowsFile || preCleanupRowsFile,
761
+ outFile: path.join(outDir, "canonical-support-rewrites", `${datasetRowsFileStem(datasetType)}.canonical-support-rewritten.jsonl`),
762
+ outDir: path.join(outDir, "canonical-support-rewrites"),
763
+ options: {
764
+ ...options,
765
+ prepareAccountLocalSupportCandidates: booleanOption(options.mintUnmatchedFpUgSupport),
766
+ },
767
+ }));
768
+ const canonicalSupportRowsFile = resolveRepoPath(canonicalSupportRewriteStage.files?.output_rows ||
769
+ canonicalSupportRewriteStage.output_rows_file);
770
+ const canonicalSupportReportFile = resolveRepoPath(canonicalSupportRewriteStage.files?.report);
771
+ const canonicalSupportPrewriteBlockers = ensureArray(canonicalSupportRewriteStage.blockers).map((blocker) => ({
772
+ ...blocker,
773
+ stage: "canonical_support_rewrites",
774
+ source: "canonical_support_rewrites",
775
+ severity: blocker.severity || "error",
776
+ }));
777
+ const cleanup = timeStage("curation_cleanup", () => runDatasetCurationCleanup({
778
+ repoRoot,
779
+ options: {
780
+ ...options,
781
+ type: datasetType,
782
+ rowsFile: canonicalSupportRowsFile || preCleanupRowsFile,
783
+ sourceRowsFile: options.sourceRowsFile ||
784
+ options.sourceRows ||
785
+ options.originalSourceRowsFile ||
786
+ options.originalRowsFile,
787
+ outDir: path.join(outDir, "cleanup"),
788
+ outFile: options.cleanedRowsFile || options.cleanedRows || options.outFile,
789
+ },
790
+ }));
791
+ const cleanupReportFile = resolveRepoPath(cleanup.files?.report);
792
+ const cleanedRowsFile = resolveRepoPath(cleanup.files?.cleaned_rows || cleanup.cleaned_rows_file);
793
+ if (cleanup.status !== "completed" || !cleanedRowsFile) {
794
+ const staleManagedArtifacts = [
795
+ ...postCleanupArtifactDirectories
796
+ .map((directory) => path.join(outDir, directory))
797
+ .filter((directory) => fs.existsSync(directory)),
798
+ ...postCleanupArtifactFiles
799
+ .map((file) => path.join(outDir, file))
800
+ .filter((file) => fs.existsSync(file)),
801
+ ];
802
+ const priorStageBlockers = [
803
+ ...ensureArray(identityReferenceRewriteStage.blockers).map((blocker) => ({
804
+ ...blocker,
805
+ stage: "identity_reference_rewrites",
806
+ source: "identity_reference_rewrites",
807
+ severity: blocker.severity || "error",
808
+ })),
809
+ ...ensureArray(unresolvedExchangeExternalizeStage.blockers).map((blocker) => ({
810
+ ...blocker,
811
+ stage: "unresolved_exchange_externalization",
812
+ source: "unresolved_exchange_externalization",
813
+ severity: blocker.severity || "error",
814
+ })),
815
+ ...ensureArray(sourceContactRewriteStage.blockers).map((blocker) => ({
816
+ ...blocker,
817
+ stage: "source_contact_rewrites",
818
+ source: "source_contact_rewrites",
819
+ severity: blocker.severity || "error",
820
+ })),
821
+ ...ensureArray(sourceContactSupportFinalize.blockers).map((blocker) => ({
822
+ ...blocker,
823
+ stage: "source_contact_support_finalize",
824
+ source: "source_contact_support_finalize",
825
+ severity: blocker.severity || "error",
826
+ })),
827
+ ...ensureArray(sourceContactSupportFinalize.commit_handoff?.blockers).map((blocker) => ({
828
+ ...blocker,
829
+ stage: "source_contact_support_finalize",
830
+ source: "source_contact_support_commit_handoff",
831
+ severity: blocker.severity || "error",
832
+ })),
833
+ ...canonicalSupportPrewriteBlockers,
834
+ ];
835
+ const cleanupBlockers = ensureArray(cleanup.blockers).map((blocker) => ({
836
+ ...blocker,
837
+ stage: "curation_cleanup",
838
+ source: "curation_cleanup",
839
+ severity: blocker.severity || "error",
840
+ }));
841
+ const cleanupNotReadyBlocker = {
842
+ code: "curation_cleanup_not_ready",
843
+ stage: "curation_cleanup",
844
+ source: "curation_cleanup",
845
+ severity: "error",
846
+ cleanup_status: cleanup.status ?? null,
847
+ cleanup_report: repoRelativeMaybe(cleanupReportFile),
848
+ action: "Resolve every cleanup blocker and rerun post-authoring finalize before any downstream prewrite stage.",
849
+ };
850
+ const invalidationBlockers = staleManagedArtifacts.length > 0
851
+ ? [
852
+ {
853
+ code: "stale_finalize_artifacts_not_invalidated",
854
+ stage: "curation_cleanup",
855
+ source: "finalize_output_ownership",
856
+ severity: "error",
857
+ paths: staleManagedArtifacts.map((artifact) => repoRelativeMaybe(artifact)),
858
+ action: "Preserve and inspect the stale artifacts manually, then use a new output directory for the repaired rerun.",
859
+ },
860
+ ]
861
+ : [];
862
+ const blockers = [
863
+ ...priorStageBlockers,
864
+ ...cleanupBlockers,
865
+ cleanupNotReadyBlocker,
866
+ ...invalidationBlockers,
867
+ ];
868
+ const stageReports = [
869
+ {
870
+ stage: "identity_reference_rewrites",
871
+ status: identityReferenceRewriteStage.status,
872
+ exit_code: (identityReferenceRewriteStage.blockers ?? []).length > 0 ? 1 : 0,
873
+ command: "foundry.dataset-identity-reference-rewrites-apply",
874
+ args: [],
875
+ stderr: "",
876
+ report_file: null,
877
+ },
878
+ {
879
+ stage: "unresolved_exchange_externalization",
880
+ status: unresolvedExchangeExternalizeStage.status,
881
+ exit_code: 0,
882
+ command: "foundry.externalize-unresolved-process-flow-exchanges",
883
+ args: [],
884
+ stderr: "",
885
+ report_file: resolveRepoPath(unresolvedExchangeExternalizeStage.files?.report),
886
+ },
887
+ {
888
+ stage: "source_contact_rewrites",
889
+ status: sourceContactRewriteStage.status,
890
+ exit_code: 0,
891
+ command: "foundry.dataset-source-contact-rewrites-apply",
892
+ args: [],
893
+ stderr: "",
894
+ report_file: resolveRepoPath(sourceContactRewriteStage.files?.report),
895
+ },
896
+ {
897
+ stage: "canonical_support_rewrites",
898
+ status: canonicalSupportRewriteStage.status,
899
+ exit_code: (canonicalSupportRewriteStage.counts?.blockers ?? 0) > 0 ? 1 : 0,
900
+ command: "foundry.dataset-canonical-support-rewrites-apply",
901
+ args: [],
902
+ stderr: "",
903
+ report_file: canonicalSupportReportFile,
904
+ },
905
+ {
906
+ stage: "curation_cleanup",
907
+ status: cleanup.status,
908
+ exit_code: 1,
909
+ command: "foundry.dataset-curation-cleanup",
910
+ args: [],
911
+ stderr: "",
912
+ report_file: cleanupReportFile,
913
+ },
914
+ ];
915
+ const timingsByStage = new Map(finalizeTimings.map((timing) => [timing.stage, timing]));
916
+ const reportPath = path.join(outDir, "dataset-post-authoring-finalize-report.json");
917
+ const report = {
918
+ schema_version: 1,
919
+ started_at_utc: finalizeStartedAtUtc,
920
+ generated_at_utc: nowIso(),
921
+ status: "blocked",
922
+ dataset_type: datasetType,
923
+ profile: String(options.profile || "generic"),
924
+ rows_file: repoRelativePath(rowsFile),
925
+ pre_cleanup_rows_file: repoRelativeMaybe(preCleanupRowsFile),
926
+ source_contact_rows_file: repoRelativeMaybe(sourceContactRowsFile),
927
+ canonical_support_rows_file: repoRelativeMaybe(canonicalSupportRowsFile),
928
+ final_rows_file: null,
929
+ remote_write_mode: "read-only",
930
+ policy: {
931
+ purpose: "Finalize AI-authored or support TIDAS rows into exact-scope prewrite evidence without committing to the database.",
932
+ cleanup_gate: "Curation cleanup must complete with a concrete cleaned rows artifact before identity preflight, queue, schema, QA, dry-run, mutation manifest, or handoff stages may run.",
933
+ },
934
+ counts: {
935
+ finalize_duration_ms: Date.now() - finalizeStartedAtMs,
936
+ finalize_substage_count: finalizeTimings.length,
937
+ blockers: blockers.length,
938
+ mutation_manifest_blockers: 0,
939
+ prewrite_gate_blockers: blockers.length,
940
+ canonical_support_blockers: canonicalSupportRewriteStage.counts?.blockers ?? 0,
941
+ write_candidates: 0,
942
+ },
943
+ blockers,
944
+ stages: stageReports.map((stage) => compactStageReportWithTiming(stage, timingsByStage)),
945
+ timings: finalizeTimings,
946
+ files: {
947
+ source_contact_rewrite_report: sourceContactRewriteStage.files?.report ?? null,
948
+ source_contact_rewritten_rows: sourceContactRewriteStage.files?.output_rows ?? null,
949
+ cleanup_report: repoRelativeMaybe(cleanupReportFile),
950
+ canonical_support_rewrite_report: repoRelativeMaybe(canonicalSupportReportFile),
951
+ canonical_support_rewritten_rows: repoRelativeMaybe(canonicalSupportRowsFile),
952
+ final_rows: null,
953
+ identity_preflight_run_report: null,
954
+ identity_preflight_index: null,
955
+ curation_queue_dir: null,
956
+ curation_queue_report: null,
957
+ schema_report: null,
958
+ qa_report: null,
959
+ location_audit_report: null,
960
+ curation_gate_report: null,
961
+ dry_run_report: null,
962
+ remote_verify_report: null,
963
+ mutation_manifest: null,
964
+ commit_handoff_plan: null,
965
+ },
966
+ commit_handoff: {
967
+ status: "not_requested",
968
+ command: null,
969
+ post_write_verify_command: null,
970
+ blockers,
971
+ },
972
+ };
973
+ writeJson(reportPath, report);
974
+ const importLedger = writeFinalizeImportLedger
975
+ ? timeStage("import_ledger", () => writeFinalizeImportLedger({
976
+ report,
977
+ reportPath,
978
+ ledgerDir: resolveRepoPath(options.ledgerDir || options.importLedgerDir || path.join(outDir, "import-ledger")),
979
+ }))
980
+ : { status: "skipped", files: {}, counts: { entries_written: 0 } };
981
+ const finalReport = {
982
+ ...report,
983
+ counts: {
984
+ ...report.counts,
985
+ finalize_duration_ms: Date.now() - finalizeStartedAtMs,
986
+ finalize_substage_count: finalizeTimings.length,
987
+ import_ledger_entries: importLedger.counts?.entries_written ?? 0,
988
+ },
989
+ timings: finalizeTimings,
990
+ files: {
991
+ ...report.files,
992
+ import_ledger: importLedger.files ?? {},
993
+ },
994
+ };
995
+ writeJson(reportPath, finalReport);
996
+ return {
997
+ ...finalReport,
998
+ files: {
999
+ ...finalReport.files,
1000
+ report: repoRelativePath(reportPath),
1001
+ },
1002
+ };
1003
+ }
1004
+ if (sourceContactSupportFinalizeRequested) {
1005
+ sourceContactSupportFinalize = timeStage("source_contact_support_finalize", finalizeSourceContactSupport);
1006
+ }
1007
+ const identityPreflightRunStage = timeStage("identity_preflight_run", () => runFinalizeIdentityPreflightStage({
1008
+ rowsFile: cleanedRowsFile,
1009
+ outDir,
1010
+ options: {
1011
+ ...options,
1012
+ type: datasetType,
1013
+ },
1014
+ }));
1015
+ const curationQueueStage = timeStage("curation_queue", () => runFinalizeAutoCurationQueue({
1016
+ datasetType,
1017
+ rowsFile,
1018
+ cleanedRowsFile,
1019
+ outDir,
1020
+ options,
1021
+ fullContextRequirement,
1022
+ identityReferenceRewriteStage,
1023
+ }));
1024
+ const curationQueueDir = curationQueueStage.queue_dir || resolveRepoPath(options.queueDir || options.curationQueueDir);
1025
+ const schemaOutDir = path.join(outDir, "schema", datasetType);
1026
+ const schemaStage = timeStage("schema_validate", () => {
1027
+ const stage = runTidasRowsValidation({
1028
+ rowsFile: cleanedRowsFile,
1029
+ type: datasetType === "support" ? "auto" : datasetType,
1030
+ outDir: schemaOutDir,
1031
+ });
1032
+ return {
1033
+ stage: "schema_validate",
1034
+ command: stage.executable,
1035
+ executable: stage.executable,
1036
+ args: stage.args,
1037
+ exit_code: stage.exit_code,
1038
+ stderr: stage.stderr,
1039
+ report: stage.report,
1040
+ report_file: stage.report_file,
1041
+ rust_report: stage.rust_report,
1042
+ binary_version: stage.binary_version,
1043
+ };
1044
+ });
1045
+ const qaOutDir = path.join(outDir, "qa", datasetType);
1046
+ const qaStage = timeStage(`${datasetType}_qa`, () => {
1047
+ const stage = requiresDeterministicQa
1048
+ ? runTiangongJsonStage(`${datasetType}_qa`, [
1049
+ "qa",
1050
+ datasetType,
1051
+ "--rows-file",
1052
+ cleanedRowsFile,
1053
+ "--out-dir",
1054
+ qaOutDir,
1055
+ "--json",
1056
+ ])
1057
+ : {
1058
+ stage: `${datasetType}_qa`,
1059
+ status: "not_required_for_support_rows",
1060
+ exit_code: 0,
1061
+ command: "skipped",
1062
+ args: [],
1063
+ stderr: "Support rows do not have a deterministic qa <type> gate.",
1064
+ report: { status: "not_required_for_support_rows" },
1065
+ report_file: null,
1066
+ };
1067
+ if (requiresDeterministicQa) {
1068
+ stage.report_file = reportFileFromCliStage(stage, ["files.report"], path.join(qaOutDir, datasetType === "flow"
1069
+ ? "flow_qa_report.json"
1070
+ : datasetType === "lifecyclemodel"
1071
+ ? "lifecyclemodel_qa_report.json"
1072
+ : "process-qa-report.json"));
1073
+ }
1074
+ else {
1075
+ const supportQaOutDir = path.join(outDir, "qa", datasetType);
1076
+ const supportQaReportFile = path.join(supportQaOutDir, `${datasetType}-qa-report.json`);
1077
+ const supportQaReport = {
1078
+ schema_version: 1,
1079
+ status: "not_required_for_support_rows",
1080
+ dataset_type: datasetType,
1081
+ rows_file: repoRelativePath(cleanedRowsFile),
1082
+ findings: [],
1083
+ blockers: [],
1084
+ counts: {
1085
+ blockers: 0,
1086
+ findings: 0,
1087
+ },
1088
+ files: {
1089
+ report: repoRelativePath(supportQaReportFile),
1090
+ },
1091
+ };
1092
+ writeJson(supportQaReportFile, supportQaReport);
1093
+ stage.report = supportQaReport;
1094
+ stage.report_file = supportQaReportFile;
1095
+ }
1096
+ return stage;
1097
+ });
1098
+ const locationAuditOutDir = path.join(outDir, "location-audit", datasetType);
1099
+ const locationAuditStage = timeStage("location_audit", () => {
1100
+ const stage = runTiangongJsonStage("location_audit", [
1101
+ "dataset",
1102
+ "classification",
1103
+ "audit",
1104
+ "--type",
1105
+ "location",
1106
+ "--input",
1107
+ cleanedRowsFile,
1108
+ "--out-dir",
1109
+ locationAuditOutDir,
1110
+ "--json",
1111
+ ]);
1112
+ stage.report_file = reportFileFromCliStage(stage, ["files.report"], path.join(locationAuditOutDir, "outputs", "location-audit-report.json"));
1113
+ return stage;
1114
+ });
1115
+ const locationAuditBlockers = blockersFromLocationAuditStage(locationAuditStage);
1116
+ const curationGate = timeStage("post_authoring_curation_gate", () => requiresCurationGate
1117
+ ? runDatasetCurationGate({
1118
+ repoRoot,
1119
+ options: {
1120
+ ...options,
1121
+ type: datasetType,
1122
+ rowsFile: cleanedRowsFile,
1123
+ schemaReport: schemaStage.report_file,
1124
+ qaReport: qaStage.report_file,
1125
+ outDir: path.join(outDir, "curation-gate"),
1126
+ requireIdentityPreflight: identityPreflightRequired,
1127
+ identityPreflightIndex: identityPreflightRunStage.index_file ||
1128
+ options.identityPreflightIndex ||
1129
+ options.identityPreflightRequests ||
1130
+ options.identityPreflightRequestsIndex,
1131
+ identityReferenceRewrites: identityReferenceRewriteFile,
1132
+ classificationDecisionApplyReport: options.classificationDecisionApplyReport ||
1133
+ options.classificationDecisionsApplyReport,
1134
+ unresolvedExchangeExternalizationReport: unresolvedExchangeExternalizeStage.files?.report,
1135
+ sourceContactRewriteReport: sourceContactRewriteStage.files?.report,
1136
+ canonicalSupportRewriteReport: canonicalSupportReportFile,
1137
+ cleanupReport: cleanupReportFile,
1138
+ identityDecisionApplyReport: options.identityDecisionApplyReport || options.identityDecisionsApplyReport,
1139
+ queueDir: curationQueueDir,
1140
+ requireQueueContext: booleanOption(options.requireQueueContext || options.requireCurationQueueContext) ||
1141
+ (Boolean(fullContextRequirement) && datasetType === "process"),
1142
+ },
1143
+ })
1144
+ : {
1145
+ status: "not_required_for_support_rows",
1146
+ files: {},
1147
+ });
1148
+ const curationGateReportFile = requiresCurationGate
1149
+ ? resolveRepoPath(curationGate.files?.report)
1150
+ : null;
1151
+ const prewriteGateBlockers = postAuthoringPrewriteGateBlockers({
1152
+ schemaStage,
1153
+ qaStage,
1154
+ locationAuditBlockers,
1155
+ curationGate,
1156
+ curationGateReportFile,
1157
+ requireDeterministicQa: requiresDeterministicQa,
1158
+ requireCurationGate: requiresCurationGate,
1159
+ }).concat(canonicalSupportPrewriteBlockers);
1160
+ const prewriteGateReady = prewriteGateBlockers.length === 0;
1161
+ const dryRunOutDir = path.join(outDir, "dry-run", datasetType === "support" || supportTypes.includes(datasetType)
1162
+ ? `${datasetType}-save-draft`
1163
+ : datasetType === "flow"
1164
+ ? "flow-publish-version"
1165
+ : datasetType === "lifecyclemodel"
1166
+ ? "lifecyclemodel-save-draft"
1167
+ : "process-save-draft");
1168
+ const dryRunArgs = (() => {
1169
+ if (datasetType === "support" || supportTypes.includes(datasetType)) {
1170
+ const supportDryRunArgs = [
1171
+ "dataset",
1172
+ "save-draft",
1173
+ "--type",
1174
+ datasetType === "support" ? "auto" : datasetType,
1175
+ "--input",
1176
+ cleanedRowsFile,
1177
+ "--out-dir",
1178
+ dryRunOutDir,
1179
+ "--dry-run",
1180
+ "--json",
1181
+ ];
1182
+ // Account-local FP/UG (P1a) in the support set are reference-only types
1183
+ // and need the override to clear the save-draft governance gate. No-op for
1184
+ // contact/source-only support sets (e.g. BAFU), so it is gated on the
1185
+ // profile override rather than a USLCI-specific flag.
1186
+ if (booleanOption(options.mintUnmatchedFpUgSupport) ||
1187
+ taskAuthorizationAllows(taskAuthorization, "flowproperty_write") ||
1188
+ taskAuthorizationAllows(taskAuthorization, "unitgroup_write")) {
1189
+ supportDryRunArgs.push("--allow-account-local-support");
1190
+ }
1191
+ return supportDryRunArgs;
1192
+ }
1193
+ if (datasetType === "flow") {
1194
+ return [
1195
+ "flow",
1196
+ "publish-version",
1197
+ "--input-file",
1198
+ cleanedRowsFile,
1199
+ "--out-dir",
1200
+ dryRunOutDir,
1201
+ "--dry-run",
1202
+ "--json",
1203
+ ];
1204
+ }
1205
+ if (datasetType === "lifecyclemodel") {
1206
+ return [
1207
+ "lifecyclemodel",
1208
+ "save-draft",
1209
+ "--input",
1210
+ cleanedRowsFile,
1211
+ "--out-dir",
1212
+ dryRunOutDir,
1213
+ "--dry-run",
1214
+ "--json",
1215
+ ];
1216
+ }
1217
+ return [
1218
+ "process",
1219
+ "save-draft",
1220
+ "--input",
1221
+ cleanedRowsFile,
1222
+ "--out-dir",
1223
+ dryRunOutDir,
1224
+ "--dry-run",
1225
+ "--json",
1226
+ ];
1227
+ })();
1228
+ if (datasetType === "flow") {
1229
+ appendOption(dryRunArgs, "--target-user-id", options.remoteTargetUserId || options.targetUserId);
1230
+ }
1231
+ const dryRunStageName = datasetType === "support" || supportTypes.includes(datasetType)
1232
+ ? `${datasetType}_save_draft_dry_run`
1233
+ : datasetType === "flow"
1234
+ ? "flow_publish_version_dry_run"
1235
+ : datasetType === "lifecyclemodel"
1236
+ ? "lifecyclemodel_save_draft_dry_run"
1237
+ : "process_save_draft_dry_run";
1238
+ const dryRunStage = timeStage(dryRunStageName, () => {
1239
+ const stage = prewriteGateReady
1240
+ ? runTiangongJsonStage(dryRunStageName, dryRunArgs)
1241
+ : skippedPrewriteStage(dryRunStageName, "Skipped because schema, QA, canonical support, location audit, or post-authoring curation gate is not ready.");
1242
+ if (prewriteGateReady) {
1243
+ stage.report_file = reportFileFromCliStage(stage, datasetType === "flow" ? ["files.report"] : ["files.summary_json"], datasetType === "support" || supportTypes.includes(datasetType)
1244
+ ? path.join(dryRunOutDir, "outputs", "dataset-save-draft", "summary.json")
1245
+ : datasetType === "flow"
1246
+ ? path.join(dryRunOutDir, "flows_tidas_sdk_plus_classification_mcp_sync_report.json")
1247
+ : datasetType === "lifecyclemodel"
1248
+ ? path.join(dryRunOutDir, "outputs", "save-draft-bundle", "summary.json")
1249
+ : path.join(dryRunOutDir, "outputs", "save-draft-rpc", "summary.json"));
1250
+ }
1251
+ return stage;
1252
+ });
1253
+ let remoteVerifyStage = null;
1254
+ let remoteVerifyReportFile = null;
1255
+ if (booleanOption(options.verifyRemote || options.precommitVerifyRemote)) {
1256
+ const remoteOutDir = path.join(outDir, "precommit-verify-remote");
1257
+ const remoteArgs = [
1258
+ "dataset",
1259
+ "verify-remote",
1260
+ "--input",
1261
+ cleanedRowsFile,
1262
+ "--out-dir",
1263
+ remoteOutDir,
1264
+ "--root-policy",
1265
+ String(options.remoteRootPolicy || options.rootPolicy || "candidate"),
1266
+ "--json",
1267
+ ];
1268
+ if (booleanOption(options.compareRootPayload || options.remoteCompareRootPayload)) {
1269
+ remoteArgs.push("--compare-root-payload");
1270
+ }
1271
+ appendOption(remoteArgs, "--target-user-id", options.remoteTargetUserId || options.targetUserId);
1272
+ appendOption(remoteArgs, "--state-code", options.remoteStateCode || options.stateCode || "0");
1273
+ remoteVerifyStage = timeStage("remote_verify_precommit", () => {
1274
+ const stage = prewriteGateReady
1275
+ ? runTiangongJsonStage("remote_verify_precommit", remoteArgs)
1276
+ : skippedPrewriteStage("remote_verify_precommit", "Skipped because schema, QA, canonical support, location audit, or post-authoring curation gate is not ready.");
1277
+ if (prewriteGateReady) {
1278
+ stage.report_file = reportFileFromCliStage(stage, ["files.report"], path.join(remoteOutDir, "outputs", "remote-verification-report.json"));
1279
+ remoteVerifyReportFile = stage.report_file;
1280
+ }
1281
+ return stage;
1282
+ });
1283
+ }
1284
+ const identityDecisionApplyReportOptions = unique([
1285
+ ...normalizedList(options.identityDecisionApplyReport),
1286
+ ...normalizedList(options.identityDecisionsApplyReport),
1287
+ ...normalizedList(options.identityDecisionApplyReports),
1288
+ ...normalizedList(options.identityDecisionsApplyReports),
1289
+ ]);
1290
+ const identityDecisionApplyReportFiles = identityDecisionApplyReportOptions
1291
+ .map(resolveRepoPath)
1292
+ .filter((file) => Boolean(file) && fileExists(file));
1293
+ const verifiedReferenceLedgerDir = resolveRepoPath(options.ledgerDir || options.importLedgerDir || path.join(outDir, "import-ledger"));
1294
+ const verifiedReferenceLedgerFiles = verifiedReferenceLedgerFilesForDir(verifiedReferenceLedgerDir);
1295
+ const mutationManifest = timeStage("mutation_manifest", () => runDatasetMutationManifest({
1296
+ repoRoot,
1297
+ options: {
1298
+ ...options,
1299
+ type: datasetType,
1300
+ rowsFile: cleanedRowsFile,
1301
+ referenceRowsFile: identityReferenceRewriteStage.reference_rows_file ||
1302
+ options.referenceRowsFile ||
1303
+ options.referenceRows ||
1304
+ options.reuseRowsFile,
1305
+ schemaReport: schemaStage.report_file,
1306
+ qaReport: requiresDeterministicQa ? qaStage.report_file : null,
1307
+ curationGateReport: requiresCurationGate ? curationGateReportFile : null,
1308
+ cleanupReport: cleanupReportFile,
1309
+ dryRunReport: prewriteGateReady ? dryRunStage.report_file : null,
1310
+ remoteVerifyReport: remoteVerifyReportFile,
1311
+ unresolvedExchangeExternalizationReport: unresolvedExchangeExternalizeStage.files?.report,
1312
+ sourceContactRewriteReport: sourceContactRewriteStage.files?.report,
1313
+ canonicalSupportRewriteReport: canonicalSupportReportFile,
1314
+ classificationDecisionApplyReport: options.classificationDecisionApplyReport || options.classificationDecisionsApplyReport,
1315
+ locationDecisionApplyReport: options.locationDecisionApplyReport || options.locationDecisionsApplyReport,
1316
+ identityDecisionApplyReport: options.identityDecisionApplyReport || options.identityDecisionsApplyReport,
1317
+ identityDecisionApplyReports: identityDecisionApplyReportOptions,
1318
+ identityReferenceRewriteStatus: identityReferenceRewriteStage.status,
1319
+ identityReferenceRewriteInputRows: rowsFile,
1320
+ identityReferenceRewriteOutputRows: identityReferenceRewriteStage.output_rows_file,
1321
+ sourceReferenceRewrites: sourceContactRewriteStage.files?.source_reference_rewrites ||
1322
+ sourceReferenceRewritesFileForRowsFile(rowsFile, options),
1323
+ identityReferenceRewrites: identityReferenceRewriteFile,
1324
+ verifiedReferenceLedgers: verifiedReferenceLedgerFiles,
1325
+ outDir: path.join(outDir, "mutation-manifest"),
1326
+ requireCurationGate: requiresCurationGate,
1327
+ },
1328
+ }));
1329
+ const patchApplyReportFile = resolveRepoPath(options.patchApplyReport);
1330
+ const patchCollectReportFile = resolveRepoPath(options.patchCollectReport || options.authoringPatchCollectReport);
1331
+ const classificationDecisionApplyReportFile = resolveRepoPath(options.classificationDecisionApplyReport || options.classificationDecisionsApplyReport);
1332
+ const locationDecisionApplyReportFile = resolveRepoPath(options.locationDecisionApplyReport || options.locationDecisionsApplyReport);
1333
+ const sourceContactSupportFinalizeRawBlockers = Number(sourceContactSupportFinalize.counts?.blockers ?? 0);
1334
+ const sourceContactSupportCommitHandoffRawBlockers = ensureArray(sourceContactSupportFinalize.commit_handoff?.blockers).length;
1335
+ const sourceContactSupportFinalizeAdvisory = Boolean(sourceContactSupportFinalizeRequested &&
1336
+ sourceContactSupportFinalize.status === "blocked" &&
1337
+ mutationManifest.status === "ready_for_remote_write" &&
1338
+ Number(mutationManifest.counts?.blockers ?? 0) === 0);
1339
+ const sourceContactSupportFinalizeStatus = sourceContactSupportFinalizeAdvisory
1340
+ ? "advisory_blocked_top_level_remote_verified"
1341
+ : sourceContactSupportFinalize.status;
1342
+ const sourceContactSupportFinalizeBlockers = sourceContactSupportFinalizeAdvisory
1343
+ ? 0
1344
+ : sourceContactSupportFinalizeRawBlockers;
1345
+ const sourceContactSupportCommitHandoffBlockers = sourceContactSupportFinalizeAdvisory
1346
+ ? 0
1347
+ : sourceContactSupportCommitHandoffRawBlockers;
1348
+ const stageReports = [
1349
+ {
1350
+ stage: "identity_preflight_run",
1351
+ status: identityPreflightRunStage.status,
1352
+ exit_code: [
1353
+ "not_requested",
1354
+ "planned",
1355
+ "completed",
1356
+ "completed_with_identity_findings",
1357
+ ].includes(identityPreflightRunStage.status ?? "")
1358
+ ? 0
1359
+ : 1,
1360
+ command: identityPreflightRunStage.status === "not_requested"
1361
+ ? "skipped"
1362
+ : "foundry.dataset-identity-preflight-run",
1363
+ args: [],
1364
+ stderr: "",
1365
+ report_file: identityPreflightRunStage.report_file,
1366
+ },
1367
+ {
1368
+ stage: "identity_reference_rewrites",
1369
+ status: identityReferenceRewriteStage.status,
1370
+ exit_code: (identityReferenceRewriteStage.blockers ?? []).length > 0 ? 1 : 0,
1371
+ command: "foundry.dataset-identity-reference-rewrites-apply",
1372
+ args: [],
1373
+ stderr: "",
1374
+ report_file: null,
1375
+ },
1376
+ {
1377
+ stage: "unresolved_exchange_externalization",
1378
+ status: unresolvedExchangeExternalizeStage.status,
1379
+ exit_code: 0,
1380
+ command: "foundry.externalize-unresolved-process-flow-exchanges",
1381
+ args: [],
1382
+ stderr: "",
1383
+ report_file: resolveRepoPath(unresolvedExchangeExternalizeStage.files?.report),
1384
+ },
1385
+ {
1386
+ stage: "source_contact_rewrites",
1387
+ status: sourceContactRewriteStage.status,
1388
+ exit_code: 0,
1389
+ command: "foundry.dataset-source-contact-rewrites-apply",
1390
+ args: [],
1391
+ stderr: "",
1392
+ report_file: resolveRepoPath(sourceContactRewriteStage.files?.report),
1393
+ },
1394
+ {
1395
+ stage: "source_contact_support_finalize",
1396
+ status: sourceContactSupportFinalizeStatus,
1397
+ exit_code: sourceContactSupportFinalizeBlockers > 0 ? 1 : 0,
1398
+ command: sourceContactSupportFinalizeRequested
1399
+ ? "foundry.dataset-post-authoring-finalize"
1400
+ : "skipped",
1401
+ args: [],
1402
+ stderr: "",
1403
+ report_file: resolveRepoPath(sourceContactSupportFinalize.files?.report),
1404
+ },
1405
+ {
1406
+ stage: "canonical_support_rewrites",
1407
+ status: canonicalSupportRewriteStage.status,
1408
+ exit_code: (canonicalSupportRewriteStage.counts?.blockers ?? 0) > 0 ? 1 : 0,
1409
+ command: "foundry.dataset-canonical-support-rewrites-apply",
1410
+ args: [],
1411
+ stderr: "",
1412
+ report_file: canonicalSupportReportFile,
1413
+ },
1414
+ {
1415
+ stage: "curation_cleanup",
1416
+ status: cleanup.status,
1417
+ exit_code: 0,
1418
+ command: "foundry.dataset-curation-cleanup",
1419
+ args: [],
1420
+ stderr: "",
1421
+ report_file: cleanupReportFile,
1422
+ },
1423
+ {
1424
+ stage: "curation_queue",
1425
+ status: curationQueueStage.status,
1426
+ exit_code: curationQueueStage.status === "not_required" ||
1427
+ curationQueueStage.status === "provided" ||
1428
+ curationQueueStage.status === "ready"
1429
+ ? 0
1430
+ : 1,
1431
+ command: curationQueueStage.status === "not_required" || curationQueueStage.status === "provided"
1432
+ ? "skipped"
1433
+ : "foundry.dataset-curation-queue-build",
1434
+ args: [],
1435
+ stderr: curationQueueStage.report?.foundry_wrapper?.stderr || "",
1436
+ report_file: curationQueueStage.report_file,
1437
+ },
1438
+ schemaStage,
1439
+ qaStage,
1440
+ locationAuditStage,
1441
+ {
1442
+ stage: "post_authoring_curation_gate",
1443
+ status: curationGate.status,
1444
+ exit_code: !requiresCurationGate ||
1445
+ ["ready", "ready_with_profile_waivers"].includes(curationGate.status ?? "")
1446
+ ? 0
1447
+ : 1,
1448
+ command: "foundry.dataset-curation-gate",
1449
+ args: [],
1450
+ stderr: "",
1451
+ report_file: curationGateReportFile,
1452
+ },
1453
+ dryRunStage,
1454
+ ...(remoteVerifyStage ? [remoteVerifyStage] : []),
1455
+ {
1456
+ stage: "mutation_manifest",
1457
+ status: mutationManifest.status,
1458
+ exit_code: ["ready_for_remote_write", "ready_reference_only"].includes(mutationManifest.status ?? "")
1459
+ ? 0
1460
+ : 1,
1461
+ command: "foundry.dataset-mutation-manifest",
1462
+ args: [],
1463
+ stderr: "",
1464
+ report_file: resolveRepoPath(mutationManifest.files?.report),
1465
+ },
1466
+ ];
1467
+ const timingsByStage = new Map(finalizeTimings.map((timing) => [timing.stage, timing]));
1468
+ const mutationBlockerCount = Number(mutationManifest.counts?.blockers ?? 0);
1469
+ const mutationManifestBlockers = [];
1470
+ const seenMutationBlockers = new Set();
1471
+ const addMutationBlocker = (blocker, extra = {}) => {
1472
+ if (!blocker || typeof blocker !== "object")
1473
+ return;
1474
+ const normalized = {
1475
+ ...blocker,
1476
+ stage: blocker.stage || "mutation_manifest",
1477
+ source: "mutation_manifest",
1478
+ ...extra,
1479
+ };
1480
+ const key = JSON.stringify([
1481
+ normalized.code,
1482
+ normalized.stage,
1483
+ normalized.row_index,
1484
+ normalized.table,
1485
+ normalized.reference_id,
1486
+ normalized.reference_version,
1487
+ normalized.path,
1488
+ ]);
1489
+ if (seenMutationBlockers.has(key))
1490
+ return;
1491
+ seenMutationBlockers.add(key);
1492
+ mutationManifestBlockers.push(normalized);
1493
+ };
1494
+ for (const blocker of ensureArray(mutationManifest.evidence?.scope_blockers)) {
1495
+ addMutationBlocker(blocker);
1496
+ }
1497
+ for (const item of ensureArray(mutationManifest.items)) {
1498
+ for (const blocker of ensureArray(item?.blockers)) {
1499
+ addMutationBlocker(blocker, {
1500
+ dataset_type: item?.dataset_type ?? null,
1501
+ entity_id: item?.entity_id ?? null,
1502
+ version: item?.version ?? null,
1503
+ });
1504
+ }
1505
+ }
1506
+ const blockerCount = mutationBlockerCount + prewriteGateBlockers.length;
1507
+ const status = prewriteGateBlockers.length > 0
1508
+ ? "blocked"
1509
+ : mutationManifest.status === "ready_for_remote_write"
1510
+ ? "ready_for_remote_write"
1511
+ : mutationBlockerCount > 0
1512
+ ? "blocked"
1513
+ : mutationManifest.status;
1514
+ const report = {
1515
+ schema_version: 1,
1516
+ started_at_utc: finalizeStartedAtUtc,
1517
+ generated_at_utc: nowIso(),
1518
+ status,
1519
+ dataset_type: datasetType,
1520
+ profile: mutationManifest.profile || String(options.profile || "generic"),
1521
+ rows_file: repoRelativePath(rowsFile),
1522
+ pre_cleanup_rows_file: repoRelativeMaybe(preCleanupRowsFile),
1523
+ source_contact_rows_file: repoRelativeMaybe(sourceContactRowsFile),
1524
+ canonical_support_rows_file: repoRelativeMaybe(canonicalSupportRowsFile),
1525
+ final_rows_file: repoRelativeMaybe(cleanedRowsFile),
1526
+ remote_write_mode: "read-only",
1527
+ policy: {
1528
+ purpose: "Finalize AI-authored or support TIDAS rows into exact-scope prewrite evidence without committing to the database.",
1529
+ commit_boundary: "A later explicit CLI commit command is required after this report and mutation manifest are ready.",
1530
+ required_multilang_english_before_write: true,
1531
+ preserve_source_language_variants: true,
1532
+ full_context_ai_patch_evidence: "When the active profile requires full-context AI completion, mutation manifest must prove deterministic AI semantic evidence: classification/location decision apply evidence for queued decisions, or patch collect/apply evidence with authoring package hash, closed action items, resolution.mode, and resolution.used_context_kinds for field patches.",
1533
+ identity_preflight_gate: identityPreflightRequired
1534
+ ? "Process/flow full-context profiles require completed CLI identity-preflight evidence from flow_hybrid_search/process_hybrid_search before post-authoring dry-run or remote write planning."
1535
+ : "Not required for this dataset type/profile unless --require-identity-preflight is provided.",
1536
+ location_code_audit: "Final rows must pass tiangong-lca dataset classification audit --type location against tidas_locations_category.json before remote write.",
1537
+ },
1538
+ counts: {
1539
+ finalize_duration_ms: Date.now() - finalizeStartedAtMs,
1540
+ finalize_substage_count: finalizeTimings.length,
1541
+ blockers: blockerCount,
1542
+ mutation_manifest_blockers: mutationBlockerCount,
1543
+ prewrite_gate_blockers: prewriteGateBlockers.length,
1544
+ canonical_support_blockers: canonicalSupportRewriteStage.counts?.blockers ?? 0,
1545
+ source_contact_rewrite_input_rows: sourceContactRewriteStage.counts?.input_rows ?? null,
1546
+ source_contact_rewrite_output_rows: sourceContactRewriteStage.counts?.output_rows ?? null,
1547
+ source_contact_reference_rewrites: sourceContactRewriteStage.counts?.contact_reference_rewrites ?? 0,
1548
+ source_contact_source_reference_rewrites: sourceContactRewriteStage.counts?.source_reference_rewrites ?? 0,
1549
+ source_contact_support_rows: sourceContactRewriteStage.counts?.support_rows ?? 0,
1550
+ source_contact_support_finalize_status: sourceContactSupportFinalizeStatus,
1551
+ source_contact_support_finalize_raw_status: sourceContactSupportFinalize.status,
1552
+ source_contact_support_finalize_advisory: sourceContactSupportFinalizeAdvisory,
1553
+ source_contact_support_finalize_write_candidates: sourceContactSupportFinalize.counts?.write_candidates ?? 0,
1554
+ source_contact_support_finalize_blockers: sourceContactSupportFinalizeBlockers,
1555
+ source_contact_support_finalize_raw_blockers: sourceContactSupportFinalizeRawBlockers,
1556
+ source_contact_support_commit_handoff_blockers: sourceContactSupportCommitHandoffBlockers,
1557
+ source_contact_support_commit_handoff_raw_blockers: sourceContactSupportCommitHandoffRawBlockers,
1558
+ canonical_support_input_rows: canonicalSupportRewriteStage.counts?.input_rows ?? null,
1559
+ canonical_support_output_rows: canonicalSupportRewriteStage.counts?.output_rows ?? null,
1560
+ canonical_support_deferred_rows: canonicalSupportRewriteStage.counts?.deferred_rows ?? 0,
1561
+ canonical_support_deferred_blockers: canonicalSupportRewriteStage.counts?.deferred_blockers ?? 0,
1562
+ canonical_flow_property_reference_rewrites: canonicalSupportRewriteStage.counts?.canonical_flow_property_reference_rewrites ?? 0,
1563
+ canonical_unit_group_reference_proofs: canonicalSupportRewriteStage.counts?.canonical_unit_group_reference_proofs ?? 0,
1564
+ full_context_ai_completion_required: Boolean(fullContextRequirement),
1565
+ identity_preflight_required: identityPreflightRequired,
1566
+ identity_preflight_run_selected: identityPreflightRunStage.report?.counts?.selected_rows ?? 0,
1567
+ identity_preflight_run_completed: identityPreflightRunStage.report?.counts?.completed ?? 0,
1568
+ identity_preflight_run_skipped_existing: identityPreflightRunStage.report?.counts?.skipped_existing_report ?? 0,
1569
+ identity_preflight_run_failed: identityPreflightRunStage.report?.counts?.failed ?? 0,
1570
+ identity_preflight_refresh_required: Boolean(identityPreflightRunStage.refresh_required),
1571
+ identity_preflight_refresh_forced: Boolean(identityPreflightRunStage.refresh_forced),
1572
+ identity_preflight_refresh_reason: identityPreflightRunStage.refresh_plan?.reason ?? null,
1573
+ identity_preflight_refresh_force_skipped_exact: Boolean(identityPreflightRunStage.refresh_force_skipped_exact),
1574
+ identity_preflight_refreshed_current_rows: identityPreflightRunStage.refresh_report?.counts?.request_rows ?? 0,
1575
+ identity_preflight_merge_replaced_rows: identityPreflightRunStage.merge_report?.counts?.replaced_rows ?? 0,
1576
+ location_audit_blockers: locationAuditBlockers.length,
1577
+ location_code_targets: locationAuditStage.report?.counts?.location_targets ?? 0,
1578
+ location_code_invalid: locationAuditStage.report?.counts?.invalid ?? 0,
1579
+ write_candidates: mutationManifest.counts?.write_candidates ?? 0,
1580
+ ai_patch_evidence_entries: mutationManifest.counts?.ai_patch_evidence_entries ?? 0,
1581
+ ai_classification_decision_entries: mutationManifest.counts?.ai_classification_decision_entries ?? 0,
1582
+ ai_location_decision_entries: mutationManifest.counts?.ai_location_decision_entries ?? 0,
1583
+ ai_identity_decision_entries: mutationManifest.counts?.ai_identity_decision_entries ?? 0,
1584
+ source_contact_rewrite_semantic_evidence_entries: mutationManifest.counts?.source_contact_rewrite_semantic_evidence_entries ?? 0,
1585
+ ai_semantic_evidence_entries: (Number(mutationManifest.counts?.ai_patch_evidence_entries ?? 0) || 0) +
1586
+ (Number(mutationManifest.counts?.ai_classification_decision_entries ?? 0) || 0) +
1587
+ (Number(mutationManifest.counts?.ai_location_decision_entries ?? 0) || 0) +
1588
+ (Number(mutationManifest.counts?.ai_identity_decision_entries ?? 0) || 0) +
1589
+ (Number(mutationManifest.counts?.source_contact_rewrite_semantic_evidence_entries ?? 0) ||
1590
+ 0),
1591
+ unresolved_trace_entries: mutationManifest.counts?.unresolved_trace_entries ?? 0,
1592
+ unresolved_exchange_externalized: unresolvedExchangeExternalizeStage.counts?.externalized_exchanges ?? 0,
1593
+ blocked_flow_dependency_externalized: unresolvedExchangeExternalizeStage.counts?.blocked_flow_dependency_externalized ?? 0,
1594
+ source_exchange_completeness_entries: mutationManifest.counts?.source_exchange_completeness_entries ?? 0,
1595
+ source_reference_rewrites: mutationManifest.counts?.source_reference_rewrites ?? 0,
1596
+ identity_reference_rewrites: mutationManifest.counts?.identity_reference_rewrites ?? 0,
1597
+ identity_flow_reference_rewrites: identityReferenceRewriteStage.counts?.flow_reference_rewrites ?? 0,
1598
+ identity_reference_reuse_rows: identityReferenceRewriteStage.counts?.reference_rows ?? 0,
1599
+ curation_queue_status: curationQueueStage.status === "not_required" ? "not_required" : curationQueueStage.status,
1600
+ curation_queue_blockers: curationQueueStage.report?.counts?.blockers ?? 0,
1601
+ curation_queue_tasks: curationQueueStage.report?.counts?.tasks ?? 0,
1602
+ curation_queue_process_rows: curationQueueStage.report?.counts?.process_rows ?? 0,
1603
+ curation_queue_flow_rows: curationQueueStage.report?.counts?.flow_rows ?? 0,
1604
+ curation_queue_external_flow_refs: curationQueueStage.report?.counts?.external_flow_refs ?? 0,
1605
+ full_context_scope_blockers: mutationManifest.evidence?.scope_blockers?.filter((blocker) => String(blocker?.stage ?? "").includes("full_context_ai_completion")).length ?? 0,
1606
+ },
1607
+ blockers: [...prewriteGateBlockers, ...mutationManifestBlockers],
1608
+ stages: stageReports.map((stage) => compactStageReportWithTiming(stage, timingsByStage)),
1609
+ timings: finalizeTimings,
1610
+ files: {
1611
+ source_contact_rewrite_report: sourceContactRewriteStage.files?.report ?? null,
1612
+ source_contact_rewritten_rows: sourceContactRewriteStage.files?.output_rows ?? null,
1613
+ source_contact_support_rows: sourceContactRewriteStage.files?.support_rows ?? null,
1614
+ source_contact_support_finalize_report: sourceContactSupportFinalize.files?.report ?? null,
1615
+ source_contact_support_finalize_rows: sourceContactSupportFinalize.files?.final_rows ?? null,
1616
+ source_contact_support_commit_handoff_plan: sourceContactSupportFinalize.files?.commit_handoff_plan ?? null,
1617
+ source_contact_source_reference_rewrites: sourceContactRewriteStage.files?.source_reference_rewrites ?? null,
1618
+ cleanup_report: repoRelativeMaybe(cleanupReportFile),
1619
+ canonical_support_rewrite_report: repoRelativeMaybe(canonicalSupportReportFile),
1620
+ canonical_support_rewritten_rows: repoRelativeMaybe(canonicalSupportRowsFile),
1621
+ canonical_support_deferred_rows: canonicalSupportRewriteStage.files?.deferred_rows ?? null,
1622
+ canonical_support_rewrites: canonicalSupportRewriteStage.files?.canonical_support_rewrites ?? null,
1623
+ canonical_support_blockers: canonicalSupportRewriteStage.files?.canonical_support_blockers ?? null,
1624
+ identity_reference_rewrites: identityReferenceRewriteStage.rewrite_file ?? null,
1625
+ identity_preflight_run_report: repoRelativeMaybe(identityPreflightRunStage.report_file),
1626
+ identity_preflight_index: identityPreflightRunStage.index_file ?? null,
1627
+ identity_preflight_base_index: identityPreflightRunStage.base_index_file ?? null,
1628
+ identity_preflight_refresh_report: identityPreflightRunStage.refresh_report_file ?? null,
1629
+ identity_preflight_merge_report: identityPreflightRunStage.merge_report_file ?? null,
1630
+ identity_rewritten_rows: Number(identityReferenceRewriteStage.counts?.flow_reference_rewrites ?? 0) > 0
1631
+ ? identityReferenceRewriteStage.output_rows_file
1632
+ : null,
1633
+ unresolved_exchange_externalization_report: unresolvedExchangeExternalizeStage.files?.report ?? null,
1634
+ unresolved_exchange_externalized_rows: Number(unresolvedExchangeExternalizeStage.counts?.externalized_exchanges ?? 0) > 0
1635
+ ? unresolvedExchangeExternalizeStage.files?.output_rows
1636
+ : null,
1637
+ unresolved_exchange_traces: unresolvedExchangeExternalizeStage.files?.traces ?? null,
1638
+ identity_reference_reuse_rows: identityReferenceRewriteStage.reference_rows_file ?? null,
1639
+ curation_queue_dir: repoRelativeMaybe(curationQueueDir),
1640
+ curation_queue_report: repoRelativeMaybe(curationQueueStage.report_file),
1641
+ curation_queue_identity_external_flow_refs: curationQueueStage.files?.identity_external_flow_refs ?? null,
1642
+ curation_queue_process_reference_external_flow_refs: curationQueueStage.files?.process_reference_external_flow_refs ?? null,
1643
+ final_rows: repoRelativeMaybe(cleanedRowsFile),
1644
+ schema_report: repoRelativeMaybe(schemaStage.report_file),
1645
+ qa_report: repoRelativeMaybe(qaStage.report_file),
1646
+ location_audit_report: repoRelativeMaybe(locationAuditStage.report_file),
1647
+ curation_gate_report: repoRelativeMaybe(curationGateReportFile),
1648
+ patch_collect_report: repoRelativeMaybe(patchCollectReportFile),
1649
+ patch_apply_report: repoRelativeMaybe(patchApplyReportFile),
1650
+ classification_decision_apply_report: repoRelativeMaybe(classificationDecisionApplyReportFile),
1651
+ location_decision_apply_report: repoRelativeMaybe(locationDecisionApplyReportFile),
1652
+ identity_decision_apply_reports: identityDecisionApplyReportFiles.map((file) => repoRelativePath(file)),
1653
+ patch_evidence: mutationManifest.evidence?.patch_evidence_file ?? null,
1654
+ dry_run_report: repoRelativeMaybe(dryRunStage.report_file),
1655
+ remote_verify_report: repoRelativeMaybe(remoteVerifyReportFile),
1656
+ mutation_manifest: mutationManifest.files?.report ?? null,
1657
+ unresolved_traces: mutationManifest.files?.unresolved_traces ?? null,
1658
+ source_exchange_completeness_traces: mutationManifest.files?.source_exchange_completeness_traces ?? null,
1659
+ source_reference_rewrites: mutationManifest.files?.source_reference_rewrites ?? null,
1660
+ mutation_identity_reference_rewrites: mutationManifest.files?.identity_reference_rewrites ?? null,
1661
+ },
1662
+ };
1663
+ const reportPath = path.join(outDir, "dataset-post-authoring-finalize-report.json");
1664
+ writeJson(reportPath, report);
1665
+ const commitHandoffPlan = timeStage("commit_handoff_plan", () => runDatasetCommitHandoffPlan({
1666
+ finalizeReport: reportPath,
1667
+ outDir: path.join(outDir, "commit-handoff"),
1668
+ stateCode: options.commitStateCode ?? options.postWriteStateCode ?? options.stateCode ?? "0",
1669
+ targetUserId: options.targetUserId,
1670
+ rootPolicy: options.postWriteRootPolicy || options.rootPolicy || options.remoteRootPolicy,
1671
+ }));
1672
+ const finalReportBase = {
1673
+ ...report,
1674
+ counts: {
1675
+ ...report.counts,
1676
+ commit_handoff_blockers: commitHandoffPlan.counts?.blockers ?? 0,
1677
+ },
1678
+ commit_handoff: {
1679
+ status: commitHandoffPlan.status,
1680
+ command: commitHandoffPlan.commands?.commit ?? null,
1681
+ post_write_verify_command: commitHandoffPlan.commands?.post_write_verify ?? null,
1682
+ blockers: commitHandoffPlan.blockers ?? [],
1683
+ },
1684
+ files: {
1685
+ ...report.files,
1686
+ commit_handoff_plan: commitHandoffPlan.files?.report ?? null,
1687
+ },
1688
+ };
1689
+ const importLedger = writeFinalizeImportLedger
1690
+ ? timeStage("import_ledger", () => writeFinalizeImportLedger({
1691
+ report: finalReportBase,
1692
+ reportPath,
1693
+ ledgerDir: resolveRepoPath(options.ledgerDir || options.importLedgerDir || path.join(outDir, "import-ledger")),
1694
+ }))
1695
+ : { status: "skipped", files: {}, counts: { entries_written: 0 } };
1696
+ const finalReport = {
1697
+ ...finalReportBase,
1698
+ counts: {
1699
+ ...finalReportBase.counts,
1700
+ finalize_duration_ms: Date.now() - finalizeStartedAtMs,
1701
+ finalize_substage_count: finalizeTimings.length,
1702
+ import_ledger_entries: importLedger.counts?.entries_written ?? 0,
1703
+ },
1704
+ timings: finalizeTimings,
1705
+ files: {
1706
+ ...finalReportBase.files,
1707
+ import_ledger: importLedger.files ?? {},
1708
+ },
1709
+ };
1710
+ writeJson(reportPath, finalReport);
1711
+ return {
1712
+ ...finalReport,
1713
+ files: {
1714
+ ...finalReport.files,
1715
+ report: repoRelativePath(reportPath),
1716
+ },
1717
+ };
1718
+ }
1719
+ return { runDatasetPostAuthoringFinalize };
1720
+ }