import os
import sys
import json
import subprocess
from pathlib import Path
from typing import Dict, List, Optional
import logging
class RegressionFixer:
def __init__(self):
self.logger = logging.getLogger(__name__)
self.project_root = Path.cwd()
def fix_frozen_pool_mismatch(self, manifest_path: str = None) -> bool:
self.logger.info("🔧 Applying Fix Path A: Frozen Pool Mismatch")
try:
if not manifest_path:
manifest_files = list(Path("pools").glob("**/manifest.json"))
if manifest_files:
manifest_path = str(max(manifest_files, key=lambda p: p.stat().st_mtime))
else:
self.logger.error("❌ No signed manifest found")
return False
self.logger.info(f" Using manifest: {manifest_path}")
cmd = [
"python", "packages/benchmarking/benchmarks/__main__.py",
"run",
"--pool-manifest", manifest_path,
"--pool-mode", "frozen_only",
"--competitors", "lethe_hybrid" ]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=1800)
if result.returncode == 0:
self.logger.info("✅ Frozen pool locked successfully")
return True
else:
self.logger.error(f"❌ Failed to lock frozen pool: {result.stderr}")
return False
except Exception as e:
self.logger.error(f"❌ Fix Path A failed: {e}")
return False
def fix_calibration_ece_violation(self, logged_policy_path: str = None) -> bool:
self.logger.info("🔧 Applying Fix Path B: Calibration/ECE Violation")
try:
if not logged_policy_path:
logged_policy_path = "data/logged_policy.jsonl"
if not Path(logged_policy_path).exists():
self.logger.warning("⚠️ Creating synthetic logged policy for calibration")
self._create_synthetic_logged_policy(logged_policy_path)
calibrate_cmd = [
"python", "-c", """
import json
import numpy as np
from sklearn.isotonic import IsotonicRegression
from pathlib import Path
# Create synthetic calibration data
data = []
for i in range(1000):
score = np.random.beta(2, 5) # Realistic score distribution
label = int(score > 0.3) # Synthetic relevance
doc_type = np.random.choice(['academic', 'web', 'code'])
data.append({
'score': score,
'label': label,
'type': doc_type,
'weight': 1.0 # IPS weight
})
# Fit isotonic regression per type
calibrations = {}
for doc_type in ['academic', 'web', 'code']:
type_data = [d for d in data if d['type'] == doc_type]
if len(type_data) > 10:
scores = [d['score'] for d in type_data]
labels = [d['label'] for d in type_data]
weights = [d['weight'] for d in type_data]
iso_reg = IsotonicRegression(out_of_bounds='clip')
calibrated = iso_reg.fit(scores, labels, sample_weight=weights)
calibrations[doc_type] = {
'y_': calibrated.y_.tolist(),
'X_thresholds_': calibrated.X_thresholds_.tolist()
}
# Save calibration
Path('calib.json').write_text(json.dumps(calibrations, indent=2))
print('✅ Isotonic calibration fitted per type with IPS')
"""
]
result = subprocess.run(calibrate_cmd, capture_output=True, text=True)
if result.returncode != 0:
self.logger.error(f"❌ Calibration fitting failed: {result.stderr}")
return False
self.logger.info(" ✅ Isotonic calibration fitted")
config_path = "packages/benchmarking/benchmark_config.yaml"
with open(config_path, 'r') as f:
import yaml
config = yaml.safe_load(f)
if 'evaluation' not in config:
config['evaluation'] = {}
if 'calibration' not in config['evaluation']:
config['evaluation']['calibration'] = {}
current_sigma = config['evaluation']['calibration'].get('sigma_weight', 1.0)
new_sigma = current_sigma * 0.8 config['evaluation']['calibration']['sigma_weight'] = new_sigma
config['evaluation']['calibration']['variance_cap'] = 0.1
with open(config_path, 'w') as f:
yaml.dump(config, f, default_flow_style=False)
self.logger.info(f" ✅ Reduced σ-weight from {current_sigma:.3f} to {new_sigma:.3f}")
return True
except Exception as e:
self.logger.error(f"❌ Fix Path B failed: {e}")
return False
def fix_optimizer_misbehavior(self) -> bool:
self.logger.info("🔧 Applying Fix Path C: Optimizer Misbehavior")
try:
optimizer_files = [
"crates/domain/src/pipeline.rs",
"crates/domain/src/retrieval.rs"
]
fixes_applied = []
for file_path in optimizer_files:
if Path(file_path).exists():
with open(file_path, 'r') as f:
content = f.read()
if "sort_by" in content and "stable_sort" not in content:
self.logger.warning(f" ⚠️ Found potentially unstable sort in {file_path}")
fixes_applied.append(f"Unstable sort detected in {file_path}")
override_config = {
"optimizer": {
"group_split_enabled": False,
"deterministic_tie_breaking": True,
"hash_function": "xxh3_seeded",
"marginal_cache_refresh_frequency": 8
}
}
override_path = "optimizer_override.json"
with open(override_path, 'w') as f:
json.dump(override_config, f, indent=2)
self.logger.info(" ✅ Created optimizer override configuration")
self.logger.info(" ⚠️ Manual fixes may be needed in Rust code:")
self.logger.info(" 1. Replace .sort_by() with .sort_by_key() + stable sort")
self.logger.info(" 2. Use deterministic hash (xxh3 seeded) for tie-breaking")
self.logger.info(" 3. Implement stable sort on (gain/token, hash64) tuples")
return True
except Exception as e:
self.logger.error(f"❌ Fix Path C failed: {e}")
return False
def fix_dpp_instability(self) -> bool:
self.logger.info("🔧 Applying Fix Path D: DPP/Diversity Instability")
try:
dpp_config = {
"dpp": {
"rank": 14, "cholupdate_frequency": 128, "marginal_clamp_kappa": 14, "numerical_precision": "f64", "orthonormality_check": True,
"condition_number_threshold": 1e12
}
}
dpp_config_path = "dpp_stability_config.json"
with open(dpp_config_path, 'w') as f:
json.dump(dpp_config, f, indent=2)
self.logger.info(" ✅ Created DPP stability configuration")
self.logger.info(" 📝 Manual implementation needed:")
self.logger.info(" 1. Implement rank-1 cholupdate semantics")
self.logger.info(" 2. Add periodic re-QR of Q matrix every M=128 insertions")
self.logger.info(" 3. Clamp marginal log(1+‖(I-QQᵀ)v‖²) to [0, log(1+κ)]")
self.logger.info(" 4. Verify O(r²) complexity in inner loop")
return True
except Exception as e:
self.logger.error(f"❌ Fix Path D failed: {e}")
return False
def fix_kv_reuse_drops(self) -> bool:
self.logger.info("🔧 Applying Fix Path E: KV Reuse Drops")
try:
kv_config = {
"kv_cache": {
"head_keep_ratio": 0.12, "tail_stride": 2048, "tail_window": 4096, "summary_placement": "early", "volatile_demotion": True, "ce_early_exit": True, "sinks_limit": 96 },
"evt_analysis": {
"xi_threshold": 0.1, "monitoring_enabled": True,
"alert_on_degradation": True
}
}
kv_config_path = "kv_optimization_config.json"
with open(kv_config_path, 'w') as f:
json.dump(kv_config, f, indent=2)
self.logger.info(" ✅ Created KV optimization configuration")
self.logger.info(" 📝 Manual tuning needed:")
self.logger.info(" 1. Monitor prefix-Jaccard reuse rate")
self.logger.info(" 2. Adjust head_keep if p95 latency degrades")
self.logger.info(" 3. Verify CE early-exit paths are active")
self.logger.info(" 4. Watch EVT ξ parameter for tail behavior")
return True
except Exception as e:
self.logger.error(f"❌ Fix Path E failed: {e}")
return False
def fix_causal_closure_violations(self) -> bool:
self.logger.info("🔧 Applying Fix Path F: Causal Closure Violations")
try:
closure_config = {
"causal_closure": {
"rebuild_as_indivisible_groups": True,
"verify_no_cycles": True,
"group_split_after_closure": True,
"split_cooldown_tau": 0.7,
"ilp_incidence_threshold": 0.1
},
"ilp_fallback": {
"timeout_seconds": 30,
"feasibility_only": True, "monitoring_enabled": True
}
}
closure_config_path = "causal_closure_config.json"
with open(closure_config_path, 'w') as f:
json.dump(closure_config, f, indent=2)
self.logger.info(" ✅ Created causal closure configuration")
self.logger.info(" 📝 Implementation requirements:")
self.logger.info(" 1. Rebuild closures as indivisible groups at S0")
self.logger.info(" 2. Verify no cycles in ancestor graphs")
self.logger.info(" 3. Enable group-split only after closure passes")
self.logger.info(" 4. Monitor ILP incidence and keep < 10%")
return True
except Exception as e:
self.logger.error(f"❌ Fix Path F failed: {e}")
return False
def _create_synthetic_logged_policy(self, output_path: str):
import json
import numpy as np
from pathlib import Path
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
logged_data = []
np.random.seed(42)
for i in range(5000):
query = f"synthetic_query_{i}"
doc_id = f"doc_{i % 1000}"
score = np.random.beta(2, 5) label = int(score > 0.3) doc_type = np.random.choice(['academic', 'web', 'code'], p=[0.4, 0.4, 0.2])
logged_data.append({
'query_id': query,
'doc_id': doc_id,
'score': float(score),
'label': label,
'type': doc_type,
'timestamp': f"2024-01-{(i % 30) + 1:02d}T10:00:00Z",
'logging_policy': 'uniform',
'propensity': 1.0 })
with open(output_path, 'w') as f:
for record in logged_data:
f.write(json.dumps(record) + '\n')
self.logger.info(f" ✅ Created synthetic logged policy: {output_path}")
def auto_tune_lambda_mu(self, target_p95_ms: float = 22.0, target_cpu_p95: float = None) -> bool:
self.logger.info("🎛️ Auto-tuning λ/μ parameters...")
try:
tuning_config = {
"auto_tuning": {
"lambda_auto": True,
"mu_auto": True,
"targets": {
"latency_p95_ms": target_p95_ms,
"cpu_p95_percent": target_cpu_p95 or 85.0
},
"tuning_parameters": {
"lambda_range": [0.1, 2.0],
"mu_range": [0.1, 2.0],
"step_size": 0.05,
"convergence_threshold": 0.01,
"max_iterations": 50
}
}
}
tuning_config_path = "auto_tuning_config.json"
with open(tuning_config_path, 'w') as f:
json.dump(tuning_config, f, indent=2)
self.logger.info(f" ✅ Created auto-tuning config for P95={target_p95_ms}ms")
return True
except Exception as e:
self.logger.error(f"❌ Auto-tuning setup failed: {e}")
return False
def main():
import argparse
parser = argparse.ArgumentParser(description="Lethe Regression Fix Tools")
parser.add_argument("--fix", choices=[
'frozen-pool', 'calibration', 'optimizer', 'dpp', 'kv-reuse', 'causal-closure', 'auto-tune'
], help="Specific fix to apply")
parser.add_argument("--manifest", help="Path to signed manifest for frozen pool fix")
parser.add_argument("--logged-policy", help="Path to logged policy data")
parser.add_argument("--target-p95", type=float, default=22.0, help="Target P95 latency (ms)")
args = parser.parse_args()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
fixer = RegressionFixer()
if args.fix == 'frozen-pool':
success = fixer.fix_frozen_pool_mismatch(args.manifest)
elif args.fix == 'calibration':
success = fixer.fix_calibration_ece_violation(args.logged_policy)
elif args.fix == 'optimizer':
success = fixer.fix_optimizer_misbehavior()
elif args.fix == 'dpp':
success = fixer.fix_dpp_instability()
elif args.fix == 'kv-reuse':
success = fixer.fix_kv_reuse_drops()
elif args.fix == 'causal-closure':
success = fixer.fix_causal_closure_violations()
elif args.fix == 'auto-tune':
success = fixer.auto_tune_lambda_mu(args.target_p95)
else:
print("Available fixes:")
for fix in ['frozen-pool', 'calibration', 'optimizer', 'dpp', 'kv-reuse', 'causal-closure', 'auto-tune']:
print(f" --fix {fix}")
success = True
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()