fix(7733): a rule with triggers holds or refuses only the acts they match
CI & Build / TypeScript typecheck (push) Successful in 35s
CI & Build / Plugin hooks (push) Failing after 19s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Python tests (push) Successful in 2m31s
CI & Build / integration (push) Successful in 2m18s
CI & Build / Build & push image (push) Canceled after 27s

Rules 2, 4, 10 and 153 are set to hold and mounted on work.deliver. A
push to dev therefore stopped four times in a row, once for each of them,
although none of them governs a push to dev: their triggers (git push
origin main, git tag, pytest, pull_request_write) name the acts they do
govern.

A rule that has triggers now stops an act only when one of them matched it.
Reached by its moment alone, it is a line beside the work. A rule with no
triggers still holds wherever it is mounted, because the mount is all that
says where it applies. A mounted rule whose unscoped trigger also matched
now counts as reached by the trigger, so it ranks as triggered and may
stop the act.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 15:53:36 -05:00
co-authored by Claude Opus 5.5
parent e0621713d4
commit 738bd24d6e
4 changed files with 48 additions and 4 deletions
+2 -1
View File
@@ -99,7 +99,8 @@ onMounted(load);
Suggested from each rule's text and what happened when it reached sessions. Inform is a Suggested from each rule's text and what happened when it reached sessions. Inform is a
line beside the work; hold makes the act or reply wait until the rule has been read once line beside the work; hold makes the act or reply wait until the rule has been read once
(file edits are never held); enforce refuses the act it governs. A trigger reaches the (file edits are never held); enforce refuses the act it governs. A trigger reaches the
rule exactly, by the tool, path, command or word the act carries. rule exactly, by the tool, path, command or word the act carries; a rule with triggers
holds or refuses only the acts they match, and elsewhere it is a line.
</p> </p>
<ul class="proposal-list"> <ul class="proposal-list">
<li v-for="rule in rules" :key="rule.id" class="proposal-rule"> <li v-for="rule in rules" :key="rule.id" class="proposal-rule">
+13 -3
View File
@@ -1748,10 +1748,13 @@ async def run_moment_arm(
by_moment = {hit["moment"]: hit for hit in reached} by_moment = {hit["moment"]: hit for hit in reached}
# (rule, at, where, specificity) for every candidate line. # (rule, at, where, specificity) for every candidate line.
cands = [] cands = []
# A mounted rule whose unscoped trigger also matched this act was
# reached by its trigger: it ranks, and may stop the act, as one.
fired_ids = {rule.id for rule, _trig in fired}
for rule, at in pairs or []: for rule, at in pairs or []:
if board is not None and (rule.id, at) in board.gated: if board is not None and (rule.id, at) in board.gated:
continue continue
met = board is not None and (rule.id, at) in board.matched met = board is not None and ((rule.id, at) in board.matched or rule.id in fired_ids)
cands.append((rule, at, f"at {at}, reached by {_reached_by(by_moment.get(at, {}))}", cands.append((rule, at, f"at {at}, reached by {_reached_by(by_moment.get(at, {}))}",
rule_order.BY_TRIGGER if met else rule_order.BY_MOUNT)) rule_order.BY_TRIGGER if met else rule_order.BY_MOUNT))
taken = {c[0].id for c in cands} taken = {c[0].id for c in cands}
@@ -1762,9 +1765,16 @@ async def run_moment_arm(
cands.append((rule, trig.key, f"its trigger {trig.describe()} matched this act", cands.append((rule, trig.key, f"its trigger {trig.describe()} matched this act",
rule_order.BY_TRIGGER)) rule_order.BY_TRIGGER))
# Read before retirement: a rule that enforces does so on every act it # Read before retirement: a rule that enforces does so on every act it
# reaches, however often this session has been shown it. # reaches, however often this session has been shown it. A rule with
# triggers of its own stops only an act one of them matched: its
# triggers name the acts it governs, and its moment is broader (a
# rule about pushing to `main` mounted on work.deliver would hold
# every push to `dev`). Reached by the mount alone, it is a line.
triggered = board.triggered if board is not None else set()
decision = act_decision( decision = act_decision(
[(rule, at, where) for rule, at, where, _s in cands], held=moment.held, [(rule, at, where) for rule, at, where, spec in cands
if spec == rule_order.BY_TRIGGER or rule.id not in triggered],
held=moment.held,
) )
gone = await _retired(io, moment, [rule for rule, _a, _w, _s in cands]) gone = await _retired(io, moment, [rule for rule, _a, _w, _s in cands])
if gone: if gone:
+6
View File
@@ -247,11 +247,15 @@ class Board:
condition this act did not meet: not delivered. `matched` — conditional condition this act did not meet: not delivered. `matched` — conditional
mounts this act met: delivered as triggered. `fired` — rules an unscoped mounts this act met: delivered as triggered. `fired` — rules an unscoped
trigger delivers on this act, each with the trigger that fired. trigger delivers on this act, each with the trigger that fired.
`triggered` — every in-scope rule that has a trigger at all, whether or
not this act met it: such a rule names the acts it governs, so a mount
alone shows it beside the work and only its trigger can stop an act.
""" """
gated: set[tuple[int, str]] = field(default_factory=set) gated: set[tuple[int, str]] = field(default_factory=set)
matched: set[tuple[int, str]] = field(default_factory=set) matched: set[tuple[int, str]] = field(default_factory=set)
fired: list[tuple[object, Trigger]] = field(default_factory=list) fired: list[tuple[object, Trigger]] = field(default_factory=list)
triggered: set[int] = field(default_factory=set)
async def board( async def board(
@@ -261,6 +265,8 @@ async def board(
"""Check every in-scope trigger against this act (or this text).""" """Check every in-scope trigger against this act (or this text)."""
out = Board() out = Board()
for rule, triggers in await _in_scope(user_id, project_id): for rule, triggers in await _in_scope(user_id, project_id):
if triggers:
out.triggered.add(rule.id)
first_unscoped = None first_unscoped = None
for trig in triggers: for trig in triggers:
hit = matches(trig, tool, tool_input, text) hit = matches(trig, tool, tool_input, text)
+27
View File
@@ -148,5 +148,32 @@ async def test_a_rule_both_mounted_and_fired_is_delivered_once():
assert result.shown_rule_ids == [1] assert result.shown_rule_ids == [1]
async def test_a_rule_with_triggers_stops_only_the_acts_they_match():
# Rule 2 holds pushes to main; mounted on work.deliver it reaches every
# push, and only its own trigger may make one wait.
guard = _r(2, enforcement="hold")
pairs = [(guard, "work.deliver")]
deliver = [{"moment": "work.deliver", "tool": "Bash", "match": "git push", "via": "default"}]
to_dev = rt.Board(triggered={2})
result = await _arm(pairs, to_dev, reached=deliver)
assert result.decision == {} and result.shown_rule_ids == [2]
to_main = rt.Board(triggered={2}, fired=[(guard, T("command", "git push origin main"))])
result = await _arm(pairs, to_main, reached=deliver)
assert result.decision["kind"] == "hold" and result.decision["rule_ids"] == [2]
# With no trigger of its own, the mount is all that says where it applies.
result = await _arm(pairs, rt.Board(), reached=deliver)
assert result.decision["kind"] == "hold"
async def test_the_board_lists_every_rule_that_has_a_trigger(monkeypatch):
monkeypatch.setattr(rt, "_in_scope", AsyncMock(return_value=[
(_r(1), [T("path", "alembic/**", "work.change")]),
(_r(2), [T("command", "git push origin main")]),
(_r(3), []),
]))
out = await rt.board(1, 2, tool="Bash", tool_input={"command": "ls"})
assert out.triggered == {1, 2}
def test_triggered_outranks_mounted_in_the_one_order(): def test_triggered_outranks_mounted_in_the_one_order():
assert rule_order.BY_TRIGGER > rule_order.BY_MOUNT > rule_order.BY_RESEMBLANCE assert rule_order.BY_TRIGGER > rule_order.BY_MOUNT > rule_order.BY_RESEMBLANCE