-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathhook_executor.py
More file actions
590 lines (513 loc) · 21.5 KB
/
Copy pathhook_executor.py
File metadata and controls
590 lines (513 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
"""Hook execution engine — mirrors TypeScript utils/hooks.ts.
Core hook execution with shell command protocol, JSON stdin/stdout,
exit code semantics, timeout handling, and settings-driven configuration.
Exit code semantics:
- 0: success (stdout parsed as JSON for decisions)
- 2: blocking error (stderr or stdout used as error message)
- other non-zero: non-blocking error (logged, execution continues)
Security invariant: hooks cannot lower security level.
Hook 'allow' does not bypass settings deny/ask rules.
"""
from __future__ import annotations
import asyncio
import json
import logging
import os
import time
import warnings
from typing import Any, AsyncGenerator
from uuid import uuid4
from src.hooks.hook_types import (
TOOL_HOOK_EXECUTION_TIMEOUT_MS,
HookConfig,
HookEvent,
HookResult,
)
from src.hooks.trust_gate import should_skip_hook_due_to_trust
from src.types.messages import (
create_attachment_message,
create_progress_message,
)
logger = logging.getLogger(__name__)
def _get_hooks_from_snapshot(tool_use_context: Any) -> dict[str, list[HookConfig]]:
"""Read hooks from the frozen snapshot held on the active HookConfigManager.
Mirrors typescript/src/utils/hooks/hooksConfigSnapshot.ts:119-124
(``getHooksConfigFromSnapshot``). settings.json is never re-read implicitly;
snapshot updates flow only through ``HookConfigManager.load()`` (startup) or
``HookConfigManager.reload_if_changed()`` (explicit /hooks command).
**Back-compat:** if ``hook_config_manager`` is not set on the context but
``options.hooks`` is, fall back to the legacy read path with a
``DeprecationWarning``. This keeps existing callers / tests working during
one CHANGELOG cycle. After the deprecation cycle, the fallback is removed.
"""
manager = getattr(tool_use_context, "hook_config_manager", None)
if manager is not None:
snapshot = getattr(manager, "snapshot", None)
if snapshot is None:
# Manager exists but hasn't been loaded — treat as empty.
return {}
# Defensive copy: callers MUST NOT mutate the returned dict.
return {ev: list(hooks) for ev, hooks in snapshot.hooks.items()}
# Legacy fallback — emit DeprecationWarning if options.hooks carries data.
return _get_hooks_from_options_legacy(tool_use_context)
def _get_hooks_from_options_legacy(tool_use_context: Any) -> dict[str, list[HookConfig]]:
"""Legacy read path: ``tool_use_context.options.hooks``.
Bypasses the snapshot freezing semantic introduced in WI-0.1. Preserved for
one CHANGELOG cycle so existing callers / tests do not break in lockstep
with the rewire. Emits a ``DeprecationWarning`` when it actually returns
data (silent no-op when options.hooks is empty/None).
"""
try:
options = getattr(tool_use_context, "options", None)
if options is None:
return {}
hooks_config = getattr(options, "hooks", None)
if hooks_config is None or not hooks_config:
return {}
if not isinstance(hooks_config, dict):
return {}
result: dict[str, list[HookConfig]] = {}
for event_name, hook_list in hooks_config.items():
if isinstance(hook_list, list):
configs = []
for h in hook_list:
if isinstance(h, dict):
configs.append(HookConfig(
type=h.get("type", "command"),
command=h.get("command", ""),
timeout=h.get("timeout"),
matcher=h.get("matcher"),
))
elif isinstance(h, HookConfig):
configs.append(h)
result[event_name] = configs
if result:
warnings.warn(
"Reading hooks from tool_use_context.options.hooks is deprecated; "
"wire a HookConfigManager onto tool_use_context.hook_config_manager "
"and call .load() at bootstrap. The legacy path bypasses the snapshot "
"security model (chapter §'The Snapshot Security Model'). Will be "
"removed two CHANGELOG entries after the rename.",
DeprecationWarning,
stacklevel=3,
)
return result
except Exception:
return {}
# Back-compat alias — preserved for any external test fixtures still importing
# ``_get_hooks_from_settings`` directly. New code uses ``_get_hooks_from_snapshot``.
def _get_hooks_from_settings(tool_use_context: Any) -> dict[str, list[HookConfig]]:
return _get_hooks_from_snapshot(tool_use_context)
def has_hook_for_event(event: str, tool_use_context: Any) -> bool:
hooks = _get_hooks_from_snapshot(tool_use_context)
return bool(hooks.get(event))
def _matches_tool(matcher: str | None, tool_name: str) -> bool:
if matcher is None:
return True
if matcher == tool_name:
return True
if matcher.endswith("*"):
prefix = matcher[:-1]
return tool_name.startswith(prefix)
if matcher.startswith("*"):
suffix = matcher[1:]
return tool_name.endswith(suffix)
return matcher == tool_name
def _build_hook_env(
hook: HookConfig,
stdin_data: dict[str, Any],
tool_use_context: Any | None,
) -> dict[str, str]:
"""Compute the env-var dict passed to a command-hook subprocess.
Phase-1 / WI-1.5 — adds three vars on top of inherited ``os.environ``:
* ``CLAUDE_HOOK_EVENT`` — the canonical event name (existing).
* ``CLAUDE_PROJECT_DIR`` — workspace root from the active context.
Empty string if the context doesn't carry a workspace_root.
* ``CLAUDE_PLUGIN_ROOT`` — set from ``hook.skill_root`` (populated only
for skill-declared hooks; empty for everything else).
* ``CLAUDE_ENV_FILE`` — per-fire ephemeral env file path. Set ONLY for
the three lifecycle events that benefit from env propagation
(``SessionStart``, ``Setup``, ``CwdChanged``). For other events:
empty string. Per N4: this WI sets the path; the
sourcing-and-applying loop (read the file back and apply exports to
subsequent shells in the session) is a separate follow-up ticket.
TODO(ch12-followup): ticket #<TBD> covers the env-file source/apply
cycle.
"""
event_name = stdin_data.get("hook_event", "")
workspace_root = ""
if tool_use_context is not None:
wr = getattr(tool_use_context, "workspace_root", None)
if wr is not None:
workspace_root = str(wr)
env_file = _env_file_for_event(event_name)
return {
**os.environ,
"CLAUDE_HOOK_EVENT": event_name,
"CLAUDE_PROJECT_DIR": workspace_root,
"CLAUDE_PLUGIN_ROOT": hook.skill_root or "",
"CLAUDE_ENV_FILE": env_file,
}
def _env_file_for_event(event_name: str) -> str:
"""Return a writable path for the hook to write env exports to.
Only set for events whose hooks may legitimately propagate env to
subsequent shells in the session (TS pattern). For other events, return
empty string — the hook MAY still observe ``CLAUDE_ENV_FILE`` and treat
"empty" as "no env propagation requested."
The file is per-fire ephemeral: a unique path under
``~/.clawcodex/hook-env/<event>.<pid>.<nanos>``. This WI does NOT
create the file or read it back; it only computes the path. Sourcing is
a follow-up.
"""
if event_name not in ("SessionStart", "Setup", "CwdChanged"):
return ""
home = os.path.expanduser("~")
return os.path.join(
home, ".clawcodex", "hook-env",
f"{event_name}.{os.getpid()}.{time.time_ns()}",
)
async def _execute_command_hook(
hook: HookConfig,
stdin_data: dict[str, Any],
abort_signal: Any | None = None,
timeout_ms: int = TOOL_HOOK_EXECUTION_TIMEOUT_MS,
tool_use_context: Any | None = None,
) -> HookResult:
command = hook.command
if not command:
return HookResult()
effective_timeout = (hook.timeout or timeout_ms) / 1000.0
start_time = time.monotonic()
try:
stdin_json = json.dumps(stdin_data, default=str)
# Round-2 / Ch12 — per-hook shell selection. ``shell="powershell"``
# spawns ``pwsh`` with explicit argv and skips the bash-shell path.
# ``None`` / ``"bash"`` keeps the historical ``create_subprocess_shell``
# invocation. Mirrors the TS branch at
# ``typescript/src/utils/hooks.ts:1098-1125``.
if hook.shell == "powershell":
from .shell_invocation import build_powershell_args, find_powershell_path
pwsh_path = find_powershell_path()
if pwsh_path is None:
duration_ms = int((time.monotonic() - start_time) * 1000)
# Error string mirrors TS at typescript/src/utils/hooks.ts:1102-1106
# verbatim (single quotes around 'powershell' as in the TS source)
# so log scrapers / regression tests written against TS messages
# transfer unchanged.
return HookResult(
blocking_error=(
f"Hook \"{command}\" has shell: 'powershell' but no "
"PowerShell executable (pwsh or powershell) was found "
"on PATH. Install PowerShell, or remove "
"\"shell\": \"powershell\" to use bash."
),
exit_code=-1,
duration_ms=duration_ms,
command=command,
)
process = await asyncio.create_subprocess_exec(
pwsh_path,
*build_powershell_args(command),
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=_build_hook_env(hook, stdin_data, tool_use_context),
)
else:
# Default (bash on POSIX via /bin/sh, the historical path).
# An explicit ``shell="bash"`` lands here too — it's a no-op
# alias for ``None`` per the chapter's "defaults to bash"
# contract.
process = await asyncio.create_subprocess_shell(
command,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=_build_hook_env(hook, stdin_data, tool_use_context),
)
try:
stdout_bytes, stderr_bytes = await asyncio.wait_for(
process.communicate(stdin_json.encode()),
timeout=effective_timeout,
)
except asyncio.TimeoutError:
try:
process.kill()
except Exception:
pass
duration_ms = int((time.monotonic() - start_time) * 1000)
return HookResult(
blocking_error=f"Hook timed out after {duration_ms}ms",
exit_code=-1,
duration_ms=duration_ms,
command=command,
)
duration_ms = int((time.monotonic() - start_time) * 1000)
stdout = stdout_bytes.decode("utf-8", errors="replace").strip()
stderr = stderr_bytes.decode("utf-8", errors="replace").strip()
exit_code = process.returncode or 0
if abort_signal and abort_signal.aborted:
return HookResult(
exit_code=exit_code,
duration_ms=duration_ms,
command=command,
)
if exit_code == 2:
error_msg = stderr or stdout or "Hook exited with code 2"
return HookResult(
blocking_error=error_msg,
exit_code=exit_code,
stdout=stdout,
stderr=stderr,
duration_ms=duration_ms,
command=command,
)
if exit_code != 0:
return HookResult(
exit_code=exit_code,
stdout=stdout,
stderr=stderr,
duration_ms=duration_ms,
command=command,
)
result = HookResult(
exit_code=0,
stdout=stdout,
stderr=stderr,
duration_ms=duration_ms,
command=command,
)
# Phase-1 / WI-1.4 — schema-validated output parsing. Replaces the
# prior ad-hoc ``dict.get`` block: malformed output (capital-D
# ``Deny``, unknown fields, non-string ``reason``, etc.) used to no-op
# silently; now it logs a WARNING and the decision payload is
# dropped (exit code is still honored).
if stdout:
from src.hooks.output_schema import parse_hook_output # local import: pydantic
parsed, err = parse_hook_output(stdout)
if err is not None:
logger.warning(
"Hook %r emitted output that failed schema validation; "
"dropping decision payload. error=%s stdout=%r",
command, err, stdout[:200],
)
elif parsed is not None:
if parsed.decision is not None:
result.permission_behavior = parsed.decision
result.hook_permission_decision_reason = parsed.reason
if parsed.updatedInput:
result.updated_input = parsed.updatedInput
if parsed.preventContinuation:
result.prevent_continuation = True
result.stop_reason = parsed.stopReason
if parsed.additionalContexts:
result.additional_contexts = parsed.additionalContexts
if parsed.updatedMCPToolOutput is not None:
result.updated_mcp_tool_output = parsed.updatedMCPToolOutput
return result
except Exception as e:
duration_ms = int((time.monotonic() - start_time) * 1000)
return HookResult(
blocking_error=f"Hook execution error: {e}",
exit_code=-1,
duration_ms=duration_ms,
command=command,
)
async def _run_hooks_for_event(
event: str,
tool_name: str | None,
stdin_data: dict[str, Any],
tool_use_context: Any,
abort_signal: Any | None = None,
timeout_ms: int = TOOL_HOOK_EXECUTION_TIMEOUT_MS,
) -> AsyncGenerator[dict[str, Any], None]:
# WI-0.2 — workspace-trust gate. Skip non-policy hooks while the workspace
# is untrusted. The per-hook policy check happens below since policy-source
# identification is per-HookConfig.
trust_skip = should_skip_hook_due_to_trust(tool_use_context)
# WI-0.1 — read from the frozen snapshot, not from options.hooks. The
# snapshot is built once at startup by HookConfigManager.load() and is
# immune to settings.json mutation between trust acceptance and tool calls.
hooks = _get_hooks_from_snapshot(tool_use_context)
event_hooks = hooks.get(event, [])
if trust_skip:
# Drop everything that isn't a policy-source hook. ``HookConfig.source``
# is a ``HookSource`` enum; any non-policy value is gated. Imported
# locally to avoid pulling the enum into module-init paths that don't
# need it. Phase-1 / WI-1.2 renamed ``POLICY`` → ``POLICY_SETTINGS``;
# the ``is_policy`` predicate is the canonical way to ask the
# question and shields callers from future renames.
gated = [h for h in event_hooks if not h.source.is_policy]
event_hooks = [h for h in event_hooks if h.source.is_policy]
if gated:
# Breadcrumb for "why don't my hooks run": configured hooks
# silently not firing in an untrusted workspace is
# undiagnosable otherwise (#275).
logger.info(
"skipping %d non-policy %s hook(s): workspace is untrusted",
len(gated),
event,
)
tool_use_id = stdin_data.get("tool_use_id", str(uuid4()))
parent_tool_use_id = ""
for hook in event_hooks:
if tool_name and not _matches_tool(hook.matcher, tool_name):
continue
yield {
"message": create_progress_message(
toolUseID=tool_use_id,
parentToolUseID=parent_tool_use_id,
data={"command": hook.command, "prompt_text": None},
),
}
result = await _execute_command_hook(
hook,
{**stdin_data, "hook_event": event},
abort_signal=abort_signal,
timeout_ms=timeout_ms,
tool_use_context=tool_use_context,
)
if result.blocking_error:
yield {"blocking_error": {"blocking_error": result.blocking_error, "command": result.command}}
if result.permission_behavior is not None:
yield {
"permission_behavior": result.permission_behavior,
"hook_permission_decision_reason": result.hook_permission_decision_reason,
"updated_input": result.updated_input,
}
if result.prevent_continuation:
yield {
"prevent_continuation": True,
"stop_reason": result.stop_reason,
}
if result.additional_contexts:
yield {"additional_contexts": result.additional_contexts}
if result.updated_mcp_tool_output is not None:
yield {"updated_mcp_tool_output": result.updated_mcp_tool_output}
if result.updated_input and result.permission_behavior is None:
yield {"updated_input": result.updated_input}
if result.exit_code is not None and result.exit_code != 0 and result.exit_code != 2:
yield {
"message": create_attachment_message({
"type": "hook_non_blocking_error",
"hook_event": event,
"exit_code": result.exit_code,
"stdout": result.stdout,
"stderr": result.stderr,
"duration_ms": result.duration_ms,
"command": result.command,
}),
}
elif result.exit_code == 0:
yield {
"message": create_attachment_message({
"type": "hook_success",
"hook_event": event,
"stdout": result.stdout,
"stderr": result.stderr,
"duration_ms": result.duration_ms,
"command": result.command,
}),
}
async def execute_pre_tool_hooks(
tool_name: str,
tool_use_id: str,
tool_input: dict[str, Any],
tool_use_context: Any,
) -> AsyncGenerator[dict[str, Any], None]:
stdin_data = {
"tool_name": tool_name,
"tool_use_id": tool_use_id,
"tool_input": tool_input,
}
abort_signal = None
abort_ctrl = getattr(tool_use_context, "abort_controller", None)
if abort_ctrl:
abort_signal = abort_ctrl.signal
async for result in _run_hooks_for_event(
"PreToolUse",
tool_name,
stdin_data,
tool_use_context,
abort_signal=abort_signal,
):
yield result
async def execute_post_tool_hooks(
tool_name: str,
tool_use_id: str,
tool_input: dict[str, Any],
tool_response: Any,
tool_use_context: Any,
) -> AsyncGenerator[dict[str, Any], None]:
stdin_data = {
"tool_name": tool_name,
"tool_use_id": tool_use_id,
"tool_input": tool_input,
"tool_response": str(tool_response) if not isinstance(tool_response, (str, dict, list)) else tool_response,
}
abort_signal = None
abort_ctrl = getattr(tool_use_context, "abort_controller", None)
if abort_ctrl:
abort_signal = abort_ctrl.signal
async for result in _run_hooks_for_event(
"PostToolUse",
tool_name,
stdin_data,
tool_use_context,
abort_signal=abort_signal,
):
yield result
async def execute_post_tool_failure_hooks(
tool_name: str,
tool_use_id: str,
tool_input: dict[str, Any],
error: str,
tool_use_context: Any,
) -> AsyncGenerator[dict[str, Any], None]:
stdin_data = {
"tool_name": tool_name,
"tool_use_id": tool_use_id,
"tool_input": tool_input,
"error": error,
}
abort_signal = None
abort_ctrl = getattr(tool_use_context, "abort_controller", None)
if abort_ctrl:
abort_signal = abort_ctrl.signal
async for result in _run_hooks_for_event(
"PostToolUseFailure",
tool_name,
stdin_data,
tool_use_context,
abort_signal=abort_signal,
):
yield result
async def execute_stop_hooks(
*,
permission_mode: str | None = None,
abort_signal: Any | None = None,
timeout_ms: int = TOOL_HOOK_EXECUTION_TIMEOUT_MS,
stop_hook_active: bool = False,
subagent_id: str | None = None,
tool_use_context: Any = None,
messages: list[Any] | None = None,
agent_type: str | None = None,
) -> AsyncGenerator[dict[str, Any], None]:
event = "SubagentStop" if subagent_id else "Stop"
stdin_data: dict[str, Any] = {
"permission_mode": permission_mode,
"stop_hook_active": stop_hook_active,
}
if subagent_id:
stdin_data["subagent_id"] = subagent_id
if agent_type:
stdin_data["agent_type"] = agent_type
async for result in _run_hooks_for_event(
event,
None,
stdin_data,
tool_use_context,
abort_signal=abort_signal,
timeout_ms=timeout_ms,
):
yield result