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
+43
View File
@@ -1,4 +1,5 @@
"""The one delivery order, key by key, and both rule arms reading it."""
import dataclasses
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
@@ -129,3 +130,45 @@ async def test_history_reorders_rules_of_equal_rank_and_a_failed_read_is_unknown
history=broken),
)
assert result.shown_rule_ids == [5, 6]
@pytest.mark.asyncio
async def test_a_repeat_already_reminded_once_is_retired_unless_it_holds_or_was_opened():
pairs = [(_r(5), "work.finish"), (_r(6), "work.finish"), (_r(7, "hold"), "work.finish"),
(_r(8), "work.finish")]
asked = {}
async def retired(_uid, ids):
asked["ids"] = sorted(ids)
return set(ids)
result = await rp.run_moment_arm(
PUSH, rp.RuleMoment(user_id=1, query="", project_id=2,
exclude=frozenset({6, 7, 8}), held=frozenset({8})),
io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(),
retired=retired),
)
# Only the unopened, non-holding repeat is asked about, and only it goes.
assert asked["ids"] == [6]
assert sorted(result.shown_rule_ids) == [5, 7, 8]
@pytest.mark.asyncio
async def test_the_semantic_arm_retires_the_same_way_and_a_failed_read_retires_nothing():
hits = [(0.91, _r(1)), (0.80, _r(2))]
async def retired(_uid, ids):
return {2}
io = rp.RuleIO(search=AsyncMock(return_value=hits), record_retrieval=MagicMock(),
record_rule_surfaced=MagicMock(), retired=retired)
moment = rp.RuleMoment(user_id=1, query="q", project_id=2, where="here", exclude=frozenset({2}))
result = await rp.run_rule_arm(rp.PROMPT_RULE, moment, floor=0.5, budget=5, io=io)
assert result.shown_rule_ids == [1]
async def broken(_uid, ids):
raise RuntimeError("no database")
io = dataclasses.replace(io, retired=broken)
result = await rp.run_rule_arm(rp.PROMPT_RULE, moment, floor=0.5, budget=5, io=io)
assert sorted(result.shown_rule_ids) == [1, 2]