fix(7733): a rule's open rate counts only deliveries whose plugin reports opens
CI & Build / TypeScript typecheck (push) Canceled after 0s
CI & Build / Plugin hooks (push) Canceled after 0s
CI & Build / Python lint (push) Canceled after 0s
CI & Build / Python tests (push) Canceled after 0s
CI & Build / integration (push) Canceled after 0s
CI & Build / Build & push image (push) Canceled after 0s

Each rule line a hook delivers now records the plugin version that asked for
it (the X-Grimoire-Plugin header every hook request sends). The open rate
used to order rules, and the delivery history the weighing pass reads,
count only deliveries that carry one.

A plugin before 2026.11.22.2312 never reported an open of a rule a hook had
just named, so every delivery made to such a session read as unopened. The
server shipped delivery recording from dev on 2026-11-22. The plugin ships
from main, and a running session keeps the plugin it started with, so those
sessions kept arriving. They are why opens read about 1 in 600. Existing
rows have no version and drop out of the rate; nothing is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 15:52:42 -05:00
co-authored by Claude Opus 5.5
parent 3a11c11543
commit e0621713d4
24 changed files with 150 additions and 17 deletions
+2 -2
View File
@@ -85,13 +85,13 @@ def _bound_request_records_only_kept():
spawned = MagicMock()
with patch.object(rule_deliveries, "spawn", spawned), \
patch.object(rule_deliveries, "_insert", lambda rows: rows):
rule_deliveries.bind_session("s-1")
rule_deliveries.bind_session("s-1", "2026.11.23.2100")
rule_deliveries.record(1, None, "pre_tool_rule", [(11, "pre_tool_rule", "full"),
(34, "pre_tool_rule", "full")])
assert not spawned.called, "a bound request's rows wait for the payload"
rule_deliveries.flush({11})
rows = spawned.call_args.args[0]
assert [r["rule_id"] for r in rows] == [11]
assert [(r["rule_id"], r["plugin"]) for r in rows] == [(11, "2026.11.23.2100")]
spawned.reset_mock()
rule_deliveries.flush()
assert not spawned.called, "the buffer is spent once flushed"
+41 -5
View File
@@ -15,6 +15,9 @@ pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine"
RULE = 9_100_001
OTHER = 9_100_002
# A plugin version, as the `X-Grimoire-Plugin` header carries it. Deliveries
# count toward an open rate only when their plugin sent one.
PLUGIN = "2026.11.23.2100"
async def _user() -> int:
@@ -34,12 +37,19 @@ async def _rows(uid: int) -> list[RuleDelivery]:
)).scalars().all())
async def _record(uid, sid, source, lines):
async def _record(uid, sid, source, lines, plugin=PLUGIN):
# A bound request's rows wait for the door's payload; flushing all of
# them is what a door does when every line it rendered was kept.
rule_deliveries.record(uid, sid, source, lines)
rule_deliveries.flush()
await background.drain()
# them is what a door does when every line it rendered was kept. A named
# session is recorded as a request from `plugin` would be.
if sid is not None:
rule_deliveries.bind_session("", plugin)
try:
rule_deliveries.record(uid, sid, source, lines)
rule_deliveries.flush()
await background.drain()
finally:
if sid is not None:
rule_deliveries.bind_session("")
async def test_an_open_is_stamped_on_the_sessions_latest_delivery_only():
@@ -96,6 +106,32 @@ async def test_history_is_the_smoothed_open_rate_at_each_place():
assert await rule_deliveries.history(uid, [(RULE, "reply.report")]) == {}
async def test_a_delivery_keeps_the_plugin_version_that_asked_for_it():
uid = await _user()
rule_deliveries.bind_session("s-p", PLUGIN)
try:
await _record(uid, None, "prompt_rule", [(RULE, "prompt_rule", "full")])
finally:
rule_deliveries.bind_session("")
await _record(uid, "s-old", "prompt_rule", [(RULE, "prompt_rule", "full")], plugin=None)
assert [(r.session_id, r.plugin) for r in await _rows(uid)] == [("s-p", PLUGIN), ("s-old", None)]
async def test_history_counts_only_deliveries_whose_plugin_reports_opens():
uid = await _user()
# An older plugin never reported these opens, so they read as unopened
# however often they were read; the rate is taken over the others.
await _record(uid, "s-old", "moment_rule", [(RULE, "work.finish", "full")] * 4, plugin=None)
await _record(uid, "s-old", "moment_rule", [(OTHER, "work.finish", "full")], plugin=None)
await _record(uid, "s-new", "moment_rule", [(RULE, "work.finish", "full")])
await rule_deliveries.mark_opened(uid, "s-new", RULE)
history = await rule_deliveries.history(uid, [(RULE, "work.finish"), (OTHER, "work.finish")])
assert history[RULE] == pytest.approx((1 + 1) / (1 + 2))
# Delivered only to a plugin that does not report opens: unknown, not zero.
assert OTHER not in history
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")])
@@ -195,8 +195,11 @@ async def test_the_pass_reads_each_rule_with_its_history(world):
for i, (at, opened) in enumerate([("work.deliver", True), ("work.deliver", False),
("pre_tool_rule", False)]):
s.add(RuleDelivery(user_id=uid, session_id=f"s{i}", rule_id=push.id,
source="hook", at=at, form="full",
source="hook", at=at, form="full", plugin="2026.11.23.2100",
opened_at=now if opened else None))
# From a plugin that does not report opens: not part of the history.
s.add(RuleDelivery(user_id=uid, session_id="s-old", rule_id=push.id,
source="hook", at="work.deliver", form="full"))
s.add(RuleUsageEvent(user_id=uid, rule_id=push.id, event="departed",
source="mcp_rule_outcome", detail="the operator asked"))
await s.commit()
+16
View File
@@ -133,6 +133,22 @@ def test_an_open_sends_the_window_and_prints_the_answer(sandbox):
assert (tmp / "grimoire-priorart" / "s1.opened.ids").read_text().startswith("11\t")
def test_every_request_names_the_plugin_version_that_sent_it(sandbox):
# The server counts a delivery toward a rule's open rate only when its
# plugin sent its version, since that is the plugin that reports opens.
tmp, env = sandbox
version = json.loads((ROOT / "plugin" / ".claude-plugin" / "plugin.json").read_text())["version"]
_run("grimoire_moment.sh", {"session_id": "s1", "tool_name": "Bash",
"tool_input": {"command": "ls"}}, env,
FAKE_CURL_REPLY='{"tools":["bash"]}')
_run("grimoire_record_opened.sh",
{"session_id": "s1", "tool_name": "mcp__grimoire__get_rule",
"tool_input": {"rule_id": 11}}, env)
calls = (tmp / "curl.log").read_text().splitlines()
assert len(calls) >= 2
assert all(f"X-Grimoire-Plugin: {version}" in call for call in calls), calls
def test_the_window_is_the_last_three_acts(sandbox):
tmp, env = sandbox
ledger = tmp / "grimoire-moment"