diff --git a/src/server/admin.ts b/src/server/admin.ts index 222ad0897b..ce343d01a9 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -30,7 +30,7 @@ import { formatUpstreamError, getUpstreamConnectionStatus, proxyDispatcher, reco import { detectStaleInstall, getLastInstall } from "../update.js"; import { getPlannedUpgrade, getUpdatePolicyState } from "../update-policy.js"; import { PACKAGE_NAME, VERSION, BUILD_COMMIT } from "../version.js"; -import { buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, handleConfigGet, handleConfigPut, hiddenEmptyCount, renderUI } from "../web/index.js"; +import { buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, buildBlockContent, buildRefLookup, handleConfigGet, handleConfigPut, hiddenEmptyCount, renderUI } from "../web/index.js"; import { buildSessionReportMd, buildSessionReportSvg } from "../web/report.js"; import { queryLogLines } from "../web/logs-query.js"; import { buildDiagnostics } from "../web/diagnostics.js"; @@ -215,6 +215,8 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv if (req.method === "GET" && req.url?.startsWith("/__bili/logs")) return sendWebLogs(res, req); if (req.method === "GET" && req.url?.startsWith("/__bili/diagnostics")) return sendDiagnostics(res, req.url); if (req.method === "GET" && req.url?.startsWith("/__bili/sessions/") && req.url.endsWith("/detail")) return sendWebSessionDetail(res, req.url); + if (req.method === "GET" && req.url?.startsWith("/__bili/sessions/") && req.url.split("?")[0].endsWith("/block-content")) return sendWebBlockContent(res, req.url); + if (req.method === "GET" && req.url?.startsWith("/__bili/sessions/") && req.url.split("?")[0].endsWith("/block-ref")) return sendWebBlockRef(res, req.url); if (req.method === "GET" && req.url === "/") { // Browser visits root → redirect to the web UI. curl / health probes // (Accept: */* or no Accept) still get the JSON health check so @@ -1202,6 +1204,86 @@ async function sendWebSessionDetail(res: http.ServerResponse, url: string): Prom res.end(JSON.stringify(detail, null, 2)); } +function parseBlockUrl(url: string): { id: string; params: URLSearchParams } | null { + let u: URL; + try { + u = new URL(url, "http://localhost"); + } catch { + return null; + } + const m = /^\/__bili\/sessions\/([^/]+)\/(?:block-content|block-ref)$/.exec(u.pathname); + if (!m) return null; + let id = ""; + try { + id = decodeURIComponent(m[1]); + } catch { + // malformed percent-encoding → treat as unknown session (404 below) + } + return { id, params: u.searchParams }; +} + +async function sendWebBlockContent(res: http.ServerResponse, url: string): Promise { + const parsed = parseBlockUrl(url); + if (!parsed) { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "not found" })); + return; + } + const blockId = parsed.params.get("block"); + const mode = parsed.params.get("mode"); + if (!blockId || !mode) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "missing block or mode query parameter" })); + return; + } + const r = await buildBlockContent(parsed.id, blockId, mode); + if (r.status === "unknown-session") { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: `unknown session: ${parsed.id}` })); + return; + } + if (r.status === "unknown-block") { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: `unknown block: ${r.blockId}` })); + return; + } + if (r.status === "bad-mode") { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: `invalid mode: ${r.mode} (expected one|full)` })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(r.content, null, 2)); +} + +async function sendWebBlockRef(res: http.ServerResponse, url: string): Promise { + const parsed = parseBlockUrl(url); + if (!parsed) { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "not found" })); + return; + } + const q = parsed.params.get("q"); + if (!q || !q.trim()) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "missing q query parameter" })); + return; + } + const r = await buildRefLookup(parsed.id, q); + if (r.status === "unknown-session") { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: `unknown session: ${parsed.id}` })); + return; + } + if (r.status === "bad-query") { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "empty q query parameter" })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(r, null, 2)); +} + async function sendSessionReport(res: http.ServerResponse, url: string): Promise { const q = new URL(url, "http://localhost").searchParams; const id = q.get("session"); diff --git a/src/web/client.ts b/src/web/client.ts index 5d6f8906fa..0630987e0f 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -1929,6 +1929,17 @@ export const WEB_CLIENT = `(function () { }); } parts.push(""); + // #3917: compression structure — tier tree over directBlockIds, lazy + // originals (one/full), mNNNNN locate, JSON export of the current view. + parts.push('
' + t("det.cstruct_title") + '' + t("det.cstruct_hint") + '
'); + if (!blocks.length) { + parts.push('
' + t("det.blocks_empty") + "
"); + } else { + parts.push('
"); + parts.push('
'); + parts.push(buildCstructTree(blocks)); + } + parts.push("
"); if (Array.isArray(d.conflicts) && d.conflicts.length > 0) { // #2102: per-session evidence rows — the banner aggregates across sessions, // so this page is where its detail pointer lands. @@ -1969,6 +1980,7 @@ export const WEB_CLIENT = `(function () { host.innerHTML = buildDetailHtml(d); bindHandoffActions(d); bindBlocksActions(d); + bindCstructActions(d); bindCacheReportActions(d); bindSessionReportActions(d); bindTrajectoryBrush(d); @@ -2080,6 +2092,35 @@ export const WEB_CLIENT = `(function () { d.blockDetails.forEach((b) => { L.push(""); L.push(buildBlockMd(b)); }); return L.join("\\n"); } + // #3917: tier tree over directBlockIds. Roots = blocks no other block lists + // as a child (orphans whose parent was pruned surface as roots too). + function buildCstructTree(blocks) { + const byId = {}; + blocks.forEach((b) => { byId[b.blockId] = b; }); + const hasParent = new Set(); + blocks.forEach((b) => (Array.isArray(b.children) ? b.children : []).forEach((c) => hasParent.add(c))); + const roots = blocks.filter((b) => !hasParent.has(b.blockId)); + const node = (b, depth) => { + const refRange = refRangeOf(b); + const badge = b.active + ? '' + t("det.block_active") + "" + : '' + t("det.block_inactive") + ""; + const extBadge = b.external ? '' + t("det.block_external") + "" : ""; + // Same row shape as the flat 压缩块 list (class block-item) so the + // shared typography applies; cstruct-node adds tree behavior. + let html = '
' + escapeHtml(b.blockId) + '' + badge + extBadge + '' + escapeHtml(blockTopic(b)) + 'T' + String(b.tier) + " · " + fmtW(b.compressedTokens) + " · " + timeAgo(b.createdAt) + (refRange ? " · " + escapeHtml(refRange) : "") + '
'; + html += renderBlockMd(b.summary || ""); + html += '
'; + html += "
"; + (Array.isArray(b.children) ? b.children : []).forEach((cid) => { + const cb = byId[cid]; + if (cb) html += node(cb, depth + 1); + }); + html += "
"; + return html; + }; + return roots.map((r) => node(r, 0)).join(""); + } // #3057 report export: MD/SVG render server-side (/__bili/session-report); the browser downloads MD and rasterizes the closed-vector SVG to PNG. function downloadUrl(url, name) { var a = document.createElement("a"); @@ -2156,6 +2197,98 @@ export const WEB_CLIENT = `(function () { btn.addEventListener("click", (ev) => { ev.preventDefault(); ev.stopPropagation(); copyText(buildBlockMd(b), btn); }); }); } + // #3917: structure-tab actions — mNNNNN locate (exact id-set membership on + // the server), lazy originals per node, JSON export of the current view + // (blockDetails + whatever originals were loaded during this page visit). + function bindCstructActions(d) { + const findBtn = $("cstruct-find"); + if (!findBtn) return; + const qEl = $("cstruct-q"); + const status = $("cstruct-status"); + const originals = {}; + const nodeOf = (blockId) => document.querySelector('.cstruct-node[data-block="' + blockId + '"]'); + const parentIdOf = (blockId) => { + for (const b of d.blockDetails || []) { + if ((Array.isArray(b.children) ? b.children : []).includes(blockId)) return b.blockId; + } + return null; + }; + const clearHits = () => document.querySelectorAll(".cstruct-node.hit").forEach((n) => n.classList.remove("hit")); + const locate = async () => { + const q = String(qEl.value || "").trim(); + if (!q) return; + clearHits(); + status.textContent = ""; + busy(findBtn, true); + try { + const r = await json(API_ROOT + "/sessions/" + encodeURIComponent(d.id) + "/block-ref?q=" + encodeURIComponent(q)); + const matches = Array.isArray(r.matches) ? r.matches : []; + if (!matches.length) { + status.textContent = q + " — " + t("det.cstruct_search_notfound"); + return; + } + matches.forEach((m) => { + const n = nodeOf(m.blockId); + if (n) n.classList.add("hit"); + }); + const owner = r.owner ? r.owner.blockId : matches[matches.length - 1].blockId; + for (let p = owner; p; p = parentIdOf(p)) { + const n = nodeOf(p); + if (n) n.open = true; else break; + } + status.textContent = q + " — " + t("det.cstruct_search_found", { n: matches.length, owner: owner }); + const target = nodeOf(owner); + if (target && target.scrollIntoView) target.scrollIntoView({ block: "center", behavior: "smooth" }); + } catch (e) { + toast(friendlyMsg(e), "err"); + } finally { + busy(findBtn, false); + } + }; + findBtn.addEventListener("click", locate); + qEl.addEventListener("keydown", (ev) => { if (ev.key === "Enter") locate(); }); + document.querySelectorAll(".cstruct-load").forEach((btn) => { + btn.addEventListener("click", async () => { + const blockId = btn.getAttribute("data-block"); + const mode = btn.getAttribute("data-mode"); + const host = document.querySelector('.cstruct-orig[data-orig="' + blockId + '"] .cstruct-orig-body'); + if (!host) return; + busy(btn, true); + try { + const key = blockId + ":" + mode; + let c = originals[key]; + if (!c) { + c = await json(API_ROOT + "/sessions/" + encodeURIComponent(d.id) + "/block-content?block=" + encodeURIComponent(blockId) + "&mode=" + mode); + originals[key] = c; + } + if (c.missing) { + host.innerHTML = '
' + t("det.cstruct_no_content") + "
"; + return; + } + let note = ""; + if (c.identicalToFull) note += '
(' + t("det.cstruct_same_as_full") + ")
"; + if (c.truncated) note += '
' + t("det.cstruct_truncated") + "
"; + host.innerHTML = note + '
' + renderBlockMd(String(c.text || "")) + "
"; + } catch (e) { + toast(friendlyMsg(e), "err"); + } finally { + busy(btn, false); + } + }); + }); + const exBtn = $("cstruct-export"); + if (exBtn) exBtn.addEventListener("click", () => { + const payload = { + session: d.id, + ...(d.title ? { title: d.title } : {}), + generatedAt: new Date().toISOString(), + blocks: d.blockDetails || [], + originals: Object.keys(originals).map((k) => ({ blockId: k.split(":")[0], mode: k.split(":")[1], content: originals[k] })), + }; + const url = URL.createObjectURL(new Blob([JSON.stringify(payload, null, 2)], { type: "application/json;charset=utf-8" })); + downloadUrl(url, "billion-context-cstruct-" + String(d.id).replace(/[^A-Za-z0-9._-]/g, "_") + ".json"); + }); + } function bindCacheReportActions(d) { const copyB = $("cacherpt-copy"); const dlB = $("cacherpt-dl"); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index aad6cc658a..8fe03fb6cc 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -356,6 +356,18 @@ export const MESSAGES: Record> = { "det.rpt_missed": "未命中", "det.rpt_hit": "命中率", "det.blocks_copy_md": "复制 Markdown", + "det.cstruct_title": "压缩结构", + "det.cstruct_hint": "层级视图:点开看摘要,按需加载原文,可定位 mNNNNN 并导出当前视图", + "det.cstruct_search_ph": "定位 mNNNNN 或原始消息 ID", + "det.cstruct_search_btn": "定位", + "det.cstruct_search_found": "{n} 个块包含该消息,所属块 {owner}", + "det.cstruct_search_notfound": "该消息不在任何压缩块中(可能仍在活动上下文里)", + "det.cstruct_load_one": "加载一级原文", + "det.cstruct_load_full": "加载完整原文", + "det.cstruct_truncated": "⚠️ 超过 2 MiB 已截断", + "det.cstruct_same_as_full": "与完整原文相同", + "det.cstruct_no_content": "该块没有持久化原文", + "det.cstruct_export": "导出当前视图 (.json)", "det.handoff": "会话交接文档", "det.handoff_hint": "与 bili export 相同的内容 —— 复制粘贴到新会话即可继续", "det.thinking": "思考", @@ -851,6 +863,18 @@ export const MESSAGES: Record> = { "det.rpt_missed": "missed", "det.rpt_hit": "hit %", "det.blocks_copy_md": "copy Markdown", + "det.cstruct_title": "Compression structure", + "det.cstruct_hint": "Tier tree: open a node for its summary, load originals on demand, locate mNNNNN refs, export the current view", + "det.cstruct_search_ph": "Locate mNNNNN or raw message id", + "det.cstruct_search_btn": "Locate", + "det.cstruct_search_found": "{n} block(s) contain it; owning block {owner}", + "det.cstruct_search_notfound": "not inside any compressed block (it may still be live context)", + "det.cstruct_load_one": "Load one-level originals", + "det.cstruct_load_full": "Load full originals", + "det.cstruct_truncated": "⚠️ truncated beyond 2 MiB", + "det.cstruct_same_as_full": "identical to full", + "det.cstruct_no_content": "this block has no persisted originals", + "det.cstruct_export": "Export current view (.json)", "det.handoff": "Handoff document", "det.handoff_hint": "Same content as bili export — paste into a new session to continue", "det.thinking": "thinking", diff --git a/src/web/index.ts b/src/web/index.ts index 708dd5475b..d71d2a6379 100644 --- a/src/web/index.ts +++ b/src/web/index.ts @@ -9,7 +9,7 @@ export { readUpstreamSettings, } from "./api.js"; -export { buildOverview, buildSessionList, buildSessionPage, buildSessionDetail, hiddenEmptyCount } from "./sessions-data.js"; +export { buildOverview, buildSessionList, buildSessionPage, buildSessionDetail, buildBlockContent, buildRefLookup, hiddenEmptyCount } from "./sessions-data.js"; export function renderUI(origin: string, opts?: { embed?: boolean; theme?: "light" | "dark" }): string { // #1426 fix: reuse the bundle-safe VERSION from src/version.ts — resolving diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 237ce9c60d..54106a26bb 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -211,6 +211,10 @@ interface WebSessionDetail extends WebSessionSummary { /** Present when the block's summary was written by the external * summary chain — the web block badge mirrors /acp's "⚡ext". */ external?: true; + /** #3917: child block ids (the blocks this block absorbed when it was + * compressed) — the edge source for the structure tree view; the + * kernel carries no parentBlockId, so children come from directBlockIds. */ + children: string[]; }>; } @@ -924,6 +928,31 @@ export async function buildOverview(): Promise { const detailInFlight = new Map>(); +/** #3917: resolve ONE session for read-only web access without creating it — + * live pool → unified/disk store → disk-index file (#1937: never a directory + * scan for a single id). Shared by the detail page and the block-content / + * ref-lookup endpoints so all three agree on what "exists" means. */ +async function resolveSessionForWeb(id: string): Promise { + const live = listSessions().find((s) => s.id === id); + let session: Session | undefined = live; + if (!session) { + // #2960 R2.2: unified-first — in full mode loadSync resolves the + // index row even when the legacy file is gone; in legacy/shadow it + // composes an origin-less path and misses, falling through to the + // disk-index file below. + session = getDiskStore().loadSync(id) ?? undefined; + } + if (!session) { + const files = await ensureIndex(); + const abs = byId?.get(id); + if (abs && files.has(abs)) { + detailDecodes++; + session = (await getDiskStore().loadSessionFromFile(abs, id)) ?? undefined; + } + } + return session; +} + /** 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 — @@ -935,22 +964,7 @@ export function buildSessionDetail(id: string): Promise if (existing) return existing; const run = (async (): Promise => { const live = listSessions().find((s) => s.id === id); - let session: Session | undefined = live; - if (!session) { - // #2960 R2.2: unified-first — in full mode loadSync resolves the - // index row even when the legacy file is gone; in legacy/shadow it - // composes an origin-less path and misses, falling through to the - // disk-index file below. - session = getDiskStore().loadSync(id) ?? undefined; - } - if (!session) { - const files = await ensureIndex(); - const abs = byId?.get(id); - if (abs && files.has(abs)) { - detailDecodes++; - session = (await getDiskStore().loadSessionFromFile(abs, id)) ?? undefined; - } - } + const session = await resolveSessionForWeb(id); if (!session) return null; return renderDetail(session, !!live); })(); @@ -1056,7 +1070,111 @@ function renderDetail(session: Session, live: boolean): WebSessionDetail { ...(span === null && b.endRef !== undefined ? { endRef: b.endRef } : {}), active: b.active, ...(isExternalSummaryBlock(b) ? { external: true } : {}), + // #3917: parent-child wiring for the structure tree view — the + // kernel has no parentBlockId, so children are the edge source. + // Sparse/legacy block records may lack the field entirely. + children: [...(b.directBlockIds ?? [])], }; }), }; } + +// --------------------------------------------------------------------------- +// #3917 compression structure: lazy originals + message→block lookup +// --------------------------------------------------------------------------- + +/** Response guard: one lazy-loaded original view must never exceed 2 MiB of + * JSON — beyond that the browser tab dies before the user sees anything. */ +const BLOCK_CONTENT_MAX_CHARS = 2 * 1024 * 1024; + +type BlockContentMode = "one" | "full"; + +export interface WebBlockContent { + blockId: string; + mode: BlockContentMode; + /** True when the block exists but carries no persisted originals + * (text/count absent) — e.g. blocks whose content was never stored. */ + missing?: true; + /** Number of messages in the served view. */ + count?: number; + /** Served text (truncated to BLOCK_CONTENT_MAX_CHARS when larger). */ + text?: string; + /** True when text was cut down to the guard above. */ + truncated?: true; + /** mode=one only: the one-level view is byte-identical to full and was + * persisted as a single copy (#401) — same bytes, served under "one". */ + identicalToFull?: true; +} + +export type WebBlockContentResult = + | { status: "ok"; content: WebBlockContent } + | { status: "unknown-session" } + | { status: "unknown-block"; blockId: string } + | { status: "bad-mode"; mode: string }; + +/** Lazy fetch of ONE block's originals in the requested view: "one" = direct + * messages + nested child summaries (null ⇒ byte-identical to full, then + * full's bytes are served with a marker), "full" = every original message. + * Read-only: resolves exactly like the detail page, never creates sessions. */ +export async function buildBlockContent(id: string, blockId: string, mode: string): Promise { + if (mode !== "one" && mode !== "full") return { status: "bad-mode", mode }; + const session = await resolveSessionForWeb(id); + if (!session) return { status: "unknown-session" }; + const block = session.state.blocks.find((b) => b.blockId === blockId); + if (!block) return { status: "unknown-block", blockId }; + const content = session.blockContents.get(blockId); + if (!content) return { status: "ok", content: { blockId, mode: mode as BlockContentMode, missing: true } }; + let view = mode === "full" ? content.full : content.one; + let identicalToFull = false; + if (view === null) { + view = content.full; + identicalToFull = true; + } + const truncated = view.text.length > BLOCK_CONTENT_MAX_CHARS; + return { + status: "ok", + content: { + blockId, + mode: mode as BlockContentMode, + count: view.count, + text: truncated ? view.text.slice(0, BLOCK_CONTENT_MAX_CHARS) : view.text, + ...(truncated ? { truncated: true as const } : {}), + ...(identicalToFull ? { identicalToFull: true as const } : {}), + }, + }; +} + +interface WebRefMatch { + blockId: string; + tier: number; + active: boolean; +} + +export type WebRefLookupResult = + | { status: "ok"; query: string; kind: "ref" | "raw"; owner?: WebRefMatch; matches: Array } + | { status: "unknown-session" } + | { status: "bad-query" }; + +/** Locate ONE message inside the compression structure. Accepts a padded ref + * ("m00042", resolved through state.messageRefs.byRef) or a raw id verbatim. + * Membership is EXACT over each block's effectiveMessageIds (direct + nested) + * — no span guessing — so every containing block (leaf + all ancestors) is + * reported; `owner` is the deepest one (smallest id set). */ +export async function buildRefLookup(id: string, query: string): Promise { + const session = await resolveSessionForWeb(id); + if (!session) return { status: "unknown-session" }; + const q = query.trim(); + if (!q) return { status: "bad-query" }; + const kind = /^m\d+$/.test(q) ? "ref" as const : "raw" as const; + const rawId = kind === "ref" ? session.state.messageRefs.byRef[q] : q; + if (!rawId) return { status: "ok", query: q, kind, matches: [] }; + const matches = session.state.blocks + .filter((b) => b.effectiveMessageIds.includes(rawId)) + .map((b) => ({ blockId: b.blockId, tier: b.tier, active: b.active })); + if (matches.length > 0) { + const sizeOf = (m: WebRefMatch) => session.state.blocks.find((b) => b.blockId === m.blockId)?.effectiveMessageIds.length ?? Infinity; + const owner = [...matches].sort((a, b) => sizeOf(a) - sizeOf(b) || b.tier - a.tier)[0]; + return { status: "ok", query: q, kind, owner, matches }; + } + return { status: "ok", query: q, kind, matches: [] }; +} diff --git a/src/web/styles.ts b/src/web/styles.ts index 41675a28b0..fa43707822 100644 --- a/src/web/styles.ts +++ b/src/web/styles.ts @@ -249,6 +249,10 @@ td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; font-fam .block-item .body.md hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; } .block-item .body.md strong { color: var(--text); } +/* #3917 compression structure tree: rows reuse .block-item; only the locate highlight is new */ +.cstruct-node.hit summary { background: rgba(212, 167, 44, 0.18); } +.cstruct-node.hit summary .bid { color: #d4a72c; } + .handoff { font-size: 13.5px; line-height: 1.65; } .handoff h1, .handoff h2, .handoff h3, .handoff h4 { margin: 18px 0 8px; line-height: 1.3; } .handoff h1 { font-size: 17px; } .handoff h2 { font-size: 15.5px; } .handoff h3 { font-size: 14px; } diff --git a/tests/web-sessions.test.ts b/tests/web-sessions.test.ts index df0fd99887..cb4f228548 100644 --- a/tests/web-sessions.test.ts +++ b/tests/web-sessions.test.ts @@ -16,7 +16,8 @@ import { getSession, _resetSessionsForTest } from "../src/session.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { markdownToHtml } from "../src/web/markdown.ts"; import { WEB_CLIENT } from "../src/web/client.ts"; -import { buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, hiddenEmptyCount, _resetDiskCacheForTest, _diskScanStatsForTest } from "../src/web/sessions-data.ts"; +import { buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, hiddenEmptyCount, _resetDiskCacheForTest, _diskScanStatsForTest, buildBlockContent, buildRefLookup } from "../src/web/sessions-data.ts"; +import type { WebBlockContent, WebBlockContentResult, WebRefLookupResult } from "../src/web/sessions-data.ts"; import vm from "node:vm"; import { rmrf } from "./tmp-rm.ts"; @@ -1120,3 +1121,215 @@ withFullMode("R2.2 file-less rows: web list and detail survive a vanished legacy assert.equal(detail.blockDetails[0].blockId, "b1"); assert.equal(detail.blockDetails[0].compressedTokens, 2500); }); + +// #3917 compression structure: tier-tree children in the detail payload, lazy +// per-block originals (one|full, 2 MiB guard) and exact mNNNNN/raw-id lookup. + +// A tier-2 parent absorbing a tier-1 child plus one standalone leaf. raw-1/raw-2 +// live in both parent and child; raw-3 only in the parent; raw-99 is known to +// messageRefs but sits in no block. b1/b2 carry persisted contents (b2's one-view +// is null = byte-identical to full); b3 has none. +function mkCstructSession(id: string): Session { + const s = makeSession(id, { protocol: "openai", title: "Cstruct" }, { requests: 3, inputTokens: 900, cachedTokens: 300, contextTokens: 400 }); + const parent = mkBlock("b1", 5000, true, "Parent topic"); + parent.tier = 2; + parent.directBlockIds = ["b2"]; + parent.effectiveMessageIds = ["raw-1", "raw-2", "raw-3"]; + const child = mkBlock("b2", 2000, true, "Child topic"); + child.directMessageIds = ["raw-1", "raw-2"]; + child.effectiveMessageIds = ["raw-1", "raw-2"]; + const solo = mkBlock("b3", 700, false, "Solo"); + s.state.blocks.push(parent, child, solo); + s.state.messageRefs.byRef["m00001"] = "raw-1"; + s.state.messageRefs.byRef["m00002"] = "raw-2"; + s.state.messageRefs.byRef["m00003"] = "raw-3"; + s.state.messageRefs.byRef["m00099"] = "raw-99"; + for (const [ref, raw] of Object.entries(s.state.messageRefs.byRef)) s.state.messageRefs.byRaw[raw] = ref; + s.blockContents.set("b1", { one: { text: "ONE-B1", count: 6 }, full: { text: "FULL-B1", count: 9 } }); + s.blockContents.set("b2", { one: null, full: { text: "FULL-B2", count: 3 } }); + return s; +} + +function assertOkContent(r: WebBlockContentResult): WebBlockContent { + assert.equal(r.status, "ok"); + if (r.status !== "ok") throw new Error("unreachable: expected ok content result"); + return r.content; +} + +function assertOkLookup(r: WebRefLookupResult): Extract { + assert.equal(r.status, "ok"); + if (r.status !== "ok") throw new Error("unreachable: expected ok lookup result"); + return r; +} + +withSessionsDir("compression structure detail exposes tier-tree children (#3917)", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(mkCstructSession("cs-1")); + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + const d = await buildSessionDetail("cs-1"); + assert.ok(d); + const byId = Object.fromEntries(d.blockDetails.map((b) => [b.blockId, b])); + assert.deepEqual(byId.b1.children, ["b2"], "tier-2 parent lists its absorbed child"); + assert.deepEqual(byId.b2.children, [], "leaf has no children"); + assert.deepEqual(byId.b3.children, [], "standalone block has no children"); +}); + +withSessionsDir("buildBlockContent serves lazy originals with the 2 MiB guard (#3917)", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(mkCstructSession("cb-1")); + const big = makeSession("cb-big", { protocol: "openai" }); + big.state.blocks.push(mkBlock("b1", 100, true)); + big.blockContents.set("b1", { one: null, full: { text: "x".repeat(2 * 1024 * 1024 + 1000), count: 1 } }); + await store.writeNow(big); + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + + const full = assertOkContent(await buildBlockContent("cb-1", "b1", "full")); + assert.equal(full.mode, "full"); + assert.equal(full.text, "FULL-B1"); + assert.equal(full.count, 9); + assert.equal(full.truncated, undefined); + assert.equal(full.identicalToFull, undefined); + + const one = assertOkContent(await buildBlockContent("cb-1", "b1", "one")); + assert.equal(one.text, "ONE-B1"); + assert.equal(one.count, 6); + + // one === null means the single persisted copy IS the full view (#401) + const same = assertOkContent(await buildBlockContent("cb-1", "b2", "one")); + assert.equal(same.text, "FULL-B2"); + assert.equal(same.identicalToFull, true); + + const missing = assertOkContent(await buildBlockContent("cb-1", "b3", "full")); + assert.equal(missing.missing, true); + + assert.deepEqual(await buildBlockContent("nope", "b1", "full"), { status: "unknown-session" }); + assert.deepEqual(await buildBlockContent("cb-1", "bX", "full"), { status: "unknown-block", blockId: "bX" }); + // mode validation runs before session resolution + assert.deepEqual(await buildBlockContent("nope", "b1", "bogus"), { status: "bad-mode", mode: "bogus" }); + + const trunc = assertOkContent(await buildBlockContent("cb-big", "b1", "full")); + assert.equal(trunc.truncated, true); + assert.ok(trunc.text, "truncated view still carries text"); + assert.equal(trunc.text.length, 2 * 1024 * 1024); + assert.equal(trunc.count, 1, "count reflects the untruncated view"); +}); + +withSessionsDir("buildRefLookup resolves mNNNNN/raw ids to exact owning blocks (#3917)", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + await store.writeNow(mkCstructSession("cs-2")); + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + + const ref = assertOkLookup(await buildRefLookup("cs-2", "m00001")); + assert.equal(ref.kind, "ref"); + assert.deepEqual(ref.matches.map((m) => m.blockId).sort(), ["b1", "b2"], "every containing block reported"); + assert.equal(ref.owner?.blockId, "b2", "deepest containing block owns"); + + const raw = assertOkLookup(await buildRefLookup("cs-2", "raw-2")); + assert.equal(raw.kind, "raw"); + assert.deepEqual(raw.matches.map((m) => m.blockId).sort(), ["b1", "b2"]); + + // raw-3 exists only inside the tier-2 parent + const parentOnly = assertOkLookup(await buildRefLookup("cs-2", "m00003")); + assert.deepEqual(parentOnly.matches.map((m) => m.blockId), ["b1"]); + assert.equal(parentOnly.owner?.blockId, "b1"); + + // known ref with no block membership / unknown raw id → honest empty set + assert.deepEqual(assertOkLookup(await buildRefLookup("cs-2", "m00099")).matches, []); + assert.deepEqual(assertOkLookup(await buildRefLookup("cs-2", "raw-none")).matches, []); + + assert.equal((await buildRefLookup("cs-2", "")).status, "bad-query"); + assert.equal((await buildRefLookup("cs-2", " ")).status, "bad-query"); + assert.equal((await buildRefLookup("nope", "m00001")).status, "unknown-session"); +}); + +test("web endpoints serve lazy block content and ref lookup (#3917)", async () => { + const root = path.join(tmpdir(), `bili-web-sess-cstruct-${process.pid}-${Date.now()}`); + mkdirSync(root, { recursive: true }); + const biliConfig = path.join(root, "billion-context.json"); + writeFileSync(biliConfig, '{"providers":{}}\n', "utf8"); + const prevConfig = process.env.BILI_CONFIG_FILE; + const prevSessions = process.env.BILI_SESSIONS_DIR; + process.env.BILI_CONFIG_FILE = biliConfig; + process.env.BILI_SESSIONS_DIR = root; + + const store = new SessionStore({ dir: root, debounceMs: 0, enabled: true }); + await store.writeNow(mkCstructSession("ep-cs")); + _resetDiskCacheForTest(); + _setStoreForTest(new SessionStore({ enabled: false })); + 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, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: 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 bcRes = await fetch(`${base}/__bili/sessions/ep-cs/block-content?block=b1&mode=full`); + assert.equal(bcRes.status, 200); + const bc = (await bcRes.json()) as { text: string; count: number }; + assert.equal(bc.text, "FULL-B1"); + assert.equal(bc.count, 9); + + const oneRes = await fetch(`${base}/__bili/sessions/ep-cs/block-content?block=b1&mode=one`); + assert.equal(oneRes.status, 200); + assert.equal(((await oneRes.json()) as { text: string }).text, "ONE-B1"); + + const missBlock = await fetch(`${base}/__bili/sessions/ep-cs/block-content?block=bX&mode=full`); + assert.equal(missBlock.status, 404); + assert.match(((await missBlock.json()) as { error: string }).error, /unknown block/); + + const badMode = await fetch(`${base}/__bili/sessions/ep-cs/block-content?block=b1&mode=bogus`); + assert.equal(badMode.status, 400); + assert.match(((await badMode.json()) as { error: string }).error, /invalid mode/); + + const noParam = await fetch(`${base}/__bili/sessions/ep-cs/block-content?block=b1`); + assert.equal(noParam.status, 400); + + const missSess = await fetch(`${base}/__bili/sessions/nope/block-ref?q=m00001`); + assert.equal(missSess.status, 404); + assert.match(((await missSess.json()) as { error: string }).error, /unknown session/); + + const refRes = await fetch(`${base}/__bili/sessions/ep-cs/block-ref?q=m00001`); + assert.equal(refRes.status, 200); + const refBody = (await refRes.json()) as { owner?: { blockId: string }; matches: Array<{ blockId: string }> }; + assert.equal(refBody.owner?.blockId, "b2"); + assert.deepEqual(refBody.matches.map((m) => m.blockId).sort(), ["b1", "b2"]); + + const noQ = await fetch(`${base}/__bili/sessions/ep-cs/block-ref`); + assert.equal(noQ.status, 400); + } 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; + await close(proxy); + _resetDiskCacheForTest(); + rmrf(root); + } +});