feat(8141): every gate that stops a tool call is recorded, with what followed

A rule set to enforce or hold, the act checkpoint and the project fence
stopped work and left no record. The hook that makes each stop now reports
it to POST /api/plugin/gate-stop; the hook decides, not the server, because
the per-rule and per-session caps are kept in the plugin.

- gate_stops table (migration 0138): gate, outcome (stopped / let_through /
  asked), rule, where it was reached, tool, target project, call id.
- An open of the rule stamps the stop as cleared (rule-opened). A fenced
  call that then runs stamps ran: grimoire_fence_ran.sh, a PostToolUse on
  the fenced tools, matches the call id the fence left pending. That is
  the operator's yes.
- A hold or checkpoint the spent session budget lets through is reported
  as let_through; the per-rule once is not.
- The readout: by gate and by rule, stops, cleared by a read, misfired
  (rule_misfired after it), let through by the cap, ran. In
  retrieval_telemetry as `gates`, at /api/retrieval/gate-stops, and on the
  Moments settings page ("When a rule stopped the work").
- act_decision carries `at`, the moment the deciding rule was reached at.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 14:12:14 -05:00
co-authored by Claude Opus 5.5
parent 706f49a12a
commit 65f1be80b6
24 changed files with 1128 additions and 13 deletions
+46
View File
@@ -1081,6 +1081,52 @@ grimoire_checkpoint_allowed() {
return 0
}
grimoire_gate_report() {
# $1 session id, $2 gate (enforce|hold|checkpoint|fence), $3 outcome
# (stopped|let_through|asked), $4 tool name, $5 rule id or "", $6 where the
# rule was reached (a moment or an arm) or "", $7 the target project or "",
# $8 the tool call's id or "".
#
# Tells the server a gate stood in front of this call. The hook reports it,
# not the server, because whether a hold actually stops is decided here, by
# `grimoire_checkpoint_allowed`'s caps. Needs `url`, `token` and
# `plugin_version` from grimoire_config. Short deadline and silent: it runs
# only on a stop, and a stop the server never heard of costs one row, while
# a hook that hangs costs the call it was deciding.
local sid="${1:-}" gate="${2:-}" outcome="${3:-}" tool="${4:-}" rule rest="" v pair
[ -n "$sid" ] && [ -n "$gate" ] && [ -n "$outcome" ] && [ -n "$tool" ] || return 0
[ -n "${url:-}" ] && [ -n "${token:-}" ] || return 0
command -v curl >/dev/null 2>&1 || return 0
rule=$(printf '%s' "${5:-}" | tr -cd '0-9')
for pair in "at=${6:-}" "target=${7:-}" "call_id=${8:-}"; do
v=${pair#*=}
[ -n "$v" ] || continue
rest="${rest},\"${pair%%=*}\":\"$(printf '%s' "$v" | grimoire_json_escape)\""
done
printf '{"session_id":"%s","gate":"%s","outcome":"%s","tool":"%s","rule_id":%s%s}' \
"$(printf '%s' "$sid" | grimoire_json_escape)" "$gate" "$outcome" \
"$(printf '%s' "$tool" | grimoire_json_escape)" "${rule:-null}" "$rest" \
| curl -fsS --max-time 2 \
-H "Authorization: Bearer ${token}" \
-H "X-Grimoire-Plugin: ${plugin_version:-}" \
-H "Content-Type: application/json" \
--data-binary @- \
"${url%/}/api/plugin/gate-stop" >/dev/null 2>&1 || true
return 0
}
grimoire_checkpoint_spent() {
# $1 ledger file, $2 rule id. True when `grimoire_checkpoint_allowed` just
# refused because the SESSION's budget is spent, as against this rule
# having held an act already. Only the first is a call the cap let through;
# the second is the per-rule once, working as meant.
local f="$1" id
id=$(printf '%s' "${2:-}" | tr -cd '0-9')
[ -n "$f" ] && [ -n "$id" ] && [ -f "$f" ] || return 1
grep -qx "$id" "$f" 2>/dev/null && return 1
return 0
}
grimoire_json_deny() {
# $1 hook event name, $2 the reason the agent reads INSTEAD of the result.
#
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Grimoire — the operator's answer to a project fence ask, read from what ran.
#
# grimoire_project_fence.sh asks the operator before an act lands in another
# project, reports the ask to the server, and leaves the tool call's id in
# `grimoire-fence/<sid>.fence.pending`. The answer itself never reaches a
# hook, but Claude Code sends PostToolUse only for a call that ran. So when
# this fires for the pending call, the operator said yes, and the server is
# told (POST /api/plugin/gate-ran). An ask that is never followed by this was
# declined, or the call failed.
#
# Registered PostToolUse on the tools the fence checks. Nearly every call has
# no ask pending, so the first thing it does is look for a pending file, and
# with none it exits before reading the event.
#
# EXIT 0, ALWAYS, and silent: it decorates a record.
set -uo pipefail
fence_dir="${TMPDIR:-/tmp}/grimoire-fence"
pending=""
for pending in "$fence_dir"/*.fence.pending; do
[ -e "$pending" ] && break
done
[ -e "$pending" ] || exit 0
# shellcheck source=plugin/hooks/grimoire_defs.sh
. "$(dirname "${BASH_SOURCE[0]}")/grimoire_defs.sh"
event=$(cat 2>/dev/null || true)
[ -n "$event" ] || exit 0
event_flat=$(printf '%s' "$event" | grimoire_json_flat)
session_id=$(grimoire_json_pick "$event_flat" '.session_id')
call_id=$(grimoire_json_pick "$event_flat" '.tool_use_id')
[ -n "$session_id" ] && [ -n "$call_id" ] || exit 0
safe_sid=$(printf '%s' "$session_id" | tr -c 'A-Za-z0-9._-' '_')
pending_file="$fence_dir/${safe_sid}.fence.pending"
[ -s "$pending_file" ] || exit 0
[ "$(cat "$pending_file" 2>/dev/null)" = "$call_id" ] || exit 0
rm -f "$pending_file" 2>/dev/null || true
grimoire_config || exit 0
command -v curl >/dev/null 2>&1 || exit 0
printf '{"session_id":"%s","call_id":"%s"}' \
"$(printf '%s' "$session_id" | grimoire_json_escape)" \
"$(printf '%s' "$call_id" | grimoire_json_escape)" \
| curl -fsS --max-time 2 \
-H "Authorization: Bearer ${token}" \
-H "X-Grimoire-Plugin: ${plugin_version:-}" \
-H "Content-Type: application/json" \
--data-binary @- \
"${url%/}/api/plugin/gate-ran" >/dev/null 2>&1 || true
exit 0
+13 -2
View File
@@ -160,21 +160,32 @@ grimoire_json_list "$body_flat" '.shape_keys' | grimoire_shapes_append "$shapefi
# before the act, once per rule and under the session cap that the act
# checkpoint and the reply hold share, so a mis-set rule costs one round trip
# rather than a session that cannot move.
#
# Every stop is reported (`grimoire_gate_report`), and so is a hold the
# session's budget let through, so how often each gate stops work, and
# whether the cap is what lets calls past, can be read on the server.
d_kind=$(grimoire_json_pick "$body_flat" '.decision.kind')
d_reason=$(grimoire_json_pick "$body_flat" '.decision.reason')
d_rule=$(grimoire_json_list "$body_flat" '.decision.rule_ids' | head -n 1)
d_at=$(grimoire_json_pick "$body_flat" '.decision.at')
call_id=$(grimoire_json_pick "$event_flat" '.tool_use_id')
if [ -n "$d_reason" ]; then
case "$d_kind" in
enforce)
grimoire_json_deny PreToolUse "$d_reason"
grimoire_gate_report "$session_id" enforce stopped "$tool_key" "$d_rule" "$d_at" "" "$call_id"
exit 0
;;
hold)
if [ -n "$d_rule" ] \
&& grimoire_checkpoint_allowed "$state_dir/${safe_sid}.checkpoint.ids" "$d_rule"; then
stops="$state_dir/${safe_sid}.checkpoint.ids"
if [ -n "$d_rule" ] && grimoire_checkpoint_allowed "$stops" "$d_rule"; then
grimoire_json_deny PreToolUse "$d_reason"
grimoire_gate_report "$session_id" hold stopped "$tool_key" "$d_rule" "$d_at" "" "$call_id"
exit 0
fi
if [ -n "$d_rule" ] && grimoire_checkpoint_spent "$stops" "$d_rule"; then
grimoire_gate_report "$session_id" hold let_through "$tool_key" "$d_rule" "$d_at" "" "$call_id"
fi
;;
esac
fi
+18
View File
@@ -34,6 +34,12 @@
#
# Run as PostToolUse on enter_project, it records the project a session
# without one has entered, and says nothing.
#
# EVERY ASK IS REPORTED (POST /api/plugin/gate-stop), with the tool call's
# id, which is left in `<sid>.fence.pending`. The operator's answer never
# reaches a hook, but a PostToolUse arrives only for a call that ran, so
# grimoire_fence_ran.sh reports the matching call as run: the operator said
# yes. An ask with no run after it was declined, or the call failed.
set -uo pipefail
# shellcheck source=plugin/hooks/grimoire_defs.sh
@@ -56,6 +62,8 @@ cwd=$(grimoire_json_pick "$event_flat" '.cwd')
state_dir="${TMPDIR:-/tmp}/grimoire-fence"
safe_sid=$(printf '%s' "${session_id:-none}" | tr -c 'A-Za-z0-9._-' '_')
entered_file="$state_dir/${safe_sid}.entered"
pending_file="$state_dir/${safe_sid}.fence.pending"
call_id=$(grimoire_json_pick "$event_flat" '.tool_use_id')
# ── PostToolUse on enter_project: remember the first project entered ─────
if [ "$hook_event" = "PostToolUse" ]; then
@@ -238,6 +246,16 @@ while IFS= read -r check; do
reason=$(grimoire_json_pick "$flat" '.reason')
esc=$(printf '%s' "$reason" | grimoire_json_escape) || exit 0
printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"%s"}}\n' "$esc"
# The ask, and the call it is about, so the run that follows a yes can
# be matched to it. `at` is what the act was: the tool for a Grimoire
# call, the act phrase ("git push in") for a file or a git command.
if [ "$check" = "tool" ]; then fence_at="$short"; fence_tool="$short"; else fence_at="$act"; fence_tool="$tool_name"; fi
if [ -n "$call_id" ] && [ -n "$session_id" ]; then
mkdir -p "$state_dir" 2>/dev/null || true
printf '%s' "$call_id" > "$pending_file" 2>/dev/null || true
fi
grimoire_gate_report "$session_id" fence asked "$fence_tool" "" "$fence_at" \
"$(grimoire_json_pick "$flat" '.targets[0].id')" "$call_id"
exit 0
fi
done <<EOF
+10 -1
View File
@@ -153,13 +153,22 @@ fi
# It cannot recur. `grimoire_checkpoint_allowed` holds at most one act per rule
# and at most five per session, so the worst case of a mis-set floor is a noisy
# session rather than one that cannot move.
#
# Each stop is reported to the server, and so is a checkpoint the session's
# budget let through: the cap is decided here, so only this hook knows.
cp_rule=$(grimoire_json_pick "$body_flat" '.checkpoint.rule_id')
cp_reason=$(grimoire_json_pick "$body_flat" '.checkpoint.reason')
if [ -n "$cp_rule" ] && [ -n "$cp_reason" ] && [ -n "$session_id" ]; then
if grimoire_checkpoint_allowed "$state_dir/${safe_sid}.checkpoint.ids" "$cp_rule"; then
stops="$state_dir/${safe_sid}.checkpoint.ids"
call_id=$(grimoire_json_pick "$event_flat" '.tool_use_id')
if grimoire_checkpoint_allowed "$stops" "$cp_rule"; then
grimoire_json_deny PreToolUse "$cp_reason"
grimoire_gate_report "$session_id" checkpoint stopped "$tool_name" "$cp_rule" pre_tool_rule "" "$call_id"
exit 0
fi
if grimoire_checkpoint_spent "$stops" "$cp_rule"; then
grimoire_gate_report "$session_id" checkpoint let_through "$tool_name" "$cp_rule" pre_tool_rule "" "$call_id"
fi
fi
[ -n "$context" ] || exit 0
+9
View File
@@ -116,6 +116,15 @@
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/hooks/grimoire_project_fence.sh\""
}
]
},
{
"matcher": "Write|Edit|MultiEdit|NotebookEdit|Bash|mcp__.*grimoire.*",
"hooks": [
{
"type": "command",
"command": "bash \"${CLAUDE_PLUGIN_ROOT}/hooks/grimoire_fence_ran.sh\""
}
]
}
],
"PreCompact": [