File size: 46,356 Bytes
2ec0d39 |
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 |
"""
Context Management with Dynamic Sizing System
==========================================
Advanced context management system with dynamic window sizing, relevance scoring,
context expiry and refresh protocols, and conflict resolution strategies.
"""
import asyncio
import json
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional, Set, Tuple, Union, Callable
from dataclasses import dataclass, field, asdict
from enum import Enum
import numpy as np
from collections import defaultdict, deque
import heapq
from functools import lru_cache
import threading
from concurrent.futures import ThreadPoolExecutor
from ai_agent_framework.core.context_engineering_agent import (
ContextElement, ContextModality, ContextDimension
)
logger = logging.getLogger(__name__)
class ContextPriority(Enum):
"""Context priority levels."""
CRITICAL = "critical"
HIGH = "high"
MEDIUM = "medium"
LOW = "low"
ARCHIVED = "archived"
class SizingStrategy(Enum):
"""Dynamic sizing strategies."""
FIXED = "fixed"
ADAPTIVE = "adaptive"
PREDICTIVE = "predictive"
OPTIMIZED = "optimized"
COMPRESSIVE = "compressive"
class RefreshTrigger(Enum):
"""Context refresh triggers."""
TIME_BASED = "time_based"
RELEVANCE_BASED = "relevance_based"
INTERACTION_BASED = "interaction_based"
QUALITY_BASED = "quality_based"
CAPACITY_BASED = "capacity_based"
@dataclass
class ContextItem:
"""Individual context item with metadata."""
id: str
content: Any
modality: ContextModality
dimension: ContextDimension
priority: ContextPriority
timestamp: datetime
expiry_time: Optional[datetime]
relevance_score: float
quality_score: float
access_count: int
last_accessed: datetime
dependencies: Set[str]
metadata: Dict[str, Any]
def __post_init__(self):
if not self.id:
self.id = f"context_{int(time.time())}_{hash(str(self.content))}"
if not self.timestamp:
self.timestamp = datetime.utcnow()
if not self.last_accessed:
self.last_accessed = self.timestamp
if not self.metadata:
self.metadata = {}
@dataclass
class ContextWindow:
"""Context window with dynamic sizing capabilities."""
window_id: str
size_limit: int
current_size: int
strategy: SizingStrategy
items: List[ContextItem]
metrics: Dict[str, float]
created_at: datetime
last_resized: datetime
def __post_init__(self):
if not self.window_id:
self.window_id = f"window_{int(time.time())}"
if not self.created_at:
self.created_at = datetime.utcnow()
if not self.last_resized:
self.last_resized = self.created_at
if not self.metrics:
self.metrics = {}
if not self.items:
self.items = []
@dataclass
class ContextConflict:
"""Represents a conflict between context items."""
conflict_id: str
conflicting_items: List[str]
conflict_type: str
resolution_strategy: str
confidence: float
created_at: datetime
def __post_init__(self):
if not self.conflict_id:
self.conflict_id = f"conflict_{int(time.time())}"
if not self.created_at:
self.created_at = datetime.utcnow()
class ContextManager:
"""Core context management engine with dynamic sizing."""
def __init__(self, max_context_windows: int = 10):
self.max_context_windows = max_context_windows
self.context_windows = {} # window_id -> ContextWindow
self.context_index = {} # item_id -> List[window_id]
self.refresh_scheduler = {}
self.conflict_resolver = ContextConflictResolver()
self.sizing_algorithms = {
SizingStrategy.FIXED: self._fixed_sizing,
SizingStrategy.ADAPTIVE: self._adaptive_sizing,
SizingStrategy.PREDICTIVE: self._predictive_sizing,
SizingStrategy.OPTIMIZED: self._optimized_sizing,
SizingStrategy.COMPRESSIVE: self._compressive_sizing
}
self.refresh_handlers = {
RefreshTrigger.TIME_BASED: self._time_based_refresh,
RefreshTrigger.RELEVANCE_BASED: self._relevance_based_refresh,
RefreshTrigger.INTERACTION_BASED: self._interaction_based_refresh,
RefreshTrigger.QUALITY_BASED: self._quality_based_refresh,
RefreshTrigger.CAPACITY_BASED: self._capacity_based_refresh
}
# Performance metrics
self.metrics = {
"total_windows": 0,
"total_items": 0,
"average_window_utilization": 0.0,
"refresh_frequency": 0.0,
"conflict_resolution_rate": 0.0,
"relevance_retention": 0.0
}
# Thread-safe operations
self._lock = threading.RLock()
async def create_context_window(
self,
window_id: Optional[str] = None,
size_limit: int = 100,
strategy: SizingStrategy = SizingStrategy.ADAPTIVE
) -> ContextWindow:
"""Create a new context window with specified strategy."""
with self._lock:
if len(self.context_windows) >= self.max_context_windows:
# Remove oldest window if at capacity
oldest_window_id = min(
self.context_windows.keys(),
key=lambda w_id: self.context_windows[w_id].created_at
)
await self._remove_context_window(oldest_window_id)
window = ContextWindow(
window_id=window_id,
size_limit=size_limit,
current_size=0,
strategy=strategy,
items=[],
metrics={},
created_at=datetime.utcnow(),
last_resized=datetime.utcnow()
)
self.context_windows[window.window_id] = window
self.metrics["total_windows"] = len(self.context_windows)
logger.info(f"Created context window {window.window_id} with strategy {strategy.value}")
return window
async def add_context_item(
self,
window_id: str,
item: ContextItem,
refresh_trigger: Optional[RefreshTrigger] = None
) -> Dict[str, Any]:
"""Add context item to window with dynamic sizing."""
if window_id not in self.context_windows:
raise ValueError(f"Window {window_id} does not exist")
window = self.context_windows[window_id]
# Check for conflicts
conflicts = await self._detect_conflicts(window, item)
# Resolve conflicts if any
if conflicts:
resolved = await self._resolve_conflicts(window, conflicts, item)
if not resolved:
logger.warning(f"Failed to resolve conflicts for item {item.id}")
return {"status": "rejected", "reason": "unresolved_conflicts"}
# Add item to index
if item.id not in self.context_index:
self.context_index[item.id] = []
self.context_index[item.id].append(window_id)
# Determine sizing strategy
if window.current_size < window.size_limit:
# Add directly if within limits
window.items.append(item)
window.current_size += 1
result = {"status": "added_directly"}
else:
# Apply sizing strategy
sizing_func = self.sizing_strategies[window.strategy]
result = await sizing_func(window, item)
# Schedule refresh if needed
if refresh_trigger:
await self._schedule_refresh(window_id, refresh_trigger)
# Update metrics
await self._update_window_metrics(window)
return {
"status": result["status"],
"item_id": item.id,
"window_id": window_id,
"conflicts_resolved": len(conflicts) > 0,
"new_size": window.current_size
}
async def get_context_items(
self,
window_id: str,
limit: Optional[int] = None,
include_metadata: bool = True
) -> Dict[str, Any]:
"""Retrieve context items from window with optimization."""
if window_id not in self.context_windows:
raise ValueError(f"Window {window_id} does not exist")
window = self.context_windows[window_id]
# Sort by relevance and recency
sorted_items = await self._sort_context_items(window.items)
# Apply limit if specified
if limit:
sorted_items = sorted_items[:limit]
# Format response
items_data = []
for item in sorted_items:
item_data = {
"id": item.id,
"content": item.content,
"modality": item.modality.value,
"dimension": item.dimension.value,
"priority": item.priority.value,
"relevance_score": item.relevance_score,
"quality_score": item.quality_score,
"timestamp": item.timestamp.isoformat()
}
if include_metadata:
item_data.update({
"access_count": item.access_count,
"dependencies": list(item.dependencies),
"metadata": item.metadata
})
items_data.append(item_data)
return {
"window_id": window_id,
"items": items_data,
"total_items": len(window.items),
"window_utilization": window.current_size / window.size_limit,
"metrics": window.metrics
}
async def refresh_context_window(
self,
window_id: str,
force_refresh: bool = False
) -> Dict[str, Any]:
"""Refresh context window based on configured strategy."""
if window_id not in self.context_windows:
raise ValueError(f"Window {window_id} does not exist")
window = self.context_windows[window_id]
# Check if refresh is needed
if not force_refresh:
refresh_needed = await self._should_refresh(window)
if not refresh_needed["needed"]:
return {
"status": "skipped",
"reason": refresh_needed["reason"]
}
# Determine refresh strategy
refresh_strategy = await self._determine_refresh_strategy(window)
# Execute refresh
refresh_func = self.refresh_handlers[refresh_strategy]
refresh_result = await refresh_func(window_id)
# Update metrics
await self._update_refresh_metrics(window, refresh_result)
return {
"status": "refreshed",
"strategy": refresh_strategy.value,
"items_affected": refresh_result.get("items_affected", 0),
"new_window_utilization": window.current_size / window.size_limit
}
async def optimize_context_window(
self,
window_id: str,
optimization_goals: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Optimize context window for better performance."""
if window_id not in self.context_windows:
raise ValueError(f"Window {window_id} does not exist")
window = self.context_windows[window_id]
if not optimization_goals:
optimization_goals = ["relevance", "efficiency", "quality"]
optimization_results = {}
for goal in optimization_goals:
if goal == "relevance":
result = await self._optimize_relevance(window)
optimization_results["relevance"] = result
elif goal == "efficiency":
result = await self._optimize_efficiency(window)
optimization_results["efficiency"] = result
elif goal == "quality":
result = await self._optimize_quality(window)
optimization_results["quality"] = result
elif goal == "diversity":
result = await self._optimize_diversity(window)
optimization_results["diversity"] = result
# Apply optimizations
total_improvements = 0
for goal, result in optimization_results.items():
if result.get("improved", False):
total_improvements += 1
await self._update_window_metrics(window)
return {
"status": "optimized",
"optimization_goals": optimization_goals,
"improvements_made": total_improvements,
"optimization_results": optimization_results,
"new_metrics": window.metrics
}
async def resolve_context_conflicts(
self,
window_id: str,
conflict_resolution_strategy: str = "priority_based"
) -> Dict[str, Any]:
"""Resolve conflicts within context window."""
if window_id not in self.context_windows:
raise ValueError(f"Window {window_id} does not exist")
window = self.context_windows[window_id]
# Detect all conflicts
all_conflicts = await self._detect_all_conflicts(window)
if not all_conflicts:
return {
"status": "no_conflicts",
"conflicts_resolved": 0
}
# Resolve conflicts
resolved_count = 0
resolution_details = []
for conflict in all_conflicts:
resolution_result = await self._resolve_single_conflict(
window, conflict, conflict_resolution_strategy
)
if resolution_result["resolved"]:
resolved_count += 1
resolution_details.append(resolution_result)
# Update metrics
self.metrics["conflict_resolution_rate"] = resolved_count / len(all_conflicts)
return {
"status": "conflicts_resolved",
"total_conflicts": len(all_conflicts),
"conflicts_resolved": resolved_count,
"resolution_rate": resolved_count / len(all_conflicts),
"resolution_details": resolution_details
}
# Sizing strategy implementations
async def _fixed_sizing(self, window: ContextWindow, new_item: ContextItem) -> Dict[str, Any]:
"""Fixed sizing strategy - replace lowest priority item."""
# Find lowest priority item
lowest_priority_idx = 0
for i, item in enumerate(window.items):
if (item.priority.value == "low" or
(item.relevance_score < window.items[lowest_priority_idx].relevance_score)):
lowest_priority_idx = i
# Replace if new item has higher priority
if (new_item.priority.value in ["high", "critical"] or
new_item.relevance_score > window.items[lowest_priority_idx].relevance_score):
# Remove old item
removed_item = window.items.pop(lowest_priority_idx)
# Add new item
window.items.append(new_item)
return {
"status": "replaced",
"replaced_item_id": removed_item.id,
"new_item_id": new_item.id
}
else:
return {
"status": "rejected",
"reason": "lower_priority_than_existing"
}
async def _adaptive_sizing(self, window: ContextWindow, new_item: ContextItem) -> Dict[str, Any]:
"""Adaptive sizing strategy - dynamically adjust based on relevance."""
# Calculate overall relevance threshold
current_relevances = [item.relevance_score for item in window.items]
threshold = np.percentile(current_relevances, 25) # 25th percentile
# Check if new item meets threshold
if new_item.relevance_score < threshold:
return {
"status": "rejected",
"reason": "below_relevance_threshold",
"threshold": threshold
}
# Find items to remove (lowest relevance, lowest priority)
removal_candidates = []
for i, item in enumerate(window.items):
score = (item.relevance_score * 0.7) + (1 - self._priority_weight(item.priority) * 0.3)
removal_candidates.append((score, i, item))
removal_candidates.sort(key=lambda x: x[0]) # Sort by score
# Remove enough items to make space
items_removed = 0
for score, idx, item in removal_candidates:
if len(window.items) + 1 <= window.size_limit:
break
window.items.pop(idx)
items_removed += 1
# Add new item
window.items.append(new_item)
return {
"status": "adaptive_replaced",
"items_removed": items_removed,
"new_item_id": new_item.id,
"relevance_threshold": threshold
}
async def _predictive_sizing(self, window: ContextWindow, new_item: ContextItem) -> Dict[str, Any]:
"""Predictive sizing strategy - use patterns to predict future relevance."""
# Analyze historical access patterns
access_patterns = await self._analyze_access_patterns(window)
# Predict future relevance for existing items
predicted_relevances = {}
for item in window.items:
predicted_relevance = await self._predict_future_relevance(item, access_patterns)
predicted_relevances[item.id] = predicted_relevance
# Compare with new item's predicted relevance
new_item_predicted = await self._predict_future_relevance(new_item, access_patterns)
# Find items to replace
replacement_candidates = []
for item in window.items:
predicted = predicted_relevances[item.id]
current = item.relevance_score
combined_score = (predicted * 0.6) + (current * 0.4)
replacement_candidates.append((combined_score, item))
replacement_candidates.sort(key=lambda x: x[0])
# Replace if beneficial
if replacement_candidates and replacement_candidates[0][0] < new_item_predicted:
replaced_item = replacement_candidates[0][1]
window.items.remove(replaced_item)
window.items.append(new_item)
return {
"status": "predictive_replaced",
"replaced_item_id": replaced_item.id,
"new_item_id": new_item.id,
"predicted_improvement": new_item_predicted - replacement_candidates[0][0]
}
else:
return {
"status": "rejected",
"reason": "no_predictive_benefit"
}
async def _optimized_sizing(self, window: ContextWindow, new_item: ContextItem) -> Dict[str, Any]:
"""Optimized sizing strategy - maximize overall utility."""
# Calculate utility scores for all items including new one
all_items = window.items + [new_item]
utilities = {}
for item in all_items:
utility = await self._calculate_item_utility(item, window)
utilities[item.id] = utility
# Select optimal subset
sorted_items = sorted(all_items, key=lambda x: utilities[x.id], reverse=True)
optimal_items = sorted_items[:window.size_limit]
# Check if composition changed
current_ids = {item.id for item in window.items}
optimal_ids = {item.id for item in optimal_items}
if current_ids != optimal_ids:
# Update window
window.items = optimal_items
removed_items = current_ids - optimal_ids
added_items = optimal_ids - current_ids
return {
"status": "optimized",
"removed_items": list(removed_items),
"added_items": list(added_items),
"total_utility": sum(utilities[item.id] for item in optimal_items)
}
else:
return {
"status": "no_change",
"reason": "already_optimal"
}
async def _compressive_sizing(self, window: ContextWindow, new_item: ContextItem) -> Dict[str, Any]:
"""Compressive sizing strategy - compress redundant information."""
# Identify redundant items
redundant_items = await self._identify_redundant_items(window.items + [new_item])
# Compress redundant information
compression_result = await self._compress_information(window.items, new_item)
if compression_result["compressed"]:
window.items = compression_result["compressed_items"]
return {
"status": "compressed",
"compression_ratio": compression_result["ratio"],
"items_removed": compression_result["items_removed"],
"information_preserved": compression_result["information_preserved"]
}
else:
# No compression possible, use adaptive strategy
return await self._adaptive_sizing(window, new_item)
# Refresh strategy implementations
async def _time_based_refresh(self, window_id: str) -> Dict[str, Any]:
"""Time-based refresh strategy."""
window = self.context_windows[window_id]
refresh_threshold = timedelta(minutes=30) # Refresh every 30 minutes
current_time = datetime.utcnow()
items_to_refresh = []
items_to_remove = []
for item in window.items:
age = current_time - item.last_accessed
if age > refresh_threshold:
if item.priority.value == "low":
items_to_remove.append(item)
else:
items_to_refresh.append(item)
# Refresh items
for item in items_to_refresh:
await self._refresh_item_relevance(item)
# Remove expired items
for item in items_to_remove:
window.items.remove(item)
window.current_size -= 1
return {
"items_refreshed": len(items_to_refresh),
"items_removed": len(items_to_remove),
"refresh_type": "time_based"
}
async def _relevance_based_refresh(self, window_id: str) -> Dict[str, Any]:
"""Relevance-based refresh strategy."""
window = self.context_windows[window_id]
# Recalculate relevance for all items
for item in window.items:
await self._recalculate_relevance(item)
# Remove low relevance items
relevance_threshold = 0.3
items_to_remove = [
item for item in window.items
if item.relevance_score < relevance_threshold
]
for item in items_to_remove:
window.items.remove(item)
window.current_size -= 1
return {
"items_recalculated": len(window.items),
"items_removed": len(items_to_remove),
"refresh_type": "relevance_based"
}
async def _interaction_based_refresh(self, window_id: str) -> Dict[str, Any]:
"""Interaction-based refresh strategy."""
window = self.context_windows[window_id]
# Analyze recent interactions (simplified)
recent_interactions = await self._get_recent_interactions(window)
# Update relevance based on interaction patterns
items_updated = 0
for item in window.items:
old_relevance = item.relevance_score
new_relevance = await self._update_relevance_from_interactions(item, recent_interactions)
if abs(new_relevance - old_relevance) > 0.1: # Significant change
item.relevance_score = new_relevance
items_updated += 1
return {
"items_updated": items_updated,
"refresh_type": "interaction_based"
}
async def _quality_based_refresh(self, window_id: str) -> Dict[str, Any]:
"""Quality-based refresh strategy."""
window = self.context_windows[window_id]
# Recalculate quality scores
quality_updates = 0
for item in window.items:
old_quality = item.quality_score
new_quality = await self._recalculate_quality(item)
if new_quality != old_quality:
item.quality_score = new_quality
quality_updates += 1
# Re-sort items by quality
window.items.sort(key=lambda x: x.quality_score, reverse=True)
return {
"quality_updates": quality_updates,
"refresh_type": "quality_based"
}
async def _capacity_based_refresh(self, window_id: str) -> Dict[str, Any]:
"""Capacity-based refresh strategy."""
window = self.context_windows[window_id]
utilization = window.current_size / window.size_limit
if utilization > 0.9: # Near capacity
# Remove lowest priority, lowest relevance items
items_to_remove = []
sorted_items = sorted(
window.items,
key=lambda x: (x.priority.value, x.relevance_score),
reverse=True
)
# Remove bottom 20% to make space
remove_count = int(len(sorted_items) * 0.2)
items_to_remove = sorted_items[:remove_count]
for item in items_to_remove:
window.items.remove(item)
window.current_size -= 1
return {
"items_removed": len(items_to_remove),
"capacity_freed": len(items_to_remove),
"refresh_type": "capacity_based"
}
else:
return {
"items_removed": 0,
"refresh_type": "capacity_based"
}
# Helper methods
def _priority_weight(self, priority: ContextPriority) -> float:
"""Get weight for priority level."""
weights = {
ContextPriority.CRITICAL: 1.0,
ContextPriority.HIGH: 0.8,
ContextPriority.MEDIUM: 0.6,
ContextPriority.LOW: 0.4,
ContextPriority.ARCHIVED: 0.2
}
return weights.get(priority, 0.5)
async def _sort_context_items(self, items: List[ContextItem]) -> List[ContextItem]:
"""Sort context items by relevance and recency."""
def sort_key(item):
recency_score = 1.0 / (1.0 + (datetime.utcnow() - item.last_accessed).total_seconds() / 3600)
combined_score = (item.relevance_score * 0.7) + (item.quality_score * 0.2) + (recency_score * 0.1)
return combined_score
return sorted(items, key=sort_key, reverse=True)
async def _detect_conflicts(self, window: ContextWindow, new_item: ContextItem) -> List[ContextConflict]:
"""Detect conflicts between new item and existing items."""
conflicts = []
for existing_item in window.items:
# Check for content conflicts
if await self._are_conflicting(existing_item, new_item):
conflict = ContextConflict(
conflict_id=f"conflict_{existing_item.id}_{new_item.id}",
conflicting_items=[existing_item.id, new_item.id],
conflict_type="content_conflict",
resolution_strategy="priority_based",
confidence=0.8
)
conflicts.append(conflict)
return conflicts
async def _are_conflicting(self, item1: ContextItem, item2: ContextItem) -> bool:
"""Check if two items conflict."""
# Check for dependency conflicts
if item1.dependencies & {item2.id} or item2.dependencies & {item1.id}:
return True
# Check for contradictory information (simplified)
if (item1.modality == item2.modality and
item1.dimension == item2.dimension and
item1.priority == item2.priority):
# Additional logic would check for contradictory content
return False # Simplified for this example
return False
async def _resolve_conflicts(
self,
window: ContextWindow,
conflicts: List[ContextConflict],
new_item: ContextItem
) -> bool:
"""Resolve conflicts and update window."""
resolver = self.conflict_resolver
for conflict in conflicts:
resolution_result = await resolver.resolve_conflict(window, conflict)
if not resolution_result["success"]:
return False
return True
async def _detect_all_conflicts(self, window: ContextWindow) -> List[ContextConflict]:
"""Detect all conflicts in window."""
conflicts = []
items = window.items
for i in range(len(items)):
for j in range(i + 1, len(items)):
if await self._are_conflicting(items[i], items[j]):
conflict = ContextConflict(
conflict_id=f"conflict_{items[i].id}_{items[j].id}",
conflicting_items=[items[i].id, items[j].id],
conflict_type="pairwise_conflict",
resolution_strategy="priority_based",
confidence=0.8
)
conflicts.append(conflict)
return conflicts
async def _resolve_single_conflict(
self,
window: ContextWindow,
conflict: ContextConflict,
strategy: str
) -> Dict[str, Any]:
"""Resolve a single conflict."""
items = {item.id: item for item in window.items}
conflicting_ids = conflict.conflicting_items
if not all(item_id in items for item_id in conflicting_ids):
return {"resolved": False, "reason": "missing_items"}
if strategy == "priority_based":
# Keep highest priority item
items_list = [items[item_id] for item_id in conflicting_ids]
items_list.sort(key=lambda x: x.priority.value, reverse=True)
winner = items_list[0]
losers = items_list[1:]
for loser in losers:
window.items.remove(loser)
window.current_size -= 1
return {
"resolved": True,
"winner": winner.id,
"losers": [l.id for l in losers],
"strategy": "priority_based"
}
elif strategy == "relevance_based":
# Keep highest relevance item
items_list = [items[item_id] for item_id in conflicting_ids]
items_list.sort(key=lambda x: x.relevance_score, reverse=True)
winner = items_list[0]
losers = items_list[1:]
for loser in losers:
window.items.remove(loser)
window.current_size -= 1
return {
"resolved": True,
"winner": winner.id,
"losers": [l.id for l in losers],
"strategy": "relevance_based"
}
return {"resolved": False, "reason": "unknown_strategy"}
# Utility methods for sizing and optimization
async def _analyze_access_patterns(self, window: ContextWindow) -> Dict[str, Any]:
"""Analyze historical access patterns."""
# Simplified access pattern analysis
access_patterns = {
"frequent_items": [],
"recent_activity": {},
"access_distribution": {}
}
for item in window.items:
if item.access_count > 5: # Frequently accessed
access_patterns["frequent_items"].append(item.id)
access_patterns["recent_activity"][item.id] = item.access_count
access_patterns["access_distribution"][item.priority.value] = \
access_patterns["access_distribution"].get(item.priority.value, 0) + 1
return access_patterns
async def _predict_future_relevance(self, item: ContextItem, patterns: Dict[str, Any]) -> float:
"""Predict future relevance of an item."""
# Simple prediction based on access patterns
base_relevance = item.relevance_score
# Adjust based on access frequency
access_factor = min(1.5, 1.0 + (item.access_count * 0.1))
# Adjust based on recency
recency_hours = (datetime.utcnow() - item.last_accessed).total_seconds() / 3600
recency_factor = max(0.5, 1.0 - (recency_hours / 168)) # Decay over a week
predicted_relevance = base_relevance * access_factor * recency_factor
return min(1.0, predicted_relevance)
async def _calculate_item_utility(self, item: ContextWindow, window: ContextWindow) -> float:
"""Calculate utility score for an item."""
# Multi-factor utility calculation
relevance_utility = item.relevance_score * 0.4
quality_utility = item.quality_score * 0.3
priority_utility = self._priority_weight(item.priority) * 0.2
recency_utility = 1.0 / (1.0 + (datetime.utcnow() - item.last_accessed).total_seconds() / 3600) * 0.1
total_utility = relevance_utility + quality_utility + priority_utility + recency_utility
return total_utility
async def _identify_redundant_items(self, items: List[ContextItem]) -> List[str]:
"""Identify redundant items in the list."""
redundant_ids = []
# Group by modality and dimension
groups = defaultdict(list)
for item in items:
key = f"{item.modality.value}_{item.dimension.value}"
groups[key].append(item)
# Find redundant items within groups
for group_items in groups.values():
if len(group_items) > 1:
# Sort by relevance and quality
group_items.sort(key=lambda x: (x.relevance_score, x.quality_score), reverse=True)
# Mark items below threshold as redundant
threshold = group_items[0].relevance_score * 0.8
for item in group_items[1:]:
if item.relevance_score < threshold:
redundant_ids.append(item.id)
return redundant_ids
async def _compress_information(self, items: List[ContextItem], new_item: ContextItem) -> Dict[str, Any]:
"""Compress redundant information."""
# Simplified compression
all_items = items + [new_item]
redundant_ids = await self._identify_redundant_items(all_items)
if not redundant_ids:
return {"compressed": False}
# Remove redundant items
compressed_items = [item for item in all_items if item.id not in redundant_ids]
return {
"compressed": True,
"compressed_items": compressed_items,
"items_removed": len(redundant_ids),
"ratio": len(compressed_items) / len(all_items),
"information_preserved": sum(item.quality_score for item in compressed_items) / max(1, sum(item.quality_score for item in all_items))
}
async def _should_refresh(self, window: ContextWindow) -> Dict[str, Any]:
"""Determine if window should be refreshed."""
time_since_refresh = datetime.utcnow() - window.last_resized
# Time-based threshold
if time_since_refresh > timedelta(minutes=60):
return {"needed": True, "reason": "time_threshold"}
# Capacity-based threshold
utilization = window.current_size / window.size_limit
if utilization > 0.95:
return {"needed": True, "reason": "high_utilization"}
# Quality-based threshold
low_quality_items = sum(1 for item in window.items if item.quality_score < 0.5)
if low_quality_items > len(window.items) * 0.3:
return {"needed": True, "reason": "low_quality"}
return {"needed": False, "reason": "no_refresh_needed"}
async def _determine_refresh_strategy(self, window: ContextWindow) -> RefreshTrigger:
"""Determine best refresh strategy for window."""
# Analyze window characteristics
utilization = window.current_size / window.size_limit
age = (datetime.utcnow() - window.last_resized).total_seconds() / 60 # minutes
# Choose strategy based on conditions
if utilization > 0.9:
return RefreshTrigger.CAPACITY_BASED
elif age > 45:
return RefreshTrigger.TIME_BASED
elif window.strategy == SizingStrategy.PREDICTIVE:
return RefreshTrigger.INTERACTION_BASED
else:
return RefreshTrigger.RELEVANCE_BASED
async def _update_window_metrics(self, window: ContextWindow) -> None:
"""Update window metrics."""
window.metrics.update({
"utilization": window.current_size / window.size_limit,
"avg_relevance": np.mean([item.relevance_score for item in window.items]) if window.items else 0,
"avg_quality": np.mean([item.quality_score for item in window.items]) if window.items else 0,
"diversity_score": len(set(item.modality for item in window.items)) / len(window.items) if window.items else 0,
"last_updated": datetime.utcnow().isoformat()
})
# Update global metrics
self.metrics["total_items"] = sum(w.current_size for w in self.context_windows.values())
self.metrics["average_window_utilization"] = np.mean([w.current_size / w.size_limit for w in self.context_windows.values()])
# Placeholder methods for item operations
async def _remove_context_window(self, window_id: str) -> None:
"""Remove a context window."""
if window_id in self.context_windows:
del self.context_windows[window_id]
self.metrics["total_windows"] = len(self.context_windows)
async def _refresh_item_relevance(self, item: ContextItem) -> None:
"""Refresh item relevance score."""
# Simplified refresh
item.relevance_score *= 0.95 # Gradual decay
async def _recalculate_relevance(self, item: ContextItem) -> None:
"""Recalculate item relevance score."""
# Simplified recalculation
pass
async def _get_recent_interactions(self, window: ContextWindow) -> List[Dict[str, Any]]:
"""Get recent interactions affecting the window."""
return [] # Simplified
async def _update_relevance_from_interactions(self, item: ContextItem, interactions: List[Dict[str, Any]]) -> float:
"""Update relevance based on recent interactions."""
return item.relevance_score # Simplified
async def _recalculate_quality(self, item: ContextItem) -> float:
"""Recalculate item quality score."""
return item.quality_score # Simplified
async def _update_refresh_metrics(self, window: ContextWindow, refresh_result: Dict[str, Any]) -> None:
"""Update refresh-related metrics."""
window.last_resized = datetime.utcnow()
# Optimization methods
async def _optimize_relevance(self, window: ContextWindow) -> Dict[str, Any]:
"""Optimize for relevance."""
initial_avg_relevance = np.mean([item.relevance_score for item in window.items]) if window.items else 0
# Remove low-relevance items
threshold = initial_avg_relevance * 0.7
items_to_remove = [item for item in window.items if item.relevance_score < threshold]
for item in items_to_remove:
window.items.remove(item)
window.current_size -= 1
final_avg_relevance = np.mean([item.relevance_score for item in window.items]) if window.items else 0
return {
"improved": final_avg_relevance > initial_avg_relevance,
"initial_avg": initial_avg_relevance,
"final_avg": final_avg_relevance,
"items_removed": len(items_to_remove)
}
async def _optimize_efficiency(self, window: ContextWindow) -> Dict[str, Any]:
"""Optimize for efficiency."""
# Improve processing efficiency
initial_diversity = len(set(item.modality for item in window.items)) / len(window.items) if window.items else 0
# Ensure good diversity while maintaining focus
target_diversity = 0.6
if initial_diversity < target_diversity:
# Items are too similar, encourage diversity
pass # Simplified
return {
"improved": True,
"initial_diversity": initial_diversity,
"target_diversity": target_diversity
}
async def _optimize_quality(self, window: ContextWindow) -> Dict[str, Any]:
"""Optimize for quality."""
initial_avg_quality = np.mean([item.quality_score for item in window.items]) if window.items else 0
# Promote high-quality items
window.items.sort(key=lambda x: x.quality_score, reverse=True)
final_avg_quality = np.mean([item.quality_score for item in window.items]) if window.items else 0
return {
"improved": final_avg_quality >= initial_avg_quality,
"initial_avg": initial_avg_quality,
"final_avg": final_avg_quality
}
async def _optimize_diversity(self, window: ContextWindow) -> Dict[str, Any]:
"""Optimize for diversity."""
modality_counts = defaultdict(int)
for item in window.items:
modality_counts[item.modality] += 1
# Ensure balanced representation
max_modality_count = max(modality_counts.values()) if modality_counts else 0
target_per_modality = len(window.items) // len(modality_counts) if modality_counts else 0
# Simplified diversity optimization
return {
"improved": True,
"modality_distribution": dict(modality_counts),
"max_modality_count": max_modality_count
}
async def _schedule_refresh(self, window_id: str, trigger: RefreshTrigger) -> None:
"""Schedule a refresh for the window."""
# Simplified scheduling
pass
# Properties
@property
def sizing_strategies(self) -> Dict[SizingStrategy, Callable]:
"""Get available sizing strategies."""
return self._sizing_strategies if hasattr(self, '_sizing_strategies') else self.sizing_algorithms
class ContextConflictResolver:
"""Specialized conflict resolution for context items."""
def __init__(self):
self.resolution_strategies = {
"priority_based": self._priority_resolution,
"relevance_based": self._relevance_resolution,
"quality_based": self._quality_resolution,
"temporal_based": self._temporal_resolution
}
async def resolve_conflict(self, window: ContextWindow, conflict: ContextConflict) -> Dict[str, Any]:
"""Resolve a context conflict."""
strategy_func = self.resolution_strategies.get(conflict.resolution_strategy)
if not strategy_func:
return {"success": False, "reason": "unknown_strategy"}
return await strategy_func(window, conflict)
async def _priority_resolution(self, window: ContextWindow, conflict: ContextConflict) -> Dict[str, Any]:
"""Resolve conflict based on priority."""
# Implementation would resolve based on priority
return {"success": True, "strategy": "priority_based"}
async def _relevance_resolution(self, window: ContextWindow, conflict: ContextConflict) -> Dict[str, Any]:
"""Resolve conflict based on relevance."""
# Implementation would resolve based on relevance
return {"success": True, "strategy": "relevance_based"}
async def _quality_resolution(self, window: ContextWindow, conflict: ContextConflict) -> Dict[str, Any]:
"""Resolve conflict based on quality."""
# Implementation would resolve based on quality
return {"success": True, "strategy": "quality_based"}
async def _temporal_resolution(self, window: ContextWindow, conflict: ContextConflict) -> Dict[str, Any]:
"""Resolve conflict based on temporal information."""
# Implementation would resolve based on timing
return {"success": True, "strategy": "temporal_based"}
if __name__ == "__main__":
print("Context Management with Dynamic Sizing System Initialized")
print("=" * 60)
manager = ContextManager()
print("Ready for advanced context management and dynamic sizing!") |