Codebase audit: fix bugs, performance issues, dead code, and edge cases

1. Bugs fixed:
- Backup schema now accepts theme: "aero" so backups exported under
  Frutiger Aero pass parseBackup on import, and applyImport in "replace"
  mode replaces SRS schedules, answer history, and chats instead of
  merging into existing SRS or skipping empty arrays.
- Typed FillBlank cards bind the input value to chosen ?? value instead
  of chosen ?? "", and normalise() preserves Unicode letters (\p{L}\p{N})
  when grading non-ASCII answers.
- OpenAI-compatible vision calls (contentFor in lib/openai-compat.ts)
  prefix data:image/jpeg;base64, when routes pass stripped base64.
- Streaming calls (ollamaChatStream, providerChatStream) keep their
  AbortController active across the entire body stream and API routes
  forward request.signal so stopping a chat or cancelling a run aborts
  the upstream request immediately.
- HomeworkCard uses homeworkModeLabelKey so "check" records display the
  Checked label instead of Step-by-step guide.
- Desktop dropzones on /create and /homework accept dropped documents as
  well as images.
- LibraryScreen filters reported questions out of its due and mistake
  counts to match ReviewClient.
- Clearing a provider key resets generation if it pointed at that
  provider; chat conversations persist and restore provider; ModelPicker
  keys items by provider:name.
- RouteTransition no longer mutates module-level lastPath during render.
- Service worker asset handler falls back to Response.error() when an
  uncached asset fetch fails offline.
- Confetti and .ice-orb / .ice-bubble respect the in-app reduce-motion
  toggle.

2. Performance & cleanup:
- Deduplicate window storage listeners across all six localStorage
  stores (one listener while subscribed instead of N^2 notifications).
- Use todayAnswerCount in LibraryScreen and pass sorted folders to
  DeckCard.
- Memoize MessageRow with stable callbacks in ChatScreen and compute
  last message IDs in a single pass.
- Close ImageBitmap after drawing in lib/image.ts and destroy pdf.js
  documents in finally in lib/documents.ts.
