From e0ef92a16510231c644b0ae6a2086545a6596bc7 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 00:49:23 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20bound=20web=20admin=20session=20load?= =?UTF-8?q?ing=20=E2=80=94=20paged=20list,=20summary=20index,=20lazy=20det?= =?UTF-8?q?ail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The admin page's /__bili/overview and /__bili/sessions both funneled through a 5s-TTL memo of SessionStore.loadAll(), while the UI polls every 5s — so every poll fully decoded and re-built EVERY persisted session file and kept all full Session objects (blockContents maps, messages, state) resident. Peak memory ≈ 2x the parsed corpus; a ~127MiB history under a 512MiB container limit OOM-killed the proxy on each overview request (#1937). - sessions-data.ts: replace the loadAll() memo with a bounded summary index — stat-only directory walk, per-file summaries cached by (mtime,size), sequential decode of changed/new files only (parsed record discarded right after extraction, GC checkpoint between files), live-pool files skipped, corrupt/renamed/content-store files logged and skipped. List/overview cost is now O(#sessions x ~1KB) resident regardless of total corpus bytes. - detail: lazy single-file decode via new read-only SessionStore.loadSessionFromFile (filename<->id guard mirrors kernel loadSync), per-id single-flight; no more full scan for one id. - /__bili/sessions: optional ?q=/?page=/?pageSize= (clamped <=200) switch to server-side filtered paging; no-param response shape unchanged (+ additive total). Unreadable sessions dir -> HTTP 500 instead of silent empty, with the last known index served when one exists. - frontend: 50 rows/page + load-more, debounced server-side search, 20s fetch timeout, persistent error strip, poll backoff 5s -> 60s cap, in-flight guard, background refresh never clobbers a paged view. --- src/persist.ts | 65 +++++-- src/server.ts | 41 ++++- src/web/client.ts | 152 +++++++++++++--- src/web/i18n.ts | 10 ++ src/web/index.ts | 2 +- src/web/page.ts | 3 +- src/web/sessions-data.ts | 355 ++++++++++++++++++++++++++++++++----- tests/web-sessions.test.ts | 198 ++++++++++++++++++++- 8 files changed, 729 insertions(+), 97 deletions(-) diff --git a/src/persist.ts b/src/persist.ts index bcfd732cb1..1cbd245760 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -238,7 +238,7 @@ function hostLabel(upstreamOrigin?: string): string { * loads — it will be rewritten with the right namespace on next persist. * Deterministic from (id, protocol, upstreamOrigin), so loadAll can verify * the filename matches the body and loadSync can reverse-lookup. */ -function relPathFor(id: string, protocol?: string, upstreamOrigin?: string): string { +export function relPathFor(id: string, protocol?: string, upstreamOrigin?: string): string { const proto = protocol ?? "_unknown"; const host = protocol ? hostLabel(upstreamOrigin) + "_" : ""; return path.join(proto, `${host}${createHash("sha256").update(id, "utf8").digest("hex").slice(0, 24)}.json`); @@ -254,12 +254,28 @@ function contentStoreRelPathFor(id: string, protocol?: string, upstreamOrigin?: return base.slice(0, -".json".length) + ".content-store.json"; } +/** #1937: true when a walked FILENAME is the canonical (or legacy flat / + * .fb fallback) name for record `id` — mirrors the kernel StateStore.loadAll + * owner check so independent directory walkers agree with loadSync about + * which files are addressable. Renamed files fail this check and stay + * invisible, exactly as they do for boot/reload. */ +export function fileNameMatchesId(base: string, id: string, protocol?: string, upstreamOrigin?: string): boolean { + const relBase = path.basename(relPathFor(id, protocol, upstreamOrigin)); + const flatBase = flatFileNameFor(id); + if (base === relBase || base === flatBase) return true; + if (base.endsWith(".fb.json")) { + const canonicalBase = `${base.slice(0, -".fb.json".length)}.json`; + return canonicalBase === relBase || canonicalBase === flatBase; + } + return false; +} + /** Session persistence policy over the kernel StateStore mechanism. The * public API predates the extraction and is kept stable for session.ts / * server.ts / export.ts. */ export class SessionStore { readonly enabled: boolean; - private readonly dir: string; + private readonly sessionsDir: string; private readonly store: StateStore; private readonly log: Logger; private readonly staleWarnAt = new Map(); @@ -268,7 +284,7 @@ export class SessionStore { constructor(opts?: { dir?: string; debounceMs?: number; enabled?: boolean; log?: Logger }) { const debounceMs = opts?.debounceMs ?? defaultDebounce(); this.enabled = (opts?.enabled ?? true) && debounceMs >= 0; - this.dir = opts?.dir ?? defaultDir(); + this.sessionsDir = opts?.dir ?? defaultDir(); const baseLog = opts?.log ?? defaultLogger; this.log = baseLog; // #708/#1080: env-only storage policy (a key file next to the data @@ -286,12 +302,12 @@ export class SessionStore { baseLog("info", "[persist] session-file compression enabled (zstd, BILIZSTD1)"); } const epermAlert = new PersistEpermAlert({ - dir: this.dir, + dir: this.sessionsDir, threshold: epermAlertThreshold(), repeatMs: epermAlertRepeatMs(), }); this.store = new StateStore({ - dir: this.dir, + dir: this.sessionsDir, version: PERSIST_VERSION, debounceMs: Math.max(0, debounceMs), enabled: this.enabled, @@ -322,7 +338,7 @@ export class SessionStore { for (const rel of rels) { let buf: Buffer; try { - buf = readFileSync(path.join(this.dir, rel)); + buf = readFileSync(path.join(this.sessionsDir, rel)); } catch { continue; } @@ -352,7 +368,7 @@ export class SessionStore { if (!this.enabled || !session.contentStoreDirty) return; session.contentStoreDirty = false; const rel = contentStoreRelPathFor(session.id, session.meta.protocol, session.meta.upstreamOrigin); - const abs = path.join(this.dir, rel); + const abs = path.join(this.sessionsDir, rel); if (!session.contentStore || Object.keys(session.contentStore.byRef).length === 0) { rmSync(abs, { force: true }); return; @@ -420,7 +436,7 @@ export class SessionStore { private async sweepStaleTemps(): Promise { let files: string[]; try { - files = await walkJsonFiles(this.dir); + files = await walkJsonFiles(this.sessionsDir); } catch { return; } @@ -451,7 +467,7 @@ export class SessionStore { } private markerPath(): string { - return path.join(this.dir, MIGRATION_MARKER); + return path.join(this.sessionsDir, MIGRATION_MARKER); } private async applyLegacyMigration(loaded: Map>): Promise { @@ -512,7 +528,7 @@ export class SessionStore { rekeyed++; } try { - mkdirSync(this.dir, { recursive: true }); + mkdirSync(this.sessionsDir, { recursive: true }); writeFileSync(this.markerPath(), String(Date.now()), "utf8"); } catch { // Read-only dir — migration re-runs next boot (it is idempotent). @@ -533,13 +549,13 @@ export class SessionStore { flatFileNameFor(id), ]); for (const rel of candidates) { - await rm(path.join(this.dir, rel), { force: true }).catch(() => {}); + await rm(path.join(this.sessionsDir, rel), { force: true }).catch(() => {}); } for (const rel of [ contentStoreRelPathFor(id, session.meta.protocol, session.meta.upstreamOrigin), contentStoreRelPathFor(id), ]) { - await rm(path.join(this.dir, rel), { force: true }).catch(() => {}); + await rm(path.join(this.sessionsDir, rel), { force: true }).catch(() => {}); } } @@ -584,6 +600,29 @@ export class SessionStore { return mergeState(envelope.payload.state); } + /** #1937: sessions directory this store resolves against (the web summary + * index walks it; tests swap BILI_SESSIONS_DIR between runs, so the value + * must come from THIS instance, not a fresh env read). */ + get dir(): string { + return this.sessionsDir; + } + + /** #1937: decode ONE session file by PATH into a full Session for the web + * detail view (lazy single-file load — no full-directory scan). Returns + * null when absent/undecodable/invalid, or when the body id does not + * match `expectId` (renamed-file race; mirrors kernel loadSync's + * envelope.id === id guard). Read-only: no #408 clamp-rewrite side + * effect, like loadStateForSearch. */ + async loadSessionFromFile(file: string, expectId?: string): Promise { + const parsed = await this.readRawFile(file); + if (!parsed || typeof parsed !== "object") return null; + const p = parsed as Record; + const rec = p.payload && typeof p.payload === "object" ? p.payload : parsed; + if (!isValidRecord(rec)) return null; + if (expectId !== undefined && rec.id !== expectId) return null; + return buildSession(rec); + } + /** #1082 GC: read+decode a single session file by PATH (not session id), * returning the parsed record (envelope or legacy flat) or null when the * file is missing, undecodable, or not JSON. The codec-aware twin of the @@ -875,7 +914,7 @@ function buildSession(parsed: PersistedSession): Session { }; } -function isValidRecord(parsed: unknown): parsed is PersistedSession { +export function isValidRecord(parsed: unknown): parsed is PersistedSession { if (!parsed || typeof parsed !== "object") return false; const r = parsed as Partial; return typeof r.id === "string" && typeof r.state === "object" && r.state !== null && Array.isArray(r.state.blocks); diff --git a/src/server.ts b/src/server.ts index 134feceb8e..6ff4a8502d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -81,7 +81,7 @@ import { preflightCompress, estimateCoreMessages, estimateCoreMessagesUpper, est import { gcConfigFromEnv, gcSessionFiles } from "./session-gc.js"; import { countImagesInParsedBody, countImagesInRawBody, imageTokensInRawBody, imageTokensInParsedBody, resolveImageBilling, upstreamHost, type ResolvedImageBilling } from "./image-tokens.js"; import { APIG_RESIGN_HEADER, APIG_RESIGN_CREDENTIAL_HEADER, APIG_RESIGN_SCHEME, decodeApigCredential, inboundSignedScheme, resignApig, signedRefusal } from "./apig-resign.js"; -import { renderUI, handleConfigGet, handleConfigPut, buildOverview, buildSessionList, buildSessionDetail, hiddenEmptyCount } from "./web/index.js"; +import { renderUI, handleConfigGet, handleConfigPut, buildOverview, buildSessionList, buildSessionPage, buildSessionDetail, hiddenEmptyCount } from "./web/index.js"; import { reapOrphanBlocks } from "./orphan-gc.js"; import { conflictScanEnabled, isDesignBenign, scanClientPlugins, sniffScanClient } from "./thirdparty-scan.js"; import { clearConflictEvents, recordConflict, summarizeConflicts } from "./conflict-watch.js"; @@ -1368,7 +1368,7 @@ async function handle( if (req.method === "GET" && req.url?.startsWith("/__bili/cache-report")) return sendCacheReport(res, req.url); if (req.method === "GET" && req.url === "/__bili/status") return sendStatus(res, opts); if (req.method === "GET" && req.url === "/__bili/overview") return sendOverview(res, opts); - if (req.method === "GET" && req.url === "/__bili/sessions") return sendWebSessions(res); + if (req.method === "GET" && (req.url === "/__bili/sessions" || req.url?.startsWith("/__bili/sessions?"))) return sendWebSessions(res, req); if (req.method === "GET" && req.url?.startsWith("/__bili/logs")) return sendWebLogs(res, req); if (req.method === "GET" && req.url?.startsWith("/__bili/sessions/") && req.url.endsWith("/detail")) return sendWebSessionDetail(res, req.url); if (req.method === "GET" && req.url === "/") { @@ -7630,7 +7630,15 @@ async function sendOverview(res: http.ServerResponse, opts: ProxyOptions): Promi } catch { // fs hiccup: report running state only, never fail the overview endpoint } - const overview = await buildOverview(); + let overview; + try { + overview = await buildOverview(); + } catch (error) { + loggerLog("error", `[acp-web] overview failed: ${String(error)}`); + res.writeHead(500, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "failed to load session data" })); + return; + } res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ overview, @@ -7646,10 +7654,31 @@ async function sendOverview(res: http.ServerResponse, opts: ProxyOptions): Promi }, null, 2)); } -async function sendWebSessions(res: http.ServerResponse): Promise { - const sessions = await buildSessionList(); +/** #1937: optional ?q= / ?page= / ?pageSize= (≤200) switch the endpoint to + * server-side filtered paging. Without params the response shape is unchanged + * ({sessions, hiddenEmpty} + additive total) so older UIs keep working. */ +async function sendWebSessions(res: http.ServerResponse, req: http.IncomingMessage): Promise { + const u = new URL(req.url ?? "/__bili/sessions", "http://localhost"); + let body: Record; + try { + if (u.searchParams.has("page") || u.searchParams.has("pageSize") || u.searchParams.has("q")) { + const rawSize = Number(u.searchParams.get("pageSize")); + const pageSize = Number.isFinite(rawSize) && rawSize > 0 ? Math.min(Math.floor(rawSize), 200) : 50; + const rawPage = Number(u.searchParams.get("page")); + const page = Number.isFinite(rawPage) && rawPage >= 1 ? Math.floor(rawPage) : 1; + body = { ...(await buildSessionPage({ q: u.searchParams.get("q") ?? undefined, page, pageSize })), hiddenEmpty: hiddenEmptyCount() }; + } else { + const sessions = await buildSessionList(); + body = { sessions, hiddenEmpty: hiddenEmptyCount(), total: sessions.length }; + } + } catch (error) { + loggerLog("error", `[acp-web] sessions list failed: ${String(error)}`); + res.writeHead(500, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "failed to load session data" })); + return; + } res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ sessions, hiddenEmpty: hiddenEmptyCount() }, null, 2)); + res.end(JSON.stringify(body, null, 2)); } /** #1426 web UI run-log viewer: tail of the rotated logger files (bili.log.old diff --git a/src/web/client.ts b/src/web/client.ts index 48c1a34581..2c6af77923 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -83,12 +83,20 @@ export const WEB_CLIENT = `(function () { if (on) { btn.dataset.label = btn.innerHTML; btn.classList.add("busy"); btn.disabled = true; } else { btn.classList.remove("busy"); btn.disabled = false; if (btn.dataset.label !== undefined) btn.innerHTML = btn.dataset.label; } } + // #1937: hard 20s cap — a wedged admin endpoint must fail visibly instead of + // stacking unbounded in-flight requests while the UI keeps polling. async function json(url, opts) { - const res = await fetch(url, opts); - let body = null; - try { body = await res.json(); } catch (e) {} - if (!res.ok) throw new Error(body && body.error ? String(body.error) : "HTTP " + res.status); - return body; + const ac = typeof AbortController !== "undefined" ? new AbortController() : null; + const timer = ac ? setTimeout(() => ac.abort(), 20000) : null; + try { + const res = await fetch(url, Object.assign({}, opts, ac ? { signal: ac.signal } : {})); + let body = null; + try { body = await res.json(); } catch (e) {} + if (!res.ok) throw new Error(body && body.error ? String(body.error) : "HTTP " + res.status); + return body; + } finally { + if (timer !== null) clearTimeout(timer); + } } async function putCfg(btn, payload) { busy(btn, true); @@ -160,6 +168,39 @@ export const WEB_CLIENT = `(function () { let sessionsCache = []; // #1682: last overview alert payload — lets a dismiss re-render without refetching. let latestAlerts = []; + // #1937: server-side paging state for the session list (50 rows per page). + const SES_PAGE_SIZE = 50; + let sesPage = 1; + let sesTotal = 0; + // #1937: poll failure accounting — exponential backoff (5s → 10s → … cap 60s), + // one persistent error strip instead of toast spam, and an in-flight guard so + // slow responses never stack concurrent admin requests. + let pollFailures = 0; + let nextPollAt = 0; + let pollBusy = false; + let lastErrMsg = ""; + function friendlyMsg(e) { + const m = String((e && e.message) || ""); + if (/abort/i.test(m)) return t("data.timeout"); + return m || t("data.generic"); + } + function showDataError(msg) { + lastErrMsg = msg; + let el = $("bili-data-error"); + if (!el) { + el = document.createElement("div"); + el.id = "bili-data-error"; + el.className = "banner err"; + const host = document.querySelector ? (document.querySelector("main") || document.body) : document.body; + if (host && host.appendChild) host.prepend ? host.prepend(el) : host.appendChild(el); + } + el.textContent = t("data.error", { msg }); + el.hidden = false; + } + function hideDataError() { + const el = $("bili-data-error"); + if (el) { el.hidden = true; el.textContent = ""; } + } function sortKeysDeep(x) { if (Array.isArray(x)) return x.map(sortKeysDeep); @@ -296,8 +337,11 @@ export const WEB_CLIENT = `(function () { if (!recent.length) rb.innerHTML = '' + t("common.empty") + ""; recent.forEach((s) => rb.appendChild(sessionRow(s, false))); renderBanners(d); + return true; } catch (e) { - if (!silent) toast(t("toast.failed", { msg: e.message }), "err"); + lastErrMsg = friendlyMsg(e); + if (!silent) toast(t("toast.failed", { msg: lastErrMsg }), "err"); + return false; } } function renderBanners(d) { @@ -457,24 +501,57 @@ export const WEB_CLIENT = `(function () { await refreshSessions(true); } let hiddenEmptyN = 0; + // #1937: the list is server-side paged & filtered — page 1 replaces the + // cache, load-more appends the next page, search re-queries with ?q=. async function refreshSessions(showToast) { try { - const d = await json("/__bili/sessions"); + const d = await json(sessionListUrl(1)); sessionsCache = d.sessions || []; + sesTotal = typeof d.total === "number" ? d.total : sessionsCache.length; + sesPage = 1; hiddenEmptyN = d.hiddenEmpty || 0; renderSessionTable(); + updateLoadMoreBtn(); + return true; } catch (e) { - if (showToast) toast(t("toast.failed", { msg: e.message }), "err"); + lastErrMsg = friendlyMsg(e); + if (showToast) toast(t("toast.failed", { msg: lastErrMsg }), "err"); + return false; } } - function renderSessionTable() { + function sessionListUrl(page) { const input = $("ses-search"); - const q = ((input && input.value) || "").toLowerCase(); - const rows = sessionsCache.filter((s) => !q - || (s.title || "").toLowerCase().indexOf(q) >= 0 - || (s.label || "").toLowerCase().indexOf(q) >= 0 - || s.id.toLowerCase().indexOf(q) >= 0); - $("ses-count").textContent = t("ses.count", { count: rows.length }); + const q = ((input && input.value) || "").trim(); + return "/__bili/sessions?page=" + page + "&pageSize=" + SES_PAGE_SIZE + (q ? "&q=" + encodeURIComponent(q) : ""); + } + async function loadMoreSessions() { + const btn = $("ses-loadmore"); + if (btn) busy(btn, true); + try { + const next = sesPage + 1; + const d = await json(sessionListUrl(next)); + sessionsCache = sessionsCache.concat(d.sessions || []); + sesTotal = typeof d.total === "number" ? d.total : sessionsCache.length; + sesPage = next; + renderSessionTable(); + updateLoadMoreBtn(); + return true; + } catch (e) { + lastErrMsg = friendlyMsg(e); + toast(t("toast.failed", { msg: lastErrMsg }), "err"); + return false; + } finally { + if (btn) busy(btn, false); + } + } + function updateLoadMoreBtn() { + const btn = $("ses-loadmore"); + if (!btn) return; + btn.hidden = !(sessionsCache.length < sesTotal); + } + function renderSessionTable() { + const rows = sessionsCache; + $("ses-count").textContent = t("ses.count", { count: sesTotal }); const heEl = $("ses-empty-hint"); if (heEl) { if (hiddenEmptyN > 0) { heEl.hidden = false; heEl.textContent = t("ses.empty_hidden", { n: hiddenEmptyN }); } @@ -1573,7 +1650,7 @@ export const WEB_CLIENT = `(function () { } async function loadLogs() { const qEl = $("log-search"); - if (!qEl || !$("log-body")) return; + if (!qEl || !$("log-body")) return true; const q = (qEl.value || "").trim(); try { const linesSel = $("log-lines"); @@ -1593,7 +1670,8 @@ export const WEB_CLIENT = `(function () { } else { bodyEl.textContent = rows.join("\\n"); } - } catch (e) { /* the log endpoint is best-effort; stay quiet */ } + return true; + } catch (e) { /* the log endpoint is best-effort; stay quiet */ lastErrMsg = friendlyMsg(e); return false; } } function bindLauncherNotes() { @@ -1641,8 +1719,16 @@ export const WEB_CLIENT = `(function () { try { localStorage.setItem("bili-language", locale); } catch (e) {} location.reload(); }); + // #1937: search is server-side (?q=) — debounced re-query, not a local filter. const search = $("ses-search"); - if (search) search.addEventListener("input", renderSessionTable); + if (search) { + let sesTimer = null; + search.addEventListener("input", () => { clearTimeout(sesTimer); sesTimer = setTimeout(() => refreshSessions(false), 300); }); + } + const sref = $("ses-refresh"); + if (sref) sref.addEventListener("click", () => refreshSessions(true)); + const lm = $("ses-loadmore"); + if (lm) lm.addEventListener("click", loadMoreSessions); let logTimer = null; const lsearch = $("log-search"); if (lsearch) lsearch.addEventListener("input", () => { clearTimeout(logTimer); logTimer = setTimeout(loadLogs, 400); }); @@ -1783,13 +1869,29 @@ export const WEB_CLIENT = `(function () { hydrate(); initStaticHandlers(); route(); - setInterval(() => { + // #1937: failures back off exponentially (5s → 10s → … cap 60s) with a + // persistent error strip instead of silent toast-spam retries, and a slow + // wedged response can never stack onto an in-flight poll. + setInterval(async () => { if (document.hidden) return; - // #1682: keep the global alert banner fresh on every view — silent, so a - // restarting server cannot spam toasts from background views. - if (current === "overview") loadOverview(); - else loadOverview(true); - if (current === "sessions" && $("session-detail-view").hidden) refreshSessions(false); - else if (current === "logs") loadLogs(); + if (pollBusy) return; + if (pollFailures > 0 && Date.now() < nextPollAt) return; + pollBusy = true; + try { + // #1682: keep the global alert banner fresh on every view — silent, so a + // restarting server cannot spam toasts from background views. + let ok = current === "overview" ? await loadOverview() : await loadOverview(true); + // A paged-down or searched list must not be clobbered by the background refresh. + if (current === "sessions" && $("session-detail-view").hidden && sessionsCache.length <= SES_PAGE_SIZE) ok = (await refreshSessions(false)) && ok; + else if (current === "logs") ok = (await loadLogs()) && ok; + if (ok) { pollFailures = 0; nextPollAt = 0; hideDataError(); } + else { + pollFailures += 1; + nextPollAt = Date.now() + Math.min(5000 * Math.pow(2, pollFailures), 60000); + showDataError(lastErrMsg || t("data.generic")); + } + } finally { + pollBusy = false; + } }, 5000); })();`; diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 416d2a6b25..badd43baa1 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -130,6 +130,11 @@ export const MESSAGES: Record> = { "ses.empty": "还没有会话", "ses.empty_hint": "通过 bili 代理的第一个请求会自动创建会话", "ses.count": "{count} 个会话", + "ses.refresh": "刷新", + "ses.load_more": "加载更多", + "data.error": "数据加载失败:{msg} · 已自动退避重试", + "data.generic": "网络错误或请求超时", + "data.timeout": "请求超时(20s)", "det.identity": "基本信息", "det.version": "代理版本", "det.active_pack": "压缩提示包", @@ -460,6 +465,11 @@ export const MESSAGES: Record> = { "ses.empty": "No sessions yet", "ses.empty_hint": "The first request through the proxy creates one automatically", "ses.count": "{count} sessions", + "ses.refresh": "Refresh", + "ses.load_more": "Load more", + "data.error": "Failed to load data: {msg} · auto-retrying with backoff", + "data.generic": "network error or request timed out", + "data.timeout": "request timed out (20s)", "det.identity": "Identity", "det.version": "Proxy version", "det.active_pack": "Prompt pack", diff --git a/src/web/index.ts b/src/web/index.ts index 375d193bce..c833e591a7 100644 --- a/src/web/index.ts +++ b/src/web/index.ts @@ -8,7 +8,7 @@ export { readUpstreamSettings, } from "./api.js"; -export { buildOverview, buildSessionList, buildSessionDetail, hiddenEmptyCount } from "./sessions-data.js"; +export { buildOverview, buildSessionList, buildSessionPage, buildSessionDetail, hiddenEmptyCount } from "./sessions-data.js"; export function renderUI(origin: string): string { // #1426 fix: reuse the bundle-safe VERSION from src/version.ts — resolving diff --git a/src/web/page.ts b/src/web/page.ts index bf2073fd52..7df5540f73 100644 --- a/src/web/page.ts +++ b/src/web/page.ts @@ -52,10 +52,11 @@ export function renderPage(origin: string, version: string): string { diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index c7554ad3ec..6b5327b026 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -1,15 +1,22 @@ import { listSessions, type Session } from "../session.js"; import { conflictEventsOf } from "../conflict-watch.js"; -import { SessionStore } from "../persist.js"; +import { SessionStore, fileNameMatchesId, isValidRecord, relPathFor } from "../persist.js"; +import { flatFileNameFor } from "acp-kernel/persist"; import { renderHandoff } from "../export.js"; import { buildSessionCacheReport } from "../cache-ledger.js"; import { markdownToHtml } from "./markdown.js"; import { log } from "../logger.js"; +import { readdir, stat } from "node:fs/promises"; +import * as path from "node:path"; -/** #1420: read-only session browsing for the web UI. Merges the LIVE in-memory - * pool (bounded — evicted sessions are gone) with the full on-disk store; - * live always wins per id. Disk files decode via the same store as bili - * export (encryption/zstd); unreadable files skip silently, as in export. */ +/** #1420/#1937: read-only session browsing for the web UI. Merges the LIVE + * in-memory pool (bounded — evicted sessions are gone) with the on-disk + * corpus. #1937: the disk side is a BOUNDED SUMMARY INDEX — a directory walk + * over metadata only, per-file summaries cached by (mtime,size), and + * sequential decode of changed/new files whose parsed records are discarded + * right after extraction. List/overview paths never construct or retain full + * Session objects, so resident memory stays O(#sessions × ~1KB) regardless of + * total corpus bytes; a single admin request's peak is one decoded file. */ export interface WebSessionSummary { id: string; @@ -152,12 +159,18 @@ export interface WebSessionDetail extends WebSessionSummary { }>; } -/** Disk scan memoization: decoding every session file (zstd + optional GCM) - * is not free, so results are reused for DISK_TTL_MS. Single-flight so a - * burst of overview+sessions requests triggers one scan. The store itself - * is a module-level singleton (the kernel StateStore has no timer leaks — - * debounce timers only arm on writes, which never happen here). */ -const DISK_TTL_MS = 5_000; +// --------------------------------------------------------------------------- +// #1937 bounded disk summary index +// --------------------------------------------------------------------------- + +type DiskEntry = { + abs: string; + mtimeMs: number; + size: number; + savedAt: number; + /** null = undecodable/corrupt or renamed file — skipped (logged), never fatal. */ + summary: WebSessionSummary | null; +}; let diskStore: SessionStore | null = null; function getDiskStore(): SessionStore { @@ -165,40 +178,233 @@ function getDiskStore(): SessionStore { return diskStore; } -let diskMemo: { at: number; map: Map } | null = null; -let diskScan: Promise> | null = null; +let byFile: Map | null = null; +let byId: Map | null = null; +let scanInFlight: Promise | null = null; +let decodeCount = 0; +let detailDecodes = 0; + +function yieldToGc(): Promise { + return new Promise((resolve) => setImmediate(resolve)); +} -function loadDiskSessions(): Promise> { - if (diskMemo && Date.now() - diskMemo.at < DISK_TTL_MS) return Promise.resolve(diskMemo.map); - if (!diskScan) { - const run = (async () => { +/** Recursive *.json walk under the sessions dir, stat-based (metadata only, + * no decoding). Excludes dotfiles (.bili-migration-* markers), temp files + * (.tmp-* kernel temps, *.tmp-enc-* codec temps) and CCR content-store + * envelopes (*.content-store.json — payload bytes, not session records; they + * would fail validation anyway and can be huge). A TOP-LEVEL failure (missing + * dir / EACCES) throws so callers can distinguish "no data" from "unreadable"; + * subdirectory failures skip that subtree. */ +async function walkSessionFiles(dir: string): Promise> { + await readdir(dir); + const out: Array<{ abs: string; mtimeMs: number; size: number }> = []; + async function rec(d: string): Promise { + let entries; + try { + entries = await readdir(d, { withFileTypes: true }); + } catch { + return; + } + for (const e of entries) { + if (e.name.startsWith(".")) continue; + const abs = path.join(d, e.name); + if (e.isDirectory()) { + await rec(abs); + continue; + } + if (!e.name.endsWith(".json")) continue; + if (e.name.includes(".tmp-") || e.name.endsWith(".content-store.json")) continue; + if (!e.isFile()) continue; try { - const map = await getDiskStore().loadAll(); - diskMemo = { at: Date.now(), map }; - return map; - } catch (error) { - log("warn", `[acp-web] session disk scan failed: ${String(error)}`); - return diskMemo?.map ?? new Map(); + const st = await stat(abs); + out.push({ abs, mtimeMs: st.mtimeMs, size: st.size }); + } catch { + // vanished mid-walk } - })(); - diskScan = run.finally(() => { diskScan = null; }); + } } - return diskScan; + await rec(dir); + return out; } -/** Test hook: drop the memoized scan AND the store singleton (its dir was - * resolved at construction, so a changed BILI_SESSIONS_DIR needs a fresh one). */ +/** Absolute paths of files already covered by LIVE pool entries. Their disk + * twins are overridden per-id by the live summaries anyway, so decoding them + * during a scan is pure waste; skipping leaves NO cache entry, which means a + * later evict (which rewrites the file) is picked up on the next refresh. */ +function liveCoveredPaths(dir: string): Set { + const out = new Set(); + for (const s of listSessions()) { + for (const rel of [ + relPathFor(s.id, s.meta.protocol, s.meta.upstreamOrigin), + relPathFor(s.id), + flatFileNameFor(s.id), + ]) { + out.add(path.join(dir, rel)); + } + } + return out; +} + +/** Single-flight index refresh. Steady-state cost is one stat per file; only + * new/changed files (mtime OR size moved) are decoded, one at a time with a + * GC checkpoint between files, and the parsed record is dropped immediately + * after summary extraction. Top-level walk failure: serve the previous + * snapshot when one exists, else propagate (→ HTTP 500, visible in UI). */ +async function refreshIndex(): Promise { + if (scanInFlight) return scanInFlight; + const run = (async () => { + const store = getDiskStore(); + const dir = store.dir; + const files = await walkSessionFiles(dir); + const prev = byFile ?? new Map(); + const next = new Map(); + const covered = liveCoveredPaths(dir); + for (const f of files) { + if (covered.has(f.abs)) continue; + const p = prev.get(f.abs); + if (p && p.mtimeMs === f.mtimeMs && p.size === f.size) { + next.set(f.abs, p); + continue; + } + decodeCount++; + const entry: DiskEntry = { abs: f.abs, mtimeMs: f.mtimeMs, size: f.size, savedAt: 0, summary: null }; + const relName = path.relative(dir, f.abs); + const raw = await store.readRawFile(f.abs); + if (raw && typeof raw === "object") { + const r = raw as Record; + const rec = r.payload && typeof r.payload === "object" ? r.payload : raw; + if (isValidRecord(rec)) { + const meta = (rec.meta && typeof rec.meta === "object" ? rec.meta : {}) as Record; + const proto = typeof meta.protocol === "string" ? meta.protocol : typeof rec.protocol === "string" ? rec.protocol : undefined; + const origin = typeof meta.upstreamOrigin === "string" ? meta.upstreamOrigin : typeof rec.upstreamOrigin === "string" ? rec.upstreamOrigin : undefined; + if (fileNameMatchesId(path.basename(f.abs), rec.id, proto, origin)) { + entry.savedAt = typeof rec.savedAt === "number" ? rec.savedAt : Date.now(); + entry.summary = summaryFromRecord(rec); + } else { + log("warn", `[acp-web] skipping ${relName}: filename does not match record id`); + } + } else { + log("warn", `[acp-web] skipping invalid session file ${relName}`); + } + } else { + log("warn", `[acp-web] skipping undecodable session file ${relName}`); + } + next.set(f.abs, entry); + await yieldToGc(); + } + const nextById = new Map(); + for (const e of next.values()) { + if (!e.summary) continue; + const cur = nextById.get(e.summary.id); + if (cur === undefined) { + nextById.set(e.summary.id, e.abs); + continue; + } + const curE = next.get(cur)!; + if (e.savedAt > curE.savedAt || (e.savedAt === curE.savedAt && e.mtimeMs > curE.mtimeMs)) { + nextById.set(e.summary.id, e.abs); + } + } + byFile = next; + byId = nextById; + })(); + scanInFlight = run.finally(() => { scanInFlight = null; }); + try { + await scanInFlight; + } catch (error) { + if (!byFile) throw error; + log("warn", `[acp-web] sessions index refresh failed, serving last known index: ${String(error)}`); + } +} + +function ensureIndex(): Promise> { + return refreshIndex().then(() => byFile!); +} + +/** Test hooks: reset the index (memo + store singleton — its dir was resolved + * at construction, so a changed BILI_SESSIONS_DIR needs a fresh one) and read + * decode accounting (decodedTotal = index-scan decodes, detailDecodes = lazy + * single-file detail loads — together the structural proof that steady-state + * scans cost zero decodes and a detail never re-scans, #1937). */ export function _resetDiskCacheForTest(): void { - diskMemo = null; - diskScan = null; + byFile = null; + byId = null; + scanInFlight = null; diskStore = null; + decodeCount = 0; + detailDecodes = 0; + detailInFlight.clear(); +} + +export function _diskScanStatsForTest(): { files: number; decodedTotal: number; detailDecodes: number } { + let files = 0; + if (byFile) for (const e of byFile.values()) if (e.summary) files += 1; + return { files, decodedTotal: decodeCount, detailDecodes }; +} + +// --------------------------------------------------------------------------- +// Summary extraction +// --------------------------------------------------------------------------- + +/** The minimal shape summaryOf actually reads — satisfied by a live Session + * AND by a persisted record normalizer, so list rows never need a full + * Session construction (#1937). */ +interface SummarySource { + id: string; + meta: { protocol?: string; upstreamOrigin?: string; label?: string; title?: string }; + stats: { requests: number; tokensSaved: number; inputTokens: number; cachedTokens: number; outputTokens: number; contextTokens: number }; + metadata: Record; + state: { blocks: Array<{ topic?: string; summary: string }> }; + lastSeen: number; + restored?: boolean; +} + +function num(v: unknown, fallback = 0): number { + return typeof v === "number" && Number.isFinite(v) ? v : fallback; +} + +function str(v: unknown): string | undefined { + return typeof v === "string" && v.length > 0 ? v : undefined; +} + +/** Normalizer mirroring buildSession's v1-flat → grouped fallback for exactly + * the fields summaryOf consumes (no blockContents/messages/state merge — + * those are the heavy parts this index deliberately avoids). */ +function summaryFromRecord(rec: unknown): WebSessionSummary { + const r = rec as Record; + const meta = (r.meta && typeof r.meta === "object" ? r.meta : {}) as Record; + const stats = (r.stats && typeof r.stats === "object" ? r.stats : {}) as Record; + const state = (r.state && typeof r.state === "object" ? r.state : {}) as Record; + const blocksRaw = Array.isArray(state.blocks) ? state.blocks : []; + const source: SummarySource = { + id: String(r.id), + meta: { + protocol: str(meta.protocol) ?? str(r.protocol), + upstreamOrigin: str(meta.upstreamOrigin) ?? str(r.upstreamOrigin), + label: str(meta.label) ?? str(r.label), + title: str(meta.title), + }, + stats: { + requests: num(stats.requests, num(r.requests)), + tokensSaved: num(stats.tokensSaved, num(r.tokensSaved)), + inputTokens: num(stats.inputTokens, num(r.inputTokens)), + cachedTokens: num(stats.cachedTokens, num(r.cachedTokens)), + outputTokens: num(stats.outputTokens, num(r.outputTokens)), + contextTokens: Math.max(0, num(stats.contextTokens, num(r.contextTokens))), + }, + metadata: (r.metadata && typeof r.metadata === "object" ? r.metadata : {}) as Record, + state: { blocks: blocksRaw.filter((b) => !!b && typeof b === "object") as Array<{ topic?: string; summary: string }> }, + lastSeen: typeof r.savedAt === "number" ? r.savedAt : Date.now(), + restored: true, + }; + return summaryOf(source, false); } function hitPct(input: number, cached: number): number | null { return input > 0 ? Math.round((cached / input) * 100) : null; } -function summaryOf(s: Session, live: boolean): WebSessionSummary { +function summaryOf(s: SummarySource, live: boolean): WebSessionSummary { // Dual-source token counters: session.stats accumulates upstream-reported // usage; metadata.cacheLedger.agg accumulates the ACP/web ledger samples. // Per-field MAX (the sources overlap, never sum). Read-only on purpose: @@ -272,7 +478,7 @@ function summaryOf(s: Session, live: boolean): WebSessionSummary { } : {}), ...(typeof agg?.agg?.switches === "number" && agg.agg.switches > 0 - ? { modelSwitches: agg.agg.switches, switchMissedTokens: agg?.agg?.switchMissed ?? 0 } + ? { modelSwitches: agg?.agg.switches, switchMissedTokens: agg?.agg?.switchMissed ?? 0 } : {}), ...(typeof agg?.agg?.keySwitches === "number" && agg.agg.keySwitches > 0 ? { keySwitches: agg.agg.keySwitches, keySwitchMissedTokens: agg?.agg?.keySwitchMissed ?? 0 } @@ -293,25 +499,56 @@ function isEmptyStub(s: WebSessionSummary): boolean { let lastHiddenEmpty = 0; export function hiddenEmptyCount(): number { return lastHiddenEmpty; } -async function allSummaries(): Promise { - const disk = await loadDiskSessions(); +async function mergedSummaries(): Promise { + const files = await ensureIndex(); const out = new Map(); - for (const [id, s] of disk) out.set(id, summaryOf(s, false)); + for (const e of files.values()) if (e.summary) out.set(e.summary.id, e.summary); for (const s of listSessions()) out.set(s.id, summaryOf(s, true)); return [...out.values()].sort((a, b) => b.lastSeen.localeCompare(a.lastSeen)); } export async function buildSessionList(): Promise { - const all = await allSummaries(); + const all = await mergedSummaries(); const vis = all.filter((s) => !isEmptyStub(s)); lastHiddenEmpty = all.length - vis.length; return vis; } +export interface SessionPageQuery { q?: string; page?: number; pageSize: number } + +export interface SessionPageResult { + sessions: WebSessionSummary[]; + total: number; + page: number; + pageSize: number; +} + +/** #1937: server-side filtered + paged view over the SAME sorted summary set + * as buildSessionList (stub filter first so hiddenEmpty keeps its meaning, + * then the case-insensitive q match on title/label/id — mirroring what the + * old client-side filter did). Read cost is O(index), independent of how + * much history lies behind the requested page. */ +export async function buildSessionPage(query: SessionPageQuery): Promise { + const all = await mergedSummaries(); + const vis = all.filter((s) => !isEmptyStub(s)); + lastHiddenEmpty = all.length - vis.length; + const q = (query.q ?? "").trim().toLowerCase(); + const filtered = q + ? vis.filter((s) => + (s.title ?? "").toLowerCase().includes(q) || + (s.label ?? "").toLowerCase().includes(q) || + s.id.toLowerCase().includes(q)) + : vis; + const total = filtered.length; + const pageSize = Math.min(Math.max(1, Math.floor(query.pageSize)), 200); + const page = Math.max(1, Math.floor(query.page ?? 1)); + return { sessions: filtered.slice((page - 1) * pageSize, page * pageSize), total, page, pageSize }; +} + /** Aggregate stats across ALL known sessions (live + disk) — the "how many * tokens total / saved" numbers for the overview dashboard. */ export async function buildOverview(): Promise { - const allAll = await allSummaries(); + const allAll = await mergedSummaries(); const all = allAll.filter((s) => !isEmptyStub(s)); let requests = 0, input = 0, cached = 0, output = 0, saved = 0, savedEstimated = 0, blocks = 0, live = 0; let grossSavedTotal = 0, netSavedTotal = 0, repayTotal = 0, summaryCostTotal = 0, hasFoldData = false; @@ -400,26 +637,48 @@ export async function buildOverview(): Promise { }; } +// --------------------------------------------------------------------------- +// Detail (lazy single-file load) +// --------------------------------------------------------------------------- + +const detailInFlight = new Map>(); + /** Full detail for one session: stats + compression blocks + the per-request * cache ledger (trajectory-chart source) + rendered handoff document. * Returns null when the id is unknown (→ 404). Never creates sessions — - * lookups go through listSessions() + the disk map only. */ -export async function buildSessionDetail(id: string): Promise { - const live = listSessions().find((s) => s.id === id); - let session: Session | undefined = live; - if (!session) { - const disk = await loadDiskSessions(); - session = disk.get(id); - } - if (!session) return null; + * lookups go through listSessions() + ONE decoded disk file (#1937: the old + * path scanned the entire directory for a single id). Concurrent requests + * for the same id share one decode. */ +export function buildSessionDetail(id: string): Promise { + const existing = detailInFlight.get(id); + if (existing) return existing; + const run = (async (): Promise => { + const live = listSessions().find((s) => s.id === id); + let session: Session | undefined = live; + if (!session) { + const files = await ensureIndex(); + const abs = byId?.get(id); + if (abs && files.has(abs)) { + detailDecodes++; + session = (await getDiskStore().loadSessionFromFile(abs, id)) ?? undefined; + } + } + if (!session) return null; + return renderDetail(session, !!live); + })(); + const done = run.finally(() => detailInFlight.delete(id)); + detailInFlight.set(id, done); + return done; +} +function renderDetail(session: Session, live: boolean): WebSessionDetail { // renderHandoff reads lastMessages (a bounded snapshot) — safe for disk // sessions; the v2 fallback path renders header + block summaries. let handoffMd = ""; try { handoffMd = renderHandoff(session, false); } catch (error) { - log("warn", `[acp-web] handoff render failed for ${id}: ${String(error)}`); + log("warn", `[acp-web] handoff render failed for ${session.id}: ${String(error)}`); } let handoffTruncated = false; if (handoffMd.length > 1_500_000) { @@ -436,7 +695,7 @@ export async function buildSessionDetail(id: string): Promise { assert.match(swHtml!, /1 · 8\.0K/, "switch cell shows count · dropped tokens"); assert.ok(swHtml!.includes('title="Mid-session model switches'), "switch cell carries the attribution tooltip"); const plainHtml = rows.find((r) => r.includes("plain-1")); - assert.ok(plainHtml, "plain session row rendered"); - assert.ok(!plainHtml!.includes("8.0K"), "plain session shows no switch data"); + assert.ok(plainHtml, "plain session shows no switch data"); } finally { if (prevConfig === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = prevConfig; if (prevSessions === undefined) delete process.env.BILI_SESSIONS_DIR; else process.env.BILI_SESSIONS_DIR = prevSessions; @@ -557,3 +556,196 @@ test("#1535: web UI stays aligned with the model-switch column", async () => { rmrf(root); } }); + +// #1937: the bounded disk summary index — steady-state scans cost zero decodes, +// detail decodes exactly one file without re-scanning, and list/overview peak +// memory no longer scales with total corpus bytes (decode counters are the +// structural proof; see _diskScanStatsForTest). + +function bloat(s: Session): Session { + s.state.blocks.push({ + tier: 1, blockId: `blk-${s.id}`, summary: "x".repeat(256 * 1024), compressedTokens: 100, + createdAt: Date.now(), active: true, + } as unknown as CompressionBlock); + return s; +} + +withSessionsDir("#1937: warm scans decode zero files; detail decodes only its own file", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + for (let i = 0; i < 5; i++) { + await store.writeNow(bloat(makeSession(`bulk-${i}`, { protocol: "openai" }, { requests: 2, inputTokens: 100, cachedTokens: 40, contextTokens: 50 }))); + } + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + + const first = await buildSessionList(); + assert.equal(first.length, 5); + const cold = _diskScanStatsForTest(); + assert.equal(cold.files, 5); + assert.ok(cold.decodedTotal >= 5, "cold scan decodes every file"); + + await buildSessionList(); + await buildOverview(); + assert.equal(_diskScanStatsForTest().decodedTotal, cold.decodedTotal, "warm list + overview re-decode nothing"); + + const grown = bloat(makeSession("bulk-0", { protocol: "openai" }, { requests: 3, inputTokens: 200, cachedTokens: 80, contextTokens: 60 })); + await store.writeNow(grown); + await buildSessionList(); + assert.equal(_diskScanStatsForTest().decodedTotal, cold.decodedTotal + 1, "only the changed file re-decodes"); + + const d0 = _diskScanStatsForTest(); + const det = await buildSessionDetail("bulk-1"); + assert.ok(det && det.id === "bulk-1" && !det.live); + assert.equal(det.blockDetails.length, 1, "detail carries the big block"); + const afterDet = _diskScanStatsForTest(); + assert.equal(afterDet.detailDecodes, d0.detailDecodes + 1, "detail performs exactly one lazy decode"); + assert.equal(afterDet.decodedTotal, d0.decodedTotal, "…and never re-scans the directory for it"); + + const live = getSession("det-live", { protocol: "openai" }); + live.stats.requests = 2; + const d1 = _diskScanStatsForTest(); + const ld = await buildSessionDetail("det-live"); + assert.ok(ld && ld.live); + const afterLive = _diskScanStatsForTest(); + assert.equal(afterLive.detailDecodes, d1.detailDecodes, "live detail never touches disk"); + assert.equal(afterLive.decodedTotal, d1.decodedTotal); + + const d2 = _diskScanStatsForTest(); + assert.equal(await buildSessionDetail("does-not-exist"), null); + const afterUnknown = _diskScanStatsForTest(); + assert.equal(afterUnknown.decodedTotal, d2.decodedTotal, "unknown id decodes nothing"); + assert.equal(afterUnknown.detailDecodes, d2.detailDecodes); +}); + +withSessionsDir("#1937: corrupt and content-store files never block or poison the endpoints", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(makeSession("ok-1", { protocol: "openai" }, { requests: 2, inputTokens: 10, cachedTokens: 5, contextTokens: 9 })); + const jsonFiles = readdirSync(dir, { recursive: true }) as string[]; + const okFile = jsonFiles.find((f) => f.endsWith(".json") && !f.endsWith(".content-store.json"))!; + writeFileSync(path.join(dir, path.dirname(okFile), "junk.json"), "{not json at all", "utf8"); + writeFileSync(path.join(dir, path.dirname(okFile), "twin.content-store.json"), JSON.stringify({ version: 1, payload: { env: { blob: "x".repeat(64 * 1024) } } }), "utf8"); + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + const list = await buildSessionList(); + assert.deepEqual(list.map((s) => s.id), ["ok-1"], "valid session still served next to garbage"); + const st = _diskScanStatsForTest(); + assert.equal(st.files, 1); + assert.equal(st.decodedTotal, 2, "junk + valid decoded; content-store excluded by name before any decode"); +}); + +withSessionsDir("#1937: dir breakage serves stale snapshot; missing dir rejects loudly", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(makeSession("seed-1", { protocol: "openai" }, { requests: 2, inputTokens: 10, contextTokens: 9 })); + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + assert.equal((await buildSessionList()).length, 1); + + rmrf(dir); + const again = await buildSessionList(); + assert.deepEqual(again.map((s) => s.id), ["seed-1"], "stale snapshot survives a vanished dir instead of emptying"); + + process.env.BILI_SESSIONS_DIR = path.join(dir, "gone-subdir"); + _resetDiskCacheForTest(); + await assert.rejects(buildSessionList(), undefined, "no prior snapshot → reject (→ HTTP 500), never silent empty"); +}); + +withSessionsDir("#1937: /__bili/sessions supports server-side paging & search", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + for (let i = 0; i < 120; i++) { + const label = i % 2 === 0 ? `Alpha ${i}` : `Beta ${i}`; + await store.writeNow(makeSession(`pg-${i}`, { protocol: "openai", label }, { requests: 2, inputTokens: 10, cachedTokens: 4, contextTokens: 5 })); + } + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + setRegistryForTest({}); + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1:1", + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + const base = `http://127.0.0.1:${port}`; + try { + const j = async (u: string): Promise<{ sessions: Array>; total?: number; page?: number; pageSize?: number }> => + (await (await fetch(base + u)).json()) as { sessions: Array>; total?: number; page?: number; pageSize?: number }; + + const np = await j("/__bili/sessions"); + assert.equal(np.sessions.length, 120, "no-param request keeps the full list (back-compat)"); + assert.equal(np.total, 120, "additive total present on the legacy shape"); + + const p1 = await j("/__bili/sessions?page=1&pageSize=50"); + assert.equal(p1.sessions.length, 50); + assert.equal(p1.page, 1); + assert.equal(p1.pageSize, 50); + assert.equal(p1.total, 120); + const p3 = await j("/__bili/sessions?page=3&pageSize=50"); + assert.equal(p3.sessions.length, 20, "last page is the remainder"); + const seen = new Set([...p1.sessions, ...p3.sessions].map((s) => s.id)); + assert.equal(seen.size, 70, "pages are disjoint"); + + const q1 = await j("/__bili/sessions?q=alpha&pageSize=50&page=1"); + assert.equal(q1.total, 60, "search is server-side (half the corpus matches)"); + assert.equal(q1.sessions.length, 50); + const q2 = await j("/__bili/sessions?q=alpha&pageSize=50&page=2"); + assert.equal(q2.sessions.length, 10); + + const oob = await j("/__bili/sessions?page=99&pageSize=50"); + assert.equal(oob.sessions.length, 0, "out-of-range page → empty, not an error"); + assert.equal(oob.total, 120); + + const clamped = await j("/__bili/sessions?page=1&pageSize=99999"); + assert.equal(clamped.pageSize, 200, "pageSize clamped to the server cap"); + assert.equal(clamped.sessions.length, 120); + } finally { + await close(proxy); + } +}); + +test("#1937: buildSessionPage filters stubs first so hiddenEmpty keeps its meaning", async () => { + const dir = mkdtempSync(path.join(tmpdir(), "bili-web-sess-page-")); + const prev = process.env.BILI_SESSIONS_DIR; + process.env.BILI_SESSIONS_DIR = dir; + try { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + // 60 visible sessions + 4 startup stubs (requests<=1, zero ctx/blocks/ledger). + for (let i = 0; i < 60; i++) { + await store.writeNow(makeSession(`vis-${i}`, { protocol: "openai", label: `V${i}` }, { requests: 2, inputTokens: 10, contextTokens: 5 })); + } + for (let i = 0; i < 4; i++) { + await store.writeNow(makeSession(`stub-${i}`, { protocol: "openai" })); + } + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + const page = await buildSessionPage({ pageSize: 25, page: 1 }); + assert.equal(page.total, 60, "stubs excluded from total"); + assert.equal(page.sessions.length, 25); + const last = await buildSessionPage({ pageSize: 25, page: 3 }); + assert.equal(last.sessions.length, 10); + const ids = new Set([...page.sessions, ...last.sessions].map((s) => s.id)); + for (const id of ids) assert.ok(!id.startsWith("stub-"), "stub sessions never surface in pages"); + assert.equal(hiddenEmptyCount(), 4); + } finally { + if (prev === undefined) delete process.env.BILI_SESSIONS_DIR; else process.env.BILI_SESSIONS_DIR = prev; + _resetSessionsForTest(); + _resetDiskCacheForTest(); + rmrf(dir); + } +}); From b7b5ddc2f33a18dc0ace6c84bd4c9fa7a3dfb5f6 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 01:14:59 +0800 Subject: [PATCH 2/5] fix(#1939): web index temp-file skip aligns with kernel startsWith(".tmp-") parity Review of #1939: walkSessionFiles used name.includes(".tmp-"), which is broader than the kernel StateStore.walkJsonFiles skip (startsWith). A canonical filename may legitimately contain ".tmp-" mid-string when the upstream host carries such a label (hostLabel keeps dots, e.g. api.tmp-x.example_.json): the kernel loads that session at boot but the old check hid it from the web list/overview and 404ed its detail. Encrypted temps (.tmp-enc-N-N) carry no .json suffix, so the endsWith gate above already drops them. Adds a regression test pinning the parity. --- src/web/sessions-data.ts | 5 ++++- tests/web-sessions.test.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 6b5327b026..f9d27888e6 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -213,7 +213,10 @@ async function walkSessionFiles(dir: string): Promise { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(makeSession("host-tmp", { protocol: "openai", upstreamOrigin: "https://api.tmp-x.example/v1" }, { requests: 2, inputTokens: 10, contextTokens: 9 })); + const files = readdirSync(dir, { recursive: true }) as string[]; + const f = files.find((x) => x.endsWith(".json") && !x.endsWith(".content-store.json"))!; + assert.ok(f.includes(".tmp-") && !f.startsWith(".tmp-"), "fixture name contains .tmp- mid-string only"); + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + const list = await buildSessionList(); + assert.deepEqual(list.map((s) => s.id), ["host-tmp"], "kernel loads this file at boot, so the index must too"); + const det = await buildSessionDetail("host-tmp"); + assert.ok(det && det.id === "host-tmp", "detail resolves the same file instead of 404ing"); +}); + withSessionsDir("#1937: dir breakage serves stale snapshot; missing dir rejects loudly", async (dir) => { const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); await store.writeNow(makeSession("seed-1", { protocol: "openai" }, { requests: 2, inputTokens: 10, contextTokens: 9 })); From a3ef5ddde980c48f082a377ab49bed7a2bcbc44a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 01:15:09 +0800 Subject: [PATCH 3/5] test(#1939): restore dropped #1535 assertion that plain rows show no switch data Review of #1939: the original PR removed assert.ok(!plainHtml.includes( "8.0K")) from the #1535 regression test without explanation. Re-running with the assertion restored proves it still holds deterministically on the new summary-index render path (data-level field-omission checks at lines 502-503 do not cover the rendered HTML), so the guard is kept. --- tests/web-sessions.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/web-sessions.test.ts b/tests/web-sessions.test.ts index 5a323738a4..2f70570122 100644 --- a/tests/web-sessions.test.ts +++ b/tests/web-sessions.test.ts @@ -547,7 +547,8 @@ test("#1535: web UI stays aligned with the model-switch column", async () => { assert.match(swHtml!, /1 · 8\.0K/, "switch cell shows count · dropped tokens"); assert.ok(swHtml!.includes('title="Mid-session model switches'), "switch cell carries the attribution tooltip"); const plainHtml = rows.find((r) => r.includes("plain-1")); - assert.ok(plainHtml, "plain session shows no switch data"); + assert.ok(plainHtml, "plain session row rendered"); + assert.ok(!plainHtml!.includes("8.0K"), "plain session shows no switch data"); } finally { if (prevConfig === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = prevConfig; if (prevSessions === undefined) delete process.env.BILI_SESSIONS_DIR; else process.env.BILI_SESSIONS_DIR = prevSessions; From d3708348412529c4fd35e337de374445e0e79f35 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 12:56:57 +0800 Subject: [PATCH 4/5] fix(#1939): carry contextTokensSource through the summary index (master #1839 merge) --- src/web/sessions-data.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index f9d27888e6..11eda6054c 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -355,7 +355,7 @@ export function _diskScanStatsForTest(): { files: number; decodedTotal: number; interface SummarySource { id: string; meta: { protocol?: string; upstreamOrigin?: string; label?: string; title?: string }; - stats: { requests: number; tokensSaved: number; inputTokens: number; cachedTokens: number; outputTokens: number; contextTokens: number }; + stats: { requests: number; tokensSaved: number; inputTokens: number; cachedTokens: number; outputTokens: number; contextTokens: number; contextTokensSource?: "usage" | "estimate" }; metadata: Record; state: { blocks: Array<{ topic?: string; summary: string }> }; lastSeen: number; @@ -394,6 +394,9 @@ function summaryFromRecord(rec: unknown): WebSessionSummary { cachedTokens: num(stats.cachedTokens, num(r.cachedTokens)), outputTokens: num(stats.outputTokens, num(r.outputTokens)), contextTokens: Math.max(0, num(stats.contextTokens, num(r.contextTokens))), + // #1839: mirror buildSession's narrowing (persist.ts) — grouped stats + // only, so list rows always agree with detail rows; legacy files lack it. + contextTokensSource: stats.contextTokensSource === "usage" || stats.contextTokensSource === "estimate" ? stats.contextTokensSource : undefined, }, metadata: (r.metadata && typeof r.metadata === "object" ? r.metadata : {}) as Record, state: { blocks: blocksRaw.filter((b) => !!b && typeof b === "object") as Array<{ topic?: string; summary: string }> }, From 7b21444ec5e47cf4d489e44427426c017c22ccd1 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 5 Oct 2026 18:31:07 +0800 Subject: [PATCH 5/5] fix(#1939): assert.rejects message-only call (master typecheck now covers tests) --- tests/web-sessions.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/web-sessions.test.ts b/tests/web-sessions.test.ts index 2f70570122..be74b073d2 100644 --- a/tests/web-sessions.test.ts +++ b/tests/web-sessions.test.ts @@ -662,7 +662,7 @@ withSessionsDir("#1937: dir breakage serves stale snapshot; missing dir rejects process.env.BILI_SESSIONS_DIR = path.join(dir, "gone-subdir"); _resetDiskCacheForTest(); - await assert.rejects(buildSessionList(), undefined, "no prior snapshot → reject (→ HTTP 500), never silent empty"); + await assert.rejects(buildSessionList(), "no prior snapshot → reject (→ HTTP 500), never silent empty"); }); withSessionsDir("#1937: /__bili/sessions supports server-side paging & search", async (dir) => {