feat(7728): a rule declares its enforcement and salience
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Failing after 21s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / integration (push) Successful in 2m2s
CI & Build / Build & push image (push) Skipped
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Failing after 21s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / integration (push) Successful in 2m2s
CI & Build / Build & push image (push) Skipped
A rule now carries what it does when it arrives, declared by its author rather than guessed from a score: enforce (the act is checked), hold (the reply or act waits for one read) or inform (a line beside the work, the default). And a salience, high, normal or low, for ranking among rules of the same enforcement at a crowded moment. A preference is always inform. Migration 0128 adds both with CHECKs. Every door carries them: the service, the MCP create and update tools, REST, backup and restore, and the rule editor, which also marks holding and enforced rules in the list. Unknown values are refused rather than mapped. Nothing reads them yet beyond the record; the ordering function and the lifecycle come next in this step, and step 5 decides who holds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -273,6 +273,38 @@ RELATION_KINDS = ("co_surfaces", "overrides", "elaborates")
|
||||
# Migration 0098's CHECK. `rule` binds; `preference` is how the operator
|
||||
# wants work done — see the model comment for why both live on one table.
|
||||
KINDS = ("rule", "preference")
|
||||
# Migration 0128's CHECKs, strongest first — the order the ordering function
|
||||
# ranks them in (services/rule_order.py).
|
||||
ENFORCEMENTS = ("enforce", "hold", "inform")
|
||||
SALIENCES = ("high", "normal", "low")
|
||||
|
||||
|
||||
def require_enforcement(enforcement: str | None, kind: str | None) -> str:
|
||||
"""The enforcement a rule of this kind may carry, or a refusal naming the choices.
|
||||
|
||||
Unlike `kind`, an unknown enforcement is refused rather than mapped: there is no
|
||||
safe direction to fall in. Mapped up, a typo turns a line into a stop;
|
||||
mapped down, it turns a stop into a line. A preference is always `inform`
|
||||
— it says how work is liked done, and a stop would claim a force it does
|
||||
not have.
|
||||
"""
|
||||
clean = (enforcement or "inform").strip().lower()
|
||||
if clean not in ENFORCEMENTS:
|
||||
raise ValueError(f"enforcement must be one of {', '.join(ENFORCEMENTS)}, got {enforcement!r}")
|
||||
if (kind or "rule") == "preference" and clean != "inform":
|
||||
raise ValueError(
|
||||
"a preference's enforcement is always `inform`: it says how work is liked "
|
||||
"done, and holding or enforcing it would make it a rule. Record it "
|
||||
"as a rule if it must stop the work."
|
||||
)
|
||||
return clean
|
||||
|
||||
|
||||
def require_salience(salience: str | None) -> str:
|
||||
clean = (salience or "normal").strip().lower()
|
||||
if clean not in SALIENCES:
|
||||
raise ValueError(f"salience must be one of {', '.join(SALIENCES)}, got {salience!r}")
|
||||
return clean
|
||||
|
||||
|
||||
# The rule columns that are nullable, and therefore the ones where EMPTY has
|
||||
@@ -339,8 +371,14 @@ def rule_brief(rule: Rule, **extra) -> dict:
|
||||
# one, and the assumption it would reach for — "this binds" — is the
|
||||
# expensive one to get wrong in the other direction. Say it outright.
|
||||
"kind": rule.kind or "rule",
|
||||
# Unconditional for the same reason as `kind`: whether a rule stops
|
||||
# the work is never something a reader should have to assume.
|
||||
"enforcement": rule.enforcement or "inform",
|
||||
"updated_at": rule.updated_at.date().isoformat() if rule.updated_at else None,
|
||||
}
|
||||
# Salience is attached only when the author raised or lowered it.
|
||||
if (rule.salience or "normal") != "normal":
|
||||
out["salience"] = rule.salience
|
||||
# Attached only when present: never a null key that reads as a
|
||||
# capability the record doesn't have.
|
||||
if rule.when_to_apply:
|
||||
@@ -506,8 +544,12 @@ async def create_rule(
|
||||
why: str = "", how_to_apply: str = "", order_index: int = 0,
|
||||
when_to_apply: str = "", arose_from_id: int = 0,
|
||||
verify_with: str = "", expires_when: str = "", kind: str = "rule",
|
||||
enforcement: str = "inform", salience: str = "normal",
|
||||
) -> Rule:
|
||||
_require_trigger(when_to_apply)
|
||||
kind = _valid_kind(kind)
|
||||
enforcement = require_enforcement(enforcement, kind)
|
||||
salience = require_salience(salience)
|
||||
async with async_session() as session:
|
||||
await _assert_topic_owned(session, topic_id, user_id)
|
||||
rule = Rule(
|
||||
@@ -515,7 +557,7 @@ async def create_rule(
|
||||
title=title,
|
||||
statement=statement,
|
||||
when_to_apply=when_to_apply or None,
|
||||
kind=_valid_kind(kind),
|
||||
kind=kind, enforcement=enforcement, salience=salience,
|
||||
why=why or None,
|
||||
how_to_apply=how_to_apply or None,
|
||||
verify_with=verify_with or None,
|
||||
@@ -535,6 +577,7 @@ async def create_project_rule(
|
||||
why: str = "", how_to_apply: str = "", order_index: int = 0,
|
||||
when_to_apply: str = "", arose_from_id: int = 0,
|
||||
verify_with: str = "", expires_when: str = "", kind: str = "rule",
|
||||
enforcement: str = "inform", salience: str = "normal",
|
||||
) -> Rule:
|
||||
"""Create a rule scoped to a single project (no rulebook ceremony).
|
||||
|
||||
@@ -544,6 +587,9 @@ async def create_project_rule(
|
||||
constraint enforces exactly-one of (topic_id, project_id).
|
||||
"""
|
||||
_require_trigger(when_to_apply)
|
||||
kind = _valid_kind(kind)
|
||||
enforcement = require_enforcement(enforcement, kind)
|
||||
salience = require_salience(salience)
|
||||
async with async_session() as session:
|
||||
await _assert_project_owned(session, project_id, user_id)
|
||||
rule = Rule(
|
||||
@@ -551,7 +597,7 @@ async def create_project_rule(
|
||||
title=title,
|
||||
statement=statement,
|
||||
when_to_apply=when_to_apply or None,
|
||||
kind=_valid_kind(kind),
|
||||
kind=kind, enforcement=enforcement, salience=salience,
|
||||
why=why or None,
|
||||
how_to_apply=how_to_apply or None,
|
||||
verify_with=verify_with or None,
|
||||
@@ -691,7 +737,7 @@ async def update_rule(
|
||||
allowed = {
|
||||
"title", "statement", "why", "how_to_apply", "order_index",
|
||||
"when_to_apply", "kind", "arose_from_id",
|
||||
"verify_with", "expires_when",
|
||||
"verify_with", "expires_when", "enforcement", "salience",
|
||||
}
|
||||
check_before = rule.verify_with
|
||||
# A create-time guard is worth nothing if an edit can undo it, and
|
||||
@@ -719,6 +765,11 @@ async def update_rule(
|
||||
continue
|
||||
if key == "kind":
|
||||
value = _valid_kind(value)
|
||||
elif key == "salience":
|
||||
value = require_salience(value)
|
||||
elif key == "enforcement":
|
||||
# Checked against the kind after the loop, which may change it.
|
||||
value = (value or "").strip().lower() or "inform"
|
||||
elif key in NULLABLE_RULE_TEXT:
|
||||
value = value or None
|
||||
elif key == "arose_from_id":
|
||||
@@ -731,6 +782,12 @@ async def update_rule(
|
||||
# rule wrongly vouched for costs the thing the sweep exists to catch.
|
||||
if rule.verify_with != check_before:
|
||||
rule.verified_at = None
|
||||
# A rule turned into a preference without naming an enforcement drops to
|
||||
# `inform`, since a preference carries no other. Named explicitly
|
||||
# alongside the new kind, a stronger enforcement is refused instead.
|
||||
if rule.kind == "preference" and "enforcement" not in fields:
|
||||
rule.enforcement = "inform"
|
||||
rule.enforcement = require_enforcement(rule.enforcement, rule.kind)
|
||||
if trigger_before and not (rule.when_to_apply or "").strip():
|
||||
raise ValueError(
|
||||
"when_to_apply cannot be cleared: it is how this rule arrives, "
|
||||
|
||||
Reference in New Issue
Block a user