@pushary/agent-hooks 0.36.0 → 0.38.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.
@@ -1,8 +1,4 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- removeClaudeMcpServers,
4
- removePusharySettings
5
- } from "../chunk-SO6ZG5ML.js";
6
2
  import {
7
3
  removeCodexHooks,
8
4
  removeGeminiSettings,
@@ -11,6 +7,10 @@ import {
11
7
  import {
12
8
  execNpm
13
9
  } from "../chunk-J7JWI3KU.js";
10
+ import {
11
+ removeClaudeMcpServers,
12
+ removePusharySettings
13
+ } from "../chunk-7EW3USQF.js";
14
14
  import "../chunk-Z5PL3K7C.js";
15
15
 
16
16
  // bin/pushary-clean.ts
@@ -32,7 +32,7 @@ import {
32
32
  toCodexWire,
33
33
  toPolicyLookup,
34
34
  waitForAnswer
35
- } from "../chunk-WH5ASAZK.js";
35
+ } from "../chunk-JHN3H6LX.js";
36
36
  import {
37
37
  isGatingMoment,
38
38
  recordKeylessMoment
@@ -4,7 +4,7 @@ import {
4
4
  getMachineId,
5
5
  reportEvent,
6
6
  waitForAnswer
7
- } from "../chunk-WH5ASAZK.js";
7
+ } from "../chunk-JHN3H6LX.js";
8
8
  import "../chunk-DWED7BS3.js";
9
9
  import "../chunk-Z5PL3K7C.js";
10
10
  import {
@@ -24,7 +24,7 @@ import {
24
24
  savePendingQuestion,
25
25
  sendNotification,
26
26
  waitForAnswer
27
- } from "../chunk-WH5ASAZK.js";
27
+ } from "../chunk-JHN3H6LX.js";
28
28
  import {
29
29
  isGatingMoment,
30
30
  recordKeylessMoment
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePreToolUse
4
- } from "../chunk-JR4H3FND.js";
4
+ } from "../chunk-X6L25EOF.js";
5
+ import "../chunk-7EW3USQF.js";
5
6
  import "../chunk-KQYIHZ5E.js";
6
- import "../chunk-WH5ASAZK.js";
7
+ import "../chunk-JHN3H6LX.js";
7
8
  import "../chunk-R5AJNXZS.js";
8
9
  import "../chunk-DWED7BS3.js";
9
10
  import "../chunk-Z5PL3K7C.js";
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleNotification
4
- } from "../chunk-WH5ASAZK.js";
4
+ } from "../chunk-JHN3H6LX.js";
5
5
  import "../chunk-DWED7BS3.js";
6
6
  import "../chunk-Z5PL3K7C.js";
7
7
  import "../chunk-NKXSILEW.js";
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ handlePermissionDenied
4
+ } from "../chunk-X6L25EOF.js";
5
+ import "../chunk-7EW3USQF.js";
6
+ import "../chunk-KQYIHZ5E.js";
7
+ import "../chunk-JHN3H6LX.js";
8
+ import "../chunk-R5AJNXZS.js";
9
+ import "../chunk-DWED7BS3.js";
10
+ import "../chunk-Z5PL3K7C.js";
11
+ import "../chunk-NKXSILEW.js";
12
+
13
+ // bin/pushary-permission-denied-hook.ts
14
+ var main = async () => {
15
+ let rawInput = "";
16
+ for await (const chunk of process.stdin) {
17
+ rawInput += chunk;
18
+ }
19
+ try {
20
+ const input = rawInput.trim() ? JSON.parse(rawInput) : {};
21
+ const output = await handlePermissionDenied(input);
22
+ if (output) process.stdout.write(JSON.stringify(output));
23
+ } catch {
24
+ }
25
+ };
26
+ main();
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ handlePermissionRequest
4
+ } from "../chunk-X6L25EOF.js";
5
+ import "../chunk-7EW3USQF.js";
6
+ import "../chunk-KQYIHZ5E.js";
7
+ import "../chunk-JHN3H6LX.js";
8
+ import "../chunk-R5AJNXZS.js";
9
+ import "../chunk-DWED7BS3.js";
10
+ import "../chunk-Z5PL3K7C.js";
11
+ import "../chunk-NKXSILEW.js";
12
+
13
+ // bin/pushary-permission-hook.ts
14
+ var main = async () => {
15
+ let rawInput = "";
16
+ for await (const chunk of process.stdin) {
17
+ rawInput += chunk;
18
+ }
19
+ try {
20
+ const input = rawInput.trim() ? JSON.parse(rawInput) : {};
21
+ const output = await handlePermissionRequest(input);
22
+ if (output) process.stdout.write(JSON.stringify(output));
23
+ } catch {
24
+ }
25
+ };
26
+ main();
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePostToolUse
4
- } from "../chunk-WH5ASAZK.js";
4
+ } from "../chunk-JHN3H6LX.js";
5
5
  import "../chunk-DWED7BS3.js";
6
6
  import "../chunk-Z5PL3K7C.js";
7
7
  import "../chunk-NKXSILEW.js";
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleUserPrompt
4
- } from "../chunk-WH5ASAZK.js";
4
+ } from "../chunk-JHN3H6LX.js";
5
5
  import "../chunk-DWED7BS3.js";
6
6
  import "../chunk-Z5PL3K7C.js";
7
7
  import "../chunk-NKXSILEW.js";
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleSessionEnd
4
- } from "../chunk-WH5ASAZK.js";
4
+ } from "../chunk-JHN3H6LX.js";
5
5
  import "../chunk-DWED7BS3.js";
6
6
  import "../chunk-Z5PL3K7C.js";
7
7
  import "../chunk-NKXSILEW.js";
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleSessionStart
4
- } from "../chunk-WH5ASAZK.js";
4
+ } from "../chunk-JHN3H6LX.js";
5
5
  import "../chunk-DWED7BS3.js";
6
6
  import "../chunk-Z5PL3K7C.js";
7
7
  import "../chunk-NKXSILEW.js";
@@ -1,9 +1,4 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- addClaudeMcpServer,
4
- addPusharyHooks,
5
- addPusharyToolPermissions
6
- } from "../chunk-SO6ZG5ML.js";
7
2
  import {
8
3
  GEMINI_HOOK_BINARY,
9
4
  addCodexHookTrust,
@@ -22,9 +17,14 @@ import {
22
17
  resolveGlobalBinDir,
23
18
  resolveGlobalBinary
24
19
  } from "../chunk-J7JWI3KU.js";
20
+ import {
21
+ addClaudeMcpServer,
22
+ addPusharyHooks,
23
+ addPusharyToolPermissions
24
+ } from "../chunk-7EW3USQF.js";
25
25
  import {
26
26
  reportEvent
27
- } from "../chunk-WH5ASAZK.js";
27
+ } from "../chunk-JHN3H6LX.js";
28
28
  import "../chunk-DWED7BS3.js";
29
29
  import {
30
30
  isValidApiKey
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleStop
4
- } from "../chunk-WH5ASAZK.js";
4
+ } from "../chunk-JHN3H6LX.js";
5
5
  import "../chunk-DWED7BS3.js";
6
6
  import "../chunk-Z5PL3K7C.js";
7
7
  import "../chunk-NKXSILEW.js";
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ handleStopFailure
4
+ } from "../chunk-JHN3H6LX.js";
5
+ import "../chunk-DWED7BS3.js";
6
+ import "../chunk-Z5PL3K7C.js";
7
+ import "../chunk-NKXSILEW.js";
8
+
9
+ // bin/pushary-stopfailure-hook.ts
10
+ var main = async () => {
11
+ let rawInput = "";
12
+ for await (const chunk of process.stdin) {
13
+ rawInput += chunk;
14
+ }
15
+ try {
16
+ const input = rawInput.trim() ? JSON.parse(rawInput) : {};
17
+ await handleStopFailure(input);
18
+ } catch {
19
+ }
20
+ };
21
+ main();
@@ -6,6 +6,8 @@ import {
6
6
  import { join } from "path";
7
7
  var PUSHARY_MCP_URL = "https://pushary.com/api/mcp/mcp";
8
8
  var PUSHARY_PERMISSION_RULE = "mcp__pushary__*";
9
+ var PRETOOLUSE_GATED_TOOLS = ["Bash", "Write", "Edit", "MultiEdit", "NotebookEdit", "PowerShell", "Monitor"];
10
+ var PRETOOLUSE_HANDLED_TOOLS = /* @__PURE__ */ new Set([...PRETOOLUSE_GATED_TOOLS, "AskUserQuestion"]);
9
11
  var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
10
12
  var ensureRecord = (target, key) => {
11
13
  const existing = asRecord(target[key]);
@@ -20,7 +22,7 @@ var isPusharyHook = (entry) => {
20
22
  if (!Array.isArray(hooks)) return false;
21
23
  return hooks.some((hook) => {
22
24
  const command = String(asRecord(hook)?.command ?? "");
23
- return command.includes("pushary-hook") || command.includes("pushary-post-hook") || command.includes("pushary-stop-hook") || command.includes("pushary-prompt-hook") || command.includes("pushary-notification-hook") || command.includes("pushary-session-start-hook") || command.includes("pushary-session-end-hook");
25
+ return command.includes("pushary-hook") || command.includes("pushary-post-hook") || command.includes("pushary-stop-hook") || command.includes("pushary-prompt-hook") || command.includes("pushary-notification-hook") || command.includes("pushary-session-start-hook") || command.includes("pushary-session-end-hook") || command.includes("pushary-stopfailure-hook") || command.includes("pushary-permission-hook") || command.includes("pushary-permission-denied-hook");
24
26
  });
25
27
  };
26
28
  var addClaudeMcpServer = (config, apiKey) => {
@@ -65,7 +67,7 @@ var addPusharyHooks = (settings, binDir) => {
65
67
  return /\s/.test(path) ? `"${path}"` : path;
66
68
  };
67
69
  const hooks = ensureRecord(settings, "hooks");
68
- const SHELL_AND_FILE_TOOLS = "Bash|Write|Edit|MultiEdit|NotebookEdit|PowerShell|Monitor";
70
+ const SHELL_AND_FILE_TOOLS = PRETOOLUSE_GATED_TOOLS.join("|");
69
71
  const preToolUse = (Array.isArray(hooks.PreToolUse) ? hooks.PreToolUse : []).filter((entry) => !isPusharyHook(entry));
70
72
  preToolUse.push({
71
73
  matcher: `${SHELL_AND_FILE_TOOLS}|AskUserQuestion`,
@@ -133,6 +135,33 @@ var addPusharyHooks = (settings, binDir) => {
133
135
  }]
134
136
  });
135
137
  hooks.SessionEnd = sessionEnd;
138
+ const stopFailure = (Array.isArray(hooks.StopFailure) ? hooks.StopFailure : []).filter((entry) => !isPusharyHook(entry));
139
+ stopFailure.push({
140
+ hooks: [{
141
+ type: "command",
142
+ command: resolve("pushary-stopfailure-hook"),
143
+ timeout: 10
144
+ }]
145
+ });
146
+ hooks.StopFailure = stopFailure;
147
+ const permissionRequest = (Array.isArray(hooks.PermissionRequest) ? hooks.PermissionRequest : []).filter((entry) => !isPusharyHook(entry));
148
+ permissionRequest.push({
149
+ hooks: [{
150
+ type: "command",
151
+ command: resolve("pushary-permission-hook"),
152
+ timeout: HOOK_BUDGETS.claude.budgetSeconds
153
+ }]
154
+ });
155
+ hooks.PermissionRequest = permissionRequest;
156
+ const permissionDenied = (Array.isArray(hooks.PermissionDenied) ? hooks.PermissionDenied : []).filter((entry) => !isPusharyHook(entry));
157
+ permissionDenied.push({
158
+ hooks: [{
159
+ type: "command",
160
+ command: resolve("pushary-permission-denied-hook"),
161
+ timeout: 60
162
+ }]
163
+ });
164
+ hooks.PermissionDenied = permissionDenied;
136
165
  };
137
166
  var removePusharySettings = (settings) => {
138
167
  let changed = removeClaudeMcpServers(settings);
@@ -151,7 +180,7 @@ var removePusharySettings = (settings) => {
151
180
  }
152
181
  const hooks = asRecord(settings.hooks);
153
182
  if (hooks) {
154
- for (const key of ["PreToolUse", "PostToolUse", "Stop", "UserPromptSubmit", "Notification", "SessionStart", "SessionEnd"]) {
183
+ for (const key of ["PreToolUse", "PostToolUse", "Stop", "StopFailure", "UserPromptSubmit", "Notification", "SessionStart", "SessionEnd", "PermissionRequest", "PermissionDenied"]) {
155
184
  const entries = hooks[key];
156
185
  if (!Array.isArray(entries)) continue;
157
186
  const filtered = entries.filter((entry) => !isPusharyHook(entry));
@@ -170,6 +199,7 @@ var removePusharySettings = (settings) => {
170
199
  };
171
200
 
172
201
  export {
202
+ PRETOOLUSE_HANDLED_TOOLS,
173
203
  addClaudeMcpServer,
174
204
  removeClaudeMcpServers,
175
205
  addPusharyToolPermissions,
@@ -812,12 +812,14 @@ var reportEvent = async (event, options = {}) => {
812
812
  }
813
813
  }, { maxAttempts: options.maxAttempts ?? 2, baseDelayMs: 300 });
814
814
  };
815
+ var NON_GATED_MIRROR_TOOLS = /* @__PURE__ */ new Set(["TodoWrite"]);
815
816
  var handlePostToolUse = async (input, agent = CLAUDE_CODE_AGENT) => {
816
817
  try {
817
818
  const projectName = basename(input.cwd ?? process.cwd());
818
819
  const action = describeToolCall(input.tool_name, input.tool_input, "event");
819
820
  const lookup = toPolicyLookup(input.tool_name, input.tool_input);
820
- const isError = isToolResultError(input.tool_result);
821
+ const toolResult = input.tool_response ?? input.tool_result;
822
+ const isError = isToolResultError(toolResult);
821
823
  const receiptsEnabled = process.env.PUSHARY_RECEIPTS !== "off";
822
824
  const sessionKey = input.session_id || DEFAULT_SESSION;
823
825
  const liveMode = await fetchModeState(getApiKey(), input.session_id);
@@ -827,18 +829,19 @@ var handlePostToolUse = async (input, agent = CLAUDE_CODE_AGENT) => {
827
829
  additionalContext = buildLateAnswerContext(late);
828
830
  for (const l of late) removePendingQuestion(sessionKey, l.correlationId);
829
831
  }
830
- const decisionSource = deriveDecisionSource(lookup.tool, lookup.input, liveMode);
831
- const beacon = decisionSource === "policy_auto" && throttlePass(`autodecision:${sessionKey}:${lookup.tool}`, AUTO_DECISION_WINDOW_MS) ? deriveAutoDecisionBeacon(lookup.tool, lookup.input, liveMode) : void 0;
832
+ const isMirrorOnly = NON_GATED_MIRROR_TOOLS.has(input.tool_name);
833
+ const decisionSource = isMirrorOnly ? void 0 : deriveDecisionSource(lookup.tool, lookup.input, liveMode);
834
+ const beacon = !isMirrorOnly && decisionSource === "policy_auto" && throttlePass(`autodecision:${sessionKey}:${lookup.tool}`, AUTO_DECISION_WINDOW_MS) ? deriveAutoDecisionBeacon(lookup.tool, lookup.input, liveMode) : void 0;
832
835
  const toolReport = reportEvent({
833
836
  event: isError ? "tool_error" : "tool_complete",
834
837
  agentType: agent.type,
835
838
  agentName: `${agent.label} - ${projectName}`,
836
839
  action,
837
840
  sessionId: input.session_id,
838
- error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0,
841
+ error: isError ? String(toolResult?.error ?? toolResult?.stderr ?? "").slice(0, 500) : void 0,
839
842
  decisionSource,
840
843
  ...beacon ? { decisionOrigin: beacon.decisionOrigin, toolName: beacon.toolName, toolTarget: beacon.toolTarget } : {},
841
- meta: receiptsEnabled ? deriveReceiptMeta(lookup.tool, lookup.input, input.tool_result, input.cwd ?? process.cwd()) : void 0,
844
+ meta: receiptsEnabled ? deriveReceiptMeta(lookup.tool, lookup.input, toolResult, input.cwd ?? process.cwd()) : void 0,
842
845
  usage: deriveUsage(input.transcript_path, input.session_id)
843
846
  }, { maxAttempts: 1 });
844
847
  const extraReports = [];
@@ -902,6 +905,14 @@ var summarizeFinalMessage = (message) => {
902
905
  if (!collapsed) return "Session ended";
903
906
  return collapsed.slice(0, STOP_SUMMARY_MAX_LENGTH);
904
907
  };
908
+ var countArray = (value) => Array.isArray(value) ? value.length : 0;
909
+ var summarizeStop = (input) => {
910
+ const running = countArray(input.background_tasks);
911
+ if (running > 0) return `Paused with ${running} background task${running === 1 ? "" : "s"} still running`;
912
+ const crons = countArray(input.session_crons);
913
+ if (crons > 0) return `Idle until a scheduled task wakes it (${crons} pending)`;
914
+ return summarizeFinalMessage(input.last_assistant_message);
915
+ };
905
916
  var handleStop = async (input, agent = CLAUDE_CODE_AGENT) => {
906
917
  try {
907
918
  const projectName = basename(input.cwd ?? process.cwd());
@@ -915,7 +926,7 @@ var handleStop = async (input, agent = CLAUDE_CODE_AGENT) => {
915
926
  event: "session_end",
916
927
  agentType: agent.type,
917
928
  agentName: `${agent.label} - ${projectName}`,
918
- action: summarizeFinalMessage(input.last_assistant_message),
929
+ action: summarizeStop(input),
919
930
  sessionId: input.session_id,
920
931
  usage: deriveUsage(input.transcript_path, input.session_id)
921
932
  }, { maxAttempts: 1 })
@@ -948,7 +959,7 @@ var handleSessionStart = async (input, agent = CLAUDE_CODE_AGENT) => {
948
959
  agentName: `${agent.label} - ${projectName}`,
949
960
  action,
950
961
  sessionId: input.session_id
951
- }, { maxAttempts: 1, timeoutMs: 800 });
962
+ }, { maxAttempts: 2, timeoutMs: 4e3 });
952
963
  } catch {
953
964
  }
954
965
  };
@@ -965,6 +976,12 @@ var handleSessionEnd = async (input, agent = CLAUDE_CODE_AGENT) => {
965
976
  const projectName = basename(input.cwd ?? process.cwd());
966
977
  const action = typeof input.reason === "string" && SESSION_END_ACTIONS[input.reason] || "Session ended";
967
978
  const sessionKey = input.session_id || DEFAULT_SESSION;
979
+ const late = await reconcilePendingQuestions(sessionKey);
980
+ if (late.length > 0) {
981
+ await notifyLateAnswers(getApiKey(), late, `${agent.label} - ${projectName}`, input.session_id).catch(() => {
982
+ });
983
+ for (const l of late) removePendingQuestion(sessionKey, l.correlationId);
984
+ }
968
985
  if (!isDefaultSession(sessionKey)) removePendingSession(sessionKey);
969
986
  await reportEvent({
970
987
  event: "session_closed",
@@ -972,7 +989,7 @@ var handleSessionEnd = async (input, agent = CLAUDE_CODE_AGENT) => {
972
989
  agentName: `${agent.label} - ${projectName}`,
973
990
  action,
974
991
  sessionId: input.session_id
975
- }, { maxAttempts: 1, timeoutMs: 800 });
992
+ }, { maxAttempts: 2, timeoutMs: 3e3 });
976
993
  } catch {
977
994
  }
978
995
  };
@@ -1014,6 +1031,57 @@ var handleNotification = async (input, agent = CLAUDE_CODE_AGENT) => {
1014
1031
  } catch {
1015
1032
  }
1016
1033
  };
1034
+ var STOPFAILURE_MAX_ERROR_LENGTH = 300;
1035
+ var STOPFAILURE_REASONS = {
1036
+ rate_limit: "Rate limited by the API",
1037
+ overloaded: "The API is overloaded right now",
1038
+ authentication_failed: "Authentication failed",
1039
+ billing_error: "A billing error stopped the turn"
1040
+ };
1041
+ var describeStopFailure = (error) => {
1042
+ const fromString = (s) => {
1043
+ const v = s.trim();
1044
+ if (!v) return void 0;
1045
+ return STOPFAILURE_REASONS[v] ?? v.slice(0, STOPFAILURE_MAX_ERROR_LENGTH);
1046
+ };
1047
+ if (typeof error === "string") {
1048
+ const described = fromString(error);
1049
+ if (described) return described;
1050
+ } else if (error && typeof error === "object") {
1051
+ const e = error;
1052
+ for (const key of ["type", "message", "error", "code"]) {
1053
+ const val = e[key];
1054
+ if (typeof val === "string") {
1055
+ const described = fromString(val);
1056
+ if (described) return described;
1057
+ }
1058
+ }
1059
+ }
1060
+ return "The turn ended on an API error (rate limit, billing, or auth).";
1061
+ };
1062
+ var handleStopFailure = async (input, agent = CLAUDE_CODE_AGENT) => {
1063
+ try {
1064
+ let apiKey;
1065
+ try {
1066
+ apiKey = getApiKey();
1067
+ } catch {
1068
+ return;
1069
+ }
1070
+ const projectName = basename(input.cwd ?? process.cwd());
1071
+ const mode = await fetchModeState(apiKey, input.session_id).catch(() => null);
1072
+ if (mode?.kill || mode?.mode === "terminal_only") return;
1073
+ const reason = describeStopFailure(input.error);
1074
+ await sendNotification(apiKey, {
1075
+ title: `${agent.label} stopped on an error`,
1076
+ body: reason,
1077
+ agentName: `${agent.label} - ${projectName}`,
1078
+ sessionId: input.session_id,
1079
+ context: { type: "error", errorMessage: reason }
1080
+ }, 4e3).catch(() => {
1081
+ });
1082
+ } catch {
1083
+ }
1084
+ };
1017
1085
 
