feat(research): reproduce retained canonical runs with economic comparison
This commit is contained in:
parent
e3bd708838
commit
30accfcf43
31 changed files with 2969 additions and 28 deletions
|
|
@ -1,5 +1,8 @@
|
|||
import { canonicalRunRequestSchema } from '@stock-bot/stock-config/canonical-research';
|
||||
import type {
|
||||
CanonicalDatasetCatalogEntry,
|
||||
CanonicalReproductionComparison,
|
||||
CanonicalReproductionPreview,
|
||||
CanonicalRunCapability,
|
||||
ResearchRunResult,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
|
|
@ -29,6 +32,7 @@ export interface Backtest {
|
|||
|
||||
export interface Run {
|
||||
id: string;
|
||||
originalRunId?: string | null;
|
||||
backtestId: string;
|
||||
runNumber: number;
|
||||
status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled' | 'paused';
|
||||
|
|
@ -84,7 +88,137 @@ async function requestError(response: Response, fallback: string): Promise<Error
|
|||
return new Error(message);
|
||||
}
|
||||
|
||||
export class ReproductionSubmissionError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly status: number | null
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
get ambiguous() {
|
||||
return this.status === null || this.status >= 500;
|
||||
}
|
||||
}
|
||||
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
||||
typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
const nullableText = (value: unknown) => value === null || typeof value === 'string';
|
||||
const hashOrNull = (value: unknown) =>
|
||||
value === null || (typeof value === 'string' && /^[a-f0-9]{64}$/.test(value));
|
||||
|
||||
export const backtestApiV2 = {
|
||||
async reproductionPreview(
|
||||
id: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<CanonicalReproductionPreview> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/api/v2/runs/${encodeURIComponent(id)}/reproduction`,
|
||||
{ signal }
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw await requestError(response, 'Could not check original-run reproduction');
|
||||
}
|
||||
const body: unknown = await response.json();
|
||||
if (
|
||||
!isRecord(body) ||
|
||||
body.originalRunId !== id ||
|
||||
typeof body.backtestId !== 'string' ||
|
||||
typeof body.available !== 'boolean' ||
|
||||
!nullableText(body.reason) ||
|
||||
!nullableText(body.currentReviewId)
|
||||
) {
|
||||
throw new Error('The original-run preview response could not be verified');
|
||||
}
|
||||
const request = body.request === null ? null : canonicalRunRequestSchema.parse(body.request);
|
||||
if (
|
||||
(request && (request.runId !== id || request.backtestId !== body.backtestId)) ||
|
||||
(body.available && (!request || !body.currentReviewId))
|
||||
) {
|
||||
throw new Error('The original-run preview does not match its retained source');
|
||||
}
|
||||
return {
|
||||
originalRunId: id,
|
||||
backtestId: body.backtestId,
|
||||
available: body.available,
|
||||
reason: body.reason as string | null,
|
||||
currentReviewId: body.currentReviewId as string | null,
|
||||
request,
|
||||
};
|
||||
},
|
||||
async reproduceRun(originalRunId: string, submissionKey: string): Promise<Run> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(
|
||||
`${API_BASE_URL}/api/v2/runs/${encodeURIComponent(originalRunId)}/reproduce`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Idempotency-Key': submissionKey },
|
||||
body: '{}',
|
||||
}
|
||||
);
|
||||
} catch {
|
||||
throw new ReproductionSubmissionError(
|
||||
'The reproduction response was lost. Retry with this same submission key to recover the accepted run.',
|
||||
null
|
||||
);
|
||||
}
|
||||
if (!response.ok) {
|
||||
const error = await requestError(response, 'Could not reproduce original run');
|
||||
throw new ReproductionSubmissionError(error.message, response.status);
|
||||
}
|
||||
const body: unknown = await response.json().catch(() => null);
|
||||
if (
|
||||
response.status !== 202 ||
|
||||
!isRecord(body) ||
|
||||
typeof body.id !== 'string' ||
|
||||
!body.id ||
|
||||
body.id === originalRunId ||
|
||||
typeof body.backtestId !== 'string' ||
|
||||
body.originalRunId !== originalRunId ||
|
||||
!['pending', 'running', 'completed', 'failed', 'cancelled', 'paused'].includes(
|
||||
String(body.status)
|
||||
)
|
||||
) {
|
||||
throw new ReproductionSubmissionError(
|
||||
'The accepted reproduction response could not be verified. Retry with this same submission key.',
|
||||
null
|
||||
);
|
||||
}
|
||||
return body as unknown as Run;
|
||||
},
|
||||
async reproductionComparison(
|
||||
id: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<CanonicalReproductionComparison> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/api/v2/runs/${encodeURIComponent(id)}/reproduction-comparison`,
|
||||
{ signal }
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw await requestError(response, 'Could not load reproduction comparison');
|
||||
}
|
||||
const body: unknown = await response.json();
|
||||
if (
|
||||
!isRecord(body) ||
|
||||
body.version !== 'canonical-reproduction/v1' ||
|
||||
body.runId !== id ||
|
||||
typeof body.backtestId !== 'string' ||
|
||||
!nullableText(body.originalRunId) ||
|
||||
!nullableText(body.reason) ||
|
||||
!hashOrNull(body.originalHash) ||
|
||||
!hashOrNull(body.reproducedHash) ||
|
||||
!['not-reproduction', 'pending', 'unavailable', 'match', 'mismatch'].includes(
|
||||
String(body.status)
|
||||
) ||
|
||||
(['match', 'mismatch'].includes(String(body.status)) &&
|
||||
(!body.originalRunId || !body.originalHash || !body.reproducedHash)) ||
|
||||
(body.status === 'match' && body.originalHash !== body.reproducedHash) ||
|
||||
(body.status === 'mismatch' && body.originalHash === body.reproducedHash)
|
||||
) {
|
||||
throw new Error('The reproduction comparison response could not be verified');
|
||||
}
|
||||
return body as unknown as CanonicalReproductionComparison;
|
||||
},
|
||||
|
||||
async canonicalCapability(id: string, signal?: AbortSignal): Promise<CanonicalRunCapability> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/api/v2/backtests/${encodeURIComponent(id)}/canonical-capability`,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue