Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0x4m4 Hexstrike ai execute command with recovery

From Leeroopedia
Knowledge Sources
Domains Subprocess, Error_Recovery, Tool_Execution
Last Updated 2026-10-10 00:00 GMT

Overview

Executes shell commands with intelligent error detection, classification, and multi-strategy recovery including retry, parameter adjustment, tool switching, and human escalation.

Description

The execute_command_with_recovery function wraps execute_command with a recovery loop. On each failure, it delegates to IntelligentErrorHandler.handle_tool_failure to classify the error and select a recovery strategy. It then applies the strategy: RETRY_WITH_BACKOFF waits with exponential backoff, RETRY_WITH_REDUCED_SCOPE adjusts parameters, SWITCH_TO_ALTERNATIVE_TOOL suggests a replacement, ADJUST_PARAMETERS tunes tool-specific settings, GRACEFUL_DEGRADATION returns partial results, ESCALATE_TO_HUMAN creates an escalation report, and ABORT_OPERATION terminates. The function tracks a full recovery history across attempts.

Usage

Call this function from Flask tool endpoints to execute external security tools with automatic recovery:

  • Standard tool execution: Use when use_recovery=True is set on the API request
  • Error-prone tools: Tools like nmap, gobuster, and sqlmap benefit from automatic retry on timeouts and rate limits
  • Production scans: Long-running scans where unattended recovery is needed

Code Reference

Source Location

  • Repository: HexStrike AI
  • File: hexstrike_server.py
  • Lines: L8664-8877 (execute_command_with_recovery); L8636-8662 (execute_command); L8879+ (_rebuild_command_with_params)

Signature

def execute_command_with_recovery(
    tool_name: str,
    command: str,
    parameters: Dict[str, Any] = None,
    use_cache: bool = True,
    max_attempts: int = 3
) -> Dict[str, Any]:
    """
    Execute a command with intelligent error handling and recovery.

    Args:
        tool_name: Name of the tool being executed
        command: The shell command to execute
        parameters: Tool parameters for recovery context
        use_cache: Whether to use command caching
        max_attempts: Maximum number of recovery attempts

    Returns:
        Dict with success, stdout, stderr, recovery_info, and optionally
        human_escalation or alternative_tool_suggested
    """

def execute_command(command: str, use_cache: bool = True) -> Dict[str, Any]:
    """Execute a shell command with caching support (no recovery)."""

Import

# These are module-level functions in hexstrike_server.py
# Called internally by Flask endpoints; not typically imported directly
from hexstrike_server import execute_command_with_recovery, execute_command

I/O Contract

Inputs

Name Type Required Description
tool_name str Yes Name of the tool (e.g. "nmap", "gobuster")
command str Yes Full shell command string to execute
parameters Dict[str, Any] No Tool context for recovery (target, ports, etc.)
use_cache bool No Whether to cache results (default True)
max_attempts int No Maximum recovery attempts (default 3)

Outputs

Name Type Description
success bool Whether execution ultimately succeeded
stdout str Command standard output
stderr str Command standard error
recovery_info Dict Contains attempts_made, recovery_applied, recovery_history, final_action
human_escalation Dict Present when escalated to human (urgency, suggested_actions)
alternative_tool_suggested str Present when tool switch is recommended

Usage Examples

Execute with Recovery

from hexstrike_server import execute_command_with_recovery

# Execute nmap with automatic error recovery
result = execute_command_with_recovery(
    tool_name="nmap",
    command="nmap -sV -T4 -Pn 192.168.1.1",
    parameters={"target": "192.168.1.1", "scan_type": "-sV"},
    use_cache=True,
    max_attempts=3
)

if result["success"]:
    print(f"Scan succeeded after {result['recovery_info']['attempts_made']} attempt(s)")
    print(result["stdout"])
else:
    if "human_escalation" in result:
        print(f"Escalated to human: {result['human_escalation']['suggested_actions']}")
    if "alternative_tool_suggested" in result:
        print(f"Try alternative: {result['alternative_tool_suggested']}")

Execute Without Recovery

from hexstrike_server import execute_command

# Simple execution without recovery logic
result = execute_command("nmap -sV 192.168.1.1", use_cache=True)
print(f"Success: {result['success']}")

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment