claude-dev-env 8.116.5 → 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.
Files changed (117) hide show
  1. package/bin/ever-registered-hooks.mjs +2 -0
  2. package/bin/ever-registered-hooks.test.mjs +4 -0
  3. package/bin/install-constants.mjs +3 -1
  4. package/bin/install.cursor-rules.test.mjs +64 -2
  5. package/bin/install.mjs +15 -2
  6. package/bin/install.prune.test.mjs +92 -0
  7. package/bin/install.test.mjs +1 -0
  8. package/docs/model-choice/escalation.md +3 -3
  9. package/docs/model-choice/subagents-and-fan-out.md +2 -2
  10. package/docs/model-choice/which-model.md +1 -1
  11. package/docs/references/working-style.md +1 -0
  12. package/docs/rule-guides/question-presentation.md +6 -0
  13. package/docs/rule-guides/session-title.md +8 -9
  14. package/docs/rule-guides/subagent-model-effort.md +13 -13
  15. package/hooks/advisory/owned_pr_review_loop.py +2 -3
  16. package/hooks/blocking/bash_post_call_dispatcher.py +17 -9
  17. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +12 -8
  18. package/hooks/blocking/final_reply_gate.py +5 -9
  19. package/hooks/blocking/open_tasks_stop_gate.py +9 -20
  20. package/hooks/blocking/open_tasks_transcript.py +8 -3
  21. package/hooks/blocking/pii_prevention_blocker_parts/repository_exemption.py +7 -2
  22. package/hooks/blocking/question_brief_gate.py +382 -0
  23. package/hooks/blocking/step_note_gate.py +5 -2
  24. package/hooks/blocking/task_subject_length_gate.py +111 -0
  25. package/hooks/blocking/test_bash_post_call_dispatcher.py +36 -1
  26. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +57 -0
  27. package/hooks/blocking/test_final_reply_gate.py +26 -2
  28. package/hooks/blocking/test_open_tasks_stop_gate.py +21 -11
  29. package/hooks/blocking/test_question_brief_gate.py +916 -0
  30. package/hooks/blocking/test_step_note_gate.py +26 -0
  31. package/hooks/blocking/test_task_subject_length_gate.py +221 -0
  32. package/hooks/blocking/test_turn_review_child.py +166 -0
  33. package/hooks/blocking/test_turn_review_claim.py +194 -0
  34. package/hooks/blocking/test_turn_review_findings.py +124 -1
  35. package/hooks/blocking/test_turn_review_gate.py +1312 -10
  36. package/hooks/blocking/test_turn_review_repositories.py +65 -0
  37. package/hooks/blocking/test_turn_review_snapshot.py +8 -0
  38. package/hooks/blocking/test_turn_review_state.py +132 -0
  39. package/hooks/blocking/test_turn_review_transcript.py +125 -0
  40. package/hooks/blocking/tests/test_repository_exemption.py +11 -0
  41. package/hooks/blocking/turn_review_child.py +145 -0
  42. package/hooks/blocking/turn_review_claim.py +306 -0
  43. package/hooks/blocking/turn_review_findings.py +173 -13
  44. package/hooks/blocking/turn_review_gate.py +259 -262
  45. package/hooks/blocking/turn_review_repositories.py +58 -0
  46. package/hooks/blocking/turn_review_snapshot.py +5 -1
  47. package/hooks/blocking/turn_review_state.py +260 -0
  48. package/hooks/blocking/turn_review_transcript.py +158 -0
  49. package/hooks/features/bash-dispatchers.md +1 -3
  50. package/hooks/features/conduct-gates.md +19 -9
  51. package/hooks/features/lifecycle-cleanup.md +1 -1
  52. package/hooks/features/spawn-routing.md +1 -1
  53. package/hooks/hooks.json +24 -11
  54. package/hooks/hooks_constants/final_reply_gate_constants.py +0 -3
  55. package/hooks/hooks_constants/hook_time_budget.py +61 -0
  56. package/hooks/hooks_constants/hosted_hook_runner.py +51 -21
  57. package/hooks/hooks_constants/hosted_hook_runner_constants.py +3 -0
  58. package/hooks/hooks_constants/nested_project_hooks_constants.py +8 -0
  59. package/hooks/hooks_constants/once_per_session_constants.py +1 -0
  60. package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +0 -4
  61. package/hooks/hooks_constants/owned_pr_review_loop_constants.py +0 -4
  62. package/hooks/hooks_constants/pre_tool_use_context_runner.py +23 -3
  63. package/hooks/hooks_constants/pre_tool_use_context_runner_constants.py +5 -0
  64. package/hooks/hooks_constants/pre_tool_use_stdin.py +1 -1
  65. package/hooks/hooks_constants/question_brief_gate_constants.py +66 -0
  66. package/hooks/hooks_constants/session_title_constants.py +8 -29
  67. package/hooks/hooks_constants/stop_hook_hold.py +54 -0
  68. package/hooks/hooks_constants/stop_hook_hold_constants.py +9 -0
  69. package/hooks/hooks_constants/subagent_effort_levels.json +6 -0
  70. package/hooks/hooks_constants/subagent_effort_route_constants.py +20 -19
  71. package/hooks/hooks_constants/task_subject_length_gate_constants.py +43 -0
  72. package/hooks/hooks_constants/test_hook_time_budget.py +104 -0
  73. package/hooks/hooks_constants/test_hosted_hook_runner.py +12 -0
  74. package/hooks/hooks_constants/test_pre_tool_use_context_runner.py +16 -0
  75. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +6 -0
  76. package/hooks/hooks_constants/test_stop_hook_hold.py +40 -0
  77. package/hooks/hooks_constants/thread_spawn_pace_hook_constants.py +1 -2
  78. package/hooks/hooks_constants/turn_review_gate_constants.py +246 -12
  79. package/hooks/lifecycle/nested_project_hooks.py +34 -4
  80. package/hooks/lifecycle/test_nested_project_hooks.py +52 -0
  81. package/hooks/once_per_session.py +33 -4
  82. package/hooks/open_task_asks.py +1 -1
  83. package/hooks/owned_pr_session_record.py +1 -76
  84. package/hooks/routing/subagent_effort_route.py +107 -13
  85. package/hooks/routing/subagent_effort_route_hook.py +9 -8
  86. package/hooks/routing/test_subagent_effort_route.py +201 -14
  87. package/hooks/routing/test_subagent_effort_route_hook.py +56 -0
  88. package/hooks/routing/test_thread_spawn_pace_hook.py +32 -0
  89. package/hooks/routing/thread_spawn_pace_hook.py +3 -3
  90. package/hooks/session/background_silence_prompt.py +1 -1
  91. package/hooks/session/test_background_silence_prompt.py +8 -0
  92. package/hooks/session/test_tracking_nudge.py +125 -2
  93. package/hooks/session/tracking_nudge.py +28 -18
  94. package/hooks/session_title_transcript.py +216 -0
  95. package/hooks/test_once_per_session.py +10 -0
  96. package/hooks/test_owned_pr_session_record.py +0 -26
  97. package/hooks/test_session_title_transcript.py +142 -0
  98. package/hooks/validation/post_tool_use_dispatcher.py +38 -21
  99. package/hooks/validation/test_post_tool_use_dispatcher.py +43 -0
  100. package/package.json +1 -1
  101. package/rules/question-presentation.md +4 -4
  102. package/rules/subagent-model-effort.md +1 -1
  103. package/scripts/account_broker_support.py +24 -5
  104. package/scripts/claude_account_worker.py +4 -3
  105. package/scripts/dev_env_scripts_constants/account_broker_constants.py +1 -0
  106. package/scripts/policy_lint/adapter_configuration.py +12 -11
  107. package/scripts/policy_lint/config/approved_test_pairs.py +27 -0
  108. package/scripts/policy_lint/config/constants.py +2 -2
  109. package/scripts/test_account_broker_support.py +43 -1
  110. package/scripts/test_claude_account_worker.py +1 -0
  111. package/scripts/tests/test_adapter_configuration.py +10 -2
  112. package/scripts/tests/test_policy_lint_rules_pairing.py +24 -0
  113. package/hooks/blocking/owned_pr_merge_stop_gate.py +0 -185
  114. package/hooks/blocking/session_title_turn_gate.py +0 -417
  115. package/hooks/blocking/test_owned_pr_merge_stop_gate.py +0 -254
  116. package/hooks/blocking/test_session_title_turn_gate.py +0 -396
  117. package/hooks/hooks_constants/owned_pr_merge_stop_gate_constants.py +0 -40
