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
+1
View File
@@ -61,6 +61,7 @@ from grimoire.models.retrieval_tuning import RetrievalTuningEvent # noqa: E402,
from grimoire.models.note_usage import NoteUsageEvent # noqa: E402, F401
from grimoire.models.rule_usage import RuleUsageEvent # noqa: E402, F401
from grimoire.models.rule_delivery import RuleDelivery # noqa: E402, F401
from grimoire.models.rule_adherence import RuleAdherence # noqa: E402, F401
from grimoire.models.system_usage import SystemUsageEvent # noqa: E402, F401
from grimoire.models.retrieval_judgment import RetrievalJudgment # noqa: E402, F401
from grimoire.models.moment_mapping import MomentMapping # noqa: E402, F401
+86
View File
@@ -0,0 +1,86 @@
from datetime import datetime
from sqlalchemy import BigInteger, DateTime, Index, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column
from grimoire.models import Base
from grimoire.models.base import iso
# What happened to a rule that was in force at a moment of a session.
FOLLOWED = "followed"
DEPARTED = "departed"
BROKEN = "broken"
NOT_APPLICABLE = "not_applicable"
VERDICTS = (FOLLOWED, DEPARTED, BROKEN, NOT_APPLICABLE)
# Where the verdict came from.
REFUSED = "refused" # the act was refused by an `enforce` rule: kept by construction
OUTCOME = "outcome" # the session declared it with `rule_outcome`
MISFIRE = "misfire" # the session reported it with `rule_misfired`
JUDGE = "judge" # a review of the session's transcript
SOURCES = (REFUSED, OUTCOME, MISFIRE, JUDGE)
class RuleAdherence(Base):
"""Whether a rule in force at a moment of a session was followed there.
The unit is a rule delivered at one place (`at`: a moment such as
`work.deliver`, or an arm such as `pre_tool_rule`) in one session — the
same unit `rule_deliveries` records, so every verdict sits on a delivery
and the readout can put "followed" beside "delivered" and "opened". A
rule delivered ten times at one moment of one session gets one verdict:
it was followed there or it was not.
`verdict` is one of:
- `followed` — the work kept the rule;
- `departed` — the session read it and deliberately did not follow it,
with its reason in `evidence`. A departure is an answer, not a fault;
- `broken` — the work went against it with no stated reason;
- `not_applicable` — the rule arrived but did not bear on what the
session was doing there (a misfire).
`source` says how it is known, strongest first: `refused` (an `enforce`
rule refused the act, so the act did not happen), `outcome` and
`misfire` (the session said so in a tool call, watched by the plugin),
`judge` (a later review read the transcript). A `refused` verdict is a
fact and is never replaced; any other is replaced by a later one, so a
review can correct a session's own account.
FK-free on `rule_id` and `user_id` like the other telemetry tables, and
pruned with the deliveries it describes.
"""
__tablename__ = "rule_adherence"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
user_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
session_id: Mapped[str] = mapped_column(Text, nullable=False)
rule_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
at: Mapped[str] = mapped_column(Text, nullable=False)
verdict: Mapped[str] = mapped_column(Text, nullable=False)
source: Mapped[str] = mapped_column(Text, nullable=False)
evidence: Mapped[str | None] = mapped_column(Text, nullable=True)
judged_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=func.now(),
)
__table_args__ = (
UniqueConstraint("user_id", "session_id", "rule_id", "at",
name="uq_rule_adherence_session_rule_at"),
Index("ix_rule_adherence_rule_at", "rule_id", "at"),
Index("ix_rule_adherence_judged_at", "judged_at"),
)
def to_dict(self) -> dict:
return {
"id": self.id,
"user_id": self.user_id,
"session_id": self.session_id,
"rule_id": self.rule_id,
"at": self.at,
"verdict": self.verdict,
"source": self.source,
"evidence": self.evidence,
"judged_at": iso(self.judged_at),
}