stock-bot/apps/stock/orchestrator/src/contracts/paper-runtime.ts

119 lines
4 KiB
TypeScript

/** Explicit trusted producer boundary. No legacy timestamp repair or broker capabilities. */
import { z } from 'zod';
import {
deployedPaperConfigurationSchema,
paperPolicySchema,
} from '@stock-bot/stock-config/paper-deployments';
import { parseFinancialDecimal } from '@stock-bot/types';
import { builtinStrategySelectionSchema } from './builtin-strategy';
import { canonicalMatchingConfigSchema, matchingCommandSchema } from './canonical-execution';
import { stepSchema } from './native-replay';
const id = matchingCommandSchema.shape.key.shape.instrumentId;
const time = matchingCommandSchema.shape.key.shape.at;
const counter = matchingCommandSchema.shape.key.shape.sourceSequence;
const decimal = z.string().transform(value => parseFinancialDecimal(value));
export { paperPolicySchema } from '@stock-bot/stock-config/paper-deployments';
const legacyPaperConfigurationSchema = z
.object({
version: z.literal(1),
matching: canonicalMatchingConfigSchema,
strategies: z.array(builtinStrategySelectionSchema).min(1),
policy: paperPolicySchema,
})
.strict();
export const paperConfigurationSchema = z.union([
legacyPaperConfigurationSchema,
deployedPaperConfigurationSchema,
]);
/** availableAt is the producer's atomic release time for the whole quote frame.
* Quotes retain their original physical timestamp and source sequence. The
* supplied completed bars were available by this release; quotes never synthesize bars. */
export const paperFrameSchema = z
.object({
version: z.literal(1),
sourceId: id,
connectionId: id,
id,
sequence: counter,
availableAt: time,
quotes: z
.array(
z
.object({
instrumentId: id,
seriesId: id,
observedAt: time,
sourceSequence: counter,
bid: decimal,
ask: decimal,
bidSize: decimal,
askSize: decimal,
})
.strict()
)
.min(1),
observations: z.array(stepSchema.shape.observations.element),
})
.strict();
export const paperConnectionSchema = z.object({ sourceId: id, connectionId: id }).strict();
export const paperActivationSchema = z
.object({ recoverPending: z.boolean().default(false) })
.strict();
export const paperHaltSchema = z.object({ reason: z.string().trim().min(1) }).strict();
export type PaperConfigurationInput = z.input<typeof paperConfigurationSchema>;
export type PaperConfiguration = z.output<typeof paperConfigurationSchema>;
export type PaperPolicy = z.output<typeof paperPolicySchema>;
export type PaperFrameInput = z.input<typeof paperFrameSchema>;
export type PaperFrame = z.output<typeof paperFrameSchema>;
export type PaperConnection = z.infer<typeof paperConnectionSchema>;
export class PaperRuntimeError extends Error {
constructor(
readonly kind: 'invalid-input' | 'not-ready' | 'conflict',
message: string
) {
super(message);
this.name = 'PaperRuntimeError';
}
}
export interface PaperReadiness {
configured: boolean;
accountId: string | null;
sourceId: string | null;
active: boolean;
ready: boolean;
operatorHalted: boolean;
connected: boolean;
reasons: string[];
lastReceivedAt: number | null;
lastAvailableAt: number | null;
requiresHistoricalRecovery: boolean;
pendingInput: string | null;
pendingBatch: string | null;
pendingDecisionSequence: number | null;
nativeStatus: 'Running' | 'Complete' | { Failed: { diagnostic: string } } | null;
risk: { dailyHalted: boolean; drawdownHalted: boolean } | null;
}
export interface PaperRuntime {
initialize(configuration: PaperConfigurationInput): Promise<void>;
connect(connection: PaperConnection): Promise<PaperReadiness>;
ingest(frame: PaperFrameInput): Promise<{
duplicate: boolean;
disposition: 'health-only' | 'admitted';
readiness: PaperReadiness;
}>;
disconnect(connection: PaperConnection): Promise<PaperReadiness>;
activate(options: { recoverPending: boolean }): Promise<PaperReadiness>;
halt(reason: string): Promise<PaperReadiness>;
status(): Promise<PaperReadiness>;
shutdown(): Promise<void>;
}