1018
1086
  export {
1019
1087
  askUser,
@@ -1032,6 +1100,7 @@ export {
1032
1100
  getMachineId,
1033
1101
  DEFAULT_SESSION,
1034
1102
  savePendingQuestion,
1103
+ throttlePass,
1035
1104
  readLastUserPrompt,
1036
1105
  CODEX_AGENT,
1037
1106
  codexAllow,
@@ -1049,5 +1118,6 @@ export {
1049
1118
  handleStop,
1050
1119
  handleSessionStart,
1051
1120
  handleSessionEnd,
1052
- handleNotification
1121
+ handleNotification,
1122
+ handleStopFailure
1053
1123
  };
@@ -1,3 +1,6 @@
1
+ import {
2
+ PRETOOLUSE_HANDLED_TOOLS
3
+ } from "./chunk-7EW3USQF.js";
1
4
  import {
2
5
  denyReasonFrom,
3
6
  isDeferAnswer
@@ -19,8 +22,9 @@ import {
19
22
  resolvePolicy,
20
23
  savePendingQuestion,
21
24
  sendNotification,
25
+ throttlePass,
22
26
  waitForAnswer
23
- } from "./chunk-WH5ASAZK.js";
27
+ } from "./chunk-JHN3H6LX.js";
24
28
  import {
25
29
  isGatingMoment,
26
30
  recordKeylessMoment
@@ -195,6 +199,31 @@ var handleNotifyOnly = async (apiKey, description, projectName, sessionId, machi
195
199
  }
196
200
  return ask();
197
201
  };
202
+ var dispatchModeHandler = async (apiKey, input, toolPolicy, machineId) => {
203
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
204
+ const projectName = basename(input.cwd ?? process.cwd());
205
+ const sessionId = input.session_id;
206
+ const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
207
+ const intent = readLastPrompt(sessionId ?? DEFAULT_SESSION) ?? (input.transcript_path ? readLastUserPrompt(input.transcript_path) : void 0);
208
+ const decision = {
209
+ intent,
210
+ action: deriveAction(input.tool_name, input.tool_input),
211
+ blocker: deriveBlocker(toolPolicy.mode),
212
+ actionBody: deriveActionBody(input.tool_name, input.tool_input)
213
+ };
214
+ switch (toolPolicy.mode) {
215
+ case "push_only":
216
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget, decision);
217
+ case "terminal_only":
218
+ return handleTerminalOnly();
219
+ case "push_first":
220
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget, decision);
221
+ case "notify_only":
222
+ return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
223
+ default:
224
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget, decision);
225
+ }
226
+ };
198
227
  var KEYLESS_NOTICE = "[pushary] Not connected (no API key), so your agent runs untouched. Approval moments are counted locally; see them with: npx @pushary/agent-hooks@latest stats. Connect: https://pushary.com/sign-up?utm_source=cli&utm_medium=keyless-hook";
