xerv-core 0.1.0

Workflow orchestration core: memory-mapped arena, write-ahead log, traits, and type system
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
//! Log event types for trace execution logging.
//!
//! Provides structured log events with correlation IDs (trace_id, node_id)
//! for debugging and observability of flow executions.

use crate::types::{NodeId, TraceId};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

/// Log severity level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
    /// Fine-grained debugging information.
    Trace,
    /// Debugging information.
    Debug,
    /// Informational messages.
    Info,
    /// Warning messages.
    Warn,
    /// Error messages.
    Error,
}

impl LogLevel {
    /// Parse a log level from a string.
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "trace" => Some(Self::Trace),
            "debug" => Some(Self::Debug),
            "info" => Some(Self::Info),
            "warn" | "warning" => Some(Self::Warn),
            "error" => Some(Self::Error),
            _ => None,
        }
    }

    /// Get the string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Trace => "trace",
            Self::Debug => "debug",
            Self::Info => "info",
            Self::Warn => "warn",
            Self::Error => "error",
        }
    }
}

impl Default for LogLevel {
    fn default() -> Self {
        Self::Info
    }
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl std::str::FromStr for LogLevel {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s).ok_or("invalid log level")
    }
}

/// Category of log event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LogCategory {
    /// Trace lifecycle events (start, complete, fail).
    Trace,
    /// Node execution events (start, complete, error).
    Node,
    /// Trigger events (fire, pause, resume).
    Trigger,
    /// Pipeline events (deploy, start, stop).
    Pipeline,
    /// Schema/data validation events.
    Schema,
    /// System/internal events.
    System,
    /// User-defined custom events.
    Custom,
}

impl LogCategory {
    /// Get the string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Trace => "trace",
            Self::Node => "node",
            Self::Trigger => "trigger",
            Self::Pipeline => "pipeline",
            Self::Schema => "schema",
            Self::System => "system",
            Self::Custom => "custom",
        }
    }
}

impl fmt::Display for LogCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// A structured log event with correlation IDs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEvent {
    /// Unique event ID.
    pub id: u64,
    /// Timestamp in nanoseconds since UNIX epoch.
    pub timestamp_ns: u64,
    /// Log severity level.
    pub level: LogLevel,
    /// Event category.
    pub category: LogCategory,
    /// Associated trace ID (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trace_id: Option<TraceId>,
    /// Associated node ID (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<NodeId>,
    /// Associated pipeline ID (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pipeline_id: Option<String>,
    /// Human-readable message.
    pub message: String,
    /// Structured fields for additional context.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub fields: HashMap<String, serde_json::Value>,
}

impl LogEvent {
    /// Create a new log event with the current timestamp.
    pub fn new(level: LogLevel, category: LogCategory, message: impl Into<String>) -> Self {
        Self {
            id: 0, // Will be assigned by collector
            timestamp_ns: current_timestamp_ns(),
            level,
            category,
            trace_id: None,
            node_id: None,
            pipeline_id: None,
            message: message.into(),
            fields: HashMap::new(),
        }
    }

    /// Create a trace-level log event.
    pub fn trace(category: LogCategory, message: impl Into<String>) -> Self {
        Self::new(LogLevel::Trace, category, message)
    }

    /// Create a debug-level log event.
    pub fn debug(category: LogCategory, message: impl Into<String>) -> Self {
        Self::new(LogLevel::Debug, category, message)
    }

    /// Create an info-level log event.
    pub fn info(category: LogCategory, message: impl Into<String>) -> Self {
        Self::new(LogLevel::Info, category, message)
    }

    /// Create a warn-level log event.
    pub fn warn(category: LogCategory, message: impl Into<String>) -> Self {
        Self::new(LogLevel::Warn, category, message)
    }

    /// Create an error-level log event.
    pub fn error(category: LogCategory, message: impl Into<String>) -> Self {
        Self::new(LogLevel::Error, category, message)
    }

