From 61077752b9840a8acf6d8b627bf4e06a7753d236 Mon Sep 17 00:00:00 2001
From: Bryan Van Deusen
Date: Sun, 22 Nov 2026 18:05:45 -0500
Subject: [PATCH 1/5] feat(7728): a rule declares its enforcement and salience
A rule now carries what it does when it arrives, declared by its author
rather than guessed from a score: enforce (the act is checked), hold (the
reply or act waits for one read) or inform (a line beside the work, the
default). And a salience, high, normal or low, for ranking among rules of
the same enforcement at a crowded moment. A preference is always inform.
Migration 0128 adds both with CHECKs. Every door carries them: the
service, the MCP create and update tools, REST, backup and restore, and
the rule editor, which also marks holding and enforced rules in the list.
Unknown values are refused rather than mapped. Nothing reads them yet
beyond the record; the ordering function and the lifecycle come next in
this step, and step 5 decides who holds.
Co-Authored-By: Claude Opus 5.5
---
alembic/versions/0128_rule_enforcement.py | 48 ++++++++
frontend/src/api/rulebooks.ts | 17 +++
.../components/rules/RuleEditorSlideOver.vue | 51 +++++++-
.../src/components/rules/RuleListPane.vue | 9 ++
src/grimoire/mcp/tools/rulebooks.py | 47 ++++++-
src/grimoire/models/rulebook.py | 11 ++
src/grimoire/routes/rulebooks.py | 29 ++++-
src/grimoire/services/backup.py | 10 ++
src/grimoire/services/rulebooks.py | 63 +++++++++-
tests/helpers.py | 3 +
tests/test_integration_rule_enforcement.py | 116 ++++++++++++++++++
tests/test_services_backup.py | 8 +-
12 files changed, 402 insertions(+), 10 deletions(-)
create mode 100644 alembic/versions/0128_rule_enforcement.py
create mode 100644 tests/test_integration_rule_enforcement.py
diff --git a/alembic/versions/0128_rule_enforcement.py b/alembic/versions/0128_rule_enforcement.py
new file mode 100644
index 00000000..d5bbd282
--- /dev/null
+++ b/alembic/versions/0128_rule_enforcement.py
@@ -0,0 +1,48 @@
+"""rule enforcement and salience — what a rule does when it arrives, and how much it outranks
+
+Revision ID: 0128
+Revises: 0127
+Create Date: 2026-11-22
+
+Two declared columns on a rule, read by the one function that orders what
+reaches a moment:
+
+- `enforcement`: `enforce` (the act itself is checked), `hold` (the reply or act
+ waits for one read), `inform` (a line beside the work). Every existing rule
+ starts as `inform`; declaring which ones hold is the author's call.
+- `salience`: `high`, `normal`, `low` — the author's ranking among rules of
+ the same enforcement.
+"""
+import sqlalchemy as sa
+from alembic import op
+
+revision = "0128"
+down_revision = "0127"
+branch_labels = None
+depends_on = None
+
+_ENFORCEMENTS = ("enforce", "hold", "inform")
+_SALIENCES = ("high", "normal", "low")
+
+
+def _in_list(column: str, values: tuple[str, ...]) -> str:
+ return f"{column} IN ({', '.join(repr(v) for v in values)})"
+
+
+def upgrade() -> None:
+ op.add_column("rules", sa.Column("enforcement", sa.Text(), nullable=False, server_default="inform"))
+ op.add_column("rules", sa.Column("salience", sa.Text(), nullable=False, server_default="normal"))
+ op.create_check_constraint("ck_rules_enforcement", "rules", _in_list("enforcement", _ENFORCEMENTS))
+ op.create_check_constraint("ck_rules_salience", "rules", _in_list("salience", _SALIENCES))
+ # A preference claims no force: it is how work is liked done, not a bar.
+ op.create_check_constraint(
+ "ck_rules_preference_informs", "rules", "kind <> 'preference' OR enforcement = 'inform'",
+ )
+
+
+def downgrade() -> None:
+ op.drop_constraint("ck_rules_preference_informs", "rules", type_="check")
+ op.drop_constraint("ck_rules_salience", "rules", type_="check")
+ op.drop_constraint("ck_rules_enforcement", "rules", type_="check")
+ op.drop_column("rules", "salience")
+ op.drop_column("rules", "enforcement")
diff --git a/frontend/src/api/rulebooks.ts b/frontend/src/api/rulebooks.ts
index 06b96c1d..d3c19573 100644
--- a/frontend/src/api/rulebooks.ts
+++ b/frontend/src/api/rulebooks.ts
@@ -54,6 +54,15 @@ export interface RulebookTopic {
*/
export type RuleKind = "rule" | "preference";
+/**
+ * What a rule does when it arrives, declared by its author. `inform` puts a
+ * line beside the work; `hold` makes the reply or act it governs wait for one
+ * read; `enforce` checks the act itself. A preference is always `inform`.
+ */
+export type RuleEnforcement = "enforce" | "hold" | "inform";
+/** How a rule ranks among rules of the same enforcement at a crowded moment. */
+export type RuleSalience = "high" | "normal" | "low";
+
/** A new record opened from somewhere that already knows what it is for —
* "Adjust this" on a reply shape opens a preference mounted on that shape's
* moment. The editor applies it once, when creating. */
@@ -70,6 +79,8 @@ export interface Rule {
title: string;
statement: string;
kind: RuleKind;
+ enforcement: RuleEnforcement;
+ salience: RuleSalience;
/** WHEN this rule fires — the trigger, not the instruction. */
when_to_apply: string;
why: string;
@@ -113,6 +124,10 @@ export interface RuleHeader {
topic_id: number | null;
/** Unconditional on the wire (services.rulebooks.rule_brief). */
kind: RuleKind;
+ /** Unconditional on the wire, like `kind`. */
+ enforcement: RuleEnforcement;
+ /** Present only when raised or lowered from `normal`. */
+ salience?: RuleSalience;
/** A date (YYYY-MM-DD), not a timestamp. */
updated_at: string | null;
when_to_apply?: string;
@@ -217,6 +232,8 @@ export interface RuleWrite {
/** Writable from the editor: a preference is not a lesser rule, it is a
* different force, and the person writing it is the one who knows which. */
kind: RuleKind;
+ enforcement: RuleEnforcement;
+ salience: RuleSalience;
when_to_apply: string;
why: string;
how_to_apply: string;
diff --git a/frontend/src/components/rules/RuleEditorSlideOver.vue b/frontend/src/components/rules/RuleEditorSlideOver.vue
index 8eff520b..b0f9954f 100644
--- a/frontend/src/components/rules/RuleEditorSlideOver.vue
+++ b/frontend/src/components/rules/RuleEditorSlideOver.vue
@@ -6,7 +6,10 @@ import { useMomentsStore } from "@/stores/moments";
import RuleHistoryPanel from "@/components/rules/RuleHistoryPanel.vue";
import RuleHomePicker from "@/components/rules/RuleHomePicker.vue";
import { apiErrorMessage } from "@/api/client";
-import { listRulebooks, listTopics, type Rule, type RuleKind, type RulePreset } from "@/api/rulebooks";
+import {
+ listRulebooks, listTopics,
+ type Rule, type RuleEnforcement, type RuleKind, type RulePreset, type RuleSalience,
+} from "@/api/rulebooks";
const props = defineProps<{
ruleId: number | null;
@@ -26,6 +29,14 @@ const whenToApply = ref("");
// followed too faithfully, where a rule mislabelled as a preference is one a
// session may quietly rewrite.
const kind = ref("rule");
+// The server's defaults: a line beside the work, at normal rank.
+const enforcement = ref("inform");
+const salience = ref("normal");
+// A preference carries no force but `inform`, and the server refuses any
+// other, so switching kind settles it here rather than as a failed save.
+watch(kind, (k) => {
+ if (k === "preference") enforcement.value = "inform";
+});
const systemIds = ref([]);
// The moments this rule is mounted on. Catalog moments are
// ticked; a named procedure's moment (`skill.`) is typed, since its
@@ -123,6 +134,8 @@ async function load() {
statement.value = r.statement;
whenToApply.value = r.when_to_apply || "";
kind.value = r.kind;
+ enforcement.value = r.enforcement ?? "inform";
+ salience.value = r.salience ?? "normal";
systemIds.value = (r.systems ?? []).map((sys) => sys.id);
ruleMoments.value = [...(r.moments ?? [])];
why.value = r.why || "";
@@ -135,6 +148,8 @@ async function load() {
statement.value = "";
whenToApply.value = props.preset?.whenToApply ?? "";
kind.value = props.preset?.kind ?? "rule";
+ enforcement.value = "inform";
+ salience.value = "normal";
systemIds.value = [];
ruleMoments.value = [...(props.preset?.moments ?? [])];
why.value = "";
@@ -181,6 +196,8 @@ async function save() {
statement: statement.value,
when_to_apply: whenToApply.value,
kind: kind.value,
+ enforcement: enforcement.value,
+ salience: salience.value,
// Always sent, so clearing the last area actually clears it — the server
// reads a list as "these ARE the areas now".
system_ids: systemIds.value,
@@ -271,6 +288,38 @@ watch(() => props.ruleId, load);
preference.
+
Sessions rewrite preferences without asking, which is what makes them
stay current. Every rewrite is kept, and
diff --git a/frontend/src/components/rules/RuleListPane.vue b/frontend/src/components/rules/RuleListPane.vue
index 4c2f8d27..92713d0e 100644
--- a/frontend/src/components/rules/RuleListPane.vue
+++ b/frontend/src/components/rules/RuleListPane.vue
@@ -27,6 +27,15 @@ const emit = defineEmits<{
class="rule-chip rule-chip-preference"
title="How you want work done. It does not bind, and Grimoire updates it as the work teaches it."
>preference
+
+ {{ r.enforcement === "hold" ? "holds" : "enforced" }}
diff --git a/src/grimoire/mcp/tools/rulebooks.py b/src/grimoire/mcp/tools/rulebooks.py
index 19c8711a..e160050d 100644
--- a/src/grimoire/mcp/tools/rulebooks.py
+++ b/src/grimoire/mcp/tools/rulebooks.py
@@ -342,6 +342,7 @@ async def create_rule(
arose_from_id: int = 0, verify_with: str = "", expires_when: str = "",
system_ids: list[int] | None = None,
moments: list[str] | None = None, force: bool = False,
+ enforcement: str = "inform", salience: str = "normal",
) -> dict:
"""Create a new rule in a rulebook (a SHARED rule — keep it general).
@@ -530,6 +531,14 @@ async def create_rule(
constraint expires when the ground under it moves, not on a
schedule. Pairs with verify_with; both empty is the normal case.
order_index: Display order within the topic (default 0).
+ enforcement: What this rule does when it arrives. `inform` (the
+ default) puts a line beside the work. `hold` makes the reply or
+ act it governs wait for one read of it — for a rule whose miss
+ costs a rewrite. `enforce` checks the act itself — for a hard
+ boundary. Declare hold or enforce on purpose; most rules inform.
+ salience: `high`, `normal` (the default) or `low` — how this record
+ ranks against others of the same enforcement when a moment has
+ more to say than fits.
force: Bypass the near-duplicate gate. By default, a title-identical rule
already in this topic BLOCKS creation and returns its id so you update
it instead. Set true only for a genuinely distinct rule. A rule or
@@ -553,6 +562,7 @@ async def create_rule(
arose_from_id=arose_from_id,
why=why, how_to_apply=how_to_apply, order_index=order_index,
verify_with=verify_with, expires_when=expires_when,
+ enforcement=enforcement, salience=salience,
)
data = await rulebooks_svc.rule_detail(uid, rule, system_ids, moments)
data.update(dedup_svc.overlap_response(overlaps, "rule"))
@@ -565,6 +575,7 @@ async def create_project_rule(
arose_from_id: int = 0, verify_with: str = "", expires_when: str = "",
system_ids: list[int] | None = None,
moments: list[str] | None = None, force: bool = False,
+ enforcement: str = "inform", salience: str = "normal",
) -> dict:
"""Create a rule scoped to a single project (no rulebook needed).
@@ -640,6 +651,14 @@ async def create_project_rule(
expires_when: The state under which the rule stops being true — see
create_rule. A state, not a date.
order_index: Display order within the project's rule list (default 0).
+ enforcement: What this rule does when it arrives. `inform` (the
+ default) puts a line beside the work. `hold` makes the reply or
+ act it governs wait for one read of it — for a rule whose miss
+ costs a rewrite. `enforce` checks the act itself — for a hard
+ boundary. Declare hold or enforce on purpose; most rules inform.
+ salience: `high`, `normal` (the default) or `low` — how this record
+ ranks against others of the same enforcement when a moment has
+ more to say than fits.
force: Bypass the near-duplicate gate. By default, a title-identical rule
already on this project BLOCKS creation and returns its id so you
update it instead. Set true only for a genuinely distinct rule. An
@@ -662,6 +681,7 @@ async def create_project_rule(
arose_from_id=arose_from_id,
why=why, how_to_apply=how_to_apply, order_index=order_index,
verify_with=verify_with, expires_when=expires_when,
+ enforcement=enforcement, salience=salience,
)
data = await rulebooks_svc.rule_detail(uid, rule, system_ids, moments)
data.update(dedup_svc.overlap_response(overlaps, "rule"))
@@ -675,6 +695,7 @@ async def update_rule(
moments: list[str] | None = None, arose_from_id: int = 0,
verify_with: str = "", expires_when: str = "", kind: str = "",
clear_fields: list[str] | None = None,
+ enforcement: str = "", salience: str = "",
) -> dict:
"""Update a rule. Empty strings / order_index=-1 leave fields unchanged.
@@ -735,6 +756,15 @@ async def update_rule(
expires_when: The state under which the rule stops being true. A
state, not a date. See create_rule.
clear_fields: Names of fields to empty, as above.
+ enforcement: What this rule does when it arrives. `inform` (the
+ default for a new rule) puts a line beside the work. `hold` makes the reply or
+ act it governs wait for one read of it — for a rule whose miss
+ costs a rewrite. `enforce` checks the act itself — for a hard
+ boundary. Declare hold or enforce on purpose; most rules inform.
+ salience: `high`, `normal` (the default) or `low` — how this record
+ ranks against others of the same enforcement when a moment has
+ more to say than fits.
+ Omit either to leave it unchanged.
"""
uid = current_user_id()
moments = moments_svc.require_moments(moments)
@@ -759,6 +789,10 @@ async def update_rule(
fields["expires_when"] = expires_when
if order_index >= 0:
fields["order_index"] = order_index
+ if enforcement:
+ fields["enforcement"] = enforcement
+ if salience:
+ fields["salience"] = salience
rule = await rulebooks_svc.update_rule(
rule_id, uid, clear=clear_fields or (), **fields,
)
@@ -786,7 +820,7 @@ async def create_preference(
arose_from_id: int, why: str = "", how_to_apply: str = "",
order_index: int = 0, system_ids: list[int] | None = None,
moments: list[str] | None = None,
- force: bool = False,
+ force: bool = False, salience: str = "normal",
) -> dict:
"""Record how the operator wants work done. No approval loop — write it.
@@ -860,6 +894,9 @@ async def create_preference(
done rather than what it is about: a rule on when work counts as
finished belongs at work.finish and reply.report, where nothing
said resembles it. Replaces the set; [] clears it.
+ salience: `high`, `normal` (the default) or `low` — how this record
+ ranks against others of the same enforcement when a moment has
+ more to say than fits.
force: Bypass the near-duplicate gate. For a genuinely distinct
preference, not for one that is "mostly" different — a mostly
different preference is an update. A RULE that already answers
@@ -893,6 +930,7 @@ async def create_preference(
title=title, statement=statement, when_to_apply=when_to_apply,
kind="preference", arose_from_id=arose_from_id,
why=why, how_to_apply=how_to_apply, order_index=order_index,
+ salience=salience,
)
data = await rulebooks_svc.rule_detail(uid, rule, system_ids, moments)
data.update(dedup_svc.overlap_response(overlaps, "preference"))
@@ -905,6 +943,7 @@ async def update_preference(
how_to_apply: str = "", order_index: int = -1,
system_ids: list[int] | None = None,
moments: list[str] | None = None, clear_fields: list[str] | None = None,
+ salience: str = "",
) -> dict:
"""Bring a preference up to date. Doing this mid-work is expected.
@@ -959,6 +998,10 @@ async def update_preference(
moments: Replaces the moments this preference arrives at (see
create_preference); omit to leave them alone.
when_to_apply: The moment it applies, in session vocabulary. See above.
+ salience: `high`, `normal` (the default) or `low` — how this record
+ ranks against others of the same enforcement when a moment has
+ more to say than fits.
+ Omit to leave it unchanged.
"""
uid = current_user_id()
moments = moments_svc.require_moments(moments)
@@ -981,6 +1024,8 @@ async def update_preference(
fields["how_to_apply"] = how_to_apply
if order_index >= 0:
fields["order_index"] = order_index
+ if salience:
+ fields["salience"] = salience
rule = await rulebooks_svc.update_rule(
rule_id, uid, clear=clear_fields or (), **fields,
)
diff --git a/src/grimoire/models/rulebook.py b/src/grimoire/models/rulebook.py
index 71c4ab21..50a6855b 100644
--- a/src/grimoire/models/rulebook.py
+++ b/src/grimoire/models/rulebook.py
@@ -116,6 +116,15 @@ class Rule(Base, TimestampMixin, SoftDeleteMixin):
# Defaults to `rule` so nothing changes force on upgrade.
# CHECK ck_rules_kind (migration 0098).
kind: Mapped[str] = mapped_column(Text, default="rule", server_default="rule")
+ # WHAT THE RULE DOES WHEN IT ARRIVES, declared by its author rather than
+ # guessed from a score. `enforce`: the act it governs is checked. `hold`:
+ # the reply or act waits for one read of it. `inform`: a line beside the
+ # work, and never a stop. A preference is always `inform` (CHECK
+ # ck_rules_preference_informs). CHECK ck_rules_enforcement (migration 0128).
+ enforcement: Mapped[str] = mapped_column(Text, default="inform", server_default="inform")
+ # How this rule ranks among rules of the same enforcement when a moment has more
+ # to say than fits: `high`, `normal`, `low`. CHECK ck_rules_salience.
+ salience: Mapped[str] = mapped_column(Text, default="normal", server_default="normal")
why: Mapped[str | None] = mapped_column(Text, nullable=True)
how_to_apply: Mapped[str | None] = mapped_column(Text, nullable=True)
# The three fields that tell a CONSTRAINT apart from a NORM (milestone
@@ -161,6 +170,8 @@ class Rule(Base, TimestampMixin, SoftDeleteMixin):
# codebase keeps re-encountering. Twenty bytes buys an answer that
# cannot be misread.
"kind": self.kind or "rule",
+ "enforcement": self.enforcement or "inform",
+ "salience": self.salience or "normal",
"why": self.why or "",
"how_to_apply": self.how_to_apply or "",
"verify_with": self.verify_with or "",
diff --git a/src/grimoire/routes/rulebooks.py b/src/grimoire/routes/rulebooks.py
index 61295b0d..ee01badf 100644
--- a/src/grimoire/routes/rulebooks.py
+++ b/src/grimoire/routes/rulebooks.py
@@ -18,6 +18,18 @@ from grimoire.services.rule_usage import (
rulebooks_bp = Blueprint("rulebooks", __name__, url_prefix="/api")
+def _declared_or_refusal(data: dict):
+ """The request's enforcement and salience, validated before any write, so
+ a bad value is a 400 naming the choices rather than a half-made rule."""
+ try:
+ return (
+ rulebooks_svc.require_enforcement(data.get("enforcement"), data.get("kind")),
+ rulebooks_svc.require_salience(data.get("salience")),
+ ), None
+ except ValueError as exc:
+ return None, (jsonify({"error": str(exc)}), 400)
+
+
def _moments_or_refusal(data: dict):
"""The request's `moments`, validated BEFORE any write — so an unknown
name is a 400 and not a rule created without the mounts it asked for.
@@ -184,6 +196,10 @@ async def create_rule(topic_id: int):
moments, refused = _moments_or_refusal(data)
if refused:
return refused
+ declared, refused = _declared_or_refusal(data)
+ if refused:
+ return refused
+ enforcement, salience = declared
try:
rule = await rulebooks_svc.create_rule(
topic_id=topic_id,
@@ -202,6 +218,7 @@ async def create_rule(topic_id: int):
arose_from_id=data.get("arose_from_id", 0) or 0,
verify_with=data.get("verify_with", ""),
expires_when=data.get("expires_when", ""),
+ enforcement=enforcement, salience=salience,
)
except ValueError as exc:
return jsonify({"error": str(exc)}), 404
@@ -234,7 +251,7 @@ async def update_rule(rule_id: int):
k: v for k, v in data.items()
if k in ("title", "statement", "why", "how_to_apply", "order_index",
"when_to_apply", "kind", "arose_from_id",
- "verify_with", "expires_when")
+ "verify_with", "expires_when", "enforcement", "salience")
}
# No clear_fields here: a form sends "" for an emptied input, and the
# service normalises "" to NULL for every nullable text column. The MCP
@@ -243,7 +260,10 @@ async def update_rule(rule_id: int):
moments, refused = _moments_or_refusal(data)
if refused:
return refused
- rule = await rulebooks_svc.update_rule(rule_id, uid, **fields)
+ try:
+ rule = await rulebooks_svc.update_rule(rule_id, uid, **fields)
+ except ValueError as exc:
+ return jsonify({"error": str(exc)}), 400
if rule is None:
return jsonify({"error": "rule not found"}), 404
return jsonify(await rulebooks_svc.rule_detail(
@@ -425,6 +445,10 @@ async def create_project_rule(project_id: int):
moments, refused = _moments_or_refusal(data)
if refused:
return refused
+ declared, refused = _declared_or_refusal(data)
+ if refused:
+ return refused
+ enforcement, salience = declared
try:
rule = await rulebooks_svc.create_project_rule(
project_id=project_id,
@@ -443,6 +467,7 @@ async def create_project_rule(project_id: int):
arose_from_id=data.get("arose_from_id", 0) or 0,
verify_with=data.get("verify_with", ""),
expires_when=data.get("expires_when", ""),
+ enforcement=enforcement, salience=salience,
)
except ValueError as exc:
return jsonify({"error": str(exc)}), 404
diff --git a/src/grimoire/services/backup.py b/src/grimoire/services/backup.py
index d8ba4360..e27bcc79 100644
--- a/src/grimoire/services/backup.py
+++ b/src/grimoire/services/backup.py
@@ -959,6 +959,7 @@ def _rule_rows(rows) -> list[dict]:
"title": r.title, "statement": r.statement, "why": r.why,
"how_to_apply": r.how_to_apply, "order_index": r.order_index,
"when_to_apply": r.when_to_apply, "kind": r.kind,
+ "enforcement": r.enforcement, "salience": r.salience,
"verify_with": r.verify_with, "expires_when": r.expires_when,
"verified_at": r.verified_at.isoformat() if r.verified_at else None,
"arose_from_id": r.arose_from_id,
@@ -1717,6 +1718,15 @@ def _build_rule(row: dict, maps: _Maps) -> Rule | None:
# way would restore an old backup with things that had always bound
# quietly not binding.
kind=row.get("kind") or "rule",
+ # An archive from before these columns restores every rule as it
+ # behaved then: a line beside the work, at normal rank. A preference
+ # is always `inform`, whatever the archive says, since the CHECK would
+ # refuse anything else and take the whole restore down with it.
+ enforcement=(
+ "inform" if (row.get("kind") or "rule") == "preference"
+ else (row.get("enforcement") if row.get("enforcement") in ("enforce", "hold", "inform") else "inform")
+ ),
+ salience=row.get("salience") if row.get("salience") in ("high", "normal", "low") else "normal",
verify_with=row.get("verify_with") or None,
expires_when=row.get("expires_when") or None,
# Restored as-is, NOT reset to null. `verified_at` records when someone
diff --git a/src/grimoire/services/rulebooks.py b/src/grimoire/services/rulebooks.py
index 612508fe..a5600aac 100644
--- a/src/grimoire/services/rulebooks.py
+++ b/src/grimoire/services/rulebooks.py
@@ -273,6 +273,38 @@ RELATION_KINDS = ("co_surfaces", "overrides", "elaborates")
# Migration 0098's CHECK. `rule` binds; `preference` is how the operator
# wants work done — see the model comment for why both live on one table.
KINDS = ("rule", "preference")
+# Migration 0128's CHECKs, strongest first — the order the ordering function
+# ranks them in (services/rule_order.py).
+ENFORCEMENTS = ("enforce", "hold", "inform")
+SALIENCES = ("high", "normal", "low")
+
+
+def require_enforcement(enforcement: str | None, kind: str | None) -> str:
+ """The enforcement a rule of this kind may carry, or a refusal naming the choices.
+
+ Unlike `kind`, an unknown enforcement is refused rather than mapped: there is no
+ safe direction to fall in. Mapped up, a typo turns a line into a stop;
+ mapped down, it turns a stop into a line. A preference is always `inform`
+ — it says how work is liked done, and a stop would claim a force it does
+ not have.
+ """
+ clean = (enforcement or "inform").strip().lower()
+ if clean not in ENFORCEMENTS:
+ raise ValueError(f"enforcement must be one of {', '.join(ENFORCEMENTS)}, got {enforcement!r}")
+ if (kind or "rule") == "preference" and clean != "inform":
+ raise ValueError(
+ "a preference's enforcement is always `inform`: it says how work is liked "
+ "done, and holding or enforcing it would make it a rule. Record it "
+ "as a rule if it must stop the work."
+ )
+ return clean
+
+
+def require_salience(salience: str | None) -> str:
+ clean = (salience or "normal").strip().lower()
+ if clean not in SALIENCES:
+ raise ValueError(f"salience must be one of {', '.join(SALIENCES)}, got {salience!r}")
+ return clean
# The rule columns that are nullable, and therefore the ones where EMPTY has
@@ -339,8 +371,14 @@ def rule_brief(rule: Rule, **extra) -> dict:
# one, and the assumption it would reach for — "this binds" — is the
# expensive one to get wrong in the other direction. Say it outright.
"kind": rule.kind or "rule",
+ # Unconditional for the same reason as `kind`: whether a rule stops
+ # the work is never something a reader should have to assume.
+ "enforcement": rule.enforcement or "inform",
"updated_at": rule.updated_at.date().isoformat() if rule.updated_at else None,
}
+ # Salience is attached only when the author raised or lowered it.
+ if (rule.salience or "normal") != "normal":
+ out["salience"] = rule.salience
# Attached only when present: never a null key that reads as a
# capability the record doesn't have.
if rule.when_to_apply:
@@ -506,8 +544,12 @@ async def create_rule(
why: str = "", how_to_apply: str = "", order_index: int = 0,
when_to_apply: str = "", arose_from_id: int = 0,
verify_with: str = "", expires_when: str = "", kind: str = "rule",
+ enforcement: str = "inform", salience: str = "normal",
) -> Rule:
_require_trigger(when_to_apply)
+ kind = _valid_kind(kind)
+ enforcement = require_enforcement(enforcement, kind)
+ salience = require_salience(salience)
async with async_session() as session:
await _assert_topic_owned(session, topic_id, user_id)
rule = Rule(
@@ -515,7 +557,7 @@ async def create_rule(
title=title,
statement=statement,
when_to_apply=when_to_apply or None,
- kind=_valid_kind(kind),
+ kind=kind, enforcement=enforcement, salience=salience,
why=why or None,
how_to_apply=how_to_apply or None,
verify_with=verify_with or None,
@@ -535,6 +577,7 @@ async def create_project_rule(
why: str = "", how_to_apply: str = "", order_index: int = 0,
when_to_apply: str = "", arose_from_id: int = 0,
verify_with: str = "", expires_when: str = "", kind: str = "rule",
+ enforcement: str = "inform", salience: str = "normal",
) -> Rule:
"""Create a rule scoped to a single project (no rulebook ceremony).
@@ -544,6 +587,9 @@ async def create_project_rule(
constraint enforces exactly-one of (topic_id, project_id).
"""
_require_trigger(when_to_apply)
+ kind = _valid_kind(kind)
+ enforcement = require_enforcement(enforcement, kind)
+ salience = require_salience(salience)
async with async_session() as session:
await _assert_project_owned(session, project_id, user_id)
rule = Rule(
@@ -551,7 +597,7 @@ async def create_project_rule(
title=title,
statement=statement,
when_to_apply=when_to_apply or None,
- kind=_valid_kind(kind),
+ kind=kind, enforcement=enforcement, salience=salience,
why=why or None,
how_to_apply=how_to_apply or None,
verify_with=verify_with or None,
@@ -691,7 +737,7 @@ async def update_rule(
allowed = {
"title", "statement", "why", "how_to_apply", "order_index",
"when_to_apply", "kind", "arose_from_id",
- "verify_with", "expires_when",
+ "verify_with", "expires_when", "enforcement", "salience",
}
check_before = rule.verify_with
# A create-time guard is worth nothing if an edit can undo it, and
@@ -719,6 +765,11 @@ async def update_rule(
continue
if key == "kind":
value = _valid_kind(value)
+ elif key == "salience":
+ value = require_salience(value)
+ elif key == "enforcement":
+ # Checked against the kind after the loop, which may change it.
+ value = (value or "").strip().lower() or "inform"
elif key in NULLABLE_RULE_TEXT:
value = value or None
elif key == "arose_from_id":
@@ -731,6 +782,12 @@ async def update_rule(
# rule wrongly vouched for costs the thing the sweep exists to catch.
if rule.verify_with != check_before:
rule.verified_at = None
+ # A rule turned into a preference without naming an enforcement drops to
+ # `inform`, since a preference carries no other. Named explicitly
+ # alongside the new kind, a stronger enforcement is refused instead.
+ if rule.kind == "preference" and "enforcement" not in fields:
+ rule.enforcement = "inform"
+ rule.enforcement = require_enforcement(rule.enforcement, rule.kind)
if trigger_before and not (rule.when_to_apply or "").strip():
raise ValueError(
"when_to_apply cannot be cleared: it is how this rule arrives, "
diff --git a/tests/helpers.py b/tests/helpers.py
index bf01f2f6..02cf17ec 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -290,6 +290,9 @@ def fake_rule(**attrs) -> MagicMock:
# claim to carry a check and rule_brief would stamp a MagicMock date
# onto all of them. Most rules have none — that is the default here.
"verify_with": None, "expires_when": None, "verified_at": None,
+ # A MagicMock here would read as a declared enforcement and a raised
+ # salience on every stand-in; the defaults are what a new rule gets.
+ "enforcement": "inform", "salience": "normal",
"order_index": 0, "created_at": _now(), "updated_at": _now(),
}, attrs)
diff --git a/tests/test_integration_rule_enforcement.py b/tests/test_integration_rule_enforcement.py
new file mode 100644
index 00000000..6234408a
--- /dev/null
+++ b/tests/test_integration_rule_enforcement.py
@@ -0,0 +1,116 @@
+"""Real-Postgres tests for `rules.enforcement` and `rules.salience` (migration 0128).
+
+The CHECKs are what a mock cannot show: an unknown value refused, a preference
+refused anything but `inform`, and a row written without either reading back
+as the defaults every existing rule took on upgrade.
+"""
+import pytest
+import pytest_asyncio
+from sqlalchemy import select
+from sqlalchemy.exc import IntegrityError
+
+from grimoire.models import async_session
+from grimoire.models.rulebook import Rule, Rulebook
+from grimoire.services import rulebooks as rulebooks_svc
+from tests.helpers import ensure_user
+
+pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
+
+OWNER_USERNAME = "rule_enforcement_owner"
+
+
+@pytest_asyncio.fixture
+async def topic_id():
+ # Purged at setup, not teardown: the write path's detached embedding task
+ # would deadlock against a teardown deleting the rulebook.
+ async with async_session() as s:
+ owner = await ensure_user(s, OWNER_USERNAME)
+ uid = owner.id
+ await s.commit()
+ for book in (await s.execute(
+ select(Rulebook).where(Rulebook.owner_user_id == uid)
+ )).scalars().all():
+ await s.delete(book)
+ await s.commit()
+ book = await rulebooks_svc.create_rulebook(uid, "Enforcement fixtures")
+ topic = await rulebooks_svc.create_topic(book.id, uid, "conventions")
+ return uid, topic.id
+
+
+async def _write_raw(topic: int, **kw) -> int:
+ """Straight to the model, around the service's own checks, to reach the CHECKs."""
+ async with async_session() as s:
+ rule = Rule(topic_id=topic, title=kw.pop("title", "t"), statement="s", **kw)
+ s.add(rule)
+ await s.commit()
+ return rule.id
+
+
+async def _read(rule_id: int) -> Rule:
+ async with async_session() as s:
+ return (await s.execute(select(Rule).where(Rule.id == rule_id))).scalar_one()
+
+
+async def test_a_rule_written_without_either_reads_back_as_the_defaults(topic_id):
+ _uid, topic = topic_id
+ rule = await _read(await _write_raw(topic))
+ assert (rule.enforcement, rule.salience) == ("inform", "normal")
+
+
+async def test_an_unknown_enforcement_is_refused_by_the_database(topic_id):
+ _uid, topic = topic_id
+ with pytest.raises(IntegrityError):
+ await _write_raw(topic, enforcement="block")
+
+
+async def test_an_unknown_salience_is_refused_by_the_database(topic_id):
+ _uid, topic = topic_id
+ with pytest.raises(IntegrityError):
+ await _write_raw(topic, salience="urgent")
+
+
+async def test_a_preference_that_holds_is_refused_by_the_database(topic_id):
+ _uid, topic = topic_id
+ with pytest.raises(IntegrityError):
+ await _write_raw(topic, kind="preference", enforcement="hold")
+
+
+async def test_the_service_writes_a_declared_hold_and_reads_it_back(topic_id):
+ uid, topic = topic_id
+ rule = await rulebooks_svc.create_rule(
+ topic, uid, "Holds", "Read before replying", when_to_apply="about to reply",
+ enforcement="hold", salience="high",
+ )
+ back = await _read(rule.id)
+ assert (back.enforcement, back.salience) == ("hold", "high")
+ assert rulebooks_svc.rule_brief(back)["enforcement"] == "hold"
+ assert rulebooks_svc.rule_brief(back)["salience"] == "high"
+
+
+async def test_the_service_refuses_a_preference_that_holds(topic_id):
+ uid, topic = topic_id
+ with pytest.raises(ValueError, match="always `inform`"):
+ await rulebooks_svc.create_rule(
+ topic, uid, "Pref", "Prefer x", when_to_apply="when x",
+ kind="preference", enforcement="hold",
+ )
+
+
+async def test_a_rule_turned_into_a_preference_drops_to_inform(topic_id):
+ uid, topic = topic_id
+ rule = await rulebooks_svc.create_rule(
+ topic, uid, "Becomes a preference", "Do x", when_to_apply="when x",
+ enforcement="enforce",
+ )
+ updated = await rulebooks_svc.update_rule(rule.id, uid, kind="preference")
+ assert (updated.kind, updated.enforcement) == ("preference", "inform")
+
+
+async def test_naming_a_hold_alongside_the_preference_kind_is_refused(topic_id):
+ uid, topic = topic_id
+ rule = await rulebooks_svc.create_rule(
+ topic, uid, "Stays a rule", "Do y", when_to_apply="when y",
+ )
+ with pytest.raises(ValueError, match="always `inform`"):
+ await rulebooks_svc.update_rule(rule.id, uid, kind="preference", enforcement="hold")
+ assert (await _read(rule.id)).kind == "rule"
diff --git a/tests/test_services_backup.py b/tests/test_services_backup.py
index 031682c7..042682fa 100644
--- a/tests/test_services_backup.py
+++ b/tests/test_services_backup.py
@@ -624,13 +624,15 @@ def test_rule_rows_carry_the_verification_fields():
row = SimpleNamespace(
id=1, topic_id=2, project_id=None, title="t", statement="s",
why="w", how_to_apply="h", order_index=0,
- when_to_apply="when", kind="rule",
+ when_to_apply="when", kind="rule", enforcement="hold", salience="high",
verify_with="cat some/file", expires_when="the file grows a shell",
verified_at=checked, arose_from_id=99,
created_at=checked, updated_at=checked,
)
out = backup._rule_rows([row])[0]
+ assert (out["enforcement"], out["salience"]) == ("hold", "high")
+
assert out["verify_with"] == "cat some/file"
assert out["expires_when"] == "the file grows a shell"
assert out["verified_at"] == checked.isoformat()
@@ -650,7 +652,7 @@ def test_rule_rows_keep_an_unverified_rule_unverified():
row = SimpleNamespace(
id=1, topic_id=2, project_id=None, title="t", statement="s",
why=None, how_to_apply=None, order_index=0,
- when_to_apply=None, kind="rule",
+ when_to_apply=None, kind="rule", enforcement="inform", salience="normal",
verify_with=None, expires_when=None, verified_at=None,
arose_from_id=None,
created_at=datetime(2026, 8, 27, tzinfo=timezone.utc),
@@ -680,7 +682,7 @@ def test_rule_rows_carry_the_kind_so_a_preference_does_not_restore_as_a_rule():
row = SimpleNamespace(
id=1, topic_id=2, project_id=None, title="t", statement="s",
why=None, how_to_apply=None, order_index=0,
- when_to_apply="when", kind="preference",
+ when_to_apply="when", kind="preference", enforcement="inform", salience="normal",
verify_with=None, expires_when=None, verified_at=None,
arose_from_id=None, created_at=stamp, updated_at=stamp,
)
--
2.54.0
From 61873b184b327658becee00f6aa245220b5f0f20 Mon Sep 17 00:00:00 2001
From: Bryan Van Deusen
Date: Sun, 22 Nov 2026 18:13:16 -0500
Subject: [PATCH 2/5] feat(7728): one delivery order for rules, and each
delivery recorded with whether it was opened
Every rule arm now ranks what it delivers with one function
(services/rule_order.py): enforcement, then salience, then how narrowly
the rule was reached, then first mention before reminder, then the
rule's own open rate at that moment, then similarity. A crowded moment
drops the same rules whichever arm brought them, and a rule that holds
is never cut for a line beside it.
The open rate comes from a new table, rule_deliveries (migration 0129):
one row per rule line a plugin session is shown, full or repeat, with the
open stamped on the latest delivery it followed. The plugin routes bind
the session per request; the prompt, pre-tool and write-path hooks now
send it, and the opened hook reports every open, with its acts only when
no hook had just named the rule. Rows are pruned daily after 180 days.
Also fixes the TypeScript check: the store's list row now carries the
new enforcement field.
Co-Authored-By: Claude Opus 5.5
---
alembic/versions/0129_rule_deliveries.py | 42 +++++
frontend/src/stores/rulebooks.ts | 3 +
plugin/.claude-plugin/plugin.json | 2 +-
plugin/hooks/grimoire_autoinject.sh | 2 +
plugin/hooks/grimoire_prior_art.sh | 2 +
plugin/hooks/grimoire_record_opened.sh | 33 ++--
plugin/hooks/grimoire_tool_rules.sh | 2 +
src/grimoire/app.py | 3 +
src/grimoire/models/__init__.py | 1 +
src/grimoire/models/rule_delivery.py | 69 ++++++++
src/grimoire/routes/plugin.py | 29 +++-
src/grimoire/services/backup.py | 4 +
src/grimoire/services/moment_delivery.py | 5 +-
src/grimoire/services/plugin_context.py | 5 +
src/grimoire/services/retrieval_pipeline.py | 72 +++++++-
src/grimoire/services/rule_deliveries.py | 180 ++++++++++++++++++++
src/grimoire/services/rule_order.py | 80 +++++++++
tests/test_integration_rule_deliveries.py | 107 ++++++++++++
tests/test_rule_moment_signal_hooks.py | 10 +-
tests/test_rule_order.py | 131 ++++++++++++++
20 files changed, 759 insertions(+), 23 deletions(-)
create mode 100644 alembic/versions/0129_rule_deliveries.py
create mode 100644 src/grimoire/models/rule_delivery.py
create mode 100644 src/grimoire/services/rule_deliveries.py
create mode 100644 src/grimoire/services/rule_order.py
create mode 100644 tests/test_integration_rule_deliveries.py
create mode 100644 tests/test_rule_order.py
diff --git a/alembic/versions/0129_rule_deliveries.py b/alembic/versions/0129_rule_deliveries.py
new file mode 100644
index 00000000..1261c430
--- /dev/null
+++ b/alembic/versions/0129_rule_deliveries.py
@@ -0,0 +1,42 @@
+"""rule deliveries — each rule line a session was shown, and whether it was opened after
+
+Revision ID: 0129
+Revises: 0128
+Create Date: 2026-11-22
+
+One row per rule line delivered to a session, with the open stamped on the
+delivery it followed. The open rate per rule and moment is read from it, both
+for the ordering of what reaches a moment and for the adherence readout.
+"""
+import sqlalchemy as sa
+from alembic import op
+
+revision = "0129"
+down_revision = "0128"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ op.create_table(
+ "rule_deliveries",
+ 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("source", sa.Text(), nullable=False),
+ sa.Column("at", sa.Text(), nullable=False),
+ sa.Column("form", sa.Text(), nullable=False),
+ sa.Column("delivered_at", sa.DateTime(timezone=True), nullable=False,
+ server_default=sa.func.now()),
+ sa.Column("opened_at", sa.DateTime(timezone=True), nullable=True),
+ sa.CheckConstraint("form IN ('full', 'repeat')", name="ck_rule_deliveries_form"),
+ )
+ op.create_index("ix_rule_deliveries_session_rule", "rule_deliveries",
+ ["user_id", "session_id", "rule_id", "delivered_at"])
+ op.create_index("ix_rule_deliveries_rule_at", "rule_deliveries", ["rule_id", "at"])
+ op.create_index("ix_rule_deliveries_delivered_at", "rule_deliveries", ["delivered_at"])
+
+
+def downgrade() -> None:
+ op.drop_table("rule_deliveries")
diff --git a/frontend/src/stores/rulebooks.ts b/frontend/src/stores/rulebooks.ts
index 1eac4b60..fbb31d73 100644
--- a/frontend/src/stores/rulebooks.ts
+++ b/frontend/src/stores/rulebooks.ts
@@ -111,6 +111,9 @@ export const useRulebooksStore = defineStore("rulebooks", () => {
// force as the same row re-fetched, or a preference the operator just
// created would sit in the list looking like a rule until a reload.
kind: rule.kind,
+ enforcement: rule.enforcement ?? "inform",
+ // Mirrors rule_brief: present only when raised or lowered.
+ salience: rule.salience && rule.salience !== "normal" ? rule.salience : undefined,
updated_at: rule.updated_at,
when_to_apply: rule.when_to_apply || undefined,
arose_from_id: rule.arose_from_id ?? undefined,
diff --git a/plugin/.claude-plugin/plugin.json b/plugin/.claude-plugin/plugin.json
index ae191fa1..fd964e8f 100644
--- a/plugin/.claude-plugin/plugin.json
+++ b/plugin/.claude-plugin/plugin.json
@@ -1,7 +1,7 @@
{
"name": "grimoire",
"description": "Grimoire for Claude Code: connects the grimoire MCP server, adds the hooks that deliver live project state and relevant records at the right moment, ships the shared client-neutral Grimoire skills (using-grimoire, writing-plans, reporting-back, systematic-debugging, verification, brainstorming, reusing-code, shape-accounting, family-canon), and syncs your saved Grimoire Processes as skills (/grimoire:sync).",
- "version": "2026.11.05.0301",
+ "version": "2026.11.22.2312",
"author": {
"name": "Bryan Van Deusen"
},
diff --git a/plugin/hooks/grimoire_autoinject.sh b/plugin/hooks/grimoire_autoinject.sh
index c3da7c75..474d0334 100755
--- a/plugin/hooks/grimoire_autoinject.sh
+++ b/plugin/hooks/grimoire_autoinject.sh
@@ -125,6 +125,8 @@ if [ -n "$session_id" ]; then
[ -n "$rule_seen" ] && exclude_q="${exclude_q}&exclude_rule_ids=${rule_seen}"
# What the session actually OPENED, as against what it was shown.
exclude_q="${exclude_q}$(grimoire_held_query "$rule_state_dir/${safe_sid}.opened.ids")"
+ # Which session the rule lines go to, so each is recorded as a delivery.
+ exclude_q="${exclude_q}&session_id=${safe_sid}"
# Which reply shapes it already holds in full: the core is
# sent whole once, and as its one-line reminder on every later turn.
shapefile=$(grimoire_shapes_file "$safe_sid")
diff --git a/plugin/hooks/grimoire_prior_art.sh b/plugin/hooks/grimoire_prior_art.sh
index da8c9f79..5cf88fd1 100755
--- a/plugin/hooks/grimoire_prior_art.sh
+++ b/plugin/hooks/grimoire_prior_art.sh
@@ -240,6 +240,8 @@ if [ -n "$session_id" ]; then
[ -n "$rule_seen" ] && rule_exclude_q="&exclude_rule_ids=${rule_seen}"
# What the session actually OPENED, as against what it was shown.
rule_exclude_q="${rule_exclude_q}$(grimoire_held_query "$state_dir/${safe_sid}.opened.ids")"
+ # Which session the rule lines go to, so each is recorded as a delivery.
+ rule_exclude_q="${rule_exclude_q}&session_id=${safe_sid}"
fi
# Not `|| exit 0`: an unreachable instance must not discard a local finding
diff --git a/plugin/hooks/grimoire_record_opened.sh b/plugin/hooks/grimoire_record_opened.sh
index a33b116a..6c338ee9 100644
--- a/plugin/hooks/grimoire_record_opened.sh
+++ b/plugin/hooks/grimoire_record_opened.sh
@@ -79,23 +79,30 @@ printf '%s\n' "$rule_id" | grimoire_rules_append "$state_dir/${safe_sid}.opened.
# answers with one line putting the question to the reader. Nothing is
# mounted by this.
#
-# An open of a rule a hook has just NAMED is not reported. A ranked arm or a
-# moment line put it in front of the reader, and that explains the open; the
-# moment beside it did not, so counting it would propose mounts on whatever
-# moment happened to fire while search was surfacing the rule. The window is
-# short for the same reason: a few minutes of agent work reaches many moments.
+# EVERY OPEN IS REPORTED: the server stamps it on this session's latest
+# delivery of the rule, which is how an open rate per delivery is measured.
+#
+# The acts go with it only when the rule was NOT just named by a hook. A
+# ranked arm or a moment line put a named rule in front of the reader, and
+# that explains the open; the moment beside it did not, so counting it would
+# propose mounts on whatever moment happened to fire while search was
+# surfacing the rule. The window is short for the same reason: a few minutes
+# of agent work reaches many moments.
command -v curl >/dev/null 2>&1 || exit 0
grimoire_config || exit 0
+acts=""
case ",$(grimoire_rules_live "$state_dir/${safe_sid}.rules.ids")," in
- *",${rule_id},"*) exit 0 ;;
+ *",${rule_id},"*) ;;
+ *)
+ acts_file="${TMPDIR:-/tmp}/grimoire-moment/${safe_sid}.acts"
+ now=$(date +%s 2>/dev/null) || now=0
+ if [ -f "$acts_file" ] && [ "$now" -gt 0 ]; then
+ acts=$(awk -F '\t' -v since=$((now - 90)) \
+ '$1 + 0 >= since { sub(/^[^\t]*\t/, ""); print }' "$acts_file" 2>/dev/null \
+ | tail -n 3 | paste -sd ',' - 2>/dev/null)
+ fi
+ ;;
esac
-acts_file="${TMPDIR:-/tmp}/grimoire-moment/${safe_sid}.acts"
-[ -f "$acts_file" ] || exit 0
-now=$(date +%s 2>/dev/null) || exit 0
-acts=$(awk -F '\t' -v since=$((now - 90)) \
- '$1 + 0 >= since { sub(/^[^\t]*\t/, ""); print }' "$acts_file" 2>/dev/null \
- | tail -n 3 | paste -sd ',' - 2>/dev/null)
-[ -n "$acts" ] || exit 0
sid_esc=$(printf '%s' "$session_id" | grimoire_json_escape) || exit 0
event_cwd=$(grimoire_json_pick "$event_flat" '.cwd')
diff --git a/plugin/hooks/grimoire_tool_rules.sh b/plugin/hooks/grimoire_tool_rules.sh
index d53feb61..d12d2f2b 100644
--- a/plugin/hooks/grimoire_tool_rules.sh
+++ b/plugin/hooks/grimoire_tool_rules.sh
@@ -101,6 +101,8 @@ if [ -n "$session_id" ]; then
[ -n "$rule_seen" ] && rule_exclude_q="&exclude_rule_ids=${rule_seen}"
# What the session actually OPENED, as against what it was shown.
rule_exclude_q="${rule_exclude_q}$(grimoire_held_query "$state_dir/${safe_sid}.opened.ids")"
+ # Which session the rule lines go to, so each is recorded as a delivery.
+ rule_exclude_q="${rule_exclude_q}&session_id=${safe_sid}"
# The Systems whose rulings were shown in full — shared with the write-path
# hook, for the reason the rule file is.
rulingsfile="$state_dir/${safe_sid}.rulings.ids"
diff --git a/src/grimoire/app.py b/src/grimoire/app.py
index 51c54798..0835df91 100644
--- a/src/grimoire/app.py
+++ b/src/grimoire/app.py
@@ -198,9 +198,12 @@ def create_app() -> Quart:
from grimoire.services.logging import start_log_retention_loop
from grimoire.services.notifications import start_notification_loop
+ from grimoire.services.rule_deliveries import start_prune_loop as start_rule_delivery_prune
+
start_log_retention_loop()
start_notification_loop()
start_auth_token_retention_loop()
+ start_rule_delivery_prune()
# The proxy hop count saved in Settings decides every request's client
# address, so it is loaded before serving, under a deadline (a failed
diff --git a/src/grimoire/models/__init__.py b/src/grimoire/models/__init__.py
index bfe87230..71ae222c 100644
--- a/src/grimoire/models/__init__.py
+++ b/src/grimoire/models/__init__.py
@@ -60,6 +60,7 @@ from grimoire.models.retrieval_log import RetrievalLog # noqa: E402, F401
from grimoire.models.retrieval_tuning import RetrievalTuningEvent # noqa: E402, F401
from grimoire.models.note_usage import NoteUsageEvent # noqa: E402, F401
from grimoire.models.rule_usage import RuleUsageEvent # noqa: E402, F401
+from grimoire.models.rule_delivery import RuleDelivery # noqa: E402, F401
from grimoire.models.system_usage import SystemUsageEvent # noqa: E402, F401
from grimoire.models.retrieval_judgment import RetrievalJudgment # noqa: E402, F401
from grimoire.models.moment_mapping import MomentMapping # noqa: E402, F401
diff --git a/src/grimoire/models/rule_delivery.py b/src/grimoire/models/rule_delivery.py
new file mode 100644
index 00000000..54144a67
--- /dev/null
+++ b/src/grimoire/models/rule_delivery.py
@@ -0,0 +1,69 @@
+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
+
+FULL = "full"
+REPEAT = "repeat"
+FORMS = (FULL, REPEAT)
+
+
+class RuleDelivery(Base):
+ """One rule line put in front of one session, and whether it was opened after.
+
+ WHAT IT ANSWERS that `rule_usage_events` cannot. A surfaced event and a
+ pulled event are counted separately, so "was THIS delivery opened" has no
+ answer there: a rule opened once in a session reads as pulled whether it
+ was delivered once or twenty times. Here each delivery is a row, and the
+ open is stamped on the delivery it followed — so the open rate per rule,
+ per moment and per arm is a count of rows, and the ordering function reads
+ it as the rule's history at that moment.
+
+ `at` is where the rule was delivered: the moment it was mounted on
+ (`work.finish`) or the arm that found it (`pre_tool_rule`). `form` is
+ whether the line carried the rule's trigger (`full`) or only cited it
+ (`repeat`). Repeats are recorded too: they are deliveries a session can
+ open, and the case for retiring a rule rests on how rarely they are.
+
+ FK-free on `rule_id` and `user_id`, like the other telemetry tables: the
+ evidence that a rule was delivered forty times and opened never outlives
+ the rule, because it is the case for having deleted it.
+ """
+
+ __tablename__ = "rule_deliveries"
+
+ 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)
+ rule_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
+ source: Mapped[str] = mapped_column(Text, nullable=False)
+ at: Mapped[str] = mapped_column(Text, nullable=False)
+ form: Mapped[str] = mapped_column(Text, nullable=False)
+ delivered_at: Mapped[datetime] = mapped_column(
+ DateTime(timezone=True), nullable=False, server_default=func.now(),
+ )
+ opened_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
+
+ __table_args__ = (
+ # Stamping an open: this session's latest delivery of this rule.
+ Index("ix_rule_deliveries_session_rule", "user_id", "session_id", "rule_id", "delivered_at"),
+ # History: a batch of rules at one moment.
+ Index("ix_rule_deliveries_rule_at", "rule_id", "at"),
+ Index("ix_rule_deliveries_delivered_at", "delivered_at"),
+ )
+
+ def to_dict(self) -> dict:
+ return {
+ "id": self.id,
+ "user_id": self.user_id,
+ "session_id": self.session_id,
+ "rule_id": self.rule_id,
+ "source": self.source,
+ "at": self.at,
+ "form": self.form,
+ "delivered_at": iso(self.delivered_at),
+ "opened_at": iso(self.opened_at),
+ }
diff --git a/src/grimoire/routes/plugin.py b/src/grimoire/routes/plugin.py
index 553b931e..4424c4b5 100644
--- a/src/grimoire/routes/plugin.py
+++ b/src/grimoire/routes/plugin.py
@@ -20,6 +20,7 @@ from grimoire.services import plugin_context as plugin_ctx_svc
from grimoire.services import reply_shapes as reply_shapes_svc
from grimoire.services import repo_bindings as repo_bindings_svc
from grimoire.services import report_check as report_check_svc
+from grimoire.services import rule_deliveries as rule_deliveries_svc
from grimoire.services import rule_moment_judgments as judgments_svc
from grimoire.services import shape_check as shape_check_svc
from grimoire.services import task_claims as task_claims_svc
@@ -36,6 +37,22 @@ plugin_bp = Blueprint("plugin", __name__, url_prefix="/api/plugin")
_MARKETPLACE_KEY = "plugin_marketplace_url"
+@plugin_bp.before_request
+async def _bind_delivery_session() -> None:
+ """Bind the session this request delivers to, for the delivery record.
+
+ A hook names it as `session_id` in the query, or carries it in the event
+ it posts (`/moment` sends the PreToolUse event whole). A request that
+ names none records no deliveries and reads no delivery history.
+ """
+ sid = request.args.get("session_id") or ""
+ if not sid and request.method == "POST":
+ body = await request.get_json(silent=True)
+ if isinstance(body, dict):
+ sid = str(body.get("session_id") or "")
+ rule_deliveries_svc.bind_session(sid)
+
+
def _int_list(raw: str | None) -> list[int]:
"""A comma-separated id list from the query string; non-ints dropped."""
return [int(p) for p in (raw or "").split(",") if p.strip().isdigit()]
@@ -336,7 +353,10 @@ async def rule_opened():
"""A session opened a rule shortly after these acts.
Body: `{"rule_id", "session_id", "acts": [, ...]}` —
- the calls the plugin saw in the window before the `get_rule`. Each is
+ the calls the plugin saw in the window before the `get_rule`. Every open
+ is first stamped on this session's latest delivery of the rule, which is
+ how the open rate per delivery is measured; `acts` is empty when the
+ rule was one a hook had just named, which explains the open by itself. Each is
resolved to its moments, and the open is counted as evidence that the
rule belongs on them; once a pair has repeated across enough distinct
sessions, `context` carries the line asking the reader to offer the
@@ -349,8 +369,13 @@ async def rule_opened():
rule_id = int(data.get("rule_id") or 0)
except (TypeError, ValueError):
rule_id = 0
+ if rule_id > 0:
+ # The open, against the delivery it followed — whatever the acts say.
+ await rule_deliveries_svc.mark_opened(
+ g.user.id, str(data.get("session_id") or ""), rule_id,
+ )
acts = data.get("acts")
- if rule_id <= 0 or not isinstance(acts, list):
+ if rule_id <= 0 or not isinstance(acts, list) or not acts:
return jsonify({"moments": [], "context": ""})
project_id, _repo, _unbound = await _project_scope()
return jsonify(await judgments_svc.opened_after(
diff --git a/src/grimoire/services/backup.py b/src/grimoire/services/backup.py
index e27bcc79..452065fc 100644
--- a/src/grimoire/services/backup.py
+++ b/src/grimoire/services/backup.py
@@ -183,6 +183,10 @@ _NOT_INCLUDED = [
"app_logs", "notifications",
"invitation_tokens", "password_reset_tokens",
"retrieval_logs",
+ # Per-session delivery rows, keyed by session ids that mean nothing on
+ # another instance and pruned after months anyway. The ordering reads
+ # them as history; a restored instance rebuilds it within weeks.
+ "rule_deliveries",
# Verdicts on retrieval_logs rows, so they go where their subject
# goes: a restore that carried a judgment of a call it did not carry would
# leave a verdict on nothing. A sample, re-takeable by the review pass.
diff --git a/src/grimoire/services/moment_delivery.py b/src/grimoire/services/moment_delivery.py
index 29c480ef..047f1ec2 100644
--- a/src/grimoire/services/moment_delivery.py
+++ b/src/grimoire/services/moment_delivery.py
@@ -24,11 +24,14 @@ logger = logging.getLogger(__name__)
def _io() -> rp.MomentIO:
"""Resolved at CALL time, so a test patching either module's name is seen."""
- from grimoire.services import rule_usage, rulebooks
+ from grimoire.services import rule_deliveries, rule_usage, rulebooks
+ bound = rule_deliveries.bound()
return rp.MomentIO(
lookup=rulebooks.rules_on_moments,
record_rule_surfaced=rule_usage.record_rule_surfaced,
+ history=rule_deliveries.history if bound else None,
+ record_delivery=rule_deliveries.record if bound else None,
)
diff --git a/src/grimoire/services/plugin_context.py b/src/grimoire/services/plugin_context.py
index cd153ebf..6b7a17dd 100644
--- a/src/grimoire/services/plugin_context.py
+++ b/src/grimoire/services/plugin_context.py
@@ -974,10 +974,15 @@ def _rule_io() -> rp.RuleIO:
the pipeline reports to whatever this module's `semantic_search_rules`,
`record_retrieval` and `record_rule_surfaced` are at the moment it runs.
"""
+ from grimoire.services import rule_deliveries
+
+ bound = rule_deliveries.bound()
return rp.RuleIO(
search=semantic_search_rules,
record_retrieval=record_retrieval,
record_rule_surfaced=record_rule_surfaced,
+ history=rule_deliveries.history if bound else None,
+ record_delivery=rule_deliveries.record if bound else None,
)
diff --git a/src/grimoire/services/retrieval_pipeline.py b/src/grimoire/services/retrieval_pipeline.py
index 0b056805..559dc5bf 100644
--- a/src/grimoire/services/retrieval_pipeline.py
+++ b/src/grimoire/services/retrieval_pipeline.py
@@ -35,6 +35,7 @@ import time
from dataclasses import dataclass, field
from typing import Any, Callable
+from grimoire.services import rule_order
from grimoire.services.lessons import LESSON_NOTE_TYPE, claim_line
logger = logging.getLogger(__name__)
@@ -576,6 +577,12 @@ class RuleIO:
record_retrieval: Callable[..., Any]
record_rule_surfaced: Callable[..., Any]
+ history: Callable[..., Any] | None = None
+ """`rule_deliveries.history`: `{rule_id: open rate}` for (rule, at) pairs.
+ None leaves every rule's history unknown."""
+ record_delivery: Callable[..., Any] | None = None
+ """`rule_deliveries.record`: each line shown, against the session the
+ door bound. None records nothing."""
@dataclass(frozen=True)
@@ -773,6 +780,30 @@ async def _reserve_slot_for_preference(
# ── The pipeline ─────────────────────────────────────────────────────────
+def _record_delivery(io, moment: RuleMoment, source: str, shown: list[tuple[int, str, bool]]) -> None:
+ """Record each line shown — `(rule_id, at, was_a_repeat)` — as a delivery."""
+ if io.record_delivery is None or not shown:
+ return
+ try:
+ io.record_delivery(moment.user_id, None, source, [
+ (rid, at, "repeat" if repeat else "full") for rid, at, repeat in shown
+ ])
+ except Exception: # noqa: BLE001 - observation never breaks the observed
+ _telemetry_failed(source)
+
+
+async def _history(io, moment: RuleMoment, pairs: list[tuple[int, str]]) -> dict[int, float]:
+ """Each rule's open rate where it is about to be delivered; {} when the
+ door has no history to read or the read fails — unknown, never an error."""
+ if io.history is None or not pairs:
+ return {}
+ try:
+ return await io.history(moment.user_id, pairs) or {}
+ except Exception: # noqa: BLE001 - history orders lines, it never withholds them
+ logger.debug("rule delivery history unreadable", exc_info=True)
+ return {}
+
+
async def run_rule_arm(
arm: RuleArm, moment: RuleMoment, *, floor: float, budget: int, io: RuleIO,
checkpoint_floor: float = 0.0,
@@ -801,6 +832,19 @@ async def run_rule_arm(
# still has something to say, it just says it differently.
if not shown:
return RuleResult()
+ # Into the one delivery order: a rule that holds outranks one that
+ # informs whatever their scores, and score breaks the last tie.
+ history = await _history(io, moment, [(rule.id, arm.source) for _s, rule in shown])
+ shown = [
+ c.extra for c in rule_order.ordered(
+ rule_order.Candidate(
+ rule=rule, specificity=rule_order.BY_RESEMBLANCE,
+ opened_rate=history.get(rule.id), similarity=score,
+ seen=rule.id in moment.exclude, extra=(score, rule),
+ )
+ for score, rule in shown
+ )
+ ]
lines = [
_rule_hint_line(
@@ -836,6 +880,11 @@ async def run_rule_arm(
)
except Exception: # noqa: BLE001 - observation never breaks the observed
_telemetry_failed(source)
+ # A stop-only arm shows its lines to nobody: only a hold speaks.
+ if not arm.stop_only:
+ _record_delivery(io, moment, source, [
+ (rule.id, source, rule.id in moment.exclude) for _s, rule in shown
+ ])
return RuleResult(
lines=lines, rule_ids=rule_ids,
shown_rule_ids=[rule.id for _score, rule in shown],
@@ -1597,6 +1646,10 @@ class MomentIO:
"""`rulebooks.rules_on_moments`, or a stand-in with its signature."""
record_rule_surfaced: Callable[..., Any]
+ history: Callable[..., Any] | None = None
+ """As on `RuleIO`."""
+ record_delivery: Callable[..., Any] | None = None
+ """As on `RuleIO`."""
def _reached_by(hit: dict) -> str:
@@ -1626,8 +1679,20 @@ async def run_moment_arm(
if not pairs:
return RuleResult()
by_moment = {hit["moment"]: hit for hit in reached}
- # Stable: catalog order within each half, fresh half first.
- shown = sorted(pairs, key=lambda pair: pair[0].id in moment.exclude)[:limit]
+ # The one delivery order (services/rule_order.py), then the cap — so
+ # what a crowded moment drops is the lowest-ranked, not the last
+ # mounted. Ties keep catalog order.
+ history = await _history(io, moment, [(rule.id, at) for rule, at in pairs])
+ shown = [
+ c.extra for c in rule_order.ordered(
+ rule_order.Candidate(
+ rule=rule, specificity=rule_order.BY_MOUNT,
+ opened_rate=history.get(rule.id),
+ seen=rule.id in moment.exclude, extra=(rule, at),
+ )
+ for rule, at in pairs
+ )
+ ][:limit]
lines = []
for rule, at in shown:
@@ -1651,6 +1716,9 @@ async def run_moment_arm(
)
except Exception: # noqa: BLE001 - observation never breaks the observed
_telemetry_failed(MOMENT_RULE_SOURCE)
+ _record_delivery(io, moment, MOMENT_RULE_SOURCE, [
+ (rule.id, at, rule.id in moment.exclude) for rule, at in shown
+ ])
return RuleResult(
lines=lines, rule_ids=rule_ids,
shown_rule_ids=[rule.id for rule, _at in shown],
diff --git a/src/grimoire/services/rule_deliveries.py b/src/grimoire/services/rule_deliveries.py
new file mode 100644
index 00000000..e98bb2ab
--- /dev/null
+++ b/src/grimoire/services/rule_deliveries.py
@@ -0,0 +1,180 @@
+"""Each rule line a session is shown, whether it was opened after, and what
+that history says about where a rule is worth delivering.
+
+Three calls, one table (`rule_deliveries`):
+
+- `record` — fire-and-forget, from the plugin doors that know the session:
+ every line an arm put in front of it, full or repeat.
+- `mark_opened` — the session opened a rule: the latest unopened delivery of
+ it in that session gets the stamp. An open with no delivery behind it (the
+ agent looked a rule up unprompted) stamps nothing.
+- `history` — for the ordering function: each rule's smoothed open rate at
+ the place it is about to be delivered, over the last `HISTORY_DAYS`.
+
+Deliveries are kept `RETAIN_DAYS` and pruned daily: enough history to rank on
+and to read adherence from, without a row per tool call accumulating forever.
+"""
+from __future__ import annotations
+
+import logging
+import re
+from contextvars import ContextVar
+from datetime import datetime, timedelta, timezone
+
+from sqlalchemy import and_, case, delete, func, select, tuple_, update
+
+from grimoire.models import async_session
+from grimoire.models.rule_delivery import FORMS, FULL, RuleDelivery
+from grimoire.services import rule_order
+from grimoire.services.background import spawn
+
+logger = logging.getLogger(__name__)
+
+HISTORY_DAYS = 90
+RETAIN_DAYS = 180
+# An open counts against a delivery made within this window before it; an
+# older one was read by a context that is long gone.
+OPEN_WINDOW = timedelta(hours=6)
+_SESSION_MAX = 200
+_UNSAFE = re.compile(r"[^A-Za-z0-9._-]")
+
+# The session a plugin request is delivering to, bound once per request by the
+# plugin routes so the arms several calls deep record against it without every
+# signature between carrying it. Unbound (the MCP door, a test) records nothing.
+_session: ContextVar[str] = ContextVar("rule_delivery_session", default="")
+
+
+def bind_session(session_id: str | None) -> None:
+ _session.set(_clean_session(session_id))
+
+
+def bound() -> bool:
+ """Whether this request delivers to a known session — the doors that
+ record deliveries are the ones that read their history back."""
+ return bool(_session.get())
+
+
+def _clean_session(session_id: str | None) -> str:
+ # The plugin's own filename-safe form of the id, so a hook that sends the
+ # raw id and one that sends the safe one name the same session.
+ return _UNSAFE.sub("_", (session_id or "").strip())[:_SESSION_MAX]
+
+
+def record(
+ user_id: int, session_id: str | None, source: str,
+ lines: list[tuple[int, str, str]],
+) -> None:
+ """Record delivered lines: `(rule_id, at, form)` each. Fire-and-forget,
+ and a no-op without a session, since an open could never be tied back.
+ `session_id` None means the one bound for this request."""
+ sid = _clean_session(_session.get() if session_id is None else session_id)
+ if not sid or not lines:
+ return
+ try:
+ rows = [
+ {"user_id": int(user_id), "session_id": sid, "rule_id": int(rid),
+ "source": source, "at": str(at or source), "form": form if form in FORMS else FULL}
+ for rid, at, form in lines
+ ]
+ except Exception: # noqa: BLE001 - observation never breaks the observed
+ logger.debug("rule delivery payload build failed", exc_info=True)
+ return
+ spawn(_insert(rows), site="rule_delivery_write")
+
+
+async def _insert(rows: list[dict]) -> None:
+ async with async_session() as session:
+ session.add_all(RuleDelivery(**row) for row in rows)
+ await session.commit()
+
+
+async def mark_opened(user_id: int, session_id: str | None, rule_id: int) -> bool:
+ """Stamp the latest unopened delivery of this rule in this session.
+
+ Returns whether a delivery was stamped. Only that one: a rule delivered
+ five times and opened once was opened after the fifth line, and the four
+ before it were passed over.
+ """
+ sid = _clean_session(session_id)
+ if not sid or not rule_id:
+ return False
+ now = datetime.now(timezone.utc)
+ async with async_session() as session:
+ latest = (await session.execute(
+ select(RuleDelivery.id)
+ .where(
+ RuleDelivery.user_id == user_id,
+ RuleDelivery.session_id == sid,
+ RuleDelivery.rule_id == rule_id,
+ RuleDelivery.opened_at.is_(None),
+ RuleDelivery.delivered_at >= now - OPEN_WINDOW,
+ )
+ .order_by(RuleDelivery.delivered_at.desc(), RuleDelivery.id.desc())
+ .limit(1)
+ )).scalar_one_or_none()
+ if latest is None:
+ return False
+ await session.execute(
+ update(RuleDelivery).where(RuleDelivery.id == latest).values(opened_at=now)
+ )
+ await session.commit()
+ return True
+
+
+async def history(user_id: int, pairs: list[tuple[int, str]]) -> dict[int, float]:
+ """`{rule_id: smoothed open rate}` at each pair's place, for this user.
+
+ A rule with no delivery there is left out, which the ordering reads as
+ unknown rather than as never opened.
+ """
+ wanted = list(dict.fromkeys((int(rid), str(at)) for rid, at in pairs))
+ if not wanted:
+ return {}
+ since = datetime.now(timezone.utc) - timedelta(days=HISTORY_DAYS)
+ async with async_session() as session:
+ rows = (await session.execute(
+ select(
+ RuleDelivery.rule_id, RuleDelivery.at,
+ func.count(RuleDelivery.id),
+ func.sum(case((RuleDelivery.opened_at.is_not(None), 1), else_=0)),
+ )
+ .where(
+ RuleDelivery.user_id == user_id,
+ RuleDelivery.delivered_at >= since,
+ tuple_(RuleDelivery.rule_id, RuleDelivery.at).in_(wanted),
+ )
+ .group_by(RuleDelivery.rule_id, RuleDelivery.at)
+ )).all()
+ return {
+ int(rid): rule_order.opened_rate(int(delivered), int(opened or 0))
+ for rid, _at, delivered, opened in rows
+ }
+
+
+async def prune(retain_days: int = RETAIN_DAYS) -> int:
+ cutoff = datetime.now(timezone.utc) - timedelta(days=retain_days)
+ async with async_session() as session:
+ result = await session.execute(
+ delete(RuleDelivery).where(and_(RuleDelivery.delivered_at < cutoff))
+ )
+ await session.commit()
+ return int(result.rowcount or 0)
+
+
+_prune_task = None
+
+
+async def _prune_tick() -> None:
+ try:
+ removed = await prune()
+ if removed:
+ logger.info("Rule delivery retention: deleted %d row(s)", removed)
+ except Exception:
+ logger.exception("Rule delivery retention failed")
+
+
+def start_prune_loop() -> None:
+ global _prune_task
+ if _prune_task is None or _prune_task.done():
+ from grimoire.services.background import start_periodic
+ _prune_task = start_periodic(86400, _prune_tick, label="rule_delivery_retention") # daily
diff --git a/src/grimoire/services/rule_order.py b/src/grimoire/services/rule_order.py
new file mode 100644
index 00000000..2f99f672
--- /dev/null
+++ b/src/grimoire/services/rule_order.py
@@ -0,0 +1,80 @@
+"""The one order in which rules reach a moment.
+
+Every arm that puts rules in front of a session — the rules mounted on a
+moment, the rules a typed trigger matched, the rules a semantic search found —
+ranks what it has the same way, so a crowded moment drops the same rules
+whichever arm brought them:
+
+1. **enforcement** — `enforce`, then `hold`, then `inform`. A rule that stops
+ the work is never the one cut to make room for a line beside it.
+2. **salience** — `high`, `normal`, `low`, as the rule's author declared it.
+3. **specificity** — how narrowly the rule was reached. A trigger that named
+ this command or this path says more than a mount on the whole moment, and a
+ mount says more than a resemblance.
+4. **news** — at the same rank, a rule this session has not been shown yet
+ comes before one it has, so a cap cuts a reminder rather than a first
+ mention.
+5. **history** — how often this rule was opened when delivered at this
+ moment before. A rule sessions keep opening here outranks one they keep
+ passing over.
+6. **similarity** — the search score, when there is one. Last, because it is
+ the one signal that says what the work resembles rather than what it is.
+
+Each key is a declared or recorded fact, read in that order, so the outcome
+can be explained line by line: "it outranked that one because it holds".
+"""
+from __future__ import annotations
+
+from collections.abc import Iterable
+from dataclasses import dataclass
+from typing import Any
+
+ENFORCEMENT_RANK = {"enforce": 0, "hold": 1, "inform": 2}
+SALIENCE_RANK = {"high": 0, "normal": 1, "low": 2}
+
+# How narrowly a rule was reached, larger is narrower.
+BY_RESEMBLANCE = 0
+BY_MOUNT = 1
+BY_TRIGGER = 2
+
+# A rule with no delivery history at a moment ranks as if half its deliveries
+# there were opened: unknown is neither proof of use nor of neglect.
+UNKNOWN_HISTORY = 0.5
+
+
+@dataclass(frozen=True)
+class Candidate:
+ """One rule an arm could deliver, with what it knows about how it got here."""
+
+ rule: Any
+ specificity: int = BY_RESEMBLANCE
+ opened_rate: float | None = None
+ """Share of this rule's earlier deliveries at this moment that were opened."""
+ similarity: float | None = None
+ seen: bool = False
+ """Already shown to this session."""
+ extra: Any = None
+ """Whatever the arm needs back with the rule — the moment it was mounted
+ on, its score pair — carried through the sort untouched."""
+
+
+def order_key(c: Candidate) -> tuple:
+ rule = c.rule
+ return (
+ ENFORCEMENT_RANK.get(getattr(rule, "enforcement", None) or "inform", 2),
+ SALIENCE_RANK.get(getattr(rule, "salience", None) or "normal", 1),
+ -c.specificity,
+ c.seen,
+ -(UNKNOWN_HISTORY if c.opened_rate is None else c.opened_rate),
+ -(c.similarity or 0.0),
+ )
+
+
+def ordered(candidates: Iterable[Candidate]) -> list[Candidate]:
+ """The candidates in delivery order. Stable: ties keep the arm's own order."""
+ return sorted(candidates, key=order_key)
+
+
+def opened_rate(delivered: int, opened: int) -> float:
+ """A smoothed open rate, so one delivery opened once does not read as certainty."""
+ return (opened + 1) / (delivered + 2)
diff --git a/tests/test_integration_rule_deliveries.py b/tests/test_integration_rule_deliveries.py
new file mode 100644
index 00000000..8d471147
--- /dev/null
+++ b/tests/test_integration_rule_deliveries.py
@@ -0,0 +1,107 @@
+"""Real-Postgres tests for `rule_deliveries`: a delivery recorded against a
+session, the open stamped on the one it followed, and the history read back."""
+import asyncio
+from datetime import datetime, timedelta, timezone
+
+import pytest
+from sqlalchemy import delete, select, update
+
+from grimoire.models import async_session
+from grimoire.models.rule_delivery import RuleDelivery
+from grimoire.services import background, rule_deliveries
+from tests.helpers import ensure_user
+
+pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
+
+RULE = 9_100_001
+OTHER = 9_100_002
+
+
+async def _user() -> int:
+ async with async_session() as s:
+ user = await ensure_user(s, "rule_delivery_owner")
+ uid = user.id
+ await s.commit()
+ await s.execute(delete(RuleDelivery).where(RuleDelivery.user_id == uid))
+ await s.commit()
+ return uid
+
+
+async def _rows(uid: int) -> list[RuleDelivery]:
+ async with async_session() as s:
+ return list((await s.execute(
+ select(RuleDelivery).where(RuleDelivery.user_id == uid).order_by(RuleDelivery.id)
+ )).scalars().all())
+
+
+async def _record(uid, sid, source, lines):
+ rule_deliveries.record(uid, sid, source, lines)
+ await background.drain()
+
+
+async def test_an_open_is_stamped_on_the_sessions_latest_delivery_only():
+ uid = await _user()
+ await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "full")])
+ await asyncio.sleep(0.01)
+ await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "repeat")])
+ await _record(uid, "s-2", "moment_rule", [(RULE, "work.finish", "full")])
+
+ assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is True
+ rows = await _rows(uid)
+ opened = [(r.session_id, r.form) for r in rows if r.opened_at is not None]
+ assert opened == [("s-1", "repeat")]
+ # A second open with nothing left unopened in the session stamps the earlier one.
+ assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is True
+ assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is False
+
+
+async def test_an_open_with_no_delivery_behind_it_stamps_nothing():
+ uid = await _user()
+ assert await rule_deliveries.mark_opened(uid, "s-1", OTHER) is False
+ assert await rule_deliveries.mark_opened(uid, "", RULE) is False
+
+
+async def test_without_a_session_nothing_is_recorded():
+ uid = await _user()
+ rule_deliveries.bind_session("")
+ await _record(uid, None, "prompt_rule", [(RULE, "prompt_rule", "full")])
+ assert await _rows(uid) == []
+
+
+async def test_a_bound_session_is_the_one_recorded():
+ uid = await _user()
+ rule_deliveries.bind_session("bound s/3")
+ await _record(uid, None, "prompt_rule", [(RULE, "prompt_rule", "full")])
+ # The plugin's filename-safe form, so the raw and safe ids agree.
+ assert [r.session_id for r in await _rows(uid)] == ["bound_s_3"]
+ rule_deliveries.bind_session("")
+
+
+async def test_history_is_the_smoothed_open_rate_at_each_place():
+ uid = await _user()
+ await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "full")] * 3
+ + [(RULE, "work.deliver", "full"), (OTHER, "work.finish", "full")])
+ await rule_deliveries.mark_opened(uid, "s-1", RULE) # one of the five RULE rows
+
+ history = await rule_deliveries.history(uid, [(RULE, "work.finish"), (OTHER, "work.finish")])
+ rows = await _rows(uid)
+ stamped_at = next(r.at for r in rows if r.opened_at is not None)
+ finish_opened = 1 if stamped_at == "work.finish" else 0
+ assert history[RULE] == pytest.approx((finish_opened + 1) / (3 + 2))
+ assert history[OTHER] == pytest.approx(1 / 3)
+ # A place with no delivery is unknown, not zero.
+ assert await rule_deliveries.history(uid, [(RULE, "reply.report")]) == {}
+
+
+async def test_old_deliveries_are_pruned():
+ uid = await _user()
+ await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "full"), (OTHER, "work.finish", "full")])
+ async with async_session() as s:
+ await s.execute(
+ update(RuleDelivery)
+ .where(RuleDelivery.user_id == uid, RuleDelivery.rule_id == RULE)
+ .values(delivered_at=datetime.now(timezone.utc) - timedelta(days=rule_deliveries.RETAIN_DAYS + 1))
+ )
+ await s.commit()
+ assert await rule_deliveries.prune() >= 1
+ assert [r.rule_id for r in await _rows(uid)] == [OTHER]
diff --git a/tests/test_rule_moment_signal_hooks.py b/tests/test_rule_moment_signal_hooks.py
index c0371a4b..01994f02 100644
--- a/tests/test_rule_moment_signal_hooks.py
+++ b/tests/test_rule_moment_signal_hooks.py
@@ -166,7 +166,9 @@ def test_an_open_of_a_rule_a_hook_just_named_is_not_reported(sandbox):
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env)
assert out.stdout == ""
- assert not (tmp / "curl.log").exists()
+ # The open is reported for the delivery record, with no acts to count.
+ assert json.loads((tmp / "curl.body").read_text()) == {
+ "rule_id": 11, "session_id": "s1", "acts": []}
# The open itself is still recorded.
assert (named / "s1.opened.ids").read_text().startswith("11\t")
# A rule no hook named is still reported.
@@ -176,7 +178,7 @@ def test_an_open_of_a_rule_a_hook_just_named_is_not_reported(sandbox):
assert json.loads((tmp / "curl.body").read_text())["rule_id"] == 1
-def test_an_open_with_nothing_in_the_window_sends_nothing(sandbox):
+def test_an_open_with_nothing_in_the_window_sends_only_the_open(sandbox):
tmp, env = sandbox
ledger = tmp / "grimoire-moment"
ledger.mkdir()
@@ -185,9 +187,9 @@ def test_an_open_with_nothing_in_the_window_sends_nothing(sandbox):
)
out = _run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
- "tool_input": {"rule_id": 11}}, env)
+ "tool_input": {"rule_id": 11}}, env, FAKE_CURL_REPLY='{"moments":[],"context":""}')
assert out.stdout == ""
- assert not (tmp / "curl.log").exists()
+ assert json.loads((tmp / "curl.body").read_text())["acts"] == []
def test_session_end_removes_the_sessions_moment_files_and_no_others(sandbox):
diff --git a/tests/test_rule_order.py b/tests/test_rule_order.py
new file mode 100644
index 00000000..773bf628
--- /dev/null
+++ b/tests/test_rule_order.py
@@ -0,0 +1,131 @@
+"""The one delivery order, key by key, and both rule arms reading it."""
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from grimoire.services import retrieval_pipeline as rp
+from grimoire.services import rule_order as ro
+
+
+def _r(rid, enforcement="inform", salience="normal"):
+ return SimpleNamespace(id=rid, title=f"rule {rid}", kind="rule", when_to_apply="when",
+ enforcement=enforcement, salience=salience)
+
+
+def _ids(cands):
+ return [c.rule.id for c in ro.ordered(cands)]
+
+
+def test_a_rule_that_stops_the_work_outranks_any_line_beside_it():
+ cands = [ro.Candidate(_r(1), similarity=0.95),
+ ro.Candidate(_r(2, "hold"), similarity=0.71),
+ ro.Candidate(_r(3, "enforce"), similarity=0.70)]
+ assert _ids(cands) == [3, 2, 1]
+
+
+def test_salience_decides_between_rules_of_the_same_enforcement():
+ cands = [ro.Candidate(_r(1, salience="low")), ro.Candidate(_r(2)),
+ ro.Candidate(_r(3, salience="high"))]
+ assert _ids(cands) == [3, 2, 1]
+
+
+def test_a_trigger_outranks_a_mount_and_a_mount_outranks_a_resemblance():
+ cands = [ro.Candidate(_r(1), specificity=ro.BY_RESEMBLANCE, similarity=0.9),
+ ro.Candidate(_r(2), specificity=ro.BY_MOUNT),
+ ro.Candidate(_r(3), specificity=ro.BY_TRIGGER)]
+ assert _ids(cands) == [3, 2, 1]
+
+
+def test_a_first_mention_outranks_a_reminder_at_the_same_rank():
+ cands = [ro.Candidate(_r(1), seen=True), ro.Candidate(_r(2))]
+ assert _ids(cands) == [2, 1]
+
+
+def test_history_breaks_the_tie_before_similarity_does():
+ cands = [ro.Candidate(_r(1), opened_rate=0.1, similarity=0.9),
+ ro.Candidate(_r(2), opened_rate=0.8, similarity=0.7),
+ ro.Candidate(_r(3), similarity=0.99)]
+ # Unknown history ranks between them, as an even chance.
+ assert _ids(cands) == [2, 3, 1]
+
+
+def test_ties_keep_the_arms_own_order():
+ cands = [ro.Candidate(_r(i)) for i in (4, 2, 9)]
+ assert _ids(cands) == [4, 2, 9]
+
+
+def test_a_rule_with_no_declared_fields_reads_as_inform_normal():
+ bare = SimpleNamespace(id=1)
+ assert ro.order_key(ro.Candidate(bare))[:2] == (2, 1)
+
+
+@pytest.mark.parametrize(("delivered", "opened", "rate"), [(0, 0, 0.5), (1, 1, 2 / 3), (10, 0, 1 / 12)])
+def test_the_open_rate_is_smoothed(delivered, opened, rate):
+ assert ro.opened_rate(delivered, opened) == pytest.approx(rate)
+
+
+PUSH = [{"moment": "work.deliver", "tool": "Bash", "match": "git push", "via": "default"}]
+
+
+@pytest.mark.asyncio
+async def test_the_moment_arm_delivers_in_that_order_before_its_cap():
+ pairs = [(_r(i), "work.deliver") for i in range(1, 11)] + [(_r(99, "hold"), "work.deliver")]
+ result = await rp.run_moment_arm(
+ PUSH, rp.RuleMoment(user_id=1, query="", project_id=2),
+ io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock()),
+ )
+ # The holding rule was mounted last and still leads; the cap cut ordinary ones.
+ assert result.shown_rule_ids[0] == 99
+ assert len(result.shown_rule_ids) == rp.MOMENT_RULE_LIMIT
+
+
+@pytest.mark.asyncio
+async def test_the_semantic_arm_delivers_in_that_order():
+ hits = [(0.91, _r(1)), (0.74, _r(2, "hold"))]
+ io = rp.RuleIO(search=AsyncMock(return_value=hits), record_retrieval=MagicMock(),
+ record_rule_surfaced=MagicMock())
+ result = await rp.run_rule_arm(
+ rp.PROMPT_RULE, rp.RuleMoment(user_id=1, query="q", project_id=2, where="here"),
+ floor=0.5, budget=5, io=io,
+ )
+ assert result.shown_rule_ids[:2] == [2, 1]
+
+
+@pytest.mark.asyncio
+async def test_each_line_shown_is_recorded_as_a_delivery_with_its_form():
+ recorded = MagicMock()
+ pairs = [(_r(5), "work.finish"), (_r(6), "work.finish")]
+ await rp.run_moment_arm(
+ PUSH, rp.RuleMoment(user_id=1, query="", project_id=2, exclude=frozenset({6})),
+ io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(),
+ record_delivery=recorded),
+ )
+ _uid, _sid, source, lines = recorded.call_args.args
+ assert source == rp.MOMENT_RULE_SOURCE
+ assert lines == [(5, "work.finish", "full"), (6, "work.finish", "repeat")]
+
+
+@pytest.mark.asyncio
+async def test_history_reorders_rules_of_equal_rank_and_a_failed_read_is_unknown():
+ pairs = [(_r(5), "work.finish"), (_r(6), "work.finish")]
+
+ async def history(_uid, _pairs):
+ return {6: 0.9, 5: 0.1}
+
+ result = await rp.run_moment_arm(
+ PUSH, rp.RuleMoment(user_id=1, query="", project_id=2),
+ io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(),
+ history=history),
+ )
+ assert result.shown_rule_ids == [6, 5]
+
+ async def broken(_uid, _pairs):
+ raise RuntimeError("no database")
+
+ result = await rp.run_moment_arm(
+ PUSH, rp.RuleMoment(user_id=1, query="", project_id=2),
+ io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(),
+ history=broken),
+ )
+ assert result.shown_rule_ids == [5, 6]
--
2.54.0
From 7df478550fb42c0073ee42d2f86835bc65638e10 Mon Sep 17 00:00:00 2001
From: Bryan Van Deusen
Date: Sun, 22 Nov 2026 18:18:52 -0500
Subject: [PATCH 3/5] test(7728): the pre-tool hook and route agree that
session_id is sent and bound
The hook now names its session for the delivery record, and the plugin
blueprint binds it before every handler. The contract test pins both.
Co-Authored-By: Claude Opus 5.5
---
tests/test_rule_usage_wiring.py | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tests/test_rule_usage_wiring.py b/tests/test_rule_usage_wiring.py
index 9a49d460..60167a40 100644
--- a/tests/test_rule_usage_wiring.py
+++ b/tests/test_rule_usage_wiring.py
@@ -721,7 +721,8 @@ def test_the_hook_and_the_route_agree_on_every_parameter_name():
sent = set(re.findall(r"[?&]([a-z_]+)=", hook))
# `root` and `cwd`: where the command runs, so the paths it
# names can be made repo-relative for the rulings arm.
- assert sent == {"tool", "command", "exclude_rule_ids", "root", "cwd"}, sent
+ # `session_id`: which session the lines go to, for the delivery record.
+ assert sent == {"tool", "command", "exclude_rule_ids", "root", "cwd", "session_id"}, sent
# The project-scope key is the shared helper's to choose: the hook
# splices in `grimoire_scope_query`'s output, which is `repo=` inside a git
@@ -763,6 +764,12 @@ def test_the_hook_and_the_route_agree_on_every_parameter_name():
assert f'request.args.get("{arg}")' in handler, (
f"the hook sends {arg!r} and the route never reads it"
)
+ # The session is bound for every plugin route at once, before the handler.
+ binder = route.split("async def _bind_delivery_session")[1].split("\n@plugin_bp")[0]
+ assert "@plugin_bp.before_request" in route.split("async def _bind_delivery_session")[0][-80:]
+ assert 'request.args.get("session_id")' in binder, (
+ "the hook sends session_id and no route reads it"
+ )
# ── The CALL log is unconditional; the SURFACING log is not ───────────
--
2.54.0
From c9e64450ba6fac6b2d42d5a416f02667616923ca Mon Sep 17 00:00:00 2001
From: Bryan Van Deusen
Date: Sun, 22 Nov 2026 18:24:39 -0500
Subject: [PATCH 4/5] feat(7728): typed triggers on a rule, and a rule says its
piece once per session
Triggers (migration 0130, table rule_triggers): a rule can name the act it
applies to, as a tool, a path glob, a command head or a keyword, checked
exactly against the act rather than by resemblance. With no moment a
trigger delivers its rule on any matching act, even one that reaches no
moment; scoped to a moment, it makes the rule's mount there conditional
on the act matching. A triggered line ranks above a mounted one in the
delivery order and names the trigger that fired. Writable through every
MCP and REST rule door, carried by backup (version 29), and edited in the
rule editor under "Acts that trigger it". The plugin's catch-all hook is
told which tools a trigger can reach.
Lifecycle: a rule's life in a session is one full line, one reminder,
then silence. A repeat whose latest delivery in the session was already a
reminder is left out of both rule arms. A rule that holds stays armed
until it is opened, an opened rule keeps its line, and a full delivery
(after a compaction or once the ledger ages it out) re-arms it. Read from
the delivery records, so it needs no new plugin ledger.
Co-Authored-By: Claude Opus 5.5
---
alembic/versions/0130_rule_triggers.py | 38 +++
frontend/src/api/rulebooks.ts | 15 +
.../components/rules/RuleEditorSlideOver.vue | 102 +++++++
src/grimoire/mcp/tools/rulebooks.py | 76 ++++-
src/grimoire/models/rulebook.py | 16 +
src/grimoire/routes/rulebooks.py | 26 +-
src/grimoire/services/backup.py | 41 ++-
src/grimoire/services/moment_delivery.py | 50 ++-
src/grimoire/services/plugin_context.py | 1 +
src/grimoire/services/retrieval_pipeline.py | 88 +++++-
src/grimoire/services/rule_deliveries.py | 29 +-
src/grimoire/services/rule_triggers.py | 289 ++++++++++++++++++
src/grimoire/services/rulebooks.py | 14 +-
tests/conftest.py | 17 ++
tests/helpers.py | 2 +-
tests/test_integration_rule_deliveries.py | 20 ++
tests/test_integration_rule_triggers.py | 107 +++++++
tests/test_rule_moment_doors.py | 4 +-
tests/test_rule_order.py | 43 +++
tests/test_rule_triggers.py | 152 +++++++++
tests/test_services_backup.py | 4 +-
21 files changed, 1093 insertions(+), 41 deletions(-)
create mode 100644 alembic/versions/0130_rule_triggers.py
create mode 100644 src/grimoire/services/rule_triggers.py
create mode 100644 tests/test_integration_rule_triggers.py
create mode 100644 tests/test_rule_triggers.py
diff --git a/alembic/versions/0130_rule_triggers.py b/alembic/versions/0130_rule_triggers.py
new file mode 100644
index 00000000..c0504d29
--- /dev/null
+++ b/alembic/versions/0130_rule_triggers.py
@@ -0,0 +1,38 @@
+"""rule triggers — typed predicates on the act that deliver a rule, checked in code
+
+Revision ID: 0130
+Revises: 0129
+Create Date: 2026-11-22
+
+A trigger is a kind (tool, path, command, keyword) and a pattern, optionally
+scoped to a moment. Unscoped, it delivers the rule on any act that matches;
+scoped, it makes the rule's mount on that moment conditional on the act
+matching. Matched in code against the act, never by embedding.
+"""
+import sqlalchemy as sa
+from alembic import op
+
+revision = "0130"
+down_revision = "0129"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ op.create_table(
+ "rule_triggers",
+ sa.Column("rule_id", sa.BigInteger(),
+ sa.ForeignKey("rules.id", ondelete="CASCADE"), primary_key=True),
+ sa.Column("kind", sa.Text(), primary_key=True),
+ sa.Column("pattern", sa.Text(), primary_key=True),
+ sa.Column("moment", sa.Text(), primary_key=True, server_default=""),
+ sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
+ sa.CheckConstraint(
+ "kind IN ('tool', 'path', 'command', 'keyword')", name="ck_rule_triggers_kind",
+ ),
+ sa.CheckConstraint("length(pattern) > 0", name="ck_rule_triggers_pattern"),
+ )
+
+
+def downgrade() -> None:
+ op.drop_table("rule_triggers")
diff --git a/frontend/src/api/rulebooks.ts b/frontend/src/api/rulebooks.ts
index d3c19573..7f20f6b1 100644
--- a/frontend/src/api/rulebooks.ts
+++ b/frontend/src/api/rulebooks.ts
@@ -62,6 +62,17 @@ export type RuleKind = "rule" | "preference";
export type RuleEnforcement = "enforce" | "hold" | "inform";
/** How a rule ranks among rules of the same enforcement at a crowded moment. */
export type RuleSalience = "high" | "normal" | "low";
+/** What a trigger is matched against on the act: the tool called, a path it
+ * touches, the head of a command it runs, or a word in what it writes. */
+export type RuleTriggerKind = "tool" | "path" | "command" | "keyword";
+/** A typed predicate on the act, checked exactly. With no `moment` it
+ * delivers the rule on any matching act; with one, it makes the rule's
+ * mount on that moment conditional on the act matching. */
+export interface RuleTrigger {
+ kind: RuleTriggerKind;
+ pattern: string;
+ moment?: string;
+}
/** A new record opened from somewhere that already knows what it is for —
* "Adjust this" on a reply shape opens a preference mounted on that shape's
@@ -105,6 +116,8 @@ export interface Rule {
/** The moments this rule arrives at whenever they happen,
* in catalog order. Present only when the rule is mounted. */
moments?: string[];
+ /** Present only when the rule has triggers. */
+ triggers?: RuleTrigger[];
relations?: RuleRelation[];
/** The lessons that point at this rule — the concrete
* situations judged instances of it, plus any suggested and awaiting a
@@ -241,6 +254,8 @@ export interface RuleWrite {
system_ids: number[];
/** Replaces the set; [] unmounts the rule from every moment. */
moments: string[];
+ /** Replaces the set; [] clears every trigger. */
+ triggers: RuleTrigger[];
arose_from_id: number | null;
verify_with: string;
expires_when: string;
diff --git a/frontend/src/components/rules/RuleEditorSlideOver.vue b/frontend/src/components/rules/RuleEditorSlideOver.vue
index b0f9954f..7fcdbd52 100644
--- a/frontend/src/components/rules/RuleEditorSlideOver.vue
+++ b/frontend/src/components/rules/RuleEditorSlideOver.vue
@@ -9,6 +9,7 @@ import { apiErrorMessage } from "@/api/client";
import {
listRulebooks, listTopics,
type Rule, type RuleEnforcement, type RuleKind, type RulePreset, type RuleSalience,
+ type RuleTrigger, type RuleTriggerKind,
} from "@/api/rulebooks";
const props = defineProps<{
@@ -50,6 +51,44 @@ const momentCatalog = computed(() => momentsStore.data?.moments ?? []);
const procedureMoments = computed(() =>
ruleMoments.value.filter((m) => m.startsWith(SKILL_PREFIX)),
);
+// The acts this rule is triggered by, checked exactly on the server.
+const triggers = ref([]);
+const triggerKind = ref("path");
+const triggerPattern = ref("");
+const triggerMoment = ref("");
+const triggerError = ref("");
+const TRIGGER_KINDS: { value: RuleTriggerKind; label: string; placeholder: string }[] = [
+ { value: "path", label: "Path", placeholder: "alembic/versions/*.py — a file the act touches" },
+ { value: "command", label: "Command", placeholder: "git push — the head of a command it runs" },
+ { value: "tool", label: "Tool", placeholder: "update_task — the tool it calls" },
+ { value: "keyword", label: "Keyword", placeholder: "migration — a word in what it writes" },
+];
+const triggerPlaceholder = computed(
+ () => TRIGGER_KINDS.find((k) => k.value === triggerKind.value)?.placeholder ?? "",
+);
+
+function triggerLabel(t: RuleTrigger) {
+ return TRIGGER_KINDS.find((k) => k.value === t.kind)?.label ?? t.kind;
+}
+
+function addTrigger() {
+ const pattern = triggerPattern.value.trim().replace(/\s+/g, " ");
+ if (!pattern) return;
+ const moment = triggerMoment.value || undefined;
+ const same = (t: RuleTrigger) =>
+ t.kind === triggerKind.value && t.pattern === pattern && (t.moment ?? "") === (moment ?? "");
+ if (triggers.value.some(same)) {
+ triggerError.value = "This rule already has that trigger.";
+ return;
+ }
+ triggerError.value = "";
+ triggers.value.push({ kind: triggerKind.value, pattern, ...(moment ? { moment } : {}) });
+ triggerPattern.value = "";
+}
+
+function removeTrigger(at: number) {
+ triggers.value.splice(at, 1);
+}
const why = ref("");
const howToApply = ref("");
const verifyWith = ref("");
@@ -138,6 +177,7 @@ async function load() {
salience.value = r.salience ?? "normal";
systemIds.value = (r.systems ?? []).map((sys) => sys.id);
ruleMoments.value = [...(r.moments ?? [])];
+ triggers.value = (r.triggers ?? []).map((t) => ({ ...t }));
why.value = r.why || "";
howToApply.value = r.how_to_apply || "";
verifyWith.value = r.verify_with || "";
@@ -152,6 +192,7 @@ async function load() {
salience.value = "normal";
systemIds.value = [];
ruleMoments.value = [...(props.preset?.moments ?? [])];
+ triggers.value = [];
why.value = "";
howToApply.value = "";
verifyWith.value = "";
@@ -159,6 +200,9 @@ async function load() {
}
procedureDraft.value = "";
procedureError.value = "";
+ triggerPattern.value = "";
+ triggerMoment.value = "";
+ triggerError.value = "";
await Promise.all([canon.fetchCatalog(), momentsStore.load(), loadHomes()]);
}
@@ -204,6 +248,8 @@ async function save() {
// Always sent, for system_ids' reason: the server reads the list as the
// whole set, so unticking the last moment actually unmounts the rule.
moments: ruleMoments.value,
+ // Always sent, for the same reason: the list is the whole set.
+ triggers: triggers.value,
why: why.value,
how_to_apply: howToApply.value,
// Always sent, including empty. The REST door maps "" to NULL, so
@@ -391,6 +437,55 @@ watch(() => props.ruleId, load);