Component: CLIToolsMixin
Module:
gaia_agent_code.tools.cli_tools
Import: from gaia_agent_code.tools.cli_tools import CLIToolsMixinOverview
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
-
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
-
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
-
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
- Scan output for common error patterns:
-
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
-
Process Lifecycle
- Start processes with proper isolation (process groups/sessions)
- Monitor startup with configurable timeout
- Graceful shutdown with escalating signals:
- SIGINT (Ctrl+C)
- SIGTERM
- SIGKILL (force)
- Cleanup on agent destruction
Non-Functional Requirements
-
Platform Compatibility
- Windows (CREATE_NEW_PROCESS_GROUP)
- Unix-like systems (start_new_session)
- Handle platform-specific signal handling
-
Performance
- Real-time output streaming
- Efficient log file management
- Minimal overhead for monitoring
-
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 executeworking_dir(str, optional): Directory to execute inbackground(bool, optional): Run as background processtimeout(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 toauto_respond(str, optional): Response for interactive prompts (default: “y\n”)
2. stop_process
Stop a background process gracefully or forcefully. Parameters:pid(int, required): Process ID to stopforce(bool, optional): Force kill without graceful shutdown
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 IDlines(int, optional): Number of lines to return (default: 50)
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
- Local development environments
- Trusted CI/CD pipelines
- Developer productivity tools
- User-facing applications
- Untrusted input
- Production services
CLIToolsMixin Technical Specification