feat(paper): gate canonical execution on durable feed readiness
This commit is contained in:
parent
c01e96e4dd
commit
75abae2cfc
24 changed files with 3720 additions and 66 deletions
146
apps/stock/orchestrator/src/contracts/paper-runtime.ts
Normal file
146
apps/stock/orchestrator/src/contracts/paper-runtime.ts
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
/** Explicit trusted producer boundary. No legacy timestamp repair or broker capabilities. */
|
||||
import { z } from 'zod';
|
||||
import { parseFinancialDecimal } from '@stock-bot/types';
|
||||
import { builtinStrategySelectionSchema } from './builtin-strategy';
|
||||
import { canonicalMatchingConfigSchema, matchingCommandSchema } from './canonical-execution';
|
||||
import { replayConfigSchema, 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 duration = counter.refine(value => value > 0, 'Duration must be positive');
|
||||
const decimal = z.string().transform(value => parseFinancialDecimal(value));
|
||||
|
||||
export const paperPolicySchema = z
|
||||
.object({
|
||||
version: z.literal(1),
|
||||
sourceId: id,
|
||||
priceBasis: z.literal('raw'),
|
||||
maxFeedAgeMs: duration,
|
||||
observations: z.array(
|
||||
z
|
||||
.object({
|
||||
seriesId: id,
|
||||
instrumentId: id,
|
||||
intervalMs: duration,
|
||||
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(
|
||||
replayConfigSchema.shape.matching.shape.risk.shape.ledger.shape.riskSession
|
||||
),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export const paperConfigurationSchema = z
|
||||
.object({
|
||||
version: z.literal(1),
|
||||
matching: canonicalMatchingConfigSchema,
|
||||
strategies: z.array(builtinStrategySelectionSchema).min(1),
|
||||
policy: paperPolicySchema,
|
||||
})
|
||||
.strict();
|
||||
|
||||
/** 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>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue