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 | 31x 3719x 6668x 946x 946x 946x 1x 533x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 946x 413x 413x 533x 533x 533x 533x 533x 533x 533x 533x 946x 946x 45x 639x 907x 45x 272x 272x 266x 2x 1875x 575x 575x 622x 622x 622x 622x 1x 615x 269x 268x 2x 612x 2x 631x 109x 109x 3x 106x 105x 105x 108x 4x 104x 104x 103x 100x 631x 631x 631x 665x 632x 632x 1198x 631x 631x 631x 631x 631x 631x 643x 401x 401x 1x 1357x 400x 1x 399x 399x 994x 994x 994x 994x 993x 992x 992x 992x 399x 667x 946x 667x 945x 1x 667x 645x 304x 574x 2x 670x 670x 670x 670x 670x 669x 669x 669x 668x 668x 668x 1x 1x 667x 667x 667x 667x 667x 75x 75x 75x 75x 75x 670x 75x 75x 4x 4x 4x 1x 3x | import {
DIDACTIC_PHASES,
PROGRESSION_BLOCKED_REASONS,
type DidacticPhase,
type ProgressionBlockedReason,
} from "../curriculum/progression-state";
import { canonicalDigest } from "./canonical";
import { isValidRevealLiteral, normalizeRevealLiteral } from "./reveal-check";
export interface TutorQualityHistoryEntrySource {
readonly question: string;
readonly answer?: string;
readonly responseStyle?: "normal" | "philosophical";
readonly answerRating?: 1 | 2 | 3 | 4 | 5;
readonly questionId?: string;
}
export type TutorQualityTurnSource =
| {
readonly kind: "initial";
readonly difficulty?: number;
}
| {
readonly kind: "dialog";
readonly question: string;
readonly answer: string;
readonly continuationOf?: number;
readonly difficulty?: number;
readonly history?: readonly TutorQualityHistoryEntrySource[];
};
export interface TutorQualityScenarioSource {
readonly id: string;
readonly sketch: string;
readonly courseContent: string;
readonly model?: string;
readonly judge?: TutorQualityJudgeSource;
readonly turns: readonly TutorQualityTurnSource[];
readonly expected?: TutorQualityExpectation;
}
export type TutorQualityAnswerRatingBand = readonly [min: number, max: number];
export interface TutorQualityExpectation {
readonly topicId?: string;
readonly topicIdAbsent?: string;
readonly learningPhase?: DidacticPhase;
readonly phaseAfter?: DidacticPhase;
readonly answerRating?: TutorQualityAnswerRatingBand;
readonly progressionBlockedReason?: ProgressionBlockedReason;
readonly stateUnchanged?: boolean;
readonly questionNotRepeat?: "exact-or-heuristic";
readonly mustNotReveal?: readonly string[];
}
export interface TutorQualityJudgeSource {
readonly facts: readonly string[];
readonly criteria: readonly {
readonly id: string;
readonly text: string;
}[];
}
export interface TutorQualityCorpusSource {
readonly corpusId: string;
readonly corpusVersion: number;
readonly scenarios: readonly TutorQualityScenarioSource[];
}
export interface TutorQualityCorpusReferences {
readonly sketches: ReadonlySet<string>;
readonly courseContentFixtures: ReadonlySet<string>;
}
export interface TutorQualityCorpus extends TutorQualityCorpusSource {
readonly digest: string;
}
export interface CorpusEvolutionComparison {
readonly valid: boolean;
readonly reason?: "version-not-increased" | "version-changed-without-semantic-change";
}
function fail(message: string): never {
throw new Error(`Invalid Tutor Quality corpus: ${message}`);
}
function assertObject(value: unknown, label: string): asserts value is Record<string, unknown> {
Iif (value === null || typeof value !== "object" || Array.isArray(value)) fail(`${label} must be an object`);
}
function assertNonEmptyString(value: unknown, label: string): asserts value is string {
Iif (typeof value !== "string" || value.trim().length === 0) fail(`${label} must be a non-empty string`);
}
function optionalInteger(value: unknown, label: string): number | undefined {
Iif (value === undefined) return undefined;
Iif (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 100) {
fail(`${label} must be an integer from 1 to 100`);
}
return value;
}
function hasPhilosophicalAnswerRating(entry: Record<string, unknown>): boolean {
return entry.responseStyle === "philosophical" && entry.answerRating !== undefined;
}
function parseHistory(value: unknown, label: string): readonly TutorQualityHistoryEntrySource[] | undefined {
if (value === undefined) return undefined;
Iif (!Array.isArray(value)) fail(`${label}.history must be an array`);
for (const [index, entry] of value.entries()) {
const historyLabel = `${label}.history[${index}]`;
assertObject(entry, historyLabel);
assertNonEmptyString(entry.question, `${historyLabel}.question`);
Iif (entry.answer !== undefined) assertNonEmptyString(entry.answer, `${historyLabel}.answer`);
Iif (entry.responseStyle !== undefined && entry.responseStyle !== "normal" && entry.responseStyle !== "philosophical") {
fail(`${historyLabel}.responseStyle is invalid`);
}
Iif (entry.answerRating !== undefined && ![1, 2, 3, 4, 5].includes(entry.answerRating as 1 | 2 | 3 | 4 | 5)) {
fail(`${historyLabel}.answerRating is invalid`);
}
// Keep corpus history aligned with the existing TutorDialogTurn contract in
// ssot/ssot_function_definition_LearningQuestions.md (philosophical fallback).
Eif (hasPhilosophicalAnswerRating(entry)) {
fail(`${historyLabel}.answerRating is not allowed for philosophical responses`);
}
if (entry.questionId !== undefined) assertNonEmptyString(entry.questionId, `${historyLabel}.questionId`);
}
return value as readonly TutorQualityHistoryEntrySource[];
}
function parseInitialTurn(value: Record<string, unknown>, label: string): TutorQualityTurnSource | undefined {
if (value.kind !== "initial") return undefined;
const difficulty = optionalInteger(value.difficulty, `${label}.difficulty`);
return { kind: "initial", ...(difficulty === undefined ? {} : { difficulty }) };
}
function parseDialogTurn(value: Record<string, unknown>, label: string): TutorQualityTurnSource {
Iif (value.kind !== "dialog") fail(`${label}.kind must be initial or dialog`);
assertNonEmptyString(value.question, `${label}.question`);
assertNonEmptyString(value.answer, `${label}.answer`);
const continuationOf = value.continuationOf;
Iif (continuationOf !== undefined && (typeof continuationOf !== "number" || !Number.isInteger(continuationOf) || continuationOf < 0)) {
fail(`${label}.continuationOf must reference a preceding turn`);
}
const difficulty = optionalInteger(value.difficulty, `${label}.difficulty`);
const history = parseHistory(value.history, label);
return {
kind: "dialog",
question: value.question,
answer: value.answer,
...(continuationOf === undefined ? {} : { continuationOf }),
...(difficulty === undefined ? {} : { difficulty }),
...(history === undefined ? {} : { history }),
};
}
function parseTurn(value: unknown, label: string): TutorQualityTurnSource {
assertObject(value, label);
return parseInitialTurn(value, label) ?? parseDialogTurn(value, label);
}
function normalizedCorpusSource(source: TutorQualityCorpusSource): TutorQualityCorpusSource {
return {
corpusId: source.corpusId,
corpusVersion: source.corpusVersion,
scenarios: source.scenarios.map((scenario) => ({
...scenario,
turns: scenario.turns.map((turn) => ({ ...turn })),
})),
};
}
export function tutorQualityCorpusDigest(source: TutorQualityCorpusSource): string {
return canonicalDigest(normalizedCorpusSource(source));
}
function parseAnswerRatingBand(value: unknown, label: string): TutorQualityAnswerRatingBand {
const valid = Array.isArray(value)
&& value.length === 2
&& value.every((rating) => typeof rating === "number" && Number.isInteger(rating) && rating >= 1 && rating <= 5)
&& (value[0] as number) <= (value[1] as number);
if (!valid) fail(`${label}.expected.answerRating must be [min, max] with 1 <= min <= max <= 5`);
return [value[0] as number, value[1] as number];
}
const EXPECTED_KEYS = new Set(["topicId", "topicIdAbsent", "learningPhase", "phaseAfter", "answerRating", "progressionBlockedReason", "stateUnchanged", "questionNotRepeat", "mustNotReveal"]);
function optionalEnumValue<T extends string>(value: unknown, allowed: readonly T[], label: string): T | undefined {
if (value === undefined) return undefined;
Iif (typeof value !== "string" || !(allowed as readonly string[]).includes(value)) fail(`${label} is invalid`);
return value as T;
}
function parseProgressionExpectation(
value: Record<string, unknown>,
label: string,
turns: readonly TutorQualityTurnSource[],
): Pick<TutorQualityExpectation, "learningPhase" | "phaseAfter" | "answerRating"> {
const learningPhase = optionalEnumValue(value.learningPhase, DIDACTIC_PHASES, `${label}.expected.learningPhase`);
const phaseAfter = optionalEnumValue(value.phaseAfter, DIDACTIC_PHASES, `${label}.expected.phaseAfter`);
const answerRating = value.answerRating === undefined ? undefined : parseAnswerRatingBand(value.answerRating, label);
if (answerRating !== undefined && turns.at(-1)?.kind !== "dialog") {
fail(`${label}.expected.answerRating requires the final turn to be a dialog turn`);
}
if (phaseAfter !== undefined) {
if (learningPhase === undefined) fail(`${label}.expected.phaseAfter requires expected.learningPhase`);
// Progression out of LEARN and DEEPEN always depends on ratings.
if (learningPhase !== "EXPAND" && answerRating === undefined) {
fail(`${label}.expected.phaseAfter after a ${learningPhase} turn requires expected.answerRating`);
}
}
return {
...(learningPhase === undefined ? {} : { learningPhase }),
...(phaseAfter === undefined ? {} : { phaseAfter }),
...(answerRating === undefined ? {} : { answerRating }),
};
}
const MAX_REVEAL_LITERALS = 5;
function parseMustNotReveal(
value: unknown,
label: string,
turns: readonly TutorQualityTurnSource[],
): readonly string[] | undefined {
if (value === undefined) return undefined;
const field = `${label}.expected.mustNotReveal`;
if (!Array.isArray(value) || value.length < 1 || value.length > MAX_REVEAL_LITERALS) {
fail(`${field} must be a list of 1 to ${MAX_REVEAL_LITERALS} literals`);
}
if (!turns.some((turn) => turn.kind === "dialog")) fail(`${field} requires a dialog turn`);
const seen = new Set<string>();
for (const literal of value) {
if (typeof literal !== "string" || !isValidRevealLiteral(literal)) {
fail(`${field} literals must be specific strings (at least 3 characters with a letter)`);
}
const normalized = normalizeRevealLiteral(literal);
if (seen.has(normalized)) fail(`${field} contains a duplicate literal`);
seen.add(normalized);
}
return value as readonly string[];
}
function parseTopicExpectations(
value: Record<string, unknown>,
label: string,
): Pick<TutorQualityExpectation, "topicId" | "topicIdAbsent"> {
if (value.topicId !== undefined) assertNonEmptyString(value.topicId, `${label}.expected.topicId`);
if (value.topicIdAbsent !== undefined) assertNonEmptyString(value.topicIdAbsent, `${label}.expected.topicIdAbsent`);
return {
...(typeof value.topicId === "string" ? { topicId: value.topicId } : {}),
...(typeof value.topicIdAbsent === "string" ? { topicIdAbsent: value.topicIdAbsent } : {}),
};
}
function parseExpected(
value: unknown,
label: string,
turns: readonly TutorQualityTurnSource[],
): TutorQualityExpectation | undefined {
if (value === undefined) return undefined;
assertObject(value, `${label}.expected`);
for (const key of Object.keys(value)) {
if (!EXPECTED_KEYS.has(key)) fail(`${label}.expected.${key} is unknown`);
}
const topicExpectations = parseTopicExpectations(value, label);
const progressionBlockedReason = optionalEnumValue(value.progressionBlockedReason, PROGRESSION_BLOCKED_REASONS, `${label}.expected.progressionBlockedReason`);
Iif (value.stateUnchanged !== undefined && typeof value.stateUnchanged !== "boolean") fail(`${label}.expected.stateUnchanged is invalid`);
Iif (value.questionNotRepeat !== undefined && value.questionNotRepeat !== "exact-or-heuristic") {
fail(`${label}.expected.questionNotRepeat is invalid`);
}
const mustNotReveal = parseMustNotReveal(value.mustNotReveal, label, turns);
return {
...topicExpectations,
...parseProgressionExpectation(value, label, turns),
...(progressionBlockedReason === undefined ? {} : { progressionBlockedReason }),
...(typeof value.stateUnchanged === "boolean" ? { stateUnchanged: value.stateUnchanged } : {}),
...(value.questionNotRepeat === "exact-or-heuristic" ? { questionNotRepeat: value.questionNotRepeat } : {}),
...(mustNotReveal === undefined ? {} : { mustNotReveal }),
};
}
function parseJudge(value: unknown, label: string): TutorQualityJudgeSource | undefined {
if (value === undefined) return undefined;
assertObject(value, `${label}.judge`);
if (!Array.isArray(value.facts) || value.facts.length === 0) {
fail(`${label}.judge.facts must contain at least one fact`);
}
value.facts.forEach((fact, index) => assertNonEmptyString(fact, `${label}.judge.facts[${index}]`));
if (!Array.isArray(value.criteria) || value.criteria.length < 1 || value.criteria.length > 6) {
fail(`${label}.judge.criteria must contain 1 to 6 criteria`);
}
const ids = new Set<string>();
const criteria = value.criteria.map((criterion, index) => {
const criterionLabel = `${label}.judge.criteria[${index}]`;
assertObject(criterion, criterionLabel);
assertNonEmptyString(criterion.id, `${criterionLabel}.id`);
if (!/^[a-z][a-z0-9-]{0,63}$/.test(criterion.id)) fail(`${criterionLabel}.id is invalid`);
if (ids.has(criterion.id)) fail(`${label}.judge.criteria has duplicate id ${criterion.id}`);
ids.add(criterion.id);
assertNonEmptyString(criterion.text, `${criterionLabel}.text`);
return { id: criterion.id, text: criterion.text };
});
return { facts: value.facts as string[], criteria };
}
function parseTurns(value: unknown, label: string): readonly TutorQualityTurnSource[] {
Iif (!Array.isArray(value) || value.length === 0) fail(`${label}.turns must be a non-empty array`);
const turns = value.map((turn, turnIndex) => parseTurn(turn, `${label}.turns[${turnIndex}]`));
turns.forEach((turn, turnIndex) => {
if (turn.kind === "dialog" && turn.continuationOf !== undefined && turn.continuationOf >= turnIndex) {
fail(`${label}.turns[${turnIndex}].continuationOf must reference a preceding turn`);
}
});
return turns;
}
// R-TURN-3: in a Strategy case (one that declares expected.learningPhase) the planner, not the
// corpus author, must choose the answered question, so every dialog turn is bound via continuationOf.
function requireBoundStrategyTurns(
turns: readonly TutorQualityTurnSource[],
expected: TutorQualityExpectation | undefined,
label: string,
): void {
if (expected?.learningPhase === undefined) return;
turns.forEach((turn, turnIndex) => {
if (turn.kind === "dialog" && turn.continuationOf === undefined) {
fail(`${label}.turns[${turnIndex}] must declare continuationOf because the case declares expected.learningPhase (R-TURN-3)`);
}
});
}
function parseScenario(
rawScenario: unknown,
index: number,
references: TutorQualityCorpusReferences,
ids: Set<string>,
): TutorQualityScenarioSource {
const label = `scenarios[${index}]`;
assertObject(rawScenario, label);
assertNonEmptyString(rawScenario.id, `${label}.id`);
Iif (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(rawScenario.id)) fail(`${label}.id is not stable-safe`);
if (ids.has(rawScenario.id)) fail(`duplicate scenario id ${rawScenario.id}`);
ids.add(rawScenario.id);
assertNonEmptyString(rawScenario.sketch, `${label}.sketch`);
if (!references.sketches.has(rawScenario.sketch)) fail(`${label}.sketch is not a registered fixture`);
assertNonEmptyString(rawScenario.courseContent, `${label}.courseContent`);
Iif (rawScenario.courseContent !== "free" && !references.courseContentFixtures.has(rawScenario.courseContent)) {
fail(`${label}.courseContent is not a registered fixture`);
}
if (rawScenario.model !== undefined) {
assertNonEmptyString(rawScenario.model, `${label}.model`);
Eif (rawScenario.model === "auto") fail(`${label}.model must be a fixed model id`);
}
const turns = parseTurns(rawScenario.turns, label);
const expected = parseExpected(rawScenario.expected, label, turns);
requireBoundStrategyTurns(turns, expected, label);
const judge = parseJudge(rawScenario.judge, label);
return {
id: rawScenario.id,
sketch: rawScenario.sketch,
courseContent: rawScenario.courseContent,
...(rawScenario.model === undefined ? {} : { model: rawScenario.model }),
...(judge === undefined ? {} : { judge }),
turns,
...(expected === undefined ? {} : { expected }),
} satisfies TutorQualityScenarioSource;
}
export function parseTutorQualityCorpus(
source: unknown,
references: TutorQualityCorpusReferences,
): TutorQualityCorpus {
assertObject(source, "corpus");
assertNonEmptyString(source.corpusId, "corpusId");
Iif (typeof source.corpusVersion !== "number" || !Number.isInteger(source.corpusVersion) || source.corpusVersion < 1) {
fail("corpusVersion must be a positive integer");
}
Iif (!Array.isArray(source.scenarios) || source.scenarios.length === 0) fail("scenarios must be a non-empty array");
const ids = new Set<string>();
const scenarios = source.scenarios.map((rawScenario, index) => parseScenario(rawScenario, index, references, ids));
const normalized = { corpusId: source.corpusId, corpusVersion: source.corpusVersion, scenarios } satisfies TutorQualityCorpusSource;
return { ...normalized, digest: tutorQualityCorpusDigest(normalized) };
}
export function compareTutorQualityCorpusVersions(
previous: TutorQualityCorpus,
current: TutorQualityCorpus,
): CorpusEvolutionComparison {
const changed = previous.digest !== current.digest;
Iif (!changed && previous.corpusVersion !== current.corpusVersion) {
return { valid: false, reason: "version-changed-without-semantic-change" };
}
if (changed && current.corpusVersion <= previous.corpusVersion) {
return { valid: false, reason: "version-not-increased" };
}
return { valid: true };
}
|