patcher
agent_cover.instrumentation.prompts.patcher
This module instruments prompt classes to track their execution.
It defines strategies for wrapping the initialization and execution methods of prompt classes, allowing for the registration of prompt instances and their executions within the AgentRegistry.
Classes
PromptExecutionStrategy
Bases: WrapperStrategy
Strategy for wrapping prompt execution methods (e.g., generate).
This strategy registers the execution of a prompt method with the AgentRegistry.
Methods:
| Name | Description |
|---|---|
wrap |
Wraps the original method. |
Source code in src/agent_cover/instrumentation/prompts/patcher.py
Functions
wrap(original_method, ctx_manager)
Wraps the original method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_method
|
The original method of the prompt class. |
required | |
ctx_manager
|
AgentContextManager
|
The AgentContextManager to check the agent's context. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
A callable that will replace the original method. |
Source code in src/agent_cover/instrumentation/prompts/patcher.py
PromptInitStrategy
Bases: WrapperStrategy
Strategy for intercepting prompt creation.
It captures the template content and calculates a unique hash. This allows AgentCover to identify the same prompt template even if instantiated in different parts of the lifecycle.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
The AgentRegistry to register the prompt with. |
|
stack_walker |
A callable used to walk the stack and identify the prompt's definition location. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the PromptInitStrategy. |
wrap |
Wraps the original init method. |
_register_prompt_instance |
Registers a prompt instance with the AgentRegistry. |
Source code in src/agent_cover/instrumentation/prompts/patcher.py
Functions
__init__(registry, stack_walker=None)
Initializes the PromptInitStrategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
AgentRegistry
|
The AgentRegistry to register the prompt with. |
required |
stack_walker
|
Optional[Callable]
|
A callable used to walk the stack and identify the prompt's definition location. |
None
|
Source code in src/agent_cover/instrumentation/prompts/patcher.py
wrap(original_init, cls_name, content_attr=None)
Wraps the original init method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_init
|
The original init method of the prompt class. |
required | |
cls_name
|
str
|
The name of the class. |
required |
content_attr
|
Optional[str]
|
The attribute name containing the prompt content (configured in targets). |
None
|
Returns:
| Type | Description |
|---|---|
Callable
|
A callable that will replace the original init method. |
Source code in src/agent_cover/instrumentation/prompts/patcher.py
PromptInstrumentor
Bases: BaseInstrumentor
Instruments prompt classes to track their definition and runtime usage.
This instrumentor applies a dual-strategy approach:
1. Init Strategy: Patches __init__ to register the prompt instance in the registry immediately upon creation. This establishes the "Total Prompts" count.
2. Execution Strategy: Patches methods like format or format_messages to track when a prompt is actually used by the agent.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
AgentRegistry
|
The registry to store coverage data. |
init_strategy |
PromptInitStrategy
|
Strategy for wrapping initialization. |
exec_strategy |
PromptExecutionStrategy
|
Strategy for wrapping execution methods. |
Examples:
How it tracks a LangChain prompt:
# 1. __init__ triggers registration (Definition Coverage)
prompt = PromptTemplate.from_template("Hello {name}")
# 2. format() triggers execution (Runtime Coverage)
prompt.format(name="World")
Source code in src/agent_cover/instrumentation/prompts/patcher.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
Functions
__init__(registry=None, context_manager=None, patch_manager=None, module_iterator=None, importer_func=None, targets_provider=None, init_strategy=None, exec_strategy=None, stack_walker=None)
Initializes the PromptInstrumentor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry. |
None
|
context_manager
|
Optional[AgentContextManager]
|
The AgentContextManager. |
None
|
patch_manager
|
Optional[PatchManager]
|
The PatchManager. |
None
|
module_iterator
|
Optional[Callable[[], Dict[str, Any]]]
|
A callable to iterate through modules. |
None
|
importer_func
|
Optional[Callable[[str], Any]]
|
A function to import modules. |
None
|
targets_provider
|
Optional[Callable[[], TargetList]]
|
A callable to provide instrumentation targets. |
None
|
init_strategy
|
Optional[PromptInitStrategy]
|
The strategy for wrapping the init method. |
None
|
exec_strategy
|
Optional[PromptExecutionStrategy]
|
The strategy for wrapping execution methods. |
None
|
stack_walker
|
Optional[Callable[[Any], Iterator[Any]]]
|
A callable used to walk the stack and |
None
|
Source code in src/agent_cover/instrumentation/prompts/patcher.py
instrument()
Instruments the prompts.
This method retrieves the targets, iterates through the modules, and applies the appropriate strategies to patch the prompt classes.
Source code in src/agent_cover/instrumentation/prompts/patcher.py
Functions
instrument_prompts(registry=None)
Backward compatibility function for instrumenting prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
The AgentRegistry. |
None
|
Returns:
| Type | Description |
|---|---|
|
The PromptInstrumentor instance. |
Source code in src/agent_cover/instrumentation/prompts/patcher.py
register_existing_prompts(registry=None, root_path=None, module_iterator=None)
Registers existing prompts by scanning modules.
This function scans modules for prompts that are already defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry to register the prompts with. |
None
|
root_path
|
Optional[str]
|
The root path to search for modules. |
None
|
module_iterator
|
Optional[Callable]
|
A callable to iterate through modules. |
None
|