Jesús Pérez d14150da75 feat: Phase 5.3 - Multi-Agent Learning Infrastructure
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.
2026-01-11 13:03:53 +00:00

324 lines
9.9 KiB
Plaintext

"""
VAPORA Storage Configuration
Defines SurrealDB, Redis, NATS, and persistent volumes
"""
import k.api.all as k
# ===== SURREALDB STATEFULSET =====
surrealdb_statefulset = k.StatefulSet {
apiVersion = "apps/v1"
kind = "StatefulSet"
metadata = {
name = "surrealdb"
namespace = "vapora-system"
labels = {"app": "surrealdb"}
}
spec = {
serviceName = "surrealdb"
replicas = 3
selector = {matchLabels = {"app": "surrealdb"}}
template = {
metadata = {labels = {"app": "surrealdb"}}
spec = {
containers = [{
name = "surrealdb"
image = "surrealdb/surrealdb:1.8"
imagePullPolicy = "IfNotPresent"
ports = [{name = "http", containerPort = 8000}]
args = [
"start",
"file:/data/vapora",
"--auth",
"--user", "root",
"--pass", "$(SURREAL_PASSWORD)"
]
env = [
{name = "SURREAL_PASSWORD", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "surrealdb-password"}}}
{name = "RUST_LOG", value = "info"}
]
livenessProbe = {
httpGet = {path = "/health", port = 8000}
initialDelaySeconds = 30
periodSeconds = 10
}
readinessProbe = {
httpGet = {path = "/health", port = 8000}
initialDelaySeconds = 10
periodSeconds = 5
}
resources = {
requests = {cpu = "2000m", memory = "4Gi"}
limits = {cpu = "4000m", memory = "8Gi"}
}
volumeMounts = [
{name = "data", mountPath = "/data"}
]
}]
}
}
volumeClaimTemplates = [{
metadata = {name = "data"}
spec = {
accessModes = ["ReadWriteOnce"]
storageClassName = "ssd"
resources = {requests = {storage = "50Gi"}}
}
}]
}
}
surrealdb_service = k.Service {
apiVersion = "v1"
kind = "Service"
metadata = {name = "surrealdb", namespace = "vapora-system"}
spec = {
clusterIP = "None" # Headless service
ports = [{name = "http", port = 8000, targetPort = 8000}]
selector = {"app": "surrealdb"}
}
}
# ===== REDIS STATEFULSET =====
redis_statefulset = k.StatefulSet {
apiVersion = "apps/v1"
kind = "StatefulSet"
metadata = {
name = "redis"
namespace = "vapora-system"
labels = {"app": "redis"}
}
spec = {
serviceName = "redis"
replicas = 3
selector = {matchLabels = {"app": "redis"}}
template = {
metadata = {labels = {"app": "redis"}}
spec = {
containers = [{
name = "redis"
image = "redis:7.2-alpine"
imagePullPolicy = "IfNotPresent"
ports = [{name = "redis", containerPort = 6379}]
command = [
"redis-server",
"--masterauth", "$(REDIS_PASSWORD)",
"--requirepass", "$(REDIS_PASSWORD)",
"--appendonly", "yes",
"--replicaof", "redis-0.redis.vapora-system.svc.cluster.local", "6379"
]
env = [
{name = "REDIS_PASSWORD", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "redis-password"}}}
]
livenessProbe = {
exec = {command = ["redis-cli", "ping"]}
initialDelaySeconds = 30
periodSeconds = 10
}
readinessProbe = {
exec = {command = ["redis-cli", "ping"]}
initialDelaySeconds = 10
periodSeconds = 5
}
resources = {
requests = {cpu = "1000m", memory = "2Gi"}
limits = {cpu = "2000m", memory = "4Gi"}
}
volumeMounts = [
{name = "data", mountPath = "/data"}
]
}]
}
}
volumeClaimTemplates = [{
metadata = {name = "data"}
spec = {
accessModes = ["ReadWriteOnce"]
storageClassName = "ssd"
resources = {requests = {storage = "20Gi"}}
}
}]
}
}
redis_service = k.Service {
apiVersion = "v1"
kind = "Service"
metadata = {name = "redis", namespace = "vapora-system"}
spec = {
clusterIP = "None" # Headless service
ports = [{name = "redis", port = 6379, targetPort = 6379}]
selector = {"app": "redis"}
}
}
# ===== NATS JETSTREAM STATEFULSET =====
nats_statefulset = k.StatefulSet {
apiVersion = "apps/v1"
kind = "StatefulSet"
metadata = {
name = "nats"
namespace = "vapora-system"
labels = {"app": "nats"}
}
spec = {
serviceName = "nats"
replicas = 3
selector = {matchLabels = {"app": "nats"}}
template = {
metadata = {labels = {"app": "nats"}}
spec = {
containers = [{
name = "nats"
image = "nats:2.10-alpine"
imagePullPolicy = "IfNotPresent"
ports = [
{name = "client", containerPort = 4222}
{name = "cluster", containerPort = 6222}
{name = "monitor", containerPort = 8222}
]
command = ["nats-server"]
args = [
"-c", "/etc/nats/nats.conf"
]
livenessProbe = {
httpGet = {path = "/varz", port = 8222}
initialDelaySeconds = 30
periodSeconds = 10
}
readinessProbe = {
exec = {command = ["nats", "-s", "nats://localhost:4222", "server", "info"]}
initialDelaySeconds = 10
periodSeconds = 5
}
resources = {
requests = {cpu = "1000m", memory = "2Gi"}
limits = {cpu = "2000m", memory = "4Gi"}
}
volumeMounts = [
{name = "config", mountPath = "/etc/nats", readOnly = true}
{name = "data", mountPath = "/var/lib/nats"}
]
}]
volumes = [{
name = "config"
configMap = {name = "nats-config"}
}]
}
}
volumeClaimTemplates = [{
metadata = {name = "data"}
spec = {
accessModes = ["ReadWriteOnce"]
storageClassName = "ssd"
resources = {requests = {storage = "30Gi"}}
}
}]
}
}
nats_service = k.Service {
apiVersion = "v1"
kind = "Service"
metadata = {name = "nats", namespace = "vapora-system"}
spec = {
clusterIP = "None" # Headless service
ports = [
{name = "client", port = 4222, targetPort = 4222}
{name = "cluster", port = 6222, targetPort = 6222}
{name = "monitor", port = 8222, targetPort = 8222}
]
selector = {"app": "nats"}
}
}
# ===== PERSISTENT VOLUME CLAIMS =====
pvc_agent_state = k.PersistentVolumeClaim {
apiVersion = "v1"
kind = "PersistentVolumeClaim"
metadata = {
name = "vapora-agent-state-pvc"
namespace = "vapora-system"
}
spec = {
accessModes = ["ReadWriteMany"]
storageClassName = "ssd"
resources = {requests = {storage = "20Gi"}}
}
}
pvc_routing_cache = k.PersistentVolumeClaim {
apiVersion = "v1"
kind = "PersistentVolumeClaim"
metadata = {
name = "vapora-routing-cache-pvc"
namespace = "vapora-system"
}
spec = {
accessModes = ["ReadWriteOnce"]
storageClassName = "ssd"
resources = {requests = {storage = "5Gi"}}
}
}
pvc_plugins = k.PersistentVolumeClaim {
apiVersion = "v1"
kind = "PersistentVolumeClaim"
metadata = {
name = "vapora-plugins-pvc"
namespace = "vapora-system"
}
spec = {
accessModes = ["ReadWriteMany"]
storageClassName = "ssd"
resources = {requests = {storage = "10Gi"}}
}
}
# ===== NATS CONFIG MAP =====
nats_config = k.ConfigMap {
apiVersion = "v1"
kind = "ConfigMap"
metadata = {
name = "nats-config"
namespace = "vapora-system"
}
data = {
"nats.conf" = """
port: 4222
cluster {
port: 6222
routes: [
nats://nats-0.nats.vapora-system.svc.cluster.local:6222
nats://nats-1.nats.vapora-system.svc.cluster.local:6222
nats://nats-2.nats.vapora-system.svc.cluster.local:6222
]
}
jetstream {
store_dir: /var/lib/nats
max_memory_store: 8GB
max_file_store: 30GB
}
monitor_port: 8222
"""
}
}
# ===== OUTPUT =====
output = {
surrealdb = {statefulset = surrealdb_statefulset, service = surrealdb_service}
redis = {statefulset = redis_statefulset, service = redis_service}
nats = {statefulset = nats_statefulset, service = nats_service, config = nats_config}
pvcs = {
agent_state = pvc_agent_state
routing_cache = pvc_routing_cache
plugins = pvc_plugins
}
}