Spaces:
Runtime error

Vidya-V3-Tutor / src /phase_manager.py
TrueTensor's picture
Upload 9 files
8ab5cfe verified
Raw History Blame Contribute Delete
3.63 kB
"""
phase_manager.py β€” Phase assignment and fallback logic.
Two responsibilities:
1. mastery_to_phase() β€” assigns initial CA phase from mastery_level + ICP
2. update_mastery() β€” updates mastery_level each turn from signal scores
3. fallback_phase_update() β€” safety net when Gemini returns invalid phase
4. validate_phase() β€” checks phase string is a valid enum value
"""
from src.icp_profiles import mastery_to_phase # re-exported for convenience
_PHASE_ORDER = ["MODEL", "COACH", "SCAFFOLD", "FADE"]
# ── Mastery update constants ──────────────────────────────────────────────────
MASTERY_BOOST = 0.04 # confidence + effort β†’ mastery goes up
MASTERY_DROP = 0.02 # confusion or frustration β†’ mastery goes down
MASTERY_MIN = 0.0
MASTERY_MAX = 1.0
# Signal score thresholds (0–100 scale from SignalScorecard)
HIGH_THRESHOLD = 65.0
LOW_THRESHOLD = 35.0
def update_mastery(
current_mastery: float,
signal_scores: dict[str, float],
) -> float:
"""
Updates mastery_level every turn based on current signal scores.
Rules:
- confidence AND effort both high (>= 65) β†’ mastery +0.04
- confidence AND effort both moderate (>= 55) β†’ mastery +0.02
(catches Turn 1 where scores haven't built up yet but intent is clear)
- confusion OR frustration high (>= 65) β†’ mastery -0.02
- otherwise β†’ mastery unchanged
Returns new mastery_level clamped to [0.0, 1.0].
"""
confidence = signal_scores.get("confidence", 50)
effort = signal_scores.get("effort", 50)
confusion = signal_scores.get("confusion", 50)
frustration = signal_scores.get("frustration",50)
delta = 0.0
if confidence >= HIGH_THRESHOLD and effort >= HIGH_THRESHOLD:
# Both clearly active β†’ full boost
delta = +MASTERY_BOOST
elif confidence >= 55 and effort >= 55:
# Both moderate β€” user is trying and somewhat confident
# (common on Turn 1 before scores accumulate)
delta = +MASTERY_BOOST / 2 # +0.02
if confusion >= HIGH_THRESHOLD or frustration >= HIGH_THRESHOLD:
delta -= MASTERY_DROP
new_mastery = current_mastery + delta
return round(max(MASTERY_MIN, min(MASTERY_MAX, new_mastery)), 3)
def phase_from_mastery(mastery_level: float, icp_type: str) -> str:
"""Wrapper around icp_profiles.mastery_to_phase for convenience."""
return mastery_to_phase(mastery_level, icp_type)
def fallback_phase_update(
current_phase: str,
signal_scores: dict[str, float],
) -> str:
"""
Fallback CA phase decision using signal scores.
Only called when Gemini returns an invalid phase value.
"""
idx = _PHASE_ORDER.index(current_phase) if current_phase in _PHASE_ORDER else 0
confusion_high = signal_scores.get("confusion", 50) >= HIGH_THRESHOLD
frustration_high = signal_scores.get("frustration", 50) >= HIGH_THRESHOLD
confidence_high = signal_scores.get("confidence", 50) >= HIGH_THRESHOLD
effort_high = signal_scores.get("effort", 50) >= HIGH_THRESHOLD
if frustration_high or confusion_high:
new_idx = max(0, idx - 1)
elif confidence_high and effort_high:
new_idx = min(len(_PHASE_ORDER) - 1, idx + 1)
else:
new_idx = idx
return _PHASE_ORDER[new_idx]
def validate_phase(phase_value: str, current_phase: str) -> str:
"""Returns phase_value if valid, otherwise current_phase."""
return phase_value if phase_value in _PHASE_ORDER else current_phase