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,6 +1,6 @@
|
|||
import { Hono } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { getVerifiedHttpPrincipal } from '@stock-bot/di';
|
||||
import { getCredentialHttpPrincipal, getVerifiedHttpPrincipal } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import {
|
||||
|
|
@ -13,6 +13,7 @@ import {
|
|||
CatalogExecutionUnavailableError,
|
||||
RunSubmissionConflictError,
|
||||
} from '../services/backtest-v2.service';
|
||||
import { RunReproductionError } from '../services/run-reproduction';
|
||||
import { StrategyCatalogError } from '../services/strategy-catalog.service';
|
||||
|
||||
const logger = getLogger('backtests-v2-routes');
|
||||
|
|
@ -184,6 +185,79 @@ export function createBacktestV2Routes(container: IServiceContainer) {
|
|||
}
|
||||
});
|
||||
|
||||
app.get('/api/v2/runs/:id/reproduction', async c => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
const id = z.string().uuid().safeParse(c.req.param('id'));
|
||||
if (!id.success) {
|
||||
return c.json({ error: 'Invalid original run ID' }, 400);
|
||||
}
|
||||
try {
|
||||
return c.json(await backtestService.getReproductionPreview(id.data));
|
||||
} catch (error) {
|
||||
if (error instanceof RunReproductionError) {
|
||||
return c.json({ error: error.message, code: error.code }, error.status);
|
||||
}
|
||||
logger.error('Failed to preview reproduction', error);
|
||||
return c.json({ error: 'Could not inspect original run reproduction' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/v2/runs/:id/reproduce', async c => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
const principal = getCredentialHttpPrincipal(c.req.raw);
|
||||
if (!principal) {
|
||||
return c.json({ error: 'Verified credential principal required' }, 401);
|
||||
}
|
||||
let originalRunId: string;
|
||||
let key: string;
|
||||
try {
|
||||
originalRunId = z.string().uuid().parse(c.req.param('id'));
|
||||
key = z
|
||||
.string()
|
||||
.regex(/^[\x21-\x7e]{1,200}$/)
|
||||
.parse(c.req.header('Idempotency-Key'));
|
||||
z.object({})
|
||||
.strict()
|
||||
.parse(await c.req.json());
|
||||
} catch {
|
||||
return c.json(
|
||||
{ error: 'Use a valid run ID, an empty object and a 1–200 visible ASCII Idempotency-Key' },
|
||||
400
|
||||
);
|
||||
}
|
||||
try {
|
||||
const run = await backtestService.reproduceRun(originalRunId, { principal, key });
|
||||
c.header('Location', `/api/v2/runs/${run.id}`);
|
||||
return c.json(run, 202);
|
||||
} catch (error) {
|
||||
if (error instanceof RunReproductionError) {
|
||||
return c.json({ error: error.message, code: error.code }, error.status);
|
||||
}
|
||||
if (error instanceof BacktestRetentionError || error instanceof RunSubmissionConflictError) {
|
||||
return c.json({ error: error.message }, 409);
|
||||
}
|
||||
logger.error('Failed to reproduce original run', error);
|
||||
return c.json({ error: 'Could not reproduce original run' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/v2/runs/:id/reproduction-comparison', async c => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
const id = z.string().uuid().safeParse(c.req.param('id'));
|
||||
if (!id.success) {
|
||||
return c.json({ error: 'Invalid run ID' }, 400);
|
||||
}
|
||||
try {
|
||||
return c.json(await backtestService.getReproductionComparison(id.data));
|
||||
} catch (error) {
|
||||
if (error instanceof RunReproductionError) {
|
||||
return c.json({ error: error.message, code: error.code }, error.status);
|
||||
}
|
||||
logger.error('Failed to compare reproduction', error);
|
||||
return c.json({ error: 'Could not compare retained reproduction results' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/v2/runs/:id', async c => {
|
||||
try {
|
||||
const run = await backtestService.getRun(c.req.param('id'));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue