All files / web/src/components/practice autoPauseCalculator.ts

94.2% Statements 325/345
100% Branches 44/44
88.88% Functions 8/9
94.2% Lines 325/345

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 3461x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 2x 2x 10x 10x 10x 10x 10x 12x 1x 1x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 7x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 4x 1x 1x 3x 3x 4x 4x 4x 4x 4x 2x 2x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                         1x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 52x 52x 52x 52x 10x 10x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 45x 45x 4x 4x 4x 41x 41x 12x 12x 2x 2x 10x 10x 10x 10x 12x 1x 1x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 8x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 24x 10x 10x 10x 14x 14x 14x  
/**
 * Auto-pause threshold calculation utilities
 *
 * Calculates when to auto-pause based on the student's response times.
 * Uses mean + 2*stdDev with clamping between 30s and 5 minutes.
 */
 
import type { SlotResult } from '@/db/schema/session-plans'
import type { ProgressiveAssistanceTimingConfig } from '@/constants/helpTiming'
 
// ============================================================================
// Constants
// ============================================================================
 
/** Default timeout when not enough samples for statistics (5 minutes) */
export const DEFAULT_PAUSE_TIMEOUT_MS = 5 * 60 * 1000
 
/** Minimum problems needed for statistical calculation */
export const MIN_SAMPLES_FOR_STATISTICS = 5
 
/** Minimum clamp for the auto-pause threshold (30 seconds) */
export const MIN_PAUSE_THRESHOLD_MS = 30_000
 
/** Maximum clamp for the auto-pause threshold (5 minutes) */
export const MAX_PAUSE_THRESHOLD_MS = DEFAULT_PAUSE_TIMEOUT_MS
 
// ============================================================================
// Types
// ============================================================================
 
/**
 * Auto-pause statistics for display and debugging
 */
export interface AutoPauseStats {
  /** Mean response time in milliseconds */
  meanMs: number
  /** Standard deviation of response times in milliseconds */
  stdDevMs: number
  /** Calculated threshold (mean + 2*stdDev) in milliseconds */
  thresholdMs: number
  /** Number of samples used to calculate stats */
  sampleCount: number
  /** Whether statistical calculation was used (vs default timeout) */
  usedStatistics: boolean
  /** Mean response time per complexity-cost unit (ms/cost), if complexity scaling was used */
  meanPerUnitMs?: number
  /** Complexity cost of the current problem, if available */
  currentProblemCost?: number
  /** True when complexity cost data is missing (data quality issue) */
  complexityCostMissing?: boolean
}
 
/**
 * Information about why a session was paused
 */
export interface PauseInfo {
  /** When the pause occurred */
  pausedAt: Date
  /** Why the session was paused */
  reason: 'manual' | 'auto-timeout' | 'teacher'
  /** Auto-pause statistics (only present for auto-timeout) */
  autoPauseStats?: AutoPauseStats
  /** Teacher's custom message (only present for teacher-initiated pause) */
  teacherMessage?: string
}
 
/**
 * Response time statistics
 */
export interface ResponseTimeStats {
  /** Mean response time in milliseconds */
  mean: number
  /** Standard deviation of response times in milliseconds */
  stdDev: number
  /** Number of samples */
  count: number
}
 
// ============================================================================
// Functions
// ============================================================================
 
/**
 * Calculate mean and standard deviation of response times
 */
export function calculateResponseTimeStats(results: SlotResult[]): ResponseTimeStats {
  if (results.length === 0) {
    return { mean: 0, stdDev: 0, count: 0 }
  }
 
  const times = results.map((r) => r.responseTimeMs)
  const count = times.length
  const mean = times.reduce((sum, t) => sum + t, 0) / count
 
  if (count < 2) {
    return { mean, stdDev: 0, count }
  }
 
  const squaredDiffs = times.map((t) => (t - mean) ** 2)
  const variance = squaredDiffs.reduce((sum, d) => sum + d, 0) / (count - 1) // Sample std dev
  const stdDev = Math.sqrt(variance)
 
  return { mean, stdDev, count }
}
 
/**
 * Calculate the auto-pause threshold and full stats for display.
 *
 * @returns threshold in ms and stats for debugging/display
 */
