From 32f40ee753081612dc83f1bef753e218b50c7bf6 Mon Sep 17 00:00:00 2001 From: Boki Date: Tue, 27 Oct 2026 14:14:30 -0400 Subject: [PATCH] fix(research): preserve verified hypothesis links after partial lookup failure (RP11) --- .../research/components/ResearchEntry.tsx | 31 +++--- test/e2e/workbench.test.ts | 23 +++++ .../research-workspace-ui.test.tsx | 99 +++++++++++++++++++ 3 files changed, 136 insertions(+), 17 deletions(-) diff --git a/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx b/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx index 14acffd3..faa0a557 100644 --- a/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx +++ b/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx @@ -55,7 +55,7 @@ function HypothesisReference({ if (known) { setHypothesisId(known.id); } else { - void Promise.all( + void Promise.allSettled( candidates.map(async item => { const { revisions } = await researchApi.hypothesisRevisions(item.id, controller.signal); return revisions.some( @@ -64,25 +64,22 @@ function HypothesisReference({ ? item.id : null; }) - ) - .then(ids => { - if (current) { - const found = ids.find(Boolean) ?? null; - setHypothesisId(found); - if (!found) { - setError( - 'The linked hypothesis revision is unavailable in the loaded project history.' - ); - } - } - }) - .catch(() => { - if (current) { + ).then(results => { + if (current) { + const ids = results.flatMap(result => + result.status === 'fulfilled' ? [result.value] : [] + ); + const found = ids.find(Boolean) ?? null; + setHypothesisId(found); + if (!found) { setError( - 'Could not resolve the linked hypothesis history. Refresh the project to try again.' + results.some(result => result.status === 'rejected') + ? 'Could not resolve the linked hypothesis history. Refresh the project to try again.' + : 'The linked hypothesis revision is unavailable in the loaded project history.' ); } - }); + } + }); } return () => { current = false; diff --git a/test/e2e/workbench.test.ts b/test/e2e/workbench.test.ts index c344f1c9..aabb2ee7 100644 --- a/test/e2e/workbench.test.ts +++ b/test/e2e/workbench.test.ts @@ -3456,12 +3456,35 @@ test('research_project_to_finding_browser_with_canonical_strategy_setup_run_and_ await f.page.getByRole('button', { name: 'View revision 1', exact: true }).click(); await visible(f.page, 'Costs and losses require further validation.'); await visible(f.page, 'Immutable history: this revision is read-only.'); + // Move the cited hypothesis forward so the exact link requires history resolution. + await f.request(`/api/v2/hypotheses/${hypothesis.id}/revisions`, 'POST', { + expectedRevision: 1, + title: 'Revised AI trend hypothesis', + proposition: 'A revised hypothesis, still not validated alpha.', + validationCriteria: 'Out-of-sample evidence required.', + contentOrigin: 'ai', + }); + const { hypothesis: unrelatedHypothesis } = await f.request( + `/api/v2/research-projects/${project.id}/hypotheses`, + 'POST', + { title: 'Unrelated hypothesis', proposition: 'Separate future investigation.' } + ); + const unrelatedHistoryPath = `**/api/v2/hypotheses/${unrelatedHypothesis.id}/revisions`; + await f.page.route(unrelatedHistoryPath, route => + route.fulfill({ status: 503, json: { error: 'Unrelated history unavailable' } }) + ); + const unrelatedHistoryFailure = f.page.waitForResponse(response => + response.url().endsWith(`/hypotheses/${unrelatedHypothesis.id}/revisions`) + ); await f.page.reload(); + expect((await unrelatedHistoryFailure).status()).toBe(503); await visible(f.page, 'Costs and losses require further validation.'); await f.page.getByRole('link', { name: 'Open exact hypothesis revision', exact: true }).click(); await f.page.waitForURL( `${f.origin}/research/${project.id}?hypothesis=${hypothesis.id}&revision=${hypothesis.latestRevision.id}` ); + await f.page.unroute(unrelatedHistoryPath); + await visible(f.page, 'A hypothesis, not validated alpha.'); await visible(f.page, /AI-authored \(declared\).*Unvalidated narrative/); await f.page.getByRole('button', { name: 'Archive project', exact: true }).click(); await visible(f.page, 'Archived project: research entries are read-only.'); diff --git a/test/integration/research-workspace-ui.test.tsx b/test/integration/research-workspace-ui.test.tsx index 9198fb00..1658a628 100644 --- a/test/integration/research-workspace-ui.test.tsx +++ b/test/integration/research-workspace-ui.test.tsx @@ -627,6 +627,105 @@ describe('research workspace', () => { [...host.querySelectorAll('button')].some(item => item.textContent === 'Edit latest revision') ).toBe(false); }); + for (const failureFirst of [false, true]) { + test(`exact hypothesis link survives unrelated history failure (${failureFirst ? 'failure first' : 'match first'})`, async () => { + const original = hypothesis().latestRevision; + const linked = hypothesis('h1', { + latestRevision: { ...original, id: 'h1-r2', ordinal: 2 }, + }); + const finding = artifact('a1', { kind: 'finding', hypothesisRevisionId: original.id }); + await mount('/research/p1?artifact=a1'); + await flush(() => replyProject(project(), [linked, hypothesis('h2')], [finding])); + await flush(() => replyArtifact(finding)); + const match = () => + reply(request('hypotheses/h1/revisions'), { + revisions: [original, linked.latestRevision], + }); + const failure = () => + reply(request('hypotheses/h2/revisions'), { error: 'Temporary history failure' }, 503); + await flush(failureFirst ? failure : match); + await flush(failureFirst ? match : failure); + expect( + host.querySelector('a[href="/research/p1?hypothesis=h1&revision=h1-r1"]') + ).not.toBeNull(); + expect(host.textContent).not.toContain('Could not resolve the linked hypothesis history'); + expect(host.textContent).not.toContain('unavailable in the loaded project history'); + }); + } + test('incomplete hypothesis lookup remains retryable through project refresh', async () => { + const original = hypothesis().latestRevision; + const linked = hypothesis('h1', { + latestRevision: { ...original, id: 'h1-r2', ordinal: 2 }, + }); + const unrelated = hypothesis('h2'); + const finding = artifact('a1', { kind: 'finding', hypothesisRevisionId: original.id }); + await mount('/research/p1?artifact=a1'); + await flush(() => replyProject(project(), [linked, unrelated], [finding])); + await flush(() => replyArtifact(finding)); + await flush(() => { + reply(request('hypotheses/h1/revisions'), { error: 'Temporary history failure' }, 503); + reply(request('hypotheses/h2/revisions'), { revisions: [unrelated.latestRevision] }); + }); + expect(host.textContent).toContain('Could not resolve the linked hypothesis history'); + expect(host.textContent).not.toContain('unavailable in the loaded project history'); + expect(host.querySelector('a[href*="?hypothesis=h1&revision=h1-r1"]')).toBeNull(); + await flush(() => click('Refresh project')); + await flush(() => replyProject(project(), [linked, unrelated], [finding])); + await flush(() => { + reply(request('hypotheses/h1/revisions'), { revisions: [original, linked.latestRevision] }); + reply(request('hypotheses/h2/revisions'), { revisions: [unrelated.latestRevision] }); + }); + expect(host.textContent).not.toContain('Could not resolve the linked hypothesis history'); + expect( + host.querySelector('a[href="/research/p1?hypothesis=h1&revision=h1-r1"]') + ).not.toBeNull(); + }); + test('fully read hypothesis history distinguishes absence and rejects a foreign revision owner', async () => { + const finding = artifact('a1', { kind: 'finding', hypothesisRevisionId: 'missing-r1' }); + await mount('/research/p1?artifact=a1'); + await flush(() => replyProject(project(), [hypothesis()], [finding])); + await flush(() => replyArtifact(finding)); + await flush(() => + reply(request('hypotheses/h1/revisions'), { + revisions: [ + hypothesis().latestRevision, + { ...hypothesis('h2').latestRevision, id: 'missing-r1' }, + ], + }) + ); + expect(host.textContent).toContain('unavailable in the loaded project history'); + expect(host.textContent).not.toContain('Could not resolve the linked hypothesis history'); + expect(host.querySelector('a[href*="?hypothesis="]')).toBeNull(); + }); + for (const destination of ['finding', 'project'] as const) { + test(`late hypothesis resolution cannot attach to a different ${destination}`, async () => { + const original = hypothesis().latestRevision; + const linked = hypothesis('h1', { + latestRevision: { ...original, id: 'h1-r2', ordinal: 2 }, + }); + const finding = artifact('a1', { kind: 'finding', hypothesisRevisionId: original.id }); + await mount('/research/p1?artifact=a1'); + await flush(() => replyProject(project(), [linked, hypothesis('h2')], [finding])); + await flush(() => replyArtifact(finding)); + const previousMatch = request('hypotheses/h1/revisions'); + const previousFailure = request('hypotheses/h2/revisions'); + const nextProjectId = destination === 'project' ? 'p2' : 'p1'; + const next = artifact('a2', { projectId: nextProjectId, kind: 'finding' }); + await flush(() => navigate(`/research/${nextProjectId}?artifact=a2`)); + if (destination === 'project') { + await flush(() => replyProject(project('p2'), [], [next])); + } + await flush(() => replyArtifact(next)); + await flush(() => { + reply(previousMatch, { revisions: [original, linked.latestRevision] }); + reply(previousFailure, { error: 'Temporary history failure' }, 503); + }); + expect(location).toBe(`/research/${nextProjectId}?artifact=a2`); + expect(host.querySelector('a[href*="?hypothesis="]')).toBeNull(); + expect(host.textContent).not.toContain('Linked hypothesis revision:'); + expect(host.textContent).not.toContain('Could not resolve the linked hypothesis history'); + }); + } test('late entry read and late revision success cannot overwrite a newly selected entry', async () => { await mount('/research/p1?hypothesis=h1'); await flush(() => replyProject());