Implement the following plan: Plan: Expose Trading Data to Python Agents Context Python…
- User
Implement the following plan:
Plan: Expose Trading Data to Python Agents
Context
Python agents currently operate blind. Despite all data model types being PyO3-exported, agents receive none of the trading or market data flowing through the system. The agent protocol only supports: user messages, risk limits text, a text-only order result (which is never even sent), token state, and shutdown. Agents cannot see their fills, order statuses, prices, or balances.
Since agents run as separate Python subprocesses (JSON-lines over stdin/stdout), data must be serialized to JSON — PyO3 types can't be used directly.
Changes
1. Add
Serializeto data model typesAll relevant structs only derive
Deserialize. AddSerializeso they can be serialized to JSON for the agent pipe.Files:
src/models/execution.rs—ExecutionData,Fee,Triggers,Contingentsrc/models/ticker.rs—TickerDatasrc/models/trade.rs—TradeDatasrc/models/balance.rs—BalanceData,WalletBalance
Each file: change
use serde::Deserializetouse serde::{Deserialize, Serialize}and addSerializeto the#[derive(...)].2. Expand the agent protocol (
src/agent.rs)Add new
AgentCommandvariants:ExecutionUpdate(Vec<ExecutionData>)— order status changes, fillsTickerUpdate(TickerData)— throttled price snapshotsTradeUpdate(Vec<TradeData>)— market tradesBalanceUpdate(Vec<BalanceData>)— balance changesOrderResponse { success, order_id, cl_ord_id, order_userref, error }— structured order placement result (replaces unusedOrderResult)
Add matching
TuiToAgentvariants (same structure, serialized to JSON).Update
spawn_stdin_writermatch arms to handle the new variants.Remove the old
OrderResultvariant fromAgentCommandandTuiToAgent(it is never sent anywhere in ). Assistant
I'll start by reading all the files that need to be modified in parallel to understand the current state.
Assistant
Now let me check a couple more things I need for the implementation.
Assistant
Let me check the
AddOrderResultandAddOrderParamsstructures, and see howcl_ord_idworks in the builder.Assistant
Good, I have a clear picture. Let me create tasks and start implementing.
Assistant
Now let me start with task 1 — adding
Serializeto all model types. These are all independent edits.Assistant
Now add
Serializeto the derive macros on each struct. Starting with execution.rs:Assistant
Now ticker.rs and trade.rs:
Assistant
Now balance.rs — four structs need Serialize:
Assistant
Now task 2 — expand the agent protocol. I need to rewrite
agent.rswith the new variants:Assistant
Now I'll replace the
AgentCommandenum,AgentToTuienum,TuiToAgentenum, and updatespawn_stdin_writer:Assistant
Now update
AgentToTuito addcl_ord_idtoPlaceOrder:Assistant
Now update
TuiToAgent— replace oldOrderResultwith new variants:Assistant
Now update
spawn_stdout_readerto threadcl_ord_idthroughPlaceOrder:Assistant
Now update the
spawn_stdin_writermatch arms:Assistant
Task 3 — add
cl_ord_idto theAgentOrderRequestmessage variant and thread it through the order builder inevent.rs:Assistant
Now update the handler for
AgentOrderRequestto includecl_ord_id:Assistant
Task 4 — forward data streams in
main.rs. I need to add intercepts beforetui::event::update()for Execution, Balance, OrderPlaced, Ticker (throttled), and Trade messages.Assistant
I need to add
ClonetoAgentCommandso we can send to multiple agents. Let me update that first, then do the main.rs intercepts.Assistant
Now update
main.rs— add imports and the ticker throttle map, then add the data forwarding intercepts:Assistant
Now add the ticker throttle state before the main loop:
Assistant
Now add the data forwarding intercepts after the AgentReady intercept and before the
tui::event::update()call:Assistant
Task 5 — update the Python agent base class with new callbacks,
place_order(), and updated dispatch loop:Assistant
Task 6 — verification. Let me run cargo check first, then test and clippy.
Assistant
Compiles clean. Now run tests and clippy in parallel:
Assistant
All 150 tests pass, clippy clean, no warnings.
Assistant
Let me also run
cargo fmt --checkto ensure formatting is correct:Assistant
All clean. Here's a summary of what was implemented:
Changes Made
1. Model types — added
Serialize(4 files)src/models/execution.rs—ExecutionData,Fee,Triggers,Contingentsrc/models/ticker.rs—TickerDatasrc/models/trade.rs—TradeDatasrc/models/balance.rs—BalanceData,WalletBalance
2. Agent protocol —
src/agent.rs- Replaced
OrderResultvariant with structuredOrderResponse(success, order_id, cl_ord_id, order_userref, error) - Added , , , variants to both and