LibreChat/e2e/client-build.test.mjs
Danny Avila 741978c954
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
🔭 feat: Recover Stale Chunks Without the Error Page and Ship Client Logs to RUM (#16701)
* 🔭 feat: Recover Stale Chunks Without the Error Page and Ship Client Logs to RUM

Stale chunks: a tab left open across a deploy that opens a code-split panel
requests a hashed chunk the new deploy removed. Vite's preload handler hands
the failure to the stale-asset recovery, which reloads, but the import then
resolves to `undefined` and `React.lazy` throws a TypeError into the route
error boundary, so the error page flashes before the reload. Lazy imports now
go through `lazyWithRecovery`/`importWithRecovery`, which turn that result into
a `ChunkLoadError`; the route boundary recognizes chunk failures (and any error
while a recovery reload is underway), asks the page recovery once, and renders
a localized "Updating to the latest version…" status. When the loop guard
declines, the normal error page with its reload action shows instead. The
page recovery is now idempotent within a page, so concurrent callers join the
in-flight reload rather than tripping the guard.

Client logs: with RUM in proxy mode, the client logger's warn/error calls,
error-boundary catches and stale-asset recovery events are exported as OTLP
log records through the authenticated `/api/rum/v1/logs` proxy. Records carry
allowlisted, scrubbed fields only, follow RUM session sampling, dedupe repeats
with a count, are budgeted per minute and per page, batch with backoff, flush
with keepalive on page hide, and stop after persistent failures. The proxy
now refuses non-OTLP content types and applies a per-user rate limit.
`RUM_CLIENT_LOGS=false` turns the export off.

* 🔒 fix: Keep Client Logs Opt-In and Drop Application Error Messages

- RUM_CLIENT_LOGS now defaults to off, like console capture, so enabling
  RUM proxy mode never starts a new log stream on its own.
- Error messages are exported only for engine, browser and library
  generated types (TypeError, SyntaxError, DOMException, AxiosError,
  chunk failures); application errors keep type, status and stack only,
  since their messages can echo prompt or response text. All quoted
  fragments are now redacted.
- Batches are measured in encoded UTF-8 bytes of the exact request body,
  and page hide drains every queued record that fits the keepalive quota.
- Queued stale-asset events are delivered as soon as the log exporter
  starts, independent of the RUM SDK loading, and marked so they are
  never logged twice.

* 🔒 fix: Export Only Known Generated Error Messages and Ack Queued Events

- Error messages are exported only when the whole text matches a known
  engine, browser or library message shape (property access on
  undefined, "x is not a function", fetch/abort/axios status, chunk and
  CSS preload failures). The error class alone no longer qualifies, since
  any code can throw a TypeError carrying arbitrary text.
- Queued stale-asset events are marked logged only after the collector
  accepts the record carrying them, so an undelivered event replays on the
  next page; in-flight events are skipped by the SDK path.
- A fractional RUM_PROXY_USER_MAX below one falls back to the default
  instead of flooring to a limit of zero.

* 🔒 fix: Hold Unacknowledged Asset Events, Re-send In-Flight Logs on Hide

- The RUM SDK flush no longer drops queued stale-asset events whose client
  log record is still unacknowledged: they stay queued and persisted,
  flagged so their SDK action is not repeated, and leave the queue once
  the collector accepts the record.
- Page hide re-sends the records of an unanswered regular request with
  keepalive; each record belongs to the latest request carrying it, so a
  late response to the original request neither marks nor requeues it.
- Client-log export stops when the authenticated layout unmounts.
- The proxy refuses /v1/logs while client logs, console capture and replay
  are all off, so turning RUM_CLIENT_LOGS off also stops tabs that still
  hold an older startup config.

* 🔒 fix: Export Only Complete Stack Frames, Never the Message Header

Stack reduction now strips the V8 `name: message` header (including every
line of a multi-line message) and keeps only lines with a complete V8,
Gecko or WebKit frame shape that ends in a URL or absolute path with a
line number. A message ending in a source-position-like suffix can no
longer pass as a frame.

* fix: centralize RUM delivery and asset recovery lifecycle

* fix: preserve RUM page lifetime and collector acknowledgements

* fix: retain page transport quotas and recover functional chunks

* fix: scope RUM client sessions to authenticated identity

---------

Co-authored-by: Lia <lia@librechat.ai>
2026-10-03 23:17:56 -04:00

249 lines
11 KiB
JavaScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { createServer } from 'node:http';
import { mkdtemp, mkdir, readFile, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { chromium } from '@playwright/test';
import { build } from 'vite';
const root = fileURLToPath(new URL('../', import.meta.url));
// A small production-build fixture exercises the actual bootstrap and worker recovery code
// without requiring a database or identity provider. It does not simulate an active model run.
test(
'an old tab retains its bundle identity and draft across a worker update',
/** Allow a slow CI browser launch plus both activations; individual browser waits remain bounded. */
{ timeout: 90000 },
async () => {
const temporary = await mkdtemp(path.join(tmpdir(), 'librechat-builds-'));
const appHtml = await readFile(path.join(root, 'client/index.html'), 'utf8');
const guards = [...appHtml.matchAll(/<script>([\s\S]*?)<\/script>/g)]
.map((match) => match[0])
.join('\n');
const heal = await readFile(path.join(root, 'client/sw/heal.js'), 'utf8');
let serving = 'A';
let browser;
const server = createServer(async (req, res) => {
const pathname = new URL(req.url, 'http://localhost').pathname;
try {
const relative = pathname.startsWith('/assets/') ? pathname.slice(1) : 'index.html';
const filename = pathname === '/sw.js' ? 'sw.js' : relative;
if (filename.includes('..')) {
res.writeHead(400).end();
return;
}
const content = await readFile(path.join(temporary, serving, 'dist', filename));
res.setHeader('Content-Type', filename.endsWith('.js') ? 'text/javascript' : 'text/html');
res.setHeader('Cache-Control', 'no-store');
res.end(content);
} catch {
res.writeHead(404).end();
}
});
try {
for (const version of ['A', 'B']) {
const fixture = path.join(temporary, version);
await mkdir(fixture);
await writeFile(
path.join(fixture, 'index.html'),
`<html><head>${guards}<script data-lc-client-entry type="module" src="/entry.js"></script></head><body><input aria-label="Draft"></body></html>`,
);
await writeFile(
path.join(fixture, 'entry.js'),
`import { installRumBootstrap } from ${JSON.stringify(path.join(root, 'client/src/lib/rum/bootstrap.js'))};
window.__lcRumPush('before-bootstrap');
installRumBootstrap(window);
window.fixtureVersion = ${JSON.stringify(version)};
navigator.serviceWorker.register('/sw.js').then(
registration => { window.fixtureRegistration = registration; },
error => { window.fixtureWorkerError = String(error); },
);`,
);
await build({
root: fixture,
configFile: false,
logLevel: 'silent',
build: {
rollupOptions: { output: { entryFileNames: 'assets/[name].[hash].js' } },
},
});
await writeFile(
path.join(fixture, 'dist/sw.js'),
`${heal}\n// ${version}\nself.skipWaiting();`,
);
}
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
browser = await chromium.launch({
headless: true,
channel: process.env.PLAYWRIGHT_CHANNEL,
// CI runners can take longer than 10s to start Chrome under contention.
// Keep startup independently bounded; page/worker waits still use 10s.
timeout: 30000,
});
const page = await browser.newPage();
page.setDefaultTimeout(10000);
const url = `http://127.0.0.1:${server.address().port}`;
await page.goto(`${url}/c/example`);
await page.waitForFunction(() => window.fixtureVersion === 'A');
/** `ready` has no timeout and a controller can still be activating. Finish
* A's handshake before clearing its events or requesting another worker. */
await page.waitForFunction(
() =>
window.fixtureWorkerError ||
(window.fixtureRegistration?.active?.state === 'activated' &&
navigator.serviceWorker.controller?.state === 'activated'),
);
assert.equal(await page.evaluate(() => window.fixtureWorkerError), undefined);
await page.getByLabel('Draft').fill('Keep my unsent text');
const firstId = await page.evaluate(() => window.__lcRumQueue[0].attributes.clientBuildId);
assert.match(firstId, /^index\..+\.js$/);
assert.equal(
await page.evaluate(
() =>
window.__lcRumQueue.find((event) => event.type === 'before-bootstrap').attributes
.clientBuildId,
),
firstId,
);
await page.evaluate(() => {
window.__lcRumQueue.length = 0;
window.fixturePreviousController = navigator.serviceWorker.controller;
});
serving = 'B';
/** `evaluate` does not bound an awaited update promise. Observe its result
* through the timed wait, and require B's activation, not a late A ping. */
await page.evaluate(() => {
window.fixtureRegistration.update().then(
() => {
window.fixtureUpdateFinished = true;
},
(error) => {
window.fixtureWorkerError = String(error);
},
);
});
await page.waitForFunction(
() =>
window.fixtureWorkerError ||
(window.fixtureUpdateFinished &&
navigator.serviceWorker.controller !== window.fixturePreviousController &&
navigator.serviceWorker.controller?.state === 'activated' &&
window.__lcRumQueue.some((event) => event.type === 'sw-ping')),
);
assert.equal(await page.evaluate(() => window.fixtureWorkerError), undefined);
// Outlive the worker's unresponsive-client deadline to catch an unwanted navigation.
await page.waitForTimeout(2000);
assert.equal(await page.getByLabel('Draft').inputValue(), 'Keep my unsent text');
assert.equal(await page.evaluate(() => window.fixtureVersion), 'A');
await page.evaluate(() => window.__lcRumPush('after-update'));
assert.equal(
await page.evaluate(() => window.__lcRumQueue.at(-1).attributes.clientBuildId),
firstId,
);
await page.goto(`${url}/login?redirect_to=%2Fc%2Fexample`);
await page.waitForFunction(() => window.fixtureVersion === 'B');
const nextId = await page.evaluate(
() =>
window.__lcRumQueue.findLast((event) => event.type === 'inline-start').attributes
.clientBuildId,
);
assert.equal(
await page.evaluate(
() =>
window.__lcRumQueue.find((event) => event.type === 'after-update').attributes
.clientBuildId,
),
firstId,
);
assert.notEqual(nextId, firstId);
assert.match(nextId, /^index\..+\.js$/);
} finally {
await browser?.close();
await new Promise((resolve) => server.close(resolve));
await rm(temporary, { recursive: true, force: true });
}
},
);
// Optional diagnostics catch their own failures. Only an explicitly required import heals the page.
test(
'optional preload failures do not reload or consume recovery for a required chunk',
{ timeout: 60000 },
async () => {
const temporary = await mkdtemp(path.join(tmpdir(), 'librechat-import-policy-'));
const appHtml = await readFile(path.join(root, 'client/index.html'), 'utf8');
const guards = [...appHtml.matchAll(/<script>([\s\S]*?)<\/script>/g)]
.map((match) => match[0])
.join('\n');
let browser;
const server = createServer(async (req, res) => {
const pathname = new URL(req.url, 'http://localhost').pathname;
try {
const file = pathname.startsWith('/assets/') ? pathname.slice(1) : 'index.html';
if (file.includes('..')) {
res.writeHead(400).end();
return;
}
const content = await readFile(path.join(temporary, 'dist', file));
res.setHeader('Content-Type', file.endsWith('.js') ? 'text/javascript' : 'text/html');
res.end(content);
} catch {
res.writeHead(404).end();
}
});
try {
await writeFile(
path.join(temporary, 'index.html'),
`<html><head>${guards}</head><body><input aria-label="Draft"><script type="module" src="/entry.js"></script></body></html>`,
);
await writeFile(
path.join(temporary, 'entry.js'),
`
import { importWithRecovery } from ${JSON.stringify(path.join(root, 'client/src/lib/assets/lazy.ts'))};
window.optional = () => import('./optional.js').catch(() => { window.optionalFailed = true; });
window.required = () => importWithRecovery(() => import('./required.js')).catch(() => {});
`,
);
await writeFile(
path.join(temporary, 'optional.js'),
"import './optional.css'; export const value = 1;",
);
await writeFile(path.join(temporary, 'optional.css'), 'body { color: inherit; }');
await writeFile(path.join(temporary, 'required.js'), 'export const value = 2;');
await build({ root: temporary, configFile: false, logLevel: 'silent' });
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
browser = await chromium.launch({
headless: true,
channel: process.env.PLAYWRIGHT_CHANNEL,
timeout: 30000,
});
const page = await browser.newPage();
page.setDefaultTimeout(10000);
await page.route(/\/(?:optional|required)-[^/]+\.(?:js|css)$/, (route) => route.abort());
await page.goto(`http://127.0.0.1:${server.address().port}`);
await page.waitForFunction(() => typeof window.optional === 'function');
await page.getByLabel('Draft').fill('Keep my unsent text');
await page.evaluate(() => window.optional());
assert.equal(await page.evaluate(() => window.optionalFailed), true);
assert.equal(await page.getByLabel('Draft').inputValue(), 'Keep my unsent text');
assert.equal(await page.evaluate(() => sessionStorage.getItem('lc-asset-recovery-at')), null);
const reloaded = page.waitForEvent('load');
await page.evaluate(() => {
window.required();
});
await reloaded;
await page.waitForFunction(() => typeof window.required === 'function');
assert.ok(
(await page.evaluate(() => Number(sessionStorage.getItem('lc-asset-recovery-at')))) > 0,
);
} finally {
await browser?.close();
await new Promise((resolve) => server.close(resolve));
await rm(temporary, { recursive: true, force: true });
}
},
);