All files / web/src/lib/curriculum/bkt compute-bkt.ts

78.71% Statements 318/404
66.66% Branches 32/48
60% Functions 3/5
78.71% Lines 318/404

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 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 4052x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                             2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 28196x 28196x 28196x 28196x 28196x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 222163x 2410x 2410x 2410x 2410x 2410x 2410x 2410x 2410x 2410x 222163x 28285x 28285x 28285x 28285x 28285x 89x 89x 28285x 28285x 222163x 2410x 2410x 2410x 2410x 2410x 2410x 2410x 2410x 2410x 222163x 28285x 28285x 28285x 28285x                                     28285x 28285x 28285x 222163x 222163x 222163x 222163x 222163x 222163x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 28285x 13837x 14448x 28285x 28285x 28285x 28285x                     28285x 28285x 28285x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x                       222163x 222163x 222163x 222163x 222163x 222163x 222163x 28285x 28285x 28285x 28285x 28285x 222163x 222163x 222163x 222163x 222163x 222163x 28285x 28285x 28285x 13837x 14448x 28285x 28285x 28285x 28285x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 222163x 28285x 28285x 28285x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 2x 2x 2x 2x 2x 178x 178x 178x 178x 178x 178x 178x 178x 4820x 4820x 4820x 4820x 4820x 4820x                   4820x 4820x 4820x 4820x 4820x 4820x 4820x 4820x             4820x 4820x 4820x 4820x 4820x 4820x 4820x 4820x 4820x 4820x 4820x 4820x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4820x 4820x 4820x 4820x 4820x 4820x 4820x 230x 230x 4590x 4820x 3988x 4820x 576x 602x 26x 26x 4820x 2x 2x 2x 2x 2x 2x                                      
/**
 * Main BKT Computation Function
 *
 * This is the entry point for computing BKT state from problem history.
 * It replays all SlotResults in chronological order to build up
 * the current P(known) estimate for each skill.
 */
 
import { BKT_THRESHOLDS } from '../config/bkt-integration'
import type { ProblemResultWithContext } from '../session-planner'
import { calculateConfidence, getUncertaintyRange } from './confidence'
import { type BlameMethod, updateOnCorrect, updateOnIncorrectWithMethod } from './conjunctive-bkt'
import { helpWeight, responseTimeWeight } from './evidence-quality'
import { getDefaultParams } from './skill-priors'
import type {
  BktComputeOptions,
  BktComputeResult,
  BktModeResult,
  BktSkillState,
  MasteryClassification,
  SkillBktResult,
} from './types'
 
/** Extended options including blame method (not part of base BktComputeOptions to avoid breaking changes) */
export interface BktComputeExtendedOptions extends BktComputeOptions {
  /** Which blame attribution method to use for incorrect multi-skill problems */
  blameMethod?: BlameMethod
}
 
/**
 * Default computation options.
 */
export const DEFAULT_BKT_OPTIONS: BktComputeExtendedOptions = {
  confidenceThreshold: 0.5,
  useCrossStudentPriors: false,
  applyDecay: false,
  decayHalfLifeDays: 30,
  blameMethod: 'heuristic',
}
 
/**
 * Apply time-based decay to P(known).
 * Uses exponential decay toward the prior (pInit).
 *
 * @param pKnown - Current P(known) estimate
 * @param daysSinceLastPractice - Days since the skill was practiced
 * @param halfLifeDays - Half-life of decay in days
 * @param pInit - Prior probability to decay toward
 * @returns Decayed P(known)
 */
function applyTimeDecay(
  pKnown: number,
  daysSinceLastPractice: number,
  halfLifeDays: number,
  pInit: number
): number {
  if (daysSinceLastPractice <= 0) return pKnown

  // Exponential decay: after halfLifeDays, the "learned" portion decays by 50%
  // pKnown decays toward pInit (prior), not toward 0
  const decayFactor = 0.5 ** (daysSinceLastPractice / halfLifeDays)
  const learnedPortion = pKnown - pInit
  return pInit + learnedPortion * decayFactor
}
 
