Files
FabledGrimoire/tests/test_services_shape_check.py
bvandeusenandClaude Opus 5.5 0f5ef0be63
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
rename(547): the server is grimoire — package, image, DB defaults, backups
Step 2 of the Scribe -> Grimoire rename.

- src/scribe -> src/grimoire, and every import, patch target, module path,
  hypercorn entry point and `python -m grimoire.restore` with it; pyproject,
  uv.lock and the frontend package are named grimoire.
- Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service
  runs as grimoire / grimoire_test.
- Compose files default the DB user, database and (prod) volume to grimoire,
  each overridable from .env, so an install created as Scribe keeps its data
  by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME.
- Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar.
  Archives named scribe-* with the scribe-backup-tar manifest still restore
  and are still counted at the destination (folder and S3), with tests.
- Exports, the MCP auth realm, the ASGI scope key and the persistent logger
  carry the new name.

Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1",
because every stored secret was derived under it.

Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is
step 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:50:45 -04:00

90 lines
3.8 KiB
Python

"""The server half of the end-of-turn shape check: what the
agent is asked, 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_first_stop_blocks_or_passes_and_the_next_never_blocks():
from grimoire.services.shape_check import outcome_for
one = [{"path": "a", "kind": "sym", "symbol": "b"}]
assert outcome_for("check", one) == "blocked"
assert outcome_for("check", []) == "passed"
assert outcome_for("after", one) == "left_after_block"
assert outcome_for("after", []) == "judged_after_block"
def test_the_reason_names_each_shape_its_evidence_and_the_one_call():
from grimoire.services.shape_check import block_reason
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 = block_reason(unjudged, project_id=3, repo="git.example.com/a/w")
assert "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, block_reason
many = [{"path": "p.py", "kind": "sym", "symbol": f"f{i}", "status": "new", "evidence": {}}
for i in range(_LISTED + 3)]
reason = block_reason(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, "blocked", 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": "blocked", "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", "blocked", "judged_after_block", "blocked", "left_after_block"])
assert got == {"checked": 3, "asked": 2, "left": 1}