claude-dev-env 8.123.0 → 8.124.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.
Files changed (31) hide show
  1. package/bin/ever-registered-hooks.mjs +2 -0
  2. package/bin/ever-registered-hooks.test.mjs +5 -0
  3. package/docs/templates/artifact-page/README.md +14 -1
  4. package/docs/templates/artifact-page/review-rounds.md +1 -1
  5. package/docs/templates/artifact-page/template.html +26 -18
  6. package/docs/templates/html-plan/README.md +4 -0
  7. package/hooks/blocking/artifact_current_page_fixtures/piled_review_page.html +399 -0
  8. package/hooks/blocking/artifact_current_page_fixtures/rebuilt_review_page.html +403 -0
  9. package/hooks/blocking/artifact_current_page_gate.py +418 -0
  10. package/hooks/blocking/artifact_repeated_item_render.cjs +112 -0
  11. package/hooks/blocking/open_tasks_stop_gate.py +12 -1
  12. package/hooks/blocking/open_tasks_transcript.py +30 -0
  13. package/hooks/blocking/open_tasks_transcript_records.py +57 -1
  14. package/hooks/blocking/test_artifact_current_page_gate.py +415 -0
  15. package/hooks/blocking/test_open_tasks_stop_gate.py +64 -0
  16. package/hooks/blocking/test_open_tasks_transcript.py +34 -0
  17. package/hooks/blocking/test_open_tasks_transcript_records.py +34 -0
  18. package/hooks/features/conduct-gates.md +6 -0
  19. package/hooks/hooks.json +15 -0
  20. package/hooks/hooks_constants/artifact_current_page_gate_constants.py +91 -0
  21. package/hooks/hooks_constants/artifact_dark_mode_gate_constants.py +3 -3
  22. package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +8 -0
  23. package/hooks/hooks_constants/test_artifact_current_page_gate_constants.py +53 -0
  24. package/hooks/hooks_constants/test_artifact_dark_mode_gate_constants.py +6 -0
  25. package/hooks/session/artifact_link_record.py +109 -0
  26. package/hooks/session/test_artifact_link_record.py +101 -0
  27. package/hooks/test_context_budget_hooks.py +2 -0
  28. package/package.json +1 -1
  29. package/scripts/policy_lint/adapter_configuration.py +6 -0
  30. package/scripts/policy_lint/config/constants.py +1 -0
  31. package/scripts/tests/test_adapter_configuration.py +8 -0
@@ -1,7 +1,8 @@
1
1
  """Read a transcript file into records, and read what one record holds.
2
2
 
3
3
  One record can hold the prompt text that starts a turn, a tool call that is work,
4
- a read, or a question card, or the files a ``SendUserFile`` call sends.
4
+ a read, or a question card, the files a ``SendUserFile`` call sends, or the
5
+ names of the tools a Claude Code session lists.
5
6
  ``open_tasks_transcript`` folds these readers over every record. Its module
6
7
  docstring lists which records start a turn and which tool calls count as work.
7
8
  """
@@ -15,8 +16,15 @@ from pathlib import Path
15
16
  from hooks_constants.send_user_file_worktree_root_gate_constants import SEND_USER_FILE_TOOL_NAME
16
17
  from hooks_constants.setup_project_paths_constants import DECODE_ERRORS_POLICY
17
18
  from hooks_constants.open_tasks_stop_gate_constants import (
19
+ ALL_DEFERRED_TOOLS_DELTA_NAME_KEYS,
18
20
  ALL_SKIPPED_PROMPT_FLAGS,
19
21
  ALL_CODEX_TOOL_CALL_TYPES,
22
+ ATTACHMENT_KEY,
23
+ DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE,
24
+ DEFERRED_TOOLS_RECORD_ATTACHMENT_TYPE,
25
+ DEFERRED_TOOLS_RECORD_ENTRIES_KEY,
26
+ PROMPT_SNAPSHOT_ATTACHMENT_TYPE,
27
+ PROMPT_SNAPSHOT_TOOLS_KEY,
20
28
  ALL_NON_WORK_TOOL_NAMES,
21
29
  ALL_QUESTION_TOOL_NAMES,
22
30
  ALL_READ_TOOL_NAMES,
@@ -204,6 +212,54 @@ def send_call_file_paths(tool_name: str, all_input_fields: dict[str, object]) ->
204
212
  return [each_path for each_path in all_sent_files if isinstance(each_path, str)]
205
213
 
206
214
 
215
+ def _entry_names(all_entries: object) -> list[str]:
216
+ if not isinstance(all_entries, list):
217
+ return []
218
+ return [
219
+ each_entry[TOOL_NAME_KEY]
220
+ for each_entry in all_entries
221
+ if isinstance(each_entry, dict) and isinstance(each_entry.get(TOOL_NAME_KEY), str)
222
+ ]
223
+
224
+
225
+ def listed_tool_names(all_record_fields: dict[str, object]) -> tuple[str, list[str]] | None:
226
+ """Return the attachment type and tool names of a Claude Code record that lists the session's tools.
227
+
228
+ ::
229
+
230
+ prompt_snapshot with a tools list -> ("prompt_snapshot", the inline tool names)
231
+ deferred_tools_delta -> ("deferred_tools_delta", the added, readded,
232
+ surfaced, and loaded names)
233
+ deferred_tools_record -> ("deferred_tools_record", the names ToolSearch loaded)
234
+ prompt_snapshot with no tools list -> None
235
+ any other record -> None
236
+
237
+ Names come only from these fields, so a tool name inside prose never counts.
238
+ """
239
+ attachment_body = all_record_fields.get(ATTACHMENT_KEY)
240
+ if not isinstance(attachment_body, dict):
241
+ return None
242
+ attachment_type = attachment_body.get(RECORD_TYPE_KEY)
243
+ if attachment_type == PROMPT_SNAPSHOT_ATTACHMENT_TYPE:
244
+ all_inline_tools = attachment_body.get(PROMPT_SNAPSHOT_TOOLS_KEY)
245
+ if not isinstance(all_inline_tools, list):
246
+ return None
247
+ return attachment_type, _entry_names(all_inline_tools)
248
+ if attachment_type == DEFERRED_TOOLS_RECORD_ATTACHMENT_TYPE:
249
+ return attachment_type, _entry_names(attachment_body.get(DEFERRED_TOOLS_RECORD_ENTRIES_KEY))
250
+ if attachment_type == DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE:
251
+ all_delta_names = [
252
+ each_name
253
+ for each_key in ALL_DEFERRED_TOOLS_DELTA_NAME_KEYS
254
+ for each_delta_value in [attachment_body.get(each_key)]
255
+ if isinstance(each_delta_value, list)
256
+ for each_name in each_delta_value
257
+ if isinstance(each_name, str)
258
+ ]
259
+ return attachment_type, all_delta_names
260
+ return None
261
+
262
+
207
263
  def is_assistant_record(all_record_fields: dict[str, object]) -> bool:
208
264
  """Return True for a record the assistant wrote, by its type, its role, or its message role."""
209
265
  message_body = all_record_fields.get(MESSAGE_KEY)
@@ -0,0 +1,415 @@
1
+ """Behavior tests for the artifact current page publish gate."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import re
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ from collections import Counter
11
+ from pathlib import Path
12
+
13
+ import pytest
14
+
15
+ sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
16
+
17
+ from blocking.artifact_current_page_gate import (
18
+ format_repeat_reason,
19
+ full_rule_pointer,
20
+ is_artifact_link,
21
+ link_record_path,
22
+ page_source_after_edits,
23
+ page_title,
24
+ recorded_page_link,
25
+ repeated_items,
26
+ )
27
+ from hooks_constants.artifact_current_page_gate_constants import REPUBLISH_NOTE
28
+ from hooks_constants.artifact_dark_mode_gate_constants import ALLOW_EXIT_CODE, BLOCK_EXIT_CODE
29
+
30
+ GATE_SCRIPT_PATH = Path(__file__).resolve().parent / "artifact_current_page_gate.py"
31
+ FIXTURES_DIRECTORY = Path(__file__).resolve().parent / "artifact_current_page_fixtures"
32
+ PILED_PAGE_PATH = FIXTURES_DIRECTORY / "piled_review_page.html"
33
+ REBUILT_PAGE_PATH = FIXTURES_DIRECTORY / "rebuilt_review_page.html"
34
+ PAGE_LINK = "https://claude.ai/artifact/SamplePageLink01"
35
+ ITEM_IMAGE_STYLE = "<style>img{width:200px;height:300px}</style>"
36
+ PAGE_WITH_ONE_SCREEN = (
37
+ "<title>Sample Run</title>" + ITEM_IMAGE_STYLE + '<img src="screens/home.png" alt="Home">'
38
+ )
39
+ SECOND_DRAW_OF_THE_SCREEN = '<h2>Warnings</h2><img src="screens/home.png" alt="Home again">'
40
+
41
+
42
+ def _renderer_available() -> bool:
43
+ if shutil.which("node") is None:
44
+ return False
45
+ all_repeats, _ = repeated_items("<p>probe</p>")
46
+ return all_repeats is not None
47
+
48
+
49
+ needs_renderer = pytest.mark.skipif(
50
+ not _renderer_available(), reason="node Playwright with Chromium is not installed"
51
+ )
52
+
53
+
54
+ def _run_gate(tool_input: dict[str, object]) -> subprocess.CompletedProcess[str]:
55
+ return subprocess.run(
56
+ [sys.executable, str(GATE_SCRIPT_PATH)],
57
+ input=json.dumps({"tool_name": "Artifact", "tool_input": tool_input}),
58
+ capture_output=True,
59
+ text=True,
60
+ check=False,
61
+ )
62
+
63
+
64
+ def _figure_counts_in_data_block(page_path: Path) -> tuple[int, int]:
65
+ data_block = re.search(
66
+ r'<script type="application/json" id="data">(.*?)</script>',
67
+ page_path.read_text(encoding="utf-8"),
68
+ re.DOTALL,
69
+ )
70
+ assert data_block is not None
71
+ page_data = json.loads(data_block.group(1))
72
+ all_screen_lists = [each_group["screens"] for each_group in page_data.get("groups", [])]
73
+ if isinstance(page_data.get("warnings"), list):
74
+ all_screen_lists += [each_warning["screens"] for each_warning in page_data["warnings"]]
75
+ all_screen_lists += [each_section["screens"] for each_section in page_data.get("sections", [])]
76
+ all_screen_lists.append(page_data.get("pick", {}).get("strips", []))
77
+ all_drawn_names = [
78
+ each_screen[0] for each_screen_list in all_screen_lists for each_screen in each_screen_list
79
+ ]
80
+ return len(all_drawn_names), len(Counter(all_drawn_names))
81
+
82
+
83
+ def test_should_keep_the_incident_counts_in_the_reduced_fixtures() -> None:
84
+ assert _figure_counts_in_data_block(PILED_PAGE_PATH) == (33, 20)
85
+ assert _figure_counts_in_data_block(REBUILT_PAGE_PATH) == (20, 20)
86
+
87
+
88
+ def test_should_apply_an_edit_whose_old_text_occurs_once() -> None:
89
+ assert page_source_after_edits("<p>old</p>", [{"old_str": "old", "new_str": "new"}]) == (
90
+ "<p>new</p>"
91
+ )
92
+
93
+
94
+ def test_should_leave_the_page_alone_for_an_ambiguous_or_malformed_edit() -> None:
95
+ assert page_source_after_edits("<p>a a</p>", [{"old_str": "a", "new_str": "b"}]) == "<p>a a</p>"
96
+ assert page_source_after_edits("<p>a</p>", [{"old_str": "a"}, "x"]) == "<p>a</p>"
97
+ assert page_source_after_edits("<p>a</p>", None) == "<p>a</p>"
98
+
99
+
100
+ def test_should_apply_no_edit_when_one_edit_of_the_call_misses() -> None:
101
+ all_edits = [{"old_str": "a", "new_str": "x"}, {"old_str": "missing", "new_str": "y"}]
102
+ assert page_source_after_edits("<p>a b</p>", all_edits) == "<p>a b</p>"
103
+
104
+
105
+ def test_should_read_the_page_title() -> None:
106
+ assert page_title("<title> Sample Run </title><p>x</p>") == "Sample Run"
107
+ assert page_title("<p>no title</p>") == ""
108
+
109
+
110
+ def test_should_name_the_record_file_after_the_page_file() -> None:
111
+ assert link_record_path(Path("/work/first-look.html")) == Path(
112
+ "/work/first-look.html.artifact-link.json"
113
+ )
114
+
115
+
116
+ def test_should_find_no_link_for_a_page_with_no_record(tmp_path: Path) -> None:
117
+ assert recorded_page_link(tmp_path / "page.html", PAGE_WITH_ONE_SCREEN) == ""
118
+
119
+
120
+ def test_should_read_the_link_from_the_meta_tag_in_the_page(tmp_path: Path) -> None:
121
+ page_source = f'<meta name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN
122
+ assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
123
+
124
+
125
+ def test_should_read_the_meta_tag_with_its_attributes_in_either_order(tmp_path: Path) -> None:
126
+ page_source = f"<meta content='{PAGE_LINK}' name='artifact-link'>" + PAGE_WITH_ONE_SCREEN
127
+ assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
128
+
129
+
130
+ def test_should_ignore_a_meta_tag_that_holds_no_artifact_link(tmp_path: Path) -> None:
131
+ page_source = '<meta name="artifact-link" content="<link>">' + PAGE_WITH_ONE_SCREEN
132
+ assert recorded_page_link(tmp_path / "page.html", page_source) == ""
133
+
134
+
135
+ def test_should_read_the_first_of_two_name_attributes_as_html_does(tmp_path: Path) -> None:
136
+ page_source = (
137
+ f'<meta name="artifact-link" name="other" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN
138
+ )
139
+ assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
140
+
141
+
142
+ def test_should_tell_an_artifact_link_from_other_values() -> None:
143
+ assert is_artifact_link(PAGE_LINK)
144
+ assert is_artifact_link("https://claude.ai/code/artifact/0e557edc-f0c7")
145
+ assert not is_artifact_link(PAGE_LINK + "/")
146
+ assert not is_artifact_link("<link>")
147
+ assert not is_artifact_link(None)
148
+
149
+
150
+ def test_should_read_attributes_with_no_space_before_them(tmp_path: Path) -> None:
151
+ for each_meta_tag in (
152
+ f'<meta name="artifact-link"content="{PAGE_LINK}">',
153
+ f'<meta/name="artifact-link" content="{PAGE_LINK}">',
154
+ ):
155
+ page_source = each_meta_tag + PAGE_WITH_ONE_SCREEN
156
+ assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
157
+
158
+
159
+ def test_should_read_the_name_attribute_beside_a_prefixed_one(tmp_path: Path) -> None:
160
+ page_source = (
161
+ f'<meta og:name="site" name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN
162
+ )
163
+ assert recorded_page_link(tmp_path / "page.html", page_source) == PAGE_LINK
164
+
165
+
166
+ def test_should_ignore_a_record_that_holds_no_artifact_link(tmp_path: Path) -> None:
167
+ page_path = tmp_path / "page.html"
168
+ link_record_path(page_path).write_text(json.dumps({"url": "<link>", "title": "Sample Run"}))
169
+ assert recorded_page_link(page_path, PAGE_WITH_ONE_SCREEN) == ""
170
+
171
+
172
+ def test_should_read_the_link_from_the_record_beside_the_page(tmp_path: Path) -> None:
173
+ page_path = tmp_path / "page.html"
174
+ link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Other"}))
175
+ assert recorded_page_link(page_path, PAGE_WITH_ONE_SCREEN) == PAGE_LINK
176
+
177
+
178
+ def test_should_find_the_link_of_a_page_renamed_in_its_folder(tmp_path: Path) -> None:
179
+ link_record_path(tmp_path / "page.html").write_text(
180
+ json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
181
+ )
182
+ assert recorded_page_link(tmp_path / "page-v2.html", PAGE_WITH_ONE_SCREEN) == PAGE_LINK
183
+ assert recorded_page_link(tmp_path / "other.html", "<title>Other Page</title>") == ""
184
+
185
+
186
+ def test_should_leave_a_separate_page_with_the_same_title_unlinked(tmp_path: Path) -> None:
187
+ first_page_path = tmp_path / "team-a.html"
188
+ first_page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
189
+ link_record_path(first_page_path).write_text(
190
+ json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
191
+ )
192
+ assert recorded_page_link(tmp_path / "team-b.html", PAGE_WITH_ONE_SCREEN) == ""
193
+
194
+
195
+ def test_should_skip_an_old_name_record_whose_link_a_present_page_holds(tmp_path: Path) -> None:
196
+ link_record_path(tmp_path / "first-name.html").write_text(
197
+ json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
198
+ )
199
+ renamed_page_path = tmp_path / "second-name.html"
200
+ renamed_page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
201
+ link_record_path(renamed_page_path).write_text(
202
+ json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
203
+ )
204
+ assert recorded_page_link(tmp_path / "third-page.html", PAGE_WITH_ONE_SCREEN) == ""
205
+ assert recorded_page_link(renamed_page_path, PAGE_WITH_ONE_SCREEN) == PAGE_LINK
206
+
207
+
208
+ def test_should_name_each_repeated_item_and_its_count() -> None:
209
+ block_reason = format_repeat_reason([{"kind": "image", "item": "screens/home.png", "times": 3}])
210
+ assert "- screens/home.png is drawn 3 times" in block_reason
211
+ assert "Show each item once" in block_reason
212
+
213
+
214
+ def test_should_point_each_message_at_the_installed_rule_text() -> None:
215
+ rule_pointer = full_rule_pointer()
216
+ assert "Republish a page" in rule_pointer
217
+ assert "artifact-page" in rule_pointer and "README.md" in rule_pointer
218
+ assert format_repeat_reason([]).endswith(rule_pointer)
219
+
220
+
221
+ @needs_renderer
222
+ def test_should_report_the_eleven_repeated_screens_of_the_piled_page() -> None:
223
+ all_repeats, _ = repeated_items(PILED_PAGE_PATH.read_text(encoding="utf-8"))
224
+ assert all_repeats is not None
225
+ assert len(all_repeats) == 11
226
+ assert sorted(each_repeat["times"] for each_repeat in all_repeats) == [2] * 9 + [3, 3]
227
+
228
+
229
+ @needs_renderer
230
+ def test_should_pass_the_rebuilt_page_that_shows_three_draws_of_one_screen() -> None:
231
+ assert repeated_items(REBUILT_PAGE_PATH.read_text(encoding="utf-8")) == ([], "")
232
+
233
+
234
+ @needs_renderer
235
+ def test_should_refuse_the_piled_page_that_a_source_tag_count_passes() -> None:
236
+ piled_source = PILED_PAGE_PATH.read_text(encoding="utf-8")
237
+ assert piled_source.count("<img") == 1
238
+ denied_run = _run_gate({"file_path": str(PILED_PAGE_PATH)})
239
+ assert denied_run.returncode == BLOCK_EXIT_CODE
240
+ assert "screens/sample-theme/screen-12.png is drawn 3 times" in denied_run.stderr
241
+ assert _run_gate({"file_path": str(REBUILT_PAGE_PATH)}).returncode == ALLOW_EXIT_CODE
242
+
243
+
244
+ @needs_renderer
245
+ def test_should_pass_a_small_icon_used_many_times() -> None:
246
+ icon_row = '<img src="icons/check.png" width="24" height="24" alt="Done">' * 5
247
+ assert repeated_items(icon_row) == ([], "")
248
+
249
+
250
+ @needs_renderer
251
+ def test_should_count_an_image_with_no_size_set_as_an_item() -> None:
252
+ page_source = '<img src="screens/home.png" alt="Home"><p>Later</p><img src="screens/home.png" alt="">'
253
+ all_repeats, _ = repeated_items(page_source)
254
+ assert all_repeats == [{"kind": "image", "item": "screens/home.png", "times": 2}]
255
+
256
+
257
+ @needs_renderer
258
+ def test_should_count_two_spellings_of_one_image_file_as_one_item() -> None:
259
+ page_source = (
260
+ ITEM_IMAGE_STYLE
261
+ + '<img src="screens/home.png" alt="Home">'
262
+ + '<img src="./screens/home.png" alt="Home again">'
263
+ )
264
+ all_repeats, _ = repeated_items(page_source)
265
+ assert all_repeats == [{"kind": "image", "item": "screens/home.png", "times": 2}]
266
+
267
+
268
+ @needs_renderer
269
+ def test_should_count_a_lazy_image_below_the_first_screen() -> None:
270
+ page_source = (
271
+ '<img src="screens/home.png" alt="Home">'
272
+ '<div style="height:4000px"></div>'
273
+ '<img src="screens/home.png" alt="Home" loading="lazy">'
274
+ )
275
+ all_repeats, _ = repeated_items(page_source)
276
+ assert all_repeats == [{"kind": "image", "item": "screens/home.png", "times": 2}]
277
+
278
+
279
+ @needs_renderer
280
+ def test_should_pass_a_thumbnail_whose_full_view_is_hidden() -> None:
281
+ page_source = (
282
+ ITEM_IMAGE_STYLE
283
+ + '<img src="screens/home.png" alt="Home">'
284
+ + '<dialog><img src="screens/home.png" alt="Home, full size"></dialog>'
285
+ + '<details><summary>Full size</summary><img src="screens/home.png" alt="Home"></details>'
286
+ )
287
+ assert repeated_items(page_source) == ([], "")
288
+
289
+
290
+ @needs_renderer
291
+ def test_should_refuse_a_baseline_drawn_beside_each_variant() -> None:
292
+ page_source = (
293
+ ITEM_IMAGE_STYLE
294
+ + '<img src="screens/baseline.png" alt="Baseline"><img src="screens/variant-a.png" alt="A">'
295
+ + '<img src="screens/baseline.png" alt="Baseline"><img src="screens/variant-b.png" alt="B">'
296
+ )
297
+ all_repeats, _ = repeated_items(page_source)
298
+ assert all_repeats == [{"kind": "image", "item": "screens/baseline.png", "times": 2}]
299
+
300
+
301
+ @needs_renderer
302
+ def test_should_refuse_two_drawn_elements_with_one_item_key() -> None:
303
+ page_source = (
304
+ '<article data-item="release-date">Pick a release date</article>'
305
+ '<article data-item="login-test">Flaky login test</article>'
306
+ '<article data-item="release-date">Pick a release date</article>'
307
+ '<article data-item="login-test" hidden>Flaky login test</article>'
308
+ )
309
+ all_repeats, _ = repeated_items(page_source)
310
+ assert all_repeats == [{"kind": "data-item", "item": "release-date", "times": 2}]
311
+
312
+
313
+ @needs_renderer
314
+ def test_should_refuse_an_edit_that_adds_a_second_draw_of_a_screen(tmp_path: Path) -> None:
315
+ page_path = tmp_path / "page.html"
316
+ page_path.write_text(PAGE_WITH_ONE_SCREEN + "<footer>End</footer>", encoding="utf-8")
317
+ all_edits = [
318
+ {
319
+ "old_str": "<footer>End</footer>",
320
+ "new_str": SECOND_DRAW_OF_THE_SCREEN + "<footer>End</footer>",
321
+ }
322
+ ]
323
+ assert _run_gate({"file_path": str(page_path)}).returncode == ALLOW_EXIT_CODE
324
+ denied_run = _run_gate({"file_path": str(page_path), "edits": all_edits})
325
+ assert denied_run.returncode == BLOCK_EXIT_CODE
326
+ assert "screens/home.png is drawn 2 times" in denied_run.stderr
327
+
328
+
329
+ @needs_renderer
330
+ def test_should_refuse_a_second_link_and_allow_the_recorded_one(tmp_path: Path) -> None:
331
+ page_path = tmp_path / "page.html"
332
+ page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
333
+ first_publish = _run_gate({"file_path": str(page_path)})
334
+ assert first_publish.returncode == ALLOW_EXIT_CODE
335
+ assert first_publish.stdout == ""
336
+ link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Sample Run"}))
337
+ denied_run = _run_gate({"file_path": str(page_path)})
338
+ assert denied_run.returncode == BLOCK_EXIT_CODE
339
+ assert PAGE_LINK in denied_run.stderr
340
+ assert "When the user asks for a separate copy" in denied_run.stderr
341
+ assert "page.html.artifact-link.json" in denied_run.stderr
342
+ allowed_run = _run_gate({"file_path": str(page_path), "url": PAGE_LINK})
343
+ assert allowed_run.returncode == ALLOW_EXIT_CODE
344
+ assert (
345
+ REPUBLISH_NOTE in json.loads(allowed_run.stdout)["hookSpecificOutput"]["additionalContext"]
346
+ )
347
+
348
+
349
+ def test_should_name_every_place_that_keeps_the_link(tmp_path: Path) -> None:
350
+ page_path = tmp_path / "page.html"
351
+ page_path.write_text(
352
+ f'<meta name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN,
353
+ encoding="utf-8",
354
+ )
355
+ link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Sample Run"}))
356
+ denied_run = _run_gate({"file_path": str(page_path)})
357
+ assert denied_run.returncode == BLOCK_EXIT_CODE
358
+ assert "the artifact-link meta tag from the page and " in denied_run.stderr
359
+ assert "page.html.artifact-link.json, then publish" in denied_run.stderr
360
+
361
+
362
+ def test_should_name_each_old_name_record_of_a_page_renamed_twice(tmp_path: Path) -> None:
363
+ for each_old_name in ("first-name.html", "second-name.html"):
364
+ link_record_path(tmp_path / each_old_name).write_text(
365
+ json.dumps({"url": PAGE_LINK, "title": "Sample Run"})
366
+ )
367
+ page_path = tmp_path / "third-name.html"
368
+ page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
369
+ denied_run = _run_gate({"file_path": str(page_path)})
370
+ assert denied_run.returncode == BLOCK_EXIT_CODE
371
+ assert "first-name.html.artifact-link.json" in denied_run.stderr
372
+ assert "second-name.html.artifact-link.json" in denied_run.stderr
373
+
374
+
375
+ def test_should_name_the_other_link_a_second_place_holds(tmp_path: Path) -> None:
376
+ other_link = "https://claude.ai/artifact/OtherPageLink02"
377
+ page_path = tmp_path / "page.html"
378
+ page_path.write_text(
379
+ f'<meta name="artifact-link" content="{PAGE_LINK}">' + PAGE_WITH_ONE_SCREEN,
380
+ encoding="utf-8",
381
+ )
382
+ link_record_path(page_path).write_text(json.dumps({"url": other_link, "title": "Sample Run"}))
383
+ denied_run = _run_gate({"file_path": str(page_path)})
384
+ assert denied_run.returncode == BLOCK_EXIT_CODE
385
+ assert f"This page already has a link: {PAGE_LINK}" in denied_run.stderr
386
+ assert f"page.html.artifact-link.json (it holds {other_link})" in denied_run.stderr
387
+
388
+
389
+ def test_should_refuse_a_call_url_that_is_no_artifact_link(tmp_path: Path) -> None:
390
+ page_path = tmp_path / "page.html"
391
+ page_path.write_text(PAGE_WITH_ONE_SCREEN, encoding="utf-8")
392
+ link_record_path(page_path).write_text(json.dumps({"url": PAGE_LINK, "title": "Sample Run"}))
393
+ denied_run = _run_gate({"file_path": str(page_path), "url": PAGE_LINK + "/"})
394
+ assert denied_run.returncode == BLOCK_EXIT_CODE
395
+ assert PAGE_LINK in denied_run.stderr
396
+ assert f"The url {PAGE_LINK}/ in this call is not an artifact link" in denied_run.stderr
397
+
398
+
399
+ @needs_renderer
400
+ def test_should_check_a_page_saved_in_another_text_encoding(tmp_path: Path) -> None:
401
+ page_path = tmp_path / "page.html"
402
+ page_path.write_bytes(
403
+ (PAGE_WITH_ONE_SCREEN + "<p>Caf\u00e9</p>" + SECOND_DRAW_OF_THE_SCREEN).encode("latin-1")
404
+ )
405
+ denied_run = _run_gate({"file_path": str(page_path)})
406
+ assert denied_run.returncode == BLOCK_EXIT_CODE
407
+ assert "screens/home.png is drawn 2 times" in denied_run.stderr
408
+
409
+
410
+ def test_should_allow_reads_assets_and_other_tools(tmp_path: Path) -> None:
411
+ page_path = tmp_path / "page.html"
412
+ page_path.write_text(PAGE_WITH_ONE_SCREEN + SECOND_DRAW_OF_THE_SCREEN, encoding="utf-8")
413
+ assert _run_gate({"action": "read", "url": PAGE_LINK}).returncode == ALLOW_EXIT_CODE
414
+ assert _run_gate({"file_path": str(page_path), "asset": True}).returncode == ALLOW_EXIT_CODE
415
+ assert _run_gate({"file_path": str(tmp_path / "missing.html")}).returncode == ALLOW_EXIT_CODE
@@ -84,6 +84,70 @@ def test_should_block_a_work_turn_that_left_the_task_list_unchanged(
84
84
  assert "followup_message" not in block_output
85
85
 
86
86
 
87
+ def prompt_snapshot_record(all_tool_names: list[str]) -> dict[str, object]:
88
+ all_tools = [{"name": each_name, "description": f"{each_name} tool"} for each_name in all_tool_names]
89
+ return {"type": "attachment", "attachment": {"type": "prompt_snapshot", "systemPrompt": "TaskCreate", "tools": all_tools}}
90
+
91
+
92
+ def deferred_tools_delta_record(all_added_names: list[str]) -> dict[str, object]:
93
+ return {
94
+ "type": "attachment",
95
+ "attachment": {"type": "deferred_tools_delta", "addedNames": all_added_names, "removedNames": [], "readdedNames": []},
96
+ }
97
+
98
+
99
+ def deferred_tools_record(all_loaded_names: list[str]) -> dict[str, object]:
100
+ all_entries = [{"name": each_name, "description": f"{each_name} tool"} for each_name in all_loaded_names]
101
+ return {"type": "attachment", "attachment": {"type": "deferred_tools_record", "entries": all_entries}}
102
+
103
+
104
+ NO_TRACKER_TOOL_LISTING = [prompt_snapshot_record(["Bash", "Read", "Edit"]), deferred_tools_delta_record(["WebSearch", "TaskStop"])]
105
+
106
+
107
+ def test_should_allow_a_work_turn_in_a_session_that_lists_no_task_tool(
108
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
109
+ ) -> None:
110
+ transcript_path = write_records(
111
+ tmp_path / "s.jsonl", [*NO_TRACKER_TOOL_LISTING, user_prompt_record(WORK_PROMPT), tool_call_record("Bash")]
112
+ )
113
+ assert run_gate(monkeypatch, capsys, transcript_path) == {}
114
+
115
+
116
+ @pytest.mark.parametrize(
117
+ "all_listing_records",
118
+ [
119
+ [prompt_snapshot_record(["Bash"]), deferred_tools_delta_record(["WebSearch", "TaskCreate", "TaskUpdate"])],
120
+ [prompt_snapshot_record(["Bash", "TodoWrite"]), deferred_tools_delta_record(["WebSearch"])],
121
+ [prompt_snapshot_record(["Bash", "mcp__ccd_turn__update_plan"]), deferred_tools_delta_record(["WebSearch"])],
122
+ [*NO_TRACKER_TOOL_LISTING, deferred_tools_record(["TaskCreate", "TaskUpdate"])],
123
+ [*NO_TRACKER_TOOL_LISTING, user_prompt_record("Port the hook"), tool_call_record("TaskCreate", {"subject": "Port"})],
124
+ [deferred_tools_delta_record(["WebSearch"])],
125
+ [prompt_snapshot_record(["Bash"])],
126
+ ],
127
+ ids=[
128
+ "deferred-task-tools",
129
+ "inline-todowrite",
130
+ "inline-desktop-plan",
131
+ "task-tools-loaded-by-tool-search",
132
+ "task-tool-called-in-an-earlier-turn",
133
+ "no-inline-tool-listing",
134
+ "no-deferred-tool-listing",
135
+ ],
136
+ )
137
+ def test_should_block_an_untracked_work_turn_unless_the_session_lists_no_task_tool(
138
+ monkeypatch: pytest.MonkeyPatch,
139
+ capsys: pytest.CaptureFixture[str],
140
+ tmp_path: Path,
141
+ all_listing_records: list[dict[str, object]],
142
+ ) -> None:
143
+ transcript_path = write_records(
144
+ tmp_path / "s.jsonl", [*all_listing_records, user_prompt_record(WORK_PROMPT), tool_call_record("Bash")]
145
+ )
146
+ block_output = run_gate(monkeypatch, capsys, transcript_path)
147
+ assert block_output["decision"] == "block"
148
+ assert "left the task list unchanged" in block_output["reason"]
149
+
150
+
87
151
  @pytest.mark.parametrize(
88
152
  "tracker_record",
89
153
  [
@@ -22,6 +22,40 @@ from open_tasks_transcript import (
22
22
  )
23
23
 
24
24
 
25
+ def tool_listing_attachment(all_attachment_fields: dict[str, object]) -> dict[str, object]:
26
+ return {"type": "attachment", "attachment": all_attachment_fields}
27
+
28
+
29
+ @pytest.mark.parametrize(
30
+ ("all_records", "expected_lists_no_tracker_tool"),
31
+ [
32
+ (
33
+ [
34
+ tool_listing_attachment({"type": "prompt_snapshot", "tools": [{"name": "Bash"}]}),
35
+ tool_listing_attachment({"type": "deferred_tools_delta", "addedNames": ["WebSearch"]}),
36
+ ],
37
+ True,
38
+ ),
39
+ (
40
+ [
41
+ tool_listing_attachment({"type": "prompt_snapshot", "tools": [{"name": "Bash"}]}),
42
+ tool_listing_attachment({"type": "deferred_tools_delta", "addedNames": ["TaskCreate"]}),
43
+ tool_listing_attachment({"type": "deferred_tools_delta", "addedNames": []}),
44
+ ],
45
+ False,
46
+ ),
47
+ ([tool_listing_attachment({"type": "deferred_tools_delta", "addedNames": ["WebSearch"]})], False),
48
+ ([], False),
49
+ ],
50
+ ids=["listings-without-tracker", "tracker-in-an-earlier-delta", "no-inline-listing", "no-listing"],
51
+ )
52
+ def test_should_note_a_session_that_lists_no_tracker_tool(
53
+ tmp_path: Path, all_records: list[dict[str, object]], expected_lists_no_tracker_tool: bool
54
+ ) -> None:
55
+ replay = replayed_transcript(str(write_transcript(tmp_path / "s.jsonl", all_records)))
56
+ assert replay.lists_no_tracker_tool is expected_lists_no_tracker_tool
57
+
58
+
25
59
  def write_transcript(transcript_path: Path, all_records: list[dict[str, object]]) -> Path:
26
60
  transcript_path.write_text("\n".join(json.dumps(each_record) for each_record in all_records), encoding="utf-8")
27
61
  return transcript_path
@@ -9,6 +9,7 @@ from open_tasks_transcript_records import (
9
9
  is_codex_work_call,
10
10
  is_read_tool_name,
11
11
  is_work_tool_name,
12
+ listed_tool_names,
12
13
  normalized_text,
13
14
  parsed_object,
14
15
  send_call_file_paths,
@@ -172,3 +173,36 @@ def test_should_count_a_codex_call_as_a_read_by_its_type_and_tool_name() -> None
172
173
  assert is_codex_read_call(codex_record({"type": "function_call", "name": "apply_patch"})) is False
173
174
  assert is_codex_read_call(codex_record({"type": "message", "name": "shell"})) is False
174
175
  assert is_codex_read_call({"type": "event_msg", "payload": {"type": "local_shell_call"}}) is False
176
+
177
+
178
+ def attachment_record(all_attachment_fields: dict[str, object]) -> dict[str, object]:
179
+ return {"type": "attachment", "attachment": all_attachment_fields}
180
+
181
+
182
+ @pytest.mark.parametrize(
183
+ ("record", "expected_listing"),
184
+ [
185
+ (
186
+ attachment_record({"type": "prompt_snapshot", "systemPrompt": "TaskCreate", "tools": [{"name": "Bash"}, {"name": "TodoWrite"}]}),
187
+ ("prompt_snapshot", ["Bash", "TodoWrite"]),
188
+ ),
189
+ (
190
+ attachment_record(
191
+ {"type": "deferred_tools_delta", "addedNames": ["TaskCreate"], "readdedNames": ["WebSearch"], "removedNames": ["Monitor"]}
192
+ ),
193
+ ("deferred_tools_delta", ["TaskCreate", "WebSearch"]),
194
+ ),
195
+ (
196
+ attachment_record({"type": "deferred_tools_record", "entries": [{"name": "TaskUpdate", "description": "x"}]}),
197
+ ("deferred_tools_record", ["TaskUpdate"]),
198
+ ),
199
+ (attachment_record({"type": "prompt_snapshot", "systemPrompt": "TaskCreate"}), None),
200
+ (attachment_record({"type": "hook_blocking_error", "blockingError": "TaskCreate the ask"}), None),
201
+ ({"type": "user", "message": {"role": "user", "content": "use TaskCreate"}}, None),
202
+ ],
203
+ ids=["inline-tools", "deferred-delta", "tool-search-load", "snapshot-without-tools", "hook-error-prose", "user-prose"],
204
+ )
205
+ def test_should_read_tool_names_only_from_a_tool_listing_record(
206
+ record: dict[str, object], expected_listing: tuple[str, list[str]] | None
207
+ ) -> None:
208
+ assert listed_tool_names(record) == expected_listing