@@ -0,0 +1,1035 @@ |
| 1 | +"""Runtime safeguards to improve agent behavior. |
| 2 | + |
| 3 | +These safeguards help keep the agent on track when models don't follow |
| 4 | +instructions perfectly. They work at runtime to filter, detect, and correct |
| 5 | +problematic patterns. |
| 6 | +""" |
| 7 | + |
| 8 | +import re |
| 9 | +from dataclasses import dataclass, field |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class FilterResult: |
| 15 | + """Result of filtering content.""" |
| 16 | + content: str # Filtered content |
| 17 | + was_filtered: bool # Whether any filtering occurred |
| 18 | + removed_blocks: list[str] = field(default_factory=list) # What was removed |
| 19 | + |
| 20 | + |
| 21 | +class CodeBlockFilter: |
| 22 | + """Filters markdown code blocks and bracket tool calls from streamed content. |
| 23 | + |
| 24 | + Handles both complete blocks (```...```) and partial blocks that span |
| 25 | + multiple stream chunks. Also filters [calls X tool with ...] patterns. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + self._buffer = "" |
| 30 | + self._in_code_block = False |
| 31 | + self._block_lang = "" |
| 32 | + self._current_block = "" |
| 33 | + self._in_bracket = False |
| 34 | + self._bracket_content = "" |
| 35 | + self._in_json_tool = False |
| 36 | + self._json_brace_count = 0 |
| 37 | + |
| 38 | + def reset(self): |
| 39 | + """Reset filter state.""" |
| 40 | + self._buffer = "" |
| 41 | + self._in_code_block = False |
| 42 | + self._block_lang = "" |
| 43 | + self._current_block = "" |
| 44 | + self._in_bracket = False |
| 45 | + self._bracket_content = "" |
| 46 | + self._in_json_tool = False |
| 47 | + self._json_brace_count = 0 |
| 48 | + |
| 49 | + def _is_bracket_tool_start(self, text: str) -> bool: |
| 50 | + """Check if text looks like start of a bracket tool call.""" |
| 51 | + # Patterns like: [calls, [call, [USE |
| 52 | + return bool(re.match(r'\[(?:calls?|USE)\s', text, re.IGNORECASE)) |
| 53 | + |
| 54 | + def filter_chunk(self, chunk: str) -> FilterResult: |
| 55 | + """Filter a streaming chunk, removing code blocks and bracket tool calls. |
| 56 | + |
| 57 | + Returns filtered content. Handles partial blocks across chunks. |
| 58 | + """ |
| 59 | + if not chunk: |
| 60 | + return FilterResult(content="", was_filtered=False) |
| 61 | + |
| 62 | + result_parts = [] |
| 63 | + removed = [] |
| 64 | + was_filtered = False |
| 65 | + |
| 66 | + # Process character by character to handle streaming |
| 67 | + self._buffer += chunk |
| 68 | + |
| 69 | + while self._buffer: |
| 70 | + # Handle bracket tool calls: [calls X tool with ...] |
| 71 | + if self._in_bracket: |
| 72 | + # Look for closing ] |
| 73 | + end_idx = self._buffer.find(']') |
| 74 | + if end_idx >= 0: |
| 75 | + self._bracket_content += self._buffer[:end_idx] |
| 76 | + removed.append(f"[{self._bracket_content}]") |
| 77 | + self._buffer = self._buffer[end_idx + 1:] |
| 78 | + self._in_bracket = False |
| 79 | + self._bracket_content = "" |
| 80 | + was_filtered = True |
| 81 | + else: |
| 82 | + # Still in bracket, consume all |
| 83 | + self._bracket_content += self._buffer |
| 84 | + self._buffer = "" |
| 85 | + was_filtered = True |
| 86 | + continue |
| 87 | + |
| 88 | + # Check for bracket start: [calls, [USE, or [output (fake outputs) |
| 89 | + bracket_match = re.search(r'\[(?=(?:calls?|USE|output)\s*[:\s])', self._buffer, re.IGNORECASE) |
| 90 | + if bracket_match: |
| 91 | + # Output everything before the bracket |
| 92 | + result_parts.append(self._buffer[:bracket_match.start()]) |
| 93 | + self._buffer = self._buffer[bracket_match.start() + 1:] # Skip the [ |
| 94 | + self._in_bracket = True |
| 95 | + was_filtered = True |
| 96 | + continue |
| 97 | + |
| 98 | + # Handle JSON tool calls: {"name": "write", "arguments": {...}} |
| 99 | + if self._in_json_tool: |
| 100 | + # Track braces to find the end |
| 101 | + for i, char in enumerate(self._buffer): |
| 102 | + if char == '{': |
| 103 | + self._json_brace_count += 1 |
| 104 | + elif char == '}': |
| 105 | + self._json_brace_count -= 1 |
| 106 | + if self._json_brace_count == 0: |
| 107 | + # Found end of JSON |
| 108 | + removed.append(self._buffer[:i + 1]) |
| 109 | + self._buffer = self._buffer[i + 1:] |
| 110 | + self._in_json_tool = False |
| 111 | + was_filtered = True |
| 112 | + break |
| 113 | + else: |
| 114 | + # Still in JSON, consume all |
| 115 | + self._buffer = "" |
| 116 | + was_filtered = True |
| 117 | + continue |
| 118 | + |
| 119 | + # Check for JSON tool call start: {"name": "write" etc |
| 120 | + json_tool_match = re.search( |
| 121 | + r'\{\s*"name"\s*:\s*"(?:write|read|edit|bash|glob|grep)"', |
| 122 | + self._buffer |
| 123 | + ) |
| 124 | + if json_tool_match: |
| 125 | + # Output everything before the JSON |
| 126 | + result_parts.append(self._buffer[:json_tool_match.start()]) |
| 127 | + self._buffer = self._buffer[json_tool_match.start():] |
| 128 | + self._in_json_tool = True |
| 129 | + self._json_brace_count = 0 # Will count starting from { |
| 130 | + was_filtered = True |
| 131 | + continue |
| 132 | + |
| 133 | + # Check for preamble patterns and filter the line |
| 134 | + preamble_match = re.search( |
| 135 | + r'(Here is a JSON response|Here are the function calls|' |
| 136 | + r'Here is the response with|I will respond with|' |
| 137 | + r'The following JSON|Below is the)', |
| 138 | + self._buffer, re.IGNORECASE |
| 139 | + ) |
| 140 | + if preamble_match: |
| 141 | + # Find end of line and remove whole line |
| 142 | + line_start = self._buffer.rfind('\n', 0, preamble_match.start()) + 1 |
| 143 | + line_end = self._buffer.find('\n', preamble_match.end()) |
| 144 | + if line_end == -1: |
| 145 | + # Line continues to end of buffer - wait for more |
| 146 | + if line_start > 0: |
| 147 | + result_parts.append(self._buffer[:line_start]) |
| 148 | + self._buffer = self._buffer[line_start:] |
| 149 | + break |
| 150 | + else: |
| 151 | + # Remove the whole line |
| 152 | + result_parts.append(self._buffer[:line_start]) |
| 153 | + removed.append(self._buffer[line_start:line_end]) |
| 154 | + self._buffer = self._buffer[line_end:] |
| 155 | + was_filtered = True |
| 156 | + continue |
| 157 | + if self._in_code_block: |
| 158 | + # Look for closing ``` |
| 159 | + end_match = re.search(r'```', self._buffer) |
| 160 | + if end_match: |
| 161 | + # Found end of code block |
| 162 | + block_content = self._buffer[:end_match.start()] |
| 163 | + self._current_block += block_content |
| 164 | + removed.append(f"```{self._block_lang}\n{self._current_block}```") |
| 165 | + self._buffer = self._buffer[end_match.end():] |
| 166 | + self._in_code_block = False |
| 167 | + self._current_block = "" |
| 168 | + self._block_lang = "" |
| 169 | + was_filtered = True |
| 170 | + else: |
| 171 | + # Still in code block, consume all |
| 172 | + self._current_block += self._buffer |
| 173 | + self._buffer = "" |
| 174 | + was_filtered = True |
| 175 | + else: |
| 176 | + # Look for opening ``` |
| 177 | + start_match = re.search(r'```(\w*)\n?', self._buffer) |
| 178 | + if start_match: |
| 179 | + # Found start of code block |
| 180 | + # Output everything before the block |
| 181 | + result_parts.append(self._buffer[:start_match.start()]) |
| 182 | + self._block_lang = start_match.group(1) |
| 183 | + self._buffer = self._buffer[start_match.end():] |
| 184 | + self._in_code_block = True |
| 185 | + was_filtered = True |
| 186 | + else: |
| 187 | + # Check if buffer ends with partial ``` marker |
| 188 | + if self._buffer.endswith('`') or self._buffer.endswith('``'): |
| 189 | + # Hold back potential partial marker |
| 190 | + split_point = len(self._buffer) - self._buffer[::-1].index('`') - 1 |
| 191 | + if split_point > 0: |
| 192 | + # Find where backticks start |
| 193 | + for i in range(len(self._buffer) - 1, -1, -1): |
| 194 | + if self._buffer[i] != '`': |
| 195 | + result_parts.append(self._buffer[:i+1]) |
| 196 | + self._buffer = self._buffer[i+1:] |
| 197 | + break |
| 198 | + break |
| 199 | + else: |
| 200 | + # No code block markers, output all |
| 201 | + result_parts.append(self._buffer) |
| 202 | + self._buffer = "" |
| 203 | + |
| 204 | + return FilterResult( |
| 205 | + content="".join(result_parts), |
| 206 | + was_filtered=was_filtered, |
| 207 | + removed_blocks=removed, |
| 208 | + ) |
| 209 | + |
| 210 | + def filter_complete(self, content: str) -> FilterResult: |
| 211 | + """Filter complete content (non-streaming), removing code blocks, bracket tool calls, and preambles.""" |
| 212 | + removed = [] |
| 213 | + |
| 214 | + # Pattern to match code blocks |
| 215 | + code_pattern = r'```\w*\n?[\s\S]*?```' |
| 216 | + removed.extend(re.findall(code_pattern, content)) |
| 217 | + filtered = re.sub(code_pattern, '', content) |
| 218 | + |
| 219 | + # Pattern to match bracket-format tool calls: [calls X tool with ...] and fake outputs |
| 220 | + bracket_patterns = [ |
| 221 | + r'\[calls?\s+\w+\s+tool\s+with[:\s][^\]]+\]', |
| 222 | + r'\[USE\s+\w+\s+tool[:\s][^\]]+\]', |
| 223 | + r'\[output[:\s][^\]]+\]', # Fake outputs from model |
| 224 | + ] |
| 225 | + for pattern in bracket_patterns: |
| 226 | + matches = re.findall(pattern, filtered, re.IGNORECASE) |
| 227 | + removed.extend(matches) |
| 228 | + filtered = re.sub(pattern, '', filtered, flags=re.IGNORECASE) |
| 229 | + |
| 230 | + # Pattern to match JSON tool calls: {"name": "write", "arguments": {...}} |
| 231 | + # Use a function to handle nested braces properly |
| 232 | + def remove_json_tool_calls(text: str) -> tuple[str, list[str]]: |
| 233 | + json_removed = [] |
| 234 | + tool_pattern = r'\{\s*"name"\s*:\s*"(?:write|read|edit|bash|glob|grep)"' |
| 235 | + result = text |
| 236 | + while True: |
| 237 | + match = re.search(tool_pattern, result) |
| 238 | + if not match: |
| 239 | + break |
| 240 | + # Find matching closing brace |
| 241 | + start = match.start() |
| 242 | + brace_count = 0 |
| 243 | + end = start |
| 244 | + for i, char in enumerate(result[start:], start): |
| 245 | + if char == '{': |
| 246 | + brace_count += 1 |
| 247 | + elif char == '}': |
| 248 | + brace_count -= 1 |
| 249 | + if brace_count == 0: |
| 250 | + end = i + 1 |
| 251 | + break |
| 252 | + if end > start: |
| 253 | + json_removed.append(result[start:end]) |
| 254 | + result = result[:start] + result[end:] |
| 255 | + else: |
| 256 | + break # Couldn't find matching brace |
| 257 | + return result, json_removed |
| 258 | + |
| 259 | + filtered, json_matches = remove_json_tool_calls(filtered) |
| 260 | + removed.extend(json_matches) |
| 261 | + |
| 262 | + # Pattern to match preamble lines (remove entire line) |
| 263 | + preamble_patterns = [ |
| 264 | + r'^.*Here is a JSON response.*$', |
| 265 | + r'^.*Here are the function calls.*$', |
| 266 | + r'^.*Here is the response with.*$', |
| 267 | + r'^.*I will respond with.*$', |
| 268 | + r'^.*The following (JSON|function calls|tool calls).*$', |
| 269 | + r'^.*Below (is|are) the (JSON|function|tool).*$', |
| 270 | + ] |
| 271 | + for pattern in preamble_patterns: |
| 272 | + matches = re.findall(pattern, filtered, re.IGNORECASE | re.MULTILINE) |
| 273 | + removed.extend(matches) |
| 274 | + filtered = re.sub(pattern, '', filtered, flags=re.IGNORECASE | re.MULTILINE) |
| 275 | + |
| 276 | + # Filter internal recovery/system prompts (multiline blocks) |
| 277 | + internal_prompt_patterns = [ |
| 278 | + # Recovery prompts |
| 279 | + r'## TOOL FAILURE - INVESTIGATE AND ADAPT[\s\S]*?What will you do\?', |
| 280 | + r'## REQUIRED: Choose ONE[\s\S]*?(?=\n\n|\Z)', |
| 281 | + r'## CRITICAL RULES:[\s\S]*?(?=\n\n|\Z)', |
| 282 | + r'## Current attempt:.*$', |
| 283 | + r'\*\*Your next action should gather information[\s\S]*?What will you do\?', |
| 284 | + # Observation prefixes |
| 285 | + r'^Observation \[[\w]+\]:.*$', |
| 286 | + ] |
| 287 | + for pattern in internal_prompt_patterns: |
| 288 | + matches = re.findall(pattern, filtered, re.MULTILINE) |
| 289 | + removed.extend(matches) |
| 290 | + filtered = re.sub(pattern, '', filtered, flags=re.MULTILINE) |
| 291 | + |
| 292 | + # Clean up multiple blank lines left behind |
| 293 | + filtered = re.sub(r'\n{3,}', '\n\n', filtered) |
| 294 | + |
| 295 | + return FilterResult( |
| 296 | + content=filtered.strip(), |
| 297 | + was_filtered=bool(removed), |
| 298 | + removed_blocks=removed, |
| 299 | + ) |
| 300 | + |
| 301 | + |
| 302 | +@dataclass |
| 303 | +class PatternMatch: |
| 304 | + """A detected problematic pattern.""" |
| 305 | + pattern_type: str # 'code_block', 'narration', 'preview', 'repetition' |
| 306 | + match_text: str |
| 307 | + severity: str # 'low', 'medium', 'high' |
| 308 | + |
| 309 | + |
| 310 | +class PatternDetector: |
| 311 | + """Detects problematic patterns in agent output. |
| 312 | + |
| 313 | + Patterns include: |
| 314 | + - Code blocks (which should be tool calls instead) |
| 315 | + - Narration ("I will call...", "Now I'll...") |
| 316 | + - Previews ("The file will look like:", "After editing:") |
| 317 | + - Repetitive commands |
| 318 | + """ |
| 319 | + |
| 320 | + # Narration patterns - model announcing what it will do instead of doing it |
| 321 | + NARRATION_PATTERNS = [ |
| 322 | + (r"I('ll| will) (use|call|execute|run) the (\w+) tool", "narration", "high"), |
| 323 | + (r"Let me (use|call|execute|run) the (\w+) tool", "narration", "high"), |
| 324 | + (r"Now I('ll| will) (create|write|edit|run|execute)", "narration", "medium"), |
| 325 | + (r"I('m going to| am going to) (use|call|create|write)", "narration", "medium"), |
| 326 | + (r"First,? I('ll| will) (use|call|create)", "narration", "medium"), |
| 327 | + (r"Next,? I('ll| will) (use|call|create)", "narration", "medium"), |
| 328 | + ] |
| 329 | + |
| 330 | + # Preview patterns - model showing content instead of using tools |
| 331 | + PREVIEW_PATTERNS = [ |
| 332 | + (r"(The|This) file will (look like|contain|have):", "preview", "high"), |
| 333 | + (r"After editing,? (the file|it) will (look like|contain):", "preview", "high"), |
| 334 | + (r"Here('s| is) (the|what) (content|code|file):", "preview", "high"), |
| 335 | + (r"Save this (to|as|in) [\w./]+:", "preview", "high"), |
| 336 | + (r"Create a file (with|containing):", "preview", "medium"), |
| 337 | + (r"(The|Your) [\w./]+ (should|will) (look like|contain):", "preview", "medium"), |
| 338 | + ] |
| 339 | + |
| 340 | + # Preamble patterns - model describing JSON/function calls instead of using them |
| 341 | + PREAMBLE_PATTERNS = [ |
| 342 | + (r"Here is a JSON response", "preamble", "high"), |
| 343 | + (r"Here are the function calls", "preamble", "high"), |
| 344 | + (r"Here is the response with", "preamble", "high"), |
| 345 | + (r"I will respond with", "preamble", "high"), |
| 346 | + (r"The following (JSON|function calls|tool calls)", "preamble", "high"), |
| 347 | + (r"Below (is|are) the (JSON|function|tool)", "preamble", "high"), |
| 348 | + ] |
| 349 | + |
| 350 | + # Code block patterns |
| 351 | + CODE_BLOCK_PATTERNS = [ |
| 352 | + (r'```\w+\n', "code_block", "high"), |
| 353 | + (r'```\n', "code_block", "medium"), |
| 354 | + ] |
| 355 | + |
| 356 | + def __init__(self): |
| 357 | + self._all_patterns = ( |
| 358 | + self.NARRATION_PATTERNS + |
| 359 | + self.PREVIEW_PATTERNS + |
| 360 | + self.PREAMBLE_PATTERNS + |
| 361 | + self.CODE_BLOCK_PATTERNS |
| 362 | + ) |
| 363 | + self._recent_detections: list[PatternMatch] = [] |
| 364 | + |
| 365 | + def reset(self): |
| 366 | + """Reset detection state.""" |
| 367 | + self._recent_detections = [] |
| 368 | + |
| 369 | + def detect(self, content: str) -> list[PatternMatch]: |
| 370 | + """Detect problematic patterns in content.""" |
| 371 | + matches = [] |
| 372 | + |
| 373 | + for pattern, ptype, severity in self._all_patterns: |
| 374 | + for match in re.finditer(pattern, content, re.IGNORECASE): |
| 375 | + matches.append(PatternMatch( |
| 376 | + pattern_type=ptype, |
| 377 | + match_text=match.group(0), |
| 378 | + severity=severity, |
| 379 | + )) |
| 380 | + |
| 381 | + self._recent_detections.extend(matches) |
| 382 | + return matches |
| 383 | + |
| 384 | + def has_high_severity(self, content: str) -> bool: |
| 385 | + """Check if content has high-severity patterns.""" |
| 386 | + matches = self.detect(content) |
| 387 | + return any(m.severity == "high" for m in matches) |
| 388 | + |
| 389 | + def get_steering_message(self, matches: list[PatternMatch]) -> str | None: |
| 390 | + """Generate a steering message based on detected patterns. |
| 391 | + |
| 392 | + Returns None if no steering needed. |
| 393 | + """ |
| 394 | + if not matches: |
| 395 | + return None |
| 396 | + |
| 397 | + # Prioritize high severity |
| 398 | + high_severity = [m for m in matches if m.severity == "high"] |
| 399 | + if not high_severity: |
| 400 | + return None |
| 401 | + |
| 402 | + # Generate appropriate steering message |
| 403 | + pattern_types = set(m.pattern_type for m in high_severity) |
| 404 | + |
| 405 | + if "preamble" in pattern_types: |
| 406 | + return ( |
| 407 | + "[STOP] Do not describe JSON or function calls. " |
| 408 | + "Just USE the tools directly. No preambles." |
| 409 | + ) |
| 410 | + elif "code_block" in pattern_types or "preview" in pattern_types: |
| 411 | + return ( |
| 412 | + "[REMINDER] Do not show code blocks or previews. " |
| 413 | + "Use tools directly to create/edit files. " |
| 414 | + "No ```code```, just call the tool." |
| 415 | + ) |
| 416 | + elif "narration" in pattern_types: |
| 417 | + return ( |
| 418 | + "[REMINDER] Don't announce tool calls. " |
| 419 | + "Just use the tool directly without narration." |
| 420 | + ) |
| 421 | + |
| 422 | + return None |
| 423 | + |
| 424 | + |
| 425 | +class ActionTracker: |
| 426 | + """Tracks completed actions to prevent duplicates and detect loops. |
| 427 | + |
| 428 | + Tracks: |
| 429 | + - Files created (by path) |
| 430 | + - Files edited (by path + edit signature) |
| 431 | + - Commands executed (by command string) |
| 432 | + - Directories created (by path) |
| 433 | + - Action sequence for loop detection |
| 434 | + - Response hashes for text loop detection |
| 435 | + """ |
| 436 | + |
| 437 | + MAX_SEQUENCE_LENGTH = 20 # Track last N actions |
| 438 | + LOOP_PATTERN_MIN = 2 # Minimum pattern length to detect |
| 439 | + LOOP_REPEAT_THRESHOLD = 2 # How many times pattern must repeat |
| 440 | + MAX_RESPONSE_HISTORY = 5 # Track last N responses for text loops |
| 441 | + |
| 442 | + def __init__(self): |
| 443 | + self._files_created: set[str] = set() |
| 444 | + self._files_edited: dict[str, list[str]] = {} # path -> list of edit sigs |
| 445 | + self._commands_run: set[str] = set() |
| 446 | + self._dirs_created: set[str] = set() |
| 447 | + self._action_sequence: list[str] = [] # For loop detection |
| 448 | + self._response_history: list[str] = [] # For text loop detection |
| 449 | + |
| 450 | + def reset(self): |
| 451 | + """Reset all tracking.""" |
| 452 | + self._files_created.clear() |
| 453 | + self._files_edited.clear() |
| 454 | + self._commands_run.clear() |
| 455 | + self._dirs_created.clear() |
| 456 | + self._action_sequence.clear() |
| 457 | + self._response_history.clear() |
| 458 | + |
| 459 | + def _normalize_path(self, path: str) -> str: |
| 460 | + """Normalize a file path for comparison.""" |
| 461 | + # Expand ~ and resolve to absolute |
| 462 | + expanded = Path(path).expanduser() |
| 463 | + try: |
| 464 | + return str(expanded.resolve()) |
| 465 | + except Exception: |
| 466 | + return str(expanded) |
| 467 | + |
| 468 | + def _make_edit_signature(self, old_string: str, new_string: str) -> str: |
| 469 | + """Create a signature for an edit operation.""" |
| 470 | + # Use hash of old+new to detect same edit |
| 471 | + return f"{hash(old_string)}:{hash(new_string)}" |
| 472 | + |
| 473 | + def would_duplicate_file_create(self, file_path: str) -> bool: |
| 474 | + """Check if creating this file would be a duplicate.""" |
| 475 | + norm_path = self._normalize_path(file_path) |
| 476 | + return norm_path in self._files_created |
| 477 | + |
| 478 | + def would_duplicate_edit(self, file_path: str, old_string: str, new_string: str) -> bool: |
| 479 | + """Check if this edit would be a duplicate.""" |
| 480 | + norm_path = self._normalize_path(file_path) |
| 481 | + sig = self._make_edit_signature(old_string, new_string) |
| 482 | + return sig in self._files_edited.get(norm_path, []) |
| 483 | + |
| 484 | + def would_duplicate_command(self, command: str) -> bool: |
| 485 | + """Check if this command would be a duplicate.""" |
| 486 | + # Normalize whitespace |
| 487 | + norm_cmd = " ".join(command.split()) |
| 488 | + return norm_cmd in self._commands_run |
| 489 | + |
| 490 | + def would_duplicate_mkdir(self, dir_path: str) -> bool: |
| 491 | + """Check if creating this directory would be a duplicate.""" |
| 492 | + norm_path = self._normalize_path(dir_path) |
| 493 | + return norm_path in self._dirs_created |
| 494 | + |
| 495 | + def record_file_create(self, file_path: str) -> None: |
| 496 | + """Record that a file was created.""" |
| 497 | + norm_path = self._normalize_path(file_path) |
| 498 | + self._files_created.add(norm_path) |
| 499 | + |
| 500 | + def record_edit(self, file_path: str, old_string: str, new_string: str) -> None: |
| 501 | + """Record that an edit was made.""" |
| 502 | + norm_path = self._normalize_path(file_path) |
| 503 | + sig = self._make_edit_signature(old_string, new_string) |
| 504 | + if norm_path not in self._files_edited: |
| 505 | + self._files_edited[norm_path] = [] |
| 506 | + self._files_edited[norm_path].append(sig) |
| 507 | + |
| 508 | + def record_command(self, command: str) -> None: |
| 509 | + """Record that a command was run.""" |
| 510 | + norm_cmd = " ".join(command.split()) |
| 511 | + self._commands_run.add(norm_cmd) |
| 512 | + |
| 513 | + # Also track mkdir commands specially |
| 514 | + mkdir_match = re.match(r'mkdir\s+(-p\s+)?(.+)', norm_cmd) |
| 515 | + if mkdir_match: |
| 516 | + dir_path = mkdir_match.group(2).strip().strip('"\'') |
| 517 | + self._dirs_created.add(self._normalize_path(dir_path)) |
| 518 | + |
| 519 | + def record_mkdir(self, dir_path: str) -> None: |
| 520 | + """Record that a directory was created.""" |
| 521 | + norm_path = self._normalize_path(dir_path) |
| 522 | + self._dirs_created.add(norm_path) |
| 523 | + |
| 524 | + def check_tool_call(self, tool_name: str, arguments: dict) -> tuple[bool, str]: |
| 525 | + """Check if a tool call would be a duplicate. |
| 526 | + |
| 527 | + Returns (is_duplicate, reason). |
| 528 | + """ |
| 529 | + if tool_name == "write": |
| 530 | + file_path = arguments.get("file_path", "") |
| 531 | + if self.would_duplicate_file_create(file_path): |
| 532 | + return True, f"File already created: {file_path}" |
| 533 | + |
| 534 | + elif tool_name == "edit": |
| 535 | + file_path = arguments.get("file_path", "") |
| 536 | + old_string = arguments.get("old_string", "") |
| 537 | + new_string = arguments.get("new_string", "") |
| 538 | + if self.would_duplicate_edit(file_path, old_string, new_string): |
| 539 | + return True, f"Same edit already applied to: {file_path}" |
| 540 | + |
| 541 | + elif tool_name == "bash": |
| 542 | + command = arguments.get("command", "") |
| 543 | + if self.would_duplicate_command(command): |
| 544 | + return True, f"Command already executed: {command[:50]}..." |
| 545 | + |
| 546 | + return False, "" |
| 547 | + |
| 548 | + def record_tool_call(self, tool_name: str, arguments: dict) -> None: |
| 549 | + """Record a tool call as completed.""" |
| 550 | + # Track in action sequence for loop detection |
| 551 | + self._action_sequence.append(tool_name) |
| 552 | + if len(self._action_sequence) > self.MAX_SEQUENCE_LENGTH: |
| 553 | + self._action_sequence.pop(0) |
| 554 | + |
| 555 | + if tool_name == "write": |
| 556 | + file_path = arguments.get("file_path", "") |
| 557 | + if file_path: |
| 558 | + self.record_file_create(file_path) |
| 559 | + |
| 560 | + elif tool_name == "edit": |
| 561 | + file_path = arguments.get("file_path", "") |
| 562 | + old_string = arguments.get("old_string", "") |
| 563 | + new_string = arguments.get("new_string", "") |
| 564 | + if file_path: |
| 565 | + self.record_edit(file_path, old_string, new_string) |
| 566 | + |
| 567 | + elif tool_name == "bash": |
| 568 | + command = arguments.get("command", "") |
| 569 | + if command: |
| 570 | + self.record_command(command) |
| 571 | + |
| 572 | + def detect_loop(self) -> tuple[bool, str]: |
| 573 | + """Detect if the agent is in a repetitive loop. |
| 574 | + |
| 575 | + Returns (is_loop, pattern_description). |
| 576 | + """ |
| 577 | + seq = self._action_sequence |
| 578 | + if len(seq) < self.LOOP_PATTERN_MIN * self.LOOP_REPEAT_THRESHOLD: |
| 579 | + return False, "" |
| 580 | + |
| 581 | + # Check for repeating patterns of length 2, 3, 4 |
| 582 | + for pattern_len in range(self.LOOP_PATTERN_MIN, min(6, len(seq) // 2 + 1)): |
| 583 | + # Get the most recent pattern |
| 584 | + pattern = seq[-pattern_len:] |
| 585 | + |
| 586 | + # Count how many times this pattern appears consecutively |
| 587 | + repeats = 1 |
| 588 | + for i in range(len(seq) - pattern_len * 2, -1, -pattern_len): |
| 589 | + if seq[i:i + pattern_len] == pattern: |
| 590 | + repeats += 1 |
| 591 | + else: |
| 592 | + break |
| 593 | + |
| 594 | + if repeats >= self.LOOP_REPEAT_THRESHOLD: |
| 595 | + pattern_str = " → ".join(pattern) |
| 596 | + return True, f"Repeating pattern detected ({repeats}x): {pattern_str}" |
| 597 | + |
| 598 | + return False, "" |
| 599 | + |
| 600 | + def _normalize_response(self, response: str) -> str: |
| 601 | + """Normalize a response for comparison. |
| 602 | + |
| 603 | + Strips whitespace, lowercases, and takes first ~200 chars |
| 604 | + to create a signature for detecting similar responses. |
| 605 | + """ |
| 606 | + # Take first part of response for comparison |
| 607 | + normalized = response.strip().lower()[:200] |
| 608 | + # Remove common variable parts like paths, numbers |
| 609 | + normalized = re.sub(r'/[\w/.-]+', '<PATH>', normalized) |
| 610 | + normalized = re.sub(r'\d+', '<NUM>', normalized) |
| 611 | + return normalized |
| 612 | + |
| 613 | + def record_response(self, response: str) -> None: |
| 614 | + """Record a response for text loop detection.""" |
| 615 | + normalized = self._normalize_response(response) |
| 616 | + self._response_history.append(normalized) |
| 617 | + if len(self._response_history) > self.MAX_RESPONSE_HISTORY: |
| 618 | + self._response_history.pop(0) |
| 619 | + |
| 620 | + def detect_text_loop(self, response: str) -> tuple[bool, str]: |
| 621 | + """Detect if the agent is repeating the same response. |
| 622 | + |
| 623 | + Returns (is_loop, description). |
| 624 | + """ |
| 625 | + if len(self._response_history) < 1: |
| 626 | + return False, "" |
| 627 | + |
| 628 | + normalized = self._normalize_response(response) |
| 629 | + |
| 630 | + # Check if this response matches recent ones (exact match) |
| 631 | + exact_matches = sum(1 for r in self._response_history if r == normalized) |
| 632 | + if exact_matches >= 1: |
| 633 | + return True, f"Agent repeated the same response {exact_matches + 1} times" |
| 634 | + |
| 635 | + # Check for common repetitive phrases that indicate looping |
| 636 | + repetitive_phrases = [ |
| 637 | + "apologies for any confusion", |
| 638 | + "let me proceed", |
| 639 | + "i will now use the", |
| 640 | + "let's proceed with creating", |
| 641 | + "i'll create the", |
| 642 | + ] |
| 643 | + response_lower = response.lower() |
| 644 | + for phrase in repetitive_phrases: |
| 645 | + if phrase in response_lower: |
| 646 | + # Check if this phrase appeared in recent responses |
| 647 | + phrase_count = sum(1 for r in self._response_history if phrase in r) |
| 648 | + if phrase_count >= 1: |
| 649 | + return True, f"Agent is stuck repeating '{phrase}'" |
| 650 | + |
| 651 | + # Check for high similarity (not exact match) |
| 652 | + current_words = set(normalized.split()) |
| 653 | + similarity_matches = 0 |
| 654 | + for prev in self._response_history[-3:]: |
| 655 | + prev_words = set(prev.split()) |
| 656 | + if len(current_words) > 5 and len(prev_words) > 5: |
| 657 | + overlap = len(current_words & prev_words) |
| 658 | + similarity = overlap / max(len(current_words), len(prev_words)) |
| 659 | + if similarity > 0.7: # Lower threshold |
| 660 | + similarity_matches += 1 |
| 661 | + |
| 662 | + if similarity_matches >= 1: |
| 663 | + return True, "Agent responses are highly repetitive" |
| 664 | + |
| 665 | + return False, "" |
| 666 | + |
| 667 | + |
| 668 | +@dataclass |
| 669 | +class ValidationResult: |
| 670 | + """Result of pre-action validation.""" |
| 671 | + valid: bool |
| 672 | + reason: str = "" |
| 673 | + suggestion: str = "" |
| 674 | + severity: str = "warning" # 'warning', 'error', 'block' |
| 675 | + |
| 676 | + |
| 677 | +class PreActionValidator: |
| 678 | + """Validates tool calls before execution to catch problematic actions. |
| 679 | + |
| 680 | + Catches: |
| 681 | + - Empty/missing required arguments |
| 682 | + - Invalid file paths |
| 683 | + - Dangerous bash commands |
| 684 | + - Writing empty content |
| 685 | + - Nonsensical operations |
| 686 | + """ |
| 687 | + |
| 688 | + # Dangerous bash patterns that should be blocked |
| 689 | + DANGEROUS_PATTERNS = [ |
| 690 | + (r'rm\s+(-[rf]+\s+)?/', "Dangerous: removing from root directory"), |
| 691 | + (r'rm\s+-rf\s+~', "Dangerous: removing home directory"), |
| 692 | + (r'>\s*/dev/sd[a-z]', "Dangerous: writing directly to disk device"), |
| 693 | + (r'mkfs\.', "Dangerous: formatting filesystem"), |
| 694 | + (r'dd\s+.*of=/dev/', "Dangerous: dd to device"), |
| 695 | + (r'chmod\s+-R\s+777\s+/', "Dangerous: making everything world-writable"), |
| 696 | + (r':\(\)\s*\{\s*:\|:\s*&\s*\}\s*;', "Dangerous: fork bomb"), |
| 697 | + ] |
| 698 | + |
| 699 | + # Suspicious patterns that warrant a warning |
| 700 | + SUSPICIOUS_PATTERNS = [ |
| 701 | + (r'rm\s+-rf\s+', "Warning: recursive force delete"), |
| 702 | + (r'>\s*/etc/', "Warning: overwriting system config"), |
| 703 | + (r'curl\s+.*\|\s*sh', "Warning: piping curl to shell"), |
| 704 | + (r'wget\s+.*\|\s*sh', "Warning: piping wget to shell"), |
| 705 | + (r'eval\s+', "Warning: using eval"), |
| 706 | + (r'sudo\s+', "Warning: using sudo"), |
| 707 | + ] |
| 708 | + |
| 709 | + def validate(self, tool_name: str, arguments: dict) -> ValidationResult: |
| 710 | + """Validate a tool call before execution. |
| 711 | + |
| 712 | + Returns ValidationResult indicating if the action is valid. |
| 713 | + """ |
| 714 | + if tool_name == "bash": |
| 715 | + return self._validate_bash(arguments) |
| 716 | + elif tool_name == "write": |
| 717 | + return self._validate_write(arguments) |
| 718 | + elif tool_name == "edit": |
| 719 | + return self._validate_edit(arguments) |
| 720 | + elif tool_name == "read": |
| 721 | + return self._validate_read(arguments) |
| 722 | + elif tool_name in ("glob", "grep"): |
| 723 | + return self._validate_search(tool_name, arguments) |
| 724 | + |
| 725 | + return ValidationResult(valid=True) |
| 726 | + |
| 727 | + def _validate_bash(self, arguments: dict) -> ValidationResult: |
| 728 | + """Validate bash command.""" |
| 729 | + command = arguments.get("command", "") |
| 730 | + |
| 731 | + if not command or not command.strip(): |
| 732 | + return ValidationResult( |
| 733 | + valid=False, |
| 734 | + reason="Empty command", |
| 735 | + suggestion="Provide a valid command to execute", |
| 736 | + severity="error", |
| 737 | + ) |
| 738 | + |
| 739 | + # Check for dangerous patterns |
| 740 | + for pattern, reason in self.DANGEROUS_PATTERNS: |
| 741 | + if re.search(pattern, command): |
| 742 | + return ValidationResult( |
| 743 | + valid=False, |
| 744 | + reason=reason, |
| 745 | + suggestion="This command is too dangerous to execute", |
| 746 | + severity="block", |
| 747 | + ) |
| 748 | + |
| 749 | + # Check for suspicious patterns (allow but warn) |
| 750 | + for pattern, reason in self.SUSPICIOUS_PATTERNS: |
| 751 | + if re.search(pattern, command): |
| 752 | + return ValidationResult( |
| 753 | + valid=True, # Allow but flag |
| 754 | + reason=reason, |
| 755 | + severity="warning", |
| 756 | + ) |
| 757 | + |
| 758 | + # Check for commands that won't work in non-interactive mode |
| 759 | + interactive_patterns = [ |
| 760 | + (r'\bnano\b', "nano requires interactive terminal"), |
| 761 | + (r'\bvim?\b', "vim requires interactive terminal"), |
| 762 | + (r'\bemacs\b', "emacs requires interactive terminal"), |
| 763 | + (r'\bless\b', "less requires interactive terminal"), |
| 764 | + (r'\bmore\b', "more requires interactive terminal"), |
| 765 | + (r'\btop\b', "top requires interactive terminal"), |
| 766 | + (r'\bhtop\b', "htop requires interactive terminal"), |
| 767 | + ] |
| 768 | + for pattern, reason in interactive_patterns: |
| 769 | + if re.search(pattern, command): |
| 770 | + return ValidationResult( |
| 771 | + valid=False, |
| 772 | + reason=reason, |
| 773 | + suggestion="Use non-interactive alternatives (cat, head, tail for viewing; sed for editing)", |
| 774 | + severity="error", |
| 775 | + ) |
| 776 | + |
| 777 | + return ValidationResult(valid=True) |
| 778 | + |
| 779 | + def _validate_write(self, arguments: dict) -> ValidationResult: |
| 780 | + """Validate write operation.""" |
| 781 | + file_path = arguments.get("file_path", "") |
| 782 | + content = arguments.get("content", "") |
| 783 | + |
| 784 | + if not file_path or not file_path.strip(): |
| 785 | + return ValidationResult( |
| 786 | + valid=False, |
| 787 | + reason="Empty file path", |
| 788 | + suggestion="Provide a valid file path", |
| 789 | + severity="error", |
| 790 | + ) |
| 791 | + |
| 792 | + # Check for path issues |
| 793 | + path_result = self._validate_path(file_path) |
| 794 | + if not path_result.valid: |
| 795 | + return path_result |
| 796 | + |
| 797 | + # Warn about empty content (might be intentional) |
| 798 | + if content is None or (isinstance(content, str) and not content.strip()): |
| 799 | + return ValidationResult( |
| 800 | + valid=True, # Allow but warn |
| 801 | + reason="Writing empty content to file", |
| 802 | + severity="warning", |
| 803 | + ) |
| 804 | + |
| 805 | + # Check for writing to sensitive locations |
| 806 | + sensitive_paths = ['/etc/', '/usr/', '/bin/', '/sbin/', '/boot/', '/sys/', '/proc/'] |
| 807 | + for sensitive in sensitive_paths: |
| 808 | + if file_path.startswith(sensitive): |
| 809 | + return ValidationResult( |
| 810 | + valid=False, |
| 811 | + reason=f"Cannot write to system directory: {sensitive}", |
| 812 | + suggestion="Write to a user directory instead", |
| 813 | + severity="block", |
| 814 | + ) |
| 815 | + |
| 816 | + return ValidationResult(valid=True) |
| 817 | + |
| 818 | + def _validate_edit(self, arguments: dict) -> ValidationResult: |
| 819 | + """Validate edit operation.""" |
| 820 | + file_path = arguments.get("file_path", "") |
| 821 | + old_string = arguments.get("old_string", "") |
| 822 | + new_string = arguments.get("new_string", "") |
| 823 | + |
| 824 | + if not file_path or not file_path.strip(): |
| 825 | + return ValidationResult( |
| 826 | + valid=False, |
| 827 | + reason="Empty file path", |
| 828 | + suggestion="Provide a valid file path", |
| 829 | + severity="error", |
| 830 | + ) |
| 831 | + |
| 832 | + # Check for path issues |
| 833 | + path_result = self._validate_path(file_path) |
| 834 | + if not path_result.valid: |
| 835 | + return path_result |
| 836 | + |
| 837 | + # old_string can be empty (for prepending), but warn |
| 838 | + if old_string is None: |
| 839 | + return ValidationResult( |
| 840 | + valid=False, |
| 841 | + reason="old_string is None", |
| 842 | + suggestion="Provide the text to replace (can be empty string for prepend)", |
| 843 | + severity="error", |
| 844 | + ) |
| 845 | + |
| 846 | + # new_string can legitimately be empty (for deletion) |
| 847 | + if new_string is None: |
| 848 | + return ValidationResult( |
| 849 | + valid=False, |
| 850 | + reason="new_string is None", |
| 851 | + suggestion="Provide the replacement text (can be empty string for deletion)", |
| 852 | + severity="error", |
| 853 | + ) |
| 854 | + |
| 855 | + # Check if old and new are identical |
| 856 | + if old_string == new_string: |
| 857 | + return ValidationResult( |
| 858 | + valid=False, |
| 859 | + reason="old_string and new_string are identical - no change would occur", |
| 860 | + suggestion="Provide different old and new strings", |
| 861 | + severity="error", |
| 862 | + ) |
| 863 | + |
| 864 | + return ValidationResult(valid=True) |
| 865 | + |
| 866 | + def _validate_read(self, arguments: dict) -> ValidationResult: |
| 867 | + """Validate read operation.""" |
| 868 | + file_path = arguments.get("file_path", "") |
| 869 | + |
| 870 | + if not file_path or not file_path.strip(): |
| 871 | + return ValidationResult( |
| 872 | + valid=False, |
| 873 | + reason="Empty file path", |
| 874 | + suggestion="Provide a valid file path", |
| 875 | + severity="error", |
| 876 | + ) |
| 877 | + |
| 878 | + return self._validate_path(file_path) |
| 879 | + |
| 880 | + def _validate_search(self, tool_name: str, arguments: dict) -> ValidationResult: |
| 881 | + """Validate glob/grep operations.""" |
| 882 | + pattern = arguments.get("pattern", "") |
| 883 | + |
| 884 | + if not pattern or not pattern.strip(): |
| 885 | + return ValidationResult( |
| 886 | + valid=False, |
| 887 | + reason=f"Empty {tool_name} pattern", |
| 888 | + suggestion="Provide a valid search pattern", |
| 889 | + severity="error", |
| 890 | + ) |
| 891 | + |
| 892 | + return ValidationResult(valid=True) |
| 893 | + |
| 894 | + def _validate_path(self, file_path: str) -> ValidationResult: |
| 895 | + """Validate a file path for common issues.""" |
| 896 | + # Check for null bytes (security issue) |
| 897 | + if '\x00' in file_path: |
| 898 | + return ValidationResult( |
| 899 | + valid=False, |
| 900 | + reason="Path contains null byte", |
| 901 | + suggestion="Remove null bytes from path", |
| 902 | + severity="block", |
| 903 | + ) |
| 904 | + |
| 905 | + # Check for path traversal attempts outside reasonable bounds |
| 906 | + # (Some traversal is fine for relative paths) |
| 907 | + if '/../../../' in file_path or file_path.count('..') > 5: |
| 908 | + return ValidationResult( |
| 909 | + valid=False, |
| 910 | + reason="Excessive path traversal", |
| 911 | + suggestion="Use a direct path instead", |
| 912 | + severity="warning", |
| 913 | + ) |
| 914 | + |
| 915 | + return ValidationResult(valid=True) |
| 916 | + |
| 917 | + |
| 918 | +class RuntimeSafeguards: |
| 919 | + """Combined runtime safeguards for the agent. |
| 920 | + |
| 921 | + Usage: |
| 922 | + safeguards = RuntimeSafeguards() |
| 923 | + |
| 924 | + # For streaming: |
| 925 | + filtered = safeguards.filter_stream_chunk(chunk) |
| 926 | + if safeguards.should_steer(): |
| 927 | + steering_msg = safeguards.get_steering_message() |
| 928 | + |
| 929 | + # Before tool execution: |
| 930 | + is_dup, reason = safeguards.check_duplicate(tool_name, args) |
| 931 | + if is_dup: |
| 932 | + skip this tool call |
| 933 | + |
| 934 | + # Pre-action validation: |
| 935 | + validation = safeguards.validate_action(tool_name, args) |
| 936 | + if not validation.valid: |
| 937 | + skip or warn |
| 938 | + |
| 939 | + # After tool execution: |
| 940 | + safeguards.record_action(tool_name, args) |
| 941 | + """ |
| 942 | + |
| 943 | + def __init__(self): |
| 944 | + self.code_filter = CodeBlockFilter() |
| 945 | + self.pattern_detector = PatternDetector() |
| 946 | + self.action_tracker = ActionTracker() |
| 947 | + self.validator = PreActionValidator() |
| 948 | + self._pending_steering: str | None = None |
| 949 | + self._accumulated_content = "" |
| 950 | + |
| 951 | + def reset(self): |
| 952 | + """Reset all safeguards for a new conversation.""" |
| 953 | + self.code_filter.reset() |
| 954 | + self.pattern_detector.reset() |
| 955 | + self.action_tracker.reset() |
| 956 | + self._pending_steering = None |
| 957 | + self._accumulated_content = "" |
| 958 | + |
| 959 | + def filter_stream_chunk(self, chunk: str) -> str: |
| 960 | + """Filter a streaming chunk, removing code blocks. |
| 961 | + |
| 962 | + Also detects patterns for potential steering. |
| 963 | + """ |
| 964 | + # Filter code blocks |
| 965 | + result = self.code_filter.filter_chunk(chunk) |
| 966 | + |
| 967 | + # Accumulate for pattern detection |
| 968 | + self._accumulated_content += chunk |
| 969 | + |
| 970 | + # Check for patterns periodically (every 200 chars) |
| 971 | + if len(self._accumulated_content) > 200: |
| 972 | + matches = self.pattern_detector.detect(self._accumulated_content) |
| 973 | + if matches: |
| 974 | + steering = self.pattern_detector.get_steering_message(matches) |
| 975 | + if steering: |
| 976 | + self._pending_steering = steering |
| 977 | + self._accumulated_content = self._accumulated_content[-100:] # Keep last 100 chars for context |
| 978 | + |
| 979 | + return result.content |
| 980 | + |
| 981 | + def filter_complete_content(self, content: str) -> str: |
| 982 | + """Filter complete content (non-streaming).""" |
| 983 | + result = self.code_filter.filter_complete(content) |
| 984 | + |
| 985 | + # Also detect patterns |
| 986 | + matches = self.pattern_detector.detect(content) |
| 987 | + if matches: |
| 988 | + steering = self.pattern_detector.get_steering_message(matches) |
| 989 | + if steering: |
| 990 | + self._pending_steering = steering |
| 991 | + |
| 992 | + return result.content |
| 993 | + |
| 994 | + def should_steer(self) -> bool: |
| 995 | + """Check if we should inject a steering message.""" |
| 996 | + return self._pending_steering is not None |
| 997 | + |
| 998 | + def get_steering_message(self) -> str | None: |
| 999 | + """Get pending steering message and clear it.""" |
| 1000 | + msg = self._pending_steering |
| 1001 | + self._pending_steering = None |
| 1002 | + return msg |
| 1003 | + |
| 1004 | + def check_duplicate(self, tool_name: str, arguments: dict) -> tuple[bool, str]: |
| 1005 | + """Check if a tool call would be a duplicate.""" |
| 1006 | + return self.action_tracker.check_tool_call(tool_name, arguments) |
| 1007 | + |
| 1008 | + def record_action(self, tool_name: str, arguments: dict) -> None: |
| 1009 | + """Record a completed tool action.""" |
| 1010 | + self.action_tracker.record_tool_call(tool_name, arguments) |
| 1011 | + |
| 1012 | + def detect_loop(self) -> tuple[bool, str]: |
| 1013 | + """Detect if the agent is in a repetitive loop. |
| 1014 | + |
| 1015 | + Returns (is_loop, pattern_description). |
| 1016 | + """ |
| 1017 | + return self.action_tracker.detect_loop() |
| 1018 | + |
| 1019 | + def validate_action(self, tool_name: str, arguments: dict) -> ValidationResult: |
| 1020 | + """Validate a tool action before execution. |
| 1021 | + |
| 1022 | + Returns ValidationResult with validity and any warnings/errors. |
| 1023 | + """ |
| 1024 | + return self.validator.validate(tool_name, arguments) |
| 1025 | + |
| 1026 | + def record_response(self, response: str) -> None: |
| 1027 | + """Record a response for text loop detection.""" |
| 1028 | + self.action_tracker.record_response(response) |
| 1029 | + |
| 1030 | + def detect_text_loop(self, response: str) -> tuple[bool, str]: |
| 1031 | + """Detect if the agent is repeating the same response. |
| 1032 | + |
| 1033 | + Returns (is_loop, description). |
| 1034 | + """ |
| 1035 | + return self.action_tracker.detect_text_loop(response) |