From e70b2c2c0039e2a2feae8833ae0ddc15f35be085 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Mon, 23 Nov 2026 17:19:59 -0500 Subject: [PATCH 1/6] feat(7733): Apply all confirms every waiting weight suggestion at once, after a dialog that counts what changes The dialog names how many changes across how many rules, and how many of those rules will start holding or refusing the acts they govern. A refused item is reported in a toast; the rest still apply. Co-Authored-By: Claude Opus 5.5 --- frontend/src/assets/rule-proposals.css | 1 + frontend/src/components/WeightProposals.vue | 64 ++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/frontend/src/assets/rule-proposals.css b/frontend/src/assets/rule-proposals.css index 1877d1d7..72f5f239 100644 --- a/frontend/src/assets/rule-proposals.css +++ b/frontend/src/assets/rule-proposals.css @@ -3,6 +3,7 @@ Loaded scoped by each, so these class names stay local to the panels. */ .proposals { display: flex; flex-direction: column; gap: var(--fs-space-2); } .sub-title { margin: 0; font-size: 0.95rem; color: var(--fs-text-primary); } +.proposals-bar { display: flex; justify-content: flex-end; } .proposal-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--fs-space-2); } .proposal-rule { padding: var(--fs-space-3); diff --git a/frontend/src/components/WeightProposals.vue b/frontend/src/components/WeightProposals.vue index 037fdd9c..f207c5c7 100644 --- a/frontend/src/components/WeightProposals.vue +++ b/frontend/src/components/WeightProposals.vue @@ -7,22 +7,41 @@ import type { import type { RuleTrigger } from "@/api/rulebooks"; import { apiErrorMessage } from "@/api/client"; import { useToastStore } from "@/stores/toast"; +import ConfirmDialog from "@/components/ConfirmDialog.vue"; /** * Suggestions for how strongly a rule arrives and what reaches it, waiting on * a person. A pass read each rule beside what happened when it reached * sessions, and proposed its enforcement (inform, hold, enforce), its salience, * or a typed trigger. Nothing changes until "Apply" is pressed; "Not this" - * keeps the value from being proposed again. The same judgment - * `judge_rule_weights` makes in a session, through the same service. + * keeps the value from being proposed again. "Apply all" confirms every + * listed suggestion at once, after a dialog that counts what changes. The + * same judgment `judge_rule_weights` makes in a session, through the same + * service. */ const toast = useToastStore(); const data = ref(null); const failed = ref(false); const busy = ref(null); +const confirmingAll = ref(false); const rules = computed(() => data.value?.rules ?? []); +const proposalCount = computed(() => rules.value.reduce((n, r) => n + r.proposals.length, 0)); + +// The dialog names how many rules would start stopping work, since a hold or +// an enforce is the change a person notices in their next session. +const allSummary = computed(() => { + const stopping = rules.value.filter((r) => r.proposals.some( + (p) => p.field === "enforcement" && p.value !== "inform", + )).length; + const changes = `${proposalCount.value} change${proposalCount.value === 1 ? "" : "s"}`; + const across = `${rules.value.length} rule${rules.value.length === 1 ? "" : "s"}`; + const stops = stopping + ? ` ${stopping} of them will hold or refuse the acts they govern.` + : ""; + return `${changes} across ${across} take effect at once.${stops}`; +}); // What each enforcement does when the rule arrives — said beside the change, // since "hold" and "enforce" stop work and the person applying one should @@ -84,6 +103,31 @@ async function decide(rule: RuleWeightProposals, p: WeightProposal, verdict: Ver } } +async function applyAll() { + confirmingAll.value = false; + busy.value = "all"; + try { + const out = await judgeWeightProposals(rules.value.flatMap((rule) => rule.proposals.map((p) => ({ + rule_id: rule.id, field: p.field, value: p.trigger ?? p.value, + verdict: "confirm" as Verdict, note: "Applied with Apply all in Settings.", + })))); + if (out.refused.length) { + const first = out.refused[0]; + toast.show( + `${out.judged.length} applied; ${out.refused.length} refused — #${first.rule_id}: ${first.error}`, + "error", + ); + } else { + toast.show(`${out.judged.length} applied.`, "success"); + } + await load(); + } catch (e) { + toast.show(apiErrorMessage(e, "Could not apply the suggestions"), "error"); + } finally { + busy.value = null; + } +} + onMounted(load); @@ -102,6 +146,13 @@ onMounted(load); rule exactly, by the tool, path, command or word the act carries; a rule with triggers holds or refuses only the acts they match, and elsewhere it is a line.

+
+ +
  • @@ -147,6 +198,15 @@ onMounted(load);
+ From b4bdd7798c1c36bf8fb622b4ab267e1ffb564ea0 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Mon, 23 Nov 2026 17:28:23 -0500 Subject: [PATCH 2/6] feat(7733): adherence is measured per rule and per moment, and shown A rule is in force at a moment of a session when it was delivered there. Each (session, rule, place) now carries at most one verdict (rule_adherence, migration 0134): followed, departed, broken or not applicable, with where it came from: - an enforce rule refusing the act: followed, recorded by the moment arm, and never replaced; - the session's own rule_outcome (applied, departed) and rule_misfired calls, which the plugin's outcome hook now also posts with the session id (/api/plugin/rule-verdict) onto the delivery being answered; - a transcript review: adherence_to_judge lists what has no verdict yet, by session, and judge_adherence records verdicts with their evidence. rule_adherence (MCP) and /api/retrieval/adherence give the readout: sessions in force, opened and each verdict per rule and per place. Settings > Moments shows it in a "Were they followed" panel, beside each moment's delivered and opened counts, and per rule inside each moment. Verdicts are pruned with their deliveries and left out of backups like them. Plugin 2026.11.23.2227. Co-Authored-By: Claude Opus 5.5 --- alembic/versions/0134_rule_adherence.py | 43 +++ frontend/src/api/moments.ts | 38 +++ frontend/src/components/MomentRuleUsage.vue | 25 +- frontend/src/components/MomentsSettings.vue | 46 ++- frontend/src/components/RuleAdherence.vue | 107 ++++++ plugin/.claude-plugin/plugin.json | 2 +- plugin/hooks/grimoire_record_outcome.sh | 36 +- plugin/hooks/hooks.json | 2 +- plugin/skills/using-grimoire/moments.md | 9 + src/grimoire/mcp/server.py | 4 + src/grimoire/mcp/tools/moments.py | 64 ++++ src/grimoire/models/__init__.py | 1 + src/grimoire/models/rule_adherence.py | 86 +++++ src/grimoire/routes/plugin.py | 32 ++ src/grimoire/routes/retrieval.py | 17 + src/grimoire/services/backup.py | 3 + src/grimoire/services/moment_delivery.py | 3 +- src/grimoire/services/retrieval_pipeline.py | 21 +- src/grimoire/services/rule_adherence.py | 358 ++++++++++++++++++++ src/grimoire/services/rule_deliveries.py | 20 +- src/grimoire/services/write_scope.py | 1 + tests/test_integration_rule_adherence.py | 162 +++++++++ tests/test_moments.py | 6 +- tests/test_rule_moment_signal_hooks.py | 27 ++ tests/test_rule_triggers.py | 26 ++ 25 files changed, 1114 insertions(+), 25 deletions(-) create mode 100644 alembic/versions/0134_rule_adherence.py create mode 100644 frontend/src/components/RuleAdherence.vue create mode 100644 src/grimoire/models/rule_adherence.py create mode 100644 src/grimoire/services/rule_adherence.py create mode 100644 tests/test_integration_rule_adherence.py diff --git a/alembic/versions/0134_rule_adherence.py b/alembic/versions/0134_rule_adherence.py new file mode 100644 index 00000000..05b69189 --- /dev/null +++ b/alembic/versions/0134_rule_adherence.py @@ -0,0 +1,43 @@ +"""rule adherence: whether a rule in force at a moment of a session was followed + +Revision ID: 0134 +Revises: 0133 +Create Date: 2026-11-23 + +One verdict per rule, per place it was delivered, per session — the unit +`rule_deliveries` records — from an `enforce` refusal, the session's own +`rule_outcome` / `rule_misfired`, or a transcript review. +""" +import sqlalchemy as sa +from alembic import op + +revision = "0134" +down_revision = "0133" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "rule_adherence", + 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("rule_id", sa.BigInteger(), nullable=False), + sa.Column("at", sa.Text(), nullable=False), + sa.Column("verdict", sa.Text(), nullable=False), + sa.Column("source", sa.Text(), nullable=False), + sa.Column("evidence", sa.Text(), nullable=True), + sa.Column("judged_at", sa.DateTime(timezone=True), nullable=False, + server_default=sa.func.now()), + sa.UniqueConstraint("user_id", "session_id", "rule_id", "at", + name="uq_rule_adherence_session_rule_at"), + ) + op.create_index("ix_rule_adherence_rule_at", "rule_adherence", ["rule_id", "at"]) + op.create_index("ix_rule_adherence_judged_at", "rule_adherence", ["judged_at"]) + + +def downgrade() -> None: + op.drop_index("ix_rule_adherence_judged_at", table_name="rule_adherence") + op.drop_index("ix_rule_adherence_rule_at", table_name="rule_adherence") + op.drop_table("rule_adherence") diff --git a/frontend/src/api/moments.ts b/frontend/src/api/moments.ts index 04788cee..e5dcd72d 100644 --- a/frontend/src/api/moments.ts +++ b/frontend/src/api/moments.ts @@ -246,6 +246,44 @@ export interface WeightJudgeResult { refused: { rule_id: number; field?: string; error: string }[]; } +/** One rule at one place, or a sum of them: how many sessions it was in + * force in, opened in, and what each was judged. */ +export interface AdherenceCounts { + in_force: number; + opened: number; + followed: number; + /** Read and deliberately not followed, with a stated reason. */ + departed: number; + broken: number; + not_applicable: number; + unjudged: number; +} + +export interface RuleAdherenceRow extends AdherenceCounts { + rule_id: number; + /** Null for a rule deleted since. */ + title: string | null; + by_moment: (AdherenceCounts & { at: string })[]; +} + +export interface MomentAdherenceRow extends AdherenceCounts { + /** A moment (`work.deliver`) or an arm (`pre_tool_rule`). */ + at: string; + by_rule: (AdherenceCounts & { rule_id: number; title: string | null })[]; +} + +export interface AdherencePayload { + days: number; + totals: AdherenceCounts; + by_rule: RuleAdherenceRow[]; + by_moment: MomentAdherenceRow[]; +} + +/** Whether the rules in force at each moment were followed there. */ +export function getAdherence(days = 30): Promise { + return apiGet(`/api/retrieval/adherence?days=${days}`); +} + export function getWeightProposals(ruleId?: number): Promise { return apiGet(`/api/retrieval/weights/proposals${ruleId ? `?rule_id=${ruleId}` : ""}`); } diff --git a/frontend/src/components/MomentRuleUsage.vue b/frontend/src/components/MomentRuleUsage.vue index 70578498..976f27ff 100644 --- a/frontend/src/components/MomentRuleUsage.vue +++ b/frontend/src/components/MomentRuleUsage.vue @@ -1,12 +1,26 @@ + + followed {{ followed(name)!.followed }} of {{ followed(name)!.in_force }} in force + - + diff --git a/frontend/src/components/RuleAdherence.vue b/frontend/src/components/RuleAdherence.vue new file mode 100644 index 00000000..0921605f --- /dev/null +++ b/frontend/src/components/RuleAdherence.vue @@ -0,0 +1,107 @@ + + + + + + +