feat(research): download validated retained datasets (RP07)

This commit is contained in:
Boki 2026-10-27 10:59:19 -04:00
parent 43ea2cc9b6
commit b959779ada
14 changed files with 1076 additions and 11 deletions

View file

@ -50,6 +50,16 @@ const invalid = (message: string): never => {
throw new HistoricalDataRequestError(message);
};
/** Read rejection statuses for exports; existing callers may still catch the base request error. */
export class CanonicalDatasetReadError extends HistoricalDataRequestError {
constructor(
readonly status: 400 | 404 | 409 | 422,
message: string
) {
super(message);
}
}
/** The existing workbench gate, shared by retained selection and source preview. */
export function canonicalDatasetUnavailableReason(dataset: HistoricalDatasetV2): string | null {
return datasetUnavailableReason(dataset.manifest);
@ -61,28 +71,31 @@ export function canonicalDatasetUnavailableReason(dataset: HistoricalDatasetV2):
*/
export async function readCanonicalDataset(
database: DatasetCatalogQuery,
hash: unknown
hash: unknown,
signal?: AbortSignal
): Promise<HistoricalDatasetV2> {
signal?.throwIfAborted();
if (typeof hash !== 'string' || !/^[a-f0-9]{64}$/.test(hash)) {
return invalid('Choose an explicitly retained v2 dataset');
throw new CanonicalDatasetReadError(400, 'Choose an explicitly retained v2 dataset');
}
const row = (await database.query('SELECT payload FROM backtest_datasets WHERE hash=$1', [hash]))
.rows[0];
signal?.throwIfAborted();
if (!row) {
return invalid('Selected canonical dataset was not found');
throw new CanonicalDatasetReadError(404, 'Selected canonical dataset was not found');
}
let dataset;
try {
dataset = validateDatasetV2(row.payload);
} catch {
return invalid('Retained v2 dataset failed content validation');
throw new CanonicalDatasetReadError(409, 'Retained v2 dataset failed content validation');
}
if (dataset.manifest.hash !== hash) {
return invalid('Retained dataset identity mismatch');
throw new CanonicalDatasetReadError(409, 'Retained dataset identity mismatch');
}
const unavailable = canonicalDatasetUnavailableReason(dataset);
if (unavailable) {
return invalid(unavailable);
throw new CanonicalDatasetReadError(422, unavailable);
}
return dataset;
}