Files
ruflo/docs/08-terminal-management.md
T
rUv cff0cbd997 Add comprehensive tests and reporting for Claude-Flow task system
- Implemented mock-based and comprehensive tests for batch task processing.
- Created detailed test results documentation including metrics and performance characteristics.
- Added error handling for unexpected arguments in test execution.
- Generated HTML and JSON reports summarizing test outcomes.
- Established integration tests for agent spawning, task assignment, and execution tracking.
- Enhanced event monitoring for task lifecycle and agent performance.
2025-06-10 17:24:18 +00:00

23 KiB

Terminal Management and Session Handling

Claude-Flow's terminal management system provides efficient, pooled terminal sessions with advanced features for command execution, session recycling, and cross-platform compatibility. This guide covers comprehensive terminal management capabilities and best practices.

Terminal Pool Architecture

Pool Management System

The terminal manager maintains a sophisticated pool of reusable terminal sessions to optimize performance and resource usage.

Pool Configuration:

# Configure terminal pool size
claude-flow config set terminal.poolSize 10

# Set recycling threshold
claude-flow config set terminal.recycleAfter 20

# Configure health check interval
claude-flow config set terminal.healthCheckInterval 30000

# Monitor pool status
claude-flow terminal pool status --detailed

# View pool statistics
claude-flow terminal pool stats \
  --metrics "utilization,performance,recycling" \
  --time-range "24h"

Pool Architecture Visualization:

graph TB
    TM[Terminal Manager] --> Pool[Terminal Pool]
    Pool --> Available[Available Terminals]
    Pool --> Active[Active Terminals]
    Pool --> Recycling[Recycling Queue]
    
    Available --> T1[Terminal 1]
    Available --> T2[Terminal 2]
    Available --> T3[Terminal 3]
    
    Active --> A1[Agent Session 1]
    Active --> A2[Agent Session 2]
    Active --> A3[Agent Session 3]
    
    Recycling --> R1[Cleanup Process]
    Recycling --> R2[Health Check]

Terminal Types and Configuration

Supported Terminal Types:

# Auto-detection (recommended)
claude-flow config set terminal.type auto

# Integrated terminals (VS Code, IDEs)
claude-flow config set terminal.type integrated

# External terminal applications
claude-flow config set terminal.type external

# Headless terminals for automation
claude-flow config set terminal.type headless

# Container-based terminals
claude-flow config set terminal.type container

Advanced Terminal Configuration:

{
  "terminal": {
    "type": "auto",
    "poolSize": 10,
    "recycleAfter": 25,
    "healthCheckInterval": 30000,
    "commandTimeout": 300000,
    "maxConcurrentCommands": 5,
    "shellPreference": ["bash", "zsh", "fish", "sh"],
    "environment": {
      "PATH": "/usr/local/bin:/usr/bin:/bin",
      "LANG": "en_US.UTF-8",
      "TERM": "xterm-256color"
    },
    "security": {
      "allowedCommands": ["*"],
      "blockedCommands": ["rm -rf /", "sudo rm", "format"],
      "sandboxed": false,
      "maxExecutionTime": 600000
    },
    "performance": {
      "bufferSize": "64KB",
      "compression": true,
      "keepAlive": true
    },
    "logging": {
      "enabled": true,
      "level": "info",
      "includeOutput": false,
      "maxLogSize": "10MB"
    }
  }
}

Session Management

Creating and Managing Terminal Sessions

Basic Session Creation:

# Create new terminal session
claude-flow terminal create \
  --name "development-session" \
  --shell bash \
  --working-directory "/project"

# Create session with environment variables
claude-flow terminal create \
  --name "node-development" \
  --shell bash \
  --env "NODE_ENV=development,DEBUG=true,PORT=3000" \
  --working-directory "/project/web"

# Create persistent session
claude-flow terminal create \
  --name "persistent-session" \
  --persistent true \
  --auto-restart true \
  --restart-on-failure true

Advanced Session Configuration:

# Create session with resource limits
claude-flow terminal create \
  --name "limited-session" \
  --memory-limit "512MB" \
  --cpu-limit "1.0" \
  --timeout "1h" \
  --max-processes 10

# Create containerized session
claude-flow terminal create \
  --name "docker-session" \
  --type "container" \
  --image "node:18-alpine" \
  --volume "/project:/workspace" \
  --working-directory "/workspace"

# Create SSH session
claude-flow terminal create \
  --name "remote-session" \
  --type "ssh" \
  --host "server.example.com" \
  --user "developer" \
  --key-file "~/.ssh/id_rsa"

Session Control Operations

Session Lifecycle Management:

# List all active sessions
claude-flow terminal list --detailed

# Get comprehensive session information
claude-flow terminal info <session-id> \
  --include-stats true \
  --include-history true

# Attach to running session
claude-flow terminal attach <session-id> --interactive

# Detach from session (keep running)
claude-flow terminal detach <session-id>

# Pause session execution
claude-flow terminal pause <session-id> \
  --reason "maintenance-window"

# Resume paused session
claude-flow terminal resume <session-id>

# Gracefully terminate session
claude-flow terminal terminate <session-id> \
  --graceful true \
  --timeout 30s

Batch Session Operations:

# Create multiple sessions
claude-flow terminal batch-create \
  --config "session-configs.json" \
  --parallel true

# Terminate idle sessions
claude-flow terminal cleanup \
  --idle-longer-than "30m" \
  --exclude-persistent

# Restart all sessions of specific type
claude-flow terminal batch-restart \
  --filter "type:development" \
  --graceful true

Session Configuration Files

session-configs.json:

{
  "sessions": [
    {
      "name": "backend-dev",
      "shell": "bash",
      "workingDirectory": "/project/backend",
      "environment": {
        "NODE_ENV": "development",
        "DATABASE_URL": "postgresql://localhost/app_dev"
      },
      "persistent": true,
      "autoStart": ["npm install", "npm run dev"]
    },
    {
      "name": "frontend-dev",
      "shell": "bash", 
      "workingDirectory": "/project/frontend",
      "environment": {
        "NODE_ENV": "development",
        "REACT_APP_API_URL": "http://localhost:3001"
      },
      "persistent": true,
      "autoStart": ["npm install", "npm start"]
    },
    {
      "name": "testing",
      "shell": "bash",
      "workingDirectory": "/project",
      "environment": {
        "NODE_ENV": "test"
      },
      "persistent": false,
      "commands": ["npm run test:watch"]
    }
  ]
}

Command Execution

Basic Command Execution

Single Command Execution:

# Execute single command
claude-flow terminal exec "ls -la" \
  --session <session-id> \
  --timeout 30s

# Execute with environment override
claude-flow terminal exec "npm test" \
  --session <session-id> \
  --env "NODE_ENV=test,CI=true" \
  --timeout 300s

# Execute in background
claude-flow terminal exec "npm run build" \
  --session <session-id> \
  --background true \
  --log-output "build.log"

Command with Input/Output Handling:

# Execute command with input file
claude-flow terminal exec "python script.py" \
  --session <session-id> \
  --input-file "input-data.txt" \
  --output-file "results.txt" \
  --error-file "errors.txt"

# Execute with streaming output
claude-flow terminal exec "docker build ." \
  --session <session-id> \
  --stream-output true \
  --real-time true

Batch Command Execution

Sequential Command Execution:

# Execute commands in sequence
claude-flow terminal batch-exec \
  --session <session-id> \
  --commands "cd /project,npm install,npm test,npm run build" \
  --stop-on-error true \
  --log-each-command

# Execute from command file
claude-flow terminal batch-exec \
  --session <session-id> \
  --file "deployment-commands.txt" \
  --variables "ENV=production,VERSION=2.1.0"

Parallel Command Execution:

# Execute commands in parallel
claude-flow terminal parallel-exec \
  --session <session-id> \
  --commands "npm run lint,npm run test,npm run build" \
  --max-parallel 3 \
  --collect-results true

# Parallel execution across sessions
claude-flow terminal multi-session-exec \
  --sessions "backend,frontend,testing" \
  --command "git pull origin main" \
  --parallel true

deployment-commands.txt:

# Deployment script with variables
echo "Deploying version ${VERSION} to ${ENV}"
git fetch origin
git checkout main
git pull origin main
npm install --production
npm run build
docker build -t app:${VERSION} .
docker tag app:${VERSION} registry.example.com/app:${VERSION}
docker push registry.example.com/app:${VERSION}
kubectl set image deployment/app app=registry.example.com/app:${VERSION}
kubectl rollout status deployment/app
echo "Deployment complete"

Interactive Command Execution

Interactive Session Management:

# Start interactive Python session
claude-flow terminal exec-interactive "python3" \
  --session <session-id> \
  --input-file "python-commands.txt" \
  --expect-prompts ">>>"

# Interactive database session
claude-flow terminal exec-interactive "psql -h localhost -d app" \
  --session <session-id> \
  --input-file "sql-commands.sql" \
  --expect-prompts "app=#"

# Interactive shell with command history
claude-flow terminal shell <session-id> \
  --interactive true \
  --history-file ".terminal-history" \
  --completion true

python-commands.txt:

import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
print(df.head())
result = df.groupby('category').sum()
print(result)
result.to_csv('output.csv')
exit()

Multi-Terminal Orchestration

Multi-Terminal Configuration

Creating Multi-Terminal Workflows:

# Create multi-terminal configuration
claude-flow terminal multi-config create \
  --name "fullstack-development" \
  --config "multi-terminal-config.json"

# Launch complete development environment
claude-flow terminal multi-launch "fullstack-development" \
  --start-order "sequential" \
  --monitor true

multi-terminal-config.json:

{
  "name": "fullstack-development",
  "description": "Complete full-stack development environment",
  "version": "2.0",
  "terminals": [
    {
      "name": "database",
      "shell": "bash",
      "workingDirectory": "/project",
      "environment": {
        "POSTGRES_USER": "dev",
        "POSTGRES_PASSWORD": "dev",
        "POSTGRES_DB": "app_dev"
      },
      "commands": [
        "docker-compose up -d postgres",
        "sleep 5",
        "docker-compose logs -f postgres"
      ],
      "persistent": true,
      "autoStart": true,
      "healthCheck": {
        "command": "pg_isready -h localhost -p 5432",
        "interval": "10s",
        "retries": 30
      }
    },
    {
      "name": "backend-api",
      "shell": "bash",
      "workingDirectory": "/project/backend",
      "environment": {
        "NODE_ENV": "development",
        "DATABASE_URL": "postgresql://dev:dev@localhost:5432/app_dev",
        "JWT_SECRET": "dev-secret",
        "PORT": "3001"
      },
      "commands": [
        "npm install",
        "npm run migrate",
        "npm run seed",
        "npm run dev"
      ],
      "dependencies": ["database"],
      "persistent": true,
      "healthCheck": {
        "command": "curl -f http://localhost:3001/health",
        "interval": "15s",
        "retries": 20
      }
    },
    {
      "name": "frontend-app",
      "shell": "bash",
      "workingDirectory": "/project/frontend",
      "environment": {
        "NODE_ENV": "development",
        "REACT_APP_API_URL": "http://localhost:3001",
        "REACT_APP_ENV": "development",
        "PORT": "3000"
      },
      "commands": [
        "npm install",
        "npm start"
      ],
      "dependencies": ["backend-api"],
      "persistent": true,
      "healthCheck": {
        "command": "curl -f http://localhost:3000",
        "interval": "20s",
        "retries": 15
      }
    },
    {
      "name": "testing-runner",
      "shell": "bash",
      "workingDirectory": "/project",
      "environment": {
        "NODE_ENV": "test",
        "CI": "false"
      },
      "commands": [
        "npm run test:install",
        "npm run test:watch"
      ],
      "dependencies": ["backend-api"],
      "autoStart": false,
      "persistent": false
    },
    {
      "name": "log-aggregator",
      "shell": "bash",
      "workingDirectory": "/project",
      "commands": [
        "tail -f backend/logs/app.log frontend/logs/app.log | grep -E 'ERROR|WARN'"
      ],
      "persistent": true,
      "autoStart": false
    }
  ],
  "coordination": {
    "startupOrder": ["database", "backend-api", "frontend-app"],
    "shutdownOrder": ["frontend-app", "backend-api", "database"],
    "dependencies": {
      "backend-api": ["database"],
      "frontend-app": ["backend-api"],
      "testing-runner": ["backend-api"]
    },
    "healthChecks": {
      "enabled": true,
      "timeout": "5m",
      "failureAction": "restart"
    }
  },
  "monitoring": {
    "dashboard": true,
    "logAggregation": true,
    "performanceMetrics": ["cpu", "memory", "response-time"],
    "alerts": {
      "healthCheckFailure": true,
      "resourceThreshold": "memory > 80%"
    }
  }
}

