diff --git a/docs/additional-analysis.md b/docs/additional-analysis.md index 7333578..cc1ae75 100644 --- a/docs/additional-analysis.md +++ b/docs/additional-analysis.md @@ -1,6 +1,15 @@ # 論点型追加分析とPDF CMap +## 全体の見立てとコンパクトな補足表示(2026-10-10) + +追加分析は同じ1要求の中で `overallSummary: { text, evidenceIds } | null` と個別論点を生成する。全体の見立ては論点より前に常時表示し、個別の結論を流用せず、資料全体の変化・強弱・主要条件を統合するよう指示する。根拠不足ならnullを許し、追加の有料要求や先頭論点からの代替生成は行わない。 + +- 全体の見立ては入力の引用可能な根拠IDと独立照合する。隔離された論点を根拠にせず、入力から根拠本文・属性・ページを再構成する。形式・根拠参照が不正、または個別論点の見出し/結論/読みと完全一致する場合は見立てのみを隔離し、有効な論点は残す。参照の一致は文章の意味検証ではなく、全体の見立てもAI推論として表示する。 +- v4に任意の保存フィールドを追加し、候補・表示文・根拠・注意を保存再検証する。旧v4の見立てなし結果も読めるが、見立てを捏造しない。診断には元応答と隔離理由を保持する。 +- 要約の「補足要約の未整理部分」は本文では短い未反映の注意にし、章別一覧を原文と同じ一つの閉じた展開欄にまとめる。Markdown原本・コピー・原文とページ参照は保持する。 +- 担当テスト: additional-analysis(根拠・隔離・コピー拒否・保存改変)、additional-analysis-transport(実通信コード+固定応答で1要求・診断・復元)、summaryHtmlBuilder(冒頭配置・閉じた根拠・HTMLエスケープ)、summary-presentation(未整理一覧と原文の一体表示)。固定応答の合格は実モデルの統合品質を保証しない。実API評価は未実施。 + ## 追加分析の論点化とCMap同梱(2026-10-06 / v100) 追加分析v3は固定の「解釈・短期・中期・長期」から、通常2〜4件の重要な論点へ変更する。根拠が不足する場合は1件または0件を許し、形式を埋めるための「判断不能」は要求しない。各論点は結論・根拠・条件付きの読み・限界・次の確認点を持つ。 @@ -126,3 +135,6 @@ v121の最終結果: 全1,599件/53ファイル、type-check、lint、buildが 代表的な結合検査で、空ページのみを省いたsmart要約から全文の追加分析・採点へ進むこと、回転文字の原文引用・保存復元、文書種別ごとの目印、原文改変の拒否を確認する。入力指紋をv123へ変更し、旧入力の分析を再利用しない。実モデル品質と未検出のPDF情報に関する従来の検証限界は変わらない。 v123確認: 全1,636件/58ファイル、type-check、lint、build、CMap168個+LICENSEの同梱照合が成功。4資料のオフライン再生で抽出原文の保持と予算を再確認した。回転文字のない安川・26ページ資料では元アイテムの重複追加は0。26ページ資料は約17.1万文字/236KBで既存予算内。過去の点検応答不足・旧形式拒否はv122時の境界を維持し、有料API・新モデル品質・実Chrome確認を成功として追加していない。 + +最終診断の保存に失敗した場合は、結果ID・実行ID・入力ハッシュ・PDF URLが完全一致する最終スナップショットだけを、現在のService Workerのメモリから取得できる。保存済みの途中記録を最終結果として返さない。一時記録は最大12件・全体1MiB・1件256KiBに制限し、JSONには未保存であることと一時的な保持であることを明示する。Worker終了・再起動や上限による除去後は取得できない。画面側は取得中に設定・表示結果が変わった場合の古い応答を破棄する。 +一時診断の`persistenceFailure`は容量不足かその他の保存エラーか、およびread/prepare/writeの失敗箇所のみを記録する。生の保存例外や設定情報を転記せず、生成のoutcome/errorと分ける。 diff --git a/docs/development.md b/docs/development.md index 775d44d..9084224 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1978,3 +1978,11 @@ v96の事前固定2実行は6要求で終了。BlueMeme/Worldとも独立した 境界の担当は `disclosure-observation`(所有者・別名・保存表示)、`additional-analysis`(根拠の投影)、`useSummarize`(実React・保存失敗・明示再試行・ライフサイクル)。生成指示やAPI上限は変更せず、個社/PDF専用分岐や新しい評価基盤は追加しない。旧条件のキャッシュを再利用しないよう分析指紋をv104へ更新する。実API・実Chrome画面の追加確認は行っていない。 最終差分は通常の `npm test`(設定済み2並列)40ファイル/1,254件、type-check・lint・build・diff checkが成功。元の診断応答は19確定事実と24指標/11説明の意味点検到達を保持し、固定した点検応答の表示と保存復元が一致した。固定点検の成功を実モデル品質の証明には数えない。 + +### 2026-10-10: 要約保存と比較・追加分析表示 + +分析指紋v124。原資料で選択済みの累計実績と同一表・同一指標軸にある前年値を、通常の候補検証と一意な比較成立を通して取得する。期間・主体・範囲・会計基準・条件の照合は緩和しない。新しい有料要求は追加しない。 + +256KiB以上の要約キャッシュはgzip/base64で可逆圧縮して保存し、読込時は展開上限32MiBと外側の識別情報を検査した後、従来の原文・要約表示・結果ID検証を通す。旧非圧縮キャッシュも読める。圧縮中の設定変更・キャッシュ削除/置換は保存直前にも検査する。容量不足は他の保存エラーと区別して表示し、既存の要約や診断を自動削除しない。設定のキャッシュ一覧は逐次展開し、一覧用メタデータだけを保持する。 + +v124の保存失敗後の回復: 新しい要約の保存失敗を既存キャッシュの消失と同一視しない。同じキーの保存済み要約を原文・表示・結果IDで再検証し、画面の新しい結果を置換せず再表示可能性を維持する。存在確認の読込失敗や古い読込は「保存なし」ではないため有料生成へ進めない。一覧の「表示」で保存が失われていた場合も、その操作中は生成せず、次の明示的な「要約」を待つ。クリック時には毎回保存を再確認し、現在のキーで正常に「保存なし」と読めた「要約」操作のみ通常生成へ進む。設定変更・削除・置換後の遅い再確認は無効化する。 diff --git a/evaluation/scripts/check-extension-smoke.ts b/evaluation/scripts/check-extension-smoke.ts index 7c46bbc..f420ded 100644 --- a/evaluation/scripts/check-extension-smoke.ts +++ b/evaluation/scripts/check-extension-smoke.ts @@ -7,6 +7,8 @@ import path from 'node:path'; import { pathToFileURL } from 'node:url'; import { validateSavedFacts } from '../../src/lib/fact-cache'; import { validatePresentation } from '../../src/lib/summary-presentation'; +import { decodeSummaryStorage } from '../../src/lib/summary-storage'; +import type { CachedSummary } from '../../src/types/summaryMetadata'; import { SUMMARY_DIAGNOSTICS_KEY } from '../../src/lib/summary-trace'; import { preflightExtensionSmoke, @@ -211,7 +213,7 @@ export async function checkExtensionSmoke(args: string[]) { outcome === 'success' ? 'firstSuccess' : 'partialSuccess' ); assert.equal(stored.caches.length, 1); - const saved = stored.caches[0][1]; + const saved = (await decodeSummaryStorage(stored.caches[0][1])) as CachedSummary; validateSavedFacts(saved.facts); validatePresentation(saved.presentation, saved.facts); assert.deepEqual(saved.facts, preflight.result!.facts); diff --git a/src/background/index.test.ts b/src/background/index.test.ts index b6d587c..eefec03 100644 --- a/src/background/index.test.ts +++ b/src/background/index.test.ts @@ -31,6 +31,8 @@ interface TestResponse { retryExtractionMode?: 'full'; error?: string; diagnosticRunId: string; + diagnosticInputHash?: string; + trace?: import('../lib/analysis-trace').AnalysisDiagnosticExport; diagnosticPersistence?: 'saved' | 'failed'; persistenceWarning?: string; summary: string; @@ -889,6 +891,28 @@ describe('要約・採点・追加分析の分離', () => { expect(result.analysis).toMatchObject({ issues: [] }); expect(result.diagnosticPersistence).toBe('failed'); expect(result.persistenceWarning).toContain('保存できません'); + expect(result.persistenceWarning).toContain('保存容量が不足'); + expect(mocked.generateText).toHaveBeenCalledTimes(2); + const exported = await request({ + action: 'getAnalysisDiagnostic', + runId: result.diagnosticRunId, + summaryResultId: summary.resultId, + inputHash: result.diagnosticInputHash, + }); + expect(exported.trace).toMatchObject({ + runId: result.diagnosticRunId, + outcome: 'success', + volatile: true, + persistenceWarning: expect.stringContaining('未保存'), + persistenceFailure: { code: 'quota', stage: 'write' }, + }); + const wrong = await request({ + action: 'getAnalysisDiagnostic', + runId: result.diagnosticRunId, + summaryResultId: summary.resultId, + inputHash: 'other', + }); + expect(wrong.error).toContain('未保存の最終診断'); expect(mocked.generateText).toHaveBeenCalledTimes(2); vi.mocked(chrome.storage.local.set).mockImplementation(write); const next = await request(followup); diff --git a/src/background/index.ts b/src/background/index.ts index f38baa5..e06b515 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,3 +1,4 @@ +import { summaryStorageWarning } from '@/lib/summary-storage'; import { saveSummaryTrace, summaryBuildDigest, @@ -12,6 +13,7 @@ import { } from '@/lib/pdf-extraction-error'; import { saveAnalysisTrace, + loadAnalysisTrace, nextAnalysisIntentAt, type AnalysisTrace, type AnalysisGenerationDiagnostic, @@ -92,28 +94,59 @@ chrome.runtime.onInstalled.addListener((details) => { }) .catch((error) => console.error('旧ホスト権限の削除に失敗:', error)); }); -chrome.runtime.onMessage.addListener((request: Request, _sender, sendResponse) => { - if (!['summarize', 'score', 'analyze'].includes(request.action)) return; - const diagnosticRunId = request.action !== 'score' ? crypto.randomUUID() : null; - const task = - request.action === 'summarize' - ? handleSummarize(request, diagnosticRunId!) - : request.action === 'analyze' - ? handleAnalyze(request, diagnosticRunId!) - : handleFollowup(request); - task - .then((response) => - sendResponse({ ...response, ...(diagnosticRunId ? { diagnosticRunId } : {}) }) - ) - .catch((error) => - sendResponse({ - error: error instanceof Error ? error.message : String(error), - ...(error instanceof SummarySourceSelectionError ? { retryExtractionMode: 'full' } : {}), - ...(diagnosticRunId ? { diagnosticRunId } : {}), - }) - ); - return true; -}); +interface AnalysisDiagnosticRequest { + action: 'getAnalysisDiagnostic'; + pdfUrl: string; + runId: string; + summaryResultId: string; + inputHash: string; +} +chrome.runtime.onMessage.addListener( + (request: Request | AnalysisDiagnosticRequest, _sender, sendResponse) => { + if (request.action === 'getAnalysisDiagnostic') { + if ( + ![request.pdfUrl, request.runId, request.summaryResultId, request.inputHash].every( + (value) => typeof value === 'string' && value.length > 0 + ) + ) { + sendResponse({ error: '追加分析診断の識別情報が不正です' }); + return; + } + void loadAnalysisTrace( + request.pdfUrl, + request.runId, + request.summaryResultId, + 'failed', + request.inputHash + ) + .then((trace) => sendResponse({ trace })) + .catch((error) => + sendResponse({ error: error instanceof Error ? error.message : String(error) }) + ); + return true; + } + if (!['summarize', 'score', 'analyze'].includes(request.action)) return; + const diagnosticRunId = request.action !== 'score' ? crypto.randomUUID() : null; + const task = + request.action === 'summarize' + ? handleSummarize(request, diagnosticRunId!) + : request.action === 'analyze' + ? handleAnalyze(request, diagnosticRunId!) + : handleFollowup(request); + task + .then((response) => + sendResponse({ ...response, ...(diagnosticRunId ? { diagnosticRunId } : {}) }) + ) + .catch((error) => + sendResponse({ + error: error instanceof Error ? error.message : String(error), + ...(error instanceof SummarySourceSelectionError ? { retryExtractionMode: 'full' } : {}), + ...(diagnosticRunId ? { diagnosticRunId } : {}), + }) + ); + return true; + } +); // Concurrent summary/follow-up requests share only the in-flight initialization. // Recheck contexts after completion so a subsequently closed document is recreated. @@ -359,22 +392,23 @@ async function handleAnalyze(request: FollowupRequest, runId: string) { elapsedMs: 0, }; let diagnosticPersistence: DiagnosticPersistence = 'failed'; + let diagnosticWarning: string | undefined; const saveTrace = async () => { trace.elapsedMs = Math.round(performance.now() - started); try { await saveAnalysisTrace(trace); diagnosticPersistence = 'saved'; - } catch { + diagnosticWarning = undefined; + } catch (error) { diagnosticPersistence = 'failed'; + diagnosticWarning = summaryStorageWarning(error, '追加分析の診断'); } }; let terminalObserved = false; const metadata = () => ({ diagnosticPersistence, diagnosticInputHash: trace.inputHash, - ...(diagnosticPersistence === 'failed' - ? { persistenceWarning: '追加分析の診断を保存できませんでした' } - : {}), + ...(diagnosticPersistence === 'failed' ? { persistenceWarning: diagnosticWarning } : {}), }); try { trace.pdfUrl = fullUrl(request.pdfUrl); @@ -459,7 +493,9 @@ async function handleFollowup( ) throw new Error('要約結果の識別子が一致しません'); if (request.action === 'analyze') - return { analysis: await analyzeFacts(config, facts, presentation, diagnostic, extraction.pages) }; + return { + analysis: await analyzeFacts(config, facts, presentation, diagnostic, extraction.pages), + }; if (!settings.experimentalScoring) throw new Error('実験的スコアがOFFです'); const score = await attachScore( config, diff --git a/src/content/SummaryButton.tsx b/src/content/SummaryButton.tsx index 20b374b..2d799d0 100644 --- a/src/content/SummaryButton.tsx +++ b/src/content/SummaryButton.tsx @@ -3,7 +3,7 @@ import { createPortal } from 'react-dom'; import { useSummarize } from './hooks/useSummarize'; import { useSummaryRow } from './hooks/useSummaryRow'; import { loadSummaryTrace } from '@/lib/summary-trace'; -import { loadAnalysisTrace } from '@/lib/analysis-trace'; +import { requestAnalysisTrace } from '@/lib/analysis-trace'; import { ACTION_BUTTON_STYLE, BUTTON_STYLES, FOCUS_STYLE } from './constants/styles'; interface RowData { @@ -156,7 +156,7 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } try { const trace = kind !== 'summary' - ? await loadAnalysisTrace( + ? await requestAnalysisTrace( rowData.pdfUrl, kind === 'analysis' ? analysis.diagnosticRunId! : lastAnalysisRunId!, result!.resultId!, @@ -207,8 +207,11 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } // 同一描画内の連打でも要約要求は一度だけ送る。 if (summaryPending.current) return; + const requestedGeneration = !hasCached; const request = (async () => { - if (!hasCached || !(await showCached())) await summarize(); + // Always read first: availability can be unknown after a storage error. + // A click on 表示 must never silently turn into a paid generation. + if (!(await showCached()) && requestedGeneration) await summarize(); })(); summaryPending.current = request; const clearPending = () => { diff --git a/src/content/hooks/useSummarize.test.ts b/src/content/hooks/useSummarize.test.ts index c39951b..aea962c 100644 --- a/src/content/hooks/useSummarize.test.ts +++ b/src/content/hooks/useSummarize.test.ts @@ -1,4 +1,6 @@ // @vitest-environment jsdom +import { encodeSummaryStorage } from '@/lib/summary-storage'; +import type { CachedSummary } from '@/types/summaryMetadata'; /// import { webcrypto } from 'node:crypto'; import { setImmediate } from 'node:timers/promises'; @@ -1136,11 +1138,87 @@ describe('実Reactの要約行アクション配置', () => { await click(copy); expect( summaryRow.querySelector('[data-diagnostic-root] [role="alert"]')?.textContent - ).toContain('保存できません'); + ).toContain('対応する診断がありません'); expect(writeText).not.toHaveBeenCalled(); - expect(sendMessage).toHaveBeenCalledTimes(2); + expect(sendMessage).toHaveBeenCalledTimes(3); + expect(sendMessage).toHaveBeenLastCalledWith( + expect.objectContaining({ + action: 'getAnalysisDiagnostic', + runId: 'unsaved-analysis', + summaryResultId: response.resultId, + }) + ); }); + it.each([false, true])( + '未保存の最終診断をコピーし、遅い応答は新しい画面へ混ぜない(切替=%s)', + async (changed) => { + settings.experimentalScoring = false; + const response = await responseFor(); + const input = buildAnalysisInput(facts, presentation); + const diagnostic = deferred(); + sendMessage + .mockResolvedValueOnce(response) + .mockResolvedValueOnce({ + analysis: additionalAnalysis, + diagnosticRunId: 'volatile-final', + diagnosticPersistence: 'failed', + diagnosticInputHash: input.inputHash, + }) + .mockReturnValueOnce(diagnostic.promise); + const writeText = vi.fn().mockResolvedValue(undefined); + vi.stubGlobal('navigator', { clipboard: { writeText } }); + const { row, button } = await mountButton(); + await click(button); + const summaryRow = await summaryRowFor(row); + await click(summaryRow.querySelector('#analyze-btn')!); + const copy = Array.from( + summaryRow.querySelectorAll('[data-diagnostic-root] button') + ).find((b) => b.textContent === '表示中の追加分析の診断JSONをコピー')!; + await click(copy); + if (changed) await changeSettings({ model: 'new-model' }); + const trace = { + version: 1, + stage: 'analysis', + runId: 'volatile-final', + summaryResultId: response.resultId, + startedAt: '2026-10-10T00:00:00Z', + intentAt: 1, + pdfUrl, + provider: 'openai', + model: 'fixture', + buildDigest: 'build', + fingerprint: response.metadata.analysisFingerprint, + inputHash: input.inputHash, + input, + contract: null, + response: '{}', + usage: null, + outcome: 'success', + error: null, + elapsedMs: 1, + volatile: true, + persistenceFailure: { code: 'quota', stage: 'write' }, + }; + await act(async () => { + diagnostic.resolve({ trace }); + }); + if (changed) { + expect(writeText).not.toHaveBeenCalled(); + expect(document.querySelector('[data-diagnostic-root] textarea')).toBeNull(); + } else { + const exported = JSON.parse(writeText.mock.calls[0][0]); + expect(exported.runId).toBe('volatile-final'); + expect(exported.volatile).toBe(true); + expect(exported.persistenceWarning).toContain('未保存'); + expect(summaryRow.querySelector('[data-diagnostic-root] textarea')?.textContent).toContain( + 'volatile-final' + ); + } + expect(sendMessage).toHaveBeenCalledTimes(3); + } + ); + it.each(['throw', 'reject'] as const)( '診断読込の無効化エラー(%s)はページ再読み込みを案内し、コピー成功にしない', async (failure) => { @@ -1275,6 +1353,241 @@ describe('保存失敗と一覧ライフサイクルの回復', () => { expect(button.textContent).toBe('閉じる'); }); + it.each([false, true])( + '再要約の上書き保存失敗後も、以前の有効な保存を無料で再表示する(圧縮=%s)', + async (compressed) => { + settings.experimentalScoring = false; + const response = await responseFor(); + const key = await keyFor('full'); + const old: CachedSummary = { + ...response, + ...options, + diagnosticRunId: 'old-cached-run', + cachedAt: 1, + metadata: { + ...response.metadata, + totalPages: 2, + extractedPages: [1, 2], + extractionMode: 'full', + }, + title: compressed ? '保存内容'.repeat(100000) : options.title, + }; + stored[key] = compressed ? await encodeSummaryStorage(old) : old; + const previous = structuredClone(stored[key]); + save.mockRejectedValue(new Error('QUOTA_BYTES')); + sendMessage.mockResolvedValue({ ...response, diagnosticRunId: 'new-unsaved-run' }); + const { row, button } = await mountButton(); + await vi.waitFor(() => expect(button.textContent).toBe('表示')); + await click(button); + let shown = await summaryRowFor(row); + expect(sendMessage).not.toHaveBeenCalled(); + await click(shown.querySelector('#resummarize-btn')!); + shown = await summaryRowFor(row); + await vi.waitFor(() => + expect(shown.querySelector('[data-persistence-warning]')?.textContent).toContain( + '以前の保存済み要約' + ) + ); + expect(shown.textContent).toContain('100百万円'); + expect(sendMessage).toHaveBeenCalledOnce(); + expect(stored[key]).toEqual(previous); + await click(button); + expect(button.textContent).toBe('表示'); + await click(button); + shown = await summaryRowFor(row); + expect(shown.textContent).toContain('100百万円'); + expect(sendMessage).toHaveBeenCalledOnce(); + expect(remove).not.toHaveBeenCalled(); + } + ); + + it('保存失敗後の再確認は新しい表示中の実行を旧保存結果で置き換えない', async () => { + const response = await responseFor(); + const key = await keyFor('full'); + stored[key] = { ...response, diagnosticRunId: 'old-cached-run' }; + save.mockRejectedValue(new Error('QUOTA_BYTES')); + sendMessage.mockResolvedValue({ ...response, diagnosticRunId: 'new-unsaved-run' }); + const hook = await mount(); + await act(async () => { + await hook().showCached(); + }); + expect(hook().result?.diagnosticRunId).toBe('old-cached-run'); + await act(async () => { + await hook().summarize(); + }); + expect(hook().hasCached).toBe(true); + expect(hook().result?.diagnosticRunId).toBe('new-unsaved-run'); + await act(async () => { + hook().reset(); + }); + await act(async () => { + await hook().showCached(); + }); + expect(hook().result?.diagnosticRunId).toBe('old-cached-run'); + expect(sendMessage).toHaveBeenCalledOnce(); + }); + + it('初回の存在確認が失敗しても、クリック時に残っている保存を読み、有料生成しない', async () => { + settings.experimentalScoring = false; + stored[await keyFor('full')] = await responseFor(); + const get = chrome.storage.local.get; + chrome.storage.local.get = vi + .fn() + .mockRejectedValueOnce(new Error('temporary read failure')) + .mockImplementation(get); + const { row, button } = await mountButton(); + expect(button.textContent).toBe('要約'); + await click(button); + expect((await summaryRowFor(row)).textContent).toContain('100百万円'); + expect(sendMessage).not.toHaveBeenCalled(); + }); + + it.each(['settings', 'deleted', 'replaced'])( + '保存失敗後の遅い再確認が%s後の状態を上書きしない', + async (change) => { + const response = await responseFor(); + const key = await keyFor('full'); + stored[key] = response; + const hook = await mount(); + await vi.waitFor(() => expect(hook().hasCached).toBe(true)); + const readback = deferred>(); + let failedSave = false, + reading = false; + const get = chrome.storage.local.get; + chrome.storage.local.get = vi.fn().mockImplementation((...args) => { + if (failedSave && !reading) { + reading = true; + return readback.promise; + } + return get(...args); + }); + save.mockImplementation(async () => { + failedSave = true; + throw new Error('QUOTA_BYTES'); + }); + sendMessage.mockResolvedValue(response); + let pending!: Promise; + await act(async () => { + pending = hook().summarize(); + }); + await vi.waitFor(() => expect(reading).toBe(true)); + if (change === 'settings') await changeSettings({ model: 'new-model' }); + else + await act(async () => { + if (change === 'deleted') delete stored[key]; + for (const listener of listeners) listener({ [key]: { newValue: stored[key] } }, 'local'); + }); + await vi.waitFor(() => expect(hook().hasCached).toBe(change === 'replaced')); + await act(async () => { + readback.resolve(change === 'replaced' ? {} : { [key]: response }); + await pending; + }); + expect(hook().hasCached).toBe(change === 'replaced'); + expect(sendMessage).toHaveBeenCalledOnce(); + if (change === 'settings') expect(hook().result).toBeNull(); + else expect(hook().result?.summary).toBe(response.summary); + } + ); + + it.each(['deleted', 'replaced'])( + '圧縮中にキャッシュが%sになったら遅い保存で上書きしない', + async (change) => { + const response = await responseFor(); + const gate = deferred(); + let compressing = false; + vi.stubGlobal( + 'CompressionStream', + class { + readable: ReadableStream; + writable: WritableStream; + constructor() { + const stream = new TransformStream({ + async transform(chunk, controller) { + compressing = true; + await gate.promise; + controller.enqueue(chunk); + }, + }); + this.readable = stream.readable; + this.writable = stream.writable; + } + } + ); + sendMessage.mockResolvedValue({ + ...response, + metadata: { ...response.metadata, qualityWarnings: ['原文'.repeat(150000)] }, + }); + const hook = await mount(); + let pending!: Promise; + await act(async () => { + pending = hook().summarize(); + }); + await vi.waitFor(() => expect(compressing).toBe(true)); + const key = await keyFor('full'); + if (change === 'replaced') stored[key] = response; + await act(async () => { + for (const listener of listeners) listener({ [key]: { newValue: stored[key] } }, 'local'); + gate.resolve(); + await pending; + }); + expect(save).not.toHaveBeenCalled(); + expect(hook().result?.summary).toBe(response.summary); + expect(remove).not.toHaveBeenCalled(); + } + ); + + it('大きい要約を圧縮保存し、再表示で原文と結果IDを通常どおり検証する', async () => { + const response = await responseFor(); + const entry: CachedSummary = { + ...response, + ...options, + metadata: { + ...response.metadata, + totalPages: 2, + extractedPages: [1, 2], + extractionMode: 'full', + }, + title: '原文'.repeat(150000), + cachedAt: 1, + }; + const key = await keyFor('full'); + stored[key] = await encodeSummaryStorage(entry); + expect(stored[key]).toHaveProperty('encoding', 'tdnet-summary-gzip-v1'); + const hook = await mount(); + await vi.waitFor(() => expect(hook().hasCached).toBe(true)); + await act(async () => { + await hook().showCached(); + }); + expect(hook().result?.summary).toBe(response.summary); + expect(hook().result?.resultId).toBe(response.resultId); + expect(sendMessage).not.toHaveBeenCalled(); + stored[key] = await encodeSummaryStorage({ ...entry, summary: '改変された要約' }); + await act(async () => { + await hook().showCached(); + }); + expect(hook().result?.summary).toBeNull(); + expect(hook().result?.error).toContain('不正'); + }); + + it('壊れた圧縮保存を拒否し、保存容量以外の失敗を区別する', async () => { + const key = await keyFor('full'); + stored[key] = { encoding: 'tdnet-summary-gzip-v1', payload: 'broken', decodedBytes: 10 }; + const hook = await mount(); + await act(async () => { + await hook().showCached(); + }); + expect(hook().hasCached).toBe(false); + expect(hook().result?.error).toContain('不正'); + save.mockRejectedValue(new Error('Extension context invalidated')); + sendMessage.mockResolvedValue(await responseFor()); + await act(async () => { + await hook().summarize(); + }); + expect(hook().result?.summary).toBeTruthy(); + expect(hook().persistenceWarning).toContain('拡張機能が有効か'); + expect(hook().persistenceWarning).not.toContain('容量が不足'); + }); + it('保存失敗を生成失敗にせず、要約・追加分析を表示して保存警告を分ける', async () => { settings.experimentalScoring = false; const response = await responseFor(); @@ -1309,13 +1622,16 @@ describe('保存失敗と一覧ライフサイクルの回復', () => { '要約を保存できませんでした' ); expect(summaryRow.querySelector('#score-result')!.previousElementSibling).toBe(body); + expect(summaryRow.querySelector('[data-persistence-warning]')?.textContent).toContain( + '要約キャッシュ管理' + ); expect(button.textContent).toBe('閉じる'); await click(button); expect(button.textContent).toBe('要約'); expect(remove).not.toHaveBeenCalled(); }); - it('削除通知で表示を要約へ戻し、通知前のキャッシュミスも一度のクリックで回復する', async () => { + it('表示操作のキャッシュ消失・読込失敗を有料生成へ切り替えない', async () => { settings.experimentalScoring = false; const response = await responseFor(); const key = await keyFor('full'); @@ -1338,6 +1654,10 @@ describe('保存失敗と一覧ライフサイクルの回復', () => { button.click(); button.click(); }); + expect(sendMessage).not.toHaveBeenCalled(); + expect(button.textContent).toBe('要約'); + // Only the next explicit 要約 click authorizes generation after a confirmed miss. + await click(button); await summaryRowFor(row); expect(sendMessage).toHaveBeenCalledTimes(1); expect(button.textContent).toBe('閉じる'); @@ -1349,7 +1669,8 @@ describe('保存失敗と一覧ライフサイクルの回復', () => { .mockImplementation(get); await click(button); await summaryRowFor(row); - expect(sendMessage).toHaveBeenCalledTimes(2); + expect(sendMessage).toHaveBeenCalledTimes(1); + expect(row.nextElementSibling?.textContent).toContain('保存された要約を読み込めませんでした'); expect(button.textContent).toBe('閉じる'); }); @@ -1379,7 +1700,7 @@ describe('保存失敗と一覧ライフサイクルの回復', () => { availability.resolve({ [key]: response }); await showing; }); - expect(await showing).toBe(false); + expect(await showing).toBe(true); expect(hook().hasCached).toBe(false); expect(hook().result).toBeNull(); expect(sendMessage).not.toHaveBeenCalled(); diff --git a/src/content/hooks/useSummarize.ts b/src/content/hooks/useSummarize.ts index 9f56168..9c47d7a 100644 --- a/src/content/hooks/useSummarize.ts +++ b/src/content/hooks/useSummarize.ts @@ -1,3 +1,8 @@ +import { + decodeSummaryStorage, + encodeSummaryStorage, + summaryStorageWarning, +} from '@/lib/summary-storage'; import { useState, useEffect, useCallback, useRef } from 'react'; import type { DiagnosticPersistence } from '@/lib/summary-trace'; import { @@ -86,6 +91,28 @@ function isCachedSummary(value: unknown, key: string, pdfUrl: string): value is } } +async function validatedCachedSummary( + value: unknown, + key: string, + pdfUrl: string +): Promise { + try { + const entry = await decodeSummaryStorage(value); + if (!isCachedSummary(entry, key, pdfUrl)) return null; + return (await summaryResultId( + pdfUrl, + entry.metadata.analysisFingerprint!, + entry.facts, + entry.metadata.documentHash!, + entry.presentation + )) === entry.resultId + ? entry + : null; + } catch { + return null; + } +} + export function useSummarize({ pdfUrl, title, code, companyName }: Options) { const [loading, setLoading] = useState(false); const [result, setResult] = useState(null); @@ -229,17 +256,8 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { cacheKey === keyRef.current; try { const data = await chrome.storage.local.get(storageKey); - const entry = data[storageKey] as CachedSummary | undefined; - const valid = - isCachedSummary(entry, cacheKey, pdfUrl) && - (await summaryResultId( - pdfUrl, - entry.metadata.analysisFingerprint!, - entry.facts, - entry.metadata.documentHash!, - entry.presentation - )) === entry.resultId; - if (current()) setHasCached(valid); + const entry = await validatedCachedSummary(data[storageKey], cacheKey, pdfUrl); + if (current()) setHasCached(entry !== null); } catch { if (current()) setHasCached(false); } @@ -408,40 +426,48 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { try { data = await chrome.storage.local.get(SUMMARY_PREFIX + key); } catch { - if (key !== keyRef.current || run !== runRef.current) return true; - invalidate(); - return false; + if ( + !mountedRef.current || + key !== keyRef.current || + run !== runRef.current || + revision !== cacheRevisionRef.current + ) + return true; + // A failed read is not an absent entry and must never authorize a paid retry. + setResult({ + summary: null, + metadata: null, + facts: null, + presentation: null, + resultId: null, + diagnosticRunId: null, + error: + '保存された要約を読み込めませんでした。時間をおいて再度表示してください。再要約は新たな生成を行います。', + }); + return true; } - if (key !== keyRef.current || run !== runRef.current) return true; - const entry = data[SUMMARY_PREFIX + key] as CachedSummary | undefined; - const valid = - isCachedSummary(entry, key, pdfUrl) && - (await summaryResultId( - pdfUrl, - entry.metadata.analysisFingerprint!, - entry.facts, - entry.metadata.documentHash!, - entry.presentation - )) === entry.resultId; - if (key !== keyRef.current || run !== runRef.current) return true; - if (revision !== cacheRevisionRef.current || !valid) { + if (!mountedRef.current || key !== keyRef.current || run !== runRef.current) return true; + const value = data[SUMMARY_PREFIX + key]; + const entry = await validatedCachedSummary(value, key, pdfUrl); + if (!mountedRef.current || key !== keyRef.current || run !== runRef.current) return true; + // A newer storage event owns availability. Do not clear its state or convert + // an interrupted lookup into permission to generate a new paid summary. + if (revision !== cacheRevisionRef.current) return true; + if (!entry) { invalidate(); - if (revision !== cacheRevisionRef.current || entry === undefined) return false; - } - if (!valid) { - if (entry !== undefined) - setResult({ - summary: null, - metadata: null, - facts: null, - presentation: null, - resultId: null, - diagnosticRunId: null, - error: '保存された現行要約の形式・原数量・設定が不正です。再要約してください。', - }); + if (value === undefined) return false; + setResult({ + summary: null, + metadata: null, + facts: null, + presentation: null, + resultId: null, + diagnosticRunId: null, + error: '保存された現行要約の形式・原数量・設定が不正です。再要約してください。', + }); return true; } - if (!entry) return false; + setHasCached(true); idRef.current = entry.resultId; setPersistenceWarning(entry.metadata.persistenceWarning ?? null); setResult({ @@ -556,7 +582,15 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { }; const cacheRevision = cacheRevisionRef.current; try { - await chrome.storage.local.set({ [SUMMARY_PREFIX + key]: entry }); + const storedEntry = await encodeSummaryStorage(entry); + if ( + !mountedRef.current || + run !== runRef.current || + key !== keyRef.current || + cacheRevision !== cacheRevisionRef.current + ) + return; + await chrome.storage.local.set({ [SUMMARY_PREFIX + key]: storedEntry }); if ( run === runRef.current && key === keyRef.current && @@ -567,13 +601,37 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { cacheRevisionRef.current++; setHasCached(true); } - } catch { - if (run === runRef.current) { - setPersistenceWarning( - [diagnosticWarning, '要約を保存できませんでした。表示結果は利用できます'] - .filter(Boolean) - .join(' / ') - ); + } catch (error) { + if (run === runRef.current && key === keyRef.current) { + const warning = [diagnosticWarning, summaryStorageWarning(error)] + .filter(Boolean) + .join(' / '); + setPersistenceWarning(warning); + // Replacement failure says nothing about the previous entry. Re-read + // and validate it without replacing the fresh visible result. + if (cacheRevision === cacheRevisionRef.current) { + try { + const saved = await chrome.storage.local.get(SUMMARY_PREFIX + key); + const prior = await validatedCachedSummary( + saved[SUMMARY_PREFIX + key], + key, + pdfUrl + ); + if ( + mountedRef.current && + run === runRef.current && + key === keyRef.current && + cacheRevision === cacheRevisionRef.current + ) { + setHasCached(prior !== null); + if (prior) + setPersistenceWarning(`${warning}。以前の保存済み要約は引き続き表示できます`); + } + } catch { + // Unknown availability is not a cache miss. Keep prior knowledge; + // reopening always performs a fresh read before any generation. + } + } } } } catch (error) { @@ -688,7 +746,7 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { } : {}), }); - } catch { + } catch (error) { if (isCurrent()) set({ loading: false, @@ -697,7 +755,7 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { ...diagnosticState(), persistenceWarning: [ diagnosticWarning, - `${action === 'score' ? '採点' : '追加分析'}を保存できませんでした。表示結果は利用できます`, + summaryStorageWarning(error, action === 'score' ? '採点' : '追加分析'), ] .filter(Boolean) .join(' / '), diff --git a/src/content/utils/markdownParser.ts b/src/content/utils/markdownParser.ts index 22f5714..611f051 100644 --- a/src/content/utils/markdownParser.ts +++ b/src/content/utils/markdownParser.ts @@ -193,17 +193,20 @@ export function parseSummaryMarkdown(markdown: string, pdfUrl?: string): string let start = 0; for (let i = 0; i < tokens.length; i++) { const token = tokens[i]; + if (token.type !== 'heading') continue; + const unresolved = token.depth === 2 && token.text === '補足要約の未整理部分'; if ( - token.type !== 'heading' || - token.depth !== 3 || - !['原文を見る', '確認の詳細(原文)'].includes(token.text) + !unresolved && + (token.depth !== 3 || !['原文を見る', '確認の詳細(原文)'].includes(token.text)) ) continue; html += parser.parser(tokens.slice(start, i)); let end = i + 1; while (end < tokens.length) { const next = tokens[end]; - if (next.type === 'heading' && next.depth <= 3) break; + if (next.type === 'heading' && next.depth <= (unresolved ? 2 : 3)) { + if (!(unresolved && next.depth === 2 && next.text === '原文')) break; + } end++; } const pages = new Set(); @@ -211,18 +214,32 @@ export function parseSummaryMarkdown(markdown: string, pdfUrl?: string): string if (item.type === 'link' && /^tdnet-page:[1-9]\d*$/.test(item.href)) pages.add(Number(item.href.slice('tdnet-page:'.length))); }); + const body = tokens + .slice(i + 1, end) + .filter( + (item) => + !( + unresolved && + item.type === 'heading' && + ((item.depth === 2 && item.text === '原文') || + (item.depth === 3 && item.text === '原文を見る')) + ) + ); const label = - token.text + + (unresolved ? '原文を見る' : token.text) + (pages.size ? `(${[...pages] .sort((a, b) => a - b) .map((page) => `p.${page}`) .join('、')})` : ''); + if (unresolved) + html += + '

一部の補足説明・数値は要約に反映できていません。

'; html += '
' + `${escapeHtml(label)}` + - parser.parser(tokens.slice(i + 1, end)) + + parser.parser(body) + '
'; start = end; i = end - 1; diff --git a/src/content/utils/summaryHtmlBuilder.test.ts b/src/content/utils/summaryHtmlBuilder.test.ts index fbdea5f..7764787 100644 --- a/src/content/utils/summaryHtmlBuilder.test.ts +++ b/src/content/utils/summaryHtmlBuilder.test.ts @@ -63,6 +63,10 @@ describe('追加分析の表示境界', () => { const data: AdditionalAnalysis = { version: 4, inputHash: 'fixture', + overallSummary: { + text: `売上の伸びと利益の質を総合して見る必要があります ${unsafe}`, + evidenceIds: ['fact:verified'], + }, candidates: [ { ...issue, title: '隔離した本文は見せない', evidenceIds: ['missing'] }, issue, @@ -141,13 +145,23 @@ describe('追加分析の表示境界', () => { expect(notices.textContent).not.toContain('元の論点0'); expect(root.textContent).not.toContain('隔離した本文は見せない'); expect(root.querySelectorAll('img,script,[onerror]')).toHaveLength(0); + const overview = root.querySelector('[data-analysis-overall-summary]')!; + expect(overview.textContent).toContain(data.overallSummary!.text); + expect(overview.closest('details')).toBeNull(); + expect(overview.querySelector('details')!.open).toBe(false); + expect(overview.textContent).toContain('確認済み事実'); + if (!allQuarantined) + expect( + overview.compareDocumentPosition(root.querySelector('article')!) & + Node.DOCUMENT_POSITION_FOLLOWING + ).toBeTruthy(); expect(root.querySelectorAll('article')).toHaveLength(allQuarantined ? 0 : 1); if (allQuarantined) { expect(root.textContent).toContain('表示できる論点はありません'); expect(notices.textContent).toContain('非表示の論点2件'); } else { expect(notices.textContent).toContain('元の論点2・注意: 文章が長い'); - expect(root.querySelector('h6')?.textContent).toBe(issue.title); + expect(root.querySelector('article h6')?.textContent).toBe(issue.title); expect(root.querySelector('[data-analysis-conclusion]')?.closest('details')).toBeNull(); expect(root.querySelector('[data-analysis-reading]')?.textContent).toBe(issue.reading); const evidenceDetails = root.querySelector('[data-analysis-evidence]')!; diff --git a/src/content/utils/summaryHtmlBuilder.ts b/src/content/utils/summaryHtmlBuilder.ts index f47e164..4b1c27f 100644 --- a/src/content/utils/summaryHtmlBuilder.ts +++ b/src/content/utils/summaryHtmlBuilder.ts @@ -220,21 +220,37 @@ function buildAnalysisHtml( `; }) .join(''); + const overview = analysis.overallSummary; + const overviewEvidence = + overview?.evidenceIds.map((id) => { + const item = analysis.evidence.find((e) => e.id === id); + if (!item) throw new Error('全体要約の根拠が表示結果にありません'); + return item; + }) ?? []; + const overallSummary = overview + ? `
+
全体の見立て
+