@@ -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
@@ -1,4 +1,4 @@
1
- """Read the findings out of a turn review's text, for the turn review Stop gate.
1
+ """Read the findings, the dismissal requests and the judge's verdicts for the turn review gate.
2
2
 
3
3
  A review either prints a JSON array of findings or prose that names each
4
4
  problem by its changed path and line::
@@ -14,49 +14,92 @@ The last non-empty array of objects with a ``file`` key wins. Its entries count
14
14
  when ``file`` is a changed path and ``line`` is a number. With no such array,
15
15
  each line that opens with a changed path, a colon and a line number counts,
16
16
  after an optional list marker, backtick or emphasis mark.
17
+
18
+ The agent contests a finding with a reply line, and the judge answers in JSON::
19
+
20
+ "Dismiss TR-3: the caller retries" -> {"TR-3": "the caller retries"}
21
+ '[{"id": "TR-3", "verdict": "reject", "reason": "nothing retries"}]'
22
+ -> {"TR-3": JudgeVerdict(is_approved=False, reason="nothing retries")}
23
+
24
+ A dismiss line may open with a list marker, quote mark, backtick or emphasis
25
+ mark. The first line for an id wins, and its reason is cut to 2000 characters.
26
+ The judge's whole answer must be one JSON array, inside at most one code fence.
27
+ Each object holds exactly ``id``, ``verdict`` and ``reason`` once each, and
28
+ each id is one the judge was asked about. Any other answer gives None, such as
29
+ an object that repeats a key or a NaN or Infinity value. So an array or a key
30
+ that the agent planted in its reason and the judge echoed never counts. An entry
31
+ counts when its verdict is ``approve`` or ``reject``, its reason is not empty,
32
+ and no other entry names its id. A character in a reason that UTF-8 cannot
33
+ write, such as a lone surrogate, is kept as its escape text.
17
34
  """
18
35
 
19
36
  from __future__ import annotations
20
37
 
21
38
  import json
22
39
  import re
40
+ from collections import Counter
41
+ from dataclasses import dataclass
42
+ from typing import TypeGuard
23
43
 
24
44
  from hooks_constants.turn_review_gate_constants import (
45
+ ALL_JUDGE_ENTRY_KEYS,
46
+ DISMISS_LINE_PREFIX_PATTERN,
25
47
  FINDING_LINE_PATTERN_TEMPLATE,
26
48
  JSON_ARRAY_START,
49
+ JUDGE_ANSWER_CONSTANT_REASON,
50
+ JUDGE_ANSWER_FENCE_PATTERN,
51
+ JUDGE_ANSWER_REPEATED_KEY_REASON,
52
+ JUDGE_APPROVE_VERDICT,
53
+ JUDGE_ID_KEY,
54
+ JUDGE_REASON_KEY,
55
+ JUDGE_REASON_TEXT_ENCODING,
56
+ JUDGE_REASON_UNENCODABLE_HANDLER,
57
+ JUDGE_REJECT_VERDICT,
58
+ JUDGE_VERDICT_KEY,
27
59
  JSON_FINDING_FILE_KEY,
28
60
  JSON_FINDING_LINE_KEY,
29
61
  JSON_FINDING_LINE_TEMPLATE,
30
62
  JSON_FINDING_SUMMARY_KEY,
63
+ MAXIMUM_DISMISS_REASON_CHARACTERS,
31
64
  NO_FINDINGS_MARKER,
65
+ REASON_EDGE_CHARACTERS,
66
+ REPLY_LINE_SEPARATOR,
32
67
  )
33
68
 
34
69
 
70
+ @dataclass(frozen=True)
71
+ class JudgeVerdict:
72
+ """The judge's answer on one dismissal request."""
73
+
74
+ is_approved: bool
75
+ reason: str
76
+
77
+
35
78
  def _json_finding_lines(review_text: str, all_changed_paths: list[str]) -> list[str] | None:
