@uniqbit/mate-core 0.20.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (286) hide show
  1. package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +1 -1
  2. package/claude-capability-plugins/companion-mentions/hooks/register.ts +1 -1
  3. package/claude-plugin/hooks/hooks.json +10 -1
  4. package/claude-plugin/hooks/rtk-worktree-git.mjs +58 -0
  5. package/opencode-plugin/src/server.ts +9 -2
  6. package/package.json +2 -1
  7. package/src/cli/commands/artifact/delivery/environment.ts +28 -8
  8. package/src/cli/commands/artifact/delivery/link.ts +28 -9
  9. package/src/cli/commands/artifact/delivery/output.ts +7 -0
  10. package/src/cli/commands/companion/update.ts +2 -19
  11. package/src/cli/commands/factory/factory.ts +11 -1
  12. package/src/cli/commands/install.ts +47 -36
  13. package/src/cli/commands/launch/claude.ts +2 -0
  14. package/src/cli/commands/launch/shared.ts +33 -6
  15. package/src/cli/commands/plugin/install.ts +27 -11
  16. package/src/cli/commands/report/index.ts +180 -184
  17. package/src/cli/commands/report/markdown.ts +67 -72
  18. package/src/cli/commands/shared/companion-selection.ts +3 -1
  19. package/src/cli/commands/studio/access.ts +6 -14
  20. package/src/cli/commands/studio/areas.ts +112 -33
  21. package/src/cli/commands/studio/companion-add.ts +87 -0
  22. package/src/cli/commands/studio/companion-removal.ts +15 -8
  23. package/src/cli/commands/studio/factory-credential.ts +1 -15
  24. package/src/cli/commands/studio/factory-git-tokens.ts +40 -0
  25. package/src/cli/commands/studio/factory-relay.ts +18 -0
  26. package/src/cli/commands/studio/graph-model.ts +61 -0
  27. package/src/cli/commands/studio/graph.ts +336 -0
  28. package/src/cli/commands/studio/index.ts +4 -4
  29. package/src/cli/commands/studio/payload.ts +42 -15
  30. package/src/cli/commands/studio/repository-add.ts +116 -0
  31. package/src/cli/commands/studio/routes.ts +6 -0
  32. package/src/cli/commands/studio/selection.ts +12 -3
  33. package/src/cli/commands/studio/server.ts +127 -5
  34. package/src/cli/commands/studio/services.ts +40 -0
  35. package/src/cli/commands/studio/terminal.ts +45 -3
  36. package/src/cli/commands/studio/views/vault/markdown-tree.ts +84 -3
  37. package/src/cli/commands/studio/workset-session.ts +47 -14
  38. package/src/cli/commands/unwrap.ts +3 -0
  39. package/src/cli/commands/update.ts +2 -0
  40. package/src/cli/commands/vscode/release-vsix.json +4 -0
  41. package/src/cli/commands/vscode/setup.ts +71 -13
  42. package/src/cli/commands/vscode/vsix.ts +48 -7
  43. package/src/cli/commands/working/cleanup.ts +40 -8
  44. package/src/cli/commands/working/worktree.ts +176 -35
  45. package/src/cli/main.ts +17 -2
  46. package/src/cli/usage.ts +1 -1
  47. package/src/hooks/rtk-worktree-git.ts +48 -0
  48. package/src/hooks/validate-artifact-path.ts +38 -7
  49. package/src/lib/delivery/branch-template.ts +122 -0
  50. package/src/lib/delivery/branches.ts +21 -16
  51. package/src/lib/delivery/checkout.ts +102 -0
  52. package/src/lib/delivery/environment.ts +2 -0
  53. package/src/lib/delivery/merge-git.ts +13 -1
  54. package/src/lib/delivery/record-delivery.ts +83 -29
  55. package/src/lib/delivery/record.ts +15 -3
  56. package/src/lib/delivery/resolve-branch.ts +157 -110
  57. package/src/lib/factory/attach-client.ts +11 -1
  58. package/src/lib/factory/broker.ts +37 -40
  59. package/src/lib/factory/client.ts +22 -1
  60. package/src/lib/factory/clone-url.ts +39 -0
  61. package/src/lib/factory/command.ts +21 -5
  62. package/src/lib/factory/git-auth.ts +61 -0
  63. package/src/lib/factory/git-credential-store.ts +181 -0
  64. package/src/lib/factory/ids.ts +12 -2
  65. package/src/lib/factory/outcomes/actions.ts +20 -4
  66. package/src/lib/factory/outcomes/index.ts +77 -11
  67. package/src/lib/factory/outcomes/lifecycle.ts +9 -2
  68. package/src/lib/factory/outcomes/sessions.ts +23 -1
  69. package/src/lib/factory/prepare.ts +9 -3
  70. package/src/lib/factory/runner/scheduler.ts +96 -14
  71. package/src/lib/factory/runner/workflow.ts +5 -1
  72. package/src/lib/factory/runtime.ts +143 -43
  73. package/src/lib/orchestrator/companion-hub.ts +6 -1
  74. package/src/lib/orchestrator/engine-floor.ts +48 -0
  75. package/src/lib/orchestrator/launcher.ts +34 -1
  76. package/src/lib/orchestrator/projection-claude-commands.ts +2 -2
  77. package/src/lib/orchestrator/projection-claude-entry.ts +42 -21
  78. package/src/lib/orchestrator/projection-claude-links.ts +32 -8
  79. package/src/lib/orchestrator/projection-claude-skills.ts +2 -2
  80. package/src/lib/orchestrator/projection-entries.ts +22 -18
  81. package/src/lib/orchestrator/projection-opencode-copies.ts +65 -21
  82. package/src/lib/orchestrator/projection-runtime-documents.ts +66 -43
  83. package/src/lib/orchestrator/projection-types.ts +28 -1
  84. package/src/lib/orchestrator/refresh-working-claude-links.ts +3 -1
  85. package/src/lib/orchestrator/session-prepare.ts +6 -1
  86. package/src/lib/orchestrator/setup-compatibilities.ts +1 -1
  87. package/src/lib/orchestrator/types.ts +4 -0
  88. package/src/lib/orchestrator/umbrella-checkouts.ts +102 -0
  89. package/src/lib/orchestrator/working-repo-projection.ts +24 -4
  90. package/src/lib/orchestrator/wrap-pass.ts +1 -0
  91. package/src/lib/public-npm.ts +95 -50
  92. package/src/lib/session-state.ts +72 -2
  93. package/src/lib/update-checker.ts +7 -1
  94. package/src/lib/usage/aggregate.ts +202 -0
  95. package/src/lib/usage/cache.ts +87 -0
  96. package/src/lib/usage/claude-pricing.ts +71 -0
  97. package/src/lib/usage/claude.ts +189 -0
  98. package/src/lib/usage/collect.ts +56 -0
  99. package/src/lib/usage/opencode.ts +188 -0
  100. package/src/lib/usage/records.ts +80 -0
  101. package/src/lib/usage/rtk-database.ts +72 -0
  102. package/src/lib/usage/savings.ts +209 -0
  103. package/src/lib/usage/scope.ts +182 -0
  104. package/src/lib/usage/source.ts +102 -0
  105. package/src/lib/usage/types.ts +143 -0
  106. package/src/lib/usage/window.ts +37 -0
  107. package/src/lib/workset/factory-local.ts +157 -0
  108. package/src/lib/workset/record.ts +25 -11
  109. package/src/lib/workset/repositories.ts +118 -25
  110. package/src/lib/workset/worktree-include.ts +161 -0
  111. package/src/lib/workset/worktree-index.ts +6 -2
  112. package/src/lib/workset/worktree.ts +591 -101
  113. package/src/opencode/companion-hooks.ts +46 -8
  114. package/src/opencode/companion-policy.ts +18 -5
  115. package/src/opencode/enter-worktree.ts +254 -6
  116. package/src/opencode/session-register.ts +2 -2
  117. package/src/runtime/claude-bg-isolation.ts +138 -0
  118. package/src/runtime/companion-git.ts +10 -2
  119. package/src/runtime/companion-guidance.ts +17 -4
  120. package/src/runtime/current-branch.ts +16 -2
  121. package/src/runtime/entered-worktree.ts +24 -0
  122. package/src/runtime/git-tracked.ts +60 -0
  123. package/src/runtime/repo-destination.ts +72 -0
  124. package/src/runtime/repo-local.ts +25 -3
  125. package/src/runtime/rtk-git-guard.ts +66 -0
  126. package/src/runtime/runtime-document-manifest.ts +229 -19
  127. package/src/runtime/runtime-document-manifest.typecheck.ts +10 -0
  128. package/src/runtime/secret-guard.ts +9 -2
  129. package/src/runtime/secret-policy-weakening.ts +122 -0
  130. package/src/runtime/serving-claude-wrap.ts +15 -2
  131. package/src/runtime/umbrella-folder.ts +56 -0
  132. package/src/runtime/wrap-state.ts +5 -13
  133. package/src/studio.ts +37 -0
  134. package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +61 -6
  135. package/src/templates/mate-skills/agents/mate-explore-change/SKILL.md +8 -4
  136. package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +9 -5
  137. package/src/templates/mate-skills/agents/mate-publish-artifact/SKILL.md +5 -4
  138. package/src/templates/mate-skills/agents/mate-show-me/SKILL.md +10 -4
  139. package/src/templates/mate-skills/agents/mate-simplify-code/SKILL.md +5 -1
  140. package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +61 -6
  141. package/src/templates/mate-skills/claude/mate-explore-change/SKILL.md +8 -4
  142. package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +9 -5
  143. package/src/templates/mate-skills/claude/mate-publish-artifact/SKILL.md +5 -4
  144. package/src/templates/mate-skills/claude/mate-show-me/SKILL.md +10 -4
  145. package/src/templates/mate-skills/claude/mate-simplify-code/SKILL.md +5 -1
  146. package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +305 -42
  147. package/src/tools/setup/capabilities/rtk.ts +93 -1
  148. package/src/tools/setup/dynamic-plugins/audiences.ts +65 -21
  149. package/src/tools/setup/dynamic-plugins/frozen.ts +32 -13
  150. package/src/tools/setup/dynamic-plugins/hydrate.ts +4 -4
  151. package/src/tools/setup/dynamic-plugins/install.ts +24 -22
  152. package/src/tools/setup/dynamic-plugins/loader.ts +11 -7
  153. package/src/tools/setup/dynamic-plugins/paths.ts +30 -10
  154. package/src/tools/setup/dynamic-plugins/verify.ts +10 -7
  155. package/src/tools/setup/dynamic-plugins/workspaces.ts +153 -0
  156. package/src/tools/setup/mate.ts +3 -3
  157. package/src/tools/setup/plugins/gitignore.ts +15 -9
  158. package/src/tools/setup/providers/claude-plugin-hooks.ts +12 -0
  159. package/src/tools/setup/providers/claude.ts +25 -3
  160. package/src/tools/setup/utils.ts +4 -2
  161. package/src/tools/setup/working-repo-cleanup.ts +2 -2
  162. package/src/tools/setup.ts +25 -14
  163. package/studio-dist/client/assets/app-D9cw6szy.css +2 -0
  164. package/studio-dist/client/assets/artifacts-WD2ZXYrx.js +1 -0
  165. package/studio-dist/client/assets/button-Br0O1sXw.js +1 -0
  166. package/studio-dist/client/assets/combobox-DA7RAUUZ.js +1 -0
  167. package/studio-dist/client/assets/companion-view-CrabvxS8.js +1 -0
  168. package/studio-dist/client/assets/factory-DGS0E16C.js +6 -0
  169. package/studio-dist/client/assets/fields-6aXBpFhJ.js +1 -0
  170. package/studio-dist/client/assets/getting-started-Clde4w6P.js +1 -0
  171. package/studio-dist/client/assets/getting-started-D6kgWwWZ.js +1 -0
  172. package/studio-dist/client/assets/getting-started._topic-BiunccbT.js +1 -0
  173. package/studio-dist/client/assets/getting-started.index-DpiNoGw8.js +1 -0
  174. package/studio-dist/client/assets/graph-B_TCBVbu.js +2 -0
  175. package/studio-dist/client/assets/graph-canvas-Ba37Y5AE.js +3242 -0
  176. package/studio-dist/client/assets/guides-Du_RtJxP.js +1 -0
  177. package/studio-dist/client/assets/index-Df8L1LQa.js +40 -0
  178. package/studio-dist/client/assets/jsx-runtime-MZURdZre.js +1 -0
  179. package/studio-dist/client/assets/markdown-view-BOX6x0rL.js +1 -0
  180. package/studio-dist/client/assets/panel-dbmCx7Mx.js +1 -0
  181. package/studio-dist/client/assets/queries-RvHeXY12.js +1 -0
  182. package/studio-dist/client/assets/react-3QStrcDO.js +27 -0
  183. package/studio-dist/client/assets/report-url-XRL0x1SY.js +1 -0
  184. package/studio-dist/client/assets/reports-r3IK7Doz.js +1 -0
  185. package/studio-dist/client/assets/routes-C0A5LTFc.js +1 -0
  186. package/studio-dist/client/assets/select-CnaMd617.js +1 -0
  187. package/studio-dist/client/assets/skills-CzjzHpBT.js +1 -0
  188. package/studio-dist/client/assets/specs-B0ldF_6J.js +1 -0
  189. package/studio-dist/client/assets/specs-view-BnCLA0gW.js +1 -0
  190. package/studio-dist/client/assets/spinner-Dcm3fqK1.js +1 -0
  191. package/studio-dist/client/assets/steps-C0p2N1Pb.js +1 -0
  192. package/studio-dist/client/assets/store-Dw34catY.js +1 -0
  193. package/studio-dist/client/assets/studio-Cpz_HRHp.js +100 -0
  194. package/studio-dist/client/assets/studio-queries-ibmpFo6R.js +2 -0
  195. package/studio-dist/client/assets/usage-C2QJ_0IZ.js +55 -0
  196. package/studio-dist/client/assets/useAnchoredPopupScrollLock-Bj-FWb8z.js +1 -0
  197. package/studio-dist/client/assets/useMutation-aAr2uiay.js +1 -0
  198. package/studio-dist/client/assets/useSelector-AYcH7lB0.js +2 -0
  199. package/studio-dist/client/assets/vault-BGcxkSIY.js +1 -0
  200. package/studio-dist/client/assets/warnings-R1cE1rLf.js +1 -0
  201. package/studio-dist/client/assets/workflow-C2qTrMDH.js +1 -0
  202. package/studio-dist/server/assets/__23tanstack-start-server-fn-resolver-Bn896ZYj.js +122 -0
  203. package/studio-dist/server/assets/_tanstack-start-manifest_v.js +102 -35
  204. package/studio-dist/server/assets/{actions-Bvgj9eb8.js → actions-DYSv-9VV.js} +10 -4
  205. package/studio-dist/server/assets/artifacts-DnF9L3dZ.js +216 -0
  206. package/studio-dist/server/assets/{button-CIJqAhni.js → button-CgOIVdmD.js} +13 -1702
  207. package/studio-dist/server/assets/combobox-DV_s9W0s.js +483 -0
  208. package/studio-dist/server/assets/{companion-view-DISASvdJ.js → companion-view-BKVXD3TO.js} +3 -3
  209. package/studio-dist/server/assets/companions-C7fctnU9.js +31 -0
  210. package/studio-dist/server/assets/{createServerRpc-tK2lE9R2.js → createServerRpc-bFKFpGFG.js} +1 -1
  211. package/studio-dist/server/assets/{factory-DUTa0eDs.js → factory-C7Ybg0xG.js} +1256 -353
  212. package/studio-dist/server/assets/{factory-DGPmNGbo.js → factory-Df5uAHjb.js} +53 -6
  213. package/studio-dist/server/assets/fields-BbieQTD0.js +1190 -0
  214. package/studio-dist/server/assets/getting-started-B136TP57.js +4 -0
  215. package/studio-dist/server/assets/getting-started-CRxMWorQ.js +67 -0
  216. package/studio-dist/server/assets/getting-started._topic-DJKQAzYl.js +13 -0
  217. package/studio-dist/server/assets/getting-started.index-D84gec1t.js +13 -0
  218. package/studio-dist/server/assets/graph-CSje3029.js +801 -0
  219. package/studio-dist/server/assets/graph-DDZzXM1L.js +16 -0
  220. package/studio-dist/server/assets/graph-canvas-22bWB5-2.js +21066 -0
  221. package/studio-dist/server/assets/graph-wire-BdSUx0G5.js +58 -0
  222. package/studio-dist/server/assets/guides-D9dk1Frq.js +13 -0
  223. package/studio-dist/server/assets/guides-IE-19POz.js +5 -0
  224. package/studio-dist/server/assets/guides.server-vcYRY5wx.js +66 -0
  225. package/studio-dist/server/assets/jsx-runtime-Bt0s0YKS.js +451 -0
  226. package/studio-dist/server/assets/{markdown-view-Bn7xPdks.js → markdown-view-BR8dJ7aR.js} +28 -9
  227. package/studio-dist/server/assets/{panel-3Jmn4awF.js → panel-BR_t9iMM.js} +4 -4
  228. package/studio-dist/server/assets/queries-DWoTin-q.js +32 -0
  229. package/studio-dist/server/assets/react-CdqSUhaL.js +89 -0
  230. package/studio-dist/server/assets/redirect-BaZ_QyqF.js +39 -0
  231. package/studio-dist/server/assets/report-url-BUXPcE_8.js +9 -0
  232. package/studio-dist/server/assets/{reports-D6YMqhnO.js → reports-CQ9qQ1-n.js} +9 -10
  233. package/studio-dist/server/assets/{reports-C06RXVE2.js → reports-dYDEoBeq.js} +3 -3
  234. package/studio-dist/server/assets/{router-C8MGqlAY.js → router-DkRHNTpE.js} +787 -2862
  235. package/studio-dist/server/assets/{routes-BcFgvqf7.js → routes-B6GfOew4.js} +22 -171
  236. package/studio-dist/server/assets/{search-JAKurDNh.js → search-BJnWLfkG.js} +21 -2
  237. package/studio-dist/server/assets/select-Af7Wibii.js +1609 -0
  238. package/studio-dist/server/assets/{shell-DS3wUexh.js → shell-Hq-hXgnT.js} +6 -5
  239. package/studio-dist/server/assets/{skills-BEnOxiDl.js → skills-B7nEuVbx.js} +9 -8
  240. package/studio-dist/server/assets/{skills-CtrBAtX3.js → skills-B_YYL7q8.js} +3 -3
  241. package/studio-dist/server/assets/specs-BpLUFntA.js +51 -0
  242. package/studio-dist/server/assets/specs-view-0peMVd22.js +426 -0
  243. package/studio-dist/server/assets/spinner-CdwJW4VB.js +20 -0
  244. package/studio-dist/server/assets/{steps-Dq_CwY_s.js → steps-D56DApvK.js} +1 -1
  245. package/studio-dist/server/assets/store-Cs17okNG.js +280 -0
  246. package/studio-dist/server/assets/studio-CAcKp3bM.js +10340 -0
  247. package/studio-dist/server/assets/studio-queries-BtRGzDdh.js +1451 -0
  248. package/studio-dist/server/assets/usage-COdjd1gD.js +25570 -0
  249. package/studio-dist/server/assets/usage-f6pqE1tm.js +24 -0
  250. package/studio-dist/server/assets/useAnchoredPopupScrollLock-DlBKHRw7.js +756 -0
  251. package/studio-dist/server/assets/useMutation-DzTZGenE.js +690 -0
  252. package/studio-dist/server/assets/{__23tanstack-start-server-fn-resolver-M2GBs8s3.js → useSelector-DUmCUXAs.js} +4 -668
  253. package/studio-dist/server/assets/{vault-Fvxt2Tzl.js → vault-BDuIUq0F.js} +8 -5
  254. package/studio-dist/server/assets/{vault-BmWnryh3.js → vault-BVLp6zu3.js} +3 -3
  255. package/studio-dist/server/assets/{views-BgYa-za2.js → views-flRUr21z.js} +4 -4
  256. package/studio-dist/server/assets/{warnings-DzR6fyZh.js → warnings-CDy9pdgL.js} +3 -2
  257. package/studio-dist/server/assets/{delivery-path-C29fBynY.js → workflow-D9GKI2EV.js} +132 -106
  258. package/studio-dist/server/server.js +582 -8
  259. package/src/cli/commands/report/collector.ts +0 -288
  260. package/studio-dist/client/assets/app-CdPiFv59.css +0 -2
  261. package/studio-dist/client/assets/button-BIIHE0EQ.js +0 -3
  262. package/studio-dist/client/assets/companion-view-DM6YyXaR.js +0 -1
  263. package/studio-dist/client/assets/delivery-path-DELxj0Av.js +0 -1
  264. package/studio-dist/client/assets/factory-BZy0E8ty.js +0 -4
  265. package/studio-dist/client/assets/index-B3wkMzjA.js +0 -40
  266. package/studio-dist/client/assets/markdown-view-pQ2Qp_i5.js +0 -1
  267. package/studio-dist/client/assets/panel-CbajlC3p.js +0 -1
  268. package/studio-dist/client/assets/prepaint-CwSrpdFA.js +0 -27
  269. package/studio-dist/client/assets/react-CN2GSx7x.js +0 -1
  270. package/studio-dist/client/assets/reports-DllhYQ5_.js +0 -1
  271. package/studio-dist/client/assets/rolldown-runtime-WkwZ5CU-.js +0 -1
  272. package/studio-dist/client/assets/routes-COm-G3mK.js +0 -1
  273. package/studio-dist/client/assets/skills-DmWAy-jZ.js +0 -1
  274. package/studio-dist/client/assets/specs-BG3Wi3ln.js +0 -1
  275. package/studio-dist/client/assets/steps-DFsH3Neu.js +0 -1
  276. package/studio-dist/client/assets/vault-B2q1nBY5.js +0 -1
  277. package/studio-dist/client/assets/warnings-BSXIyuSQ.js +0 -1
  278. package/studio-dist/client/assets/workflow-DlsQD6Hc.js +0 -1
  279. package/studio-dist/server/assets/companions-BYbHWKOZ.js +0 -15
  280. package/studio-dist/server/assets/createServerFn-F5sAOF8V.js +0 -459
  281. package/studio-dist/server/assets/prepaint-DJfTQ_VH.js +0 -45
  282. package/studio-dist/server/assets/react-Ce6dj15C.js +0 -46
  283. package/studio-dist/server/assets/rolldown-runtime-DYf_mQCB.js +0 -34
  284. package/studio-dist/server/assets/specs-C8rv34-5.js +0 -211
  285. package/studio-dist/server/assets/studio-BeltxxKt.js +0 -216
  286. package/studio-dist/server/assets/workflow-liRYecHk.js +0 -73
@@ -1,50 +1,41 @@
1
1
  import { locateChange } from "../change-layout";
2
- import type { MergeGit } from "./merge-git";
2
+ import { companionBranchTemplate } from "./branches";
3
+ import type { DeliveryCheckout, HasRef, ListRefs } from "./checkout";
3
4
  import { declaredDependencies } from "./overlap";
4
- import { localMergeEvidence } from "./refresh";
5
5
  import {
6
6
  findWorkset,
7
7
  resolveWorksetBranch,
8
8
  resolveWorksetOf,
9
9
  type WorksetRecord,
10
10
  } from "../workset/record";
11
+ import {
12
+ branchTemplatePrefix,
13
+ DEFAULT_BRANCH_TEMPLATE,
14
+ matchBranchTemplate,
15
+ type BranchTemplate,
16
+ } from "./branch-template";
11
17
  import {
12
18
  defaultDeliveryBranch,
13
19
  findDelivery,
14
- isJiraKey,
15
20
  readDeliveryRecord,
16
21
  type DeliveryRecord,
17
22
  } from "./record";
18
23
 
19
- /** True when the ref resolves in the repository's checkout. */
20
- export type HasRef = (ref: string) => Promise<boolean>;
21
-
22
- /** Full names of the refs under the given prefixes, e.g. `refs/heads/workset/`. */
23
- export type ListRefs = (prefixes: string[]) => Promise<string[]>;
24
-
25
24
  /** Where the resolved branch lives: a local branch, only `origin/<branch>`, or nowhere yet. */
26
25
  export type BranchSource = "local" | "remote" | "new";
27
26
 
28
- export interface DeliveryBranchRequest {
27
+ /** What a Delivery Branch is resolved for: a change, or a Workset by id. */
28
+ export type DeliverySubject = {
29
29
  companionPath: string;
30
30
  /** Null when no repository is known; recorded branches are then not consulted. */
31
31
  repository: string | null;
32
- /** Null when no checkout is known: the computed name, with `source` null. */
33
- hasRef: HasRef | null;
34
- /**
35
- * Finds an existing `workset/<TICKET>-<id>` when no ticket is given; without it only
36
- * `workset/<id>` is probed.
37
- */
38
- listRefs?: ListRefs | null;
39
- /**
40
- * Checks a dependency never checked for a merge before its branch is inherited; without it
41
- * such a branch is inherited with a diagnostic asking for a refresh.
42
- */
43
- mergeProbe?: MergeProbe | null;
32
+ } & ({ change: string } | { worksetId: string });
33
+
34
+ export interface DeliveryBranchOptions {
44
35
  /**
45
- * Names the branch `workset/<TICKET>-<id>`, even over an existing `workset/<id>`; never
46
- * renames a recorded or inherited one. Defaults to the explicit Workset record's ticket,
47
- * which yields to any existing `workset/<id>` or `workset/<TICKET>-<id>`.
36
+ * Names the branch with the template and this ticket, even over an existing branch without
37
+ * one; never renames a recorded or inherited one. Defaults to the explicit Workset record's
38
+ * ticket, which yields to any existing branch the template names for the Workset.
48
39
  */
49
40
  ticket?: string | null;
50
41
  }
@@ -57,6 +48,8 @@ export interface DeliveryBranchResolution {
57
48
  /** The `dependsOn` changes whose branch `branch` is; empty when it is not inherited. */
58
49
  inheritedFrom: string[];
59
50
  diagnostics: string[];
51
+ /** Set when the companion's branch template is invalid and the default applies. */
52
+ templateDiagnostic?: string;
60
53
  }
61
54
 
62
55
  interface Member {
@@ -92,47 +85,60 @@ async function sourceOf(branch: string, hasRef: HasRef): Promise<BranchSource> {
92
85
  return "new";
93
86
  }
94
87
 
95
- const WORKSET_REF = /^refs\/(?:heads|remotes\/origin)\/workset\//;
88
+ const BRANCH_REF = /^refs\/(?:heads|remotes\/origin)\//;
96
89
 
97
- /** The key of `workset/<TICKET>-<id>`, else null. */
98
- function ticketKeyOf(branch: string, worksetId: string): string | null {
99
- const prefix = "workset/";
100
- const suffix = `-${worksetId}`;
101
- if (!branch.startsWith(prefix) || !branch.endsWith(suffix)) return null;
102
- const key = branch.slice(prefix.length, -suffix.length);
103
- return isJiraKey(key) ? key : null;
90
+ /** The ticket key the template put into `branch` for the Workset, else null. */
91
+ function ticketKeyOf(branch: string, worksetId: string, template: BranchTemplate): string | null {
92
+ return matchBranchTemplate(template, branch, worksetId)?.ticket ?? null;
104
93
  }
105
94
 
106
- /** Existing `workset/<TICKET>-<id>` branches, local or on `origin`, for any ticket. */
107
- async function ticketedBranches(worksetId: string, listRefs: ListRefs): Promise<string[]> {
108
- const refs = await listRefs(["refs/heads/workset/", "refs/remotes/origin/workset/"]);
95
+ /** Existing branches the template names for the Workset with any ticket, local or on `origin`. */
96
+ async function ticketedBranches(
97
+ worksetId: string,
98
+ listRefs: ListRefs,
99
+ template: BranchTemplate,
100
+ ): Promise<string[]> {
101
+ if (!template.includes("{ticket}")) return [];
102
+ const prefix = branchTemplatePrefix(template);
103
+ const refs = await listRefs([`refs/heads/${prefix}`, `refs/remotes/origin/${prefix}`]);
109
104
  return distinct(
110
105
  refs.map((ref) => {
111
- if (!WORKSET_REF.test(ref)) return undefined;
112
- const branch = `workset/${ref.replace(WORKSET_REF, "")}`;
113
- return ticketKeyOf(branch, worksetId) ? branch : undefined;
106
+ if (!BRANCH_REF.test(ref)) return undefined;
107
+ const branch = ref.replace(BRANCH_REF, "");
108
+ return ticketKeyOf(branch, worksetId, template) ? branch : undefined;
114
109
  }),
115
110
  ).toSorted();
116
111
  }
117
112
 
118
- /** A local checkout's merge check, without fetch or forge; see {@link mergeProbeWith}. */
119
- export type MergeProbe = (
120
- branch: string,
121
- recordedHead: string | undefined,
122
- ) => Promise<"merged" | "squashed" | "open">;
113
+ type Existing = { branch: string; source: BranchSource } | "ambiguous" | null;
123
114
 
124
115
  /**
125
- * A {@link MergeProbe} over local Git: a merge commit settles `merged`, commits matched on the
126
- * default branch read as `squashed`. A head on the default branch's own line is `open`: it
127
- * may be a fast-forward, but then its commits are already there and continuing on it is safe.
116
+ * An existing branch the template names for the Workset: the one without a ticket, else the
117
+ * single one with any ticket; `ambiguous` for several, reported in `diagnostics`. `probed` is a
118
+ * branch already known not to exist.
128
119
  */
129
- export function mergeProbeWith(git: MergeGit, defaultBranch: string): MergeProbe {
130
- return async (branch, recordedHead) => {
131
- const evidence = await localMergeEvidence(git, defaultBranch, branch, recordedHead);
132
- if (evidence.merged) return "merged";
133
- const style = evidence.suggestion?.style;
134
- return style === "squash" || style === "rebase" ? "squashed" : "open";
135
- };
120
+ async function existingBranch(
121
+ worksetId: string,
122
+ template: BranchTemplate,
123
+ { hasRef, listRefs }: DeliveryCheckout,
124
+ diagnostics: string[],
125
+ probed?: string,
126
+ ): Promise<Existing> {
127
+ const plain = defaultDeliveryBranch(worksetId, null, template);
128
+ if (plain !== probed) {
129
+ const source = await sourceOf(plain, hasRef);
130
+ if (source !== "new") return { branch: plain, source };
131
+ }
132
+ // The ticket lives only in the branch name, so a branch created with one is found by name.
133
+ const ticketed = await ticketedBranches(worksetId, listRefs, template);
134
+ if (ticketed.length > 1) {
135
+ diagnostics.push(
136
+ `Workset ${worksetId} has several ticket branches (${ticketed.join(", ")}); select a branch explicitly.`,
137
+ );
138
+ return "ambiguous";
139
+ }
140
+ if (ticketed.length === 0) return null;
141
+ return { branch: ticketed[0]!, source: await sourceOf(ticketed[0]!, hasRef) };
136
142
  }
137
143
 
138
144
  const SETTLED: ReadonlySet<string> = new Set(["merged", "closed-unmerged"]);
@@ -147,14 +153,14 @@ interface Inherited {
147
153
  /**
148
154
  * The branches the Workset's direct `dependsOn` changes deliver in `repository` that are still
149
155
  * open (not direct, not merged or dropped, present when refs can be probed), each with the
150
- * dependencies recording it. A dependency never checked for a merge is probed locally when
151
- * `mergeProbe` is given; one that looks merged is skipped and named in `diagnostics`.
156
+ * dependencies recording it. A dependency never checked for a merge is probed in the
157
+ * checkout; one that looks merged is skipped and named in `diagnostics`.
152
158
  */
153
159
  async function inheritedBranches(
154
160
  companionPath: string,
155
161
  found: Member[],
156
162
  repository: string,
157
- { hasRef, mergeProbe }: Pick<DeliveryBranchRequest, "hasRef" | "mergeProbe">,
163
+ checkout: DeliveryCheckout | null,
158
164
  diagnostics: string[],
159
165
  ): Promise<Inherited[]> {
160
166
  const own = new Set(found.map((member) => member.change));
@@ -169,10 +175,10 @@ async function inheritedBranches(
169
175
  return null;
170
176
  }
171
177
  const { branch } = delivery;
172
- if (hasRef && (await sourceOf(branch, hasRef)) === "new") return null;
178
+ if (checkout && (await sourceOf(branch, checkout.hasRef)) === "new") return null;
173
179
  if (delivery.merge !== undefined) return { name, branch, unchecked: false };
174
- if (!mergeProbe) return { name, branch, unchecked: true };
175
- const probed = await mergeProbe(branch, delivery.head);
180
+ const probed = checkout ? await checkout.mergeProbe(branch, delivery.head) : null;
181
+ if (probed === null) return { name, branch, unchecked: true };
176
182
  if (probed === "open") return { name, branch, unchecked: false };
177
183
  diagnostics.push(
178
184
  probed === "merged"
@@ -199,17 +205,26 @@ async function inheritedBranches(
199
205
 
200
206
  /** Branch order for a Workset whose identity is already checked. */
201
207
  async function branchOf(
202
- request: DeliveryBranchRequest,
208
+ checkout: DeliveryCheckout | null,
209
+ { companionPath, repository }: DeliverySubject,
210
+ options: DeliveryBranchOptions,
211
+ template: BranchTemplate,
203
212
  worksetId: string,
204
213
  ): Promise<DeliveryBranchResolution> {
205
- const { companionPath, repository, hasRef, listRefs } = request;
206
214
  const workset = await findWorkset(companionPath, worksetId);
207
- const ticket = request.ticket ?? workset?.ticket ?? null;
208
- // The branch the ticket names; `workset/<id>` without one.
209
- const named = defaultDeliveryBranch(worksetId, ticket);
215
+ const ticket = options.ticket ?? workset?.ticket ?? null;
216
+ // The template's branch with the ticket; without it when there is none.
217
+ const named = defaultDeliveryBranch(worksetId, ticket, template);
218
+ // The same under the default template, for branches made before the team switched templates.
219
+ const defaultNamed = defaultDeliveryBranch(worksetId, ticket);
210
220
  const found = await members(companionPath, worksetId, workset);
211
221
  const records = found.flatMap((member) => (member.record ? [member.record] : []));
212
222
  const diagnostics: string[] = [];
223
+ if (options.ticket && !template.includes("{ticket}")) {
224
+ diagnostics.push(
225
+ `Ticket ${options.ticket} is not in the branch name: branchTemplate ${template} has no {ticket}.`,
226
+ );
227
+ }
213
228
  const unresolved = (): DeliveryBranchResolution => ({
214
229
  branch: null,
215
230
  source: null,
@@ -220,14 +235,14 @@ async function branchOf(
220
235
  branch: string,
221
236
  { source, inheritedFrom = [] }: { source?: BranchSource; inheritedFrom?: string[] } = {},
222
237
  ): Promise<DeliveryBranchResolution> => {
223
- if (ticket && branch !== named) {
238
+ if (ticket && branch !== named && branch !== defaultNamed) {
224
239
  diagnostics.push(
225
240
  `Ticket ${ticket} does not name the branch: ${branch} already delivers Workset ${worksetId}.`,
226
241
  );
227
242
  }
228
243
  return {
229
244
  branch,
230
- source: source ?? (hasRef ? await sourceOf(branch, hasRef) : null),
245
+ source: source ?? (checkout ? await sourceOf(branch, checkout.hasRef) : null),
231
246
  inheritedFrom,
232
247
  diagnostics,
233
248
  };
@@ -248,9 +263,11 @@ async function branchOf(
248
263
  }
249
264
  if (recorded.length === 1) {
250
265
  const branch = recorded[0]!;
251
- const key = ticketKeyOf(branch, worksetId);
266
+ const key =
267
+ ticketKeyOf(branch, worksetId, template) ??
268
+ ticketKeyOf(branch, worksetId, DEFAULT_BRANCH_TEMPLATE);
252
269
  // Only the branch name carries a ticket, so a wrong one is fixed by renaming the branch.
253
- if (request.ticket && key && key.toUpperCase() !== request.ticket.toUpperCase()) {
270
+ if (options.ticket && key && key.toUpperCase() !== options.ticket.toUpperCase()) {
254
271
  const changes = found.map((member) => member.change).join(" ");
255
272
  diagnostics.push(
256
273
  `To change the ticket of ${branch} before it is pushed, run \`git branch -m ${branch} ${named}\`, then \`mate artifact link ${changes} --branch ${named}\`.`,
@@ -263,7 +280,7 @@ async function branchOf(
263
280
  companionPath,
264
281
  found,
265
282
  repository,
266
- request,
283
+ checkout,
267
284
  diagnostics,
268
285
  );
269
286
  if (inherited.length > 1) {
@@ -283,63 +300,82 @@ async function branchOf(
283
300
  }
284
301
  }
285
302
 
286
- const plain = defaultDeliveryBranch(worksetId);
287
- if (!hasRef) return resolved(named);
303
+ const plain = defaultDeliveryBranch(worksetId, null, template);
304
+ const followsDefault = (branch: string, source: BranchSource) => {
305
+ diagnostics.push(
306
+ `${branch} follows the default template ${DEFAULT_BRANCH_TEMPLATE}, not branchTemplate ${template}; it is used because it already exists.`,
307
+ );
308
+ return resolved(branch, { source });
309
+ };
310
+ if (!checkout) return resolved(named);
311
+ const { hasRef } = checkout;
288
312
  if (ticket) {
289
313
  const source = await sourceOf(named, hasRef);
290
314
  if (source !== "new") return resolved(named, { source });
315
+ if (defaultNamed !== named) {
316
+ const defaultSource = await sourceOf(defaultNamed, hasRef);
317
+ if (defaultSource !== "new") return followsDefault(defaultNamed, defaultSource);
318
+ }
291
319
  // An explicit ticket asks for its own branch; a stale plain one must not take its place.
292
- if (request.ticket) {
293
- if ((await sourceOf(plain, hasRef)) !== "new") {
294
- diagnostics.push(
295
- `Existing ${plain} is not used: ticket ${ticket} names ${named}; record ${plain} with --branch to keep it.`,
296
- );
320
+ if (options.ticket) {
321
+ const stale = distinct([plain, defaultDeliveryBranch(worksetId)]).filter(
322
+ (branch) => branch !== named,
323
+ );
324
+ for (const branch of stale) {
325
+ // oxlint-disable-next-line no-await-in-loop -- at most two probes, reported in order
326
+ if ((await sourceOf(branch, hasRef)) !== "new") {
327
+ diagnostics.push(
328
+ `Existing ${branch} is not used: ticket ${ticket} names ${named}; record ${branch} with --branch to keep it.`,
329
+ );
330
+ }
297
331
  }
298
332
  return resolved(named, { source });
299
333
  }
300
334
  // A Workset record's ticket only names a branch none of whose forms exists yet.
301
335
  }
302
- const plainSource = await sourceOf(plain, hasRef);
303
- if (plainSource !== "new") return resolved(plain, { source: plainSource });
304
- if (!listRefs) return resolved(named, { source: "new" });
305
- // The ticket lives only in the branch name, so a branch created with one is found by name.
306
- const ticketed = await ticketedBranches(worksetId, listRefs);
307
- if (ticketed.length > 1) {
308
- diagnostics.push(
309
- `Workset ${worksetId} has several ticket branches (${ticketed.join(", ")}); select a branch explicitly.`,
336
+ const existing = await existingBranch(worksetId, template, checkout, diagnostics);
337
+ if (existing === "ambiguous") return unresolved();
338
+ if (existing) return resolved(existing.branch, { source: existing.source });
339
+ // A branch made before the team switched templates keeps resolving.
340
+ if (template !== DEFAULT_BRANCH_TEMPLATE) {
341
+ const fallback = await existingBranch(
342
+ worksetId,
343
+ DEFAULT_BRANCH_TEMPLATE,
344
+ checkout,
345
+ diagnostics,
346
+ plain,
310
347
  );
311
- return unresolved();
348
+ if (fallback === "ambiguous") return unresolved();
349
+ if (fallback) return followsDefault(fallback.branch, fallback.source);
312
350
  }
313
- if (ticketed.length === 1) return resolved(ticketed[0]!);
314
351
  return resolved(named, { source: "new" });
315
352
  }
316
353
 
317
- /** A {@link ListRefs} over a Git runner that answers stdout, or null on failure. */
318
- export function listRefsWith(git: (args: string[]) => Promise<string | null>): ListRefs {
319
- return async (prefixes) => {
320
- const stdout = await git(["for-each-ref", "--format=%(refname)", ...prefixes]);
321
- return stdout ? stdout.split("\n").filter(Boolean) : [];
322
- };
323
- }
324
-
325
354
  /**
326
355
  * The Delivery Branch link, worktree, and factory spawning share, for a change or a Workset in
327
356
  * `repository`: the Workset identity check, then the branch its changes record when they agree,
328
- * a branch inherited from a `dependsOn` change, then with a ticket `workset/<TICKET>-<id>`
329
- * (a record's ticket yields to any existing Workset branch); without one an existing
330
- * `workset/<id>`, else the single existing `workset/<TICKET>-<id>`, else `workset/<id>`. Direct
331
- * deliveries are not a Workset's branch. Existence is read through `hasRef`, local branch
332
- * first, so callers fetch before resolving. A damaged record throws, as
333
- * when it is read on its own.
357
+ * a branch inherited from a `dependsOn` change, then with a ticket the template's name with it
358
+ * (a record's ticket yields to any existing Workset branch); without one an existing branch the
359
+ * template names without a ticket, else the single one with any ticket, else for a non-default
360
+ * template the same under the default, else the name without a ticket. The default template
361
+ * gives `workset/<id>` and `workset/<TICKET>-<id>`. Direct
362
+ * deliveries are not a Workset's branch. The template is the companion's; an invalid one
363
+ * counts as the default and is named in `templateDiagnostic`. Existence and merges are read
364
+ * in `checkout`, local branch first, so callers fetch before resolving; a null checkout gives
365
+ * the computed name with `source` null. A damaged record throws, as when it is read on its own.
334
366
  */
335
367
  export async function resolveDeliveryBranch(
336
- request: DeliveryBranchRequest & ({ change: string } | { worksetId: string }),
368
+ checkout: DeliveryCheckout | null,
369
+ subject: DeliverySubject,
370
+ options: DeliveryBranchOptions = {},
337
371
  ): Promise<DeliveryBranchResolution & { worksetId: string | null }> {
338
- const repository = request.repository ?? undefined;
372
+ const repository = subject.repository ?? undefined;
373
+ const setting = await companionBranchTemplate(subject.companionPath);
374
+ const templateField = setting.diagnostic ? { templateDiagnostic: setting.diagnostic } : {};
339
375
  const identity =
340
- "change" in request
341
- ? await resolveWorksetOf(request.companionPath, request.change, repository)
342
- : await resolveWorksetBranch(request.companionPath, request.worksetId, repository);
376
+ "change" in subject
377
+ ? await resolveWorksetOf(subject.companionPath, subject.change, repository)
378
+ : await resolveWorksetBranch(subject.companionPath, subject.worksetId, repository);
343
379
  if (identity.id === null) {
344
380
  return {
345
381
  worksetId: null,
@@ -347,13 +383,15 @@ export async function resolveDeliveryBranch(
347
383
  source: null,
348
384
  inheritedFrom: [],
349
385
  diagnostics: identity.diagnostics,
386
+ ...templateField,
350
387
  };
351
388
  }
352
- const resolution = await branchOf(request, identity.id);
389
+ const resolution = await branchOf(checkout, subject, options, setting.template, identity.id);
353
390
  return {
354
391
  ...resolution,
355
392
  worksetId: identity.id,
356
393
  diagnostics: [...identity.diagnostics, ...resolution.diagnostics],
394
+ ...templateField,
357
395
  };
358
396
  }
359
397
 
@@ -375,9 +413,18 @@ export function worktreeAddArgs(
375
413
  target: string,
376
414
  base = "origin/HEAD",
377
415
  ): string[] {
378
- if (source === "local") return ["worktree", "add", target, branch];
416
+ if (source === "local") return ["worktree", "add", "--end-of-options", target, branch];
379
417
  if (source === "remote") {
380
- return ["worktree", "add", "--track", "-b", branch, target, `origin/${branch}`];
418
+ return [
419
+ "worktree",
420
+ "add",
421
+ "--track",
422
+ "-b",
423
+ branch,
424
+ "--end-of-options",
425
+ target,
426
+ `origin/${branch}`,
427
+ ];
381
428
  }
382
- return ["worktree", "add", "--no-track", "-b", branch, target, base];
429
+ return ["worktree", "add", "--no-track", "-b", branch, "--end-of-options", target, base];
383
430
  }
@@ -23,13 +23,23 @@ export interface AgentLaunchOptions {
23
23
  /** A Claude session id; anything else never reaches the command line. */
24
24
  export const CLAUDE_SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
25
25
 
26
+ /** The managed launch's command and its Mate options; the agent's own arguments follow. */
27
+ function managedLaunch(mode: "--prepare-only" | "--prepared", noGit?: boolean): string[] {
28
+ return ["mate", "claude", "--", "--yes", mode, ...(noGit ? ["--no-git"] : [])];
29
+ }
30
+
31
+ /** The launch's preparation (wrap, index) run through exec, so the attached terminal shows none of it. */
32
+ export function prepareCommand(options: Pick<AgentLaunchOptions, "noGit"> = {}): string[] {
33
+ return managedLaunch("--prepare-only", options.noGit);
34
+ }
35
+
26
36
  /** The managed launch inside the Workset container, with the server-built first turn. */
27
37
  export function agentCommand(
28
38
  start: AttachStart,
29
39
  intent: string,
30
40
  options: AgentLaunchOptions = {},
31
41
  ): string[] {
32
- const launch = ["mate", "claude", "--", "--yes", ...(options.noGit ? ["--no-git"] : [])];
42
+ const launch = managedLaunch("--prepared", options.noGit);
33
43
  if (start === "continue") return [...launch, "--continue"];
34
44
  if (start === "resume") {
35
45
  if (!options.resumeSession || !CLAUDE_SESSION_ID.test(options.resumeSession)) {
@@ -1,10 +1,10 @@
1
1
  import {
2
- AGENT_CREDENTIAL_NAMES,
3
2
  checkedCredential,
4
3
  credentialKind,
5
4
  type CredentialStore,
6
5
  NO_CREDENTIAL,
7
6
  } from "./credential-store";
7
+ import { checkedGitKey, checkedGitToken, GitCredentialStore } from "./git-credential-store";
8
8
  import { FactoryError } from "./ids";
9
9
  import type { WorksetRuntime } from "./runtime";
10
10
 
@@ -12,11 +12,11 @@ export { AGENT_CREDENTIAL_NAMES, takeAgentCredential } from "./credential-store"
12
12
 
13
13
  /**
14
14
  * The factory broker's API: the Workset operations and the Workset agent
15
- * credential, over HTTP on a Unix socket, and nothing else. It is the boundary
15
+ * credential and Git tokens, over HTTP on a Unix socket, and nothing else. It is the boundary
16
16
  * between processes running as the unprivileged user and the inner Docker
17
17
  * daemon, so every request is checked field by field and an unexpected field is
18
- * refused rather than ignored: a caller cannot ask for a mount, an image or a
19
- * privilege. No answer carries the credential's value.
18
+ * refused: callers cannot choose a mount, image, privilege or environment.
19
+ * No answer carries the credential's value.
20
20
  */
21
21
 
22
22
  type Body = Record<string, unknown>;
@@ -31,19 +31,6 @@ function only(body: Body, allowed: string[]): void {
31
31
  }
32
32
  }
33
33
 
34
- function stringRecord(value: unknown, what: string): Record<string, string> | undefined {
35
- if (value === undefined) return undefined;
36
- if (
37
- value === null ||
38
- typeof value !== "object" ||
39
- Array.isArray(value) ||
40
- !Object.values(value).every((entry) => typeof entry === "string")
41
- ) {
42
- throw new FactoryError(`${what} must map names to strings`);
43
- }
44
- return value as Record<string, string>;
45
- }
46
-
47
34
  export interface BrokerHandler {
48
35
  (operation: string, body: Body): Promise<unknown>;
49
36
  }
@@ -51,6 +38,7 @@ export interface BrokerHandler {
51
38
  export function brokerHandler(
52
39
  runtime: WorksetRuntime,
53
40
  credentials: CredentialStore,
41
+ gitTokens: GitCredentialStore = new GitCredentialStore(null, null),
54
42
  ): BrokerHandler {
55
43
  return async (operation, body) => {
56
44
  switch (operation) {
@@ -95,39 +83,48 @@ export function brokerHandler(
95
83
  only(body, []);
96
84
  credentials.clear();
97
85
  return {};
86
+ case "companion.clone":
87
+ only(body, ["url"]);
88
+ return runtime.cloneCompanion(body.url);
89
+ case "remote.check":
90
+ only(body, ["url"]);
91
+ await runtime.checkRemote(body.url);
92
+ return {};
93
+ case "git.token.list":
94
+ only(body, []);
95
+ return { tokens: gitTokens.status() };
96
+ case "git.token.set":
97
+ only(body, ["key", "token"]);
98
+ gitTokens.set(checkedGitKey(body.key), checkedGitToken(body.token));
99
+ return {};
100
+ case "git.token.clear":
101
+ only(body, ["key"]);
102
+ gitTokens.clear(checkedGitKey(body.key));
103
+ return {};
98
104
  case "exec": {
99
- only(body, ["id", "command", "env", "cwd", "agent", "stdin"]);
105
+ only(body, ["id", "command", "cwd", "agent", "stdin"]);
100
106
  if (body.stdin !== undefined && typeof body.stdin !== "string") {
101
107
  throw new FactoryError("stdin must be base64 text");
102
108
  }
103
- const env = { ...stringRecord(body.env, "exec's environment") };
104
- for (const name of AGENT_CREDENTIAL_NAMES) {
105
- if (name in env) {
106
- throw new FactoryError(
107
- `${name} is set by the broker for agent commands, not by callers`,
108
- );
109
- }
110
- }
111
109
  if (body.agent !== undefined && typeof body.agent !== "boolean") {
112
110
  throw new FactoryError("agent must be true or false");
113
111
  }
114
112
  if (body.cwd !== undefined && typeof body.cwd !== "string") {
115
113
  throw new FactoryError("cwd must be a path relative to the worktree");
116
114
  }
117
- if (body.agent === true) {
118
- const credential = credentials.current();
119
- if (credential === null) throw new FactoryError(NO_CREDENTIAL);
120
- env[credential.name] = credential.value;
121
- }
122
- const result = await runtime.exec({
123
- id: body.id as string,
124
- command: body.command as string[],
125
- env,
126
- cwd: body.cwd as string | undefined,
127
- ...(typeof body.stdin === "string"
128
- ? { stdin: new Uint8Array(Buffer.from(body.stdin, "base64")) }
129
- : {}),
130
- });
115
+ const credential = body.agent === true ? credentials.current() : undefined;
116
+ if (credential === null) throw new FactoryError(NO_CREDENTIAL);
117
+ const result = await runtime.exec(
118
+ {
119
+ id: body.id as string,
120
+ command: body.command as string[],
121
+ cwd: body.cwd as string | undefined,
122
+ ...(typeof body.stdin === "string"
123
+ ? { stdin: new Uint8Array(Buffer.from(body.stdin, "base64")) }
124
+ : {}),
125
+ },
126
+ credential,
127
+ );
131
128
  return result;
132
129
  }
133
130
  default:
@@ -2,6 +2,7 @@ import path from "node:path";
2
2
 
3
3
  import type { CommandResult } from "./command";
4
4
  import type { CredentialKind, CredentialStatus } from "./credential-store";
5
+ import type { GitTokenStatus } from "./git-credential-store";
5
6
  import { FactoryError } from "./ids";
6
7
  import type { ExecRequest, SpawnRequest, WorksetInfo } from "./runtime";
7
8
 
@@ -26,6 +27,19 @@ export interface FactoryClient {
26
27
  set(kind: CredentialKind, value: string): Promise<void>;
27
28
  clear(): Promise<void>;
28
29
  };
30
+ /**
31
+ * Clones a companion into the factory's companions directory, or reports the checkout of the
32
+ * same repository already there; the folder is the broker's choice.
33
+ */
34
+ cloneCompanion(url: string): Promise<{ path: string; existing: boolean }>;
35
+ /** Whether a Git URL answers with the stored token for it; refused with code `auth` when it needs one. */
36
+ checkRemote(url: string): Promise<void>;
37
+ /** The Git tokens by host or host and organization; no call returns a token. */
38
+ gitToken: {
39
+ list(): Promise<GitTokenStatus[]>;
40
+ set(key: string, token: string): Promise<void>;
41
+ clear(key: string): Promise<void>;
42
+ };
29
43
  }
30
44
 
31
45
  /** The broker serves attaches on a second socket beside its own. */
@@ -63,7 +77,7 @@ export function factoryClient(
63
77
  if (!answer.ok) {
64
78
  throw new FactoryError(
65
79
  answer.error ?? "the factory broker refused the request",
66
- answer.code === "busy" ? "busy" : undefined,
80
+ answer.code === "busy" || answer.code === "auth" ? answer.code : undefined,
67
81
  );
68
82
  }
69
83
  return answer.result as T;
@@ -88,5 +102,12 @@ export function factoryClient(
88
102
  set: async (kind, value) => void (await call("credential.set", { kind, value })),
89
103
  clear: async () => void (await call("credential.clear", {})),
90
104
  },
105
+ cloneCompanion: (url) => call("companion.clone", { url }),
106
+ checkRemote: async (url) => void (await call("remote.check", { url })),
107
+ gitToken: {
108
+ list: async () => (await call<{ tokens: GitTokenStatus[] }>("git.token.list", {})).tokens,
109
+ set: async (key, token) => void (await call("git.token.set", { key, token })),
110
+ clear: async (key) => void (await call("git.token.clear", { key })),
111
+ },
91
112
  };
92
113
  }