All files / server/services/tutor curriculum-tutor-adapter.ts

96.81% Statements 243/251
91.63% Branches 252/275
100% Functions 67/67
99% Lines 198/200

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663                                                                                                                                197x           197x 197x 197x       70x 70x 70x 70x               144x 144x 104x 103x       156x     156x     156x 156x 137x 137x       173x 173x 156x     156x 156x 156x 156x 156x 154x 154x                   154x 154x 154x 154x 152x       152x 125x 11x 11x 11x   114x       190x 35x 35x   155x 155x 26x       38x         114x 114x           47x   67x         114x 114x   9x     114x 7x       629x 625x 625x     1x 1x           625x 625x               625x 625x 625x 567x 567x   567x 625x 625x 625x 625x 625x 625x 625x   625x 625x 625x 551x 551x 551x 625x 625x                                         1187x 1183x 1183x 1187x   555x 1187x   6x 1187x     1187x   36x 1187x       70x 70x 18x 18x 18x 70x 70x                                             566x 566x 551x 551x 551x 551x                   566x       1x                               578x                   566x     4x                 551x 2x 2x                     551x 160x 551x 329x   551x           156x 156x 18x 18x             18x   138x 138x 120x     156x               156x 84x               84x 30x 30x 30x 30x       293x 293x                                                           584x 30x       566x 566x       566x 566x 566x       551x 273x       222x 77x 171x 222x                                         4101x 4101x 923x 923x 923x 784x 630x 1414x             923x 540x 540x 148x 148x   923x         201x                     662x 1497x 662x 662x 284x   378x     21x     67x       118x               293x 177x 293x 177x             137x 137x 14x   137x 14x   123x 132x 137x 104x       49x 49x                                                   24x 24x 24x                           4x                         1x 1x 1x                             120x                
import type { TutorDialogTurn, TutorDifficulty } from "@shared/tutor";
import { Logger } from "@shared/logger";
import type { ExampleTutorAnnotation } from "../course-content/embedded-tutor-annotation";
import {
  resolveEffectiveTutorStrategy,
  type EffectiveTutorStrategy,
  type StrategyResolution,
} from "./strategy/effective-tutor-strategy";
import {
  DefaultLearningPlanner,
  buildPlan,
  classifyTopic,
  collectObservations,
  findCurriculumQuestion,
  type LearningPlanner,
  type Observation,
  type TopicClassification,
} from "./curriculum/learning-planner";
import { buildInlineFocusTopic, INLINE_FOCUS_TOPIC_PREFIX } from "./curriculum/inline-focus-topic";
import { DefaultSketchFactExtractor, type SketchFactExtractor } from "./curriculum/sketch-facts";
import { DefaultTopicMatcher, type TopicMatch, type TopicMatcher } from "./curriculum/topic-matcher";
import type {
  TutorPlan,
  TutorPlanningBlocked,
  TutorPlanningContentContext,
  TutorPlanningExtension,
  TutorPlanningResult,
  TutorExpansionBrief,
} from "./tutor-planning";
import {
  appendEvidence,
  cloneTutorProgressionState,
  createTutorProgressionState,
  deepeningCriteria,
  hasMetDeepeningCriteria,
  markExpansionTargetUsed,
  markTopicMastered,
  resetTutorProgressionState,
  type DidacticPhase,
  type TutorProgressionState,
} from "./curriculum/progression-state";
import type { CurriculumQuestion, CurriculumTopic } from "./curriculum/curriculum-schema";
 
/**
 * Course Content reaches the adapter only as the context passed into each
 * planning call (the Tutor route supplies the session's pinned context).
 */
export interface CurriculumTutorAdapterDependencies {
  readonly factExtractor?: SketchFactExtractor;
  readonly topicMatcher?: TopicMatcher;
  readonly planner?: LearningPlanner;
}
 
type TutorFollowupInput = {
  readonly code: string;
  readonly history: readonly TutorDialogTurn[];
  readonly currentQuestion: string;
  readonly rating: Parameters<NonNullable<LearningPlanner["advance"]>>[5];
  readonly difficulty: TutorDifficulty;
  readonly exampleId?: string;
  readonly courseContent?: TutorPlanningContentContext;
};
 
export class CurriculumTutorAdapter implements TutorPlanningExtension {
  private readonly logger = new Logger("CurriculumTutorAdapter");
  private readonly factExtractor: SketchFactExtractor;
  private readonly topicMatcher: TopicMatcher;
  private readonly planner: LearningPlanner;
 
