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:
@@ -14,6 +14,7 @@ from quart import Blueprint, g, jsonify, request
|
||||
|
||||
from grimoire.auth import admin_required, get_current_user_id, login_required
|
||||
from grimoire.config import Config
|
||||
from grimoire.services import gate_stops as gate_stops_svc
|
||||
from grimoire.services import hook_payload as hook_payload_svc
|
||||
from grimoire.services import moment_delivery as moment_delivery_svc
|
||||
from grimoire.services import plugin_context as plugin_ctx_svc
|
||||
@@ -455,6 +456,10 @@ async def rule_opened():
|
||||
await rule_deliveries_svc.mark_opened(
|
||||
g.user.id, str(data.get("session_id") or ""), rule_id,
|
||||
)
|
||||
# And against the stop it cleared, when a gate had stopped a call on it.
|
||||
await gate_stops_svc.mark_cleared(
|
||||
g.user.id, str(data.get("session_id") or ""), rule_id,
|
||||
)
|
||||
acts = data.get("acts")
|
||||
if rule_id <= 0 or not isinstance(acts, list) or not acts:
|
||||
return jsonify({"moments": [], "context": ""})
|
||||
@@ -465,6 +470,54 @@ async def rule_opened():
|
||||
))
|
||||
|
||||
|
||||
@plugin_bp.post("/gate-stop")
|
||||
@login_required
|
||||
async def gate_stop():
|
||||
"""A hook stood in front of a tool call.
|
||||
|
||||
Body: `{"session_id", "gate", "outcome", "tool", "rule_id"?, "at"?,
|
||||
"target"?, "call_id"?}`. `gate` is `enforce`, `hold`, `checkpoint` or
|
||||
`fence`; `outcome` is `stopped` (denied), `let_through` (a hold the
|
||||
session's stop budget no longer covered) or `asked` (put to the
|
||||
operator). Posted by the hook that made the stop, since whether a hold
|
||||
actually stops is decided by caps kept in the plugin. Answers
|
||||
`{"recorded"}`.
|
||||
"""
|
||||
data = await request.get_json(silent=True) or {}
|
||||
if not isinstance(data, dict):
|
||||
return jsonify({"recorded": False})
|
||||
try:
|
||||
rule_id = int(data.get("rule_id") or 0)
|
||||
except (TypeError, ValueError):
|
||||
rule_id = 0
|
||||
recorded = await gate_stops_svc.record(
|
||||
g.user.id, str(data.get("session_id") or ""),
|
||||
gate=str(data.get("gate") or ""), outcome=str(data.get("outcome") or ""),
|
||||
tool=str(data.get("tool") or ""), rule_id=rule_id or None,
|
||||
at=data.get("at"), target=data.get("target"), call_id=data.get("call_id"),
|
||||
plugin=rule_deliveries_svc.current_plugin(),
|
||||
)
|
||||
return jsonify({"recorded": recorded})
|
||||
|
||||
|
||||
@plugin_bp.post("/gate-ran")
|
||||
@login_required
|
||||
async def gate_ran():
|
||||
"""A tool call a gate stood in front of ran after all.
|
||||
|
||||
Body: `{"session_id", "call_id"}`, posted after the call by the hook that
|
||||
asked about it. For the project fence that is the operator's yes: the
|
||||
harness reports a call only once it has run. Answers `{"recorded"}`.
|
||||
"""
|
||||
data = await request.get_json(silent=True) or {}
|
||||
if not isinstance(data, dict):
|
||||
return jsonify({"recorded": False})
|
||||
recorded = await gate_stops_svc.mark_ran(
|
||||
g.user.id, str(data.get("session_id") or ""), str(data.get("call_id") or ""),
|
||||
)
|
||||
return jsonify({"recorded": recorded})
|
||||
|
||||
|
||||
@plugin_bp.post("/record-opened")
|
||||
@login_required
|
||||
async def record_opened():
|
||||
|
||||
Reference in New Issue
Block a user