    /// Set the trace ID.
    pub fn with_trace_id(mut self, trace_id: TraceId) -> Self {
        self.trace_id = Some(trace_id);
        self
    }

    /// Set the node ID.
    pub fn with_node_id(mut self, node_id: NodeId) -> Self {
        self.node_id = Some(node_id);
        self
    }

    /// Set the pipeline ID.
    pub fn with_pipeline_id(mut self, pipeline_id: impl Into<String>) -> Self {
        self.pipeline_id = Some(pipeline_id.into());
        self
    }

    /// Add a string field.
    pub fn with_field(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.fields
            .insert(key.into(), serde_json::Value::String(value.into()));
        self
    }

    /// Add a numeric field.
    pub fn with_field_i64(mut self, key: impl Into<String>, value: i64) -> Self {
        self.fields
            .insert(key.into(), serde_json::Value::Number(value.into()));
        self
    }

    /// Add a boolean field.
    pub fn with_field_bool(mut self, key: impl Into<String>, value: bool) -> Self {
        self.fields
            .insert(key.into(), serde_json::Value::Bool(value));
        self
    }

    /// Add a JSON value field.
    pub fn with_field_json(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.fields.insert(key.into(), value);
        self
    }

    /// Get the timestamp as a DateTime string (ISO 8601).
    pub fn timestamp_iso(&self) -> String {
        let secs = self.timestamp_ns / 1_000_000_000;
        let nanos = (self.timestamp_ns % 1_000_000_000) as u32;

        if let Some(datetime) = chrono::DateTime::from_timestamp(secs as i64, nanos) {
            datetime.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string()
        } else {
            format!("{}ns", self.timestamp_ns)
        }
    }

    /// Format as a single log line.
    pub fn format_line(&self) -> String {
        let mut parts = vec![
            self.timestamp_iso(),
            format!("[{}]", self.level.as_str().to_uppercase()),
            format!("[{}]", self.category.as_str()),
        ];

        if let Some(ref trace_id) = self.trace_id {
            parts.push(format!("trace={}", trace_id));
        }

        if let Some(node_id) = self.node_id {
            parts.push(format!("node={}", node_id.as_u32()));
        }

        if let Some(ref pipeline_id) = self.pipeline_id {
            parts.push(format!("pipeline={}", pipeline_id));
        }

        parts.push(self.message.clone());

        if !self.fields.is_empty() {
            let fields_str: Vec<String> = self
                .fields
                .iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect();
            parts.push(format!("{{{}}}", fields_str.join(", ")));
        }

        parts.join(" ")
    }
}

/// Get current timestamp in nanoseconds since UNIX epoch.
fn current_timestamp_ns() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0)
}

/// Builder for creating log events with common context.
#[derive(Debug, Clone)]
pub struct LogEventBuilder {
    trace_id: Option<TraceId>,
    node_id: Option<NodeId>,
    pipeline_id: Option<String>,
}

