claude-dev-env 8.123.0 → 8.125.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ever-registered-hooks.mjs +2 -0
- package/bin/ever-registered-hooks.test.mjs +5 -0
- package/bin/profile-stop-gate.mjs +5 -7
- package/bin/profile-stop-gate.test.mjs +29 -1
- package/docs/agents-guidance/asks.md +1 -1
- package/docs/rule-guides/question-presentation.md +1 -1
- package/docs/templates/artifact-page/README.md +14 -1
- package/docs/templates/artifact-page/review-rounds.md +1 -1
- package/docs/templates/artifact-page/template.html +26 -18
- package/docs/templates/html-plan/README.md +4 -0
- package/hooks/blocking/artifact_current_page_fixtures/piled_review_page.html +399 -0
- package/hooks/blocking/artifact_current_page_fixtures/rebuilt_review_page.html +403 -0
- package/hooks/blocking/artifact_current_page_gate.py +418 -0
- package/hooks/blocking/artifact_repeated_item_render.cjs +112 -0
- package/hooks/blocking/closing_reply_judge.py +332 -0
- package/hooks/blocking/closing_reply_shapes.py +365 -0
- package/hooks/blocking/conftest.py +84 -0
- package/hooks/blocking/open_tasks_stop_gate.py +259 -11
- package/hooks/blocking/open_tasks_transcript.py +55 -2
- package/hooks/blocking/open_tasks_transcript_records.py +67 -1
- package/hooks/blocking/question_brief_gate.py +6 -4
- package/hooks/blocking/test_artifact_current_page_gate.py +415 -0
- package/hooks/blocking/test_closing_reply_judge.py +246 -0
- package/hooks/blocking/test_closing_reply_shapes.py +247 -0
- package/hooks/blocking/test_open_tasks_stop_gate.py +626 -0
- package/hooks/blocking/test_open_tasks_transcript.py +57 -0
- package/hooks/blocking/test_open_tasks_transcript_records.py +48 -0
- package/hooks/blocking/test_question_brief_gate.py +22 -0
- package/hooks/features/conduct-gates.md +10 -3
- package/hooks/hooks.json +16 -1
- package/hooks/hooks_constants/artifact_current_page_gate_constants.py +91 -0
- package/hooks/hooks_constants/artifact_dark_mode_gate_constants.py +3 -3
- package/hooks/hooks_constants/closing_reply_judge_constants.py +143 -0
- package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +101 -0
- package/hooks/hooks_constants/test_artifact_current_page_gate_constants.py +53 -0
- package/hooks/hooks_constants/test_artifact_dark_mode_gate_constants.py +6 -0
- package/hooks/hooks_constants/test_hook_time_budget.py +7 -0
- package/hooks/once_per_session.py +65 -1
- package/hooks/session/artifact_link_record.py +109 -0
- package/hooks/session/test_artifact_link_record.py +101 -0
- package/hooks/test_context_budget_hooks.py +2 -0
- package/hooks/test_once_per_session.py +35 -0
- package/package.json +1 -1
- package/scripts/policy_lint/adapter_configuration.py +6 -0
- package/scripts/policy_lint/config/constants.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +8 -0
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""Constants for the open tasks Stop gate."""
|
|
2
2
|
|
|
3
|
+
import re
|
|
4
|
+
|
|
3
5
|
HOOK_EVENT_NAME = "Stop"
|
|
4
6
|
TRANSCRIPT_PATH_KEY = "transcript_path"
|
|
5
7
|
ALLOW_EXIT_CODE = 0
|
|
@@ -17,6 +19,7 @@ TASK_STATUS_KEY = "status"
|
|
|
17
19
|
PENDING_STATUS = "pending"
|
|
18
20
|
INCOMPLETE_STATUSES = frozenset({PENDING_STATUS, "in_progress"})
|
|
19
21
|
RECORD_TYPE_KEY = "type"
|
|
22
|
+
RECORD_UUID_KEY = "uuid"
|
|
20
23
|
MESSAGE_KEY = "message"
|
|
21
24
|
CONTENT_KEY = "content"
|
|
22
25
|
TEXT_KEY = "text"
|
|
@@ -105,6 +108,14 @@ ALL_QUESTION_TOOL_NAMES = frozenset(
|
|
|
105
108
|
"request_user_input_async",
|
|
106
109
|
}
|
|
107
110
|
)
|
|
111
|
+
ALL_TRACKER_TOOL_LISTING_NAMES = frozenset({*ALL_TRACKER_TOOL_NAMES, CCD_PLAN_TOOL_NAME, CODEX_PLAN_TOOL_NAME})
|
|
112
|
+
ATTACHMENT_KEY = "attachment"
|
|
113
|
+
PROMPT_SNAPSHOT_ATTACHMENT_TYPE = "prompt_snapshot"
|
|
114
|
+
PROMPT_SNAPSHOT_TOOLS_KEY = "tools"
|
|
115
|
+
DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE = "deferred_tools_delta"
|
|
116
|
+
ALL_DEFERRED_TOOLS_DELTA_NAME_KEYS = ("addedNames", "readdedNames", "surfacedNames", "loadedBeforeSummaryNames")
|
|
117
|
+
DEFERRED_TOOLS_RECORD_ATTACHMENT_TYPE = "deferred_tools_record"
|
|
118
|
+
DEFERRED_TOOLS_RECORD_ENTRIES_KEY = "entries"
|
|
108
119
|
SESSION_TITLE_TOOL_SUFFIX = "__set_session_title"
|
|
109
120
|
CODEX_LOCAL_SHELL_CALL_TYPE = "local_shell_call"
|
|
110
121
|
ALL_CODEX_TOOL_CALL_TYPES = frozenset({CODEX_FUNCTION_CALL_TYPE, "custom_tool_call", CODEX_LOCAL_SHELL_CALL_TYPE})
|
|
@@ -172,3 +183,93 @@ TASK_LIST_NUDGE = (
|
|
|
172
183
|
" turn ends, so the list shows only open work."
|
|
173
184
|
)
|
|
174
185
|
TASK_LIST_NUDGE_ONCE_MARKER_NAME = "tracking_nudge"
|
|
186
|
+
APOSTROPHE_CLASS = "['’]"
|
|
187
|
+
ALL_PROMISED_REPORT_PATTERNS = tuple(
|
|
188
|
+
re.compile(each_pattern, re.IGNORECASE)
|
|
189
|
+
for each_pattern in (
|
|
190
|
+
rf"\bwhen you(?:{APOSTROPHE_CLASS}re| are| get| come) back\b",
|
|
191
|
+
r"\b(?:report|summary|write-?up|recap|results?)\b[^.!?\n]{0,80}?"
|
|
192
|
+
r"\bwill (?:be (?:here|waiting|ready|posted)|follow|land|arrive|come)\b",
|
|
193
|
+
rf"\bI(?:{APOSTROPHE_CLASS}ll| will| am going to)\s+(?:report|post|send|share|update you|let you know"
|
|
194
|
+
r"|follow up|circle back|check back|get back to you)\b[^.!?\n]{0,80}?"
|
|
195
|
+
r"\b(?:when|once|after|later|tomorrow|in the morning)\b",
|
|
196
|
+
rf"\bI(?:{APOSTROPHE_CLASS}ll| will| am going to)\s+report back\b",
|
|
197
|
+
)
|
|
198
|
+
)
|
|
199
|
+
PROMISE_NEGATOR_PATTERN = re.compile(r"\b(?:no|not|never|none|nothing)\b", re.IGNORECASE)
|
|
200
|
+
REPLY_SENTENCE_END_PATTERN = re.compile(r"[.!?\n]")
|
|
201
|
+
REPORT_VERB_ASK_PATTERN = re.compile(
|
|
202
|
+
r"\b(?:report back|summari[sz]e|update me|let me know|tell me (?:when|once|how))\b", re.IGNORECASE
|
|
203
|
+
)
|
|
204
|
+
REPORT_NOUN_ASK_PATTERN = re.compile(r"\b(?:report|summary|recap)\b", re.IGNORECASE)
|
|
205
|
+
LATER_TIME_PATTERN = re.compile(r"\b(?:when|once|after|later|tomorrow|then|at the end)\b", re.IGNORECASE)
|
|
206
|
+
LIST_ASK_PATTERN = re.compile(
|
|
207
|
+
r"\blist\s+(?:the|them|all|each|every|what|out)\b"
|
|
208
|
+
r"|\b(?:as|in|into)\s+(?:a\s+)?(?:list|bullets?|table)\b"
|
|
209
|
+
r"|\ba\s+list\s+of\b"
|
|
210
|
+
r"|\bgive\s+me\s+(?:a|the)\s+(?:list|table|bullets?)\b"
|
|
211
|
+
r"|\bbulleted\b",
|
|
212
|
+
re.IGNORECASE,
|
|
213
|
+
)
|
|
214
|
+
ASK_NEGATOR_PATTERN = re.compile(
|
|
215
|
+
rf"\b(?:no|not|never|none|nothing|without|don{APOSTROPHE_CLASS}t|do not|no need|never mind)\b", re.IGNORECASE
|
|
216
|
+
)
|
|
217
|
+
PROMPT_CLAUSE_SPLIT_PATTERN = re.compile(r"[.!?;,\n]|(?=\bthen\b)", re.IGNORECASE)
|
|
218
|
+
QUOTED_PROMPT_LINE_PATTERN = re.compile(r"^[ \t]*>.*$", re.MULTILINE)
|
|
219
|
+
BACKTICK_TEXT_PATTERN = re.compile(r"`[^`]*`")
|
|
220
|
+
SPACED_QUOTE_PATTERN = re.compile(r'"[^"]*\s[^"]*"|“[^”]*\s[^”]*”')
|
|
221
|
+
REMOVED_PROMPT_TEXT = " "
|
|
222
|
+
OPEN_ITEM_LEAD_PATTERN = re.compile(
|
|
223
|
+
r"\b(?:deferred|defer|sidestepped|not (?:yet )?done|left open|still open|remaining|outstanding|open items?"
|
|
224
|
+
r"|unfinished|unresolved|pending|parked|postponed|follow-?ups?|next steps|to-?dos?|loose ends)\b",
|
|
225
|
+
re.IGNORECASE,
|
|
226
|
+
)
|
|
227
|
+
LEAD_NEGATOR_PATTERN = re.compile(r"\b(?:no|none|nothing|nobody|zero)\b", re.IGNORECASE)
|
|
228
|
+
QUOTED_REPLY_LINE_PATTERN = re.compile(r"^\s*>")
|
|
229
|
+
ALL_INDENTED_LINE_PREFIXES = (" ", "\t")
|
|
230
|
+
OPEN_ITEM_LIST_MINIMUM = 3
|
|
231
|
+
LEAD_QUOTED_TEXT_PATTERN = re.compile(r'`[^`]*`|"[^"]*"|“[^”]*”')
|
|
232
|
+
COMPLETION_WORD_PATTERN = re.compile(
|
|
233
|
+
rf"(?P<negator>\b(?:not|never|\w+n{APOSTROPHE_CLASS}t)\s+(?:yet\s+|been\s+)?)?"
|
|
234
|
+
r"\b(?:fixed|closed|resolved|removed|passed|passes|completed|merged|landed|addressed|finished|cleared"
|
|
235
|
+
r"|shipped|handled|done|covers)\b",
|
|
236
|
+
re.IGNORECASE,
|
|
237
|
+
)
|
|
238
|
+
COMPLETION_NEGATOR_GROUP = "negator"
|
|
239
|
+
DONE_LEAD_TAIL_PATTERN = re.compile(r"[\s:*_.]*")
|
|
240
|
+
GOVERNANCE_BREAK_PATTERN = re.compile(
|
|
241
|
+
r"[,;:.!?()—–]|\b(?:and|or|but|while|after|before|besides|except|beyond|plus)\b", re.IGNORECASE
|
|
242
|
+
)
|
|
243
|
+
PROMISED_REPORT_REASON_TEMPLATE = (
|
|
244
|
+
'The closing reply promises the user a later report ("{phrase}"), and no typed prompt in this turn asked'
|
|
245
|
+
" for one. Drop the promise and carry the work to completion. If work is still open, do it now. If the"
|
|
246
|
+
" turn waits on a run or a wake you armed, say in one sentence what runs and what you do when it ends."
|
|
247
|
+
)
|
|
248
|
+
PROMISED_REPORT_KEEP_CONDITION = (
|
|
249
|
+
"Leave the promise as it is only when the reply already says that in one sentence, or when the user's"
|
|
250
|
+
" typed words in this turn asked for a report later."
|
|
251
|
+
)
|
|
252
|
+
OPEN_ITEM_LIST_REASON_TEMPLATE = (
|
|
253
|
+
'The closing reply lists {count} items under "{lead}", and this turn sent no question through the'
|
|
254
|
+
" native question tool. Read the live state of each item and drop the ones that are done. Act now on each"
|
|
255
|
+
" item you can settle yourself. Put each item that needs the user's judgment in its own AskUserQuestion"
|
|
256
|
+
" call (Codex: request_user_input), with the context in the question text and two or three options whose"
|
|
257
|
+
" effects sit in their descriptions. The first question call after this hold can be refused once for the"
|
|
258
|
+
" length of the held reply; send the same call again."
|
|
259
|
+
)
|
|
260
|
+
OPEN_ITEM_LIST_KEEP_CONDITION = (
|
|
261
|
+
"Leave the list as it is only when every listed item is finished work or plain status that needs no"
|
|
262
|
+
" decision from the user, or when the user's typed words in this turn asked for the items as a list or a"
|
|
263
|
+
' table in chat. A question such as "what did you defer?" asks for an answer, and the open decisions in'
|
|
264
|
+
" that answer still go out as question cards."
|
|
265
|
+
)
|
|
266
|
+
REWORKED_REPLY_INSTRUCTION = "Then send the reworked reply and end the turn."
|
|
267
|
+
SHORT_REPLY_INSTRUCTION = "Then send a short reply that names what you did and end the turn."
|
|
268
|
+
CLOSING_REPLY_HOLD_MARKER_NAME = "closing_reply_hold"
|
|
269
|
+
ALL_OWN_REASON_OPENINGS = (
|
|
270
|
+
"This turn worked on the user's ask and left the task list unchanged.",
|
|
271
|
+
"The task list shows completed work beside open items",
|
|
272
|
+
"This turn tells the user that something waits on them",
|
|
273
|
+
"The closing reply promises the user a later report",
|
|
274
|
+
"The closing reply lists",
|
|
275
|
+
)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Behavioral tests for the artifact current page gate's patterns, paths and messages."""
|
|
2
|
+
|
|
3
|
+
from hooks_constants.artifact_current_page_gate_constants import (
|
|
4
|
+
ARTIFACT_LINK_PATTERN,
|
|
5
|
+
LINK_META_NAME,
|
|
6
|
+
META_TAG_PATTERN,
|
|
7
|
+
PAGE_TITLE_PATTERN,
|
|
8
|
+
REPEAT_RENDERER_SCRIPT_PATH,
|
|
9
|
+
REPEATED_ITEM_LINE_TEMPLATE,
|
|
10
|
+
SECOND_LINK_REASON_TEMPLATE,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_renderer_path_names_the_packaged_render_script() -> None:
|
|
15
|
+
assert REPEAT_RENDERER_SCRIPT_PATH.is_file()
|
|
16
|
+
assert REPEAT_RENDERER_SCRIPT_PATH.name == "artifact_repeated_item_render.cjs"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_link_pattern_matches_both_artifact_link_shapes_and_no_other_page() -> None:
|
|
20
|
+
assert ARTIFACT_LINK_PATTERN.search("see https://claude.ai/artifact/Ab12Cd34 now").group(0) == (
|
|
21
|
+
"https://claude.ai/artifact/Ab12Cd34"
|
|
22
|
+
)
|
|
23
|
+
assert ARTIFACT_LINK_PATTERN.search("https://claude.ai/code/artifact/0e557edc-f0c7.").group(
|
|
24
|
+
0
|
|
25
|
+
) == ("https://claude.ai/code/artifact/0e557edc-f0c7")
|
|
26
|
+
assert ARTIFACT_LINK_PATTERN.search("https://claude.ai/code/session_01") is None
|
|
27
|
+
assert ARTIFACT_LINK_PATTERN.fullmatch("https://claude.ai/artifact/Ab_12Cd") is not None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_meta_tag_pattern_finds_each_meta_tag_of_a_page() -> None:
|
|
31
|
+
page_head = (
|
|
32
|
+
'<meta name="viewport" content="width=device-width">'
|
|
33
|
+
'<meta content="https://claude.ai/artifact/Ab12Cd34" name="artifact-link">'
|
|
34
|
+
)
|
|
35
|
+
assert len(META_TAG_PATTERN.findall(page_head)) == 2
|
|
36
|
+
assert META_TAG_PATTERN.findall(page_head)[1].count(LINK_META_NAME) == 1
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_title_pattern_reads_a_title_that_spans_lines() -> None:
|
|
40
|
+
assert PAGE_TITLE_PATTERN.search("<title>\nSample Run\n</title>").group(1).strip() == (
|
|
41
|
+
"Sample Run"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_messages_name_the_item_the_count_and_the_link() -> None:
|
|
46
|
+
assert REPEATED_ITEM_LINE_TEMPLATE.format(item="screens/home.png", times=3) == (
|
|
47
|
+
"- screens/home.png is drawn 3 times\n"
|
|
48
|
+
)
|
|
49
|
+
second_link_reason = SECOND_LINK_REASON_TEMPLATE.format(
|
|
50
|
+
link="https://claude.ai/artifact/Ab12Cd34", link_source="the record file"
|
|
51
|
+
)
|
|
52
|
+
assert "https://claude.ai/artifact/Ab12Cd34" in second_link_reason
|
|
53
|
+
assert "remove the record file, then publish" in second_link_reason
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Behavioral tests for the artifact page gate's paths and publish wrapper."""
|
|
2
2
|
|
|
3
3
|
from hooks_constants.artifact_dark_mode_gate_constants import (
|
|
4
|
+
BLOCKING_HOOKS_DIRECTORY,
|
|
4
5
|
PUBLISH_SKELETON_PREFIX,
|
|
5
6
|
RENDERER_SCRIPT_PATH,
|
|
6
7
|
)
|
|
@@ -11,6 +12,11 @@ def test_renderer_path_names_the_packaged_render_script() -> None:
|
|
|
11
12
|
assert RENDERER_SCRIPT_PATH.suffix == ".cjs"
|
|
12
13
|
|
|
13
14
|
|
|
15
|
+
def test_blocking_directory_holds_the_gate_that_runs_the_renderer() -> None:
|
|
16
|
+
assert (BLOCKING_HOOKS_DIRECTORY / "artifact_dark_mode_gate.py").is_file()
|
|
17
|
+
assert RENDERER_SCRIPT_PATH.parent == BLOCKING_HOOKS_DIRECTORY
|
|
18
|
+
|
|
19
|
+
|
|
14
20
|
def test_publish_wrapper_pins_body_light_and_opens_the_body() -> None:
|
|
15
21
|
assert "body{margin:0;padding:0;" in PUBLISH_SKELETON_PREFIX
|
|
16
22
|
assert "background:#faf9f5;color:#141413" in PUBLISH_SKELETON_PREFIX
|
|
@@ -9,6 +9,7 @@ from pathlib import Path
|
|
|
9
9
|
|
|
10
10
|
import pytest
|
|
11
11
|
|
|
12
|
+
from hooks_constants import closing_reply_judge_constants
|
|
12
13
|
from hooks_constants import nested_project_hooks_constants
|
|
13
14
|
from hooks_constants import turn_review_gate_constants
|
|
14
15
|
from hooks_constants.hook_time_budget import call_timeout, remaining_seconds, time_budget
|
|
@@ -102,3 +103,9 @@ def test_should_give_the_nested_hooks_a_budget_on_every_event_it_is_registered_o
|
|
|
102
103
|
if each_hook.get("command", "").endswith("/hooks/lifecycle/nested_project_hooks.py")
|
|
103
104
|
}
|
|
104
105
|
assert set(nested_project_hooks_constants.ALL_RUN_BUDGET_SECONDS_BY_EVENT) == all_registered_events
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_should_register_the_open_tasks_gate_with_a_45_second_timeout() -> None:
|
|
109
|
+
gate_timeout = registered_timeout("Stop", "blocking/open_tasks_stop_gate.py")
|
|
110
|
+
assert gate_timeout == 45
|
|
111
|
+
assert gate_timeout - closing_reply_judge_constants.JUDGE_TIMEOUT_SECONDS >= MINIMUM_MARGIN_SECONDS
|
|
@@ -16,6 +16,9 @@ Codex rollout with a ``compacted`` entry.
|
|
|
16
16
|
|
|
17
17
|
``claim_only_showing`` leaves the compaction count out of the marker name, so
|
|
18
18
|
its text shows once in the whole session, compactions included.
|
|
19
|
+
``claim_turn_showing`` puts a turn key in the marker name and reports a marker
|
|
20
|
+
it cannot keep as None, so a caller can fall back to its own behavior.
|
|
21
|
+
``is_turn_claimed`` reads whether that turn marker exists and creates nothing.
|
|
19
22
|
"""
|
|
20
23
|
|
|
21
24
|
from __future__ import annotations
|
|
@@ -40,7 +43,14 @@ from hooks_constants.once_per_session_constants import (
|
|
|
40
43
|
)
|
|
41
44
|
from hooks_constants.session_file_id import session_file_id
|
|
42
45
|
|
|
43
|
-
__all__ = [
|
|
46
|
+
__all__ = [
|
|
47
|
+
"claim_first_showing",
|
|
48
|
+
"claim_only_showing",
|
|
49
|
+
"claim_turn_showing",
|
|
50
|
+
"clear_showings",
|
|
51
|
+
"compaction_count",
|
|
52
|
+
"is_turn_claimed",
|
|
53
|
+
]
|
|
44
54
|
|
|
45
55
|
|
|
46
56
|
def _is_compaction_entry(transcript_line: bytes) -> bool:
|
|
@@ -110,6 +120,60 @@ def claim_only_showing(hook_name: str, all_hook_fields: Mapping[str, object]) ->
|
|
|
110
120
|
return _claim_marker(hook_name, all_hook_fields, WHOLE_SESSION_MARKER_PART)
|
|
111
121
|
|
|
112
122
|
|
|
123
|
+
def claim_turn_showing(hook_name: str, all_hook_fields: Mapping[str, object], turn_key: str) -> bool | None:
|
|
124
|
+
"""Return True the first time for this turn and record it, False after that, or None when no marker can be kept.
|
|
125
|
+
|
|
126
|
+
::
|
|
127
|
+
|
|
128
|
+
session s1, turn t1, first claim -> True
|
|
129
|
+
session s1, turn t1, later claim -> False
|
|
130
|
+
session s1, turn t2, first claim -> True
|
|
131
|
+
payload with no session id -> None
|
|
132
|
+
marker cannot be written -> None
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
hook_name: A short name that keeps each hook's marker apart.
|
|
136
|
+
all_hook_fields: The hook payload, read for its session id and agent id.
|
|
137
|
+
turn_key: A key that names the turn, such as the id of the prompt that started it.
|
|
138
|
+
"""
|
|
139
|
+
session_id = all_hook_fields.get(SESSION_ID_KEY)
|
|
140
|
+
if not isinstance(session_id, str) or not session_id:
|
|
141
|
+
return None
|
|
142
|
+
try:
|
|
143
|
+
with _marker_path(hook_name, session_id, all_hook_fields, session_file_id(turn_key)).open(
|
|
144
|
+
EXCLUSIVE_CREATE_MODE
|
|
145
|
+
):
|
|
146
|
+
return True
|
|
147
|
+
except FileExistsError:
|
|
148
|
+
return False
|
|
149
|
+
except OSError:
|
|
150
|
+
return None
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def is_turn_claimed(hook_name: str, all_hook_fields: Mapping[str, object], turn_key: str) -> bool | None:
|
|
154
|
+
"""Return whether ``claim_turn_showing`` already claimed this turn, without claiming it.
|
|
155
|
+
|
|
156
|
+
::
|
|
157
|
+
|
|
158
|
+
session s1, turn t1, before any claim -> False
|
|
159
|
+
session s1, turn t1, after a claim -> True
|
|
160
|
+
payload with no session id -> None
|
|
161
|
+
marker cannot be read -> None
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
hook_name: The name the hook passes to ``claim_turn_showing``.
|
|
165
|
+
all_hook_fields: The hook payload, read for its session id and agent id.
|
|
166
|
+
turn_key: The turn key the hook passes to ``claim_turn_showing``.
|
|
167
|
+
"""
|
|
168
|
+
session_id = all_hook_fields.get(SESSION_ID_KEY)
|
|
169
|
+
if not isinstance(session_id, str) or not session_id:
|
|
170
|
+
return None
|
|
171
|
+
try:
|
|
172
|
+
return _marker_path(hook_name, session_id, all_hook_fields, session_file_id(turn_key)).is_file()
|
|
173
|
+
except OSError:
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
|
|
113
177
|
def _claim_marker(hook_name: str, all_hook_fields: Mapping[str, object], showing_part: str) -> bool:
|
|
114
178
|
session_id = all_hook_fields.get(SESSION_ID_KEY)
|
|
115
179
|
if not isinstance(session_id, str) or not session_id:
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""PostToolUse hook that records the link of a published page beside the page file.
|
|
3
|
+
|
|
4
|
+
An agent that republishes a page must pass the page's link, or the publish
|
|
5
|
+
opens a second copy. An agent that lost its context has no other place to
|
|
6
|
+
find the link. This hook writes it down after each page publish::
|
|
7
|
+
|
|
8
|
+
Artifact {"file_path": "/work/first-look.html"} -> result names https://claude.ai/artifact/abc
|
|
9
|
+
writes /work/first-look.html.artifact-link.json {"url": "...", "title": "..."}
|
|
10
|
+
Artifact {"file_path": "...", "url": "<link>"} -> records <link>
|
|
11
|
+
Artifact {"action": "read", ...} -> no record
|
|
12
|
+
|
|
13
|
+
``blocking/artifact_current_page_gate.py`` reads the record on the next
|
|
14
|
+
publish. The hook never blocks and prints nothing.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import json
|
|
20
|
+
import sys
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
24
|
+
if hooks_root_directory not in sys.path:
|
|
25
|
+
sys.path.insert(0, hooks_root_directory)
|
|
26
|
+
|
|
27
|
+
from blocking.artifact_current_page_gate import is_artifact_link, link_record_path, page_title
|
|
28
|
+
from blocking.artifact_dark_mode_gate import published_page_path
|
|
29
|
+
from hooks_constants.artifact_current_page_gate_constants import (
|
|
30
|
+
ARTIFACT_LINK_PATTERN,
|
|
31
|
+
ARTIFACT_URL_INPUT_KEY,
|
|
32
|
+
LINK_RECORD_TITLE_KEY,
|
|
33
|
+
LINK_RECORD_URL_KEY,
|
|
34
|
+
TOOL_RESPONSE_KEY,
|
|
35
|
+
)
|
|
36
|
+
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
37
|
+
from hooks_constants.spawn_readiness_hook_constants import TOOL_INPUT_KEY, TOOL_NAME_KEY
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def published_link(all_tool_input: dict[str, object], tool_response: object) -> str:
|
|
41
|
+
"""Return the link a page publish went to, or an empty string.
|
|
42
|
+
|
|
43
|
+
A publish that passes ``url`` went to that link. A first publish names its
|
|
44
|
+
new link in the tool result, under ``url`` or in its text::
|
|
45
|
+
|
|
46
|
+
{"url": "https://claude.ai/artifact/abc"}, <any result> -> https://claude.ai/artifact/abc
|
|
47
|
+
{}, {"url": "https://claude.ai/artifact/xyz", ...} -> https://claude.ai/artifact/xyz
|
|
48
|
+
{}, "Published to https://claude.ai/artifact/xyz" -> https://claude.ai/artifact/xyz
|
|
49
|
+
{}, "Copied from <link a> to <link b>" -> ""
|
|
50
|
+
{}, "The publish was refused" -> ""
|
|
51
|
+
|
|
52
|
+
Result text that names two different links does not say which one is the
|
|
53
|
+
page, so it gives no link. A call ``url`` that is no artifact link is
|
|
54
|
+
skipped.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
all_tool_input: The input of the publish call.
|
|
58
|
+
tool_response: The tool result, as text or as a parsed object.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
The link, or an empty string when neither place names one.
|
|
62
|
+
"""
|
|
63
|
+
link_in_call = all_tool_input.get(ARTIFACT_URL_INPUT_KEY)
|
|
64
|
+
if isinstance(link_in_call, str) and is_artifact_link(link_in_call):
|
|
65
|
+
return link_in_call
|
|
66
|
+
if isinstance(tool_response, dict):
|
|
67
|
+
link_in_result = tool_response.get(ARTIFACT_URL_INPUT_KEY)
|
|
68
|
+
if isinstance(link_in_result, str) and is_artifact_link(link_in_result):
|
|
69
|
+
return link_in_result
|
|
70
|
+
response_text = (
|
|
71
|
+
tool_response if isinstance(tool_response, str) else json.dumps(tool_response, default=str)
|
|
72
|
+
)
|
|
73
|
+
all_links_in_text = set(ARTIFACT_LINK_PATTERN.findall(response_text))
|
|
74
|
+
return all_links_in_text.pop() if len(all_links_in_text) == 1 else ""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def record_page_link(page_path: Path, link: str) -> None:
|
|
78
|
+
"""Write the link record beside the page, and skip a page file that cannot be read.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
page_path: The local page file that was published.
|
|
82
|
+
link: The link the page was published at.
|
|
83
|
+
"""
|
|
84
|
+
try:
|
|
85
|
+
title = page_title(page_path.read_text(encoding="utf-8", errors="replace"))
|
|
86
|
+
link_record_path(page_path).write_text(
|
|
87
|
+
json.dumps({LINK_RECORD_URL_KEY: link, LINK_RECORD_TITLE_KEY: title}) + "\n",
|
|
88
|
+
encoding="utf-8",
|
|
89
|
+
)
|
|
90
|
+
except OSError:
|
|
91
|
+
return
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def main() -> int:
|
|
95
|
+
hook_input = read_hook_input_dictionary_from_stdin()
|
|
96
|
+
if hook_input is None:
|
|
97
|
+
return 0
|
|
98
|
+
tool_input = hook_input.get(TOOL_INPUT_KEY)
|
|
99
|
+
page_path = published_page_path(hook_input.get(TOOL_NAME_KEY), tool_input)
|
|
100
|
+
if page_path is None or not page_path.is_file() or not isinstance(tool_input, dict):
|
|
101
|
+
return 0
|
|
102
|
+
link = published_link(tool_input, hook_input.get(TOOL_RESPONSE_KEY))
|
|
103
|
+
if link:
|
|
104
|
+
record_page_link(page_path, link)
|
|
105
|
+
return 0
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
sys.exit(main())
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Behavior tests for the hook that records a published page's link beside the page."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
11
|
+
|
|
12
|
+
from blocking.artifact_current_page_gate import link_record_path, recorded_page_link
|
|
13
|
+
from session.artifact_link_record import published_link, record_page_link
|
|
14
|
+
|
|
15
|
+
RECORD_SCRIPT_PATH = Path(__file__).resolve().parent / "artifact_link_record.py"
|
|
16
|
+
PAGE_LINK = "https://claude.ai/artifact/SamplePageLink01"
|
|
17
|
+
PAGE_SOURCE = "<title>Sample Run</title><p>One screen.</p>"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _run_record(tool_input: dict[str, object], tool_response: object) -> int:
|
|
21
|
+
completed_run = subprocess.run(
|
|
22
|
+
[sys.executable, str(RECORD_SCRIPT_PATH)],
|
|
23
|
+
input=json.dumps(
|
|
24
|
+
{"tool_name": "Artifact", "tool_input": tool_input, "tool_response": tool_response}
|
|
25
|
+
),
|
|
26
|
+
capture_output=True,
|
|
27
|
+
text=True,
|
|
28
|
+
check=False,
|
|
29
|
+
)
|
|
30
|
+
assert completed_run.stdout == ""
|
|
31
|
+
return completed_run.returncode
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_should_take_the_link_the_call_passed() -> None:
|
|
35
|
+
assert published_link({"url": PAGE_LINK}, "Updated the page.") == PAGE_LINK
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_should_skip_a_call_url_that_is_no_artifact_link() -> None:
|
|
39
|
+
assert published_link({"url": "<link>"}, "The publish was refused.") == ""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_should_take_the_link_the_result_names() -> None:
|
|
43
|
+
assert published_link({}, f"Published to {PAGE_LINK} (private).") == PAGE_LINK
|
|
44
|
+
code_link = "https://claude.ai/code/artifact/0e557edc-f0c7-4382-8568-aee7ab02950b"
|
|
45
|
+
assert published_link({}, [{"type": "text", "text": f"Live at {code_link}."}]) == code_link
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_should_take_the_url_field_of_a_publish_result_over_another_link_in_it() -> None:
|
|
49
|
+
publish_result = {
|
|
50
|
+
"note": "Started from https://claude.ai/artifact/SomeOtherPage01",
|
|
51
|
+
"url": PAGE_LINK,
|
|
52
|
+
"title": "Sample Run",
|
|
53
|
+
"seq": 1,
|
|
54
|
+
}
|
|
55
|
+
assert published_link({}, publish_result) == PAGE_LINK
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_should_find_no_link_in_a_result_that_names_none() -> None:
|
|
59
|
+
assert published_link({}, "The publish was refused.") == ""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_should_take_a_link_whose_id_holds_an_underscore() -> None:
|
|
63
|
+
underscore_link = "https://claude.ai/artifact/Ab_12Cd"
|
|
64
|
+
assert published_link({}, f"Published to {underscore_link}.") == underscore_link
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_should_record_nothing_when_the_result_text_names_two_links() -> None:
|
|
68
|
+
result_text = f"Copied from https://claude.ai/artifact/SourcePage01 to {PAGE_LINK}."
|
|
69
|
+
assert published_link({}, result_text) == ""
|
|
70
|
+
assert published_link({}, f"Published to {PAGE_LINK}. Open {PAGE_LINK}.") == PAGE_LINK
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_should_write_the_link_and_title_beside_the_page(tmp_path: Path) -> None:
|
|
74
|
+
page_path = tmp_path / "first-look.html"
|
|
75
|
+
page_path.write_text(PAGE_SOURCE, encoding="utf-8")
|
|
76
|
+
record_page_link(page_path, PAGE_LINK)
|
|
77
|
+
assert json.loads(link_record_path(page_path).read_text(encoding="utf-8")) == {
|
|
78
|
+
"url": PAGE_LINK,
|
|
79
|
+
"title": "Sample Run",
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_should_write_no_record_for_a_page_that_is_gone(tmp_path: Path) -> None:
|
|
84
|
+
missing_page_path = tmp_path / "missing.html"
|
|
85
|
+
record_page_link(missing_page_path, PAGE_LINK)
|
|
86
|
+
assert not link_record_path(missing_page_path).exists()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_should_record_a_first_publish_so_the_gate_finds_its_link(tmp_path: Path) -> None:
|
|
90
|
+
page_path = tmp_path / "first-look.html"
|
|
91
|
+
page_path.write_text(PAGE_SOURCE, encoding="utf-8")
|
|
92
|
+
assert _run_record({"file_path": str(page_path)}, f"Published: {PAGE_LINK}") == 0
|
|
93
|
+
assert recorded_page_link(page_path, PAGE_SOURCE) == PAGE_LINK
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_should_write_no_record_for_a_read_or_a_result_with_no_link(tmp_path: Path) -> None:
|
|
97
|
+
page_path = tmp_path / "first-look.html"
|
|
98
|
+
page_path.write_text(PAGE_SOURCE, encoding="utf-8")
|
|
99
|
+
assert _run_record({"action": "read", "url": PAGE_LINK}, PAGE_SOURCE) == 0
|
|
100
|
+
assert _run_record({"file_path": str(page_path)}, "The publish was refused.") == 0
|
|
101
|
+
assert not link_record_path(page_path).exists()
|
|
@@ -31,6 +31,8 @@ UNMEASURED_REASON_BY_SCRIPT = {
|
|
|
31
31
|
"session/plugin_change_warning.py": "emits only after a plugin changes on disk mid-session, "
|
|
32
32
|
"once per plugin, naming plugin ids",
|
|
33
33
|
"blocking/wakeup_cap_gate.py": "emits one fixed-template line only when it rewrites a wake delay",
|
|
34
|
+
"blocking/artifact_current_page_gate.py": "emits one fixed republish note only after a "
|
|
35
|
+
"Playwright render of the page, which the measuring run has no browser for",
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
|
|
@@ -7,8 +7,10 @@ import pytest
|
|
|
7
7
|
from once_per_session import (
|
|
8
8
|
claim_first_showing,
|
|
9
9
|
claim_only_showing,
|
|
10
|
+
claim_turn_showing,
|
|
10
11
|
clear_showings,
|
|
11
12
|
compaction_count,
|
|
13
|
+
is_turn_claimed,
|
|
12
14
|
)
|
|
13
15
|
|
|
14
16
|
CLAUDE_COMPACTION_LINE = json.dumps({"type": "system", "subtype": "compact_boundary"})
|
|
@@ -117,3 +119,36 @@ def test_should_show_only_once_in_a_session_across_compactions(transcript_path:
|
|
|
117
119
|
assert claim_only_showing("hook", _fields(transcript_path, session_id="s2")) is True
|
|
118
120
|
clear_showings("s1")
|
|
119
121
|
assert claim_only_showing("hook", _fields(transcript_path)) is True
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def test_should_claim_a_turn_once_and_each_new_turn_again(transcript_path: Path) -> None:
|
|
125
|
+
assert claim_turn_showing("hook", _fields(transcript_path), "turn-1") is True
|
|
126
|
+
assert claim_turn_showing("hook", _fields(transcript_path), "turn-1") is False
|
|
127
|
+
assert claim_turn_showing("hook", _fields(transcript_path), "turn-2") is True
|
|
128
|
+
clear_showings("s1")
|
|
129
|
+
assert claim_turn_showing("hook", _fields(transcript_path), "turn-1") is True
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def test_should_report_no_claim_when_the_turn_marker_cannot_be_kept(
|
|
133
|
+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
134
|
+
) -> None:
|
|
135
|
+
monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path / "absent_directory"))
|
|
136
|
+
assert claim_turn_showing("hook", {"session_id": "s1"}, "turn-1") is None
|
|
137
|
+
assert claim_turn_showing("hook", {}, "turn-1") is None
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_is_turn_claimed_should_report_a_claim_without_making_one(transcript_path: Path) -> None:
|
|
141
|
+
assert is_turn_claimed("hook", _fields(transcript_path), "turn-1") is False
|
|
142
|
+
assert claim_turn_showing("hook", _fields(transcript_path), "turn-1") is True
|
|
143
|
+
assert is_turn_claimed("hook", _fields(transcript_path), "turn-1") is True
|
|
144
|
+
assert is_turn_claimed("hook", _fields(transcript_path), "turn-2") is False
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_is_turn_claimed_should_not_create_the_marker(transcript_path: Path) -> None:
|
|
148
|
+
assert is_turn_claimed("hook", _fields(transcript_path), "turn-1") is False
|
|
149
|
+
assert is_turn_claimed("hook", _fields(transcript_path), "turn-1") is False
|
|
150
|
+
assert claim_turn_showing("hook", _fields(transcript_path), "turn-1") is True
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_is_turn_claimed_should_report_none_without_a_session_id(transcript_path: Path) -> None:
|
|
154
|
+
assert is_turn_claimed("hook", {}, "turn-1") is None
|
package/package.json
CHANGED
|
@@ -136,6 +136,7 @@ def _names_exempt_registration_path(registered_string: str) -> bool:
|
|
|
136
136
|
hooks/blocking/session_title_format_gate.py -> exempt
|
|
137
137
|
hooks/blocking/task_subject_outcome_gate.py -> exempt
|
|
138
138
|
hooks/blocking/artifact_dark_mode_gate.py -> exempt
|
|
139
|
+
hooks/blocking/artifact_current_page_gate.py -> exempt
|
|
139
140
|
hooks/blocking/send_user_file_worktree_root_gate.py -> exempt
|
|
140
141
|
hooks/blocking/sent_file_question_gate.py -> exempt
|
|
141
142
|
hooks/blocking/question_brief_gate.py -> exempt
|
|
@@ -227,6 +228,11 @@ def _names_exempt_registration_path(registered_string: str) -> bool:
|
|
|
227
228
|
characters. It decides the shape of a task subject and no code or safety
|
|
228
229
|
policy.
|
|
229
230
|
|
|
231
|
+
The artifact current page gate denies a page publish that draws one item
|
|
232
|
+
more than once, or that would open a second link for a page with a
|
|
233
|
+
recorded link. It decides what a published page shows and no code or
|
|
234
|
+
safety policy.
|
|
235
|
+
|
|
230
236
|
Args:
|
|
231
237
|
registered_string: One command, path, script, or entrypoint string.
|
|
232
238
|
"""
|
|
@@ -74,6 +74,7 @@ ALL_ACTION_BOUNDARY_EXEMPT_REGISTRATION_PATHS = (
|
|
|
74
74
|
"blocking/task_subject_outcome_gate.py",
|
|
75
75
|
"blocking/image_read_size_gate.py",
|
|
76
76
|
"blocking/artifact_dark_mode_gate.py",
|
|
77
|
+
"blocking/artifact_current_page_gate.py",
|
|
77
78
|
"blocking/send_user_file_worktree_root_gate.py",
|
|
78
79
|
"blocking/sent_file_question_gate.py",
|
|
79
80
|
"blocking/question_brief_gate.py",
|
|
@@ -162,6 +162,14 @@ def test_new_artifact_page_gate_registration_is_clean(tmp_path: Path) -> None:
|
|
|
162
162
|
assert adapters.hook_configuration_diagnostics(current_document, tmp_path) == ()
|
|
163
163
|
|
|
164
164
|
|
|
165
|
+
def test_new_artifact_current_page_gate_registration_is_clean(tmp_path: Path) -> None:
|
|
166
|
+
current_document = _hook_document(
|
|
167
|
+
["hooks/blocking/artifact_current_page_gate.py"],
|
|
168
|
+
'{"hooks": {}}',
|
|
169
|
+
)
|
|
170
|
+
assert adapters.hook_configuration_diagnostics(current_document, tmp_path) == ()
|
|
171
|
+
|
|
172
|
+
|
|
165
173
|
def test_new_send_user_file_gate_registration_is_clean(tmp_path: Path) -> None:
|
|
166
174
|
current_document = _hook_document(
|
|
167
175
|
["hooks/blocking/send_user_file_worktree_root_gate.py"],
|