${escapeMetadataText(overview.text)}

+
全体の見立ての根拠 +
    ${overviewEvidence.map((e) => `
  • ${labels[e.kind]}: ${escapeMetadataText(e.text)}${e.context ? ` ${escapeMetadataText(e.context)}` : ''} ${e.pages.map(link).join('・')}
  • `).join('')}
+
` + : ''; const c = analysis.coverage; const quarantined = new Set( analysis.notices - .filter((notice) => notice.severity === 'quarantined') + .filter((notice) => notice.severity === 'quarantined' && notice.issueIndex >= 0) .map((notice) => notice.issueIndex) ).size; const notices = analysis.notices.length ? `
追加分析の確認事項 -

表示できる論点${analysis.issues.length}件${quarantined ? ` / 非表示の論点${quarantined}件` : ''}。注意付きの推論文は生成されたまま表示しています。形式・根拠参照が不正な論点は本文を表示していません。

-
    ${analysis.notices.map((notice) => `
  • ${notice.issueIndex < 0 ? '応答全体' : `元の論点${notice.issueIndex + 1}`}・${notice.severity === 'quarantined' ? '非表示' : '注意'}: ${escapeMetadataText(notice.message)}(${escapeMetadataText(notice.code)} / ${escapeMetadataText(notice.path)})
  • `).join('')}
