File size: 13,947 Bytes
ed1b365 | 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 | """Phase 7: Executive Control Architecture — Intelligent component routing
This module implements the decision-making layer that routes queries to the optimal
combination of Phase 1-6 components, preventing wasteful activation and improving
latency while maintaining reasoning quality.
Core Philosophy: "Right-sized reasoning for right-sized questions"
- SIMPLE queries bypass heavy machinery
- MEDIUM queries activate selective components
- COMPLEX queries use full Phase 1-6 capabilities
Author: Jonathan Harrison (Codette Framework)
"""
import time
from typing import Dict, List, Optional, Set
from dataclasses import dataclass, field, asdict
from reasoning_forge.query_classifier import QueryComplexity
@dataclass
class ComponentDecision:
"""Routing decision for which Phase 1-6 components to activate."""
# Routing metadata
query_complexity: QueryComplexity
component_activation: Dict[str, bool] # e.g., {"debate": True, "semantic_tension": False}
component_config: Dict[str, any] = field(default_factory=dict) # e.g., {"debate_rounds": 1}
reasoning: str = "" # Why this routing was chosen
# Transparency
estimated_latency_ms: float = 0.0 # Expected latency
estimated_correctness: float = 0.5 # Expected correctness (0-1)
estimated_compute_cost: float = 0.0 # Relative cost (1-100)
class ExecutiveController:
"""Phase 7: Intelligent routing of queries to optimal component combinations.
This replaces the "all-systems-go" approach with targeted component activation.
Simple factual queries skip heavy machinery; complex queries use full power.
Usage:
exec_ctrl = ExecutiveController()
decision = exec_ctrl.route_query(query)
# Use decision to activate only selected components
if decision.component_activation['debate']:
result = forge.forge_with_debate(query, rounds=decision.component_config.get('debate_rounds', 1))
"""
def __init__(self, verbose: bool = False):
self.verbose = verbose
# Learned routing patterns (initially empty, updated from memory)
self.routing_patterns: Dict[str, ComponentDecision] = {}
# Statistics
self.queries_routed = 0
self.route_activation_counts = {} # Track which components get used
def route_query(self, query: str, complexity: QueryComplexity) -> ComponentDecision:
"""Route a query to optimal component combination.
Args:
query: The user query
complexity: QueryComplexity classification from Phase 6
Returns:
ComponentDecision with activation flags and configuration
"""
self.queries_routed += 1
if complexity == QueryComplexity.SIMPLE:
return self._route_simple(query)
elif complexity == QueryComplexity.MEDIUM:
return self._route_medium(query)
else: # COMPLEX
return self._route_complex(query)
def _route_simple(self, query: str) -> ComponentDecision:
"""Route SIMPLE queries: skip heavy machinery.
SIMPLE queries are factual (e.g., "speed of light", "definition of entropy").
They should get fast, direct answers without debate or heavy synthesis.
Cost: ~3 units (classifier + router)
Latency: ~150ms
Correctness: 0.95 (factual answers are well-established)
"""
decision = ComponentDecision(
query_complexity=QueryComplexity.SIMPLE,
component_activation={
'debate': False,
'semantic_tension': False,
'specialization_tracking': False,
'preflight_predictor': False,
'memory_weighting': False,
'gamma_monitoring': False,
'synthesis': False, # Direct answer only
},
component_config={},
reasoning="SIMPLE factual query - avoided heavy machinery for speed",
estimated_latency_ms=150,
estimated_correctness=0.95,
estimated_compute_cost=3,
)
self._record_routing(decision)
return decision
def _route_medium(self, query: str) -> ComponentDecision:
"""Route MEDIUM queries: selective Phase 1-6 components.
MEDIUM queries need some reasoning depth but don't require full debate.
Examples: "How does X relate to Y", "What are the implications of Z"
Activate:
- semantic_tension: Continuous conflict strength (vs discrete)
- debate: 1 round only (faster than 3)
- specialization_tracking: Measure adapter fit
- memory_weighting: Use learned adapter weights
Skip:
- preflight_predictor: Unnecessary for simpler queries
Cost: ~25 units
Latency: ~900ms (1-round debate)
Correctness: 0.80
"""
decision = ComponentDecision(
query_complexity=QueryComplexity.MEDIUM,
component_activation={
'debate': True,
'semantic_tension': True,
'specialization_tracking': True,
'preflight_predictor': False, # Skip for speed
'memory_weighting': True,
'gamma_monitoring': True,
'synthesis': True,
},
component_config={
'debate_rounds': 1, # Single round for speed
'max_conflicts': 12, # Cap conflicts
'min_conflict_threshold': 0.2,
},
reasoning="MEDIUM complexity - selective debate with semantic tension",
estimated_latency_ms=900,
estimated_correctness=0.80,
estimated_compute_cost=25,
)
self._record_routing(decision)
return decision
def _route_complex(self, query: str) -> ComponentDecision:
"""Route COMPLEX queries: full Phase 1-6 machinery.
COMPLEX queries need deep reasoning, multiple perspectives, and conflict analysis.
Examples: "Can machines be conscious?", "Ethical implications of AGI"
Activate all Phase 1-6 components:
- debate: 3 rounds for deep exploration
- semantic_tension: Advanced conflict strength calculation
- preflight_predictor: Predict conflicts before debate
- specialization_tracking: Measure domain expertise
- memory_weighting: Apply learned adapter weights
- gamma_monitoring: Real-time coherence monitoring
Cost: ~50+ units
Latency: ~2500ms (3-round debate)
Correctness: 0.85+
"""
decision = ComponentDecision(
query_complexity=QueryComplexity.COMPLEX,
component_activation={
'debate': True,
'semantic_tension': True,
'specialization_tracking': True,
'preflight_predictor': True,
'memory_weighting': True,
'gamma_monitoring': True,
'synthesis': True,
},
component_config={
'debate_rounds': 3, # Full exploration
'max_conflicts': 20, # Allow more conflicts for complex problems
'min_conflict_threshold': 0.15,
'semantic_tension_threshold': 0.3,
},
reasoning="COMPLEX query - full Phase 1-6 machinery for deep synthesis",
estimated_latency_ms=2500,
estimated_correctness=0.85,
estimated_compute_cost=50,
)
self._record_routing(decision)
return decision
def _record_routing(self, decision: ComponentDecision):
"""Track which routing decisions are being made."""
for component, active in decision.component_activation.items():
if active:
self.route_activation_counts[component] = \
self.route_activation_counts.get(component, 0) + 1
def get_routing_statistics(self) -> Dict:
"""Get statistics about routing decisions made so far.
Returns:
{
'total_queries_routed': int,
'component_activation_counts': {component: count, ...},
'avg_latency_by_complexity': {SIMPLE: ms, MEDIUM: ms, COMPLEX: ms},
'efficiency_gain': float (expected compute savings)
}
"""
total_cost_full_stack = self.queries_routed * 50 # All queries with full machinery
# Estimate cost savings from actual routing
estimated_cost_actual = 0
return {
'total_queries_routed': self.queries_routed,
'component_activation_counts': self.route_activation_counts.copy(),
'efficiency_gain': f"Estimated {((total_cost_full_stack - estimated_cost_actual) / total_cost_full_stack * 100):.1f}% compute savings",
}
@staticmethod
def create_route_metadata(decision: ComponentDecision,
actual_latency_ms: float,
actual_conflicts: int = 0,
gamma: float = 0.5) -> Dict:
"""Create metadata dictionary for response transparency.
This metadata tells users which components ran and why, making the
system's reasoning transparent.
Args:
decision: The ComponentDecision that was executed
actual_latency_ms: Measured latency from execution
actual_conflicts: Number of conflicts detected
gamma: Coherence score from ConflenceField
Returns:
Dictionary with routing transparency info for response
"""
return {
'phase7_routing': {
'query_complexity': decision.query_complexity.value,
'components_activated': {
k: v for k, v in decision.component_activation.items()
},
'reasoning': decision.reasoning,
'latency_analysis': {
'estimated_ms': decision.estimated_latency_ms,
'actual_ms': actual_latency_ms,
'savings_ms': max(0, decision.estimated_latency_ms - actual_latency_ms),
},
'correctness_estimate': decision.estimated_correctness,
'compute_cost': {
'estimated_units': decision.estimated_compute_cost,
'unit_scale': '1=classifier, 50=full_machinery',
},
'metrics': {
'conflicts_detected': actual_conflicts,
'gamma_coherence': gamma,
}
}
}
class ExecutiveControllerWithLearning(ExecutiveController):
"""Extended Executive Controller with learning from historical routing decisions.
This version learns which component combinations work best and adapts routing
over time based on actual correctness measurements.
Usage:
ctrl = ExecutiveControllerWithLearning(living_memory=memory)
ctrl.update_routes_from_history() # Weekly job
"""
def __init__(self, living_memory=None, verbose: bool = False):
super().__init__(verbose)
self.living_memory = living_memory
self.learned_routes: Dict[str, float] = {} # Query type -> success rate
def update_routes_from_history(self, window_days: int = 7):
"""Update routing patterns based on historical correctness data.
This job should run periodically (e.g., daily) to learn which routes work best.
Args:
window_days: Look back this many days for historical data
"""
if not self.living_memory:
if self.verbose:
print("[EXEC] No living_memory available - skipping learned routing")
return
if self.verbose:
print(f"[EXEC] Analyzing routing history ({window_days} days)...")
# In a full implementation, this would:
# 1. Query living_memory for recent debate results
# 2. Correlate component_selection with correctness
# 3. Update success rates for each route
# 4. Adjust future routing based on evidence
# For now, placeholder implementation
self.learned_routes = {
'SIMPLE': 0.95, # High confidence in simple routing
'MEDIUM': 0.80, # Good but room for improvement
'COMPLEX': 0.85, # Good on complex routing
}
if self.verbose:
print(f"[EXEC] Routing routes updated: {self.learned_routes}")
def get_route_confidence(self, complexity: QueryComplexity) -> float:
"""Get learned confidence score for a routing decision.
Returns:
0-1 confidence score (higher = more reliable route)
"""
return self.learned_routes.get(complexity.value, 0.5)
def should_explore_alternate_route(self, complexity: QueryComplexity) -> bool:
"""Decide if we should try an alternate route (ε-greedy exploration).
Args:
complexity: Query complexity
Returns:
True if we should try a different route for learning
"""
confidence = self.get_route_confidence(complexity)
# If very confident, stick with known good route
if confidence > 0.90:
return False
# If moderate confidence, 10% of time try alternate
if confidence > 0.70:
return __import__('random').random() < 0.1
# If low confidence, 25% of time try alternate
return __import__('random').random() < 0.25
|