refactor(orchestrator): share pure strategy bookkeeping (CL19)
This commit is contained in:
parent
984dc1a9cc
commit
6055aeeee9
17 changed files with 297 additions and 157 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { AsyncLocalStorage } from 'node:async_hooks';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { stageFinancialTransaction } from '@stock-bot/engine';
|
||||
import { canonicalJson } from '../canonical-json';
|
||||
import {
|
||||
canonicalMatchingConfigSchema,
|
||||
financialTransactionRequestSchema,
|
||||
|
|
@ -15,7 +16,9 @@ import { nativeAdmissionOrders, type DecisionContext } from '../contracts/native
|
|||
import {
|
||||
freeze,
|
||||
increment,
|
||||
initialStrategyDiagnostic,
|
||||
metadataSchema,
|
||||
ownerAdmissionFeedback,
|
||||
stageStrategyDecision,
|
||||
stateCopy,
|
||||
type Metadata,
|
||||
|
|
@ -32,7 +35,6 @@ import {
|
|||
type CanonicalTransaction,
|
||||
} from './canonical-paper';
|
||||
import { ExecutionConflictError } from './repository';
|
||||
import { canonicalJson } from './strategy-recovery';
|
||||
|
||||
const callback = new AsyncLocalStorage<boolean>();
|
||||
function outsideCallback() {
|
||||
|
|
@ -85,16 +87,7 @@ export class CanonicalPaperExecution {
|
|||
version: 1,
|
||||
definition: { ...metadata, initialState },
|
||||
cursor: 0,
|
||||
diagnostic: {
|
||||
ownerId,
|
||||
strategyId,
|
||||
strategyVersion,
|
||||
nextDecisionSequence: 0,
|
||||
state: stateCopy(initialState),
|
||||
rng: { algorithm: 'lcg32-v1', seed, state: seed, draws: 0 },
|
||||
observationCount: 0,
|
||||
previousAdmission: null,
|
||||
},
|
||||
diagnostic: initialStrategyDiagnostic(metadata, initialState),
|
||||
pendingObservations: [],
|
||||
};
|
||||
})
|
||||
|
|
@ -327,18 +320,11 @@ export class CanonicalPaperExecution {
|
|||
if (!decision) {
|
||||
continue;
|
||||
}
|
||||
owner.diagnostic.previousAdmission = {
|
||||
decision: decision.decision,
|
||||
revision: result.outcome.revision,
|
||||
orders: orders
|
||||
.filter(order => order.order.decision.ownerId === owner.definition.ownerId)
|
||||
.map(order => ({
|
||||
orderId: order.order.orderId,
|
||||
instrumentId: order.order.instrumentId,
|
||||
decision: order.order.decision,
|
||||
status: order.status,
|
||||
})),
|
||||
};
|
||||
owner.diagnostic.previousAdmission = ownerAdmissionFeedback(
|
||||
decision.decision,
|
||||
result.outcome.revision,
|
||||
orders
|
||||
);
|
||||
await tx.putOwner(owner);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { canonicalJson } from '../canonical-json';
|
||||
import { increment } from '../strategies/strategy-decision';
|
||||
import {
|
||||
canonicalAccountSchema,
|
||||
|
|
@ -12,7 +13,6 @@ import {
|
|||
type CanonicalTransaction,
|
||||
} from './canonical-paper';
|
||||
import { ExecutionConflictError } from './repository';
|
||||
import { canonicalJson } from './strategy-recovery';
|
||||
|
||||
function accountState(raw: unknown): CanonicalAccount {
|
||||
const result = canonicalAccountSchema.safeParse(raw);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import {
|
|||
canonicalExecutionCheckpointSchema,
|
||||
financialTransactionResultSchema,
|
||||
matchingCommandSchema,
|
||||
type CanonicalMatchingCommand,
|
||||
} from '../contracts/canonical-execution';
|
||||
import {
|
||||
nativeFinancialViewSchema,
|
||||
|
|
@ -196,18 +195,7 @@ export interface CanonicalExecutionRepository {
|
|||
transact<T>(accountId: string, work: (tx: CanonicalTransaction) => Promise<T>): Promise<T>;
|
||||
}
|
||||
export type CanonicalPostgres = Pick<PostgresClient, 'transaction'>;
|
||||
export function compareEventKeys(
|
||||
a: CanonicalMatchingCommand['key'],
|
||||
b: CanonicalMatchingCommand['key']
|
||||
): number {
|
||||
const phases = { Observation: 0, Decision: 1, Execution: 2, SessionClose: 3 };
|
||||
return (
|
||||
a.at - b.at ||
|
||||
phases[a.phase] - phases[b.phase] ||
|
||||
Buffer.compare(Buffer.from(a.instrumentId), Buffer.from(b.instrumentId)) ||
|
||||
a.sourceSequence - b.sourceSequence
|
||||
);
|
||||
}
|
||||
export { compareEventKeys } from '../contracts/canonical-execution';
|
||||
/** Legacy callers must never reinterpret any version of a canonical envelope. */
|
||||
export function refuseCanonicalAccount(state: unknown): void {
|
||||
if (state && typeof state === 'object' && 'kind' in state) {
|
||||
|
|
|
|||
|
|
@ -1,30 +1,9 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import { canonicalJson } from '../canonical-json';
|
||||
import type { MarketData, OrderRequest, StrategyConfig } from '../types';
|
||||
import type { Account, ExecutionReport, Fill } from './model';
|
||||
|
||||
/** Only JSON values are checkpointable; reject lossy JSON.stringify conversions. */
|
||||
export function canonicalJson(value: unknown): string {
|
||||
const visit = (input: unknown): unknown => {
|
||||
if (input === null || typeof input === 'string' || typeof input === 'boolean') {
|
||||
return input;
|
||||
}
|
||||
if (typeof input === 'number' && Number.isFinite(input)) {
|
||||
return input;
|
||||
}
|
||||
if (Array.isArray(input)) {
|
||||
return Array.from(input, visit);
|
||||
}
|
||||
if (input && typeof input === 'object' && Object.getPrototypeOf(input) === Object.prototype) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(input)
|
||||
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
||||
.map(([key, item]) => [key, visit(item)])
|
||||
);
|
||||
}
|
||||
throw new Error('Strategy checkpoints require finite, plain JSON values');
|
||||
};
|
||||
return JSON.stringify(visit(value));
|
||||
}
|
||||
export { canonicalJson } from '../canonical-json';
|
||||
export function strategyConfigHash(config: StrategyConfig, version: string): string {
|
||||
const { enabled: _enabled, ...definition } = config;
|
||||
return createHash('sha256').update(canonicalJson({ definition, version })).digest('hex');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue