claude-dev-env 8.119.1 → 8.120.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/hooks/blocking/followup_pr_dedupe.py +71 -13
- package/hooks/blocking/gh_api_budget_gate.py +286 -0
- package/hooks/blocking/merge_review_gate.py +4 -0
- package/hooks/blocking/merge_review_gate_parts/create_folders.py +6 -2
- package/hooks/blocking/merge_review_gate_parts/open_request.py +40 -15
- package/hooks/blocking/resolve_thread_reply_gate.py +116 -1
- package/hooks/blocking/test_bash_dispatcher_interpreter_starts.py +8 -3
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +1 -0
- package/hooks/blocking/test_cloud_graphql_gate.py +4 -0
- package/hooks/blocking/test_followup_pr_dedupe.py +99 -0
- package/hooks/blocking/test_gh_api_budget_gate.py +602 -0
- package/hooks/blocking/test_gh_global_account_switch_gate.py +4 -0
- package/hooks/blocking/test_headless_claude_broker_gate.py +4 -0
- package/hooks/blocking/test_merge_review_gate.py +133 -0
- package/hooks/blocking/test_python_windows_invocation_gate.py +4 -0
- package/hooks/blocking/test_resolve_thread_reply_gate.py +430 -0
- package/hooks/blocking/tests/test_create_folders.py +7 -0
- package/hooks/blocking/tests/test_open_request.py +21 -0
- package/hooks/features/bash-dispatchers.md +3 -0
- package/hooks/features/conduct-gates.md +3 -3
- package/hooks/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +4 -0
- package/hooks/hooks_constants/followup_pr_dedupe_constants.py +5 -0
- package/hooks/hooks_constants/gh_api_budget.py +186 -0
- package/hooks/hooks_constants/gh_api_budget_gate_constants.py +215 -0
- package/hooks/hooks_constants/gh_argument_lists.py +169 -0
- package/hooks/hooks_constants/gh_shell_loop_scan.py +386 -0
- package/hooks/hooks_constants/merge_review_gate_constants.py +5 -0
- package/hooks/hooks_constants/pytest_invocation.py +24 -9
- package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +21 -1
- package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +5 -1
- package/hooks/hooks_constants/test_gh_api_budget.py +125 -0
- package/hooks/hooks_constants/test_gh_argument_lists.py +49 -0
- package/hooks/hooks_constants/test_gh_shell_loop_scan.py +50 -0
- package/hooks/hooks_constants/test_pytest_invocation.py +25 -0
- package/hooks/hooks_constants/test_shell_command_mutation.py +11 -0
- package/hooks/hooks_constants/transcript_skill_scan_constants.py +22 -0
- package/hooks/session/test_npx_pele_guidance.py +4 -0
- package/hooks/test_transcript_skill_scan.py +72 -0
- package/hooks/transcript_skill_scan.py +138 -4
- package/hooks/validators/config/security_checks_constants.py +88 -0
- package/hooks/validators/security_checks.py +186 -1
- package/hooks/validators/test_security_checks.py +149 -0
- package/package.json +1 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""Tests for gh_api_budget, which reads the GitHub core REST limit through a five minute cache."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
_HOOKS_ROOT = Path(__file__).resolve().parent.parent
|
|
14
|
+
if str(_HOOKS_ROOT) not in sys.path:
|
|
15
|
+
sys.path.insert(0, str(_HOOKS_ROOT))
|
|
16
|
+
|
|
17
|
+
import hooks_constants.gh_api_budget as budget_reader
|
|
18
|
+
|
|
19
|
+
_RESET_EPOCH_SECONDS = 1_900_000_000
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class _RateLimitQueries:
|
|
23
|
+
"""Stand-in for the ``gh api rate_limit`` subprocess that counts its calls."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, limit: int, used: int) -> None:
|
|
26
|
+
self.call_count = 0
|
|
27
|
+
self.all_creation_flags: list[object] = []
|
|
28
|
+
self.stdout_text = json.dumps(
|
|
29
|
+
{"resources": {"core": {"limit": limit, "used": used, "reset": _RESET_EPOCH_SECONDS}}}
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
def __call__(
|
|
33
|
+
self, *all_arguments: object, **all_options: object
|
|
34
|
+
) -> subprocess.CompletedProcess[str]:
|
|
35
|
+
self.call_count += 1
|
|
36
|
+
self.all_creation_flags.append(all_options.get("creationflags"))
|
|
37
|
+
return subprocess.CompletedProcess(
|
|
38
|
+
args=all_arguments, returncode=0, stdout=self.stdout_text, stderr=""
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@pytest.fixture
|
|
43
|
+
def cache_path(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
|
|
44
|
+
"""Point the managed root at a temporary folder and return the cache file inside it."""
|
|
45
|
+
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))
|
|
46
|
+
return tmp_path / "cache" / "gh_api_budget.json"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@pytest.fixture
|
|
50
|
+
def rate_queries(monkeypatch: pytest.MonkeyPatch) -> _RateLimitQueries:
|
|
51
|
+
"""Replace the rate limit subprocess with a stand-in that reports 4200 of 5000 used."""
|
|
52
|
+
queries = _RateLimitQueries(limit=5000, used=4200)
|
|
53
|
+
monkeypatch.setattr(budget_reader.subprocess, "run", queries)
|
|
54
|
+
return queries
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _write_cache(cache_path: Path, read_at: float, used: int) -> None:
|
|
58
|
+
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
|
59
|
+
cache_path.write_text(
|
|
60
|
+
json.dumps(
|
|
61
|
+
{"read_at": read_at, "limit": 5000, "used": used, "reset": _RESET_EPOCH_SECONDS}
|
|
62
|
+
),
|
|
63
|
+
encoding="utf-8",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@pytest.mark.parametrize(
|
|
68
|
+
("limit", "used", "is_spent"),
|
|
69
|
+
[(5000, 3999, False), (5000, 4000, True), (60, 48, True), (60, 47, False), (0, 0, False)],
|
|
70
|
+
)
|
|
71
|
+
def test_should_count_the_budget_spent_at_four_fifths(
|
|
72
|
+
limit: int, used: int, is_spent: bool
|
|
73
|
+
) -> None:
|
|
74
|
+
budget = budget_reader.CoreRateBudget(limit=limit, used=used, reset=None)
|
|
75
|
+
assert budget_reader.is_core_budget_spent(budget) is is_spent
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_should_read_a_fresh_cache_without_a_query(
|
|
79
|
+
cache_path: Path, rate_queries: _RateLimitQueries
|
|
80
|
+
) -> None:
|
|
81
|
+
_write_cache(cache_path, time.time(), used=4000)
|
|
82
|
+
assert budget_reader.read_core_rate_budget() == budget_reader.CoreRateBudget(
|
|
83
|
+
limit=5000, used=4000, reset=_RESET_EPOCH_SECONDS
|
|
84
|
+
)
|
|
85
|
+
assert rate_queries.call_count == 0
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_should_query_once_for_a_stale_cache_and_store_the_answer(
|
|
89
|
+
cache_path: Path, rate_queries: _RateLimitQueries
|
|
90
|
+
) -> None:
|
|
91
|
+
_write_cache(cache_path, time.time() - 301, used=10)
|
|
92
|
+
expected_budget = budget_reader.CoreRateBudget(
|
|
93
|
+
limit=5000, used=4200, reset=_RESET_EPOCH_SECONDS
|
|
94
|
+
)
|
|
95
|
+
assert budget_reader.read_core_rate_budget() == expected_budget
|
|
96
|
+
assert budget_reader.read_core_rate_budget() == expected_budget
|
|
97
|
+
assert rate_queries.call_count == 1
|
|
98
|
+
assert json.loads(cache_path.read_text(encoding="utf-8"))["used"] == 4200
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_should_start_the_rate_limit_query_without_a_console_window(
|
|
102
|
+
cache_path: Path, rate_queries: _RateLimitQueries, monkeypatch: pytest.MonkeyPatch
|
|
103
|
+
) -> None:
|
|
104
|
+
windows_no_console_flag = 0x08000000
|
|
105
|
+
monkeypatch.setattr(
|
|
106
|
+
budget_reader, "hidden_window_creation_flags", lambda: windows_no_console_flag
|
|
107
|
+
)
|
|
108
|
+
budget_reader.read_core_rate_budget()
|
|
109
|
+
assert rate_queries.all_creation_flags == [windows_no_console_flag]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_should_return_none_and_cache_the_failure_when_gh_is_missing(
|
|
113
|
+
cache_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
114
|
+
) -> None:
|
|
115
|
+
all_calls: list[object] = []
|
|
116
|
+
|
|
117
|
+
def _missing_gh(*all_arguments: object, **all_options: object) -> None:
|
|
118
|
+
all_calls.append(all_arguments)
|
|
119
|
+
raise FileNotFoundError("gh")
|
|
120
|
+
|
|
121
|
+
monkeypatch.setattr(budget_reader.subprocess, "run", _missing_gh)
|
|
122
|
+
assert budget_reader.read_core_rate_budget() is None
|
|
123
|
+
assert budget_reader.read_core_rate_budget() is None
|
|
124
|
+
assert len(all_calls) == 1
|
|
125
|
+
assert json.loads(cache_path.read_text(encoding="utf-8"))["limit"] is None
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Pin how gh_argument_lists finds the gh calls a shell command runs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from hooks_constants import gh_argument_lists
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.mark.parametrize(
|
|
11
|
+
("command", "all_expected_argument_lists"),
|
|
12
|
+
[
|
|
13
|
+
("gh pr view 12", [["pr", "view", "12"]]),
|
|
14
|
+
("$s = gh run watch 1", [["run", "watch", "1"]]),
|
|
15
|
+
("{gh run view 1}", [["run", "view", "1", "}"]]),
|
|
16
|
+
("watch -n 5 gh run view 1", [["run", "view", "1"]]),
|
|
17
|
+
('bash -c "gh run view 1"', [["run", "view", "1"]]),
|
|
18
|
+
('echo "gh run watch"', []),
|
|
19
|
+
("git status", []),
|
|
20
|
+
],
|
|
21
|
+
)
|
|
22
|
+
def test_should_return_the_arguments_of_each_gh_call(
|
|
23
|
+
command: str, all_expected_argument_lists: list[list[str]]
|
|
24
|
+
) -> None:
|
|
25
|
+
assert gh_argument_lists.all_gh_call_argument_lists(command) == all_expected_argument_lists
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_should_split_a_segment_at_each_glued_brace() -> None:
|
|
29
|
+
assert gh_argument_lists.brace_split_segments(["{gh", "run", "view", "1}"]) == [
|
|
30
|
+
["{", "gh", "run", "view", "1", "}"]
|
|
31
|
+
]
|
|
32
|
+
assert gh_argument_lists.brace_split_segments(["${name}"]) == [["${name}"]]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@pytest.mark.parametrize(
|
|
36
|
+
("all_arguments", "expected_interval_and_command"),
|
|
37
|
+
[
|
|
38
|
+
(["-n", "5", "gh", "run", "view", "1"], (5.0, "gh run view 1")),
|
|
39
|
+
(["--interval=30", "gh", "pr", "view"], (30.0, "gh pr view")),
|
|
40
|
+
(["gh", "run", "view", "1"], (2.0, "gh run view 1")),
|
|
41
|
+
(["-n", "$delay", "gh", "run", "view"], (None, "gh run view")),
|
|
42
|
+
],
|
|
43
|
+
)
|
|
44
|
+
def test_should_read_the_watch_interval_and_the_command_it_runs(
|
|
45
|
+
all_arguments: list[str], expected_interval_and_command: tuple[float | None, str]
|
|
46
|
+
) -> None:
|
|
47
|
+
assert (
|
|
48
|
+
gh_argument_lists.watch_interval_and_command(all_arguments) == expected_interval_and_command
|
|
49
|
+
)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Pin how gh_shell_loop_scan reads shell loops and ``watch`` calls that poll gh."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from hooks_constants import gh_shell_loop_scan
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.mark.parametrize(
|
|
11
|
+
("command_text", "is_tight"),
|
|
12
|
+
[
|
|
13
|
+
("while :; do gh run view 1; sleep 30; done", True),
|
|
14
|
+
("while ($true) { gh run view 1 }", True),
|
|
15
|
+
("for (;;) { gh run view 1; Start-Sleep -Milliseconds 500 }", True),
|
|
16
|
+
("while :; do gh run view 1; sleep .5; done", True),
|
|
17
|
+
("while ($true) { gh run view 1; Start-Sleep -Seconds 5 -Verbose }", True),
|
|
18
|
+
("while ($true) { gh run view 1; Start-Sleep -Duration 00:00:05 }", True),
|
|
19
|
+
("while ($true) { gh run view 1; Start-Sleep -Duration 00:01:30 }", False),
|
|
20
|
+
("while :; do gh run view 1; sleep 5 2>/dev/null; done", True),
|
|
21
|
+
("while :; do gh run view 1; sleep 5 > /dev/null; done", True),
|
|
22
|
+
("while :; do gh run view 1; sleep 90 2>/dev/null; done", False),
|
|
23
|
+
("while :; do gh run view 1; sleep `cat delay`; done", False),
|
|
24
|
+
("while :; do gh run view 1; sleep 30; sleep 30; done", False),
|
|
25
|
+
("while :; do gh run view 1; sleep 1m; done", False),
|
|
26
|
+
("for x in a b c; do gh pr view $x; done", False),
|
|
27
|
+
("while :; do gh run view 1; sleep $delay; done", False),
|
|
28
|
+
("while :; do date; sleep 5; done", False),
|
|
29
|
+
],
|
|
30
|
+
)
|
|
31
|
+
def test_should_flag_a_loop_that_calls_gh_with_too_little_sleep(
|
|
32
|
+
command_text: str, is_tight: bool
|
|
33
|
+
) -> None:
|
|
34
|
+
assert gh_shell_loop_scan.is_tight_gh_loop_text(command_text) is is_tight
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@pytest.mark.parametrize(
|
|
38
|
+
("all_segment_tokens", "is_tight"),
|
|
39
|
+
[
|
|
40
|
+
(["watch", "-n", "5", "gh", "run", "view", "1"], True),
|
|
41
|
+
(["watch", "gh", "run", "view", "1"], True),
|
|
42
|
+
(["watch", "-n", "60", "gh", "run", "view", "1"], False),
|
|
43
|
+
(["watch", "-n", "5", "date"], False),
|
|
44
|
+
(["gh", "run", "view", "1"], False),
|
|
45
|
+
],
|
|
46
|
+
)
|
|
47
|
+
def test_should_flag_a_watch_on_gh_under_sixty_seconds(
|
|
48
|
+
all_segment_tokens: list[str], is_tight: bool
|
|
49
|
+
) -> None:
|
|
50
|
+
assert gh_shell_loop_scan.is_tight_gh_watch_segment(all_segment_tokens) is is_tight
|
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import pytest
|
|
6
6
|
|
|
7
7
|
from hooks_constants.pytest_invocation import (
|
|
8
|
+
all_tokens_after_wrappers,
|
|
8
9
|
segment_reports_a_pytest_exit_code,
|
|
9
10
|
segment_runs_pytest,
|
|
10
11
|
)
|
|
@@ -134,3 +135,27 @@ def test_should_not_false_classify_option_value_named_pytest() -> None:
|
|
|
134
135
|
def test_should_not_false_classify_script_argument_named_pytest() -> None:
|
|
135
136
|
all_tokens = _first_segment_tokens("python myscript.py -m pytest")
|
|
136
137
|
assert segment_runs_pytest(all_tokens) is False
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@pytest.mark.parametrize(
|
|
141
|
+
("all_segment_tokens", "all_expected_tokens"),
|
|
142
|
+
[
|
|
143
|
+
(["nohup", "env", "-u", "GH_REPO", "gh", "pr"], ["gh", "pr"]),
|
|
144
|
+
(["GH_REPO=o/r", "nice", "env", "-u", "GH_REPO", "gh", "pr"], ["gh", "pr"]),
|
|
145
|
+
(
|
|
146
|
+
["nice", "-n", "5", "env", "-u", "GH_REPO", "stdbuf", "-o0", "gh", "pr"],
|
|
147
|
+
["stdbuf", "-o0", "gh", "pr"],
|
|
148
|
+
),
|
|
149
|
+
(["stdbuf", "-o0", "env", "-u", "FOO", "gh", "pr"], ["gh", "pr"]),
|
|
150
|
+
(["time", "nohup", "env", "--unset", "FOO", "nice", "gh", "pr"], ["nice", "gh", "pr"]),
|
|
151
|
+
(["env", "nice", "env", "-u", "FOO", "gh", "pr"], ["gh", "pr"]),
|
|
152
|
+
(["nohup", "timeout", "-s", "KILL", "60", "gh", "pr"], ["gh", "pr"]),
|
|
153
|
+
(["setsid", "sudo", "-u", "ci", "gh", "pr"], ["gh", "pr"]),
|
|
154
|
+
(["nohup", "gh", "pr"], ["nohup", "gh", "pr"]),
|
|
155
|
+
(["gh", "pr"], ["gh", "pr"]),
|
|
156
|
+
],
|
|
157
|
+
)
|
|
158
|
+
def test_all_tokens_after_wrappers_should_step_over_an_option_reading_wrapper_behind_launchers(
|
|
159
|
+
all_segment_tokens: list[str], all_expected_tokens: list[str]
|
|
160
|
+
) -> None:
|
|
161
|
+
assert all_tokens_after_wrappers(all_segment_tokens) == all_expected_tokens
|
|
@@ -22,6 +22,17 @@ def test_should_read_a_write_as_mutating(command: str) -> None:
|
|
|
22
22
|
assert is_mutating_shell_command(command) is True
|
|
23
23
|
|
|
24
24
|
|
|
25
|
+
@pytest.mark.parametrize(
|
|
26
|
+
"command",
|
|
27
|
+
[
|
|
28
|
+
"nohup env -u FOO git push origin main",
|
|
29
|
+
"time -p env -u HOME bash -c 'touch x'",
|
|
30
|
+
],
|
|
31
|
+
)
|
|
32
|
+
def test_should_read_a_write_behind_a_launcher_and_env_unset_as_mutating(command: str) -> None:
|
|
33
|
+
assert is_mutating_shell_command(command) is True
|
|
34
|
+
|
|
35
|
+
|
|
25
36
|
@pytest.mark.parametrize(
|
|
26
37
|
"command",
|
|
27
38
|
[
|
|
@@ -10,6 +10,28 @@ META_ENTRY_FIELD = "isMeta"
|
|
|
10
10
|
TOOL_RESULT_BLOCK_TYPE = "tool_result"
|
|
11
11
|
IS_ERROR_FIELD = "is_error"
|
|
12
12
|
TOOL_USE_ID_FIELD = "tool_use_id"
|
|
13
|
+
ORIGIN_FIELD = "origin"
|
|
14
|
+
ORIGIN_KIND_FIELD = "kind"
|
|
15
|
+
TASK_NOTIFICATION_ORIGIN_KIND = "task-notification"
|
|
16
|
+
RUN_IN_BACKGROUND_INPUT_KEY = "run_in_background"
|
|
17
|
+
MOVED_TO_BACKGROUND_RESULT_PATTERN = (
|
|
18
|
+
r"(?m)^Command (?:running in background with ID: |was manually backgrounded by user with ID: "
|
|
19
|
+
r"|did not complete within [^\n]*\bmoved to the background\b)"
|
|
20
|
+
)
|
|
21
|
+
ALL_BACKGROUND_SHELL_TOOL_NAMES = frozenset({"Bash", "PowerShell"})
|
|
22
|
+
ATTACHMENT_ENTRY_TYPE = "attachment"
|
|
23
|
+
ATTACHMENT_FIELD = "attachment"
|
|
24
|
+
QUEUED_COMMAND_ATTACHMENT_TYPE = "queued_command"
|
|
25
|
+
QUEUED_COMMAND_PROMPT_FIELD = "prompt"
|
|
26
|
+
RESULT_TEXT_FIELD = "text"
|
|
27
|
+
RESULT_TEXT_SEPARATOR = "\n"
|
|
28
|
+
TASK_NOTIFICATION_OPENING_TAG = "<task-notification>"
|
|
29
|
+
TASK_NOTIFICATION_CLOSING_TAG = "</task-notification>"
|
|
30
|
+
TASK_NOTIFICATION_SUMMARY_OPENING_TAG = "<summary>"
|
|
31
|
+
TASK_NOTIFICATION_TOOL_USE_ID_PATTERN = r"<tool-use-id>\s*([^<\s]+)\s*</tool-use-id>"
|
|
32
|
+
TASK_NOTIFICATION_COMPLETED_STATUS_PATTERN = r"<status>\s*completed\s*</status>"
|
|
33
|
+
TASK_NOTIFICATION_ANY_STATUS_PATTERN = r"<status>"
|
|
34
|
+
TASK_NOTIFICATION_EXIT_ZERO_ENDING_PATTERN = r" completed \(exit code 0\)\s*</summary>\s*</task-notification>\s*\Z"
|
|
13
35
|
NEWLINE_BYTE = b"\n"
|
|
14
36
|
CARRIAGE_RETURN_BYTE = b"\r"
|
|
15
37
|
ALL_LINE_BREAK_BYTES = (NEWLINE_BYTE, CARRIAGE_RETURN_BYTE)
|
|
@@ -102,6 +102,10 @@ def test_npx_pele_commands_match(command_text: str) -> None:
|
|
|
102
102
|
assert npx_pele_guidance.is_npx_pele_command({"command": command_text})
|
|
103
103
|
|
|
104
104
|
|
|
105
|
+
def test_should_match_npx_pele_behind_a_launcher_and_env_unset() -> None:
|
|
106
|
+
assert npx_pele_guidance.is_npx_pele_command({"command": "nohup env -u FOO npx pele a.mmd"})
|
|
107
|
+
|
|
108
|
+
|
|
105
109
|
@pytest.mark.parametrize(
|
|
106
110
|
"command_text",
|
|
107
111
|
[
|
|
@@ -232,6 +232,78 @@ def should_list_each_tool_call_since_the_last_prompt_with_its_outcome() -> None:
|
|
|
232
232
|
]
|
|
233
233
|
|
|
234
234
|
|
|
235
|
+
def should_count_a_background_call_only_after_its_notice_reports_exit_code_zero() -> None:
|
|
236
|
+
background_input: dict[str, object] = {"command": "sleep 1", "run_in_background": True}
|
|
237
|
+
|
|
238
|
+
def _call(tool_use_id: str) -> str:
|
|
239
|
+
tool_use = {"type": "tool_use", "id": tool_use_id, "name": "Bash", "input": background_input}
|
|
240
|
+
return json.dumps({"type": "assistant", "message": {"content": [tool_use]}})
|
|
241
|
+
|
|
242
|
+
def _started(tool_use_id: str) -> str:
|
|
243
|
+
tool_result = {"type": "tool_result", "tool_use_id": tool_use_id, "is_error": False, "content": "started"}
|
|
244
|
+
return json.dumps({"type": "user", "message": {"content": [tool_result]}})
|
|
245
|
+
|
|
246
|
+
def _notice(tool_use_id: str, status: str, exit_code: int) -> str:
|
|
247
|
+
text = (
|
|
248
|
+
f"<task-notification><tool-use-id>{tool_use_id}</tool-use-id><status>{status}</status>"
|
|
249
|
+
f"<summary>Background command {status} (exit code {exit_code})</summary></task-notification>"
|
|
250
|
+
)
|
|
251
|
+
return json.dumps({"type": "user", "origin": {"kind": "task-notification"}, "message": {"content": text}})
|
|
252
|
+
|
|
253
|
+
lines = [
|
|
254
|
+
json.dumps({"type": "user", "message": {"content": "First."}}),
|
|
255
|
+
_call("done"),
|
|
256
|
+
_started("done"),
|
|
257
|
+
_call("failed"),
|
|
258
|
+
_started("failed"),
|
|
259
|
+
_call("running"),
|
|
260
|
+
_started("running"),
|
|
261
|
+
_notice("done", "completed", 0),
|
|
262
|
+
_notice("failed", "failed", 3),
|
|
263
|
+
]
|
|
264
|
+
assert tool_calls_since_last_prompt(lines) == [
|
|
265
|
+
TranscriptToolCall("Bash", background_input, True),
|
|
266
|
+
TranscriptToolCall("Bash", background_input, False),
|
|
267
|
+
TranscriptToolCall("Bash", background_input, False),
|
|
268
|
+
]
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def should_read_queued_command_attachments_and_calls_moved_to_the_background() -> None:
|
|
272
|
+
def _call(tool_use_id: str, tool_input: dict[str, object]) -> str:
|
|
273
|
+
tool_use = {"type": "tool_use", "id": tool_use_id, "name": "Bash", "input": tool_input}
|
|
274
|
+
return json.dumps({"type": "assistant", "message": {"content": [tool_use]}})
|
|
275
|
+
|
|
276
|
+
def _result(tool_use_id: str, text: str) -> str:
|
|
277
|
+
tool_result = {"type": "tool_result", "tool_use_id": tool_use_id, "is_error": False, "content": text}
|
|
278
|
+
return json.dumps({"type": "user", "message": {"content": [tool_result]}})
|
|
279
|
+
|
|
280
|
+
def _queued(text: str, origin_kind: str) -> str:
|
|
281
|
+
attachment = {"type": "queued_command", "prompt": text, "origin": {"kind": origin_kind}}
|
|
282
|
+
return json.dumps({"type": "attachment", "attachment": attachment})
|
|
283
|
+
|
|
284
|
+
def _notice(tool_use_id: str) -> str:
|
|
285
|
+
return (
|
|
286
|
+
f"<task-notification><tool-use-id>{tool_use_id}</tool-use-id><status>completed</status>"
|
|
287
|
+
'<summary>Background command "x" completed (exit code 0)</summary></task-notification>'
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
all_turn_lines = [
|
|
291
|
+
json.dumps({"type": "user", "message": {"content": "First."}}),
|
|
292
|
+
_call("flag-text", {"run_in_background": "true"}),
|
|
293
|
+
_result("flag-text", "Command running in background with ID: b1"),
|
|
294
|
+
_call("moved", {}),
|
|
295
|
+
_result("moved", "Command did not complete within its 120s timeout and was moved to the background."),
|
|
296
|
+
_call("waiting", {"run_in_background": 1}),
|
|
297
|
+
_result("waiting", "Command running in background with ID: b3"),
|
|
298
|
+
_call("plain", {}),
|
|
299
|
+
_result("plain", "done"),
|
|
300
|
+
_queued(_notice("flag-text"), "task-notification"),
|
|
301
|
+
_queued(_notice("moved"), "task-notification"),
|
|
302
|
+
]
|
|
303
|
+
assert [each_call.succeeded for each_call in tool_calls_since_last_prompt(all_turn_lines)] == [True, True, False, True]
|
|
304
|
+
assert tool_calls_since_last_prompt([*all_turn_lines, _queued("Stop.", "human")]) == []
|
|
305
|
+
|
|
306
|
+
|
|
235
307
|
NEWEST_FIRST_SKILL_NAMES = ("pr-lifecycle",)
|
|
236
308
|
NEWEST_FIRST_MARKERS = ("<command-name>/pr-lifecycle</command-name>",)
|
|
237
309
|
ALL_GENERATED_LINE_KINDS = (
|
|
@@ -17,14 +17,33 @@ from typing import BinaryIO, NamedTuple
|
|
|
17
17
|
from hooks_constants.transcript_skill_scan_constants import (
|
|
18
18
|
ALL_LINE_BREAK_BYTES,
|
|
19
19
|
ASSISTANT_ENTRY_TYPE,
|
|
20
|
+
ALL_BACKGROUND_SHELL_TOOL_NAMES,
|
|
21
|
+
ATTACHMENT_ENTRY_TYPE,
|
|
22
|
+
ATTACHMENT_FIELD,
|
|
20
23
|
CARRIAGE_RETURN_BYTE,
|
|
21
24
|
COMMAND_ARGUMENTS_PATTERN,
|
|
22
25
|
COMPACT_BOUNDARY_SUBTYPE,
|
|
23
26
|
FORWARD_COPY_BYTE_CAP,
|
|
24
27
|
IS_ERROR_FIELD,
|
|
25
28
|
META_ENTRY_FIELD,
|
|
29
|
+
MOVED_TO_BACKGROUND_RESULT_PATTERN,
|
|
26
30
|
NEWLINE_BYTE,
|
|
31
|
+
ORIGIN_FIELD,
|
|
32
|
+
ORIGIN_KIND_FIELD,
|
|
33
|
+
QUEUED_COMMAND_ATTACHMENT_TYPE,
|
|
34
|
+
QUEUED_COMMAND_PROMPT_FIELD,
|
|
35
|
+
RESULT_TEXT_FIELD,
|
|
36
|
+
RESULT_TEXT_SEPARATOR,
|
|
37
|
+
RUN_IN_BACKGROUND_INPUT_KEY,
|
|
27
38
|
SKILL_TOOL_NAME,
|
|
39
|
+
TASK_NOTIFICATION_ORIGIN_KIND,
|
|
40
|
+
TASK_NOTIFICATION_ANY_STATUS_PATTERN,
|
|
41
|
+
TASK_NOTIFICATION_CLOSING_TAG,
|
|
42
|
+
TASK_NOTIFICATION_COMPLETED_STATUS_PATTERN,
|
|
43
|
+
TASK_NOTIFICATION_EXIT_ZERO_ENDING_PATTERN,
|
|
44
|
+
TASK_NOTIFICATION_OPENING_TAG,
|
|
45
|
+
TASK_NOTIFICATION_SUMMARY_OPENING_TAG,
|
|
46
|
+
TASK_NOTIFICATION_TOOL_USE_ID_PATTERN,
|
|
28
47
|
TOOL_RESULT_BLOCK_TYPE,
|
|
29
48
|
TRANSCRIPT_BLOCK_BYTES,
|
|
30
49
|
TRANSCRIPT_LINE_BYTE_CAP,
|
|
@@ -523,6 +542,83 @@ def _succeeded_result_ids(all_entry_fields: dict[str, object]) -> set[object]:
|
|
|
523
542
|
}
|
|
524
543
|
|
|
525
544
|
|
|
545
|
+
def _queued_command(all_entry_fields: dict[str, object]) -> dict[str, object] | None:
|
|
546
|
+
attachment = all_entry_fields.get(ATTACHMENT_FIELD)
|
|
547
|
+
if all_entry_fields.get("type") != ATTACHMENT_ENTRY_TYPE or not isinstance(attachment, dict):
|
|
548
|
+
return None
|
|
549
|
+
return attachment if attachment.get("type") == QUEUED_COMMAND_ATTACHMENT_TYPE else None
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def _is_task_notification_origin(all_fields: dict[str, object]) -> bool:
|
|
553
|
+
origin = all_fields.get(ORIGIN_FIELD)
|
|
554
|
+
return isinstance(origin, dict) and origin.get(ORIGIN_KIND_FIELD) == TASK_NOTIFICATION_ORIGIN_KIND
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
def _task_notification_text(all_entry_fields: dict[str, object]) -> str | None:
|
|
558
|
+
queued_command = _queued_command(all_entry_fields)
|
|
559
|
+
if queued_command is not None:
|
|
560
|
+
if not _is_task_notification_origin(queued_command):
|
|
561
|
+
return None
|
|
562
|
+
queued_text = queued_command.get(QUEUED_COMMAND_PROMPT_FIELD)
|
|
563
|
+
return queued_text if isinstance(queued_text, str) else ""
|
|
564
|
+
if not _is_task_notification_origin(all_entry_fields):
|
|
565
|
+
return None
|
|
566
|
+
return _user_command_text(all_entry_fields) or ""
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
def _result_block_text(result_content: object) -> str:
|
|
570
|
+
if isinstance(result_content, str):
|
|
571
|
+
return result_content
|
|
572
|
+
all_parts = result_content if isinstance(result_content, list) else []
|
|
573
|
+
return RESULT_TEXT_SEPARATOR.join(
|
|
574
|
+
each_part[RESULT_TEXT_FIELD]
|
|
575
|
+
for each_part in all_parts
|
|
576
|
+
if isinstance(each_part, dict) and isinstance(each_part.get(RESULT_TEXT_FIELD), str)
|
|
577
|
+
)
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def _moved_to_background_result_ids(all_entry_fields: dict[str, object]) -> set[object]:
|
|
581
|
+
return {
|
|
582
|
+
each_block.get(TOOL_USE_ID_FIELD)
|
|
583
|
+
for each_block in _content_blocks(all_entry_fields, USER_ENTRY_TYPE)
|
|
584
|
+
if each_block.get("type") == TOOL_RESULT_BLOCK_TYPE
|
|
585
|
+
and re.search(MOVED_TO_BACKGROUND_RESULT_PATTERN, _result_block_text(each_block.get("content"))) is not None
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def _background_ids_finished_without_error(notification_text: str) -> set[object]:
|
|
590
|
+
"""Return the tool use id of a notice that reports exit code 0, as a set of one, else an empty set.
|
|
591
|
+
|
|
592
|
+
::
|
|
593
|
+
|
|
594
|
+
one notice, status completed, summary ends "(exit code 0)" -> {its id}
|
|
595
|
+
one notice, status failed, summary ends "(exit code 3)" -> {}
|
|
596
|
+
two notices in one text, either outcome -> {}
|
|
597
|
+
a failed notice whose summary quotes "(exit code 0)" -> {}
|
|
598
|
+
a failed notice whose summary quotes a whole passing notice -> {}
|
|
599
|
+
|
|
600
|
+
The harness writes the id and the status before the summary, and the exit
|
|
601
|
+
code as the last words of the summary. The summary also quotes the
|
|
602
|
+
command's description, which the agent wrote, so only the text before the
|
|
603
|
+
summary and the very end of the notice are read.
|
|
604
|
+
"""
|
|
605
|
+
if (
|
|
606
|
+
notification_text.count(TASK_NOTIFICATION_OPENING_TAG) != 1
|
|
607
|
+
or notification_text.count(TASK_NOTIFICATION_CLOSING_TAG) != 1
|
|
608
|
+
):
|
|
609
|
+
return set()
|
|
610
|
+
harness_header = notification_text.partition(TASK_NOTIFICATION_SUMMARY_OPENING_TAG)[0]
|
|
611
|
+
all_tool_use_ids = re.findall(TASK_NOTIFICATION_TOOL_USE_ID_PATTERN, harness_header)
|
|
612
|
+
if (
|
|
613
|
+
len(all_tool_use_ids) != 1
|
|
614
|
+
or len(re.findall(TASK_NOTIFICATION_ANY_STATUS_PATTERN, harness_header)) != 1
|
|
615
|
+
or re.search(TASK_NOTIFICATION_COMPLETED_STATUS_PATTERN, harness_header) is None
|
|
616
|
+
or re.search(TASK_NOTIFICATION_EXIT_ZERO_ENDING_PATTERN, notification_text) is None
|
|
617
|
+
):
|
|
618
|
+
return set()
|
|
619
|
+
return set(all_tool_use_ids)
|
|
620
|
+
|
|
621
|
+
|
|
526
622
|
def tool_calls_since_last_prompt(all_transcript_lines: Iterable[str]) -> list[TranscriptToolCall]:
|
|
527
623
|
"""Return the tool calls after the last user prompt, in order, each marked by whether it succeeded.
|
|
528
624
|
|
|
@@ -532,23 +628,61 @@ def tool_calls_since_last_prompt(all_transcript_lines: Iterable[str]) -> list[Tr
|
|
|
532
628
|
prompt, reply call, its result with is_error -> [reply failed]
|
|
533
629
|
prompt, resolve call with no result yet -> [resolve failed]
|
|
534
630
|
reply call and result, then a new prompt -> []
|
|
631
|
+
reply call and result, then a queued prompt -> []
|
|
632
|
+
background call, started -> [call failed]
|
|
633
|
+
background call, notice of exit code 0 -> [call succeeded]
|
|
634
|
+
background call, notice of exit code 3 -> [call failed]
|
|
635
|
+
shell call whose result is the harness's own line -> [call failed] until its notice
|
|
636
|
+
any other call whose result holds those words -> [call succeeded]
|
|
535
637
|
|
|
536
638
|
A hook that denies a call leaves an errored result, so a denied call does
|
|
537
|
-
not count as a success. A tool result leaves the turn open.
|
|
639
|
+
not count as a success. A tool result leaves the turn open. A background
|
|
640
|
+
task notice is no user prompt, so it leaves the turn open too, whether it
|
|
641
|
+
is a user entry or a queued command attachment. Any other queued command
|
|
642
|
+
attachment, such as a prompt the user sent mid-turn, ends the turn as a
|
|
643
|
+
user prompt does. A call runs in the background when its
|
|
644
|
+
``run_in_background`` input is truthy, or when it is a ``Bash`` or
|
|
645
|
+
``PowerShell`` call whose result holds a line the harness writes for a
|
|
646
|
+
command in the background, starting ``Command running in background with
|
|
647
|
+
ID:``, ``Command was manually backgrounded by user with ID:`` or ``Command
|
|
648
|
+
did not complete within``. The same words in the result of any other tool,
|
|
649
|
+
or elsewhere in a line, change nothing. Such a call succeeds only
|
|
650
|
+
when the notice that names its tool use id reports a completed status
|
|
651
|
+
with exit code 0.
|
|
538
652
|
|
|
539
653
|
Args:
|
|
540
654
|
all_transcript_lines: JSON transcript entries, one per line.
|
|
541
655
|
"""
|
|
542
656
|
all_calls: list[tuple[object, str, dict[str, object]]] = []
|
|
543
657
|
all_succeeded_ids: set[object] = set()
|
|
658
|
+
all_finished_background_ids: set[object] = set()
|
|
659
|
+
all_moved_to_background_ids: set[object] = set()
|
|
544
660
|
all_entries = (each_entry for each_entry in map(_parsed_entry, all_transcript_lines) if each_entry is not None)
|
|
545
661
|
for each_entry_fields in all_entries:
|
|
546
|
-
|
|
547
|
-
|
|
662
|
+
notification_text = _task_notification_text(each_entry_fields)
|
|
663
|
+
if notification_text is not None:
|
|
664
|
+
all_finished_background_ids.update(_background_ids_finished_without_error(notification_text))
|
|
665
|
+
continue
|
|
666
|
+
if _is_user_prompt(each_entry_fields) or _queued_command(each_entry_fields) is not None:
|
|
667
|
+
all_calls, all_succeeded_ids, all_finished_background_ids = [], set(), set()
|
|
668
|
+
all_moved_to_background_ids = set()
|
|
548
669
|
continue
|
|
549
670
|
all_calls.extend(_tool_use_calls(each_entry_fields))
|
|
550
671
|
all_succeeded_ids.update(_succeeded_result_ids(each_entry_fields))
|
|
672
|
+
all_moved_to_background_ids.update(_moved_to_background_result_ids(each_entry_fields))
|
|
551
673
|
return [
|
|
552
|
-
TranscriptToolCall(
|
|
674
|
+
TranscriptToolCall(
|
|
675
|
+
each_name,
|
|
676
|
+
each_input,
|
|
677
|
+
each_id is not None
|
|
678
|
+
and each_id in all_succeeded_ids
|
|
679
|
+
and (
|
|
680
|
+
each_id in all_finished_background_ids
|
|
681
|
+
or not (
|
|
682
|
+
each_input.get(RUN_IN_BACKGROUND_INPUT_KEY)
|
|
683
|
+
or (each_name in ALL_BACKGROUND_SHELL_TOOL_NAMES and each_id in all_moved_to_background_ids)
|
|
684
|
+
)
|
|
685
|
+
),
|
|
686
|
+
)
|
|
553
687
|
for each_id, each_name, each_input in all_calls
|
|
554
688
|
]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Constants for the hardcoded-secret value judgment in security_checks."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
MINIMUM_MESSAGE_WORD_COUNT: int = 4
|
|
8
|
+
|
|
9
|
+
MAXIMUM_ORDINARY_WORD_LENGTH: int = 14
|
|
10
|
+
|
|
11
|
+
MAXIMUM_CAPITALS_WORD_LENGTH: int = 5
|
|
12
|
+
|
|
13
|
+
MAXIMUM_CAPITALS_WORD_COUNT: int = 1
|
|
14
|
+
|
|
15
|
+
MINIMUM_GROUP_WORD_LENGTH: int = 4
|
|
16
|
+
|
|
17
|
+
MAXIMUM_SHORT_WORD_LENGTH: int = 2
|
|
18
|
+
|
|
19
|
+
MINIMUM_GROUP_RUN_LENGTH: int = 3
|
|
20
|
+
|
|
21
|
+
MINIMUM_VARIABLE_SEGMENT_COUNT: int = 2
|
|
22
|
+
|
|
23
|
+
MINIMUM_FLAG_SEGMENT_COUNT: int = 1
|
|
24
|
+
|
|
25
|
+
WORD_SEPARATOR: str = " "
|
|
26
|
+
|
|
27
|
+
WORD_EDGE_PUNCTUATION: str = ".,;:!?()"
|
|
28
|
+
|
|
29
|
+
LABEL_CLOSING_MARK: str = ":"
|
|
30
|
+
|
|
31
|
+
NAME_SEGMENT_SEPARATOR: str = "_"
|
|
32
|
+
|
|
33
|
+
FLAG_SEGMENT_SEPARATOR: str = "-"
|
|
34
|
+
|
|
35
|
+
FLAG_LEAD: str = "--"
|
|
36
|
+
|
|
37
|
+
FORMAT_PLACEHOLDER_PATTERN: re.Pattern[str] = re.compile(
|
|
38
|
+
r"\{(?:[a-z]+(?:_[a-z]+)*)?(?:![rsa])?(?::[<>^]?[0-9,.]*[a-z%]?)?\}"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
UNPRONOUNCEABLE_RUN_PATTERN: re.Pattern[str] = re.compile(
|
|
42
|
+
r"[b-df-hj-np-tv-xz]{5,}", re.IGNORECASE
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
ALL_PASSWORD_NAME_WORDS: frozenset[str] = frozenset(
|
|
46
|
+
{"password", "passwd", "pwd", "passphrase", "passcode", "phrase", "answer"}
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
ALL_PASSWORD_NAME_SEGMENTS: frozenset[str] = frozenset({"pass"})
|
|
50
|
+
|
|
51
|
+
ALL_AUTH_SCHEME_LEAD_WORDS: frozenset[str] = frozenset(
|
|
52
|
+
{"bearer", "basic", "digest", "negotiate", "token", "key", "apikey", "bot"}
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
ALL_AUTH_SCHEME_WORDS: frozenset[str] = frozenset(
|
|
56
|
+
{"bearer", "basic", "digest", "negotiate"}
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
ALL_SENTENCE_FUNCTION_WORDS: frozenset[str] = frozenset(
|
|
60
|
+
{
|
|
61
|
+
"a", "an", "the", "is", "are", "was", "were", "be", "been", "no", "not",
|
|
62
|
+
"to", "of", "in", "on", "for", "with", "from", "by", "at", "and", "or",
|
|
63
|
+
"this", "that", "it", "before", "after", "has", "have", "can", "cannot",
|
|
64
|
+
"will", "when", "if", "as", "do", "so", "up",
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
ALL_POINTER_VALUE_WORDS: tuple[str, ...] = (
|
|
69
|
+
"token",
|
|
70
|
+
"key",
|
|
71
|
+
"secret",
|
|
72
|
+
"auth",
|
|
73
|
+
"password",
|
|
74
|
+
"credential",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
ALL_VARIABLE_NAME_SUFFIXES: tuple[str, ...] = (
|
|
78
|
+
"_env",
|
|
79
|
+
"_env_var",
|
|
80
|
+
"_env_name",
|
|
81
|
+
"_variable_name",
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
ALL_FLAG_NAME_SUFFIXES: tuple[str, ...] = ("_flag", "_option")
|
|
85
|
+
|
|
86
|
+
VARIABLE_NAME_SEGMENT_PATTERN: re.Pattern[str] = re.compile(r"[A-Z]{1,12}")
|
|
87
|
+
|
|
88
|
+
FLAG_SEGMENT_PATTERN: re.Pattern[str] = re.compile(r"[a-z]{1,12}")
|