scanner
agent_cover.instrumentation.raw_strings.scanner
Raw string heuristic scanner.
Not all prompts use PromptTemplate classes. Many developers use simple global
string variables or f-strings. This module scans your codebase for string
variables that match specific naming conventions.
Heuristics
By default, it looks for variables starting with prefixes like PROMPT_
or ending with suffixes like _TEMPLATE.
It handles f-strings by creating a robust regex that matches the variable
content, replacing {variables} with wildcard patterns (.*?).
Examples:
If your code has:
This scanner detectsSALES_PROMPT and creates a coverage target that matches
any runtime string starting with "Hello " and ending with ", buy this!".
Attributes
DEFAULT_PREFIXES = ['PROMPT_', 'TEMPLATE_', 'SYS_MSG_', 'SYSTEM_MESSAGE']
module-attribute
Default prefixes used to identify potential prompt variables.
DEFAULT_SUFFIXES = ['_PROMPT', '_TEMPLATE']
module-attribute
Default suffixes used to identify potential prompt variables.
Classes
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 |