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 REPLY = "reply" # a rule holding the finished reply for one read GATES = (ENFORCE, HOLD, CHECKPOINT, FENCE, REPLY) # 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, the project fence and the hold on a finished reply (the Stop hook, `tool` = "reply") 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, }