mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
fix(ui): tag chunk-load reports with the reload recovery step that gave up (#23613)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The board UI loads each page as a lazy chunk. When a chunk fails to load, chunk-load recovery reloads the page. It retries while a deploy settles (#17349, #19361). > - Safari reported `TypeError: Importing a module script failed.` from the route error boundary. The build already had the retry logic. So recovery gave up and the user saw the error page. > - Recovery can give up for four reasons. The reload did not leave the page within 10 seconds, the retries were used up, session storage was blocked, or an earlier reload in this page was abandoned. The report does not say which one occurred. Breadcrumbs are off for privacy, so the event has no other clue. > - This pull request records the recovery decision for each chunk-load error and sends it as one enum tag. > - The benefit is that the next report shows which recovery step failed, so a real fix can target that step. ## Linked Issues or Issue Description Refs #17349 Refs #19361 **What happened?** A Safari user got `TypeError: Importing a module script failed.` from the route error boundary. The page was more than 10 minutes old and had become visible 10–60 seconds earlier. The build included the chunk-load retry logic, so recovery gave up. The event does not tell why. **Expected behavior** A chunk-load error report states which recovery step gave up, without URLs or page data. **Steps to reproduce** 1. Open a lazy route in a build where its chunk can no longer load. 2. Let the reload stall for more than 10 seconds, or use up the retries in the guard window. 3. Look at the Sentry event. It has no field that tells which case occurred. ## What Changed - `chunk-load-recovery.ts`: Record a `ChunkLoadRecoveryOutcome` for each chunk-load error. The values are `reload_pending`, `reload_abandoned`, `reload_cancelled`, `retries_exhausted`, `storage_unavailable`, and `disabled_after_abandon`. Errors that wait on a reload change to `reload_abandoned` or `reload_cancelled` when that reload ends. Add `readChunkLoadRecoveryOutcome(error)`. The reload behavior does not change. - `sentry.ts`: In `beforeSend`, add the `browser_chunk_reload` tag when the original exception has a recorded outcome. - Tests for each outcome and for the tag. ## Verification - `cd ui && npx vitest run src/lib/chunk-load-recovery.test.ts src/lib/sentry.test.ts src/components/RouteErrorBoundary.test.tsx src/components/AppErrorBoundary.test.tsx`: 83 tests pass. - Red check: the 4 new tests fail on the old implementation and pass with this change. - `cd ui && npx tsc --noEmit -p tsconfig.json`: no errors in changed files. The only errors are the ignored generated Storybook catalog files, which were not generated locally. - Note: `sentry.test.ts > initializes with the DSN it receives` can time out at 5 s on a cold cache. The same flake occurs on unchanged `master`. - `pnpm verify` did not finish in this environment (no output in 300 s, also with `--explain`). CI supplies the broad coverage. ## Risks Low risk. The change only adds one enum tag to Sentry events. It sends no URL, path, or page data. The outcome map is a `WeakMap`, so it holds no errors in memory. The reload timing and the guard logic do not change. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), agentic coding with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
4 files changed
+157
-6
No files matched your search
@@ -212,6 +212,82 @@ describe("reloadForChunkLoadError", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("readChunkLoadRecoveryOutcome", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
const moduleScriptError = () => new TypeError("Importing a module script failed.");
|
||||
|
||||
it("records a reload that the page abandoned, for each error waiting on it", () => {
|
||||
vi.useFakeTimers();
|
||||
const recovery = createChunkLoadRecovery({ storage: () => memoryStorage(), reload: vi.fn() });
|
||||
const first = moduleScriptError();
|
||||
const second = moduleScriptError();
|
||||
recovery.reloadForChunkLoadError(first);
|
||||
recovery.reloadForChunkLoadError(second);
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(first)).toBe("reload_pending");
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(second)).toBe("reload_pending");
|
||||
|
||||
let reportedOutcome: string | undefined;
|
||||
recovery.onChunkLoadReloadAbandoned(() => {
|
||||
reportedOutcome = recovery.readChunkLoadRecoveryOutcome(first);
|
||||
});
|
||||
vi.advanceTimersByTime(RELOAD_ABANDONED_MS);
|
||||
expect(reportedOutcome).toBe("reload_abandoned");
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(second)).toBe("reload_abandoned");
|
||||
|
||||
const later = moduleScriptError();
|
||||
recovery.reloadForChunkLoadError(later);
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(later)).toBe("disabled_after_abandon");
|
||||
});
|
||||
|
||||
it("records why the error surfaced without a reload", () => {
|
||||
const storage = memoryStorage();
|
||||
let now = 1_000_000;
|
||||
for (let attempt = 0; attempt < RELOAD_DELAYS_MS.length; attempt += 1) {
|
||||
createChunkLoadRecovery({ storage: () => storage, reload: vi.fn(), now: () => now }).reloadForChunkLoadError(cssPreloadError);
|
||||
now += 10_000;
|
||||
}
|
||||
const exhausted = createChunkLoadRecovery({ storage: () => storage, reload: vi.fn(), now: () => now });
|
||||
const error = moduleScriptError();
|
||||
exhausted.reloadForChunkLoadError(error);
|
||||
expect(exhausted.readChunkLoadRecoveryOutcome(error)).toBe("retries_exhausted");
|
||||
|
||||
const noStorage = createChunkLoadRecovery({ storage: () => null, reload: vi.fn() });
|
||||
const unguarded = moduleScriptError();
|
||||
noStorage.reloadForChunkLoadError(unguarded);
|
||||
expect(noStorage.readChunkLoadRecoveryOutcome(unguarded)).toBe("storage_unavailable");
|
||||
|
||||
const throwing = createChunkLoadRecovery({
|
||||
storage: () => ({
|
||||
getItem: () => { throw new Error("SecurityError"); },
|
||||
setItem: () => { throw new Error("SecurityError"); },
|
||||
}),
|
||||
reload: vi.fn(),
|
||||
});
|
||||
const blocked = moduleScriptError();
|
||||
throwing.reloadForChunkLoadError(blocked);
|
||||
expect(throwing.readChunkLoadRecoveryOutcome(blocked)).toBe("storage_unavailable");
|
||||
});
|
||||
|
||||
it("records a cancelled delayed retry and ignores errors it never handled", () => {
|
||||
vi.useFakeTimers();
|
||||
const storage = memoryStorage({ "paperclip:chunk-load-reload-at": "1000000" });
|
||||
const recovery = createChunkLoadRecovery({ storage: () => storage, reload: vi.fn(), now: () => 1_010_000 });
|
||||
const error = moduleScriptError();
|
||||
recovery.reloadForChunkLoadError(error);
|
||||
recovery.cancelPendingChunkLoadReload();
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(error)).toBe("reload_cancelled");
|
||||
|
||||
const renderError = new Error("boom");
|
||||
recovery.reloadForChunkLoadError(renderError);
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(renderError)).toBeUndefined();
|
||||
expect(recovery.readChunkLoadRecoveryOutcome(moduleScriptError())).toBeUndefined();
|
||||
expect(recovery.readChunkLoadRecoveryOutcome("Importing a module script failed.")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("startChunkLoadRecovery", () => {
|
||||
it("reloads for a Vite preload failure without cancelling the rejection", () => {
|
||||
const target = new EventTarget();
|
||||
|
||||
@@ -24,6 +24,18 @@ export function isChunkLoadError(error: unknown): boolean {
|
||||
|
||||
type ReloadStorage = Pick<Storage, "getItem" | "setItem">;
|
||||
|
||||
/**
|
||||
* Why a chunk-load error did or did not lead to a page reload. Error reports
|
||||
* carry it so a report can tell which recovery step gave up.
|
||||
*/
|
||||
export type ChunkLoadRecoveryOutcome =
|
||||
| "reload_pending"
|
||||
| "reload_abandoned"
|
||||
| "reload_cancelled"
|
||||
| "retries_exhausted"
|
||||
| "storage_unavailable"
|
||||
| "disabled_after_abandon";
|
||||
|
||||
function isPositiveTime(value: number): boolean {
|
||||
return Number.isFinite(value) && value > 0;
|
||||
}
|
||||
@@ -60,11 +72,30 @@ export function createChunkLoadRecovery(options: ChunkLoadRecoveryOptions = {})
|
||||
let pendingReloadTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let abandonTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
const abandonedListeners = new Set<() => void>();
|
||||
const outcomes = new WeakMap<Error, ChunkLoadRecoveryOutcome>();
|
||||
const waitingErrors = new Set<Error>();
|
||||
|
||||
function settleWaitingErrors(outcome: ChunkLoadRecoveryOutcome) {
|
||||
for (const error of waitingErrors) outcomes.set(error, outcome);
|
||||
waitingErrors.clear();
|
||||
}
|
||||
|
||||
function giveUp(error: Error, outcome: ChunkLoadRecoveryOutcome): false {
|
||||
outcomes.set(error, outcome);
|
||||
return false;
|
||||
}
|
||||
|
||||
function waitForReload(error: Error): true {
|
||||
outcomes.set(error, "reload_pending");
|
||||
waitingErrors.add(error);
|
||||
return true;
|
||||
}
|
||||
|
||||
function abandonReload() {
|
||||
abandonTimer = null;
|
||||
reloading = false;
|
||||
reloadAbandoned = true;
|
||||
settleWaitingErrors("reload_abandoned");
|
||||
const listeners = [...abandonedListeners];
|
||||
abandonedListeners.clear();
|
||||
for (const listener of listeners) listener();
|
||||
@@ -72,25 +103,28 @@ export function createChunkLoadRecovery(options: ChunkLoadRecoveryOptions = {})
|
||||
|
||||
function reloadForChunkLoadError(error: unknown): boolean {
|
||||
if (!isChunkLoadError(error)) return false;
|
||||
if (reloading) return true;
|
||||
if (reloadAbandoned) return false;
|
||||
// isChunkLoadError accepts Error instances only.
|
||||
const chunkError = error as Error;
|
||||
if (reloading) return waitForReload(chunkError);
|
||||
if (reloadAbandoned) return giveUp(chunkError, "disabled_after_abandon");
|
||||
const storage = readStorage();
|
||||
if (!storage) return false;
|
||||
if (!storage) return giveUp(chunkError, "storage_unavailable");
|
||||
const at = now();
|
||||
let delayMs: number;
|
||||
try {
|
||||
const startedAt = Number(storage.getItem(RELOAD_AT_KEY));
|
||||
const inWindow = isPositiveTime(startedAt) && at - startedAt < RELOAD_GUARD_MS;
|
||||
const attempt = inWindow ? readAttempts(storage.getItem(RELOAD_ATTEMPTS_KEY), startedAt) : 0;
|
||||
if (attempt >= RELOAD_DELAYS_MS.length) return false;
|
||||
if (attempt >= RELOAD_DELAYS_MS.length) return giveUp(chunkError, "retries_exhausted");
|
||||
delayMs = RELOAD_DELAYS_MS[attempt]!;
|
||||
const windowStartedAt = inWindow ? startedAt : at;
|
||||
if (!inWindow) storage.setItem(RELOAD_AT_KEY, String(windowStartedAt));
|
||||
storage.setItem(RELOAD_ATTEMPTS_KEY, `${windowStartedAt}:${attempt + 1}`);
|
||||
} catch {
|
||||
return false;
|
||||
return giveUp(chunkError, "storage_unavailable");
|
||||
}
|
||||
reloading = true;
|
||||
waitForReload(chunkError);
|
||||
if (delayMs > 0) {
|
||||
pendingReloadTimer = globalThis.setTimeout(() => {
|
||||
pendingReloadTimer = null;
|
||||
@@ -113,9 +147,15 @@ export function createChunkLoadRecovery(options: ChunkLoadRecoveryOptions = {})
|
||||
abandonTimer = null;
|
||||
reloading = false;
|
||||
abandonedListeners.clear();
|
||||
settleWaitingErrors("reload_cancelled");
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The recovery decision recorded for this exact error, if it was a chunk-load error. */
|
||||
function readChunkLoadRecoveryOutcome(error: unknown): ChunkLoadRecoveryOutcome | undefined {
|
||||
return error instanceof Error ? outcomes.get(error) : undefined;
|
||||
}
|
||||
|
||||
function onChunkLoadReloadAbandoned(listener: () => void): () => void {
|
||||
if (!reloading) {
|
||||
listener();
|
||||
@@ -135,7 +175,13 @@ export function createChunkLoadRecovery(options: ChunkLoadRecoveryOptions = {})
|
||||
return () => target.removeEventListener("vite:preloadError", onPreloadError);
|
||||
}
|
||||
|
||||
return { reloadForChunkLoadError, cancelPendingChunkLoadReload, onChunkLoadReloadAbandoned, startChunkLoadRecovery };
|
||||
return {
|
||||
reloadForChunkLoadError,
|
||||
cancelPendingChunkLoadReload,
|
||||
onChunkLoadReloadAbandoned,
|
||||
readChunkLoadRecoveryOutcome,
|
||||
startChunkLoadRecovery,
|
||||
};
|
||||
}
|
||||
|
||||
const browserRecovery = createChunkLoadRecovery();
|
||||
@@ -143,4 +189,5 @@ const browserRecovery = createChunkLoadRecovery();
|
||||
export const reloadForChunkLoadError = browserRecovery.reloadForChunkLoadError;
|
||||
export const cancelPendingChunkLoadReload = browserRecovery.cancelPendingChunkLoadReload;
|
||||
export const onChunkLoadReloadAbandoned = browserRecovery.onChunkLoadReloadAbandoned;
|
||||
export const readChunkLoadRecoveryOutcome = browserRecovery.readChunkLoadRecoveryOutcome;
|
||||
export const startChunkLoadRecovery = browserRecovery.startChunkLoadRecovery;
|
||||
@@ -659,6 +659,29 @@ describe("buildBrowserSentryInitOptions", () => {
|
||||
expect(host?.tags).not.toHaveProperty("browser_plugin_module");
|
||||
});
|
||||
|
||||
it("tags a chunk-load error with the reload recovery step that gave up", async () => {
|
||||
const { buildBrowserSentryInitOptions } = await importFreshSentry();
|
||||
const recovery = await import("./chunk-load-recovery");
|
||||
window.sessionStorage.clear();
|
||||
const beforeSend = buildBrowserSentryInitOptions(DSN).beforeSend!;
|
||||
const eventFor = () => ({
|
||||
exception: { values: [{ type: "TypeError", mechanism: { type: "generic", handled: true } }] },
|
||||
});
|
||||
|
||||
// Earlier reloads in this tab already used every retry in the guard window.
|
||||
const exhaustedPage = new TypeError("Importing a module script failed.");
|
||||
const seeded = Date.now();
|
||||
window.sessionStorage.setItem("paperclip:chunk-load-reload-at", String(seeded));
|
||||
window.sessionStorage.setItem("paperclip:chunk-load-reload-attempts", `${seeded}:3`);
|
||||
expect(recovery.reloadForChunkLoadError(exhaustedPage)).toBe(false);
|
||||
const tagged = await beforeSend(eventFor() as never, { originalException: exhaustedPage });
|
||||
expect(tagged?.tags).toMatchObject({ browser_chunk_reload: "retries_exhausted" });
|
||||
|
||||
const renderError = await beforeSend(eventFor() as never, { originalException: new Error("boom") });
|
||||
expect(renderError?.tags).not.toHaveProperty("browser_chunk_reload");
|
||||
window.sessionStorage.clear();
|
||||
});
|
||||
|
||||
it("adds diagnostics without a transaction filter", async () => {
|
||||
const { buildBrowserSentryInitOptions } = await importFreshSentry();
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ import {
|
||||
readFetchWrapperState,
|
||||
startFetchRejectionDiagnostics,
|
||||
} from "./fetch-rejection-origin";
|
||||
import { readChunkLoadRecoveryOutcome } from "./chunk-load-recovery";
|
||||
import { isIntentionalAbortReason } from "./intentional-abort";
|
||||
import { readPluginModuleOrigin } from "./plugin-module-origins";
|
||||
|
||||
@@ -285,6 +286,10 @@ export function buildBrowserSentryInitOptions(
|
||||
// whose module holds the innermost such frame, never the URL.
|
||||
const pluginModule = readPluginModuleOrigin(event.exception?.values);
|
||||
if (pluginModule) event.tags.browser_plugin_module = pluginModule;
|
||||
// A lazy chunk that failed to load normally reloads the page. Record
|
||||
// which recovery step gave up, so a report shows why it still surfaced.
|
||||
const chunkReload = readChunkLoadRecoveryOutcome(hint.originalException);
|
||||
if (chunkReload) event.tags.browser_chunk_reload = chunkReload;
|
||||
if (isGlobalOnError(event)) {
|
||||
event.tags.browser_thrown_kind = classifyThrownValue(hint.originalException);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user