@uniqbit/mate-core 0.18.5 → 1.0.0-canary.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 (390) hide show
  1. package/claude-capability-plugins/companion-mentions/.claude-plugin/marketplace.json +14 -0
  2. package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +8 -0
  3. package/claude-capability-plugins/companion-mentions/hooks/companion.ts +457 -0
  4. package/claude-capability-plugins/companion-mentions/hooks/hooks.json +1 -0
  5. package/claude-capability-plugins/companion-mentions/hooks/register.ts +403 -0
  6. package/claude-plugin/hooks/companion-instructions.mjs +9 -0
  7. package/claude-plugin/hooks/hooks.json +21 -2
  8. package/claude-plugin/hooks/tokensave-route.mjs +52 -0
  9. package/opencode-plugin/src/server.ts +4 -0
  10. package/opencode-plugin/src/tui.tsx +2 -0
  11. package/opencode-plugin/tui.tsx +2 -0
  12. package/package.json +52 -15
  13. package/src/brand/art.generated.ts +69 -0
  14. package/src/brand/index.ts +12 -0
  15. package/src/brand/palette.ts +19 -0
  16. package/src/brand/svg.generated.ts +8 -0
  17. package/src/brand/terminal.ts +88 -0
  18. package/src/brand/types.ts +33 -0
  19. package/src/cli/commands/artifact/apply-context.ts +275 -0
  20. package/src/cli/commands/artifact/artifact.ts +12 -0
  21. package/src/cli/commands/artifact/delivery/archive.ts +125 -0
  22. package/src/cli/commands/artifact/delivery/branch-flags.ts +55 -0
  23. package/src/cli/commands/artifact/delivery/environment.ts +127 -0
  24. package/src/cli/commands/artifact/delivery/link.ts +174 -0
  25. package/src/cli/commands/artifact/delivery/output.ts +89 -0
  26. package/src/cli/commands/artifact/finish/command.ts +83 -8
  27. package/src/cli/commands/artifact/finish/engine.ts +91 -14
  28. package/src/cli/commands/artifact/finish/finisher.ts +10 -0
  29. package/src/cli/commands/artifact/finish/git.ts +132 -19
  30. package/src/cli/commands/artifact/finish/openspec.ts +182 -138
  31. package/src/cli/commands/artifact/finish/secret-scan.ts +43 -0
  32. package/src/cli/commands/artifact/finish/worktree-cleanup.ts +92 -0
  33. package/src/cli/commands/artifact/pending/command.ts +31 -3
  34. package/src/cli/commands/artifact/pending/discovery.ts +21 -57
  35. package/src/cli/commands/cap/graphify.ts +52 -3
  36. package/src/cli/commands/cap/index-cmd.ts +71 -15
  37. package/src/cli/commands/cap/openspec.ts +13 -25
  38. package/src/cli/commands/cap/tokensave.ts +7 -5
  39. package/src/cli/commands/companion/link.ts +14 -1
  40. package/src/cli/commands/companion/open.ts +68 -46
  41. package/src/cli/commands/companion/sync.ts +10 -0
  42. package/src/cli/commands/companion/tui.ts +17 -7
  43. package/src/cli/commands/companion/update.ts +143 -48
  44. package/src/cli/commands/doctor.ts +195 -7
  45. package/src/cli/commands/factory/factory.ts +301 -0
  46. package/src/cli/commands/install.ts +133 -9
  47. package/src/cli/commands/launch/shared.ts +16 -11
  48. package/src/cli/commands/plugin/install.ts +9 -4
  49. package/src/cli/commands/plugin/plugin.ts +3 -2
  50. package/src/cli/commands/report/adapter.ts +11 -2
  51. package/src/cli/commands/report/contract.ts +21 -0
  52. package/src/cli/commands/report/highlight.ts +34 -4
  53. package/src/cli/commands/report/index.ts +20 -1
  54. package/src/cli/commands/report/markdown.ts +110 -0
  55. package/src/cli/commands/report/palette.ts +118 -0
  56. package/src/cli/commands/report/renderer.ts +456 -153
  57. package/src/cli/commands/report/types.ts +16 -0
  58. package/src/cli/commands/session/context.ts +102 -0
  59. package/src/cli/commands/setup.ts +50 -24
  60. package/src/cli/commands/studio/factory-credential.ts +49 -0
  61. package/src/cli/commands/studio/index.ts +29 -2
  62. package/src/cli/commands/studio/openspec-cli.ts +10 -58
  63. package/src/cli/commands/studio/payload.ts +249 -15
  64. package/src/cli/commands/studio/routes.ts +41 -6
  65. package/src/cli/commands/studio/selection.ts +161 -17
  66. package/src/cli/commands/studio/server.ts +404 -324
  67. package/src/cli/commands/studio/services.ts +60 -0
  68. package/src/cli/commands/studio/studio-app.ts +67 -0
  69. package/src/cli/commands/studio/terminal-assets.ts +21 -33
  70. package/src/cli/commands/studio/terminal.ts +85 -15
  71. package/src/cli/commands/studio/views/model.ts +18 -0
  72. package/src/cli/commands/studio/views/vault/markdown-tree.ts +267 -0
  73. package/src/cli/commands/studio/views/vault/view.ts +185 -0
  74. package/src/cli/commands/studio/workset-session.ts +131 -0
  75. package/src/cli/commands/unwrap.ts +8 -0
  76. package/src/cli/commands/update.ts +27 -1
  77. package/src/cli/commands/working/cleanup.ts +10 -1
  78. package/src/cli/commands/working/link.ts +76 -0
  79. package/src/cli/commands/working/working.ts +10 -0
  80. package/src/cli/commands/working/worktree.ts +356 -0
  81. package/src/cli/commands/wrap.ts +5 -13
  82. package/src/cli/companion-link-wizard.tsx +2 -0
  83. package/src/cli/companion-selector.tsx +2 -0
  84. package/src/cli/confirm.ts +2 -0
  85. package/src/cli/install-plan.tsx +127 -160
  86. package/src/cli/launch-selector.tsx +2 -0
  87. package/src/cli/main.ts +22 -1
  88. package/src/cli/parse-flags.ts +32 -6
  89. package/src/cli/repo-list-table.tsx +2 -0
  90. package/src/cli/setup-selector.tsx +86 -151
  91. package/src/cli/usage.ts +14 -0
  92. package/src/create-mate.ts +5 -4
  93. package/src/hooks/companion-instructions.ts +190 -0
  94. package/src/hooks/hook-io.ts +63 -0
  95. package/src/hooks/session-banner.ts +21 -29
  96. package/src/hooks/session-guidance.ts +9 -17
  97. package/src/hooks/session-prepare.ts +115 -28
  98. package/src/hooks/tokensave-route.ts +26 -0
  99. package/src/hooks/validate-artifact-path.ts +82 -24
  100. package/src/index.ts +4 -0
  101. package/src/lib/apply-context/fingerprint.ts +174 -0
  102. package/src/lib/apply-context/protocol.ts +193 -0
  103. package/src/lib/change-layout.ts +154 -0
  104. package/src/lib/components/confirm-prompt.tsx +21 -9
  105. package/src/lib/components/select-menu.tsx +10 -3
  106. package/src/lib/components/spinner.tsx +4 -2
  107. package/src/lib/components/startup-progress.tsx +227 -41
  108. package/src/lib/components/text-input-display.tsx +2 -0
  109. package/src/lib/components/wizard-footer.tsx +2 -0
  110. package/src/lib/components/wizard-header.tsx +16 -12
  111. package/src/lib/context-mode-package.ts +24 -11
  112. package/src/lib/delivery/archive-change.ts +166 -0
  113. package/src/lib/delivery/branches.ts +132 -0
  114. package/src/lib/delivery/environment.ts +35 -0
  115. package/src/lib/delivery/forge.ts +279 -0
  116. package/src/lib/delivery/merge-git.ts +261 -0
  117. package/src/lib/delivery/overlap.ts +209 -0
  118. package/src/lib/delivery/record-delivery.ts +283 -0
  119. package/src/lib/delivery/record.ts +403 -0
  120. package/src/lib/delivery/refresh.ts +268 -0
  121. package/src/lib/delivery/repository-access.ts +141 -0
  122. package/src/lib/delivery/resolve-branch.ts +162 -0
  123. package/src/lib/delivery/stages.ts +111 -0
  124. package/src/lib/delivery/token.ts +24 -0
  125. package/src/lib/factory/attach-client.ts +113 -0
  126. package/src/lib/factory/attach.ts +178 -0
  127. package/src/lib/factory/broker.ts +190 -0
  128. package/src/lib/factory/bun-commands.ts +39 -0
  129. package/src/lib/factory/client.ts +92 -0
  130. package/src/lib/factory/command.ts +61 -0
  131. package/src/lib/factory/credential-store.ts +153 -0
  132. package/src/lib/factory/frames.ts +77 -0
  133. package/src/lib/factory/ids.ts +58 -0
  134. package/src/lib/factory/keyed-serial.ts +27 -0
  135. package/src/lib/factory/outcomes/actions.ts +114 -0
  136. package/src/lib/factory/outcomes/index.ts +381 -0
  137. package/src/lib/factory/outcomes/lifecycle.ts +205 -0
  138. package/src/lib/factory/outcomes/runs.ts +81 -0
  139. package/src/lib/factory/outcomes/sessions.ts +38 -0
  140. package/src/lib/factory/prepare.ts +49 -0
  141. package/src/lib/factory/pty.ts +103 -0
  142. package/src/lib/factory/runner/companion-context.ts +178 -0
  143. package/src/lib/factory/runner/control.ts +92 -0
  144. package/src/lib/factory/runner/harness-agent.ts +250 -0
  145. package/src/lib/factory/runner/live.ts +159 -0
  146. package/src/lib/factory/runner/questions.ts +22 -0
  147. package/src/lib/factory/runner/rank.ts +26 -0
  148. package/src/lib/factory/runner/run-policy.ts +12 -0
  149. package/src/lib/factory/runner/run.ts +199 -0
  150. package/src/lib/factory/runner/scheduler.ts +416 -0
  151. package/src/lib/factory/runner/state.ts +233 -0
  152. package/src/lib/factory/runner/workflow.ts +542 -0
  153. package/src/lib/factory/runtime.ts +972 -0
  154. package/src/lib/factory/sandbox.ts +292 -0
  155. package/src/lib/factory/worktree-branch.ts +17 -0
  156. package/src/lib/frontmatter.ts +18 -0
  157. package/src/lib/install.ts +66 -14
  158. package/src/lib/live-progress.ts +36 -0
  159. package/src/lib/member-choices.ts +67 -0
  160. package/src/lib/opencode-plugin-package.ts +5 -5
  161. package/src/lib/openspec-cli.ts +116 -0
  162. package/src/lib/openspec-scopes.ts +86 -0
  163. package/src/lib/orchestrator/adapters/base.ts +55 -22
  164. package/src/lib/orchestrator/adapters/claude-args.ts +50 -0
  165. package/src/lib/orchestrator/adapters/claude.ts +54 -41
  166. package/src/lib/orchestrator/adapters/opencode.ts +2 -0
  167. package/src/lib/orchestrator/config-store.ts +3 -2
  168. package/src/lib/orchestrator/editor.ts +31 -25
  169. package/src/lib/orchestrator/openspec-store.ts +14 -54
  170. package/src/lib/orchestrator/projection-claude-commands.ts +36 -0
  171. package/src/lib/orchestrator/projection-claude-links.ts +159 -0
  172. package/src/lib/orchestrator/projection-claude-skills.ts +19 -137
  173. package/src/lib/orchestrator/projection-entries.ts +100 -34
  174. package/src/lib/orchestrator/projection-opencode-copies.ts +32 -15
  175. package/src/lib/orchestrator/projection-openspec-store.ts +54 -9
  176. package/src/lib/orchestrator/projection-runtime-documents.ts +15 -6
  177. package/src/lib/orchestrator/projection-types.ts +36 -1
  178. package/src/lib/orchestrator/refresh-working-claude-links.ts +87 -0
  179. package/src/lib/orchestrator/session-context-link.ts +67 -0
  180. package/src/lib/orchestrator/session-prepare.ts +45 -5
  181. package/src/lib/orchestrator/setup-compatibilities.ts +56 -70
  182. package/src/lib/orchestrator/types.ts +7 -2
  183. package/src/lib/orchestrator/working-repo-projection.ts +50 -7
  184. package/src/lib/orchestrator/workspace-document.ts +105 -0
  185. package/src/lib/orchestrator/wrap-pass.ts +22 -1
  186. package/src/lib/package-paths.ts +37 -0
  187. package/src/lib/runtime-directory-refresh.ts +17 -1
  188. package/src/lib/workset/record.ts +513 -0
  189. package/src/lib/workset/repositories.ts +55 -0
  190. package/src/lib/workset/stage.ts +148 -0
  191. package/src/lib/workset/stages.ts +113 -0
  192. package/src/lib/workset/worktree-index.ts +116 -0
  193. package/src/lib/workset/worktree.ts +830 -0
  194. package/src/opencode/companion-hooks.ts +109 -7
  195. package/src/opencode/companion-policy.ts +37 -18
  196. package/src/opencode/enter-worktree.ts +72 -0
  197. package/src/opencode/projected-guidance.ts +4 -1
  198. package/src/opencode/session-prepare.ts +2 -1
  199. package/src/opencode/tui.tsx +13 -6
  200. package/src/plugins.ts +1 -0
  201. package/src/runtime/artifact-refusal.ts +63 -0
  202. package/src/runtime/claude-delivery-plan.ts +107 -8
  203. package/src/runtime/commit-attribution-guard.ts +181 -0
  204. package/src/runtime/companion-git.ts +52 -10
  205. package/src/runtime/companion-guidance.ts +20 -8
  206. package/src/runtime/companion-instructions.ts +340 -0
  207. package/src/runtime/companion-lock.ts +30 -7
  208. package/src/runtime/env.ts +48 -3
  209. package/src/runtime/error-message.ts +4 -0
  210. package/src/runtime/git-layout.ts +219 -0
  211. package/src/runtime/index.ts +29 -0
  212. package/src/runtime/managed-block.ts +117 -0
  213. package/src/runtime/markdown-scan.ts +266 -0
  214. package/src/runtime/physical-path.ts +111 -0
  215. package/src/runtime/policy.ts +50 -2
  216. package/src/runtime/projected-flag.ts +7 -0
  217. package/src/runtime/projected-guidance.ts +1 -0
  218. package/src/runtime/runtime-directory.ts +5 -0
  219. package/src/runtime/runtime-document-manifest.ts +62 -11
  220. package/src/runtime/secret-guard.ts +162 -0
  221. package/src/runtime/secret-patterns.ts +397 -0
  222. package/src/runtime/secret-scan.ts +90 -0
  223. package/src/runtime/session-context.ts +98 -0
  224. package/src/runtime/session-kind.ts +13 -0
  225. package/src/runtime/shell-heredoc.ts +81 -0
  226. package/src/runtime/tokensave-route.ts +152 -0
  227. package/src/runtime/wrap-state.ts +44 -2
  228. package/src/runtime/write-atomic.ts +64 -0
  229. package/src/studio.ts +124 -0
  230. package/src/templates/capabilities/openspec-cap/mate-minimal/schema.yaml +14 -10
  231. package/src/templates/capabilities/openspec-cap/mate-minimal/templates/spec.md +3 -5
  232. package/src/templates/capabilities/openspec-cap/mate-v1/schema.yaml +18 -102
  233. package/src/templates/capabilities/openspec-cap/mate-v1/templates/spec.md +3 -5
  234. package/src/templates/capabilities/openspec-cap/openspec-conventions.yaml +20 -14
  235. package/src/templates/mate-skills/THIRD-PARTY-NOTICES.md +40 -0
  236. package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +134 -0
  237. package/src/templates/mate-skills/agents/mate-apply-change/references/mate-compact-apply.md +47 -0
  238. package/src/templates/mate-skills/agents/mate-apply-change/references/openspec-apply-change.md +200 -0
  239. package/src/templates/mate-skills/agents/mate-archive-change/SKILL.md +86 -0
  240. package/src/templates/mate-skills/{claude/mate-openspec-backfill → agents/mate-backfill-spec}/SKILL.md +11 -4
  241. package/src/templates/mate-skills/agents/mate-clarify-intent/SKILL.md +110 -0
  242. package/src/templates/mate-skills/agents/mate-explore-change/SKILL.md +66 -0
  243. package/src/templates/mate-skills/agents/mate-explore-change/references/openspec-explore.md +350 -0
  244. package/src/templates/mate-skills/agents/mate-grill-me/SKILL.md +11 -4
  245. package/src/templates/mate-skills/agents/mate-grill-with-docs/SKILL.md +2 -2
  246. package/src/templates/mate-skills/agents/mate-interview-me/SKILL.md +12 -5
  247. package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +108 -0
  248. package/src/templates/mate-skills/agents/mate-propose-change/references/mate-revise.md +47 -0
  249. package/src/templates/mate-skills/agents/mate-propose-change/references/openspec-propose.md +169 -0
  250. package/src/templates/mate-skills/agents/mate-propose-change/references/openspec-update-change.md +103 -0
  251. package/src/templates/mate-skills/agents/{mate-artifact-publish → mate-publish-artifact}/SKILL.md +111 -24
  252. package/src/templates/mate-skills/{claude/mate-artifact-publish → agents/mate-publish-artifact}/references/openspec.md +8 -5
  253. package/src/templates/mate-skills/agents/mate-show-me/SKILL.md +95 -16
  254. package/src/templates/mate-skills/agents/mate-simplify-code/SKILL.md +87 -68
  255. package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +134 -0
  256. package/src/templates/mate-skills/claude/mate-apply-change/references/mate-compact-apply.md +47 -0
  257. package/src/templates/mate-skills/claude/mate-apply-change/references/openspec-apply-change.md +200 -0
  258. package/src/templates/mate-skills/claude/mate-archive-change/SKILL.md +86 -0
  259. package/src/templates/mate-skills/{agents/mate-openspec-backfill → claude/mate-backfill-spec}/SKILL.md +11 -4
  260. package/src/templates/mate-skills/claude/mate-clarify-intent/SKILL.md +110 -0
  261. package/src/templates/mate-skills/claude/mate-explore-change/SKILL.md +66 -0
  262. package/src/templates/mate-skills/claude/mate-explore-change/references/openspec-explore.md +350 -0
  263. package/src/templates/mate-skills/claude/mate-grill-me/SKILL.md +11 -4
  264. package/src/templates/mate-skills/claude/mate-grill-with-docs/SKILL.md +2 -2
  265. package/src/templates/mate-skills/claude/mate-interview-me/SKILL.md +12 -5
  266. package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +108 -0
  267. package/src/templates/mate-skills/claude/mate-propose-change/references/mate-revise.md +47 -0
  268. package/src/templates/mate-skills/claude/mate-propose-change/references/openspec-propose.md +169 -0
  269. package/src/templates/mate-skills/claude/mate-propose-change/references/openspec-update-change.md +103 -0
  270. package/src/templates/mate-skills/claude/{mate-artifact-publish → mate-publish-artifact}/SKILL.md +111 -24
  271. package/src/templates/mate-skills/{agents/mate-artifact-publish → claude/mate-publish-artifact}/references/openspec.md +8 -5
  272. package/src/templates/mate-skills/claude/mate-show-me/SKILL.md +95 -16
  273. package/src/templates/mate-skills/claude/mate-simplify-code/SKILL.md +87 -68
  274. package/src/templates/mate-skills/openspec-references.ts +142 -0
  275. package/src/templates/root/TEMPLATE_AGENTS.md +2 -2
  276. package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +3938 -391
  277. package/src/tools/setup/capabilities/companion-mentions.ts +27 -0
  278. package/src/tools/setup/capabilities/context-mode.ts +56 -7
  279. package/src/tools/setup/capabilities/graphify-gate.ts +31 -0
  280. package/src/tools/setup/capabilities/graphify-secrets.ts +243 -0
  281. package/src/tools/setup/capabilities/graphify.ts +46 -7
  282. package/src/tools/setup/capabilities/openspec.ts +163 -272
  283. package/src/tools/setup/capabilities/react-doctor.ts +8 -1
  284. package/src/tools/setup/capabilities/rtk.ts +4 -0
  285. package/src/tools/setup/capabilities/tokensave.ts +113 -71
  286. package/src/tools/setup/context-services.ts +9 -17
  287. package/src/tools/setup/dynamic-plugins/install.ts +24 -28
  288. package/src/tools/setup/engine.ts +100 -23
  289. package/src/tools/setup/install-contract.ts +8 -0
  290. package/src/tools/setup/managed-output.ts +51 -0
  291. package/src/tools/setup/mate-skill-copies.ts +51 -0
  292. package/src/tools/setup/mate.ts +86 -11
  293. package/src/tools/setup/package-managers/uv.ts +73 -11
  294. package/src/tools/setup/plugin.ts +40 -0
  295. package/src/tools/setup/plugins/guidance.ts +7 -4
  296. package/src/tools/setup/providers/agent-file-sections.ts +4 -2
  297. package/src/tools/setup/providers/claude-attribution.ts +65 -0
  298. package/src/tools/setup/providers/claude-plugin-folders.ts +625 -0
  299. package/src/tools/setup/providers/claude-plugin-hooks.ts +51 -19
  300. package/src/tools/setup/providers/claude-worktree-plugins.ts +122 -0
  301. package/src/tools/setup/providers/claude.ts +290 -8
  302. package/src/tools/setup/providers/opencode.ts +180 -1
  303. package/src/tools/setup/utils.ts +18 -4
  304. package/src/tools/setup/working-repo-cleanup.ts +29 -1
  305. package/src/tools/setup/working-repo-local-state.ts +252 -75
  306. package/src/tools/setup.ts +183 -26
  307. package/studio-dist/client/assets/companion-view-Csva26Ir.js +1 -0
  308. package/studio-dist/client/assets/delivery-path-DJMpRmiY.js +1 -0
  309. package/studio-dist/client/assets/dist-Dm7MtyHc.js +5 -0
  310. package/studio-dist/client/assets/factory-B7l9ZQyx.js +4 -0
  311. package/studio-dist/client/assets/index-n6uo_92S.js +40 -0
  312. package/studio-dist/client/assets/preload-helper-BLdHbfdN.js +1 -0
  313. package/studio-dist/client/assets/prepaint-CD5OpmTM.js +24 -0
  314. package/studio-dist/client/assets/queries-CN3JwMBX.js +1 -0
  315. package/studio-dist/client/assets/react-CabXf2Ds.js +1 -0
  316. package/studio-dist/client/assets/report-time-Dc-KOqQu.js +1 -0
  317. package/studio-dist/client/assets/reports-C01RsnMA.js +1 -0
  318. package/studio-dist/client/assets/rolldown-runtime-WkwZ5CU-.js +1 -0
  319. package/studio-dist/client/assets/routes-4pbC-2Hj.js +1 -0
  320. package/studio-dist/client/assets/skills-CihQganl.js +1 -0
  321. package/studio-dist/client/assets/spec-areas-C2L48N4C.js +1 -0
  322. package/studio-dist/client/assets/specs-D24_35_K.js +1 -0
  323. package/studio-dist/client/assets/studio-DKxfi691.css +1 -0
  324. package/studio-dist/client/assets/studio-queries-rvbJupCy.js +3 -0
  325. package/studio-dist/client/assets/terminal-eU5fzQzv.css +1 -0
  326. package/studio-dist/client/assets/vault-Bd4CQiTK.js +1 -0
  327. package/studio-dist/client/assets/warnings-V_Ys51wJ.js +1 -0
  328. package/studio-dist/client/assets/wasm-inline-C2en61B5.js +1 -0
  329. package/studio-dist/client/assets/workflow-BJejeVwQ.js +1 -0
  330. package/studio-dist/server/assets/__23tanstack-start-server-fn-resolver-BQKuMdJW.js +3881 -0
  331. package/studio-dist/server/assets/_tanstack-start-manifest_v.js +85 -0
  332. package/studio-dist/server/assets/actions-Bvgj9eb8.js +54 -0
  333. package/studio-dist/server/assets/actions-D8erOphH.js +44 -0
  334. package/studio-dist/server/assets/companion-view-aBady57v.js +33 -0
  335. package/studio-dist/server/assets/createServerFn-F5sAOF8V.js +459 -0
  336. package/studio-dist/server/assets/createServerRpc-tK2lE9R2.js +11 -0
  337. package/studio-dist/server/assets/delivery-path-BkKNjgkf.js +778 -0
  338. package/studio-dist/server/assets/dist-DpJWKyCm.js +5582 -0
  339. package/studio-dist/server/assets/empty-plugin-adapters-Cipd4ZH4.js +4 -0
  340. package/studio-dist/server/assets/factory-D6xnjedJ.js +2385 -0
  341. package/studio-dist/server/assets/factory-DT3UzHlh.js +68 -0
  342. package/studio-dist/server/assets/model-PyPALjii.js +18 -0
  343. package/studio-dist/server/assets/prepaint-BMke0fo1.js +41 -0
  344. package/studio-dist/server/assets/queries-Ag2iL8q2.js +107 -0
  345. package/studio-dist/server/assets/react-COs0LHn-.js +51 -0
  346. package/studio-dist/server/assets/report-time-DAfzln-N.js +8 -0
  347. package/studio-dist/server/assets/reports-BnUNJVtx.js +126 -0
  348. package/studio-dist/server/assets/rolldown-runtime-DYf_mQCB.js +34 -0
  349. package/studio-dist/server/assets/router-BH--drIx.js +4298 -0
  350. package/studio-dist/server/assets/routes-khSDK1bV.js +361 -0
  351. package/studio-dist/server/assets/search-JAKurDNh.js +36 -0
  352. package/studio-dist/server/assets/shell-CboT2f66.js +51 -0
  353. package/studio-dist/server/assets/skills-MWYnWoKs.js +106 -0
  354. package/studio-dist/server/assets/spec-areas-DcnO6vW-.js +16 -0
  355. package/studio-dist/server/assets/specs-DE3w8hZP.js +155 -0
  356. package/studio-dist/server/assets/start-BW5ovN0k.js +33 -0
  357. package/studio-dist/server/assets/studio-BeltxxKt.js +216 -0
  358. package/studio-dist/server/assets/studio-queries-CLnP9kGu.js +3365 -0
  359. package/studio-dist/server/assets/utils-C9CX1kcy.js +221 -0
  360. package/studio-dist/server/assets/vault-BGulVoNh.js +70 -0
  361. package/studio-dist/server/assets/vault-CuyyhLyB.js +832 -0
  362. package/studio-dist/server/assets/views-Bd7cRr27.js +82 -0
  363. package/studio-dist/server/assets/warnings-BH05rOuG.js +15 -0
  364. package/studio-dist/server/assets/wasm-inline-CNeBKGwC.js +3 -0
  365. package/studio-dist/server/assets/workflow-BSuIKWqH.js +42 -0
  366. package/studio-dist/server/server.js +16220 -0
  367. package/wrappers/bin/graphify +52 -4
  368. package/wrappers/bin/openspec +33 -9
  369. package/wrappers/lib/graphify-gate.mjs +8 -0
  370. package/src/cli/commands/studio/views/client.ts +0 -654
  371. package/src/cli/commands/studio/views/companion-picker.tsx +0 -95
  372. package/src/cli/commands/studio/views/companion-selector.tsx +0 -94
  373. package/src/cli/commands/studio/views/dashboard/changes.tsx +0 -107
  374. package/src/cli/commands/studio/views/dashboard/index.tsx +0 -19
  375. package/src/cli/commands/studio/views/document.tsx +0 -842
  376. package/src/cli/commands/studio/views/error.tsx +0 -20
  377. package/src/cli/commands/studio/views/reports/index.tsx +0 -49
  378. package/src/cli/commands/studio/views/selection-fields.tsx +0 -14
  379. package/src/cli/commands/studio/views/skills/index.tsx +0 -87
  380. package/src/cli/commands/studio/views/specs/index.tsx +0 -157
  381. package/src/cli/commands/studio/views/styles.ts +0 -519
  382. package/src/cli/commands/studio/views/terminal.tsx +0 -484
  383. package/src/cli/commands/studio/views/vault/state.ts +0 -43
  384. package/src/cli/commands/studio/views/warnings.tsx +0 -21
  385. package/src/cli/commands/studio/views/workflow/index.tsx +0 -21
  386. package/src/cli/commands/studio/views/workflow/steps.ts +0 -337
  387. package/src/cli/commands/studio/views/workflow/transcript.tsx +0 -190
  388. package/src/lib/components/theme.ts +0 -2
  389. package/src/tools/setup/git-utils.ts +0 -30
  390. package/src/tools/setup/runtime-documents.ts +0 -291
