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>
111 lines
4.6 KiB
Python
111 lines
4.6 KiB
Python
"""A hook payload fitted to a budget: what is kept, in what order, what its
|
|
ids say, and which rule deliveries are recorded."""
|
|
import contextvars
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from grimoire.services import hook_payload as hp
|
|
from grimoire.services import rule_deliveries
|
|
from grimoire.services.hook_payload import Part
|
|
|
|
|
|
def _p(text, **kw):
|
|
return Part(text=text, **kw)
|
|
|
|
|
|
def test_parts_are_kept_in_order_and_the_payload_stays_within_the_budget():
|
|
parts = [_p("a" * 600, ids={hp.RULE: (1,)}), _p("b" * 600, ids={hp.RULE: (2,)}),
|
|
_p("c" * 600, ids={hp.NOTE: (9,)})]
|
|
fitted = hp.assemble(parts, 1300)
|
|
assert len(fitted.context) <= 1300
|
|
assert fitted.ids(hp.RULE) == [1, 2] and fitted.ids(hp.NOTE) == []
|
|
assert fitted.context.index("a") < fitted.context.index("b")
|
|
|
|
|
|
def test_a_part_that_does_not_fit_is_skipped_and_a_smaller_later_one_still_fits():
|
|
parts = [_p("r" * 500), _p("S" * 2000, ids={hp.SHAPE: ("core",)}), _p("n" * 300, ids={hp.NOTE: (4,)})]
|
|
fitted = hp.assemble(parts, 1000)
|
|
assert "S" not in fitted.context
|
|
assert fitted.ids(hp.SHAPE) == [] and fitted.ids(hp.NOTE) == [4]
|
|
|
|
|
|
def test_a_blocks_header_prints_once_before_its_first_kept_part_and_is_charged():
|
|
head = "> Possibly relevant:"
|
|
parts = [_p("> - #1 one", block="menu", header=head, ids={hp.NOTE: (1,)}),
|
|
_p("> - #2 two", block="menu", header=head, ids={hp.NOTE: (2,)})]
|
|
fitted = hp.assemble(parts, 5000)
|
|
assert fitted.context == f"{head}\n> - #1 one\n> - #2 two"
|
|
# The header counts against the part it rides: one that fits alone does
|
|
# not fit once its block's header is added.
|
|
long_head, line = "h" * 600, "x" * 500
|
|
tight = hp.assemble([_p(line, block="m", header=long_head, ids={hp.NOTE: (1,)})], 1000)
|
|
assert tight.context == "" and tight.ids(hp.NOTE) == []
|
|
|
|
|
|
def test_separate_blocks_are_separated_by_a_blank_line():
|
|
fitted = hp.assemble([_p("rule", block="rules"), _p("shape")], 5000)
|
|
assert fitted.context == "rule\n\nshape"
|
|
|
|
|
|
def test_keep_only_filters_a_response_list_to_what_was_shown():
|
|
fitted = hp.assemble([_p("x" * 400, ids={hp.RULE: (3,)}), _p("y" * 9000, ids={hp.RULE: (5,)})], 2000)
|
|
assert fitted.keep_only(hp.RULE, [5, 3, 7]) == [3]
|
|
|
|
|
|
def test_the_budget_is_bounded_under_the_hook_context_cap():
|
|
assert hp.HARD_MAX < 8_000
|
|
assert hp.clamp(50_000) == hp.HARD_MAX
|
|
assert hp.clamp(10) == hp.MIN_BUDGET
|
|
assert hp.clamp("not a number") == hp.DEFAULT_BUDGET
|
|
assert hp.MIN_BUDGET <= hp.DEFAULT_BUDGET <= hp.HARD_MAX
|
|
|
|
|
|
def test_a_result_without_parts_is_one_part_carrying_all_its_ids():
|
|
result = {"context": "whole", "rule_ids": [1, 2]}
|
|
parts = hp.parts_of(result, {hp.RULE: "rule_ids"})
|
|
assert parts == [Part(text="whole", ids={hp.RULE: (1, 2)})]
|
|
assert hp.parts_of({"context": ""}, {}) == []
|
|
own = {"context": "c", "parts": [_p("p")]}
|
|
assert hp.parts_of(own, {}) == [_p("p")] and "parts" not in own
|
|
|
|
|
|
def test_rule_parts_pair_each_line_with_its_rule():
|
|
parts = hp.rule_parts(["one", "two"], [11, 34], block="rules")
|
|
assert [p.ids[hp.RULE] for p in parts] == [(11,), (34,)]
|
|
|
|
|
|
# ── the delivery record ────────────────────────────────────────────────
|
|
|
|
# Each runs in a copy of the context, so the session it binds ends with it.
|
|
|
|
def test_a_bound_request_records_only_the_rule_lines_that_were_kept():
|
|
contextvars.copy_context().run(_bound_request_records_only_kept)
|
|
|
|
|
|
def _bound_request_records_only_kept():
|
|
spawned = MagicMock()
|
|
with patch.object(rule_deliveries, "spawn", spawned), \
|
|
patch.object(rule_deliveries, "_insert", lambda rows: rows):
|
|
rule_deliveries.bind_session("s-1")
|
|
rule_deliveries.record(1, None, "pre_tool_rule", [(11, "pre_tool_rule", "full"),
|
|
(34, "pre_tool_rule", "full")])
|
|
assert not spawned.called, "a bound request's rows wait for the payload"
|
|
rule_deliveries.flush({11})
|
|
rows = spawned.call_args.args[0]
|
|
assert [r["rule_id"] for r in rows] == [11]
|
|
spawned.reset_mock()
|
|
rule_deliveries.flush()
|
|
assert not spawned.called, "the buffer is spent once flushed"
|
|
|
|
|
|
def test_a_session_named_outright_records_at_once():
|
|
contextvars.copy_context().run(_named_session_records_at_once)
|
|
|
|
|
|
def _named_session_records_at_once():
|
|
spawned = MagicMock()
|
|
with patch.object(rule_deliveries, "spawn", spawned), \
|
|
patch.object(rule_deliveries, "_insert", lambda rows: rows):
|
|
rule_deliveries.bind_session("s-2")
|
|
rule_deliveries.record(1, "other", "x", [(5, "x", "full")])
|
|
assert spawned.called
|