devicerail-protocol 0.3.7

Canonical wire protocol DTOs for DeviceRail
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::fmt;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;

use crate::{ActionResult, AssetRef, DeviceId, Observation, RecordedActionCall, RpcId, Viewport};

/// Maximum Unicode code points in a persisted Verdict summary.
pub const MAX_VERDICT_SUMMARY_LENGTH: usize = 16 * 1024;
/// Maximum typed Evidence references attached to one Verdict.
pub const MAX_VERDICT_EVIDENCE_REFERENCES: usize = 64;

const fn is_false(value: &bool) -> bool {
    !*value
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(transparent)]
pub struct SessionId(pub Uuid);

impl SessionId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

impl Default for SessionId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for SessionId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl From<Uuid> for SessionId {
    fn from(value: Uuid) -> Self {
        Self(value)
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(transparent)]
pub struct EventId(pub Uuid);

impl EventId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

impl Default for EventId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for EventId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl From<Uuid> for EventId {
    fn from(value: Uuid) -> Self {
        Self(value)
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(transparent)]
pub struct MediaStreamId(pub Uuid);

impl MediaStreamId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

impl Default for MediaStreamId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for MediaStreamId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl From<Uuid> for MediaStreamId {
    fn from(value: Uuid) -> Self {
        Self(value)
    }
}

/// A one-based sequence number within one session.
///
/// The wire value is capped at JavaScript's maximum safe integer so generated
/// clients can sort and resume event streams without losing precision.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(transparent)]
pub struct EventSequence(
    #[serde(
        serialize_with = "crate::wire_integer::serialize_js_safe_u64",
        deserialize_with = "deserialize_event_sequence"
    )]
    #[cfg_attr(
        feature = "schema",
        schemars(range(min = 1_u64, max = 9_007_199_254_740_991_u64))
    )]
    u64,
);

fn deserialize_event_sequence<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = crate::wire_integer::deserialize_js_safe_u64(deserializer)?;
    if value == 0 {
        Err(serde::de::Error::custom(
            "event sequence must be a one-based integer",
        ))
    } else {
        Ok(value)
    }
}

impl EventSequence {
    pub const FIRST: Self = Self(1);

    pub fn new(value: u64) -> Option<Self> {
        (value > 0 && value <= crate::MAX_SAFE_INTEGER).then_some(Self(value))
    }

    pub const fn get(self) -> u64 {
        self.0
    }

