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 @@ + + + + + + +