  constructor(deps: CurriculumTutorAdapterDependencies = {}) {
    this.factExtractor = deps.factExtractor ?? new DefaultSketchFactExtractor();
    this.topicMatcher = deps.topicMatcher ?? new DefaultTopicMatcher();
    this.planner = deps.planner ?? new DefaultLearningPlanner();
  }
 
  async resolveStrategy(input: { code?: string; courseContent?: TutorPlanningContentContext }): Promise<StrategyResolution> {
    try {
      const snapshot = input.courseContent ?? null;
      const phase = this.resolveActivePhase(snapshot, input.code);
      return this.resolveSnapshotStrategy(snapshot, undefined, phase);
    } catch (error) {
      this.logger.warn(`Strategy resolution failed; using the built-in strategy: ${describeError(error)}`);
      return resolveEffectiveTutorStrategy({});
    }
  }
 
  async planInitial(input: { code: string; history: readonly TutorDialogTurn[]; difficulty: TutorDifficulty; exampleId?: string; courseContent?: TutorPlanningContentContext }): Promise<TutorPlanningResult | null> {
    const context = await this.match(input.code, input.history, input.difficulty, input.courseContent, input.exampleId);
    if (!context) return null;
    if (context.blocked) return context.blocked;
    return this.startPlan(context, sessionHistory(context.topic, input.history, context.state), input.difficulty, context.phase);
  }
 
  async planAnswered(input: Omit<TutorFollowupInput, "rating">): Promise<TutorPlanningResult | null> {
    const snapshot = input.courseContent;
    // Read-only: match against a copy so that resolving the context cannot change the session
    // state; a question that cannot be resolved yields no context instead of a new plan.
    const readOnly = snapshot?.progressionState
      ? { ...snapshot, progressionState: cloneTutorProgressionState(snapshot.progressionState) }
      : snapshot;
    const context = await this.match(input.code, input.history, input.difficulty, readOnly, input.exampleId);
    if (!context) return null;
    Iif (context.blocked) return context.blocked;
    return answeredQuestionPlan(context, input.currentQuestion, input.history);
  }
 
  async planFollowup(input: TutorFollowupInput): Promise<TutorPlanningResult | null> {
    const context = await this.match(input.code, input.history, input.difficulty, input.courseContent, input.exampleId);
    if (!context) return null;
    Iif (context.blocked) return context.blocked;
    // The session view of the answered turn's past, taken before the answer is recorded: the
    // planner adds the current answer itself and must not count it twice.
    const answeredHistory = sessionHistory(context.topic, input.history, context.state);
    recordFollowupObservation(context, input);
    const stateUpdate = updateStateAfterFollowup(context, input);
    const refreshed = await this.match(input.code, input.history, input.difficulty, input.courseContent, input.exampleId);
    if (!refreshed) return null;
    Iif (refreshed.blocked) return refreshed.blocked;
    return this.continueFollowup(context, refreshed, input, stateUpdate, answeredHistory);
  }
 
  private continueFollowup(
    context: AdapterContext,
    refreshed: AdapterContext,
    input: TutorFollowupInput,
    stateUpdate: FollowupStateUpdate,
    answeredHistory: readonly TutorDialogTurn[],
  ): TutorPlanningResult | null {
    const activeChanged = refreshed.topic.id !== context.topic.id;
    Iif (!activeChanged && stateUpdate.status === "unresolved") return blockedResult(refreshed, context.state);
    const nextPhase = refreshed.phase;
    if (activeChanged) return this.startPlan(refreshed, sessionHistory(refreshed.topic, input.history, context.state), input.difficulty, nextPhase);
    const planningHistory = [...answeredHistory, answeredTurn(input.currentQuestion)];
    // The answer is evaluated by the strategy that produced the current turn.
    // A phase transition is committed to session state now, but its first plan
    // and strategy are exposed at the next request boundary.
    if (nextPhase !== context.phase) return this.currentTurnPlan(context, planningHistory, input.difficulty);
    if (nextPhase === "DEEPEN" && hasMetDeepeningCriteria(refreshed.topic, context.state.postMasteryEvidence[refreshed.topic.id] ?? [])) {
      context.state.phase = "EXPAND";
      context.state.retainedPhases[refreshed.topic.id] = "EXPAND";
      return this.currentTurnPlan(context, planningHistory, input.difficulty);
    }
    return this.advancePlan(refreshed, input, answeredHistory);
  }
 
  private startPlan(context: AdapterContext, history: readonly TutorDialogTurn[], difficulty: TutorDifficulty, phase: DidacticPhase): TutorPlanningResult | null {
    if (phase === "EXPAND" && context.expansionBrief) {
      markExpansionTargetUsed(context.state, context.expansionBrief.sourceTopicId, context.expansionBrief.targetTopicId);
      return buildExpansionPlan(context, phase, context.expansionBrief);
    }
    const plan = this.planner.start(context.topic, context.revision, context.facts, history, difficulty, context.strategy.strategy, progressionOptions(context.topic, phase, context.state));
    if (plan) return normalizePlan(plan, context.strategy, context.state, phase, context.extensionTargetTopicId, context.expansionBrief);
    return phase === "LEARN" ? null : exhaustionResult(context, phase);
  }
 
  private currentTurnPlan(context: AdapterContext, history: readonly TutorDialogTurn[], difficulty: TutorDifficulty): TutorPlanningResult {
    return this.startPlan(context, history, difficulty, context.phase)
      ?? transitionResult(context);
  }
 
  private advancePlan(context: AdapterContext, input: TutorFollowupInput, answeredHistory: readonly TutorDialogTurn[]): TutorPlanningResult | null {
    const answeredPhase = context.state.phase ?? context.phase;
    if (answeredPhase === "EXPAND") {
      // Every EXPAND step is planned as at phase start: an unused extension first, then an unused
      // post-mastery Topic question, and content-exhausted only when neither exists (R-EXP-2). This
      // holds whatever was answered: the planner cannot advance from a generated extension or from
      // a question it cannot resolve, and advancing from a Topic question would never offer an
      // extension.
      return this.startPlan(context, [...answeredHistory, answeredTurn(input.currentQuestion)], input.difficulty, answeredPhase);
    }
    const plan = this.planner.advance(context.topic, context.revision, context.facts, answeredHistory, input.currentQuestion, input.rating, {
      difficulty: input.difficulty,
      strategy: context.strategy.strategy,
      ...progressionOptions(context.topic, context.state.phase ?? context.phase, context.state),
    });
    const phase = context.state.phase ?? context.phase;
    if (plan) return normalizePlan(plan, context.strategy, context.state, phase, context.extensionTargetTopicId, context.expansionBrief);
    // A weak answer to the last question of a teacher's focus area must not drop the remaining areas.
    const nextArea = phase === "LEARN" && context.topic.id.startsWith(INLINE_FOCUS_TOPIC_PREFIX)
      ? this.planner.start(context.topic, context.revision, context.facts, [...answeredHistory, answeredTurn(input.currentQuestion)], input.difficulty, context.strategy.strategy, { phase })
      : null;
    if (nextArea) return normalizePlan(nextArea, context.strategy, context.state, phase, context.extensionTargetTopicId, context.expansionBrief);
    return phase === "LEARN" ? null : exhaustionResult(context, phase);
  }
 
  private async match(code: string, history: readonly TutorDialogTurn[], difficulty: TutorDifficulty, supplied?: TutorPlanningContentContext, exampleId?: string) {
    if (!supplied) return null;
    try {
      return this.matchCourseContent(code, history, difficulty, supplied, exampleId);
    } catch (error) {
      // The free Tutor stays available, but a failing planner must be visible.
      this.logger.warn(`Course Content planning failed; continuing without a plan: ${describeError(error)}`);
      return null;
    }
  }
 
  /** Resolves the Topic and phase for this request, then records the selection in the session state. */
  private matchCourseContent(code: string, history: readonly TutorDialogTurn[], difficulty: TutorDifficulty, snapshot: TutorPlanningContentContext, requestedExampleId?: string): AdapterContext | null {
    const resolution = this.resolveMatch(code, history, difficulty, snapshot, requestedExampleId);
    return resolution ? applyMatch(resolution) : null;
  }
 
  /**
   * Pure part of the match: reads the session state (as if reset when it belongs to another
   * revision) and changes nothing.
   */
  private resolveMatch(code: string, history: readonly TutorDialogTurn[], difficulty: TutorDifficulty, snapshot: TutorPlanningContentContext, requestedExampleId?: string): MatchResolution | null {
    const exampleContextId = snapshot.exampleId ?? requestedExampleId;
    const annotation = exampleContextId === undefined ? undefined : snapshot.exampleTutorAnnotation;
    if (snapshot.tutor?.status !== "valid" || (snapshot.tutor.topics.length === 0 && annotation?.focus === undefined)) return null;
    const facts = this.factExtractor.extract(code);
    const inline = inlineFocusMatch(exampleContextId, annotation);
    // The teacher's focus runs first; by default the Tutor then continues freely, without repository Topics.
    const focusThenFree = inline !== undefined && (annotation?.afterFocus ?? "free") === "free";
    const matches = focusThenFree ? [] : this.topicMatcher.match(snapshot.tutor.topics, facts);
    const orderedMatches = [...(inline ? [inline] : []), ...orderTopicMatches(matches, annotation)];
    const state = snapshot.progressionState ?? createTutorProgressionState(snapshot.revision);
    const resetsRevision = state.revision !== snapshot.revision;
    const view = resetsRevision ? createTutorProgressionState(snapshot.revision) : state;
    const learnStrategy = this.resolveSnapshotStrategy(snapshot, annotation, "LEARN");
    const classifications = classifyMatches(orderedMatches, facts, history, view, difficulty, learnStrategy.strategy);
    // A finished focus (mastered, or out of questions) hands over to the free Tutor.
    const open = focusThenFree ? classifications.filter(({ classification }) => classification.status === "probeable") : classifications;
    const selected = selectProgressionMatch(open, view);
    if (!selected) return { state, revision: snapshot.revision, resetsRevision, selection: null };
    const topic = selected.match.topic;
    const phase = phaseForTopic(view, topic.id, selected.classification.status);
    const extensionTargetTopicId = resolveExtensionTarget(snapshot.tutor.topics, view.activeTopicId, view.phase ?? "LEARN", topic.id);
    const expansionBrief = resolveExpansionBrief(snapshot.tutor.topics, topic, phase, view);
    return {
      state,
      revision: snapshot.revision,
      resetsRevision,
      selection: {
        facts,
        topic,
        phase,
        unresolved: selected.classification.status === "unresolved",
        strategy: this.resolveSnapshotStrategy(snapshot, annotation, phase),
        ...(extensionTargetTopicId ? { extensionTargetTopicId } : {}),
        ...(expansionBrief ? { expansionBrief } : {}),
      },
    };
  }
 
  private resolveSnapshotStrategy(
    snapshot: TutorPlanningContentContext | null,
    knownAnnotation?: ExampleTutorAnnotation,
    phase: DidacticPhase = "LEARN",
  ): StrategyResolution {
    if (snapshot?.tutor?.status !== "valid") return resolveEffectiveTutorStrategy({});
    const tutor = snapshot.tutor;
    const annotation = knownAnnotation ?? snapshot.exampleTutorAnnotation;
    const repositoryDefault = tutor.manifest.defaultStrategy === undefined
      ? undefined
      : tutor.strategies.find(({ id }) => id === tutor.manifest.defaultStrategy);
    const perExample = annotation?.strategy === undefined
      ? undefined
      : tutor.strategies.find(({ id }) => id === annotation.strategy);
    const phaseStrategyId = phase === "LEARN" || tutor.manifest.schemaVersion !== 2
      ? undefined
      : tutor.manifest.phaseStrategies?.[phase.toLowerCase() as "deepen" | "expand"];
    const phaseStrategy = phaseStrategyId === undefined
      ? undefined
      : tutor.strategies.find(({ id }) => id === phaseStrategyId);
    return resolveEffectiveTutorStrategy({ perExample, repositoryDefault: phaseStrategy ?? repositoryDefault });
  }
 
  private resolveActivePhase(snapshot: TutorPlanningContentContext | null, code?: string): DidacticPhase {
    const state = snapshot?.progressionState;
    if (!snapshot || !code || !state?.activeTopicId || !state.phase || state.phase === "LEARN") return "LEARN";
    Iif (state.revision !== snapshot.revision || snapshot.tutor?.status !== "valid") return "LEARN";
    const facts = this.factExtractor.extract(code);
    const annotation = snapshot.exampleId === undefined ? undefined : snapshot.exampleTutorAnnotation;
    const inline = inlineFocusMatch(snapshot.exampleId, annotation);
    return [...(inline ? [inline] : []), ...this.topicMatcher.match(snapshot.tutor.topics, facts)].some(({ topic }) => topic.id === state.activeTopicId)
      ? state.phase
      : "LEARN";
  }
}
 
type MatchResolution = {
  readonly state: TutorProgressionState;
  readonly revision: string;
  readonly resetsRevision: boolean;
  readonly selection: {
    readonly facts: ReturnType<SketchFactExtractor["extract"]>;
    readonly topic: CurriculumTopic;
    readonly phase: DidacticPhase;
    readonly unresolved: boolean;
    readonly strategy: StrategyResolution;
    readonly extensionTargetTopicId?: string;
    readonly expansionBrief?: TutorExpansionBrief;
  } | null;
};
 
/** The state-changing part of the match: the only place a match writes the session state. */
function applyMatch({ state, revision, resetsRevision, selection }: MatchResolution): AdapterContext | null {
  if (resetsRevision) resetTutorProgressionState(state, revision);
  if (!selection) return null;
  state.activeTopicId = selection.topic.id;
  state.phase = selection.phase;
  state.progressionBlockedReason = undefined;
  const context: AdapterContext = {
    revision,
    facts: selection.facts,
    topic: selection.topic,
    strategy: selection.strategy,
    phase: selection.phase,
    state,
    ...(selection.extensionTargetTopicId ? { extensionTargetTopicId: selection.extensionTargetTopicId } : {}),
    ...(selection.expansionBrief ? { expansionBrief: selection.expansionBrief } : {}),
  };
  return selection.unresolved ? { ...context, blocked: blockedResult(context, state) } : context;
}
 
function describeError(error: unknown): string {
  return error instanceof Error ? error.message : String(error);
}
 
type ClassifiedTopicMatch = {
  readonly match: TopicMatch;
  readonly classification: TopicClassification;
};
 
function classifyMatches(
  matches: readonly TopicMatch[],
  facts: ReturnType<SketchFactExtractor["extract"]>,
  history: readonly TutorDialogTurn[],
  state: TutorProgressionState,
  difficulty: TutorDifficulty,
  strategy: EffectiveTutorStrategy,
): readonly ClassifiedTopicMatch[] {
  return matches.map((match) => ({
    match,
    classification: classifyWithState(match.topic, facts, history, state, difficulty, strategy),
  }));
}
 
function selectProgressionMatch(
  classifications: readonly ClassifiedTopicMatch[],
  state: TutorProgressionState,
): ClassifiedTopicMatch | undefined {
  return classifications.find(({ classification }) => classification.status === "probeable")
    ?? classifications.find(({ classification }) => classification.status === "unresolved")
    ?? (state.activeTopicId ? classifications.find(({ match }) => match.topic.id === state.activeTopicId) : undefined)
    ?? classifications.find(({ classification }) => classification.status === "mastered");
}
 
function resolveExtensionTarget(
  topics: readonly CurriculumTopic[],
  previousTopicId: string | undefined,
  previousPhase: DidacticPhase,
  nextTopicId: string,
): string | undefined {
  if (!previousTopicId || previousPhase !== "EXPAND" || previousTopicId === nextTopicId) return undefined;
  const previousTopic = topics.find(({ id }) => id === previousTopicId);
  return previousTopic?.schemaVersion === 2 && previousTopic.extensions?.some(({ topic }) => topic === nextTopicId)
    ? nextTopicId
    : undefined;
}
 
function resolveExpansionBrief(
  topics: readonly CurriculumTopic[],
  topic: CurriculumTopic,
  phase: DidacticPhase,
  state: TutorProgressionState,
): TutorExpansionBrief | undefined {
  if (phase !== "EXPAND" || topic.schemaVersion !== 2 || !topic.extensions) return undefined;
  const usedTargets = new Set(state.usedExpansionTargetTopicIds[topic.id] ?? []);
  const extension = topic.extensions.find(({ topic: targetTopicId }) =>
    topics.some(({ id }) => id === targetTopicId) && !usedTargets.has(targetTopicId),
  );
  return extension
    ? { sourceTopicId: topic.id, targetTopicId: extension.topic, objective: extension.objective }
    : undefined;
}
 
function recordFollowupObservation(context: AdapterContext, input: TutorFollowupInput): void {
  const expansionTargetId = answeredExpansionTarget(context.topic, context.state, input.currentQuestion);
  if (expansionTargetId) {
    const id = expansionQuestionId(expansionTargetId);
    appendEvidence(context.state, "postMasteryEvidence", context.topic.id, {
      questionId: id,
      conceptId: id,
      indicatorId: EXPANSION_INDICATOR_ID,
      kind: "transfer",
      rating: input.rating,
    });
    return;
  }
  const current = findCurriculumQuestion(context.topic, input.currentQuestion, input.history);
  if (!current) return;
  const evidenceKey = context.phase === "DEEPEN" || context.phase === "EXPAND"
    ? "postMasteryEvidence"
    : "masteryEvidence";
  appendEvidence(context.state, evidenceKey, context.topic.id, toObservation(current, input.rating));
}
 
type FollowupStateUpdate = {
  readonly status: TopicClassification["status"];
};
 
function updateStateAfterFollowup(context: AdapterContext, input: TutorFollowupInput): FollowupStateUpdate {
  if (context.phase !== "LEARN") return { status: "mastered" };
  const classification = classifyWithState(
    context.topic,
    context.facts,
    input.history,
    context.state,
    input.difficulty,
    context.strategy.strategy,
  );
  if (classification.status !== "mastered") return { status: classification.status };
  markTopicMastered(context.state, context.topic.id);
  context.state.phase = "DEEPEN";
  context.state.retainedPhases[context.topic.id] = "DEEPEN";
  return { status: classification.status };
}
 
function normalizePlan(plan: Awaited<ReturnType<LearningPlanner["start"]>>, strategy = resolveEffectiveTutorStrategy({}), state?: TutorProgressionState, phase: DidacticPhase = "LEARN", extensionTargetTopicId?: string, expansionBrief?: TutorExpansionBrief): TutorPlan {
  Iif (!plan) throw new Error("Cannot normalize an empty tutor plan");
  return {
    ...plan.brief,
    ...(plan.brief.scaffold ? { scaffold: { ...plan.brief.scaffold } } : {}),
    contentRevision: plan.contentRevision,
    strategyId: strategy.strategy.id,
    strategySource: strategy.source === "built-in" ? "built-in" : "repository",
    effectiveStrategy: strategy.strategy,
    learningPhase: phase,
    ...(state?.activeTopicId ? { activeTopicId: state.activeTopicId } : {}),
    ...(state ? { masteredTopicIds: [...state.masteredTopicIds] } : {}),
    ...(state?.progressionBlockedReason ? { progressionBlockedReason: state.progressionBlockedReason } : {}),
    ...(extensionTargetTopicId ? { extensionTargetTopicId } : {}),
    ...(expansionBrief ? { expansionBrief } : {}),
  };
}
 
type AdapterContext = {
  readonly revision: string;
  readonly facts: ReturnType<SketchFactExtractor["extract"]>;
  readonly topic: CurriculumTopic;
  readonly strategy: StrategyResolution;
  readonly phase: DidacticPhase;
  readonly state: TutorProgressionState;
  readonly extensionTargetTopicId?: string;
  readonly expansionBrief?: TutorExpansionBrief;
  readonly blocked?: TutorPlanningBlocked;
};
 
/** The teacher-authored focus of an Example always comes first and needs no sketch-fact activation. */
function inlineFocusMatch(exampleId: string | undefined, annotation: ExampleTutorAnnotation | undefined): TopicMatch | undefined {
  if (exampleId === undefined || annotation?.focus === undefined) return undefined;
  return { topic: buildInlineFocusTopic(exampleId, annotation.focus), score: Number.MAX_SAFE_INTEGER };
}
 
function orderTopicMatches(matches: readonly TopicMatch[], annotation?: ExampleTutorAnnotation): readonly TopicMatch[] {
  const byId = new Map(matches.map((match) => [match.topic.id, match]));
  const boundIds = [
    ...(annotation?.primaryTopic ? [annotation.primaryTopic] : []),
    ...(annotation?.topics ?? []),
  ];
  const bound = boundIds.map((id) => byId.get(id)).filter((match): match is TopicMatch => match !== undefined);
  const boundSet = new Set(bound.map(({ topic }) => topic.id));
  return [...bound, ...matches.filter(({ topic }) => !boundSet.has(topic.id))];
}
 
function phaseForTopic(state: TutorProgressionState, topicId: string, classification?: string): DidacticPhase {
  if (classification === "mastered" || state.masteredTopicIds.includes(topicId)) return state.retainedPhases[topicId] ?? "DEEPEN";
  return "LEARN";
}
 