+

表示できる論点${analysis.issues.length}件${quarantined ? ` / 非表示の論点${quarantined}件` : ''}。注意付きの推論文は生成されたまま表示しています。形式・根拠参照が不正な文章は本文を表示していません。

+
    ${analysis.notices.map((notice) => `
  • ${notice.path.startsWith('$.overallSummary') ? '全体の見立て' : notice.issueIndex < 0 ? '応答全体' : `元の論点${notice.issueIndex + 1}`}・${notice.severity === 'quarantined' ? '非表示' : '注意'}: ${escapeMetadataText(notice.message)}(${escapeMetadataText(notice.code)} / ${escapeMetadataText(notice.path)})
  • `).join('')}
` : ''; return `

AIによる分析です。本文の数値、因果関係、情報がないという主張には未検証の推論を含みます。根拠IDの一致は意味の検証ではありません。展開した根拠欄で、確認済み事実・会社説明・機械計算・抽出原文を区別しています。

${notices} + ${overallSummary} ${issues || (quarantined ? '

表示できる論点はありません。形式・根拠参照の確認で全論点を非表示にしました。

' : '

提供された入力から、要約に追加できる論点を生成できませんでした。

')} ${c.organizationStatus !== 'ready' || c.unresolvedSources ? '

一部の説明・指標は意味の点検が済んでいません。分析にない事項も原PDFを確認してください。

' : ''} ${c.limitations.map((text) => `

${escapeMetadataText(text)}

`).join('')} diff --git a/src/lib/additional-analysis-transport.test.ts b/src/lib/additional-analysis-transport.test.ts index 3bec069..f2f94e3 100644 --- a/src/lib/additional-analysis-transport.test.ts +++ b/src/lib/additional-analysis-transport.test.ts @@ -24,6 +24,10 @@ const facts = parseFactSummary( const presentation = buildPresentation(facts, [page]); const raw = JSON.stringify({ version: 4, + overallSummary: { + text: '利益計上は確認できますが、持続性の評価には収益要因の裏付けが必要です。', + evidenceIds: [`fact:${facts.facts[0].id}`], + }, issues: [ { title: '利益の継続性', @@ -70,6 +74,7 @@ describe('追加分析の通信から保存までの出力上限境界', () => { snapshots.push(value); }); expect(result.issues).toHaveLength(1); + expect(result.overallSummary).toEqual(JSON.parse(raw).overallSummary); expect(result.notices).toContainEqual( expect.objectContaining({ code: 'output_limit', issueIndex: -1, severity: 'warning' }) ); diff --git a/src/lib/additional-analysis.test.ts b/src/lib/additional-analysis.test.ts index 4d64857..dfddd14 100644 --- a/src/lib/additional-analysis.test.ts +++ b/src/lib/additional-analysis.test.ts @@ -56,6 +56,67 @@ afterEach(() => { }); describe('追加分析の根拠と論点の契約', () => { + // New boundary: independently grounded overview survives issue quarantine; + // invalid/duplicated overviews cannot replace it with an arbitrary first issue. + it('全体要約を独立した根拠で照合し、保存復元と改変拒否を保つ', () => { + const overview = { + text: '会社説明を踏まえると、計上利益の強さと持続性を分けて評価する局面です。', + evidenceIds: ['explanation:explanation-0'], + }; + const result = parseAnalysisResponse( + JSON.stringify({ + version: 4, + overallSummary: overview, + issues: [{ ...issue(), evidenceIds: ['missing'] }], + }), + input() + ); + expect(result.overallSummary).toEqual(overview); + expect(result.issues).toEqual([]); + expect(result.evidence.map((e) => e.id)).toEqual(overview.evidenceIds); + expect(parseAnalysis(JSON.stringify(result), facts, presentation)).toEqual(result); + for (const patch of [ + { overallSummary: { ...overview, text: '改変された文章' } }, + { overallSummaryCandidate: { ...overview, evidenceIds: ['missing'] } }, + { evidence: [] }, + ]) + expect(() => + parseAnalysis(JSON.stringify({ ...result, ...patch }), facts, presentation) + ).toThrow(); + }); + + it.each(['missing', 'copy', 'null', 'legacy'])( + '全体要約の欠損・不正だけで個別論点を失わない: %s', + (kind) => { + const raw = { + version: 4, + issues: [issue()], + ...(kind === 'legacy' + ? {} + : { + overallSummary: + kind === 'null' + ? null + : { + text: kind === 'copy' ? issue().conclusion : '全体像の文章', + evidenceIds: kind === 'missing' ? ['missing'] : issue().evidenceIds, + }, + }), + }; + const result = parseAnalysisResponse(JSON.stringify(raw), input()); + expect(result.issues).toEqual([issue()]); + expect(result.overallSummary ?? null).toBeNull(); + if (kind === 'missing' || kind === 'copy') + expect(result.notices).toContainEqual( + expect.objectContaining({ + path: expect.stringContaining('$.overallSummary'), + severity: 'quarantined', + }) + ); + expect(parseAnalysis(JSON.stringify(result), facts, presentation)).toEqual(result); + } + ); + it('確認済み事実だけでなく点検済み説明・指標と原文対応を渡し、未点検を除く', () => { const display = structuredClone(presentation); const value = display.values.find((v) => v.id === facts.facts[0].id)!; @@ -528,7 +589,7 @@ describe('生成契約と診断の正負境界', () => { expect(schema).toMatchObject({ type: 'object', additionalProperties: false, - required: ['version', 'issues'], + required: ['version', 'overallSummary', 'issues'], properties: { version: { enum: [4] }, issues: { @@ -551,6 +612,9 @@ describe('生成契約と診断の正負境界', () => { }, }); const messages = analysisPrompt(source); + expect(messages[0].content).toContain('個別論点を横断した全体像'); + expect(messages[0].content).toContain('最初の論点・結論のコピー'); + expect(messages[0].content).toContain('個別論点と同じ一回の応答'); expect(messages[0].content).toContain('titleは80文字以内'); expect(messages[0].content).toContain('各350文字以内'); expect(messages[0].content).toContain('evidence[].idとraw:<行ID>・rawspan:<文字列ID>'); diff --git a/src/lib/additional-analysis.ts b/src/lib/additional-analysis.ts index 3282c01..a1519cb 100644 --- a/src/lib/additional-analysis.ts +++ b/src/lib/additional-analysis.ts @@ -23,6 +23,10 @@ export interface AnalysisIssue { caveat: string; nextCheck: string; } +export interface AnalysisOverallSummary { + text: string; + evidenceIds: string[]; +} export interface AnalysisNotice { issueIndex: number; code: string; @@ -36,6 +40,8 @@ export interface AdditionalAnalysis { issues: AnalysisIssue[]; candidates: unknown[]; rootExtras?: Record; + overallSummary?: AnalysisOverallSummary | null; + overallSummaryCandidate?: unknown; notices: AnalysisNotice[]; evidence: AnalysisEvidence[]; coverage: AnalysisCoverage; @@ -255,8 +261,73 @@ function withRootExtrasNotice( }); return parsed; } -const selectedEvidence = (issues: AnalysisIssue[], input: AnalysisInput) => { - const ids = new Set(issues.flatMap((i) => i.evidenceIds)); +/** Independently validate the overview against source-backed input. A malformed + * issue can never provide evidence by itself. Valid linkage does not verify prose. */ +function withOverallSummary>( + parsed: T, + candidate: unknown, + input: AnalysisInput +): T & { overallSummary: AnalysisOverallSummary | null; overallSummaryCandidate: unknown } { + let overallSummary: AnalysisOverallSummary | null = null; + try { + if (candidate !== null) { + if (!record(candidate) || !exact(candidate, ['text', 'evidenceIds'])) + throw failure('overview_shape', '$.overallSummary', '全体要約の形式が不正です'); + if (typeof candidate.text !== 'string' || !candidate.text.trim()) + throw failure('overview_text', '$.overallSummary.text', '全体要約の文章が空または不正です'); + if ([...candidate.text].length > ANALYSIS_RESOURCE_LIMITS.text) + throw failure( + 'text_budget', + '$.overallSummary.text', + '全体要約が安全に表示できる長さを超えています' + ); + const ids = candidate.evidenceIds; + if ( + !Array.isArray(ids) || + !ids.length || + ids.length > ANALYSIS_RESOURCE_LIMITS.references || + ids.some((id) => typeof id !== 'string' || !input.evidence.some((e) => e.id === id)) + ) + throw failure( + 'overview_evidence', + '$.overallSummary.evidenceIds', + '全体要約の根拠IDが入力と一致しません' + ); + if ( + parsed.issues.some((issue) => + [issue.title, issue.conclusion, issue.reading].some( + (text) => normalize(text) === normalize(candidate.text as string) + ) + ) + ) + throw failure( + 'overview_restatement', + '$.overallSummary.text', + '全体要約が個別論点のコピーのため表示していません' + ); + overallSummary = { text: candidate.text, evidenceIds: [...new Set(ids)] as string[] }; + } + } catch (error) { + if (!(error instanceof AnalysisValidationError)) throw error; + parsed.notices.push({ + issueIndex: -1, + code: error.code, + path: error.path, + message: error.detail, + severity: 'quarantined', + }); + } + return { ...parsed, overallSummary, overallSummaryCandidate: candidate }; +} +const selectedEvidence = ( + issues: AnalysisIssue[], + input: AnalysisInput, + overallSummary?: AnalysisOverallSummary | null +) => { + const ids = new Set([ + ...issues.flatMap((i) => i.evidenceIds), + ...(overallSummary?.evidenceIds ?? []), + ]); return input.evidence.filter((e) => ids.has(e.id)); }; export function parseAnalysisResponse( @@ -271,19 +342,27 @@ export function parseAnalysisResponse( throw failure('response_version', '$.version', '応答の形式バージョンが一致しません'); // Retain ignored envelope fields without interpreting them as prose or evidence. const extras = Object.fromEntries( - Object.entries(value).filter(([key]) => key !== 'version' && key !== 'issues') + Object.entries(value).filter( + ([key]) => key !== 'version' && key !== 'issues' && key !== 'overallSummary' + ) ); const rootExtras = Object.keys(extras).length ? extras : undefined; const parsed = withRootExtrasNotice( withGenerationNotice(parseIssues(value.issues, input), usage), rootExtras ); + const projected: ReturnType & { + overallSummary?: AnalysisOverallSummary | null; + overallSummaryCandidate?: unknown; + } = Object.prototype.hasOwnProperty.call(value, 'overallSummary') + ? withOverallSummary(parsed, value.overallSummary, input) + : parsed; return { version: ANALYSIS_VERSION, inputHash: input.inputHash, - ...parsed, + ...projected, ...(rootExtras ? { rootExtras } : {}), - evidence: selectedEvidence(parsed.issues, input), + evidence: selectedEvidence(parsed.issues, input, projected.overallSummary), coverage: input.coverage, usage, }; @@ -310,6 +389,9 @@ export function parseAnalysis( 'coverage', 'usage', ...(Object.prototype.hasOwnProperty.call(value, 'rootExtras') ? ['rootExtras'] : []), + ...(Object.prototype.hasOwnProperty.call(value, 'overallSummaryCandidate') + ? ['overallSummaryCandidate', 'overallSummary'] + : []), ]) || value.version !== ANALYSIS_VERSION || value.inputHash !== input.inputHash @@ -341,10 +423,19 @@ export function parseAnalysis( withGenerationNotice(parseIssues(value.candidates, input), value.usage as Usage | null), rootExtras as Record | undefined ); + const projected: ReturnType & { + overallSummary?: AnalysisOverallSummary | null; + overallSummaryCandidate?: unknown; + } = Object.prototype.hasOwnProperty.call(value, 'overallSummaryCandidate') + ? withOverallSummary(parsed, value.overallSummaryCandidate, input) + : parsed; if ( + ('overallSummary' in projected && + canonicalJSON(value.overallSummary) !== canonicalJSON(projected.overallSummary)) || canonicalJSON(value.issues) !== canonicalJSON(parsed.issues) || canonicalJSON(value.notices) !== canonicalJSON(parsed.notices) || - canonicalJSON(value.evidence) !== canonicalJSON(selectedEvidence(parsed.issues, input)) || + canonicalJSON(value.evidence) !== + canonicalJSON(selectedEvidence(parsed.issues, input, projected.overallSummary)) || canonicalJSON(value.coverage) !== canonicalJSON(input.coverage) ) throw failure('saved_evidence', '$', '保存結果の根拠または入力範囲が一致しません'); @@ -369,9 +460,28 @@ export function analysisResponseSchema(input: AnalysisInput): Record e.id) }, + }, + }, + }, + ], + }, issues: { type: 'array', maxItems: ANALYSIS_RESOURCE_LIMITS.issues, @@ -412,11 +522,11 @@ export function analysisPrompt(input: AnalysisInput) { factは照合済み事実、explanationとobservationは原文との独立点検を通った会社説明・指標、calculationはコードによる機械計算です。sourceおよびsourceDocumentは抽出原文であり、数値の見た目が取得できても期間・行列対応・因果関係の意味が検証済みとは限りません。原文の見出し・列・注記と照合し、読み取れない対応を補いません。会社説明は「会社は〜と説明」、独自計算は計算式・対象期間・単位、条件付き解釈は「〜なら」と分かる文章にします。入力の機械計算を優先し、自分の計算を検算済みと称しません。単なる比較計算と、原因・継続性の仮説を同じものとして扱いません。市場予想・株価反応・売買推奨・上方修正の確実性を捏造しません。 caveatとnextCheckは、その論点の判断を実際に変える限界・観測がある場合だけ書き、なければ空文字にします。未確認・不足と書く前に引用した根拠だけでなくsourceDocumentを含む入力全体を確認し、既にある金額・進捗・説明を未確認としないでください。資料内で確認できる事項を次回開示待ちにしません。抽出や要約の不足、原文中の意味対応が未点検であること、会社が開示していないことを区別します。抽出原文があっても画像・図・抽出失敗を含むPDF全体の完全な網羅は保証されないため、見つからないだけで「資料に記載がない」と断定しません。coverageは入力範囲の情報であり論点の根拠ではありません。未知事項はそれが何の判断を変えるのかまで述べ、一般的な「次回確認する」だけで埋めません。 本文はAIが生成する文章です。根拠IDの一致は引用先の存在を示すだけで、数値・因果・不在の主張の意味の検証になりません。根拠欄はコードが入力から表示するため、本文を確認済み事実へ昇格させません。各論点の主張を支える根拠IDを選び、比較の片側だけを引用しないでください。 -出力の契約: versionは${ANALYSIS_VERSION}、最上位はversionとissuesだけです。issuesは通常${ANALYSIS_LIMITS.issues}件以内を目安とし、各項目はtitle/conclusion/evidenceIds/reading/caveat/nextCheckをすべて持ちます。titleは${ANALYSIS_LIMITS.title}文字以内、conclusion/reading/caveat/nextCheckは各${ANALYSIS_LIMITS.text}文字以内を目安とします。title/conclusion/readingは空文字・空白だけ・「判断不能」だけを避けます。caveat/nextCheckは不要なら空文字です。evidenceIdsは入力のallowedEvidenceIds(evidence[].idとraw:<行ID>・rawspan:<文字列ID>・rawitem:<原文字ID>)から完全一致で1〜${ANALYSIS_LIMITS.references}件を目安に選び、同じ配列内で重複させません。IDを省略・作成・変更せず、PDFセル・段落IDを代用しません。論点間で同じtitleや同一の根拠ID集合を繰り返しません。`, +出力の契約: versionは${ANALYSIS_VERSION}、最上位はversion/overallSummary/issuesだけです。overallSummaryは{text,evidenceIds}で、個別論点を横断した全体像を先に二〜三文で述べます。何が変わり、強さと弱さを総合するとどう読めるか、見方を左右する主要条件を入力根拠から統合してください。最初の論点・結論のコピーや見出しの羅列にせず、個別論点と同じ一回の応答で作成します。evidenceIdsは全体要約の主張を直接支えるallowedEvidenceIdsのみを選び、比較の両側を参照します。全体像を支える入力がない場合はnullにし、推測で埋めません。全体要約も未検証のAI推論であり、根拠参照だけで内容が検証済みとはしません。issuesは通常${ANALYSIS_LIMITS.issues}件以内を目安とし、各項目はtitle/conclusion/evidenceIds/reading/caveat/nextCheckをすべて持ちます。titleは${ANALYSIS_LIMITS.title}文字以内、conclusion/reading/caveat/nextCheckは各${ANALYSIS_LIMITS.text}文字以内を目安とします。title/conclusion/readingは空文字・空白だけ・「判断不能」だけを避けます。caveat/nextCheckは不要なら空文字です。evidenceIdsは入力のallowedEvidenceIds(evidence[].idとraw:<行ID>・rawspan:<文字列ID>・rawitem:<原文字ID>)から完全一致で1〜${ANALYSIS_LIMITS.references}件を目安に選び、同じ配列内で重複させません。IDを省略・作成・変更せず、PDFセル・段落IDを代用しません。論点間で同じtitleや同一の根拠ID集合を繰り返しません。`, }, { role: 'user' as const, - content: `形式: {"version":${ANALYSIS_VERSION},"issues":[{"title":"論点名","conclusion":"何が重要かの条件付き結論","evidenceIds":["allowedEvidenceIdsから選んだID"],"reading":"根拠をつないだ条件付きの読み","caveat":"具体的な限界・反対の可能性","nextCheck":"次の資料で確認する具体的な条件"}]}\n入力: ${JSON.stringify(analysisModelInput(input))}`, + content: `形式: {"version":${ANALYSIS_VERSION},"overallSummary":{"text":"根拠を横断して統合した全体像と主要な条件","evidenceIds":["allowedEvidenceIdsから選んだID"]},"issues":[{"title":"論点名","conclusion":"何が重要かの条件付き結論","evidenceIds":["allowedEvidenceIdsから選んだID"],"reading":"根拠をつないだ条件付きの読み","caveat":"具体的な限界・反対の可能性","nextCheck":"次の資料で確認する具体的な条件"}]}\n入力: ${JSON.stringify(analysisModelInput(input))}`, }, ]; } diff --git a/src/lib/analysis-trace.test.ts b/src/lib/analysis-trace.test.ts index cad5e91..0de0529 100644 --- a/src/lib/analysis-trace.test.ts +++ b/src/lib/analysis-trace.test.ts @@ -3,6 +3,7 @@ import { ANALYSIS_DIAGNOSTICS_KEY, ANALYSIS_DIAGNOSTICS_LIMITS, loadAnalysisTrace, + requestAnalysisTrace, readLastAnalysisAttempt, saveAnalysisTrace, readAnalysisDiagnosticReference, @@ -371,7 +372,15 @@ describe('追加分析の実行別診断', () => { await expect(saveAnalysisTrace(item)).rejects.toThrow('quota'); await expect( loadAnalysisTrace(pdfUrl, item.runId, item.summaryResultId, 'failed') - ).rejects.toThrow('途中の記録'); + ).rejects.toThrow('未保存の最終診断'); + expect( + await loadAnalysisTrace(pdfUrl, item.runId, item.summaryResultId, 'failed', item.inputHash!) + ).toMatchObject({ + outcome: 'success', + response: item.response, + volatile: true, + persistenceWarning: expect.stringContaining('未保存'), + }); expect(await loadAnalysisTrace(pdfUrl, item.runId, item.summaryResultId)).toMatchObject({ outcome: 'running', }); @@ -380,6 +389,114 @@ describe('追加分析の実行別診断', () => { expect(await loadAnalysisTrace(pdfUrl, next.runId, next.summaryResultId)).toEqual(next); }); + // New boundaries: failed final save is exportable only in this worker and for + // the exact requested identity; retention has the same count/byte limits as disk. + it('未保存の最終記録は呼出時に固定し、別実行・結果・入力・PDFへ流用しない', async () => { + const item = trace('volatile-identity'); + vi.mocked(chrome.storage.local.set).mockRejectedValue(new Error('quota')); + const pending = saveAnalysisTrace(item); + item.response = 'changed after save'; + await expect(pending).rejects.toThrow('quota'); + const load = ( + runId = item.runId, + result = item.summaryResultId, + input = item.inputHash!, + pdf = pdfUrl + ) => loadAnalysisTrace(pdf, runId, result, 'failed', input); + const saved = await load(); + expect(saved.response).not.toBe(item.response); + saved.input!.evidence[0].text = 'changed after export'; + expect((await load()).input!.evidence[0].text).toBe('fixture'); + for (const args of [ + ['other'], + [item.runId, 'other'], + [item.runId, item.summaryResultId, 'other'], + [item.runId, item.summaryResultId, item.inputHash!, 'other.pdf'], + ]) { + await expect(load(...args)).rejects.toThrow('未保存の最終診断'); + } + vi.resetModules(); + const restarted = await import('./analysis-trace'); + await expect( + restarted.loadAnalysisTrace( + pdfUrl, + item.runId, + item.summaryResultId, + 'failed', + item.inputHash! + ) + ).rejects.toThrow('再起動'); + }); + + it.each(['count', 'bytes'] as const)( + '未保存記録の%s上限では古い記録を復活させない', + async (boundary) => { + vi.mocked(chrome.storage.local.set).mockRejectedValue(new Error('quota')); + const count = boundary === 'count' ? 13 : 12; + const items = Array.from({ length: count }, (_, i) => ({ + ...trace(`volatile-${boundary}-${i}`), + response: boundary === 'bytes' ? 'あ'.repeat(40_000) : '{}', + })); + for (const item of items) await expect(saveAnalysisTrace(item)).rejects.toThrow('quota'); + const load = (item: AnalysisTrace) => + loadAnalysisTrace(pdfUrl, item.runId, item.summaryResultId, 'failed', item.inputHash!); + await expect(load(items[0])).rejects.toThrow('保持上限'); + expect(await load(items.at(-1)!)).toMatchObject({ volatile: true }); + } + ); + + it.each([ + { stage: 'write', code: 'quota', message: 'QUOTA_BYTES secret-api-key' }, + { stage: 'read', code: 'storage-error', message: 'read failed private-endpoint' }, + ] as const)( + '未保存診断は生成結果と別に安全な保存理由を保持する: $stage/$code', + async ({ stage, code, message }) => { + const item = trace(`safe-storage-error-${stage}`); + if (stage === 'write') + vi.mocked(chrome.storage.local.set).mockRejectedValueOnce(new Error(message)); + else vi.mocked(chrome.storage.local.get).mockRejectedValueOnce(new Error(message)); + await expect(saveAnalysisTrace(item)).rejects.toThrow(message); + const exported = await loadAnalysisTrace( + pdfUrl, + item.runId, + item.summaryResultId, + 'failed', + item.inputHash! + ); + expect(exported).toMatchObject({ + outcome: 'success', + error: null, + persistenceFailure: { code, stage }, + }); + expect(JSON.stringify(exported)).not.toMatch(/secret-api-key|private-endpoint/); + } + ); + + it('content側は未保存診断だけをworkerへ要求し、異なる応答や途中記録を拒否する', async () => { + const item = { + ...trace('bridge'), + persistenceFailure: { code: 'quota' as const, stage: 'write' as const }, + }; + const sendMessage = vi.fn().mockResolvedValue({ trace: { ...item, volatile: true } }); + Object.assign(chrome, { runtime: { sendMessage } }); + const load = () => + requestAnalysisTrace(pdfUrl, item.runId, item.summaryResultId, 'failed', item.inputHash!); + expect(await load()).toMatchObject({ runId: item.runId, volatile: true }); + expect(sendMessage).toHaveBeenCalledWith({ + action: 'getAnalysisDiagnostic', + pdfUrl, + runId: item.runId, + summaryResultId: item.summaryResultId, + inputHash: item.inputHash, + }); + for (const change of [{ runId: 'other' }, { outcome: 'running' }, { volatile: undefined }]) { + sendMessage.mockResolvedValueOnce({ trace: { ...item, volatile: true, ...change } }); + await expect(load()).rejects.toThrow('対応する診断'); + } + sendMessage.mockResolvedValueOnce({ error: '再起動後は利用できません' }); + await expect(load()).rejects.toThrow('再起動後'); + }); + it('直近の試行は応答完了順でなく開始順に結び、別結果・別入力へ混ぜない', async () => { const older = { ...trace('older-intent'), intentAt: 1, outcome: 'running' as const }; const newer = { diff --git a/src/lib/analysis-trace.ts b/src/lib/analysis-trace.ts index a6fe330..ed92ebd 100644 --- a/src/lib/analysis-trace.ts +++ b/src/lib/analysis-trace.ts @@ -393,49 +393,102 @@ function boundedTrace(trace: AnalysisTrace): AnalysisTrace { } throw new Error('追加分析診断の識別情報が保存上限を超えています'); } +/** Terminal snapshots exist only in this service-worker instance. No storage/session + * permission or persistent recovery is implied; eviction/restart makes them unavailable. */ +interface AnalysisPersistenceFailure { + code: 'quota' | 'storage-error'; + stage: 'read' | 'prepare' | 'write'; +} +const failedTerminalTraces = new Map< + string, + { + trace: AnalysisTrace; + persistenceFailure: AnalysisPersistenceFailure; + } +>(); +export interface AnalysisDiagnosticExport extends AnalysisTrace { + volatile?: true; + persistenceFailure?: AnalysisPersistenceFailure; + persistenceWarning?: string; +} +const volatileWarning = + '未保存の最終診断です。この一時記録は現在のサービスワーカーが終了するか、保持上限を超えると失われます'; +function retainFailedTerminal( + copy: AnalysisTrace, + persistenceFailure: AnalysisPersistenceFailure +): void { + failedTerminalTraces.delete(copy.runId); + if (copy.outcome === 'running' || !copy.inputHash) return; + failedTerminalTraces.set(copy.runId, { trace: copy, persistenceFailure }); + while ( + failedTerminalTraces.size > ANALYSIS_DIAGNOSTICS_LIMITS.records || + bytes([...failedTerminalTraces.values()]) > ANALYSIS_DIAGNOSTICS_LIMITS.bytes + ) { + failedTerminalTraces.delete(failedTerminalTraces.keys().next().value!); + } +} let writeQueue: Promise = Promise.resolve(); export function saveAnalysisTrace(trace: AnalysisTrace): Promise { // Snapshot when invoked, before queued work observes a later mutation. const copy = boundedTrace(trace); - const write = writeQueue.then(async () => { - const saved = await chrome.storage.local.get(ANALYSIS_DIAGNOSTICS_KEY); - const store = readStore(saved[ANALYSIS_DIAGNOSTICS_KEY]); - store.traces = store.traces.filter((item) => item.runId !== copy.runId); - store.traces.push(copy); - if (copy.inputHash) { - const prior = store.lastAttempts.find( - (item) => item.summaryResultId === copy.summaryResultId && item.inputHash === copy.inputHash - ); - if (!prior || copy.intentAt > prior.intentAt || prior.runId === copy.runId) { - store.lastAttempts = store.lastAttempts.filter((item) => item !== prior); - store.lastAttempts.push({ - version: 1, - runId: copy.runId, - summaryResultId: copy.summaryResultId, - inputHash: copy.inputHash, - pdfUrl: copy.pdfUrl, - intentAt: copy.intentAt, - persistence: 'saved', - outcome: copy.outcome, - error: copy.error, + let stage: AnalysisPersistenceFailure['stage'] = 'read'; + const write = writeQueue + .then(async () => { + const saved = await chrome.storage.local.get(ANALYSIS_DIAGNOSTICS_KEY); + stage = 'prepare'; + const store = readStore(saved[ANALYSIS_DIAGNOSTICS_KEY]); + store.traces = store.traces.filter((item) => item.runId !== copy.runId); + store.traces.push(copy); + if (copy.inputHash) { + const prior = store.lastAttempts.find( + (item) => + item.summaryResultId === copy.summaryResultId && item.inputHash === copy.inputHash + ); + if (!prior || copy.intentAt > prior.intentAt || prior.runId === copy.runId) { + store.lastAttempts = store.lastAttempts.filter((item) => item !== prior); + store.lastAttempts.push({ + version: 1, + runId: copy.runId, + summaryResultId: copy.summaryResultId, + inputHash: copy.inputHash, + pdfUrl: copy.pdfUrl, + intentAt: copy.intentAt, + persistence: 'saved', + outcome: copy.outcome, + error: copy.error, + }); + store.lastAttempts.sort((a, b) => a.intentAt - b.intentAt); + } + } + store.lastAttempts = store.lastAttempts.slice(-ANALYSIS_DIAGNOSTICS_LIMITS.records); + while ( + store.traces.length > ANALYSIS_DIAGNOSTICS_LIMITS.records || + bytes(store) > ANALYSIS_DIAGNOSTICS_LIMITS.bytes + ) { + const completed = store.traces.findIndex( + (item) => item.runId !== copy.runId && item.outcome !== 'running' + ); + if (store.traces.length > 1) store.traces.splice(completed < 0 ? 0 : completed, 1); + else if (store.lastAttempts.length > 1) store.lastAttempts.shift(); + else throw new Error('追加分析診断の識別情報が保存上限を超えています'); + } + stage = 'write'; + await chrome.storage.local.set({ [ANALYSIS_DIAGNOSTICS_KEY]: store }); + }) + .then( + () => { + failedTerminalTraces.delete(copy.runId); + }, + (error: unknown) => { + // Classify only; raw storage errors can contain private endpoint/config data. + const message = error instanceof Error ? error.message : String(error); + retainFailedTerminal(copy, { + code: /quota|QUOTA_BYTES|容量|上限/i.test(message) ? 'quota' : 'storage-error', + stage, }); - store.lastAttempts.sort((a, b) => a.intentAt - b.intentAt); + throw error; } - } - store.lastAttempts = store.lastAttempts.slice(-ANALYSIS_DIAGNOSTICS_LIMITS.records); - while ( - store.traces.length > ANALYSIS_DIAGNOSTICS_LIMITS.records || - bytes(store) > ANALYSIS_DIAGNOSTICS_LIMITS.bytes - ) { - const completed = store.traces.findIndex( - (item) => item.runId !== copy.runId && item.outcome !== 'running' - ); - if (store.traces.length > 1) store.traces.splice(completed < 0 ? 0 : completed, 1); - else if (store.lastAttempts.length > 1) store.lastAttempts.shift(); - else throw new Error('追加分析診断の識別情報が保存上限を超えています'); - } - await chrome.storage.local.set({ [ANALYSIS_DIAGNOSTICS_KEY]: store }); - }); + ); writeQueue = write.then( () => {}, () => {} @@ -448,7 +501,28 @@ export async function loadAnalysisTrace( summaryResultId: string, persistence?: DiagnosticPersistence, expectedInputHash?: string -): Promise { +): Promise { + if (persistence === 'failed') { + const retained = failedTerminalTraces.get(runId); + const terminal = retained?.trace; + if ( + terminal && + expectedInputHash !== undefined && + terminal.inputHash === expectedInputHash && + terminal.summaryResultId === summaryResultId && + terminal.pdfUrl === normalizeTdnetPdfUrl(pdfUrl) + ) + return { + ...snapshot(terminal), + volatile: true, + persistenceWarning: volatileWarning, + persistenceFailure: { ...retained!.persistenceFailure }, + }; + // Never pass off an older running record as the missing final result. + throw new Error( + '追加分析の未保存の最終診断は利用できません。一時記録の保持上限またはサービスワーカーの再起動により失われた可能性があります' + ); + } const saved = await chrome.storage.local.get(ANALYSIS_DIAGNOSTICS_KEY); const trace = readStore(saved[ANALYSIS_DIAGNOSTICS_KEY]).traces.find( (item) => item.runId === runId @@ -459,17 +533,55 @@ export async function loadAnalysisTrace( trace.pdfUrl !== normalizeTdnetPdfUrl(pdfUrl) || (expectedInputHash !== undefined && trace.inputHash !== expectedInputHash) ) - throw new Error( - persistence === 'failed' ? 'この追加分析の診断を保存できませんでした' : unavailable - ); - // A saved running snapshot is useful after interruption, but must not pretend - // to be the missing terminal outcome following a failed final write. - if (persistence === 'failed') - throw new Error( - '追加分析の最終診断を保存できませんでした。途中の記録だけが残っている可能性があります' - ); + throw new Error(unavailable); return snapshot(trace); } +/** Content scripts have separate module memory; ask the worker for failed writes. + * Callers must still discard replies after their current UI/request epoch changes. */ +export async function requestAnalysisTrace( + pdfUrl: string, + runId: string, + summaryResultId: string, + persistence?: DiagnosticPersistence, + expectedInputHash?: string +): Promise { + if (persistence !== 'failed') + return loadAnalysisTrace(pdfUrl, runId, summaryResultId, persistence, expectedInputHash); + const response: unknown = await chrome.runtime.sendMessage({ + action: 'getAnalysisDiagnostic', + pdfUrl, + runId, + summaryResultId, + inputHash: expectedInputHash, + }); + if (record(response) && typeof response.error === 'string') throw new Error(response.error); + const trace = record(response) ? response.trace : undefined; + if ( + !isTrace(trace) || + trace.runId !== runId || + trace.summaryResultId !== summaryResultId || + !expectedInputHash || + trace.inputHash !== expectedInputHash || + trace.pdfUrl !== normalizeTdnetPdfUrl(pdfUrl) || + trace.outcome === 'running' || + !record(trace) || + trace.volatile !== true || + !record(trace.persistenceFailure) || + !exact(trace.persistenceFailure, ['code', 'stage']) || + !['quota', 'storage-error'].includes(String(trace.persistenceFailure.code)) || + !['read', 'prepare', 'write'].includes(String(trace.persistenceFailure.stage)) + ) + throw new Error(unavailable); + return { + ...snapshot(trace), + volatile: true, + persistenceWarning: volatileWarning, + persistenceFailure: { + code: trace.persistenceFailure.code as AnalysisPersistenceFailure['code'], + stage: trace.persistenceFailure.stage as AnalysisPersistenceFailure['stage'], + }, + }; +} export function readAnalysisDiagnosticReference( value: unknown, summaryResultId: string, diff --git a/src/lib/analysis-version.ts b/src/lib/analysis-version.ts index 71962ed..772bc34 100644 --- a/src/lib/analysis-version.ts +++ b/src/lib/analysis-version.ts @@ -1,7 +1,7 @@ import { effectiveApiUrl } from './llm-endpoint'; import type { ExtractionMode } from '@/types/summaryMetadata'; -export const ANALYSIS_SCHEMA_VERSION = 123; +export const ANALYSIS_SCHEMA_VERSION = 124; export interface AnalysisFingerprintSettings { provider: string; diff --git a/src/lib/fact-candidates.test.ts b/src/lib/fact-candidates.test.ts index e27893e..c9b8183 100644 --- a/src/lib/fact-candidates.test.ts +++ b/src/lib/fact-candidates.test.ts @@ -672,7 +672,7 @@ describe('生成専用候補と原文文脈の契約', () => { ).toMatchObject({ kind: 'event', state: 'forecast', periodKind: 'none' }); }); // These repair flows replay the complete PDF fixture through several validation passes. - it('当年だけの応答を受理せず、原文の前年値だけを修復して保存する', async () => { + it('原文で前年金額を補完し、未補完のEPSだけを修復して保存する', async () => { const r = review(candidates); expect(r.unverified).toEqual([]); expect(r.facts).toHaveLength(fixture.length); @@ -723,12 +723,34 @@ describe('生成専用候補と原文文脈の契約', () => { vi.mocked(generateText) .mockReset() .mockResolvedValueOnce(raw(candidates.slice(0, 14))) - .mockResolvedValueOnce(raw(candidates.slice(14))); + .mockResolvedValueOnce( + raw(candidates.slice(14).filter((c) => c.meaning.metricKind === 'perShare')) + ); const repaired = await generateVerifiedFactSummary(config, 'earnings', 'source', pages); expect(repaired.repairAttempted).toBe(true); - expect(repaired.facts.facts).toEqual(r.facts); + // Exact source facts remain identical; deterministic prior amounts inherit + // the selected current metric's importance instead of model priority. + expect([...repaired.facts.facts].sort((a, b) => a.id.localeCompare(b.id))).toEqual( + r.facts + .map((fact) => + fact.period === '2025年3月期' && fact.semantics.metricKind === 'amount' + ? { ...fact, importance: 'key' } + : fact + ) + .sort((a, b) => a.id.localeCompare(b.id)) + ); + expect(parseFactSummary(JSON.stringify(repaired.facts), 'earnings', pages)).toEqual( + repaired.facts + ); expect(vi.mocked(generateText)).toHaveBeenCalledTimes(2); - expect(vi.mocked(generateText).mock.calls[1][1][1].content).toContain('修復方式=delta'); + const repairInput = vi.mocked(generateText).mock.calls[1][1][1].content; + expect(repairInput).toContain('修復方式=delta'); + const missing = JSON.parse( + repairInput.split('不足slotの型と原文: ')[1].split('\n独立した診断:')[0] + ); + expect(missing).toHaveLength(1); + expect(missing[0].expected.metricKind).toBe('perShare'); + expect(missing[0].expected.period).toBe('2025年3月期'); }, 30_000); it('通期の前年値を四半期の前年比較へ転用しない', () => { const items = structuredClone(corpus[0].pages[0].items); diff --git a/src/lib/fact-summary.ts b/src/lib/fact-summary.ts index 897a24a..30a7697 100644 --- a/src/lib/fact-summary.ts +++ b/src/lib/fact-summary.ts @@ -1,3 +1,4 @@ +import { acquireSourceComparisons } from './source-comparison'; import { unchangedDividend, unchangedDividendReference } from './dividend-semantics'; import type { DocumentType } from './document-type'; import type { ExtractedPage } from '@/types/summaryMetadata'; @@ -208,7 +209,12 @@ export async function generateVerifiedFacts( { role: 'system', content: prompt.system }, { role: 'user', content: prompt.user }, ]); - const first = reviewCandidates(raw, documentType, pages, context); + const first = acquireSourceComparisons( + reviewCandidates(raw, documentType, pages, context), + documentType, + pages, + context + ); const pendingSlots = (slots: CoverageSlot[]) => slots.filter((s) => s.status !== 'satisfied' && s.status !== 'outsideSelection'); const firstSlots = coverageReport(documentType, pages, first.facts, first.diagnostics, context); @@ -289,7 +295,12 @@ export async function generateVerifiedFacts( true ); } - const repaired = reviewCandidates(revised, documentType, repairPages, context); + const repaired = acquireSourceComparisons( + reviewCandidates(revised, documentType, repairPages, context), + documentType, + repairPages, + context + ); let failure: string | null = null; if (repaired.envelopeValid) for (const candidate of JSON.parse(revised).candidates) { diff --git a/src/lib/source-comparison.test.ts b/src/lib/source-comparison.test.ts new file mode 100644 index 0000000..5026231 --- /dev/null +++ b/src/lib/source-comparison.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, it } from 'vitest'; +import { cells, tableAmount } from './fixtures/fact-review-source'; +import { candidateResponse } from './fixtures/candidate-test-source'; +import { buildDocumentContext } from './document-context'; +import { reviewCandidates } from './fact-candidates'; +import { acquireSourceComparisons } from './source-comparison'; + +function fixture(metric = '独自利益', previous = '2025年3月期') { + const pages = [ + cells( + [ + ['2026年3月期 決算短信〔IFRS〕(連結)', 0, 0, 350], + ['会社名 株式会社テスト', 0, 25, 220], + ['1. 連結経営成績', 0, 50, 180], + [metric, 230, 80, 100], + ['営業利益', 365, 80, 90], + ['百万円', 230, 100, 50], + ['百万円', 365, 100, 50], + ['2026年3月期', 0, 125, 170], + ['120', 230, 125, 35], + ['70', 365, 125, 35], + [previous, 0, 150, 190], + ['100', 230, 150, 35], + ['80', 365, 150, 35], + ], + 1 + ), + ]; + const context = buildDocumentContext(pages); + const mapping = context.tableMappings.find( + (m) => pages[0].quantities.find((q) => q.id === m.valueId)?.text === '120' + )!; + const fact = tableAmount(pages, mapping, { + period: '2026年3月期', + subject: '株式会社テスト', + scope: '連結', + basis: 'IFRS', + state: 'actual', + }); + const review = reviewCandidates( + candidateResponse([fact], pages, 'earnings'), + 'earnings', + pages, + context + ); + expect(review.facts).toHaveLength(1); + return { pages, context, review }; +} + +describe('source-axis comparison acquisition', () => { + it.each(['独自利益', '税引前利益', '経常利益', '調整後税引前利益'])( + 'uses original metric-axis identity rather than a financial-name whitelist: %s', + (label) => { + const { pages, context, review } = fixture(label); + const result = acquireSourceComparisons(review, 'earnings', pages, context); + expect(result.facts.map((f) => [f.label, f.quantity?.decimal])).toEqual([ + [label, '120'], + [label, '100'], + ]); + expect(review.facts).toHaveLength(1); + } + ); + + it.each(['2024年3月期', '2025年3月期(予想)', '2025年3月期第1四半期'])( + 'does not borrow another year, state or period shape: %s', + (period) => { + const { pages, context, review } = fixture('独自利益', period); + expect(acquireSourceComparisons(review, 'earnings', pages, context).facts).toHaveLength(1); + } + ); + + it('does not override rejected source candidates or supplement non-earnings documents', () => { + const { pages, context, review } = fixture(); + const previous = context.tableMappings.find( + (m) => pages[0].quantities.find((q) => q.id === m.valueId)?.text === '100' + )!; + review.candidateSources.set('c2', { + kind: 'table', + valueId: previous.valueId, + tableId: review.facts[0].provenance!.tableId!, + contextBindingId: `ctx:${previous.valueId}`, + }); + expect(acquireSourceComparisons(review, 'earnings', pages, context).facts).toHaveLength(1); + expect(acquireSourceComparisons(review, 'other', pages, context)).toBe(review); + }); + + it.each(['subject', 'scope', 'basis', 'qualifiers', 'conditions', 'polarity'] as const)( + 'keeps comparison safeguards when the prior source cannot prove the current %s', + (role) => { + const { pages, context, review } = fixture(); + Object.assign(review.facts[0].semantics, { + [role]: + role === 'qualifiers' || role === 'conditions' + ? ['異なる条件'] + : role === 'polarity' + ? 'negative' + : '異なる属性', + }); + expect(acquireSourceComparisons(review, 'earnings', pages, context).facts).toHaveLength(1); + } + ); + + it('does not join different metric axes even if their text is identical', () => { + const { pages, context, review } = fixture(); + const current = review.facts[0]; + if (current.evidence.kind !== 'table') throw new Error('expected table'); + current.evidence.metricIds = ['different-axis']; + expect(acquireSourceComparisons(review, 'earnings', pages, context).facts).toHaveLength(1); + }); +}); diff --git a/src/lib/source-comparison.ts b/src/lib/source-comparison.ts new file mode 100644 index 0000000..aa1ab5a --- /dev/null +++ b/src/lib/source-comparison.ts @@ -0,0 +1,126 @@ +import type { ExtractedPage } from '@/types/summaryMetadata'; +import type { DocumentType } from './document-type'; +import type { DocumentContext } from './document-context'; +import { canonicalJSON } from './fact-contract'; +import { + reviewCandidates, + CANDIDATE_VERSION, + type Candidate, + type CandidateReview, +} from './fact-candidates'; +import { sourceDeclaredTables } from './source-declared-tables'; +import { reportingPeriodText } from './period-semantics'; +import { summaryComparison } from './summary-comparison'; + +/** Complete a selected headline's comparison from its own declared source axis. + * A copied meaning is only a proposal: ordinary candidate review must prove the + * other cell's period, entity, scope, accounting basis and all source qualifiers. + * This never repairs a rejected model candidate or promotes an unrelated table. + */ +export function acquireSourceComparisons( + review: CandidateReview, + documentType: DocumentType, + pages: ExtractedPage[], + context: DocumentContext +): CandidateReview { + if (!review.envelopeValid || documentType !== 'earnings') return review; + const tables = sourceDeclaredTables(pages, context).filter( + (table) => table.selectionRole === 'financialPerformance' + ); + const attempted = new Set([ + ...[...review.candidateSources.values()].flatMap((source) => + source.kind === 'table' ? [source.valueId] : [] + ), + ...review.diagnostics.flatMap((diagnostic) => + diagnostic.sourceKey ? [diagnostic.sourceKey] : [] + ), + ]); + const result = { + ...review, + facts: [...review.facts], + diagnostics: [...review.diagnostics], + unverified: [...review.unverified], + candidateSources: new Map(review.candidateSources), + candidateKinds: new Map(review.candidateKinds), + }; + let nextId = Math.max( + 0, + ...[ + ...review.candidateSources.keys(), + ...review.diagnostics.flatMap((d) => (d.candidateId ? [d.candidateId] : [])), + ].map((id) => Number(id.slice(1)) || 0) + ); + for (const current of review.facts) { + if ( + current.kind !== 'number' || + current.semantics.state !== 'actual' || + current.semantics.metricKind !== 'amount' || + current.evidence.kind !== 'table' || + summaryComparison(current, result.facts) + ) + continue; + const period = reportingPeriodText(current.period ?? ''); + const year = period.match(/^(20\d{2})年\d{1,2}月期/); + if (!year) continue; + const previousPeriod = period.replace(year[1], String(Number(year[1]) - 1)); + const fiscal = previousPeriod.match(/^20\d{2}年\d{1,2}月期/)![0]; + const table = tables.find((table) => table.tableId === current.provenance?.tableId); + const metricIds = current.evidence.metricIds; + const axis = table?.metricAxes.find( + (axis) => canonicalJSON([...axis.sourceIds].sort()) === canonicalJSON([...metricIds].sort()) + ); + if (!axis || !table) continue; + const candidates: Candidate[] = axis.valueIds.flatMap((valueId) => { + if (attempted.has(valueId)) return []; + // The prior year must be explicitly owned by this cell's source period axis. + // Review below independently proves the quarter, cumulative/standalone and state. + const periodAxis = table.periodAxes.find((axis) => axis.valueIds.includes(valueId)); + if (reportingPeriodText(periodAxis?.text ?? '').match(/20\d{2}年\d{1,2}月期/)?.[0] !== fiscal) + return []; + attempted.add(valueId); + const { qualifiers: _qualifiers, conditions: _conditions, ...meaning } = current.semantics; + void _qualifiers; + void _conditions; + return [ + { + candidateId: `c${++nextId}`, + importance: current.importance, + kind: 'number', + source: { + kind: 'table', + valueId, + tableId: table.tableId, + contextBindingId: `ctx:${valueId}`, + }, + meaning: { ...meaning, period: previousPeriod }, + }, + ]; + }); + if (!candidates.length) continue; + const checked = reviewCandidates( + JSON.stringify({ + candidateVersion: CANDIDATE_VERSION, + documentType, + candidates, + unverified: [], + }), + documentType, + pages, + context + ); + const pair = summaryComparison(current, [...result.facts, ...checked.facts]); + // A valid cell alone is insufficient: preserve uniqueness and full comparison semantics. + if (pair && checked.facts.some((fact) => fact.id === pair.reference.id)) + result.facts.push(pair.reference); + result.diagnostics.push( + ...checked.diagnostics.map((diagnostic) => ({ + ...diagnostic, + check: `comparison.${diagnostic.check}`, + })) + ); + result.unverified.push(...checked.unverified); + for (const [id, source] of checked.candidateSources) result.candidateSources.set(id, source); + for (const [id, kind] of checked.candidateKinds) result.candidateKinds.set(id, kind); + } + return result; +} diff --git a/src/lib/source-first-real-pdf.test.ts b/src/lib/source-first-real-pdf.test.ts index 761bf2f..ed9660e 100644 --- a/src/lib/source-first-real-pdf.test.ts +++ b/src/lib/source-first-real-pdf.test.ts @@ -10,6 +10,10 @@ import { parseAnalysis, parseAnalysisResponse, } from './additional-analysis'; +import { acquireSourceComparisons } from './source-comparison'; +import { buildDocumentContext } from './document-context'; +import { summaryComparison, comparisonGrowth } from './summary-comparison'; +import { renderFacts } from './fact-summary'; import { reviewCandidates } from './fact-candidates'; import { buildAnalysisCalculations } from './analysis-calculations'; import type { FactSummary } from './fact-contract'; @@ -111,6 +115,33 @@ describe('source-first public PDF regression', () => { ['税引前利益', '25204', '百万円'], ['税引前利益', '65500', '百万円'], ]); + // A selected current cell must recover its omitted prior-year partner through + // the ordinary verifier, independently of a model recalling the prior cell. + const currentOnly = pretaxCandidates(); + currentOnly.candidates = currentOnly.candidates.slice(0, 1); + currentOnly.candidates[0].importance = 'detail'; + currentOnly.candidates[0].meaning.period = '2027年2月期中間期'; + const acquired = acquireSourceComparisons( + reviewCandidates(JSON.stringify(currentOnly), 'earnings', pages), + 'earnings', + pages, + buildDocumentContext(pages) + ); + expect(acquired.facts.map((f) => f.quantity?.decimal)).toEqual(['25963', '25204']); + expect(acquired.facts[1].evidence).toMatchObject({ valueId: 'p1s62', metricIds: ['p1s38'] }); + const comparison = summaryComparison(acquired.facts[0], acquired.facts)!; + expect(comparisonGrowth(acquired.facts[0], comparison)).toEqual({ + kind: 'change', + rate: '+3.0%', + }); + const acquiredFacts = { ...noFacts, facts: acquired.facts }; + const acquiredDisplay = buildPresentation(acquiredFacts, pages); + const rendered = renderFacts(acquiredFacts, acquiredDisplay); + expect(rendered).toContain('25,204'); + expect(rendered).not.toContain('前年の値が要約に未抽出'); + expect( + renderFacts(acquiredFacts, revalidatePresentation(acquiredDisplay, acquiredFacts, pages)) + ).toBe(rendered); const facts = { ...noFacts, facts: reviewed.facts }; const display = buildPresentation(facts, pages); const selected = buildAnalysisInput(facts, display); diff --git a/src/lib/summary-narrative.test.ts b/src/lib/summary-narrative.test.ts index 6dece46..8a6e59e 100644 --- a/src/lib/summary-narrative.test.ts +++ b/src/lib/summary-narrative.test.ts @@ -311,7 +311,8 @@ describe('構造化を主とする表示と未整理部分の保持', () => { expect(reading).toContain(statement); expect(result.facts.facts[0].semantics.conditions).toContain(condition); expect(reading).toContain('当局の承認を条件とします'); - expect(reading).toContain('補足要約の未整理部分'); + expect(reading).toContain('一部の補足説明・数値は要約に反映できていません'); + expect(reading).not.toContain('補足要約の未整理部分'); const savedFacts: unknown = JSON.parse(JSON.stringify(result.facts)); validateSavedFacts(savedFacts); const restored = revalidatePresentation( @@ -513,7 +514,7 @@ describe('構造化を主とする表示と未整理部分の保持', () => { '↑増加 約+50.0%', '↑増加(+10百万円)', '価格転嫁', - '補足要約の未整理部分', + '一部の補足説明・数値は要約に反映できていません', ]) expect(reading).toContain(expected); expect(reading).not.toContain('来期の増収が確定'); diff --git a/src/lib/summary-presentation.test.ts b/src/lib/summary-presentation.test.ts index 2d482a9..29a36f2 100644 --- a/src/lib/summary-presentation.test.ts +++ b/src/lib/summary-presentation.test.ts @@ -1188,7 +1188,11 @@ describe('冒頭と本文の保持・復元・原文参照', () => { const reading = html.replace(toggles[0], ''); expect(reading).toContain(' + ({ + summary: `確認済み要約${padding}`, + companyName: 'テスト', + title: '決算', + code: '1234', + cachedAt: 1, + resultId: 'a'.repeat(64), + facts: { version: 6, facts: [] }, + presentation: {}, + metadata: {}, + }) as unknown as CachedSummary; + +describe('summary storage transport', () => { + it('keeps small/legacy entries intact and losslessly compresses large Japanese source text', async () => { + const small = entry(); + expect(await encodeSummaryStorage(small)).toBe(small); + expect(await decodeSummaryStorage(small)).toBe(small); + const large = entry('税引前利益 25,963百万円 前年同期25,204百万円\n'.repeat(15000)); + const encoded = await encodeSummaryStorage(large); + expect(JSON.stringify(encoded).length).toBeLessThan(JSON.stringify(large).length / 4); + expect(isSummaryStorageEntry(encoded)).toBe(true); + expect(await decodeSummaryStorage(JSON.parse(JSON.stringify(encoded)))).toEqual(large); + }); + + it('rejects corrupted, truncated and oversized compressed payloads', async () => { + const encoded = await encodeSummaryStorage(entry('根拠'.repeat(150000))); + expect('payload' in encoded).toBe(true); + await expect(decodeSummaryStorage({ ...encoded, resultId: 'other' })).rejects.toThrow( + '識別情報' + ); + await expect(decodeSummaryStorage({ ...encoded, payload: 'not base64!' })).rejects.toThrow(); + await expect(decodeSummaryStorage({ ...encoded, decodedBytes: 10 })).rejects.toThrow(); + await expect( + decodeSummaryStorage({ ...encoded, decodedBytes: 33 * 1024 * 1024 }) + ).rejects.toThrow(); + }); + + it('gives quota-specific next steps without presenting all failures as quota', () => { + expect(summaryStorageWarning(new Error('QUOTA_BYTES quota exceeded'))).toContain( + '要約キャッシュ管理' + ); + expect(summaryStorageWarning(new Error('Extension context invalidated'))).toContain( + '拡張機能が有効か' + ); + expect(summaryStorageWarning(new Error('Serialization error'))).not.toContain('容量が不足'); + }); +}); diff --git a/src/lib/summary-storage.ts b/src/lib/summary-storage.ts new file mode 100644 index 0000000..7b6cc52 --- /dev/null +++ b/src/lib/summary-storage.ts @@ -0,0 +1,130 @@ +import type { CachedSummary } from '@/types/summaryMetadata'; + +const ENCODING = 'tdnet-summary-gzip-v1'; +const COMPRESS_AFTER_BYTES = 256 * 1024; +const MAX_DECODED_BYTES = 32 * 1024 * 1024; +interface CompressedSummary { + encoding: typeof ENCODING; + payload: string; + decodedBytes: number; + companyName: string; + title: string; + code: string; + cachedAt: number; + resultId: string; +} + +function isCompressed(value: unknown): value is CompressedSummary { + return ( + !!value && + typeof value === 'object' && + (value as Partial).encoding === ENCODING + ); +} + +async function collect(stream: ReadableStream, limit: number): Promise { + const reader = stream.getReader(); + const chunks: Uint8Array[] = []; + let size = 0; + try { + while (true) { + const next = await reader.read(); + if (next.done) break; + size += next.value.byteLength; + if (size > limit) throw new Error('保存された要約の展開サイズが上限を超えています'); + chunks.push(next.value); + } + } finally { + await reader.cancel(); + reader.releaseLock(); + } + const result = new Uint8Array(size); + let offset = 0; + for (const chunk of chunks) { + result.set(chunk, offset); + offset += chunk.byteLength; + } + return result; +} + +function byteStream(bytes: Uint8Array): ReadableStream { + return new ReadableStream({ + start(controller) { + controller.enqueue(bytes); + controller.close(); + }, + }); +} + +/** Lossless transport only. Every restored entry still undergoes ordinary source validation. */ +export async function encodeSummaryStorage( + entry: CachedSummary +): Promise { + const bytes = new TextEncoder().encode(JSON.stringify(entry)); + if ( + bytes.byteLength < COMPRESS_AFTER_BYTES || + bytes.byteLength > MAX_DECODED_BYTES || + typeof CompressionStream === 'undefined' + ) + return entry; + const compressed = await collect( + byteStream(bytes).pipeThrough(new CompressionStream('gzip')), + MAX_DECODED_BYTES + ); + let binary = ''; + for (let offset = 0; offset < compressed.length; offset += 8192) + binary += String.fromCharCode(...compressed.subarray(offset, offset + 8192)); + const encoded: CompressedSummary = { + encoding: ENCODING, + payload: btoa(binary), + decodedBytes: bytes.byteLength, + companyName: entry.companyName, + title: entry.title, + code: entry.code, + cachedAt: entry.cachedAt, + resultId: entry.resultId, + }; + return JSON.stringify(encoded).length < bytes.byteLength ? encoded : entry; +} + +export async function decodeSummaryStorage(value: unknown): Promise { + if (!isCompressed(value)) return value; + if ( + typeof value.payload !== 'string' || + value.payload.length > MAX_DECODED_BYTES || + !Number.isSafeInteger(value.decodedBytes) || + value.decodedBytes < 0 || + value.decodedBytes > MAX_DECODED_BYTES + ) + throw new Error('保存された圧縮要約の形式が不正です'); + const binary = atob(value.payload); + const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0)); + const decoded = await collect( + byteStream(bytes).pipeThrough(new DecompressionStream('gzip')), + value.decodedBytes + ); + if (decoded.byteLength !== value.decodedBytes) + throw new Error('保存された圧縮要約のサイズが一致しません'); + const entry = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(decoded)); + if ( + !entry || + typeof entry !== 'object' || + ['resultId', 'companyName', 'title', 'code', 'cachedAt'].some( + (key) => entry[key] !== value[key as keyof CompressedSummary] + ) + ) + throw new Error('保存された圧縮要約の識別情報が一致しません'); + return entry; +} + +export function isSummaryStorageEntry(value: unknown): boolean { + return !!value && typeof value === 'object' && ('facts' in value || isCompressed(value)); +} + +export function summaryStorageWarning(error: unknown, subject = '要約'): string { + const message = error instanceof Error ? error.message : String(error); + const reason = /quota|QUOTA_BYTES|容量|上限/i.test(message) + ? '保存容量が不足しています。設定の「要約キャッシュ管理」で不要な要約を削除してから再度お試しください。' + : 'ブラウザーの保存処理に失敗しました。拡張機能が有効か確認してください。'; + return `${subject}を保存できませんでした。${reason}表示結果は利用できますが、ページを再読み込みすると失われる場合があります`; +} diff --git a/src/options/Options.test.ts b/src/options/Options.test.ts index 9bebb22..59fa57e 100644 --- a/src/options/Options.test.ts +++ b/src/options/Options.test.ts @@ -5,6 +5,8 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; import { ANALYSIS_CACHE_DIAGNOSTIC_PREFIX, ANALYSIS_DIAGNOSTICS_KEY } from '@/lib/analysis-trace'; import { SUMMARY_DIAGNOSTICS_KEY } from '@/lib/summary-trace'; import Options from './Options'; +import { encodeSummaryStorage } from '@/lib/summary-storage'; +import type { CachedSummary } from '@/types/summaryMetadata'; let root: Root | undefined; afterEach(async () => { @@ -15,9 +17,14 @@ afterEach(async () => { }); describe('追加分析キャッシュと診断参照の削除', () => { - it.each(['削除', 'すべて削除'])( - '%sで成功キャッシュの参照も消し、上限付き実行履歴は別に保つ', - async (label) => { + it.each([ + ['削除', false], + ['すべて削除', false], + ['削除', true], + ['すべて削除', true], + ] as const)( + '%sで成功キャッシュの参照も消し、上限付き実行履歴は別に保つ(圧縮=%s)', + async (label, compressed) => { const stored: Record = { 'summaryCacheV2:fixture': { resultId: 'summary-result', @@ -33,6 +40,11 @@ describe('追加分析キャッシュと診断参照の削除', () => { [ANALYSIS_DIAGNOSTICS_KEY]: { version: 1, traces: [] }, [SUMMARY_DIAGNOSTICS_KEY]: { version: 1, traces: [] }, }; + if (compressed) + stored['summaryCacheV2:fixture'] = await encodeSummaryStorage({ + ...(stored['summaryCacheV2:fixture'] as CachedSummary), + summary: '原文'.repeat(150000), + }); vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); vi.stubGlobal('chrome', { storage: { @@ -50,6 +62,10 @@ describe('追加分析キャッシュと診断参照の削除', () => { document.body.append(container); root = createRoot(container); await act(async () => root!.render(createElement(Options))); + await vi.waitFor(async () => { + await act(async () => {}); + expect(container.textContent).toContain('fixture'); + }); const button = Array.from(container.querySelectorAll('button')).find( (item) => item.textContent === label )!; diff --git a/src/options/Options.tsx b/src/options/Options.tsx index 9e7f2da..6f47a12 100644 --- a/src/options/Options.tsx +++ b/src/options/Options.tsx @@ -1,10 +1,15 @@ -import React, { useEffect, useState } from 'react'; +import { decodeSummaryStorage, isSummaryStorageEntry } from '@/lib/summary-storage'; +import React, { useEffect, useState, useRef } from 'react'; import type { ExtractionMode, CachedSummary } from '@/types/summaryMetadata'; import { ANALYSIS_CACHE_DIAGNOSTIC_PREFIX } from '@/lib/analysis-trace'; import { LLM_PROVIDERS, getProvider } from '@/lib/llm-providers'; import { customApiPermission, SCORING_PDF_PERMISSIONS } from '@/lib/host-permissions'; const CACHE_PREFIX = 'summaryCacheV2:'; +type CacheListEntry = Pick< + CachedSummary, + 'title' | 'companyName' | 'code' | 'cachedAt' | 'resultId' +>; const Options: React.FC = () => { const [provider, setProvider] = useState('openai'); @@ -17,7 +22,14 @@ const Options: React.FC = () => { const [saved, setSaved] = useState(false); const [autoSwitchedToCustom, setAutoSwitchedToCustom] = useState(false); const [hasAutoSwitched, setHasAutoSwitched] = useState(false); - const [cacheEntries, setCacheEntries] = useState<[string, CachedSummary][]>([]); + const cacheLoadRequest = useRef(0); + useEffect( + () => () => { + cacheLoadRequest.current++; + }, + [] + ); + const [cacheEntries, setCacheEntries] = useState<[string, CacheListEntry][]>([]); const [permissionMessage, setPermissionMessage] = useState(''); const SETTINGS_KEYS = [ @@ -114,15 +126,35 @@ const Options: React.FC = () => { }; const loadCacheEntries = () => { + const request = ++cacheLoadRequest.current; chrome.storage.local.get(null, (data) => { - const entries = Object.entries(data) - .filter( - ([key, value]) => - key.startsWith(CACHE_PREFIX) && value && typeof value === 'object' && 'facts' in value - ) - .map(([key, value]) => [key, value as CachedSummary] as [string, CachedSummary]) - .sort(([, a], [, b]) => b.cachedAt - a.cachedAt); - setCacheEntries(entries); + void (async () => { + const entries: [string, CacheListEntry][] = []; + // Decode one ledger at a time; retain only list metadata, never the + // expanded source documents for every cached summary in React state. + for (const [key, value] of Object.entries(data)) { + if (request !== cacheLoadRequest.current) return; + if (!key.startsWith(CACHE_PREFIX) || !isSummaryStorageEntry(value)) continue; + try { + const decoded = (await decodeSummaryStorage(value)) as CachedSummary; + const { title, companyName, code, cachedAt, resultId } = decoded; + entries.push([key, { title, companyName, code, cachedAt, resultId }]); + } catch { + entries.push([ + key, + { + title: '読み込めない保存要約', + companyName: '', + code: '', + cachedAt: 0, + resultId: '', + }, + ]); + } + } + if (request !== cacheLoadRequest.current) return; + setCacheEntries(entries.sort(([, a], [, b]) => b.cachedAt - a.cachedAt)); + })(); }); }; @@ -131,7 +163,7 @@ const Options: React.FC = () => { chrome.storage.local.remove( [ key, - ...(entry + ...(entry?.resultId ? [ `scoreCacheV1:${entry.resultId}`, `scoreCacheV2:${entry.resultId}`,