sdk
agent_cover.sdk
Main entry point for the AgentCover SDK.
This module provides high-level functions to manually control the coverage process outside of a standard pytest environment (e.g., in a custom script or a notebook).
It is designed to handle complex execution models, including multi-process environments like Microsoft PromptFlow. It orchestrates the initialization of instrumentors, manages signals and hooks for distributed workers, and handles data persistence via JSON fragments and consolidated reports.
Exposed Functions
start_coverage: Initializes and starts tracking.generate_report: Dumps the results to HTML/XML.
SDK Usage Example
If you want to run coverage for a script and automatically generate a report on exit:
from agent_cover.sdk import start_coverage
if __name__ == "__main__":
# 1. Start coverage (registers atexit hooks for reporting)
start_coverage(
source_dir="src",
report_dir="reports/agent-cover",
auto_save=True
)
# 2. Run your code
run_my_agent_logic()
# When the script ends, reports are generated automatically.
Functions
arm_worker_handlers()
Arms signal handlers and atexit hooks for the current process.
This ensures that coverage data is flushed to disk when the process receives a termination signal (SIGTERM) or exits normally. This is critical for PromptFlow workers on Linux/WSL.
Source code in src/agent_cover/sdk.py
generate_report(output_dir='coverage_report', xml_file='coverage.xml', source_dir=None)
Generates the final coverage reports (HTML and Cobertura XML).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str
|
Directory where the reports will be saved. |
'coverage_report'
|
xml_file
|
str
|
Filename for the XML Cobertura report. |
'coverage.xml'
|
source_dir
|
str
|
If provided, triggers a re-scan of definitions. |
None
|
Source code in src/agent_cover/sdk.py
start_coverage(source_dir=None, report_dir='agent_coverage_report', auto_save=True, **kwargs)
Initializes and starts the coverage tracking session.
This function sets up the configuration, instruments supported libraries, and performs static discovery of project assets (Prompts, Tools, Flows). It is suitable for manual instrumentation in scripts or distributed worker processes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Optional[str]
|
The root directory of the source code to cover. Defaults to the current working directory. |
None
|
report_dir
|
str
|
The directory where coverage reports and fragments will be saved. Defaults to "agent_coverage_report". |
'agent_coverage_report'
|
auto_save
|
bool
|
If True, enables immediate flushing on hits and registers atexit/signal hooks to generate reports automatically when the process finishes. |
True
|
atexit_registrar
|
Optional[Callable]
|
Dependency injection for testing exit registration. |
required |
**kwargs
|
Any
|
Additional parameters like |
{}
|
Examples:
Basic Usage:
from agent_cover.sdk import start_coverage
# Start tracking and auto-save on exit
start_coverage(source_dir="./src")
# Run your agent logic...
Source code in src/agent_cover/sdk.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |