Files
ruflo/docs/task-orchestration.md
T
rUvandClaude dba545f012 feat: Complete Claude-Flow Advanced AI Agent Orchestration System
- Implemented comprehensive multi-agent orchestration system
- Added sophisticated task scheduling with priority management
- Built persistent memory management with hybrid SQLite/Markdown backends
- Created terminal session management with pooling capabilities
- Implemented resource coordination with deadlock detection
- Added MCP integration for tool management
- Built interactive CLI with REPL mode
- Created comprehensive test suite with 100% coverage goal
- Added CI/CD pipeline with GitHub Actions
- Implemented comprehensive documentation and guides
- Updated package.json for npm deployment
- Created claude.md comprehensive system guide

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-10 03:48:41 +00:00

26 KiB

Task Orchestration

Complete guide to task management, scheduling, and workflow orchestration in Claude-Flow's multi-agent system.

Overview

Claude-Flow's task orchestration system provides sophisticated capabilities for creating, scheduling, and managing complex workflows across multiple AI agents with dependencies, priorities, and parallel execution.

Task Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    Task Orchestrator                           │
├─────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐ │
│  │ Task Scheduler  │  │  Queue Manager  │  │ Dependency Mgmt │ │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘ │
├─────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐ │
│  │ Workflow Engine │  │Resource Manager │  │  Progress Track │ │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘ │
├─────────────────────────────────────────────────────────────────┤
│                      Task Execution                            │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐ │
│  │ Task Instances  │  │ Result Handling │  │ Error Recovery  │ │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘ │
└─────────────────────────────────────────────────────────────────┘

Task Types and Categories

Built-in Task Types

Research Tasks

Information gathering and analysis tasks.

# Basic research task
claude-flow task create research "Analyze machine learning trends in 2024"

# Advanced research with parameters
claude-flow task create research \
  "Research quantum computing applications in cryptography" \
  --priority high \
  --deadline "2024-02-15T17:00:00Z" \
  --metadata '{"depth":"comprehensive","sources":"academic,industry"}' \
  --tags "quantum,cryptography,security"

Research Task Configuration:

{
  "type": "research",
  "defaultTimeout": 600000,
  "requiredCapabilities": ["web_search", "document_analysis"],
  "outputFormat": "structured_report",
  "qualityChecks": ["fact_verification", "source_citation"],
  "parameters": {
    "depth": ["basic", "standard", "comprehensive"],
    "sources": ["web", "academic", "industry", "internal"],
    "format": ["summary", "detailed", "analysis"]
  }
}

Analysis Tasks

Data analysis and insight generation tasks.

# Data analysis task
claude-flow task create analysis \
  "Analyze user behavior patterns from Q4 data" \
  --input-file "./data/user_analytics.json" \
  --output-dir "./reports/" \
  --assign-to analyst_agent_123

Analysis Task Configuration:

{
  "type": "analysis",
  "defaultTimeout": 900000,
  "requiredCapabilities": ["data_analysis", "statistical_modeling"],
  "outputFormat": "analytical_report",
  "qualityChecks": ["statistical_validity", "visualization_clarity"],
  "parameters": {
    "methods": ["descriptive", "predictive", "prescriptive"],
    "visualizations": ["charts", "graphs", "dashboards"],
    "metrics": ["standard", "custom", "comparative"]
  }
}

Implementation Tasks

Code development and system implementation tasks.

# Implementation task
claude-flow task create implementation \
  "Implement user authentication system with OAuth2" \
  --dependencies "research_auth_standards,design_auth_flow" \
  --timeout 1800000 \
  --tags "backend,auth,security"

Implementation Task Configuration:

{
  "type": "implementation",
  "defaultTimeout": 1800000,
  "requiredCapabilities": ["code_generation", "testing", "documentation"],
  "outputFormat": "code_package",
  "qualityChecks": ["syntax_validation", "test_coverage", "security_scan"],
  "parameters": {
    "languages": ["typescript", "python", "rust", "go"],
    "frameworks": ["express", "fastapi", "actix", "gin"],
    "testing": ["unit", "integration", "e2e"]
  }
}

