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.
#