36
- all_filled_arrays = _filled_finding_arrays(review_text)
79
+ all_filled_arrays = _filled_object_arrays(review_text, JSON_FINDING_FILE_KEY)
37
80
  if not all_filled_arrays:
38
81
  return None
39
82
  return _entries_on_changed_paths(all_filled_arrays[-1], all_changed_paths)
40
83
 
41
84
 
42
- def _filled_finding_arrays(review_text: str) -> list[list[dict[str, object]]]:
85
+ def _filled_object_arrays(text: str, required_key: str) -> list[list[dict[str, object]]]:
43
86
  json_decoder = json.JSONDecoder()
44
- all_finding_arrays: list[list[dict[str, object]]] = []
45
- array_start = review_text.find(JSON_ARRAY_START)
87
+ all_object_arrays: list[list[dict[str, object]]] = []
88
+ array_start = text.find(JSON_ARRAY_START)
46
89
  while array_start >= 0:
47
90
  try:
48
- decoded_array, next_search_start = json_decoder.raw_decode(review_text, array_start)
49
- except ValueError:
91
+ decoded_array, next_search_start = json_decoder.raw_decode(text, array_start)
92
+ except (ValueError, RecursionError):
50
93
  decoded_array, next_search_start = None, array_start + 1
51
- if decoded_array and _is_finding_array(decoded_array):
52
- all_finding_arrays.append(decoded_array)
53
- array_start = review_text.find(JSON_ARRAY_START, next_search_start)
54
- return all_finding_arrays
94
+ if decoded_array and _is_object_array(decoded_array, required_key):
95
+ all_object_arrays.append(decoded_array)
96
+ array_start = text.find(JSON_ARRAY_START, next_search_start)
97
+ return all_object_arrays
55
98
 
56
99
 
57
- def _is_finding_array(decoded_array: object) -> bool:
100
+ def _is_object_array(decoded_array: object, required_key: str) -> bool:
58
101
  return isinstance(decoded_array, list) and all(
59
- isinstance(each_entry, dict) and JSON_FINDING_FILE_KEY in each_entry
102
+ isinstance(each_entry, dict) and required_key in each_entry
60
103
  for each_entry in decoded_array
61
104
  )
62
105
 
@@ -100,3 +143,120 @@ def finding_lines(review_text: str, all_changed_paths: list[str]) -> list[str]:
100
143
  for each_line in review_text.splitlines()
101
144
  if any(each_pattern.search(each_line) for each_pattern in all_patterns)
102
145
  ]
146
+
147
+
148
+ def dismiss_requests(reply_text: str) -> dict[str, str]:
149
+ """Return the reason of each ``Dismiss TR-<n>: <reason>`` line in a reply, by finding id.
150
+
151
+ Args:
152
+ reply_text: The text of the agent's last reply.
153
+
154
+ Returns:
155
+ The reason for each finding id, cut to its first
156
+ ``MAXIMUM_DISMISS_REASON_CHARACTERS``; the first line for an id wins.
157
+ """
158
+ prefix_pattern = re.compile(DISMISS_LINE_PREFIX_PATTERN)
159
+ all_requests: dict[str, str] = {}
160
+ for each_line in reply_text.split(REPLY_LINE_SEPARATOR):
161
+ prefix_match = prefix_pattern.match(each_line)
162
+ if prefix_match is None:
163
+ continue
164
+ reason = each_line[prefix_match.end() :].strip(REASON_EDGE_CHARACTERS)
165
+ if reason and not reason[0].isspace():
166
+ all_requests.setdefault(
167
+ prefix_match.group(1), reason[:MAXIMUM_DISMISS_REASON_CHARACTERS]
168
+ )
169
+ return all_requests
170
+
171
+
172
+ def _object_without_repeated_keys(all_pairs: list[tuple[str, object]]) -> dict[str, object]:
173
+ all_fields = dict(all_pairs)
174
+ if len(all_fields) != len(all_pairs):
175
+ raise ValueError(JUDGE_ANSWER_REPEATED_KEY_REASON)
176
+ return all_fields
177
+
178
+
179
+ def _refused_constant(constant_name: str) -> object:
180
+ raise ValueError(JUDGE_ANSWER_CONSTANT_REASON)
181
+
182
+
183
+ def _decoded_answer(result_text: str) -> object | None:
184
+ answer_text = result_text.strip()
185
+ fence_match = re.fullmatch(JUDGE_ANSWER_FENCE_PATTERN, answer_text, re.DOTALL)
186
+ if fence_match is not None:
187
+ answer_text = fence_match.group(1)
188
+ try:
189
+ return json.loads(
190
+ answer_text,
191
+ object_pairs_hook=_object_without_repeated_keys,
192
+ parse_constant=_refused_constant,
193
+ )
194
+ except (ValueError, RecursionError):
195
+ return None
196
+
197
+
198
+ def _is_verdict_array(
199
+ decoded_answer: object, all_requested_ids: list[str]
200
+ ) -> TypeGuard[list[dict[str, object]]]:
201
+ return (
202
+ isinstance(decoded_answer, list)
203
+ and bool(decoded_answer)
204
+ and all(
205
+ isinstance(each_entry, dict)
206
+ and set(each_entry) == ALL_JUDGE_ENTRY_KEYS
207
+ and each_entry[JUDGE_ID_KEY] in all_requested_ids
208
+ for each_entry in decoded_answer
209
+ )
210
+ )
211
+
212
+
213
+ def _entry_verdict(all_entry_fields: dict[str, object]) -> JudgeVerdict | None:
214
+ verdict_word = all_entry_fields[JUDGE_VERDICT_KEY]
215
+ reason = all_entry_fields[JUDGE_REASON_KEY]
216
+ if (
217
+ verdict_word in (JUDGE_APPROVE_VERDICT, JUDGE_REJECT_VERDICT)
218
+ and isinstance(reason, str)
219
+ and reason.strip()
220
+ ):
221
+ return JudgeVerdict(
222
+ verdict_word == JUDGE_APPROVE_VERDICT, _encodable_text(reason.strip())
223
+ )
224
+ return None
225
+
226
+
227
+ def _encodable_text(reason: str) -> str:
228
+ return reason.encode(
229
+ JUDGE_REASON_TEXT_ENCODING, JUDGE_REASON_UNENCODABLE_HANDLER
230
+ ).decode(JUDGE_REASON_TEXT_ENCODING)
231
+
232
+
233
+ def judge_verdicts(
234
+ result_text: str, all_requested_ids: list[str]
235
+ ) -> dict[str, JudgeVerdict] | None:
236
+ """Return the judge's verdict for each requested finding its answer covers.
237
+
238
+ Args:
239
+ result_text: The ``result`` text of the judge run.
240
+ all_requested_ids: The finding ids the judge was asked about.
241
+
242
+ Returns:
243
+ The verdicts by finding id. An id the answer names twice gets none. None when
244
+ the whole answer, inside at most one code fence, is not one strict JSON array
245
+ of objects with exactly an id the judge was asked about, a verdict and a
246
+ reason, each named once.
247
+ """
248
+ decoded_answer = _decoded_answer(result_text)
249
+ if not _is_verdict_array(decoded_answer, all_requested_ids):
250
+ return None
251
+ all_entry_counts = Counter(each_entry[JUDGE_ID_KEY] for each_entry in decoded_answer)
252
+ all_verdicts: dict[str, JudgeVerdict] = {}
253
+ for each_entry in decoded_answer:
254
+ entry_id = each_entry[JUDGE_ID_KEY]
255
+ entry_verdict = _entry_verdict(each_entry)
256
+ if (
257
+ isinstance(entry_id, str)
258
+ and entry_verdict is not None
259
+ and all_entry_counts[entry_id] == 1
260
+ ):
261
+ all_verdicts[entry_id] = entry_verdict
262
+ return all_verdicts