claude-dev-env 8.122.0 → 8.123.0
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/hooks/blocking/closing_question_round_gate.py +387 -0
- package/hooks/blocking/closing_question_round_lines.py +109 -0
- package/hooks/blocking/closing_question_round_open_items.py +348 -0
- package/hooks/blocking/closing_question_round_transcript.py +344 -0
- package/hooks/blocking/test_closing_question_round_gate.py +983 -0
- package/hooks/blocking/test_closing_question_round_gate_declaration.py +307 -0
- package/hooks/blocking/test_closing_question_round_gate_work.py +349 -0
- package/hooks/blocking/test_closing_question_round_lines.py +70 -0
- package/hooks/blocking/test_closing_question_round_open_items.py +238 -0
- package/hooks/blocking/test_closing_question_round_transcript.py +236 -0
- package/hooks/hooks_constants/closing_question_round_gate_constants.py +167 -0
- package/hooks/hooks_constants/test_closing_question_round_gate_constants.py +92 -0
- package/package.json +1 -1
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Stop hook that ends a session a person started with a question card round or a declaration.
|
|
3
|
+
|
|
4
|
+
The gate holds the stop for two shapes, and only in a session it covers::
|
|
5
|
+
|
|
6
|
+
hold: "I deferred the backoff setting." no question card after it
|
|
7
|
+
hold: "Nothing is deferred." a marker line; negation does not cancel it
|
|
8
|
+
hold: an Edit call, then "Done." work with no card and no declaration
|
|
9
|
+
allow: an Edit call, then "Open items: none." the declaration ends the reply
|
|
10
|
+
allow: "Both tests pass." no work and no marker line
|
|
11
|
+
allow: a marker line, then an answered AskUserQuestion call
|
|
12
|
+
|
|
13
|
+
The stretch is every top-level record after the last question call that
|
|
14
|
+
reached the user, then the payload's closing reply. A marker line is a prose
|
|
15
|
+
line ``closing_question_round_open_items`` finds: a marker word or a line that
|
|
16
|
+
ends with a question mark. Work is any tool call that is no question call and
|
|
17
|
+
that sits in the stretch after the last record whose final text ends with the
|
|
18
|
+
declaration, in a record with no work call of its own. The declaration is the
|
|
19
|
+
closing reply's last prose line, read after masking as ``Open items: none.``,
|
|
20
|
+
with or without its period, alone or wrapped in one bold or italic mark. The closing reply is the newest
|
|
21
|
+
text after the last work call.
|
|
22
|
+
|
|
23
|
+
- A marker line holds the stop, and a declaration does not excuse it. The
|
|
24
|
+
reason quotes the last 8 marker lines at most, each cut to 160 characters,
|
|
25
|
+
and counts the earlier ones. Three dots in backticks stand where a code span
|
|
26
|
+
sat.
|
|
27
|
+
- Work with no marker line and no declaration holds the stop with the work
|
|
28
|
+
reason.
|
|
29
|
+
|
|
30
|
+
The gate leaves a stop alone, with exit code 0 and no output, when:
|
|
31
|
+
|
|
32
|
+
- the payload cannot be read, or it carries a non-empty ``agent_id`` (a
|
|
33
|
+
subagent) or a ``conversation_id`` (Cursor);
|
|
34
|
+
- ``CLAUDE_CODE_SESSION_ATTENDED`` is ``0`` (a headless run);
|
|
35
|
+
- the payload's ``background_tasks`` is a non-empty list, object, or string;
|
|
36
|
+
- the transcript is missing or cannot be read;
|
|
37
|
+
- no person started the session. A record that carries ``turnOrigin`` equal to
|
|
38
|
+
``human`` and starts a turn, as ``claude_prompt_text`` reads it, is a prompt
|
|
39
|
+
a person typed. When no record carries a ``turnOrigin`` key, any record that
|
|
40
|
+
starts a turn counts when ``CLAUDE_CODE_SESSION_ATTENDED`` is ``1``;
|
|
41
|
+
- this gate already held twice since the later of the last typed prompt and
|
|
42
|
+
the last question call that reached the user. A hold is a ``user`` record
|
|
43
|
+
whose text starts with "Stop hook feedback" and carries the first sentence
|
|
44
|
+
of either reason within its first 200 characters;
|
|
45
|
+
- the payload is over 16 MiB, which the gate does not parse;
|
|
46
|
+
- 7 seconds pass on a monotonic clock, checked before each transcript line
|
|
47
|
+
read, and before each text, paragraph, line, and marker scan window;
|
|
48
|
+
- 8 seconds pass, counted from the start of the stdin read. The check runs in
|
|
49
|
+
a child process, which is killed then, and a watchdog thread ends a stdin
|
|
50
|
+
read that never finishes at 8.5 seconds;
|
|
51
|
+
- the child exits with a code other than 0 or 2;
|
|
52
|
+
- stdin, stdout, or stderr is closed;
|
|
53
|
+
- stdout or stderr cannot be written, or the write has not finished 1
|
|
54
|
+
second after it starts;
|
|
55
|
+
- any step raises.
|
|
56
|
+
|
|
57
|
+
A transcript line over 16 MiB or a text over 2 MiB is skipped. The gate does
|
|
58
|
+
not read ``stop_hook_active``. Both reasons tell the agent to read the task
|
|
59
|
+
list, its notes, and the live state of each item, to keep the turn's chat text inside
|
|
60
|
+
the limit ``question_brief_gate.py`` counts, and to send each item as its own
|
|
61
|
+
``AskUserQuestion`` question. A hold exits with code 2 and writes the reason
|
|
62
|
+
to stdout and stderr.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
from __future__ import annotations
|
|
66
|
+
|
|
67
|
+
import json
|
|
68
|
+
import os
|
|
69
|
+
import subprocess
|
|
70
|
+
import sys
|
|
71
|
+
import threading
|
|
72
|
+
import time
|
|
73
|
+
from pathlib import Path
|
|
74
|
+
|
|
75
|
+
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
76
|
+
if hooks_root_directory not in sys.path:
|
|
77
|
+
sys.path.insert(0, hooks_root_directory)
|
|
78
|
+
blocking_directory = str(Path(__file__).resolve().parent)
|
|
79
|
+
if blocking_directory not in sys.path:
|
|
80
|
+
sys.path.insert(0, blocking_directory)
|
|
81
|
+
|
|
82
|
+
from closing_question_round_lines import transcript_lines
|
|
83
|
+
from closing_question_round_open_items import declares_no_open_items, marked_open_lines
|
|
84
|
+
from closing_question_round_transcript import (
|
|
85
|
+
assistant_texts,
|
|
86
|
+
block_texts,
|
|
87
|
+
closing_reply_texts,
|
|
88
|
+
has_work_call,
|
|
89
|
+
last_typed_prompt_position,
|
|
90
|
+
stretch_start,
|
|
91
|
+
top_level_records,
|
|
92
|
+
)
|
|
93
|
+
from open_tasks_transcript_records import is_assistant_record
|
|
94
|
+
|
|
95
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
96
|
+
ALL_REASON_LEADS,
|
|
97
|
+
BACKGROUND_TASKS_KEY,
|
|
98
|
+
CLIPPED_LINE_END,
|
|
99
|
+
CLOSING_ROUND_INSTRUCTION,
|
|
100
|
+
CLOSING_ROUND_REASON_LEAD,
|
|
101
|
+
CODE_SPAN_SHOWN_TEXT,
|
|
102
|
+
DECISION_PROCESS_FLAG,
|
|
103
|
+
FEEDBACK_OPENING_LENGTH,
|
|
104
|
+
HEADLESS_SESSION_ATTENDED_TEXT,
|
|
105
|
+
MAXIMUM_HOLD_COUNT,
|
|
106
|
+
MAXIMUM_PAYLOAD_BYTES,
|
|
107
|
+
MAXIMUM_QUOTED_LINE_COUNT,
|
|
108
|
+
MAXIMUM_QUOTED_LINE_LENGTH,
|
|
109
|
+
OPEN_ITEM_LINES_LABEL,
|
|
110
|
+
OUTPUT_WATCHDOG_SECONDS,
|
|
111
|
+
QUOTED_LINE_TEMPLATE,
|
|
112
|
+
REASON_PART_SEPARATOR,
|
|
113
|
+
REMAINING_LINES_TEMPLATE,
|
|
114
|
+
SESSION_ATTENDED_ENVIRONMENT_VARIABLE,
|
|
115
|
+
STDIN_WATCHDOG_SECONDS,
|
|
116
|
+
STOP_DEADLINE_SECONDS,
|
|
117
|
+
SUBAGENT_ID_KEY,
|
|
118
|
+
WATCHDOG_GRACE_SECONDS,
|
|
119
|
+
WORK_INSTRUCTION,
|
|
120
|
+
WORK_REASON_LEAD,
|
|
121
|
+
)
|
|
122
|
+
from hooks_constants.hook_block_logger import log_hook_block
|
|
123
|
+
from hooks_constants.setup_project_paths_constants import UTF8_BYTE_ORDER_MARK
|
|
124
|
+
from hooks_constants.open_tasks_stop_gate_constants import (
|
|
125
|
+
ALLOW_EXIT_CODE,
|
|
126
|
+
CONTENT_KEY,
|
|
127
|
+
CURSOR_CONVERSATION_ID_KEY,
|
|
128
|
+
HOOK_EVENT_NAME,
|
|
129
|
+
MESSAGE_KEY,
|
|
130
|
+
RECORD_TYPE_KEY,
|
|
131
|
+
STOP_HOOK_FEEDBACK_PREFIX,
|
|
132
|
+
TRANSCRIPT_PATH_KEY,
|
|
133
|
+
USER_RECORD_TYPE,
|
|
134
|
+
WORD_SEPARATOR,
|
|
135
|
+
)
|
|
136
|
+
from hooks_constants.stop_hook_hold import hold_stop
|
|
137
|
+
from hooks_constants.stop_hook_hold_constants import BLOCK_EXIT_CODE
|
|
138
|
+
from hooks_constants.user_step_wording_constants import CODE_SPAN_STAND_IN
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def is_exempt_stop(all_hook_input_fields: dict[str, object]) -> bool:
|
|
142
|
+
"""Return True for a subagent, a Cursor stop, a headless run, or a stop with background work running."""
|
|
143
|
+
if all_hook_input_fields.get(SUBAGENT_ID_KEY) or all_hook_input_fields.get(
|
|
144
|
+
CURSOR_CONVERSATION_ID_KEY
|
|
145
|
+
):
|
|
146
|
+
return True
|
|
147
|
+
if os.environ.get(SESSION_ATTENDED_ENVIRONMENT_VARIABLE) == HEADLESS_SESSION_ATTENDED_TEXT:
|
|
148
|
+
return True
|
|
149
|
+
all_background_tasks = all_hook_input_fields.get(BACKGROUND_TASKS_KEY)
|
|
150
|
+
return isinstance(all_background_tasks, (list, dict, str)) and bool(all_background_tasks)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _record_text(all_record_fields: dict[str, object]) -> str:
|
|
154
|
+
message_body = all_record_fields.get(MESSAGE_KEY)
|
|
155
|
+
content_value = message_body.get(CONTENT_KEY) if isinstance(message_body, dict) else None
|
|
156
|
+
if isinstance(content_value, str):
|
|
157
|
+
return content_value
|
|
158
|
+
return WORD_SEPARATOR.join(block_texts(all_record_fields))
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _is_own_hold(all_record_fields: dict[str, object]) -> bool:
|
|
162
|
+
if all_record_fields.get(RECORD_TYPE_KEY) != USER_RECORD_TYPE:
|
|
163
|
+
return False
|
|
164
|
+
feedback_text = _record_text(all_record_fields).lstrip()
|
|
165
|
+
if not feedback_text.startswith(STOP_HOOK_FEEDBACK_PREFIX):
|
|
166
|
+
return False
|
|
167
|
+
feedback_opening = feedback_text[:FEEDBACK_OPENING_LENGTH]
|
|
168
|
+
return any(each_lead in feedback_opening for each_lead in ALL_REASON_LEADS)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def prior_hold_count(all_counted_records: list[dict[str, object]]) -> int:
|
|
172
|
+
"""Return how many times this gate already held a stop among the records.
|
|
173
|
+
|
|
174
|
+
A hold is a ``user`` record whose text starts with "Stop hook feedback" and
|
|
175
|
+
carries the first sentence of either of this gate's reasons within its
|
|
176
|
+
first 200 characters. A record that quotes the sentence further in, such
|
|
177
|
+
as a worker's report, is no hold.
|
|
178
|
+
"""
|
|
179
|
+
return sum(_is_own_hold(each_record) for each_record in all_counted_records)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _quoted_line(open_line: str) -> str:
|
|
183
|
+
shown_line = open_line.replace(CODE_SPAN_STAND_IN, CODE_SPAN_SHOWN_TEXT)
|
|
184
|
+
if len(shown_line) > MAXIMUM_QUOTED_LINE_LENGTH:
|
|
185
|
+
kept_length = MAXIMUM_QUOTED_LINE_LENGTH - len(CLIPPED_LINE_END)
|
|
186
|
+
shown_line = shown_line[:kept_length] + CLIPPED_LINE_END
|
|
187
|
+
return QUOTED_LINE_TEMPLATE.format(line=shown_line)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def closing_round_reason(all_open_item_lines: list[str]) -> str:
|
|
191
|
+
"""Return the hold reason: the fixed first sentence, the quoted lines, and the instruction.
|
|
192
|
+
|
|
193
|
+
The last 8 lines at most are quoted, each cut to 160 characters, because
|
|
194
|
+
the newest lines are the ones most likely to still stand. A count of the
|
|
195
|
+
earlier lines follows them when there are more.
|
|
196
|
+
"""
|
|
197
|
+
all_quoted_lines = [
|
|
198
|
+
_quoted_line(each_line) for each_line in all_open_item_lines[-MAXIMUM_QUOTED_LINE_COUNT:]
|
|
199
|
+
]
|
|
200
|
+
remaining_count = len(all_open_item_lines) - len(all_quoted_lines)
|
|
201
|
+
all_remaining_notes = (
|
|
202
|
+
[REMAINING_LINES_TEMPLATE.format(remaining_count=remaining_count)]
|
|
203
|
+
if remaining_count
|
|
204
|
+
else []
|
|
205
|
+
)
|
|
206
|
+
return REASON_PART_SEPARATOR.join(
|
|
207
|
+
[
|
|
208
|
+
CLOSING_ROUND_REASON_LEAD,
|
|
209
|
+
OPEN_ITEM_LINES_LABEL,
|
|
210
|
+
*all_quoted_lines,
|
|
211
|
+
*all_remaining_notes,
|
|
212
|
+
CLOSING_ROUND_INSTRUCTION,
|
|
213
|
+
]
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def work_reason() -> str:
|
|
218
|
+
"""Return the hold reason for work with no card and no declaration: its fixed first sentence, then the instruction."""
|
|
219
|
+
return REASON_PART_SEPARATOR.join([WORK_REASON_LEAD, WORK_INSTRUCTION])
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _covered_texts_and_records(
|
|
223
|
+
all_hook_input_fields: dict[str, object], deadline_at: float
|
|
224
|
+
) -> tuple[list[str], list[dict[str, object]], list[dict[str, object]]] | None:
|
|
225
|
+
all_lines = transcript_lines(all_hook_input_fields.get(TRANSCRIPT_PATH_KEY), deadline_at)
|
|
226
|
+
prompt_position = last_typed_prompt_position(all_lines, deadline_at)
|
|
227
|
+
if prompt_position is None:
|
|
228
|
+
return None
|
|
229
|
+
start_position = stretch_start(all_lines, deadline_at)
|
|
230
|
+
count_start = max(start_position, prompt_position + 1)
|
|
231
|
+
all_counted_records = top_level_records(all_lines[count_start:], deadline_at)
|
|
232
|
+
all_stretch_records = [
|
|
233
|
+
*top_level_records(all_lines[start_position:count_start], deadline_at),
|
|
234
|
+
*all_counted_records,
|
|
235
|
+
]
|
|
236
|
+
all_payload_texts = closing_reply_texts(all_hook_input_fields, all_lines, deadline_at)
|
|
237
|
+
return all_payload_texts, all_stretch_records, all_counted_records
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _records_since_declaration(
|
|
241
|
+
all_stretch_records: list[dict[str, object]], deadline_at: float
|
|
242
|
+
) -> list[dict[str, object]]:
|
|
243
|
+
for each_position in range(len(all_stretch_records) - 1, -1, -1):
|
|
244
|
+
each_record = all_stretch_records[each_position]
|
|
245
|
+
all_record_texts = block_texts(each_record) if is_assistant_record(each_record) else []
|
|
246
|
+
if has_work_call([each_record]) or not all_record_texts:
|
|
247
|
+
continue
|
|
248
|
+
if declares_no_open_items(all_record_texts[-1], deadline_at):
|
|
249
|
+
return all_stretch_records[each_position + 1 :]
|
|
250
|
+
return all_stretch_records
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _hold_reason(all_hook_input_fields: dict[str, object], deadline_at: float) -> str | None:
|
|
254
|
+
covered_session = _covered_texts_and_records(all_hook_input_fields, deadline_at)
|
|
255
|
+
if covered_session is None:
|
|
256
|
+
return None
|
|
257
|
+
all_payload_texts, all_stretch_records, all_counted_records = covered_session
|
|
258
|
+
if prior_hold_count(all_counted_records) >= MAXIMUM_HOLD_COUNT:
|
|
259
|
+
return None
|
|
260
|
+
all_texts = [*assistant_texts(all_stretch_records), *all_payload_texts]
|
|
261
|
+
all_open_item_lines = marked_open_lines(all_texts, deadline_at)
|
|
262
|
+
if all_open_item_lines:
|
|
263
|
+
return closing_round_reason(all_open_item_lines)
|
|
264
|
+
all_undeclared_records = _records_since_declaration(all_stretch_records, deadline_at)
|
|
265
|
+
all_work_positions = [
|
|
266
|
+
each_position
|
|
267
|
+
for each_position, each_record in enumerate(all_undeclared_records)
|
|
268
|
+
if has_work_call([each_record])
|
|
269
|
+
]
|
|
270
|
+
if not all_work_positions:
|
|
271
|
+
return None
|
|
272
|
+
all_later_texts = [
|
|
273
|
+
*assistant_texts(all_undeclared_records[all_work_positions[-1] + 1 :]),
|
|
274
|
+
*all_payload_texts,
|
|
275
|
+
]
|
|
276
|
+
closing_reply = all_later_texts[-1] if all_later_texts else ""
|
|
277
|
+
if declares_no_open_items(closing_reply, deadline_at):
|
|
278
|
+
return None
|
|
279
|
+
return work_reason()
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _capped_hook_input() -> dict[str, object] | None:
|
|
283
|
+
payload_bytes = sys.stdin.buffer.read(MAXIMUM_PAYLOAD_BYTES + 1)
|
|
284
|
+
if len(payload_bytes) > MAXIMUM_PAYLOAD_BYTES:
|
|
285
|
+
return None
|
|
286
|
+
payload_text = payload_bytes.decode(errors="replace").strip().removeprefix(UTF8_BYTE_ORDER_MARK)
|
|
287
|
+
try:
|
|
288
|
+
parsed_payload = json.loads(payload_text)
|
|
289
|
+
except ValueError:
|
|
290
|
+
return None
|
|
291
|
+
return parsed_payload if isinstance(parsed_payload, dict) else None
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _stop_decision(deadline_at: float) -> int:
|
|
295
|
+
all_hook_input_fields = _capped_hook_input()
|
|
296
|
+
if all_hook_input_fields is None or is_exempt_stop(all_hook_input_fields):
|
|
297
|
+
return ALLOW_EXIT_CODE
|
|
298
|
+
block_reason = _hold_reason(all_hook_input_fields, deadline_at)
|
|
299
|
+
if block_reason is None:
|
|
300
|
+
return ALLOW_EXIT_CODE
|
|
301
|
+
log_hook_block(Path(__file__).name, HOOK_EVENT_NAME, block_reason)
|
|
302
|
+
return hold_stop(all_hook_input_fields, block_reason)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def _allow_now(output_lock: threading.Lock) -> None:
|
|
306
|
+
if output_lock.acquire(blocking=False):
|
|
307
|
+
os._exit(ALLOW_EXIT_CODE)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def main() -> int:
|
|
311
|
+
"""Decide the stop in this process and return its exit code."""
|
|
312
|
+
deadline_at = time.monotonic() + STOP_DEADLINE_SECONDS
|
|
313
|
+
try:
|
|
314
|
+
return _stop_decision(deadline_at)
|
|
315
|
+
except Exception:
|
|
316
|
+
return ALLOW_EXIT_CODE
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def _decision_run(payload_bytes: bytes, deadline_at: float) -> subprocess.CompletedProcess[bytes]:
|
|
320
|
+
return subprocess.run(
|
|
321
|
+
[sys.executable, str(Path(__file__).resolve()), DECISION_PROCESS_FLAG],
|
|
322
|
+
input=payload_bytes,
|
|
323
|
+
capture_output=True,
|
|
324
|
+
timeout=max(deadline_at - time.monotonic(), 0.0),
|
|
325
|
+
check=False,
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _relayed_exit_code(completed_decision: subprocess.CompletedProcess[bytes]) -> int:
|
|
330
|
+
output_watchdog = threading.Timer(OUTPUT_WATCHDOG_SECONDS, os._exit, args=(ALLOW_EXIT_CODE,))
|
|
331
|
+
output_watchdog.daemon = True
|
|
332
|
+
output_watchdog.start()
|
|
333
|
+
all_outputs = (
|
|
334
|
+
(sys.stderr, completed_decision.stderr),
|
|
335
|
+
(sys.stdout, completed_decision.stdout),
|
|
336
|
+
)
|
|
337
|
+
try:
|
|
338
|
+
for each_stream, each_output in all_outputs:
|
|
339
|
+
each_stream.buffer.write(each_output)
|
|
340
|
+
each_stream.buffer.flush()
|
|
341
|
+
except (OSError, ValueError):
|
|
342
|
+
return ALLOW_EXIT_CODE
|
|
343
|
+
finally:
|
|
344
|
+
output_watchdog.cancel()
|
|
345
|
+
return completed_decision.returncode
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def supervised_main() -> int:
|
|
349
|
+
"""Run ``main`` in a child process and allow the stop when it outlasts 8 seconds.
|
|
350
|
+
|
|
351
|
+
One C-level step, such as a JSON parse, can hold the interpreter for seconds,
|
|
352
|
+
and no thread of the same process runs then. The child is killed at the
|
|
353
|
+
deadline, and a watchdog thread here ends a stdin read that never finishes
|
|
354
|
+
at 8.5 seconds. A child that exits with a code other than 0 or 2 allows
|
|
355
|
+
the stop with no output, and so do a closed standard stream and output
|
|
356
|
+
that cannot be written. Both streams are written and flushed here, stderr
|
|
357
|
+
first, so stdout holds the hold only once the reason reached stderr, and a
|
|
358
|
+
write that has not finished 1 second after it starts, such as one to a
|
|
359
|
+
full pipe nobody reads, ends the process with 0.
|
|
360
|
+
"""
|
|
361
|
+
if sys.stdin is None or sys.stdout is None or sys.stderr is None:
|
|
362
|
+
return ALLOW_EXIT_CODE
|
|
363
|
+
deadline_at = time.monotonic() + STOP_DEADLINE_SECONDS + WATCHDOG_GRACE_SECONDS
|
|
364
|
+
output_lock = threading.Lock()
|
|
365
|
+
watchdog = threading.Timer(STDIN_WATCHDOG_SECONDS, _allow_now, args=(output_lock,))
|
|
366
|
+
watchdog.daemon = True
|
|
367
|
+
watchdog.start()
|
|
368
|
+
try:
|
|
369
|
+
payload_bytes = sys.stdin.buffer.read(MAXIMUM_PAYLOAD_BYTES + 1)
|
|
370
|
+
if len(payload_bytes) > MAXIMUM_PAYLOAD_BYTES:
|
|
371
|
+
return ALLOW_EXIT_CODE
|
|
372
|
+
completed_decision = _decision_run(payload_bytes, deadline_at)
|
|
373
|
+
except (OSError, subprocess.SubprocessError):
|
|
374
|
+
return ALLOW_EXIT_CODE
|
|
375
|
+
finally:
|
|
376
|
+
watchdog.cancel()
|
|
377
|
+
if completed_decision.returncode not in (ALLOW_EXIT_CODE, BLOCK_EXIT_CODE):
|
|
378
|
+
return ALLOW_EXIT_CODE
|
|
379
|
+
if not output_lock.acquire(blocking=False):
|
|
380
|
+
return ALLOW_EXIT_CODE
|
|
381
|
+
return _relayed_exit_code(completed_decision)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
if __name__ == "__main__":
|
|
385
|
+
if DECISION_PROCESS_FLAG in sys.argv[1:]:
|
|
386
|
+
sys.exit(main())
|
|
387
|
+
os._exit(supervised_main())
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Read transcript lines for the closing question round gate, with a clock check before each one.
|
|
2
|
+
|
|
3
|
+
::
|
|
4
|
+
|
|
5
|
+
b'{"a": 1}\\n{"b": 2}' -> ['{"a": 1}', '{"b": 2}']
|
|
6
|
+
b'{"\\\\u0074urnOrigin": "sdk"}' -> ['{"turnOrigin": "sdk"}']
|
|
7
|
+
a 200 MB line between two lines -> [first line, "", last line]
|
|
8
|
+
|
|
9
|
+
The gate's substring scans run on these lines, so each line reaches them with
|
|
10
|
+
escapes of printable ASCII spelled out and no byte order mark.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
from collections.abc import Iterable, Iterator
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import TypeVar
|
|
21
|
+
|
|
22
|
+
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
23
|
+
if hooks_root_directory not in sys.path:
|
|
24
|
+
sys.path.insert(0, hooks_root_directory)
|
|
25
|
+
blocking_directory = str(Path(__file__).resolve().parent)
|
|
26
|
+
if blocking_directory not in sys.path:
|
|
27
|
+
sys.path.insert(0, blocking_directory)
|
|
28
|
+
|
|
29
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
30
|
+
ESCAPED_ASCII_TEXT,
|
|
31
|
+
ESCAPED_PRINTABLE_ASCII_PATTERN,
|
|
32
|
+
LINE_FEED_BYTES,
|
|
33
|
+
MAXIMUM_SCANNED_BYTES,
|
|
34
|
+
MAXIMUM_SCANNED_TEXT_BYTES,
|
|
35
|
+
)
|
|
36
|
+
from hooks_constants.open_tasks_stop_gate_constants import UTF8_ENCODING
|
|
37
|
+
from hooks_constants.setup_project_paths_constants import (
|
|
38
|
+
DECODE_ERRORS_POLICY,
|
|
39
|
+
UTF8_BYTE_ORDER_MARK,
|
|
40
|
+
)
|
|
41
|
+
from open_tasks_transcript_records import parsed_object
|
|
42
|
+
|
|
43
|
+
ScannedItem = TypeVar("ScannedItem")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def before_deadline(all_items: Iterable[ScannedItem], deadline_at: float) -> Iterator[ScannedItem]:
|
|
47
|
+
"""Yield each text, line, or match in order, and raise TimeoutError once the monotonic clock reaches the deadline.
|
|
48
|
+
|
|
49
|
+
The clock is read before each item is yielded.
|
|
50
|
+
"""
|
|
51
|
+
for each_item in all_items:
|
|
52
|
+
if time.monotonic() >= deadline_at:
|
|
53
|
+
raise TimeoutError
|
|
54
|
+
yield each_item
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def is_within_scan_cap(scanned_text: str) -> bool:
|
|
58
|
+
"""Return True when the text holds 2 MiB or less once encoded as UTF-8."""
|
|
59
|
+
return len(scanned_text) <= MAXIMUM_SCANNED_TEXT_BYTES and (
|
|
60
|
+
len(scanned_text.encode(UTF8_ENCODING, DECODE_ERRORS_POLICY)) <= MAXIMUM_SCANNED_TEXT_BYTES
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def transcript_lines(transcript_path: object, deadline_at: float) -> list[str]:
|
|
65
|
+
"""Return the transcript file's lines, with each line over 16 MiB read as an empty line.
|
|
66
|
+
|
|
67
|
+
::
|
|
68
|
+
|
|
69
|
+
b'{"a": 1}\\n{"b": 2}' -> ['{"a": 1}', '{"b": 2}']
|
|
70
|
+
a 200 MB line between two lines -> [first line, "", last line]
|
|
71
|
+
no string, or a missing file -> []
|
|
72
|
+
|
|
73
|
+
The file is read as bytes and cut at each line feed. Each line of 16 MiB or
|
|
74
|
+
less is decoded as UTF-8 with bad bytes replaced, and the empty stand-in
|
|
75
|
+
keeps the positions of the lines after it. A byte order mark at the start
|
|
76
|
+
of a line is dropped. A line that holds an escape of a printable ASCII
|
|
77
|
+
character, ``\\u0020`` to ``\\u007f``, and parses as a JSON object is
|
|
78
|
+
written out again without escapes for printable characters,
|
|
79
|
+
so ``"\\u0074urnOrigin"`` reads as ``"turnOrigin"`` for the substring
|
|
80
|
+
checks that run before each parse. The clock is read before each line, and
|
|
81
|
+
TimeoutError is raised once it reaches ``deadline_at``.
|
|
82
|
+
"""
|
|
83
|
+
if not isinstance(transcript_path, str):
|
|
84
|
+
return []
|
|
85
|
+
try:
|
|
86
|
+
transcript_bytes = Path(transcript_path).read_bytes()
|
|
87
|
+
except (OSError, ValueError):
|
|
88
|
+
return []
|
|
89
|
+
return [
|
|
90
|
+
_scannable_line(each_line)
|
|
91
|
+
for each_line in before_deadline(transcript_bytes.split(LINE_FEED_BYTES), deadline_at)
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _scannable_line(line_bytes: bytes) -> str:
|
|
96
|
+
if len(line_bytes) > MAXIMUM_SCANNED_BYTES:
|
|
97
|
+
return ""
|
|
98
|
+
line_text = line_bytes.decode(UTF8_ENCODING, DECODE_ERRORS_POLICY).removeprefix(
|
|
99
|
+
UTF8_BYTE_ORDER_MARK
|
|
100
|
+
)
|
|
101
|
+
if ESCAPED_ASCII_TEXT not in line_text or not ESCAPED_PRINTABLE_ASCII_PATTERN.search(line_text):
|
|
102
|
+
return line_text
|
|
103
|
+
each_record = parsed_object(line_text)
|
|
104
|
+
if each_record is None:
|
|
105
|
+
return line_text
|
|
106
|
+
try:
|
|
107
|
+
return json.dumps(each_record, ensure_ascii=False)
|
|
108
|
+
except (RecursionError, ValueError):
|
|
109
|
+
return line_text
|