feat(research): run durable chronological validation and retain reports

This commit is contained in:
Boki 2026-10-24 12:03:41 -04:00
parent 67ab30cb7a
commit abd1c5b83d
42 changed files with 5345 additions and 74 deletions

View file

@ -3,6 +3,7 @@ import { z } from 'zod';
import { getCredentialHttpPrincipal } from '@stock-bot/di';
import { getLogger } from '@stock-bot/logger';
import type { IServiceContainer } from '@stock-bot/services';
import { ChronologicalValidationService } from '../services/chronological-validation.service';
import {
ExperimentError,
experimentId,
@ -12,12 +13,18 @@ import {
} from '../services/experiment-input';
import { parseExperimentRetry } from '../services/experiment-retry';
import { ExperimentsService } from '../services/experiments.service';
import {
parseValidationFinalization,
parseValidationSelection,
parseValidationSubmission,
} from '../services/validation-evidence';
import { parseChronologicalValidation } from '../services/validation-preview';
/** Mounted HTTP security owns read authorization; every mutation additionally requires credential proof. */
export function createExperimentRoutes(container: IServiceContainer) {
const app = new Hono();
const service = new ExperimentsService(container);
const validations = new ChronologicalValidationService(container);
const logger = getLogger('experiments-api');
const credentials: MiddlewareHandler = async (c, next) => {
c.header('Cache-Control', 'no-store');
@ -47,6 +54,66 @@ export function createExperimentRoutes(container: IServiceContainer) {
const input = parseChronologicalValidation(await body(c.req.raw));
return c.json({ preview: await service.previewValidation(input) });
});
app.post('/api/v2/experiments/validation', async c => {
const input = parseValidationSubmission(await body(c.req.raw));
const validation = await validations.submit(
input,
experimentReceipt({
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
key: c.req.header('Idempotency-Key') ?? '',
})
);
c.header('Location', `/api/v2/experiments/validation/${validation.id}`);
return c.json({ validation }, 202);
});
app.get('/api/v2/experiments/validation', async c =>
c.json({ validations: await validations.list(c.req.query('backtestId')) })
);
app.get('/api/v2/experiments/validation/:id', async c =>
c.json({ validation: await validations.get(experimentId(c.req.param('id'))) })
);
app.post('/api/v2/experiments/validation/:id/select', async c => {
const id = experimentId(c.req.param('id')),
input = parseValidationSelection(await body(c.req.raw));
return c.json(
await validations.select(
id,
input,
experimentReceipt({
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
key: c.req.header('Idempotency-Key') ?? '',
})
),
202
);
});
app.post('/api/v2/experiments/validation/:id/finalize', async c => {
const id = experimentId(c.req.param('id')),
input = parseValidationFinalization(await body(c.req.raw));
return c.json(
await validations.finalize(
id,
input,
experimentReceipt({
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
key: c.req.header('Idempotency-Key') ?? '',
})
),
202
);
});
app.post('/api/v2/experiments/validation/:id/cancel', async c => {
const id = experimentId(c.req.param('id'));
if (
!z
.object({})
.strict()
.safeParse(await body(c.req.raw)).success
) {
throw new ExperimentError(400, 'Cancellation accepts an empty object only');
}
return c.json({ validation: await validations.cancel(id) });
});
app.post('/api/v2/experiments/preview', async c => {
const input = parseExperimentInput(await body(c.req.raw));
return c.json({ preview: await service.preview(input) });