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
+42
View File
@@ -340,6 +340,48 @@ export function getRecordReach(days = 30): Promise<RecordReachPayload> {
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<GateName, GateCounts & { sessions: number }>;
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<GateStopsPayload> {
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<AdherencePayload> {
return apiGet(`/api/retrieval/adherence?days=${days}`);
+121
View File
@@ -0,0 +1,121 @@
<script setup lang="ts">
import { computed } from "vue";
import type { GateCounts, GateName, GateStopsPayload } from "@/api/moments";
import { fmtDate } from "@/utils/dateFormat";
/**
* Every time Grimoire stood in front of a tool call instead of commenting
* beside it: a rule set to enforce or hold, the checkpoint on a command, or
* the project fence asking the operator. Per gate and per rule, how often it
* stopped work and what followed — the rule opened (the stop cleared the way
* it is meant to), the rule reported as a misfire (a stop at the wrong
* moment), or a hold let through because the session's stop budget was
* spent.
*/
const props = defineProps<{
data: GateStopsPayload | null;
failed: boolean;
}>();
const emit = defineEmits<{ retry: [] }>();
const GATES: [GateName, string][] = [
["enforce", "Refused (enforce)"],
["hold", "Held for a read (hold)"],
["checkpoint", "Command checkpoint"],
["fence", "Project fence"],
];
const any = computed(() =>
!!props.data && GATES.some(([g]) => (props.data!.by_gate[g]?.stops ?? 0) + (props.data!.by_gate[g]?.let_through ?? 0) > 0),
);
function gateText(gate: GateName, c: GateCounts): string[] {
if (gate === "fence") return [`asked ${c.stops}`, `approved ${c.ran}`];
const parts = [`stopped ${c.stops}`, `cleared by a read ${c.cleared}`, `misfired ${c.misfired}`];
if (gate !== "enforce") parts.push(`let through by the cap ${c.let_through}`);
return parts;
}
</script>
<template>
<section class="gates" aria-labelledby="gates-title">
<h3 id="gates-title" class="sub-title">When a rule stopped the work</h3>
<p v-if="failed" class="error-msg">
Gate stops could not be loaded.
<button type="button" class="btn-text" @click="emit('retry')">Try again</button>
</p>
<p v-else-if="!data" class="empty-msg">Loading gate stops…</p>
<p v-else-if="!any" class="empty-msg">
No gate stopped a tool call in the last {{ data.days }} days.
<template v-if="data.complete_from === null">
None has ever been recorded: a session reports its stops once its plugin is updated.
</template>
</p>
<template v-else>
<p class="field-hint">
Over the last {{ data.days }} days. “Cleared by a read” means the session opened the rule
after the stop; “misfired” means it then reported the rule as not applying there; “let
through by the cap” counts calls a hold would have stopped once the session’s budget of
stops was spent.
<template v-if="data.covers_window === false">
Recording began {{ fmtDate(data.complete_from!) }}, so earlier days are missing, not quiet.
</template>
</p>
<ul class="gate-list">
<li v-for="[gate, label] in GATES" :key="gate" class="gate-row">
<span class="gate-title">{{ label }}</span>
<span class="gate-counts" :class="{ 'way-none': !data.by_gate[gate]?.stops && !data.by_gate[gate]?.let_through }">
<span v-for="t in gateText(gate, data.by_gate[gate])" :key="t">{{ t }}</span>
</span>
</li>
</ul>
<details v-if="data.by_rule.length" class="gate-detail">
<summary>By rule</summary>
<ul class="gate-list">
<li v-for="r in data.by_rule" :key="`${r.rule_id}:${r.gate}:${r.at}`" class="gate-row">
<span class="gate-title">
Rule {{ r.rule_id }}<template v-if="r.title"> “{{ r.title }}”</template>
</span>
<code class="moment-name">{{ r.gate }}<template v-if="r.at"> · {{ r.at }}</template></code>
<span class="gate-counts">
<span v-for="t in gateText(r.gate, r)" :key="t">{{ t }}</span>
<span>{{ r.sessions }} {{ r.sessions === 1 ? "session" : "sessions" }}</span>
<span v-if="r.last_stopped_at" class="stat-when">last {{ fmtDate(r.last_stopped_at) }}</span>
</span>
</li>
</ul>
</details>
<details v-if="data.fence.length" class="gate-detail">
<summary>Fence asks by tool</summary>
<ul class="gate-list">
<li v-for="f in data.fence" :key="`${f.tool}:${f.target}`" class="gate-row">
<code class="moment-name">{{ f.tool }}</code>
<span v-if="f.target" class="gate-title">into project {{ f.target }}</span>
<span class="gate-counts">
<span>asked {{ f.asks }}</span>
<span>approved {{ f.ran }}</span>
</span>
</li>
</ul>
</details>
</template>
</section>
</template>
<style scoped>
.gates { display: flex; flex-direction: column; gap: var(--fs-space-2); }
.sub-title { margin: 0; font-size: 0.95rem; color: var(--fs-text-primary); }
.gate-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--fs-space-1); }
.gate-detail .gate-list { margin: var(--fs-space-1) 0 var(--fs-space-2) var(--fs-space-4); }
.gate-detail summary { cursor: pointer; font-size: var(--fs-size-body-sm); color: var(--fs-text-secondary); }
.gate-row { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--fs-space-2); }
.gate-title { font-size: var(--fs-size-body-sm); color: var(--fs-text-primary); }
.gate-counts {
display: inline-flex; flex-wrap: wrap; gap: var(--fs-space-2); margin-left: auto; justify-content: flex-end;
font-size: var(--fs-size-tiny); color: var(--fs-text-secondary); font-variant-numeric: tabular-nums;
}
/* A gate that stopped nothing is a gap, not a zero to read. */
.way-none { color: var(--fs-text-tertiary); }
</style>
<style src="@/assets/moments-shared.css" />
+20 -3
View File
@@ -7,9 +7,12 @@ import WeightProposals from "@/components/WeightProposals.vue";
import LessonSituationProposals from "@/components/LessonSituationProposals.vue";
import RecordKinds from "@/components/RecordKinds.vue";
import RecordReach from "@/components/RecordReach.vue";
import GateStops from "@/components/GateStops.vue";
import RuleAdherence from "@/components/RuleAdherence.vue";
import { getAdherence, getRecordReach } from "@/api/moments";
import type { AdherencePayload, MomentAction, MomentAdherenceRow, MomentUsage, RecordReachPayload } from "@/api/moments";
import { getAdherence, getGateStops, getRecordReach } from "@/api/moments";
import type {
AdherencePayload, GateStopsPayload, MomentAction, MomentAdherenceRow, MomentUsage, RecordReachPayload,
} from "@/api/moments";
import { fmtDate } from "@/utils/dateFormat";
/**
@@ -65,6 +68,19 @@ async function loadReach() {
}
}
// When a gate stopped a tool call, read on its own for the same reason.
const gates = ref<GateStopsPayload | null>(null);
const gatesFailed = ref(false);
async function loadGates() {
try {
gates.value = await getGateStops(days.value);
gatesFailed.value = false;
} catch {
gatesFailed.value = true;
}
}
function followed(moment: string): MomentAdherenceRow | null {
return adherence.value?.by_moment.find((m) => m.at === moment) ?? null;
}
@@ -145,7 +161,7 @@ async function addMapping() {
onMounted(async () => {
await store.load(true);
await Promise.all([loadAdherence(), loadReach()]);
await Promise.all([loadAdherence(), loadReach(), loadGates()]);
});
</script>
@@ -174,6 +190,7 @@ onMounted(async () => {
<WeightProposals />
<LessonSituationProposals />
<RuleAdherence :data="adherence" :failed="adherenceFailed" @retry="loadAdherence" />
<GateStops :data="gates" :failed="gatesFailed" @retry="loadGates" />
<RecordReach :data="reach" :failed="reachFailed" @retry="loadReach" />
<RecordKinds :days="days" />