function progressionOptions(topic: CurriculumTopic, phase: DidacticPhase, state: TutorProgressionState) {
  if (phase === "LEARN") return { phase } as const;
  const criteria = deepeningCriteria(topic);
  const existingQuestionKinds = (state.postMasteryEvidence[topic.id] ?? []).map(({ kind }) => kind);
  return {
    phase,
    preferredQuestionKinds: criteria.requiredQuestionKinds,
    existingQuestionKinds,
  } as const;
}
 
/**
 * The Topic's turns of the whole Tutor session, as the planner and the Topic classification see
 * them. The browser sends only the last INPUT_LIMITS.tutor.maxHistoryEntries dialog turns, while
 * the progression state keeps every answered planned question of the session as evidence. A
 * question that has left the dialog window is therefore restored from the evidence: it stays used
 * (strict repetition never reuses a Question ID, TutorQuality SSOT §3), and its LEARN rating keeps
 * counting for Concept mastery. Stored evidence wins over a dialog turn for the same question;
 * post-mastery evidence marks questions as used without counting toward LEARN mastery.
 */
function sessionHistory(
  topic: CurriculumTopic,
  history: readonly TutorDialogTurn[],
  state: TutorProgressionState,
): TutorDialogTurn[] {
  const byText = new Map(topic.questions.flatMap((question) => question.text ? [[question.text, question.id] as const] : []));
  const textById = new Map(topic.questions.map((question) => [question.id, question.text ?? question.template ?? question.id]));
  const mastery = state.masteryEvidence[topic.id] ?? [];
  const storedRatingIds = new Set(mastery.map(({ questionId }) => questionId));
  const evidenceTurns = [
    ...mastery.map((observation) => ({ observation, rated: true })),
    ...(state.postMasteryEvidence[topic.id] ?? []).map((observation) => ({ observation, rated: false })),
  ].map(({ observation, rated }): TutorDialogTurn => ({
    question: textById.get(observation.questionId) ?? observation.questionId,
    questionId: observation.questionId,
    answer: "",
    responseStyle: "normal",
    ...(rated ? { answerRating: observation.rating } : {}),
  }));
  const dialogTurns = history.map((turn) => {
    const questionId = turn.questionId ?? byText.get(turn.question);
    if (questionId === undefined || !storedRatingIds.has(questionId)) return turn;
    const { answerRating: _storedInstead, ...unrated } = turn;
    return { ...unrated, questionId };
  });
  return [...evidenceTurns, ...dialogTurns];
}
 
/** The question being answered, as a used turn without a rating of its own. */
function answeredTurn(currentQuestion: string): TutorDialogTurn {
  return { question: currentQuestion, answer: "", responseStyle: "normal" };
}
 
function classifyWithState(
  topic: CurriculumTopic,
  facts: ReturnType<SketchFactExtractor["extract"]>,
  history: readonly TutorDialogTurn[],
  state: TutorProgressionState,
  difficulty: TutorDifficulty,
  strategy: EffectiveTutorStrategy,
) {
  const session = sessionHistory(topic, history, state);
  const usedQuestionIds = new Set(session.flatMap(({ questionId }) => questionId ? [questionId] : []));
  const classification = classifyTopic(topic, facts, collectObservations(topic, session), usedQuestionIds, difficulty, strategy);
  if (state.masteredTopicIds.includes(topic.id) && classification.status !== "inapplicable") {
    return { status: "mastered", masteryDomain: classification.masteryDomain } satisfies TopicClassification;
  }
  return classification;
}
 
const EXPANSION_INDICATOR_ID = "expansion";
 
function expansionQuestionId(targetTopicId: string): string {
  return `expand-${targetTopicId.slice(0, 56)}`;
}
 
function expansionQuestionText(objective: string): string {
  return `Welche kleine, direkt am aktuellen Sketch prüfbare Erweiterung würdest du als Nächstes selbst umsetzen, um dieses Lernziel zu bearbeiten: „${objective}“, und woran würdest du ihre Wirkung erkennen?`;
}
 
// The dialog request carries only the text of the answered question, not its ID.
// A generated extension question is therefore recognized by the extension targets
// the application already recorded as delivered for this Topic; the text is built
// by the same function that generated it.
function answeredExpansionTarget(topic: CurriculumTopic, state: TutorProgressionState, currentQuestion: string): string | undefined {
  if (topic.schemaVersion !== 2 || !topic.extensions) return undefined;
  const delivered = new Set(state.usedExpansionTargetTopicIds[topic.id] ?? []);
  return topic.extensions.find(({ topic: targetTopicId, objective }) =>
    delivered.has(targetTopicId) && expansionQuestionText(objective) === currentQuestion,
  )?.topic;
}
 
