CI & Build / TypeScript typecheck (push) Canceled after 0s
CI & Build / Plugin hooks (push) Canceled after 0s
CI & Build / Python lint (push) Canceled after 0s
CI & Build / Python tests (push) Canceled after 0s
CI & Build / integration (push) Canceled after 0s
CI & Build / Build & push image (push) Canceled after 0s
Each rule line a hook delivers now records the plugin version that asked for it (the X-Grimoire-Plugin header every hook request sends). The open rate used to order rules, and the delivery history the weighing pass reads, count only deliveries that carry one. A plugin before 2026.11.22.2312 never reported an open of a rule a hook had just named, so every delivery made to such a session read as unopened. The server shipped delivery recording from dev on 2026-11-22. The plugin ships from main, and a running session keeps the plugin it started with, so those sessions kept arriving. They are why opens read about 1 in 600. Existing rows have no version and drop out of the rate; nothing is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.1 KiB
Bash
121 lines
5.1 KiB
Bash
#!/usr/bin/env bash
|
|
# Grimoire plugin — Stop hook: the turn's new shapes are judged by the agent
|
|
# that wrote them, at the start of its next turn.
|
|
#
|
|
# The write hooks (grimoire_prior_art.sh, grimoire_after_write.sh) append every
|
|
# definition a write names to a session ledger, `<sid>.written.ids`
|
|
# (`path<TAB>kind<TAB>name`, see grimoire_written_append). At the end of the
|
|
# turn this hook sends that ledger to the instance, which answers with what
|
|
# nobody has judged yet as a `note`. The hook leaves the note for
|
|
# grimoire_autoinject.sh, which carries it into the next prompt's payload: the
|
|
# agent that built the code is the one participant who knows what it is, and
|
|
# the next turn of the same session still does.
|
|
#
|
|
# IT NEVER HOLDS THE REPLY. A held reply is rewritten, and a hold on every turn
|
|
# that wrote code made most replies cost two. The question waits one prompt
|
|
# instead, and is asked inside that prompt's budget.
|
|
#
|
|
# THE STATE, in <TMPDIR>/grimoire-shapecheck:
|
|
# <sid>.note — the note, until a prompt carries it (the prompt hook
|
|
# removes it once the instance has answered);
|
|
# <sid>.asked — the lines that note named. At the first stop after the note
|
|
# was carried, they go back as `phase=after`, which records
|
|
# whether they were judged, and are then forgotten.
|
|
# A note no prompt has carried yet (two stops in a row) is asked again with
|
|
# that turn's writes added, so nothing is dropped and nothing is asked twice.
|
|
#
|
|
# Silent on outage: the ledger is kept when the instance could not be reached,
|
|
# so the question is asked at the next stop that can be answered.
|
|
#
|
|
# Config (same as the other hooks):
|
|
# CLAUDE_PLUGIN_OPTION_API_ENDPOINT base URL, no trailing slash
|
|
# CLAUDE_PLUGIN_OPTION_API_TOKEN fmcp_ API key (sensitive)
|
|
# GRIMOIRE_URL / GRIMOIRE_TOKEN override for the settings.json dogfooding path.
|
|
set -uo pipefail
|
|
|
|
command -v curl >/dev/null 2>&1 || exit 0
|
|
|
|
# shellcheck source=plugin/hooks/grimoire_defs.sh
|
|
. "$(dirname "${BASH_SOURCE[0]}")/grimoire_defs.sh"
|
|
|
|
# Stop delivers { session_id, transcript_path, cwd, hook_event_name, stop_hook_active }.
|
|
event=$(cat 2>/dev/null || true)
|
|
event_flat=$(printf '%s' "$event" | grimoire_json_flat)
|
|
session_id=$(grimoire_json_pick "$event_flat" '.session_id')
|
|
active=$(grimoire_json_pick "$event_flat" '.stop_hook_active')
|
|
event_cwd=$(grimoire_json_pick "$event_flat" '.cwd')
|
|
[ -n "$session_id" ] || exit 0
|
|
# The stop after another hook held the reply: this turn's writes were already
|
|
# sent at the stop before it.
|
|
[ "$active" = "true" ] && exit 0
|
|
|
|
safe_sid=$(printf '%s' "$session_id" | tr -c 'A-Za-z0-9._-' '_')
|
|
ledger="${TMPDIR:-/tmp}/grimoire-priorart/${safe_sid}.written.ids"
|
|
state_dir="${TMPDIR:-/tmp}/grimoire-shapecheck"
|
|
mkdir -p "$state_dir" 2>/dev/null || true
|
|
note_file="$state_dir/${safe_sid}.note"
|
|
asked_file="$state_dir/${safe_sid}.asked"
|
|
|
|
# A note no prompt has carried: its lines join this turn's, asked afresh.
|
|
if [ -f "$note_file" ]; then
|
|
if [ -s "$asked_file" ]; then
|
|
mkdir -p "$(dirname "$ledger")" 2>/dev/null || true
|
|
cat "$asked_file" >> "$ledger" 2>/dev/null || true
|
|
fi
|
|
rm -f "$note_file" "$asked_file" 2>/dev/null || true
|
|
fi
|
|
|
|
[ -s "$asked_file" ] || [ -s "$ledger" ] || exit 0
|
|
grimoire_config || exit 0
|
|
|
|
dir="${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}"
|
|
scope_q=""
|
|
scope=$(grimoire_scope_query "$dir")
|
|
[ -n "$scope" ] && scope_q="&${scope}"
|
|
# The repo rides along even when a marker names the project: the instance
|
|
# needs it to tell the agent which repo a just-written shape belongs to.
|
|
case "$scope" in
|
|
repo=*) ;;
|
|
*)
|
|
remote=$(git -C "$dir" remote get-url origin 2>/dev/null || true)
|
|
if [ -n "$remote" ]; then
|
|
remote_enc=$(printf '%s' "$remote" | grimoire_urlenc) || remote_enc=""
|
|
[ -n "$remote_enc" ] && scope_q="${scope_q}&repo=${remote_enc}"
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
# Unique lines, oldest first, capped: the request stays a GET (a read-scoped
|
|
# key runs the plugin), and a turn that wrote more than this generated code.
|
|
_unique() { awk '!seen[$0]++' "$1" 2>/dev/null | head -n 80; }
|
|
|
|
# The note a prompt carried: were its lines judged since?
|
|
if [ -s "$asked_file" ]; then
|
|
asked=$(_unique "$asked_file")
|
|
asked_enc=$(printf '%s' "$asked" | grimoire_urlenc) || asked_enc=""
|
|
if [ -n "$asked_enc" ] && curl -fsS --max-time 4 \
|
|
-H "Authorization: Bearer ${token}" \
|
|
-H "X-Grimoire-Plugin: ${plugin_version:-}" \
|
|
"${url%/}/api/plugin/shape-check?phase=after&written=${asked_enc}${scope_q}" \
|
|
>/dev/null 2>&1; then
|
|
rm -f "$asked_file" 2>/dev/null || true
|
|
fi
|
|
fi
|
|
|
|
[ -s "$ledger" ] || exit 0
|
|
written=$(_unique "$ledger")
|
|
written_enc=$(printf '%s' "$written" | grimoire_urlenc) || exit 0
|
|
answer=$(curl -fsS --max-time 4 \
|
|
-H "Authorization: Bearer ${token}" \
|
|
-H "X-Grimoire-Plugin: ${plugin_version:-}" \
|
|
"${url%/}/api/plugin/shape-check?phase=check&written=${written_enc}${scope_q}" 2>/dev/null) \
|
|
|| exit 0 # unreachable: keep the ledger
|
|
|
|
note=$(grimoire_json_pick "$(printf '%s' "$answer" | grimoire_json_flat)" '.note')
|
|
if [ -n "$note" ]; then
|
|
printf '%s' "$note" > "$note_file" 2>/dev/null || true
|
|
printf '%s\n' "$written" > "$asked_file" 2>/dev/null || true
|
|
fi
|
|
rm -f "$ledger" 2>/dev/null || true
|
|
exit 0
|