Terminal Coordination

Coordinated Operations:

# Start terminals in dependency order
claude-flow terminal multi-start "fullstack-development" \
  --respect-dependencies true \
  --wait-for-health-checks true \
  --timeout 10m

# Monitor all terminals with dashboard
claude-flow terminal multi-monitor "fullstack-development" \
  --dashboard true \
  --refresh-rate 2s \
  --show-logs true

# Send command to multiple terminals
claude-flow terminal multi-exec "fullstack-development" \
  --command "git status" \
  --terminals "backend-api,frontend-app" \
  --parallel true

# Graceful shutdown of entire environment
claude-flow terminal multi-shutdown "fullstack-development" \
  --graceful true \
  --save-state true \
  --cleanup true

Dynamic Terminal Management:

# Add terminal to running configuration
claude-flow terminal multi-add "fullstack-development" \
  --terminal-config "new-service.json" \
  --start-immediately false

# Remove terminal from configuration
claude-flow terminal multi-remove "fullstack-development" \
  --terminal "testing-runner" \
  --graceful-shutdown true

# Scale terminal instances
claude-flow terminal multi-scale "fullstack-development" \
  --terminal "worker-processes" \
  --instances 5 \
  --load-balance true

Advanced Terminal Features

Terminal Recording and Playback

Session Recording:

# Start recording terminal session
claude-flow terminal record <session-id> \
  --output "session-recording-$(date +%Y%m%d-%H%M).json" \
  --include-timing true \
  --include-environment true \
  --compress true

# Record with filters
claude-flow terminal record <session-id> \
  --output "filtered-recording.json" \
  --exclude-commands "cat,ls,pwd" \
  --mask-secrets true \
  --include-output false

Session Playback:

# Replay terminal session
claude-flow terminal replay "session-recording.json" \
  --session <new-session-id> \
  --speed 2.0 \
  --interactive-pause true

# Replay with modifications
claude-flow terminal replay "session-recording.json" \
  --session <new-session-id> \
  --substitute-vars "OLD_PATH:/project,NEW_PATH:/new-project" \
  --skip-failed-commands

# Export recording to executable script
claude-flow terminal export-script "session-recording.json" \
  --output "replay-script.sh" \
  --include-error-handling \
  --add-logging

Terminal Sharing and Collaboration

Session Sharing:

# Share terminal session with read-only access
claude-flow terminal share <session-id> \
  --access-level "read" \
  --permissions "view-output,view-commands" \
  --expiry "2h" \
  --generate-url

