feat(research): retain exact experiment and report citations in findings

This commit is contained in:
Boki 2026-10-24 14:42:14 -04:00
parent 46a4f13acd
commit 06039d17ba
36 changed files with 4170 additions and 253 deletions

View file

@ -17,12 +17,17 @@ function fixture(
security: 'enabled' | 'disabled' | 'absent' = 'enabled',
query: Query = async () => {
throw new Error('Rejected request reached storage');
}
},
allowReadTransaction = false
) {
const transactionQuery = mock(query);
const database = {
query: mock(query),
transaction: mock(async () => {
throw new Error('Rejected request reached transaction');
transaction: mock(async (callback: (client: { query: Query }) => Promise<unknown>) => {
if (!allowReadTransaction) {
throw new Error('Rejected request reached transaction');
}
return callback({ query: transactionQuery });
}),
};
const logger = { error: mock((_message: string, _details?: unknown) => {}) };
@ -44,6 +49,7 @@ function fixture(
app.post('/api/v2/unrelated', c => c.json({ ok: true }));
return {
database,
transactionQuery,
logger,
app,
request(
@ -349,79 +355,94 @@ describe('research HTTP credential and transport boundary', () => {
const runId = randomUUID();
const backtestId = randomUUID();
const stamp = new Date('2026-01-01');
const f = fixture('enabled', async sql => {
if (sql.includes('FROM analysis_artifacts WHERE')) {
return {
rows: [
{
id: artifactId,
project_id: projectId,
hypothesis_revision_id: null,
kind: 'finding',
owner_principal: 'operator',
updated_by: 'operator',
archived_at: null,
created_at: stamp,
updated_at: stamp,
},
],
};
}
if (sql.includes('FROM analysis_artifact_revisions WHERE')) {
return {
rows: [
{
id: revisionId,
artifact_id: artifactId,
ordinal: 1,
title: 'Retained finding',
body: 'Narrative is not a metric',
submitted_by: 'operator',
content_origin: 'human',
created_at: stamp,
},
],
};
}
if (sql.includes('FROM analysis_evidence WHERE')) {
return {
rows: [
{
revision_id: revisionId,
ordinal: 1,
run_id: runId,
metrics_version: 'canonical-analytics/v1',
metric_path: 'analytics.metrics.totalReturn',
strategy_version_id: null,
},
],
};
}
if (sql.includes('FROM runs WHERE')) {
return { rows: [{ id: runId, backtest_id: backtestId, status: 'completed' }] };
}
if (sql.includes('FROM backtest_results br')) {
// The reference is valid. This damage is in retained server evidence,
// and must not be classified as a malformed client request.
return {
rows: [
{
run_id: runId,
backtest_id: backtestId,
run_status: 'completed',
canonical_result: { kind: 'canonical-result/v1' },
request_snapshot: { kind: 'canonical-research/v1', 'private-retained-marker': true },
},
],
};
}
throw new Error(`Unexpected evidence query: ${sql}`);
});
const f = fixture(
'enabled',
async sql => {
if (sql === 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY') {
return { rows: [] };
}
if (sql.includes('FROM analysis_artifacts WHERE')) {
return {
rows: [
{
id: artifactId,
project_id: projectId,
hypothesis_revision_id: null,
kind: 'finding',
owner_principal: 'operator',
updated_by: 'operator',
archived_at: null,
created_at: stamp,
updated_at: stamp,
},
],
};
}
if (sql.includes('FROM analysis_artifact_revisions WHERE')) {
return {
rows: [
{
id: revisionId,
artifact_id: artifactId,
ordinal: 1,
title: 'Retained finding',
body: 'Narrative is not a metric',
submitted_by: 'operator',
content_origin: 'human',
created_at: stamp,
},
],
};
}
if (sql.includes('FROM analysis_evidence WHERE')) {
return {
rows: [
{
revision_id: revisionId,
ordinal: 1,
run_id: runId,
metrics_version: 'canonical-analytics/v1',
metric_path: 'analytics.metrics.totalReturn',
strategy_version_id: null,
},
],
};
}
if (sql.includes('FROM runs WHERE')) {
return { rows: [{ id: runId, backtest_id: backtestId, status: 'completed' }] };
}
if (sql.includes('FROM backtest_results br')) {
// The reference is valid. This damage is in retained server evidence,
// and must not be classified as a malformed client request.
return {
rows: [
{
run_id: runId,
backtest_id: backtestId,
run_status: 'completed',
canonical_result: { kind: 'canonical-result/v1' },
request_snapshot: {
kind: 'canonical-research/v1',
'private-retained-marker': true,
},
},
],
};
}
throw new Error(`Unexpected evidence query: ${sql}`);
},
true
);
const response = await f.request(`analysis-artifacts/${artifactId}`, undefined, 'read');
expect(response.status).toBe(500);
expect(await response.json()).toEqual({ error: 'Research workspace unavailable' });
expect(response.headers.get('Cache-Control')).toBe('no-store');
expect(f.database.query.mock.calls).toHaveLength(5);
expect(f.database.query.mock.calls).toHaveLength(0);
expect(f.database.transaction.mock.calls).toHaveLength(1);
expect(f.transactionQuery.mock.calls).toHaveLength(6);
expect(f.transactionQuery.mock.calls[0]?.[0]).toBe(
'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'
);
expect(f.logger.error.mock.calls).toHaveLength(1);
const diagnostic = f.logger.error.mock.calls[0]?.[1] as { error?: unknown } | undefined;
expect(diagnostic?.error).toBeInstanceOf(Error);