claude-dev-env 8.117.0 → 8.117.2
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/docs/references/working-style.md +3 -0
- package/docs/rule-guides/question-presentation.md +8 -0
- package/hooks/blocking/final_reply_gate.py +20 -7
- package/hooks/blocking/open_tasks_stop_gate.py +133 -7
- package/hooks/blocking/open_tasks_transcript.py +77 -9
- package/hooks/blocking/test_final_reply_gate.py +14 -0
- package/hooks/blocking/test_open_tasks_stop_gate.py +204 -1
- package/hooks/blocking/test_open_tasks_transcript.py +123 -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/test_user_step_wording.py +481 -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/blocking/user_step_wording.py +384 -0
- package/hooks/features/conduct-gates.md +2 -2
- package/hooks/hooks_constants/final_reply_gate_constants.py +1 -1
- package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +58 -5
- package/hooks/hooks_constants/session_title_constants.py +2 -2
- package/hooks/hooks_constants/user_step_wording_constants.py +79 -0
- 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/rules/question-presentation.md +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,58 @@
|
|
|
1
|
+
"""Find the checkouts that take part in the turn review gate.
|
|
2
|
+
|
|
3
|
+
A repository takes part when it is the session's project folder or a folder
|
|
4
|
+
directly inside it, and its GitHub origin ``owner/repo`` slug is listed in
|
|
5
|
+
``CLAUDE_TURN_REVIEW_REPOSITORIES``::
|
|
6
|
+
|
|
7
|
+
project folder /work, listed slugs "Owner/App, owner/lib"
|
|
8
|
+
/work/app origin owner/app -> takes part
|
|
9
|
+
/work/docs origin owner/docs -> left out
|
|
10
|
+
/work/notes no .git entry -> left out
|
|
11
|
+
|
|
12
|
+
The listed slugs are compared without regard to letter case. A folder that
|
|
13
|
+
cannot be read is left out.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import os
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
from hooks_constants.turn_review_gate_constants import (
|
|
22
|
+
GIT_DIRECTORY_NAME,
|
|
23
|
+
PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE,
|
|
24
|
+
REPOSITORIES_ENVIRONMENT_VARIABLE,
|
|
25
|
+
REPOSITORY_SEPARATOR,
|
|
26
|
+
)
|
|
27
|
+
from pii_prevention_blocker_parts.repository_exemption import repository_origin_slug
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _opted_in_repository_slugs() -> frozenset[str]:
|
|
31
|
+
listed = os.environ.get(REPOSITORIES_ENVIRONMENT_VARIABLE, "")
|
|
32
|
+
return frozenset(
|
|
33
|
+
each.strip().casefold() for each in listed.split(REPOSITORY_SEPARATOR) if each.strip()
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _opted_in_repository_roots(all_slugs: frozenset[str]) -> list[Path]:
|
|
38
|
+
project_directory = os.environ.get(PROJECT_DIRECTORY_ENVIRONMENT_VARIABLE, "")
|
|
39
|
+
if not project_directory or not Path(project_directory).is_dir():
|
|
40
|
+
return []
|
|
41
|
+
project_path = Path(project_directory).resolve()
|
|
42
|
+
all_candidates = [project_path, *sorted(_child_entries(project_path))]
|
|
43
|
+
return [each for each in all_candidates if _is_opted_in_checkout(each, all_slugs)]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _child_entries(project_path: Path) -> list[Path]:
|
|
47
|
+
try:
|
|
48
|
+
return list(project_path.iterdir())
|
|
49
|
+
except OSError:
|
|
50
|
+
return []
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _is_opted_in_checkout(folder: Path, all_slugs: frozenset[str]) -> bool:
|
|
54
|
+
try:
|
|
55
|
+
is_checkout = folder.is_dir() and (folder / GIT_DIRECTORY_NAME).exists()
|
|
56
|
+
except OSError:
|
|
57
|
+
return False
|
|
58
|
+
return is_checkout and repository_origin_slug(folder) in all_slugs
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""Keep the session's review state, its file lock and the log for the turn review gate.
|
|
2
|
+
|
|
3
|
+
Each session has one state file in the temp folder. It holds the snapshot each
|
|
4
|
+
checkout was last reviewed up to, the hold count, and every finding by its id::
|
|
5
|
+
|
|
6
|
+
turn-review-<session>.json
|
|
7
|
+
{"baselines": {"/work/app": "<snapshot B>"}, "blocks_in_a_row": 1,
|
|
8
|
+
"findings": {"TR-1": {"text": "src/service.py:3 - ...", "status": "open"}}}
|
|
9
|
+
|
|
10
|
+
A saved entry in another form is left out on load, such as a baseline that is
|
|
11
|
+
not a commit name or a finding with an unknown status. A state file that
|
|
12
|
+
cannot be read gives an empty state. A save that fails writes one log line.
|
|
13
|
+
|
|
14
|
+
The lock functions take and release a lock on an open file, with ``msvcrt`` on
|
|
15
|
+
Windows and ``fcntl`` on other systems. ``_log`` adds one dated line to
|
|
16
|
+
``~/.claude/logs/turn-review.log``. A line that cannot be written is dropped.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import datetime
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import re
|
|
25
|
+
import sys
|
|
26
|
+
import tempfile
|
|
27
|
+
from dataclasses import dataclass, field
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
|
|
30
|
+
if sys.platform == "win32":
|
|
31
|
+
import msvcrt
|
|
32
|
+
else:
|
|
33
|
+
import fcntl
|
|
34
|
+
|
|
35
|
+
from atomic_file_writer import write_text_atomically
|
|
36
|
+
from hooks_constants.atomic_file_writer_constants import ATOMIC_WRITE_TEMPORARY_SUFFIX
|
|
37
|
+
from hooks_constants.session_file_id import session_file_id
|
|
38
|
+
from hooks_constants.turn_review_gate_constants import (
|
|
39
|
+
ALL_FINDING_STATUSES,
|
|
40
|
+
ALL_LOG_PATH_PARTS,
|
|
41
|
+
BASELINE_COMMIT_PATTERN,
|
|
42
|
+
FINDING_BASE_KEY,
|
|
43
|
+
FINDING_ID_PATTERN,
|
|
44
|
+
FINDING_ID_TEMPLATE,
|
|
45
|
+
FINDING_JUDGE_REASON_KEY,
|
|
46
|
+
FINDING_REPOSITORY_KEY,
|
|
47
|
+
FINDING_STATUS_KEY,
|
|
48
|
+
FINDING_STATUS_OPEN,
|
|
49
|
+
FINDING_TEXT_KEY,
|
|
50
|
+
FINDING_TURN_KEY,
|
|
51
|
+
LOG_LINE_TEMPLATE,
|
|
52
|
+
LOG_TEXT_ENCODING,
|
|
53
|
+
LOG_UNENCODABLE_HANDLER,
|
|
54
|
+
STATE_BASELINES_KEY,
|
|
55
|
+
STATE_BLOCKS_KEY,
|
|
56
|
+
STATE_FILE_TEMPLATE,
|
|
57
|
+
STATE_FINDINGS_KEY,
|
|
58
|
+
STATE_LAST_WAKE_KEY,
|
|
59
|
+
STATE_LOCK_BYTE_COUNT,
|
|
60
|
+
STATE_OPEN_TURN_PROMPT_KEY,
|
|
61
|
+
STATE_SAVE_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
62
|
+
STATE_TEMPORARY_PREFIX,
|
|
63
|
+
STATE_TEXT_ENCODING,
|
|
64
|
+
STATE_TURN_OPEN_KEY,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if sys.platform == "win32":
|
|
69
|
+
|
|
70
|
+
def _try_file_lock(file_descriptor: int) -> None:
|
|
71
|
+
os.lseek(file_descriptor, 0, os.SEEK_SET)
|
|
72
|
+
msvcrt.locking(file_descriptor, msvcrt.LK_NBLCK, STATE_LOCK_BYTE_COUNT)
|
|
73
|
+
|
|
74
|
+
def _release_file_lock(file_descriptor: int) -> None:
|
|
75
|
+
os.lseek(file_descriptor, 0, os.SEEK_SET)
|
|
76
|
+
msvcrt.locking(file_descriptor, msvcrt.LK_UNLCK, STATE_LOCK_BYTE_COUNT)
|
|
77
|
+
|
|
78
|
+
else:
|
|
79
|
+
|
|
80
|
+
def _try_file_lock(file_descriptor: int) -> None:
|
|
81
|
+
fcntl.flock(file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
82
|
+
|
|
83
|
+
def _release_file_lock(file_descriptor: int) -> None:
|
|
84
|
+
fcntl.flock(file_descriptor, fcntl.LOCK_UN)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _log(diagnostic_text: str) -> None:
|
|
88
|
+
timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
|
89
|
+
try:
|
|
90
|
+
log_path = Path.home().joinpath(*ALL_LOG_PATH_PARTS)
|
|
91
|
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
92
|
+
with log_path.open(
|
|
93
|
+
"a", encoding=LOG_TEXT_ENCODING, errors=LOG_UNENCODABLE_HANDLER
|
|
94
|
+
) as log_file:
|
|
95
|
+
log_file.write(LOG_LINE_TEMPLATE.format(timestamp=timestamp, text=diagnostic_text))
|
|
96
|
+
except (OSError, RuntimeError):
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _is_lock_taken(file_descriptor: int) -> bool:
|
|
101
|
+
try:
|
|
102
|
+
_try_file_lock(file_descriptor)
|
|
103
|
+
except OSError:
|
|
104
|
+
return False
|
|
105
|
+
return True
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _release_and_close(file_descriptor: int, is_locked: bool) -> None:
|
|
109
|
+
try:
|
|
110
|
+
if is_locked:
|
|
111
|
+
_release_file_lock(file_descriptor)
|
|
112
|
+
finally:
|
|
113
|
+
os.close(file_descriptor)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _state_path(session_id: object) -> Path:
|
|
117
|
+
return Path(tempfile.gettempdir()) / STATE_FILE_TEMPLATE.format(
|
|
118
|
+
session_id=session_file_id(session_id)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@dataclass
|
|
123
|
+
class _Finding:
|
|
124
|
+
text: str
|
|
125
|
+
repository: str
|
|
126
|
+
base_commit: str
|
|
127
|
+
turn_commit: str
|
|
128
|
+
status: str = FINDING_STATUS_OPEN
|
|
129
|
+
judge_reason: str | None = None
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@dataclass
|
|
133
|
+
class _ReviewState:
|
|
134
|
+
all_baselines: dict[str, str] = field(default_factory=dict)
|
|
135
|
+
blocks_in_a_row: int = 0
|
|
136
|
+
is_turn_open: bool = False
|
|
137
|
+
last_wake_at: float | None = None
|
|
138
|
+
open_turn_prompt_at: float | None = None
|
|
139
|
+
all_findings: dict[str, _Finding] = field(default_factory=dict)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _saved_time(raw_value: object) -> float | None:
|
|
143
|
+
if isinstance(raw_value, bool) or not isinstance(raw_value, (int, float)):
|
|
144
|
+
return None
|
|
145
|
+
return float(raw_value)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _is_commit_name(raw_value: object) -> bool:
|
|
149
|
+
return isinstance(raw_value, str) and re.fullmatch(BASELINE_COMMIT_PATTERN, raw_value) is not None
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _saved_finding(raw_entry: object) -> _Finding | None:
|
|
153
|
+
if not isinstance(raw_entry, dict):
|
|
154
|
+
return None
|
|
155
|
+
text = raw_entry.get(FINDING_TEXT_KEY)
|
|
156
|
+
repository = raw_entry.get(FINDING_REPOSITORY_KEY)
|
|
157
|
+
base_commit = raw_entry.get(FINDING_BASE_KEY)
|
|
158
|
+
turn_commit = raw_entry.get(FINDING_TURN_KEY)
|
|
159
|
+
status = raw_entry.get(FINDING_STATUS_KEY)
|
|
160
|
+
judge_reason = raw_entry.get(FINDING_JUDGE_REASON_KEY)
|
|
161
|
+
if not (
|
|
162
|
+
isinstance(text, str)
|
|
163
|
+
and isinstance(repository, str)
|
|
164
|
+
and _is_commit_name(base_commit)
|
|
165
|
+
and _is_commit_name(turn_commit)
|
|
166
|
+
and status in ALL_FINDING_STATUSES
|
|
167
|
+
):
|
|
168
|
+
return None
|
|
169
|
+
return _Finding(
|
|
170
|
+
text,
|
|
171
|
+
repository,
|
|
172
|
+
str(base_commit),
|
|
173
|
+
str(turn_commit),
|
|
174
|
+
str(status),
|
|
175
|
+
judge_reason if isinstance(judge_reason, str) else None,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _saved_findings(raw_findings: object) -> dict[str, _Finding]:
|
|
180
|
+
if not isinstance(raw_findings, dict):
|
|
181
|
+
return {}
|
|
182
|
+
all_findings: dict[str, _Finding] = {}
|
|
183
|
+
for each_id, each_entry in raw_findings.items():
|
|
184
|
+
saved_finding = _saved_finding(each_entry)
|
|
185
|
+
if isinstance(each_id, str) and re.fullmatch(FINDING_ID_PATTERN, each_id) and saved_finding:
|
|
186
|
+
all_findings[each_id] = saved_finding
|
|
187
|
+
return all_findings
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _finding_number(finding_id: str) -> int:
|
|
191
|
+
id_match = re.fullmatch(FINDING_ID_PATTERN, finding_id)
|
|
192
|
+
return int(id_match.group(1)) if id_match else 0
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _next_finding_id(review_state: _ReviewState) -> str:
|
|
196
|
+
highest_number = max(map(_finding_number, review_state.all_findings), default=0)
|
|
197
|
+
return FINDING_ID_TEMPLATE.format(number=highest_number + 1)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _finding_record(finding: _Finding) -> dict[str, str | None]:
|
|
201
|
+
return {
|
|
202
|
+
FINDING_TEXT_KEY: finding.text,
|
|
203
|
+
FINDING_REPOSITORY_KEY: finding.repository,
|
|
204
|
+
FINDING_BASE_KEY: finding.base_commit,
|
|
205
|
+
FINDING_TURN_KEY: finding.turn_commit,
|
|
206
|
+
FINDING_STATUS_KEY: finding.status,
|
|
207
|
+
FINDING_JUDGE_REASON_KEY: finding.judge_reason,
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _load_state(session_id: object) -> _ReviewState:
|
|
212
|
+
try:
|
|
213
|
+
saved_state = json.loads(_state_path(session_id).read_text(encoding=STATE_TEXT_ENCODING))
|
|
214
|
+
except (OSError, ValueError):
|
|
215
|
+
return _ReviewState()
|
|
216
|
+
if not isinstance(saved_state, dict):
|
|
217
|
+
return _ReviewState()
|
|
218
|
+
raw_baselines = saved_state.get(STATE_BASELINES_KEY)
|
|
219
|
+
all_baselines = {
|
|
220
|
+
each_root: each_commit
|
|
221
|
+
for each_root, each_commit in (
|
|
222
|
+
raw_baselines.items() if isinstance(raw_baselines, dict) else []
|
|
223
|
+
)
|
|
224
|
+
if isinstance(each_root, str) and _is_commit_name(each_commit)
|
|
225
|
+
}
|
|
226
|
+
raw_blocks = saved_state.get(STATE_BLOCKS_KEY)
|
|
227
|
+
return _ReviewState(
|
|
228
|
+
all_baselines,
|
|
229
|
+
raw_blocks if isinstance(raw_blocks, int) else 0,
|
|
230
|
+
saved_state.get(STATE_TURN_OPEN_KEY) is True,
|
|
231
|
+
_saved_time(saved_state.get(STATE_LAST_WAKE_KEY)),
|
|
232
|
+
_saved_time(saved_state.get(STATE_OPEN_TURN_PROMPT_KEY)),
|
|
233
|
+
_saved_findings(saved_state.get(STATE_FINDINGS_KEY)),
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _save_state(session_id: object, review_state: _ReviewState) -> None:
|
|
238
|
+
try:
|
|
239
|
+
write_text_atomically(
|
|
240
|
+
_state_path(session_id),
|
|
241
|
+
json.dumps(
|
|
242
|
+
{
|
|
243
|
+
STATE_BASELINES_KEY: review_state.all_baselines,
|
|
244
|
+
STATE_BLOCKS_KEY: review_state.blocks_in_a_row,
|
|
245
|
+
STATE_TURN_OPEN_KEY: review_state.is_turn_open,
|
|
246
|
+
STATE_LAST_WAKE_KEY: review_state.last_wake_at,
|
|
247
|
+
STATE_OPEN_TURN_PROMPT_KEY: review_state.open_turn_prompt_at,
|
|
248
|
+
STATE_FINDINGS_KEY: {
|
|
249
|
+
each_id: _finding_record(each_finding)
|
|
250
|
+
for each_id, each_finding in review_state.all_findings.items()
|
|
251
|
+
},
|
|
252
|
+
}
|
|
253
|
+
),
|
|
254
|
+
STATE_TEXT_ENCODING,
|
|
255
|
+
STATE_TEMPORARY_PREFIX,
|
|
256
|
+
ATOMIC_WRITE_TEMPORARY_SUFFIX,
|
|
257
|
+
False,
|
|
258
|
+
)
|
|
259
|
+
except (OSError, UnicodeError) as save_error:
|
|
260
|
+
_log(STATE_SAVE_FAILURE_DIAGNOSTIC_TEMPLATE.format(detail=save_error))
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""Read the reply and the user's messages from a Stop input, for the turn review gate.
|
|
2
|
+
|
|
3
|
+
The reply comes from ``last_assistant_message`` in the Stop input, or from
|
|
4
|
+
the text of the last assistant message in ``transcript_path`` when that field
|
|
5
|
+
is missing::
|
|
6
|
+
|
|
7
|
+
{"last_assistant_message": "Dismiss TR-1: the caller retries"} -> that text
|
|
8
|
+
{"transcript_path": "session.jsonl"} -> the text of the last assistant message
|
|
9
|
+
|
|
10
|
+
The user's messages come from ``transcript_path``. Only entries with
|
|
11
|
+
``origin.kind`` ``human`` count, and text that starts with ``<`` is left out.
|
|
12
|
+
The last five are kept, each cut to 2000 characters. The transcript is read
|
|
13
|
+
from its newest line back, and each line is decoded once per stop.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
from collections.abc import Iterator
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
from hooks_constants.turn_review_gate_constants import (
|
|
23
|
+
ALL_TRANSCRIPT_NOT_TYPED_FLAGS,
|
|
24
|
+
MAXIMUM_USER_MESSAGE_CHARACTERS,
|
|
25
|
+
NESTED_TOO_DEEP_REASON,
|
|
26
|
+
REPLY_TEXT_KEY,
|
|
27
|
+
TRANSCRIPT_ASSISTANT_TYPE,
|
|
28
|
+
TRANSCRIPT_BLOCK_TYPE_KEY,
|
|
29
|
+
TRANSCRIPT_CONTENT_KEY,
|
|
30
|
+
TRANSCRIPT_ENTRY_TYPE_KEY,
|
|
31
|
+
TRANSCRIPT_HUMAN_ORIGIN_KIND,
|
|
32
|
+
TRANSCRIPT_MARKUP_START,
|
|
33
|
+
TRANSCRIPT_MESSAGE_ID_KEY,
|
|
34
|
+
TRANSCRIPT_MESSAGE_KEY,
|
|
35
|
+
TRANSCRIPT_ORIGIN_KEY,
|
|
36
|
+
TRANSCRIPT_ORIGIN_KIND_KEY,
|
|
37
|
+
TRANSCRIPT_PATH_KEY,
|
|
38
|
+
TRANSCRIPT_TEXT_BLOCK_TYPE,
|
|
39
|
+
TRANSCRIPT_TEXT_ENCODING,
|
|
40
|
+
TRANSCRIPT_TEXT_KEY,
|
|
41
|
+
TRANSCRIPT_TEXT_SEPARATOR,
|
|
42
|
+
TRANSCRIPT_UNREADABLE_DIAGNOSTIC_TEMPLATE,
|
|
43
|
+
TRANSCRIPT_USER_TYPE,
|
|
44
|
+
USER_MESSAGE_COUNT,
|
|
45
|
+
)
|
|
46
|
+
from turn_review_state import _log
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _reversed_transcript_entries(hook_input: dict) -> Iterator[dict]:
|
|
50
|
+
transcript_path = hook_input.get(TRANSCRIPT_PATH_KEY)
|
|
51
|
+
if not isinstance(transcript_path, str) or not transcript_path:
|
|
52
|
+
return
|
|
53
|
+
try:
|
|
54
|
+
all_transcript_lines = Path(transcript_path).read_text(
|
|
55
|
+
encoding=TRANSCRIPT_TEXT_ENCODING, errors="replace"
|
|
56
|
+
).splitlines()
|
|
57
|
+
except OSError:
|
|
58
|
+
return
|
|
59
|
+
for each_line in reversed(all_transcript_lines):
|
|
60
|
+
try:
|
|
61
|
+
transcript_entry = json.loads(each_line)
|
|
62
|
+
except ValueError:
|
|
63
|
+
continue
|
|
64
|
+
except RecursionError:
|
|
65
|
+
_log(TRANSCRIPT_UNREADABLE_DIAGNOSTIC_TEMPLATE.format(reason=NESTED_TOO_DEEP_REASON))
|
|
66
|
+
continue
|
|
67
|
+
if isinstance(transcript_entry, dict):
|
|
68
|
+
yield transcript_entry
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class _ReversedTranscript:
|
|
72
|
+
"""The transcript's entries, newest first, with each line decoded once per stop."""
|
|
73
|
+
|
|
74
|
+
def __init__(self, hook_input: dict) -> None:
|
|
75
|
+
self._unread_entries = _reversed_transcript_entries(hook_input)
|
|
76
|
+
self._all_read_entries: list[dict] = []
|
|
77
|
+
|
|
78
|
+
def __iter__(self) -> Iterator[dict]:
|
|
79
|
+
yield from self._all_read_entries
|
|
80
|
+
for each_entry in self._unread_entries:
|
|
81
|
+
self._all_read_entries.append(each_entry)
|
|
82
|
+
yield each_entry
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _entry_message(transcript_entry: dict) -> dict:
|
|
86
|
+
message = transcript_entry.get(TRANSCRIPT_MESSAGE_KEY)
|
|
87
|
+
return message if isinstance(message, dict) else {}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _entry_text(transcript_entry: dict) -> str:
|
|
91
|
+
content = _entry_message(transcript_entry).get(TRANSCRIPT_CONTENT_KEY)
|
|
92
|
+
if isinstance(content, str):
|
|
93
|
+
return content.strip()
|
|
94
|
+
if not isinstance(content, list):
|
|
95
|
+
return ""
|
|
96
|
+
return TRANSCRIPT_TEXT_SEPARATOR.join(
|
|
97
|
+
each_block[TRANSCRIPT_TEXT_KEY]
|
|
98
|
+
for each_block in content
|
|
99
|
+
if isinstance(each_block, dict)
|
|
100
|
+
and each_block.get(TRANSCRIPT_BLOCK_TYPE_KEY) == TRANSCRIPT_TEXT_BLOCK_TYPE
|
|
101
|
+
and isinstance(each_block.get(TRANSCRIPT_TEXT_KEY), str)
|
|
102
|
+
).strip()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _is_assistant_entry(transcript_entry: dict) -> bool:
|
|
106
|
+
return transcript_entry.get(TRANSCRIPT_ENTRY_TYPE_KEY) == TRANSCRIPT_ASSISTANT_TYPE
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _last_assistant_message_text(transcript: _ReversedTranscript) -> str:
|
|
110
|
+
all_text_parts: list[str] = []
|
|
111
|
+
last_message_id: object = None
|
|
112
|
+
for each_entry in transcript:
|
|
113
|
+
is_first_assistant_entry = not all_text_parts and last_message_id is None
|
|
114
|
+
if is_first_assistant_entry and not _is_assistant_entry(each_entry):
|
|
115
|
+
continue
|
|
116
|
+
message_id = _entry_message(each_entry).get(TRANSCRIPT_MESSAGE_ID_KEY)
|
|
117
|
+
if not is_first_assistant_entry and (
|
|
118
|
+
not _is_assistant_entry(each_entry) or message_id != last_message_id
|
|
119
|
+
):
|
|
120
|
+
break
|
|
121
|
+
all_text_parts.append(_entry_text(each_entry))
|
|
122
|
+
if message_id is None:
|
|
123
|
+
break
|
|
124
|
+
last_message_id = message_id
|
|
125
|
+
return TRANSCRIPT_TEXT_SEPARATOR.join(reversed(all_text_parts)).strip()
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _reply_text(hook_input: dict, transcript: _ReversedTranscript) -> str:
|
|
129
|
+
reply_text = hook_input.get(REPLY_TEXT_KEY)
|
|
130
|
+
if isinstance(reply_text, str):
|
|
131
|
+
return reply_text
|
|
132
|
+
return _last_assistant_message_text(transcript)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _typed_user_text(transcript_entry: dict) -> str:
|
|
136
|
+
if transcript_entry.get(TRANSCRIPT_ENTRY_TYPE_KEY) != TRANSCRIPT_USER_TYPE:
|
|
137
|
+
return ""
|
|
138
|
+
if any(transcript_entry.get(each_flag) for each_flag in ALL_TRANSCRIPT_NOT_TYPED_FLAGS):
|
|
139
|
+
return ""
|
|
140
|
+
origin = transcript_entry.get(TRANSCRIPT_ORIGIN_KEY)
|
|
141
|
+
if (
|
|
142
|
+
not isinstance(origin, dict)
|
|
143
|
+
or origin.get(TRANSCRIPT_ORIGIN_KIND_KEY) != TRANSCRIPT_HUMAN_ORIGIN_KIND
|
|
144
|
+
):
|
|
145
|
+
return ""
|
|
146
|
+
typed_text = _entry_text(transcript_entry)
|
|
147
|
+
return "" if typed_text.startswith(TRANSCRIPT_MARKUP_START) else typed_text
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _typed_user_messages(transcript: _ReversedTranscript) -> list[str]:
|
|
151
|
+
all_messages: list[str] = []
|
|
152
|
+
for each_entry in transcript:
|
|
153
|
+
typed_text = _typed_user_text(each_entry)
|
|
154
|
+
if typed_text:
|
|
155
|
+
all_messages.append(typed_text[:MAXIMUM_USER_MESSAGE_CHARACTERS])
|
|
156
|
+
if len(all_messages) == USER_MESSAGE_COUNT:
|
|
157
|
+
break
|
|
158
|
+
return list(reversed(all_messages))
|