fix(7735): a rule the session already read counts as opened when it arrives again
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 2m2s
CI & Build / integration (push) Successful in 2m10s
CI & Build / Build & push image (push) Successful in 34s

A delivery to a session that opened the rule within the open window is
stamped opened as it is written, and migration 0135 stamps the rows already
recorded the same way. The adherence readout, the ordering open rate and the
weighing pass read one definition: a rule read early in a long session is not
counted unread each time it is delivered later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 07:06:18 -05:00
co-authored by Claude Opus 5.5
parent c486524b48
commit 78fa165e67
4 changed files with 102 additions and 2 deletions
+3 -1
View File
@@ -20,7 +20,9 @@ class RuleDelivery(Base):
was delivered once or twenty times. Here each delivery is a row, and the
open is stamped on the delivery it followed — so the open rate per rule,
per moment and per arm is a count of rows, and the ordering function reads
it as the rule's history at that moment.
it as the rule's history at that moment. A delivery made to a session that
had already opened the rule is stamped as it is written, with its own
time: it reached a reader who had the rule.
`at` is where the rule was delivered: the moment it was mounted on
(`work.finish`) or the arm that found it (`pre_tool_rule`). `form` is
+31 -1
View File
@@ -9,7 +9,9 @@ Three calls, one table (`rule_deliveries`):
that made it into the payload (`hook_payload.fit`).
- `mark_opened` — the session opened a rule: the latest unopened delivery of
it in that session gets the stamp. An open with no delivery behind it (the
agent looked a rule up unprompted) stamps nothing.
agent looked a rule up unprompted) stamps nothing. A line for a rule the
session has already opened is stamped as it is written: it reached a
reader who has the rule.
- `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`.
@@ -124,11 +126,39 @@ def record(
async def _insert(rows: list[dict]) -> None:
now = datetime.now(timezone.utc)
async with async_session() as session:
read = await _read_already(session, rows, now)
for row in rows:
if (row["user_id"], row["session_id"], row["rule_id"]) in read:
row["opened_at"] = now
session.add_all(RuleDelivery(**row) for row in rows)
await session.commit()
async def _read_already(session, rows: list[dict], now: datetime) -> set:
"""`{(user_id, session_id, rule_id)}` among these rows that the session
opened within `OPEN_WINDOW` before now.
A line for a rule the session has already read is delivered to a reader
who has it, so it is stamped opened as it is written: the open rate, the
adherence readout and the weighing pass all read "opened" as "the session
had this rule", and a rule read early in a long session is no less read
when it arrives again later. Older opens do not count, for the reason
`OPEN_WINDOW` gives.
"""
keys = {(r["user_id"], r["session_id"], r["rule_id"]) for r in rows}
found = await session.execute(
select(RuleDelivery.user_id, RuleDelivery.session_id, RuleDelivery.rule_id)
.where(
tuple_(RuleDelivery.user_id, RuleDelivery.session_id, RuleDelivery.rule_id).in_(list(keys)),
RuleDelivery.opened_at >= now - OPEN_WINDOW,
)
.distinct()
)
return {tuple(k) for k in found.all()}
async def mark_opened(user_id: int, session_id: str | None, rule_id: int) -> bool:
"""Stamp the latest unopened delivery of this rule in this session.