Files
FabledGrimoire/tests/test_service_user_id_contract.py
T
bvandeusenandClaude Opus 5.5 2a4d73176e test(8249): signature and source-text batch, the hook tests run the hook
- The caller-scoping property now covers services/family, family_adoption and
  platforms, with the seven reads that need no caller named and why. The
  hand-written platform list goes.
- The snippet location keys: one test checks every door (service, MCP tool,
  web route) takes them; the route-only copy goes.
- The write-path hook: the payload-shape and skip-list tests searched the
  shell script for strings. They now run the hook against a local listener and
  check what it sends: the code from each of the four payload field names, and
  nothing for a note, JSON or SVG while a YAML file and a .py are still asked about.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 07:10:02 -05:00

72 lines
2.9 KiB
Python

"""Every public service the routes and MCP tools call is scoped by its caller.
The access helpers (services/access.py) need the caller's id, so a public
coroutine in these modules takes `user_id`. A new one without it fails here,
before a route passes it nothing. The exceptions are named with the reason
each needs none: they work on records the caller has already been allowed to
read, on ids it already holds, or on the whole instance.
"""
import ast
import pathlib
import pytest
SERVICES = pathlib.Path(__file__).resolve().parents[1] / "src" / "grimoire" / "services"
NEEDS_NO_CALLER = {
"systems": {
"standard_systems": "the shared vocabulary, the same for everyone",
"system_names_for": "names for records the caller has already read",
},
"rulebooks": {
"list_rule_moments": "batch detail for rule ids the caller already holds",
"list_rule_systems": "batch detail for rule ids the caller already holds",
"list_rule_relations": "batch detail for rule ids the caller already holds",
},
"snippets": {
"backfill_snippet_data": "a startup backfill over every snippet",
"attach_live_body": "decorates a snippet the caller has already pulled",
"invalidate_for_push": "a forge push touches every snippet at that location",
},
"design_systems": {},
"family": {
"next_version": "numbers a canon version inside a write that has checked the caller",
},
"family_adoption": {
"family_reference_ids": "the coverage refresh's mechanical read for a project",
"family_reach_ids": "retrieval's reach for a project it has already scoped",
},
"platforms": {
"list_platforms": "the catalog is global and the same for everyone",
"resolve_slugs": "a lookup in the global catalog",
"record_detected": "the coverage refresh records what it detected",
"detect_for_project": "the coverage refresh's own call",
},
}
def _public_coroutines(module: str) -> dict[str, list[str]]:
tree = ast.parse((SERVICES / f"{module}.py").read_text())
return {
n.name: [a.arg for a in n.args.args + n.args.kwonlyargs]
for n in tree.body
if isinstance(n, ast.AsyncFunctionDef) and not n.name.startswith("_")
}
@pytest.mark.parametrize("module", sorted(NEEDS_NO_CALLER))
def test_every_public_service_takes_the_caller(module):
found = _public_coroutines(module)
missing = sorted(
name for name, args in found.items()
if "user_id" not in args and name not in NEEDS_NO_CALLER[module]
)
assert not missing, f"services/{module}.py: {missing} take no user_id"
# An exception for a function that is gone, or now takes the caller,
# would excuse whatever takes the name next.
stale = sorted(
name for name in NEEDS_NO_CALLER[module]
if name not in found or "user_id" in found[name]
)
assert not stale, f"services/{module}.py: stale exceptions {stale}"