fix(api): reject retired backtest creation before writes (CL23)
This commit is contained in:
parent
3fc0a272d8
commit
cd1aa61481
10 changed files with 297 additions and 48 deletions
|
|
@ -2,7 +2,7 @@ import { Hono } from 'hono';
|
|||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { isBacktestRequestError } from '@stock-bot/stock-config/backtest';
|
||||
import { BacktestService } from '../services/backtest.service';
|
||||
import { BacktestService, LegacyBacktestCreationRetiredError } from '../services/backtest.service';
|
||||
|
||||
const logger = getLogger('backtest-routes');
|
||||
|
||||
|
|
@ -10,7 +10,7 @@ export function createBacktestRoutes(container: IServiceContainer) {
|
|||
const backtestRoutes = new Hono();
|
||||
const backtestService = new BacktestService(container);
|
||||
|
||||
// Create a new backtest
|
||||
// Retired creation remains an explicit diagnostic for old clients.
|
||||
backtestRoutes.post('/api/backtests', async c => {
|
||||
try {
|
||||
const body = await c.req.json();
|
||||
|
|
@ -32,10 +32,13 @@ export function createBacktestRoutes(container: IServiceContainer) {
|
|||
|
||||
return c.json(backtest, 201);
|
||||
} catch (error) {
|
||||
logger.error('Failed to create backtest', { error });
|
||||
if (error instanceof LegacyBacktestCreationRetiredError) {
|
||||
return c.json({ code: error.code, error: error.message }, 410);
|
||||
}
|
||||
if (isBacktestRequestError(error)) {
|
||||
return c.json({ error: error.message }, 400);
|
||||
}
|
||||
logger.error('Failed to create backtest', { error });
|
||||
return c.json({ error: 'Failed to create backtest' }, 500);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ export function createBacktestV2Routes(container: IServiceContainer) {
|
|||
if (body.autoStart !== undefined && typeof body.autoStart !== 'boolean') {
|
||||
return c.json({ error: 'autoStart must be a boolean' }, 400);
|
||||
}
|
||||
// Omitted autoStart preserves legacy clients. New web clients explicitly save only.
|
||||
// Saving requires explicit autoStart:false; run submission is a separate operation.
|
||||
const { autoStart, ...definition } = body;
|
||||
const backtest = await backtestService.createBacktest(definition, { autoStart });
|
||||
return c.json(backtest, 201);
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ export class BacktestServiceV2 {
|
|||
) {
|
||||
const services = requireServices(container, 'postgres');
|
||||
this.runs = new BacktestRuns(services, request);
|
||||
this.templates = new BacktestTemplates(services, input => this.runs.createRun(input));
|
||||
this.templates = new BacktestTemplates(services);
|
||||
this.results = new BacktestResults(services);
|
||||
this.reproduction = new BacktestReproduction(services, this.results);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,16 @@ export interface BacktestJob {
|
|||
error?: string;
|
||||
}
|
||||
|
||||
/** Compatibility facade: V1 uses the same definitions, runs, and result reader as V2. */
|
||||
export class LegacyBacktestCreationRetiredError extends Error {
|
||||
readonly code = 'legacy_backtest_creation_retired';
|
||||
constructor() {
|
||||
super(
|
||||
'Legacy backtest creation is retired. Create a catalog template with POST /api/v2/backtests, then submit a run through its /runs endpoint.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Historical V1 reads and cancellation use the same saved records as V2. */
|
||||
export class BacktestService {
|
||||
private readonly service: BacktestServiceV2;
|
||||
constructor(container: IServiceContainer, request?: BacktestTransport) {
|
||||
|
|
@ -39,15 +48,9 @@ export class BacktestService {
|
|||
|
||||
async createBacktest(request: BacktestRequest): Promise<BacktestJob> {
|
||||
rejectCatalogVersionReferences(request);
|
||||
const backtest = await this.service.createBacktest(
|
||||
{
|
||||
...request,
|
||||
name: typeof request.config?.name === 'string' ? request.config.name : 'Backtest',
|
||||
},
|
||||
{ autoStart: false }
|
||||
);
|
||||
const run = await this.service.createRun({ backtestId: backtest.id, speedMultiplier: null });
|
||||
return this.toJob(backtest, run);
|
||||
// Reject before saving a template: no legacy definition can execute, and a
|
||||
// failure after its commit would leave an orphan on every client retry.
|
||||
throw new LegacyBacktestCreationRetiredError();
|
||||
}
|
||||
async getBacktest(id: string): Promise<BacktestJob | null> {
|
||||
const backtest = await this.service.getBacktest(id);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import { randomUUID as uuidv4 } from 'node:crypto';
|
||||
import type { PoolClient } from 'pg';
|
||||
import { at } from '@stock-bot/invariants';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import {
|
||||
backtestRequestFromDefinition,
|
||||
backtestSettingsFromDefinition,
|
||||
|
|
@ -23,19 +22,12 @@ import {
|
|||
type Backtest,
|
||||
type BacktestContainer,
|
||||
type BacktestRequest,
|
||||
type Run,
|
||||
type RunRequest,
|
||||
} from './contracts';
|
||||
import { mapBacktest } from './mapping';
|
||||
|
||||
const logger = getLogger('backtest-v2-service');
|
||||
|
||||
/** Focused templates operations; transaction order is preserved. */
|
||||
export class BacktestTemplates {
|
||||
constructor(
|
||||
private readonly container: BacktestContainer,
|
||||
private readonly startRun: (request: RunRequest) => Promise<Run>
|
||||
) {}
|
||||
constructor(private readonly container: BacktestContainer) {}
|
||||
async createBacktest(
|
||||
request: BacktestRequest,
|
||||
options: { autoStart?: boolean } = {}
|
||||
|
|
@ -46,6 +38,9 @@ export class BacktestTemplates {
|
|||
}
|
||||
if (!catalog) {
|
||||
rejectCatalogVersionReferences(request);
|
||||
if (options.autoStart !== false) {
|
||||
throw new HistoricalDataRequestError('Backtest drafts require explicit autoStart:false');
|
||||
}
|
||||
}
|
||||
const backtestId = uuidv4();
|
||||
const result = await this.container.postgres.transaction(async client => {
|
||||
|
|
@ -76,24 +71,7 @@ export class BacktestTemplates {
|
|||
);
|
||||
});
|
||||
|
||||
const backtest = mapBacktest(at(result.rows, 0));
|
||||
|
||||
// Automatically create and start a run for the new backtest
|
||||
if (options.autoStart !== false) {
|
||||
try {
|
||||
await this.startRun({
|
||||
backtestId: backtest.id,
|
||||
speedMultiplier: null, // Max speed (instant)
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to auto-create run for new backtest', {
|
||||
backtestId: backtest.id,
|
||||
error,
|
||||
});
|
||||
// Don't fail the backtest creation if run creation fails
|
||||
}
|
||||
}
|
||||
return backtest;
|
||||
return mapBacktest(at(result.rows, 0));
|
||||
}
|
||||
|
||||
async getBacktest(id: string): Promise<Backtest | null> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue