feat(research): read verified frozen datasets (RP28)
This commit is contained in:
parent
04892a59fe
commit
4b9c642817
9 changed files with 263 additions and 18 deletions
|
|
@ -1,5 +1,6 @@
|
|||
/** Authorized research API client. No strategy import, financial simulation, or trading controls. */
|
||||
import { z } from 'zod';
|
||||
import { validateDatasetV2 } from '@stock-bot/research/datasets';
|
||||
import { parseCanonicalRunResult } from '@stock-bot/research/results';
|
||||
import {
|
||||
chronologicalValidationInputSchema,
|
||||
|
|
@ -763,6 +764,22 @@ export class ResearchClient {
|
|||
key
|
||||
);
|
||||
}
|
||||
canonicalDataset(datasetHash: string, signal?: AbortSignal) {
|
||||
datasetHash = hash.parse(datasetHash);
|
||||
return this.#request(
|
||||
`datasets/canonical/${datasetHash}/export`,
|
||||
'GET',
|
||||
200,
|
||||
value => {
|
||||
const dataset = validateDatasetV2(value);
|
||||
expect(dataset.manifest.hash === datasetHash);
|
||||
return dataset;
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
signal
|
||||
);
|
||||
}
|
||||
canonicalRunResult(runId: string, signal?: AbortSignal) {
|
||||
runId = uuid.parse(runId);
|
||||
return this.#request(
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { createHash } from 'node:crypto';
|
|||
import { describe, expect, test } from 'bun:test';
|
||||
import type { ExperimentDetail } from '@stock-bot/stock-config/experiments';
|
||||
import { canonicalResultFixture } from '../../test/fixtures/canonical-result';
|
||||
import { nativeReplayFixture } from '../../test/fixtures/native-replay';
|
||||
import { comparisonFixture, tableFixture } from '../benchmark-experiment-tables';
|
||||
import {
|
||||
pollResearch,
|
||||
|
|
@ -37,6 +38,86 @@ function client(handler: (call: Call, index: number) => Response | Promise<Respo
|
|||
}
|
||||
const ordinary = () => tableFixture(1) as ExperimentDetail;
|
||||
|
||||
describe('frozen market data inspection', () => {
|
||||
test('reads the complete requested snapshot without changing bars or metadata', async () => {
|
||||
const dataset = nativeReplayFixture().dataset;
|
||||
const before = structuredClone(dataset);
|
||||
const { value, calls } = client(() => Response.json(dataset));
|
||||
const saved = await value.canonicalDataset(dataset.manifest.hash);
|
||||
expect(saved).toEqual(before);
|
||||
expect(Object.isFrozen(saved)).toBe(true);
|
||||
expect(dataset).toEqual(before);
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(calls[0]!.url.pathname).toBe(
|
||||
`/api/v2/datasets/canonical/${dataset.manifest.hash}/export`
|
||||
);
|
||||
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('rejects altered content, derived policy and a different valid snapshot without retries', async () => {
|
||||
const dataset = nativeReplayFixture().dataset;
|
||||
const changedBar = {
|
||||
...dataset,
|
||||
bars: dataset.bars.map((bar, index) => (index ? bar : { ...bar, volume: bar.volume + 1 })),
|
||||
};
|
||||
const changedTime = {
|
||||
...dataset,
|
||||
bars: dataset.bars.map((bar, index) =>
|
||||
index ? bar : { ...bar, availableAt: bar.availableAt + 1 }
|
||||
),
|
||||
};
|
||||
const changedPolicy = {
|
||||
...dataset,
|
||||
manifest: { ...dataset.manifest, eligibility: 'exploratory' },
|
||||
};
|
||||
for (const response of [
|
||||
null,
|
||||
{},
|
||||
changedBar,
|
||||
changedTime,
|
||||
changedPolicy,
|
||||
nativeReplayFixture(4).dataset,
|
||||
]) {
|
||||
const { value, calls } = client(() => Response.json(response));
|
||||
await expect(value.canonicalDataset(dataset.manifest.hash)).rejects.toMatchObject({
|
||||
status: null,
|
||||
uncertain: false,
|
||||
});
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(calls[0]!.init.method).toBe('GET');
|
||||
}
|
||||
});
|
||||
|
||||
test('invalid hashes, HTTP failures and aborts never query alternate data', async () => {
|
||||
const invalid = client(() => {
|
||||
throw new Error('No transport expected');
|
||||
});
|
||||
for (const input of ['../runs', 'a'.repeat(63), 'A'.repeat(64)]) {
|
||||
expect(() => invalid.value.canonicalDataset(input)).toThrow();
|
||||
}
|
||||
expect(invalid.calls).toHaveLength(0);
|
||||
for (const status of [401, 404, 409, 500]) {
|
||||
const { value, calls } = client(() =>
|
||||
Response.json({ error: 'Dataset unavailable' }, { status })
|
||||
);
|
||||
await expect(value.canonicalDataset('a'.repeat(64))).rejects.toMatchObject({
|
||||
status,
|
||||
uncertain: false,
|
||||
});
|
||||
expect(calls).toHaveLength(1);
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const stalled = client(() => new Promise<Response>(() => {}));
|
||||
const pending = stalled.value.canonicalDataset('a'.repeat(64), controller.signal);
|
||||
controller.abort();
|
||||
await expect(pending).rejects.toMatchObject({ uncertain: false });
|
||||
expect(stalled.calls).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('canonical run inspection', () => {
|
||||
test('reads complete native finances and retained inputs through authenticated GET', async () => {
|
||||
const result = canonicalResultFixture();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue