From 64f8c103769cd1f0b095507866f2a62acab2356e Mon Sep 17 00:00:00 2001 From: ivgtr Date: Thu, 8 Oct 2026 18:03:55 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20PDF=E3=81=AE=E7=8B=AC=E7=AB=8B=E8=A1=A8?= =?UTF-8?q?=E3=81=AE=E3=82=BB=E3=83=AB=E6=8E=A2=E7=B4=A2=E3=81=A8=E6=8A=BD?= =?UTF-8?q?=E5=87=BA=E3=82=A8=E3=83=A9=E3=83=BC=E8=A8=BA=E6=96=AD=E3=82=92?= =?UTF-8?q?=E6=94=B9=E5=96=84=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- evaluation/pdf-table-extraction.md | 17 ++++ src/background/index.test.ts | 51 ++++++++++++ src/background/index.ts | 19 +++-- src/content/SummaryButton.tsx | 7 +- src/content/hooks/useSummarize.test.ts | 29 +++++++ src/lib/pdf-extraction-error.ts | 94 +++++++++++++++++++++ src/lib/pdf-layout.ts | 12 +-- src/lib/summary-trace.ts | 2 + src/lib/table-layout.test.ts | 111 +++++++++++++++++++++++++ src/lib/table-layout.ts | 69 +++++++++------ src/offscreen/index.test.ts | 101 ++++++++++++++++++++++ src/offscreen/index.ts | 58 ++++++------- 12 files changed, 506 insertions(+), 64 deletions(-) create mode 100644 evaluation/pdf-table-extraction.md create mode 100644 src/lib/pdf-extraction-error.ts create mode 100644 src/lib/table-layout.test.ts diff --git a/evaluation/pdf-table-extraction.md b/evaluation/pdf-table-extraction.md new file mode 100644 index 0000000..ef028fb --- /dev/null +++ b/evaluation/pdf-table-extraction.md @@ -0,0 +1,17 @@ +# 独立した表のセル探索と抽出エラー診断(2026-10-08) + +基準は `bfdd545`(v0.7.8)。[対象PDF](https://www.release.tdnet.info/inbs/140120261008547647.pdf) は26ページ、SHA-256は `ed8e4105ee93a8fb44c294f5be9f88cce9adb1e8e2500d977be3c0a02d726ecf`。旧版では表紙の文字・描画演算は読めるが、独立した表の軸の組合せが増え、`SOURCE_DRAWING:表のセル解析の処理上限` で停止した。旧調査の `validatePages` 成功は抽出できた25ページだけで、PDF全体の成功ではなかった。 + +### 変更と担当テスト + +- `table-layout` は既存の最小幅8・高さ4と連続する上辺・左辺を必要条件として探索を絞る。軸ごとの実罫線を再利用し、辺が途切れた後の遠い軸も探索しない。実罫線・許容幅・最小閉矩形の順序は不変。罫線2,000本と候補100,000回の上限を維持し、枝刈りの開始点・左辺探索にも各100,000回の上限を置く。既に旧上限内だった入力では、各追加カウントは旧候補数以下になる。 +- `table-layout.test.ts` は独立6表、結合セル、最小寸法、分割線の許容幅、欠けた各辺、密なセルと疎なページの上限を担当する。企業・PDFごとの例外や文字だけへの代替は加えない。 +- `offscreen/index.test.ts` は一部成功後のページ読込・文字・演算・レイアウト失敗、後始末による元例外の上書き防止、文書読込失敗を確認する。`background/index.test.ts` はJSON通信後のページ・段階・例外名・コード・元メッセージを `pdfExtractionError` に保存し、LLMを呼ばず停止することと接頭辞の重複防止を担当する。 +- `useSummarize.test.ts` は診断コピー中の拡張無効化を同期例外・非同期拒否で確認する。TDnetページの再読み込みを案内し、コピー済みと表示しない。 + +### 検証結果と境界 + +- 最終製品コードで `npm test` は1,274件/41ファイル成功。type-check・lint・build、同梱CMap168個とLICENSEのバイト照合も成功。 +- 対象PDFをPDF.js 5.4.394と本番共通 `extractPdfPageLayout` で全26ページ抽出後に `validatePages` を実行した。失敗・空ページ0、24,207文字。表紙106セルの座標・順序は上限だけを外した旧探索結果と一致し、従来成功していた残り25ページの全抽出データも基準と一致。全ページの生成入力SHA-256は `970d76f4b74507f278cfd615bce6569ffe6cfac9c13a189667e3e957645d5496`。 +- 保存済みの `reporting-table-corpus`・`pdf-layout-corpus`・`ir-semantic-corpus` 合計20エントリ/32ページで、抽出データと生成入力が基準と完全一致。実PDF20件を再取得した検証ではない。独立差分レビューでは、疎な罫線・結合セル・最小寸法付近のランダム2,500グリッドも旧探索とセル順・ID・座標・原文所属が一致した。 +- 固定応答smokeの事前検査は成功・部分成功・最終失敗の3例とも成功。クラウドChromiumでの実拡張画面は起動時の `socket() failed: Operation not permitted` で停止し、実画面・実拡張通信の成功には数えない。実API呼出し0。対象PDFの実モデルによる要約品質と利用者のChrome画面は未検証。 diff --git a/src/background/index.test.ts b/src/background/index.test.ts index 2943f42..72ae83b 100644 --- a/src/background/index.test.ts +++ b/src/background/index.test.ts @@ -10,6 +10,7 @@ import type { ExtractedPage, ExtractionMode } from '../types/summaryMetadata'; import { serializePagesForAnalysis } from '../lib/page-text'; import { fixedOrganization } from '../lib/fixtures/summary-narrative-source'; import type { SummaryAttempt } from '../lib/summary-trace'; +import type { PdfExtractionErrorDetails } from '../lib/pdf-extraction-error'; const mocked = vi.hoisted(() => ({ generateText: vi.fn(), @@ -288,6 +289,56 @@ describe('要約・採点・追加分析の分離', () => { } ); + it('Offscreenの元例外を診断JSONへ保持し、失敗ページから生成しない', async () => { + const request = await setup(false); + const details: PdfExtractionErrorDetails = { + pageNumber: 2, + stage: 'page-layout', + name: 'Error', + code: 'SOURCE_DRAWING', + message: 'SOURCE_DRAWING:表のセル解析の処理上限', + }; + const response = JSON.parse( + JSON.stringify({ + success: false, + error: `PDF抽出エラー: PDF p.2: ${details.message}`, + pdfExtractionError: details, + }) + ); + vi.mocked(chrome.runtime.sendMessage).mockResolvedValueOnce(response); + const result = await request({ action: 'summarize' }); + const writes = vi.mocked(chrome.storage.local.set).mock.calls; + const lastWrite = writes[writes.length - 1][0] as Record; + const trace = JSON.parse(JSON.stringify(lastWrite.summaryLastRunV1)); + expect(result.error).toBe(response.error); + expect(trace).toMatchObject({ + runId: result.diagnosticRunId, + outcome: 'failure', + error: response.error, + pdfExtractionError: details, + attempts: [], + usage: [], + resultId: null, + }); + expect(matchingSummaryTrace(trace, 'test.pdf', result.diagnosticRunId, null)).toEqual(trace); + expect(mocked.generateText).not.toHaveBeenCalled(); + }); + + it.each([undefined, { pageNumber: 0, stage: 'page-layout', message: 'invalid details' }])( + '旧形式または不正な抽出エラー詳細%sでも元メッセージを失わず接頭辞を重複させない', + async (details) => { + const request = await setup(false); + vi.mocked(chrome.runtime.sendMessage).mockResolvedValueOnce({ + success: false, + error: 'PDF抽出エラー: PDF抽出エラー: SOURCE:PDF p.1の抽出失敗', + pdfExtractionError: details, + }); + const result = await request({ action: 'summarize' }); + expect(result.error).toBe('PDF抽出エラー: SOURCE:PDF p.1の抽出失敗'); + expect(mocked.generateText).not.toHaveBeenCalled(); + } + ); + it.each(['success', 'failure', 'earlyFailure', 'lateExtraction'] as const)( '古い完了%sは新しい要求の診断を上書きしない', async (stage) => { diff --git a/src/background/index.ts b/src/background/index.ts index 0e290b8..2110d7a 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,4 +1,10 @@ import { SUMMARY_TRACE_KEY, summaryBuildDigest, type SummaryTrace } from '@/lib/summary-trace'; +import { + PdfExtractionError, + pdfExtractionError, + readPdfExtractionError, + type PdfExtractionStage, +} from '@/lib/pdf-extraction-error'; import { serializeCandidateSource } from '@/lib/fact-candidates'; import type { LLMConfig } from '@/lib/llm-client'; import { configuredApiUrl } from '@/lib/llm-endpoint'; @@ -303,6 +309,7 @@ async function handleSummarize(request: SummarizeRequest, runId: string) { } catch (error) { trace.outcome = 'failure'; trace.error = error instanceof Error ? error.message : String(error); + if (error instanceof PdfExtractionError) trace.pdfExtractionError = error.details; await saveTrace(); throw error; } @@ -540,6 +547,7 @@ async function extractTextFromPDF( documentType: DocumentType, extractionMode: ExtractionMode ): Promise { + let stage: PdfExtractionStage = 'transport'; try { // ArrayBufferを配列に変換して送信 const uint8Array = new Uint8Array(pdfData); @@ -553,9 +561,13 @@ async function extractTextFromPDF( }); if (!response.success) { - throw new Error(response.error || 'PDF抽出に失敗しました'); + throw ( + readPdfExtractionError(response.pdfExtractionError) ?? + new Error(response.error || 'PDF抽出に失敗しました') + ); } + stage = 'validation'; validatePages(response.pages); if ( typeof response.text !== 'string' || @@ -582,9 +594,6 @@ async function extractTextFromPDF( }; } catch (error) { console.error('[Background] PDF抽出エラー:', error); - if (error instanceof Error) { - throw new Error(`PDF抽出エラー: ${error.message}`); - } - throw new Error('PDFからテキストを抽出できませんでした。'); + throw pdfExtractionError(error, stage); } } diff --git a/src/content/SummaryButton.tsx b/src/content/SummaryButton.tsx index e0dafac..aa956f5 100644 --- a/src/content/SummaryButton.tsx +++ b/src/content/SummaryButton.tsx @@ -167,7 +167,12 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } } } catch (e) { if (request !== diagnosticRequest.current) return; - setDiagnosticError(e instanceof Error ? e.message : String(e)); + const message = e instanceof Error ? e.message : String(e); + setDiagnosticError( + /extension context invalidated/i.test(message) + ? '拡張機能との接続が切れています。TDnetのページを再読み込みしてから、診断JSONのコピーをやり直してください。' + : message + ); } }; const handleClick = () => { diff --git a/src/content/hooks/useSummarize.test.ts b/src/content/hooks/useSummarize.test.ts index 59e28c5..7f04d75 100644 --- a/src/content/hooks/useSummarize.test.ts +++ b/src/content/hooks/useSummarize.test.ts @@ -779,6 +779,35 @@ describe('実Reactの要約行アクション配置', () => { expect(writeText).toHaveBeenCalledTimes(3); }); + it.each(['throw', 'reject'] as const)( + '診断読込の無効化エラー(%s)はページ再読み込みを案内し、コピー成功にしない', + async (failure) => { + settings.experimentalScoring = false; + sendMessage.mockResolvedValueOnce(await responseFor()); + const writeText = vi.fn().mockResolvedValue(undefined); + vi.stubGlobal('navigator', { clipboard: { writeText } }); + const { row, button } = await mountButton(); + await click(button); + const summaryRow = await summaryRowFor(row); + const host = summaryRow.querySelector('[data-diagnostic-root]')!; + const error = new Error('Extension context invalidated.'); + if (failure === 'throw') + vi.mocked(chrome.storage.local.get).mockImplementationOnce(() => { + throw error; + }); + else vi.mocked(chrome.storage.local.get).mockRejectedValueOnce(error); + await click(host.querySelector('button')!); + expect(host.querySelector('[role="alert"]')?.textContent).toBe( + '拡張機能との接続が切れています。TDnetのページを再読み込みしてから、診断JSONのコピーをやり直してください。' + ); + expect(host.querySelector('[role="status"]')?.textContent).toBe(''); + expect(host.querySelector('textarea')).toBeNull(); + expect(writeText).not.toHaveBeenCalled(); + expect(sendMessage).toHaveBeenCalledTimes(1); + expect(summaryRow.textContent).toContain('100百万円'); + } + ); + it('エラー行でも閉じると診断を用意し、エラーの隣の全文再要約で回復する', async () => { settings.extractionMode = 'smart'; settings.experimentalScoring = false; diff --git a/src/lib/pdf-extraction-error.ts b/src/lib/pdf-extraction-error.ts new file mode 100644 index 0000000..54c5c7a --- /dev/null +++ b/src/lib/pdf-extraction-error.ts @@ -0,0 +1,94 @@ +import { record } from './fact-contract'; + +const stages = [ + 'document-load', + 'page-load', + 'text-content', + 'operator-list', + 'page-layout', + 'validation', + 'selection', + 'cleanup', + 'transport', +] as const; +export type PdfExtractionStage = (typeof stages)[number]; + +/** Plain data only: Error properties do not survive Chrome message serialization. */ +export interface PdfExtractionErrorDetails { + pageNumber: number | null; + stage: PdfExtractionStage; + name: string; + code: string | number | null; + message: string; +} + +export class PdfExtractionError extends Error { + constructor(readonly details: PdfExtractionErrorDetails) { + const message = details.message.replace(/^(?:PDF抽出エラー:\s*)+/, ''); + super( + `PDF抽出エラー: ${details.pageNumber === null ? '' : `PDF p.${details.pageNumber}: `}${message}` + ); + this.name = 'PdfExtractionError'; + } +} + +/** Keep the earliest stage and original cause when a failure crosses another boundary. */ +export function pdfExtractionError( + error: unknown, + stage: PdfExtractionStage, + pageNumber: number | null = null +): PdfExtractionError { + if (error instanceof PdfExtractionError) return error; + const source = record(error) ? error : {}; + const message = typeof source.message === 'string' ? source.message : String(error); + return new PdfExtractionError({ + pageNumber, + stage, + name: typeof source.name === 'string' ? source.name : 'Error', + code: + typeof source.code === 'string' || + (typeof source.code === 'number' && Number.isFinite(source.code)) + ? source.code + : (message.match(/^([A-Z][A-Z0-9_]+):/)?.[1] ?? null), + message, + }); +} + +export async function withPdfExtractionStage( + stage: PdfExtractionStage, + pageNumber: number | null, + operation: () => T | Promise +): Promise { + try { + return await operation(); + } catch (error) { + throw pdfExtractionError(error, stage, pageNumber); + } +} + +/** Rehydrate only the diagnostic fields; never trust or copy arbitrary error properties. */ +export function readPdfExtractionError(value: unknown): PdfExtractionError | null { + if ( + !record(value) || + !( + value.pageNumber === null || + (Number.isInteger(value.pageNumber) && Number(value.pageNumber) > 0) + ) || + !stages.includes(value.stage as PdfExtractionStage) || + typeof value.name !== 'string' || + typeof value.message !== 'string' || + !( + value.code === null || + typeof value.code === 'string' || + (typeof value.code === 'number' && Number.isFinite(value.code)) + ) + ) + return null; + return new PdfExtractionError({ + pageNumber: value.pageNumber as number | null, + stage: value.stage as PdfExtractionStage, + name: value.name, + code: value.code as string | number | null, + message: value.message, + }); +} diff --git a/src/lib/pdf-layout.ts b/src/lib/pdf-layout.ts index 49f4029..090026a 100644 --- a/src/lib/pdf-layout.ts +++ b/src/lib/pdf-layout.ts @@ -16,6 +16,7 @@ import { type DrawingOperation, } from './pdf-drawing'; import { buildTableRegions, buildTableCells } from './table-layout'; +import { withPdfExtractionStage } from './pdf-extraction-error'; export interface PdfSpan { id: string; @@ -36,11 +37,12 @@ export async function extractPdfPageLayout( pageNumber: number, operatorRegistry: Record ): Promise { - const [text, operators] = await Promise.all([page.getTextContent(), page.getOperatorList()]); - return extractPageLayout( - text.items, - pageNumber, - captureDrawingOperations(operators, operatorRegistry) + const [text, operators] = await Promise.all([ + withPdfExtractionStage('text-content', pageNumber, () => page.getTextContent()), + withPdfExtractionStage('operator-list', pageNumber, () => page.getOperatorList()), + ]); + return withPdfExtractionStage('page-layout', pageNumber, () => + extractPageLayout(text.items, pageNumber, captureDrawingOperations(operators, operatorRegistry)) ); } diff --git a/src/lib/summary-trace.ts b/src/lib/summary-trace.ts index 5eef03a..6dad188 100644 --- a/src/lib/summary-trace.ts +++ b/src/lib/summary-trace.ts @@ -3,6 +3,7 @@ import type { Diagnostic } from './fact-candidates'; import type { LLMConfig } from './llm-client'; import { record } from './fact-contract'; import { normalizeTdnetPdfUrl } from './tdnet-url'; +import type { PdfExtractionErrorDetails } from './pdf-extraction-error'; export const SUMMARY_TRACE_KEY = 'summaryLastRunV1'; export type Usage = Parameters>[0]; export interface SummaryAttempt { @@ -34,6 +35,7 @@ export interface SummaryTrace { elapsedMs: number; outcome: 'running' | 'firstSuccess' | 'repairSuccess' | 'partialSuccess' | 'failure'; error: string | null; + pdfExtractionError?: PdfExtractionErrorDetails; } /** A fresh attempt must match its run; cached success must match its exact result. */ export function matchingSummaryTrace( diff --git a/src/lib/table-layout.test.ts b/src/lib/table-layout.test.ts new file mode 100644 index 0000000..85cdd5e --- /dev/null +++ b/src/lib/table-layout.test.ts @@ -0,0 +1,111 @@ +import { describe, expect, it } from 'vitest'; +import { buildTableCells } from './table-layout'; +import type { DrawingLine } from './pdf-drawing'; +import type { PdfSpan } from './pdf-layout'; + +const line = (x1: number, y1: number, x2: number, y2: number): DrawingLine => ({ + id: `${x1},${y1},${x2},${y2}`, + x1, + y1, + x2, + y2, + operatorIndices: [0], +}); +const grid = (xs: number[], ys: number[]) => [ + ...xs.map((x) => line(x, ys[0], x, ys[ys.length - 1])), + ...ys.map((y) => line(xs[0], y, xs[xs.length - 1], y)), +]; +const bounds = (cells: ReturnType) => + cells.map(({ left, top, right, bottom }) => [left, top, right, bottom]); + +describe('罫線セルの探索', () => { + it('独立した6表の軸を組み合わせず、原文のセル順と所属を保持する', () => { + const lines = []; + const expected = []; + for (let table = 0; table < 6; table++) { + const xs = Array.from({ length: 7 }, (_, i) => table * 100 + i * 10); + const ys = Array.from({ length: 4 }, (_, i) => table * 80 + i * 10); + lines.push(...grid(xs, ys)); + for (let row = 0; row < 3; row++) + for (let col = 0; col < 6; col++) + expected.push([xs[col], ys[row], xs[col + 1], ys[row + 1]]); + } + const spans = [ + { id: 'p2s1', text: '123', x: 2, y: 7, width: 5, height: 5 }, + { id: 'p2s2', text: '456', x: 502, y: 407, width: 5, height: 5 }, + { id: 'p2s3', text: '表の間の本文', x: 75, y: 50, width: 10, height: 5 }, + ] as PdfSpan[]; + const cells = buildTableCells(lines, spans, 2); + expect(bounds(cells)).toEqual(expected); + expect(cells.map((c) => c.id)).toEqual(expected.map((_, i) => `p2cell${i + 1}`)); + expect(cells.flatMap((c) => c.spanIds)).toEqual(['p2s1', 'p2s2']); + }); + + it('内部罫線がない結合セルと最小幅・高さのセルを保持する', () => { + const lines = [ + ...grid([0, 40], [0, 10, 20]), + line(20, 10, 20, 20), + ...grid([60, 68], [0, 4]), + ...grid([80, 87], [0, 3]), + ]; + expect(bounds(buildTableCells(lines, [], 1))).toEqual([ + [0, 0, 40, 10], + [60, 0, 68, 4], + [0, 10, 20, 20], + [20, 10, 40, 20], + ]); + }); + + it.each([ + { gap: 0.79, count: 1 }, + { gap: 0.81, count: 0 }, + ])('分割された実罫線の既存の許容幅を変えない: $gap', ({ gap, count }) => { + const lines = [ + line(0, 0, 10, 0), + line(10 + gap, 0, 20, 0), + line(0, 10, 20, 10), + line(0, 0, 0, 10), + line(20, 0, 20, 10), + ]; + expect(buildTableCells(lines, [], 1)).toHaveLength(count); + }); + + it.each(['top', 'bottom', 'left', 'right'])('欠けた%s辺を別表や文字から補わない', (missing) => { + const edges = { + top: line(0, 0, 20, 0), + bottom: line(0, 10, 20, 10), + left: line(0, 0, 0, 10), + right: line(20, 0, 20, 10), + }; + const lines = [ + ...Object.entries(edges) + .filter(([side]) => side !== missing) + .map(([, edge]) => edge), + ...grid([40, 60], [30, 40]), + ]; + expect(bounds(buildTableCells(lines, [], 1))).toEqual([[40, 30, 60, 40]]); + }); + + it('閉じたセルがない疎なページでも、枝刈り処理を無制限にしない', () => { + const axes = Array.from({ length: 318 }, (_, i) => i * 10); + const lines = [ + ...axes.map((x) => line(x, 5000, x, 5001)), + ...axes.map((y) => line(5000, y, 5001, y)), + ]; + expect(() => buildTableCells(lines, [], 1)).toThrow('SOURCE_DRAWING:表のセル解析の処理上限'); + }); + + it('多数の罫線と実在するセルの探索上限は引き続き失敗にする', () => { + expect(() => + buildTableCells( + Array.from({ length: 2001 }, () => line(0, 0, 20, 0)), + [], + 1 + ) + ).toThrow('SOURCE_DRAWING:表の罫線解析の処理上限'); + const axes = Array.from({ length: 318 }, (_, i) => i * 10); + expect(() => buildTableCells(grid(axes, axes), [], 1)).toThrow( + 'SOURCE_DRAWING:表のセル解析の処理上限' + ); + }); +}); diff --git a/src/lib/table-layout.ts b/src/lib/table-layout.ts index 36a8cbb..9ad1258 100644 --- a/src/lib/table-layout.ts +++ b/src/lib/table-layout.ts @@ -114,44 +114,65 @@ export function buildTableCells( ys = snap(horizontal.map((l) => l.y1)); const covers = (segments: Array<[number, number]>, a: number, b: number) => { let end = a; - for (const [start, stop] of segments.sort((x, y) => x[0] - y[0])) { + for (const [start, stop] of segments) { if (start > end + 0.8) break; end = Math.max(end, stop); } return end >= b - 0.8; }; - const h = (y: number, a: number, b: number) => - covers( - horizontal.filter((l) => Math.abs(l.y1 - y) < 0.8).map((l) => [l.x1, l.x2]), - a, - b - ); - const v = (x: number, a: number, b: number) => - covers( - vertical.filter((l) => Math.abs(l.x1 - x) < 0.8).map((l) => [l.y1, l.y2]), - a, - b - ); + // Keep the same edge tolerance and coverage test, but sort each axis only once. + const horizontalSegments = new Map( + ys.map((y) => [ + y, + horizontal + .filter((l) => Math.abs(l.y1 - y) < 0.8) + .map((l): [number, number] => [l.x1, l.x2]) + .sort((a, b) => a[0] - b[0]), + ]) + ); + const verticalSegments = new Map( + xs.map((x) => [ + x, + vertical + .filter((l) => Math.abs(l.x1 - x) < 0.8) + .map((l): [number, number] => [l.y1, l.y2]) + .sort((a, b) => a[0] - b[0]), + ]) + ); + const h = (y: number, a: number, b: number) => covers(horizontalSegments.get(y)!, a, b); + const v = (x: number, a: number, b: number) => covers(verticalSegments.get(x)!, a, b); const cells: TableCell[] = []; - let attempts = 0; + let starts = 0, + leftEdges = 0, + attempts = 0; + const checkBudget = (count: number) => { + if (count > 100000) throw new Error('SOURCE_DRAWING:表のセル解析の処理上限'); + }; // A merged cell spans absent internal edges. Its smallest closed rectangle owns its text. for (let yi = 0; yi < ys.length - 1; yi++) for (let xi = 0; xi < xs.length - 1; xi++) { + checkBudget(++starts); const left = xs[xi], top = ys[yi]; + // A closed cell must have a continuous top and left edge of minimum size. + // Axes belonging to independent tables must not create rectangle candidates. + if (!h(top, left, left + 8) || !v(left, top, top + 4)) continue; for (let yj = yi + 1; yj < ys.length; yj++) { + // Bound the pruning work too. Each count is no greater than the old + // rectangle count for an input that previously stayed within its budget. + checkBudget(++leftEdges); + const bottom = ys[yj]; + if (bottom - top < 4) continue; + if (!v(left, top, bottom)) break; let found = false; for (let xj = xi + 1; xj < xs.length; xj++) { - if (++attempts > 100000) throw new Error('SOURCE_DRAWING:表のセル解析の処理上限'); - const right = xs[xj], - bottom = ys[yj]; - if (right - left < 8 || bottom - top < 4) continue; - if ( - h(top, left, right) && - h(bottom, left, right) && - v(left, top, bottom) && - v(right, top, bottom) - ) { + checkBudget(++attempts); + const right = xs[xj]; + if (right - left < 8) continue; + // Coverage is monotone in the endpoint; a missing edge cannot return + // at a farther axis, even when another independent table starts there. + if (!h(top, left, right)) break; + if (h(bottom, left, right) && v(right, top, bottom)) { cells.push({ id: `p${pageNumber}cell${cells.length + 1}`, left, diff --git a/src/offscreen/index.test.ts b/src/offscreen/index.test.ts index 9a82e95..42fb123 100644 --- a/src/offscreen/index.test.ts +++ b/src/offscreen/index.test.ts @@ -1,4 +1,5 @@ import { afterEach, expect, it, vi } from 'vitest'; +import type { PdfExtractionErrorDetails } from '../lib/pdf-extraction-error'; const pdfjs = vi.hoisted(() => ({ getDocument: vi.fn(), GlobalWorkerOptions: { workerSrc: '' } })); vi.mock('pdfjs-dist', () => ({ ...pdfjs, OPS: {} })); @@ -55,3 +56,103 @@ it('loads packed CMaps from the extension for an Offscreen extraction request', 'chrome-extension://test-extension/assets/pdf.worker.test.mjs' ); }); + +// The real page extractor owns stage tagging; only PDF.js input and Chrome transport are replaced. +it.each(['page-load', 'text-content', 'operator-list', 'page-layout'] as const)( + 'preserves the first failed page and %s cause without returning partial text', + async (stage) => { + const addListener = vi.fn(); + vi.stubGlobal('chrome', { + runtime: { onMessage: { addListener }, getURL: (path: string) => path }, + }); + const original = Object.assign(new Error('PDF.js page operation failed'), { + name: 'InvalidPDFException', + code: 17, + }); + const page = { + getTextContent: vi.fn(async () => ({ + items: [ + { + str: '部分抽出だけでは要約しない', + transform: [12, 0, 0, 12, 20, 700], + width: 120, + height: 12, + dir: 'ltr', + hasEOL: true, + }, + ], + })), + getOperatorList: vi.fn(async () => ({ fnArray: [] as number[], argsArray: [] })), + cleanup: vi.fn(), + }; + const getPage = vi.fn(async (pageNumber: number) => { + if (pageNumber === 2) { + if (stage === 'page-load') throw original; + if (stage === 'text-content') page.getTextContent.mockRejectedValueOnce(original); + if (stage === 'operator-list') page.getOperatorList.mockRejectedValueOnce(original); + if (stage === 'page-layout') + page.getOperatorList.mockResolvedValueOnce({ fnArray: [999999], argsArray: [] }); + } + return page; + }); + const destroy = vi.fn().mockRejectedValue(new Error('later cleanup failure')); + pdfjs.getDocument.mockReturnValue({ + promise: Promise.resolve({ numPages: 3, getPage, destroy }), + }); + await import('./index'); + const response = await new Promise<{ + success: boolean; + error: string; + pdfExtractionError: PdfExtractionErrorDetails; + }>((resolve) => + addListener.mock.calls[0][0]({ action: 'extractPdfText', pdfData: [] }, {}, resolve) + ); + + expect(response.success).toBe(false); + expect(response).not.toHaveProperty('text'); + expect(response).not.toHaveProperty('pages'); + const originalDetails = + stage === 'page-layout' + ? { + name: 'Error', + code: 'SOURCE_DRAWING', + message: 'SOURCE_DRAWING:PDF.jsの未知の描画演算', + } + : { name: original.name, code: original.code, message: original.message }; + expect(JSON.parse(JSON.stringify(response)).pdfExtractionError).toEqual({ + pageNumber: 2, + stage, + ...originalDetails, + }); + expect(response.error).toBe(`PDF抽出エラー: PDF p.2: ${originalDetails.message}`); + expect(getPage.mock.calls.map(([pageNumber]) => pageNumber)).toEqual([1, 2]); + expect(destroy).toHaveBeenCalledTimes(1); + } +); + +it('preserves a document-load failure before any page is available', async () => { + const addListener = vi.fn(); + vi.stubGlobal('chrome', { + runtime: { onMessage: { addListener }, getURL: (path: string) => path }, + }); + pdfjs.getDocument.mockImplementation(() => ({ + promise: Promise.reject( + Object.assign(new Error('Invalid PDF structure'), { name: 'InvalidPDFException' }) + ), + })); + await import('./index'); + const response = await new Promise((resolve) => + addListener.mock.calls[0][0]({ action: 'extractPdfText', pdfData: [] }, {}, resolve) + ); + expect(response).toEqual({ + success: false, + error: 'PDF抽出エラー: Invalid PDF structure', + pdfExtractionError: { + pageNumber: null, + stage: 'document-load', + name: 'InvalidPDFException', + code: null, + message: 'Invalid PDF structure', + }, + }); +}); diff --git a/src/offscreen/index.ts b/src/offscreen/index.ts index ded9820..163fb36 100644 --- a/src/offscreen/index.ts +++ b/src/offscreen/index.ts @@ -18,6 +18,11 @@ import { type QualityCheckResult, } from '@/lib/section-detector'; import { extractPdfPageLayout } from '@/lib/pdf-layout'; +import { + pdfExtractionError, + withPdfExtractionStage, + type PdfExtractionStage, +} from '@/lib/pdf-extraction-error'; import { validatePages } from '@/lib/fact-validation'; import { buildDocumentContext } from '@/lib/document-context'; import { tableContinuations, noteLinks } from '@/lib/document-links'; @@ -50,7 +55,12 @@ chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => { }) .catch((error) => { console.error('[Offscreen] PDF抽出エラー:', error); - sendResponse({ success: false, error: error.message }); + const failure = pdfExtractionError(error, 'document-load'); + sendResponse({ + success: false, + error: failure.message, + pdfExtractionError: failure.details, + }); }); return true; // 非同期レスポンスを示す } @@ -229,6 +239,7 @@ async function extractTextFromPDF( extractionMode: ExtractionMode, documentType: DocumentType ): Promise { + let stage: PdfExtractionStage = 'document-load'; try { // pdf.jsを動的インポート const pdfjsLib = await import('pdfjs-dist'); @@ -258,38 +269,31 @@ async function extractTextFromPDF( const pages: ExtractedPage[] = []; - // 全ページからテキストを抽出 - for (let pageNum = 1; pageNum <= numPages; pageNum++) { - try { - const page = await pdf.getPage(pageNum); + try { + // 未解析ページを空の成功として扱わず、元のページ・段階・原因で停止する。 + for (let pageNum = 1; pageNum <= numPages; pageNum++) { + const page = await withPdfExtractionStage('page-load', pageNum, () => pdf.getPage(pageNum)); pages.push(await extractPdfPageLayout(page, pageNum, pdfjsLib.OPS)); - - // メモリ解放 - page.cleanup(); - } catch (pageError) { - console.error(`[Offscreen] ページ ${pageNum} の抽出エラー:`, pageError); - pages.push({ - pageNumber: pageNum, - spans: [], - sourceItems: [], - blocks: [], - quantities: [], - drawingOperations: [], - drawingLines: [], - tableRegions: [], - status: 'failed', - selection: 'selected', - text: '', - }); + await withPdfExtractionStage('cleanup', pageNum, () => page.cleanup()); + } + } catch (error) { + // Destroy failures must not replace the original page extraction failure. + try { + await pdf.destroy(); + } catch (cleanupError) { + console.error('[Offscreen] PDF解放エラー:', cleanupError); } + throw error; } - await pdf.destroy(); + await withPdfExtractionStage('cleanup', null, () => pdf.destroy()); + stage = 'validation'; validatePages(pages); if (!pages.some(({ text }) => text.trim())) { throw new Error('PDFからテキストを抽出できませんでした。画像PDFの可能性があります。'); } + stage = 'selection'; // 抽出モード分岐 if (extractionMode === 'full') { // fullモード: 全文返却 @@ -324,11 +328,7 @@ async function extractTextFromPDF( return await extractSmartMode(pages, numPages, documentType); } } catch (error) { - console.error('[Offscreen] PDF抽出エラー:', error); - if (error instanceof Error) { - throw new Error(`PDF抽出エラー: ${error.message}`); - } - throw new Error('PDFからテキストを抽出できませんでした。'); + throw pdfExtractionError(error, stage); } }