feat: persist seed and prngAlgorithm for exact problem reproducibility

Ensures that shared and auto-saved worksheets generate the exact same
problems when loaded:

Seed & Algorithm Persistence:
- Added seed and prngAlgorithm to extractConfigFields()
- Both fields now included in auto-save, sharing, and settings persistence
- prngAlgorithm defaults to 'mulberry32' (current implementation)

Type Updates:
- Added prngAlgorithm: string to WorksheetConfig (required)
- Added prngAlgorithm?: string to WorksheetFormState (optional)
- Updated extractConfigFields return type to include both fields

Problem Reproducibility:
- Same seed + same algorithm = exact same problems
- Critical for sharing worksheets with specific problem sets
- Allows reload to show same problems (via auto-save)
- Future-proofs for potential algorithm changes

Documentation Updates:
- Updated useWorksheetAutoSave comments to reflect seed persistence
- Clarified that seed/algo are critical for reproducibility, not transient

Bug Fix:
- Fixed broken link on /create page (/create/worksheets/addition → /create/worksheets)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Thomas Hallock
2025-11-11 18:37:00 -06:00
parent a47c3e8f89
commit 8cb2209d84
5 changed files with 16 additions and 5 deletions

View File

@@ -283,7 +283,7 @@ export default function CreateHubPage() {
</Link>
{/* Worksheet Creator */}
<Link href="/create/worksheets/addition">
<Link href="/create/worksheets">
<div
data-element="worksheets-card"
className={css({

View File

@@ -14,7 +14,8 @@ interface UseWorksheetAutoSaveReturn {
*
* Features:
* - Debounced auto-save (1000ms delay)
* - Only persists settings, not transient state (date, seed, rows, total)
* - Persists settings including seed and prngAlgorithm for problem reproducibility
* - Excludes transient state (date, rows, total)
* - Persists V4 fields: mode, digitRange, displayRules, difficultyProfile, manualPreset
* - Silent error handling (auto-save is not critical)
* - StrictMode-safe (handles double renders)
@@ -45,7 +46,7 @@ export function useWorksheetAutoSave(
console.log('[useWorksheetAutoSave] Attempting to save settings...')
setIsSaving(true)
try {
// Extract persisted config fields (excludes date, seed, derived state)
// Extract persisted config fields (includes seed/prngAlgorithm, excludes date and derived state)
const config = extractConfigFields(formState)
const response = await fetch('/api/worksheets/settings', {

View File

@@ -24,7 +24,10 @@ export type WorksheetConfig = AdditionConfigV4 & {
// Personalization
date: string
// Problem reproducibility (critical for sharing)
seed: number
prngAlgorithm: string
// Layout
page: {
@@ -61,7 +64,9 @@ export type WorksheetFormState = Partial<Omit<AdditionConfigV4Smart, 'version'>>
rows?: number
total?: number
date?: string
// Problem reproducibility (critical for sharing)
seed?: number
prngAlgorithm?: string
}
/**

View File

@@ -3,7 +3,8 @@ import type { WorksheetFormState } from '../types'
/**
* Extract only the persisted config fields from formState
* Excludes derived state (rows, total, date, seed)
* Excludes derived state (rows, total, date)
* INCLUDES seed and prngAlgorithm to ensure exact problem reproduction when shared
*
* This ensures consistent field extraction across:
* - Auto-save (useWorksheetAutoSave)
@@ -15,7 +16,7 @@ import type { WorksheetFormState } from '../types'
*/
export function extractConfigFields(
formState: WorksheetFormState
): Omit<AdditionConfigV4, 'version'> {
): Omit<AdditionConfigV4, 'version'> & { seed?: number; prngAlgorithm?: string } {
return {
problemsPerPage: formState.problemsPerPage!,
cols: formState.cols!,
@@ -45,5 +46,8 @@ export function extractConfigFields(
currentStepId: formState.currentStepId,
currentAdditionSkillId: formState.currentAdditionSkillId,
currentSubtractionSkillId: formState.currentSubtractionSkillId,
// CRITICAL: Include seed and algorithm to ensure exact same problems when sharing
seed: formState.seed,
prngAlgorithm: formState.prngAlgorithm ?? 'mulberry32',
}
}

View File

@@ -148,6 +148,7 @@ export function validateWorksheetConfig(formState: WorksheetFormState): Validati
fontSize,
seed,
prngAlgorithm: formState.prngAlgorithm ?? 'mulberry32',
}
// Build mode-specific config