Coverage for lintro / utils / ai_metadata.py: 71%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Shared AI metadata accessors for tool results. 

2 

3Provides a single implementation for extracting integer counts from 

4``ai_metadata`` dictionaries attached to :class:`ToolResult` objects. 

5""" 

6 

7from __future__ import annotations 

8 

9 

10def get_ai_count(result: object, key: str) -> int: 

11 """Get an integer AI metadata count from a result object. 

12 

13 Falls back from ``applied_count`` to ``fixed_count`` for 

14 backward compatibility with older metadata. 

15 

16 Args: 

17 result: Tool result with an optional ``ai_metadata`` dict attribute. 

18 key: Metadata key to read (e.g. ``"applied_count"``). 

19 

20 Returns: 

21 Non-negative integer count, or ``0`` when absent/invalid. 

22 """ 

23 ai_metadata = getattr(result, "ai_metadata", None) 

24 if not isinstance(ai_metadata, dict): 

25 return 0 

26 value = ai_metadata.get(key) 

27 if value is None and key == "applied_count": 

28 value = ai_metadata.get("fixed_count", 0) 

29 if value is None: 

30 return 0 

31 try: 

32 return max(0, int(value)) 

33 except (TypeError, ValueError): 

34 return 0