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 | /** * Journey Runner * * Orchestrates a simulated learning journey through multiple practice sessions. * Uses REAL session planner code (not mocked) to generate problems. * * The runner: * 1. Sets up practicing skills for the student * 2. Generates session plans using the real planner * 3. Simulates student answers based on their true mastery * 4. Records results to the database * 5. Computes BKT estimates after each session * 6. Calculates final metrics comparing BKT to ground truth */ import type { TestDatabase } from './EphemeralDatabase' import type { SeededRandom } from './SeededRandom' import type { SimulatedStudent } from './SimulatedStudent' import type { BktEstimate, JourneyConfig, JourneyMetrics, JourneyResult, SessionSnapshot, SkillTrajectory, } from './types' // These imports will use the mocked database import { computeBktFromHistory } from '@/lib/curriculum/bkt' import { approveSessionPlan, generateSessionPlan, getRecentSessionResults, recordSlotResult, startSessionPlan, } from '@/lib/curriculum/session-planner' import { setPracticingSkills } from '@/lib/curriculum/progress-manager' /** * Runs a simulated learning journey through multiple sessions. */ export class JourneyRunner { private db: TestDatabase private student: SimulatedStudent private config: JourneyConfig private rng: SeededRandom private playerId: string private originalMathRandom: () => number constructor( db: TestDatabase, student: SimulatedStudent, config: JourneyConfig, rng: SeededRandom, playerId: string ) { this.db = db this.student = student this.config = config this.rng = rng this.playerId = playerId this.originalMathRandom = Math.random } /** * Run the full journey simulation. * * @returns Complete journey result with all snapshots and metrics */ async run(): Promise<JourneyResult> { const startTime = Date.now() const snapshots: SessionSnapshot[] = [] // Ensure the student is tracking all skills (for exposure counting) this.student.ensureSkillsTracked(this.config.practicingSkills) // Set up practicing skills in the database await setPracticingSkills(this.playerId, this.config.practicingSkills) // Mock Math.random for deterministic problem generation Math.random = this.rng.createMathRandomMock() try { for (let sessionNum = 1; sessionNum <= this.config.sessionCount; sessionNum++) { const snapshot = await this.runSession(sessionNum) snapshots.push(snapshot) } } finally { // Restore Math.random Math.random = this.originalMathRandom } // Calculate final metrics const finalMetrics = this.calculateFinalMetrics(snapshots) const runtimeMs = Date.now() - startTime return { config: this.config, snapshots, finalMetrics, runtimeMs, } } /** * Run a single session and return a snapshot of the state. */ private async runSession(sessionNumber: number): Promise<SessionSnapshot> { // Check BKT data BEFORE generating session const preHistory = await getRecentSessionResults(this.playerId, 50) console.log( `[DEBUG] Session ${sessionNumber} PRE-GENERATION: ${preHistory.length} results available for BKT` ) // Generate session plan using REAL session planner const plan = await generateSessionPlan({ playerId: this.playerId, durationMinutes: this.config.sessionDurationMinutes, problemGenerationMode: this.config.mode, }) // Approve and start the session await approveSessionPlan(plan.id) const startedPlan = await startSessionPlan(plan.id) // Track skill exposures THIS session (separate from cumulative) const sessionExposures = new Map<string, number>() let correctCount = 0 let totalProblems = 0 let sessionFatigue = 0 // Process all parts and slots for (const part of startedPlan.parts) { for (const slot of part.slots) { if (!slot.problem) continue totalProblems++ // Simulate the student answering this problem // Note: This also increments exposure counts in the student model // and calculates fatigue based on true probabilities BEFORE learning const answer = this.student.answerProblem(slot.problem) if (answer.isCorrect) correctCount++ sessionFatigue += answer.fatigue // Track session-specific skill exposures for (const skillId of answer.skillsChallenged) { sessionExposures.set(skillId, (sessionExposures.get(skillId) ?? 0) + 1) } // Record result using real API await recordSlotResult(plan.id, { slotIndex: slot.index, problem: slot.problem, studentAnswer: answer.isCorrect ? slot.problem.answer : slot.problem.answer + 1, isCorrect: answer.isCorrect, responseTimeMs: answer.responseTimeMs, skillsExercised: answer.skillsChallenged, usedOnScreenAbacus: false, hadHelp: answer.hadHelp, incorrectAttempts: answer.isCorrect ? 0 : 1, helpTrigger: answer.hadHelp ? 'manual' : 'none', }) } } // DEBUG: Check session status after processing all slots const { getSessionPlan } = await import('@/lib/curriculum/session-planner') const finalPlan = await getSessionPlan(plan.id) console.log( `[DEBUG] Session ${sessionNumber} final status: ${finalPlan?.status}, results count: ${finalPlan?.results?.length ?? 0}` ) // Get BKT estimates after this session const problemHistory = await getRecentSessionResults(this.playerId, 50) console.log(`[DEBUG] getRecentSessionResults returned ${problemHistory.length} results`) // Check if skillsExercised is populated if (problemHistory.length > 0) { const sample = problemHistory[0] console.log( `[DEBUG] Sample result: isCorrect=${sample.isCorrect}, skillsExercised=${JSON.stringify(sample.skillsExercised)}` ) } const bktResult = computeBktFromHistory(problemHistory, { confidenceThreshold: 0.5, useCrossStudentPriors: false, applyDecay: false, decayHalfLifeDays: 30, blameMethod: this.config.blameMethod ?? 'heuristic', }) const bktEstimates = new Map<string, BktEstimate>() for (const skill of bktResult.skills) { bktEstimates.set(skill.skillId, { pKnown: skill.pKnown, confidence: skill.confidence, }) } return { sessionNumber, bktEstimates, // Cumulative exposures from student model (persists across sessions) cumulativeExposures: this.student.getAllExposures(), // Exposures just from this session sessionExposures, // True P(correct) per skill based on Hill function (ground truth) trueSkillProbabilities: this.student.getAllTrueProbabilities(), accuracy: totalProblems > 0 ? correctCount / totalProblems : 0, problemsAttempted: totalProblems, sessionPlanId: plan.id, // Cognitive fatigue accumulated during this session sessionFatigue, } } /** * Calculate final metrics from all session snapshots. */ private calculateFinalMetrics(snapshots: SessionSnapshot[]): JourneyMetrics { const lastSnapshot = snapshots[snapshots.length - 1] const firstSnapshot = snapshots[0] // Calculate BKT vs true mastery correlation const bktCorrelation = this.calculateCorrelation(lastSnapshot) // Calculate weak skill surfacing ratio const weakSkillSurfacing = this.calculateWeakSkillSurfacing(snapshots) // Calculate accuracy improvement const accuracyImprovement = lastSnapshot.accuracy - firstSnapshot.accuracy // Build skill trajectories const skillTrajectories = this.buildSkillTrajectories(snapshots) // Calculate total fatigue across all sessions const totalFatigue = snapshots.reduce((sum, s) => sum + s.sessionFatigue, 0) const avgFatiguePerSession = snapshots.length > 0 ? totalFatigue / snapshots.length : 0 return { bktCorrelation, weakSkillSurfacing, accuracyImprovement, skillTrajectories, totalFatigue, avgFatiguePerSession, } } /** * Calculate Pearson correlation between BKT pKnown and true probability (Hill function). */ private calculateCorrelation(snapshot: SessionSnapshot): number { const pairs: Array<[number, number]> = [] for (const skillId of this.config.practicingSkills) { // True probability from Hill function (ground truth) const trueProbability = snapshot.trueSkillProbabilities.get(skillId) ?? 0 // BKT's estimate const bktEstimate = snapshot.bktEstimates.get(skillId)?.pKnown ?? 0.5 pairs.push([trueProbability, bktEstimate]) } if (pairs.length < 2) return 0 // Pearson correlation coefficient const n = pairs.length const sumX = pairs.reduce((s, [x]) => s + x, 0) const sumY = pairs.reduce((s, [, y]) => s + y, 0) const sumXY = pairs.reduce((s, [x, y]) => s + x * y, 0) const sumX2 = pairs.reduce((s, [x]) => s + x * x, 0) const sumY2 = pairs.reduce((s, [, y]) => s + y * y, 0) const numerator = n * sumXY - sumX * sumY const denominator = Math.sqrt((n * sumX2 - sumX ** 2) * (n * sumY2 - sumY ** 2)) return denominator === 0 ? 0 : numerator / denominator } /** * Calculate weak skill surfacing ratio. * * This measures whether the session planner is exposing weak skills * more than their fair share (baseline expectation). * * - Ratio > 1.0 means weak skills are surfaced more than baseline * - Ratio < 1.0 means weak skills are under-represented * * A skill is considered "weak" if its true P(correct) based on * the Hill function is < 0.5. */ private calculateWeakSkillSurfacing(snapshots: SessionSnapshot[]): number { const lastSnapshot = snapshots[snapshots.length - 1] // Identify weak skills (true probability < 0.5 based on Hill function) const weakSkills = this.config.practicingSkills.filter( (id) => (lastSnapshot.trueSkillProbabilities.get(id) ?? 0) < 0.5 ) if (weakSkills.length === 0) return 1.0 // No weak skills // Count total session exposures for weak vs strong skills let weakExposures = 0 let strongExposures = 0 for (const snapshot of snapshots) { for (const skillId of this.config.practicingSkills) { const exposures = snapshot.sessionExposures.get(skillId) ?? 0 if (weakSkills.includes(skillId)) { weakExposures += exposures } else { strongExposures += exposures } } } const total = weakExposures + strongExposures if (total === 0) return 0 // Calculate ratio of actual weak exposure to expected const weakRatio = weakExposures / total const expectedRatio = weakSkills.length / this.config.practicingSkills.length return expectedRatio > 0 ? weakRatio / expectedRatio : 1.0 } /** * Build skill trajectories across all sessions. */ private buildSkillTrajectories(snapshots: SessionSnapshot[]): Map<string, SkillTrajectory> { const trajectories = new Map<string, SkillTrajectory>() for (const skillId of this.config.practicingSkills) { const dataPoints = snapshots.map((s) => ({ session: s.sessionNumber, // True P(correct) from Hill function (ground truth) trueProbability: s.trueSkillProbabilities.get(skillId) ?? 0, // Cumulative exposures for this skill cumulativeExposures: s.cumulativeExposures.get(skillId) ?? 0, // BKT estimates bktPKnown: s.bktEstimates.get(skillId)?.pKnown ?? 0.5, bktConfidence: s.bktEstimates.get(skillId)?.confidence ?? 0, accuracy: s.accuracy, })) trajectories.set(skillId, { skillId, dataPoints }) } return trajectories } /** * Get the player ID used in this journey. */ getPlayerId(): string { return this.playerId } } |