claude-dev-env 8.118.0 → 8.118.1
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/hooks/blocking/open_tasks_transcript.py +15 -186
- package/hooks/blocking/open_tasks_transcript_records.py +211 -0
- package/hooks/blocking/test_open_tasks_transcript.py +32 -0
- package/hooks/blocking/test_open_tasks_transcript_records.py +174 -0
- package/hooks/blocking/test_turn_review_gate.py +19 -0
- package/hooks/features/lifecycle-cleanup.md +1 -1
- package/hooks/lifecycle/nested_project_hooks.py +1 -148
- package/hooks/lifecycle/nested_project_hooks_discovery.py +168 -0
- package/hooks/lifecycle/test_nested_project_hooks.py +41 -0
- package/hooks/lifecycle/test_nested_project_hooks_discovery.py +164 -0
- package/hooks/validation/post_tool_use_dispatcher.py +27 -8
- package/hooks/validation/test_post_tool_use_dispatcher.py +51 -0
- package/package.json +1 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Behavior tests for finding the hooks that nested checkouts declare.
|
|
2
|
+
|
|
3
|
+
Each case builds a session directory with child checkouts on disk and reads
|
|
4
|
+
which checkouts and which command hooks the discovery functions return.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import pathlib
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
from lifecycle.nested_project_hooks_discovery import (
|
|
15
|
+
NestedHook,
|
|
16
|
+
collect_nested_hooks,
|
|
17
|
+
find_nested_projects,
|
|
18
|
+
matcher_selects,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
DEFAULT_TIMEOUT_SECONDS = 600.0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _make_checkout(
|
|
25
|
+
session_directory: pathlib.Path,
|
|
26
|
+
checkout_name: str,
|
|
27
|
+
all_hook_groups_by_event: dict[str, list[dict[str, object]]],
|
|
28
|
+
) -> pathlib.Path:
|
|
29
|
+
checkout_directory = session_directory / checkout_name
|
|
30
|
+
(checkout_directory / ".git").mkdir(parents=True)
|
|
31
|
+
settings_directory = checkout_directory / ".claude"
|
|
32
|
+
settings_directory.mkdir()
|
|
33
|
+
(settings_directory / "settings.json").write_text(
|
|
34
|
+
json.dumps({"hooks": all_hook_groups_by_event}), encoding="utf-8"
|
|
35
|
+
)
|
|
36
|
+
return checkout_directory
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_should_find_each_child_checkout_with_settings_in_name_order(
|
|
40
|
+
tmp_path: pathlib.Path,
|
|
41
|
+
) -> None:
|
|
42
|
+
beta_directory = _make_checkout(tmp_path, "beta", {})
|
|
43
|
+
alpha_directory = _make_checkout(tmp_path, "alpha", {})
|
|
44
|
+
(tmp_path / "no-git" / ".claude").mkdir(parents=True)
|
|
45
|
+
(tmp_path / "no-git" / ".claude" / "settings.json").write_text("{}", encoding="utf-8")
|
|
46
|
+
(tmp_path / "no-settings" / ".git").mkdir(parents=True)
|
|
47
|
+
(tmp_path / "loose-file.txt").write_text("not a checkout", encoding="utf-8")
|
|
48
|
+
|
|
49
|
+
assert find_nested_projects(tmp_path) == [alpha_directory, beta_directory]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_should_find_no_child_when_the_session_directory_is_a_checkout(
|
|
53
|
+
tmp_path: pathlib.Path,
|
|
54
|
+
) -> None:
|
|
55
|
+
(tmp_path / ".git").mkdir()
|
|
56
|
+
_make_checkout(tmp_path, "alpha", {})
|
|
57
|
+
|
|
58
|
+
assert find_nested_projects(tmp_path) == []
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_should_find_no_child_when_the_session_directory_has_its_own_settings(
|
|
62
|
+
tmp_path: pathlib.Path,
|
|
63
|
+
) -> None:
|
|
64
|
+
(tmp_path / ".claude").mkdir()
|
|
65
|
+
(tmp_path / ".claude" / "settings.json").write_text("{}", encoding="utf-8")
|
|
66
|
+
_make_checkout(tmp_path, "alpha", {})
|
|
67
|
+
|
|
68
|
+
assert find_nested_projects(tmp_path) == []
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_should_find_no_child_when_the_session_directory_is_missing(
|
|
72
|
+
tmp_path: pathlib.Path,
|
|
73
|
+
) -> None:
|
|
74
|
+
assert find_nested_projects(tmp_path / "gone") == []
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@pytest.mark.parametrize(
|
|
78
|
+
("matcher", "match_target", "is_selected"),
|
|
79
|
+
[
|
|
80
|
+
(None, "Bash", True),
|
|
81
|
+
("", "Bash", True),
|
|
82
|
+
("*", "Bash", True),
|
|
83
|
+
("Write|Edit", "Edit", True),
|
|
84
|
+
("Write|Edit", "Bash", False),
|
|
85
|
+
("Bas", "Bash", False),
|
|
86
|
+
("[", "[", True),
|
|
87
|
+
("[", "Bash", False),
|
|
88
|
+
(7, "Bash", False),
|
|
89
|
+
],
|
|
90
|
+
)
|
|
91
|
+
def test_should_select_a_target_only_when_the_matcher_covers_all_of_it(
|
|
92
|
+
matcher: object, match_target: str, is_selected: bool
|
|
93
|
+
) -> None:
|
|
94
|
+
assert matcher_selects(matcher, match_target) is is_selected
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_should_collect_command_hooks_by_checkout_then_declaration_order(
|
|
98
|
+
tmp_path: pathlib.Path,
|
|
99
|
+
) -> None:
|
|
100
|
+
alpha_directory = _make_checkout(
|
|
101
|
+
tmp_path,
|
|
102
|
+
"alpha",
|
|
103
|
+
{
|
|
104
|
+
"PreToolUse": [
|
|
105
|
+
{"matcher": "Bash", "hooks": [{"type": "command", "command": "./first.sh"}]},
|
|
106
|
+
{
|
|
107
|
+
"matcher": "Bash",
|
|
108
|
+
"hooks": [{"type": "command", "command": "./second.sh", "timeout": 5}],
|
|
109
|
+
},
|
|
110
|
+
]
|
|
111
|
+
},
|
|
112
|
+
)
|
|
113
|
+
beta_directory = _make_checkout(
|
|
114
|
+
tmp_path,
|
|
115
|
+
"beta",
|
|
116
|
+
{"PreToolUse": [{"hooks": [{"type": "command", "command": "./third.sh"}]}]},
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
assert collect_nested_hooks(tmp_path, "PreToolUse", "Bash") == [
|
|
120
|
+
NestedHook(alpha_directory, "./first.sh", DEFAULT_TIMEOUT_SECONDS),
|
|
121
|
+
NestedHook(alpha_directory, "./second.sh", 5.0),
|
|
122
|
+
NestedHook(beta_directory, "./third.sh", DEFAULT_TIMEOUT_SECONDS),
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def test_should_skip_entries_that_are_not_runnable_command_hooks(
|
|
127
|
+
tmp_path: pathlib.Path,
|
|
128
|
+
) -> None:
|
|
129
|
+
alpha_directory = _make_checkout(
|
|
130
|
+
tmp_path,
|
|
131
|
+
"alpha",
|
|
132
|
+
{
|
|
133
|
+
"PreToolUse": [
|
|
134
|
+
{"matcher": "Edit", "hooks": [{"type": "command", "command": "./edit-only.sh"}]},
|
|
135
|
+
"not a group",
|
|
136
|
+
{
|
|
137
|
+
"matcher": "Bash",
|
|
138
|
+
"hooks": [
|
|
139
|
+
{"type": "prompt", "prompt": "check the call"},
|
|
140
|
+
{"type": "command", "command": " "},
|
|
141
|
+
{"type": "command", "command": "./zero-timeout.sh", "timeout": 0},
|
|
142
|
+
"not a hook",
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
"SessionStart": [{"hooks": [{"type": "command", "command": "./start.sh"}]}],
|
|
147
|
+
},
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
assert collect_nested_hooks(tmp_path, "PreToolUse", "Bash") == [
|
|
151
|
+
NestedHook(alpha_directory, "./zero-timeout.sh", DEFAULT_TIMEOUT_SECONDS),
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@pytest.mark.parametrize("unreadable_settings", ["{not json", "[]", '{"hooks": []}'])
|
|
156
|
+
def test_should_collect_nothing_from_settings_that_cannot_be_read(
|
|
157
|
+
tmp_path: pathlib.Path, unreadable_settings: str
|
|
158
|
+
) -> None:
|
|
159
|
+
checkout_directory = _make_checkout(tmp_path, "alpha", {})
|
|
160
|
+
(checkout_directory / ".claude" / "settings.json").write_text(
|
|
161
|
+
unreadable_settings, encoding="utf-8"
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
assert collect_nested_hooks(tmp_path, "PreToolUse", "Bash") == []
|
|
@@ -82,6 +82,32 @@ def _log_hook_crash(hook_script_path: str, error: Exception) -> None:
|
|
|
82
82
|
sys.stderr.flush()
|
|
83
83
|
|
|
84
84
|
|
|
85
|
+
def _run_hook_script(hook_script_path: str) -> tuple[bool, int]:
|
|
86
|
+
"""Run one hook script under ``__main__`` and return whether it crashed and its exit code.
|
|
87
|
+
|
|
88
|
+
::
|
|
89
|
+
|
|
90
|
+
the script calls sys.exit(2) -> (False, 2)
|
|
91
|
+
the script ends without an exit -> (False, 0)
|
|
92
|
+
the script calls sys.exit("text") -> (False, 0)
|
|
93
|
+
the script raises ValueError -> (True, 0), and the crash is logged
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
hook_script_path: Absolute path of the hook script to run.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The crash flag, then the integer the hook passed to SystemExit, else 0.
|
|
100
|
+
"""
|
|
101
|
+
try:
|
|
102
|
+
runpy.run_path(hook_script_path, run_name="__main__")
|
|
103
|
+
except SystemExit as exit_signal:
|
|
104
|
+
return False, exit_signal.code if isinstance(exit_signal.code, int) else 0
|
|
105
|
+
except Exception as error:
|
|
106
|
+
_log_hook_crash(hook_script_path, error)
|
|
107
|
+
return True, 0
|
|
108
|
+
return False, 0
|
|
109
|
+
|
|
110
|
+
|
|
85
111
|
def run_hosted_hook(
|
|
86
112
|
hook_script_path: str,
|
|
87
113
|
all_hook_arguments: list[str],
|
|
@@ -113,19 +139,12 @@ def run_hosted_hook(
|
|
|
113
139
|
original_stdout = sys.stdout
|
|
114
140
|
original_argv = sys.argv
|
|
115
141
|
captured_output = io.StringIO()
|
|
116
|
-
hook_did_crash = False
|
|
117
|
-
hook_exit_code = 0
|
|
118
142
|
|
|
119
143
|
try:
|
|
120
144
|
sys.stdin = io.StringIO(payload_text)
|
|
121
145
|
sys.stdout = captured_output
|
|
122
146
|
sys.argv = [hook_script_path, *all_hook_arguments]
|
|
123
|
-
|
|
124
|
-
except SystemExit as exit_signal:
|
|
125
|
-
hook_exit_code = exit_signal.code if isinstance(exit_signal.code, int) else 0
|
|
126
|
-
except Exception as error:
|
|
127
|
-
_log_hook_crash(hook_script_path, error)
|
|
128
|
-
hook_did_crash = True
|
|
147
|
+
hook_did_crash, hook_exit_code = _run_hook_script(hook_script_path)
|
|
129
148
|
finally:
|
|
130
149
|
sys.stdin = original_stdin
|
|
131
150
|
sys.stdout = original_stdout
|
|
@@ -411,3 +411,54 @@ def test_should_pass_a_hosted_hook_exit_two_to_the_host_with_its_stderr(
|
|
|
411
411
|
assert (raised_exit.value.code or 0) == expected_exit_code
|
|
412
412
|
assert "probe child says stop" in capsys.readouterr().err
|
|
413
413
|
assert later_marker.exists()
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
@pytest.mark.parametrize(
|
|
417
|
+
("probe_source", "expected_outcome"),
|
|
418
|
+
[
|
|
419
|
+
("import sys\nsys.exit(2)\n", (False, 2)),
|
|
420
|
+
("marker = 1\n", (False, 0)),
|
|
421
|
+
("import sys\nsys.exit('stop')\n", (False, 0)),
|
|
422
|
+
("raise ValueError('probe broke')\n", (True, 0)),
|
|
423
|
+
],
|
|
424
|
+
)
|
|
425
|
+
def test_should_report_the_crash_flag_and_exit_code_of_a_hosted_hook(
|
|
426
|
+
tmp_path: Path,
|
|
427
|
+
capsys: pytest.CaptureFixture[str],
|
|
428
|
+
probe_source: str,
|
|
429
|
+
expected_outcome: tuple[bool, int],
|
|
430
|
+
) -> None:
|
|
431
|
+
dispatcher_module = sys.modules[aggregate_post_hosted_hook_results.__module__]
|
|
432
|
+
probe_path = tmp_path / "probe.py"
|
|
433
|
+
probe_path.write_text(probe_source, encoding="utf-8")
|
|
434
|
+
|
|
435
|
+
hosted_result = dispatcher_module.run_hosted_hook(
|
|
436
|
+
str(probe_path), [], "", is_blocking=True
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
assert (hosted_result.did_crash, hosted_result.exit_code) == expected_outcome
|
|
440
|
+
did_crash = expected_outcome[0]
|
|
441
|
+
assert ("probe broke" in capsys.readouterr().err) is did_crash
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def test_should_restore_stdin_stdout_and_argv_after_a_hosted_hook_crashes(
|
|
445
|
+
tmp_path: Path,
|
|
446
|
+
) -> None:
|
|
447
|
+
dispatcher_module = sys.modules[aggregate_post_hosted_hook_results.__module__]
|
|
448
|
+
probe_path = tmp_path / "crash_probe.py"
|
|
449
|
+
probe_path.write_text(
|
|
450
|
+
"import sys\nprint(sys.argv[1] + sys.stdin.read())\nraise ValueError('probe broke')\n",
|
|
451
|
+
encoding="utf-8",
|
|
452
|
+
)
|
|
453
|
+
stdin_before, stdout_before, argv_before = sys.stdin, sys.stdout, sys.argv
|
|
454
|
+
|
|
455
|
+
hosted_result = dispatcher_module.run_hosted_hook(
|
|
456
|
+
str(probe_path), ["tail"], "payload", is_blocking=True
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
assert sys.stdin is stdin_before
|
|
460
|
+
assert sys.stdout is stdout_before
|
|
461
|
+
assert sys.argv is argv_before
|
|
462
|
+
assert hosted_result == PostHostedHookResult(
|
|
463
|
+
captured_stdout="tailpayload\n", did_crash=True, is_blocking=True, exit_code=0
|
|
464
|
+
)
|