File size: 16,582 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  chaptersWithPhysicsSeeds,
  getAllLessonSeeds,
  type LessonSeedContentType,
  type TuitionLessonSeed,
} from "./physicsLessonSeeds";
import {
  getPhysicsMissionBlueprint,
  getPhysicsMissionBlueprints,
  getMissingBlueprintRequirements,
  type PhysicsMissionBlueprintType,
} from "./physicsMissionBlueprints";
import { getPhysicsSourcePack, getPhysicsSourcePacks, type PhysicsChapterSourcePack } from "./physicsSourcePacks";
import { evaluateMagneticEffectCoverageGate } from "./magneticEffectCoverageGate";

/**
 * Curation status distinguishes *source readiness* from *teachable curation*:
 * - "curated"        β€” final teachable seeds exist and are registered.
 * - "curation_ready" β€” source repaired + evidence gate passed; final seeds MISSING (Phase 6J).
 * - "draftable"      β€” source strong enough to draft review-only, not gate-passed.
 * - "blocked"        β€” source too thin; needs manual source repair first.
 */
export type ChapterCurationStatus = "curated" | "curation_ready" | "draftable" | "blocked";

export type DraftSeedStatus =
  | "curated"
  | "draft_needs_review"
  | "blocked_missing_source"
  | "blocked_missing_required_sections";

export type PhysicsDraftLessonSeed = {
  missionId: string;
  chapterId: string;
  chapterTitle: string;
  missionTitle: string;
  status: DraftSeedStatus;
  canTeach: boolean;
  missingFields: string[];
  warnings: string[];
  seed?: TuitionLessonSeed;
};

export type PhysicsSeedCoverage = {
  chapterId: string;
  chapterTitle: string;
  curatedSeedCount: number;
  missionIds: string[];
  missionTypes: LessonSeedContentType[];
};

export type PhysicsDraftSeedPlan = {
  chapterId: string;
  chapterTitle: string;
  status: DraftSeedStatus;
  sourceConfidence: PhysicsChapterSourcePack["coverageConfidence"] | "missing";
  recommendedBlueprints: PhysicsMissionBlueprintType[];
  missingSignals: string[];
  warnings: string[];
  nextAction: string;
};

export type PhysicsSeedFactoryReport = {
  totalPhysicsChapters: number;
  curatedChapters: string[];
  uncuratedChapters: string[];
  blockedChapters: Array<{ chapterId: string; reason: string }>;
  draftableChapters: string[];
  /** Chapters whose thin extraction was repaired by a manual evidence override (Phase 6I). */
  sourceRepairedChapters: string[];
  /** Source-repaired + gate-passed chapters awaiting final seed authoring (not teachable yet). */
  curationReadyChapters: string[];
  nextRecommendedBatch: string[];
  /** Human-readable next action so source repair is never confused with final curation. */
  nextRecommendedAction: string;
  coverageByChapter: Array<{
    chapterId: string;
    chapterTitle: string;
    sourceConfidence: PhysicsChapterSourcePack["coverageConfidence"];
    curatedSeedCount: number;
    curationStatus: ChapterCurationStatus;
    sourceRepaired: boolean;
    finalSeedsPresent: boolean;
    missingMissionTypes: PhysicsMissionBlueprintType[];
    warnings: string[];
  }>;
};

export type PhysicsSeedFactoryStatus = {
  officialCuratedChapters: string[];
  uncuratedChapters: string[];
  draftableChapters: string[];
  blockedChapters: Array<{ chapterId: string; reason: string }>;
  officialSeedCount: number;
};

function unique<T>(values: T[]): T[] {
  return Array.from(new Set(values));
}

function curatedSeedsFor(chapterId: string): TuitionLessonSeed[] {
  return getAllLessonSeeds().filter((seed) => seed.chapterId === chapterId);
}

function missionTypeFromContentType(contentType: LessonSeedContentType): PhysicsMissionBlueprintType {
  if (contentType === "concept") return "concept";
  if (contentType === "diagram") return "diagram";
  if (contentType === "formula") return "formula";
  if (contentType === "numerical") return "numerical";
  if (contentType === "exam_answer") return "exam_answer";
  if (contentType === "revision") return "revision";
  if (contentType === "pyq_placeholder") return "pyq_placeholder";
  return "concept";
}

function recommendedBlueprintsFor(pack: PhysicsChapterSourcePack): PhysicsMissionBlueprintType[] {
  const signals = pack.extractedSignals;
  const recommended: PhysicsMissionBlueprintType[] = [];

  if (signals.definitions.length || signals.keyTerms.length) recommended.push("concept");
  if (signals.diagrams.length) recommended.push("diagram");
  if (signals.formulas.length) recommended.push("formula");
  if (signals.formulas.length && signals.numericalPatterns.length) recommended.push("numerical");
  if (signals.possibleQuestions.length) recommended.push("exam_answer");
  if (signals.definitions.length || signals.formulas.length || signals.possibleQuestions.length) recommended.push("revision");

  return recommended.length ? unique(recommended) : ["concept", "exam_answer", "revision"];
}

function missingSignalsFor(pack: PhysicsChapterSourcePack, blueprint: PhysicsMissionBlueprintType): string[] {
  const signals = pack.extractedSignals;
  const missing: string[] = [];
  const requiredSignals = getPhysicsMissionBlueprint(blueprint).requiredSignals;

  if (requiredSignals.includes("definitions") && signals.definitions.length === 0) missing.push("definitions");
  if (requiredSignals.includes("keyTerms") && signals.keyTerms.length === 0) missing.push("key terms");
  if (requiredSignals.includes("formulas") && signals.formulas.length === 0) missing.push("formulas");
  if (requiredSignals.includes("visualBoard") && signals.diagrams.length === 0) missing.push("visual board frames");
  if (requiredSignals.includes("solvedExample") && signals.numericalPatterns.length === 0) missing.push("solved numerical example");
  if (requiredSignals.includes("examAnswers") && signals.possibleQuestions.length === 0) missing.push("exam answer questions");
  if (requiredSignals.includes("keywords") && signals.keyTerms.length === 0) missing.push("exam keywords");
  if (requiredSignals.includes("flashcards") && signals.definitions.length === 0) missing.push("flashcards/revision anchors");
  if (requiredSignals.includes("weakAreaTags") && signals.keyTerms.length === 0) missing.push("weak area tags");
  if (requiredSignals.includes("curatedPyqEvidence")) missing.push("curated PYQ evidence");

  return unique(missing);
}

function draftMissingFields(pack: PhysicsChapterSourcePack, blueprint: PhysicsMissionBlueprintType): string[] {
  return unique([
    "human-authored 11-step lesson seed",
    "human review approval",
    ...missingSignalsFor(pack, blueprint),
    ...getMissingBlueprintRequirements(blueprint, {
      sourceRefs: pack.sourceRefs,
      formulas: pack.extractedSignals.formulas.map((formula) => ({ formula, symbols: [] })),
      solvedExamples: pack.extractedSignals.numericalPatterns,
      visualFrames: pack.extractedSignals.diagrams,
      examAnswers: pack.extractedSignals.possibleQuestions.map(() => ({ keywords: pack.extractedSignals.keyTerms })),
      flashcards: pack.extractedSignals.definitions,
      weakAreaTags: pack.extractedSignals.keyTerms,
      pyqEvidenceStatus: "not_curated",
    }),
  ]);
}

export function getCuratedPhysicsSeedCoverage(): PhysicsSeedCoverage[] {
  const byChapter = new Map<string, TuitionLessonSeed[]>();
  for (const seed of getAllLessonSeeds()) {
    byChapter.set(seed.chapterId, [...(byChapter.get(seed.chapterId) ?? []), seed]);
  }

  return Array.from(byChapter.entries()).map(([chapterId, seeds]) => ({
    chapterId,
    chapterTitle: seeds[0]?.chapterTitle ?? chapterId,
    curatedSeedCount: seeds.length,
    missionIds: seeds.map((seed) => seed.missionId),
    missionTypes: unique(seeds.map((seed) => seed.contentType)),
  }));
}

export function getUncuratedPhysicsChapters(): PhysicsChapterSourcePack[] {
  const curated = new Set(chaptersWithPhysicsSeeds());
  return getPhysicsSourcePacks().filter((pack) => !curated.has(pack.chapterId));
}

export function createPhysicsDraftSeedPlan(chapterId: string): PhysicsDraftSeedPlan {
  const pack = getPhysicsSourcePack(chapterId);
  if (!pack) {
    return {
      chapterId,
      chapterTitle: chapterId,
      status: "blocked_missing_source",
      sourceConfidence: "missing",
      recommendedBlueprints: [],
      missingSignals: ["source pack"],
      warnings: ["No Physics source pack exists for this chapter."],
      nextAction: "Locate source metadata before drafting any lesson seed.",
    };
  }

  if (chaptersWithPhysicsSeeds().includes(chapterId)) {
    return {
      chapterId,
      chapterTitle: pack.chapterTitle,
      status: "curated",
      sourceConfidence: pack.coverageConfidence,
      recommendedBlueprints: [],
      missingSignals: [],
      warnings: pack.warnings,
      nextAction: "Already curated. Do not regenerate from the factory.",
    };
  }

  const recommendedBlueprints = recommendedBlueprintsFor(pack);
  const missingSignals = unique(recommendedBlueprints.flatMap((blueprint) => missingSignalsFor(pack, blueprint)));
  const blocked = pack.coverageConfidence === "low";

  return {
    chapterId,
    chapterTitle: pack.chapterTitle,
    status: blocked ? "blocked_missing_required_sections" : "draft_needs_review",
    sourceConfidence: pack.coverageConfidence,
    recommendedBlueprints,
    missingSignals,
    warnings: pack.warnings,
    nextAction: blocked
      ? "Create a human curation TODO before drafting. Extracted signals are too thin for safe seed drafting."
      : "Draft review-only seeds from the recommended blueprints; keep canTeach=false until human-authored content validates.",
  };
}

export function createPhysicsDraftSeedsForChapter(chapterId: string): PhysicsDraftLessonSeed[] {
  const pack = getPhysicsSourcePack(chapterId);
  if (!pack) {
    return [
      {
        missionId: "TODO",
        chapterId,
        chapterTitle: chapterId,
        missionTitle: "Source pack missing",
        status: "blocked_missing_source",
        canTeach: false,
        missingFields: ["source pack", "sourceRefs"],
        warnings: ["No Physics source pack exists for this chapter."],
      },
    ];
  }

  const curatedSeeds = curatedSeedsFor(chapterId);
  if (curatedSeeds.length > 0) {
    return curatedSeeds.map((seed) => ({
      missionId: seed.missionId,
      chapterId: seed.chapterId,
      chapterTitle: seed.chapterTitle,
      missionTitle: seed.missionTitle,
      status: "curated",
      canTeach: seed.canTeach,
      missingFields: [],
      warnings: seed.qualityFlags.reason ? [seed.qualityFlags.reason] : [],
      seed,
    }));
  }

  const plan = createPhysicsDraftSeedPlan(chapterId);
  if (plan.status === "blocked_missing_required_sections") {
    return [
      {
        missionId: "TODO",
        chapterId: pack.chapterId,
        chapterTitle: pack.chapterTitle,
        missionTitle: `Review source pack for ${pack.chapterTitle}`,
        status: "blocked_missing_required_sections",
        canTeach: false,
        missingFields: unique(["strong source signals", ...plan.missingSignals, "human-authored 11-step lesson seed"]),
        warnings: plan.warnings,
      },
    ];
  }

  return plan.recommendedBlueprints.map((blueprint, index) => ({
    missionId: `D${index + 1}`,
    chapterId: pack.chapterId,
    chapterTitle: pack.chapterTitle,
    missionTitle: `${getPhysicsMissionBlueprint(blueprint).title} - ${pack.chapterTitle}`,
    status: "draft_needs_review",
    canTeach: false,
    missingFields: draftMissingFields(pack, blueprint),
    warnings: unique([
      ...plan.warnings,
      "Draft output is review-only and is not registered in Tuition Mode.",
      "No official canTeach=true seed is produced by the factory.",
    ]),
  }));
}

/**
 * Curation status per chapter. Distinguishes source repair (Phase 6I) from final
 * teachable curation. A source-repaired chapter that passes its evidence gate is
 * "curation_ready" β€” its final seeds are still MISSING and it is NOT teachable.
 */
function curationStatusFor(pack: PhysicsChapterSourcePack, hasSeeds: boolean): ChapterCurationStatus {
  if (hasSeeds) return "curated";

  if (pack.sourceRepaired) {
    if (pack.chapterId === "phy-p1-c4") {
      const gate = evaluateMagneticEffectCoverageGate();
      if (gate.status === "curation_ready") return "curation_ready";
      if (gate.status === "draftable") return "draftable";
      return "blocked";
    }
    return "draftable";
  }

  return pack.coverageConfidence === "low" ? "blocked" : "draftable";
}

export function createPhysicsBatchCurationReport(): PhysicsSeedFactoryReport {
  const packs = getPhysicsSourcePacks();
  const coverage = getCuratedPhysicsSeedCoverage();
  const curatedByChapter = new Map(coverage.map((item) => [item.chapterId, item]));
  const curatedChapters = coverage.map((item) => item.chapterId);
  const uncurated = packs.filter((pack) => !curatedByChapter.has(pack.chapterId));

  const statusByChapter = new Map<string, ChapterCurationStatus>(
    packs.map((pack) => [pack.chapterId, curationStatusFor(pack, curatedByChapter.has(pack.chapterId))]),
  );

  const blockedChapters = packs
    .filter((pack) => statusByChapter.get(pack.chapterId) === "blocked")
    .map((pack) => ({ chapterId: pack.chapterId, reason: pack.warnings[0] ?? "Source too thin to draft." }));
  const draftableChapters = packs
    .filter((pack) => statusByChapter.get(pack.chapterId) === "draftable")
    .map((pack) => pack.chapterId);
  const curationReadyChapters = packs
    .filter((pack) => statusByChapter.get(pack.chapterId) === "curation_ready")
    .map((pack) => pack.chapterId);
  const sourceRepairedChapters = packs.filter((pack) => pack.sourceRepaired).map((pack) => pack.chapterId);

  // Priority of the next real work:
  //   1. curation_ready chapters β†’ author final teaching seeds (Phase 6J).
  //   2. draftable chapters β†’ draft review-only seeds.
  //   3. blocked chapters β†’ repair the source first.
  let nextRecommendedBatch: string[];
  let nextRecommendedAction: string;
  if (curationReadyChapters.length > 0) {
    nextRecommendedBatch = curationReadyChapters.slice(0, 2);
    nextRecommendedAction = `Phase 6J: curate final lesson seeds for ${curationReadyChapters.join(", ")} (source repaired, final seeds missing, not teachable yet).`;
  } else if (draftableChapters.length > 0) {
    nextRecommendedBatch = draftableChapters.slice(0, 2);
    nextRecommendedAction = `Draft review-only seeds for ${draftableChapters.slice(0, 2).join(", ")}.`;
  } else if (blockedChapters.length > 0) {
    nextRecommendedBatch = blockedChapters.map((chapter) => chapter.chapterId).slice(0, 2);
    nextRecommendedAction = `Repair the source layer for ${blockedChapters.map((c) => c.chapterId).slice(0, 2).join(", ")} before drafting.`;
  } else {
    nextRecommendedBatch = [];
    nextRecommendedAction =
      "Physics is complete β€” all 7 chapters are curated. Next: a Physics QA pass, or begin Chemistry source repair/curation.";
  }

  return {
    totalPhysicsChapters: packs.length,
    curatedChapters,
    uncuratedChapters: uncurated.map((pack) => pack.chapterId),
    blockedChapters,
    draftableChapters,
    sourceRepairedChapters,
    curationReadyChapters,
    nextRecommendedBatch,
    nextRecommendedAction,
    coverageByChapter: packs.map((pack) => {
      const item = curatedByChapter.get(pack.chapterId);
      const existingMissionTypes = new Set((item?.missionTypes ?? []).map(missionTypeFromContentType));
      const missingMissionTypes = getPhysicsMissionBlueprints()
        .filter((blueprint) => blueprint.canBeTeachable)
        .map((blueprint) => blueprint.type)
        .filter((type) => !existingMissionTypes.has(type));

      return {
        chapterId: pack.chapterId,
        chapterTitle: pack.chapterTitle,
        sourceConfidence: pack.coverageConfidence,
        curatedSeedCount: item?.curatedSeedCount ?? 0,
        curationStatus: statusByChapter.get(pack.chapterId) ?? "blocked",
        sourceRepaired: Boolean(pack.sourceRepaired),
        finalSeedsPresent: (item?.curatedSeedCount ?? 0) > 0,
        missingMissionTypes,
        warnings: pack.warnings,
      };
    }),
  };
}

export function getPhysicsSeedFactoryStatus(): PhysicsSeedFactoryStatus {
  const report = createPhysicsBatchCurationReport();
  return {
    officialCuratedChapters: report.curatedChapters,
    uncuratedChapters: report.uncuratedChapters,
    draftableChapters: report.draftableChapters,
    blockedChapters: report.blockedChapters,
    // Day 20 β€” the seed pool now includes the first Chemistry draft; this
    // factory reports PHYSICS curation status, so count Physics seeds only.
    officialSeedCount: getAllLessonSeeds().filter((seed) => seed.subject === "Physics").length,
  };
}