@uniqbit/mate-core 1.0.0-canary.2 → 1.0.0-canary.5

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 (189) hide show
  1. package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +1 -1
  2. package/claude-plugin/hooks/hooks.json +21 -1
  3. package/claude-plugin/hooks/session-attention.mjs +10 -0
  4. package/claude-plugin/hooks/session-end.mjs +9 -0
  5. package/claude-plugin/hooks/session-guidance.mjs +1 -1
  6. package/opencode-plugin/src/server.ts +13 -2
  7. package/opencode-plugin/src/tui.tsx +1 -1
  8. package/opencode-plugin/tui.tsx +2 -1
  9. package/package.json +1 -1
  10. package/src/cli/commands/artifact/delivery/archive.ts +2 -2
  11. package/src/cli/commands/artifact/delivery/link.ts +22 -5
  12. package/src/cli/commands/artifact/delivery/output.ts +9 -1
  13. package/src/cli/commands/artifact/finish/openspec.ts +1 -3
  14. package/src/cli/commands/companion/companion.ts +1 -1
  15. package/src/cli/commands/companion/link.ts +36 -4
  16. package/src/cli/commands/companion/open.ts +40 -12
  17. package/src/cli/commands/doctor.ts +122 -0
  18. package/src/cli/commands/session/prepare.ts +32 -4
  19. package/src/cli/commands/session/registry.ts +90 -0
  20. package/src/cli/commands/studio/companion-removal.ts +79 -0
  21. package/src/cli/commands/studio/mate-inventory.ts +1 -1
  22. package/src/cli/commands/studio/payload.ts +12 -1
  23. package/src/cli/commands/studio/server.ts +39 -16
  24. package/src/cli/commands/studio/services.ts +20 -0
  25. package/src/cli/commands/studio/skill-files.ts +133 -0
  26. package/src/cli/commands/studio/vault.ts +218 -44
  27. package/src/cli/commands/studio/views/model.ts +0 -5
  28. package/src/cli/commands/studio/views/vault/markdown-tree.ts +255 -4
  29. package/src/cli/commands/unwrap.ts +8 -0
  30. package/src/cli/commands/update.ts +26 -8
  31. package/src/cli/commands/vscode/setup.ts +155 -0
  32. package/src/cli/commands/vscode/vsix.ts +73 -0
  33. package/src/cli/commands/working/cleanup.ts +34 -10
  34. package/src/cli/commands/working/worktree.ts +58 -8
  35. package/src/cli/commands/wrap.ts +33 -7
  36. package/src/cli/main.ts +9 -2
  37. package/src/cli/usage.ts +6 -1
  38. package/src/hooks/companion-instructions.ts +19 -11
  39. package/src/hooks/hook-io.ts +15 -0
  40. package/src/hooks/session-banner.ts +5 -3
  41. package/src/hooks/session-end.ts +10 -0
  42. package/src/hooks/session-guidance.ts +10 -6
  43. package/src/hooks/session-prepare.ts +47 -31
  44. package/src/hooks/session-register.ts +199 -0
  45. package/src/hooks/tokensave-route.ts +7 -3
  46. package/src/hooks/validate-artifact-path.ts +9 -2
  47. package/src/lib/delivery/archive-change.ts +0 -1
  48. package/src/lib/delivery/branches.ts +15 -0
  49. package/src/lib/delivery/record-delivery.ts +94 -39
  50. package/src/lib/delivery/record.ts +30 -28
  51. package/src/lib/delivery/refresh.ts +37 -17
  52. package/src/lib/delivery/resolve-branch.ts +258 -37
  53. package/src/lib/factory/outcomes/index.ts +11 -1
  54. package/src/lib/factory/runtime.ts +10 -1
  55. package/src/lib/opencode-server.ts +238 -0
  56. package/src/lib/orchestrator/adapters/claude.ts +147 -2
  57. package/src/lib/orchestrator/adapters/opencode.ts +53 -2
  58. package/src/lib/orchestrator/checkout-adoption.ts +125 -0
  59. package/src/lib/orchestrator/companion-registry-store.ts +6 -2
  60. package/src/lib/orchestrator/editor.ts +2 -2
  61. package/src/lib/orchestrator/framework-context.ts +26 -14
  62. package/src/lib/orchestrator/global-config-store.ts +11 -0
  63. package/src/lib/orchestrator/launcher.ts +15 -2
  64. package/src/lib/orchestrator/projection-claude-links.ts +52 -5
  65. package/src/lib/orchestrator/projection-claude-skills.ts +28 -2
  66. package/src/lib/orchestrator/projection-companion-link.ts +12 -1
  67. package/src/lib/orchestrator/registry-checkouts.ts +177 -0
  68. package/src/lib/orchestrator/repository-link.ts +216 -57
  69. package/src/lib/orchestrator/serving-claude-wrap.ts +43 -0
  70. package/src/lib/orchestrator/session-context-link.ts +11 -5
  71. package/src/lib/orchestrator/session-prepare.ts +36 -6
  72. package/src/lib/orchestrator/types.ts +5 -0
  73. package/src/lib/orchestrator/workspace-inventory.ts +5 -1
  74. package/src/lib/public-npm.ts +17 -0
  75. package/src/lib/session-attribution.ts +52 -0
  76. package/src/lib/session-state.ts +285 -0
  77. package/src/lib/session-stop.ts +64 -0
  78. package/src/lib/workset/record.ts +20 -0
  79. package/src/lib/workset/worktree.ts +85 -17
  80. package/src/opencode/background-server.ts +145 -0
  81. package/src/opencode/companion-hooks.ts +3 -1
  82. package/src/opencode/companion-policy.ts +2 -2
  83. package/src/opencode/projected-guidance.ts +2 -1
  84. package/src/opencode/session-register.ts +176 -0
  85. package/src/opencode/tui.tsx +19 -29
  86. package/src/runtime/claude-delivery-plan.ts +14 -3
  87. package/src/runtime/current-branch.ts +10 -0
  88. package/src/runtime/env.ts +13 -7
  89. package/src/runtime/git-layout.ts +164 -17
  90. package/src/runtime/launch-env-guard.ts +65 -0
  91. package/src/runtime/opencode-server-record.ts +136 -0
  92. package/src/runtime/projection.ts +40 -6
  93. package/src/runtime/repo-local.ts +6 -1
  94. package/src/runtime/serving-claude-wrap.ts +28 -0
  95. package/src/runtime/session-context.ts +4 -3
  96. package/src/runtime/session-registry.ts +434 -0
  97. package/src/runtime/tokensave-route.ts +10 -5
  98. package/src/runtime/wrap-state.ts +31 -0
  99. package/src/studio.ts +2 -0
  100. package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +49 -21
  101. package/src/templates/mate-skills/agents/mate-archive-change/SKILL.md +0 -5
  102. package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +2 -8
  103. package/src/templates/mate-skills/agents/mate-propose-change/references/mate-revise.md +1 -3
  104. package/src/templates/mate-skills/agents/mate-publish-artifact/SKILL.md +2 -4
  105. package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +49 -21
  106. package/src/templates/mate-skills/claude/mate-archive-change/SKILL.md +0 -5
  107. package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +2 -8
  108. package/src/templates/mate-skills/claude/mate-propose-change/references/mate-revise.md +1 -3
  109. package/src/templates/mate-skills/claude/mate-publish-artifact/SKILL.md +2 -4
  110. package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +404 -30
  111. package/src/tools/setup/dynamic-plugins/hydrate.ts +4 -3
  112. package/src/tools/setup/providers/claude-plugin-hooks.ts +26 -0
  113. package/src/tools/setup/providers/claude.ts +21 -1
  114. package/studio-dist/client/assets/app-CdPiFv59.css +2 -0
  115. package/studio-dist/client/assets/button-BIIHE0EQ.js +3 -0
  116. package/studio-dist/client/assets/companion-view-DM6YyXaR.js +1 -0
  117. package/studio-dist/client/assets/delivery-path-DELxj0Av.js +1 -0
  118. package/studio-dist/client/assets/factory-BZy0E8ty.js +4 -0
  119. package/studio-dist/client/assets/index-B3wkMzjA.js +40 -0
  120. package/studio-dist/client/assets/markdown-view-pQ2Qp_i5.js +1 -0
  121. package/studio-dist/client/assets/panel-CbajlC3p.js +1 -0
  122. package/studio-dist/client/assets/{prepaint-CD5OpmTM.js → prepaint-CwSrpdFA.js} +7 -4
  123. package/studio-dist/client/assets/react-CN2GSx7x.js +1 -0
  124. package/studio-dist/client/assets/reports-DllhYQ5_.js +1 -0
  125. package/studio-dist/client/assets/routes-COm-G3mK.js +1 -0
  126. package/studio-dist/client/assets/skills-DmWAy-jZ.js +1 -0
  127. package/studio-dist/client/assets/specs-BG3Wi3ln.js +1 -0
  128. package/studio-dist/client/assets/steps-DFsH3Neu.js +1 -0
  129. package/studio-dist/client/assets/vault-B2q1nBY5.js +1 -0
  130. package/studio-dist/client/assets/warnings-BSXIyuSQ.js +1 -0
  131. package/studio-dist/client/assets/workflow-DlsQD6Hc.js +1 -0
  132. package/studio-dist/server/assets/{__23tanstack-start-server-fn-resolver-BQKuMdJW.js → __23tanstack-start-server-fn-resolver-M2GBs8s3.js} +179 -9
  133. package/studio-dist/server/assets/_tanstack-start-manifest_v.js +35 -25
  134. package/studio-dist/server/assets/button-CIJqAhni.js +11929 -0
  135. package/studio-dist/server/assets/{companion-view-aBady57v.js → companion-view-DISASvdJ.js} +13 -7
  136. package/studio-dist/server/assets/companions-BYbHWKOZ.js +15 -0
  137. package/studio-dist/server/assets/delivery-path-C29fBynY.js +654 -0
  138. package/studio-dist/server/assets/{factory-DT3UzHlh.js → factory-DGPmNGbo.js} +1 -0
  139. package/studio-dist/server/assets/{factory-D6xnjedJ.js → factory-DUTa0eDs.js} +317 -530
  140. package/studio-dist/server/assets/markdown-view-Bn7xPdks.js +86 -0
  141. package/studio-dist/server/assets/panel-3Jmn4awF.js +169 -0
  142. package/studio-dist/server/assets/{prepaint-BMke0fo1.js → prepaint-DJfTQ_VH.js} +6 -2
  143. package/studio-dist/server/assets/{react-COs0LHn-.js → react-Ce6dj15C.js} +4 -9
  144. package/studio-dist/server/assets/reports-C06RXVE2.js +18 -0
  145. package/studio-dist/server/assets/reports-D6YMqhnO.js +142 -0
  146. package/studio-dist/server/assets/router-C8MGqlAY.js +13713 -0
  147. package/studio-dist/server/assets/routes-BcFgvqf7.js +622 -0
  148. package/studio-dist/server/assets/{shell-CboT2f66.js → shell-DS3wUexh.js} +1 -0
  149. package/studio-dist/server/assets/skills-BEnOxiDl.js +266 -0
  150. package/studio-dist/server/assets/skills-CtrBAtX3.js +28 -0
  151. package/studio-dist/server/assets/specs-C8rv34-5.js +211 -0
  152. package/studio-dist/server/assets/{start-BW5ovN0k.js → start-h3ICWwd2.js} +4 -1
  153. package/studio-dist/server/assets/steps-Dq_CwY_s.js +181 -0
  154. package/studio-dist/server/assets/{vault-BGulVoNh.js → vault-BmWnryh3.js} +16 -1
  155. package/studio-dist/server/assets/vault-Fvxt2Tzl.js +1032 -0
  156. package/studio-dist/server/assets/{views-Bd7cRr27.js → views-BgYa-za2.js} +1 -3
  157. package/studio-dist/server/assets/warnings-DzR6fyZh.js +23 -0
  158. package/studio-dist/server/assets/workflow-liRYecHk.js +73 -0
  159. package/studio-dist/server/server.js +3 -157
  160. package/studio-dist/client/assets/companion-view-Csva26Ir.js +0 -1
  161. package/studio-dist/client/assets/delivery-path-DJMpRmiY.js +0 -1
  162. package/studio-dist/client/assets/factory-B7l9ZQyx.js +0 -4
  163. package/studio-dist/client/assets/index-n6uo_92S.js +0 -40
  164. package/studio-dist/client/assets/queries-CN3JwMBX.js +0 -1
  165. package/studio-dist/client/assets/react-CabXf2Ds.js +0 -1
  166. package/studio-dist/client/assets/report-time-Dc-KOqQu.js +0 -1
  167. package/studio-dist/client/assets/reports-C01RsnMA.js +0 -1
  168. package/studio-dist/client/assets/routes-4pbC-2Hj.js +0 -1
  169. package/studio-dist/client/assets/skills-CihQganl.js +0 -1
  170. package/studio-dist/client/assets/spec-areas-C2L48N4C.js +0 -1
  171. package/studio-dist/client/assets/specs-D24_35_K.js +0 -1
  172. package/studio-dist/client/assets/studio-DKxfi691.css +0 -1
  173. package/studio-dist/client/assets/studio-queries-rvbJupCy.js +0 -3
  174. package/studio-dist/client/assets/vault-Bd4CQiTK.js +0 -1
  175. package/studio-dist/client/assets/warnings-V_Ys51wJ.js +0 -1
  176. package/studio-dist/client/assets/workflow-BJejeVwQ.js +0 -1
  177. package/studio-dist/server/assets/delivery-path-BkKNjgkf.js +0 -778
  178. package/studio-dist/server/assets/queries-Ag2iL8q2.js +0 -107
  179. package/studio-dist/server/assets/report-time-DAfzln-N.js +0 -8
  180. package/studio-dist/server/assets/reports-BnUNJVtx.js +0 -126
  181. package/studio-dist/server/assets/router-BH--drIx.js +0 -4298
  182. package/studio-dist/server/assets/routes-khSDK1bV.js +0 -361
  183. package/studio-dist/server/assets/skills-MWYnWoKs.js +0 -106
  184. package/studio-dist/server/assets/spec-areas-DcnO6vW-.js +0 -16
  185. package/studio-dist/server/assets/specs-DE3w8hZP.js +0 -155
  186. package/studio-dist/server/assets/studio-queries-CLnP9kGu.js +0 -3365
  187. package/studio-dist/server/assets/vault-CuyyhLyB.js +0 -832
  188. package/studio-dist/server/assets/warnings-BH05rOuG.js +0 -15
  189. package/studio-dist/server/assets/workflow-BSuIKWqH.js +0 -42
@@ -4,8 +4,13 @@ import path from "node:path";
4
4
  import { promisify } from "node:util";
5
5
 
6
6
  import { FRAMEWORK_NAME } from "../../../framework";
7
+ import {
8
+ companionTicketFormat,
9
+ TICKETS_OFF,
10
+ type TicketFormat,
11
+ } from "../../../lib/delivery/branches";
7
12
  import { recordDelivery } from "../../../lib/delivery/record-delivery";
8
- import { sameRepository } from "../../../lib/delivery/record";
13
+ import { isJiraKey, normalizeJiraKey, sameRepository } from "../../../lib/delivery/record";
9
14
  import { resolveForCapability } from "../../../lib/orchestrator/framework-context";
10
15
  import {
11
16
  collectWorkspaceInventory,
@@ -19,19 +24,20 @@ import {
19
24
  type PruneResult,
20
25
  type WorktreeResult,
21
26
  } from "../../../lib/workset/worktree";
27
+ import { attributeCurrentSession } from "../../../lib/session-attribution";
22
28
  import { errorMessage } from "../../../runtime/error-message";
23
29
  import { gitEnvironment } from "../../../runtime/companion-git";
24
30
  import { resolveDeliveryEnvironment } from "../artifact/delivery/environment";
25
31
  import { sharedRefusalMessage } from "../artifact/delivery/output";
26
- import { ensureUnambiguousCompanion } from "../shared/companion-selection";
32
+ import { ensureUnambiguousCompanion, launchAmbiguityDeps } from "../shared/companion-selection";
27
33
  import { readCompanionWorksetStages } from "../studio/payload";
28
34
 
29
35
  const execFileAsync = promisify(execFile);
30
36
 
31
37
  const n = FRAMEWORK_NAME;
32
38
  const USAGE = [
33
- `${n} working worktree: usage: ${n} working worktree <workset-id> [--companion <path>] [--no-install] [--json]`,
34
- `${n} working worktree: usage: ${n} working worktree --change <change> [--enter] [--companion <path>] [--no-install] [--json]`,
39
+ `${n} working worktree: usage: ${n} working worktree <workset-id> [--ticket <KEY>] [--companion <path>] [--no-install] [--json]`,
40
+ `${n} working worktree: usage: ${n} working worktree --change <change> [--enter] [--ticket <KEY>] [--companion <path>] [--no-install] [--json]`,
35
41
  `${n} working worktree: usage: ${n} working worktree prune [--companion <path>] [--json]`,
36
42
  ].join("\n");
37
43
 
@@ -68,6 +74,10 @@ export interface WorkingWorktreeDeps {
68
74
  createForChange?: typeof createChangeWorktrees;
69
75
  prune?: typeof pruneWorksetWorktrees;
70
76
  worksetStages?: typeof readCompanionWorksetStages;
77
+ /** The companion's ticket setting; `--ticket` is refused unless it is `jira`. */
78
+ ticketFormat?: (companion: string) => Promise<TicketFormat>;
79
+ /** Records the change, and the worktree it moves to, on the agent session this runs in. */
80
+ attribute?: typeof attributeCurrentSession;
71
81
  }
72
82
 
73
83
  async function canonicalPath(candidate: string): Promise<string> {
@@ -98,12 +108,14 @@ type Parsed = {
98
108
  companion?: string;
99
109
  install: boolean;
100
110
  json: boolean;
111
+ ticket?: string;
101
112
  } & ({ target: string } | { change: string; enter: boolean });
102
113
 
103
114
  function parse(argv: string[]): Parsed | null {
104
115
  const positional: string[] = [];
105
116
  let companion: string | undefined;
106
117
  let change: string | undefined;
118
+ let ticket: string | undefined;
107
119
  let install = true;
108
120
  let json = false;
109
121
  let enter = false;
@@ -115,17 +127,25 @@ function parse(argv: string[]): Parsed | null {
115
127
  } else if (arg === "--change") {
116
128
  change = argv[++index];
117
129
  if (!change) return null;
130
+ } else if (arg === "--ticket") {
131
+ ticket = argv[++index];
132
+ if (!ticket) return null;
118
133
  } else if (arg === "--no-install") install = false;
119
134
  else if (arg === "--enter") enter = true;
120
135
  else if (arg === "--json") json = true;
121
136
  else if (arg.startsWith("-")) return null;
122
137
  else positional.push(arg);
123
138
  }
124
- const rest = { ...(companion ? { companion } : {}), install, json };
139
+ const rest = {
140
+ ...(companion ? { companion } : {}),
141
+ ...(ticket ? { ticket } : {}),
142
+ install,
143
+ json,
144
+ };
125
145
  if (change) return positional.length === 0 ? { change, enter, ...rest } : null;
126
146
  const [target] = positional;
127
147
  if (positional.length !== 1 || !target || enter) return null;
128
- if (target === "prune" && !install) return null;
148
+ if (target === "prune" && (!install || ticket)) return null;
129
149
  return { target, ...rest };
130
150
  }
131
151
 
@@ -152,7 +172,12 @@ async function resolveCompanion(
152
172
  roots.includes(await canonicalPath(pairing.repository.path)) ? pairing.companionPath : null,
153
173
  ),
154
174
  );
155
- const companions = [...new Set(paired.filter((entry): entry is string => entry !== null))];
175
+ const pairedCompanions = [...new Set(paired.filter((entry): entry is string => entry !== null))];
176
+ // A registry entry naming a removed folder pairs with nothing; the repo-local link still holds.
177
+ const companions =
178
+ pairedCompanions.length > 0
179
+ ? pairedCompanions
180
+ : (await launchAmbiguityDeps.resolveLinkedCompanions(cwd)).map((m) => m.companionPath);
156
181
  if (companions.length === 1) return { companion: companions[0]! };
157
182
  if (companions.length === 0) {
158
183
  return { error: `${roots[0]} is not linked to a companion; pass --companion <path>` };
@@ -327,7 +352,25 @@ export async function runWorkingWorktreeCommand(
327
352
  else printPruned(result);
328
353
  return;
329
354
  }
330
- const options = { install: parsed.install, streamInstall: !parsed.json };
355
+ let ticket: string | undefined;
356
+ if (parsed.ticket !== undefined) {
357
+ if (!isJiraKey(parsed.ticket)) {
358
+ console.error(`${n} working worktree: --ticket expects a Jira key such as ABC-123.`);
359
+ process.exitCode = 1;
360
+ return;
361
+ }
362
+ if ((await (deps.ticketFormat ?? companionTicketFormat)(companion)) !== "jira") {
363
+ console.error(`${n} working worktree: ${TICKETS_OFF}`);
364
+ process.exitCode = 1;
365
+ return;
366
+ }
367
+ ticket = normalizeJiraKey(parsed.ticket);
368
+ }
369
+ const options = {
370
+ install: parsed.install,
371
+ streamInstall: !parsed.json,
372
+ ...(ticket ? { ticket } : {}),
373
+ };
331
374
  const result =
332
375
  "change" in parsed
333
376
  ? await (deps.createForChange ?? createChangeWorktrees)(companion, parsed.change, options)
@@ -340,6 +383,13 @@ export async function runWorkingWorktreeCommand(
340
383
  const recorded = await record(companion, parsed.change, result, deps.cwd ?? process.cwd());
341
384
  if ("failed" in recorded) result.warnings.push(...recorded.failed);
342
385
  enter = enterInstruction(deps.env ?? process.env, recorded);
386
+ await (deps.attribute ?? attributeCurrentSession)(
387
+ {
388
+ change: parsed.change,
389
+ ...("path" in recorded ? { move: { cwd: recorded.path, branch: recorded.branch } } : {}),
390
+ },
391
+ deps.env ?? process.env,
392
+ );
343
393
  }
344
394
  reportProblems(result.diagnostics, result.warnings);
345
395
  if (parsed.json) {
@@ -1,9 +1,14 @@
1
1
  import path from "node:path";
2
2
 
3
3
  import { FRAMEWORK_NAME } from "../../framework";
4
+ import { adoptNewCheckout } from "../../lib/orchestrator/checkout-adoption";
4
5
  import { findRepoLocalLinkedRepository } from "../../lib/orchestrator/repository-link";
5
6
  import type { ProjectionChangedPaths } from "../../lib/orchestrator/projection-types";
6
7
  import { attendedLockOptions } from "../../lib/orchestrator/companion-git-sync";
8
+ import {
9
+ reconcileServingClaudeWrap,
10
+ type ServingClaudeWrap,
11
+ } from "../../lib/orchestrator/serving-claude-wrap";
7
12
  import { runWrapPass, type WrapPassResult } from "../../lib/orchestrator/wrap-pass";
8
13
  import { withCompanionLock } from "../../runtime/companion-lock";
9
14
  import { WRAP_RUNTIMES } from "../../runtime/wrap-state";
@@ -11,12 +16,15 @@ import { launchAmbiguityDeps } from "./shared/companion-selection";
11
16
 
12
17
  export interface WrapCommandDependencies {
13
18
  findRepoLocalLinkedRepository?: typeof findRepoLocalLinkedRepository;
19
+ /** A new Checkout of a linked clone is adopted before it is wrapped (design Decision 7). */
20
+ adoptNewCheckout?: (folder: string) => ReturnType<typeof adoptNewCheckout>;
14
21
  runWrapPass?: typeof runWrapPass;
15
22
  resolveLinkedCompanions?: typeof launchAmbiguityDeps.resolveLinkedCompanions;
16
23
  }
17
24
 
18
25
  export const wrapCommandDeps: Required<WrapCommandDependencies> = {
19
26
  findRepoLocalLinkedRepository,
27
+ adoptNewCheckout,
20
28
  runWrapPass,
21
29
  resolveLinkedCompanions: (cwd: string) => launchAmbiguityDeps.resolveLinkedCompanions(cwd),
22
30
  };
@@ -73,6 +81,7 @@ export async function runWrapCommand(
73
81
  deps.findRepoLocalLinkedRepository ?? wrapCommandDeps.findRepoLocalLinkedRepository;
74
82
  const wrap = deps.runWrapPass ?? wrapCommandDeps.runWrapPass;
75
83
  const resolveLinked = deps.resolveLinkedCompanions ?? wrapCommandDeps.resolveLinkedCompanions;
84
+ const adopt = deps.adoptNewCheckout ?? wrapCommandDeps.adoptNewCheckout;
76
85
 
77
86
  const parsed = parseWrapArgs(argv);
78
87
  if ("error" in parsed) {
@@ -81,7 +90,7 @@ export async function runWrapCommand(
81
90
  return;
82
91
  }
83
92
 
84
- const repository = await findRepository(cwd);
93
+ const repository = (await findRepository(cwd)) ?? (await adopt(cwd));
85
94
  if (!repository) {
86
95
  console.error(
87
96
  `${FRAMEWORK_NAME}: this directory is not a working repository linked to a companion.`,
@@ -106,13 +115,30 @@ export async function runWrapCommand(
106
115
  return;
107
116
  }
108
117
 
109
- /** Serialized with Session Prepare and every sync, which rewrite the same manifest. */
110
- const locked = await withCompanionLock(companionPath, attendedLockOptions(companionPath), () =>
111
- wrap(companionPath, repository),
118
+ /**
119
+ * Serialized with Session Prepare and every sync, which rewrite the same
120
+ * manifest. In a linked worktree the Claude wrap serving it comes first, so
121
+ * the worktree's own leaves out what that one carries (design Decision 12).
122
+ */
123
+ const locked = await withCompanionLock(
124
+ companionPath,
125
+ attendedLockOptions(companionPath),
126
+ async () => {
127
+ const served = await reconcileServingClaudeWrap(companionPath, repository, { create: true });
128
+ if (served?.result.kind === "failed") return { served, result: served.result };
129
+ return { served, result: await wrap(companionPath, repository) };
130
+ },
112
131
  );
113
- const result: WrapPassResult = locked.acquired
114
- ? locked.value
115
- : { kind: "failed", message: "another companion synchronization is still running." };
132
+ const { served, result }: { served?: ServingClaudeWrap | null; result: WrapPassResult } =
133
+ locked.acquired
134
+ ? locked.value
135
+ : {
136
+ result: {
137
+ kind: "failed",
138
+ message: "another companion synchronization is still running.",
139
+ },
140
+ };
141
+ if (served) console.log(`Serving Claude wrap: ${served.repository.path} (${served.result.kind})`);
116
142
  if (result.kind === "failed") {
117
143
  console.error(`${FRAMEWORK_NAME}: ${result.message}`);
118
144
  process.exitCode = 1;
package/src/cli/main.ts CHANGED
@@ -21,6 +21,7 @@ import { runSessionCommand } from "./commands/session/prepare";
21
21
  import { runStudioCommand } from "./commands/studio";
22
22
  import { runUpdateCommand } from "./commands/update";
23
23
  import { runUnwrapCommand } from "./commands/unwrap";
24
+ import { runVscodeCommand } from "./commands/vscode/setup";
24
25
  import { parseWrapArgs, runWrapCommand } from "./commands/wrap";
25
26
  import { runWorkingCommand } from "./commands/working/working";
26
27
  import { runFactoryCommand } from "./commands/factory/factory";
@@ -324,12 +325,18 @@ export async function main(argv = process.argv, deps: MainDeps = mainDeps): Prom
324
325
  }
325
326
  case "session":
326
327
  /**
327
- * Spawned by the OpenCode plugin for a bare session: no update prompt,
328
- * no companion picker, no install repair may block it.
328
+ * Spawned by the OpenCode plugin for a bare session and by editor plugins
329
+ * for the session registry: no update prompt, no companion picker, no
330
+ * install repair may block it.
329
331
  */
330
332
  if (!(await gate({}))) return;
331
333
  await runSessionCommand(subcommand, rest);
332
334
  return;
335
+ case "vscode":
336
+ // Installs an editor extension: needs no companion, repository, or installation.
337
+ if (!(await gate({}))) return;
338
+ await runVscodeCommand(subcommand, rest);
339
+ return;
333
340
  case "unwrap":
334
341
  // A recovery path: it withdraws what a wrap placed, so it must never be
335
342
  // gated on a companion or an installation it does not consult.
package/src/cli/usage.ts CHANGED
@@ -18,7 +18,7 @@ export function usage(): string {
18
18
  ` ${n} companion list`,
19
19
  ` ${n} companion sync`,
20
20
  ` ${n} companion update [--yes]`,
21
- ` ${n} companion open`,
21
+ ` ${n} companion open [--no-open] [--json]`,
22
22
  ` ${n} companion tui`,
23
23
  ` ${n} working cleanup`,
24
24
  ` ${n} working link --companion <path> --yes`,
@@ -29,6 +29,10 @@ export function usage(): string {
29
29
  ` ${n} unwrap`,
30
30
  ` ${n} session prepare --repo <dir> [--json] [--sync]`,
31
31
  ` ${n} session context --repo <dir> [--json]`,
32
+ ` ${n} session add --runtime <claude|opencode> --id <id> --cwd <dir> --json`,
33
+ ` ${n} session list --json`,
34
+ ` ${n} session remove <id> --json`,
35
+ ` ${n} session stop <id> --json`,
32
36
  ` ${n} hub init [folder]`,
33
37
  ` ${n} hub add [source] [--id ID] [--path PATH]`,
34
38
  ` ${n} hub sync [--json] (companions + hub plugins)`,
@@ -51,6 +55,7 @@ export function usage(): string {
51
55
  " --user <name> --companions-dir <dir> (the appliance's factory broker, run as root)",
52
56
  ` ${n} factory attach <workset> --session <id> --companion <path> [--start clarify|explore|continue]`,
53
57
  ` ${n} config`,
58
+ ` ${n} vscode setup [--editor code|cursor] [--vsix <path>] [--if-installed]`,
54
59
  ` ${n} claude [args...] (after --: --no-git, --companion, --yes no-op)`,
55
60
  ` ${n} opencode [args...] (after --: --no-git, --companion, --yes no-op)`,
56
61
  ` ${n} cap openspec <subcommand> [args...]`,
@@ -11,9 +11,10 @@ import {
11
11
  import { MATE_ENV, hasLaunchEnvironment } from "../runtime/env";
12
12
  import { FRAMEWORK_NAME } from "../runtime/framework";
13
13
  import { PROJECTED_FLAG } from "../runtime/projected-flag";
14
- import { resolveProjection } from "../runtime/projection";
14
+ import { checkoutServedFrom } from "../runtime/git-layout";
15
+ import { projectionCheckout, resolveProjection } from "../runtime/projection";
15
16
  import { managedSessionKind, type SessionKind } from "../runtime/session-kind";
16
- import { hookOutcome, runHook, type HookCall, type HookEnv } from "./hook-io";
17
+ import { hookFolder, hookOutcome, runHook, type HookCall, type HookEnv } from "./hook-io";
17
18
 
18
19
  export interface InstructionHookPayload {
19
20
  hookSpecificOutput?: { hookEventName: InstructionEvent; additionalContext: string };
@@ -79,30 +80,37 @@ function ownsEvent(event: InstructionEvent, kind: SessionKind, projected: boolea
79
80
  return false;
80
81
  }
81
82
 
82
- /** A launch sets every path; a partial one is not completed from a projection, which may name another repository. */
83
- function launchReaderInput(env: HookEnv): CompanionInstructionsInput | null {
83
+ /**
84
+ * A launch sets every path; a partial one is not completed from a projection, which may
85
+ * name another repository. A session moved into another Checkout of the clone names it.
86
+ */
87
+ function launchReaderInput(env: HookEnv, folder: string): CompanionInstructionsInput | null {
84
88
  const companionPath = env[MATE_ENV.companionPath];
85
89
  const id = env[MATE_ENV.repositoryId];
86
90
  const repoPath = env[MATE_ENV.repositoryPath];
87
91
  const wrapperBinPath = env[MATE_ENV.wrapperBinPath];
88
92
  if (!companionPath || !id || !repoPath || !wrapperBinPath) return null;
89
- return { companionPath, repository: { id, path: repoPath }, wrapperBinPath };
93
+ return {
94
+ companionPath,
95
+ repository: { id, path: checkoutServedFrom(repoPath, folder) },
96
+ wrapperBinPath,
97
+ };
90
98
  }
91
99
 
92
100
  /** The environment always wins: a managed session never reads the projection. */
93
101
  function readerInput(
94
102
  env: HookEnv,
95
103
  kind: SessionKind,
96
- cwd: string,
104
+ folder: string,
97
105
  resolve: typeof resolveProjection,
98
106
  ): CompanionInstructionsInput | null {
99
- if (kind !== "unmanaged") return launchReaderInput(env);
100
- const projection = resolve(cwd);
107
+ if (kind !== "unmanaged") return launchReaderInput(env, folder);
108
+ const projection = resolve(folder);
101
109
  if (!projection) return null;
102
110
  return {
103
111
  companionPath: projection.companionPath,
104
- /** A linked worktree on a projection copied from the main checkout names itself, not that checkout. */
105
- repository: { id: projection.repositoryId, path: projection.repoRoot },
112
+ /** A linked worktree on a borrowed or copied projection names itself, not the lender. */
113
+ repository: { id: projection.repositoryId, path: projectionCheckout(projection) },
106
114
  wrapperBinPath: projection.wrapperBinPath,
107
115
  };
108
116
  }
@@ -127,7 +135,7 @@ function evaluate(
127
135
  const kind = sessionKind(env);
128
136
  if (!ownsEvent(event, kind, projected)) return null;
129
137
 
130
- const reader = readerInput(env, kind, directory || input.cwd || cwd, resolve);
138
+ const reader = readerInput(env, kind, directory || hookFolder(input, env, cwd), resolve);
131
139
  if (!reader) return null;
132
140
 
133
141
  const result = read(reader);
@@ -1,4 +1,5 @@
1
1
  /** Stdin and stdout of a Claude hook shim. */
2
+ import { guardLaunchEnvironment } from "../runtime/launch-env-guard";
2
3
 
3
4
  export type HookEnv = Record<string, string | undefined>;
4
5
 
@@ -10,6 +11,18 @@ export interface HookCall {
10
11
  input: unknown;
11
12
  }
12
13
 
14
+ /**
15
+ * The folder a hook resolves its session for (design Decision 10): the
16
+ * input's `cwd`, which follows the session into a worktree after EnterWorktree,
17
+ * then `CLAUDE_PROJECT_DIR`, which still names the folder the session started
18
+ * in, then the process's own folder.
19
+ */
20
+ export function hookFolder(input: unknown, env: HookEnv, cwd: string): string {
21
+ const fromInput =
22
+ input && typeof input === "object" ? (input as { cwd?: unknown }).cwd : undefined;
23
+ return (typeof fromInput === "string" && fromInput) || env.CLAUDE_PROJECT_DIR || cwd;
24
+ }
25
+
13
26
  export interface HookOutcome {
14
27
  exitCode: number;
15
28
  stdout: string;
@@ -47,6 +60,8 @@ export async function runHook(
47
60
  ): Promise<number> {
48
61
  let outcome: HookOutcome;
49
62
  try {
63
+ // A background session can inherit another launch's environment from Claude's daemon.
64
+ guardLaunchEnvironment(process.env, process.cwd());
50
65
  outcome = await build(await readHookInput());
51
66
  } catch (error) {
52
67
  outcome = onError(error);
@@ -14,7 +14,8 @@ import { FRAMEWORK_NAME } from "../runtime/framework";
14
14
  import { projectionFreshness, projectionStalenessLines } from "../runtime/freshness";
15
15
  import { mateVersion } from "../runtime/install";
16
16
  import { PROJECTED_FLAG } from "../runtime/projected-flag";
17
- import { hookOutcome, runHook, type HookEnv, type HookOutcome } from "./hook-io";
17
+ import { projectionCheckout } from "../runtime/projection";
18
+ import { hookFolder, hookOutcome, runHook, type HookEnv, type HookOutcome } from "./hook-io";
18
19
 
19
20
  /**
20
21
  * Resolves in-process from either source — no subprocess in either branch.
@@ -46,7 +47,8 @@ export async function buildBanner(
46
47
  */
47
48
  const ownsGitSync =
48
49
  !hasLaunchEnvironment(env) &&
49
- unmanagedClaudeDelivery(projection?.repoRoot).gitSync === "banner";
50
+ unmanagedClaudeDelivery(projection ? projectionCheckout(projection) : undefined).gitSync ===
51
+ "banner";
50
52
  const gitNotes = ownsGitSync
51
53
  ? unattendedSyncStalenessLines(
52
54
  await syncCompanionUnattended(context.companionPath, { lock: "try", ...syncOptions }),
@@ -72,7 +74,7 @@ export async function buildBanner(
72
74
  /** Plugin-shim entry; exits zero even when the banner fails. */
73
75
  export function run(argv: readonly string[] = process.argv.slice(2)): Promise<number> {
74
76
  return runHook(
75
- () => buildBanner(process.env, process.cwd(), argv),
77
+ (input) => buildBanner(process.env, hookFolder(input, process.env, process.cwd()), argv),
76
78
  (error) =>
77
79
  hookOutcome({
78
80
  systemMessage: `${FRAMEWORK_NAME}: session banner failed: ${errorMessage(error)}`,
@@ -0,0 +1,10 @@
1
+ /** Shim entries for the `SessionEnd` and `Notification` hooks: session registry updates only. */
2
+ import { isWrapCopy, runEnd, runNotification } from "./session-register";
3
+
4
+ export function run(argv: readonly string[] = process.argv.slice(2)): Promise<number> {
5
+ return runEnd(isWrapCopy, argv);
6
+ }
7
+
8
+ export function runAttention(argv: readonly string[] = process.argv.slice(2)): Promise<number> {
9
+ return runNotification(isWrapCopy, argv);
10
+ }
@@ -1,6 +1,7 @@
1
1
  // Deliver the companion guidance to a Claude session Mate did not launch.
2
+ import { guardLaunchEnvironment } from "../runtime/launch-env-guard";
2
3
  import { buildProjectedGuidance } from "../runtime/projected-guidance";
3
- import { hookOutcome, type HookEnv, type HookOutcome } from "./hook-io";
4
+ import { hookFolder, hookOutcome, runHook, type HookEnv, type HookOutcome } from "./hook-io";
4
5
 
5
6
  /**
6
7
  * `SessionStart.additionalContext` is the file-based form of the managed
@@ -24,9 +25,12 @@ export function buildSessionGuidance(env: HookEnv, cwd: string = process.cwd()):
24
25
  });
25
26
  }
26
27
 
27
- // Plugin-shim entry.
28
- export function run(): number {
29
- const outcome = buildSessionGuidance(process.env);
30
- if (outcome.stdout) process.stdout.write(outcome.stdout);
31
- return outcome.exitCode;
28
+ /** Plugin-shim entry; emits nothing when it fails, as when no companion resolves. */
29
+ export function run(): Promise<number> {
30
+ // A background session can inherit another launch's environment from Claude's daemon.
31
+ guardLaunchEnvironment(process.env, process.cwd());
32
+ return runHook(
33
+ (input) => buildSessionGuidance(process.env, hookFolder(input, process.env, process.cwd())),
34
+ () => hookOutcome(null),
35
+ );
32
36
  }
@@ -5,8 +5,7 @@ import { errorMessage } from "../runtime/error-message";
5
5
  import { unmanagedClaudeDelivery } from "../runtime/claude-delivery-plan";
6
6
  import { hasLaunchEnvironment } from "../runtime/env";
7
7
  import { FRAMEWORK_NAME } from "../runtime/framework";
8
- import { PROJECTED_FLAG } from "../runtime/projected-flag";
9
- import { resolveProjection } from "../runtime/projection";
8
+ import { projectionCheckout, resolveProjection } from "../runtime/projection";
10
9
  import { runtimeDirectoryPath, runtimeMateBin } from "../runtime/runtime-directory";
11
10
  import {
12
11
  PREPARE_TIMEOUT_MS,
@@ -16,7 +15,21 @@ import {
16
15
  type PrepareRunner,
17
16
  } from "../runtime/session-prepare-runner";
18
17
  import { instructionHookPayload, type InstructionHookDeps } from "./companion-instructions";
19
- import { hookOutcome, runHook, type HookCall, type HookEnv, type HookOutcome } from "./hook-io";
18
+ import {
19
+ hookFolder,
20
+ hookOutcome,
21
+ runHook,
22
+ type HookCall,
23
+ type HookEnv,
24
+ type HookOutcome,
25
+ } from "./hook-io";
26
+ import {
27
+ isWrapCopy,
28
+ registerClaudeSession,
29
+ registersHere,
30
+ sessionRegisterDeps,
31
+ type SessionRegisterDeps,
32
+ } from "./session-register";
20
33
 
21
34
  export interface SessionPrepareHookDeps {
22
35
  /** The runtime-directory launcher; `null` until a `mate` run wrote it. */
@@ -26,6 +39,8 @@ export interface SessionPrepareHookDeps {
26
39
  timeoutMs: number;
27
40
  /** The main thread's instruction read, after the prepare. */
28
41
  instructions?: InstructionHookDeps;
42
+ /** Session registry writes; defaults to the real registry. */
43
+ registry?: SessionRegisterDeps;
29
44
  }
30
45
 
31
46
  export const sessionPrepareHookDeps: SessionPrepareHookDeps = {
@@ -46,9 +61,9 @@ function operatorMessage(lines: string[]): HookOutcome {
46
61
  async function runLocalPhase(
47
62
  deps: SessionPrepareHookDeps,
48
63
  mateBin: string,
49
- projectDir: string,
64
+ checkoutRoot: string,
50
65
  ): Promise<PrepareRun> {
51
- const pending = deps.runner(mateBin, projectDir)({ sync: false, timeoutMs: deps.timeoutMs });
66
+ const pending = deps.runner(mateBin, checkoutRoot)({ sync: false, timeoutMs: deps.timeoutMs });
52
67
  try {
53
68
  return await pending;
54
69
  } catch (error) {
@@ -74,15 +89,10 @@ export async function runSessionPrepareHook(
74
89
  cwd: string = process.cwd(),
75
90
  deps: SessionPrepareHookDeps = sessionPrepareHookDeps,
76
91
  ): Promise<HookOutcome> {
77
- const prepared = await guardedPrepare(env, cwd, deps);
92
+ const prepared = await guardedPrepare(env, hookFolder(null, env, cwd), deps);
78
93
  return operatorMessage([...prepared.notes, ...startSync(prepared)]);
79
94
  }
80
95
 
81
- /** The folder the prepare runs in, and the main thread's instructions are read from. */
82
- function prepareDirectory(env: HookEnv, cwd: string): string {
83
- return env.CLAUDE_PROJECT_DIR || cwd;
84
- }
85
-
86
96
  function prepareFailure(error: unknown): string {
87
97
  return `${FRAMEWORK_NAME}: session prepare failed: ${errorMessage(error)}`;
88
98
  }
@@ -90,11 +100,11 @@ function prepareFailure(error: unknown): string {
90
100
  /** A prepare that throws becomes a note, so the instructions after it still go out. */
91
101
  async function guardedPrepare(
92
102
  env: HookEnv,
93
- cwd: string,
103
+ folder: string,
94
104
  deps: SessionPrepareHookDeps,
95
105
  ): Promise<Prepared> {
96
106
  try {
97
- return await prepare(env, cwd, deps);
107
+ return await prepare(env, folder, deps);
98
108
  } catch (error) {
99
109
  return { notes: [prepareFailure(error)], startSync: NO_SYNC };
100
110
  }
@@ -110,13 +120,18 @@ function startSync(prepared: Prepared): string[] {
110
120
  }
111
121
  }
112
122
 
113
- async function prepare(env: HookEnv, cwd: string, deps: SessionPrepareHookDeps): Promise<Prepared> {
123
+ /** Prepares the Checkout the session serves, a borrowing worktree's own root included. */
124
+ async function prepare(
125
+ env: HookEnv,
126
+ folder: string,
127
+ deps: SessionPrepareHookDeps,
128
+ ): Promise<Prepared> {
114
129
  const nothing = { notes: [], startSync: NO_SYNC };
115
130
  if (hasLaunchEnvironment(env)) return nothing;
116
- const projectDir = prepareDirectory(env, cwd);
117
- const projection = resolveProjection(projectDir);
131
+ const projection = resolveProjection(folder);
118
132
  if (!projection) return nothing;
119
- const plan = unmanagedClaudeDelivery(projection.repoRoot);
133
+ const checkoutRoot = projectionCheckout(projection);
134
+ const plan = unmanagedClaudeDelivery(checkoutRoot);
120
135
  const reconciles = plan.reconcile === "prepare-hook";
121
136
  const syncs = plan.gitSync === "prepare-hook";
122
137
  if (!reconciles && !syncs) return nothing;
@@ -132,24 +147,15 @@ async function prepare(env: HookEnv, cwd: string, deps: SessionPrepareHookDeps):
132
147
  }
133
148
 
134
149
  const local = reconciles
135
- ? await runLocalPhase(deps, mateBin, projectDir)
150
+ ? await runLocalPhase(deps, mateBin, checkoutRoot)
136
151
  : { ok: true, notes: [] };
137
152
  const verdict = local.ok ? "warning" : "failed";
138
153
  return {
139
154
  notes: local.notes.map((note) => `${FRAMEWORK_NAME}: session prepare ${verdict}: ${note}`),
140
- startSync: syncs ? () => deps.startSync(mateBin, projectDir) : NO_SYNC,
155
+ startSync: syncs ? () => deps.startSync(mateBin, checkoutRoot) : NO_SYNC,
141
156
  };
142
157
  }
143
158
 
144
- /**
145
- * Legacy rule: wraps before `--projected` registered this copy bare, and only
146
- * this hook re-wraps them. So a bare copy counts as the wrap's unless
147
- * `CLAUDE_PLUGIN_ROOT`, set for plugin hooks only, marks it as the plugin's.
148
- */
149
- function isWrapCopy({ argv, env }: HookCall): boolean {
150
- return argv.includes(PROJECTED_FLAG) || !env.CLAUDE_PLUGIN_ROOT;
151
- }
152
-
153
159
  /**
154
160
  * The prepare, the main thread's Companion Instructions, then the Git sync, in
155
161
  * one hook: Claude Code runs an event's hooks in parallel, and the prepare and
@@ -162,11 +168,21 @@ export async function runSessionStartHook(
162
168
  ): Promise<HookOutcome> {
163
169
  const { env, cwd, input } = call;
164
170
  if (!input || typeof input !== "object" || Array.isArray(input)) return hookOutcome(null);
165
- if (!isWrapCopy(call)) return hookOutcome(null);
166
- const prepared = await guardedPrepare(env, cwd, deps);
171
+ const registry = deps.registry ?? sessionRegisterDeps;
172
+ const wrapCopy = isWrapCopy(call);
173
+ const folder = hookFolder(input, env, cwd);
174
+ // Registration never throws; it runs beside the prepare, off its critical path.
175
+ const registering = registersHere(wrapCopy, folder, registry)
176
+ ? registerClaudeSession(env, cwd, input, registry)
177
+ : Promise.resolve();
178
+ if (!wrapCopy) {
179
+ await registering;
180
+ return hookOutcome(null);
181
+ }
182
+ const [prepared] = await Promise.all([guardedPrepare(env, folder, deps), registering]);
167
183
  const { hookSpecificOutput, systemMessage } =
168
184
  instructionHookPayload(
169
- { env, cwd, input, projected: true, directory: prepareDirectory(env, cwd) },
185
+ { env, cwd, input, projected: true, directory: folder },
170
186
  deps.instructions,
171
187
  ) ?? {};
172
188
  const notes = [...prepared.notes, ...startSync(prepared)];