ruvnet / ruvnet/agentic-flow

Enhance Agent Prompts for Concurrent/Parallel Swarm Execution

Open
#43 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
812
Forks
175
Avg merge
2m
Merged PRs (30d)
3

Description

Enhance Agent Prompts for Concurrent/Parallel Swarm Execution

Summary

Improve agent system prompts and instructions to better leverage concurrent/parallel execution patterns in agentic-flow, based on analysis of Claude Agent SDK patterns and existing codebase implementations.

Problem Statement

Currently, agentic-flow has 83 agent definitions in .claude/agents/ and robust infrastructure for parallel execution (QUIC transport, Promise.all patterns, batch operations), but agent prompts don't explicitly guide LLMs to:

  1. Think in parallel - Consider concurrent task decomposition
  2. Use batch operations - Leverage existing parallel APIs
  3. Coordinate efficiently - Minimize sequential dependencies
  4. Spawn subagents - Use CLI subprocesses for true parallelism
  5. Use ReasoningBank - Maintain context across distributed agents

Current State Analysis

✅ What We Have (Infrastructure)

Parallel Execution Infrastructure:

  • /agentic-flow/src/examples/parallel-swarm-deployment.ts - 6 examples of concurrent patterns
  • /agentic-flow/src/mcp/fastmcp/tools/agent/parallel.ts - Parallel mode MCP tool
  • /agentic-flow/src/coordination/parallelSwarm.js - Core parallel coordination
  • /examples/complex-multi-agent-deployment.ts - Multi-agent orchestration
  • /examples/quic-swarm-coordination.js - QUIC transport (50-70% faster)

Batch APIs:

// Available parallel execution functions
deploySwarmConcurrently()    // Deploy swarm + agents in parallel
batchSpawnAgents()           // Spawn multiple agents concurrently
executeTasksConcurrently()   // Parallel task execution
deployAndExecuteConcurrently() // Deploy + execute simultaneously
scaleSwarmConcurrently()     // Dynamic scaling

ReasoningBank Integration:

  • /agentic-flow/src/reasoningbank/ - Persistent memory across agents
  • Pattern storage and retrieval for learning
  • Cross-agent state coordination
  • Memory namespaces for swarm isolation
  • Trajectory tracking for optimization

Performance Gains:

  • 2.8-4.4x speedup via parallel execution
  • 50-70% time reduction with QUIC transport
  • 84.8% SWE-Bench solve rate
  • 32.3% token reduction
❌ What We're Missing (Prompts)

Agent prompts don't teach:

  1. How to identify parallelizable tasks
  2. When to use Promise.all vs sequential execution
  3. Batch operation patterns
  4. CLI subprocess spawning (npx agentic-flow --agent TYPE --task TASK)
  5. ReasoningBank coordination (memory namespaces for distributed state)
  6. QUIC transport benefits for distributed work
  7. Result synthesis patterns (combining subprocess outputs)

Analysis of Claude SDK Patterns

Current Agent Execution Flow

From /agentic-flow/src/agents/claudeAgent.ts:

// Single agent execution - no parallelism guidance
export async function claudeAgent(
  agent: AgentDefinition,
  input: string,
  onStream?: (chunk: string) => void,
  modelOverride?: string
) {
  // Uses Claude Agent SDK query()
  // No subprocess spawning
  // No parallel coordination
  // No ReasoningBank integration in prompts
}

Multi-provider support exists:

  • Anthropic (native tools)
  • OpenRouter/Gemini/DeepSeek (99% cost savings)
  • ONNX (local inference, $0 cost)

Provider-specific instructions (/agentic-flow/src/proxy/provider-instructions.ts):

  • Different models need different prompting strategies
  • XML tags for non-Anthropic models
  • Emphasis and examples vary by model family

But no parallel execution instructions in any provider template!

Parallel Execution Example (From Codebase)

/agentic-flow/src/examples/parallel-swarm-deployment.ts demonstrates the pattern:

// Example 2: Parallel Task Execution
const result = await executeTasksConcurrently({
  tasks: [
    { description: 'Analyze requirements', agentType: 'researcher', priority: 'high' },
    { description: 'Design architecture', agentType: 'architect', priority: 'high' },
    { description: 'Implement API endpoints', agentType: 'coder', priority: 'high' },
    { description: 'Write unit tests', agentType: 'tester', priority: 'medium' },
    { description: 'Create documentation', agentType: 'documenter', priority: 'medium' },
    { description: 'Review code quality', agentType: 'reviewer', priority: 'medium' }
  ],
  strategy: 'parallel',
  maxConcurrency: 6 // All 6 tasks run concurrently
});

// Result: 3-5x speedup vs sequential execution

This pattern works but agents don't know about it!

Proposed Solution

1. Create Parallel Execution Prompt Templates

Location: /agentic-flow/src/prompts/parallel-execution-guide.md

Content structure:

# Concurrent Execution Guide for AI Agents

## Core Principles

### 1. CLI Subprocess Spawning (Native Pattern)

When you need parallel execution, spawn subagents via CLI:

\`\`\`bash
# Spawn 5 parallel research agents
npx agentic-flow --agent researcher --task "Analyze security patterns"
npx agentic-flow --agent researcher --task "Analyze performance patterns"
npx agentic-flow --agent researcher --task "Analyze scalability patterns"
npx agentic-flow --agent coder --task "Implement core features"
npx agentic-flow --agent tester --task "Create comprehensive tests"
\`\`\`

**In JavaScript/TypeScript:**
\`\`\`typescript
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);

// Spawn 5 parallel subagents
const results = await Promise.all([
  execAsync('npx agentic-flow --agent researcher --task "domain1"'),
  execAsync('npx agentic-flow --agent researcher --task "domain2"'),
  execAsync('npx agentic-flow --agent researcher --task "domain3"'),
  execAsync('npx agentic-flow --agent coder --task "implement"'),
  execAsync('npx agentic-flow --agent tester --task "test"')
]);
\`\`\`

### 2. ReasoningBank Coordination

Each subagent stores results in ReasoningBank for cross-process coordination:

\`\`\`typescript
// Subagent stores its findings
await reasoningBank.storePattern({
  sessionId: 'swarm-task-123',
  task: 'Analyze security patterns',
  output: findings,
  reward: 0.95,
  success: true
});

// Parent agent retrieves all results
const allFindings = await reasoningBank.searchPatterns('swarm-task-123', { k: 10 });

// Synthesize final report
const report = synthesizeResults(allFindings);
\`\`\`

**Memory namespace pattern:**
- `swarm/{TASK_ID}/{AGENT_ID}` - Individual agent results
- `swarm/{TASK_ID}/synthesis` - Combined report
- `swarm/{TASK_ID}/metadata` - Execution metrics

### 3. Think in Parallel First

Before executing tasks, ask:
- Which tasks have NO dependencies?
- Which operations can run concurrently?
- Can I spawn subagents via CLI?
- Should I use Promise.all?

### 4. Use Batch APIs

When spawning agents programmatically:
\`\`\`typescript
import { batchSpawnAgents, executeTasksConcurrently } from 'agentic-flow';

// Batch spawn multiple agents
const agents = await batchSpawnAgents([
  { type: 'researcher', count: 3, capabilities: ['search', 'analyze'] },
  { type: 'coder', count: 2, capabilities: ['implement', 'refactor'] },
  { type: 'tester', count: 2, capabilities: ['test', 'validate'] }
]);

// Execute tasks concurrently
const results = await executeTasksConcurrently({
  tasks: [...],
  strategy: 'parallel',
  maxConcurrency: 7
});
\`\`\`

## Complete Example: Parallel Code Review

**Scenario:** Review 1000 files across a large codebase

\`\`\`typescript
// 1. Decompose task into parallel subtasks
const fileBatches = chunkArray(files, 200); // 5 batches of 200 files

// 2. Spawn 5 reviewer agents via CLI (parallel subprocesses)
const reviewPromises = fileBatches.map((batch, i) => 
  execAsync(\`npx agentic-flow --agent code-reviewer --task "Review batch ${i}: ${batch.join(',')}" --output reasoningbank:swarm-review/batch-${i}\`)
);

// 3. Wait for all reviews to complete
await Promise.all(reviewPromises);

// 4. Retrieve all results from ReasoningBank
const allReviews = await Promise.all(
  fileBatches.map((_, i) => 
    reasoningBank.retrieve('swarm-review/batch-' + i)
  )
);

// 5. Synthesize final report
const report = {
  totalFiles: files.length,
  criticalIssues: allReviews.flatMap(r => r.critical),
  warnings: allReviews.flatMap(r => r.warnings),
  suggestions: allReviews.flatMap(r => r.suggestions),
  executionTime: Date.now() - startTime,
  speedup: calculateSpeedup(sequentialEstimate, actualTime)
};

// 6. Store successful pattern for learning
await reasoningBank.storePattern({
  sessionId: 'parallel-code-review-pattern',
  task: 'Large-scale code review with 5 parallel agents',
  output: JSON.stringify(report),
  reward: report.speedup / 5, // Normalize by expected speedup
  success: true
});
\`\`\`

**Result:** 50-70% faster than sequential review (QUIC transport + parallel execution)

## Decision Tree

\`\`\`
Task received
├─ Complexity > 3 subtasks?
│  ├─ YES: Parallel execution recommended
│  │  ├─ Check dependencies
│  │  │  ├─ No dependencies → Spawn ALL subagents in parallel
│  │  │  │  └─ Use: Promise.all([exec(...), exec(...), ...])
│  │  │  └─ Has dependencies → Pipeline with parallel stages
│  │  │     └─ Use: Sequential Promise.all blocks
│  │  └─ Configure ReasoningBank coordination
│  │     ├─ Define memory namespace: swarm/{TASK_ID}
│  │     ├─ Each subagent stores results
│  │     └─ Parent retrieves + synthesizes
│  └─ NO: Single agent execution
│     └─ Use: npx agentic-flow --agent TYPE --task TASK
\`\`\`

## Performance Patterns

### 1. Large-scale operations (100+ files/tasks)
\`\`\`typescript
// QUIC transport for distributed coordination
const coordinator = new QuicTransport({
  host: 'localhost',
  port: 4433,
  maxConcurrentStreams: 100 // 100 parallel agents
});

// Spawn agents with QUIC coordination
const agents = await Promise.all(
  Array.from({ length: 10 }, (_, i) =>
    execAsync(\`npx agentic-flow --agent worker --task "batch-${i}" --transport quic\`)
  )
);
\`\`\`

**Benefits:**
- 50-70% faster than TCP
- 0-RTT reconnection (instant)
- Stream multiplexing (no head-of-line blocking)

### 2. Multi-agent coordination with memory
\`\`\`typescript
// Shared memory namespace for coordination
const namespace = 'swarm-task-456';

// Each agent reads shared context
const sharedContext = await reasoningBank.retrieve(\`${namespace}/context\`);

// Agents store individual results
await reasoningBank.store(\`${namespace}/agent-${agentId}\`, results);

// Parent synthesizes all results
const allResults = await reasoningBank.searchPatterns(namespace);
\`\`\`

### 3. Dynamic scaling
\`\`\`typescript
// Start with 3 agents
let activeAgents = 3;

// Monitor workload
if (taskQueue.length > threshold) {
  // Scale up to 8 agents
  await scaleSwarmConcurrently(activeAgents, 8, 'worker');
  activeAgents = 8;
}

// Scale down when idle
if (taskQueue.length < lowThreshold) {
  await scaleSwarmConcurrently(activeAgents, 3, 'worker');
  activeAgents = 3;
}
\`\`\`

## Error Handling

\`\`\`typescript
// Graceful failure handling for parallel execution
try {
  const results = await Promise.allSettled([
    execAsync('npx agentic-flow --agent researcher --task "task1"'),
    execAsync('npx agentic-flow --agent coder --task "task2"'),
    execAsync('npx agentic-flow --agent tester --task "task3"')
  ]);
  
  const successful = results.filter(r => r.status === 'fulfilled');
  const failed = results.filter(r => r.status === 'rejected');
  
  if (failed.length > 0) {
    console.log(\`Partial failure: ${successful.length}/${results.length} succeeded\`);
    // Retry failed tasks or proceed with partial results
  }
} catch (error) {
  // Catastrophic failure - all agents failed
  console.error('Swarm execution failed:', error);
}
\`\`\`
2. Update Agent Definitions

For all 83 agents in .claude/agents/:

Add comprehensive parallel execution section:

---
name: example-agent
description: Example agent
concurrency: true          # Supports parallel execution
batch_size: 5              # Recommended concurrent batch size
subprocess_capable: true   # Can spawn subagents via CLI
reasoningbank_enabled: true # Uses ReasoningBank for coordination
---

# System Prompt

[Existing prompt...]

## Concurrent Execution Capabilities

You are part of a parallel swarm system with the following capabilities:

### 1. Subprocess Spawning
When you encounter tasks that can be parallelized:

\`\`\`bash
# Spawn multiple subagents concurrently
npx agentic-flow --agent TYPE --task "TASK" --output reasoningbank:NAMESPACE
\`\`\`

Use `Promise.all()` in TypeScript/JavaScript to run subprocesses in parallel.

### 2. ReasoningBank Coordination
Store and retrieve results using memory namespaces:

- **Store:** `reasoningbank.store('swarm/{TASK_ID}/{AGENT_ID}', results)`
- **Retrieve:** `reasoningbank.retrieve('swarm/{TASK_ID}/{AGENT_ID}')`
- **Search:** `reasoningbank.searchPatterns('swarm/{TASK_ID}', { k: 10 })`

### 3. Result Synthesis
After all subagents complete:
1. Retrieve all results from ReasoningBank
2. Combine and synthesize findings
3. Store successful patterns for future learning

### 4. Performance Optimization
- Use QUIC transport for distributed coordination (`--transport quic`)
- Batch size: 5 concurrent subagents (adjustable based on task complexity)
- Monitor memory usage and scale dynamically

See `/agentic-flow/src/prompts/parallel-execution-guide.md` for detailed patterns and examples.
3. Integrate into Provider Instructions

Update /agentic-flow/src/proxy/provider-instructions.ts:

// Add comprehensive parallel execution instructions
export const PARALLEL_EXECUTION_INSTRUCTIONS = `
═══════════════════════════════════════════════════════════════
CRITICAL: Concurrent Execution System
═══════════════════════════════════════════════════════════════

This system supports parallel agent execution via CLI subprocesses and 
ReasoningBank coordination. Follow these patterns for optimal performance:

1. SUBPROCESS SPAWNING
   When decomposing tasks, spawn subagents via CLI:
   
   \`\`\`typescript
   const results = await Promise.all([
     exec('npx agentic-flow --agent researcher --task "analyze security"'),
     exec('npx agentic-flow --agent coder --task "implement features"'),
     exec('npx agentic-flow --agent tester --task "create tests"')
   ]);
   \`\`\`

2. REASONINGBANK COORDINATION
   Each subagent stores results in ReasoningBank:
   
   \`\`\`typescript
   // Subagent stores results
   await reasoningBank.store('swarm/task-123/agent-1', findings);
   
   // Parent retrieves all results
   const allResults = await reasoningBank.searchPatterns('swarm/task-123');
   \`\`\`

3. RESULT SYNTHESIS
   Combine subprocess outputs into final report:
   
   \`\`\`typescript
   const report = {
     findings: allResults.map(r => r.output),
     metadata: { executionTime, speedup, agentCount },
     success: allResults.every(r => r.success)
   };
   \`\`\`

4. PERFORMANCE BENEFITS
   - 2.8-4.4x speedup with parallel execution
   - 50-70% time reduction with QUIC transport
   - 32.3% token reduction via batching
   - 84.8% SWE-Bench solve rate

═══════════════════════════════════════════════════════════════
Example: Parallel Code Review (1000 files)
═══════════════════════════════════════════════════════════════

\`\`\`typescript
// 1. Split into 5 batches of 200 files each
const batches = chunkArray(files, 200);

// 2. Spawn 5 parallel reviewers
const reviews = await Promise.all(
  batches.map((batch, i) =>
    exec(\`npx agentic-flow --agent code-reviewer --task "Review batch ${i}" --output reasoningbank:swarm-review/batch-${i}\`)
  )
);

// 3. Retrieve and synthesize results
const allReviews = await Promise.all(
  batches.map((_, i) => reasoningBank.retrieve(\`swarm-review/batch-${i}\`))
);

// 4. Combine into final report
const report = synthesizeReviews(allReviews);
\`\`\`

Result: 50-70% faster than sequential review
═══════════════════════════════════════════════════════════════
`;

// Enhanced instruction provider
export function getInstructionsForModel(
  modelId: string, 
  provider?: string,
  options: {
    enableParallel?: boolean;
    batchSize?: number;
    enableReasoningBank?: boolean;
  } = {}
): ToolInstructions {
  const { enableParallel = true, batchSize = 5, enableReasoningBank = true } = options;
  
  // Get base instructions for provider
  const base = getBaseInstructions(modelId, provider);
  
  // Add parallel execution instructions
  if (enableParallel) {
    base.emphasis += "\n\n" + PARALLEL_EXECUTION_INSTRUCTIONS;
    
    // Add batch size recommendation
    base.emphasis += `\n\nRECOMMENDED BATCH SIZE: ${batchSize} concurrent subagents`;
  }
  
  // Add ReasoningBank instructions
  if (enableReasoningBank) {
    base.emphasis += `\n\n
REASONINGBANK USAGE:
- Store: await reasoningBank.store('swarm/{TASK_ID}/{AGENT_ID}', results)
- Retrieve: await reasoningBank.retrieve('swarm/{TASK_ID}/{AGENT_ID}')
- Search: await reasoningBank.searchPatterns('swarm/{TASK_ID}', { k: 10 })
    `;
  }
  
  return base;
}

// Model-specific parallel execution support
export function getParallelCapabilities(modelId: string): {
  maxConcurrency: number;
  recommendedBatchSize: number;
  supportsSubprocesses: boolean;
  supportsReasoningBank: boolean;
} {
  const normalized = modelId.toLowerCase();
  
  // High-capability models (Claude, GPT-4)
  if (normalized.includes('claude') || normalized.includes('gpt-4')) {
    return {
      maxConcurrency: 10,
      recommendedBatchSize: 5,
      supportsSubprocesses: true,
      supportsReasoningBank: true
    };
  }
  
  // Mid-tier models (DeepSeek, Llama 3.1)
  if (normalized.includes('deepseek') || normalized.includes('llama-3.1')) {
    return {
      maxConcurrency: 5,
      recommendedBatchSize: 3,
      supportsSubprocesses: true,
      supportsReasoningBank: true
    };
  }
  
  // Lower-tier models
  return {
    maxConcurrency: 3,
    recommendedBatchSize: 2,
    supportsSubprocesses: true,
    supportsReasoningBank: false
  };
}
4. Add Parallel Mode to CLI

Update /agentic-flow/src/cli/claude-code-wrapper.ts:

# Enable parallel execution mode
npx agentic-flow --agent coder --task "implement feature" --parallel

# Set concurrent batch size
npx agentic-flow --agent researcher --task "analyze" --batch-size 10

# Spawn multiple agents concurrently
npx agentic-flow --agents researcher,coder,tester --task "build feature" --concurrent

# Force sequential execution (for debugging)
npx agentic-flow --agent coder --task "fix bug" --sequential

# Use QUIC transport for distributed coordination
npx agentic-flow --agent worker --task "process data" --transport quic

# Output to ReasoningBank namespace
npx agentic-flow --agent researcher --task "analyze" --output reasoningbank:swarm/task-123/researcher

New CLI Flags:

Flag Description Default
--parallel Enable parallel execution mode false
--batch-size N Set concurrent batch size 5
--concurrent Force concurrent agent spawning false
--sequential Force sequential execution (debugging) false
--transport TYPE Transport layer (quic, tcp, ws) tcp
--output DEST Output destination (reasoningbank:NAMESPACE, file:PATH) stdout
--max-concurrency N Maximum concurrent subprocesses 10
--enable-reasoning-bank Enable ReasoningBank coordination true

Implementation:

// CLI argument parsing
const args = parseArgs(process.argv);

if (args.parallel || args.concurrent) {
  // Enable parallel execution mode
  const parallelConfig = {
    batchSize: args.batchSize || 5,
    maxConcurrency: args.maxConcurrency || 10,
    transport: args.transport || 'tcp',
    enableReasoningBank: args.enableReasoningBank !== false
  };
  
  // Inject parallel instructions into agent prompt
  const instructions = getInstructionsForModel(
    args.model,
    args.provider,
    {
      enableParallel: true,
      batchSize: parallelConfig.batchSize,
      enableReasoningBank: parallelConfig.enableReasoningBank
    }
  );
  
  // Execute with parallel mode
  await executeParallelMode(args.task, parallelConfig, instructions);
} else {
  // Standard single-agent execution
  await executeSingleAgent(args.agent, args.task);
}
5. Create Example Prompts for Common Patterns

