claude-dev-env 8.117.3 → 8.117.5

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 (80) 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/references/advisor-tool.md +2 -0
  14. package/docs/rule-guides/features-start-with-an-eval.md +3 -3
  15. package/docs/rule-guides/proof-before-pull-request.md +2 -2
  16. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +11 -4
  17. package/hooks/blocking/build_eval_before_eval_write_gate.py +10 -4
  18. package/hooks/blocking/followup_pr_dedupe.py +381 -64
  19. package/hooks/blocking/gate_child_process.py +187 -0
  20. package/hooks/blocking/gate_run_reads.py +116 -35
  21. package/hooks/blocking/merge_review_gate.py +93 -10
  22. package/hooks/blocking/merge_review_gate_parts/create_folders.py +346 -0
  23. package/hooks/blocking/merge_review_gate_parts/create_options.py +86 -0
  24. package/hooks/blocking/merge_review_gate_parts/folder_state.py +107 -0
  25. package/hooks/blocking/merge_review_gate_parts/open_request.py +199 -64
  26. package/hooks/blocking/merge_review_gate_parts/quoted_lines.py +283 -0
  27. package/hooks/blocking/merge_review_gate_parts/wrapped_creates.py +126 -0
  28. package/hooks/blocking/pr_lifecycle_skill_gate.py +61 -18
  29. package/hooks/blocking/pre_tool_use_dispatcher.py +25 -3
  30. package/hooks/blocking/pull_request_proof.py +5 -51
  31. package/hooks/blocking/pull_request_proof_edit_gate.py +19 -5
  32. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +54 -0
  33. package/hooks/blocking/test_build_eval_before_eval_write_gate.py +64 -0
  34. package/hooks/blocking/test_followup_pr_dedupe.py +464 -1
  35. package/hooks/blocking/test_gate_child_process.py +64 -0
  36. package/hooks/blocking/test_gate_run_reads.py +364 -4
  37. package/hooks/blocking/test_merge_review_gate.py +832 -39
  38. package/hooks/blocking/test_pr_lifecycle_skill_gate.py +130 -4
  39. package/hooks/blocking/test_pre_tool_use_dispatcher.py +64 -0
  40. package/hooks/blocking/test_pull_request_proof.py +16 -0
  41. package/hooks/blocking/test_pull_request_proof_edit_gate.py +60 -0
  42. package/hooks/blocking/tests/test_create_folders.py +290 -0
  43. package/hooks/blocking/tests/test_create_options.py +34 -0
  44. package/hooks/blocking/tests/test_folder_state.py +40 -0
  45. package/hooks/blocking/tests/test_open_request.py +350 -44
  46. package/hooks/blocking/tests/test_quoted_lines.py +86 -0
  47. package/hooks/blocking/tests/test_wrapped_creates.py +42 -0
  48. package/hooks/features/bash-dispatchers.md +1 -1
  49. package/hooks/features/conduct-gates.md +7 -7
  50. package/hooks/features/session-context.md +3 -3
  51. package/hooks/features/write-edit-blocking.md +3 -3
  52. package/hooks/hooks_constants/advisor_rules_prompt_constants.py +14 -6
  53. package/hooks/hooks_constants/build_eval_before_eval_write_gate_constants.py +5 -0
  54. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +34 -1
  55. package/hooks/hooks_constants/gate_fault.py +52 -0
  56. package/hooks/hooks_constants/gate_fault_constants.py +13 -0
  57. package/hooks/hooks_constants/gate_run_reads_constants.py +9 -1
  58. package/hooks/hooks_constants/merge_review_gate_constants.py +94 -1
  59. package/hooks/hooks_constants/piped_pytest_blocker_constants.py +2 -0
  60. package/hooks/hooks_constants/pr_lifecycle_skill_gate_constants.py +6 -1
  61. package/hooks/hooks_constants/pre_tool_use_stdin.py +23 -3
  62. package/hooks/hooks_constants/shell_command_pipeline.py +68 -8
  63. package/hooks/hooks_constants/shell_command_segments.py +2 -2
  64. package/hooks/hooks_constants/shell_command_wrappers.py +3 -2
  65. package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +7 -0
  66. package/hooks/hooks_constants/test_gate_fault.py +41 -0
  67. package/hooks/hooks_constants/test_piped_pytest_blocker_constants.py +5 -0
  68. package/hooks/hooks_constants/test_pr_lifecycle_skill_gate_constants.py +10 -0
  69. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +97 -0
  70. package/hooks/hooks_constants/test_shell_command_pipeline.py +55 -0
  71. package/hooks/hooks_constants/test_shell_command_segments.py +15 -0
  72. package/hooks/hooks_constants/test_shell_command_wrappers.py +6 -0
  73. package/hooks/hooks_constants/transcript_skill_scan_constants.py +6 -0
  74. package/hooks/pyproject.toml +2 -3
  75. package/hooks/session/advisor_rules_prompt.py +53 -9
  76. package/hooks/session/test_advisor_rules_prompt.py +70 -4
  77. package/hooks/test_python_floor.py +197 -0
  78. package/hooks/test_transcript_skill_scan.py +324 -0
  79. package/hooks/transcript_skill_scan.py +233 -2
  80. package/package.json +1 -1
@@ -19,19 +19,30 @@ import advisor_rules_prompt as starter
19
19
 
20
20
  from hooks_constants.advisor_rules_prompt_constants import (
21
21
  ADVISOR_DISABLE_ENV_VAR,
22
- ADVISOR_RULES_PROMPT,
23
22
  ALL_ADVISOR_RULE_SENTENCES,
24
23
  CLAUDE_CONFIG_DIR_ENV_VAR,
25
24
  )
26
25
 
27
26
 
28
- def _run_main() -> str:
27
+ def _run_main(session_start_payload: object = "") -> str:
28
+ stdin_text = (
29
+ session_start_payload
30
+ if isinstance(session_start_payload, str)
31
+ else json.dumps(session_start_payload)
32
+ )
29
33
  captured_stdout = StringIO()
30
- with patch("sys.stdout", captured_stdout):
34
+ with patch("sys.stdin", StringIO(stdin_text)), patch("sys.stdout", captured_stdout):
31
35
  starter.main()
32
36
  return captured_stdout.getvalue()
33
37
 
34
38
 
39
+ def _session_start_payload(session_model: str | None) -> dict[str, str]:
40
+ payload = {"hook_event_name": "SessionStart", "source": "startup"}
41
+ if session_model is not None:
42
+ payload["model"] = session_model
43
+ return payload
44
+
45
+
35
46
  @pytest.fixture
36
47
  def config_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
37
48
  monkeypatch.setenv(CLAUDE_CONFIG_DIR_ENV_VAR, str(tmp_path))
@@ -49,7 +60,62 @@ class TestAdvisorRulesPrompt:
49
60
  _write_settings(config_directory, {"advisorModel": "fable"})
50
61
  hook_output = json.loads(_run_main())["hookSpecificOutput"]
51
62
  assert hook_output["hookEventName"] == "SessionStart"
52
- assert hook_output["additionalContext"] == ADVISOR_RULES_PROMPT
63
+ for each_rule_sentence in ALL_ADVISOR_RULE_SENTENCES:
64
+ assert each_rule_sentence in hook_output["additionalContext"]
65
+
66
+ def test_should_name_the_configured_advisor_model(self, config_directory: Path) -> None:
67
+ _write_settings(config_directory, {"advisorModel": "fable"})
68
+ hook_output = json.loads(_run_main())["hookSpecificOutput"]
69
+ assert "The advisor runs on `fable`." in hook_output["additionalContext"]
70
+
71
+ @pytest.mark.parametrize(
72
+ ("advisor_model", "session_model"),
73
+ [
74
+ ("fable", "claude-fable-5-2"),
75
+ ("claude-fable-5-2", "claude-fable-5-2"),
76
+ ("opus", "claude-opus-5-5[1m]"),
77
+ ("claude-opus-4-7", "claude-opus-5"),
78
+ ("claude-mythos-1", "claude-mythos-1"),
79
+ ("Custom-Advisor", "custom-advisor"),
80
+ ],
81
+ )
82
+ def test_should_emit_nothing_when_session_runs_on_the_advisor_model(
83
+ self, config_directory: Path, advisor_model: str, session_model: str
84
+ ) -> None:
85
+ _write_settings(config_directory, {"advisorModel": advisor_model})
86
+ assert _run_main(_session_start_payload(session_model)) == ""
87
+
88
+ @pytest.mark.parametrize(
89
+ ("advisor_model", "session_model"),
90
+ [
91
+ ("fable", "claude-opus-5-5"),
92
+ ("opus", "claude-sonnet-5-5"),
93
+ ("fable", "claude-mythos-1"),
94
+ ("custom-advisor", "claude-fable-5-2"),
95
+ ("custom-advisor", "claude-mythos-1"),
96
+ ],
97
+ )
98
+ def test_should_emit_rules_when_session_runs_on_another_model(
99
+ self, config_directory: Path, advisor_model: str, session_model: str
100
+ ) -> None:
101
+ _write_settings(config_directory, {"advisorModel": advisor_model})
102
+ hook_output = json.loads(_run_main(_session_start_payload(session_model)))
103
+ assert hook_output["hookSpecificOutput"]["hookEventName"] == "SessionStart"
104
+
105
+ @pytest.mark.parametrize("session_start_payload", [_session_start_payload(None), "", "{not json", "[]"])
106
+ def test_should_emit_rules_when_payload_names_no_model(
107
+ self, config_directory: Path, session_start_payload: object
108
+ ) -> None:
109
+ _write_settings(config_directory, {"advisorModel": "fable"})
110
+ hook_output = json.loads(_run_main(session_start_payload))
111
+ assert hook_output["hookSpecificOutput"]["hookEventName"] == "SessionStart"
112
+
113
+ def test_should_emit_rules_when_payload_model_is_not_a_string(
114
+ self, config_directory: Path
115
+ ) -> None:
116
+ _write_settings(config_directory, {"advisorModel": "fable"})
117
+ hook_output = json.loads(_run_main({"hook_event_name": "SessionStart", "model": 5}))
118
+ assert hook_output["hookSpecificOutput"]["hookEventName"] == "SessionStart"
53
119
 
54
120
  def test_should_emit_nothing_when_advisor_model_is_missing(
55
121
  self, config_directory: Path
@@ -0,0 +1,197 @@
1
+ """The package's Python floor holds for the hooks, their checkers, and the install prose.
2
+
3
+ The floor is the ``requires-python`` bound in the package ``pyproject.toml``. Every
4
+ other statement of it must name the same version, and every hook module must parse
5
+ under that version's grammar, so a hook that cannot start on the floor fails here.
6
+ """
7
+
8
+ import ast
9
+ import importlib.util
10
+ import os
11
+ import re
12
+ import shutil
13
+ import subprocess
14
+ import sys
15
+ import tomllib
16
+ from pathlib import Path
17
+
18
+ import pytest
19
+
20
+ HOOKS_ROOT = Path(__file__).resolve().parent
21
+ PACKAGE_ROOT = HOOKS_ROOT.parent
22
+ REPOSITORY_ROOT = PACKAGE_ROOT.parent.parent
23
+ PACKAGE_PYPROJECT = PACKAGE_ROOT / "pyproject.toml"
24
+ HOOKS_PYPROJECT = HOOKS_ROOT / "pyproject.toml"
25
+ ALL_SHARED_MYPY_PYPROJECTS = (
26
+ PACKAGE_ROOT / "_shared" / "pr-loop" / "scripts" / "pyproject.toml",
27
+ PACKAGE_ROOT / "_shared" / "process-tree" / "scripts" / "pyproject.toml",
28
+ )
29
+ README_PATH = REPOSITORY_ROOT / "README.md"
30
+ INSTALL_PLAN_PATH = PACKAGE_ROOT / "bin" / "install-plan.mjs"
31
+ PYTHON_PROBE_PATH = PACKAGE_ROOT / "bin" / "python-probe.mjs"
32
+
33
+ REQUIRES_PYTHON_PATTERN = re.compile(r"^>=\s*(\d+)\.(\d+)$")
34
+ README_FLOOR_PATTERN = re.compile(r"Python (\d+)\.(\d+)\+")
35
+ INSTALLER_FLOOR_PATTERN = re.compile(r"MINIMUM_PYTHON_VERSION = '(\d+)\.(\d+)'")
36
+ INSTALLER_MESSAGE_FLOOR_REFERENCE = "Install Python ${MINIMUM_PYTHON_VERSION}+"
37
+
38
+ TYPE_PARAMETER_SOURCE = "def first[T](all_items: list[T]) -> T:\n return all_items[0]\n"
39
+ NESTED_QUOTE_FSTRING_SOURCE = (
40
+ 'all_names = ["first", "second"]\njoined_names = f"{", ".join(all_names)}"\n'
41
+ )
42
+ NEWER_TARGET_VERSION = "py312"
43
+
44
+
45
+ def _read_toml(toml_path: Path) -> dict:
46
+ with toml_path.open("rb") as toml_file:
47
+ return tomllib.load(toml_file)
48
+
49
+
50
+ def _package_floor() -> tuple[int, int]:
51
+ requires_python = _read_toml(PACKAGE_PYPROJECT)["project"]["requires-python"]
52
+ floor_match = REQUIRES_PYTHON_PATTERN.match(requires_python.strip())
53
+ assert floor_match, f"requires-python must read '>=X.Y', found {requires_python!r}"
54
+ return int(floor_match.group(1)), int(floor_match.group(2))
55
+
56
+
57
+ def _floor_text(floor: tuple[int, int]) -> str:
58
+ return f"{floor[0]}.{floor[1]}"
59
+
60
+
61
+ def _ruff_excluded_entries() -> list[str]:
62
+ return list(_read_toml(HOOKS_PYPROJECT)["tool"]["ruff"]["extend-exclude"])
63
+
64
+
65
+ def _is_excluded(relative_path: Path, all_excluded_entries: list[str]) -> bool:
66
+ relative_text = relative_path.as_posix()
67
+ return any(
68
+ relative_text.startswith(each_entry + "/") or each_entry in relative_path.parts
69
+ for each_entry in all_excluded_entries
70
+ )
71
+
72
+
73
+ def _all_hook_modules() -> list[Path]:
74
+ all_excluded_entries = _ruff_excluded_entries()
75
+ return sorted(
76
+ each_path
77
+ for each_path in HOOKS_ROOT.rglob("*.py")
78
+ if not _is_excluded(each_path.relative_to(HOOKS_ROOT), all_excluded_entries)
79
+ )
80
+
81
+
82
+ def _parse_at_floor(source: str, source_name: str, floor: tuple[int, int]) -> None:
83
+ ast.parse(source, filename=source_name, feature_version=floor)
84
+
85
+
86
+ def _floor_parse_failures(
87
+ all_module_paths: list[Path], modules_root: Path, floor: tuple[int, int]
88
+ ) -> list[str]:
89
+ all_failures: list[str] = []
90
+ for each_path in all_module_paths:
91
+ try:
92
+ _parse_at_floor(each_path.read_text(encoding="utf-8"), str(each_path), floor)
93
+ except SyntaxError as parse_error:
94
+ relative_path = each_path.relative_to(modules_root).as_posix()
95
+ all_failures.append(f"{relative_path}:{parse_error.lineno}: {parse_error.msg}")
96
+ return all_failures
97
+
98
+
99
+ def _run_ruff_on(
100
+ module_path: Path, all_extra_arguments: list[str]
101
+ ) -> subprocess.CompletedProcess[str]:
102
+ return subprocess.run(
103
+ [
104
+ sys.executable,
105
+ "-m",
106
+ "ruff",
107
+ "check",
108
+ "--no-cache",
109
+ "--config",
110
+ str(HOOKS_PYPROJECT),
111
+ *all_extra_arguments,
112
+ str(module_path),
113
+ ],
114
+ capture_output=True,
115
+ text=True,
116
+ check=False,
117
+ )
118
+
119
+
120
+ def test_hooks_ruff_target_should_equal_the_package_floor() -> None:
121
+ floor = _package_floor()
122
+ ruff_target = _read_toml(HOOKS_PYPROJECT)["tool"]["ruff"]["target-version"]
123
+ assert ruff_target == f"py{floor[0]}{floor[1]}"
124
+
125
+
126
+ @pytest.mark.parametrize(
127
+ "mypy_pyproject",
128
+ [HOOKS_PYPROJECT, *ALL_SHARED_MYPY_PYPROJECTS],
129
+ ids=lambda each_path: each_path.parent.relative_to(PACKAGE_ROOT).as_posix(),
130
+ )
131
+ def test_mypy_python_version_should_equal_the_package_floor(mypy_pyproject: Path) -> None:
132
+ mypy_version = _read_toml(mypy_pyproject)["tool"]["mypy"]["python_version"]
133
+ assert mypy_version == _floor_text(_package_floor())
134
+
135
+
136
+ def test_every_hook_module_should_parse_at_the_package_floor() -> None:
137
+ all_module_paths = _all_hook_modules()
138
+ assert all_module_paths
139
+ all_failures = _floor_parse_failures(all_module_paths, HOOKS_ROOT, _package_floor())
140
+ assert not all_failures, "\n".join(all_failures)
141
+
142
+
143
+ def test_floor_parse_should_reject_a_type_parameter_list() -> None:
144
+ with pytest.raises(SyntaxError):
145
+ _parse_at_floor(TYPE_PARAMETER_SOURCE, "planted_type_parameters.py", _package_floor())
146
+
147
+
148
+ def test_floor_parse_should_name_the_file_and_line_of_a_newer_module(tmp_path: Path) -> None:
149
+ planted_module = tmp_path / "planted_type_parameters.py"
150
+ planted_module.write_text("import os\n\n" + TYPE_PARAMETER_SOURCE, encoding="utf-8")
151
+ all_failures = _floor_parse_failures([planted_module], tmp_path, _package_floor())
152
+ assert len(all_failures) == 1
153
+ assert all_failures[0].startswith("planted_type_parameters.py:3: ")
154
+
155
+
156
+ def test_hooks_ruff_config_should_reject_a_nested_quote_fstring(tmp_path: Path) -> None:
157
+ if importlib.util.find_spec("ruff") is None:
158
+ pytest.skip("the ruff module is not installed")
159
+ planted_module = tmp_path / "planted_nested_quote_fstring.py"
160
+ planted_module.write_text(NESTED_QUOTE_FSTRING_SOURCE, encoding="utf-8")
161
+ newer_target_run = _run_ruff_on(planted_module, ["--target-version", NEWER_TARGET_VERSION])
162
+ assert newer_target_run.returncode == 0, newer_target_run.stdout + newer_target_run.stderr
163
+ floor_run = _run_ruff_on(planted_module, [])
164
+ assert floor_run.returncode != 0
165
+ assert "syntax was added in Python 3.12" in floor_run.stdout + floor_run.stderr
166
+
167
+
168
+ def test_hooks_should_compile_under_the_floor_interpreter(tmp_path: Path) -> None:
169
+ floor_interpreter = shutil.which(f"python{_floor_text(_package_floor())}")
170
+ if floor_interpreter is None:
171
+ pytest.skip("no interpreter for the floor version on PATH")
172
+ compile_environment = {**os.environ, "PYTHONPYCACHEPREFIX": str(tmp_path)}
173
+ compile_run = subprocess.run(
174
+ [floor_interpreter, "-m", "compileall", "-q", str(HOOKS_ROOT)],
175
+ capture_output=True,
176
+ text=True,
177
+ check=False,
178
+ env=compile_environment,
179
+ )
180
+ assert compile_run.returncode == 0, compile_run.stdout + compile_run.stderr
181
+
182
+
183
+ def test_readme_should_name_the_package_floor() -> None:
184
+ all_readme_floors = README_FLOOR_PATTERN.findall(README_PATH.read_text(encoding="utf-8"))
185
+ assert all_readme_floors
186
+ floor = _package_floor()
187
+ assert {(int(major), int(minor)) for major, minor in all_readme_floors} == {floor}
188
+
189
+
190
+ def test_installer_should_name_the_package_floor() -> None:
191
+ python_probe_source = PYTHON_PROBE_PATH.read_text(encoding="utf-8")
192
+ installer_floor_match = INSTALLER_FLOOR_PATTERN.search(python_probe_source)
193
+ assert installer_floor_match, "python-probe.mjs must export MINIMUM_PYTHON_VERSION"
194
+ install_plan_source = INSTALL_PLAN_PATH.read_text(encoding="utf-8")
195
+ installer_floor = (int(installer_floor_match.group(1)), int(installer_floor_match.group(2)))
196
+ assert installer_floor == _package_floor()
197
+ assert INSTALLER_MESSAGE_FLOOR_REFERENCE in install_plan_source
@@ -1,20 +1,29 @@
1
1
  """Tests for skill invocation state across transcript compactions."""
2
2
 
3
+ import errno
4
+ import io
3
5
  import json
6
+ import random
4
7
  import sys
5
8
  from pathlib import Path
9
+ from typing import BinaryIO
10
+
11
+ import pytest
6
12
 
7
13
  HOOKS_DIRECTORY = Path(__file__).resolve().parent
8
14
  if str(HOOKS_DIRECTORY) not in sys.path:
9
15
  sys.path.insert(0, str(HOOKS_DIRECTORY))
10
16
 
11
17
  from transcript_skill_scan import (
18
+ ScanCaps,
12
19
  TranscriptToolCall,
13
20
  invokes_skill_with_argument,
14
21
  is_skill_loaded_after_last_compaction,
22
+ is_skill_loaded_after_last_compaction_newest_first,
15
23
  is_skill_loaded_since_last_prompt,
16
24
  skill_invocation_status,
17
25
  tool_calls_since_last_prompt,
26
+ transcript_lines_newest_first,
18
27
  )
19
28
 
20
29
 
@@ -221,3 +230,318 @@ def should_list_each_tool_call_since_the_last_prompt_with_its_outcome() -> None:
221
230
  TranscriptToolCall("Edit", {}, False),
222
231
  TranscriptToolCall("Write", {}, False),
223
232
  ]
233
+
234
+
235
+ NEWEST_FIRST_SKILL_NAMES = ("pr-lifecycle",)
236
+ NEWEST_FIRST_MARKERS = ("<command-name>/pr-lifecycle</command-name>",)
237
+ ALL_GENERATED_LINE_KINDS = (
238
+ _skill_entry("pr-lifecycle"),
239
+ _skill_entry("plugin:pr-lifecycle"),
240
+ json.dumps({"type": "system", "subtype": "compact_boundary"}),
241
+ json.dumps(
242
+ {
243
+ "type": "assistant",
244
+ "subtype": "compact_boundary",
245
+ "message": {"content": [{"type": "tool_use", "name": "Skill", "input": {"skill": "pr-lifecycle"}}]},
246
+ }
247
+ ),
248
+ json.dumps({"type": "user", "message": {"content": "<command-name>/pr-lifecycle</command-name>"}}),
249
+ json.dumps({"type": "user", "message": {"content": "Please run pr-lifecycle later."}}),
250
+ json.dumps({"type": "user", "message": {"content": [{"type": "tool_result", "content": "pr-lifecycle"}]}}),
251
+ _skill_entry("other"),
252
+ json.dumps({"type": "user", "message": {"content": "Unrelated."}}),
253
+ "pr-lifecycle {",
254
+ )
255
+ EXHAUSTIVE_SEQUENCE_LENGTH = 4
256
+ GENERATED_SEQUENCE_COUNT = 3000
257
+ LONGEST_GENERATED_SEQUENCE = 30
258
+ GENERATED_SEQUENCE_SEED = 2338
259
+ ALL_TINY_BLOCK_SIZES = (1, 2, 3, 7, 64, 1 << 20)
260
+ ALL_READER_CONTENTS = (
261
+ b"",
262
+ b"\n",
263
+ b"one",
264
+ b"one\n",
265
+ b"one\ntwo",
266
+ b"one\ntwo\n",
267
+ b"\n\nmiddle\n\n",
268
+ b"short\n" + b"x" * 200 + b"\nend",
269
+ "café ☃ \U0001f600\nnext é\n".encode("utf-8"),
270
+ b"bad \xff\xfe byte\n\xc3\nhalf\xe2\x98\n",
271
+ b"carriage\r\nreturn\rinside\n",
272
+ b"lone\rcarriage\rreturns\r",
273
+ b"\r",
274
+ b"\r\r\n\n\r",
275
+ b"ab\r\ncd\r\nef\r\n",
276
+ b"x\ry\r\nz",
277
+ b"abcdef\r\nghijk\rlmnopq\n",
278
+ )
279
+ ALL_LINE_ENDINGS = ("\n", "\r\n", "\r")
280
+ ALL_SEQUENCE_BLOCK_SIZES = (1, 2, 3, 7, 64)
281
+ MIXED_ENDING_SEQUENCE_COUNT = 400
282
+ STOP_AFTER_BLOCK_COUNT = 2
283
+
284
+
285
+ def _newest_first_load(all_lines: list[str]) -> bool:
286
+ return is_skill_loaded_after_last_compaction_newest_first(
287
+ list(reversed(all_lines)), NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS
288
+ )
289
+
290
+
291
+ def _oldest_first_load(all_lines: list[str]) -> bool:
292
+ return is_skill_loaded_after_last_compaction(all_lines, NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS)
293
+
294
+
295
+ def test_newest_first_load_should_take_the_newest_load_or_compaction() -> None:
296
+ load, plugin_load, compaction, compaction_that_also_loads, slash_command = ALL_GENERATED_LINE_KINDS[:5]
297
+ unrelated = ALL_GENERATED_LINE_KINDS[-2]
298
+ assert _newest_first_load([compaction, load, unrelated])
299
+ assert _newest_first_load([load, compaction, slash_command])
300
+ assert not _newest_first_load([load, compaction])
301
+ assert not _newest_first_load([plugin_load, compaction_that_also_loads])
302
+ assert not _newest_first_load([unrelated])
303
+ assert not _newest_first_load([])
304
+
305
+
306
+ def test_newest_first_load_should_match_the_oldest_first_answer_on_every_short_sequence() -> None:
307
+ all_sequences: list[list[str]] = [[]]
308
+ for _ in range(EXHAUSTIVE_SEQUENCE_LENGTH):
309
+ all_sequences = [
310
+ each_sequence + [each_kind] for each_sequence in all_sequences for each_kind in ALL_GENERATED_LINE_KINDS
311
+ ]
312
+ for each_sequence in all_sequences:
313
+ assert _newest_first_load(each_sequence) == _oldest_first_load(each_sequence), each_sequence
314
+
315
+
316
+ def test_newest_first_load_should_match_the_oldest_first_answer_on_generated_long_sequences() -> None:
317
+ generator = random.Random(GENERATED_SEQUENCE_SEED)
318
+ all_answers: set[bool] = set()
319
+ for _ in range(GENERATED_SEQUENCE_COUNT):
320
+ all_lines = [
321
+ generator.choice(ALL_GENERATED_LINE_KINDS) for _ in range(generator.randint(0, LONGEST_GENERATED_SEQUENCE))
322
+ ]
323
+ oldest_first_answer = _oldest_first_load(all_lines)
324
+ assert _newest_first_load(all_lines) == oldest_first_answer, all_lines
325
+ all_answers.add(oldest_first_answer)
326
+ assert all_answers == {True, False}
327
+
328
+
329
+ def _lines_read_oldest_first(path: Path) -> list[str]:
330
+ with open(path, encoding="utf-8", errors="replace") as transcript:
331
+ return [each_line.removesuffix("\n") for each_line in transcript]
332
+
333
+
334
+ def _lines_read_newest_first(path: Path, block_size: int) -> list[str]:
335
+ with open(path, "rb") as transcript:
336
+ return list(transcript_lines_newest_first(transcript, "utf-8", "replace", block_size))
337
+
338
+
339
+ def test_lines_newest_first_should_return_each_line_of_the_file_newest_first(tmp_path: Path) -> None:
340
+ transcript_path = tmp_path / "session.jsonl"
341
+ for each_content in ALL_READER_CONTENTS:
342
+ transcript_path.write_bytes(each_content)
343
+ expected_lines = list(reversed(_lines_read_oldest_first(transcript_path)))
344
+ all_answers = {
345
+ each_block_size: _lines_read_newest_first(transcript_path, each_block_size)
346
+ for each_block_size in ALL_TINY_BLOCK_SIZES
347
+ }
348
+ assert all_answers == dict.fromkeys(ALL_TINY_BLOCK_SIZES, expected_lines), each_content
349
+
350
+
351
+ def test_lines_newest_first_should_split_a_carriage_return_pair_across_a_block_edge(tmp_path: Path) -> None:
352
+ transcript_path = tmp_path / "session.jsonl"
353
+ transcript_path.write_bytes(b"ab\r\ncd\n")
354
+ assert _lines_read_newest_first(transcript_path, 4) == ["cd", "ab"]
355
+ transcript_path.write_bytes(b"ab\rcd\n")
356
+ assert _lines_read_newest_first(transcript_path, 3) == ["cd", "ab"]
357
+ transcript_path.write_bytes(b"ab\r\rcd")
358
+ assert _lines_read_newest_first(transcript_path, 2) == ["cd", "", "ab"]
359
+
360
+
361
+ def _old_gate_answer(path: Path) -> bool:
362
+ return _oldest_first_load(_lines_read_oldest_first(path))
363
+
364
+
365
+ def _new_gate_answer(path: Path, block_size: int) -> bool:
366
+ with open(path, "rb") as transcript:
367
+ all_lines = transcript_lines_newest_first(transcript, "utf-8", "replace", block_size)
368
+ return is_skill_loaded_after_last_compaction_newest_first(all_lines, NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS)
369
+
370
+
371
+ def test_newest_first_scan_should_answer_as_the_old_gate_on_a_lone_carriage_return(tmp_path: Path) -> None:
372
+ load, _, compaction = ALL_GENERATED_LINE_KINDS[:3]
373
+ transcript_path = tmp_path / "session.jsonl"
374
+ transcript_path.write_bytes(f'{load}\n{compaction}\r{{"x":1}}\n'.encode())
375
+ assert not _old_gate_answer(transcript_path)
376
+ assert [_new_gate_answer(transcript_path, each_size) for each_size in ALL_SEQUENCE_BLOCK_SIZES] == [False] * 5
377
+
378
+
379
+ def _mixed_ending_transcript(generator: random.Random) -> bytes:
380
+ all_lines = [generator.choice(ALL_GENERATED_LINE_KINDS) for _ in range(generator.randint(0, 12))]
381
+ return "".join(each_line + generator.choice(ALL_LINE_ENDINGS) for each_line in all_lines).encode("utf-8")
382
+
383
+
384
+ def test_newest_first_scan_should_answer_as_the_old_gate_with_every_line_ending(tmp_path: Path) -> None:
385
+ generator = random.Random(GENERATED_SEQUENCE_SEED)
386
+ transcript_path = tmp_path / "session.jsonl"
387
+ all_answers: set[bool] = set()
388
+ for _ in range(MIXED_ENDING_SEQUENCE_COUNT):
389
+ transcript_path.write_bytes(_mixed_ending_transcript(generator))
390
+ old_answer = _old_gate_answer(transcript_path)
391
+ all_new_answers = {_new_gate_answer(transcript_path, each_size) for each_size in ALL_SEQUENCE_BLOCK_SIZES}
392
+ assert all_new_answers == {old_answer}, transcript_path.read_bytes()
393
+ all_answers.add(old_answer)
394
+ assert all_answers == {True, False}
395
+
396
+
397
+ def test_lines_newest_first_should_stop_between_blocks_once_told_to_stop(tmp_path: Path) -> None:
398
+ transcript_path = tmp_path / "session.jsonl"
399
+ transcript_path.write_bytes(b"a\nb\nc\n" + b"x" * 40)
400
+ all_asks: list[int] = []
401
+
402
+ def keeps_reading() -> bool:
403
+ all_asks.append(len(all_asks))
404
+ return len(all_asks) <= STOP_AFTER_BLOCK_COUNT
405
+
406
+ with open(transcript_path, "rb") as transcript:
407
+ assert list(transcript_lines_newest_first(transcript, "utf-8", "replace", 4, keeps_reading)) == []
408
+ assert transcript.tell() == transcript_path.stat().st_size - 4
409
+ transcript_path.write_bytes(b"a\nb\nc\n")
410
+ all_asks.clear()
411
+ with open(transcript_path, "rb") as transcript:
412
+ assert list(transcript_lines_newest_first(transcript, "utf-8", "replace", 2, keeps_reading)) == ["c"]
413
+ assert len(all_asks) == STOP_AFTER_BLOCK_COUNT + 1
414
+
415
+
416
+ def test_lines_newest_first_should_give_a_recent_load_before_reading_the_whole_file(tmp_path: Path) -> None:
417
+ transcript_path = tmp_path / "session.jsonl"
418
+ all_old_lines = [json.dumps({"type": "user", "message": {"content": f"line {each_index}"}}) for each_index in range(2000)]
419
+ transcript_path.write_text("\n".join([*all_old_lines, _skill_entry("pr-lifecycle")]) + "\n", encoding="utf-8")
420
+ with open(transcript_path, "rb") as transcript:
421
+ all_lines = transcript_lines_newest_first(transcript, "utf-8", "replace", 256)
422
+ assert is_skill_loaded_after_last_compaction_newest_first(all_lines, NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS)
423
+ assert transcript.tell() > transcript_path.stat().st_size - 1024
424
+
425
+
426
+ SMALL_LINE_BYTE_CAP = 256
427
+ SMALL_COPY_BYTE_CAP = 4096
428
+ ALL_CAP_BLOCK_SIZES = (1, 5, 16, 1000)
429
+ ALL_COPY_BLOCK_SIZES = (1, 7, 64, 4096, 5000)
430
+
431
+
432
+ class UnseekableTranscript(io.BytesIO):
433
+ def seekable(self) -> bool:
434
+ return False
435
+
436
+ def seek(self, *all_arguments: int) -> int:
437
+ raise io.UnsupportedOperation("seek")
438
+
439
+
440
+ class EndSeekRefusedTranscript(io.BytesIO):
441
+ def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
442
+ if whence == io.SEEK_END:
443
+ raise OSError(errno.EINVAL, "Invalid argument")
444
+ return super().seek(offset, whence)
445
+
446
+
447
+ def _capped_lines(
448
+ transcript: BinaryIO, block_size: int, all_cuts: list[int], copy_byte_cap: int = SMALL_COPY_BYTE_CAP
449
+ ) -> list[str]:
450
+ caps = ScanCaps(SMALL_LINE_BYTE_CAP, copy_byte_cap, lambda: all_cuts.append(1))
451
+ return list(transcript_lines_newest_first(transcript, "utf-8", "replace", block_size, caps=caps))
452
+
453
+
454
+ def _capped_load(content: bytes, block_size: int, all_cuts: list[int], is_seekable: bool = True) -> bool:
455
+ transcript = io.BytesIO(content) if is_seekable else UnseekableTranscript(content)
456
+ all_lines = _capped_lines(transcript, block_size, all_cuts)
457
+ return is_skill_loaded_after_last_compaction_newest_first(all_lines, NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS)
458
+
459
+
460
+ def _load_line() -> bytes:
461
+ return _skill_entry("pr-lifecycle").encode()
462
+
463
+
464
+ def test_load_older_than_a_line_over_the_cap_should_not_be_found_and_should_count_a_cut() -> None:
465
+ content = _load_line() + b"\n" + b"y" * (SMALL_LINE_BYTE_CAP + 1) + b"\n"
466
+ for each_block_size in ALL_CAP_BLOCK_SIZES:
467
+ all_cuts: list[int] = []
468
+ assert not _capped_load(content, each_block_size, all_cuts)
469
+ assert all_cuts == [1]
470
+
471
+
472
+ def test_load_newer_than_a_line_over_the_cap_should_be_found() -> None:
473
+ content = b"x\n" + b"y" * (SMALL_LINE_BYTE_CAP * 3) + b"\n" + _load_line() + b"\n"
474
+ for each_block_size in ALL_CAP_BLOCK_SIZES:
475
+ assert _capped_load(content, each_block_size, [])
476
+
477
+
478
+ def test_line_at_exactly_the_cap_should_be_read() -> None:
479
+ content = _load_line() + b"\n" + b"y" * SMALL_LINE_BYTE_CAP + b"\r\n" + b"z" * SMALL_LINE_BYTE_CAP
480
+ for each_block_size in ALL_CAP_BLOCK_SIZES:
481
+ all_cuts: list[int] = []
482
+ assert _capped_load(content, each_block_size, all_cuts)
483
+ assert all_cuts == []
484
+
485
+
486
+ def test_unseekable_transcript_should_be_read_newest_first_after_a_forward_copy() -> None:
487
+ content = b"a\nb\r\nc\rd"
488
+ all_lines = transcript_lines_newest_first(UnseekableTranscript(content), "utf-8", "replace", 3)
489
+ assert list(all_lines) == ["d", "c", "b", "a"]
490
+ assert _capped_load(b"x\n" + _load_line() + b"\n", 7, [], is_seekable=False)
491
+
492
+
493
+ def test_unseekable_transcript_should_give_no_lines_once_the_forward_copy_is_stopped() -> None:
494
+ all_asks: list[int] = []
495
+
496
+ def keeps_reading() -> bool:
497
+ all_asks.append(len(all_asks))
498
+ return len(all_asks) <= STOP_AFTER_BLOCK_COUNT
499
+
500
+ transcript = UnseekableTranscript(_load_line() + b"\n" + b"x" * 40)
501
+ assert list(transcript_lines_newest_first(transcript, "utf-8", "replace", 4, keeps_reading)) == []
502
+
503
+
504
+ def test_unseekable_transcript_should_stop_the_copy_at_a_line_over_the_cap() -> None:
505
+ all_cuts: list[int] = []
506
+ content = b"y" * (SMALL_LINE_BYTE_CAP * 4) + b"\n" + _load_line() + b"\n"
507
+ assert not _capped_load(content, 16, all_cuts, is_seekable=False)
508
+ assert all_cuts == [1]
509
+
510
+
511
+ def _copy_sized_stream(total_bytes: int) -> bytes:
512
+ load_part = _load_line() + b"\n"
513
+ filler_line = b"f" * 99 + b"\n"
514
+ filler = filler_line * ((total_bytes - len(load_part)) // len(filler_line) + 1)
515
+ return (load_part + filler)[:total_bytes]
516
+
517
+
518
+ def test_transcript_whose_end_seek_fails_should_be_read_newest_first_after_a_forward_copy() -> None:
519
+ content = b"x\n" + _load_line() + b"\nnewest"
520
+ all_lines = _capped_lines(EndSeekRefusedTranscript(content), 7, [])
521
+ assert all_lines[0] == "newest"
522
+ assert is_skill_loaded_after_last_compaction_newest_first(all_lines, NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS)
523
+
524
+
525
+ def test_procfs_transcript_should_be_read_after_a_forward_copy() -> None:
526
+ if sys.platform != "linux":
527
+ pytest.skip("procfs is a Linux file system")
528
+ with open("/proc/version", "rb") as transcript:
529
+ all_lines = list(transcript_lines_newest_first(transcript, "utf-8", "replace"))
530
+ assert all_lines[0].startswith("Linux version")
531
+
532
+
533
+ def test_stream_one_byte_over_the_copy_cap_should_give_no_lines_and_count_a_cut() -> None:
534
+ content = _copy_sized_stream(SMALL_COPY_BYTE_CAP + 1)
535
+ for each_block_size in ALL_COPY_BLOCK_SIZES:
536
+ all_cuts: list[int] = []
537
+ assert _capped_lines(UnseekableTranscript(content), each_block_size, all_cuts) == []
538
+ assert all_cuts == [1]
539
+
540
+
541
+ def test_stream_at_exactly_the_copy_cap_should_be_read() -> None:
542
+ content = _copy_sized_stream(SMALL_COPY_BYTE_CAP)
543
+ for each_block_size in ALL_COPY_BLOCK_SIZES:
544
+ all_cuts: list[int] = []
545
+ all_lines = _capped_lines(EndSeekRefusedTranscript(content), each_block_size, all_cuts)
546
+ assert all_cuts == []
547
+ assert is_skill_loaded_after_last_compaction_newest_first(all_lines, NEWEST_FIRST_SKILL_NAMES, NEWEST_FIRST_MARKERS)