# Share with write access
claude-flow terminal share <session-id> \
  --access-level "write" \
  --permissions "execute-commands,view-output" \
  --collaborators "user1,user2" \
  --session-recording true

# Create collaborative session
claude-flow terminal collaborate \
  --session <session-id> \
  --agents "agent-1,agent-2,agent-3" \
  --coordination-mode "turn-based" \
  --conflict-resolution "last-writer-wins"

Join Shared Sessions:

# Join shared session with token
claude-flow terminal join \
  --token <share-token> \
  --mode "observer"

# Join collaborative session
claude-flow terminal join \
  --session-id <session-id> \
  --collaborator-id "agent-2" \
  --request-control true

Terminal Automation

Automated Terminal Workflows:

# Create automation script
claude-flow terminal automation create \
  --name "deployment-automation" \
  --triggers "git-push:main,schedule:daily-2am" \
  --actions "deployment-actions.json"

# Schedule recurring commands
claude-flow terminal schedule \
  --command "npm run backup-database" \
  --cron "0 2 * * *" \
  --session <session-id> \
  --notification "email:admin@company.com"

# Conditional execution
claude-flow terminal conditional-exec \
  --condition "file-exists:package.json AND git-status:clean" \
  --command "npm audit fix" \
  --session <session-id> \
  --max-attempts 3

deployment-actions.json:

{
  "automation": {
    "name": "deployment-automation",
    "triggers": [
      {
        "type": "git-push",
        "branch": "main",
        "repository": "/project"
      },
      {
        "type": "schedule",
        "cron": "0 2 * * *"
      }
    ],
    "actions": [
      {
        "name": "run-tests",
        "command": "npm run test:ci",
        "timeout": "10m",
        "retry": 2,
        "continue_on_failure": false
      },
      {
        "name": "build-application",
        "command": "npm run build",
        "timeout": "15m",
        "dependencies": ["run-tests"]
      },
      {
        "name": "deploy-staging",
        "command": "npm run deploy:staging",
        "timeout": "5m",
        "dependencies": ["build-application"],
        "approval_required": false
      },
      {
        "name": "deploy-production",
        "command": "npm run deploy:production",
        "timeout": "5m",
        "dependencies": ["deploy-staging"],
        "approval_required": true,
        "approvers": ["admin@company.com"]
      }
    ],
    "notifications": {
      "on_success": ["team@company.com"],
      "on_failure": ["admin@company.com", "team@company.com"],
      "channels": ["email", "slack"]
    }
  }
}

Platform-Specific Features

VS Code Integration

VS Code Terminal Management:

# Launch VS Code with integrated terminals
claude-flow terminal vscode-launch \
  --config "vscode-terminals.json" \
  --workspace "/project" \
  --layout "split-screen"

# Send commands to VS Code terminal
claude-flow terminal vscode-exec \
  --terminal-name "Backend Development" \
  --command "npm run dev" \
  --focus-terminal true

# Create VS Code terminal layout
claude-flow terminal vscode-layout \
  --layout "custom" \
  --terminals "backend:bottom-left,frontend:bottom-right,testing:side-panel"

vscode-terminals.json:

{
  "vscode_terminals": {
    "workspace": "/project",
    "terminals": [
      {
        "name": "Backend Development",
        "shell": "bash",
        "cwd": "${workspaceFolder}/backend",
        "env": {
          "NODE_ENV": "development"
        },
        "commands": ["npm install", "npm run dev"],
        "position": "bottom",
        "group": "development"
      },
      {
        "name": "Frontend Development",
        "shell": "bash",
        "cwd": "${workspaceFolder}/frontend", 
        "env": {
          "NODE_ENV": "development"
        },
        "commands": ["npm install", "npm start"],
        "position": "bottom",
        "group": "development"
      },
      {
        "name": "Testing",
        "shell": "bash",
        "cwd": "${workspaceFolder}",
        "commands": ["npm run test:watch"],
        "position": "right",
        "group": "testing"
      }
    ],
    "layout": {
      "type": "split",
      "orientation": "horizontal",
      "groups": ["development", "testing"]
    }
  }
}

Docker Integration

Container-Based Terminals:

# Create containerized terminal
claude-flow terminal docker-create \
  --image "node:18-alpine" \
  --name "nodejs-dev" \
  --volume "/project:/workspace" \
  --working-directory "/workspace" \
  --env "NODE_ENV=development"

# Execute commands in container
claude-flow terminal docker-exec \
  --container <container-id> \
  --command "npm install && npm test" \
  --user "node" \
  --interactive true

# Multi-container terminal setup
claude-flow terminal docker-compose \
  --file "docker-compose.dev.yml" \
  --services "web,db,redis" \
  --follow-logs true

SSH Terminal Sessions

Remote Terminal Management:

# Create SSH terminal session
claude-flow terminal ssh-create \
  --host "server.example.com" \
  --user "developer" \
  --key-file "~/.ssh/id_rsa" \
  --session-name "remote-development" \
  --port 22

# SSH tunnel for local development
claude-flow terminal ssh-tunnel \
  --local-session <local-session-id> \
  --remote-host "server.example.com" \
  --remote-path "/project" \
  --tunnel-ports "3000:3000,5432:5432"

# Multi-server terminal coordination
claude-flow terminal ssh-multi \
  --servers "web1.example.com,web2.example.com,db.example.com" \
  --user "admin" \
  --command "sudo service nginx reload" \
  --parallel true

Terminal Monitoring and Diagnostics

Performance Monitoring

Real-time Terminal Metrics:

# Monitor terminal performance
claude-flow terminal monitor \
  --session <session-id> \
  --metrics "cpu,memory,io,network" \
  --interval 5s \
  --alert-thresholds "cpu:80%,memory:512MB"

# System-wide terminal monitoring
claude-flow terminal system-monitor \
  --all-sessions true \
  --dashboard true \
  --export-metrics "prometheus" \
  --endpoint "/metrics"

# Performance analysis
claude-flow terminal performance-analysis \
  --session <session-id> \
  --time-range "1h" \
  --report "performance-report.json" \
  --recommendations true

Health Checks and Diagnostics

Terminal Health Management:

# Comprehensive health check
claude-flow terminal health-check \
  --session <session-id> \
  --detailed true \
  --fix-issues true

# System diagnostics
claude-flow terminal diagnose \
  --all-sessions true \
  --check-dependencies true \
  --verify-configuration true

# Network connectivity testing
claude-flow terminal network-test \
  --session <session-id> \
  --targets "google.com,github.com,api.company.com" \
  --protocols "http,https,ssh"

Log Management

Terminal Logging:

# View terminal logs
claude-flow terminal logs <session-id> \
  --tail 100 \
  --follow true \
  --level "info" \
  --format "json"

# Search command history
claude-flow terminal search-history \
  --session <session-id> \
  --pattern "npm.*install" \
  --time-range "7d" \
  --context 2

# Export terminal history
claude-flow terminal export-history \
  --session <session-id> \
  --format "json" \
  --include-output true \
  --output "terminal-history-$(date +%Y%m%d).json"

Debugging and Troubleshooting

Terminal Debugging:

# Debug terminal session
claude-flow terminal debug <session-id> \
  --verbose true \
  --trace-commands true \
  --monitor-resources true

# Session recovery
claude-flow terminal recover <session-id> \
  --backup-file "session-backup.json" \
  --restore-state true \
  --verify-integrity true

# Performance optimization
claude-flow terminal optimize \
  --session <session-id> \
  --cleanup-history true \
  --compress-logs true \
  --rebuild-index true

This comprehensive terminal management guide provides all the tools needed to effectively manage complex terminal workflows, coordinate multi-terminal environments, and maintain high-performance terminal operations in Claude-Flow.