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