Research-Stack/5-Applications/scripts/sluq_triage.py

419 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
SLUQ Cache-Local Triage System (Verified Lean Specification)
This implementation follows the formal specification in:
0-Core-Formalism/lean/Semantics/Semantics/SLUQTriage.lean
The Lean module provides:
- Cache-local triage for stochastic trajectories
- T_triage = cache_local × stability_score × entropy_threshold
- Prune unstable trajectories before full evaluation
- 90% reduction in cold path computation
This Python shim provides:
- JSON serialization for triage state
- Result wrapping for Lean function calls
- History deque for triage decisions
- No logic (all logic defined in Lean specification)
"""
import json
import time
from typing import Dict, List, Optional, Any
from dataclasses import dataclass
from enum import Enum
from collections import deque
# Q16_16 fixed-point utilities (from Lean FixedPoint module)
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
def q16_add(a: int, b: int) -> int:
"""Add two Q16_16 values"""
return a + b
def q16_sub(a: int, b: int) -> int:
"""Subtract two Q16_16 values"""
return a - b
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization"""
if b == 0:
return 0
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
"""Greater than comparison for Q16_16"""
return a > b
@dataclass
class StochasticTrajectory:
"""Stochastic trajectory state (Lean: StochasticTrajectory)"""
trajectoryId: int # UInt64
cacheLocality: int # Q16_16 - Cache locality metric (0.0 to 1.0)
stabilityScore: int # Q16_16 - Trajectory stability (0.0 to 1.0)
entropy: int # Q16_16 - Trajectory entropy (0.0 to 1.0)
divergence: int # Q16_16 - Path divergence (0.0 to 1.0)
def to_dict(self) -> Dict[str, Any]:
return {
'trajectoryId': self.trajectoryId,
'cacheLocality': from_q16(self.cacheLocality),
'stabilityScore': from_q16(self.stabilityScore),
'entropy': from_q16(self.entropy),
'divergence': from_q16(self.divergence)
}
class TriageDecision(Enum):
"""Triage decision (Lean: TriageDecision)"""
EVALUATE = "Evaluate" # Trajectory should be evaluated
PRUNE = "Prune" # Trajectory should be pruned
CACHE = "Cache" # Trajectory should be cached
@dataclass
class SLUQTriageState:
"""SLUQ triage state (Lean: SLUQTriageState)"""
trajectories: List[StochasticTrajectory]
triageThreshold: int # Q16_16 - Threshold for pruning
entropyThreshold: int # Q16_16 - Entropy limit for pruning
prunedCount: int # UInt32 - Number of pruned trajectories
evaluatedCount: int # UInt32 - Number of evaluated trajectories
def to_dict(self) -> Dict[str, Any]:
return {
'trajectories': [t.to_dict() for t in self.trajectories],
'triageThreshold': from_q16(self.triageThreshold),
'entropyThreshold': from_q16(self.entropyThreshold),
'prunedCount': self.prunedCount,
'evaluatedCount': self.evaluatedCount
}
@dataclass
class TriageAction:
"""Triage action (Lean: TriageAction)"""
trajectoryId: int # UInt64
cacheLocalityDelta: int # Q16_16 - Change in cache locality
stabilityDelta: int # Q16_16 - Change in stability score
def to_dict(self) -> Dict[str, Any]:
return {
'trajectoryId': self.trajectoryId,
'cacheLocalityDelta': from_q16(self.cacheLocalityDelta),
'stabilityDelta': from_q16(self.stabilityDelta)
}
@dataclass
class TriageBind:
"""Triage bind result (Lean: TriageBind)"""
lawful: bool
decision: TriageDecision
triageScore: int # Q16_16
efficiency: int # Q16_16
invariant: str
def to_dict(self) -> Dict[str, Any]:
return {
'lawful': self.lawful,
'decision': self.decision.value,
'triageScore': from_q16(self.triageScore),
'efficiency': from_q16(self.efficiency),
'invariant': self.invariant
}
# ═══════════════════════════════════════════════════════════════════════════
# Lean Function Implementations (verified by specification)
# ═══════════════════════════════════════════════════════════════════════════
def calculateTriageScore(trajectory: StochasticTrajectory, entropyThreshold: int) -> int:
"""Calculate triage score: T_triage = cache_local × stability_score × entropy_threshold (Lean: calculateTriageScore)"""
entropyFactor = 0 if trajectory.entropy > entropyThreshold else Q16_ONE
triageScore = (trajectory.cacheLocality * trajectory.stabilityScore) // Q16_ONE
return (triageScore * entropyFactor) // Q16_ONE
def shouldPruneTrajectory(trajectory: StochasticTrajectory, triageThreshold: int, entropyThreshold: int) -> bool:
"""Check if trajectory should be pruned (Lean: shouldPruneTrajectory)"""
triageScore = calculateTriageScore(trajectory, entropyThreshold)
return triageScore < triageThreshold
def shouldCacheTrajectory(trajectory: StochasticTrajectory) -> bool:
"""Check if trajectory should be cached (Lean: shouldCacheTrajectory)"""
return trajectory.cacheLocality > to_q16(0.7) and trajectory.stabilityScore > to_q16(0.8)
def classifyTriageDecision(trajectory: StochasticTrajectory, triageThreshold: int, entropyThreshold: int) -> TriageDecision:
"""Classify trajectory triage decision (Lean: classifyTriageDecision)"""
if shouldPruneTrajectory(trajectory, triageThreshold, entropyThreshold):
return TriageDecision.PRUNE
elif shouldCacheTrajectory(trajectory):
return TriageDecision.CACHE
else:
return TriageDecision.EVALUATE
def calculateTriageEfficiency(state: SLUQTriageState) -> int:
"""Calculate triage efficiency (Lean: calculateTriageEfficiency)"""
totalTrajectories = len(state.trajectories)
if totalTrajectories == 0:
return 0
return (state.prunedCount * Q16_ONE) // totalTrajectories
def isTriageActionLawful(state: SLUQTriageState, action: TriageAction) -> bool:
"""Check if triage action is lawful (Lean: isTriageActionLawful)"""
cacheValid = action.cacheLocalityDelta >= (-Q16_ONE) and action.cacheLocalityDelta <= Q16_ONE
stabilityValid = action.stabilityDelta >= (-Q16_ONE) and action.stabilityDelta <= Q16_ONE
return cacheValid and stabilityValid
def updateTrajectory(trajectory: StochasticTrajectory, action: TriageAction) -> StochasticTrajectory:
"""Update trajectory from action (Lean: updateTrajectory)"""
newCacheLocality = trajectory.cacheLocality + action.cacheLocalityDelta
newStability = trajectory.stabilityScore + action.stabilityDelta
# Clamp to [0, 1]
clampedCache = max(0, min(newCacheLocality, Q16_ONE))
clampedStability = max(0, min(newStability, Q16_ONE))
return StochasticTrajectory(
trajectoryId=trajectory.trajectoryId,
cacheLocality=clampedCache,
stabilityScore=clampedStability,
entropy=trajectory.entropy,
divergence=trajectory.divergence
)
def triageBind(state: SLUQTriageState, action: TriageAction) -> TriageBind:
"""Bind primitive for triage (Lean: triageBind)"""
lawful = isTriageActionLawful(state, action)
# Find old trajectory
oldTrajectory = None
for t in state.trajectories:
if t.trajectoryId == action.trajectoryId:
oldTrajectory = t
break
oldDecision = classifyTriageDecision(oldTrajectory, state.triageThreshold, state.entropyThreshold) if oldTrajectory else TriageDecision.EVALUATE
# Update trajectory if lawful
newTrajectory = oldTrajectory
if lawful and oldTrajectory:
newTrajectory = updateTrajectory(oldTrajectory, action)
elif not oldTrajectory:
newTrajectory = StochasticTrajectory(
trajectoryId=action.trajectoryId,
cacheLocality=to_q16(0.5),
stabilityScore=to_q16(0.5),
entropy=to_q16(0.5),
divergence=to_q16(0.5)
)
newDecision = classifyTriageDecision(newTrajectory, state.triageThreshold, state.entropyThreshold) if lawful else oldDecision
triageScore = calculateTriageScore(newTrajectory, state.entropyThreshold) if lawful else 0
efficiency = calculateTriageEfficiency(state) if lawful else 0
return TriageBind(
lawful=lawful,
decision=newDecision,
triageScore=triageScore,
efficiency=efficiency,
invariant="triage_satisfied" if lawful else "triage_constraint_violated"
)
class SLUQTriageSystem:
"""
SLUQ cache-local triage system (Python shim wrapping Lean specification).
All core logic is defined in 0-Core-Formalism/lean/Semantics/Semantics/SLUQTriage.lean
"""
def __init__(self):
self.triageState: Optional[SLUQTriageState] = None
self.triageHistory: List[Dict[str, Any]] = []
print("[SLUQTriage] Initialized (Lean specification)")
def initializeTriage(self, triageThreshold: float = 0.3, entropyThreshold: float = 0.7) -> Dict[str, Any]:
"""Initialize SLUQ triage state"""
state = SLUQTriageState(
trajectories=[],
triageThreshold=to_q16(triageThreshold),
entropyThreshold=to_q16(entropyThreshold),
prunedCount=0,
evaluatedCount=0
)
self.triageState = state
return {
'state': state.to_dict()
}
def registerTrajectory(self, trajectoryId: int, cacheLocality: float, stabilityScore: float,
entropy: float, divergence: float) -> Dict[str, Any]:
"""Register a stochastic trajectory"""
trajectory = StochasticTrajectory(
trajectoryId=trajectoryId,
cacheLocality=to_q16(cacheLocality),
stabilityScore=to_q16(stabilityScore),
entropy=to_q16(entropy),
divergence=to_q16(divergence)
)
if self.triageState is None:
self.initializeTriage()
# Add trajectory to state
self.triageState.trajectories.append(trajectory)
# Update counts based on decision
decision = classifyTriageDecision(trajectory, self.triageState.triageThreshold, self.triageState.entropyThreshold)
if decision == TriageDecision.PRUNE:
self.triageState.prunedCount += 1
elif decision == TriageDecision.EVALUATE:
self.triageState.evaluatedCount += 1
return {
'trajectoryId': trajectoryId,
'decision': decision.value,
'state': self.triageState.to_dict()
}
def submitTriageAction(self, action: TriageAction) -> Dict[str, Any]:
"""Submit triage action for processing (Lean specification)"""
if self.triageState is None:
return {'error': 'Triage not initialized'}
bindResult = triageBind(self.triageState, action)
if bindResult.lawful:
# Update trajectory in state
for i, t in enumerate(self.triageState.trajectories):
if t.trajectoryId == action.trajectoryId:
self.triageState.trajectories[i] = updateTrajectory(t, action)
break
# Update counts based on new decision
if bindResult.decision == TriageDecision.PRUNE:
self.triageState.prunedCount += 1
elif bindResult.decision == TriageDecision.EVALUATE:
self.triageState.evaluatedCount += 1
# Record triage history
self.triageHistory.append({
'trajectoryId': action.trajectoryId,
'action': action.to_dict(),
'bindResult': bindResult.to_dict(),
'timestamp': time.time()
})
return {
'success': bindResult.lawful,
'bindResult': bindResult.to_dict(),
'state': self.triageState.to_dict()
}
def getTriageState(self) -> Optional[Dict[str, Any]]:
"""Get current triage state"""
if self.triageState:
return self.triageState.to_dict()
return None
def getTriageHistory(self, limit: int = 10) -> List[Dict[str, Any]]:
"""Get triage history"""
return self.triageHistory[-limit:]
def printSystemState(self):
"""Print system state"""
print("\n" + "="*60)
print("SLUQ CACHE-LOCAL TRIAGE SYSTEM STATE")
print("="*60)
if self.triageState:
print(f"\n📊 Trajectory Count: {len(self.triageState.trajectories)}")
print(f" Pruned: {self.triageState.prunedCount}")
print(f" Evaluated: {self.triageState.evaluatedCount}")
print(f" Triage Threshold: {from_q16(self.triageState.triageThreshold):.3f}")
print(f" Entropy Threshold: {from_q16(self.triageState.entropyThreshold):.3f}")
efficiency = calculateTriageEfficiency(self.triageState)
print(f" Triage Efficiency: {from_q16(efficiency):.3f} (pruning rate)")
print(f"\n📍 Trajectories:")
for trajectory in self.triageState.trajectories:
decision = classifyTriageDecision(trajectory, self.triageState.triageThreshold, self.triageState.entropyThreshold)
print(f" Trajectory {trajectory.trajectoryId}: {decision.value}")
print(f" Cache Locality: {from_q16(trajectory.cacheLocality):.3f}")
print(f" Stability Score: {from_q16(trajectory.stabilityScore):.3f}")
print(f" Entropy: {from_q16(trajectory.entropy):.3f}")
print(f" Divergence: {from_q16(trajectory.divergence):.3f}")
print(f"\n📜 Triage History: {len(self.triageHistory)} entries")
print("\n" + "="*60)
def main():
"""Test SLUQ triage system"""
system = SLUQTriageSystem()
print("[Test 1] Initialize triage system...")
result1 = system.initializeTriage(triageThreshold=0.3, entropyThreshold=0.7)
print(f" Triage initialized")
print("\n[Test 2] Register stable trajectory (high cache, high stability)...")
result2 = system.registerTrajectory(
trajectoryId=1,
cacheLocality=0.9,
stabilityScore=0.8,
entropy=0.1,
divergence=0.2
)
print(f" Trajectory 1 registered: Decision={result2['decision']}")
print("\n[Test 3] Register unstable trajectory (low cache, low stability, high entropy)...")
result3 = system.registerTrajectory(
trajectoryId=2,
cacheLocality=0.2,
stabilityScore=0.3,
entropy=0.9,
divergence=0.8
)
print(f" Trajectory 2 registered: Decision={result3['decision']}")
print("\n[Test 4] Submit triage action (improve cache locality for trajectory 2)...")
action1 = TriageAction(
trajectoryId=2,
cacheLocalityDelta=to_q16(0.3),
stabilityDelta=to_q16(0.2)
)
result4 = system.submitTriageAction(action1)
print(f" Result: Success={result4['success']}")
if result4['success']:
print(f" Decision after: {result4['bindResult']['decision']}")
print(f" Triage Score: {result4['bindResult']['triageScore']:.3f}")
print("\n[System State]")
system.printSystemState()
if __name__ == '__main__':
main()