Skip to main content
Component: CLIToolsMixin Module: gaia_agent_code.tools.cli_tools Import: from gaia_agent_code.tools.cli_tools import CLIToolsMixin

Overview

CLIToolsMixin provides universal CLI command execution with background process management, error detection, and graceful shutdown capabilities. It enables agents to run any command-line tool (npm, python, docker, gh, etc.) with support for long-running processes like development servers. Key Features:
  • Universal CLI command execution without restrictions
  • Background process management with PID tracking
  • Automatic startup error detection
  • Port conflict management
  • Graceful process shutdown (SIGINT/Ctrl+C)
  • Real-time output streaming
  • Cross-platform support (Windows, Unix-like systems)

Requirements

Functional Requirements

  1. Universal Command Execution
    • Execute any CLI command without security restrictions
    • Support for both foreground and background execution
    • Configurable timeouts for long-running commands
    • Auto-response for interactive prompts
  2. Background Process Management
    • Start processes in background with PID tracking
    • Monitor process startup for errors
    • Manage multiple background processes
    • Stop processes gracefully or forcefully
    • Retrieve process logs and status
  3. Error Detection
    • Scan output for common error patterns:
      • Port conflicts
      • Missing dependencies
      • Compilation errors
      • Permission issues
      • Resource exhaustion
      • Network errors
    • Report errors with context and line numbers
    • Detect process crashes during startup
  4. Port Management
    • Detect ports from command arguments
    • Check port availability before starting
    • Find available ports in a range
    • Kill processes using specific ports
    • Track port-to-PID mappings
  5. Process Lifecycle
    • Start processes with proper isolation (process groups/sessions)
    • Monitor startup with configurable timeout
    • Graceful shutdown with escalating signals:
      1. SIGINT (Ctrl+C)
      2. SIGTERM
      3. SIGKILL (force)
    • Cleanup on agent destruction

Non-Functional Requirements

  1. Platform Compatibility
    • Windows (CREATE_NEW_PROCESS_GROUP)
    • Unix-like systems (start_new_session)
    • Handle platform-specific signal handling
  2. Performance
    • Real-time output streaming
    • Efficient log file management
    • Minimal overhead for monitoring
  3. Reliability
    • Automatic cleanup of background processes
    • Safe handling of process crashes
    • Robust error recovery

API Specification

File Location

Public Interface


Tool Specifications

1. run_cli_command

Execute any CLI command with optional background execution. Parameters:
  • command (str, required): Command to execute
  • working_dir (str, optional): Directory to execute in
  • background (bool, optional): Run as background process
  • timeout (int, optional): Timeout for foreground commands (default: 120s)
  • startup_timeout (int, optional): Timeout for startup error detection (default: 5s)
  • expected_port (int, optional): Port the process should bind to
  • auto_respond (str, optional): Response for interactive prompts (default: “y\n”)
Returns:
Example:

2. stop_process

Stop a background process gracefully or forcefully. Parameters:
  • pid (int, required): Process ID to stop
  • force (bool, optional): Force kill without graceful shutdown
Returns:

3. list_processes

List all managed background processes. Returns:

4. get_process_logs

Get output logs from a background process. Parameters:
  • pid (int, required): Process ID
  • lines (int, optional): Number of lines to return (default: 50)
Returns:

5. cleanup_all_processes

Stop all managed background processes. Returns:

Error Detection Patterns

Port Conflicts

Missing Dependencies

Compilation Errors

Permission Issues


Usage Examples

Example 1: Start Development Server

Example 2: Run Build Command

Example 3: Manage Background Processes

Example 4: Port Conflict Handling


Testing Requirements

Unit Tests

File: tests/agents/code/test_cli_tools.py

Dependencies

Required Packages

Import Dependencies


Platform-Specific Implementation

Windows Process Management

Unix Process Management


Performance Characteristics

  • Command Execution: ~100ms overhead
  • Background Startup: 5s default monitoring period
  • Process Cleanup: 5s graceful, immediate for force
  • Log Reading: O(n) for n lines
  • Output Truncation: 10,000 characters max per stream

Security Considerations

Warning: This mixin provides unrestricted command execution.
  • Designed for trusted developer tools only
  • No sandboxing or command validation
  • Full shell access with shell=True
  • Can modify filesystem and network
  • Can spawn additional processes
Use Cases:
  • Local development environments
  • Trusted CI/CD pipelines
  • Developer productivity tools
Not Suitable For:
  • User-facing applications
  • Untrusted input
  • Production services

CLIToolsMixin Technical Specification