feat(research): reopen artifacts and immutable history (RP25)

This commit is contained in:
Boki 2026-10-27 22:47:47 -04:00
parent f9b97c3004
commit e39989c4d5
4 changed files with 385 additions and 16 deletions

View file

@ -84,6 +84,194 @@ function crossStudyFinding() {
};
return { input, artifact };
}
function readableArtifact(): Awaited<ReturnType<ResearchClient['artifact']>> {
const { artifact } = crossStudyFinding();
const createdAt = '2026-10-27T12:00:00.000Z';
return {
...artifact,
ownerPrincipal: agent,
updatedBy: agent,
archived: false,
createdAt,
updatedAt: createdAt,
latestRevision: {
...artifact.latestRevision,
submittedBy: agent,
artifactId,
ordinal: 1,
createdAt,
evidence: artifact.latestRevision.evidence.map(reference => ({
...reference,
backtestId: ordinary().backtestId,
unit: 'fraction',
})),
},
};
}
describe('retained research artifact reads', () => {
test('reopens a saved artifact through authenticated GET without admission', async () => {
const artifact = readableArtifact();
const { value, calls } = client(() => Response.json({ artifact }));
const saved = await value.artifact(artifactId.toUpperCase());
expect(saved).toEqual(artifact);
expect(calls).toHaveLength(1);
expect(calls[0]!.url.pathname).toBe(`/api/v2/analysis-artifacts/${artifactId}`);
expect(calls[0]!.init.method).toBe('GET');
expect(calls[0]!.body).toBeUndefined();
expect(new Headers(calls[0]!.init.headers).get('authorization')).toBe(`Bearer ${token}`);
expect(new Headers(calls[0]!.init.headers).has('idempotency-key')).toBe(false);
});
test('retains exact old AI revision when latest is human-authored', async () => {
const original = readableArtifact().latestRevision;
const updated: typeof original = {
...structuredClone(original),
id: hypothesisId,
ordinal: 2,
contentOrigin: 'human',
submittedBy: 'operator',
body: 'Revised human interpretation, still unvalidated.',
};
const revisions = [updated, original];
const { value, calls } = client(() => Response.json({ revisions }));
const history = await value.artifactRevisions(artifactId);
expect(history).toEqual(revisions);
expect(history.find(item => item.id === revisionId)).toEqual(original);
expect(history[0]!.submittedBy).toBe('operator');
expect(calls).toHaveLength(1);
expect(calls[0]!.url.pathname).toBe(`/api/v2/analysis-artifacts/${artifactId}/revisions`);
expect(calls[0]!.init.method).toBe('GET');
});
test('reads human legacy findings and service notes without certifying old measurements', async () => {
const artifact = readableArtifact();
artifact.latestRevision.contentOrigin = 'human';
artifact.latestRevision.submittedBy = 'operator';
artifact.latestRevision.evidence = [
{
...artifact.latestRevision.evidence[0]!,
strategyVersionId: null,
metricsVersion: 'legacy-display/v1',
metricPath: 'metrics.totalReturn',
resultKind: 'legacy',
financialEngine: 'native',
datasetHash: null,
limitations: [{ code: 'legacy-unverified-economics', message: 'Historical display only' }],
},
];
const read = client(() => Response.json({ artifact }));
expect(await read.value.artifact(artifactId)).toEqual(artifact);
const note = readableArtifact();
note.kind = 'note';
note.archived = true;
note.latestRevision.contentOrigin = 'unknown';
note.latestRevision.submittedBy = 'service';
note.latestRevision.evidenceState = 'none';
note.latestRevision.evidence = [];
const { source: _source, ...withoutSource } = note.latestRevision;
const notes = client(() =>
Response.json({ artifact: { ...note, latestRevision: withoutSource } })
);
const saved = await notes.value.artifact(artifactId);
expect(saved.kind).toBe('note');
expect(saved.archived).toBe(true);
expect(saved.latestRevision.source).toBeNull();
expect(saved.latestRevision.contentOrigin).toBe('unknown');
expect(saved.latestRevision.submittedBy).toBe('service');
expect(saved.latestRevision.evidence).toEqual([]);
});
test('rejects foreign artifact identity and malformed retained evidence as read errors', async () => {
const artifact = readableArtifact();
const revision = artifact.latestRevision;
const badRevisions = [
{ artifactId: projectId },
{ validationState: 'validated' },
{ evidenceState: 'none' },
{ evidence: [] },
{ evidence: [revision.evidence[0], revision.evidence[0]] },
{ source: { kind: 'experiment/v1', experimentId: projectId, previewHash: 'bad-hash' } },
...[
{ runId: 'bad-id' },
{ metricPath: 'analytics.metrics.unknown' },
{ metricsVersion: 'legacy-display/v1' },
{ value: null },
{ unit: 'dollars' },
{ availability: 'unavailable' },
{ unavailableReason: 'Contradictory' },
{ datasetHash: null },
{ resultKind: 'legacy' },
{ financialEngine: 'native' },
].map(change => ({ evidence: [{ ...revision.evidence[0], ...change }] })),
];
const bodies = [
{},
{ artifact: null },
{ artifact: { ...artifact, id: projectId } },
...badRevisions.map(change => ({
artifact: { ...artifact, latestRevision: { ...revision, ...change } },
})),
];
for (const body of bodies) {
const { value, calls } = client(() => Response.json(body));
await expect(value.artifact(artifactId)).rejects.toMatchObject({
name: 'ResearchRequestError',
uncertain: false,
status: null,
});
expect(calls).toHaveLength(1);
expect(calls[0]!.init.method).toBe('GET');
}
});
test('rejects missing, mixed-parent and duplicate history instead of replacing original identity', async () => {
const revision = readableArtifact().latestRevision;
for (const revisions of [
null,
[],
[{ ...revision, artifactId: projectId }],
[revision, { ...revision, ordinal: 2 }],
[revision, { ...revision, id: projectId }],
[{ ...revision, ordinal: 0 }],
]) {
const { value, calls } = client(() => Response.json({ revisions }));
await expect(value.artifactRevisions(artifactId)).rejects.toMatchObject({
uncertain: false,
status: null,
});
expect(calls).toHaveLength(1);
}
});
test('invalid IDs, HTTP failures and aborts never create or retry work', async () => {
const invalid = client(() => {
throw new Error('No transport expected');
});
expect(() => invalid.value.artifact('../strategies')).toThrow();
expect(() => invalid.value.artifactRevisions('bad-id')).toThrow();
expect(invalid.calls).toHaveLength(0);
for (const status of [401, 404, 503]) {
const { value, calls } = client(() => Response.json({ error: 'Unavailable' }, { status }));
await expect(value.artifact(artifactId)).rejects.toMatchObject({ status, uncertain: false });
await expect(value.artifactRevisions(artifactId)).rejects.toMatchObject({
status,
uncertain: false,
});
expect(calls).toHaveLength(2);
}
const controller = new AbortController();
const stalled = client(() => new Promise<Response>(() => {}));
const pending = stalled.value.artifact(artifactId, controller.signal);
controller.abort(new Error('Stop reading'));
await expect(pending).rejects.toMatchObject({ uncertain: false });
expect(stalled.calls).toHaveLength(1);
await expect(
stalled.value.artifactRevisions(artifactId, controller.signal)
).rejects.toMatchObject({ uncertain: false });
expect(stalled.calls).toHaveLength(1);
});
});
describe('authorized research client', () => {
test('only research credentials, fixed API origin, no trading methods or redirect credential forwarding', async () => {