Index
agent_cover.instrumentation.raw_strings
Raw String Heuristics.
This module scans your codebase for simple string variables acting as prompts (e.g., f-strings).
🔗 Architectural Relationships
Since raw strings are not objects, they cannot be patched directly. This module acts as a Pattern Generator.
- Feeds: AgentRegistry (Registers the "Definition" with a generated Regex).
- Enables: CoverageCallbackHandler (The handler uses the regexes generated here to match runtime text).
- Configured by: CLI flags (
--prompt-prefixes,--prompt-suffixes).
⚠️ Limitations
Global Scope Only:
Currently, this scanner uses runtime introspection (vars(module)). Therefore, it only detects global module-level constants.
Variables defined inside functions (local scope) are invisible to the scanner and will not be tracked.
⚙️ How it works
- Static Scan: It iterates through loaded modules looking for variables like
PROMPT_SALES. - Regex Generation: It converts the string content into a robust regex (handling whitespace and f-string placeholders like
{user}). - Runtime Match: When the LLM is called, the Callback Handler checks if the input text matches these regexes.
Usage
from agent_cover.instrumentation.raw_strings import (
set_custom_prefixes,
scan_raw_string_prompts
)
# 1. Configure custom naming conventions
set_custom_prefixes(["BOT_SAY_", "USER_ASK_"])
# 2. Run the scan
scan_raw_string_prompts()
Customizing Raw String Scanning
If you don't use PromptTemplate objects but store prompts in global string variables,
you can tell AgentCover how to find them.
By default, we look for variables starting with PROMPT_ or ending with _TEMPLATE.
Via Python SDK
You can add your own prefixes/suffixes programmatically:
from agent_cover.instrumentation.raw_strings import set_custom_prefixes
set_custom_prefixes(["MY_BOT_MSG_", "AI_INSTRUCTION_"])
# Now variables like MY_BOT_MSG_WELCOME = "..." will be tracked.
Via Pytest CLI
Alternatively, if you are using the pytest plugin, you can configure these without changing code using CLI flags:
--prompt-prefixes="MY_BOT_MSG_,AI_INSTRUCTION_"--prompt-suffixes="_TEXT,_MSG"
Functions
scan_raw_string_prompts(registry=None, root_path=None, module_iterator=None, source_reader=None)
Scans for raw string prompts in Python modules and registers them.
It iterates through all loaded modules in sys.modules that reside within
root_path. For each string variable matching the configured prefixes/suffixes,
it:
1. Calculates a regex pattern for runtime matching.
2. Registers it in the AgentRegistry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
An optional AgentRegistry instance. If not provided, it defaults to the global registry. |
None
|
root_path
|
Optional[str]
|
An optional root path to start the scan from. If not provided, it defaults to the current working directory. |
None
|
module_iterator
|
Optional[Callable[[], Dict[str, Any]]]
|
An optional callable for iterating through loaded modules. Defaults to _default_module_iterator. |
None
|
source_reader
|
Optional[Callable[[str], List[str]]]
|
An optional callable for reading source file contents. Defaults to _default_source_reader. |
None
|
Source code in src/agent_cover/instrumentation/raw_strings/scanner.py
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 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 | |
set_custom_prefixes(prefixes)
Sets custom prefixes for identifying prompt variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefixes
|
list[str]
|
A list of string prefixes. |
required |
Source code in src/agent_cover/instrumentation/raw_strings/scanner.py
set_custom_suffixes(suffixes)
Sets custom suffixes for identifying prompt variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suffixes
|
list[str]
|
A list of string suffixes. |
required |