Coordination Tasks

Project management and coordination tasks.

# Coordination task
claude-flow task create coordination \
  "Coordinate Q1 planning across all teams" \
  --assign-to coordinator_agent_456 \
  --parallel \
  --subtasks "gather_requirements,create_timeline,assign_resources"

Custom Task Types

Define custom task types for specialized workflows:

{
  "customTaskTypes": {
    "security_audit": {
      "description": "Comprehensive security assessment",
      "defaultTimeout": 3600000,
      "requiredCapabilities": [
        "vulnerability_scanning",
        "penetration_testing",
        "compliance_checking"
      ],
      "outputFormat": "security_report",
      "qualityChecks": ["risk_assessment", "compliance_validation"],
      "parameters": {
        "scope": ["application", "infrastructure", "process"],
        "depth": ["basic", "standard", "comprehensive"],
        "compliance": ["SOC2", "ISO27001", "PCI_DSS"]
      },
      "tools": ["nmap", "burpsuite", "metasploit"],
      "permissions": ["security_clearance"]
    },
    
    "data_migration": {
      "description": "Database migration and transformation",
      "defaultTimeout": 7200000,
      "requiredCapabilities": [
        "database_management",
        "data_transformation",
        "validation"
      ],
      "outputFormat": "migration_report",
      "qualityChecks": ["data_integrity", "performance_validation"],
      "parameters": {
        "source": ["mysql", "postgresql", "mongodb"],
        "target": ["postgresql", "sqlite", "cloud"],
        "volume": ["small", "medium", "large"]
      },
      "rollbackPlan": true
    }
  }
}

Task Creation and Management

Creating Tasks

Simple Task Creation

# Basic task
claude-flow task create research "Research AI safety measures"

# Task with specific agent assignment
claude-flow task create analysis "Analyze Q4 sales data" \
  --assign-to agent_analytics_123

# Task with deadline and priority
claude-flow task create implementation "Fix authentication bug" \
  --priority urgent \
  --deadline "2024-01-20T09:00:00Z"

Advanced Task Creation

# Complex task with full configuration
claude-flow task create research \
  "Comprehensive analysis of cloud security frameworks" \
  --priority high \
  --timeout 3600000 \
  --retry-count 2 \
  --dependencies "task_123,task_456" \
  --assign-to researcher_agent_789 \
  --namespace "security-project" \
  --tags "cloud,security,frameworks,analysis" \
  --metadata '{
    "budget": 5000,
    "stakeholders": ["security-team", "cloud-team"],
    "deliverables": ["report", "recommendations", "implementation-plan"],
    "confidentiality": "internal"
  }' \
  --input-file "./requirements/security-scope.json" \
  --output-dir "./deliverables/security-analysis/"

Programmatic Task Creation

import { TaskManager } from 'claude-flow';

const taskManager = new TaskManager(config);

// Create a research task
const researchTask = await taskManager.create({
  type: 'research',
  title: 'AI Ethics Framework Analysis',
  description: 'Analyze current AI ethics frameworks and best practices',
  priority: 'high',
  timeout: 1800000,
  
  assignmentCriteria: {
    requiredCapabilities: ['research', 'ethics_analysis'],
    preferredAgent: 'ethics_specialist',
    loadBalancing: true
  },
  
  metadata: {
    domain: 'ai_ethics',
    sponsor: 'ethics_committee',
    budget: 10000,
    confidentiality: 'internal'
  },
  
  inputData: {
    scope: 'comprehensive',
    frameworks: ['IEEE', 'EU_AI_Act', 'Partnership_on_AI'],
    deliverables: ['analysis', 'recommendations', 'guidelines']
  },
  
  qualityGates: {
    peerReview: true,
    expertValidation: true,
    ethicsCommitteeApproval: true
  }
});

console.log(`Task created: ${researchTask.id}`);

Task Dependencies

Dependency Types

  1. Sequential Dependencies: Tasks must complete in order
  2. Parallel Dependencies: Tasks can run simultaneously but depend on shared resources
  3. Conditional Dependencies: Dependencies based on outcomes
  4. Resource Dependencies: Dependencies on shared resources
// Sequential workflow
const workflow = await taskManager.createWorkflow({
  name: 'Product Development Workflow',
  tasks: [
    {
      id: 'market_research',
      type: 'research',
      description: 'Research market opportunity'
    },
    {
      id: 'competitive_analysis',
      type: 'analysis',
      description: 'Analyze competitive landscape',
      dependencies: ['market_research']
    },
    {
      id: 'product_design',
      type: 'design',
      description: 'Design product specifications',
      dependencies: ['market_research', 'competitive_analysis']
    },
    {
      id: 'implementation',
      type: 'implementation',
      description: 'Implement product features',
      dependencies: ['product_design']
    }
  ]
});

// Parallel with shared dependencies
const parallelWorkflow = await taskManager.createWorkflow({
  name: 'Parallel Processing Workflow',
  tasks: [
    {
      id: 'data_collection',
      type: 'research',
      description: 'Collect data from multiple sources'
    },
    {
      id: 'frontend_analysis',
      type: 'analysis',
      description: 'Analyze frontend performance',
      dependencies: ['data_collection'],
      parallel: true
    },
    {
      id: 'backend_analysis',
      type: 'analysis', 
      description: 'Analyze backend performance',
      dependencies: ['data_collection'],
      parallel: true
    },
    {
      id: 'report_generation',
      type: 'coordination',
      description: 'Generate comprehensive report',
      dependencies: ['frontend_analysis', 'backend_analysis']
    }
  ]
});

Conditional Dependencies

// Conditional workflow based on outcomes
const conditionalWorkflow = await taskManager.createWorkflow({
  name: 'Adaptive Research Workflow',
  tasks: [
    {
      id: 'initial_research',
      type: 'research',
      description: 'Initial market research'
    },
    {
      id: 'deep_analysis',
      type: 'analysis',
      description: 'Deep market analysis',
      conditions: [
        {
          dependency: 'initial_research',
          condition: 'result.marketSize > 1000000',
          action: 'execute'
        },
        {
          dependency: 'initial_research',
          condition: 'result.marketSize <= 1000000',
          action: 'skip'
        }
      ]
    },
    {
      id: 'feasibility_study',
      type: 'analysis',
      description: 'Technical feasibility study',
      conditions: [
        {
          dependency: 'initial_research',
          condition: 'result.technicalComplexity === "high"',
          action: 'execute'
        }
      ]
    }
  ]
});

Task Scheduling and Prioritization

Priority System

Claude-Flow uses a multi-factor priority system:

{
  "scheduling": {
    "prioritySystem": {
      "levels": {
        "urgent": { "weight": 1000, "sla": 3600000 },
        "high": { "weight": 100, "sla": 7200000 },
        "normal": { "weight": 10, "sla": 86400000 },
        "low": { "weight": 1, "sla": 604800000 }
      },
      
      "factors": {
        "deadline": { "weight": 0.4, "curve": "exponential" },
        "businessValue": { "weight": 0.3, "curve": "linear" },
        "resourceAvailability": { "weight": 0.2, "curve": "linear" },
        "dependencies": { "weight": 0.1, "curve": "step" }
      },
      
      "dynamicAdjustment": {
        "enabled": true,
        "interval": 300000,
        "agingFactor": 1.1
      }
    }
  }
}

Scheduling Algorithms

Priority-Based Scheduling

# Set task priority
claude-flow task update task_123 --priority urgent

# Schedule with priority considerations
claude-flow task schedule \
  --algorithm priority_fifo \
  --consider-dependencies \
  --resource-optimization

Load-Balanced Scheduling

