ibkr-agent-gateway 0.5.2

Unofficial local-first CLI and MCP gateway for Interactive Brokers workflows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Live order commands.

use crate::cli::{commands::account::parse_account_id, output::print_output};
use crate::internal::approval::ApprovalId;
use crate::internal::audit::{
    OrderIdempotencyOperation, OrderIdempotencyRecoveryContext, OrderIdempotencyWorkflow,
    SqliteAuditWriter,
};
use crate::internal::backend::IbkrBackend;
use crate::internal::config::LiveTradingConfig;
use crate::internal::domain::{
    AssetClass, BrokerOrderId, CurrencyCode, ErrorCode, GatewayError, LocalUserId, MarketSnapshot,
    Money, Quantity, ValidatedOrder,
};
use crate::internal::orders::{
    IdempotencyKey, IdempotencyStore, KillSwitch, LiveCancelRequest, LiveOrderWriter,
    LiveSubmitRequest, PaperToLiveMigrationChecklist, cancel_live_order,
    handle_pending_order_error, stable_request_hash, submit_live_order,
};
use crate::internal::risk::{
    LiveFrequencyLimit, LiveLimitContext, LiveLimitPolicy, LiveSessionLimit, StaticPolicyRegistry,
    apply_live_rate_counters, live_limit_context_for_order,
};
use rust_decimal::Decimal;
use serde::Serialize;

const LIVE_SUBMIT_HUMAN_OUTPUT: &str = "live order candidate recorded";
const LIVE_CANCEL_HUMAN_OUTPUT: &str = "live cancel candidate recorded";

/// Runs a live submit command.
pub async fn submit(
    runtime: LiveOrderCommandRuntime<'_>,
    account: &str,
    approval_id: &str,
    idempotency_key: &str,
    gates: LiveCommandGates,
    json: bool,
) -> Result<(), GatewayError> {
    let audit_writer = runtime.audit_writer;
    let account_id = parse_account_id(account)?;
    let approval_id = ApprovalId::parse(approval_id)?;
    let approval = audit_writer
        .load_approval(&approval_id)
        .await?
        .ok_or_else(missing_approval)?;
    let preview_record = audit_writer
        .load_order_preview(&approval.preview_id)
        .await?
        .ok_or_else(missing_preview)?;
    let idempotency_key = IdempotencyKey::new(idempotency_key)?;
    let request_hash = stable_request_hash(
        "cli.live.submit",
        &LiveSubmitCliFingerprint {
            account,
            approval_id: approval_id.as_uuid().to_string(),
            gates,
        },
    )?;
    if let Some(payload) = audit_writer
        .replay_order_idempotency(&idempotency_key, &request_hash)
        .await?
    {
        return print_output(json, "live order candidate replayed", &payload);
    }

    let market_snapshot = runtime
        .backend
        .market_snapshot(&preview_record.validated_order.contract_id)
        .await?;
    let live_config = live_config_for_invocation(runtime.live_config, gates.enable_live);
    let live_policy = live_limit_policy(&live_config)?;
    let mut live_limit_context =
        live_limit_context(&preview_record.validated_order, market_snapshot)?;
    apply_live_rate_counters(
        audit_writer,
        &account_id,
        &live_policy,
        &mut live_limit_context,
    )
    .await?;
    let request = LiveSubmitRequest {
        order: preview_record.validated_order,
        approval,
        idempotency_key: idempotency_key.clone(),
        live_config,
        live_scope_granted: gates.live_scope,
        live_limit_context,
        kill_switch: kill_switch(gates.open_kill_switch),
        audit_available: true,
        migration_checklist: migration_checklist(gates.acknowledge_migration),
    };
    let mut idempotency_store = IdempotencyStore::default();
    let policy_registry = StaticPolicyRegistry::single(live_policy);
    let recovery_context = OrderIdempotencyRecoveryContext {
        workflow: OrderIdempotencyWorkflow::Live,
        operation: OrderIdempotencyOperation::Submit,
        account_id: request.order.account_id.clone(),
        broker_order_id: None,
    };
    audit_writer
        .insert_order_pending_with_context(&idempotency_key, &request_hash, Some(&recovery_context))
        .await?;
    let result = match submit_live_order(
        request,
        runtime.writer,
        &policy_registry,
        &mut idempotency_store,
    )
    .await
    {
        Ok(result) => result,
        Err(error) => {
            handle_pending_order_error(audit_writer, &idempotency_key, &request_hash, &error)
                .await?;
            return Err(error);
        }
    };
    let payload = serde_json::to_value(&result.lifecycle).map_err(|_| output_payload_error())?;
    audit_writer
        .complete_live_order_workflow(
            &idempotency_key,
            &request_hash,
            &payload,
            &result.lifecycle,
            std::slice::from_ref(&result.consumed_approval),
        )
        .await?;
    print_output(json, LIVE_SUBMIT_HUMAN_OUTPUT, &result.lifecycle)
}

/// Runs a live cancel command.
pub async fn cancel(
    runtime: LiveOrderCommandRuntime<'_>,
    account: &str,
    broker_order_id: &str,
    idempotency_key: &str,
    gates: LiveCommandGates,
    json: bool,
) -> Result<(), GatewayError> {
    let audit_writer = runtime.audit_writer;
    let account_id = parse_account_id(account)?;
    let Some(broker_order_id) = BrokerOrderId::new(broker_order_id) else {
        return Err(GatewayError::new(
            ErrorCode::OrderValidationFailed,
            "Broker order id is required",
            false,
            Some("Provide a broker order id".to_string()),
        ));
    };
    let idempotency_key = IdempotencyKey::new(idempotency_key)?;
    let request_hash = stable_request_hash(
        "cli.live.cancel",
        &LiveCancelCliFingerprint {
            account,
            broker_order_id: broker_order_id.as_str(),
            gates,
        },
    )?;
    if let Some(payload) = audit_writer
        .replay_order_idempotency(&idempotency_key, &request_hash)
        .await?
    {
        return print_output(json, "live cancel candidate replayed", &payload);
    }

    let request = LiveCancelRequest {
        account_id: account_id.clone(),
        broker_order_id,
        idempotency_key: idempotency_key.clone(),
        live_config: live_config_for_invocation(runtime.live_config, gates.enable_live),
        live_scope_granted: gates.live_scope,
        kill_switch: kill_switch(gates.open_kill_switch),
        audit_available: true,
        migration_checklist: migration_checklist(gates.acknowledge_migration),
    };
    let mut idempotency_store = IdempotencyStore::default();
    let recovery_context = OrderIdempotencyRecoveryContext {
        workflow: OrderIdempotencyWorkflow::Live,
        operation: OrderIdempotencyOperation::Cancel,
        account_id: request.account_id.clone(),
        broker_order_id: Some(request.broker_order_id.clone()),
    };
    audit_writer
        .insert_order_pending_with_context(&idempotency_key, &request_hash, Some(&recovery_context))
        .await?;
    let result = match cancel_live_order(request, runtime.writer, &mut idempotency_store).await {
        Ok(result) => result,
        Err(error) => {
            handle_pending_order_error(audit_writer, &idempotency_key, &request_hash, &error)
                .await?;
            return Err(error);
        }
    };
    let payload = serde_json::to_value(&result.lifecycle).map_err(|_| output_payload_error())?;
    audit_writer
        .complete_live_order_workflow(
            &idempotency_key,
            &request_hash,
            &payload,
            &result.lifecycle,
            &[],
        )
        .await?;
    print_output(json, LIVE_CANCEL_HUMAN_OUTPUT, &result.lifecycle)
}

/// Live CLI gate flags.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
pub struct LiveCommandGates {
    /// Explicit live enablement.
    pub enable_live: bool,
    /// Simulated live scope.
    pub live_scope: bool,
    /// Simulated kill switch state.
    pub open_kill_switch: bool,
    /// Paper-to-live checklist acknowledgement.
    pub acknowledge_migration: bool,
}

/// Runtime dependencies for live order CLI commands.
#[derive(Clone, Copy)]
pub struct LiveOrderCommandRuntime<'a> {
    /// Durable audit and workflow state.
    pub audit_writer: &'a SqliteAuditWriter,
    /// Broker read backend.
    pub backend: &'a dyn IbkrBackend,
    /// Selected live writer.
    pub writer: &'a dyn LiveOrderWriter,
    /// Validated live trading configuration.
    pub live_config: &'a LiveTradingConfig,
}

fn live_config_for_invocation(
    config: &LiveTradingConfig,
    invocation_enabled: bool,
) -> LiveTradingConfig {
    let mut config = config.clone();
    config.enabled = config.enabled && invocation_enabled;
    config
}

pub(crate) fn kill_switch(open: bool) -> KillSwitch {
    if open {
        KillSwitch::open(LocalUserId::from_static("local-user"), "cli live smoke")
    } else {
        KillSwitch::closed(LocalUserId::from_static("local-user"), "closed by default")
    }
}

pub(crate) fn migration_checklist(acknowledged: bool) -> PaperToLiveMigrationChecklist {
    if acknowledged {
        PaperToLiveMigrationChecklist::acknowledged(LocalUserId::from_static("local-user"))
    } else {
        PaperToLiveMigrationChecklist {
            paper_trading_validated: false,
            approvals_reviewed: false,
            limits_reviewed: false,
            kill_switch_tested: false,
            incident_runbook_reviewed: false,
            acknowledged_by: None,
            acknowledged_at: None,
        }
    }
}

pub(crate) fn live_limit_policy(
    config: &LiveTradingConfig,
) -> Result<LiveLimitPolicy, GatewayError> {
    let Some(currency) = CurrencyCode::new("USD") else {
        return Err(GatewayError::new(
            ErrorCode::OrderValidationFailed,
            "Static currency is invalid",
            false,
            None,
        ));
    };
    let policy_id = config
        .risk_policy_id
        .as_deref()
        .unwrap_or("cli-live-policy-unconfigured");

    Ok(LiveLimitPolicy {
        policy_id: policy_id.to_string(),
        enabled: true,
        max_notional: Some(Money {
            amount: Decimal::new(1_000, 0),
            currency: currency.clone(),
        }),
        max_quantity: Some(Quantity::new(Decimal::new(10, 0))),
        allowed_symbols: vec!["AAPL".to_string()],
        allowed_asset_classes: vec![AssetClass::Stock],
        frequency_limit: Some(LiveFrequencyLimit {
            max_orders: 5,
            window_seconds: 300,
        }),
        session_limit: Some(LiveSessionLimit {
            max_orders_per_session: 20,
            max_session_notional: Some(Money {
                amount: Decimal::new(5_000, 0),
                currency,
            }),
        }),
        max_price_deviation_bps: Some(500),
        max_quote_age_seconds: Some(30),
    })
}

pub(crate) fn live_limit_context(
    order: &ValidatedOrder,
    market_snapshot: MarketSnapshot,
) -> Result<LiveLimitContext, GatewayError> {
    live_limit_context_for_order(order, Some(market_snapshot))
}

#[cfg(test)]
mod tests {
    use super::{
        LIVE_CANCEL_HUMAN_OUTPUT, LIVE_SUBMIT_HUMAN_OUTPUT, live_config_for_invocation,
        live_limit_policy,
    };
    use crate::internal::config::LiveTradingConfig;
    use crate::internal::domain::AccountId;

    #[test]
    fn live_human_outputs_describe_local_candidates_not_broker_execution() {
        assert_eq!(LIVE_SUBMIT_HUMAN_OUTPUT, "live order candidate recorded");
        assert_eq!(LIVE_CANCEL_HUMAN_OUTPUT, "live cancel candidate recorded");
        assert!(!LIVE_SUBMIT_HUMAN_OUTPUT.contains("submitted"));
        assert!(!LIVE_CANCEL_HUMAN_OUTPUT.contains("cancelled"));
    }

    #[test]
    fn live_cli_uses_runtime_allowlist_instead_of_target_account() {
        let config = LiveTradingConfig {
            enabled: true,
            allowed_accounts: vec![AccountId::from_static("U1111111")],
            risk_policy_id: Some("configured-policy".to_string()),
            paper_to_live_checklist_acknowledged: true,
            reconciler_interval_seconds: 5,
        };

        let effective = live_config_for_invocation(&config, true);

        assert_eq!(effective.allowed_accounts, config.allowed_accounts);
        assert!(
            !effective
                .allowed_accounts
                .contains(&AccountId::from_static("U2222222"))
        );
    }

    #[test]
    fn live_cli_policy_id_comes_from_runtime_config()
    -> Result<(), crate::internal::domain::GatewayError> {
        let config = LiveTradingConfig {
            enabled: true,
            allowed_accounts: vec![AccountId::from_static("U1111111")],
            risk_policy_id: Some("configured-policy".to_string()),
            paper_to_live_checklist_acknowledged: true,
            reconciler_interval_seconds: 5,
        };

        let policy = live_limit_policy(&config)?;

        assert_eq!(policy.policy_id, "configured-policy");
        Ok(())
    }
}

#[derive(Serialize)]
struct LiveSubmitCliFingerprint<'a> {
    account: &'a str,
    approval_id: String,
    gates: LiveCommandGates,
}

#[derive(Serialize)]
struct LiveCancelCliFingerprint<'a> {
    account: &'a str,
    broker_order_id: &'a str,
    gates: LiveCommandGates,
}

fn missing_approval() -> GatewayError {
    GatewayError::new(
        ErrorCode::PaperApprovalRequired,
        "Live submit requires an existing approval record",
        false,
        Some("Run approvals create and pass its approval_id".to_string()),
    )
}

fn missing_preview() -> GatewayError {
    GatewayError::new(
        ErrorCode::PaperApprovalRequired,
        "Live submit requires the approved preview to be present",
        false,
        Some("Create a fresh preview and approval before live submit".to_string()),
    )
}

fn output_payload_error() -> GatewayError {
    GatewayError::new(
        ErrorCode::AuditWriteFailed,
        "Unable to serialize live lifecycle for idempotency",
        true,
        Some("Retry the live workflow".to_string()),
    )
}