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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8406x 8406x 8406x 8406x 8406x 8406x 8406x 4x 4x 4x 4x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x 8410x | /**
* Problem Generator - Generates problems from slot constraints
*
* Extracted from ActiveSession.tsx to be shared between:
* - Server-side plan generation (session-planner.ts)
* - Client-side fallback (ActiveSession.tsx)
*
* This is a pure function with no side effects, so it works in both environments.
*/
import type { GeneratedProblem, ProblemConstraints } from '@/db/schema/session-plans'
import { createBasicSkillSet, type SkillSet } from '@/types/tutorial'
import {
type GenerationDiagnostics,
type ProblemConstraints as GeneratorConstraints,
generateSingleProblemWithDiagnostics,
} from '@/utils/problemGenerator'
import type { SkillCostCalculator } from '@/utils/skillComplexity'
/**
* Error thrown when problem generation fails
*/
export class ProblemGenerationError extends Error {
constructor(
message: string,
public readonly constraints: ProblemConstraints,
public readonly diagnostics?: GenerationDiagnostics
) {
super(message)
this.name = 'ProblemGenerationError'
}
}
/**
* Format diagnostics into an actionable error message
*/
function formatDiagnosticsMessage(diagnostics: GenerationDiagnostics): string {
const lines: string[] = []
// Identify the main failure mode
if (diagnostics.sequenceFailures === diagnostics.totalAttempts) {
lines.push('CAUSE: All attempts failed during sequence generation.')
lines.push(
'This means no valid sequence of terms could be built with the given skill/budget constraints.'
)
if (diagnostics.enabledAllowedSkills.length === 0) {
lines.push('FIX: No allowed skills are enabled - enable at least some basic skills.')
} else {
lines.push(`Enabled skills: ${diagnostics.enabledAllowedSkills.slice(0, 5).join(', ')}...`)
}
} else if (diagnostics.skillMatchFailures > 0) {
lines.push(
`CAUSE: ${diagnostics.skillMatchFailures}/${diagnostics.totalAttempts} attempts generated problems but they didn't match skill requirements.`
)
if (diagnostics.lastGeneratedSkills) {
lines.push(`Last problem used skills: ${diagnostics.lastGeneratedSkills.join(', ')}`)
}
if (diagnostics.enabledTargetSkills.length > 0) {
lines.push(
`Target skills required: ${diagnostics.enabledTargetSkills.slice(0, 5).join(', ')}`
)
lines.push('FIX: Generated problems may not naturally use the target skills.')
}
} else if (diagnostics.sumConstraintFailures > 0) {
lines.push(`CAUSE: ${diagnostics.sumConstraintFailures} attempts failed sum constraints.`)
lines.push('FIX: Adjust min/max sum constraints or number range.')
}
// Add stats
lines.push('')
lines.push(
`Stats: ${diagnostics.totalAttempts} attempts = ${diagnostics.sequenceFailures} seq failures + ${diagnostics.sumConstraintFailures} sum failures + ${diagnostics.skillMatchFailures} skill failures`
)
return lines.join('\n')
}
/**
* Generate a problem from slot constraints using the skill-based algorithm.
*
* @param constraints - The constraints for problem generation (skills, digit range, term count)
* @param costCalculator - Optional student-aware calculator for complexity budget enforcement
* @returns A generated problem with terms, answer, and skills required
* @throws {ProblemGenerationError} If the generator fails to produce a valid problem
*/
export function generateProblemFromConstraints(
constraints: ProblemConstraints,
costCalculator?: SkillCostCalculator
): GeneratedProblem {
const baseSkillSet = createBasicSkillSet()
const allowedSkills: SkillSet = {
basic: { ...baseSkillSet.basic, ...constraints.allowedSkills?.basic },
fiveComplements: {
...baseSkillSet.fiveComplements,
...constraints.allowedSkills?.fiveComplements,
},
tenComplements: {
...baseSkillSet.tenComplements,
...constraints.allowedSkills?.tenComplements,
},
fiveComplementsSub: {
...baseSkillSet.fiveComplementsSub,
...constraints.allowedSkills?.fiveComplementsSub,
},
tenComplementsSub: {
...baseSkillSet.tenComplementsSub,
...constraints.allowedSkills?.tenComplementsSub,
},
advanced: {
...baseSkillSet.advanced,
...constraints.allowedSkills?.advanced,
},
}
const maxDigits = constraints.digitRange?.max || 1
const maxValue = 10 ** maxDigits - 1
const generatorConstraints: GeneratorConstraints = {
numberRange: { min: 1, max: maxValue },
minTerms: constraints.termCount?.min || 3,
maxTerms: constraints.termCount?.max || 5,
problemCount: 1,
minComplexityBudgetPerTerm: constraints.minComplexityBudgetPerTerm,
maxComplexityBudgetPerTerm: constraints.maxComplexityBudgetPerTerm,
}
const { problem: generatedProblem, diagnostics } = generateSingleProblemWithDiagnostics({
constraints: generatorConstraints,
allowedSkills,
targetSkills: constraints.targetSkills,
forbiddenSkills: constraints.forbiddenSkills,
costCalculator,
})
if (process.env.DEBUG_SESSION_PLANNER === 'true' && constraints.targetSkills) {
const targetList = Object.entries(constraints.targetSkills).flatMap(([cat, skills]) =>
Object.entries(skills as Record<string, boolean>)
.filter(([, v]) => v)
.map(([s]) => `${cat}.${s}`)
)
if (targetList.length > 0) {
console.log(
`[ProblemGenerator] Targeting: ${targetList.join(', ')} → Generated: ${generatedProblem?.skillsUsed.join(', ') || 'null'}`
)
}
}
if (generatedProblem) {
return {
terms: generatedProblem.terms,
answer: generatedProblem.answer,
skillsRequired: generatedProblem.skillsUsed,
generationTrace: generatedProblem.generationTrace,
}
}
// Build actionable error message
const basicInfo =
`Failed to generate problem with constraints:\n` +
` termCount: ${constraints.termCount?.min}-${constraints.termCount?.max}\n` +
` digitRange: ${constraints.digitRange?.min}-${constraints.digitRange?.max}\n` +
` minComplexityBudget: ${constraints.minComplexityBudgetPerTerm ?? 'none'}\n` +
` maxComplexityBudget: ${constraints.maxComplexityBudgetPerTerm ?? 'none'}\n`
const diagnosticsMessage = formatDiagnosticsMessage(diagnostics)
throw new ProblemGenerationError(`${basicInfo}\n${diagnosticsMessage}`, constraints, diagnostics)
}
|