@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.
- package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +1 -1
- package/claude-capability-plugins/companion-mentions/hooks/register.ts +1 -1
- package/claude-plugin/hooks/hooks.json +10 -1
- package/claude-plugin/hooks/rtk-worktree-git.mjs +58 -0
- package/opencode-plugin/src/server.ts +9 -2
- package/package.json +2 -1
- package/src/cli/commands/artifact/delivery/environment.ts +28 -8
- package/src/cli/commands/artifact/delivery/link.ts +28 -9
- package/src/cli/commands/artifact/delivery/output.ts +7 -0
- package/src/cli/commands/companion/update.ts +2 -19
- package/src/cli/commands/factory/factory.ts +11 -1
- package/src/cli/commands/install.ts +47 -36
- package/src/cli/commands/launch/claude.ts +2 -0
- package/src/cli/commands/launch/shared.ts +33 -6
- package/src/cli/commands/plugin/install.ts +27 -11
- package/src/cli/commands/report/index.ts +180 -184
- package/src/cli/commands/report/markdown.ts +67 -72
- package/src/cli/commands/shared/companion-selection.ts +3 -1
- package/src/cli/commands/studio/access.ts +6 -14
- package/src/cli/commands/studio/areas.ts +112 -33
- package/src/cli/commands/studio/companion-add.ts +87 -0
- package/src/cli/commands/studio/companion-removal.ts +15 -8
- package/src/cli/commands/studio/factory-credential.ts +1 -15
- package/src/cli/commands/studio/factory-git-tokens.ts +40 -0
- package/src/cli/commands/studio/factory-relay.ts +18 -0
- package/src/cli/commands/studio/graph-model.ts +61 -0
- package/src/cli/commands/studio/graph.ts +336 -0
- package/src/cli/commands/studio/index.ts +4 -4
- package/src/cli/commands/studio/payload.ts +42 -15
- package/src/cli/commands/studio/repository-add.ts +116 -0
- package/src/cli/commands/studio/routes.ts +6 -0
- package/src/cli/commands/studio/selection.ts +12 -3
- package/src/cli/commands/studio/server.ts +127 -5
- package/src/cli/commands/studio/services.ts +40 -0
- package/src/cli/commands/studio/terminal.ts +45 -3
- package/src/cli/commands/studio/views/vault/markdown-tree.ts +84 -3
- package/src/cli/commands/studio/workset-session.ts +47 -14
- package/src/cli/commands/unwrap.ts +3 -0
- package/src/cli/commands/update.ts +2 -0
- package/src/cli/commands/vscode/release-vsix.json +4 -0
- package/src/cli/commands/vscode/setup.ts +71 -13
- package/src/cli/commands/vscode/vsix.ts +48 -7
- package/src/cli/commands/working/cleanup.ts +40 -8
- package/src/cli/commands/working/worktree.ts +176 -35
- package/src/cli/main.ts +17 -2
- package/src/cli/usage.ts +1 -1
- package/src/hooks/rtk-worktree-git.ts +48 -0
- package/src/hooks/validate-artifact-path.ts +38 -7
- package/src/lib/delivery/branch-template.ts +122 -0
- package/src/lib/delivery/branches.ts +21 -16
- package/src/lib/delivery/checkout.ts +102 -0
- package/src/lib/delivery/environment.ts +2 -0
- package/src/lib/delivery/merge-git.ts +13 -1
- package/src/lib/delivery/record-delivery.ts +83 -29
- package/src/lib/delivery/record.ts +15 -3
- package/src/lib/delivery/resolve-branch.ts +157 -110
- package/src/lib/factory/attach-client.ts +11 -1
- package/src/lib/factory/broker.ts +37 -40
- package/src/lib/factory/client.ts +22 -1
- package/src/lib/factory/clone-url.ts +39 -0
- package/src/lib/factory/command.ts +21 -5
- package/src/lib/factory/git-auth.ts +61 -0
- package/src/lib/factory/git-credential-store.ts +181 -0
- package/src/lib/factory/ids.ts +12 -2
- package/src/lib/factory/outcomes/actions.ts +20 -4
- package/src/lib/factory/outcomes/index.ts +77 -11
- package/src/lib/factory/outcomes/lifecycle.ts +9 -2
- package/src/lib/factory/outcomes/sessions.ts +23 -1
- package/src/lib/factory/prepare.ts +9 -3
- package/src/lib/factory/runner/scheduler.ts +96 -14
- package/src/lib/factory/runner/workflow.ts +5 -1
- package/src/lib/factory/runtime.ts +143 -43
- package/src/lib/orchestrator/companion-hub.ts +6 -1
- package/src/lib/orchestrator/engine-floor.ts +48 -0
- package/src/lib/orchestrator/launcher.ts +34 -1
- package/src/lib/orchestrator/projection-claude-commands.ts +2 -2
- package/src/lib/orchestrator/projection-claude-entry.ts +42 -21
- package/src/lib/orchestrator/projection-claude-links.ts +32 -8
- package/src/lib/orchestrator/projection-claude-skills.ts +2 -2
- package/src/lib/orchestrator/projection-entries.ts +22 -18
- package/src/lib/orchestrator/projection-opencode-copies.ts +65 -21
- package/src/lib/orchestrator/projection-runtime-documents.ts +66 -43
- package/src/lib/orchestrator/projection-types.ts +28 -1
- package/src/lib/orchestrator/refresh-working-claude-links.ts +3 -1
- package/src/lib/orchestrator/session-prepare.ts +6 -1
- package/src/lib/orchestrator/setup-compatibilities.ts +1 -1
- package/src/lib/orchestrator/types.ts +4 -0
- package/src/lib/orchestrator/umbrella-checkouts.ts +102 -0
- package/src/lib/orchestrator/working-repo-projection.ts +24 -4
- package/src/lib/orchestrator/wrap-pass.ts +1 -0
- package/src/lib/public-npm.ts +95 -50
- package/src/lib/session-state.ts +72 -2
- package/src/lib/update-checker.ts +7 -1
- package/src/lib/usage/aggregate.ts +202 -0
- package/src/lib/usage/cache.ts +87 -0
- package/src/lib/usage/claude-pricing.ts +71 -0
- package/src/lib/usage/claude.ts +189 -0
- package/src/lib/usage/collect.ts +56 -0
- package/src/lib/usage/opencode.ts +188 -0
- package/src/lib/usage/records.ts +80 -0
- package/src/lib/usage/rtk-database.ts +72 -0
- package/src/lib/usage/savings.ts +209 -0
- package/src/lib/usage/scope.ts +182 -0
- package/src/lib/usage/source.ts +102 -0
- package/src/lib/usage/types.ts +143 -0
- package/src/lib/usage/window.ts +37 -0
- package/src/lib/workset/factory-local.ts +157 -0
- package/src/lib/workset/record.ts +25 -11
- package/src/lib/workset/repositories.ts +118 -25
- package/src/lib/workset/worktree-include.ts +161 -0
- package/src/lib/workset/worktree-index.ts +6 -2
- package/src/lib/workset/worktree.ts +591 -101
- package/src/opencode/companion-hooks.ts +46 -8
- package/src/opencode/companion-policy.ts +18 -5
- package/src/opencode/enter-worktree.ts +254 -6
- package/src/opencode/session-register.ts +2 -2
- package/src/runtime/claude-bg-isolation.ts +138 -0
- package/src/runtime/companion-git.ts +10 -2
- package/src/runtime/companion-guidance.ts +17 -4
- package/src/runtime/current-branch.ts +16 -2
- package/src/runtime/entered-worktree.ts +24 -0
- package/src/runtime/git-tracked.ts +60 -0
- package/src/runtime/repo-destination.ts +72 -0
- package/src/runtime/repo-local.ts +25 -3
- package/src/runtime/rtk-git-guard.ts +66 -0
- package/src/runtime/runtime-document-manifest.ts +229 -19
- package/src/runtime/runtime-document-manifest.typecheck.ts +10 -0
- package/src/runtime/secret-guard.ts +9 -2
- package/src/runtime/secret-policy-weakening.ts +122 -0
- package/src/runtime/serving-claude-wrap.ts +15 -2
- package/src/runtime/umbrella-folder.ts +56 -0
- package/src/runtime/wrap-state.ts +5 -13
- package/src/studio.ts +37 -0
- package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +61 -6
- package/src/templates/mate-skills/agents/mate-explore-change/SKILL.md +8 -4
- package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +9 -5
- package/src/templates/mate-skills/agents/mate-publish-artifact/SKILL.md +5 -4
- package/src/templates/mate-skills/agents/mate-show-me/SKILL.md +10 -4
- package/src/templates/mate-skills/agents/mate-simplify-code/SKILL.md +5 -1
- package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +61 -6
- package/src/templates/mate-skills/claude/mate-explore-change/SKILL.md +8 -4
- package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +9 -5
- package/src/templates/mate-skills/claude/mate-publish-artifact/SKILL.md +5 -4
- package/src/templates/mate-skills/claude/mate-show-me/SKILL.md +10 -4
- package/src/templates/mate-skills/claude/mate-simplify-code/SKILL.md +5 -1
- package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +305 -42
- package/src/tools/setup/capabilities/rtk.ts +93 -1
- package/src/tools/setup/dynamic-plugins/audiences.ts +65 -21
- package/src/tools/setup/dynamic-plugins/frozen.ts +32 -13
- package/src/tools/setup/dynamic-plugins/hydrate.ts +4 -4
- package/src/tools/setup/dynamic-plugins/install.ts +24 -22
- package/src/tools/setup/dynamic-plugins/loader.ts +11 -7
- package/src/tools/setup/dynamic-plugins/paths.ts +30 -10
- package/src/tools/setup/dynamic-plugins/verify.ts +10 -7
- package/src/tools/setup/dynamic-plugins/workspaces.ts +153 -0
- package/src/tools/setup/mate.ts +3 -3
- package/src/tools/setup/plugins/gitignore.ts +15 -9
- package/src/tools/setup/providers/claude-plugin-hooks.ts +12 -0
- package/src/tools/setup/providers/claude.ts +25 -3
- package/src/tools/setup/utils.ts +4 -2
- package/src/tools/setup/working-repo-cleanup.ts +2 -2
- package/src/tools/setup.ts +25 -14
- package/studio-dist/client/assets/app-D9cw6szy.css +2 -0
- package/studio-dist/client/assets/artifacts-WD2ZXYrx.js +1 -0
- package/studio-dist/client/assets/button-Br0O1sXw.js +1 -0
- package/studio-dist/client/assets/combobox-DA7RAUUZ.js +1 -0
- package/studio-dist/client/assets/companion-view-CrabvxS8.js +1 -0
- package/studio-dist/client/assets/factory-DGS0E16C.js +6 -0
- package/studio-dist/client/assets/fields-6aXBpFhJ.js +1 -0
- package/studio-dist/client/assets/getting-started-Clde4w6P.js +1 -0
- package/studio-dist/client/assets/getting-started-D6kgWwWZ.js +1 -0
- package/studio-dist/client/assets/getting-started._topic-BiunccbT.js +1 -0
- package/studio-dist/client/assets/getting-started.index-DpiNoGw8.js +1 -0
- package/studio-dist/client/assets/graph-B_TCBVbu.js +2 -0
- package/studio-dist/client/assets/graph-canvas-Ba37Y5AE.js +3242 -0
- package/studio-dist/client/assets/guides-Du_RtJxP.js +1 -0
- package/studio-dist/client/assets/index-Df8L1LQa.js +40 -0
- package/studio-dist/client/assets/jsx-runtime-MZURdZre.js +1 -0
- package/studio-dist/client/assets/markdown-view-BOX6x0rL.js +1 -0
- package/studio-dist/client/assets/panel-dbmCx7Mx.js +1 -0
- package/studio-dist/client/assets/queries-RvHeXY12.js +1 -0
- package/studio-dist/client/assets/react-3QStrcDO.js +27 -0
- package/studio-dist/client/assets/report-url-XRL0x1SY.js +1 -0
- package/studio-dist/client/assets/reports-r3IK7Doz.js +1 -0
- package/studio-dist/client/assets/routes-C0A5LTFc.js +1 -0
- package/studio-dist/client/assets/select-CnaMd617.js +1 -0
- package/studio-dist/client/assets/skills-CzjzHpBT.js +1 -0
- package/studio-dist/client/assets/specs-B0ldF_6J.js +1 -0
- package/studio-dist/client/assets/specs-view-BnCLA0gW.js +1 -0
- package/studio-dist/client/assets/spinner-Dcm3fqK1.js +1 -0
- package/studio-dist/client/assets/steps-C0p2N1Pb.js +1 -0
- package/studio-dist/client/assets/store-Dw34catY.js +1 -0
- package/studio-dist/client/assets/studio-Cpz_HRHp.js +100 -0
- package/studio-dist/client/assets/studio-queries-ibmpFo6R.js +2 -0
- package/studio-dist/client/assets/usage-C2QJ_0IZ.js +55 -0
- package/studio-dist/client/assets/useAnchoredPopupScrollLock-Bj-FWb8z.js +1 -0
- package/studio-dist/client/assets/useMutation-aAr2uiay.js +1 -0
- package/studio-dist/client/assets/useSelector-AYcH7lB0.js +2 -0
- package/studio-dist/client/assets/vault-BGcxkSIY.js +1 -0
- package/studio-dist/client/assets/warnings-R1cE1rLf.js +1 -0
- package/studio-dist/client/assets/workflow-C2qTrMDH.js +1 -0
- package/studio-dist/server/assets/__23tanstack-start-server-fn-resolver-Bn896ZYj.js +122 -0
- package/studio-dist/server/assets/_tanstack-start-manifest_v.js +102 -35
- package/studio-dist/server/assets/{actions-Bvgj9eb8.js → actions-DYSv-9VV.js} +10 -4
- package/studio-dist/server/assets/artifacts-DnF9L3dZ.js +216 -0
- package/studio-dist/server/assets/{button-CIJqAhni.js → button-CgOIVdmD.js} +13 -1702
- package/studio-dist/server/assets/combobox-DV_s9W0s.js +483 -0
- package/studio-dist/server/assets/{companion-view-DISASvdJ.js → companion-view-BKVXD3TO.js} +3 -3
- package/studio-dist/server/assets/companions-C7fctnU9.js +31 -0
- package/studio-dist/server/assets/{createServerRpc-tK2lE9R2.js → createServerRpc-bFKFpGFG.js} +1 -1
- package/studio-dist/server/assets/{factory-DUTa0eDs.js → factory-C7Ybg0xG.js} +1256 -353
- package/studio-dist/server/assets/{factory-DGPmNGbo.js → factory-Df5uAHjb.js} +53 -6
- package/studio-dist/server/assets/fields-BbieQTD0.js +1190 -0
- package/studio-dist/server/assets/getting-started-B136TP57.js +4 -0
- package/studio-dist/server/assets/getting-started-CRxMWorQ.js +67 -0
- package/studio-dist/server/assets/getting-started._topic-DJKQAzYl.js +13 -0
- package/studio-dist/server/assets/getting-started.index-D84gec1t.js +13 -0
- package/studio-dist/server/assets/graph-CSje3029.js +801 -0
- package/studio-dist/server/assets/graph-DDZzXM1L.js +16 -0
- package/studio-dist/server/assets/graph-canvas-22bWB5-2.js +21066 -0
- package/studio-dist/server/assets/graph-wire-BdSUx0G5.js +58 -0
- package/studio-dist/server/assets/guides-D9dk1Frq.js +13 -0
- package/studio-dist/server/assets/guides-IE-19POz.js +5 -0
- package/studio-dist/server/assets/guides.server-vcYRY5wx.js +66 -0
- package/studio-dist/server/assets/jsx-runtime-Bt0s0YKS.js +451 -0
- package/studio-dist/server/assets/{markdown-view-Bn7xPdks.js → markdown-view-BR8dJ7aR.js} +28 -9
- package/studio-dist/server/assets/{panel-3Jmn4awF.js → panel-BR_t9iMM.js} +4 -4
- package/studio-dist/server/assets/queries-DWoTin-q.js +32 -0
- package/studio-dist/server/assets/react-CdqSUhaL.js +89 -0
- package/studio-dist/server/assets/redirect-BaZ_QyqF.js +39 -0
- package/studio-dist/server/assets/report-url-BUXPcE_8.js +9 -0
- package/studio-dist/server/assets/{reports-D6YMqhnO.js → reports-CQ9qQ1-n.js} +9 -10
- package/studio-dist/server/assets/{reports-C06RXVE2.js → reports-dYDEoBeq.js} +3 -3
- package/studio-dist/server/assets/{router-C8MGqlAY.js → router-DkRHNTpE.js} +787 -2862
- package/studio-dist/server/assets/{routes-BcFgvqf7.js → routes-B6GfOew4.js} +22 -171
- package/studio-dist/server/assets/{search-JAKurDNh.js → search-BJnWLfkG.js} +21 -2
- package/studio-dist/server/assets/select-Af7Wibii.js +1609 -0
- package/studio-dist/server/assets/{shell-DS3wUexh.js → shell-Hq-hXgnT.js} +6 -5
- package/studio-dist/server/assets/{skills-BEnOxiDl.js → skills-B7nEuVbx.js} +9 -8
- package/studio-dist/server/assets/{skills-CtrBAtX3.js → skills-B_YYL7q8.js} +3 -3
- package/studio-dist/server/assets/specs-BpLUFntA.js +51 -0
- package/studio-dist/server/assets/specs-view-0peMVd22.js +426 -0
- package/studio-dist/server/assets/spinner-CdwJW4VB.js +20 -0
- package/studio-dist/server/assets/{steps-Dq_CwY_s.js → steps-D56DApvK.js} +1 -1
- package/studio-dist/server/assets/store-Cs17okNG.js +280 -0
- package/studio-dist/server/assets/studio-CAcKp3bM.js +10340 -0
- package/studio-dist/server/assets/studio-queries-BtRGzDdh.js +1451 -0
- package/studio-dist/server/assets/usage-COdjd1gD.js +25570 -0
- package/studio-dist/server/assets/usage-f6pqE1tm.js +24 -0
- package/studio-dist/server/assets/useAnchoredPopupScrollLock-DlBKHRw7.js +756 -0
- package/studio-dist/server/assets/useMutation-DzTZGenE.js +690 -0
- package/studio-dist/server/assets/{__23tanstack-start-server-fn-resolver-M2GBs8s3.js → useSelector-DUmCUXAs.js} +4 -668
- package/studio-dist/server/assets/{vault-Fvxt2Tzl.js → vault-BDuIUq0F.js} +8 -5
- package/studio-dist/server/assets/{vault-BmWnryh3.js → vault-BVLp6zu3.js} +3 -3
- package/studio-dist/server/assets/{views-BgYa-za2.js → views-flRUr21z.js} +4 -4
- package/studio-dist/server/assets/{warnings-DzR6fyZh.js → warnings-CDy9pdgL.js} +3 -2
- package/studio-dist/server/assets/{delivery-path-C29fBynY.js → workflow-D9GKI2EV.js} +132 -106
- package/studio-dist/server/server.js +582 -8
- package/src/cli/commands/report/collector.ts +0 -288
- package/studio-dist/client/assets/app-CdPiFv59.css +0 -2
- package/studio-dist/client/assets/button-BIIHE0EQ.js +0 -3
- package/studio-dist/client/assets/companion-view-DM6YyXaR.js +0 -1
- package/studio-dist/client/assets/delivery-path-DELxj0Av.js +0 -1
- package/studio-dist/client/assets/factory-BZy0E8ty.js +0 -4
- package/studio-dist/client/assets/index-B3wkMzjA.js +0 -40
- package/studio-dist/client/assets/markdown-view-pQ2Qp_i5.js +0 -1
- package/studio-dist/client/assets/panel-CbajlC3p.js +0 -1
- package/studio-dist/client/assets/prepaint-CwSrpdFA.js +0 -27
- package/studio-dist/client/assets/react-CN2GSx7x.js +0 -1
- package/studio-dist/client/assets/reports-DllhYQ5_.js +0 -1
- package/studio-dist/client/assets/rolldown-runtime-WkwZ5CU-.js +0 -1
- package/studio-dist/client/assets/routes-COm-G3mK.js +0 -1
- package/studio-dist/client/assets/skills-DmWAy-jZ.js +0 -1
- package/studio-dist/client/assets/specs-BG3Wi3ln.js +0 -1
- package/studio-dist/client/assets/steps-DFsH3Neu.js +0 -1
- package/studio-dist/client/assets/vault-B2q1nBY5.js +0 -1
- package/studio-dist/client/assets/warnings-BSXIyuSQ.js +0 -1
- package/studio-dist/client/assets/workflow-DlsQD6Hc.js +0 -1
- package/studio-dist/server/assets/companions-BYbHWKOZ.js +0 -15
- package/studio-dist/server/assets/createServerFn-F5sAOF8V.js +0 -459
- package/studio-dist/server/assets/prepaint-DJfTQ_VH.js +0 -45
- package/studio-dist/server/assets/react-Ce6dj15C.js +0 -46
- package/studio-dist/server/assets/rolldown-runtime-DYf_mQCB.js +0 -34
- package/studio-dist/server/assets/specs-C8rv34-5.js +0 -211
- package/studio-dist/server/assets/studio-BeltxxKt.js +0 -216
- package/studio-dist/server/assets/workflow-liRYecHk.js +0 -73
|
@@ -13,10 +13,12 @@ import { hasLaunchEnvironment } from "../runtime/env";
|
|
|
13
13
|
import { MATE_ENV } from "../runtime/env-names";
|
|
14
14
|
import { resolveProjection } from "../runtime/projection";
|
|
15
15
|
import { checkSecretAccess, type SecretToolCall } from "../runtime/secret-guard";
|
|
16
|
+
import { isSecretPolicyFile } from "../runtime/secret-policy-weakening";
|
|
16
17
|
import { routeTokensaveInput } from "../runtime/tokensave-route";
|
|
17
18
|
import { readWrapStateSync } from "../runtime/wrap-state";
|
|
18
19
|
import {
|
|
19
20
|
buildArtifactError,
|
|
21
|
+
expandHome,
|
|
20
22
|
extractPatchAdditions,
|
|
21
23
|
extractPatchPaths,
|
|
22
24
|
isArtifactPath,
|
|
@@ -117,16 +119,44 @@ function secretToolCall(
|
|
|
117
119
|
case "edit":
|
|
118
120
|
return {
|
|
119
121
|
kind: "write",
|
|
120
|
-
writes: [
|
|
122
|
+
writes: [
|
|
123
|
+
{
|
|
124
|
+
path: target(args.filePath),
|
|
125
|
+
content: text(args.newString),
|
|
126
|
+
result: {
|
|
127
|
+
kind: "replace",
|
|
128
|
+
edits: [
|
|
129
|
+
{
|
|
130
|
+
oldText: text(args.oldString),
|
|
131
|
+
newText: text(args.newString),
|
|
132
|
+
all: args.replaceAll === true,
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
],
|
|
121
138
|
};
|
|
122
|
-
case "apply_patch":
|
|
139
|
+
case "apply_patch": {
|
|
140
|
+
const patchText = text(args.patchText);
|
|
123
141
|
return {
|
|
124
142
|
kind: "write",
|
|
125
|
-
writes:
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
143
|
+
writes: [
|
|
144
|
+
...extractPatchAdditions(patchText).map((addition) => ({
|
|
145
|
+
path: target(addition.path),
|
|
146
|
+
content: addition.content,
|
|
147
|
+
})),
|
|
148
|
+
// A patch's result is not replayed, so a patch touching the policy file is judged unknown.
|
|
149
|
+
...extractPatchPaths(patchText)
|
|
150
|
+
.map(target)
|
|
151
|
+
.filter((filePath) => isSecretPolicyFile(filePath, context.companionPath))
|
|
152
|
+
.map((filePath) => ({
|
|
153
|
+
path: filePath,
|
|
154
|
+
content: "",
|
|
155
|
+
result: { kind: "unknown" as const },
|
|
156
|
+
})),
|
|
157
|
+
],
|
|
129
158
|
};
|
|
159
|
+
}
|
|
130
160
|
case "bash":
|
|
131
161
|
return { kind: "shell", command: text(args.command) };
|
|
132
162
|
case "grep":
|
|
@@ -136,6 +166,9 @@ function secretToolCall(
|
|
|
136
166
|
}
|
|
137
167
|
}
|
|
138
168
|
|
|
169
|
+
/** OpenCode v2 names these tools `patch` and `shell` and their file argument `path`; v1 names are kept. */
|
|
170
|
+
const V1_TOOL_NAMES: Record<string, string> = { patch: "apply_patch", shell: "bash" };
|
|
171
|
+
|
|
139
172
|
/**
|
|
140
173
|
* Rejects when the tool call touches a secret, commits with a model co-author
|
|
141
174
|
* trailer, or would write an artifact outside the companion. Checked in that
|
|
@@ -143,11 +176,16 @@ function secretToolCall(
|
|
|
143
176
|
*/
|
|
144
177
|
export async function guardToolInput(
|
|
145
178
|
context: CompanionContext,
|
|
146
|
-
|
|
179
|
+
toolName: string,
|
|
147
180
|
input: unknown,
|
|
148
181
|
env: Record<string, string | undefined> = process.env,
|
|
149
182
|
): Promise<void> {
|
|
150
|
-
const
|
|
183
|
+
const tool = V1_TOOL_NAMES[toolName] ?? toolName;
|
|
184
|
+
const args = { ...(input as Record<string, unknown> | undefined) };
|
|
185
|
+
if (args.filePath === undefined) args.filePath = args.path;
|
|
186
|
+
for (const key of ["filePath", "path"]) {
|
|
187
|
+
if (typeof args[key] === "string") args[key] = expandHome(args[key]);
|
|
188
|
+
}
|
|
151
189
|
|
|
152
190
|
const secretCall = context.companionPath ? secretToolCall(context, tool, args) : null;
|
|
153
191
|
if (secretCall) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
2
|
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
|
|
5
6
|
import { artifactRefusal, resolveArtifactTarget } from "../runtime/artifact-refusal";
|
|
@@ -140,7 +141,14 @@ export function shouldBlockArtifactWrite(context: CompanionContext, filePath: st
|
|
|
140
141
|
return !isGitignored(context, filePath);
|
|
141
142
|
}
|
|
142
143
|
|
|
143
|
-
|
|
144
|
+
/** OpenCode expands a leading `~` to the home folder before it touches a path. */
|
|
145
|
+
export function expandHome(filePath: string): string {
|
|
146
|
+
if (filePath === "~") return os.homedir();
|
|
147
|
+
return filePath.startsWith("~/") ? path.join(os.homedir(), filePath.slice(2)) : filePath;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** `*** Move to:` is OpenCode v2's move line; `*** Move to File:` is kept. */
|
|
151
|
+
const PATCH_FILE_MARKER = /\*\*\* (?:(?:Add|Update|Delete) File|Move to(?: File)?):\s*(.+)$/gm;
|
|
144
152
|
|
|
145
153
|
export function extractPatchPaths(patchText: string): string[] {
|
|
146
154
|
const paths: string[] = [];
|
|
@@ -148,7 +156,7 @@ export function extractPatchPaths(patchText: string): string[] {
|
|
|
148
156
|
while ((match = PATCH_FILE_MARKER.exec(patchText)) !== null) {
|
|
149
157
|
const raw = match[1].trim();
|
|
150
158
|
if (raw) {
|
|
151
|
-
paths.push(raw);
|
|
159
|
+
paths.push(expandHome(raw));
|
|
152
160
|
}
|
|
153
161
|
}
|
|
154
162
|
return paths;
|
|
@@ -162,9 +170,11 @@ export function extractPatchAdditions(patchText: string): { path: string; conten
|
|
|
162
170
|
const additions: { path: string; content: string[] }[] = [];
|
|
163
171
|
let current: { path: string; content: string[] } | undefined;
|
|
164
172
|
for (const line of patchText.split("\n")) {
|
|
165
|
-
const marker = /^\*\*\* (Add|Update|Delete|Move to
|
|
173
|
+
const marker = /^\*\*\* (Add File|Update File|Delete File|Move to(?: File)?):\s*(.+)$/.exec(
|
|
174
|
+
line,
|
|
175
|
+
);
|
|
166
176
|
if (marker) {
|
|
167
|
-
if (marker[1]
|
|
177
|
+
if (marker[1].startsWith("Move to") && current) {
|
|
168
178
|
current.path = marker[2].trim();
|
|
169
179
|
} else {
|
|
170
180
|
current = { path: marker[2].trim(), content: [] };
|
|
@@ -174,7 +184,10 @@ export function extractPatchAdditions(patchText: string): { path: string; conten
|
|
|
174
184
|
}
|
|
175
185
|
if (current && line.startsWith("+")) current.content.push(line.slice(1));
|
|
176
186
|
}
|
|
177
|
-
return additions.map((entry) => ({
|
|
187
|
+
return additions.map((entry) => ({
|
|
188
|
+
path: expandHome(entry.path),
|
|
189
|
+
content: entry.content.join("\n"),
|
|
190
|
+
}));
|
|
178
191
|
}
|
|
179
192
|
|
|
180
193
|
export function buildArtifactError(context: CompanionContext, filePath: string): string {
|
|
@@ -1,23 +1,41 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
1
2
|
import path from "node:path";
|
|
2
3
|
|
|
3
4
|
import type { Context } from "@opencode/plugin/promise/plugin";
|
|
5
|
+
import { parse } from "yaml";
|
|
4
6
|
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
checkoutRootOf,
|
|
9
|
+
isLinkedWorktreeOf,
|
|
10
|
+
mainCheckoutOf,
|
|
11
|
+
readGitLayout,
|
|
12
|
+
} from "../runtime/git-layout";
|
|
6
13
|
import { realPathOrNull as realPath } from "../runtime/physical-path";
|
|
7
|
-
import { companionLinkPath } from "../runtime/repo-local";
|
|
14
|
+
import { companionLinkPath, repoLocalRegistryPath } from "../runtime/repo-local";
|
|
15
|
+
import {
|
|
16
|
+
mirroredUmbrellaOf,
|
|
17
|
+
umbrellaFolderOf,
|
|
18
|
+
umbrellaMirroredBy,
|
|
19
|
+
} from "../runtime/umbrella-folder";
|
|
8
20
|
import type { CompanionContext } from "./companion-policy";
|
|
21
|
+
import { eventDirectory, openCodeRegisterDeps, type SessionEvent } from "./session-register";
|
|
9
22
|
|
|
10
23
|
export const ENTER_WORKTREE_TOOL = "mate_enter_worktree";
|
|
24
|
+
export const EXIT_WORKTREE_TOOL = "mate_exit_worktree";
|
|
11
25
|
|
|
12
26
|
/**
|
|
13
27
|
* Why the session may not move into `target`, or null when `target` is a linked worktree
|
|
14
|
-
* of the session's Working Repository
|
|
28
|
+
* of the session's Working Repository, or its Umbrella Repository's mirrored worktree,
|
|
29
|
+
* whose Repository Link names the session's companion.
|
|
15
30
|
*/
|
|
16
31
|
export function enterWorktreeRefusal(context: CompanionContext, target: string): string | null {
|
|
17
32
|
if (!context.repositoryPath) return "this session has no Working Repository";
|
|
18
33
|
if (!path.isAbsolute(target)) return `${target} is not an absolute path`;
|
|
19
34
|
const worktree = realPath(target);
|
|
20
35
|
if (!worktree) return `${target} does not exist`;
|
|
36
|
+
if (umbrellaFolderOf(worktree) === worktree) {
|
|
37
|
+
return mirroredWorktreeRefusal(context, target, worktree);
|
|
38
|
+
}
|
|
21
39
|
if (!isLinkedWorktreeOf(worktree, context.repositoryPath)) {
|
|
22
40
|
// No `.git` of its own: a folder inside a checkout, not its root.
|
|
23
41
|
return readGitLayout(worktree)
|
|
@@ -31,15 +49,147 @@ export function enterWorktreeRefusal(context: CompanionContext, target: string):
|
|
|
31
49
|
return null;
|
|
32
50
|
}
|
|
33
51
|
|
|
34
|
-
/**
|
|
52
|
+
/** The repository id `folder`'s repo-local link records for the companion; null without one. */
|
|
53
|
+
function linkedIdTo(folder: string, companion: string): string | null {
|
|
54
|
+
try {
|
|
55
|
+
const parsed = parse(fs.readFileSync(repoLocalRegistryPath(folder), "utf8")) as {
|
|
56
|
+
companions?: { path?: unknown; repositoryId?: unknown }[];
|
|
57
|
+
} | null;
|
|
58
|
+
for (const pointer of Array.isArray(parsed?.companions) ? parsed.companions : []) {
|
|
59
|
+
if (typeof pointer?.path !== "string" || typeof pointer.repositoryId !== "string") continue;
|
|
60
|
+
if (realPath(pointer.path) === companion) return pointer.repositoryId;
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
// An unreadable link links nothing.
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Why the session may not move into the non-Git folder `worktree`, or null when it is
|
|
70
|
+
* the session's Umbrella Repository's mirrored worktree, linked under the umbrella's id
|
|
71
|
+
* to the session's companion.
|
|
72
|
+
*/
|
|
73
|
+
function mirroredWorktreeRefusal(
|
|
74
|
+
context: CompanionContext,
|
|
75
|
+
target: string,
|
|
76
|
+
worktree: string,
|
|
77
|
+
): string | null {
|
|
78
|
+
// A session already in a mirrored worktree moves between its umbrella's.
|
|
79
|
+
const home = homeOf(context.repositoryPath!);
|
|
80
|
+
if (!isWorktreeOf(worktree, home)) {
|
|
81
|
+
return `${target} is not a mirrored worktree of ${context.repositoryPath}`;
|
|
82
|
+
}
|
|
83
|
+
const companion = realPath(context.companionPath);
|
|
84
|
+
const id = companion ? linkedIdTo(worktree, companion) : null;
|
|
85
|
+
if (!id || id !== linkedIdTo(home, companion!)) {
|
|
86
|
+
return `${target} is not linked to the companion ${context.companionPath}`;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Where a session at `repositoryPath` returns to: the main checkout, or for an Umbrella
|
|
93
|
+
* Repository or its mirrored worktree, the umbrella.
|
|
94
|
+
*/
|
|
95
|
+
function homeOf(repositoryPath: string): string {
|
|
96
|
+
const folder = realPath(repositoryPath);
|
|
97
|
+
if (!folder || umbrellaFolderOf(folder) !== folder) return mainCheckoutOf(repositoryPath);
|
|
98
|
+
const mirrored = mirroredUmbrellaOf(folder);
|
|
99
|
+
const umbrella = mirrored ? realPath(mirrored) : null;
|
|
100
|
+
return umbrella && umbrellaFolderOf(umbrella) === umbrella ? umbrella : folder;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The umbrella folder or Git checkout holding `dir`; null outside both. A child checkout
|
|
105
|
+
* of a mirrored worktree belongs to that worktree.
|
|
106
|
+
*/
|
|
107
|
+
function rootOf(dir: string): string | null {
|
|
108
|
+
const real = realPath(dir);
|
|
109
|
+
const umbrella = real ? umbrellaFolderOf(real) : null;
|
|
110
|
+
if (umbrella) return umbrella;
|
|
111
|
+
const checkout = checkoutRootOf(real ?? dir);
|
|
112
|
+
const parent = checkout ? path.dirname(checkout) : null;
|
|
113
|
+
return parent && umbrellaFolderOf(parent) === parent && umbrellaMirroredBy(parent)
|
|
114
|
+
? parent
|
|
115
|
+
: checkout;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** `root` is a linked Git worktree of `home`, or a mirrored worktree of the umbrella `home`. */
|
|
119
|
+
function isWorktreeOf(root: string, home: string): boolean {
|
|
120
|
+
if (isLinkedWorktreeOf(root, home)) return true;
|
|
121
|
+
const umbrella = mirroredUmbrellaOf(root);
|
|
122
|
+
return !!umbrella && umbrellaFolderOf(root) === root && realPath(umbrella) === home;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface FollowState {
|
|
126
|
+
/** The worktree a session entered last. */
|
|
127
|
+
followed: string | null;
|
|
128
|
+
/** Sessions moved by following, with the worktree their note names. */
|
|
129
|
+
notes: Map<string, string>;
|
|
130
|
+
/** Follow moves in flight; a session's first request waits for its move. */
|
|
131
|
+
moving: Map<string, Promise<void>>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Per home (main checkout or umbrella), shared by the plugin's instances: OpenCode sets the
|
|
136
|
+
* plugin up once per location, and a session's later tool calls run in its worktree's
|
|
137
|
+
* instance. Kept on `globalThis` so a module loaded again in the same process shares it.
|
|
138
|
+
*/
|
|
139
|
+
const states: Map<string, FollowState> = ((globalThis as Record<symbol, unknown>)[
|
|
140
|
+
Symbol.for("mate.opencode.followStates")
|
|
141
|
+
] ??= new Map<string, FollowState>()) as Map<string, FollowState>;
|
|
142
|
+
|
|
143
|
+
function stateOf(home: string): FollowState {
|
|
144
|
+
let state = states.get(home);
|
|
145
|
+
if (!state) {
|
|
146
|
+
state = { followed: null, notes: new Map(), moving: new Map() };
|
|
147
|
+
states.set(home, state);
|
|
148
|
+
}
|
|
149
|
+
return state;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
interface SystemText {
|
|
153
|
+
type?: string;
|
|
154
|
+
text?: string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function followedNote(worktree: string, frameworkName: string): string {
|
|
158
|
+
return `${frameworkName}: this session was moved into the worktree ${worktree}, the last one a session of this OpenCode server entered; it is the Working Repository. \`${EXIT_WORKTREE_TOOL}\` returns to the main checkout.`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Appends to the last text part: the companion prompt keeps the system prompt one entry. */
|
|
162
|
+
function appendSystemNote(system: unknown[], note: string): void {
|
|
163
|
+
const parts = system as SystemText[];
|
|
164
|
+
if (parts.some((part) => part?.text?.includes(note))) return;
|
|
165
|
+
const last = parts.at(-1);
|
|
166
|
+
if (last?.type === "text" && typeof last.text === "string") last.text = `${last.text}\n\n${note}`;
|
|
167
|
+
else system.push({ type: "text", text: note });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface EnterWorktreeDeps {
|
|
171
|
+
/** Registered sessions were started by an editor in a directory it chose; they never follow. */
|
|
172
|
+
registered: (sessionID: string) => Promise<boolean>;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Registers `mate_enter_worktree` and `mate_exit_worktree`, which move the calling session
|
|
177
|
+
* through the plugin's own client, which talks to the server that owns it. New root sessions
|
|
178
|
+
* created in the main checkout follow the last entered worktree, held in memory only.
|
|
179
|
+
* Returns the cleanup that ends the event subscription.
|
|
180
|
+
*/
|
|
35
181
|
export async function registerEnterWorktree(
|
|
36
182
|
api: Context,
|
|
37
183
|
context: CompanionContext,
|
|
38
|
-
|
|
184
|
+
deps: EnterWorktreeDeps = { registered: openCodeRegisterDeps.registered },
|
|
185
|
+
): Promise<() => void> {
|
|
186
|
+
const home = context.repositoryPath ? homeOf(context.repositoryPath) : null;
|
|
187
|
+
const state = home ? stateOf(home) : null;
|
|
188
|
+
|
|
39
189
|
await api.tool.transform((editor) => {
|
|
40
190
|
editor.add({
|
|
41
191
|
name: ENTER_WORKTREE_TOOL,
|
|
42
|
-
description: `Move this session into a linked Git worktree of the Working Repository that ${context.frameworkName} created, such as the path \`${context.frameworkName} working worktree --enter\` reports. Later tool calls run there
|
|
192
|
+
description: `Move this session into a linked Git worktree of the Working Repository, or an Umbrella Repository's mirrored worktree, that ${context.frameworkName} created, such as the path \`${context.frameworkName} working worktree --enter\` reports. Later tool calls run there, and new sessions follow until \`${EXIT_WORKTREE_TOOL}\`.`,
|
|
43
193
|
input: {
|
|
44
194
|
type: "object",
|
|
45
195
|
properties: {
|
|
@@ -62,11 +212,109 @@ export async function registerEnterWorktree(
|
|
|
62
212
|
}
|
|
63
213
|
const directory = realPath(target as string)!;
|
|
64
214
|
await api.session.move({ sessionID: tool.sessionID, directory: directory as never });
|
|
215
|
+
if (state) {
|
|
216
|
+
state.followed = directory;
|
|
217
|
+
state.notes.delete(tool.sessionID);
|
|
218
|
+
}
|
|
65
219
|
return {
|
|
66
220
|
content: `Moved this session to ${directory}; it is now the Working Repository.`,
|
|
67
221
|
metadata: { moved: true, directory },
|
|
68
222
|
};
|
|
69
223
|
},
|
|
70
224
|
});
|
|
225
|
+
editor.add({
|
|
226
|
+
name: EXIT_WORKTREE_TOOL,
|
|
227
|
+
description: `Move this session from a linked Git worktree back to the Working Repository's main checkout, or from a mirrored worktree back to its Umbrella Repository; new sessions stop following the worktree. The worktree stays.`,
|
|
228
|
+
input: { type: "object", properties: {}, additionalProperties: false },
|
|
229
|
+
async execute(_input, tool) {
|
|
230
|
+
if (!home || !state) {
|
|
231
|
+
return {
|
|
232
|
+
content: "Refused: this session has no Working Repository.",
|
|
233
|
+
metadata: { moved: false },
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
// Forget first: a failed lookup must still stop new sessions following.
|
|
237
|
+
state.followed = null;
|
|
238
|
+
state.notes.delete(tool.sessionID);
|
|
239
|
+
const session = await api.session.get({ sessionID: tool.sessionID as never });
|
|
240
|
+
const current = rootOf(session.location.directory);
|
|
241
|
+
if (!current || !isWorktreeOf(current, home)) {
|
|
242
|
+
return {
|
|
243
|
+
content: `This session is not in a linked worktree; it stays in ${session.location.directory}. New sessions no longer follow a worktree.`,
|
|
244
|
+
metadata: { moved: false },
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
await api.session.move({ sessionID: tool.sessionID, directory: home as never });
|
|
248
|
+
return {
|
|
249
|
+
content: `Moved this session to ${home}; it is now the Working Repository. ${current} stays.`,
|
|
250
|
+
metadata: { moved: true, directory: home },
|
|
251
|
+
};
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
if (!home || !state) return () => {};
|
|
257
|
+
|
|
258
|
+
await api.session.hook("context", async (event) => {
|
|
259
|
+
await state.moving.get(event.sessionID);
|
|
260
|
+
const worktree = state.notes.get(event.sessionID);
|
|
261
|
+
if (!worktree) return;
|
|
262
|
+
const session = await api.session.get({ sessionID: event.sessionID }).catch(() => null);
|
|
263
|
+
if (!session) return;
|
|
264
|
+
// Left the worktree some other way, or the worktree is gone.
|
|
265
|
+
if (rootOf(session.location.directory) !== worktree) {
|
|
266
|
+
state.notes.delete(event.sessionID);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
appendSystemNote(event.system, followedNote(worktree, context.frameworkName));
|
|
71
270
|
});
|
|
271
|
+
|
|
272
|
+
const follow = async (id: string) => {
|
|
273
|
+
const worktree = state.followed;
|
|
274
|
+
if (!worktree) return;
|
|
275
|
+
if (enterWorktreeRefusal(context, worktree)) {
|
|
276
|
+
if (state.followed === worktree) state.followed = null;
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (await deps.registered(id).catch(() => true)) return;
|
|
280
|
+
// An exit or another enter during the lookup wins.
|
|
281
|
+
if (state.followed !== worktree) return;
|
|
282
|
+
await api.session.move({ sessionID: id as never, directory: worktree as never });
|
|
283
|
+
state.notes.set(id, worktree);
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Only instances at home follow, each for sessions created at its own location,
|
|
288
|
+
* so a server-wide stream moves a session once.
|
|
289
|
+
*/
|
|
290
|
+
const location = realPath(api.location.directory);
|
|
291
|
+
if (!location || rootOf(location) !== home) return () => {};
|
|
292
|
+
const controller = new AbortController();
|
|
293
|
+
void (async () => {
|
|
294
|
+
try {
|
|
295
|
+
for await (const raw of api.event.subscribe({ signal: controller.signal })) {
|
|
296
|
+
const event = raw as SessionEvent;
|
|
297
|
+
const id = event.data?.sessionID;
|
|
298
|
+
if (!id) continue;
|
|
299
|
+
if (event.type === "session.created") {
|
|
300
|
+
if (!state.followed || event.data?.parentID) continue;
|
|
301
|
+
const directory = eventDirectory(event);
|
|
302
|
+
if (!directory || realPath(directory) !== location) continue;
|
|
303
|
+
// Not awaited: a slow move must not hold up later events.
|
|
304
|
+
state.moving.set(
|
|
305
|
+
id,
|
|
306
|
+
follow(id)
|
|
307
|
+
.catch(() => undefined)
|
|
308
|
+
.finally(() => state.moving.delete(id)),
|
|
309
|
+
);
|
|
310
|
+
} else if (event.type === "session.deleted") {
|
|
311
|
+
state.notes.delete(id);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
} catch {
|
|
315
|
+
/* subscription closed: the location is shutting down */
|
|
316
|
+
}
|
|
317
|
+
})();
|
|
318
|
+
|
|
319
|
+
return () => controller.abort();
|
|
72
320
|
}
|
|
@@ -53,7 +53,7 @@ export const openCodeRegisterDeps: OpenCodeRegisterDeps = {
|
|
|
53
53
|
now: () => new Date(),
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
-
interface SessionEvent {
|
|
56
|
+
export interface SessionEvent {
|
|
57
57
|
type?: string;
|
|
58
58
|
location?: { directory?: string };
|
|
59
59
|
data?: {
|
|
@@ -85,7 +85,7 @@ function activityAfter(event: SessionEvent): SessionActivity | undefined {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
function eventDirectory(event: SessionEvent): string | undefined {
|
|
88
|
+
export function eventDirectory(event: SessionEvent): string | undefined {
|
|
89
89
|
return event.location?.directory ?? event.data?.location?.directory;
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { checkoutAt } from "./git-layout";
|
|
6
|
+
|
|
7
|
+
type IsolationEnv = Readonly<Record<string, string | undefined>>;
|
|
8
|
+
type Isolation = "worktree" | "none";
|
|
9
|
+
|
|
10
|
+
export interface ClaudeSessionIsolation {
|
|
11
|
+
isolated: boolean;
|
|
12
|
+
/** Isolation values Claude Code does not define, each ignored. */
|
|
13
|
+
diagnostics: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isIsolation(value: unknown): value is Isolation {
|
|
17
|
+
return value === "worktree" || value === "none";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function ignored(source: string, value: unknown): string {
|
|
21
|
+
return `${source} is ${JSON.stringify(value)}, not "worktree" or "none"; ignored`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function bgIsolationOf(
|
|
25
|
+
settings: unknown,
|
|
26
|
+
source: string,
|
|
27
|
+
diagnostics: string[],
|
|
28
|
+
): Isolation | undefined {
|
|
29
|
+
if (typeof settings !== "object" || settings === null) return undefined;
|
|
30
|
+
const worktree = (settings as { worktree?: unknown }).worktree;
|
|
31
|
+
if (typeof worktree !== "object" || worktree === null) return undefined;
|
|
32
|
+
const value = (worktree as { bgIsolation?: unknown }).bgIsolation;
|
|
33
|
+
if (isIsolation(value)) return value;
|
|
34
|
+
if (value !== undefined) diagnostics.push(ignored(`worktree.bgIsolation in ${source}`, value));
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Lenient like Claude Code: unparseable JSON, or an unreadable file, sets nothing.
|
|
39
|
+
function parseJson(text: string): unknown {
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(text);
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readJson(file: string): unknown {
|
|
48
|
+
try {
|
|
49
|
+
return parseJson(fs.readFileSync(file, "utf8"));
|
|
50
|
+
} catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A `--settings` value, a file path or inline JSON as Claude reads it. */
|
|
56
|
+
function flagSettings(value: string): unknown {
|
|
57
|
+
return value.trimStart().startsWith("{") ? parseJson(value) : readJson(value);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function settingsFlagValue(flags: readonly unknown[], index: number): unknown {
|
|
61
|
+
const flag = flags[index];
|
|
62
|
+
if (flag === "--settings") return flags[index + 1];
|
|
63
|
+
if (typeof flag === "string" && flag.startsWith("--settings=")) {
|
|
64
|
+
return flag.slice("--settings=".length);
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The isolation a background job was started with: its own (e.g. `none` for a
|
|
71
|
+
* backgrounded REPL), else the last `--settings` it was launched with that sets one.
|
|
72
|
+
*/
|
|
73
|
+
function jobIsolation(jobDir: string, diagnostics: string[]): Isolation | undefined {
|
|
74
|
+
const state = readJson(path.join(jobDir, "state.json")) as {
|
|
75
|
+
bgIsolation?: unknown;
|
|
76
|
+
respawnFlags?: unknown;
|
|
77
|
+
} | null;
|
|
78
|
+
const own = state?.bgIsolation;
|
|
79
|
+
if (isIsolation(own)) return own;
|
|
80
|
+
const flags = Array.isArray(state?.respawnFlags) ? state.respawnFlags : [];
|
|
81
|
+
let isolation: Isolation | undefined;
|
|
82
|
+
for (let index = 0; index < flags.length; index += 1) {
|
|
83
|
+
const value = settingsFlagValue(flags, index);
|
|
84
|
+
if (typeof value !== "string") continue;
|
|
85
|
+
isolation = bgIsolationOf(flagSettings(value), "--settings", diagnostics) ?? isolation;
|
|
86
|
+
}
|
|
87
|
+
return isolation;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether Claude Code refuses this session's writes in `repoPath`: true for a background
|
|
92
|
+
* session whose isolation is not `none`, when `repoPath` lies in a Git checkout that is
|
|
93
|
+
* not a linked worktree. A folder outside Git, such as an Umbrella Repository, is never
|
|
94
|
+
* isolated.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* A background session is told by `CLAUDE_JOB_DIR`; Claude usually hides
|
|
98
|
+
* `CLAUDE_CODE_SESSION_KIND` and `CLAUDE_BG_ISOLATION` from its tools, and either is
|
|
99
|
+
* honored when visible. Isolation resolves from `CLAUDE_BG_ISOLATION`, then the job's
|
|
100
|
+
* `state.json` (its own isolation, else the `--settings` it was launched with, as the
|
|
101
|
+
* VS Code extension sets), then `worktree.bgIsolation` in
|
|
102
|
+
* `<repoPath>/.claude/settings.local.json`, `<repoPath>/.claude/settings.json`, the user
|
|
103
|
+
* `settings.json` (`CLAUDE_CONFIG_DIR`, else `~/.claude`), else `worktree`. An undefined
|
|
104
|
+
* value is ignored and reported. Managed policy and the folder the session started in are
|
|
105
|
+
* not visible here.
|
|
106
|
+
*/
|
|
107
|
+
export function claudeSessionIsolation(
|
|
108
|
+
env: IsolationEnv,
|
|
109
|
+
repoPath: string,
|
|
110
|
+
): ClaudeSessionIsolation {
|
|
111
|
+
const diagnostics: string[] = [];
|
|
112
|
+
const background = env.CLAUDE_CODE_SESSION_KIND === "bg" || Boolean(env.CLAUDE_JOB_DIR);
|
|
113
|
+
if (!background) return { isolated: false, diagnostics };
|
|
114
|
+
// Claude guards only a Git checkout, found upward as Git finds it, that is not a linked worktree.
|
|
115
|
+
const checkout = checkoutAt(repoPath);
|
|
116
|
+
if (!checkout || checkout.layout.kind === "linked") return { isolated: false, diagnostics };
|
|
117
|
+
const isolated = resolveIsolation(env, repoPath, diagnostics) !== "none";
|
|
118
|
+
return { isolated, diagnostics };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function resolveIsolation(env: IsolationEnv, repoPath: string, diagnostics: string[]): Isolation {
|
|
122
|
+
const variable = env.CLAUDE_BG_ISOLATION;
|
|
123
|
+
if (isIsolation(variable)) return variable;
|
|
124
|
+
if (variable) diagnostics.push(ignored("CLAUDE_BG_ISOLATION", variable));
|
|
125
|
+
const job = env.CLAUDE_JOB_DIR ? jobIsolation(env.CLAUDE_JOB_DIR, diagnostics) : undefined;
|
|
126
|
+
if (job) return job;
|
|
127
|
+
const userDir = env.CLAUDE_CONFIG_DIR || path.join(env.HOME || os.homedir(), ".claude");
|
|
128
|
+
const files = [
|
|
129
|
+
path.join(repoPath, ".claude", "settings.local.json"),
|
|
130
|
+
path.join(repoPath, ".claude", "settings.json"),
|
|
131
|
+
path.join(userDir, "settings.json"),
|
|
132
|
+
];
|
|
133
|
+
for (const file of files) {
|
|
134
|
+
const value = bgIsolationOf(readJson(file), file, diagnostics);
|
|
135
|
+
if (value) return value;
|
|
136
|
+
}
|
|
137
|
+
return "worktree";
|
|
138
|
+
}
|
|
@@ -449,11 +449,19 @@ async function resolveUpstreamTarget(
|
|
|
449
449
|
): Promise<UpstreamTarget | null> {
|
|
450
450
|
const upstream = await run(["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]);
|
|
451
451
|
const configured = parseRemoteRef(upstream.stdout);
|
|
452
|
-
if (upstream.status === 0 && configured)
|
|
452
|
+
if (upstream.status === 0 && configured) {
|
|
453
|
+
return configured.remote.startsWith("-") || configured.branch.startsWith("-")
|
|
454
|
+
? null
|
|
455
|
+
: configured;
|
|
456
|
+
}
|
|
453
457
|
|
|
454
458
|
const remoteHead = await run(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]);
|
|
455
459
|
const fromRemoteHead = parseRemoteRef(remoteHead.stdout);
|
|
456
|
-
if (remoteHead.status === 0 && fromRemoteHead)
|
|
460
|
+
if (remoteHead.status === 0 && fromRemoteHead) {
|
|
461
|
+
return fromRemoteHead.remote.startsWith("-") || fromRemoteHead.branch.startsWith("-")
|
|
462
|
+
? null
|
|
463
|
+
: fromRemoteHead;
|
|
464
|
+
}
|
|
457
465
|
|
|
458
466
|
for (const branch of ["main", "master"]) {
|
|
459
467
|
// oxlint-disable-next-line no-await-in-loop -- the steps are a decision chain
|