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

166 lines
7.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { REVIEW_STEPS } from "../saved/review.ts";
import {
NO_MEMORY,
combineMemories,
comingBack,
decodeMemory,
dueCount,
MAX_INTERVAL_DAYS,
newShareFor,
REVIEWS_PER_DAY,
reviewedToday,
scheduleAnswer,
stateOf,
todayCount,
urgency,
type Memory,
} from "./memory.ts";
const DAY = 86_400_000;
const now = new Date("2026-10-01T08:00:00Z");
const later = (days: number) => new Date(now.getTime() + days * DAY);
const q = { id: "transform:annas-sister", kind: "transform" } as const;
const gap = (memory: Memory, from: Date) => Math.round((memory[q.id]!.due - from.getTime()) / DAY);
test("each right answer on time stretches the next interval, a miss shortens it", () => {
// FSRS-5's first stabilities: a first right answer comes back in 3 days
// (w2 = 3.173), a first miss in 1 (w0 = 0.40, at least a day).
let memory: Memory = scheduleAnswer(NO_MEMORY, q, "right", now);
assert.equal(gap(memory, now), 3);
assert.equal(gap(scheduleAnswer(NO_MEMORY, q, "wrong", now), now), 1);
const intervals = [3];
let at = now;
for (let i = 0; i < 4; i++) {
at = new Date(memory[q.id]!.due);
memory = scheduleAnswer(memory, q, "right", at);
intervals.push(gap(memory, at));
}
for (let i = 1; i < intervals.length; i++) assert.ok(intervals[i]! > intervals[i - 1]!, `intervals ${intervals.join(", ")}`);
assert.equal(memory[q.id]!.step, REVIEW_STEPS.length - 1);
const before = gap(memory, at);
at = new Date(memory[q.id]!.due);
memory = scheduleAnswer(memory, q, "wrong", at);
assert.equal(memory[q.id]!.step, 0);
assert.equal(memory[q.id]!.lapses, 1);
assert.ok(gap(memory, at) < before);
});
test("a right answer typed comes back later than one chosen", () => {
const chosen = scheduleAnswer(NO_MEMORY, q, "right", now);
const typed = scheduleAnswer(NO_MEMORY, q, "right", now, { typed: true });
assert.ok(gap(typed, now) > gap(chosen, now));
});
test("it keeps coming back instead of vanishing, at most half a year apart", () => {
let memory: Memory = NO_MEMORY;
for (let i = 0; i < 20; i++) memory = scheduleAnswer(memory, q, "right", new Date(memory[q.id]?.due ?? now.getTime()));
const card = memory[q.id]!;
assert.ok(card.due - card.seen <= MAX_INTERVAL_DAYS * DAY);
});
test("a card from the old ladder keeps its day, and goes on from it", () => {
// Step 2 of 1, 3, 7, 16, 35: answered on day 0, due on day 7.
const old: Memory = { [q.id]: { step: 2, due: later(7).getTime(), seen: now.getTime(), lapses: 0 } };
assert.equal(urgency(old, q.id, later(6)), 2);
assert.equal(urgency(old, q.id, later(7)), 0);
assert.equal(stateOf(old[q.id]!).s, 7);
const next = scheduleAnswer(old, q, "right", later(7));
assert.ok(gap(next, later(7)) > 7, "a right answer on time stretches the old interval");
assert.equal(next[q.id]!.n, 2);
});
test("today asks at most a day's cap, less what was reviewed today", () => {
let memory: Memory = NO_MEMORY;
for (let i = 0; i < 40; i++) memory = scheduleAnswer(memory, { id: `transform:${i}`, kind: "transform" }, "wrong", later(-2));
assert.equal(dueCount(memory, now), 40);
assert.equal(todayCount(memory, now), REVIEWS_PER_DAY);
const morning = later(-0.1);
for (let i = 0; i < 10; i++) memory = scheduleAnswer(memory, { id: `transform:${i}`, kind: "transform" }, "right", morning);
assert.equal(dueCount(memory, now), 30);
assert.equal(todayCount(memory, now), Math.min(30, REVIEWS_PER_DAY - reviewedToday(memory, now)));
});
test("new questions give way while the pile is large", () => {
assert.equal(newShareFor(0, 0.5), 0.5);
assert.equal(newShareFor(REVIEWS_PER_DAY, 0.5), 0.5);
assert.ok(newShareFor(1.5 * REVIEWS_PER_DAY, 0.5) < 0.5);
assert.equal(newShareFor(2 * REVIEWS_PER_DAY, 0.5), 0);
});
test("a skip and a kept word change nothing", () => {
assert.equal(scheduleAnswer(NO_MEMORY, q, "skipped", now), NO_MEMORY);
assert.equal(scheduleAnswer(NO_MEMORY, { id: "recall:gäll", kind: "recall" }, "right", now), NO_MEMORY);
});
test("due, new and resting", () => {
const memory = scheduleAnswer(NO_MEMORY, q, "right", now);
assert.equal(urgency(memory, "never:asked", now), 1);
assert.equal(urgency(memory, q.id, now), 2);
assert.equal(urgency(memory, q.id, later(3)), 0);
assert.equal(dueCount(memory, now), 0);
assert.equal(dueCount(memory, later(3)), 1);
assert.equal(dueCount(memory, later(3), ["other"]), 0);
assert.equal(comingBack(memory, now, 1), 0);
assert.equal(comingBack(memory, now, 3), 1);
});
test("across devices, the newest answer of each question wins", () => {
const phone = scheduleAnswer(NO_MEMORY, q, "right", now);
const laptop = scheduleAnswer(scheduleAnswer(NO_MEMORY, q, "right", later(-2)), q, "wrong", later(1));
const both = combineMemories([phone, laptop]);
assert.equal(both[q.id]!.lapses, 1);
assert.deepEqual(combineMemories([laptop, phone]), both);
});
test("the decoder keeps good cards and refuses what is not a memory", () => {
assert.equal(decodeMemory("[]"), null);
assert.equal(decodeMemory("nope"), null);
const decoded = decodeMemory(
JSON.stringify({ ok: { step: 99, due: 5, seen: 4, lapses: 0 }, bad: { step: "1" }, neg: { step: 0, due: -1, seen: 0, lapses: 0 } }),
);
assert.deepEqual(Object.keys(decoded!), ["ok"]);
assert.equal(decoded!.ok!.step, REVIEW_STEPS.length - 1);
assert.equal(decoded!.ok!.s, undefined);
// The memory state travels: what one device wrote, the other reads.
const card = scheduleAnswer(NO_MEMORY, q, "right", now)[q.id]!;
assert.deepEqual(decodeMemory(JSON.stringify({ [q.id]: card }))![q.id], card);
// Half a state is no state: read like an old card.
const half = decodeMemory(JSON.stringify({ [q.id]: { ...card, d: 99 } }))![q.id]!;
assert.equal(half.s, undefined);
assert.equal(half.d, undefined);
});
test("a sitting shares its seats between what is due and what is new, and leaves resting questions alone", async () => {
const { PACK_ITEMS } = await import("./published.ts");
const { orderSession } = await import("./session.ts");
const translate = PACK_ITEMS.filter((i) => i.kind === "translate").slice(0, 10);
const resting = PACK_ITEMS.filter((i) => i.kind === "cloze").slice(0, 30);
let memory: Memory = NO_MEMORY;
for (const item of translate) memory = scheduleAnswer(memory, item, "right", later(-3));
for (const item of resting) memory = scheduleAnswer(memory, item, "right", now);
const { planSession } = await import("./session.ts");
const planned = planSession({ items: PACK_ITEMS, saved: [], now, memory, size: 8 });
const session = planned.items;
// Ten due questions of ONE kind take every review seat, not one of them —
// and the review seats are the sitting less its share of new questions.
// Counted by the card that was due: a review may sit in another form.
const { NEW_SHARE } = await import("./session.ts");
const dueCards = planned.due.map((id) => planned.reviewOf[id] ?? id);
assert.equal(dueCards.filter((id) => translate.some((t) => t.id === id)).length, 8 - Math.round(8 * NEW_SHARE));
assert.equal(session.length, 8);
// A day and a half earlier nothing is due yet: every answered question is
// resting, and new ones fill the sitting.
const noDue = orderSession({ items: PACK_ITEMS, saved: [], now: later(-1.5), memory, size: 8 });
const answered = new Set(Object.keys(memory));
assert.ok(!noDue.some((i) => answered.has(i.id)), "a resting question waits while new ones remain");
});