Coverage for src / lexigram / contracts / ai / relay / logs.py: 0%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-15 18:57 +0800

1"""Request log contracts for the relay gateway. 

2 

3A request-log entry carries dispatch metadata only — never prompts, 

4tool arguments, media bytes, or credential-bearing headers. The write 

5protocol is the framework boundary: the gateway only emits entries and 

6implementations (durable stores) never import the gateway. 

7""" 

8 

9from __future__ import annotations 

10 

11from dataclasses import dataclass 

12from datetime import datetime 

13from typing import Protocol, runtime_checkable 

14 

15 

16@dataclass(frozen=True, slots=True) 

17class RelayRequestLogEntry: 

18 """One gateway request dispatch, redaction-safe.""" 

19 

20 request_id: str 

21 user_id: str 

22 token_id: str 

23 endpoint_kind: str 

24 model: str 

25 channel_name: str 

26 status: str # completed | failed | cancelled | rate_limited | unauthorized 

27 created_at: datetime # set by the emitter at completion 

28 prompt_tokens: int = 0 

29 completion_tokens: int = 0 

30 cost: str = "0" # Decimal string, units match relay billing estimation 

31 latency_ms: int = 0 

32 error_code: str = "" 

33 

34 

35@runtime_checkable 

36class RelayRequestLogStoreProtocol(Protocol): 

37 """Best-effort durable request-log sink. 

38 

39 Implementations must not raise to callers; the gateway treats any 

40 store failure as a warning-only event. 

41 """ 

42 

43 async def append(self, entry: RelayRequestLogEntry) -> None: ... 

44 

45 

46__all__ = ["RelayRequestLogEntry", "RelayRequestLogStoreProtocol"]