"""Full anonymous/keyed agent loops, durable limits and adversarial isolation.""" from __future__ import annotations import json from collections.abc import Iterator from concurrent.futures import ThreadPoolExecutor from pathlib import Path from typing import Any import pytest from fastapi.testclient import TestClient from tessera import QueueConfig, Store, TaskSpec from tessera.access import ServiceSettings from tessera.clock import FakeClock from tessera.service import create_app @pytest.fixture def access(tmp_path: Path) -> Iterator[tuple[TestClient, Store, FakeClock, str]]: clock = FakeClock() store = Store(str(tmp_path / "access.db"), clock=clock) admin, _ = store.issue_key( "operator", queues=["*"], scopes=["admin", "store"], account_id="account:operator" ) settings = ServiceSettings( anonymous_access_enabled=True, reaper_seconds=86400, public_url="http://testserver", claims_per_minute=1000, ) with TestClient(create_app(store, settings)) as client: yield client, store, clock, admin store.close() def auth(secret: str) -> dict[str, str]: return {"Authorization": "Bearer " + secret} def anonymous(client: TestClient, label: str = "test-agent") -> dict[str, Any]: response = client.post("/v1/session/anonymous", json={"agent_label": label}) assert response.status_code == 201 return response.json() def rpc(client: TestClient, name: str, arguments: dict[str, Any], secret: str | None = None) -> Any: headers = {"Accept": "application/json, text/event-stream", **(auth(secret) if secret else {})} return client.post( "/mcp", json={ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": name, "arguments": arguments}, }, headers=headers, ) def test_anonymous_full_loop_and_durable_provenance( access: tuple[TestClient, Store, FakeClock, str], ) -> None: client, store, _, admin = access session = anonymous(client, "test