"""The one order in which rules reach a moment. Every arm that puts rules in front of a session — the rules mounted on a moment, the rules a typed trigger matched, the rules a semantic search found — ranks what it has the same way, so a crowded moment drops the same rules whichever arm brought them: 1. **enforcement** — `enforce`, then `hold`, then `inform`. A rule that stops the work is never the one cut to make room for a line beside it. 2. **salience** — `high`, `normal`, `low`, as the rule's author declared it. 3. **specificity** — how narrowly the rule was reached. A trigger that named this command or this path says more than a mount on the whole moment, and a mount says more than a resemblance. 4. **news** — at the same rank, a rule this session has not been shown yet comes before one it has, so a cap cuts a reminder rather than a first mention. 5. **history** — how often this rule was opened when delivered at this moment before. A rule sessions keep opening here outranks one they keep passing over. 6. **similarity** — the search score, when there is one. Last, because it is the one signal that says what the work resembles rather than what it is. Each key is a declared or recorded fact, read in that order, so the outcome can be explained line by line: "it outranked that one because it holds". """ from __future__ import annotations from collections.abc import Iterable from dataclasses import dataclass from typing import Any ENFORCEMENT_RANK = {"enforce": 0, "hold": 1, "inform": 2} SALIENCE_RANK = {"high": 0, "normal": 1, "low": 2} def force(rule: Any) -> str: """What this rule does when it arrives: `enforce`, `hold` or `inform`. A preference is always `inform`, whatever the field says: it records how work is liked, and claims no force that could stop it. """ if getattr(rule, "kind", None) == "preference": return "inform" value = getattr(rule, "enforcement", None) return value if value in ENFORCEMENT_RANK else "inform" def holds(rule: Any) -> bool: """Whether this rule may make a reply or an act wait for one read. Only a rule declared `hold` or `enforce` may. Every other rule is a line beside the work: holding the work on a rule nobody declared as one that holds made each reply after a write cost a rewrite, for rules that asked only to be read. """ return force(rule) in ("enforce", "hold") # How narrowly a rule was reached, larger is narrower. BY_RESEMBLANCE = 0 BY_MOUNT = 1 BY_TRIGGER = 2 # A rule with no delivery history at a moment ranks as if half its deliveries # there were opened: unknown is neither proof of use nor of neglect. UNKNOWN_HISTORY = 0.5 @dataclass(frozen=True) class Candidate: """One rule an arm could deliver, with what it knows about how it got here.""" rule: Any specificity: int = BY_RESEMBLANCE opened_rate: float | None = None """Share of this rule's earlier deliveries at this moment that were opened.""" similarity: float | None = None seen: bool = False """Already shown to this session.""" extra: Any = None """Whatever the arm needs back with the rule — the moment it was mounted on, its score pair — carried through the sort untouched.""" def order_key(c: Candidate) -> tuple: rule = c.rule return ( ENFORCEMENT_RANK.get(getattr(rule, "enforcement", None) or "inform", 2), SALIENCE_RANK.get(getattr(rule, "salience", None) or "normal", 1), -c.specificity, c.seen, -(UNKNOWN_HISTORY if c.opened_rate is None else c.opened_rate), -(c.similarity or 0.0), ) def ordered(candidates: Iterable[Candidate]) -> list[Candidate]: """The candidates in delivery order. Stable: ties keep the arm's own order.""" return sorted(candidates, key=order_key) def opened_rate(delivered: int, opened: int) -> float: """A smoothed open rate, so one delivery opened once does not read as certainty.""" return (opened + 1) / (delivered + 2)