feat(7732): a pass weighs each rule — enforcement, salience and triggers proposed from its text and delivery history, confirmed by the operator
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
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
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>
This commit is contained in:
@@ -26,6 +26,7 @@ from grimoire.models.rulebook import (
|
||||
rule_triggers as rule_triggers_t,
|
||||
)
|
||||
from grimoire.models.lesson_rule_link import LessonNoRule, LessonRuleLink
|
||||
from grimoire.models.rule_field_judgment import RuleFieldJudgment
|
||||
from grimoire.models.rule_moment_judgment import RuleMomentJudgment
|
||||
from grimoire.models.code_shape import CodeShape, CodeShapeEvent, CodeShapeUse
|
||||
from grimoire.models.project import Project
|
||||
@@ -116,7 +117,10 @@ logger = logging.getLogger(__name__)
|
||||
# from. A live instance rolls back only to a backup with its own id.
|
||||
# v29 rule_triggers: the typed predicates on the act that deliver a rule or
|
||||
# make its mount conditional. Nothing in the rule's text records them.
|
||||
BACKUP_VERSION = 29
|
||||
# v30 rule_field_judgments: suggested, agreed and rejected values for a rule's
|
||||
# enforcement, salience and triggers. Lose them and a weighed corpus reads
|
||||
# as unweighed, and every rejected value is proposed again.
|
||||
BACKUP_VERSION = 30
|
||||
|
||||
# Every table this backup carries, by its REAL name. Paired with _NOT_INCLUDED
|
||||
# below, these two lists must together account for the entire schema — which is
|
||||
@@ -162,6 +166,7 @@ _BACKED_UP = [
|
||||
"rule_moments",
|
||||
"rule_moment_judgments",
|
||||
"rule_triggers",
|
||||
"rule_field_judgments",
|
||||
# Family canon. `platforms` is global like
|
||||
# canonical_systems and rides every export for the same reason: the rows
|
||||
# below name platforms by slug, and a partial catalog restores partial
|
||||
@@ -269,6 +274,8 @@ _COLUMN_EXCLUSIONS: dict[str, set[str]] = {
|
||||
"lesson_rule_links": {"id"},
|
||||
# The same shape: the (rule, moment) pair is the row.
|
||||
"rule_moment_judgments": {"id"},
|
||||
# And again: the (rule, field, value) triple is the row.
|
||||
"rule_field_judgments": {"id"},
|
||||
# Keyed by the lesson itself; nothing to exclude, every column travels.
|
||||
"lesson_no_rule": set(),
|
||||
"note_usage_events": {"id"},
|
||||
@@ -376,6 +383,7 @@ _IMPORT_COLUMN_EXCLUSIONS: dict[str, set[str]] = {
|
||||
"rule_relations": {"id", "created_at"},
|
||||
"lesson_rule_links": {"id"},
|
||||
"rule_moment_judgments": {"id"},
|
||||
"rule_field_judgments": {"id"},
|
||||
"lesson_no_rule": set(),
|
||||
"note_usage_events": {"id"},
|
||||
"rule_usage_events": {"id"},
|
||||
@@ -830,6 +838,21 @@ def _rule_moment_judgment_rows(rows) -> list[dict]:
|
||||
]
|
||||
|
||||
|
||||
def _rule_field_judgment_rows(rows) -> list[dict]:
|
||||
"""Suggested, agreed and rejected values for a rule's enforcement,
|
||||
salience and triggers (v30). The rule id is a SOURCE id, remapped at
|
||||
restore."""
|
||||
return [
|
||||
{
|
||||
"rule_id": r.rule_id, "field": r.field, "value": r.value, "state": r.state,
|
||||
"source": r.source, "note": r.note, "evidence": r.evidence,
|
||||
"judged_at": r.judged_at.isoformat() if r.judged_at else None,
|
||||
"created_at": r.created_at.isoformat() if r.created_at else None,
|
||||
}
|
||||
for r in rows
|
||||
]
|
||||
|
||||
|
||||
def _rule_system_rows(rows) -> list[dict]:
|
||||
"""A rule's area tags, carried by canonical SLUG for the same reason the
|
||||
Systems are: the catalog is global and its ids are per-install."""
|
||||
@@ -1042,6 +1065,9 @@ async def export_full_backup() -> dict:
|
||||
rule_moment_judgments = (await session.execute(
|
||||
select(RuleMomentJudgment).order_by(RuleMomentJudgment.id)
|
||||
)).scalars().all()
|
||||
rule_field_judgments = (await session.execute(
|
||||
select(RuleFieldJudgment).order_by(RuleFieldJudgment.id)
|
||||
)).scalars().all()
|
||||
rule_relations = (await session.execute(select(RuleRelation).order_by(RuleRelation.id))).scalars().all()
|
||||
lesson_rule_links = (await session.execute(select(LessonRuleLink).order_by(LessonRuleLink.id))).scalars().all()
|
||||
lesson_no_rule = (await session.execute(select(LessonNoRule).order_by(LessonNoRule.lesson_id))).scalars().all()
|
||||
@@ -1133,6 +1159,7 @@ async def export_full_backup() -> dict:
|
||||
"rule_moments": _rule_moment_rows(rule_moment_rows),
|
||||
"rule_triggers": _rule_trigger_rows(rule_trigger_rows),
|
||||
"rule_moment_judgments": _rule_moment_judgment_rows(rule_moment_judgments),
|
||||
"rule_field_judgments": _rule_field_judgment_rows(rule_field_judgments),
|
||||
"rule_relations": _rule_relation_rows(rule_relations),
|
||||
"lesson_rule_links": _lesson_rule_link_rows(lesson_rule_links),
|
||||
"lesson_no_rule": _lesson_no_rule_rows(lesson_no_rule),
|
||||
@@ -1301,6 +1328,10 @@ async def export_user_backup(user_id: int) -> dict:
|
||||
rule_moment_judgments = (await session.execute(
|
||||
select(RuleMomentJudgment).where(RuleMomentJudgment.rule_id.in_(_rule_ids))
|
||||
)).scalars().all() if _rule_ids else []
|
||||
rule_field_judgments = (await session.execute(
|
||||
select(RuleFieldJudgment).where(RuleFieldJudgment.rule_id.in_(_rule_ids))
|
||||
.order_by(RuleFieldJudgment.id)
|
||||
)).scalars().all() if _rule_ids else []
|
||||
# Scoped through the RULE, not the version's user_id. That column is
|
||||
# the ACTOR, so filtering on it would carry the
|
||||
# versions this user wrote on someone ELSE's rule and drop the ones
|
||||
@@ -1426,6 +1457,7 @@ async def export_user_backup(user_id: int) -> dict:
|
||||
"rule_moments": _rule_moment_rows(rule_moment_rows),
|
||||
"rule_triggers": _rule_trigger_rows(rule_trigger_rows),
|
||||
"rule_moment_judgments": _rule_moment_judgment_rows(rule_moment_judgments),
|
||||
"rule_field_judgments": _rule_field_judgment_rows(rule_field_judgments),
|
||||
"rule_relations": _rule_relation_rows(rule_relations),
|
||||
"lesson_rule_links": _lesson_rule_link_rows(lesson_rule_links),
|
||||
"lesson_no_rule": _lesson_no_rule_rows(lesson_no_rule),
|
||||
@@ -1857,6 +1889,25 @@ def _build_rule_moment_judgment(row: dict, maps: _Maps) -> RuleMomentJudgment |
|
||||
)
|
||||
|
||||
|
||||
def _build_rule_field_judgment(row: dict, maps: _Maps) -> RuleFieldJudgment | None:
|
||||
"""Skipped when its rule did not restore, for the reason a moment
|
||||
judgment is."""
|
||||
rule = maps.rules.get(row.get("rule_id", 0))
|
||||
if rule is None or not row.get("field") or row.get("value") is None:
|
||||
return None
|
||||
return RuleFieldJudgment(
|
||||
rule_id=rule,
|
||||
field=row["field"],
|
||||
value=row["value"],
|
||||
state=row.get("state") or "suggested",
|
||||
source=row.get("source") or "pass",
|
||||
note=row.get("note") or None,
|
||||
evidence=row.get("evidence"),
|
||||
judged_at=_dt_or_none(row.get("judged_at")),
|
||||
created_at=_dt(row.get("created_at")),
|
||||
)
|
||||
|
||||
|
||||
def _build_platform(row: dict, maps: _Maps) -> Platform | None:
|
||||
"""Matched on SLUG, like a canonical system: this install seeded the
|
||||
standard platforms from its migrations, so the common case creates nothing
|
||||
@@ -2447,7 +2498,7 @@ async def _load_v2(session, data: dict, maps: "_Maps", *, keep_ids: bool) -> dic
|
||||
"note_supersessions": 0, "code_shapes": 0, "code_shape_events": 0,
|
||||
"code_shape_uses": 0, "canonical_systems": 0,
|
||||
"rule_systems": 0, "rule_moments": 0, "rule_moment_judgments": 0,
|
||||
"rule_triggers": 0,
|
||||
"rule_triggers": 0, "rule_field_judgments": 0,
|
||||
"rule_relations": 0, "rule_versions": 0,
|
||||
"retrieval_tuning_events": 0, "lesson_rule_links": 0,
|
||||
"lesson_no_rule": 0, "moment_mappings": 0,
|
||||
@@ -2666,6 +2717,15 @@ async def _load_v2(session, data: dict, maps: "_Maps", *, keep_ids: bool) -> dic
|
||||
session.add(judgment)
|
||||
stats["rule_moment_judgments"] += 1
|
||||
|
||||
# Weighed values for each rule (v30); archives before v30 carry none, and
|
||||
# every rule restores unweighed.
|
||||
for rf in data.get("rule_field_judgments", []):
|
||||
weighed = _build_rule_field_judgment(rf, maps)
|
||||
if weighed is None:
|
||||
continue
|
||||
session.add(weighed)
|
||||
stats["rule_field_judgments"] += 1
|
||||
|
||||
for rr in data.get("rule_relations", []):
|
||||
relation = _build_rule_relation(rr, maps)
|
||||
if relation is None:
|
||||
|
||||
Reference in New Issue
Block a user