context
agent_cover.context
Context management utilities for tracking Agent execution scope.
This module provides the mechanism to distinguish between: 1. Test Logic: Code running in your test function (setup, assertions). 2. Agent Logic: Code running inside the agent's execution loop.
It utilizes contextvars to ensure async-safety. This means even if multiple
agents run in parallel (e.g., pytest-xdist or asyncio.gather), the
coverage tracking remains isolated and accurate for each execution flow.
๐ Architectural Relationships
To ensure accurate coverage metrics without false positives, different instrumentation components interact with this manager in specific ways:
1. Scope Providers (The Activators)
- Agents: These components act as the entry point.
They call
set_active(True)to signal that the "Agent Logic" loop has started.
2. Scope Consumers (The Gatekeepers)
- Tools & Prompts: These objects are ambiguousโthey can be executed by the agent or manually by your test setup code (e.g., formatting a prompt to assert against a string).
- Behavior: They explicitly check this manager. If
is_active()returnsFalse, they ignore the execution to prevent polluting the coverage report with test setup data.
3. Context-Agnostic Components (The Bypassers)
- LLM Providers, Callbacks, PromptFlow: These components do not check this context.
- Reason: Their execution implies inherent relevance.
- An LLM Call is an expensive I/O operation representing the agent's output/decision.
- Framework Callbacks and PromptFlow internals are structurally bound to the framework's lifecycle. Therefore, they are tracked unconditionally, regardless of the context state.
Classes
AgentContextManager
Manages the execution context using a thread-safe/async-safe ContextVar.
This manager acts as a switch. When an agent starts (e.g. agent.invoke()),
the instrumentor calls set_active(True). Only when this switch is True
does AgentCover record executions.
This prevents false positives, such as recording an LLM call made during test setup as "Agent Coverage".
Attributes:
| Name | Type | Description |
|---|---|---|
_var |
A contextvars.ContextVar that stores the boolean state of the agent context (defaults to False). |
Methods:
| Name | Description |
|---|---|
set_active |
Sets the agent context to the specified boolean value. |
reset |
Resets the agent context to its previous state. |
is_active |
Returns whether the agent context is currently active. |
Source code in src/agent_cover/context.py
Functions
__init__()
Initializes a new AgentContextManager instance.
is_active()
Checks if the agent context is currently active.
Returns:
| Type | Description |
|---|---|
bool
|
True if the agent context is active, False otherwise. |
reset(token)
Resets the agent context to its previous state using a token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The token returned by |
required |
set_active(active)
Sets the agent context to active or inactive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
active
|
bool
|
True to activate the agent context, False to deactivate. |
required |
Returns:
| Type | Description |
|---|---|
Token
|
A contextvars.Token that can be used to reset the context to its |
Token
|
previous state. |
Source code in src/agent_cover/context.py
Functions
get_global_context_manager()
Retrieves the global AgentContextManager instance.
This function is provided for backward compatibility and allows accessing the default context manager instance.
Returns:
| Type | Description |
|---|---|
AgentContextManager
|
The global AgentContextManager singleton instance. |
Source code in src/agent_cover/context.py
is_in_agent_context()
Checks if the global agent context is currently active.
This function is provided for backward compatibility and uses the default global context manager instance.
Returns:
| Type | Description |
|---|---|
bool
|
True if the agent context is active, False otherwise. |
Source code in src/agent_cover/context.py
reset_agent_context(token)
Resets the global agent context to its previous state.
This function is provided for backward compatibility and uses the default global context manager instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The token returned by |
required |
Source code in src/agent_cover/context.py
set_agent_context(active)
Sets the global agent context to active or inactive.
This function is provided for backward compatibility and uses the default global context manager instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
active
|
bool
|
True to activate the agent context, False to deactivate. |
required |
Returns:
| Type | Description |
|---|---|
Token
|
A contextvars.Token that can be used to reset the context. |