Coverage for app / common / invocation.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""The svc-core invocation a request's work is part of. 

2 

3svc-core mints one id per governed turn — an agent or model chat answer, a 

4skill run, an evaluation cell — and sends it on every call that turn makes 

5(``X-Invocation-Id``). Its usage ledger, review queue, governance activity 

6and messages carry the same id, and svc-ai stamps it on the trace rows the 

7request writes (``llm_invocations.core_invocation_id``, 

8``tool_invocations.core_invocation_id``), so the decision record svc-core 

9assembles for one answer can read the model and tool calls behind it. 

10 

11Named ``core_invocation_id`` on the rows because "invocation" is already 

12svc-ai's word for a trace row: ``llm_invocation_calls.invocation_id`` is the 

13``llm_invocations`` row it belongs to. 

14""" 

15 

16from __future__ import annotations 

17 

18from contextvars import ContextVar, Token 

19from uuid import UUID 

20 

21HEADER = "X-Invocation-Id" 

22 

23_CURRENT: ContextVar[UUID | None] = ContextVar("svc_core_invocation", default=None) 

24 

25 

26def parse(raw: str | None) -> UUID | None: 

27 """The id svc-core sent, or None for anything that is not a UUID — 

28 stamped as nothing rather than stored as given.""" 

29 if not raw: 

30 return None 

31 try: 

32 return UUID(raw.strip()) 

33 except ValueError: 

34 return None 

35 

36 

37def current() -> UUID | None: 

38 """The invocation the current request is part of, if svc-core named one.""" 

39 return _CURRENT.get() 

40 

41 

42def set_current(raw: str | None) -> Token: 

43 return _CURRENT.set(parse(raw)) 

44 

45 

46def reset(token: Token) -> None: 

47 _CURRENT.reset(token)