feat(research): run trusted SMA strategies through native workers
This commit is contained in:
parent
4f6e44a853
commit
ca5abdde0e
16 changed files with 1682 additions and 28 deletions
51
apps/stock/orchestrator/src/contracts/builtin-strategy.ts
Normal file
51
apps/stock/orchestrator/src/contracts/builtin-strategy.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/** Trusted built-in selection only. No callbacks, code paths or checkpoint transport. */
|
||||
import { z } from 'zod';
|
||||
import { parseFinancialDecimal } from '@stock-bot/types';
|
||||
import { maxStrategyPeriod } from './strategies';
|
||||
import { builtinStrategy } from './strategy-names';
|
||||
|
||||
const identity = z
|
||||
.string()
|
||||
.min(1)
|
||||
.refine(value =>
|
||||
[...value].every(character => {
|
||||
const code = character.charCodeAt(0);
|
||||
return (
|
||||
code >= 32 && code !== 127 && !(character.length === 1 && code >= 0xd800 && code <= 0xdfff)
|
||||
);
|
||||
})
|
||||
);
|
||||
const period = z.number().int().min(1).max(maxStrategyPeriod);
|
||||
|
||||
export const builtinStrategySelectionSchema = z
|
||||
.object({
|
||||
ownerId: identity,
|
||||
strategyId: z
|
||||
.string()
|
||||
.refine(value => builtinStrategy(value) === 'sma_crossover', 'Unsupported canonical strategy')
|
||||
.transform(() => 'sma_crossover' as const),
|
||||
strategyVersion: z.literal('sma-crossover-long-only/v1'),
|
||||
seed: z.number().int().min(0).max(0xffffffff),
|
||||
instrumentId: identity,
|
||||
seriesId: identity,
|
||||
ownerBudget: z
|
||||
.string()
|
||||
.transform(value => parseFinancialDecimal(value))
|
||||
.refine(
|
||||
value => !value.startsWith('-') && value !== '0.00000000',
|
||||
'Owner budget must be positive'
|
||||
),
|
||||
parameters: z
|
||||
.object({
|
||||
fastPeriod: period.default(5),
|
||||
slowPeriod: period.min(2).default(15),
|
||||
})
|
||||
.strict()
|
||||
.refine(
|
||||
value => value.fastPeriod < value.slowPeriod,
|
||||
'SMA fastPeriod must be below slowPeriod'
|
||||
),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export type BuiltinStrategySelection = z.infer<typeof builtinStrategySelectionSchema>;
|
||||
72
apps/stock/orchestrator/src/contracts/strategy-replay.ts
Normal file
72
apps/stock/orchestrator/src/contracts/strategy-replay.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/** Data-only trusted built-in execution protocol; no strategy code or native imports. */
|
||||
import { z } from 'zod';
|
||||
import { builtinStrategySelectionSchema, type BuiltinStrategySelection } from './builtin-strategy';
|
||||
import {
|
||||
parseReplayProjectionValue,
|
||||
parseReplayReportValue,
|
||||
parseReplayResultValue,
|
||||
replayConfigSchema,
|
||||
replayResultFormatSchema,
|
||||
type ReplayConfig,
|
||||
type ReplayIdentity,
|
||||
type ReplayOutput,
|
||||
type ReplayResultFormat,
|
||||
} from './native-replay';
|
||||
|
||||
export const strategyReplayRequestSchema = z
|
||||
.object({
|
||||
type: z.literal('runStrategyReplay'),
|
||||
version: z.literal(1),
|
||||
dataset: z.unknown(),
|
||||
config: replayConfigSchema,
|
||||
eventBudget: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
|
||||
resultFormat: replayResultFormatSchema.optional(),
|
||||
strategies: z.array(builtinStrategySelectionSchema).min(1),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export type StrategyReplayRequest = z.input<typeof strategyReplayRequestSchema>;
|
||||
|
||||
const strategyReplayResultSchema = z
|
||||
.object({
|
||||
type: z.literal('strategyReplayResult'),
|
||||
version: z.literal(1),
|
||||
strategies: z.array(builtinStrategySelectionSchema).min(1),
|
||||
result: z.unknown(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export interface StrategyReplayResult {
|
||||
type: 'strategyReplayResult';
|
||||
version: 1;
|
||||
strategies: BuiltinStrategySelection[];
|
||||
result: ReplayOutput;
|
||||
}
|
||||
|
||||
export interface StrategyReplayExpectation {
|
||||
strategies: readonly BuiltinStrategySelection[];
|
||||
identity: ReplayIdentity;
|
||||
policy: ReplayConfig;
|
||||
format: ReplayResultFormat;
|
||||
}
|
||||
|
||||
/** Strategy selection is additional provenance; Rust remains the only result authority. */
|
||||
export function parseStrategyReplayResult(
|
||||
input: string,
|
||||
expected: StrategyReplayExpectation
|
||||
): StrategyReplayResult {
|
||||
const envelope = strategyReplayResultSchema.parse(JSON.parse(input));
|
||||
if (JSON.stringify(envelope.strategies) !== JSON.stringify(expected.strategies)) {
|
||||
throw new Error('Native strategy replay selection mismatch');
|
||||
}
|
||||
const parse =
|
||||
expected.format === 'report'
|
||||
? parseReplayReportValue
|
||||
: expected.format === 'projection'
|
||||
? parseReplayProjectionValue
|
||||
: parseReplayResultValue;
|
||||
return {
|
||||
...envelope,
|
||||
result: parse(envelope.result, expected.identity, expected.policy),
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue