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
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:
@@ -185,139 +185,64 @@ def checkpoint_reason(checkpoint: dict) -> str:
|
||||
)
|
||||
|
||||
|
||||
# How much of a rule's statement a first mention carries. A statement past it
|
||||
# is cut at a sentence boundary and the line says where the rest is.
|
||||
STATEMENT_CHARS = 600
|
||||
# The register clause, the one place a line says how much force it carries.
|
||||
RULE_FORCE = "it binds"
|
||||
PREFERENCE_FORCE = "how this has been done before"
|
||||
|
||||
|
||||
def _statement_excerpt(rule) -> str:
|
||||
"""The rule's own words, up to STATEMENT_CHARS, cut where a sentence ends."""
|
||||
text = getattr(rule, "statement", "")
|
||||
text = " ".join(text.split()) if isinstance(text, str) else ""
|
||||
if len(text) <= STATEMENT_CHARS:
|
||||
return text
|
||||
cut = text[:STATEMENT_CHARS]
|
||||
stop = max(cut.rfind(". "), cut.rfind("; "), cut.rfind(": "))
|
||||
return (cut[: stop + 1] if stop > STATEMENT_CHARS // 2 else cut.rstrip()) + " …"
|
||||
|
||||
|
||||
def _rule_hint_line(
|
||||
rule, *, where: str, seen: bool, held: bool = False, compact: bool = False,
|
||||
) -> str:
|
||||
"""One rule hint line — both arms, both tails, both kinds.
|
||||
"""One rule line, for every arm and both kinds.
|
||||
|
||||
THREE INDEPENDENT AXES. `compact` joins `kind` and `seen`, and like them
|
||||
it reads none of the others: it says how much ROOM this line
|
||||
gets, which is a fact about its rank among today's hits rather than about
|
||||
the rule. A compact line is still a full claim that the rule may apply —
|
||||
it simply cites the rule instead of quoting its trigger. Crucially it
|
||||
still carries the `seen` tail, so the three axes stay genuinely
|
||||
independent: shortening a line must not decide what it says about whether
|
||||
the session is holding the rule.
|
||||
A first mention carries the rule itself — its title, why it is here, and
|
||||
its statement — so the reader has what the rule says without a second
|
||||
call. A repeat, or a lower-ranked line (`compact`), is a reference: the
|
||||
rule's number and title. Every line ends by saying where this session
|
||||
stands with the rule and where its full text is.
|
||||
|
||||
WHY THE LATER LINES ARE QUIETER. A full line runs ~143 tokens once the
|
||||
trigger is rendered, so five full lines cost ~568 tokens before every Bash
|
||||
call. Top-full-plus-references costs ~299 — about 2x a single full line,
|
||||
for four more rules. The token budget forbids four voices at full volume,
|
||||
not four voices.
|
||||
Written as statements about the rule, never as instructions to the
|
||||
reader: a line beside the work reports what applies, and the reader
|
||||
decides what to do with it.
|
||||
|
||||
The top hit keeps the full rendering because it is the one the ranker is
|
||||
most confident about, and a reader who acts on exactly one line should
|
||||
have acted on that one.
|
||||
|
||||
TWO INDEPENDENT AXES. `kind` decides the head, `seen` decides the tail,
|
||||
and neither reads the other. A preference and a rule differ in force; a
|
||||
repeat and a first surfacing differ in whether the session already holds
|
||||
the line. Those are unrelated facts, and keeping them unrelated in the
|
||||
code is what lets a new kind join without reopening the repeat question.
|
||||
|
||||
ONE FUNCTION BECAUSE THE TAILS MUST NOT DRIFT. The two arms phrase their
|
||||
heads differently ("may apply here" vs "may apply to this Bash call") and
|
||||
that difference is deliberate. Everything after it must not differ: an
|
||||
arm modelled on its sibling rather than sharing with it inherits its
|
||||
defects, and two copies of a two-branch string is how one branch gets
|
||||
fixed and the other does not.
|
||||
|
||||
WHY A REPEAT GETS A LINE AT ALL. Dropping a hit whose id is already on the
|
||||
session's exclusion ledger is correct only while the session still HOLDS
|
||||
what it was told, and a compaction breaks exactly that: the earlier
|
||||
injection is summarized away while the id stays on the ledger, so the rule
|
||||
would be absent from context AND unreachable for the rest of the session.
|
||||
The same holds, more slowly, for a session that simply stops holding a
|
||||
line it read an hour ago.
|
||||
|
||||
Only ONE CLAUSE of a first-surfacing line is false on a repeat — the claim that
|
||||
the rule is not in the session's loaded set. So only that clause changes.
|
||||
Title, trigger and pull pointer are identical either way, the statement is
|
||||
never injected either way, and a repeat therefore costs the same ~40 tokens
|
||||
as a first surfacing and no more.
|
||||
|
||||
DELIBERATELY NOT ASKING THE SESSION WHETHER IT HOLDS THE RULE. A model
|
||||
asked "do you still hold rule N?" will say yes, and the claim is
|
||||
unverifiable self-report about its own context. The answer is also not
|
||||
needed: the line is cheap enough to always emit and carries its own remedy
|
||||
in both branches. Removing the question removes the fragility rather than
|
||||
managing it.
|
||||
Three independent axes:
|
||||
- `kind` moves the noun and the register clause: a rule binds, a
|
||||
preference says how this has been done before.
|
||||
- the ledger moves the closing clause: first mention, named earlier and
|
||||
not opened (`seen`), or opened earlier (`held`). The server cannot know
|
||||
what the reader still holds, only what it named and what was opened.
|
||||
- `compact`, or any repeat, drops the statement and keeps the rest.
|
||||
"""
|
||||
trigger = (rule.when_to_apply or "").strip()
|
||||
preference = rule.kind == "preference"
|
||||
# KIND CHANGES THE HEAD; `seen` CHANGES THE TAIL. The two axes are
|
||||
# independent and stay that way, which is what lets the repeat logic above
|
||||
# survive a second kind without being reasoned about again: whether a
|
||||
# record is already on the ledger has nothing to do with how much force it
|
||||
# carries, so the seen-branch is shared verbatim.
|
||||
#
|
||||
# The noun is the whole of the visual difference, and that is deliberate.
|
||||
# A reader skimming an injected block gets one word to place the register \u2014
|
||||
# so it is the SECOND word that moves, and it is the word naming force.
|
||||
# Everything structural after it is identical, so the three kinds read as
|
||||
# one set rather than three formats (the lesson voice is written against
|
||||
# these two; they are one paragraph, not three).
|
||||
noun = "Preference" if preference else "Standing rule"
|
||||
# The only other place force is asserted. A rule's line tells the reader
|
||||
# not to dismiss it unread, because dismissing a rule unread is how the
|
||||
# thing it prevents happens. A preference makes no such claim: it says
|
||||
# where to find how this has been done, and following it is what keeps
|
||||
# things consistent rather than what keeps them correct.
|
||||
reason = (
|
||||
"for how this has been done before" if preference
|
||||
else "before deciding it does not apply"
|
||||
)
|
||||
# THREE STATES, BECAUSE TWO OF THEM MUST NOT BE TOLD THE SAME THING.
|
||||
#
|
||||
# `seen` means an arm NAMED this rule earlier. It does not mean the session
|
||||
# read it — the line is a teaser, and a teaser skimmed past leaves nothing
|
||||
# behind, least of all after a compaction summarises the turn it arrived
|
||||
# in. "You saw it earlier this session" would assert something about the
|
||||
# reader's context that the server has no way to know.
|
||||
#
|
||||
# `held` is the observable half: a PostToolUse hook watches for the
|
||||
# `get_rule` call itself, so this is a recorded EVENT rather than a claim.
|
||||
# That distinction is what keeps the non-goal above intact — the objection
|
||||
# was to asking a model about its own context, not to noticing what it did.
|
||||
#
|
||||
# The middle state is the honest one: named, not opened. It gets the full
|
||||
# invitation, because a session that skipped
|
||||
# the teaser is in almost the same position as one that never saw it.
|
||||
noun = "Preference" if preference else "Rule"
|
||||
force = PREFERENCE_FORCE if preference else RULE_FORCE
|
||||
if held:
|
||||
tail = (
|
||||
f"You opened it earlier this session; pull it with "
|
||||
f"get_rule({rule.id}) again if you no longer hold it."
|
||||
)
|
||||
state = "Opened earlier this session"
|
||||
elif seen:
|
||||
tail = (
|
||||
f"Mentioned earlier this session but not opened — read it with "
|
||||
f"get_rule({rule.id}) {reason}."
|
||||
)
|
||||
state = "Named earlier this session, not opened"
|
||||
else:
|
||||
tail = (
|
||||
f"Read it with get_rule({rule.id}) {reason}; it is not in this "
|
||||
"session's loaded set."
|
||||
)
|
||||
if compact:
|
||||
# THE TRIGGER GOES; THE TAIL STAYS. Only one of the two is expensive \u2014
|
||||
# a trigger runs 300-400 characters, the tail about 100 \u2014
|
||||
# so dropping the trigger is nearly the whole saving and dropping the
|
||||
# tail would be mostly sacrifice.
|
||||
#
|
||||
# It would also destroy the one thing a repeat line exists to say. The
|
||||
# tail is what tells a reader whether they were already told this and
|
||||
# may no longer be holding it, and that is the entire difference they
|
||||
# can act on; a reference with no tail reads as a first surfacing
|
||||
# whether it is one or not.
|
||||
# test_a_rule_the_session_already_holds_is_referenced_not_re_offered
|
||||
# pins this.
|
||||
return (
|
||||
f"Also \u2014 {noun.lower()} \u201c{rule.title}\u201d. {tail}"
|
||||
)
|
||||
state = "First mention this session"
|
||||
tail = f"{state}; full text: get_rule({rule.id})."
|
||||
statement = "" if (compact or seen or held) else _statement_excerpt(rule)
|
||||
if not statement:
|
||||
return f"Also relevant: {noun} {rule.id} \u201c{rule.title}\u201d ({force}). {tail}"
|
||||
return (
|
||||
f"{noun} that may apply {where} \u2014 \u201c{rule.title}\u201d"
|
||||
+ (f" ({trigger})" if trigger else "")
|
||||
+ f". {tail}"
|
||||
f"{noun} {rule.id} \u201c{rule.title}\u201d may apply {where}; {force}: "
|
||||
f"{statement} {tail}"
|
||||
)
|
||||
|
||||
|
||||
@@ -626,6 +551,13 @@ class RuleResult:
|
||||
|
||||
checkpoint: dict = field(default_factory=dict)
|
||||
|
||||
line_rule_ids: list[int] = field(default_factory=list)
|
||||
"""The rule behind each of `lines`, in order, when a caller added lines
|
||||
of its own after the arm's; empty means `shown_rule_ids` lines up."""
|
||||
|
||||
def rule_per_line(self) -> list[int]:
|
||||
return list(self.line_rule_ids or self.shown_rule_ids)
|
||||
|
||||
|
||||
# ── The stages ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
Reference in New Issue
Block a user