Overview
Error handler that classifies failures via regex pattern matching, selects recovery strategies by scoring success probability and time cost, adjusts parameters automatically, suggests alternative tools, and escalates to humans when recovery is exhausted.
Description
The IntelligentErrorHandler class is the core of HexStrike's error recovery system. It maintains regex error_patterns for 11 error types (timeout, permission_denied, network_unreachable, rate_limited, tool_not_found, invalid_parameters, resource_exhausted, authentication_failed, target_unreachable, parsing_error, unknown), recovery_strategies per error type, tool_alternatives mappings, and parameter_adjustments. The classify_error method matches error messages against patterns. handle_tool_failure orchestrates classification, context creation, and strategy selection. _select_best_strategy scores strategies by adjusted success probability minus time cost.
Usage
- Error classification: Call classify_error(error_message, exception) to get an ErrorType enum
- Recovery orchestration: Call handle_tool_failure(tool, error, context) to get a RecoveryStrategy
- Parameter adjustment: Call auto_adjust_parameters(tool, error_type, params) for tuned retry params
- Alternative tools: Call get_alternative_tool(failed_tool, context) for a replacement
- Human escalation: Call escalate_to_human(context, urgency) for escalation reports
Code Reference
Source Location
- Repository: HexStrike AI
- File: hexstrike_server.py
- Lines: L1606-2200 (IntelligentErrorHandler class); L1558-1604 (ErrorType, RecoveryAction, ErrorContext, RecoveryStrategy)
Signature
class ErrorType(Enum):
TIMEOUT = "timeout"
PERMISSION_DENIED = "permission_denied"
NETWORK_UNREACHABLE = "network_unreachable"
RATE_LIMITED = "rate_limited"
TOOL_NOT_FOUND = "tool_not_found"
INVALID_PARAMETERS = "invalid_parameters"
RESOURCE_EXHAUSTED = "resource_exhausted"
AUTHENTICATION_FAILED = "authentication_failed"
TARGET_UNREACHABLE = "target_unreachable"
PARSING_ERROR = "parsing_error"
UNKNOWN = "unknown"
class RecoveryAction(Enum):
RETRY_WITH_BACKOFF = "retry_with_backoff"
RETRY_WITH_REDUCED_SCOPE = "retry_with_reduced_scope"
SWITCH_TO_ALTERNATIVE_TOOL = "switch_to_alternative_tool"
ADJUST_PARAMETERS = "adjust_parameters"
ESCALATE_TO_HUMAN = "escalate_to_human"
GRACEFUL_DEGRADATION = "graceful_degradation"
ABORT_OPERATION = "abort_operation"
class IntelligentErrorHandler:
def classify_error(self, error_message: str, exception: Exception = None) -> ErrorType: ...
def handle_tool_failure(self, tool: str, error: Exception, context: Dict[str, Any]) -> RecoveryStrategy: ...
def _select_best_strategy(self, strategies: List[RecoveryStrategy], context: ErrorContext) -> RecoveryStrategy: ...
def auto_adjust_parameters(self, tool: str, error_type: ErrorType, original_params: Dict[str, Any]) -> Dict[str, Any]: ...
def get_alternative_tool(self, failed_tool: str, context: Dict[str, Any]) -> Optional[str]: ...
def escalate_to_human(self, context: ErrorContext, urgency: str = "medium") -> Dict[str, Any]: ...
Import
from hexstrike_server import IntelligentErrorHandler, ErrorType, RecoveryAction, ErrorContext, RecoveryStrategy
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| error_message |
str |
Yes (classify_error) |
Error text from stderr or exception
|
| exception |
Exception |
No |
Exception object for type-based classification
|
| tool |
str |
Yes (handle_tool_failure) |
Name of the failed tool
|
| error |
Exception |
Yes (handle_tool_failure) |
The failure exception
|
| context |
Dict[str, Any] |
Yes (handle_tool_failure) |
Contains target, parameters, attempt_count
|
| context (escalation) |
ErrorContext |
Yes (escalate_to_human) |
Full error context dataclass
|
| urgency |
str |
No (escalate_to_human) |
"medium" or "high"
|
Outputs
| Name |
Type |
Description
|
| ErrorType |
Enum |
Classified error type
|
| RecoveryStrategy |
dataclass |
action, parameters, max_attempts, backoff_multiplier, success_probability, estimated_time
|
| adjusted params |
Dict[str, Any] |
Parameters with tool-specific or generic adjustments
|
| alternative tool |
Optional[str] |
Suggested replacement tool name
|
| escalation |
Dict[str, Any] |
timestamp, tool, target, error_type, suggested_actions, urgency
|
Usage Examples
Classifying and Handling an Error
from hexstrike_server import IntelligentErrorHandler, ErrorType, RecoveryAction
handler = IntelligentErrorHandler()
# Classify an error message
error_type = handler.classify_error("Connection timed out to target host")
print(f"Error type: {error_type}") # ErrorType.TIMEOUT
# Get a recovery strategy
strategy = handler.handle_tool_failure(
tool="nmap",
error=Exception("Connection timed out"),
context={"target": "192.168.1.1", "attempt_count": 1}
)
print(f"Action: {strategy.action}") # RecoveryAction.RETRY_WITH_BACKOFF
print(f"Success probability: {strategy.success_probability}")
Escalating to Human
from hexstrike_server import ErrorContext, ErrorType
from datetime import datetime
context = ErrorContext(
tool_name="sqlmap",
target="example.com",
parameters={"url": "http://example.com"},
error_type=ErrorType.RATE_LIMITED,
error_message="Rate limit exceeded (429)",
attempt_count=3,
timestamp=datetime.now(),
stack_trace="",
system_resources={}
)
escalation = handler.escalate_to_human(context, urgency="high")
print(f"Urgency: {escalation['urgency']}")
print(f"Suggested actions: {escalation['suggested_actions']}")
Related Pages
Implements Principle
Uses Heuristic
Requires Environment