@uniqbit/mate-core 1.0.0-canary.6 → 1.0.0-canary.7
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/package.json +1 -1
- package/src/cli/commands/doctor.ts +51 -0
- package/src/cli/commands/friction.ts +558 -0
- package/src/cli/commands/studio/connection.ts +53 -0
- package/src/cli/commands/studio/routes.ts +3 -0
- package/src/cli/commands/studio/selection.ts +10 -1
- package/src/cli/commands/studio/server.ts +31 -0
- package/src/cli/commands/studio/services.ts +25 -0
- package/src/cli/commands/triage.ts +151 -0
- package/src/cli/commands/vscode/release-vsix.json +2 -2
- package/src/cli/main.ts +35 -13
- package/src/cli/plugin-commands.ts +6 -1
- package/src/cli/usage.ts +10 -0
- package/src/create-mate.ts +11 -5
- package/src/distribution.ts +2 -0
- package/src/hooks/validate-artifact-path.ts +64 -14
- package/src/lib/friction/describe.ts +42 -0
- package/src/lib/friction/fields.ts +60 -0
- package/src/lib/friction/mark.ts +156 -0
- package/src/lib/friction/view/model.ts +67 -0
- package/src/lib/friction/view/read.ts +136 -0
- package/src/lib/friction/view/select.ts +54 -0
- package/src/lib/orchestrator/adapters/claude.ts +5 -4
- package/src/lib/orchestrator/adapters/opencode.ts +2 -0
- package/src/lib/orchestrator/root-context.ts +23 -6
- package/src/lib/package-paths.ts +23 -15
- package/src/lib/session-attribution.ts +3 -7
- package/src/lib/triage/context.ts +186 -0
- package/src/lib/triage/launch.ts +52 -0
- package/src/lib/triage/prepare.ts +97 -0
- package/src/lib/triage/session.ts +126 -0
- package/src/opencode/companion-hooks.ts +53 -5
- package/src/opencode/projected-guidance.ts +2 -0
- package/src/playbooks/companion-guidance.ts +2 -2
- package/src/plugin-api.ts +5 -0
- package/src/runtime/command-friction.ts +97 -0
- package/src/runtime/companion-guidance.ts +32 -15
- package/src/runtime/friction-fingerprint.ts +35 -0
- package/src/runtime/friction-log.ts +352 -0
- package/src/runtime/friction-recorder.ts +199 -0
- package/src/runtime/friction-retention.ts +1 -0
- package/src/runtime/friction-state.ts +110 -0
- package/src/runtime/friction-switch.ts +26 -0
- package/src/runtime/friction-vocabulary.ts +42 -0
- package/src/runtime/projected-guidance.ts +2 -1
- package/src/runtime/secret-values.ts +16 -0
- package/src/runtime/session-id.ts +24 -0
- package/src/runtime/session-prepare-runner.ts +3 -0
- package/src/runtime/workflow/next-handoff-process.ts +2 -1
- package/src/studio.ts +41 -0
- package/src/templates/triage/PLAYBOOK.md +112 -0
- package/src/tools/setup/capabilities/rtk.ts +19 -8
- package/src/tools/setup/plugin.ts +5 -0
- package/src/tools/setup/service.ts +4 -0
- package/src/tools/setup.ts +16 -2
- package/studio-dist/client/assets/app-aIwDxf48.css +2 -0
- package/studio-dist/client/assets/button-CX0sWAJl.js +1 -0
- package/studio-dist/client/assets/combobox-BmA8RjOc.js +1 -0
- package/studio-dist/client/assets/{companion-view-BVaZNtiI.js → companion-view-DINdPF-b.js} +1 -1
- package/studio-dist/client/assets/fields-BobQ7zRF.js +1 -0
- package/studio-dist/client/assets/friction-YdYd3zaz.js +1 -0
- package/studio-dist/client/assets/getting-started-CY7NEtTw.js +1 -0
- package/studio-dist/client/assets/getting-started-djaQeWlu.js +1 -0
- package/studio-dist/client/assets/getting-started._topic-DVB8e5R9.js +1 -0
- package/studio-dist/client/assets/getting-started.index-DQvLHEr1.js +1 -0
- package/studio-dist/client/assets/graph-DbNnR884.js +2 -0
- package/studio-dist/client/assets/{graph-canvas-DDuzsNSx.js → graph-canvas-DIcAqwtK.js} +1 -1
- package/studio-dist/client/assets/guides-D6T6W_jV.js +1 -0
- package/studio-dist/client/assets/index-CysBCSxM.js +40 -0
- package/studio-dist/client/assets/markdown-view-jMiaBTSO.js +1 -0
- package/studio-dist/client/assets/{panel-B8F8sOKQ.js → panel-Csr49lBo.js} +1 -1
- package/studio-dist/client/assets/plugins._service._view-CEA_CcPe.js +1 -0
- package/studio-dist/client/assets/prepaint-CKsXyJOR.js +27 -0
- package/studio-dist/client/assets/{queries-axYk2u9L.js → queries-Dfp5deWj.js} +1 -1
- package/studio-dist/client/assets/react-BYUSgtBs.js +1 -0
- package/studio-dist/client/assets/reports-CLdgxxkg.js +1 -0
- package/studio-dist/client/assets/routes-DGilWbDE.js +1 -0
- package/studio-dist/client/assets/search-CXosfvYh.js +1 -0
- package/studio-dist/client/assets/select-DdebbX7K.js +1 -0
- package/studio-dist/client/assets/skills-C3zNkU8g.js +1 -0
- package/studio-dist/client/assets/{specs-DU1kqcp5.js → specs-Dl8HfGlU.js} +1 -1
- package/studio-dist/client/assets/specs-view-B_n0Pekv.js +1 -0
- package/studio-dist/client/assets/{steps-DhIFuAZy.js → steps-CKUno4lh.js} +1 -1
- package/studio-dist/client/assets/store-DyXbi6o9.js +1 -0
- package/studio-dist/client/assets/{studio-BR9j15J0.js → studio-0ueISxwP.js} +8 -8
- package/studio-dist/client/assets/{studio-queries-CFP_yTpr.js → studio-queries-BflKt9va.js} +2 -2
- package/studio-dist/client/assets/{usage-CHmwtkoM.js → usage-Blcl8kA_.js} +16 -16
- package/studio-dist/client/assets/useAnchoredPopupScrollLock-BBXJpX5L.js +1 -0
- package/studio-dist/client/assets/{useBaseQuery-BlC-AZ35.js → useBaseQuery-C2cjmeH2.js} +1 -1
- package/studio-dist/client/assets/useMutation-DI6LbvVa.js +1 -0
- package/studio-dist/client/assets/vault-Cb9DmM1i.js +1 -0
- package/studio-dist/client/assets/warnings-B-rvoW8q.js +1 -0
- package/studio-dist/client/assets/workflow-DN5T19jd.js +1 -0
- package/studio-dist/server/assets/{__23tanstack-start-server-fn-resolver-DAbZ6fzY.js → __23tanstack-start-server-fn-resolver-JoHQ5ihx.js} +30 -14
- package/studio-dist/server/assets/_tanstack-start-manifest_v.js +66 -50
- package/studio-dist/server/assets/{button-BfoNadNR.js → button-B7e1fRyW.js} +228 -3
- package/studio-dist/server/assets/combobox--S_OmUfh.js +483 -0
- package/studio-dist/server/assets/{companion-view-DHUjPpA0.js → companion-view-D-vGM55e.js} +2 -2
- package/studio-dist/server/assets/{companions-kBrJZAd6.js → companions-Dr6IpNAb.js} +1 -1
- package/studio-dist/server/assets/{fields-BD_c65T1.js → fields-B4qM2ZwC.js} +1 -1
- package/studio-dist/server/assets/friction-CWOTjpCV.js +62 -0
- package/studio-dist/server/assets/friction-sLxaWY-H.js +903 -0
- package/studio-dist/server/assets/{getting-started-7P0oyWaM.js → getting-started-Cq5Pcnsr.js} +2 -2
- package/studio-dist/server/assets/{getting-started._topic-B1mV0kGz.js → getting-started._topic-CmWdl3u3.js} +2 -2
- package/studio-dist/server/assets/{getting-started.index-CeGtvUsm.js → getting-started.index-AB8xpTFY.js} +2 -2
- package/studio-dist/server/assets/{graph-BWtmJOH2.js → graph-B11ITte0.js} +1 -1
- package/studio-dist/server/assets/{graph-DSDVEELZ.js → graph-C_Hn-Wf_.js} +7 -8
- package/studio-dist/server/assets/{graph-canvas-CID9Nksc.js → graph-canvas-BthrSmPS.js} +2 -2
- package/studio-dist/server/assets/{guides-Bm3RbUB-.js → guides-B8HdeK_C.js} +1 -1
- package/studio-dist/server/assets/{guides-BLJenHwQ.js → guides-BVQ4pt5B.js} +1 -1
- package/studio-dist/server/assets/{guides.server-rHHqRuzV.js → guides.server-DqFQAStf.js} +1 -1
- package/studio-dist/server/assets/{markdown-view-D3v9x-cM.js → markdown-view-rwiiVcPC.js} +1 -1
- package/studio-dist/server/assets/{panel-yqOl5GMU.js → panel-CUxRIpYL.js} +3 -3
- package/studio-dist/server/assets/{plugins._service._view-DwqJX7s9.js → plugins._service._view-BSvGnlhU.js} +6 -4
- package/studio-dist/server/assets/prepaint-B28ClcPN.js +45 -0
- package/studio-dist/server/assets/{queries-DYmGrhNO.js → queries-S_Pl90IG.js} +1 -1
- package/studio-dist/server/assets/react-CiBZPDs8.js +45 -0
- package/studio-dist/server/assets/{reports-C4Y_Jd-x.js → reports-B9k98oE6.js} +1 -1
- package/studio-dist/server/assets/{reports-7yaPz7aP.js → reports-CJpAItPk.js} +6 -7
- package/studio-dist/server/assets/{router-Dnoq5auE.js → router-DF0etoNa.js} +627 -1563
- package/studio-dist/server/assets/{routes-Cok4p3mK.js → routes-6ylCLkyg.js} +7 -8
- package/studio-dist/server/assets/{search-DUK7Jl6f.js → search-b-yQesv3.js} +25 -2
- package/studio-dist/server/assets/{combobox-oxUI6r4V.js → select-CjuKScan.js} +34 -514
- package/studio-dist/server/assets/{shell-DNVXedgE.js → shell-DLkP7wG9.js} +2 -2
- package/studio-dist/server/assets/{skills-pKmuAs0F.js → skills-DGg0mWDM.js} +1 -1
- package/studio-dist/server/assets/{skills-D8qdXSku.js → skills-zeU80A9S.js} +7 -8
- package/studio-dist/server/assets/{specs-DeHYUNFp.js → specs-DDBNT_v1.js} +4 -4
- package/studio-dist/server/assets/{specs-view-BGn-5Ik_.js → specs-view-exvmWdg0.js} +9 -9
- package/studio-dist/server/assets/{steps-CV7Yk7L7.js → steps-D1tYAyWZ.js} +1 -1
- package/studio-dist/server/assets/store-kS7zk0Hr.js +93 -0
- package/studio-dist/server/assets/{studio-CB94np48.js → studio-qRvQjRpz.js} +47 -2
- package/studio-dist/server/assets/{studio-queries-BqZGBE70.js → studio-queries-qOXOYoPt.js} +66 -8
- package/studio-dist/server/assets/{usage-M6Gi6Km7.js → usage-C2um7YiV.js} +12 -43
- package/studio-dist/server/assets/{usage-j5jR9gX9.js → usage-Cwle9-xY.js} +1 -1
- package/studio-dist/server/assets/useAnchoredPopupScrollLock-kQBB3y_b.js +756 -0
- package/studio-dist/server/assets/{useBaseQuery-Tv_QJt9r.js → useBaseQuery--iH3rd-T.js} +1 -1
- package/studio-dist/server/assets/useMutation-C-WO6hCM.js +690 -0
- package/studio-dist/server/assets/{vault-PqVmssmW.js → vault-B8vgVIH9.js} +6 -6
- package/studio-dist/server/assets/{vault-lBlvSyD4.js → vault-CEBBRLMb.js} +1 -1
- package/studio-dist/server/assets/{views-CsFp1i1G.js → views-UgO0Qz_g.js} +2 -2
- package/studio-dist/server/assets/{warnings-BDs34j_m.js → warnings-BxK4VfV5.js} +1 -2
- package/studio-dist/server/assets/{workflow-T0eEldsa.js → workflow-BrTK632u.js} +8 -9
- package/studio-dist/server/server.js +2 -2
- package/studio-dist/client/assets/app-BFVyWHGo.css +0 -2
- package/studio-dist/client/assets/button-D6LrRkOG.js +0 -1
- package/studio-dist/client/assets/combobox-Dk2MMZWE.js +0 -1
- package/studio-dist/client/assets/fields-D_FYhxQr.js +0 -1
- package/studio-dist/client/assets/getting-started-ByWRg7hC.js +0 -1
- package/studio-dist/client/assets/getting-started-DZSjYC-b.js +0 -1
- package/studio-dist/client/assets/getting-started._topic-C6OyQ4G6.js +0 -1
- package/studio-dist/client/assets/getting-started.index-BkmPHdEm.js +0 -1
- package/studio-dist/client/assets/graph-7jm2XzyL.js +0 -2
- package/studio-dist/client/assets/guides-D9VTjWXP.js +0 -1
- package/studio-dist/client/assets/index-BBmFpG4y.js +0 -40
- package/studio-dist/client/assets/markdown-view-RL5YvfZg.js +0 -1
- package/studio-dist/client/assets/plugins._service._view-CynxTbMx.js +0 -1
- package/studio-dist/client/assets/react-r45OnJCw.js +0 -27
- package/studio-dist/client/assets/reports-BCAyXh6p.js +0 -1
- package/studio-dist/client/assets/routes-BFPqq89E.js +0 -1
- package/studio-dist/client/assets/skills-DTks_uBx.js +0 -1
- package/studio-dist/client/assets/specs-view-omrd_5P3.js +0 -1
- package/studio-dist/client/assets/store-DJ5cncKA.js +0 -1
- package/studio-dist/client/assets/vault-DRg8MScm.js +0 -1
- package/studio-dist/client/assets/warnings-UHFy6HAp.js +0 -1
- package/studio-dist/client/assets/workflow-Bv9vSfVV.js +0 -1
- package/studio-dist/server/assets/react-BO0uMhpm.js +0 -89
- package/studio-dist/server/assets/store-CWDGIp6x.js +0 -280
package/src/lib/package-paths.ts
CHANGED
|
@@ -6,16 +6,23 @@ import { getActiveDistribution } from "../distribution";
|
|
|
6
6
|
|
|
7
7
|
const require = createRequire(import.meta.url);
|
|
8
8
|
|
|
9
|
+
function resolveAsset(relative: string, bundled: string): string {
|
|
10
|
+
for (const root of getActiveDistribution().config.assetRoots ?? []) {
|
|
11
|
+
const candidate = path.join(root, relative);
|
|
12
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
13
|
+
}
|
|
14
|
+
return bundled;
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
/**
|
|
10
18
|
* Resolved wrapper directory: a distribution asset root that ships
|
|
11
19
|
* `wrappers/bin` wins over core's bundled default.
|
|
12
20
|
*/
|
|
13
21
|
export function getWrapperBinPath(): string {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return path.resolve(import.meta.dirname, "../../wrappers/bin");
|
|
22
|
+
return resolveAsset(
|
|
23
|
+
path.join("wrappers", "bin"),
|
|
24
|
+
path.resolve(import.meta.dirname, "../../wrappers/bin"),
|
|
25
|
+
);
|
|
19
26
|
}
|
|
20
27
|
|
|
21
28
|
function canonicalDir(dir: string): string {
|
|
@@ -49,11 +56,7 @@ export function withoutMateWrapperDirs(
|
|
|
49
56
|
* mate-core version.
|
|
50
57
|
*/
|
|
51
58
|
export function getClaudePluginRoot(): string {
|
|
52
|
-
|
|
53
|
-
const candidate = path.join(root, "claude-plugin");
|
|
54
|
-
if (fs.existsSync(candidate)) return candidate;
|
|
55
|
-
}
|
|
56
|
-
return path.resolve(import.meta.dirname, "../../claude-plugin");
|
|
59
|
+
return resolveAsset("claude-plugin", path.resolve(import.meta.dirname, "../../claude-plugin"));
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
/** mate-core's folder of the Claude plugins its built-in Capabilities contribute. */
|
|
@@ -99,11 +102,10 @@ export function validateClaudePluginAssets(pluginRoot = getClaudePluginRoot()):
|
|
|
99
102
|
* projection, never a committed config.
|
|
100
103
|
*/
|
|
101
104
|
export function getOpenCodePluginRoot(): string {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return path.resolve(import.meta.dirname, "../../opencode-plugin");
|
|
105
|
+
return resolveAsset(
|
|
106
|
+
"opencode-plugin",
|
|
107
|
+
path.resolve(import.meta.dirname, "../../opencode-plugin"),
|
|
108
|
+
);
|
|
107
109
|
}
|
|
108
110
|
|
|
109
111
|
/** Root entries OpenCode resolves for a plugin referenced by directory path. */
|
|
@@ -124,6 +126,12 @@ export function validateOpenCodePluginAssets(pluginRoot = getOpenCodePluginRoot(
|
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
|
|
129
|
+
/** Resolved triage playbook: a distribution asset root's `templates/triage/PLAYBOOK.md` wins. */
|
|
130
|
+
export function getTriagePlaybookPath(): string {
|
|
131
|
+
const relative = path.join("templates", "triage", "PLAYBOOK.md");
|
|
132
|
+
return resolveAsset(relative, path.resolve(import.meta.dirname, "..", relative));
|
|
133
|
+
}
|
|
134
|
+
|
|
127
135
|
export function getReactDoctorBinPath(): string {
|
|
128
136
|
try {
|
|
129
137
|
const entryPath = require.resolve("react-doctor");
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
2
|
|
|
3
|
+
import { currentSession } from "../runtime/session-id";
|
|
3
4
|
import {
|
|
4
5
|
patchSession,
|
|
5
6
|
type RegisteredSession,
|
|
@@ -12,15 +13,10 @@ import {
|
|
|
12
13
|
* agent session, or for an unregistered id, nothing changes. Never throws.
|
|
13
14
|
*/
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
const SESSION_ID_VARIABLES = ["CLAUDE_CODE_SESSION_ID", "OPENCODE_SESSION_ID"] as const;
|
|
16
|
+
export { currentSession };
|
|
17
17
|
|
|
18
18
|
export function currentSessionId(env: Record<string, string | undefined>): string | null {
|
|
19
|
-
|
|
20
|
-
const value = env[name]?.trim();
|
|
21
|
-
if (value) return value;
|
|
22
|
-
}
|
|
23
|
-
return null;
|
|
19
|
+
return currentSession(env)?.id ?? null;
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
export interface Attribution {
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { FRAMEWORK_NAME } from "../../framework";
|
|
6
|
+
import type { FrictionEvent } from "../../runtime/friction-log";
|
|
7
|
+
import { scanContent } from "../../runtime/secret-scan";
|
|
8
|
+
import { readFrictionRecords } from "../friction/view/read";
|
|
9
|
+
import { TRIAGE_AGENT_NAMES, TRIAGE_AGENTS, type FoundAgent } from "./launch";
|
|
10
|
+
|
|
11
|
+
const FRICTION_SNAPSHOT_DAYS = 30;
|
|
12
|
+
const FRICTION_EVENTS_FILE = "friction-events.jsonl";
|
|
13
|
+
const FRICTION_REPORTS_FILE = "friction-reports.jsonl";
|
|
14
|
+
const FRICTION_STATES_FILE = "friction-states.json";
|
|
15
|
+
|
|
16
|
+
interface FrictionSnapshot {
|
|
17
|
+
/** The companion the records were scoped to; none means nothing was copied. */
|
|
18
|
+
companion: string | null;
|
|
19
|
+
events: number;
|
|
20
|
+
reports: number;
|
|
21
|
+
/** Copied fingerprints and Free Reports that have a Friction State. */
|
|
22
|
+
states: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const EVIDENCE_FIELDS = ["message", "command", "errorText", "paths", "flags"] as const;
|
|
26
|
+
|
|
27
|
+
async function withoutFlaggedEvidence(
|
|
28
|
+
events: FrictionEvent[],
|
|
29
|
+
scan: typeof scanContent,
|
|
30
|
+
): Promise<FrictionEvent[]> {
|
|
31
|
+
const lines = events.map((event) => JSON.stringify(event));
|
|
32
|
+
let flagged: Map<number, string[]> | null = new Map();
|
|
33
|
+
try {
|
|
34
|
+
for (const finding of await scan(FRICTION_EVENTS_FILE, lines.join("\n"))) {
|
|
35
|
+
const index = finding.line - 1;
|
|
36
|
+
flagged.set(index, [...(flagged.get(index) ?? []), finding.ruleId]);
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
flagged = null;
|
|
40
|
+
}
|
|
41
|
+
return events.map((event, index) => {
|
|
42
|
+
const rules = flagged ? flagged.get(index) : ["scan-failed"];
|
|
43
|
+
if (!rules) return event;
|
|
44
|
+
const detail = { ...event.detail };
|
|
45
|
+
for (const field of EVIDENCE_FIELDS) {
|
|
46
|
+
if (field in detail) detail[field] = `[withheld: ${[...new Set(rules)].join(", ")}]`;
|
|
47
|
+
}
|
|
48
|
+
return { ...event, detail };
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Copies the companion's records of the window into the session; a family without records gets
|
|
54
|
+
* no file. Only that companion's: the copy feeds a public issue draft.
|
|
55
|
+
*/
|
|
56
|
+
export async function writeFrictionSnapshot(
|
|
57
|
+
sessionDir: string,
|
|
58
|
+
options: {
|
|
59
|
+
companion: string | null;
|
|
60
|
+
homeDir?: string;
|
|
61
|
+
now?: Date;
|
|
62
|
+
scan?: typeof scanContent;
|
|
63
|
+
},
|
|
64
|
+
): Promise<FrictionSnapshot> {
|
|
65
|
+
const { companion } = options;
|
|
66
|
+
if (!companion) return { companion: null, events: 0, reports: 0, states: 0 };
|
|
67
|
+
const {
|
|
68
|
+
events: recorded,
|
|
69
|
+
reports,
|
|
70
|
+
states,
|
|
71
|
+
} = readFrictionRecords(
|
|
72
|
+
{ companion },
|
|
73
|
+
{ days: FRICTION_SNAPSHOT_DAYS, homeDir: options.homeDir, now: options.now },
|
|
74
|
+
);
|
|
75
|
+
const events = await withoutFlaggedEvidence(recorded, options.scan ?? scanContent);
|
|
76
|
+
const write = (name: string, records: object[]) => {
|
|
77
|
+
if (records.length === 0) return;
|
|
78
|
+
const lines = records.map((record) => `${JSON.stringify(record)}\n`).join("");
|
|
79
|
+
fs.writeFileSync(path.join(sessionDir, name), lines, "utf8");
|
|
80
|
+
};
|
|
81
|
+
write(FRICTION_EVENTS_FILE, events);
|
|
82
|
+
write(FRICTION_REPORTS_FILE, reports);
|
|
83
|
+
if (events.length === 0 && reports.length === 0) {
|
|
84
|
+
return { companion, events: 0, reports: 0, states: 0 };
|
|
85
|
+
}
|
|
86
|
+
fs.writeFileSync(
|
|
87
|
+
path.join(sessionDir, FRICTION_STATES_FILE),
|
|
88
|
+
`${JSON.stringify({ v: 1, ...states }, null, 2)}\n`,
|
|
89
|
+
"utf8",
|
|
90
|
+
);
|
|
91
|
+
return {
|
|
92
|
+
companion,
|
|
93
|
+
events: events.length,
|
|
94
|
+
reports: reports.length,
|
|
95
|
+
states: Object.keys(states.fingerprints).length + Object.keys(states.reports).length,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface TriageContextInput {
|
|
100
|
+
now: Date;
|
|
101
|
+
version: string;
|
|
102
|
+
argv: string[];
|
|
103
|
+
cwd: string;
|
|
104
|
+
sessionDir: string;
|
|
105
|
+
issueRepository: string | undefined;
|
|
106
|
+
agents: FoundAgent[];
|
|
107
|
+
friction: FrictionSnapshot;
|
|
108
|
+
doctor: { report: unknown } | { error: string };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function runtimeVersion(): string {
|
|
112
|
+
return process.versions.bun ? `Bun ${process.versions.bun}` : `Node.js ${process.versions.node}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function agentLines(agents: FoundAgent[]): string[] {
|
|
116
|
+
return TRIAGE_AGENTS.map((id) => {
|
|
117
|
+
const found = agents.find((agent) => agent.agent === id);
|
|
118
|
+
if (!found) return `- ${TRIAGE_AGENT_NAMES[id]} (\`${id}\`): not found`;
|
|
119
|
+
return `- ${TRIAGE_AGENT_NAMES[id]} (\`${id}\`): ${found.version ?? "version unknown"}, at ${found.binary}`;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function count(n: number, noun: string): string {
|
|
124
|
+
return `${n} ${noun}${n === 1 ? "" : "s"}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function frictionLines(friction: FrictionSnapshot): string[] {
|
|
128
|
+
if (!friction.companion) {
|
|
129
|
+
return [
|
|
130
|
+
`No friction was copied: this folder belongs to no companion. To include it, run \`${FRAMEWORK_NAME} triage\` in the affected repository.`,
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
if (friction.events === 0 && friction.reports === 0) {
|
|
134
|
+
return [
|
|
135
|
+
`No friction was recorded for ${friction.companion} in the last ${FRICTION_SNAPSHOT_DAYS} days.`,
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
const lines = [
|
|
139
|
+
`Records for ${friction.companion} from the last ${FRICTION_SNAPSHOT_DAYS} days, one JSON object per line:`,
|
|
140
|
+
];
|
|
141
|
+
if (friction.events > 0) {
|
|
142
|
+
lines.push(`- \`${FRICTION_EVENTS_FILE}\`: ${count(friction.events, "Friction Event")}`);
|
|
143
|
+
}
|
|
144
|
+
if (friction.reports > 0) {
|
|
145
|
+
lines.push(`- \`${FRICTION_REPORTS_FILE}\`: ${count(friction.reports, "agent report")}`);
|
|
146
|
+
}
|
|
147
|
+
lines.push(
|
|
148
|
+
`- \`${FRICTION_STATES_FILE}\`: ${count(friction.states, "Friction State")}, by fingerprint and Free Report id; an item without one is open`,
|
|
149
|
+
);
|
|
150
|
+
return lines;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Holds no environment value: the doctor report and the facts below carry none. */
|
|
154
|
+
export function renderTriageContext(input: TriageContextInput): string {
|
|
155
|
+
const doctor =
|
|
156
|
+
"report" in input.doctor
|
|
157
|
+
? ["```json", JSON.stringify(input.doctor.report, null, 2), "```"]
|
|
158
|
+
: [`The doctor report could not be built: ${input.doctor.error}`];
|
|
159
|
+
return [
|
|
160
|
+
`# ${FRAMEWORK_NAME} triage context`,
|
|
161
|
+
"",
|
|
162
|
+
`- Written: ${input.now.toISOString()}`,
|
|
163
|
+
`- ${FRAMEWORK_NAME} version: ${input.version}`,
|
|
164
|
+
`- Invoked as: \`${[FRAMEWORK_NAME, "triage", ...input.argv].join(" ")}\``,
|
|
165
|
+
`- Executable: ${process.execPath}`,
|
|
166
|
+
`- Entry point: ${process.argv[1] ?? "unknown"}`,
|
|
167
|
+
`- Operating system: ${os.type()} ${os.release()} (${process.platform}, ${process.arch})`,
|
|
168
|
+
`- JavaScript runtime: ${runtimeVersion()}`,
|
|
169
|
+
`- Folder the command ran in: ${input.cwd}`,
|
|
170
|
+
`- Triage session: ${input.sessionDir}`,
|
|
171
|
+
`- Issue repository: ${input.issueRepository ?? "none set"}`,
|
|
172
|
+
"",
|
|
173
|
+
"## Agent Runtimes",
|
|
174
|
+
"",
|
|
175
|
+
...agentLines(input.agents),
|
|
176
|
+
"",
|
|
177
|
+
"## Friction",
|
|
178
|
+
"",
|
|
179
|
+
...frictionLines(input.friction),
|
|
180
|
+
"",
|
|
181
|
+
`## Doctor report for ${input.cwd}`,
|
|
182
|
+
"",
|
|
183
|
+
...doctor,
|
|
184
|
+
"",
|
|
185
|
+
].join("\n");
|
|
186
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
import { resolveCommandOnPath } from "../../tools/setup/utils";
|
|
4
|
+
import { withoutMateWrapperDirs } from "../package-paths";
|
|
5
|
+
|
|
6
|
+
export const TRIAGE_AGENTS = ["claude", "opencode"] as const;
|
|
7
|
+
export type TriageAgent = (typeof TRIAGE_AGENTS)[number];
|
|
8
|
+
|
|
9
|
+
export const TRIAGE_AGENT_NAMES: Record<TriageAgent, string> = {
|
|
10
|
+
claude: "Claude Code",
|
|
11
|
+
opencode: "OpenCode",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export interface FoundAgent {
|
|
15
|
+
agent: TriageAgent;
|
|
16
|
+
binary: string;
|
|
17
|
+
/** First line of `--version`, or null when it fails. */
|
|
18
|
+
version: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function probeVersion(binary: string): string | null {
|
|
22
|
+
const result = spawnSync(binary, ["--version"], { encoding: "utf8", timeout: 5_000 });
|
|
23
|
+
if (result.status !== 0) return null;
|
|
24
|
+
return result.stdout.trim().split("\n")[0]?.trim() || null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function findAgents(pathValue: string): FoundAgent[] {
|
|
28
|
+
const searchPath = withoutMateWrapperDirs(pathValue);
|
|
29
|
+
return TRIAGE_AGENTS.flatMap((agent) => {
|
|
30
|
+
const binary = resolveCommandOnPath(agent, searchPath);
|
|
31
|
+
return binary ? [{ agent, binary, version: probeVersion(binary) }] : [];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Verified against Claude Code 2.1 (`claude [prompt]`) and OpenCode 2.2 (`opencode --prompt`). */
|
|
36
|
+
function agentArgs(agent: TriageAgent, prompt: string): string[] {
|
|
37
|
+
return agent === "claude" ? [prompt] : ["--prompt", prompt];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Starts the agent's own CLI, never a Mate launch: triage must work when that launch is broken. */
|
|
41
|
+
export function launchAgent(
|
|
42
|
+
options: Pick<FoundAgent, "agent" | "binary"> & { sessionDir: string; prompt: string },
|
|
43
|
+
): Promise<number> {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const child = spawn(options.binary, agentArgs(options.agent, options.prompt), {
|
|
46
|
+
cwd: options.sessionDir,
|
|
47
|
+
stdio: "inherit",
|
|
48
|
+
});
|
|
49
|
+
child.on("error", reject);
|
|
50
|
+
child.on("close", (code, signal) => resolve(code ?? (signal ? 1 : 0)));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { getActiveDistribution } from "../../distribution";
|
|
5
|
+
import { FRAMEWORK_NAME } from "../../framework";
|
|
6
|
+
import { errorMessage } from "../../runtime/error-message";
|
|
7
|
+
import { getTriagePlaybookPath } from "../package-paths";
|
|
8
|
+
import { maskSecretValues } from "../../runtime/secret-values";
|
|
9
|
+
import { renderTriageContext, writeFrictionSnapshot } from "./context";
|
|
10
|
+
import { findAgents, type FoundAgent } from "./launch";
|
|
11
|
+
import { createTriageSession } from "./session";
|
|
12
|
+
|
|
13
|
+
export interface TriageSessionFiles {
|
|
14
|
+
dir: string;
|
|
15
|
+
context: string;
|
|
16
|
+
playbook: string;
|
|
17
|
+
prompt: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface PrepareTriageOptions {
|
|
21
|
+
argv: string[];
|
|
22
|
+
cwd: string;
|
|
23
|
+
homeDir: string | undefined;
|
|
24
|
+
now: Date;
|
|
25
|
+
pathValue: string;
|
|
26
|
+
env: Record<string, string | undefined>;
|
|
27
|
+
/** Builds the doctor report for `cwd`; a throw is recorded in place of the report. */
|
|
28
|
+
collectDoctor: (cwd: string) => Promise<unknown>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function seedPrompt(): string {
|
|
32
|
+
return [
|
|
33
|
+
`You are triaging a problem with ${FRAMEWORK_NAME} on this machine.`,
|
|
34
|
+
"Read PLAYBOOK.md in this folder and follow it step by step.",
|
|
35
|
+
"The machine facts and the recorded friction are described in context.md.",
|
|
36
|
+
].join(" ");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function doctorSection(
|
|
40
|
+
cwd: string,
|
|
41
|
+
collect: (cwd: string) => Promise<unknown>,
|
|
42
|
+
): Promise<{ report: unknown } | { error: string }> {
|
|
43
|
+
try {
|
|
44
|
+
return { report: await collect(cwd) };
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return { error: errorMessage(error) };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function companionOf(doctor: { report: unknown } | { error: string }): string | null {
|
|
51
|
+
if (!("report" in doctor) || !doctor.report || typeof doctor.report !== "object") return null;
|
|
52
|
+
const { companionPath } = doctor.report as { companionPath?: unknown };
|
|
53
|
+
return typeof companionPath === "string" && companionPath ? companionPath : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Writes a fresh triage session; nothing is written outside its folder except pruning old ones. */
|
|
57
|
+
export async function prepareTriageSession(
|
|
58
|
+
options: PrepareTriageOptions,
|
|
59
|
+
): Promise<{ files: TriageSessionFiles; agents: FoundAgent[] }> {
|
|
60
|
+
const { now, cwd } = options;
|
|
61
|
+
const dir = createTriageSession({ homeDir: options.homeDir, now });
|
|
62
|
+
const files: TriageSessionFiles = {
|
|
63
|
+
dir,
|
|
64
|
+
context: path.join(dir, "context.md"),
|
|
65
|
+
playbook: path.join(dir, "PLAYBOOK.md"),
|
|
66
|
+
prompt: path.join(dir, "prompt.md"),
|
|
67
|
+
};
|
|
68
|
+
const agents = findAgents(options.pathValue);
|
|
69
|
+
const doctor = await doctorSection(cwd, options.collectDoctor);
|
|
70
|
+
const friction = await writeFrictionSnapshot(dir, {
|
|
71
|
+
companion: companionOf(doctor),
|
|
72
|
+
homeDir: options.homeDir,
|
|
73
|
+
now,
|
|
74
|
+
});
|
|
75
|
+
const { config } = getActiveDistribution();
|
|
76
|
+
fs.writeFileSync(
|
|
77
|
+
files.context,
|
|
78
|
+
maskSecretValues(
|
|
79
|
+
renderTriageContext({
|
|
80
|
+
now,
|
|
81
|
+
version: config.version,
|
|
82
|
+
argv: options.argv,
|
|
83
|
+
cwd,
|
|
84
|
+
sessionDir: dir,
|
|
85
|
+
issueRepository: config.issueRepository,
|
|
86
|
+
agents,
|
|
87
|
+
friction,
|
|
88
|
+
doctor,
|
|
89
|
+
}),
|
|
90
|
+
options.env,
|
|
91
|
+
),
|
|
92
|
+
"utf8",
|
|
93
|
+
);
|
|
94
|
+
fs.copyFileSync(getTriagePlaybookPath(), files.playbook);
|
|
95
|
+
fs.writeFileSync(files.prompt, `${seedPrompt()}\n`, "utf8");
|
|
96
|
+
return { files, agents };
|
|
97
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { realPath } from "../../runtime/git-layout";
|
|
6
|
+
import { processAlive } from "../../runtime/opencode-server-record";
|
|
7
|
+
import { isWithin } from "../../runtime/physical-path";
|
|
8
|
+
import { mateHome } from "../../runtime/runtime-directory";
|
|
9
|
+
|
|
10
|
+
const TRIAGE_RETENTION_DAYS = 30;
|
|
11
|
+
const LAUNCHER_FILE = ".mate-triage.pid";
|
|
12
|
+
const MAX_ANCESTORS = 64;
|
|
13
|
+
|
|
14
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
15
|
+
const SESSION_NAME = /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z(?:-\d+)?$/;
|
|
16
|
+
|
|
17
|
+
export function triageRoot(homeDir?: string): string {
|
|
18
|
+
return path.join(mateHome(homeDir), "triage");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The triage session folder `cwd` lies in, or null; a stray folder under the triage root is none. */
|
|
22
|
+
export function triageSessionDir(cwd: string, homeDir?: string): string | null {
|
|
23
|
+
const root = realPath(triageRoot(homeDir));
|
|
24
|
+
const real = realPath(cwd);
|
|
25
|
+
if (!isWithin(real, root)) return null;
|
|
26
|
+
const [name] = path.relative(root, real).split(path.sep);
|
|
27
|
+
return name && SESSION_NAME.test(name) ? path.join(root, name) : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toPid(text: string | undefined): number | null {
|
|
31
|
+
const pid = Number(text?.trim());
|
|
32
|
+
return Number.isSafeInteger(pid) && pid > 1 ? pid : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Linux and macOS; null when unknown or at the top of the tree. */
|
|
36
|
+
export function parentPid(pid: number): number | null {
|
|
37
|
+
try {
|
|
38
|
+
if (process.platform === "linux") {
|
|
39
|
+
// The command name in parentheses may hold spaces; state and ppid follow its last ")".
|
|
40
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
41
|
+
return toPid(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
42
|
+
}
|
|
43
|
+
// An absolute path: an agent could put its own `ps` first on PATH.
|
|
44
|
+
const ps = spawnSync("/bin/ps", ["-o", "ppid=", "-p", String(pid)], {
|
|
45
|
+
encoding: "utf8",
|
|
46
|
+
timeout: 5_000,
|
|
47
|
+
});
|
|
48
|
+
return ps.status === 0 ? toPid(ps.stdout) : null;
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Whether `pid` is an ancestor of this process; on Windows, without a cheap parent lookup, whether it runs. */
|
|
55
|
+
export function isAncestorProcess(pid: number): boolean {
|
|
56
|
+
if (process.platform === "win32") return processAlive(pid);
|
|
57
|
+
let current: number | null = process.ppid;
|
|
58
|
+
for (let hop = 0; current !== null && hop < MAX_ANCESTORS; hop += 1) {
|
|
59
|
+
if (current === pid) return true;
|
|
60
|
+
current = parentPid(current);
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Records this process as the one running the session's agent; the returned function removes the record. */
|
|
66
|
+
export function recordTriageLauncher(sessionDir: string): () => void {
|
|
67
|
+
const file = path.join(sessionDir, LAUNCHER_FILE);
|
|
68
|
+
fs.writeFileSync(file, `${process.pid}\n`, "utf8");
|
|
69
|
+
return () => fs.rmSync(file, { force: true });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The triage session folder `cwd` lies in, when the `mate triage` recorded there is an ancestor of
|
|
74
|
+
* this process (see `isAncestorProcess`); else null, as for an old session or a folder an agent made.
|
|
75
|
+
*/
|
|
76
|
+
export function launchedTriageSessionDir(
|
|
77
|
+
cwd: string,
|
|
78
|
+
options: { homeDir?: string; isAncestor?: (pid: number) => boolean } = {},
|
|
79
|
+
): string | null {
|
|
80
|
+
const dir = triageSessionDir(cwd, options.homeDir);
|
|
81
|
+
if (!dir) return null;
|
|
82
|
+
let launcher: number | null;
|
|
83
|
+
try {
|
|
84
|
+
launcher = toPid(fs.readFileSync(path.join(dir, LAUNCHER_FILE), "utf8"));
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return launcher !== null && (options.isAncestor ?? isAncestorProcess)(launcher) ? dir : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function sessionName(date: Date): string {
|
|
92
|
+
return date.toISOString().replace(/[:.]/g, "-");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function pruneTriageSessions(root: string, now: Date): void {
|
|
96
|
+
let entries: fs.Dirent[];
|
|
97
|
+
try {
|
|
98
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
99
|
+
} catch {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const oldest = sessionName(new Date(now.getTime() - TRIAGE_RETENTION_DAYS * DAY_MS));
|
|
103
|
+
for (const entry of entries) {
|
|
104
|
+
if (entry.isDirectory() && SESSION_NAME.test(entry.name) && entry.name < oldest) {
|
|
105
|
+
fs.rmSync(path.join(root, entry.name), { recursive: true, force: true });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Prunes old sessions, then creates a new, empty session folder and returns its path. */
|
|
111
|
+
export function createTriageSession(options: { homeDir?: string; now?: Date } = {}): string {
|
|
112
|
+
const now = options.now ?? new Date();
|
|
113
|
+
const root = triageRoot(options.homeDir);
|
|
114
|
+
pruneTriageSessions(root, now);
|
|
115
|
+
fs.mkdirSync(root, { recursive: true });
|
|
116
|
+
const base = sessionName(now);
|
|
117
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
118
|
+
const dir = path.join(root, attempt === 0 ? base : `${base}-${attempt}`);
|
|
119
|
+
try {
|
|
120
|
+
fs.mkdirSync(dir);
|
|
121
|
+
return dir;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -11,6 +11,8 @@ import {
|
|
|
11
11
|
} from "../runtime/companion-sync";
|
|
12
12
|
import { hasLaunchEnvironment } from "../runtime/env";
|
|
13
13
|
import { MATE_ENV } from "../runtime/env-names";
|
|
14
|
+
import { recordRefusal } from "../runtime/friction-recorder";
|
|
15
|
+
import type { FrictionCheck } from "../runtime/friction-fingerprint";
|
|
14
16
|
import { resolveProjection } from "../runtime/projection";
|
|
15
17
|
import { checkSecretAccess, type SecretToolCall } from "../runtime/secret-guard";
|
|
16
18
|
import { isSecretPolicyFile } from "../runtime/secret-policy-weakening";
|
|
@@ -96,7 +98,17 @@ export async function refuseForkedCompanionWrite(
|
|
|
96
98
|
if (!filePath || !isArtifactPath(filePath)) return;
|
|
97
99
|
if (!isCompanionTarget(context, filePath)) return;
|
|
98
100
|
const refusal = await companionForkRefusal(env, context.companionPath);
|
|
99
|
-
if (refusal) throw new
|
|
101
|
+
if (refusal) throw new GuardRefusal("fork", refusal);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A guard refusal; its message is what the agent sees, `check` names the check that refused. */
|
|
105
|
+
export class GuardRefusal extends Error {
|
|
106
|
+
readonly check: FrictionCheck;
|
|
107
|
+
|
|
108
|
+
constructor(check: FrictionCheck, message: string) {
|
|
109
|
+
super(message);
|
|
110
|
+
this.check = check;
|
|
111
|
+
}
|
|
100
112
|
}
|
|
101
113
|
|
|
102
114
|
const textOf = (value: unknown) => (typeof value === "string" ? value : "");
|
|
@@ -195,11 +207,11 @@ export async function guardToolInput(
|
|
|
195
207
|
companionPath: context.companionPath,
|
|
196
208
|
isCompanionPath: (filePath) => filePath.startsWith(companionRoot),
|
|
197
209
|
});
|
|
198
|
-
if (refusal) throw new
|
|
210
|
+
if (refusal) throw new GuardRefusal("secret", refusal);
|
|
199
211
|
}
|
|
200
212
|
if (tool === "bash") {
|
|
201
213
|
const refusal = checkCommitAttribution(String(args.command ?? ""));
|
|
202
|
-
if (refusal) throw new
|
|
214
|
+
if (refusal) throw new GuardRefusal("attribution", refusal);
|
|
203
215
|
}
|
|
204
216
|
const filePaths =
|
|
205
217
|
tool === "write" || tool === "edit"
|
|
@@ -210,7 +222,7 @@ export async function guardToolInput(
|
|
|
210
222
|
|
|
211
223
|
for (const filePath of filePaths) {
|
|
212
224
|
if (context.repositoryPath && filePath && shouldBlockArtifactWrite(context, filePath)) {
|
|
213
|
-
throw new
|
|
225
|
+
throw new GuardRefusal("artifact", buildArtifactError(context, filePath));
|
|
214
226
|
}
|
|
215
227
|
// oxlint-disable-next-line no-await-in-loop -- the first refusal wins
|
|
216
228
|
await refuseForkedCompanionWrite(context, filePath, env);
|
|
@@ -234,7 +246,43 @@ export async function registerCompanionHooks(
|
|
|
234
246
|
await repairCompanionGitOnce(context, env, api.location.directory).catch(() => []);
|
|
235
247
|
|
|
236
248
|
await api.tool.hook("execute.before", async (event) => {
|
|
237
|
-
|
|
249
|
+
try {
|
|
250
|
+
await guardToolInput(context, event.tool, event.input, env);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (error instanceof GuardRefusal) {
|
|
253
|
+
const nudge = recordToolRefusal(error, event, context, env);
|
|
254
|
+
if (nudge) error.message = `${error.message}\n${nudge}`;
|
|
255
|
+
}
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function recordToolRefusal(
|
|
262
|
+
refusal: GuardRefusal,
|
|
263
|
+
event: { tool: string; sessionID: string; input: unknown },
|
|
264
|
+
context: CompanionContext,
|
|
265
|
+
env: Record<string, string | undefined>,
|
|
266
|
+
): string | null {
|
|
267
|
+
const tool = V1_TOOL_NAMES[event.tool] ?? event.tool;
|
|
268
|
+
const args = (event.input && typeof event.input === "object" ? event.input : {}) as Record<
|
|
269
|
+
string,
|
|
270
|
+
unknown
|
|
271
|
+
>;
|
|
272
|
+
const paths =
|
|
273
|
+
tool === "apply_patch"
|
|
274
|
+
? extractPatchPaths(textOf(args.patchText))
|
|
275
|
+
: [textOf(args.filePath) || textOf(args.path)];
|
|
276
|
+
return recordRefusal({
|
|
277
|
+
check: refusal.check,
|
|
278
|
+
toolKind: secretToolCall(context, tool, args)?.kind ?? "other",
|
|
279
|
+
message: refusal.message,
|
|
280
|
+
paths,
|
|
281
|
+
companionPath: context.companionPath,
|
|
282
|
+
repositoryPath: context.repositoryPath || null,
|
|
283
|
+
runtime: "opencode",
|
|
284
|
+
sessionId: event.sessionID,
|
|
285
|
+
env,
|
|
238
286
|
});
|
|
239
287
|
}
|
|
240
288
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { buildOpenCodeGuidance, declaredGuidance } from "../runtime/companion-guidance";
|
|
2
2
|
import { MATE_ENV } from "../runtime/env-names";
|
|
3
|
+
import { readFrictionSwitch } from "../runtime/friction-switch";
|
|
3
4
|
import { parseGuidanceContent, type MateGuidanceFile } from "../runtime/guidance";
|
|
4
5
|
import { readCompanionPolicy } from "../runtime/policy";
|
|
5
6
|
import { resolveProjection } from "../runtime/projection";
|
|
@@ -49,6 +50,7 @@ export function resolveOpenCodeGuidance(
|
|
|
49
50
|
policy.enabledCapabilities.map((name) => ({ name })),
|
|
50
51
|
{
|
|
51
52
|
secretGuardrails: policy.secrets.guardrails,
|
|
53
|
+
friction: readFrictionSwitch(env.HOME),
|
|
52
54
|
...declaredGuidance(projection.companionPath),
|
|
53
55
|
},
|
|
54
56
|
);
|
|
@@ -26,11 +26,11 @@ export { GRAPHIFY_SHARED_COMPANION_PATH_CONTRACT } from "../runtime/companion-gu
|
|
|
26
26
|
*/
|
|
27
27
|
export function buildCompanionGuidance(
|
|
28
28
|
context: GuidanceContext,
|
|
29
|
-
options: { wrapperBinPath?: string } = {},
|
|
29
|
+
options: { wrapperBinPath?: string; friction?: boolean } = {},
|
|
30
30
|
): string {
|
|
31
31
|
return buildCompanionGuidanceWith(
|
|
32
32
|
// The companion's declared artifact types and stages, read live like its other predicates.
|
|
33
33
|
{ ...declaredGuidance(context.companionPath), ...context },
|
|
34
|
-
{ wrapperBinPath: options.wrapperBinPath ?? getWrapperBinPath() },
|
|
34
|
+
{ wrapperBinPath: options.wrapperBinPath ?? getWrapperBinPath(), friction: options.friction },
|
|
35
35
|
);
|
|
36
36
|
}
|