riglr-core 0.3.0

Core abstractions and job execution engine for riglr - building resilient AI agents.
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
//! Error types for riglr-core.

use serde::Serialize;
use thiserror::Error;

/// Helper function to serialize Duration as seconds for JSON compatibility
fn serialize_duration_as_secs<S>(
    duration: &Option<std::time::Duration>,
    serializer: S,
) -> std::result::Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    match duration {
        Some(d) => serializer.serialize_some(&d.as_secs()),
        None => serializer.serialize_none(),
    }
}

/// Main error type for riglr-core operations.
#[derive(Error, Debug, Serialize)]
pub enum CoreError {
    /// Queue operation failed
    #[error("Queue error: {0}")]
    Queue(String),

    /// Job execution failed
    #[error("Job execution error: {0}")]
    JobExecution(String),

    /// Serialization/deserialization failed
    #[error("Serialization error: {0}")]
    #[serde(skip)]
    Serialization(#[from] serde_json::Error),

    /// Redis connection error (only available with redis feature)
    #[cfg(feature = "redis")]
    #[error("Redis error: {0}")]
    #[serde(skip)]
    Redis(#[from] redis::RedisError),

    /// Generic error
    #[error("Core error: {0}")]
    Generic(String),

    /// Invalid input provided
    #[error("Invalid input: {0}")]
    InvalidInput(String),
}

/// Result type alias for riglr-core operations.
pub type Result<T> = std::result::Result<T, CoreError>;

/// Worker-specific error type for distinguishing system-level worker failures
/// from tool execution failures.
#[derive(Error, Debug, Serialize)]
pub enum WorkerError {
    /// Tool not found in the worker's registry
    #[error("Tool '{tool_name}' not found in worker registry")]
    ToolNotFound {
        /// Name of the tool that was not found
        tool_name: String,
    },

    /// Failed to acquire semaphore for concurrency control
    #[error("Failed to acquire semaphore for tool '{tool_name}': {source_message}")]
    SemaphoreAcquisition {
        /// Name of the tool for which semaphore acquisition failed
        tool_name: String,
        /// The error message that caused the semaphore acquisition failure
        source_message: String,
    },

    /// Idempotency store operation failed
    #[error("Idempotency store operation failed: {source_message}")]
    IdempotencyStore {
        /// The error message that caused the idempotency store operation to fail
        source_message: String,
    },

    /// Job serialization/deserialization error
    #[error("Job serialization error: {source_message}")]
    JobSerialization {
        /// The JSON serialization error message
        source_message: String,
    },

    /// Tool execution exceeded configured timeout
    #[error("Tool execution timed out after {timeout:?}")]
    ExecutionTimeout {
        /// The duration after which the execution timed out
        timeout: std::time::Duration,
    },

    /// Internal worker system error
    #[error("Internal worker error: {message}")]
    Internal {
        /// Human-readable description of the internal error
        message: String,
    },
}

impl From<&str> for CoreError {
    fn from(err: &str) -> Self {
        Self::Generic(err.to_string())
    }
}

/// Tool-specific error type for distinguishing retriable vs permanent failures.
#[derive(Debug, Clone, Serialize, serde::Deserialize)]
pub enum ToolError {
    /// Operation can be retried
    Retriable {
        /// The typed error source (not serialized)
        #[serde(skip)]
        source: Option<std::sync::Arc<dyn std::error::Error + Send + Sync>>,
        /// The error message for serialization
        source_message: String,
        /// Additional context about the error
        context: String,
    },

    /// Rate limited, retry after delay
    RateLimited {
        /// The typed error source (not serialized)
        #[serde(skip)]
        source: Option<std::sync::Arc<dyn std::error::Error + Send + Sync>>,
        /// The error message for serialization
        source_message: String,
        /// Additional context about the rate limiting
        context: String,
        /// Optional duration to wait before retrying (in seconds)
        #[serde(serialize_with = "serialize_duration_as_secs")]
        retry_after: Option<std::time::Duration>,
    },

    /// Permanent error, do not retry
    Permanent {
        /// The typed error source (not serialized)
        #[serde(skip)]
        source: Option<std::sync::Arc<dyn std::error::Error + Send + Sync>>,
        /// The error message for serialization
        source_message: String,
        /// Additional context about the permanent error
        context: String,
    },

    /// Invalid input provided
    InvalidInput {
        /// The typed error source (not serialized)
        #[serde(skip)]
        source: Option<std::sync::Arc<dyn std::error::Error + Send + Sync>>,
        /// The error message for serialization
        source_message: String,
        /// Description of what input was invalid
        context: String,
    },

    /// Signer context error
    SignerContext(String),
}

impl PartialEq for ToolError {
    fn eq(&self, other: &Self) -> bool {
        use ToolError::*;
        match (self, other) {
            (
                Retriable {
                    source_message: s1,
                    context: c1,
                    ..
                },
                Retriable {
                    source_message: s2,
                    context: c2,
                    ..
                },
            ) => s1 == s2 && c1 == c2,
            (
                RateLimited {
                    source_message: s1,
                    context: c1,
                    retry_after: r1,
                    ..
                },
                RateLimited {
                    source_message: s2,
                    context: c2,
                    retry_after: r2,
                    ..
                },
            ) => s1 == s2 && c1 == c2 && r1 == r2,
            (
                Permanent {
                    source_message: s1,
                    context: c1,
                    ..
                },
                Permanent {
                    source_message: s2,
                    context: c2,
                    ..
                },
            ) => s1 == s2 && c1 == c2,
            (
                InvalidInput {
                    source_message: s1,
                    context: c1,
                    ..
                },
                InvalidInput {
                    source_message: s2,
                    context: c2,
                    ..
                },
            ) => s1 == s2 && c1 == c2,
            (SignerContext(s1), SignerContext(s2)) => s1 == s2,
            _ => false,
        }
    }
}

impl ToolError {
    /// Creates a retriable error with context and source preservation
    pub fn retriable_with_source<E: std::error::Error + Send + Sync + 'static>(
        source: E,
        context: impl Into<String>,
    ) -> Self {
        let source_message = source.to_string();
        Self::Retriable {
            source: Some(std::sync::Arc::new(source)),
            source_message,
            context: context.into(),
        }
    }

    /// Creates a permanent error with context and source preservation
    pub fn permanent_with_source<E: std::error::Error + Send + Sync + 'static>(
        source: E,
        context: impl Into<String>,
    ) -> Self {
        let source_message = source.to_string();
        Self::Permanent {
            source: Some(std::sync::Arc::new(source)),
            source_message,
            context: context.into(),
        }
    }

    /// Creates a rate limited error with optional retry duration
    pub fn rate_limited_with_source<E: std::error::Error + Send + Sync + 'static>(
        source: E,
        context: impl Into<String>,
        retry_after: Option<std::time::Duration>,
    ) -> Self {
        let source_message = source.to_string();
        Self::RateLimited {
            source: Some(std::sync::Arc::new(source)),
            source_message,
            context: context.into(),
            retry_after,
        }
    }

    /// Creates an invalid input error
    pub fn invalid_input_with_source<E: std::error::Error + Send + Sync + 'static>(
        source: E,
        context: impl Into<String>,
    ) -> Self {
        let source_message = source.to_string();
        Self::InvalidInput {
            source: Some(std::sync::Arc::new(source)),
            source_message,
            context: context.into(),
        }
    }

    /// Returns whether this error is retriable
    #[must_use]
    pub const fn is_retriable(&self) -> bool {
        matches!(self, Self::Retriable { .. } | Self::RateLimited { .. })
    }

    /// Returns the retry delay if this is a rate limited error
    #[must_use]
    pub const fn retry_after(&self) -> Option<std::time::Duration> {
        match self {
            Self::RateLimited { retry_after, .. } => *retry_after,
            _ => None,
        }
    }

    /// Checks if the error is rate limited (for compatibility)
    #[must_use]
    pub const fn is_rate_limited(&self) -> bool {
        matches!(self, Self::RateLimited { .. })
    }

    /// Creates a retriable error from a string message
    pub fn retriable_string<S: Into<String>>(msg: S) -> Self {
        let msg = msg.into();
        Self::Retriable {
            source: None,
            source_message: msg.clone(),
            context: msg,
        }
    }

    /// Creates a permanent error from a string message
    pub fn permanent_string<S: Into<String>>(msg: S) -> Self {
        let msg = msg.into();
        Self::Permanent {
            source: None,
            source_message: msg.clone(),
            context: msg,
        }
    }

    /// Creates a rate limited error from a string message
    pub fn rate_limited_string<S: Into<String>>(msg: S) -> Self {
        let msg = msg.into();
        Self::RateLimited {
            source: None,
            source_message: msg.clone(),
            context: msg,
            retry_after: None,
        }
    }

    /// Creates an invalid input error from a string message
    pub fn invalid_input_string<S: Into<String>>(msg: S) -> Self {
        let msg = msg.into();
        Self::InvalidInput {
            source: None,
            source_message: msg.clone(),
            context: msg,
        }
    }

    /// Check if the error contains a specific substring in its string representation
    pub fn contains(&self, needle: &str) -> bool {
        self.to_string().contains(needle)
    }

    /// Creates a retriable error from any error type.
    pub fn retriable_from_error<E: std::error::Error + Send + Sync + 'static>(
        source: E,
        context: impl Into<String>,
    ) -> Self {
        Self::retriable_with_source(source, context)
    }

    /// Creates a permanent error from any error type.
    pub fn permanent_from_error<E: std::error::Error + Send + Sync + 'static>(
        source: E,
        context: impl Into<String>,
    ) -> Self {
        Self::permanent_with_source(source, context)
    }
}

impl std::fmt::Display for ToolError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Retriable {
                context,
                source_message,
                ..
            } => {
                write!(
                    f,
                    "Operation can be retried: {} - {}",
                    context, source_message
                )
            }
            Self::RateLimited {
                context,
                source_message,
                ..
            } => {
                write!(f, "Rate limited: {} - {}", context, source_message)
            }
            Self::Permanent {
                context,
                source_message,
                ..
            } => {
                write!(f, "Permanent error: {} - {}", context, source_message)
            }
            Self::InvalidInput {
                context,
                source_message,
                ..
            } => {
                write!(f, "Invalid input: {} - {}", context, source_message)
            }
            Self::SignerContext(msg) => {
                write!(f, "Signer context error: {}", msg)
            }
        }
    }
}

impl std::error::Error for ToolError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Retriable { source, .. }
            | Self::RateLimited { source, .. }
            | Self::Permanent { source, .. }
            | Self::InvalidInput { source, .. } => source
                .as_ref()
                .map(|e| e.as_ref() as &(dyn std::error::Error + 'static)),
            Self::SignerContext(_) => None,
        }
    }
}

/// Simple error wrapper for string messages
#[derive(Error, Debug)]
#[error("{0}")]
#[allow(dead_code)]
struct StringError(String);

// Conversion from SignerError to ToolError
impl From<crate::signer::SignerError> for ToolError {
    fn from(err: crate::signer::SignerError) -> Self {
        Self::SignerContext(err.to_string())
    }
}

