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
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:
@@ -13,6 +13,10 @@ Three calls, one table (`rule_deliveries`):
|
||||
- `history` — for the ordering function: each rule's smoothed open rate at
|
||||
the place it is about to be delivered, over the last `HISTORY_DAYS`.
|
||||
|
||||
An open rate counts only deliveries that carry the plugin version (`REPORTS_OPENS`):
|
||||
a plugin that does not send it does not report opens either, so its
|
||||
deliveries would read as never opened however often they were read.
|
||||
|
||||
Deliveries are kept `RETAIN_DAYS` and pruned daily: enough history to rank on
|
||||
and to read adherence from, without a row per tool call accumulating forever.
|
||||
"""
|
||||
@@ -46,10 +50,17 @@ _UNSAFE = re.compile(r"[^A-Za-z0-9._-]")
|
||||
_session: ContextVar[str] = ContextVar("rule_delivery_session", default="")
|
||||
# Rows rendered during the bound request, written by `flush`.
|
||||
_pending: ContextVar[list | None] = ContextVar("rule_delivery_pending", default=None)
|
||||
# The plugin version that sent the bound request (`X-Grimoire-Plugin`).
|
||||
_plugin: ContextVar[str] = ContextVar("rule_delivery_plugin", default="")
|
||||
_PLUGIN_MAX = 64
|
||||
|
||||
# The deliveries an open rate is read from: those whose plugin reports opens.
|
||||
REPORTS_OPENS = RuleDelivery.plugin.is_not(None)
|
||||
|
||||
|
||||
def bind_session(session_id: str | None) -> None:
|
||||
def bind_session(session_id: str | None, plugin: str | None = None) -> None:
|
||||
_session.set(_clean_session(session_id))
|
||||
_plugin.set(_UNSAFE.sub("_", (plugin or "").strip())[:_PLUGIN_MAX])
|
||||
_pending.set([])
|
||||
|
||||
|
||||
@@ -89,10 +100,12 @@ def record(
|
||||
sid = _clean_session(_session.get() if session_id is None else session_id)
|
||||
if not sid or not lines:
|
||||
return
|
||||
plugin = _plugin.get() or None
|
||||
try:
|
||||
rows = [
|
||||
{"user_id": int(user_id), "session_id": sid, "rule_id": int(rid),
|
||||
"source": source, "at": str(at or source), "form": form if form in FORMS else FULL}
|
||||
"source": source, "at": str(at or source), "form": form if form in FORMS else FULL,
|
||||
"plugin": plugin}
|
||||
for rid, at, form in lines
|
||||
]
|
||||
except Exception: # noqa: BLE001 - observation never breaks the observed
|
||||
@@ -148,7 +161,8 @@ async def history(user_id: int, pairs: list[tuple[int, str]]) -> dict[int, float
|
||||
"""`{rule_id: smoothed open rate}` at each pair's place, for this user.
|
||||
|
||||
A rule with no delivery there is left out, which the ordering reads as
|
||||
unknown rather than as never opened.
|
||||
unknown rather than as never opened. So is one delivered only to
|
||||
sessions whose plugin does not report opens.
|
||||
"""
|
||||
wanted = list(dict.fromkeys((int(rid), str(at)) for rid, at in pairs))
|
||||
if not wanted:
|
||||
@@ -164,6 +178,7 @@ async def history(user_id: int, pairs: list[tuple[int, str]]) -> dict[int, float
|
||||
.where(
|
||||
RuleDelivery.user_id == user_id,
|
||||
RuleDelivery.delivered_at >= since,
|
||||
REPORTS_OPENS,
|
||||
tuple_(RuleDelivery.rule_id, RuleDelivery.at).in_(wanted),
|
||||
)
|
||||
.group_by(RuleDelivery.rule_id, RuleDelivery.at)
|
||||
|
||||
Reference in New Issue
Block a user