Skip to main content

mesh_llm_runtime_event_contracts/
native.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub struct NativeEventCategory(u32);
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct NativeEventKind(u32);
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct NativeEmitter(u32);
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct NativeProgressUnit(u32);
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct NativeFailureCode(u32);
15
16macro_rules! native_u32_value {
17    ($name:ident) => {
18        impl $name {
19            #[must_use]
20            pub const fn new(raw: u32) -> Self {
21                Self(raw)
22            }
23
24            #[must_use]
25            pub const fn raw(self) -> u32 {
26                self.0
27            }
28        }
29    };
30}
31
32native_u32_value!(NativeEventCategory);
33native_u32_value!(NativeEventKind);
34native_u32_value!(NativeEmitter);
35native_u32_value!(NativeProgressUnit);
36native_u32_value!(NativeFailureCode);
37
38/// Which native callback sequence a fact belongs to. Process reporters use a
39/// sequence shared by callbacks that can be assigned to different operation
40/// scopes; model-open reporters use a sequence local to one operation.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42pub enum NativeSequenceDomain {
43    Operation,
44    Process,
45}
46
47/// Evidence computed at the native adapter boundary, before host lanes can
48/// coalesce or discard an observation. The reducer records only `Gap`; it
49/// never infers a gap from two surviving reduced facts.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum NativeSequenceEvidence {
52    Unchecked,
53    First,
54    Contiguous,
55    Gap,
56    DuplicateOrOutOfOrder,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub struct NativeSequenceObservation {
61    pub domain: NativeSequenceDomain,
62    pub emitter: NativeEmitter,
63    pub sequence: u64,
64    pub evidence: NativeSequenceEvidence,
65}
66
67impl NativeSequenceObservation {
68    #[must_use]
69    pub const fn new(
70        domain: NativeSequenceDomain,
71        emitter: NativeEmitter,
72        sequence: u64,
73        evidence: NativeSequenceEvidence,
74    ) -> Self {
75        Self {
76            domain,
77            emitter,
78            sequence,
79            evidence,
80        }
81    }
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct NativeStatus(i32);
86
87impl NativeStatus {
88    #[must_use]
89    pub const fn new(raw: i32) -> Self {
90        Self(raw)
91    }
92
93    #[must_use]
94    pub const fn raw(self) -> i32 {
95        self.0
96    }
97}
98
99#[derive(Debug, Clone, PartialEq, Eq)]
100pub struct NativeDetail(Vec<u8>);
101
102impl NativeDetail {
103    #[must_use]
104    pub fn bytes(value: &[u8]) -> Self {
105        Self(value.to_vec())
106    }
107
108    #[must_use]
109    pub fn as_bytes(&self) -> &[u8] {
110        &self.0
111    }
112}
113
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct NativeSourceEnvelope {
116    pub abi_version: u32,
117    pub struct_size: u32,
118    pub category: NativeEventCategory,
119    pub kind: NativeEventKind,
120    pub emitter: NativeEmitter,
121    pub reserved0: u32,
122    pub sequence: u64,
123    pub timestamp_mono_ns: u64,
124    pub model_id: u64,
125    pub stage_id: u64,
126    pub session_id: u64,
127    pub progress_current: u64,
128    pub progress_total: u64,
129    pub progress_unit: NativeProgressUnit,
130    pub failure_code: NativeFailureCode,
131    pub status: NativeStatus,
132    pub reserved1: u32,
133    pub detail: NativeDetail,
134    // Present only when the native `struct_size` covered that field's
135    // offset in the raw ABI struct; `None` means the emitting native
136    // runtime predates the append-only extension (or built without it),
137    // not that the value is zero.
138    pub numeric_summary_0: Option<u64>,
139    pub numeric_summary_1: Option<u64>,
140    pub numeric_summary_2: Option<u64>,
141    pub numeric_summary_3: Option<u64>,
142}