fix(research): verify revision acknowledgements before clearing drafts (RP30)

This commit is contained in:
Boki 2026-10-28 09:10:46 -04:00
parent dd8955ecbf
commit e4056c7d13
7 changed files with 434 additions and 21 deletions

View file

@ -38,6 +38,7 @@ import type {
ResearchArtifact,
ResearchArtifactRevision,
ResearchFindingSource,
ResearchHypothesis,
ResearchHypothesisRevision,
} from '@stock-bot/types';
import {
@ -2187,6 +2188,147 @@ test('canonical_native_smoke', async () => {
}
});
test('revision_acknowledgements_preserve_drafts_and_reopen_exact_committed_history_browser', async () => {
const f = await fixture(true);
try {
await f.login();
await createCanonicalInBrowser(f, 'Revision acknowledgement oracle', true);
const runId = f.page.url().split('/').at(-1)!;
f.dispatcher.start();
await visible(f.page, 'Canonical research result');
const native = (await f.request(`/api/v2/runs/${runId}/results`)) as CanonicalRunResult;
const { project } = await f.request('/api/v2/research-projects', 'POST', {
title: 'Revision acknowledgement recovery',
});
const { hypothesis } = (await f.request(
`/api/v2/research-projects/${project.id}/hypotheses`,
'POST',
{
title: 'Original question',
proposition: 'An unvalidated question.',
validationCriteria: 'Retain untouched holdouts.',
contentOrigin: 'ai',
}
)) as { hypothesis: ResearchHypothesis };
const { artifact } = (await f.request(
`/api/v2/research-projects/${project.id}/analysis-artifacts`,
'POST',
{
kind: 'finding',
hypothesisRevisionId: hypothesis.latestRevision.id,
title: 'Original finding',
body: 'An unvalidated interpretation.',
contentOrigin: 'human',
evidence: [
{
runId,
strategyVersionId: native.request.strategy.versionId,
metricsVersion: 'canonical-analytics/v1',
metricPath: 'analytics.metrics.totalReturn',
},
],
}
)) as { artifact: ResearchArtifact };
const counts = async () =>
(
await f.pool.query(`SELECT
(SELECT count(*)::int FROM runs) AS runs,
(SELECT count(*)::int FROM backtest_dispatch) AS dispatch,
(SELECT count(*)::int FROM research_hypothesis_revisions) AS hypotheses,
(SELECT count(*)::int FROM analysis_artifact_revisions) AS artifacts`)
).rows[0];
expect(await counts()).toEqual({ runs: 1, dispatch: 1, hypotheses: 1, artifacts: 1 });
f.page.on('dialog', event => void event.accept());
for (const [kind, original] of [
['hypothesis', hypothesis],
['artifact', artifact],
] as const) {
const path = kind === 'hypothesis' ? 'hypotheses' : 'analysis-artifacts';
const endpoint = `**/api/v2/${path}/${original.id}/revisions`;
await f.page.goto(`${f.origin}/research/${project.id}?${kind}=${original.id}`);
await f.page.getByRole('button', { name: 'Edit latest revision', exact: true }).click();
const title = ` Revised ${kind} `;
const body = ` Retained ${kind} draft.\n`;
await f.page.getByLabel('Revision title', { exact: true }).fill(title);
await f.page.getByLabel('Revision body', { exact: true }).fill(body);
await f.page.getByLabel('Revision content origin', { exact: true }).selectOption('ai');
if (kind === 'hypothesis') {
await f.page
.getByLabel('Revision validation criteria', { exact: true })
.fill('Additional untouched validation.');
}
let posts = 0;
let committed: ResearchHypothesis | ResearchArtifact | undefined;
await f.page.route(endpoint, async route => {
if (route.request().method() !== 'POST') {
await route.continue();
return;
}
posts++;
const response = await route.fetch();
expect(response.status()).toBe(201);
const payload = (await response.json()) as {
hypothesis: ResearchHypothesis;
artifact: ResearchArtifact;
};
committed = structuredClone(payload[kind]);
// The write commits unchanged. Only the browser acknowledgement is corrupted.
if (kind === 'hypothesis') {
payload.hypothesis.latestRevision.ordinal = original.latestRevision.ordinal;
} else {
payload.artifact.latestRevision.evidence = [];
}
await route.fulfill({ response, json: payload });
});
await f.page.getByRole('button', { name: 'Save new revision', exact: true }).click();
await visible(f.page, 'The saved revision response could not be verified.');
await visible(f.page, /The draft is preserved/);
expect(await f.page.getByLabel('Revision title', { exact: true }).inputValue()).toBe(title);
expect(await f.page.getByLabel('Revision body', { exact: true }).inputValue()).toBe(body);
expect(await f.page.getByLabel('Revision content origin', { exact: true }).inputValue()).toBe(
'ai'
);
expect(
await f.page.getByRole('button', { name: 'Save new revision', exact: true }).isDisabled()
).toBe(true);
expect(posts).toBe(1);
if (!committed) {
throw new Error('Expected the ordinary revision write to commit');
}
expect(committed.latestRevision.ordinal).toBe(2);
expect(committed.latestRevision.title).toBe(title.trim());
await f.page.unroute(endpoint);
await f.page
.getByRole('button', { name: 'Discard draft and reload history', exact: true })
.click();
await f.page.getByRole('button', { name: 'View revision 2', exact: true }).waitFor();
await visible(f.page, title.trim());
expect(
await f.page.getByRole('form', { name: 'Revise research entry', exact: true }).count()
).toBe(0);
const history = await f.request(`/api/v2/${path}/${original.id}/revisions`);
expect(history.revisions).toEqual([original.latestRevision, committed.latestRevision]);
if ('hypothesisRevisionId' in committed) {
expect(committed.latestRevision.evidence).toEqual(artifact.latestRevision.evidence);
expect(committed.hypothesisRevisionId).toBe(hypothesis.latestRevision.id);
}
await f.page.reload();
await f.page.getByRole('button', { name: 'View revision 2', exact: true }).waitFor();
expect(posts).toBe(1);
expect(await counts()).toEqual({
runs: 1,
dispatch: 1,
hypotheses: 2,
artifacts: kind === 'artifact' ? 2 : 1,
});
}
expect(await f.request(`/api/v2/runs/${runId}/results`)).toEqual(native);
expect(f.unexpectedNetwork).toEqual([]);
} finally {
await f.close();
}
});
test('single_run_finding_acknowledgement_blocks_duplicates_and_reopens_committed_evidence_browser', async () => {
const f = await fixture(true);
try {