@modootoday/pmcp 0.19.9 → 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/.agents/plugins/marketplace.json +1 -1
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.mcp.json +1 -1
- package/dist/{apply-TM6B2Y3G.js → apply-6FLMTJDV.js} +57 -23
- package/dist/{attachment-D84OGI6n.d.ts → attachment-ms6s8Aow.d.ts} +1 -1
- package/dist/{available-HYSDAFYH.js → available-BFXLII4A.js} +7 -7
- package/dist/{broadcast-DSCHWUMB.js → broadcast-ZALGUJVR.js} +4 -4
- package/dist/chunk-2GI5RM3M.js +53 -0
- package/dist/{chunk-ICF2JZSN.js → chunk-2MAD4WNZ.js} +2 -2
- package/dist/{chunk-QYQGPDBE.js → chunk-33KGCJB2.js} +2 -2
- package/dist/{chunk-XNPOHPMJ.js → chunk-3X5JQYKN.js} +3 -3
- package/dist/{chunk-4XNAKVSO.js → chunk-57Q75MS4.js} +18 -18
- package/dist/{chunk-2S44RDO4.js → chunk-5FW7A6TG.js} +7 -4
- package/dist/chunk-5YCKFU5Q.js +1672 -0
- package/dist/{chunk-MCYGYZVZ.js → chunk-7FV2LORT.js} +2 -2
- package/dist/{chunk-57FHYW74.js → chunk-DEY23WTH.js} +122 -4
- package/dist/chunk-DJZPPCJP.js +274 -0
- package/dist/{chunk-4XWVLC23.js → chunk-G6JXCUSK.js} +2 -2
- package/dist/{chunk-T6GOWHK6.js → chunk-GFOQDLPL.js} +6 -1
- package/dist/{chunk-PG42KALG.js → chunk-GQP4GWU4.js} +5 -5
- package/dist/chunk-H4ZOKIER.js +7 -0
- package/dist/{chunk-XPWIJOZT.js → chunk-JQVSDPFL.js} +3 -3
- package/dist/{chunk-OG7QR2DP.js → chunk-KEHB4FZP.js} +17 -2
- package/dist/chunk-KUBHGVTD.js +126 -0
- package/dist/{chunk-SPUCY4AG.js → chunk-KVNNVLJR.js} +2 -2
- package/dist/chunk-LFFIQBNY.js +115 -0
- package/dist/{chunk-V7HG3STC.js → chunk-MR5IBS6L.js} +31 -15
- package/dist/{chunk-W5RLIUNC.js → chunk-NHUMPEPV.js} +2 -2
- package/dist/{chunk-I4DLMIN5.js → chunk-OFJIMBTL.js} +2 -2
- package/dist/{chunk-A6TM4NTI.js → chunk-OFPNCLG7.js} +2 -2
- package/dist/{chunk-3UOFHLY4.js → chunk-OS5DF3LL.js} +207 -15
- package/dist/{chunk-BZ2RXI5I.js → chunk-POTI2RJ6.js} +2 -2
- package/dist/{chunk-QTKD7JEL.js → chunk-R3MVL67T.js} +34 -34
- package/dist/{chunk-YN5GQCHN.js → chunk-RHJEGLYM.js} +6 -1
- package/dist/{chunk-YKU7KHJO.js → chunk-TBKVC2TA.js} +3 -5
- package/dist/{chunk-YYG3GUQW.js → chunk-UFWQTZDX.js} +29 -17
- package/dist/{chunk-PBDPWE5J.js → chunk-VBDDFO2V.js} +2 -2
- package/dist/{chunk-BRSESCGX.js → chunk-VUM6LO77.js} +4717 -4694
- package/dist/{chunk-FR6JHNLC.js → chunk-YLITJLTR.js} +3 -3
- package/dist/{chunk-CJ6OFTS3.js → chunk-YOWIYFIV.js} +6 -37
- package/dist/{chunk-VDBUZKXC.js → chunk-Z4Q6RYJH.js} +2 -2
- package/dist/{chunk-RRKGSYQ4.js → chunk-Z6WRFOFT.js} +13 -2
- package/dist/{chunk-YXAPCHXJ.js → chunk-ZIJ5IUXH.js} +31 -5
- package/dist/{claude-2C5L2HQL.js → claude-XVLF4N4S.js} +3 -3
- package/dist/{cli-BVRPAI23.js → cli-6FWRBCWM.js} +8 -8
- package/dist/{cli-YV6SFE4D.js → cli-RVPR3MH3.js} +7 -7
- package/dist/cli.js +11 -11
- package/dist/{completion-B3UT3GVI.js → completion-YTPHEVEW.js} +7 -7
- package/dist/{diagnostics-ONIAKCA3.js → diagnostics-IW54MLFK.js} +5 -6
- package/dist/{doctor-MJFU5L6R.js → doctor-EN4JPSKV.js} +35 -35
- package/dist/{entry-DZGKCL4G.js → entry-TQWSDTJ4.js} +14 -15
- package/dist/{exec-mcp-QK2VC6RX.js → exec-mcp-KKQ75N5O.js} +15 -13
- package/dist/{harness-GTED4GLK.js → harness-EB76F6BG.js} +6 -6
- package/dist/harness-launch-gate.js +3 -4
- package/dist/harness-worker.js +5 -6
- package/dist/hook-body.js +281 -98
- package/dist/{hook-spec-C7umArqw.d.ts → hook-spec-hZL4MSgi.d.ts} +8 -3
- package/dist/hooks-YWSHUNLM.js +24 -0
- package/dist/{host-AGNF2AQH.js → host-LSVITS6M.js} +6 -7
- package/dist/{host-3HTCZNVJ.js → host-OGGADMBT.js} +17 -15
- package/dist/{host-run-2OQZVQID.js → host-run-XO2TS4JC.js} +5 -7
- package/dist/http.d.ts +1 -1
- package/dist/http.js +1 -1
- package/dist/{import-mcp-7KEPR2B6.js → import-mcp-BJDJTPVS.js} +14 -11
- package/dist/index.d.ts +12 -3
- package/dist/index.js +25 -25
- package/dist/{init-BHGCF7JL.js → init-773PTQM5.js} +3 -3
- package/dist/{install-HF6CROQP.js → install-7V6NIA7W.js} +4 -4
- package/dist/{invoke-CVHF6PEK.js → invoke-Y7XH5S2L.js} +7 -8
- package/dist/{list-6XGQ3FR3.js → list-5W65XVES.js} +20 -17
- package/dist/{logout-6RJLG2KG.js → logout-XO6ALD45.js} +15 -12
- package/dist/{logs-VTW7S2LR.js → logs-74JGLQG6.js} +4 -4
- package/dist/mailbox/index.d.ts +3 -3
- package/dist/{mailbox-CNAONXN7.js → mailbox-VK5AVTHI.js} +9 -9
- package/dist/{mainline-JSG4MUKH.js → mainline-M2NSTAGC.js} +105 -36
- package/dist/{mcp-P55VLQLR.js → mcp-R62ZNVGJ.js} +2 -2
- package/dist/{pause-V5VCNOSH.js → pause-CZ2OZYLC.js} +4 -4
- package/dist/project/hook-adapter.d.ts +1 -1
- package/dist/{project-B57IQPDK.js → project-W5DQIZA6.js} +5 -5
- package/dist/{pty-FD4JGBZL.js → pty-CDLQ4NP2.js} +3 -3
- package/dist/report-Z3JV2PCT.js +105 -0
- package/dist/{resume-7S4K3R4X.js → resume-DFIHT2RO.js} +4 -4
- package/dist/{run-MC2BPLOE.js → run-OZLGSRHF.js} +14 -11
- package/dist/{run-LFJXONLW.js → run-WYJMTLX7.js} +4 -4
- package/dist/{serve-TFAEVXX3.js → serve-DCQGAGB7.js} +3 -3
- package/dist/{session-XM2CR425.js → session-YZVX4NGN.js} +4 -4
- package/dist/{setup-FHFQZJY6.js → setup-D3KEJBGO.js} +4 -4
- package/dist/smoke-FQQJOQBD.js +16 -0
- package/dist/{start-login-KXOHCR5A.js → start-login-EKXM75XQ.js} +15 -12
- package/dist/{status-YXOIEVOJ.js → status-23UDIYYP.js} +39 -22
- package/dist/{status-OYRB3KL5.js → status-3RTGDN2K.js} +10 -10
- package/dist/{status-AW7DOW7A.js → status-EDSEG32U.js} +15 -12
- package/dist/stdio.d.ts +2 -2
- package/dist/stdio.js +2 -2
- package/dist/{sweep-ZSYG6FNR.js → sweep-EPJMEZR6.js} +4 -4
- package/dist/{test-2HTOY4PJ.js → test-TM6PIZTV.js} +26 -18
- package/dist/{tui-DQFXYQ4H.js → tui-EQNJIDN5.js} +4 -4
- package/dist/{unbroadcast-FONZJWEW.js → unbroadcast-NTOC3PY3.js} +4 -4
- package/dist/{validate-WOFI7TMT.js → validate-KFBCUFI4.js} +2 -2
- package/dist/{worker-UXPGXAV2.js → worker-7X6WBPEI.js} +9 -10
- package/dist/{worktree-list-4PLE7K6L.js → worktree-list-EMKUMCTQ.js} +2 -2
- package/dist/{worktree-sync-GQVYAXGJ.js → worktree-sync-T6C5YEYS.js} +3 -3
- package/docs/commands.md +13 -6
- package/docs/git-workflow.md +46 -10
- package/docs/hooks.md +139 -12
- package/docs/project-configuration.md +34 -6
- package/docs/upgrading.md +94 -0
- package/gemini-extension.json +2 -2
- package/mcp.json +1 -1
- package/package.json +5 -5
- package/plugin.json +1 -1
- package/plugins/pmcp/.claude-plugin/plugin.json +1 -1
- package/plugins/pmcp/.mcp.json +1 -1
- package/plugins/pmcp/gemini-extension.json +2 -2
- package/plugins/pmcp/mcp.json +1 -1
- package/plugins/pmcp/mod/reader/read.ts +14 -4
- package/plugins/pmcp/mod/sessions/format.ts +16 -1
- package/plugins/pmcp/mod/sessions/types.ts +1 -1
- package/plugins/pmcp/plugin.json +1 -1
- package/schema/catalog-v1.json +1 -7
- package/dist/chunk-EONAJAMJ.js +0 -130
- package/dist/chunk-IACJDHFQ.js +0 -1025
- package/dist/chunk-ISNRDHSD.js +0 -74
- package/dist/chunk-YGTJQVCK.js +0 -7
- package/dist/chunk-YLPXTJJM.js +0 -396
- package/dist/hooks-FRZ4SUNJ.js +0 -21
- package/dist/{core-C1LPjxK6.d.ts → core-Sug_rZrx.d.ts} +1 -1
|
@@ -0,0 +1,1672 @@
|
|
|
1
|
+
// src/hooks/contract.ts
|
|
2
|
+
var RUNTIME_IDS = [
|
|
3
|
+
"claude",
|
|
4
|
+
"codex",
|
|
5
|
+
"gemini",
|
|
6
|
+
"antigravity",
|
|
7
|
+
"grok",
|
|
8
|
+
"opencode"
|
|
9
|
+
];
|
|
10
|
+
var DEFAULT_RUNTIME_IDS = ["claude", "codex"];
|
|
11
|
+
var NEUTRAL_EVENTS = [
|
|
12
|
+
"session_start",
|
|
13
|
+
"prompt",
|
|
14
|
+
"before_tool",
|
|
15
|
+
"after_tool",
|
|
16
|
+
"stop",
|
|
17
|
+
"session_end",
|
|
18
|
+
"notification"
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
// src/hooks/runtimes/antigravity/capabilities.ts
|
|
22
|
+
var capabilities = {
|
|
23
|
+
events: {
|
|
24
|
+
session_start: "measured",
|
|
25
|
+
prompt: "measured",
|
|
26
|
+
before_tool: "measured",
|
|
27
|
+
after_tool: "measured",
|
|
28
|
+
stop: "measured",
|
|
29
|
+
session_end: "unsupported",
|
|
30
|
+
notification: "unsupported"
|
|
31
|
+
},
|
|
32
|
+
outputs: {
|
|
33
|
+
deny: "measured",
|
|
34
|
+
ask: "unsupported",
|
|
35
|
+
context: "measured",
|
|
36
|
+
feedback: "unqualified"
|
|
37
|
+
},
|
|
38
|
+
context: ["session_start", "prompt", "stop"]
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/hooks/runtimes/antigravity/events.ts
|
|
42
|
+
var NATIVE_EVENTS = {
|
|
43
|
+
session_start: "PreInvocation",
|
|
44
|
+
prompt: "PreInvocation",
|
|
45
|
+
before_tool: "PreToolUse",
|
|
46
|
+
after_tool: "PostToolUse",
|
|
47
|
+
stop: "Stop"
|
|
48
|
+
};
|
|
49
|
+
var TOOL_KINDS = {
|
|
50
|
+
run_command: "shell",
|
|
51
|
+
write_to_file: "edit",
|
|
52
|
+
replace_file_content: "edit",
|
|
53
|
+
multi_replace_file_content: "edit",
|
|
54
|
+
view_file: "read",
|
|
55
|
+
list_dir: "read",
|
|
56
|
+
grep_search: "read"
|
|
57
|
+
};
|
|
58
|
+
function toolKind(name) {
|
|
59
|
+
return TOOL_KINDS[name] ?? "other";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/hooks/runtimes/answer.ts
|
|
63
|
+
function readAnswer(stdout, pick) {
|
|
64
|
+
const text7 = stdout.trim();
|
|
65
|
+
if (!text7) return "allow";
|
|
66
|
+
try {
|
|
67
|
+
const out = JSON.parse(text7);
|
|
68
|
+
if (!out || typeof out !== "object" || Array.isArray(out)) return null;
|
|
69
|
+
return pick(out);
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function field(value, key) {
|
|
75
|
+
return value && typeof value === "object" ? value[key] : void 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/hooks/runtimes/antigravity/output.ts
|
|
79
|
+
var MAX_REASON = 2e3;
|
|
80
|
+
function silent(event) {
|
|
81
|
+
return event === "before_tool" ? '{"decision":"allow"}\n' : "{}\n";
|
|
82
|
+
}
|
|
83
|
+
function render(input, decision) {
|
|
84
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
85
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON);
|
|
86
|
+
if (input.event === "before_tool") {
|
|
87
|
+
const out2 = blocking ? { decision: "deny", reason } : { decision: "allow" };
|
|
88
|
+
return { stdout: `${JSON.stringify(out2)}
|
|
89
|
+
`, exitCode: 0 };
|
|
90
|
+
}
|
|
91
|
+
const notes = [...decision.context ?? []];
|
|
92
|
+
if (blocking) notes.unshift(reason);
|
|
93
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
94
|
+
let out = {};
|
|
95
|
+
if (notes.length > 0 && input.event !== "after_tool") {
|
|
96
|
+
if (input.event === "stop" && !input.stop?.active)
|
|
97
|
+
out = { decision: "continue", reason: notes.join("\n\n") };
|
|
98
|
+
if (input.event === "session_start" || input.event === "prompt")
|
|
99
|
+
out = { injectSteps: [{ ephemeralMessage: notes.join("\n\n") }] };
|
|
100
|
+
}
|
|
101
|
+
return { stdout: `${JSON.stringify(out)}
|
|
102
|
+
`, exitCode: 0 };
|
|
103
|
+
}
|
|
104
|
+
function verdict(stdout) {
|
|
105
|
+
return readAnswer(
|
|
106
|
+
stdout,
|
|
107
|
+
(out) => out["decision"] === "deny" ? "deny" : "allow"
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/hooks/runtimes/antigravity/payload.ts
|
|
112
|
+
import { isAbsolute, resolve } from "path";
|
|
113
|
+
function object(value) {
|
|
114
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
115
|
+
}
|
|
116
|
+
var text = (value) => typeof value === "string" ? value : void 0;
|
|
117
|
+
var count = (value) => typeof value === "number" ? value : 0;
|
|
118
|
+
function invocationEvent(raw) {
|
|
119
|
+
if (count(raw["invocationNum"]) !== 0) return null;
|
|
120
|
+
return count(raw["initialNumSteps"]) <= 1 ? "session_start" : "prompt";
|
|
121
|
+
}
|
|
122
|
+
var NEUTRAL = {
|
|
123
|
+
PreToolUse: "before_tool",
|
|
124
|
+
PostToolUse: "after_tool",
|
|
125
|
+
Stop: "stop"
|
|
126
|
+
};
|
|
127
|
+
function normalize(nativeEvent, payload, projectRoot) {
|
|
128
|
+
const raw = object(payload);
|
|
129
|
+
const event = nativeEvent === "PreInvocation" ? invocationEvent(raw) : NEUTRAL[nativeEvent] ?? null;
|
|
130
|
+
if (!event) return null;
|
|
131
|
+
const call = object(raw["toolCall"]);
|
|
132
|
+
const args = object(call["args"]);
|
|
133
|
+
const workspaces = Array.isArray(raw["workspacePaths"]) ? raw["workspacePaths"] : [];
|
|
134
|
+
const cwd = text(args["Cwd"]) ?? text(workspaces[0]) ?? projectRoot;
|
|
135
|
+
const name = text(call["name"]);
|
|
136
|
+
const kind = name ? toolKind(name) : void 0;
|
|
137
|
+
const target = text(args["TargetFile"]);
|
|
138
|
+
const paths = target ? [isAbsolute(target) ? target : resolve(cwd, target)] : [];
|
|
139
|
+
return {
|
|
140
|
+
contract: 2,
|
|
141
|
+
runtime: "antigravity",
|
|
142
|
+
event,
|
|
143
|
+
session: { id: text(raw["conversationId"]) ?? "" },
|
|
144
|
+
cwd,
|
|
145
|
+
projectRoot,
|
|
146
|
+
...name && kind ? {
|
|
147
|
+
tool: {
|
|
148
|
+
kind,
|
|
149
|
+
native: name,
|
|
150
|
+
...kind === "shell" ? { command: text(args["CommandLine"]) ?? "" } : {},
|
|
151
|
+
paths: kind === "shell" ? [] : paths
|
|
152
|
+
}
|
|
153
|
+
} : {},
|
|
154
|
+
...event === "stop" ? { stop: { active: count(raw["executionNum"]) > 0 } } : {}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/hooks/runtimes/climb.ts
|
|
159
|
+
function climbCommand(run, missing, start) {
|
|
160
|
+
const skip = `echo "pmcp: no pmcp.toml at or above $(pwd -P); hook skipped" >&2`;
|
|
161
|
+
const from = start ? `cd ${start} 2>/dev/null; ` : "";
|
|
162
|
+
return `${from}cd "$(pwd -P)" && while [ ! -f pmcp.toml ] && [ "$(pwd)" != / ]; do cd ..; done; if [ -f pmcp.toml ]; then exec ${run}; fi; ${skip}` + (missing ? `; ${missing}` : "");
|
|
163
|
+
}
|
|
164
|
+
var HOOK_PATH = ".agents/pmcp-hooks/hook.mjs";
|
|
165
|
+
|
|
166
|
+
// src/hooks/runtimes/antigravity/projection.ts
|
|
167
|
+
var HOOKS_FILE = ".agents/hooks.json";
|
|
168
|
+
var HOOK_SET = "pmcp";
|
|
169
|
+
var TOOL_EVENTS = /* @__PURE__ */ new Set(["PreToolUse", "PostToolUse"]);
|
|
170
|
+
function command(event) {
|
|
171
|
+
const allow = event === "before_tool" ? `printf '%s\\n' '{"decision":"allow"}'` : void 0;
|
|
172
|
+
return climbCommand(`node ${HOOK_PATH} antigravity ${event}`, allow);
|
|
173
|
+
}
|
|
174
|
+
function project(plan) {
|
|
175
|
+
const byNative = /* @__PURE__ */ new Map();
|
|
176
|
+
for (const binding of plan.bindings) {
|
|
177
|
+
if (byNative.has(binding.native)) continue;
|
|
178
|
+
const event = binding.native === "PreInvocation" ? "prompt" : binding.event;
|
|
179
|
+
const hook = {
|
|
180
|
+
type: "command",
|
|
181
|
+
command: command(event),
|
|
182
|
+
timeout: binding.timeoutSec
|
|
183
|
+
};
|
|
184
|
+
const entry = TOOL_EVENTS.has(binding.native) ? { matcher: "*", hooks: [hook] } : hook;
|
|
185
|
+
byNative.set(binding.native, entry);
|
|
186
|
+
}
|
|
187
|
+
return [...byNative].map(([native, entry]) => ({
|
|
188
|
+
file: HOOKS_FILE,
|
|
189
|
+
key: [HOOK_SET, native],
|
|
190
|
+
entries: [entry]
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/hooks/runtimes/antigravity/synthetic.ts
|
|
195
|
+
function synthetic(options) {
|
|
196
|
+
const { cwd, session, event, command: command6, path, tool } = options;
|
|
197
|
+
const defaults = {
|
|
198
|
+
shell: "run_command",
|
|
199
|
+
edit: "write_to_file",
|
|
200
|
+
read: "view_file",
|
|
201
|
+
other: "unknown_tool"
|
|
202
|
+
};
|
|
203
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
204
|
+
if (!name && command6 !== void 0) name = "run_command";
|
|
205
|
+
if (!name && path !== void 0) name = "write_to_file";
|
|
206
|
+
const args = name === "run_command" ? { CommandLine: command6 ?? "", Cwd: cwd } : { TargetFile: path ?? "", CodeContent: "" };
|
|
207
|
+
return {
|
|
208
|
+
conversationId: session,
|
|
209
|
+
workspacePaths: [cwd],
|
|
210
|
+
...event === "PreInvocation" ? { invocationNum: 0, initialNumSteps: 1 } : {},
|
|
211
|
+
...event === "Stop" ? { executionNum: 0 } : {},
|
|
212
|
+
...name ? { toolCall: { name, args } } : {}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/hooks/runtimes/antigravity/adapter.ts
|
|
217
|
+
var antigravity = {
|
|
218
|
+
id: "antigravity",
|
|
219
|
+
synthetic,
|
|
220
|
+
capabilities,
|
|
221
|
+
bindings(event, _tools, timeoutSec) {
|
|
222
|
+
const native = NATIVE_EVENTS[event];
|
|
223
|
+
if (!native || capabilities.events[event] === "unsupported") return [];
|
|
224
|
+
return [{ event, native, timeoutSec }];
|
|
225
|
+
},
|
|
226
|
+
normalize,
|
|
227
|
+
render,
|
|
228
|
+
readVerdict: verdict,
|
|
229
|
+
silent,
|
|
230
|
+
project,
|
|
231
|
+
nativeFiles: [{ file: HOOKS_FILE, root: HOOK_SET }],
|
|
232
|
+
nativeEvent: (event) => NATIVE_EVENTS[event],
|
|
233
|
+
trust: "none"
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// src/hooks/runtimes/claude/capabilities.ts
|
|
237
|
+
var capabilities2 = {
|
|
238
|
+
events: {
|
|
239
|
+
session_start: "measured",
|
|
240
|
+
prompt: "measured",
|
|
241
|
+
before_tool: "measured",
|
|
242
|
+
after_tool: "measured",
|
|
243
|
+
stop: "measured",
|
|
244
|
+
session_end: "measured",
|
|
245
|
+
notification: "unqualified"
|
|
246
|
+
},
|
|
247
|
+
outputs: {
|
|
248
|
+
deny: "measured",
|
|
249
|
+
ask: "unqualified",
|
|
250
|
+
context: "measured",
|
|
251
|
+
feedback: "unqualified"
|
|
252
|
+
},
|
|
253
|
+
context: ["session_start", "prompt", "before_tool", "after_tool", "stop"]
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// src/hooks/runtimes/claude/events.ts
|
|
257
|
+
var NATIVE_EVENTS2 = {
|
|
258
|
+
session_start: "SessionStart",
|
|
259
|
+
prompt: "UserPromptSubmit",
|
|
260
|
+
before_tool: "PreToolUse",
|
|
261
|
+
after_tool: "PostToolUse",
|
|
262
|
+
stop: "Stop",
|
|
263
|
+
session_end: "SessionEnd",
|
|
264
|
+
notification: "Notification"
|
|
265
|
+
};
|
|
266
|
+
var NEUTRAL_EVENTS2 = Object.fromEntries(
|
|
267
|
+
Object.entries(NATIVE_EVENTS2).map(([neutral, native]) => [native, neutral])
|
|
268
|
+
);
|
|
269
|
+
var TOOL_KINDS2 = {
|
|
270
|
+
Bash: "shell",
|
|
271
|
+
Edit: "edit",
|
|
272
|
+
Write: "edit",
|
|
273
|
+
MultiEdit: "edit",
|
|
274
|
+
NotebookEdit: "edit",
|
|
275
|
+
Read: "read",
|
|
276
|
+
Grep: "read",
|
|
277
|
+
Glob: "read"
|
|
278
|
+
};
|
|
279
|
+
function toolKind2(name) {
|
|
280
|
+
return TOOL_KINDS2[name] ?? "other";
|
|
281
|
+
}
|
|
282
|
+
function matcherFor(tools) {
|
|
283
|
+
if (tools.includes("other")) return void 0;
|
|
284
|
+
const names = Object.entries(TOOL_KINDS2).filter(([, kind]) => tools.includes(kind)).map(([name]) => name);
|
|
285
|
+
return names.length > 0 ? names.join("|") : void 0;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/hooks/runtimes/claude/output.ts
|
|
289
|
+
var MAX_REASON2 = 2e3;
|
|
290
|
+
function render2(input, decision) {
|
|
291
|
+
const hookEventName = NATIVE_EVENTS2[input.event];
|
|
292
|
+
const context = decision.context?.length ? decision.context.join("\n\n") : void 0;
|
|
293
|
+
const reason = decision.reason?.slice(0, MAX_REASON2);
|
|
294
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
295
|
+
const out = shape(input, decision, {
|
|
296
|
+
hookEventName,
|
|
297
|
+
context,
|
|
298
|
+
reason,
|
|
299
|
+
blocking
|
|
300
|
+
});
|
|
301
|
+
return { stdout: out ? `${JSON.stringify(out)}
|
|
302
|
+
` : "", exitCode: 0 };
|
|
303
|
+
}
|
|
304
|
+
function shape(input, decision, parts) {
|
|
305
|
+
const { hookEventName, context, reason, blocking } = parts;
|
|
306
|
+
const contextOnly = context ? { hookSpecificOutput: { hookEventName, additionalContext: context } } : {};
|
|
307
|
+
if (input.event === "before_tool") {
|
|
308
|
+
if (!blocking && !context) return null;
|
|
309
|
+
let refusal = {};
|
|
310
|
+
if (blocking) {
|
|
311
|
+
refusal = {
|
|
312
|
+
permissionDecision: decision.verdict,
|
|
313
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
hookSpecificOutput: {
|
|
318
|
+
hookEventName,
|
|
319
|
+
...refusal,
|
|
320
|
+
...context ? { additionalContext: context } : {}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
if (input.event === "prompt" || input.event === "after_tool" || input.event === "stop") {
|
|
325
|
+
const feedback = blocking ? reason ?? "Refused" : decision.feedback;
|
|
326
|
+
if (!feedback && !context) return null;
|
|
327
|
+
return {
|
|
328
|
+
...feedback ? { decision: "block", reason: feedback } : {},
|
|
329
|
+
...contextOnly
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
if (input.event === "session_start" && context) return contextOnly;
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
function verdict2(stdout) {
|
|
336
|
+
return readAnswer(stdout, (out) => {
|
|
337
|
+
const decision = field(out["hookSpecificOutput"], "permissionDecision");
|
|
338
|
+
return decision === "deny" || decision === "ask" ? decision : "allow";
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// src/hooks/runtimes/claude/payload.ts
|
|
343
|
+
import { isAbsolute as isAbsolute2, resolve as resolve2 } from "path";
|
|
344
|
+
function object2(value) {
|
|
345
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
346
|
+
}
|
|
347
|
+
var text2 = (value) => typeof value === "string" ? value : void 0;
|
|
348
|
+
function normalize2(nativeEvent, payload, projectRoot) {
|
|
349
|
+
const event = NEUTRAL_EVENTS2[nativeEvent];
|
|
350
|
+
const raw = object2(payload);
|
|
351
|
+
if (!event) return null;
|
|
352
|
+
const cwd = text2(raw["cwd"]) ?? projectRoot;
|
|
353
|
+
const name = text2(raw["tool_name"]);
|
|
354
|
+
const toolInput = object2(raw["tool_input"]);
|
|
355
|
+
const kind = name ? toolKind2(name) : void 0;
|
|
356
|
+
const paths = [
|
|
357
|
+
toolInput["file_path"],
|
|
358
|
+
toolInput["notebook_path"],
|
|
359
|
+
kind === "read" ? toolInput["path"] : void 0
|
|
360
|
+
].filter((value) => typeof value === "string" && !!value).map((path) => isAbsolute2(path) ? path : resolve2(cwd, path));
|
|
361
|
+
const agentType = text2(raw["agent_type"]);
|
|
362
|
+
const turnId = text2(raw["prompt_id"]);
|
|
363
|
+
const lastMessage = text2(raw["last_assistant_message"]);
|
|
364
|
+
const prompt = text2(raw["prompt"]);
|
|
365
|
+
return {
|
|
366
|
+
contract: 2,
|
|
367
|
+
runtime: "claude",
|
|
368
|
+
event,
|
|
369
|
+
session: {
|
|
370
|
+
id: text2(raw["session_id"]) ?? "",
|
|
371
|
+
...agentType ? { agentType } : {},
|
|
372
|
+
...turnId ? { turnId } : {}
|
|
373
|
+
},
|
|
374
|
+
cwd,
|
|
375
|
+
projectRoot,
|
|
376
|
+
...toolFields(name, kind, toolInput, paths),
|
|
377
|
+
...prompt !== void 0 ? { prompt } : {},
|
|
378
|
+
...stopFields(event, raw, lastMessage),
|
|
379
|
+
...notificationFields(event, raw)
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
function notificationFields(event, raw) {
|
|
383
|
+
if (event !== "notification") return {};
|
|
384
|
+
const type = text2(raw["notification_type"]);
|
|
385
|
+
const message = text2(raw["message"]);
|
|
386
|
+
return {
|
|
387
|
+
notification: {
|
|
388
|
+
...type ? { type } : {},
|
|
389
|
+
...message ? { message } : {}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function toolFields(name, kind, input, paths) {
|
|
394
|
+
if (!name || !kind) return {};
|
|
395
|
+
return {
|
|
396
|
+
tool: {
|
|
397
|
+
kind,
|
|
398
|
+
native: name,
|
|
399
|
+
...kind === "shell" ? { command: text2(input["command"]) ?? "" } : {},
|
|
400
|
+
paths
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function stopFields(event, raw, lastMessage) {
|
|
405
|
+
if (event !== "stop") return {};
|
|
406
|
+
return {
|
|
407
|
+
stop: {
|
|
408
|
+
active: raw["stop_hook_active"] === true,
|
|
409
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/hooks/runtimes/label.ts
|
|
415
|
+
function hookLabel(event) {
|
|
416
|
+
return `pmcp ${event.replaceAll("_", " ")}`;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// src/hooks/runtimes/claude/projection.ts
|
|
420
|
+
var SETTINGS_FILE = ".claude/settings.json";
|
|
421
|
+
function command2(event) {
|
|
422
|
+
return `node "$CLAUDE_PROJECT_DIR/.agents/pmcp-hooks/hook.mjs" claude ${event}`;
|
|
423
|
+
}
|
|
424
|
+
function project2(plan) {
|
|
425
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
426
|
+
for (const binding of plan.bindings) {
|
|
427
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
428
|
+
entries.push({
|
|
429
|
+
...binding.matcher ? { matcher: binding.matcher } : {},
|
|
430
|
+
hooks: [
|
|
431
|
+
{
|
|
432
|
+
type: "command",
|
|
433
|
+
command: command2(binding.event),
|
|
434
|
+
timeout: binding.timeoutSec,
|
|
435
|
+
statusMessage: hookLabel(binding.event)
|
|
436
|
+
}
|
|
437
|
+
]
|
|
438
|
+
});
|
|
439
|
+
byEvent.set(binding.native, entries);
|
|
440
|
+
}
|
|
441
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
442
|
+
file: SETTINGS_FILE,
|
|
443
|
+
key: ["hooks", native],
|
|
444
|
+
entries
|
|
445
|
+
}));
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// src/hooks/runtimes/claude/synthetic.ts
|
|
449
|
+
function synthetic2(options) {
|
|
450
|
+
const { cwd, session, event, command: command6, path, tool } = options;
|
|
451
|
+
const defaults = {
|
|
452
|
+
shell: "Bash",
|
|
453
|
+
edit: "Edit",
|
|
454
|
+
read: "Read",
|
|
455
|
+
other: "Unknown"
|
|
456
|
+
};
|
|
457
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
458
|
+
if (!name && command6 !== void 0) name = "Bash";
|
|
459
|
+
if (!name && path !== void 0) name = "Edit";
|
|
460
|
+
return {
|
|
461
|
+
session_id: session,
|
|
462
|
+
cwd,
|
|
463
|
+
hook_event_name: event,
|
|
464
|
+
prompt_id: "pmcp-hooks-test-turn",
|
|
465
|
+
...name ? { tool_name: name, tool_input: { command: command6, file_path: path } } : {},
|
|
466
|
+
...event === "Stop" ? { stop_hook_active: false } : {}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// src/hooks/runtimes/claude/adapter.ts
|
|
471
|
+
var TOOL_EVENTS2 = /* @__PURE__ */ new Set(["before_tool", "after_tool"]);
|
|
472
|
+
var claude = {
|
|
473
|
+
id: "claude",
|
|
474
|
+
synthetic: synthetic2,
|
|
475
|
+
capabilities: capabilities2,
|
|
476
|
+
bindings(event, tools, timeoutSec) {
|
|
477
|
+
if (capabilities2.events[event] === "unsupported") return [];
|
|
478
|
+
const matcher = TOOL_EVENTS2.has(event) ? matcherFor(tools) : void 0;
|
|
479
|
+
return [
|
|
480
|
+
{
|
|
481
|
+
event,
|
|
482
|
+
native: NATIVE_EVENTS2[event],
|
|
483
|
+
...matcher ? { matcher } : {},
|
|
484
|
+
timeoutSec
|
|
485
|
+
}
|
|
486
|
+
];
|
|
487
|
+
},
|
|
488
|
+
normalize: normalize2,
|
|
489
|
+
render: render2,
|
|
490
|
+
readVerdict: verdict2,
|
|
491
|
+
project: project2,
|
|
492
|
+
nativeFiles: [{ file: SETTINGS_FILE, root: "hooks" }],
|
|
493
|
+
nativeEvent: (event) => NATIVE_EVENTS2[event],
|
|
494
|
+
trust: "none"
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
// src/hooks/runtimes/codex/capabilities.ts
|
|
498
|
+
var capabilities3 = {
|
|
499
|
+
events: {
|
|
500
|
+
session_start: "measured",
|
|
501
|
+
prompt: "measured",
|
|
502
|
+
before_tool: "measured",
|
|
503
|
+
after_tool: "measured",
|
|
504
|
+
stop: "measured",
|
|
505
|
+
session_end: "measured",
|
|
506
|
+
notification: "unsupported"
|
|
507
|
+
},
|
|
508
|
+
outputs: {
|
|
509
|
+
deny: "measured",
|
|
510
|
+
ask: "unsupported",
|
|
511
|
+
context: "measured",
|
|
512
|
+
feedback: "unqualified"
|
|
513
|
+
},
|
|
514
|
+
context: ["session_start", "prompt", "before_tool", "after_tool"]
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// src/hooks/runtimes/codex/events.ts
|
|
518
|
+
var NATIVE_EVENTS3 = {
|
|
519
|
+
session_start: "SessionStart",
|
|
520
|
+
prompt: "UserPromptSubmit",
|
|
521
|
+
before_tool: "PreToolUse",
|
|
522
|
+
after_tool: "PostToolUse",
|
|
523
|
+
stop: "Stop",
|
|
524
|
+
session_end: "SessionEnd"
|
|
525
|
+
};
|
|
526
|
+
var NEUTRAL_EVENTS3 = Object.fromEntries(
|
|
527
|
+
Object.entries(NATIVE_EVENTS3).map(([neutral, native]) => [native, neutral])
|
|
528
|
+
);
|
|
529
|
+
var TOOL_KINDS3 = {
|
|
530
|
+
Bash: "shell",
|
|
531
|
+
exec_command: "shell",
|
|
532
|
+
shell: "shell",
|
|
533
|
+
shell_command: "shell",
|
|
534
|
+
apply_patch: "edit"
|
|
535
|
+
};
|
|
536
|
+
function toolKind3(name) {
|
|
537
|
+
return TOOL_KINDS3[name] ?? "other";
|
|
538
|
+
}
|
|
539
|
+
function matcherFor2(tools) {
|
|
540
|
+
if (tools.includes("other")) return void 0;
|
|
541
|
+
const names = Object.entries(TOOL_KINDS3).filter(([, kind]) => tools.includes(kind)).map(([name]) => name);
|
|
542
|
+
return names.length > 0 ? names.join("|") : void 0;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// src/hooks/runtimes/codex/output.ts
|
|
546
|
+
var MAX_REASON3 = 2e3;
|
|
547
|
+
function render3(input, decision) {
|
|
548
|
+
const hookEventName = NATIVE_EVENTS3[input.event];
|
|
549
|
+
const context = decision.context?.length ? decision.context.join("\n\n") : void 0;
|
|
550
|
+
const reason = decision.reason?.slice(0, MAX_REASON3);
|
|
551
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
552
|
+
const out = shape2(input, decision, {
|
|
553
|
+
hookEventName,
|
|
554
|
+
context,
|
|
555
|
+
reason,
|
|
556
|
+
blocking
|
|
557
|
+
});
|
|
558
|
+
if (!out && input.event === "stop") return { stdout: "{}\n", exitCode: 0 };
|
|
559
|
+
return { stdout: out ? `${JSON.stringify(out)}
|
|
560
|
+
` : "", exitCode: 0 };
|
|
561
|
+
}
|
|
562
|
+
function shape2(input, decision, parts) {
|
|
563
|
+
const { hookEventName, context, reason, blocking } = parts;
|
|
564
|
+
const contextOnly = context ? { hookSpecificOutput: { hookEventName, additionalContext: context } } : {};
|
|
565
|
+
if (input.event === "before_tool") {
|
|
566
|
+
if (!blocking && !context) return null;
|
|
567
|
+
let refusal = {};
|
|
568
|
+
if (blocking) {
|
|
569
|
+
refusal = {
|
|
570
|
+
permissionDecision: "deny",
|
|
571
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
hookSpecificOutput: {
|
|
576
|
+
hookEventName,
|
|
577
|
+
...refusal,
|
|
578
|
+
...context ? { additionalContext: context } : {}
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
if (input.event === "prompt" || input.event === "after_tool" || input.event === "stop") {
|
|
583
|
+
const feedback = blocking ? reason ?? "Refused" : decision.feedback;
|
|
584
|
+
if (!feedback && !context) return null;
|
|
585
|
+
return {
|
|
586
|
+
...feedback ? { decision: "block", reason: feedback } : {},
|
|
587
|
+
...input.event === "stop" ? {} : contextOnly
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
if (input.event === "session_start" && context) return contextOnly;
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
function verdict3(stdout) {
|
|
594
|
+
return readAnswer(stdout, (out) => {
|
|
595
|
+
const decision = field(out["hookSpecificOutput"], "permissionDecision");
|
|
596
|
+
return decision === "deny" || decision === "ask" ? decision : "allow";
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/hooks/runtimes/codex/payload.ts
|
|
601
|
+
import { isAbsolute as isAbsolute3, resolve as resolve3 } from "path";
|
|
602
|
+
var PATCH_HEADER = /^\*\*\* (?:Update File|Add File|Delete File|Move to): (.+)$/gmu;
|
|
603
|
+
function object3(value) {
|
|
604
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
605
|
+
}
|
|
606
|
+
var text3 = (value) => typeof value === "string" ? value : void 0;
|
|
607
|
+
function patchPaths(patch) {
|
|
608
|
+
return [...patch.matchAll(PATCH_HEADER)].map((match) => match[1].trim());
|
|
609
|
+
}
|
|
610
|
+
function normalize3(nativeEvent, payload, projectRoot) {
|
|
611
|
+
const event = NEUTRAL_EVENTS3[nativeEvent];
|
|
612
|
+
const raw = object3(payload);
|
|
613
|
+
if (!event) return null;
|
|
614
|
+
const cwd = text3(raw["cwd"]) ?? projectRoot;
|
|
615
|
+
const name = text3(raw["tool_name"]);
|
|
616
|
+
const toolInput = object3(raw["tool_input"]);
|
|
617
|
+
const kind = name ? toolKind3(name) : void 0;
|
|
618
|
+
const body = text3(toolInput["command"]) ?? "";
|
|
619
|
+
const paths = (kind === "edit" ? patchPaths(body) : []).filter(Boolean).map((path) => isAbsolute3(path) ? path : resolve3(cwd, path));
|
|
620
|
+
const turnId = text3(raw["turn_id"]);
|
|
621
|
+
const lastMessage = text3(raw["last_assistant_message"]);
|
|
622
|
+
const prompt = text3(raw["prompt"]);
|
|
623
|
+
return {
|
|
624
|
+
contract: 2,
|
|
625
|
+
runtime: "codex",
|
|
626
|
+
event,
|
|
627
|
+
session: {
|
|
628
|
+
id: text3(raw["session_id"]) ?? "",
|
|
629
|
+
...turnId ? { turnId } : {}
|
|
630
|
+
},
|
|
631
|
+
cwd,
|
|
632
|
+
projectRoot,
|
|
633
|
+
...toolFields2(name, kind, body, paths),
|
|
634
|
+
...prompt !== void 0 ? { prompt } : {},
|
|
635
|
+
...stopFields2(event, raw, lastMessage)
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
function toolFields2(name, kind, body, paths) {
|
|
639
|
+
if (!name || !kind) return {};
|
|
640
|
+
return {
|
|
641
|
+
tool: {
|
|
642
|
+
kind,
|
|
643
|
+
native: name,
|
|
644
|
+
...kind === "shell" || kind === "edit" ? { command: body } : {},
|
|
645
|
+
paths
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
function stopFields2(event, raw, lastMessage) {
|
|
650
|
+
if (event !== "stop") return {};
|
|
651
|
+
return {
|
|
652
|
+
stop: {
|
|
653
|
+
active: raw["stop_hook_active"] === true,
|
|
654
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// src/hooks/runtimes/codex/projection.ts
|
|
660
|
+
var HOOKS_FILE2 = ".codex/hooks.json";
|
|
661
|
+
function command3(event) {
|
|
662
|
+
return climbCommand(`node ${HOOK_PATH} codex ${event}`);
|
|
663
|
+
}
|
|
664
|
+
function project3(plan) {
|
|
665
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
666
|
+
for (const binding of plan.bindings) {
|
|
667
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
668
|
+
entries.push({
|
|
669
|
+
...binding.matcher ? { matcher: binding.matcher } : {},
|
|
670
|
+
hooks: [
|
|
671
|
+
{
|
|
672
|
+
type: "command",
|
|
673
|
+
command: command3(binding.event),
|
|
674
|
+
timeout: binding.timeoutSec,
|
|
675
|
+
statusMessage: hookLabel(binding.event)
|
|
676
|
+
}
|
|
677
|
+
]
|
|
678
|
+
});
|
|
679
|
+
byEvent.set(binding.native, entries);
|
|
680
|
+
}
|
|
681
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
682
|
+
file: HOOKS_FILE2,
|
|
683
|
+
key: ["hooks", native],
|
|
684
|
+
entries
|
|
685
|
+
}));
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// src/hooks/runtimes/codex/synthetic.ts
|
|
689
|
+
function synthetic3(options) {
|
|
690
|
+
const { cwd, session, event, command: command6, path, tool } = options;
|
|
691
|
+
const defaults = {
|
|
692
|
+
shell: "Bash",
|
|
693
|
+
edit: "apply_patch",
|
|
694
|
+
other: "Unknown"
|
|
695
|
+
};
|
|
696
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
697
|
+
if (!name && command6 !== void 0) name = "Bash";
|
|
698
|
+
if (!name && path !== void 0) name = "apply_patch";
|
|
699
|
+
const body = name === "apply_patch" && path !== void 0 ? "*** Begin Patch\n*** Update File: " + path + "\n*** End Patch" : command6;
|
|
700
|
+
return {
|
|
701
|
+
session_id: session,
|
|
702
|
+
cwd,
|
|
703
|
+
hook_event_name: event,
|
|
704
|
+
turn_id: "pmcp-hooks-test-turn",
|
|
705
|
+
...name ? { tool_name: name, tool_input: { command: body } } : {},
|
|
706
|
+
...event === "Stop" ? { stop_hook_active: false } : {}
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// src/hooks/runtimes/codex/adapter.ts
|
|
711
|
+
var TOOL_EVENTS3 = /* @__PURE__ */ new Set(["before_tool", "after_tool"]);
|
|
712
|
+
var SESSION_END_TIMEOUT_SEC = 3;
|
|
713
|
+
var codex = {
|
|
714
|
+
id: "codex",
|
|
715
|
+
synthetic: synthetic3,
|
|
716
|
+
capabilities: capabilities3,
|
|
717
|
+
bindings(event, tools, timeoutSec) {
|
|
718
|
+
const native = NATIVE_EVENTS3[event];
|
|
719
|
+
if (!native || capabilities3.events[event] === "unsupported") return [];
|
|
720
|
+
const matcher = TOOL_EVENTS3.has(event) ? matcherFor2(tools) : void 0;
|
|
721
|
+
const timeout = event === "session_end" ? Math.min(timeoutSec, SESSION_END_TIMEOUT_SEC) : timeoutSec;
|
|
722
|
+
return [
|
|
723
|
+
{ event, native, ...matcher ? { matcher } : {}, timeoutSec: timeout }
|
|
724
|
+
];
|
|
725
|
+
},
|
|
726
|
+
normalize: normalize3,
|
|
727
|
+
render: render3,
|
|
728
|
+
readVerdict: verdict3,
|
|
729
|
+
project: project3,
|
|
730
|
+
nativeFiles: [{ file: HOOKS_FILE2, root: "hooks" }],
|
|
731
|
+
nativeEvent: (event) => NATIVE_EVENTS3[event],
|
|
732
|
+
trust: "hash_review"
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
// src/hooks/runtimes/gemini/capabilities.ts
|
|
736
|
+
var capabilities4 = {
|
|
737
|
+
events: {
|
|
738
|
+
session_start: "measured",
|
|
739
|
+
prompt: "measured",
|
|
740
|
+
before_tool: "measured",
|
|
741
|
+
after_tool: "measured",
|
|
742
|
+
stop: "measured",
|
|
743
|
+
session_end: "measured",
|
|
744
|
+
notification: "unqualified"
|
|
745
|
+
},
|
|
746
|
+
outputs: {
|
|
747
|
+
deny: "measured",
|
|
748
|
+
ask: "unsupported",
|
|
749
|
+
context: "measured",
|
|
750
|
+
feedback: "unqualified"
|
|
751
|
+
},
|
|
752
|
+
context: ["session_start", "prompt", "after_tool"]
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
// src/hooks/runtimes/gemini/events.ts
|
|
756
|
+
var NATIVE_EVENTS4 = {
|
|
757
|
+
session_start: "SessionStart",
|
|
758
|
+
prompt: "BeforeAgent",
|
|
759
|
+
before_tool: "BeforeTool",
|
|
760
|
+
after_tool: "AfterTool",
|
|
761
|
+
stop: "AfterAgent",
|
|
762
|
+
session_end: "SessionEnd",
|
|
763
|
+
notification: "Notification"
|
|
764
|
+
};
|
|
765
|
+
var NEUTRAL_EVENTS4 = Object.fromEntries(
|
|
766
|
+
Object.entries(NATIVE_EVENTS4).map(([neutral, native]) => [native, neutral])
|
|
767
|
+
);
|
|
768
|
+
var TOOL_KINDS4 = {
|
|
769
|
+
run_shell_command: "shell",
|
|
770
|
+
write_file: "edit",
|
|
771
|
+
replace: "edit",
|
|
772
|
+
read_file: "read",
|
|
773
|
+
read_many_files: "read",
|
|
774
|
+
glob: "read",
|
|
775
|
+
search_file_content: "read"
|
|
776
|
+
};
|
|
777
|
+
function toolKind4(name) {
|
|
778
|
+
return TOOL_KINDS4[name] ?? "other";
|
|
779
|
+
}
|
|
780
|
+
function matcherFor3(tools) {
|
|
781
|
+
if (tools.includes("other")) return void 0;
|
|
782
|
+
const names = Object.entries(TOOL_KINDS4).filter(([, kind]) => tools.includes(kind)).map(([name]) => name);
|
|
783
|
+
return names.length > 0 ? `^(${names.join("|")})$` : void 0;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// src/hooks/runtimes/gemini/output.ts
|
|
787
|
+
var MAX_REASON4 = 2e3;
|
|
788
|
+
var DELIVERS = [
|
|
789
|
+
"session_start",
|
|
790
|
+
"prompt",
|
|
791
|
+
"after_tool"
|
|
792
|
+
];
|
|
793
|
+
function render4(input, decision) {
|
|
794
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
795
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON4);
|
|
796
|
+
const out = {};
|
|
797
|
+
const notes = [...decision.context ?? []];
|
|
798
|
+
if (blocking && input.event === "before_tool")
|
|
799
|
+
Object.assign(out, { decision: "deny", reason });
|
|
800
|
+
else if (blocking) notes.unshift(reason);
|
|
801
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
802
|
+
if (notes.length > 0 && DELIVERS.includes(input.event))
|
|
803
|
+
out["hookSpecificOutput"] = {
|
|
804
|
+
hookEventName: NATIVE_EVENTS4[input.event],
|
|
805
|
+
additionalContext: notes.join("\n\n")
|
|
806
|
+
};
|
|
807
|
+
return { stdout: `${JSON.stringify(out)}
|
|
808
|
+
`, exitCode: 0 };
|
|
809
|
+
}
|
|
810
|
+
function verdict4(stdout) {
|
|
811
|
+
return readAnswer(
|
|
812
|
+
stdout,
|
|
813
|
+
(out) => out["decision"] === "deny" ? "deny" : "allow"
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// src/hooks/runtimes/gemini/payload.ts
|
|
818
|
+
import { isAbsolute as isAbsolute4, resolve as resolve4 } from "path";
|
|
819
|
+
function object4(value) {
|
|
820
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
821
|
+
}
|
|
822
|
+
var text4 = (value) => typeof value === "string" ? value : void 0;
|
|
823
|
+
function normalize4(nativeEvent, payload, projectRoot) {
|
|
824
|
+
const event = NEUTRAL_EVENTS4[nativeEvent];
|
|
825
|
+
if (!event) return null;
|
|
826
|
+
const raw = object4(payload);
|
|
827
|
+
const cwd = text4(raw["cwd"]) ?? projectRoot;
|
|
828
|
+
const name = text4(raw["tool_name"]);
|
|
829
|
+
const input = object4(raw["tool_input"]);
|
|
830
|
+
const kind = name ? toolKind4(name) : void 0;
|
|
831
|
+
const paths = [input["file_path"], input["absolute_path"], input["path"]].filter((value) => typeof value === "string" && !!value).map((path) => isAbsolute4(path) ? path : resolve4(cwd, path));
|
|
832
|
+
const prompt = text4(raw["prompt"]);
|
|
833
|
+
const lastMessage = text4(raw["prompt_response"]);
|
|
834
|
+
return {
|
|
835
|
+
contract: 2,
|
|
836
|
+
runtime: "gemini",
|
|
837
|
+
event,
|
|
838
|
+
session: { id: text4(raw["session_id"]) ?? "" },
|
|
839
|
+
cwd,
|
|
840
|
+
projectRoot,
|
|
841
|
+
...name && kind ? {
|
|
842
|
+
tool: {
|
|
843
|
+
kind,
|
|
844
|
+
native: name,
|
|
845
|
+
...kind === "shell" ? { command: text4(input["command"]) ?? "" } : {},
|
|
846
|
+
paths: kind === "shell" ? [] : paths
|
|
847
|
+
}
|
|
848
|
+
} : {},
|
|
849
|
+
...prompt !== void 0 && event === "prompt" ? { prompt } : {},
|
|
850
|
+
...event === "stop" ? {
|
|
851
|
+
stop: {
|
|
852
|
+
active: raw["stop_hook_active"] === true,
|
|
853
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
854
|
+
}
|
|
855
|
+
} : {}
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// src/hooks/runtimes/gemini/projection.ts
|
|
860
|
+
var SETTINGS_FILE2 = ".gemini/settings.json";
|
|
861
|
+
function command4(event) {
|
|
862
|
+
return climbCommand(`node ${HOOK_PATH} gemini ${event}`);
|
|
863
|
+
}
|
|
864
|
+
function project4(plan) {
|
|
865
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
866
|
+
for (const binding of plan.bindings) {
|
|
867
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
868
|
+
entries.push({
|
|
869
|
+
...binding.matcher ? { matcher: binding.matcher } : {},
|
|
870
|
+
hooks: [
|
|
871
|
+
{
|
|
872
|
+
type: "command",
|
|
873
|
+
name: `pmcp-${binding.event}`,
|
|
874
|
+
command: command4(binding.event),
|
|
875
|
+
timeout: binding.timeoutSec * 1e3
|
|
876
|
+
}
|
|
877
|
+
]
|
|
878
|
+
});
|
|
879
|
+
byEvent.set(binding.native, entries);
|
|
880
|
+
}
|
|
881
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
882
|
+
file: SETTINGS_FILE2,
|
|
883
|
+
key: ["hooks", native],
|
|
884
|
+
entries
|
|
885
|
+
}));
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// src/hooks/runtimes/gemini/synthetic.ts
|
|
889
|
+
function synthetic4(options) {
|
|
890
|
+
const { cwd, session, event, command: command6, path, tool } = options;
|
|
891
|
+
const defaults = {
|
|
892
|
+
shell: "run_shell_command",
|
|
893
|
+
edit: "write_file",
|
|
894
|
+
read: "read_file",
|
|
895
|
+
other: "unknown_tool"
|
|
896
|
+
};
|
|
897
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
898
|
+
if (!name && command6 !== void 0) name = "run_shell_command";
|
|
899
|
+
if (!name && path !== void 0) name = "write_file";
|
|
900
|
+
const input = name === "run_shell_command" ? { command: command6 ?? "" } : { file_path: path ?? "", content: "" };
|
|
901
|
+
return {
|
|
902
|
+
session_id: session,
|
|
903
|
+
cwd,
|
|
904
|
+
hook_event_name: event,
|
|
905
|
+
timestamp: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
906
|
+
...name ? { tool_name: name, tool_input: input } : {},
|
|
907
|
+
...event === "AfterAgent" ? { stop_hook_active: false } : {}
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// src/hooks/runtimes/gemini/adapter.ts
|
|
912
|
+
var TOOL_EVENTS4 = /* @__PURE__ */ new Set(["before_tool", "after_tool"]);
|
|
913
|
+
var gemini = {
|
|
914
|
+
id: "gemini",
|
|
915
|
+
synthetic: synthetic4,
|
|
916
|
+
capabilities: capabilities4,
|
|
917
|
+
bindings(event, tools, timeoutSec) {
|
|
918
|
+
if (capabilities4.events[event] === "unsupported") return [];
|
|
919
|
+
const matcher = TOOL_EVENTS4.has(event) ? matcherFor3(tools) : void 0;
|
|
920
|
+
return [
|
|
921
|
+
{
|
|
922
|
+
event,
|
|
923
|
+
native: NATIVE_EVENTS4[event],
|
|
924
|
+
...matcher ? { matcher } : {},
|
|
925
|
+
timeoutSec
|
|
926
|
+
}
|
|
927
|
+
];
|
|
928
|
+
},
|
|
929
|
+
normalize: normalize4,
|
|
930
|
+
render: render4,
|
|
931
|
+
readVerdict: verdict4,
|
|
932
|
+
project: project4,
|
|
933
|
+
nativeFiles: [{ file: SETTINGS_FILE2, root: "hooks" }],
|
|
934
|
+
nativeEvent: (event) => NATIVE_EVENTS4[event],
|
|
935
|
+
trust: "folder"
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
// src/hooks/runtimes/grok/capabilities.ts
|
|
939
|
+
var capabilities5 = {
|
|
940
|
+
events: {
|
|
941
|
+
session_start: "measured",
|
|
942
|
+
prompt: "measured",
|
|
943
|
+
before_tool: "measured",
|
|
944
|
+
after_tool: "measured",
|
|
945
|
+
stop: "measured",
|
|
946
|
+
session_end: "measured",
|
|
947
|
+
notification: "unsupported"
|
|
948
|
+
},
|
|
949
|
+
outputs: {
|
|
950
|
+
deny: "measured",
|
|
951
|
+
ask: "unsupported",
|
|
952
|
+
context: "measured",
|
|
953
|
+
feedback: "unqualified"
|
|
954
|
+
},
|
|
955
|
+
context: ["before_tool", "after_tool", "stop"]
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
// src/hooks/runtimes/grok/events.ts
|
|
959
|
+
var NATIVE_EVENTS5 = {
|
|
960
|
+
session_start: "SessionStart",
|
|
961
|
+
prompt: "UserPromptSubmit",
|
|
962
|
+
before_tool: "PreToolUse",
|
|
963
|
+
after_tool: "PostToolUse",
|
|
964
|
+
stop: "Stop",
|
|
965
|
+
session_end: "SessionEnd"
|
|
966
|
+
};
|
|
967
|
+
var NEUTRAL_EVENTS5 = Object.fromEntries(
|
|
968
|
+
Object.entries(NATIVE_EVENTS5).map(([neutral, native]) => [native, neutral])
|
|
969
|
+
);
|
|
970
|
+
var TOOL_KINDS5 = {
|
|
971
|
+
run_terminal_command: "shell",
|
|
972
|
+
write: "edit",
|
|
973
|
+
search_replace: "edit",
|
|
974
|
+
read_file: "read",
|
|
975
|
+
list_dir: "read",
|
|
976
|
+
grep_search: "read"
|
|
977
|
+
};
|
|
978
|
+
function toolKind5(name) {
|
|
979
|
+
return TOOL_KINDS5[name] ?? "other";
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// src/hooks/runtimes/grok/output.ts
|
|
983
|
+
var MAX_REASON5 = 2e3;
|
|
984
|
+
var DELIVERS2 = [
|
|
985
|
+
"before_tool",
|
|
986
|
+
"after_tool",
|
|
987
|
+
"stop"
|
|
988
|
+
];
|
|
989
|
+
function render5(input, decision) {
|
|
990
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
991
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON5);
|
|
992
|
+
const notes = [...decision.context ?? []];
|
|
993
|
+
const refuse = blocking && input.event === "before_tool";
|
|
994
|
+
if (blocking && !refuse) notes.unshift(reason);
|
|
995
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
996
|
+
const quiet = input.event === "stop" && input.stop?.active === true;
|
|
997
|
+
const context = notes.length > 0 && DELIVERS2.includes(input.event) && !quiet ? notes.join("\n\n") : void 0;
|
|
998
|
+
if (!refuse && !context) return { stdout: "", exitCode: 0 };
|
|
999
|
+
const out = {
|
|
1000
|
+
hookSpecificOutput: {
|
|
1001
|
+
hookEventName: NATIVE_EVENTS5[input.event],
|
|
1002
|
+
...refuse ? { permissionDecision: "deny", permissionDecisionReason: reason } : {},
|
|
1003
|
+
...context ? { additionalContext: context } : {}
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
return { stdout: `${JSON.stringify(out)}
|
|
1007
|
+
`, exitCode: 0 };
|
|
1008
|
+
}
|
|
1009
|
+
function verdict5(stdout) {
|
|
1010
|
+
return readAnswer(stdout, (out) => {
|
|
1011
|
+
const decision = field(out["hookSpecificOutput"], "permissionDecision");
|
|
1012
|
+
return decision === "deny" || decision === "ask" ? decision : "allow";
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
// src/hooks/runtimes/grok/payload.ts
|
|
1017
|
+
import { isAbsolute as isAbsolute5, resolve as resolve5 } from "path";
|
|
1018
|
+
function object5(value) {
|
|
1019
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1020
|
+
}
|
|
1021
|
+
var text5 = (value) => typeof value === "string" ? value : void 0;
|
|
1022
|
+
function claims(payload) {
|
|
1023
|
+
const raw = object5(payload);
|
|
1024
|
+
return typeof raw["sessionId"] === "string" && typeof raw["workspaceRoot"] === "string";
|
|
1025
|
+
}
|
|
1026
|
+
function normalize5(nativeEvent, payload, projectRoot) {
|
|
1027
|
+
const event = NEUTRAL_EVENTS5[nativeEvent];
|
|
1028
|
+
if (!event) return null;
|
|
1029
|
+
const raw = object5(payload);
|
|
1030
|
+
const cwd = text5(raw["cwd"]) ?? text5(raw["workspaceRoot"]) ?? projectRoot;
|
|
1031
|
+
const name = text5(raw["toolName"]) ?? text5(raw["tool_name"]);
|
|
1032
|
+
const input = object5(raw["toolInput"] ?? raw["tool_input"]);
|
|
1033
|
+
const kind = name ? toolKind5(name) : void 0;
|
|
1034
|
+
const path = text5(input["file_path"]);
|
|
1035
|
+
const paths = path ? [isAbsolute5(path) ? path : resolve5(cwd, path)] : [];
|
|
1036
|
+
const turnId = text5(raw["promptId"]);
|
|
1037
|
+
const prompt = text5(raw["prompt"]);
|
|
1038
|
+
const lastMessage = text5(raw["lastAssistantMessage"]);
|
|
1039
|
+
return {
|
|
1040
|
+
contract: 2,
|
|
1041
|
+
runtime: "grok",
|
|
1042
|
+
event,
|
|
1043
|
+
session: {
|
|
1044
|
+
id: text5(raw["sessionId"]) ?? text5(raw["session_id"]) ?? "",
|
|
1045
|
+
...turnId ? { turnId } : {}
|
|
1046
|
+
},
|
|
1047
|
+
cwd,
|
|
1048
|
+
projectRoot,
|
|
1049
|
+
...name && kind ? {
|
|
1050
|
+
tool: {
|
|
1051
|
+
kind,
|
|
1052
|
+
native: name,
|
|
1053
|
+
...kind === "shell" ? { command: text5(input["command"]) ?? "" } : {},
|
|
1054
|
+
paths: kind === "shell" ? [] : paths
|
|
1055
|
+
}
|
|
1056
|
+
} : {},
|
|
1057
|
+
...prompt !== void 0 && event === "prompt" ? { prompt } : {},
|
|
1058
|
+
...event === "stop" ? {
|
|
1059
|
+
stop: {
|
|
1060
|
+
active: raw["stopHookActive"] === true,
|
|
1061
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
1062
|
+
}
|
|
1063
|
+
} : {}
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// src/hooks/runtimes/grok/projection.ts
|
|
1068
|
+
var HOOKS_FILE3 = ".grok/hooks/pmcp.json";
|
|
1069
|
+
function command5(event) {
|
|
1070
|
+
return climbCommand(
|
|
1071
|
+
`node ${HOOK_PATH} grok ${event}`,
|
|
1072
|
+
void 0,
|
|
1073
|
+
'"${GROK_WORKSPACE_ROOT:-.}"'
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
function project5(plan) {
|
|
1077
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
1078
|
+
for (const binding of plan.bindings) {
|
|
1079
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
1080
|
+
entries.push({
|
|
1081
|
+
hooks: [
|
|
1082
|
+
{
|
|
1083
|
+
type: "command",
|
|
1084
|
+
command: command5(binding.event),
|
|
1085
|
+
timeout: binding.timeoutSec,
|
|
1086
|
+
statusMessage: hookLabel(binding.event)
|
|
1087
|
+
}
|
|
1088
|
+
]
|
|
1089
|
+
});
|
|
1090
|
+
byEvent.set(binding.native, entries);
|
|
1091
|
+
}
|
|
1092
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
1093
|
+
file: HOOKS_FILE3,
|
|
1094
|
+
key: ["hooks", native],
|
|
1095
|
+
entries
|
|
1096
|
+
}));
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
// src/hooks/runtimes/grok/synthetic.ts
|
|
1100
|
+
function synthetic5(options) {
|
|
1101
|
+
const { cwd, session, event, command: command6, path, tool } = options;
|
|
1102
|
+
const defaults = {
|
|
1103
|
+
shell: "run_terminal_command",
|
|
1104
|
+
edit: "write",
|
|
1105
|
+
read: "read_file",
|
|
1106
|
+
other: "unknown_tool"
|
|
1107
|
+
};
|
|
1108
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
1109
|
+
if (!name && command6 !== void 0) name = "run_terminal_command";
|
|
1110
|
+
if (!name && path !== void 0) name = "write";
|
|
1111
|
+
const input = name === "run_terminal_command" ? { command: command6 ?? "" } : { file_path: path ?? "", content: "" };
|
|
1112
|
+
return {
|
|
1113
|
+
hookEventName: event,
|
|
1114
|
+
sessionId: session,
|
|
1115
|
+
cwd,
|
|
1116
|
+
workspaceRoot: cwd,
|
|
1117
|
+
session_id: session,
|
|
1118
|
+
hook_event_name: event,
|
|
1119
|
+
...name ? { toolName: name, toolInput: input, tool_name: name, tool_input: input } : {},
|
|
1120
|
+
...event === "Stop" ? { stopHookActive: false, stop_hook_active: false } : {}
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
// src/hooks/runtimes/grok/adapter.ts
|
|
1125
|
+
var grok = {
|
|
1126
|
+
id: "grok",
|
|
1127
|
+
synthetic: synthetic5,
|
|
1128
|
+
capabilities: capabilities5,
|
|
1129
|
+
// No matcher: only grok's Claude aliases (Bash, Write) were measured, and dispatch filters.
|
|
1130
|
+
bindings(event, _tools, timeoutSec) {
|
|
1131
|
+
const native = NATIVE_EVENTS5[event];
|
|
1132
|
+
if (!native || capabilities5.events[event] === "unsupported") return [];
|
|
1133
|
+
return [{ event, native, timeoutSec }];
|
|
1134
|
+
},
|
|
1135
|
+
normalize: normalize5,
|
|
1136
|
+
render: render5,
|
|
1137
|
+
readVerdict: verdict5,
|
|
1138
|
+
project: project5,
|
|
1139
|
+
nativeFiles: [{ file: HOOKS_FILE3, root: "hooks" }],
|
|
1140
|
+
nativeEvent: (event) => NATIVE_EVENTS5[event],
|
|
1141
|
+
ridesOn: { runtime: "claude", claims },
|
|
1142
|
+
trust: "folder"
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
// src/hooks/runtimes/opencode/capabilities.ts
|
|
1146
|
+
var capabilities6 = {
|
|
1147
|
+
events: {
|
|
1148
|
+
session_start: "measured",
|
|
1149
|
+
prompt: "measured",
|
|
1150
|
+
before_tool: "measured",
|
|
1151
|
+
after_tool: "measured",
|
|
1152
|
+
stop: "measured",
|
|
1153
|
+
session_end: "unsupported",
|
|
1154
|
+
notification: "unsupported"
|
|
1155
|
+
},
|
|
1156
|
+
outputs: {
|
|
1157
|
+
deny: "measured",
|
|
1158
|
+
ask: "unsupported",
|
|
1159
|
+
context: "measured",
|
|
1160
|
+
feedback: "unsupported"
|
|
1161
|
+
},
|
|
1162
|
+
context: ["session_start", "prompt", "after_tool"]
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1165
|
+
// src/hooks/runtimes/opencode/events.ts
|
|
1166
|
+
var NATIVE_EVENTS6 = {
|
|
1167
|
+
session_start: "session.created",
|
|
1168
|
+
prompt: "chat.message",
|
|
1169
|
+
before_tool: "tool.execute.before",
|
|
1170
|
+
after_tool: "tool.execute.after",
|
|
1171
|
+
stop: "session.idle"
|
|
1172
|
+
};
|
|
1173
|
+
var NEUTRAL_EVENTS6 = Object.fromEntries(
|
|
1174
|
+
Object.entries(NATIVE_EVENTS6).map(([neutral, native]) => [native, neutral])
|
|
1175
|
+
);
|
|
1176
|
+
var TOOL_KINDS6 = {
|
|
1177
|
+
bash: "shell",
|
|
1178
|
+
write: "edit",
|
|
1179
|
+
edit: "edit",
|
|
1180
|
+
patch: "edit",
|
|
1181
|
+
read: "read",
|
|
1182
|
+
list: "read",
|
|
1183
|
+
glob: "read",
|
|
1184
|
+
grep: "read"
|
|
1185
|
+
};
|
|
1186
|
+
function toolKind6(name) {
|
|
1187
|
+
return TOOL_KINDS6[name] ?? "other";
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
// src/hooks/runtimes/opencode/output.ts
|
|
1191
|
+
var MAX_REASON6 = 2e3;
|
|
1192
|
+
var DELIVERS3 = [
|
|
1193
|
+
"session_start",
|
|
1194
|
+
"prompt",
|
|
1195
|
+
"after_tool"
|
|
1196
|
+
];
|
|
1197
|
+
function render6(input, decision) {
|
|
1198
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
1199
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON6);
|
|
1200
|
+
if (input.event === "before_tool") {
|
|
1201
|
+
const out2 = blocking ? { deny: reason } : {};
|
|
1202
|
+
return { stdout: `${JSON.stringify(out2)}
|
|
1203
|
+
`, exitCode: 0 };
|
|
1204
|
+
}
|
|
1205
|
+
const notes = [...decision.context ?? []];
|
|
1206
|
+
if (blocking) notes.unshift(reason);
|
|
1207
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
1208
|
+
const out = notes.length > 0 && DELIVERS3.includes(input.event) ? { context: notes.join("\n\n") } : {};
|
|
1209
|
+
return { stdout: `${JSON.stringify(out)}
|
|
1210
|
+
`, exitCode: 0 };
|
|
1211
|
+
}
|
|
1212
|
+
function verdict6(stdout) {
|
|
1213
|
+
return readAnswer(stdout, (out) => "deny" in out ? "deny" : "allow");
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
// src/hooks/runtimes/opencode/payload.ts
|
|
1217
|
+
import { isAbsolute as isAbsolute6, resolve as resolve6 } from "path";
|
|
1218
|
+
function object6(value) {
|
|
1219
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1220
|
+
}
|
|
1221
|
+
var text6 = (value) => typeof value === "string" ? value : void 0;
|
|
1222
|
+
function normalize6(nativeEvent, payload, projectRoot) {
|
|
1223
|
+
const event = NEUTRAL_EVENTS6[nativeEvent];
|
|
1224
|
+
if (!event) return null;
|
|
1225
|
+
const raw = object6(payload);
|
|
1226
|
+
const input = object6(raw["input"]);
|
|
1227
|
+
const properties = object6(raw["properties"]);
|
|
1228
|
+
const cwd = text6(raw["directory"]) ?? projectRoot;
|
|
1229
|
+
const name = text6(input["tool"]);
|
|
1230
|
+
const args = object6(raw["args"] ?? input["args"]);
|
|
1231
|
+
const kind = name ? toolKind6(name) : void 0;
|
|
1232
|
+
const path = text6(args["filePath"]);
|
|
1233
|
+
const paths = path ? [isAbsolute6(path) ? path : resolve6(cwd, path)] : [];
|
|
1234
|
+
const session = text6(input["sessionID"]) ?? text6(properties["sessionID"]) ?? "";
|
|
1235
|
+
return {
|
|
1236
|
+
contract: 2,
|
|
1237
|
+
runtime: "opencode",
|
|
1238
|
+
event,
|
|
1239
|
+
session: {
|
|
1240
|
+
id: session,
|
|
1241
|
+
...text6(raw["serverUrl"]) ? { wakeUrl: text6(raw["serverUrl"]) } : {}
|
|
1242
|
+
},
|
|
1243
|
+
cwd,
|
|
1244
|
+
projectRoot,
|
|
1245
|
+
...name && kind ? {
|
|
1246
|
+
tool: {
|
|
1247
|
+
kind,
|
|
1248
|
+
native: name,
|
|
1249
|
+
...kind === "shell" ? { command: text6(args["command"]) ?? "" } : {},
|
|
1250
|
+
paths: kind === "shell" ? [] : paths
|
|
1251
|
+
}
|
|
1252
|
+
} : {},
|
|
1253
|
+
...event === "stop" ? { stop: { active: false } } : {}
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
// src/hooks/runtimes/opencode/plugin.ts
|
|
1258
|
+
var PLUGIN_TEMPLATE = `// Generated by pmcp project from pmcp.toml [hooks]; edit pmcp.toml, not this file.
|
|
1259
|
+
import { spawn } from 'node:child_process';
|
|
1260
|
+
import { join } from 'node:path';
|
|
1261
|
+
import { fileURLToPath } from 'node:url';
|
|
1262
|
+
|
|
1263
|
+
const ROOT = fileURLToPath(new URL("../..", import.meta.url));
|
|
1264
|
+
const HOOK = join(ROOT, ".agents/pmcp-hooks/hook.mjs");
|
|
1265
|
+
const TIMEOUT_MS = __TIMEOUTS__;
|
|
1266
|
+
|
|
1267
|
+
// Resolves the hook's answer, or null when it could not run or answered nonsense.
|
|
1268
|
+
function ask(event, payload) {
|
|
1269
|
+
if (!(event in TIMEOUT_MS)) return Promise.resolve({});
|
|
1270
|
+
return new Promise((done) => {
|
|
1271
|
+
const child = spawn("node", [HOOK, "opencode", event], {
|
|
1272
|
+
cwd: ROOT,
|
|
1273
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
1274
|
+
});
|
|
1275
|
+
let out = "";
|
|
1276
|
+
const timer = setTimeout(() => child.kill(), TIMEOUT_MS[event]);
|
|
1277
|
+
child.stdout.on("data", (chunk) => {
|
|
1278
|
+
out += chunk;
|
|
1279
|
+
});
|
|
1280
|
+
child.on("error", () => {
|
|
1281
|
+
clearTimeout(timer);
|
|
1282
|
+
done(null);
|
|
1283
|
+
});
|
|
1284
|
+
child.on("close", (code) => {
|
|
1285
|
+
clearTimeout(timer);
|
|
1286
|
+
try {
|
|
1287
|
+
done(code === 0 ? JSON.parse(out || "{}") : null);
|
|
1288
|
+
} catch {
|
|
1289
|
+
done(null);
|
|
1290
|
+
}
|
|
1291
|
+
});
|
|
1292
|
+
child.stdin.end(JSON.stringify(payload));
|
|
1293
|
+
});
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
export const Pmcp = async ({ directory, serverUrl }) => {
|
|
1297
|
+
// The server this session runs on, so pmcp sessions send --wake can prompt it while idle.
|
|
1298
|
+
const server = serverUrl ? String(serverUrl) : undefined;
|
|
1299
|
+
// queued: notes not yet shown; showing: notes every model call of this turn carries. A turn
|
|
1300
|
+
// can make several model calls (opencode's first is a small title call), so a note shown
|
|
1301
|
+
// once is kept until the next prompt starts a new turn.
|
|
1302
|
+
const queued = new Map();
|
|
1303
|
+
const showing = new Map();
|
|
1304
|
+
const hold = (session, text) => {
|
|
1305
|
+
if (session && text) queued.set(session, [...(queued.get(session) ?? []), text]);
|
|
1306
|
+
};
|
|
1307
|
+
return {
|
|
1308
|
+
"tool.execute.before": async (input, output) => {
|
|
1309
|
+
const answer = await ask("before_tool", { input, args: output.args, directory });
|
|
1310
|
+
// A tool call goes ahead only on a clear answer, as with the other runtimes' guards.
|
|
1311
|
+
if (!answer) throw new Error("pmcp hook did not answer; the call was refused");
|
|
1312
|
+
if (answer.deny) throw new Error(answer.deny);
|
|
1313
|
+
},
|
|
1314
|
+
"tool.execute.after": async (input, output) => {
|
|
1315
|
+
const answer = await ask("after_tool", { input, directory });
|
|
1316
|
+
if (answer?.context) output.output = \`\${output.output ?? ""}\\n\\n\${answer.context}\`;
|
|
1317
|
+
},
|
|
1318
|
+
"chat.message": async (input) => {
|
|
1319
|
+
showing.delete(input.sessionID);
|
|
1320
|
+
const answer = await ask("prompt", { input, directory });
|
|
1321
|
+
hold(input.sessionID, answer?.context);
|
|
1322
|
+
},
|
|
1323
|
+
"experimental.chat.system.transform": async (input, output) => {
|
|
1324
|
+
const session = input.sessionID;
|
|
1325
|
+
const notes = [...(showing.get(session) ?? []), ...(queued.get(session) ?? [])];
|
|
1326
|
+
queued.delete(session);
|
|
1327
|
+
if (notes.length === 0) return;
|
|
1328
|
+
showing.set(session, notes);
|
|
1329
|
+
output.system.push(notes.join("\\n\\n"));
|
|
1330
|
+
},
|
|
1331
|
+
event: async ({ event }) => {
|
|
1332
|
+
if (event.type === "session.created") {
|
|
1333
|
+
const answer = await ask("session_start", { ...event, directory, serverUrl: server });
|
|
1334
|
+
hold(event.properties?.sessionID, answer?.context);
|
|
1335
|
+
}
|
|
1336
|
+
if (event.type === "session.idle") await ask("stop", { ...event, directory });
|
|
1337
|
+
},
|
|
1338
|
+
};
|
|
1339
|
+
};
|
|
1340
|
+
`;
|
|
1341
|
+
|
|
1342
|
+
// src/hooks/runtimes/opencode/projection.ts
|
|
1343
|
+
var PLUGIN_FILE = ".opencode/plugins/pmcp.js";
|
|
1344
|
+
function files(plan) {
|
|
1345
|
+
if (plan.bindings.length === 0) return [];
|
|
1346
|
+
const timeouts = Object.fromEntries(
|
|
1347
|
+
plan.bindings.map((binding) => [binding.event, binding.timeoutSec * 1e3])
|
|
1348
|
+
);
|
|
1349
|
+
return [
|
|
1350
|
+
{
|
|
1351
|
+
path: PLUGIN_FILE,
|
|
1352
|
+
content: PLUGIN_TEMPLATE.replace(
|
|
1353
|
+
"__TIMEOUTS__",
|
|
1354
|
+
JSON.stringify(timeouts)
|
|
1355
|
+
)
|
|
1356
|
+
}
|
|
1357
|
+
];
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
// src/hooks/runtimes/opencode/synthetic.ts
|
|
1361
|
+
function synthetic6(options) {
|
|
1362
|
+
const { cwd, session, event, command: command6, path, tool } = options;
|
|
1363
|
+
const defaults = {
|
|
1364
|
+
shell: "bash",
|
|
1365
|
+
edit: "write",
|
|
1366
|
+
read: "read",
|
|
1367
|
+
other: "unknown_tool"
|
|
1368
|
+
};
|
|
1369
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
1370
|
+
if (!name && command6 !== void 0) name = "bash";
|
|
1371
|
+
if (!name && path !== void 0) name = "write";
|
|
1372
|
+
const args = name === "bash" ? { command: command6 ?? "" } : { filePath: path ?? "", content: "" };
|
|
1373
|
+
if (event === "session.created" || event === "session.idle")
|
|
1374
|
+
return { type: event, properties: { sessionID: session }, directory: cwd };
|
|
1375
|
+
return {
|
|
1376
|
+
input: {
|
|
1377
|
+
sessionID: session,
|
|
1378
|
+
...name ? { tool: name, callID: "c1" } : {}
|
|
1379
|
+
},
|
|
1380
|
+
...name ? { args } : {},
|
|
1381
|
+
directory: cwd
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// src/hooks/runtimes/opencode/adapter.ts
|
|
1386
|
+
var opencode = {
|
|
1387
|
+
id: "opencode",
|
|
1388
|
+
synthetic: synthetic6,
|
|
1389
|
+
capabilities: capabilities6,
|
|
1390
|
+
bindings(event, _tools, timeoutSec) {
|
|
1391
|
+
const native = NATIVE_EVENTS6[event];
|
|
1392
|
+
if (!native || capabilities6.events[event] === "unsupported") return [];
|
|
1393
|
+
return [{ event, native, timeoutSec }];
|
|
1394
|
+
},
|
|
1395
|
+
normalize: normalize6,
|
|
1396
|
+
render: render6,
|
|
1397
|
+
readVerdict: verdict6,
|
|
1398
|
+
// opencode has no hook configuration to edit; the projection is the generated plugin file.
|
|
1399
|
+
project: () => [],
|
|
1400
|
+
files,
|
|
1401
|
+
nativeFiles: [],
|
|
1402
|
+
nativeEvent: (event) => NATIVE_EVENTS6[event],
|
|
1403
|
+
trust: "none"
|
|
1404
|
+
};
|
|
1405
|
+
|
|
1406
|
+
// src/hooks/runtimes/index.ts
|
|
1407
|
+
var RUNTIMES = {
|
|
1408
|
+
claude,
|
|
1409
|
+
codex,
|
|
1410
|
+
gemini,
|
|
1411
|
+
antigravity,
|
|
1412
|
+
grok,
|
|
1413
|
+
opencode
|
|
1414
|
+
};
|
|
1415
|
+
var NATIVE_HOOK_FILES = Object.values(
|
|
1416
|
+
RUNTIMES
|
|
1417
|
+
).flatMap((adapter) => adapter.nativeFiles);
|
|
1418
|
+
|
|
1419
|
+
// src/hooks/inspection/smoke.ts
|
|
1420
|
+
import { spawnSync } from "child_process";
|
|
1421
|
+
import { existsSync, mkdtempSync, readFileSync as readFileSync3, rmSync } from "fs";
|
|
1422
|
+
import { tmpdir } from "os";
|
|
1423
|
+
import { join as join3 } from "path";
|
|
1424
|
+
import { isDeepStrictEqual } from "util";
|
|
1425
|
+
|
|
1426
|
+
// src/hooks/compiled.ts
|
|
1427
|
+
import { createHash } from "crypto";
|
|
1428
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
1429
|
+
import { join as join2 } from "path";
|
|
1430
|
+
|
|
1431
|
+
// src/hooks/integrity-cache.ts
|
|
1432
|
+
import {
|
|
1433
|
+
mkdirSync,
|
|
1434
|
+
readFileSync,
|
|
1435
|
+
renameSync,
|
|
1436
|
+
statSync,
|
|
1437
|
+
writeFileSync
|
|
1438
|
+
} from "fs";
|
|
1439
|
+
import { homedir } from "os";
|
|
1440
|
+
import { dirname, join } from "path";
|
|
1441
|
+
|
|
1442
|
+
// src/hooks/compiled.ts
|
|
1443
|
+
var HOOK_DIRECTORY = ".agents/pmcp-hooks";
|
|
1444
|
+
var POLICY_FILE = `${HOOK_DIRECTORY}/policy.json`;
|
|
1445
|
+
var HOOK_FILE = `${HOOK_DIRECTORY}/hook.mjs`;
|
|
1446
|
+
var HOOK_BODY_FILE = `${HOOK_DIRECTORY}/hook-body.mjs`;
|
|
1447
|
+
var LOCK_FILE = "pmcp.lock";
|
|
1448
|
+
function sha256(text7) {
|
|
1449
|
+
return createHash("sha256").update(text7).digest("hex");
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
// src/hooks/inspection/smoke.ts
|
|
1453
|
+
var VERDICTS = ["allow", "deny", "ask"];
|
|
1454
|
+
var CASE_TIMEOUT_MS = 15e3;
|
|
1455
|
+
var SESSION = "pmcp-hooks-smoke";
|
|
1456
|
+
var REFUSING = /* @__PURE__ */ new Set(["deny", "ask", "graded"]);
|
|
1457
|
+
function readTestTable(value) {
|
|
1458
|
+
if (value === void 0) return void 0;
|
|
1459
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
1460
|
+
throw new Error("[hooks.test] must be a table");
|
|
1461
|
+
const unknown = Object.keys(value).find((key) => key !== "fixtures");
|
|
1462
|
+
if (unknown) throw new Error(`unknown [hooks.test] key ${unknown}`);
|
|
1463
|
+
const fixtures = value["fixtures"];
|
|
1464
|
+
if (fixtures === void 0) return void 0;
|
|
1465
|
+
if (typeof fixtures !== "string" || fixtures === "")
|
|
1466
|
+
throw new Error("[hooks.test] fixtures must be a path");
|
|
1467
|
+
return fixtures;
|
|
1468
|
+
}
|
|
1469
|
+
function readFixtures(text7) {
|
|
1470
|
+
const value = JSON.parse(text7);
|
|
1471
|
+
const fail = (message) => {
|
|
1472
|
+
throw new Error(`fixtures: ${message}`);
|
|
1473
|
+
};
|
|
1474
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
1475
|
+
fail("must be a JSON object with cases");
|
|
1476
|
+
const { cases, failure } = value;
|
|
1477
|
+
if (!Array.isArray(cases)) return fail("cases must be a list");
|
|
1478
|
+
for (const [index, item] of cases.entries()) {
|
|
1479
|
+
const where = `case ${index + 1}`;
|
|
1480
|
+
if (!item || typeof item !== "object") fail(`${where} must be an object`);
|
|
1481
|
+
const entry = item;
|
|
1482
|
+
for (const key of ["name", "guard", "command"])
|
|
1483
|
+
if (typeof entry[key] !== "string" || entry[key] === "")
|
|
1484
|
+
fail(`${where} needs ${key}`);
|
|
1485
|
+
if (entry["cwd"] !== void 0 && entry["cwd"] !== "linked-worktree")
|
|
1486
|
+
fail(`${where} cwd can only be "linked-worktree"`);
|
|
1487
|
+
const want = entry["expect"];
|
|
1488
|
+
const ok = typeof want === "string" && VERDICTS.includes(want) || !!want && typeof want === "object" && !Array.isArray(want) && Object.entries(want).every(
|
|
1489
|
+
([runtime, verdict7]) => RUNTIME_IDS.includes(runtime) && typeof verdict7 === "string" && VERDICTS.includes(verdict7)
|
|
1490
|
+
);
|
|
1491
|
+
if (!ok)
|
|
1492
|
+
fail(
|
|
1493
|
+
`${where} expect must be allow, deny or ask, or a table of runtime to one of them`
|
|
1494
|
+
);
|
|
1495
|
+
}
|
|
1496
|
+
return {
|
|
1497
|
+
cases,
|
|
1498
|
+
...failure !== void 0 ? { failure } : {}
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1501
|
+
function expected(testCase, runtime) {
|
|
1502
|
+
return typeof testCase.expect === "string" ? testCase.expect : testCase.expect[runtime];
|
|
1503
|
+
}
|
|
1504
|
+
function verdictOf(runtime, stdout) {
|
|
1505
|
+
return RUNTIMES[runtime].readVerdict(stdout) ?? "unreadable";
|
|
1506
|
+
}
|
|
1507
|
+
function enforcedGuards(policy) {
|
|
1508
|
+
const builtIn = policy.guards.filter(
|
|
1509
|
+
(guard) => /^(?:shell|git)\./u.test(guard.id) && REFUSING.has(guard.mode)
|
|
1510
|
+
).map((guard) => guard.id);
|
|
1511
|
+
const custom = policy.custom.filter(
|
|
1512
|
+
(entry) => entry.event === "before_shell" && REFUSING.has(entry.mode ?? "deny")
|
|
1513
|
+
).map((entry) => `custom.${entry.name}`);
|
|
1514
|
+
return [...builtIn, ...custom].sort();
|
|
1515
|
+
}
|
|
1516
|
+
function coverageGaps(cases, guards, runtimes) {
|
|
1517
|
+
const gaps = [];
|
|
1518
|
+
for (const guard of guards)
|
|
1519
|
+
for (const runtime of runtimes) {
|
|
1520
|
+
const mine = cases.filter((c) => c.guard === guard).map((c) => expected(c, runtime)).filter((verdict7) => verdict7 !== void 0);
|
|
1521
|
+
if (!mine.some((verdict7) => verdict7 !== "allow"))
|
|
1522
|
+
gaps.push(`${guard} (${runtime}): no refusing case`);
|
|
1523
|
+
if (!mine.some((verdict7) => verdict7 === "allow"))
|
|
1524
|
+
gaps.push(`${guard} (${runtime}): no passing case`);
|
|
1525
|
+
}
|
|
1526
|
+
return gaps;
|
|
1527
|
+
}
|
|
1528
|
+
function isolatedEnv(env, stateHome) {
|
|
1529
|
+
const clean = {};
|
|
1530
|
+
for (const [key, value] of Object.entries(env))
|
|
1531
|
+
if (value !== void 0 && !key.startsWith("GIT_")) clean[key] = value;
|
|
1532
|
+
return {
|
|
1533
|
+
...clean,
|
|
1534
|
+
XDG_STATE_HOME: stateHome,
|
|
1535
|
+
PMCP_HOOK_COMPILE_CACHE: "off"
|
|
1536
|
+
};
|
|
1537
|
+
}
|
|
1538
|
+
function wiredRuntimes(root, policy, fail) {
|
|
1539
|
+
const wired = [];
|
|
1540
|
+
for (const id of policy.targets) {
|
|
1541
|
+
const adapter = RUNTIMES[id];
|
|
1542
|
+
if (adapter.capabilities.events.before_tool === "unsupported") continue;
|
|
1543
|
+
const host = adapter.ridesOn?.runtime;
|
|
1544
|
+
const files2 = (host && policy.targets.includes(host) ? RUNTIMES[host] : adapter).nativeFiles.map((native) => join3(root, native.file));
|
|
1545
|
+
const entry = (file) => existsSync(file) && readFileSync3(file, "utf8").includes(`${HOOK_DIRECTORY}/hook.mjs`);
|
|
1546
|
+
if (files2.length > 0 && !files2.some(entry)) {
|
|
1547
|
+
fail(
|
|
1548
|
+
id,
|
|
1549
|
+
"a [hooks] target whose native hook entry is missing; run pmcp project"
|
|
1550
|
+
);
|
|
1551
|
+
continue;
|
|
1552
|
+
}
|
|
1553
|
+
wired.push(id);
|
|
1554
|
+
}
|
|
1555
|
+
return wired;
|
|
1556
|
+
}
|
|
1557
|
+
function linkedWorktree(base, env) {
|
|
1558
|
+
const repo = join3(base, "repo");
|
|
1559
|
+
const git = (cwd, ...args) => {
|
|
1560
|
+
const run = spawnSync("git", ["-c", "core.hooksPath=/dev/null", ...args], {
|
|
1561
|
+
cwd,
|
|
1562
|
+
env,
|
|
1563
|
+
encoding: "utf8"
|
|
1564
|
+
});
|
|
1565
|
+
if (run.status !== 0)
|
|
1566
|
+
throw new Error(`git ${args.join(" ")}: ${run.stderr.trim()}`);
|
|
1567
|
+
};
|
|
1568
|
+
git(base, "init", "-q", "-b", "main", repo);
|
|
1569
|
+
git(
|
|
1570
|
+
repo,
|
|
1571
|
+
"-c",
|
|
1572
|
+
"user.name=smoke",
|
|
1573
|
+
"-c",
|
|
1574
|
+
"user.email=smoke@example.invalid",
|
|
1575
|
+
"commit",
|
|
1576
|
+
"-q",
|
|
1577
|
+
"--allow-empty",
|
|
1578
|
+
"-m",
|
|
1579
|
+
"init"
|
|
1580
|
+
);
|
|
1581
|
+
git(repo, "worktree", "add", "-q", join3(base, "wt"), "-b", "held");
|
|
1582
|
+
return repo;
|
|
1583
|
+
}
|
|
1584
|
+
function runCase(root, runtime, command6, cwd, env) {
|
|
1585
|
+
const adapter = RUNTIMES[runtime];
|
|
1586
|
+
const payload = adapter.synthetic({
|
|
1587
|
+
cwd,
|
|
1588
|
+
session: SESSION,
|
|
1589
|
+
event: adapter.nativeEvent("before_tool") ?? "",
|
|
1590
|
+
command: command6,
|
|
1591
|
+
tool: "shell"
|
|
1592
|
+
});
|
|
1593
|
+
const run = spawnSync(
|
|
1594
|
+
process.execPath,
|
|
1595
|
+
[join3(root, HOOK_FILE), runtime, "before_tool"],
|
|
1596
|
+
{
|
|
1597
|
+
cwd: root,
|
|
1598
|
+
input: JSON.stringify(payload),
|
|
1599
|
+
encoding: "utf8",
|
|
1600
|
+
timeout: CASE_TIMEOUT_MS,
|
|
1601
|
+
env
|
|
1602
|
+
}
|
|
1603
|
+
);
|
|
1604
|
+
if (run.error) return { problem: run.error.message };
|
|
1605
|
+
if (run.status !== 0)
|
|
1606
|
+
return { problem: `exit ${run.status}; a hook always exits 0` };
|
|
1607
|
+
return { got: verdictOf(runtime, run.stdout) };
|
|
1608
|
+
}
|
|
1609
|
+
function smoke(root, fixtures, env = process.env) {
|
|
1610
|
+
const findings = [];
|
|
1611
|
+
const fail = (where, message) => findings.push({ where, message });
|
|
1612
|
+
for (const file of [HOOK_FILE, POLICY_FILE])
|
|
1613
|
+
if (!existsSync(join3(root, file))) fail(file, "missing; run pmcp project");
|
|
1614
|
+
if (findings.length > 0)
|
|
1615
|
+
return { runtimes: [], guards: [], cases: 0, findings };
|
|
1616
|
+
const policy = JSON.parse(
|
|
1617
|
+
readFileSync3(join3(root, POLICY_FILE), "utf8")
|
|
1618
|
+
);
|
|
1619
|
+
const runtimes = wiredRuntimes(root, policy, fail);
|
|
1620
|
+
const guards = enforcedGuards(policy);
|
|
1621
|
+
if (fixtures.failure !== void 0 && !isDeepStrictEqual(policy.failure, fixtures.failure))
|
|
1622
|
+
fail(
|
|
1623
|
+
"[hooks.failure]",
|
|
1624
|
+
`expected ${JSON.stringify(fixtures.failure)}, projected ${JSON.stringify(policy.failure ?? null)}`
|
|
1625
|
+
);
|
|
1626
|
+
for (const gap of coverageGaps(fixtures.cases, guards, runtimes))
|
|
1627
|
+
fail("coverage", gap);
|
|
1628
|
+
const scratch = mkdtempSync(join3(tmpdir(), "pmcp-hooks-smoke-"));
|
|
1629
|
+
const childEnv = isolatedEnv(env, join3(scratch, "state"));
|
|
1630
|
+
let executed = 0;
|
|
1631
|
+
try {
|
|
1632
|
+
let worktree;
|
|
1633
|
+
for (const testCase of fixtures.cases) {
|
|
1634
|
+
const cwd = testCase.cwd === "linked-worktree" ? worktree ??= linkedWorktree(scratch, childEnv) : root;
|
|
1635
|
+
for (const runtime of runtimes) {
|
|
1636
|
+
const want = expected(testCase, runtime);
|
|
1637
|
+
if (!want) continue;
|
|
1638
|
+
executed += 1;
|
|
1639
|
+
const where = `${runtime} :: ${testCase.name}`;
|
|
1640
|
+
const result = runCase(root, runtime, testCase.command, cwd, childEnv);
|
|
1641
|
+
if ("problem" in result) fail(where, result.problem);
|
|
1642
|
+
else if (result.got !== want)
|
|
1643
|
+
fail(where, `expected ${want}, got ${result.got}`);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
} finally {
|
|
1647
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
1648
|
+
}
|
|
1649
|
+
if (runtimes.length === 0)
|
|
1650
|
+
fail("runtimes", "no projected runtime answers a shell before_tool");
|
|
1651
|
+
if (executed === 0) fail("cases", "0 cases executed");
|
|
1652
|
+
return { runtimes, guards, cases: executed, findings };
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
export {
|
|
1656
|
+
RUNTIME_IDS,
|
|
1657
|
+
DEFAULT_RUNTIME_IDS,
|
|
1658
|
+
NEUTRAL_EVENTS,
|
|
1659
|
+
climbCommand,
|
|
1660
|
+
RUNTIMES,
|
|
1661
|
+
NATIVE_HOOK_FILES,
|
|
1662
|
+
HOOK_DIRECTORY,
|
|
1663
|
+
POLICY_FILE,
|
|
1664
|
+
HOOK_FILE,
|
|
1665
|
+
HOOK_BODY_FILE,
|
|
1666
|
+
LOCK_FILE,
|
|
1667
|
+
sha256,
|
|
1668
|
+
readTestTable,
|
|
1669
|
+
readFixtures,
|
|
1670
|
+
smoke
|
|
1671
|
+
};
|
|
1672
|
+
//# sourceMappingURL=chunk-5YCKFU5Q.js.map
|