State guard
state_guard ¶
StateGuard utility for preserving ADK state injection tokens.
This module provides the StateGuard class which validates and repairs mutated component_text to ensure required state injection tokens are preserved and unauthorized tokens are escaped.
Terminology
- component_text: The text content being evolved (e.g., agent instruction)
- token: ADK state injection placeholder (e.g., {user_id}, {context})
Note
This utility ensures ADK state injection tokens (e.g., {user_id}) remain functional after LLM reflection modifies component_text. Tokens must be present in both the original component_text and required_tokens list to be repaired.
StateGuard ¶
Validates and repairs mutated component_text to preserve ADK state tokens.
StateGuard ensures that required state injection tokens are preserved during component_text evolution, and escapes unauthorized new tokens introduced by reflection. Supports simple tokens ({name}), prefixed tokens ({app:settings}), optional tokens ({name?}), and combined formats ({app:config?}).
| ATTRIBUTE | DESCRIPTION |
|---|---|
required_tokens | List of tokens that must always be present, including braces (e.g., ["{user_id}", "{app:settings}", "{name?}"]). TYPE: |
repair_missing | Whether to re-append missing tokens. Defaults to True. TYPE: |
escape_unauthorized | Whether to escape new unauthorized tokens. Defaults to True. TYPE: |
_token_pattern | Compiled regex for token detection (private). TYPE: |
Examples:
Basic usage with token repair:
guard = StateGuard(required_tokens=["{user_id}", "{context}"])
original = "Hello {user_id}, context: {context}"
mutated = "Hello {user_id}, welcome!"
result = guard.validate(original, mutated)
# result == "Hello {user_id}, welcome!\n\n{context}"
Escaping unauthorized tokens:
guard = StateGuard(required_tokens=["{user_id}"])
original = "Process for {user_id}"
mutated = "Process for {user_id} with {malicious}"
result = guard.validate(original, mutated)
# result == "Process for {user_id} with {{malicious}}"
Note
All validation logic is stateless and operates on string inputs only. No external dependencies or I/O operations are performed.
Supported token formats: - Simple tokens: {name}, {user_id}, {context} - Prefixed tokens: {app:settings}, {user:api_key}, {temp:session} - Optional tokens: {name?}, {user_id?} - Combined: {app:config?}, {user:pref?}
Source code in src/gepa_adk/utils/state_guard.py
21 22 23 24 25 26 27 28 29 30 31 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 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
__init__ ¶
__init__(
required_tokens: list[str] | None = None,
repair_missing: bool = True,
escape_unauthorized: bool = True,
) -> None
Initialize StateGuard with configuration.
| PARAMETER | DESCRIPTION |
|---|---|
required_tokens | List of tokens that must be preserved, including braces (e.g., ["{user_id}", "{context}"]). Defaults to empty list. TYPE: |
repair_missing | If True, re-append missing required tokens. Defaults to True. TYPE: |
escape_unauthorized | If True, escape new unauthorized tokens. Defaults to True. TYPE: |
Note
Configuration determines which tokens are protected and which behaviors are enabled. Both repair and escape are enabled by default for maximum safety.
Source code in src/gepa_adk/utils/state_guard.py
get_validation_summary ¶
Summarize missing token repairs and unauthorized token escapes.
| PARAMETER | DESCRIPTION |
|---|---|
original | The component_text before mutation (reference for tokens). TYPE: |
mutated | The component_text after mutation (pre-validation). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
tuple[list[str], list[str]] | Tuple of (repaired_tokens, escaped_tokens) as token strings with braces. |
Examples:
Summarize repairs and escapes before validation:
guard = StateGuard(required_tokens=["{user_id}"])
repaired, escaped = guard.get_validation_summary(
"Hello {user_id}",
"Hello {user_id} {malicious}",
)
# repaired == []
# escaped == ["{malicious}"]
Note
Outputs what validate() would repair or escape given the current configuration and inputs, using the same token detection logic as validate(), without modifying the component_text.
Source code in src/gepa_adk/utils/state_guard.py
validate ¶
Validate and repair mutated component_text.
Compares the original and mutated component_text to: 1. Re-append missing required tokens (if repair_missing=True) 2. Escape unauthorized new tokens (if escape_unauthorized=True)
| PARAMETER | DESCRIPTION |
|---|---|
original | The component_text before mutation (reference for tokens). Used to determine which tokens were present initially. TYPE: |
mutated | The component_text after mutation (to be validated). This is the component_text that may have missing or unauthorized tokens. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The mutated component_text with repairs and escapes applied. |
str | Missing required tokens are appended at the end with |
str | Unauthorized new tokens are escaped by doubling braces: |
Examples:
Repair missing token:
guard = StateGuard(required_tokens=["{user_id}"])
result = guard.validate("Hello {user_id}", "Hello")
# result == "Hello\n\n{user_id}"
Escape unauthorized token:
guard = StateGuard(required_tokens=["{user_id}"])
result = guard.validate(
"Process {user_id}", "Process {user_id} {malicious}"
)
# result == "Process {user_id} {{malicious}}"
Note
Only tokens present in both the original component_text and the required_tokens list are eligible for repair. New tokens not in required_tokens are escaped by default.
Source code in src/gepa_adk/utils/state_guard.py
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |