72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
/** 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),
|
|
};
|
|
}
|