impl LogEventBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            trace_id: None,
            node_id: None,
            pipeline_id: None,
        }
    }

    /// Set the trace ID for all events.
    pub fn with_trace_id(mut self, trace_id: TraceId) -> Self {
        self.trace_id = Some(trace_id);
        self
    }

    /// Set the node ID for all events.
    pub fn with_node_id(mut self, node_id: NodeId) -> Self {
        self.node_id = Some(node_id);
        self
    }

    /// Set the pipeline ID for all events.
    pub fn with_pipeline_id(mut self, pipeline_id: impl Into<String>) -> Self {
        self.pipeline_id = Some(pipeline_id.into());
        self
    }

    /// Create a log event with the builder's context.
    pub fn event(
        &self,
        level: LogLevel,
        category: LogCategory,
        message: impl Into<String>,
    ) -> LogEvent {
        let mut event = LogEvent::new(level, category, message);
        if let Some(trace_id) = self.trace_id {
            event.trace_id = Some(trace_id);
        }
        if let Some(node_id) = self.node_id {
            event.node_id = Some(node_id);
        }
        if let Some(ref pipeline_id) = self.pipeline_id {
            event.pipeline_id = Some(pipeline_id.clone());
        }
        event
    }

    /// Create a trace-level event.
    pub fn trace(&self, category: LogCategory, message: impl Into<String>) -> LogEvent {
        self.event(LogLevel::Trace, category, message)
    }

    /// Create a debug-level event.
    pub fn debug(&self, category: LogCategory, message: impl Into<String>) -> LogEvent {
        self.event(LogLevel::Debug, category, message)
    }

    /// Create an info-level event.
    pub fn info(&self, category: LogCategory, message: impl Into<String>) -> LogEvent {
        self.event(LogLevel::Info, category, message)
    }

    /// Create a warn-level event.
    pub fn warn(&self, category: LogCategory, message: impl Into<String>) -> LogEvent {
        self.event(LogLevel::Warn, category, message)
    }

    /// Create an error-level event.
    pub fn error(&self, category: LogCategory, message: impl Into<String>) -> LogEvent {
        self.event(LogLevel::Error, category, message)
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn log_level_parsing() {
        assert_eq!(LogLevel::parse("trace"), Some(LogLevel::Trace));
        assert_eq!(LogLevel::parse("DEBUG"), Some(LogLevel::Debug));
        assert_eq!(LogLevel::parse("Info"), Some(LogLevel::Info));
        assert_eq!(LogLevel::parse("WARN"), Some(LogLevel::Warn));
        assert_eq!(LogLevel::parse("warning"), Some(LogLevel::Warn));
        assert_eq!(LogLevel::parse("error"), Some(LogLevel::Error));
        assert_eq!(LogLevel::parse("invalid"), None);
    }

    #[test]
    fn log_level_ordering() {
        assert!(LogLevel::Trace < LogLevel::Debug);
        assert!(LogLevel::Debug < LogLevel::Info);
        assert!(LogLevel::Info < LogLevel::Warn);
        assert!(LogLevel::Warn < LogLevel::Error);
    }

    #[test]
    fn log_event_creation() {
        let event = LogEvent::info(LogCategory::Node, "Node started")
            .with_trace_id(TraceId::new())
            .with_node_id(NodeId::new(42))
            .with_field("duration_ms", "123");

        assert_eq!(event.level, LogLevel::Info);
        assert_eq!(event.category, LogCategory::Node);
        assert_eq!(event.message, "Node started");
        assert!(event.trace_id.is_some());
        assert_eq!(event.node_id, Some(NodeId::new(42)));
        assert!(event.fields.contains_key("duration_ms"));
    }

    #[test]
    fn log_event_builder() {
        let trace_id = TraceId::new();
        let builder = LogEventBuilder::new()
            .with_trace_id(trace_id)
            .with_pipeline_id("test_pipeline");

        let event = builder.info(LogCategory::Trace, "Trace started");

        assert_eq!(event.trace_id, Some(trace_id));
        assert_eq!(event.pipeline_id, Some("test_pipeline".to_string()));
        assert_eq!(event.level, LogLevel::Info);
    }

    #[test]
    fn log_event_format_line() {
        let event = LogEvent::info(LogCategory::Node, "Processing order")
            .with_pipeline_id("order_pipeline")
            .with_field("order_id", "ORD-123");

        let line = event.format_line();
        assert!(line.contains("[INFO]"));
        assert!(line.contains("[node]"));
        assert!(line.contains("pipeline=order_pipeline"));
        assert!(line.contains("Processing order"));
        assert!(line.contains("order_id"));
    }

    #[test]
    fn log_event_serialization() {
        let event = LogEvent::error(LogCategory::System, "Connection failed")
            .with_field("host", "localhost")
            .with_field_i64("port", 8080);

        let json = serde_json::to_string(&event).unwrap();
        let parsed: LogEvent = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.level, LogLevel::Error);
        assert_eq!(parsed.category, LogCategory::System);
        assert_eq!(parsed.message, "Connection failed");
        assert_eq!(parsed.fields.len(), 2);
    }
}