feat(moments): rules mount on moments, through every rule door (milestone 458 step 3, #4921)
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 54s
CI & Build / integration (push) Successful in 1m0s
CI & Build / Python tests (push) Successful in 1m51s
CI & Build / Build & push image (push) Successful in 29s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 54s
CI & Build / integration (push) Successful in 1m0s
CI & Build / Python tests (push) Successful in 1m51s
CI & Build / Build & push image (push) Successful in 29s
rule_moments (migration 0117) records which moments a rule arrives at, by catalog name, cascading with the rule. rule_detail, the one seam every rule door already returns through, gains moments beside system_ids: None leaves the mounts alone, a list replaces them. get_rule and both list_rules doors read them back, batched per page. All five MCP rule/preference writes and the three REST ones take moments and validate them before their create or update. An unknown name is refused with the catalog listed and leaves no half-made rule behind; a parity test pins that ordering on every door. Backup v22 carries the mounts as a join table remapped through the rule map; a real-Postgres round trip checks they land on the restored rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -433,26 +433,36 @@ async def co_surfaced_partners(user_id: int, rule_ids: list[int]) -> list[Rule]:
|
||||
return out
|
||||
|
||||
|
||||
async def rule_detail(user_id: int, rule: Rule, system_ids: list[int] | None = None) -> dict:
|
||||
"""The full record, with its areas and edges attached.
|
||||
async def rule_detail(
|
||||
user_id: int, rule: Rule, system_ids: list[int] | None = None,
|
||||
moments: list[str] | None = None,
|
||||
) -> dict:
|
||||
"""The full record, with its areas, moments and edges attached.
|
||||
|
||||
ONE seam for both doors and every write path, so create, update and get
|
||||
cannot disagree about what a rule looks like coming back — the same
|
||||
reasoning as attach_relations for notes (#2859), and the same reasoning
|
||||
rule_brief exists for one level down.
|
||||
|
||||
`system_ids=None` means "leave the tags alone"; a list (including [])
|
||||
REPLACES them.
|
||||
`system_ids=None` / `moments=None` mean "leave them alone"; a list
|
||||
(including []) REPLACES them. Doors validate `moments` with
|
||||
`moments.require_moments` BEFORE their create, so an unknown name is
|
||||
refused without leaving a rule behind.
|
||||
"""
|
||||
if system_ids is not None:
|
||||
await set_rule_systems(rule.id, user_id, system_ids)
|
||||
if moments is not None:
|
||||
await set_rule_moments(rule.id, user_id, moments)
|
||||
data = rule.to_dict()
|
||||
systems = (await list_rule_systems([rule.id])).get(rule.id, [])
|
||||
mounted = (await list_rule_moments([rule.id])).get(rule.id, [])
|
||||
relations = (await list_rule_relations([rule.id])).get(rule.id, [])
|
||||
# Attached only when present (#2483): an empty key reads as a capability
|
||||
# the record has and isn't using, which is a different claim.
|
||||
if systems:
|
||||
data["systems"] = systems
|
||||
if mounted:
|
||||
data["moments"] = mounted
|
||||
if relations:
|
||||
data["relations"] = relations
|
||||
# The concrete situations judged (or proposed) to be instances of this
|
||||
@@ -1100,6 +1110,56 @@ async def set_rule_systems(
|
||||
return sorted(wanted)
|
||||
|
||||
|
||||
async def set_rule_moments(
|
||||
rule_id: int, user_id: int, moments: list[str],
|
||||
) -> list[str] | None:
|
||||
"""Replace which MOMENTS a rule arrives at (milestone 458). None if not owned.
|
||||
|
||||
Set-semantics like set_rule_systems: the list given IS the state after.
|
||||
Every name is checked against the catalog first and the whole write is
|
||||
refused on the first unknown one — a typo stored as a mount would read
|
||||
back as attached and never fire, the silent miss moments exist to end.
|
||||
"""
|
||||
from scribe.models.rulebook import rule_moments as rule_moments_t
|
||||
from scribe.services.moments import require_moments
|
||||
|
||||
wanted = require_moments(moments) or []
|
||||
async with async_session() as session:
|
||||
rule = await _fetch_owned_rule(session, rule_id, user_id)
|
||||
if rule is None:
|
||||
return None
|
||||
await session.execute(
|
||||
sql_delete(rule_moments_t).where(rule_moments_t.c.rule_id == rule_id)
|
||||
)
|
||||
for moment in wanted:
|
||||
await session.execute(
|
||||
insert(rule_moments_t).values(rule_id=rule_id, moment=moment)
|
||||
)
|
||||
await session.commit()
|
||||
return wanted
|
||||
|
||||
|
||||
async def list_rule_moments(rule_ids: list[int]) -> dict[int, list[str]]:
|
||||
"""The moments each of a batch of rules is mounted on, in catalog order."""
|
||||
from scribe.models.rulebook import rule_moments as rule_moments_t
|
||||
from scribe.services.moments import MOMENTS
|
||||
|
||||
if not rule_ids:
|
||||
return {}
|
||||
async with async_session() as session:
|
||||
rows = (await session.execute(
|
||||
select(rule_moments_t.c.rule_id, rule_moments_t.c.moment)
|
||||
.where(rule_moments_t.c.rule_id.in_(rule_ids))
|
||||
)).all()
|
||||
order = {name: i for i, name in enumerate(MOMENTS)}
|
||||
out: dict[int, list[str]] = {}
|
||||
for rule_id, moment in rows:
|
||||
out.setdefault(rule_id, []).append(moment)
|
||||
for names in out.values():
|
||||
names.sort(key=lambda n: (order.get(n, len(order)), n))
|
||||
return out
|
||||
|
||||
|
||||
async def list_rule_systems(rule_ids: list[int]) -> dict[int, list[dict]]:
|
||||
"""The canon tags for a batch of rules, keyed by rule id.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user