export function calculateAutoPauseInfo(results: SlotResult[]): {
  threshold: number
  stats: AutoPauseStats
} {
  const { mean, stdDev, count } = calculateResponseTimeStats(results)
  const usedStatistics = count >= MIN_SAMPLES_FOR_STATISTICS
 
  let threshold: number
  if (usedStatistics) {
    // Use mean + 2 standard deviations
    threshold = mean + 2 * stdDev
    // Clamp between 30 seconds and 5 minutes
    threshold = Math.max(MIN_PAUSE_THRESHOLD_MS, Math.min(threshold, MAX_PAUSE_THRESHOLD_MS))
  } else {
    threshold = DEFAULT_PAUSE_TIMEOUT_MS
  }
 
  return {
    threshold,
    stats: {
      meanMs: mean,
      stdDevMs: stdDev,
      thresholdMs: threshold,
      sampleCount: count,
      usedStatistics,
    },
  }
}
 
/**
 * Get a human-readable explanation of how the auto-pause threshold was calculated.
 * Used in the SessionOverview component to explain the timing.
 */
export function getAutoPauseExplanation(stats: AutoPauseStats): string {
  if (!stats.usedStatistics) {
    return `Default timeout (${formatMs(stats.thresholdMs)}) - need ${MIN_SAMPLES_FOR_STATISTICS}+ problems for statistical calculation`
  }
 
  const rawThreshold = stats.meanMs + 2 * stats.stdDevMs
  const wasClamped = rawThreshold < MIN_PAUSE_THRESHOLD_MS || rawThreshold > MAX_PAUSE_THRESHOLD_MS
 
  let explanation = `mean (${formatMs(stats.meanMs)}) + 2×stdDev (${formatMs(stats.stdDevMs)}) = ${formatMs(rawThreshold)}`
 
  if (wasClamped) {
    explanation += ` → clamped to ${formatMs(stats.thresholdMs)}`
  }
 
  return explanation
}
 
// ============================================================================
// Progressive Assistance Thresholds
// ============================================================================
 
/**
 * Thresholds for progressive assistance escalation.
 * All values in milliseconds.
 */
export interface ProgressiveThresholds {
  /** When to show encouragement text (based on mean response time) */
  encouragementMs: number
  /** When to offer help button (based on mean + 1σ) */
  helpOfferMs: number
  /** When to auto-pause (based on mean + 2σ) */
  autoPauseMs: number
}
 
/**
 * Calculate progressive assistance thresholds from historical response times.
 *
 * Uses the same statistical approach as auto-pause but with three escalation levels:
 * - Encouragement: mean response time (clamped)
 * - Help offer: mean + 1σ (clamped)
 * - Auto-pause: mean + 2σ (clamped) — same formula as existing auto-pause
 */
export function calculateProgressiveThresholds(
  results: SlotResult[],
  timing: ProgressiveAssistanceTimingConfig
): ProgressiveThresholds {
  const { mean, stdDev, count } = calculateResponseTimeStats(results)
  const hasSufficientData = count >= MIN_SAMPLES_FOR_STATISTICS

  if (!hasSufficientData) {
    return {
      encouragementMs: timing.defaultEncouragementMs,
      helpOfferMs: timing.defaultHelpOfferMs,
      autoPauseMs: DEFAULT_PAUSE_TIMEOUT_MS,
    }
  }

  return {
    encouragementMs: clamp(mean, timing.minEncouragementMs, timing.maxEncouragementMs),
    helpOfferMs: clamp(mean + stdDev, timing.minHelpOfferMs, timing.maxHelpOfferMs),
    autoPauseMs: clamp(mean + 2 * stdDev, timing.minAutoPauseMs, timing.maxAutoPauseMs),
  }
}
 
function clamp(value: number, min: number, max: number): number {
  return Math.max(min, Math.min(value, max))
}
 
// ============================================================================
// Complexity-Scaled Thresholds
// ============================================================================
 
/**
 * Normalized response time statistics (per complexity-cost unit)
 */
export interface NormalizedResponseTimeStats {
  /** Mean response time per unit of complexity cost (ms/cost) */
  meanPerUnit: number
  /** Standard deviation per unit of complexity cost (ms/cost) */
  stdDevPerUnit: number
  /** Number of results with valid complexity costs used */
  count: number
  /** Number of results skipped due to missing or zero complexity cost */
  skippedCount: number
}
 
