claude-dev-env 8.117.2 → 8.117.4

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 (77) hide show
  1. package/_shared/pr-loop/scripts/pyproject.toml +1 -1
  2. package/_shared/process-tree/scripts/pyproject.toml +1 -1
  3. package/bin/cde.mjs +4 -3
  4. package/bin/cde.test.mjs +87 -1
  5. package/bin/codex-compat.mjs +4 -3
  6. package/bin/codex-compat.test.mjs +91 -1
  7. package/bin/install-plan.mjs +2 -1
  8. package/bin/install.mjs +24 -7
  9. package/bin/install.plan.test.mjs +15 -0
  10. package/bin/install.test.mjs +61 -0
  11. package/bin/python-probe.mjs +42 -0
  12. package/bin/python-probe.test.mjs +19 -2
  13. package/docs/rule-guides/features-start-with-an-eval.md +3 -3
  14. package/docs/rule-guides/proof-before-pull-request.md +2 -2
  15. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +11 -4
  16. package/hooks/blocking/build_eval_before_eval_write_gate.py +10 -4
  17. package/hooks/blocking/followup_pr_dedupe.py +381 -64
  18. package/hooks/blocking/gate_child_process.py +187 -0
  19. package/hooks/blocking/gate_run_reads.py +116 -35
  20. package/hooks/blocking/merge_review_gate.py +93 -10
  21. package/hooks/blocking/merge_review_gate_parts/create_folders.py +346 -0
  22. package/hooks/blocking/merge_review_gate_parts/create_options.py +86 -0
  23. package/hooks/blocking/merge_review_gate_parts/folder_state.py +107 -0
  24. package/hooks/blocking/merge_review_gate_parts/open_request.py +199 -64
  25. package/hooks/blocking/merge_review_gate_parts/quoted_lines.py +283 -0
  26. package/hooks/blocking/merge_review_gate_parts/wrapped_creates.py +126 -0
  27. package/hooks/blocking/pr_lifecycle_skill_gate.py +61 -18
  28. package/hooks/blocking/pre_tool_use_dispatcher.py +25 -3
  29. package/hooks/blocking/pull_request_proof.py +5 -51
  30. package/hooks/blocking/pull_request_proof_edit_gate.py +19 -5
  31. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +54 -0
  32. package/hooks/blocking/test_build_eval_before_eval_write_gate.py +64 -0
  33. package/hooks/blocking/test_followup_pr_dedupe.py +464 -1
  34. package/hooks/blocking/test_gate_child_process.py +64 -0
  35. package/hooks/blocking/test_gate_run_reads.py +364 -4
  36. package/hooks/blocking/test_merge_review_gate.py +832 -39
  37. package/hooks/blocking/test_pr_lifecycle_skill_gate.py +130 -4
  38. package/hooks/blocking/test_pre_tool_use_dispatcher.py +64 -0
  39. package/hooks/blocking/test_pull_request_proof.py +16 -0
  40. package/hooks/blocking/test_pull_request_proof_edit_gate.py +60 -0
  41. package/hooks/blocking/tests/test_create_folders.py +290 -0
  42. package/hooks/blocking/tests/test_create_options.py +34 -0
  43. package/hooks/blocking/tests/test_folder_state.py +40 -0
  44. package/hooks/blocking/tests/test_open_request.py +350 -44
  45. package/hooks/blocking/tests/test_quoted_lines.py +86 -0
  46. package/hooks/blocking/tests/test_wrapped_creates.py +42 -0
  47. package/hooks/features/bash-dispatchers.md +1 -1
  48. package/hooks/features/conduct-gates.md +7 -7
  49. package/hooks/features/write-edit-blocking.md +3 -3
  50. package/hooks/hooks_constants/build_eval_before_eval_write_gate_constants.py +5 -0
  51. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +34 -1
  52. package/hooks/hooks_constants/gate_fault.py +52 -0
  53. package/hooks/hooks_constants/gate_fault_constants.py +13 -0
  54. package/hooks/hooks_constants/gate_run_reads_constants.py +9 -1
  55. package/hooks/hooks_constants/merge_review_gate_constants.py +94 -1
  56. package/hooks/hooks_constants/piped_pytest_blocker_constants.py +2 -0
  57. package/hooks/hooks_constants/pr_lifecycle_skill_gate_constants.py +6 -1
  58. package/hooks/hooks_constants/pre_tool_use_stdin.py +23 -3
  59. package/hooks/hooks_constants/shell_command_pipeline.py +68 -8
  60. package/hooks/hooks_constants/shell_command_segments.py +2 -2
  61. package/hooks/hooks_constants/shell_command_wrappers.py +3 -2
  62. package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +7 -0
  63. package/hooks/hooks_constants/test_gate_fault.py +41 -0
  64. package/hooks/hooks_constants/test_piped_pytest_blocker_constants.py +5 -0
  65. package/hooks/hooks_constants/test_pr_lifecycle_skill_gate_constants.py +10 -0
  66. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +97 -0
  67. package/hooks/hooks_constants/test_shell_command_pipeline.py +55 -0
  68. package/hooks/hooks_constants/test_shell_command_segments.py +15 -0
  69. package/hooks/hooks_constants/test_shell_command_wrappers.py +6 -0
  70. package/hooks/hooks_constants/transcript_skill_scan_constants.py +6 -0
  71. package/hooks/pyproject.toml +2 -3
  72. package/hooks/test_python_floor.py +197 -0
  73. package/hooks/test_transcript_skill_scan.py +324 -0
  74. package/hooks/transcript_skill_scan.py +233 -2
  75. package/hooks/validators/code_quality_checks.py +119 -17
  76. package/hooks/validators/test_code_quality_checks.py +110 -4
  77. package/package.json +1 -1
@@ -1,10 +1,15 @@
1
1
  """Behavior tests for the answers one pull request lifecycle gate run remembers."""
2
2
 
3
3
  import ast
4
+ import io
4
5
  import json
6
+ import os
7
+ import signal
5
8
  import subprocess
6
9
  import sys
7
10
  import threading
11
+ import time
12
+ from collections.abc import Iterator
8
13
  from pathlib import Path
9
14
 
10
15
  import pytest
@@ -13,15 +18,17 @@ HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
13
18
  if str(HOOKS_DIRECTORY) not in sys.path:
14
19
  sys.path.insert(0, str(HOOKS_DIRECTORY))
15
20
 
16
- from blocking import gate_run_reads
21
+ from blocking import gate_child_process, gate_run_reads
17
22
  from blocking.gate_run_reads import (
18
23
  ReadSlice,
19
24
  answer_once,
20
25
  answer_within_allowance,
21
26
  child_stdout,
22
27
  gate_run,
28
+ lines_within_slice,
23
29
  read_allowance,
24
30
  read_deadlines_started,
31
+ transcript_lines_within_slice,
25
32
  )
26
33
  from hooks_constants.followup_pr_dedupe_constants import READ_TIMEOUT_SECONDS
27
34
  from hooks_constants.gate_run_reads_constants import (
@@ -29,7 +36,9 @@ from hooks_constants.gate_run_reads_constants import (
29
36
  KILL_GRACE_SECONDS,
30
37
  MOST_CUT_READS_PER_RUN,
31
38
  PROOF_AND_LOOK_READS_SECONDS,
32
- STARTUP_AND_SCAN_MARGIN_SECONDS,
39
+ STARTUP_AND_OUTPUT_MARGIN_SECONDS,
40
+ PROCESS_HANDLE_ATTRIBUTE,
41
+ STOPPED_JOB_EXIT_CODE,
33
42
  )
34
43
 
35
44
  CLOCK_START = 100.0
@@ -53,6 +62,29 @@ CHILD_STARTS_A_GRANDCHILD = (
53
62
  " time.sleep(30)"
54
63
  )
55
64
  GATE_COMMAND_SUFFIX = "hooks/blocking/pr_lifecycle_" + "skill_gate.py"
65
+ CHILD_STARTS_A_SLEEPING_GRANDCHILD = (
66
+ "import subprocess, sys, time;"
67
+ " subprocess.Popen([sys.executable, '-c', sys.argv[1], sys.argv[2]]);"
68
+ " time.sleep(60)"
69
+ )
70
+ GRANDCHILD_WRITES_ITS_PID_AND_SLEEPS = (
71
+ "import os, sys, time;"
72
+ " open(sys.argv[1] + '.part', 'w').write(str(os.getpid()));"
73
+ " os.replace(sys.argv[1] + '.part', sys.argv[1]);"
74
+ " time.sleep(60)"
75
+ )
76
+ GRANDCHILD_READ_TIMEOUT_SECONDS = 3
77
+ GRANDCHILD_PID_WAIT_SECONDS = 5
78
+ GRANDCHILD_GONE_WITHIN_SECONDS = 2
79
+ WINDOWS_STOP_SLACK_SECONDS = 0.5
80
+ CHILD_STARTS_A_GRANDCHILD_AND_EXITS = (
81
+ "import subprocess, sys;"
82
+ " subprocess.Popen([sys.executable, '-c', sys.argv[1], sys.argv[2]])"
83
+ )
84
+ FAKE_JOB_HANDLE = 4242
85
+ FAKE_PROCESS_HANDLE = 777
86
+ HUGE_LINE_BYTES = 1000
87
+ SECONDS_LEFT_BEFORE_HUGE_LINE = 5
56
88
 
57
89
 
58
90
  class FakeClock:
@@ -182,6 +214,14 @@ def test_child_stdout_should_return_the_output_of_a_child_that_answers() -> None
182
214
  assert not run.was_read_cut_short
183
215
 
184
216
 
217
+ def test_child_stdout_should_start_its_child_with_the_environment_it_is_given() -> None:
218
+ read_variable = [sys.executable, "-c", "import os; print(os.environ.get('GATE_READ_PROBE', 'unset'))"]
219
+ environment = {**os.environ, "GATE_READ_PROBE": "given"}
220
+ with gate_run():
221
+ assert child_stdout(read_variable, 5, ReadSlice.PROOF_AND_LOOK, None, environment) == "given\n"
222
+ assert child_stdout(read_variable, 5, ReadSlice.PROOF_AND_LOOK) == "unset\n"
223
+
224
+
185
225
  def test_child_stdout_should_start_its_child_with_the_hidden_window_flags(monkeypatch: pytest.MonkeyPatch) -> None:
186
226
  all_seen_flags: list[object] = []
187
227
 
@@ -189,7 +229,7 @@ def test_child_stdout_should_start_its_child_with_the_hidden_window_flags(monkey
189
229
  all_seen_flags.append(all_options.get("creationflags"))
190
230
  raise OSError("this test starts no child")
191
231
 
192
- monkeypatch.setattr(gate_run_reads, "hidden_window_creation_flags", lambda: HIDDEN_WINDOW_FLAGS)
232
+ monkeypatch.setattr(gate_child_process, "hidden_window_creation_flags", lambda: HIDDEN_WINDOW_FLAGS)
193
233
  monkeypatch.setattr(gate_run_reads.subprocess, "Popen", refuse_to_start)
194
234
  assert child_stdout(["git", "status"], 5, ReadSlice.PROOF_AND_LOOK) is None
195
235
  assert all_seen_flags == [HIDDEN_WINDOW_FLAGS]
@@ -206,6 +246,19 @@ def test_child_stdout_should_kill_a_child_that_outlives_its_own_timeout(
206
246
  assert not run.was_read_cut_short
207
247
 
208
248
 
249
+ def test_child_stdout_should_count_a_read_stopped_at_its_own_timeout() -> None:
250
+ with gate_run() as run:
251
+ assert child_stdout([sys.executable, "-c", SLEEP_THEN_PRINT], 0.5, ReadSlice.PROOF_AND_LOOK) is None
252
+ assert run.timed_out_read_count == 1
253
+ assert not run.was_read_cut_short
254
+
255
+
256
+ def test_child_stdout_should_not_count_a_fast_failing_child_as_timed_out() -> None:
257
+ with gate_run() as run:
258
+ assert child_stdout([sys.executable, "-c", "raise SystemExit(3)"], 5, ReadSlice.PROOF_AND_LOOK) is None
259
+ assert run.timed_out_read_count == 0
260
+
261
+
209
262
  def test_child_stdout_should_stop_waiting_when_a_grandchild_holds_the_pipe(tmp_path: Path) -> None:
210
263
  gave_up_path = tmp_path / "gave-up"
211
264
  release_path = tmp_path / "release"
@@ -223,6 +276,226 @@ def test_child_stdout_should_stop_waiting_when_a_grandchild_holds_the_pipe(tmp_p
223
276
  assert not gave_up_path.exists()
224
277
 
225
278
 
279
+ def _is_running(process_id: int) -> bool:
280
+ stat_path = Path(f"/proc/{process_id}/stat")
281
+ if stat_path.parent.parent.is_dir():
282
+ try:
283
+ stat_text = stat_path.read_text(encoding="ascii", errors="replace")
284
+ except OSError:
285
+ return False
286
+ return stat_text.rsplit(")", 1)[1].split()[0] != "Z"
287
+ try:
288
+ os.kill(process_id, 0)
289
+ except OSError:
290
+ return False
291
+ return True
292
+
293
+
294
+ def _is_gone_within(process_id: int, seconds: float) -> bool:
295
+ gives_up_at = time.monotonic() + seconds
296
+ while _is_running(process_id):
297
+ if time.monotonic() >= gives_up_at:
298
+ return False
299
+ time.sleep(0.05)
300
+ return True
301
+
302
+
303
+ def _grandchild_process_id(pid_path: Path) -> int:
304
+ gives_up_at = time.monotonic() + GRANDCHILD_PID_WAIT_SECONDS
305
+ while not pid_path.exists() or not pid_path.read_text(encoding="ascii").strip():
306
+ assert time.monotonic() < gives_up_at, "the grandchild never wrote its process id"
307
+ time.sleep(0.05)
308
+ return int(pid_path.read_text(encoding="ascii"))
309
+
310
+
311
+ def test_child_stdout_should_end_a_grandchild_that_holds_the_output_pipe(tmp_path: Path) -> None:
312
+ if sys.platform == "win32":
313
+ pytest.skip("POSIX process groups")
314
+ pid_path = tmp_path / "grandchild.pid"
315
+ command = [sys.executable, "-c", CHILD_STARTS_A_SLEEPING_GRANDCHILD, GRANDCHILD_WRITES_ITS_PID_AND_SLEEPS, str(pid_path)]
316
+ answer = child_stdout(command, GRANDCHILD_READ_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK)
317
+ grandchild_process_id = _grandchild_process_id(pid_path)
318
+ try:
319
+ assert answer is None
320
+ assert _is_gone_within(grandchild_process_id, GRANDCHILD_GONE_WITHIN_SECONDS)
321
+ finally:
322
+ if _is_running(grandchild_process_id):
323
+ os.kill(grandchild_process_id, signal.SIGKILL)
324
+
325
+
326
+ def test_child_stdout_should_end_a_grandchild_that_holds_the_output_pipe_on_windows(tmp_path: Path) -> None:
327
+ if sys.platform != "win32":
328
+ pytest.skip("taskkill runs only on Windows")
329
+ pid_path = tmp_path / "grandchild.pid"
330
+ command = [sys.executable, "-c", CHILD_STARTS_A_SLEEPING_GRANDCHILD, GRANDCHILD_WRITES_ITS_PID_AND_SLEEPS, str(pid_path)]
331
+ started_at = time.monotonic()
332
+ answer = child_stdout(command, GRANDCHILD_READ_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK)
333
+ stop_seconds = time.monotonic() - started_at - GRANDCHILD_READ_TIMEOUT_SECONDS
334
+ grandchild_process_id = _grandchild_process_id(pid_path)
335
+ assert answer is None
336
+ assert stop_seconds <= KILL_GRACE_SECONDS + WINDOWS_STOP_SLACK_SECONDS
337
+ tasklist = subprocess.run(
338
+ ["tasklist", "/FI", f"PID eq {grandchild_process_id}", "/NH"],
339
+ capture_output=True,
340
+ text=True,
341
+ check=False,
342
+ )
343
+ assert str(grandchild_process_id) not in tasklist.stdout
344
+
345
+
346
+ def test_child_stdout_should_stop_a_windows_child_with_a_hidden_bounded_tree_kill(
347
+ monkeypatch: pytest.MonkeyPatch,
348
+ ) -> None:
349
+ if sys.platform == "win32":
350
+ pytest.skip("on Windows the read makes a job object and needs no taskkill")
351
+ all_stop_calls: list[tuple[list[str], dict[str, object]]] = []
352
+
353
+ def record_stop(all_command_words: list[str], **all_options: object) -> subprocess.CompletedProcess[str]:
354
+ all_stop_calls.append((all_command_words, all_options))
355
+ return subprocess.CompletedProcess(all_command_words, 0)
356
+
357
+ start_on_this_platform = gate_run_reads.subprocess.Popen
358
+
359
+ def start_without_windows_flags(*all_arguments: object, **all_options: object) -> subprocess.Popen[str]:
360
+ all_options.pop("creationflags")
361
+ return start_on_this_platform(*all_arguments, **all_options)
362
+
363
+ monkeypatch.setattr(gate_run_reads.sys, "platform", "win32")
364
+ monkeypatch.setattr(gate_child_process, "hidden_window_creation_flags", lambda: HIDDEN_WINDOW_FLAGS)
365
+ monkeypatch.setattr(gate_run_reads.subprocess, "Popen", start_without_windows_flags)
366
+ monkeypatch.setattr(gate_run_reads.subprocess, "run", record_stop)
367
+ started_at = time.monotonic()
368
+ assert child_stdout([sys.executable, "-c", SLEEP_THEN_PRINT], 0.5, ReadSlice.PROOF_AND_LOOK) is None
369
+ assert time.monotonic() - started_at < 0.5 + KILL_GRACE_SECONDS + WINDOWS_STOP_SLACK_SECONDS
370
+ ((all_command_words, all_options),) = all_stop_calls
371
+ assert all_command_words[:4] == ["taskkill", "/T", "/F", "/PID"]
372
+ assert all_command_words[4].isdigit()
373
+ assert all_options["creationflags"] == HIDDEN_WINDOW_FLAGS
374
+ assert 0 < float(str(all_options["timeout"])) < KILL_GRACE_SECONDS
375
+
376
+
377
+ class FakeKernel32:
378
+ """Stands in for kernel32 and notes each job call the read makes."""
379
+
380
+ def __init__(self, assigns: bool) -> None:
381
+ self.assigns = assigns
382
+ self.all_calls: list[tuple[str, tuple[object, ...]]] = []
383
+
384
+ def CreateJobObjectW(self, *all_arguments: object) -> int:
385
+ self.all_calls.append(("create", all_arguments))
386
+ return FAKE_JOB_HANDLE
387
+
388
+ def AssignProcessToJobObject(self, *all_arguments: object) -> int:
389
+ self.all_calls.append(("assign", all_arguments))
390
+ return int(self.assigns)
391
+
392
+ def TerminateJobObject(self, *all_arguments: object) -> int:
393
+ self.all_calls.append(("terminate", all_arguments))
394
+ return 1
395
+
396
+ def CloseHandle(self, *all_arguments: object) -> int:
397
+ self.all_calls.append(("close", all_arguments))
398
+ return 1
399
+
400
+
401
+ def _windows_read_with_fake_kernel(
402
+ monkeypatch: pytest.MonkeyPatch, kernel: FakeKernel32, script: str, own_timeout_seconds: float
403
+ ) -> tuple[str | None, list[list[str]]]:
404
+ if sys.platform == "win32":
405
+ pytest.skip("the fake kernel32 stands in for Windows on other platforms")
406
+ all_taskkill_words: list[list[str]] = []
407
+ start_on_this_platform = gate_run_reads.subprocess.Popen
408
+
409
+ def start_with_a_handle(*all_arguments: object, **all_options: object) -> subprocess.Popen[str]:
410
+ all_options.pop("creationflags")
411
+ child = start_on_this_platform(*all_arguments, **all_options)
412
+ vars(child)[PROCESS_HANDLE_ATTRIBUTE] = FAKE_PROCESS_HANDLE
413
+ return child
414
+
415
+ def record_taskkill(all_command_words: list[str], **all_options: object) -> subprocess.CompletedProcess[str]:
416
+ all_taskkill_words.append(all_command_words)
417
+ return subprocess.CompletedProcess(all_command_words, 0)
418
+
419
+ monkeypatch.setattr(gate_run_reads.sys, "platform", "win32")
420
+ monkeypatch.setattr(gate_child_process, "_kernel32", lambda: kernel)
421
+ monkeypatch.setattr(gate_run_reads.subprocess, "Popen", start_with_a_handle)
422
+ monkeypatch.setattr(gate_run_reads.subprocess, "run", record_taskkill)
423
+ answer = child_stdout([sys.executable, "-c", script], own_timeout_seconds, ReadSlice.PROOF_AND_LOOK)
424
+ return answer, all_taskkill_words
425
+
426
+
427
+ def test_child_stdout_should_end_the_windows_job_of_a_stopped_read(monkeypatch: pytest.MonkeyPatch) -> None:
428
+ kernel = FakeKernel32(assigns=True)
429
+ answer, all_taskkill_words = _windows_read_with_fake_kernel(monkeypatch, kernel, SLEEP_THEN_PRINT, 0.5)
430
+ assert answer is None
431
+ assert all_taskkill_words == []
432
+ assert [each_name for each_name, _ in kernel.all_calls] == ["create", "assign", "terminate", "close"]
433
+ assert kernel.all_calls[1][1] == (FAKE_JOB_HANDLE, FAKE_PROCESS_HANDLE)
434
+ assert kernel.all_calls[2][1] == (FAKE_JOB_HANDLE, STOPPED_JOB_EXIT_CODE)
435
+
436
+
437
+ def test_child_stdout_should_close_the_windows_job_of_a_read_that_answers(monkeypatch: pytest.MonkeyPatch) -> None:
438
+ kernel = FakeKernel32(assigns=True)
439
+ answer, all_taskkill_words = _windows_read_with_fake_kernel(monkeypatch, kernel, "print('answer')", 5)
440
+ assert answer == "answer\n"
441
+ assert all_taskkill_words == []
442
+ assert [each_name for each_name, _ in kernel.all_calls] == ["create", "assign", "close"]
443
+
444
+
445
+ def test_child_stdout_should_fall_back_to_taskkill_when_the_child_cannot_join_a_job(
446
+ monkeypatch: pytest.MonkeyPatch,
447
+ ) -> None:
448
+ kernel = FakeKernel32(assigns=False)
449
+ answer, all_taskkill_words = _windows_read_with_fake_kernel(monkeypatch, kernel, SLEEP_THEN_PRINT, 0.5)
450
+ assert answer is None
451
+ assert [each_words[:4] for each_words in all_taskkill_words] == [["taskkill", "/T", "/F", "/PID"]]
452
+ assert [each_name for each_name, _ in kernel.all_calls] == ["create", "assign", "close"]
453
+
454
+
455
+ def test_child_stdout_should_end_a_grandchild_that_outlives_its_exited_child(tmp_path: Path) -> None:
456
+ if sys.platform == "win32":
457
+ pytest.skip("POSIX process groups")
458
+ pid_path = tmp_path / "grandchild.pid"
459
+ command = [sys.executable, "-c", CHILD_STARTS_A_GRANDCHILD_AND_EXITS, GRANDCHILD_WRITES_ITS_PID_AND_SLEEPS, str(pid_path)]
460
+ answer = child_stdout(command, GRANDCHILD_READ_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK)
461
+ grandchild_process_id = _grandchild_process_id(pid_path)
462
+ try:
463
+ assert answer is None
464
+ assert _is_gone_within(grandchild_process_id, GRANDCHILD_GONE_WITHIN_SECONDS)
465
+ finally:
466
+ if _is_running(grandchild_process_id):
467
+ os.kill(grandchild_process_id, signal.SIGKILL)
468
+
469
+
470
+ def _is_running_on_windows(process_id: int) -> bool:
471
+ tasklist = subprocess.run(
472
+ ["tasklist", "/FI", f"PID eq {process_id}", "/NH"], capture_output=True, text=True, check=False
473
+ )
474
+ return str(process_id) in tasklist.stdout
475
+
476
+
477
+ def test_child_stdout_should_end_a_grandchild_that_outlives_its_exited_child_on_windows(tmp_path: Path) -> None:
478
+ if sys.platform != "win32":
479
+ pytest.skip("Windows job objects")
480
+ pid_path = tmp_path / "grandchild.pid"
481
+ command = [sys.executable, "-c", CHILD_STARTS_A_GRANDCHILD_AND_EXITS, GRANDCHILD_WRITES_ITS_PID_AND_SLEEPS, str(pid_path)]
482
+ with gate_run() as run:
483
+ answer = child_stdout(command, GRANDCHILD_READ_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK)
484
+ grandchild_process_id = _grandchild_process_id(pid_path)
485
+ assert answer is None
486
+ assert run.timed_out_read_count == 1
487
+ assert not _is_running_on_windows(grandchild_process_id)
488
+
489
+
490
+ def test_child_stdout_should_return_the_text_of_a_plain_read_on_windows() -> None:
491
+ if sys.platform != "win32":
492
+ pytest.skip("Windows job objects")
493
+ with gate_run() as run:
494
+ answer = child_stdout([sys.executable, "-c", "print('answer')"], 5, ReadSlice.PROOF_AND_LOOK)
495
+ assert answer == "answer\n"
496
+ assert run.timed_out_read_count == 0
497
+
498
+
226
499
  def test_child_stdout_should_mark_the_run_when_the_deadline_cuts_a_read_short(
227
500
  all_started_children: list[RecordedChild],
228
501
  ) -> None:
@@ -234,6 +507,92 @@ def test_child_stdout_should_mark_the_run_when_the_deadline_cuts_a_read_short(
234
507
  assert child.all_wait_timeouts == [pytest.approx(0.4), KILL_GRACE_SECONDS]
235
508
  assert child.wait() != 0
236
509
  assert run.was_read_cut_short
510
+ assert run.timed_out_read_count == 1
511
+
512
+
513
+ def _lines_that_spend_a_second_each(clock: FakeClock, first_line_at: float) -> Iterator[str]:
514
+ for each_index in range(5):
515
+ clock.now = first_line_at + each_index
516
+ yield f"line {each_index}"
517
+
518
+
519
+ def test_lines_within_slice_should_stop_the_scan_at_the_slice_deadline() -> None:
520
+ clock = FakeClock()
521
+ with read_deadlines_started(clock), gate_run() as run:
522
+ first_line_at = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS - 3
523
+ all_lines = list(lines_within_slice(_lines_that_spend_a_second_each(clock, first_line_at), ReadSlice.PROOF_AND_LOOK))
524
+ assert all_lines == ["line 0", "line 1", "line 2"]
525
+ assert run.cut_scan_count == 1
526
+ assert not run.was_read_cut_short
527
+
528
+
529
+ def test_lines_within_slice_should_give_every_line_while_the_slice_has_time() -> None:
530
+ clock = FakeClock()
531
+ with read_deadlines_started(clock), gate_run() as run:
532
+ first_line_at = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS
533
+ all_lines = list(lines_within_slice(_lines_that_spend_a_second_each(clock, first_line_at), ReadSlice.FOLLOWUP))
534
+ with gate_run() as unbounded_run:
535
+ all_unbounded_lines = list(lines_within_slice(_lines_that_spend_a_second_each(clock, 0.0), ReadSlice.PROOF_AND_LOOK))
536
+ assert all_lines == [f"line {each_index}" for each_index in range(5)]
537
+ assert all_unbounded_lines == all_lines
538
+ assert run.cut_scan_count == 0
539
+ assert unbounded_run.cut_scan_count == 0
540
+
541
+
542
+ def test_lines_within_slice_should_give_no_line_once_the_deadline_passed() -> None:
543
+ clock = FakeClock()
544
+ with read_deadlines_started(clock), gate_run() as run:
545
+ clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS
546
+ assert list(lines_within_slice(iter(["line 0"]), ReadSlice.PROOF_AND_LOOK)) == []
547
+ assert list(lines_within_slice(iter([]), ReadSlice.PROOF_AND_LOOK)) == []
548
+ assert run.cut_scan_count == 1
549
+
550
+
551
+ class ClockedTranscript(io.BytesIO):
552
+ """A binary transcript whose every read spends one second on a fake clock."""
553
+
554
+ def __init__(self, content: bytes, clock: FakeClock) -> None:
555
+ super().__init__(content)
556
+ self.clock = clock
557
+ self.read_count = 0
558
+
559
+ def read(self, size: int | None = -1, /) -> bytes:
560
+ self.read_count += 1
561
+ self.clock.now += 1
562
+ return super().read(size)
563
+
564
+
565
+ def test_transcript_lines_within_slice_should_stop_inside_a_line_longer_than_the_time_left() -> None:
566
+ clock = FakeClock()
567
+ transcript = ClockedTranscript(b"old line\n" + b"x" * HUGE_LINE_BYTES, clock)
568
+ with read_deadlines_started(clock), gate_run() as run:
569
+ clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS - SECONDS_LEFT_BEFORE_HUGE_LINE
570
+ all_lines = list(transcript_lines_within_slice(transcript, "utf-8", "replace", ReadSlice.PROOF_AND_LOOK, 10))
571
+ assert all_lines == []
572
+ assert run.cut_scan_count == 1
573
+ assert transcript.read_count == SECONDS_LEFT_BEFORE_HUGE_LINE
574
+
575
+
576
+ def test_transcript_lines_within_slice_should_end_the_scan_at_a_line_over_the_cap_and_count_it_cut() -> None:
577
+ clock = FakeClock()
578
+ transcript = io.BytesIO(b"old line\n" + b"x" * HUGE_LINE_BYTES + b"\nnew line\n")
579
+ with read_deadlines_started(clock), gate_run() as run:
580
+ all_lines = list(
581
+ transcript_lines_within_slice(
582
+ transcript, "utf-8", "replace", ReadSlice.FOLLOWUP, 10, line_byte_cap=HUGE_LINE_BYTES - 1
583
+ )
584
+ )
585
+ assert all_lines == ["new line"]
586
+ assert run.cut_scan_count == 1
587
+
588
+
589
+ def test_transcript_lines_within_slice_should_give_every_line_newest_first_while_the_slice_has_time() -> None:
590
+ clock = FakeClock()
591
+ transcript = ClockedTranscript(b"one\r\ntwo\rthree\n", clock)
592
+ with read_deadlines_started(clock), gate_run() as run:
593
+ all_lines = list(transcript_lines_within_slice(transcript, "utf-8", "replace", ReadSlice.FOLLOWUP, 4))
594
+ assert all_lines == ["three", "two", "one"]
595
+ assert run.cut_scan_count == 0
237
596
 
238
597
 
239
598
  def test_answer_within_allowance_should_bound_the_whole_read() -> None:
@@ -252,6 +611,7 @@ def test_answer_within_allowance_should_bound_the_whole_read() -> None:
252
611
  release.set()
253
612
  assert answer is None
254
613
  assert run.was_read_cut_short
614
+ assert run.timed_out_read_count == 1
255
615
 
256
616
 
257
617
  def test_answer_within_allowance_should_return_the_answer_and_raise_the_read_error() -> None:
@@ -292,7 +652,7 @@ def test_registered_timeout_should_outlast_every_read_deadline_and_kill_grace()
292
652
  + FOLLOWUP_READS_SECONDS
293
653
  + KILL_GRACE_SECONDS * MOST_CUT_READS_PER_RUN
294
654
  )
295
- assert _registered_gate_timeout_seconds() >= last_read_ends_at + STARTUP_AND_SCAN_MARGIN_SECONDS
655
+ assert _registered_gate_timeout_seconds() >= last_read_ends_at + STARTUP_AND_OUTPUT_MARGIN_SECONDS
296
656
 
297
657
 
298
658
  def test_gate_run_reads_should_parse_under_the_oldest_supported_python() -> None: