fix(7716): the reply shape rides after the top two rule lines, so a compaction cannot push it out
CI & Build / Python lint (push) Successful in 1s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 2m28s
CI & Build / integration (push) Successful in 2m23s
CI & Build / Build & push image (push) Successful in 25s

A compaction sweeps the rule ledger as well as the shape ledger. On the
next prompt every rule the prompt arm reaches and every rule mounted on
reply.report comes back as a first mention with its statement (about 750
characters each), and the route placed all of them ahead of the shape. Six
fill the 4,500-character default budget, so the 2,343-character core was
left out on exactly the turn it is meant to return in full. Seen in a real
session: after 9 of 10 compactions the first prompt payload carried no
reply shape at all.

The prompt payload is now ordered as milestone 639 designed it: the first
two rule lines, the reply shape, the remaining rule lines, the reply's
lessons, the carried note, the records menu. Rules that do not fit stay off
the ledger and arrive on a later call.

The lesson-placement test now pins the lesson after the shape (a lesson
binds nothing; the shape is what the reply is written to). New test: six
full-length rule lines and the real core at the default budget; the shape
is kept and the later rules wait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 10:03:46 -05:00
co-authored by Claude Opus 5.5
parent 3ad7d93386
commit a3c4cc5921
2 changed files with 79 additions and 22 deletions
+45 -2
View File
@@ -57,7 +57,8 @@ async def test_the_turns_shape_follows_the_rules_and_its_key_comes_back():
assert body["rule_ids"] == [11, 173]
async def test_a_lesson_recorded_for_the_reply_sits_between_the_turns_rules_and_its_shape():
async def test_a_lesson_recorded_for_the_reply_follows_its_shape():
"""A lesson binds nothing; the shape is what the reply is written to."""
from grimoire.services import hook_payload as hp
body, _turn = await _retrieve({"q": "write the report"}, turn_value={
@@ -68,7 +69,49 @@ async def test_a_lesson_recorded_for_the_reply_sits_between_the_turns_rules_and_
"shape_parts": [hp.Part(text="CORE SHAPE", ids={hp.SHAPE: ("core",)})],
})
ctx = body["context"]
assert ctx.index("TURN RULE") < ctx.index("LESSON LINE") < ctx.index("CORE SHAPE") < ctx.index("NOTES MENU")
assert ctx.index("TURN RULE") < ctx.index("CORE SHAPE") < ctx.index("LESSON LINE") < ctx.index("NOTES MENU")
async def test_the_full_shape_survives_a_turn_where_every_rule_is_a_first_mention():
"""After a compaction both ledgers are swept, so the prompt arm's rules and
those mounted on reply.report all return with their statements. Six of
them fill the default budget on their own; the shape still goes out in
full, and it is later rule lines that wait for a later call."""
from grimoire.services import hook_payload as hp
def long_rules(first_id, block_text):
lines = [f"{block_text} {first_id + i}: " + "x" * 700 for i in range(3)]
return hp.rule_parts(lines, [first_id + i for i in range(3)], block="rules")
core = reply_shapes.core()
blocks, forms = reply_shapes.deliver([core], frozenset())
from grimoire.routes import plugin as routes
turn = AsyncMock(return_value={
"context": "", "rule_ids": [201, 202, 203], "shape_forms": forms,
"rule_parts": long_rules(201, "TURN RULE"),
"shape_parts": [hp.Part(text=b, ids={hp.SHAPE: (k,)}) for b, k in zip(blocks, forms, strict=False)],
})
app = Quart(__name__)
async with app.test_request_context("/api/plugin/retrieve", query_string={"q": "carry on"}):
g.user = SimpleNamespace(id=7)
with patch.object(routes.plugin_ctx_svc, "build_prompt_rule_hint",
AsyncMock(return_value={"context": "", "rule_ids": [101, 102, 103],
"parts": long_rules(101, "PROMPT RULE")})), \
patch.object(routes.plugin_ctx_svc, "build_autoinject_hint",
AsyncMock(return_value={"context": "", "note_ids": []})), \
patch.object(routes.moment_delivery_svc, "deliver_for_turn", turn), \
patch.object(routes.hook_payload_svc, "payload_budget",
AsyncMock(return_value=hp.DEFAULT_BUDGET)):
body = await (await inspect.unwrap(routes.autoinject_retrieve)()).get_json()
assert body["shape_keys"] == ["core"]
assert blocks[0] in body["context"]
assert len(body["context"]) <= hp.DEFAULT_BUDGET
# The top two rules still lead, and the rules that did not fit are left
# off the ledger so they can arrive on a later call.
assert body["context"].index("PROMPT RULE 101") < body["context"].index(blocks[0][:40])
assert 101 in body["rule_ids"] and 203 not in body["rule_ids"]
async def test_the_route_hands_the_ledger_and_the_rules_just_named_to_the_turn():