weavegraph 0.3.0

Graph-driven, concurrent agent workflow framework with versioned state, deterministic barrier merges, and rich diagnostics.
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
//! ID generation utilities for run, step, node, and session identifiers.
//!
//! Provides utilities for generating unique, deterministic, and contextual IDs
//! throughout the Weavegraph framework. Supports both random UUID-based generation
//! and deterministic seeded generation for testing and reproducibility.

use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use thiserror::Error;
use uuid::Uuid;

/// Errors that can occur during ID generation.
#[derive(Debug, Error)]
#[cfg_attr(feature = "diagnostics", derive(miette::Diagnostic))]
pub enum IdError {
    /// Invalid format for ID parsing or validation.
    #[error("Invalid ID format: {format}")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(code(weavegraph::id::invalid_format))
    )]
    InvalidFormat { format: String },

    /// ID generation failed due to system constraints.
    #[error("ID generation failed: {reason}")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(code(weavegraph::id::generation_failed))
    )]
    GenerationFailed { reason: String },
}

/// Configuration for ID generation behavior.
#[derive(Debug, Clone, Default)]
pub struct IdConfig {
    /// Random seed for deterministic ID generation (optional).
    pub seed: Option<u64>,
    /// Prefix to use for all generated IDs.
    pub prefix: Option<String>,
    /// Whether to include timestamps in IDs.
    pub include_timestamp: bool,
    /// Counter for sequential ID generation.
    pub use_counter: bool,
}

impl fmt::Display for IdConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "IdConfig {{ seed: {:?}, prefix: {:?}, timestamp: {}, counter: {} }}",
            self.seed, self.prefix, self.include_timestamp, self.use_counter
        )
    }
}

/// High-performance ID generator with configurable behavior.
///
/// Supports multiple ID generation strategies including UUID-based random IDs,
/// deterministic seeded IDs, and sequential counter-based IDs. Thread-safe
/// and optimized for high-throughput scenarios.
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::id_generator::{IdGenerator, IdConfig};
///
/// // Default random ID generation
/// let generator = IdGenerator::new();
/// let id = generator.generate_run_id();
/// assert!(id.starts_with("run-"));
///
/// // Deterministic generation for testing
/// let config = IdConfig {
///     seed: Some(12345),
///     prefix: Some("test".into()),
///     ..Default::default()
/// };
/// let det_generator = IdGenerator::with_config(config);
/// let det_id = det_generator.generate_id();
/// ```
#[derive(Debug)]
pub struct IdGenerator {
    config: IdConfig,
    counter: AtomicU64,
}

impl IdGenerator {
    /// Create a new ID generator with default configuration.
    ///
    /// Uses random UUID generation without prefixes or deterministic behavior.
    ///
    /// # Returns
    /// A new IdGenerator instance with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            config: IdConfig::default(),
            counter: AtomicU64::new(0),
        }
    }

    /// Create an ID generator with custom configuration.
    ///
    /// # Parameters
    /// * `config` - Configuration specifying ID generation behavior
    ///
    /// # Returns
    /// A new IdGenerator instance with the specified configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::id_generator::{IdGenerator, IdConfig};
    ///
    /// let config = IdConfig {
    ///     seed: Some(42),
    ///     prefix: Some("weavegraph".into()),
    ///     include_timestamp: true,
    ///     use_counter: false,
    /// };
    /// let generator = IdGenerator::with_config(config);
    /// ```
    #[must_use]
    pub fn with_config(config: IdConfig) -> Self {
        Self {
            config,
            counter: AtomicU64::new(0),
        }
    }

    /// Generate a generic ID using the configured strategy.
    ///
    /// This is the core ID generation method that respects all configuration
    /// options including seeds, prefixes, timestamps, and counters.
    ///
    /// # Returns
    /// A newly generated ID string.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::id_generator::IdGenerator;
    ///
    /// let generator = IdGenerator::new();
    /// let id = generator.generate_id();
    /// assert!(!id.is_empty());
    /// ```
    #[must_use]
    pub fn generate_id(&self) -> String {
        let base_id = if let Some(seed) = self.config.seed {
            if self.config.use_counter {
                let counter = self.counter.fetch_add(1, Ordering::Relaxed);
                format!("seeded-{}-{}", seed, counter)
            } else {
                format!("seeded-{}", seed)
            }
        } else if self.config.use_counter {
            let counter = self.counter.fetch_add(1, Ordering::Relaxed);
            format!("counter-{}", counter)
        } else {
            self.generate_uuid()
        };

        let mut final_id = base_id;

        if self.config.include_timestamp {
            let timestamp = chrono::Utc::now().timestamp();
            final_id = format!("{}-t{}", final_id, timestamp);
        }

        if let Some(prefix) = &self.config.prefix {
            final_id = format!("{}-{}", prefix, final_id);
        }

        final_id
    }

    /// Generate a UUID v4 as a string.
    ///
    /// Provides direct access to UUID generation regardless of configuration.
    ///
    /// # Returns
    /// A new UUID v4 formatted as a string.
    #[must_use]
    pub fn generate_uuid(&self) -> String {
        Uuid::new_v4().to_string()
    }

    /// Generate an ID with a specific prefix.
    ///
    /// This method combines the configured generation strategy with a
    /// custom prefix, useful for creating typed IDs.
    ///
    /// # Parameters
    /// * `prefix` - Prefix to prepend to the generated ID
    ///
    /// # Returns
    /// A new ID with the specified prefix.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::id_generator::IdGenerator;
    ///
    /// let generator = IdGenerator::new();
    /// let session_id = generator.generate_id_with_prefix("session");
    /// assert!(session_id.starts_with("session-"));
    /// ```
    #[must_use]
    pub fn generate_id_with_prefix(&self, prefix: &str) -> String {
        format!("{}-{}", prefix, self.generate_base_id())
    }

    /// Generate a run ID for workflow execution tracking.
    ///
    /// Run IDs are used to track complete workflow executions from start to finish.
    ///
    /// # Returns
    /// A new run ID with "run" prefix.
    #[must_use]
    pub fn generate_run_id(&self) -> String {
        self.generate_id_with_prefix("run")
    }

    /// Generate a step ID for individual workflow steps.
    ///
    /// Step IDs track individual execution steps within a workflow run.
    ///
    /// # Returns
    /// A new step ID with "step" prefix.
    #[must_use]
    pub fn generate_step_id(&self) -> String {
        self.generate_id_with_prefix("step")
    }

    /// Generate a node ID for graph node instances.
    ///
    /// Node IDs identify specific instances of nodes in the execution graph.
    ///
    /// # Returns
    /// A new node ID with "node" prefix.
    #[must_use]
    pub fn generate_node_id(&self) -> String {
        self.generate_id_with_prefix("node")
    }

    /// Generate a session ID for checkpoint and persistence tracking.
    ///
    /// Session IDs are used for checkpoint management and state persistence.
    ///
    /// # Returns
    /// A new session ID with "session" prefix.
    #[must_use]
    pub fn generate_session_id(&self) -> String {
        self.generate_id_with_prefix("session")
    }

    /// Generate a random ID without any configuration-based modifications.
    ///
    /// This bypasses all configuration and always generates a random UUID.
    ///
    /// # Returns
    /// A random UUID string.
    #[must_use]
    pub fn generate_random_id(&self) -> String {
        self.generate_uuid()
    }

    /// Parse and validate an ID format.
    ///
    /// Checks if an ID follows expected patterns and extracts components.
    ///
    /// # Parameters
    /// * `id` - ID string to validate
    ///
    /// # Returns
    /// Ok(ParsedId) if valid, Err(IdError) if invalid.
    pub fn parse_id(&self, id: &str) -> Result<ParsedId, IdError> {
        if id.is_empty() {
            return Err(IdError::InvalidFormat {
                format: "empty string".into(),
            });
        }

        let parts: Vec<&str> = id.split('-').collect();
        if parts.len() < 2 {
            return Err(IdError::InvalidFormat {
                format: "missing separator".into(),
            });
        }

        Ok(ParsedId {
            prefix: parts[0].to_string(),
            base: parts[1..].join("-"),
            original: id.to_string(),
        })
    }

    /// Get the current counter value.
    ///
    /// Useful for testing and debugging sequential ID generation.
    ///
    /// # Returns
    /// Current counter value.
    #[must_use]
    pub fn current_counter(&self) -> u64 {
        self.counter.load(Ordering::Relaxed)
    }

    /// Reset the counter to zero.
    ///
    /// Useful for testing scenarios that require predictable counter values.
    pub fn reset_counter(&self) {
        self.counter.store(0, Ordering::Relaxed);
    }

    /// Private helper method for base ID generation
    fn generate_base_id(&self) -> String {
        if let Some(seed) = self.config.seed {
            if self.config.use_counter {
                let counter = self.counter.fetch_add(1, Ordering::Relaxed);
                format!("seeded-{}-{}", seed, counter)
            } else {
                format!("seeded-{}", seed)
            }
        } else if self.config.use_counter {
            let counter = self.counter.fetch_add(1, Ordering::Relaxed);
            format!("counter-{}", counter)
        } else {
            self.generate_uuid()
        }
    }
}

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

