patcher
agent_cover.instrumentation.agents.patcher
Instrumentation module for agent runners.
This module provides the main instrumentor class responsible for applying wrappers to agent execution methods.
Context Tracking
This module wraps methods like AgentExecutor.invoke to set a "Context Token".
This allows AgentCover to know when an LLM call is happening inside an agent
versus outside (e.g., during test setup), ensuring accurate metrics.
Attributes
Classes
AgentInstrumentor
Bases: BaseInstrumentor
Instruments agent classes with context management strategies.
This class handles the lifecycle of patching agent runners. It identifies target modules, loads them if necessary, resolves the specific classes, and wraps execution methods with the appropriate context managers.
Attributes:
| Name | Type | Description |
|---|---|---|
targets_provider |
A callable returning a list of targets to instrument. |
|
strategies |
A dictionary mapping strategy keys to WrapperStrategy instances. |
|
registry |
The agent registry instance. |
|
context_manager |
The context manager for handling agent contexts. |
|
patch_manager |
The manager responsible for applying and reverting patches. |
|
module_iterator |
A callable returning the current snapshot of sys.modules. |
|
importer_func |
A callable used to import modules by name. |
|
is_instrumented |
A boolean indicating if instrumentation has been applied. |
Methods:
| Name | Description |
|---|---|
instrument |
Performs the instrumentation process on all defined targets. |
Source code in src/agent_cover/instrumentation/agents/patcher.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
Functions
__init__(registry=None, context_manager=None, patch_manager=None, module_iterator=None, importer_func=None, targets_provider=None, strategies=None)
Initializes the AgentInstrumentor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
Optional registry for tracking agents. |
None
|
context_manager
|
Optional[AgentContextManager]
|
Optional manager for agent context. |
None
|
patch_manager
|
Optional[PatchManager]
|
Optional manager for handling patches. |
None
|
module_iterator
|
Optional[Callable[[], Dict[str, Any]]]
|
Optional callable to get current modules. |
None
|
importer_func
|
Optional[Callable[[str], Any]]
|
Optional callable to import modules. |
None
|
targets_provider
|
Optional[Callable[[], TargetList]]
|
Optional callable to provide instrumentation targets. |
None
|
strategies
|
Optional[Dict[str, WrapperStrategy]]
|
Optional dictionary of instrumentation strategies. |
None
|
Source code in src/agent_cover/instrumentation/agents/patcher.py
instrument()
Applies instrumentation to all discoverable agent targets.
This method iterates through the targets provided by the targets_provider. It attempts to load the module if it is not present, resolves the target class using the base class resolver, and applies the configured wrapper strategies to the specified methods.
Source code in src/agent_cover/instrumentation/agents/patcher.py
Functions
instrument_agents(registry=None)
Helper function to instantiate and run the AgentInstrumentor.
This function exists primarily for backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
An optional AgentRegistry instance. |
None
|
Returns:
| Type | Description |
|---|---|
AgentInstrumentor
|
The initialized and executed AgentInstrumentor instance. |