aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-02-02 12:22:03 +0000
committerClaude <noreply@anthropic.com>2026-02-02 12:22:03 +0000
commitd4489f24a05911d1395e8473fe86c3442d9397ee (patch)
tree3f302c9c531c7ba25e2c5ad74c44b086df03a0d7
parentc20922a1aa339e34b4bed3747222f4b9d9941cd6 (diff)
downloadkioku-d4489f24a05911d1395e8473fe86c3442d9397ee.tar.gz
kioku-d4489f24a05911d1395e8473fe86c3442d9397ee.tar.zst
kioku-d4489f24a05911d1395e8473fe86c3442d9397ee.zip
fix(study): use date-based comparison with 3 AM boundary for due cards
Instead of comparing due timestamps exactly (card.due <= now), compare against the next 3 AM boundary so all cards due within the current study day appear at once. This prevents new cards from trickling in throughout the day when FSRS fuzz spreads due times. https://claude.ai/code/session_01FeDztLcyGofd6nxh8ct7a3
-rw-r--r--src/client/db/repositories.test.ts3
-rw-r--r--src/client/db/repositories.ts5
-rw-r--r--src/client/pages/DeckDetailPage.tsx7
-rw-r--r--src/server/repositories/card.ts9
-rw-r--r--src/shared/date.test.ts58
-rw-r--r--src/shared/date.ts21
6 files changed, 94 insertions, 9 deletions
diff --git a/src/client/db/repositories.test.ts b/src/client/db/repositories.test.ts
index 448cb9e..38cf3fb 100644
--- a/src/client/db/repositories.test.ts
+++ b/src/client/db/repositories.test.ts
@@ -299,7 +299,8 @@ describe("localCardRepository", () => {
describe("findDueCards", () => {
it("should return cards that are due", async () => {
const pastDue = new Date(Date.now() - 60000);
- const future = new Date(Date.now() + 60000);
+ // Use a date far enough in the future to be beyond the next 3 AM boundary
+ const future = new Date(Date.now() + 2 * 24 * 60 * 60 * 1000);
const card1 = await localCardRepository.create({
deckId,
diff --git a/src/client/db/repositories.ts b/src/client/db/repositories.ts
index e01254e..5121bae 100644
--- a/src/client/db/repositories.ts
+++ b/src/client/db/repositories.ts
@@ -1,4 +1,5 @@
import { v4 as uuidv4 } from "uuid";
+import { getEndOfStudyDayBoundary } from "../../shared/date";
import {
CardState,
db,
@@ -140,11 +141,11 @@ export const localCardRepository = {
* Get due cards for a deck
*/
async findDueCards(deckId: string, limit?: number): Promise<LocalCard[]> {
- const now = new Date();
+ const boundary = getEndOfStudyDayBoundary();
const query = db.cards
.where("deckId")
.equals(deckId)
- .filter((card) => card.deletedAt === null && card.due <= now);
+ .filter((card) => card.deletedAt === null && card.due < boundary);
const cards = await query.toArray();
// Sort by due date ascending
diff --git a/src/client/pages/DeckDetailPage.tsx b/src/client/pages/DeckDetailPage.tsx
index be4dc90..97f8378 100644
--- a/src/client/pages/DeckDetailPage.tsx
+++ b/src/client/pages/DeckDetailPage.tsx
@@ -7,6 +7,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useAtomValue } from "jotai";
import { Suspense } from "react";
import { Link, useParams } from "wouter";
+import { getEndOfStudyDayBoundary } from "../../shared/date";
import { cardsByDeckAtomFamily, deckByIdAtomFamily } from "../atoms";
import { ErrorBoundary } from "../components/ErrorBoundary";
import { LoadingSpinner } from "../components/LoadingSpinner";
@@ -27,9 +28,9 @@ function DeckHeader({ deckId }: { deckId: string }) {
function DeckStats({ deckId }: { deckId: string }) {
const { data: cards } = useAtomValue(cardsByDeckAtomFamily(deckId));
- // Count cards due today
- const now = new Date();
- const dueCards = cards.filter((card) => new Date(card.due) <= now);
+ // Count cards due today (study day boundary is 3:00 AM)
+ const boundary = getEndOfStudyDayBoundary();
+ const dueCards = cards.filter((card) => new Date(card.due) < boundary);
return (
<div className="bg-white rounded-xl border border-border/50 p-6 mb-6">
diff --git a/src/server/repositories/card.ts b/src/server/repositories/card.ts
index ac03bc6..2f14149 100644
--- a/src/server/repositories/card.ts
+++ b/src/server/repositories/card.ts
@@ -1,4 +1,5 @@
-import { and, eq, isNull, lte, sql } from "drizzle-orm";
+import { and, eq, isNull, lt, sql } from "drizzle-orm";
+import { getEndOfStudyDayBoundary } from "../../shared/date.js";
import { db } from "../db/index.js";
import {
CardState,
@@ -189,6 +190,7 @@ export const cardRepository: CardRepository = {
now: Date,
limit: number,
): Promise<Card[]> {
+ const boundary = getEndOfStudyDayBoundary(now);
const result = await db
.select()
.from(cards)
@@ -196,7 +198,7 @@ export const cardRepository: CardRepository = {
and(
eq(cards.deckId, deckId),
isNull(cards.deletedAt),
- lte(cards.due, now),
+ lt(cards.due, boundary),
),
)
.orderBy(cards.due)
@@ -205,6 +207,7 @@ export const cardRepository: CardRepository = {
},
async countDueCards(deckId: string, now: Date): Promise<number> {
+ const boundary = getEndOfStudyDayBoundary(now);
const result = await db
.select({ count: sql<number>`count(*)::int` })
.from(cards)
@@ -212,7 +215,7 @@ export const cardRepository: CardRepository = {
and(
eq(cards.deckId, deckId),
isNull(cards.deletedAt),
- lte(cards.due, now),
+ lt(cards.due, boundary),
),
);
return result[0]?.count ?? 0;
diff --git a/src/shared/date.test.ts b/src/shared/date.test.ts
new file mode 100644
index 0000000..1c61a15
--- /dev/null
+++ b/src/shared/date.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, it } from "vitest";
+import { getEndOfStudyDayBoundary } from "./date";
+
+describe("getEndOfStudyDayBoundary", () => {
+ it("should return next day 3:00 AM when current time is after 3:00 AM", () => {
+ // Feb 2, 2026 10:00 AM
+ const now = new Date(2026, 1, 2, 10, 0, 0, 0);
+ const boundary = getEndOfStudyDayBoundary(now);
+
+ expect(boundary.getFullYear()).toBe(2026);
+ expect(boundary.getMonth()).toBe(1);
+ expect(boundary.getDate()).toBe(3);
+ expect(boundary.getHours()).toBe(3);
+ expect(boundary.getMinutes()).toBe(0);
+ expect(boundary.getSeconds()).toBe(0);
+ });
+
+ it("should return today 3:00 AM when current time is before 3:00 AM", () => {
+ // Feb 2, 2026 1:30 AM
+ const now = new Date(2026, 1, 2, 1, 30, 0, 0);
+ const boundary = getEndOfStudyDayBoundary(now);
+
+ expect(boundary.getFullYear()).toBe(2026);
+ expect(boundary.getMonth()).toBe(1);
+ expect(boundary.getDate()).toBe(2);
+ expect(boundary.getHours()).toBe(3);
+ expect(boundary.getMinutes()).toBe(0);
+ expect(boundary.getSeconds()).toBe(0);
+ });
+
+ it("should return next day 3:00 AM when current time is exactly 3:00 AM", () => {
+ // Feb 2, 2026 3:00 AM
+ const now = new Date(2026, 1, 2, 3, 0, 0, 0);
+ const boundary = getEndOfStudyDayBoundary(now);
+
+ expect(boundary.getDate()).toBe(3);
+ expect(boundary.getHours()).toBe(3);
+ });
+
+ it("should return next day 3:00 AM when current time is 11:59 PM", () => {
+ // Feb 2, 2026 11:59 PM
+ const now = new Date(2026, 1, 2, 23, 59, 0, 0);
+ const boundary = getEndOfStudyDayBoundary(now);
+
+ expect(boundary.getDate()).toBe(3);
+ expect(boundary.getHours()).toBe(3);
+ });
+
+ it("should handle month boundaries correctly", () => {
+ // Jan 31, 2026 15:00
+ const now = new Date(2026, 0, 31, 15, 0, 0, 0);
+ const boundary = getEndOfStudyDayBoundary(now);
+
+ expect(boundary.getMonth()).toBe(1); // February
+ expect(boundary.getDate()).toBe(1);
+ expect(boundary.getHours()).toBe(3);
+ });
+});
diff --git a/src/shared/date.ts b/src/shared/date.ts
new file mode 100644
index 0000000..583d2a6
--- /dev/null
+++ b/src/shared/date.ts
@@ -0,0 +1,21 @@
+/**
+ * Returns the end-of-day boundary for due card comparison.
+ *
+ * The "study day" is defined as 3:00 AM to the next day's 3:00 AM.
+ * All cards with `due < boundary` are considered due for the current study day.
+ *
+ * - If current time >= 3:00 AM, boundary = tomorrow 3:00 AM local time
+ * - If current time < 3:00 AM, boundary = today 3:00 AM local time
+ */
+export function getEndOfStudyDayBoundary(now: Date = new Date()): Date {
+ const boundary = new Date(now);
+ boundary.setMinutes(0, 0, 0);
+
+ if (boundary.getHours() >= 3) {
+ // Move to next day
+ boundary.setDate(boundary.getDate() + 1);
+ }
+
+ boundary.setHours(3);
+ return boundary;
+}