Files
bvandeusenandClaude Opus 5.5 c5eeec471f
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / TypeScript typecheck (push) Successful in 53s
CI & Build / integration (push) Successful in 1m2s
CI & Build / Python tests (push) Failing after 1m26s
CI & Build / Build & push image (push) Skipped
rename(547): the plugin is grimoire — manifest, MCP key, hooks, env, skills
Step 3 of the Scribe -> Grimoire rename.

- Plugin `grimoire` in marketplace `grimoire-plugin`, MCP server key
  `grimoire`, so tools reach a session as mcp__plugin_grimoire_grimoire__*.
- hooks/scribe_* -> hooks/grimoire_*, their shell functions and the TMPDIR
  ledgers with them; SCRIBE_URL / SCRIBE_TOKEN / SCRIBE_HOOK_DIR /
  SCRIBE_LEDGER_DIRS -> GRIMOIRE_*; the project marker is `.grimoire`.
- skills/using-scribe -> using-grimoire, /grimoire:sync, stored processes
  arrive as grimoire-proc-<slug>; moment_actions names them the same way.
- Migration 0124 rewrites stored skill names (rule_moments,
  rule_moment_judgments, moment_mappings) from scribe:/scribe-proc- to
  grimoire:/grimoire-proc-, so a rule mounted on a skill keeps firing.
- Settings' install commands and MCP-only defaults, the docs and the
  tests follow.
- scripts/check_plugin.py drops the smoke event for a report-check hook
  that has no file in plugin/hooks.

Kept on purpose: the secret-sealing label, and the names that let backups
written as Scribe restore and be counted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:52:55 -04:00

155 lines
7.0 KiB
Bash

