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.
@@ -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))
@@ -84,7 +84,7 @@ Preconditions:
84
84
  - The ask nudge runs only from `hooks.json`, so Codex and Cursor sessions get no turn-start note. Their Stop gate still holds a work turn with no tracker call.
85
85
  - The background silence note is the last point before the reply. A Stop hook sees the reply after it is on screen, and holding the text at `MessageDisplay` would need a judgment on whether the exception applies.
86
86
  - A headless run whose input is streamed, such as `claude -p --input-format stream-json` or an SDK client, runs the Stop entry in the background, and the host kills it at teardown, so that run loses its review.
87
- - Claude Code queues a wake as a `task-notification` message. Whether that message fires `UserPromptSubmit` is not proven, so the turn review takes any prompt within 10 seconds of the last wake as the wake's own. A prompt the user sends in those 10 seconds keeps the hold count too.
87
+ - Claude Code queues a wake as a `task-notification` message. Whether that message fires `UserPromptSubmit` is not proven, so the gate takes any prompt within 10 seconds of the last wake as the wake's own. A prompt the user sends in those 10 seconds keeps the hold count too.
88
88
  - `blocking/turn_review_gate.py` starts `claude -p`, which inherits the parent session id unless the gate strips it. A review that kept `CLAUDE_CODE_SESSION_ID` would write into the parent's transcript.
89
89
  - Each review run calls a model, Sonnet at high effort. Twelve measured runs cost $0.50 in total, from $0.01 to $0.13 each. A judge run is a second model call, on Sonnet at high effort, and its cost is not measured. Leave `CLAUDE_TURN_REVIEW_REPOSITORIES` unset and the gate makes no snapshot and no call.
90
90
  - The `rewakeMessage` and `rewakeSummary` of the Stop entry in `hooks.json` speak of a review of the last reply's changes. A wake that holds only a rejected dismissal gets the same prefix.
@@ -48,8 +48,8 @@ BLOCK_NAME_KEY = "name"
48
48
  TOOL_USE_ID_KEY = "tool_use_id"
49
49
  IS_ERROR_KEY = "is_error"
50
50
  SIDECHAIN_FLAG_KEY = "isSidechain"
51
- ALL_TOOL_LISTING_NAME_KEYS = ("addedNames", "readdedNames")
52
- ALL_TOOL_LISTING_RECORD_KEYS = ("tools", "entries")
51
+ FULL_TOOL_LISTING_KEY = "tools"
52
+ ALL_DEFERRED_TOOL_KEYS = ("addedNames", "readdedNames", "entries")
53
53
  REMOVED_NAMES_KEY = "removedNames"
54
54
  TITLE_TOOL_DISPLAY_SEPARATOR = " or "
55
55
 
@@ -6,6 +6,9 @@ from pathlib import Path
6
6
 
7
7
  import pytest
8
8
 
9
+ import thread_spawn_pace_hook
10
+ from hooks_constants.usage_pace_constants import EXIT_CODE_UNDER_PACE
11
+
9
12
  HOOK_SCRIPT = Path(__file__).resolve().parent / "thread_spawn_pace_hook.py"
10
13
  SPAWN_INPUT = {
11
14
  "thread_id": "cmsg_example",
@@ -111,6 +114,35 @@ def test_should_pass_an_allowed_pair_through_when_usage_is_under_pace(
111
114
  )
112
115
 
113
116
 
117
+ def test_should_route_with_the_levels_read_from_the_file(
118
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
119
+ ) -> None:
120
+ levels_path = tmp_path / "subagent_effort_levels.json"
121
+ levels_path.write_text(
122
+ json.dumps(
123
+ {
124
+ "opus": {"allowed": ["low"], "default": "low"},
125
+ "sonnet": {"allowed": ["high"], "default": "high"},
126
+ "fable": {"allowed": ["high"], "default": "high"},
127
+ "haiku": {"allowed": ["medium"], "default": "medium"},
128
+ }
129
+ ),
130
+ encoding="utf-8",
131
+ )
132
+ monkeypatch.setattr(thread_spawn_pace_hook, "EFFORT_LEVELS_PATH", levels_path)
133
+ spawn_input = {**SPAWN_INPUT, "model": "claude-opus-5-5", "effort": "medium"}
134
+
135
+ decision = thread_spawn_pace_hook.decide_hook_output(
136
+ spawn_input, EXIT_CODE_UNDER_PACE, ""
137
+ )
138
+
139
+ assert decision is not None
140
+ assert _hook_specific_output(json.dumps(decision))["updatedInput"] == {
141
+ **spawn_input,
142
+ "effort": "low",
143
+ }
144
+
145
+
114
146
  def test_should_reshape_the_spawn_when_usage_is_unreadable(tmp_path: Path) -> None:
115
147
  _assert_reshaped(
116
148
  _run_hook(
@@ -265,6 +265,14 @@ def test_should_build_the_note_only_under_silence() -> None:
265
265
  )
266
266
 
267
267
 
268
+ def test_should_read_the_transcript_with_the_session_title_reader() -> None:
269
+ session_title_transcript = importlib.import_module("session_title_transcript")
270
+ assert (
271
+ background_silence_prompt.read_transcript_entries
272
+ is session_title_transcript.read_transcript_entries
273
+ )
274
+
275
+
268
276
  def test_should_print_the_documented_output_through_a_subprocess(tmp_path: Path) -> None:
269
277
  transcript_path = tmp_path / "session.jsonl"
270
278
  transcript_path.write_text(json.dumps(typed_entry(SILENCE_REQUEST)), encoding="utf-8")
@@ -232,6 +232,17 @@ def test_should_add_nothing_about_titles_in_a_session_without_a_title_tool(tmp_p
232
232
  assert "title" not in context_text
233
233
 
234
234
 
235
+ def test_should_add_nothing_about_titles_once_a_later_tool_listing_leaves_out_the_title_tool(
236
+ tmp_path: Path,
237
+ ) -> None:
238
+ later_listing = {"type": "attachment", "attachment": {"tools": [{"name": "Bash"}]}}
239
+ context_text = script_context(
240
+ run_nudge_script(tmp_path, "Export the APKs", [TOOL_LISTING, later_listing])
241
+ )
242
+ assert TASK_LIST_NUDGE in context_text
243
+ assert "title" not in context_text
244
+
245
+
235
246
  @pytest.mark.parametrize("earlier_title", ["\U0001f6a9 Merge approval", "✅ Gate"])
236
247
  def test_should_keep_the_stale_title_note_through_the_script(
237
248
  tmp_path: Path, earlier_title: str