@pushary/agent-hooks 0.24.0 → 0.25.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-codex-hook.js +1 -1
- package/dist/bin/pushary-codex.js +1 -1
- package/dist/bin/pushary-gemini-hook.js +1 -1
- package/dist/bin/pushary-hook.js +2 -2
- package/dist/bin/pushary-post-hook.js +1 -1
- package/dist/bin/pushary-prompt-hook.js +1 -1
- package/dist/bin/pushary-setup.js +1 -1
- package/dist/bin/pushary-stop-hook.js +1 -1
- package/dist/chunk-22CV7V7A.js +38 -0
- package/dist/chunk-2BE2IPMO.js +315 -0
- package/dist/chunk-2CRLBMOX.js +244 -0
- package/dist/chunk-2HMNOZPY.js +109 -0
- package/dist/chunk-2I6DLXJN.js +219 -0
- package/dist/chunk-2Q6XPZ4Q.js +395 -0
- package/dist/{chunk-U4SSWRVM.js → chunk-3LC4YUMA.js} +1 -1
- package/dist/chunk-3MIR7ODJ.js +112 -0
- package/dist/chunk-3QZESA66.js +222 -0
- package/dist/chunk-4Z4MB37G.js +96 -0
- package/dist/chunk-5GFUI5N6.js +132 -0
- package/dist/chunk-5JEDLXEC.js +99 -0
- package/dist/chunk-5LTQNC7R.js +824 -0
- package/dist/chunk-5QHQVXWN.js +174 -0
- package/dist/chunk-6A3WCJO2.js +416 -0
- package/dist/chunk-6QWFMVCD.js +328 -0
- package/dist/chunk-6SRXMZAN.js +99 -0
- package/dist/chunk-7PTU7TGE.js +96 -0
- package/dist/chunk-AB4KX4XT.js +109 -0
- package/dist/chunk-AC4UYAGX.js +136 -0
- package/dist/chunk-ACE77TKQ.js +328 -0
- package/dist/chunk-BHAMEPOP.js +323 -0
- package/dist/chunk-BYNYYZWW.js +412 -0
- package/dist/chunk-C5TFTNHG.js +244 -0
- package/dist/chunk-CH53PBQN.js +265 -0
- package/dist/chunk-DF3BM6BF.js +195 -0
- package/dist/chunk-DKZVKEOW.js +175 -0
- package/dist/chunk-EMPL27ZV.js +96 -0
- package/dist/chunk-EQJXMEIR.js +175 -0
- package/dist/chunk-FXYTRZHC.js +177 -0
- package/dist/chunk-GTJGZBTQ.js +328 -0
- package/dist/{chunk-OQZH5PVA.js → chunk-GYPPVD3P.js} +32 -18
- package/dist/chunk-HJMFYDWY.js +222 -0
- package/dist/chunk-HRQEECB6.js +315 -0
- package/dist/chunk-HWEMAAOY.js +370 -0
- package/dist/chunk-IBWCHA5M.js +10 -0
- package/dist/chunk-KTM4ZQ3W.js +96 -0
- package/dist/chunk-KTP2EPVB.js +27 -0
- package/dist/chunk-KYARP7KP.js +97 -0
- package/dist/chunk-M2N5DYWN.js +247 -0
- package/dist/chunk-M5F2L4KF.js +177 -0
- package/dist/chunk-M6S3M6E3.js +244 -0
- package/dist/chunk-N7VXDBQU.js +6 -0
- package/dist/chunk-O2QL6U6G.js +194 -0
- package/dist/chunk-O6A5RHWY.js +122 -0
- package/dist/chunk-ODUXELPM.js +219 -0
- package/dist/chunk-OF5WIOYS.js +129 -0
- package/dist/chunk-OP4QAN23.js +99 -0
- package/dist/chunk-P4JH2Q7Z.js +102 -0
- package/dist/chunk-PMD5JSV3.js +242 -0
- package/dist/chunk-PSSHNBEL.js +177 -0
- package/dist/chunk-QEPRH7JB.js +315 -0
- package/dist/chunk-QRXWPZKN.js +308 -0
- package/dist/chunk-QY4L6XHN.js +384 -0
- package/dist/chunk-RNWPCELY.js +176 -0
- package/dist/chunk-RSHN2AQ7.js +29 -0
- package/dist/chunk-RZLVE57X.js +38 -0
- package/dist/chunk-SDCIKREA.js +241 -0
- package/dist/chunk-SDREQWNI.js +175 -0
- package/dist/chunk-SH26ZOHU.js +159 -0
- package/dist/chunk-TRLBBLSS.js +176 -0
- package/dist/chunk-U6OXZPYC.js +384 -0
- package/dist/chunk-UIBTFZUD.js +314 -0
- package/dist/chunk-USUCPCUC.js +156 -0
- package/dist/chunk-V4GLWPD7.js +174 -0
- package/dist/chunk-VC6U3QGF.js +142 -0
- package/dist/chunk-VIST7ACL.js +88 -0
- package/dist/chunk-VUNL35KE.js +16 -0
- package/dist/chunk-VWBNI4SC.js +174 -0
- package/dist/chunk-W5KRWUNE.js +262 -0
- package/dist/chunk-WCGKLHCL.js +154 -0
- package/dist/chunk-WNXGIEX7.js +219 -0
- package/dist/chunk-XPVSZIA3.js +315 -0
- package/dist/chunk-Y633ZIJF.js +384 -0
- package/dist/chunk-YCL4GUFR.js +222 -0
- package/dist/chunk-YTMKB44I.js +220 -0
- package/dist/chunk-ZWBS3T7Q.js +244 -0
- package/dist/pushary-mode-T7XOPI6Z.js +83 -0
- package/dist/src/index.js +2 -2
- package/package.json +1 -1
|
@@ -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, timeoutMs = 15e3) => {
|
|
35
|
+
const result = await callMcpTool(apiKey, "ask_user", { ...params, wait: false }, { maxRetries: 3, timeoutMs });
|
|
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
|
+
};
|
|
@@ -156,6 +156,30 @@ var API_KEY_PATTERN = /^pk_[a-f0-9]+\.[a-f0-9]+$/;
|
|
|
156
156
|
var isValidApiKey = (value) => API_KEY_PATTERN.test(value);
|
|
157
157
|
var ACTION_BODY_MAX = 4e3;
|
|
158
158
|
var DECISION_LINE_MAX = 500;
|
|
159
|
+
var SECRET_REDACTION_RULES = [
|
|
160
|
+
{ pattern: /-----BEGIN[A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END[A-Z0-9 ]*PRIVATE KEY-----/g, replacement: "[redacted key]" },
|
|
161
|
+
{ pattern: /\bsk-[A-Za-z0-9_-]{16,}\b/g, replacement: "[redacted]" },
|
|
162
|
+
{ pattern: /\b[spr]k_(?:live|test)_[A-Za-z0-9]{8,}\b/g, replacement: "[redacted]" },
|
|
163
|
+
{ pattern: /\bwhsec_[A-Za-z0-9]{16,}\b/g, replacement: "[redacted]" },
|
|
164
|
+
{ pattern: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\b/g, replacement: "[redacted]" },
|
|
165
|
+
{ pattern: /\bgithub_pat_[A-Za-z0-9_]{22,}\b/g, replacement: "[redacted]" },
|
|
166
|
+
{ pattern: /\bglpat-[A-Za-z0-9_-]{20,}\b/g, replacement: "[redacted]" },
|
|
167
|
+
{ pattern: /\bxox[baprs]-[A-Za-z0-9-]{10,}\b/g, replacement: "[redacted]" },
|
|
168
|
+
{ pattern: /\bAIza[A-Za-z0-9_-]{35}\b/g, replacement: "[redacted]" },
|
|
169
|
+
{ pattern: /\bAKIA[0-9A-Z]{16}\b/g, replacement: "[redacted]" },
|
|
170
|
+
{ pattern: /\bnpm_[A-Za-z0-9]{36}\b/g, replacement: "[redacted]" },
|
|
171
|
+
{ pattern: /\bxai-[A-Za-z0-9]{16,}\b/g, replacement: "[redacted]" },
|
|
172
|
+
{ pattern: /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g, replacement: "[redacted]" },
|
|
173
|
+
{ pattern: /\bbearer\s+[A-Za-z0-9._~+/=-]+/gi, replacement: "bearer [redacted]" },
|
|
174
|
+
{ pattern: /\bauthorization:\s*\S+/gi, replacement: "authorization: [redacted]" },
|
|
175
|
+
{
|
|
176
|
+
pattern: /((?:secret|token|password|passwd|api[_-]?key|access[_-]?key|client[_-]?secret|private[_-]?key)\s*[=:]\s*)("[^"]*"|'[^']*'|\S+)/gi,
|
|
177
|
+
replacement: "$1[redacted]"
|
|
178
|
+
}
|
|
179
|
+
];
|
|
180
|
+
var HIGH_ENTROPY_RULE = { pattern: /[A-Za-z0-9+/]{40,}={0,2}/g, replacement: "[redacted]" };
|
|
181
|
+
var redactSecrets = (text) => SECRET_REDACTION_RULES.reduce((acc, rule) => acc.replace(rule.pattern, rule.replacement), text);
|
|
182
|
+
var redactSecretsDeep = (text) => redactSecrets(text).replace(HIGH_ENTROPY_RULE.pattern, HIGH_ENTROPY_RULE.replacement);
|
|
159
183
|
|
|
160
184
|
// src/validate.ts
|
|
161
185
|
var isPolicyConfig = (data) => {
|
|
@@ -334,16 +358,20 @@ var hookPrefixes = {
|
|
|
334
358
|
Read: (input) => `read file: ${input.file_path ?? "(unknown path)"}`
|
|
335
359
|
};
|
|
336
360
|
var eventPrefixes = {
|
|
337
|
-
Bash: (input) => `ran: ${String(input.command ?? "")
|
|
361
|
+
Bash: (input) => `ran: ${String(input.command ?? "")}`,
|
|
338
362
|
Write: (input) => `wrote: ${input.file_path ?? "unknown"}`,
|
|
339
363
|
Edit: (input) => `edited: ${input.file_path ?? "unknown"}`,
|
|
340
364
|
Read: (input) => `read: ${input.file_path ?? "unknown"}`
|
|
341
365
|
};
|
|
366
|
+
var EVENT_ACTION_MAX = 120;
|
|
367
|
+
var HOOK_FALLBACK_MAX = 200;
|
|
342
368
|
var describeToolCall = (toolName, toolInput, format = "hook") => {
|
|
343
369
|
const prefixes = format === "hook" ? hookPrefixes : eventPrefixes;
|
|
344
370
|
const builder = prefixes[toolName];
|
|
345
|
-
|
|
346
|
-
|
|
371
|
+
const raw = builder ? builder(toolInput) : format === "hook" ? `${toolName}: ${JSON.stringify(toolInput)}` : `${toolName}: done`;
|
|
372
|
+
const redacted = redactSecrets(raw);
|
|
373
|
+
if (format === "event") return redacted.slice(0, EVENT_ACTION_MAX);
|
|
374
|
+
return builder ? redacted : redacted.slice(0, HOOK_FALLBACK_MAX);
|
|
347
375
|
};
|
|
348
376
|
var TOOL_TARGET_MAX_LENGTH = 80;
|
|
349
377
|
var deriveCommandHead = (command) => {
|
|
@@ -405,20 +433,6 @@ var deriveReceiptMeta = (toolName, toolInput, toolResult, cwd) => {
|
|
|
405
433
|
}
|
|
406
434
|
};
|
|
407
435
|
var firstString = (...values) => values.find((v) => typeof v === "string" && v.length > 0);
|
|
408
|
-
var REDACTION_RULES = [
|
|
409
|
-
{ pattern: /\bsk-[A-Za-z0-9]{20,}\b/g, replacement: "[redacted]" },
|
|
410
|
-
{ pattern: /\bpk_(?:live|test)_[A-Za-z0-9]+\b/g, replacement: "[redacted]" },
|
|
411
|
-
{ pattern: /\brk_[A-Za-z0-9]+\b/g, replacement: "[redacted]" },
|
|
412
|
-
{ pattern: /\bAKIA[0-9A-Z]{16}\b/g, replacement: "[redacted]" },
|
|
413
|
-
{ pattern: /\bbearer\s+[A-Za-z0-9._-]+/gi, replacement: "bearer [redacted]" },
|
|
414
|
-
{ pattern: /\bauthorization:\s*\S+/gi, replacement: "authorization: [redacted]" },
|
|
415
|
-
{
|
|
416
|
-
pattern: /((?:secret|token|password|passwd|api[_-]?key|private[_-]?key)\s*[=:]\s*)(\S+)/gi,
|
|
417
|
-
replacement: "$1[redacted]"
|
|
418
|
-
},
|
|
419
|
-
{ pattern: /[A-Za-z0-9+/]{40,}={0,2}/g, replacement: "[redacted]" }
|
|
420
|
-
];
|
|
421
|
-
var redactSecrets = (text) => REDACTION_RULES.reduce((acc, rule) => acc.replace(rule.pattern, rule.replacement), text);
|
|
422
436
|
var ACTION_BODY_TRUNCATION_MARKER = "\n\u2026 [truncated]";
|
|
423
437
|
var capActionBody = (text) => text.length <= ACTION_BODY_MAX ? text : `${text.slice(0, ACTION_BODY_MAX - ACTION_BODY_TRUNCATION_MARKER.length)}${ACTION_BODY_TRUNCATION_MARKER}`;
|
|
424
438
|
var rawActionBody = (toolName, toolInput) => {
|
|
@@ -439,7 +453,7 @@ var deriveActionBody = (toolName, toolInput) => {
|
|
|
439
453
|
try {
|
|
440
454
|
const raw = rawActionBody(toolName, toolInput);
|
|
441
455
|
if (!raw) return void 0;
|
|
442
|
-
return capActionBody(
|
|
456
|
+
return capActionBody(redactSecretsDeep(raw));
|
|
443
457
|
} catch {
|
|
444
458
|
return void 0;
|
|
445
459
|
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_SESSION,
|
|
3
|
+
cancelQuestion,
|
|
4
|
+
deriveReceiptMeta,
|
|
5
|
+
describeToolCall,
|
|
6
|
+
fetchModeState,
|
|
7
|
+
getMachineId,
|
|
8
|
+
isDefaultSession,
|
|
9
|
+
isPolicyConfig,
|
|
10
|
+
isToolResultError,
|
|
11
|
+
listPendingQuestions,
|
|
12
|
+
removePendingQuestion,
|
|
13
|
+
removePendingSession,
|
|
14
|
+
resolvePolicy
|
|
15
|
+
} from "./chunk-XPVSZIA3.js";
|
|
16
|
+
import {
|
|
17
|
+
withRetry
|
|
18
|
+
} from "./chunk-3MIR7ODJ.js";
|
|
19
|
+
import {
|
|
20
|
+
getApiKey,
|
|
21
|
+
getBaseUrl
|
|
22
|
+
} from "./chunk-VUNL35KE.js";
|
|
23
|
+
|
|
24
|
+
// src/codex-adapter.ts
|
|
25
|
+
var CODEX_AGENT = { type: "codex", label: "Codex" };
|
|
26
|
+
var codexAllow = () => ({ kind: "allow" });
|
|
27
|
+
var codexDeny = (reason) => ({ kind: "deny", reason });
|
|
28
|
+
var codexPass = () => ({ kind: "pass" });
|
|
29
|
+
var toCodexWire = (event, decision) => {
|
|
30
|
+
if (event === "PermissionRequest") {
|
|
31
|
+
if (decision.kind === "allow") {
|
|
32
|
+
return {
|
|
33
|
+
hookSpecificOutput: {
|
|
34
|
+
hookEventName: "PermissionRequest",
|
|
35
|
+
decision: { behavior: "allow" }
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (decision.kind === "deny") {
|
|
40
|
+
return {
|
|
41
|
+
hookSpecificOutput: {
|
|
42
|
+
hookEventName: "PermissionRequest",
|
|
43
|
+
decision: { behavior: "deny", message: decision.reason }
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (event === "PreToolUse" && decision.kind === "deny") {
|
|
50
|
+
return {
|
|
51
|
+
hookSpecificOutput: {
|
|
52
|
+
hookEventName: "PreToolUse",
|
|
53
|
+
permissionDecision: "deny",
|
|
54
|
+
permissionDecisionReason: decision.reason
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
};
|
|
60
|
+
var toPolicyLookup = (toolName, toolInput) => {
|
|
61
|
+
if (toolName !== "apply_patch") return { tool: toolName, input: toolInput };
|
|
62
|
+
const command = toolInput.command;
|
|
63
|
+
return {
|
|
64
|
+
tool: "Edit",
|
|
65
|
+
input: typeof command === "string" ? { file_path: command } : {}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
var permissionTimeoutDecision = (timeoutAction) => {
|
|
69
|
+
if (timeoutAction === "approve") return codexAllow();
|
|
70
|
+
if (timeoutAction === "deny") return codexDeny("No response within timeout");
|
|
71
|
+
return codexPass();
|
|
72
|
+
};
|
|
73
|
+
var preToolUseTimeoutDecision = (timeoutAction, denyReason = "No response within timeout") => timeoutAction === "deny" ? codexDeny(denyReason) : codexPass();
|
|
74
|
+
|
|
75
|
+
// src/events.ts
|
|
76
|
+
import { basename, join } from "path";
|
|
77
|
+
import { createHash } from "crypto";
|
|
78
|
+
import { existsSync, readFileSync } from "fs";
|
|
79
|
+
import { tmpdir } from "os";
|
|
80
|
+
var cleanupPendingQuestions = async (sessionId) => {
|
|
81
|
+
try {
|
|
82
|
+
const files = listPendingQuestions(sessionId);
|
|
83
|
+
const apiKey = getApiKey();
|
|
84
|
+
for (const correlationId of files) {
|
|
85
|
+
try {
|
|
86
|
+
await cancelQuestion(apiKey, correlationId);
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
removePendingQuestion(sessionId, correlationId);
|
|
90
|
+
}
|
|
91
|
+
if (!isDefaultSession(sessionId)) removePendingSession(sessionId);
|
|
92
|
+
} catch {
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
var CLAUDE_CODE_AGENT = { type: "claude_code", label: "Claude Code" };
|
|
96
|
+
var POLICY_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
97
|
+
var readFreshCachedPolicy = (apiKey) => {
|
|
98
|
+
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
99
|
+
const path = join(tmpdir(), `pushary-policy-${hash}.json`);
|
|
100
|
+
if (!existsSync(path)) return null;
|
|
101
|
+
const cached = JSON.parse(readFileSync(path, "utf-8"));
|
|
102
|
+
if (!isPolicyConfig(cached)) return null;
|
|
103
|
+
if (!cached._cachedAt || Date.now() - cached._cachedAt >= POLICY_CACHE_TTL_MS) return null;
|
|
104
|
+
return cached;
|
|
105
|
+
};
|
|
106
|
+
var deriveDecisionSource = (toolName, toolInput, liveMode) => {
|
|
107
|
+
try {
|
|
108
|
+
if (liveMode.kill) return "terminal";
|
|
109
|
+
const policy = readFreshCachedPolicy(getApiKey());
|
|
110
|
+
if (!policy) return void 0;
|
|
111
|
+
const resolved = resolvePolicy(policy, toolName, liveMode.mode, toolInput);
|
|
112
|
+
if (resolved.timeoutSeconds === 0 && resolved.timeoutAction === "approve") return "policy_auto";
|
|
113
|
+
if (resolved.mode === "push_only" || resolved.mode === "push_first") return "human";
|
|
114
|
+
return "terminal";
|
|
115
|
+
} catch {
|
|
116
|
+
return void 0;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var reportEvent = async (event, options = {}) => {
|
|
120
|
+
const apiKey = getApiKey();
|
|
121
|
+
const baseUrl = getBaseUrl();
|
|
122
|
+
await withRetry(async () => {
|
|
123
|
+
await fetch(`${baseUrl}/api/agent/event`, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
headers: {
|
|
126
|
+
"Content-Type": "application/json",
|
|
127
|
+
"Authorization": `Bearer ${apiKey}`
|
|
128
|
+
},
|
|
129
|
+
body: JSON.stringify({
|
|
130
|
+
...event,
|
|
131
|
+
machineId: event.machineId ?? getMachineId()
|
|
132
|
+
}),
|
|
133
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 1e4)
|
|
134
|
+
});
|
|
135
|
+
}, { maxAttempts: options.maxAttempts ?? 2, baseDelayMs: 300 });
|
|
136
|
+
};
|
|
137
|
+
var handlePostToolUse = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
138
|
+
try {
|
|
139
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
140
|
+
const action = describeToolCall(input.tool_name, input.tool_input, "event");
|
|
141
|
+
const lookup = toPolicyLookup(input.tool_name, input.tool_input);
|
|
142
|
+
const isError = isToolResultError(input.tool_result);
|
|
143
|
+
const receiptsEnabled = process.env.PUSHARY_RECEIPTS !== "off";
|
|
144
|
+
const liveMode = await fetchModeState(getApiKey(), input.session_id);
|
|
145
|
+
await Promise.allSettled([
|
|
146
|
+
cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
|
|
147
|
+
reportEvent({
|
|
148
|
+
event: isError ? "tool_error" : "tool_complete",
|
|
149
|
+
agentType: agent.type,
|
|
150
|
+
agentName: `${agent.label} - ${projectName}`,
|
|
151
|
+
action,
|
|
152
|
+
sessionId: input.session_id,
|
|
153
|
+
error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0,
|
|
154
|
+
decisionSource: deriveDecisionSource(lookup.tool, lookup.input, liveMode),
|
|
155
|
+
meta: receiptsEnabled ? deriveReceiptMeta(lookup.tool, lookup.input, input.tool_result, input.cwd ?? process.cwd()) : void 0
|
|
156
|
+
})
|
|
157
|
+
]);
|
|
158
|
+
} catch {
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var TASK_TITLE_MAX_LENGTH = 120;
|
|
162
|
+
var handleUserPrompt = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
163
|
+
try {
|
|
164
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
165
|
+
const titlesEnabled = process.env.PUSHARY_TASK_TITLES !== "off";
|
|
166
|
+
const taskTitle = titlesEnabled ? input.prompt?.replace(/\s+/g, " ").trim().slice(0, TASK_TITLE_MAX_LENGTH) || void 0 : void 0;
|
|
167
|
+
await reportEvent({
|
|
168
|
+
event: "user_prompt",
|
|
169
|
+
agentType: agent.type,
|
|
170
|
+
agentName: `${agent.label} - ${projectName}`,
|
|
171
|
+
sessionId: input.session_id,
|
|
172
|
+
taskTitle
|
|
173
|
+
}, { maxAttempts: 1, timeoutMs: 800 });
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
var handleStop = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
178
|
+
try {
|
|
179
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
180
|
+
await Promise.allSettled([
|
|
181
|
+
cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
|
|
182
|
+
reportEvent({
|
|
183
|
+
event: "session_end",
|
|
184
|
+
agentType: agent.type,
|
|
185
|
+
agentName: `${agent.label} - ${projectName}`,
|
|
186
|
+
action: "Session ended",
|
|
187
|
+
sessionId: input.session_id
|
|
188
|
+
})
|
|
189
|
+
]);
|
|
190
|
+
} catch {
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
var handleNotification = async (input) => {
|
|
194
|
+
try {
|
|
195
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
196
|
+
await reportEvent({
|
|
197
|
+
event: input.type === "error" ? "error" : "notification",
|
|
198
|
+
agentType: "claude_code",
|
|
199
|
+
agentName: `Claude Code - ${projectName}`,
|
|
200
|
+
action: input.title ?? input.message ?? "Notification",
|
|
201
|
+
sessionId: input.session_id,
|
|
202
|
+
error: input.type === "error" ? input.message : void 0
|
|
203
|
+
});
|
|
204
|
+
} catch {
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export {
|
|
209
|
+
CODEX_AGENT,
|
|
210
|
+
codexAllow,
|
|
211
|
+
codexDeny,
|
|
212
|
+
codexPass,
|
|
213
|
+
toCodexWire,
|
|
214
|
+
toPolicyLookup,
|
|
215
|
+
permissionTimeoutDecision,
|
|
216
|
+
preToolUseTimeoutDecision,
|
|
217
|
+
reportEvent,
|
|
218
|
+
handlePostToolUse,
|
|
219
|
+
handleUserPrompt,
|
|
220
|
+
handleStop,
|
|
221
|
+
handleNotification
|
|
222
|
+
};
|