refactor(orchestrator): share pure strategy bookkeeping (CL19)

This commit is contained in:
Boki 2026-10-26 20:33:56 -04:00
parent 984dc1a9cc
commit 6055aeeee9
17 changed files with 297 additions and 157 deletions

View file

@ -1,5 +1,6 @@
import { createHash, randomUUID } from 'node:crypto';
import type { PaperDeploymentBinding } from '@stock-bot/stock-config/paper-deployments';
import { canonicalJson } from '../canonical-json';
import type { StrategyProgram } from '../contracts/canonical-strategy';
import {
paperActivationSchema,
@ -21,7 +22,6 @@ import type {
CanonicalTransaction,
} from '../execution/canonical-paper';
import { CanonicalPaperExecution } from '../execution/CanonicalPaperExecution';
import { canonicalJson } from '../execution/strategy-recovery';
import { planPaperFrame, validatePaperConfiguration, validatePaperFrame } from './feed';
const fingerprint = (value: unknown) =>

View file

@ -3,6 +3,7 @@
import { isDeepStrictEqual } from 'node:util';
import { ZodError } from 'zod';
import {
compareEventKeys,
matchingCommandSchema,
type CanonicalMatchingCommand,
} from '../contracts/canonical-execution';
@ -45,15 +46,6 @@ function unique(values: readonly string[], label: string): void {
invalid(`Duplicate paper ${label}`);
}
}
function keyCompare(a: Key, b: Key): number {
const phase = { Observation: 0, Decision: 1, Execution: 2, SessionClose: 3 };
return (
a.at - b.at ||
phase[a.phase] - phase[b.phase] ||
compareText(a.instrumentId, b.instrumentId) ||
a.sourceSequence - b.sourceSequence
);
}
const observationId = (row: Observation) =>
JSON.stringify([row.series_id, row.event_time, row.source_sequence]);
const physicalQuote = (instrumentId: string, observedAt: number, sourceSequence: number) =>
@ -231,7 +223,7 @@ export function validatePaperFrame(
invalid('Paper quote identity, time, price or size is invalid');
}
}
frame.quotes.sort((a, b) => keyCompare(quoteKey(frame, a), quoteKey(frame, b)));
frame.quotes.sort((a, b) => compareEventKeys(quoteKey(frame, a), quoteKey(frame, b)));
for (let index = 1; index < frame.quotes.length; index++) {
const prior = frame.quotes[index - 1];
const current = frame.quotes[index];
@ -350,7 +342,7 @@ export function planPaperFrame(
const add = (command: CanonicalMatchingCommand) => {
if (
command.key.at <= frame.availableAt &&
(!checkpoint.cursor || keyCompare(command.key, checkpoint.cursor) > 0)
(!checkpoint.cursor || compareEventKeys(command.key, checkpoint.cursor) > 0)
) {
commands.push(command);
}
@ -409,7 +401,7 @@ export function planPaperFrame(
});
}
const active = new Map(checkpoint.sessions);
commands.sort((a, b) => keyCompare(a.key, b.key));
commands.sort((a, b) => compareEventKeys(a.key, b.key));
for (const command of commands) {
if (command.body.kind === 'openSession') {
if (active.has(command.key.instrumentId)) {
@ -424,12 +416,12 @@ export function planPaperFrame(
}
}
const quotes = [...frame.quotes].sort((a, b) =>
keyCompare(quoteKey(frame, a), quoteKey(frame, b))
compareEventKeys(quoteKey(frame, a), quoteKey(frame, b))
);
for (const quote of quotes) {
const key = quoteKey(frame, quote);
if (
(checkpoint.cursor && keyCompare(key, checkpoint.cursor) <= 0) ||
(checkpoint.cursor && compareEventKeys(key, checkpoint.cursor) <= 0) ||
!isDeepStrictEqual(
active.get(quote.instrumentId),
activeSession(config, quote.instrumentId, frame.availableAt)