Location: /agentic-flow/examples/prompts/

File 1: parallel-code-review.md
# Parallel Code Review Pattern

**Scenario:** Review 1000+ files efficiently

## Implementation

\`\`\`typescript
import { exec } from 'child_process';
import { promisify } from 'util';
import { reasoningBank } from 'agentic-flow';

const execAsync = promisify(exec);

async function parallelCodeReview(files: string[]) {
  const BATCH_SIZE = 200;
  const batches = chunkArray(files, BATCH_SIZE);
  const taskId = 'code-review-' + Date.now();
  
  // Spawn 5 parallel reviewers
  console.log(\`Spawning ${batches.length} parallel reviewers...\`);
  
  const reviewPromises = batches.map((batch, i) =>
    execAsync(
      \`npx agentic-flow --agent code-reviewer " +
      \`--task "Review batch ${i}: ${batch.join(',')}" " +
      \`--output reasoningbank:swarm/${taskId}/batch-${i}\`
    )
  );
  
  await Promise.all(reviewPromises);
  
  // Retrieve all results
  const allReviews = await Promise.all(
    batches.map((_, i) => 
      reasoningBank.retrieve(\`swarm/${taskId}/batch-${i}\`)
    )
  );
  
  // Synthesize final report
  return {
    totalFiles: files.length,
    batchesReviewed: batches.length,
    criticalIssues: allReviews.flatMap(r => r.critical || []),
    warnings: allReviews.flatMap(r => r.warnings || []),
    suggestions: allReviews.flatMap(r => r.suggestions || []),
    executionTimeMs: Date.now() - startTime,
    speedup: (files.length * 100) / executionTimeMs // Estimated speedup
  };
}
\`\`\`

## Performance Metrics

- **Files:** 1000
- **Batch Size:** 200 files/batch
- **Parallel Agents:** 5
- **Expected Speedup:** 4-5x vs sequential
- **Time Reduction:** 75-80%
File 2: parallel-refactoring.md
# Parallel Refactoring Pattern

**Scenario:** Refactor large codebase across multiple modules

## Implementation

\`\`\`typescript
async function parallelRefactoring(modules: string[]) {
  const taskId = 'refactor-' + Date.now();
  
  // Phase 1: Analyze all modules in parallel
  const analysisPromises = modules.map(module =>
    execAsync(\`npx agentic-flow --agent code-analyzer --task "Analyze ${module}" --output reasoningbank:swarm/${taskId}/analysis/${module}\`)
  );
  await Promise.all(analysisPromises);
  
  // Phase 2: Retrieve analysis results
  const analyses = await Promise.all(
    modules.map(m => reasoningBank.retrieve(\`swarm/${taskId}/analysis/${m}\`))
  );
  
  // Phase 3: Generate refactoring plan
  const plan = generateRefactoringPlan(analyses);
  
  // Phase 4: Execute refactoring in parallel
  const refactorPromises = plan.tasks.map(task =>
    execAsync(\`npx agentic-flow --agent coder --task "Refactor: ${task.description}" --output reasoningbank:swarm/${taskId}/refactor/${task.id}\`)
  );
  await Promise.all(refactorPromises);
  
  // Phase 5: Parallel testing
  const testPromises = modules.map(module =>
    execAsync(\`npx agentic-flow --agent tester --task "Test ${module} after refactoring" --output reasoningbank:swarm/${taskId}/tests/${module}\`)
  );
  await Promise.all(testPromises);
  
  // Phase 6: Synthesize results
  const testResults = await Promise.all(
    modules.map(m => reasoningBank.retrieve(\`swarm/${taskId}/tests/${m}\`))
  );
  
  return {
    modulesRefactored: modules.length,
    tasksCompleted: plan.tasks.length,
    testsRun: testResults.length,
    allTestsPassed: testResults.every(r => r.success),
    totalExecutionTimeMs: Date.now() - startTime
  };
}
\`\`\`
File 3: swarm-deployment.md
# Large-Scale Swarm Deployment

**Scenario:** Deploy 20+ agents for complex multi-domain task

## Implementation

\`\`\`typescript
async function largeScaleSwarmDeployment(domains: string[]) {
  const taskId = 'swarm-' + Date.now();
  const agentTypes = ['researcher', 'analyst', 'coder', 'tester', 'reviewer'];
  
  // Create agent assignments (4 agents per domain)
  const assignments = domains.flatMap(domain =>
    agentTypes.map(type => ({
      domain,
      type,
      task: \`${type} work for ${domain}\`
    }))
  );
  
  console.log(\`Deploying ${assignments.length} agents across ${domains.length} domains\`);
  
  // Spawn all agents concurrently with QUIC transport
  const agentPromises = assignments.map(({ domain, type, task }) =>
    execAsync(
      \`npx agentic-flow --agent ${type} " +
      \`--task "${task}" " +
      \`--transport quic " +
      \`--output reasoningbank:swarm/${taskId}/${domain}/${type}\`
    )
  );
  
  // Wait for all agents with progress tracking
  let completed = 0;
  const results = await Promise.allSettled(
    agentPromises.map(async (p) => {
      const result = await p;
      completed++;
      console.log(\`Progress: ${completed}/${assignments.length} agents completed\`);
      return result;
    })
  );
  
  // Analyze results
  const successful = results.filter(r => r.status === 'fulfilled');
  const failed = results.filter(r => r.status === 'rejected');
  
  // Retrieve all successful results from ReasoningBank
  const allResults = await Promise.all(
    domains.flatMap(domain =>
      agentTypes.map(type =>
        reasoningBank.retrieve(\`swarm/${taskId}/${domain}/${type}\`)
      )
    )
  );
  
  // Synthesize domain-specific reports
  const domainReports = domains.map(domain => ({
    domain,
    results: agentTypes.map(type =>
      allResults.find(r => r.domain === domain && r.type === type)
    ),
    summary: synthesizeDomainResults(domain, allResults)
  }));
  
  return {
    totalAgents: assignments.length,
    successful: successful.length,
    failed: failed.length,
    domains: domainReports,
    executionTimeMs: Date.now() - startTime,
    speedup: (assignments.length * 5000) / (Date.now() - startTime) // Estimated
  };
}
\`\`\`

## Configuration

- **Domains:** 5 (security, performance, scalability, maintainability, UX)
- **Agent Types:** 5 (researcher, analyst, coder, tester, reviewer)
- **Total Agents:** 25 (5 domains × 5 types)
- **Transport:** QUIC (50-70% faster than TCP)
- **Expected Speedup:** 10-15x vs sequential
6. Add Validation Hooks

Create /agentic-flow/src/hooks/parallel-validation.ts:

/**
 * Validation hooks to ensure agents follow parallel execution best practices
 */

import { AgentResponse, ExecutionMetrics } from '../types';
import { logger } from '../utils/logger';

export interface ParallelValidationResult {
  score: number; // 0-1, where 1 is perfect parallel execution
  issues: string[];
  recommendations: string[];
  metrics: {
    parallelOpsCount: number;
    sequentialOpsCount: number;
    avgBatchSize: number;
    subprocessesSpawned: number;
    reasoningBankUsage: number;
  };
}

/**
 * Validate agent's parallel execution patterns
 */
export function validateParallelExecution(
  response: AgentResponse,
  metrics: ExecutionMetrics
): ParallelValidationResult {
  const issues: string[] = [];
  const recommendations: string[] = [];
  let score = 1.0;
  
  // Check 1: Sequential subprocess spawning
  if (hasSequentialSubprocessSpawning(response)) {
    issues.push("Sequential subprocess spawning detected");
    recommendations.push(
      "Use Promise.all() to spawn all subprocesses concurrently:\n" +
      "await Promise.all([exec('agent1'), exec('agent2'), exec('agent3')])"
    );
    score -= 0.3;
  }
  
  // Check 2: Missing ReasoningBank coordination
  if (response.subprocesses.length > 1 && !usesReasoningBank(response)) {
    issues.push("Multiple subprocesses without ReasoningBank coordination");
    recommendations.push(
      "Store subprocess results in ReasoningBank for proper coordination:\n" +
      "await reasoningBank.store('swarm/task-id/agent-id', results)"
    );
    score -= 0.2;
  }
  
  // Check 3: Small batch sizes
  if (metrics.avgBatchSize < 3) {
    issues.push(`Small batch size detected: ${metrics.avgBatchSize} (recommended: 5+)`);
    recommendations.push(
      "Increase batch size to maximize parallelism. Target 5-10 concurrent operations."
    );
    score -= 0.1;
  }
  
  // Check 4: No QUIC transport for large-scale operations
  if (metrics.subprocessesSpawned > 10 && !usesQuicTransport(response)) {
    issues.push("Large-scale operation without QUIC transport");
    recommendations.push(
      "Use QUIC transport for 50-70% performance improvement:\n" +
      "npx agentic-flow --agent TYPE --task TASK --transport quic"
    );
    score -= 0.15;
  }
  
  // Check 5: Missing result synthesis
  if (response.subprocesses.length > 1 && !synthesizesResults(response)) {
    issues.push("Multiple subprocesses without result synthesis");
    recommendations.push(
      "Combine subprocess results into a unified report:\n" +
      "const allResults = await Promise.all(subprocesses.map(retrieveResult));\n" +
      "const report = synthesize(allResults);"
    );
    score -= 0.15;
  }
  
  // Check 6: No pattern storage for successful executions
  if (score > 0.8 && !storesSuccessPattern(response)) {
    recommendations.push(
      "Store successful execution patterns in ReasoningBank for learning:\n" +
      "await reasoningBank.storePattern({ sessionId, task, output, reward, success })"
    );
    score -= 0.1;
  }
  
  return {
    score: Math.max(0, score),
    issues,
    recommendations,
    metrics: {
      parallelOpsCount: countParallelOps(response),
      sequentialOpsCount: countSequentialOps(response),
      avgBatchSize: metrics.avgBatchSize,
      subprocessesSpawned: metrics.subprocessesSpawned,
      reasoningBankUsage: metrics.reasoningBankUsage
    }
  };
}

// Helper functions
function hasSequentialSubprocessSpawning(response: AgentResponse): boolean {
  // Check if subprocess spawning uses await in sequence vs Promise.all
  const code = response.code || '';
  const hasAwaitExec = /await\s+exec/.test(code);
  const hasPromiseAll = /Promise\.all\(/i.test(code);
  
  return hasAwaitExec && !hasPromiseAll;
}

function usesReasoningBank(response: AgentResponse): boolean {
  const code = response.code || '';
  return /reasoningBank\.(store|retrieve|search)/.test(code);
}

function usesQuicTransport(response: AgentResponse): boolean {
  const code = response.code || '';
  return /--transport\s+quic/i.test(code) || /QuicTransport/.test(code);
}

function synthesizesResults(response: AgentResponse): boolean {
  const code = response.code || '';
  return /synthesize|combine|merge|aggregate/i.test(code);
}

function storesSuccessPattern(response: AgentResponse): boolean {
  const code = response.code || '';
  return /storePattern|reasoningBank\.store/.test(code);
}

function countParallelOps(response: AgentResponse): number {
  const code = response.code || '';
  const promiseAllMatches = code.match(/Promise\.all\(/g);
  return promiseAllMatches?.length || 0;
}

function countSequentialOps(response: AgentResponse): number {
  const code = response.code || '';
  const awaitMatches = code.match(/await\s+(?!Promise\.all)/g);
  return awaitMatches?.length || 0;
}

/**
 * Post-execution hook: Log validation results and suggestions
 */
export async function postExecutionValidation(
  response: AgentResponse,
  metrics: ExecutionMetrics
): Promise<void> {
  const validation = validateParallelExecution(response, metrics);
  
  logger.info('Parallel execution validation', {
    score: validation.score,
    issues: validation.issues.length,
    recommendations: validation.recommendations.length
  });
  
  if (validation.issues.length > 0) {
    logger.warn('Parallel execution issues detected', {
      issues: validation.issues,
      recommendations: validation.recommendations
    });
  }
  
  // Store validation results for learning
  if (validation.score < 0.7) {
    await reasoningBank.storePattern({
      sessionId: 'parallel-validation-' + Date.now(),
      task: 'Parallel execution validation',
      output: JSON.stringify(validation),
      reward: validation.score,
      success: validation.score > 0.5
    });
  }
}

Implementation Plan

Phase 1: Foundation (Week 1)
  • Analyze current codebase for parallel execution patterns
  • Document Claude Agent SDK integration points
  • Create comprehensive GitHub issue

Deliverables:

  • Create /agentic-flow/src/prompts/parallel-execution-guide.md
  • Update /agentic-flow/src/proxy/provider-instructions.ts with parallel instructions
  • Add CLI flags: --parallel, --batch-size, --concurrent, --transport, --output
  • Document in README.md with examples
Phase 2: Agent Updates (Week 2)

Top 10 Priority Agents:

  1. coder - Most frequently used for implementation
  2. researcher - Parallel research across domains
  3. code-reviewer - Large-scale code review
  4. tester - Parallel test generation
  5. task-orchestrator - Swarm coordination
  6. system-architect - Multi-domain architecture design
  7. api-docs - Documentation across multiple APIs
  8. backend-dev - Parallel microservice development
  9. performance-benchmarker - Concurrent benchmarking
  10. swarm-memory-manager - Distributed memory coordination

Tasks:

  • Add concurrency, batch_size, subprocess_capable, reasoningbank_enabled metadata
  • Inject parallel execution section into system prompts
  • Create validation hooks
  • Add 5 example prompts (code review, refactoring, testing, documentation, swarm deployment)
Phase 3: Testing & Optimization (Week 3)

Benchmarks:

  • Parallel vs sequential code review (1000 files)
  • Multi-domain research task (5 domains, 25 agents)
  • Large-scale refactoring (50+ modules)
  • Distributed testing (100+ test suites)

Metrics to measure:

  • Execution time (target: 3-5x improvement)
  • Token usage (target: 25%+ reduction)
  • Agent coordination overhead
  • ReasoningBank performance
  • QUIC vs TCP transport comparison

A/B Testing:

  • Test with Anthropic Claude (native tools)
  • Test with OpenRouter/DeepSeek (99% cost savings)
  • Test with ONNX local models ($0 cost)
  • Document provider-specific performance characteristics
Phase 4: Rollout (Week 4)
  • Update all 83 agents with parallel execution capabilities
  • Create migration guide for custom user agents
  • Add to SPARC workflow documentation
  • Create tutorial video/blog post
  • Publish performance benchmarks
  • Update CLI help documentation

Success Metrics

Performance Metrics
Metric Baseline Target Measurement
Multi-agent task speedup 1x 3-5x Execution time comparison
Total execution time reduction 0% 40-50% Large-scale benchmarks
Token usage reduction 0% 25-35% Fewer sequential messages
Agent coordination overhead N/A <5% ReasoningBank latency
QUIC transport benefit 0% 50-70% QUIC vs TCP comparison
Code Quality Metrics
Metric Target Validation
Agents following parallel rules 90%+ Validation hooks
Sequential bottlenecks eliminated 95%+ Code analysis
Batch operation usage 80%+ Pattern matching
ReasoningBank integration 75%+ Memory usage tracking
Subprocess spawning patterns 85%+ CLI usage analytics
Developer Experience Metrics
Metric Target Measurement
Clear examples for common patterns 100% 10+ documented examples
CLI ease of use High User surveys
Error messages clarity 90%+ Issue tracking
Documentation completeness 95%+ Coverage analysis

Related Files & Architecture

Core Infrastructure
/agentic-flow/
├── src/
│   ├── coordination/
│   │   └── parallelSwarm.js           # Parallel coordination APIs
│   ├── examples/
│   │   └── parallel-swarm-deployment.ts # 6 concrete examples
│   ├── reasoningbank/
│   │   ├── core/                      # Pattern storage & retrieval
│   │   └── controllers/               # Memory coordination
│   ├── transport/
│   │   └── quic.ts                    # QUIC transport (50-70% faster)
│   └── agents/
│       └── claudeAgent.ts             # Agent SDK integration
├── examples/
│   ├── complex-multi-agent-deployment.ts # Multi-agent orchestration
│   └── quic-swarm-coordination.js     # QUIC examples
└── .claude/
    └── agents/                         # 83 agent definitions
Prompting System
/agentic-flow/src/
├── proxy/
│   └── provider-instructions.ts       # Model-specific prompts
├── prompts/ (NEW)
│   └── parallel-execution-guide.md    # Parallel execution patterns
├── hooks/ (NEW)
│   └── parallel-validation.ts         # Validation & recommendations
└── cli/
    ├── agent-manager.ts               # Agent management
    └── claude-code-wrapper.ts         # CLI with new flags

Questions to Resolve

1. Model Compatibility

Question: Which LLM providers benefit most from parallel execution prompting?

Hypothesis:

  • High benefit: Claude, GPT-4 (strong tool calling + reasoning)
  • Medium benefit: DeepSeek, Llama 3.1 (good reasoning, weaker tool calling)
  • Low benefit: Smaller models (may struggle with complex coordination)

Testing approach:

  • Benchmark same task across 5 providers
  • Measure: execution time, token usage, success rate
  • Document provider-specific recommendations
2. Batch Size Tuning

Question: Optimal batch size per agent type and task complexity?

Hypothesis:

  • Simple tasks: 3-5 concurrent agents
  • Medium complexity: 5-8 concurrent agents
  • High complexity: 8-10 concurrent agents
  • Resource-intensive: 2-3 concurrent agents

Testing approach:

  • Vary batch size from 1-15 for standard tasks
  • Measure: total execution time, memory usage, error rate
  • Create lookup table for optimal batch sizes
3. Error Handling

Question: How to handle partial failures in parallel operations?

Options:
A. Fail fast: Abort all subprocesses on first failure
B. Best effort: Continue with successful results, report failures
C. Retry: Automatically retry failed subprocesses
D. Hybrid: Retry critical tasks, continue for non-critical

Recommendation: Option D (hybrid approach)

  • Critical tasks: Retry up to 3 times
  • Non-critical: Continue with partial results
  • Always log failures to ReasoningBank for learning
4. Memory Coordination

Question: Best practices for cross-agent state sharing via ReasoningBank?

Patterns to document:

  • Namespace hierarchy: swarm/{TASK_ID}/{AGENT_ID}/{RESOURCE}
  • TTL strategy: Short-lived (5 min) for coordination, long-lived (24h) for results
  • Conflict resolution: Last-write-wins vs merge strategies
  • Memory cleanup: Automatic cleanup after task completion
5. Token Budgets

Question: How to prevent token overflow with large parallel operations?

Strategies:

  • Chunking: Break large tasks into smaller batches
  • Summarization: Summarize subprocess results before synthesis
  • Streaming: Stream results instead of batching
  • Selective retrieval: Only retrieve necessary data from ReasoningBank

Implementation:

// Auto-chunking for large operations
if (tasks.length > TOKEN_LIMIT_THRESHOLD) {
  const chunks = chunkByTokenBudget(tasks, MAX_TOKENS_PER_CHUNK);
  for (const chunk of chunks) {
    await processChunk(chunk);
  }
} else {
  await processAllTasks(tasks);
}

References & Documentation

Performance Data
  • CLAUDE.md: "1 MESSAGE = ALL RELATED OPERATIONS" rule (lines 1-45)
  • parallel-swarm-deployment.ts: 6 concrete examples with metrics
  • quic-swarm-coordination.js: 50-70% performance improvement demos
  • SWE-Bench: 84.8% solve rate with current architecture
Architecture
  • provider-instructions.ts: Model-specific prompting patterns (295 lines)
  • claudeAgent.ts: Multi-provider SDK integration (400+ lines)
  • agentLoader.ts: Agent definition system (199 lines)
  • agent-manager.ts: 83 agent CLI management (563 lines)
Examples & Benchmarks
  • complex-multi-agent-deployment.ts: Hierarchical swarm with 8 agents
  • parallel-swarm-deployment.ts: 6 patterns (spawn, execute, deploy, batch, scale, large-scale)
  • reasoningbank/benchmark.ts: Pattern storage & retrieval benchmarks

Priority: 🔴 High
Complexity: 🟡 Medium
Impact: 🟢 High (affects all 83 agents + user experience + performance)
Dependencies: ✅ None (builds on existing infrastructure)
Timeline: 4 weeks (phased rollout)
Assignee: TBD

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading .claude/agents/, src/agents/claudeAgent.ts, src/proxy/provider-instructions.ts, and the parallel execution examples and coordination files named in the issue. Define the prompt guidance and update the agent definitions consistently, then verify that the documented concurrent, batch, subprocess, and ReasoningBank patterns match the existing APIs.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
ai, developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.