perf(research): add compact replay projections and representative profiling

This commit is contained in:
Boki 2026-10-23 10:42:31 -04:00
parent 19e9dad68b
commit e3bd722e31
21 changed files with 2830 additions and 130 deletions

View file

@ -176,6 +176,72 @@ const resultSchema = z
.strict();
export type ReplayStep = z.infer<typeof stepSchema>;
export type ReplayResult = z.infer<typeof resultSchema>;
const nonnegative = decimal.refine(value => !value.startsWith('-'));
const positive = nonnegative.refine(value => value !== '0.00000000');
const ownerValuationSchema = z
.object({
ownerId: id,
initialCapital: nonnegative,
markedHoldings: nonnegative,
remainingCost: nonnegative,
realizedGrossPnl: decimal,
unrealizedPnl: decimal,
fees: nonnegative,
equityContribution: decimal,
})
.strict();
export const financialProjectionSchema = z
.object({
valuation: z
.object({
cash: nonnegative,
markedHoldings: nonnegative,
equity: nonnegative,
remainingCost: nonnegative,
realizedGrossPnl: decimal,
unrealizedPnl: decimal,
fees: nonnegative,
owners: z.array(ownerValuationSchema),
})
.strict(),
fills: z.array(
z
.object({
commandId: id,
eventTime: time,
ownerId: id,
decision: z.object({ ownerId: id, decisionId: id, sequence: counter }).strict(),
orderId: id,
fillId: id,
instrumentId: id,
side: z.enum(['buy', 'sell']),
quantity: positive,
price: positive,
fee: z.object({ feeId: id, amount: nonnegative }).strict(),
// Gross notional cash movement; the separate fee posting is not included.
cashDelta: decimal,
quantityDelta: decimal,
releasedBasis: nonnegative,
realizedGrossPnl: decimal,
})
.strict()
.refine(fill => fill.ownerId === fill.decision.ownerId, 'Fill ownership mismatch')
),
})
.strict();
const projectionResultSchema = resultSchema
.omit({ receipts: true })
.extend({
format: z.literal('projection'),
projectionVersion: z.literal(1),
financialVersion: z.literal(1),
projection: financialProjectionSchema,
})
.strict();
export type ReplayProjectionResult = z.infer<typeof projectionResultSchema>;
export const replayResultFormatSchema = z.enum(['trace', 'projection']);
export type ReplayResultFormat = z.infer<typeof replayResultFormatSchema>;
export type ReplayOutput = ReplayResult | ReplayProjectionResult;
export type DecisionContext = Extract<ReplayStep['step'], { type: 'decisionBatch' }>;
export type PublishedObservation = z.infer<typeof observationSchema>;
@ -193,7 +259,14 @@ export function parseReplayResult(
identity: ReplayIdentity,
expectedPolicy?: ReplayConfig
): ReplayResult {
const result = resultSchema.parse(JSON.parse(input));
return parseReplayResultValue(JSON.parse(input), identity, expectedPolicy);
}
function checkResultIdentity(
result: ReplayOutput,
identity: ReplayIdentity,
expectedPolicy?: ReplayConfig
): void {
sameIdentity(result.identity, identity);
const ledger = result.policy.matching.risk.ledger,
portfolio = result.view.portfolio;
@ -210,8 +283,39 @@ export function parseReplayResult(
) {
throw new Error('Native replay result identity mismatch');
}
}
export function parseReplayResultValue(
input: unknown,
identity: ReplayIdentity,
expectedPolicy?: ReplayConfig
): ReplayResult {
const result = resultSchema.parse(input);
checkResultIdentity(result, identity, expectedPolicy);
return result;
}
export function parseReplayProjectionValue(
input: unknown,
identity: ReplayIdentity,
expectedPolicy?: ReplayConfig
): ReplayProjectionResult {
const result = projectionResultSchema.parse(input);
checkResultIdentity(result, identity, expectedPolicy);
if (
result.projection.valuation.cash !== result.view.portfolio.cash ||
result.projection.valuation.equity !== result.view.equity
) {
throw new Error('Native replay projection does not match terminal view');
}
return result;
}
export function parseReplayProjectionResult(
input: string,
identity: ReplayIdentity,
expectedPolicy?: ReplayConfig
): ReplayProjectionResult {
return parseReplayProjectionValue(JSON.parse(input), identity, expectedPolicy);
}
export const replayTraceSchema = z
.object({
@ -220,6 +324,7 @@ export const replayTraceSchema = z
dataset: z.unknown(),
config: replayConfigSchema,
eventBudget: counter.positive(),
resultFormat: replayResultFormatSchema.optional(),
trace: z.array(
z
.object({