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.
Files changed (39) hide show
  1. package/bin/ever-registered-hooks.test.mjs +4 -0
  2. package/docs/references/working-style.md +3 -0
  3. package/docs/rule-guides/question-presentation.md +8 -0
  4. package/hooks/blocking/final_reply_gate.py +20 -7
  5. package/hooks/blocking/open_tasks_stop_gate.py +133 -7
  6. package/hooks/blocking/open_tasks_transcript.py +77 -9
  7. package/hooks/blocking/test_final_reply_gate.py +14 -0
  8. package/hooks/blocking/test_open_tasks_stop_gate.py +204 -1
  9. package/hooks/blocking/test_open_tasks_transcript.py +123 -0
  10. package/hooks/blocking/test_turn_review_child.py +166 -0
  11. package/hooks/blocking/test_turn_review_claim.py +194 -0
  12. package/hooks/blocking/test_turn_review_repositories.py +65 -0
  13. package/hooks/blocking/test_turn_review_state.py +132 -0
  14. package/hooks/blocking/test_turn_review_transcript.py +125 -0
  15. package/hooks/blocking/test_user_step_wording.py +481 -0
  16. package/hooks/blocking/turn_review_child.py +145 -0
  17. package/hooks/blocking/turn_review_claim.py +306 -0
  18. package/hooks/blocking/turn_review_gate.py +32 -808
  19. package/hooks/blocking/turn_review_repositories.py +58 -0
  20. package/hooks/blocking/turn_review_state.py +260 -0
  21. package/hooks/blocking/turn_review_transcript.py +158 -0
  22. package/hooks/blocking/user_step_wording.py +384 -0
  23. package/hooks/features/conduct-gates.md +2 -2
  24. package/hooks/hooks_constants/final_reply_gate_constants.py +1 -1
  25. package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +58 -5
  26. package/hooks/hooks_constants/session_title_constants.py +2 -2
  27. package/hooks/hooks_constants/user_step_wording_constants.py +79 -0
  28. package/hooks/routing/test_thread_spawn_pace_hook.py +32 -0
  29. package/hooks/session/test_background_silence_prompt.py +8 -0
  30. package/hooks/session/test_tracking_nudge.py +11 -0
  31. package/hooks/session_title_transcript.py +38 -17
  32. package/hooks/test_session_title_transcript.py +31 -0
  33. package/package.json +1 -1
  34. package/rules/question-presentation.md +1 -1
  35. package/scripts/account_broker_support.py +24 -5
  36. package/scripts/dev_env_scripts_constants/account_broker_constants.py +1 -0
  37. package/scripts/policy_lint/config/approved_test_pairs.py +27 -0
  38. package/scripts/test_account_broker_support.py +43 -1
  39. package/scripts/tests/test_policy_lint_rules_pairing.py +24 -0
@@ -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