CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 10s
CI & Build / integration (push) Successful in 47s
CI & Build / TypeScript typecheck (push) Successful in 53s
CI & Build / Python tests (push) Failing after 1m1s
CI & Build / Build & push image (push) Skipped
All six hooks scoped their requests one way: `git remote get-url origin`,
resolved server-side through the repo bindings. That key does not exist
outside a git repo, so a session in a plain directory was unscoped in every
hook at once — no project context, no prior-art scoping, no project rules —
and silently, because a missing remote is indistinguishable from a remote
nobody bound.
A `.scribe` file is the second key, read by the shared scribe_scope_query so a
directory scopes the same way everywhere:
{"instance": "https://scribe.example.com", "project_id": 2, "project": "…"}
`instance` is why the file is not just a number: an id is a different project
on every Scribe, so a marker that travels — a copied directory, a shared
machine, a repo someone else clones — would otherwise scope the session to the
wrong project without a word. Compared host-only, and a mismatch drops the id:
no project beats the wrong project. A bare integer is accepted too, since it
is what a person writes by hand. The marker beats a git remote — someone put
the file there on purpose — which is also how a directory overrides its
binding.
Two things it found on the way:
* An explicit project_id that did not resolve rendered NO message at all —
the branch hung off `if project_id` as an `elif`, so a caller holding a
pointer it believed in got a context that silently omitted the project it
had asked for. Now reported.
* The refusal reason was a global set inside a function every caller reads
through `$( )`. The assignment died with the subshell, leaving the caller
to read an unset variable under `set -u` — which aborts the hook and costs
the whole session's SessionStart context, to fetch a warning about a file.
It comes back through stdout with the id instead, and a test pins it.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01821k5B3Ysecp9fNYs92Kuy
115 lines
5.1 KiB
Bash
115 lines
5.1 KiB
Bash
#!/usr/bin/env bash
|
|
# Scribe — PreToolUse rule arm for ACTIONS (#3476).
|
|
#
|
|
# The sibling of scribe_prior_art.sh. That hook is registered on Write|Edit and
|
|
# asks "what is recorded about the file being written". This one asks "does a
|
|
# standing rule speak to the command about to be run" — the question nothing
|
|
# could ask before, and the reason every rule about which tool to reach for had
|
|
# to live in the preload instead, back when there was one.
|
|
#
|
|
# WHY A HOOK AND NOT AN INSTRUCTION. A reflex generates no query (note #3089):
|
|
# you reach for `curl` confidently, with no moment of doubt, so a surface that
|
|
# waits to be asked never fires. Here nothing is asked — the tool call IS the
|
|
# query, and the reflex has to become a tool call before it can do anything.
|
|
#
|
|
# SILENT ON OUTAGE, deliberately, unlike the prior-art hook. A write is
|
|
# occasional; a Bash call is not, and an "instance did not answer" line before
|
|
# every command is the noise that gets a channel muted. scribe_prior_art.sh
|
|
# still speaks for both when the instance is down.
|
|
#
|
|
# Env:
|
|
# SCRIBE_URL / SCRIBE_TOKEN override for the settings.json dogfooding path.
|
|
|
|
command -v jq >/dev/null 2>&1 || exit 0
|
|
command -v curl >/dev/null 2>&1 || exit 0
|
|
|
|
# PreToolUse delivers { session_id, cwd, tool_name, tool_input: {...}, ... }
|
|
event=$(cat 2>/dev/null || true)
|
|
tool_name=$(printf '%s' "$event" | jq -r '.tool_name // empty' 2>/dev/null) || exit 0
|
|
session_id=$(printf '%s' "$event" | jq -r '.session_id // empty' 2>/dev/null) || session_id=""
|
|
event_cwd=$(printf '%s' "$event" | jq -r '.cwd // empty' 2>/dev/null) || event_cwd=""
|
|
|
|
[ -n "$tool_name" ] || exit 0
|
|
|
|
# The action, as text. `.command` is Bash's field; the fallbacks let the matcher
|
|
# in hooks.json widen to other tools without this script changing — which is the
|
|
# whole reason the server side takes a name and a string rather than a schema.
|
|
command_text=$(printf '%s' "$event" | jq -r '
|
|
.tool_input.command //
|
|
.tool_input.url //
|
|
.tool_input.prompt //
|
|
empty' 2>/dev/null) || command_text=""
|
|
|
|
[ -n "$command_text" ] || exit 0
|
|
|
|
# shellcheck source=plugin/hooks/scribe_defs.sh
|
|
. "$(dirname "${BASH_SOURCE[0]}")/scribe_defs.sh"
|
|
|
|
# scribe_config, not a hand-rolled pair of parameter expansions: it also treats
|
|
# an UNEXPANDED `${...}` placeholder as unset, which would otherwise be sent as
|
|
# a garbage Bearer token and 401 on every call (#2198's class).
|
|
scribe_config || exit 0
|
|
|
|
# Bounded before encoding: a heredoc or a pasted script can be enormous, and
|
|
# the verb and its target — the part a rule is about — sit at the front. The
|
|
# server bounds it again; this keeps a huge payload off the wire in the first
|
|
# place. `head -c`, never `cut -c`: cut truncates each LINE and caps nothing.
|
|
command_text=$(printf '%s' "$command_text" | head -c 2000)
|
|
|
|
# -sRr, never -rR: jq -R without -s reads LINE BY LINE, so a multi-line command
|
|
# would encode per line and join with raw newlines — an invalid URL.
|
|
cmd_enc=$(printf '%s' "$command_text" | jq -sRr '@uri' 2>/dev/null) || exit 0
|
|
tool_enc=$(printf '%s' "$tool_name" | jq -sRr '@uri' 2>/dev/null) || exit 0
|
|
|
|
repo_q=""
|
|
lookup_dir=${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}
|
|
scope=$(scribe_scope_query "$lookup_dir")
|
|
[ -n "$scope" ] && repo_q="&${scope}"
|
|
|
|
# THE SHARED SESSION LEDGER, and the thing most worth getting right here.
|
|
#
|
|
# scribe_prior_art.sh keeps the rules it has already named in
|
|
# <state>/<sid>.rules.ids and passes them as exclude_rule_ids. This hook reads
|
|
# and appends to that SAME file rather than keeping its own: two ledgers would
|
|
# mean a rule named by one arm gets re-offered by the other, and the hint that
|
|
# fires most often is exactly the one that must not repeat itself.
|
|
#
|
|
# The directory keeps the prior-art name on purpose — renaming it would orphan
|
|
# every live session's state for a cosmetic gain.
|
|
state_dir="${TMPDIR:-/tmp}/scribe-priorart"
|
|
mkdir -p "$state_dir" 2>/dev/null || true
|
|
rulefile=""
|
|
rule_exclude_q=""
|
|
if [ -n "$session_id" ]; then
|
|
safe_sid=$(printf '%s' "$session_id" | tr -c 'A-Za-z0-9._-' '_')
|
|
rulefile="$state_dir/${safe_sid}.rules.ids"
|
|
# Ageing, not a flat read (#3751): an id named two hours ago is not one the
|
|
# session is still holding. scribe_rules_live carries the reasoning.
|
|
rule_seen=$(scribe_rules_live "$rulefile")
|
|
[ -n "$rule_seen" ] && rule_exclude_q="&exclude_rule_ids=${rule_seen}"
|
|
fi
|
|
|
|
# `|| exit 0` here, unlike the prior-art hook: there is no local arm whose
|
|
# finding would be discarded, and an outage line before every command is worse
|
|
# than silence. See the header.
|
|
body=$(curl -fsS --max-time 5 \
|
|
-H "Authorization: Bearer ${token}" \
|
|
"${url%/}/api/plugin/tool-rules?tool=${tool_enc}&command=${cmd_enc}${repo_q}${rule_exclude_q}" 2>/dev/null) || exit 0
|
|
|
|
context=$(printf '%s' "$body" | jq -r '.context // empty' 2>/dev/null) || exit 0
|
|
[ -n "$context" ] || exit 0
|
|
|
|
# Remember what was named so it is not repeated this session.
|
|
if [ -n "$rulefile" ]; then
|
|
printf '%s' "$body" | jq -r '(.rule_ids // [])[]?' 2>/dev/null \
|
|
| scribe_rules_append "$rulefile"
|
|
fi
|
|
|
|
jq -cn --arg ctx "$context" '{
|
|
hookSpecificOutput: {
|
|
hookEventName: "PreToolUse",
|
|
additionalContext: $ctx
|
|
}
|
|
}' 2>/dev/null || true
|
|
exit 0
|