dora_message/metadata.rs
1use std::collections::BTreeMap;
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Additional data that is sent as part of output messages.
7///
8/// Includes a timestamp and additional user-provided parameters. The payload is
9/// a self-describing Arrow IPC stream, so the message carries no separate type
10/// descriptor.
11///
12/// A node receives a `Metadata` alongside every input. The [`timestamp`] and
13/// [`metadata_version`] are read through accessors, while [`parameters`] — the
14/// user-provided key/value pairs — is a public field read and written directly.
15///
16/// [`timestamp`]: Self::timestamp
17/// [`metadata_version`]: Self::metadata_version
18/// [`parameters`]: Self::parameters
19///
20/// # Examples
21///
22/// ```
23/// use dora_message::metadata::{get_integer_param, Metadata, Parameter};
24///
25/// let timestamp = uhlc::HLC::default().new_timestamp();
26/// let mut metadata = Metadata::new(timestamp);
27///
28/// // A fresh `Metadata` carries the current wire-format version and no
29/// // user parameters.
30/// assert_eq!(metadata.metadata_version(), Metadata::CURRENT_VERSION);
31/// assert!(metadata.parameters.is_empty());
32///
33/// // Attach a typed parameter and read it back with the matching getter.
34/// metadata
35/// .parameters
36/// .insert("frame".to_string(), Parameter::Integer(7));
37/// assert_eq!(get_integer_param(&metadata.parameters, "frame"), Some(7));
38/// ```
39#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
40pub struct Metadata {
41 metadata_version: u16,
42 timestamp: uhlc::Timestamp,
43 /// User-provided key/value parameters carried alongside the payload.
44 ///
45 /// A [`BTreeMap`] of [`Parameter`] values keyed by name. Read the common
46 /// scalar cases through the type-checked [`get_string_param`],
47 /// [`get_integer_param`], and [`get_bool_param`] helpers rather than
48 /// matching on the [`Parameter`] variant directly.
49 pub parameters: MetadataParameters,
50}
51
52impl Metadata {
53 /// Current metadata wire-format version, stamped on every outgoing message.
54 ///
55 /// Bumped from 0 to 1 when the `ArrowTypeInfo` sidecar was dropped and the
56 /// wire format became Arrow-IPC-only, and from 1 to 2 when the binary
57 /// encoding moved from bincode to postcard (varint integers and length
58 /// prefixes, so the byte layout differs even though the field order does
59 /// not). A receiver can compare
60 /// [`metadata_version`](Self::metadata_version) against this to detect a peer
61 /// speaking an incompatible format and report it clearly instead of failing
62 /// with a cryptic positional-deserialization error.
63 pub const CURRENT_VERSION: u16 = 2;
64
65 /// Create metadata with the given timestamp and no user parameters.
66 pub fn new(timestamp: uhlc::Timestamp) -> Self {
67 Self::from_parameters(timestamp, Default::default())
68 }
69
70 /// Metadata for a startup route-probe marker (see [`STARTUP_MARKER_PARAM`]).
71 pub fn startup_marker(timestamp: uhlc::Timestamp) -> Self {
72 Self::from_parameters(
73 timestamp,
74 BTreeMap::from([(STARTUP_MARKER_PARAM.to_owned(), Parameter::Bool(true))]),
75 )
76 }
77
78 /// Whether this message is a startup route-probe marker rather than node
79 /// data. Markers are consumed by the receiving node's startup barrier and
80 /// must never be decoded or surfaced to user code — see
81 /// [`STARTUP_MARKER_PARAM`].
82 pub fn is_startup_marker(&self) -> bool {
83 get_bool_param(&self.parameters, STARTUP_MARKER_PARAM).unwrap_or(false)
84 }
85
86 /// Metadata for a startup route-probe **ack**: the consumer-side reply to a
87 /// startup marker, identifying which consumer input received it (see
88 /// [`STARTUP_ACK_PARAM`]).
89 pub fn startup_ack(timestamp: uhlc::Timestamp, consumer_node: &str, input_id: &str) -> Self {
90 Self::from_parameters(
91 timestamp,
92 BTreeMap::from([
93 (STARTUP_ACK_PARAM.to_owned(), Parameter::Bool(true)),
94 (
95 STARTUP_ACK_CONSUMER_PARAM.to_owned(),
96 Parameter::String(consumer_node.to_owned()),
97 ),
98 (
99 STARTUP_ACK_INPUT_PARAM.to_owned(),
100 Parameter::String(input_id.to_owned()),
101 ),
102 ]),
103 )
104 }
105
106 /// `Some((consumer_node, input_id))` iff this message is a well-formed
107 /// startup route-probe ack — see [`STARTUP_ACK_PARAM`]. Malformed acks
108 /// (missing or wrongly-typed identity parameters) return `None` and are
109 /// ignored by producers, which keeps the affected output on the reliable
110 /// daemon path instead of switching on bad evidence.
111 pub fn startup_ack_identity(&self) -> Option<(&str, &str)> {
112 if !get_bool_param(&self.parameters, STARTUP_ACK_PARAM).unwrap_or(false) {
113 return None;
114 }
115 let consumer = get_string_param(&self.parameters, STARTUP_ACK_CONSUMER_PARAM)?;
116 let input = get_string_param(&self.parameters, STARTUP_ACK_INPUT_PARAM)?;
117 Some((consumer, input))
118 }
119
120 /// Create metadata with the given timestamp and user parameters, stamping
121 /// the current wire-format version ([`CURRENT_VERSION`](Self::CURRENT_VERSION)).
122 pub fn from_parameters(timestamp: uhlc::Timestamp, parameters: MetadataParameters) -> Self {
123 Self {
124 metadata_version: Self::CURRENT_VERSION,
125 timestamp,
126 parameters,
127 }
128 }
129
130 /// The wire-format version stamped on this metadata. Compare against
131 /// [`CURRENT_VERSION`](Self::CURRENT_VERSION) on receive to reject peers
132 /// using an incompatible format.
133 pub fn metadata_version(&self) -> u16 {
134 self.metadata_version
135 }
136
137 /// The hybrid-logical-clock timestamp assigned when this message was sent.
138 pub fn timestamp(&self) -> uhlc::Timestamp {
139 self.timestamp
140 }
141
142 /// The serialized OpenTelemetry propagation context carried in the
143 /// `open_telemetry_context` parameter, or an empty string if absent.
144 pub fn open_telemetry_context(&self) -> String {
145 get_string_param(&self.parameters, OPEN_TELEMETRY_CONTEXT)
146 .unwrap_or("")
147 .to_string()
148 }
149}
150
151/// Reserved [`MetadataParameters`] key marking a message as a **startup
152/// route-probe marker** rather than node data.
153///
154/// The zenoh data plane is direct node-to-node pub/sub, so a producer that
155/// publishes before a consumer's subscription has propagated would drop those
156/// early samples. Rather than infer route-readiness from zenoh declarations,
157/// each producer publishes markers on its real output topic while an output is
158/// still on the reliable daemon path, and each consumer answers every received
159/// marker with an ack (see [`STARTUP_ACK_PARAM`]): an ack arriving back at the
160/// producer is end-to-end proof that the route pair carries data. The producer
161/// stops marking an output — and switches it to the direct zenoh path — once
162/// all its required consumers have acked, but only within a bounded startup
163/// window: an output still un-acked when the window closes is pinned to the
164/// daemon path for the rest of the run, so a topic's messages never straddle a
165/// path switch (dora-rs/dora#2891). A route that never proves itself just keeps
166/// the output on the daemon path.
167///
168/// The `__dora_` prefix is reserved; user parameters must not use it. Receivers
169/// filter markers before decoding the payload, so they never reach user code.
170pub const STARTUP_MARKER_PARAM: &str = "__dora_startup_marker";
171
172/// Reserved [`MetadataParameters`] key marking a message as a **startup
173/// route-probe ack**: the consumer-side half of the startup handshake.
174///
175/// When a consumer's data subscriber receives a startup marker (see
176/// [`STARTUP_MARKER_PARAM`]) for an input, it replies with an ack on the
177/// output's dedicated `@ack` topic. An ack arriving back at the producer is
178/// end-to-end proof that the route works in *both* directions; once every
179/// required consumer of an output has acked, the producer switches that output
180/// from the reliable daemon path to the direct node-to-node zenoh path. Ack
181/// timing is load-bearing: the switch can only happen inside the producer's
182/// bounded startup window (see [`STARTUP_MARKER_PARAM`]), so a consumer that
183/// acks late costs that output the fast path for the whole run. A missing or
184/// late ack never fails anything — the output simply stays on the daemon path.
185///
186/// Acks travel as an empty-payload message whose attachment carries this flag
187/// plus the acking consumer's identity under [`STARTUP_ACK_CONSUMER_PARAM`] and
188/// [`STARTUP_ACK_INPUT_PARAM`] — the identity rides in the attachment rather
189/// than the zenoh key so consumer/input ids never need key escaping.
190pub const STARTUP_ACK_PARAM: &str = "__dora_startup_ack";
191
192/// Reserved key carrying the acking consumer's node id as a
193/// [`Parameter::String`] — see [`STARTUP_ACK_PARAM`].
194pub const STARTUP_ACK_CONSUMER_PARAM: &str = "__dora_startup_ack_consumer";
195
196/// Reserved key carrying the acking consumer's input id as a
197/// [`Parameter::String`] — see [`STARTUP_ACK_PARAM`].
198pub const STARTUP_ACK_INPUT_PARAM: &str = "__dora_startup_ack_input";
199
200/// Additional metadata that can be sent as part of output messages.
201pub type MetadataParameters = BTreeMap<String, Parameter>;
202
203/// A typed metadata parameter sent as part of output messages.
204///
205/// Parameters are stored by key in [`MetadataParameters`]. The `get_*_param`
206/// helpers ([`get_string_param`], [`get_integer_param`], [`get_bool_param`])
207/// are type-checked: they return the value only when the stored variant matches
208/// the requested type, and `None` both for a missing key **and** for a key whose
209/// stored value has a different type. Callers therefore never need to match on
210/// the variant themselves for the common scalar cases.
211///
212/// ```
213/// use dora_message::metadata::{
214/// get_integer_param, get_string_param, MetadataParameters, Parameter,
215/// };
216///
217/// let mut params = MetadataParameters::new();
218/// params.insert("frame".to_string(), Parameter::Integer(7));
219///
220/// // Matching type -> the value.
221/// assert_eq!(get_integer_param(¶ms, "frame"), Some(7));
222/// // Wrong requested type -> None (not a panic, not a coercion).
223/// assert_eq!(get_string_param(¶ms, "frame"), None);
224/// // Missing key -> None.
225/// assert_eq!(get_integer_param(¶ms, "absent"), None);
226/// ```
227#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
228pub enum Parameter {
229 /// A boolean value.
230 Bool(bool),
231 /// A signed 64-bit integer value.
232 Integer(i64),
233 /// A UTF-8 string value.
234 String(String),
235 /// A list of signed 64-bit integers.
236 ListInt(Vec<i64>),
237 /// A 64-bit floating-point value.
238 Float(f64),
239 /// A list of 64-bit floating-point values.
240 ListFloat(Vec<f64>),
241 /// A list of UTF-8 strings.
242 ListString(Vec<String>),
243 /// A UTC timestamp.
244 Timestamp(DateTime<Utc>),
245}
246
247/// Extract a string parameter from metadata, returning `None` if missing or
248/// not a `Parameter::String`.
249pub fn get_string_param<'a>(params: &'a MetadataParameters, key: &str) -> Option<&'a str> {
250 params.get(key).and_then(|p| match p {
251 Parameter::String(s) => Some(s.as_str()),
252 _ => None,
253 })
254}
255
256/// Extract an integer parameter from metadata, returning `None` if missing or
257/// not a `Parameter::Integer`.
258pub fn get_integer_param(params: &MetadataParameters, key: &str) -> Option<i64> {
259 params.get(key).and_then(|p| match p {
260 Parameter::Integer(n) => Some(*n),
261 _ => None,
262 })
263}
264
265/// Extract a bool parameter from metadata, returning `None` if missing or
266/// not a `Parameter::Bool`.
267pub fn get_bool_param(params: &MetadataParameters, key: &str) -> Option<bool> {
268 params.get(key).and_then(|p| match p {
269 Parameter::Bool(b) => Some(*b),
270 _ => None,
271 })
272}
273
274// ---------------------------------------------------------------------------
275// Well-known metadata parameter keys for service and action patterns
276// ---------------------------------------------------------------------------
277
278/// Metadata key for correlating a service request with its response.
279pub const REQUEST_ID: &str = "request_id";
280
281/// Metadata key for identifying an action goal across feedback/result messages.
282pub const GOAL_ID: &str = "goal_id";
283
284/// Metadata key for the completion status of an action goal.
285pub const GOAL_STATUS: &str = "goal_status";
286
287/// Goal completed successfully.
288pub const GOAL_STATUS_SUCCEEDED: &str = "succeeded";
289
290/// Goal was aborted by the server.
291pub const GOAL_STATUS_ABORTED: &str = "aborted";
292
293/// Goal was canceled by the client.
294pub const GOAL_STATUS_CANCELED: &str = "canceled";
295
296// ---------------------------------------------------------------------------
297// Well-known metadata parameter key for distributed tracing
298// ---------------------------------------------------------------------------
299
300/// Metadata key carrying the serialized OpenTelemetry propagation context, so a
301/// trace can be continued across a dora message hop. Read via
302/// [`Metadata::open_telemetry_context`]; stamped by the node and runtime send
303/// paths when tracing is enabled. Keep this the single source of truth so the
304/// write and read sides can never drift.
305pub const OPEN_TELEMETRY_CONTEXT: &str = "open_telemetry_context";
306
307// ---------------------------------------------------------------------------
308// Well-known metadata parameter keys for the streaming pattern
309// ---------------------------------------------------------------------------
310
311/// Metadata key identifying the conversation/session.
312pub const SESSION_ID: &str = "session_id";
313
314/// Metadata key for the logical segment within a session (e.g. one utterance).
315pub const SEGMENT_ID: &str = "segment_id";
316
317/// Metadata key for chunk sequence number within a segment.
318pub const SEQ: &str = "seq";
319
320/// Metadata key marking the last chunk of a segment (`true` on final chunk).
321pub const FIN: &str = "fin";
322
323/// Metadata key to discard older queued messages on this input (`true` to flush).
324pub const FLUSH: &str = "flush";
325
326/// Metadata key indicating the wire framing of the data payload.
327/// When set to `"arrow-ipc"`, the payload is an Arrow IPC stream.
328pub const FRAMING: &str = "_framing";
329
330/// Value for [`FRAMING`] indicating Arrow IPC stream framing.
331pub const FRAMING_ARROW_IPC: &str = "arrow-ipc";
332
333/// Metadata key carrying the FNV-1a hash (as an `i64`) of the Arrow IPC schema
334/// for a zenoh data message. Present on schema-once messages so a receiver can
335/// tell which primed decoder a schema-less batch belongs to and detect schema
336/// changes. Absent on messages a receiver should decode as a standalone full
337/// stream (large/SHM and daemon-path payloads).
338pub const SCHEMA_HASH: &str = "_schema_hash";
339
340/// Metadata key carrying the true on-wire byte size (as an `i64`) of a
341/// `dora topic` debug frame's data sample.
342///
343/// The daemon rebuilds a self-describing Arrow IPC stream for inspection by
344/// prepending the retained schema block to each schema-once batch, so the
345/// rebuilt stream the CLI receives is larger than what actually travelled on
346/// the wire (the schema-less batch — the schema is published only once on the
347/// `@schema` subtopic). To keep `dora topic info`'s bandwidth accounting
348/// accurate, the daemon stamps the original data-sample length here; the CLI
349/// measures this instead of the rebuilt stream length (dora-rs/dora#2584).
350///
351/// Debug/inspection path only — never set on real node→node outputs, and
352/// removed by [`strip_internal_parameters`] so it can't leak into one through a
353/// recorded or re-sent debug frame.
354pub const WIRE_SIZE: &str = "_wire_size";
355
356/// Byte size to charge for a `dora topic` debug frame when accounting bandwidth.
357///
358/// Prefers the daemon-stamped [`WIRE_SIZE`] (the real on-wire data-sample
359/// length): the `data` the CLI receives is a rebuilt self-describing stream
360/// whose schema was re-prepended for inspection, so for a schema-once output
361/// `data.len()` over-reports what actually travelled. Falls back to the buffer
362/// length when the key is absent — an older daemon, or a non-debug frame that
363/// never carried it (dora-rs/dora#2584). A present-but-out-of-range stamp (a
364/// negative `i64` that can't be a byte count) also falls back rather than
365/// silently counting zero. Keep this the single reader of [`WIRE_SIZE`] so the
366/// daemon stamp and the CLI accounting can never disagree on the fallback rule.
367pub fn debug_frame_wire_size(params: &MetadataParameters, data: Option<&[u8]>) -> usize {
368 get_integer_param(params, WIRE_SIZE)
369 .and_then(|n| usize::try_from(n).ok())
370 .or_else(|| data.map(|d| d.len()))
371 .unwrap_or(0)
372}
373
374/// Returns `true` if the given parameters carry any pattern-correlation key
375/// ([`REQUEST_ID`], [`GOAL_ID`], or [`GOAL_STATUS`]).
376///
377/// Messages marked with these keys belong to a service or action pattern where
378/// multiple Arrow schemas can legitimately flow through a single output/input,
379/// distinguished by metadata rather than a fixed Arrow type. Runtime type
380/// checks skip such messages (dora-rs/adora#150), and the schema-once zenoh
381/// optimization excludes them — on the send side (they always travel as full
382/// self-describing streams), on the node receive side (their schemas must not
383/// churn the per-input decoder), and on the daemon's `dora topic` debug path
384/// (same, for its schema cache). Keep this the single definition so those
385/// layers can never disagree on what "pattern-correlated" means.
386pub fn carries_pattern_correlation(params: &MetadataParameters) -> bool {
387 params.contains_key(REQUEST_ID)
388 || params.contains_key(GOAL_ID)
389 || params.contains_key(GOAL_STATUS)
390}
391
392/// Remove internal wire-protocol keys ([`SCHEMA_HASH`], [`FRAMING`], and the
393/// debug-frame [`WIRE_SIZE`] stamp) from a parameter map. Call this at every
394/// wire→user boundary: the keys are meaningless after decode, and a stale
395/// [`SCHEMA_HASH`] forwarded from an input's metadata into `send_output`
396/// parameters (a standard pattern, e.g. replay) would ride onto outputs that
397/// don't overwrite it, making receivers hash-mismatch and silently drop them
398/// (dora-rs/dora#2366 review).
399///
400/// The daemon stamps [`WIRE_SIZE`] *after* stripping a debug frame, so `dora
401/// topic info` still sees it; consumers that store, print or re-send debug
402/// frames call this to drop it.
403pub fn strip_internal_parameters(params: &mut MetadataParameters) {
404 params.remove(SCHEMA_HASH);
405 params.remove(FRAMING);
406 params.remove(WIRE_SIZE);
407}
408
409/// FNV-1a-64 hash with a fixed seed (cross-process deterministic). Used to
410/// fingerprint an Arrow IPC schema block so a schema-less batch can be matched
411/// to the schema it was encoded against (see [`SCHEMA_HASH`]). The producer
412/// node, the consumer node, and the daemon's `dora topic` debug path all hash
413/// the same schema-block bytes with this function, so the value must stay
414/// identical across crates — keep this the single source of truth. The CLI
415/// also persists digests made with it (session and build-lockfile
416/// fingerprints), another reason it must never change.
417pub fn fnv1a(bytes: &[u8]) -> u64 {
418 let mut hash: u64 = 0xcbf29ce484222325;
419 for b in bytes {
420 hash ^= *b as u64;
421 hash = hash.wrapping_mul(0x100000001b3);
422 }
423 hash
424}
425
426#[cfg(test)]
427mod tests {
428 use super::*;
429
430 #[test]
431 fn fnv1a_matches_standard_vectors() {
432 // Canonical FNV-1a-64 vectors — pin the algorithm so the producer and
433 // consumers (across crates/processes) never disagree on a schema hash.
434 assert_eq!(fnv1a(b""), 0xcbf29ce484222325);
435 assert_eq!(fnv1a(b"a"), 0xaf63dc4c8601ec8c);
436 }
437
438 fn test_timestamp() -> uhlc::Timestamp {
439 uhlc::HLC::default().new_timestamp()
440 }
441
442 #[test]
443 fn startup_marker_is_detected_and_survives_the_wire() {
444 // A marker is recognized only via the reserved parameter, and the flag
445 // must survive postcard round-tripping — it travels as the zenoh
446 // attachment, and a receiver that failed to recognize it would decode
447 // the marker as node data and surface it to user code.
448 let marker = Metadata::startup_marker(test_timestamp());
449 assert!(marker.is_startup_marker());
450
451 let bytes = crate::encode(&marker).expect("serialize");
452 let decoded: Metadata = crate::decode(&bytes).expect("deserialize");
453 assert!(decoded.is_startup_marker());
454 }
455
456 #[test]
457 fn ordinary_metadata_is_not_a_startup_marker() {
458 // Guard the other direction: real data must never be mistaken for a
459 // marker (it would be silently dropped instead of delivered).
460 assert!(!Metadata::new(test_timestamp()).is_startup_marker());
461
462 // Wrong type under the reserved key must not count as a marker.
463 let wrong_type = Metadata::from_parameters(
464 test_timestamp(),
465 BTreeMap::from([(
466 STARTUP_MARKER_PARAM.to_owned(),
467 Parameter::String("true".into()),
468 )]),
469 );
470 assert!(!wrong_type.is_startup_marker());
471
472 // Explicit `false` is not a marker either.
473 let explicit_false = Metadata::from_parameters(
474 test_timestamp(),
475 BTreeMap::from([(STARTUP_MARKER_PARAM.to_owned(), Parameter::Bool(false))]),
476 );
477 assert!(!explicit_false.is_startup_marker());
478 }
479
480 #[test]
481 fn startup_marker_key_is_reserved_and_distinct() {
482 // The `__dora_` prefix keeps it out of the user parameter namespace, and
483 // it must not collide with any well-known protocol key.
484 assert_eq!(STARTUP_MARKER_PARAM, "__dora_startup_marker");
485 assert!(STARTUP_MARKER_PARAM.starts_with("__dora_"));
486 for key in [
487 REQUEST_ID,
488 GOAL_ID,
489 GOAL_STATUS,
490 SESSION_ID,
491 SEGMENT_ID,
492 SEQ,
493 FIN,
494 FLUSH,
495 ] {
496 assert_ne!(STARTUP_MARKER_PARAM, key);
497 }
498 }
499
500 #[test]
501 fn startup_ack_round_trips_and_extracts_identity() {
502 // The ack travels as a postcard attachment on the `@ack` topic; the
503 // producer must recover exactly the (consumer, input) identity it needs
504 // to tick off a required acker.
505 let ack = Metadata::startup_ack(test_timestamp(), "camera-consumer", "image/depth");
506 assert_eq!(
507 ack.startup_ack_identity(),
508 Some(("camera-consumer", "image/depth"))
509 );
510 // An ack is not a marker (and vice versa, checked below): the two
511 // travel on different topics but share the filtering code path.
512 assert!(!ack.is_startup_marker());
513
514 let bytes = crate::encode(&ack).expect("serialize");
515 let decoded: Metadata = crate::decode(&bytes).expect("deserialize");
516 assert_eq!(
517 decoded.startup_ack_identity(),
518 Some(("camera-consumer", "image/depth"))
519 );
520 }
521
522 #[test]
523 fn malformed_startup_acks_are_rejected() {
524 // Ordinary metadata and markers are not acks.
525 assert_eq!(Metadata::new(test_timestamp()).startup_ack_identity(), None);
526 assert_eq!(
527 Metadata::startup_marker(test_timestamp()).startup_ack_identity(),
528 None
529 );
530
531 // Flag present but identity missing → not a valid ack: a producer must
532 // never count an acker it cannot identify.
533 let flag_only = Metadata::from_parameters(
534 test_timestamp(),
535 BTreeMap::from([(STARTUP_ACK_PARAM.to_owned(), Parameter::Bool(true))]),
536 );
537 assert_eq!(flag_only.startup_ack_identity(), None);
538
539 // Wrongly-typed identity parameters are rejected too.
540 let wrong_types = Metadata::from_parameters(
541 test_timestamp(),
542 BTreeMap::from([
543 (STARTUP_ACK_PARAM.to_owned(), Parameter::Bool(true)),
544 (STARTUP_ACK_CONSUMER_PARAM.to_owned(), Parameter::Integer(1)),
545 (STARTUP_ACK_INPUT_PARAM.to_owned(), Parameter::Integer(2)),
546 ]),
547 );
548 assert_eq!(wrong_types.startup_ack_identity(), None);
549
550 // `Bool(false)` under the flag key is not an ack.
551 let explicit_false = Metadata::from_parameters(
552 test_timestamp(),
553 BTreeMap::from([
554 (STARTUP_ACK_PARAM.to_owned(), Parameter::Bool(false)),
555 (
556 STARTUP_ACK_CONSUMER_PARAM.to_owned(),
557 Parameter::String("c".into()),
558 ),
559 (
560 STARTUP_ACK_INPUT_PARAM.to_owned(),
561 Parameter::String("i".into()),
562 ),
563 ]),
564 );
565 assert_eq!(explicit_false.startup_ack_identity(), None);
566 }
567
568 #[test]
569 fn startup_ack_keys_are_reserved_and_distinct() {
570 let ack_keys = [
571 STARTUP_ACK_PARAM,
572 STARTUP_ACK_CONSUMER_PARAM,
573 STARTUP_ACK_INPUT_PARAM,
574 ];
575 for key in ack_keys {
576 assert!(key.starts_with("__dora_"));
577 assert_ne!(key, STARTUP_MARKER_PARAM);
578 }
579 for (i, a) in ack_keys.iter().enumerate() {
580 for b in &ack_keys[i + 1..] {
581 assert_ne!(a, b);
582 }
583 }
584 }
585
586 #[test]
587 fn well_known_keys_have_stable_values() {
588 // These string values are part of the cross-language protocol
589 // (Python nodes use the same literal strings). Do not change.
590 assert_eq!(REQUEST_ID, "request_id");
591 assert_eq!(GOAL_ID, "goal_id");
592 assert_eq!(GOAL_STATUS, "goal_status");
593 assert_eq!(GOAL_STATUS_SUCCEEDED, "succeeded");
594 assert_eq!(GOAL_STATUS_ABORTED, "aborted");
595 assert_eq!(GOAL_STATUS_CANCELED, "canceled");
596 assert_eq!(SESSION_ID, "session_id");
597 assert_eq!(SEGMENT_ID, "segment_id");
598 assert_eq!(SEQ, "seq");
599 assert_eq!(FIN, "fin");
600 assert_eq!(FLUSH, "flush");
601 }
602
603 #[test]
604 fn well_known_keys_are_distinct() {
605 let keys = [
606 REQUEST_ID,
607 GOAL_ID,
608 GOAL_STATUS,
609 SESSION_ID,
610 SEGMENT_ID,
611 SEQ,
612 FIN,
613 FLUSH,
614 ];
615 for (i, a) in keys.iter().enumerate() {
616 for b in &keys[i + 1..] {
617 assert_ne!(a, b);
618 }
619 }
620 }
621
622 #[test]
623 fn goal_status_values_are_distinct() {
624 let vals = [
625 GOAL_STATUS_SUCCEEDED,
626 GOAL_STATUS_ABORTED,
627 GOAL_STATUS_CANCELED,
628 ];
629 for (i, a) in vals.iter().enumerate() {
630 for b in &vals[i + 1..] {
631 assert_ne!(a, b);
632 }
633 }
634 }
635
636 #[test]
637 fn outgoing_metadata_is_stamped_with_current_version() {
638 // The wire format is positional (postcard) and carries no separate type
639 // descriptor, so `metadata_version` is the only in-band signal of an
640 // incompatible layout. Every constructor must stamp `CURRENT_VERSION`.
641 assert_eq!(Metadata::CURRENT_VERSION, 2);
642 let ts = uhlc::HLC::default().new_timestamp();
643 assert_eq!(
644 Metadata::new(ts).metadata_version(),
645 Metadata::CURRENT_VERSION
646 );
647 assert_eq!(
648 Metadata::from_parameters(ts, Default::default()).metadata_version(),
649 Metadata::CURRENT_VERSION
650 );
651 }
652
653 #[test]
654 fn get_string_param_extracts_string() {
655 let mut params = MetadataParameters::default();
656 params.insert("key".to_string(), Parameter::String("value".to_string()));
657 assert_eq!(get_string_param(¶ms, "key"), Some("value"));
658 assert_eq!(get_string_param(¶ms, "missing"), None);
659 }
660
661 #[test]
662 fn get_string_param_returns_none_for_non_string() {
663 let mut params = MetadataParameters::default();
664 params.insert("num".to_string(), Parameter::Integer(42));
665 assert_eq!(get_string_param(¶ms, "num"), None);
666 }
667
668 #[test]
669 fn get_integer_param_extracts_integer() {
670 let mut params = MetadataParameters::default();
671 params.insert("key".to_string(), Parameter::Integer(42));
672 assert_eq!(get_integer_param(¶ms, "key"), Some(42));
673 assert_eq!(get_integer_param(¶ms, "missing"), None);
674 }
675
676 #[test]
677 fn get_integer_param_returns_none_for_non_integer() {
678 let mut params = MetadataParameters::default();
679 params.insert("s".to_string(), Parameter::String("hello".to_string()));
680 assert_eq!(get_integer_param(¶ms, "s"), None);
681 }
682
683 #[test]
684 fn get_bool_param_extracts_bool() {
685 let mut params = MetadataParameters::default();
686 params.insert("key".to_string(), Parameter::Bool(true));
687 assert_eq!(get_bool_param(¶ms, "key"), Some(true));
688 assert_eq!(get_bool_param(¶ms, "missing"), None);
689 }
690
691 #[test]
692 fn get_bool_param_returns_none_for_non_bool() {
693 let mut params = MetadataParameters::default();
694 params.insert("n".to_string(), Parameter::Integer(1));
695 assert_eq!(get_bool_param(¶ms, "n"), None);
696 }
697
698 #[test]
699 fn debug_frame_wire_size_prefers_stamped_value() {
700 // The stamped size wins over the (larger, schema-inflated) buffer: a
701 // schema-once frame's rebuilt `data` is bigger than what travelled.
702 let mut params = MetadataParameters::default();
703 params.insert(WIRE_SIZE.to_string(), Parameter::Integer(17));
704 assert_eq!(debug_frame_wire_size(¶ms, Some(&[0u8; 42])), 17);
705 }
706
707 #[test]
708 fn debug_frame_wire_size_falls_back_to_buffer_len() {
709 // No stamp (older daemon / non-debug frame) ⇒ use the buffer length.
710 let params = MetadataParameters::default();
711 assert_eq!(debug_frame_wire_size(¶ms, Some(&[0u8; 42])), 42);
712 }
713
714 #[test]
715 fn debug_frame_wire_size_falls_back_on_out_of_range_stamp() {
716 // A negative i64 can't be a byte count; fall back rather than count 0.
717 let mut params = MetadataParameters::default();
718 params.insert(WIRE_SIZE.to_string(), Parameter::Integer(-1));
719 assert_eq!(debug_frame_wire_size(¶ms, Some(&[0u8; 42])), 42);
720 }
721
722 #[test]
723 fn debug_frame_wire_size_zero_without_stamp_or_buffer() {
724 assert_eq!(
725 debug_frame_wire_size(&MetadataParameters::default(), None),
726 0
727 );
728 }
729}