Refactor execution boundaries and add durable strategy recovery
This commit is contained in:
parent
d6b09eb635
commit
694c48bbdb
36 changed files with 3077 additions and 491 deletions
|
|
@ -5,6 +5,13 @@ import {
|
|||
type ExecutionRepository,
|
||||
type ExecutionTransaction,
|
||||
} from './repository';
|
||||
import {
|
||||
canonicalJson,
|
||||
type MarketInput,
|
||||
type StrategyCheckpoint,
|
||||
type StrategyJournalPayload,
|
||||
type StrategyOrderIntent,
|
||||
} from './strategy-recovery';
|
||||
|
||||
export class PostgresExecutionRepository implements ExecutionRepository {
|
||||
constructor(private readonly postgres: Pick<PostgresClient, 'transaction'>) {}
|
||||
|
|
@ -102,6 +109,96 @@ export class PostgresExecutionRepository implements ExecutionRepository {
|
|||
}
|
||||
return false;
|
||||
},
|
||||
async appendJournal(payload: StrategyJournalPayload) {
|
||||
const account = state.rows[0].state as Account;
|
||||
const sequence = (account.strategySequence ?? 0) + 1;
|
||||
if (!Number.isSafeInteger(sequence)) {
|
||||
throw new Error('Strategy journal sequence exhausted');
|
||||
}
|
||||
account.strategySequence = sequence;
|
||||
await client.query(
|
||||
'INSERT INTO simulation_strategy_journal (account_id,sequence,payload) VALUES ($1,$2,$3::jsonb)',
|
||||
[id, sequence, JSON.stringify(payload)]
|
||||
);
|
||||
},
|
||||
async nextJournal(cursor) {
|
||||
const result = await client.query(
|
||||
'SELECT sequence,payload FROM simulation_strategy_journal WHERE account_id = $1 AND sequence > $2 ORDER BY sequence LIMIT 1',
|
||||
[id, cursor]
|
||||
);
|
||||
const row = result.rows[0];
|
||||
if (!row) {
|
||||
return undefined;
|
||||
}
|
||||
const sequence = Number(row.sequence);
|
||||
if (!Number.isSafeInteger(sequence)) {
|
||||
throw new Error('Invalid strategy journal sequence');
|
||||
}
|
||||
return { sequence, payload: row.payload as StrategyJournalPayload };
|
||||
},
|
||||
async strategies() {
|
||||
const result = await client.query(
|
||||
'SELECT payload FROM simulation_strategy_checkpoints WHERE account_id = $1 ORDER BY strategy_id COLLATE "C"',
|
||||
[id]
|
||||
);
|
||||
return result.rows.map(row => row.payload as StrategyCheckpoint);
|
||||
},
|
||||
async putStrategy(checkpoint) {
|
||||
await client.query(
|
||||
`INSERT INTO simulation_strategy_checkpoints (account_id,strategy_id,payload) VALUES ($1,$2,$3::jsonb)
|
||||
ON CONFLICT (account_id,strategy_id) DO UPDATE SET payload = EXCLUDED.payload`,
|
||||
[id, checkpoint.strategyId, JSON.stringify(checkpoint)]
|
||||
);
|
||||
},
|
||||
async nextIntent() {
|
||||
const result = await client.query(
|
||||
`SELECT payload FROM simulation_strategy_intents WHERE account_id = $1 AND status = 'pending' ORDER BY sequence,strategy_id COLLATE "C",client_order_id COLLATE "C" LIMIT 1`,
|
||||
[id]
|
||||
);
|
||||
return result.rows[0]?.payload as StrategyOrderIntent | undefined;
|
||||
},
|
||||
async putIntent(intent) {
|
||||
const previous = await client.query(
|
||||
'SELECT payload FROM simulation_strategy_intents WHERE account_id = $1 AND client_order_id = $2',
|
||||
[id, intent.clientOrderId]
|
||||
);
|
||||
const old = previous.rows[0]?.payload as StrategyOrderIntent | undefined;
|
||||
if (
|
||||
old &&
|
||||
(old.strategyId !== intent.strategyId ||
|
||||
old.sequence !== intent.sequence ||
|
||||
canonicalJson(old.request) !== canonicalJson(intent.request) ||
|
||||
(old.status === 'dispatched' && intent.status !== 'dispatched'))
|
||||
) {
|
||||
throw new ExecutionConflictError('Conflicting strategy order intent');
|
||||
}
|
||||
await client.query(
|
||||
`INSERT INTO simulation_strategy_intents (account_id,client_order_id,strategy_id,sequence,status,payload) VALUES ($1,$2,$3,$4,$5,$6::jsonb)
|
||||
ON CONFLICT (account_id,client_order_id) DO UPDATE SET status = EXCLUDED.status,payload = EXCLUDED.payload`,
|
||||
[
|
||||
id,
|
||||
intent.clientOrderId,
|
||||
intent.strategyId,
|
||||
intent.sequence,
|
||||
intent.status,
|
||||
JSON.stringify(intent),
|
||||
]
|
||||
);
|
||||
},
|
||||
async nextInput() {
|
||||
const result = await client.query(
|
||||
`SELECT payload FROM simulation_market_inputs WHERE account_id = $1 AND status = 'pending' ORDER BY sequence LIMIT 1`,
|
||||
[id]
|
||||
);
|
||||
return result.rows[0]?.payload as MarketInput | undefined;
|
||||
},
|
||||
async putInput(input) {
|
||||
await client.query(
|
||||
`INSERT INTO simulation_market_inputs (account_id,id,sequence,status,payload) VALUES ($1,$2,$3,$4,$5::jsonb)
|
||||
ON CONFLICT (account_id,id) DO UPDATE SET status = EXCLUDED.status,payload = EXCLUDED.payload`,
|
||||
[id, input.id, input.sequence, input.status, JSON.stringify(input)]
|
||||
);
|
||||
},
|
||||
});
|
||||
await client.query(
|
||||
'UPDATE simulation_accounts SET state = $2::jsonb,updated_at = NOW() WHERE id = $1',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue