Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /context_memory /optimizer /optimizer.py
| """ | |
| ACGS-2 Context Optimizer - Main Optimizer | |
| Constitutional Hash: 608508a9bd224290 | |
| Main context window optimizer combining all optimizations. | |
| Provides 30x context length increase with sub-5ms P99 latency. | |
| """ | |
| from __future__ import annotations | |
| import asyncio | |
| import inspect | |
| from collections import OrderedDict | |
| from collections.abc import Callable | |
| from datetime import UTC, datetime | |
| from typing import TYPE_CHECKING, Any, TypeAlias | |
| try: | |
| from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH | |
| except ImportError: | |
| CONSTITUTIONAL_HASH = "standalone" | |
| from enhanced_agent_bus.observability.structured_logging import get_logger | |
| if TYPE_CHECKING: | |
| from enhanced_agent_bus._compat.types import JSONDict | |
| else: | |
| try: | |
| from enhanced_agent_bus._compat.types import JSONDict | |
| except ImportError: | |
| JSONDict: TypeAlias = dict[str, Any] | |
| from enhanced_agent_bus.context_memory.models import ( | |
| ContextChunk, | |
| ContextType, | |
| ContextWindow, | |
| ) | |
| from .batch_processor import ParallelBatchProcessor | |
| from .config import OptimizerConfig | |
| from .models import AdaptiveCacheEntry, BatchProcessingResult, StreamingResult | |
| from .prefetch import PrefetchManager | |
| # Re-export NUMPY_AVAILABLE from scorer | |
| from .scorer import NUMPY_AVAILABLE, VectorizedScorer | |
| from .streaming import StreamingProcessor | |
| logger = get_logger(__name__) | |
| CACHE_FETCH_ERRORS = ( | |
| RuntimeError, | |
| ValueError, | |
| TypeError, | |
| KeyError, | |
| AttributeError, | |
| ConnectionError, | |
| OSError, | |
| asyncio.TimeoutError, | |
| ) | |
| class ContextWindowOptimizer: | |
| """Main context window optimizer combining all optimizations. | |
| Provides 30x context length increase with sub-5ms P99 latency. | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| def __init__( | |
| self, | |
| config: OptimizerConfig | None = None, | |
| constitutional_hash: str = CONSTITUTIONAL_HASH, | |
| ): | |
| self.config = config or OptimizerConfig() | |
| self.constitutional_hash = constitutional_hash | |
| if constitutional_hash != CONSTITUTIONAL_HASH: | |
| raise ValueError(f"Invalid constitutional hash: {constitutional_hash}") | |
| # Initialize components | |
| self.scorer = VectorizedScorer( | |
| batch_size=self.config.score_batch_size, | |
| constitutional_hash=constitutional_hash, | |
| ) | |
| self.batch_processor = ParallelBatchProcessor( | |
| max_parallel=self.config.max_parallel_chunks, | |
| batch_size=self.config.batch_size, | |
| constitutional_hash=constitutional_hash, | |
| ) | |
| self.streaming_processor = StreamingProcessor( | |
| buffer_size=self.config.stream_buffer_size, | |
| overlap_ratio=self.config.overlap_ratio, | |
| constitutional_hash=constitutional_hash, | |
| ) | |
| self.prefetch_manager = PrefetchManager( | |
| threshold=self.config.prefetch_threshold, | |
| max_entries=self.config.max_prefetch_entries, | |
| constitutional_hash=constitutional_hash, | |
| ) | |
| # Adaptive cache | |
| self._adaptive_cache: OrderedDict[str, AdaptiveCacheEntry] = OrderedDict() | |
| self._cache_max_size = 1000 | |
| # Performance tracking | |
| self._latencies: list[float] = [] | |
| self._latency_window = 1000 | |
| # Background task tracking (prevent GC before completion) | |
| self._background_tasks: set[asyncio.Task[object]] = set() | |
| logger.info( | |
| f"Initialized ContextWindowOptimizer " | |
| f"(strategy={self.config.optimization_strategy.value}, " | |
| f"target_multiplier={self.config.target_context_multiplier}x)" | |
| ) | |
| async def optimize_context( | |
| self, | |
| query: str, | |
| chunks: list[ContextChunk], | |
| max_tokens: int = 100_000, | |
| ) -> tuple[ContextWindow, dict[str, float]]: | |
| """Optimize context selection and ordering. | |
| Args: | |
| query: Query for relevance scoring | |
| chunks: Available chunks | |
| max_tokens: Maximum tokens | |
| Returns: | |
| Tuple of (optimized ContextWindow, relevance scores dict) | |
| """ | |
| import time | |
| start_time = time.perf_counter() | |
| # Score chunks in batch | |
| scoring_result = self.scorer.score_batch(query, chunks) | |
| scores = { | |
| chunk.chunk_id: score | |
| for chunk, score in zip(chunks, scoring_result.scores, strict=False) | |
| } | |
| # Sort by score | |
| scored_chunks = list(zip(chunks, scoring_result.scores, strict=False)) | |
| scored_chunks.sort(key=lambda x: x[1], reverse=True) | |
| # Build optimized window | |
| window = ContextWindow( | |
| max_tokens=max_tokens, | |
| constitutional_hash=self.constitutional_hash, | |
| ) | |
| # Add chunks in score order, constitutional first | |
| constitutional_chunks = [ | |
| (c, s) for c, s in scored_chunks if c.context_type == ContextType.CONSTITUTIONAL | |
| ] | |
| other_chunks = [ | |
| (c, s) for c, s in scored_chunks if c.context_type != ContextType.CONSTITUTIONAL | |
| ] | |
| for chunk, _score in constitutional_chunks + other_chunks: | |
| if not window.add_chunk(chunk): | |
| break | |
| # Record latency | |
| latency = (time.perf_counter() - start_time) * 1000 | |
| self._record_latency(latency) | |
| return window, scores | |
| async def process_parallel( | |
| self, | |
| chunks: list[ContextChunk], | |
| processor_fn: Callable[[ContextChunk], object], | |
| ) -> BatchProcessingResult: | |
| """Process chunks in parallel. | |
| Args: | |
| chunks: Chunks to process | |
| processor_fn: Processing function | |
| Returns: | |
| BatchProcessingResult | |
| """ | |
| return await self.batch_processor.process_batch(chunks, processor_fn) | |
| async def stream_embeddings( | |
| self, | |
| embeddings: object, | |
| processor_fn: Callable[[object], object], | |
| ) -> StreamingResult: | |
| """Stream process embeddings. | |
| Args: | |
| embeddings: Input embeddings | |
| processor_fn: Processing function | |
| Returns: | |
| StreamingResult | |
| """ | |
| return await self.streaming_processor.stream_process(embeddings, processor_fn) | |
| async def get_cached( | |
| self, | |
| key: str, | |
| fetch_fn: Callable[[str], object] | None = None, | |
| ) -> object | None: | |
| """Get value from adaptive cache. | |
| Args: | |
| key: Cache key | |
| fetch_fn: Optional fetch function for cache miss | |
| Returns: | |
| Cached value or None | |
| """ | |
| now = datetime.now(UTC) | |
| # Check prefetch cache first | |
| prefetched = self.prefetch_manager.get_prefetched(key) | |
| if prefetched is not None: | |
| return prefetched | |
| # Check adaptive cache | |
| if key in self._adaptive_cache: | |
| entry = self._adaptive_cache[key] | |
| if not entry.is_expired(now): | |
| entry.record_access() | |
| self._adaptive_cache.move_to_end(key) | |
| # Record access for prefetching | |
| self.prefetch_manager.record_access(key) | |
| # Trigger prefetch if enabled | |
| if self.config.enable_prefetching and fetch_fn: | |
| task = asyncio.create_task(self.prefetch_manager.prefetch(key, fetch_fn)) | |
| self._background_tasks.add(task) | |
| task.add_done_callback(self._background_tasks.discard) | |
| return entry.value | |
| # Entry expired, remove it | |
| del self._adaptive_cache[key] | |
| # Cache miss - fetch if function provided | |
| if fetch_fn: | |
| try: | |
| if inspect.iscoroutinefunction(fetch_fn): | |
| value = await fetch_fn(key) | |
| else: | |
| value = fetch_fn(key) | |
| await self.set_cached(key, value) | |
| return value # type: ignore[no-any-return] | |
| except CACHE_FETCH_ERRORS as e: | |
| logger.debug("Cache fetch failed for %s: %s", key, str(e)) | |
| return None | |
| return None | |
| async def set_cached( | |
| self, | |
| key: str, | |
| value: object, | |
| is_constitutional: bool = False, | |
| ) -> None: | |
| """Set value in adaptive cache. | |
| Args: | |
| key: Cache key | |
| value: Value to cache | |
| is_constitutional: Whether constitutional content | |
| """ | |
| # Evict if necessary | |
| while len(self._adaptive_cache) >= self._cache_max_size: | |
| # Remove least recently used non-constitutional entry | |
| for old_key in list(self._adaptive_cache.keys()): | |
| if not self._adaptive_cache[old_key].is_constitutional: | |
| del self._adaptive_cache[old_key] | |
| break | |
| else: | |
| # All constitutional, remove oldest | |
| self._adaptive_cache.popitem(last=False) | |
| # Create entry | |
| entry = AdaptiveCacheEntry( | |
| key=key, | |
| value=value, | |
| created_at=datetime.now(UTC), | |
| base_ttl_seconds=self.config.min_ttl_seconds, | |
| is_constitutional=is_constitutional, | |
| constitutional_hash=self.constitutional_hash, | |
| ) | |
| self._adaptive_cache[key] = entry | |
| self._adaptive_cache.move_to_end(key) | |
| def _record_latency(self, latency_ms: float) -> None: | |
| """Record latency for P99 tracking.""" | |
| self._latencies.append(latency_ms) | |
| if len(self._latencies) > self._latency_window: | |
| self._latencies = self._latencies[-self._latency_window :] | |
| def get_p99_latency(self) -> float: | |
| """Get P99 latency in milliseconds.""" | |
| if not self._latencies: | |
| return 0.0 | |
| sorted_latencies = sorted(self._latencies) | |
| p99_idx = int(len(sorted_latencies) * 0.99) | |
| return sorted_latencies[min(p99_idx, len(sorted_latencies) - 1)] | |
| def is_within_latency_target(self) -> bool: | |
| """Check if P99 latency is within target.""" | |
| return self.get_p99_latency() <= self.config.p99_latency_target_ms | |
| def get_metrics(self) -> JSONDict: | |
| """Get optimizer metrics.""" | |
| return { | |
| "scorer_metrics": self.scorer.get_metrics(), | |
| "batch_processor_metrics": self.batch_processor.get_metrics(), | |
| "streaming_metrics": self.streaming_processor.get_metrics(), | |
| "prefetch_metrics": self.prefetch_manager.get_metrics(), | |
| "cache_size": len(self._adaptive_cache), | |
| "p99_latency_ms": self.get_p99_latency(), | |
| "p99_within_target": self.is_within_latency_target(), | |
| "latency_target_ms": self.config.p99_latency_target_ms, | |
| "strategy": self.config.optimization_strategy.value, | |
| "constitutional_hash": self.constitutional_hash, | |
| } | |
| def reset(self) -> None: | |
| """Reset optimizer state.""" | |
| self._adaptive_cache.clear() | |
| self._latencies.clear() | |
| self.prefetch_manager.clear_session() | |
| __all__ = [ | |
| "NUMPY_AVAILABLE", | |
| "ContextWindowOptimizer", | |
| ] | |
Xet Storage Details
- Size:
- 11.1 kB
- Xet hash:
- 1fbfe2629175a5a7c12962b9ccb3fa511768bb4af53516fca59570d23d70535a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.