@primitive.ai/prim 0.1.0-alpha.119 → 0.1.0-alpha.120
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/dist/{chunk-XWAKFL3T.js → chunk-EYBE724K.js} +3 -5
- package/dist/{chunk-4L7TAM5S.js → chunk-L52OD62C.js} +268 -3
- package/dist/{chunk-CSE44DPA.js → chunk-R2NUH4B7.js} +1 -1
- package/dist/{chunk-2JA524SC.js → chunk-YTF6OCF3.js} +3 -7
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +5 -27
- package/dist/hook-runtime/hooks/pre-tool-use.js +1 -36
- package/dist/hooks/post-tool-use.js +3 -4
- package/dist/hooks/pre-tool-use-worker.js +19 -45
- package/dist/hooks/pre-tool-use.js +1 -26
- package/dist/hooks/prim-hook.js +3 -4
- package/dist/index.js +2 -3
- package/package.json +1 -1
- package/dist/chunk-W27EFR7W.js +0 -271
|
@@ -2,8 +2,9 @@ import {
|
|
|
2
2
|
getOrCreateWorkspaceId
|
|
3
3
|
} from "./chunk-EZXEWFDG.js";
|
|
4
4
|
import {
|
|
5
|
-
analyzeShellTargets
|
|
6
|
-
|
|
5
|
+
analyzeShellTargets,
|
|
6
|
+
extractFileTargets
|
|
7
|
+
} from "./chunk-L52OD62C.js";
|
|
7
8
|
import {
|
|
8
9
|
scrubEnvironmentPaths
|
|
9
10
|
} from "./chunk-NQFRQ56V.js";
|
|
@@ -17,9 +18,6 @@ import {
|
|
|
17
18
|
relatedWorktreeTarget,
|
|
18
19
|
repoSyncId
|
|
19
20
|
} from "./chunk-P2O7NLVN.js";
|
|
20
|
-
import {
|
|
21
|
-
extractFileTargets
|
|
22
|
-
} from "./chunk-W27EFR7W.js";
|
|
23
21
|
|
|
24
22
|
// src/hooks/file-refs.ts
|
|
25
23
|
var MAX_PREFLIGHT_FILE_TARGETS = 25;
|
|
@@ -10,9 +10,263 @@ import {
|
|
|
10
10
|
withFileLock,
|
|
11
11
|
withFileLockSync
|
|
12
12
|
} from "./chunk-P2O7NLVN.js";
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from "
|
|
13
|
+
|
|
14
|
+
// src/hooks/pre-tool-use-scoring.ts
|
|
15
|
+
import { isAbsolute, relative, sep } from "path";
|
|
16
|
+
function anyUnverified(results) {
|
|
17
|
+
return results.some((r) => r.verdict === "unavailable" || r.truncated);
|
|
18
|
+
}
|
|
19
|
+
function unverifiedNote(results) {
|
|
20
|
+
const causes = [];
|
|
21
|
+
const unavailable = results.find((r) => r.verdict === "unavailable");
|
|
22
|
+
if (unavailable) {
|
|
23
|
+
causes.push(
|
|
24
|
+
unavailable.unavailable ? `decision check skipped \u2014 ${unavailable.unavailable}` : "decision check skipped \u2014 not verified"
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if (results.some((r) => r.truncated)) {
|
|
28
|
+
causes.push("decision check partial \u2014 conflict set truncated (per-file cap hit)");
|
|
29
|
+
}
|
|
30
|
+
return causes.map((c) => `[primitive] ${c}`).join("\n");
|
|
31
|
+
}
|
|
32
|
+
function blockingReason(results) {
|
|
33
|
+
const parts = results.filter((result) => result.verdict === "ask" || result.verdict === "deny").flatMap((result) => [result.reason, result.additionalContext]).filter((value) => value.length > 0);
|
|
34
|
+
return [...new Set(parts)].join("\n\n") || "[primitive] conflict detected (no detail available)";
|
|
35
|
+
}
|
|
36
|
+
function approvalReason(results) {
|
|
37
|
+
const summaries = results.filter((result) => result.verdict === "ask" || result.verdict === "deny").map((result) => result.reason).filter(Boolean);
|
|
38
|
+
return [
|
|
39
|
+
...new Set(summaries),
|
|
40
|
+
"Approve runs this edit once; it does not change these decisions."
|
|
41
|
+
].join("\n");
|
|
42
|
+
}
|
|
43
|
+
function agentContext(results) {
|
|
44
|
+
return [...new Set(results.map((result) => result.additionalContext).filter(Boolean))].join(
|
|
45
|
+
"\n\n"
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
function advisoryNote(aggregate, results) {
|
|
49
|
+
const details = results.flatMap((result) => [
|
|
50
|
+
...aggregate === "warn" ? [result.reason] : [],
|
|
51
|
+
result.additionalContext
|
|
52
|
+
]);
|
|
53
|
+
if (anyUnverified(results)) details.push(unverifiedNote(results));
|
|
54
|
+
return [...new Set(details.filter((value) => value.length > 0))].join("\n\n");
|
|
55
|
+
}
|
|
56
|
+
function buildHookOutput(aggregate, results) {
|
|
57
|
+
if (aggregate === "ask") {
|
|
58
|
+
const context = agentContext(results);
|
|
59
|
+
return {
|
|
60
|
+
hookSpecificOutput: {
|
|
61
|
+
hookEventName: "PreToolUse",
|
|
62
|
+
permissionDecision: "ask",
|
|
63
|
+
permissionDecisionReason: approvalReason(results),
|
|
64
|
+
...context ? { additionalContext: context } : {}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (aggregate === "deny") {
|
|
69
|
+
return {
|
|
70
|
+
hookSpecificOutput: {
|
|
71
|
+
hookEventName: "PreToolUse",
|
|
72
|
+
permissionDecision: aggregate,
|
|
73
|
+
permissionDecisionReason: blockingReason(results)
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const notes = advisoryNote(aggregate, results);
|
|
78
|
+
if (notes.length > 0) {
|
|
79
|
+
return {
|
|
80
|
+
systemMessage: notes,
|
|
81
|
+
hookSpecificOutput: {
|
|
82
|
+
hookEventName: "PreToolUse",
|
|
83
|
+
permissionDecision: "allow",
|
|
84
|
+
additionalContext: notes
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
hookSpecificOutput: {
|
|
90
|
+
hookEventName: "PreToolUse",
|
|
91
|
+
permissionDecision: "allow"
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function buildCodexOutput(aggregate, results) {
|
|
96
|
+
if (aggregate === "deny" || aggregate === "ask") {
|
|
97
|
+
return {
|
|
98
|
+
hookSpecificOutput: {
|
|
99
|
+
hookEventName: "PreToolUse",
|
|
100
|
+
permissionDecision: "deny",
|
|
101
|
+
permissionDecisionReason: blockingReason(results)
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const notes = advisoryNote(aggregate, results);
|
|
106
|
+
return notes ? {
|
|
107
|
+
systemMessage: notes,
|
|
108
|
+
hookSpecificOutput: { hookEventName: "PreToolUse", additionalContext: notes }
|
|
109
|
+
} : {};
|
|
110
|
+
}
|
|
111
|
+
function buildCursorOutput(aggregate, results) {
|
|
112
|
+
if (aggregate === "deny" || aggregate === "ask") {
|
|
113
|
+
const reason = blockingReason(results);
|
|
114
|
+
return { permission: "deny", user_message: reason, agent_message: reason };
|
|
115
|
+
}
|
|
116
|
+
const notes = advisoryNote(aggregate, results);
|
|
117
|
+
return notes ? { permission: "allow", agent_message: notes } : { permission: "allow" };
|
|
118
|
+
}
|
|
119
|
+
function failOpenOutput() {
|
|
120
|
+
return {
|
|
121
|
+
hookSpecificOutput: {
|
|
122
|
+
hookEventName: "PreToolUse",
|
|
123
|
+
permissionDecision: "allow"
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function failOpenCodex() {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
130
|
+
function failOpenCursor() {
|
|
131
|
+
return { permission: "allow" };
|
|
132
|
+
}
|
|
133
|
+
function buildHermesOutput(aggregate, results) {
|
|
134
|
+
if (aggregate !== "deny" && aggregate !== "ask") {
|
|
135
|
+
return {};
|
|
136
|
+
}
|
|
137
|
+
const reason = results.filter((r) => r.verdict === "deny" || r.verdict === "ask").map((r) => r.reason).filter((s) => s.length > 0).join("\n\n");
|
|
138
|
+
const directive = results.map((r) => r.additionalContext).filter((s) => s.length > 0 && s !== reason).join("\n");
|
|
139
|
+
const message = [reason, directive].filter((s) => s.length > 0).join("\n\n") || "[primitive] conflict detected (no detail available)";
|
|
140
|
+
return { action: "block", message };
|
|
141
|
+
}
|
|
142
|
+
function failOpenHermes() {
|
|
143
|
+
return {};
|
|
144
|
+
}
|
|
145
|
+
var SUPPORTED_TOOLS = /* @__PURE__ */ new Set(["Edit", "Write", "MultiEdit", "NotebookEdit"]);
|
|
146
|
+
var APPLY_PATCH_FILE_RE = /^\*\*\* (?:Update|Add|Delete) File: (?<path>.+)$/;
|
|
147
|
+
var MOVE_FILE_RE = /^\*\*\* Move File:\s*(?<src>.+?)\s*->\s*(?<dst>.+?)\s*$/;
|
|
148
|
+
var MOVE_TO_RE = /^\*\*\* Move to: (?<dst>.+)$/;
|
|
149
|
+
var LINE_SPLIT_RE = /\r?\n/;
|
|
150
|
+
function parseApplyPatchTargets(command) {
|
|
151
|
+
const paths = /* @__PURE__ */ new Set();
|
|
152
|
+
let activeUpdate = false;
|
|
153
|
+
let complete = true;
|
|
154
|
+
for (const line of command.split(LINE_SPLIT_RE)) {
|
|
155
|
+
const path = APPLY_PATCH_FILE_RE.exec(line)?.groups?.path;
|
|
156
|
+
if (path) {
|
|
157
|
+
paths.add(path);
|
|
158
|
+
activeUpdate = line.startsWith("*** Update File:");
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const moveTo = MOVE_TO_RE.exec(line)?.groups?.dst;
|
|
162
|
+
if (moveTo) {
|
|
163
|
+
if (activeUpdate) paths.add(moveTo);
|
|
164
|
+
else complete = false;
|
|
165
|
+
activeUpdate = false;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const move = MOVE_FILE_RE.exec(line)?.groups;
|
|
169
|
+
if (move?.src) {
|
|
170
|
+
paths.add(move.src);
|
|
171
|
+
if (move.dst) {
|
|
172
|
+
paths.add(move.dst);
|
|
173
|
+
}
|
|
174
|
+
activeUpdate = false;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (line === "*** Begin Patch" || line === "*** End Patch") activeUpdate = false;
|
|
178
|
+
else if (line.startsWith("*** ")) complete = false;
|
|
179
|
+
}
|
|
180
|
+
return { paths: [...paths], complete: complete && paths.size > 0 };
|
|
181
|
+
}
|
|
182
|
+
function extractCodexFileTargets(toolName, toolInput) {
|
|
183
|
+
if (toolName !== "apply_patch") {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
const text = codexPatchText(toolInput);
|
|
187
|
+
return text !== null ? parseApplyPatchTargets(text) : { paths: [], complete: false };
|
|
188
|
+
}
|
|
189
|
+
function codexPatchText(toolInput) {
|
|
190
|
+
if (typeof toolInput === "string") return toolInput;
|
|
191
|
+
if (!toolInput || typeof toolInput !== "object") return null;
|
|
192
|
+
const input = toolInput;
|
|
193
|
+
for (const key of ["command", "input", "patch"]) {
|
|
194
|
+
const value = input[key];
|
|
195
|
+
if (typeof value === "string") return value;
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
var HERMES_EDITING_TOOLS = /* @__PURE__ */ new Set(["write_file", "patch"]);
|
|
200
|
+
function extractHermesFileTargets(toolName, toolInput) {
|
|
201
|
+
if (!HERMES_EDITING_TOOLS.has(toolName)) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
if (!toolInput || typeof toolInput !== "object") {
|
|
205
|
+
return { paths: [], complete: false };
|
|
206
|
+
}
|
|
207
|
+
const input = toolInput;
|
|
208
|
+
if (toolName === "patch" && input.mode === "patch") {
|
|
209
|
+
return typeof input.patch === "string" ? parseApplyPatchTargets(input.patch) : { paths: [], complete: false };
|
|
210
|
+
}
|
|
211
|
+
const paths = typeof input.path === "string" && input.path.length > 0 ? [input.path] : [];
|
|
212
|
+
const mode = input.mode;
|
|
213
|
+
return { paths, complete: paths.length > 0 && (mode === void 0 || mode === "replace") };
|
|
214
|
+
}
|
|
215
|
+
function extractCursorFileTargets(toolName, toolInput) {
|
|
216
|
+
if (toolName !== "Write" && toolName !== "Delete") return null;
|
|
217
|
+
if (!toolInput || typeof toolInput !== "object" || Array.isArray(toolInput)) {
|
|
218
|
+
return { paths: [], complete: false };
|
|
219
|
+
}
|
|
220
|
+
const input = toolInput;
|
|
221
|
+
const candidates = [input.file_path, input.path].filter(
|
|
222
|
+
(value) => typeof value === "string" && value.length > 0
|
|
223
|
+
);
|
|
224
|
+
const paths = [...new Set(candidates)];
|
|
225
|
+
return { paths, complete: paths.length === 1 };
|
|
226
|
+
}
|
|
227
|
+
function extractFileTargets(toolName, toolInput, agent = "claude_code") {
|
|
228
|
+
if (agent === "codex") {
|
|
229
|
+
return extractCodexFileTargets(toolName, toolInput);
|
|
230
|
+
}
|
|
231
|
+
if (agent === "cursor") {
|
|
232
|
+
return extractCursorFileTargets(toolName, toolInput);
|
|
233
|
+
}
|
|
234
|
+
if (agent === "hermes") {
|
|
235
|
+
return extractHermesFileTargets(toolName, toolInput);
|
|
236
|
+
}
|
|
237
|
+
if (!SUPPORTED_TOOLS.has(toolName)) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
if (!toolInput || typeof toolInput !== "object") {
|
|
241
|
+
return { paths: [], complete: false };
|
|
242
|
+
}
|
|
243
|
+
const input = toolInput;
|
|
244
|
+
if (toolName === "NotebookEdit") {
|
|
245
|
+
const paths2 = typeof input.notebook_path === "string" && input.notebook_path.length > 0 ? [input.notebook_path] : [];
|
|
246
|
+
return { paths: paths2, complete: paths2.length > 0 };
|
|
247
|
+
}
|
|
248
|
+
const paths = typeof input.file_path === "string" && input.file_path.length > 0 ? [input.file_path] : [];
|
|
249
|
+
return { paths, complete: paths.length > 0 };
|
|
250
|
+
}
|
|
251
|
+
function readHookMode(env) {
|
|
252
|
+
if (env.PRIM_BYPASS === "1" || env.PRIM_BYPASS === "true") {
|
|
253
|
+
return "off";
|
|
254
|
+
}
|
|
255
|
+
const mode = env.PRIM_HOOK_MODE;
|
|
256
|
+
if (mode === "off" || mode === "warn") {
|
|
257
|
+
return mode;
|
|
258
|
+
}
|
|
259
|
+
return "block";
|
|
260
|
+
}
|
|
261
|
+
function demoteForMode(verdict, mode) {
|
|
262
|
+
if (mode === "off") {
|
|
263
|
+
return "allow";
|
|
264
|
+
}
|
|
265
|
+
if (mode === "warn" && (verdict === "ask" || verdict === "deny")) {
|
|
266
|
+
return "warn";
|
|
267
|
+
}
|
|
268
|
+
return verdict;
|
|
269
|
+
}
|
|
16
270
|
|
|
17
271
|
// src/hooks/shell-targets.ts
|
|
18
272
|
import { realpathSync } from "fs";
|
|
@@ -1152,6 +1406,17 @@ function isolationDiagnostics(run) {
|
|
|
1152
1406
|
}
|
|
1153
1407
|
|
|
1154
1408
|
export {
|
|
1409
|
+
buildHookOutput,
|
|
1410
|
+
buildCodexOutput,
|
|
1411
|
+
buildCursorOutput,
|
|
1412
|
+
failOpenOutput,
|
|
1413
|
+
failOpenCodex,
|
|
1414
|
+
failOpenCursor,
|
|
1415
|
+
buildHermesOutput,
|
|
1416
|
+
failOpenHermes,
|
|
1417
|
+
extractFileTargets,
|
|
1418
|
+
readHookMode,
|
|
1419
|
+
demoteForMode,
|
|
1155
1420
|
analyzeShellTargets,
|
|
1156
1421
|
RUN_ID,
|
|
1157
1422
|
runPath,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
analyzeShellTargets
|
|
3
|
-
|
|
2
|
+
analyzeShellTargets,
|
|
3
|
+
extractFileTargets
|
|
4
|
+
} from "./chunk-L52OD62C.js";
|
|
4
5
|
import {
|
|
5
6
|
DEFAULT_RULES,
|
|
6
7
|
scrub
|
|
@@ -17,9 +18,6 @@ import {
|
|
|
17
18
|
canonicalGitRoot,
|
|
18
19
|
canonicalRepositoryPath
|
|
19
20
|
} from "./chunk-P2O7NLVN.js";
|
|
20
|
-
import {
|
|
21
|
-
extractFileTargets
|
|
22
|
-
} from "./chunk-W27EFR7W.js";
|
|
23
21
|
|
|
24
22
|
// src/hooks/approval-copy.ts
|
|
25
23
|
var MAX_EXCERPT_CHARS = 80;
|
|
@@ -455,8 +453,6 @@ export {
|
|
|
455
453
|
toolInputDigest,
|
|
456
454
|
hasCompleteCoverage,
|
|
457
455
|
hasConfirmedConflict,
|
|
458
|
-
PreflightPausedError,
|
|
459
|
-
pausedResult,
|
|
460
456
|
resultForPreflight2 as resultForPreflight,
|
|
461
457
|
requestPreflight,
|
|
462
458
|
waitForPreflight
|
|
@@ -34919,14 +34919,6 @@ function hermesAdvisory(result, aggregate) {
|
|
|
34919
34919
|
const message = [...new Set(parts.filter(Boolean))].join("\n");
|
|
34920
34920
|
return message.startsWith("[primitive]") ? message : message ? `[primitive] ${message}` : "";
|
|
34921
34921
|
}
|
|
34922
|
-
var protectedShell = false;
|
|
34923
|
-
async function pause(message, checkId) {
|
|
34924
|
-
const result = pausedResult(message, checkId);
|
|
34925
|
-
await emit(
|
|
34926
|
-
agent === "codex" ? buildCodexOutput("deny", [result]) : buildHookOutput("deny", [result]),
|
|
34927
|
-
true
|
|
34928
|
-
);
|
|
34929
|
-
}
|
|
34930
34922
|
async function main() {
|
|
34931
34923
|
const mode = readHookMode(process.env);
|
|
34932
34924
|
if (mode === "off") {
|
|
@@ -34975,7 +34967,7 @@ async function main() {
|
|
|
34975
34967
|
return;
|
|
34976
34968
|
}
|
|
34977
34969
|
const toolName = envelope.tool_name;
|
|
34978
|
-
protectedShell = (agent === "claude_code" || agent === "codex") && toolName === "Bash";
|
|
34970
|
+
const protectedShell = (agent === "claude_code" || agent === "codex") && toolName === "Bash";
|
|
34979
34971
|
const cwd = typeof envelope.cwd === "string" && envelope.cwd.length > 0 ? envelope.cwd : process.cwd();
|
|
34980
34972
|
const targets = resolvePreflightTargets({
|
|
34981
34973
|
toolName,
|
|
@@ -35004,10 +34996,6 @@ async function main() {
|
|
|
35004
34996
|
const sessionId = envelope.session_id;
|
|
35005
34997
|
const callId = invocationId(envelope);
|
|
35006
34998
|
if (!binding || typeof sessionId !== "string" || !sessionId || !callId) {
|
|
35007
|
-
if (protectedShell) {
|
|
35008
|
-
await pause("Repository or invocation identity is unavailable");
|
|
35009
|
-
return;
|
|
35010
|
-
}
|
|
35011
34999
|
await emit(failOpen());
|
|
35012
35000
|
return;
|
|
35013
35001
|
}
|
|
@@ -35049,12 +35037,8 @@ async function main() {
|
|
|
35049
35037
|
try {
|
|
35050
35038
|
const response = await waitForPreflight(request2, {
|
|
35051
35039
|
deadline,
|
|
35052
|
-
stopOnConflict:
|
|
35040
|
+
stopOnConflict: true
|
|
35053
35041
|
});
|
|
35054
|
-
if (protectedShell && (!hasCompleteCoverage(response) || !hasConfirmedConflict(response) && response.verdict !== "allow" || response.evaluationCoverage.candidateCount > 0 && proposalIsTruncated(proposalInput))) {
|
|
35055
|
-
await pause(response.message || "Shell evaluation is incomplete", response.checkId);
|
|
35056
|
-
return;
|
|
35057
|
-
}
|
|
35058
35042
|
if (!hasConfirmedConflict(response)) {
|
|
35059
35043
|
await emit(failOpen(), true);
|
|
35060
35044
|
return;
|
|
@@ -35065,13 +35049,8 @@ async function main() {
|
|
|
35065
35049
|
request2,
|
|
35066
35050
|
collectScopeAdmits2 && (toolName === "Bash" || toolName === "Shell") && proposalIsTruncated(proposalInput)
|
|
35067
35051
|
);
|
|
35068
|
-
} catch
|
|
35069
|
-
|
|
35070
|
-
await pause(
|
|
35071
|
-
"Shell evaluation is unavailable or exceeded the two-second deadline",
|
|
35072
|
-
error instanceof PreflightPausedError ? error.checkId : void 0
|
|
35073
|
-
);
|
|
35074
|
-
else await emit(failOpen());
|
|
35052
|
+
} catch {
|
|
35053
|
+
await emit(failOpen());
|
|
35075
35054
|
return;
|
|
35076
35055
|
}
|
|
35077
35056
|
const aggregate = demoteForMode(result.verdict === "unavailable" ? "deny" : result.verdict, mode);
|
|
@@ -35084,6 +35063,5 @@ async function main() {
|
|
|
35084
35063
|
await emit(output, true);
|
|
35085
35064
|
}
|
|
35086
35065
|
main().catch(async () => {
|
|
35087
|
-
|
|
35088
|
-
else await emit(failOpen());
|
|
35066
|
+
await emit(failOpen());
|
|
35089
35067
|
});
|
|
@@ -15,19 +15,6 @@ function parseAgent(argv) {
|
|
|
15
15
|
return "claude_code";
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
// src/hooks/pre-tool-use-scoring.ts
|
|
19
|
-
import { isAbsolute, relative, sep } from "path";
|
|
20
|
-
function readHookMode(env) {
|
|
21
|
-
if (env.PRIM_BYPASS === "1" || env.PRIM_BYPASS === "true") {
|
|
22
|
-
return "off";
|
|
23
|
-
}
|
|
24
|
-
const mode = env.PRIM_HOOK_MODE;
|
|
25
|
-
if (mode === "off" || mode === "warn") {
|
|
26
|
-
return mode;
|
|
27
|
-
}
|
|
28
|
-
return "block";
|
|
29
|
-
}
|
|
30
|
-
|
|
31
18
|
// src/hooks/pre-tool-use.ts
|
|
32
19
|
var BUDGET_MS = 2e3;
|
|
33
20
|
var OUTPUT_RESERVE_MS = 150;
|
|
@@ -41,28 +28,6 @@ var child;
|
|
|
41
28
|
var finished = false;
|
|
42
29
|
var stdout = "";
|
|
43
30
|
var stderr = "";
|
|
44
|
-
var input = "";
|
|
45
|
-
var pauseReason = "[primitive] Operation paused: shell evaluation did not finish within two seconds. Retry the tool call to validate the current invocation.";
|
|
46
|
-
function fallback() {
|
|
47
|
-
if (readHookMode(process.env) === "off" || agent !== "claude_code" && agent !== "codex")
|
|
48
|
-
return silent;
|
|
49
|
-
try {
|
|
50
|
-
const envelope = JSON.parse(input);
|
|
51
|
-
if (envelope?.hook_event_name !== "PreToolUse" || envelope?.tool_name !== "Bash") return silent;
|
|
52
|
-
} catch {
|
|
53
|
-
return silent;
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
hookSpecificOutput: {
|
|
57
|
-
hookEventName: "PreToolUse",
|
|
58
|
-
permissionDecision: "deny",
|
|
59
|
-
permissionDecisionReason: pauseReason
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
process.stdin.setEncoding("utf8").on("data", (chunk) => {
|
|
64
|
-
input += chunk;
|
|
65
|
-
});
|
|
66
31
|
function stopWorker() {
|
|
67
32
|
if (!child?.pid) return;
|
|
68
33
|
try {
|
|
@@ -71,7 +36,7 @@ function stopWorker() {
|
|
|
71
36
|
} catch {
|
|
72
37
|
}
|
|
73
38
|
}
|
|
74
|
-
function finish(output =
|
|
39
|
+
function finish(output = silent, diagnostics = "") {
|
|
75
40
|
if (finished) return;
|
|
76
41
|
finished = true;
|
|
77
42
|
clearTimeout(timer);
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
enrichHookPayloadWithFileRefs,
|
|
7
7
|
preserveHookFileMetadata
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-EYBE724K.js";
|
|
9
9
|
import {
|
|
10
10
|
appendCodexContext,
|
|
11
11
|
prepareCodexContext
|
|
@@ -26,13 +26,13 @@ import {
|
|
|
26
26
|
import "../chunk-SLDZJD6Q.js";
|
|
27
27
|
import {
|
|
28
28
|
observeDemoInvocation
|
|
29
|
-
} from "../chunk-
|
|
29
|
+
} from "../chunk-R2NUH4B7.js";
|
|
30
30
|
import {
|
|
31
31
|
normalizeEnvelope,
|
|
32
32
|
readHookStdin,
|
|
33
33
|
shouldSuppressImportedCursorHandler
|
|
34
34
|
} from "../chunk-TC4FTRYO.js";
|
|
35
|
-
import "../chunk-
|
|
35
|
+
import "../chunk-L52OD62C.js";
|
|
36
36
|
import "../chunk-MWNRXAEB.js";
|
|
37
37
|
import {
|
|
38
38
|
scrubFromCwd
|
|
@@ -55,7 +55,6 @@ import {
|
|
|
55
55
|
import {
|
|
56
56
|
parseAgent
|
|
57
57
|
} from "../chunk-KZB4ABIB.js";
|
|
58
|
-
import "../chunk-W27EFR7W.js";
|
|
59
58
|
import "../chunk-JSBRDJBE.js";
|
|
60
59
|
|
|
61
60
|
// src/hooks/post-tool-use.ts
|
|
@@ -1,28 +1,36 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
PREFLIGHT_PROTOCOL_VERSION,
|
|
4
|
-
PreflightPausedError,
|
|
5
4
|
boundedClientVersion,
|
|
6
5
|
editPreview,
|
|
7
|
-
hasCompleteCoverage,
|
|
8
6
|
hasConfirmedConflict,
|
|
9
|
-
pausedResult,
|
|
10
7
|
proposalFor,
|
|
11
8
|
proposalIsTruncated,
|
|
12
9
|
resolvePreflightTargets,
|
|
13
10
|
resultForPreflight,
|
|
14
11
|
toolInputDigest,
|
|
15
12
|
waitForPreflight
|
|
16
|
-
} from "../chunk-
|
|
13
|
+
} from "../chunk-YTF6OCF3.js";
|
|
17
14
|
import {
|
|
18
15
|
observeDemoInvocation
|
|
19
|
-
} from "../chunk-
|
|
16
|
+
} from "../chunk-R2NUH4B7.js";
|
|
20
17
|
import {
|
|
21
18
|
normalizeEnvelope,
|
|
22
19
|
readHookStdin,
|
|
23
20
|
shouldSuppressImportedCursorHandler
|
|
24
21
|
} from "../chunk-TC4FTRYO.js";
|
|
25
|
-
import
|
|
22
|
+
import {
|
|
23
|
+
buildCodexOutput,
|
|
24
|
+
buildCursorOutput,
|
|
25
|
+
buildHermesOutput,
|
|
26
|
+
buildHookOutput,
|
|
27
|
+
demoteForMode,
|
|
28
|
+
failOpenCodex,
|
|
29
|
+
failOpenCursor,
|
|
30
|
+
failOpenHermes,
|
|
31
|
+
failOpenOutput,
|
|
32
|
+
readHookMode
|
|
33
|
+
} from "../chunk-L52OD62C.js";
|
|
26
34
|
import "../chunk-MWNRXAEB.js";
|
|
27
35
|
import "../chunk-NQFRQ56V.js";
|
|
28
36
|
import {
|
|
@@ -40,18 +48,6 @@ import {
|
|
|
40
48
|
import {
|
|
41
49
|
parseAgent
|
|
42
50
|
} from "../chunk-KZB4ABIB.js";
|
|
43
|
-
import {
|
|
44
|
-
buildCodexOutput,
|
|
45
|
-
buildCursorOutput,
|
|
46
|
-
buildHermesOutput,
|
|
47
|
-
buildHookOutput,
|
|
48
|
-
demoteForMode,
|
|
49
|
-
failOpenCodex,
|
|
50
|
-
failOpenCursor,
|
|
51
|
-
failOpenHermes,
|
|
52
|
-
failOpenOutput,
|
|
53
|
-
readHookMode
|
|
54
|
-
} from "../chunk-W27EFR7W.js";
|
|
55
51
|
import "../chunk-JSBRDJBE.js";
|
|
56
52
|
|
|
57
53
|
// src/hooks/pre-tool-use-worker.ts
|
|
@@ -94,14 +90,6 @@ function hermesAdvisory(result, aggregate) {
|
|
|
94
90
|
const message = [...new Set(parts.filter(Boolean))].join("\n");
|
|
95
91
|
return message.startsWith("[primitive]") ? message : message ? `[primitive] ${message}` : "";
|
|
96
92
|
}
|
|
97
|
-
var protectedShell = false;
|
|
98
|
-
async function pause(message, checkId) {
|
|
99
|
-
const result = pausedResult(message, checkId);
|
|
100
|
-
await emit(
|
|
101
|
-
agent === "codex" ? buildCodexOutput("deny", [result]) : buildHookOutput("deny", [result]),
|
|
102
|
-
true
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
93
|
async function main() {
|
|
106
94
|
const mode = readHookMode(process.env);
|
|
107
95
|
if (mode === "off") {
|
|
@@ -150,7 +138,7 @@ async function main() {
|
|
|
150
138
|
return;
|
|
151
139
|
}
|
|
152
140
|
const toolName = envelope.tool_name;
|
|
153
|
-
protectedShell = (agent === "claude_code" || agent === "codex") && toolName === "Bash";
|
|
141
|
+
const protectedShell = (agent === "claude_code" || agent === "codex") && toolName === "Bash";
|
|
154
142
|
const cwd = typeof envelope.cwd === "string" && envelope.cwd.length > 0 ? envelope.cwd : process.cwd();
|
|
155
143
|
const targets = resolvePreflightTargets({
|
|
156
144
|
toolName,
|
|
@@ -179,10 +167,6 @@ async function main() {
|
|
|
179
167
|
const sessionId = envelope.session_id;
|
|
180
168
|
const callId = invocationId(envelope);
|
|
181
169
|
if (!binding || typeof sessionId !== "string" || !sessionId || !callId) {
|
|
182
|
-
if (protectedShell) {
|
|
183
|
-
await pause("Repository or invocation identity is unavailable");
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
170
|
await emit(failOpen());
|
|
187
171
|
return;
|
|
188
172
|
}
|
|
@@ -224,12 +208,8 @@ async function main() {
|
|
|
224
208
|
try {
|
|
225
209
|
const response = await waitForPreflight(request, {
|
|
226
210
|
deadline,
|
|
227
|
-
stopOnConflict:
|
|
211
|
+
stopOnConflict: true
|
|
228
212
|
});
|
|
229
|
-
if (protectedShell && (!hasCompleteCoverage(response) || !hasConfirmedConflict(response) && response.verdict !== "allow" || response.evaluationCoverage.candidateCount > 0 && proposalIsTruncated(proposalInput))) {
|
|
230
|
-
await pause(response.message || "Shell evaluation is incomplete", response.checkId);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
213
|
if (!hasConfirmedConflict(response)) {
|
|
234
214
|
await emit(failOpen(), true);
|
|
235
215
|
return;
|
|
@@ -240,13 +220,8 @@ async function main() {
|
|
|
240
220
|
request,
|
|
241
221
|
collectScopeAdmits && (toolName === "Bash" || toolName === "Shell") && proposalIsTruncated(proposalInput)
|
|
242
222
|
);
|
|
243
|
-
} catch
|
|
244
|
-
|
|
245
|
-
await pause(
|
|
246
|
-
"Shell evaluation is unavailable or exceeded the two-second deadline",
|
|
247
|
-
error instanceof PreflightPausedError ? error.checkId : void 0
|
|
248
|
-
);
|
|
249
|
-
else await emit(failOpen());
|
|
223
|
+
} catch {
|
|
224
|
+
await emit(failOpen());
|
|
250
225
|
return;
|
|
251
226
|
}
|
|
252
227
|
const aggregate = demoteForMode(result.verdict === "unavailable" ? "deny" : result.verdict, mode);
|
|
@@ -259,6 +234,5 @@ async function main() {
|
|
|
259
234
|
await emit(output, true);
|
|
260
235
|
}
|
|
261
236
|
main().catch(async () => {
|
|
262
|
-
|
|
263
|
-
else await emit(failOpen());
|
|
237
|
+
await emit(failOpen());
|
|
264
238
|
});
|
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
import {
|
|
3
3
|
parseAgent
|
|
4
4
|
} from "../chunk-KZB4ABIB.js";
|
|
5
|
-
import {
|
|
6
|
-
readHookMode
|
|
7
|
-
} from "../chunk-W27EFR7W.js";
|
|
8
5
|
import "../chunk-JSBRDJBE.js";
|
|
9
6
|
|
|
10
7
|
// src/hooks/pre-tool-use.ts
|
|
@@ -22,28 +19,6 @@ var child;
|
|
|
22
19
|
var finished = false;
|
|
23
20
|
var stdout = "";
|
|
24
21
|
var stderr = "";
|
|
25
|
-
var input = "";
|
|
26
|
-
var pauseReason = "[primitive] Operation paused: shell evaluation did not finish within two seconds. Retry the tool call to validate the current invocation.";
|
|
27
|
-
function fallback() {
|
|
28
|
-
if (readHookMode(process.env) === "off" || agent !== "claude_code" && agent !== "codex")
|
|
29
|
-
return silent;
|
|
30
|
-
try {
|
|
31
|
-
const envelope = JSON.parse(input);
|
|
32
|
-
if (envelope?.hook_event_name !== "PreToolUse" || envelope?.tool_name !== "Bash") return silent;
|
|
33
|
-
} catch {
|
|
34
|
-
return silent;
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
hookSpecificOutput: {
|
|
38
|
-
hookEventName: "PreToolUse",
|
|
39
|
-
permissionDecision: "deny",
|
|
40
|
-
permissionDecisionReason: pauseReason
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
process.stdin.setEncoding("utf8").on("data", (chunk) => {
|
|
45
|
-
input += chunk;
|
|
46
|
-
});
|
|
47
22
|
function stopWorker() {
|
|
48
23
|
if (!child?.pid) return;
|
|
49
24
|
try {
|
|
@@ -52,7 +27,7 @@ function stopWorker() {
|
|
|
52
27
|
} catch {
|
|
53
28
|
}
|
|
54
29
|
}
|
|
55
|
-
function finish(output =
|
|
30
|
+
function finish(output = silent, diagnostics = "") {
|
|
56
31
|
if (finished) return;
|
|
57
32
|
finished = true;
|
|
58
33
|
clearTimeout(timer);
|
package/dist/hooks/prim-hook.js
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
import {
|
|
19
19
|
enrichHookPayloadWithFileRefs,
|
|
20
20
|
preserveHookFileMetadata
|
|
21
|
-
} from "../chunk-
|
|
21
|
+
} from "../chunk-EYBE724K.js";
|
|
22
22
|
import {
|
|
23
23
|
prepareCodexContext
|
|
24
24
|
} from "../chunk-HXICZG5J.js";
|
|
@@ -38,13 +38,13 @@ import {
|
|
|
38
38
|
import "../chunk-SLDZJD6Q.js";
|
|
39
39
|
import {
|
|
40
40
|
observeDemoInvocation
|
|
41
|
-
} from "../chunk-
|
|
41
|
+
} from "../chunk-R2NUH4B7.js";
|
|
42
42
|
import {
|
|
43
43
|
normalizeEnvelope,
|
|
44
44
|
readHookStdin,
|
|
45
45
|
shouldSuppressImportedCursorHandler
|
|
46
46
|
} from "../chunk-TC4FTRYO.js";
|
|
47
|
-
import "../chunk-
|
|
47
|
+
import "../chunk-L52OD62C.js";
|
|
48
48
|
import "../chunk-MWNRXAEB.js";
|
|
49
49
|
import {
|
|
50
50
|
scrubFromCwd,
|
|
@@ -63,7 +63,6 @@ import {
|
|
|
63
63
|
import {
|
|
64
64
|
parseAgent
|
|
65
65
|
} from "../chunk-KZB4ABIB.js";
|
|
66
|
-
import "../chunk-W27EFR7W.js";
|
|
67
66
|
import "../chunk-JSBRDJBE.js";
|
|
68
67
|
|
|
69
68
|
// src/hooks/prim-hook.ts
|
package/dist/index.js
CHANGED
|
@@ -32,7 +32,7 @@ import {
|
|
|
32
32
|
hasCompleteCoverage,
|
|
33
33
|
requestPreflight,
|
|
34
34
|
waitForPreflight
|
|
35
|
-
} from "./chunk-
|
|
35
|
+
} from "./chunk-YTF6OCF3.js";
|
|
36
36
|
import {
|
|
37
37
|
fetchFeedbackCapability
|
|
38
38
|
} from "./chunk-2AKDX4RU.js";
|
|
@@ -93,7 +93,7 @@ import {
|
|
|
93
93
|
readDemoJson,
|
|
94
94
|
readRun,
|
|
95
95
|
saveRun
|
|
96
|
-
} from "./chunk-
|
|
96
|
+
} from "./chunk-L52OD62C.js";
|
|
97
97
|
import {
|
|
98
98
|
MANAGED_GIT_HOOK_NAMES,
|
|
99
99
|
assertOwnedHookRuntime,
|
|
@@ -198,7 +198,6 @@ import {
|
|
|
198
198
|
withFileLock,
|
|
199
199
|
withFileLockSync
|
|
200
200
|
} from "./chunk-P2O7NLVN.js";
|
|
201
|
-
import "./chunk-W27EFR7W.js";
|
|
202
201
|
import "./chunk-JSBRDJBE.js";
|
|
203
202
|
|
|
204
203
|
// src/index.ts
|
package/package.json
CHANGED
package/dist/chunk-W27EFR7W.js
DELETED
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
// src/hooks/pre-tool-use-scoring.ts
|
|
2
|
-
import { isAbsolute, relative, sep } from "path";
|
|
3
|
-
function anyUnverified(results) {
|
|
4
|
-
return results.some((r) => r.verdict === "unavailable" || r.truncated);
|
|
5
|
-
}
|
|
6
|
-
function unverifiedNote(results) {
|
|
7
|
-
const causes = [];
|
|
8
|
-
const unavailable = results.find((r) => r.verdict === "unavailable");
|
|
9
|
-
if (unavailable) {
|
|
10
|
-
causes.push(
|
|
11
|
-
unavailable.unavailable ? `decision check skipped \u2014 ${unavailable.unavailable}` : "decision check skipped \u2014 not verified"
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
if (results.some((r) => r.truncated)) {
|
|
15
|
-
causes.push("decision check partial \u2014 conflict set truncated (per-file cap hit)");
|
|
16
|
-
}
|
|
17
|
-
return causes.map((c) => `[primitive] ${c}`).join("\n");
|
|
18
|
-
}
|
|
19
|
-
function blockingReason(results) {
|
|
20
|
-
const parts = results.filter((result) => result.verdict === "ask" || result.verdict === "deny").flatMap((result) => [result.reason, result.additionalContext]).filter((value) => value.length > 0);
|
|
21
|
-
return [...new Set(parts)].join("\n\n") || "[primitive] conflict detected (no detail available)";
|
|
22
|
-
}
|
|
23
|
-
function approvalReason(results) {
|
|
24
|
-
const summaries = results.filter((result) => result.verdict === "ask" || result.verdict === "deny").map((result) => result.reason).filter(Boolean);
|
|
25
|
-
return [
|
|
26
|
-
...new Set(summaries),
|
|
27
|
-
"Approve runs this edit once; it does not change these decisions."
|
|
28
|
-
].join("\n");
|
|
29
|
-
}
|
|
30
|
-
function agentContext(results) {
|
|
31
|
-
return [...new Set(results.map((result) => result.additionalContext).filter(Boolean))].join(
|
|
32
|
-
"\n\n"
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
function advisoryNote(aggregate, results) {
|
|
36
|
-
const details = results.flatMap((result) => [
|
|
37
|
-
...aggregate === "warn" ? [result.reason] : [],
|
|
38
|
-
result.additionalContext
|
|
39
|
-
]);
|
|
40
|
-
if (anyUnverified(results)) details.push(unverifiedNote(results));
|
|
41
|
-
return [...new Set(details.filter((value) => value.length > 0))].join("\n\n");
|
|
42
|
-
}
|
|
43
|
-
function buildHookOutput(aggregate, results) {
|
|
44
|
-
if (aggregate === "ask") {
|
|
45
|
-
const context = agentContext(results);
|
|
46
|
-
return {
|
|
47
|
-
hookSpecificOutput: {
|
|
48
|
-
hookEventName: "PreToolUse",
|
|
49
|
-
permissionDecision: "ask",
|
|
50
|
-
permissionDecisionReason: approvalReason(results),
|
|
51
|
-
...context ? { additionalContext: context } : {}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if (aggregate === "deny") {
|
|
56
|
-
return {
|
|
57
|
-
hookSpecificOutput: {
|
|
58
|
-
hookEventName: "PreToolUse",
|
|
59
|
-
permissionDecision: aggregate,
|
|
60
|
-
permissionDecisionReason: blockingReason(results)
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
const notes = advisoryNote(aggregate, results);
|
|
65
|
-
if (notes.length > 0) {
|
|
66
|
-
return {
|
|
67
|
-
systemMessage: notes,
|
|
68
|
-
hookSpecificOutput: {
|
|
69
|
-
hookEventName: "PreToolUse",
|
|
70
|
-
permissionDecision: "allow",
|
|
71
|
-
additionalContext: notes
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
hookSpecificOutput: {
|
|
77
|
-
hookEventName: "PreToolUse",
|
|
78
|
-
permissionDecision: "allow"
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
function buildCodexOutput(aggregate, results) {
|
|
83
|
-
if (aggregate === "deny" || aggregate === "ask") {
|
|
84
|
-
return {
|
|
85
|
-
hookSpecificOutput: {
|
|
86
|
-
hookEventName: "PreToolUse",
|
|
87
|
-
permissionDecision: "deny",
|
|
88
|
-
permissionDecisionReason: blockingReason(results)
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
const notes = advisoryNote(aggregate, results);
|
|
93
|
-
return notes ? {
|
|
94
|
-
systemMessage: notes,
|
|
95
|
-
hookSpecificOutput: { hookEventName: "PreToolUse", additionalContext: notes }
|
|
96
|
-
} : {};
|
|
97
|
-
}
|
|
98
|
-
function buildCursorOutput(aggregate, results) {
|
|
99
|
-
if (aggregate === "deny" || aggregate === "ask") {
|
|
100
|
-
const reason = blockingReason(results);
|
|
101
|
-
return { permission: "deny", user_message: reason, agent_message: reason };
|
|
102
|
-
}
|
|
103
|
-
const notes = advisoryNote(aggregate, results);
|
|
104
|
-
return notes ? { permission: "allow", agent_message: notes } : { permission: "allow" };
|
|
105
|
-
}
|
|
106
|
-
function failOpenOutput() {
|
|
107
|
-
return {
|
|
108
|
-
hookSpecificOutput: {
|
|
109
|
-
hookEventName: "PreToolUse",
|
|
110
|
-
permissionDecision: "allow"
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
function failOpenCodex() {
|
|
115
|
-
return {};
|
|
116
|
-
}
|
|
117
|
-
function failOpenCursor() {
|
|
118
|
-
return { permission: "allow" };
|
|
119
|
-
}
|
|
120
|
-
function buildHermesOutput(aggregate, results) {
|
|
121
|
-
if (aggregate !== "deny" && aggregate !== "ask") {
|
|
122
|
-
return {};
|
|
123
|
-
}
|
|
124
|
-
const reason = results.filter((r) => r.verdict === "deny" || r.verdict === "ask").map((r) => r.reason).filter((s) => s.length > 0).join("\n\n");
|
|
125
|
-
const directive = results.map((r) => r.additionalContext).filter((s) => s.length > 0 && s !== reason).join("\n");
|
|
126
|
-
const message = [reason, directive].filter((s) => s.length > 0).join("\n\n") || "[primitive] conflict detected (no detail available)";
|
|
127
|
-
return { action: "block", message };
|
|
128
|
-
}
|
|
129
|
-
function failOpenHermes() {
|
|
130
|
-
return {};
|
|
131
|
-
}
|
|
132
|
-
var SUPPORTED_TOOLS = /* @__PURE__ */ new Set(["Edit", "Write", "MultiEdit", "NotebookEdit"]);
|
|
133
|
-
var APPLY_PATCH_FILE_RE = /^\*\*\* (?:Update|Add|Delete) File: (?<path>.+)$/;
|
|
134
|
-
var MOVE_FILE_RE = /^\*\*\* Move File:\s*(?<src>.+?)\s*->\s*(?<dst>.+?)\s*$/;
|
|
135
|
-
var MOVE_TO_RE = /^\*\*\* Move to: (?<dst>.+)$/;
|
|
136
|
-
var LINE_SPLIT_RE = /\r?\n/;
|
|
137
|
-
function parseApplyPatchTargets(command) {
|
|
138
|
-
const paths = /* @__PURE__ */ new Set();
|
|
139
|
-
let activeUpdate = false;
|
|
140
|
-
let complete = true;
|
|
141
|
-
for (const line of command.split(LINE_SPLIT_RE)) {
|
|
142
|
-
const path = APPLY_PATCH_FILE_RE.exec(line)?.groups?.path;
|
|
143
|
-
if (path) {
|
|
144
|
-
paths.add(path);
|
|
145
|
-
activeUpdate = line.startsWith("*** Update File:");
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
const moveTo = MOVE_TO_RE.exec(line)?.groups?.dst;
|
|
149
|
-
if (moveTo) {
|
|
150
|
-
if (activeUpdate) paths.add(moveTo);
|
|
151
|
-
else complete = false;
|
|
152
|
-
activeUpdate = false;
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
const move = MOVE_FILE_RE.exec(line)?.groups;
|
|
156
|
-
if (move?.src) {
|
|
157
|
-
paths.add(move.src);
|
|
158
|
-
if (move.dst) {
|
|
159
|
-
paths.add(move.dst);
|
|
160
|
-
}
|
|
161
|
-
activeUpdate = false;
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
if (line === "*** Begin Patch" || line === "*** End Patch") activeUpdate = false;
|
|
165
|
-
else if (line.startsWith("*** ")) complete = false;
|
|
166
|
-
}
|
|
167
|
-
return { paths: [...paths], complete: complete && paths.size > 0 };
|
|
168
|
-
}
|
|
169
|
-
function extractCodexFileTargets(toolName, toolInput) {
|
|
170
|
-
if (toolName !== "apply_patch") {
|
|
171
|
-
return null;
|
|
172
|
-
}
|
|
173
|
-
const text = codexPatchText(toolInput);
|
|
174
|
-
return text !== null ? parseApplyPatchTargets(text) : { paths: [], complete: false };
|
|
175
|
-
}
|
|
176
|
-
function codexPatchText(toolInput) {
|
|
177
|
-
if (typeof toolInput === "string") return toolInput;
|
|
178
|
-
if (!toolInput || typeof toolInput !== "object") return null;
|
|
179
|
-
const input = toolInput;
|
|
180
|
-
for (const key of ["command", "input", "patch"]) {
|
|
181
|
-
const value = input[key];
|
|
182
|
-
if (typeof value === "string") return value;
|
|
183
|
-
}
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
var HERMES_EDITING_TOOLS = /* @__PURE__ */ new Set(["write_file", "patch"]);
|
|
187
|
-
function extractHermesFileTargets(toolName, toolInput) {
|
|
188
|
-
if (!HERMES_EDITING_TOOLS.has(toolName)) {
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
if (!toolInput || typeof toolInput !== "object") {
|
|
192
|
-
return { paths: [], complete: false };
|
|
193
|
-
}
|
|
194
|
-
const input = toolInput;
|
|
195
|
-
if (toolName === "patch" && input.mode === "patch") {
|
|
196
|
-
return typeof input.patch === "string" ? parseApplyPatchTargets(input.patch) : { paths: [], complete: false };
|
|
197
|
-
}
|
|
198
|
-
const paths = typeof input.path === "string" && input.path.length > 0 ? [input.path] : [];
|
|
199
|
-
const mode = input.mode;
|
|
200
|
-
return { paths, complete: paths.length > 0 && (mode === void 0 || mode === "replace") };
|
|
201
|
-
}
|
|
202
|
-
function extractCursorFileTargets(toolName, toolInput) {
|
|
203
|
-
if (toolName !== "Write" && toolName !== "Delete") return null;
|
|
204
|
-
if (!toolInput || typeof toolInput !== "object" || Array.isArray(toolInput)) {
|
|
205
|
-
return { paths: [], complete: false };
|
|
206
|
-
}
|
|
207
|
-
const input = toolInput;
|
|
208
|
-
const candidates = [input.file_path, input.path].filter(
|
|
209
|
-
(value) => typeof value === "string" && value.length > 0
|
|
210
|
-
);
|
|
211
|
-
const paths = [...new Set(candidates)];
|
|
212
|
-
return { paths, complete: paths.length === 1 };
|
|
213
|
-
}
|
|
214
|
-
function extractFileTargets(toolName, toolInput, agent = "claude_code") {
|
|
215
|
-
if (agent === "codex") {
|
|
216
|
-
return extractCodexFileTargets(toolName, toolInput);
|
|
217
|
-
}
|
|
218
|
-
if (agent === "cursor") {
|
|
219
|
-
return extractCursorFileTargets(toolName, toolInput);
|
|
220
|
-
}
|
|
221
|
-
if (agent === "hermes") {
|
|
222
|
-
return extractHermesFileTargets(toolName, toolInput);
|
|
223
|
-
}
|
|
224
|
-
if (!SUPPORTED_TOOLS.has(toolName)) {
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
if (!toolInput || typeof toolInput !== "object") {
|
|
228
|
-
return { paths: [], complete: false };
|
|
229
|
-
}
|
|
230
|
-
const input = toolInput;
|
|
231
|
-
if (toolName === "NotebookEdit") {
|
|
232
|
-
const paths2 = typeof input.notebook_path === "string" && input.notebook_path.length > 0 ? [input.notebook_path] : [];
|
|
233
|
-
return { paths: paths2, complete: paths2.length > 0 };
|
|
234
|
-
}
|
|
235
|
-
const paths = typeof input.file_path === "string" && input.file_path.length > 0 ? [input.file_path] : [];
|
|
236
|
-
return { paths, complete: paths.length > 0 };
|
|
237
|
-
}
|
|
238
|
-
function readHookMode(env) {
|
|
239
|
-
if (env.PRIM_BYPASS === "1" || env.PRIM_BYPASS === "true") {
|
|
240
|
-
return "off";
|
|
241
|
-
}
|
|
242
|
-
const mode = env.PRIM_HOOK_MODE;
|
|
243
|
-
if (mode === "off" || mode === "warn") {
|
|
244
|
-
return mode;
|
|
245
|
-
}
|
|
246
|
-
return "block";
|
|
247
|
-
}
|
|
248
|
-
function demoteForMode(verdict, mode) {
|
|
249
|
-
if (mode === "off") {
|
|
250
|
-
return "allow";
|
|
251
|
-
}
|
|
252
|
-
if (mode === "warn" && (verdict === "ask" || verdict === "deny")) {
|
|
253
|
-
return "warn";
|
|
254
|
-
}
|
|
255
|
-
return verdict;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
export {
|
|
259
|
-
buildHookOutput,
|
|
260
|
-
buildCodexOutput,
|
|
261
|
-
buildCursorOutput,
|
|
262
|
-
failOpenOutput,
|
|
263
|
-
failOpenCodex,
|
|
264
|
-
failOpenCursor,
|
|
265
|
-
buildHermesOutput,
|
|
266
|
-
failOpenHermes,
|
|
267
|
-
parseApplyPatchTargets,
|
|
268
|
-
extractFileTargets,
|
|
269
|
-
readHookMode,
|
|
270
|
-
demoteForMode
|
|
271
|
-
};
|