qx-execution 0.4.2

牵星统一执行事实归约边界
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Bounded, restartable submission of a deterministic TWAP/VWAP child plan.
//!
//! The scheduler calls `step` with its current clock. Each call submits or
//! cancels at most one child through the ordinary risk/OMS/venue gateway. The
//! caller persists the returned state after every step; replaying an older
//! checkpoint uses the same child client id and the OMS event log remains the
//! authority for whether that child already left the process.

use crate::{ExecutionEventPort, PortExecutionService, RiskPort, VenuePort};
mod validation;
use qx_core::{Order, OrderStatus};
use qx_zhenlu::AlgoChildSlice;
use serde::{Deserialize, Serialize};
use validation::{same_child_identity, validate_checkpoint, validate_input};

pub const ALGO_EXECUTION_STATE_SCHEMA_VERSION: u32 = 1;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlgoExecutionStatus {
    Ready,
    Running,
    CancelRequested,
    Completed,
    Cancelled,
    Failed,
    ReconcileRequired,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AlgoExecutionStep {
    NotDue {
        next_scheduled_ts: u64,
    },
    Submitted {
        client_order_id: u64,
        status: OrderStatus,
    },
    WaitingForChildren {
        open_orders: usize,
        filled_raw: i128,
    },
    CancellationSubmitted {
        client_order_id: u64,
    },
    Completed {
        filled_raw: i128,
        unfilled_raw: i128,
    },
    Cancelled {
        filled_raw: i128,
        unfilled_raw: i128,
    },
    Failed {
        reason: String,
    },
    ReconcileRequired {
        reason: String,
    },
}

/// Serializable parent-order execution checkpoint. Persist this value with a
/// compare-and-swap or append-only store owned by the runtime; it contains no
/// venue client, credentials, thread, or non-deterministic clock state.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AlgoExecutionState {
    pub schema_version: u32,
    pub parent_id: String,
    pub parent_order: Order,
    pub plan: Vec<AlgoChildSlice>,
    pub child_order_id_base: u64,
    pub next_slice_position: usize,
    pub child_order_ids: Vec<u64>,
    /// Last scheduler timestamp accepted by `step`. Optional for compatibility
    /// with checkpoints written before monotonic clock validation was added.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_step_ts: Option<u64>,
    pub status: AlgoExecutionStatus,
    pub cancel_requested: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub failure: Option<String>,
}

/// Construction input kept separate from the persisted checkpoint so callers
/// cannot accidentally resume a partially advanced state by rebuilding it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AlgoParentOrder {
    pub parent_id: String,
    pub parent_order: Order,
    pub plan: Vec<AlgoChildSlice>,
    pub child_order_id_base: u64,
}

impl AlgoExecutionState {
    pub fn new(input: AlgoParentOrder) -> Result<Self, String> {
        validate_input(&input)?;
        let state = Self {
            schema_version: ALGO_EXECUTION_STATE_SCHEMA_VERSION,
            parent_id: input.parent_id,
            parent_order: input.parent_order,
            plan: input.plan,
            child_order_id_base: input.child_order_id_base,
            next_slice_position: 0,
            child_order_ids: Vec::new(),
            last_step_ts: None,
            status: AlgoExecutionStatus::Ready,
            cancel_requested: false,
            failure: None,
        };
        validate_checkpoint(&state)?;
        Ok(state)
    }

    pub fn request_cancel(&mut self) -> Result<(), String> {
        validate_checkpoint(self)?;
        match self.status {
            AlgoExecutionStatus::Completed
            | AlgoExecutionStatus::Cancelled
            | AlgoExecutionStatus::Failed
            | AlgoExecutionStatus::ReconcileRequired => {
                return Err(format!("算法父单终态不可取消: {:?}", self.status));
            }
            _ => {}
        }
        self.cancel_requested = true;
        self.status = AlgoExecutionStatus::CancelRequested;
        Ok(())
    }

    pub fn to_json(&self) -> Result<String, String> {
        validate_checkpoint(self)?;
        serde_json::to_string(self).map_err(|error| format!("算法执行状态序列化失败: {error}"))
    }

    pub fn from_json(payload: &str) -> Result<Self, String> {
        let state: Self = serde_json::from_str(payload)
            .map_err(|error| format!("算法执行状态 JSON 无效: {error}"))?;
        validate_checkpoint(&state)?;
        Ok(state)
    }

