diff --git a/docs/development.md b/docs/development.md index 0962f50..775d44d 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1959,3 +1959,22 @@ v96の事前固定2実行は6要求で終了。BlueMeme/Worldとも独立した テストの担当はsummary-narrativeの事実表示・数量所有境界と、summary-presentationの比較行選択・保存復元。既存のイベント例と同額セル例を更新し、全経路へ反例を複製しない。旧表示のキャッシュと分離するため分析指紋をv98にする。事実v6・表示v6・補足保存v3は維持する。今回の確認は固定応答とローカル検査で、実モデル呼出し・実拡張画面・配置は行わない。生成へ渡る表示数量の一部が変わるため、v96の実モデル成功をv98の実績へ読み替えない。 最終差分で `npm test` は1,141件/35ファイル成功(19.54秒)。type-check・lint・buildも成功した。固定応答の実PDF.js→HTTP→検証→HTML→保存再照合は通常テストに含まれる。表示・数量の独立差分レビューを終え、全体検証後の製品ソース変更はない。実拡張smokeのsocket制約と実モデル未評価は残る。 + +### PR #30の残存境界修正(2026-10-07、v103) + +- キャッシュ・結果照合・採点/追加分析は、実際の要求と共通の実効API URLを含む分析指紋を使う。URLの正規化はHTTPの実効URLに合わせ、パス・クエリの違いを維持する。保存されるのはURLのSHA-256で、APIキー・URL文字列を指紋へ含めない。URLのみの設定変更も進行中の結果と既存表示を無効化する +- 補足指標は意味の一致より先に原数量の物理IDを照合する。確認済みの状態・標準指標区分・範囲/基準・明示期間と明確に矛盾する現在値/比較値は未整理とし、既知の実績・比較は残す。複数の確定文脈のどれかが矛盾しない場合や、自由な名称・欠けた文脈は一律に拒否しない。同額の別セルを同一視しない +- 担当はURL/指紋の部品、実Reactの設定変更と遅い応答、backgroundの後続要求、disclosure-observationの採否。意味点検を誤承認する固定応答の保存復元→本文/冒頭→追加分析/計算と、除外した数量の未整理範囲を代表例で確認する。既存の実PDF由来コーパスは通常テストで維持する + +実API・実Chromeの画面/拡張通信は未確認。固定した誤承認応答での再現は、実モデルで誤承認が発生したという証拠ではない。API追加・個社例外・新CIはない。 + +### PR #30の再レビュー対応(2026-10-08、v104) + +- 数量参照の検査を現在値・比較値・原文増減率へ揃える。増減率の物理所有者に既知の指標・期間・範囲・基準・状態の矛盾があれば除外する。確定した金額/率の対応がある場合は現在値の物理IDを照合し、確認済み比較があれば比較軸と比較値IDも照合する。前期金額の未抽出だけで率の所有者を失わず、未知の文脈・未対応の補足率を一律には拒否しない。 +- 既知の指標・範囲・会計基準の別名を、矛盾検査・統合・比較基準・比較値の重複除外で共有する。未知の自由記述と既知の正規キーを区別し、同額の別セルを統合しない。追加分析では統合済み指標を確定事実への補足として渡し、同じ現在値を別の数量根拠として再掲しない。点検済み区分・比較・条件・観測ID・原文対応は残す。 +- 後続キャッシュの読込失敗と保存スコアの検証失敗を「未保存」と区別する。要約本文と明示再試行を維持し、開き直しや採点設定切替による暗黙の再課金を止める。遅い読込失敗で開始済みの追加分析を置き換えない。 +- React rootのDOM挿入と非同期の購読完了を区別し、行変更・iframe再読込/置換・再有効化の検査を購読完了まで待機する。タイムアウトや検査条件を緩めない。 + +境界の担当は `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説明の意味点検到達を保持し、固定した点検応答の表示と保存復元が一致した。固定点検の成功を実モデル品質の証明には数えない。 diff --git a/evaluation/scripts/check-extension.ts b/evaluation/scripts/check-extension.ts index 87129db..6bc88a3 100644 --- a/evaluation/scripts/check-extension.ts +++ b/evaluation/scripts/check-extension.ts @@ -14,7 +14,12 @@ import { stableFactId, FACT_SCHEMA_VERSION, type VerifiedFact } from '../../src/ import expectations from '../../src/lib/fixtures/ir-semantic-expectations.json'; import { parseFactSummary } from '../../src/lib/fact-summary'; import { buildPresentation } from '../../src/lib/summary-presentation'; -import { ANALYSIS_SCHEMA_VERSION } from '../../src/lib/analysis-version'; +import { + ANALYSIS_SCHEMA_VERSION, + buildAnalysisFingerprint, + buildSummaryCacheKey, +} from '../../src/lib/analysis-version'; +import { configuredApiUrl } from '../../src/lib/llm-endpoint'; import { additionalReviewFixture } from './additional-review-fixture'; import { seedOldExtensionProfile } from './extension-upgrade-fixture'; import { @@ -1440,15 +1445,26 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: assert.equal(apiCalls, callsBefore); const expectedRestored = stored.value.facts; assert.deepEqual(renderedFactErrors(expectedRestored.facts, await displayedFacts(summary)), []); + const restoredSettings = await worker.evaluate(async () => + chrome.storage.sync.get(['provider', 'model', 'customUrl', 'extractionMode']) + ); + const restoredKey = + 'summaryCacheV2:' + + buildSummaryCacheKey( + pdfUrl, + await buildAnalysisFingerprint({ + provider: restoredSettings.provider ?? 'openai', + model: restoredSettings.model ?? 'gpt-4o', + extractionMode: restoredSettings.extractionMode ?? 'full', + baseUrl: configuredApiUrl({ + provider: restoredSettings.provider ?? 'openai', + customUrl: restoredSettings.customUrl, + }), + }) + ); const restoredValue = await worker.evaluate( - async (request: any) => { - const settings = await chrome.storage.sync.get(['provider', 'model', 'extractionMode']); - const fingerprint = `v${request.version}:${encodeURIComponent(settings.provider)}:${encodeURIComponent(settings.model)}:${settings.extractionMode}`; - return (await chrome.storage.local.get(`summaryCacheV2:${fingerprint}:${request.pdfUrl}`))[ - `summaryCacheV2:${fingerprint}:${request.pdfUrl}` - ]; - }, - { version: ANALYSIS_SCHEMA_VERSION, pdfUrl } + async (key: string) => (await chrome.storage.local.get(key))[key], + restoredKey ); assert.deepEqual(restoredValue.facts, expectedRestored); assert.equal(restoredValue.metadata.extractionMode, 'full'); @@ -1628,17 +1644,14 @@ export async function checkExtension(item: BrowserCase, config: LLMConfig, args: await worker.evaluate(async () => chrome.storage.sync.set({ experimentalScoring: false })); await row.getByRole('button', { name: '閉じる', exact: true }).click(); await worker.evaluate( - async (request: any) => { - const settings = await chrome.storage.sync.get(['provider', 'model', 'extractionMode']); - const fingerprint = `v${request.version}:${encodeURIComponent(settings.provider)}:${encodeURIComponent(settings.model)}:${settings.extractionMode}`; - const key = `summaryCacheV2:${fingerprint}:${request.pdfUrl}`; + async (key: string) => { const entry = { key, value: (await chrome.storage.local.get(key))[key] }; if (!entry.value) throw new Error('現在の設定で復元したキャッシュがありません'); await chrome.storage.local.set({ [entry.key]: { ...entry.value, facts: { ...entry.value.facts, version: 3 } }, }); }, - { version: ANALYSIS_SCHEMA_VERSION, pdfUrl } + restoredKey ); const previousCalls = apiCalls; await row.getByRole('button', { name: '表示', exact: true }).click(); diff --git a/evaluation/scripts/run-fact-summary.ts b/evaluation/scripts/run-fact-summary.ts index 977690e..105888b 100644 --- a/evaluation/scripts/run-fact-summary.ts +++ b/evaluation/scripts/run-fact-summary.ts @@ -87,6 +87,8 @@ const implementationFiles = [ 'src/lib/summary-renderer.ts', 'src/lib/summary-result-id.ts', 'src/lib/llm-client.ts', + 'src/lib/llm-endpoint.ts', + 'src/lib/analysis-version.ts', 'src/lib/llm-providers.ts', 'src/lib/structured-output.ts', ]; @@ -179,7 +181,12 @@ for (const item of selected) { const output = { item, schemaVersion: FACT_SCHEMA_VERSION, - analysisFingerprint: buildAnalysisFingerprint({ provider, model, extractionMode: 'full' }), + analysisFingerprint: await buildAnalysisFingerprint({ + provider, + model, + extractionMode: 'full', + baseUrl: process.env.TDNET_DIGEST_BASE_URL || undefined, + }), implementationDigest, requestLimits: factSummaryRequestLimits({ provider, model }), sourceHash, diff --git a/src/background/index.test.ts b/src/background/index.test.ts index 590d9cc..2943f42 100644 --- a/src/background/index.test.ts +++ b/src/background/index.test.ts @@ -22,7 +22,7 @@ interface TestResponse { error?: string; diagnosticRunId: string; summary: string; - metadata: { analysisFingerprint: string; score?: unknown }; + metadata: { analysisFingerprint: string; score?: unknown; persistenceWarning?: string }; facts: FactSummary; presentation: import('../lib/summary-presentation').SummaryPresentation; resultId: string; @@ -350,6 +350,70 @@ describe('要約・採点・追加分析の分離', () => { } ); + it('診断保存の失敗は完成結果と生成元のエラーを覆わず、後続の保存も妨げない', async () => { + const request = await setup(false); + mocked.generateText.mockResolvedValue( + candidateResponse(facts.facts, [nativePage], facts.documentType) + ); + vi.mocked(chrome.storage.local.set).mockRejectedValue(new Error('storage quota exceeded')); + const success = await request({ action: 'summarize' }); + expect(success.error).toBeUndefined(); + expect(success.summary).toContain('1150百万円'); + expect(success.metadata.persistenceWarning).toContain('診断を保存できませんでした'); + mocked.generateText.mockRejectedValueOnce(new Error('generation failed')); + const failure = await request({ action: 'summarize' }); + expect(failure.error).toBe('generation failed'); + vi.mocked(chrome.storage.local.set).mockResolvedValue(undefined); + const next = await request({ action: 'summarize' }); + expect(next.error).toBeUndefined(); + expect(next.metadata.persistenceWarning).toBeUndefined(); + expect(vi.mocked(chrome.storage.local.set).mock.calls.at(-1)?.[0]).toMatchObject({ + summaryLastRunV1: { runId: next.diagnosticRunId, outcome: 'firstSuccess' }, + }); + }); + + it.each([false, true])( + '同時初回要求はOffscreen初期化を共有し、失敗=%sの後も再確認する', + async (fails) => { + const request = await setup(false); + mocked.generateText.mockResolvedValue( + candidateResponse(facts.facts, [nativePage], facts.documentType) + ); + chrome.runtime.getContexts = vi.fn(async () => []); + let finish!: () => void; + let entered!: () => void; + const started = new Promise((resolve) => { + entered = resolve; + }); + vi.mocked(chrome.offscreen.createDocument) + .mockImplementationOnce(async () => { + entered(); + await new Promise((resolve) => { + finish = resolve; + }); + if (fails) throw new Error('offscreen creation failed'); + }) + .mockResolvedValue(undefined); + const first = request({ action: 'summarize', pdfUrl: 'first.pdf' }); + await started; + const second = request({ action: 'summarize', pdfUrl: 'second.pdf' }); + // Both requests have downloaded before the shared initialization completes. + await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(2)); + expect(chrome.offscreen.createDocument).toHaveBeenCalledTimes(1); + finish(); + const results = await Promise.all([first, second]); + for (const result of results) { + if (fails) expect(result.error).toBe('offscreen creation failed'); + else expect(result.summary).toContain('1150百万円'); + } + expect(chrome.runtime.getContexts).toHaveBeenCalledTimes(1); + const next = await request({ action: 'summarize' }); + expect(next.error).toBeUndefined(); + expect(chrome.runtime.getContexts).toHaveBeenCalledTimes(2); + expect(chrome.offscreen.createDocument).toHaveBeenCalledTimes(2); + } + ); + it('更新前から残る二段階要約設定を削除し、要約を続行する', async () => { mocked.generateText.mockResolvedValue( candidateResponse(facts.facts, [nativePage], facts.documentType) @@ -431,6 +495,46 @@ describe('要約・採点・追加分析の分離', () => { expect(mocked.extractScoreInput).not.toHaveBeenCalled(); }); + it('customUrlだけの変更で旧要約の追加分析・採点を通信前に拒否する', async () => { + mocked.generateText.mockResolvedValue( + candidateResponse(facts.facts, [nativePage], facts.documentType) + ); + const request = await setup(true); + const settings = { + provider: 'custom', + model: 'test', + apiKey: 'private-api-key', + customUrl: 'HTTPS://API.EXAMPLE.COM:443/v1/chat?deployment=a&token=private-token#ignored', + extractionMode: 'full', + experimentalScoring: true, + }; + chrome.storage.sync.get = vi.fn(async () => settings) as typeof chrome.storage.sync.get; + const summary = await request({ action: 'summarize' }); + expect(summary.error).toBeUndefined(); + expect(mocked.generateText.mock.calls[0][0].baseUrl).toBe( + 'https://api.example.com/v1/chat?deployment=a&token=private-token' + ); + const persisted = JSON.stringify(vi.mocked(chrome.storage.local.set).mock.calls); + expect(persisted).not.toContain('private-api-key'); + expect(persisted).not.toContain('private-token'); + expect(summary.metadata.analysisFingerprint).not.toContain('api.example.com'); + const fetches = vi.mocked(fetch).mock.calls.length; + settings.customUrl = settings.customUrl.replace('deployment=a', 'deployment=b'); + for (const action of ['analyze', 'score']) { + const stale = await request({ + action, + facts: summary.facts, + presentation: summary.presentation, + resultId: summary.resultId, + fingerprint: summary.metadata.analysisFingerprint, + }); + expect(stale.error).toContain('設定が変更されています'); + } + expect(fetch).toHaveBeenCalledTimes(fetches); + expect(mocked.generateText).toHaveBeenCalledTimes(1); + expect(mocked.extractScoreInput).not.toHaveBeenCalled(); + }); + it('スコアONの採点は別要求で事実を起点にする', async () => { mocked.generateText.mockResolvedValue( candidateResponse(facts.facts, [nativePage], facts.documentType) diff --git a/src/background/index.ts b/src/background/index.ts index 555b7ea..0e290b8 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,6 +1,7 @@ import { SUMMARY_TRACE_KEY, summaryBuildDigest, type SummaryTrace } from '@/lib/summary-trace'; import { serializeCandidateSource } from '@/lib/fact-candidates'; import type { LLMConfig } from '@/lib/llm-client'; +import { configuredApiUrl } from '@/lib/llm-endpoint'; import { detectDocumentType, type DocumentType } from '@/lib/document-type'; import { FACT_SCHEMA_VERSION, @@ -98,16 +99,26 @@ chrome.runtime.onMessage.addListener((request: Request, _sender, sendResponse) = return true; }); -async function setupOffscreenDocument(): Promise { - const existing = await chrome.runtime.getContexts({ - contextTypes: ['OFFSCREEN_DOCUMENT' as chrome.runtime.ContextType], - }); - if (!existing.length) - await chrome.offscreen.createDocument({ - url: 'offscreen.html', - reasons: ['DOM_PARSER' as chrome.offscreen.Reason], - justification: 'PDF.jsによる文字抽出', +// Concurrent summary/follow-up requests share only the in-flight initialization. +// Recheck contexts after completion so a subsequently closed document is recreated. +let offscreenSetup: Promise | null = null; +function setupOffscreenDocument(): Promise { + if (!offscreenSetup) { + offscreenSetup = (async () => { + const existing = await chrome.runtime.getContexts({ + contextTypes: ['OFFSCREEN_DOCUMENT' as chrome.runtime.ContextType], + }); + if (!existing.length) + await chrome.offscreen.createDocument({ + url: 'offscreen.html', + reasons: ['DOM_PARSER' as chrome.offscreen.Reason], + justification: 'PDF.jsによる文字抽出', + }); + })().finally(() => { + offscreenSetup = null; }); + } + return offscreenSetup; } async function getSettings(): Promise { @@ -160,7 +171,7 @@ function configOf(settings: Settings): LLMConfig { provider: settings.provider, apiKey: settings.apiKey, model: settings.model, - baseUrl: settings.customUrl || undefined, + baseUrl: configuredApiUrl(settings), }; } async function fetchPDF(url: string): Promise { @@ -196,6 +207,7 @@ async function handleSummarize(request: SummarizeRequest, runId: string) { outcome: 'running', error: null, }; + let persistenceWarning: string | undefined; const saveTrace = async () => { trace.elapsedMs = Math.round(performance.now() - started); try { @@ -204,13 +216,11 @@ async function handleSummarize(request: SummarizeRequest, runId: string) { if (currentSummaryRunId === runId) await chrome.storage.local.set({ [SUMMARY_TRACE_KEY]: snapshot }); }); - // A failed write belongs to its request, and cannot poison later requests. + // Diagnostic persistence cannot fail generation or poison later writes. traceWriteQueue = write.catch(() => {}); await write; } catch { - throw new Error( - `診断の保存に失敗しました。${trace.error ?? '直近実行を保存できませんでした'}` - ); + persistenceWarning = '診断を保存できませんでした。表示結果は利用できます'; } }; @@ -230,9 +240,8 @@ async function handleSummarize(request: SummarizeRequest, runId: string) { await setupOffscreenDocument(); const extraction = await extractTextFromPDF(data, documentType, mode); const config = configOf(settings); - const fingerprint = buildAnalysisFingerprint({ - provider: settings.provider, - model: settings.model, + const fingerprint = await buildAnalysisFingerprint({ + ...config, extractionMode: mode, }); const documentHash = await hashPdf(data); @@ -282,6 +291,7 @@ async function handleSummarize(request: SummarizeRequest, runId: string) { summaryMode: 'sourced-summary', generationCalls: trace.attempts.length, analysisFingerprint: fingerprint, + ...(persistenceWarning ? { persistenceWarning } : {}), }; return { summary: renderFacts(facts, generated.presentation), @@ -303,8 +313,11 @@ async function handleFollowup( ): Promise<{ score?: ExperimentalScore; analysis?: AdditionalAnalysis }> { validatePresentation(request.presentation, request.facts); const settings = await getSettings(); - const fingerprints = (['full', 'smart'] as const).map((extractionMode) => - buildAnalysisFingerprint({ provider: settings.provider, model: settings.model, extractionMode }) + const config = configOf(settings); + const fingerprints = await Promise.all( + (['full', 'smart'] as const).map((extractionMode) => + buildAnalysisFingerprint({ ...config, extractionMode }) + ) ); if (!fingerprints.includes(request.fingerprint)) throw new Error('設定が変更されています。要約をやり直してください'); @@ -331,7 +344,6 @@ async function handleFollowup( )) !== request.resultId ) throw new Error('要約結果の識別子が一致しません'); - const config = configOf(settings); if (request.action === 'analyze') return { analysis: await analyzeFacts(config, facts, presentation) }; if (!settings.experimentalScoring) throw new Error('実験的スコアがOFFです'); diff --git a/src/content/SummaryButton.tsx b/src/content/SummaryButton.tsx index 4ff79c0..e0dafac 100644 --- a/src/content/SummaryButton.tsx +++ b/src/content/SummaryButton.tsx @@ -27,6 +27,7 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } analysis, scoringEnabled, hasCached, + persistenceWarning, cacheKey, summarize, showCached, @@ -117,9 +118,14 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } }, [result, removeSummaryRow, insertSummaryRow, reset, summarize, triggerUpdate]); useEffect(() => { - updateStages(scoringEnabled ? score : undefined, analysis, result?.facts?.facts); + updateStages( + scoringEnabled ? score : undefined, + analysis, + result?.facts?.facts, + persistenceWarning + ); if (result?.summary && scoringEnabled) startScore(); - }, [score, analysis, scoringEnabled, result, updateStages, startScore]); + }, [score, analysis, scoringEnabled, result, persistenceWarning, updateStages, startScore]); const [diagnosticError, setDiagnosticError] = useState(null); const [diagnosticText, setDiagnosticText] = useState(null); @@ -175,18 +181,16 @@ const SummaryButton: React.FC = ({ rowData, row, iframeDoc } return; } - if (hasCached) { - showCached(); - return; - } - // 同一描画内の連打でも要約要求は一度だけ送る。 if (summaryPending.current) return; - const request = summarize(); + const request = (async () => { + if (!hasCached || !(await showCached())) await summarize(); + })(); summaryPending.current = request; - void request.finally(() => { + const clearPending = () => { if (summaryPending.current === request) summaryPending.current = null; - }); + }; + void request.then(clearPending, clearPending); }; // ボタンテキスト diff --git a/src/content/contentLifecycle.tsx b/src/content/contentLifecycle.tsx new file mode 100644 index 0000000..9d578af --- /dev/null +++ b/src/content/contentLifecycle.tsx @@ -0,0 +1,197 @@ +import React from 'react'; +import { createRoot, type Root } from 'react-dom/client'; +import SummaryButton from './SummaryButton'; +import { extractRowData } from './utils/rowDataExtractor'; +import { addHeaderColumn, getRowCellClass, updateLastCellClass } from './utils/tdnetDomHelper'; + +interface MountedRow { + root: Root; + cell: HTMLTableCellElement; + data: string; + restoreCell: () => void; +} + +// Each enabled session owns its frame, observers and React roots. DOM removal is +// always preceded by unmount, including rows/documents replaced by the host page. +export function startContentScript(hostDocument: Document = document): () => void { + let disposed = false; + let enabled = false; + let settingsRevision = 0; + let hostObserver: MutationObserver | null = null; + let frameObserver: MutationObserver | null = null; + let frame: HTMLIFrameElement | null = null; + let frameDocument: Document | null = null; + let header: { cell: Element; restore: () => void } | null = null; + const roots = new Map(); + + const removeRow = (row: HTMLTableRowElement, mounted: MountedRow) => { + roots.delete(row); + mounted.root.unmount(); + mounted.cell.remove(); + mounted.restoreCell(); + }; + const clearDocument = () => { + frameObserver?.disconnect(); + frameObserver = null; + roots.forEach((mounted, row) => removeRow(row, mounted)); + header?.restore(); + header = null; + frameDocument = null; + }; + + const inject = () => { + const doc = frameDocument; + if (disposed || !enabled || !doc) return; + const table = doc.querySelector('#main-list-table'); + roots.forEach((mounted, row) => { + if ( + !table?.contains(row) || + !row.contains(mounted.cell) || + JSON.stringify(extractRowData(row)) !== mounted.data + ) + removeRow(row, mounted); + }); + if (header && !doc.contains(header.cell)) { + header.restore(); + header = null; + } + if (!header) { + const previous = doc.querySelector('#list-head tr td:last-child'); + const previousClass = previous?.className; + const previousRadius = previous?.style.borderRadius; + addHeaderColumn(doc); + const cell = doc.querySelector('.tdnet-digest-header'); + if (cell) { + const appliedClass = previous?.className; + header = { + cell, + restore: () => { + cell.remove(); + if (previous && previous.className === appliedClass) { + previous.className = previousClass!; + previous.style.borderRadius = previousRadius!; + } + }, + }; + } + } + table?.querySelectorAll('tbody > tr').forEach((row) => { + if (roots.has(row) || row.classList.contains('tdnet-digest-summary-row')) return; + const rowData = extractRowData(row); + if (!rowData) return; + const previous = row.lastElementChild; + const previousClass = previous?.className; + updateLastCellClass(row); + const appliedClass = previous?.className; + const cell = doc.createElement('td'); + cell.className = `${getRowCellClass(row)} tdnet-digest-button-cell`; + cell.setAttribute('nowrap', ''); + cell.setAttribute('align', 'center'); + cell.style.width = '80px'; + const container = doc.createElement('div'); + cell.appendChild(container); + row.appendChild(cell); + const root = createRoot(container); + roots.set(row, { + root, + cell, + data: JSON.stringify(rowData), + restoreCell: () => { + if (previous && previous.className === appliedClass) previous.className = previousClass!; + }, + }); + root.render( + + + + ); + }); + }; + + const attachDocument = () => { + if (!enabled || disposed || !frame) return; + let doc: Document | null = null; + try { + doc = frame.contentDocument; + } catch { + // A navigating frame can temporarily be inaccessible. Its next load retries. + } + if (doc !== frameDocument) { + clearDocument(); + frameDocument = doc; + if (doc) { + frameObserver = new MutationObserver(inject); + frameObserver.observe(doc, { + childList: true, + subtree: true, + characterData: true, + attributes: true, + attributeFilter: ['href'], + }); + } + } + inject(); + }; + const loaded = () => { + clearDocument(); + attachDocument(); + }; + const clearFrame = () => { + frame?.removeEventListener('load', loaded); + clearDocument(); + frame = null; + }; + const findFrame = () => { + if (!enabled || disposed) return; + const next = hostDocument.querySelector('iframe#main_list'); + if (next !== frame) { + clearFrame(); + frame = next; + frame?.addEventListener('load', loaded); + } + attachDocument(); + }; + const setEnabled = (next: boolean) => { + if (disposed || next === enabled) return; + enabled = next; + if (enabled) { + hostObserver = new MutationObserver(findFrame); + hostObserver.observe(hostDocument, { childList: true, subtree: true }); + findFrame(); + } else { + hostObserver?.disconnect(); + hostObserver = null; + clearFrame(); + } + }; + const onMessage = (request: { action?: string; enabled?: boolean }) => { + if (request.action !== 'toggleExtension') return; + settingsRevision++; + setEnabled(request.enabled === true); + }; + const onStorage = (changes: Record, area: string) => { + if (area !== 'sync' || !('extensionEnabled' in changes)) return; + settingsRevision++; + setEnabled(changes.extensionEnabled.newValue !== false); + }; + const pageHidden = (event: PageTransitionEvent) => { + // BFCache retains this realm and its listeners. Keep it usable on Back. + if (!event.persisted) dispose(); + }; + const dispose = () => { + if (disposed) return; + setEnabled(false); + disposed = true; + chrome.runtime.onMessage.removeListener(onMessage); + chrome.storage.onChanged.removeListener(onStorage); + hostDocument.defaultView?.removeEventListener('pagehide', pageHidden); + }; + chrome.runtime.onMessage.addListener(onMessage); + chrome.storage.onChanged.addListener(onStorage); + hostDocument.defaultView?.addEventListener('pagehide', pageHidden); + const revision = settingsRevision; + chrome.storage.sync.get(['extensionEnabled'], (settings) => { + if (revision === settingsRevision) setEnabled(settings.extensionEnabled !== false); + }); + return dispose; +} diff --git a/src/content/hooks/useSummarize.test.ts b/src/content/hooks/useSummarize.test.ts index 441f3b9..59e28c5 100644 --- a/src/content/hooks/useSummarize.test.ts +++ b/src/content/hooks/useSummarize.test.ts @@ -20,6 +20,7 @@ import { buildAnalysisInput } from '@/lib/analysis-input'; import { SUMMARY_TRACE_KEY, type SummaryTrace } from '@/lib/summary-trace'; import SummaryButton from '../SummaryButton'; import { useSummarize } from './useSummarize'; +import { startContentScript } from '../contentLifecycle'; const pdfUrl = 'https://www.release.tdnet.info/inbs/example.pdf'; const options = { pdfUrl, title: '開示', code: '1234', companyName: '株式会社テスト' }; @@ -47,17 +48,22 @@ const facts = parseFactSummary( ); const presentation = buildPresentation(facts, pages); const documentHash = 'c'.repeat(64); -const keyFor = (mode: 'smart' | 'full', url = pdfUrl) => +const keyFor = async (mode: 'smart' | 'full', url = pdfUrl) => 'summaryCacheV2:' + buildSummaryCacheKey( url, - buildAnalysisFingerprint({ provider: 'openai', model: 'fixture', extractionMode: mode }) + await buildAnalysisFingerprint({ provider: 'openai', model: 'fixture', extractionMode: mode }) ); -async function responseFor(mode: 'smart' | 'full' = 'full') { - const analysisFingerprint = buildAnalysisFingerprint({ +async function responseFor( + mode: 'smart' | 'full' = 'full', + endpoint: { provider: string; customUrl: string } | undefined = undefined +) { + const analysisFingerprint = await buildAnalysisFingerprint({ provider: 'openai', model: 'fixture', extractionMode: mode, + ...endpoint, + baseUrl: endpoint?.customUrl, }); return { error: null, @@ -71,6 +77,7 @@ async function responseFor(mode: 'smart' | 'full' = 'full') { } let root: Root; +let stopContentScript: (() => void) | undefined; let container: HTMLDivElement; let settings: Record; let stored: Record; @@ -78,6 +85,7 @@ const sendMessage = vi.fn(); const save = vi.fn(); const remove = vi.fn(); const listeners = new Set<(changes: Record, area: string) => void>(); +const messages = new Set<(request: { action: string; enabled: boolean }) => void>(); // Only the Chrome transport/storage boundary is replaced. React owns state, effects and rerenders. async function mount(url = pdfUrl) { @@ -90,6 +98,11 @@ async function mount(url = pdfUrl) { document.body.append(container); root = createRoot(container); await act(async () => root.render(createElement(Probe))); + if (!vi.isMockFunction(chrome.storage.sync.get)) + await vi.waitFor(async () => { + await act(async () => {}); + expect(current.cacheKey).not.toBeNull(); + }); return () => current; } async function mountButton() { @@ -110,14 +123,20 @@ async function mountButton() { }) ) ); + await vi.waitFor(async () => { + await act(async () => {}); + expect(chrome.storage.local.get).toHaveBeenCalled(); + }); return { row, cell, button: cell.querySelector('button')! }; } function deferred() { let resolve!: (value: T) => void; - const promise = new Promise((done) => { + let reject!: (error: Error) => void; + const promise = new Promise((done, fail) => { resolve = done; + reject = fail; }); - return { promise, resolve }; + return { promise, resolve, reject }; } async function click(button: HTMLElement) { await act(async () => button.click()); @@ -146,7 +165,7 @@ const additionalAnalysis = parseAnalysisResponse( buildAnalysisInput(facts, presentation) ); -function traceFor(runId: string, resultId: string | null): SummaryTrace { +async function traceFor(runId: string, resultId: string | null): Promise { return { version: 1, runId, @@ -157,7 +176,7 @@ function traceFor(runId: string, resultId: string | null): SummaryTrace { provider: 'openai', model: 'fixture', extractionMode: 'full', - fingerprint: buildAnalysisFingerprint({ + fingerprint: await buildAnalysisFingerprint({ provider: 'openai', model: 'fixture', extractionMode: 'full', @@ -175,7 +194,10 @@ function traceFor(runId: string, resultId: string | null): SummaryTrace { } async function changeSettings(update: Record) { Object.assign(settings, update); - await act(async () => listeners.forEach((listener) => listener(update, 'sync'))); + const changes = Object.fromEntries( + Object.entries(update).map(([key, newValue]) => [key, { newValue }]) + ); + await act(async () => listeners.forEach((listener) => listener(changes, 'sync'))); } beforeEach(() => { @@ -187,6 +209,7 @@ beforeEach(() => { }; stored = {}; listeners.clear(); + messages.clear(); sendMessage.mockReset(); save .mockReset() @@ -200,9 +223,16 @@ beforeEach(() => { vi.stubGlobal('crypto', webcrypto); vi.stubGlobal('chrome', { storage: { - sync: { get: (_keys: string[], callback: (value: unknown) => void) => callback(settings) }, + sync: { + get: (keys: string[], callback: (value: unknown) => void) => + callback( + Object.fromEntries( + keys.filter((key) => key in settings).map((key) => [key, settings[key]]) + ) + ), + }, local: { - get: async (keys: string | string[], callback?: (data: unknown) => void) => { + get: vi.fn(async (keys: string | string[], callback?: (data: unknown) => void) => { const data = Object.fromEntries( [keys] .flat() @@ -211,7 +241,7 @@ beforeEach(() => { ); callback?.(data); return data; - }, + }), set: save, remove, }, @@ -222,13 +252,24 @@ beforeEach(() => { listeners.delete(listener), }, }, - runtime: { sendMessage }, + runtime: { + sendMessage, + onMessage: { + addListener: (listener: (request: { action: string; enabled: boolean }) => void) => + messages.add(listener), + removeListener: (listener: (request: { action: string; enabled: boolean }) => void) => + messages.delete(listener), + }, + }, }); }); afterEach(async () => { await act(async () => root?.unmount()); + await act(async () => stopContentScript?.()); + stopContentScript = undefined; container?.remove(); expect(listeners.size).toBe(0); + expect(messages.size).toBe(0); vi.unstubAllGlobals(); }); @@ -269,33 +310,43 @@ describe('実Reactでの要約・保存・後続処理の境界', () => { expect(stored[`analysisCacheV3:${response.resultId}`]).toEqual(fresh); }); - it('遅い保存読込が開始済みの追加分析を解除・置換しない', async () => { - const response = await responseFor(); - const read = deferred>(), - analysis = deferred(); - sendMessage.mockResolvedValueOnce(response).mockReturnValueOnce(analysis.promise); - const hook = await mount(); - await act(async () => hook().summarize()); - await act(async () => hook().reset()); - const originalGet = chrome.storage.local.get; - chrome.storage.local.get = ((keys: string | string[], callback?: (data: unknown) => void) => - Array.isArray(keys) - ? read.promise - : originalGet(keys, callback!)) as typeof chrome.storage.local.get; - let restoring: Promise; - await act(async () => { - restoring = hook().showCached(); - }); - await act(async () => hook().analyze()); - expect(hook().analysis.loading).toBe(true); - await act(async () => { - read.resolve({}); - await restoring; - }); - expect(hook().analysis.loading).toBe(true); - await act(async () => analysis.resolve({ analysis: additionalAnalysis })); - expect(hook().analysis.data).toEqual(additionalAnalysis); - }); + it.each(['success', 'failure'])( + '遅い保存読込の%sが開始済みの追加分析を解除・置換しない', + async (outcome) => { + const response = await responseFor(); + const read = deferred>(), + analysis = deferred(); + sendMessage.mockResolvedValueOnce(response).mockReturnValueOnce(analysis.promise); + const hook = await mount(); + await act(async () => hook().summarize()); + await act(async () => hook().reset()); + const originalGet = chrome.storage.local.get; + chrome.storage.local.get = ((keys: string | string[], callback?: (data: unknown) => void) => + Array.isArray(keys) + ? read.promise + : originalGet(keys, callback!)) as typeof chrome.storage.local.get; + let restoring: Promise; + await act(async () => { + restoring = hook().showCached(); + }); + // Source/result-ID verification must finish before analysis can be started; + // only the independent stage-cache read is deliberately left pending. + await vi.waitFor(async () => { + await act(async () => {}); + expect(hook().result?.resultId).toBe(response.resultId); + }); + await act(async () => hook().analyze()); + expect(hook().analysis.loading).toBe(true); + await act(async () => { + if (outcome === 'success') read.resolve({}); + else read.reject(new Error('storage read failed')); + await restoring; + }); + expect(hook().analysis.loading).toBe(true); + await act(async () => analysis.resolve({ analysis: additionalAnalysis })); + expect(hook().analysis.data).toEqual(additionalAnalysis); + } + ); it('smart→全文→通常へ切り替え、保存後は通信せず復元する', async () => { settings.extractionMode = 'smart'; @@ -305,13 +356,13 @@ describe('実Reactでの要約・保存・後続処理の境界', () => { const hook = await mount(); await act(async () => hook().summarize('full')); expect(hook().result).toMatchObject({ error: null, diagnosticRunId: 'full-run' }); - expect(stored[keyFor('full')]).toBeDefined(); + expect(stored[await keyFor('full')]).toBeDefined(); const displayed = hook().result; await changeSettings({ experimentalScoring: false }); expect(hook().result).toBe(displayed); await act(async () => hook().summarize()); expect(hook().result).toMatchObject({ error: null, diagnosticRunId: 'smart-run' }); - expect(stored[keyFor('smart')]).toBeDefined(); + expect(stored[await keyFor('smart')]).toBeDefined(); await act(async () => hook().reset()); await act(async () => hook().showCached()); expect(hook().result).toMatchObject({ @@ -368,6 +419,63 @@ describe('実Reactでの要約・保存・後続処理の境界', () => { expect(save).not.toHaveBeenCalled(); }); + it('customUrlだけの変更で保存要約・分析を外し、遅い旧分析を保存しない', async () => { + const endpoint = { provider: 'custom', customUrl: 'https://api.example.com/v1/chat?route=a' }; + Object.assign(settings, endpoint); + const response = await responseFor('full', endpoint); + const oldKey = + 'summaryCacheV2:' + buildSummaryCacheKey(pdfUrl, response.metadata.analysisFingerprint); + stored[oldKey] = response; + stored[`analysisCacheV3:${response.resultId}`] = additionalAnalysis; + const hook = await mount(); + await act(async () => hook().showCached()); + expect(hook().result?.resultId).toBe(response.resultId); + expect(hook().analysis.data).toEqual(additionalAnalysis); + // The event is enough even when a follow-up storage read would remain pending. + const pendingSettingsRead = vi.fn(); + chrome.storage.sync.get = pendingSettingsRead; + await changeSettings({ customUrl: 'HTTPS://API.EXAMPLE.COM:443/x/../v1/chat?route=a#ignored' }); + expect(hook().result?.resultId).toBe(response.resultId); + expect(hook().analysis.data).toEqual(additionalAnalysis); + const oldAnalysis = deferred(); + sendMessage.mockReturnValueOnce(oldAnalysis.promise); + await act(async () => hook().analyze()); + expect(hook().analysis.loading).toBe(true); + await changeSettings({ customUrl: 'https://api.example.com/v1/chat?route=b' }); + expect(hook().result).toBeNull(); + expect(hook().analysis).toEqual({ loading: false, data: null, error: null }); + expect(hook().hasCached).toBe(false); + await act(async () => + oldAnalysis.resolve({ + analysis: { + ...additionalAnalysis, + issues: [{ ...additionalAnalysis.issues[0], title: '旧APIの遅い分析' }], + }, + }) + ); + expect(save).not.toHaveBeenCalled(); + expect(hook().analysis.data).toBeNull(); + await vi.waitFor(async () => { + await act(async () => {}); + expect(hook().cacheKey).not.toBeNull(); + }); + expect('summaryCacheV2:' + hook().cacheKey).not.toBe(oldKey); + await act(async () => hook().showCached()); + expect(hook().result).toBeNull(); + expect(hook().hasCached).toBe(false); + const fresh = await responseFor('full', { + ...endpoint, + customUrl: settings.customUrl as string, + }); + expect(fresh.resultId).not.toBe(response.resultId); + sendMessage.mockResolvedValueOnce(fresh); + await act(async () => hook().summarize()); + expect(hook().result?.resultId).toBe(fresh.resultId); + expect(hook().analysis.data).toBeNull(); + expect(stored[`analysisCacheV3:${response.resultId}`]).toEqual(additionalAnalysis); + expect(pendingSettingsRead).not.toHaveBeenCalled(); + }); + it('算出不能スコアを保存せず、要約を保ち、明示再試行を許す', async () => { sendMessage .mockResolvedValueOnce(await responseFor()) @@ -388,7 +496,7 @@ describe('実Reactでの要約・保存・後続処理の境界', () => { it('保存済み算出不能スコアを削除し、再採点可能な要約を復元する', async () => { const response = await responseFor(); - stored[keyFor('full')] = response; + stored[await keyFor('full')] = response; stored[`scoreCacheV4:${response.resultId}`] = { value: null, unverified: ['過去の失敗'] }; const hook = await mount(); await act(async () => hook().showCached()); @@ -438,7 +546,7 @@ describe('実Reactでの要約・保存・後続処理の境界', () => { }, ], }; - stored[keyFor('full', url)] = response; + stored[await keyFor('full', url)] = response; stored[`scoreCacheV4:${response.resultId}`] = score; const hook = await mount(url); await act(async () => hook().showCached()); @@ -448,7 +556,14 @@ describe('実Reactでの要約・保存・後続処理の境界', () => { ? { loading: false, data: score, error: null } : { loading: false, data: null, error: '保存された採点の形式・確定事実との対応が不正です' } ); + await act(async () => hook().startScore()); expect(sendMessage).not.toHaveBeenCalled(); + if (!valid) { + sendMessage.mockResolvedValue({ error: '明示的な再採点' }); + await act(async () => hook().retryScore()); + expect(sendMessage).toHaveBeenCalledOnce(); + expect(sendMessage).toHaveBeenCalledWith(expect.objectContaining({ action: 'score' })); + } }); }); @@ -586,7 +701,7 @@ describe('実Reactの要約行アクション配置', () => { it('診断ポータルの手動コピーと状態を後続処理で保ち、別実行と閉じる前の遅い完了を表示しない', async () => { const response = await responseFor(); - const trace = traceFor(response.diagnosticRunId, response.resultId); + const trace = await traceFor(response.diagnosticRunId, response.resultId); stored[SUMMARY_TRACE_KEY] = trace; const score = deferred(); const analysis = deferred(); @@ -667,7 +782,7 @@ describe('実Reactの要約行アクション配置', () => { it('エラー行でも閉じると診断を用意し、エラーの隣の全文再要約で回復する', async () => { settings.extractionMode = 'smart'; settings.experimentalScoring = false; - const trace = traceFor('failed-run', null); + const trace = await traceFor('failed-run', null); stored[SUMMARY_TRACE_KEY] = trace; const response = await responseFor('full'); const retry = deferred(); @@ -716,3 +831,308 @@ describe('実Reactの要約行アクション配置', () => { expect(sendMessage).toHaveBeenCalledTimes(2); }); }); + +// Storage failures and lifecycle races belong here: real state/effects/DOM, +// with only Chrome messaging and storage substituted at the boundary. +describe('保存失敗と一覧ライフサイクルの回復', () => { + it('後続キャッシュの読込失敗では開き直しても自動課金せず、本文を保って明示再試行できる', async () => { + const response = await responseFor(); + stored[await keyFor('full')] = response; + const get = vi.mocked(chrome.storage.local.get).getMockImplementation()!; + vi.mocked(chrome.storage.local.get).mockImplementation((keys, callback) => + Array.isArray(keys) ? Promise.reject(new Error('storage read failed')) : get(keys, callback) + ); + sendMessage.mockImplementation(async ({ action }) => + action === 'score' ? { error: '固定応答の採点失敗' } : { analysis: additionalAnalysis } + ); + const { row, button } = await mountButton(); + await vi.waitFor(() => expect(button.textContent).toBe('表示')); + await click(button); + const summaryRow = await summaryRowFor(row); + await vi.waitFor(() => + expect(summaryRow.querySelector('[data-persistence-warning]')?.textContent).toContain( + '保存された採点・追加分析を読み込めませんでした' + ) + ); + expect(sendMessage).not.toHaveBeenCalled(); + await click(button); + await click(button); + const reopened = await summaryRowFor(row); + await vi.waitFor(() => + expect(reopened.querySelector('#score-result')?.textContent).toContain( + '保存された採点を読み込めませんでした' + ) + ); + await changeSettings({ experimentalScoring: false }); + await changeSettings({ experimentalScoring: true }); + expect(sendMessage).not.toHaveBeenCalled(); + const body = reopened.querySelector('#score-result')!.previousElementSibling!; + const bodyHtml = body.innerHTML; + await click(reopened.querySelector('#retry-score-btn')!); + expect(sendMessage).toHaveBeenCalledOnce(); + expect(sendMessage).toHaveBeenLastCalledWith(expect.objectContaining({ action: 'score' })); + const analyze = reopened.querySelector('#analyze-btn')!; + expect(analyze.textContent).toBe('再試行'); + await click(analyze); + expect(sendMessage).toHaveBeenCalledTimes(2); + expect(sendMessage).toHaveBeenLastCalledWith(expect.objectContaining({ action: 'analyze' })); + expect(reopened.querySelector('#analysis-result')?.textContent).toContain( + additionalAnalysis.issues[0].conclusion + ); + expect(reopened.querySelector('#score-result')!.previousElementSibling).toBe(body); + expect(body.innerHTML).toBe(bodyHtml); + expect(button.textContent).toBe('閉じる'); + }); + + it('保存失敗を生成失敗にせず、要約・追加分析を表示して保存警告を分ける', async () => { + settings.experimentalScoring = false; + const response = await responseFor(); + const saveSummary = deferred(); + save.mockReturnValueOnce(saveSummary.promise).mockRejectedValue(new Error('QUOTA_BYTES')); + sendMessage + .mockResolvedValueOnce({ + ...response, + metadata: { ...response.metadata, persistenceWarning: '診断を保存できませんでした' }, + }) + .mockResolvedValueOnce({ analysis: additionalAnalysis }); + const { row, button } = await mountButton(); + await click(button); + const summaryRow = await summaryRowFor(row); + const body = summaryRow.querySelector('#score-result')!.previousElementSibling!; + const analysisButton = summaryRow.querySelector('#analyze-btn')!; + // A completed result is usable before its independent cache write finishes. + await click(analysisButton); + expect(summaryRow.querySelector('#analysis-result')?.textContent).toContain( + additionalAnalysis.issues[0].conclusion + ); + expect(summaryRow.querySelector('#analysis-result')?.textContent).toContain( + '追加分析を保存できませんでした' + ); + expect(summaryRow.querySelector('[role="alert"]')).toBeNull(); + // The late cache rejection must not replace the completed body or analysis. + await act(async () => saveSummary.reject(new Error('QUOTA_BYTES'))); + expect(summaryRow.querySelector('[data-persistence-warning]')?.textContent).toContain( + '診断を保存できませんでした' + ); + expect(summaryRow.querySelector('[data-persistence-warning]')?.textContent).toContain( + '要約を保存できませんでした' + ); + expect(summaryRow.querySelector('#score-result')!.previousElementSibling).toBe(body); + expect(button.textContent).toBe('閉じる'); + await click(button); + expect(button.textContent).toBe('要約'); + expect(remove).not.toHaveBeenCalled(); + }); + + it('削除通知で表示を要約へ戻し、通知前のキャッシュミスも一度のクリックで回復する', async () => { + settings.experimentalScoring = false; + const response = await responseFor(); + const key = await keyFor('full'); + stored[key] = response; + sendMessage.mockResolvedValue(response); + const { row, button } = await mountButton(); + await vi.waitFor(() => expect(button.textContent).toBe('表示')); + delete stored[key]; + await act(async () => + listeners.forEach((listener) => listener({ [key]: { oldValue: response } }, 'local')) + ); + expect(button.textContent).toBe('要約'); + stored[key] = response; + await act(async () => + listeners.forEach((listener) => listener({ [key]: { newValue: response } }, 'local')) + ); + await vi.waitFor(() => expect(button.textContent).toBe('表示')); + delete stored[key]; + await act(async () => { + button.click(); + button.click(); + }); + await summaryRowFor(row); + expect(sendMessage).toHaveBeenCalledTimes(1); + expect(button.textContent).toBe('閉じる'); + await click(button); + const get = chrome.storage.local.get; + chrome.storage.local.get = vi + .fn() + .mockRejectedValueOnce(new Error('storage read failed')) + .mockImplementation(get); + await click(button); + await summaryRowFor(row); + expect(sendMessage).toHaveBeenCalledTimes(2); + expect(button.textContent).toBe('閉じる'); + }); + + it('削除前に開始した遅いキャッシュ読込を表示にも存在判定にも戻さない', async () => { + const response = await responseFor(); + const key = await keyFor('full'); + stored[key] = response; + const availability = deferred>(); + const first = deferred>(); + const originalGet = chrome.storage.local.get; + chrome.storage.local.get = vi + .fn() + .mockReturnValueOnce(availability.promise) + .mockReturnValueOnce(first.promise) + .mockImplementation(originalGet); + const hook = await mount(); + let showing!: Promise; + await act(async () => { + showing = hook().showCached(); + }); + delete stored[key]; + await act(async () => + listeners.forEach((listener) => listener({ [key]: { oldValue: response } }, 'local')) + ); + await act(async () => { + first.resolve({ [key]: response }); + availability.resolve({ [key]: response }); + await showing; + }); + expect(await showing).toBe(false); + expect(hook().hasCached).toBe(false); + expect(hook().result).toBeNull(); + expect(sendMessage).not.toHaveBeenCalled(); + + // Commit notification, deletion, then write completion: deletion wins. + const write = deferred(); + save.mockImplementation((entries) => { + Object.assign(stored, entries); + listeners.forEach((listener) => listener({ [key]: { newValue: entries[key] } }, 'local')); + return write.promise; + }); + sendMessage.mockResolvedValue(response); + let generating!: Promise; + await act(async () => { + generating = hook().summarize(); + }); + await vi.waitFor(async () => { + await act(async () => {}); + expect(save).toHaveBeenCalledOnce(); + }); + delete stored[key]; + await act(async () => + listeners.forEach((listener) => listener({ [key]: { oldValue: response } }, 'local')) + ); + await act(async () => { + write.resolve(); + await generating; + }); + expect(hook().hasCached).toBe(false); + expect(hook().result?.error).toBeNull(); + }); + + function frameFixture() { + const frame = document.createElement('iframe'); + frame.id = 'main_list'; + container.append(frame); + const doc = frame.contentDocument!; + doc.body.innerHTML = `
表題
+
15:001234株式会社テスト開示
`; + return { frame, doc, row: doc.querySelector('#main-list-table tr')! }; + } + async function toggle(enabled: boolean) { + await act(async () => + messages.forEach((listener) => listener({ action: 'toggleExtension', enabled })) + ); + } + async function waitForSubscriptions(expected: number) { + // Root insertion precedes the asynchronous settings/cache effects. + await vi.waitFor(async () => { + await act(async () => {}); + expect(listeners.size).toBe(expected); + }); + } + async function mountLifecycle() { + container = document.createElement('div'); + document.body.append(container); + const fixture = frameFixture(); + await act(async () => { + stopContentScript = startContentScript(); + }); + await vi.waitFor(async () => { + await act(async () => {}); + expect(chrome.storage.local.get).toHaveBeenCalled(); + }); + return fixture; + } + + it('OFFでReactと購読を破棄し、遅い応答を保存・再表示せず、ONで一組だけ作り直す', async () => { + settings.experimentalScoring = false; + const pending = deferred(); + sendMessage.mockReturnValueOnce(pending.promise).mockResolvedValue(await responseFor()); + const { frame, doc, row } = await mountLifecycle(); + const subscriptions = listeners.size; + await act(async () => { + window.dispatchEvent(new PageTransitionEvent('pagehide', { persisted: true })); + window.dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); + }); + expect(listeners.size).toBe(subscriptions); + expect(messages.size).toBe(1); + await click(row.querySelector('button')!); + await toggle(false); + expect( + doc.querySelectorAll( + '.tdnet-digest-button-cell, .tdnet-digest-summary-row, .tdnet-digest-header' + ) + ).toHaveLength(0); + expect(listeners.size).toBe(1); // Only the lifecycle's enable/disable control remains. + expect(row.lastElementChild?.className).toBe('oddnew-R kjTitle'); + expect(doc.querySelector('#list-head td')?.className).toBe('header-R'); + await act(async () => pending.resolve(await responseFor())); + await act(async () => frame.dispatchEvent(new Event('load'))); + expect(doc.querySelector('.tdnet-digest-summary-row')).toBeNull(); + expect(save).not.toHaveBeenCalled(); + await toggle(true); + await toggle(true); + expect(doc.querySelectorAll('.tdnet-digest-button-cell')).toHaveLength(1); + await waitForSubscriptions(subscriptions); + await click(row.querySelector('button')!); + await summaryRowFor(row); + await toggle(false); + await toggle(true); + await waitForSubscriptions(subscriptions); + expect(doc.querySelector('.tdnet-digest-summary-row')).toBeNull(); + }); + + it('同じ行のPDF変更・行削除・iframe再読込と置換で旧rootを残さない', async () => { + settings.experimentalScoring = false; + const stale = deferred(); + sendMessage.mockReturnValueOnce(stale.promise); + const { frame, doc, row } = await mountLifecycle(); + const subscriptions = listeners.size; + const oldButton = row.querySelector('button')!; + await click(oldButton); + await act(async () => row.querySelector('a')!.setAttribute('href', 'replacement.pdf')); + await waitForSubscriptions(subscriptions); + expect(row.querySelector('button')).not.toBe(oldButton); + expect(row.querySelector('button')?.textContent).toBe('要約'); + await act(async () => stale.resolve(await responseFor())); + expect(row.nextElementSibling).toBeNull(); + expect(save).not.toHaveBeenCalled(); + const reloadButton = row.querySelector('button'); + await act(async () => frame.dispatchEvent(new Event('load'))); + await waitForSubscriptions(subscriptions); + expect(row.querySelector('button')).not.toBe(reloadButton); + await act(async () => row.querySelector('a')!.setAttribute('href', pdfUrl)); + await waitForSubscriptions(subscriptions); + sendMessage.mockResolvedValue(await responseFor()); + await click(row.querySelector('button')!); + const displayed = await summaryRowFor(row); + await act(async () => row.remove()); + expect(displayed.isConnected).toBe(false); + expect(doc.querySelector('.tdnet-digest-summary-row')).toBeNull(); + expect(listeners.size).toBe(1); + expect(row.querySelector('.tdnet-digest-button-cell')).toBeNull(); + let replacement!: ReturnType; + await act(async () => { + frame.remove(); + replacement = frameFixture(); + }); + await waitForSubscriptions(subscriptions); + expect(doc.querySelector('.tdnet-digest-header')).toBeNull(); + expect(replacement.doc.querySelectorAll('.tdnet-digest-button-cell')).toHaveLength(1); + await act(async () => frame.dispatchEvent(new Event('load'))); + expect(doc.querySelector('.tdnet-digest-header')).toBeNull(); + expect(replacement.doc.querySelectorAll('.tdnet-digest-button-cell')).toHaveLength(1); + }); +}); diff --git a/src/content/hooks/useSummarize.ts b/src/content/hooks/useSummarize.ts index 031eef2..04adc35 100644 --- a/src/content/hooks/useSummarize.ts +++ b/src/content/hooks/useSummarize.ts @@ -1,7 +1,12 @@ import { useState, useEffect, useCallback, useRef } from 'react'; import { summaryResultId } from '@/lib/summary-result-id'; import { normalizeTdnetPdfUrl } from '@/lib/tdnet-url'; -import { buildAnalysisFingerprint, buildSummaryCacheKey } from '@/lib/analysis-version'; +import { + buildAnalysisFingerprint, + buildSummaryCacheKey, + type AnalysisFingerprintSettings, +} from '@/lib/analysis-version'; +import { configuredApiUrl } from '@/lib/llm-endpoint'; import { FACT_SCHEMA_VERSION, renderFacts } from '@/lib/fact-summary'; import { validateSavedFacts, validateSavedScore } from '@/lib/fact-cache'; import type { FactSummary } from '@/lib/fact-summary'; @@ -34,6 +39,7 @@ export interface Stage { loading: boolean; data: T | null; error: string | null; + persistenceWarning?: string; } const emptyStage = (): Stage => ({ loading: false, data: null, error: null }); const SUMMARY_PREFIX = 'summaryCacheV2:'; @@ -69,77 +75,170 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { const [analysis, setAnalysis] = useState>(emptyStage()); const [scoringEnabled, setScoringEnabled] = useState(false); const [hasCached, setHasCached] = useState(false); + const [persistenceWarning, setPersistenceWarning] = useState(null); const [stagesReady, setStagesReady] = useState(false); const [cacheKey, setCacheKey] = useState(null); const keyRef = useRef(null); const configuredKeyRef = useRef(null); - const settingsRef = useRef<{ - provider: string; - model: string; - extractionMode: ExtractionMode; - } | null>(null); + const settingsRef = useRef< + | (AnalysisFingerprintSettings & { + fingerprints: Record; + }) + | null + >(null); const idRef = useRef(null); const scoreStarted = useRef(null); const runRef = useRef(0); const stageRequestRef = useRef({ score: 0, analyze: 0 }); + const mountedRef = useRef(false); + const cacheRevisionRef = useRef(0); useEffect(() => { + mountedRef.current = true; let active = true; - const keys = ['provider', 'model', 'extractionMode', 'experimentalScoring']; - const refresh = () => - chrome.storage.sync.get(keys, (settings) => { - if (!active) return; - const mode = settings.extractionMode ?? 'full'; - const currentSettings = { - provider: settings.provider ?? 'openai', - model: settings.model ?? 'gpt-4o', - extractionMode: mode as ExtractionMode, + let settingsRequest = 0; + let configuredSignature: string | null = null; + let storedSettings: Record | null = null; + const keys = ['provider', 'model', 'customUrl', 'extractionMode', 'experimentalScoring']; + const invalidate = () => { + configuredKeyRef.current = null; + keyRef.current = null; + settingsRef.current = null; + idRef.current = null; + scoreStarted.current = null; + runRef.current++; + setLoading(false); + setResult(null); + setScore(emptyStage()); + setAnalysis(emptyStage()); + setHasCached(false); + setPersistenceWarning(null); + setStagesReady(false); + setCacheKey(null); + }; + const applySettings = async (settings: Record, request: number) => { + if (!active || request !== settingsRequest) return; + storedSettings = { ...settings }; + try { + const provider = settings.provider ?? 'openai'; + const model = settings.model ?? 'gpt-4o'; + const customUrl = settings.customUrl ?? ''; + const extractionMode = settings.extractionMode ?? 'full'; + if ( + typeof provider !== 'string' || + typeof model !== 'string' || + typeof customUrl !== 'string' || + (extractionMode !== 'full' && extractionMode !== 'smart') + ) + throw new Error('設定値が不正です'); + const currentSettings: AnalysisFingerprintSettings = { + provider, + model, + baseUrl: configuredApiUrl({ provider, customUrl }), + extractionMode, }; - const next = buildSummaryCacheKey(pdfUrl, buildAnalysisFingerprint(currentSettings)); - settingsRef.current = currentSettings; + const signature = JSON.stringify([ + currentSettings.provider, + currentSettings.model, + currentSettings.extractionMode, + currentSettings.baseUrl, + ]); + if (signature !== configuredSignature) { + // Invalidate before hashing so old responses cannot win the async digest race. + configuredSignature = signature; + invalidate(); + } + setScoringEnabled(settings.experimentalScoring === true); + const [full, smart] = await Promise.all( + (['full', 'smart'] as const).map((extractionMode) => + buildAnalysisFingerprint({ ...currentSettings, extractionMode }) + ) + ); + const fingerprints = { full, smart }; + const next = buildSummaryCacheKey(pdfUrl, fingerprints[currentSettings.extractionMode]); + if (!active || request !== settingsRequest) return; + settingsRef.current = { ...currentSettings, fingerprints }; if (next !== configuredKeyRef.current) { configuredKeyRef.current = next; keyRef.current = next; - idRef.current = null; - scoreStarted.current = null; - runRef.current++; - setLoading(false); - setResult(null); - setScore(emptyStage()); - setAnalysis(emptyStage()); - setHasCached(false); - setStagesReady(false); setCacheKey(next); } - setScoringEnabled(settings.experimentalScoring === true); - }); + } catch { + if (!active || request !== settingsRequest) return; + configuredSignature = null; + invalidate(); + } + }; + const refresh = () => { + const request = ++settingsRequest; + chrome.storage.sync.get(keys, (settings) => void applySettings(settings, request)); + }; refresh(); const changed = (changes: Record, area: string) => { - if (area === 'sync' && keys.some((key) => key in changes)) refresh(); + if (area !== 'sync' || !keys.some((key) => key in changes)) return; + if (!storedSettings) return refresh(); + // StorageChange carries complete values. Apply them immediately so a delayed + // sync.get cannot let an old summary or follow-up response be persisted. + const next = { ...storedSettings }; + for (const key of keys) if (key in changes) next[key] = changes[key].newValue; + void applySettings(next, ++settingsRequest); }; chrome.storage.onChanged.addListener(changed); + const pendingRun = runRef; return () => { active = false; + mountedRef.current = false; + pendingRun.current++; + keyRef.current = null; + configuredKeyRef.current = null; + settingsRef.current = null; chrome.storage.onChanged.removeListener(changed); }; }, [pdfUrl]); useEffect(() => { if (!cacheKey) return; - chrome.storage.local.get(SUMMARY_PREFIX + cacheKey, async (data) => { - if (cacheKey !== keyRef.current) return; - const entry = data[SUMMARY_PREFIX + cacheKey] 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 (cacheKey === keyRef.current) setHasCached(valid); - }); + let active = true; + let request = 0; + const storageKey = SUMMARY_PREFIX + cacheKey; + const refresh = async () => { + const read = ++request; + const revision = cacheRevisionRef.current; + const current = () => + active && + read === request && + revision === cacheRevisionRef.current && + 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); + } catch { + if (current()) setHasCached(false); + } + }; + const changed = (changes: Record, area: string) => { + if (area !== 'local' || !(storageKey in changes)) return; + cacheRevisionRef.current++; + // A deleted entry must not remain clickable while an older read is in flight. + setHasCached(false); + void refresh(); + }; + chrome.storage.onChanged.addListener(changed); + void refresh(); + return () => { + active = false; + chrome.storage.onChanged.removeListener(changed); + }; }, [cacheKey, pdfUrl]); const restoreStages = useCallback( @@ -154,11 +253,38 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { const current = () => idRef.current === id && runRef.current === epoch; const canRestore = (stage: 'score' | 'analyze') => current() && stageRequestRef.current[stage] === requests[stage]; - const data = await chrome.storage.local.get([SCORE_PREFIX + id, ANALYSIS_PREFIX + id]); + let data: Record; + try { + data = await chrome.storage.local.get([SCORE_PREFIX + id, ANALYSIS_PREFIX + id]); + } catch { + if (!current()) return; + setPersistenceWarning((warning) => + [warning, '保存された採点・追加分析を読み込めませんでした。再実行できます'] + .filter(Boolean) + .join(' / ') + ); + // An unread cache is not a cache miss. Require an explicit retry, and + // do not replace a follow-up the user started while this read was pending. + if (canRestore('score')) + setScore({ + loading: false, + data: null, + error: '保存された採点を読み込めませんでした', + }); + if (canRestore('analyze')) + setAnalysis({ + loading: false, + data: null, + error: '保存された追加分析を読み込めませんでした', + }); + setStagesReady(true); + return; + } if (!current()) return; const cachedScore = data[SCORE_PREFIX + id] as ExperimentalScore | undefined; if (cachedScore?.value === null && canRestore('score')) { - await chrome.storage.local.remove(SCORE_PREFIX + id); + // Failure to clean up a legacy failed score must not hide a valid summary. + await chrome.storage.local.remove(SCORE_PREFIX + id).catch(() => {}); if (!current()) return; } if (canRestore('score')) { @@ -201,12 +327,30 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { ); const showCached = useCallback(async () => { + if (!mountedRef.current) return true; const run = ++runRef.current; const key = keyRef.current; - if (!key) return; + if (!key) return true; + const revision = cacheRevisionRef.current; setStagesReady(false); - const data = await chrome.storage.local.get(SUMMARY_PREFIX + key); - if (key !== keyRef.current || run !== runRef.current) return; + const invalidate = () => { + setHasCached(false); + idRef.current = null; + scoreStarted.current = null; + setResult(null); + setScore(emptyStage()); + setAnalysis(emptyStage()); + setPersistenceWarning(null); + }; + let data: Record; + try { + data = await chrome.storage.local.get(SUMMARY_PREFIX + key); + } catch { + if (key !== keyRef.current || run !== runRef.current) return true; + invalidate(); + return false; + } + if (key !== keyRef.current || run !== runRef.current) return true; const entry = data[SUMMARY_PREFIX + key] as CachedSummary | undefined; const valid = isCachedSummary(entry, key, pdfUrl) && @@ -217,7 +361,11 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { entry.metadata.documentHash!, entry.presentation )) === entry.resultId; - if (key !== keyRef.current || run !== runRef.current) return; + if (key !== keyRef.current || run !== runRef.current) return true; + if (revision !== cacheRevisionRef.current || !valid) { + invalidate(); + if (revision !== cacheRevisionRef.current || entry === undefined) return false; + } if (!valid) { if (entry !== undefined) setResult({ @@ -229,11 +377,11 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { diagnosticRunId: null, error: '保存された現行要約の形式・原数量・設定が不正です。再要約してください。', }); - return; + return true; } - if (key !== keyRef.current || run !== runRef.current) return; - if (!entry) return; + if (!entry) return false; idRef.current = entry.resultId; + setPersistenceWarning(entry.metadata.persistenceWarning ?? null); setResult({ summary: entry.summary, metadata: entry.metadata, @@ -249,16 +397,19 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { entry.metadata.documentHash!, entry.presentation ); + return true; }, [restoreStages, pdfUrl]); const summarize = useCallback( async (forceExtractionMode?: ExtractionMode) => { + if (!mountedRef.current) return; const run = ++runRef.current; setLoading(true); setResult(null); setScore(emptyStage()); setAnalysis(emptyStage()); setStagesReady(false); + setPersistenceWarning(null); scoreStarted.current = null; idRef.current = null; let diagnosticRunId: string | null = null; @@ -268,10 +419,7 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { if (!settings) throw new Error('設定の読み込みが完了していません'); const expectedKey = buildSummaryCacheKey( pdfUrl, - buildAnalysisFingerprint({ - ...settings, - extractionMode: forceExtractionMode ?? settings.extractionMode, - }) + settings.fingerprints[forceExtractionMode ?? settings.extractionMode] ); const response = await chrome.runtime.sendMessage({ action: 'summarize', @@ -321,6 +469,8 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { error: null, }); setStagesReady(true); + const diagnosticWarning = response.metadata.persistenceWarning ?? null; + setPersistenceWarning(diagnosticWarning); const entry: CachedSummary = { summary: response.summary, facts: response.facts, @@ -332,8 +482,28 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { code, cachedAt: Date.now(), }; - await chrome.storage.local.set({ [SUMMARY_PREFIX + key]: entry }); - if (key === keyRef.current) setHasCached(true); + const cacheRevision = cacheRevisionRef.current; + try { + await chrome.storage.local.set({ [SUMMARY_PREFIX + key]: entry }); + if ( + run === runRef.current && + key === keyRef.current && + cacheRevision === cacheRevisionRef.current + ) { + // A newer storage event owns availability. A late write completion + // must not undo a deletion or let a pre-write read replace this state. + cacheRevisionRef.current++; + setHasCached(true); + } + } catch { + if (run === runRef.current) { + setPersistenceWarning( + [diagnosticWarning, '要約を保存できませんでした。表示結果は利用できます'] + .filter(Boolean) + .join(' / ') + ); + } + } } catch (error) { if (run === runRef.current) setResult({ @@ -355,11 +525,18 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { const requestStage = useCallback( async (action: 'score' | 'analyze', current: SummaryResult) => { - if (!current.facts || !current.resultId || !current.metadata?.analysisFingerprint) return; + if ( + !mountedRef.current || + !current.facts || + !current.resultId || + !current.metadata?.analysisFingerprint + ) + return; const id = current.resultId; const epoch = runRef.current; const requestNumber = ++stageRequestRef.current[action]; const isCurrent = () => + mountedRef.current && idRef.current === id && runRef.current === epoch && stageRequestRef.current[action] === requestNumber; @@ -392,7 +569,17 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { if (action === 'score' && data.value === null) throw new Error(data.unverified?.join(' / ') || '採点の根拠を確認できません'); set({ loading: false, data, error: null }); - await chrome.storage.local.set({ [cache]: data }); + try { + await chrome.storage.local.set({ [cache]: data }); + } catch { + if (isCurrent()) + set({ + loading: false, + data, + error: null, + persistenceWarning: `${action === 'score' ? '採点' : '追加分析'}を保存できませんでした。表示結果は利用できます`, + }); + } } catch (error) { if (isCurrent()) set({ @@ -412,6 +599,7 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { !result?.resultId || score.loading || score.data || + score.error || scoreStarted.current === result.resultId ) return; @@ -431,6 +619,8 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { idRef.current = null; scoreStarted.current = null; setResult(null); + setLoading(false); + setPersistenceWarning(null); setScore(emptyStage()); setAnalysis(emptyStage()); setStagesReady(false); @@ -442,6 +632,7 @@ export function useSummarize({ pdfUrl, title, code, companyName }: Options) { analysis, scoringEnabled, hasCached, + persistenceWarning, cacheKey, summarize, showCached, diff --git a/src/content/hooks/useSummaryRow.ts b/src/content/hooks/useSummaryRow.ts index 6f1e5e4..98681c0 100644 --- a/src/content/hooks/useSummaryRow.ts +++ b/src/content/hooks/useSummaryRow.ts @@ -29,23 +29,21 @@ interface UseSummaryRowOptions { export function useSummaryRow({ row, iframeDoc, rowData }: UseSummaryRowOptions) { const stageHtml = useRef<{ row: Element; score: string; analysis: string } | null>(null); + const ownedRow = useRef(null); /** * 既存の要約行を削除 */ const removeSummaryRow = useCallback(() => { stageHtml.current = null; - const existingSummaryRow = row.nextElementSibling; - if (existingSummaryRow?.classList.contains('tdnet-digest-summary-row')) { - existingSummaryRow.remove(); - } - }, [row]); + ownedRow.current?.remove(); + ownedRow.current = null; + }, []); /** * 要約行が表示中かどうかを判定 */ const isSummaryRowVisible = useCallback((): boolean => { - const next = row.nextElementSibling; - return next?.classList.contains('tdnet-digest-summary-row') ?? false; + return ownedRow.current !== null && row.nextElementSibling === ownedRow.current; }, [row]); /** @@ -73,6 +71,7 @@ export function useSummaryRow({ row, iframeDoc, rowData }: UseSummaryRowOptions) // 要約行を作成 const summaryRow = iframeDoc.createElement('tr'); summaryRow.className = 'tdnet-digest-summary-row'; + ownedRow.current = summaryRow; const summaryCell = iframeDoc.createElement('td'); summaryCell.setAttribute('colspan', '8'); @@ -161,12 +160,18 @@ export function useSummaryRow({ row, iframeDoc, rowData }: UseSummaryRowOptions) ( score?: Stage, analysis?: Stage, - facts: VerifiedFact[] = [] + facts: VerifiedFact[] = [], + persistenceWarning: string | null = null ) => { - const summaryRow = row.nextElementSibling; - if (!summaryRow?.classList.contains('tdnet-digest-summary-row')) return; + const summaryRow = ownedRow.current; + if (!summaryRow) return; const scoreCell = summaryRow.querySelector('#score-result'); const analysisCell = summaryRow.querySelector('#analysis-result'); + const warning = summaryRow.querySelector('[data-persistence-warning]'); + if (warning) { + warning.textContent = persistenceWarning ?? ''; + warning.hidden = !persistenceWarning; + } const scoreHtml = buildScoreStageHtml(score); const analysisHtml = buildAnalysisStageHtml(analysis, facts, rowData.pdfUrl); const previous = stageHtml.current?.row === summaryRow ? stageHtml.current : null; @@ -187,7 +192,7 @@ export function useSummaryRow({ row, iframeDoc, rowData }: UseSummaryRowOptions) } stageHtml.current = { row: summaryRow, score: scoreHtml, analysis: analysisHtml }; }, - [row, rowData.pdfUrl] + [rowData.pdfUrl] ); return { removeSummaryRow, insertSummaryRow, updateStages, isSummaryRowVisible }; diff --git a/src/content/index.tsx b/src/content/index.tsx index 512523a..bf33119 100644 --- a/src/content/index.tsx +++ b/src/content/index.tsx @@ -1,181 +1,3 @@ -import React from 'react'; -import { createRoot } from 'react-dom/client'; -import SummaryButton from './SummaryButton'; -import { extractRowData } from './utils/rowDataExtractor'; -import { addHeaderColumn, getRowCellClass, updateLastCellClass } from './utils/tdnetDomHelper'; +import { startContentScript } from './contentLifecycle'; -// 拡張機能が有効かどうかのフラグ -let extensionEnabled = true; - -// iframe内のテーブルにボタンを追加 -function injectSummaryButtons() { - // 拡張機能が無効の場合はボタンを注入しない - if (!extensionEnabled) { - console.log('TDnet Digest: Extension is disabled'); - return; - } - - // iframeを取得 - const iframe = document.querySelector('#main_list') as HTMLIFrameElement; - if (!iframe) { - console.log('TDnet Digest: iframe not found'); - return; - } - - const iframeDoc = iframe.contentDocument || iframe.contentWindow?.document; - if (!iframeDoc) { - console.log('TDnet Digest: iframe document not accessible'); - return; - } - - // ヘッダーに「AI要約」列を追加 - addHeaderColumn(iframeDoc); - - // テーブル内の各行にボタンを追加 - const mainTable = iframeDoc.querySelector('#main-list-table'); - if (!mainTable) { - console.log('TDnet Digest: main table not found'); - return; - } - - const rows = mainTable.querySelectorAll('tbody > tr'); - rows.forEach((row) => { - // 既にボタンが追加されている、または要約結果の行はスキップ - if ( - row.querySelector('.tdnet-digest-button-cell') || - row.classList.contains('tdnet-digest-summary-row') - ) { - return; - } - - // 行データを抽出 - const rowData = extractRowData(row); - if (!rowData) return; - - // 既存の最後のセル(更新履歴)の -R を -M に変更 - updateLastCellClass(row); - - // ボタン用のtdを作成 - const buttonCell = iframeDoc.createElement('td'); - const cellClass = getRowCellClass(row); - buttonCell.className = `${cellClass} tdnet-digest-button-cell`; - buttonCell.setAttribute('nowrap', ''); - buttonCell.setAttribute('align', 'center'); - buttonCell.style.width = '80px'; - - // Reactコンポーネントをレンダリング - const container = iframeDoc.createElement('div'); - buttonCell.appendChild(container); - row.appendChild(buttonCell); - - const root = createRoot(container); - root.render( - - - - ); - }); - - console.log('TDnet Digest: Buttons injected successfully'); -} - -// 既存のボタンとヘッダーを削除 -function removeAllButtons() { - const iframe = document.querySelector('#main_list') as HTMLIFrameElement; - if (!iframe) return; - - const iframeDoc = iframe.contentDocument || iframe.contentWindow?.document; - if (!iframeDoc) return; - - // ヘッダーの「AI要約」列を削除 - const headerCell = iframeDoc.querySelector('.tdnet-digest-header'); - if (headerCell) { - headerCell.remove(); - } - - // 全てのボタンセルを削除 - const buttonCells = iframeDoc.querySelectorAll('.tdnet-digest-button-cell'); - buttonCells.forEach((cell) => cell.remove()); - - // 全ての要約行を削除 - const summaryRows = iframeDoc.querySelectorAll('.tdnet-digest-summary-row'); - summaryRows.forEach((row) => row.remove()); - - console.log('TDnet Digest: All buttons removed'); -} - -// MutationObserverの参照を保持 -let currentObserver: MutationObserver | null = null; - -// iframe内のコンテンツ監視を設定 -function setupIframeObserver(iframe: HTMLIFrameElement) { - // 既存のObserverがあれば解除 - if (currentObserver) { - currentObserver.disconnect(); - currentObserver = null; - } - - // 新しいObserverを設定 - const iframeDoc = iframe.contentDocument || iframe.contentWindow?.document; - if (iframeDoc && iframeDoc.body) { - currentObserver = new MutationObserver(() => { - injectSummaryButtons(); - }); - - currentObserver.observe(iframeDoc.body, { - childList: true, - subtree: true, - }); - console.log('TDnet Digest: Observer set up for iframe content'); - } -} - -// iframe読み込みを待機 -function waitForIframe() { - const iframe = document.querySelector('#main_list') as HTMLIFrameElement; - if (!iframe) { - setTimeout(waitForIframe, 100); - return; - } - - // iframeのloadイベントを監視(再読み込みごとに発火) - iframe.addEventListener('load', () => { - console.log('TDnet Digest: iframe loaded'); - injectSummaryButtons(); - setupIframeObserver(iframe); - }); - - // 初回の読み込みが完了している場合 - if (iframe.contentDocument?.readyState === 'complete') { - console.log('TDnet Digest: iframe already loaded'); - injectSummaryButtons(); - setupIframeObserver(iframe); - } -} - -// 初期設定を読み込み -chrome.storage.sync.get(['extensionEnabled'], (result) => { - extensionEnabled = result.extensionEnabled !== false; // デフォルトはtrue - - // ページ読み込み完了時に実行 - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', waitForIframe); - } else { - waitForIframe(); - } -}); - -// Popupからのメッセージを受信 -chrome.runtime.onMessage.addListener((request, _sender, _sendResponse) => { - if (request.action === 'toggleExtension') { - extensionEnabled = request.enabled; - - if (extensionEnabled) { - // 有効化された場合はボタンを追加 - injectSummaryButtons(); - } else { - // 無効化された場合はボタンを削除 - removeAllButtons(); - } - } -}); +startContentScript(); diff --git a/src/content/utils/summaryHtmlBuilder.ts b/src/content/utils/summaryHtmlBuilder.ts index 66d5c1f..2ffa00e 100644 --- a/src/content/utils/summaryHtmlBuilder.ts +++ b/src/content/utils/summaryHtmlBuilder.ts @@ -115,6 +115,7 @@ export function buildSummaryHtml( + ${buildMetadataHtml(metadata, 'warning')}
${parseSummaryMarkdown(summaryText, rowData.pdfUrl)}
${buildScoreStageHtml(score)}
@@ -157,7 +158,7 @@ export function buildScoreStageHtml(score?: Stage): string { : score?.error ? `採点失敗: ${escapeMetadataText(score.error)} ` : score?.data - ? buildScoreHtml(score.data) + ? buildScoreHtml(score.data) + buildPersistenceWarningHtml(score.persistenceWarning) : ''; } @@ -171,10 +172,17 @@ export function buildAnalysisStageHtml( : analysis?.error ? `

