# executionState.py # Contains all execution state management logic extracted from managerChat.py import logging from typing import List from datetime import datetime, UTC from modules.interfaces.interfaceChatModel import TaskStep, ActionResult logger = logging.getLogger(__name__) class TaskExecutionState: """Manages execution state for a task with retry logic""" def __init__(self, task_step: TaskStep): self.task_step = task_step self.successful_actions: List[ActionResult] = [] # Preserved across retries self.failed_actions: List[ActionResult] = [] # For analysis self.current_action_index = 0 self.retry_count = 0 self.max_retries = 3 def addSuccessfulAction(self, action_result: ActionResult): """Add a successful action to the state""" self.successful_actions.append(action_result) self.current_action_index += 1 def addFailedAction(self, action_result: ActionResult): """Add a failed action to the state for analysis""" self.failed_actions.append(action_result) self.current_action_index += 1 def getAvailableResults(self) -> list: """Get available results from successful actions""" results = [] for action in self.successful_actions: if action.data and action.data.get('result'): results.append(action.data['result']) return results def shouldRetryTask(self) -> bool: """Determine if task should be retried based on failure patterns""" return len(self.failed_actions) > 0 and self.canRetry() def canRetry(self) -> bool: """Check if task can be retried""" return self.retry_count < self.max_retries def incrementRetryCount(self): """Increment retry count""" self.retry_count += 1 def getFailurePatterns(self) -> list: """Analyze failure patterns from failed actions""" patterns = [] for action in self.failed_actions: error = action.error.lower() if action.error else '' if "timeout" in error: patterns.append("timeout_issues") elif "document_not_found" in error or "file not found" in error: patterns.append("document_reference_issues") elif "empty_result" in error or "no content" in error: patterns.append("content_extraction_issues") elif "invalid_format" in error or "wrong format" in error: patterns.append("format_issues") elif "permission" in error or "access denied" in error: patterns.append("permission_issues") return list(set(patterns))