utils
agent_cover.utils
Utility module for low-level operations.
This module provides helpers for: 1. Time: Testable time providers. 2. Safety: Decorators that don't crash on Mock objects. 3. Stack Inspection: The core logic for finding where a prompt or tool was defined.
Classes
FrameInterface
Mock Helper for tests: simulates a stack frame.
This class provides a mock implementation of a Python stack frame, allowing tests to simulate call stacks without executing actual code.
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
str
|
The filename of the code. |
lineno |
int
|
The line number. |
lasti |
int
|
The last instruction index. |
globals_dict |
dict
|
A dictionary of global variables. |
_back |
Optional[Any]
|
The previous frame (simulating the call stack). |
Source code in src/agent_cover/utils.py
Attributes
f_back
property
Optional[Any]: The previous stack frame.
f_code
property
Returns a mock f_code object.
Required to simulate a stack frame structure expected by inspect module.
Returns:
| Name | Type | Description |
|---|---|---|
object |
A mock Code object with a co_filename attribute. |
f_globals
property
dict: The global variables dictionary of the frame.
f_lasti
property
int: The index of the last instruction executed.
f_lineno
property
int: The line number of the frame.
Functions
__init__(filename, lineno, lasti, globals_dict, back=None)
Initializes a FrameInterface object.
Used for mocking stack frames in tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename of the code. |
required |
lineno
|
int
|
The line number. |
required |
lasti
|
int
|
The last instruction index. |
required |
globals_dict
|
dict
|
A dictionary of global variables. |
required |
back
|
Optional[Any]
|
The previous frame. Defaults to None. |
None
|
Source code in src/agent_cover/utils.py
Functions
format_iso_time(timestamp=None)
Formats a timestamp into ISO 8601 format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
Optional[float]
|
An optional timestamp to format. If None, the current time is used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The timestamp in ISO 8601 format. |
Source code in src/agent_cover/utils.py
get_definition_location(registry=None, start_frame=None, root_path=None, internal_filter=None, stack_walker=None)
Determines the source file and line number of the caller.
It traverses the Python call stack backwards to find the first frame that belongs to the user's codebase (ignoring internal AgentCover frames and external libraries).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry instance. If None, retrieves the global registry. |
None
|
start_frame
|
Optional[Any]
|
An optional starting frame. |
None
|
root_path
|
Optional[str]
|
The root path of the project. If None, uses the current working directory. |
None
|
internal_filter
|
Optional[Callable[[str, str], bool]]
|
An optional filter function for internal files. Defaults to _default_internal_filter. |
None
|
stack_walker
|
Optional[Callable[[Any], Iterator[Any]]]
|
A callable used to walk the stack. Defaults to _default_frame_walker. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representing the definition location (e.g., "file.py:123") |
str
|
or "unknown:0" if the location cannot be determined. |
Source code in src/agent_cover/utils.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
get_timestamp(provider=None)
Gets a timestamp using the provided provider or the default time provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
Optional[Callable[[], float]]
|
An optional callable that provides the timestamp. If None, the default time provider is used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The timestamp as a float. |
Source code in src/agent_cover/utils.py
safe_wraps(original_func)
Robust alternative to functools.wraps.
This decorator does not fail if the original object is an incomplete Mock (common in tests). It preserves the original function's metadata (name, docstring, etc.) when wrapping a function. It handles cases where the original function might be a Mock object that doesn't fully implement the attributes expected by functools.wraps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_func
|
Callable
|
The original function to wrap. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
A decorator that, when applied to a wrapper function, sets |
Callable
|
the wrapper's attributes to match the original function's. |