Tesser RFC-005: Unified State Reconciliation Engine #89
Closed
EwigMidori
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Tesser RFC-005: Unified State Reconciliation Engine
tesser-portfolio(Portfolio State), Exchange Client Interface1. Motivation
1.1 The State Drift Problem
A trading system's local state (positions, balances, open orders) can easily diverge from the exchange's state due to process crashes, network partitions, or exchange-side events (e.g., funding payments, order expirations). Currently, Tesser has a basic
spawn_reconciliation_loopfor runtime checks and separate, yet-to-be-defined logic for startup recovery.1.2 The Risk of Dual Systems
Maintaining two separate reconciliation systems—one for startup and one for runtime—is a significant architectural risk. It violates the DRY (Don't Repeat Yourself) principle and inevitably leads to subtle behavioral inconsistencies. For example, the definition of a "zombie" order (an order existing on the exchange but not tracked locally) might differ between the two systems, leading to unpredictable behavior and making the system difficult to maintain and reason about.
This RFC proposes a unified architecture to solve state drift reliably and maintainably.
2. Proposed Architecture
We will introduce a Unified Reconciliation Engine built on a layered design. The core idea is to separate the detection of state differences from the resolution of those differences.
2.1 Layer 1: The Core
StateDifferThis will be a pure, stateless component responsible for one task: comparing a snapshot of the local
LiveStatewith a snapshot of the remote exchange state.ReconciliationReport. This report does not perform any actions; it simply categorizes all discrepancies found.The
ReconciliationReportwill contain categorized lists, such as:Matched Orders: Exist in both states (but may have different filled quantities).Ghost Orders: Exist locally but are missing on the exchange (implying they were filled or canceled during a disconnect).Zombie Orders: Exist on the exchange but are not tracked locally.This component becomes the single source of truth for identifying state drift.
2.2 Layer 2: Contextual
HandlersThese components consume the
ReconciliationReportand execute state-modifying actions. The key is that we will have different handlers for different operational contexts.A. The
StartupHandlerB. The
RuntimeHandlerspawn_reconciliation_loop).liquidate_onlymode, but it must not perform a hard reset.Fillevents, and dispatch them to the portfolio to ensure accounting remains accurate.3. Implementation Plan
3.1 Phase 1: Core Differ & Runtime Refactor
StateDifferand theReconciliationReportdata structures.spawn_reconciliation_loopto use the newStateDifferand evolve its logic into theRuntimeHandler.RuntimeHandlerto fetch historical fills forGhost Ordersand translate them into internal events.3.2 Phase 2: Startup Handler & Integration
StartupHandlerwith its "hard reset" logic.Portfolio::from_exchange_stateto allow the preservation of key metrics (likepeak_equityfor drawdown calculations) during a reset.StartupHandlerinto the engine's boot sequence, ensuring it runs after loading from disk but before the strategy logic begins.3.3 Phase 3: Configuration & Policies
Zombie Orders(Cancel,Adopt, orIgnore).4. Future Benefits
StateDifferto change how discrepancies are detected, making the system easier to upgrade and debug.Beta Was this translation helpful? Give feedback.
All reactions