claude-dev-env 8.117.0 → 8.117.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ever-registered-hooks.test.mjs +4 -0
- package/hooks/blocking/test_turn_review_child.py +166 -0
- package/hooks/blocking/test_turn_review_claim.py +194 -0
- package/hooks/blocking/test_turn_review_repositories.py +65 -0
- package/hooks/blocking/test_turn_review_state.py +132 -0
- package/hooks/blocking/test_turn_review_transcript.py +125 -0
- package/hooks/blocking/turn_review_child.py +145 -0
- package/hooks/blocking/turn_review_claim.py +306 -0
- package/hooks/blocking/turn_review_gate.py +32 -808
- package/hooks/blocking/turn_review_repositories.py +58 -0
- package/hooks/blocking/turn_review_state.py +260 -0
- package/hooks/blocking/turn_review_transcript.py +158 -0
- package/hooks/features/conduct-gates.md +1 -1
- package/hooks/hooks_constants/session_title_constants.py +2 -2
- package/hooks/routing/test_thread_spawn_pace_hook.py +32 -0
- package/hooks/session/test_background_silence_prompt.py +8 -0
- package/hooks/session/test_tracking_nudge.py +11 -0
- package/hooks/session_title_transcript.py +38 -17
- package/hooks/test_session_title_transcript.py +31 -0
- package/package.json +1 -1
- package/scripts/account_broker_support.py +24 -5
- package/scripts/dev_env_scripts_constants/account_broker_constants.py +1 -0
- package/scripts/policy_lint/config/approved_test_pairs.py +27 -0
- package/scripts/test_account_broker_support.py +43 -1
- package/scripts/tests/test_policy_lint_rules_pairing.py +24 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import tempfile
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from turn_review_transcript import _ReversedTranscript, _reply_text, _typed_user_messages
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.fixture(autouse=True)
|
|
11
|
+
def isolated_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
12
|
+
home_directory = tmp_path / "home"
|
|
13
|
+
home_directory.mkdir()
|
|
14
|
+
state_directory = tmp_path / "state"
|
|
15
|
+
state_directory.mkdir()
|
|
16
|
+
monkeypatch.setenv("HOME", str(home_directory))
|
|
17
|
+
monkeypatch.setenv("USERPROFILE", str(home_directory))
|
|
18
|
+
monkeypatch.setattr(tempfile, "tempdir", str(state_directory))
|
|
19
|
+
return home_directory
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def assistant_entry(message_id: str, text: str) -> dict[str, object]:
|
|
23
|
+
return {
|
|
24
|
+
"type": "assistant",
|
|
25
|
+
"message": {"id": message_id, "content": [{"type": "text", "text": text}]},
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def typed_entry(text: str, **all_extra_fields: object) -> dict[str, object]:
|
|
30
|
+
return {
|
|
31
|
+
"type": "user",
|
|
32
|
+
"origin": {"kind": "human"},
|
|
33
|
+
"message": {"content": text},
|
|
34
|
+
**all_extra_fields,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def tool_result_entry() -> dict[str, object]:
|
|
39
|
+
return {
|
|
40
|
+
"type": "user",
|
|
41
|
+
"toolUseResult": {"stdout": "ok"},
|
|
42
|
+
"message": {"content": [{"type": "tool_result", "content": "ok"}]},
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def stop_input(
|
|
47
|
+
tmp_path: Path, all_entries: list[dict[str, object]], **all_extra_fields: object
|
|
48
|
+
) -> dict:
|
|
49
|
+
transcript_path = tmp_path / "session.jsonl"
|
|
50
|
+
transcript_path.write_text(
|
|
51
|
+
"\n".join(json.dumps(each_entry) for each_entry in all_entries) + "\n"
|
|
52
|
+
)
|
|
53
|
+
return {"transcript_path": str(transcript_path), **all_extra_fields}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_should_take_the_reply_field_even_when_it_is_empty(tmp_path: Path) -> None:
|
|
57
|
+
hook_input = stop_input(
|
|
58
|
+
tmp_path, [assistant_entry("m1", "Dismiss TR-1: an older reply")], last_assistant_message=""
|
|
59
|
+
)
|
|
60
|
+
assert _reply_text(hook_input, _ReversedTranscript(hook_input)) == ""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_should_join_the_entries_of_the_last_assistant_message_oldest_first(tmp_path: Path) -> None:
|
|
64
|
+
hook_input = stop_input(
|
|
65
|
+
tmp_path,
|
|
66
|
+
[
|
|
67
|
+
assistant_entry("m1", "earlier reply"),
|
|
68
|
+
typed_entry("next question"),
|
|
69
|
+
assistant_entry("m2", "first part"),
|
|
70
|
+
assistant_entry("m2", "second part"),
|
|
71
|
+
tool_result_entry(),
|
|
72
|
+
],
|
|
73
|
+
)
|
|
74
|
+
assert _reply_text(hook_input, _ReversedTranscript(hook_input)) == "first part\nsecond part"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_should_still_see_every_entry_after_the_reply_read_part_of_the_transcript(
|
|
78
|
+
tmp_path: Path,
|
|
79
|
+
) -> None:
|
|
80
|
+
hook_input = stop_input(
|
|
81
|
+
tmp_path, [typed_entry("first ask"), assistant_entry("m1", "the reply")]
|
|
82
|
+
)
|
|
83
|
+
transcript = _ReversedTranscript(hook_input)
|
|
84
|
+
assert _reply_text(hook_input, transcript) == "the reply"
|
|
85
|
+
assert _typed_user_messages(transcript) == ["first ask"]
|
|
86
|
+
assert len(list(transcript)) == 2
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_should_keep_the_last_five_typed_messages_oldest_first_and_cut_each(tmp_path: Path) -> None:
|
|
90
|
+
hook_input = stop_input(
|
|
91
|
+
tmp_path,
|
|
92
|
+
[
|
|
93
|
+
typed_entry("first"),
|
|
94
|
+
typed_entry("second"),
|
|
95
|
+
typed_entry("x" * 2500),
|
|
96
|
+
typed_entry("<system-reminder>injected</system-reminder>"),
|
|
97
|
+
typed_entry("meta words", isMeta=True),
|
|
98
|
+
typed_entry("third"),
|
|
99
|
+
{"type": "user", "origin": {"kind": "peer"}, "message": {"content": "peer words"}},
|
|
100
|
+
typed_entry("fourth"),
|
|
101
|
+
assistant_entry("m1", "a reply"),
|
|
102
|
+
typed_entry("fifth"),
|
|
103
|
+
],
|
|
104
|
+
)
|
|
105
|
+
assert _typed_user_messages(_ReversedTranscript(hook_input)) == [
|
|
106
|
+
"second",
|
|
107
|
+
"x" * 2000,
|
|
108
|
+
"third",
|
|
109
|
+
"fourth",
|
|
110
|
+
"fifth",
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_should_log_a_line_that_nests_too_deep_and_read_the_lines_before_it(
|
|
115
|
+
tmp_path: Path, isolated_home: Path
|
|
116
|
+
) -> None:
|
|
117
|
+
hook_input = stop_input(tmp_path, [typed_entry("the ask")])
|
|
118
|
+
with Path(hook_input["transcript_path"]).open("a") as transcript_file:
|
|
119
|
+
transcript_file.write("[" * 100_000 + "]" * 100_000 + "\n")
|
|
120
|
+
assert _typed_user_messages(_ReversedTranscript(hook_input)) == ["the ask"]
|
|
121
|
+
log_text = (isolated_home / ".claude" / "logs" / "turn-review.log").read_text()
|
|
122
|
+
assert (
|
|
123
|
+
log_text.count("[turn_review_gate] a transcript line could not be read: it nests too deep")
|
|
124
|
+
== 1
|
|
125
|
+
)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Start the review and judge runs for the turn review gate.
|
|
2
|
+
|
|
3
|
+
Each run is a separate ``claude -p`` process that starts in the checkout it
|
|
4
|
+
reads::
|
|
5
|
+
|
|
6
|
+
review -> claude -p "/code-review low A..B"
|
|
7
|
+
judge -> claude -p --model sonnet --effort high, prompt on stdin
|
|
8
|
+
|
|
9
|
+
The review and judge
|
|
10
|
+
runs carry ``CLAUDE_TURN_REVIEW_CHILD=1`` and none of the parent session's id,
|
|
11
|
+
messaging or ingress variables. Each may read
|
|
12
|
+
files and run ``git diff``, ``git show`` and ``git log``; every other tool is
|
|
13
|
+
denied. A run that times out, cannot start or ends with another exit code than
|
|
14
|
+
0 writes one log line and gives no output.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import os
|
|
20
|
+
import shutil
|
|
21
|
+
import subprocess
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
from hooks_constants.subprocess_window import hidden_window_creation_flags
|
|
26
|
+
from hooks_constants.turn_review_gate_constants import (
|
|
27
|
+
ALL_PARENT_SESSION_VARIABLES,
|
|
28
|
+
ALL_REVIEW_ALLOWED_TOOLS,
|
|
29
|
+
ALL_REVIEW_DISALLOWED_TOOLS,
|
|
30
|
+
JUDGE_EFFORT,
|
|
31
|
+
JUDGE_MODEL_ALIAS,
|
|
32
|
+
REVIEW_CHILD_ENVIRONMENT_VARIABLE,
|
|
33
|
+
REVIEW_CHILD_MARKER,
|
|
34
|
+
REVIEW_EFFORT,
|
|
35
|
+
REVIEW_EXECUTABLE_NAME,
|
|
36
|
+
REVIEW_MODEL_ALIAS,
|
|
37
|
+
REVIEW_MODEL_EFFORT,
|
|
38
|
+
REVIEW_PROMPT_TEMPLATE,
|
|
39
|
+
REVIEW_SETTINGS_TEXT,
|
|
40
|
+
REVIEW_SETTING_SOURCES_TEXT,
|
|
41
|
+
REVIEW_TOOLS_TEXT,
|
|
42
|
+
)
|
|
43
|
+
from turn_review_state import _log
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _review_command(base_commit: str, turn_commit: str) -> list[str]:
|
|
47
|
+
review_prompt = REVIEW_PROMPT_TEMPLATE.format(
|
|
48
|
+
effort=REVIEW_EFFORT, base_commit=base_commit, turn_commit=turn_commit
|
|
49
|
+
)
|
|
50
|
+
return [
|
|
51
|
+
shutil.which(REVIEW_EXECUTABLE_NAME) or REVIEW_EXECUTABLE_NAME,
|
|
52
|
+
"-p",
|
|
53
|
+
review_prompt,
|
|
54
|
+
"--model",
|
|
55
|
+
REVIEW_MODEL_ALIAS,
|
|
56
|
+
"--effort",
|
|
57
|
+
REVIEW_MODEL_EFFORT,
|
|
58
|
+
*_read_only_child_options(),
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _judge_command() -> list[str]:
|
|
63
|
+
return [
|
|
64
|
+
shutil.which(REVIEW_EXECUTABLE_NAME) or REVIEW_EXECUTABLE_NAME,
|
|
65
|
+
"-p",
|
|
66
|
+
"--model",
|
|
67
|
+
JUDGE_MODEL_ALIAS,
|
|
68
|
+
"--effort",
|
|
69
|
+
JUDGE_EFFORT,
|
|
70
|
+
*_read_only_child_options(),
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _read_only_child_options() -> list[str]:
|
|
75
|
+
return [
|
|
76
|
+
"--output-format",
|
|
77
|
+
"json",
|
|
78
|
+
"--permission-mode",
|
|
79
|
+
"dontAsk",
|
|
80
|
+
"--setting-sources",
|
|
81
|
+
REVIEW_SETTING_SOURCES_TEXT,
|
|
82
|
+
"--tools",
|
|
83
|
+
REVIEW_TOOLS_TEXT,
|
|
84
|
+
"--no-session-persistence",
|
|
85
|
+
"--allowedTools",
|
|
86
|
+
*ALL_REVIEW_ALLOWED_TOOLS,
|
|
87
|
+
"--disallowedTools",
|
|
88
|
+
*ALL_REVIEW_DISALLOWED_TOOLS,
|
|
89
|
+
"--settings",
|
|
90
|
+
REVIEW_SETTINGS_TEXT,
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _review_environment() -> dict[str, str]:
|
|
95
|
+
all_child_environment_variables = {
|
|
96
|
+
each_name: each_value
|
|
97
|
+
for each_name, each_value in os.environ.items()
|
|
98
|
+
if each_name not in ALL_PARENT_SESSION_VARIABLES
|
|
99
|
+
}
|
|
100
|
+
all_child_environment_variables[REVIEW_CHILD_ENVIRONMENT_VARIABLE] = REVIEW_CHILD_MARKER
|
|
101
|
+
return all_child_environment_variables
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass(frozen=True)
|
|
105
|
+
class _ChildRun:
|
|
106
|
+
repository_root: Path
|
|
107
|
+
all_command_parts: list[str]
|
|
108
|
+
prompt_text: str | None
|
|
109
|
+
failure_template: str
|
|
110
|
+
|
|
111
|
+
def write_failure(self, failure_text: str) -> None:
|
|
112
|
+
_log(
|
|
113
|
+
self.failure_template.format(
|
|
114
|
+
repository=self.repository_root.name, reason=failure_text
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _child_stdout(child_run: _ChildRun, remaining_seconds: float) -> str | None:
|
|
120
|
+
closed_stdin = subprocess.DEVNULL if child_run.prompt_text is None else None
|
|
121
|
+
try:
|
|
122
|
+
completed_process = subprocess.run(
|
|
123
|
+
child_run.all_command_parts,
|
|
124
|
+
check=False,
|
|
125
|
+
capture_output=True,
|
|
126
|
+
text=True,
|
|
127
|
+
encoding="utf-8",
|
|
128
|
+
errors="replace",
|
|
129
|
+
timeout=remaining_seconds,
|
|
130
|
+
cwd=str(child_run.repository_root),
|
|
131
|
+
env=_review_environment(),
|
|
132
|
+
creationflags=hidden_window_creation_flags(),
|
|
133
|
+
stdin=closed_stdin,
|
|
134
|
+
input=child_run.prompt_text,
|
|
135
|
+
)
|
|
136
|
+
except subprocess.TimeoutExpired:
|
|
137
|
+
child_run.write_failure("timed out")
|
|
138
|
+
return None
|
|
139
|
+
except OSError as start_error:
|
|
140
|
+
child_run.write_failure(str(start_error))
|
|
141
|
+
return None
|
|
142
|
+
if completed_process.returncode != 0:
|
|
143
|
+
child_run.write_failure(f"exit {completed_process.returncode}")
|
|
144
|
+
return None
|
|
145
|
+
return completed_process.stdout
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"""Change the session's review state for a prompt and a stop, for the turn review gate.
|
|
2
|
+
|
|
3
|
+
The gate calls these while it holds the lock on the session's state file. A
|
|
4
|
+
stop claims its range before the review starts. It moves the baseline to
|
|
5
|
+
snapshot B, marks the turn closed, and saves both. A second stop during the
|
|
6
|
+
review starts from B, so no range is reviewed twice.
|
|
7
|
+
|
|
8
|
+
A prompt marks the turn open. When the last turn is still open, no stop has
|
|
9
|
+
claimed it yet: its stop is racing this prompt, or the user interrupted it::
|
|
10
|
+
|
|
11
|
+
prompt 1 -> baseline A, turn open
|
|
12
|
+
stop 1 -> starts; the next prompt runs before its claim
|
|
13
|
+
prompt 2 -> turn still open, so A stays the baseline
|
|
14
|
+
stop 1 -> claims A..B and reviews turn 1's edits
|
|
15
|
+
|
|
16
|
+
Edits the user made between those two turns are then reviewed too. When a
|
|
17
|
+
prompt found the turn open less than 10 seconds before the claim, that prompt
|
|
18
|
+
began the next turn, so the stop leaves it open. A later claim closes it,
|
|
19
|
+
which ends the open turn an interrupt left behind.
|
|
20
|
+
|
|
21
|
+
The claim also lists the findings the reply contests. The judge gets the
|
|
22
|
+
checkout path and one JSON object of data::
|
|
23
|
+
|
|
24
|
+
{"requests": [{"id": "TR-1", "commit_range": "A..B",
|
|
25
|
+
"finding": "src/service.py:3 - ...", "reason_to_dismiss": "..."}],
|
|
26
|
+
"user_messages": ["the last five messages the user typed, cut to 2000 characters"]}
|
|
27
|
+
|
|
28
|
+
The judge gets no other part of the
|
|
29
|
+
conversation. A ``Dismiss`` line for an id that is not open, or
|
|
30
|
+
one sent at the hold cap, is logged and not judged.
|
|
31
|
+
|
|
32
|
+
After the review, each new finding gets the next id of the session and is
|
|
33
|
+
saved as open. An approved dismissal marks its finding dismissed. A rejection
|
|
34
|
+
marks it upheld and adds one line to the wake. A stop with a wake adds one to
|
|
35
|
+
the hold count. A stop below the cap with no
|
|
36
|
+
finding, no rejection and no dismissal to judge resets the count. A prompt within 10 seconds
|
|
37
|
+
of the last wake, or one that starts with ``<task-notification``, belongs to
|
|
38
|
+
the wake.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
from __future__ import annotations
|
|
42
|
+
|
|
43
|
+
import json
|
|
44
|
+
import time
|
|
45
|
+
from dataclasses import dataclass, field
|
|
46
|
+
from pathlib import Path
|
|
47
|
+
|
|
48
|
+
from hooks_constants.turn_review_gate_constants import (
|
|
49
|
+
BLOCK_REASON_TEMPLATE,
|
|
50
|
+
DISMISS_AT_CAP_DIAGNOSTIC_TEMPLATE,
|
|
51
|
+
DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE,
|
|
52
|
+
FINDING_DISMISSED_DIAGNOSTIC_TEMPLATE,
|
|
53
|
+
FINDING_LINE_SEPARATOR,
|
|
54
|
+
FINDING_LINE_WITH_ID_TEMPLATE,
|
|
55
|
+
FINDING_STATUS_DISMISSED,
|
|
56
|
+
FINDING_STATUS_OPEN,
|
|
57
|
+
FINDING_STATUS_UPHELD,
|
|
58
|
+
FINDING_UPHELD_DIAGNOSTIC_TEMPLATE,
|
|
59
|
+
JUDGE_COMMIT_RANGE_TEMPLATE,
|
|
60
|
+
JUDGE_DATA_INDENT,
|
|
61
|
+
JUDGE_DATA_REQUESTS_KEY,
|
|
62
|
+
JUDGE_DATA_USER_MESSAGES_KEY,
|
|
63
|
+
JUDGE_ID_KEY,
|
|
64
|
+
JUDGE_PROMPT_TEMPLATE,
|
|
65
|
+
JUDGE_REQUEST_FINDING_KEY,
|
|
66
|
+
JUDGE_REQUEST_RANGE_KEY,
|
|
67
|
+
JUDGE_REQUEST_REASON_KEY,
|
|
68
|
+
MACHINE_PROMPT_PREFIX,
|
|
69
|
+
MAXIMUM_BLOCKS_IN_A_ROW,
|
|
70
|
+
MAXIMUM_USER_MESSAGE_CHARACTERS,
|
|
71
|
+
OUTSIDE_REPOSITORY_LABEL,
|
|
72
|
+
RACING_STOP_WINDOW_SECONDS,
|
|
73
|
+
REJECTION_LINE_SEPARATOR,
|
|
74
|
+
REJECTION_LINE_TEMPLATE,
|
|
75
|
+
REPOSITORY_FINDINGS_SEPARATOR,
|
|
76
|
+
REPOSITORY_FINDINGS_TEMPLATE,
|
|
77
|
+
SENTENCE_END_CHARACTERS,
|
|
78
|
+
UNKNOWN_FINDING_LABEL,
|
|
79
|
+
WAKE_PART_SEPARATOR,
|
|
80
|
+
WAKE_PROMPT_WINDOW_SECONDS,
|
|
81
|
+
WORD_SEPARATOR,
|
|
82
|
+
)
|
|
83
|
+
from turn_review_findings import JudgeVerdict
|
|
84
|
+
from turn_review_snapshot import working_tree_snapshot
|
|
85
|
+
from turn_review_state import (
|
|
86
|
+
_Finding,
|
|
87
|
+
_ReviewState,
|
|
88
|
+
_finding_number,
|
|
89
|
+
_load_state,
|
|
90
|
+
_log,
|
|
91
|
+
_next_finding_id,
|
|
92
|
+
_save_state,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _is_wake_prompt(review_state: _ReviewState, prompt_text: object, now: float) -> bool:
|
|
97
|
+
if isinstance(prompt_text, str) and prompt_text.lstrip().startswith(MACHINE_PROMPT_PREFIX):
|
|
98
|
+
return True
|
|
99
|
+
if review_state.last_wake_at is None:
|
|
100
|
+
return False
|
|
101
|
+
return now - review_state.last_wake_at < WAKE_PROMPT_WINDOW_SECONDS
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _prompt_baselines(review_state: _ReviewState, all_roots: list[Path]) -> dict[str, str]:
|
|
105
|
+
all_baselines = dict(review_state.all_baselines) if review_state.is_turn_open else {}
|
|
106
|
+
for each_root in all_roots:
|
|
107
|
+
if str(each_root) in all_baselines:
|
|
108
|
+
continue
|
|
109
|
+
snapshot_commit = working_tree_snapshot(each_root, None)
|
|
110
|
+
if snapshot_commit is not None:
|
|
111
|
+
all_baselines[str(each_root)] = snapshot_commit
|
|
112
|
+
return all_baselines
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@dataclass(frozen=True)
|
|
116
|
+
class _JudgeRequest:
|
|
117
|
+
finding_id: str
|
|
118
|
+
finding: _Finding
|
|
119
|
+
reason: str
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@dataclass(frozen=True)
|
|
123
|
+
class _StopClaim:
|
|
124
|
+
all_turn_ranges: list[tuple[Path, str, str]] = field(default_factory=list)
|
|
125
|
+
all_judge_requests: list[_JudgeRequest] = field(default_factory=list)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _judge_requests(
|
|
129
|
+
review_state: _ReviewState,
|
|
130
|
+
all_dismiss_reasons: dict[str, str],
|
|
131
|
+
all_roots: list[Path],
|
|
132
|
+
is_capped: bool,
|
|
133
|
+
) -> list[_JudgeRequest]:
|
|
134
|
+
all_root_names = {str(each_root) for each_root in all_roots}
|
|
135
|
+
all_requests: list[_JudgeRequest] = []
|
|
136
|
+
for each_id, each_reason in all_dismiss_reasons.items():
|
|
137
|
+
finding = review_state.all_findings.get(each_id)
|
|
138
|
+
skip_diagnostic = _dismiss_skip_diagnostic(each_id, finding, all_root_names, is_capped)
|
|
139
|
+
if skip_diagnostic is not None:
|
|
140
|
+
_log(skip_diagnostic)
|
|
141
|
+
continue
|
|
142
|
+
if finding is not None:
|
|
143
|
+
all_requests.append(_JudgeRequest(each_id, finding, each_reason))
|
|
144
|
+
return all_requests
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _dismiss_skip_diagnostic(
|
|
148
|
+
finding_id: str, finding: _Finding | None, all_root_names: set[str], is_capped: bool
|
|
149
|
+
) -> str | None:
|
|
150
|
+
if finding is None:
|
|
151
|
+
return DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE.format(
|
|
152
|
+
finding_id=finding_id, status=UNKNOWN_FINDING_LABEL
|
|
153
|
+
)
|
|
154
|
+
if finding.status != FINDING_STATUS_OPEN:
|
|
155
|
+
return DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE.format(
|
|
156
|
+
finding_id=finding_id, status=finding.status
|
|
157
|
+
)
|
|
158
|
+
if finding.repository not in all_root_names:
|
|
159
|
+
return DISMISS_IGNORED_DIAGNOSTIC_TEMPLATE.format(
|
|
160
|
+
finding_id=finding_id, status=OUTSIDE_REPOSITORY_LABEL
|
|
161
|
+
)
|
|
162
|
+
if is_capped:
|
|
163
|
+
return DISMISS_AT_CAP_DIAGNOSTIC_TEMPLATE.format(finding_id=finding_id)
|
|
164
|
+
return None
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _claim_locked_stop(
|
|
168
|
+
session_id: object, all_roots: list[Path], all_dismiss_reasons: dict[str, str]
|
|
169
|
+
) -> _StopClaim:
|
|
170
|
+
review_state = _load_state(session_id)
|
|
171
|
+
is_capped = review_state.blocks_in_a_row >= MAXIMUM_BLOCKS_IN_A_ROW
|
|
172
|
+
all_judge_requests = _judge_requests(review_state, all_dismiss_reasons, all_roots, is_capped)
|
|
173
|
+
all_next_baselines: dict[str, str] = {}
|
|
174
|
+
all_turn_ranges: list[tuple[Path, str, str]] = []
|
|
175
|
+
for each_root in all_roots:
|
|
176
|
+
base_commit = review_state.all_baselines.get(str(each_root))
|
|
177
|
+
turn_commit = working_tree_snapshot(each_root, base_commit)
|
|
178
|
+
if turn_commit is None:
|
|
179
|
+
continue
|
|
180
|
+
all_next_baselines[str(each_root)] = turn_commit
|
|
181
|
+
if base_commit is not None and base_commit != turn_commit and not is_capped:
|
|
182
|
+
all_turn_ranges.append((each_root, base_commit, turn_commit))
|
|
183
|
+
if not all_turn_ranges and not all_judge_requests and not is_capped:
|
|
184
|
+
review_state.blocks_in_a_row = 0
|
|
185
|
+
review_state.all_baselines = all_next_baselines
|
|
186
|
+
review_state.is_turn_open = (
|
|
187
|
+
review_state.open_turn_prompt_at is not None
|
|
188
|
+
and time.time() - review_state.open_turn_prompt_at < RACING_STOP_WINDOW_SECONDS
|
|
189
|
+
)
|
|
190
|
+
review_state.open_turn_prompt_at = None
|
|
191
|
+
_save_state(session_id, review_state)
|
|
192
|
+
return _StopClaim(all_turn_ranges, all_judge_requests)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _judge_prompt(
|
|
196
|
+
repository_root: Path, all_requests: list[_JudgeRequest], all_user_messages: list[str]
|
|
197
|
+
) -> str:
|
|
198
|
+
judge_data = {
|
|
199
|
+
JUDGE_DATA_REQUESTS_KEY: [
|
|
200
|
+
{
|
|
201
|
+
JUDGE_ID_KEY: each_request.finding_id,
|
|
202
|
+
JUDGE_REQUEST_RANGE_KEY: JUDGE_COMMIT_RANGE_TEMPLATE.format(
|
|
203
|
+
base_commit=each_request.finding.base_commit,
|
|
204
|
+
turn_commit=each_request.finding.turn_commit,
|
|
205
|
+
),
|
|
206
|
+
JUDGE_REQUEST_FINDING_KEY: each_request.finding.text,
|
|
207
|
+
JUDGE_REQUEST_REASON_KEY: each_request.reason,
|
|
208
|
+
}
|
|
209
|
+
for each_request in all_requests
|
|
210
|
+
],
|
|
211
|
+
JUDGE_DATA_USER_MESSAGES_KEY: all_user_messages,
|
|
212
|
+
}
|
|
213
|
+
return JUDGE_PROMPT_TEMPLATE.format(
|
|
214
|
+
repository=repository_root,
|
|
215
|
+
user_message_characters=MAXIMUM_USER_MESSAGE_CHARACTERS,
|
|
216
|
+
judge_data=json.dumps(judge_data, indent=JUDGE_DATA_INDENT, ensure_ascii=True),
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _one_line(judge_reason: str) -> str:
|
|
221
|
+
return WORD_SEPARATOR.join(judge_reason.split())
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _one_sentence(judge_reason: str) -> str:
|
|
225
|
+
return _one_line(judge_reason).rstrip(SENTENCE_END_CHARACTERS)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _apply_verdicts(
|
|
229
|
+
review_state: _ReviewState, all_verdicts: dict[str, JudgeVerdict]
|
|
230
|
+
) -> list[str]:
|
|
231
|
+
all_rejection_lines: list[str] = []
|
|
232
|
+
for each_id in sorted(all_verdicts, key=_finding_number):
|
|
233
|
+
finding = review_state.all_findings.get(each_id)
|
|
234
|
+
if finding is None or finding.status != FINDING_STATUS_OPEN:
|
|
235
|
+
continue
|
|
236
|
+
verdict = all_verdicts[each_id]
|
|
237
|
+
finding.judge_reason = verdict.reason
|
|
238
|
+
if verdict.is_approved:
|
|
239
|
+
finding.status = FINDING_STATUS_DISMISSED
|
|
240
|
+
_log(
|
|
241
|
+
FINDING_DISMISSED_DIAGNOSTIC_TEMPLATE.format(
|
|
242
|
+
finding_id=each_id, reason=_one_line(verdict.reason)
|
|
243
|
+
)
|
|
244
|
+
)
|
|
245
|
+
continue
|
|
246
|
+
finding.status = FINDING_STATUS_UPHELD
|
|
247
|
+
_log(
|
|
248
|
+
FINDING_UPHELD_DIAGNOSTIC_TEMPLATE.format(
|
|
249
|
+
finding_id=each_id, reason=_one_line(verdict.reason)
|
|
250
|
+
)
|
|
251
|
+
)
|
|
252
|
+
all_rejection_lines.append(
|
|
253
|
+
REJECTION_LINE_TEMPLATE.format(
|
|
254
|
+
finding_id=each_id, reason=_one_sentence(verdict.reason)
|
|
255
|
+
)
|
|
256
|
+
)
|
|
257
|
+
return all_rejection_lines
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def _record_new_findings(
|
|
261
|
+
review_state: _ReviewState, all_repository_findings: list[tuple[Path, str, str, list[str]]]
|
|
262
|
+
) -> list[str]:
|
|
263
|
+
all_repository_blocks: list[str] = []
|
|
264
|
+
for each_root, each_base_commit, each_turn_commit, each_finding_lines in all_repository_findings:
|
|
265
|
+
all_lines_with_ids: list[str] = []
|
|
266
|
+
for each_line in each_finding_lines:
|
|
267
|
+
finding_id = _next_finding_id(review_state)
|
|
268
|
+
review_state.all_findings[finding_id] = _Finding(
|
|
269
|
+
each_line, str(each_root), each_base_commit, each_turn_commit
|
|
270
|
+
)
|
|
271
|
+
all_lines_with_ids.append(
|
|
272
|
+
FINDING_LINE_WITH_ID_TEMPLATE.format(finding_id=finding_id, finding=each_line)
|
|
273
|
+
)
|
|
274
|
+
all_repository_blocks.append(
|
|
275
|
+
REPOSITORY_FINDINGS_TEMPLATE.format(
|
|
276
|
+
repository=each_root.name,
|
|
277
|
+
findings=FINDING_LINE_SEPARATOR.join(all_lines_with_ids),
|
|
278
|
+
)
|
|
279
|
+
)
|
|
280
|
+
return all_repository_blocks
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _locked_stop_wake(
|
|
284
|
+
session_id: object,
|
|
285
|
+
all_repository_findings: list[tuple[Path, str, str, list[str]]],
|
|
286
|
+
all_verdicts: dict[str, JudgeVerdict],
|
|
287
|
+
) -> str | None:
|
|
288
|
+
review_state = _load_state(session_id)
|
|
289
|
+
all_wake_parts: list[str] = []
|
|
290
|
+
all_rejection_lines = _apply_verdicts(review_state, all_verdicts)
|
|
291
|
+
if all_rejection_lines:
|
|
292
|
+
all_wake_parts.append(REJECTION_LINE_SEPARATOR.join(all_rejection_lines))
|
|
293
|
+
all_repository_blocks = _record_new_findings(review_state, all_repository_findings)
|
|
294
|
+
if all_repository_blocks:
|
|
295
|
+
all_wake_parts.append(
|
|
296
|
+
BLOCK_REASON_TEMPLATE.format(
|
|
297
|
+
all_findings=REPOSITORY_FINDINGS_SEPARATOR.join(all_repository_blocks)
|
|
298
|
+
)
|
|
299
|
+
)
|
|
300
|
+
if all_wake_parts:
|
|
301
|
+
review_state.blocks_in_a_row += 1
|
|
302
|
+
review_state.last_wake_at = time.time()
|
|
303
|
+
elif review_state.blocks_in_a_row < MAXIMUM_BLOCKS_IN_A_ROW:
|
|
304
|
+
review_state.blocks_in_a_row = 0
|
|
305
|
+
_save_state(session_id, review_state)
|
|
306
|
+
return WAKE_PART_SEPARATOR.join(all_wake_parts) or None
|