import { z } from 'zod'; export const text = z.string().min(1); export const instant = z.number().int().nonnegative().max(8_640_000_000_000_000); export const sequence = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER); export const price = z.number().finite().positive(); export const day = z .string() .regex(/^\d{4}-\d{2}-\d{2}$/) .refine(value => { const parsed = Date.parse(value); return Number.isFinite(parsed) && new Date(parsed).toISOString().slice(0, 10) === value; }, 'Invalid session date'); export const hash = z .string() .length(64) .regex(/^[a-f0-9]{64}$/); export const metadataSchema = z .object({ calendars: z .array( z .object({ id: text, venue: text, timeZone: text, revision: text, source: text, startDate: day, endDate: day, sessions: z .array(z.object({ date: day, open: instant, close: instant }).strict()) .min(1), }) .strict() ) .min(1), series: z .array( z .object({ id: text, instrumentId: text, interval: z.enum(['1m', '5m', '15m', '1h', '1d']), calendarId: text, venue: text, source: z.object({ provider: text, revision: text }).strict(), timestamp: z.enum(['session-date', 'interval-start', 'interval-end']), availability: z.enum(['explicit', 'interval-end-assumption']), adjustment: z.enum(['raw', 'provider-adjusted']), }) .strict() ) .min(1), executionSeries: z.array(z.object({ instrumentId: text, seriesId: text }).strict()).min(1), bounds: z.object({ warmupStart: instant, tradeStart: instant, tradeEnd: instant }).strict(), universe: z .object({ selection: z.enum(['point-in-time', 'current-constituents', 'unknown']), asOf: instant.nullable(), revision: text, instrumentIds: z.array(text).min(1), }) .strict(), corporateActions: z .array( z .object({ instrumentId: text, status: z.enum(['unknown', 'action-free', 'known-actions']), start: instant, end: instant, revision: text, actions: z.array( z .object({ effectiveAt: instant, kind: z.enum(['split', 'dividend', 'other']), reference: text, }) .strict() ), }) .strict() ) .min(1), }) .strict(); export const manifestSchema = metadataSchema .extend({ version: z.literal(2), hash, normalizationRevision: z.literal('historical-normalization-v2'), parentDatasetHash: hash.nullable(), volumePolicy: z.literal('reported'), qualityPolicy: z.literal('reject-invalid-no-interpolation'), intradayPolicy: z.literal('full-duration-session-contained'), eligibility: z.enum(['eligible', 'exploratory', 'ineligible']), limitations: z.array(text), barCount: z.number().int().positive(), }) .strict(); /** Response shapes add cheap consistency checks; content/hash validation stays server-side. */ export const responseMetadataSchema = metadataSchema.extend({ calendars: metadataSchema.shape.calendars.element .extend({ sessions: metadataSchema.shape.calendars.element.shape.sessions.element .refine(value => value.open < value.close) .array() .min(1), }) .array() .min(1), bounds: metadataSchema.shape.bounds.refine( value => value.warmupStart <= value.tradeStart && value.tradeStart < value.tradeEnd ), }); export const preparationManifestSchema = responseMetadataSchema .extend({ ...manifestSchema.omit({ calendars: true, series: true, executionSeries: true, bounds: true, universe: true, corporateActions: true, }).shape, parentDatasetHash: z.null(), barCount: z.number().int().positive().safe(), }) .strict(); /** Structural workbench policy, not authentication of retained dataset bytes. */ export function datasetUnavailableReason(manifest: { eligibility: string; executionSeries: readonly unknown[]; series: readonly unknown[]; universe: { instrumentIds: readonly string[] }; }): string | null { if (manifest.eligibility !== 'eligible') { return 'Canonical research requires an eligible dataset with known timing and action coverage'; } if ( manifest.executionSeries.length !== 1 || manifest.series.length !== 1 || manifest.universe.instrumentIds.length !== 1 ) { return 'Canonical workbench currently supports one execution asset and one retained series'; } return null; }