    /// Advance no more than one scheduled child or one cancellation per call.
    /// This gives an external scheduler a strict work bound and a persistence
    /// boundary after every venue side effect.
    pub fn step<V, P, R>(
        &mut self,
        now: u64,
        gateway: &mut PortExecutionService<'_, V, P>,
        risk: &R,
    ) -> Result<AlgoExecutionStep, String>
    where
        V: VenuePort,
        P: ExecutionEventPort,
        R: RiskPort,
    {
        validate_checkpoint(self)?;
        if gateway.now != now {
            return Err(format!(
                "算法调度时间与执行网关时间不一致: scheduler={now} gateway={}",
                gateway.now
            ));
        }
        if let Some(last) = self.last_step_ts {
            if now < last {
                return Err(format!(
                    "算法执行时钟不得回退: previous={last} current={now}"
                ));
            }
        }
        self.last_step_ts = Some(now);
        if let Some(result) = self.terminal_step(gateway)? {
            return Ok(result);
        }
        if self.cancel_requested {
            return self.cancel_one(gateway);
        }
        if self.next_slice_position < self.plan.len() {
            let slice = &self.plan[self.next_slice_position];
            if now < slice.scheduled_ts {
                return Ok(AlgoExecutionStep::NotDue {
                    next_scheduled_ts: slice.scheduled_ts,
                });
            }
            return self.submit_one(gateway, risk);
        }
        self.finish_or_wait(gateway)
    }

    fn submit_one<V, P, R>(
        &mut self,
        gateway: &mut PortExecutionService<'_, V, P>,
        risk: &R,
    ) -> Result<AlgoExecutionStep, String>
    where
        V: VenuePort,
        P: ExecutionEventPort,
        R: RiskPort,
    {
        let position = self.next_slice_position;
        let slice = self.plan[position].clone();
        let client_order_id = self.child_client_id(position)?;
        let child_order = self.child_order(&slice, client_order_id);

        if let Some(existing) = gateway.order(client_order_id) {
            if !same_child_identity(&existing, &child_order) {
                return self.require_reconcile(format!(
                    "child client id 已被不同订单占用: parent={} child_index={} client_order_id={client_order_id}",
                    self.parent_id, slice.child_index
                ));
            }
            match existing.status {
                OrderStatus::PendingSubmit => {
                    gateway.submit_with_risk_and_correlation(
                        child_order,
                        risk,
                        self.child_correlation(&slice),
                    )?;
                }
                OrderStatus::Unknown | OrderStatus::Submitted | OrderStatus::CancelPending => {
                    return self.require_reconcile(format!(
                        "恢复时子单状态 {:?} 需要对账,禁止重发: client_order_id={client_order_id}",
                        existing.status
                    ));
                }
                _ => {
                    self.child_order_ids.push(client_order_id);
                    self.next_slice_position += 1;
                    self.status = AlgoExecutionStatus::Running;
                    return Ok(AlgoExecutionStep::Submitted {
                        client_order_id,
                        status: existing.status,
                    });
                }
            }
        } else if let Err(error) = gateway.submit_with_risk_and_correlation(
            child_order,
            risk,
            self.child_correlation(&slice),
        ) {
            if gateway
                .order(client_order_id)
                .is_some_and(|order| order.status == OrderStatus::Unknown)
            {
                return self.require_reconcile(format!(
                    "子单提交结果未知,必须先对账: client_order_id={client_order_id}: {error}"
                ));
            }
            self.status = AlgoExecutionStatus::Failed;
            self.failure = Some(error.clone());
            return Ok(AlgoExecutionStep::Failed { reason: error });
        }

        let Some(order) = gateway.order(client_order_id) else {
            return self.require_reconcile(format!(
                "Venue 已返回但 OMS 查不到子单: client_order_id={client_order_id}"
            ));
        };
        if order.status == OrderStatus::Unknown {
            return self.require_reconcile(format!(
                "子单提交后进入待对账: client_order_id={client_order_id}"
            ));
        }
        self.child_order_ids.push(client_order_id);
        self.next_slice_position += 1;
        self.status = AlgoExecutionStatus::Running;
        Ok(AlgoExecutionStep::Submitted {
            client_order_id,
            status: order.status,
        })
    }

    fn cancel_one<V, P>(
        &mut self,
        gateway: &mut PortExecutionService<'_, V, P>,
    ) -> Result<AlgoExecutionStep, String>
    where
        V: VenuePort,
        P: ExecutionEventPort,
    {
        for client_order_id in self.child_order_ids.iter().copied() {
            let Some(order) = gateway.order(client_order_id) else {
                return self.require_reconcile(format!(
                    "撤单前 OMS 查不到算法子单: client_order_id={client_order_id}"
                ));
            };
            match order.status {
                OrderStatus::Accepted | OrderStatus::Working | OrderStatus::PartiallyFilled => {
                    if let Err(error) = gateway.cancel(client_order_id) {
                        if gateway
                            .order(client_order_id)
                            .is_some_and(|order| order.status == OrderStatus::Unknown)
                        {
                            return self.require_reconcile(format!(
                                "子单撤销结果未知,必须先对账: client_order_id={client_order_id}: {error}"
                            ));
                        }
                        return Err(error);
                    }
                    return Ok(AlgoExecutionStep::CancellationSubmitted { client_order_id });
                }
                OrderStatus::Unknown | OrderStatus::Submitted | OrderStatus::CancelPending => {
                    return self.require_reconcile(format!(
                        "撤单遇到 {:?} 子单,需要先对账: client_order_id={client_order_id}",
                        order.status
                    ));
                }
                OrderStatus::PendingSubmit
                | OrderStatus::Filled
                | OrderStatus::Cancelled
                | OrderStatus::Rejected
                | OrderStatus::Expired => {}
            }
        }
        let (filled_raw, unfilled_raw) = self.fill_totals(gateway)?;
        self.status = AlgoExecutionStatus::Cancelled;
        Ok(AlgoExecutionStep::Cancelled {
            filled_raw,
            unfilled_raw,
        })
    }

    fn finish_or_wait<V, P>(
        &mut self,
        gateway: &PortExecutionService<'_, V, P>,
    ) -> Result<AlgoExecutionStep, String>
    where
        V: VenuePort,
        P: ExecutionEventPort,
    {
        let mut open_orders = 0;
        for client_order_id in &self.child_order_ids {
            let Some(order) = gateway.order(*client_order_id) else {
                return self.require_reconcile(format!(
                    "算法执行状态引用了缺失子单: client_order_id={client_order_id}"
                ));
            };
            match order.status {
                OrderStatus::Unknown | OrderStatus::Submitted | OrderStatus::CancelPending => {
                    return self.require_reconcile(format!(
                        "算法子单处于 {:?},必须先对账: client_order_id={client_order_id}",
                        order.status
                    ));
                }
                OrderStatus::Accepted | OrderStatus::Working | OrderStatus::PartiallyFilled => {
                    open_orders += 1;
                }
                OrderStatus::PendingSubmit
                | OrderStatus::Filled
                | OrderStatus::Cancelled
                | OrderStatus::Rejected
                | OrderStatus::Expired => {}
            }
        }
        let (filled_raw, unfilled_raw) = self.fill_totals(gateway)?;
        if open_orders > 0 {
            return Ok(AlgoExecutionStep::WaitingForChildren {
                open_orders,
                filled_raw,
            });
        }
        self.status = AlgoExecutionStatus::Completed;
        Ok(AlgoExecutionStep::Completed {
            filled_raw,
            unfilled_raw,
        })
    }

    fn terminal_step<V, P>(
        &mut self,
        gateway: &PortExecutionService<'_, V, P>,
    ) -> Result<Option<AlgoExecutionStep>, String>
    where
        V: VenuePort,
        P: ExecutionEventPort,
    {
        let result = match self.status {
            AlgoExecutionStatus::Completed => {
                let (filled_raw, unfilled_raw) = self.fill_totals(gateway)?;
                Some(AlgoExecutionStep::Completed {
                    filled_raw,
                    unfilled_raw,
                })
            }
            AlgoExecutionStatus::Cancelled => {
                let (filled_raw, unfilled_raw) = self.fill_totals(gateway)?;
                Some(AlgoExecutionStep::Cancelled {
                    filled_raw,
                    unfilled_raw,
                })
            }
            AlgoExecutionStatus::Failed => Some(AlgoExecutionStep::Failed {
                reason: self
                    .failure
                    .clone()
                    .unwrap_or_else(|| "算法执行失败".into()),
            }),
            AlgoExecutionStatus::ReconcileRequired => Some(AlgoExecutionStep::ReconcileRequired {
                reason: self.failure.clone().unwrap_or_else(|| "需要对账".into()),
            }),
            _ => None,
        };
        Ok(result)
    }

    fn fill_totals<V, P>(
        &self,
        gateway: &PortExecutionService<'_, V, P>,
    ) -> Result<(i128, i128), String>
    where
        V: VenuePort,
        P: ExecutionEventPort,
    {
        let filled_raw = self.child_order_ids.iter().try_fold(0_i128, |sum, id| {
            let order = gateway
                .order(*id)
                .ok_or_else(|| format!("算法子单缺失: client_order_id={id}"))?;
            sum.checked_add(order.filled.raw())
                .ok_or_else(|| "算法父单累计成交数量溢出".to_string())
        })?;
        if filled_raw > self.parent_order.qty.raw() {
            return Err(format!(
                "算法子单累计成交超过父单: parent={} filled={} qty={}",
                self.parent_id,
                filled_raw,
                self.parent_order.qty.raw()
            ));
        }
        Ok((filled_raw, self.parent_order.qty.raw() - filled_raw))
    }

    fn child_order(&self, slice: &AlgoChildSlice, client_order_id: u64) -> Order {
        let mut child = self.parent_order.clone();
        child.client_id = client_order_id;
        child.qty = qx_core::Quantity::from_raw(slice.quantity_raw);
        child.filled = qx_core::Quantity::ZERO;
        child.status = OrderStatus::PendingSubmit;
        child
    }

    fn child_client_id(&self, position: usize) -> Result<u64, String> {
        self.child_order_id_base
            .checked_add(position as u64)
            .ok_or_else(|| "算法子单 client id 溢出".into())
    }

    fn child_correlation(&self, slice: &AlgoChildSlice) -> String {
        format!("algo:{}:child:{}", self.parent_id, slice.child_index)
    }

    fn require_reconcile(&mut self, reason: String) -> Result<AlgoExecutionStep, String> {
        self.status = AlgoExecutionStatus::ReconcileRequired;
        self.failure = Some(reason.clone());
        Ok(AlgoExecutionStep::ReconcileRequired { reason })
    }
}

#[cfg(test)]
mod tests;