Files
FabledGrimoire/tests/test_lesson_situation_delivery.py
T
bvandeusenandClaude Opus 5.5 07c4a716bd
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / integration (push) Successful in 2m0s
CI & Build / Python tests (push) Successful in 3m48s
CI & Build / Build & push image (push) Successful in 20s
fix(7883): a lesson recorded for reply.report reaches the turn before the reply, and reply.ask is not offered to a lesson
Situated lessons arrived only at tool acts and in MCP replies, so a lesson
given a reply moment was accepted and never shown. The per-turn delivery now
carries lessons confirmed on reply.report, once per session, between the
turn's rules and its reply shape. reply.ask is known only after the reply is
written, so lesson_situations refuses it and leaves it out of the catalog the
pass and the lesson page offer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 10:48:12 -05:00

83 lines
4.2 KiB
Python

"""A situated lesson reaches the work through the moment doors: the plugin's
act door and the turn's delivery before a reply show it once per session,
and an MCP reply, which has no session, carries it every time its act
happens. A moment no door reaches is not offered to a lesson."""
from unittest.mock import AsyncMock, patch
import pytest
from grimoire.services import lesson_situations, moment_delivery
from grimoire.services import retrieval_pipeline as rp
ARRIVALS = [
lesson_situations.Arrival(41, "A green build is not a running deploy", "moment `work.deliver`"),
lesson_situations.Arrival(42, "Read the job log first", "command `git push`"),
]
@pytest.mark.asyncio
async def test_the_plugin_door_leaves_out_what_the_session_already_has():
with patch.object(lesson_situations, "for_act", AsyncMock(return_value=ARRIVALS)), \
patch("grimoire.services.record_deliveries.delivered_this_session",
AsyncMock(return_value={41})):
lines, ids = await moment_delivery.lessons_for_act(
1, [{"moment": "work.deliver"}], "Bash", {"command": "git push"},
)
assert ids == [42]
assert lines == [lesson_situations.line(ARRIVALS[1])]
assert "`get_lesson(42)`" in lines[0] and "command `git push`" in lines[0]
@pytest.mark.asyncio
async def test_an_mcp_reply_carries_the_lessons_for_its_act_every_time():
ledger = AsyncMock(return_value={41, 42})
with patch.object(moment_delivery, "deliver_for_act",
AsyncMock(return_value=([{"moment": "work.finish"}], rp.RuleResult()))), \
patch.object(moment_delivery, "shapes_for_act", lambda reached: ([], {})), \
patch.object(lesson_situations, "for_act", AsyncMock(return_value=ARRIVALS[:1])), \
patch("grimoire.services.record_deliveries.delivered_this_session", ledger):
data = await moment_delivery.attach_moment_rules(1, "update_task", {"status": "done"}, {})
assert data["moment_lessons"]["lesson_ids"] == [41]
assert data["moment_lessons"]["open_with"] == "get_lesson(id)"
ledger.assert_not_awaited()
@pytest.mark.asyncio
async def test_a_turn_carries_the_lessons_recorded_for_the_reply_before_it_is_written():
found = AsyncMock(return_value=ARRIVALS[:1])
with patch.object(moment_delivery, "deliver_moments", AsyncMock(return_value=rp.RuleResult())), \
patch.object(lesson_situations, "for_act", found), \
patch("grimoire.services.record_deliveries.delivered_this_session",
AsyncMock(return_value=set())):
out = await moment_delivery.deliver_for_turn(1, record=False)
assert found.await_args.args[1] == ["reply.report"]
assert out["lesson_ids"] == [41]
assert [p.block for p in out["lesson_parts"]] == ["lessons"]
assert lesson_situations.line(ARRIVALS[0]) in out["context"]
@pytest.mark.asyncio
async def test_a_turn_leaves_out_a_reply_lesson_the_session_already_has():
with patch.object(moment_delivery, "deliver_moments", AsyncMock(return_value=rp.RuleResult())), \
patch.object(lesson_situations, "for_act", AsyncMock(return_value=ARRIVALS[:1])), \
patch("grimoire.services.record_deliveries.delivered_this_session",
AsyncMock(return_value={41})):
out = await moment_delivery.deliver_for_turn(1, record=False)
assert out["lesson_ids"] == [] and out["lesson_parts"] == []
def test_a_lesson_is_offered_only_the_moments_a_door_brings_it_to():
offered = {m["name"] for m in lesson_situations.lesson_moments()}
assert "reply.report" in offered and "work.deliver" in offered
assert offered.isdisjoint(lesson_situations.NO_LESSON_DOOR)
# Whether a reply asks is known only after it is written: refused, with
# the moment that does arrive in time named in the reason.
with pytest.raises(ValueError, match="reply.report"):
lesson_situations._values(["reply.ask"], [])
assert lesson_situations._values(["reply.report"], []) == [("moment", "reply.report")]
def test_a_situation_reads_as_what_the_act_carries():
assert lesson_situations.describe("moment", "work.finish") == "moment `work.finish`"
assert lesson_situations.describe("trigger", "path\tmigrations/**\t") == "path `migrations/**`"