追加分析失敗: ${escapeMetadataText(analysis.error)}

` : analysis?.data - ? buildAnalysisHtml(analysis.data, facts, pdfUrl) + ? buildAnalysisHtml(analysis.data, facts, pdfUrl) + + buildPersistenceWarningHtml(analysis.persistenceWarning) : ''; } +function buildPersistenceWarningHtml(warning?: string): string { + return warning + ? `

${escapeMetadataText(warning)}

` + : ''; +} + function buildAnalysisHtml( analysis: AdditionalAnalysis, _facts: VerifiedFact[], diff --git a/src/lib/additional-analysis.test.ts b/src/lib/additional-analysis.test.ts index 47d3488..453f75e 100644 --- a/src/lib/additional-analysis.test.ts +++ b/src/lib/additional-analysis.test.ts @@ -81,8 +81,8 @@ describe('追加分析の根拠と論点の契約', () => { pages: [5], }); expect(built.evidence.find((e) => e.id === 'observation:observation')).toMatchObject({ - text: '営業利益: 100百万円', - sourceIds: value.sourceIds, + text: `営業利益: 確定事実 fact:${facts.facts[0].id} の同じ原数量への補足(区分: profit)`, + sourceIds: [...value.sourceIds, facts.facts[0].id], pages: [5], }); expect(JSON.stringify(built)).not.toContain('根拠なしの将来成長'); diff --git a/src/lib/analysis-calculations.ts b/src/lib/analysis-calculations.ts index ae922a6..c570ff8 100644 --- a/src/lib/analysis-calculations.ts +++ b/src/lib/analysis-calculations.ts @@ -293,7 +293,12 @@ export function buildAnalysisCalculations( sourceIds: unique([...known.sourceIds, ...operand.sourceIds]), }; }; - const observations = supportedObservations(presentation.organization); + const observations = supportedObservations( + presentation.organization, + facts, + presentation.values, + presentation.excerpts + ); const observationOperands = observations.flatMap((o) => { const operand = fromObservation(o); return operand ? [operand] : []; diff --git a/src/lib/analysis-input.ts b/src/lib/analysis-input.ts index dc881af..bdeeddc 100644 --- a/src/lib/analysis-input.ts +++ b/src/lib/analysis-input.ts @@ -2,7 +2,7 @@ import { canonicalJSON, hashText, type FactSummary } from './fact-contract'; import type { SummaryPresentation } from './summary-presentation'; import { supportedExplanations, - supportedObservations, + reconciledOrganizationObservations, unresolvedExplanationSources, unresolvedTableSources, } from './summary-organization'; @@ -105,15 +105,26 @@ export function buildAnalysisInput( }); const explanations = supportedExplanations(presentation.organization); - const observations = supportedObservations(presentation.organization); + const reconciled = reconciledOrganizationObservations( + presentation.organization, + facts, + presentation.values, + presentation.excerpts + ); + const observations = reconciled.accepted; for (const [kind, items] of [ ['explanation', explanations], ['observation', observations], ] as const) { for (const item of items) { + const mergedFactId = kind === 'observation' ? reconciled.merged.get(item.id) : undefined; const text = 'metric' in item - ? `${item.metric}: ${literalValue(presentation.values.find((v) => v.id === item.valueId)!)}${item.comparison ? ` / ${comparisonAxisLabels[item.comparison.axis]} ${item.comparison.period}: ${literalValue(presentation.values.find((v) => v.id === item.comparison!.valueId)!)}` : ''}` + ? `${item.metric}: ${ + mergedFactId + ? `確定事実 fact:${mergedFactId} の同じ原数量への補足(区分: ${item.measure})` + : literalValue(presentation.values.find((v) => v.id === item.valueId)!) + }${item.comparison ? ` / ${comparisonAxisLabels[item.comparison.axis]} ${item.comparison.period} ${stateLabels[item.comparison.state]}: ${literalValue(presentation.values.find((v) => v.id === item.comparison!.valueId)!)}` : ''}` : renderNarrativeText(item.text, presentation.values); evidence.push({ id: `${kind}:${item.id}`, @@ -129,8 +140,8 @@ export function buildAnalysisInput( ] .filter(Boolean) .join(' / '), - sourceIds: unique(item.sourceIds), - pages: pagesOf(item.sourceIds), + sourceIds: unique([...item.sourceIds, ...(mergedFactId ? [mergedFactId] : [])]), + pages: pagesOf([...item.sourceIds, ...(mergedFactId ? [mergedFactId] : [])]), }); } } @@ -160,7 +171,6 @@ export function buildAnalysisInput( pages: unique(evidence.flatMap((e) => e.pages)).sort((a, b) => a - b), organizationStatus: presentation.organization.status, unresolvedSources: unique([ - ...presentation.organization.issues.flatMap((i) => i.sourceIds), ...unresolvedExplanationSources(presentation.organization, presentation.excerpts).map( (e) => e.id ), diff --git a/src/lib/analysis-version.test.ts b/src/lib/analysis-version.test.ts index 3662590..110dfdf 100644 --- a/src/lib/analysis-version.test.ts +++ b/src/lib/analysis-version.test.ts @@ -1,3 +1,5 @@ +import { createHash } from 'node:crypto'; +import { configuredApiUrl } from './llm-endpoint'; import { describe, expect, it } from 'vitest'; import { ANALYSIS_SCHEMA_VERSION, @@ -5,30 +7,63 @@ import { buildSummaryCacheKey, } from './analysis-version'; +const settings = { + provider: 'custom', + model: 'namespace/fixture', + baseUrl: 'https://api.example.com/v1/chat?deployment=a&token=private-token', + extractionMode: 'full' as const, +}; + describe('分析バージョン付きキャッシュキー', () => { - it('要約仕様・モデル・抽出方式を含む', () => { - const fingerprint = buildAnalysisFingerprint({ - provider: 'openrouter', - model: 'deepseek/deepseek-v4.1-flash', - extractionMode: 'full', - }); + it('仕様・モデル・抽出方式と実効URLのハッシュだけを含み、秘密値を残さない', async () => { + const fingerprint = await buildAnalysisFingerprint(settings); expect(fingerprint).toBe( - `v${ANALYSIS_SCHEMA_VERSION}:openrouter:deepseek%2Fdeepseek-v4.1-flash:full` + `v${ANALYSIS_SCHEMA_VERSION}:custom:namespace%2Ffixture:full:${createHash('sha256').update(settings.baseUrl).digest('hex')}` ); + expect(fingerprint).not.toContain('private-token'); + expect(fingerprint).not.toContain('api.example.com'); expect(buildSummaryCacheKey('20260714.pdf', fingerprint)).toBe(`${fingerprint}:20260714.pdf`); + const rotatedKey = { ...settings, apiKey: 'rotated-key' }; + expect(await buildAnalysisFingerprint(rotatedKey)).toBe(fingerprint); }); - it('設定が変わると別のキャッシュになる', () => { - const base = { - provider: 'openrouter', - model: 'deepseek/deepseek-v4.1-flash', - extractionMode: 'full' as const, - }; - expect(buildAnalysisFingerprint(base)).not.toBe( - buildAnalysisFingerprint({ ...base, model: 'deepseek/deepseek-v4-flash' }) - ); - expect(buildAnalysisFingerprint(base)).not.toBe( - buildAnalysisFingerprint({ ...base, extractionMode: 'smart' }) - ); + it('モデル・抽出方式・URLの経路やqueryの違いを区別する', async () => { + const fingerprint = await buildAnalysisFingerprint(settings); + for (const update of [ + { model: 'other' }, + { extractionMode: 'smart' as const }, + { baseUrl: settings.baseUrl.replace('/chat?', '/chat/?') }, + { baseUrl: settings.baseUrl.replace('/v1/', '/v2/') }, + { baseUrl: settings.baseUrl.replace('deployment=a', 'deployment=b') }, + { + baseUrl: settings.baseUrl.replace( + '?deployment=a&token=private-token', + '?token=private-token&deployment=a' + ), + }, + ]) + expect(await buildAnalysisFingerprint({ ...settings, ...update })).not.toBe(fingerprint); + }); + + it('fetchの同じURL表記は同一とし、標準プロバイダーのURL上書きも実効URLに含む', async () => { + expect( + await buildAnalysisFingerprint({ + ...settings, + baseUrl: + ' HTTPS://API.EXAMPLE.COM:443/x/../v1/chat?deployment=a&token=private-token#ignored ', + }) + ).toBe(await buildAnalysisFingerprint(settings)); + expect( + configuredApiUrl({ provider: 'openai', customUrl: 'https://unused.example/another' }) + ).toBe('https://unused.example/another'); + await expect( + buildAnalysisFingerprint({ + ...settings, + baseUrl: 'https://user:private-password@api.example.com/v1/chat', + }) + ).rejects.toThrow('URLに認証情報を含めない'); + await expect( + buildAnalysisFingerprint({ ...settings, baseUrl: 'private-invalid-url' }) + ).rejects.toThrow(/^API URLが不正です$/); }); }); diff --git a/src/lib/analysis-version.ts b/src/lib/analysis-version.ts index 99b44a1..4a689a1 100644 --- a/src/lib/analysis-version.ts +++ b/src/lib/analysis-version.ts @@ -1,19 +1,32 @@ +import { effectiveApiUrl } from './llm-endpoint'; import type { ExtractionMode } from '@/types/summaryMetadata'; -export const ANALYSIS_SCHEMA_VERSION = 100; +export const ANALYSIS_SCHEMA_VERSION = 104; export interface AnalysisFingerprintSettings { provider: string; model: string; + baseUrl?: string; extractionMode: ExtractionMode; } -export function buildAnalysisFingerprint(settings: AnalysisFingerprintSettings): string { +export async function buildAnalysisFingerprint( + settings: AnalysisFingerprintSettings +): Promise { + // Only a digest enters cache keys/metadata/diagnostics; never API keys or raw URLs. + const digest = await crypto.subtle.digest( + 'SHA-256', + new TextEncoder().encode(effectiveApiUrl(settings)) + ); + const endpoint = Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, '0')).join( + '' + ); return [ `v${ANALYSIS_SCHEMA_VERSION}`, encodeURIComponent(settings.provider), encodeURIComponent(settings.model), settings.extractionMode, + endpoint, ].join(':'); } diff --git a/src/lib/disclosure-observation.test.ts b/src/lib/disclosure-observation.test.ts new file mode 100644 index 0000000..81f8cfc --- /dev/null +++ b/src/lib/disclosure-observation.test.ts @@ -0,0 +1,577 @@ +import { 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 type { FactSummary } from './fact-contract'; +import { + factObservation, + reconcileObservations, + observationChange, + type DisclosureObservation, + type ConfirmedObservation, +} from './disclosure-observation'; +import { buildPresentation, revalidatePresentation } from './summary-presentation'; +import { + emptyOrganization, + explanationSources, + organizationHash, + unresolvedExplanationSources, + unresolvedTableSources, + supportedObservations, +} from './summary-organization'; +import { renderFacts } from './fact-summary'; +import { buildAnalysisInput } from './analysis-input'; +import { buildAnalysisCalculations } from './analysis-calculations'; + +function reviewable(value: ConfirmedObservation): DisclosureObservation { + const copy: Partial = { ...value }; + delete copy.sourceBasis; + delete copy.comparisonSourceBasis; + delete copy.unresolved; + return copy as DisclosureObservation; +} + +function reviewedTable(label = '事業損失', previous = '10', includeRate = false) { + const columns = [ + { label: '売上高', current: '100', previous: '90', x: 280, rate: includeRate }, + { label, current: '20', previous, x: includeRate ? 740 : 480, rate: false }, + { label: '当期純利益', current: '10', previous: '8', x: includeRate ? 940 : 680, rate: false }, + ]; + const page = cells( + [ + ['上場会社名 株式会社テスト', 0, 0, 240], + ['1. 連結経営成績', 0, 30, 280], + ['2026年3月期', 0, 120, 140], + ['2025年3月期', 0, 150, 140], + ...columns.flatMap((column): [string, number, number, number][] => [ + [column.label, column.x, 60, column.rate ? 360 : 140], + ['百万円', column.x + 60, 90, 80], + [column.current, column.x + 60, 120, 80], + [column.previous, column.x + 60, 150, 80], + ...(column.rate + ? ([ + ['%', column.x + 180, 90, 80], + ['11.1', column.x + 180, 120, 80], + ['5.0', column.x + 180, 150, 80], + ] as [string, number, number, number][]) + : []), + ]), + ], + 1 + ); + const inputs = buildDocumentContext([page]).tableMappings.map((mapping) => { + const amount = tableAmount([page], mapping, { + period: page.spans.find((s) => s.id === mapping.periodIds[0])!.text, + subject: '株式会社テスト', + scope: '連結', + basis: null, + state: 'actual', + }); + if (amount.unit === '%') amount.semantics.metricKind = 'rate'; + return amount; + }); + const reviewed = reviewCandidates(candidateResponse(inputs, [page], 'other'), 'other', [page]); + expect(reviewed.unverified).toEqual([]); + const facts: FactSummary = { + version: 6, + documentType: 'other', + facts: reviewed.facts, + unverified: [], + }; + const display = buildPresentation(facts, [page]); + const fact = facts.facts.find((f) => f.label === label && f.value === 20)!; + const base = factObservation(fact, facts, display.excerpts, display.values); + const observation = reviewable(base); + const review = (observations: DisclosureObservation[]) => { + const organization = { ...emptyOrganization(), observations }; + organization.review = { + contentHash: organizationHash(organization, facts, display.values, display.excerpts), + claims: Object.fromEntries(observations.map((o) => [o.id, null])), + sources: Object.fromEntries(explanationSources(display.excerpts).map((e) => [e.id, null])), + }; + organization.status = + unresolvedExplanationSources(organization, display.excerpts).length || + unresolvedTableSources(organization, facts, display.values, display.excerpts).length + ? 'partial' + : 'ready'; + if (!supportedObservations(organization, facts, display.values, display.excerpts).length) + organization.status = 'unavailable'; + display.organization = organization; + return display; + }; + return { + page, + facts, + display, + fact, + base, + observation: { ...observation, id: 'observation-0' }, + review, + }; +} + +it.each([ + ['事業損失', '10', 'loss', '↓損失拡大'], + ['事業利益', '△10', 'profit', '↑黒字転換'], +] as const)( + '未分類の確定値 %s に同じ数量の点検済み意味を接続し、保存後も保持する', + (label, previous, measure, expected) => { + const { facts, fact, page, display, base, observation, review } = reviewedTable( + label, + previous + ); + expect(base.measure).toBe('other'); + const selected = { + ...observation, + measure, + period: `${observation.period}通期`, + comparison: { ...observation.comparison!, period: `${observation.comparison!.period}通期` }, + valueId: fact.evidence.kind === 'table' ? fact.evidence.valueId : fact.id, + }; + const merged = reconcileObservations(facts, [selected], display.excerpts, display.values); + expect(merged.conflicts).toEqual([]); + expect(merged.supplement).toEqual([]); + expect(merged.primary.get(fact.id)).toMatchObject({ + measure, + valueId: fact.id, + sourceBasis: base.sourceBasis, + }); + expect(observationChange(merged.primary.get(fact.id)!, display.values).text).toContain( + expected + ); + review([selected]); + const markdown = renderFacts(facts, display); + const rows = markdown + .split('\n') + .filter((line) => line.startsWith('| ') && line.includes(label)); + expect(rows).toHaveLength(1); + expect(rows[0]).toContain(expected); + expect(rows[0]).toContain('20百万円'); + expect( + renderFacts(facts, revalidatePresentation(JSON.parse(JSON.stringify(display)), facts, [page])) + ).toBe(markdown); + } +); + +it('点検済みの意味や比較の競合を順序で上書きせず、確定値と未整理の通知を残す', () => { + const { facts, fact, display, base, observation, review } = reviewedTable(); + expect(renderFacts(facts, display).split('## 開示内容')[0]).toContain('↓損失拡大'); + const loss = { ...observation, measure: 'loss' as const }; + const profit = { ...observation, id: 'observation-1', measure: 'profit' as const }; + const differentAxis = { + ...loss, + comparison: { ...loss.comparison!, axis: 'sequential' as const }, + }; + for (const observations of [[loss, profit], [profit, loss], [differentAxis]]) { + const merged = reconcileObservations(facts, observations, display.excerpts, display.values); + expect(merged.primary.get(fact.id)).toEqual({ ...base, unresolved: true }); + expect(merged.conflicts).toEqual(observations); + expect(merged.supplement).toEqual([]); + } + const markdown = renderFacts(facts, review([loss, profit])); + expect(markdown).toContain('補足指標の未整理'); + expect(markdown).toContain('20百万円'); + expect(markdown).not.toContain('↓損失拡大'); + expect(buildAnalysisInput(facts, display).coverage.observations).toBe(0); + expect(buildAnalysisInput(facts, display).evidence.some((e) => e.kind === 'observation')).toBe( + false + ); + expect( + buildAnalysisCalculations(facts, display).every((c) => c.sourceObservationIds.length === 0) + ).toBe(true); +}); + +it.each([false, true])( + '異なる株式分割基準を比較へ混ぜず各数量の分母・注記を表示する(当期注記 %s)', + (currentNote) => { + const page = cells( + [ + ['会社名 株式会社テスト', 0, 0, 240], + ['2. 配当の状況', 0, 30, 250], + ['年間配当金期末', 230, 60, 150], + ['年間配当金合計', 430, 60, 210], + ['円', 230, 85, 80], + ['円', 430, 85, 80], + ['2026年3月期', 0, 110, 180], + ['20', 230, 110, 80], + ['40', 430, 110, 80], + ['2025年3月期', 0, 140, 180], + ['10', 230, 140, 80], + ['20', 430, 140, 80], + ['2025年3月期の配当金については、株式分割前の金額を記載しています。', 0, 180, 1000], + ...(currentNote + ? [ + [ + '2026年3月期の配当金については、株式分割後の金額を記載しています。', + 0, + 210, + 1000, + ] as [string, number, number, number], + ] + : []), + ], + 1 + ); + const inputs = buildDocumentContext([page]).tableMappings.map((mapping) => { + const fact = tableAmount([page], mapping, { + period: page.spans.find((s) => s.id === mapping.periodIds[0])!.text, + subject: '株式会社テスト', + scope: null, + basis: null, + state: 'actual', + }); + fact.semantics.metricKind = 'perShare'; + return fact; + }); + const reviewed = reviewCandidates(candidateResponse(inputs, [page], 'other'), 'other', [page]); + expect(reviewed.unverified).toEqual([]); + expect(reviewed.facts).toHaveLength(4); + const facts: FactSummary = { + version: 6, + documentType: 'other', + facts: reviewed.facts, + unverified: [], + }; + const display = buildPresentation(facts, [page]); + const current = facts.facts.find( + (f) => f.label === '年間配当金期末' && f.period === '2026年3月期' + )!; + const previous = facts.facts.find( + (f) => f.label === current.label && f.period === '2025年3月期' + )!; + const base = reviewable(factObservation(current, facts, display.excerpts, display.values)); + expect(base.comparison).toBeNull(); + const observation: DisclosureObservation = { + ...base, + id: 'observation-0', + comparison: { + axis: 'yearOnYear', + period: previous.period!, + state: 'actual', + valueId: previous.id, + rateId: null, + }, + }; + expect( + reconcileObservations(facts, [observation], display.excerpts, display.values).conflicts + ).toEqual([observation]); + const markdown = renderFacts(facts, display); + const rows = markdown + .split('\n') + .filter((line) => line.startsWith('| ') && line.includes(current.label)); + expect(rows).toHaveLength(2); + expect(rows.find((line) => line.includes('10円'))).toContain('1株当たり、株式分割前'); + const currentRow = rows.find((line) => line.includes('20円'))!; + expect(currentRow).toContain('1株当たり'); + expect(currentRow.includes('株式分割後')).toBe(currentNote); + expect(currentRow).not.toContain('株式分割前'); + expect(markdown.split('\n## 原文\n')[0]).toContain( + '2025年3月期の配当金については、株式分割前の金額を記載しています。' + ); + expect( + renderFacts(facts, revalidatePresentation(JSON.parse(JSON.stringify(display)), facts, [page])) + ).toBe(markdown); + } +); + +it('原数量を先に照合し、明確な文脈矛盾だけを除き、別名・未知の文脈・別セルは残す', () => { + const { facts, fact, display, observation } = reviewedTable('営業利益', '20'); + const nativeId = fact.evidence.kind === 'table' ? fact.evidence.valueId : fact.id; + const selected = { ...observation, valueId: nativeId, comparison: null }; + const classify = (claim: DisclosureObservation, input = facts) => + reconcileObservations(input, [claim], display.excerpts, display.values); + for (const change of [ + { state: 'forecast' as const }, + { scope: '個別' }, + { metric: '売上高' }, + { metric: '営業損失', measure: 'loss' as const }, + { period: '2025年3月期' }, + ]) { + const claim = { ...selected, ...change }; + expect(classify(claim).conflicts).toEqual([claim]); + expect(classify(claim).primary.get(fact.id)?.unresolved).toBe(false); + } + for (const change of [ + { entity: '当社' }, + { entity: null, scope: null, period: null, state: 'unspecified' as const }, + { scope: 'グループ全体', period: '当期' }, + { period: '2026年3月期通期' }, + { period: '2026年03月期' }, + ]) + expect(classify({ ...selected, ...change }).conflicts).toEqual([]); + + const withMeaning = ( + scope: string, + basis: string, + period = fact.period!, + state = fact.semantics.state + ): FactSummary => ({ + ...facts, + facts: facts.facts.map((f) => + f.id === fact.id ? { ...f, period, semantics: { ...f.semantics, scope, basis, state } } : f + ), + }); + const aliases = classify( + { ...selected, scope: '非連結', basis: '国際会計基準' }, + withMeaning('個別', 'IFRS') + ); + expect(aliases.conflicts).toEqual([]); + expect(aliases.supplement).toEqual([]); + expect(aliases.primary.get(fact.id)?.valueId).toBe(fact.id); + expect( + classify({ ...selected, basis: '日本基準' }, withMeaning('連結', 'IFRS')).conflicts + ).toHaveLength(1); + expect( + classify( + { ...selected, state: 'forecast' }, + withMeaning('連結', 'IFRS', fact.period!, 'forecastAfter') + ).conflicts + ).toEqual([]); + expect( + classify( + { ...selected, state: 'forecastBefore' }, + withMeaning('連結', 'IFRS', fact.period!, 'forecastAfter') + ).conflicts + ).toHaveLength(1); + expect( + classify( + { ...selected, period: '2026年3月期第2四半期' }, + withMeaning('連結', 'IFRS', '2026年3月期第2四半期累計') + ).conflicts + ).toEqual([]); + + expect( + classify( + { ...selected, period: '2026年3月期第1四半期単独' }, + withMeaning('連結', 'IFRS', '2026年3月期第1四半期累計') + ).conflicts + ).toHaveLength(1); + + const other = facts.facts.find((f) => f.id !== fact.id && f.value === 20)!; + expect(other.evidence).not.toEqual(fact.evidence); + const distinct = { + ...selected, + state: 'forecast' as const, + valueId: other.evidence.kind === 'table' ? other.evidence.valueId : other.id, + }; + expect( + classify(distinct, { ...facts, facts: facts.facts.filter((f) => f.id !== other.id) }).conflicts + ).toEqual([]); + const alternate = { + ...fact, + id: 'alternate', + semantics: { ...fact.semantics, state: 'forecast' as const }, + }; + expect( + classify({ ...selected, state: 'forecast' }, { ...facts, facts: [...facts.facts, alternate] }) + .conflicts + ).toEqual([]); +}); + +it('補足側だけの現在値でも比較原数量の文脈を照合し、複数の確定文脈を順序で拒否しない', () => { + const { facts, fact, display, observation } = reviewedTable('営業利益'); + const nativeCurrent = fact.evidence.kind === 'table' ? fact.evidence.valueId : fact.id; + const supplemental = { + ...observation, + valueId: nativeCurrent, + comparison: { ...observation.comparison!, state: 'forecast' as const }, + }; + const onlyReference = { ...facts, facts: facts.facts.filter((f) => f.id !== fact.id) }; + expect( + reconcileObservations(onlyReference, [supplemental], display.excerpts, display.values).conflicts + ).toEqual([supplemental]); + const organization = { ...emptyOrganization(), observations: [supplemental] }; + organization.review = { + contentHash: organizationHash(organization, onlyReference, display.values, display.excerpts), + claims: { [supplemental.id]: null }, + sources: {}, + }; + expect( + unresolvedTableSources(organization, onlyReference, display.values, display.excerpts).some( + (source) => source.spanIds.includes(nativeCurrent) + ) + ).toBe(true); + const reference = facts.facts.find((f) => f.id === observation.comparison!.valueId)!; + const alternate = { + ...reference, + id: 'alternate', + semantics: { ...reference.semantics, scope: '個別' }, + }; + const multiple = { ...facts, facts: [alternate, ...facts.facts] }; + const result = reconcileObservations(multiple, [observation], display.excerpts, display.values); + expect(result.conflicts).toEqual([]); + expect(result.primary.get(fact.id)?.comparison).toEqual(observation.comparison); +}); + +it('意味点検が同一実績セルを予想として誤承認しても、保存復元後の本文・分析・計算へ通さない', () => { + const { facts, fact, page, display, observation, review } = reviewedTable('営業利益'); + const misapproved = { + ...observation, + state: 'forecast' as const, + comparison: null, + valueId: fact.evidence.kind === 'table' ? fact.evidence.valueId : fact.id, + }; + review([misapproved]); + const restored = revalidatePresentation(JSON.parse(JSON.stringify(display)), facts, [page]); + const markdown = renderFacts(facts, restored); + expect(markdown).toContain('補足指標の未整理'); + expect(markdown).toContain('20百万円'); + expect(markdown.split('## 開示内容')[0]).toContain('↑増益'); + expect(markdown).not.toContain('2026年3月期/予想/連結'); + expect( + supportedObservations(restored.organization!, facts, restored.values, restored.excerpts) + ).toEqual([]); + const analysis = buildAnalysisInput(facts, restored); + expect(analysis.coverage.observations).toBe(0); + expect(analysis.evidence.some((e) => e.kind === 'observation')).toBe(false); + expect( + buildAnalysisCalculations(facts, restored).every((c) => c.sourceObservationIds.length === 0) + ).toBe(true); + expect(restored.organization!.observations).toEqual([misapproved]); +}); + +it('増減率も原数量の所有者と比較軸を照合し、無関係な比率を表示・保存・分析へ通さない', () => { + const { facts, fact, display, page, observation, review } = reviewedTable('営業利益', '10', true); + const rate = facts.facts.find( + (f) => f.label === '売上高' && f.unit === '%' && f.period === fact.period + )!; + const revenue = facts.facts.find( + (f) => f.label === '売上高' && f.unit === '百万円' && f.period === fact.period + )!; + expect(observation.comparison?.rateId).toBeNull(); + const nativeRate = rate.evidence.kind === 'table' ? rate.evidence.valueId : rate.id; + const classify = (claim: DisclosureObservation, input = facts) => + reconcileObservations(input, [claim], display.excerpts, display.values); + for (const rateId of [rate.id, nativeRate]) { + const claim = { ...observation, comparison: { ...observation.comparison!, rateId } }; + expect(classify(claim).conflicts).toEqual([claim]); + // The same guard applies when only the current amount is supplemental. + expect( + classify(claim, { ...facts, facts: facts.facts.filter((f) => f.id !== fact.id) }).conflicts + ).toEqual([claim]); + } + const valid = { + ...reviewable(factObservation(revenue, facts, display.excerpts, display.values)), + id: 'observation-1', + }; + expect(valid.comparison?.rateId).toBe(rate.id); + expect(classify(valid).conflicts).toEqual([]); + // An unknown entity alias deliberately bypasses the primary merge. The known + // physical rate still belongs to this amount's year-on-year comparison. + const wrongAxis = { + ...valid, + entity: '当社', + comparison: { ...valid.comparison!, axis: 'sequential' as const }, + }; + expect(classify(wrongAxis).conflicts).toEqual([wrongAxis]); + const ambiguousReference = { + ...valid, + entity: '当社', + comparison: { + ...valid.comparison!, + period: '前期', + state: 'unspecified' as const, + }, + }; + expect(classify(ambiguousReference).conflicts).toEqual([]); + expect(classify(ambiguousReference).supplement).toEqual([ambiguousReference]); + + const onlyRateOwner = { + ...facts, + facts: facts.facts.filter( + (f) => f.label !== '営業利益' && !(f.label === '売上高' && f.period !== fact.period) + ), + }; + const borrowed = { + ...valid, + valueId: observation.valueId, + comparison: { ...valid.comparison!, valueId: observation.comparison!.valueId }, + }; + expect(classify(borrowed, onlyRateOwner).conflicts).toEqual([borrowed]); + const unowned = { ...facts, facts: facts.facts.filter((f) => f.id !== rate.id) }; + expect( + classify({ ...valid, comparison: { ...valid.comparison!, rateId: nativeRate } }, unowned) + .conflicts + ).toEqual([]); + + const unrelated = { + ...observation, + comparison: { ...observation.comparison!, rateId: nativeRate }, + }; + review([unrelated]); + const restored = revalidatePresentation(JSON.parse(JSON.stringify(display)), facts, [page]); + expect( + supportedObservations(restored.organization!, facts, restored.values, restored.excerpts) + ).toEqual([]); + const profitRow = renderFacts(facts, restored) + .split('\n') + .find((line) => line.startsWith('| ') && line.includes('営業利益'))!; + expect(profitRow).toContain('約'); + expect(profitRow).not.toContain('原文'); + expect(buildAnalysisInput(facts, restored).coverage.observations).toBe(0); +}); + +it('既知の指標別名は同じ数量へ統合し、比較値の別名も重複表示しない', () => { + const { facts, display, review } = reviewedTable(); + const current = facts.facts.find((f) => f.label === '売上高' && f.value === 100)!; + const base = reviewable(factObservation(current, facts, display.excerpts, display.values)); + const alias = { ...base, id: 'observation-0', metric: '営業収益' }; + const before = { + ...alias, + id: 'observation-1', + valueId: base.comparison!.valueId, + period: base.comparison!.period, + state: base.comparison!.state, + comparison: null, + }; + const result = reconcileObservations(facts, [alias, before], display.excerpts, display.values); + expect(result.conflicts).toEqual([]); + expect(result.supplement).toEqual([]); + expect(result.primary.get(current.id)?.comparison).toEqual(base.comparison); + const aliasContext = { ...alias, scope: '非連結', basis: '国際会計基準' }; + const aliasedFacts = { + ...facts, + facts: facts.facts.map((f) => ({ + ...f, + semantics: { + ...f.semantics, + scope: f.id === current.id ? '個別' : '単体', + basis: f.id === current.id ? 'IFRS' : '国際会計基準', + }, + })), + }; + const aligned = reconcileObservations( + aliasedFacts, + [aliasContext], + display.excerpts, + display.values + ); + expect(aligned.conflicts).toEqual([]); + expect(aligned.supplement).toEqual([]); + expect(aligned.primary.get(current.id)?.comparison).toEqual(base.comparison); + const noFacts = { ...facts, facts: [] }; + const priorAlias = { ...before, metric: '売上収益' }; + expect( + reconcileObservations(noFacts, [alias, priorAlias], display.excerpts, display.values).supplement + ).toEqual([alias]); + const unknownPeriod = { ...priorAlias, period: '比較期間不明' }; + expect( + reconcileObservations(noFacts, [alias, unknownPeriod], display.excerpts, display.values) + .supplement + ).toEqual([alias, unknownPeriod]); + const unknownName = { ...alias, metric: 'revenue' }; + expect( + reconcileObservations(facts, [unknownName], display.excerpts, display.values).supplement + ).toEqual([unknownName]); + expect(result.merged.get(alias.id)).toBe(current.id); + review([alias]); + const analysis = buildAnalysisInput(facts, display); + const note = analysis.evidence.find((e) => e.id === `observation:${alias.id}`)!; + expect(note.text).toContain(`fact:${current.id} の同じ原数量への補足`); + expect(note.text).toContain('区分: revenue'); + expect(note.text).toContain('前年同期 2025年3月期 実績: 90百万円'); + expect(note.text).not.toContain('100百万円'); + expect(note.sourceIds).toContain(current.id); + expect(analysis.evidence.find((e) => e.id === `fact:${current.id}`)?.text).toContain('100百万円'); +}); diff --git a/src/lib/disclosure-observation.ts b/src/lib/disclosure-observation.ts index 25196f8..1f30dbd 100644 --- a/src/lib/disclosure-observation.ts +++ b/src/lib/disclosure-observation.ts @@ -22,6 +22,7 @@ import { import { literalValue, quantityChange } from './summary-narrative-renderer'; import type { SourceExcerpt } from './summary-source-inventory'; import type { ContentRole } from './summary-content-policy'; +import type { SourceProvenance } from './source-provenance'; /** Meaning, not a table layout. Company-specific metrics and periods remain named data. */ export const OBSERVATION_TOPICS = [ @@ -81,6 +82,29 @@ export interface DisclosureObservation extends DisclosureContext { valueId: string; comparison: ObservationComparison | null; } +/** Derived only from saved, confirmed facts; never accepted from the model's wire format. */ +export interface ConfirmedObservation extends DisclosureObservation { + sourceBasis: Pick; + comparisonSourceBasis: Pick | null; + unresolved: boolean; +} +export const adjustmentLabels: Record = { + splitAdjusted: '株式分割調整済み', + beforeSplit: '株式分割前', + afterSplit: '株式分割後', +}; +export function observationBasis( + value: DisclosureObservation | ConfirmedObservation, + comparison = false +): string[] { + if (!('sourceBasis' in value)) return []; + const basis = comparison ? value.comparisonSourceBasis : value.sourceBasis; + if (!basis) return []; + return [ + ...(basis.denominator && !/[11]株(?:当たり|あたり)/.test(value.metric) ? ['1株当たり'] : []), + ...new Set(basis.adjustments.map((a) => adjustmentLabels[a.basis])), + ]; +} export interface DisclosureExplanation extends NarrativeLine, DisclosureContext {} const named = (v: unknown): v is string => typeof v === 'string' && !!v.trim() && v.length <= 1000; export const nullableName = (v: unknown) => v === null || named(v); @@ -291,9 +315,11 @@ export function observationGroup(value: DisclosureObservation): string { return canonicalJSON([value.topic, value.scope, value.basis, value.conditions]); } export function observationChange( - value: DisclosureObservation, + value: DisclosureObservation | ConfirmedObservation, values: NarrativeValue[] ): { text: string; calculated: boolean } { + if ('unresolved' in value && value.unresolved) + return { text: '指標区分・比較は未確認', calculated: false }; if (!value.comparison) return { text: '', calculated: false }; const current = values.find((q) => q.id === value.valueId)!; const previous = values.find((q) => q.id === value.comparison!.valueId)!; @@ -365,7 +391,7 @@ export function factObservation( facts: FactSummary, excerpts: SourceExcerpt[], values: NarrativeValue[] -): DisclosureObservation { +): ConfirmedObservation { const pair = summaryComparison(fact, facts.facts); const metric = reportingMetricKey(fact.label); const rates = facts.facts.filter((rate) => canPair(fact, rate)); @@ -382,9 +408,20 @@ export function factObservation( : 'profit' : 'other'; const owner = fact.evidence.kind === 'table' ? fact.evidence.valueId : fact.evidence.blockId; + const sourceBasis = { + denominator: fact.provenance?.denominator ?? null, + adjustments: fact.provenance?.adjustments ?? [], + }; + const basisSources = new Set([ + ...(sourceBasis.denominator?.sourceIds ?? []), + ...sourceBasis.adjustments.map((a) => a.noteId), + ]); const sourceIds = [ ...new Set([ ...excerpts.filter((e) => e.blockId === owner || e.spanIds.includes(owner)).map((e) => e.id), + ...excerpts + .filter((e) => basisSources.has(e.blockId) || e.spanIds.some((id) => basisSources.has(id))) + .map((e) => e.id), ...values .filter( (q) => @@ -425,16 +462,114 @@ export function factObservation( ]), ], sourceIds, + sourceBasis, + comparisonSourceBasis: pair + ? { + denominator: pair.reference.provenance?.denominator ?? null, + adjustments: pair.reference.provenance?.adjustments ?? [], + } + : null, + unresolved: false, }; } +const compactContext = (value: string | null) => + value?.normalize('NFKC').replace(/\s/g, '') ?? null; +const scopeKey = (value: string | null) => { + const text = compactContext(value); + return text === '連結' + ? 'consolidated' + : /^(個別|単体|非連結)$/.test(text ?? '') + ? 'separate' + : null; +}; +const basisKey = (value: string | null) => { + const text = compactContext(value)?.toUpperCase(); + return text === 'IFRS' || text === '国際会計基準' + ? 'IFRS' + : text === '日本基準' || text === '米国基準' + ? text + : null; +}; +// Unknown names retain their own identity; two unknown canonical keys are not aliases. +const contextIdentity = (value: string | null, key: (value: string) => string | null) => { + if (value === null) return null; + const known = key(value); + return known === null ? `named:${compactContext(value)}` : `known:${known}`; +}; +const sameMetric = (a: string, b: string) => + contextIdentity(a, reportingMetricKey) === contextIdentity(b, reportingMetricKey); +const sameScope = (a: string | null, b: string | null) => + contextIdentity(a, scopeKey) === contextIdentity(b, scopeKey); +const sameBasis = (a: string | null, b: string | null) => + contextIdentity(a, basisKey) === contextIdentity(b, basisKey); + +/** Only disjoint, explicitly understood meanings are contradictions. Free-form labels, + * aliases and missing metadata still belong to the independent semantic review. */ +function contradictsConfirmedContext( + confirmed: DisclosureObservation, + claimed: DisclosureObservation +): boolean { + const period = (value: string | null) => { + if (!value) return null; + const text = reportingPeriodText(value); + if (!new RegExp(`^${REPORTING_FISCAL_PERIOD_PATTERN}$`).test(text)) return null; + const fiscal = text + .match(/^(20\d{2})年(\d{1,2})月期/)! + .slice(1) + .map(Number) + .join(':'); + const shape = reportingPeriodShapes(text)[0] ?? '通期'; + const qualifier = /単独/.test(text) + ? '単独' + : /累計|中間期/.test(text) || shape === '第1四半期' + ? '累計' + : null; + return { fiscal, shape, qualifier }; + }; + const different = (a: string | null, b: string | null) => a !== null && b !== null && a !== b; + const numericStates = ['actual', 'forecast', 'forecastBefore', 'forecastAfter']; + const stateConflict = + numericStates.includes(confirmed.state) && + numericStates.includes(claimed.state) && + confirmed.state !== claimed.state && + !( + [confirmed.state, claimed.state].includes('forecast') && + confirmed.state.startsWith('forecast') && + claimed.state.startsWith('forecast') + ); + const before = period(confirmed.period), + after = period(claimed.period); + const periodConflict = + before && + after && + (before.fiscal !== after.fiscal || + before.shape !== after.shape || + different(before.qualifier, after.qualifier)); + return ( + stateConflict || + (confirmed.measure !== 'other' && + claimed.measure !== 'other' && + confirmed.measure !== claimed.measure) || + different(reportingMetricKey(confirmed.metric), reportingMetricKey(claimed.metric)) || + different(scopeKey(confirmed.scope), scopeKey(claimed.scope)) || + different(basisKey(confirmed.basis), basisKey(claimed.basis)) || + !!periodConflict + ); +} + /** Join independently reviewed relationships with confirmed amounts by native identity. */ export function reconcileObservations( facts: FactSummary, observations: DisclosureObservation[], excerpts: SourceExcerpt[], values: NarrativeValue[] -): { primary: Map; supplement: DisclosureObservation[] } { +): { + primary: Map; + merged: Map; + supplement: DisclosureObservation[]; + conflicts: DisclosureObservation[]; +} { const native = (id: string) => { const fact = facts.facts.find((f) => f.id === id); return fact?.evidence.kind === 'table' @@ -448,43 +583,166 @@ export function reconcileObservations( .filter((f) => f.quantity) .map((f) => [f.id, factObservation(f, facts, excerpts, values)]) ); + const merged = new Map(); const supplement: DisclosureObservation[] = []; + const conflicts: DisclosureObservation[] = []; + const periodKey = (period: string | null) => + period && + reportingPeriodText(period) + .replace(/(20\d{2})年0?(\d{1,2})月期/, '$1年$2月期') + .replace(/^(20\d{2}年\d{1,2}月期)通期$/, '$1') + .replace(/中間期(?:累計)?(?:期間)?$/, '第2四半期累計') + .replace(/\((累計|単独)\)(?:期間)?$/, '$1') + .replace(/(累計|単独)期間$/, '$1'); + const comparisonKey = (comparison: ObservationComparison) => + canonicalJSON([ + native(comparison.valueId), + comparison.axis, + periodKey(comparison.period), + comparison.state, + ]); + const grouped = new Map(); + const owners = new Map(); + for (const confirmed of primary.values()) { + const id = native(confirmed.valueId); + owners.set(id, [...(owners.get(id) ?? []), confirmed]); + } + const contradicted = (claim: DisclosureObservation) => { + const confirmed = owners.get(native(claim.valueId)) ?? []; + // One physical quantity may legitimately have several confirmed contexts. + // A compatible or ambiguous owner prevents declaring a clear contradiction. + return ( + confirmed.length > 0 && confirmed.every((value) => contradictsConfirmedContext(value, claim)) + ); + }; + const contradictedRate = (observation: DisclosureObservation) => { + const comparison = observation.comparison; + if (!comparison?.rateId) return false; + const rateId = native(comparison.rateId); + const rateOwners = owners.get(rateId) ?? []; + const claimedRate = { ...observation, valueId: comparison.rateId, measure: 'rate' as const }; + return ( + rateOwners.length > 0 && + rateOwners.every((owner) => { + const rate = facts.facts.find((f) => f.id === owner.id)!; + if (rate.semantics.metricKind !== 'rate' || contradictsConfirmedContext(owner, claimedRate)) + return true; + // A known amount/rate pairing owns the current physical amount, even + // without an extracted comparison. A known comparison also owns its axis. + const paired = facts.facts.filter((amount) => amount.quantity && canPair(amount, rate)); + return ( + paired.length > 0 && + paired.every((amount) => { + const expected = primary.get(amount.id)!.comparison; + return ( + native(amount.id) !== native(observation.valueId) || + (expected !== null && + (expected.axis !== comparison.axis || + native(expected.valueId) !== native(comparison.valueId))) + ); + }) + ); + }) + ); + }; for (const observation of observations) { + // Resolve physical identity before semantic matching: a changed state/metric + // must not escape reconciliation as a supposedly new quantity. Apply the same + // rule to comparison operands, even when the current quantity is supplemental. + if ( + contradicted(observation) || + (observation.comparison && contradicted({ ...observation, ...observation.comparison })) || + contradictedRate(observation) + ) { + conflicts.push(observation); + continue; + } const matches = [...primary.values()].filter( (p) => native(p.valueId) === native(observation.valueId) && (observation.entity === null || observation.entity === p.entity) && - observation.metric === p.metric && + sameMetric(observation.metric, p.metric) && observation.state === p.state && - (observation.scope === null || observation.scope === p.scope) && - (observation.basis === null || observation.basis === p.basis) + (observation.period === null || periodKey(observation.period) === periodKey(p.period)) && + (observation.scope === null || sameScope(observation.scope, p.scope)) && + (observation.basis === null || sameBasis(observation.basis, p.basis)) ); if (matches.length !== 1) { supplement.push(observation); continue; } const current = matches[0]; + grouped.set(current.id, [...(grouped.get(current.id) ?? []), observation]); + } + for (const [id, reviewed] of grouped) { + const current = primary.get(id)!; + const measures = new Set( + [current, ...reviewed].map((o) => o.measure).filter((measure) => measure !== 'other') + ); + const comparisons = [current, ...reviewed].flatMap((o) => (o.comparison ? [o.comparison] : [])); + const rateIds = new Set(comparisons.flatMap((c) => (c.rateId ? [native(c.rateId)] : []))); + const comparisonBasis = (fact: VerifiedFact) => + canonicalJSON([ + fact.semantics.subject, + contextIdentity(fact.semantics.scope, scopeKey), + contextIdentity(fact.semantics.basis, basisKey), + fact.semantics.metricKind, + fact.semantics.qualifiers, + fact.semantics.conditions, + fact.provenance?.denominator + ? [fact.provenance.denominator.value, fact.provenance.denominator.unit] + : null, + fact.provenance?.adjustments ?? [], + ]); + const fact = facts.facts.find((f) => f.id === id)!; + const compatibleReference = (c: ObservationComparison, f: VerifiedFact) => + native(f.id) === native(c.valueId) && + comparisonBasis(fact) === comparisonBasis(f) && + periodKey(c.period) === periodKey(factPeriodName(f)) && + c.state === f.semantics.state; + const incompatibleReference = comparisons.some((c) => { + const references = facts.facts.filter((f) => native(f.id) === native(c.valueId)); + return references.length > 0 && !references.some((f) => compatibleReference(c, f)); + }); + if ( + measures.size > 1 || + new Set(reviewed.map((o) => o.topic)).size > 1 || + new Set(comparisons.map(comparisonKey)).size > 1 || + rateIds.size > 1 || + incompatibleReference + ) { + // Never let order decide which independently reviewed meaning wins. The + // confirmed amounts remain visible; incompatible review stays unresolved. + conflicts.push(...reviewed); + primary.set(id, { ...current, unresolved: true }); + continue; + } + for (const observation of reviewed) merged.set(observation.id, id); + const comparison = comparisons[0] ?? null; + const reference = comparison && facts.facts.find((f) => compatibleReference(comparison, f)); primary.set(current.id, { ...current, - comparison: - current.comparison && - observation.comparison && - native(current.comparison.valueId) === native(observation.comparison.valueId) && - current.comparison.axis === observation.comparison.axis && - current.comparison.state === observation.comparison.state - ? { - ...current.comparison, - rateId: current.comparison.rateId ?? observation.comparison.rateId, - } - : (current.comparison ?? observation.comparison), - conditions: [...new Set([...current.conditions, ...observation.conditions])], - sourceIds: [...new Set([...current.sourceIds, ...observation.sourceIds])], + topic: reviewed[0].topic, + measure: [...measures][0] ?? 'other', + comparison: comparison + ? { ...comparison, rateId: comparisons.find((c) => c.rateId)?.rateId ?? null } + : null, + comparisonSourceBasis: reference + ? { + denominator: reference.provenance?.denominator ?? null, + adjustments: reference.provenance?.adjustments ?? [], + } + : null, + conditions: [...new Set([current, ...reviewed].flatMap((o) => o.conditions))], + sourceIds: [...new Set([current, ...reviewed].flatMap((o) => o.sourceIds))], }); } // A comparison value already has a visible place in its owning observation. const displayed = [...primary.values(), ...supplement]; return { primary, + merged, + conflicts, supplement: supplement.filter( (p) => p.comparison || @@ -493,10 +751,11 @@ export function reconcileObservations( owner.comparison && native(owner.comparison.valueId) === native(p.valueId) && owner.entity === p.entity && - owner.metric === p.metric && - owner.scope === p.scope && - owner.basis === p.basis && - owner.comparison.state === p.state + sameMetric(owner.metric, p.metric) && + sameScope(owner.scope, p.scope) && + sameBasis(owner.basis, p.basis) && + owner.comparison.state === p.state && + periodKey(owner.comparison.period) === periodKey(p.period) ) ), }; diff --git a/src/lib/llm-client.test.ts b/src/lib/llm-client.test.ts index b565c29..97d1a58 100644 --- a/src/lib/llm-client.test.ts +++ b/src/lib/llm-client.test.ts @@ -33,6 +33,33 @@ afterEach(() => { vi.restoreAllMocks(); }); +it.each(['custom', 'anthropic'])( + '実効URLを%sの送信にも使用し、経路とqueryを保つ', + async (provider) => { + const fetchMock = vi + .fn() + .mockImplementation(async () => + provider === 'anthropic' + ? response('{}') + : new Response( + JSON.stringify({ choices: [{ message: { content: '{}' }, finish_reason: 'stop' }] }) + ) + ); + vi.stubGlobal('fetch', fetchMock); + await generateText( + { + ...config, + provider, + baseUrl: ' HTTPS://API.EXAMPLE.COM:443/x/../v1/chat/?deployment=a&deployment=b#fragment ', + }, + messages + ); + expect(fetchMock.mock.calls[0][0]).toBe( + 'https://api.example.com/v1/chat/?deployment=a&deployment=b' + ); + } +); + describe('Anthropicの出力予算と完了判定', () => { it('既定値と明示した出力予算をMessages APIへ渡す', async () => { const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(response('{}'))); diff --git a/src/lib/llm-client.ts b/src/lib/llm-client.ts index 0763dde..469e93f 100644 --- a/src/lib/llm-client.ts +++ b/src/lib/llm-client.ts @@ -1,3 +1,5 @@ +import { effectiveApiUrl } from './llm-endpoint'; + /** * 統一LLMクライアント * 各プロバイダーのAPIフォーマットの違いを吸収し、統一されたインターフェースを提供 @@ -83,8 +85,8 @@ export async function generateText(config: LLMConfig, messages: ChatMessage[]): * OpenAI、Google (OpenAI互換モード)、OpenRouter、カスタムプロバイダーに対応 */ async function generateTextOpenAI(config: LLMConfig, messages: ChatMessage[]): Promise { - // カスタムプロバイダーの場合はbaseUrlを使用、それ以外は既定のbaseUrlを使用 - const baseUrl = config.baseUrl || getDefaultBaseUrl(config.provider); + // 明示したbaseUrlまたはプロバイダー既定のURLを指紋と同じ規則で正規化する + const baseUrl = effectiveApiUrl(config); const started = performance.now(); const response = await fetch(baseUrl, { @@ -163,7 +165,7 @@ async function generateTextOpenAI(config: LLMConfig, messages: ChatMessage[]): P * Anthropicは独自のAPIフォーマットを使用 */ async function generateTextAnthropic(config: LLMConfig, messages: ChatMessage[]): Promise { - const baseUrl = config.baseUrl || 'https://api.anthropic.com/v1/messages'; + const baseUrl = effectiveApiUrl(config); // systemメッセージを分離 const systemMessage = messages.find((msg) => msg.role === 'system'); @@ -221,24 +223,6 @@ async function generateTextAnthropic(config: LLMConfig, messages: ChatMessage[]) return data.content[0].text; } -/** - * プロバイダーのデフォルトbaseURLを取得 - */ -function getDefaultBaseUrl(provider: string): string { - switch (provider) { - case 'openai': - return 'https://api.openai.com/v1/chat/completions'; - case 'google': - return 'https://generativelanguage.googleapis.com/v1beta/chat/completions'; - case 'openrouter': - return 'https://openrouter.ai/api/v1/chat/completions'; - case 'anthropic': - return 'https://api.anthropic.com/v1/messages'; - default: - throw new Error(`プロバイダー ${provider} のデフォルトURLが見つかりません`); - } -} - function buildApiError(status: number, statusText: string, errorText: string): ApiError { const detail = extractApiErrorMessage(errorText); const text = detail || statusText; diff --git a/src/lib/llm-endpoint.ts b/src/lib/llm-endpoint.ts new file mode 100644 index 0000000..d898fff --- /dev/null +++ b/src/lib/llm-endpoint.ts @@ -0,0 +1,25 @@ +import { getProvider } from './llm-providers'; + +/** The URL that fetch sends: keep path/query semantics, discard only the fragment. */ +export function effectiveApiUrl(config: { provider: string; baseUrl?: string }): string { + const raw = config.baseUrl || getProvider(config.provider)?.baseUrl; + let url: URL; + try { + url = new URL(raw ?? ''); + } catch { + // Never echo a URL: query parameters may contain credentials. + throw new Error('API URLが不正です'); + } + if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password) + throw new Error('API URLが不正です。URLに認証情報を含めないでください'); + url.hash = ''; + return url.href; +} + +/** Preserve stored URL overrides for every provider, including imported settings. */ +export function configuredApiUrl(settings: { provider: string; customUrl?: string }): string { + return effectiveApiUrl({ + provider: settings.provider, + baseUrl: settings.customUrl, + }); +} diff --git a/src/lib/real-pdf-corpus.test.ts b/src/lib/real-pdf-corpus.test.ts index 60f1497..802dd4f 100644 --- a/src/lib/real-pdf-corpus.test.ts +++ b/src/lib/real-pdf-corpus.test.ts @@ -19,6 +19,9 @@ import { verifyCoverage, coverageReport } from './fact-coverage'; import { preflightCandidateSource } from './source-preflight'; import { buildDocumentContext } from './document-context'; import { stableFactId } from './fact-contract'; +import { buildPresentation, revalidatePresentation } from './summary-presentation'; +import { factObservation, observationBasis, adjustmentLabels } from './disclosure-observation'; +import { literalMarkdown } from './summary-renderer'; function proseEvent( blockId: string, candidateId: string, @@ -242,6 +245,46 @@ describe('原PDFから独立に固定した表紙の正常受理', () => { }; validateSavedFacts(summary); expect(parseFactSummary(JSON.stringify(summary), 'other', pages)).toEqual(summary); + const adjusted = summary.facts.filter((f) => f.provenance!.adjustments.length); + if (adjusted.length) { + const display = buildPresentation(summary, pages); + const markdown = renderFacts(summary, display); + const visible = markdown.split('\n## 原文\n')[0]; + for (const fact of adjusted) { + const observation = factObservation(fact, summary, display.excerpts, display.values); + expect(observation.sourceBasis).toEqual({ + denominator: fact.provenance!.denominator, + adjustments: fact.provenance!.adjustments, + }); + const row = visible + .split('\n') + .find((line) => line.startsWith('| ') && line.includes(literalMarkdown(fact.label)))!; + expect(row).toBeDefined(); + for (const adjustment of fact.provenance!.adjustments) { + expect(row).toContain(adjustmentLabels[adjustment.basis]); + expect(visible).toContain(literalMarkdown(adjustment.text)); + expect(observation.sourceIds).toContain(`source:${adjustment.noteId}`); + } + for (const basis of observationBasis(observation)) expect(row).toContain(basis); + if (observation.comparison) { + const comparison = summary.facts.find((f) => f.id === observation.comparison!.valueId)!; + expect(observation.comparisonSourceBasis).toEqual({ + denominator: comparison.provenance!.denominator, + adjustments: comparison.provenance!.adjustments, + }); + for (const basis of observationBasis(observation, true)) { + expect(row.split('前年同期')[1]).toContain(basis); + const headline = visible + .split('\n') + .find((line) => line.startsWith('- ') && line.includes(literalMarkdown(fact.label)))!; + expect(headline.split('前年同期')[0]).toContain(basis); + expect(headline.split('前年同期')[1]).toContain(basis); + } + } + } + const restored = revalidatePresentation(JSON.parse(JSON.stringify(display)), summary, pages); + expect(renderFacts(summary, restored)).toBe(markdown); + } if (fixture.id === 'holdout-remix-20260611') { const context = buildDocumentContext(pages); expect(() => diff --git a/src/lib/summary-narrative.test.ts b/src/lib/summary-narrative.test.ts index c9cab36..d9aefac 100644 --- a/src/lib/summary-narrative.test.ts +++ b/src/lib/summary-narrative.test.ts @@ -28,6 +28,7 @@ import { checkText, parseNarrativeResponse, } from './summary-narrative'; +import { buildAnalysisInput } from './analysis-input'; import { buildSummaryHtml } from '../content/utils/summaryHtmlBuilder'; import type { SummaryAttempt } from './summary-trace'; import { literalValue, quantityChange } from './summary-narrative-renderer'; @@ -265,7 +266,7 @@ describe('構造化を主とする表示と未整理部分の保持', () => { expect(reading).toContain(statement); expect(result.facts.facts[0].semantics.conditions).toContain(condition); expect(reading).toContain('当局の承認を条件とします'); - expect(reading).toContain('要約未作成'); + expect(reading).toContain('補足要約の未整理部分'); const savedFacts: unknown = JSON.parse(JSON.stringify(result.facts)); validateSavedFacts(savedFacts); const restored = revalidatePresentation( @@ -357,7 +358,7 @@ describe('構造化を主とする表示と未整理部分の保持', () => { '↑増加 約+50.0%', '↑増加(+10百万円)', '価格転嫁', - '要約未作成', + '補足要約の未整理部分', ]) expect(reading).toContain(expected); expect(reading).not.toContain('来期の増収が確定'); @@ -412,7 +413,7 @@ describe('構造化を主とする表示と未整理部分の保持', () => { expect(result.presentation.organization.claims).toEqual([]); expect(result.presentation.organization.observations).toEqual([]); expect(renderFacts(result.facts, result.presentation)).toContain('100百万円'); - expect(renderFacts(result.facts, result.presentation)).toContain('要約未作成'); + expect(renderFacts(result.facts, result.presentation)).toContain('補足要約の未整理部分'); expect(attempts[attempts.length - 1]?.error).toBe('説明点検の期限'); expect(vi.mocked(generateText)).toHaveBeenCalledTimes(3); }); @@ -531,13 +532,111 @@ describe('説明候補と独立点検の契約', () => { expect(() => validatePresentation(restored, facts)).toThrow('点検範囲'); }); - it.each<[string, (input: ReturnType) => void, string]>([ + it('生成時だけ既知の重複参照を正規化し、保存済みの重複は受け入れない', async () => { + const input = wire(); + for (const item of [...input.contexts, ...input.observations, ...input.claims]) + item.sourceIds = [...item.sourceIds, item.sourceIds[0]]; + const result = await organize(JSON.stringify(input)); + expect(result.observations).toHaveLength(input.observations.length); + expect(result.claims).toHaveLength(input.claims.length); + expect(result.issues).toEqual([]); + expect(vi.mocked(generateText)).toHaveBeenCalledTimes(2); + const restored = revalidatePresentation( + JSON.parse(JSON.stringify({ ...draft, organization: result })), + facts, + [page] + ); + expect(restored.organization).toEqual(result); + restored.organization.observations[0].sourceIds.push( + restored.organization.observations[0].sourceIds[0] + ); + expect(() => validatePresentation(restored, facts)).toThrow('保存形式'); + }); + + it('不正な未使用文脈で点検済みの説明範囲を未整理へ戻さない', async () => { + const baseline = await organize(); + const input = wire(); + input.contexts.push({ ...input.contexts[0], id: 'unused-invalid', sourceIds: [] }); + const result = await organize(JSON.stringify(input)); + expect(result.issues).toHaveLength(1); + expect(result.status).toBe('partial'); + expect(supportedExplanations(result)).toEqual(supportedExplanations(baseline)); + expect(unresolvedExplanationSources(result, draft.excerpts)).toEqual( + unresolvedExplanationSources(baseline, draft.excerpts) + ); + expect( + buildAnalysisInput(facts, { ...draft, organization: result }).coverage.unresolvedSources + ).toBe( + buildAnalysisInput(facts, { ...draft, organization: baseline }).coverage.unresolvedSources + ); + }); + + it.each<[string, (input: ReturnType) => void, string[], string]>([ [ '存在しない文脈ID', (v) => (v.observations[0].contextId = 'absent-context'), + ['observation-0'], + 'OBSERVATION_CONTEXT', + ], + [ + '重複する文脈ID', + (v) => v.contexts.push({ ...v.contexts[0] }), + ['observation-0', 'observation-1'], 'OBSERVATION_CONTEXT', ], - ['重複する文脈ID', (v) => v.contexts.push({ ...v.contexts[0] }), 'OBSERVATION_CONTEXT'], + [ + '未知の根拠ID', + (v) => (v.observations[0].sourceIds = [...sources, 'unknown-source']), + ['observation-0'], + 'EXPLANATION_SCHEMA', + ], + [ + '空の根拠', + (v) => (v.observations[0].sourceIds = []), + ['observation-0'], + 'EXPLANATION_SCHEMA', + ], + [ + '文字列以外の根拠', + (v) => Object.assign(v.observations[0], { sourceIds: [null] }), + ['observation-0'], + 'EXPLANATION_SCHEMA', + ], + [ + '未知の数量', + (v) => (v.observations[0].valueId = 'unknown-quantity'), + ['observation-0'], + 'REFERENCE', + ], + [ + '壊れた説明', + (v) => Object.assign(v.claims[0], { text: null }), + ['explanation-0'], + 'EXPLANATION_SCHEMA', + ], + ])('%sだけを除き、独立点検した残りを保存する', async (_, change, rejected, error) => { + const input = wire(); + change(input); + const verdict = review(); + verdict.claims = verdict.claims.filter((c) => !rejected.includes(c.id)); + const result = await organize(JSON.stringify(input), verdict); + expect(result.status).toBe('partial'); + expect(organizationClaims(result).map((c) => c.id)).toEqual( + expect.arrayContaining(verdict.claims.map((c) => c.id)) + ); + expect(organizationClaims(result)).toHaveLength(verdict.claims.length); + expect(result.issues.some((issue) => issue.reason.includes(error))).toBe(true); + expect(vi.mocked(generateText)).toHaveBeenCalledTimes(2); + expect( + revalidatePresentation( + JSON.parse(JSON.stringify({ ...draft, organization: result })), + facts, + [page] + ).organization + ).toEqual(result); + }); + + it.each<[string, (input: ReturnType) => void, string]>([ [ '指標数の上限超過', (v) => { diff --git a/src/lib/summary-organization.ts b/src/lib/summary-organization.ts index 570fc6b..f1ada0c 100644 --- a/src/lib/summary-organization.ts +++ b/src/lib/summary-organization.ts @@ -26,6 +26,7 @@ import { type DisclosureContext, observationLine, checkObservation, + reconcileObservations, type DisclosureObservation, type DisclosureExplanation, } from './disclosure-observation'; @@ -100,21 +101,36 @@ const verdicts = (value: unknown, keys: string[]): value is Record result.review!.claims[c.id] === null) : []; } -export function supportedObservations(result: SummaryOrganization): DisclosureObservation[] { - return result.review +export function reconciledOrganizationObservations( + result: SummaryOrganization, + facts: FactSummary, + values: NarrativeValue[], + excerpts: SourceExcerpt[] +) { + const reviewed = result.review ? result.observations.filter((value) => result.review!.claims[value.id] === null) : []; + const reconciled = reconcileObservations(facts, reviewed, excerpts, values); + const conflicts = new Set(reconciled.conflicts.map((o) => o.id)); + return { ...reconciled, accepted: reviewed.filter((o) => !conflicts.has(o.id)) }; +} +export function supportedObservations( + result: SummaryOrganization, + facts: FactSummary, + values: NarrativeValue[], + excerpts: SourceExcerpt[] +): DisclosureObservation[] { + return reconciledOrganizationObservations(result, facts, values, excerpts).accepted; } export function unresolvedExplanationSources( result: SummaryOrganization, excerpts: SourceExcerpt[] ): SourceExcerpt[] { + // Failed candidates do not undo coverage independently proved by surviving claims. const accepted = supportedExplanations(result); return explanationSources(excerpts).filter( (e) => - result.review?.sources[e.id] !== null || - !accepted.some((c) => c.sourceIds.includes(e.id)) || - result.issues.some((issue) => issue.sourceIds.includes(e.id)) + result.review?.sources[e.id] !== null || !accepted.some((c) => c.sourceIds.includes(e.id)) ); } /** Each numeric cell is tracked separately; one confirmed value does not consume a row. */ @@ -127,7 +143,7 @@ export function unresolvedTableSources( const shown = new Set( facts.facts.flatMap((f) => (f.evidence.kind === 'table' ? [f.evidence.valueId, f.id] : [f.id])) ); - for (const value of supportedObservations(result)) { + for (const value of supportedObservations(result, facts, values, excerpts)) { shown.add(value.valueId); if (value.comparison) { shown.add(value.comparison.valueId); @@ -156,9 +172,11 @@ function statusOf( facts: FactSummary, values: NarrativeValue[] ) { - if (!supportedExplanations(result).length && !supportedObservations(result).length) + const observations = reconciledOrganizationObservations(result, facts, values, excerpts); + if (!supportedExplanations(result).length && !observations.accepted.length) return 'unavailable' as const; return result.issues.length || + observations.conflicts.length || unresolvedExplanationSources(result, excerpts).length || unresolvedTableSources(result, facts, values, excerpts).length || Object.values(result.review?.claims ?? {}).some((verdict) => verdict !== null) @@ -460,57 +478,77 @@ export async function generateSummaryOrganization( draft.claims.length > ORGANIZATION_LIMITS.claims ) throw new Error('OBSERVATION_SCHEMA:共通文脈と指標候補の形式が不正です'); - const contexts = new Map(); - for (const context of draft.contexts) { + // Generation may repeat a known reference. Canonicalize only this boundary; + // cached records and every unknown, empty or non-string reference stay strict. + const normalizeRefs = (value: unknown): string[] => { if ( - !record(context) || - !exact(context, ['id', ...contextKeys]) || - typeof context.id !== 'string' || - !context.id.trim() || - contexts.has(context.id) || - !contextShape(context) || - !refs(context.sourceIds, sourceIds) + !Array.isArray(value) || + !value.length || + !value.every((id) => typeof id === 'string' && sourceIds.has(id)) ) - throw new Error('OBSERVATION_CONTEXT:共通文脈・根拠の形式が不正です'); - const { id, ...meaning } = context; - contexts.set(id, meaning as unknown as DisclosureContext); + throw new Error('EXPLANATION_SCHEMA:根拠IDの形式が不正です'); + return [...new Set(value)] as string[]; + }; + const candidateSources = (input: unknown): string[] => { + const ids = + record(input) && Array.isArray(input.sourceIds) + ? [ + ...new Set( + input.sourceIds.filter( + (id): id is string => typeof id === 'string' && sourceIds.has(id) + ) + ), + ] + : []; + return ids.length ? ids : [...sourceIds]; + }; + const contexts = new Map(); + // Ambiguous IDs invalidate every dependent item, never pick the first/last meaning. + const contextIds = draft.contexts.flatMap((v) => + record(v) && typeof v.id === 'string' ? [v.id] : [] + ); + for (const context of draft.contexts) { + try { + if ( + !record(context) || + !exact(context, ['id', ...contextKeys]) || + typeof context.id !== 'string' || + !context.id.trim() || + contextIds.filter((id) => id === context.id).length !== 1 || + !contextShape(context) + ) + throw new Error('OBSERVATION_CONTEXT:共通文脈・根拠の形式が不正です'); + const { id, ...meaning } = context; + contexts.set(id, { + ...meaning, + sourceIds: normalizeRefs(context.sourceIds), + } as DisclosureContext); + } catch (error) { + issue(error instanceof Error ? error.message : String(error), candidateSources(context)); + } } const contextOf = (id: unknown) => { const value = typeof id === 'string' ? contexts.get(id) : undefined; if (!value) throw new Error('OBSERVATION_CONTEXT:存在しない共通文脈IDです'); return value; }; - for (const input of draft.observations) { - if ( - !record(input) || - !exact(input, ['contextId', 'metric', 'measure', 'valueId', 'comparison', 'sourceIds']) || - !refs(input.sourceIds, sourceIds) || - (input.comparison !== null && - (!record(input.comparison) || - !exact(input.comparison, ['axis', 'contextId', 'valueId', 'rateId']))) - ) - throw new Error('OBSERVATION_SCHEMA:指標・比較の形式が不正です'); - contextOf(input.contextId); - if (input.comparison) contextOf(input.comparison.contextId); - } - for (const input of draft.claims) { - if ( - !record(input) || - !exact(input, ['contextId', 'text', 'sourceIds']) || - typeof input.text !== 'string' || - input.text.length > ORGANIZATION_LIMITS.text || - !refs(input.sourceIds, sourceIds) - ) - throw new Error('EXPLANATION_SCHEMA:説明の文脈・長さ・根拠が不正です'); - contextOf(input.contextId); - } for (const [index, input] of draft.observations.entries()) { - const context = contextOf(input.contextId); - const before = input.comparison ? contextOf(input.comparison.contextId) : null; - const proofIds = [ - ...new Set([...context.sourceIds, ...input.sourceIds, ...(before?.sourceIds ?? [])]), - ]; + let proofIds = candidateSources(input); try { + if ( + !record(input) || + !exact(input, ['contextId', 'metric', 'measure', 'valueId', 'comparison', 'sourceIds']) || + (input.comparison !== null && + (!record(input.comparison) || + !exact(input.comparison, ['axis', 'contextId', 'valueId', 'rateId']))) + ) + throw new Error('OBSERVATION_SCHEMA:指標・比較の形式が不正です'); + const inputSources = normalizeRefs(input.sourceIds); + const context = contextOf(input.contextId); + const before = input.comparison ? contextOf(input.comparison.contextId) : null; + proofIds = [ + ...new Set([...context.sourceIds, ...inputSources, ...(before?.sourceIds ?? [])]), + ]; if ( before && (!before.period || @@ -521,21 +559,22 @@ export async function generateSummaryOrganization( throw new Error( 'OBSERVATION_CONTEXT:異なる対象・範囲・基準の比較、または比較期が未特定です' ); - const observation: DisclosureObservation = { + const observation = { ...context, id: `observation-${index}`, metric: input.metric, measure: input.measure, valueId: input.valueId, - comparison: before - ? { - axis: input.comparison.axis, - period: before.period!, - state: before.state, - valueId: input.comparison.valueId, - rateId: input.comparison.rateId, - } - : null, + comparison: + before && input.comparison + ? { + axis: input.comparison.axis, + period: before.period!, + state: before.state, + valueId: input.comparison.valueId, + rateId: input.comparison.rateId, + } + : null, conditions: [...new Set([...context.conditions, ...(before?.conditions ?? [])])], sourceIds: proofIds, }; @@ -551,9 +590,18 @@ export async function generateSummaryOrganization( } } for (const [index, input] of draft.claims.entries()) { - const context = contextOf(input.contextId); - const ids = [...new Set([...context.sourceIds, ...input.sourceIds])]; + let ids = candidateSources(input); try { + if ( + !record(input) || + !exact(input, ['contextId', 'text', 'sourceIds']) || + typeof input.text !== 'string' || + input.text.length > ORGANIZATION_LIMITS.text + ) + throw new Error('EXPLANATION_SCHEMA:説明の文脈・長さ・根拠が不正です'); + const inputSources = normalizeRefs(input.sourceIds); + const context = contextOf(input.contextId); + ids = [...new Set([...context.sourceIds, ...inputSources])]; const text = bindLiteralQuantities(input.text, ids, values, excerpts); const conditions = context.conditions.map((text) => bindLiteralQuantities(text, ids, values, excerpts) diff --git a/src/lib/summary-presentation.test.ts b/src/lib/summary-presentation.test.ts index 440eacc..8be2c7e 100644 --- a/src/lib/summary-presentation.test.ts +++ b/src/lib/summary-presentation.test.ts @@ -94,17 +94,19 @@ describe('冒頭と本文の保持・復元・原文参照', () => { expect(overview).toContain('[p.1](tdnet-page:1)'); expect(overview).not.toContain(reasons[0]); const pending = nativePresentation(document, [first, second]); - const pendingOverview = renderFacts(document, pending).split('## 業績と増減要因')[0]; - expect(pendingOverview).toContain('要点の説明未作成'); + const pendingSummary = renderFacts(document, pending); + const pendingOverview = pendingSummary.split('## 業績と増減要因')[0]; + expect(pendingSummary).toContain('補足要約の未整理部分'); expect(pendingOverview).not.toContain(reasons[0]); expect( renderFacts(document, revalidatePresentation(supported, document, [first, second])) ).toBe(renderFacts(document, supported)); const incomplete = structuredClone(supported); incomplete.organization.review!.sources[selected] = '説明の一部が未要約'; - const incompleteOverview = renderFacts(document, incomplete).split('## 業績と増減要因')[0]; + const incompleteSummary = renderFacts(document, incomplete); + const incompleteOverview = incompleteSummary.split('## 業績と増減要因')[0]; expect(incompleteOverview).toContain('新店効果が売上を押し上げた。'); - expect(incompleteOverview).toContain('要点の説明に未整理部分'); + expect(incompleteSummary).toContain('補足要約の未整理部分'); }); it('目次・定型注意書き・記載省略・該当なしを冒頭の理由や条件にせず全文は保持する', () => { @@ -413,19 +415,59 @@ describe('冒頭と本文の保持・復元・原文参照', () => { expect(reading).toContain('開始時期と翌年度への影響は未定'); expect(reading).not.toContain('原文抜粋'); expect(reading).not.toContain('価格改定も行いました。'); + expect(reading).not.toContain('補足要約の未整理部分'); expect(html).toContain('価格改定も行いました。'); expect(revalidatePresentation(display, facts, [page])).toEqual(display); }); it('説明の内訳・混在した状態・未分類の施策を原文のまま保持する', () => { expect(sourceInventory([page]).map((e) => e.text)).toEqual(expectation.retained); expect(revalidatePresentation(presentation, facts, [page])).toEqual(presentation); - const body = renderFacts(facts, presentation).split('## 業績と増減要因')[1]; + const body = renderFacts(facts, presentation).split('\n## 業績と増減要因\n')[1]; expect(body).toContain('価格改定も行いました。'); expect(body).toContain('承認を条件に実施する予定です。'); expect(body).toContain('詳細は未定です。'); // Literal source quotations do not become verified semantic facts used for scoring. expect(facts.facts).toHaveLength(2); }); + it('未整理の説明と表を章・ページ別に一度だけ知らせ、全原文を末尾の一つのトグルへ残す', () => { + const table = cells( + [ + ['4.キャッシュ・フロー', 0, 10, 180], + ['営業活動', 0, 40, 70], + ['50百万円', 150, 40, 60], + ['40百万円', 250, 40, 60], + ], + 2 + ); + const display = nativePresentation(facts, [page, table]); + const summary = renderFacts(facts, display); + const coverage = summary.split('## 補足要約の未整理部分\n')[1].split('\n## 原文\n')[0]; + expect(coverage.match(/^- 業績と増減要因:/gm)).toHaveLength(1); + expect(coverage).toContain('説明 [p.1](tdnet-page:1)'); + expect(coverage).toContain('財政状態・資金の動き:数値・表 [p.2](tdnet-page:2)'); + expect(summary.match(/^## 補足要約の未整理部分$/gm)).toHaveLength(1); + expect(summary).not.toContain('要約未作成'); + expect(summary).not.toContain('\n## 財政状態・資金の動き\n'); + const html = buildSummaryHtml(summary, null, { + companyName: 'テスト', + title: '決算', + pdfUrl: 'https://www.release.tdnet.info/inbs/test.pdf', + }); + const toggles = html.match(/
]*>[\s\S]*?<\/details>/g)!; + expect(toggles).toHaveLength(1); + expect(toggles[0]).not.toMatch(/^]*\bopen\b/); + expect(toggles[0]).toContain('価格改定も行いました。'); + expect(toggles[0]).toContain('50百万円'); + expect(toggles[0]).toContain('test.pdf#page=2'); + expect(toggles[0]).not.toContain(' { const brief = { ...presentation, overview: [facts.facts[0].id] }; const body = (text: string) => text.slice(text.indexOf('## 業績と増減要因')); diff --git a/src/lib/summary-renderer.ts b/src/lib/summary-renderer.ts index fdb989c..93a6fd0 100644 --- a/src/lib/summary-renderer.ts +++ b/src/lib/summary-renderer.ts @@ -4,13 +4,12 @@ import type { SourceExcerpt } from './summary-source-inventory'; import { renderNarrativeText } from './summary-narrative-renderer'; import { supportedExplanations, - supportedObservations, + reconciledOrganizationObservations, unresolvedExplanationSources, unresolvedTableSources, } from './summary-organization'; import { OBSERVATION_TOPICS, - reconcileObservations, factPeriodName as periodText, canPair, observationChange, @@ -18,6 +17,9 @@ import { observationRole, observationTitles, comparisonAxisLabels, + observationBasis, + factObservation, + type ConfirmedObservation, type DisclosureObservation, type ObservationTopic, } from './disclosure-observation'; @@ -119,22 +121,35 @@ function overviewGrowth(f: VerifiedFact, comparison: SummaryComparison, facts: F function overviewNumber( f: VerifiedFact, facts: FactSummary, - _presentation: SummaryPresentation + presentation: SummaryPresentation, + observation: ConfirmedObservation ): string { + const basis = observationBasis(observation); + const metric = literalMarkdown(f.label); + const currentValue = numberText(f) + (basis.length ? `(${basis.join('、')})` : ''); + if (observation.unresolved) return `${metric}:${currentValue}(指標区分・比較は未確認)`; const comparison = summaryComparison(f, facts.facts); if (comparison) { const before = comparison.reference; + const previousBasis = observationBasis(observation, true); + const previousValue = + numberText(before) + (previousBasis.length ? `(${previousBasis.join('、')})` : ''); const values = comparison.axis === 'revision' - ? `${numberText(before)} → ${numberText(f)}` - : `${numberText(f)}(${f.semantics.periodKind === 'fullYear' ? '前期' : '前年同期'} ${numberText(before)})`; + ? `${previousValue} → ${currentValue}` + : `${currentValue}(${f.semantics.periodKind === 'fullYear' ? '前期' : '前年同期'} ${previousValue})`; const percent = overviewGrowth(f, comparison, facts).text; + const interpretation = + observation.measure !== + factObservation(f, facts, presentation.excerpts, presentation.values).measure + ? observationChange(observation, presentation.values).text + : comparisonLabel(f, comparison) + percent; const rate = facts.facts.find((r) => canPair(f, r)); - return `${literalMarkdown(f.label)}:**${comparisonLabel(f, comparison)}${literalMarkdown(percent)}** ${values}${comparison.axis === 'revision' && rate ? `(原文の増減率 ${numberText(rate)})` : ''}`; + return `${metric}:**${comparison.axis === 'revision' ? comparisonLabel(f, comparison) + literalMarkdown(percent) : literalMarkdown(interpretation)}** ${values}${comparison.axis === 'revision' && rate ? `(原文の増減率 ${numberText(rate)})` : ''}`; } const rate = facts.facts.find((r) => canPair(f, r)); - if (rate) return `${literalMarkdown(f.label)}:${numberText(f)}(比率 ${numberText(rate)})`; - return `${literalMarkdown(f.label)}:${numberText(f)}${f.semantics.state === 'actual' || f.semantics.state === 'forecastAfter' ? `(${comparisonIssue(f, facts.facts)})` : ''}`; + if (rate) return `${metric}:${currentValue}(比率 ${numberText(rate)})`; + return `${metric}:${currentValue}${f.semantics.state === 'actual' || f.semantics.state === 'forecastAfter' ? `(${comparisonIssue(f, facts.facts)})` : ''}`; } function overviewStatement(f: VerifiedFact, facts: FactSummary): string { const text = statementText(f); @@ -176,7 +191,7 @@ function renderExcerpts(excerpts: SourceExcerpt[]): string[] { paragraph.push(literalMarkdown(e.text)); } else { flush(); - if (e.kind === 'heading') lines.push('', `#### ${literalMarkdown(e.text)}`); + if (e.kind === 'heading') lines.push('', `##### ${literalMarkdown(e.text)}`); else { if (!same || previous?.kind !== 'row') lines.push(''); lines.push(literalMarkdown(e.text)); @@ -190,13 +205,13 @@ function renderExcerpts(excerpts: SourceExcerpt[]): string[] { } /** Layout is a projection of semantic records; no caption/header parsing. */ function renderObservationGroups( - records: DisclosureObservation[], + records: Array, presentation: SummaryPresentation, primarySubject: string | null, headings: boolean ): string[] { const lines: string[] = []; - const groups = new Map(); + const groups = new Map>(); for (const observation of records) { const key = observationGroup(observation); groups.set(key, [...(groups.get(key) ?? []), observation]); @@ -244,14 +259,18 @@ function renderObservationGroups( const previous = comparison ? presentation.values.find((q) => q.id === comparison.valueId)! : null; + const previousBasis = observationBasis(value, true); const cells = [ ...(subject ? [literalMarkdown(value.entity ?? '全社')] : []), ...(periods.length > 1 ? [literalMarkdown(value.period ?? '対象期未特定')] : []), ...(states.length > 1 ? [stateLabels[value.state]] : []), - literalMarkdown(value.metric), + literalMarkdown( + value.metric + + (observationBasis(value).length ? `(${observationBasis(value).join('、')})` : '') + ), literalMarkdown(literalValue(quantity)), comparison - ? `${comparisonAxisLabels[comparison.axis]} ${literalMarkdown(comparison.period)} ${literalMarkdown(literalValue(previous!))}` + ? `${comparisonAxisLabels[comparison.axis]} ${literalMarkdown(comparison.period)} ${literalMarkdown(literalValue(previous!))}${previousBasis.length ? `(${previousBasis.join('、')})` : ''}` : '', literalMarkdown( observationChange(value, presentation.values).text || @@ -261,6 +280,22 @@ function renderObservationGroups( lines.push(`| ${cells.join(' | ')} |`); } for (const condition of first.conditions) lines.push(`- 比較条件:${text(condition)}`); + const notes = new Map( + group.flatMap((value) => + 'sourceBasis' in value + ? [ + ...value.sourceBasis.adjustments, + ...(value.comparisonSourceBasis?.adjustments ?? []), + ].map((a) => [a.noteId, a] as const) + : [] + ) + ); + for (const note of notes.values()) { + const source = presentation.excerpts.find((e) => e.blockId === note.noteId); + lines.push( + `- 比較条件(原文):${literalMarkdown(note.text)}${source ? ` ${ref(source.page)}` : ''}` + ); + } lines.push( '', `根拠:${references(group.flatMap((value) => value.sourceIds.map((id) => presentation.excerpts.find((e) => e.id === id)!.page)))}` @@ -274,6 +309,15 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat validatePresentation(presentation, facts); const byId = new Map(facts.facts.map((f) => [f.id, f])); const sources = new Map(presentation.excerpts.map((e) => [e.id, e])); + const organization = presentation.organization; + const accepted = supportedExplanations(organization); + const reconciled = reconciledOrganizationObservations( + organization, + facts, + presentation.values, + presentation.excerpts + ); + const observations = reconciled.accepted; const single = (field: 'subject' | 'scope' | 'basis') => { const values = [...new Set(facts.facts.map((f) => f.semantics[field]).filter(Boolean))]; return values.length === 1 ? values[0] : null; @@ -300,17 +344,8 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat lines.push('', current, ''); previousContext = current; } - lines.push('- ' + overviewNumber(f, facts, presentation)); + lines.push('- ' + overviewNumber(f, facts, presentation, reconciled.primary.get(f.id)!)); } - const organization = presentation.organization; - const accepted = supportedExplanations(organization); - const observations = supportedObservations(organization); - const reconciled = reconcileObservations( - facts, - observations, - presentation.excerpts, - presentation.values - ); const unresolved = new Set( unresolvedExplanationSources(organization, presentation.excerpts).map((e) => e.id) ); @@ -354,12 +389,10 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat `根拠:${references(claim.sourceIds.map((sourceId) => sources.get(sourceId)!.page))}` ); } - if (!claims.length || unresolved.has(source.id)) { + if (fact && (!claims.length || unresolved.has(source.id))) { const section = presentation.sections.find((s) => s.excerptIds.includes(source.id))!; lines.push( - fact - ? `- 確認済み事項(原文):「${literalMarkdown(section.title)}」に記載 ${ref(fact.page)}${unresolved.has(source.id) ? '(説明は未整理)' : ''}` - : `- ${claims.length ? '要点の説明に未整理部分' : '要点の説明未作成'}:${literalMarkdown(source.heading?.text ?? section.title)} ${ref(source.page)}(原文を見る)` + `- 確認済み事項(原文):「${literalMarkdown(section.title)}」に記載 ${ref(fact.page)}` ); } } @@ -407,7 +440,6 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat const primarySubject = single('subject'); // Every accepted fact has a body location, independently of organization output. for (const section of sections) { - const excerpts = section.excerptIds.map((id) => sources.get(id)!); const members = section.factIds.map((id) => byId.get(id)!); const sectionClaims = accepted.filter( (value) => destination(value.topic, value.sourceIds) === section.title @@ -415,6 +447,7 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat const sectionObservations = reconciled.supplement.filter( (value) => destination(value.topic, value.sourceIds) === section.title ); + if (!members.length && !sectionClaims.length && !sectionObservations.length) continue; lines.push('', `## ${literalMarkdown(section.title)}`); const numericRows = members.filter(numeric).map((fact) => reconciled.primary.get(fact.id)!); const referencesInPairs = new Set( @@ -498,25 +531,11 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat lines.push('- 条件:' + text(condition)); } } - const residual = excerpts.filter((e) => unresolved.has(e.id)); - const headings = new Map(); - for (const e of residual) { - const heading = e.heading?.text ?? section.title; - headings.set(heading, [...(headings.get(heading) ?? []), e.page]); - } - for (const [heading, pages] of headings) - lines.push(`- 要約未作成:${literalMarkdown(heading)} ${references(pages)}`); - const remainingRows = excerpts.filter((e) => unresolvedTables.has(e.id)); - if (remainingRows.length) - lines.push( - `- 未整理の数値・表:${references(remainingRows.map((e) => e.page))}(原文を見る)` - ); if (sectionClaims.length) lines.push( '', `根拠:${references(sectionClaims.flatMap((c) => c.sourceIds.map((id) => sources.get(id)!.page)))}` ); - if (excerpts.length) lines.push('', '### 原文を見る', ...renderExcerpts(excerpts)); } if ( overviewFacts.some((f) => { @@ -559,5 +578,40 @@ export function renderSummary(facts: FactSummary, presentation: SummaryPresentat ...facts.unverified.map((s) => '- ' + literalMarkdown(s)) ); } + // Disclose missing coverage once, without interleaving unorganized source material + // with confirmed facts. The same Markdown remains complete when copied or restored. + if (unresolved.size || unresolvedTables.size || reconciled.conflicts.length) { + lines.push( + '', + '## 補足要約の未整理部分', + '', + '以下には要約に反映できていない説明・数値があります。末尾の原文を確認してください。' + ); + for (const metric of new Set(reconciled.conflicts.map((c) => c.metric))) + lines.push( + `- 補足指標の未整理:${literalMarkdown(metric)}の指標区分・期間・比較に不一致があります。確認済みの数値は本文に表示しています` + ); + for (const section of sections) { + const excerpts = section.excerptIds.map((id) => sources.get(id)!); + const remaining = [ + ['説明', unresolved], + ['数値・表', unresolvedTables], + ] as const; + const coverage = remaining.flatMap(([label, ids]) => { + const pages = excerpts.filter((e) => ids.has(e.id)).map((e) => e.page); + return pages.length ? [`${label} ${references(pages)}`] : []; + }); + if (coverage.length) + lines.push(`- ${literalMarkdown(section.title)}:${coverage.join('/')}`); + } + } + if (presentation.excerpts.length) { + lines.push('', '## 原文', '', '### 原文を見る'); + for (const section of sections) { + const excerpts = section.excerptIds.map((id) => sources.get(id)!); + if (excerpts.length) + lines.push('', `#### ${literalMarkdown(section.title)}`, ...renderExcerpts(excerpts)); + } + } return lines.join('\n'); } diff --git a/src/types/summaryMetadata.ts b/src/types/summaryMetadata.ts index 9b3ae0c..a059d89 100644 --- a/src/types/summaryMetadata.ts +++ b/src/types/summaryMetadata.ts @@ -34,6 +34,7 @@ export interface QualityWarning { } export interface SummaryMetadata { + persistenceWarning?: string; documentHash?: string; totalPages: number; extractedPages: number[];