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

341 lines
11 KiB
YAML

apiVersion: provisioning.vapora.io/v1
kind: Workflow
metadata:
name: upgrade-vapora
description: Rolling upgrade of VAPORA services with zero downtime
spec:
version: "0.2.0"
namespace: vapora-system
timeout: 1800s # 30 minutes max
inputs:
- name: backend_version
type: string
required: true
description: "Target version for backend service (e.g., 0.3.0)"
- name: frontend_version
type: string
required: true
description: "Target version for frontend service"
- name: agents_version
type: string
required: true
description: "Target version for agent runtime"
- name: upgrade_strategy
type: string
required: false
default: "rolling"
description: "rolling | blue-green | canary"
- name: skip_tests
type: boolean
required: false
default: false
description: "Skip smoke tests before upgrade"
- name: dry_run
type: boolean
required: false
default: false
description: "Perform dry-run without actual upgrades"
phases:
# Phase 1: Pre-upgrade checks
- name: "Pre-Upgrade Validation"
description: "Verify cluster health and prepare for upgrade"
retryable: true
steps:
- name: "Check cluster health"
command: "provisioning health-check --cluster"
timeout: 300s
- name: "Backup current state"
command: |
provisioning backup create --cluster vapora-cluster \
--label pre-upgrade-$(date +%Y%m%d-%H%M%S)
timeout: 600s
- name: "Verify all services are running"
command: "provisioning health-check --services all --strict"
timeout: 300s
- name: "Create git tag for current state"
command: |
CURRENT_BACKEND=$(kubectl get deployment vapora-backend -n vapora-system -o jsonpath='{.spec.template.spec.containers[0].image}')
git tag -a "pre-upgrade-$(echo $CURRENT_BACKEND | cut -d: -f2)" -m "Pre-upgrade checkpoint"
timeout: 60s
# Phase 2: Drain traffic gracefully
- name: "Prepare for Upgrade"
description: "Gracefully drain and prepare services for upgrade"
retryable: true
steps:
- name: "Drain agent queue"
command: |
provisioning agents drain --timeout 600s \
--allow-new-work false
timeout: 700s
- name: "Enable maintenance mode"
command: |
kubectl patch configmap vapora-config \
-n vapora-system \
-p '{"data":{"maintenance_mode":"true"}}'
timeout: 60s
- name: "Wait for in-flight requests to complete"
command: |
provisioning metrics wait-for \
--metric "http_requests_in_flight" \
--target 0 \
--timeout 300s
timeout: 320s
# Phase 3: Database migration (if needed)
- name: "Database Migrations"
description: "Apply database schema changes"
retryable: false
steps:
- name: "Create database backup"
command: |
provisioning db backup --database surrealdb \
--output backup-pre-upgrade-$(date +%s).sql
timeout: 600s
- name: "Run migration scripts"
command: |
for MIGRATION in scripts/migrations/v0.3.0/*.surql; do
echo "Running migration: $MIGRATION"
provisioning db execute --database surrealdb --file "$MIGRATION" || {
echo "Migration failed, restoring backup"
exit 1
}
done
timeout: 600s
- name: "Verify migration success"
command: "provisioning db verify --database surrealdb"
timeout: 300s
# Phase 4: Update backend service
- name: "Upgrade Backend Service"
description: "Rolling update of REST API backend"
retryable: true
steps:
- name: "Update backend image"
command: |
if [ "$DRY_RUN" = "true" ]; then
echo "[DRY-RUN] Would update backend to vapora/backend:$BACKEND_VERSION"
else
provisioning taskserv upgrade vapora-backend \
--image vapora/backend:$BACKEND_VERSION \
--strategy rolling \
--max-surge 1 \
--max-unavailable 0
fi
timeout: 600s
env:
- name: BACKEND_VERSION
value: "${backend_version}"
- name: DRY_RUN
value: "${dry_run}"
- name: "Wait for backend rollout"
command: "kubectl rollout status deployment/vapora-backend -n vapora-system --timeout=300s"
timeout: 320s
- name: "Run smoke tests"
command: |
if [ "$SKIP_TESTS" != "true" ]; then
provisioning test smoke --api http://vapora-backend.vapora-system:8080 \
--endpoints "/api/v1/health" "/api/v1/ready"
fi
timeout: 180s
env:
- name: SKIP_TESTS
value: "${skip_tests}"
continueOnError: true
# Phase 5: Update LLM Router and MCP Gateway
- name: "Upgrade Backend Components"
description: "Update LLM Router and MCP Gateway in parallel"
retryable: true
parallel: true
steps:
- name: "Upgrade LLM Router"
command: |
if [ "$DRY_RUN" != "true" ]; then
provisioning taskserv upgrade vapora-llm-router \
--image vapora/llm-router:$VERSION \
--strategy rolling \
--max-unavailable 0
fi
timeout: 600s
env:
- name: VERSION
value: "${backend_version}"
- name: "Upgrade MCP Gateway"
command: |
if [ "$DRY_RUN" != "true" ]; then
provisioning taskserv upgrade vapora-mcp-gateway \
--image vapora/mcp-gateway:$VERSION \
--strategy rolling \
--max-unavailable 0
fi
timeout: 600s
env:
- name: VERSION
value: "${backend_version}"
# Phase 6: Update agent runtime
- name: "Upgrade Agent Runtime"
description: "Update agent runtime with safe rollout"
retryable: true
steps:
- name: "Update agent image"
command: |
if [ "$DRY_RUN" != "true" ]; then
provisioning taskserv upgrade vapora-agents \
--image vapora/agents:$VERSION \
--strategy rolling \
--max-surge 1 \
--max-unavailable 1 \
--drain-timeout 300s
fi
timeout: 900s
env:
- name: VERSION
value: "${agents_version}"
- name: "Wait for agents to stabilize"
command: |
kubectl wait --for=condition=Ready pod \
-l app=vapora-agents \
-n vapora-system \
--timeout=600s
timeout: 620s
# Phase 7: Update frontend service
- name: "Upgrade Frontend Service"
description: "Update UI frontend with minimal user impact"
retryable: true
steps:
- name: "Update frontend image"
command: |
if [ "$DRY_RUN" != "true" ]; then
provisioning taskserv upgrade vapora-frontend \
--image vapora/frontend:$VERSION \
--strategy rolling \
--max-surge 1 \
--max-unavailable 0
fi
timeout: 600s
env:
- name: VERSION
value: "${frontend_version}"
- name: "Wait for frontend rollout"
command: "kubectl rollout status deployment/vapora-frontend -n vapora-system --timeout=300s"
timeout: 320s
- name: "Test frontend endpoints"
command: |
if [ "$SKIP_TESTS" != "true" ]; then
provisioning test smoke --frontend http://vapora-frontend.vapora-system:3000 \
--endpoints "/"
fi
timeout: 180s
# Phase 8: Post-upgrade verification
- name: "Post-Upgrade Verification"
description: "Comprehensive validation of upgraded system"
retryable: false
steps:
- name: "Disable maintenance mode"
command: |
kubectl patch configmap vapora-config \
-n vapora-system \
-p '{"data":{"maintenance_mode":"false"}}'
timeout: 60s
- name: "Health check all services"
command: "provisioning health-check --services all --strict"
timeout: 300s
- name: "Verify agent communication"
command: "provisioning agents health-check --nats nats://nats-0.vapora-system:4222"
timeout: 120s
- name: "Run integration tests"
command: "provisioning test integration --timeout 600s"
timeout: 620s
continueOnError: true
- name: "Check application logs for errors"
command: |
ERROR_COUNT=$(kubectl logs -n vapora-system -l app=vapora-backend --tail=1000 | grep -c 'ERROR\|CRITICAL')
if [ "$ERROR_COUNT" -gt 10 ]; then
echo "WARNING: Found $ERROR_COUNT errors in backend logs"
exit 1
fi
timeout: 120s
continueOnError: true
- name: "Re-enable agent work"
command: "provisioning agents drain --disable"
timeout: 60s
# Phase 9: Tag and document upgrade
- name: "Finalize Upgrade"
description: "Document upgrade completion"
retryable: false
steps:
- name: "Create upgrade completion tag"
command: |
git tag -a "upgraded-to-$BACKEND_VERSION-$(date +%Y%m%d-%H%M%S)" \
-m "Upgrade completed: backend=$BACKEND_VERSION, frontend=$FRONTEND_VERSION, agents=$AGENTS_VERSION"
timeout: 60s
env:
- name: BACKEND_VERSION
value: "${backend_version}"
- name: FRONTEND_VERSION
value: "${frontend_version}"
- name: AGENTS_VERSION
value: "${agents_version}"
- name: "Generate upgrade report"
command: |
provisioning report generate \
--type upgrade \
--format markdown \
--output "upgrade-report-$(date +%Y%m%d-%H%M%S).md"
timeout: 120s
# Rollback procedure
onFailure:
rollback: true
procedure:
- name: "Restore from pre-upgrade backup"
command: "provisioning backup restore --label pre-upgrade-* --latest"
- name: "Verify rollback success"
command: "provisioning health-check --cluster"
outputs:
- name: upgrade_status
value: "echo 'Upgrade completed successfully'"
- name: versions_deployed
command: "kubectl get deployment -n vapora-system -o wide"
notifications:
onStart:
- "slack: #deployment"
- "email: devops@example.com"
onSuccess:
- "slack: #deployment"
- "slack: notify: Upgrade successful"
onFailure:
- "slack: #deployment"
- "slack: #alerts"
- "email: devops@example.com"
- "severity: critical"