claude-dev-env 8.114.8 → 8.114.9
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/.agents/skills/build-eval/reference/context-budget.md +2 -0
- package/hooks/blocking/final_reply_gate.py +4 -3
- package/hooks/blocking/merge_review_gate.py +98 -1
- package/hooks/blocking/owned_pr_merge_stop_gate.py +50 -20
- package/hooks/blocking/reply_length_gate.py +52 -10
- package/hooks/blocking/test_final_reply_gate.py +136 -0
- package/hooks/blocking/test_merge_review_gate.py +48 -0
- package/hooks/blocking/test_owned_pr_merge_stop_gate.py +97 -6
- package/hooks/blocking/test_reply_length_gate.py +39 -0
- package/hooks/context_budget/model.py +3 -0
- package/hooks/context_budget/policy_changes.py +54 -5
- package/hooks/context_budget/policy_file.py +1 -0
- package/hooks/context_budget/tests/test_model.py +1 -0
- package/hooks/context_budget/tests/test_policy_changes.py +121 -1
- package/hooks/context_budget/tests/test_policy_file.py +11 -0
- package/hooks/features/bash-dispatchers.md +2 -2
- package/hooks/features/conduct-gates.md +3 -3
- package/hooks/hooks_constants/context_budget_constants.py +1 -0
- package/hooks/hooks_constants/merge_review_gate_constants.py +28 -1
- package/hooks/hooks_constants/owned_pr_merge_stop_gate_constants.py +4 -1
- package/hooks/hooks_constants/owned_pr_review_loop_constants.py +4 -0
- package/hooks/hooks_constants/reply_length_gate_constants.py +17 -1
- package/hooks/hooks_constants/test_reply_length_gate_constants.py +20 -0
- package/hooks/hooks_constants/test_visual_reply_rules_constants.py +5 -0
- package/hooks/hooks_constants/visual_reply_rules_constants.py +11 -0
- package/hooks/owned_pr_session_record.py +76 -1
- package/hooks/test_owned_pr_session_record.py +26 -0
- package/hooks/test_visual_reply_rules.py +27 -0
- package/hooks/visual_reply_rules.py +64 -0
- package/package.json +1 -1
- package/scripts/tests/test_policy_lint_rules_context_budget.py +15 -0
|
@@ -42,6 +42,8 @@ A person never edits the baseline by hand. After a change shrinks listed files,
|
|
|
42
42
|
python packages/claude-dev-env/scripts/context_budget_check.py --write-baseline
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
A policy with no `baseline` key may gain its first list in one change. No file entry may exceed what the tree measures. After that, the list may only shrink, and deleting the key is a finding.
|
|
46
|
+
|
|
45
47
|
## Where it runs
|
|
46
48
|
|
|
47
49
|
- The `context-budget` and `context-budget-policy` rules in `cde_lint` run in CI on every pull request.
|
|
@@ -10,7 +10,8 @@ session ends its turn with, which reaches the user with no tool call::
|
|
|
10
10
|
ok: Run 18239174012 took 39 minutes. (a tool result shows the run)
|
|
11
11
|
|
|
12
12
|
It applies the banned words, the unvalidated claim pattern, and the cited
|
|
13
|
-
run, job, check, and pull request numbers from ``reply_length_gate.py``.
|
|
13
|
+
run, job, check, and pull request numbers from ``reply_length_gate.py``. A
|
|
14
|
+
pull request link also counts as shown when hook or harness text shows it. The
|
|
14
15
|
citation check is skipped when the transcript cannot be read. It also flags
|
|
15
16
|
a path outside code whose backslash the terminal drops, such as
|
|
16
17
|
``D:\\Reports\\.claude``. A block sends
|
|
@@ -46,7 +47,7 @@ if hooks_root_directory not in sys.path:
|
|
|
46
47
|
from reply_length_gate import (
|
|
47
48
|
banned_word_violation,
|
|
48
49
|
configured_banned_words,
|
|
49
|
-
|
|
50
|
+
unseen_citation_violation_with_hook_text,
|
|
50
51
|
unvalidated_claim_violation,
|
|
51
52
|
)
|
|
52
53
|
from open_tasks_transcript import replayed_transcript
|
|
@@ -272,7 +273,7 @@ def final_reply_violation(reply_text: str, transcript_path: object) -> str | Non
|
|
|
272
273
|
return (
|
|
273
274
|
banned_word_violation(reply_text, configured_banned_words())
|
|
274
275
|
or unvalidated_claim_violation(reply_text)
|
|
275
|
-
or
|
|
276
|
+
or unseen_citation_violation_with_hook_text(reply_text, transcript_path)
|
|
276
277
|
or windows_path_violation(reply_text)
|
|
277
278
|
)
|
|
278
279
|
|
|
@@ -15,6 +15,7 @@ link to the posted rulings. The open and the merge then name that exact head::
|
|
|
15
15
|
merge, no receipt or the receipt counts open findings -> deny, run the loop
|
|
16
16
|
auto-merge or enqueue pinned to the clean head -> allow
|
|
17
17
|
auto-merge that names no head -> deny, pin it
|
|
18
|
+
gh pr merge --disable-auto, which only turns auto-merge off -> allow
|
|
18
19
|
repository listed in CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES -> allow
|
|
19
20
|
|
|
20
21
|
The deny reason names the review command CLAUDE_MERGE_REVIEW_COMMAND sets,
|
|
@@ -27,6 +28,7 @@ from __future__ import annotations
|
|
|
27
28
|
|
|
28
29
|
import json
|
|
29
30
|
import os
|
|
31
|
+
import shlex
|
|
30
32
|
import sys
|
|
31
33
|
from dataclasses import dataclass
|
|
32
34
|
from pathlib import Path
|
|
@@ -48,17 +50,25 @@ from hooks_constants.hook_specific_output_keys import (
|
|
|
48
50
|
PERMISSION_DECISION_KEY,
|
|
49
51
|
)
|
|
50
52
|
from hooks_constants.merge_review_gate_constants import (
|
|
53
|
+
ALL_AUTO_FLAG_VALUE_PREFIXES,
|
|
54
|
+
ALL_OPTIONS_WITH_VALUE,
|
|
51
55
|
ALL_SHELL_TOOL_NAMES,
|
|
52
56
|
AUTO_MERGE_REASON_TEMPLATE,
|
|
53
57
|
AUTO_MERGE_TOOL_SUFFIX,
|
|
58
|
+
AUTO_OPTION,
|
|
54
59
|
API_SHA_FIELD_PATTERN,
|
|
55
60
|
AUTO_OPTION_PATTERN,
|
|
56
61
|
DEFAULT_REVIEW_COMMAND,
|
|
57
62
|
DENY_DECISION,
|
|
63
|
+
DISABLE_AUTO_OPTION,
|
|
64
|
+
END_OF_OPTIONS_WORD,
|
|
58
65
|
EXEMPT_REPOSITORIES_ENVIRONMENT_VARIABLE,
|
|
59
66
|
EXPECTED_HEAD_FIELD,
|
|
60
67
|
GH_API_MERGE_PATTERN,
|
|
68
|
+
GH_PR_MERGE_CALL_PATTERN,
|
|
61
69
|
GH_PR_MERGE_PATTERN,
|
|
70
|
+
ALL_GH_PR_MERGE_SUBCOMMAND_WORDS,
|
|
71
|
+
GH_PROGRAM_WORD_PATTERN,
|
|
62
72
|
HOOK_EVENT_NAME,
|
|
63
73
|
MATCH_HEAD_PATTERN,
|
|
64
74
|
MERGE_TOOL_SUFFIX,
|
|
@@ -75,6 +85,13 @@ from hooks_constants.merge_review_gate_constants import (
|
|
|
75
85
|
REPO_OPTION_PATTERN,
|
|
76
86
|
REPOSITORY_SEPARATOR,
|
|
77
87
|
REVIEW_COMMAND_ENVIRONMENT_VARIABLE,
|
|
88
|
+
SHELL_COMMENT_START_PATTERN,
|
|
89
|
+
SHELL_PUNCTUATION_CHARACTERS,
|
|
90
|
+
SHELL_QUOTED_TEXT_PATTERN,
|
|
91
|
+
SHELL_QUOTED_TEXT_PLACEHOLDER,
|
|
92
|
+
SHELL_WHITESPACE_CHARACTERS,
|
|
93
|
+
SHELL_WORD_SEPARATOR,
|
|
94
|
+
SHORT_OPTION_PREFIX,
|
|
78
95
|
STALE_REVIEW_REASON_TEMPLATE,
|
|
79
96
|
UNKNOWN_OPEN_TARGET_REASON,
|
|
80
97
|
UNKNOWN_TARGET_REASON,
|
|
@@ -180,7 +197,7 @@ def _shell_merge_request(command: str) -> MergeRequest | None:
|
|
|
180
197
|
is_auto_merge=False,
|
|
181
198
|
)
|
|
182
199
|
merge_match = GH_PR_MERGE_PATTERN.search(command)
|
|
183
|
-
if not merge_match:
|
|
200
|
+
if not merge_match or _only_disables_auto_merge(command):
|
|
184
201
|
return None
|
|
185
202
|
rest = merge_match["rest"]
|
|
186
203
|
owner, repo, number = _gh_pr_merge_target(rest)
|
|
@@ -194,6 +211,86 @@ def _shell_merge_request(command: str) -> MergeRequest | None:
|
|
|
194
211
|
)
|
|
195
212
|
|
|
196
213
|
|
|
214
|
+
def _only_disables_auto_merge(command: str) -> bool:
|
|
215
|
+
all_segments = _shell_segments(command)
|
|
216
|
+
if all_segments is None:
|
|
217
|
+
return False
|
|
218
|
+
all_merge_segments = [
|
|
219
|
+
each_segment
|
|
220
|
+
for each_segment in all_segments
|
|
221
|
+
if GH_PR_MERGE_CALL_PATTERN.search(SHELL_WORD_SEPARATOR.join(each_segment))
|
|
222
|
+
]
|
|
223
|
+
return len(all_merge_segments) == len(GH_PR_MERGE_CALL_PATTERN.findall(command)) and all(
|
|
224
|
+
_carries_disable_auto(each_segment) for each_segment in all_merge_segments
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _has_shell_comment(command: str) -> bool:
|
|
229
|
+
unquoted_command = SHELL_QUOTED_TEXT_PATTERN.sub(SHELL_QUOTED_TEXT_PLACEHOLDER, command)
|
|
230
|
+
return SHELL_COMMENT_START_PATTERN.search(unquoted_command) is not None
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _shell_segments(command: str) -> list[list[str]] | None:
|
|
234
|
+
if _has_shell_comment(command):
|
|
235
|
+
return None
|
|
236
|
+
lexer = shlex.shlex(command, posix=True, punctuation_chars=SHELL_PUNCTUATION_CHARACTERS)
|
|
237
|
+
lexer.whitespace = SHELL_WHITESPACE_CHARACTERS
|
|
238
|
+
lexer.whitespace_split = True
|
|
239
|
+
lexer.commenters = ""
|
|
240
|
+
try:
|
|
241
|
+
all_words = list(lexer)
|
|
242
|
+
except ValueError:
|
|
243
|
+
return None
|
|
244
|
+
all_segments: list[list[str]] = [[]]
|
|
245
|
+
for each_word in all_words:
|
|
246
|
+
if each_word and set(each_word) <= set(SHELL_PUNCTUATION_CHARACTERS):
|
|
247
|
+
all_segments.append([])
|
|
248
|
+
else:
|
|
249
|
+
all_segments[-1].append(each_word)
|
|
250
|
+
return all_segments
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _merge_arguments(all_words: list[str]) -> list[str] | None:
|
|
254
|
+
for each_index in range(len(all_words) - len(ALL_GH_PR_MERGE_SUBCOMMAND_WORDS)):
|
|
255
|
+
is_gh_program = GH_PROGRAM_WORD_PATTERN.search(all_words[each_index]) is not None
|
|
256
|
+
subcommand_end = each_index + 1 + len(ALL_GH_PR_MERGE_SUBCOMMAND_WORDS)
|
|
257
|
+
if is_gh_program and tuple(all_words[each_index + 1 : subcommand_end]) == ALL_GH_PR_MERGE_SUBCOMMAND_WORDS:
|
|
258
|
+
return all_words[subcommand_end:]
|
|
259
|
+
return None
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _carries_disable_auto(all_segment_words: list[str]) -> bool:
|
|
263
|
+
all_words = _merge_arguments(all_segment_words)
|
|
264
|
+
if all_words is None:
|
|
265
|
+
return False
|
|
266
|
+
all_argument_words = set()
|
|
267
|
+
is_option_value = False
|
|
268
|
+
for each_word in all_words:
|
|
269
|
+
if is_option_value:
|
|
270
|
+
is_option_value = False
|
|
271
|
+
continue
|
|
272
|
+
all_argument_words.add(each_word)
|
|
273
|
+
is_option_value = each_word in ALL_OPTIONS_WITH_VALUE
|
|
274
|
+
sets_auto_flag_value = any(
|
|
275
|
+
each_word.startswith(ALL_AUTO_FLAG_VALUE_PREFIXES) for each_word in all_argument_words
|
|
276
|
+
)
|
|
277
|
+
has_unread_flag_form = any(_is_unread_flag_form(each_word) for each_word in all_argument_words)
|
|
278
|
+
return (
|
|
279
|
+
DISABLE_AUTO_OPTION in all_argument_words
|
|
280
|
+
and AUTO_OPTION not in all_argument_words
|
|
281
|
+
and not sets_auto_flag_value
|
|
282
|
+
and not has_unread_flag_form
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _is_unread_flag_form(word: str) -> bool:
|
|
287
|
+
if word == END_OF_OPTIONS_WORD:
|
|
288
|
+
return True
|
|
289
|
+
if word.startswith(END_OF_OPTIONS_WORD) or not word.startswith(SHORT_OPTION_PREFIX):
|
|
290
|
+
return False
|
|
291
|
+
return len(word) > len(SHORT_OPTION_PREFIX) + 1
|
|
292
|
+
|
|
293
|
+
|
|
197
294
|
def _gh_pr_merge_target(arguments: str) -> tuple[str, str, int | None]:
|
|
198
295
|
url_match = PR_URL_PATTERN.search(arguments)
|
|
199
296
|
if url_match:
|
|
@@ -3,15 +3,19 @@
|
|
|
3
3
|
|
|
4
4
|
The owned pull request review-loop hook records each pull request the agent
|
|
5
5
|
opens on an owned repository outside the preset list. At the end of a turn
|
|
6
|
-
this gate asks GitHub for the state of each recorded pull request::
|
|
6
|
+
this gate asks GitHub for the state and head of each recorded pull request::
|
|
7
7
|
|
|
8
8
|
allow: every recorded pull request reads closed (merged or closed)
|
|
9
9
|
allow: the reply has "Blocked: acme/widgets#42 - the owner must approve"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
allow: acme/widgets#42 is open on the head this gate already blocked on
|
|
11
|
+
block: acme/widgets#42 reads open on a new head and the reply names no blocker
|
|
12
|
+
|
|
13
|
+
A block sends those pull requests back as the reason, so the model keeps
|
|
14
|
+
working the loop, and records each head it blocked on. A later stop on the
|
|
15
|
+
same head passes, and a push to a new head blocks once more. An open pull
|
|
16
|
+
request whose head GitHub does not report blocks on every stop. Claude Code
|
|
17
|
+
ends the turn after eight blocks in a row, and Codex turns the reason into a
|
|
18
|
+
continuation prompt. A state lookup that fails,
|
|
15
19
|
such as a missing ``gh`` or no network, counts as closed, so the gate never
|
|
16
20
|
holds a turn on a fact it could not read. The failure goes to stderr, where
|
|
17
21
|
the host shows it in the hook log.
|
|
@@ -42,6 +46,7 @@ from hooks_constants.owned_pr_merge_stop_gate_constants import (
|
|
|
42
46
|
GH_API_SUBCOMMAND,
|
|
43
47
|
GH_JQ_OPTION,
|
|
44
48
|
GH_PROGRAM_NAME,
|
|
49
|
+
HEAD_FIELD,
|
|
45
50
|
HOOK_EVENT_NAME,
|
|
46
51
|
LAST_ASSISTANT_MESSAGE_KEY,
|
|
47
52
|
LOOKUP_FAILURE_TEMPLATE,
|
|
@@ -54,14 +59,23 @@ from hooks_constants.owned_pr_merge_stop_gate_constants import (
|
|
|
54
59
|
PULL_REQUEST_STATE_QUERY,
|
|
55
60
|
REASON_KEY,
|
|
56
61
|
SESSION_ID_KEY,
|
|
62
|
+
STATE_AND_HEAD_SEPARATOR,
|
|
57
63
|
STATE_QUERY_TIMEOUT_SECONDS,
|
|
64
|
+
URL_FIELD,
|
|
58
65
|
)
|
|
59
66
|
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
60
|
-
from owned_pr_session_record import
|
|
67
|
+
from owned_pr_session_record import (
|
|
68
|
+
all_blocked_heads,
|
|
69
|
+
all_recorded_pull_requests,
|
|
70
|
+
record_blocked_heads,
|
|
71
|
+
)
|
|
72
|
+
|
|
61
73
|
|
|
74
|
+
def open_head(all_pull_request_fields: dict[str, str]) -> str | None:
|
|
75
|
+
"""Return the head sha when GitHub reports the pull request as open, else None.
|
|
62
76
|
|
|
63
|
-
|
|
64
|
-
|
|
77
|
+
An open pull request whose head GitHub did not report returns an empty
|
|
78
|
+
string, which no earlier block can match.
|
|
65
79
|
|
|
66
80
|
Args:
|
|
67
81
|
all_pull_request_fields: The owner, repository, number, and url fields.
|
|
@@ -77,10 +91,12 @@ def is_pull_request_open(all_pull_request_fields: dict[str, str]) -> bool:
|
|
|
77
91
|
)
|
|
78
92
|
except (OSError, subprocess.TimeoutExpired) as lookup_error:
|
|
79
93
|
sys.stderr.write(LOOKUP_FAILURE_TEMPLATE.format(endpoint=endpoint, detail=lookup_error))
|
|
80
|
-
return
|
|
94
|
+
return None
|
|
81
95
|
if completed.returncode != 0:
|
|
82
96
|
sys.stderr.write(LOOKUP_FAILURE_TEMPLATE.format(endpoint=endpoint, detail=completed.stderr.strip()))
|
|
83
|
-
|
|
97
|
+
return None
|
|
98
|
+
state, _, head = completed.stdout.strip().partition(STATE_AND_HEAD_SEPARATOR)
|
|
99
|
+
return head.strip() if state == OPEN_PULL_REQUEST_STATE else None
|
|
84
100
|
|
|
85
101
|
|
|
86
102
|
def is_reported_blocked(all_pull_request_fields: dict[str, str], reply_text: str) -> bool:
|
|
@@ -111,21 +127,27 @@ def is_reported_blocked(all_pull_request_fields: dict[str, str], reply_text: str
|
|
|
111
127
|
|
|
112
128
|
|
|
113
129
|
def unfinished_pull_requests(all_hook_input_fields: dict[str, object]) -> list[dict[str, str]]:
|
|
114
|
-
"""Return the recorded pull requests that are open
|
|
130
|
+
"""Return the recorded pull requests that are open, not reported blocked, and on a new head.
|
|
131
|
+
|
|
132
|
+
Each entry carries its head sha under ``head``. A pull request the gate
|
|
133
|
+
already blocked on at its current head is left out.
|
|
115
134
|
|
|
116
135
|
Args:
|
|
117
136
|
all_hook_input_fields: The Stop hook payload.
|
|
118
137
|
"""
|
|
119
138
|
reply_value = all_hook_input_fields.get(LAST_ASSISTANT_MESSAGE_KEY)
|
|
120
139
|
reply_text = reply_value if isinstance(reply_value, str) else ""
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
140
|
+
session_id = all_hook_input_fields.get(SESSION_ID_KEY)
|
|
141
|
+
blocked_heads_by_url = all_blocked_heads(session_id)
|
|
142
|
+
all_unfinished = []
|
|
143
|
+
for each_pull_request in all_recorded_pull_requests(session_id):
|
|
144
|
+
if is_reported_blocked(each_pull_request, reply_text):
|
|
145
|
+
continue
|
|
146
|
+
head = open_head(each_pull_request)
|
|
147
|
+
if head is None or (head and blocked_heads_by_url.get(each_pull_request[URL_FIELD]) == head):
|
|
148
|
+
continue
|
|
149
|
+
all_unfinished.append({**each_pull_request, HEAD_FIELD: head})
|
|
150
|
+
return all_unfinished
|
|
129
151
|
|
|
130
152
|
|
|
131
153
|
def block_reason_for(all_open_pull_requests: list[dict[str, str]]) -> str:
|
|
@@ -145,6 +167,14 @@ def main() -> int:
|
|
|
145
167
|
all_open_pull_requests = unfinished_pull_requests(hook_input)
|
|
146
168
|
if not all_open_pull_requests:
|
|
147
169
|
return ALLOW_EXIT_CODE
|
|
170
|
+
record_blocked_heads(
|
|
171
|
+
hook_input.get(SESSION_ID_KEY),
|
|
172
|
+
{
|
|
173
|
+
each_pull_request[URL_FIELD]: each_pull_request[HEAD_FIELD]
|
|
174
|
+
for each_pull_request in all_open_pull_requests
|
|
175
|
+
if each_pull_request[HEAD_FIELD]
|
|
176
|
+
},
|
|
177
|
+
)
|
|
148
178
|
block_reason = block_reason_for(all_open_pull_requests)
|
|
149
179
|
log_hook_block(Path(__file__).name, HOOK_EVENT_NAME, block_reason)
|
|
150
180
|
sys.stdout.write(json.dumps({DECISION_KEY: BLOCK_DECISION, REASON_KEY: block_reason}))
|
|
@@ -43,7 +43,9 @@ a check did not run::
|
|
|
43
43
|
It denies an Actions run, a pull request link, or a run, job, or check id
|
|
44
44
|
that no successful tool result in the transcript shows. A subagent's report
|
|
45
45
|
is the result of its Agent call. With no readable transcript, this check
|
|
46
|
-
is skipped.
|
|
46
|
+
is skipped. The closing-reply gate uses
|
|
47
|
+
``unseen_citation_violation_with_hook_text``, where a pull request link also
|
|
48
|
+
counts as shown when hook or harness text shows it.
|
|
47
49
|
|
|
48
50
|
The banned-word, unvalidated claim, and citation checks also read the prose of a
|
|
49
51
|
decision card: its question, its context, and each option's label and
|
|
@@ -99,6 +101,7 @@ from json_file_reader import read_json_object
|
|
|
99
101
|
from visual_reply_rules import (
|
|
100
102
|
VisualReplyRules,
|
|
101
103
|
abbreviation_violation,
|
|
104
|
+
hook_supplied_text,
|
|
102
105
|
load_rules,
|
|
103
106
|
mode_enabled,
|
|
104
107
|
results_picture_violation,
|
|
@@ -152,6 +155,7 @@ from hooks_constants.reply_length_gate_constants import (
|
|
|
152
155
|
CARD_TEXT_SEPARATOR,
|
|
153
156
|
CITED_NUMBER_IN_RESULT_TEMPLATE,
|
|
154
157
|
CITED_NUMBER_PATTERN,
|
|
158
|
+
CITED_PULL_NUMBER_GROUP,
|
|
155
159
|
CLAUDE_HOME_DIRECTORY_NAME,
|
|
156
160
|
CONFIG_FILE_ENCODING,
|
|
157
161
|
CURLY_APOSTROPHE,
|
|
@@ -176,8 +180,10 @@ from hooks_constants.reply_length_gate_constants import (
|
|
|
176
180
|
MAXIMUM_WORDS_PER_SENTENCE,
|
|
177
181
|
MID_SENTENCE_COLON_MESSAGE,
|
|
178
182
|
MID_SENTENCE_COLON_PATTERN,
|
|
183
|
+
PULL_LINK_IN_HOOK_TEXT_TEMPLATE,
|
|
179
184
|
QUESTION_CARD_QUESTIONS_KEY,
|
|
180
185
|
QUESTION_CARD_TOOL_NAME,
|
|
186
|
+
REPLY_ECHO_PATTERN,
|
|
181
187
|
RETRY_INSTRUCTION,
|
|
182
188
|
SENTENCE_END_PATTERN,
|
|
183
189
|
SENTENCE_PREVIEW_SUFFIX,
|
|
@@ -416,19 +422,15 @@ def unvalidated_claim_violation(reply_text: str) -> str | None:
|
|
|
416
422
|
return joined_violations(all_claims)
|
|
417
423
|
|
|
418
424
|
|
|
419
|
-
def
|
|
420
|
-
"""Return the deny reason for each cited run, job, check, or pull request no tool result shows.
|
|
421
|
-
|
|
422
|
-
Returns None when the text cites nothing or the transcript cannot be read.
|
|
423
|
-
"""
|
|
425
|
+
def _citations_no_tool_result_shows(reply_text: str, transcript_path: object) -> list[re.Match[str]]:
|
|
424
426
|
all_citations = list(CITED_NUMBER_PATTERN.finditer(FENCED_BLOCK_PATTERN.sub(" ", reply_text)))
|
|
425
427
|
if not all_citations:
|
|
426
|
-
return
|
|
428
|
+
return []
|
|
427
429
|
result_text = successful_result_text(transcript_path)
|
|
428
430
|
if result_text is None:
|
|
429
|
-
return
|
|
430
|
-
return
|
|
431
|
-
|
|
431
|
+
return []
|
|
432
|
+
return [
|
|
433
|
+
each_match
|
|
432
434
|
for each_match in all_citations
|
|
433
435
|
if re.search(
|
|
434
436
|
CITED_NUMBER_IN_RESULT_TEMPLATE.format(
|
|
@@ -437,6 +439,46 @@ def unseen_citation_violation(reply_text: str, transcript_path: object) -> str |
|
|
|
437
439
|
result_text,
|
|
438
440
|
)
|
|
439
441
|
is None
|
|
442
|
+
]
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def unseen_citation_violation(reply_text: str, transcript_path: object) -> str | None:
|
|
446
|
+
"""Return the deny reason for each cited run, job, check, or pull request no tool result shows.
|
|
447
|
+
|
|
448
|
+
Returns None when the text cites nothing or the transcript cannot be read.
|
|
449
|
+
"""
|
|
450
|
+
return joined_violations(
|
|
451
|
+
UNSEEN_CITATION_MESSAGE.format(citation=each_match.group(0))
|
|
452
|
+
for each_match in _citations_no_tool_result_shows(reply_text, transcript_path)
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def unseen_citation_violation_with_hook_text(reply_text: str, transcript_path: object) -> str | None:
|
|
457
|
+
"""Return the deny reason for each cited run, job, check, or pull request the session never showed.
|
|
458
|
+
|
|
459
|
+
A tool result that shows the number counts for every kind. A ``/pull/N``
|
|
460
|
+
link also counts when hook or harness text shows the same link, such as
|
|
461
|
+
stop-hook feedback or a background agent's task notification. This gate's
|
|
462
|
+
own feedback, which quotes the citation or sentence it blocked, does not count.
|
|
463
|
+
|
|
464
|
+
Returns None when the text cites nothing or the transcript cannot be read.
|
|
465
|
+
"""
|
|
466
|
+
all_unseen = _citations_no_tool_result_shows(reply_text, transcript_path)
|
|
467
|
+
hook_text = (
|
|
468
|
+
REPLY_ECHO_PATTERN.sub(" ", hook_supplied_text(transcript_path) or "")
|
|
469
|
+
if any(each_match[CITED_PULL_NUMBER_GROUP] for each_match in all_unseen)
|
|
470
|
+
else ""
|
|
471
|
+
)
|
|
472
|
+
return joined_violations(
|
|
473
|
+
UNSEEN_CITATION_MESSAGE.format(citation=each_match.group(0))
|
|
474
|
+
for each_match in all_unseen
|
|
475
|
+
if not (
|
|
476
|
+
each_match[CITED_PULL_NUMBER_GROUP]
|
|
477
|
+
and re.search(
|
|
478
|
+
PULL_LINK_IN_HOOK_TEXT_TEMPLATE.format(number=each_match[CITED_PULL_NUMBER_GROUP]),
|
|
479
|
+
hook_text,
|
|
480
|
+
)
|
|
481
|
+
)
|
|
440
482
|
)
|
|
441
483
|
|
|
442
484
|
|
|
@@ -87,6 +87,142 @@ def test_should_allow_a_run_a_tool_result_shows(
|
|
|
87
87
|
assert run_gate(monkeypatch, capsys, MEASURED_REPLY, transcript_path) == (0, "")
|
|
88
88
|
|
|
89
89
|
|
|
90
|
+
CITED_PULL_URL = "https://github.com/acme/widgets/pull/8127"
|
|
91
|
+
CITED_PULL_REPLY = f"The control is open as [PR 8127]({CITED_PULL_URL})."
|
|
92
|
+
TASK_NOTIFICATION_ENTRY = {
|
|
93
|
+
"type": "user",
|
|
94
|
+
"message": {
|
|
95
|
+
"role": "user",
|
|
96
|
+
"content": (
|
|
97
|
+
"<task-notification>\n<status>completed</status>\n<result>1. PR "
|
|
98
|
+
f"[#8127]({CITED_PULL_URL}) is open. It is not armed.</result>\n</task-notification>"
|
|
99
|
+
),
|
|
100
|
+
},
|
|
101
|
+
"origin": {"kind": "task-notification", "producer": "session-task"},
|
|
102
|
+
"promptSource": "system",
|
|
103
|
+
}
|
|
104
|
+
OWN_ECHO_FEEDBACK = (
|
|
105
|
+
'Stop hook feedback:\nThe text cites "/pull/8127", and no tool result in this session'
|
|
106
|
+
" shows it. Read it yourself or through a subagent first. Rewrite the reply without it."
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def write_transcript_entries(transcript_path: Path, all_entries: list[dict[str, object]]) -> Path:
|
|
111
|
+
transcript_path.write_text(
|
|
112
|
+
"\n".join(json.dumps(each_entry) for each_entry in all_entries), encoding="utf-8"
|
|
113
|
+
)
|
|
114
|
+
return transcript_path
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@pytest.mark.parametrize(
|
|
118
|
+
"hook_entry",
|
|
119
|
+
[
|
|
120
|
+
TASK_NOTIFICATION_ENTRY,
|
|
121
|
+
{
|
|
122
|
+
"type": "user",
|
|
123
|
+
"isMeta": True,
|
|
124
|
+
"message": {
|
|
125
|
+
"role": "user",
|
|
126
|
+
"content": f"Stop hook feedback:\nThese pull requests are still open:\n- {CITED_PULL_URL}",
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"type": "attachment",
|
|
131
|
+
"attachment": {
|
|
132
|
+
"type": "hook_additional_context",
|
|
133
|
+
"content": [f"You opened acme/widgets#8127 {CITED_PULL_URL}"],
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"type": "attachment",
|
|
138
|
+
"attachment": {"type": "hook_system_message", "content": f"Watching {CITED_PULL_URL}"},
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"type": "attachment",
|
|
142
|
+
"attachment": {
|
|
143
|
+
"type": "queued_command",
|
|
144
|
+
"origin": {"kind": "task-notification", "producer": "session-task"},
|
|
145
|
+
"prompt": f"<task-notification><result>Opened {CITED_PULL_URL}</result></task-notification>",
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
)
|
|
150
|
+
def test_should_allow_a_pull_request_link_hook_text_shows(
|
|
151
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
152
|
+
capsys: pytest.CaptureFixture[str],
|
|
153
|
+
tmp_path: Path,
|
|
154
|
+
hook_entry: dict[str, object],
|
|
155
|
+
) -> None:
|
|
156
|
+
transcript_path = write_transcript_entries(tmp_path / "s.jsonl", [hook_entry])
|
|
157
|
+
assert run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path) == (0, "")
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_should_block_a_pull_request_link_only_the_gate_feedback_echoes(
|
|
161
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
162
|
+
) -> None:
|
|
163
|
+
feedback_entry = {"type": "user", "isMeta": True, "message": {"role": "user", "content": OWN_ECHO_FEEDBACK}}
|
|
164
|
+
blocking_error_entry = {
|
|
165
|
+
"type": "attachment",
|
|
166
|
+
"attachment": {
|
|
167
|
+
"type": "hook_blocking_error",
|
|
168
|
+
"blockingError": {"blockingError": OWN_ECHO_FEEDBACK.removeprefix("Stop hook feedback:\n")},
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
transcript_path = write_transcript_entries(
|
|
172
|
+
tmp_path / "s.jsonl", [feedback_entry, blocking_error_entry]
|
|
173
|
+
)
|
|
174
|
+
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
175
|
+
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def test_should_block_a_pull_request_link_the_gate_quotes_in_a_sentence(
|
|
179
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
180
|
+
) -> None:
|
|
181
|
+
quoting_feedback = (
|
|
182
|
+
f'Stop hook feedback:\nNo tool validated this: "{CITED_PULL_REPLY}". Run the check yourself'
|
|
183
|
+
" or through a subagent, then state what the run, log line, output, or file and line shows."
|
|
184
|
+
" When you cannot run it this turn, report it as `Open: <check> - <next step>`."
|
|
185
|
+
f'\nSentence too long: 40 words, limit 35: "Opened {CITED_PULL_URL} and...".'
|
|
186
|
+
)
|
|
187
|
+
feedback_entry = {"type": "user", "isMeta": True, "message": {"role": "user", "content": quoting_feedback}}
|
|
188
|
+
transcript_path = write_transcript_entries(tmp_path / "s.jsonl", [feedback_entry])
|
|
189
|
+
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
190
|
+
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def test_should_block_a_pull_request_link_only_a_typed_queued_prompt_shows(
|
|
194
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
195
|
+
) -> None:
|
|
196
|
+
typed_entry = {
|
|
197
|
+
"type": "attachment",
|
|
198
|
+
"attachment": {"type": "queued_command", "origin": {"kind": "human"}, "prompt": f"Is {CITED_PULL_URL} done?"},
|
|
199
|
+
}
|
|
200
|
+
transcript_path = write_transcript_entries(tmp_path / "s.jsonl", [typed_entry])
|
|
201
|
+
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
202
|
+
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_should_block_a_pull_request_link_no_text_shows(
|
|
206
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
207
|
+
) -> None:
|
|
208
|
+
transcript_path = write_transcript_entries(
|
|
209
|
+
tmp_path / "s.jsonl", [{**TASK_NOTIFICATION_ENTRY, "message": {"role": "user", "content": "PR 8126 opened."}}]
|
|
210
|
+
)
|
|
211
|
+
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
212
|
+
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_should_block_a_run_only_hook_text_shows(
|
|
216
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
217
|
+
) -> None:
|
|
218
|
+
transcript_path = write_transcript_entries(
|
|
219
|
+
tmp_path / "s.jsonl",
|
|
220
|
+
[{**TASK_NOTIFICATION_ENTRY, "message": {"role": "user", "content": "run 18239174012 took 39m"}}],
|
|
221
|
+
)
|
|
222
|
+
_, stdout_text = run_gate(monkeypatch, capsys, MEASURED_REPLY, transcript_path)
|
|
223
|
+
assert 'cites "run 18239174012"' in json.loads(stdout_text)["reason"]
|
|
224
|
+
|
|
225
|
+
|
|
90
226
|
def test_should_steer_a_skipped_check_to_an_open_line(
|
|
91
227
|
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
92
228
|
) -> None:
|
|
@@ -184,6 +184,54 @@ def test_should_read_a_gh_api_merge_with_a_sha_field() -> None:
|
|
|
184
184
|
assert _reason(_bash(command)) is None
|
|
185
185
|
|
|
186
186
|
|
|
187
|
+
@pytest.mark.parametrize(
|
|
188
|
+
"command",
|
|
189
|
+
[
|
|
190
|
+
"gh pr merge --disable-auto 7844",
|
|
191
|
+
"gh pr merge 12 --repo acme/tools --disable-auto",
|
|
192
|
+
"gh pr merge --disable-auto 12 --repo acme/tools; gh pr merge --disable-auto 13 --repo acme/tools",
|
|
193
|
+
"gh pr merge 12 --repo acme/tools --body 'turn auto-merge off' --disable-auto",
|
|
194
|
+
'gh pr merge 12 --repo acme/tools --disable-auto --body "Done (see notes); thanks"',
|
|
195
|
+
'gh pr merge --disable-auto 12 --repo acme/tools --body "#12 done"',
|
|
196
|
+
"gh pr merge --disable-auto 12 --repo acme/tools --body issue#12",
|
|
197
|
+
'gh pr merge --body "" --disable-auto 12 --repo acme/tools',
|
|
198
|
+
],
|
|
199
|
+
)
|
|
200
|
+
def test_should_allow_a_merge_call_that_only_disables_auto_merge(command: str) -> None:
|
|
201
|
+
assert gate.decision_for(_bash(command)) is None
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
@pytest.mark.parametrize(
|
|
205
|
+
"command",
|
|
206
|
+
[
|
|
207
|
+
"gh pr merge --disable-auto 12 --repo acme/tools && gh pr merge 12 --repo acme/tools",
|
|
208
|
+
"gh pr merge --disable-auto 12 --repo acme/tools\ngh pr merge 12 --repo acme/tools",
|
|
209
|
+
"gh pr merge 12 --repo acme/tools && gh pr merge --disable-auto 12 --repo acme/tools",
|
|
210
|
+
"gh pr merge 12 --repo acme/tools --body ' --disable-auto '",
|
|
211
|
+
"gh pr merge --disable-auto 12 --repo acme/tools $(gh pr merge 12 --repo acme/tools)",
|
|
212
|
+
"gh pr merge --disable-auto 12 --repo acme/tools `gh pr merge 12 --repo acme/tools`",
|
|
213
|
+
"gh pr merge 12 --repo acme/tools --body x\\ --disable-auto",
|
|
214
|
+
"gh pr merge 12 --repo acme/tools --body --disable-auto",
|
|
215
|
+
"gh pr merge 12 --repo acme/tools --disable-auto --body 'unclosed",
|
|
216
|
+
"gh pr merge 12 --repo acme/tools # --disable-auto",
|
|
217
|
+
"gh pr merge --disable-auto 12 --repo acme/tools a#b; gh pr merge 12 --repo acme/tools",
|
|
218
|
+
'gh pr merge --disable-auto 12 --repo acme/tools --body "$(gh pr merge 12 --repo acme/tools)"',
|
|
219
|
+
'gh pr merge --disable-auto 12 --repo acme/tools && g""h pr merge --disable-auto 14 --repo acme/tools'
|
|
220
|
+
" && echo x#y && gh pr merge 12 --repo acme/tools",
|
|
221
|
+
"bash -c ' gh pr merge 12 --repo acme/tools --squash' sh --disable-auto",
|
|
222
|
+
"gh pr merge 12 --repo acme/tools --auto --disable-auto",
|
|
223
|
+
"gh pr merge 12 --repo acme/tools --disable-auto --auto=true",
|
|
224
|
+
"gh pr merge 12 --repo acme/tools --disable-auto --disable-auto=false",
|
|
225
|
+
"gh pr merge 12 --repo acme/tools -sb --disable-auto",
|
|
226
|
+
"gh pr merge 12 --repo acme/tools -dt --disable-auto",
|
|
227
|
+
"gh pr merge 12 --repo acme/tools -mF --disable-auto",
|
|
228
|
+
"gh pr merge 12 --repo acme/tools -- --disable-auto",
|
|
229
|
+
],
|
|
230
|
+
)
|
|
231
|
+
def test_should_check_a_merge_chained_with_a_disable_auto_call(command: str) -> None:
|
|
232
|
+
assert "no clean review" in _reason(_bash(command))
|
|
233
|
+
|
|
234
|
+
|
|
187
235
|
def test_should_ignore_commands_that_merge_nothing() -> None:
|
|
188
236
|
assert gate.decision_for(_bash("gh pr view 12 --repo acme/tools")) is None
|
|
189
237
|
assert gate.decision_for(_bash("git merge origin/main")) is None
|