fix(research): verify creation acknowledgements before leaving drafts (RP31)

This commit is contained in:
Boki 2026-10-28 09:27:39 -04:00
parent b09b2de406
commit 57ba86d035
8 changed files with 552 additions and 22 deletions

View file

@ -164,7 +164,15 @@ const artifact = (hypothesisRevisionId: string | null = null): ResearchArtifact
});
function acceptedFinding(hypothesisRevisionId: string | null = null): ResearchArtifact {
const saved = artifact(hypothesisRevisionId);
const input = post().body as { evidence: ResearchEvidenceReference[] };
const input = post().body as {
title: string;
body: string;
contentOrigin: 'human' | 'ai' | 'unknown';
evidence: ResearchEvidenceReference[];
};
saved.latestRevision.title = input.title;
saved.latestRevision.body = input.body;
saved.latestRevision.contentOrigin = input.contentOrigin;
saved.latestRevision.source = null;
saved.latestRevision.evidence = input.evidence.map(reference => ({
...reference,
@ -411,6 +419,43 @@ describe('saved-result finding', () => {
expect(host.textContent).toContain('narrative remains unvalidated');
expect(closed).toBe(0);
});
test.each([
['title', { title: 'Substituted narrative' }],
['body', { body: 'Substituted narrative' }],
['origin', { contentOrigin: 'unknown' }],
['parent', { artifactId: 'different-entry' }],
['noninitial ordinal', { ordinal: 2 }],
['validated state', { validationState: 'validated' }],
['evidence state', { evidenceState: 'none' }],
] as const)('%s narrative acknowledgement freezes the finding draft', async (_label, change) => {
await mountDialog();
await draft();
await preflight();
const saved = acceptedFinding();
await flush(() =>
reply(
post(),
{ artifact: { ...saved, latestRevision: { ...saved.latestRevision, ...change } } },
201
)
);
expect(host.textContent).toContain('saved finding response could not be verified');
expect(host.textContent).not.toContain('Finding saved.');
expect(field('Finding title').value).toBe('Exact finding');
expect(field('Finding body').value).toBe('Keep this narrative');
expect(button('Save finding').disabled).toBe(true);
expect(host.querySelector('a[href*="artifact=saved-finding"]')).toBeNull();
await flush(() => button('Save finding').click());
expect(requests.filter(item => item.method === 'POST')).toHaveLength(1);
});
test('archived creation acknowledgement cannot claim a saved finding', async () => {
await mountDialog();
await draft();
await preflight();
await flush(() => reply(post(), { artifact: { ...acceptedFinding(), archived: true } }, 201));
expect(host.textContent).toContain('saved finding response could not be verified');
expect(button('Save finding').disabled).toBe(true);
});
test.each([
'empty',
'missing',

View file

@ -759,6 +759,9 @@ function sourceFor(value = standalone()) {
}
function savedArtifact(body: ArtifactInput): ResearchArtifact {
const value = artifact();
value.latestRevision.title = body.title;
value.latestRevision.body = body.body;
value.latestRevision.contentOrigin = body.contentOrigin ?? 'human';
value.latestRevision.source = body.source ?? null;
value.latestRevision.evidence = (body.evidence ?? []).map(reference => ({
...reference,

View file

@ -13,6 +13,7 @@ import { act, createElement } from 'react';
import type { Root } from 'react-dom/client';
import type {
ResearchArtifact,
ResearchContentOrigin,
ResearchHypothesis,
ResearchProject,
StrategyRecord,
@ -270,6 +271,77 @@ const revisionDraft = {
body: ' Retain the revised interpretation.\n',
criteria: ' Keep untouched holdouts.\n',
};
const creationDraft = {
title: ' New research title ',
body: ' Preserve this new proposal.\n',
criteria: ' Keep untouched validation.\n',
};
async function submitCreation(
kind: 'project' | 'hypothesis' | 'note',
origin: ResearchContentOrigin = 'ai',
body = creationDraft.body
) {
await mount(kind === 'project' ? '/research' : '/research/p1');
await flush(() => {
if (kind === 'project') {
reply(request('research-projects?includeArchived=false'), { projects: [] });
} else {
replyProject();
}
});
await flush(() => click(`New ${kind}`));
await flush(() => {
edit(
kind === 'project'
? 'Project title'
: kind === 'hypothesis'
? 'Hypothesis title'
: 'Note title',
creationDraft.title
);
edit(
kind === 'project'
? 'Project description'
: kind === 'hypothesis'
? 'Proposition'
: 'Note body',
body
);
if (kind !== 'project') {
edit('Declared content origin', origin);
}
if (kind === 'hypothesis') {
edit('Validation criteria', creationDraft.criteria);
}
});
await flush(() => click(`Create ${kind}`));
const path =
kind === 'project'
? 'research-projects'
: `research-projects/p1/${kind === 'hypothesis' ? 'hypotheses' : 'analysis-artifacts'}`;
const saved =
kind === 'project'
? project('p1', { title: creationDraft.title.trim(), description: body })
: kind === 'hypothesis'
? hypothesis('h1', {
latestRevision: {
...hypothesis().latestRevision,
title: creationDraft.title.trim(),
proposition: body.trim(),
validationCriteria: creationDraft.criteria,
contentOrigin: origin,
},
})
: artifact('a1', {
latestRevision: {
...artifact().latestRevision,
title: creationDraft.title.trim(),
body,
contentOrigin: origin,
},
});
return { post: request(path, 'POST'), saved };
}
async function submitRevision(original: ResearchHypothesis | ResearchArtifact) {
const isHypothesis = 'proposition' in original.latestRevision;
await mount(`/research/p1?${isHypothesis ? 'hypothesis' : 'artifact'}=${original.id}`);
@ -599,6 +671,134 @@ describe('research workspace', () => {
await flush(() => click('Start new request'));
expect(button('Create project').disabled).toBe(false);
});
for (const [label, change] of [
['title', { title: 'Different project' }],
['description', { description: 'Different description' }],
['noninitial revision', { revision: 2 }],
['archival', { archived: true }],
] as [string, Partial<ResearchProject>][]) {
test(`project creation ${label} acknowledgement retains the draft without navigation`, async () => {
const { post, saved } = await submitCreation('project');
await flush(() => reply(post, { project: { ...saved, ...change } }, 201));
expect(host.textContent).toContain('The saved project response could not be verified.');
expect(field('Project title').value).toBe(creationDraft.title);
expect(field('Project description').value).toBe(creationDraft.body);
expect(button('Create project').disabled).toBe(true);
expect(location).toBe('/research');
await flush(() => click('Create project'));
expect(requests.filter(item => item.method === 'POST')).toHaveLength(1);
expect(requests.filter(item => !item.settled)).toHaveLength(0);
});
}
for (const kind of ['hypothesis', 'note'] as const) {
for (const [label, rootChange, revisionChange] of [
['project', { projectId: 'p2' }, {}],
['archival', { archived: true }, {}],
['parent', {}, kind === 'hypothesis' ? { hypothesisId: 'other' } : { artifactId: 'other' }],
['noninitial ordinal', {}, { ordinal: 2 }],
['title', {}, { title: 'Different title' }],
[
'body',
{},
kind === 'hypothesis' ? { proposition: 'Different proposal' } : { body: 'Different note' },
],
['origin', {}, { contentOrigin: 'human' }],
['validated state', {}, { validationState: 'validated' }],
...(kind === 'hypothesis'
? [['criteria', {}, { validationCriteria: 'Different criteria' }]]
: [
['kind', { kind: 'finding' }, {}],
['hypothesis link', { hypothesisRevisionId: 'unselected' }, {}],
[
'source',
{},
{
source: {
kind: 'experiment/v1',
experimentId: '11111111-1111-4111-8111-111111111111',
previewHash: 'a'.repeat(64),
},
},
],
['evidence', {}, { evidence: [{}] }],
['evidence state', {}, { evidenceState: 'referenced' }],
]),
] as [string, Record<string, unknown>, Record<string, unknown>][]) {
test(`${kind} creation ${label} acknowledgement retains and blocks the proposal`, async () => {
const { post, saved } = await submitCreation(kind);
if (!('latestRevision' in saved)) {
throw new Error('Expected a narrative fixture');
}
await flush(() =>
reply(
post,
{
[kind === 'hypothesis' ? 'hypothesis' : 'artifact']: {
...saved,
...rootChange,
latestRevision: { ...saved.latestRevision, ...revisionChange },
},
},
201
)
);
expect(host.textContent).toContain(
'The saved research entry response could not be verified.'
);
expect(field(kind === 'hypothesis' ? 'Hypothesis title' : 'Note title').value).toBe(
creationDraft.title
);
expect(field(kind === 'hypothesis' ? 'Proposition' : 'Note body').value).toBe(
creationDraft.body
);
expect(field('Declared content origin').value).toBe('ai');
if (kind === 'hypothesis') {
expect(field('Validation criteria').value).toBe(creationDraft.criteria);
}
expect(button(`Create ${kind}`).disabled).toBe(true);
expect(location).toBe('/research/p1');
await flush(() => click(`Create ${kind}`));
expect(requests.filter(item => item.method === 'POST')).toHaveLength(1);
expect(requests.filter(item => !item.settled)).toHaveLength(0);
await flush(() => click('Check saved records'));
await flush(() => replyProject());
expect(field(kind === 'hypothesis' ? 'Proposition' : 'Note body').value).toBe(
creationDraft.body
);
expect(button(`Create ${kind}`).disabled).toBe(true);
});
}
}
for (const kind of ['project', 'hypothesis', 'note'] as const) {
const origins: ResearchContentOrigin[] =
kind === 'project' ? ['human'] : ['human', 'ai', 'unknown'];
for (const origin of origins) {
test(`normalized ${kind} creation with ${origin} origin opens the accepted record`, async () => {
const { post, saved } = await submitCreation(
kind,
origin,
kind === 'note' ? '' : creationDraft.body
);
expect(post.body).toMatchObject({ title: creationDraft.title.trim() });
await flush(() =>
reply(
post,
{
[kind === 'project' ? 'project' : kind === 'hypothesis' ? 'hypothesis' : 'artifact']:
saved,
},
201
)
);
expect(location).toBe(
kind === 'project'
? '/research/p1'
: `/research/p1?${kind === 'hypothesis' ? 'hypothesis=h1' : 'artifact=a1'}`
);
expect(requests.filter(item => item.method === 'POST')).toHaveLength(1);
});
}
}
test('late create success cannot navigate away from a newer project', async () => {
await mount('/research');
await flush(() => reply(request('research-projects?includeArchived=false'), { projects: [] }));
@ -695,9 +895,11 @@ describe('research workspace', () => {
await flush(() => click('Create note'));
expect(request('research-projects/p1/analysis-artifacts', 'POST').body).toEqual({
kind: 'note',
hypothesisRevisionId: null,
title: 'Observation',
body: 'Needs validation',
contentOrigin: 'human',
evidence: [],
});
await flush(() =>
reply(request('research-projects/p1/analysis-artifacts', 'POST'), { error: 'Uncertain' }, 500)