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
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:
@@ -23,6 +23,7 @@ from grimoire.models.family import (
|
||||
)
|
||||
from grimoire.models.rulebook import (
|
||||
RuleRelation, rule_moments as rule_moments_t, rule_systems as rule_systems_t,
|
||||
rule_triggers as rule_triggers_t,
|
||||
)
|
||||
from grimoire.models.lesson_rule_link import LessonNoRule, LessonRuleLink
|
||||
from grimoire.models.rule_moment_judgment import RuleMomentJudgment
|
||||
@@ -113,7 +114,9 @@ logger = logging.getLogger(__name__)
|
||||
# section still restore — the key is simply not read.
|
||||
# v28 instance_id in the header: the id lineage of the store the backup came
|
||||
# from. A live instance rolls back only to a backup with its own id.
|
||||
BACKUP_VERSION = 28
|
||||
# v29 rule_triggers: the typed predicates on the act that deliver a rule or
|
||||
# make its mount conditional. Nothing in the rule's text records them.
|
||||
BACKUP_VERSION = 29
|
||||
|
||||
# Every table this backup carries, by its REAL name. Paired with _NOT_INCLUDED
|
||||
# below, these two lists must together account for the entire schema — which is
|
||||
@@ -158,6 +161,7 @@ _BACKED_UP = [
|
||||
"moment_mappings",
|
||||
"rule_moments",
|
||||
"rule_moment_judgments",
|
||||
"rule_triggers",
|
||||
# Family canon. `platforms` is global like
|
||||
# canonical_systems and rides every export for the same reason: the rows
|
||||
# below name platforms by slug, and a partial catalog restores partial
|
||||
@@ -802,6 +806,15 @@ def _rule_moment_rows(rows) -> list[dict]:
|
||||
return [{"rule_id": rule_id, "moment": moment} for rule_id, moment in rows]
|
||||
|
||||
|
||||
def _rule_trigger_rows(rows) -> list[dict]:
|
||||
"""A rule's triggers. Kind, pattern and moment mean the same on every
|
||||
install, so only the rule id needs remapping on the way back in."""
|
||||
return [
|
||||
{"rule_id": rule_id, "kind": kind, "pattern": pattern, "moment": moment or ""}
|
||||
for rule_id, kind, pattern, moment in rows
|
||||
]
|
||||
|
||||
|
||||
def _rule_moment_judgment_rows(rows) -> list[dict]:
|
||||
"""Proposals and judgments about a rule's moments (v23). The rule id is a
|
||||
SOURCE id, remapped at restore; the moment is a catalog name."""
|
||||
@@ -1020,6 +1033,12 @@ async def export_full_backup() -> dict:
|
||||
select(rule_moments_t.c.rule_id, rule_moments_t.c.moment)
|
||||
.order_by(rule_moments_t.c.rule_id, rule_moments_t.c.moment)
|
||||
)).all()
|
||||
rule_trigger_rows = (await session.execute(
|
||||
select(rule_triggers_t.c.rule_id, rule_triggers_t.c.kind,
|
||||
rule_triggers_t.c.pattern, rule_triggers_t.c.moment)
|
||||
.order_by(rule_triggers_t.c.rule_id, rule_triggers_t.c.kind,
|
||||
rule_triggers_t.c.pattern, rule_triggers_t.c.moment)
|
||||
)).all()
|
||||
rule_moment_judgments = (await session.execute(
|
||||
select(RuleMomentJudgment).order_by(RuleMomentJudgment.id)
|
||||
)).scalars().all()
|
||||
@@ -1112,6 +1131,7 @@ async def export_full_backup() -> dict:
|
||||
"canonical_systems": _canonical_system_rows(canonical_systems),
|
||||
"rule_systems": _rule_system_rows(rule_system_rows),
|
||||
"rule_moments": _rule_moment_rows(rule_moment_rows),
|
||||
"rule_triggers": _rule_trigger_rows(rule_trigger_rows),
|
||||
"rule_moment_judgments": _rule_moment_judgment_rows(rule_moment_judgments),
|
||||
"rule_relations": _rule_relation_rows(rule_relations),
|
||||
"lesson_rule_links": _lesson_rule_link_rows(lesson_rule_links),
|
||||
@@ -1273,6 +1293,11 @@ async def export_user_backup(user_id: int) -> dict:
|
||||
select(rule_moments_t.c.rule_id, rule_moments_t.c.moment)
|
||||
.where(rule_moments_t.c.rule_id.in_(_rule_ids))
|
||||
)).all() if _rule_ids else []
|
||||
rule_trigger_rows = (await session.execute(
|
||||
select(rule_triggers_t.c.rule_id, rule_triggers_t.c.kind,
|
||||
rule_triggers_t.c.pattern, rule_triggers_t.c.moment)
|
||||
.where(rule_triggers_t.c.rule_id.in_(_rule_ids))
|
||||
)).all() if _rule_ids else []
|
||||
rule_moment_judgments = (await session.execute(
|
||||
select(RuleMomentJudgment).where(RuleMomentJudgment.rule_id.in_(_rule_ids))
|
||||
)).scalars().all() if _rule_ids else []
|
||||
@@ -1399,6 +1424,7 @@ async def export_user_backup(user_id: int) -> dict:
|
||||
"canonical_systems": _canonical_system_rows(canonical_systems),
|
||||
"rule_systems": _rule_system_rows(rule_system_rows),
|
||||
"rule_moments": _rule_moment_rows(rule_moment_rows),
|
||||
"rule_triggers": _rule_trigger_rows(rule_trigger_rows),
|
||||
"rule_moment_judgments": _rule_moment_judgment_rows(rule_moment_judgments),
|
||||
"rule_relations": _rule_relation_rows(rule_relations),
|
||||
"lesson_rule_links": _lesson_rule_link_rows(lesson_rule_links),
|
||||
@@ -2421,6 +2447,7 @@ async def _load_v2(session, data: dict, maps: "_Maps", *, keep_ids: bool) -> dic
|
||||
"note_supersessions": 0, "code_shapes": 0, "code_shape_events": 0,
|
||||
"code_shape_uses": 0, "canonical_systems": 0,
|
||||
"rule_systems": 0, "rule_moments": 0, "rule_moment_judgments": 0,
|
||||
"rule_triggers": 0,
|
||||
"rule_relations": 0, "rule_versions": 0,
|
||||
"retrieval_tuning_events": 0, "lesson_rule_links": 0,
|
||||
"lesson_no_rule": 0, "moment_mappings": 0,
|
||||
@@ -2618,6 +2645,18 @@ async def _load_v2(session, data: dict, maps: "_Maps", *, keep_ids: bool) -> dic
|
||||
))
|
||||
stats["rule_moments"] += 1
|
||||
|
||||
# A rule's triggers (v29), the same join-table shape; archives before
|
||||
# v29 carry none.
|
||||
for rt in data.get("rule_triggers", []):
|
||||
mapped_rule = maps.rules.get(rt.get("rule_id", 0))
|
||||
kind, pattern = rt.get("kind"), rt.get("pattern")
|
||||
if mapped_rule is None or kind not in ("tool", "path", "command", "keyword") or not pattern:
|
||||
continue
|
||||
await session.execute(rule_triggers_t.insert().values(
|
||||
rule_id=mapped_rule, kind=kind, pattern=pattern, moment=rt.get("moment") or "",
|
||||
))
|
||||
stats["rule_triggers"] += 1
|
||||
|
||||
# Proposals and judgments about those mounts (v23); archives before
|
||||
# v23 carry none, and every rule restores unjudged.
|
||||
for rj in data.get("rule_moment_judgments", []):
|
||||
|
||||
Reference in New Issue
Block a user