claude-dev-env 8.116.4 → 8.117.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/bin/ever-registered-hooks.mjs +2 -0
- package/bin/install-constants.mjs +3 -1
- package/bin/install.cursor-rules.test.mjs +64 -2
- package/bin/install.mjs +15 -2
- package/bin/install.prune.test.mjs +92 -0
- package/bin/install.test.mjs +1 -0
- package/docs/model-choice/escalation.md +3 -3
- package/docs/model-choice/subagents-and-fan-out.md +2 -2
- package/docs/model-choice/which-model.md +1 -1
- package/docs/references/working-style.md +1 -0
- package/docs/rule-guides/question-presentation.md +6 -0
- package/docs/rule-guides/session-title.md +8 -9
- package/docs/rule-guides/subagent-model-effort.md +13 -13
- package/hooks/advisory/owned_pr_review_loop.py +2 -3
- package/hooks/blocking/bash_post_call_dispatcher.py +17 -9
- package/hooks/blocking/bash_pre_tool_use_dispatcher.py +12 -8
- package/hooks/blocking/final_reply_gate.py +5 -9
- package/hooks/blocking/open_tasks_stop_gate.py +9 -20
- package/hooks/blocking/open_tasks_transcript.py +8 -3
- package/hooks/blocking/pii_prevention_blocker_parts/repository_exemption.py +7 -2
- package/hooks/blocking/question_brief_gate.py +382 -0
- package/hooks/blocking/step_note_gate.py +5 -2
- package/hooks/blocking/task_subject_length_gate.py +111 -0
- package/hooks/blocking/test_bash_post_call_dispatcher.py +36 -1
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +57 -0
- package/hooks/blocking/test_final_reply_gate.py +26 -2
- package/hooks/blocking/test_open_tasks_stop_gate.py +21 -11
- package/hooks/blocking/test_question_brief_gate.py +916 -0
- package/hooks/blocking/test_step_note_gate.py +26 -0
- package/hooks/blocking/test_task_subject_length_gate.py +221 -0
- package/hooks/blocking/test_turn_review_findings.py +124 -1
- package/hooks/blocking/test_turn_review_gate.py +1312 -10
- package/hooks/blocking/test_turn_review_snapshot.py +8 -0
- package/hooks/blocking/tests/test_repository_exemption.py +11 -0
- package/hooks/blocking/turn_review_findings.py +173 -13
- package/hooks/blocking/turn_review_gate.py +878 -105
- package/hooks/blocking/turn_review_snapshot.py +5 -1
- package/hooks/features/bash-dispatchers.md +1 -3
- package/hooks/features/conduct-gates.md +19 -9
- package/hooks/features/lifecycle-cleanup.md +1 -1
- package/hooks/features/spawn-routing.md +1 -1
- package/hooks/hooks.json +24 -11
- package/hooks/hooks_constants/final_reply_gate_constants.py +0 -3
- package/hooks/hooks_constants/hook_time_budget.py +61 -0
- package/hooks/hooks_constants/hosted_hook_runner.py +51 -21
- package/hooks/hooks_constants/hosted_hook_runner_constants.py +3 -0
- package/hooks/hooks_constants/nested_project_hooks_constants.py +8 -0
- package/hooks/hooks_constants/once_per_session_constants.py +1 -0
- package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +0 -4
- package/hooks/hooks_constants/owned_pr_review_loop_constants.py +0 -4
- package/hooks/hooks_constants/pre_tool_use_context_runner.py +23 -3
- package/hooks/hooks_constants/pre_tool_use_context_runner_constants.py +5 -0
- package/hooks/hooks_constants/pre_tool_use_stdin.py +1 -1
- package/hooks/hooks_constants/question_brief_gate_constants.py +66 -0
- package/hooks/hooks_constants/session_title_constants.py +6 -27
- package/hooks/hooks_constants/stop_hook_hold.py +54 -0
- package/hooks/hooks_constants/stop_hook_hold_constants.py +9 -0
- package/hooks/hooks_constants/subagent_effort_levels.json +6 -0
- package/hooks/hooks_constants/subagent_effort_route_constants.py +20 -19
- package/hooks/hooks_constants/task_subject_length_gate_constants.py +43 -0
- package/hooks/hooks_constants/test_hook_time_budget.py +104 -0
- package/hooks/hooks_constants/test_hosted_hook_runner.py +12 -0
- package/hooks/hooks_constants/test_pre_tool_use_context_runner.py +16 -0
- package/hooks/hooks_constants/test_pre_tool_use_stdin.py +6 -0
- package/hooks/hooks_constants/test_stop_hook_hold.py +40 -0
- package/hooks/hooks_constants/thread_spawn_pace_hook_constants.py +1 -2
- package/hooks/hooks_constants/turn_review_gate_constants.py +246 -12
- package/hooks/lifecycle/nested_project_hooks.py +34 -4
- package/hooks/lifecycle/test_nested_project_hooks.py +52 -0
- package/hooks/once_per_session.py +33 -4
- package/hooks/open_task_asks.py +1 -1
- package/hooks/owned_pr_session_record.py +1 -76
- package/hooks/routing/subagent_effort_route.py +107 -13
- package/hooks/routing/subagent_effort_route_hook.py +9 -8
- package/hooks/routing/test_subagent_effort_route.py +201 -14
- package/hooks/routing/test_subagent_effort_route_hook.py +56 -0
- package/hooks/routing/thread_spawn_pace_hook.py +3 -3
- package/hooks/session/background_silence_prompt.py +1 -1
- package/hooks/session/test_tracking_nudge.py +114 -2
- package/hooks/session/tracking_nudge.py +28 -18
- package/hooks/session_title_transcript.py +195 -0
- package/hooks/test_once_per_session.py +10 -0
- package/hooks/test_owned_pr_session_record.py +0 -26
- package/hooks/test_session_title_transcript.py +111 -0
- package/hooks/validation/post_tool_use_dispatcher.py +38 -21
- package/hooks/validation/test_post_tool_use_dispatcher.py +43 -0
- package/hooks/validators/ruff_integration.py +46 -4
- package/hooks/validators/test_ruff_integration.py +67 -5
- package/package.json +1 -1
- package/rules/question-presentation.md +4 -4
- package/rules/subagent-model-effort.md +1 -1
- package/scripts/claude_account_worker.py +4 -3
- package/scripts/policy_lint/adapter_configuration.py +12 -11
- package/scripts/policy_lint/config/constants.py +2 -2
- package/scripts/test_claude_account_worker.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +10 -2
- package/hooks/blocking/owned_pr_merge_stop_gate.py +0 -185
- package/hooks/blocking/session_title_turn_gate.py +0 -417
- package/hooks/blocking/test_owned_pr_merge_stop_gate.py +0 -254
- package/hooks/blocking/test_session_title_turn_gate.py +0 -396
- package/hooks/hooks_constants/owned_pr_merge_stop_gate_constants.py +0 -40
|
@@ -1,36 +1,94 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""Stop hook that has a fresh reviewer read each turn's changes
|
|
2
|
+
"""Stop hook that has a fresh reviewer read each turn's changes, and a judge rule on each dismissal.
|
|
3
3
|
|
|
4
|
-
The agent edits ``src/service.py`` and drops a retry.
|
|
5
|
-
separate ``claude -p`` run reviews only that turn's edits
|
|
6
|
-
``/code-review low
|
|
4
|
+
The agent edits ``src/service.py`` and drops a retry. The turn ends at once.
|
|
5
|
+
In the background, a separate ``claude -p`` run reviews only that turn's edits
|
|
6
|
+
with ``/code-review low``, on Sonnet at high effort. A finding exits 2 and wakes the agent with it::
|
|
7
7
|
|
|
8
8
|
prompt arrives -> snapshot A of the working tree
|
|
9
9
|
agent edits -> src/service.py loses @with_retry
|
|
10
|
-
stop -> snapshot B (parent A), claude -p "/code-review low A..B"
|
|
11
|
-
->
|
|
12
|
-
agent fixes it -> stop reviews B..C only, finds nothing,
|
|
10
|
+
stop -> turn ends; snapshot B (parent A), claude -p "/code-review low A..B"
|
|
11
|
+
-> exit 2 wakes the agent: "src/service.py:208 - The diff removes ..."
|
|
12
|
+
agent fixes it -> stop reviews B..C only, finds nothing, prints nothing
|
|
13
13
|
|
|
14
14
|
``turn_review_snapshot`` builds the snapshots, and ``turn_review_findings``
|
|
15
15
|
reads the findings out of the review text. Edits the user left before the
|
|
16
|
-
prompt sit in snapshot A, so the review sees only the agent's work.
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
prompt sit in snapshot A, so the review sees only the agent's work.
|
|
17
|
+
|
|
18
|
+
Each finding gets an id that is unique in the session, such as ``TR-1``, and
|
|
19
|
+
is saved as open in the session's state. The wake shows the id beside the
|
|
20
|
+
finding. The agent fixes the finding or contests it with one reply line, and
|
|
21
|
+
a separate judge run decides::
|
|
22
|
+
|
|
23
|
+
reply ends with "Dismiss TR-1: the caller retries the update"
|
|
24
|
+
stop -> claude -p --model sonnet --effort high, prompt on stdin
|
|
25
|
+
judge approves -> TR-1 dismissed, reason saved, one log line, no wake
|
|
26
|
+
judge rejects -> TR-1 upheld; exit 2: "Dismissal of TR-1 rejected: <reason>. Fix it."
|
|
27
|
+
|
|
28
|
+
The reply comes from ``last_assistant_message`` in the Stop input, or from
|
|
29
|
+
the text of the last assistant message in ``transcript_path`` when that field
|
|
30
|
+
is missing. The judge gets the checkout path and one JSON object of data::
|
|
31
|
+
|
|
32
|
+
{"requests": [{"id": "TR-1", "commit_range": "A..B",
|
|
33
|
+
"finding": "src/service.py:3 - ...", "reason_to_dismiss": "..."}],
|
|
34
|
+
"user_messages": ["the last five messages the user typed, cut to 2000 characters"]}
|
|
35
|
+
|
|
36
|
+
The user's messages come from ``transcript_path``. Only entries with
|
|
37
|
+
``origin.kind`` ``human`` count, and text that starts with ``<`` is left out. The judge gets no other part of the
|
|
38
|
+
conversation. It runs with the review's read-only options, once per checkout
|
|
39
|
+
that holds a contested finding. A rejection counts toward the hold count, and
|
|
40
|
+
it shares one wake with that stop's new findings. The judge runs after the
|
|
41
|
+
review and shares its time. A judge that fails, times out or gets no time, or
|
|
42
|
+
whose whole answer is not one JSON array of verdicts, leaves the finding open
|
|
43
|
+
and exits 0 with no output. A ``Dismiss`` line for an id that is not open, or
|
|
44
|
+
one sent at the hold cap, is logged and not judged.
|
|
45
|
+
|
|
46
|
+
Claude Code runs the Stop entry with ``asyncRewake``. A plain ``claude -p``
|
|
47
|
+
run has no background, so the same entry holds that stop with exit code 2.
|
|
48
|
+
A finding or a rejected dismissal goes to stderr and to the stdout ``reason``. Every
|
|
49
|
+
other outcome exits 0 with no output, and its diagnostic goes to ``~/.claude/logs/turn-review.log``.
|
|
50
|
+
|
|
51
|
+
A stop claims its range before the review starts. Under a lock on the session's
|
|
52
|
+
state file, it moves the baseline to snapshot B, marks the turn closed, and
|
|
53
|
+
saves both. A second stop during the review starts from B, so no range is
|
|
54
|
+
reviewed twice. After the review, the stop waits up to 35 seconds for the
|
|
55
|
+
lock, longer than a prompt run may hold it, and changes only the hold count. A stop that never gets that lock
|
|
56
|
+
exits 0 with no wake, so each wake is counted.
|
|
57
|
+
|
|
58
|
+
A prompt marks the turn open. When the last turn is still open, no stop has
|
|
59
|
+
claimed it yet: its stop is racing this prompt, or the user interrupted it::
|
|
60
|
+
|
|
61
|
+
prompt 1 -> baseline A, turn open
|
|
62
|
+
stop 1 -> starts; the next prompt runs before its claim
|
|
63
|
+
prompt 2 -> turn still open, so A stays the baseline
|
|
64
|
+
stop 1 -> claims A..B and reviews turn 1's edits
|
|
65
|
+
|
|
66
|
+
Edits the user made between those two turns are then reviewed too. When a
|
|
67
|
+
prompt found the turn open less than 10 seconds before the claim, that prompt
|
|
68
|
+
began the next turn, so the stop leaves it open. A later claim closes it,
|
|
69
|
+
which ends the open turn an interrupt left behind.
|
|
19
70
|
|
|
20
71
|
A repository takes part when it is the session's project folder or a folder
|
|
21
72
|
directly inside it, and its GitHub origin ``owner/repo`` slug is listed in
|
|
22
73
|
``CLAUDE_TURN_REVIEW_REPOSITORIES``. The hook holds a stop at most three times
|
|
23
74
|
in a row, skips a turn that changed more than 30 files, and lets the stop
|
|
24
|
-
through when the review fails or times out
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
75
|
+
through when the review fails or times out. A stop below the cap with no
|
|
76
|
+
finding, no rejection and no dismissal to judge resets the count, and so does a user's prompt. A prompt within 10 seconds
|
|
77
|
+
of the last wake, or one that starts with ``<task-notification``, belongs to
|
|
78
|
+
the wake and keeps the count, so a chain of wakes stops at three.
|
|
79
|
+
A stop gets 280 seconds of work and a prompt 25, under the 300 and 30
|
|
80
|
+
second registrations. A git call or review that would start after that time
|
|
81
|
+
is skipped.
|
|
82
|
+
The cap and the file limit match Anthropic's security-guidance plugin. The review and judge
|
|
83
|
+
runs carry ``CLAUDE_TURN_REVIEW_CHILD=1`` and none of the parent session's id,
|
|
84
|
+
messaging or ingress variables, and the hook does nothing inside such a run. Each may read
|
|
28
85
|
files and run ``git diff``, ``git show`` and ``git log``; every other tool is
|
|
29
86
|
denied.
|
|
30
87
|
"""
|
|
31
88
|
|
|
32
89
|
from __future__ import annotations
|
|
33
90
|
|
|
91
|
+
import datetime
|
|
34
92
|
import json
|
|
35
93
|
import os
|
|
36
94
|
import re
|
|
@@ -39,8 +97,16 @@ import subprocess
|
|
|
39
97
|
import sys
|
|
40
98
|
import tempfile
|
|
41
99
|
import time
|
|
100
|
+
from collections.abc import Iterator
|
|
101
|
+
from contextlib import contextmanager
|
|
102
|
+
from dataclasses import dataclass, field
|
|
42
103
|
from pathlib import Path
|
|
43
104
|
|
|
105
|
+
if sys.platform == "win32":
|
|
106
|
+
import msvcrt
|
|
107
|
+
else:
|
|
108
|
+
import fcntl
|
|
109
|
+
|
|
44
110
|
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
45
111
|
if hooks_root_directory not in sys.path:
|
|
46
112
|
sys.path.insert(0, hooks_root_directory)
|
|
@@ -51,25 +117,92 @@ if blocking_directory not in sys.path:
|
|
|
51
117
|
from atomic_file_writer import write_text_atomically
|
|
52
118
|
from hooks_constants.atomic_file_writer_constants import ATOMIC_WRITE_TEMPORARY_SUFFIX
|
|
53
119
|
from hooks_constants.hook_block_logger import log_hook_block
|
|
120
|
+
from hooks_constants.hook_time_budget import call_timeout, remaining_seconds, time_budget
|
|
54
121
|
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
55
122
|
from hooks_constants.session_file_id import session_file_id
|
|
123
|
+
from hooks_constants.stop_hook_hold import hold_stop
|
|
56
124
|
from hooks_constants.subprocess_window import hidden_window_creation_flags
|
|
57
125
|
from hooks_constants.turn_review_gate_constants import (
|
|
126
|
+
ALL_FINDING_STATUSES,
|
|
127
|
+
ALL_LOG_PATH_PARTS,
|
|
58
128
|
ALL_PARENT_SESSION_VARIABLES,
|
|
59
129
|
ALL_REVIEW_ALLOWED_TOOLS,
|
|
60
130
|
ALL_REVIEW_DISALLOWED_TOOLS,
|
|
61
131
|
BASELINE_COMMIT_PATTERN,
|
|
62
|
-
BLOCK_DECISION,
|
|
63
132
|
BLOCK_REASON_TEMPLATE,
|
|
64
|
-
|
|
133
|
+
DISMISS_AT_CAP_DIAGNOSTIC_TEMPLATE,
|
|
134
|
+
DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE,
|
|
135
|
+
FINDING_BASE_KEY,
|
|
136
|
+
FINDING_DISMISSED_DIAGNOSTIC_TEMPLATE,
|
|
137
|
+
FINDING_ID_PATTERN,
|
|
138
|
+
FINDING_ID_TEMPLATE,
|
|
139
|
+
FINDING_JUDGE_REASON_KEY,
|
|
140
|
+
FINDING_LINE_WITH_ID_TEMPLATE,
|
|
141
|
+
FINDING_REPOSITORY_KEY,
|
|
142
|
+
FINDING_STATUS_DISMISSED,
|
|
143
|
+
FINDING_STATUS_KEY,
|
|
144
|
+
FINDING_STATUS_OPEN,
|
|
145
|
+
FINDING_STATUS_UPHELD,
|
|
146
|
+
FINDING_TEXT_KEY,
|
|
147
|
+
FINDING_TURN_KEY,
|
|
148
|
+
FINDING_UPHELD_DIAGNOSTIC_TEMPLATE,
|
|
149
|
+
JUDGE_EFFORT,
|
|
150
|
+
JUDGE_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
151
|
+
JUDGE_MODEL_ALIAS,
|
|
152
|
+
JUDGE_PROMPT_TEMPLATE,
|
|
153
|
+
JUDGE_COMMIT_RANGE_TEMPLATE,
|
|
154
|
+
JUDGE_DATA_INDENT,
|
|
155
|
+
JUDGE_DATA_REQUESTS_KEY,
|
|
156
|
+
JUDGE_DATA_USER_MESSAGES_KEY,
|
|
157
|
+
JUDGE_ID_KEY,
|
|
158
|
+
JUDGE_REQUEST_FINDING_KEY,
|
|
159
|
+
JUDGE_REQUEST_RANGE_KEY,
|
|
160
|
+
JUDGE_REQUEST_REASON_KEY,
|
|
161
|
+
ALL_TRANSCRIPT_NOT_TYPED_FLAGS,
|
|
162
|
+
MAXIMUM_USER_MESSAGE_CHARACTERS,
|
|
163
|
+
NESTED_TOO_DEEP_REASON,
|
|
164
|
+
TRANSCRIPT_HUMAN_ORIGIN_KIND,
|
|
165
|
+
TRANSCRIPT_MARKUP_START,
|
|
166
|
+
TRANSCRIPT_MESSAGE_ID_KEY,
|
|
167
|
+
TRANSCRIPT_ORIGIN_KEY,
|
|
168
|
+
TRANSCRIPT_ORIGIN_KIND_KEY,
|
|
169
|
+
TRANSCRIPT_UNREADABLE_DIAGNOSTIC_TEMPLATE,
|
|
170
|
+
TRANSCRIPT_USER_TYPE,
|
|
171
|
+
USER_MESSAGE_COUNT,
|
|
172
|
+
NO_VERDICT_DIAGNOSTIC_TEMPLATE,
|
|
173
|
+
OUTSIDE_REPOSITORY_LABEL,
|
|
174
|
+
REJECTION_LINE_SEPARATOR,
|
|
175
|
+
REJECTION_LINE_TEMPLATE,
|
|
176
|
+
REPLY_TEXT_KEY,
|
|
177
|
+
SENTENCE_END_CHARACTERS,
|
|
178
|
+
STATE_FINDINGS_KEY,
|
|
179
|
+
TRANSCRIPT_ASSISTANT_TYPE,
|
|
180
|
+
TRANSCRIPT_BLOCK_TYPE_KEY,
|
|
181
|
+
TRANSCRIPT_CONTENT_KEY,
|
|
182
|
+
TRANSCRIPT_ENTRY_TYPE_KEY,
|
|
183
|
+
TRANSCRIPT_MESSAGE_KEY,
|
|
184
|
+
TRANSCRIPT_PATH_KEY,
|
|
185
|
+
TRANSCRIPT_TEXT_BLOCK_TYPE,
|
|
186
|
+
TRANSCRIPT_TEXT_ENCODING,
|
|
187
|
+
TRANSCRIPT_TEXT_KEY,
|
|
188
|
+
TRANSCRIPT_TEXT_SEPARATOR,
|
|
189
|
+
UNKNOWN_FINDING_LABEL,
|
|
190
|
+
WAKE_PART_SEPARATOR,
|
|
191
|
+
WORD_SEPARATOR,
|
|
65
192
|
FINDING_LINE_SEPARATOR,
|
|
66
193
|
GIT_DIRECTORY_NAME,
|
|
67
194
|
HOOK_EVENT_NAME_KEY,
|
|
195
|
+
LOG_LINE_TEMPLATE,
|
|
196
|
+
LOG_TEXT_ENCODING,
|
|
197
|
+
LOG_UNENCODABLE_HANDLER,
|
|
68
198
|
MAXIMUM_BLOCKS_IN_A_ROW,
|
|
199
|
+
MACHINE_PROMPT_PREFIX,
|
|
69
200
|
MAXIMUM_TURN_FILES,
|
|
70
201
|
PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE,
|
|
71
202
|
PROMPT_EVENT_NAME,
|
|
72
|
-
|
|
203
|
+
PROMPT_KEY,
|
|
204
|
+
RACING_STOP_WINDOW_SECONDS,
|
|
205
|
+
PROMPT_RUN_BUDGET_SECONDS,
|
|
73
206
|
REPOSITORIES_ENVIRONMENT_VARIABLE,
|
|
74
207
|
REPOSITORY_FINDINGS_SEPARATOR,
|
|
75
208
|
REPOSITORY_FINDINGS_TEMPLATE,
|
|
@@ -79,6 +212,7 @@ from hooks_constants.turn_review_gate_constants import (
|
|
|
79
212
|
REVIEW_EFFORT,
|
|
80
213
|
REVIEW_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
81
214
|
REVIEW_MODEL_ALIAS,
|
|
215
|
+
REVIEW_MODEL_EFFORT,
|
|
82
216
|
REVIEW_PROMPT_TEMPLATE,
|
|
83
217
|
REVIEW_RESULT_KEY,
|
|
84
218
|
REVIEW_EXECUTABLE_NAME,
|
|
@@ -87,19 +221,107 @@ from hooks_constants.turn_review_gate_constants import (
|
|
|
87
221
|
MINIMUM_REVIEW_SECONDS,
|
|
88
222
|
REVIEW_TIMEOUT_SECONDS,
|
|
89
223
|
REVIEW_TOOLS_TEXT,
|
|
224
|
+
RUN_DIAGNOSTIC_TEMPLATE,
|
|
90
225
|
SESSION_ID_KEY,
|
|
91
226
|
STATE_BASELINES_KEY,
|
|
92
227
|
STATE_BLOCKS_KEY,
|
|
93
228
|
STATE_FILE_TEMPLATE,
|
|
229
|
+
STATE_OPEN_TURN_PROMPT_KEY,
|
|
230
|
+
STATE_LAST_WAKE_KEY,
|
|
231
|
+
STATE_TURN_OPEN_KEY,
|
|
232
|
+
STATE_LOCK_BUSY_DIAGNOSTIC_TEMPLATE,
|
|
233
|
+
STATE_LOCK_BYTE_COUNT,
|
|
234
|
+
STATE_LOCK_FILE_MODE,
|
|
235
|
+
STATE_LOCK_FILE_TEMPLATE,
|
|
236
|
+
STATE_LOCK_OUTCOME_WAIT_SECONDS,
|
|
237
|
+
STATE_LOCK_RETRY_SECONDS,
|
|
238
|
+
STATE_LOCK_WAIT_SECONDS,
|
|
239
|
+
STATE_SAVE_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
94
240
|
STATE_TEMPORARY_PREFIX,
|
|
95
241
|
STATE_TEXT_ENCODING,
|
|
96
242
|
STOP_EVENT_NAME,
|
|
243
|
+
STOP_HOOK_ACTIVE_KEY,
|
|
244
|
+
STOP_RUN_BUDGET_SECONDS,
|
|
97
245
|
TOO_MANY_FILES_DIAGNOSTIC_TEMPLATE,
|
|
246
|
+
WAKE_PROMPT_WINDOW_SECONDS,
|
|
98
247
|
)
|
|
99
248
|
from pii_prevention_blocker_parts.repository_exemption import repository_origin_slug
|
|
100
|
-
from turn_review_findings import finding_lines
|
|
249
|
+
from turn_review_findings import JudgeVerdict, dismiss_requests, finding_lines, judge_verdicts
|
|
101
250
|
from turn_review_snapshot import turn_changed_paths, working_tree_snapshot
|
|
102
251
|
|
|
252
|
+
if sys.platform == "win32":
|
|
253
|
+
|
|
254
|
+
def _try_file_lock(file_descriptor: int) -> None:
|
|
255
|
+
os.lseek(file_descriptor, 0, os.SEEK_SET)
|
|
256
|
+
msvcrt.locking(file_descriptor, msvcrt.LK_NBLCK, STATE_LOCK_BYTE_COUNT)
|
|
257
|
+
|
|
258
|
+
def _release_file_lock(file_descriptor: int) -> None:
|
|
259
|
+
os.lseek(file_descriptor, 0, os.SEEK_SET)
|
|
260
|
+
msvcrt.locking(file_descriptor, msvcrt.LK_UNLCK, STATE_LOCK_BYTE_COUNT)
|
|
261
|
+
|
|
262
|
+
else:
|
|
263
|
+
|
|
264
|
+
def _try_file_lock(file_descriptor: int) -> None:
|
|
265
|
+
fcntl.flock(file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
266
|
+
|
|
267
|
+
def _release_file_lock(file_descriptor: int) -> None:
|
|
268
|
+
fcntl.flock(file_descriptor, fcntl.LOCK_UN)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _log(diagnostic_text: str) -> None:
|
|
272
|
+
timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
|
273
|
+
try:
|
|
274
|
+
log_path = Path.home().joinpath(*ALL_LOG_PATH_PARTS)
|
|
275
|
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
276
|
+
with log_path.open(
|
|
277
|
+
"a", encoding=LOG_TEXT_ENCODING, errors=LOG_UNENCODABLE_HANDLER
|
|
278
|
+
) as log_file:
|
|
279
|
+
log_file.write(LOG_LINE_TEMPLATE.format(timestamp=timestamp, text=diagnostic_text))
|
|
280
|
+
except (OSError, RuntimeError):
|
|
281
|
+
return
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _is_lock_taken(file_descriptor: int) -> bool:
|
|
285
|
+
try:
|
|
286
|
+
_try_file_lock(file_descriptor)
|
|
287
|
+
except OSError:
|
|
288
|
+
return False
|
|
289
|
+
return True
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _acquire_with_wait(file_descriptor: int, wait_seconds: float) -> bool:
|
|
293
|
+
deadline = time.monotonic() + (call_timeout(wait_seconds) or 0)
|
|
294
|
+
while not _is_lock_taken(file_descriptor):
|
|
295
|
+
if time.monotonic() >= deadline:
|
|
296
|
+
return False
|
|
297
|
+
time.sleep(STATE_LOCK_RETRY_SECONDS)
|
|
298
|
+
return True
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _release_and_close(file_descriptor: int, is_locked: bool) -> None:
|
|
302
|
+
try:
|
|
303
|
+
if is_locked:
|
|
304
|
+
_release_file_lock(file_descriptor)
|
|
305
|
+
finally:
|
|
306
|
+
os.close(file_descriptor)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
@contextmanager
|
|
310
|
+
def _state_lock(session_id: object, wait_seconds: float) -> Iterator[bool]:
|
|
311
|
+
lock_path = Path(tempfile.gettempdir()) / STATE_LOCK_FILE_TEMPLATE.format(
|
|
312
|
+
session_id=session_file_id(session_id)
|
|
313
|
+
)
|
|
314
|
+
try:
|
|
315
|
+
file_descriptor = os.open(lock_path, os.O_CREAT | os.O_RDWR, STATE_LOCK_FILE_MODE)
|
|
316
|
+
except OSError:
|
|
317
|
+
yield False
|
|
318
|
+
return
|
|
319
|
+
is_locked = _acquire_with_wait(file_descriptor, wait_seconds)
|
|
320
|
+
try:
|
|
321
|
+
yield is_locked
|
|
322
|
+
finally:
|
|
323
|
+
_release_and_close(file_descriptor, is_locked)
|
|
324
|
+
|
|
103
325
|
|
|
104
326
|
def _opted_in_repository_slugs() -> frozenset[str]:
|
|
105
327
|
listed = os.environ.get(REPOSITORIES_ENVIRONMENT_VARIABLE, "")
|
|
@@ -142,6 +364,26 @@ def _review_command(base_commit: str, turn_commit: str) -> list[str]:
|
|
|
142
364
|
review_prompt,
|
|
143
365
|
"--model",
|
|
144
366
|
REVIEW_MODEL_ALIAS,
|
|
367
|
+
"--effort",
|
|
368
|
+
REVIEW_MODEL_EFFORT,
|
|
369
|
+
*_read_only_child_options(),
|
|
370
|
+
]
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def _judge_command() -> list[str]:
|
|
374
|
+
return [
|
|
375
|
+
shutil.which(REVIEW_EXECUTABLE_NAME) or REVIEW_EXECUTABLE_NAME,
|
|
376
|
+
"-p",
|
|
377
|
+
"--model",
|
|
378
|
+
JUDGE_MODEL_ALIAS,
|
|
379
|
+
"--effort",
|
|
380
|
+
JUDGE_EFFORT,
|
|
381
|
+
*_read_only_child_options(),
|
|
382
|
+
]
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def _read_only_child_options() -> list[str]:
|
|
386
|
+
return [
|
|
145
387
|
"--output-format",
|
|
146
388
|
"json",
|
|
147
389
|
"--permission-mode",
|
|
@@ -170,63 +412,80 @@ def _review_environment() -> dict[str, str]:
|
|
|
170
412
|
return all_child_environment_variables
|
|
171
413
|
|
|
172
414
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
415
|
+
@dataclass(frozen=True)
|
|
416
|
+
class _ChildRun:
|
|
417
|
+
repository_root: Path
|
|
418
|
+
all_command_parts: list[str]
|
|
419
|
+
prompt_text: str | None
|
|
420
|
+
failure_template: str
|
|
421
|
+
|
|
422
|
+
def write_failure(self, failure_text: str) -> None:
|
|
423
|
+
_log(
|
|
424
|
+
self.failure_template.format(
|
|
425
|
+
repository=self.repository_root.name, reason=failure_text
|
|
426
|
+
)
|
|
177
427
|
)
|
|
178
|
-
)
|
|
179
428
|
|
|
180
429
|
|
|
181
|
-
def
|
|
182
|
-
|
|
183
|
-
) -> str | None:
|
|
430
|
+
def _child_stdout(child_run: _ChildRun, remaining_seconds: float) -> str | None:
|
|
431
|
+
closed_stdin = subprocess.DEVNULL if child_run.prompt_text is None else None
|
|
184
432
|
try:
|
|
185
433
|
completed_process = subprocess.run(
|
|
186
|
-
|
|
434
|
+
child_run.all_command_parts,
|
|
187
435
|
check=False,
|
|
188
436
|
capture_output=True,
|
|
189
437
|
text=True,
|
|
190
438
|
encoding="utf-8",
|
|
191
439
|
errors="replace",
|
|
192
|
-
stdin=subprocess.DEVNULL,
|
|
193
440
|
timeout=remaining_seconds,
|
|
194
|
-
cwd=str(repository_root),
|
|
441
|
+
cwd=str(child_run.repository_root),
|
|
195
442
|
env=_review_environment(),
|
|
196
443
|
creationflags=hidden_window_creation_flags(),
|
|
444
|
+
stdin=closed_stdin,
|
|
445
|
+
input=child_run.prompt_text,
|
|
197
446
|
)
|
|
198
447
|
except subprocess.TimeoutExpired:
|
|
199
|
-
|
|
448
|
+
child_run.write_failure("timed out")
|
|
200
449
|
return None
|
|
201
450
|
except OSError as start_error:
|
|
202
|
-
|
|
451
|
+
child_run.write_failure(str(start_error))
|
|
203
452
|
return None
|
|
204
453
|
if completed_process.returncode != 0:
|
|
205
|
-
|
|
454
|
+
child_run.write_failure(f"exit {completed_process.returncode}")
|
|
206
455
|
return None
|
|
207
456
|
return completed_process.stdout
|
|
208
457
|
|
|
209
458
|
|
|
210
|
-
def
|
|
211
|
-
|
|
212
|
-
) -> str | None:
|
|
213
|
-
remaining_seconds = review_deadline - time.monotonic()
|
|
459
|
+
def _child_result_text(child_run: _ChildRun, deadline: float) -> str | None:
|
|
460
|
+
remaining_seconds = deadline - time.monotonic()
|
|
214
461
|
if remaining_seconds < MINIMUM_REVIEW_SECONDS:
|
|
215
|
-
|
|
462
|
+
child_run.write_failure("no time left this stop")
|
|
216
463
|
return None
|
|
217
|
-
|
|
218
|
-
if
|
|
464
|
+
child_stdout = _child_stdout(child_run, remaining_seconds)
|
|
465
|
+
if child_stdout is None:
|
|
219
466
|
return None
|
|
220
467
|
try:
|
|
221
|
-
|
|
222
|
-
except ValueError:
|
|
223
|
-
|
|
468
|
+
child_result = json.loads(child_stdout)
|
|
469
|
+
except (ValueError, RecursionError):
|
|
470
|
+
child_run.write_failure("output was not JSON")
|
|
224
471
|
return None
|
|
225
|
-
|
|
226
|
-
if not isinstance(
|
|
227
|
-
|
|
472
|
+
result_text = child_result.get(REVIEW_RESULT_KEY) if isinstance(child_result, dict) else None
|
|
473
|
+
if not isinstance(result_text, str):
|
|
474
|
+
child_run.write_failure("output had no result text")
|
|
228
475
|
return None
|
|
229
|
-
return
|
|
476
|
+
return result_text
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def _review_text(
|
|
480
|
+
repository_root: Path, base_commit: str, turn_commit: str, review_deadline: float
|
|
481
|
+
) -> str | None:
|
|
482
|
+
review_run = _ChildRun(
|
|
483
|
+
repository_root,
|
|
484
|
+
_review_command(base_commit, turn_commit),
|
|
485
|
+
None,
|
|
486
|
+
REVIEW_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
487
|
+
)
|
|
488
|
+
return _child_result_text(review_run, review_deadline)
|
|
230
489
|
|
|
231
490
|
|
|
232
491
|
def _state_path(session_id: object) -> Path:
|
|
@@ -235,53 +494,189 @@ def _state_path(session_id: object) -> Path:
|
|
|
235
494
|
)
|
|
236
495
|
|
|
237
496
|
|
|
238
|
-
|
|
497
|
+
@dataclass
|
|
498
|
+
class _Finding:
|
|
499
|
+
text: str
|
|
500
|
+
repository: str
|
|
501
|
+
base_commit: str
|
|
502
|
+
turn_commit: str
|
|
503
|
+
status: str = FINDING_STATUS_OPEN
|
|
504
|
+
judge_reason: str | None = None
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
@dataclass
|
|
508
|
+
class _ReviewState:
|
|
509
|
+
all_baselines: dict[str, str] = field(default_factory=dict)
|
|
510
|
+
blocks_in_a_row: int = 0
|
|
511
|
+
is_turn_open: bool = False
|
|
512
|
+
last_wake_at: float | None = None
|
|
513
|
+
open_turn_prompt_at: float | None = None
|
|
514
|
+
all_findings: dict[str, _Finding] = field(default_factory=dict)
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
def _saved_time(raw_value: object) -> float | None:
|
|
518
|
+
if isinstance(raw_value, bool) or not isinstance(raw_value, (int, float)):
|
|
519
|
+
return None
|
|
520
|
+
return float(raw_value)
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
def _is_commit_name(raw_value: object) -> bool:
|
|
524
|
+
return isinstance(raw_value, str) and re.fullmatch(BASELINE_COMMIT_PATTERN, raw_value) is not None
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def _saved_finding(raw_entry: object) -> _Finding | None:
|
|
528
|
+
if not isinstance(raw_entry, dict):
|
|
529
|
+
return None
|
|
530
|
+
text = raw_entry.get(FINDING_TEXT_KEY)
|
|
531
|
+
repository = raw_entry.get(FINDING_REPOSITORY_KEY)
|
|
532
|
+
base_commit = raw_entry.get(FINDING_BASE_KEY)
|
|
533
|
+
turn_commit = raw_entry.get(FINDING_TURN_KEY)
|
|
534
|
+
status = raw_entry.get(FINDING_STATUS_KEY)
|
|
535
|
+
judge_reason = raw_entry.get(FINDING_JUDGE_REASON_KEY)
|
|
536
|
+
if not (
|
|
537
|
+
isinstance(text, str)
|
|
538
|
+
and isinstance(repository, str)
|
|
539
|
+
and _is_commit_name(base_commit)
|
|
540
|
+
and _is_commit_name(turn_commit)
|
|
541
|
+
and status in ALL_FINDING_STATUSES
|
|
542
|
+
):
|
|
543
|
+
return None
|
|
544
|
+
return _Finding(
|
|
545
|
+
text,
|
|
546
|
+
repository,
|
|
547
|
+
str(base_commit),
|
|
548
|
+
str(turn_commit),
|
|
549
|
+
str(status),
|
|
550
|
+
judge_reason if isinstance(judge_reason, str) else None,
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def _saved_findings(raw_findings: object) -> dict[str, _Finding]:
|
|
555
|
+
if not isinstance(raw_findings, dict):
|
|
556
|
+
return {}
|
|
557
|
+
all_findings: dict[str, _Finding] = {}
|
|
558
|
+
for each_id, each_entry in raw_findings.items():
|
|
559
|
+
saved_finding = _saved_finding(each_entry)
|
|
560
|
+
if isinstance(each_id, str) and re.fullmatch(FINDING_ID_PATTERN, each_id) and saved_finding:
|
|
561
|
+
all_findings[each_id] = saved_finding
|
|
562
|
+
return all_findings
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
def _finding_number(finding_id: str) -> int:
|
|
566
|
+
id_match = re.fullmatch(FINDING_ID_PATTERN, finding_id)
|
|
567
|
+
return int(id_match.group(1)) if id_match else 0
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _next_finding_id(review_state: _ReviewState) -> str:
|
|
571
|
+
highest_number = max(map(_finding_number, review_state.all_findings), default=0)
|
|
572
|
+
return FINDING_ID_TEMPLATE.format(number=highest_number + 1)
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
def _finding_record(finding: _Finding) -> dict[str, str | None]:
|
|
576
|
+
return {
|
|
577
|
+
FINDING_TEXT_KEY: finding.text,
|
|
578
|
+
FINDING_REPOSITORY_KEY: finding.repository,
|
|
579
|
+
FINDING_BASE_KEY: finding.base_commit,
|
|
580
|
+
FINDING_TURN_KEY: finding.turn_commit,
|
|
581
|
+
FINDING_STATUS_KEY: finding.status,
|
|
582
|
+
FINDING_JUDGE_REASON_KEY: finding.judge_reason,
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def _load_state(session_id: object) -> _ReviewState:
|
|
239
587
|
try:
|
|
240
588
|
saved_state = json.loads(_state_path(session_id).read_text(encoding=STATE_TEXT_ENCODING))
|
|
241
589
|
except (OSError, ValueError):
|
|
242
|
-
return
|
|
590
|
+
return _ReviewState()
|
|
243
591
|
if not isinstance(saved_state, dict):
|
|
244
|
-
return
|
|
592
|
+
return _ReviewState()
|
|
245
593
|
raw_baselines = saved_state.get(STATE_BASELINES_KEY)
|
|
246
594
|
all_baselines = {
|
|
247
595
|
each_root: each_commit
|
|
248
596
|
for each_root, each_commit in (
|
|
249
597
|
raw_baselines.items() if isinstance(raw_baselines, dict) else []
|
|
250
598
|
)
|
|
251
|
-
if isinstance(each_root, str)
|
|
252
|
-
and isinstance(each_commit, str)
|
|
253
|
-
and re.fullmatch(BASELINE_COMMIT_PATTERN, each_commit)
|
|
599
|
+
if isinstance(each_root, str) and _is_commit_name(each_commit)
|
|
254
600
|
}
|
|
255
601
|
raw_blocks = saved_state.get(STATE_BLOCKS_KEY)
|
|
256
|
-
return
|
|
602
|
+
return _ReviewState(
|
|
603
|
+
all_baselines,
|
|
604
|
+
raw_blocks if isinstance(raw_blocks, int) else 0,
|
|
605
|
+
saved_state.get(STATE_TURN_OPEN_KEY) is True,
|
|
606
|
+
_saved_time(saved_state.get(STATE_LAST_WAKE_KEY)),
|
|
607
|
+
_saved_time(saved_state.get(STATE_OPEN_TURN_PROMPT_KEY)),
|
|
608
|
+
_saved_findings(saved_state.get(STATE_FINDINGS_KEY)),
|
|
609
|
+
)
|
|
257
610
|
|
|
258
611
|
|
|
259
|
-
def _save_state(session_id: object,
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
612
|
+
def _save_state(session_id: object, review_state: _ReviewState) -> None:
|
|
613
|
+
try:
|
|
614
|
+
write_text_atomically(
|
|
615
|
+
_state_path(session_id),
|
|
616
|
+
json.dumps(
|
|
617
|
+
{
|
|
618
|
+
STATE_BASELINES_KEY: review_state.all_baselines,
|
|
619
|
+
STATE_BLOCKS_KEY: review_state.blocks_in_a_row,
|
|
620
|
+
STATE_TURN_OPEN_KEY: review_state.is_turn_open,
|
|
621
|
+
STATE_LAST_WAKE_KEY: review_state.last_wake_at,
|
|
622
|
+
STATE_OPEN_TURN_PROMPT_KEY: review_state.open_turn_prompt_at,
|
|
623
|
+
STATE_FINDINGS_KEY: {
|
|
624
|
+
each_id: _finding_record(each_finding)
|
|
625
|
+
for each_id, each_finding in review_state.all_findings.items()
|
|
626
|
+
},
|
|
627
|
+
}
|
|
628
|
+
),
|
|
629
|
+
STATE_TEXT_ENCODING,
|
|
630
|
+
STATE_TEMPORARY_PREFIX,
|
|
631
|
+
ATOMIC_WRITE_TEMPORARY_SUFFIX,
|
|
632
|
+
False,
|
|
633
|
+
)
|
|
634
|
+
except (OSError, UnicodeError) as save_error:
|
|
635
|
+
_log(STATE_SAVE_FAILURE_DIAGNOSTIC_TEMPLATE.format(detail=save_error))
|
|
268
636
|
|
|
269
637
|
|
|
270
|
-
def
|
|
271
|
-
|
|
638
|
+
def _is_wake_prompt(review_state: _ReviewState, prompt_text: object, now: float) -> bool:
|
|
639
|
+
if isinstance(prompt_text, str) and prompt_text.lstrip().startswith(MACHINE_PROMPT_PREFIX):
|
|
640
|
+
return True
|
|
641
|
+
if review_state.last_wake_at is None:
|
|
642
|
+
return False
|
|
643
|
+
return now - review_state.last_wake_at < WAKE_PROMPT_WINDOW_SECONDS
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def _prompt_baselines(review_state: _ReviewState, all_roots: list[Path]) -> dict[str, str]:
|
|
647
|
+
all_baselines = dict(review_state.all_baselines) if review_state.is_turn_open else {}
|
|
272
648
|
for each_root in all_roots:
|
|
649
|
+
if str(each_root) in all_baselines:
|
|
650
|
+
continue
|
|
273
651
|
snapshot_commit = working_tree_snapshot(each_root, None)
|
|
274
652
|
if snapshot_commit is not None:
|
|
275
653
|
all_baselines[str(each_root)] = snapshot_commit
|
|
276
|
-
|
|
654
|
+
return all_baselines
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
def _record_prompt_baselines(
|
|
658
|
+
session_id: object, all_roots: list[Path], prompt_text: object
|
|
659
|
+
) -> None:
|
|
660
|
+
with _state_lock(session_id, STATE_LOCK_WAIT_SECONDS) as is_locked:
|
|
661
|
+
if not is_locked:
|
|
662
|
+
_log(STATE_LOCK_BUSY_DIAGNOSTIC_TEMPLATE.format(event=PROMPT_EVENT_NAME))
|
|
663
|
+
return
|
|
664
|
+
review_state = _load_state(session_id)
|
|
665
|
+
now = time.time()
|
|
666
|
+
review_state.all_baselines = _prompt_baselines(review_state, all_roots)
|
|
667
|
+
review_state.open_turn_prompt_at = now if review_state.is_turn_open else None
|
|
668
|
+
if not _is_wake_prompt(review_state, prompt_text, now):
|
|
669
|
+
review_state.blocks_in_a_row = 0
|
|
670
|
+
review_state.is_turn_open = True
|
|
671
|
+
_save_state(session_id, review_state)
|
|
277
672
|
|
|
278
673
|
|
|
279
674
|
def _repository_findings(
|
|
280
675
|
repository_root: Path, base_commit: str, turn_commit: str, review_deadline: float
|
|
281
|
-
) -> str | None:
|
|
676
|
+
) -> list[str] | None:
|
|
282
677
|
all_changed_paths = turn_changed_paths(repository_root, base_commit, turn_commit)
|
|
283
678
|
if len(all_changed_paths) > MAXIMUM_TURN_FILES:
|
|
284
|
-
|
|
679
|
+
_log(
|
|
285
680
|
TOO_MANY_FILES_DIAGNOSTIC_TEMPLATE.format(
|
|
286
681
|
repository=repository_root.name,
|
|
287
682
|
count=len(all_changed_paths),
|
|
@@ -292,51 +687,411 @@ def _repository_findings(
|
|
|
292
687
|
if not all_changed_paths:
|
|
293
688
|
return None
|
|
294
689
|
review_text = _review_text(repository_root, base_commit, turn_commit, review_deadline)
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
690
|
+
return finding_lines(review_text or "", all_changed_paths) or None
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
@dataclass(frozen=True)
|
|
694
|
+
class _JudgeRequest:
|
|
695
|
+
finding_id: str
|
|
696
|
+
finding: _Finding
|
|
697
|
+
reason: str
|
|
698
|
+
|
|
302
699
|
|
|
700
|
+
@dataclass(frozen=True)
|
|
701
|
+
class _StopClaim:
|
|
702
|
+
all_turn_ranges: list[tuple[Path, str, str]] = field(default_factory=list)
|
|
703
|
+
all_judge_requests: list[_JudgeRequest] = field(default_factory=list)
|
|
303
704
|
|
|
304
|
-
|
|
305
|
-
|
|
705
|
+
|
|
706
|
+
def _reversed_transcript_entries(hook_input: dict) -> Iterator[dict]:
|
|
707
|
+
transcript_path = hook_input.get(TRANSCRIPT_PATH_KEY)
|
|
708
|
+
if not isinstance(transcript_path, str) or not transcript_path:
|
|
709
|
+
return
|
|
710
|
+
try:
|
|
711
|
+
all_transcript_lines = Path(transcript_path).read_text(
|
|
712
|
+
encoding=TRANSCRIPT_TEXT_ENCODING, errors="replace"
|
|
713
|
+
).splitlines()
|
|
714
|
+
except OSError:
|
|
715
|
+
return
|
|
716
|
+
for each_line in reversed(all_transcript_lines):
|
|
717
|
+
try:
|
|
718
|
+
transcript_entry = json.loads(each_line)
|
|
719
|
+
except ValueError:
|
|
720
|
+
continue
|
|
721
|
+
except RecursionError:
|
|
722
|
+
_log(TRANSCRIPT_UNREADABLE_DIAGNOSTIC_TEMPLATE.format(reason=NESTED_TOO_DEEP_REASON))
|
|
723
|
+
continue
|
|
724
|
+
if isinstance(transcript_entry, dict):
|
|
725
|
+
yield transcript_entry
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
class _ReversedTranscript:
|
|
729
|
+
"""The transcript's entries, newest first, with each line decoded once per stop."""
|
|
730
|
+
|
|
731
|
+
def __init__(self, hook_input: dict) -> None:
|
|
732
|
+
self._unread_entries = _reversed_transcript_entries(hook_input)
|
|
733
|
+
self._all_read_entries: list[dict] = []
|
|
734
|
+
|
|
735
|
+
def __iter__(self) -> Iterator[dict]:
|
|
736
|
+
yield from self._all_read_entries
|
|
737
|
+
for each_entry in self._unread_entries:
|
|
738
|
+
self._all_read_entries.append(each_entry)
|
|
739
|
+
yield each_entry
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
def _entry_message(transcript_entry: dict) -> dict:
|
|
743
|
+
message = transcript_entry.get(TRANSCRIPT_MESSAGE_KEY)
|
|
744
|
+
return message if isinstance(message, dict) else {}
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
def _entry_text(transcript_entry: dict) -> str:
|
|
748
|
+
content = _entry_message(transcript_entry).get(TRANSCRIPT_CONTENT_KEY)
|
|
749
|
+
if isinstance(content, str):
|
|
750
|
+
return content.strip()
|
|
751
|
+
if not isinstance(content, list):
|
|
752
|
+
return ""
|
|
753
|
+
return TRANSCRIPT_TEXT_SEPARATOR.join(
|
|
754
|
+
each_block[TRANSCRIPT_TEXT_KEY]
|
|
755
|
+
for each_block in content
|
|
756
|
+
if isinstance(each_block, dict)
|
|
757
|
+
and each_block.get(TRANSCRIPT_BLOCK_TYPE_KEY) == TRANSCRIPT_TEXT_BLOCK_TYPE
|
|
758
|
+
and isinstance(each_block.get(TRANSCRIPT_TEXT_KEY), str)
|
|
759
|
+
).strip()
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
def _is_assistant_entry(transcript_entry: dict) -> bool:
|
|
763
|
+
return transcript_entry.get(TRANSCRIPT_ENTRY_TYPE_KEY) == TRANSCRIPT_ASSISTANT_TYPE
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
def _last_assistant_message_text(transcript: _ReversedTranscript) -> str:
|
|
767
|
+
all_text_parts: list[str] = []
|
|
768
|
+
last_message_id: object = None
|
|
769
|
+
for each_entry in transcript:
|
|
770
|
+
is_first_assistant_entry = not all_text_parts and last_message_id is None
|
|
771
|
+
if is_first_assistant_entry and not _is_assistant_entry(each_entry):
|
|
772
|
+
continue
|
|
773
|
+
message_id = _entry_message(each_entry).get(TRANSCRIPT_MESSAGE_ID_KEY)
|
|
774
|
+
if not is_first_assistant_entry and (
|
|
775
|
+
not _is_assistant_entry(each_entry) or message_id != last_message_id
|
|
776
|
+
):
|
|
777
|
+
break
|
|
778
|
+
all_text_parts.append(_entry_text(each_entry))
|
|
779
|
+
if message_id is None:
|
|
780
|
+
break
|
|
781
|
+
last_message_id = message_id
|
|
782
|
+
return TRANSCRIPT_TEXT_SEPARATOR.join(reversed(all_text_parts)).strip()
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
def _reply_text(hook_input: dict, transcript: _ReversedTranscript) -> str:
|
|
786
|
+
reply_text = hook_input.get(REPLY_TEXT_KEY)
|
|
787
|
+
if isinstance(reply_text, str):
|
|
788
|
+
return reply_text
|
|
789
|
+
return _last_assistant_message_text(transcript)
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
def _typed_user_text(transcript_entry: dict) -> str:
|
|
793
|
+
if transcript_entry.get(TRANSCRIPT_ENTRY_TYPE_KEY) != TRANSCRIPT_USER_TYPE:
|
|
794
|
+
return ""
|
|
795
|
+
if any(transcript_entry.get(each_flag) for each_flag in ALL_TRANSCRIPT_NOT_TYPED_FLAGS):
|
|
796
|
+
return ""
|
|
797
|
+
origin = transcript_entry.get(TRANSCRIPT_ORIGIN_KEY)
|
|
798
|
+
if (
|
|
799
|
+
not isinstance(origin, dict)
|
|
800
|
+
or origin.get(TRANSCRIPT_ORIGIN_KIND_KEY) != TRANSCRIPT_HUMAN_ORIGIN_KIND
|
|
801
|
+
):
|
|
802
|
+
return ""
|
|
803
|
+
typed_text = _entry_text(transcript_entry)
|
|
804
|
+
return "" if typed_text.startswith(TRANSCRIPT_MARKUP_START) else typed_text
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
def _typed_user_messages(transcript: _ReversedTranscript) -> list[str]:
|
|
808
|
+
all_messages: list[str] = []
|
|
809
|
+
for each_entry in transcript:
|
|
810
|
+
typed_text = _typed_user_text(each_entry)
|
|
811
|
+
if typed_text:
|
|
812
|
+
all_messages.append(typed_text[:MAXIMUM_USER_MESSAGE_CHARACTERS])
|
|
813
|
+
if len(all_messages) == USER_MESSAGE_COUNT:
|
|
814
|
+
break
|
|
815
|
+
return list(reversed(all_messages))
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
def _judge_requests(
|
|
819
|
+
review_state: _ReviewState,
|
|
820
|
+
all_dismiss_reasons: dict[str, str],
|
|
821
|
+
all_roots: list[Path],
|
|
822
|
+
is_capped: bool,
|
|
823
|
+
) -> list[_JudgeRequest]:
|
|
824
|
+
all_root_names = {str(each_root) for each_root in all_roots}
|
|
825
|
+
all_requests: list[_JudgeRequest] = []
|
|
826
|
+
for each_id, each_reason in all_dismiss_reasons.items():
|
|
827
|
+
finding = review_state.all_findings.get(each_id)
|
|
828
|
+
skip_diagnostic = _dismiss_skip_diagnostic(each_id, finding, all_root_names, is_capped)
|
|
829
|
+
if skip_diagnostic is not None:
|
|
830
|
+
_log(skip_diagnostic)
|
|
831
|
+
continue
|
|
832
|
+
if finding is not None:
|
|
833
|
+
all_requests.append(_JudgeRequest(each_id, finding, each_reason))
|
|
834
|
+
return all_requests
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
def _dismiss_skip_diagnostic(
|
|
838
|
+
finding_id: str, finding: _Finding | None, all_root_names: set[str], is_capped: bool
|
|
839
|
+
) -> str | None:
|
|
840
|
+
if finding is None:
|
|
841
|
+
return DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE.format(
|
|
842
|
+
finding_id=finding_id, status=UNKNOWN_FINDING_LABEL
|
|
843
|
+
)
|
|
844
|
+
if finding.status != FINDING_STATUS_OPEN:
|
|
845
|
+
return DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE.format(
|
|
846
|
+
finding_id=finding_id, status=finding.status
|
|
847
|
+
)
|
|
848
|
+
if finding.repository not in all_root_names:
|
|
849
|
+
return DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE.format(
|
|
850
|
+
finding_id=finding_id, status=OUTSIDE_REPOSITORY_LABEL
|
|
851
|
+
)
|
|
852
|
+
if is_capped:
|
|
853
|
+
return DISMISS_AT_CAP_DIAGNOSTIC_TEMPLATE.format(finding_id=finding_id)
|
|
854
|
+
return None
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
def _claim_locked_stop(
|
|
858
|
+
session_id: object, all_roots: list[Path], all_dismiss_reasons: dict[str, str]
|
|
859
|
+
) -> _StopClaim:
|
|
860
|
+
review_state = _load_state(session_id)
|
|
861
|
+
is_capped = review_state.blocks_in_a_row >= MAXIMUM_BLOCKS_IN_A_ROW
|
|
862
|
+
all_judge_requests = _judge_requests(review_state, all_dismiss_reasons, all_roots, is_capped)
|
|
306
863
|
all_next_baselines: dict[str, str] = {}
|
|
307
|
-
|
|
308
|
-
review_deadline = time.monotonic() + REVIEW_TIMEOUT_SECONDS
|
|
864
|
+
all_turn_ranges: list[tuple[Path, str, str]] = []
|
|
309
865
|
for each_root in all_roots:
|
|
310
|
-
base_commit = all_baselines.get(str(each_root))
|
|
866
|
+
base_commit = review_state.all_baselines.get(str(each_root))
|
|
311
867
|
turn_commit = working_tree_snapshot(each_root, base_commit)
|
|
312
868
|
if turn_commit is None:
|
|
313
869
|
continue
|
|
314
870
|
all_next_baselines[str(each_root)] = turn_commit
|
|
315
|
-
if base_commit is None
|
|
871
|
+
if base_commit is not None and base_commit != turn_commit and not is_capped:
|
|
872
|
+
all_turn_ranges.append((each_root, base_commit, turn_commit))
|
|
873
|
+
if not all_turn_ranges and not all_judge_requests and not is_capped:
|
|
874
|
+
review_state.blocks_in_a_row = 0
|
|
875
|
+
review_state.all_baselines = all_next_baselines
|
|
876
|
+
review_state.is_turn_open = (
|
|
877
|
+
review_state.open_turn_prompt_at is not None
|
|
878
|
+
and time.time() - review_state.open_turn_prompt_at < RACING_STOP_WINDOW_SECONDS
|
|
879
|
+
)
|
|
880
|
+
review_state.open_turn_prompt_at = None
|
|
881
|
+
_save_state(session_id, review_state)
|
|
882
|
+
return _StopClaim(all_turn_ranges, all_judge_requests)
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
def _claim_stop(
|
|
886
|
+
session_id: object, all_roots: list[Path], all_dismiss_reasons: dict[str, str]
|
|
887
|
+
) -> _StopClaim:
|
|
888
|
+
with _state_lock(session_id, STATE_LOCK_WAIT_SECONDS) as is_locked:
|
|
889
|
+
if not is_locked:
|
|
890
|
+
_log(STATE_LOCK_BUSY_DIAGNOSTIC_TEMPLATE.format(event=STOP_EVENT_NAME))
|
|
891
|
+
return _StopClaim()
|
|
892
|
+
return _claim_locked_stop(session_id, all_roots, all_dismiss_reasons)
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
def _judge_prompt(
|
|
896
|
+
repository_root: Path, all_requests: list[_JudgeRequest], all_user_messages: list[str]
|
|
897
|
+
) -> str:
|
|
898
|
+
judge_data = {
|
|
899
|
+
JUDGE_DATA_REQUESTS_KEY: [
|
|
900
|
+
{
|
|
901
|
+
JUDGE_ID_KEY: each_request.finding_id,
|
|
902
|
+
JUDGE_REQUEST_RANGE_KEY: JUDGE_COMMIT_RANGE_TEMPLATE.format(
|
|
903
|
+
base_commit=each_request.finding.base_commit,
|
|
904
|
+
turn_commit=each_request.finding.turn_commit,
|
|
905
|
+
),
|
|
906
|
+
JUDGE_REQUEST_FINDING_KEY: each_request.finding.text,
|
|
907
|
+
JUDGE_REQUEST_REASON_KEY: each_request.reason,
|
|
908
|
+
}
|
|
909
|
+
for each_request in all_requests
|
|
910
|
+
],
|
|
911
|
+
JUDGE_DATA_USER_MESSAGES_KEY: all_user_messages,
|
|
912
|
+
}
|
|
913
|
+
return JUDGE_PROMPT_TEMPLATE.format(
|
|
914
|
+
repository=repository_root,
|
|
915
|
+
user_message_characters=MAXIMUM_USER_MESSAGE_CHARACTERS,
|
|
916
|
+
judge_data=json.dumps(judge_data, indent=JUDGE_DATA_INDENT, ensure_ascii=True),
|
|
917
|
+
)
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
def _repository_verdicts(
|
|
921
|
+
repository_root: Path,
|
|
922
|
+
all_requests: list[_JudgeRequest],
|
|
923
|
+
all_user_messages: list[str],
|
|
924
|
+
deadline: float,
|
|
925
|
+
) -> dict[str, JudgeVerdict]:
|
|
926
|
+
judge_run = _ChildRun(
|
|
927
|
+
repository_root,
|
|
928
|
+
_judge_command(),
|
|
929
|
+
_judge_prompt(repository_root, all_requests, all_user_messages),
|
|
930
|
+
JUDGE_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
931
|
+
)
|
|
932
|
+
result_text = _child_result_text(judge_run, deadline)
|
|
933
|
+
if result_text is None:
|
|
934
|
+
return {}
|
|
935
|
+
all_requested_ids = [each_request.finding_id for each_request in all_requests]
|
|
936
|
+
all_verdicts = judge_verdicts(result_text, all_requested_ids)
|
|
937
|
+
if all_verdicts is None:
|
|
938
|
+
judge_run.write_failure("answer was not one JSON array of verdicts")
|
|
939
|
+
return {}
|
|
940
|
+
for each_id in all_requested_ids:
|
|
941
|
+
if each_id not in all_verdicts:
|
|
942
|
+
_log(NO_VERDICT_DIAGNOSTIC_TEMPLATE.format(finding_id=each_id))
|
|
943
|
+
return all_verdicts
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
def _all_judge_verdicts(
|
|
947
|
+
all_requests: list[_JudgeRequest],
|
|
948
|
+
all_roots: list[Path],
|
|
949
|
+
all_user_messages: list[str],
|
|
950
|
+
deadline: float,
|
|
951
|
+
) -> dict[str, JudgeVerdict]:
|
|
952
|
+
all_verdicts: dict[str, JudgeVerdict] = {}
|
|
953
|
+
for each_root in all_roots:
|
|
954
|
+
all_root_requests = [
|
|
955
|
+
each_request
|
|
956
|
+
for each_request in all_requests
|
|
957
|
+
if each_request.finding.repository == str(each_root)
|
|
958
|
+
]
|
|
959
|
+
if all_root_requests:
|
|
960
|
+
all_verdicts.update(
|
|
961
|
+
_repository_verdicts(each_root, all_root_requests, all_user_messages, deadline)
|
|
962
|
+
)
|
|
963
|
+
return all_verdicts
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
def _one_line(judge_reason: str) -> str:
|
|
967
|
+
return WORD_SEPARATOR.join(judge_reason.split())
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
def _one_sentence(judge_reason: str) -> str:
|
|
971
|
+
return _one_line(judge_reason).rstrip(SENTENCE_END_CHARACTERS)
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
def _apply_verdicts(
|
|
975
|
+
review_state: _ReviewState, all_verdicts: dict[str, JudgeVerdict]
|
|
976
|
+
) -> list[str]:
|
|
977
|
+
all_rejection_lines: list[str] = []
|
|
978
|
+
for each_id in sorted(all_verdicts, key=_finding_number):
|
|
979
|
+
finding = review_state.all_findings.get(each_id)
|
|
980
|
+
if finding is None or finding.status != FINDING_STATUS_OPEN:
|
|
981
|
+
continue
|
|
982
|
+
verdict = all_verdicts[each_id]
|
|
983
|
+
finding.judge_reason = verdict.reason
|
|
984
|
+
if verdict.is_approved:
|
|
985
|
+
finding.status = FINDING_STATUS_DISMISSED
|
|
986
|
+
_log(
|
|
987
|
+
FINDING_DISMISSED_DIAGNOSTIC_TEMPLATE.format(
|
|
988
|
+
finding_id=each_id, reason=_one_line(verdict.reason)
|
|
989
|
+
)
|
|
990
|
+
)
|
|
316
991
|
continue
|
|
317
|
-
|
|
318
|
-
|
|
992
|
+
finding.status = FINDING_STATUS_UPHELD
|
|
993
|
+
_log(
|
|
994
|
+
FINDING_UPHELD_DIAGNOSTIC_TEMPLATE.format(
|
|
995
|
+
finding_id=each_id, reason=_one_line(verdict.reason)
|
|
996
|
+
)
|
|
997
|
+
)
|
|
998
|
+
all_rejection_lines.append(
|
|
999
|
+
REJECTION_LINE_TEMPLATE.format(
|
|
1000
|
+
finding_id=each_id, reason=_one_sentence(verdict.reason)
|
|
1001
|
+
)
|
|
319
1002
|
)
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
1003
|
+
return all_rejection_lines
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
def _record_new_findings(
|
|
1007
|
+
review_state: _ReviewState, all_repository_findings: list[tuple[Path, str, str, list[str]]]
|
|
1008
|
+
) -> list[str]:
|
|
1009
|
+
all_repository_blocks: list[str] = []
|
|
1010
|
+
for each_root, each_base_commit, each_turn_commit, each_finding_lines in all_repository_findings:
|
|
1011
|
+
all_lines_with_ids: list[str] = []
|
|
1012
|
+
for each_line in each_finding_lines:
|
|
1013
|
+
finding_id = _next_finding_id(review_state)
|
|
1014
|
+
review_state.all_findings[finding_id] = _Finding(
|
|
1015
|
+
each_line, str(each_root), each_base_commit, each_turn_commit
|
|
1016
|
+
)
|
|
1017
|
+
all_lines_with_ids.append(
|
|
1018
|
+
FINDING_LINE_WITH_ID_TEMPLATE.format(finding_id=finding_id, finding=each_line)
|
|
1019
|
+
)
|
|
1020
|
+
all_repository_blocks.append(
|
|
1021
|
+
REPOSITORY_FINDINGS_TEMPLATE.format(
|
|
1022
|
+
repository=each_root.name,
|
|
1023
|
+
findings=FINDING_LINE_SEPARATOR.join(all_lines_with_ids),
|
|
1024
|
+
)
|
|
1025
|
+
)
|
|
1026
|
+
return all_repository_blocks
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
def _locked_stop_wake(
|
|
1030
|
+
session_id: object,
|
|
1031
|
+
all_repository_findings: list[tuple[Path, str, str, list[str]]],
|
|
1032
|
+
all_verdicts: dict[str, JudgeVerdict],
|
|
1033
|
+
) -> str | None:
|
|
1034
|
+
review_state = _load_state(session_id)
|
|
1035
|
+
all_wake_parts: list[str] = []
|
|
1036
|
+
all_rejection_lines = _apply_verdicts(review_state, all_verdicts)
|
|
1037
|
+
if all_rejection_lines:
|
|
1038
|
+
all_wake_parts.append(REJECTION_LINE_SEPARATOR.join(all_rejection_lines))
|
|
1039
|
+
all_repository_blocks = _record_new_findings(review_state, all_repository_findings)
|
|
1040
|
+
if all_repository_blocks:
|
|
1041
|
+
all_wake_parts.append(
|
|
1042
|
+
BLOCK_REASON_TEMPLATE.format(
|
|
1043
|
+
all_findings=REPOSITORY_FINDINGS_SEPARATOR.join(all_repository_blocks)
|
|
1044
|
+
)
|
|
1045
|
+
)
|
|
1046
|
+
if all_wake_parts:
|
|
1047
|
+
review_state.blocks_in_a_row += 1
|
|
1048
|
+
review_state.last_wake_at = time.time()
|
|
1049
|
+
elif review_state.blocks_in_a_row < MAXIMUM_BLOCKS_IN_A_ROW:
|
|
1050
|
+
review_state.blocks_in_a_row = 0
|
|
1051
|
+
_save_state(session_id, review_state)
|
|
1052
|
+
return WAKE_PART_SEPARATOR.join(all_wake_parts) or None
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
def _saved_stop_wake(
|
|
1056
|
+
session_id: object,
|
|
1057
|
+
all_repository_findings: list[tuple[Path, str, str, list[str]]],
|
|
1058
|
+
all_verdicts: dict[str, JudgeVerdict],
|
|
1059
|
+
) -> str | None:
|
|
1060
|
+
with _state_lock(session_id, STATE_LOCK_OUTCOME_WAIT_SECONDS) as is_locked:
|
|
1061
|
+
if not is_locked:
|
|
1062
|
+
_log(STATE_LOCK_BUSY_DIAGNOSTIC_TEMPLATE.format(event=STOP_EVENT_NAME))
|
|
1063
|
+
return None
|
|
1064
|
+
return _locked_stop_wake(session_id, all_repository_findings, all_verdicts)
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
def _turn_block_reason(
|
|
1068
|
+
session_id: object, all_roots: list[Path], hook_input: dict | None = None
|
|
1069
|
+
) -> str | None:
|
|
1070
|
+
stop_input = hook_input or {}
|
|
1071
|
+
transcript = _ReversedTranscript(stop_input)
|
|
1072
|
+
stop_claim = _claim_stop(
|
|
1073
|
+
session_id, all_roots, dismiss_requests(_reply_text(stop_input, transcript))
|
|
1074
|
+
)
|
|
1075
|
+
if not stop_claim.all_turn_ranges and not stop_claim.all_judge_requests:
|
|
328
1076
|
return None
|
|
329
|
-
|
|
330
|
-
|
|
1077
|
+
all_repository_findings: list[tuple[Path, str, str, list[str]]] = []
|
|
1078
|
+
deadline = time.monotonic() + min(REVIEW_TIMEOUT_SECONDS, remaining_seconds())
|
|
1079
|
+
for each_root, each_base_commit, each_turn_commit in stop_claim.all_turn_ranges:
|
|
1080
|
+
all_lines = _repository_findings(each_root, each_base_commit, each_turn_commit, deadline)
|
|
1081
|
+
if all_lines:
|
|
1082
|
+
all_repository_findings.append(
|
|
1083
|
+
(each_root, each_base_commit, each_turn_commit, all_lines)
|
|
1084
|
+
)
|
|
1085
|
+
all_user_messages = (
|
|
1086
|
+
_typed_user_messages(transcript) if stop_claim.all_judge_requests else []
|
|
331
1087
|
)
|
|
1088
|
+
all_verdicts = _all_judge_verdicts(
|
|
1089
|
+
stop_claim.all_judge_requests, all_roots, all_user_messages, deadline
|
|
1090
|
+
)
|
|
1091
|
+
return _saved_stop_wake(session_id, all_repository_findings, all_verdicts)
|
|
332
1092
|
|
|
333
1093
|
|
|
334
|
-
def
|
|
335
|
-
hook_input = read_hook_input_dictionary_from_stdin()
|
|
336
|
-
if hook_input is None:
|
|
337
|
-
return 0
|
|
338
|
-
if os.environ.get(REVIEW_CHILD_ENVIRONMENT_VARIABLE) == REVIEW_CHILD_MARKER:
|
|
339
|
-
return 0
|
|
1094
|
+
def _handle_event(hook_input: dict, hook_event_name: object) -> int:
|
|
340
1095
|
all_slugs = _opted_in_repository_slugs()
|
|
341
1096
|
if not all_slugs:
|
|
342
1097
|
return 0
|
|
@@ -344,18 +1099,36 @@ def main() -> int:
|
|
|
344
1099
|
if not all_roots:
|
|
345
1100
|
return 0
|
|
346
1101
|
session_id = hook_input.get(SESSION_ID_KEY)
|
|
347
|
-
|
|
1102
|
+
_log(
|
|
1103
|
+
RUN_DIAGNOSTIC_TEMPLATE.format(
|
|
1104
|
+
event=hook_event_name, stop_hook_active=hook_input.get(STOP_HOOK_ACTIVE_KEY)
|
|
1105
|
+
)
|
|
1106
|
+
)
|
|
348
1107
|
if hook_event_name == PROMPT_EVENT_NAME:
|
|
349
|
-
_record_prompt_baselines(session_id, all_roots)
|
|
350
|
-
return 0
|
|
351
|
-
if hook_event_name != STOP_EVENT_NAME:
|
|
1108
|
+
_record_prompt_baselines(session_id, all_roots, hook_input.get(PROMPT_KEY))
|
|
352
1109
|
return 0
|
|
353
|
-
block_reason = _turn_block_reason(session_id, all_roots)
|
|
1110
|
+
block_reason = _turn_block_reason(session_id, all_roots, hook_input)
|
|
354
1111
|
if block_reason is None:
|
|
355
1112
|
return 0
|
|
356
1113
|
log_hook_block(Path(__file__).name, STOP_EVENT_NAME, block_reason)
|
|
357
|
-
|
|
358
|
-
|
|
1114
|
+
return hold_stop(hook_input, block_reason)
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
def main() -> int:
|
|
1118
|
+
hook_input = read_hook_input_dictionary_from_stdin()
|
|
1119
|
+
if hook_input is None:
|
|
1120
|
+
return 0
|
|
1121
|
+
if os.environ.get(REVIEW_CHILD_ENVIRONMENT_VARIABLE) == REVIEW_CHILD_MARKER:
|
|
1122
|
+
return 0
|
|
1123
|
+
hook_event_name = hook_input.get(HOOK_EVENT_NAME_KEY)
|
|
1124
|
+
if hook_event_name == STOP_EVENT_NAME:
|
|
1125
|
+
run_budget_seconds = STOP_RUN_BUDGET_SECONDS
|
|
1126
|
+
elif hook_event_name == PROMPT_EVENT_NAME:
|
|
1127
|
+
run_budget_seconds = PROMPT_RUN_BUDGET_SECONDS
|
|
1128
|
+
else:
|
|
1129
|
+
return 0
|
|
1130
|
+
with time_budget(run_budget_seconds):
|
|
1131
|
+
return _handle_event(hook_input, hook_event_name)
|
|
359
1132
|
|
|
360
1133
|
|
|
361
1134
|
if __name__ == "__main__":
|