@pushary/agent-hooks 0.18.2 → 0.19.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/data/cursor-plugin/scripts/pushary-gate.mjs +8 -1
- package/dist/bin/pushary-clean.js +15 -2
- package/dist/bin/pushary-codex-hook.js +3 -3
- package/dist/bin/pushary-codex.js +3 -3
- package/dist/bin/pushary-doctor.js +29 -2
- package/dist/bin/pushary-gemini-hook.d.ts +1 -0
- package/dist/bin/pushary-gemini-hook.js +246 -0
- package/dist/bin/pushary-hook.js +3 -3
- package/dist/bin/pushary-post-hook.js +3 -3
- package/dist/bin/pushary-prompt-hook.js +3 -3
- package/dist/bin/pushary-setup.js +55 -4
- package/dist/bin/pushary-stop-hook.js +3 -3
- package/dist/bin/pushary.js +1 -1
- package/dist/chunk-ACE77TKQ.js +328 -0
- package/dist/chunk-QY4L6XHN.js +384 -0
- package/dist/chunk-SDREQWNI.js +175 -0
- package/dist/chunk-USUCPCUC.js +156 -0
- package/dist/chunk-ZWBS3T7Q.js +244 -0
- package/dist/src/index.js +4 -4
- package/package.json +3 -2
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import {
|
|
2
|
+
extractPolicyArg,
|
|
3
|
+
isApprovalMode,
|
|
4
|
+
isSafeReadOnlyCommand,
|
|
5
|
+
matchRankWeight,
|
|
6
|
+
matchToolPattern
|
|
7
|
+
} from "./chunk-USUCPCUC.js";
|
|
8
|
+
import {
|
|
9
|
+
callMcpTool,
|
|
10
|
+
withRetry
|
|
11
|
+
} from "./chunk-DWED7BS3.js";
|
|
12
|
+
import {
|
|
13
|
+
getBaseUrl
|
|
14
|
+
} from "./chunk-NKXSILEW.js";
|
|
15
|
+
|
|
16
|
+
// src/validate.ts
|
|
17
|
+
var isPolicyConfig = (data) => {
|
|
18
|
+
if (!data || typeof data !== "object") return false;
|
|
19
|
+
const d = data;
|
|
20
|
+
return Array.isArray(d.policies) && typeof d.defaultTimeoutSeconds === "number" && typeof d.defaultTimeoutAction === "string";
|
|
21
|
+
};
|
|
22
|
+
var isAskUserResponse = (data) => {
|
|
23
|
+
if (!data || typeof data !== "object") return false;
|
|
24
|
+
const d = data;
|
|
25
|
+
return typeof d.correlationId === "string" && typeof d.status === "string";
|
|
26
|
+
};
|
|
27
|
+
var isWaitForAnswerResponse = (data) => {
|
|
28
|
+
if (!data || typeof data !== "object") return false;
|
|
29
|
+
const d = data;
|
|
30
|
+
return typeof d.answered === "boolean";
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/api.ts
|
|
34
|
+
var askUser = async (apiKey, params) => {
|
|
35
|
+
const result = await callMcpTool(apiKey, "ask_user", { ...params, wait: false }, { maxRetries: 3 });
|
|
36
|
+
if (!isAskUserResponse(result)) throw new Error("Invalid ask_user response");
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
|
|
40
|
+
const result = await callMcpTool(apiKey, "wait_for_answer", {
|
|
41
|
+
correlationId,
|
|
42
|
+
timeoutMs
|
|
43
|
+
});
|
|
44
|
+
if (!isWaitForAnswerResponse(result)) throw new Error("Invalid wait_for_answer response");
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
var cancelQuestion = async (apiKey, correlationId) => {
|
|
48
|
+
await callMcpTool(apiKey, "cancel_question", { correlationId });
|
|
49
|
+
};
|
|
50
|
+
var sendNotification = async (apiKey, params) => {
|
|
51
|
+
await callMcpTool(apiKey, "send_notification", { ...params }, { maxRetries: 3 });
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/policy.ts
|
|
55
|
+
import { createHash } from "crypto";
|
|
56
|
+
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
57
|
+
import { join } from "path";
|
|
58
|
+
import { tmpdir } from "os";
|
|
59
|
+
var CACHE_TTL_MS = 60 * 1e3;
|
|
60
|
+
var cacheFile = (apiKey) => {
|
|
61
|
+
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
62
|
+
return join(tmpdir(), `pushary-policy-${hash}.json`);
|
|
63
|
+
};
|
|
64
|
+
var fetchPolicy = async (apiKey) => {
|
|
65
|
+
return withRetry(async () => {
|
|
66
|
+
const baseUrl = getBaseUrl();
|
|
67
|
+
const response = await fetch(`${baseUrl}/api/mcp/policy`, {
|
|
68
|
+
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
69
|
+
signal: AbortSignal.timeout(1e4)
|
|
70
|
+
});
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
throw new Error(`Failed to fetch policy: ${response.status}`);
|
|
73
|
+
}
|
|
74
|
+
const raw = await response.json();
|
|
75
|
+
if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
|
|
76
|
+
return raw;
|
|
77
|
+
}, { maxAttempts: 2 });
|
|
78
|
+
};
|
|
79
|
+
var getPolicy = async (apiKey, expectedVersion) => {
|
|
80
|
+
const path = cacheFile(apiKey);
|
|
81
|
+
let staleCache = null;
|
|
82
|
+
if (existsSync(path)) {
|
|
83
|
+
try {
|
|
84
|
+
const stat = readFileSync(path, "utf-8");
|
|
85
|
+
const cached = JSON.parse(stat);
|
|
86
|
+
if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
|
|
87
|
+
const versionStale = expectedVersion != null && (cached._policyVersion ?? null) !== expectedVersion;
|
|
88
|
+
const ttlFresh = !cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS;
|
|
89
|
+
if (ttlFresh && !versionStale) {
|
|
90
|
+
return cached;
|
|
91
|
+
}
|
|
92
|
+
staleCache = cached;
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const policy = await fetchPolicy(apiKey);
|
|
98
|
+
try {
|
|
99
|
+
writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now(), _policyVersion: expectedVersion ?? null }), "utf-8");
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
return policy;
|
|
103
|
+
} catch {
|
|
104
|
+
if (staleCache) return staleCache;
|
|
105
|
+
throw new Error("Failed to fetch policy and no cached policy available");
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var findBestMatch = (policies, toolName, arg) => {
|
|
109
|
+
let best;
|
|
110
|
+
let bestWeight = 0;
|
|
111
|
+
let bestLength = -1;
|
|
112
|
+
for (const candidate of policies) {
|
|
113
|
+
const rank = matchToolPattern(candidate.tool, toolName, arg);
|
|
114
|
+
if (rank === "none") continue;
|
|
115
|
+
const weight = matchRankWeight(rank);
|
|
116
|
+
const length = rank === "prefix" ? candidate.tool.length : -1;
|
|
117
|
+
if (weight > bestWeight || weight === bestWeight && length > bestLength) {
|
|
118
|
+
best = { policy: candidate, rank };
|
|
119
|
+
bestWeight = weight;
|
|
120
|
+
bestLength = length;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return best;
|
|
124
|
+
};
|
|
125
|
+
var autoApprove = (tool) => ({
|
|
126
|
+
tool,
|
|
127
|
+
timeoutSeconds: 0,
|
|
128
|
+
timeoutAction: "approve",
|
|
129
|
+
mode: "terminal_only",
|
|
130
|
+
pushFirstSeconds: 0
|
|
131
|
+
});
|
|
132
|
+
var resolvePolicy = (config, toolName, modeOverride, toolInput) => {
|
|
133
|
+
const arg = toolInput ? extractPolicyArg(toolName, toolInput) : void 0;
|
|
134
|
+
const match = findBestMatch(config.policies, toolName, arg);
|
|
135
|
+
let base = match?.policy ?? config.policies.find((p) => p.tool === "*") ?? {
|
|
136
|
+
tool: toolName,
|
|
137
|
+
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
138
|
+
timeoutAction: config.defaultTimeoutAction,
|
|
139
|
+
mode: config.defaultMode ?? "push_first",
|
|
140
|
+
pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
|
|
141
|
+
};
|
|
142
|
+
const governedBySpecificRule = match?.rank === "exact" || match?.rank === "prefix";
|
|
143
|
+
if (!governedBySpecificRule && toolName === "Bash" && typeof arg === "string" && isSafeReadOnlyCommand(arg)) {
|
|
144
|
+
base = autoApprove(base.tool);
|
|
145
|
+
}
|
|
146
|
+
const effectiveOverride = modeOverride ?? config.modeOverride;
|
|
147
|
+
if (effectiveOverride) {
|
|
148
|
+
return { ...base, mode: effectiveOverride };
|
|
149
|
+
}
|
|
150
|
+
return base;
|
|
151
|
+
};
|
|
152
|
+
var toPolicyVersion = (value) => typeof value === "string" || typeof value === "number" ? String(value) : null;
|
|
153
|
+
var fetchModeState = async (apiKey, sessionId) => {
|
|
154
|
+
try {
|
|
155
|
+
const baseUrl = getBaseUrl();
|
|
156
|
+
const url = sessionId ? `${baseUrl}/api/mcp/mode?session=${encodeURIComponent(sessionId)}` : `${baseUrl}/api/mcp/mode`;
|
|
157
|
+
const response = await fetch(url, {
|
|
158
|
+
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
159
|
+
signal: AbortSignal.timeout(3e3)
|
|
160
|
+
});
|
|
161
|
+
if (!response.ok) return { mode: null, kill: false, policyVersion: null };
|
|
162
|
+
const data = await response.json();
|
|
163
|
+
const mode = data.override?.mode;
|
|
164
|
+
return {
|
|
165
|
+
mode: isApprovalMode(mode) ? mode : null,
|
|
166
|
+
kill: data.kill === true,
|
|
167
|
+
policyVersion: toPolicyVersion(data.policyVersion)
|
|
168
|
+
};
|
|
169
|
+
} catch {
|
|
170
|
+
return { mode: null, kill: false, policyVersion: null };
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var fetchModeOverride = async (apiKey) => (await fetchModeState(apiKey)).mode;
|
|
174
|
+
|
|
175
|
+
// src/describe.ts
|
|
176
|
+
import { isAbsolute, relative } from "path";
|
|
177
|
+
var hookPrefixes = {
|
|
178
|
+
Bash: (input) => `bash: ${input.command ?? "(no command)"}`,
|
|
179
|
+
Write: (input) => `write file: ${input.file_path ?? "(unknown path)"}`,
|
|
180
|
+
Edit: (input) => `edit file: ${input.file_path ?? "(unknown path)"}`,
|
|
181
|
+
Read: (input) => `read file: ${input.file_path ?? "(unknown path)"}`
|
|
182
|
+
};
|
|
183
|
+
var eventPrefixes = {
|
|
184
|
+
Bash: (input) => `ran: ${String(input.command ?? "").slice(0, 120)}`,
|
|
185
|
+
Write: (input) => `wrote: ${input.file_path ?? "unknown"}`,
|
|
186
|
+
Edit: (input) => `edited: ${input.file_path ?? "unknown"}`,
|
|
187
|
+
Read: (input) => `read: ${input.file_path ?? "unknown"}`
|
|
188
|
+
};
|
|
189
|
+
var describeToolCall = (toolName, toolInput, format = "hook") => {
|
|
190
|
+
const prefixes = format === "hook" ? hookPrefixes : eventPrefixes;
|
|
191
|
+
const builder = prefixes[toolName];
|
|
192
|
+
if (builder) return builder(toolInput);
|
|
193
|
+
return format === "hook" ? `${toolName}: ${JSON.stringify(toolInput).slice(0, 200)}` : `${toolName}: done`;
|
|
194
|
+
};
|
|
195
|
+
var TOOL_TARGET_MAX_LENGTH = 80;
|
|
196
|
+
var deriveCommandHead = (command) => {
|
|
197
|
+
if (typeof command !== "string") return void 0;
|
|
198
|
+
const head = command.trim().split(/\s+/).slice(0, 2).join(" ");
|
|
199
|
+
return head ? head.slice(0, TOOL_TARGET_MAX_LENGTH) : void 0;
|
|
200
|
+
};
|
|
201
|
+
var deriveToolTarget = (toolName, toolInput) => {
|
|
202
|
+
if (toolName === "Bash") {
|
|
203
|
+
return deriveCommandHead(toolInput.command);
|
|
204
|
+
}
|
|
205
|
+
if (toolName === "Edit" || toolName === "Write") {
|
|
206
|
+
const filePath = toolInput.file_path;
|
|
207
|
+
if (typeof filePath !== "string") return void 0;
|
|
208
|
+
const separator = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
|
|
209
|
+
const base = filePath.slice(separator + 1);
|
|
210
|
+
const dot = base.lastIndexOf(".");
|
|
211
|
+
if (dot <= 0) return void 0;
|
|
212
|
+
return base.slice(dot).slice(0, TOOL_TARGET_MAX_LENGTH);
|
|
213
|
+
}
|
|
214
|
+
return void 0;
|
|
215
|
+
};
|
|
216
|
+
var RECEIPT_TARGET_MAX_LENGTH = 256;
|
|
217
|
+
var isToolResultError = (toolResult) => {
|
|
218
|
+
try {
|
|
219
|
+
return Boolean(toolResult && ("error" in toolResult || "is_error" in toolResult));
|
|
220
|
+
} catch {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
var relativizeReceiptPath = (filePath, cwd) => {
|
|
225
|
+
if (!cwd || !isAbsolute(filePath)) return filePath;
|
|
226
|
+
return relative(cwd, filePath);
|
|
227
|
+
};
|
|
228
|
+
var deriveReceiptMeta = (toolName, toolInput, toolResult, cwd) => {
|
|
229
|
+
try {
|
|
230
|
+
const ok = !isToolResultError(toolResult);
|
|
231
|
+
if (toolName === "Edit" || toolName === "Write") {
|
|
232
|
+
const filePath = toolInput.file_path;
|
|
233
|
+
if (typeof filePath !== "string" || !filePath) return void 0;
|
|
234
|
+
return {
|
|
235
|
+
kind: toolName === "Edit" ? "edit" : "write",
|
|
236
|
+
target: relativizeReceiptPath(filePath, cwd).slice(0, RECEIPT_TARGET_MAX_LENGTH),
|
|
237
|
+
ok
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (toolName === "Bash") {
|
|
241
|
+
const head = deriveCommandHead(toolInput.command);
|
|
242
|
+
if (!head) return void 0;
|
|
243
|
+
return {
|
|
244
|
+
kind: head === "git commit" ? "commit" : "bash",
|
|
245
|
+
target: head,
|
|
246
|
+
ok
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return void 0;
|
|
250
|
+
} catch {
|
|
251
|
+
return void 0;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/identity.ts
|
|
256
|
+
import { createHash as createHash2 } from "crypto";
|
|
257
|
+
import { hostname } from "os";
|
|
258
|
+
var deriveMachineId = (host) => createHash2("sha256").update(host).digest("hex").slice(0, 8);
|
|
259
|
+
var getMachineId = () => deriveMachineId(hostname());
|
|
260
|
+
|
|
261
|
+
// src/pending.ts
|
|
262
|
+
import { join as join2 } from "path";
|
|
263
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
264
|
+
import { existsSync as existsSync2, mkdirSync, writeFileSync as writeFileSync2, readdirSync, unlinkSync, rmSync, statSync } from "fs";
|
|
265
|
+
var PENDING_DIR = join2(tmpdir2(), "pushary-pending");
|
|
266
|
+
var DEFAULT_SESSION = "_no_session";
|
|
267
|
+
var GRACE_MS = 10 * 60 * 1e3;
|
|
268
|
+
var sanitize = (sessionId) => sessionId.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 128) || DEFAULT_SESSION;
|
|
269
|
+
var dirFor = (sessionId) => join2(PENDING_DIR, sanitize(sessionId));
|
|
270
|
+
var isDefaultSession = (sessionId) => sanitize(sessionId) === DEFAULT_SESSION;
|
|
271
|
+
var savePendingQuestion = (sessionId, correlationId) => {
|
|
272
|
+
const dir = dirFor(sessionId);
|
|
273
|
+
if (!existsSync2(dir)) mkdirSync(dir, { recursive: true });
|
|
274
|
+
writeFileSync2(join2(dir, correlationId), "", "utf-8");
|
|
275
|
+
};
|
|
276
|
+
var listPendingQuestions = (sessionId) => {
|
|
277
|
+
const dir = dirFor(sessionId);
|
|
278
|
+
let files;
|
|
279
|
+
try {
|
|
280
|
+
files = readdirSync(dir);
|
|
281
|
+
} catch {
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
if (!isDefaultSession(sessionId)) return files;
|
|
285
|
+
const cutoff = Date.now() - GRACE_MS;
|
|
286
|
+
return files.filter((name) => {
|
|
287
|
+
try {
|
|
288
|
+
return statSync(join2(dir, name)).mtimeMs < cutoff;
|
|
289
|
+
} catch {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
};
|
|
294
|
+
var removePendingQuestion = (sessionId, correlationId) => {
|
|
295
|
+
try {
|
|
296
|
+
unlinkSync(join2(dirFor(sessionId), correlationId));
|
|
297
|
+
} catch {
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
var removePendingSession = (sessionId) => {
|
|
301
|
+
try {
|
|
302
|
+
rmSync(dirFor(sessionId), { recursive: true, force: true });
|
|
303
|
+
} catch {
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
export {
|
|
308
|
+
isPolicyConfig,
|
|
309
|
+
askUser,
|
|
310
|
+
waitForAnswer,
|
|
311
|
+
cancelQuestion,
|
|
312
|
+
sendNotification,
|
|
313
|
+
getPolicy,
|
|
314
|
+
resolvePolicy,
|
|
315
|
+
fetchModeState,
|
|
316
|
+
fetchModeOverride,
|
|
317
|
+
describeToolCall,
|
|
318
|
+
deriveToolTarget,
|
|
319
|
+
isToolResultError,
|
|
320
|
+
deriveReceiptMeta,
|
|
321
|
+
getMachineId,
|
|
322
|
+
DEFAULT_SESSION,
|
|
323
|
+
isDefaultSession,
|
|
324
|
+
savePendingQuestion,
|
|
325
|
+
listPendingQuestions,
|
|
326
|
+
removePendingQuestion,
|
|
327
|
+
removePendingSession
|
|
328
|
+
};
|