/**
 * Compute BKT state for all skills from problem history.
 *
 * This is the main entry point - call it when displaying the Skills Dashboard.
 * It replays all SlotResults in chronological order to compute the current
 * P(known) estimate for each skill encountered.
 *
 * @param results - Problem results from session history
 * @param options - Computation options (confidence threshold, blame method, etc.)
 * @returns BKT results for all skills, sorted by need for intervention
 */
export function computeBktFromHistory(
  results: ProblemResultWithContext[],
  options: Partial<BktComputeExtendedOptions> = {}
): BktComputeResult {
  // Merge with defaults so callers can override just what they need
  const opts: BktComputeExtendedOptions = {
    ...DEFAULT_BKT_OPTIONS,
    ...options,
  }
  // Sort by timestamp to replay in chronological order
  // Note: timestamp may be a Date or a string (from JSON serialization)
  const sorted = [...results].sort((a, b) => {
    const timeA =
      a.timestamp instanceof Date ? a.timestamp.getTime() : new Date(a.timestamp).getTime()
    const timeB =
      b.timestamp instanceof Date ? b.timestamp.getTime() : new Date(b.timestamp).getTime()
    return timeA - timeB
  })
 
  // Track state for each skill
  const skillStates = new Map<string, BktSkillState>()
 
  // Track per-mode skill states (mode → skillId → state)
  const modeSkillStates = new Map<string, Map<string, BktSkillState>>()
 
  // Process each problem result
  for (const result of sorted) {
    // Extract skill IDs from the result (skillsExercised tracks what was actually used)
    const skillIds = result.skillsExercised ?? []
    if (skillIds.length === 0) continue
 
    // Check if this is a recency-refresh sentinel record
    // These update lastPracticedAt but are ZERO-WEIGHT for BKT mastery (pKnown)
    const isRecencyRefresh = result.source === 'recency-refresh'
 
    // Ensure all skills have state initialized
    for (const skillId of skillIds) {
      if (!skillStates.has(skillId)) {
        const params = getDefaultParams(skillId)
        skillStates.set(skillId, {
          pKnown: params.pInit,
          opportunities: 0,
          successCount: 0,
          lastPracticedAt: null,
          params,
        })
      }
    }
 
    // Initialize per-mode skill states if this result has a partType
    const partType = (result as { partType?: string }).partType
    if (partType) {
      if (!modeSkillStates.has(partType)) {
        modeSkillStates.set(partType, new Map())
      }
      const modeMap = modeSkillStates.get(partType)!
      for (const skillId of skillIds) {
        if (!modeMap.has(skillId)) {
          const params = getDefaultParams(skillId)
          modeMap.set(skillId, {
            pKnown: params.pInit,
            opportunities: 0,
            successCount: 0,
            lastPracticedAt: null,
            params,
          })
        }
      }
    }
 
    // For recency-refresh sentinels, only update lastPracticedAt - skip BKT calculation
    if (isRecencyRefresh) {
      const timestamp =
        result.timestamp instanceof Date ? result.timestamp : new Date(result.timestamp)
      for (const skillId of skillIds) {
        const state = skillStates.get(skillId)!
        // Update lastPracticedAt if this is more recent
        if (!state.lastPracticedAt || timestamp > state.lastPracticedAt) {
          state.lastPracticedAt = timestamp
        }
        // Also update mode-specific lastPracticedAt
        if (partType) {
          const modeState = modeSkillStates.get(partType)?.get(skillId)
          if (modeState && (!modeState.lastPracticedAt || timestamp > modeState.lastPracticedAt)) {
            modeState.lastPracticedAt = timestamp
          }
        }
      }
      continue // Skip pKnown updates for sentinel records
    }
 
    // Build skill records for BKT update
    const skillRecords = skillIds.map((skillId: string) => {
      const state = skillStates.get(skillId)!
      return {
        skillId,
        pKnown: state.pKnown,
        params: state.params,
      }
    })
 
    // Calculate evidence weight based on help usage, response time, and retry status
    const helpW = helpWeight(result.hadHelp)
    const rtWeight = responseTimeWeight(result.responseTimeMs, result.isCorrect)
    // Apply mastery weight from retry system (1.0 for first attempt, 0.5 for first retry, 0.25 for second retry)
    // If masteryWeight is undefined (old data), default to 1.0
    const retryWeight = result.masteryWeight ?? 1.0
    const evidenceWeight = helpW * rtWeight * retryWeight
 
    // Compute BKT updates (conjunctive model)
    const blameMethod = opts.blameMethod ?? 'heuristic'
    const updates = result.isCorrect
      ? updateOnCorrect(skillRecords)
      : updateOnIncorrectWithMethod(skillRecords, blameMethod)
 
    // Check if any update produced NaN (indicates invalid input data)
    const hasInvalidUpdate = updates.some((u) => !Number.isFinite(u.updatedPKnown))
    if (hasInvalidUpdate) {
      console.warn(
        '[BKT] Skipping problem result due to invalid data - skills:',
        skillIds.join(', '),
        'timestamp:',
        result.timestamp,
        'Updates with NaN:',
        updates.filter((u) => !Number.isFinite(u.updatedPKnown)).map((u) => u.skillId)
      )
      continue // Skip this problem, don't corrupt skill states
    }
 
    // Apply updates with evidence weighting
    for (const update of updates) {
      const state = skillStates.get(update.skillId)!
 
      // Weighted blend between old and new pKnown based on evidence quality
      // When evidenceWeight = 1.0, use full update
      // When evidenceWeight < 1.0, stay closer to prior
      const newPKnown = state.pKnown * (1 - evidenceWeight) + update.updatedPKnown * evidenceWeight
 
      // Final safety check - if blending still produces NaN, preserve prior state
      if (!Number.isFinite(newPKnown)) {
        console.warn(
          '[BKT] Evidence blending produced NaN for skill:',
          update.skillId,
          'evidenceWeight:',
          evidenceWeight,
          'updatedPKnown:',
          update.updatedPKnown,
          '- preserving prior state'
        )
        continue
      }
 
      state.pKnown = newPKnown
      state.opportunities += 1
      if (result.isCorrect) state.successCount += 1
      state.lastPracticedAt =
        result.timestamp instanceof Date ? result.timestamp : new Date(result.timestamp)
    }
 
    // Per-mode BKT updates (independent computation using mode-specific priors)
    if (partType) {
      const modeMap = modeSkillStates.get(partType)!
      const modeSkillRecords = skillIds.map((skillId: string) => {
        const modeState = modeMap.get(skillId)!
        return {
          skillId,
          pKnown: modeState.pKnown,
          params: modeState.params,
        }
      })
 
      const modeUpdates = result.isCorrect
        ? updateOnCorrect(modeSkillRecords)
        : updateOnIncorrectWithMethod(modeSkillRecords, blameMethod)
 
      const hasModeInvalidUpdate = modeUpdates.some((u) => !Number.isFinite(u.updatedPKnown))
      if (!hasModeInvalidUpdate) {
        for (const update of modeUpdates) {
          const modeState = modeMap.get(update.skillId)!
          const newPKnown =
            modeState.pKnown * (1 - evidenceWeight) + update.updatedPKnown * evidenceWeight
 
          if (!Number.isFinite(newPKnown)) continue
 
          modeState.pKnown = newPKnown
          modeState.opportunities += 1
          if (result.isCorrect) modeState.successCount += 1
          modeState.lastPracticedAt =
            result.timestamp instanceof Date ? result.timestamp : new Date(result.timestamp)
        }
      }
    }
  }
 
  // Convert overall states to results
  const now = new Date()
  const overall = convertStatesToResults(skillStates, opts, now)
 
  // Build per-mode results
  const byMode: Partial<Record<string, BktModeResult>> = {}
  for (const [mode, modeMap] of modeSkillStates) {
    if (modeMap.size > 0) {
      byMode[mode] = convertStatesToResults(modeMap, opts, now)
    }
  }
 
  return {
    ...overall,
    byMode: Object.keys(byMode).length > 0 ? byMode : undefined,
  }
}
 
/**
 * Convert a map of skill states into sorted BKT results with classifications.
 * Reused for both overall and per-mode result computation.
 */
function convertStatesToResults(
  states: Map<string, BktSkillState>,
  opts: BktComputeExtendedOptions,
  now: Date
): { skills: SkillBktResult[]; interventionNeeded: SkillBktResult[]; strengths: SkillBktResult[] } {
  const skills: SkillBktResult[] = []
 
  for (const [skillId, state] of states) {
    const successRate = state.opportunities > 0 ? state.successCount / state.opportunities : 0.5
    const confidence = calculateConfidence(state.opportunities, successRate)
 
    // Apply decay if enabled
    let finalPKnown = state.pKnown
    if (opts.applyDecay && state.lastPracticedAt) {
      const daysSinceLastPractice =
        (now.getTime() - state.lastPracticedAt.getTime()) / (1000 * 60 * 60 * 24)
      finalPKnown = applyTimeDecay(
        state.pKnown,
        daysSinceLastPractice,
        opts.decayHalfLifeDays,
        state.params.pInit
      )
    }
 
    const uncertaintyRange = getUncertaintyRange(finalPKnown, confidence)
 
    // Classify mastery based on P(known) and confidence
    const masteryClassification = classifyMastery(finalPKnown, confidence, opts.confidenceThreshold)
 
    // Final safety check - this should not happen after skipping invalid updates
    if (!Number.isFinite(finalPKnown)) {
      console.warn(
        `[BKT] UNEXPECTED: Skill ${skillId} has corrupted pKnown after processing: ${finalPKnown}. ` +
          `Opportunities: ${state.opportunities}, SuccessCount: ${state.successCount}. ` +
          'This should not happen - invalid updates should have been skipped.'
      )
    }
 
    skills.push({
      skillId,
      pKnown: finalPKnown,
      confidence,
      uncertaintyRange,
      opportunities: state.opportunities,
      successCount: state.successCount,
      lastPracticedAt: state.lastPracticedAt,
      masteryClassification,
    })
  }
 
  // Sort by pKnown ascending (weak skills first)
  skills.sort((a, b) => a.pKnown - b.pKnown)
 
  // Identify intervention needed (low pKnown with sufficient confidence)
  const interventionNeeded = skills.filter((s) => s.masteryClassification === 'weak')
 
  // Identify strengths (high pKnown with sufficient confidence)
  const strengths = skills.filter((s) => s.masteryClassification === 'strong')
 
  return { skills, interventionNeeded, strengths }
}
 
/**
 * Classify a skill's mastery level based on P(known) and confidence.
 *
 * Uses unified thresholds from BKT_THRESHOLDS:
 * - strong: P(known) >= 0.8
 * - weak: P(known) < 0.5
 * - developing: everything in between (or insufficient confidence)
 */
function classifyMastery(
  pKnown: number,
  confidence: number,
  confidenceThreshold: number
): MasteryClassification {
  // Need sufficient confidence to make strong claims
  if (confidence < confidenceThreshold) {
    return 'developing' // Not enough data to be sure either way
  }
 
  if (pKnown >= BKT_THRESHOLDS.strong) {
    return 'strong'
  } else if (pKnown < BKT_THRESHOLDS.weak) {
    return 'weak'
  } else {
    return 'developing'
  }
}
 
/**
 * Recompute BKT with different options (e.g., different confidence threshold).
 * Useful for UI controls that let users adjust the threshold.
 */
export function recomputeWithOptions(
  bktResult: BktComputeResult,
  newOptions: BktComputeOptions
): BktComputeResult {
  // Re-classify all skills with new threshold
  const skills = bktResult.skills.map((skill) => ({
    ...skill,
    masteryClassification: classifyMastery(
      skill.pKnown,
      skill.confidence,
      newOptions.confidenceThreshold
    ),
  }))

  const interventionNeeded = skills.filter((s) => s.masteryClassification === 'weak')
  const strengths = skills.filter((s) => s.masteryClassification === 'strong')

  return { skills, interventionNeeded, strengths }
}