Files
04cab46412 Practice: questions come back by memory (FSRS-5), a daily cap, hardness aimed at 80% (#424)
Practice plan step 4.

- memory.ts: FSRS-5 (formulas and default weights from ts-fsrs 6.0.1, MIT)
  replaces the fixed 1/3/7/16/35-day ladder. Cards gain optional s, d, n;
  an old card's stability is read from the interval it was given, so no
  schedule is lost. Max interval 180 days. Ratings: miss = again, right
  chosen = good, right typed = easy (when the caller says so).
- todayCount: at most REVIEWS_PER_DAY (24) less today's reviews; the
  dashboard, profile, start card and reminder mail show it, not the pile.
- newShareFor: a sitting's new-question share shrinks once the pile is
  over a day's cap and stops at two days'.
- difficulty.ts: the level keeps first-try success between 70% and 90%,
  aimed at 80% (a starting target from a model result, Wilson et al. 2019).
- schedule.test.ts: twelve simulated weeks; weekly pile 20..56, max 69
  (the ladder: 35..917).
- "N to review today" wording in all seven dictionaries.


Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: forge merge bridge <forge@git.orangecat.ch>
2026-12-02 07:33:45 +01:00

144 lines
6.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Level } from "../vocabulary/distance.ts";
import { frontierOf, type LearnerModel } from "./model.ts";
import type { PracticeItem } from "./types.ts";
/**
* HOW HARD A SITTING IS, as something the learner can move.
*
* WHAT WAS MISSING. The level a sitting draws from was decided by the model
* alone (`frontierOf`): level 1 until twelve answers hold, then 2, then 3.
* Nobody could say "this is too easy" — and a new learner, held at level 1,
* met five level-3 questions in ten sittings although two thirds of the
* course is level 3. The model stays the starting point; this is the lever on
* top of it.
*
* THREE SETTINGS, NOT A SLIDER. `fit` is the model's own level; `easier` and
* `harder` are one band down and one up. They map onto the three level bands
* every item already carries (`levels.ts`), so there is no second notion of
* difficulty to keep in step with the first.
*
* IT ALSO MOVES BY ITSELF, AND SAYS SO. `nudgeOf` reads the first answers of
* the sitting: a run of right answers suggests a step up, repeated misses a
* step down. The sitting applies it, shows that it did, and offers the way
* back in the same line — a level that changed silently would be the product
* deciding for the learner again.
*
* Pure. The stored value is the setting alone, per course (`stored.ts`).
*/
export type Difficulty = "easier" | "fit" | "harder";
/** Easiest first: the order the control shows them in. */
export const DIFFICULTIES = ["easier", "fit", "harder"] as const satisfies readonly Difficulty[];
export const DEFAULT_DIFFICULTY: Difficulty = "fit";
const SHIFT: Record<Difficulty, -1 | 0 | 1> = { easier: -1, fit: 0, harder: 1 };
export function isDifficulty(value: unknown): value is Difficulty {
return typeof value === "string" && (DIFFICULTIES as readonly string[]).includes(value);
}
/** Read the stored setting; null for anything that is not one. */
export function decodeDifficulty(raw: string): Difficulty | null {
try {
const value: unknown = JSON.parse(raw);
return isDifficulty(value) ? value : null;
} catch {
return null;
}
}
/** The level band a sitting aims at: the model's own, moved by the setting, kept inside 1–3. */
export function targetLevel(model: LearnerModel, difficulty: Difficulty = DEFAULT_DIFFICULTY): Level {
return Math.min(3, Math.max(1, frontierOf(model) + SHIFT[difficulty])) as Level;
}
/**
* The settings that change anything for this learner. At level 1 there is
* nothing easier, at level 3 nothing harder; offering a button that does
* nothing would be a control that lies. `fit` is always there.
*/
export function difficultiesFor(model: LearnerModel): Difficulty[] {
const fit = targetLevel(model, "fit");
return DIFFICULTIES.filter((d) => d === "fit" || targetLevel(model, d) !== fit);
}
/** One step easier (-1) or harder (+1); the same value when there is no further step. */
export function stepDifficulty(difficulty: Difficulty, by: -1 | 1): Difficulty {
const at = DIFFICULTIES.indexOf(difficulty) + by;
return DIFFICULTIES[Math.min(DIFFICULTIES.length - 1, Math.max(0, at))]!;
}
/**
* How far an item is from the level aimed at — 0 on it. Off it, a question
* ABOVE the target costs less than one below: when the target band runs out,
* the sitting reaches up before it reaches down. The report this answers was
* "too easy", never "too hard" — and a question that is its own German
* teaches nothing to somebody past it, while a harder one at least teaches.
* Whoever wants the easier band has a button for it.
*
* An item with no level (a declared course that declared none) costs 0: it is
* not known to be off target.
*/
export function levelCost(item: PracticeItem, target: Level): number {
const level = item.source.level;
if (!level || level === target) return 0;
return level > target ? level - target : target - level + 2;
}
/**
* A LEVEL-1 question — «like German» — that the learner answered and never
* missed does not come back for review once they are past level 1.
* «Was mached Sie beruflich?» is its own German; asking it five more times on
* the review schedule teaches nothing and is what made sittings feel too easy
* (the owner's screenshot, 2026-11-25, with level 1 already holding). One
* miss and it is reviewed like anything else. On `easier` the target drops
* back to 1 and it returns.
*
* Level 1 only: a level-2 question is a sound rule applied, and that is worth
* its spaced reviews at any level.
*/
export function fadedOut(item: PracticeItem, card: { lapses: number } | undefined, target: Level): boolean {
return Boolean(card && item.source.level === 1 && target > 1 && card.lapses === 0);
}
/**
* HOW HARD, AIMED AT ABOUT FOUR IN FIVE RIGHT (practice plan step 4). The
* level moves to keep a learner's first-try success near `TARGET_RIGHT`.
*
* Where the number comes from, plainly: Wilson, Shenhav, Straccia and Cohen
* (2019, «The Eighty Five Percent Rule for optimal learning», Nature
* Communications, https://doi.org/10.1038/s41467-019-12552-4) derive an
* error rate of about 15% as optimal for gradient-descent learners —
* machines and models of learning, not a study of people learning a
* language. 80% is OUR starting target from that, to be checked against
* each learner's real success rate once the stats show it; it is a band, not
* a point, so the level does not swing on every answer.
*
* The band (`NUDGE_BAND`) is ours too: up when at least nine in ten of the
* last ten are right and the latest is; down when fewer than seven in ten
* are, or at once on three misses in five, a sitting going badly. Five
* answers before any step up (`NUDGE_UP_AFTER`): fewer says nothing.
*/
export const TARGET_RIGHT = 0.8;
export const NUDGE_UP_AFTER = 5;
export const NUDGE_DOWN = { misses: 3, within: 5 } as const;
export const NUDGE_BAND = { window: 10, up: 0.9, down: 0.7, downAfter: 8 } as const;
/**
* What the sitting's FIRST answers so far suggest: +1 harder, -1 easier, 0
* stay. Skips are left out (an answer about attention, not knowledge). The
* caller passes the answers since the level last changed, so one run is not
* counted twice.
*/
export function nudgeOf(firstAnswers: readonly ("right" | "wrong" | "skipped")[]): -1 | 0 | 1 {
const answers = firstAnswers.filter((o) => o !== "skipped");
const recent = answers.slice(-NUDGE_DOWN.within);
if (recent.filter((o) => o === "wrong").length >= NUDGE_DOWN.misses) return -1;
const window = answers.slice(-NUDGE_BAND.window);
const rate = window.filter((o) => o === "right").length / Math.max(1, window.length);
if (window.length >= NUDGE_BAND.downAfter && rate < NUDGE_BAND.down) return -1;
if (window.length >= NUDGE_UP_AFTER && rate >= NUDGE_BAND.up && window.at(-1) === "right") return 1;
return 0;
}