94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
/** Pure isolated-candidate contracts. No runtime, artifact import or financial implementation. */
|
|
import { z } from 'zod';
|
|
import { parseFinancialDecimal } from '@stock-bot/types';
|
|
import type { financialDecimal } from '@stock-bot/types';
|
|
|
|
export type { financialDecimal };
|
|
|
|
const hash = z.string().regex(/^[a-f0-9]{64}$/);
|
|
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)
|
|
);
|
|
})
|
|
);
|
|
export const candidatePackageExecutionSchema = z
|
|
.object({
|
|
kind: z.literal('isolated-candidate/v1'),
|
|
/** Fixed author declaration for all parameter combinations, not a verified leakage claim. */
|
|
warmupBars: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
|
})
|
|
.strict();
|
|
export const retainedProgramSelectionSchema = z
|
|
.object({
|
|
ownerId: identity,
|
|
strategyId: identity,
|
|
strategyVersion: identity,
|
|
seed: z.number().int().min(0).max(0xffffffff),
|
|
instrumentId: identity,
|
|
seriesId: identity,
|
|
/** Strategy sizing input. Rust portfolio constraints remain the financial authority. */
|
|
ownerBudget: z
|
|
.string()
|
|
.transform(parseFinancialDecimal)
|
|
.refine(value => !value.startsWith('-') && value !== '0.00000000'),
|
|
parameters: z.record(z.union([z.string(), z.number().finite(), z.boolean()])),
|
|
})
|
|
.strict();
|
|
export type RetainedProgramSelection = z.infer<typeof retainedProgramSelectionSchema>;
|
|
|
|
export const candidateSandboxLimits = {
|
|
memoryBytes: 536870912,
|
|
cpuQuota: 1,
|
|
pids: 64,
|
|
tmpBytes: 16777216,
|
|
wallMs: 300000,
|
|
frameBytes: 8388608,
|
|
stderrBytes: 65536,
|
|
} as const;
|
|
export const candidateSandboxProfileSchema = z
|
|
.object({
|
|
version: z.literal(1),
|
|
policy: z.literal('linux-docker-guardian/v1'),
|
|
protocol: z.literal('candidate-controller/v1'),
|
|
platform: z.literal('linux'),
|
|
arch: z.literal('x64'),
|
|
hostSha256: hash,
|
|
guardianSha256: hash,
|
|
bunSha256: hash,
|
|
limits: z
|
|
.object({
|
|
memoryBytes: z.literal(candidateSandboxLimits.memoryBytes),
|
|
cpuQuota: z.literal(candidateSandboxLimits.cpuQuota),
|
|
pids: z.literal(candidateSandboxLimits.pids),
|
|
tmpBytes: z.literal(candidateSandboxLimits.tmpBytes),
|
|
wallMs: z.literal(candidateSandboxLimits.wallMs),
|
|
frameBytes: z.literal(candidateSandboxLimits.frameBytes),
|
|
stderrBytes: z.literal(candidateSandboxLimits.stderrBytes),
|
|
})
|
|
.strict(),
|
|
})
|
|
.strict();
|
|
export const candidateExecutionSchema = z
|
|
.object({
|
|
kind: z.literal('typescript-isolated-candidate/v1'),
|
|
imageId: z.string().regex(/^sha256:[a-f0-9]{64}$/),
|
|
profile: candidateSandboxProfileSchema,
|
|
})
|
|
.strict();
|
|
export type CandidateExecution = z.infer<typeof candidateExecutionSchema>;
|
|
|
|
export const candidateOverrideSchema = z
|
|
.object({
|
|
versionId: z
|
|
.string()
|
|
.uuid()
|
|
.transform(value => value.toLowerCase()),
|
|
parameters: retainedProgramSelectionSchema.shape.parameters,
|
|
})
|
|
.strict();
|