fix(research): support typed cross-study findings (RP23)
This commit is contained in:
parent
27d2bc961c
commit
8dc8724c52
4 changed files with 296 additions and 2 deletions
|
|
@ -35,6 +35,55 @@ function client(handler: (call: Call, index: number) => Response | Promise<Respo
|
|||
return { value, calls };
|
||||
}
|
||||
const ordinary = () => tableFixture(1) as ExperimentDetail;
|
||||
function crossStudyFinding() {
|
||||
const detail = ordinary();
|
||||
const input = {
|
||||
kind: 'finding' as const,
|
||||
title: 'Compare two candidates',
|
||||
body: 'These synthetic results do not validate the hypothesis.',
|
||||
evidence: [
|
||||
{
|
||||
runId: detail.trials[0]!.runId!,
|
||||
strategyVersionId: detail.strategyVersionId,
|
||||
metricsVersion: 'canonical-analytics/v1' as const,
|
||||
metricPath: 'analytics.metrics.totalReturn' as const,
|
||||
},
|
||||
{
|
||||
runId: '66666666-6666-4666-8666-666666666666',
|
||||
strategyVersionId: '77777777-7777-4777-8777-777777777777',
|
||||
metricsVersion: 'canonical-analytics/v1' as const,
|
||||
metricPath: 'analytics.metrics.totalReturn' as const,
|
||||
},
|
||||
],
|
||||
};
|
||||
const artifact: Awaited<ReturnType<ResearchClient['saveFinding']>> = {
|
||||
id: artifactId,
|
||||
projectId,
|
||||
hypothesisRevisionId: null,
|
||||
kind: 'finding',
|
||||
latestRevision: {
|
||||
id: revisionId,
|
||||
title: input.title,
|
||||
body: input.body,
|
||||
contentOrigin: 'ai',
|
||||
submittedBy: agent,
|
||||
validationState: 'unvalidated',
|
||||
evidenceState: 'referenced',
|
||||
source: null,
|
||||
evidence: input.evidence.map((reference, index) => ({
|
||||
...reference,
|
||||
value: index === 0 ? -0.01 : 0.02,
|
||||
availability: 'available',
|
||||
unavailableReason: null,
|
||||
datasetHash: detail.datasetHash,
|
||||
resultKind: 'canonical',
|
||||
financialEngine: 'rust',
|
||||
limitations: [{ code: 'fixture', message: 'Synthetic demonstration' }],
|
||||
})),
|
||||
},
|
||||
};
|
||||
return { input, artifact };
|
||||
}
|
||||
|
||||
describe('authorized research client', () => {
|
||||
test('only research credentials, fixed API origin, no trading methods or redirect credential forwarding', async () => {
|
||||
|
|
@ -368,6 +417,165 @@ describe('authorized research client', () => {
|
|||
expect(comparison.rows.some(row => !row.ready)).toBe(true);
|
||||
});
|
||||
|
||||
test.each(['null', 'omitted'] as const)(
|
||||
'cross-study findings accept a %s source and retain exact versioned evidence',
|
||||
async source => {
|
||||
const { input, artifact } = crossStudyFinding();
|
||||
const body = source === 'null' ? { ...input, source: null } : input;
|
||||
const { value, calls } = client(() => Response.json({ artifact }, { status: 201 }));
|
||||
const saved = await value.saveFinding(projectId, body);
|
||||
expect(saved).toEqual(artifact);
|
||||
expect(saved.latestRevision.source).toBeNull();
|
||||
expect(saved.latestRevision.evidence).toEqual(artifact.latestRevision.evidence);
|
||||
expect(saved.latestRevision.validationState).toBe('unvalidated');
|
||||
expect(saved.latestRevision.contentOrigin).toBe('ai');
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(calls[0]!.url.pathname).toBe(
|
||||
`/api/v2/research-projects/${projectId}/analysis-artifacts`
|
||||
);
|
||||
expect(calls[0]!.init.method).toBe('POST');
|
||||
expect(calls[0]!.body).toEqual({
|
||||
...body,
|
||||
hypothesisRevisionId: null,
|
||||
contentOrigin: 'ai',
|
||||
});
|
||||
expect(Object.hasOwn(calls[0]!.body as object, 'source')).toBe(source === 'null');
|
||||
expect(new Headers(calls[0]!.init.headers).has('idempotency-key')).toBe(false);
|
||||
}
|
||||
);
|
||||
|
||||
test('findings still bind supplied experiment and report sources exactly', async () => {
|
||||
const { input, artifact } = crossStudyFinding();
|
||||
const detail = ordinary();
|
||||
const sources = [
|
||||
{ kind: 'experiment/v1' as const, experimentId: detail.id, previewHash: detail.previewHash },
|
||||
{
|
||||
kind: 'chronological-report/v1' as const,
|
||||
validationId: detail.id,
|
||||
previewHash: detail.previewHash,
|
||||
reportId: revisionId,
|
||||
reportHash: 'a'.repeat(64),
|
||||
},
|
||||
{
|
||||
kind: 'walk-forward-report/v1' as const,
|
||||
walkForwardId: detail.id,
|
||||
previewHash: detail.previewHash,
|
||||
reportId: revisionId,
|
||||
reportHash: 'a'.repeat(64),
|
||||
},
|
||||
];
|
||||
for (const source of sources) {
|
||||
for (const returned of [source, null, { ...source, previewHash: 'f'.repeat(64) }]) {
|
||||
const { value, calls } = client(() =>
|
||||
Response.json(
|
||||
{
|
||||
artifact: {
|
||||
...artifact,
|
||||
latestRevision: { ...artifact.latestRevision, source: returned },
|
||||
},
|
||||
},
|
||||
{ status: 201 }
|
||||
)
|
||||
);
|
||||
const pending = value.saveFinding(projectId, { ...input, source });
|
||||
if (returned === source) {
|
||||
expect((await pending).latestRevision.source).toEqual(source);
|
||||
} else {
|
||||
await expect(pending).rejects.toMatchObject({ uncertain: true, status: null });
|
||||
}
|
||||
expect(calls).toHaveLength(1);
|
||||
expect((calls[0]!.body as { source: unknown }).source).toEqual(source);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('source-free findings reject substituted response identities and malformed evidence without retry', async () => {
|
||||
const { input, artifact } = crossStudyFinding();
|
||||
const revision = artifact.latestRevision;
|
||||
const invalidRevisions = [
|
||||
{
|
||||
source: { kind: 'experiment/v1', experimentId: ordinary().id, previewHash: 'f'.repeat(64) },
|
||||
},
|
||||
{ contentOrigin: 'human' },
|
||||
{ submittedBy: 'operator' },
|
||||
{ validationState: 'validated' },
|
||||
{ evidenceState: 'none' },
|
||||
...[
|
||||
{ runId: hypothesisId },
|
||||
{ strategyVersionId: hypothesisId },
|
||||
{ strategyVersionId: null },
|
||||
{ metricPath: 'analytics.metrics.maxDrawdown' },
|
||||
{ metricsVersion: 'legacy-display/v1' },
|
||||
{ value: null },
|
||||
{ availability: 'unavailable' },
|
||||
{ unavailableReason: 'Contradictory reason' },
|
||||
{ resultKind: 'legacy' },
|
||||
{ financialEngine: 'typescript' },
|
||||
].map(change => ({
|
||||
evidence: [{ ...revision.evidence[0], ...change }, revision.evidence[1]],
|
||||
})),
|
||||
];
|
||||
const invalidArtifacts = [
|
||||
{ ...artifact, projectId: hypothesisId },
|
||||
{ ...artifact, hypothesisRevisionId: revisionId },
|
||||
{ ...artifact, kind: 'note' },
|
||||
...invalidRevisions.map(change => ({
|
||||
...artifact,
|
||||
latestRevision: { ...revision, ...change },
|
||||
})),
|
||||
];
|
||||
for (const invalid of invalidArtifacts) {
|
||||
const { value, calls } = client(() => Response.json({ artifact: invalid }, { status: 201 }));
|
||||
await expect(value.saveFinding(projectId, input)).rejects.toMatchObject({
|
||||
uncertain: true,
|
||||
status: null,
|
||||
operation: `POST research-projects/${projectId}/analysis-artifacts`,
|
||||
});
|
||||
expect(calls).toHaveLength(1);
|
||||
}
|
||||
});
|
||||
|
||||
test('source-free findings require nonempty unique canonical versioned evidence before transport', () => {
|
||||
const { input } = crossStudyFinding();
|
||||
const reference = input.evidence[0]!;
|
||||
const { strategyVersionId: _version, ...unversioned } = reference;
|
||||
const invalidInputs = [
|
||||
{ kind: 'note' },
|
||||
{ evidence: [] },
|
||||
{ evidence: [reference, reference] },
|
||||
{ evidence: [unversioned] },
|
||||
{ evidence: [{ ...reference, strategyVersionId: null }] },
|
||||
{ evidence: [{ ...reference, strategyVersionId: 'bad-id' }] },
|
||||
{ evidence: [{ ...reference, runId: 'bad-id' }] },
|
||||
{ evidence: [{ ...reference, metricPath: 'analytics.metrics.unknown' }] },
|
||||
{
|
||||
evidence: [
|
||||
{ ...reference, metricsVersion: 'legacy-display/v1', metricPath: 'metrics.totalReturn' },
|
||||
],
|
||||
},
|
||||
{ source: {} },
|
||||
{ source: { kind: 'experiment/v1', experimentId: projectId, previewHash: 'bad-hash' } },
|
||||
{
|
||||
source: {
|
||||
kind: 'chronological-report/v1',
|
||||
validationId: projectId,
|
||||
previewHash: 'a'.repeat(64),
|
||||
},
|
||||
},
|
||||
];
|
||||
const { value, calls } = client(() => {
|
||||
throw new Error('Invalid finding must not reach transport');
|
||||
});
|
||||
for (const invalid of invalidInputs) {
|
||||
expect(() =>
|
||||
value.saveFinding(projectId, { ...input, ...invalid } as Parameters<
|
||||
ResearchClient['saveFinding']
|
||||
>[1])
|
||||
).toThrow();
|
||||
}
|
||||
expect(calls).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('AI hypotheses stay unvalidated and findings retain exact canonical sources without validating prose', async () => {
|
||||
const detail = ordinary(),
|
||||
runId = detail.trials[0]!.runId!;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue