Implement intelligent agent learning from Knowledge Graph execution history with per-task-type expertise tracking, recency bias, and learning curves. ## Phase 5.3 Implementation ### Learning Infrastructure (✅ Complete) - LearningProfileService with per-task-type expertise metrics - TaskTypeExpertise model tracking success_rate, confidence, learning curves - Recency bias weighting: recent 7 days weighted 3x higher (exponential decay) - Confidence scoring prevents overfitting: min(1.0, executions / 20) - Learning curves computed from daily execution windows ### Agent Scoring Service (✅ Complete) - Unified AgentScore combining SwarmCoordinator + learning profiles - Scoring formula: 0.3*base + 0.5*expertise + 0.2*confidence - Rank agents by combined score for intelligent assignment - Support for recency-biased scoring (recent_success_rate) - Methods: rank_agents, select_best, rank_agents_with_recency ### KG Integration (✅ Complete) - KGPersistence::get_executions_for_task_type() - query by agent + task type - KGPersistence::get_agent_executions() - all executions for agent - Coordinator::load_learning_profile_from_kg() - core KG→Learning integration - Coordinator::load_all_learning_profiles() - batch load for multiple agents - Convert PersistedExecution → ExecutionData for learning calculations ### Agent Assignment Integration (✅ Complete) - AgentCoordinator uses learning profiles for task assignment - extract_task_type() infers task type from title/description - assign_task() scores candidates using AgentScoringService - Fallback to load-based selection if no learning data available - Learning profiles stored in coordinator.learning_profiles RwLock ### Profile Adapter Enhancements (✅ Complete) - create_learning_profile() - initialize empty profiles - add_task_type_expertise() - set task-type expertise - update_profile_with_learning() - update swarm profiles from learning ## Files Modified ### vapora-knowledge-graph/src/persistence.rs (+30 lines) - get_executions_for_task_type(agent_id, task_type, limit) - get_agent_executions(agent_id, limit) ### vapora-agents/src/coordinator.rs (+100 lines) - load_learning_profile_from_kg() - core KG integration method - load_all_learning_profiles() - batch loading for agents - assign_task() already uses learning-based scoring via AgentScoringService ### Existing Complete Implementation - vapora-knowledge-graph/src/learning.rs - calculation functions - vapora-agents/src/learning_profile.rs - data structures and expertise - vapora-agents/src/scoring.rs - unified scoring service - vapora-agents/src/profile_adapter.rs - adapter methods ## Tests Passing - learning_profile: 7 tests ✅ - scoring: 5 tests ✅ - profile_adapter: 6 tests ✅ - coordinator: learning-specific tests ✅ ## Data Flow 1. Task arrives → AgentCoordinator::assign_task() 2. Extract task_type from description 3. Query KG for task-type executions (load_learning_profile_from_kg) 4. Calculate expertise with recency bias 5. Score candidates (SwarmCoordinator + learning) 6. Assign to top-scored agent 7. Execution result → KG → Update learning profiles ## Key Design Decisions ✅ Recency bias: 7-day half-life with 3x weight for recent performance ✅ Confidence scoring: min(1.0, total_executions / 20) prevents overfitting ✅ Hierarchical scoring: 30% base load, 50% expertise, 20% confidence ✅ KG query limit: 100 recent executions per task-type for performance ✅ Async loading: load_learning_profile_from_kg supports concurrent loads ## Next: Phase 5.4 - Cost Optimization Ready to implement budget enforcement and cost-aware provider selection.
364 lines
11 KiB
Rust
364 lines
11 KiB
Rust
// Integration tests for Phase 3: Workflow orchestration
|
|
// Tests the complete workflow system end-to-end
|
|
|
|
use std::sync::Arc;
|
|
use vapora_agents::{coordinator::AgentCoordinator, registry::AgentRegistry};
|
|
use vapora_backend::{
|
|
api::websocket::WorkflowBroadcaster,
|
|
audit::AuditTrail,
|
|
services::WorkflowService,
|
|
workflow::{
|
|
engine::WorkflowEngine,
|
|
executor::StepExecutor,
|
|
parser::WorkflowParser,
|
|
scheduler::Scheduler,
|
|
state::{Phase, StepStatus, Workflow, WorkflowStatus, WorkflowStep},
|
|
},
|
|
};
|
|
|
|
#[tokio::test]
|
|
async fn test_workflow_state_transitions() {
|
|
let mut workflow = Workflow::new("wf-1".to_string(), "Test Workflow".to_string(), vec![]);
|
|
|
|
// Test valid transitions
|
|
assert!(workflow.transition(WorkflowStatus::Planning).is_ok());
|
|
assert_eq!(workflow.status, WorkflowStatus::Planning);
|
|
|
|
assert!(workflow.transition(WorkflowStatus::InProgress).is_ok());
|
|
assert_eq!(workflow.status, WorkflowStatus::InProgress);
|
|
assert!(workflow.started_at.is_some());
|
|
|
|
assert!(workflow.transition(WorkflowStatus::Completed).is_ok());
|
|
assert_eq!(workflow.status, WorkflowStatus::Completed);
|
|
assert!(workflow.completed_at.is_some());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_workflow_parser() {
|
|
let yaml = r#"
|
|
workflow:
|
|
id: test-workflow
|
|
title: Test Workflow
|
|
phases:
|
|
- id: phase1
|
|
name: Design Phase
|
|
parallel: false
|
|
estimated_hours: 2.0
|
|
steps:
|
|
- id: step1
|
|
name: Create design
|
|
agent: architect
|
|
depends_on: []
|
|
parallelizable: false
|
|
- id: phase2
|
|
name: Implementation
|
|
parallel: true
|
|
estimated_hours: 8.0
|
|
steps:
|
|
- id: step2
|
|
name: Implement backend
|
|
agent: developer
|
|
depends_on: []
|
|
parallelizable: true
|
|
- id: step3
|
|
name: Implement frontend
|
|
agent: developer
|
|
depends_on: []
|
|
parallelizable: true
|
|
"#;
|
|
|
|
let result = WorkflowParser::parse_string(yaml);
|
|
assert!(result.is_ok());
|
|
|
|
let workflow = result.unwrap();
|
|
assert_eq!(workflow.id, "test-workflow");
|
|
assert_eq!(workflow.phases.len(), 2);
|
|
assert!(workflow.phases[1].parallel);
|
|
assert_eq!(workflow.phases[1].steps.len(), 2);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_dependency_resolution() {
|
|
let steps = vec![
|
|
WorkflowStep {
|
|
id: "a".to_string(),
|
|
name: "Step A".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec![],
|
|
can_parallelize: true,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
WorkflowStep {
|
|
id: "b".to_string(),
|
|
name: "Step B".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec!["a".to_string()],
|
|
can_parallelize: true,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
WorkflowStep {
|
|
id: "c".to_string(),
|
|
name: "Step C".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec!["a".to_string()],
|
|
can_parallelize: true,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
];
|
|
|
|
let result = Scheduler::resolve_dependencies(&steps);
|
|
assert!(result.is_ok());
|
|
|
|
let levels = result.unwrap();
|
|
assert_eq!(levels.len(), 2);
|
|
assert_eq!(levels[0], vec!["a"]);
|
|
assert_eq!(levels[1].len(), 2); // b and c can execute in parallel
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_workflow_engine() {
|
|
let registry = Arc::new(AgentRegistry::new(5));
|
|
let coordinator = Arc::new(AgentCoordinator::new(registry));
|
|
let executor = StepExecutor::new(coordinator);
|
|
let engine = WorkflowEngine::new(executor);
|
|
|
|
let workflow = Workflow::new(
|
|
"engine-test".to_string(),
|
|
"Engine Test".to_string(),
|
|
vec![Phase {
|
|
id: "p1".to_string(),
|
|
name: "Phase 1".to_string(),
|
|
status: StepStatus::Pending,
|
|
parallel: false,
|
|
estimated_hours: 1.0,
|
|
steps: vec![WorkflowStep {
|
|
id: "s1".to_string(),
|
|
name: "Step 1".to_string(),
|
|
agent_role: "developer".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec![],
|
|
can_parallelize: true,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
}],
|
|
}],
|
|
);
|
|
|
|
let id = workflow.id.clone();
|
|
let result = engine.register_workflow(workflow).await;
|
|
assert!(result.is_ok());
|
|
|
|
let retrieved = engine.get_workflow(&id).await;
|
|
assert!(retrieved.is_some());
|
|
assert_eq!(retrieved.unwrap().id, id);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_workflow_service_integration() {
|
|
let registry = Arc::new(AgentRegistry::new(5));
|
|
let coordinator = Arc::new(AgentCoordinator::new(registry));
|
|
let executor = StepExecutor::new(coordinator);
|
|
let engine = Arc::new(WorkflowEngine::new(executor));
|
|
let broadcaster = Arc::new(WorkflowBroadcaster::new());
|
|
let audit = Arc::new(AuditTrail::new());
|
|
|
|
let service = WorkflowService::new(engine, broadcaster, audit.clone());
|
|
|
|
let workflow = Workflow::new(
|
|
"service-test".to_string(),
|
|
"Service Test".to_string(),
|
|
vec![Phase {
|
|
id: "p1".to_string(),
|
|
name: "Test Phase".to_string(),
|
|
status: StepStatus::Pending,
|
|
parallel: false,
|
|
estimated_hours: 1.0,
|
|
steps: vec![],
|
|
}],
|
|
);
|
|
|
|
// Need at least one step for valid workflow
|
|
let workflow = Workflow::new(
|
|
"service-test".to_string(),
|
|
"Service Test".to_string(),
|
|
vec![Phase {
|
|
id: "p1".to_string(),
|
|
name: "Test Phase".to_string(),
|
|
status: StepStatus::Pending,
|
|
parallel: false,
|
|
estimated_hours: 1.0,
|
|
steps: vec![WorkflowStep {
|
|
id: "s1".to_string(),
|
|
name: "Test Step".to_string(),
|
|
agent_role: "developer".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec![],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
}],
|
|
}],
|
|
);
|
|
|
|
let id = workflow.id.clone();
|
|
let result = service.create_workflow(workflow).await;
|
|
assert!(result.is_ok());
|
|
|
|
// Check audit trail
|
|
let audit_entries = service.get_audit_trail(&id).await;
|
|
assert!(!audit_entries.is_empty());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_websocket_broadcaster() {
|
|
let broadcaster = WorkflowBroadcaster::new();
|
|
let mut rx = broadcaster.subscribe();
|
|
|
|
let update = vapora_backend::api::websocket::WorkflowUpdate::new(
|
|
"wf-1".to_string(),
|
|
"in_progress".to_string(),
|
|
50,
|
|
"Test update".to_string(),
|
|
);
|
|
|
|
broadcaster.send_update(update.clone());
|
|
|
|
let received = rx.recv().await.unwrap();
|
|
assert_eq!(received.workflow_id, "wf-1");
|
|
assert_eq!(received.progress, 50);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_audit_trail() {
|
|
let audit = AuditTrail::new();
|
|
|
|
audit
|
|
.log_event(
|
|
"wf-1".to_string(),
|
|
"workflow_started".to_string(),
|
|
"system".to_string(),
|
|
serde_json::json!({"test": "data"}),
|
|
)
|
|
.await;
|
|
|
|
let entries = audit.get_workflow_audit("wf-1").await;
|
|
assert_eq!(entries.len(), 1);
|
|
assert_eq!(entries[0].event_type, "workflow_started");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_circular_dependency_detection() {
|
|
let steps = vec![
|
|
WorkflowStep {
|
|
id: "a".to_string(),
|
|
name: "A".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec!["c".to_string()],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
WorkflowStep {
|
|
id: "b".to_string(),
|
|
name: "B".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec!["a".to_string()],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
WorkflowStep {
|
|
id: "c".to_string(),
|
|
name: "C".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec!["b".to_string()],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
];
|
|
|
|
let result = Scheduler::resolve_dependencies(&steps);
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_workflow_progress_calculation() {
|
|
let workflow = Workflow::new(
|
|
"progress-test".to_string(),
|
|
"Progress Test".to_string(),
|
|
vec![Phase {
|
|
id: "p1".to_string(),
|
|
name: "Phase 1".to_string(),
|
|
status: StepStatus::Running,
|
|
parallel: false,
|
|
estimated_hours: 1.0,
|
|
steps: vec![
|
|
WorkflowStep {
|
|
id: "s1".to_string(),
|
|
name: "Step 1".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Completed,
|
|
depends_on: vec![],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
WorkflowStep {
|
|
id: "s2".to_string(),
|
|
name: "Step 2".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Running,
|
|
depends_on: vec![],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
WorkflowStep {
|
|
id: "s3".to_string(),
|
|
name: "Step 3".to_string(),
|
|
agent_role: "dev".to_string(),
|
|
status: StepStatus::Pending,
|
|
depends_on: vec![],
|
|
can_parallelize: false,
|
|
started_at: None,
|
|
completed_at: None,
|
|
result: None,
|
|
error: None,
|
|
},
|
|
],
|
|
}],
|
|
);
|
|
|
|
assert_eq!(workflow.progress_percent(), 33); // 1 of 3 completed
|
|
}
|