Files
ruflo/docs/ARCHITECTURE.md
T

1412 lines
42 KiB
Markdown

# 🏗️ Claude-Flow Architecture Documentation
## Table of Contents
- [System Overview](#system-overview)
- [Core Architecture](#core-architecture)
- [Component Architecture](#component-architecture)
- [Data Flow](#data-flow)
- [Design Patterns](#design-patterns)
- [Technology Stack](#technology-stack)
- [Deployment Architecture](#deployment-architecture)
- [Security Architecture](#security-architecture)
- [Performance Architecture](#performance-architecture)
- [Scalability Design](#scalability-design)
---
## System Overview
Claude-Flow is built on a microservices architecture with event-driven communication, designed for high scalability, fault tolerance, and extensibility.
### High-Level Architecture
```
┌─────────────────────────────────────────────────────────────────┐
│ Client Layer │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ CLI │ │ API │ │WebSocket │ │ MCP │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────┐
│ API Gateway Layer │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Load Balancer │ Rate Limiter │ Auth │ Router │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────┐
│ Core Services Layer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Orchestrator │ │Swarm Manager │ │Task Engine │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │Agent Manager │ │Memory System │ │MCP Server │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────┐
│ Infrastructure Layer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Database │ │Message Queue │ │Cache Layer │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │File Storage │ │Monitoring │ │Logging │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
### Key Architectural Principles
1. **Microservices Architecture** - Loosely coupled, independently deployable services
2. **Event-Driven Design** - Asynchronous communication via event bus
3. **Domain-Driven Design** - Clear boundaries and rich domain models
4. **CQRS Pattern** - Separation of commands and queries
5. **Hexagonal Architecture** - Ports and adapters for external dependencies
---
## Core Architecture
### Component Diagram
```mermaid
graph TB
subgraph "Client Applications"
CLI[CLI Tool]
API[REST API]
WS[WebSocket]
MCP[MCP Client]
end
subgraph "Core Engine"
ORCH[Orchestrator]
AGENT[Agent Manager]
TASK[Task Engine]
SWARM[Swarm Coordinator]
MEM[Memory Manager]
end
subgraph "Provider Layer"
CLAUDE[Claude Provider]
OPENAI[OpenAI Provider]
LOCAL[Local Provider]
end
subgraph "Storage Layer"
DB[(SQLite DB)]
CACHE[(Redis Cache)]
FS[File System]
end
CLI --> ORCH
API --> ORCH
WS --> ORCH
MCP --> ORCH
ORCH --> AGENT
ORCH --> TASK
ORCH --> SWARM
ORCH --> MEM
AGENT --> CLAUDE
AGENT --> OPENAI
AGENT --> LOCAL
MEM --> DB
MEM --> CACHE
TASK --> DB
SWARM --> FS
```
### Core Components
#### 1. Orchestrator (`src/core/orchestrator.ts`)
The central coordination component that manages all system operations.
```typescript
class Orchestrator {
private agentManager: AgentManager;
private taskEngine: TaskEngine;
private swarmCoordinator: SwarmCoordinator;
private memoryManager: MemoryManager;
private eventBus: EventBus;
async initialize(): Promise<void> {
// Initialize all subsystems
await this.memoryManager.initialize();
await this.agentManager.initialize();
await this.taskEngine.initialize();
await this.swarmCoordinator.initialize();
// Setup event handlers
this.setupEventHandlers();
}
async executeTask(task: Task): Promise<TaskResult> {
// Task execution logic with agent coordination
const agent = await this.agentManager.selectAgent(task);
const context = await this.memoryManager.getContext(task);
return await agent.execute(task, context);
}
}
```
#### 2. Agent Manager (`src/agents/manager.ts`)
Manages the lifecycle and coordination of AI agents.
```typescript
class AgentManager {
private agents: Map<string, Agent>;
private pool: AgentPool;
private selector: AgentSelector;
async spawnAgent(config: AgentConfig): Promise<Agent> {
const agent = await this.createAgent(config);
await this.pool.add(agent);
this.agents.set(agent.id, agent);
return agent;
}
async selectAgent(task: Task): Promise<Agent> {
return await this.selector.select(task, this.agents);
}
}
```
#### 3. Task Engine (`src/task/engine.ts`)
Handles task scheduling, execution, and monitoring.
```typescript
class TaskEngine {
private queue: TaskQueue;
private scheduler: TaskScheduler;
private executor: TaskExecutor;
async submitTask(task: Task): Promise<string> {
await this.queue.enqueue(task);
await this.scheduler.schedule(task);
return task.id;
}
async executeTask(taskId: string): Promise<TaskResult> {
const task = await this.queue.dequeue(taskId);
return await this.executor.execute(task);
}
}
```
#### 4. Swarm Coordinator (`src/swarm/coordinator.ts`)
Manages multi-agent swarm operations and topologies.
```typescript
class SwarmCoordinator {
private topology: SwarmTopology;
private agents: Agent[];
private communicator: SwarmCommunicator;
async initializeSwarm(config: SwarmConfig): Promise<Swarm> {
this.topology = this.createTopology(config.topology);
this.agents = await this.spawnAgents(config.agents);
return new Swarm(this.topology, this.agents);
}
async coordinate(objective: string): Promise<SwarmResult> {
const tasks = await this.decomposeTasks(objective);
return await this.topology.execute(tasks, this.agents);
}
}
```
#### 5. Memory Manager (`src/memory/manager.ts`)
Distributed memory system for persistent state and knowledge.
```typescript
class MemoryManager {
private backend: MemoryBackend;
private cache: MemoryCache;
private indexer: MemoryIndexer;
async store(key: string, value: any, options?: MemoryOptions): Promise<void> {
await this.cache.set(key, value);
await this.backend.store(key, value, options);
await this.indexer.index(key, value);
}
async retrieve(key: string): Promise<any> {
const cached = await this.cache.get(key);
if (cached) return cached;
const value = await this.backend.retrieve(key);
await this.cache.set(key, value);
return value;
}
}
```
---
## Component Architecture
### Agent Architecture
```
┌─────────────────────────────────────────┐
│ Agent │
├─────────────────────────────────────────┤
│ Properties: │
│ - id: string │
│ - type: AgentType │
│ - status: AgentStatus │
│ - capabilities: Capability[] │
├─────────────────────────────────────────┤
│ Methods: │
│ - execute(task: Task): TaskResult │
│ - communicate(msg: Message): void │
│ - updateStatus(status: Status): void │
└─────────────────────────────────────────┘
│
├── CoderAgent
├── ReviewerAgent
├── TesterAgent
├── ArchitectAgent
└── [50+ specialized agents]
```
### Task Architecture
```
┌─────────────────────────────────────────┐
│ Task │
├─────────────────────────────────────────┤
│ Properties: │
│ - id: string │
│ - type: TaskType │
│ - priority: Priority │
│ - status: TaskStatus │
│ - dependencies: Task[] │
├─────────────────────────────────────────┤
│ Methods: │
│ - validate(): boolean │
│ - execute(): TaskResult │
│ - rollback(): void │
└─────────────────────────────────────────┘
```
### Swarm Topologies
#### Centralized Topology
```
┌──────────┐
│ Queen │
└────┬─────┘
│
┌─────────┼─────────┐
│ │ │
┌───▼───┐ ┌──▼───┐ ┌───▼───┐
│Worker1│ │Worker2│ │Worker3│
└───────┘ └──────┘ └───────┘
```
#### Mesh Topology
```
┌───────┐ ┌───────┐
│Agent1 │◄────►Agent2 │
└───┬───┘ └───┬───┘
│ ╳ │
│ ╱ ╲ │
┌───▼───┐ ┌───▼───┐
│Agent3 │◄────►Agent4 │
└───────┘ └───────┘
```
#### Hierarchical Topology
```
┌──────────┐
│ Root │
└────┬─────┘
│
┌────────┴────────┐
│ │
┌────▼────┐ ┌────▼────┐
│Manager1 │ │Manager2 │
└────┬────┘ └────┬────┘
│ │
┌───┴───┐ ┌───┴───┐
│ │ │ │
┌──▼─┐ ┌──▼─┐ ┌──▼─┐ ┌──▼─┐
│ W1 │ │ W2 │ │ W3 │ │ W4 │
└────┘ └────┘ └────┘ └────┘
```
---
## Data Flow
### Request Processing Flow
```mermaid
sequenceDiagram
participant Client
participant Gateway
participant Orchestrator
participant TaskEngine
participant AgentManager
participant Agent
participant Memory
Client->>Gateway: Submit Request
Gateway->>Gateway: Authenticate & Validate
Gateway->>Orchestrator: Forward Request
Orchestrator->>TaskEngine: Create Task
TaskEngine->>AgentManager: Request Agent
AgentManager->>AgentManager: Select Best Agent
AgentManager->>Agent: Assign Task
Agent->>Memory: Get Context
Memory-->>Agent: Return Context
Agent->>Agent: Execute Task
Agent->>Memory: Store Results
Agent-->>TaskEngine: Return Results
TaskEngine-->>Orchestrator: Task Complete
Orchestrator-->>Gateway: Response
Gateway-->>Client: Return Results
```
### Event Flow
```mermaid
graph LR
A[Event Producer] --> B[Event Bus]
B --> C[Event Router]
C --> D[Handler 1]
C --> E[Handler 2]
C --> F[Handler 3]
D --> G[Action 1]
E --> H[Action 2]
F --> I[Action 3]
```
---
## Design Patterns
### 1. Repository Pattern
```typescript
interface Repository<T> {
findById(id: string): Promise<T>;
findAll(): Promise<T[]>;
save(entity: T): Promise<void>;
delete(id: string): Promise<void>;
}
class AgentRepository implements Repository<Agent> {
constructor(private db: Database) {}
async findById(id: string): Promise<Agent> {
const data = await this.db.query('SELECT * FROM agents WHERE id = ?', [id]);
return this.mapToAgent(data);
}
async save(agent: Agent): Promise<void> {
await this.db.execute(
'INSERT OR REPLACE INTO agents VALUES (?, ?, ?, ?)',
[agent.id, agent.type, agent.status, JSON.stringify(agent.config)]
);
}
}
```
### 2. Strategy Pattern
```typescript
interface CoordinationStrategy {
coordinate(agents: Agent[], tasks: Task[]): Promise<ExecutionPlan>;
}
class CentralizedStrategy implements CoordinationStrategy {
async coordinate(agents: Agent[], tasks: Task[]): Promise<ExecutionPlan> {
// Queen-led coordination logic
}
}
class MeshStrategy implements CoordinationStrategy {
async coordinate(agents: Agent[], tasks: Task[]): Promise<ExecutionPlan> {
// Peer-to-peer coordination logic
}
}
class SwarmCoordinator {
constructor(private strategy: CoordinationStrategy) {}
async execute(agents: Agent[], tasks: Task[]): Promise<void> {
const plan = await this.strategy.coordinate(agents, tasks);
await this.executePlan(plan);
}
}
```
### 3. Observer Pattern
```typescript
interface Observer {
update(event: Event): void;
}
class EventBus {
private observers: Map<string, Observer[]> = new Map();
subscribe(eventType: string, observer: Observer): void {
if (!this.observers.has(eventType)) {
this.observers.set(eventType, []);
}
this.observers.get(eventType)!.push(observer);
}
publish(event: Event): void {
const observers = this.observers.get(event.type) || [];
observers.forEach(observer => observer.update(event));
}
}
```
### 4. Factory Pattern
```typescript
abstract class AgentFactory {
abstract createAgent(type: string): Agent;
spawn(type: string, config: AgentConfig): Agent {
const agent = this.createAgent(type);
agent.configure(config);
return agent;
}
}
class SpecializedAgentFactory extends AgentFactory {
createAgent(type: string): Agent {
switch(type) {
case 'coder': return new CoderAgent();
case 'reviewer': return new ReviewerAgent();
case 'tester': return new TesterAgent();
default: throw new Error(`Unknown agent type: ${type}`);
}
}
}
```
### 5. Decorator Pattern
```typescript
interface Agent {
execute(task: Task): Promise<TaskResult>;
}
class BaseAgent implements Agent {
async execute(task: Task): Promise<TaskResult> {
// Basic execution logic
}
}
class LoggingDecorator implements Agent {
constructor(private agent: Agent) {}
async execute(task: Task): Promise<TaskResult> {
console.log(`Starting task: ${task.id}`);
const result = await this.agent.execute(task);
console.log(`Completed task: ${task.id}`);
return result;
}
}
class MetricsDecorator implements Agent {
constructor(private agent: Agent) {}
async execute(task: Task): Promise<TaskResult> {
const start = Date.now();
const result = await this.agent.execute(task);
const duration = Date.now() - start;
await this.recordMetrics(task.id, duration);
return result;
}
}
```
---
## Technology Stack
### Core Technologies
| Layer | Technology | Purpose |
|-------|------------|---------|
| Runtime | Node.js v20+ | JavaScript runtime |
| Language | TypeScript 5.3+ | Type-safe development |
| Framework | Custom | Lightweight, modular |
| Database | SQLite | Embedded database |
| Cache | In-memory | Fast data access |
| Queue | Custom | Task queuing |
| CLI | Commander.js | Command-line interface |
| API | Express | REST API server |
| WebSocket | ws | Real-time communication |
| Testing | Jest | Unit & integration tests |
### Dependencies
#### Core Dependencies
```json
{
"@modelcontextprotocol/sdk": "^1.0.4",
"better-sqlite3": "^12.2.0",
"commander": "^11.1.0",
"express": "^5.1.0",
"ws": "^8.18.3",
"yaml": "^2.8.0"
}
```
#### Development Dependencies
```json
{
"@types/node": "^20.19.7",
"typescript": "^5.3.3",
"jest": "^29.7.0",
"eslint": "^8.57.1",
"prettier": "^3.1.1"
}
```
### File Structure
```
claude-flow/
├── src/
│ ├── core/ # Core orchestration logic
│ ├── agents/ # Agent implementations
│ ├── swarm/ # Swarm coordination
│ ├── task/ # Task management
│ ├── memory/ # Memory system
│ ├── providers/ # LLM providers
│ ├── api/ # REST API
│ ├── cli/ # CLI commands
│ ├── mcp/ # MCP protocol
│ ├── utils/ # Utilities
│ └── types/ # TypeScript types
├── tests/ # Test suites
├── docs/ # Documentation
├── examples/ # Example code
└── scripts/ # Build & deploy scripts
```
---
## Deployment Architecture
### Container Architecture
```dockerfile
# Multi-stage build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS runtime
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package*.json ./
EXPOSE 3000
CMD ["node", "dist/index.js"]
```
### Kubernetes Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: claude-flow
spec:
replicas: 3
selector:
matchLabels:
app: claude-flow
template:
metadata:
labels:
app: claude-flow
spec:
containers:
- name: claude-flow
image: claude-flow:latest
ports:
- containerPort: 3000
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "2000m"
env:
- name: NODE_ENV
value: "production"
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5
```
### Cloud Architecture
```
┌─────────────────────────────────────────────────────┐
│ CDN Layer │
│ (CloudFlare/Fastly) │
└─────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────┐
│ Load Balancer (ALB) │
└─────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────┐
│ Auto-Scaling Group (ASG) │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │Instance 1│ │Instance 2│ │Instance 3│ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────┐
│ Managed Services │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ RDS │ │ Redis │ │ S3 │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────┘
```
---
## Security Architecture
### Security Layers
```
┌─────────────────────────────────────────────────────┐
│ Application Layer │
│ - Input validation │
│ - Output encoding │
│ - CSRF protection │
└─────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────┐
│ Authentication Layer │
│ - JWT tokens │
│ - API keys │
│ - OAuth 2.0 │
└─────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────┐
│ Authorization Layer │
│ - Role-based access control (RBAC) │
│ - Attribute-based access control (ABAC) │
│ - Policy engine │
└─────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────┐
│ Network Layer │
│ - TLS/SSL encryption │
│ - VPC isolation │
│ - Security groups │
└─────────────────────────────────────────────────────┘
```
### Security Implementation
```typescript
class SecurityManager {
private validator: InputValidator;
private authenticator: Authenticator;
private authorizer: Authorizer;
private encryptor: Encryptor;
async validateRequest(request: Request): Promise<ValidationResult> {
// Input validation
const validation = await this.validator.validate(request);
if (!validation.valid) {
throw new ValidationError(validation.errors);
}
// Authentication
const identity = await this.authenticator.authenticate(request);
if (!identity) {
throw new AuthenticationError('Invalid credentials');
}
// Authorization
const authorized = await this.authorizer.authorize(identity, request);
if (!authorized) {
throw new AuthorizationError('Insufficient permissions');
}
return { valid: true, identity };
}
async encryptSensitiveData(data: any): Promise<string> {
return await this.encryptor.encrypt(JSON.stringify(data));
}
}
```
---
## Performance Architecture
### Performance Optimization Strategies
#### 1. Caching Strategy
```typescript
class CacheManager {
private l1Cache: Map<string, any> = new Map(); // Memory cache
private l2Cache: RedisCache; // Redis cache
private l3Cache: DatabaseCache; // Database cache
async get(key: string): Promise<any> {
// L1 Cache (Memory)
if (this.l1Cache.has(key)) {
return this.l1Cache.get(key);
}
// L2 Cache (Redis)
const l2Value = await this.l2Cache.get(key);
if (l2Value) {
this.l1Cache.set(key, l2Value);
return l2Value;
}
// L3 Cache (Database)
const l3Value = await this.l3Cache.get(key);
if (l3Value) {
await this.l2Cache.set(key, l3Value);
this.l1Cache.set(key, l3Value);
return l3Value;
}
return null;
}
}
```
#### 2. Connection Pooling
```typescript
class ConnectionPool {
private connections: Connection[] = [];
private available: Connection[] = [];
private maxConnections: number = 100;
async getConnection(): Promise<Connection> {
if (this.available.length > 0) {
return this.available.pop()!;
}
if (this.connections.length < this.maxConnections) {
const conn = await this.createConnection();
this.connections.push(conn);
return conn;
}
// Wait for available connection
return await this.waitForConnection();
}
releaseConnection(conn: Connection): void {
this.available.push(conn);
}
}
```
#### 3. Async Processing
```typescript
class AsyncProcessor {
private queue: Queue<Task>;
private workers: Worker[];
async process(task: Task): Promise<void> {
await this.queue.enqueue(task);
// Non-blocking processing
setImmediate(() => {
this.processNextTask();
});
}
private async processNextTask(): Promise<void> {
const task = await this.queue.dequeue();
if (!task) return;
const worker = await this.getAvailableWorker();
await worker.execute(task);
// Continue processing
setImmediate(() => {
this.processNextTask();
});
}
}
```
### Performance Monitoring
```typescript
class PerformanceMonitor {
private metrics: MetricsCollector;
async trackOperation<T>(
name: string,
operation: () => Promise<T>
): Promise<T> {
const startTime = process.hrtime.bigint();
const startMemory = process.memoryUsage();
try {
const result = await operation();
const endTime = process.hrtime.bigint();
const duration = Number(endTime - startTime) / 1e6; // Convert to ms
const endMemory = process.memoryUsage();
const memoryDelta = endMemory.heapUsed - startMemory.heapUsed;
await this.metrics.record({
operation: name,
duration,
memoryDelta,
success: true,
timestamp: new Date()
});
return result;
} catch (error) {
await this.metrics.record({
operation: name,
success: false,
error: error.message,
timestamp: new Date()
});
throw error;
}
}
}
```
---
## Scalability Design
### Horizontal Scaling
```typescript
class LoadBalancer {
private instances: Instance[];
private algorithm: BalancingAlgorithm;
async route(request: Request): Promise<Response> {
const instance = await this.algorithm.selectInstance(this.instances);
return await instance.handle(request);
}
}
class RoundRobinAlgorithm implements BalancingAlgorithm {
private currentIndex = 0;
async selectInstance(instances: Instance[]): Promise<Instance> {
const instance = instances[this.currentIndex];
this.currentIndex = (this.currentIndex + 1) % instances.length;
return instance;
}
}
```
### Sharding Strategy
```typescript
class ShardManager {
private shards: Map<string, Shard>;
getShard(key: string): Shard {
const shardKey = this.calculateShardKey(key);
return this.shards.get(shardKey)!;
}
private calculateShardKey(key: string): string {
const hash = this.hash(key);
const shardIndex = hash % this.shards.size;
return `shard-${shardIndex}`;
}
private hash(key: string): number {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash = ((hash << 5) - hash) + key.charCodeAt(i);
hash = hash & hash; // Convert to 32-bit integer
}
return Math.abs(hash);
}
}
```
### Auto-Scaling Configuration
```yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: claude-flow-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: claude-flow
minReplicas: 3
maxReplicas: 100
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
- type: Pods
pods:
metric:
name: task_queue_depth
target:
type: AverageValue
averageValue: "30"
```
---
## Fault Tolerance
### Circuit Breaker Pattern
```typescript
class CircuitBreaker {
private state: 'CLOSED' | 'OPEN' | 'HALF_OPEN' = 'CLOSED';
private failureCount = 0;
private successCount = 0;
private lastFailureTime?: number;
async execute<T>(operation: () => Promise<T>): Promise<T> {
if (this.state === 'OPEN') {
if (this.shouldAttemptReset()) {
this.state = 'HALF_OPEN';
} else {
throw new Error('Circuit breaker is OPEN');
}
}
try {
const result = await operation();
this.onSuccess();
return result;
} catch (error) {
this.onFailure();
throw error;
}
}
private onSuccess(): void {
this.failureCount = 0;
if (this.state === 'HALF_OPEN') {
this.successCount++;
if (this.successCount >= 5) {
this.state = 'CLOSED';
this.successCount = 0;
}
}
}
private onFailure(): void {
this.failureCount++;
this.lastFailureTime = Date.now();
if (this.failureCount >= 5) {
this.state = 'OPEN';
}
}
private shouldAttemptReset(): boolean {
return Date.now() - this.lastFailureTime! > 60000; // 1 minute
}
}
```
### Retry Mechanism
```typescript
class RetryManager {
async executeWithRetry<T>(
operation: () => Promise<T>,
options: RetryOptions = {}
): Promise<T> {
const maxRetries = options.maxRetries || 3;
const baseDelay = options.baseDelay || 1000;
const maxDelay = options.maxDelay || 30000;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await operation();
} catch (error) {
if (attempt === maxRetries) {
throw error;
}
const delay = Math.min(
baseDelay * Math.pow(2, attempt),
maxDelay
);
await this.sleep(delay);
}
}
throw new Error('Unexpected retry failure');
}
private sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
```
---
## Database Schema
### Core Tables
```sql
-- Agents table
CREATE TABLE agents (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL,
status TEXT NOT NULL,
capabilities TEXT, -- JSON array
config TEXT, -- JSON object
metrics TEXT, -- JSON object
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Tasks table
CREATE TABLE tasks (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL,
priority INTEGER DEFAULT 0,
assigned_agent TEXT,
parent_task TEXT,
dependencies TEXT, -- JSON array
result TEXT, -- JSON object
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
started_at DATETIME,
completed_at DATETIME,
FOREIGN KEY (assigned_agent) REFERENCES agents(id),
FOREIGN KEY (parent_task) REFERENCES tasks(id)
);
-- Swarms table
CREATE TABLE swarms (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
topology TEXT NOT NULL,
objective TEXT,
config TEXT, -- JSON object
status TEXT NOT NULL,
agents TEXT, -- JSON array of agent IDs
progress REAL DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Memory table
CREATE TABLE memory (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
namespace TEXT DEFAULT 'default',
version INTEGER DEFAULT 1,
ttl INTEGER,
tags TEXT, -- JSON array
metadata TEXT, -- JSON object
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
accessed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
access_count INTEGER DEFAULT 0
);
-- Events table
CREATE TABLE events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT NOT NULL,
source TEXT NOT NULL,
data TEXT, -- JSON object
correlation_id TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_type (type),
INDEX idx_source (source),
INDEX idx_correlation (correlation_id),
INDEX idx_timestamp (timestamp)
);
-- Metrics table
CREATE TABLE metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
entity_type TEXT NOT NULL,
entity_id TEXT NOT NULL,
metric_name TEXT NOT NULL,
metric_value REAL,
metadata TEXT, -- JSON object
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_entity (entity_type, entity_id),
INDEX idx_metric (metric_name),
INDEX idx_timestamp (timestamp)
);
```
---
## API Design
### RESTful Endpoints
```
/api/v2/
├── agents/
│ ├── GET / # List agents
│ ├── POST / # Create agent
│ ├── GET /:id # Get agent
│ ├── PUT /:id # Update agent
│ ├── DELETE /:id # Delete agent
│ └── GET /:id/health # Health check
├── tasks/
│ ├── GET / # List tasks
│ ├── POST / # Create task
│ ├── GET /:id # Get task
│ ├── PUT /:id # Update task
│ ├── DELETE /:id # Cancel task
│ └── POST /:id/assign # Assign to agent
├── swarms/
│ ├── GET / # List swarms
│ ├── POST / # Create swarm
│ ├── GET /:id # Get swarm
│ ├── GET /:id/status # Get status
│ └── POST /:id/control # Control swarm
├── memory/
│ ├── GET / # Query memory
│ ├── POST / # Store memory
│ ├── GET /:key # Get entry
│ ├── PUT /:key # Update entry
│ └── DELETE /:key # Delete entry
└── system/
├── GET /status # System status
├── GET /health # Health check
├── GET /metrics # Metrics
└── POST /diagnostics # Diagnostics
```
---
## Testing Strategy
### Test Pyramid
```
┌─────────┐
│ E2E │ 5%
└────┬────┘
│
┌─────▼─────┐
│Integration│ 20%
└─────┬─────┘
│
┌──────▼──────┐
│ Unit │ 75%
└─────────────┘
```
### Test Structure
```typescript
describe('Orchestrator', () => {
let orchestrator: Orchestrator;
let mockAgentManager: jest.Mocked<AgentManager>;
let mockTaskEngine: jest.Mocked<TaskEngine>;
beforeEach(() => {
mockAgentManager = createMockAgentManager();
mockTaskEngine = createMockTaskEngine();
orchestrator = new Orchestrator(mockAgentManager, mockTaskEngine);
});
describe('executeTask', () => {
it('should select appropriate agent for task', async () => {
const task = createTestTask({ type: 'code_generation' });
const agent = createTestAgent({ type: 'coder' });
mockAgentManager.selectAgent.mockResolvedValue(agent);
await orchestrator.executeTask(task);
expect(mockAgentManager.selectAgent).toHaveBeenCalledWith(task);
});
it('should handle agent selection failure', async () => {
const task = createTestTask();
mockAgentManager.selectAgent.mockRejectedValue(
new Error('No suitable agent')
);
await expect(orchestrator.executeTask(task)).rejects.toThrow(
'No suitable agent'
);
});
});
});
```
---
## Monitoring & Observability
### Metrics Collection
```typescript
class MetricsCollector {
private prometheus: PrometheusClient;
// Counter metrics
private taskCounter = new Counter({
name: 'claude_flow_tasks_total',
help: 'Total number of tasks processed',
labelNames: ['type', 'status']
});
// Gauge metrics
private activeAgents = new Gauge({
name: 'claude_flow_active_agents',
help: 'Number of active agents',
labelNames: ['type']
});
// Histogram metrics
private taskDuration = new Histogram({
name: 'claude_flow_task_duration_seconds',
help: 'Task execution duration',
labelNames: ['type'],
buckets: [0.1, 0.5, 1, 2, 5, 10, 30, 60]
});
recordTask(type: string, status: string, duration: number): void {
this.taskCounter.labels(type, status).inc();
this.taskDuration.labels(type).observe(duration);
}
updateActiveAgents(type: string, count: number): void {
this.activeAgents.labels(type).set(count);
}
}
```
### Logging Strategy
```typescript
class Logger {
private winston: Winston.Logger;
constructor() {
this.winston = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
defaultMeta: { service: 'claude-flow' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
new winston.transports.Console({
format: winston.format.simple()
})
]
});
}
info(message: string, meta?: any): void {
this.winston.info(message, meta);
}
error(message: string, error?: Error, meta?: any): void {
this.winston.error(message, { error: error?.stack, ...meta });
}
}
```
---
## Conclusion
The Claude-Flow architecture is designed to be:
1. **Scalable** - Horizontal scaling, sharding, and distributed processing
2. **Fault-Tolerant** - Circuit breakers, retry mechanisms, and redundancy
3. **Performant** - Caching, connection pooling, and async processing
4. **Maintainable** - Clean architecture, design patterns, and modularity
5. **Observable** - Comprehensive monitoring, logging, and metrics
This architecture enables Claude-Flow to handle enterprise-scale AI orchestration workloads while maintaining high availability and performance.
---
<div align="center">
**Claude-Flow Architecture v2.0.0**
[Back to README](../README.md) | [API Documentation](API_DOCUMENTATION.md)
</div>