@@ -0,0 +1,283 @@
1
+ import { workingRepoGit, type ProtectionReason, type TicketFormat } from "./branches";
2
+ import { changeNameOf, locateChange, type ChangeLocation } from "../change-layout";
3
+ import { companionRelative, type DeliveryEnvironment } from "./environment";
4
+ import {
5
+ findDelivery,
6
+ ticketCandidateOf,
7
+ legacyDeliveryBranch,
8
+ readOrUnlinkedRecord,
9
+ sameRepository,
10
+ settleChangeDir,
11
+ upsertDelivery,
12
+ writeDeliveryRecord,
13
+ type DeliveryRecord,
14
+ type DeliverySettlement,
15
+ type MergeState,
16
+ } from "./record";
17
+ import {
18
+ refreshRecord,
19
+ type BranchOutcome,
20
+ type RefreshAccess,
21
+ type RefreshCache,
22
+ } from "./refresh";
23
+ import { resolveDeliveryBranch, type HasRef } from "./resolve-branch";
24
+ import {
25
+ repositoryAccessFor,
26
+ type RepositoryAccess,
27
+ type RepositoryAccessDeps,
28
+ } from "./repository-access";
29
+
30
+ /** Edits to one repository's delivery, applied in this order around an optional refresh. */
31
+ export interface DeliveryEdit {
32
+ delivery?: { branch: string; direct: boolean } | { notApplicable: string };
33
+ /** Informational PR URL; also picks the PR a refresh asks about. */
34
+ pr?: string;
35
+ /** The user's answer, recorded after any refresh with source `user`. */
36
+ confirm?: MergeState;
37
+ /** A Jira key recorded for the change; refused while the companion's tickets are off. */
38
+ ticket?: string;
39
+ }
40
+
41
+ export interface DeliveryRequest {
42
+ changes: string[];
43
+ /**
44
+ * The delivery to edit, and the only one refreshed unless `refreshAll`. Without it the
45
+ * Working Repository's delivery is edited and every delivery is refreshed.
46
+ */
47
+ repository?: string;
48
+ edit?: DeliveryEdit;
49
+ /**
50
+ * Ask the forge, then a local checkout, for each branch's merge state; a direct delivery
51
+ * needs the user's answer instead.
52
+ */
53
+ refresh?: boolean;
54
+ /** Fetch before a local check even when the companion's Git mode is not `auto`. */
55
+ fetch?: boolean;
56
+ /** Refresh every delivery even when `repository` names the one to edit. */
57
+ refreshAll?: boolean;
58
+ /** Archived changes may be edited; otherwise only active ones are found. */
59
+ includeArchived?: boolean;
60
+ /** A change left without any delivery answers `needsBranch` and nothing is written. */
61
+ requireDelivery?: boolean;
62
+ /** Marks a record left without any delivery as `skipped`, so it stays held back. */
63
+ markSkipped?: boolean;
64
+ }
65
+
66
+ export type DeliveryRefusal =
67
+ | { reason: "unknown-change"; change: string }
68
+ | { reason: "no-repository" }
69
+ | { reason: "tickets-off" }
70
+ | { reason: "protected-branch"; branch: string; protection: ProtectionReason | null }
71
+ | {
72
+ reason: "no-delivery-branch";
73
+ change: string;
74
+ repository: string;
75
+ for: "pr" | "confirm";
76
+ };
77
+
78
+ export interface DeliveryResult {
79
+ defaultBranch: string | null;
80
+ diagnostics: string[];
81
+ /** The companion's ticket setting. */
82
+ ticketFormat: TicketFormat;
83
+ /**
84
+ * With `ticketFormat` `jira` and no recorded ticket: the single Jira key in the branch
85
+ * recorded for the repository, else in the Working Repository's current branch. Never
86
+ * recorded without an explicit `--ticket`.
87
+ */
88
+ ticketCandidate: string | null;
89
+ change: string;
90
+ /** Companion-relative path of the change directory. */
91
+ path: string;
92
+ archived: boolean;
93
+ record: DeliveryRecord;
94
+ settlement: DeliverySettlement;
95
+ /** Lookups made by the refresh, including the ones the user must answer. */
96
+ outcomes: BranchOutcome[];
97
+ }
98
+
99
+ export interface NeedsBranch {
100
+ defaultBranch: string | null;
101
+ diagnostics: string[];
102
+ change: string;
103
+ record: DeliveryRecord;
104
+ /** First existing safe default or legacy branch in the Working Repository. */
105
+ suggestedBranch: string | null;
106
+ }
107
+
108
+ export type DeliveryOutcome =
109
+ | { refusal: DeliveryRefusal }
110
+ | { needsBranch: NeedsBranch }
111
+ /** Every record is already written when the request edited or refreshed. */
112
+ | { results: DeliveryResult[] };
113
+
114
+ export interface RecordDeliveryDeps extends RepositoryAccessDeps {
115
+ now?: () => Date;
116
+ repositoryAccess?: (repository: string) => RepositoryAccess;
117
+ }
118
+
119
+ /** Ref lookup in the repository's local checkout; null when none is known. */
120
+ async function hasRefIn(
121
+ environment: DeliveryEnvironment,
122
+ repository: string,
123
+ ): Promise<HasRef | null> {
124
+ const working = environment.workingRepo;
125
+ if (working?.slug && sameRepository(working.slug, repository)) {
126
+ return (ref) => working.git.hasRef(ref);
127
+ }
128
+ const checkout = await environment.checkoutOf?.(repository);
129
+ if (!checkout) return null;
130
+ const git = workingRepoGit(checkout);
131
+ return (ref) => git.hasRef(ref);
132
+ }
133
+
134
+ function hasDeliveryBranch(record: DeliveryRecord, repository: string): boolean {
135
+ return Boolean(findDelivery(record, repository)?.branch);
136
+ }
137
+
138
+ /**
139
+ * Reads, edits, refreshes, and writes the Delivery Records of `request.changes`. Every
140
+ * change resolves before any record is written, so a refusal leaves no partial update; a
141
+ * request without an edit or refresh only reads. Network requests go only to the forge:
142
+ * for another repository's default branch, and to refresh. `--direct` is
143
+ * accepted on any branch; a protected one requires it.
144
+ */
145
+ export async function recordDelivery(
146
+ environment: DeliveryEnvironment,
147
+ request: DeliveryRequest,
148
+ deps: RecordDeliveryDeps = {},
149
+ ): Promise<DeliveryOutcome> {
150
+ const { edit = {} } = request;
151
+ const now = deps.now ?? (() => new Date());
152
+ const access = deps.repositoryAccess ?? repositoryAccessFor(environment, deps);
153
+
154
+ const located: Array<ChangeLocation & { change: string }> = [];
155
+ for (const change of request.changes) {
156
+ const location = await locateChange(environment.companionPath, change);
157
+ if (!location || (location.archived && !request.includeArchived)) {
158
+ return { refusal: { reason: "unknown-change", change } };
159
+ }
160
+ located.push({ ...location, change });
161
+ }
162
+
163
+ const repository = request.repository ?? environment.workingRepo?.slug ?? null;
164
+ const ticketFormat = environment.ticket?.format ?? "none";
165
+ if (edit.ticket !== undefined && ticketFormat === "none") {
166
+ return { refusal: { reason: "tickets-off" } };
167
+ }
168
+ const repositoryEdits =
169
+ edit.delivery !== undefined || edit.pr !== undefined || edit.confirm !== undefined;
170
+ const edits = repositoryEdits || edit.ticket !== undefined || request.markSkipped === true;
171
+ if (repositoryEdits && !repository) return { refusal: { reason: "no-repository" } };
172
+ if (repository && edit.delivery && "branch" in edit.delivery) {
173
+ const { branch, direct } = edit.delivery;
174
+ const protection = await access(repository).protection(branch);
175
+ if (protection.protected && !direct) {
176
+ return { refusal: { reason: "protected-branch", branch, protection: protection.reason } };
177
+ }
178
+ }
179
+
180
+ const refreshAccess: RefreshAccess = {
181
+ now,
182
+ forgeAccess: (repo) => access(repo).forge(),
183
+ localAccess: async (repo) => (await access(repo).local?.()) ?? null,
184
+ fetch: request.fetch === true || environment.gitAutoMode === true,
185
+ };
186
+ const cache: RefreshCache = new Map();
187
+ const resolved: Array<DeliveryResult & { dir: string }> = [];
188
+ const hasRef = repository ? await hasRefIn(environment, repository) : null;
189
+ const currentBranch = (await environment.workingRepo?.git.currentBranch()) ?? null;
190
+
191
+ for (const { change, dir, archived } of located) {
192
+ let record = await readOrUnlinkedRecord(dir, change);
193
+ const resolution = await resolveDeliveryBranch({
194
+ companionPath: environment.companionPath,
195
+ repository,
196
+ hasRef,
197
+ change,
198
+ });
199
+ const diagnostics = resolution.diagnostics;
200
+ const defaultBranch = resolution.branch;
201
+ const recordedBranch = repository ? findDelivery(record, repository)?.branch : undefined;
202
+ if (recordedBranch && defaultBranch && recordedBranch !== defaultBranch) {
203
+ diagnostics.push(
204
+ `Recorded branch ${recordedBranch} differs from default ${defaultBranch}; preserving recorded delivery unless explicitly relinked.`,
205
+ );
206
+ }
207
+ if (edit.ticket !== undefined) record = { ...record, ticket: edit.ticket };
208
+ if (repository && edit.delivery) {
209
+ record = upsertDelivery(record, repository, edit.delivery);
210
+ // Recording a branch keeps its local tip, so a later deletion cannot hide the merge.
211
+ if ("branch" in edit.delivery) {
212
+ const local = await access(repository).local?.();
213
+ const head = await local?.git.tip(edit.delivery.branch);
214
+ if (head) record = upsertDelivery(record, repository, { head });
215
+ }
216
+ }
217
+
218
+ if (request.requireDelivery && record.deliveries.length === 0) {
219
+ let suggestedBranch: string | null = null;
220
+ for (const candidate of [defaultBranch, legacyDeliveryBranch(changeNameOf(change))]) {
221
+ if (candidate && (await environment.workingRepo?.git.branchExists(candidate))) {
222
+ suggestedBranch = candidate;
223
+ break;
224
+ }
225
+ }
226
+ return { needsBranch: { change, record, defaultBranch, diagnostics, suggestedBranch } };
227
+ }
228
+ if (request.markSkipped && record.deliveries.length === 0) {
229
+ record = { ...record, skipped: true };
230
+ }
231
+
232
+ if (repository && edit.pr !== undefined) {
233
+ if (!hasDeliveryBranch(record, repository)) {
234
+ return { refusal: { reason: "no-delivery-branch", change, repository, for: "pr" } };
235
+ }
236
+ record = upsertDelivery(record, repository, { pr: edit.pr });
237
+ }
238
+
239
+ let outcomes: BranchOutcome[] = [];
240
+ if (request.refresh) {
241
+ const refreshed = await refreshRecord(record, {
242
+ ...refreshAccess,
243
+ cache,
244
+ ...(request.repository && !request.refreshAll ? { repository: request.repository } : {}),
245
+ });
246
+ record = refreshed.record;
247
+ outcomes = refreshed.outcomes;
248
+ }
249
+
250
+ if (repository && edit.confirm !== undefined) {
251
+ if (!hasDeliveryBranch(record, repository)) {
252
+ return { refusal: { reason: "no-delivery-branch", change, repository, for: "confirm" } };
253
+ }
254
+ record = upsertDelivery(record, repository, {
255
+ merge: { state: edit.confirm, source: "user", checkedAt: now().toISOString() },
256
+ });
257
+ }
258
+
259
+ const candidateSource = recordedBranch ?? currentBranch;
260
+ resolved.push({
261
+ defaultBranch,
262
+ diagnostics,
263
+ ticketFormat,
264
+ ticketCandidate:
265
+ ticketFormat === "jira" && !record.ticket && candidateSource
266
+ ? ticketCandidateOf(candidateSource)
267
+ : null,
268
+ dir,
269
+ change: record.change,
270
+ path: companionRelative(environment, dir),
271
+ archived,
272
+ record,
273
+ settlement: await settleChangeDir(dir, record),
274
+ outcomes,
275
+ });
276
+ }
277
+
278
+ if (edits || request.refresh) {
279
+ for (const { dir, record } of resolved) await writeDeliveryRecord(dir, record);
280
+ }
281
+ const results = resolved.map(({ dir: _dir, ...result }) => result);
282
+ return { results };
283
+ }
@@ -0,0 +1,403 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import { parse, stringify } from "yaml";
5
+ import { errorMessage } from "../../runtime/error-message";
6
+ import { changeNameOf } from "../change-layout";
7
+ import { scopedRepositories } from "../openspec-scopes";
8
+
9
+ /** The Delivery Record's file name at a change root; it moves with the change into its archive. */
10
+ export const DELIVERY_FILE = "delivery.yaml";
11
+
12
+ /** A merge result: `open` is "not yet merged", `closed-unmerged` is "dropped". */
13
+ export type MergeState = "merged" | "open" | "closed-unmerged";
14
+
15
+ /**
16
+ * Where a merge result came from; a user answer is recorded as such, never as forge truth.
17
+ * `git`: the delivery's head was found on the default branch in a local checkout.
18
+ */
19
+ export type MergeSource = "forge" | "git" | "user";
20
+
21
+ /** What the forge reports about a merge, when it reports anything. */
22
+ export interface MergeProvenance {
23
+ mergedAt?: string;
24
+ mergeCommit?: string;
25
+ /** Provenance only; a merger is never presented as an artifact reviewer. */
26
+ mergedBy?: string;
27
+ }
28
+
29
+ export interface MergeResult extends MergeProvenance {
30
+ state: MergeState;
31
+ source: MergeSource;
32
+ /** When the result was recorded (ISO 8601). */
33
+ checkedAt: string;
34
+ }
35
+
36
+ /** The provenance fields `source` carries, without empty ones. */
37
+ export function pickProvenance(source: MergeProvenance): MergeProvenance {
38
+ return {
39
+ ...(source.mergedAt ? { mergedAt: source.mergedAt } : {}),
40
+ ...(source.mergeCommit ? { mergeCommit: source.mergeCommit } : {}),
41
+ ...(source.mergedBy ? { mergedBy: source.mergedBy } : {}),
42
+ };
43
+ }
44
+
45
+ /** One Working Repository's delivery: a branch, or a reason no branch applies. */
46
+ export interface Delivery {
47
+ /** Canonical `org/repository` of the Working Repository. */
48
+ repository: string;
49
+ branch?: string;
50
+ /** True when the branch is protected and the developer opted into direct delivery. */
51
+ direct?: boolean;
52
+ /** Informational PR URL; the branch, not the PR, is what a change is delivered through. */
53
+ pr?: string;
54
+ /** The branch's last known tip commit, kept so detection works after the branch is deleted. */
55
+ head?: string;
56
+ merge?: MergeResult;
57
+ notApplicable?: string;
58
+ }
59
+
60
+ export interface DeliveryRecord {
61
+ version: 1;
62
+ change: string;
63
+ /** The change's Jira ticket; names its computed Delivery Branch. */
64
+ ticket?: string;
65
+ /** Set by `archive --skip-branch`, so a ticket-only record stays held back. */
66
+ skipped?: true;
67
+ deliveries: Delivery[];
68
+ }
69
+
70
+ const JIRA_KEY = /^[A-Z][A-Z0-9]+-\d+$/;
71
+ const JIRA_KEYS = /(?<![A-Za-z0-9])[A-Z][A-Z0-9]+-\d+(?!\d)/g;
72
+
73
+ /** A Jira issue key such as `ABC-123`. */
74
+ export function isJiraKey(value: string): boolean {
75
+ return JIRA_KEY.test(value);
76
+ }
77
+
78
+ /** The single distinct Jira key in a branch name; null for none or several. */
79
+ export function ticketCandidateOf(branch: string): string | null {
80
+ const keys = new Set(branch.match(JIRA_KEYS) ?? []);
81
+ return keys.size === 1 ? [...keys][0]! : null;
82
+ }
83
+
84
+ export type DeliveryPatch = Partial<Omit<Delivery, "repository">>;
85
+
86
+ /**
87
+ * - `legacy`: no record (archived before Delivery Records, or past `mate artifact archive`)
88
+ * - `unlinked`: a record without any delivery
89
+ * - `not-applicable`: every delivery carries a not-applicable reason
90
+ * - `merged`: every branch delivery is merged (the rest not applicable)
91
+ * - `dropped`: some branch was closed without merging
92
+ * - `waiting`: some branch has no merge result yet, or is still open, or a scoped
93
+ * repository has no delivery recorded
94
+ */
95
+ export type DeliveryStatus =
96
+ | "legacy"
97
+ | "unlinked"
98
+ | "not-applicable"
99
+ | "merged"
100
+ | "dropped"
101
+ | "waiting";
102
+
103
+ export interface DeliverySettlement {
104
+ status: DeliveryStatus;
105
+ /** Repositories whose delivery keeps the change from being publishable. */
106
+ unsettled: string[];
107
+ /** Scoped repositories without any delivery, branch or not-applicable; a subset of `unsettled`. */
108
+ missing: string[];
109
+ /** Recorded repositories matching none of the change's scopes; empty when it has none. */
110
+ unscoped: UnscopedDelivery[];
111
+ }
112
+
113
+ export interface UnscopedDelivery {
114
+ repository: string;
115
+ /** The scoped spelling it most likely means, when one is obvious. */
116
+ closest?: string;
117
+ }
118
+
119
+ /**
120
+ * Whether an archive with this status may be published; anything else is held back. A
121
+ * `legacy` archive (made before Delivery Records) publishes, with a warning that its merge
122
+ * was not checked.
123
+ */
124
+ export function isPublishable(status: DeliveryStatus): boolean {
125
+ return status === "merged" || status === "not-applicable" || status === "legacy";
126
+ }
127
+
128
+ /** The warning a `legacy` archive publishes with; a ticket-only record names its ticket. */
129
+ export function legacyDeliveryWarning(anchorName: string, record?: DeliveryRecord | null): string {
130
+ if (record?.ticket) {
131
+ return `${anchorName}: ticket ${record.ticket} records no delivery branch, so its merge was not checked; publishing it as a legacy archive.`;
132
+ }
133
+ return `${anchorName} has no delivery.yaml, so its merge was not checked; publishing it as a legacy archive.`;
134
+ }
135
+
136
+ /** `workset/<workset-id>`, or `workset/<TICKET>-<workset-id>` with a ticket. */
137
+ export function defaultDeliveryBranch(worksetId: string, ticket?: string | null): string {
138
+ return ticket ? `workset/${ticket}-${worksetId}` : `workset/${worksetId}`;
139
+ }
140
+
141
+ export function legacyDeliveryBranch(change: string): string {
142
+ return `change/${change}`;
143
+ }
144
+
145
+ export function unlinkedRecord(change: string): DeliveryRecord {
146
+ return { version: 1, change, deliveries: [] };
147
+ }
148
+
149
+ /** GitHub slugs are case-insensitive, so `Acme/Api` and `acme/api` name one repository. */
150
+ export function sameRepository(a: string, b: string): boolean {
151
+ return a.toLowerCase() === b.toLowerCase();
152
+ }
153
+
154
+ export function findDelivery(record: DeliveryRecord, repository: string): Delivery | undefined {
155
+ return record.deliveries.find((entry) => sameRepository(entry.repository, repository));
156
+ }
157
+
158
+ /**
159
+ * Applies `patch` to the repository's delivery, creating it when absent; an existing
160
+ * delivery keeps its spelling of the repository. A branch and a not-applicable reason
161
+ * exclude each other, and a new branch or a switch between direct and PR delivery drops
162
+ * the previous merge result and PR, which described a different delivery; a different PR
163
+ * drops the merge result.
164
+ */
165
+ export function upsertDelivery(
166
+ record: DeliveryRecord,
167
+ requested: string,
168
+ patch: DeliveryPatch,
169
+ ): DeliveryRecord {
170
+ const existing = findDelivery(record, requested);
171
+ const repository = existing?.repository ?? requested;
172
+ let next: Delivery = { ...existing, repository };
173
+ if (patch.notApplicable !== undefined) {
174
+ next = { repository, notApplicable: patch.notApplicable };
175
+ } else {
176
+ const newBranch = patch.branch !== undefined && patch.branch !== next.branch;
177
+ const newMode = patch.direct !== undefined && patch.direct !== Boolean(next.direct);
178
+ if (newBranch || newMode) {
179
+ next = { repository, branch: patch.branch ?? next.branch };
180
+ }
181
+ if (patch.direct !== undefined) next.direct = patch.direct;
182
+ if (patch.pr !== undefined && patch.pr !== next.pr) {
183
+ delete next.merge;
184
+ next.pr = patch.pr;
185
+ }
186
+ if (patch.head !== undefined) next.head = patch.head;
187
+ if (patch.merge !== undefined) next.merge = patch.merge;
188
+ if (next.direct === false) delete next.direct;
189
+ }
190
+ const deliveries = existing
191
+ ? record.deliveries.map((entry) => (entry === existing ? next : entry))
192
+ : [...record.deliveries, next];
193
+ return { ...record, deliveries };
194
+ }
195
+
196
+ /**
197
+ * Settles a record against the repositories the change is scoped to: each needs its own
198
+ * delivery, so linking only one repository of a cross-repository change settles nothing.
199
+ */
200
+ export function settleDelivery(
201
+ record: DeliveryRecord | null,
202
+ scoped: readonly string[] = [],
203
+ ): DeliverySettlement {
204
+ if (!record) return { status: "legacy", unsettled: [], missing: [], unscoped: [] };
205
+ const missing = scoped.filter((repository) => !findDelivery(record, repository));
206
+ const unscoped = unscopedDeliveries(record, scoped);
207
+ if (record.deliveries.length === 0) {
208
+ // A ticket-only record was archived past Mate; it publishes like a legacy archive.
209
+ if (record.ticket && !record.skipped) {
210
+ return { status: "legacy", unsettled: [], missing: [], unscoped };
211
+ }
212
+ return { status: "unlinked", unsettled: missing, missing, unscoped };
213
+ }
214
+ const branches = record.deliveries.filter((entry) => entry.notApplicable === undefined);
215
+ const unsettled = [
216
+ ...branches.filter((entry) => entry.merge?.state !== "merged").map((entry) => entry.repository),
217
+ ...missing,
218
+ ];
219
+ if (unsettled.length === 0) {
220
+ const status = branches.length === 0 ? "not-applicable" : "merged";
221
+ return { status, unsettled, missing, unscoped };
222
+ }
223
+ const dropped = branches.some((entry) => entry.merge?.state === "closed-unmerged");
224
+ return { status: dropped ? "dropped" : "waiting", unsettled, missing, unscoped };
225
+ }
226
+
227
+ function unscopedDeliveries(record: DeliveryRecord, scoped: readonly string[]): UnscopedDelivery[] {
228
+ if (scoped.length === 0) return [];
229
+ return record.deliveries
230
+ .filter((entry) => !scoped.some((repository) => sameRepository(repository, entry.repository)))
231
+ .map((entry) => {
232
+ const closest = scoped.find((repository) => likelySame(repository, entry.repository));
233
+ return closest ? { repository: entry.repository, closest } : { repository: entry.repository };
234
+ });
235
+ }
236
+
237
+ /** Same last segment; this also covers one path being a suffix of the other (a dropped namespace). */
238
+ function likelySame(a: string, b: string): boolean {
239
+ return a.toLowerCase().split("/").at(-1) === b.toLowerCase().split("/").at(-1);
240
+ }
241
+
242
+ function malformed(file: string, reason: string): Error {
243
+ return new Error(`${file}: malformed ${DELIVERY_FILE}: ${reason}`);
244
+ }
245
+
246
+ function optionalString(value: unknown, file: string, field: string): string | undefined {
247
+ if (value === undefined || value === null) return undefined;
248
+ if (typeof value !== "string") throw malformed(file, `${field} must be a string`);
249
+ return value;
250
+ }
251
+
252
+ const MERGE_STATES: readonly MergeState[] = ["merged", "open", "closed-unmerged"];
253
+ const MERGE_SOURCES: readonly MergeSource[] = ["forge", "git", "user"];
254
+ /** A full SHA-1 or SHA-256 object name. */
255
+ const COMMIT_ID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
256
+
257
+ function parseMerge(value: unknown, file: string): MergeResult | undefined {
258
+ if (value === undefined || value === null) return undefined;
259
+ if (typeof value !== "object") throw malformed(file, "merge must be a mapping");
260
+ const merge = value as Record<string, unknown>;
261
+ if (!MERGE_STATES.includes(merge.state as MergeState)) {
262
+ throw malformed(file, `merge.state must be one of ${MERGE_STATES.join(", ")}`);
263
+ }
264
+ if (!MERGE_SOURCES.includes(merge.source as MergeSource)) {
265
+ throw malformed(file, `merge.source must be one of ${MERGE_SOURCES.join(", ")}`);
266
+ }
267
+ const result: MergeResult = {
268
+ state: merge.state as MergeState,
269
+ source: merge.source as MergeSource,
270
+ checkedAt: optionalString(merge.checkedAt, file, "merge.checkedAt") ?? "",
271
+ };
272
+ for (const key of ["mergedAt", "mergeCommit", "mergedBy"] as const) {
273
+ const field = optionalString(merge[key], file, `merge.${key}`);
274
+ if (field !== undefined) result[key] = field;
275
+ }
276
+ return result;
277
+ }
278
+
279
+ function parseDelivery(value: unknown, file: string): Delivery {
280
+ if (!value || typeof value !== "object") throw malformed(file, "a delivery must be a mapping");
281
+ const entry = value as Record<string, unknown>;
282
+ const repository = optionalString(entry.repository, file, "repository");
283
+ if (!repository) throw malformed(file, "every delivery names its repository");
284
+ const delivery: Delivery = { repository };
285
+ const branch = optionalString(entry.branch, file, "branch");
286
+ const pr = optionalString(entry.pr, file, "pr");
287
+ const notApplicable = optionalString(entry.notApplicable, file, "notApplicable");
288
+ if (branch !== undefined) delivery.branch = branch;
289
+ if (entry.direct === true) delivery.direct = true;
290
+ if (pr !== undefined) delivery.pr = pr;
291
+ const head = optionalString(entry.head, file, "head");
292
+ if (head !== undefined) {
293
+ if (!COMMIT_ID.test(head)) throw malformed(file, "head must be a full commit id");
294
+ delivery.head = head;
295
+ }
296
+ const merge = parseMerge(entry.merge, file);
297
+ if (merge) delivery.merge = merge;
298
+ if (notApplicable !== undefined) delivery.notApplicable = notApplicable;
299
+ return delivery;
300
+ }
301
+
302
+ /**
303
+ * Reads `delivery.yaml` from a change directory, active or archived. Null means no file:
304
+ * a legacy change. A file that exists but cannot be understood throws, so a damaged
305
+ * record is never mistaken for a legacy archive and published past the merge gate.
306
+ */
307
+ export async function readDeliveryRecord(changeDir: string): Promise<DeliveryRecord | null> {
308
+ const file = path.join(changeDir, DELIVERY_FILE);
309
+ let source: string;
310
+ try {
311
+ source = await fs.readFile(file, "utf8");
312
+ } catch (error) {
313
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
314
+ throw error;
315
+ }
316
+ let parsed: unknown;
317
+ try {
318
+ parsed = parse(source);
319
+ } catch (error) {
320
+ throw malformed(file, errorMessage(error));
321
+ }
322
+ if (!parsed || typeof parsed !== "object") throw malformed(file, "not a mapping");
323
+ const document = parsed as Record<string, unknown>;
324
+ const deliveries = document.deliveries ?? [];
325
+ if (!Array.isArray(deliveries)) throw malformed(file, "deliveries must be a list");
326
+ const ticket = optionalString(document.ticket, file, "ticket");
327
+ if (ticket !== undefined && !isJiraKey(ticket)) {
328
+ throw malformed(file, "ticket must be a Jira key such as ABC-123");
329
+ }
330
+ return {
331
+ version: 1,
332
+ change:
333
+ optionalString(document.change, file, "change") ?? changeNameOf(path.basename(changeDir)),
334
+ ...(ticket !== undefined ? { ticket } : {}),
335
+ ...(document.skipped === true ? { skipped: true as const } : {}),
336
+ deliveries: deliveries.map((entry) => parseDelivery(entry, file)),
337
+ };
338
+ }
339
+
340
+ /** Keys in file order: `version, change, ticket, skipped, deliveries`. */
341
+ function orderedRecord(record: DeliveryRecord): DeliveryRecord {
342
+ return {
343
+ version: record.version,
344
+ change: record.change,
345
+ ...(record.ticket ? { ticket: record.ticket } : {}),
346
+ ...(record.skipped ? { skipped: true as const } : {}),
347
+ deliveries: record.deliveries,
348
+ };
349
+ }
350
+
351
+ /**
352
+ * The change's record, or an unlinked one when none was written yet. The name comes from
353
+ * `change`, not the file, so a renamed change's record follows its directory.
354
+ */
355
+ export async function readOrUnlinkedRecord(
356
+ changeDir: string,
357
+ change: string,
358
+ ): Promise<DeliveryRecord> {
359
+ const name = changeNameOf(change);
360
+ const record = await readDeliveryRecord(changeDir);
361
+ return record ? { ...record, change: name } : unlinkedRecord(name);
362
+ }
363
+
364
+ /** A change's record with its settlement; `error` names a record that exists but cannot be read. */
365
+ export interface DeliveryState extends DeliverySettlement {
366
+ record: DeliveryRecord | null;
367
+ error?: string;
368
+ }
369
+
370
+ /** The change's settlement against its own scopes; see {@link settleDelivery}. */
371
+ export async function settleChangeDir(
372
+ changeDir: string,
373
+ record: DeliveryRecord | null,
374
+ ): Promise<DeliverySettlement> {
375
+ return settleDelivery(record, await scopedRepositories(changeDir));
376
+ }
377
+
378
+ /**
379
+ * Like {@link readDeliveryRecord}, settled against the change's scopes; a damaged record is
380
+ * reported as `unlinked` with its error.
381
+ */
382
+ export async function readDeliveryState(changeDir: string): Promise<DeliveryState> {
383
+ try {
384
+ const record = await readDeliveryRecord(changeDir);
385
+ return { ...(await settleChangeDir(changeDir, record)), record };
386
+ } catch (error) {
387
+ return {
388
+ status: "unlinked",
389
+ unsettled: [],
390
+ missing: [],
391
+ unscoped: [],
392
+ record: null,
393
+ error: errorMessage(error),
394
+ };
395
+ }
396
+ }
397
+
398
+ export async function writeDeliveryRecord(
399
+ changeDir: string,
400
+ record: DeliveryRecord,
401
+ ): Promise<void> {
402
+ await fs.writeFile(path.join(changeDir, DELIVERY_FILE), stringify(orderedRecord(record)), "utf8");
403
+ }