@pushary/agent-hooks 0.39.0 → 0.42.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/dist/bin/pushary-claude.js +875 -48
- package/dist/bin/pushary-codex-hook.js +15 -13
- package/dist/bin/pushary-codex.js +4 -2
- package/dist/bin/pushary-gemini-hook.js +10 -8
- package/dist/bin/pushary-hook.js +3 -2
- package/dist/bin/pushary-notification-hook.js +2 -1
- package/dist/bin/pushary-permission-denied-hook.js +3 -2
- package/dist/bin/pushary-permission-hook.js +3 -2
- package/dist/bin/pushary-post-hook.js +2 -1
- package/dist/bin/pushary-prompt-hook.js +2 -1
- package/dist/bin/pushary-session-end-hook.js +2 -1
- package/dist/bin/pushary-session-start-hook.js +2 -1
- package/dist/bin/pushary-setup.js +2 -1
- package/dist/bin/pushary-stop-hook.js +2 -1
- package/dist/bin/pushary-stopfailure-hook.js +2 -1
- package/dist/bin/pushary.js +3 -1
- package/dist/chunk-ETDXSKR5.js +355 -0
- package/dist/{chunk-JHN3H6LX.js → chunk-NRCQ5UUR.js} +49 -374
- package/dist/{chunk-X6L25EOF.js → chunk-V2TSLWTV.js} +7 -5
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +8 -6
- package/package.json +2 -2
|
@@ -1,350 +1,39 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
cancelQuestion,
|
|
3
|
+
deriveReceiptMeta,
|
|
4
|
+
deriveToolTarget,
|
|
5
|
+
describeToolCall,
|
|
6
|
+
fetchModeState,
|
|
7
|
+
getMachineId,
|
|
8
|
+
isPolicyConfig,
|
|
9
|
+
isToolResultError,
|
|
10
|
+
resolveAutoResolveOrigin,
|
|
11
|
+
resolvePolicy,
|
|
12
|
+
sendNotification,
|
|
13
|
+
waitForAnswer
|
|
14
|
+
} from "./chunk-ETDXSKR5.js";
|
|
15
|
+
import {
|
|
3
16
|
withRetry
|
|
4
17
|
} from "./chunk-DWED7BS3.js";
|
|
5
|
-
import {
|
|
6
|
-
ACTION_BODY_MAX,
|
|
7
|
-
DECISION_LINE_MAX,
|
|
8
|
-
extractPolicyArg,
|
|
9
|
-
isApprovalMode,
|
|
10
|
-
isSafeReadOnlyCommand,
|
|
11
|
-
matchRankWeight,
|
|
12
|
-
matchToolPattern,
|
|
13
|
-
redactSecrets,
|
|
14
|
-
redactSecretsDeep
|
|
15
|
-
} from "./chunk-Z5PL3K7C.js";
|
|
16
18
|
import {
|
|
17
19
|
getApiKey,
|
|
18
20
|
getBaseUrl
|
|
19
21
|
} from "./chunk-NKXSILEW.js";
|
|
20
22
|
|
|
21
|
-
// src/
|
|
22
|
-
var isPolicyConfig = (data) => {
|
|
23
|
-
if (!data || typeof data !== "object") return false;
|
|
24
|
-
const d = data;
|
|
25
|
-
return Array.isArray(d.policies) && typeof d.defaultTimeoutSeconds === "number" && typeof d.defaultTimeoutAction === "string";
|
|
26
|
-
};
|
|
27
|
-
var isAskUserResponse = (data) => {
|
|
28
|
-
if (!data || typeof data !== "object") return false;
|
|
29
|
-
const d = data;
|
|
30
|
-
return typeof d.correlationId === "string" && typeof d.status === "string";
|
|
31
|
-
};
|
|
32
|
-
var isWaitForAnswerResponse = (data) => {
|
|
33
|
-
if (!data || typeof data !== "object") return false;
|
|
34
|
-
const d = data;
|
|
35
|
-
return typeof d.answered === "boolean";
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// src/api.ts
|
|
39
|
-
var askUser = async (apiKey, params, timeoutMs = 15e3) => {
|
|
40
|
-
const result = await callMcpTool(apiKey, "ask_user", { ...params, wait: false }, { maxRetries: 3, timeoutMs });
|
|
41
|
-
if (!isAskUserResponse(result)) throw new Error("Invalid ask_user response");
|
|
42
|
-
return result;
|
|
43
|
-
};
|
|
44
|
-
var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
|
|
45
|
-
const result = await callMcpTool(apiKey, "wait_for_answer", {
|
|
46
|
-
correlationId,
|
|
47
|
-
timeoutMs
|
|
48
|
-
}, { timeoutMs: timeoutMs + 1e4 });
|
|
49
|
-
if (!isWaitForAnswerResponse(result)) throw new Error("Invalid wait_for_answer response");
|
|
50
|
-
return result;
|
|
51
|
-
};
|
|
52
|
-
var cancelQuestion = async (apiKey, correlationId) => {
|
|
53
|
-
await callMcpTool(apiKey, "cancel_question", { correlationId });
|
|
54
|
-
};
|
|
55
|
-
var sendNotification = async (apiKey, params, timeoutMs = 15e3) => {
|
|
56
|
-
await callMcpTool(apiKey, "send_notification", { ...params }, { maxRetries: 3, timeoutMs });
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// src/policy.ts
|
|
60
|
-
import { createHash } from "crypto";
|
|
61
|
-
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
23
|
+
// src/pending.ts
|
|
62
24
|
import { join } from "path";
|
|
63
25
|
import { tmpdir } from "os";
|
|
64
|
-
|
|
65
|
-
var
|
|
66
|
-
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
67
|
-
return join(tmpdir(), `pushary-policy-${hash}.json`);
|
|
68
|
-
};
|
|
69
|
-
var fetchPolicy = async (apiKey) => {
|
|
70
|
-
return withRetry(async () => {
|
|
71
|
-
const baseUrl = getBaseUrl();
|
|
72
|
-
const response = await fetch(`${baseUrl}/api/mcp/policy`, {
|
|
73
|
-
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
74
|
-
signal: AbortSignal.timeout(1e4)
|
|
75
|
-
});
|
|
76
|
-
if (!response.ok) {
|
|
77
|
-
throw new Error(`Failed to fetch policy: ${response.status}`);
|
|
78
|
-
}
|
|
79
|
-
const raw = await response.json();
|
|
80
|
-
if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
|
|
81
|
-
return raw;
|
|
82
|
-
}, { maxAttempts: 2 });
|
|
83
|
-
};
|
|
84
|
-
var getPolicy = async (apiKey, expectedVersion) => {
|
|
85
|
-
const path = cacheFile(apiKey);
|
|
86
|
-
let staleCache = null;
|
|
87
|
-
if (existsSync(path)) {
|
|
88
|
-
try {
|
|
89
|
-
const stat = readFileSync(path, "utf-8");
|
|
90
|
-
const cached = JSON.parse(stat);
|
|
91
|
-
if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
|
|
92
|
-
const versionStale = expectedVersion != null && (cached._policyVersion ?? null) !== expectedVersion;
|
|
93
|
-
const ttlFresh = !cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS;
|
|
94
|
-
if (ttlFresh && !versionStale) {
|
|
95
|
-
return cached;
|
|
96
|
-
}
|
|
97
|
-
staleCache = cached;
|
|
98
|
-
} catch {
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
try {
|
|
102
|
-
const policy = await fetchPolicy(apiKey);
|
|
103
|
-
try {
|
|
104
|
-
writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now(), _policyVersion: expectedVersion ?? null }), "utf-8");
|
|
105
|
-
} catch {
|
|
106
|
-
}
|
|
107
|
-
return policy;
|
|
108
|
-
} catch {
|
|
109
|
-
if (staleCache) return staleCache;
|
|
110
|
-
throw new Error("Failed to fetch policy and no cached policy available");
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
var findBestMatch = (policies, toolName, arg) => {
|
|
114
|
-
let best;
|
|
115
|
-
let bestWeight = 0;
|
|
116
|
-
let bestLength = -1;
|
|
117
|
-
for (const candidate of policies) {
|
|
118
|
-
const rank = matchToolPattern(candidate.tool, toolName, arg);
|
|
119
|
-
if (rank === "none") continue;
|
|
120
|
-
const weight = matchRankWeight(rank);
|
|
121
|
-
const length = rank === "prefix" ? candidate.tool.length : -1;
|
|
122
|
-
if (weight > bestWeight || weight === bestWeight && length > bestLength) {
|
|
123
|
-
best = { policy: candidate, rank };
|
|
124
|
-
bestWeight = weight;
|
|
125
|
-
bestLength = length;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return best;
|
|
129
|
-
};
|
|
130
|
-
var autoApprove = (tool) => ({
|
|
131
|
-
tool,
|
|
132
|
-
timeoutSeconds: 0,
|
|
133
|
-
timeoutAction: "approve",
|
|
134
|
-
mode: "terminal_only",
|
|
135
|
-
pushFirstSeconds: 0
|
|
136
|
-
});
|
|
137
|
-
var resolveAutoResolveOrigin = (config, toolName, toolInput) => {
|
|
138
|
-
const arg = toolInput ? extractPolicyArg(toolName, toolInput) : void 0;
|
|
139
|
-
const match = findBestMatch(config.policies, toolName, arg);
|
|
140
|
-
const governedBySpecificRule = match?.rank === "exact" || match?.rank === "prefix";
|
|
141
|
-
if (!governedBySpecificRule && toolName === "Bash" && typeof arg === "string" && isSafeReadOnlyCommand(arg)) {
|
|
142
|
-
return "safe_readonly";
|
|
143
|
-
}
|
|
144
|
-
return "policy_timeout";
|
|
145
|
-
};
|
|
146
|
-
var resolvePolicy = (config, toolName, modeOverride, toolInput) => {
|
|
147
|
-
const arg = toolInput ? extractPolicyArg(toolName, toolInput) : void 0;
|
|
148
|
-
const match = findBestMatch(config.policies, toolName, arg);
|
|
149
|
-
let base = match?.policy ?? config.policies.find((p) => p.tool === "*") ?? {
|
|
150
|
-
tool: toolName,
|
|
151
|
-
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
152
|
-
timeoutAction: config.defaultTimeoutAction,
|
|
153
|
-
mode: config.defaultMode ?? "push_first",
|
|
154
|
-
pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
|
|
155
|
-
};
|
|
156
|
-
const governedBySpecificRule = match?.rank === "exact" || match?.rank === "prefix";
|
|
157
|
-
if (!governedBySpecificRule && toolName === "Bash" && typeof arg === "string" && isSafeReadOnlyCommand(arg)) {
|
|
158
|
-
base = autoApprove(base.tool);
|
|
159
|
-
}
|
|
160
|
-
const effectiveOverride = modeOverride ?? config.modeOverride;
|
|
161
|
-
if (effectiveOverride) {
|
|
162
|
-
return { ...base, mode: effectiveOverride };
|
|
163
|
-
}
|
|
164
|
-
return base;
|
|
165
|
-
};
|
|
166
|
-
var toPolicyVersion = (value) => typeof value === "string" || typeof value === "number" ? String(value) : null;
|
|
167
|
-
var fetchModeState = async (apiKey, sessionId) => {
|
|
168
|
-
try {
|
|
169
|
-
const baseUrl = getBaseUrl();
|
|
170
|
-
const url = sessionId ? `${baseUrl}/api/mcp/mode?session=${encodeURIComponent(sessionId)}` : `${baseUrl}/api/mcp/mode`;
|
|
171
|
-
const response = await fetch(url, {
|
|
172
|
-
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
173
|
-
signal: AbortSignal.timeout(3e3)
|
|
174
|
-
});
|
|
175
|
-
if (!response.ok) return { mode: null, kill: false, policyVersion: null };
|
|
176
|
-
const data = await response.json();
|
|
177
|
-
const mode = data.override?.mode;
|
|
178
|
-
return {
|
|
179
|
-
mode: isApprovalMode(mode) ? mode : null,
|
|
180
|
-
kill: data.kill === true,
|
|
181
|
-
policyVersion: toPolicyVersion(data.policyVersion)
|
|
182
|
-
};
|
|
183
|
-
} catch {
|
|
184
|
-
return { mode: null, kill: false, policyVersion: null };
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
var fetchModeOverride = async (apiKey) => (await fetchModeState(apiKey)).mode;
|
|
188
|
-
|
|
189
|
-
// src/describe.ts
|
|
190
|
-
import { isAbsolute, relative } from "path";
|
|
191
|
-
var hookPrefixes = {
|
|
192
|
-
Bash: (input) => `bash: ${input.command ?? "(no command)"}`,
|
|
193
|
-
PowerShell: (input) => `powershell: ${input.command ?? "(no command)"}`,
|
|
194
|
-
Monitor: (input) => `monitor: ${input.command ?? "(no command)"}`,
|
|
195
|
-
Write: (input) => `write file: ${input.file_path ?? "(unknown path)"}`,
|
|
196
|
-
Edit: (input) => `edit file: ${input.file_path ?? "(unknown path)"}`,
|
|
197
|
-
MultiEdit: (input) => `edit file: ${input.file_path ?? "(unknown path)"}`,
|
|
198
|
-
Read: (input) => `read file: ${input.file_path ?? "(unknown path)"}`
|
|
199
|
-
};
|
|
200
|
-
var describeTodoProgress = (todos) => {
|
|
201
|
-
if (!Array.isArray(todos)) return "updated the plan";
|
|
202
|
-
const items = todos.filter((t) => !!t && typeof t === "object");
|
|
203
|
-
const total = items.length;
|
|
204
|
-
if (total === 0) return "updated the plan";
|
|
205
|
-
const done = items.filter((t) => t.status === "completed").length;
|
|
206
|
-
const current = items.find((t) => t.status === "in_progress");
|
|
207
|
-
const label = current ? [current.activeForm, current.content].find((v) => typeof v === "string" && v.trim().length > 0) : void 0;
|
|
208
|
-
if (done >= total) return `finished all ${total} tasks`;
|
|
209
|
-
if (label) return `${label.trim()} (${done}/${total} done)`;
|
|
210
|
-
return `${done}/${total} tasks done`;
|
|
211
|
-
};
|
|
212
|
-
var eventPrefixes = {
|
|
213
|
-
Bash: (input) => `ran: ${String(input.command ?? "")}`,
|
|
214
|
-
PowerShell: (input) => `ran: ${String(input.command ?? "")}`,
|
|
215
|
-
Monitor: (input) => `ran: ${String(input.command ?? "")}`,
|
|
216
|
-
Write: (input) => `wrote: ${input.file_path ?? "unknown"}`,
|
|
217
|
-
Edit: (input) => `edited: ${input.file_path ?? "unknown"}`,
|
|
218
|
-
MultiEdit: (input) => `edited: ${input.file_path ?? "unknown"}`,
|
|
219
|
-
Read: (input) => `read: ${input.file_path ?? "unknown"}`,
|
|
220
|
-
TodoWrite: (input) => describeTodoProgress(input.todos)
|
|
221
|
-
};
|
|
222
|
-
var EVENT_ACTION_MAX = 120;
|
|
223
|
-
var HOOK_FALLBACK_MAX = 200;
|
|
224
|
-
var describeToolCall = (toolName, toolInput, format = "hook") => {
|
|
225
|
-
const prefixes = format === "hook" ? hookPrefixes : eventPrefixes;
|
|
226
|
-
const builder = prefixes[toolName];
|
|
227
|
-
const raw = builder ? builder(toolInput) : format === "hook" ? `${toolName}: ${JSON.stringify(toolInput)}` : `${toolName}: done`;
|
|
228
|
-
const redacted = redactSecrets(raw);
|
|
229
|
-
if (format === "event") return redacted.slice(0, EVENT_ACTION_MAX);
|
|
230
|
-
return builder ? redacted : redacted.slice(0, HOOK_FALLBACK_MAX);
|
|
231
|
-
};
|
|
232
|
-
var TOOL_TARGET_MAX_LENGTH = 80;
|
|
233
|
-
var deriveCommandHead = (command) => {
|
|
234
|
-
if (typeof command !== "string") return void 0;
|
|
235
|
-
const head = command.trim().split(/\s+/).slice(0, 2).join(" ");
|
|
236
|
-
return head ? head.slice(0, TOOL_TARGET_MAX_LENGTH) : void 0;
|
|
237
|
-
};
|
|
238
|
-
var deriveToolTarget = (toolName, toolInput) => {
|
|
239
|
-
if (toolName === "Bash" || toolName === "PowerShell" || toolName === "Monitor") {
|
|
240
|
-
return deriveCommandHead(toolInput.command);
|
|
241
|
-
}
|
|
242
|
-
if (toolName === "Edit" || toolName === "Write" || toolName === "MultiEdit") {
|
|
243
|
-
const filePath = toolInput.file_path;
|
|
244
|
-
if (typeof filePath !== "string") return void 0;
|
|
245
|
-
const separator = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
|
|
246
|
-
const base = filePath.slice(separator + 1);
|
|
247
|
-
const dot = base.lastIndexOf(".");
|
|
248
|
-
if (dot <= 0) return void 0;
|
|
249
|
-
return base.slice(dot).slice(0, TOOL_TARGET_MAX_LENGTH);
|
|
250
|
-
}
|
|
251
|
-
return void 0;
|
|
252
|
-
};
|
|
253
|
-
var RECEIPT_TARGET_MAX_LENGTH = 256;
|
|
254
|
-
var isToolResultError = (toolResult) => {
|
|
255
|
-
try {
|
|
256
|
-
return Boolean(toolResult && ("error" in toolResult || "is_error" in toolResult));
|
|
257
|
-
} catch {
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
var relativizeReceiptPath = (filePath, cwd) => {
|
|
262
|
-
if (!cwd || !isAbsolute(filePath)) return filePath;
|
|
263
|
-
return relative(cwd, filePath);
|
|
264
|
-
};
|
|
265
|
-
var deriveReceiptMeta = (toolName, toolInput, toolResult, cwd) => {
|
|
266
|
-
try {
|
|
267
|
-
const ok = !isToolResultError(toolResult);
|
|
268
|
-
if (toolName === "Edit" || toolName === "Write" || toolName === "MultiEdit") {
|
|
269
|
-
const filePath = toolInput.file_path;
|
|
270
|
-
if (typeof filePath !== "string" || !filePath) return void 0;
|
|
271
|
-
return {
|
|
272
|
-
kind: toolName === "Write" ? "write" : "edit",
|
|
273
|
-
target: relativizeReceiptPath(filePath, cwd).slice(0, RECEIPT_TARGET_MAX_LENGTH),
|
|
274
|
-
ok
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
if (toolName === "Bash" || toolName === "PowerShell" || toolName === "Monitor") {
|
|
278
|
-
const head = deriveCommandHead(toolInput.command);
|
|
279
|
-
if (!head) return void 0;
|
|
280
|
-
return {
|
|
281
|
-
kind: head === "git commit" ? "commit" : "bash",
|
|
282
|
-
target: head,
|
|
283
|
-
ok
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
return void 0;
|
|
287
|
-
} catch {
|
|
288
|
-
return void 0;
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
var firstString = (...values) => values.find((v) => typeof v === "string" && v.length > 0);
|
|
292
|
-
var ACTION_BODY_TRUNCATION_MARKER = "\n\u2026 [truncated]";
|
|
293
|
-
var capActionBody = (text) => text.length <= ACTION_BODY_MAX ? text : `${text.slice(0, ACTION_BODY_MAX - ACTION_BODY_TRUNCATION_MARKER.length)}${ACTION_BODY_TRUNCATION_MARKER}`;
|
|
294
|
-
var rawActionBody = (toolName, toolInput) => {
|
|
295
|
-
if (toolName === "Edit" || toolName === "replace") {
|
|
296
|
-
const before = firstString(toolInput.old_string);
|
|
297
|
-
const after = firstString(toolInput.new_string);
|
|
298
|
-
if (before === void 0 && after === void 0) return void 0;
|
|
299
|
-
return `- ${before ?? ""}
|
|
300
|
-
+ ${after ?? ""}`;
|
|
301
|
-
}
|
|
302
|
-
if (toolName === "Write" || toolName === "write_file") return firstString(toolInput.content);
|
|
303
|
-
if (toolName === "Bash" || toolName === "PowerShell" || toolName === "Monitor" || toolName === "run_shell_command" || toolName === "shell" || toolName === "apply_patch") {
|
|
304
|
-
return firstString(toolInput.command);
|
|
305
|
-
}
|
|
306
|
-
return void 0;
|
|
307
|
-
};
|
|
308
|
-
var deriveActionBody = (toolName, toolInput) => {
|
|
309
|
-
try {
|
|
310
|
-
const raw = rawActionBody(toolName, toolInput);
|
|
311
|
-
if (!raw) return void 0;
|
|
312
|
-
return capActionBody(redactSecretsDeep(raw));
|
|
313
|
-
} catch {
|
|
314
|
-
return void 0;
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
var deriveAction = (toolName, toolInput) => describeToolCall(toolName, toolInput, "hook").slice(0, DECISION_LINE_MAX);
|
|
318
|
-
var BLOCKER_BY_MODE = {
|
|
319
|
-
push_only: "Approval required before this runs",
|
|
320
|
-
push_first: "Paused for your approval before continuing",
|
|
321
|
-
escalate: "Escalated to you after the approval timeout"
|
|
322
|
-
};
|
|
323
|
-
var deriveBlocker = (mode, reason) => {
|
|
324
|
-
const text = reason?.replace(/\s+/g, " ").trim() || BLOCKER_BY_MODE[mode] || "Waiting for your approval";
|
|
325
|
-
return text.slice(0, DECISION_LINE_MAX);
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
// src/identity.ts
|
|
329
|
-
import { createHash as createHash2 } from "crypto";
|
|
330
|
-
import { hostname } from "os";
|
|
331
|
-
var deriveMachineId = (host) => createHash2("sha256").update(host).digest("hex").slice(0, 8);
|
|
332
|
-
var getMachineId = () => deriveMachineId(hostname());
|
|
333
|
-
|
|
334
|
-
// src/pending.ts
|
|
335
|
-
import { join as join2 } from "path";
|
|
336
|
-
import { tmpdir as tmpdir2 } from "os";
|
|
337
|
-
import { existsSync as existsSync2, mkdirSync, writeFileSync as writeFileSync2, readdirSync, unlinkSync, rmSync, statSync } from "fs";
|
|
338
|
-
var PENDING_DIR = join2(tmpdir2(), "pushary-pending");
|
|
26
|
+
import { existsSync, mkdirSync, writeFileSync, readdirSync, unlinkSync, rmSync, statSync } from "fs";
|
|
27
|
+
var PENDING_DIR = join(tmpdir(), "pushary-pending");
|
|
339
28
|
var DEFAULT_SESSION = "_no_session";
|
|
340
29
|
var GRACE_MS = 10 * 60 * 1e3;
|
|
341
30
|
var sanitize = (sessionId) => sessionId.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 128) || DEFAULT_SESSION;
|
|
342
|
-
var dirFor = (sessionId) =>
|
|
31
|
+
var dirFor = (sessionId) => join(PENDING_DIR, sanitize(sessionId));
|
|
343
32
|
var isDefaultSession = (sessionId) => sanitize(sessionId) === DEFAULT_SESSION;
|
|
344
33
|
var savePendingQuestion = (sessionId, correlationId) => {
|
|
345
34
|
const dir = dirFor(sessionId);
|
|
346
|
-
if (!
|
|
347
|
-
|
|
35
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
36
|
+
writeFileSync(join(dir, correlationId), "", "utf-8");
|
|
348
37
|
};
|
|
349
38
|
var listPendingQuestions = (sessionId) => {
|
|
350
39
|
const dir = dirFor(sessionId);
|
|
@@ -358,7 +47,7 @@ var listPendingQuestions = (sessionId) => {
|
|
|
358
47
|
const cutoff = Date.now() - GRACE_MS;
|
|
359
48
|
return files.filter((name) => {
|
|
360
49
|
try {
|
|
361
|
-
return statSync(
|
|
50
|
+
return statSync(join(dir, name)).mtimeMs < cutoff;
|
|
362
51
|
} catch {
|
|
363
52
|
return false;
|
|
364
53
|
}
|
|
@@ -366,7 +55,7 @@ var listPendingQuestions = (sessionId) => {
|
|
|
366
55
|
};
|
|
367
56
|
var removePendingQuestion = (sessionId, correlationId) => {
|
|
368
57
|
try {
|
|
369
|
-
unlinkSync(
|
|
58
|
+
unlinkSync(join(dirFor(sessionId), correlationId));
|
|
370
59
|
} catch {
|
|
371
60
|
}
|
|
372
61
|
};
|
|
@@ -378,9 +67,9 @@ var removePendingSession = (sessionId) => {
|
|
|
378
67
|
};
|
|
379
68
|
|
|
380
69
|
// src/usage.ts
|
|
381
|
-
import { closeSync, mkdirSync as mkdirSync2, openSync, readFileSync
|
|
382
|
-
import { join as
|
|
383
|
-
import { tmpdir as
|
|
70
|
+
import { closeSync, mkdirSync as mkdirSync2, openSync, readFileSync, readSync, statSync as statSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
71
|
+
import { join as join2 } from "path";
|
|
72
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
384
73
|
var DEFAULT_PRICES = [
|
|
385
74
|
{ match: "opus-4-1", in: 15, out: 75 },
|
|
386
75
|
{ match: "opus-4-0", in: 15, out: 75 },
|
|
@@ -415,12 +104,12 @@ var estimateCostUsd = (usage, model) => {
|
|
|
415
104
|
const perTokenOut = price.out / 1e6;
|
|
416
105
|
return usage.inputTokens * perTokenIn + usage.outputTokens * perTokenOut + usage.cacheCreationTokens * perTokenIn * CACHE_WRITE_MULTIPLIER + usage.cacheReadTokens * perTokenIn * CACHE_READ_MULTIPLIER;
|
|
417
106
|
};
|
|
418
|
-
var stateDir = () => process.env.PUSHARY_USAGE_DIR?.trim() ||
|
|
419
|
-
var stateFile = (sessionId) =>
|
|
107
|
+
var stateDir = () => process.env.PUSHARY_USAGE_DIR?.trim() || join2(tmpdir2(), "pushary-usage");
|
|
108
|
+
var stateFile = (sessionId) => join2(stateDir(), sessionId.replace(/[^a-zA-Z0-9_-]/g, "_"));
|
|
420
109
|
var emptyState = () => ({ offset: 0, tokensIn: 0, tokensOut: 0, costUsd: 0, recentIds: [] });
|
|
421
110
|
var readState = (path) => {
|
|
422
111
|
try {
|
|
423
|
-
const parsed = JSON.parse(
|
|
112
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
424
113
|
if (typeof parsed.offset === "number" && parsed.offset >= 0 && typeof parsed.tokensIn === "number" && typeof parsed.tokensOut === "number" && typeof parsed.costUsd === "number" && Array.isArray(parsed.recentIds)) {
|
|
425
114
|
return {
|
|
426
115
|
offset: parsed.offset,
|
|
@@ -535,7 +224,7 @@ var readNewUsage = (transcriptPath, sessionId) => {
|
|
|
535
224
|
}
|
|
536
225
|
}
|
|
537
226
|
mkdirSync2(stateDir(), { recursive: true });
|
|
538
|
-
|
|
227
|
+
writeFileSync2(path, JSON.stringify(state), "utf-8");
|
|
539
228
|
if (state.tokensIn === 0 && state.tokensOut === 0) return null;
|
|
540
229
|
return {
|
|
541
230
|
tokensIn: state.tokensIn,
|
|
@@ -627,18 +316,18 @@ var permissionTimeoutDecision = (timeoutAction) => {
|
|
|
627
316
|
var preToolUseTimeoutDecision = (timeoutAction, denyReason = "No response within timeout") => timeoutAction === "deny" ? codexDeny(denyReason) : codexPass();
|
|
628
317
|
|
|
629
318
|
// src/throttle.ts
|
|
630
|
-
import { join as
|
|
631
|
-
import { tmpdir as
|
|
632
|
-
import { createHash
|
|
633
|
-
import { existsSync as
|
|
634
|
-
var THROTTLE_DIR =
|
|
635
|
-
var markerPath = (key) =>
|
|
319
|
+
import { join as join3 } from "path";
|
|
320
|
+
import { tmpdir as tmpdir3 } from "os";
|
|
321
|
+
import { createHash } from "crypto";
|
|
322
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync3, statSync as statSync3, writeFileSync as writeFileSync3 } from "fs";
|
|
323
|
+
var THROTTLE_DIR = join3(tmpdir3(), "pushary-throttle");
|
|
324
|
+
var markerPath = (key) => join3(THROTTLE_DIR, createHash("sha256").update(key).digest("hex").slice(0, 16));
|
|
636
325
|
var throttlePass = (key, windowMs) => {
|
|
637
326
|
try {
|
|
638
327
|
const path = markerPath(key);
|
|
639
|
-
if (
|
|
328
|
+
if (existsSync2(path) && Date.now() - statSync3(path).mtimeMs < windowMs) return false;
|
|
640
329
|
mkdirSync3(THROTTLE_DIR, { recursive: true });
|
|
641
|
-
|
|
330
|
+
writeFileSync3(path, "", "utf-8");
|
|
642
331
|
return true;
|
|
643
332
|
} catch {
|
|
644
333
|
return false;
|
|
@@ -646,21 +335,21 @@ var throttlePass = (key, windowMs) => {
|
|
|
646
335
|
};
|
|
647
336
|
|
|
648
337
|
// src/events.ts
|
|
649
|
-
import { basename, join as
|
|
650
|
-
import { createHash as
|
|
651
|
-
import { existsSync as
|
|
652
|
-
import { tmpdir as
|
|
653
|
-
var INTENT_DIR =
|
|
338
|
+
import { basename, join as join4 } from "path";
|
|
339
|
+
import { createHash as createHash2 } from "crypto";
|
|
340
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync4, readFileSync as readFileSync2, statSync as statSync4, writeFileSync as writeFileSync4 } from "fs";
|
|
341
|
+
import { tmpdir as tmpdir4 } from "os";
|
|
342
|
+
var INTENT_DIR = join4(tmpdir4(), "pushary-intent");
|
|
654
343
|
var INTENT_MAX_BYTES = 2048;
|
|
655
344
|
var INTENT_GRACE_MS = 10 * 60 * 1e3;
|
|
656
345
|
var sanitizeSession = (sessionId) => sessionId.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 128) || DEFAULT_SESSION;
|
|
657
|
-
var intentFile = (sessionId) =>
|
|
346
|
+
var intentFile = (sessionId) => join4(INTENT_DIR, sanitizeSession(sessionId));
|
|
658
347
|
var saveLastPrompt = (sessionId, prompt) => {
|
|
659
348
|
try {
|
|
660
349
|
const trimmed = prompt.replace(/\s+/g, " ").trim().slice(0, INTENT_MAX_BYTES);
|
|
661
350
|
if (!trimmed) return;
|
|
662
351
|
mkdirSync4(INTENT_DIR, { recursive: true });
|
|
663
|
-
|
|
352
|
+
writeFileSync4(intentFile(sessionId), trimmed, "utf-8");
|
|
664
353
|
} catch {
|
|
665
354
|
}
|
|
666
355
|
};
|
|
@@ -668,7 +357,7 @@ var readLastPrompt = (sessionId) => {
|
|
|
668
357
|
try {
|
|
669
358
|
const path = intentFile(sessionId);
|
|
670
359
|
if (isDefaultSession(sessionId) && statSync4(path).mtimeMs < Date.now() - INTENT_GRACE_MS) return void 0;
|
|
671
|
-
const value =
|
|
360
|
+
const value = readFileSync2(path, "utf-8").trim();
|
|
672
361
|
return value.length > 0 ? value : void 0;
|
|
673
362
|
} catch {
|
|
674
363
|
return void 0;
|
|
@@ -724,10 +413,10 @@ var notifyLateAnswers = async (apiKey, late, agentName, sessionId) => {
|
|
|
724
413
|
var CLAUDE_CODE_AGENT = { type: "claude_code", label: "Claude Code" };
|
|
725
414
|
var POLICY_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
726
415
|
var readFreshCachedPolicy = (apiKey) => {
|
|
727
|
-
const hash =
|
|
728
|
-
const path =
|
|
729
|
-
if (!
|
|
730
|
-
const cached = JSON.parse(
|
|
416
|
+
const hash = createHash2("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
417
|
+
const path = join4(tmpdir4(), `pushary-policy-${hash}.json`);
|
|
418
|
+
if (!existsSync3(path)) return null;
|
|
419
|
+
const cached = JSON.parse(readFileSync2(path, "utf-8"));
|
|
731
420
|
if (!isPolicyConfig(cached)) return null;
|
|
732
421
|
if (!cached._cachedAt || Date.now() - cached._cachedAt >= POLICY_CACHE_TTL_MS) return null;
|
|
733
422
|
return cached;
|
|
@@ -1084,20 +773,6 @@ var handleStopFailure = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
|
1084
773
|
};
|
|
1085
774
|
|
|
1086
775
|
export {
|
|
1087
|
-
askUser,
|
|
1088
|
-
waitForAnswer,
|
|
1089
|
-
cancelQuestion,
|
|
1090
|
-
sendNotification,
|
|
1091
|
-
getPolicy,
|
|
1092
|
-
resolvePolicy,
|
|
1093
|
-
fetchModeState,
|
|
1094
|
-
fetchModeOverride,
|
|
1095
|
-
describeToolCall,
|
|
1096
|
-
deriveToolTarget,
|
|
1097
|
-
deriveActionBody,
|
|
1098
|
-
deriveAction,
|
|
1099
|
-
deriveBlocker,
|
|
1100
|
-
getMachineId,
|
|
1101
776
|
DEFAULT_SESSION,
|
|
1102
777
|
savePendingQuestion,
|
|
1103
778
|
throttlePass,
|
|
@@ -7,6 +7,12 @@ import {
|
|
|
7
7
|
} from "./chunk-KQYIHZ5E.js";
|
|
8
8
|
import {
|
|
9
9
|
DEFAULT_SESSION,
|
|
10
|
+
readLastPrompt,
|
|
11
|
+
readLastUserPrompt,
|
|
12
|
+
savePendingQuestion,
|
|
13
|
+
throttlePass
|
|
14
|
+
} from "./chunk-NRCQ5UUR.js";
|
|
15
|
+
import {
|
|
10
16
|
askUser,
|
|
11
17
|
cancelQuestion,
|
|
12
18
|
deriveAction,
|
|
@@ -17,14 +23,10 @@ import {
|
|
|
17
23
|
fetchModeState,
|
|
18
24
|
getMachineId,
|
|
19
25
|
getPolicy,
|
|
20
|
-
readLastPrompt,
|
|
21
|
-
readLastUserPrompt,
|
|
22
26
|
resolvePolicy,
|
|
23
|
-
savePendingQuestion,
|
|
24
27
|
sendNotification,
|
|
25
|
-
throttlePass,
|
|
26
28
|
waitForAnswer
|
|
27
|
-
} from "./chunk-
|
|
29
|
+
} from "./chunk-ETDXSKR5.js";
|
|
28
30
|
import {
|
|
29
31
|
isGatingMoment,
|
|
30
32
|
recordKeylessMoment
|
package/dist/src/index.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ interface ModeState {
|
|
|
33
33
|
readonly mode: ApprovalMode | null;
|
|
34
34
|
readonly kill: boolean;
|
|
35
35
|
readonly policyVersion: string | null;
|
|
36
|
+
readonly relayUrl: string | null;
|
|
36
37
|
}
|
|
37
38
|
declare const fetchModeState: (apiKey: string, sessionId?: string) => Promise<ModeState>;
|
|
38
39
|
declare const fetchModeOverride: (apiKey: string) => Promise<ApprovalMode | null>;
|
package/dist/src/index.js
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
2
|
handlePreToolUse
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-V2TSLWTV.js";
|
|
4
4
|
import "../chunk-7EW3USQF.js";
|
|
5
5
|
import "../chunk-KQYIHZ5E.js";
|
|
6
|
+
import {
|
|
7
|
+
handleNotification,
|
|
8
|
+
handlePostToolUse,
|
|
9
|
+
handleStop,
|
|
10
|
+
reportEvent
|
|
11
|
+
} from "../chunk-NRCQ5UUR.js";
|
|
6
12
|
import {
|
|
7
13
|
askUser,
|
|
8
14
|
cancelQuestion,
|
|
9
15
|
fetchModeOverride,
|
|
10
16
|
fetchModeState,
|
|
11
17
|
getPolicy,
|
|
12
|
-
handleNotification,
|
|
13
|
-
handlePostToolUse,
|
|
14
|
-
handleStop,
|
|
15
|
-
reportEvent,
|
|
16
18
|
resolvePolicy,
|
|
17
19
|
waitForAnswer
|
|
18
|
-
} from "../chunk-
|
|
20
|
+
} from "../chunk-ETDXSKR5.js";
|
|
19
21
|
import "../chunk-R5AJNXZS.js";
|
|
20
22
|
import "../chunk-DWED7BS3.js";
|
|
21
23
|
import "../chunk-Z5PL3K7C.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pushary/agent-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.42.0",
|
|
4
4
|
"description": "Permission hooks for AI coding agents: route tool approvals through Pushary push notifications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pushary",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"scripts": {
|
|
73
73
|
"build": "node scripts/bundle-plugin.mjs && tsup",
|
|
74
74
|
"dev": "tsup --watch",
|
|
75
|
-
"test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/npm.test.ts && bun test src/identity.test.ts && bun test src/pending.test.ts && bun test src/events.test.ts && bun test src/describe.test.ts && bun test src/reapply.test.ts && bun test src/suggestions.test.ts && bun test src/safe-commands.test.ts && bun test src/hook.test.ts && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts && bun test src/gemini-adapter.test.ts && bun test src/gemini-config.test.ts && bun test src/instruction-file.test.ts && bun test src/pairing.test.ts && bun test src/wait-ladder.test.ts && bun test src/ledger.test.ts && bun test src/wrapper/wrapper.test.ts"
|
|
75
|
+
"test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/npm.test.ts && bun test src/identity.test.ts && bun test src/pending.test.ts && bun test src/events.test.ts && bun test src/describe.test.ts && bun test src/reapply.test.ts && bun test src/suggestions.test.ts && bun test src/safe-commands.test.ts && bun test src/hook.test.ts && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts && bun test src/gemini-adapter.test.ts && bun test src/gemini-config.test.ts && bun test src/instruction-file.test.ts && bun test src/pairing.test.ts && bun test src/wait-ladder.test.ts && bun test src/ledger.test.ts && bun test src/wrapper/wrapper.test.ts && bun test src/wrapper/messageQueue.test.ts && bun test src/wrapper/approver.test.ts && bun test src/wrapper/remoteLoop.test.ts && bun test src/wrapper/spawnClaude.test.ts && bun test src/wrapper/wsProtocol.test.ts && bun test src/wrapper/relayClient.test.ts"
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"@inquirer/prompts": "^8.4.2",
|