ruvllm 2.2.0

LLM serving runtime with Ruvector integration - Paged attention, KV cache, and SONA learning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Claude Flow Integration for RuvLTRA
//!
//! Optimizes RuvLTRA-Small for Claude Flow use cases:
//! - Agent routing (task -> optimal agent type)
//! - Task classification (code/research/test/review)
//! - Semantic search (memory retrieval queries)
//! - Code generation (Rust/TypeScript output)
//! - HNSW-powered semantic routing (150x faster pattern search)
//! - ReasoningBank for intelligent pattern learning
//! - Multi-phase pretraining pipeline with curriculum learning
//! - **Full Claude API integration with streaming** (NEW)
//! - **Intelligent model routing (Haiku/Sonnet/Opus)** (NEW)
//!
//! ## Model Routing (NEW)
//!
//! Intelligent routing to optimal Claude model based on task complexity:
//!
//! | Model | Token Threshold | Complexity | Use Cases |
//! |-------|-----------------|------------|-----------|
//! | Haiku | < 500 tokens | Simple | Bug fixes, formatting, simple transforms |
//! | Sonnet | 500-2000 tokens | Moderate | Feature impl, refactoring, testing |
//! | Opus | > 2000 tokens | Complex | Architecture, security, deep reasoning |
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::{ModelRouter, SelectionCriteria, ClaudeModel};
//!
//! let mut router = ModelRouter::new();
//!
//! // Route task to optimal model
//! let decision = router.route("implement a REST API endpoint", None, None);
//! println!("Model: {:?}, cost: ${:.4}", decision.model, decision.estimated_cost);
//!
//! // With cost preference
//! router.set_criteria(SelectionCriteria { prefer_cost: true, ..Default::default() });
//! let decision = router.route("fix a typo", None, None);
//! assert_eq!(decision.model, ClaudeModel::Haiku);
//! ```
//!
//! ## Multi-Agent Coordination (NEW)
//!
//! The [`AgentCoordinator`] orchestrates multi-agent workflows:
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::{AgentCoordinator, ClaudeModel, AgentType, WorkflowStep};
//!
//! let mut coordinator = AgentCoordinator::new(ClaudeModel::Sonnet, 10);
//!
//! // Define workflow steps with dependencies
//! let steps = vec![
//!     WorkflowStep { step_id: "research".into(), agent_type: AgentType::Researcher, .. },
//!     WorkflowStep { step_id: "design".into(), agent_type: AgentType::Architect,
//!                    dependencies: vec!["research".into()], .. },
//!     WorkflowStep { step_id: "implement".into(), agent_type: AgentType::Coder,
//!                    dependencies: vec!["design".into()], .. },
//! ];
//!
//! // Execute with automatic dependency resolution
//! let result = coordinator.execute_workflow("my-workflow".into(), steps).await?;
//! println!("Total cost: ${:.4}", result.total_cost);
//! ```
//!
//! ## Advanced Pretraining Pipeline
//!
//! The [`PretrainPipeline`] provides a multi-phase pretraining system:
//!
//! - **Bootstrap Phase**: Seed patterns from agent keywords and typical tasks
//! - **Synthetic Phase**: Generate diverse training samples per agent type
//! - **Reinforce Phase**: Replay successful trajectories with SONA
//! - **Consolidate Phase**: EWC++ to lock in learned patterns
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::{PretrainPipeline, PretrainConfig, Phase};
//!
//! let config = PretrainConfig::for_claude_flow();
//! let mut pipeline = PretrainPipeline::new(config);
//!
//! // Run full pretraining
//! let result = pipeline.run_full_pipeline()?;
//! println!("Trained {} patterns with {:.2}% quality", result.total_patterns, result.avg_quality * 100.0);
//!
//! // Save checkpoint
//! pipeline.save_checkpoint("./checkpoints/claude_flow_v1.bin")?;
//! ```
//!
//! ## Task Generation
//!
//! The [`TaskGenerator`] creates realistic training data for pretraining:
//!
//! - Coding tasks: implement, fix, refactor, optimize
//! - Research tasks: analyze, investigate, explore
//! - Review tasks: audit, inspect, verify
//! - Architecture tasks: design, structure, plan
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::{TaskGenerator, TaskCategory, TaskComplexity};
//!
//! let mut generator = TaskGenerator::new();
//!
//! // Generate tasks for specific category
//! let task = generator.generate(TaskCategory::Coding, TaskComplexity::Moderate);
//! println!("Task: {}", task.description);
//!
//! // Generate for specific agent
//! let research_task = generator.generate_for_agent(ClaudeFlowAgent::Researcher, TaskComplexity::Complex);
//!
//! // Generate balanced batch
//! let tasks = generator.generate_balanced_batch(100);
//! ```
//!
//! ## HNSW Semantic Router
//!
//! The [`HnswRouter`] provides 150x faster pattern matching for task routing
//! using ruvector-core's HNSW index. It supports:
//!
//! - Semantic nearest-neighbor search for task patterns
//! - Online learning (add new patterns as tasks succeed)
//! - Integration with SONA for continuous improvement
//! - Hybrid routing combining keyword and semantic methods
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::{HnswRouter, HnswRouterConfig, TaskPattern, AgentType, ClaudeFlowTask};
//!
//! let config = HnswRouterConfig::default();
//! let router = HnswRouter::new(config)?;
//!
//! // Add learned patterns
//! let pattern = TaskPattern::new(
//!     embedding,
//!     AgentType::Coder,
//!     ClaudeFlowTask::CodeGeneration,
//!     "implement a function".to_string(),
//! );
//! router.add_pattern(pattern)?;
//!
//! // Route by semantic similarity
//! let result = router.route_by_similarity(&query_embedding)?;
//! println!("Best agent: {:?}, confidence: {}", result.primary_agent, result.confidence);
//! ```
//!
//! ## ReasoningBank Integration
//!
//! The [`ReasoningBankIntegration`] provides intelligent pattern learning with:
//!
//! - **Trajectory Storage**: Records task executions with verdict judgments (success/failure/partial)
//! - **Memory Distillation**: Extracts key patterns from multiple trajectories using K-means clustering
//! - **EWC++ Consolidation**: Prevents catastrophic forgetting of learned patterns
//! - **Pattern-based Routing**: Recommends agents based on historical successes
//!
//! ```rust,ignore
//! use ruvllm::claude_flow::{
//!     ReasoningBankIntegration, ReasoningBankConfig, Verdict, TrajectoryStep, AgentType
//! };
//!
//! let config = ReasoningBankConfig::default();
//! let bank = ReasoningBankIntegration::new(config);
//!
//! // Record a successful task execution
//! let steps = vec![
//!     TrajectoryStep::new("analyze_requirements", 0.8).with_agent(AgentType::Researcher),
//!     TrajectoryStep::new("implement_code", 0.9).with_agent(AgentType::Coder),
//!     TrajectoryStep::new("run_tests", 0.95).with_agent(AgentType::Tester),
//! ];
//! bank.record_trajectory(
//!     "task-123",
//!     &embedding,
//!     steps,
//!     Verdict::Success { reason: "All tests passed".into() },
//! ).unwrap();
//!
//! // Distill patterns after accumulating trajectories
//! bank.distill_patterns().unwrap();
//!
//! // Get routing recommendation for a new task
//! let rec = bank.get_recommendation(&new_embedding);
//! println!("Recommended: {:?} (confidence: {:.2})", rec.agent, rec.confidence);
//!
//! // Periodically consolidate to prevent forgetting
//! bank.consolidate().unwrap();
//! ```

use serde::{Deserialize, Serialize};

mod agent_router;
mod claude_integration;
mod flow_optimizer;
mod hnsw_router;
mod hooks_integration;
mod model_router;
mod pretrain_pipeline;
mod reasoning_bank;
mod task_classifier;
mod task_generator;

pub use agent_router::{AgentRouter, AgentType, RoutingDecision};
pub use flow_optimizer::{FlowOptimizer, OptimizationConfig, OptimizationResult};
pub use hnsw_router::{
    HnswDistanceMetric, HnswRouter, HnswRouterConfig, HnswRouterStats, HnswRoutingResult,
    HybridRouter, TaskPattern,
};
pub use pretrain_pipeline::{
    Checkpoint, CurriculumScheduler, CurriculumStats, Phase, PhaseResult, PipelineResult,
    PretrainConfig, PretrainPipeline, ProgressTracker, QualityGate, QualityGateStats,
    SerializedPattern,
};
pub use reasoning_bank::{
    DistilledPattern, ReasoningBankConfig, ReasoningBankIntegration, ReasoningBankStats,
    RoutingRecommendation, Trajectory, TrajectoryStep, Verdict,
};
pub use task_classifier::{ClassificationResult, TaskClassifier, TaskType};
pub use task_generator::{seed_rng, GeneratedTask, TaskCategory, TaskComplexity, TaskGenerator};

// Hooks Integration exports (NEW v2.3)
pub use hooks_integration::{
    HooksConfig, HooksIntegration, LearningMetrics, PatternMatch, PostEditInput, PostEditResult,
    PostTaskInput, PostTaskResult, PreEditInput, PreEditResult, PreTaskInput, PreTaskResult,
    QualityAssessment, SessionEndResult, SessionMetrics, SessionState,
};

// Claude API Integration exports (NEW)
pub use claude_integration::{
    AgentContext,
    AgentCoordinator,
    // Multi-agent coordination
    AgentState,
    // Core types
    ClaudeModel,
    ClaudeRequest,
    ClaudeResponse,
    ContentBlock,
    ContextManager,
    // Context management
    ContextWindow,
    CoordinatorStats,
    // Cost and latency tracking
    CostEstimator,
    LatencySample,
    LatencyStats,
    LatencyTracker,
    Message,
    MessageRole,
    QualityMonitor,
    ResponseStreamer,
    StepResult,
    StreamEvent,
    StreamStats,
    // Streaming
    StreamToken,
    UsageStats,
    WorkflowResult,
    WorkflowStep,
};

// Model Router exports (NEW)
pub use model_router::{
    AnalyzerStats,
    // Complexity analysis
    ComplexityFactors,
    ComplexityScore,
    ComplexityWeights,
    // Integrated router
    ModelRouter,
    ModelRoutingDecision,
    ModelSelector,
    // Model selection
    SelectionCriteria,
    SelectorStats,
    TaskComplexityAnalyzer,
};

/// Claude Flow agent types supported by RuvLTRA routing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ClaudeFlowAgent {
    /// Code implementation specialist
    Coder,
    /// Research and analysis specialist
    Researcher,
    /// Testing and validation specialist
    Tester,
    /// Code review specialist
    Reviewer,
    /// System architecture specialist
    Architect,
    /// Security audit specialist
    SecurityAuditor,
    /// Performance optimization specialist
    PerformanceEngineer,
    /// Machine learning specialist
    MlDeveloper,
    /// Backend development specialist
    BackendDev,
    /// CI/CD engineering specialist
    CicdEngineer,
}

impl ClaudeFlowAgent {
    /// Get all agent types
    pub fn all() -> &'static [ClaudeFlowAgent] {
        &[
            Self::Coder,
            Self::Researcher,
            Self::Tester,
            Self::Reviewer,
            Self::Architect,
            Self::SecurityAuditor,
            Self::PerformanceEngineer,
            Self::MlDeveloper,
            Self::BackendDev,
            Self::CicdEngineer,
        ]
    }

    /// Get agent name
    pub fn name(&self) -> &'static str {
        match self {
            Self::Coder => "coder",
            Self::Researcher => "researcher",
            Self::Tester => "tester",
            Self::Reviewer => "reviewer",
            Self::Architect => "system-architect",
            Self::SecurityAuditor => "security-auditor",
            Self::PerformanceEngineer => "performance-engineer",
            Self::MlDeveloper => "ml-developer",
            Self::BackendDev => "backend-dev",
            Self::CicdEngineer => "cicd-engineer",
        }
    }

    /// Get typical task keywords for this agent
    pub fn keywords(&self) -> &'static [&'static str] {
        match self {
            Self::Coder => &[
                "implement",
                "code",
                "write",
                "create",
                "build",
                "develop",
                "function",
                "class",
            ],
            Self::Researcher => &[
                "research",
                "analyze",
                "investigate",
                "explore",
                "find",
                "search",
                "understand",
            ],
            Self::Tester => &[
                "test",
                "verify",
                "validate",
                "check",
                "assert",
                "coverage",
                "unit",
                "integration",
            ],
            Self::Reviewer => &[
                "review",
                "audit",
                "inspect",
                "quality",
                "lint",
                "style",
                "best practice",
            ],
            Self::Architect => &[
                "design",
                "architecture",
                "structure",
                "pattern",
                "system",
                "scalable",
                "modular",
            ],
            Self::SecurityAuditor => &[
                "security",
                "vulnerability",
                "cve",
                "injection",
                "auth",
                "encrypt",
                "safe",
            ],
            Self::PerformanceEngineer => &[
                "performance",
                "optimize",
                "speed",
                "memory",
                "benchmark",
                "profile",
                "latency",
            ],
            Self::MlDeveloper => &[
                "model",
                "train",
                "neural",
                "ml",
                "ai",
                "embedding",
                "inference",
                "tensor",
            ],
            Self::BackendDev => &[
                "api", "endpoint", "database", "server", "rest", "graphql", "query",
            ],
            Self::CicdEngineer => &[
                "ci", "cd", "pipeline", "deploy", "workflow", "action", "build", "release",
            ],
        }
    }
}

/// Claude Flow task types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ClaudeFlowTask {
    /// Code generation task
    CodeGeneration,
    /// Code review task
    CodeReview,
    /// Testing task
    Testing,
    /// Research task
    Research,
    /// Documentation task
    Documentation,
    /// Debugging task
    Debugging,
    /// Refactoring task
    Refactoring,
    /// Security audit task
    Security,
    /// Performance optimization task
    Performance,
    /// Architecture design task
    Architecture,
}

impl ClaudeFlowTask {
    /// Get recommended agents for this task type
    pub fn recommended_agents(&self) -> &'static [ClaudeFlowAgent] {
        match self {
            Self::CodeGeneration => &[ClaudeFlowAgent::Coder, ClaudeFlowAgent::BackendDev],
            Self::CodeReview => &[ClaudeFlowAgent::Reviewer, ClaudeFlowAgent::SecurityAuditor],
            Self::Testing => &[ClaudeFlowAgent::Tester, ClaudeFlowAgent::Coder],
            Self::Research => &[ClaudeFlowAgent::Researcher, ClaudeFlowAgent::Architect],
            Self::Documentation => &[ClaudeFlowAgent::Researcher, ClaudeFlowAgent::Coder],
            Self::Debugging => &[ClaudeFlowAgent::Coder, ClaudeFlowAgent::Tester],
            Self::Refactoring => &[ClaudeFlowAgent::Coder, ClaudeFlowAgent::Architect],
            Self::Security => &[ClaudeFlowAgent::SecurityAuditor, ClaudeFlowAgent::Reviewer],
            Self::Performance => &[ClaudeFlowAgent::PerformanceEngineer, ClaudeFlowAgent::Coder],
            Self::Architecture => &[ClaudeFlowAgent::Architect, ClaudeFlowAgent::Reviewer],
        }
    }
}