From 42ff3e364fb1be0e8ec4ccad83765270e1bed7a6 Mon Sep 17 00:00:00 2001 From: JasonFraser Date: Tue, 13 Oct 2026 11:40:07 -0400 Subject: [PATCH] Fix pdf.js getDocument() call shape and document-switch cleanup Two real bugs from the first real test of R.1's pdf.js viewer: 1. getDocument(url) was called with a bare string. The installed pdfjs-dist (6.4.299) requires an object - getDocument(src = {}) only ever reads src.url/src.data/src.range, so src.url ended up undefined and pdf.js threw its own "expected either data, range, or url parameter" error, exactly what was reported. Fixed: getDocument({ url }). 2. Found while fixing #1: cleanup called pdfDoc.value.destroy(), but destroy() lives on the loading task (getDocument()'s own synchronous return value), not the resolved PDFDocumentProxy, which has no destroy of its own - confirmed via the package's own type definitions and a real Node script throwing "doc.destroy is not a function" otherwise. Fixed by keeping the loading task and calling loadingTask.destroy() instead. Verified for real via pdf.js's own Node support, not just re-reading the code: the buggy call shape reproduced the exact reported error first, the fix loaded both the HSBC and RBC fixtures correctly, and loading a second document after destroying the first (the real document-switching scenario) succeeded cleanly. Traced but could not find a code path connecting this to the separately reported empty transactions table - that computed has no dependency on the source panel's state. Fixed the confirmed bug; did not claim that symptom is resolved. Co-Authored-By: Claude Sonnet 5 --- CLAUDE.md | 36 ++++++++++++++++++++++++ frontend/src/composables/usePdfViewer.js | 26 ++++++++++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index f1d4cf2..89c3525 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1414,6 +1414,42 @@ behavior in a way a curl-based bundle check cannot confirm - Claude-in-Chrome st not connect. This is the one piece of R.1 that most needs the user's own look before R.2 builds on top of it. +**Two real `usePdfViewer.js` bugs found by the user's own first real test, fixed same +day.** Confirmed this is exactly why that honest "not machine-verified" note above +mattered - a bundle/curl check cannot catch either of these. + +1. `getDocument(url)` was called with the bare blob-URL string directly. The installed + `pdfjs-dist` (6.4.299) does not accept that shorthand - confirmed by reading its own + source (`getDocument(src = {})` only ever reads `src.url`/`src.data`/`src.range`, + never treats `src` itself as a url) - so `src.url` was `undefined` and pdf.js threw + its own "expected either `data`, `range`, or `url` parameter" error, exactly what the + user saw. Fixed: `getDocument({ url })`. +2. Found during the *fix* for #1, before it ever reached the user again: document-switch + cleanup called `pdfDoc.value.destroy()` - but `destroy()` lives on the LOADING TASK + (`getDocument()`'s own synchronous return value, before `.promise` resolves), not on + the resolved `PDFDocumentProxy` - confirmed both by reading the installed package's + own type definitions (`PDFDocumentProxy` has no `destroy` method at all) and by a real + Node script throwing `doc.destroy is not a function` against this exact installed + version. Fixed by keeping the loading task itself (not just the resolved document) and + calling `loadingTask.destroy()` instead. + +**Verified for real this time, not just re-reading the code** - pdf.js has real Node +support, so both fixes were run directly (not re-implemented) against the actual HSBC +and RBC fixtures via a plain Node script: the buggy call shape reproduced the exact +reported error first (confirming the diagnosis, not just a plausible-sounding one); the +fixed shape loaded both fixtures correctly (4 pages each, correct page dimensions); and +loading a second document after destroying the first - the exact document-switching +scenario the app needs - succeeded cleanly. `npm run build`/container rebuild/ +`measure.py` all re-run clean. + +**The user also reported the transactions table showing nothing for the same document.** +Traced the code for a causal link and found none: `loadDocument()`'s own `getDocument()` +call was already inside a `try/catch` (the rejection surfaces as `pdfLoadError`, nothing +propagates past it), and `TransactionsTable.vue`'s own `currentResult` is a completely +separate computed with no dependency on `SourceDocumentPanel`'s state at all. Fixed the +confirmed bug; **did not claim the transactions-table symptom is resolved** - asked the +user to retest specifically that, since no code path was found connecting the two. + **Governing rule: never silently correct or exclude data.** This shows up in several concrete places, not just as an abstract principle: - A row with an unusual status (e.g. HSBC's "Cancelled" wires) stays in the output and is diff --git a/frontend/src/composables/usePdfViewer.js b/frontend/src/composables/usePdfViewer.js index bd01995..6a3588b 100644 --- a/frontend/src/composables/usePdfViewer.js +++ b/frontend/src/composables/usePdfViewer.js @@ -47,6 +47,15 @@ export function usePdfViewer() { // don't need Vue's deep reactivity - they're only ever read inside // this composable's own functions, never rendered directly in a template. const pdfDoc = shallowRef(null) + // The LOADING TASK (getDocument()'s own synchronous return value, before + // awaiting .promise) - NOT the resolved PDFDocumentProxy, which has no + // destroy() of its own (confirmed by reading the installed package's + // own type definitions and by a real Node test throwing + // "doc.destroy is not a function"). loadingTask.destroy() is pdf.js's + // real cleanup API - "abort all network requests and destroy the + // worker" - kept outside Vue's reactivity since nothing ever reads it + // in a template. + let loadingTask = null let pageViewports = [] // each page's own {width, height} at scale=1 (PDF points) function setCanvasRef(i, el) { @@ -81,17 +90,26 @@ export function usePdfViewer() { async function loadDocument(url) { highlight.value = null loadError.value = '' - if (pdfDoc.value) { - pdfDoc.value.destroy() - pdfDoc.value = null + if (loadingTask) { + loadingTask.destroy() + loadingTask = null } + pdfDoc.value = null if (!url) { numPages.value = 0 return } loading.value = true try { - const doc = await pdfjsLib.getDocument(url).promise + // getDocument() in this installed pdfjs-dist version (6.4.299) + // requires an object with a `url` key - it does NOT accept a bare + // string shorthand despite older pdf.js docs/examples showing that + // (confirmed by reading the installed package's own source: + // getDocument(src = {}) reads src.url/src.data/src.range only - + // and confirmed for real in a Node script that this exact call + // shape throws the exact error reported). + loadingTask = pdfjsLib.getDocument({ url }) + const doc = await loadingTask.promise pdfDoc.value = doc numPages.value = doc.numPages canvasRefs.value = new Array(doc.numPages)