@pushary/agent-hooks 0.14.0 → 0.14.2
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 +26 -62
- package/dist/bin/pushary-codex.js +2 -2
- package/dist/bin/pushary-doctor.js +1 -1
- package/dist/bin/pushary-hook.js +2 -2
- package/dist/bin/pushary-post-hook.js +2 -2
- package/dist/bin/pushary-prompt-hook.js +2 -2
- package/dist/bin/pushary-setup.js +10 -4
- package/dist/bin/pushary-stop-hook.js +2 -2
- package/dist/chunk-5QHQVXWN.js +174 -0
- package/dist/chunk-HJMFYDWY.js +222 -0
- package/dist/chunk-QEPRH7JB.js +315 -0
- package/dist/chunk-V4GLWPD7.js +174 -0
- package/dist/chunk-XPVSZIA3.js +315 -0
- package/dist/chunk-YCL4GUFR.js +222 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +3 -3
- package/package.json +2 -2
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
+
CODEX_AGENT,
|
|
4
|
+
codexAllow,
|
|
5
|
+
codexDeny,
|
|
6
|
+
codexPass,
|
|
3
7
|
handlePostToolUse,
|
|
4
8
|
handleStop,
|
|
5
9
|
handleUserPrompt,
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
permissionTimeoutDecision,
|
|
11
|
+
preToolUseTimeoutDecision,
|
|
12
|
+
reportEvent,
|
|
13
|
+
toCodexWire,
|
|
14
|
+
toPolicyLookup
|
|
15
|
+
} from "../chunk-YCL4GUFR.js";
|
|
8
16
|
import {
|
|
9
17
|
DEFAULT_SESSION,
|
|
10
18
|
askUser,
|
|
@@ -17,7 +25,7 @@ import {
|
|
|
17
25
|
savePendingQuestion,
|
|
18
26
|
sendNotification,
|
|
19
27
|
waitForAnswer
|
|
20
|
-
} from "../chunk-
|
|
28
|
+
} from "../chunk-QEPRH7JB.js";
|
|
21
29
|
import "../chunk-22CV7V7A.js";
|
|
22
30
|
import "../chunk-3MIR7ODJ.js";
|
|
23
31
|
import {
|
|
@@ -26,71 +34,27 @@ import {
|
|
|
26
34
|
|
|
27
35
|
// bin/pushary-codex-hook.ts
|
|
28
36
|
import { basename, join } from "path";
|
|
29
|
-
import { tmpdir } from "os";
|
|
37
|
+
import { tmpdir, userInfo } from "os";
|
|
30
38
|
import { existsSync, mkdirSync, statSync, unlinkSync, writeFileSync } from "fs";
|
|
31
|
-
|
|
32
|
-
// src/codex-adapter.ts
|
|
33
|
-
var CODEX_AGENT = { type: "codex", label: "Codex" };
|
|
34
|
-
var codexAllow = () => ({ kind: "allow" });
|
|
35
|
-
var codexDeny = (reason) => ({ kind: "deny", reason });
|
|
36
|
-
var codexPass = () => ({ kind: "pass" });
|
|
37
|
-
var toCodexWire = (event, decision) => {
|
|
38
|
-
if (event === "PermissionRequest") {
|
|
39
|
-
if (decision.kind === "allow") {
|
|
40
|
-
return {
|
|
41
|
-
hookSpecificOutput: {
|
|
42
|
-
hookEventName: "PermissionRequest",
|
|
43
|
-
decision: { behavior: "allow" }
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
if (decision.kind === "deny") {
|
|
48
|
-
return {
|
|
49
|
-
hookSpecificOutput: {
|
|
50
|
-
hookEventName: "PermissionRequest",
|
|
51
|
-
decision: { behavior: "deny", message: decision.reason }
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
if (event === "PreToolUse" && decision.kind === "deny") {
|
|
58
|
-
return {
|
|
59
|
-
hookSpecificOutput: {
|
|
60
|
-
hookEventName: "PreToolUse",
|
|
61
|
-
permissionDecision: "deny",
|
|
62
|
-
permissionDecisionReason: decision.reason
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
return null;
|
|
67
|
-
};
|
|
68
|
-
var toPolicyLookup = (toolName, toolInput) => {
|
|
69
|
-
if (toolName !== "apply_patch") return { tool: toolName, input: toolInput };
|
|
70
|
-
const command = toolInput.command;
|
|
71
|
-
return {
|
|
72
|
-
tool: "Edit",
|
|
73
|
-
input: typeof command === "string" ? { file_path: command } : {}
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
var permissionTimeoutDecision = (timeoutAction) => {
|
|
77
|
-
if (timeoutAction === "approve") return codexAllow();
|
|
78
|
-
if (timeoutAction === "deny") return codexDeny("No response within timeout");
|
|
79
|
-
return codexPass();
|
|
80
|
-
};
|
|
81
|
-
var preToolUseTimeoutDecision = (timeoutAction, denyReason = "No response within timeout") => timeoutAction === "deny" ? codexDeny(denyReason) : codexPass();
|
|
82
|
-
|
|
83
|
-
// bin/pushary-codex-hook.ts
|
|
84
39
|
var KILL_REASON = "Stopped by user: this agent was halted from Pushary";
|
|
85
40
|
var MAX_WAIT_SECONDS = 170;
|
|
86
|
-
var APPROVAL_DIR = join(tmpdir(), "pushary-codex-approvals");
|
|
87
41
|
var APPROVAL_TTL_MS = 10 * 60 * 1e3;
|
|
88
42
|
var sanitizeId = (value) => value.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 128);
|
|
43
|
+
var userScope = () => {
|
|
44
|
+
try {
|
|
45
|
+
const info = userInfo();
|
|
46
|
+
const id = typeof info.uid === "number" && info.uid >= 0 ? String(info.uid) : info.username;
|
|
47
|
+
return sanitizeId(id) || "default";
|
|
48
|
+
} catch {
|
|
49
|
+
return "default";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var APPROVAL_DIR = join(tmpdir(), `pushary-codex-approvals-${userScope()}`);
|
|
89
53
|
var markApproved = (toolUseId) => {
|
|
90
54
|
if (!toolUseId) return;
|
|
91
55
|
try {
|
|
92
|
-
if (!existsSync(APPROVAL_DIR)) mkdirSync(APPROVAL_DIR, { recursive: true });
|
|
93
|
-
writeFileSync(join(APPROVAL_DIR, sanitizeId(toolUseId)), "", "utf-8");
|
|
56
|
+
if (!existsSync(APPROVAL_DIR)) mkdirSync(APPROVAL_DIR, { recursive: true, mode: 448 });
|
|
57
|
+
writeFileSync(join(APPROVAL_DIR, sanitizeId(toolUseId)), "", { encoding: "utf-8", mode: 384 });
|
|
94
58
|
} catch {
|
|
95
59
|
}
|
|
96
60
|
};
|
|
@@ -161,7 +125,7 @@ var decidePermissionRequest = async (input) => {
|
|
|
161
125
|
const modeState = await fetchModeState(apiKey, input.session_id);
|
|
162
126
|
if (modeState.kill) return codexDeny(KILL_REASON);
|
|
163
127
|
const lookup = toPolicyLookup(input.tool_name ?? "", input.tool_input ?? {});
|
|
164
|
-
const policy = await getPolicy(apiKey);
|
|
128
|
+
const policy = await getPolicy(apiKey, modeState.policyVersion);
|
|
165
129
|
const toolPolicy = resolvePolicy(policy, lookup.tool, modeState.mode, lookup.input);
|
|
166
130
|
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") return codexAllow();
|
|
167
131
|
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
|
|
@@ -196,7 +160,7 @@ var decidePreToolUse = async (input) => {
|
|
|
196
160
|
const modeState = await fetchModeState(apiKey, input.session_id);
|
|
197
161
|
if (modeState.kill) return codexDeny(KILL_REASON);
|
|
198
162
|
const lookup = toPolicyLookup(input.tool_name ?? "", input.tool_input ?? {});
|
|
199
|
-
const policy = await getPolicy(apiKey);
|
|
163
|
+
const policy = await getPolicy(apiKey, modeState.policyVersion);
|
|
200
164
|
const toolPolicy = resolvePolicy(policy, lookup.tool, modeState.mode, lookup.input);
|
|
201
165
|
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") return codexPass();
|
|
202
166
|
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
reportEvent
|
|
4
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-YCL4GUFR.js";
|
|
5
5
|
import {
|
|
6
6
|
askUser,
|
|
7
7
|
getMachineId,
|
|
8
8
|
waitForAnswer
|
|
9
|
-
} from "../chunk-
|
|
9
|
+
} from "../chunk-QEPRH7JB.js";
|
|
10
10
|
import "../chunk-22CV7V7A.js";
|
|
11
11
|
import "../chunk-3MIR7ODJ.js";
|
|
12
12
|
import {
|
|
@@ -162,7 +162,7 @@ var main = async () => {
|
|
|
162
162
|
if (codexNotifyPath) {
|
|
163
163
|
console.log(` ${warn} Codex: stale legacy notify entry ${dim("(double-push risk, re-run setup to remove it)")}`);
|
|
164
164
|
}
|
|
165
|
-
console.log(` ${warn} Codex:
|
|
165
|
+
console.log(` ${warn} Codex: trust the hooks inside Codex ${dim("(type /hooks in Codex and trust them, once; cannot be checked from here)")}`);
|
|
166
166
|
} else {
|
|
167
167
|
check(!!codexNotifyPath, "Codex: notify handler configured (deprecated)", codexNotifyPath ? "upgrade Codex and re-run setup for native hooks" : "missing, re-run setup");
|
|
168
168
|
if (codexNotifyPath) {
|
package/dist/bin/pushary-hook.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
handlePreToolUse
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-5QHQVXWN.js";
|
|
5
|
+
import "../chunk-QEPRH7JB.js";
|
|
6
6
|
import "../chunk-22CV7V7A.js";
|
|
7
7
|
import "../chunk-3MIR7ODJ.js";
|
|
8
8
|
import "../chunk-VUNL35KE.js";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
handlePostToolUse
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-YCL4GUFR.js";
|
|
5
|
+
import "../chunk-QEPRH7JB.js";
|
|
6
6
|
import "../chunk-22CV7V7A.js";
|
|
7
7
|
import "../chunk-3MIR7ODJ.js";
|
|
8
8
|
import "../chunk-VUNL35KE.js";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
handleUserPrompt
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-YCL4GUFR.js";
|
|
5
|
+
import "../chunk-QEPRH7JB.js";
|
|
6
6
|
import "../chunk-22CV7V7A.js";
|
|
7
7
|
import "../chunk-3MIR7ODJ.js";
|
|
8
8
|
import "../chunk-VUNL35KE.js";
|
|
@@ -518,10 +518,16 @@ var setupCodex = async (_apiKey) => {
|
|
|
518
518
|
if (hooksSupported) {
|
|
519
519
|
console.log(` ${dim2("\u2022")} Native hooks: phone approvals, policy enforcement, kill switch, session tracking`);
|
|
520
520
|
console.log();
|
|
521
|
-
console.log(` ${bold2("
|
|
522
|
-
console.log(` ${dim2("
|
|
523
|
-
console.log(` ${dim2("
|
|
524
|
-
console.log(
|
|
521
|
+
console.log(` ${bold2("One step left, and it happens inside Codex")}`);
|
|
522
|
+
console.log(` ${dim2("Codex makes you trust any hook before it runs. This is Codex's own")}`);
|
|
523
|
+
console.log(` ${dim2("security check, not a Pushary thing, and you only do it once.")}`);
|
|
524
|
+
console.log();
|
|
525
|
+
console.log(` ${cyan2("1.")} Open Codex and type ${cyan2("/hooks")}`);
|
|
526
|
+
console.log(` ${cyan2("2.")} You will see the Pushary hook commands. Trust them.`);
|
|
527
|
+
console.log();
|
|
528
|
+
console.log(` ${dim2("Until you do, Codex just runs as usual without the phone approvals.")}`);
|
|
529
|
+
console.log(` ${dim2("To confirm:")} run a command that needs approval and watch your phone.`);
|
|
530
|
+
console.log(` ${dim2("Pushary updates keep the same trust, so Codex will not ask again.")}`);
|
|
525
531
|
} else {
|
|
526
532
|
console.log(` ${dim2("\u2022")} Notify handler (deprecated): captures turn completions and approval requests`);
|
|
527
533
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
handleStop
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-YCL4GUFR.js";
|
|
5
|
+
import "../chunk-QEPRH7JB.js";
|
|
6
6
|
import "../chunk-22CV7V7A.js";
|
|
7
7
|
import "../chunk-3MIR7ODJ.js";
|
|
8
8
|
import "../chunk-VUNL35KE.js";
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_SESSION,
|
|
3
|
+
askUser,
|
|
4
|
+
deriveToolTarget,
|
|
5
|
+
describeToolCall,
|
|
6
|
+
fetchModeState,
|
|
7
|
+
getMachineId,
|
|
8
|
+
getPolicy,
|
|
9
|
+
resolvePolicy,
|
|
10
|
+
savePendingQuestion,
|
|
11
|
+
sendNotification,
|
|
12
|
+
waitForAnswer
|
|
13
|
+
} from "./chunk-QEPRH7JB.js";
|
|
14
|
+
import {
|
|
15
|
+
getApiKey
|
|
16
|
+
} from "./chunk-VUNL35KE.js";
|
|
17
|
+
|
|
18
|
+
// src/hook.ts
|
|
19
|
+
import { basename } from "path";
|
|
20
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
|
+
var allow = () => ({
|
|
22
|
+
hookSpecificOutput: {
|
|
23
|
+
hookEventName: "PreToolUse",
|
|
24
|
+
permissionDecision: "allow"
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
var deny = (reason) => ({
|
|
28
|
+
hookSpecificOutput: {
|
|
29
|
+
hookEventName: "PreToolUse",
|
|
30
|
+
permissionDecision: "deny",
|
|
31
|
+
permissionDecisionReason: reason
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
var ask = (reason) => ({
|
|
35
|
+
hookSpecificOutput: {
|
|
36
|
+
hookEventName: "PreToolUse",
|
|
37
|
+
permissionDecision: "ask",
|
|
38
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
|
|
42
|
+
while (Date.now() < deadlineMs) {
|
|
43
|
+
const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
|
|
44
|
+
let answer;
|
|
45
|
+
try {
|
|
46
|
+
answer = await waitForAnswer(apiKey, correlationId, remaining);
|
|
47
|
+
} catch {
|
|
48
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
49
|
+
await sleep(pollInterval);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (answer.answered) return answer;
|
|
53
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
54
|
+
await sleep(pollInterval);
|
|
55
|
+
}
|
|
56
|
+
return { answered: false };
|
|
57
|
+
};
|
|
58
|
+
var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction, sessionId, machineId, toolName, toolTarget) => {
|
|
59
|
+
let result;
|
|
60
|
+
try {
|
|
61
|
+
result = await askUser(apiKey, {
|
|
62
|
+
question: `Allow ${description}?`,
|
|
63
|
+
type: "confirm",
|
|
64
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
65
|
+
agentName: `Claude Code - ${projectName}`,
|
|
66
|
+
sessionId,
|
|
67
|
+
machineId,
|
|
68
|
+
toolName,
|
|
69
|
+
toolTarget
|
|
70
|
+
});
|
|
71
|
+
} catch {
|
|
72
|
+
switch (timeoutAction) {
|
|
73
|
+
case "approve":
|
|
74
|
+
return allow();
|
|
75
|
+
case "deny":
|
|
76
|
+
return deny("Push notification failed, denying per policy");
|
|
77
|
+
default:
|
|
78
|
+
return ask("Push notification failed, asking in terminal");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const deadline = Date.now() + timeoutSeconds * 1e3;
|
|
82
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
|
|
83
|
+
if (answer.answered) {
|
|
84
|
+
return answer.value === "yes" ? allow() : deny("Denied via push notification");
|
|
85
|
+
}
|
|
86
|
+
switch (timeoutAction) {
|
|
87
|
+
case "approve":
|
|
88
|
+
return allow();
|
|
89
|
+
case "deny":
|
|
90
|
+
return deny("No response within timeout");
|
|
91
|
+
default:
|
|
92
|
+
return ask("No push response, asking in terminal");
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
var handleTerminalOnly = () => {
|
|
96
|
+
return ask();
|
|
97
|
+
};
|
|
98
|
+
var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds, sessionId, machineId, toolName, toolTarget) => {
|
|
99
|
+
let result;
|
|
100
|
+
try {
|
|
101
|
+
result = await askUser(apiKey, {
|
|
102
|
+
question: `Allow ${description}?`,
|
|
103
|
+
type: "confirm",
|
|
104
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
105
|
+
agentName: `Claude Code - ${projectName}`,
|
|
106
|
+
sessionId,
|
|
107
|
+
machineId,
|
|
108
|
+
toolName,
|
|
109
|
+
toolTarget
|
|
110
|
+
});
|
|
111
|
+
} catch {
|
|
112
|
+
return ask("Push notification failed, asking in terminal");
|
|
113
|
+
}
|
|
114
|
+
const deadline = Date.now() + pushFirstSeconds * 1e3;
|
|
115
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
|
|
116
|
+
if (answer.answered) {
|
|
117
|
+
return answer.value === "yes" ? allow() : deny("Denied via push notification");
|
|
118
|
+
}
|
|
119
|
+
savePendingQuestion(sessionId || DEFAULT_SESSION, result.correlationId);
|
|
120
|
+
return ask("Sent as push notification. You can also approve here.");
|
|
121
|
+
};
|
|
122
|
+
var handleNotifyOnly = async (apiKey, description, projectName, sessionId, machineId) => {
|
|
123
|
+
try {
|
|
124
|
+
await sendNotification(apiKey, {
|
|
125
|
+
title: "Agent needs approval",
|
|
126
|
+
body: description,
|
|
127
|
+
agentName: `Claude Code - ${projectName}`,
|
|
128
|
+
sessionId,
|
|
129
|
+
machineId
|
|
130
|
+
});
|
|
131
|
+
} catch {
|
|
132
|
+
}
|
|
133
|
+
return ask();
|
|
134
|
+
};
|
|
135
|
+
var handlePreToolUse = async (input) => {
|
|
136
|
+
try {
|
|
137
|
+
const apiKey = getApiKey();
|
|
138
|
+
const modeState = await fetchModeState(apiKey, input.session_id);
|
|
139
|
+
const policy = await getPolicy(apiKey, modeState.policyVersion);
|
|
140
|
+
if (modeState.kill) {
|
|
141
|
+
return deny("Stopped by user \u2014 this agent was halted from Pushary");
|
|
142
|
+
}
|
|
143
|
+
const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode, input.tool_input);
|
|
144
|
+
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
145
|
+
return allow();
|
|
146
|
+
}
|
|
147
|
+
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
|
|
148
|
+
return deny(`Denied by policy for ${toolPolicy.tool}`);
|
|
149
|
+
}
|
|
150
|
+
const description = describeToolCall(input.tool_name, input.tool_input, "hook");
|
|
151
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
152
|
+
const sessionId = input.session_id;
|
|
153
|
+
const machineId = getMachineId();
|
|
154
|
+
const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
|
|
155
|
+
switch (toolPolicy.mode) {
|
|
156
|
+
case "push_only":
|
|
157
|
+
return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget);
|
|
158
|
+
case "terminal_only":
|
|
159
|
+
return handleTerminalOnly();
|
|
160
|
+
case "push_first":
|
|
161
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
|
|
162
|
+
case "notify_only":
|
|
163
|
+
return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
|
|
164
|
+
default:
|
|
165
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
|
|
166
|
+
}
|
|
167
|
+
} catch {
|
|
168
|
+
return ask("Pushary unavailable, falling back to terminal approval");
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export {
|
|
173
|
+
handlePreToolUse
|
|
174
|
+
};
|
|
@@ -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
|
+
};
|