@modootoday/pmcp 0.17.3 → 0.18.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/CHANGELOG.md +60 -0
- package/README.md +29 -24
- package/dist/apply-4IGTXOCQ.js +242 -0
- package/dist/{attachment-tU5FZrgZ.d.ts → attachment-D84OGI6n.d.ts} +1 -1
- package/dist/available-DBSIVVJ6.js +73 -0
- package/dist/broadcast-3RY3OZGY.js +18 -0
- package/dist/chunk-25GHX674.js +1246 -0
- package/dist/chunk-2P4Z2NUX.js +28 -0
- package/dist/chunk-34DECPVC.js +22 -0
- package/dist/chunk-3NYG4SGU.js +45 -0
- package/dist/chunk-3PW4FI7U.js +32 -0
- package/dist/chunk-3Y3HDMHA.js +108 -0
- package/dist/chunk-47O3X4VS.js +113 -0
- package/dist/chunk-4NDEP7HU.js +26 -0
- package/dist/chunk-4VCC2NXE.js +104 -0
- package/dist/chunk-5OMOPUE6.js +39 -0
- package/dist/chunk-5QQWS4US.js +53 -0
- package/dist/chunk-5ZABTVSW.js +48 -0
- package/dist/chunk-65ZHM5RH.js +4985 -0
- package/dist/chunk-665TA55Z.js +419 -0
- package/dist/chunk-6STKGOCW.js +27 -0
- package/dist/chunk-6WXMJOMI.js +9 -0
- package/dist/chunk-73AKO4UL.js +24 -0
- package/dist/chunk-7BLPN3W6.js +31 -0
- package/dist/chunk-7DLZC73N.js +29 -0
- package/dist/chunk-7GBYECSK.js +72 -0
- package/dist/chunk-7TD53M7L.js +13 -0
- package/dist/chunk-A4PKLESO.js +112 -0
- package/dist/chunk-ABTJQ4QV.js +74 -0
- package/dist/chunk-BBTBJCFA.js +380 -0
- package/dist/chunk-BU65MNVU.js +13 -0
- package/dist/chunk-BVCTKPT3.js +83 -0
- package/dist/chunk-C4NHQGHL.js +142 -0
- package/dist/chunk-CJTKR6K6.js +1213 -0
- package/dist/chunk-DCU2EXQV.js +78 -0
- package/dist/chunk-DZ2MRTYT.js +135 -0
- package/dist/chunk-E2YOZ2FF.js +207 -0
- package/dist/chunk-E3DQB5LS.js +48 -0
- package/dist/chunk-E7OOHLQR.js +7 -0
- package/dist/chunk-EATCUWGG.js +66 -0
- package/dist/chunk-EIDIVIAD.js +33 -0
- package/dist/chunk-EJBZGIDK.js +1934 -0
- package/dist/chunk-ELAGZ635.js +252 -0
- package/dist/chunk-GZHHEHKY.js +44 -0
- package/dist/chunk-HDMBVLCZ.js +53 -0
- package/dist/chunk-HQOB7OQS.js +81 -0
- package/dist/chunk-IGYRMM62.js +28 -0
- package/dist/chunk-IJBSOWHK.js +86 -0
- package/dist/chunk-INT7QLEU.js +114 -0
- package/dist/chunk-J6QLLQVL.js +1025 -0
- package/dist/chunk-J7UYNJCR.js +9 -0
- package/dist/chunk-JEL35P7S.js +53 -0
- package/dist/chunk-JGJ4INEN.js +32 -0
- package/dist/chunk-JS5RTH4I.js +84 -0
- package/dist/chunk-JUXNIXN4.js +42 -0
- package/dist/chunk-JWRZDYB5.js +59 -0
- package/dist/chunk-KMXRRSKT.js +660 -0
- package/dist/chunk-LAZQBMAU.js +67 -0
- package/dist/chunk-LLBPVZXM.js +67 -0
- package/dist/chunk-LLT5FQBE.js +57 -0
- package/dist/chunk-LMT3PPXJ.js +17 -0
- package/dist/chunk-MM2Y43LC.js +87 -0
- package/dist/chunk-MPSQXSC6.js +346 -0
- package/dist/chunk-MVFH26QT.js +97 -0
- package/dist/chunk-MWFOUBFC.js +39 -0
- package/dist/chunk-N5AQ3PP7.js +211 -0
- package/dist/chunk-NQPURJT5.js +44 -0
- package/dist/chunk-NRBQK6UX.js +105 -0
- package/dist/chunk-NZS5CD72.js +333 -0
- package/dist/chunk-OEH47H6A.js +48 -0
- package/dist/chunk-OG7QR2DP.js +75 -0
- package/dist/chunk-OPYFEHKG.js +52 -0
- package/dist/chunk-P4N7DBD5.js +83 -0
- package/dist/chunk-PAX3QXX2.js +140 -0
- package/dist/chunk-PLX3BOOS.js +396 -0
- package/dist/chunk-PMJG2CXF.js +122 -0
- package/dist/chunk-PWSJJXLQ.js +643 -0
- package/dist/chunk-Q5H3L6Y4.js +23 -0
- package/dist/chunk-Q65R3XKX.js +155 -0
- package/dist/chunk-QEW4J3IZ.js +74 -0
- package/dist/chunk-QPL7E2HW.js +464 -0
- package/dist/chunk-R2GPCZUX.js +22 -0
- package/dist/chunk-R4L6LJJ2.js +778 -0
- package/dist/chunk-R5MT42JS.js +284 -0
- package/dist/chunk-RK4PYD7L.js +73 -0
- package/dist/chunk-RMATE45Z.js +197 -0
- package/dist/chunk-S3PIAMXV.js +230 -0
- package/dist/chunk-SG5TPMKH.js +93 -0
- package/dist/chunk-T3LKX23M.js +168 -0
- package/dist/chunk-T6GOWHK6.js +30 -0
- package/dist/chunk-TIA4SPU2.js +10 -0
- package/dist/chunk-TNCZ6444.js +57 -0
- package/dist/chunk-TVL5RTE6.js +269 -0
- package/dist/chunk-UOCLRM7P.js +151 -0
- package/dist/chunk-UPQTAHUK.js +189 -0
- package/dist/chunk-V4NPDK6X.js +14 -0
- package/dist/chunk-VJFEN6DJ.js +38 -0
- package/dist/chunk-WIW772I3.js +25 -0
- package/dist/chunk-XELAZAIM.js +136 -0
- package/dist/chunk-XNML2KIC.js +23 -0
- package/dist/chunk-XP2U3LO3.js +30 -0
- package/dist/chunk-YGOWIJPE.js +51 -0
- package/dist/chunk-YIXDHQYE.js +11 -0
- package/dist/chunk-YMHUM7AV.js +22 -0
- package/dist/chunk-YSAWTGNA.js +117 -0
- package/dist/chunk-Z4HRRXRF.js +478 -0
- package/dist/chunk-ZH6R642Z.js +102 -0
- package/dist/claude-U2JC2DNQ.js +77 -0
- package/dist/cli-VYCQJZ5X.js +41 -0
- package/dist/cli-ZTCBSDW4.js +46 -0
- package/dist/cli.js +144 -16160
- package/dist/completion-E7RRG3ZT.js +142 -0
- package/dist/config-DnpLL2kT.d.ts +46 -0
- package/dist/{service-qpCsOXkQ.d.ts → core-C1LPjxK6.d.ts} +2 -6
- package/dist/diagnostics-XXH4FLUG.js +93 -0
- package/dist/doctor-ZIOESU3N.js +538 -0
- package/dist/entry-ZNFCTDVE.js +305 -0
- package/dist/exec-mcp-X6372BLE.js +365 -0
- package/dist/harness-F2XFDYXF.js +177 -0
- package/dist/harness-egress-bridge.js +4 -4
- package/dist/harness-launch-gate.js +47 -940
- package/dist/harness-pane-runner.js +13 -80
- package/dist/harness-worker.js +21 -2703
- package/dist/hook-body.js +5221 -0
- package/dist/hook-spec-C5fWTSCH.d.ts +100 -0
- package/dist/hook.js +26 -0
- package/dist/hooks-FYE2L5RW.js +21 -0
- package/dist/host-GRY3VAHH.js +22 -0
- package/dist/host-X6BR6X4O.js +200 -0
- package/dist/host-run-GCA6H6SR.js +307 -0
- package/dist/http.d.ts +5 -42
- package/dist/http.js +21 -2234
- package/dist/import-mcp-2TTBPVLY.js +400 -0
- package/dist/index.d.ts +146 -168
- package/dist/index.js +133 -4911
- package/dist/init-627KFXTG.js +14 -0
- package/dist/install-GG7HHZK2.js +796 -0
- package/dist/invoke-LW22Z6ET.js +23 -0
- package/dist/list-MCP2FXUF.js +68 -0
- package/dist/list-NGMSQ5SH.js +65 -0
- package/dist/load-BEUBABtB.d.ts +21 -0
- package/dist/logout-6CSHUWWE.js +44 -0
- package/dist/logs-SPB6AANY.js +107 -0
- package/dist/mailbox/index.d.ts +10 -5
- package/dist/mailbox/index.js +53 -1572
- package/dist/mailbox-7BV54Q4R.js +285 -0
- package/dist/mailbox-GDJ7BWQN.js +189 -0
- package/dist/mainline-JQTTYJMI.js +639 -0
- package/dist/marketplace-G4VQET5W.js +191 -0
- package/dist/mcp-2DKVU3SW.js +15 -0
- package/dist/mcp-V23HC3LB.js +193 -0
- package/dist/models-ISU4DCMA.js +32 -0
- package/dist/opencode-CN7JLQYO.js +60 -0
- package/dist/pause-FFAYRBWI.js +18 -0
- package/dist/preview-DT7DWG5F.js +91 -0
- package/dist/project/hook-adapter.d.ts +6 -3
- package/dist/project/hook-adapter.js +66 -35
- package/dist/project-FY62G55N.js +70 -0
- package/dist/pty-XTJ4MIWK.js +94 -0
- package/dist/recovery-U6QB3PMT.js +100 -0
- package/dist/reindex-Y27QJFDB.js +22 -0
- package/dist/resume-UU4HZIUP.js +18 -0
- package/dist/run-HALGEJNJ.js +19 -0
- package/dist/run-OGA3P7FC.js +126 -0
- package/dist/serve-NBKT5BKY.js +104 -0
- package/dist/session-XM2CR425.js +159 -0
- package/dist/setup-DTKZLZUT.js +18 -0
- package/dist/start-login-UIQYQBMC.js +416 -0
- package/dist/status-A2OUTNGK.js +9 -0
- package/dist/status-H2VAHIDV.js +10 -0
- package/dist/status-NOOHW5WA.js +43 -0
- package/dist/status-VHODZPXB.js +55 -0
- package/dist/status-XQANBVXW.js +286 -0
- package/dist/stdio.d.ts +3 -13
- package/dist/stdio.js +16 -3186
- package/dist/sweep-64JDCGK6.js +18 -0
- package/dist/test-MKAXFJOL.js +561 -0
- package/dist/tools-gpHNq9Dh.d.ts +328 -0
- package/dist/tui-MWKAXE4K.js +153 -0
- package/dist/unbroadcast-KPUEFLZD.js +18 -0
- package/dist/validate-5GZ5YWNA.js +46 -0
- package/dist/worker-RQ575EJL.js +80 -0
- package/dist/worktree-create-PZSRB7PH.js +89 -0
- package/dist/worktree-list-JYB6VXLW.js +51 -0
- package/dist/worktree-sync-UF5CXPWV.js +30 -0
- package/docs/hooks/capabilities/antigravity.json +199 -0
- package/docs/hooks/capabilities/claude.json +179 -0
- package/docs/hooks/capabilities/codex.json +179 -0
- package/docs/hooks/capabilities/gemini.json +196 -0
- package/docs/hooks/capabilities/grok.json +192 -0
- package/docs/hooks/capabilities/opencode.json +184 -0
- package/docs/project-configuration.md +844 -4
- package/docs/terminal-harness.md +71 -28
- package/docs/terminal-workspace.md +91 -170
- package/gemini-extension.json +1 -1
- package/package.json +13 -1
- package/plugin.json +1 -1
- package/plugins/pmcp/.claude-plugin/plugin.json +4 -2
- package/plugins/pmcp/gemini-extension.json +1 -1
- package/plugins/pmcp/mod/commands.ts +72 -0
- package/plugins/pmcp/mod/contract.d.ts +39 -0
- package/plugins/pmcp/mod/draw.tsx +216 -0
- package/plugins/pmcp/mod/guard.ts +42 -0
- package/plugins/pmcp/mod/hooks.json +4 -0
- package/plugins/pmcp/mod/host.ts +46 -0
- package/plugins/pmcp/mod/model.ts +144 -0
- package/plugins/pmcp/mod/reader/index.ts +18 -0
- package/plugins/pmcp/mod/reader/read.ts +76 -0
- package/plugins/pmcp/mod/reader/settings.ts +54 -0
- package/plugins/pmcp/mod/reader/types.ts +38 -0
- package/plugins/pmcp/mod/register.tsx +73 -0
- package/plugins/pmcp/mod/sessions/format.ts +91 -0
- package/plugins/pmcp/mod/sessions/types.ts +162 -0
- package/plugins/pmcp/mod/snapshot.ts +75 -0
- package/plugins/pmcp/mod/spinner.tsx +30 -0
- package/plugins/pmcp/mod/state.ts +28 -0
- package/plugins/pmcp/plugin.json +1 -1
- package/plugins/pmcp/skills/agent-assets-migrate/SKILL.md +96 -0
- package/plugins/pmcp/skills/headless-agents-ci-scaffold/SKILL.md +100 -0
- package/plugins/pmcp/skills/marketplace-plugin-scaffold/SKILL.md +87 -0
- package/plugins/pmcp/skills/package-skill-catalog/SKILL.md +51 -11
- package/plugins/pmcp/skills/pmcp-project-init/SKILL.md +145 -0
- package/plugins/pmcp/skills/pmcp-sessions-and-git/SKILL.md +104 -0
- package/plugins/pmcp/skills/skill-package-scaffold/SKILL.md +88 -0
- package/schema/catalog-v1.json +158 -0
- package/schema/library.json +164 -0
- package/schema/marketplace-manifest.json +130 -0
- package/schema/plugin-manifest.json +55 -0
- package/schema/skill-frontmatter.json +144 -0
- package/schema/skill-metadata-core.json +110 -0
- package/skills/agent-assets-migrate/SKILL.md +96 -0
- package/skills/headless-agents-ci-scaffold/SKILL.md +100 -0
- package/skills/marketplace-plugin-scaffold/SKILL.md +87 -0
- package/skills/package-skill-catalog/SKILL.md +51 -11
- package/skills/pmcp-project-init/SKILL.md +145 -0
- package/skills/pmcp-sessions-and-git/SKILL.md +104 -0
- package/skills/skill-package-scaffold/SKILL.md +88 -0
- package/dist/config-BkSpjUG6.d.ts +0 -50
- package/dist/embed-DRROSD3F.d.ts +0 -53
- package/dist/hook-spec-PRSnYGLV.d.ts +0 -10
- package/dist/server-VWSY3Shw.d.ts +0 -543
- package/dist/tui-worker.js +0 -6549
- /package/dist/{tui-worker.d.ts → hook.d.ts} +0 -0
|
@@ -0,0 +1,4985 @@
|
|
|
1
|
+
import {
|
|
2
|
+
readLoggingTable
|
|
3
|
+
} from "./chunk-HQOB7OQS.js";
|
|
4
|
+
import {
|
|
5
|
+
readGitConfig
|
|
6
|
+
} from "./chunk-S3PIAMXV.js";
|
|
7
|
+
import {
|
|
8
|
+
detect
|
|
9
|
+
} from "./chunk-JS5RTH4I.js";
|
|
10
|
+
import {
|
|
11
|
+
syncWorktree
|
|
12
|
+
} from "./chunk-DCU2EXQV.js";
|
|
13
|
+
import {
|
|
14
|
+
gitText
|
|
15
|
+
} from "./chunk-OPYFEHKG.js";
|
|
16
|
+
import {
|
|
17
|
+
localCli,
|
|
18
|
+
pmcpCommand,
|
|
19
|
+
recordedCli
|
|
20
|
+
} from "./chunk-EATCUWGG.js";
|
|
21
|
+
import {
|
|
22
|
+
ProjectError
|
|
23
|
+
} from "./chunk-NRBQK6UX.js";
|
|
24
|
+
import {
|
|
25
|
+
ptyFields,
|
|
26
|
+
ptyRef
|
|
27
|
+
} from "./chunk-3PW4FI7U.js";
|
|
28
|
+
import {
|
|
29
|
+
readStoreSettings
|
|
30
|
+
} from "./chunk-SG5TPMKH.js";
|
|
31
|
+
import {
|
|
32
|
+
inboxKey,
|
|
33
|
+
noticeOf
|
|
34
|
+
} from "./chunk-Z4HRRXRF.js";
|
|
35
|
+
import {
|
|
36
|
+
STATE_FILES,
|
|
37
|
+
leaseFile,
|
|
38
|
+
runtimePid
|
|
39
|
+
} from "./chunk-C4NHQGHL.js";
|
|
40
|
+
import {
|
|
41
|
+
safeEndpoint
|
|
42
|
+
} from "./chunk-Q5H3L6Y4.js";
|
|
43
|
+
import {
|
|
44
|
+
TOOLS
|
|
45
|
+
} from "./chunk-V4NPDK6X.js";
|
|
46
|
+
import {
|
|
47
|
+
packagedBinary,
|
|
48
|
+
readHost,
|
|
49
|
+
readLock
|
|
50
|
+
} from "./chunk-665TA55Z.js";
|
|
51
|
+
import {
|
|
52
|
+
checkoutOf,
|
|
53
|
+
hasLinkedWorktrees
|
|
54
|
+
} from "./chunk-IJBSOWHK.js";
|
|
55
|
+
import {
|
|
56
|
+
ConfigError,
|
|
57
|
+
readMarketplaces
|
|
58
|
+
} from "./chunk-QPL7E2HW.js";
|
|
59
|
+
|
|
60
|
+
// src/hooks/contract.ts
|
|
61
|
+
var RUNTIME_IDS = [
|
|
62
|
+
"claude",
|
|
63
|
+
"codex",
|
|
64
|
+
"gemini",
|
|
65
|
+
"antigravity",
|
|
66
|
+
"grok",
|
|
67
|
+
"opencode"
|
|
68
|
+
];
|
|
69
|
+
var DEFAULT_RUNTIME_IDS = ["claude", "codex"];
|
|
70
|
+
var NEUTRAL_EVENTS = [
|
|
71
|
+
"session_start",
|
|
72
|
+
"prompt",
|
|
73
|
+
"before_tool",
|
|
74
|
+
"after_tool",
|
|
75
|
+
"stop",
|
|
76
|
+
"session_end",
|
|
77
|
+
"notification"
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
// src/hooks/runtimes/antigravity/capabilities.ts
|
|
81
|
+
var capabilities = {
|
|
82
|
+
events: {
|
|
83
|
+
session_start: "measured",
|
|
84
|
+
prompt: "measured",
|
|
85
|
+
before_tool: "measured",
|
|
86
|
+
after_tool: "measured",
|
|
87
|
+
stop: "measured",
|
|
88
|
+
session_end: "unsupported",
|
|
89
|
+
notification: "unsupported"
|
|
90
|
+
},
|
|
91
|
+
outputs: {
|
|
92
|
+
deny: "measured",
|
|
93
|
+
ask: "unsupported",
|
|
94
|
+
context: "measured",
|
|
95
|
+
feedback: "unqualified"
|
|
96
|
+
},
|
|
97
|
+
context: ["session_start", "prompt", "stop"]
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/hooks/runtimes/antigravity/events.ts
|
|
101
|
+
var NATIVE_EVENTS = {
|
|
102
|
+
session_start: "PreInvocation",
|
|
103
|
+
prompt: "PreInvocation",
|
|
104
|
+
before_tool: "PreToolUse",
|
|
105
|
+
after_tool: "PostToolUse",
|
|
106
|
+
stop: "Stop"
|
|
107
|
+
};
|
|
108
|
+
var TOOL_KINDS = {
|
|
109
|
+
run_command: "shell",
|
|
110
|
+
write_to_file: "edit",
|
|
111
|
+
replace_file_content: "edit",
|
|
112
|
+
multi_replace_file_content: "edit",
|
|
113
|
+
view_file: "read",
|
|
114
|
+
list_dir: "read",
|
|
115
|
+
grep_search: "read"
|
|
116
|
+
};
|
|
117
|
+
function toolKind(name) {
|
|
118
|
+
return TOOL_KINDS[name] ?? "other";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/hooks/runtimes/antigravity/output.ts
|
|
122
|
+
var MAX_REASON = 2e3;
|
|
123
|
+
function silent(event) {
|
|
124
|
+
return event === "before_tool" ? '{"decision":"allow"}\n' : "{}\n";
|
|
125
|
+
}
|
|
126
|
+
function render(input, decision) {
|
|
127
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
128
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON);
|
|
129
|
+
if (input.event === "before_tool") {
|
|
130
|
+
const out2 = blocking ? { decision: "deny", reason } : { decision: "allow" };
|
|
131
|
+
return { stdout: `${JSON.stringify(out2)}
|
|
132
|
+
`, exitCode: 0 };
|
|
133
|
+
}
|
|
134
|
+
const notes = [...decision.context ?? []];
|
|
135
|
+
if (blocking) notes.unshift(reason);
|
|
136
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
137
|
+
let out = {};
|
|
138
|
+
if (notes.length > 0 && input.event !== "after_tool") {
|
|
139
|
+
if (input.event === "stop" && !input.stop?.active)
|
|
140
|
+
out = { decision: "continue", reason: notes.join("\n\n") };
|
|
141
|
+
if (input.event === "session_start" || input.event === "prompt")
|
|
142
|
+
out = { injectSteps: [{ ephemeralMessage: notes.join("\n\n") }] };
|
|
143
|
+
}
|
|
144
|
+
return { stdout: `${JSON.stringify(out)}
|
|
145
|
+
`, exitCode: 0 };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/hooks/runtimes/antigravity/payload.ts
|
|
149
|
+
import { isAbsolute, resolve } from "path";
|
|
150
|
+
function object(value) {
|
|
151
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
152
|
+
}
|
|
153
|
+
var text = (value) => typeof value === "string" ? value : void 0;
|
|
154
|
+
var count = (value) => typeof value === "number" ? value : 0;
|
|
155
|
+
function invocationEvent(raw) {
|
|
156
|
+
if (count(raw["invocationNum"]) !== 0) return null;
|
|
157
|
+
return count(raw["initialNumSteps"]) <= 1 ? "session_start" : "prompt";
|
|
158
|
+
}
|
|
159
|
+
var NEUTRAL = {
|
|
160
|
+
PreToolUse: "before_tool",
|
|
161
|
+
PostToolUse: "after_tool",
|
|
162
|
+
Stop: "stop"
|
|
163
|
+
};
|
|
164
|
+
function normalize(nativeEvent, payload, projectRoot) {
|
|
165
|
+
const raw = object(payload);
|
|
166
|
+
const event = nativeEvent === "PreInvocation" ? invocationEvent(raw) : NEUTRAL[nativeEvent] ?? null;
|
|
167
|
+
if (!event) return null;
|
|
168
|
+
const call = object(raw["toolCall"]);
|
|
169
|
+
const args = object(call["args"]);
|
|
170
|
+
const workspaces = Array.isArray(raw["workspacePaths"]) ? raw["workspacePaths"] : [];
|
|
171
|
+
const cwd = text(args["Cwd"]) ?? text(workspaces[0]) ?? projectRoot;
|
|
172
|
+
const name = text(call["name"]);
|
|
173
|
+
const kind = name ? toolKind(name) : void 0;
|
|
174
|
+
const target = text(args["TargetFile"]);
|
|
175
|
+
const paths = target ? [isAbsolute(target) ? target : resolve(cwd, target)] : [];
|
|
176
|
+
return {
|
|
177
|
+
contract: 2,
|
|
178
|
+
runtime: "antigravity",
|
|
179
|
+
event,
|
|
180
|
+
session: { id: text(raw["conversationId"]) ?? "" },
|
|
181
|
+
cwd,
|
|
182
|
+
projectRoot,
|
|
183
|
+
...name && kind ? {
|
|
184
|
+
tool: {
|
|
185
|
+
kind,
|
|
186
|
+
native: name,
|
|
187
|
+
...kind === "shell" ? { command: text(args["CommandLine"]) ?? "" } : {},
|
|
188
|
+
paths: kind === "shell" ? [] : paths
|
|
189
|
+
}
|
|
190
|
+
} : {},
|
|
191
|
+
...event === "stop" ? { stop: { active: count(raw["executionNum"]) > 0 } } : {}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/hooks/runtimes/climb.ts
|
|
196
|
+
function climbCommand(run3, missing, start) {
|
|
197
|
+
const skip = `echo "pmcp: no pmcp.toml at or above $(pwd -P); hook skipped" >&2`;
|
|
198
|
+
const from = start ? `cd ${start} 2>/dev/null; ` : "";
|
|
199
|
+
return `${from}cd "$(pwd -P)" && while [ ! -f pmcp.toml ] && [ "$(pwd)" != / ]; do cd ..; done; if [ -f pmcp.toml ]; then exec ${run3}; fi; ${skip}` + (missing ? `; ${missing}` : "");
|
|
200
|
+
}
|
|
201
|
+
var HOOK_PATH = ".agents/pmcp-hooks/hook.mjs";
|
|
202
|
+
|
|
203
|
+
// src/hooks/runtimes/antigravity/projection.ts
|
|
204
|
+
var HOOKS_FILE = ".agents/hooks.json";
|
|
205
|
+
var HOOK_SET = "pmcp";
|
|
206
|
+
var TOOL_EVENTS = /* @__PURE__ */ new Set(["PreToolUse", "PostToolUse"]);
|
|
207
|
+
function command(event) {
|
|
208
|
+
const allow = event === "before_tool" ? `printf '%s\\n' '{"decision":"allow"}'` : void 0;
|
|
209
|
+
return climbCommand(`node ${HOOK_PATH} antigravity ${event}`, allow);
|
|
210
|
+
}
|
|
211
|
+
function project(plan) {
|
|
212
|
+
const byNative = /* @__PURE__ */ new Map();
|
|
213
|
+
for (const binding of plan.bindings) {
|
|
214
|
+
if (byNative.has(binding.native)) continue;
|
|
215
|
+
const event = binding.native === "PreInvocation" ? "prompt" : binding.event;
|
|
216
|
+
const hook = {
|
|
217
|
+
type: "command",
|
|
218
|
+
command: command(event),
|
|
219
|
+
timeout: binding.timeoutSec
|
|
220
|
+
};
|
|
221
|
+
const entry = TOOL_EVENTS.has(binding.native) ? { matcher: "*", hooks: [hook] } : hook;
|
|
222
|
+
byNative.set(binding.native, entry);
|
|
223
|
+
}
|
|
224
|
+
return [...byNative].map(([native, entry]) => ({
|
|
225
|
+
file: HOOKS_FILE,
|
|
226
|
+
key: [HOOK_SET, native],
|
|
227
|
+
entries: [entry]
|
|
228
|
+
}));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/hooks/runtimes/antigravity/synthetic.ts
|
|
232
|
+
function synthetic(options2) {
|
|
233
|
+
const { cwd, session, event, command: command6, path, tool } = options2;
|
|
234
|
+
const defaults = {
|
|
235
|
+
shell: "run_command",
|
|
236
|
+
edit: "write_to_file",
|
|
237
|
+
read: "view_file",
|
|
238
|
+
other: "unknown_tool"
|
|
239
|
+
};
|
|
240
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
241
|
+
if (!name && command6 !== void 0) name = "run_command";
|
|
242
|
+
if (!name && path !== void 0) name = "write_to_file";
|
|
243
|
+
const args = name === "run_command" ? { CommandLine: command6 ?? "", Cwd: cwd } : { TargetFile: path ?? "", CodeContent: "" };
|
|
244
|
+
return {
|
|
245
|
+
conversationId: session,
|
|
246
|
+
workspacePaths: [cwd],
|
|
247
|
+
...event === "PreInvocation" ? { invocationNum: 0, initialNumSteps: 1 } : {},
|
|
248
|
+
...event === "Stop" ? { executionNum: 0 } : {},
|
|
249
|
+
...name ? { toolCall: { name, args } } : {}
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/hooks/runtimes/antigravity/adapter.ts
|
|
254
|
+
var antigravity = {
|
|
255
|
+
id: "antigravity",
|
|
256
|
+
synthetic,
|
|
257
|
+
capabilities,
|
|
258
|
+
bindings(event, _tools, timeoutSec) {
|
|
259
|
+
const native = NATIVE_EVENTS[event];
|
|
260
|
+
if (!native || capabilities.events[event] === "unsupported") return [];
|
|
261
|
+
return [{ event, native, timeoutSec }];
|
|
262
|
+
},
|
|
263
|
+
normalize,
|
|
264
|
+
render,
|
|
265
|
+
silent,
|
|
266
|
+
project,
|
|
267
|
+
nativeFiles: [{ file: HOOKS_FILE, root: HOOK_SET }],
|
|
268
|
+
nativeEvent: (event) => NATIVE_EVENTS[event],
|
|
269
|
+
trust: "none"
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// src/hooks/runtimes/claude/capabilities.ts
|
|
273
|
+
var capabilities2 = {
|
|
274
|
+
events: {
|
|
275
|
+
session_start: "measured",
|
|
276
|
+
prompt: "measured",
|
|
277
|
+
before_tool: "measured",
|
|
278
|
+
after_tool: "measured",
|
|
279
|
+
stop: "measured",
|
|
280
|
+
session_end: "measured",
|
|
281
|
+
notification: "unqualified"
|
|
282
|
+
},
|
|
283
|
+
outputs: {
|
|
284
|
+
deny: "measured",
|
|
285
|
+
ask: "unqualified",
|
|
286
|
+
context: "measured",
|
|
287
|
+
feedback: "unqualified"
|
|
288
|
+
},
|
|
289
|
+
context: ["session_start", "prompt", "before_tool", "after_tool", "stop"]
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// src/hooks/runtimes/claude/events.ts
|
|
293
|
+
var NATIVE_EVENTS2 = {
|
|
294
|
+
session_start: "SessionStart",
|
|
295
|
+
prompt: "UserPromptSubmit",
|
|
296
|
+
before_tool: "PreToolUse",
|
|
297
|
+
after_tool: "PostToolUse",
|
|
298
|
+
stop: "Stop",
|
|
299
|
+
session_end: "SessionEnd",
|
|
300
|
+
notification: "Notification"
|
|
301
|
+
};
|
|
302
|
+
var NEUTRAL_EVENTS2 = Object.fromEntries(
|
|
303
|
+
Object.entries(NATIVE_EVENTS2).map(([neutral, native]) => [native, neutral])
|
|
304
|
+
);
|
|
305
|
+
var TOOL_KINDS2 = {
|
|
306
|
+
Bash: "shell",
|
|
307
|
+
Edit: "edit",
|
|
308
|
+
Write: "edit",
|
|
309
|
+
MultiEdit: "edit",
|
|
310
|
+
NotebookEdit: "edit",
|
|
311
|
+
Read: "read",
|
|
312
|
+
Grep: "read",
|
|
313
|
+
Glob: "read"
|
|
314
|
+
};
|
|
315
|
+
function toolKind2(name) {
|
|
316
|
+
return TOOL_KINDS2[name] ?? "other";
|
|
317
|
+
}
|
|
318
|
+
function matcherFor(tools) {
|
|
319
|
+
if (tools.includes("other")) return void 0;
|
|
320
|
+
const names2 = Object.entries(TOOL_KINDS2).filter(([, kind]) => tools.includes(kind)).map(([name]) => name);
|
|
321
|
+
return names2.length > 0 ? names2.join("|") : void 0;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// src/hooks/runtimes/claude/output.ts
|
|
325
|
+
var MAX_REASON2 = 2e3;
|
|
326
|
+
function render2(input, decision) {
|
|
327
|
+
const hookEventName = NATIVE_EVENTS2[input.event];
|
|
328
|
+
const context = decision.context?.length ? decision.context.join("\n\n") : void 0;
|
|
329
|
+
const reason = decision.reason?.slice(0, MAX_REASON2);
|
|
330
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
331
|
+
const out = shape(input, decision, {
|
|
332
|
+
hookEventName,
|
|
333
|
+
context,
|
|
334
|
+
reason,
|
|
335
|
+
blocking
|
|
336
|
+
});
|
|
337
|
+
return { stdout: out ? `${JSON.stringify(out)}
|
|
338
|
+
` : "", exitCode: 0 };
|
|
339
|
+
}
|
|
340
|
+
function shape(input, decision, parts) {
|
|
341
|
+
const { hookEventName, context, reason, blocking } = parts;
|
|
342
|
+
const contextOnly = context ? { hookSpecificOutput: { hookEventName, additionalContext: context } } : {};
|
|
343
|
+
if (input.event === "before_tool") {
|
|
344
|
+
if (!blocking && !context) return null;
|
|
345
|
+
let refusal = {};
|
|
346
|
+
if (blocking) {
|
|
347
|
+
refusal = {
|
|
348
|
+
permissionDecision: decision.verdict,
|
|
349
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
return {
|
|
353
|
+
hookSpecificOutput: {
|
|
354
|
+
hookEventName,
|
|
355
|
+
...refusal,
|
|
356
|
+
...context ? { additionalContext: context } : {}
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (input.event === "prompt" || input.event === "after_tool" || input.event === "stop") {
|
|
361
|
+
const feedback = blocking ? reason ?? "Refused" : decision.feedback;
|
|
362
|
+
if (!feedback && !context) return null;
|
|
363
|
+
return {
|
|
364
|
+
...feedback ? { decision: "block", reason: feedback } : {},
|
|
365
|
+
...contextOnly
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
if (input.event === "session_start" && context) return contextOnly;
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/hooks/runtimes/claude/payload.ts
|
|
373
|
+
import { isAbsolute as isAbsolute2, resolve as resolve2 } from "path";
|
|
374
|
+
function object2(value) {
|
|
375
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
376
|
+
}
|
|
377
|
+
var text2 = (value) => typeof value === "string" ? value : void 0;
|
|
378
|
+
function normalize2(nativeEvent, payload, projectRoot) {
|
|
379
|
+
const event = NEUTRAL_EVENTS2[nativeEvent];
|
|
380
|
+
const raw = object2(payload);
|
|
381
|
+
if (!event) return null;
|
|
382
|
+
const cwd = text2(raw["cwd"]) ?? projectRoot;
|
|
383
|
+
const name = text2(raw["tool_name"]);
|
|
384
|
+
const toolInput = object2(raw["tool_input"]);
|
|
385
|
+
const kind = name ? toolKind2(name) : void 0;
|
|
386
|
+
const paths = [
|
|
387
|
+
toolInput["file_path"],
|
|
388
|
+
toolInput["notebook_path"],
|
|
389
|
+
kind === "read" ? toolInput["path"] : void 0
|
|
390
|
+
].filter((value) => typeof value === "string" && !!value).map((path) => isAbsolute2(path) ? path : resolve2(cwd, path));
|
|
391
|
+
const agentType = text2(raw["agent_type"]);
|
|
392
|
+
const turnId = text2(raw["prompt_id"]);
|
|
393
|
+
const lastMessage = text2(raw["last_assistant_message"]);
|
|
394
|
+
const prompt = text2(raw["prompt"]);
|
|
395
|
+
return {
|
|
396
|
+
contract: 2,
|
|
397
|
+
runtime: "claude",
|
|
398
|
+
event,
|
|
399
|
+
session: {
|
|
400
|
+
id: text2(raw["session_id"]) ?? "",
|
|
401
|
+
...agentType ? { agentType } : {},
|
|
402
|
+
...turnId ? { turnId } : {}
|
|
403
|
+
},
|
|
404
|
+
cwd,
|
|
405
|
+
projectRoot,
|
|
406
|
+
...toolFields(name, kind, toolInput, paths),
|
|
407
|
+
...prompt !== void 0 ? { prompt } : {},
|
|
408
|
+
...stopFields(event, raw, lastMessage),
|
|
409
|
+
...notificationFields(event, raw)
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function notificationFields(event, raw) {
|
|
413
|
+
if (event !== "notification") return {};
|
|
414
|
+
const type = text2(raw["notification_type"]);
|
|
415
|
+
const message = text2(raw["message"]);
|
|
416
|
+
return {
|
|
417
|
+
notification: {
|
|
418
|
+
...type ? { type } : {},
|
|
419
|
+
...message ? { message } : {}
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function toolFields(name, kind, input, paths) {
|
|
424
|
+
if (!name || !kind) return {};
|
|
425
|
+
return {
|
|
426
|
+
tool: {
|
|
427
|
+
kind,
|
|
428
|
+
native: name,
|
|
429
|
+
...kind === "shell" ? { command: text2(input["command"]) ?? "" } : {},
|
|
430
|
+
paths
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
function stopFields(event, raw, lastMessage) {
|
|
435
|
+
if (event !== "stop") return {};
|
|
436
|
+
return {
|
|
437
|
+
stop: {
|
|
438
|
+
active: raw["stop_hook_active"] === true,
|
|
439
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/hooks/runtimes/label.ts
|
|
445
|
+
function hookLabel(event) {
|
|
446
|
+
return `pmcp ${event.replaceAll("_", " ")}`;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/hooks/runtimes/claude/projection.ts
|
|
450
|
+
var SETTINGS_FILE = ".claude/settings.json";
|
|
451
|
+
function command2(event) {
|
|
452
|
+
return `node "$CLAUDE_PROJECT_DIR/.agents/pmcp-hooks/hook.mjs" claude ${event}`;
|
|
453
|
+
}
|
|
454
|
+
function project2(plan) {
|
|
455
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
456
|
+
for (const binding of plan.bindings) {
|
|
457
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
458
|
+
entries.push({
|
|
459
|
+
...binding.matcher ? { matcher: binding.matcher } : {},
|
|
460
|
+
hooks: [
|
|
461
|
+
{
|
|
462
|
+
type: "command",
|
|
463
|
+
command: command2(binding.event),
|
|
464
|
+
timeout: binding.timeoutSec,
|
|
465
|
+
statusMessage: hookLabel(binding.event)
|
|
466
|
+
}
|
|
467
|
+
]
|
|
468
|
+
});
|
|
469
|
+
byEvent.set(binding.native, entries);
|
|
470
|
+
}
|
|
471
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
472
|
+
file: SETTINGS_FILE,
|
|
473
|
+
key: ["hooks", native],
|
|
474
|
+
entries
|
|
475
|
+
}));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// src/hooks/runtimes/claude/synthetic.ts
|
|
479
|
+
function synthetic2(options2) {
|
|
480
|
+
const { cwd, session, event, command: command6, path, tool } = options2;
|
|
481
|
+
const defaults = {
|
|
482
|
+
shell: "Bash",
|
|
483
|
+
edit: "Edit",
|
|
484
|
+
read: "Read",
|
|
485
|
+
other: "Unknown"
|
|
486
|
+
};
|
|
487
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
488
|
+
if (!name && command6 !== void 0) name = "Bash";
|
|
489
|
+
if (!name && path !== void 0) name = "Edit";
|
|
490
|
+
return {
|
|
491
|
+
session_id: session,
|
|
492
|
+
cwd,
|
|
493
|
+
hook_event_name: event,
|
|
494
|
+
prompt_id: "pmcp-hooks-test-turn",
|
|
495
|
+
...name ? { tool_name: name, tool_input: { command: command6, file_path: path } } : {},
|
|
496
|
+
...event === "Stop" ? { stop_hook_active: false } : {}
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// src/hooks/runtimes/claude/adapter.ts
|
|
501
|
+
var TOOL_EVENTS2 = /* @__PURE__ */ new Set(["before_tool", "after_tool"]);
|
|
502
|
+
var claude = {
|
|
503
|
+
id: "claude",
|
|
504
|
+
synthetic: synthetic2,
|
|
505
|
+
capabilities: capabilities2,
|
|
506
|
+
bindings(event, tools, timeoutSec) {
|
|
507
|
+
if (capabilities2.events[event] === "unsupported") return [];
|
|
508
|
+
const matcher = TOOL_EVENTS2.has(event) ? matcherFor(tools) : void 0;
|
|
509
|
+
return [
|
|
510
|
+
{
|
|
511
|
+
event,
|
|
512
|
+
native: NATIVE_EVENTS2[event],
|
|
513
|
+
...matcher ? { matcher } : {},
|
|
514
|
+
timeoutSec
|
|
515
|
+
}
|
|
516
|
+
];
|
|
517
|
+
},
|
|
518
|
+
normalize: normalize2,
|
|
519
|
+
render: render2,
|
|
520
|
+
project: project2,
|
|
521
|
+
nativeFiles: [{ file: SETTINGS_FILE, root: "hooks" }],
|
|
522
|
+
nativeEvent: (event) => NATIVE_EVENTS2[event],
|
|
523
|
+
trust: "none"
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
// src/hooks/runtimes/codex/capabilities.ts
|
|
527
|
+
var capabilities3 = {
|
|
528
|
+
events: {
|
|
529
|
+
session_start: "measured",
|
|
530
|
+
prompt: "measured",
|
|
531
|
+
before_tool: "measured",
|
|
532
|
+
after_tool: "measured",
|
|
533
|
+
stop: "measured",
|
|
534
|
+
session_end: "measured",
|
|
535
|
+
notification: "unsupported"
|
|
536
|
+
},
|
|
537
|
+
outputs: {
|
|
538
|
+
deny: "measured",
|
|
539
|
+
ask: "unsupported",
|
|
540
|
+
context: "measured",
|
|
541
|
+
feedback: "unqualified"
|
|
542
|
+
},
|
|
543
|
+
context: ["session_start", "prompt", "before_tool", "after_tool"]
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// src/hooks/runtimes/codex/events.ts
|
|
547
|
+
var NATIVE_EVENTS3 = {
|
|
548
|
+
session_start: "SessionStart",
|
|
549
|
+
prompt: "UserPromptSubmit",
|
|
550
|
+
before_tool: "PreToolUse",
|
|
551
|
+
after_tool: "PostToolUse",
|
|
552
|
+
stop: "Stop",
|
|
553
|
+
session_end: "SessionEnd"
|
|
554
|
+
};
|
|
555
|
+
var NEUTRAL_EVENTS3 = Object.fromEntries(
|
|
556
|
+
Object.entries(NATIVE_EVENTS3).map(([neutral, native]) => [native, neutral])
|
|
557
|
+
);
|
|
558
|
+
var TOOL_KINDS3 = {
|
|
559
|
+
Bash: "shell",
|
|
560
|
+
exec_command: "shell",
|
|
561
|
+
shell: "shell",
|
|
562
|
+
shell_command: "shell",
|
|
563
|
+
apply_patch: "edit"
|
|
564
|
+
};
|
|
565
|
+
function toolKind3(name) {
|
|
566
|
+
return TOOL_KINDS3[name] ?? "other";
|
|
567
|
+
}
|
|
568
|
+
function matcherFor2(tools) {
|
|
569
|
+
if (tools.includes("other")) return void 0;
|
|
570
|
+
const names2 = Object.entries(TOOL_KINDS3).filter(([, kind]) => tools.includes(kind)).map(([name]) => name);
|
|
571
|
+
return names2.length > 0 ? names2.join("|") : void 0;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// src/hooks/runtimes/codex/output.ts
|
|
575
|
+
var MAX_REASON3 = 2e3;
|
|
576
|
+
function render3(input, decision) {
|
|
577
|
+
const hookEventName = NATIVE_EVENTS3[input.event];
|
|
578
|
+
const context = decision.context?.length ? decision.context.join("\n\n") : void 0;
|
|
579
|
+
const reason = decision.reason?.slice(0, MAX_REASON3);
|
|
580
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
581
|
+
const out = shape2(input, decision, {
|
|
582
|
+
hookEventName,
|
|
583
|
+
context,
|
|
584
|
+
reason,
|
|
585
|
+
blocking
|
|
586
|
+
});
|
|
587
|
+
if (!out && input.event === "stop") return { stdout: "{}\n", exitCode: 0 };
|
|
588
|
+
return { stdout: out ? `${JSON.stringify(out)}
|
|
589
|
+
` : "", exitCode: 0 };
|
|
590
|
+
}
|
|
591
|
+
function shape2(input, decision, parts) {
|
|
592
|
+
const { hookEventName, context, reason, blocking } = parts;
|
|
593
|
+
const contextOnly = context ? { hookSpecificOutput: { hookEventName, additionalContext: context } } : {};
|
|
594
|
+
if (input.event === "before_tool") {
|
|
595
|
+
if (!blocking && !context) return null;
|
|
596
|
+
let refusal = {};
|
|
597
|
+
if (blocking) {
|
|
598
|
+
refusal = {
|
|
599
|
+
permissionDecision: "deny",
|
|
600
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
return {
|
|
604
|
+
hookSpecificOutput: {
|
|
605
|
+
hookEventName,
|
|
606
|
+
...refusal,
|
|
607
|
+
...context ? { additionalContext: context } : {}
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
if (input.event === "prompt" || input.event === "after_tool" || input.event === "stop") {
|
|
612
|
+
const feedback = blocking ? reason ?? "Refused" : decision.feedback;
|
|
613
|
+
if (!feedback && !context) return null;
|
|
614
|
+
return {
|
|
615
|
+
...feedback ? { decision: "block", reason: feedback } : {},
|
|
616
|
+
...input.event === "stop" ? {} : contextOnly
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
if (input.event === "session_start" && context) return contextOnly;
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// src/hooks/runtimes/codex/payload.ts
|
|
624
|
+
import { isAbsolute as isAbsolute3, resolve as resolve3 } from "path";
|
|
625
|
+
var PATCH_HEADER = /^\*\*\* (?:Update File|Add File|Delete File|Move to): (.+)$/gmu;
|
|
626
|
+
function object3(value) {
|
|
627
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
628
|
+
}
|
|
629
|
+
var text3 = (value) => typeof value === "string" ? value : void 0;
|
|
630
|
+
function patchPaths(patch) {
|
|
631
|
+
return [...patch.matchAll(PATCH_HEADER)].map((match) => match[1].trim());
|
|
632
|
+
}
|
|
633
|
+
function normalize3(nativeEvent, payload, projectRoot) {
|
|
634
|
+
const event = NEUTRAL_EVENTS3[nativeEvent];
|
|
635
|
+
const raw = object3(payload);
|
|
636
|
+
if (!event) return null;
|
|
637
|
+
const cwd = text3(raw["cwd"]) ?? projectRoot;
|
|
638
|
+
const name = text3(raw["tool_name"]);
|
|
639
|
+
const toolInput = object3(raw["tool_input"]);
|
|
640
|
+
const kind = name ? toolKind3(name) : void 0;
|
|
641
|
+
const body = text3(toolInput["command"]) ?? "";
|
|
642
|
+
const paths = (kind === "edit" ? patchPaths(body) : []).filter(Boolean).map((path) => isAbsolute3(path) ? path : resolve3(cwd, path));
|
|
643
|
+
const turnId = text3(raw["turn_id"]);
|
|
644
|
+
const lastMessage = text3(raw["last_assistant_message"]);
|
|
645
|
+
const prompt = text3(raw["prompt"]);
|
|
646
|
+
return {
|
|
647
|
+
contract: 2,
|
|
648
|
+
runtime: "codex",
|
|
649
|
+
event,
|
|
650
|
+
session: {
|
|
651
|
+
id: text3(raw["session_id"]) ?? "",
|
|
652
|
+
...turnId ? { turnId } : {}
|
|
653
|
+
},
|
|
654
|
+
cwd,
|
|
655
|
+
projectRoot,
|
|
656
|
+
...toolFields2(name, kind, body, paths),
|
|
657
|
+
...prompt !== void 0 ? { prompt } : {},
|
|
658
|
+
...stopFields2(event, raw, lastMessage)
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function toolFields2(name, kind, body, paths) {
|
|
662
|
+
if (!name || !kind) return {};
|
|
663
|
+
return {
|
|
664
|
+
tool: {
|
|
665
|
+
kind,
|
|
666
|
+
native: name,
|
|
667
|
+
...kind === "shell" || kind === "edit" ? { command: body } : {},
|
|
668
|
+
paths
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function stopFields2(event, raw, lastMessage) {
|
|
673
|
+
if (event !== "stop") return {};
|
|
674
|
+
return {
|
|
675
|
+
stop: {
|
|
676
|
+
active: raw["stop_hook_active"] === true,
|
|
677
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// src/hooks/runtimes/codex/projection.ts
|
|
683
|
+
var HOOKS_FILE2 = ".codex/hooks.json";
|
|
684
|
+
function command3(event) {
|
|
685
|
+
return climbCommand(`node ${HOOK_PATH} codex ${event}`);
|
|
686
|
+
}
|
|
687
|
+
function project3(plan) {
|
|
688
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
689
|
+
for (const binding of plan.bindings) {
|
|
690
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
691
|
+
entries.push({
|
|
692
|
+
...binding.matcher ? { matcher: binding.matcher } : {},
|
|
693
|
+
hooks: [
|
|
694
|
+
{
|
|
695
|
+
type: "command",
|
|
696
|
+
command: command3(binding.event),
|
|
697
|
+
timeout: binding.timeoutSec,
|
|
698
|
+
statusMessage: hookLabel(binding.event)
|
|
699
|
+
}
|
|
700
|
+
]
|
|
701
|
+
});
|
|
702
|
+
byEvent.set(binding.native, entries);
|
|
703
|
+
}
|
|
704
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
705
|
+
file: HOOKS_FILE2,
|
|
706
|
+
key: ["hooks", native],
|
|
707
|
+
entries
|
|
708
|
+
}));
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// src/hooks/runtimes/codex/synthetic.ts
|
|
712
|
+
function synthetic3(options2) {
|
|
713
|
+
const { cwd, session, event, command: command6, path, tool } = options2;
|
|
714
|
+
const defaults = {
|
|
715
|
+
shell: "Bash",
|
|
716
|
+
edit: "apply_patch",
|
|
717
|
+
other: "Unknown"
|
|
718
|
+
};
|
|
719
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
720
|
+
if (!name && command6 !== void 0) name = "Bash";
|
|
721
|
+
if (!name && path !== void 0) name = "apply_patch";
|
|
722
|
+
const body = name === "apply_patch" && path !== void 0 ? "*** Begin Patch\n*** Update File: " + path + "\n*** End Patch" : command6;
|
|
723
|
+
return {
|
|
724
|
+
session_id: session,
|
|
725
|
+
cwd,
|
|
726
|
+
hook_event_name: event,
|
|
727
|
+
turn_id: "pmcp-hooks-test-turn",
|
|
728
|
+
...name ? { tool_name: name, tool_input: { command: body } } : {},
|
|
729
|
+
...event === "Stop" ? { stop_hook_active: false } : {}
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/hooks/runtimes/codex/adapter.ts
|
|
734
|
+
var TOOL_EVENTS3 = /* @__PURE__ */ new Set(["before_tool", "after_tool"]);
|
|
735
|
+
var SESSION_END_TIMEOUT_SEC = 3;
|
|
736
|
+
var codex = {
|
|
737
|
+
id: "codex",
|
|
738
|
+
synthetic: synthetic3,
|
|
739
|
+
capabilities: capabilities3,
|
|
740
|
+
bindings(event, tools, timeoutSec) {
|
|
741
|
+
const native = NATIVE_EVENTS3[event];
|
|
742
|
+
if (!native || capabilities3.events[event] === "unsupported") return [];
|
|
743
|
+
const matcher = TOOL_EVENTS3.has(event) ? matcherFor2(tools) : void 0;
|
|
744
|
+
const timeout = event === "session_end" ? Math.min(timeoutSec, SESSION_END_TIMEOUT_SEC) : timeoutSec;
|
|
745
|
+
return [
|
|
746
|
+
{ event, native, ...matcher ? { matcher } : {}, timeoutSec: timeout }
|
|
747
|
+
];
|
|
748
|
+
},
|
|
749
|
+
normalize: normalize3,
|
|
750
|
+
render: render3,
|
|
751
|
+
project: project3,
|
|
752
|
+
nativeFiles: [{ file: HOOKS_FILE2, root: "hooks" }],
|
|
753
|
+
nativeEvent: (event) => NATIVE_EVENTS3[event],
|
|
754
|
+
trust: "hash_review"
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
// src/hooks/runtimes/gemini/capabilities.ts
|
|
758
|
+
var capabilities4 = {
|
|
759
|
+
events: {
|
|
760
|
+
session_start: "measured",
|
|
761
|
+
prompt: "measured",
|
|
762
|
+
before_tool: "measured",
|
|
763
|
+
after_tool: "measured",
|
|
764
|
+
stop: "measured",
|
|
765
|
+
session_end: "measured",
|
|
766
|
+
notification: "unqualified"
|
|
767
|
+
},
|
|
768
|
+
outputs: {
|
|
769
|
+
deny: "measured",
|
|
770
|
+
ask: "unsupported",
|
|
771
|
+
context: "measured",
|
|
772
|
+
feedback: "unqualified"
|
|
773
|
+
},
|
|
774
|
+
context: ["session_start", "prompt", "after_tool"]
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
// src/hooks/runtimes/gemini/events.ts
|
|
778
|
+
var NATIVE_EVENTS4 = {
|
|
779
|
+
session_start: "SessionStart",
|
|
780
|
+
prompt: "BeforeAgent",
|
|
781
|
+
before_tool: "BeforeTool",
|
|
782
|
+
after_tool: "AfterTool",
|
|
783
|
+
stop: "AfterAgent",
|
|
784
|
+
session_end: "SessionEnd",
|
|
785
|
+
notification: "Notification"
|
|
786
|
+
};
|
|
787
|
+
var NEUTRAL_EVENTS4 = Object.fromEntries(
|
|
788
|
+
Object.entries(NATIVE_EVENTS4).map(([neutral, native]) => [native, neutral])
|
|
789
|
+
);
|
|
790
|
+
var TOOL_KINDS4 = {
|
|
791
|
+
run_shell_command: "shell",
|
|
792
|
+
write_file: "edit",
|
|
793
|
+
replace: "edit",
|
|
794
|
+
read_file: "read",
|
|
795
|
+
read_many_files: "read",
|
|
796
|
+
glob: "read",
|
|
797
|
+
search_file_content: "read"
|
|
798
|
+
};
|
|
799
|
+
function toolKind4(name) {
|
|
800
|
+
return TOOL_KINDS4[name] ?? "other";
|
|
801
|
+
}
|
|
802
|
+
function matcherFor3(tools) {
|
|
803
|
+
if (tools.includes("other")) return void 0;
|
|
804
|
+
const names2 = Object.entries(TOOL_KINDS4).filter(([, kind]) => tools.includes(kind)).map(([name]) => name);
|
|
805
|
+
return names2.length > 0 ? `^(${names2.join("|")})$` : void 0;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// src/hooks/runtimes/gemini/output.ts
|
|
809
|
+
var MAX_REASON4 = 2e3;
|
|
810
|
+
var DELIVERS = [
|
|
811
|
+
"session_start",
|
|
812
|
+
"prompt",
|
|
813
|
+
"after_tool"
|
|
814
|
+
];
|
|
815
|
+
function render4(input, decision) {
|
|
816
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
817
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON4);
|
|
818
|
+
const out = {};
|
|
819
|
+
const notes = [...decision.context ?? []];
|
|
820
|
+
if (blocking && input.event === "before_tool")
|
|
821
|
+
Object.assign(out, { decision: "deny", reason });
|
|
822
|
+
else if (blocking) notes.unshift(reason);
|
|
823
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
824
|
+
if (notes.length > 0 && DELIVERS.includes(input.event))
|
|
825
|
+
out["hookSpecificOutput"] = {
|
|
826
|
+
hookEventName: NATIVE_EVENTS4[input.event],
|
|
827
|
+
additionalContext: notes.join("\n\n")
|
|
828
|
+
};
|
|
829
|
+
return { stdout: `${JSON.stringify(out)}
|
|
830
|
+
`, exitCode: 0 };
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// src/hooks/runtimes/gemini/payload.ts
|
|
834
|
+
import { isAbsolute as isAbsolute4, resolve as resolve4 } from "path";
|
|
835
|
+
function object4(value) {
|
|
836
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
837
|
+
}
|
|
838
|
+
var text4 = (value) => typeof value === "string" ? value : void 0;
|
|
839
|
+
function normalize4(nativeEvent, payload, projectRoot) {
|
|
840
|
+
const event = NEUTRAL_EVENTS4[nativeEvent];
|
|
841
|
+
if (!event) return null;
|
|
842
|
+
const raw = object4(payload);
|
|
843
|
+
const cwd = text4(raw["cwd"]) ?? projectRoot;
|
|
844
|
+
const name = text4(raw["tool_name"]);
|
|
845
|
+
const input = object4(raw["tool_input"]);
|
|
846
|
+
const kind = name ? toolKind4(name) : void 0;
|
|
847
|
+
const paths = [input["file_path"], input["absolute_path"], input["path"]].filter((value) => typeof value === "string" && !!value).map((path) => isAbsolute4(path) ? path : resolve4(cwd, path));
|
|
848
|
+
const prompt = text4(raw["prompt"]);
|
|
849
|
+
const lastMessage = text4(raw["prompt_response"]);
|
|
850
|
+
return {
|
|
851
|
+
contract: 2,
|
|
852
|
+
runtime: "gemini",
|
|
853
|
+
event,
|
|
854
|
+
session: { id: text4(raw["session_id"]) ?? "" },
|
|
855
|
+
cwd,
|
|
856
|
+
projectRoot,
|
|
857
|
+
...name && kind ? {
|
|
858
|
+
tool: {
|
|
859
|
+
kind,
|
|
860
|
+
native: name,
|
|
861
|
+
...kind === "shell" ? { command: text4(input["command"]) ?? "" } : {},
|
|
862
|
+
paths: kind === "shell" ? [] : paths
|
|
863
|
+
}
|
|
864
|
+
} : {},
|
|
865
|
+
...prompt !== void 0 && event === "prompt" ? { prompt } : {},
|
|
866
|
+
...event === "stop" ? {
|
|
867
|
+
stop: {
|
|
868
|
+
active: raw["stop_hook_active"] === true,
|
|
869
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
870
|
+
}
|
|
871
|
+
} : {}
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// src/hooks/runtimes/gemini/projection.ts
|
|
876
|
+
var SETTINGS_FILE2 = ".gemini/settings.json";
|
|
877
|
+
function command4(event) {
|
|
878
|
+
return climbCommand(`node ${HOOK_PATH} gemini ${event}`);
|
|
879
|
+
}
|
|
880
|
+
function project4(plan) {
|
|
881
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
882
|
+
for (const binding of plan.bindings) {
|
|
883
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
884
|
+
entries.push({
|
|
885
|
+
...binding.matcher ? { matcher: binding.matcher } : {},
|
|
886
|
+
hooks: [
|
|
887
|
+
{
|
|
888
|
+
type: "command",
|
|
889
|
+
name: `pmcp-${binding.event}`,
|
|
890
|
+
command: command4(binding.event),
|
|
891
|
+
timeout: binding.timeoutSec * 1e3
|
|
892
|
+
}
|
|
893
|
+
]
|
|
894
|
+
});
|
|
895
|
+
byEvent.set(binding.native, entries);
|
|
896
|
+
}
|
|
897
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
898
|
+
file: SETTINGS_FILE2,
|
|
899
|
+
key: ["hooks", native],
|
|
900
|
+
entries
|
|
901
|
+
}));
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// src/hooks/runtimes/gemini/synthetic.ts
|
|
905
|
+
function synthetic4(options2) {
|
|
906
|
+
const { cwd, session, event, command: command6, path, tool } = options2;
|
|
907
|
+
const defaults = {
|
|
908
|
+
shell: "run_shell_command",
|
|
909
|
+
edit: "write_file",
|
|
910
|
+
read: "read_file",
|
|
911
|
+
other: "unknown_tool"
|
|
912
|
+
};
|
|
913
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
914
|
+
if (!name && command6 !== void 0) name = "run_shell_command";
|
|
915
|
+
if (!name && path !== void 0) name = "write_file";
|
|
916
|
+
const input = name === "run_shell_command" ? { command: command6 ?? "" } : { file_path: path ?? "", content: "" };
|
|
917
|
+
return {
|
|
918
|
+
session_id: session,
|
|
919
|
+
cwd,
|
|
920
|
+
hook_event_name: event,
|
|
921
|
+
timestamp: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
922
|
+
...name ? { tool_name: name, tool_input: input } : {},
|
|
923
|
+
...event === "AfterAgent" ? { stop_hook_active: false } : {}
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// src/hooks/runtimes/gemini/adapter.ts
|
|
928
|
+
var TOOL_EVENTS4 = /* @__PURE__ */ new Set(["before_tool", "after_tool"]);
|
|
929
|
+
var gemini = {
|
|
930
|
+
id: "gemini",
|
|
931
|
+
synthetic: synthetic4,
|
|
932
|
+
capabilities: capabilities4,
|
|
933
|
+
bindings(event, tools, timeoutSec) {
|
|
934
|
+
if (capabilities4.events[event] === "unsupported") return [];
|
|
935
|
+
const matcher = TOOL_EVENTS4.has(event) ? matcherFor3(tools) : void 0;
|
|
936
|
+
return [
|
|
937
|
+
{
|
|
938
|
+
event,
|
|
939
|
+
native: NATIVE_EVENTS4[event],
|
|
940
|
+
...matcher ? { matcher } : {},
|
|
941
|
+
timeoutSec
|
|
942
|
+
}
|
|
943
|
+
];
|
|
944
|
+
},
|
|
945
|
+
normalize: normalize4,
|
|
946
|
+
render: render4,
|
|
947
|
+
project: project4,
|
|
948
|
+
nativeFiles: [{ file: SETTINGS_FILE2, root: "hooks" }],
|
|
949
|
+
nativeEvent: (event) => NATIVE_EVENTS4[event],
|
|
950
|
+
trust: "folder"
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
// src/hooks/runtimes/grok/capabilities.ts
|
|
954
|
+
var capabilities5 = {
|
|
955
|
+
events: {
|
|
956
|
+
session_start: "measured",
|
|
957
|
+
prompt: "measured",
|
|
958
|
+
before_tool: "measured",
|
|
959
|
+
after_tool: "measured",
|
|
960
|
+
stop: "measured",
|
|
961
|
+
session_end: "measured",
|
|
962
|
+
notification: "unsupported"
|
|
963
|
+
},
|
|
964
|
+
outputs: {
|
|
965
|
+
deny: "measured",
|
|
966
|
+
ask: "unsupported",
|
|
967
|
+
context: "measured",
|
|
968
|
+
feedback: "unqualified"
|
|
969
|
+
},
|
|
970
|
+
context: ["before_tool", "after_tool", "stop"]
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
// src/hooks/runtimes/grok/events.ts
|
|
974
|
+
var NATIVE_EVENTS5 = {
|
|
975
|
+
session_start: "SessionStart",
|
|
976
|
+
prompt: "UserPromptSubmit",
|
|
977
|
+
before_tool: "PreToolUse",
|
|
978
|
+
after_tool: "PostToolUse",
|
|
979
|
+
stop: "Stop",
|
|
980
|
+
session_end: "SessionEnd"
|
|
981
|
+
};
|
|
982
|
+
var NEUTRAL_EVENTS5 = Object.fromEntries(
|
|
983
|
+
Object.entries(NATIVE_EVENTS5).map(([neutral, native]) => [native, neutral])
|
|
984
|
+
);
|
|
985
|
+
var TOOL_KINDS5 = {
|
|
986
|
+
run_terminal_command: "shell",
|
|
987
|
+
write: "edit",
|
|
988
|
+
search_replace: "edit",
|
|
989
|
+
read_file: "read",
|
|
990
|
+
list_dir: "read",
|
|
991
|
+
grep_search: "read"
|
|
992
|
+
};
|
|
993
|
+
function toolKind5(name) {
|
|
994
|
+
return TOOL_KINDS5[name] ?? "other";
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
// src/hooks/runtimes/grok/output.ts
|
|
998
|
+
var MAX_REASON5 = 2e3;
|
|
999
|
+
var DELIVERS2 = [
|
|
1000
|
+
"before_tool",
|
|
1001
|
+
"after_tool",
|
|
1002
|
+
"stop"
|
|
1003
|
+
];
|
|
1004
|
+
function render5(input, decision) {
|
|
1005
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
1006
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON5);
|
|
1007
|
+
const notes = [...decision.context ?? []];
|
|
1008
|
+
const refuse = blocking && input.event === "before_tool";
|
|
1009
|
+
if (blocking && !refuse) notes.unshift(reason);
|
|
1010
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
1011
|
+
const quiet = input.event === "stop" && input.stop?.active === true;
|
|
1012
|
+
const context = notes.length > 0 && DELIVERS2.includes(input.event) && !quiet ? notes.join("\n\n") : void 0;
|
|
1013
|
+
if (!refuse && !context) return { stdout: "", exitCode: 0 };
|
|
1014
|
+
const out = {
|
|
1015
|
+
hookSpecificOutput: {
|
|
1016
|
+
hookEventName: NATIVE_EVENTS5[input.event],
|
|
1017
|
+
...refuse ? { permissionDecision: "deny", permissionDecisionReason: reason } : {},
|
|
1018
|
+
...context ? { additionalContext: context } : {}
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
return { stdout: `${JSON.stringify(out)}
|
|
1022
|
+
`, exitCode: 0 };
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// src/hooks/runtimes/grok/payload.ts
|
|
1026
|
+
import { isAbsolute as isAbsolute5, resolve as resolve5 } from "path";
|
|
1027
|
+
function object5(value) {
|
|
1028
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1029
|
+
}
|
|
1030
|
+
var text5 = (value) => typeof value === "string" ? value : void 0;
|
|
1031
|
+
function claims(payload) {
|
|
1032
|
+
const raw = object5(payload);
|
|
1033
|
+
return typeof raw["sessionId"] === "string" && typeof raw["workspaceRoot"] === "string";
|
|
1034
|
+
}
|
|
1035
|
+
function normalize5(nativeEvent, payload, projectRoot) {
|
|
1036
|
+
const event = NEUTRAL_EVENTS5[nativeEvent];
|
|
1037
|
+
if (!event) return null;
|
|
1038
|
+
const raw = object5(payload);
|
|
1039
|
+
const cwd = text5(raw["cwd"]) ?? text5(raw["workspaceRoot"]) ?? projectRoot;
|
|
1040
|
+
const name = text5(raw["toolName"]) ?? text5(raw["tool_name"]);
|
|
1041
|
+
const input = object5(raw["toolInput"] ?? raw["tool_input"]);
|
|
1042
|
+
const kind = name ? toolKind5(name) : void 0;
|
|
1043
|
+
const path = text5(input["file_path"]);
|
|
1044
|
+
const paths = path ? [isAbsolute5(path) ? path : resolve5(cwd, path)] : [];
|
|
1045
|
+
const turnId = text5(raw["promptId"]);
|
|
1046
|
+
const prompt = text5(raw["prompt"]);
|
|
1047
|
+
const lastMessage = text5(raw["lastAssistantMessage"]);
|
|
1048
|
+
return {
|
|
1049
|
+
contract: 2,
|
|
1050
|
+
runtime: "grok",
|
|
1051
|
+
event,
|
|
1052
|
+
session: {
|
|
1053
|
+
id: text5(raw["sessionId"]) ?? text5(raw["session_id"]) ?? "",
|
|
1054
|
+
...turnId ? { turnId } : {}
|
|
1055
|
+
},
|
|
1056
|
+
cwd,
|
|
1057
|
+
projectRoot,
|
|
1058
|
+
...name && kind ? {
|
|
1059
|
+
tool: {
|
|
1060
|
+
kind,
|
|
1061
|
+
native: name,
|
|
1062
|
+
...kind === "shell" ? { command: text5(input["command"]) ?? "" } : {},
|
|
1063
|
+
paths: kind === "shell" ? [] : paths
|
|
1064
|
+
}
|
|
1065
|
+
} : {},
|
|
1066
|
+
...prompt !== void 0 && event === "prompt" ? { prompt } : {},
|
|
1067
|
+
...event === "stop" ? {
|
|
1068
|
+
stop: {
|
|
1069
|
+
active: raw["stopHookActive"] === true,
|
|
1070
|
+
...lastMessage !== void 0 ? { lastMessage } : {}
|
|
1071
|
+
}
|
|
1072
|
+
} : {}
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// src/hooks/runtimes/grok/projection.ts
|
|
1077
|
+
var HOOKS_FILE3 = ".grok/hooks/pmcp.json";
|
|
1078
|
+
function command5(event) {
|
|
1079
|
+
return climbCommand(
|
|
1080
|
+
`node ${HOOK_PATH} grok ${event}`,
|
|
1081
|
+
void 0,
|
|
1082
|
+
'"${GROK_WORKSPACE_ROOT:-.}"'
|
|
1083
|
+
);
|
|
1084
|
+
}
|
|
1085
|
+
function project5(plan) {
|
|
1086
|
+
const byEvent = /* @__PURE__ */ new Map();
|
|
1087
|
+
for (const binding of plan.bindings) {
|
|
1088
|
+
const entries = byEvent.get(binding.native) ?? [];
|
|
1089
|
+
entries.push({
|
|
1090
|
+
hooks: [
|
|
1091
|
+
{
|
|
1092
|
+
type: "command",
|
|
1093
|
+
command: command5(binding.event),
|
|
1094
|
+
timeout: binding.timeoutSec,
|
|
1095
|
+
statusMessage: hookLabel(binding.event)
|
|
1096
|
+
}
|
|
1097
|
+
]
|
|
1098
|
+
});
|
|
1099
|
+
byEvent.set(binding.native, entries);
|
|
1100
|
+
}
|
|
1101
|
+
return [...byEvent].map(([native, entries]) => ({
|
|
1102
|
+
file: HOOKS_FILE3,
|
|
1103
|
+
key: ["hooks", native],
|
|
1104
|
+
entries
|
|
1105
|
+
}));
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// src/hooks/runtimes/grok/synthetic.ts
|
|
1109
|
+
function synthetic5(options2) {
|
|
1110
|
+
const { cwd, session, event, command: command6, path, tool } = options2;
|
|
1111
|
+
const defaults = {
|
|
1112
|
+
shell: "run_terminal_command",
|
|
1113
|
+
edit: "write",
|
|
1114
|
+
read: "read_file",
|
|
1115
|
+
other: "unknown_tool"
|
|
1116
|
+
};
|
|
1117
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
1118
|
+
if (!name && command6 !== void 0) name = "run_terminal_command";
|
|
1119
|
+
if (!name && path !== void 0) name = "write";
|
|
1120
|
+
const input = name === "run_terminal_command" ? { command: command6 ?? "" } : { file_path: path ?? "", content: "" };
|
|
1121
|
+
return {
|
|
1122
|
+
hookEventName: event,
|
|
1123
|
+
sessionId: session,
|
|
1124
|
+
cwd,
|
|
1125
|
+
workspaceRoot: cwd,
|
|
1126
|
+
session_id: session,
|
|
1127
|
+
hook_event_name: event,
|
|
1128
|
+
...name ? { toolName: name, toolInput: input, tool_name: name, tool_input: input } : {},
|
|
1129
|
+
...event === "Stop" ? { stopHookActive: false, stop_hook_active: false } : {}
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// src/hooks/runtimes/grok/adapter.ts
|
|
1134
|
+
var grok = {
|
|
1135
|
+
id: "grok",
|
|
1136
|
+
synthetic: synthetic5,
|
|
1137
|
+
capabilities: capabilities5,
|
|
1138
|
+
// No matcher: only grok's Claude aliases (Bash, Write) were measured, and dispatch filters.
|
|
1139
|
+
bindings(event, _tools, timeoutSec) {
|
|
1140
|
+
const native = NATIVE_EVENTS5[event];
|
|
1141
|
+
if (!native || capabilities5.events[event] === "unsupported") return [];
|
|
1142
|
+
return [{ event, native, timeoutSec }];
|
|
1143
|
+
},
|
|
1144
|
+
normalize: normalize5,
|
|
1145
|
+
render: render5,
|
|
1146
|
+
project: project5,
|
|
1147
|
+
nativeFiles: [{ file: HOOKS_FILE3, root: "hooks" }],
|
|
1148
|
+
nativeEvent: (event) => NATIVE_EVENTS5[event],
|
|
1149
|
+
ridesOn: { runtime: "claude", claims },
|
|
1150
|
+
trust: "folder"
|
|
1151
|
+
};
|
|
1152
|
+
|
|
1153
|
+
// src/hooks/runtimes/opencode/capabilities.ts
|
|
1154
|
+
var capabilities6 = {
|
|
1155
|
+
events: {
|
|
1156
|
+
session_start: "measured",
|
|
1157
|
+
prompt: "measured",
|
|
1158
|
+
before_tool: "measured",
|
|
1159
|
+
after_tool: "measured",
|
|
1160
|
+
stop: "measured",
|
|
1161
|
+
session_end: "unsupported",
|
|
1162
|
+
notification: "unsupported"
|
|
1163
|
+
},
|
|
1164
|
+
outputs: {
|
|
1165
|
+
deny: "measured",
|
|
1166
|
+
ask: "unsupported",
|
|
1167
|
+
context: "measured",
|
|
1168
|
+
feedback: "unsupported"
|
|
1169
|
+
},
|
|
1170
|
+
context: ["session_start", "prompt", "after_tool"]
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
// src/hooks/runtimes/opencode/events.ts
|
|
1174
|
+
var NATIVE_EVENTS6 = {
|
|
1175
|
+
session_start: "session.created",
|
|
1176
|
+
prompt: "chat.message",
|
|
1177
|
+
before_tool: "tool.execute.before",
|
|
1178
|
+
after_tool: "tool.execute.after",
|
|
1179
|
+
stop: "session.idle"
|
|
1180
|
+
};
|
|
1181
|
+
var NEUTRAL_EVENTS6 = Object.fromEntries(
|
|
1182
|
+
Object.entries(NATIVE_EVENTS6).map(([neutral, native]) => [native, neutral])
|
|
1183
|
+
);
|
|
1184
|
+
var TOOL_KINDS6 = {
|
|
1185
|
+
bash: "shell",
|
|
1186
|
+
write: "edit",
|
|
1187
|
+
edit: "edit",
|
|
1188
|
+
patch: "edit",
|
|
1189
|
+
read: "read",
|
|
1190
|
+
list: "read",
|
|
1191
|
+
glob: "read",
|
|
1192
|
+
grep: "read"
|
|
1193
|
+
};
|
|
1194
|
+
function toolKind6(name) {
|
|
1195
|
+
return TOOL_KINDS6[name] ?? "other";
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// src/hooks/runtimes/opencode/output.ts
|
|
1199
|
+
var MAX_REASON6 = 2e3;
|
|
1200
|
+
var DELIVERS3 = [
|
|
1201
|
+
"session_start",
|
|
1202
|
+
"prompt",
|
|
1203
|
+
"after_tool"
|
|
1204
|
+
];
|
|
1205
|
+
function render6(input, decision) {
|
|
1206
|
+
const blocking = decision.verdict === "deny" || decision.verdict === "ask";
|
|
1207
|
+
const reason = (decision.reason ?? "Refused by pmcp").slice(0, MAX_REASON6);
|
|
1208
|
+
if (input.event === "before_tool") {
|
|
1209
|
+
const out2 = blocking ? { deny: reason } : {};
|
|
1210
|
+
return { stdout: `${JSON.stringify(out2)}
|
|
1211
|
+
`, exitCode: 0 };
|
|
1212
|
+
}
|
|
1213
|
+
const notes = [...decision.context ?? []];
|
|
1214
|
+
if (blocking) notes.unshift(reason);
|
|
1215
|
+
if (decision.feedback) notes.push(decision.feedback);
|
|
1216
|
+
const out = notes.length > 0 && DELIVERS3.includes(input.event) ? { context: notes.join("\n\n") } : {};
|
|
1217
|
+
return { stdout: `${JSON.stringify(out)}
|
|
1218
|
+
`, exitCode: 0 };
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// src/hooks/runtimes/opencode/payload.ts
|
|
1222
|
+
import { isAbsolute as isAbsolute6, resolve as resolve6 } from "path";
|
|
1223
|
+
function object6(value) {
|
|
1224
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1225
|
+
}
|
|
1226
|
+
var text6 = (value) => typeof value === "string" ? value : void 0;
|
|
1227
|
+
function normalize6(nativeEvent, payload, projectRoot) {
|
|
1228
|
+
const event = NEUTRAL_EVENTS6[nativeEvent];
|
|
1229
|
+
if (!event) return null;
|
|
1230
|
+
const raw = object6(payload);
|
|
1231
|
+
const input = object6(raw["input"]);
|
|
1232
|
+
const properties = object6(raw["properties"]);
|
|
1233
|
+
const cwd = text6(raw["directory"]) ?? projectRoot;
|
|
1234
|
+
const name = text6(input["tool"]);
|
|
1235
|
+
const args = object6(raw["args"] ?? input["args"]);
|
|
1236
|
+
const kind = name ? toolKind6(name) : void 0;
|
|
1237
|
+
const path = text6(args["filePath"]);
|
|
1238
|
+
const paths = path ? [isAbsolute6(path) ? path : resolve6(cwd, path)] : [];
|
|
1239
|
+
const session = text6(input["sessionID"]) ?? text6(properties["sessionID"]) ?? "";
|
|
1240
|
+
return {
|
|
1241
|
+
contract: 2,
|
|
1242
|
+
runtime: "opencode",
|
|
1243
|
+
event,
|
|
1244
|
+
session: {
|
|
1245
|
+
id: session,
|
|
1246
|
+
...text6(raw["serverUrl"]) ? { wakeUrl: text6(raw["serverUrl"]) } : {}
|
|
1247
|
+
},
|
|
1248
|
+
cwd,
|
|
1249
|
+
projectRoot,
|
|
1250
|
+
...name && kind ? {
|
|
1251
|
+
tool: {
|
|
1252
|
+
kind,
|
|
1253
|
+
native: name,
|
|
1254
|
+
...kind === "shell" ? { command: text6(args["command"]) ?? "" } : {},
|
|
1255
|
+
paths: kind === "shell" ? [] : paths
|
|
1256
|
+
}
|
|
1257
|
+
} : {},
|
|
1258
|
+
...event === "stop" ? { stop: { active: false } } : {}
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// src/hooks/runtimes/opencode/plugin.ts
|
|
1263
|
+
var PLUGIN_TEMPLATE = `// Generated by pmcp project from pmcp.toml [hooks]; edit pmcp.toml, not this file.
|
|
1264
|
+
import { spawn } from 'node:child_process';
|
|
1265
|
+
import { join } from 'node:path';
|
|
1266
|
+
import { fileURLToPath } from 'node:url';
|
|
1267
|
+
|
|
1268
|
+
const ROOT = fileURLToPath(new URL("../..", import.meta.url));
|
|
1269
|
+
const HOOK = join(ROOT, ".agents/pmcp-hooks/hook.mjs");
|
|
1270
|
+
const TIMEOUT_MS = __TIMEOUTS__;
|
|
1271
|
+
|
|
1272
|
+
// Resolves the hook's answer, or null when it could not run or answered nonsense.
|
|
1273
|
+
function ask(event, payload) {
|
|
1274
|
+
if (!(event in TIMEOUT_MS)) return Promise.resolve({});
|
|
1275
|
+
return new Promise((done) => {
|
|
1276
|
+
const child = spawn("node", [HOOK, "opencode", event], {
|
|
1277
|
+
cwd: ROOT,
|
|
1278
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
1279
|
+
});
|
|
1280
|
+
let out = "";
|
|
1281
|
+
const timer = setTimeout(() => child.kill(), TIMEOUT_MS[event]);
|
|
1282
|
+
child.stdout.on("data", (chunk) => {
|
|
1283
|
+
out += chunk;
|
|
1284
|
+
});
|
|
1285
|
+
child.on("error", () => {
|
|
1286
|
+
clearTimeout(timer);
|
|
1287
|
+
done(null);
|
|
1288
|
+
});
|
|
1289
|
+
child.on("close", (code) => {
|
|
1290
|
+
clearTimeout(timer);
|
|
1291
|
+
try {
|
|
1292
|
+
done(code === 0 ? JSON.parse(out || "{}") : null);
|
|
1293
|
+
} catch {
|
|
1294
|
+
done(null);
|
|
1295
|
+
}
|
|
1296
|
+
});
|
|
1297
|
+
child.stdin.end(JSON.stringify(payload));
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
export const Pmcp = async ({ directory, serverUrl }) => {
|
|
1302
|
+
// The server this session runs on, so pmcp sessions send --wake can prompt it while idle.
|
|
1303
|
+
const server = serverUrl ? String(serverUrl) : undefined;
|
|
1304
|
+
// queued: notes not yet shown; showing: notes every model call of this turn carries. A turn
|
|
1305
|
+
// can make several model calls (opencode's first is a small title call), so a note shown
|
|
1306
|
+
// once is kept until the next prompt starts a new turn.
|
|
1307
|
+
const queued = new Map();
|
|
1308
|
+
const showing = new Map();
|
|
1309
|
+
const hold = (session, text) => {
|
|
1310
|
+
if (session && text) queued.set(session, [...(queued.get(session) ?? []), text]);
|
|
1311
|
+
};
|
|
1312
|
+
return {
|
|
1313
|
+
"tool.execute.before": async (input, output) => {
|
|
1314
|
+
const answer = await ask("before_tool", { input, args: output.args, directory });
|
|
1315
|
+
// A tool call goes ahead only on a clear answer, as with the other runtimes' guards.
|
|
1316
|
+
if (!answer) throw new Error("pmcp hook did not answer; the call was refused");
|
|
1317
|
+
if (answer.deny) throw new Error(answer.deny);
|
|
1318
|
+
},
|
|
1319
|
+
"tool.execute.after": async (input, output) => {
|
|
1320
|
+
const answer = await ask("after_tool", { input, directory });
|
|
1321
|
+
if (answer?.context) output.output = \`\${output.output ?? ""}\\n\\n\${answer.context}\`;
|
|
1322
|
+
},
|
|
1323
|
+
"chat.message": async (input) => {
|
|
1324
|
+
showing.delete(input.sessionID);
|
|
1325
|
+
const answer = await ask("prompt", { input, directory });
|
|
1326
|
+
hold(input.sessionID, answer?.context);
|
|
1327
|
+
},
|
|
1328
|
+
"experimental.chat.system.transform": async (input, output) => {
|
|
1329
|
+
const session = input.sessionID;
|
|
1330
|
+
const notes = [...(showing.get(session) ?? []), ...(queued.get(session) ?? [])];
|
|
1331
|
+
queued.delete(session);
|
|
1332
|
+
if (notes.length === 0) return;
|
|
1333
|
+
showing.set(session, notes);
|
|
1334
|
+
output.system.push(notes.join("\\n\\n"));
|
|
1335
|
+
},
|
|
1336
|
+
event: async ({ event }) => {
|
|
1337
|
+
if (event.type === "session.created") {
|
|
1338
|
+
const answer = await ask("session_start", { ...event, directory, serverUrl: server });
|
|
1339
|
+
hold(event.properties?.sessionID, answer?.context);
|
|
1340
|
+
}
|
|
1341
|
+
if (event.type === "session.idle") await ask("stop", { ...event, directory });
|
|
1342
|
+
},
|
|
1343
|
+
};
|
|
1344
|
+
};
|
|
1345
|
+
`;
|
|
1346
|
+
|
|
1347
|
+
// src/hooks/runtimes/opencode/projection.ts
|
|
1348
|
+
var PLUGIN_FILE = ".opencode/plugins/pmcp.js";
|
|
1349
|
+
function files(plan) {
|
|
1350
|
+
if (plan.bindings.length === 0) return [];
|
|
1351
|
+
const timeouts = Object.fromEntries(
|
|
1352
|
+
plan.bindings.map((binding) => [binding.event, binding.timeoutSec * 1e3])
|
|
1353
|
+
);
|
|
1354
|
+
return [
|
|
1355
|
+
{
|
|
1356
|
+
path: PLUGIN_FILE,
|
|
1357
|
+
content: PLUGIN_TEMPLATE.replace(
|
|
1358
|
+
"__TIMEOUTS__",
|
|
1359
|
+
JSON.stringify(timeouts)
|
|
1360
|
+
)
|
|
1361
|
+
}
|
|
1362
|
+
];
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
// src/hooks/runtimes/opencode/synthetic.ts
|
|
1366
|
+
function synthetic6(options2) {
|
|
1367
|
+
const { cwd, session, event, command: command6, path, tool } = options2;
|
|
1368
|
+
const defaults = {
|
|
1369
|
+
shell: "bash",
|
|
1370
|
+
edit: "write",
|
|
1371
|
+
read: "read",
|
|
1372
|
+
other: "unknown_tool"
|
|
1373
|
+
};
|
|
1374
|
+
let name = tool ? defaults[tool] ?? tool : void 0;
|
|
1375
|
+
if (!name && command6 !== void 0) name = "bash";
|
|
1376
|
+
if (!name && path !== void 0) name = "write";
|
|
1377
|
+
const args = name === "bash" ? { command: command6 ?? "" } : { filePath: path ?? "", content: "" };
|
|
1378
|
+
if (event === "session.created" || event === "session.idle")
|
|
1379
|
+
return { type: event, properties: { sessionID: session }, directory: cwd };
|
|
1380
|
+
return {
|
|
1381
|
+
input: {
|
|
1382
|
+
sessionID: session,
|
|
1383
|
+
...name ? { tool: name, callID: "c1" } : {}
|
|
1384
|
+
},
|
|
1385
|
+
...name ? { args } : {},
|
|
1386
|
+
directory: cwd
|
|
1387
|
+
};
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// src/hooks/runtimes/opencode/adapter.ts
|
|
1391
|
+
var opencode = {
|
|
1392
|
+
id: "opencode",
|
|
1393
|
+
synthetic: synthetic6,
|
|
1394
|
+
capabilities: capabilities6,
|
|
1395
|
+
bindings(event, _tools, timeoutSec) {
|
|
1396
|
+
const native = NATIVE_EVENTS6[event];
|
|
1397
|
+
if (!native || capabilities6.events[event] === "unsupported") return [];
|
|
1398
|
+
return [{ event, native, timeoutSec }];
|
|
1399
|
+
},
|
|
1400
|
+
normalize: normalize6,
|
|
1401
|
+
render: render6,
|
|
1402
|
+
// opencode has no hook configuration to edit; the projection is the generated plugin file.
|
|
1403
|
+
project: () => [],
|
|
1404
|
+
files,
|
|
1405
|
+
nativeFiles: [],
|
|
1406
|
+
nativeEvent: (event) => NATIVE_EVENTS6[event],
|
|
1407
|
+
trust: "none"
|
|
1408
|
+
};
|
|
1409
|
+
|
|
1410
|
+
// src/hooks/runtimes/index.ts
|
|
1411
|
+
var RUNTIMES = {
|
|
1412
|
+
claude,
|
|
1413
|
+
codex,
|
|
1414
|
+
gemini,
|
|
1415
|
+
antigravity,
|
|
1416
|
+
grok,
|
|
1417
|
+
opencode
|
|
1418
|
+
};
|
|
1419
|
+
var NATIVE_HOOK_FILES = Object.values(
|
|
1420
|
+
RUNTIMES
|
|
1421
|
+
).flatMap((adapter) => adapter.nativeFiles);
|
|
1422
|
+
|
|
1423
|
+
// src/project/mcp-spec/_shared.ts
|
|
1424
|
+
var ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
1425
|
+
var KEYS = /* @__PURE__ */ new Set([
|
|
1426
|
+
"auth",
|
|
1427
|
+
"command",
|
|
1428
|
+
"args",
|
|
1429
|
+
"env",
|
|
1430
|
+
"env_vars",
|
|
1431
|
+
"env_from",
|
|
1432
|
+
"cwd",
|
|
1433
|
+
"url",
|
|
1434
|
+
"headers",
|
|
1435
|
+
"header_env",
|
|
1436
|
+
"bearer_token_env_var",
|
|
1437
|
+
"native",
|
|
1438
|
+
"targets",
|
|
1439
|
+
"tools",
|
|
1440
|
+
"mailbox"
|
|
1441
|
+
]);
|
|
1442
|
+
function fail(path, where, detail) {
|
|
1443
|
+
throw new ConfigError(path, `${where} ${detail}`);
|
|
1444
|
+
}
|
|
1445
|
+
function object7(path, where, value) {
|
|
1446
|
+
if (value === void 0) {
|
|
1447
|
+
return {};
|
|
1448
|
+
}
|
|
1449
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
1450
|
+
fail(path, where, "must be a table");
|
|
1451
|
+
}
|
|
1452
|
+
return value;
|
|
1453
|
+
}
|
|
1454
|
+
function list(path, where, value, unique = true) {
|
|
1455
|
+
if (value === void 0) {
|
|
1456
|
+
return [];
|
|
1457
|
+
}
|
|
1458
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === "string" && item !== "")) {
|
|
1459
|
+
fail(path, where, "must be a list of nonempty strings");
|
|
1460
|
+
}
|
|
1461
|
+
if (unique && new Set(value).size !== value.length) {
|
|
1462
|
+
fail(path, where, "contains duplicates");
|
|
1463
|
+
}
|
|
1464
|
+
return value;
|
|
1465
|
+
}
|
|
1466
|
+
function mapping(path, where, value) {
|
|
1467
|
+
const result = {};
|
|
1468
|
+
for (const [key, item] of Object.entries(object7(path, where, value))) {
|
|
1469
|
+
if (typeof item !== "string") {
|
|
1470
|
+
fail(path, where, "values must be strings");
|
|
1471
|
+
}
|
|
1472
|
+
result[key] = item;
|
|
1473
|
+
}
|
|
1474
|
+
return result;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
// src/project/mcp-spec/options.ts
|
|
1478
|
+
function nativeOptions(path, where, value) {
|
|
1479
|
+
const fields2 = {
|
|
1480
|
+
claude: ["timeout"],
|
|
1481
|
+
codex: ["startup_timeout_sec", "tool_timeout_sec"],
|
|
1482
|
+
gemini: ["timeout"],
|
|
1483
|
+
grok: ["startup_timeout_sec", "tool_timeout_sec"],
|
|
1484
|
+
antigravity: [],
|
|
1485
|
+
opencode: ["timeout"]
|
|
1486
|
+
};
|
|
1487
|
+
const result = {};
|
|
1488
|
+
for (const [tool, raw] of Object.entries(
|
|
1489
|
+
object7(path, `${where} native`, value)
|
|
1490
|
+
)) {
|
|
1491
|
+
if (!TOOLS.includes(tool)) {
|
|
1492
|
+
fail(path, where, `unknown native target ${tool}`);
|
|
1493
|
+
}
|
|
1494
|
+
const values = {};
|
|
1495
|
+
for (const [key, item] of Object.entries(
|
|
1496
|
+
object7(path, `${where} native.${tool}`, raw)
|
|
1497
|
+
)) {
|
|
1498
|
+
if (!fields2[tool].includes(key) || typeof item !== "number" || !Number.isFinite(item) || item <= 0) {
|
|
1499
|
+
fail(path, where, `unsupported native option ${tool}.${key}`);
|
|
1500
|
+
}
|
|
1501
|
+
values[key] = item;
|
|
1502
|
+
}
|
|
1503
|
+
result[tool] = values;
|
|
1504
|
+
}
|
|
1505
|
+
return result;
|
|
1506
|
+
}
|
|
1507
|
+
function environmentSources(path, where, value) {
|
|
1508
|
+
const result = {};
|
|
1509
|
+
for (const [key, raw] of Object.entries(
|
|
1510
|
+
object7(path, `${where} env_from`, value)
|
|
1511
|
+
)) {
|
|
1512
|
+
if (!ENV_NAME.test(key) || key === "PROJECT_ROOT") {
|
|
1513
|
+
fail(path, where, "invalid environment source name");
|
|
1514
|
+
}
|
|
1515
|
+
const source = object7(path, `${where} env_from.${key}`, raw);
|
|
1516
|
+
if (Object.keys(source).some((name) => name !== "file" && name !== "pointer")) {
|
|
1517
|
+
fail(path, where, "environment sources accept only file and pointer");
|
|
1518
|
+
}
|
|
1519
|
+
if (typeof source["file"] !== "string" || source["file"] === "" || typeof source["pointer"] !== "string" || !source["pointer"].startsWith("/")) {
|
|
1520
|
+
fail(path, where, "environment source needs file and a JSON pointer");
|
|
1521
|
+
}
|
|
1522
|
+
result[key] = { file: source["file"], pointer: source["pointer"] };
|
|
1523
|
+
}
|
|
1524
|
+
return result;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
// src/project/mcp-spec/transport.ts
|
|
1528
|
+
function readTransport(path, where, entry) {
|
|
1529
|
+
const command6 = entry["command"];
|
|
1530
|
+
const url = entry["url"];
|
|
1531
|
+
const auth = entry["auth"];
|
|
1532
|
+
if (auth !== void 0 && auth !== "native" && auth !== "pmcp")
|
|
1533
|
+
fail(path, where, "auth must be native or pmcp");
|
|
1534
|
+
if (auth === "pmcp" && typeof url !== "string")
|
|
1535
|
+
fail(path, where, "auth = pmcp requires a remote HTTP server");
|
|
1536
|
+
if (auth === "pmcp" && typeof url === "string") safeEndpoint(url);
|
|
1537
|
+
if (command6 === void 0 === (url === void 0)) {
|
|
1538
|
+
fail(path, where, "provide exactly one of command or url");
|
|
1539
|
+
}
|
|
1540
|
+
for (const [key, item] of [
|
|
1541
|
+
["command", command6],
|
|
1542
|
+
["url", url],
|
|
1543
|
+
["cwd", entry["cwd"]]
|
|
1544
|
+
]) {
|
|
1545
|
+
if (item !== void 0 && (typeof item !== "string" || item === "")) {
|
|
1546
|
+
fail(path, where, `${key} must be a nonempty string`);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
if (typeof url === "string") {
|
|
1550
|
+
let endpoint;
|
|
1551
|
+
try {
|
|
1552
|
+
endpoint = new URL(url);
|
|
1553
|
+
} catch {
|
|
1554
|
+
fail(path, where, "url must be an absolute HTTP(S) endpoint");
|
|
1555
|
+
}
|
|
1556
|
+
if (!["https:", "http:"].includes(endpoint.protocol) || endpoint.username || endpoint.password) {
|
|
1557
|
+
fail(path, where, "url must use HTTP(S) without embedded credentials");
|
|
1558
|
+
}
|
|
1559
|
+
for (const key of [
|
|
1560
|
+
"args",
|
|
1561
|
+
"env",
|
|
1562
|
+
"env_vars",
|
|
1563
|
+
"env_from",
|
|
1564
|
+
"cwd",
|
|
1565
|
+
"mailbox"
|
|
1566
|
+
]) {
|
|
1567
|
+
if (entry[key] !== void 0) {
|
|
1568
|
+
fail(path, where, `${key} applies only to stdio`);
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
} else {
|
|
1572
|
+
for (const key of ["headers", "header_env", "bearer_token_env_var"]) {
|
|
1573
|
+
if (entry[key] !== void 0) {
|
|
1574
|
+
fail(path, where, `${key} applies only to HTTP`);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
return { command: command6, url, auth };
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
// src/project/mcp-spec/plugin-binary.ts
|
|
1582
|
+
var PLUGIN_BINARIES = ["pmcp-browser", "pmcp-desktop"];
|
|
1583
|
+
var isPlugin = (command6) => PLUGIN_BINARIES.includes(command6);
|
|
1584
|
+
function resolvePluginCommand(command6, from) {
|
|
1585
|
+
if (!isPlugin(command6)) return command6;
|
|
1586
|
+
const found = packagedBinary(command6, from);
|
|
1587
|
+
return "path" in found ? found.path : command6;
|
|
1588
|
+
}
|
|
1589
|
+
function pluginBinaryDoctor(servers, from) {
|
|
1590
|
+
const findings = [];
|
|
1591
|
+
for (const [alias, server] of Object.entries(servers)) {
|
|
1592
|
+
const command6 = server.command;
|
|
1593
|
+
if (command6 === void 0) continue;
|
|
1594
|
+
const name = command6.split("/").pop();
|
|
1595
|
+
if (!isPlugin(name)) continue;
|
|
1596
|
+
const check = `[mcp.${alias}] ${name}`;
|
|
1597
|
+
const found = packagedBinary(name, from);
|
|
1598
|
+
if (isPlugin(command6))
|
|
1599
|
+
findings.push({
|
|
1600
|
+
tool: "project",
|
|
1601
|
+
check,
|
|
1602
|
+
status: "warn",
|
|
1603
|
+
detail: `not resolved: ${"reason" in found ? found.reason : "unknown"}; runs ${name} from PATH`
|
|
1604
|
+
});
|
|
1605
|
+
else
|
|
1606
|
+
findings.push({
|
|
1607
|
+
tool: "project",
|
|
1608
|
+
check,
|
|
1609
|
+
status: "ok",
|
|
1610
|
+
detail: "path" in found && found.path === command6 ? `resolved to ${command6} (platform package)` : `explicit command kept: ${command6}`
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
return findings;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
// src/project/mcp-spec/read.ts
|
|
1617
|
+
function readMcp(path, value) {
|
|
1618
|
+
const servers = {};
|
|
1619
|
+
for (const [alias, raw] of Object.entries(object7(path, "[mcp]", value))) {
|
|
1620
|
+
const where = `[mcp.${alias}]`;
|
|
1621
|
+
if (!/^[a-z0-9][a-z0-9-]*$/u.test(alias)) {
|
|
1622
|
+
fail(path, where, "use lowercase, digits and -");
|
|
1623
|
+
}
|
|
1624
|
+
const entry = object7(path, where, raw);
|
|
1625
|
+
for (const key of Object.keys(entry)) {
|
|
1626
|
+
if (!KEYS.has(key)) {
|
|
1627
|
+
fail(path, where, `unknown field ${key}`);
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
const { command: command6, url, auth } = readTransport(path, where, entry);
|
|
1631
|
+
const env = mapping(path, `${where} env`, entry["env"]);
|
|
1632
|
+
const envVars = list(path, `${where} env_vars`, entry["env_vars"]);
|
|
1633
|
+
const envFrom = environmentSources(path, where, entry["env_from"]);
|
|
1634
|
+
if (Object.keys(envFrom).some(
|
|
1635
|
+
(key) => Object.hasOwn(env, key) || envVars.includes(key)
|
|
1636
|
+
)) {
|
|
1637
|
+
fail(path, where, "environment source conflicts with env or env_vars");
|
|
1638
|
+
}
|
|
1639
|
+
for (const key of [...Object.keys(env), ...envVars]) {
|
|
1640
|
+
if (!ENV_NAME.test(key) || key === "PROJECT_ROOT") {
|
|
1641
|
+
fail(path, where, "invalid environment variable name");
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
const headers = mapping(path, `${where} headers`, entry["headers"]);
|
|
1645
|
+
const headerEnv = mapping(path, `${where} header_env`, entry["header_env"]);
|
|
1646
|
+
const bearerTokenEnvVar = entry["bearer_token_env_var"];
|
|
1647
|
+
if (bearerTokenEnvVar !== void 0 && (typeof bearerTokenEnvVar !== "string" || !ENV_NAME.test(bearerTokenEnvVar))) {
|
|
1648
|
+
fail(
|
|
1649
|
+
path,
|
|
1650
|
+
where,
|
|
1651
|
+
"bearer_token_env_var must name an environment variable"
|
|
1652
|
+
);
|
|
1653
|
+
}
|
|
1654
|
+
for (const key of [...Object.keys(headers), ...Object.keys(headerEnv)]) {
|
|
1655
|
+
if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/u.test(key)) {
|
|
1656
|
+
fail(path, where, "invalid HTTP header name");
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
const headerNames = [
|
|
1660
|
+
...Object.keys(headers),
|
|
1661
|
+
...Object.keys(headerEnv)
|
|
1662
|
+
].map((key) => key.toLowerCase());
|
|
1663
|
+
if (auth === "pmcp" && headerNames.includes("authorization"))
|
|
1664
|
+
fail(
|
|
1665
|
+
path,
|
|
1666
|
+
where,
|
|
1667
|
+
"auth = pmcp owns Authorization; use bearer_token_env_var and pmcp mcp login"
|
|
1668
|
+
);
|
|
1669
|
+
if (new Set(headerNames).size !== headerNames.length) {
|
|
1670
|
+
fail(path, where, "duplicate HTTP header names");
|
|
1671
|
+
}
|
|
1672
|
+
if (bearerTokenEnvVar !== void 0 && headerNames.includes("authorization")) {
|
|
1673
|
+
fail(path, where, "bearer_token_env_var conflicts with Authorization");
|
|
1674
|
+
}
|
|
1675
|
+
for (const item of Object.values(headers)) {
|
|
1676
|
+
if (/[\r\n]/u.test(item) || /\$\{/u.test(item)) {
|
|
1677
|
+
fail(path, where, "use header_env for environment references");
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
for (const item of Object.values(headerEnv)) {
|
|
1681
|
+
if (!ENV_NAME.test(item)) {
|
|
1682
|
+
fail(path, where, "header_env values must name environment variables");
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
const targets = list(path, `${where} targets`, entry["targets"]);
|
|
1686
|
+
if (entry["targets"] !== void 0 && targets.length === 0) {
|
|
1687
|
+
fail(path, where, "targets cannot be empty");
|
|
1688
|
+
}
|
|
1689
|
+
for (const tool of targets) {
|
|
1690
|
+
if (!TOOLS.includes(tool)) {
|
|
1691
|
+
fail(path, where, `unknown target ${tool}`);
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
if (entry["mailbox"] !== void 0 && typeof entry["mailbox"] !== "boolean") {
|
|
1695
|
+
fail(path, where, "mailbox must be a boolean");
|
|
1696
|
+
}
|
|
1697
|
+
servers[alias] = {
|
|
1698
|
+
...auth === "pmcp" ? { auth } : {},
|
|
1699
|
+
...typeof command6 === "string" ? { command: resolvePluginCommand(command6) } : { url },
|
|
1700
|
+
args: list(path, `${where} args`, entry["args"], false),
|
|
1701
|
+
env,
|
|
1702
|
+
...envVars.length > 0 ? { envVars } : {},
|
|
1703
|
+
...Object.keys(envFrom).length > 0 ? { envFrom } : {},
|
|
1704
|
+
...typeof entry["cwd"] === "string" ? { cwd: entry["cwd"] } : {},
|
|
1705
|
+
...Object.keys(headers).length > 0 ? { headers } : {},
|
|
1706
|
+
...Object.keys(headerEnv).length > 0 ? { headerEnv } : {},
|
|
1707
|
+
...typeof bearerTokenEnvVar === "string" ? { bearerTokenEnvVar } : {},
|
|
1708
|
+
...entry["native"] !== void 0 ? { native: nativeOptions(path, where, entry["native"]) } : {},
|
|
1709
|
+
...targets.length > 0 ? { targets } : {},
|
|
1710
|
+
...entry["tools"] !== void 0 ? { tools: list(path, `${where} tools`, entry["tools"]) } : {},
|
|
1711
|
+
...entry["mailbox"] === true ? { mailbox: true } : {}
|
|
1712
|
+
};
|
|
1713
|
+
}
|
|
1714
|
+
return servers;
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// src/hooks/policies/git/forced-branch-move.ts
|
|
1718
|
+
import { resolve as resolve7 } from "path";
|
|
1719
|
+
|
|
1720
|
+
// src/hooks/shell/tokenize.ts
|
|
1721
|
+
var SEPARATORS = /* @__PURE__ */ new Set([
|
|
1722
|
+
"&&",
|
|
1723
|
+
"||",
|
|
1724
|
+
";",
|
|
1725
|
+
"|",
|
|
1726
|
+
"&",
|
|
1727
|
+
"\n",
|
|
1728
|
+
"(",
|
|
1729
|
+
")",
|
|
1730
|
+
"{",
|
|
1731
|
+
"}"
|
|
1732
|
+
]);
|
|
1733
|
+
function quoteEnd(text7, start) {
|
|
1734
|
+
for (let index = start; index < text7.length; index += 1) {
|
|
1735
|
+
const char = text7[index];
|
|
1736
|
+
if (char === "\\") index += 1;
|
|
1737
|
+
else if (char === '"') return index;
|
|
1738
|
+
else if (text7.startsWith("$(", index)) {
|
|
1739
|
+
const close = closing(text7, index + 2);
|
|
1740
|
+
if (close < 0) return -1;
|
|
1741
|
+
index = close;
|
|
1742
|
+
} else if (char === "`") {
|
|
1743
|
+
const close = text7.indexOf("`", index + 1);
|
|
1744
|
+
if (close < 0) return -1;
|
|
1745
|
+
index = close;
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
return -1;
|
|
1749
|
+
}
|
|
1750
|
+
function closing(text7, start) {
|
|
1751
|
+
let depth = 1;
|
|
1752
|
+
for (let index = start; index < text7.length; index += 1) {
|
|
1753
|
+
const char = text7[index];
|
|
1754
|
+
let skipTo = index;
|
|
1755
|
+
if (char === "\\") skipTo = index + 1;
|
|
1756
|
+
if (char === "'") skipTo = text7.indexOf("'", index + 1);
|
|
1757
|
+
if (char === "`") skipTo = text7.indexOf("`", index + 1);
|
|
1758
|
+
if (char === '"') skipTo = quoteEnd(text7, index + 1);
|
|
1759
|
+
if (skipTo < 0) return -1;
|
|
1760
|
+
if (skipTo !== index) {
|
|
1761
|
+
index = skipTo;
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
if (char === "(") depth += 1;
|
|
1765
|
+
if (char === ")") depth -= 1;
|
|
1766
|
+
if (depth === 0) return index;
|
|
1767
|
+
}
|
|
1768
|
+
return -1;
|
|
1769
|
+
}
|
|
1770
|
+
function tokenize(script) {
|
|
1771
|
+
const tokens = [];
|
|
1772
|
+
const nested = [];
|
|
1773
|
+
let word = "";
|
|
1774
|
+
let started = false;
|
|
1775
|
+
const flush = () => {
|
|
1776
|
+
if (started) tokens.push(word);
|
|
1777
|
+
word = "";
|
|
1778
|
+
started = false;
|
|
1779
|
+
};
|
|
1780
|
+
for (let index = 0; index < script.length; index += 1) {
|
|
1781
|
+
const char = script[index];
|
|
1782
|
+
const pair = script.slice(index, index + 2);
|
|
1783
|
+
if (char === "'") {
|
|
1784
|
+
const end = script.indexOf("'", index + 1);
|
|
1785
|
+
if (end < 0) return null;
|
|
1786
|
+
word += script.slice(index + 1, end);
|
|
1787
|
+
started = true;
|
|
1788
|
+
index = end;
|
|
1789
|
+
continue;
|
|
1790
|
+
}
|
|
1791
|
+
if (char === '"' || char === "`") {
|
|
1792
|
+
let end = index + 1;
|
|
1793
|
+
let text7 = "";
|
|
1794
|
+
while (end < script.length && script[end] !== char) {
|
|
1795
|
+
if (script[end] === "\\" && char === '"') {
|
|
1796
|
+
text7 += script[end + 1] ?? "";
|
|
1797
|
+
end += 2;
|
|
1798
|
+
continue;
|
|
1799
|
+
}
|
|
1800
|
+
if (char === '"' && script.startsWith("$(", end)) {
|
|
1801
|
+
const close = closing(script, end + 2);
|
|
1802
|
+
if (close < 0) return null;
|
|
1803
|
+
nested.push(script.slice(end + 2, close));
|
|
1804
|
+
end = close + 1;
|
|
1805
|
+
continue;
|
|
1806
|
+
}
|
|
1807
|
+
if (char === '"' && script[end] === "`") {
|
|
1808
|
+
const close = script.indexOf("`", end + 1);
|
|
1809
|
+
if (close < 0) return null;
|
|
1810
|
+
nested.push(script.slice(end + 1, close));
|
|
1811
|
+
end = close + 1;
|
|
1812
|
+
continue;
|
|
1813
|
+
}
|
|
1814
|
+
text7 += script[end];
|
|
1815
|
+
end += 1;
|
|
1816
|
+
}
|
|
1817
|
+
if (end >= script.length) return null;
|
|
1818
|
+
if (char === "`") nested.push(text7);
|
|
1819
|
+
if (char === '"') word += text7;
|
|
1820
|
+
started = true;
|
|
1821
|
+
index = end;
|
|
1822
|
+
continue;
|
|
1823
|
+
}
|
|
1824
|
+
if (pair === "$(") {
|
|
1825
|
+
const close = closing(script, index + 2);
|
|
1826
|
+
if (close < 0) return null;
|
|
1827
|
+
nested.push(script.slice(index + 2, close));
|
|
1828
|
+
word += "$()";
|
|
1829
|
+
started = true;
|
|
1830
|
+
index = close;
|
|
1831
|
+
continue;
|
|
1832
|
+
}
|
|
1833
|
+
if (char === "\\") {
|
|
1834
|
+
word += script[index + 1] ?? "";
|
|
1835
|
+
started = true;
|
|
1836
|
+
index += 1;
|
|
1837
|
+
continue;
|
|
1838
|
+
}
|
|
1839
|
+
if (char === "#" && !started) {
|
|
1840
|
+
const end = script.indexOf("\n", index);
|
|
1841
|
+
index = end < 0 ? script.length : end - 1;
|
|
1842
|
+
continue;
|
|
1843
|
+
}
|
|
1844
|
+
if (pair === "&&" || pair === "||") {
|
|
1845
|
+
flush();
|
|
1846
|
+
tokens.push(pair);
|
|
1847
|
+
index += 1;
|
|
1848
|
+
continue;
|
|
1849
|
+
}
|
|
1850
|
+
if (SEPARATORS.has(char)) {
|
|
1851
|
+
flush();
|
|
1852
|
+
tokens.push(char);
|
|
1853
|
+
continue;
|
|
1854
|
+
}
|
|
1855
|
+
if (char === " " || char === " ") {
|
|
1856
|
+
flush();
|
|
1857
|
+
continue;
|
|
1858
|
+
}
|
|
1859
|
+
word += char;
|
|
1860
|
+
started = true;
|
|
1861
|
+
}
|
|
1862
|
+
flush();
|
|
1863
|
+
return { tokens, nested };
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
// src/hooks/shell/vocabulary.ts
|
|
1867
|
+
var WRAPPERS = /* @__PURE__ */ new Set([
|
|
1868
|
+
"env",
|
|
1869
|
+
"timeout",
|
|
1870
|
+
"nohup",
|
|
1871
|
+
"nice",
|
|
1872
|
+
"command",
|
|
1873
|
+
"exec",
|
|
1874
|
+
"time",
|
|
1875
|
+
"sudo",
|
|
1876
|
+
"stdbuf",
|
|
1877
|
+
"xargs"
|
|
1878
|
+
]);
|
|
1879
|
+
var SHELLS = /* @__PURE__ */ new Set([
|
|
1880
|
+
"bash",
|
|
1881
|
+
"sh",
|
|
1882
|
+
"zsh",
|
|
1883
|
+
"dash"
|
|
1884
|
+
]);
|
|
1885
|
+
var GIT_VALUE_OPTIONS = /* @__PURE__ */ new Set([
|
|
1886
|
+
"-C",
|
|
1887
|
+
"-c",
|
|
1888
|
+
"--git-dir",
|
|
1889
|
+
"--work-tree",
|
|
1890
|
+
"--namespace",
|
|
1891
|
+
"--exec-path"
|
|
1892
|
+
]);
|
|
1893
|
+
var GIT_MUTATING = /* @__PURE__ */ new Set([
|
|
1894
|
+
"commit",
|
|
1895
|
+
"push",
|
|
1896
|
+
"merge",
|
|
1897
|
+
"rebase",
|
|
1898
|
+
"reset",
|
|
1899
|
+
"checkout",
|
|
1900
|
+
"switch",
|
|
1901
|
+
"cherry-pick",
|
|
1902
|
+
"revert",
|
|
1903
|
+
"am",
|
|
1904
|
+
"stash",
|
|
1905
|
+
"tag",
|
|
1906
|
+
"clean",
|
|
1907
|
+
"restore",
|
|
1908
|
+
"rm",
|
|
1909
|
+
"mv"
|
|
1910
|
+
]);
|
|
1911
|
+
var BUN_INSTALLS = /* @__PURE__ */ new Set([
|
|
1912
|
+
"install",
|
|
1913
|
+
"i",
|
|
1914
|
+
"add",
|
|
1915
|
+
"remove",
|
|
1916
|
+
"rm",
|
|
1917
|
+
"update",
|
|
1918
|
+
"link"
|
|
1919
|
+
]);
|
|
1920
|
+
var PACKAGE_MUTATING = /* @__PURE__ */ new Set([
|
|
1921
|
+
"install",
|
|
1922
|
+
"i",
|
|
1923
|
+
"add",
|
|
1924
|
+
"remove",
|
|
1925
|
+
"publish",
|
|
1926
|
+
"ci"
|
|
1927
|
+
]);
|
|
1928
|
+
var DOCKER_MUTATING = /* @__PURE__ */ new Set([
|
|
1929
|
+
"compose",
|
|
1930
|
+
"build",
|
|
1931
|
+
"push",
|
|
1932
|
+
"run",
|
|
1933
|
+
"rm",
|
|
1934
|
+
"stop",
|
|
1935
|
+
"restart"
|
|
1936
|
+
]);
|
|
1937
|
+
var WRANGLER_MUTATING = /* @__PURE__ */ new Set([
|
|
1938
|
+
"deploy",
|
|
1939
|
+
"publish",
|
|
1940
|
+
"secret"
|
|
1941
|
+
]);
|
|
1942
|
+
|
|
1943
|
+
// src/hooks/shell/commands.ts
|
|
1944
|
+
var ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/u;
|
|
1945
|
+
function unwrap(words) {
|
|
1946
|
+
const assigned = [];
|
|
1947
|
+
let rest = [...words];
|
|
1948
|
+
for (; ; ) {
|
|
1949
|
+
if (rest.length > 0 && ASSIGNMENT.test(rest[0])) {
|
|
1950
|
+
assigned.push(rest[0].split("=")[0]);
|
|
1951
|
+
rest = rest.slice(1);
|
|
1952
|
+
continue;
|
|
1953
|
+
}
|
|
1954
|
+
if (rest.length > 0 && WRAPPERS.has(rest[0])) {
|
|
1955
|
+
rest = rest.slice(1);
|
|
1956
|
+
while (rest.length > 0 && (rest[0].startsWith("-") || /^\d+[smhd]?$/u.test(rest[0]))) {
|
|
1957
|
+
rest = rest.slice(1);
|
|
1958
|
+
}
|
|
1959
|
+
continue;
|
|
1960
|
+
}
|
|
1961
|
+
return { assigned, words: rest };
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
function commandsOf(script, depth = 0) {
|
|
1965
|
+
if (depth > 4) return null;
|
|
1966
|
+
const parsed = tokenize(script);
|
|
1967
|
+
if (!parsed) return null;
|
|
1968
|
+
const found = [];
|
|
1969
|
+
let dir = "";
|
|
1970
|
+
let current = [];
|
|
1971
|
+
const close = () => {
|
|
1972
|
+
if (current.length === 0) return true;
|
|
1973
|
+
const raw = current;
|
|
1974
|
+
const { assigned, words } = unwrap(current);
|
|
1975
|
+
current = [];
|
|
1976
|
+
if (words.length === 0) {
|
|
1977
|
+
found.push({ argv: [], raw, assigned, dir });
|
|
1978
|
+
return true;
|
|
1979
|
+
}
|
|
1980
|
+
if (words[0] === "cd") {
|
|
1981
|
+
dir = words[1] ?? "";
|
|
1982
|
+
return true;
|
|
1983
|
+
}
|
|
1984
|
+
const scriptAt = SHELLS.has(words[0]) ? words.findIndex((word) => /^-[a-z]*c$/u.test(word)) : -1;
|
|
1985
|
+
if (scriptAt > 0 || words[0] === "eval") {
|
|
1986
|
+
const source = words[0] === "eval" ? words.slice(1).join(" ") : words[scriptAt + 1] ?? "";
|
|
1987
|
+
const inner = commandsOf(source, depth + 1);
|
|
1988
|
+
if (!inner) return false;
|
|
1989
|
+
found.push(
|
|
1990
|
+
...inner.map((step) => ({
|
|
1991
|
+
...step,
|
|
1992
|
+
assigned: [...assigned, ...step.assigned]
|
|
1993
|
+
}))
|
|
1994
|
+
);
|
|
1995
|
+
return true;
|
|
1996
|
+
}
|
|
1997
|
+
found.push({ argv: words, raw, assigned, dir });
|
|
1998
|
+
return true;
|
|
1999
|
+
};
|
|
2000
|
+
for (const token of parsed.tokens) {
|
|
2001
|
+
if (!SEPARATORS.has(token)) {
|
|
2002
|
+
current.push(token);
|
|
2003
|
+
continue;
|
|
2004
|
+
}
|
|
2005
|
+
if (!close()) return null;
|
|
2006
|
+
}
|
|
2007
|
+
if (!close()) return null;
|
|
2008
|
+
for (const nested of parsed.nested) {
|
|
2009
|
+
const inner = commandsOf(nested, depth + 1);
|
|
2010
|
+
if (!inner) return null;
|
|
2011
|
+
found.push(...inner);
|
|
2012
|
+
}
|
|
2013
|
+
return found;
|
|
2014
|
+
}
|
|
2015
|
+
function gitCall(words) {
|
|
2016
|
+
let index = 1;
|
|
2017
|
+
let dir = "";
|
|
2018
|
+
while (index < words.length && words[index].startsWith("-")) {
|
|
2019
|
+
const option = words[index].split("=")[0];
|
|
2020
|
+
if (GIT_VALUE_OPTIONS.has(option) && !words[index].includes("=")) {
|
|
2021
|
+
if (option === "-C") dir = words[index + 1] ?? "";
|
|
2022
|
+
index += 2;
|
|
2023
|
+
continue;
|
|
2024
|
+
}
|
|
2025
|
+
index += 1;
|
|
2026
|
+
}
|
|
2027
|
+
return {
|
|
2028
|
+
sub: words[index] ?? "",
|
|
2029
|
+
next: words[index + 1] ?? "",
|
|
2030
|
+
at: index,
|
|
2031
|
+
dir
|
|
2032
|
+
};
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
// src/hooks/shell/classify.ts
|
|
2036
|
+
function runsScript(words, scripts) {
|
|
2037
|
+
return words[0] === "node" && words.slice(1).some((word) => scripts.some((script) => script.test(word)));
|
|
2038
|
+
}
|
|
2039
|
+
function touchesLock(words, scripts) {
|
|
2040
|
+
if (words[0] === "bun" && BUN_INSTALLS.has(words[1] ?? "")) return true;
|
|
2041
|
+
if (words[0] === "bun" && words[1] === "run" && /^git:(land|sync)$/u.test(words[2] ?? ""))
|
|
2042
|
+
return true;
|
|
2043
|
+
return runsScript(words, scripts);
|
|
2044
|
+
}
|
|
2045
|
+
function mutates(words, scripts = []) {
|
|
2046
|
+
const [name = "", sub = ""] = words;
|
|
2047
|
+
if (name === "git") {
|
|
2048
|
+
const call = gitCall(words);
|
|
2049
|
+
if (GIT_MUTATING.has(call.sub)) return true;
|
|
2050
|
+
return call.sub === "worktree" && ["add", "remove", "move"].includes(call.next);
|
|
2051
|
+
}
|
|
2052
|
+
if (touchesLock(words, scripts)) return true;
|
|
2053
|
+
if (name === "bun" && sub === "run")
|
|
2054
|
+
return /^(git:|docker:|deploy|release|publish)/u.test(words[2] ?? "");
|
|
2055
|
+
if (["npm", "pnpm", "yarn"].includes(name)) return PACKAGE_MUTATING.has(sub);
|
|
2056
|
+
if (name === "docker") return DOCKER_MUTATING.has(sub);
|
|
2057
|
+
if (name === "wrangler") return WRANGLER_MUTATING.has(sub);
|
|
2058
|
+
return false;
|
|
2059
|
+
}
|
|
2060
|
+
|
|
2061
|
+
// src/hooks/policies/_shared.ts
|
|
2062
|
+
function shellCall(input) {
|
|
2063
|
+
if (input.tool?.kind !== "shell") return null;
|
|
2064
|
+
const command6 = input.tool.command ?? "";
|
|
2065
|
+
if (command6.trim() === "") return null;
|
|
2066
|
+
return { command: command6, steps: input.tool.steps };
|
|
2067
|
+
}
|
|
2068
|
+
function program(word) {
|
|
2069
|
+
if (!word) return "";
|
|
2070
|
+
const slash = word.lastIndexOf("/");
|
|
2071
|
+
return slash < 0 ? word : word.slice(slash + 1);
|
|
2072
|
+
}
|
|
2073
|
+
function realSteps(steps) {
|
|
2074
|
+
return steps.filter((step) => step.argv.length > 0);
|
|
2075
|
+
}
|
|
2076
|
+
|
|
2077
|
+
// src/hooks/policies/types.ts
|
|
2078
|
+
var pass = {};
|
|
2079
|
+
function noOptions(raw) {
|
|
2080
|
+
const unknown = Object.keys(raw);
|
|
2081
|
+
if (unknown.length > 0) throw new Error(`unknown option ${unknown[0]}`);
|
|
2082
|
+
return {};
|
|
2083
|
+
}
|
|
2084
|
+
function stringList(raw, key) {
|
|
2085
|
+
const value = raw[key];
|
|
2086
|
+
if (value === void 0) return void 0;
|
|
2087
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) {
|
|
2088
|
+
throw new Error(`${key} must be a list of strings`);
|
|
2089
|
+
}
|
|
2090
|
+
return value;
|
|
2091
|
+
}
|
|
2092
|
+
function onlyKeys(raw, keys) {
|
|
2093
|
+
const unknown = Object.keys(raw).find((key) => !keys.includes(key));
|
|
2094
|
+
if (unknown) throw new Error(`unknown option ${unknown}`);
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
// src/hooks/policies/git/forced-branch-move.ts
|
|
2098
|
+
var FORCE = /* @__PURE__ */ new Set(["-f", "-M", "--force"]);
|
|
2099
|
+
var FALLBACK = /\bgit\b[^|;&\n]*\sbranch\b[^|;&\n]*\s(?:-f|-M|--force)(?:\s|$)/u;
|
|
2100
|
+
function forcedTarget(argv) {
|
|
2101
|
+
if (program(argv[0]) !== "git") return null;
|
|
2102
|
+
const call = gitCall(argv);
|
|
2103
|
+
if (call.sub !== "branch") return null;
|
|
2104
|
+
const args = argv.slice(call.at + 1);
|
|
2105
|
+
if (!args.some((arg) => FORCE.has(arg))) return null;
|
|
2106
|
+
const branch = args.find((arg) => !arg.startsWith("-"));
|
|
2107
|
+
return branch ? { branch, dir: call.dir } : null;
|
|
2108
|
+
}
|
|
2109
|
+
function checkedOutElsewhere(git, cwd, branch) {
|
|
2110
|
+
const here = git.toplevel(cwd);
|
|
2111
|
+
const other = git.worktrees(cwd).find((tree) => tree.branch === branch && tree.path !== here);
|
|
2112
|
+
return other?.path ?? null;
|
|
2113
|
+
}
|
|
2114
|
+
var forcedBranchMove = {
|
|
2115
|
+
id: "git.forced_branch_move",
|
|
2116
|
+
events: ["before_tool"],
|
|
2117
|
+
tools: ["shell"],
|
|
2118
|
+
fail: "closed",
|
|
2119
|
+
defaultMode: "deny",
|
|
2120
|
+
options: noOptions,
|
|
2121
|
+
evaluate(input, ctx) {
|
|
2122
|
+
const call = shellCall(input);
|
|
2123
|
+
if (!call) return pass;
|
|
2124
|
+
if (!call.steps) {
|
|
2125
|
+
if (!FALLBACK.test(call.command)) return pass;
|
|
2126
|
+
return {
|
|
2127
|
+
verdict: "deny",
|
|
2128
|
+
reason: "A forced branch move could not be read."
|
|
2129
|
+
};
|
|
2130
|
+
}
|
|
2131
|
+
for (const step of realSteps(call.steps)) {
|
|
2132
|
+
const target = forcedTarget(step.argv);
|
|
2133
|
+
if (!target) continue;
|
|
2134
|
+
const cwd = resolve7(input.cwd, step.dir, target.dir);
|
|
2135
|
+
if (!checkedOutElsewhere(ctx.git, cwd, target.branch)) continue;
|
|
2136
|
+
return {
|
|
2137
|
+
verdict: "deny",
|
|
2138
|
+
reason: `Branch ${target.branch} is checked out in another working tree. Moving it leaves that checkout's ref ahead of its files, so its status reports another session's work as deleted and committing removes it.`
|
|
2139
|
+
};
|
|
2140
|
+
}
|
|
2141
|
+
return pass;
|
|
2142
|
+
}
|
|
2143
|
+
};
|
|
2144
|
+
|
|
2145
|
+
// src/hooks/policies/git/gate-bypass-env.ts
|
|
2146
|
+
var REASON = "This takes a way around the gate that only the operator may take. Fix what the gate reports, or ask the operator.";
|
|
2147
|
+
function escape(text7) {
|
|
2148
|
+
return text7.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
2149
|
+
}
|
|
2150
|
+
function bypasses(step, options2) {
|
|
2151
|
+
if (step.assigned.some((name2) => options2.variables.includes(name2)))
|
|
2152
|
+
return true;
|
|
2153
|
+
const name = program(step.argv[0]);
|
|
2154
|
+
if (name === "export") {
|
|
2155
|
+
return step.argv.some(
|
|
2156
|
+
(word) => options2.variables.includes(word.split("=")[0])
|
|
2157
|
+
);
|
|
2158
|
+
}
|
|
2159
|
+
return options2.noVerify && name === "git" && step.argv.includes("--no-verify");
|
|
2160
|
+
}
|
|
2161
|
+
function fallback(command6, options2) {
|
|
2162
|
+
const names2 = options2.variables.map(escape);
|
|
2163
|
+
if (options2.noVerify) names2.push("--no-verify");
|
|
2164
|
+
if (names2.length === 0) return false;
|
|
2165
|
+
return new RegExp(`(?:^|[^\\w-])(?:${names2.join("|")})\\b`, "u").test(
|
|
2166
|
+
command6
|
|
2167
|
+
);
|
|
2168
|
+
}
|
|
2169
|
+
var gateBypassEnv = {
|
|
2170
|
+
id: "git.gate_bypass_env",
|
|
2171
|
+
events: ["before_tool"],
|
|
2172
|
+
tools: ["shell"],
|
|
2173
|
+
fail: "closed",
|
|
2174
|
+
defaultMode: "deny",
|
|
2175
|
+
options(raw) {
|
|
2176
|
+
onlyKeys(raw, ["names", "no_verify"]);
|
|
2177
|
+
const noVerify = raw["no_verify"] ?? true;
|
|
2178
|
+
if (typeof noVerify !== "boolean")
|
|
2179
|
+
throw new Error("no_verify must be a boolean");
|
|
2180
|
+
return { variables: stringList(raw, "names") ?? [], noVerify };
|
|
2181
|
+
},
|
|
2182
|
+
evaluate(input, ctx) {
|
|
2183
|
+
const call = shellCall(input);
|
|
2184
|
+
if (!call) return pass;
|
|
2185
|
+
const hit2 = call.steps ? call.steps.some((step) => bypasses(step, ctx.options)) : fallback(call.command, ctx.options);
|
|
2186
|
+
return hit2 ? { verdict: "deny", reason: REASON } : pass;
|
|
2187
|
+
}
|
|
2188
|
+
};
|
|
2189
|
+
|
|
2190
|
+
// src/hooks/policies/git/push-without-verify.ts
|
|
2191
|
+
var FALLBACK2 = /\bgit\b[^|;&\n]*\spush\b[^|;&\n]*\s(?:--no-verify|-n)(?:\s|$)/u;
|
|
2192
|
+
var REASON2 = "Pushing with verification disabled skips every pre-push check at once, including the ones that catch what a person cannot review by eye. Fix the failing check instead of waiving all of them.";
|
|
2193
|
+
function skipsVerify(argv) {
|
|
2194
|
+
if (program(argv[0]) !== "git") return false;
|
|
2195
|
+
const call = gitCall(argv);
|
|
2196
|
+
if (call.sub !== "push") return false;
|
|
2197
|
+
return argv.slice(call.at + 1).some((arg) => arg === "--no-verify" || arg === "-n");
|
|
2198
|
+
}
|
|
2199
|
+
var pushWithoutVerify = {
|
|
2200
|
+
id: "git.push_without_verify",
|
|
2201
|
+
events: ["before_tool"],
|
|
2202
|
+
tools: ["shell"],
|
|
2203
|
+
fail: "closed",
|
|
2204
|
+
defaultMode: "deny",
|
|
2205
|
+
options: noOptions,
|
|
2206
|
+
evaluate(input) {
|
|
2207
|
+
const call = shellCall(input);
|
|
2208
|
+
if (!call) return pass;
|
|
2209
|
+
const hit2 = call.steps ? realSteps(call.steps).some((step) => skipsVerify(step.argv)) : FALLBACK2.test(call.command);
|
|
2210
|
+
return hit2 ? { verdict: "deny", reason: REASON2 } : pass;
|
|
2211
|
+
}
|
|
2212
|
+
};
|
|
2213
|
+
|
|
2214
|
+
// src/hooks/policies/git/stage-all.ts
|
|
2215
|
+
var ADD_VALUES = /* @__PURE__ */ new Set(["--pathspec-from-file"]);
|
|
2216
|
+
var COMMIT_VALUES = /* @__PURE__ */ new Set([
|
|
2217
|
+
"--message",
|
|
2218
|
+
"--file",
|
|
2219
|
+
"--reuse-message",
|
|
2220
|
+
"--reedit-message",
|
|
2221
|
+
"--author",
|
|
2222
|
+
"--date",
|
|
2223
|
+
"--template",
|
|
2224
|
+
"--fixup",
|
|
2225
|
+
"--squash",
|
|
2226
|
+
"--trailer",
|
|
2227
|
+
"--cleanup",
|
|
2228
|
+
"--pathspec-from-file"
|
|
2229
|
+
]);
|
|
2230
|
+
var SHORT_VALUES = /* @__PURE__ */ new Set(["m", "F", "C", "c", "t"]);
|
|
2231
|
+
var ROOT_PATHS = /* @__PURE__ */ new Set([".", "./", ":/", ":(top)", ":(top)."]);
|
|
2232
|
+
var NAMES_GIT = /(?:^|[^\w.-])git(?![\w.-])/u;
|
|
2233
|
+
function stagesAll(args, commit) {
|
|
2234
|
+
let paths = false;
|
|
2235
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
2236
|
+
const arg = args[index];
|
|
2237
|
+
if (paths) {
|
|
2238
|
+
if (!commit && ROOT_PATHS.has(arg)) return true;
|
|
2239
|
+
continue;
|
|
2240
|
+
}
|
|
2241
|
+
if (arg === "--") {
|
|
2242
|
+
paths = true;
|
|
2243
|
+
continue;
|
|
2244
|
+
}
|
|
2245
|
+
if (arg === "--all") return true;
|
|
2246
|
+
const values = commit ? COMMIT_VALUES : ADD_VALUES;
|
|
2247
|
+
if (values.has(arg)) {
|
|
2248
|
+
index += 1;
|
|
2249
|
+
continue;
|
|
2250
|
+
}
|
|
2251
|
+
if (arg.startsWith("--")) continue;
|
|
2252
|
+
if (!arg.startsWith("-")) {
|
|
2253
|
+
if (!commit && ROOT_PATHS.has(arg)) return true;
|
|
2254
|
+
continue;
|
|
2255
|
+
}
|
|
2256
|
+
for (let at = 1; at < arg.length; at += 1) {
|
|
2257
|
+
const flag2 = arg[at];
|
|
2258
|
+
if (flag2 === (commit ? "a" : "A")) return true;
|
|
2259
|
+
if (commit && SHORT_VALUES.has(flag2)) {
|
|
2260
|
+
if (at === arg.length - 1) index += 1;
|
|
2261
|
+
break;
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
return false;
|
|
2266
|
+
}
|
|
2267
|
+
function stagesEverything(argv) {
|
|
2268
|
+
if (program(argv[0]) !== "git") return false;
|
|
2269
|
+
const call = gitCall(argv);
|
|
2270
|
+
if (call.sub !== "add" && call.sub !== "commit") return false;
|
|
2271
|
+
return stagesAll(argv.slice(call.at + 1), call.sub === "commit");
|
|
2272
|
+
}
|
|
2273
|
+
var stageAll = {
|
|
2274
|
+
id: "git.stage_all",
|
|
2275
|
+
events: ["before_tool"],
|
|
2276
|
+
tools: ["shell"],
|
|
2277
|
+
fail: "closed",
|
|
2278
|
+
defaultMode: "deny",
|
|
2279
|
+
options: noOptions,
|
|
2280
|
+
evaluate(input) {
|
|
2281
|
+
const call = shellCall(input);
|
|
2282
|
+
if (!call) return pass;
|
|
2283
|
+
if (!call.steps && !NAMES_GIT.test(call.command)) return pass;
|
|
2284
|
+
if (call.steps && !realSteps(call.steps).some((step) => stagesEverything(step.argv)))
|
|
2285
|
+
return pass;
|
|
2286
|
+
return {
|
|
2287
|
+
verdict: "deny",
|
|
2288
|
+
reason: "Stage explicit paths instead of staging everything; git.stage_all refuses this command."
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
};
|
|
2292
|
+
|
|
2293
|
+
// src/hooks/checks/after-edit.ts
|
|
2294
|
+
import { createHash } from "crypto";
|
|
2295
|
+
import { relative as relative2 } from "path";
|
|
2296
|
+
|
|
2297
|
+
// src/hooks/checks/_shared.ts
|
|
2298
|
+
function readState(input, ctx) {
|
|
2299
|
+
const raw = ctx.store.registry.get(input.session.id)?.["checks"];
|
|
2300
|
+
return {
|
|
2301
|
+
validators: raw?.validators ?? {},
|
|
2302
|
+
edited: raw?.edited === true,
|
|
2303
|
+
stopTurn: raw?.stopTurn,
|
|
2304
|
+
pending: raw?.pending ?? [],
|
|
2305
|
+
projectorReported: raw?.projectorReported === true
|
|
2306
|
+
};
|
|
2307
|
+
}
|
|
2308
|
+
function writeState(input, ctx, update) {
|
|
2309
|
+
ctx.store.registry.upsert(input.session.id, {
|
|
2310
|
+
checks: { ...readState(input, ctx), ...update }
|
|
2311
|
+
});
|
|
2312
|
+
}
|
|
2313
|
+
|
|
2314
|
+
// src/hooks/checks/command.ts
|
|
2315
|
+
import { spawnSync } from "child_process";
|
|
2316
|
+
function runCheck(command6, root, timeout) {
|
|
2317
|
+
if (timeout <= 0) return false;
|
|
2318
|
+
const result = spawnSync(command6[0], command6.slice(1), {
|
|
2319
|
+
cwd: root,
|
|
2320
|
+
stdio: "ignore",
|
|
2321
|
+
timeout,
|
|
2322
|
+
killSignal: "SIGKILL",
|
|
2323
|
+
shell: false
|
|
2324
|
+
});
|
|
2325
|
+
return !result.error && result.status === 0;
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
// src/hooks/checks/options.ts
|
|
2329
|
+
import { isAbsolute as isAbsolute7 } from "path";
|
|
2330
|
+
function report(raw) {
|
|
2331
|
+
if (raw["report"] !== void 0 && raw["report"] !== "context")
|
|
2332
|
+
throw new Error("report must be context");
|
|
2333
|
+
}
|
|
2334
|
+
function afterEditOptions(raw) {
|
|
2335
|
+
onlyKeys(raw, ["validator_glob", "command", "debounce_seconds", "report"]);
|
|
2336
|
+
report(raw);
|
|
2337
|
+
const glob = raw["validator_glob"];
|
|
2338
|
+
const command6 = raw["command"];
|
|
2339
|
+
const seconds = raw["debounce_seconds"] ?? 60;
|
|
2340
|
+
if (typeof glob !== "string" || !glob || isAbsolute7(glob) || glob.split("/").includes(".."))
|
|
2341
|
+
throw new Error("validator_glob must stay inside the project");
|
|
2342
|
+
if (!Array.isArray(command6) || command6.length === 0 || !command6.every(
|
|
2343
|
+
(part) => typeof part === "string" && part.length > 0 && !part.includes("\0")
|
|
2344
|
+
) || !command6.some((part) => part.includes("{validator}")))
|
|
2345
|
+
throw new Error("command must be argv containing {validator}");
|
|
2346
|
+
if (typeof seconds !== "number" || !Number.isFinite(seconds) || seconds < 0)
|
|
2347
|
+
throw new Error("debounce_seconds must be nonnegative");
|
|
2348
|
+
return {
|
|
2349
|
+
validatorGlob: glob,
|
|
2350
|
+
command: command6,
|
|
2351
|
+
debounceMs: seconds * 1e3
|
|
2352
|
+
};
|
|
2353
|
+
}
|
|
2354
|
+
function stopOptions(raw) {
|
|
2355
|
+
onlyKeys(raw, ["project_check", "report"]);
|
|
2356
|
+
report(raw);
|
|
2357
|
+
const enabled = raw["project_check"] ?? true;
|
|
2358
|
+
if (typeof enabled !== "boolean")
|
|
2359
|
+
throw new Error("project_check must be a boolean");
|
|
2360
|
+
return { projectCheck: enabled };
|
|
2361
|
+
}
|
|
2362
|
+
|
|
2363
|
+
// src/hooks/checks/validators.ts
|
|
2364
|
+
import { globSync, realpathSync, statSync } from "fs";
|
|
2365
|
+
import { dirname, isAbsolute as isAbsolute8, relative, resolve as resolve8, sep } from "path";
|
|
2366
|
+
function contains(root, target) {
|
|
2367
|
+
const rest = relative(root, target);
|
|
2368
|
+
return !isAbsolute8(rest) && rest !== ".." && !rest.startsWith(`..${sep}`);
|
|
2369
|
+
}
|
|
2370
|
+
function owner(validator) {
|
|
2371
|
+
const parts = validator.split(sep);
|
|
2372
|
+
const at = parts.findIndex((part) => part === ".agents" || part === ".agent");
|
|
2373
|
+
return at < 0 ? dirname(validator) : parts.slice(0, at).join(sep) || sep;
|
|
2374
|
+
}
|
|
2375
|
+
function validators(root, pattern, paths) {
|
|
2376
|
+
const realRoot = realpathSync(root);
|
|
2377
|
+
const edits = paths.map((path) => resolve8(path)).filter((path) => contains(root, path));
|
|
2378
|
+
if (edits.length === 0) return [];
|
|
2379
|
+
const candidates = [
|
|
2380
|
+
...globSync(pattern, {
|
|
2381
|
+
cwd: root,
|
|
2382
|
+
exclude: ["**/node_modules/**", "**/.git/**"]
|
|
2383
|
+
})
|
|
2384
|
+
].map((path) => resolve8(root, path)).filter((path) => {
|
|
2385
|
+
if (!contains(realRoot, realpathSync(path)) || !statSync(path).isFile())
|
|
2386
|
+
return false;
|
|
2387
|
+
return edits.some((edit) => contains(owner(path), edit));
|
|
2388
|
+
});
|
|
2389
|
+
return [
|
|
2390
|
+
...new Set(
|
|
2391
|
+
candidates.filter(
|
|
2392
|
+
(candidate) => edits.some((edit) => {
|
|
2393
|
+
const base = owner(candidate);
|
|
2394
|
+
if (!contains(base, edit)) return false;
|
|
2395
|
+
return !candidates.some(
|
|
2396
|
+
(other) => owner(other).length > base.length && contains(owner(other), edit)
|
|
2397
|
+
);
|
|
2398
|
+
})
|
|
2399
|
+
)
|
|
2400
|
+
)
|
|
2401
|
+
].sort();
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
// src/hooks/checks/after-edit.ts
|
|
2405
|
+
var afterEdit = {
|
|
2406
|
+
id: "checks.after_edit",
|
|
2407
|
+
events: ["after_tool"],
|
|
2408
|
+
tools: ["edit"],
|
|
2409
|
+
fail: "open",
|
|
2410
|
+
defaultMode: "warn",
|
|
2411
|
+
options: afterEditOptions,
|
|
2412
|
+
evaluate(input, ctx) {
|
|
2413
|
+
const selected = validators(
|
|
2414
|
+
input.projectRoot,
|
|
2415
|
+
ctx.options.validatorGlob,
|
|
2416
|
+
input.tool?.paths ?? []
|
|
2417
|
+
);
|
|
2418
|
+
const context = [];
|
|
2419
|
+
const deadline = Date.now() + 5e3;
|
|
2420
|
+
for (const validator of selected) {
|
|
2421
|
+
const command6 = ctx.options.command.map(
|
|
2422
|
+
(part) => part.replaceAll("{validator}", validator)
|
|
2423
|
+
);
|
|
2424
|
+
const key = createHash("sha256").update(JSON.stringify(command6)).digest("hex");
|
|
2425
|
+
const stamps = readState(input, ctx).validators;
|
|
2426
|
+
const now = Date.now();
|
|
2427
|
+
if (stamps[key] !== void 0 && now >= stamps[key] && now - stamps[key] < ctx.options.debounceMs)
|
|
2428
|
+
continue;
|
|
2429
|
+
const kept = Object.entries({ ...stamps, [key]: now }).slice(-128);
|
|
2430
|
+
writeState(input, ctx, { validators: Object.fromEntries(kept) });
|
|
2431
|
+
if (runCheck(command6, input.projectRoot, deadline - Date.now())) continue;
|
|
2432
|
+
ctx.log.error("validator check failed", {
|
|
2433
|
+
validator: relative2(input.projectRoot, validator)
|
|
2434
|
+
});
|
|
2435
|
+
if (context.length < 20)
|
|
2436
|
+
context.push(
|
|
2437
|
+
`pmcp after_edit: validation failed for ${relative2(input.projectRoot, validator)}; run the configured validator to inspect its output.`
|
|
2438
|
+
);
|
|
2439
|
+
}
|
|
2440
|
+
return context.length ? { context } : pass;
|
|
2441
|
+
}
|
|
2442
|
+
};
|
|
2443
|
+
|
|
2444
|
+
// src/hooks/checks/projection.ts
|
|
2445
|
+
import { spawnSync as spawnSync2 } from "child_process";
|
|
2446
|
+
import { existsSync, readFileSync } from "fs";
|
|
2447
|
+
import { join } from "path";
|
|
2448
|
+
var LOCK_VERSION_KEY = "pmcp_version";
|
|
2449
|
+
var PROBLEM = /^x (?:drift|stale|undeclared|dead glob|conflict): /mu;
|
|
2450
|
+
function run(command6, root, deadline) {
|
|
2451
|
+
return spawnSync2(command6[0], command6.slice(1), {
|
|
2452
|
+
cwd: root,
|
|
2453
|
+
encoding: "utf8",
|
|
2454
|
+
timeout: Math.max(1, deadline - Date.now()),
|
|
2455
|
+
killSignal: "SIGKILL",
|
|
2456
|
+
maxBuffer: 1024 * 1024,
|
|
2457
|
+
shell: false
|
|
2458
|
+
});
|
|
2459
|
+
}
|
|
2460
|
+
function readJson(path) {
|
|
2461
|
+
try {
|
|
2462
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
2463
|
+
} catch {
|
|
2464
|
+
return null;
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
function findProjector(root, stateDir, deadline) {
|
|
2468
|
+
const cli = localCli(root) ?? recordedCli(root, stateDir);
|
|
2469
|
+
if (!cli || !existsSync(cli))
|
|
2470
|
+
return { missing: cli ?? "no local or recorded pmcp" };
|
|
2471
|
+
const expected = readJson(join(root, "pmcp.lock"))?.[LOCK_VERSION_KEY];
|
|
2472
|
+
const answer = run([process.execPath, cli, "--version"], root, deadline);
|
|
2473
|
+
if (answer.error || answer.stdout.trim() !== expected)
|
|
2474
|
+
return { missing: cli };
|
|
2475
|
+
return { cli };
|
|
2476
|
+
}
|
|
2477
|
+
function projectionState(root, cli, deadline) {
|
|
2478
|
+
const result = run(
|
|
2479
|
+
[process.execPath, cli, "project", "--check"],
|
|
2480
|
+
root,
|
|
2481
|
+
deadline
|
|
2482
|
+
);
|
|
2483
|
+
if (result.error) return "unavailable";
|
|
2484
|
+
if (result.status === 0) return "clean";
|
|
2485
|
+
return PROBLEM.test(`${result.stdout}${result.stderr}`) ? "drift" : "unavailable";
|
|
2486
|
+
}
|
|
2487
|
+
|
|
2488
|
+
// src/hooks/checks/on-stop.ts
|
|
2489
|
+
function notify(input, ctx, context, update = {}) {
|
|
2490
|
+
if (input.runtime === "claude" && input.session.turnId) {
|
|
2491
|
+
writeState(input, ctx, update);
|
|
2492
|
+
return { context };
|
|
2493
|
+
}
|
|
2494
|
+
writeState(input, ctx, { ...update, pending: context });
|
|
2495
|
+
return pass;
|
|
2496
|
+
}
|
|
2497
|
+
var onStop = {
|
|
2498
|
+
id: "checks.on_stop",
|
|
2499
|
+
events: ["after_tool", "stop", "prompt"],
|
|
2500
|
+
tools: ["edit"],
|
|
2501
|
+
fail: "open",
|
|
2502
|
+
defaultMode: "warn",
|
|
2503
|
+
options: stopOptions,
|
|
2504
|
+
evaluate(input, ctx) {
|
|
2505
|
+
if (!ctx.options.projectCheck) return pass;
|
|
2506
|
+
const state = readState(input, ctx);
|
|
2507
|
+
if (input.event === "after_tool") {
|
|
2508
|
+
writeState(input, ctx, { edited: true });
|
|
2509
|
+
return pass;
|
|
2510
|
+
}
|
|
2511
|
+
if (input.event === "prompt") {
|
|
2512
|
+
if (!state.pending.length) return pass;
|
|
2513
|
+
writeState(input, ctx, { pending: [] });
|
|
2514
|
+
return { context: state.pending };
|
|
2515
|
+
}
|
|
2516
|
+
if (input.stop?.active || !state.edited) return pass;
|
|
2517
|
+
const turn = input.session.turnId;
|
|
2518
|
+
if (turn && state.stopTurn === turn) return pass;
|
|
2519
|
+
writeState(input, ctx, { edited: false, stopTurn: turn });
|
|
2520
|
+
const deadline = Date.now() + 5e3;
|
|
2521
|
+
const projector = findProjector(input.projectRoot, ctx.store.dir, deadline);
|
|
2522
|
+
if ("missing" in projector) {
|
|
2523
|
+
ctx.log.error("projecting pmcp not available");
|
|
2524
|
+
if (state.projectorReported) return pass;
|
|
2525
|
+
return notify(
|
|
2526
|
+
input,
|
|
2527
|
+
ctx,
|
|
2528
|
+
[`on_stop: projecting pmcp not available (${projector.missing})`],
|
|
2529
|
+
{ projectorReported: true }
|
|
2530
|
+
);
|
|
2531
|
+
}
|
|
2532
|
+
const projection = projectionState(
|
|
2533
|
+
input.projectRoot,
|
|
2534
|
+
projector.cli,
|
|
2535
|
+
deadline
|
|
2536
|
+
);
|
|
2537
|
+
if (projection === "clean") return pass;
|
|
2538
|
+
if (projection === "unavailable") {
|
|
2539
|
+
ctx.log.error("projection check could not run");
|
|
2540
|
+
return pass;
|
|
2541
|
+
}
|
|
2542
|
+
ctx.log.error("projection drift check failed");
|
|
2543
|
+
return notify(input, ctx, [
|
|
2544
|
+
"pmcp on_stop: project projection check failed; run pmcp project --check to inspect drift."
|
|
2545
|
+
]);
|
|
2546
|
+
}
|
|
2547
|
+
};
|
|
2548
|
+
|
|
2549
|
+
// src/hooks/policies/sessions/broadcast.ts
|
|
2550
|
+
var DELIVERABLE = [
|
|
2551
|
+
"session_start",
|
|
2552
|
+
"prompt",
|
|
2553
|
+
"before_tool",
|
|
2554
|
+
"after_tool",
|
|
2555
|
+
"stop"
|
|
2556
|
+
];
|
|
2557
|
+
function stopAllowed(input, ctx) {
|
|
2558
|
+
if (input.stop?.active) return false;
|
|
2559
|
+
const turn = input.session.turnId;
|
|
2560
|
+
if (!turn) return false;
|
|
2561
|
+
const record = ctx.store.registry.get(input.session.id);
|
|
2562
|
+
return record?.stopNoticeTurn !== turn;
|
|
2563
|
+
}
|
|
2564
|
+
var broadcast = {
|
|
2565
|
+
id: "sessions.broadcast",
|
|
2566
|
+
events: DELIVERABLE,
|
|
2567
|
+
tools: ["shell", "edit"],
|
|
2568
|
+
fail: "open",
|
|
2569
|
+
defaultMode: "warn",
|
|
2570
|
+
options(raw) {
|
|
2571
|
+
onlyKeys(raw, ["keep", "deliver_on"]);
|
|
2572
|
+
const deliverOn = stringList(raw, "deliver_on") ?? [
|
|
2573
|
+
"session_start",
|
|
2574
|
+
"prompt",
|
|
2575
|
+
"before_tool"
|
|
2576
|
+
];
|
|
2577
|
+
const bad = deliverOn.find(
|
|
2578
|
+
(event) => !NEUTRAL_EVENTS.includes(event) || !DELIVERABLE.includes(event)
|
|
2579
|
+
);
|
|
2580
|
+
if (bad) throw new Error(`deliver_on cannot include ${bad}`);
|
|
2581
|
+
return { deliverOn };
|
|
2582
|
+
},
|
|
2583
|
+
eventsFor: (options2) => options2.deliverOn,
|
|
2584
|
+
evaluate(input, ctx) {
|
|
2585
|
+
const session = input.session.id;
|
|
2586
|
+
if (!session || !ctx.options.deliverOn.includes(input.event)) return pass;
|
|
2587
|
+
if (ctx.store.mailbox.settings.enabled) return pass;
|
|
2588
|
+
if (input.event === "stop" && !stopAllowed(input, ctx)) return pass;
|
|
2589
|
+
const unseen = ctx.store.broadcast.unseen(session);
|
|
2590
|
+
if (unseen.length === 0) return pass;
|
|
2591
|
+
if (input.event === "stop")
|
|
2592
|
+
ctx.store.registry.upsert(session, {
|
|
2593
|
+
stopNoticeTurn: input.session.turnId
|
|
2594
|
+
});
|
|
2595
|
+
return {
|
|
2596
|
+
context: unseen.map(
|
|
2597
|
+
(entry) => `Operator broadcast (${entry.since}): ${entry.text}`
|
|
2598
|
+
)
|
|
2599
|
+
};
|
|
2600
|
+
}
|
|
2601
|
+
};
|
|
2602
|
+
|
|
2603
|
+
// src/hooks/policies/sessions/leases.ts
|
|
2604
|
+
function argvList(value, where) {
|
|
2605
|
+
const valid = Array.isArray(value) && value.every(
|
|
2606
|
+
(item) => Array.isArray(item) && item.length > 0 && item.every((word) => typeof word === "string")
|
|
2607
|
+
);
|
|
2608
|
+
if (!valid) throw new Error(`${where} must be a list of argv prefixes`);
|
|
2609
|
+
return value;
|
|
2610
|
+
}
|
|
2611
|
+
function startsWith(argv, prefix) {
|
|
2612
|
+
if (argv.length < prefix.length) return false;
|
|
2613
|
+
return prefix.every(
|
|
2614
|
+
(word, at) => at === 0 ? program(argv[0]) === word : argv[at] === word
|
|
2615
|
+
);
|
|
2616
|
+
}
|
|
2617
|
+
function blocked(steps, command6, rule) {
|
|
2618
|
+
if (!steps)
|
|
2619
|
+
return rule.blocks.some((prefix) => command6.includes(prefix.join(" ")));
|
|
2620
|
+
return realSteps(steps).some(
|
|
2621
|
+
(step) => rule.blocks.some((prefix) => startsWith(step.argv, prefix))
|
|
2622
|
+
);
|
|
2623
|
+
}
|
|
2624
|
+
var leases = {
|
|
2625
|
+
id: "sessions.leases",
|
|
2626
|
+
events: ["before_tool"],
|
|
2627
|
+
tools: ["shell"],
|
|
2628
|
+
fail: "closed",
|
|
2629
|
+
defaultMode: "deny",
|
|
2630
|
+
options(raw) {
|
|
2631
|
+
return Object.entries(raw).map(([name, value]) => {
|
|
2632
|
+
const table3 = value ?? {};
|
|
2633
|
+
onlyKeys(table3, ["path", "blocks"]);
|
|
2634
|
+
if (table3["path"] !== void 0 && typeof table3["path"] !== "string")
|
|
2635
|
+
throw new Error(`${name}.path must be a string`);
|
|
2636
|
+
return {
|
|
2637
|
+
name,
|
|
2638
|
+
path: table3["path"],
|
|
2639
|
+
blocks: argvList(table3["blocks"], `${name}.blocks`)
|
|
2640
|
+
};
|
|
2641
|
+
});
|
|
2642
|
+
},
|
|
2643
|
+
evaluate(input, ctx) {
|
|
2644
|
+
const call = shellCall(input);
|
|
2645
|
+
if (!call) return pass;
|
|
2646
|
+
for (const rule of ctx.options) {
|
|
2647
|
+
if (!blocked(call.steps, call.command, rule)) continue;
|
|
2648
|
+
const lease = ctx.store.leases.read(rule.name);
|
|
2649
|
+
if (!lease) continue;
|
|
2650
|
+
return {
|
|
2651
|
+
verdict: "deny",
|
|
2652
|
+
reason: `A ${rule.path ?? rule.name} change is landing (${lease.subject}, since ${lease.since}). This command waits for it; retry when it has landed.`
|
|
2653
|
+
};
|
|
2654
|
+
}
|
|
2655
|
+
return pass;
|
|
2656
|
+
}
|
|
2657
|
+
};
|
|
2658
|
+
|
|
2659
|
+
// src/hooks/policies/sessions/mailbox.ts
|
|
2660
|
+
function rolesOf(agentType, options2) {
|
|
2661
|
+
if (!agentType) return [];
|
|
2662
|
+
const named2 = Object.entries(options2.roles).filter(([, type]) => agentType === type || agentType.endsWith(`:${type}`)).map(([name]) => name);
|
|
2663
|
+
return [.../* @__PURE__ */ new Set([...named2, agentType.split(":").at(-1)])];
|
|
2664
|
+
}
|
|
2665
|
+
function recipientOf(input, ctx) {
|
|
2666
|
+
const session = input.session.id;
|
|
2667
|
+
const record = ctx.store.registry.get(session);
|
|
2668
|
+
const roles2 = rolesOf(input.session.agentType, ctx.options);
|
|
2669
|
+
if (roles2.length > 0 && JSON.stringify(record?.["roles"]) !== JSON.stringify(roles2))
|
|
2670
|
+
ctx.store.registry.upsert(session, { roles: roles2 });
|
|
2671
|
+
return {
|
|
2672
|
+
key: inboxKey({ session }),
|
|
2673
|
+
session,
|
|
2674
|
+
...record?.alias ? { alias: record.alias } : {},
|
|
2675
|
+
roles: roles2,
|
|
2676
|
+
desk: record?.worktree || input.cwd,
|
|
2677
|
+
// The registry records a session at its start, so earlier broadcasts are not its news.
|
|
2678
|
+
...record?.startedAt ? { since: record.startedAt } : {}
|
|
2679
|
+
};
|
|
2680
|
+
}
|
|
2681
|
+
var WAKE_PREFIX = "pmcp mail:";
|
|
2682
|
+
function heldForWake(input, ctx) {
|
|
2683
|
+
const record = ctx.store.registry.get(input.session.id);
|
|
2684
|
+
if (!record || !ptyRef(record)) return false;
|
|
2685
|
+
const wake = input.event === "prompt" && (input.prompt ?? "").trimStart().startsWith(WAKE_PREFIX);
|
|
2686
|
+
return !wake;
|
|
2687
|
+
}
|
|
2688
|
+
var mailbox = {
|
|
2689
|
+
id: "sessions.mailbox",
|
|
2690
|
+
events: ["session_start", "prompt", "before_tool", "after_tool"],
|
|
2691
|
+
fail: "open",
|
|
2692
|
+
defaultMode: "warn",
|
|
2693
|
+
options(raw) {
|
|
2694
|
+
const roles2 = raw["roles"] ?? {};
|
|
2695
|
+
return {
|
|
2696
|
+
roles: Object.fromEntries(
|
|
2697
|
+
Object.entries(roles2).map(([name, value]) => {
|
|
2698
|
+
const type = value?.["agent_type"];
|
|
2699
|
+
return [name, typeof type === "string" ? type : name];
|
|
2700
|
+
})
|
|
2701
|
+
)
|
|
2702
|
+
};
|
|
2703
|
+
},
|
|
2704
|
+
evaluate(input, ctx) {
|
|
2705
|
+
const box = ctx.store.mailbox;
|
|
2706
|
+
if (!input.session.id || !box.settings.enabled) return pass;
|
|
2707
|
+
if (heldForWake(input, ctx)) return pass;
|
|
2708
|
+
const recipient = recipientOf(input, ctx);
|
|
2709
|
+
const unacked = box.inbox(recipient, Date.now());
|
|
2710
|
+
const fresh = unacked.filter(
|
|
2711
|
+
(message) => !box.delivered(recipient.key, message.id)
|
|
2712
|
+
);
|
|
2713
|
+
if (fresh.length === 0) return pass;
|
|
2714
|
+
box.markDelivered(
|
|
2715
|
+
recipient.key,
|
|
2716
|
+
fresh.map((message) => message.id)
|
|
2717
|
+
);
|
|
2718
|
+
const broadcasts = fresh.map((message) => message.broadcastId).filter(Boolean);
|
|
2719
|
+
if (broadcasts.length > 0)
|
|
2720
|
+
ctx.store.registry.upsert(input.session.id, {
|
|
2721
|
+
seenBroadcast: broadcasts.sort((a, b) => Number(a) - Number(b)).at(-1)
|
|
2722
|
+
});
|
|
2723
|
+
return {
|
|
2724
|
+
context: [
|
|
2725
|
+
noticeOf(
|
|
2726
|
+
fresh,
|
|
2727
|
+
unacked.length - fresh.length,
|
|
2728
|
+
box.settings.maxDelivered,
|
|
2729
|
+
input.session.id,
|
|
2730
|
+
// The hook runs with the session's PATH, so this is the pmcp its shell would find.
|
|
2731
|
+
pmcpCommand(input.projectRoot, ctx.store.dir, process.env["PATH"])
|
|
2732
|
+
)
|
|
2733
|
+
]
|
|
2734
|
+
};
|
|
2735
|
+
}
|
|
2736
|
+
};
|
|
2737
|
+
|
|
2738
|
+
// src/hooks/policies/sessions/pause.ts
|
|
2739
|
+
var MUTATING = /\b(git\s+(commit|push|merge|rebase|reset|checkout|switch|cherry-pick|revert|am|stash|tag|clean|restore|rm|mv|worktree\s+(add|remove|move))|bun\s+(install|i|add|remove|rm|update|link)\b|(npm|pnpm|yarn)\s+(install|i|add|remove|publish|ci)\b|docker\s+(compose|build|push|run|rm|stop|restart)|wrangler\s+(deploy|publish|secret))\b/u;
|
|
2740
|
+
function changes(input, options2) {
|
|
2741
|
+
const tool = input.tool;
|
|
2742
|
+
if (tool?.kind === "edit") return true;
|
|
2743
|
+
if (tool?.kind !== "shell") return false;
|
|
2744
|
+
if (!tool.steps) {
|
|
2745
|
+
const command6 = tool.command ?? "";
|
|
2746
|
+
return MUTATING.test(command6) || options2.scripts.some((s) => s.test(command6));
|
|
2747
|
+
}
|
|
2748
|
+
return realSteps(tool.steps).some(
|
|
2749
|
+
(step) => mutates(step.argv, options2.scripts)
|
|
2750
|
+
);
|
|
2751
|
+
}
|
|
2752
|
+
function foreign(pause2, input, ctx) {
|
|
2753
|
+
const session = input.session.id;
|
|
2754
|
+
if (pause2.by === session) return false;
|
|
2755
|
+
const alias = ctx.store.registry.get(session)?.alias;
|
|
2756
|
+
return !alias || pause2.by !== alias;
|
|
2757
|
+
}
|
|
2758
|
+
var pause = {
|
|
2759
|
+
id: "sessions.pause",
|
|
2760
|
+
events: ["session_start", "prompt", "before_tool"],
|
|
2761
|
+
tools: ["shell", "edit"],
|
|
2762
|
+
fail: "closed",
|
|
2763
|
+
defaultMode: "deny",
|
|
2764
|
+
options(raw) {
|
|
2765
|
+
onlyKeys(raw, ["refuses", "scripts"]);
|
|
2766
|
+
if (raw["refuses"] !== void 0 && raw["refuses"] !== "mutating")
|
|
2767
|
+
throw new Error("refuses must be mutating");
|
|
2768
|
+
const scripts = (stringList(raw, "scripts") ?? []).map((source) => {
|
|
2769
|
+
try {
|
|
2770
|
+
return new RegExp(source, "u");
|
|
2771
|
+
} catch {
|
|
2772
|
+
throw new Error(`scripts: ${source} is not a regular expression`);
|
|
2773
|
+
}
|
|
2774
|
+
});
|
|
2775
|
+
return { scripts };
|
|
2776
|
+
},
|
|
2777
|
+
evaluate(input, ctx) {
|
|
2778
|
+
const current = ctx.store.pause.read();
|
|
2779
|
+
if (!current || !foreign(current, input, ctx)) return pass;
|
|
2780
|
+
if (input.event !== "before_tool") {
|
|
2781
|
+
return {
|
|
2782
|
+
context: [
|
|
2783
|
+
`The workspace is paused (${current.reason}, since ${current.since}). Edits and mutating commands are refused until the operator resumes it; reading and planning continue.`
|
|
2784
|
+
]
|
|
2785
|
+
};
|
|
2786
|
+
}
|
|
2787
|
+
if (!changes(input, ctx.options)) return pass;
|
|
2788
|
+
return {
|
|
2789
|
+
verdict: "deny",
|
|
2790
|
+
reason: `The workspace is paused (${current.reason}, since ${current.since}). Changes, deliveries, installs and deploys wait until the operator runs pmcp sessions resume; reading and planning continue.`
|
|
2791
|
+
};
|
|
2792
|
+
}
|
|
2793
|
+
};
|
|
2794
|
+
|
|
2795
|
+
// src/hooks/policies/sessions/pty-events.ts
|
|
2796
|
+
import { createConnection } from "net";
|
|
2797
|
+
var TIMEOUT_MS = 500;
|
|
2798
|
+
function kindOf(input) {
|
|
2799
|
+
if (input.event === "session_start") return "session_start";
|
|
2800
|
+
if (input.event === "prompt") return "turn_start";
|
|
2801
|
+
if (input.event === "stop") return "turn_end";
|
|
2802
|
+
const note = `${input.notification?.type ?? ""} ${input.notification?.message ?? ""}`;
|
|
2803
|
+
if (input.event === "notification" && /permission/iu.test(note))
|
|
2804
|
+
return "permission";
|
|
2805
|
+
return null;
|
|
2806
|
+
}
|
|
2807
|
+
function messageOf(input) {
|
|
2808
|
+
if (input.event === "stop") return input.stop?.lastMessage ?? "";
|
|
2809
|
+
if (input.event === "notification") return input.notification?.message;
|
|
2810
|
+
return void 0;
|
|
2811
|
+
}
|
|
2812
|
+
function notifyHost(record, input) {
|
|
2813
|
+
const ref = record ? ptyRef(record) : null;
|
|
2814
|
+
const kind = kindOf(input);
|
|
2815
|
+
if (!ref || !kind) return;
|
|
2816
|
+
const line = JSON.stringify({
|
|
2817
|
+
op: "event",
|
|
2818
|
+
id: ref.session,
|
|
2819
|
+
kind,
|
|
2820
|
+
message: messageOf(input),
|
|
2821
|
+
session: input.session.id,
|
|
2822
|
+
expect: { instance: ref.instance }
|
|
2823
|
+
});
|
|
2824
|
+
const client = createConnection(ref.socket);
|
|
2825
|
+
client.setTimeout(TIMEOUT_MS, () => client.destroy());
|
|
2826
|
+
client.on("error", () => client.destroy());
|
|
2827
|
+
client.on("connect", () => client.end(`${line}
|
|
2828
|
+
`));
|
|
2829
|
+
}
|
|
2830
|
+
|
|
2831
|
+
// src/hooks/policies/sessions/registry.ts
|
|
2832
|
+
var ALL_EVENTS = [
|
|
2833
|
+
"session_start",
|
|
2834
|
+
"prompt",
|
|
2835
|
+
"before_tool",
|
|
2836
|
+
"after_tool",
|
|
2837
|
+
"stop",
|
|
2838
|
+
"session_end",
|
|
2839
|
+
"notification"
|
|
2840
|
+
];
|
|
2841
|
+
function fields(input) {
|
|
2842
|
+
const cwd = { cwd: input.cwd };
|
|
2843
|
+
if (input.event === "session_start")
|
|
2844
|
+
return { ...cwd, status: "idle", runtime: input.runtime };
|
|
2845
|
+
if (input.event === "prompt")
|
|
2846
|
+
return { ...cwd, status: "working", waiting: "" };
|
|
2847
|
+
if (input.event === "after_tool") return { ...cwd, status: "working" };
|
|
2848
|
+
if (input.event === "notification") {
|
|
2849
|
+
const reason = input.notification?.type || input.notification?.message;
|
|
2850
|
+
return { status: "waiting", waiting: reason ?? "" };
|
|
2851
|
+
}
|
|
2852
|
+
if (input.event === "stop") {
|
|
2853
|
+
return {
|
|
2854
|
+
status: "idle",
|
|
2855
|
+
waiting: "",
|
|
2856
|
+
lastMessage: (input.stop?.lastMessage ?? "").slice(0, 200)
|
|
2857
|
+
};
|
|
2858
|
+
}
|
|
2859
|
+
const tool = input.tool;
|
|
2860
|
+
const action = tool?.command ? `${tool.native}: ${tool.command.slice(0, 80)}` : tool?.native ?? "";
|
|
2861
|
+
return { ...cwd, status: "working", lastAction: action };
|
|
2862
|
+
}
|
|
2863
|
+
var registry = {
|
|
2864
|
+
id: "sessions.registry",
|
|
2865
|
+
events: ALL_EVENTS,
|
|
2866
|
+
tools: ["shell", "edit"],
|
|
2867
|
+
fail: "open",
|
|
2868
|
+
defaultMode: "warn",
|
|
2869
|
+
options: noOptions,
|
|
2870
|
+
evaluate(input, ctx) {
|
|
2871
|
+
const session = input.session.id;
|
|
2872
|
+
if (!session) return pass;
|
|
2873
|
+
if (input.event === "session_end") {
|
|
2874
|
+
ctx.store.registry.remove(session);
|
|
2875
|
+
return pass;
|
|
2876
|
+
}
|
|
2877
|
+
const known = ctx.store.registry.get(session);
|
|
2878
|
+
const starting = known?.pid === void 0 || input.event === "session_start";
|
|
2879
|
+
const runtime = starting ? runtimePid() : void 0;
|
|
2880
|
+
const pid = runtime === void 0 ? {} : { pid: runtime };
|
|
2881
|
+
const hosted = runtime === void 0 ? {} : ptyFields(process.env, runtime);
|
|
2882
|
+
const policy = starting && ctx.policySha256 ? { startPolicy: ctx.policySha256 } : {};
|
|
2883
|
+
const wake = input.session.wakeUrl ? { wakeUrl: input.session.wakeUrl } : {};
|
|
2884
|
+
const record = ctx.store.registry.upsert(session, {
|
|
2885
|
+
...fields(input),
|
|
2886
|
+
...pid,
|
|
2887
|
+
...policy,
|
|
2888
|
+
...wake,
|
|
2889
|
+
...hosted
|
|
2890
|
+
});
|
|
2891
|
+
notifyHost(record, input);
|
|
2892
|
+
return pass;
|
|
2893
|
+
}
|
|
2894
|
+
};
|
|
2895
|
+
|
|
2896
|
+
// src/hooks/policies/sessions/roles.ts
|
|
2897
|
+
function readRole(name, value) {
|
|
2898
|
+
const table3 = value ?? {};
|
|
2899
|
+
onlyKeys(table3, ["agent_type", "allow_edits", "allow_commands"]);
|
|
2900
|
+
const agentType = table3["agent_type"] ?? name;
|
|
2901
|
+
const allowEdits = table3["allow_edits"] ?? false;
|
|
2902
|
+
const commands = table3["allow_commands"] ?? [];
|
|
2903
|
+
if (typeof agentType !== "string" || typeof allowEdits !== "boolean") {
|
|
2904
|
+
throw new Error(
|
|
2905
|
+
`${name}: agent_type must be a string and allow_edits a boolean`
|
|
2906
|
+
);
|
|
2907
|
+
}
|
|
2908
|
+
const valid = Array.isArray(commands) && commands.every(
|
|
2909
|
+
(item) => Array.isArray(item) && item.length > 0 && item.every((word) => typeof word === "string")
|
|
2910
|
+
);
|
|
2911
|
+
if (!valid)
|
|
2912
|
+
throw new Error(`${name}.allow_commands must be a list of argv prefixes`);
|
|
2913
|
+
return { name, agentType, allowEdits, allowCommands: commands };
|
|
2914
|
+
}
|
|
2915
|
+
function roleOf(roles2, agentType) {
|
|
2916
|
+
if (!agentType) return void 0;
|
|
2917
|
+
return roles2.find(
|
|
2918
|
+
(role) => agentType === role.agentType || agentType.endsWith(`:${role.agentType}`)
|
|
2919
|
+
);
|
|
2920
|
+
}
|
|
2921
|
+
var roles = {
|
|
2922
|
+
id: "sessions.roles",
|
|
2923
|
+
events: ["before_tool"],
|
|
2924
|
+
tools: ["shell", "edit"],
|
|
2925
|
+
fail: "closed",
|
|
2926
|
+
defaultMode: "deny",
|
|
2927
|
+
options: (raw) => Object.entries(raw).map(([name, value]) => readRole(name, value)),
|
|
2928
|
+
evaluate(input, ctx) {
|
|
2929
|
+
const role = roleOf(ctx.options, input.session.agentType);
|
|
2930
|
+
const tool = input.tool;
|
|
2931
|
+
if (!role || !tool) return pass;
|
|
2932
|
+
if (tool.kind === "edit") {
|
|
2933
|
+
if (role.allowEdits) return pass;
|
|
2934
|
+
return {
|
|
2935
|
+
verdict: "deny",
|
|
2936
|
+
reason: `The ${role.name} role does not edit files; report back instead.`
|
|
2937
|
+
};
|
|
2938
|
+
}
|
|
2939
|
+
const steps = tool.steps;
|
|
2940
|
+
const allowed = steps !== void 0 && realSteps(steps).every(
|
|
2941
|
+
(step) => role.allowCommands.some((prefix) => startsWith(step.argv, prefix))
|
|
2942
|
+
);
|
|
2943
|
+
if (allowed) return pass;
|
|
2944
|
+
const list2 = role.allowCommands.map((prefix) => prefix.join(" ")).join(", ");
|
|
2945
|
+
return {
|
|
2946
|
+
verdict: "deny",
|
|
2947
|
+
reason: `The ${role.name} role runs only: ${list2}. Report back instead.`
|
|
2948
|
+
};
|
|
2949
|
+
}
|
|
2950
|
+
};
|
|
2951
|
+
|
|
2952
|
+
// src/hooks/policies/shell/compose-teardown.ts
|
|
2953
|
+
var GLOBAL_VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
2954
|
+
"-f",
|
|
2955
|
+
"--file",
|
|
2956
|
+
"-p",
|
|
2957
|
+
"--project-name",
|
|
2958
|
+
"--project-directory",
|
|
2959
|
+
"--profile",
|
|
2960
|
+
"--env-file",
|
|
2961
|
+
"-H",
|
|
2962
|
+
"--host",
|
|
2963
|
+
"--ansi",
|
|
2964
|
+
"--progress",
|
|
2965
|
+
"--parallel"
|
|
2966
|
+
]);
|
|
2967
|
+
var VERB_VALUE_FLAGS = {
|
|
2968
|
+
stop: /* @__PURE__ */ new Set(["-t", "--timeout"]),
|
|
2969
|
+
kill: /* @__PURE__ */ new Set(["-s", "--signal"]),
|
|
2970
|
+
rm: /* @__PURE__ */ new Set(),
|
|
2971
|
+
down: /* @__PURE__ */ new Set(["-t", "--timeout", "--rmi"])
|
|
2972
|
+
};
|
|
2973
|
+
var SCOPED_VERBS = /* @__PURE__ */ new Set(["stop", "rm", "kill"]);
|
|
2974
|
+
var FALLBACK3 = /\b(?:docker[ \t]+compose|docker-compose)\b[^|;&]*\b(?:down|stop|rm|kill)\b/u;
|
|
2975
|
+
function firstArgument(words, valueFlags) {
|
|
2976
|
+
for (let index = 0; index < words.length; index += 1) {
|
|
2977
|
+
const word = words[index];
|
|
2978
|
+
if (!word.startsWith("-"))
|
|
2979
|
+
return { value: word, rest: words.slice(index + 1) };
|
|
2980
|
+
if (!word.includes("=") && valueFlags.has(word)) index += 1;
|
|
2981
|
+
}
|
|
2982
|
+
return { value: null, rest: [] };
|
|
2983
|
+
}
|
|
2984
|
+
function composeArgs(argv) {
|
|
2985
|
+
const name = program(argv[0]);
|
|
2986
|
+
if (name === "docker-compose") return argv.slice(1);
|
|
2987
|
+
if (name === "docker" && argv[1] === "compose") return argv.slice(2);
|
|
2988
|
+
return null;
|
|
2989
|
+
}
|
|
2990
|
+
function teardownReason(argv) {
|
|
2991
|
+
const args = composeArgs(argv);
|
|
2992
|
+
if (!args) return null;
|
|
2993
|
+
const { value: verb, rest } = firstArgument(args, GLOBAL_VALUE_FLAGS);
|
|
2994
|
+
if (verb === "down") {
|
|
2995
|
+
return "docker compose down takes no service argument: it tears down every container and network in the project, including ones this task never touched.";
|
|
2996
|
+
}
|
|
2997
|
+
if (!verb || !SCOPED_VERBS.has(verb)) return null;
|
|
2998
|
+
if (firstArgument(rest, VERB_VALUE_FLAGS[verb]).value) return null;
|
|
2999
|
+
return `docker compose ${verb} with no service named applies to every service in the project. Name the service you mean.`;
|
|
3000
|
+
}
|
|
3001
|
+
var composeTeardown = {
|
|
3002
|
+
id: "shell.compose_teardown",
|
|
3003
|
+
events: ["before_tool"],
|
|
3004
|
+
tools: ["shell"],
|
|
3005
|
+
fail: "closed",
|
|
3006
|
+
defaultMode: "deny",
|
|
3007
|
+
options: noOptions,
|
|
3008
|
+
evaluate(input) {
|
|
3009
|
+
const call = shellCall(input);
|
|
3010
|
+
if (!call) return pass;
|
|
3011
|
+
if (!call.steps) {
|
|
3012
|
+
if (!FALLBACK3.test(call.command)) return pass;
|
|
3013
|
+
return {
|
|
3014
|
+
verdict: "deny",
|
|
3015
|
+
reason: "A docker compose teardown could not be read."
|
|
3016
|
+
};
|
|
3017
|
+
}
|
|
3018
|
+
for (const step of realSteps(call.steps)) {
|
|
3019
|
+
const reason = teardownReason(step.argv);
|
|
3020
|
+
if (reason) return { verdict: "deny", reason };
|
|
3021
|
+
}
|
|
3022
|
+
return pass;
|
|
3023
|
+
}
|
|
3024
|
+
};
|
|
3025
|
+
|
|
3026
|
+
// src/hooks/policies/shell/heredoc.ts
|
|
3027
|
+
var HEREDOC = /<<(?!<)-?[ \t]*(['"\\]?)[A-Za-z_][A-Za-z0-9_]*\1/u;
|
|
3028
|
+
var HERESTRING = /<<</u;
|
|
3029
|
+
var REASON3 = "Heredocs and here-strings are refused: the shell expands and truncates their body silently. Write the file with the editor tool, then run it, or pass the content as an argument.";
|
|
3030
|
+
function maskQuotes(command6) {
|
|
3031
|
+
let out = "";
|
|
3032
|
+
for (let index = 0; index < command6.length; index += 1) {
|
|
3033
|
+
const char = command6[index];
|
|
3034
|
+
if (char === "\\") {
|
|
3035
|
+
out += command6.slice(index, index + 2);
|
|
3036
|
+
index += 1;
|
|
3037
|
+
continue;
|
|
3038
|
+
}
|
|
3039
|
+
if (char !== "'" && char !== '"') {
|
|
3040
|
+
out += char;
|
|
3041
|
+
continue;
|
|
3042
|
+
}
|
|
3043
|
+
let end = index + 1;
|
|
3044
|
+
while (end < command6.length && command6[end] !== char) {
|
|
3045
|
+
end += char === '"' && command6[end] === "\\" ? 2 : 1;
|
|
3046
|
+
}
|
|
3047
|
+
if (end >= command6.length) return null;
|
|
3048
|
+
out += "Q";
|
|
3049
|
+
index = end;
|
|
3050
|
+
}
|
|
3051
|
+
return out;
|
|
3052
|
+
}
|
|
3053
|
+
var heredoc = {
|
|
3054
|
+
id: "shell.heredoc",
|
|
3055
|
+
events: ["before_tool"],
|
|
3056
|
+
tools: ["shell"],
|
|
3057
|
+
fail: "closed",
|
|
3058
|
+
defaultMode: "deny",
|
|
3059
|
+
options: noOptions,
|
|
3060
|
+
evaluate(input) {
|
|
3061
|
+
const call = shellCall(input);
|
|
3062
|
+
if (!call) return pass;
|
|
3063
|
+
const text7 = maskQuotes(call.command) ?? call.command;
|
|
3064
|
+
if (HEREDOC.test(text7) || HERESTRING.test(text7)) {
|
|
3065
|
+
return { verdict: "deny", reason: REASON3 };
|
|
3066
|
+
}
|
|
3067
|
+
return pass;
|
|
3068
|
+
}
|
|
3069
|
+
};
|
|
3070
|
+
|
|
3071
|
+
// src/hooks/policies/shell/host-approval.ts
|
|
3072
|
+
var APPROVALS = "host-approvals";
|
|
3073
|
+
var VERB = ["host", "approve"];
|
|
3074
|
+
var FALLBACK4 = /\bhost\s+approve\b|host-approvals/u;
|
|
3075
|
+
var REASON4 = "Approving the pmcp-pty host policy is the operator's step: run pmcp harness host approve in your own terminal, not from an agent.";
|
|
3076
|
+
function approves(argv) {
|
|
3077
|
+
const at = argv.indexOf(VERB[0]);
|
|
3078
|
+
return at >= 0 && argv[at + 1] === VERB[1];
|
|
3079
|
+
}
|
|
3080
|
+
function hit(input) {
|
|
3081
|
+
const tool = input.tool;
|
|
3082
|
+
if (tool?.kind === "edit")
|
|
3083
|
+
return tool.paths.some((path) => path.includes(`/${APPROVALS}`));
|
|
3084
|
+
const call = shellCall(input);
|
|
3085
|
+
if (!call) return false;
|
|
3086
|
+
if (!call.steps) return FALLBACK4.test(call.command);
|
|
3087
|
+
return realSteps(call.steps).some(
|
|
3088
|
+
(step) => approves(step.argv) || step.argv.some((word) => word.includes(APPROVALS))
|
|
3089
|
+
);
|
|
3090
|
+
}
|
|
3091
|
+
var hostApproval = {
|
|
3092
|
+
id: "shell.host_approval",
|
|
3093
|
+
events: ["before_tool"],
|
|
3094
|
+
tools: ["shell", "edit"],
|
|
3095
|
+
fail: "closed",
|
|
3096
|
+
defaultMode: "deny",
|
|
3097
|
+
options: noOptions,
|
|
3098
|
+
evaluate(input) {
|
|
3099
|
+
return hit(input) ? { verdict: "deny", reason: REASON4 } : pass;
|
|
3100
|
+
}
|
|
3101
|
+
};
|
|
3102
|
+
|
|
3103
|
+
// src/hooks/policies/shell/in-place-edit.ts
|
|
3104
|
+
var EDITORS = /* @__PURE__ */ new Set(["sed", "perl", "awk", "gawk"]);
|
|
3105
|
+
var FALLBACK5 = /(^|[;&|\n]|&&)[ \t]*(?:[A-Za-z_][A-Za-z0-9_]*=\S*[ \t]+)*(sed|perl|gawk|awk)\b[^\n]*?\s(-i\b|--in-place\b|-i\S+)/u;
|
|
3106
|
+
var REASON5 = "In-place stream edits (sed -i, perl -i, awk -i) rewrite files without showing a diff and succeed on the first match rather than the intended one. Use the editor tool instead.";
|
|
3107
|
+
function inPlace(argv) {
|
|
3108
|
+
const name = program(argv[0]);
|
|
3109
|
+
if (!EDITORS.has(name)) return false;
|
|
3110
|
+
const cluster = name === "sed" || name === "perl" ? /^-[A-Za-z]*i/u : /^-i/u;
|
|
3111
|
+
return argv.slice(1).some((arg) => arg.startsWith("--in-place") || cluster.test(arg));
|
|
3112
|
+
}
|
|
3113
|
+
var inPlaceEdit = {
|
|
3114
|
+
id: "shell.in_place_edit",
|
|
3115
|
+
events: ["before_tool"],
|
|
3116
|
+
tools: ["shell"],
|
|
3117
|
+
fail: "closed",
|
|
3118
|
+
defaultMode: "deny",
|
|
3119
|
+
options: noOptions,
|
|
3120
|
+
evaluate(input) {
|
|
3121
|
+
const call = shellCall(input);
|
|
3122
|
+
if (!call) return pass;
|
|
3123
|
+
const hit2 = call.steps ? realSteps(call.steps).some((step) => inPlace(step.argv)) : FALLBACK5.test(call.command);
|
|
3124
|
+
return hit2 ? { verdict: "deny", reason: REASON5 } : pass;
|
|
3125
|
+
}
|
|
3126
|
+
};
|
|
3127
|
+
|
|
3128
|
+
// src/hooks/policies/shell/operands.ts
|
|
3129
|
+
var SEARCH = {
|
|
3130
|
+
code: ["-e", "--regexp"],
|
|
3131
|
+
files: ["-f", "--file"],
|
|
3132
|
+
values: [
|
|
3133
|
+
"-m",
|
|
3134
|
+
"--max-count",
|
|
3135
|
+
"-A",
|
|
3136
|
+
"-B",
|
|
3137
|
+
"-C",
|
|
3138
|
+
"--context",
|
|
3139
|
+
"-g",
|
|
3140
|
+
"--glob",
|
|
3141
|
+
"-t",
|
|
3142
|
+
"--type",
|
|
3143
|
+
"--include",
|
|
3144
|
+
"--exclude",
|
|
3145
|
+
"--exclude-dir"
|
|
3146
|
+
],
|
|
3147
|
+
leadingCode: true
|
|
3148
|
+
};
|
|
3149
|
+
var GRAMMARS = {
|
|
3150
|
+
grep: SEARCH,
|
|
3151
|
+
egrep: SEARCH,
|
|
3152
|
+
rg: SEARCH,
|
|
3153
|
+
ag: SEARCH,
|
|
3154
|
+
sed: {
|
|
3155
|
+
code: ["-e", "--expression"],
|
|
3156
|
+
files: ["-f", "--file"],
|
|
3157
|
+
values: [],
|
|
3158
|
+
leadingCode: true
|
|
3159
|
+
},
|
|
3160
|
+
awk: { code: [], files: ["-f"], values: ["-v", "-F"], leadingCode: true },
|
|
3161
|
+
gawk: { code: [], files: ["-f"], values: ["-v", "-F"], leadingCode: true },
|
|
3162
|
+
jq: {
|
|
3163
|
+
code: [],
|
|
3164
|
+
files: ["-f", "--from-file", "--rawfile", "--slurpfile"],
|
|
3165
|
+
values: ["--arg", "--argjson"],
|
|
3166
|
+
leadingCode: true
|
|
3167
|
+
},
|
|
3168
|
+
yq: { code: [], files: ["--from-file"], values: [], leadingCode: true }
|
|
3169
|
+
};
|
|
3170
|
+
var INLINE = /* @__PURE__ */ new Set([
|
|
3171
|
+
"python",
|
|
3172
|
+
"python3",
|
|
3173
|
+
"perl",
|
|
3174
|
+
"ruby",
|
|
3175
|
+
"node",
|
|
3176
|
+
"bun",
|
|
3177
|
+
"deno"
|
|
3178
|
+
]);
|
|
3179
|
+
var INLINE_CODE = /* @__PURE__ */ new Set(["-c", "-e", "-E", "-p", "--eval", "--print"]);
|
|
3180
|
+
function grammarOf(name) {
|
|
3181
|
+
if (GRAMMARS[name]) return GRAMMARS[name];
|
|
3182
|
+
if (INLINE.has(name))
|
|
3183
|
+
return {
|
|
3184
|
+
code: [...INLINE_CODE],
|
|
3185
|
+
files: [],
|
|
3186
|
+
values: [],
|
|
3187
|
+
leadingCode: false
|
|
3188
|
+
};
|
|
3189
|
+
return null;
|
|
3190
|
+
}
|
|
3191
|
+
function fileOperands(argv) {
|
|
3192
|
+
const grammar = grammarOf(program(argv[0]));
|
|
3193
|
+
if (!grammar) return argv.slice(1);
|
|
3194
|
+
const operands = [];
|
|
3195
|
+
let codeGiven = false;
|
|
3196
|
+
let options2 = true;
|
|
3197
|
+
for (let at = 1; at < argv.length; at += 1) {
|
|
3198
|
+
const word = argv[at];
|
|
3199
|
+
const [flag2, inline] = word.startsWith("--") && word.includes("=") ? word.split(/=(.*)/su) : [word, void 0];
|
|
3200
|
+
if (options2 && word === "--") {
|
|
3201
|
+
options2 = false;
|
|
3202
|
+
continue;
|
|
3203
|
+
}
|
|
3204
|
+
if (options2 && grammar.code.includes(flag2)) {
|
|
3205
|
+
codeGiven = true;
|
|
3206
|
+
if (inline === void 0) at += 1;
|
|
3207
|
+
continue;
|
|
3208
|
+
}
|
|
3209
|
+
if (options2 && grammar.files.includes(flag2)) {
|
|
3210
|
+
codeGiven = true;
|
|
3211
|
+
operands.push(inline ?? argv[at + 1] ?? "");
|
|
3212
|
+
if (inline === void 0) at += 1;
|
|
3213
|
+
continue;
|
|
3214
|
+
}
|
|
3215
|
+
if (options2 && grammar.values.includes(flag2)) {
|
|
3216
|
+
if (inline === void 0)
|
|
3217
|
+
at += flag2 === "--arg" || flag2 === "--argjson" ? 2 : 1;
|
|
3218
|
+
continue;
|
|
3219
|
+
}
|
|
3220
|
+
if (options2 && word.startsWith("-") && word !== "-") continue;
|
|
3221
|
+
if (grammar.leadingCode && !codeGiven) {
|
|
3222
|
+
codeGiven = true;
|
|
3223
|
+
continue;
|
|
3224
|
+
}
|
|
3225
|
+
operands.push(word);
|
|
3226
|
+
}
|
|
3227
|
+
return operands;
|
|
3228
|
+
}
|
|
3229
|
+
|
|
3230
|
+
// src/hooks/policies/shell/secret-exfiltration.ts
|
|
3231
|
+
var SECRET_PATHS = [
|
|
3232
|
+
/(?:^|[^\w.-])\.env(?:\.(?!example\b|sample\b|template\b|dist\b)[\w.-]+)?(?![\w.-])/iu,
|
|
3233
|
+
/(?:^|[^\w.-])\.env\.d(?:\/|\b)/iu,
|
|
3234
|
+
/\.dev\.vars(?:\.json)?\b/iu,
|
|
3235
|
+
/\.bak\.\d{8,}/u,
|
|
3236
|
+
/\b(?:id_rsa|id_ed25519|id_ecdsa)\b(?!\.pub)/u,
|
|
3237
|
+
/\.(?:pem|p12|pfx)\b/iu,
|
|
3238
|
+
// Known credential files only: a name that merely contains "secret" (a test fixture, a
|
|
3239
|
+
// policy source) is not one. Project-specific files go in extra_secret_paths.
|
|
3240
|
+
/\.(?:npmrc|netrc)\b/u,
|
|
3241
|
+
/\bauth\.json\b/u,
|
|
3242
|
+
/\bservice[-_]?account[\w.-]*\.json\b/iu
|
|
3243
|
+
];
|
|
3244
|
+
var READERS = new Set(
|
|
3245
|
+
"cat bat head tail less more nl tac od xxd hexdump strings base64 cut tee cp awk gawk sed grep egrep rg ag jq yq python python3 perl ruby node bun deno".split(
|
|
3246
|
+
" "
|
|
3247
|
+
)
|
|
3248
|
+
);
|
|
3249
|
+
var NETWORK = new Set(
|
|
3250
|
+
"curl wget nc ncat netcat socat telnet ssh scp rsync ftp http httpie xh".split(
|
|
3251
|
+
" "
|
|
3252
|
+
)
|
|
3253
|
+
);
|
|
3254
|
+
var URL2 = /^[^\s]*https?:\/\//u;
|
|
3255
|
+
var PATTERN = /\\[.\w]/u;
|
|
3256
|
+
var ENV_DUMPS = [
|
|
3257
|
+
["printenv"],
|
|
3258
|
+
["env"],
|
|
3259
|
+
["set"],
|
|
3260
|
+
["export", "-p"],
|
|
3261
|
+
["declare", "-x"]
|
|
3262
|
+
];
|
|
3263
|
+
var FALLBACK_PATH = /(?:^|[^\w.-])\.env(?![.\w-])|(?:^|[^\w.-])\.env\.d(?:\/|\b)|\.dev\.vars(?:\.json)?\b|\.bak\.\d{8,}/u;
|
|
3264
|
+
var FALLBACK_USE = /(?:^|[;&|(]|&&)\s*(?:sudo\s+)?(?:cat|head|tail|less|grep|rg|jq|base64|cp|curl|wget|nc|scp|rsync)\b/u;
|
|
3265
|
+
var NO_VALUES = "Use metadata verbs instead (ls, stat, wc, find), or a command that names keys without resolving values.";
|
|
3266
|
+
function names(word, options2) {
|
|
3267
|
+
if (PATTERN.test(word)) return false;
|
|
3268
|
+
return [...SECRET_PATHS, ...options2.secretPaths].some((re) => re.test(word));
|
|
3269
|
+
}
|
|
3270
|
+
function isWire(word) {
|
|
3271
|
+
return NETWORK.has(program(word)) || URL2.test(word);
|
|
3272
|
+
}
|
|
3273
|
+
function runsVerb(argv, commands) {
|
|
3274
|
+
return commands.some(
|
|
3275
|
+
(entry) => argv.some((word, at) => {
|
|
3276
|
+
if (program(word) !== entry.binary) return false;
|
|
3277
|
+
const end = argv.indexOf("--", at + 1);
|
|
3278
|
+
const args = argv.slice(at + 1, end < 0 ? void 0 : end);
|
|
3279
|
+
return entry.verbs.some((verb) => {
|
|
3280
|
+
let from = 0;
|
|
3281
|
+
for (const part of verb) {
|
|
3282
|
+
const found = args.indexOf(part, from);
|
|
3283
|
+
if (found < 0) return false;
|
|
3284
|
+
from = found + 1;
|
|
3285
|
+
}
|
|
3286
|
+
return true;
|
|
3287
|
+
});
|
|
3288
|
+
})
|
|
3289
|
+
);
|
|
3290
|
+
}
|
|
3291
|
+
function dumpsEnvironment(step) {
|
|
3292
|
+
return ENV_DUMPS.some(
|
|
3293
|
+
(dump) => step.raw.length === dump.length && dump.every((word, at) => step.raw[at] === word)
|
|
3294
|
+
);
|
|
3295
|
+
}
|
|
3296
|
+
function severity(steps, options2) {
|
|
3297
|
+
const wire = steps.some((step) => step.raw.some(isWire));
|
|
3298
|
+
const real = realSteps(steps);
|
|
3299
|
+
if (real.some((step) => runsVerb(step.argv, options2.publishCommands)))
|
|
3300
|
+
return "exfil";
|
|
3301
|
+
if (real.some((step) => runsVerb(step.argv, options2.valueCommands)))
|
|
3302
|
+
return wire ? "exfil" : "read";
|
|
3303
|
+
if (steps.some(dumpsEnvironment)) return wire ? "exfil" : "read";
|
|
3304
|
+
const touched = real.filter(
|
|
3305
|
+
(step) => fileOperands(step.argv).some((word) => names(word, options2))
|
|
3306
|
+
);
|
|
3307
|
+
if (touched.some((step) => isWire(step.argv[0]))) return "exfil";
|
|
3308
|
+
if (!touched.some((step) => READERS.has(program(step.argv[0])))) return null;
|
|
3309
|
+
return wire ? "exfil" : "read";
|
|
3310
|
+
}
|
|
3311
|
+
function fallbackSeverity(command6) {
|
|
3312
|
+
if (!FALLBACK_PATH.test(command6) || !FALLBACK_USE.test(command6)) return null;
|
|
3313
|
+
return /\b(?:curl|wget|nc|scp|rsync)\b|https?:\/\//u.test(command6) ? "exfil" : "read";
|
|
3314
|
+
}
|
|
3315
|
+
function verbsOf(table3, key) {
|
|
3316
|
+
return (stringList(table3, key) ?? []).map((verb) => verb.split(/\s+/u));
|
|
3317
|
+
}
|
|
3318
|
+
function commandTables(raw) {
|
|
3319
|
+
if (raw === void 0) return { value: [], publish: [] };
|
|
3320
|
+
if (!Array.isArray(raw))
|
|
3321
|
+
throw new Error("value_commands must be an array of tables");
|
|
3322
|
+
const value = [];
|
|
3323
|
+
const publish = [];
|
|
3324
|
+
for (const entry of raw) {
|
|
3325
|
+
const table3 = entry ?? {};
|
|
3326
|
+
onlyKeys(table3, ["binary", "read", "publish"]);
|
|
3327
|
+
const binary = table3["binary"];
|
|
3328
|
+
if (typeof binary !== "string" || binary === "") {
|
|
3329
|
+
throw new Error("value_commands entries need a binary");
|
|
3330
|
+
}
|
|
3331
|
+
value.push({ binary, verbs: verbsOf(table3, "read") });
|
|
3332
|
+
publish.push({ binary, verbs: verbsOf(table3, "publish") });
|
|
3333
|
+
}
|
|
3334
|
+
return { value, publish };
|
|
3335
|
+
}
|
|
3336
|
+
var secretExfiltration = {
|
|
3337
|
+
id: "shell.secret_exfiltration",
|
|
3338
|
+
events: ["before_tool"],
|
|
3339
|
+
tools: ["shell"],
|
|
3340
|
+
fail: "closed",
|
|
3341
|
+
defaultMode: "graded",
|
|
3342
|
+
options(raw) {
|
|
3343
|
+
onlyKeys(raw, ["extra_secret_paths", "value_commands"]);
|
|
3344
|
+
const commands = commandTables(raw["value_commands"]);
|
|
3345
|
+
return {
|
|
3346
|
+
secretPaths: (stringList(raw, "extra_secret_paths") ?? []).map(
|
|
3347
|
+
(source) => new RegExp(source, "iu")
|
|
3348
|
+
),
|
|
3349
|
+
valueCommands: commands.value,
|
|
3350
|
+
publishCommands: commands.publish
|
|
3351
|
+
};
|
|
3352
|
+
},
|
|
3353
|
+
evaluate(input, ctx) {
|
|
3354
|
+
const call = shellCall(input);
|
|
3355
|
+
if (!call) return pass;
|
|
3356
|
+
const found = call.steps ? severity(call.steps, ctx.options) : fallbackSeverity(call.command);
|
|
3357
|
+
if (found === "exfil") {
|
|
3358
|
+
return {
|
|
3359
|
+
verdict: "deny",
|
|
3360
|
+
reason: "This sends a credential, or changes a live one, over the network. Credential values must not leave the host; send a reference, never the material."
|
|
3361
|
+
};
|
|
3362
|
+
}
|
|
3363
|
+
if (found === "read") {
|
|
3364
|
+
return {
|
|
3365
|
+
verdict: "ask",
|
|
3366
|
+
reason: `This prints credential values to stdout, where they enter the transcript and every log that carries it. ${NO_VALUES}`
|
|
3367
|
+
};
|
|
3368
|
+
}
|
|
3369
|
+
return pass;
|
|
3370
|
+
}
|
|
3371
|
+
};
|
|
3372
|
+
|
|
3373
|
+
// src/hooks/policies/shell/stdin-interpreter.ts
|
|
3374
|
+
var PYTHON = /^i?python[23]?(\.\d+)?$/u;
|
|
3375
|
+
var FALLBACK6 = /(?:^|[;&|(]|&&|\|\|)[ \t]*(?:sudo[ \t]+)?i?python[23]?(?:\.\d+)?\b([^;&|)]*)/gu;
|
|
3376
|
+
var REASON6 = "This interpreter reads its program from stdin, so the command text does not contain it and whatever fed the pipe already expanded it. Write the script with the editor tool, then run it.";
|
|
3377
|
+
function programFromStdin(args) {
|
|
3378
|
+
if (args.includes("-")) return true;
|
|
3379
|
+
return !args.some(
|
|
3380
|
+
(arg) => arg === "-c" || arg === "-m" || !arg.startsWith("-")
|
|
3381
|
+
);
|
|
3382
|
+
}
|
|
3383
|
+
function fallback2(command6) {
|
|
3384
|
+
for (const match of command6.matchAll(FALLBACK6)) {
|
|
3385
|
+
const args = (match[1] ?? "").trim().split(/\s+/u).filter(Boolean);
|
|
3386
|
+
if (programFromStdin(args)) return true;
|
|
3387
|
+
}
|
|
3388
|
+
return false;
|
|
3389
|
+
}
|
|
3390
|
+
var stdinInterpreter = {
|
|
3391
|
+
id: "shell.stdin_interpreter",
|
|
3392
|
+
events: ["before_tool"],
|
|
3393
|
+
tools: ["shell"],
|
|
3394
|
+
fail: "closed",
|
|
3395
|
+
defaultMode: "ask",
|
|
3396
|
+
options: noOptions,
|
|
3397
|
+
evaluate(input) {
|
|
3398
|
+
const call = shellCall(input);
|
|
3399
|
+
if (!call) return pass;
|
|
3400
|
+
const hit2 = call.steps ? realSteps(call.steps).some(
|
|
3401
|
+
(step) => PYTHON.test(program(step.argv[0])) && programFromStdin(step.argv.slice(1))
|
|
3402
|
+
) : fallback2(call.command);
|
|
3403
|
+
return hit2 ? { verdict: "deny", reason: REASON6 } : pass;
|
|
3404
|
+
}
|
|
3405
|
+
};
|
|
3406
|
+
|
|
3407
|
+
// src/hooks/policies/workspace/shared-checkout.ts
|
|
3408
|
+
import { dirname as dirname2 } from "path";
|
|
3409
|
+
|
|
3410
|
+
// src/hooks/policies/workspace/claimants.ts
|
|
3411
|
+
function claimants(store, session, root) {
|
|
3412
|
+
return store.registry.list().filter(
|
|
3413
|
+
(record) => record.live && record.session !== session && record.worktree === root
|
|
3414
|
+
).map((record) => record.alias || record.session.slice(0, 8));
|
|
3415
|
+
}
|
|
3416
|
+
function claimantNotice(root, names2) {
|
|
3417
|
+
return `Another live session works in ${root}: ${names2.join(", ")}. Edit only files that are yours, and name every path when you commit.`;
|
|
3418
|
+
}
|
|
3419
|
+
|
|
3420
|
+
// src/hooks/policies/workspace/leftovers.ts
|
|
3421
|
+
import { spawnSync as spawnSync3 } from "child_process";
|
|
3422
|
+
import { isAbsolute as isAbsolute9, relative as relative3, resolve as resolve9 } from "path";
|
|
3423
|
+
var TOUCHED_KEPT = 50;
|
|
3424
|
+
function addTouched(touched, root, target) {
|
|
3425
|
+
const path = relative3(
|
|
3426
|
+
root,
|
|
3427
|
+
isAbsolute9(target) ? target : resolve9(root, target)
|
|
3428
|
+
);
|
|
3429
|
+
if (!path || path.startsWith("..") || touched.some((entry) => entry.path === path))
|
|
3430
|
+
return [...touched];
|
|
3431
|
+
return [...touched, { path, at: (/* @__PURE__ */ new Date()).toISOString() }].slice(
|
|
3432
|
+
-TOUCHED_KEPT
|
|
3433
|
+
);
|
|
3434
|
+
}
|
|
3435
|
+
function porcelainPaths(text7) {
|
|
3436
|
+
const fields2 = text7.split("\0").filter(Boolean);
|
|
3437
|
+
const paths = [];
|
|
3438
|
+
for (let index = 0; index < fields2.length; index += 1) {
|
|
3439
|
+
const entry = fields2[index];
|
|
3440
|
+
paths.push(entry.slice(3));
|
|
3441
|
+
if (entry[0] === "R" || entry[0] === "C") index += 1;
|
|
3442
|
+
}
|
|
3443
|
+
return paths;
|
|
3444
|
+
}
|
|
3445
|
+
function stillDirty(root, paths) {
|
|
3446
|
+
if (paths.length === 0) return [];
|
|
3447
|
+
const result = spawnSync3(
|
|
3448
|
+
"git",
|
|
3449
|
+
[
|
|
3450
|
+
"status",
|
|
3451
|
+
"--porcelain",
|
|
3452
|
+
"-z",
|
|
3453
|
+
"--ignore-submodules=all",
|
|
3454
|
+
"--untracked-files=all",
|
|
3455
|
+
"--",
|
|
3456
|
+
...paths
|
|
3457
|
+
],
|
|
3458
|
+
{ cwd: root, encoding: "utf8", timeout: 5e3 }
|
|
3459
|
+
);
|
|
3460
|
+
if (result.status !== 0) throw new Error("git status failed");
|
|
3461
|
+
const listed = porcelainPaths(result.stdout);
|
|
3462
|
+
return paths.filter(
|
|
3463
|
+
(path) => listed.some((entry) => entry === path || entry.startsWith(`${path}/`))
|
|
3464
|
+
);
|
|
3465
|
+
}
|
|
3466
|
+
|
|
3467
|
+
// src/hooks/policies/workspace/shared-checkout.ts
|
|
3468
|
+
var shared = (checkout) => checkout !== null && checkout.main && hasLinkedWorktrees(checkout);
|
|
3469
|
+
function flag(raw, key) {
|
|
3470
|
+
const value = raw[key] ?? true;
|
|
3471
|
+
if (typeof value !== "boolean") throw new Error(`${key} must be a boolean`);
|
|
3472
|
+
return value;
|
|
3473
|
+
}
|
|
3474
|
+
function sessionWarning(input, ctx) {
|
|
3475
|
+
const checkout = checkoutOf(input.cwd);
|
|
3476
|
+
const record = ctx.store.registry.get(input.session.id);
|
|
3477
|
+
if (!ctx.options.warnOnSession || !shared(checkout) || record?.sharedWarned)
|
|
3478
|
+
return [];
|
|
3479
|
+
ctx.store.registry.upsert(input.session.id, { sharedWarned: true });
|
|
3480
|
+
return [
|
|
3481
|
+
`This session works in the shared checkout ${checkout.root}, not a desk. Keep to reading, syncing and delivering here; make changes in a desk or a scratch worktree.`
|
|
3482
|
+
];
|
|
3483
|
+
}
|
|
3484
|
+
function leftovers(input, ctx) {
|
|
3485
|
+
const record = ctx.store.registry.get(input.session.id);
|
|
3486
|
+
const touched = record?.sharedTouched ?? [];
|
|
3487
|
+
if (!ctx.options.trackEdits || !record?.sharedRoot || touched.length === 0)
|
|
3488
|
+
return [];
|
|
3489
|
+
const dirty = new Set(
|
|
3490
|
+
stillDirty(
|
|
3491
|
+
record.sharedRoot,
|
|
3492
|
+
touched.map((entry) => entry.path)
|
|
3493
|
+
)
|
|
3494
|
+
);
|
|
3495
|
+
const left = touched.filter((entry) => dirty.has(entry.path));
|
|
3496
|
+
const key = left.map((entry) => entry.path).sort().join(",");
|
|
3497
|
+
ctx.store.registry.upsert(input.session.id, {
|
|
3498
|
+
sharedTouched: left,
|
|
3499
|
+
sharedNoticed: key
|
|
3500
|
+
});
|
|
3501
|
+
if (left.length === 0 || key === record.sharedNoticed) return [];
|
|
3502
|
+
const listed = left.slice(0, 6).map((entry) => entry.path).join(", ");
|
|
3503
|
+
const more = left.length > 6 ? ", ..." : "";
|
|
3504
|
+
return [
|
|
3505
|
+
`You left ${left.length} uncommitted file(s) in the shared checkout ${record.sharedRoot}: ${listed}${more}. Syncing the shared main refuses while they stay; land them within this task or move them to a scratch worktree.`
|
|
3506
|
+
];
|
|
3507
|
+
}
|
|
3508
|
+
function onEdit(input, ctx) {
|
|
3509
|
+
const target = input.tool?.paths[0];
|
|
3510
|
+
const checkout = target ? checkoutOf(dirname2(target)) : null;
|
|
3511
|
+
if (!target || !checkout) return [];
|
|
3512
|
+
const lines = [];
|
|
3513
|
+
const others = ctx.options.warnClaimants ? claimants(ctx.store, input.session.id, checkout.root) : [];
|
|
3514
|
+
if (others.length > 0) lines.push(claimantNotice(checkout.root, others));
|
|
3515
|
+
if (!ctx.options.trackEdits || !shared(checkout)) return lines;
|
|
3516
|
+
const record = ctx.store.registry.get(input.session.id);
|
|
3517
|
+
ctx.store.registry.upsert(input.session.id, {
|
|
3518
|
+
sharedRoot: checkout.root,
|
|
3519
|
+
sharedTouched: addTouched(
|
|
3520
|
+
record?.sharedTouched ?? [],
|
|
3521
|
+
checkout.root,
|
|
3522
|
+
target
|
|
3523
|
+
),
|
|
3524
|
+
sharedEditNoticed: true
|
|
3525
|
+
});
|
|
3526
|
+
if (!record?.sharedEditNoticed) {
|
|
3527
|
+
lines.push(
|
|
3528
|
+
`You are editing the shared checkout ${checkout.root}, not a desk. Land this change within the task, or make it in a scratch worktree; an edit left here blocks syncing the shared main.`
|
|
3529
|
+
);
|
|
3530
|
+
}
|
|
3531
|
+
return lines;
|
|
3532
|
+
}
|
|
3533
|
+
function notices(input, ctx) {
|
|
3534
|
+
if (input.event === "before_tool") return onEdit(input, ctx);
|
|
3535
|
+
const lines = sessionWarning(input, ctx);
|
|
3536
|
+
if (input.event === "prompt") lines.push(...leftovers(input, ctx));
|
|
3537
|
+
return lines;
|
|
3538
|
+
}
|
|
3539
|
+
var sharedCheckout = {
|
|
3540
|
+
id: "git.shared_checkout",
|
|
3541
|
+
events: ["session_start", "prompt", "before_tool"],
|
|
3542
|
+
tools: ["edit"],
|
|
3543
|
+
fail: "open",
|
|
3544
|
+
defaultMode: "warn",
|
|
3545
|
+
options(raw) {
|
|
3546
|
+
onlyKeys(raw, [
|
|
3547
|
+
"enabled",
|
|
3548
|
+
"warn_on_session",
|
|
3549
|
+
"track_edits",
|
|
3550
|
+
"warn_claimants"
|
|
3551
|
+
]);
|
|
3552
|
+
return {
|
|
3553
|
+
warnOnSession: flag(raw, "warn_on_session"),
|
|
3554
|
+
trackEdits: flag(raw, "track_edits"),
|
|
3555
|
+
warnClaimants: flag(raw, "warn_claimants")
|
|
3556
|
+
};
|
|
3557
|
+
},
|
|
3558
|
+
evaluate(input, ctx) {
|
|
3559
|
+
if (!input.session.id) return pass;
|
|
3560
|
+
const lines = notices(input, ctx);
|
|
3561
|
+
return lines.length > 0 ? { context: lines } : pass;
|
|
3562
|
+
}
|
|
3563
|
+
};
|
|
3564
|
+
|
|
3565
|
+
// src/hooks/policies/workspace/mainline-deliver.ts
|
|
3566
|
+
import { resolve as resolve10 } from "path";
|
|
3567
|
+
var PUSH = /\bgit\b[^|;&\n]*\spush\b/u;
|
|
3568
|
+
var REASON7 = "This repository's mainline moves only through pmcp git deliver, which replays, gates and pushes one delivery at a time. Commit, then run pmcp git deliver; other repositories push from their own directory.";
|
|
3569
|
+
function sameRepository(dir, commonDir) {
|
|
3570
|
+
return checkoutOf(dir)?.commonDir === commonDir;
|
|
3571
|
+
}
|
|
3572
|
+
var mainlineDeliver = {
|
|
3573
|
+
id: "git.mainline.deliver",
|
|
3574
|
+
events: ["before_tool"],
|
|
3575
|
+
tools: ["shell"],
|
|
3576
|
+
fail: "closed",
|
|
3577
|
+
defaultMode: "deny",
|
|
3578
|
+
options(raw) {
|
|
3579
|
+
onlyKeys(raw, []);
|
|
3580
|
+
return {};
|
|
3581
|
+
},
|
|
3582
|
+
evaluate(input) {
|
|
3583
|
+
const call = shellCall(input);
|
|
3584
|
+
const project6 = checkoutOf(input.projectRoot);
|
|
3585
|
+
if (!call || !project6) return pass;
|
|
3586
|
+
if (!call.steps) {
|
|
3587
|
+
const hit2 = PUSH.test(call.command) && sameRepository(input.cwd, project6.commonDir);
|
|
3588
|
+
return hit2 ? { verdict: "deny", reason: REASON7 } : pass;
|
|
3589
|
+
}
|
|
3590
|
+
for (const step of realSteps(call.steps)) {
|
|
3591
|
+
if (program(step.argv[0]) !== "git") continue;
|
|
3592
|
+
const git = gitCall(step.argv);
|
|
3593
|
+
if (git.sub !== "push") continue;
|
|
3594
|
+
const dir = resolve10(input.cwd, step.dir, git.dir);
|
|
3595
|
+
if (sameRepository(dir, project6.commonDir))
|
|
3596
|
+
return { verdict: "deny", reason: REASON7 };
|
|
3597
|
+
}
|
|
3598
|
+
return pass;
|
|
3599
|
+
}
|
|
3600
|
+
};
|
|
3601
|
+
|
|
3602
|
+
// src/hooks/policies/workspace/worktree-sync.ts
|
|
3603
|
+
import { spawn } from "child_process";
|
|
3604
|
+
import { relative as relative4, resolve as resolve11 } from "path";
|
|
3605
|
+
function options(raw) {
|
|
3606
|
+
if (typeof raw["root"] !== "string") throw new Error("root must be a string");
|
|
3607
|
+
return raw;
|
|
3608
|
+
}
|
|
3609
|
+
function desk(input, opts) {
|
|
3610
|
+
const checkout = checkoutOf(input.cwd);
|
|
3611
|
+
if (!checkout || checkout.main) return null;
|
|
3612
|
+
const desks = resolve11(checkout.commonDir, "..", opts.root);
|
|
3613
|
+
const inside2 = relative4(desks, checkout.root);
|
|
3614
|
+
if (!inside2 || inside2.startsWith("..")) return null;
|
|
3615
|
+
return detect(checkout.root, {
|
|
3616
|
+
...opts.remote ? { remote: opts.remote } : {},
|
|
3617
|
+
...opts.mainline ? { mainline: opts.mainline } : {}
|
|
3618
|
+
});
|
|
3619
|
+
}
|
|
3620
|
+
function blocked2(input, ctx) {
|
|
3621
|
+
const pause2 = ctx.store.pause.read();
|
|
3622
|
+
if (pause2 && pause2.by !== input.session.id)
|
|
3623
|
+
return `the workspace is paused (${pause2.reason})`;
|
|
3624
|
+
const lease = ctx.store.leases.read("lockfile");
|
|
3625
|
+
return lease ? `a lockfile change is landing (${lease.subject})` : null;
|
|
3626
|
+
}
|
|
3627
|
+
function changed(root, from, paths, to = "HEAD") {
|
|
3628
|
+
if (paths.length === 0) return [];
|
|
3629
|
+
return gitText(root, ["diff", "--name-only", from, to, "--", ...paths]).split("\n").filter(Boolean);
|
|
3630
|
+
}
|
|
3631
|
+
function backgroundInstall(root, command6) {
|
|
3632
|
+
const child = spawn(command6[0], command6.slice(1), {
|
|
3633
|
+
cwd: root,
|
|
3634
|
+
detached: true,
|
|
3635
|
+
stdio: "ignore"
|
|
3636
|
+
});
|
|
3637
|
+
child.on("error", () => void 0);
|
|
3638
|
+
child.unref();
|
|
3639
|
+
}
|
|
3640
|
+
function move(input, ctx, git) {
|
|
3641
|
+
const opts = ctx.options;
|
|
3642
|
+
const lines = [];
|
|
3643
|
+
const report2 = {
|
|
3644
|
+
info: (line) => lines.push(line),
|
|
3645
|
+
warn: (line) => lines.push(line)
|
|
3646
|
+
};
|
|
3647
|
+
const fetch = input.event === "session_start" && opts.onSessionStart === "fetch_and_move";
|
|
3648
|
+
const before = gitText(git.root, ["rev-parse", "HEAD"]);
|
|
3649
|
+
if (input.event === "prompt" && changed(git.root, "HEAD", opts.installWhen, git.upstream).length > 0) {
|
|
3650
|
+
return [
|
|
3651
|
+
`${git.upstream} changed ${opts.installWhen.join(", ")}; this desk moves at the next session start or with pmcp git worktree sync.`
|
|
3652
|
+
];
|
|
3653
|
+
}
|
|
3654
|
+
const install = opts.install && opts.installAfterMove === "foreground" ? {
|
|
3655
|
+
install: {
|
|
3656
|
+
command: opts.install,
|
|
3657
|
+
when: opts.installWhen,
|
|
3658
|
+
stateDir: ctx.store.dir
|
|
3659
|
+
}
|
|
3660
|
+
} : {};
|
|
3661
|
+
const result = syncWorktree({
|
|
3662
|
+
ctx: git,
|
|
3663
|
+
followSubmodules: opts.followSubmodules,
|
|
3664
|
+
fetch,
|
|
3665
|
+
report: report2,
|
|
3666
|
+
...install
|
|
3667
|
+
});
|
|
3668
|
+
if (result === "current") return [];
|
|
3669
|
+
if (result === "held")
|
|
3670
|
+
return lines.map((line) => `Desk ${git.root}: ${line}`);
|
|
3671
|
+
if (opts.install && opts.installAfterMove === "background" && changed(git.root, before, opts.installWhen).length > 0) {
|
|
3672
|
+
backgroundInstall(git.root, opts.install);
|
|
3673
|
+
lines.push(`installing in the background: ${opts.install.join(" ")}`);
|
|
3674
|
+
}
|
|
3675
|
+
const rules2 = changed(git.root, before, opts.noticePaths);
|
|
3676
|
+
if (rules2.length > 0)
|
|
3677
|
+
lines.push(
|
|
3678
|
+
`These rule files changed with the move; re-read them before going on: ${rules2.join(", ")}`
|
|
3679
|
+
);
|
|
3680
|
+
return lines.map((line) => `Desk ${git.root}: ${line}`);
|
|
3681
|
+
}
|
|
3682
|
+
var worktreeSync = {
|
|
3683
|
+
id: "git.worktrees.sync",
|
|
3684
|
+
events: ["session_start", "prompt"],
|
|
3685
|
+
fail: "open",
|
|
3686
|
+
defaultMode: "warn",
|
|
3687
|
+
options,
|
|
3688
|
+
evaluate(input, ctx) {
|
|
3689
|
+
const opts = ctx.options;
|
|
3690
|
+
if (input.event === "session_start" && opts.onSessionStart === "off")
|
|
3691
|
+
return pass;
|
|
3692
|
+
if (input.event === "prompt" && opts.betweenPrompts === "off") return pass;
|
|
3693
|
+
const git = desk(input, opts);
|
|
3694
|
+
if (!git) return pass;
|
|
3695
|
+
const reason = blocked2(input, ctx);
|
|
3696
|
+
if (reason)
|
|
3697
|
+
return input.event === "session_start" ? { context: [`Desk ${git.root} was not moved: ${reason}.`] } : pass;
|
|
3698
|
+
const lines = move(input, ctx, git);
|
|
3699
|
+
return lines.length > 0 ? { context: lines } : pass;
|
|
3700
|
+
}
|
|
3701
|
+
};
|
|
3702
|
+
|
|
3703
|
+
// src/hooks/policies/custom.ts
|
|
3704
|
+
import { spawnSync as spawnSync4 } from "child_process";
|
|
3705
|
+
import { createHash as createHash2 } from "crypto";
|
|
3706
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2, realpathSync as realpathSync2 } from "fs";
|
|
3707
|
+
import { isAbsolute as isAbsolute10, relative as relative5, resolve as resolve12 } from "path";
|
|
3708
|
+
var FAILED = "Guard failed validation or execution";
|
|
3709
|
+
function inside(base, path) {
|
|
3710
|
+
const rest = relative5(base, path);
|
|
3711
|
+
return rest !== "" && !rest.startsWith("..") && !isAbsolute10(rest);
|
|
3712
|
+
}
|
|
3713
|
+
function guardScript(root, command6) {
|
|
3714
|
+
const script = command6[0]?.startsWith(".agents/guards/") ? command6[0] : command6[1];
|
|
3715
|
+
if (!script?.startsWith(".agents/guards/")) {
|
|
3716
|
+
throw new Error("Hook command must execute a script under .agents/guards/");
|
|
3717
|
+
}
|
|
3718
|
+
const base = resolve12(root, ".agents/guards");
|
|
3719
|
+
if (!inside(base, resolve12(root, script)))
|
|
3720
|
+
throw new Error("Hook guard must stay under .agents/guards/");
|
|
3721
|
+
if (!existsSync2(resolve12(root, script)))
|
|
3722
|
+
throw new Error("Hook guard script must exist");
|
|
3723
|
+
const real = realpathSync2(resolve12(root, script));
|
|
3724
|
+
if (!inside(realpathSync2(base), real))
|
|
3725
|
+
throw new Error("Hook guard cannot resolve outside .agents/guards/");
|
|
3726
|
+
return real;
|
|
3727
|
+
}
|
|
3728
|
+
function guardHash(root, command6) {
|
|
3729
|
+
return createHash2("sha256").update(readFileSync2(guardScript(root, command6))).digest("hex");
|
|
3730
|
+
}
|
|
3731
|
+
function applies(entry, input) {
|
|
3732
|
+
if (!entry.targets.includes(input.runtime)) return false;
|
|
3733
|
+
if (entry.event === "before_shell")
|
|
3734
|
+
return input.event === "before_tool" && input.tool?.kind === "shell";
|
|
3735
|
+
return input.event === "after_tool" && input.tool?.kind === "edit";
|
|
3736
|
+
}
|
|
3737
|
+
function run2(entry, input) {
|
|
3738
|
+
const root = input.projectRoot;
|
|
3739
|
+
if (guardHash(root, entry.command) !== entry.guardSha256) {
|
|
3740
|
+
return {
|
|
3741
|
+
verdict: "deny",
|
|
3742
|
+
reason: "Guard source changed; review it and run pmcp project"
|
|
3743
|
+
};
|
|
3744
|
+
}
|
|
3745
|
+
const args = entry.command.map(
|
|
3746
|
+
(part) => part.startsWith(".agents/guards/") ? resolve12(root, part) : part
|
|
3747
|
+
);
|
|
3748
|
+
const result = spawnSync4(args[0], args.slice(1), {
|
|
3749
|
+
cwd: root,
|
|
3750
|
+
env: { PATH: process.env["PATH"], LANG: "C.UTF-8" },
|
|
3751
|
+
input: JSON.stringify({
|
|
3752
|
+
runtime: input.runtime,
|
|
3753
|
+
event: entry.event,
|
|
3754
|
+
command: input.tool?.command ?? "",
|
|
3755
|
+
cwd: input.cwd,
|
|
3756
|
+
paths: input.tool?.paths ?? []
|
|
3757
|
+
}),
|
|
3758
|
+
encoding: "utf8",
|
|
3759
|
+
timeout: entry.timeoutMs,
|
|
3760
|
+
killSignal: "SIGKILL",
|
|
3761
|
+
maxBuffer: 65536
|
|
3762
|
+
});
|
|
3763
|
+
if (result.status !== 0 || result.error)
|
|
3764
|
+
return { verdict: "deny", reason: FAILED };
|
|
3765
|
+
const response = JSON.parse(result.stdout);
|
|
3766
|
+
const extra = Object.keys(response).some(
|
|
3767
|
+
(key) => key !== "decision" && key !== "reason"
|
|
3768
|
+
);
|
|
3769
|
+
const reason = response["reason"];
|
|
3770
|
+
if (extra || reason !== void 0 && typeof reason !== "string")
|
|
3771
|
+
return { verdict: "deny", reason: FAILED };
|
|
3772
|
+
if (response["decision"] === "allow") return pass;
|
|
3773
|
+
if (response["decision"] !== "deny")
|
|
3774
|
+
return { verdict: "deny", reason: FAILED };
|
|
3775
|
+
return {
|
|
3776
|
+
verdict: "deny",
|
|
3777
|
+
reason: typeof reason === "string" ? reason.slice(0, 2e3) : FAILED
|
|
3778
|
+
};
|
|
3779
|
+
}
|
|
3780
|
+
function customPolicy(entry) {
|
|
3781
|
+
return {
|
|
3782
|
+
id: `custom.${entry.name}`,
|
|
3783
|
+
events: [entry.event === "before_shell" ? "before_tool" : "after_tool"],
|
|
3784
|
+
tools: [entry.event === "before_shell" ? "shell" : "edit"],
|
|
3785
|
+
fail: "closed",
|
|
3786
|
+
defaultMode: "deny",
|
|
3787
|
+
options: () => ({}),
|
|
3788
|
+
evaluate: (input) => applies(entry, input) ? run2(entry, input) : pass
|
|
3789
|
+
};
|
|
3790
|
+
}
|
|
3791
|
+
|
|
3792
|
+
// src/hooks/policies/index.ts
|
|
3793
|
+
var byId = (list2) => Object.fromEntries(list2.map((policy) => [policy.id, policy]));
|
|
3794
|
+
var GUARDS = byId([
|
|
3795
|
+
inPlaceEdit,
|
|
3796
|
+
heredoc,
|
|
3797
|
+
stdinInterpreter,
|
|
3798
|
+
composeTeardown,
|
|
3799
|
+
hostApproval,
|
|
3800
|
+
secretExfiltration,
|
|
3801
|
+
pushWithoutVerify,
|
|
3802
|
+
forcedBranchMove,
|
|
3803
|
+
gateBypassEnv,
|
|
3804
|
+
stageAll
|
|
3805
|
+
]);
|
|
3806
|
+
var FEATURES = byId([
|
|
3807
|
+
registry,
|
|
3808
|
+
pause,
|
|
3809
|
+
leases,
|
|
3810
|
+
broadcast,
|
|
3811
|
+
mailbox,
|
|
3812
|
+
roles,
|
|
3813
|
+
sharedCheckout,
|
|
3814
|
+
worktreeSync,
|
|
3815
|
+
mainlineDeliver
|
|
3816
|
+
]);
|
|
3817
|
+
var POLICIES = {
|
|
3818
|
+
...FEATURES,
|
|
3819
|
+
...GUARDS,
|
|
3820
|
+
...byId([afterEdit, onStop])
|
|
3821
|
+
};
|
|
3822
|
+
|
|
3823
|
+
// src/hooks/compile/bindings.ts
|
|
3824
|
+
var BASE_TIMEOUT_SEC = 10;
|
|
3825
|
+
function listening(policy, options2) {
|
|
3826
|
+
const events = policy.eventsFor ? policy.eventsFor(policy.options(options2)) : policy.events;
|
|
3827
|
+
return { events, tools: policy.tools };
|
|
3828
|
+
}
|
|
3829
|
+
function listeners(policy, id) {
|
|
3830
|
+
const guards = policy.guards.map(
|
|
3831
|
+
(guard2) => listening(POLICIES[guard2.id], guard2.options)
|
|
3832
|
+
);
|
|
3833
|
+
const custom = policy.custom.filter((entry) => entry.targets.includes(id)).map((entry) => listening(customPolicy(entry), {}));
|
|
3834
|
+
return [...guards, ...custom];
|
|
3835
|
+
}
|
|
3836
|
+
function bindingsFor(policy, id) {
|
|
3837
|
+
const adapter = RUNTIMES[id];
|
|
3838
|
+
const wanted = /* @__PURE__ */ new Map();
|
|
3839
|
+
const want = (event, tools) => {
|
|
3840
|
+
const set = wanted.get(event) ?? /* @__PURE__ */ new Set();
|
|
3841
|
+
tools.forEach((tool) => set.add(tool));
|
|
3842
|
+
wanted.set(event, set);
|
|
3843
|
+
};
|
|
3844
|
+
for (const listener of listeners(policy, id)) {
|
|
3845
|
+
for (const event of listener.events)
|
|
3846
|
+
want(event, listener.tools ?? ["other"]);
|
|
3847
|
+
}
|
|
3848
|
+
if (policy.emergencyOffFile) want("before_tool", ["shell", "edit"]);
|
|
3849
|
+
const slowest = Math.max(0, ...policy.custom.map((entry) => entry.timeoutMs));
|
|
3850
|
+
const timeoutSec = Math.max(BASE_TIMEOUT_SEC, Math.ceil(slowest / 1e3) + 2);
|
|
3851
|
+
return [...wanted].flatMap(
|
|
3852
|
+
([event, tools]) => adapter.bindings(event, [...tools], timeoutSec)
|
|
3853
|
+
);
|
|
3854
|
+
}
|
|
3855
|
+
function loweringFor(targets, askFallback, asks) {
|
|
3856
|
+
const lowering = {};
|
|
3857
|
+
for (const id of targets) {
|
|
3858
|
+
if (RUNTIMES[id].capabilities.outputs.ask !== "unsupported") continue;
|
|
3859
|
+
if (askFallback === "refuse" && asks)
|
|
3860
|
+
throw new Error(
|
|
3861
|
+
`a guard may ask, and ${id} has no ask; set ask_fallback`
|
|
3862
|
+
);
|
|
3863
|
+
lowering[id] = { ask: askFallback === "warn" ? "warn" : "deny" };
|
|
3864
|
+
}
|
|
3865
|
+
return lowering;
|
|
3866
|
+
}
|
|
3867
|
+
|
|
3868
|
+
// src/hooks/compile/order.ts
|
|
3869
|
+
function compareIdentifiers(left, right) {
|
|
3870
|
+
if (left < right) return -1;
|
|
3871
|
+
return Number(left > right);
|
|
3872
|
+
}
|
|
3873
|
+
|
|
3874
|
+
// src/hooks/compile/types.ts
|
|
3875
|
+
var CUSTOM_TARGETS = RUNTIME_IDS;
|
|
3876
|
+
function asTable(value, where) {
|
|
3877
|
+
if (value === void 0) return {};
|
|
3878
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
3879
|
+
throw new Error(`${where} must be a table`);
|
|
3880
|
+
}
|
|
3881
|
+
return value;
|
|
3882
|
+
}
|
|
3883
|
+
|
|
3884
|
+
// src/hooks/compile/custom.ts
|
|
3885
|
+
var isRuntime = (target) => RUNTIME_IDS.includes(target);
|
|
3886
|
+
var SHELL_SERVED = [
|
|
3887
|
+
"codex",
|
|
3888
|
+
"gemini",
|
|
3889
|
+
"antigravity"
|
|
3890
|
+
];
|
|
3891
|
+
function legacyEntries(entries, enabled, bundleTargets = []) {
|
|
3892
|
+
const bundled = enabled ? bundleTargets : [];
|
|
3893
|
+
const unserved = entries.flatMap((entry) => entry.targets).find((id) => !SHELL_SERVED.includes(id) && !bundled.includes(id));
|
|
3894
|
+
if (unserved)
|
|
3895
|
+
throw new Error(
|
|
3896
|
+
`${unserved} custom guards need [hooks] targets that include ${unserved}`
|
|
3897
|
+
);
|
|
3898
|
+
if (enabled) {
|
|
3899
|
+
return entries.map((entry) => ({
|
|
3900
|
+
...entry,
|
|
3901
|
+
targets: entry.targets.filter((id) => !bundled.includes(id))
|
|
3902
|
+
})).filter((entry) => entry.targets.length > 0);
|
|
3903
|
+
}
|
|
3904
|
+
if (entries.some(
|
|
3905
|
+
(entry) => entry.event === "after_edit" && entry.targets.some((id) => id !== "codex")
|
|
3906
|
+
)) {
|
|
3907
|
+
throw new Error("edit hooks currently support codex only");
|
|
3908
|
+
}
|
|
3909
|
+
return [...entries];
|
|
3910
|
+
}
|
|
3911
|
+
function pinnedEntries(root, entries, targets) {
|
|
3912
|
+
return entries.map((entry) => ({
|
|
3913
|
+
...entry,
|
|
3914
|
+
targets: entry.targets.filter(
|
|
3915
|
+
(id) => isRuntime(id) && targets.includes(id)
|
|
3916
|
+
)
|
|
3917
|
+
})).filter((entry) => entry.targets.length > 0).map((entry) => ({
|
|
3918
|
+
...entry,
|
|
3919
|
+
guardSha256: guardHash(root, entry.command)
|
|
3920
|
+
}));
|
|
3921
|
+
}
|
|
3922
|
+
var FIELDS = ["name", "command", "targets", "timeout_ms", "on"];
|
|
3923
|
+
var EVENTS = ["before_shell", "after_edit"];
|
|
3924
|
+
function readEntry(root, value, implied) {
|
|
3925
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
3926
|
+
throw new Error("shell entry must be a table");
|
|
3927
|
+
const entry = value;
|
|
3928
|
+
if (Object.keys(entry).some(
|
|
3929
|
+
(key) => !FIELDS.includes(key) || key === "on" && implied
|
|
3930
|
+
)) {
|
|
3931
|
+
throw new Error("unknown shell entry field");
|
|
3932
|
+
}
|
|
3933
|
+
const { name, command: command6, targets } = entry;
|
|
3934
|
+
const timeoutMs = entry["timeout_ms"] ?? 15e3;
|
|
3935
|
+
const event = implied ?? entry["on"] ?? "before_shell";
|
|
3936
|
+
if (typeof name !== "string" || !/^[a-z0-9][a-z0-9-]*$/u.test(name)) {
|
|
3937
|
+
throw new Error("shell names must be unique lowercase names");
|
|
3938
|
+
}
|
|
3939
|
+
const parts = Array.isArray(command6) ? command6 : [];
|
|
3940
|
+
if (parts.length === 0 || !parts.every(
|
|
3941
|
+
(part) => typeof part === "string" && part.length > 0 && !part.includes("\0")
|
|
3942
|
+
)) {
|
|
3943
|
+
throw new Error("command must be a nonempty argument list");
|
|
3944
|
+
}
|
|
3945
|
+
const list2 = Array.isArray(targets) ? targets : [];
|
|
3946
|
+
if (list2.length === 0 || !list2.every((id) => CUSTOM_TARGETS.includes(id)) || new Set(list2).size !== list2.length) {
|
|
3947
|
+
throw new Error("targets must select unique supported shell adapters");
|
|
3948
|
+
}
|
|
3949
|
+
if (typeof timeoutMs !== "number" || !Number.isSafeInteger(timeoutMs) || timeoutMs < 100 || timeoutMs > 6e4) {
|
|
3950
|
+
throw new Error("timeout_ms must be an integer from 100 to 60000");
|
|
3951
|
+
}
|
|
3952
|
+
if (typeof event !== "string" || !EVENTS.includes(event))
|
|
3953
|
+
throw new Error(`on must be one of ${EVENTS.join(", ")}`);
|
|
3954
|
+
guardScript(root, parts);
|
|
3955
|
+
return {
|
|
3956
|
+
name,
|
|
3957
|
+
command: parts,
|
|
3958
|
+
targets: list2,
|
|
3959
|
+
timeoutMs,
|
|
3960
|
+
event
|
|
3961
|
+
};
|
|
3962
|
+
}
|
|
3963
|
+
function readCustom(root, hooks, guards) {
|
|
3964
|
+
const sources = [
|
|
3965
|
+
[hooks["shell"], "before_shell"],
|
|
3966
|
+
[hooks["edit"], "after_edit"],
|
|
3967
|
+
[guards["custom"], void 0]
|
|
3968
|
+
];
|
|
3969
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
3970
|
+
const found = [];
|
|
3971
|
+
for (const [list2, implied] of sources) {
|
|
3972
|
+
if (list2 === void 0) continue;
|
|
3973
|
+
if (!Array.isArray(list2))
|
|
3974
|
+
throw new Error("custom guards must be arrays of tables");
|
|
3975
|
+
for (const value of list2) {
|
|
3976
|
+
const entry = readEntry(root, value, implied);
|
|
3977
|
+
if (names2.has(entry.name))
|
|
3978
|
+
throw new Error("shell names must be unique lowercase names");
|
|
3979
|
+
names2.add(entry.name);
|
|
3980
|
+
found.push(entry);
|
|
3981
|
+
}
|
|
3982
|
+
}
|
|
3983
|
+
return found.sort((a, b) => compareIdentifiers(a.name, b.name));
|
|
3984
|
+
}
|
|
3985
|
+
|
|
3986
|
+
// src/hooks/compile/guards.ts
|
|
3987
|
+
var MODES = ["deny", "ask", "warn", "off", "graded"];
|
|
3988
|
+
var GROUPS = ["shell", "git"];
|
|
3989
|
+
function readGuard(id, raw) {
|
|
3990
|
+
const policy = GUARDS[id];
|
|
3991
|
+
if (!policy) throw new Error(`unknown guard hooks.guards.${id}`);
|
|
3992
|
+
const { mode = policy.defaultMode, ...options2 } = asTable(
|
|
3993
|
+
raw,
|
|
3994
|
+
`[hooks.guards.${id}]`
|
|
3995
|
+
);
|
|
3996
|
+
if (!MODES.includes(mode))
|
|
3997
|
+
throw new Error(
|
|
3998
|
+
`[hooks.guards.${id}] mode must be one of ${MODES.join(", ")}`
|
|
3999
|
+
);
|
|
4000
|
+
if (mode === "graded" && policy.defaultMode !== "graded") {
|
|
4001
|
+
throw new Error(
|
|
4002
|
+
`[hooks.guards.${id}] graded applies only to guards that grade their findings`
|
|
4003
|
+
);
|
|
4004
|
+
}
|
|
4005
|
+
if (mode === "off") return null;
|
|
4006
|
+
try {
|
|
4007
|
+
policy.options(options2);
|
|
4008
|
+
} catch (error) {
|
|
4009
|
+
throw new Error(
|
|
4010
|
+
`[hooks.guards.${id}] ${error instanceof Error ? error.message : String(error)}`
|
|
4011
|
+
);
|
|
4012
|
+
}
|
|
4013
|
+
return { id, mode, options: options2 };
|
|
4014
|
+
}
|
|
4015
|
+
function readGuards(guards) {
|
|
4016
|
+
const found = [];
|
|
4017
|
+
for (const [group, value] of Object.entries(guards)) {
|
|
4018
|
+
if (group === "custom") continue;
|
|
4019
|
+
if (!GROUPS.includes(group))
|
|
4020
|
+
throw new Error(`unknown guard group hooks.guards.${group}`);
|
|
4021
|
+
for (const [name, raw] of Object.entries(
|
|
4022
|
+
asTable(value, `[hooks.guards.${group}]`)
|
|
4023
|
+
)) {
|
|
4024
|
+
const guard2 = readGuard(`${group}.${name}`, raw);
|
|
4025
|
+
if (guard2) found.push(guard2);
|
|
4026
|
+
}
|
|
4027
|
+
}
|
|
4028
|
+
return found.sort((a, b) => compareIdentifiers(a.id, b.id));
|
|
4029
|
+
}
|
|
4030
|
+
|
|
4031
|
+
// src/hooks/compile/checks.ts
|
|
4032
|
+
function readChecks(raw) {
|
|
4033
|
+
const table3 = asTable(raw, "[hooks.checks]");
|
|
4034
|
+
const definitions = { after_edit: afterEdit, on_stop: onStop };
|
|
4035
|
+
return Object.entries(table3).map(([name, value]) => {
|
|
4036
|
+
const policy = definitions[name];
|
|
4037
|
+
if (!policy) throw new Error(`unknown check hooks.checks.${name}`);
|
|
4038
|
+
const options2 = asTable(value, `[hooks.checks.${name}]`);
|
|
4039
|
+
policy.options(options2);
|
|
4040
|
+
return { id: policy.id, mode: "warn", options: options2 };
|
|
4041
|
+
});
|
|
4042
|
+
}
|
|
4043
|
+
|
|
4044
|
+
// src/hooks/compile/hooks-table.ts
|
|
4045
|
+
var KEYS2 = [
|
|
4046
|
+
"targets",
|
|
4047
|
+
"ask_fallback",
|
|
4048
|
+
"error_log",
|
|
4049
|
+
"emergency_off_file",
|
|
4050
|
+
"guards",
|
|
4051
|
+
"checks",
|
|
4052
|
+
"shell",
|
|
4053
|
+
"edit"
|
|
4054
|
+
];
|
|
4055
|
+
var LEGACY = /* @__PURE__ */ new Set(["shell", "edit"]);
|
|
4056
|
+
var FALLBACKS = ["deny", "warn", "refuse"];
|
|
4057
|
+
function relativePath(value, key) {
|
|
4058
|
+
if (value === void 0) return void 0;
|
|
4059
|
+
if (typeof value !== "string" || value === "" || value.startsWith("/") || value.split("/").includes("..")) {
|
|
4060
|
+
throw new Error(`${key} must be a path inside the project`);
|
|
4061
|
+
}
|
|
4062
|
+
return value;
|
|
4063
|
+
}
|
|
4064
|
+
function offFile(value) {
|
|
4065
|
+
if (value === void 0) return void 0;
|
|
4066
|
+
if (typeof value !== "string" || value === "") {
|
|
4067
|
+
throw new Error("emergency_off_file must be a path");
|
|
4068
|
+
}
|
|
4069
|
+
return value;
|
|
4070
|
+
}
|
|
4071
|
+
function errorLog(value) {
|
|
4072
|
+
if (value === void 0 || value === true) return { enabled: true };
|
|
4073
|
+
if (value === false) return { enabled: false };
|
|
4074
|
+
return { enabled: true, file: relativePath(value, "error_log") };
|
|
4075
|
+
}
|
|
4076
|
+
function readHooksTable(table3, tools) {
|
|
4077
|
+
const unknown = Object.keys(table3).find((key) => !KEYS2.includes(key));
|
|
4078
|
+
if (unknown) throw new Error(`unknown key ${unknown}`);
|
|
4079
|
+
const enabled = Object.keys(table3).some((key) => !LEGACY.has(key));
|
|
4080
|
+
const declared = table3["targets"];
|
|
4081
|
+
if (declared !== void 0 && (!Array.isArray(declared) || !declared.every((id) => RUNTIME_IDS.includes(id)))) {
|
|
4082
|
+
throw new Error(
|
|
4083
|
+
`targets must list runtimes from ${RUNTIME_IDS.join(", ")}`
|
|
4084
|
+
);
|
|
4085
|
+
}
|
|
4086
|
+
const targets = declared ?? DEFAULT_RUNTIME_IDS.filter((id) => tools.has(id));
|
|
4087
|
+
const askFallback = table3["ask_fallback"] ?? "deny";
|
|
4088
|
+
if (typeof askFallback !== "string" || !FALLBACKS.includes(askFallback)) {
|
|
4089
|
+
throw new Error(`ask_fallback must be one of ${FALLBACKS.join(", ")}`);
|
|
4090
|
+
}
|
|
4091
|
+
const log = errorLog(table3["error_log"]);
|
|
4092
|
+
const emergencyOffFile = offFile(table3["emergency_off_file"]);
|
|
4093
|
+
return {
|
|
4094
|
+
enabled,
|
|
4095
|
+
targets: [...new Set(targets)],
|
|
4096
|
+
askFallback,
|
|
4097
|
+
logEnabled: log.enabled,
|
|
4098
|
+
...log.file ? { errorLog: log.file } : {},
|
|
4099
|
+
...emergencyOffFile ? { emergencyOffFile } : {}
|
|
4100
|
+
};
|
|
4101
|
+
}
|
|
4102
|
+
|
|
4103
|
+
// src/hooks/compile/sessions.ts
|
|
4104
|
+
var KEYS3 = [
|
|
4105
|
+
"store",
|
|
4106
|
+
"state_dir",
|
|
4107
|
+
"registry",
|
|
4108
|
+
"pause",
|
|
4109
|
+
"broadcast",
|
|
4110
|
+
"leases",
|
|
4111
|
+
"roles",
|
|
4112
|
+
"mailbox"
|
|
4113
|
+
];
|
|
4114
|
+
var MODES2 = ["deny", "ask", "warn", "off"];
|
|
4115
|
+
function guard(id, options2, mode) {
|
|
4116
|
+
try {
|
|
4117
|
+
FEATURES[id].options(options2);
|
|
4118
|
+
} catch (error) {
|
|
4119
|
+
throw new Error(
|
|
4120
|
+
`[${id}] ${error instanceof Error ? error.message : String(error)}`
|
|
4121
|
+
);
|
|
4122
|
+
}
|
|
4123
|
+
return { id, mode, options: options2 };
|
|
4124
|
+
}
|
|
4125
|
+
function pauseGuard(raw) {
|
|
4126
|
+
const { mode = "deny", ...options2 } = asTable(raw, "[sessions.pause]");
|
|
4127
|
+
if (!MODES2.includes(mode))
|
|
4128
|
+
throw new Error(`[sessions.pause] mode must be one of ${MODES2.join(", ")}`);
|
|
4129
|
+
return guard("sessions.pause", options2, mode);
|
|
4130
|
+
}
|
|
4131
|
+
function registryCheck(raw) {
|
|
4132
|
+
const { liveness = "pid_heartbeat", ...rest } = asTable(
|
|
4133
|
+
raw,
|
|
4134
|
+
"[sessions.registry]"
|
|
4135
|
+
);
|
|
4136
|
+
if (liveness !== "pid_heartbeat")
|
|
4137
|
+
throw new Error('[sessions.registry] liveness must be "pid_heartbeat"');
|
|
4138
|
+
const unknown = Object.keys(rest).find((key) => key !== "stale_minutes");
|
|
4139
|
+
if (unknown) throw new Error(`[sessions.registry] unknown key ${unknown}`);
|
|
4140
|
+
}
|
|
4141
|
+
function readSessions(raw) {
|
|
4142
|
+
const table3 = asTable(raw, "[sessions]");
|
|
4143
|
+
const unknown = Object.keys(table3).find((key) => !KEYS3.includes(key));
|
|
4144
|
+
if (unknown) throw new Error(`[sessions] unknown key ${unknown}`);
|
|
4145
|
+
const settings = readStoreSettings(raw);
|
|
4146
|
+
registryCheck(table3["registry"]);
|
|
4147
|
+
const guards = [];
|
|
4148
|
+
if (table3["registry"] !== void 0)
|
|
4149
|
+
guards.push(guard("sessions.registry", {}, "warn"));
|
|
4150
|
+
if (table3["pause"] !== void 0) guards.push(pauseGuard(table3["pause"]));
|
|
4151
|
+
if (table3["leases"] !== void 0)
|
|
4152
|
+
guards.push(
|
|
4153
|
+
guard(
|
|
4154
|
+
"sessions.leases",
|
|
4155
|
+
asTable(table3["leases"], "[sessions.leases]"),
|
|
4156
|
+
"deny"
|
|
4157
|
+
)
|
|
4158
|
+
);
|
|
4159
|
+
if (table3["roles"] !== void 0)
|
|
4160
|
+
guards.push(
|
|
4161
|
+
guard(
|
|
4162
|
+
"sessions.roles",
|
|
4163
|
+
asTable(table3["roles"], "[sessions.roles]"),
|
|
4164
|
+
"deny"
|
|
4165
|
+
)
|
|
4166
|
+
);
|
|
4167
|
+
if (table3["broadcast"] !== void 0) {
|
|
4168
|
+
const { keep: _keep, ...options2 } = asTable(
|
|
4169
|
+
table3["broadcast"],
|
|
4170
|
+
"[sessions.broadcast]"
|
|
4171
|
+
);
|
|
4172
|
+
guards.push(guard("sessions.broadcast", options2, "warn"));
|
|
4173
|
+
}
|
|
4174
|
+
if (settings.mailbox?.enabled)
|
|
4175
|
+
guards.push(
|
|
4176
|
+
guard(
|
|
4177
|
+
"sessions.mailbox",
|
|
4178
|
+
{ roles: asTable(table3["roles"] ?? {}, "[sessions.roles]") },
|
|
4179
|
+
"warn"
|
|
4180
|
+
)
|
|
4181
|
+
);
|
|
4182
|
+
return {
|
|
4183
|
+
...settings,
|
|
4184
|
+
declared: guards.length > 0,
|
|
4185
|
+
guards: guards.filter((entry) => entry.mode !== "off")
|
|
4186
|
+
};
|
|
4187
|
+
}
|
|
4188
|
+
|
|
4189
|
+
// src/hooks/compile/workspace.ts
|
|
4190
|
+
function featureSource(declared, setting, on) {
|
|
4191
|
+
if (declared || setting !== "auto") return "toml";
|
|
4192
|
+
return on ? "auto" : "default-off";
|
|
4193
|
+
}
|
|
4194
|
+
function enabledValue(value, where) {
|
|
4195
|
+
if (value === void 0 || value === "auto" || typeof value === "boolean")
|
|
4196
|
+
return value ?? "auto";
|
|
4197
|
+
throw new Error(`${where} enabled must be "auto", true or false`);
|
|
4198
|
+
}
|
|
4199
|
+
function readWorkspace(raw, root) {
|
|
4200
|
+
const git = asTable(raw, "[git]");
|
|
4201
|
+
const gitEnabled = enabledValue(git["enabled"], "[git]");
|
|
4202
|
+
const declared = git["shared_checkout"] !== void 0;
|
|
4203
|
+
const { enabled, ...options2 } = asTable(
|
|
4204
|
+
git["shared_checkout"],
|
|
4205
|
+
"[git.shared_checkout]"
|
|
4206
|
+
);
|
|
4207
|
+
const own = enabledValue(enabled, "[git.shared_checkout]");
|
|
4208
|
+
const setting = own === "auto" ? gitEnabled : own;
|
|
4209
|
+
const on = setting === "auto" ? checkoutOf(root) !== null : setting;
|
|
4210
|
+
const source = featureSource(declared, setting, on);
|
|
4211
|
+
if (!on) return { declared, sharedCheckout: { guard: null, source } };
|
|
4212
|
+
try {
|
|
4213
|
+
FEATURES["git.shared_checkout"].options(options2);
|
|
4214
|
+
} catch (error) {
|
|
4215
|
+
throw new Error(
|
|
4216
|
+
`[git.shared_checkout] ${error instanceof Error ? error.message : String(error)}`
|
|
4217
|
+
);
|
|
4218
|
+
}
|
|
4219
|
+
return {
|
|
4220
|
+
declared,
|
|
4221
|
+
sharedCheckout: {
|
|
4222
|
+
guard: { id: "git.shared_checkout", mode: "warn", options: options2 },
|
|
4223
|
+
source
|
|
4224
|
+
}
|
|
4225
|
+
};
|
|
4226
|
+
}
|
|
4227
|
+
|
|
4228
|
+
// src/hooks/compile/worktree-sync.ts
|
|
4229
|
+
var SESSION_START = ["fetch_and_move", "move", "off"];
|
|
4230
|
+
var BETWEEN_PROMPTS = ["move_if_light", "off"];
|
|
4231
|
+
function readWorktreeSync(tables, root) {
|
|
4232
|
+
if (tables.git === void 0) return null;
|
|
4233
|
+
const config = {
|
|
4234
|
+
path: "pmcp.toml",
|
|
4235
|
+
dir: root,
|
|
4236
|
+
raw: { git: tables.git, sessions: tables.sessions }
|
|
4237
|
+
};
|
|
4238
|
+
const git = readGitConfig(config);
|
|
4239
|
+
const sync = git?.worktreeSync;
|
|
4240
|
+
if (!git || !sync) return null;
|
|
4241
|
+
if (!SESSION_START.includes(sync.onSessionStart)) {
|
|
4242
|
+
throw new Error(
|
|
4243
|
+
`[git.worktrees.sync] on_session_start must be one of ${SESSION_START.join(", ")}`
|
|
4244
|
+
);
|
|
4245
|
+
}
|
|
4246
|
+
if (!BETWEEN_PROMPTS.includes(sync.betweenPrompts)) {
|
|
4247
|
+
throw new Error(
|
|
4248
|
+
`[git.worktrees.sync] between_prompts must be one of ${BETWEEN_PROMPTS.join(", ")}`
|
|
4249
|
+
);
|
|
4250
|
+
}
|
|
4251
|
+
const options2 = {
|
|
4252
|
+
root: git.worktrees?.root ?? ".claude/worktrees",
|
|
4253
|
+
...git.remote ? { remote: git.remote } : {},
|
|
4254
|
+
...git.mainline ? { mainline: git.mainline } : {},
|
|
4255
|
+
onSessionStart: sync.onSessionStart,
|
|
4256
|
+
betweenPrompts: sync.betweenPrompts,
|
|
4257
|
+
installAfterMove: sync.installAfterMove,
|
|
4258
|
+
...git.sync?.install ? { install: git.sync.install } : {},
|
|
4259
|
+
installWhen: git.sync?.installWhenChanged ?? [],
|
|
4260
|
+
noticePaths: sync.noticePaths,
|
|
4261
|
+
followSubmodules: git.submodules?.follow === "clean_pins"
|
|
4262
|
+
};
|
|
4263
|
+
return {
|
|
4264
|
+
id: "git.worktrees.sync",
|
|
4265
|
+
mode: "warn",
|
|
4266
|
+
options: options2
|
|
4267
|
+
};
|
|
4268
|
+
}
|
|
4269
|
+
|
|
4270
|
+
// src/hooks/compile/mainline-deliver.ts
|
|
4271
|
+
function readMainlineDeliver(tables, root) {
|
|
4272
|
+
if (tables.git === void 0) return null;
|
|
4273
|
+
const config = {
|
|
4274
|
+
path: "pmcp.toml",
|
|
4275
|
+
dir: root,
|
|
4276
|
+
raw: { git: tables.git, sessions: tables.sessions }
|
|
4277
|
+
};
|
|
4278
|
+
const deliver = readGitConfig(config)?.deliver;
|
|
4279
|
+
if (!deliver?.refuseDirectPush) return null;
|
|
4280
|
+
return { id: "git.mainline.deliver", mode: "deny", options: {} };
|
|
4281
|
+
}
|
|
4282
|
+
|
|
4283
|
+
// src/hooks/compile.ts
|
|
4284
|
+
function hostGuard(host, guards) {
|
|
4285
|
+
if (host === void 0) return null;
|
|
4286
|
+
const shell = asTable(guards["shell"], "[hooks.guards.shell]");
|
|
4287
|
+
if (shell["host_approval"] !== void 0) return null;
|
|
4288
|
+
return { id: "shell.host_approval", mode: "deny", options: {} };
|
|
4289
|
+
}
|
|
4290
|
+
function compileHooks(tables, options2) {
|
|
4291
|
+
const hooks = asTable(tables.hooks, "[hooks]");
|
|
4292
|
+
const table3 = readHooksTable(hooks, options2.tools);
|
|
4293
|
+
const guardsTable = asTable(hooks["guards"], "[hooks.guards]");
|
|
4294
|
+
const entries = readCustom(options2.root, hooks, guardsTable);
|
|
4295
|
+
const sessions = readSessions(tables.sessions);
|
|
4296
|
+
const workspace = readWorkspace(tables.git, options2.root);
|
|
4297
|
+
const deskSync = readWorktreeSync(tables, options2.root);
|
|
4298
|
+
const directPush = readMainlineDeliver(tables, options2.root);
|
|
4299
|
+
const host = hostGuard(tables.host, guardsTable);
|
|
4300
|
+
const enabled = table3.enabled || host !== null || sessions.declared || workspace.declared || !!deskSync || !!directPush;
|
|
4301
|
+
const legacy = legacyEntries(entries, enabled, table3.targets);
|
|
4302
|
+
if (!enabled)
|
|
4303
|
+
return {
|
|
4304
|
+
policy: null,
|
|
4305
|
+
edits: [],
|
|
4306
|
+
files: [],
|
|
4307
|
+
legacy,
|
|
4308
|
+
sources: { "git.shared_checkout": workspace.sharedCheckout.source }
|
|
4309
|
+
};
|
|
4310
|
+
const shared2 = workspace.sharedCheckout.guard;
|
|
4311
|
+
const guards = [
|
|
4312
|
+
...sessions.guards,
|
|
4313
|
+
...shared2 ? [shared2] : [],
|
|
4314
|
+
...deskSync ? [deskSync] : [],
|
|
4315
|
+
...directPush ? [directPush] : [],
|
|
4316
|
+
...host ? [host] : [],
|
|
4317
|
+
...readGuards(guardsTable),
|
|
4318
|
+
...readChecks(hooks["checks"])
|
|
4319
|
+
];
|
|
4320
|
+
const asks = guards.some(
|
|
4321
|
+
(guard2) => guard2.mode === "ask" || guard2.mode === "graded"
|
|
4322
|
+
);
|
|
4323
|
+
const policy = {
|
|
4324
|
+
contract: 2,
|
|
4325
|
+
targets: table3.targets,
|
|
4326
|
+
...table3.emergencyOffFile ? { emergencyOffFile: table3.emergencyOffFile } : {},
|
|
4327
|
+
...sessions.stateDir ? { stateDir: sessions.stateDir } : {},
|
|
4328
|
+
...table3.errorLog ? { logFile: table3.errorLog } : {},
|
|
4329
|
+
log: { ...readLoggingTable(tables.logging), enabled: table3.logEnabled },
|
|
4330
|
+
sessions: {
|
|
4331
|
+
staleMs: sessions.staleMs,
|
|
4332
|
+
keepBroadcasts: sessions.keepBroadcasts,
|
|
4333
|
+
// Only when on, so projects without a mailbox keep the same policy.json and hash.
|
|
4334
|
+
...sessions.mailbox?.enabled ? { mailbox: sessions.mailbox } : {}
|
|
4335
|
+
},
|
|
4336
|
+
guards,
|
|
4337
|
+
custom: pinnedEntries(options2.root, entries, table3.targets),
|
|
4338
|
+
lowering: loweringFor(table3.targets, table3.askFallback, asks)
|
|
4339
|
+
};
|
|
4340
|
+
const projecting = table3.targets.filter((id) => {
|
|
4341
|
+
const host2 = RUNTIMES[id].ridesOn?.runtime;
|
|
4342
|
+
return !host2 || !table3.targets.includes(host2);
|
|
4343
|
+
});
|
|
4344
|
+
const plans = projecting.map((id) => ({
|
|
4345
|
+
adapter: RUNTIMES[id],
|
|
4346
|
+
plan: { bindings: bindingsFor(policy, id) }
|
|
4347
|
+
}));
|
|
4348
|
+
const edits = plans.flatMap(({ adapter, plan }) => adapter.project(plan));
|
|
4349
|
+
const files2 = plans.flatMap(
|
|
4350
|
+
({ adapter, plan }) => adapter.files?.(plan) ?? []
|
|
4351
|
+
);
|
|
4352
|
+
return {
|
|
4353
|
+
policy,
|
|
4354
|
+
edits,
|
|
4355
|
+
files: files2,
|
|
4356
|
+
legacy,
|
|
4357
|
+
sources: { "git.shared_checkout": workspace.sharedCheckout.source }
|
|
4358
|
+
};
|
|
4359
|
+
}
|
|
4360
|
+
|
|
4361
|
+
// src/project/hook-spec.ts
|
|
4362
|
+
function readHooks(path, root, raw, tools) {
|
|
4363
|
+
let compiled;
|
|
4364
|
+
try {
|
|
4365
|
+
compiled = compileHooks(
|
|
4366
|
+
{
|
|
4367
|
+
hooks: raw["hooks"],
|
|
4368
|
+
sessions: raw["sessions"],
|
|
4369
|
+
git: raw["git"],
|
|
4370
|
+
logging: raw["logging"],
|
|
4371
|
+
host: raw["host"]
|
|
4372
|
+
},
|
|
4373
|
+
{ root, tools }
|
|
4374
|
+
);
|
|
4375
|
+
} catch (error) {
|
|
4376
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4377
|
+
throw new ConfigError(
|
|
4378
|
+
path,
|
|
4379
|
+
message.startsWith("[") ? message : `[hooks] ${message}`
|
|
4380
|
+
);
|
|
4381
|
+
}
|
|
4382
|
+
const legacy = compiled.legacy.map((entry) => ({
|
|
4383
|
+
name: entry.name,
|
|
4384
|
+
command: entry.command,
|
|
4385
|
+
targets: entry.targets,
|
|
4386
|
+
timeoutMs: entry.timeoutMs,
|
|
4387
|
+
...entry.event === "after_edit" ? { event: "after_edit" } : {}
|
|
4388
|
+
}));
|
|
4389
|
+
const bundle = compiled.policy ? {
|
|
4390
|
+
policy: compiled.policy,
|
|
4391
|
+
edits: compiled.edits,
|
|
4392
|
+
files: compiled.files,
|
|
4393
|
+
sources: compiled.sources
|
|
4394
|
+
} : null;
|
|
4395
|
+
return {
|
|
4396
|
+
legacy,
|
|
4397
|
+
bundle,
|
|
4398
|
+
sources: compiled.sources,
|
|
4399
|
+
deprecations: deprecations(raw["hooks"])
|
|
4400
|
+
};
|
|
4401
|
+
}
|
|
4402
|
+
function deprecations(hooks) {
|
|
4403
|
+
const table3 = hooks ?? {};
|
|
4404
|
+
return ["shell", "edit"].filter((key) => table3[key] !== void 0).map(
|
|
4405
|
+
(key) => `[[hooks.${key}]] is deprecated; declare [[hooks.guards.custom]] with on = "${key === "shell" ? "before_shell" : "after_edit"}" and list its runtimes in [hooks] targets`
|
|
4406
|
+
);
|
|
4407
|
+
}
|
|
4408
|
+
|
|
4409
|
+
// src/project/ui-claude.ts
|
|
4410
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
4411
|
+
import { join as join2 } from "path";
|
|
4412
|
+
|
|
4413
|
+
// src/ui/claude/reader/read.ts
|
|
4414
|
+
var STATE_PATHS = {
|
|
4415
|
+
records: STATE_FILES.records,
|
|
4416
|
+
pause: STATE_FILES.pause,
|
|
4417
|
+
lease: leaseFile("lockfile"),
|
|
4418
|
+
broadcast: STATE_FILES.broadcast
|
|
4419
|
+
};
|
|
4420
|
+
|
|
4421
|
+
// src/ui/claude/reader/settings.ts
|
|
4422
|
+
var UI_SETTINGS_FILE = ".agents/pmcp-hooks/ui-claude.json";
|
|
4423
|
+
|
|
4424
|
+
// src/project/ui-claude.ts
|
|
4425
|
+
var KEYS4 = {
|
|
4426
|
+
sessions_pane: "sessionsPane",
|
|
4427
|
+
status_band: "statusBand",
|
|
4428
|
+
commands: "commands",
|
|
4429
|
+
hold_foreign_edits: "holdForeignEdits"
|
|
4430
|
+
};
|
|
4431
|
+
function readUiClaude(path, raw, sessionHooks) {
|
|
4432
|
+
if (raw === void 0) return void 0;
|
|
4433
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
4434
|
+
throw new ConfigError(path, "[ui] must be a table");
|
|
4435
|
+
const claude2 = raw["claude"];
|
|
4436
|
+
if (claude2 === void 0) return void 0;
|
|
4437
|
+
if (claude2 === null || typeof claude2 !== "object" || Array.isArray(claude2))
|
|
4438
|
+
throw new ConfigError(path, "[ui.claude] must be a table");
|
|
4439
|
+
const table3 = claude2;
|
|
4440
|
+
const switches = {};
|
|
4441
|
+
for (const [key, value] of Object.entries(table3)) {
|
|
4442
|
+
if (!(key in KEYS4))
|
|
4443
|
+
throw new ConfigError(path, `[ui.claude] unknown key ${key}`);
|
|
4444
|
+
if (typeof value !== "boolean")
|
|
4445
|
+
throw new ConfigError(path, `[ui.claude] ${key} must be a boolean`);
|
|
4446
|
+
switches[KEYS4[key]] = value;
|
|
4447
|
+
}
|
|
4448
|
+
if (!sessionHooks)
|
|
4449
|
+
throw new ConfigError(
|
|
4450
|
+
path,
|
|
4451
|
+
"[ui.claude] needs the session hooks: declare [sessions.registry]"
|
|
4452
|
+
);
|
|
4453
|
+
return {
|
|
4454
|
+
sessionsPane: switches["sessionsPane"] ?? true,
|
|
4455
|
+
statusBand: switches["statusBand"] ?? true,
|
|
4456
|
+
commands: switches["commands"] ?? true,
|
|
4457
|
+
holdForeignEdits: switches["holdForeignEdits"] ?? true
|
|
4458
|
+
};
|
|
4459
|
+
}
|
|
4460
|
+
var settingsText = (ui) => `${JSON.stringify(ui, null, 2)}
|
|
4461
|
+
`;
|
|
4462
|
+
function uiClaudeOutputs(spec) {
|
|
4463
|
+
if (!spec.uiClaude || !spec.tools.has("claude")) return [];
|
|
4464
|
+
return [
|
|
4465
|
+
{
|
|
4466
|
+
kind: "file",
|
|
4467
|
+
path: UI_SETTINGS_FILE,
|
|
4468
|
+
content: settingsText(spec.uiClaude)
|
|
4469
|
+
}
|
|
4470
|
+
];
|
|
4471
|
+
}
|
|
4472
|
+
function uiClaudeDoctor(spec) {
|
|
4473
|
+
const ui = spec.uiClaude;
|
|
4474
|
+
if (!ui) return [];
|
|
4475
|
+
const path = join2(spec.root, UI_SETTINGS_FILE);
|
|
4476
|
+
const current = existsSync3(path) ? readFileSync3(path, "utf8") : null;
|
|
4477
|
+
if (current !== settingsText(ui))
|
|
4478
|
+
return [
|
|
4479
|
+
{
|
|
4480
|
+
tool: "claude",
|
|
4481
|
+
check: "ui.claude mod settings",
|
|
4482
|
+
status: "fail",
|
|
4483
|
+
detail: `${UI_SETTINGS_FILE} does not match [ui.claude]; run pmcp project`
|
|
4484
|
+
}
|
|
4485
|
+
];
|
|
4486
|
+
const switches = Object.entries(KEYS4).map(
|
|
4487
|
+
([key, field]) => `${key} ${ui[field] ? "on" : "off"}`
|
|
4488
|
+
);
|
|
4489
|
+
return [
|
|
4490
|
+
{
|
|
4491
|
+
tool: "claude",
|
|
4492
|
+
check: "ui.claude mod settings",
|
|
4493
|
+
status: "ok",
|
|
4494
|
+
detail: `${switches.join(", ")}; the mod loads with the pmcp plugin`
|
|
4495
|
+
}
|
|
4496
|
+
];
|
|
4497
|
+
}
|
|
4498
|
+
|
|
4499
|
+
// src/project/permissions.ts
|
|
4500
|
+
import { join as join4 } from "path";
|
|
4501
|
+
|
|
4502
|
+
// src/project/apply/_shared.ts
|
|
4503
|
+
import { createHash as createHash3 } from "crypto";
|
|
4504
|
+
import { existsSync as existsSync4, lstatSync, readFileSync as readFileSync4, readlinkSync } from "fs";
|
|
4505
|
+
import { join as join3 } from "path";
|
|
4506
|
+
var BEGIN = "# >>> pmcp project (edit pmcp.toml, not this block) >>>";
|
|
4507
|
+
var END = "# <<< pmcp project <<<";
|
|
4508
|
+
function linkOf(abs) {
|
|
4509
|
+
try {
|
|
4510
|
+
return lstatSync(abs).isSymbolicLink() ? readlinkSync(abs) : null;
|
|
4511
|
+
} catch {
|
|
4512
|
+
return null;
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
var exists = (abs) => existsSync4(abs) || linkOf(abs) !== null;
|
|
4516
|
+
function fingerprint(abs) {
|
|
4517
|
+
if (!existsSync4(abs)) return "absent";
|
|
4518
|
+
return createHash3("sha256").update(readFileSync4(abs)).digest("hex");
|
|
4519
|
+
}
|
|
4520
|
+
function changedSincePlan(root, basis) {
|
|
4521
|
+
return [...basis].filter(([file, planned]) => fingerprint(join3(root, file)) !== planned).map(([file]) => `${file} changed since the plan; run pmcp project again`);
|
|
4522
|
+
}
|
|
4523
|
+
function readJson2(abs) {
|
|
4524
|
+
if (!existsSync4(abs)) return {};
|
|
4525
|
+
let value;
|
|
4526
|
+
try {
|
|
4527
|
+
value = JSON.parse(readFileSync4(abs, "utf8"));
|
|
4528
|
+
} catch {
|
|
4529
|
+
throw new ProjectError(
|
|
4530
|
+
"Cannot parse native JSON configuration; contents are redacted"
|
|
4531
|
+
);
|
|
4532
|
+
}
|
|
4533
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
4534
|
+
}
|
|
4535
|
+
function getKey(object8, key) {
|
|
4536
|
+
let node = object8;
|
|
4537
|
+
for (const part of key) {
|
|
4538
|
+
if (node === null || typeof node !== "object") return void 0;
|
|
4539
|
+
node = node[part];
|
|
4540
|
+
}
|
|
4541
|
+
return node;
|
|
4542
|
+
}
|
|
4543
|
+
function setKey(object8, key, value) {
|
|
4544
|
+
let node = object8;
|
|
4545
|
+
for (const part of key.slice(0, -1)) {
|
|
4546
|
+
const next = node[part];
|
|
4547
|
+
if (next === null || typeof next !== "object" || Array.isArray(next)) {
|
|
4548
|
+
node[part] = {};
|
|
4549
|
+
}
|
|
4550
|
+
node = node[part];
|
|
4551
|
+
}
|
|
4552
|
+
node[key[key.length - 1] ?? ""] = value;
|
|
4553
|
+
}
|
|
4554
|
+
function deleteKey(object8, key) {
|
|
4555
|
+
const [head, ...rest] = key;
|
|
4556
|
+
if (head === void 0 || !(head in object8)) return;
|
|
4557
|
+
if (rest.length === 0) {
|
|
4558
|
+
delete object8[head];
|
|
4559
|
+
return;
|
|
4560
|
+
}
|
|
4561
|
+
const child = object8[head];
|
|
4562
|
+
if (child === null || typeof child !== "object") return;
|
|
4563
|
+
deleteKey(child, rest);
|
|
4564
|
+
if (Object.keys(child).length === 0) delete object8[head];
|
|
4565
|
+
}
|
|
4566
|
+
var same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
4567
|
+
var block = (content) => `${BEGIN}
|
|
4568
|
+
${content}${END}
|
|
4569
|
+
`;
|
|
4570
|
+
function withoutBlock(text7) {
|
|
4571
|
+
const start = text7.indexOf(BEGIN);
|
|
4572
|
+
if (start === -1) return text7;
|
|
4573
|
+
const end = text7.indexOf(END, start);
|
|
4574
|
+
if (end === -1) return text7;
|
|
4575
|
+
const after = text7.slice(end + END.length).replace(/^\n/u, "");
|
|
4576
|
+
return `${text7.slice(0, start).replace(/\n+$/u, "\n")}${after}`;
|
|
4577
|
+
}
|
|
4578
|
+
|
|
4579
|
+
// src/project/outputs.ts
|
|
4580
|
+
var MARK = "AUTO-GENERATED by pmcp project";
|
|
4581
|
+
function jsonOutput(file, key, value) {
|
|
4582
|
+
return { kind: "json", path: `${file}#${key.join(".")}`, file, key, value };
|
|
4583
|
+
}
|
|
4584
|
+
function blockOutput(file, content) {
|
|
4585
|
+
return { kind: "block", path: `${file}#pmcp`, file, content };
|
|
4586
|
+
}
|
|
4587
|
+
function splitKeyed(path) {
|
|
4588
|
+
const at = path.indexOf("#");
|
|
4589
|
+
if (at === -1) return null;
|
|
4590
|
+
return { file: path.slice(0, at), key: path.slice(at + 1).split(".") };
|
|
4591
|
+
}
|
|
4592
|
+
|
|
4593
|
+
// src/project/permissions/types.ts
|
|
4594
|
+
var BUCKETS = ["allow", "ask", "deny"];
|
|
4595
|
+
|
|
4596
|
+
// src/project/permissions/reconcile.ts
|
|
4597
|
+
var SETTINGS = ".claude/settings.json";
|
|
4598
|
+
var count2 = (list2, value) => list2.filter((item) => item === value).length;
|
|
4599
|
+
function currentRules(settings, bucket) {
|
|
4600
|
+
const container = settings["permissions"];
|
|
4601
|
+
if (container === void 0) return { rules: [], present: false };
|
|
4602
|
+
if (container === null || typeof container !== "object")
|
|
4603
|
+
return { rules: [], present: false, problem: "is not a table" };
|
|
4604
|
+
if (Array.isArray(container))
|
|
4605
|
+
return { rules: [], present: false, problem: "is not a table" };
|
|
4606
|
+
const raw = container[bucket];
|
|
4607
|
+
if (raw === void 0) return { rules: [], present: false };
|
|
4608
|
+
if (!Array.isArray(raw) || !raw.every((v) => typeof v === "string"))
|
|
4609
|
+
return { rules: [], present: true, problem: "is not a list of strings" };
|
|
4610
|
+
return { rules: raw, present: true };
|
|
4611
|
+
}
|
|
4612
|
+
function overlaps(settings, declared, owned2) {
|
|
4613
|
+
const found = [];
|
|
4614
|
+
for (const [bucket, rules2] of Object.entries(declared)) {
|
|
4615
|
+
for (const other of BUCKETS) {
|
|
4616
|
+
if (other === bucket) continue;
|
|
4617
|
+
const mine = owned2.get(other) ?? [];
|
|
4618
|
+
const native = currentRules(settings, other).rules.filter(
|
|
4619
|
+
(rule) => !mine.includes(rule)
|
|
4620
|
+
);
|
|
4621
|
+
for (const rule of rules2.filter((item) => native.includes(item)))
|
|
4622
|
+
found.push(
|
|
4623
|
+
`overlap: ${SETTINGS}#permissions.${other} has unmanaged ${rule}, declared in ${bucket}`
|
|
4624
|
+
);
|
|
4625
|
+
}
|
|
4626
|
+
}
|
|
4627
|
+
return found;
|
|
4628
|
+
}
|
|
4629
|
+
function reconcile(settings, bucket, owned2, declared) {
|
|
4630
|
+
const where = `${SETTINGS}#permissions.${bucket}`;
|
|
4631
|
+
const current = currentRules(settings, bucket);
|
|
4632
|
+
const conflicts = [];
|
|
4633
|
+
if (current.problem !== void 0) {
|
|
4634
|
+
const key = current.present ? `permissions.${bucket}` : "permissions";
|
|
4635
|
+
conflicts.push(`conflict: ${SETTINGS}#${key} ${current.problem}`);
|
|
4636
|
+
}
|
|
4637
|
+
const rules2 = current.rules;
|
|
4638
|
+
for (const rule of owned2) {
|
|
4639
|
+
const found = count2(rules2, rule);
|
|
4640
|
+
if (found === 0 && current.present && current.problem === void 0)
|
|
4641
|
+
conflicts.push(
|
|
4642
|
+
`conflict: ${where} owned entry edited or removed ${rule}`
|
|
4643
|
+
);
|
|
4644
|
+
if (found > 1)
|
|
4645
|
+
conflicts.push(`conflict: ${where} owned entry is duplicated ${rule}`);
|
|
4646
|
+
}
|
|
4647
|
+
const dropped = owned2.filter(
|
|
4648
|
+
(rule) => !declared.includes(rule) && rules2.includes(rule)
|
|
4649
|
+
);
|
|
4650
|
+
const missing = declared.filter((rule) => !rules2.includes(rule));
|
|
4651
|
+
const value = [
|
|
4652
|
+
...rules2.filter((rule) => !dropped.includes(rule)),
|
|
4653
|
+
...missing
|
|
4654
|
+
];
|
|
4655
|
+
const managed = declared.filter(
|
|
4656
|
+
(rule) => owned2.includes(rule) || !rules2.includes(rule)
|
|
4657
|
+
);
|
|
4658
|
+
return {
|
|
4659
|
+
bucket,
|
|
4660
|
+
value,
|
|
4661
|
+
managed,
|
|
4662
|
+
borrowed: declared.filter((rule) => !managed.includes(rule)),
|
|
4663
|
+
drift: [
|
|
4664
|
+
...missing.map((rule) => `drift: ${where} missing ${rule}`),
|
|
4665
|
+
...dropped.map((rule) => `drift: ${where} no longer declared ${rule}`)
|
|
4666
|
+
],
|
|
4667
|
+
conflicts
|
|
4668
|
+
};
|
|
4669
|
+
}
|
|
4670
|
+
|
|
4671
|
+
// src/project/permissions/grammar.ts
|
|
4672
|
+
var TOOLS2 = /* @__PURE__ */ new Set([
|
|
4673
|
+
"Agent",
|
|
4674
|
+
"Bash",
|
|
4675
|
+
"Edit",
|
|
4676
|
+
"Glob",
|
|
4677
|
+
"Grep",
|
|
4678
|
+
"LS",
|
|
4679
|
+
"MultiEdit",
|
|
4680
|
+
"NotebookEdit",
|
|
4681
|
+
"NotebookRead",
|
|
4682
|
+
"Read",
|
|
4683
|
+
"Skill",
|
|
4684
|
+
"Task",
|
|
4685
|
+
"TodoWrite",
|
|
4686
|
+
"WebFetch",
|
|
4687
|
+
"WebSearch",
|
|
4688
|
+
"Write"
|
|
4689
|
+
]);
|
|
4690
|
+
var BARE_ONLY = /* @__PURE__ */ new Set(["TodoWrite", "WebSearch"]);
|
|
4691
|
+
var RULE = /^(?<tool>[A-Za-z]+)(?:\((?<specifier>.+)\))?$/su;
|
|
4692
|
+
var MCP = /^mcp__[A-Za-z0-9][A-Za-z0-9_-]*(?:__\*)?$/u;
|
|
4693
|
+
function specifierFits(tool, specifier) {
|
|
4694
|
+
if (BARE_ONLY.has(tool)) return false;
|
|
4695
|
+
if (tool === "WebFetch") return /^domain:\S+$/u.test(specifier);
|
|
4696
|
+
if (tool !== "Bash") return true;
|
|
4697
|
+
const prefix = specifier.indexOf(":*");
|
|
4698
|
+
return prefix === -1 || prefix === specifier.length - 2;
|
|
4699
|
+
}
|
|
4700
|
+
function isClaudeRule(expression) {
|
|
4701
|
+
if (MCP.test(expression)) return true;
|
|
4702
|
+
const match = RULE.exec(expression);
|
|
4703
|
+
const tool = match?.groups?.["tool"];
|
|
4704
|
+
if (tool === void 0 || !TOOLS2.has(tool)) return false;
|
|
4705
|
+
const specifier = match?.groups?.["specifier"];
|
|
4706
|
+
if (specifier === void 0) return true;
|
|
4707
|
+
return specifierFits(tool, specifier);
|
|
4708
|
+
}
|
|
4709
|
+
|
|
4710
|
+
// src/project/permissions/reader.ts
|
|
4711
|
+
var CONTROL = /[\u0000-\u001f\u007f]/u;
|
|
4712
|
+
function table(path, where, value) {
|
|
4713
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
4714
|
+
throw new ConfigError(path, `${where} must be a table`);
|
|
4715
|
+
return value;
|
|
4716
|
+
}
|
|
4717
|
+
function rules(path, bucket, value) {
|
|
4718
|
+
const where = `[permissions.claude] ${bucket}`;
|
|
4719
|
+
if (!Array.isArray(value) || !value.every((v) => typeof v === "string"))
|
|
4720
|
+
throw new ConfigError(path, `${where} must be a list of strings`);
|
|
4721
|
+
for (const rule of value) {
|
|
4722
|
+
if (rule === "") throw new ConfigError(path, `${where}: empty rule`);
|
|
4723
|
+
if (CONTROL.test(rule))
|
|
4724
|
+
throw new ConfigError(path, `${where}: rule has a control character`);
|
|
4725
|
+
if (!isClaudeRule(rule))
|
|
4726
|
+
throw new ConfigError(
|
|
4727
|
+
path,
|
|
4728
|
+
`${where}: ${rule} is not a Claude permission rule`
|
|
4729
|
+
);
|
|
4730
|
+
}
|
|
4731
|
+
return [...new Set(value)];
|
|
4732
|
+
}
|
|
4733
|
+
function readPermissions(path, raw) {
|
|
4734
|
+
if (raw === void 0) return void 0;
|
|
4735
|
+
const root = table(path, "[permissions]", raw);
|
|
4736
|
+
for (const key of Object.keys(root))
|
|
4737
|
+
if (key !== "claude")
|
|
4738
|
+
throw new ConfigError(path, `[permissions] unknown key ${key}`);
|
|
4739
|
+
if (root["claude"] === void 0) return void 0;
|
|
4740
|
+
const claude2 = table(path, "[permissions.claude]", root["claude"]);
|
|
4741
|
+
const declared = {};
|
|
4742
|
+
const seen = /* @__PURE__ */ new Map();
|
|
4743
|
+
for (const [key, value] of Object.entries(claude2)) {
|
|
4744
|
+
if (!BUCKETS.includes(key))
|
|
4745
|
+
throw new ConfigError(path, `[permissions.claude] unknown key ${key}`);
|
|
4746
|
+
const bucket = key;
|
|
4747
|
+
declared[bucket] = rules(path, bucket, value);
|
|
4748
|
+
for (const rule of declared[bucket]) {
|
|
4749
|
+
const other = seen.get(rule);
|
|
4750
|
+
if (other !== void 0)
|
|
4751
|
+
throw new ConfigError(
|
|
4752
|
+
path,
|
|
4753
|
+
`[permissions.claude] ${rule} is declared in both ${other} and ${bucket}`
|
|
4754
|
+
);
|
|
4755
|
+
seen.set(rule, bucket);
|
|
4756
|
+
}
|
|
4757
|
+
}
|
|
4758
|
+
return { claude: declared };
|
|
4759
|
+
}
|
|
4760
|
+
|
|
4761
|
+
// src/project/permissions.ts
|
|
4762
|
+
var PREFIX = `${SETTINGS}#permissions.`;
|
|
4763
|
+
var isPermissionPath = (path) => path.startsWith(PREFIX);
|
|
4764
|
+
function owned(root) {
|
|
4765
|
+
return new Map(
|
|
4766
|
+
readLock(root).filter((entry) => isPermissionPath(entry.path)).map((entry) => [
|
|
4767
|
+
entry.path.slice(PREFIX.length),
|
|
4768
|
+
(entry.managed ?? []).filter((v) => typeof v === "string")
|
|
4769
|
+
])
|
|
4770
|
+
);
|
|
4771
|
+
}
|
|
4772
|
+
function permissionPlans(spec) {
|
|
4773
|
+
const previous = owned(spec.root);
|
|
4774
|
+
const declared = spec.tools.has("claude") ? spec.permissions?.claude ?? {} : {};
|
|
4775
|
+
if (previous.size === 0 && Object.keys(declared).length === 0) return [];
|
|
4776
|
+
const settings = readJson2(join4(spec.root, SETTINGS));
|
|
4777
|
+
return BUCKETS.flatMap((bucket) => {
|
|
4778
|
+
const mine = previous.get(bucket) ?? [];
|
|
4779
|
+
const wanted = declared[bucket] ?? [];
|
|
4780
|
+
if (mine.length === 0 && wanted.length === 0) return [];
|
|
4781
|
+
return [reconcile(settings, bucket, mine, wanted)];
|
|
4782
|
+
});
|
|
4783
|
+
}
|
|
4784
|
+
function permissionOutputs(spec) {
|
|
4785
|
+
return permissionPlans(spec).map(
|
|
4786
|
+
(plan) => ({
|
|
4787
|
+
...jsonOutput(SETTINGS, ["permissions", plan.bucket], plan.value),
|
|
4788
|
+
managed: plan.managed
|
|
4789
|
+
})
|
|
4790
|
+
);
|
|
4791
|
+
}
|
|
4792
|
+
var permissionConflicts = (spec) => permissionPlans(spec).flatMap((plan) => plan.conflicts);
|
|
4793
|
+
var permissionProblems = (spec) => permissionPlans(spec).flatMap(
|
|
4794
|
+
(plan) => plan.conflicts.length > 0 ? plan.conflicts : plan.drift
|
|
4795
|
+
);
|
|
4796
|
+
function permissionWarnings(spec) {
|
|
4797
|
+
if (!spec.tools.has("claude") || !spec.permissions) return [];
|
|
4798
|
+
const settings = readJson2(join4(spec.root, SETTINGS));
|
|
4799
|
+
return overlaps(settings, spec.permissions.claude, owned(spec.root));
|
|
4800
|
+
}
|
|
4801
|
+
function doctorStatus(conflicts, warnings) {
|
|
4802
|
+
if (conflicts > 0) return "fail";
|
|
4803
|
+
return warnings > 0 ? "warn" : "ok";
|
|
4804
|
+
}
|
|
4805
|
+
function permissionDoctor(spec) {
|
|
4806
|
+
const plans = permissionPlans(spec);
|
|
4807
|
+
if (plans.length === 0)
|
|
4808
|
+
return [
|
|
4809
|
+
{
|
|
4810
|
+
tool: "claude",
|
|
4811
|
+
check: "permissions owned",
|
|
4812
|
+
status: "ok",
|
|
4813
|
+
detail: "none declared"
|
|
4814
|
+
}
|
|
4815
|
+
];
|
|
4816
|
+
const conflicts = plans.flatMap((plan) => plan.conflicts);
|
|
4817
|
+
const warnings = permissionWarnings(spec);
|
|
4818
|
+
const counts = plans.map(
|
|
4819
|
+
(plan) => `${plan.bucket}: ${plan.managed.length} owned, ${plan.borrowed.length} borrowed`
|
|
4820
|
+
);
|
|
4821
|
+
return [
|
|
4822
|
+
{
|
|
4823
|
+
tool: "claude",
|
|
4824
|
+
check: "permissions owned",
|
|
4825
|
+
status: doctorStatus(conflicts.length, warnings.length),
|
|
4826
|
+
detail: (conflicts.length > 0 ? conflicts : [...counts, ...warnings]).join("; ")
|
|
4827
|
+
}
|
|
4828
|
+
];
|
|
4829
|
+
}
|
|
4830
|
+
|
|
4831
|
+
// src/project/spec.ts
|
|
4832
|
+
var NAME = /^[a-z0-9][a-z0-9-]*$/u;
|
|
4833
|
+
function table2(path, where, value) {
|
|
4834
|
+
if (value === void 0) return {};
|
|
4835
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
4836
|
+
throw new ConfigError(path, `${where} must be a table`);
|
|
4837
|
+
}
|
|
4838
|
+
return value;
|
|
4839
|
+
}
|
|
4840
|
+
function strings(path, where, value) {
|
|
4841
|
+
if (!Array.isArray(value) || !value.every((v) => typeof v === "string")) {
|
|
4842
|
+
throw new ConfigError(path, `${where} must be a list of strings`);
|
|
4843
|
+
}
|
|
4844
|
+
return value;
|
|
4845
|
+
}
|
|
4846
|
+
function named(path, where, name) {
|
|
4847
|
+
if (!NAME.test(name)) {
|
|
4848
|
+
throw new ConfigError(
|
|
4849
|
+
path,
|
|
4850
|
+
`${where} ${name}: use lowercase, digits and -`
|
|
4851
|
+
);
|
|
4852
|
+
}
|
|
4853
|
+
return name;
|
|
4854
|
+
}
|
|
4855
|
+
function readSpec(config) {
|
|
4856
|
+
const path = config.path;
|
|
4857
|
+
const raw = config.raw;
|
|
4858
|
+
const targets = table2(path, "[targets]", raw["targets"]);
|
|
4859
|
+
const tools = /* @__PURE__ */ new Set();
|
|
4860
|
+
if (targets["tools"] !== void 0) {
|
|
4861
|
+
for (const tool of strings(path, "[targets] tools", targets["tools"])) {
|
|
4862
|
+
if (!TOOLS.includes(tool)) {
|
|
4863
|
+
throw new ConfigError(path, `[targets] unknown tool ${tool}`);
|
|
4864
|
+
}
|
|
4865
|
+
tools.add(tool);
|
|
4866
|
+
}
|
|
4867
|
+
}
|
|
4868
|
+
const skills = {};
|
|
4869
|
+
for (const [name, value] of Object.entries(
|
|
4870
|
+
table2(path, "[skills]", raw["skills"])
|
|
4871
|
+
)) {
|
|
4872
|
+
const entry = table2(path, `[skills.${name}]`, value);
|
|
4873
|
+
const invocation = entry["invocation"] ?? "any";
|
|
4874
|
+
if (invocation !== "any" && invocation !== "user") {
|
|
4875
|
+
throw new ConfigError(
|
|
4876
|
+
path,
|
|
4877
|
+
`[skills.${name}] invocation must be "any" or "user"`
|
|
4878
|
+
);
|
|
4879
|
+
}
|
|
4880
|
+
skills[named(path, "[skills]", name)] = { invocation };
|
|
4881
|
+
}
|
|
4882
|
+
const agents = {};
|
|
4883
|
+
for (const [name, value] of Object.entries(
|
|
4884
|
+
table2(path, "[agents]", raw["agents"])
|
|
4885
|
+
)) {
|
|
4886
|
+
const perTool = {};
|
|
4887
|
+
for (const [tool, fields2] of Object.entries(
|
|
4888
|
+
table2(path, `[agents.${name}]`, value)
|
|
4889
|
+
)) {
|
|
4890
|
+
if (!TOOLS.includes(tool)) {
|
|
4891
|
+
throw new ConfigError(path, `[agents.${name}] unknown tool ${tool}`);
|
|
4892
|
+
}
|
|
4893
|
+
perTool[tool] = table2(path, `[agents.${name}.${tool}]`, fields2);
|
|
4894
|
+
}
|
|
4895
|
+
agents[named(path, "[agents]", name)] = perTool;
|
|
4896
|
+
}
|
|
4897
|
+
const rules2 = table2(path, "[rules]", raw["rules"]);
|
|
4898
|
+
let packageRules = null;
|
|
4899
|
+
if (rules2["packages"] !== void 0) {
|
|
4900
|
+
const pkg = table2(path, "[rules.packages]", rules2["packages"]);
|
|
4901
|
+
const file = pkg["file"];
|
|
4902
|
+
if (typeof file !== "string" || file === "") {
|
|
4903
|
+
throw new ConfigError(path, "[rules.packages] file is required");
|
|
4904
|
+
}
|
|
4905
|
+
const dir = pkg["dir"] ?? "pkg";
|
|
4906
|
+
if (typeof dir !== "string" || !NAME.test(dir) || dir === "pmcp") {
|
|
4907
|
+
throw new ConfigError(
|
|
4908
|
+
path,
|
|
4909
|
+
'[rules.packages] dir must be a plain name other than "pmcp"'
|
|
4910
|
+
);
|
|
4911
|
+
}
|
|
4912
|
+
const strip = pkg["strip"] === void 0 ? [] : strings(path, "[rules.packages] strip", pkg["strip"]);
|
|
4913
|
+
packageRules = { file, dir, strip };
|
|
4914
|
+
}
|
|
4915
|
+
const mcp = readMcp(path, raw["mcp"]);
|
|
4916
|
+
const hooks = readHooks(path, config.dir, raw, tools);
|
|
4917
|
+
const sessionHooks = hooks.bundle?.policy?.guards.some((g) => g.id === "sessions.registry") ?? false;
|
|
4918
|
+
const marketplaces = readMarketplaces(
|
|
4919
|
+
raw["marketplaces"],
|
|
4920
|
+
config.dir,
|
|
4921
|
+
(m) => {
|
|
4922
|
+
throw new ConfigError(path, m);
|
|
4923
|
+
}
|
|
4924
|
+
);
|
|
4925
|
+
const uiClaude = readUiClaude(path, raw["ui"], sessionHooks);
|
|
4926
|
+
const permissions = readPermissions(path, raw["permissions"]);
|
|
4927
|
+
const host = readHost(path, raw["host"]);
|
|
4928
|
+
return {
|
|
4929
|
+
root: config.dir,
|
|
4930
|
+
tools,
|
|
4931
|
+
skills,
|
|
4932
|
+
agents,
|
|
4933
|
+
packageRules,
|
|
4934
|
+
mcp,
|
|
4935
|
+
...marketplaces.length > 0 ? { marketplaces } : {},
|
|
4936
|
+
...uiClaude ? { uiClaude } : {},
|
|
4937
|
+
...permissions ? { permissions } : {},
|
|
4938
|
+
...host ? { host } : {},
|
|
4939
|
+
...hooks.legacy.length > 0 ? { hooks: hooks.legacy } : {},
|
|
4940
|
+
...hooks.bundle ? { hookBundle: hooks.bundle } : {},
|
|
4941
|
+
...hooks.deprecations.length > 0 ? { deprecations: hooks.deprecations } : {},
|
|
4942
|
+
featureSources: hooks.sources
|
|
4943
|
+
};
|
|
4944
|
+
}
|
|
4945
|
+
|
|
4946
|
+
export {
|
|
4947
|
+
pluginBinaryDoctor,
|
|
4948
|
+
readMcp,
|
|
4949
|
+
commandsOf,
|
|
4950
|
+
RUNTIME_IDS,
|
|
4951
|
+
NEUTRAL_EVENTS,
|
|
4952
|
+
customPolicy,
|
|
4953
|
+
GUARDS,
|
|
4954
|
+
FEATURES,
|
|
4955
|
+
POLICIES,
|
|
4956
|
+
climbCommand,
|
|
4957
|
+
RUNTIMES,
|
|
4958
|
+
NATIVE_HOOK_FILES,
|
|
4959
|
+
uiClaudeOutputs,
|
|
4960
|
+
uiClaudeDoctor,
|
|
4961
|
+
linkOf,
|
|
4962
|
+
exists,
|
|
4963
|
+
fingerprint,
|
|
4964
|
+
changedSincePlan,
|
|
4965
|
+
readJson2 as readJson,
|
|
4966
|
+
getKey,
|
|
4967
|
+
setKey,
|
|
4968
|
+
deleteKey,
|
|
4969
|
+
same,
|
|
4970
|
+
block,
|
|
4971
|
+
withoutBlock,
|
|
4972
|
+
MARK,
|
|
4973
|
+
jsonOutput,
|
|
4974
|
+
blockOutput,
|
|
4975
|
+
splitKeyed,
|
|
4976
|
+
SETTINGS,
|
|
4977
|
+
isPermissionPath,
|
|
4978
|
+
permissionOutputs,
|
|
4979
|
+
permissionConflicts,
|
|
4980
|
+
permissionProblems,
|
|
4981
|
+
permissionWarnings,
|
|
4982
|
+
permissionDoctor,
|
|
4983
|
+
readSpec
|
|
4984
|
+
};
|
|
4985
|
+
//# sourceMappingURL=chunk-65ZHM5RH.js.map
|