Mehrsprachige Bedienung und versionierte Aufgabenvarianten einführen

Übersetzungskataloge, getrennte Sprachpräferenzen und einen versionierten Aufgabenpilot ergänzen. Historische Aufgaben und Bewertungen bleiben erhalten; äquivalente Ansichten teilen Nachweise. Lokal mit Race-Tests und PostgreSQL, Vet, Web-Build, Bank-Audits und Browser-Fixtures geprüft. Dieser Stand dient als Ausgangspunkt für die anschließend beauftragte Zusammenführung zur Muttersprache.
This commit is contained in:
Eugenius Naumenko
2026-11-07 16:26:26 +00:00
parent 87684ba0fd
commit f3d42ee2b6
68 changed files with 3895 additions and 481 deletions
+62 -41
View File
@@ -23,39 +23,59 @@ type CoursePack struct {
Reserves []AdaptiveTask `json:"reserves"`
}
type TaskOption struct {
ID string `json:"id"`
Label string `json:"label"`
ID string `json:"id"`
Label string `json:"label"`
Language string `json:"language,omitempty"`
Role string `json:"role,omitempty"`
}
type TaskKey struct {
CorrectOptionID string `json:"correct_option_id,omitempty"`
ReferenceAnswer string `json:"reference_answer,omitempty"`
Rubric []string `json:"rubric"`
Explanation string `json:"explanation"`
CorrectOptionID string `json:"correct_option_id,omitempty"`
ReferenceAnswer string `json:"reference_answer,omitempty"`
ReferenceAnswerLanguage string `json:"reference_answer_language,omitempty"`
Rubric []string `json:"rubric"`
Explanation string `json:"explanation"`
}
// TaskStimulus is public exercise material, separate from the instruction.
// Private listening text remains AudioText and is excluded by Public(false).
type TaskStimulus struct {
Role string `json:"role"`
Language string `json:"language"`
Text string `json:"text"`
}
type AdaptiveTask struct {
SchemaVersion int `json:"schema_version"`
ID string `json:"id,omitempty"`
ModuleID string `json:"module_id"`
OriginModuleKey string `json:"origin_module_key,omitempty"`
TargetLanguage string `json:"target_language"`
CourseVersion string `json:"course_version"`
CEFRLevel string `json:"cefr_level"`
Skill string `json:"skill"`
Kind string `json:"kind"`
Prompt string `json:"prompt"`
Passage string `json:"passage,omitempty"`
Options []TaskOption `json:"options,omitempty"`
ReferenceText string `json:"reference_text,omitempty"`
AudioText string `json:"audio_text,omitempty"`
AnswerKey *TaskKey `json:"answer_key,omitempty"`
Source string `json:"source,omitempty"`
BriefID string `json:"brief_id,omitempty"`
ExamID string `json:"exam_id,omitempty"`
Review bool `json:"review,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
SchemaVersion int `json:"schema_version"`
ID string `json:"id,omitempty"`
ModuleID string `json:"module_id"`
OriginModuleKey string `json:"origin_module_key,omitempty"`
TargetLanguage string `json:"target_language"`
CourseVersion string `json:"course_version"`
CEFRLevel string `json:"cefr_level"`
Skill string `json:"skill"`
Kind string `json:"kind"`
Prompt string `json:"prompt"`
Passage string `json:"passage,omitempty"`
Options []TaskOption `json:"options,omitempty"`
ReferenceText string `json:"reference_text,omitempty"`
AudioText string `json:"audio_text,omitempty"`
AnswerKey *TaskKey `json:"answer_key,omitempty"`
Source string `json:"source,omitempty"`
BriefID string `json:"brief_id,omitempty"`
ExamID string `json:"exam_id,omitempty"`
Review bool `json:"review,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
InstructionLanguage string `json:"instruction_language,omitempty"`
Instruction string `json:"instruction,omitempty"`
Stimulus *TaskStimulus `json:"stimulus,omitempty"`
VariantID string `json:"variant_id,omitempty"`
ContentRevision string `json:"content_revision,omitempty"`
CanonicalTaskID string `json:"canonical_task_id,omitempty"`
EvidenceTaskID string `json:"evidence_task_id,omitempty"`
}
func (t AdaptiveTask) Public(reveal bool) AdaptiveTask {
t.EvidenceTaskID = ""
if !reveal {
t.AnswerKey = nil
t.AudioText = ""
@@ -136,21 +156,22 @@ type CourseBrief struct {
Reason string `json:"reason"`
}
type CourseJob struct {
AIRevision int `json:"-"`
ID string `json:"id"`
UserID string `json:"-"`
Language string `json:"target_language"`
Kind string `json:"kind"`
ModuleID string `json:"module_id"`
Skill string `json:"skill,omitempty"`
SubjectID string `json:"subject_id,omitempty"`
Status string `json:"status"`
Stage string `json:"stage"`
TaskID string `json:"task_id,omitempty"`
Source string `json:"source,omitempty"`
Review bool `json:"review,omitempty"`
Error string `json:"error,omitempty"`
Lease string `json:"-"`
InstructionLanguage string `json:"instruction_language"`
AIRevision int `json:"-"`
ID string `json:"id"`
UserID string `json:"-"`
Language string `json:"target_language"`
Kind string `json:"kind"`
ModuleID string `json:"module_id"`
Skill string `json:"skill,omitempty"`
SubjectID string `json:"subject_id,omitempty"`
Status string `json:"status"`
Stage string `json:"stage"`
TaskID string `json:"task_id,omitempty"`
Source string `json:"source,omitempty"`
Review bool `json:"review,omitempty"`
Error string `json:"error,omitempty"`
Lease string `json:"-"`
}
type AdaptiveExam struct {
PreparationJobID string `json:"preparation_job_id,omitempty"`
+15
View File
@@ -9,3 +9,18 @@ type BankEntry struct {
Reviewed bool `json:"reviewed"`
Task AdaptiveTask `json:"task"`
}
// BankVariant references a reviewed base entry but has its own immutable revision.
// Only equivalent translations share a canonical identity with that base.
type BankVariant struct {
ModuleTitle string `json:"module_title"`
ModuleDescription string `json:"module_description"`
ID string `json:"id"`
Revision string `json:"revision"`
BaseEntryID string `json:"base_entry_id"`
CanonicalTaskID string `json:"canonical_task_id"`
InstructionLanguage string `json:"instruction_language"`
Equivalence string `json:"equivalence"`
Reviewed bool `json:"reviewed"`
Task AdaptiveTask `json:"task"`
}
+22 -5
View File
@@ -3,11 +3,28 @@ package domain
import "time"
type User struct {
ID string `json:"id"`
Email string `json:"email"`
UILanguage string `json:"ui_language"`
Role string `json:"role"`
CreatedAt time.Time `json:"created_at"`
ID string `json:"id"`
Email string `json:"email"`
UILanguage string `json:"ui_language"`
InstructionLanguage string `json:"instruction_language"`
Role string `json:"role"`
CreatedAt time.Time `json:"created_at"`
}
// ExplanationLanguage preserves compatibility with callers that construct users
// without the additive field; persisted accounts always have an explicit value.
func (u User) ExplanationLanguage() string {
if u.InstructionLanguage != "" {
return u.InstructionLanguage
}
if u.UILanguage != "" {
return u.UILanguage
}
return "de-DE"
}
func SupportedUILanguage(language string) bool {
return language == "de-DE" || language == "en-US"
}
type AdminOverview struct {
+49 -2
View File
@@ -34,6 +34,10 @@ func (s *Server) adaptiveProfile(w http.ResponseWriter, r *http.Request) (domain
return p, true
}
func adaptiveError(w http.ResponseWriter, err error) {
if errors.Is(err, store.ErrCourseVariantUnavailable) {
writeJSON(w, http.StatusConflict, map[string]string{"error": err.Error(), "code": "course_variant_unavailable"})
return
}
status := http.StatusInternalServerError
if errors.Is(err, store.ErrNotFound) {
status = http.StatusNotFound
@@ -72,7 +76,7 @@ func (s *Server) adaptiveCatalog(w http.ResponseWriter, r *http.Request) {
adaptiveError(w, err)
return
}
snap, err := s.course.Repo.Snapshot(r.Context(), user.ID, p.TargetLanguage, user.UILanguage)
snap, err := s.course.Repo.Snapshot(r.Context(), user.ID, p.TargetLanguage, user.ExplanationLanguage())
if err != nil {
adaptiveError(w, err)
return
@@ -112,7 +116,12 @@ func (s *Server) adaptiveCatalog(w http.ResponseWriter, r *http.Request) {
adaptiveError(w, err)
return
}
writeJSON(w, 200, map[string]any{"task_mode": s.course.Mode, "grading_available": s.course.EvaluationAvailable(r.Context()), "bank": inventory, "analysis_job": analysisJob, "profile": p, "modules": snap.Modules, "progress": snap.Progress, "analysis": snap.PreviousAnalysis, "facts_version": snap.FactsVersion, "enabled_languages": available, "languages": languages, "exams": exams})
instructionLanguages, err := s.course.Repo.InstructionLanguages(r.Context(), p.TargetLanguage)
if err != nil {
adaptiveError(w, err)
return
}
writeJSON(w, 200, map[string]any{"instruction_language": user.ExplanationLanguage(), "instruction_languages": instructionLanguages, "exam_available": s.course.Mode != "bank" || user.ExplanationLanguage() == "de-DE", "task_mode": s.course.Mode, "grading_available": s.course.EvaluationAvailable(r.Context()), "bank": inventory, "analysis_job": analysisJob, "profile": p, "modules": snap.Modules, "progress": snap.Progress, "analysis": snap.PreviousAnalysis, "facts_version": snap.FactsVersion, "enabled_languages": available, "languages": languages, "exams": exams})
}
func (s *Server) adaptiveNext(w http.ResponseWriter, r *http.Request) {
p, ok := s.adaptiveProfile(w, r)
@@ -569,6 +578,24 @@ func (s *Server) adaptiveNewExam(w http.ResponseWriter, r *http.Request) {
writeError(w, 404, store.ErrNotFound)
return
}
if s.course.Mode == "bank" {
user, err := s.store.GetUser(r.Context(), currentUserID(r))
if err != nil {
adaptiveError(w, err)
return
}
if user.ExplanationLanguage() != "de-DE" {
exists, err := s.course.Repo.HasExamRequest(r.Context(), user.ID, p.TargetLanguage, input.RequestID)
if err != nil {
adaptiveError(w, err)
return
}
if !exists {
adaptiveError(w, store.ErrCourseVariantUnavailable)
return
}
}
}
exam, err := s.course.Repo.NewExam(r.Context(), currentUserID(r), p.TargetLanguage, input.RequestID)
if err != nil {
adaptiveError(w, err)
@@ -666,3 +693,23 @@ func legacyAdaptiveTask(t domain.AdaptiveTask) domain.CourseTask {
func writeLegacyAdaptiveTask(w http.ResponseWriter, t domain.AdaptiveTask) {
writeJSON(w, 200, domain.CoursePracticeResult{Task: legacyAdaptiveTask(t), Source: t.Source})
}
// Preference changes affect future jobs only. Task snapshots keep their original
// instructions, keys and evidence identity, including tasks in older exams.
func (s *Server) adaptiveInstructionLanguage(w http.ResponseWriter, r *http.Request) {
p, ok := s.adaptiveProfile(w, r)
if !ok {
return
}
var input struct {
InstructionLanguage string `json:"instruction_language"`
}
if !adaptiveJSON(w, r, &input) {
return
}
if err := s.course.Repo.SetInstructionLanguage(r.Context(), currentUserID(r), p.TargetLanguage, input.InstructionLanguage); err != nil {
adaptiveError(w, err)
return
}
writeJSON(w, 200, map[string]string{"instruction_language": input.InstructionLanguage})
}
@@ -402,16 +402,25 @@ func TestBankBrowserSmoke(t *testing.T) {
stopDev()
t.Fatal("Vite did not start", devOutput.String())
}
node := exec.Command("node", "tests/course-bank.e2e.cjs")
node.Dir = web
node.Env = append(os.Environ(), "AI_E2E_API="+api.URL, "AI_E2E_TOKEN="+token, "AI_E2E_URL=http://localhost:15175")
output, err := node.CombinedOutput()
stopDev()
t.Log(string(output))
t.Log("Vite:", devOutput.String())
if err != nil {
t.Fatal(err)
for _, check := range []struct{ script, language string }{
{"tests/languages.e2e.cjs", "de-DE"},
{"tests/course-variants.e2e.cjs", "en-US"},
{"tests/course-bank.e2e.cjs", "de-DE"},
{"tests/course-bank.e2e.cjs", "en-US"},
} {
node := exec.Command("node", check.script)
node.Dir = web
node.Env = append(os.Environ(), "AI_E2E_API="+api.URL, "AI_E2E_TOKEN="+token, "AI_E2E_URL=http://localhost:15175")
node.Env = append(node.Env, "UI_E2E_LANGUAGE="+check.language)
output, err := node.CombinedOutput()
t.Log(string(output))
if err != nil {
stopDev()
t.Fatal(check.script, check.language, err)
}
}
stopDev()
t.Log("Vite:", devOutput.String())
}
func TestPronunciationBrowserSmoke(t *testing.T) {
@@ -126,7 +126,7 @@ func (s *Server) nextCoursePractice(w http.ResponseWriter, r *http.Request) {
}
request := domain.GenerateCourseTaskRequest{
Task: "generate_course_task", SchemaVersion: 1, TargetLanguage: detail.Module.TargetLanguage,
CEFRLevel: detail.Module.CEFRLevel, UILanguage: user.UILanguage, ModuleID: detail.Module.ID,
CEFRLevel: detail.Module.CEFRLevel, UILanguage: user.ExplanationLanguage(), ModuleID: detail.Module.ID,
Skill: selection.Goal.Skill, Kind: "choice", LearningGoal: selection.Goal,
LearningState: selection.State, AvoidPrompts: selection.AvoidPrompts,
}
+3 -2
View File
@@ -79,6 +79,7 @@ func (s *Server) Routes() http.Handler {
mux.Handle("POST /lessons/next", s.requireAuth(http.HandlerFunc(s.nextLesson)))
mux.Handle("GET /practice/exercises/{id}/audio", s.requireAuth(http.HandlerFunc(s.exerciseAudio)))
mux.Handle("POST /practice/attempts", s.requireAuth(http.HandlerFunc(s.createAttempt)))
mux.Handle("PUT /course/instruction-language", s.requireAuth(http.HandlerFunc(s.adaptiveInstructionLanguage)))
mux.Handle("GET /course/catalog", s.requireAuth(http.HandlerFunc(s.adaptiveCatalog)))
mux.Handle("POST /course/modules/{id}/tasks/next", s.requireAuth(http.HandlerFunc(s.adaptiveNext)))
mux.Handle("GET /course/modules/{id}/tasks", s.requireAuth(http.HandlerFunc(s.adaptiveHistory)))
@@ -195,7 +196,7 @@ func (s *Server) updatePreferences(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, err)
return
}
profile, err := s.store.UpdatePreferences(r.Context(), currentUserID(r), req.UILanguage, req.TargetLanguage, valueOr(req.CEFRLevel, "A1"), req.DailyGoalMinutes)
profile, err := s.store.UpdatePreferences(r.Context(), currentUserID(r), req.UILanguage, req.TargetLanguage, req.CEFRLevel, req.DailyGoalMinutes)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
@@ -339,7 +340,7 @@ func (s *Server) nextAgentExercise(ctx context.Context, user domain.User, profil
Progress: progress,
TargetLanguage: profile.TargetLanguage,
CEFRLevel: profile.CEFRLevel,
UILanguage: user.UILanguage,
UILanguage: user.ExplanationLanguage(),
SessionID: flowiseSessionID(user.ID, profile.TargetLanguage, "practice"),
RecentWeakWords: progress.WeakWords,
PreferredType: "repeat_sentence",
@@ -155,6 +155,16 @@ func courseMember(s string, values []string) bool {
// Technical validity only. No word lists, English patterns, fixed objectives,
// fixed distractors, or linguistic judgement live in the application.
func ValidateAdaptiveTask(t domain.AdaptiveTask, language, module, skill string) error {
// Locale revisions and evidence identities are assigned by Go or the reviewed
// bank importer. Adding typed fields must not let model output claim them.
if t.VariantID != "" || t.ContentRevision != "" || t.CanonicalTaskID != "" || t.EvidenceTaskID != "" || t.InstructionLanguage != "" || t.Instruction != "" || t.Stimulus != nil || (t.AnswerKey != nil && t.AnswerKey.ReferenceAnswerLanguage != "") {
return errors.New("task contains server-owned variant metadata")
}
for _, option := range t.Options {
if option.Language != "" || option.Role != "" {
return errors.New("task contains server-owned option metadata")
}
}
if !courseText(t.ModuleID, 200) || !courseText(t.TargetLanguage, 50) || t.SchemaVersion != 2 || t.TargetLanguage != language || t.CourseVersion != domain.CourseVersion || t.CEFRLevel != "A1" || t.ModuleID != module || !courseMember(t.Skill, domain.CourseSkills) || (skill != "" && t.Skill != skill) {
return errors.New("task context mismatch")
}
+10 -3
View File
@@ -98,7 +98,11 @@ func (e *CourseEngine) snapshot(ctx context.Context, j domain.CourseJob) (domain
if err != nil {
return domain.CourseSnapshot{}, err
}
return e.Repo.Snapshot(ctx, j.UserID, j.Language, user.UILanguage)
instructionLanguage := j.InstructionLanguage
if instructionLanguage == "" {
instructionLanguage = user.ExplanationLanguage()
}
return e.Repo.Snapshot(ctx, j.UserID, j.Language, instructionLanguage)
}
func (e *CourseEngine) analyze(ctx context.Context, j domain.CourseJob) (domain.CourseSnapshot, error) {
snapshot, err := e.snapshot(ctx, j)
@@ -137,6 +141,9 @@ func (e *CourseEngine) Process(ctx context.Context, j domain.CourseJob) (*domain
}
return &t, err
case "exam":
if j.InstructionLanguage != "" && j.InstructionLanguage != "de-DE" {
return nil, store.ErrCourseVariantUnavailable
}
tasks, err := e.Repo.BankExam(ctx, j.UserID, j.Language, j.ModuleID)
if errors.Is(err, store.ErrNotFound) {
tasks, err = e.Repo.ReserveExam(ctx, j.UserID, j.Language, j.ModuleID)
@@ -538,7 +545,7 @@ func (e *CourseEngine) finalizeExam(ctx context.Context, j domain.CourseJob) err
var summary struct {
Summary string `json:"summary"`
}
err = e.Agent.Teacher(ctx, "summarize_course_exam", map[string]any{"target_language": j.Language, "ui_language": user.UILanguage, "exam": exam}, &summary)
err = e.Agent.Teacher(ctx, "summarize_course_exam", map[string]any{"target_language": j.Language, "ui_language": valueOr(j.InstructionLanguage, user.ExplanationLanguage()), "exam": exam}, &summary)
if err != nil {
return err
}
@@ -548,7 +555,7 @@ func (e *CourseEngine) finalizeExam(ctx context.Context, j domain.CourseJob) err
if err = e.Repo.ExamSummary(ctx, j.UserID, j.SubjectID, summary.Summary); err != nil {
return err
}
snapshot, err := e.Repo.Snapshot(ctx, j.UserID, j.Language, user.UILanguage)
snapshot, err := e.Repo.Snapshot(ctx, j.UserID, j.Language, user.ExplanationLanguage())
if err != nil {
return err
}
+3
View File
@@ -53,6 +53,9 @@ func (s *AuthService) Register(ctx context.Context, email, password, uiLanguage,
if uiLanguage == "" {
uiLanguage = "de-DE"
}
if !domain.SupportedUILanguage(uiLanguage) {
return AuthResult{}, errors.New("unsupported UI language")
}
if targetLanguage == "" {
targetLanguage = "en-US"
}
@@ -0,0 +1,218 @@
package services
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"testing"
"time"
"speech-learning-api/internal/domain"
"speech-learning-api/internal/store"
)
func TestCourseVariantSnapshotsAndEvidence(t *testing.T) {
db, profiles := adaptiveTestDB(t)
ctx := context.Background()
user, err := profiles.CreateUser(ctx, "variants@example.test", "hash", "de-DE")
if err != nil {
t.Fatal(err)
}
if _, err = profiles.CreateLearnerProfile(ctx, user.ID, "en-US", "A1", 20); err != nil {
t.Fatal(err)
}
repo := store.NewAdaptiveStore(db)
engine := NewCourseEngine(repo, profiles, nil, slog.New(slog.NewTextHandler(io.Discard, nil)))
engine.Mode = "bank"
base, err := store.BundledBank()
if err != nil {
t.Fatal(err)
}
// Existing releases assigned ordinary task snapshots without locale metadata.
historical := func(skill string) domain.AdaptiveTask {
t.Helper()
var entry domain.BankEntry
for _, candidate := range base {
if candidate.Task.ModuleID == "a1_introductions_v1" && candidate.Task.Skill == skill && candidate.Task.Kind != "free_speech" && candidate.Task.Kind != "free_text" {
entry = candidate
break
}
}
task := entry.Task
task.ID = store.NewID("historic")
task.Source = "curated"
raw, _ := json.Marshal(task)
_, err := db.ExecContext(ctx, `INSERT INTO course_v2_tasks(id,module_id,language,version,owner_id,source,skill,kind,data,fingerprint,bank_entry_id,last_presented_at,presentation_count) VALUES($1,$2,$3,$4,$5,'curated',$6,$7,$8,$9,$10,now(),1)`, task.ID, task.ModuleID, task.TargetLanguage, task.CourseVersion, user.ID, task.Skill, task.Kind, raw, "bank:"+entry.ID, entry.ID)
if err != nil {
t.Fatal(err)
}
return task
}
old := historical("grammar")
oldSpeech := historical("speaking")
var before []byte
if err = db.QueryRowContext(ctx, `SELECT data FROM course_v2_tasks WHERE id=$1`, old.ID).Scan(&before); err != nil {
t.Fatal(err)
}
if _, _, err = engine.Submit(ctx, user.ID, "en-US", old.ID, "old-answer", old.AnswerKey.CorrectOptionID, nil); err != nil {
t.Fatal(err)
}
if err = repo.SetInstructionLanguage(ctx, user.ID, "en-US", "en-US"); err != nil {
t.Fatal(err)
}
run := func(skill, request string, changeAfterEnqueue bool) domain.AdaptiveTask {
t.Helper()
j, err := repo.Enqueue(ctx, user.ID, "en-US", "next", "a1_introductions_v1", skill, "", request)
if err != nil {
t.Fatal(err)
}
if j.InstructionLanguage != "en-US" {
t.Fatal("job did not freeze explanation language", j)
}
if changeAfterEnqueue {
if err = repo.SetInstructionLanguage(ctx, user.ID, "en-US", "de-DE"); err != nil {
t.Fatal(err)
}
}
j, err = repo.Claim(ctx)
if err != nil {
t.Fatal(err)
}
task, err := engine.Process(ctx, j)
if err != nil {
t.Fatal(err)
}
if err = repo.Finish(ctx, j, task, ""); err != nil {
t.Fatal(err)
}
if task.InstructionLanguage != "en-US" || task.VariantID == "" || task.ContentRevision == "" || task.Instruction == "" {
t.Fatal("incomplete frozen variant", task)
}
return *task
}
first := run("grammar", "english-grammar-first", false)
if _, _, err = engine.Submit(ctx, user.ID, "en-US", first.ID, "english-first-answer", first.AnswerKey.CorrectOptionID, nil); err != nil {
t.Fatal(err)
}
translated := run("grammar", "english-grammar-second", true)
if !translated.Review || translated.EvidenceTaskID != old.ID {
t.Fatal("translation did not reuse existing evidence", translated, old)
}
if _, _, err = engine.Submit(ctx, user.ID, "en-US", translated.ID, "english-second-answer", translated.AnswerKey.CorrectOptionID, nil); err != nil {
t.Fatal(err)
}
replay, err := repo.Enqueue(ctx, user.ID, "en-US", "next", "a1_introductions_v1", "grammar", "", "english-grammar-second")
if err != nil || replay.TaskID != translated.ID || replay.InstructionLanguage != "en-US" {
t.Fatal("locale change altered idempotent replay", replay, err)
}
snapshot, err := repo.Snapshot(ctx, user.ID, "en-US", "de-DE")
if err != nil {
t.Fatal(err)
}
found := false
for _, p := range snapshot.Progress {
if p.Skill == "grammar" && p.ModuleID == old.ModuleID {
found = true
if p.Attempts != 3 || p.DistinctTasks != 2 {
t.Fatalf("translation inflated task evidence: %+v", p)
}
}
}
if !found {
t.Fatal("missing graded evidence")
}
if err = repo.SetInstructionLanguage(ctx, user.ID, "en-US", "en-US"); err != nil {
t.Fatal(err)
}
inventory, err := repo.BankInventory(ctx, user.ID, "en-US")
if err != nil {
t.Fatal(err)
}
total := 0
for _, item := range inventory {
total += item.Total
if item.ModuleID != "a1_introductions_v1" {
t.Fatal("German fallback advertised as English", item)
}
}
if total != 12 {
t.Fatal("incorrect locale inventory", inventory)
}
// Local speech fixtures exercise the real observation path; no Azure is called.
speech := domain.PronunciationResult{Provider: "azure", Method: "scripted", Locale: "en-US", Alphabet: "IPA", OverallScore: 65, WordScores: []domain.WordScore{{Word: "name", Accuracy: 65, PhonemeScores: []domain.PhonemeScore{{Phoneme: "n", Accuracy: 60}}}}}
if _, _, err = engine.Submit(ctx, user.ID, "en-US", oldSpeech.ID, "old-speech", oldSpeech.ReferenceText, &speech); err != nil {
t.Fatal(err)
}
localizedSpeech := run("speaking", "localized-speech", false)
if localizedSpeech.Kind != "free_speech" { // The existing repeat may be scheduled after the unseen free task.
if localizedSpeech.Kind != "repeat_sentence" {
t.Fatal(localizedSpeech.Kind)
}
} else {
localizedSpeech = run("speaking", "localized-repeat", false)
}
if localizedSpeech.EvidenceTaskID != oldSpeech.ID {
t.Fatal("translated repeat changed evidence identity", localizedSpeech)
}
if _, _, err = engine.Submit(ctx, user.ID, "en-US", localizedSpeech.ID, "translated-speech", localizedSpeech.ReferenceText, &speech); err != nil {
t.Fatal(err)
}
profile, err := profiles.PronunciationProfile(ctx, user.ID, "en-US", time.Now().UTC().Add(time.Second))
if err != nil {
t.Fatal(err)
}
for _, target := range profile.Targets {
if target.Tasks != 1 || target.Samples != 1 {
t.Fatalf("translation inflated pronunciation density: %+v", target)
}
}
if len(profile.Targets) == 0 {
t.Fatal("missing fixture observations")
}
if err = profiles.Init(ctx); err != nil {
t.Fatal("restart after multiple locale assignments", err)
}
var after []byte
if err = db.QueryRowContext(ctx, `SELECT data FROM course_v2_tasks WHERE id=$1`, old.ID).Scan(&after); err != nil {
t.Fatal(err)
}
if !bytes.Equal(before, after) {
t.Fatal("historical snapshot was rewritten")
}
if err = repo.SetInstructionLanguage(ctx, user.ID, "en-US", "fr-FR"); !errors.Is(err, store.ErrCourseVariantUnavailable) {
t.Fatal("unsupported locale accepted", err)
}
// Missing reviewed variants must not silently select a German reserve.
if _, err = repo.Enqueue(ctx, user.ID, "en-US", "next", "a1_family_v1", "grammar", "", "missing-variants"); err != nil {
t.Fatal(err)
}
job, err := repo.Claim(ctx)
if err != nil {
t.Fatal(err)
}
if _, err = engine.Process(ctx, job); !errors.Is(err, store.ErrCourseVariantUnavailable) {
t.Fatal("wrong-language fallback", err)
}
if err = repo.Finish(ctx, job, nil, "no reviewed variants"); err != nil {
t.Fatal(err)
}
history, err := repo.History(ctx, user.ID, "en-US", "a1_family_v1", 0)
if err != nil || len(history) != 0 {
t.Fatal("unsupported request assigned German content", history, err)
}
}
func TestModelCannotSetVariantEvidence(t *testing.T) {
entries, err := store.BundledBank()
if err != nil {
t.Fatal(err)
}
task := entries[0].Task
task.EvidenceTaskID = "another-assignment"
if ValidateAdaptiveTask(task, task.TargetLanguage, task.ModuleID, task.Skill) == nil {
t.Fatal("model-owned evidence identity accepted")
}
}
@@ -0,0 +1,107 @@
package services
import (
"context"
"testing"
"speech-learning-api/internal/domain"
"speech-learning-api/internal/store"
)
func TestLanguagePreferencesPreserveCourseAndExplanations(t *testing.T) {
db, profiles := adaptiveTestDB(t)
ctx := context.Background()
user, err := profiles.CreateUser(ctx, "languages@example.test", "hash", "de-DE")
if err != nil {
t.Fatal(err)
}
original, err := profiles.CreateLearnerProfile(ctx, user.ID, "es-ES", "A2", 25)
if err != nil {
t.Fatal(err)
}
updated, err := profiles.UpdatePreferences(ctx, user.ID, "en-US", "", "", 0)
if err != nil {
t.Fatal(err)
}
if updated != original {
t.Fatalf("interface switch changed learning profile: before=%+v after=%+v", original, updated)
}
loaded, err := profiles.GetUser(ctx, user.ID)
if err != nil {
t.Fatal(err)
}
if loaded.UILanguage != "en-US" || loaded.InstructionLanguage != "de-DE" {
t.Fatalf("language roles conflated: %+v", loaded)
}
_, _, err = profiles.GetPasswordHashByEmail(ctx, user.Email)
if err != nil {
t.Fatal(err)
}
engine := NewCourseEngine(store.NewAdaptiveStore(db), profiles, nil, nil)
snapshot, err := engine.snapshot(ctx, domain.CourseJob{UserID: user.ID, Language: "es-ES"})
if err != nil {
t.Fatal(err)
}
if snapshot.UILanguage != "de-DE" {
t.Fatalf("model instructions follow interface language: %q", snapshot.UILanguage)
}
if _, err = profiles.UpdatePreferences(ctx, user.ID, "unsupported", "en-US", "A1", 10); err == nil {
t.Fatal("unsupported interface accepted")
}
active, err := profiles.GetPrimaryProfile(ctx, user.ID)
if err != nil || active != original {
t.Fatalf("rejected update changed active profile: %+v %v", active, err)
}
// A failed profile write must not commit the accompanying interface change.
if _, err = db.ExecContext(ctx, `ALTER TABLE learner_profiles ADD CONSTRAINT test_daily_limit CHECK(daily_goal_minutes<=60)`); err != nil {
t.Fatal(err)
}
if _, err = profiles.UpdatePreferences(ctx, user.ID, "de-DE", "en-US", "A1", 100); err == nil {
t.Fatal("expected profile constraint failure")
}
loaded, err = profiles.GetUser(ctx, user.ID)
if err != nil || loaded.UILanguage != "en-US" {
t.Fatalf("failed update changed interface: %+v %v", loaded, err)
}
active, err = profiles.GetPrimaryProfile(ctx, user.ID)
if err != nil || active != original {
t.Fatalf("failed update changed learning profile: %+v %v", active, err)
}
}
func TestInstructionLanguageMigrationPreservesLegacyPreference(t *testing.T) {
db, profiles := adaptiveTestDB(t)
ctx := context.Background()
user, err := profiles.CreateUser(ctx, "legacy-language@example.test", "hash", "fr-FR")
if err != nil {
t.Fatal(err)
}
// Simulate the previous schema in this disposable test schema only.
if _, err = db.ExecContext(ctx, `ALTER TABLE users DROP COLUMN instruction_language`); err != nil {
t.Fatal(err)
}
if err = profiles.Init(ctx); err != nil {
t.Fatal(err)
}
loaded, err := profiles.GetUser(ctx, user.ID)
if err != nil || loaded.InstructionLanguage != "fr-FR" {
t.Fatalf("legacy instruction preference lost: %+v %v", loaded, err)
}
if _, err = profiles.UpdatePreferences(ctx, user.ID, "en-US", "", "", 0); err != nil {
t.Fatal(err)
}
if err = profiles.Init(ctx); err != nil {
t.Fatal(err)
}
loaded, err = profiles.GetUser(ctx, user.ID)
if err != nil || loaded.UILanguage != "en-US" || loaded.InstructionLanguage != "fr-FR" {
t.Fatalf("repeated migration changed instruction preference: %+v %v", loaded, err)
}
result, err := NewAuthService(profiles, "language-test-only").Register(ctx, "new-language@example.test", "test-password", "en-US", "en-US", 15)
if err != nil {
t.Fatal(err)
}
if result.User.UILanguage != "en-US" || result.User.InstructionLanguage != "de-DE" {
t.Fatalf("new account did not keep German course explanations: %+v", result.User)
}
}
+8 -3
View File
@@ -89,7 +89,7 @@ func (s *AdaptiveStore) Task(ctx context.Context, user, language, id string) (do
t.BriefID = brief.String
t.ExamID = exam.String
if t.Source == "curated" && t.ExamID == "" {
t.Review = presentations > 1
t.Review = t.Review || presentations > 1
}
return t, nil
}
@@ -141,6 +141,7 @@ func fingerprint(t domain.AdaptiveTask) string {
return hex.EncodeToString(h[:])
}
func (s *AdaptiveStore) SaveTask(ctx context.Context, j domain.CourseJob, t domain.AdaptiveTask) (domain.AdaptiveTask, error) {
t.InstructionLanguage = j.InstructionLanguage
t.ID = NewID("a2")
if t.Source != "ai" {
t.Source = "flowise"
@@ -266,11 +267,15 @@ func (s *AdaptiveStore) Snapshot(ctx context.Context, user, language, uiLanguage
if err != nil {
return out, err
}
if err = s.localizeModuleLabels(ctx, language, uiLanguage, out.Modules); err != nil {
return out, err
}
err = s.db.QueryRowContext(ctx, `SELECT COALESCE((SELECT facts_version FROM course_v2_versions WHERE user_id=$1 AND language=$2 AND version=$3),0)`, user, language, domain.CourseVersion).Scan(&out.FactsVersion)
if err != nil {
return out, err
}
rows, err := s.db.QueryContext(ctx, `SELECT t.module_id,t.skill,count(*),count(DISTINCT t.id),avg((a.evaluation->>'score')::float8),(array_agg(a.id ORDER BY a.created_at DESC,a.id DESC))[1]
rows, err := s.db.QueryContext(ctx, `SELECT t.module_id,t.skill,count(*),count(DISTINCT COALESCE(NULLIF(t.data->>'evidence_task_id',''),t.id)),avg((a.evaluation->>'score')::float8),(array_agg(a.id ORDER BY a.created_at DESC,a.id DESC))[1]
FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id WHERE a.user_id=$1 AND t.language=$2 AND t.version=$3 AND a.status='graded' AND NOT a.demo AND (a.exam_id IS NULL OR EXISTS(SELECT 1 FROM course_v2_exams e WHERE e.id=a.exam_id AND e.status='completed')) GROUP BY t.module_id,t.skill`, user, language, domain.CourseVersion)
if err != nil {
return out, err
@@ -422,7 +427,7 @@ func (s *AdaptiveStore) Submit(ctx context.Context, user, request string, t doma
return domain.AdaptiveAttempt{}, err
}
if actualID == id {
if err = recordPronunciation(ctx, tx, user, "course", actualID, "course:"+t.ID, t.TargetLanguage, t.ExamID, actualTime, *speech); err != nil {
if err = recordPronunciation(ctx, tx, user, "course", actualID, "course:"+valueOr(t.EvidenceTaskID, t.ID), t.TargetLanguage, t.ExamID, actualTime, *speech); err != nil {
return domain.AdaptiveAttempt{}, err
}
}
+3 -3
View File
@@ -11,7 +11,7 @@ import (
func (s *AdaptiveStore) Enqueue(ctx context.Context, user, language, kind, module, skill, subject, request string) (domain.CourseJob, error) {
id := NewID("job")
_, err := s.db.ExecContext(ctx, `INSERT INTO course_v2_jobs(id,user_id,language,version,kind,module_id,skill,subject_id,request_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) ON CONFLICT(user_id,kind,request_id) DO NOTHING`, id, user, language, domain.CourseVersion, kind, module, skill, subject, request)
_, err := s.db.ExecContext(ctx, `INSERT INTO course_v2_jobs(id,user_id,language,version,kind,module_id,skill,subject_id,request_id,instruction_language) SELECT $1,$2,$3,$4,$5,$6,$7,$8,$9,COALESCE(NULLIF(u.instruction_language,''),NULLIF(u.ui_language,''),'de-DE') FROM users u WHERE u.id=$2 ON CONFLICT(user_id,kind,request_id) DO NOTHING`, id, user, language, domain.CourseVersion, kind, module, skill, subject, request)
if err != nil {
return domain.CourseJob{}, err
}
@@ -28,7 +28,7 @@ func (s *AdaptiveStore) Enqueue(ctx context.Context, user, language, kind, modul
func scanCourseJob(row interface{ Scan(...any) error }) (domain.CourseJob, error) {
var j domain.CourseJob
var result []byte
err := row.Scan(&j.ID, &j.UserID, &j.Language, &j.Kind, &j.ModuleID, &j.Skill, &j.SubjectID, &j.Status, &j.Stage, &j.Error, &j.Lease, &result, &j.AIRevision)
err := row.Scan(&j.ID, &j.UserID, &j.Language, &j.Kind, &j.ModuleID, &j.Skill, &j.SubjectID, &j.Status, &j.Stage, &j.Error, &j.Lease, &result, &j.AIRevision, &j.InstructionLanguage)
if errors.Is(err, sql.ErrNoRows) {
return j, ErrNotFound
}
@@ -51,7 +51,7 @@ func scanCourseJob(row interface{ Scan(...any) error }) (domain.CourseJob, error
return j, nil
}
const jobColumns = `id,user_id,language,kind,module_id,skill,subject_id,status,stage,error,lease,result,ai_revision`
const jobColumns = `id,user_id,language,kind,module_id,skill,subject_id,status,stage,error,lease,result,ai_revision,instruction_language`
func (s *AdaptiveStore) Job(ctx context.Context, user, language, id string) (domain.CourseJob, error) {
return scanCourseJob(s.db.QueryRowContext(ctx, `SELECT `+jobColumns+` FROM course_v2_jobs WHERE id=$1 AND user_id=$2 AND language=$3 AND version=$4`, id, user, language, domain.CourseVersion))
@@ -57,6 +57,9 @@ CREATE TABLE IF NOT EXISTS course_v2_jobs (
deliveries INT NOT NULL DEFAULT 0,created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(user_id,kind,request_id)
);
ALTER TABLE course_v2_jobs ADD COLUMN IF NOT EXISTS instruction_language TEXT NOT NULL DEFAULT '';
-- Freeze the previous effective preference once for pre-existing queued jobs.
UPDATE course_v2_jobs j SET instruction_language=COALESCE(NULLIF(u.instruction_language,''),NULLIF(u.ui_language,''),'de-DE') FROM users u WHERE j.user_id=u.id AND j.instruction_language='';
ALTER TABLE course_v2_jobs ADD COLUMN IF NOT EXISTS ai_revision INT NOT NULL DEFAULT 0;
CREATE INDEX IF NOT EXISTS course_v2_queue ON course_v2_jobs(status,created_at);
UPDATE users u SET active_profile_id=(SELECT p.id FROM learner_profiles p WHERE p.user_id=u.id ORDER BY p.created_at,p.id LIMIT 1) WHERE active_profile_id IS NULL;
+50 -20
View File
@@ -29,7 +29,6 @@ ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS bank_entry_id TEXT REFERENC
ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS last_presented_at TIMESTAMPTZ;
ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS presentation_count INT NOT NULL DEFAULT 0;
UPDATE course_v2_tasks SET presentation_count=1 WHERE source='curated' AND bank_entry_id IS NOT NULL AND presentation_count=0;
CREATE UNIQUE INDEX IF NOT EXISTS course_v2_bank_assignment ON course_v2_tasks(owner_id,bank_entry_id) WHERE exam_id IS NULL AND bank_entry_id IS NOT NULL;
ALTER TABLE course_v2_tasks DROP CONSTRAINT IF EXISTS course_v2_tasks_source_check;
ALTER TABLE course_v2_tasks ADD CONSTRAINT course_v2_tasks_source_check CHECK(source IN ('seed','flowise','ai','curated'));
ALTER TABLE course_v2_attempts DROP CONSTRAINT IF EXISTS course_v2_attempts_status_check;
@@ -201,15 +200,21 @@ func (s *AdaptiveStore) InstallBank(ctx context.Context) error {
return err
}
}
return tx.Commit()
if err = tx.Commit(); err != nil {
return err
}
return s.installVariants(ctx, entries)
}
type bankCandidate struct {
entry domain.BankEntry
taskID string
score float64
seen bool
presented int64
entry domain.BankEntry
bankEntryID string
variantID string
previousID string
taskID string
score float64
seen bool
presented int64
}
// AssignBankTask serializes selections per learner, including presentation of
@@ -245,9 +250,14 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
return t, err
}
}
rows, err := tx.QueryContext(ctx, `SELECT b.data,COALESCE(t.id,''),COALESCE((SELECT (a.evaluation->>'score')::float8 FROM course_v2_attempts a WHERE a.task_id=t.id AND a.user_id=$1 AND a.status='graded' AND NOT a.demo ORDER BY a.created_at DESC,a.id DESC LIMIT 1),-1),COALESCE(extract(epoch from t.last_presented_at)::bigint,0)
FROM course_v2_bank b LEFT JOIN course_v2_tasks t ON t.bank_entry_id=b.id AND t.owner_id=$1 AND t.exam_id IS NULL
WHERE b.active AND b.language=$2 AND b.version=$3 AND b.module_id=$4 AND ($5='' OR b.skill=$5) AND ($6=false OR b.data->'task'->>'kind'='choice')`, j.UserID, j.Language, domain.CourseVersion, j.ModuleID, j.Skill, j.Kind == "legacy_next")
instructionLanguage := valueOr(j.InstructionLanguage, "de-DE")
rows, err := tx.QueryContext(ctx, availableBank+`
SELECT av.entry,COALESCE(t.id,''),COALESCE(graded.score,-1),COALESCE(extract(epoch from previous.last_presented_at)::bigint,0),av.base_id,av.variant_id,COALESCE(previous.id,'')
FROM available av
LEFT JOIN course_v2_tasks t ON t.bank_entry_id=av.base_id AND COALESCE(t.variant_id,'')=av.variant_id AND t.owner_id=$1 AND t.exam_id IS NULL
LEFT JOIN LATERAL (SELECT r.id,r.last_presented_at FROM course_v2_tasks r WHERE r.owner_id=$1 AND r.language=$2 AND r.version=$3 AND r.exam_id IS NULL AND COALESCE(NULLIF(r.data->>'canonical_task_id',''),r.bank_entry_id)=av.canonical_id ORDER BY r.last_presented_at DESC NULLS LAST,r.id LIMIT 1) previous ON true
LEFT JOIN LATERAL (SELECT (a.evaluation->>'score')::float8 AS score FROM course_v2_attempts a JOIN course_v2_tasks r ON r.id=a.task_id WHERE a.user_id=$1 AND r.owner_id=$1 AND r.language=$2 AND r.version=$3 AND r.exam_id IS NULL AND COALESCE(NULLIF(r.data->>'canonical_task_id',''),r.bank_entry_id)=av.canonical_id AND a.status='graded' AND NOT a.demo ORDER BY a.created_at DESC,a.id DESC LIMIT 1) graded ON true
WHERE av.module_id=$5 AND ($6='' OR av.skill=$6) AND ($7=false OR av.entry->'task'->>'kind'='choice')`, j.UserID, j.Language, domain.CourseVersion, instructionLanguage, j.ModuleID, j.Skill, j.Kind == "legacy_next")
if err != nil {
return domain.AdaptiveTask{}, err
}
@@ -255,7 +265,7 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
for rows.Next() {
var c bankCandidate
var data []byte
if err = rows.Scan(&data, &c.taskID, &c.score, &c.presented); err != nil {
if err = rows.Scan(&data, &c.taskID, &c.score, &c.presented, &c.bankEntryID, &c.variantID, &c.previousID); err != nil {
rows.Close()
return domain.AdaptiveTask{}, err
}
@@ -263,7 +273,7 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
rows.Close()
return domain.AdaptiveTask{}, err
}
c.seen = c.taskID != ""
c.seen = c.previousID != ""
candidates = append(candidates, c)
}
err = rows.Err()
@@ -272,6 +282,9 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
return domain.AdaptiveTask{}, err
}
if len(candidates) == 0 {
if instructionLanguage != "de-DE" {
return domain.AdaptiveTask{}, ErrCourseVariantUnavailable
}
return domain.AdaptiveTask{}, ErrNotFound
}
type recent struct {
@@ -281,7 +294,7 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
difficulty int
}
recentScores := []recent{}
rows, err = tx.QueryContext(ctx, `SELECT b.goal,(a.evaluation->>'score')::float8,t.id,b.difficulty FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id JOIN course_v2_bank b ON b.id=t.bank_entry_id WHERE a.user_id=$1 AND t.language=$2 AND t.version=$3 AND t.module_id=$4 AND ($5='' OR t.skill=$5) AND a.status='graded' AND NOT a.demo AND a.exam_id IS NULL ORDER BY a.created_at DESC,a.id DESC LIMIT 5`, j.UserID, j.Language, domain.CourseVersion, j.ModuleID, j.Skill)
rows, err = tx.QueryContext(ctx, `SELECT b.goal,(a.evaluation->>'score')::float8,COALESCE(NULLIF(t.data->>'evidence_task_id',''),t.id),b.difficulty FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id JOIN course_v2_bank b ON b.id=t.bank_entry_id WHERE a.user_id=$1 AND t.language=$2 AND t.version=$3 AND t.module_id=$4 AND ($5='' OR t.skill=$5) AND a.status='graded' AND NOT a.demo AND a.exam_id IS NULL ORDER BY a.created_at DESC,a.id DESC LIMIT 5`, j.UserID, j.Language, domain.CourseVersion, j.ModuleID, j.Skill)
if err != nil {
return domain.AdaptiveTask{}, err
}
@@ -334,15 +347,15 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
}
sort.Slice(candidates, func(i, k int) bool {
a, b := candidates[i], candidates[k]
if a.seen && b.seen && (a.taskID == lastID) != (b.taskID == lastID) {
return b.taskID == lastID
if a.seen && b.seen && (a.previousID == lastID) != (b.previousID == lastID) {
return b.previousID == lastID
}
if rank(a) != rank(b) {
return rank(a) < rank(b)
}
if a.seen && b.seen {
if (a.taskID == lastID) != (b.taskID == lastID) {
return b.taskID == lastID
if (a.previousID == lastID) != (b.previousID == lastID) {
return b.previousID == lastID
}
if a.presented != b.presented {
return a.presented < b.presented
@@ -367,10 +380,23 @@ func (s *AdaptiveStore) AssignBankTask(ctx context.Context, j domain.CourseJob)
t.Source = "curated"
t.Review = chosen.seen
t.ID = chosen.taskID
if !chosen.seen {
if chosen.taskID == "" {
t.ID = NewID("a2")
if t.InstructionLanguage == "" {
t.InstructionLanguage = "de-DE"
}
if t.CanonicalTaskID == "" {
t.CanonicalTaskID = chosen.bankEntryID
}
// Reuse the earliest evidence identity without editing any historic snapshot.
err = tx.QueryRowContext(ctx, `SELECT COALESCE(NULLIF(data->>'evidence_task_id',''),id) FROM course_v2_tasks WHERE owner_id=$1 AND language=$2 AND version=$3 AND exam_id IS NULL AND COALESCE(NULLIF(data->>'canonical_task_id',''),bank_entry_id)=$4 ORDER BY created_at,id LIMIT 1`, j.UserID, j.Language, domain.CourseVersion, t.CanonicalTaskID).Scan(&t.EvidenceTaskID)
if errors.Is(err, sql.ErrNoRows) {
t.EvidenceTaskID = t.ID
} else if err != nil {
return t, err
}
data, _ := json.Marshal(t)
_, err = tx.ExecContext(ctx, `INSERT INTO course_v2_tasks(id,module_id,language,version,owner_id,source,skill,kind,data,fingerprint,bank_entry_id,last_presented_at,presentation_count) VALUES($1,$2,$3,$4,$5,'curated',$6,$7,$8,$9,$10,now(),1)`, t.ID, t.ModuleID, t.TargetLanguage, t.CourseVersion, j.UserID, t.Skill, t.Kind, data, "bank:"+chosen.entry.ID, chosen.entry.ID)
_, err = tx.ExecContext(ctx, `INSERT INTO course_v2_tasks(id,module_id,language,version,owner_id,source,skill,kind,data,fingerprint,bank_entry_id,variant_id,last_presented_at,presentation_count) VALUES($1,$2,$3,$4,$5,'curated',$6,$7,$8,$9,$10,NULLIF($11,''),now(),1)`, t.ID, t.ModuleID, t.TargetLanguage, t.CourseVersion, j.UserID, t.Skill, t.Kind, data, "bank:"+chosen.entry.ID, chosen.bankEntryID, chosen.variantID)
} else {
_, err = tx.ExecContext(ctx, `UPDATE course_v2_tasks SET last_presented_at=now(),presentation_count=presentation_count+1 WHERE id=$1`, t.ID)
}
@@ -434,7 +460,11 @@ type BankInventoryItem struct {
}
func (s *AdaptiveStore) BankInventory(ctx context.Context, user, language string) ([]BankInventoryItem, error) {
rows, err := s.db.QueryContext(ctx, `SELECT b.module_id,b.skill,count(*),count(*) FILTER(WHERE t.id IS NULL) FROM course_v2_bank b LEFT JOIN course_v2_tasks t ON t.bank_entry_id=b.id AND t.owner_id=$1 AND t.exam_id IS NULL WHERE b.active AND b.language=$2 AND b.version=$3 GROUP BY b.module_id,b.skill ORDER BY b.module_id,b.skill`, user, language, domain.CourseVersion)
var instructionLanguage string
if err := s.db.QueryRowContext(ctx, `SELECT instruction_language FROM users WHERE id=$1`, user).Scan(&instructionLanguage); err != nil {
return nil, err
}
rows, err := s.db.QueryContext(ctx, availableBank+` SELECT av.module_id,av.skill,count(*),count(*) FILTER(WHERE NOT EXISTS(SELECT 1 FROM course_v2_tasks t WHERE t.owner_id=$1 AND t.language=$2 AND t.version=$3 AND t.exam_id IS NULL AND COALESCE(NULLIF(t.data->>'canonical_task_id',''),t.bank_entry_id)=av.canonical_id)) FROM available av GROUP BY av.module_id,av.skill ORDER BY av.module_id,av.skill`, user, language, domain.CourseVersion, instructionLanguage)
if err != nil {
return nil, err
}
+270
View File
@@ -0,0 +1,270 @@
package store
import (
"context"
"crypto/sha256"
"database/sql"
"embed"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strings"
"speech-learning-api/internal/domain"
)
//go:embed coursebank/variants/*.json
var variantFiles embed.FS
var ErrCourseVariantUnavailable = errors.New("no reviewed exercises are available for this explanation language")
const variantSchema = `
CREATE TABLE IF NOT EXISTS course_v2_bank_variants (
id TEXT PRIMARY KEY,base_entry_id TEXT NOT NULL REFERENCES course_v2_bank(id),
canonical_task_id TEXT NOT NULL,instruction_language TEXT NOT NULL,revision TEXT NOT NULL,
data JSONB NOT NULL,checksum TEXT NOT NULL,active BOOLEAN NOT NULL DEFAULT true);
CREATE UNIQUE INDEX IF NOT EXISTS course_v2_variant_active_presentation ON course_v2_bank_variants(base_entry_id,instruction_language) WHERE active;
CREATE INDEX IF NOT EXISTS course_v2_variant_lookup ON course_v2_bank_variants(instruction_language,base_entry_id) WHERE active;
ALTER TABLE course_v2_tasks ADD COLUMN IF NOT EXISTS variant_id TEXT REFERENCES course_v2_bank_variants(id);
CREATE INDEX IF NOT EXISTS course_v2_task_canonical ON course_v2_tasks(owner_id,language,version,(COALESCE(NULLIF(data->>'canonical_task_id',''),bank_entry_id))) WHERE exam_id IS NULL;
-- Separate frozen presentations are allowed; their evidence identity is shared
-- only when the editorial review classifies them as equivalent translations.
DROP INDEX IF EXISTS course_v2_bank_assignment;
CREATE UNIQUE INDEX IF NOT EXISTS course_v2_bank_variant_assignment ON course_v2_tasks(owner_id,bank_entry_id,COALESCE(variant_id,'')) WHERE exam_id IS NULL AND bank_entry_id IS NOT NULL;
`
func BundledVariants(base []domain.BankEntry) ([]domain.BankVariant, error) {
manifestData, err := variantFiles.ReadFile("coursebank/variants/manifest.json")
if err != nil {
return nil, err
}
var manifest struct {
Total int `json:"total"`
Files map[string]string `json:"files"`
}
if err = json.Unmarshal(manifestData, &manifest); err != nil {
return nil, err
}
variants := []domain.BankVariant{}
for name, digest := range manifest.Files {
if strings.ContainsAny(name, "/\\") || name == "manifest.json" {
return nil, errors.New("invalid variant manifest path")
}
data, err := variantFiles.ReadFile("coursebank/variants/" + name)
if err != nil {
return nil, err
}
sum := sha256.Sum256(data)
if hex.EncodeToString(sum[:]) != digest {
return nil, fmt.Errorf("variant checksum mismatch: %s", name)
}
var batch []domain.BankVariant
dec := json.NewDecoder(strings.NewReader(string(data)))
dec.DisallowUnknownFields()
if err = dec.Decode(&batch); err != nil {
return nil, err
}
variants = append(variants, batch...)
}
if len(variants) != manifest.Total || manifest.Total == 0 {
return nil, errors.New("invalid variant count")
}
return variants, ValidateVariants(base, variants)
}
func ValidateVariants(base []domain.BankEntry, variants []domain.BankVariant) error {
entries := map[string]domain.BankEntry{}
for _, entry := range base {
entries[entry.ID] = entry
}
seen := map[string]bool{}
presentations := map[string]bool{}
moduleLabels := map[string]string{}
for _, v := range variants {
original, ok := entries[v.BaseEntryID]
t := v.Task
b := original.Task
if !ok || v.ID == "" || seen[v.ID] || !v.Reviewed || v.Revision == "" || v.InstructionLanguage == "" || t.ID != "" || t.Source != "" || t.ExamID != "" || t.EvidenceTaskID != "" || t.VariantID != v.ID || t.ContentRevision != v.Revision || t.CanonicalTaskID != v.CanonicalTaskID || t.InstructionLanguage != v.InstructionLanguage || t.SchemaVersion != b.SchemaVersion || t.ModuleID != b.ModuleID || t.TargetLanguage != b.TargetLanguage || t.CourseVersion != b.CourseVersion || t.CEFRLevel != b.CEFRLevel || t.Skill != b.Skill || t.Kind != b.Kind || t.AnswerKey == nil || t.Instruction == "" || len([]rune(t.Prompt)) > 2000 || strings.Contains(t.Prompt, "<") {
return fmt.Errorf("invalid task variant: %s", v.ID)
}
if _, exists := entries[v.ID]; exists {
return fmt.Errorf("variant identity collides with a base entry: %s", v.ID)
}
if v.ModuleTitle == "" || v.ModuleDescription == "" {
return fmt.Errorf("variant module labels missing: %s", v.ID)
}
labelKey := t.ModuleID + ":" + v.InstructionLanguage
labelValue := v.ModuleTitle + "\n" + v.ModuleDescription
if previous, exists := moduleLabels[labelKey]; exists && previous != labelValue {
return fmt.Errorf("inconsistent module labels: %s", v.ID)
}
moduleLabels[labelKey] = labelValue
seen[v.ID] = true
presentation := v.BaseEntryID + ":" + v.InstructionLanguage
if presentations[presentation] {
return fmt.Errorf("duplicate active locale presentation: %s", v.ID)
}
presentations[presentation] = true
referenceLanguage := t.TargetLanguage
if t.Skill == "vocabulary" {
referenceLanguage = t.InstructionLanguage
}
if t.AnswerKey.ReferenceAnswerLanguage != referenceLanguage {
return fmt.Errorf("invalid reference language: %s", v.ID)
}
if (v.Equivalence != "translation" && v.Equivalence != "adaptation") || (v.Equivalence == "translation" && v.CanonicalTaskID != v.BaseEntryID) || (v.Equivalence == "adaptation" && v.CanonicalTaskID != v.ID) {
return fmt.Errorf("invalid variant identity: %s", v.ID)
}
prompt := t.Instruction
if t.Stimulus != nil {
stimulus := t.Stimulus
if stimulus.Text == "" || stimulus.Language == "" || (stimulus.Role == "target" && stimulus.Language != t.TargetLanguage) || !bankMember(stimulus.Role, []string{"target", "source", "context"}) || (stimulus.Role == "source" && stimulus.Language == t.TargetLanguage) || t.Skill == "listening" {
return fmt.Errorf("invalid public stimulus: %s", v.ID)
}
prompt += " " + stimulus.Text
}
if t.Prompt != prompt || t.AudioText != b.AudioText || t.Passage != b.Passage || t.ReferenceText != b.ReferenceText || len(t.Options) != len(b.Options) || t.AnswerKey.CorrectOptionID != b.AnswerKey.CorrectOptionID || t.AnswerKey.Explanation == "" || len(t.AnswerKey.Rubric) == 0 || t.AnswerKey.ReferenceAnswerLanguage == "" {
return fmt.Errorf("variant changes protected task content: %s", v.ID)
}
labels := map[string]bool{}
for i, option := range t.Options {
label := strings.ToLower(strings.TrimSpace(option.Label))
expectedRole := "target"
expectedLanguage := t.TargetLanguage
if t.Skill == "vocabulary" {
expectedRole = "meaning"
expectedLanguage = t.InstructionLanguage
}
if option.Role != expectedRole || option.Language != expectedLanguage {
return fmt.Errorf("invalid option language role: %s", v.ID)
}
if expectedRole == "meaning" && v.Equivalence == "translation" && t.InstructionLanguage == t.TargetLanguage {
return fmt.Errorf("same-language meanings need a reviewed adaptation: %s", v.ID)
}
if option.ID != b.Options[i].ID || option.Language == "" || label == "" || labels[label] || (v.Equivalence == "translation" && option.Role == "target" && option.Label != b.Options[i].Label) {
return fmt.Errorf("invalid variant options: %s", v.ID)
}
labels[label] = true
if option.ID == t.AnswerKey.CorrectOptionID && option.Label != t.AnswerKey.ReferenceAnswer {
return fmt.Errorf("variant reference/key mismatch: %s", v.ID)
}
}
if v.Equivalence == "translation" && t.Skill != "vocabulary" && t.AnswerKey.ReferenceAnswer != b.AnswerKey.ReferenceAnswer {
return fmt.Errorf("translation changes reference: %s", v.ID)
}
}
return nil
}
func (s *AdaptiveStore) installVariants(ctx context.Context, base []domain.BankEntry) error {
variants, err := BundledVariants(base)
if err != nil {
return err
}
if _, err = s.db.ExecContext(ctx, variantSchema); err != nil {
return err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if _, err = tx.ExecContext(ctx, `UPDATE course_v2_bank_variants v SET active=false FROM course_v2_bank b WHERE v.base_entry_id=b.id AND b.language='en-US' AND b.version=$1`, domain.CourseVersion); err != nil {
return err
}
for _, v := range variants {
data, err := json.Marshal(v)
if err != nil {
return err
}
sum := sha256.Sum256(data)
checksum := hex.EncodeToString(sum[:])
var old string
err = tx.QueryRowContext(ctx, `SELECT checksum FROM course_v2_bank_variants WHERE id=$1`, v.ID).Scan(&old)
if err == nil && old != checksum {
return fmt.Errorf("variant revision is immutable: %s", v.ID)
}
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
if _, err = tx.ExecContext(ctx, `INSERT INTO course_v2_bank_variants(id,base_entry_id,canonical_task_id,instruction_language,revision,data,checksum) VALUES($1,$2,$3,$4,$5,$6,$7) ON CONFLICT(id) DO UPDATE SET active=true`, v.ID, v.BaseEntryID, v.CanonicalTaskID, v.InstructionLanguage, v.Revision, data, checksum); err != nil {
return err
}
}
return tx.Commit()
}
// availableBank joins a locale presentation to its original curriculum entry.
// Existing German entries are left byte-for-byte intact in the bank.
const availableBank = `WITH available AS (
SELECT b.id AS base_id,''::text AS variant_id,b.id AS canonical_id,b.data AS entry,b.module_id,b.skill
FROM course_v2_bank b WHERE b.active AND b.language=$2 AND b.version=$3 AND $4='de-DE'
UNION ALL
SELECT b.id,v.id,v.canonical_task_id,jsonb_build_object('id',v.id,'revision',v.revision,'learning_goal_id',b.goal,'difficulty',b.difficulty,'reviewed',true,'task',v.data->'task'),b.module_id,b.skill
FROM course_v2_bank_variants v JOIN course_v2_bank b ON b.id=v.base_entry_id
WHERE v.active AND b.active AND b.language=$2 AND b.version=$3 AND v.instruction_language=$4
)`
func (s *AdaptiveStore) InstructionLanguages(ctx context.Context, language string) ([]string, error) {
rows, err := s.db.QueryContext(ctx, `SELECT 'de-DE' WHERE EXISTS(SELECT 1 FROM course_v2_bank WHERE active AND language=$1 AND version=$2) UNION SELECT DISTINCT v.instruction_language FROM course_v2_bank_variants v JOIN course_v2_bank b ON b.id=v.base_entry_id WHERE v.active AND b.active AND b.language=$1 AND b.version=$2 ORDER BY 1`, language, domain.CourseVersion)
if err != nil {
return nil, err
}
defer rows.Close()
out := []string{}
for rows.Next() {
var value string
if err = rows.Scan(&value); err != nil {
return nil, err
}
out = append(out, value)
}
return out, rows.Err()
}
func (s *AdaptiveStore) SetInstructionLanguage(ctx context.Context, user, language, instruction string) error {
options, err := s.InstructionLanguages(ctx, language)
if err != nil {
return err
}
if !bankMember(instruction, options) {
return ErrCourseVariantUnavailable
}
_, err = s.db.ExecContext(ctx, `UPDATE users SET instruction_language=$2 WHERE id=$1`, user, instruction)
return err
}
// A request replay may resume its frozen German exam after the learner has
// chosen another explanation language. Availability applies to new sessions.
func (s *AdaptiveStore) HasExamRequest(ctx context.Context, user, language, request string) (bool, error) {
var exists bool
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM course_v2_exams WHERE user_id=$1 AND language=$2 AND version=$3 AND request_id=$4)`, user, language, domain.CourseVersion, request).Scan(&exists)
return exists, err
}
// Module labels are derived display content, never a rewrite of stored course
// packs, task snapshots or learning-goal identifiers.
func (s *AdaptiveStore) localizeModuleLabels(ctx context.Context, language, instruction string, modules []domain.AdaptiveModule) error {
if instruction == "" || instruction == "de-DE" {
return nil
}
rows, err := s.db.QueryContext(ctx, `SELECT DISTINCT b.module_id,v.data->>'module_title',v.data->>'module_description' FROM course_v2_bank_variants v JOIN course_v2_bank b ON b.id=v.base_entry_id WHERE v.active AND b.active AND b.language=$1 AND b.version=$2 AND v.instruction_language=$3`, language, domain.CourseVersion, instruction)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var id, title, description string
if err = rows.Scan(&id, &title, &description); err != nil {
return err
}
for i := range modules {
if modules[i].ID == id {
modules[i].Title = title
modules[i].Description = description
}
}
}
return rows.Err()
}
@@ -0,0 +1,74 @@
package store
import (
"encoding/json"
"testing"
"speech-learning-api/internal/domain"
)
func TestReviewedVariantsRejectChangedIdentityAndLeakedMedia(t *testing.T) {
base, err := BundledBank()
if err != nil {
t.Fatal(err)
}
variants, err := BundledVariants(base)
if err != nil {
t.Fatal(err)
}
if len(variants) != 12 {
t.Fatal("unexpected pilot count", len(variants))
}
copyVariants := func() []domain.BankVariant {
raw, _ := json.Marshal(variants)
var out []domain.BankVariant
if err := json.Unmarshal(raw, &out); err != nil {
t.Fatal(err)
}
return out
}
t.Run("changed requirement is not a translation", func(t *testing.T) {
v := copyVariants()
for i := range v {
if v[i].Equivalence == "adaptation" {
v[i].Equivalence = "translation"
break
}
}
if ValidateVariants(base, v) == nil {
t.Fatal("adaptation with shared identity accepted")
}
})
t.Run("ambiguous options", func(t *testing.T) {
v := copyVariants()
v[0].Task.Options[1].Label = v[0].Task.Options[0].Label
if ValidateVariants(base, v) == nil {
t.Fatal("ambiguous options accepted")
}
})
t.Run("private listening content", func(t *testing.T) {
v := copyVariants()
for i := range v {
if v[i].Task.Skill == "listening" {
v[i].Task.Stimulus = &domain.TaskStimulus{Role: "target", Language: "en-US", Text: v[i].Task.AudioText}
break
}
}
if ValidateVariants(base, v) == nil {
t.Fatal("public listening transcript accepted")
}
})
t.Run("identity translation", func(t *testing.T) {
v := copyVariants()
v[0].Task.Stimulus.Role = "source"
if ValidateVariants(base, v) == nil {
t.Fatal("source and target identity translation accepted")
}
})
for _, v := range variants {
public := v.Task.Public(false)
if public.AnswerKey != nil || public.AudioText != "" || public.EvidenceTaskID != "" {
t.Fatal("private fields exposed", v.ID)
}
}
}
@@ -1,7 +1,7 @@
{
"revision": "en-A1-bank-v2",
"total": 840,
"editorial_review_sha256": "4cd23522372690392e0d6eae90ede2c918ed129296464035bf32bed30b8a8b7b",
"editorial_review_sha256": "a64cc0b862192e996265420c7e6d8ac82c3543449c97fbdd99dc816a270e141a",
"files": {
"introductions.grammar.json": "e166492fa3e9e13760620bda1af2c0bb6472d91216a4c46808360264450c5ebc",
"introductions.vocabulary.json": "d72c534e3c7422d85b2aa03f2988b77da31163637c70ac511ca07f278d1fdd81",
@@ -0,0 +1,630 @@
[
{
"id": "intro_en_explanations_v1_grammar_01",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_grammar_01",
"canonical_task_id": "a1bank_en_v1_introductions_grammar_01",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "grammar",
"kind": "choice",
"prompt": "Complete the sentence. I ___ Anna.",
"options": [
{
"id": "o1",
"label": "am",
"role": "target",
"language": "en-US"
},
{
"id": "o2",
"label": "is",
"role": "target",
"language": "en-US"
},
{
"id": "o3",
"label": "are",
"role": "target",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o1",
"reference_answer": "am",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "Use am after I.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_grammar_01",
"canonical_task_id": "a1bank_en_v1_introductions_grammar_01",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Complete the sentence.",
"stimulus": {
"role": "target",
"language": "en-US",
"text": "I ___ Anna."
}
}
},
{
"id": "intro_en_explanations_v1_grammar_02",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_grammar_02",
"canonical_task_id": "a1bank_en_v1_introductions_grammar_02",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "grammar",
"kind": "choice",
"prompt": "Complete the sentence. You ___ my friend.",
"options": [
{
"id": "o1",
"label": "is",
"role": "target",
"language": "en-US"
},
{
"id": "o2",
"label": "are",
"role": "target",
"language": "en-US"
},
{
"id": "o3",
"label": "am",
"role": "target",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o2",
"reference_answer": "are",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "Use are after you.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_grammar_02",
"canonical_task_id": "a1bank_en_v1_introductions_grammar_02",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Complete the sentence.",
"stimulus": {
"role": "target",
"language": "en-US",
"text": "You ___ my friend."
}
}
},
{
"id": "intro_en_explanations_v1_vocabulary_01",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_vocabulary_01",
"canonical_task_id": "intro_en_explanations_v1_vocabulary_01",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "adaptation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "vocabulary",
"kind": "choice",
"prompt": "What does hello mean in this sentence? Hello, I am Anna.",
"options": [
{
"id": "o1",
"label": "a greeting",
"role": "meaning",
"language": "en-US"
},
{
"id": "o2",
"label": "a farewell",
"role": "meaning",
"language": "en-US"
},
{
"id": "o3",
"label": "a polite request",
"role": "meaning",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o1",
"reference_answer": "a greeting",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "Hello is a greeting. We use it when we meet someone.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_vocabulary_01",
"canonical_task_id": "intro_en_explanations_v1_vocabulary_01",
"content_revision": "introductions-en-explanations-v1",
"instruction": "What does hello mean in this sentence?",
"stimulus": {
"role": "target",
"language": "en-US",
"text": "Hello, I am Anna."
}
}
},
{
"id": "intro_en_explanations_v1_vocabulary_02",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_vocabulary_02",
"canonical_task_id": "intro_en_explanations_v1_vocabulary_02",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "adaptation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "vocabulary",
"kind": "choice",
"prompt": "What does name mean in this sentence? My name is Ben.",
"options": [
{
"id": "o1",
"label": "where someone lives",
"role": "meaning",
"language": "en-US"
},
{
"id": "o2",
"label": "what a person is called",
"role": "meaning",
"language": "en-US"
},
{
"id": "o3",
"label": "how old someone is",
"role": "meaning",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o2",
"reference_answer": "what a person is called",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "A name is what a person is called. The person's name here is Ben.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_vocabulary_02",
"canonical_task_id": "intro_en_explanations_v1_vocabulary_02",
"content_revision": "introductions-en-explanations-v1",
"instruction": "What does name mean in this sentence?",
"stimulus": {
"role": "target",
"language": "en-US",
"text": "My name is Ben."
}
}
},
{
"id": "intro_en_explanations_v1_reading_01",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_reading_01",
"canonical_task_id": "a1bank_en_v1_introductions_reading_01",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "reading",
"kind": "choice",
"prompt": "What is the person's name?",
"passage": "My name is Anna. I am from Spain.",
"options": [
{
"id": "o1",
"label": "Anna",
"role": "target",
"language": "en-US"
},
{
"id": "o2",
"label": "Mia",
"role": "target",
"language": "en-US"
},
{
"id": "o3",
"label": "Eva",
"role": "target",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o1",
"reference_answer": "Anna",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "The text says: My name is Anna. The correct answer is Anna.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_reading_01",
"canonical_task_id": "a1bank_en_v1_introductions_reading_01",
"content_revision": "introductions-en-explanations-v1",
"instruction": "What is the person's name?"
}
},
{
"id": "intro_en_explanations_v1_reading_02",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_reading_02",
"canonical_task_id": "a1bank_en_v1_introductions_reading_02",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "reading",
"kind": "choice",
"prompt": "Which country is Ben from?",
"passage": "Ben is from Canada. He lives in London.",
"options": [
{
"id": "o1",
"label": "Italy",
"role": "target",
"language": "en-US"
},
{
"id": "o2",
"label": "Canada",
"role": "target",
"language": "en-US"
},
{
"id": "o3",
"label": "Germany",
"role": "target",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o2",
"reference_answer": "Canada",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "The text says: Ben is from Canada. The correct answer is Canada.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_reading_02",
"canonical_task_id": "a1bank_en_v1_introductions_reading_02",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Which country is Ben from?"
}
},
{
"id": "intro_en_explanations_v1_listening_01",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_listening_01",
"canonical_task_id": "a1bank_en_v1_introductions_listening_01",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "listening",
"kind": "choice",
"prompt": "Which country is Anna from?",
"audio_text": "My name is Anna. I am from Spain.",
"options": [
{
"id": "o1",
"label": "Spain",
"role": "target",
"language": "en-US"
},
{
"id": "o2",
"label": "France",
"role": "target",
"language": "en-US"
},
{
"id": "o3",
"label": "Italy",
"role": "target",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o1",
"reference_answer": "Spain",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "The recording says: I am from Spain. The correct answer is Spain.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_listening_01",
"canonical_task_id": "a1bank_en_v1_introductions_listening_01",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Which country is Anna from?"
}
},
{
"id": "intro_en_explanations_v1_listening_02",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_listening_02",
"canonical_task_id": "a1bank_en_v1_introductions_listening_02",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "listening",
"kind": "choice",
"prompt": "Where does Ben live?",
"audio_text": "Ben is from Canada. He lives in London.",
"options": [
{
"id": "o1",
"label": "Rome",
"role": "target",
"language": "en-US"
},
{
"id": "o2",
"label": "London",
"role": "target",
"language": "en-US"
},
{
"id": "o3",
"label": "Paris",
"role": "target",
"language": "en-US"
}
],
"answer_key": {
"correct_option_id": "o2",
"reference_answer": "London",
"rubric": [
"Choose the one answer supported by the sentence, word meaning or supplied text."
],
"explanation": "The recording says: He lives in London. The correct answer is London.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_listening_02",
"canonical_task_id": "a1bank_en_v1_introductions_listening_02",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Where does Ben live?"
}
},
{
"id": "intro_en_explanations_v1_speaking_01",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_speaking_01",
"canonical_task_id": "a1bank_en_v1_introductions_speaking_01",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "speaking",
"kind": "repeat_sentence",
"prompt": "Repeat the English sentence.",
"reference_text": "My name is Anna.",
"answer_key": {
"reference_answer": "My name is Anna.",
"rubric": [
"Repeat the displayed English sentence clearly.",
"Do not claim pronunciation or fluency errors without actual Azure measurements."
],
"explanation": "Repeat the complete sentence clearly. Pronunciation feedback uses only available speech measurements.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_speaking_01",
"canonical_task_id": "a1bank_en_v1_introductions_speaking_01",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Repeat the English sentence."
}
},
{
"id": "intro_en_explanations_v1_speaking_11",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v2_introductions_speaking_11",
"canonical_task_id": "a1bank_en_v2_introductions_speaking_11",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "speaking",
"kind": "free_speech",
"prompt": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Say two or three short English sentences in at most 30 seconds.",
"answer_key": {
"reference_answer": "My name is Nina. I am not from Germany. I am from Poland.",
"rubric": [
"Introduce one person with a name and country of origin.",
"Two or three short, understandable English sentences meet the required length.",
"Real or invented names and countries are allowed. Facts in the example are not mandatory.",
"Assess only the explicitly requested content and actual language errors. Do not penalise ambiguity in the task.",
"For this free-speech response, assess content and English formulations only. Azure pronunciation is displayed separately and is not part of this score."
],
"explanation": "The example is only one possible answer. You may choose your own name and country. Content and language are assessed separately from Azure pronunciation.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_speaking_11",
"canonical_task_id": "a1bank_en_v2_introductions_speaking_11",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Say two or three short English sentences in at most 30 seconds."
}
},
{
"id": "intro_en_explanations_v1_writing_01",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v1_introductions_writing_01",
"canonical_task_id": "intro_en_explanations_v1_writing_01",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "adaptation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "writing",
"kind": "short_text",
"prompt": "Write one short English sentence to introduce yourself. For this exercise, use the name Anna. Name: Anna",
"answer_key": {
"reference_answer": "My name is Anna.",
"rubric": [
"Write one understandable English sentence to introduce yourself using the explicitly requested name Anna.",
"Accept My name is Anna., I am Anna. and other correct equivalent formulations.",
"Consider actual A1 language errors proportionately. Do not invent additional requirements."
],
"explanation": "My name is Anna. and I am Anna. are both valid answers. Use the name Anna as requested; other correct ways to introduce yourself are allowed.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_writing_01",
"canonical_task_id": "intro_en_explanations_v1_writing_01",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Write one short English sentence to introduce yourself. For this exercise, use the name Anna.",
"stimulus": {
"role": "context",
"language": "en-US",
"text": "Name: Anna"
}
}
},
{
"id": "intro_en_explanations_v1_writing_11",
"revision": "introductions-en-explanations-v1",
"base_entry_id": "a1bank_en_v2_introductions_writing_11",
"canonical_task_id": "a1bank_en_v2_introductions_writing_11",
"instruction_language": "en-US",
"module_title": "Greetings & introductions",
"module_description": "Greet people, introduce a name and origin; I am, my name is",
"equivalence": "translation",
"reviewed": true,
"task": {
"schema_version": 2,
"module_id": "a1_introductions_v1",
"origin_module_key": "introductions",
"target_language": "en-US",
"course_version": "a1-v2",
"cefr_level": "A1",
"skill": "writing",
"kind": "free_text",
"prompt": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Write two or three short English sentences.",
"answer_key": {
"reference_answer": "My name is Nina. I am not from Germany. I am from Poland.",
"rubric": [
"Introduce one person with a name and country of origin.",
"Two or three short, understandable English sentences meet the required length.",
"Real or invented names and countries are allowed. Facts in the example are not mandatory.",
"Assess only the explicitly requested content and actual language errors. Do not penalise ambiguity in the task."
],
"explanation": "The example is only one possible answer. You may choose your own name and country. Only the explicitly requested information is required.",
"reference_answer_language": "en-US"
},
"instruction_language": "en-US",
"variant_id": "intro_en_explanations_v1_writing_11",
"canonical_task_id": "a1bank_en_v2_introductions_writing_11",
"content_revision": "introductions-en-explanations-v1",
"instruction": "Introduce yourself. Give a name and the country you are from. You may use real or invented details. Write two or three short English sentences."
}
}
]
@@ -0,0 +1,7 @@
{
"total": 12,
"editorial_review_sha256": "05cf8e999f341338101fa42d667e031ea6509a19f56b116b5af7329457a75368",
"files": {
"introductions.en-US.v1.json": "07f3a3935290760bd64935ca66825c6321654aa017cd45c5651b86ddd9fbdc39"
}
}
+1 -1
View File
@@ -131,7 +131,7 @@ func recordPronunciation(ctx context.Context, tx *sql.Tx, user, source, sourceID
// and free speech do not enter the scripted profile. Historical practice rows
// without metadata remain untouched and explicitly unverified.
func (s *PostgresStore) BackfillPronunciation(ctx context.Context) error {
rows, err := s.db.QueryContext(ctx, `SELECT a.id,a.user_id,a.task_id,t.language,COALESCE(a.exam_id,''),a.created_at,a.speech
rows, err := s.db.QueryContext(ctx, `SELECT a.id,a.user_id,COALESCE(NULLIF(t.data->>'evidence_task_id',''),a.task_id),t.language,COALESCE(a.exam_id,''),a.created_at,a.speech
FROM course_v2_attempts a JOIN course_v2_tasks t ON t.id=a.task_id
WHERE a.speech->>'provider'='azure' AND a.demo=false AND a.legacy_id IS NULL AND t.kind='repeat_sentence'
AND NOT EXISTS(SELECT 1 FROM pronunciation_assessments p WHERE p.source='course' AND p.source_id=a.id)`)
+48 -13
View File
@@ -182,9 +182,9 @@ func (s *PostgresStore) CreateUser(ctx context.Context, email, passwordHash, uiL
row := s.db.QueryRowContext(ctx, `
INSERT INTO users (id, email, password_hash, ui_language)
VALUES ($1, $2, $3, $4)
RETURNING id, email, ui_language, role, created_at
RETURNING id, email, ui_language, instruction_language, role, created_at
`, user.ID, user.Email, passwordHash, uiLanguage)
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.InstructionLanguage, &user.Role, &user.CreatedAt); err != nil {
return domain.User{}, err
}
return user, nil
@@ -194,11 +194,11 @@ func (s *PostgresStore) GetPasswordHashByEmail(ctx context.Context, email string
var user domain.User
var hash string
row := s.db.QueryRowContext(ctx, `
SELECT id, email, password_hash, ui_language, role, created_at
SELECT id, email, password_hash, ui_language, instruction_language, role, created_at
FROM users
WHERE email = $1
`, strings.ToLower(strings.TrimSpace(email)))
if err := row.Scan(&user.ID, &user.Email, &hash, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
if err := row.Scan(&user.ID, &user.Email, &hash, &user.UILanguage, &user.InstructionLanguage, &user.Role, &user.CreatedAt); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return domain.User{}, "", ErrNotFound
}
@@ -210,11 +210,11 @@ func (s *PostgresStore) GetPasswordHashByEmail(ctx context.Context, email string
func (s *PostgresStore) GetUser(ctx context.Context, id string) (domain.User, error) {
var user domain.User
row := s.db.QueryRowContext(ctx, `
SELECT id, email, ui_language, role, created_at
SELECT id, email, ui_language, instruction_language, role, created_at
FROM users
WHERE id = $1
`, id)
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.Role, &user.CreatedAt); err != nil {
if err := row.Scan(&user.ID, &user.Email, &user.UILanguage, &user.InstructionLanguage, &user.Role, &user.CreatedAt); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return domain.User{}, ErrNotFound
}
@@ -265,20 +265,48 @@ func (s *PostgresStore) GetPrimaryProfile(ctx context.Context, userID string) (d
}
func (s *PostgresStore) UpdatePreferences(ctx context.Context, userID, uiLanguage, targetLanguage, cefrLevel string, dailyGoalMinutes int) (domain.LearnerProfile, error) {
if uiLanguage != "" {
if _, err := s.db.ExecContext(ctx, `UPDATE users SET ui_language = $1 WHERE id = $2`, uiLanguage, userID); err != nil {
return domain.LearnerProfile{}, err
}
if uiLanguage != "" && !domain.SupportedUILanguage(uiLanguage) {
return domain.LearnerProfile{}, errors.New("unsupported UI language")
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return domain.LearnerProfile{}, err
}
defer tx.Rollback()
// Lock the account so partial preference updates preserve the active course
// and cannot race with another language/profile switch.
var accountID string
if err = tx.QueryRowContext(ctx, `SELECT id FROM users WHERE id=$1 FOR UPDATE`, userID).Scan(&accountID); err != nil {
return domain.LearnerProfile{}, err
}
var current domain.LearnerProfile
err = tx.QueryRowContext(ctx, `SELECT p.target_language,p.cefr_level,p.daily_goal_minutes
FROM learner_profiles p JOIN users u ON u.id=p.user_id WHERE p.user_id=$1
ORDER BY (p.id=u.active_profile_id) DESC NULLS LAST,p.created_at,p.id LIMIT 1`, userID).Scan(&current.TargetLanguage, &current.CEFRLevel, &current.DailyGoalMinutes)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return current, err
}
targetLanguage = valueOr(targetLanguage, valueOr(current.TargetLanguage, "en-US"))
cefrLevel = valueOr(cefrLevel, valueOr(current.CEFRLevel, "A1"))
if dailyGoalMinutes <= 0 {
dailyGoalMinutes = current.DailyGoalMinutes
}
if dailyGoalMinutes <= 0 {
dailyGoalMinutes = 10
}
profile, err := s.CreateLearnerProfile(ctx, userID, valueOr(targetLanguage, "en-US"), valueOr(cefrLevel, "A1"), dailyGoalMinutes)
var profile domain.LearnerProfile
err = tx.QueryRowContext(ctx, `INSERT INTO learner_profiles(id,user_id,target_language,cefr_level,daily_goal_minutes)
VALUES($1,$2,$3,$4,$5) ON CONFLICT(user_id,target_language) DO UPDATE SET
cefr_level=EXCLUDED.cefr_level,daily_goal_minutes=EXCLUDED.daily_goal_minutes
RETURNING id,user_id,target_language,cefr_level,daily_goal_minutes,created_at`, NewID("lrn"), userID, targetLanguage, cefrLevel, dailyGoalMinutes).Scan(&profile.ID, &profile.UserID, &profile.TargetLanguage, &profile.CEFRLevel, &profile.DailyGoalMinutes, &profile.CreatedAt)
if err != nil {
return profile, err
}
_, err = s.db.ExecContext(ctx, `UPDATE users SET active_profile_id=$2 WHERE id=$1`, userID, profile.ID)
return profile, err
_, err = tx.ExecContext(ctx, `UPDATE users SET active_profile_id=$2,ui_language=CASE WHEN $3='' THEN ui_language ELSE $3 END WHERE id=$1`, userID, profile.ID, uiLanguage)
if err != nil {
return profile, err
}
return profile, tx.Commit()
}
func (s *PostgresStore) ListLanguages(ctx context.Context) ([]domain.LanguageProfile, error) {
@@ -665,6 +693,13 @@ CREATE TABLE IF NOT EXISTS users (
ALTER TABLE users ADD COLUMN IF NOT EXISTS role TEXT NOT NULL DEFAULT 'learner'
CHECK (role IN ('learner', 'admin'));
-- Previously ui_language also selected the language of explanations. Preserve
-- that preference once, independently of all future interface-language changes.
ALTER TABLE users ADD COLUMN IF NOT EXISTS instruction_language TEXT;
UPDATE users SET instruction_language=ui_language WHERE instruction_language IS NULL;
ALTER TABLE users ALTER COLUMN instruction_language SET DEFAULT 'de-DE';
ALTER TABLE users ALTER COLUMN instruction_language SET NOT NULL;
CREATE TABLE IF NOT EXISTS learner_profiles (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,