lxmf-sdk 0.7.0

High-level Rust SDK for LXMF clients and RPC-backed LXMF 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
410
411
412
413
414
415
416
417
418
419
420
use crate::error::{code, ErrorCategory, SdkError};
use crate::types::{RuntimeState, StartRequest};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SdkMethod {
    Start,
    Send,
    Cancel,
    Status,
    Configure,
    Tick,
    PollEvents,
    Snapshot,
    Shutdown,
    SubscribeEvents,
}

impl SdkMethod {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Start => "start",
            Self::Send => "send",
            Self::Cancel => "cancel",
            Self::Status => "status",
            Self::Configure => "configure",
            Self::Tick => "tick",
            Self::PollEvents => "poll_events",
            Self::Snapshot => "snapshot",
            Self::Shutdown => "shutdown",
            Self::SubscribeEvents => "subscribe_events",
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Lifecycle {
    state: RuntimeState,
    active_start_request: Option<StartRequest>,
}

impl Default for Lifecycle {
    fn default() -> Self {
        Self { state: RuntimeState::New, active_start_request: None }
    }
}

impl Lifecycle {
    pub fn state(&self) -> RuntimeState {
        self.state.clone()
    }

    pub fn ensure_method_legal(&self, method: SdkMethod) -> Result<(), SdkError> {
        if legal_states_for_method(method).contains(&self.state) {
            return Ok(());
        }
        Err(SdkError::invalid_state(method.as_str(), self.state_name()))
    }

    pub fn check_start_reentry(&self, req: &StartRequest) -> Result<bool, SdkError> {
        match self.state {
            RuntimeState::New => Ok(false),
            RuntimeState::Running => match &self.active_start_request {
                Some(active) if active == req => Ok(true),
                _ => Err(SdkError::new(
                    code::RUNTIME_ALREADY_RUNNING_WITH_DIFFERENT_CONFIG,
                    ErrorCategory::Runtime,
                    "runtime is already running with a different start request",
                )
                .with_user_actionable(true)
                .with_detail("method", JsonValue::String("start".to_owned()))),
            },
            _ => Err(SdkError::invalid_state("start", self.state_name())),
        }
    }

    pub fn mark_starting(&mut self) -> Result<(), SdkError> {
        if self.state != RuntimeState::New {
            return Err(SdkError::invalid_state("start", self.state_name()));
        }
        self.state = RuntimeState::Starting;
        Ok(())
    }

    pub fn mark_running(&mut self, req: StartRequest) -> Result<(), SdkError> {
        if self.state != RuntimeState::Starting {
            return Err(SdkError::invalid_state("start", self.state_name()));
        }
        self.state = RuntimeState::Running;
        self.active_start_request = Some(req);
        Ok(())
    }

    pub fn mark_draining(&mut self) -> Result<(), SdkError> {
        if !matches!(self.state, RuntimeState::Running | RuntimeState::Starting) {
            return Err(SdkError::invalid_state("shutdown", self.state_name()));
        }
        self.state = RuntimeState::Draining;
        Ok(())
    }

    pub fn mark_stopped(&mut self) {
        self.state = RuntimeState::Stopped;
    }

    pub fn mark_failed(&mut self) {
        self.state = RuntimeState::Failed;
    }

    pub fn reset_to_new(&mut self) {
        self.state = RuntimeState::New;
        self.active_start_request = None;
    }

    fn state_name(&self) -> &'static str {
        match self.state {
            RuntimeState::New => "new",
            RuntimeState::Starting => "starting",
            RuntimeState::Running => "running",
            RuntimeState::Draining => "draining",
            RuntimeState::Stopped => "stopped",
            RuntimeState::Failed => "failed",
            RuntimeState::Unknown => "unknown",
        }
    }
}

fn legal_states_for_method(method: SdkMethod) -> &'static [RuntimeState] {
    use RuntimeState as S;
    match method {
        SdkMethod::Start => &[S::New, S::Running],
        SdkMethod::Send => &[S::Running],
        SdkMethod::Cancel => &[S::Running, S::Draining],
        SdkMethod::Status => &[S::Running, S::Draining],
        SdkMethod::Configure => &[S::Running],
        SdkMethod::Tick => &[S::Running, S::Draining],
        SdkMethod::PollEvents => &[S::Running, S::Draining],
        SdkMethod::Snapshot => &[S::Running, S::Draining],
        SdkMethod::Shutdown => &[S::Starting, S::Running, S::Draining, S::Stopped, S::Failed],
        SdkMethod::SubscribeEvents => &[S::Running, S::Draining],
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{
        AuthMode, BindMode, EventSinkConfig, EventSinkKind, EventStreamConfig, OverflowPolicy,
        Profile, RedactionConfig, RedactionTransform, SdkConfig, StoreForwardCapacityPolicy,
        StoreForwardConfig, StoreForwardEvictionPriority,
    };
    use std::collections::BTreeMap;

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum ModelState {
        New,
        Starting,
        Running,
        Draining,
        Stopped,
        Failed,
    }

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum ModelOp {
        Start,
        Run,
        Drain,
        Stop,
        Fail,
        Reset,
    }

    const MODEL_OPS: [ModelOp; 6] = [
        ModelOp::Start,
        ModelOp::Run,
        ModelOp::Drain,
        ModelOp::Stop,
        ModelOp::Fail,
        ModelOp::Reset,
    ];

    fn sample_start_request() -> StartRequest {
        StartRequest {
            supported_contract_versions: vec![2, 1],
            requested_capabilities: vec!["sdk.capability.cursor_replay".to_owned()],
            config: SdkConfig {
                profile: Profile::DesktopFull,
                bind_mode: BindMode::LocalOnly,
                auth_mode: AuthMode::LocalTrusted,
                overflow_policy: OverflowPolicy::Reject,
                block_timeout_ms: None,
                store_forward: StoreForwardConfig {
                    max_messages: 50_000,
                    max_message_age_ms: 604_800_000,
                    capacity_policy: StoreForwardCapacityPolicy::DropOldest,
                    eviction_priority: StoreForwardEvictionPriority::TerminalFirst,
                },
                event_stream: EventStreamConfig {
                    max_poll_events: 256,
                    max_event_bytes: 65_536,
                    max_batch_bytes: 1_048_576,
                    max_extension_keys: 32,
                },
                event_sink: EventSinkConfig {
                    enabled: false,
                    max_event_bytes: 65_536,
                    allow_kinds: vec![
                        EventSinkKind::Webhook,
                        EventSinkKind::Mqtt,
                        EventSinkKind::Custom,
                    ],
                    extensions: BTreeMap::new(),
                },
                idempotency_ttl_ms: 86_400_000,
                redaction: RedactionConfig {
                    enabled: true,
                    sensitive_transform: RedactionTransform::Hash,
                    break_glass_allowed: false,
                    break_glass_ttl_ms: None,
                },
                rpc_backend: None,
                extensions: BTreeMap::new(),
            },
        }
    }

    fn apply_model(state: ModelState, op: ModelOp) -> Result<ModelState, ()> {
        match op {
            ModelOp::Start if state == ModelState::New => Ok(ModelState::Starting),
            ModelOp::Run if state == ModelState::Starting => Ok(ModelState::Running),
            ModelOp::Drain if matches!(state, ModelState::Starting | ModelState::Running) => {
                Ok(ModelState::Draining)
            }
            ModelOp::Stop => Ok(ModelState::Stopped),
            ModelOp::Fail => Ok(ModelState::Failed),
            ModelOp::Reset => Ok(ModelState::New),
            _ => Err(()),
        }
    }

    fn apply_lifecycle(lifecycle: &mut Lifecycle, op: ModelOp) -> Result<(), SdkError> {
        match op {
            ModelOp::Start => lifecycle.mark_starting(),
            ModelOp::Run => lifecycle.mark_running(sample_start_request()),
            ModelOp::Drain => lifecycle.mark_draining(),
            ModelOp::Stop => {
                lifecycle.mark_stopped();
                Ok(())
            }
            ModelOp::Fail => {
                lifecycle.mark_failed();
                Ok(())
            }
            ModelOp::Reset => {
                lifecycle.reset_to_new();
                Ok(())
            }
        }
    }

    fn model_to_runtime(state: ModelState) -> RuntimeState {
        match state {
            ModelState::New => RuntimeState::New,
            ModelState::Starting => RuntimeState::Starting,
            ModelState::Running => RuntimeState::Running,
            ModelState::Draining => RuntimeState::Draining,
            ModelState::Stopped => RuntimeState::Stopped,
            ModelState::Failed => RuntimeState::Failed,
        }
    }

    fn model_method_legal(state: ModelState, method: SdkMethod) -> bool {
        match method {
            SdkMethod::Start => matches!(state, ModelState::New | ModelState::Running),
            SdkMethod::Send => state == ModelState::Running,
            SdkMethod::Cancel
            | SdkMethod::Status
            | SdkMethod::Tick
            | SdkMethod::PollEvents
            | SdkMethod::Snapshot
            | SdkMethod::SubscribeEvents => {
                matches!(state, ModelState::Running | ModelState::Draining)
            }
            SdkMethod::Configure => state == ModelState::Running,
            SdkMethod::Shutdown => {
                matches!(
                    state,
                    ModelState::Starting
                        | ModelState::Running
                        | ModelState::Draining
                        | ModelState::Stopped
                        | ModelState::Failed
                )
            }
        }
    }

    fn generate_sequences(max_len: usize) -> Vec<Vec<ModelOp>> {
        fn recurse(target_len: usize, current: &mut Vec<ModelOp>, out: &mut Vec<Vec<ModelOp>>) {
            if current.len() == target_len {
                out.push(current.clone());
                return;
            }
            for op in MODEL_OPS {
                current.push(op);
                recurse(target_len, current, out);
                current.pop();
            }
        }

        let mut out = Vec::new();
        for len in 1..=max_len {
            let mut current = Vec::new();
            recurse(len, &mut current, &mut out);
        }
        out
    }

    fn assert_method_legality_matches_model(lifecycle: &Lifecycle, model_state: ModelState) {
        let all_methods = [
            SdkMethod::Start,
            SdkMethod::Send,
            SdkMethod::Cancel,
            SdkMethod::Status,
            SdkMethod::Configure,
            SdkMethod::Tick,
            SdkMethod::PollEvents,
            SdkMethod::Snapshot,
            SdkMethod::Shutdown,
            SdkMethod::SubscribeEvents,
        ];

        for method in all_methods {
            let expected_ok = model_method_legal(model_state, method);
            let actual_ok = lifecycle.ensure_method_legal(method).is_ok();
            assert_eq!(
                actual_ok, expected_ok,
                "method legality mismatch for state {:?} and method {:?}",
                model_state, method
            );
        }
    }

    #[test]
    fn method_legality_matrix_enforced() {
        let mut lifecycle = Lifecycle::default();
        assert!(lifecycle.ensure_method_legal(SdkMethod::Start).is_ok());
        assert!(lifecycle.ensure_method_legal(SdkMethod::Send).is_err());

        lifecycle.mark_starting().expect("new -> starting");
        lifecycle.mark_running(sample_start_request()).expect("starting -> running");
        assert!(lifecycle.ensure_method_legal(SdkMethod::Send).is_ok());
        assert!(lifecycle.ensure_method_legal(SdkMethod::Configure).is_ok());
        assert!(lifecycle.ensure_method_legal(SdkMethod::Shutdown).is_ok());
    }

    #[test]
    fn start_reentry_same_request_reuses_running_session() {
        let request = sample_start_request();
        let mut lifecycle = Lifecycle::default();
        lifecycle.mark_starting().expect("new -> starting");
        lifecycle.mark_running(request.clone()).expect("starting -> running");
        let reused = lifecycle.check_start_reentry(&request).expect("same request should reuse");
        assert!(reused);
    }

    #[test]
    fn start_reentry_different_request_is_rejected() {
        let request = sample_start_request();
        let mut other_request = sample_start_request();
        other_request.requested_capabilities = vec!["sdk.capability.async_events".to_owned()];
        let mut lifecycle = Lifecycle::default();
        lifecycle.mark_starting().expect("new -> starting");
        lifecycle.mark_running(request).expect("starting -> running");
        let err = lifecycle.check_start_reentry(&other_request).expect_err("must reject mismatch");
        assert_eq!(err.machine_code, code::RUNTIME_ALREADY_RUNNING_WITH_DIFFERENT_CONFIG);
    }

    #[test]
    fn lifecycle_model_transitions_and_method_legality_match_reference() {
        let sequences = generate_sequences(4);
        for sequence in sequences {
            let mut lifecycle = Lifecycle::default();
            let mut model_state = ModelState::New;
            assert_method_legality_matches_model(&lifecycle, model_state);

            for op in &sequence {
                let expected = apply_model(model_state, *op);
                let actual = apply_lifecycle(&mut lifecycle, *op);

                assert_eq!(
                    actual.is_ok(),
                    expected.is_ok(),
                    "operation mismatch: op={:?}, sequence={:?}, runtime_state={:?}, model_state={:?}",
                    op,
                    sequence,
                    lifecycle.state(),
                    model_state
                );

                if let Ok(next_state) = expected {
                    model_state = next_state;
                }

                assert_eq!(
                    lifecycle.state(),
                    model_to_runtime(model_state),
                    "state mismatch after op {:?} in sequence {:?}",
                    op,
                    sequence
                );
                assert_method_legality_matches_model(&lifecycle, model_state);
            }
        }
    }
}