All files / web/src/lib/seed problem-generation.ts

89.2% Statements 190/213
95.65% Branches 44/46
75% Functions 9/12
89.2% Lines 190/213

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 2141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1672x 1672x 1x 1522x 1522x 1x 150x 150x 1x     1x     1x     1x 1x 1x 1x 1x 1x 76x 76x 72x 72x 1x 1x 1x 1x 1x 68x 68x 67x 67x 68x 51x 68x 9x 16x 2x 7x 1x 5x 1x 4x 1x 1x 68x 1x 1x 1x 1x 1x 33x 33x 33x 33x 23x 21x 21x 2x 33x 3x 33x 4x 4x 4x 4x 4x 4x 4x 4x 4x 33x 1x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 1x 33x 33x 1x 1x 1x 1x 1x 24x 24x 24x 24x 12x 12x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x     20x 1x 1x 1x 20x 20x 20x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 20x 20x 20x                               20x 20x 20x  
import { createEmptySkillSet, type SkillSet } from '../../types/tutorial'
import {
  generateSingleProblem,
  type GeneratedProblem as GenProblem,
} from '../../utils/problemGenerator'
import {
  getBaseComplexity,
  ROTATION_MULTIPLIERS,
  type SkillCostCalculator,
} from '../../utils/skillComplexity'
import type { RealisticProblem } from './types'
 
/**
 * Lightweight cost calculator for seed data.
 * Treats all skills as "in rotation" (multiplier = 3).
 * This ensures totalComplexityCost is populated in generation traces.
 */
const seedCostCalculator: SkillCostCalculator = {
  calculateSkillCost(skillId: string): number {
    return getBaseComplexity(skillId) * ROTATION_MULTIPLIERS.inRotation
  },
  calculateTermCost(skillIds: string[]): number {
    return skillIds.reduce((total, id) => total + this.calculateSkillCost(id), 0)
  },
  getIsPracticing(): boolean {
    return true
  },
  getMultiplier(): number {
    return ROTATION_MULTIPLIERS.inRotation
  },
  getBktResult(): undefined {
    return undefined
  },
  getMode() {
    return 'adaptive' as const
  },
}
 
/**
 * Maps a skill ID to the category and key for SkillSet modification
 */
export function parseSkillId(skillId: string): { category: string; key: string } | null {
  const parts = skillId.split('.')
  if (parts.length !== 2) return null
  return { category: parts[0], key: parts[1] }
}
 
/**
 * Enables a specific skill in a SkillSet (mutates the set)
 */
export function enableSkill(skillSet: SkillSet, skillId: string): void {
  const parsed = parseSkillId(skillId)
  if (!parsed) return
 
  const { category, key } = parsed
  if (category === 'basic' && key in skillSet.basic) {
    ;(skillSet.basic as Record<string, boolean>)[key] = true
  } else if (category === 'fiveComplements' && key in skillSet.fiveComplements) {
    ;(skillSet.fiveComplements as Record<string, boolean>)[key] = true
  } else if (category === 'tenComplements' && key in skillSet.tenComplements) {
    ;(skillSet.tenComplements as Record<string, boolean>)[key] = true
  } else if (category === 'fiveComplementsSub' && key in skillSet.fiveComplementsSub) {
    ;(skillSet.fiveComplementsSub as Record<string, boolean>)[key] = true
  } else if (category === 'tenComplementsSub' && key in skillSet.tenComplementsSub) {
    ;(skillSet.tenComplementsSub as Record<string, boolean>)[key] = true
  } else if (category === 'advanced' && key in skillSet.advanced) {
    ;(skillSet.advanced as Record<string, boolean>)[key] = true
  }
}
 
/**
 * Get prerequisite skills that must be enabled for a target skill to be reachable.
 */
export function getPrerequisiteSkills(skillId: string): string[] {
  const category = skillId.split('.')[0]
 
  switch (category) {
    case 'basic':
      if (skillId === 'basic.directAddition') {
        return []
      }
      return ['basic.directAddition']
    case 'fiveComplements':
      return ['basic.directAddition', 'basic.heavenBead']
    case 'tenComplements':
      return [
        'basic.directAddition',
        'basic.heavenBead',
        'basic.simpleCombinations',
        'fiveComplements.4=5-1',
        'fiveComplements.3=5-2',
        'fiveComplements.2=5-3',
        'fiveComplements.1=5-4',
      ]
    case 'fiveComplementsSub':
      return ['basic.directSubtraction', 'basic.heavenBeadSubtraction']
    case 'tenComplementsSub':
      return [
        'basic.directSubtraction',
        'basic.heavenBeadSubtraction',
        'basic.simpleCombinationsSub',
        'fiveComplementsSub.-4=-5+1',
        'fiveComplementsSub.-3=-5+2',
        'fiveComplementsSub.-2=-5+3',
        'fiveComplementsSub.-1=-5+4',
      ]
    default:
      return []
  }
}
 
/**
 * Creates a SkillSet that enables the target skill plus prerequisites
 */
export function createSkillSetForTarget(targetSkill: string): SkillSet {
  const skillSet = createEmptySkillSet()
 
  const prereqs = getPrerequisiteSkills(targetSkill)
  for (const prereq of prereqs) {
    enableSkill(skillSet, prereq)
  }
 
  enableSkill(skillSet, targetSkill)
 
  return skillSet
}
 
/**
 * Creates a target SkillSet with only the target skill enabled (for problem matching)
 */
export function createTargetSkillSet(targetSkill: string): Partial<SkillSet> {
  const skillSet = createEmptySkillSet()
  enableSkill(skillSet, targetSkill)
  return skillSet
}
 
/**
 * Generates a batch of realistic problems targeting a specific skill.
 * IMPORTANT: Only returns problems that actually exercise the target skill.
 * This ensures BKT sees the correct skill in skillsExercised.
 */
export function generateRealisticProblems(
  targetSkill: string,
  count: number,
  maxAttempts: number = 100
): RealisticProblem[] {
  const problems: RealisticProblem[] = []
  const allowedSkills = createSkillSetForTarget(targetSkill)
  const targetSkillSet = createTargetSkillSet(targetSkill)
 
  // Determine number range based on skill category
  const category = targetSkill.split('.')[0]
  let numberRange = { min: 1, max: 9 }
  let maxSum = 20
 
  if (category === 'tenComplements' || category === 'tenComplementsSub') {
    numberRange = { min: 1, max: 99 }
    maxSum = 200
  } else if (category === 'fiveComplements' || category === 'fiveComplementsSub') {
    numberRange = { min: 1, max: 9 }
    maxSum = 20
  }
 
  let attempts = 0
  while (problems.length < count && attempts < count * maxAttempts) {
    attempts++
 
    const problem = generateSingleProblem({
      constraints: {
        numberRange,
        maxSum,
        maxTerms: 3,
        minTerms: 2,
        problemCount: 1,
      },
      allowedSkills,
      targetSkills: targetSkillSet,
      costCalculator: seedCostCalculator,
      attempts: 20,
    })
 
    // STRICT: Only accept problems that actually use the target skill
    if (problem && problem.skillsUsed.includes(targetSkill)) {
      problems.push({
        terms: problem.terms,
        answer: problem.answer,
        // IMPORTANT: Force single-skill annotation for predictable BKT outcomes.
        skillsUsed: [targetSkill],
        generationTrace: problem.generationTrace,
      })
    }
  }
 
  // If we couldn't generate enough problems, synthesize
  if (problems.length < count) {
    console.warn(
      `[Seed] Could only generate ${problems.length}/${count} problems for ${targetSkill}. ` +
        `Synthesizing ${count - problems.length} more.`
    )

    while (problems.length < count) {
      const a = Math.floor(Math.random() * 8) + 1
      const b = Math.floor(Math.random() * 8) + 1
      problems.push({
        terms: [a, b],
        answer: a + b,
        skillsUsed: [targetSkill],
      })
    }
  }
 
  return problems
}