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), }