Files
FabledGrimoire/tests/test_hook_payload.py
T
bvandeusenandClaude Opus 5.5 e0621713d4
CI & Build / TypeScript typecheck (push) Canceled after 0s
CI & Build / Plugin hooks (push) Canceled after 0s
CI & Build / Python lint (push) Canceled after 0s
CI & Build / Python tests (push) Canceled after 0s
CI & Build / integration (push) Canceled after 0s
CI & Build / Build & push image (push) Canceled after 0s
fix(7733): a rule's open rate counts only deliveries whose plugin reports opens
Each rule line a hook delivers now records the plugin version that asked for
it (the X-Grimoire-Plugin header every hook request sends). The open rate
used to order rules, and the delivery history the weighing pass reads,
count only deliveries that carry one.

A plugin before 2026.11.22.2312 never reported an open of a rule a hook had
just named, so every delivery made to such a session read as unopened. The
server shipped delivery recording from dev on 2026-11-22. The plugin ships
from main, and a running session keeps the plugin it started with, so those
sessions kept arriving. They are why opens read about 1 in 600. Existing
rows have no version and drop out of the rate; nothing is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 15:52:42 -05:00

111 lines
4.7 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", "2026.11.23.2100")
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"], r["plugin"]) for r in rows] == [(11, "2026.11.23.2100")]
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