feat(research): run retained strategies through durable canonical engine
This commit is contained in:
parent
d929db707a
commit
618fb9e33c
58 changed files with 6596 additions and 779 deletions
|
|
@ -1,7 +1,18 @@
|
|||
import { z } from 'zod';
|
||||
import {
|
||||
canonicalBuildIdentitySchema,
|
||||
canonicalRunRequestSchema,
|
||||
parseCanonicalFinancialResult,
|
||||
type CanonicalBuildIdentity,
|
||||
type CanonicalRunRequest,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import { canonicalStrategyJson } from '@stock-bot/stock-config/strategy';
|
||||
import type { CanonicalBacktestAnalytics } from '@stock-bot/types';
|
||||
import { replayTraceSchema } from '../contracts/native-replay';
|
||||
import type { ReplayReportResult } from '../contracts/native-replay';
|
||||
import { strategyReplayRequestSchema } from '../contracts/strategy-replay';
|
||||
import { BacktestConfigSchema } from '../types';
|
||||
import { buildCanonicalBacktestReport } from './canonical-result';
|
||||
|
||||
const finite = z.number().finite();
|
||||
const legacyRequestSchema = z.object({
|
||||
|
|
@ -22,12 +33,61 @@ const legacyRequestSchema = z.object({
|
|||
),
|
||||
});
|
||||
|
||||
export const retainedStrategyRequestSchema = z
|
||||
.object({
|
||||
type: z.literal('runRetainedStrategy'),
|
||||
request: canonicalRunRequestSchema,
|
||||
dataset: z.unknown(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export const nativeWorkerRequestSchema = z.union([
|
||||
legacyRequestSchema,
|
||||
replayTraceSchema,
|
||||
strategyReplayRequestSchema,
|
||||
retainedStrategyRequestSchema,
|
||||
]);
|
||||
|
||||
export interface RetainedStrategyResult {
|
||||
type: 'retainedStrategyResult';
|
||||
version: 1;
|
||||
build: CanonicalBuildIdentity;
|
||||
artifactHash: string;
|
||||
financial: ReplayReportResult;
|
||||
analytics: CanonicalBacktestAnalytics;
|
||||
}
|
||||
|
||||
const retainedStrategyResultSchema = z
|
||||
.object({
|
||||
type: z.literal('retainedStrategyResult'),
|
||||
version: z.literal(1),
|
||||
build: canonicalBuildIdentitySchema,
|
||||
artifactHash: z.string().regex(/^[a-f0-9]{64}$/),
|
||||
financial: z.unknown(),
|
||||
analytics: z.unknown(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
/** Financial facts are validated, never recalculated. F08 alone owns statistical reporting. */
|
||||
export function parseRetainedStrategyResult(
|
||||
input: string,
|
||||
request: CanonicalRunRequest
|
||||
): RetainedStrategyResult {
|
||||
const envelope = retainedStrategyResultSchema.parse(JSON.parse(input));
|
||||
if (
|
||||
canonicalStrategyJson(envelope.build) !== canonicalStrategyJson(request.build) ||
|
||||
envelope.artifactHash !== request.strategy.package.artifact.sha256
|
||||
) {
|
||||
throw new Error('Retained strategy result build/artifact identity mismatch');
|
||||
}
|
||||
const financial = parseCanonicalFinancialResult(envelope.financial, request);
|
||||
const analytics = buildCanonicalBacktestReport(financial, request.analyticsPolicy).analytics;
|
||||
if (canonicalStrategyJson(envelope.analytics) !== canonicalStrategyJson(analytics)) {
|
||||
throw new Error('Retained strategy result analytics do not match the frozen policy');
|
||||
}
|
||||
return { ...envelope, financial, analytics };
|
||||
}
|
||||
|
||||
export const nativeWorkerMessageSchema = z.discriminatedUnion('type', [
|
||||
z.object({ type: z.literal('progress'), progress: finite.min(0).max(100), message: z.string() }),
|
||||
z.object({ type: z.literal('result'), result: z.string() }),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue