215 lines
6.7 KiB
TypeScript
215 lines
6.7 KiB
TypeScript
/** Explicit human promotion into a trusted paper host. No live capability or code import. */
|
|
import { z } from 'zod';
|
|
import type { financialDecimal } from '@stock-bot/types';
|
|
import { retainedProgramSelectionSchema } from './candidate-execution';
|
|
import { replayConfigSchema } from './canonical-replay';
|
|
import { researchFindingSourceSchema } from './research';
|
|
|
|
export type { financialDecimal };
|
|
|
|
const id = z
|
|
.string()
|
|
.min(1)
|
|
.refine(value => [...value].every(c => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127));
|
|
const uuid = z
|
|
.string()
|
|
.uuid()
|
|
.transform(value => value.toLowerCase());
|
|
const hash = z.string().regex(/^[a-f0-9]{64}$/);
|
|
const counter = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER);
|
|
const time = z.number().int().min(-Number.MAX_SAFE_INTEGER).max(Number.MAX_SAFE_INTEGER);
|
|
const riskSession = replayConfigSchema.shape.matching.shape.risk.shape.ledger.shape.riskSession;
|
|
export const paperPolicySchema = z
|
|
.object({
|
|
version: z.literal(1),
|
|
sourceId: id,
|
|
priceBasis: z.literal('raw'),
|
|
maxFeedAgeMs: counter.refine(v => v > 0),
|
|
observations: z.array(
|
|
z
|
|
.object({
|
|
seriesId: id,
|
|
instrumentId: id,
|
|
intervalMs: counter.refine(v => v > 0),
|
|
staleAfterMs: counter,
|
|
})
|
|
.strict()
|
|
),
|
|
cadence: z.array(z.object({ ownerId: id, seriesIds: z.array(id).min(1) }).strict()),
|
|
calendars: z
|
|
.array(
|
|
z
|
|
.object({
|
|
instrumentId: id,
|
|
calendarId: id,
|
|
revision: id,
|
|
sessions: z
|
|
.array(z.object({ sessionId: id, opensAt: time, closesAt: time }).strict())
|
|
.min(1),
|
|
})
|
|
.strict()
|
|
)
|
|
.min(1),
|
|
riskBoundaries: z.array(riskSession),
|
|
})
|
|
.strict()
|
|
.transform(value => ({
|
|
...value,
|
|
calendars: value.calendars.map(calendar => ({
|
|
...calendar,
|
|
sessions: [...calendar.sessions].sort((a, b) => a.opensAt - b.opensAt),
|
|
})),
|
|
riskBoundaries: [...value.riskBoundaries].sort((a, b) => a.startsAt - b.startsAt),
|
|
}));
|
|
export const deployedPaperConfigurationSchema = z
|
|
.object({
|
|
version: z.literal(2),
|
|
matching: replayConfigSchema.shape.matching.extend({
|
|
series: z
|
|
.array(
|
|
replayConfigSchema.shape.matching.shape.series.element.extend({
|
|
mode: z.literal('Quote'),
|
|
})
|
|
)
|
|
.length(1),
|
|
}),
|
|
strategies: z.tuple([retainedProgramSelectionSchema]),
|
|
policy: paperPolicySchema,
|
|
})
|
|
.strict();
|
|
export const paperHostIdentitySchema = z
|
|
.object({
|
|
version: z.literal('paper-host/v1'),
|
|
hostSha256: hash,
|
|
engineWrapperSha256: hash,
|
|
engineBinarySha256: hash,
|
|
dependencyLockSha256: hash,
|
|
runtime: z.literal('bun'),
|
|
runtimeVersion: id,
|
|
platform: id,
|
|
arch: id,
|
|
})
|
|
.strict();
|
|
/** Only transport and future clocks are operator inputs. Economics come from the selected cost trial. */
|
|
export const paperTransportSchema = z
|
|
.object({
|
|
accountId: id,
|
|
sessionId: id,
|
|
quoteSeriesId: id,
|
|
riskSession,
|
|
policy: paperPolicySchema,
|
|
})
|
|
.strict();
|
|
export const paperPromotionInputSchema = z
|
|
.object({
|
|
name: z.string().trim().min(1),
|
|
outOfSample: researchFindingSourceSchema.refine(
|
|
value => value.kind !== 'experiment/v1',
|
|
'Select a sealed chronological or walk-forward report'
|
|
),
|
|
testRunId: uuid,
|
|
costExperimentId: uuid,
|
|
costPreviewHash: hash,
|
|
costRunId: uuid,
|
|
transport: paperTransportSchema,
|
|
reason: z.string().trim().min(1),
|
|
acknowledgeTrustedCode: z.literal(true),
|
|
})
|
|
.strict();
|
|
export const paperPromotionSubmissionSchema = paperPromotionInputSchema.extend({
|
|
previewHash: hash,
|
|
});
|
|
export const paperMetricEvidenceSchema = z
|
|
.object({
|
|
runId: uuid,
|
|
datasetHash: hash,
|
|
economicHash: hash,
|
|
resultHash: hash,
|
|
metrics: z.record(z.number().finite().nullable()),
|
|
fillCount: counter,
|
|
limitations: z.array(z.string()),
|
|
})
|
|
.strict();
|
|
export const paperPromotionPreviewSchema = z
|
|
.object({
|
|
version: z.literal('paper-promotion-preview/v1'),
|
|
previewHash: hash,
|
|
input: paperPromotionInputSchema,
|
|
strategy: z
|
|
.object({
|
|
familyId: uuid,
|
|
versionId: uuid,
|
|
reviewId: uuid,
|
|
origin: z.enum(['builtin', 'candidate']),
|
|
artifactSha256: hash,
|
|
dependencyLockSha256: hash,
|
|
})
|
|
.strict(),
|
|
configuration: deployedPaperConfigurationSchema,
|
|
host: paperHostIdentitySchema,
|
|
evidence: z
|
|
.object({ test: paperMetricEvidenceSchema, costs: z.array(paperMetricEvidenceSchema).min(2) })
|
|
.strict(),
|
|
limitations: z.array(z.string()),
|
|
})
|
|
.strict();
|
|
export const paperDeploymentSchema = z
|
|
.object({
|
|
id: uuid,
|
|
version: z.literal('paper-deployment/v1'),
|
|
principal: z.literal('operator'),
|
|
createdAt: z.string().datetime(),
|
|
promotionHash: hash,
|
|
preview: paperPromotionPreviewSchema,
|
|
})
|
|
.strict();
|
|
export const paperDeploymentBindingSchema = z
|
|
.object({
|
|
deploymentId: uuid,
|
|
promotionHash: hash,
|
|
strategyVersionId: uuid,
|
|
artifactSha256: hash,
|
|
dependencyLockSha256: hash,
|
|
host: paperHostIdentitySchema,
|
|
})
|
|
.strict();
|
|
export const paperReadinessSchema = z
|
|
.object({
|
|
configured: z.boolean(),
|
|
accountId: z.string().nullable(),
|
|
sourceId: z.string().nullable(),
|
|
active: z.boolean(),
|
|
ready: z.boolean(),
|
|
operatorHalted: z.boolean(),
|
|
connected: z.boolean(),
|
|
reasons: z.array(z.string()),
|
|
lastReceivedAt: time.nullable(),
|
|
lastAvailableAt: time.nullable(),
|
|
requiresHistoricalRecovery: z.boolean(),
|
|
pendingInput: z.string().nullable(),
|
|
pendingBatch: z.string().nullable(),
|
|
pendingDecisionSequence: counter.nullable(),
|
|
nativeStatus: z
|
|
.union([
|
|
z.literal('Running'),
|
|
z.literal('Complete'),
|
|
z.object({ Failed: z.object({ diagnostic: z.string() }).strict() }).strict(),
|
|
])
|
|
.nullable(),
|
|
risk: z.object({ dailyHalted: z.boolean(), drawdownHalted: z.boolean() }).strict().nullable(),
|
|
})
|
|
.strict();
|
|
export const paperDeploymentStatusSchema = z
|
|
.object({
|
|
deploymentId: uuid,
|
|
boundDeploymentId: uuid.nullable(),
|
|
readiness: paperReadinessSchema,
|
|
})
|
|
.strict();
|
|
export type PaperPromotionInput = z.input<typeof paperPromotionInputSchema>;
|
|
export type PaperPromotionPreview = z.infer<typeof paperPromotionPreviewSchema>;
|
|
export type PaperDeployment = z.infer<typeof paperDeploymentSchema>;
|
|
export type PaperDeploymentBinding = z.infer<typeof paperDeploymentBindingSchema>;
|
|
export type PaperHostIdentity = z.infer<typeof paperHostIdentitySchema>;
|
|
export type PaperDeploymentStatus = z.infer<typeof paperDeploymentStatusSchema>;
|
|
export type DeployedPaperConfiguration = z.infer<typeof deployedPaperConfigurationSchema>;
|