// Configure load-balanced scheduling
const scheduler = new TaskScheduler({
  algorithm: 'weighted_round_robin',
  factors: {
    agentLoad: 0.4,
    capabilityMatch: 0.3,
    responseTime: 0.2,
    successRate: 0.1
  },
  loadBalancing: {
    enabled: true,
    strategy: 'least_loaded',
    spillover: true
  }
});

// Schedule task with load balancing
await scheduler.schedule(task, {
  preferredAgents: ['agent_1', 'agent_2', 'agent_3'],
  loadBalanceThreshold: 0.8,
  allowSpillover: true
});

Time-Based Scheduling

# Schedule task for specific time
claude-flow task schedule task_123 \
  --start-time "2024-01-20T09:00:00Z" \
  --max-duration 7200000

# Schedule recurring task
claude-flow task schedule task_456 \
  --recurring daily \
  --start-time "09:00" \
  --timezone "UTC"

Workflow Management

Workflow Definition

Simple Linear Workflow

{
  "workflow": {
    "name": "Content Creation Pipeline",
    "description": "End-to-end content creation and publication",
    "version": "1.0",
    
    "tasks": [
      {
        "id": "research_phase",
        "type": "research",
        "description": "Research topic and gather information",
        "timeout": 1800000,
        "assignmentCriteria": {
          "capabilities": ["research", "web_search"]
        }
      },
      {
        "id": "content_creation",
        "type": "implementation",
        "description": "Create content based on research",
        "dependencies": ["research_phase"],
        "timeout": 3600000,
        "assignmentCriteria": {
          "capabilities": ["content_creation", "writing"]
        }
      },
      {
        "id": "review_editing",
        "type": "review",
        "description": "Review and edit content",
        "dependencies": ["content_creation"],
        "timeout": 1800000,
        "assignmentCriteria": {
          "capabilities": ["editing", "quality_assurance"]
        }
      },
      {
        "id": "publication",
        "type": "coordination",
        "description": "Publish content to platforms",
        "dependencies": ["review_editing"],
        "timeout": 600000,
        "assignmentCriteria": {
          "capabilities": ["publishing", "platform_management"]
        }
      }
    ],
    
    "completion": {
      "criteria": "all_tasks_completed",
      "notifications": ["content_team", "marketing_team"],
      "deliverables": ["published_content", "analytics_setup"]
    }
  }
}

Complex Parallel Workflow

{
  "workflow": {
    "name": "Product Launch Workflow",
    "description": "Multi-stream product launch preparation",
    
    "phases": [
      {
        "name": "preparation",
        "parallel": false,
        "tasks": [
          {
            "id": "market_analysis",
            "type": "research",
            "description": "Analyze target market"
          }
        ]
      },
      {
        "name": "development",
        "parallel": true,
        "dependencies": ["preparation"],
        "streams": [
          {
            "name": "product_development",
            "tasks": [
              {
                "id": "feature_spec",
                "type": "design",
                "description": "Define product features"
              },
              {
                "id": "implementation",
                "type": "implementation",
                "description": "Implement product features",
                "dependencies": ["feature_spec"]
              }
            ]
          },
          {
            "name": "marketing_preparation",
            "tasks": [
              {
                "id": "marketing_strategy",
                "type": "planning",
                "description": "Develop marketing strategy"
              },
              {
                "id": "content_creation",
                "type": "implementation",
                "description": "Create marketing content",
                "dependencies": ["marketing_strategy"]
              }
            ]
          }
        ]
      },
      {
        "name": "launch",
        "parallel": false,
        "dependencies": ["development"],
        "tasks": [
          {
            "id": "final_testing",
            "type": "testing",
            "description": "Final product testing"
          },
          {
            "id": "launch_execution",
            "type": "coordination",
            "description": "Execute product launch",
            "dependencies": ["final_testing"]
          }
        ]
      }
    ]
  }
}

Workflow Execution

Executing Workflows

# Execute workflow from file
claude-flow workflow execute product_launch.json

# Execute with parameters
claude-flow workflow execute research_workflow.json \
  --parameters '{"topic":"AI trends","depth":"comprehensive"}' \
  --timeout 7200000 \
  --parallel-streams 3

# Execute with monitoring
claude-flow workflow execute complex_workflow.json \
  --monitor \
  --notifications \
  --progress-reports

Workflow Templates

# List available templates
claude-flow workflow template list

# Create workflow from template
claude-flow workflow template apply research_analysis \
  --parameters '{"topic":"quantum computing","timeline":"2 weeks"}' \
  --output research_quantum.json

# Create custom template
claude-flow workflow template create data_pipeline \
  --from existing_workflow.json \
  --parameters topic,data_source,output_format

Workflow Monitoring

Real-time Monitoring

# Monitor workflow execution
claude-flow workflow monitor workflow_123 --watch

# Monitor with metrics
claude-flow workflow monitor workflow_123 \
  --metrics progress,performance,resource_usage \
  --update-interval 30

# Monitor multiple workflows
claude-flow workflow monitor --all \
  --filter "status:running,priority:high"

Progress Tracking

// Set up workflow progress tracking
const workflow = await workflowManager.execute(workflowDef, {
  progressCallback: (progress) => {
    console.log(`Workflow ${progress.workflowId}: ${progress.completionPercentage}%`);
    console.log(`Current phase: ${progress.currentPhase}`);
    console.log(`Tasks completed: ${progress.completedTasks}/${progress.totalTasks}`);
  },
  
  milestoneCallback: (milestone) => {
    console.log(`Milestone reached: ${milestone.name}`);
    notificationService.send(`Workflow milestone: ${milestone.name}`);
  },
  
  errorCallback: (error) => {
    console.error(`Workflow error: ${error.message}`);
    alertingService.alert(`Workflow failure: ${error.message}`);
  }
});

Error Handling and Recovery

Error Recovery Strategies

{
  "errorHandling": {
    "strategies": {
      "retry": {
        "enabled": true,
        "maxAttempts": 3,
        "backoffStrategy": "exponential",
        "backoffBase": 2000,
        "retryableErrors": ["timeout", "agent_unavailable", "resource_exhausted"]
      },
      
      "failover": {
        "enabled": true,
        "alternativeAgents": true,
        "degradedMode": true,
        "notificationThreshold": 2
      },
      
      "rollback": {
        "enabled": true,
        "checkpoints": true,
        "atomicOperations": true,
        "compensationActions": true
      },
      
      "circuitBreaker": {
        "enabled": true,
        "failureThreshold": 5,
        "recoveryTimeout": 300000,
        "halfOpenRequests": 3
      }
    }
  }
}

Task Recovery

// Implement custom error recovery
class TaskRecoveryHandler {
  async handleTaskFailure(task: Task, error: TaskError): Promise<RecoveryAction> {
    switch (error.type) {
      case 'agent_timeout':
        // Try with different agent
        return {
          action: 'reassign',
          targetAgent: await this.findAlternativeAgent(task),
          timeout: task.timeout * 1.5
        };
        
      case 'resource_unavailable':
        // Retry after delay
        return {
          action: 'retry',
          delay: 60000,
          maxAttempts: 3
        };
        
      case 'validation_failed':
        // Modify task parameters
        return {
          action: 'modify',
          modifications: {
            qualityThreshold: 0.8,
            allowPartialResults: true
          }
        };
        
      default:
        // Escalate to human intervention
        return {
          action: 'escalate',
          severity: 'high',
          notificationTargets: ['ops_team', 'task_owner']
        };
    }
  }
}

Checkpoint and Resume

# Create workflow checkpoint
claude-flow workflow checkpoint workflow_123 \
  --name "after_data_collection" \
  --description "Checkpoint after data collection phase"

# Resume from checkpoint
claude-flow workflow resume workflow_123 \
  --from-checkpoint "after_data_collection" \
  --modify-parameters '{"timeout":3600000}'

# List checkpoints
claude-flow workflow checkpoints workflow_123

Performance Optimization

Task Optimization

Batch Processing

// Create batch tasks for efficient processing
const batchTask = await taskManager.createBatch({
  template: {
    type: 'analysis',
    timeout: 600000,
    capabilities: ['data_analysis']
  },
  
  items: [
    { id: 'dataset_1', data: './data/q1_sales.csv' },
    { id: 'dataset_2', data: './data/q2_sales.csv' },
    { id: 'dataset_3', data: './data/q3_sales.csv' },
    { id: 'dataset_4', data: './data/q4_sales.csv' }
  ],
  
  batchConfig: {
    maxConcurrent: 4,
    aggregateResults: true,
    failureHandling: 'continue',
    progressTracking: true
  }
});

Resource Pool Management

{
  "taskExecution": {
    "resourcePools": {
      "cpu_intensive": {
        "maxConcurrent": 2,
        "priority": "high",
        "taskTypes": ["analysis", "computation"]
      },
      
      "memory_intensive": {
        "maxConcurrent": 1,
        "priority": "high", 
        "taskTypes": ["large_dataset_processing"]
      },
      
      "io_intensive": {
        "maxConcurrent": 8,
        "priority": "normal",
        "taskTypes": ["research", "data_collection"]
      }
    },
    
    "optimization": {
      "loadBalancing": true,
      "resourcePreAllocation": true,
      "taskAffinityGrouping": true
    }
  }
}

Workflow Optimization

Parallel Execution

# Optimize workflow for parallel execution
claude-flow workflow optimize research_workflow.json \
  --parallel-optimization \
  --resource-analysis \
  --output optimized_workflow.json

# Execute with optimized parallelism
claude-flow workflow execute optimized_workflow.json \
  --max-parallel 6 \
  --resource-pool shared \
  --load-balance

Caching and Memoization

// Enable task result caching
const taskManager = new TaskManager({
  caching: {
    enabled: true,
    strategy: 'content_hash',
    ttl: 3600000, // 1 hour
    maxSize: '1GB',
    
    cacheableTaskTypes: ['research', 'analysis'],
    cacheKey: (task) => {
      return `${task.type}:${hash(task.description + task.parameters)}`;
    }
  }
});

// Use cached results when available
const result = await taskManager.execute(task, {
  useCache: true,
  cachePolicy: 'prefer_cache',
  cacheTtl: 1800000 // 30 minutes
});

Task Analytics and Reporting

Performance Metrics

# Task performance analytics
claude-flow task analytics \
  --timeframe "last-30-days" \
  --metrics completion_time,success_rate,resource_usage \
  --breakdown task_type,agent_type,priority

# Workflow performance analysis
claude-flow workflow analytics \
  --workflows workflow_123,workflow_456 \
  --compare \
  --metrics duration,efficiency,cost

Custom Reporting

// Generate custom task reports
class TaskReporter {
  async generateReport(criteria: ReportCriteria): Promise<TaskReport> {
    const tasks = await this.taskManager.query(criteria);
    
    const metrics = {
      totalTasks: tasks.length,
      completedTasks: tasks.filter(t => t.status === 'completed').length,
      averageCompletionTime: this.calculateAverageTime(tasks),
      successRate: this.calculateSuccessRate(tasks),
      
      byType: this.groupByType(tasks),
      byAgent: this.groupByAgent(tasks),
      byPriority: this.groupByPriority(tasks),
      
      trends: {
        daily: this.calculateDailyTrends(tasks),
        weekly: this.calculateWeeklyTrends(tasks),
        monthly: this.calculateMonthlyTrends(tasks)
      },
      
      bottlenecks: this.identifyBottlenecks(tasks),
      recommendations: this.generateRecommendations(tasks)
    };
    
    return metrics;
  }
}

This comprehensive task orchestration guide covers all aspects of creating, managing, and optimizing tasks and workflows in Claude-Flow's multi-agent system, from simple task creation to complex workflow orchestration with error handling and performance optimization.