/**
 * Extract the totalComplexityCost from a problem's generation trace.
 * Returns the cost if present and > 0, otherwise null.
 *
 * Note: basic skills (directAddition, directSubtraction, etc.) have base
 * complexity 0, so their totalComplexityCost is legitimately 0/undefined.
 * This is NOT a data quality issue — use `hasGenerationTrace` to distinguish.
 */
export function getComplexityCost(
  problem: { generationTrace?: { totalComplexityCost?: number } } | null | undefined
): number | null {
  const cost = problem?.generationTrace?.totalComplexityCost
  if (cost != null && cost > 0) return cost
  return null
}
 
/**
 * Calculate normalized response time statistics by dividing each response time
 * by its problem's complexity cost. Results without valid complexity cost are skipped.
 */
export function calculateNormalizedResponseTimeStats(
  results: SlotResult[]
): NormalizedResponseTimeStats {
  const normalized: number[] = []
  let skippedCount = 0
 
  for (const r of results) {
    const cost = getComplexityCost(r.problem)
    if (cost === null) {
      skippedCount++
      continue
    }
    normalized.push(r.responseTimeMs / cost)
  }
 
  if (normalized.length === 0) {
    return { meanPerUnit: 0, stdDevPerUnit: 0, count: 0, skippedCount }
  }
 
  const count = normalized.length
  const meanPerUnit = normalized.reduce((sum, v) => sum + v, 0) / count
 
  if (count < 2) {
    return { meanPerUnit, stdDevPerUnit: 0, count, skippedCount }
  }
 
  const squaredDiffs = normalized.map((v) => (v - meanPerUnit) ** 2)
  const variance = squaredDiffs.reduce((sum, d) => sum + d, 0) / (count - 1)
  const stdDevPerUnit = Math.sqrt(variance)
 
  return { meanPerUnit, stdDevPerUnit, count, skippedCount }
}
 
/**
 * Calculate progressive assistance thresholds scaled by the current problem's
 * complexity cost. Harder problems get proportionally more time.
 *
 * When currentProblemCost is null (missing trace/data), returns flat 5-minute defaults.
 * When fewer than MIN_SAMPLES_FOR_STATISTICS results have valid costs, returns timing defaults.
 */
export function calculateComplexityScaledThresholds(
  results: SlotResult[],
  timing: ProgressiveAssistanceTimingConfig,
  currentProblemCost: number | null
): ProgressiveThresholds {
  // Missing cost → flat defaults (data quality issue — no generation trace)
  if (currentProblemCost === null) {
    return {
      encouragementMs: DEFAULT_PAUSE_TIMEOUT_MS,
      helpOfferMs: DEFAULT_PAUSE_TIMEOUT_MS,
      autoPauseMs: DEFAULT_PAUSE_TIMEOUT_MS,
    }
  }
 
  const { meanPerUnit, stdDevPerUnit, count } = calculateNormalizedResponseTimeStats(results)
 
  // Not enough data with valid costs → timing defaults (same as flat path)
  if (count < MIN_SAMPLES_FOR_STATISTICS) {
    return {
      encouragementMs: timing.defaultEncouragementMs,
      helpOfferMs: timing.defaultHelpOfferMs,
      autoPauseMs: DEFAULT_PAUSE_TIMEOUT_MS,
    }
  }
 
  // Scale normalized stats back up by current problem's cost
  return {
    encouragementMs: clamp(
      meanPerUnit * currentProblemCost,
      timing.minEncouragementMs,
      timing.maxEncouragementMs
    ),
    helpOfferMs: clamp(
      (meanPerUnit + stdDevPerUnit) * currentProblemCost,
      timing.minHelpOfferMs,
      timing.maxHelpOfferMs
    ),
    autoPauseMs: clamp(
      (meanPerUnit + 2 * stdDevPerUnit) * currentProblemCost,
      timing.minAutoPauseMs,
      timing.maxAutoPauseMs
    ),
  }
}
 
/**
 * Format milliseconds as a human-readable time string
 */
export function formatMs(ms: number): string {
  if (ms >= 60_000) {
    const minutes = ms / 60_000
    return `${minutes.toFixed(1)}m`
  }
  const seconds = ms / 1000
  return `${seconds.toFixed(1)}s`
}