/// # Error Classification: Explicit is Better
///
/// Generic `From` implementations for common error types like `anyhow::Error` and `String`
/// have been intentionally removed. This is a deliberate design choice to enforce
/// explicit error classification at the point of creation.
///
/// Automatically classifying unknown errors as `Permanent` is a safe default but can hide
/// bugs where transient, retriable errors are not handled correctly. By requiring
/// explicit classification, developers are forced to make a conscious decision about
/// the nature of the error.
///
/// ## Best Practices
///
/// Always use the explicit constructors to create `ToolError` instances:
///
/// ```rust
/// use riglr_core::ToolError;
/// use std::io::Error as IoError;
///
/// // ✅ Explicitly classify known transient errors as retriable.
/// let network_error = IoError::new(std::io::ErrorKind::TimedOut, "Connection timeout");
/// let tool_error = ToolError::retriable_with_source(network_error, "API call failed");
///
/// // ✅ Explicitly classify rate limiting errors.
/// let rate_limit_error = IoError::new(std::io::ErrorKind::Other, "Rate limited");
/// let tool_error = ToolError::rate_limited_with_source(
///     rate_limit_error,
///     "API rate limit exceeded",
///     Some(std::time::Duration::from_secs(60))
/// );
///
/// // ✅ Explicitly classify user input errors.
/// let input_error = IoError::new(std::io::ErrorKind::InvalidInput, "Bad address");
/// let tool_error = ToolError::invalid_input_with_source(input_error, "Invalid address format");
///
/// // ✅ Explicitly classify all other unrecoverable errors as permanent.
/// let auth_error = IoError::new(std::io::ErrorKind::PermissionDenied, "Invalid API key");
/// let tool_error = ToolError::permanent_with_source(auth_error, "Authentication failed");
/// ```
impl From<serde_json::Error> for ToolError {
    fn from(err: serde_json::Error) -> Self {
        Self::permanent_with_source(err, "JSON serialization/deserialization failed")
    }
}