#!/usr/bin/env bash
# Grimoire — PreToolUse moment arm: rules MOUNTED on a moment arrive when the
# moment happens.
#
# The other PreToolUse arms search: they turn the act into a query and hope a
# rule's words resemble it. A mount needs no resemblance. The operator said
# "this rule applies when work is delivered", the install maps `git push` onto
# work.deliver, and so `git push` brings the rule — whatever the command's
# words happen to be. The server resolves the call to its moments and looks up
# what is mounted there; this hook only carries the event and the answer.
#
# REGISTERED ON EVERY TOOL, KEPT OFF THE WIRE FOR MOST OF THEM. Which calls can
# reach a mounted rule depends on this install's mappings and mounts, so the
# matcher cannot say. Instead the hook keeps the server's answer to "which
# tools can?" (`/moment-tools`) on disk for a few minutes, and a call to any
# other tool costs one file read. An install that has mounted nothing gets an
# empty list and never sends a moment request at all.
#
# GRIMOIRE'S OWN TOOLS ARE SKIPPED for delivery. Closing a task or recording a
# lesson reaches its moment through the tool's own response (`moment_rules`),
# which works for a client without this plugin too; delivering it here as well
# would say it twice. They are still written to the acts ledger below.
#
# SILENT ON OUTAGE, like grimoire_tool_rules.sh and for its reason: this fires
# before many calls, and an outage line before each is noise. A failed list
# fetch is cached as an empty list, so a down instance costs one timeout per
# window rather than one per call.
#
# Env:
# GRIMOIRE_URL / GRIMOIRE_TOKEN override for the settings.json dogfooding path.
command -v curl >/dev/null 2>&1 || exit 0
# shellcheck source=plugin/hooks/grimoire_defs.sh
. "$(dirname "${BASH_SOURCE[0]}")/grimoire_defs.sh"
event=$(cat 2>/dev/null || true)
event_flat=$(printf '%s' "$event" | grimoire_json_flat)
tool_name=$(grimoire_json_pick "$event_flat" '.tool_name')
session_id=$(grimoire_json_pick "$event_flat" '.session_id')
event_cwd=$(grimoire_json_pick "$event_flat" '.cwd')
[ -n "$tool_name" ] && [ -n "$session_id" ] || exit 0
grimoire_config || exit 0
# The tool's KEY, as the server writes mappings: no MCP server prefix, lowercase.
tool_key=${tool_name##*__}
tool_key=$(printf '%s' "$tool_key" | tr '[:upper:]' '[:lower:]')
state_dir="${TMPDIR:-/tmp}/grimoire-priorart"
mkdir -p "$state_dir" 2>/dev/null || true
safe_sid=$(printf '%s' "$session_id" | tr -c 'A-Za-z0-9._-' '_')
# Its own directory, outside GRIMOIRE_LEDGER_DIRS, so a compaction does not
# sweep it: what is kept here describes the install and what the session
# DID, not what its context holds. grimoire_session_end.sh removes it.
cache_dir="${TMPDIR:-/tmp}/grimoire-moment"
mkdir -p "$cache_dir" 2>/dev/null || true
now=$(date +%s 2>/dev/null) || now=0
# ── The acts ledger ──────────────────────────────────────────────────────
#
# Every call, before any filter: `epoch<TAB>event`, one line each. When the
# session then opens a rule, grimoire_record_opened.sh sends the last few
# minutes of these, and the server counts the open as evidence the rule
# belongs on the moments they reached — which is how a rule nobody mounted
# gets proposed for one. Recorded whether or not anything is mounted, since
# an unmounted moment is exactly what the evidence is for, and including
# Grimoire's own tools (closing a task is a moment too). The open itself is
# not an act. A large event (a file write) is kept as its tool name alone.
if [ "$now" -gt 0 ] && [ "$tool_key" != "get_rule" ]; then
acts_file="$cache_dir/${safe_sid}.acts"
act=$(printf '%s' "$event" | tr -d '\n\r')
if [ "${#act}" -gt 16384 ]; then
esc=$(printf '%s' "$tool_name" | grimoire_json_escape) || esc=""
act="{\"tool_name\":\"${esc}\",\"tool_input\":{}}"
fi
printf '%s\t%s\n' "$now" "$act" >> "$acts_file" 2>/dev/null || true
# Kept short: only the last few minutes are ever read.
size=$(wc -c < "$acts_file" 2>/dev/null) || size=0
if [ "${size:-0}" -gt 262144 ]; then
tail -n 50 "$acts_file" > "$acts_file.tmp" 2>/dev/null \
&& mv -f "$acts_file.tmp" "$acts_file" 2>/dev/null
fi
fi
case "$tool_name" in
mcp__*grimoire*__*) exit 0 ;;
esac
# ── Which tools can reach a mounted rule: cached, first line a timestamp ──
#
# Five minutes. A mount or a mapping made in this session reaches the hook
# within that window; the MCP door delivers Grimoire's own moments at once.
tools_file="$cache_dir/${safe_sid}.tools"
stamp=""
[ -f "$tools_file" ] && stamp=$(head -n 1 "$tools_file" 2>/dev/null)
case "$stamp" in ''|*[!0-9]*) stamp=0 ;; esac
if [ "$now" -eq 0 ] || [ $((now - stamp)) -gt 300 ]; then
listed=$(curl -fsS --max-time 3 \
-H "Authorization: Bearer ${token}" \
"${url%/}/api/plugin/moment-tools" 2>/dev/null) || listed=""
{
printf '%s\n' "$now"
grimoire_json_list "$(printf '%s' "$listed" | grimoire_json_flat)" '.tools'
} > "$tools_file" 2>/dev/null || true
fi
tail -n +2 "$tools_file" 2>/dev/null | grep -Fqx -- "$tool_key" || exit 0
# ── The moment request ────────────────────────────────────────────────────
repo_q=""
lookup_dir=${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}
scope=$(grimoire_scope_query "$lookup_dir")
[ -n "$scope" ] && repo_q="&${scope}"
# The shared session ledger, as grimoire_tool_rules.sh reads and writes it: a
# rule this session was already shown is cited, not quoted again.
rulefile="$state_dir/${safe_sid}.rules.ids"
ledger_q=""
rule_seen=$(grimoire_rules_live "$rulefile")
[ -n "$rule_seen" ] && ledger_q="&exclude_rule_ids=${rule_seen}"
ledger_q="${ledger_q}$(grimoire_held_query "$state_dir/${safe_sid}.opened.ids")"
# The reply-shape ledger: a shape this session already holds in
# full comes back as its reminder.
shapefile=$(grimoire_shapes_file "$safe_sid")
shapes_seen=$(grimoire_shapes_seen "$shapefile")
[ -n "$shapes_seen" ] && ledger_q="${ledger_q}&shapes_seen=${shapes_seen}"
# The event whole, since which field a mapping matches is the mapping's
# business. A write of a very large file is reduced to its name: the moments a
# write reaches never depend on its content.
if [ "${#event}" -gt 65536 ]; then
esc=$(printf '%s' "$tool_name" | grimoire_json_escape) || exit 0
event="{\"tool_name\":\"${esc}\",\"tool_input\":{}}"
fi
query="${repo_q}${ledger_q}"
query=${query#&}
body=$(printf '%s' "$event" | curl -fsS --max-time 3 \
-H "Authorization: Bearer ${token}" \
-H "Content-Type: application/json" \
--data-binary @- \
"${url%/}/api/plugin/moment${query:+?$query}" 2>/dev/null) || exit 0
body_flat=$(printf '%s' "$body" | grimoire_json_flat)
grimoire_json_list "$body_flat" '.rule_ids' | grimoire_rules_append "$rulefile"
grimoire_json_list "$body_flat" '.shape_keys' | grimoire_shapes_append "$shapefile"
context=$(grimoire_json_pick "$body_flat" '.context')
[ -n "$context" ] || exit 0
grimoire_json_out PreToolUse "$context"
exit 0