// The plan of the question being answered, resolved by identity: a generated extension the
// application delivered for this Topic, or a Topic question. Reserves nothing in the state.
function answeredQuestionPlan(context: AdapterContext, currentQuestion: string, history: readonly TutorDialogTurn[]): TutorPlan | null {
  const expansionTargetId = answeredExpansionTarget(context.topic, context.state, currentQuestion);
  const extension = expansionTargetId && context.topic.schemaVersion === 2
    ? context.topic.extensions?.find(({ topic }) => topic === expansionTargetId)
    : undefined;
  if (extension) {
    return buildExpansionPlan(context, context.phase, { sourceTopicId: context.topic.id, targetTopicId: extension.topic, objective: extension.objective });
  }
  const question = findCurriculumQuestion(context.topic, currentQuestion, history);
  const concept = question ? context.topic.concepts.find(({ id }) => id === question.concept) : undefined;
  if (!question || !concept) return null;
  return normalizePlan(buildPlan(context.topic, context.revision, concept, question), context.strategy, context.state, context.phase, context.extensionTargetTopicId);
}
 
function buildExpansionPlan(context: AdapterContext, phase: DidacticPhase, expansionBrief: TutorExpansionBrief): TutorPlan {
  const questionId = expansionQuestionId(expansionBrief.targetTopicId);
  return {
    topicId: context.topic.id,
    topicTitle: context.topic.title,
    conceptId: questionId,
    conceptTitle: "Eigene Erweiterung",
    objective: expansionBrief.objective,
    questionId,
    questionKind: "transfer",
    indicatorId: EXPANSION_INDICATOR_ID,
    indicator: expansionBrief.objective,
    question: expansionQuestionText(expansionBrief.objective),
    misconceptions: [],
    contentRevision: context.revision,
    strategyId: context.strategy.strategy.id,
    strategySource: context.strategy.source === "built-in" ? "built-in" : "repository",
    effectiveStrategy: context.strategy.strategy,
    learningPhase: phase,
    activeTopicId: context.state.activeTopicId,
    masteredTopicIds: [...context.state.masteredTopicIds],
    expansionBrief,
  };
}
 
type PlanningContext = Pick<AdapterContext, "revision" | "topic" | "strategy" | "phase" | "state">;
 
function exhaustionResult(context: PlanningContext, phase: DidacticPhase): TutorPlanningBlocked {
  context.state.phase = phase;
  context.state.progressionBlockedReason = "content-exhausted";
  return {
    kind: "blocked",
    progressionBlockedReason: "content-exhausted",
    contentRevision: context.revision,
    learningPhase: phase,
    activeTopicId: context.topic.id,
    masteredTopicIds: [...context.state.masteredTopicIds],
    strategyId: context.strategy.strategy.id,
    strategySource: context.strategy.source === "built-in" ? "built-in" : "repository",
    effectiveStrategy: context.strategy.strategy,
  };
}
 
function transitionResult(context: PlanningContext): import("./tutor-planning").TutorPlanningTransition {
  return {
    kind: "transition",
    contentRevision: context.revision,
    learningPhase: context.phase,
    activeTopicId: context.topic.id,
    masteredTopicIds: [...context.state.masteredTopicIds],
    strategyId: context.strategy.strategy.id,
    strategySource: context.strategy.source === "built-in" ? "built-in" : "repository",
    effectiveStrategy: context.strategy.strategy,
  };
}
 
function blockedResult(context: PlanningContext, state: TutorProgressionState): TutorPlanningBlocked {
  state.phase = context.phase;
  state.progressionBlockedReason = "content-exhausted";
  return {
    kind: "blocked",
    progressionBlockedReason: "content-exhausted",
    contentRevision: context.revision,
    learningPhase: context.phase,
    activeTopicId: context.topic.id,
    masteredTopicIds: [...state.masteredTopicIds],
    strategyId: context.strategy.strategy.id,
    strategySource: context.strategy.source === "built-in" ? "built-in" : "repository",
    effectiveStrategy: context.strategy.strategy,
  };
}
 
 
function toObservation(current: CurriculumQuestion, rating: Parameters<NonNullable<LearningPlanner["advance"]>>[5]): Observation {
  return {
    questionId: current.id,
    conceptId: current.concept,
    indicatorId: current.indicator,
    kind: current.kind,
    rating,
  };
}