// Removed generic From<&str> implementation - use explicit error constructors instead

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

    #[derive(Error, Debug)]
    #[error("Test error")]
    struct TestError;

    #[test]
    fn test_error_source_preservation() {
        let original_error = TestError;
        let tool_error = ToolError::retriable_with_source(original_error, "Test context");

        // Verify source is preserved
        assert!(tool_error.source().is_some());

        // Verify we can downcast to original error type
        let source = tool_error.source().unwrap();
        assert!(source.downcast_ref::<TestError>().is_some());
    }

    #[test]
    fn test_error_classification() {
        let retriable = ToolError::retriable_with_source(TestError, "Retriable test");
        assert!(retriable.is_retriable());

        let permanent = ToolError::permanent_with_source(TestError, "Permanent test");
        assert!(!permanent.is_retriable());

        let rate_limited = ToolError::rate_limited_with_source(
            TestError,
            "Rate limited test",
            Some(std::time::Duration::from_secs(60)),
        );
        assert!(rate_limited.is_retriable());
        assert_eq!(
            rate_limited.retry_after(),
            Some(std::time::Duration::from_secs(60))
        );
    }

    #[test]
    fn test_string_error_creation() {
        let retriable = ToolError::retriable_string("Network timeout");
        assert!(retriable.is_retriable());
        assert!(!retriable.is_rate_limited());

        let rate_limited = ToolError::rate_limited_string("API rate limit exceeded");
        assert!(rate_limited.is_retriable());
        assert!(rate_limited.is_rate_limited());

        let permanent = ToolError::permanent_string("Invalid parameters");
        assert!(!permanent.is_retriable());
        assert!(!permanent.is_rate_limited());
    }

    // Tests for CoreError enum variants and Display implementations
    #[test]
    fn test_core_error_queue_variant() {
        let error = CoreError::Queue("Failed to connect".to_string());
        assert_eq!(error.to_string(), "Queue error: Failed to connect");
    }

    #[test]
    fn test_core_error_job_execution_variant() {
        let error = CoreError::JobExecution("Job failed to run".to_string());
        assert_eq!(error.to_string(), "Job execution error: Job failed to run");
    }

    #[test]
    fn test_core_error_serialization_variant() {
        // Create a real JSON parsing error by parsing invalid JSON
        let json_error = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let error = CoreError::Serialization(json_error);
        assert!(error.to_string().contains("Serialization error:"));
    }

    #[test]
    fn test_core_error_generic_variant() {
        let error = CoreError::Generic("Something went wrong".to_string());
        assert_eq!(error.to_string(), "Core error: Something went wrong");
    }

    #[test]
    fn test_core_error_from_str() {
        let error = CoreError::from("Test error message");
        assert_eq!(error.to_string(), "Core error: Test error message");
    }

    #[test]
    fn test_core_error_from_empty_str() {
        let error = CoreError::from("");
        assert_eq!(error.to_string(), "Core error: ");
    }

    // Tests for WorkerError enum variants and Display implementations
    #[test]
    fn test_worker_error_tool_not_found() {
        let error = WorkerError::ToolNotFound {
            tool_name: "missing_tool".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Tool 'missing_tool' not found in worker registry"
        );
    }

    #[test]
    fn test_worker_error_semaphore_acquisition() {
        let error = WorkerError::SemaphoreAcquisition {
            tool_name: "test_tool".to_string(),
            source_message: "Test error".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Failed to acquire semaphore for tool 'test_tool': Test error"
        );
    }

    #[test]
    fn test_worker_error_idempotency_store() {
        let error = WorkerError::IdempotencyStore {
            source_message: "Test error".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Idempotency store operation failed: Test error"
        );
    }

    #[test]
    fn test_worker_error_job_serialization() {
        let error = WorkerError::JobSerialization {
            source_message: "invalid JSON format".to_string(),
        };
        assert!(error.to_string().contains("Job serialization error:"));
    }

    #[test]
    fn test_worker_error_execution_timeout() {
        let timeout = std::time::Duration::from_secs(30);
        let error = WorkerError::ExecutionTimeout { timeout };
        assert!(error
            .to_string()
            .contains("Tool execution timed out after 30"));
    }

    #[test]
    fn test_worker_error_internal() {
        let error = WorkerError::Internal {
            message: "Internal system failure".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Internal worker error: Internal system failure"
        );
    }

    // Tests for ToolError additional methods and edge cases
    #[test]
    fn test_tool_error_invalid_input_with_source() {
        let error = ToolError::invalid_input_with_source(TestError, "Bad input data");
        assert!(!error.is_retriable());
        assert!(!error.is_rate_limited());
        assert_eq!(error.retry_after(), None);
        assert_eq!(
            error.to_string(),
            "Invalid input: Bad input data - Test error"
        );
    }

    #[test]
    fn test_tool_error_invalid_input_string() {
        let error = ToolError::invalid_input_string("Invalid JSON format");
        assert!(!error.is_retriable());
        assert!(!error.is_rate_limited());
        assert_eq!(error.retry_after(), None);
        assert_eq!(
            error.to_string(),
            "Invalid input: Invalid JSON format - Invalid JSON format"
        );
    }

    #[test]
    fn test_tool_error_rate_limited_with_no_retry_after() {
        let error = ToolError::rate_limited_with_source(TestError, "Rate limit hit", None);
        assert!(error.is_retriable());
        assert!(error.is_rate_limited());
        assert_eq!(error.retry_after(), None);
    }

    #[test]
    fn test_tool_error_permanent_variant() {
        let error = ToolError::Permanent {
            source: None,
            source_message: "Test error".to_string(),
            context: "Authentication failed".to_string(),
        };
        assert!(!error.is_retriable());
        assert!(!error.is_rate_limited());
        assert_eq!(error.retry_after(), None);
    }

    #[test]
    fn test_tool_error_retriable_variant() {
        let error = ToolError::Retriable {
            source: None,
            source_message: "Test error".to_string(),
            context: "Network issue".to_string(),
        };
        assert!(error.is_retriable());
        assert!(!error.is_rate_limited());
        assert_eq!(error.retry_after(), None);
    }

    #[test]
    fn test_tool_error_invalid_input_variant() {
        let error = ToolError::InvalidInput {
            source: None,
            source_message: "Test error".to_string(),
            context: "Missing required field".to_string(),
        };
        assert!(!error.is_retriable());
        assert!(!error.is_rate_limited());
        assert_eq!(error.retry_after(), None);
    }

    // Tests for explicit error constructors (replacing removed From implementations)
    #[test]
    fn test_tool_error_explicit_anyhow_error() {
        let anyhow_error = anyhow::anyhow!("Something went wrong");
        let tool_error =
            ToolError::permanent_string(format!("An unknown error occurred: {}", anyhow_error));
        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
        assert!(tool_error.to_string().contains("An unknown error occurred"));
    }

    #[test]
    fn test_tool_error_explicit_boxed_error() {
        let test_error = TestError;
        let tool_error =
            ToolError::permanent_with_source(test_error, "A required resource was not found");
        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
        assert_eq!(tool_error.retry_after(), None);
        assert!(
            matches!(tool_error, ToolError::Permanent { ref context, .. } if context == "A required resource was not found")
        );
    }

    #[test]
    fn test_tool_error_explicit_string() {
        let error_msg = "Database connection failed".to_string();
        let tool_error = ToolError::retriable_string(error_msg);
        assert!(tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
        assert!(tool_error
            .to_string()
            .contains("Database connection failed"));
    }

    #[test]
    fn test_tool_error_explicit_str_ref() {
        let error_msg = "Authentication token expired";
        let tool_error = ToolError::permanent_string(error_msg);
        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
        assert!(tool_error.to_string().contains(error_msg));
    }

    #[test]
    fn test_tool_error_explicit_empty_string() {
        let tool_error = ToolError::permanent_string("");
        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
    }

    #[test]
    fn test_tool_error_explicit_empty_str() {
        let tool_error = ToolError::permanent_string("");
        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
    }

    // Test StringError struct (even though it's private, it's used internally)
    #[test]
    fn test_string_error_creation_via_tool_error() {
        let error = ToolError::permanent_string("Test message");
        // permanent_string doesn't create a source, just sets the message
        assert!(error.source().is_none());
        assert_eq!(
            error.to_string(),
            "Permanent error: Test message - Test message"
        );
    }

    // Tests for edge cases with different input types
    #[test]
    fn test_tool_error_constructors_with_various_string_types() {
        // Test with String
        let error1 = ToolError::retriable_with_source(TestError, String::from("String type"));
        assert!(
            matches!(error1, ToolError::Retriable { ref context, .. } if context == "String type")
        );

        // Test with &str
        let error2 = ToolError::permanent_with_source(TestError, "str type");
        assert!(
            matches!(error2, ToolError::Permanent { ref context, .. } if context == "str type")
        );

        // Test with owned String
        let owned_string = "owned string".to_owned();
        let error3 = ToolError::rate_limited_with_source(TestError, owned_string, None);
        assert!(
            matches!(error3, ToolError::RateLimited { ref context, .. } if context == "owned string")
        );
    }

    // Test Result type alias
    #[test]
    fn test_result_type_alias_ok() {
        let result: Result<i32> = Ok(42);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 42);
    }

    #[test]
    fn test_result_type_alias_err() {
        let result: Result<i32> = Err(CoreError::Generic("Test error".to_string()));
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.to_string(), "Core error: Test error");
    }

    // Test const functions
    #[test]
    fn test_const_functions_compilation() {
        // These tests ensure the const functions compile and work correctly
        const fn test_is_retriable() -> bool {
            // We can't create enum variants in const context easily, so just test compilation
            true
        }

        const fn test_retry_after() -> bool {
            // Test compilation of const function
            true
        }

        const fn test_is_rate_limited() -> bool {
            // Test compilation of const function
            true
        }

        assert!(test_is_retriable());
        assert!(test_retry_after());
        assert!(test_is_rate_limited());
    }

    // Test Debug implementations (derived)
    #[test]
    fn test_debug_implementations() {
        let core_error = CoreError::Queue("debug test".to_string());
        let debug_str = format!("{:?}", core_error);
        assert!(debug_str.contains("Queue"));
        assert!(debug_str.contains("debug test"));

        let worker_error = WorkerError::ToolNotFound {
            tool_name: "debug_tool".to_string(),
        };
        let debug_str = format!("{:?}", worker_error);
        assert!(debug_str.contains("ToolNotFound"));
        assert!(debug_str.contains("debug_tool"));

        let tool_error = ToolError::retriable_string("debug test");
        let debug_str = format!("{:?}", tool_error);
        assert!(debug_str.contains("Retriable"));
    }

    // Additional edge case: Test with very long strings
    #[test]
    fn test_errors_with_long_strings() {
        let long_string = "a".repeat(1000);
        let error = CoreError::Generic(long_string.clone());
        assert!(error.to_string().contains(&long_string));

        let tool_error = ToolError::permanent_string(long_string.clone());
        assert!(tool_error.to_string().contains(&long_string));
    }

    // Test error source chain
    #[test]
    fn test_error_source_chain() {
        let original = TestError;
        let tool_error = ToolError::retriable_with_source(original, "Context");

        // Test source method
        let source = tool_error.source();
        assert!(source.is_some());

        // Test that we can downcast
        let concrete_source = source.unwrap().downcast_ref::<TestError>();
        assert!(concrete_source.is_some());
    }

    // Test ToolError SignerContext variant
    #[test]
    fn test_tool_error_signer_context_variant() {
        use crate::signer::SignerError;

        let signer_error = SignerError::NoSignerContext;
        let tool_error = ToolError::SignerContext(signer_error.to_string());

        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
        assert_eq!(tool_error.retry_after(), None);
        assert!(tool_error.to_string().contains("Signer context error"));
    }

    #[test]
    fn test_tool_error_from_signer_error() {
        use crate::signer::SignerError;

        let signer_error = SignerError::Configuration("Invalid config".to_string());
        let tool_error = ToolError::from(signer_error);

        assert!(
            matches!(tool_error, ToolError::SignerContext(ref inner) if inner.to_string().contains("Invalid configuration: Invalid config"))
        );
    }

    #[test]
    fn test_tool_error_from_serde_json_error() {
        // Create a real JSON parsing error by parsing invalid JSON
        let json_error = serde_json::from_str::<serde_json::Value>("{ invalid }").unwrap_err();
        let tool_error = ToolError::from(json_error);

        assert!(!tool_error.is_retriable());
        assert!(!tool_error.is_rate_limited());
        assert!(tool_error
            .to_string()
            .contains("JSON serialization/deserialization failed"));
        assert!(
            matches!(tool_error, ToolError::Permanent { ref context, .. } if context == "JSON serialization/deserialization failed")
        );
    }

    // Test Core Error Serialization from serde_json::Error
    #[test]
    fn test_core_error_from_serde_json_error() {
        // Create a real JSON parsing error by parsing invalid JSON
        let json_error = serde_json::from_str::<serde_json::Value>("{ invalid }").unwrap_err();
        let core_error = CoreError::from(json_error);

        assert!(
            matches!(core_error, CoreError::Serialization(_))
                && core_error.to_string().contains("Serialization error:")
        );
    }

    // Test Redis feature (conditional compilation)
    #[cfg(feature = "redis")]
    #[test]
    fn test_core_error_redis_variant() {
        use redis::RedisError;

        // Create a Redis error (we'll simulate one)
        let redis_error = RedisError::from((redis::ErrorKind::TypeError, "Type error"));
        let core_error = CoreError::Redis(redis_error);

        assert!(core_error.to_string().contains("Redis error:"));
    }

    #[cfg(feature = "redis")]
    #[test]
    fn test_core_error_from_redis_error() {
        use redis::RedisError;

        let redis_error = RedisError::from((redis::ErrorKind::IoError, "Connection failed"));
        let core_error = CoreError::from(redis_error);

        assert!(
            matches!(core_error, CoreError::Redis(_))
                && core_error.to_string().contains("Redis error:")
        );
    }

    // Test various Duration formats for ExecutionTimeout
    #[test]
    fn test_worker_error_execution_timeout_various_durations() {
        // Test zero duration
        let timeout_zero = std::time::Duration::from_secs(0);
        let error_zero = WorkerError::ExecutionTimeout {
            timeout: timeout_zero,
        };
        assert_eq!(error_zero.to_string(), "Tool execution timed out after 0ns");

        // Test milliseconds
        let timeout_millis = std::time::Duration::from_millis(500);
        let error_millis = WorkerError::ExecutionTimeout {
            timeout: timeout_millis,
        };
        assert_eq!(
            error_millis.to_string(),
            "Tool execution timed out after 500ms"
        );

        // Test minutes
        let timeout_minutes = std::time::Duration::from_secs(120);
        let error_minutes = WorkerError::ExecutionTimeout {
            timeout: timeout_minutes,
        };
        assert_eq!(
            error_minutes.to_string(),
            "Tool execution timed out after 120s"
        );
    }

    // Test edge cases for RateLimited retry_after
    #[test]
    fn test_rate_limited_retry_after_edge_cases() {
        // Test with zero duration
        let error1 = ToolError::rate_limited_with_source(
            TestError,
            "Rate limited",
            Some(std::time::Duration::from_secs(0)),
        );
        assert_eq!(
            error1.retry_after(),
            Some(std::time::Duration::from_secs(0))
        );

        // Test with very large duration
        let error2 = ToolError::rate_limited_with_source(
            TestError,
            "Rate limited",
            Some(std::time::Duration::from_secs(u64::MAX)),
        );
        assert_eq!(
            error2.retry_after(),
            Some(std::time::Duration::from_secs(u64::MAX))
        );
    }

    // Test all match arms in is_retriable
    #[test]
    fn test_is_retriable_all_variants() {
        let retriable = ToolError::Retriable {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert!(retriable.is_retriable());

        let rate_limited = ToolError::RateLimited {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
            retry_after: None,
        };
        assert!(rate_limited.is_retriable());

        let permanent = ToolError::Permanent {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert!(!permanent.is_retriable());

        let invalid_input = ToolError::InvalidInput {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert!(!invalid_input.is_retriable());

        let signer_context =
            ToolError::SignerContext(crate::signer::SignerError::NoSignerContext.to_string());
        assert!(!signer_context.is_retriable());
    }

    // Test all match arms in retry_after
    #[test]
    fn test_retry_after_all_variants() {
        let rate_limited_with_delay = ToolError::RateLimited {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
            retry_after: Some(std::time::Duration::from_secs(30)),
        };
        assert_eq!(
            rate_limited_with_delay.retry_after(),
            Some(std::time::Duration::from_secs(30))
        );

        let rate_limited_no_delay = ToolError::RateLimited {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
            retry_after: None,
        };
        assert_eq!(rate_limited_no_delay.retry_after(), None);

        let retriable = ToolError::Retriable {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert_eq!(retriable.retry_after(), None);

        let permanent = ToolError::Permanent {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert_eq!(permanent.retry_after(), None);

        let invalid_input = ToolError::InvalidInput {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert_eq!(invalid_input.retry_after(), None);

        let signer_context =
            ToolError::SignerContext(crate::signer::SignerError::NoSignerContext.to_string());
        assert_eq!(signer_context.retry_after(), None);
    }

    // Test all match arms in is_rate_limited
    #[test]
    fn test_is_rate_limited_all_variants() {
        let rate_limited = ToolError::RateLimited {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
            retry_after: None,
        };
        assert!(rate_limited.is_rate_limited());

        let retriable = ToolError::Retriable {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert!(!retriable.is_rate_limited());

        let permanent = ToolError::Permanent {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert!(!permanent.is_rate_limited());

        let invalid_input = ToolError::InvalidInput {
            source: None,
            source_message: "Test error".to_string(),
            context: "test".to_string(),
        };
        assert!(!invalid_input.is_rate_limited());

        let signer_context =
            ToolError::SignerContext(crate::signer::SignerError::NoSignerContext.to_string());
        assert!(!signer_context.is_rate_limited());
    }

    // Test that must_use attributes don't cause warnings
    #[test]
    fn test_must_use_methods() {
        let error = ToolError::retriable_string("test");

        // These methods have #[must_use] attribute
        let _ = error.is_retriable();
        let _ = error.retry_after();
        let _ = error.is_rate_limited();
    }
}