Files
FabledGrimoire/src/grimoire/models/rule_field_judgment.py
T
bvandeusenandClaude Opus 5.5 619612732f
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 15s
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Python tests (push) Failing after 1m41s
CI & Build / integration (push) Successful in 2m9s
CI & Build / Build & push image (push) Skipped
feat(7732): a pass weighs each rule — enforcement, salience and triggers proposed from its text and delivery history, confirmed by the operator
Adds rule_field_judgments (migration 0132), the rules_to_weigh / propose_rule_weights / rule_weight_proposals / judge_rule_weights tools, the routes and the Settings › Moments panel that confirms them. A value equal to the rule's own is recorded as agreed; a different one waits; enforce is refused for a rule no act reaches; a direct edit answers a waiting suggestion. Backup v30 carries the judgments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 13:05:38 -05:00

74 lines
3.1 KiB
Python

from datetime import datetime
from sqlalchemy import BigInteger, DateTime, ForeignKey, Text, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
from grimoire.models import Base
from grimoire.models.base import CreatedAtMixin, iso
from grimoire.models.lesson_rule_link import CONFIRMED, LINK_STATES, REJECTED, SUGGESTED
__all__ = [
"CONFIRMED", "ENFORCEMENT", "FIELDS", "REJECTED", "SALIENCE", "SUGGESTED",
"TRIGGER", "RuleFieldJudgment",
]
# The fields of a rule that decide how it arrives once it is reached: what it
# does (`enforcement`), how it ranks beside rules of the same force
# (`salience`), and the typed predicates that reach it (`trigger`).
ENFORCEMENT, SALIENCE, TRIGGER = "enforcement", "salience", "trigger"
FIELDS = (ENFORCEMENT, SALIENCE, TRIGGER)
class RuleFieldJudgment(Base, CreatedAtMixin):
"""A proposed or judged value for one of a rule's delivery fields.
The rule row holds the current value; this holds the reasoning behind it
and the values that were turned down. One row per (rule, field, value):
- ``suggested`` — a pass read the rule and its delivery history and
proposed this value. Changes nothing until a person confirms it.
- ``confirmed`` — the value was judged right. For a proposal equal to
what the rule already carries, the pass's agreement is recorded this way,
so a rule that was looked at and left alone does not read as one nobody
looked at.
- ``rejected`` — judged wrong, kept with its reason so it is never
proposed again.
`value` is the enforcement or salience name, or for a trigger
`kind<TAB>pattern<TAB>moment` — one string, so the pair is unique.
`evidence` is the delivery history the proposer read, as it stood then.
"""
__tablename__ = "rule_field_judgments"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
rule_id: Mapped[int] = mapped_column(
BigInteger, ForeignKey("rules.id", ondelete="CASCADE"), index=True,
)
field: Mapped[str] = mapped_column(Text)
value: Mapped[str] = mapped_column(Text)
state: Mapped[str] = mapped_column(Text, default=SUGGESTED, server_default=SUGGESTED)
# `pass` — an agent proposed it; `edit` — a person set the field directly.
source: Mapped[str] = mapped_column(Text, default="pass", server_default="pass")
note: Mapped[str | None] = mapped_column(Text, nullable=True)
evidence: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
judged_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
__table_args__ = (
UniqueConstraint("rule_id", "field", "value", name="uq_rule_field_judgments_value"),
)
def to_dict(self) -> dict:
return {
"rule_id": self.rule_id,
"field": self.field,
"value": self.value,
"state": self.state,
"source": self.source,
"note": self.note or "",
"evidence": self.evidence or {},
"judged_at": iso(self.judged_at),
"created_at": iso(self.created_at),
}