rustvello-core 0.1.5

Core traits and types for the Rustvello distributed task library
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
use rustvello_proto::identifiers::{InvocationId, TaskId};
use rustvello_proto::status::{InvocationStatus, StatusMachineError};
use std::fmt;

/// Classification of infrastructure failures.
///
/// Used to distinguish retriable (Connection, Timeout) from non-retriable
/// (Query, DataCorruption) infrastructure errors without string parsing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum InfraErrorKind {
    /// Network or pool connection failure (retriable).
    Connection,
    /// Operation timed out (retriable).
    Timeout,
    /// Query or command failed (not retriable).
    Query,
    /// Stored data is corrupt or unparsable.
    DataCorruption,
    /// Uncategorised infrastructure error.
    Other,
}

impl fmt::Display for InfraErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Connection => write!(f, "connection"),
            Self::Timeout => write!(f, "timeout"),
            Self::Query => write!(f, "query"),
            Self::DataCorruption => write!(f, "data_corruption"),
            Self::Other => write!(f, "other"),
        }
    }
}

impl InfraErrorKind {
    /// Returns `true` for errors that are typically transient and worth retrying.
    pub fn is_retriable(self) -> bool {
        matches!(self, Self::Connection | Self::Timeout)
    }
}

/// Root error type — mirrors pynenc's exception hierarchy.
///
/// Each variant maps 1:1 to a pynenc exception class (see 302 §4).
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RustvelloError {
    // --- Retry errors (mirror RetryError) ---
    /// Task explicitly requested a retry
    #[error("retry requested: {reason}")]
    Retry { reason: String },

    /// Concurrency control triggered a retry
    #[error("concurrency retry: task {task_id} — {reason}")]
    ConcurrencyRetry { task_id: TaskId, reason: String },

    // --- Serialization (mirror SerializationError) ---
    /// Serialization/deserialization error
    #[error("serialization error: {message}")]
    Serialization { message: String },

    // --- Task errors (mirror TaskError hierarchy) ---
    /// Task definition not found in any registry
    #[error("task not found: {task_id}")]
    TaskNotFound { task_id: TaskId },

    /// Task exists but is not registered in this app instance
    #[error("task not registered: {task_id}")]
    TaskNotRegistered { task_id: TaskId },

    /// Dependency cycle detected in task graph
    #[error("cycle detected: {task_id} — {message}")]
    CycleDetected { task_id: TaskId, message: String },

    /// Runner cannot execute this task type
    #[error("runner not executable: {task_id} — {message}")]
    RunnerNotExecutable { task_id: TaskId, message: String },

    /// Task class/type not found during deserialization
    #[error("task class not found: {task_id}")]
    TaskClassNotFound { task_id: TaskId },

    // --- Invocation errors (mirror InvocationError hierarchy) ---
    /// Invocation not found
    #[error("invocation not found: {invocation_id}")]
    InvocationNotFound { invocation_id: InvocationId },

    /// Invalid status transition attempted
    #[error("invalid status transition: {from_status} -> {to_status}")]
    InvalidStatusTransition {
        invocation_id: InvocationId,
        from_status: InvocationStatus,
        to_status: InvocationStatus,
        allowed_statuses: Vec<InvocationStatus>,
    },

    /// Runner ownership violation during status transition
    #[error("ownership violation: {from_status} -> {to_status}, owner={current_owner}, requester={attempted_owner}")]
    OwnershipViolation {
        invocation_id: InvocationId,
        from_status: InvocationStatus,
        to_status: InvocationStatus,
        current_owner: String,
        attempted_owner: String,
        reason: String,
    },

    /// Status changed between read and write (optimistic locking failure)
    #[error("status race condition on {invocation_id}")]
    StatusRaceCondition {
        invocation_id: InvocationId,
        previous_status: InvocationStatus,
        expected_status: InvocationStatus,
        actual_status: InvocationStatus,
    },

    // --- Task execution errors ---
    /// Task function raised an error during execution.
    /// Carries the original exception type name for retry matching.
    #[error("task execution error ({error_type}): {message}")]
    TaskExecution {
        error_type: String,
        message: String,
        traceback: Option<String>,
    },

    // --- Infrastructure errors ---
    /// Unified infrastructure error with structured classification.
    #[error("infrastructure error ({kind}): {message}")]
    Infrastructure {
        kind: InfraErrorKind,
        message: String,
        #[source]
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
    },

    // --- Config errors ---
    /// Configuration error
    #[error("configuration error: {message}")]
    Configuration { message: String },

    // --- Internal (no pynenc equivalent — for Rust-only bugs) ---
    /// Generic internal error
    #[error("internal error: {message}")]
    Internal { message: String },

    /// Backend does not support this operation.
    #[error("{backend} backend does not support {method}")]
    NotSupported { backend: String, method: String },
}

impl RustvelloError {
    /// State backend query/storage error (not retriable).
    pub fn state_backend(message: impl Into<String>) -> Self {
        Self::Infrastructure {
            kind: InfraErrorKind::Query,
            message: message.into(),
            source: None,
        }
    }

    /// Broker messaging error (not retriable).
    pub fn broker_err(message: impl Into<String>) -> Self {
        Self::Infrastructure {
            kind: InfraErrorKind::Query,
            message: message.into(),
            source: None,
        }
    }

    /// Runner infrastructure error.
    pub fn runner_err(message: impl Into<String>) -> Self {
        Self::Infrastructure {
            kind: InfraErrorKind::Other,
            message: message.into(),
            source: None,
        }
    }

    /// Connection failure (retriable).
    pub fn connection_err(message: impl Into<String>) -> Self {
        Self::Infrastructure {
            kind: InfraErrorKind::Connection,
            message: message.into(),
            source: None,
        }
    }
}

/// Convenience type alias for Results in Rustvello.
pub type RustvelloResult<T> = Result<T, RustvelloError>;

/// Convert a mutex lock-poison error into [`RustvelloError::Internal`].
///
/// Use this in backend implementations to avoid duplicating lock-error
/// conversion logic.
pub fn lock_err(e: impl std::fmt::Display) -> RustvelloError {
    RustvelloError::Internal {
        message: format!("lock poisoned: {}", e),
    }
}

/// Error information stored with a failed invocation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TaskError {
    pub error_type: String,
    pub message: String,
    pub traceback: Option<String>,
}

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

/// Convert a [`StatusMachineError`] into a [`RustvelloError`].
///
/// `fallback_from_status` is used when the transition error's `from` field
/// is `None` (i.e. the current status was not captured in the error itself).
pub fn status_machine_error_to_rustvello(
    e: StatusMachineError,
    invocation_id: &InvocationId,
    fallback_from_status: InvocationStatus,
) -> RustvelloError {
    match e {
        StatusMachineError::Transition(t) => RustvelloError::InvalidStatusTransition {
            invocation_id: invocation_id.clone(),
            from_status: t.from.unwrap_or(fallback_from_status),
            to_status: t.to,
            allowed_statuses: t.allowed,
        },
        StatusMachineError::Ownership(o) => RustvelloError::OwnershipViolation {
            invocation_id: invocation_id.clone(),
            from_status: o.from_status,
            to_status: o.to_status,
            current_owner: o.current_owner.unwrap_or_default(),
            attempted_owner: o.attempted_owner.unwrap_or_default(),
            reason: o.reason,
        },
        _ => RustvelloError::Internal {
            message: format!("unexpected status machine error: {e}"),
        },
    }
}

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

    #[test]
    fn error_display_messages() {
        let e = RustvelloError::InvalidStatusTransition {
            invocation_id: InvocationId::from_string("inv-1"),
            from_status: InvocationStatus::Registered,
            to_status: InvocationStatus::Running,
            allowed_statuses: vec![InvocationStatus::Pending],
        };
        assert!(e.to_string().contains("REGISTERED"));
        assert!(e.to_string().contains("RUNNING"));

        let e = RustvelloError::InvocationNotFound {
            invocation_id: InvocationId::from_string("inv-1"),
        };
        assert!(e.to_string().contains("inv-1"));

        let e = RustvelloError::TaskNotRegistered {
            task_id: TaskId::new("mod", "func"),
        };
        assert!(e.to_string().contains("mod.func"));

        let e = RustvelloError::Serialization {
            message: "bad json".to_string(),
        };
        assert!(e.to_string().contains("bad json"));

        let e = RustvelloError::state_backend("disk full".to_string());
        assert!(e.to_string().contains("disk full"));

        let e = RustvelloError::Configuration {
            message: "bad config".to_string(),
        };
        assert!(e.to_string().contains("bad config"));

        let e = RustvelloError::broker_err("queue full".to_string());
        assert!(e.to_string().contains("queue full"));

        let e = RustvelloError::runner_err("timeout waiting for invocation inv-2".to_string());
        assert!(e.to_string().contains("inv-2"));

        let e = RustvelloError::Internal {
            message: "unexpected".to_string(),
        };
        assert!(e.to_string().contains("unexpected"));
    }

    #[test]
    fn task_error_display() {
        let te = TaskError {
            error_type: "ValueError".to_string(),
            message: "negative number".to_string(),
            traceback: Some("line 1".to_string()),
        };
        assert_eq!(te.to_string(), "ValueError: negative number");
    }

    #[test]
    fn task_error_serde() {
        let te = TaskError {
            error_type: "RuntimeError".to_string(),
            message: "oops".to_string(),
            traceback: None,
        };
        let json = serde_json::to_string(&te).unwrap();
        let back: TaskError = serde_json::from_str(&json).unwrap();
        assert_eq!(back.error_type, "RuntimeError");
        assert_eq!(back.message, "oops");
        assert!(back.traceback.is_none());
    }

    #[test]
    fn ownership_violation_display() {
        let e = RustvelloError::OwnershipViolation {
            invocation_id: InvocationId::from_string("inv-1"),
            from_status: InvocationStatus::Running,
            to_status: InvocationStatus::Success,
            current_owner: "runner-a".to_string(),
            attempted_owner: "runner-b".to_string(),
            reason: "status requires ownership".to_string(),
        };
        let s = e.to_string();
        assert!(s.contains("runner-a"));
        assert!(s.contains("runner-b"));
    }

    #[test]
    fn retry_display() {
        let e = RustvelloError::Retry {
            reason: "transient network error".to_string(),
        };
        let s = e.to_string();
        assert!(s.contains("retry"));
        assert!(s.contains("transient network error"));
    }

    #[test]
    fn concurrency_retry_display() {
        let e = RustvelloError::ConcurrencyRetry {
            task_id: TaskId::new("mod", "my_task"),
            reason: "task-level lock held".to_string(),
        };
        let s = e.to_string();
        assert!(s.contains("mod.my_task"));
        assert!(s.contains("task-level lock held"));
    }

    #[test]
    fn lock_err_converts_to_internal() {
        let e = lock_err("PoisonError { .. }");
        match e {
            RustvelloError::Internal { message } => assert!(message.contains("lock poisoned")),
            other => panic!("expected Internal, got {other:?}"),
        }
    }

    #[test]
    fn status_race_condition_display() {
        let e = RustvelloError::StatusRaceCondition {
            invocation_id: InvocationId::from_string("inv-1"),
            previous_status: InvocationStatus::Pending,
            expected_status: InvocationStatus::Running,
            actual_status: InvocationStatus::Failed,
        };
        let s = e.to_string();
        assert!(s.contains("inv-1"));
    }

    #[test]
    fn runner_error_display() {
        let e = RustvelloError::runner_err("process exited with code 1".to_string());
        assert!(e.to_string().contains("process exited"));
    }

    #[test]
    fn task_error_variants_display() {
        let tid = TaskId::new("mymod", "myfunc");

        let e = RustvelloError::TaskNotFound {
            task_id: tid.clone(),
        };
        assert!(e.to_string().contains("mymod.myfunc"));

        let e = RustvelloError::CycleDetected {
            task_id: tid.clone(),
            message: "A -> B -> A".to_string(),
        };
        assert!(e.to_string().contains("cycle"));

        let e = RustvelloError::TaskClassNotFound {
            task_id: tid.clone(),
        };
        assert!(e.to_string().contains("class not found"));
    }
}