199
228
  var keylessNoticeOnce = (sessionId) => {
200
229
  try {
@@ -308,35 +337,115 @@ var handlePreToolUse = async (input) => {
308
337
  if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
309
338
  return deny(`Denied by policy for ${toolPolicy.tool}`);
310
339
  }
311
- const description = describeToolCall(input.tool_name, input.tool_input, "hook");
340
+ return dispatchModeHandler(apiKey, input, toolPolicy, getMachineId());
341
+ } catch {
342
+ return void 0;
343
+ }
344
+ };
345
+ var permReqAllow = (updatedInput) => ({
346
+ hookSpecificOutput: {
347
+ hookEventName: "PermissionRequest",
348
+ decision: { behavior: "allow", ...updatedInput ? { updatedInput } : {} }
349
+ }
350
+ });
351
+ var permReqDeny = (message) => ({
352
+ hookSpecificOutput: {
353
+ hookEventName: "PermissionRequest",
354
+ decision: { behavior: "deny", ...message ? { message } : {} }
355
+ }
356
+ });
357
+ var toPermissionRequestOutput = (hookOutput) => {
358
+ if (!hookOutput) return void 0;
359
+ const d = hookOutput.hookSpecificOutput;
360
+ if (d.permissionDecision === "allow") return permReqAllow(d.updatedInput);
361
+ if (d.permissionDecision === "deny") return permReqDeny(d.permissionDecisionReason);
362
+ return void 0;
363
+ };
364
+ var handlePermissionRequest = async (input) => {
365
+ if (PRETOOLUSE_HANDLED_TOOLS.has(input.tool_name)) return void 0;
366
+ if (input.tool_name.startsWith("mcp__pushary__")) return void 0;
367
+ let apiKey;
368
+ try {
369
+ apiKey = getApiKey();
370
+ } catch {
371
+ return void 0;
372
+ }
373
+ try {
374
+ const modeState = await fetchModeState(apiKey, input.session_id);
375
+ const policy = await getPolicy(apiKey, modeState.policyVersion);
376
+ if (modeState.kill) return permReqDeny("Stopped by user \u2014 this agent was halted from Pushary");
377
+ const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode, input.tool_input);
378
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") return permReqAllow();
379
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
380
+ return permReqDeny(`Denied by policy for ${toolPolicy.tool}`);
381
+ }
382
+ return toPermissionRequestOutput(await dispatchModeHandler(apiKey, input, toolPolicy, getMachineId()));
383
+ } catch {
384
+ return void 0;
385
+ }
386
+ };
387
+ var PERMISSION_DENIED_WINDOW_MS = 5 * 60 * 1e3;
388
+ var PERMISSION_DENIED_WAIT_SECONDS = 25;
389
+ var automodeRetryEnabled = () => {
390
+ const flag = process.env.PUSHARY_AUTOMODE_RETRY;
391
+ return flag === "1" || flag === "true";
392
+ };
393
+ var handlePermissionDenied = async (input) => {
394
+ if (input.tool_name.startsWith("mcp__pushary__")) return void 0;
395
+ let apiKey;
396
+ try {
397
+ apiKey = getApiKey();
398
+ } catch {
399
+ return void 0;
400
+ }
401
+ try {
312
402
  const projectName = basename(input.cwd ?? process.cwd());
313
- const sessionId = input.session_id;
314
- const machineId = getMachineId();
315
- const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
316
- const intent = readLastPrompt(sessionId ?? DEFAULT_SESSION) ?? (input.transcript_path ? readLastUserPrompt(input.transcript_path) : void 0);
317
- const decision = {
318
- intent,
319
- action: deriveAction(input.tool_name, input.tool_input),
320
- blocker: deriveBlocker(toolPolicy.mode),
321
- actionBody: deriveActionBody(input.tool_name, input.tool_input)
322
- };
323
- switch (toolPolicy.mode) {
324
- case "push_only":
325
- return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget, decision);
326
- case "terminal_only":
327
- return handleTerminalOnly();
328
- case "push_first":
329
- return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget, decision);
330
- case "notify_only":
331
- return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
332
- default:
333
- return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget, decision);
403
+ const sessionKey = input.session_id || DEFAULT_SESSION;
404
+ if (!throttlePass(`permdenied:${sessionKey}:${input.tool_name}`, PERMISSION_DENIED_WINDOW_MS)) return void 0;
405
+ const mode = await fetchModeState(apiKey, input.session_id).catch(() => null);
406
+ if (mode?.kill || mode?.mode === "terminal_only") return void 0;
407
+ if (!automodeRetryEnabled()) {
408
+ await sendNotification(apiKey, {
409
+ title: `Auto-mode blocked ${input.tool_name}`,
410
+ body: `Your agent was denied ${input.tool_name} in ${projectName}.`,
411
+ agentName: `Claude Code - ${projectName}`,
412
+ sessionId: input.session_id
413
+ }, 4e3).catch(() => {
414
+ });
415
+ return void 0;
416
+ }
417
+ let result;
418
+ try {
419
+ result = await askUser(apiKey, {
420
+ question: `Auto-mode blocked ${input.tool_name} in ${projectName}. Let the agent retry it?`,
421
+ type: "confirm",
422
+ context: "Your agent hit a permission wall in auto mode",
423
+ agentName: `Claude Code - ${projectName}`,
424
+ sessionId: input.session_id,
425
+ machineId: getMachineId(),
426
+ toolName: input.tool_name
427
+ });
428
+ } catch {
429
+ return void 0;
334
430
  }
