feat(7733): adherence is measured per rule and per moment, and shown
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 16s
CI & Build / TypeScript typecheck (push) Successful in 36s
CI & Build / Python tests (push) Failing after 1m48s
CI & Build / integration (push) Successful in 2m26s
CI & Build / Build & push image (push) Skipped

A rule is in force at a moment of a session when it was delivered there.
Each (session, rule, place) now carries at most one verdict (rule_adherence,
migration 0134): followed, departed, broken or not applicable, with where it
came from:

- an enforce rule refusing the act: followed, recorded by the moment arm, and
  never replaced;
- the session's own rule_outcome (applied, departed) and rule_misfired calls,
  which the plugin's outcome hook now also posts with the session id
  (/api/plugin/rule-verdict) onto the delivery being answered;
- a transcript review: adherence_to_judge lists what has no verdict yet, by
  session, and judge_adherence records verdicts with their evidence.

rule_adherence (MCP) and /api/retrieval/adherence give the readout: sessions
in force, opened and each verdict per rule and per place. Settings > Moments
shows it in a "Were they followed" panel, beside each moment's delivered and
opened counts, and per rule inside each moment.

Verdicts are pruned with their deliveries and left out of backups like them.
Plugin 2026.11.23.2227.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 17:28:23 -05:00
co-authored by Claude Opus 5.5
parent e70b2c2c00
commit b4bdd7798c
25 changed files with 1114 additions and 25 deletions
+15 -5
View File
@@ -59,7 +59,7 @@ REPORTS_OPENS = RuleDelivery.plugin.is_not(None)
def bind_session(session_id: str | None, plugin: str | None = None) -> None:
_session.set(_clean_session(session_id))
_session.set(clean_session(session_id))
_plugin.set(_UNSAFE.sub("_", (plugin or "").strip())[:_PLUGIN_MAX])
_pending.set([])
@@ -84,7 +84,12 @@ def bound() -> bool:
return bool(_session.get())
def _clean_session(session_id: str | None) -> str:
def current_session() -> str:
"""The session this request delivers to, or "" outside a plugin request."""
return _session.get()
def clean_session(session_id: str | None) -> str:
# The plugin's own filename-safe form of the id, so a hook that sends the
# raw id and one that sends the safe one name the same session.
return _UNSAFE.sub("_", (session_id or "").strip())[:_SESSION_MAX]
@@ -97,7 +102,7 @@ def record(
"""Record delivered lines: `(rule_id, at, form)` each. A no-op without a
session, since an open could never be tied back. `session_id` None means
the one bound for this request, whose rows wait for `flush`."""
sid = _clean_session(_session.get() if session_id is None else session_id)
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
@@ -131,7 +136,7 @@ async def mark_opened(user_id: int, session_id: str | None, rule_id: int) -> boo
five times and opened once was opened after the fifth line, and the four
before it were passed over.
"""
sid = _clean_session(session_id)
sid = clean_session(session_id)
if not sid or not rule_id:
return False
now = datetime.now(timezone.utc)
@@ -217,13 +222,18 @@ async def retired(user_id: int, rule_ids: list[int]) -> set[int]:
async def prune(retain_days: int = RETAIN_DAYS) -> int:
"""Delete deliveries older than `retain_days`, and the adherence verdicts
on them (`rule_adherence`), which mean nothing without their delivery."""
from grimoire.services import rule_adherence
cutoff = datetime.now(timezone.utc) - timedelta(days=retain_days)
async with async_session() as session:
result = await session.execute(
delete(RuleDelivery).where(and_(RuleDelivery.delivered_at < cutoff))
)
await session.commit()
return int(result.rowcount or 0)
await rule_adherence.prune(cutoff)
return int(result.rowcount or 0)
_prune_task = None