feat(7728): typed triggers on a rule, and a rule says its piece once per session
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Plugin hooks (push) Successful in 22s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Python tests (push) Failing after 1m48s
CI & Build / integration (push) Successful in 2m8s
CI & Build / Build & push image (push) Skipped

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 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 18:24:39 -05:00
co-authored by Claude Opus 5.5
parent 7df478550f
commit c9e64450ba
21 changed files with 1093 additions and 41 deletions
+23 -3
View File
@@ -40,6 +40,17 @@ def _moments_or_refusal(data: dict):
return None, (jsonify({"error": str(exc)}), 400)
def _triggers_or_refusal(data: dict):
"""The request's `triggers`, validated before any write, as `moments` is.
Absent means "leave them alone" (None)."""
from grimoire.services.rule_triggers import require_triggers
try:
return require_triggers(data.get("triggers")), None
except ValueError as exc:
return None, (jsonify({"error": str(exc)}), 400)
# ── Rulebooks ───────────────────────────────────────────────────────────
@rulebooks_bp.get("/rulebooks")
@@ -194,6 +205,9 @@ async def create_rule(topic_id: int):
if not title or not statement:
return jsonify({"error": "title and statement are required"}), 400
moments, refused = _moments_or_refusal(data)
if refused:
return refused
triggers, refused = _triggers_or_refusal(data)
if refused:
return refused
declared, refused = _declared_or_refusal(data)
@@ -223,7 +237,7 @@ async def create_rule(topic_id: int):
except ValueError as exc:
return jsonify({"error": str(exc)}), 404
return jsonify(await rulebooks_svc.rule_detail(
get_current_user_id(), rule, data.get("system_ids"), moments,
get_current_user_id(), rule, data.get("system_ids"), moments, triggers=triggers,
)), 201
@@ -258,6 +272,9 @@ async def update_rule(rule_id: int):
# door needs the explicit list only because "" already means "unchanged"
# there — two idioms, one outcome.
moments, refused = _moments_or_refusal(data)
if refused:
return refused
triggers, refused = _triggers_or_refusal(data)
if refused:
return refused
try:
@@ -267,7 +284,7 @@ async def update_rule(rule_id: int):
if rule is None:
return jsonify({"error": "rule not found"}), 404
return jsonify(await rulebooks_svc.rule_detail(
uid, rule, data.get("system_ids"), moments,
uid, rule, data.get("system_ids"), moments, triggers=triggers,
))
@@ -443,6 +460,9 @@ async def create_project_rule(project_id: int):
}), 400
title = (data.get("title") or "").strip() or statement.split(".")[0][:50]
moments, refused = _moments_or_refusal(data)
if refused:
return refused
triggers, refused = _triggers_or_refusal(data)
if refused:
return refused
declared, refused = _declared_or_refusal(data)
@@ -472,7 +492,7 @@ async def create_project_rule(project_id: int):
except ValueError as exc:
return jsonify({"error": str(exc)}), 404
return jsonify(await rulebooks_svc.rule_detail(
get_current_user_id(), rule, data.get("system_ids"), moments,
get_current_user_id(), rule, data.get("system_ids"), moments, triggers=triggers,
)), 201