431
+ if (result.suppressed || result.noDevices) {
432
+ await cancelQuestion(apiKey, result.correlationId).catch(() => {
433
+ });
434
+ return void 0;
435
+ }
436
+ const deadline = hookWaitDeadline(START_MS, PERMISSION_DENIED_WAIT_SECONDS, "claude", Date.now());
437
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
438
+ if (answer.answered && answer.value === "yes") {
439
+ return { hookSpecificOutput: { hookEventName: "PermissionDenied", retry: true } };
440
+ }
441
+ return void 0;
335
442
  } catch {
336
443
  return void 0;
337
444
  }
338
445
  };
339
446
 
340
447
  export {
341
- handlePreToolUse
448
+ handlePreToolUse,
449
+ handlePermissionRequest,
450
+ handlePermissionDenied
342
451
  };
@@ -78,6 +78,7 @@ declare const reportEvent: (event: AgentEvent, options?: ReportEventOptions) =>
78
78
  declare const handlePostToolUse: (input: {
79
79
  tool_name: string;
80
80
  tool_input: Record<string, unknown>;
81
+ tool_response?: Record<string, unknown>;
81
82
  tool_result?: Record<string, unknown>;
82
83
  cwd?: string;
83
84
  session_id?: string;
@@ -93,6 +94,8 @@ declare const handleStop: (input: {
93
94
  stop_hook_active?: boolean;
94
95
  transcript_path?: string;
95
96
  last_assistant_message?: string;
97
+ background_tasks?: unknown;
98
+ session_crons?: unknown;
96
99
  }, agent?: AgentIdentity) => Promise<StopHookOutput | undefined>;
97
100
  declare const handleNotification: (input: {
98
101
  message?: string;
package/dist/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  handlePreToolUse
3
- } from "../chunk-JR4H3FND.js";
3
+ } from "../chunk-X6L25EOF.js";
4
+ import "../chunk-7EW3USQF.js";
4
5
  import "../chunk-KQYIHZ5E.js";
5
6
  import {
6
7
  askUser,
@@ -14,7 +15,7 @@ import {
14
15
  reportEvent,
15
16
  resolvePolicy,
16
17
  waitForAnswer
17
- } from "../chunk-WH5ASAZK.js";
18
+ } from "../chunk-JHN3H6LX.js";
18
19
  import "../chunk-R5AJNXZS.js";
19
20
  import "../chunk-DWED7BS3.js";
20
21
  import "../chunk-Z5PL3K7C.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.36.0",
3
+ "version": "0.38.0",
4
4
  "description": "Permission hooks for AI coding agents: route tool approvals through Pushary push notifications",
5
5
  "keywords": [
6
6
  "pushary",
@@ -50,6 +50,9 @@
50
50
  "pushary-notification-hook": "./dist/bin/pushary-notification-hook.js",
51
51
  "pushary-session-start-hook": "./dist/bin/pushary-session-start-hook.js",
52
52
  "pushary-session-end-hook": "./dist/bin/pushary-session-end-hook.js",
53
+ "pushary-stopfailure-hook": "./dist/bin/pushary-stopfailure-hook.js",
54
+ "pushary-permission-hook": "./dist/bin/pushary-permission-hook.js",
55
+ "pushary-permission-denied-hook": "./dist/bin/pushary-permission-denied-hook.js",
53
56
  "pushary-claude": "./dist/bin/pushary-claude.js",
54
57
  "pushary-codex": "./dist/bin/pushary-codex.js",
55
58
  "pushary-codex-hook": "./dist/bin/pushary-codex-hook.js",