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.
- package/bin/ever-registered-hooks.mjs +2 -0
- package/bin/ever-registered-hooks.test.mjs +5 -0
- 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/open_tasks_stop_gate.py +12 -1
- package/hooks/blocking/open_tasks_transcript.py +30 -0
- package/hooks/blocking/open_tasks_transcript_records.py +57 -1
- package/hooks/blocking/test_artifact_current_page_gate.py +415 -0
- package/hooks/blocking/test_open_tasks_stop_gate.py +64 -0
- package/hooks/blocking/test_open_tasks_transcript.py +34 -0
- package/hooks/blocking/test_open_tasks_transcript_records.py +34 -0
- package/hooks/features/conduct-gates.md +6 -0
- package/hooks/hooks.json +15 -0
- 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/open_tasks_stop_gate_constants.py +8 -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/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/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
|
@@ -23,6 +23,8 @@ This family stops a chat reply, edited message, or tool call when its payload br
|
|
|
23
23
|
- `blocking/build_eval_before_eval_write_gate.py` denies a write to an eval file until a Skill call named exactly `claude-api` with args `build-eval`, or the `/claude-api build-eval` command, appears in the transcript.
|
|
24
24
|
- `blocking/merge_review_gate.py` denies an agent's merge until a review pass on the merged head reported zero findings, or ruled out every finding and linked the posted rulings. It reads the receipt `scripts/merge_review_receipt.py` writes, requires the merge, auto-merge or merge-queue entry to be pinned to that head, and skips repositories listed in `CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES`. A `gh pr merge` call whose every merge segment carries `--disable-auto` only turns auto-merge off, so it passes. A segment that also carries `--auto`, gives `--auto` or `--disable-auto` an `=` value, joins short flags such as `-sb`, or holds a bare `--` keeps every check. Its deny reason names the review command `CLAUDE_MERGE_REVIEW_COMMAND` sets. The reason tells the agent to run that command for the first three passes and `/code-review low --fix <number>` for each later pass; the gate itself counts no passes. It also denies a pull request open, through a shell create or a `create_pull_request` tool, until a pass recorded as `merge_review_receipt.py <owner/name> open <head> 0` covers the head the pull request opens with. A shell create is any spelling the shared listing in `blocking/followup_pr_dedupe.py` reads: `gh pr create`, `gh pr new`, `gh` with a root `-R` or `--repo` before `pr`, a `GH_REPO=` prefix, and `pull_request.py create` run by any `python`, `python3.x`, `py` or `uv run` launcher, with interpreter options or a path to the script. A command that chains several creates is checked create by create, in command order, and the deny names the first head with no clean pass. Each create reads its repository as gh does: among every `-R`, `--repo`, `--repo=`, `-R=` or clustered `-dR` flag before or after `pr`, the last one decides, and a flag word that is the value of another option, such as `--title --repo=x/y`, is not a flag. `blocking/merge_review_gate_parts/create_options.py` reads each value as gh does: an option that takes a value takes the next word, so `--title -Hx` and `--body -H --title x` name no head, and in a short cluster such as `-bHx` the first letter that takes a value takes the rest of the cluster. With no flag it reads the last `GH_REPO=` prefix, then the origin remote. A wrapper such as `env -u GH_REPO`, `env -i` or `sudo` can drop the prefix before gh runs, as in `GH_REPO=o/r env -u GH_REPO gh pr create`, where gh opens in the origin repository. So every create the walk reaches whose repository comes only from a `GH_REPO=` prefix needs a clean pass for the prefix repository and for origin, also when the prefix value names no repository, and its deny for origin says to name the repository with `--repo owner/name` on the create. The walk steps over `env`, `sudo` and `timeout` with their options, and over the launchers `nohup`, `nice`, `stdbuf`, `setsid` and `time` when each launcher option holds its value in the same word or takes a number, as in `stdbuf -o0` or `nice -n 5`. It also reads a create behind `command` or `exec`, as in `command env -u GH_REPO` or `exec nohup env -u GH_REPO`. A create after the first one is not read behind a program the walk does not step over, such as `ionice`, `flock` or `doas`, or behind a launcher option with a separate value that is not a number, such as `stdbuf -o L` or `time -o out.txt`. A create that passes `--repo owner/name` needs a pass for that repository only. Two creates with the same options stay two checks when one has a prefix and the other does not. The first create in a command is also read the way the gate read it before it walked every create: its repository comes from a `--repo` flag or the origin remote, never from a prefix. A `GH_REPO+=` append, in a create's own prefix or anywhere else in the command, counts as a set whose value the gate cannot read. When the command sets `GH_REPO` anywhere outside a create's own prefix, through an assignment before another program, such as `GH_REPO=o/r bash -c ...`, or through an `export`, `declare`, `typeset`, `local` or `readonly` word that names `GH_REPO`, each create that names no repository flag and no `GH_REPO=` prefix of its own is denied as unread, and the reason says to pass `--repo owner/name`. The gate does not follow where that value reaches. `blocking/followup_pr_dedupe.py` reads the same condition through the same `sets_gh_repository` helper and refuses such a follow-up create, with a reason that says to name the repository with `--repo`. For a follow-up create whose repository comes only from a `GH_REPO=` prefix, it reads the open pull requests of the prefix repository and of the origin remote, since a wrapper can drop the prefix; a prefix value that names no repository, such as `GH_REPO=nonsense`, still gets the origin read, and a deny from the origin read opens with the sentence to name the repository with `--repo owner/name`; when the origin remote names no GitHub repository it reads the prefix repository only. A duplicate found in origin that way is refused with a reason that says to name the repository with `--repo owner/name` on the create, and a create that names its repository with its own `--repo` reads that repository only. Each head it names through `-H x`, `-Hx`, `-H=x`, `--head x`, `--head=x` or a cluster of short flags such as `-dHx`, `-dH x` or `-fH=x` gets its own check, also a head a later head flag overrides, as gh reads each one. A create that names no head reads `HEAD`. The first-create reading takes a head only from `-H x`, `--head x` or their `=` forms, so a create that gives its head as `-Hx` is also checked on `HEAD`; name the head with `--head` when the working folder is on another branch. `blocking/merge_review_gate_parts/create_folders.py` walks the command to give each create its folder, and `blocking/merge_review_gate_parts/folder_state.py` holds that folder, whether it is followed, and the `pushd` stack. The walk follows `cd`, `chdir`, `Set-Location`, `pushd` and `popd`, also after `builtin` or `command`; the end of a subshell, back to the folder in effect when that subshell opened, also when nested subshells close together on one line or across line breaks; and `eval`, which changes the folder of its own shell. A create inside `bash -c`, `sh -c` or a command substitution runs in the folder before it, also when a line break inside the script comes before it. A `cd` in a background job stays in that job. The walk does not model a folder change in a pipeline member, a brace group, `if`, `case`, `for`, `while`, `until`, `select` or a function body, a folder change after a redirect, or a folder word anywhere but the program place. Each later create in that scope is denied as unreadable. So is each create after a folder the walk cannot name, such as `cd "$VAR"`, `cd -` or a glob, and each create after a folder change the walk cannot follow, such as `popd` with nothing pushed, in any wrapper. A create in a folder that does not exist gets no git answer and is denied as unreadable. Text nested past 16 levels of `bash -c`, `eval` or substitution that names `gh` and a `create` or `new` word, or names `pull_request.py`, gives one create whose folder is not followed, so it is denied as unread. `blocking/merge_review_gate_parts/quoted_lines.py` cuts the text into lines and words as bash reads its quotes: `\"` inside a double-quoted value does not end the value, an ANSI-C `$'...'` value is one word, and a line break ends a command only outside quotes, subshells and substitutions. So the walk reads no command in a heredoc body or in a quoted option value, such as `--body "a; gh pr create --head x"`, with or without a line break inside it. It does read the script that `bash -c`, `sh -c`, `pwsh -Command`, `eval` or `Invoke-Expression` gets, line breaks included, and each `$(...)` or backtick substitution, also one inside a double-quoted value, such as `--body "$(gh pr create --head x)"`, since bash runs those. It reads no substitution bash does not run: one inside single quotes, such as `--body '$(gh pr create --head x)'` or `echo '$(gh pr create --head x)'`, or one whose `$(` or backtick a backslash escapes, such as `echo \$(gh pr create --head x)`. It reads `eval`'s words joined by spaces, as bash does, so `eval 'gh pr create --body "a b"'` is one create, and it reads the words after the last `)` inside a segment, such as the command of a `case` arm, as a create and as a wrapper. A command whose quotes do not close is read both ways the earlier walk read it, POSIX and raw words, as written and with its quoted line breaks joined. `blocking/merge_review_gate_parts/wrapped_creates.py` reads each wrapped inner text once more in POSIX words and gives a create the walk did not place as one whose folder is not followed; it skips the command itself, each closed text with its line breaks read as spaces, and each text unwrapped from one, so a heredoc body inside `--body "$(cat <<'B' ...)"` runs no command and `bash -c 'gh pr create\nls'` is one create. The gate also keeps the earlier first-create reading: the first `gh pr create` or `pull_request.py create` the raw text spells outside `--title`, `--body` and `--body-file` values, in the folder of the last `cd`, `pushd`, `Set-Location` or `sl` before it, with its first `--repo` and its first `-H` or `--head` given with a space or `=`. That reading is one more check unless a walk check equals it. So these are checked on the head that first create names: `echo gh pr create --head x | bash`; a create in a heredoc fed to `bash` or `.`; and the first create words after `;`, `&&`, `||` or `|` inside a quoted value, such as a `git commit -m` message. Beyond that first-create reading, the gate does not read: a create a shell gets through its input, a here-string, a heredoc, a process substitution such as `bash <(echo ...)` or a script file; a create in a command string that a program other than `bash`, `sh`, `pwsh`, `powershell`, `cmd /c`, `eval`, `Invoke-Expression`, `find -exec`, `xargs` or `Start-Process` runs, such as `zsh -c`, `su -c`, `ssh`, `watch`, `python -c` or a git alias; or a program name that a variable, alias or function holds. These forms are read in part. A `case` arm inside a double-quoted substitution, such as `--body "$(case x in x) gh pr create ;; esac)"`, is cut at the arm's `)`, so its create is not read. An arithmetic `<<` followed by a word, such as `$((1<<x))`, opens a heredoc for the walk, so the lines after it up to a line `x` are not read; the first-create reading still checks the first create in the raw text. A substitution in an unquoted heredoc body fed to a plain command such as `cat` is not walked; the first-create reading checks the first create there with the closing `)` kept in its head, so that head is unreadable and denied. A create in a heredoc fed to `.` or `bash` is checked only through the first-create reading: it reads `--repo` and a `--head` or `-H` given with a space or `=`, checks a `-Hx` or clustered head as `HEAD`, and does not read a create after the first. The words of a quoted option value are not commands, so a create in one is not read and needs no review. Once the gate knows the call opens or merges a pull request, a check that raises denies the call with the fault reason, which names the error type. A payload read that raises is denied the same way for a `create_pull_request`, `merge_pull_request` or `enable_pr_auto_merge` tool, and for a shell command whose text holds the word `gh` and a `create`, `new` or `merge` word anywhere, or names `pull_request.py`. Any other payload read fault goes to the shared helper. A call that opens and merges nothing gets no such deny. An open pass also counts when every finding is ruled out: `merge_review_receipt.py <owner/name> open <head> <count> --ruled-out <count> --rulings <path:line,...>`, with a different `path:line` for each finding, checked against the file at that head when the pass is recorded. A merge pinned to that same head passes on the open receipt only when the pass found zero findings.
|
|
25
25
|
- `blocking/artifact_dark_mode_gate.py` denies an Artifact page publish when the page, inside the publish wrapper, keeps a light body or shows text under 4.5:1 contrast in dark mode. `blocking/artifact_dark_mode_render.cjs` renders the page in headless Chromium for it.
|
|
26
|
+
- `blocking/artifact_current_page_gate.py` denies an Artifact page publish that draws one item more than once, and names each item with its count. `blocking/artifact_repeated_item_render.cjs` renders the page in headless Chromium for it, with a 400 pixel stand-in for each image. An item is an `<img>` drawn larger than 64 pixels on a side, counted by the file its `src` names, so `screens/a.png` and `./screens/a.png` are one item, or a drawn element with a `data-item` key. An image in a closed `<details>`, a closed `<dialog>` or a hidden element is not drawn. The gate applies the call's `edits` before it reads the page, all or none, as the tool does. It also denies a publish with no `url` when the page has a recorded link, and names the link and where it is kept: a `<meta name="artifact-link">` tag in the page whose content is an artifact link, in either attribute order, the record file beside the page, or the record of a page renamed in the same folder with the same `<title>`. A record whose page file is still there belongs to a separate page and is skipped. A record with no artifact link in it, or an old-name record whose link the record of another page in the folder still holds, is skipped too. A publish whose `url` does not match `ARTIFACT_LINK_PATTERN` is treated as a publish with no `url`. The refusal names every place that holds a link, with the other link beside a place that holds a different one, and says to remove them only when the user asks for a separate copy or says the page at the link was deleted. An allowed publish that passes `url` gets one context note: rebuild the page from the current state.
|
|
27
|
+
- `session/artifact_link_record.py` writes `<page file>.artifact-link.json` beside the page after a page publish, with the link and the page title. The link is the call's `url`, else the `url` field of the publish result, else the first artifact link in the result text. It prints nothing and never blocks.
|
|
26
28
|
- `blocking/send_user_file_worktree_root_gate.py` denies a `SendUserFile` call that names a file outside the session's working directory, the `cwd` in the hook payload. The desktop file pane opens a sent file only under that folder and shows "Couldn't find this file" for any other path. The reason lists each outside file and names `<cwd>\.scratch\` as the folder to copy it into.
|
|
27
29
|
- `blocking/sent_file_question_gate.py` denies a `SendUserFile` call or an `Artifact` page publish when a Markdown, text, or HTML file it sends asks the user a question. A question is a heading that opens a question or decision section, such as "Open questions" or "Decisions needed", or a sentence that ends with a question mark (or a full-width one) and names the reader, opens with a request such as "Should I", "Can I", "Thoughts", or "Proceed", asks what works best ("What release date works best?"), or offers a choice with "or". A heading such as "Need your input" also counts, as do a heading that asks the reader something with no paragraph below it ("## Which works best?"), a question heading in the writer's or the reader-addressed voice whatever follows it ("## What should I do next?", with should I or we, shall I or we, do, would, will, or can you, want me, your, or prefer), a sentence that asks for a reply on its own ("Let me know which you prefer.", "Send me your pick."), and a sentence that opens with reply, answer, respond, choose, pick, select, or decide and offers yes or no, one of two or of these, or a choice between one-letter, one-digit, or "option" labels ("Choose A or B."); a reply verb after "please" or a comma that offers yes or no ("Please answer yes or no.") counts too; "Select one or more archives." and "Choose Color or Image." are steps and pass. A sentence or heading over 2,000 characters is read through its first and last 400 characters. Reader-voice and subject headings such as "## Can I run it offline?" and "## Why does this exist?" with a paragraph below them, `summary` and `dt` labels with a paragraph below them, quotes, fenced and indented code, a question the next sentence answers with yes or no, a question that asks for no wish or go-ahead with a statement after it, such as a frequently asked question and its answer, a checklist item that asks whether the reader did a piece of work ("- [ ] Did you run the tests?" with a task box or beside another question item: "Did you" or "Have you" and a work verb such as ran, saved, pushed, or updated, with no word such as ok, fine, ready, ship, merge, delete, approve, confirm, choice, or signed off; a checkbox `input` in a page counts as a task box), a `Q:` line with an `A:` line below it, and a table cell with an answer cell after it pass. A publish that declares the `db` or `artifact` capability, such as option cards that save the answer, passes; other capabilities do not. A file over 256 KB is read through its first and last 128 KB, and the refusal or a system message says so. A file that is not UTF-8 text passes. The read has a 4 s budget; when the HTML parser does not finish, a second read strips the tags in one pass with no parser, with another 4 s, and only when that also runs out does the call run, with a system message that the files went unchecked. The reason quotes at most 160 characters of the question and says to ask it through the question tool and send the file again without it.
|
|
28
30
|
- `blocking/question_brief_gate.py` denies an `AskUserQuestion` call when the assistant chat text before it in the same turn runs over 450 words or 15 list or table items. A turn starts at a message the user types, at a result of an earlier question call, or at the first input after an `end_turn` reply or a `stop_hook_summary` entry. A Stop hook that holds the turn adds no input, so the text before a held stop still counts. Each bullet or numbered line is one item, each table row below the header is one item, and lines inside a code fence are not items. The reason names each limit passed and says to shorten the brief or split the question. The gate polls the transcript for up to 3 seconds for its own call and passes when the call never appears. A subagent call, an unreadable payload, and a missing or unreadable transcript, or one that is not a regular file, pass at once. The gate reads only the last 1 MB of the transcript, so a long review followed by more than 1 MB of tool output in the same turn is not seen.
|
|
@@ -50,6 +52,8 @@ This family stops a chat reply, edited message, or tool call when its payload br
|
|
|
50
52
|
- `blocking/build_eval_before_eval_write_gate.py` runs on `PreToolUse`, matcher `Write|Edit|MultiEdit|NotebookEdit|apply_patch`, timeout `10` seconds in `hooks.json`. Its transcript scan reads the clock before each block and each line, and stops at the first such read after 5 s, so a line already being parsed finishes first. A scan ended by that stop, or by a line over 16 MiB, counts as no build-eval call shown.
|
|
51
53
|
- `blocking/merge_review_gate.py` runs on `PreToolUse`, matcher `Bash|PowerShell|mcp__.*__(create_pull_request|merge_pull_request|enable_pr_auto_merge)`, timeout `30` seconds in `hooks.json`. A `create_pull_request` call reads its head branch from GitHub with `git ls-remote` over https, so the working folder does not matter. The lookup runs in an empty folder, so no checkout's git config applies, takes no terminal prompt, and gets 5 seconds or the time left before the shared deadline, whichever is less. It runs through the same child read as the local git reads, so a lookup that times out ends `git` and every helper it started, in its own process group on POSIX and its job object on Windows, and the deny says that a second run may pass. A fork head `user:branch` reads the repository of `user`. A branch GitHub does not list, or a lookup that fails, resolves in the working folder as a shell open does, as `origin/<branch>` first. Each local git read gets 3 seconds or the time left, whichever is less, and the `origin` read runs once per folder. Every read answers to one deadline 20 seconds after the gate starts, so all reads end by about 21 seconds, inside the 30 second timeout. A read with no time left is skipped. A head the gate could not read is denied as unreadable. When a read stopped at its own timeout or at the shared deadline, the deny adds that a second run may pass. A read that fails fast, such as a git error, does not add that sentence.
|
|
52
54
|
- `blocking/artifact_dark_mode_gate.py` runs on `PreToolUse`, matcher `Artifact`, timeout `60` seconds in `hooks.json`.
|
|
55
|
+
- `blocking/artifact_current_page_gate.py` runs on `PreToolUse`, matcher `Artifact`, timeout `60` seconds in `hooks.json`.
|
|
56
|
+
- `session/artifact_link_record.py` runs on `PostToolUse`, matcher `Artifact`, timeout `10` seconds in `hooks.json`.
|
|
53
57
|
- `blocking/send_user_file_worktree_root_gate.py` runs on `PreToolUse`, matcher `SendUserFile`, timeout `10` seconds in `hooks.json`.
|
|
54
58
|
- `blocking/sent_file_question_gate.py` runs on `PreToolUse`, matcher `SendUserFile|Artifact`, timeout `10` seconds in `hooks.json`. It runs in Claude Code only.
|
|
55
59
|
- `blocking/turn_review_gate.py` runs on `UserPromptSubmit`, matcher empty, timeout `30` seconds, and on `Stop`, timeout `300` seconds with `asyncRewake`, `rewakeMessage` and `rewakeSummary`, in `hooks.json`. Claude Code still enforces the timeout on an `asyncRewake` hook. The review itself times out after 240 seconds. A stop's work stops at 280 seconds and a prompt's at 25. The installer keeps it out of the Codex prompt hooks through `ALL_CODEX_EXCLUDED_USER_PROMPT_SUBMIT_HOOK_SCRIPT_NAMES`, and it writes no turn review into the Codex or Cursor Stop hooks. A copy of `~/.claude/settings.json` that Cursor imports is not tested.
|
|
@@ -80,6 +84,7 @@ Preconditions:
|
|
|
80
84
|
- **Eval write.** Input is a Write to `evals/cases.json` with no build-eval call in the transcript. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_build_eval_before_eval_write_gate.py -q`. The adjacent test observes a denial, then an allow once the call appears; a write outside an eval path passes.
|
|
81
85
|
- **Clean review.** Input is a merge with no receipt, a receipt with findings, a merge pinned to a newer head, an unpinned merge, or auto-merge with no head. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_merge_review_gate.py -q`. The adjacent test observes a denial for each; a merge or auto-merge pinned to the clean head and a merge in an exempt repository pass. A `gh pr merge --disable-auto` call passes; a merge chained with it, or one that adds `--disable-auto=false` or `-sb --disable-auto`, is still checked. An open with no clean open receipt at its head is denied; an open reviewed clean at its head, and a merge pinned to that head, pass. An open pass whose every finding has its own `path:line` ruling lets the open through; a merge pinned to that head is denied, since only a zero-finding open pass carries to the merge. An open whose branch neither GitHub nor the working folder holds is denied as unreadable. Two chained creates whose second head has no clean open receipt are denied with the second head named. An open whose reads ran past the shared deadline, or whose git read hit its own timeout, is denied as unreadable, with the sentence that a second run may pass; a git read that fails fast gets no such sentence. Every create spelling is denied when its head has no clean pass. A create whose last repository flag names a repository with no clean pass is denied, however many repository flags come before it. A create that names an unreviewed second head with `-Hx`, a repeated `--head` or `--head=` is denied, and so is one whose short-flag cluster, such as `-dHx`, names an unreviewed head. A flag word that is another option's value, such as `--title --repo=x/y`, `--title -Hx` or `-bHx`, is read as that value. A create with no repository of its own in a command that sets `GH_REPO` elsewhere, through an assignment or `export`, is denied as unread with the `--repo owner/name` sentence, and one that names `--repo` or a `GH_REPO=` prefix is read. A first create behind a `GH_REPO=` prefix is denied when origin has no clean pass, also when the prefix repository is exempt or reviewed clean. A later create is denied the same way when the walk reaches it: behind `env`, `sudo` and `timeout` with their options, and behind chains of the launchers `nohup`, `nice`, `stdbuf`, `setsid` and `time` whose options hold their value in the same word or take a number, such as `nice -n 5 env -u GH_REPO stdbuf -o0`. A create after the first one, behind a program the walk does not step over, such as `ionice`, is not read. A create the walk reaches whose prefix value names no repository, such as `GH_REPO=nonsense`, is denied for origin with the `--repo owner/name` sentence. A first create the walk does not reach, such as one behind `stdbuf -o L`, is denied for origin through the first-create reading without that sentence, and a prefixed create after an unprefixed create on the same head is denied on the unprefixed create, also without it. A create that names an exempt repository with its own `--repo` passes. A create after a subshell or `popd` reads the starting folder, also after nested subshells close on one line or across line breaks, and `builtin cd` and `command cd` are followed. A create in a `case` arm, also one with a root `-R`, is checked on its head. A create inside a single-quoted or backslash-escaped substitution is not read, and one inside a double-quoted or bare substitution is checked. A create after `cd "$VAR"`, or after a `cd` in a piped brace group, `if`, `case`, a loop, a function body or after a redirect, is denied as unreadable, and so is a create nested past the unwrap depth. The first create the raw text spells is also checked with the earlier first-create reading, which a frozen table of commands pins, so a create fed to `bash` through a pipe or a heredoc is checked on its head. A later create on an unreviewed head is denied after an ANSI-C `$'...'` value, after a line break inside `bash -c`, `sh -c` or `eval`, and after a double-quoted value whose substitution holds a quote character. A later create inside a quoted value, such as `--body "say \"x\" ; gh pr create --head y"`, is not read. A here-string or piped create beyond the first, a create in a command string run by a program the walk does not unwrap, and a program name in a variable or alias are not read. An open or a merge whose check raises is denied with the fault reason, also when the text spells the create or merge in a form the strict pattern misses, and so is an MCP merge whose `pullNumber` overflows.
|
|
82
86
|
- **Dark mode page.** Input is an Artifact publish of a page that themes `html` but leaves `body` to the publish wrapper. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_artifact_dark_mode_gate.py -q`. The adjacent test observes a denial that names the text and its contrast; a page that themes `body` passes. The render tests skip on a host with no Node Playwright and Chromium.
|
|
87
|
+
- **Current page.** Input is an Artifact publish of `blocking/artifact_current_page_fixtures/piled_review_page.html`, which draws 33 figures from 20 screens through one `<img` tag in its source. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_artifact_current_page_gate.py packages/claude-dev-env/hooks/session/test_artifact_link_record.py -q`. The adjacent test observes a denial that names a screen drawn 3 times; `rebuilt_review_page.html`, with each screen once, passes. A small icon used five times and a thumbnail with a hidden full view pass. A baseline drawn beside each variant, an edit that adds a second draw, and two drawn elements with one `data-item` key are denied. After the record hook runs on a first publish, a publish with no `url` is denied with the recorded link, and the same publish with `url` passes with the note. The render tests skip on a host with no Node Playwright and Chromium.
|
|
83
88
|
- **Sent file root.** Input is a `SendUserFile` call for a file on `Y:\` or in the session scratchpad, with `cwd` set to a worktree. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_send_user_file_worktree_root_gate.py -q`. The adjacent test observes a denial that names the file and `<cwd>\.scratch\`; a file under `<cwd>\.scratch\` passes.
|
|
84
89
|
- **Sent file question.** Input is a `SendUserFile` call or an `Artifact` publish of a file that holds "Should I archive the old branches?" or a "Questions for you" heading. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_sent_file_question_gate.py packages/claude-dev-env/hooks/blocking/test_sent_file_scan.py -q`. The adjacent test observes a denial that quotes the question and names the file; a FAQ heading or list item, a quote, code, an answered question, and a page that declares `db` or `artifact` pass.
|
|
85
90
|
- **Question brief.** Input is a transcript whose turn posts 451 words or 16 bullets before an `AskUserQuestion` call. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_question_brief_gate.py -q`. The adjacent test observes exit 2 with a reason that names the limit passed. A list before a held stop still counts. A short brief, text from an earlier turn, fenced lines, a subagent call, and a missing transcript pass.
|
|
@@ -87,6 +92,7 @@ Preconditions:
|
|
|
87
92
|
|
|
88
93
|
## Gotchas
|
|
89
94
|
|
|
95
|
+
- `blocking/artifact_current_page_gate.py` counts `<img>` elements and `data-item` keys. It does not see a repeated item that has neither, a CSS background image, or a frame of a `<video>` or `<canvas>`. An image with no size set in the page takes the 400 pixel stand-in size and counts as an item. `link_record_path` names a file beside the page file, so a copy of the page in another folder with no `artifact-link` meta tag, or a page written again in a fresh container, has no record there and a publish with no `url` passes.
|
|
90
96
|
- The ask nudge runs only from `hooks.json`, so Codex and Cursor sessions get no turn-start note. Their Stop gate still holds a work turn with no tracker call.
|
|
91
97
|
- The background silence note is the last point before the reply. A Stop hook sees the reply after it is on screen, and holding the text at `MessageDisplay` would need a judgment on whether the exception applies.
|
|
92
98
|
- A headless run whose input is streamed, such as `claude -p --input-format stream-json` or an SDK client, runs the Stop entry in the background, and the host kills it at teardown, so that run loses its review.
|
package/hooks/hooks.json
CHANGED
|
@@ -261,6 +261,11 @@
|
|
|
261
261
|
"type": "command",
|
|
262
262
|
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/blocking/artifact_dark_mode_gate.py",
|
|
263
263
|
"timeout": 60
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
"type": "command",
|
|
267
|
+
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/blocking/artifact_current_page_gate.py",
|
|
268
|
+
"timeout": 60
|
|
264
269
|
}
|
|
265
270
|
]
|
|
266
271
|
},
|
|
@@ -539,6 +544,16 @@
|
|
|
539
544
|
}
|
|
540
545
|
]
|
|
541
546
|
},
|
|
547
|
+
{
|
|
548
|
+
"matcher": "Artifact",
|
|
549
|
+
"hooks": [
|
|
550
|
+
{
|
|
551
|
+
"type": "command",
|
|
552
|
+
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/session/artifact_link_record.py",
|
|
553
|
+
"timeout": 10
|
|
554
|
+
}
|
|
555
|
+
]
|
|
556
|
+
},
|
|
542
557
|
{
|
|
543
558
|
"matcher": "Bash|PowerShell",
|
|
544
559
|
"hooks": [
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Names, patterns and messages for the artifact current page gate and its link record."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from hooks_constants.artifact_dark_mode_gate_constants import BLOCKING_HOOKS_DIRECTORY
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"ARTIFACT_EDITS_INPUT_KEY",
|
|
11
|
+
"ARTIFACT_LINK_PATTERN",
|
|
12
|
+
"ARTIFACT_URL_INPUT_KEY",
|
|
13
|
+
"EDIT_NEW_TEXT_KEY",
|
|
14
|
+
"EDIT_OLD_TEXT_KEY",
|
|
15
|
+
"FULL_RULE_POINTER_TEMPLATE",
|
|
16
|
+
"LINK_META_NAME",
|
|
17
|
+
"LINK_META_SOURCE",
|
|
18
|
+
"LINK_RECORD_SUFFIX",
|
|
19
|
+
"LINK_RECORD_TITLE_KEY",
|
|
20
|
+
"LINK_RECORD_URL_KEY",
|
|
21
|
+
"LINK_SOURCE_JOINER",
|
|
22
|
+
"LINK_SOURCE_WITH_OTHER_LINK_TEMPLATE",
|
|
23
|
+
"MALFORMED_CALL_URL_TEMPLATE",
|
|
24
|
+
"META_CONTENT_ATTRIBUTE",
|
|
25
|
+
"META_NAME_ATTRIBUTE",
|
|
26
|
+
"META_TAG_PATTERN",
|
|
27
|
+
"PAGE_TITLE_PATTERN",
|
|
28
|
+
"REPEATED_ITEM_HEADER",
|
|
29
|
+
"REPEATED_ITEM_LINE_TEMPLATE",
|
|
30
|
+
"REPEATED_ITEM_RETRY_INSTRUCTION",
|
|
31
|
+
"REPEAT_ITEM_KEY",
|
|
32
|
+
"REPEAT_RENDERER_MISSING_HEADER",
|
|
33
|
+
"REPEAT_RENDERER_SCRIPT_PATH",
|
|
34
|
+
"REPEAT_TIMES_KEY",
|
|
35
|
+
"REPUBLISH_NOTE",
|
|
36
|
+
"SECOND_LINK_REASON_TEMPLATE",
|
|
37
|
+
"TAG_ATTRIBUTE_PATTERN",
|
|
38
|
+
"TOOL_RESPONSE_KEY",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
ARTIFACT_URL_INPUT_KEY = "url"
|
|
42
|
+
ARTIFACT_EDITS_INPUT_KEY = "edits"
|
|
43
|
+
EDIT_OLD_TEXT_KEY = "old_str"
|
|
44
|
+
EDIT_NEW_TEXT_KEY = "new_str"
|
|
45
|
+
TOOL_RESPONSE_KEY = "tool_response"
|
|
46
|
+
LINK_RECORD_SUFFIX = ".artifact-link.json"
|
|
47
|
+
LINK_RECORD_URL_KEY = "url"
|
|
48
|
+
LINK_RECORD_TITLE_KEY = "title"
|
|
49
|
+
REPEAT_ITEM_KEY = "item"
|
|
50
|
+
REPEAT_TIMES_KEY = "times"
|
|
51
|
+
ARTIFACT_LINK_PATTERN = re.compile(r"https://claude\.ai/(?:code/)?artifact/[A-Za-z0-9_-]+")
|
|
52
|
+
META_TAG_PATTERN = re.compile(r"<meta\b[^>]*>", re.IGNORECASE)
|
|
53
|
+
TAG_ATTRIBUTE_PATTERN = re.compile(r"""(?<=[\s"'/])([^\s=/>"']+)\s*=\s*(?:"([^"]*)"|'([^']*)')""")
|
|
54
|
+
META_NAME_ATTRIBUTE = "name"
|
|
55
|
+
META_CONTENT_ATTRIBUTE = "content"
|
|
56
|
+
LINK_META_NAME = "artifact-link"
|
|
57
|
+
LINK_META_SOURCE = "the artifact-link meta tag from the page"
|
|
58
|
+
LINK_SOURCE_JOINER = " and "
|
|
59
|
+
LINK_SOURCE_WITH_OTHER_LINK_TEMPLATE = "{source} (it holds {link})"
|
|
60
|
+
MALFORMED_CALL_URL_TEMPLATE = (
|
|
61
|
+
"The url {url} in this call is not an artifact link, so the call counts as a publish with"
|
|
62
|
+
" no url.\n"
|
|
63
|
+
)
|
|
64
|
+
PAGE_TITLE_PATTERN = re.compile(r"<title[^>]*>(.*?)</title>", re.IGNORECASE | re.DOTALL)
|
|
65
|
+
REPEAT_RENDERER_SCRIPT_PATH = BLOCKING_HOOKS_DIRECTORY / "artifact_repeated_item_render.cjs"
|
|
66
|
+
REPEATED_ITEM_HEADER = "This page draws the same item more than once, so the reader cannot tell which entry is current.\n"
|
|
67
|
+
REPEATED_ITEM_LINE_TEMPLATE = "- {item} is drawn {times} times\n"
|
|
68
|
+
REPEATED_ITEM_RETRY_INSTRUCTION = (
|
|
69
|
+
"\nRebuild the page from the current state. Show each item once, and put its status and"
|
|
70
|
+
" every warning that names it on that one entry. Remove each section that repeats an item,"
|
|
71
|
+
" and remove content that a newer result replaced. State at the top what the page shows and"
|
|
72
|
+
" when it was updated. Then publish again to the same link. A small icon that is used many"
|
|
73
|
+
" times needs a width and height of 64 pixels or less."
|
|
74
|
+
)
|
|
75
|
+
SECOND_LINK_REASON_TEMPLATE = (
|
|
76
|
+
"This page already has a link: {link}\n"
|
|
77
|
+
"A publish with no url opens a second link, and the reader then holds two copies."
|
|
78
|
+
" Publish again with url set to that link. A separate page gets its own file and its own"
|
|
79
|
+
" title. When the user asks for a separate copy, or says the page at that link was deleted,"
|
|
80
|
+
" remove {link_source}, then publish with no url."
|
|
81
|
+
)
|
|
82
|
+
FULL_RULE_POINTER_TEMPLATE = ' The full rule is under "Republish a page" in {path_of_page_readme}.'
|
|
83
|
+
REPEAT_RENDERER_MISSING_HEADER = (
|
|
84
|
+
"The page did not render for its repeated item check before publish: "
|
|
85
|
+
)
|
|
86
|
+
REPUBLISH_NOTE = (
|
|
87
|
+
"This publish updates a page a person has already seen. Rebuild the page from the current"
|
|
88
|
+
" state: each item once, with its status and warnings on that one entry; content that a"
|
|
89
|
+
" newer result replaced removed; and a line at the top that says what the page shows and"
|
|
90
|
+
" when it was updated."
|
|
91
|
+
)
|
|
@@ -11,6 +11,7 @@ __all__ = [
|
|
|
11
11
|
"ARTIFACT_FILE_PATH_INPUT_KEY",
|
|
12
12
|
"ARTIFACT_PUBLISH_ACTION",
|
|
13
13
|
"ARTIFACT_TOOL_NAME",
|
|
14
|
+
"BLOCKING_HOOKS_DIRECTORY",
|
|
14
15
|
"BLOCK_EXIT_CODE",
|
|
15
16
|
"DARK_MODE_FAILURE_HEADER",
|
|
16
17
|
"GATE_EVENT_NAME",
|
|
@@ -34,9 +35,8 @@ ARTIFACT_FILE_PATH_INPUT_KEY = "file_path"
|
|
|
34
35
|
ALL_SKIPPED_PUBLISH_INPUT_KEYS = ("asset", "type_url")
|
|
35
36
|
HTML_FILE_SUFFIX = ".html"
|
|
36
37
|
GATE_EVENT_NAME = "PreToolUse"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)
|
|
38
|
+
BLOCKING_HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent / "blocking"
|
|
39
|
+
RENDERER_SCRIPT_PATH = BLOCKING_HOOKS_DIRECTORY / "artifact_dark_mode_render.cjs"
|
|
40
40
|
PUBLISH_SKELETON_PREFIX = (
|
|
41
41
|
"<!doctype html><html><head><meta charset=utf8>"
|
|
42
42
|
'<meta name=viewport content="width=device-width,initial-scale=1">'
|
|
@@ -105,6 +105,14 @@ ALL_QUESTION_TOOL_NAMES = frozenset(
|
|
|
105
105
|
"request_user_input_async",
|
|
106
106
|
}
|
|
107
107
|
)
|
|
108
|
+
ALL_TRACKER_TOOL_LISTING_NAMES = frozenset({*ALL_TRACKER_TOOL_NAMES, CCD_PLAN_TOOL_NAME, CODEX_PLAN_TOOL_NAME})
|
|
109
|
+
ATTACHMENT_KEY = "attachment"
|
|
110
|
+
PROMPT_SNAPSHOT_ATTACHMENT_TYPE = "prompt_snapshot"
|
|
111
|
+
PROMPT_SNAPSHOT_TOOLS_KEY = "tools"
|
|
112
|
+
DEFERRED_TOOLS_DELTA_ATTACHMENT_TYPE = "deferred_tools_delta"
|
|
113
|
+
ALL_DEFERRED_TOOLS_DELTA_NAME_KEYS = ("addedNames", "readdedNames", "surfacedNames", "loadedBeforeSummaryNames")
|
|
114
|
+
DEFERRED_TOOLS_RECORD_ATTACHMENT_TYPE = "deferred_tools_record"
|
|
115
|
+
DEFERRED_TOOLS_RECORD_ENTRIES_KEY = "entries"
|
|
108
116
|
SESSION_TITLE_TOOL_SUFFIX = "__set_session_title"
|
|
109
117
|
CODEX_LOCAL_SHELL_CALL_TYPE = "local_shell_call"
|
|
110
118
|
ALL_CODEX_TOOL_CALL_TYPES = frozenset({CODEX_FUNCTION_CALL_TYPE, "custom_tool_call", CODEX_LOCAL_SHELL_CALL_TYPE})
|
|
@@ -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
|
|
@@ -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
|
|
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"],
|