feat(7729): one assembler per hook — rule statements first, then the reply shape, then records, under a budget
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Python tests (push) Failing after 1m47s
CI & Build / integration (push) Failing after 2m0s
CI & Build / Build & push image (push) Skipped

A rule line on first mention now carries the rule's own statement, written as
a statement about the rule ("Rule 11 … may apply here; it binds: …") rather
than an instruction to go read it; a repeat or lower-ranked line is a
reference ("Also relevant: Rule 11 …"). Every line ends with where the session
stands with the rule and get_rule(id) for the full text.

services/hook_payload.py fits every injecting door's payload (prompt,
pre-tool rules, moment, write path) to the user's budget: parts in one order,
kept while they fit, dropped whole otherwise. A dropped part's ids leave the
response lists, so the plugin ledgers mark only what was shown, and rule
deliveries are buffered per request and written only for kept lines.

Budget: Settings, default 4,500 characters (the full core reply shape is
2,343 and a full rule line up to ~750, so 3,000 would have crowded the shape
out of most turns), clamped to 1,000-7,500 — under the lower of the two
recorded hook caps (10,000 in the docs, 8,000 read from an installed build).
check_plugin asserts the ceiling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 19:04:58 -05:00
co-authored by Claude Opus 5.5
parent 97d79927b5
commit a16dee4080
20 changed files with 656 additions and 213 deletions
+28 -5
View File
@@ -3,8 +3,10 @@ that history says about where a rule is worth delivering.
Three calls, one table (`rule_deliveries`):
- `record` — fire-and-forget, from the plugin doors that know the session:
every line an arm put in front of it, full or repeat.
- `record` — from the plugin doors that know the session: every line an arm
rendered, full or repeat. Within a bound request the rows wait in a buffer
until the door's payload is assembled, and `flush` writes only the lines
that made it into the payload (`hook_payload.fit`).
- `mark_opened` — the session opened a rule: the latest unopened delivery of
it in that session gets the stamp. An open with no delivery behind it (the
agent looked a rule up unprompted) stamps nothing.
@@ -42,10 +44,27 @@ _UNSAFE = re.compile(r"[^A-Za-z0-9._-]")
# plugin routes so the arms several calls deep record against it without every
# signature between carrying it. Unbound (the MCP door, a test) records nothing.
_session: ContextVar[str] = ContextVar("rule_delivery_session", default="")
# Rows rendered during the bound request, written by `flush`.
_pending: ContextVar[list | None] = ContextVar("rule_delivery_pending", default=None)
def bind_session(session_id: str | None) -> None:
_session.set(_clean_session(session_id))
_pending.set([])
def flush(kept: set[int] | None = None) -> None:
"""Write this request's buffered rows: those for rules in `kept`, or all
of them when `kept` is None. Rows for a line the payload left out are
dropped, since the session was never shown it."""
rows = _pending.get()
if not rows:
return
_pending.set([])
if kept is not None:
rows = [r for r in rows if r["rule_id"] in kept]
if rows:
spawn(_insert(rows), site="rule_delivery_write")
def bound() -> bool:
@@ -64,9 +83,9 @@ def record(
user_id: int, session_id: str | None, source: str,
lines: list[tuple[int, str, str]],
) -> None:
"""Record delivered lines: `(rule_id, at, form)` each. Fire-and-forget,
and a no-op without a session, since an open could never be tied back.
`session_id` None means the one bound for this request."""
"""Record delivered lines: `(rule_id, at, form)` each. A no-op without a
session, since an open could never be tied back. `session_id` None means
the one bound for this request, whose rows wait for `flush`."""
sid = _clean_session(_session.get() if session_id is None else session_id)
if not sid or not lines:
return
@@ -79,6 +98,10 @@ def record(
except Exception: # noqa: BLE001 - observation never breaks the observed
logger.debug("rule delivery payload build failed", exc_info=True)
return
pending = _pending.get() if session_id is None else None
if pending is not None:
pending.extend(rows)
return
spawn(_insert(rows), site="rule_delivery_write")