analyzer
agent_cover.instrumentation.analyzer
Module for analyzing agent outputs against configured decisions.
This module implements the Verification Layer of AgentCover. While other modules track if code ran, this module tracks what the code produced.
Flow
- Interception: Instrumentors capture generated text.
- Normalization:
OutputAnalyzerconverts inputs (Strings, Pydantic, Dicts) to a standard dict. - Evaluation: Compares data against
DecisionConfig.
Classes
OutputAnalyzer
Analyzes agent outputs to verify business logic coverage.
This class decouples data extraction from validation. It is robust to different data formats, capable of parsing JSON strings embedded in Markdown or handling raw Pydantic models.
Key Features
- Adapter System: Automatically handles Pydantic V1/V2 and Dataclasses.
- Fuzzy JSON Parsing: Can extract JSON from messy LLM outputs (e.g., wrapped in markdown code blocks).
- Decision Matching: Registers hits in the registry when output fields match expected values.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
AgentRegistry
|
Where decision hits are recorded. |
config |
AgentCoverConfig
|
Source of the decision rules. |
adapter_registry |
AdapterRegistry
|
Registry for converting objects to dicts. |
Source code in src/agent_cover/instrumentation/analyzer.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 | |
Functions
__init__(registry=None, config=None, adapter_registry=None)
Initializes the OutputAnalyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry instance. |
None
|
config
|
Optional[AgentCoverConfig]
|
The AgentCoverConfig instance. |
None
|
adapter_registry
|
Optional[AdapterRegistry]
|
The adapter registry to use for data conversion. |
None
|
Source code in src/agent_cover/instrumentation/analyzer.py
analyze(payload)
Analyzes the given payload to detect matches against configured decisions.
The method handles three types of payloads: 1. Objects: Uses adapters to convert Pydantic/Dataclasses to dicts. 2. Dictionaries: Analyzes fields directly. 3. Strings: Attempts to parse JSON. If parsing fails, performs substring matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Any
|
The output to analyze. Can be a Dict, str, Pydantic Model, etc. |
required |
Examples:
>>> analyzer.analyze({"intent": "REFUND", "confidence": 0.9})
# Registers a hit for decision with target_field="intent" and expected_values=["REFUND"]
>>> analyzer.analyze("The user wants to buy something.")
# Registers a hit if a decision expects "buy" in the raw text.
>>> analyzer.analyze("```json\n{'status': 'DONE'}\n```")
# fuzzy parses JSON and matches status=DONE.
Source code in src/agent_cover/instrumentation/analyzer.py
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 | |