    pub fn checked_next(self) -> Option<Self> {
        self.0.checked_add(1).and_then(Self::new)
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum VerdictStatus {
    Pass,
    Fail,
    Unknown,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Verdict {
    pub status: VerdictStatus,
    #[cfg_attr(feature = "schema", schemars(length(min = 1, max = 16_384)))]
    pub summary: String,
    #[serde(default)]
    #[cfg_attr(feature = "schema", schemars(length(max = 64)))]
    pub evidence: Vec<AssetRef>,
}

impl Verdict {
    pub fn validate(&self) -> Result<(), VerdictValidationError> {
        if self.summary.trim().is_empty() {
            return Err(VerdictValidationError::EmptySummary);
        }
        let summary_length = self.summary.chars().count();
        if summary_length > MAX_VERDICT_SUMMARY_LENGTH {
            return Err(VerdictValidationError::SummaryTooLong {
                actual: summary_length,
                maximum: MAX_VERDICT_SUMMARY_LENGTH,
            });
        }
        if self.evidence.len() > MAX_VERDICT_EVIDENCE_REFERENCES {
            return Err(VerdictValidationError::TooManyEvidenceReferences {
                actual: self.evidence.len(),
                maximum: MAX_VERDICT_EVIDENCE_REFERENCES,
            });
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VerdictValidationError {
    EmptySummary,
    SummaryTooLong { actual: usize, maximum: usize },
    TooManyEvidenceReferences { actual: usize, maximum: usize },
}

impl fmt::Display for VerdictValidationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptySummary => formatter.write_str("verdict summary must not be blank"),
            Self::SummaryTooLong { actual, maximum } => write!(
                formatter,
                "verdict summary contains {actual} Unicode code points; maximum is {maximum}"
            ),
            Self::TooManyEvidenceReferences { actual, maximum } => write!(
                formatter,
                "verdict contains {actual} Evidence references; maximum is {maximum}"
            ),
        }
    }
}

impl std::error::Error for VerdictValidationError {}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ErrorInfo {
    pub code: String,
    pub message: String,
    pub retryable: bool,
    pub details: Option<Value>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum SessionState {
    Active,
    Ended,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum SessionOutcome {
    Completed,
    Failed,
    Cancelled,
    Shutdown,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SessionInfo {
    pub id: SessionId,
    pub state: SessionState,
    #[serde(
        serialize_with = "crate::wire_integer::serialize_js_safe_u64",
        deserialize_with = "crate::wire_integer::deserialize_js_safe_u64"
    )]
    #[cfg_attr(feature = "schema", schemars(range(max = 9_007_199_254_740_991_u64)))]
    pub started_at_ms: u64,
    #[serde(
        default,
        serialize_with = "crate::wire_integer::serialize_optional_js_safe_u64",
        deserialize_with = "crate::wire_integer::deserialize_optional_js_safe_u64"
    )]
    #[cfg_attr(feature = "schema", schemars(with = "Option<SafeWireIntegerSchema>"))]
    pub ended_at_ms: Option<u64>,
    pub event_count: EventSequence,
    pub last_sequence: EventSequence,
}

#[cfg(feature = "schema")]
#[allow(dead_code)]
#[derive(schemars::JsonSchema)]
#[schemars(inline)]
struct SafeWireIntegerSchema(#[schemars(range(max = 9_007_199_254_740_991_u64))] u64);

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SessionExport {
    pub session: SessionInfo,
    pub events: Vec<TestEvent>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum MediaStreamKind {
    Screenshot,
    Video,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MediaStreamInfo {
    pub id: MediaStreamId,
    pub kind: MediaStreamKind,
    #[cfg_attr(feature = "schema", schemars(length(min = 1, max = 255)))]
    pub media_type: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub viewport: Option<Viewport>,
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MediaFrame {
    pub stream_id: MediaStreamId,
    pub frame_index: EventSequence,
    #[serde(default, skip_serializing_if = "is_false")]
    pub key_frame: bool,
    #[serde(
        default,
        serialize_with = "crate::wire_integer::serialize_optional_js_safe_u64",
        deserialize_with = "crate::wire_integer::deserialize_optional_js_safe_u64",
        skip_serializing_if = "Option::is_none"
    )]
    #[cfg_attr(feature = "schema", schemars(with = "Option<SafeWireIntegerSchema>"))]
    pub duration_ms: Option<u64>,
    pub evidence: AssetRef,
}

/// The terminal outcome for one action call.
///
/// Keeping the four outcomes structurally distinct prevents clients from
/// having to infer timeout or cancellation from human-readable error text.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(
    tag = "outcome",
    rename_all = "camelCase",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum ActionOutcome {
    Succeeded {
        result: Box<ActionResult>,
    },
    Failed {
        error: ErrorInfo,
    },
    Cancelled {
        error: ErrorInfo,
    },
    TimedOut {
        error: ErrorInfo,
        #[serde(
            serialize_with = "crate::wire_integer::serialize_js_safe_u64",
            deserialize_with = "crate::wire_integer::deserialize_js_safe_u64"
        )]
        #[cfg_attr(feature = "schema", schemars(range(max = 9_007_199_254_740_991_u64)))]
        timeout_ms: u64,
    },
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(
    tag = "type",
    rename_all = "camelCase",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum TestEventPayload {
    SessionStarted,
    SessionEnded {
        outcome: SessionOutcome,
        reason: Option<String>,
    },
    ObservationCaptured {
        observation: Box<Observation>,
    },
    ActionStarted {
        call: RecordedActionCall,
    },
    ActionCompleted {
        call_id: Uuid,
        outcome: ActionOutcome,
    },
    MediaStreamStarted {
        stream: MediaStreamInfo,
    },
    MediaFrameCaptured {
        frame: MediaFrame,
    },
    MediaStreamEnded {
        stream_id: MediaStreamId,
        #[serde(
            serialize_with = "crate::wire_integer::serialize_js_safe_u64",
            deserialize_with = "crate::wire_integer::deserialize_js_safe_u64"
        )]
        #[cfg_attr(feature = "schema", schemars(range(max = 9_007_199_254_740_991_u64)))]
        frame_count: u64,
    },
    VerdictRecorded {
        verdict: Verdict,
    },
    Error {
        error: ErrorInfo,
    },
}

impl TestEventPayload {
    /// Lowest Protocol 1.x minor that can represent this payload without
    /// silently dropping additive fields.
    pub fn required_protocol_minor(&self) -> u16 {
        match self {
            Self::MediaStreamStarted { .. }
            | Self::MediaFrameCaptured { .. }
            | Self::MediaStreamEnded { .. } => 4,
            Self::ObservationCaptured { observation }
                if observation.ui_snapshot.is_some()
                    || observation.ui_snapshot_omission.is_some() =>
            {
                5
            }
            Self::ActionCompleted {
                outcome: ActionOutcome::Succeeded { result },
                ..
            } if result.execution.is_some()
                || result
                    .before
                    .iter()
                    .chain(result.after.iter())
                    .any(|observation| {
                        observation.ui_snapshot.is_some()
                            || observation.ui_snapshot_omission.is_some()
                    }) =>
            {
                5
            }
            _ => 0,
        }
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TestEvent {
    pub event_id: EventId,
    pub session_id: SessionId,
    pub sequence: EventSequence,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub request_id: Option<RpcId>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub device_id: Option<DeviceId>,
    #[serde(
        serialize_with = "crate::wire_integer::serialize_js_safe_u64",
        deserialize_with = "crate::wire_integer::deserialize_js_safe_u64"
    )]
    #[cfg_attr(feature = "schema", schemars(range(max = 9_007_199_254_740_991_u64)))]
    pub at_ms: u64,
    pub payload: TestEventPayload,
}

impl TestEvent {
    pub fn required_protocol_minor(&self) -> u16 {
        self.payload.required_protocol_minor()
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use uuid::Uuid;

    use super::{
        ActionOutcome, ErrorInfo, EventId, EventSequence, MAX_VERDICT_EVIDENCE_REFERENCES,
        MAX_VERDICT_SUMMARY_LENGTH, SessionId, TestEvent, TestEventPayload, Verdict, VerdictStatus,
        VerdictValidationError,
    };
    use crate::AssetRef;

    #[test]
    fn envelope_nests_payload_and_has_js_safe_sequence() {
        let event = TestEvent {
            event_id: EventId::from(Uuid::nil()),
            session_id: SessionId::from(Uuid::nil()),
            sequence: EventSequence::FIRST,
            request_id: None,
            device_id: None,
            at_ms: 1,
            payload: TestEventPayload::SessionStarted,
        };

        assert_eq!(
            serde_json::to_value(event).expect("serialize event"),
            json!({
                "eventId": Uuid::nil(),
                "sessionId": Uuid::nil(),
                "sequence": 1,
                "atMs": 1,
                "payload": {
                    "type": "sessionStarted"
                }
            })
        );
        assert!(EventSequence::new(crate::MAX_SAFE_INTEGER).is_some());
        assert!(EventSequence::new(crate::MAX_SAFE_INTEGER + 1).is_none());
        assert!(serde_json::from_value::<EventSequence>(json!(0)).is_err());
        assert!(
            serde_json::from_value::<EventSequence>(json!(crate::MAX_SAFE_INTEGER + 1)).is_err()
        );
    }

    #[test]
    fn action_outcomes_have_explicit_wire_status() {
        let outcome = ActionOutcome::Cancelled {
            error: ErrorInfo {
                code: "action_cancelled".to_owned(),
                message: "cancelled".to_owned(),
                retryable: false,
                details: None,
            },
        };
        assert_eq!(
            serde_json::to_value(outcome).expect("serialize outcome")["outcome"],
            "cancelled"
        );
    }

    #[test]
    fn verdict_validation_uses_schema_aligned_unicode_and_evidence_bounds() {
        let mut verdict = Verdict {
            status: VerdictStatus::Unknown,
            summary: "🧪".repeat(MAX_VERDICT_SUMMARY_LENGTH),
            evidence: Vec::new(),
        };
        verdict.validate().expect("maximum Unicode length");

        verdict.summary.push('x');
        assert_eq!(
            verdict.validate(),
            Err(VerdictValidationError::SummaryTooLong {
                actual: MAX_VERDICT_SUMMARY_LENGTH + 1,
                maximum: MAX_VERDICT_SUMMARY_LENGTH,
            })
        );

        verdict.summary = " \n\t".to_owned();
        assert_eq!(
            verdict.validate(),
            Err(VerdictValidationError::EmptySummary)
        );

        verdict.summary = "bounded".to_owned();
        verdict.evidence = vec![
            AssetRef {
                id: "asset".to_owned(),
                media_type: "image/png".to_owned(),
                uri: "evidence://asset".to_owned(),
                sha256: None,
            };
            MAX_VERDICT_EVIDENCE_REFERENCES + 1
        ];
        assert_eq!(
            verdict.validate(),
            Err(VerdictValidationError::TooManyEvidenceReferences {
                actual: MAX_VERDICT_EVIDENCE_REFERENCES + 1,
                maximum: MAX_VERDICT_EVIDENCE_REFERENCES,
            })
        );
    }
}