Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-1991]): clamp SRS ease to schema max and cap interval at one year

[._.]/ Adam Eivydc59d9698d92
diff --git a/server/services/meatspacePostMemory.js b/server/services/meatspacePostMemory.js
index 46ca1e4f0c..7b0523d5cc 100644
--- a/server/services/meatspacePostMemory.js
+++ b/server/services/meatspacePostMemory.js
@@ -27,6 +27,16 @@ const TRAINING_LOG_FILE = join(MEATSPACE_DIR, 'post-training-log.json');
export const DEFAULT_EASE = 2.5;
const MIN_EASE = 1.3;
+// Ceiling mirrors `memoryScheduleSchema.ease.max(5)` in postValidation.js — the
+// per-session +0.1 bumps are unbounded otherwise, so ~26 perfect reps would push
+// ease past 5 and a later round-trip through POST/PUT (import / out-of-band
+// reschedule) would 400 on the server's own value. Keep the two in sync.
+const MAX_EASE = 5;
+// Cap the interval at a year so a long run of perfect reviews can't grow it
+// without bound — an astronomically large `intervalDays` would overflow
+// `new Date(now + intervalDays*DAY_MS)` into an Invalid Date and throw. A yearly
+// review floor is a conventional SRS ceiling and keeps items resurfacing.
+const MAX_INTERVAL_DAYS = 365;
const DAY_MS = 24 * 60 * 60 * 1000;
/** Fresh schedule — due at `nowIso` (i.e. immediately). */
@@ -63,7 +73,7 @@ export function advanceSchedule(schedule, ratio, now = new Date()) {
const prevEase = typeof prev.ease === 'number' ? prev.ease : DEFAULT_EASE;
let ease = prevEase + (0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02));
- ease = Math.max(MIN_EASE, Math.round(ease * 100) / 100);
+ ease = Math.min(MAX_EASE, Math.max(MIN_EASE, Math.round(ease * 100) / 100));
let intervalDays;
if (quality < 3) {
@@ -73,12 +83,39 @@ export function advanceSchedule(schedule, ratio, now = new Date()) {
if (prevInterval <= 0) intervalDays = 1;
else if (prevInterval < 6) intervalDays = 6;
else intervalDays = Math.max(1, Math.round(prevInterval * ease));
+ intervalDays = Math.min(MAX_INTERVAL_DAYS, intervalDays);
}
const nextReview = new Date(now.getTime() + intervalDays * DAY_MS).toISOString();
return { ease, intervalDays, nextReview, lastReviewed: nowIso };
}
+/**
+ * Merge a freshly-advanced schedule against the item's prior schedule, gating
+ * interval GROWTH to once per review day.
+ *
+ * Why: a "review" of a memory item is one pass through it, but the existing
+ * spaced-practice flow (`MemoryPractice.advanceSpaced`) submits once PER CHUNK.
+ * Advancing on every submission would compound a multi-chunk item's interval in
+ * a single sitting (0→1→6→16d) and drop it off the due list far longer than one
+ * completed review warrants. So a same-day continuation only refreshes ease and
+ * `lastReviewed` — it keeps the interval/nextReview already set earlier today.
+ * A miss (interval shrinks to 0) always applies, so a fumbled chunk still
+ * resurfaces the item immediately regardless of earlier same-day success.
+ */
+export function mergeScheduleAdvance(prev, advanced, now = new Date()) {
+ const prevInterval = typeof prev?.intervalDays === 'number' ? prev.intervalDays : 0;
+ const lastReviewedMs = Date.parse(prev?.lastReviewed ?? '');
+ const sameReviewDay = Number.isFinite(lastReviewedMs)
+ && new Date(lastReviewedMs).toISOString().slice(0, 10) === now.toISOString().slice(0, 10);
+ // Suppress compounding only when this is a same-day continuation AND the
+ // interval would grow. Any shrink (a miss reset) always applies.
+ if (sameReviewDay && advanced.intervalDays > prevInterval) {
+ return { ...prev, ease: advanced.ease, lastReviewed: advanced.lastReviewed };
+ }
+ return advanced;
+}
+
/** True when an item is due for review (no schedule / invalid date = due). */
export function isMemoryItemDue(item, now = new Date()) {
const nr = item?.schedule?.nextReview;
@@ -391,10 +428,16 @@ export async function submitPractice(id, practiceData) {
// Recompute overall mastery percentage
item.mastery.overallPct = computeOverallMastery(item);
- // Advance the spaced-repetition schedule from this session's accuracy.
+ // Advance the spaced-repetition schedule from this session's accuracy. The
+ // schedule is per-ITEM (mastery is per-chunk); practicing any part of an item
+ // counts as reviewing it, so it resurfaces on the next review day rather than
+ // staying due today. `mergeScheduleAdvance` gates interval growth to once per
+ // day so the per-chunk submits of a spaced session don't compound the interval,
+ // while a miss anywhere still resets the item to due-now.
const correctCount = results.filter(r => r.correct).length;
const ratio = results.length ? correctCount / results.length : 0;
- item.schedule = advanceSchedule(item.schedule, ratio, new Date(now));
+ const advanced = advanceSchedule(item.schedule, ratio, new Date(now));
+ item.schedule = mergeScheduleAdvance(item.schedule, advanced, new Date(now));
item.updatedAt = now;
await saveMemoryItems(items);
diff --git a/server/services/meatspacePostMemory.test.js b/server/services/meatspacePostMemory.test.js
index 37bb468fc9..2ffa70ee87 100644
--- a/server/services/meatspacePostMemory.test.js
+++ b/server/services/meatspacePostMemory.test.js
@@ -21,6 +21,7 @@ import {
generateMemoryDrill,
getDueMemoryItems,
advanceSchedule,
+ mergeScheduleAdvance,
isMemoryItemDue,
defaultSchedule,
DEFAULT_EASE,
@@ -308,6 +309,12 @@ describe('advanceSchedule', () => {
expect(schedule.ease).toBe(1.3);
});
+ it('never lifts ease above the schema ceiling of 5 across many perfect reps', () => {
+ let schedule = { ease: 4.9, intervalDays: 6, nextReview: now.toISOString() };
+ for (let i = 0; i < 40; i++) schedule = advanceSchedule(schedule, 1, now);
+ expect(schedule.ease).toBeLessThanOrEqual(5); // matches memoryScheduleSchema.ease.max(5)
+ });
+
it('tolerates a missing/garbage prior schedule', () => {
const next = advanceSchedule(undefined, 1, now);
expect(next.intervalDays).toBe(1);
@@ -315,6 +322,35 @@ describe('advanceSchedule', () => {
});
});
+describe('mergeScheduleAdvance', () => {
+ const now = new Date('2026-07-01T12:00:00.000Z');
+
+ it('applies a first-of-day advance (no prior same-day review)', () => {
+ const prev = { ease: 2.5, intervalDays: 0, nextReview: '2026-06-30T00:00:00.000Z', lastReviewed: '2026-06-30T00:00:00.000Z' };
+ const advanced = advanceSchedule(prev, 1, now);
+ const merged = mergeScheduleAdvance(prev, advanced, now);
+ expect(merged.intervalDays).toBe(1); // yesterday's review → today advances once
+ });
+
+ it('does not compound the interval on a same-day continuation (per-chunk submits)', () => {
+ // Chunk 1 already advanced the item today: interval 1, reviewed today.
+ const afterChunk1 = { ease: 2.6, intervalDays: 1, nextReview: '2026-07-02T12:00:00.000Z', lastReviewed: '2026-07-01T09:00:00.000Z' };
+ const advanced = advanceSchedule(afterChunk1, 1, now); // would step 1 → 6
+ const merged = mergeScheduleAdvance(afterChunk1, advanced, now);
+ expect(merged.intervalDays).toBe(1); // interval held, not compounded to 6
+ expect(merged.nextReview).toBe(afterChunk1.nextReview); // still tomorrow
+ expect(merged.lastReviewed).toBe(advanced.lastReviewed); // ease/lastReviewed refreshed
+ });
+
+ it('still resets to due-now on a same-day miss after earlier success', () => {
+ const afterChunk1 = { ease: 2.6, intervalDays: 1, nextReview: '2026-07-02T12:00:00.000Z', lastReviewed: '2026-07-01T09:00:00.000Z' };
+ const advanced = advanceSchedule(afterChunk1, 0, now); // miss → interval 0
+ const merged = mergeScheduleAdvance(afterChunk1, advanced, now);
+ expect(merged.intervalDays).toBe(0); // shrink always applies
+ expect(Date.parse(merged.nextReview)).toBe(now.getTime()); // due now again
+ });
+});
+
describe('isMemoryItemDue', () => {
const now = new Date('2026-07-01T00:00:00.000Z');