Files
FabledGrimoire/tests/test_services_shape_check.py
T
bvandeusenandClaude Opus 5.5 55f53de92d fix(8139): a shape the agent walked past at the end-of-turn check is named again in its next notes
The after phase answers `left`, the asked lines still unjudged; the Stop hook
carries them as `earlier=` into the next checks, each up to two more asks, and
the note names them apart from the last turn's writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 09:41:09 -05:00

128 lines
5.7 KiB
Python

"""The server half of the end-of-turn shape check: what the
agent is asked at its next prompt, what the hook's ledger may say, and the
outcome record."""
import json
from unittest.mock import patch
import pytest
from tests.helpers import make_mock_session
def test_the_ledger_lines_parse_and_garbage_is_dropped():
from grimoire.services.shape_check import parse_written
raw = ("a.py\tsym\thelper\n"
"a.py\tsym\thelper\n" # duplicate
"web/A.svelte\tfile\tA\n"
"x.py\tbogus\tthing\n" # unknown kind
"only-two\tsym\n" # malformed
"\tsym\tnameless-path\n")
assert parse_written(raw) == [("a.py", "sym", "helper"), ("web/A.svelte", "file", "A")]
assert len(parse_written("".join(f"p\tsym\tn{i}\n" for i in range(500)), cap=7)) == 7
def test_a_stop_notes_or_passes_and_the_stop_after_the_note_records_the_answer():
from grimoire.services.shape_check import outcome_for
one = [{"path": "a", "kind": "sym", "symbol": "b"}]
assert outcome_for("check", one) == "noted"
assert outcome_for("check", []) == "passed"
assert outcome_for("after", one) == "left_after_note"
assert outcome_for("after", []) == "judged_after_note"
def test_the_note_names_each_shape_its_evidence_and_the_one_call():
from grimoire.services.shape_check import note_text
unjudged = [
{"path": "web/A.svelte", "kind": "file", "symbol": "A", "status": "new", "evidence": {}},
{"path": "src/x.py", "kind": "sym", "symbol": "load", "status": "unclassified",
"evidence": {"looks_like": {"snippet_id": 12, "basis": "semantic", "score": 0.84},
"diverges_from": 9}},
]
reason = note_text(unjudged, project_id=3, repo="git.example.com/a/w")
assert "last turn wrote 2 definitions" in reason
assert "web/A.svelte · A (file) — new" in reason
assert "looks like #12 0.84" in reason and "#9 is canon in this directory" in reason
assert 'classify_shapes(project_id=3, repo="git.example.com/a/w"' in reason
for verdict in ("create_snippet", "`instance`", "`variant`", "`exempt`"):
assert verdict in reason, verdict
def test_a_long_turn_is_listed_with_a_tail_not_dropped():
from grimoire.services.shape_check import _LISTED, note_text
many = [{"path": "p.py", "kind": "sym", "symbol": f"f{i}", "status": "new", "evidence": {}}
for i in range(_LISTED + 3)]
reason = note_text(many, project_id=1, repo="")
assert "… and 3 more" in reason
assert "repo=" not in reason
async def test_the_outcome_is_recorded_as_a_plugin_event():
from grimoire.services.shape_check import record_shape_check
session = make_mock_session()
with patch("grimoire.services.shape_check.async_session", return_value=session):
await record_shape_check(7, "noted", written=5, unjudged=2, project_id=2)
row = session.add.call_args.args[0]
assert (row.category, row.action, row.user_id) == ("plugin", "shape_check", 7)
assert json.loads(row.details) == {"outcome": "noted", "written": 5,
"unjudged": 2, "project_id": 2}
async def test_an_unknown_outcome_is_refused_before_anything_is_written():
from grimoire.services.shape_check import record_shape_check
session = make_mock_session()
with patch("grimoire.services.shape_check.async_session", return_value=session), \
pytest.raises(ValueError):
await record_shape_check(7, "skipped", written=1, unjudged=1)
session.add.assert_not_called()
def test_the_window_counts_turns_asks_and_asks_walked_past():
from grimoire.services.shape_check import summarise
assert summarise([]) == {"checked": 0, "asked": 0, "left": 0}
got = summarise(["passed", "noted", "judged_after_note", "noted", "left_after_note"])
assert got == {"checked": 3, "asked": 2, "left": 1}
# A window that spans the end-of-turn hold this check once was still sums.
mixed = summarise(["blocked", "left_after_block", "noted", "judged_after_note"])
assert mixed == {"checked": 2, "asked": 2, "left": 1}
def test_the_note_names_walked_past_shapes_apart_from_this_turns():
from grimoire.services.shape_check import note_text
fresh = [{"path": "a.py", "kind": "sym", "symbol": "new_one", "status": "new", "evidence": {}}]
old = [{"path": "b.py", "kind": "sym", "symbol": "old_one", "status": "unclassified",
"evidence": {}}]
both = note_text(fresh, project_id=2, repo="", earlier=old)
assert "last turn wrote 1 definition " in both
assert "Also: 1 definition you wrote earlier in this session is still unjudged" in both
assert both.index("a.py · new_one") < both.index("b.py · old_one")
alone = note_text([], project_id=2, repo="", earlier=old + old)
assert "last turn" not in alone
assert alone.startswith("2 definitions you wrote earlier in this session are still unjudged")
assert "classify_shapes(project_id=2" in alone
def test_the_after_phase_hands_back_ledger_lines():
from grimoire.services.shape_check import ledger_lines
items = [{"path": "a.py", "kind": "sym", "symbol": "f"},
{"path": "w/A.svelte", "kind": "file", "symbol": "A"}]
assert ledger_lines(items) == "a.py\tsym\tf\nw/A.svelte\tfile\tA"
assert ledger_lines([]) == ""
async def test_a_repeat_is_counted_apart_in_the_outcome_record():
from grimoire.services.shape_check import record_shape_check
session = make_mock_session()
with patch("grimoire.services.shape_check.async_session", return_value=session):
await record_shape_check(7, "noted", written=3, unjudged=2, project_id=2, earlier=1)
assert json.loads(session.add.call_args.args[0].details)["earlier"] == 1