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
+1
View File
@@ -63,6 +63,7 @@ from grimoire.models.rule_usage import RuleUsageEvent # noqa: E402, F401
from grimoire.models.rule_delivery import RuleDelivery # noqa: E402, F401
from grimoire.models.rule_adherence import RuleAdherence # noqa: E402, F401
from grimoire.models.record_delivery import RecordDelivery # noqa: E402, F401
from grimoire.models.gate_stop import GateStop # noqa: E402, F401
from grimoire.models.system_usage import SystemUsageEvent # noqa: E402, F401
from grimoire.models.retrieval_judgment import RetrievalJudgment # noqa: E402, F401
from grimoire.models.moment_mapping import MomentMapping # noqa: E402, F401
+88
View File
@@ -0,0 +1,88 @@
from datetime import datetime
from sqlalchemy import BigInteger, DateTime, Index, Text, func
from sqlalchemy.orm import Mapped, mapped_column
from grimoire.models import Base
from grimoire.models.base import iso
# The gates that can stop a tool call, as the plugin hooks name them.
ENFORCE = "enforce" # a moment rule set to enforce: refused every time
HOLD = "hold" # a moment rule set to hold: one read before the act
CHECKPOINT = "checkpoint" # the act checkpoint on a command (tool-rules)
FENCE = "fence" # the project fence: the operator is asked
GATES = (ENFORCE, HOLD, CHECKPOINT, FENCE)
# What the hook did with it.
STOPPED = "stopped" # the call was denied, the reason shown instead
LET_THROUGH = "let_through" # would have held, but the session cap was spent
ASKED = "asked" # the fence put the call to the operator
OUTCOMES = (STOPPED, LET_THROUGH, ASKED)
class GateStop(Base):
"""One time a Grimoire gate stood in front of a tool call.
WHY IT EXISTS. A rule set to enforce or hold, the act checkpoint and the
project fence are the only places Grimoire stops work rather than
commenting on it, and a stop that fires at the wrong moment costs a
round trip every time. Whether the plugin actually stops a call is
decided client-side (the per-rule and per-session caps in
`grimoire_checkpoint_allowed`), so the server's own decision is not the
record: the hook reports what it did, here.
`cleared_at` is stamped when the session opened the rule after the stop,
which is how a hold is meant to clear. `ran_at` is stamped when the
stopped call ran after all, which for the fence means the operator said
yes (the harness reports a PostToolUse only for a call that ran); the
tool call's own id (`call_id`) ties the two. A fence ask with no `ran_at`
was declined, or the call failed.
`at` is the moment the rule was reached at, or the arm that found it.
`target` is the project a fenced act would have landed in.
FK-free, like the other telemetry tables: the record that a rule kept
stopping work outlives the rule, since it is the case for changing it.
"""
__tablename__ = "gate_stops"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
user_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
session_id: Mapped[str] = mapped_column(Text, nullable=False)
gate: Mapped[str] = mapped_column(Text, nullable=False)
outcome: Mapped[str] = mapped_column(Text, nullable=False)
rule_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
tool: Mapped[str] = mapped_column(Text, nullable=False)
at: Mapped[str | None] = mapped_column(Text, nullable=True)
target: Mapped[str | None] = mapped_column(Text, nullable=True)
call_id: Mapped[str | None] = mapped_column(Text, nullable=True)
stopped_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=func.now(),
)
cleared_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
ran_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
plugin: Mapped[str | None] = mapped_column(Text, nullable=True)
__table_args__ = (
Index("ix_gate_stops_session_rule", "user_id", "session_id", "rule_id", "stopped_at"),
Index("ix_gate_stops_stopped_at", "stopped_at"),
)
def to_dict(self) -> dict:
return {
"id": self.id,
"user_id": self.user_id,
"session_id": self.session_id,
"gate": self.gate,
"outcome": self.outcome,
"rule_id": self.rule_id,
"tool": self.tool,
"at": self.at,
"target": self.target,
"call_id": self.call_id,
"stopped_at": iso(self.stopped_at),
"cleared_at": iso(self.cleared_at),
"ran_at": iso(self.ran_at),
"plugin": self.plugin,
}