- Avoid re-parsing serialized sync payloads in POST /api/sync.
- Deduplicate copyText (lib/share.ts -> lib/clipboard.ts), chatMessageId,
  describeExtraction, ALL_FOLDERS, useIsPhone, and remove unused helpers
  and boilerplate public/*.svg files.

Version 0.5.8, SW -> freezmo-v80.
This commit is contained in:
Martin Dvořák 2026-10-14 19:00:17 +00:00
commit 982391ca45
57 changed files with 714 additions and 497 deletions

View file

@ -87,6 +87,9 @@ export async function POST(request: Request) {
if ("response" in gate) return gate.response;
const abort = new AbortController();
const timer = setTimeout(() => abort.abort(), config.timeoutMs);
const onRequestAbort = () => abort.abort();
if (request.signal.aborted) abort.abort();
else request.signal.addEventListener("abort", onRequestAbort, { once: true });
try {
if (images.length === 0) {
// Plain text needs no model call at all.
@ -117,6 +120,7 @@ export async function POST(request: Request) {
);
} finally {
clearTimeout(timer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
}
}

View file

@ -158,6 +158,9 @@ export async function POST(request: Request) {
const encode = encoder();
const abort = new AbortController();
const globalTimer = setTimeout(() => abort.abort(), config.timeoutMs);
const onRequestAbort = () => abort.abort();
if (request.signal.aborted) abort.abort();
else request.signal.addEventListener("abort", onRequestAbort, { once: true });
const stream = new ReadableStream<Uint8Array>({
async start(controller) {
@ -208,6 +211,7 @@ export async function POST(request: Request) {
}
} finally {
clearTimeout(globalTimer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
try {
controller.close();
@ -218,6 +222,7 @@ export async function POST(request: Request) {
},
cancel() {
clearTimeout(globalTimer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
abort.abort();
},

View file

@ -131,6 +131,9 @@ export async function POST(request: Request) {
const abort = new AbortController();
// A single hard deadline for the whole pipeline (two sequential model calls).
const globalTimer = setTimeout(() => abort.abort(), config.timeoutMs * 2);
const onRequestAbort = () => abort.abort();
if (request.signal.aborted) abort.abort();
else request.signal.addEventListener("abort", onRequestAbort, { once: true });
// Heartbeats: one quiet model call can run for minutes, and the client's
// stall guard needs to tell "slow" from "dead" — a silent stream is how the
// wizard ended up loading forever. Cleared with the stream either way.
@ -234,6 +237,7 @@ export async function POST(request: Request) {
} finally {
if (heartbeat) clearInterval(heartbeat);
clearTimeout(globalTimer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
try {
controller.close();
@ -245,6 +249,7 @@ export async function POST(request: Request) {
cancel() {
if (heartbeat) clearInterval(heartbeat);
clearTimeout(globalTimer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
abort.abort();
},

View file

@ -81,6 +81,11 @@ export async function POST(request: Request) {
const gate = acquireModelGate(request, config, locale);
if ("response" in gate) return gate.response;
const abort = new AbortController();
const timer = setTimeout(() => abort.abort(), Math.min(config.timeoutMs, 180_000));
const onRequestAbort = () => abort.abort();
if (request.signal.aborted) abort.abort();
else request.signal.addEventListener("abort", onRequestAbort, { once: true });
try {
const text = await refineHomeworkText({
model: textModel,
@ -89,6 +94,7 @@ export async function POST(request: Request) {
instruction: parsed.data.instruction,
text: parsed.data.text,
context: parsed.data.context,
signal: abort.signal,
});
return Response.json(
{ text, model: provider ? textModel : resolveModelName(config.textModel, baseAuth.directCloud) },
@ -105,6 +111,8 @@ export async function POST(request: Request) {
{ status: 502 },
);
} finally {
clearTimeout(timer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
}
}

View file

@ -124,6 +124,9 @@ export async function POST(request: Request) {
const abort = new AbortController();
// A single hard deadline for the whole pipeline (two sequential model calls).
const globalTimer = setTimeout(() => abort.abort(), config.timeoutMs * 2);
const onRequestAbort = () => abort.abort();
if (request.signal.aborted) abort.abort();
else request.signal.addEventListener("abort", onRequestAbort, { once: true });
// Heartbeats so the client's stall guard can tell "slow" from "dead".
let heartbeat: ReturnType<typeof setInterval> | null = null;
@ -291,6 +294,7 @@ export async function POST(request: Request) {
} finally {
if (heartbeat) clearInterval(heartbeat);
clearTimeout(globalTimer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
try {
controller.close();
@ -302,6 +306,7 @@ export async function POST(request: Request) {
cancel() {
if (heartbeat) clearInterval(heartbeat);
clearTimeout(globalTimer);
request.signal.removeEventListener("abort", onRequestAbort);
gate.gate.release();
abort.abort();
},

View file

@ -144,7 +144,8 @@ export async function POST(request: Request) {
}
const file = fileFor(config.sync.dir, parsed.data.id);
const serialised = JSON.stringify({ updatedAt: Date.now(), envelope: parsed.data.envelope });
const updatedAt = Date.now();
const serialised = JSON.stringify({ updatedAt, envelope: parsed.data.envelope });
// Total storage cap, checked only when a new blob is created.
const exists = await fs
@ -162,7 +163,6 @@ export async function POST(request: Request) {
}
await writeAtomic(file, serialised);
const updatedAt = (JSON.parse(serialised) as { updatedAt: number }).updatedAt;
return Response.json({ updatedAt }, { headers: { "Cache-Control": "no-store" } });
}

View file

@ -1597,10 +1597,6 @@ body:has(.overlay-open) :is(.app-header, .segmented, .tabbar, .cookie-notice) {
.fade-in-mobile { animation: none; }
}
@media (display-mode: standalone) {
.standalone-hide { display: none }
}
/* Phones: Apple's 44px minimum for touch targets. The class is a no-op on
desktop (and on tablets), so components can carry it unconditionally. */
@media (max-width: 639px) {
@ -1657,7 +1653,17 @@ html.reduce-motion .ice-blob,
html.reduce-motion .ice-orb,
html.reduce-motion .ice-bubble {
animation: none !important;
will-change: auto;
}
html.reduce-motion.aero .ice-bubble {
opacity: 0.55;
transform: translate3d(0, -60vh, 0);
}
html.reduce-motion .ice-orb-1 { top: 14%; left: 12%; }
html.reduce-motion .ice-orb-2 { top: 58%; left: 4%; }
html.reduce-motion .ice-orb-3 { top: auto; bottom: -70px; right: 10%; }
html.reduce-motion .ice-orb-4 { top: 24%; right: 28%; }
html.reduce-motion .ice-orb-5 { top: auto; bottom: 16%; left: 38%; }
html.reduce-motion *,
html.reduce-motion *::before,
html.reduce-motion *::after {