From 65f1be80b690d3b6a024f9dd70d168a8548a88b9 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 14:12:14 -0500 Subject: [PATCH] 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 --- alembic/versions/0138_gate_stops.py | 49 +++++ frontend/src/api/moments.ts | 42 ++++ frontend/src/components/GateStops.vue | 121 +++++++++++ frontend/src/components/MomentsSettings.vue | 23 +- plugin/PACKAGING.md | 7 +- plugin/hooks/grimoire_defs.sh | 46 ++++ plugin/hooks/grimoire_fence_ran.sh | 53 +++++ plugin/hooks/grimoire_moment.sh | 15 +- plugin/hooks/grimoire_project_fence.sh | 18 ++ plugin/hooks/grimoire_tool_rules.sh | 11 +- plugin/hooks/hooks.json | 9 + scripts/check_plugin.py | 7 + src/grimoire/mcp/tools/search.py | 21 ++ src/grimoire/models/__init__.py | 1 + src/grimoire/models/gate_stop.py | 88 ++++++++ src/grimoire/routes/plugin.py | 53 +++++ src/grimoire/routes/retrieval.py | 15 ++ src/grimoire/services/backup.py | 3 + src/grimoire/services/gate_stops.py | 217 +++++++++++++++++++ src/grimoire/services/retrieval_pipeline.py | 7 +- src/grimoire/services/retrieval_telemetry.py | 11 + src/grimoire/services/rule_deliveries.py | 6 +- tests/test_gate_stop_hooks.py | 196 +++++++++++++++++ tests/test_integration_gate_stops.py | 122 +++++++++++ 24 files changed, 1128 insertions(+), 13 deletions(-) create mode 100644 alembic/versions/0138_gate_stops.py create mode 100644 frontend/src/components/GateStops.vue create mode 100644 plugin/hooks/grimoire_fence_ran.sh create mode 100644 src/grimoire/models/gate_stop.py create mode 100644 src/grimoire/services/gate_stops.py create mode 100644 tests/test_gate_stop_hooks.py create mode 100644 tests/test_integration_gate_stops.py diff --git a/alembic/versions/0138_gate_stops.py b/alembic/versions/0138_gate_stops.py new file mode 100644 index 00000000..57540682 --- /dev/null +++ b/alembic/versions/0138_gate_stops.py @@ -0,0 +1,49 @@ +"""gate stops: each time a Grimoire gate stood in front of a tool call + +Revision ID: 0138 +Revises: 0137 +Create Date: 2026-11-27 + +A row per stop the plugin made: a moment rule set to enforce or hold, the +act checkpoint, or the project fence asking the operator. With whether the +session then opened the rule (`cleared_at`) and whether the stopped call ran +after all (`ran_at`), so how often each gate stops work, for which rule, and +whether it cleared the way it is meant to can be read. +""" +import sqlalchemy as sa +from alembic import op + +revision = "0138" +down_revision = "0137" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "gate_stops", + sa.Column("id", sa.BigInteger(), primary_key=True), + sa.Column("user_id", sa.BigInteger(), nullable=False), + sa.Column("session_id", sa.Text(), nullable=False), + sa.Column("gate", sa.Text(), nullable=False), + sa.Column("outcome", sa.Text(), nullable=False), + sa.Column("rule_id", sa.BigInteger(), nullable=True), + sa.Column("tool", sa.Text(), nullable=False), + sa.Column("at", sa.Text(), nullable=True), + sa.Column("target", sa.Text(), nullable=True), + sa.Column("call_id", sa.Text(), nullable=True), + sa.Column("stopped_at", sa.DateTime(timezone=True), nullable=False, + server_default=sa.func.now()), + sa.Column("cleared_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("ran_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("plugin", sa.Text(), nullable=True), + ) + op.create_index("ix_gate_stops_session_rule", "gate_stops", + ["user_id", "session_id", "rule_id", "stopped_at"]) + op.create_index("ix_gate_stops_stopped_at", "gate_stops", ["stopped_at"]) + + +def downgrade() -> None: + op.drop_index("ix_gate_stops_stopped_at", table_name="gate_stops") + op.drop_index("ix_gate_stops_session_rule", table_name="gate_stops") + op.drop_table("gate_stops") diff --git a/frontend/src/api/moments.ts b/frontend/src/api/moments.ts index 0815a37c..e02bb439 100644 --- a/frontend/src/api/moments.ts +++ b/frontend/src/api/moments.ts @@ -340,6 +340,48 @@ export function getRecordReach(days = 30): Promise { return apiGet(`/api/retrieval/record-reach?days=${days}`); } +/** The gates that can stop a tool call. */ +export type GateName = "enforce" | "hold" | "checkpoint" | "fence"; + +export interface GateCounts { + /** Calls denied, or for the fence, put to the operator. */ + stops: number; + /** Calls a hold would have stopped, let through because the session's + * stop budget was spent. */ + let_through: number; + /** Stops the session cleared by opening the rule. */ + cleared: number; + /** Stops whose rule the session then reported as a misfire. */ + misfired: number; + /** Stopped calls that ran after all; for the fence, the operator's yes. */ + ran: number; +} + +export interface GateRuleRow extends GateCounts { + rule_id: number; + gate: GateName; + /** The moment or arm the rule was reached at. */ + at: string | null; + title: string | null; + sessions: number; + last_stopped_at: string | null; +} + +export interface GateStopsPayload { + days: number; + by_gate: Record; + by_rule: GateRuleRow[]; + fence: { tool: string; target: string | null; asks: number; ran: number }[]; + /** The first stop ever recorded; null when none has been. */ + complete_from: string | null; + covers_window: boolean | null; +} + +/** How often each gate stood in front of a tool call, and what followed. */ +export function getGateStops(days = 30): Promise { + return apiGet(`/api/retrieval/gate-stops?days=${days}`); +} + /** Whether the rules in force at each moment were followed there. */ export function getAdherence(days = 30): Promise { return apiGet(`/api/retrieval/adherence?days=${days}`); diff --git a/frontend/src/components/GateStops.vue b/frontend/src/components/GateStops.vue new file mode 100644 index 00000000..98cf2717 --- /dev/null +++ b/frontend/src/components/GateStops.vue @@ -0,0 +1,121 @@ + + + + + + +