Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
92 lines
4.0 KiB
TypeScript
92 lines
4.0 KiB
TypeScript
/**
|
||
* BUG-2175 (left over from BUG-2138): every single-chapter read (`view=chapter`) loaded the whole
|
||
* stored text (≈3 MB for a Chinese report, from the appendix row or the English snapshot), projected
|
||
* it and cut it, to send one chapter. The chapters of one edition are now kept in this process
|
||
* after the first read, keyed by account, report and language, and checked against the stored
|
||
* text's version on every read (the appendix row's content hash; the snapshot row's status and
|
||
* updated_at for English), which costs one small row instead of the whole text.
|
||
*
|
||
* Bounded for the 2 vCPU / 4 GB host: at most CHAPTER_CACHE_MAX_ENTRIES editions and
|
||
* CHAPTER_CACHE_MAX_CHARS characters in all (a JS string is at most two bytes a character, so the
|
||
* text held is under 2 × that in bytes); the least recently read edition goes first, and an edition
|
||
* larger than the whole budget is never kept. Sign-in, the origin check and ownership stay in
|
||
* resolveReportRead, which runs before this is asked; the cache never answers for a report the
|
||
* caller has not been checked against.
|
||
*
|
||
* No server-only imports: tests drive it directly.
|
||
*/
|
||
import { buildLongformOutline } from "./personal-report-longform-outline.ts";
|
||
import { dataChapters, readerMarkdown } from "./personal-report-outline-view.ts";
|
||
import type { ReportLanguage } from "./report-language.ts";
|
||
|
||
export const CHAPTER_CACHE_MAX_ENTRIES = 8;
|
||
export const CHAPTER_CACHE_MAX_CHARS = 12_000_000;
|
||
|
||
export type CachedChapter = Readonly<{ id: string; title: string; markdown: string }>;
|
||
|
||
type Entry = Readonly<{ version: string; chapters: readonly CachedChapter[]; chars: number }>;
|
||
|
||
/** The data chapters of a stored text, cut exactly as `chapterReportBody` cuts the full read's body. */
|
||
export function storedTextChapters(stored: string, language: ReportLanguage): CachedChapter[] {
|
||
const text = readerMarkdown(stored, language);
|
||
if (!text) return [];
|
||
return dataChapters(buildLongformOutline(text)).map((chapter) => ({ id: chapter.id, title: chapter.title, markdown: chapter.markdown }));
|
||
}
|
||
|
||
export function createChapterCache(limits: Readonly<{ maxEntries: number; maxChars: number }> = {
|
||
maxEntries: CHAPTER_CACHE_MAX_ENTRIES,
|
||
maxChars: CHAPTER_CACHE_MAX_CHARS,
|
||
}) {
|
||
// A Map keeps insertion order: the first key is the least recently read.
|
||
const entries = new Map<string, Entry>();
|
||
let chars = 0;
|
||
const drop = (key: string) => {
|
||
const entry = entries.get(key);
|
||
if (!entry) return;
|
||
entries.delete(key);
|
||
chars -= entry.chars;
|
||
};
|
||
return {
|
||
/** The chapters stored under `key` when they were cut from text of this `version`. */
|
||
get(key: string, version: string): readonly CachedChapter[] | null {
|
||
const entry = entries.get(key);
|
||
if (!entry) return null;
|
||
if (entry.version !== version) {
|
||
drop(key);
|
||
return null;
|
||
}
|
||
entries.delete(key);
|
||
entries.set(key, entry);
|
||
return entry.chapters;
|
||
},
|
||
set(key: string, version: string, chapters: readonly CachedChapter[]) {
|
||
drop(key);
|
||
const size = chapters.reduce((total, chapter) => total + chapter.markdown.length + chapter.title.length + chapter.id.length, 0);
|
||
if (size > limits.maxChars || limits.maxEntries < 1) return;
|
||
while (entries.size >= limits.maxEntries || chars + size > limits.maxChars) {
|
||
const oldest = entries.keys().next().value;
|
||
if (oldest === undefined) break;
|
||
drop(oldest);
|
||
}
|
||
entries.set(key, { version, chapters, chars: size });
|
||
chars += size;
|
||
},
|
||
clear() {
|
||
entries.clear();
|
||
chars = 0;
|
||
},
|
||
stats() {
|
||
return { entries: entries.size, chars };
|
||
},
|
||
};
|
||
}
|
||
|
||
export type ChapterCache = ReturnType<typeof createChapterCache>;
|
||
|
||
/** The process-wide cache the report route reads chapters from. */
|
||
export const reportChapterCache: ChapterCache = createChapterCache();
|
||
|
||
export function chapterCacheKey(userId: string, reportId: string, language: ReportLanguage): string {
|
||
return `${userId}:${reportId}:${language}`;
|
||
}
|