"""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": {}, } 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}"