/// Parsed ID components for analysis and validation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedId {
    /// The prefix part of the ID (e.g., "run", "step", "node").
    pub prefix: String,
    /// The base identifier part after the prefix.
    pub base: String,
    /// The original complete ID string.
    pub original: String,
}

impl ParsedId {
    /// Check if this ID has a specific prefix.
    ///
    /// # Parameters
    /// * `expected_prefix` - Prefix to check for
    ///
    /// # Returns
    /// True if the ID has the expected prefix.
    #[must_use]
    pub fn has_prefix(&self, expected_prefix: &str) -> bool {
        self.prefix == expected_prefix
    }

    /// Extract timestamp from the ID if present.
    ///
    /// # Returns
    /// Timestamp if found and valid, None otherwise.
    #[must_use]
    pub fn extract_timestamp(&self) -> Option<i64> {
        if let Some(timestamp_part) = self.base.split('-').find(|part| part.starts_with('t')) {
            timestamp_part[1..].parse().ok()
        } else {
            None
        }
    }
}

/// Utility functions for common ID operations.
pub mod id_utils {
    use super::*;

    /// Create a deterministic ID generator for testing.
    ///
    /// # Parameters
    /// * `seed` - Seed value for deterministic generation
    ///
    /// # Returns
    /// IdGenerator configured for deterministic behavior.
    #[must_use]
    pub fn create_test_generator(seed: u64) -> IdGenerator {
        IdGenerator::with_config(IdConfig {
            seed: Some(seed),
            use_counter: true,
            ..Default::default()
        })
    }

    /// Create a production ID generator with timestamps.
    ///
    /// # Returns
    /// IdGenerator configured for production use with timestamps.
    #[must_use]
    pub fn create_production_generator() -> IdGenerator {
        IdGenerator::with_config(IdConfig {
            include_timestamp: true,
            ..Default::default()
        })
    }

    /// Validate that an ID follows expected patterns.
    ///
    /// # Parameters
    /// * `id` - ID to validate
    /// * `expected_prefix` - Expected prefix (optional)
    ///
    /// # Returns
    /// True if the ID is valid.
    pub fn is_valid_id(id: &str, expected_prefix: Option<&str>) -> bool {
        if id.is_empty() {
            return false;
        }

        let parts: Vec<&str> = id.split('-').collect();
        if parts.len() < 2 {
            return false;
        }

        if let Some(prefix) = expected_prefix {
            parts[0] == prefix
        } else {
            true
        }
    }

    /// Extract the type of an ID from its prefix.
    ///
    /// # Parameters
    /// * `id` - ID to analyze
    ///
    /// # Returns
    /// ID type based on prefix, or "unknown" if unrecognized.
    #[must_use]
    pub fn get_id_type(id: &str) -> String {
        id.split('-')
            .next()
            .map(|s| s.to_string())
            .unwrap_or_else(|| "unknown".to_string())
    }
}