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
+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