Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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<PersistedSession>;
private readonly log: Logger;
private readonly staleWarnAt = new Map<string, number>();
Expand All @@ -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
Expand All @@ -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<PersistedSession>({
dir: this.dir,
dir: this.sessionsDir,
version: PERSIST_VERSION,
debounceMs: Math.max(0, debounceMs),
enabled: this.enabled,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -420,7 +436,7 @@ export class SessionStore {
private async sweepStaleTemps(): Promise<void> {
let files: string[];
try {
files = await walkJsonFiles(this.dir);
files = await walkJsonFiles(this.sessionsDir);
} catch {
return;
}
Expand Down Expand Up @@ -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<string, PersistedEnvelope<PersistedSession>>): Promise<void> {
Expand Down Expand Up @@ -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).
Expand All @@ -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(() => {});
}
}

Expand Down Expand Up @@ -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<Session | null> {
const parsed = await this.readRawFile(file);
if (!parsed || typeof parsed !== "object") return null;
const p = parsed as Record<string, unknown>;
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
Expand Down Expand Up @@ -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<PersistedSession>;
return typeof r.id === "string" && typeof r.state === "object" && r.state !== null && Array.isArray(r.state.blocks);
Expand Down
41 changes: 35 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 === "/") {
Expand Down Expand Up @@ -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,
Expand All @@ -7646,10 +7654,31 @@ async function sendOverview(res: http.ServerResponse, opts: ProxyOptions): Promi
}, null, 2));
}

async function sendWebSessions(res: http.ServerResponse): Promise<void> {
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<void> {
const u = new URL(req.url ?? "/__bili/sessions", "http://localhost");
let body: Record<string, unknown>;
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
Expand Down
Loading
Loading