converge-core 2.0.0

Converge Agent OS - correctness-first, context-driven multi-agent runtime
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
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT

//! LLM Backend Interface — The unification boundary for local and remote LLMs.
//!
//! # The Unification Rule
//!
//! All model invocations—local or remote—must produce the same top-level artifact:
//! - `BackendResponse` containing `ProposedContent`(s)
//! - Plus a `TraceLink` that makes the invocation auditable, budgeted, and comparable
//!
//! "Interchangeable" means:
//! - Same request type
//! - Same output type
//! - Same contract evaluation surface
//! - Different execution backend
//!
//! # Determinism Guarantees
//!
//! | Backend | Determinism | TraceLink |
//! |---------|-------------|-----------|
//! | Local (converge-llm) | Strong (replay-eligible) | `LocalTraceLink` |
//! | Remote (providers) | Bounded stochasticity (audit-eligible) | `RemoteTraceLink` |
//!
//! Remote runs are:
//! - **Auditable**: Full request/response + metadata
//! - **Repeatable-ish**: Best effort (temp=0 helps)
//! - **Non-replayable**: Strictly (model versions, safety layers can shift)
//!
//! # Example
//!
//! ```
//! use converge_core::backend::{
//!     LlmBackend, BackendCapability, BackendRequest, BackendResponse,
//!     BackendPrompt, BackendBudgets, BackendResult,
//! };
//!
//! // Both local and remote backends implement the same trait
//! fn process_with_any_backend<B: LlmBackend>(
//!     backend: &B,
//!     request: &BackendRequest,
//! ) -> BackendResult<BackendResponse> {
//!     // Check capabilities first
//!     if backend.supports_capability(BackendCapability::Replay) {
//!         println!("Using replay-eligible backend: {}", backend.name());
//!     }
//!     backend.execute(request)
//! }
//! ```

use serde::{Deserialize, Serialize};

use crate::kernel_boundary::{ProposedContent, TraceLink};

// ============================================================================
// Backend Error
// ============================================================================

/// Error type for backend operations.
///
/// This is capability-agnostic - implementations can wrap their specific errors.
///
/// # Retryable Errors
///
/// Some errors are transient and can be retried:
/// - `Timeout` - operation exceeded deadline but might succeed on retry
/// - `Unavailable` - backend temporarily unavailable
/// - `ExecutionFailed` - if caused by transient infrastructure issues
///
/// Use `is_retryable()` to check if an error should trigger retry logic.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BackendError {
    /// Request validation failed (NOT retryable - fix the request)
    InvalidRequest { message: String },
    /// Backend execution failed (may be retryable depending on cause)
    ExecutionFailed { message: String },
    /// Backend is unavailable (retryable - try again later)
    Unavailable { message: String },
    /// Budget exceeded (NOT retryable - increase budget or reduce request)
    BudgetExceeded { resource: String, limit: String },
    /// Contract validation failed (NOT retryable - output doesn't match contract)
    ContractFailed { contract: String, message: String },
    /// Capability not supported (NOT retryable - use different backend)
    UnsupportedCapability { capability: BackendCapability },
    /// Adapter not found or incompatible (NOT retryable - fix configuration)
    AdapterError { message: String },
    /// Recall operation failed (may be retryable)
    RecallError { message: String },
    /// Operation timed out (retryable - might succeed with more time)
    Timeout {
        /// Configured deadline in milliseconds
        deadline_ms: u64,
        /// Actual elapsed time in milliseconds
        elapsed_ms: u64,
    },
    /// Circuit breaker is open (NOT retryable until circuit closes)
    CircuitOpen {
        /// Name of the backend with open circuit
        backend: String,
        /// When the circuit will transition to half-open (Unix timestamp ms)
        retry_after_ms: Option<u64>,
    },
    /// Retryable wrapper - indicates retry was attempted
    Retried {
        /// The final error after all retries exhausted
        message: String,
        /// Number of attempts made
        attempts: usize,
        /// Whether the underlying error was transient
        was_transient: bool,
    },
    /// Generic error with context
    Other { message: String },
}

impl std::fmt::Display for BackendError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidRequest { message } => write!(f, "Invalid request: {}", message),
            Self::ExecutionFailed { message } => write!(f, "Execution failed: {}", message),
            Self::Unavailable { message } => write!(f, "Backend unavailable: {}", message),
            Self::BudgetExceeded { resource, limit } => {
                write!(f, "Budget exceeded: {} (limit: {})", resource, limit)
            }
            Self::ContractFailed { contract, message } => {
                write!(f, "Contract '{}' failed: {}", contract, message)
            }
            Self::UnsupportedCapability { capability } => {
                write!(f, "Unsupported capability: {:?}", capability)
            }
            Self::AdapterError { message } => write!(f, "Adapter error: {}", message),
            Self::RecallError { message } => write!(f, "Recall error: {}", message),
            Self::Timeout {
                deadline_ms,
                elapsed_ms,
            } => {
                write!(
                    f,
                    "Operation timed out: elapsed {}ms, deadline {}ms",
                    elapsed_ms, deadline_ms
                )
            }
            Self::CircuitOpen {
                backend,
                retry_after_ms,
            } => {
                if let Some(retry_after) = retry_after_ms {
                    write!(
                        f,
                        "Circuit breaker open for '{}', retry after {}ms",
                        backend, retry_after
                    )
                } else {
                    write!(f, "Circuit breaker open for '{}'", backend)
                }
            }
            Self::Retried {
                message,
                attempts,
                was_transient,
            } => {
                write!(
                    f,
                    "Failed after {} attempts (transient: {}): {}",
                    attempts, was_transient, message
                )
            }
            Self::Other { message } => write!(f, "{}", message),
        }
    }
}

impl std::error::Error for BackendError {}

impl BackendError {
    /// Check if this error is retryable.
    ///
    /// Retryable errors are transient and might succeed on retry:
    /// - Timeout
    /// - Unavailable
    /// - Some ExecutionFailed (network issues, rate limits)
    ///
    /// Non-retryable errors require intervention:
    /// - InvalidRequest (fix the request)
    /// - BudgetExceeded (increase budget)
    /// - ContractFailed (output doesn't match)
    /// - UnsupportedCapability (use different backend)
    /// - CircuitOpen (wait for circuit to close)
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        match self {
            Self::Timeout { .. } => true,
            Self::Unavailable { .. } => true,
            Self::ExecutionFailed { message } => {
                // Heuristic: network/rate limit errors are retryable
                let msg_lower = message.to_lowercase();
                msg_lower.contains("timeout")
                    || msg_lower.contains("rate limit")
                    || msg_lower.contains("429")
                    || msg_lower.contains("503")
                    || msg_lower.contains("502")
                    || msg_lower.contains("504")
                    || msg_lower.contains("connection")
                    || msg_lower.contains("network")
            }
            Self::RecallError { message } => {
                // Recall errors might be transient (embedding service down)
                let msg_lower = message.to_lowercase();
                msg_lower.contains("timeout") || msg_lower.contains("unavailable")
            }
            // Not retryable
            Self::InvalidRequest { .. } => false,
            Self::BudgetExceeded { .. } => false,
            Self::ContractFailed { .. } => false,
            Self::UnsupportedCapability { .. } => false,
            Self::AdapterError { .. } => false,
            Self::CircuitOpen { .. } => false, // Must wait for circuit to close
            Self::Retried { .. } => false,     // Already retried
            Self::Other { .. } => false,
        }
    }

    /// Check if this error indicates the backend is overloaded.
    ///
    /// Used by circuit breakers to track failure patterns.
    #[must_use]
    pub fn is_overload(&self) -> bool {
        match self {
            Self::Unavailable { .. } => true,
            Self::Timeout { .. } => true,
            Self::ExecutionFailed { message } => {
                let msg_lower = message.to_lowercase();
                msg_lower.contains("rate limit")
                    || msg_lower.contains("429")
                    || msg_lower.contains("503")
                    || msg_lower.contains("overloaded")
            }
            _ => false,
        }
    }
}

/// Result type for backend operations.
pub type BackendResult<T> = Result<T, BackendError>;

// ============================================================================
// Backend Capability
// ============================================================================

/// Backend capabilities for routing decisions.
///
/// These capabilities determine what a backend can do and influence
/// which backend is selected for a given request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BackendCapability {
    /// Deterministic replay - same inputs produce identical outputs
    Replay,
    /// LoRA adapters for task-specific tuning
    Adapters,
    /// Recall injection from corpus
    Recall,
    /// Step-level contract validation
    StepContracts,
    /// Frontier reasoning capabilities (Claude Opus, GPT-4, etc.)
    FrontierReasoning,
    /// Fast iteration for interactive use
    FastIteration,
    /// Offline operation (no network required)
    Offline,
    /// Streaming output
    Streaming,
    /// Vision/multimodal input
    Vision,
    /// Tool use / function calling
    ToolUse,
}

// ============================================================================
// Retry Policy
// ============================================================================

/// Backoff strategy for retries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BackoffStrategy {
    /// Fixed delay between retries
    Fixed,
    /// Linear increase: delay * attempt
    Linear,
    /// Exponential increase: delay * 2^attempt
    Exponential,
}

impl Default for BackoffStrategy {
    fn default() -> Self {
        Self::Exponential
    }
}

/// Configuration for retry behavior.
///
/// # Example
///
/// ```
/// use converge_core::backend::{RetryPolicy, BackoffStrategy};
///
/// // Retry up to 3 times with exponential backoff starting at 100ms
/// let policy = RetryPolicy {
///     max_attempts: 3,
///     initial_delay_ms: 100,
///     max_delay_ms: 5000,
///     backoff: BackoffStrategy::Exponential,
///     jitter_percent: 20,
/// };
///
/// assert_eq!(policy.delay_for_attempt(1), 100); // First retry: 100ms
/// // Second retry: ~200ms, Third retry: ~400ms (plus jitter)
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RetryPolicy {
    /// Maximum number of attempts (including initial attempt)
    pub max_attempts: usize,
    /// Initial delay between retries in milliseconds
    pub initial_delay_ms: u64,
    /// Maximum delay cap in milliseconds
    pub max_delay_ms: u64,
    /// Backoff strategy
    pub backoff: BackoffStrategy,
    /// Jitter percentage (0-100) to add randomness to delays
    pub jitter_percent: u8,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            initial_delay_ms: 100,
            max_delay_ms: 10_000,
            backoff: BackoffStrategy::Exponential,
            jitter_percent: 20,
        }
    }
}

impl RetryPolicy {
    /// Create a policy that never retries.
    #[must_use]
    pub fn no_retry() -> Self {
        Self {
            max_attempts: 1,
            ..Default::default()
        }
    }

    /// Create an aggressive retry policy for critical operations.
    #[must_use]
    pub fn aggressive() -> Self {
        Self {
            max_attempts: 5,
            initial_delay_ms: 50,
            max_delay_ms: 30_000,
            backoff: BackoffStrategy::Exponential,
            jitter_percent: 25,
        }
    }

    /// Calculate the delay for a given attempt number (1-indexed).
    ///
    /// Does not include jitter - caller should add jitter separately.
    #[must_use]
    pub fn delay_for_attempt(&self, attempt: usize) -> u64 {
        if attempt == 0 {
            return 0;
        }
        let attempt = attempt.saturating_sub(1); // Convert to 0-indexed for calculation

        let delay = match self.backoff {
            BackoffStrategy::Fixed => self.initial_delay_ms,
            BackoffStrategy::Linear => self.initial_delay_ms.saturating_mul(attempt as u64 + 1),
            BackoffStrategy::Exponential => self
                .initial_delay_ms
                .saturating_mul(1u64 << attempt.min(10)),
        };

        delay.min(self.max_delay_ms)
    }

    /// Check if another attempt should be made.
    #[must_use]
    pub fn should_retry(&self, attempt: usize) -> bool {
        attempt < self.max_attempts
    }
}

// ============================================================================
// Circuit Breaker
// ============================================================================

/// Configuration for circuit breaker behavior.
///
/// Circuit breakers protect against cascading failures by stopping requests
/// to a failing backend until it recovers.
///
/// # States
///
/// - **Closed**: Normal operation, requests pass through
/// - **Open**: Backend is failing, requests fast-fail immediately
/// - **Half-Open**: Testing if backend recovered, limited requests allowed
///
/// # Example
///
/// ```
/// use converge_core::backend::CircuitBreakerConfig;
///
/// let config = CircuitBreakerConfig {
///     failure_threshold: 5,
///     success_threshold: 2,
///     timeout_ms: 30_000,
///     half_open_max_requests: 3,
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CircuitBreakerConfig {
    /// Number of failures before circuit opens
    pub failure_threshold: usize,
    /// Number of successes in half-open state before closing
    pub success_threshold: usize,
    /// Time in milliseconds before transitioning from open to half-open
    pub timeout_ms: u64,
    /// Maximum requests allowed in half-open state
    pub half_open_max_requests: usize,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 5,
            success_threshold: 2,
            timeout_ms: 30_000,
            half_open_max_requests: 3,
        }
    }
}

impl CircuitBreakerConfig {
    /// Create a sensitive circuit breaker that opens quickly.
    #[must_use]
    pub fn sensitive() -> Self {
        Self {
            failure_threshold: 3,
            success_threshold: 1,
            timeout_ms: 15_000,
            half_open_max_requests: 1,
        }
    }

    /// Create a tolerant circuit breaker that allows more failures.
    #[must_use]
    pub fn tolerant() -> Self {
        Self {
            failure_threshold: 10,
            success_threshold: 3,
            timeout_ms: 60_000,
            half_open_max_requests: 5,
        }
    }

    /// Disable circuit breaker (never opens).
    #[must_use]
    pub fn disabled() -> Self {
        Self {
            failure_threshold: usize::MAX,
            success_threshold: 1,
            timeout_ms: 0,
            half_open_max_requests: usize::MAX,
        }
    }
}

/// Current state of a circuit breaker.
///
/// This is runtime state, not configuration. Implementations track this
/// per-backend to manage circuit breaker behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CircuitState {
    /// Normal operation - requests pass through
    #[default]
    Closed,
    /// Backend failing - requests fast-fail
    Open,
    /// Testing recovery - limited requests allowed
    HalfOpen,
}

// ============================================================================
// Backend Request
// ============================================================================

/// A unified request to any LLM backend.
///
/// Both local kernel and remote providers accept this same request type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendRequest {
    /// Intent identifier for tracking
    pub intent_id: String,
    /// Truth targets this invocation aims to satisfy
    pub truth_ids: Vec<String>,
    /// Prompt version for reproducibility
    pub prompt_version: String,
    /// Hash of the state injection (for audit)
    pub state_injection_hash: String,
    /// The actual prompt/messages to send
    pub prompt: BackendPrompt,
    /// Contracts to validate against
    pub contracts: Vec<ContractSpec>,
    /// Resource budgets
    pub budgets: BackendBudgets,
    /// Recall policy (optional, local-only capability)
    pub recall_policy: Option<BackendRecallPolicy>,
    /// Adapter policy (optional, local-only capability)
    pub adapter_policy: Option<BackendAdapterPolicy>,
    /// Retry policy (optional, overrides backend default)
    #[serde(default)]
    pub retry_policy: Option<RetryPolicy>,
}

/// The prompt content for the backend.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BackendPrompt {
    /// Simple text prompt
    Text(String),
    /// Chat-style messages
    Messages(Vec<Message>),
}

/// A chat message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub role: MessageRole,
    pub content: String,
}

impl Message {
    /// Create a system message.
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::System,
            content: content.into(),
        }
    }

    /// Create a user message.
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::User,
            content: content.into(),
        }
    }

    /// Create an assistant message.
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::Assistant,
            content: content.into(),
        }
    }
}

/// Message role in chat format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageRole {
    System,
    User,
    Assistant,
}

/// Contract specification for validation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractSpec {
    /// Contract/truth name
    pub name: String,
    /// Expected output schema (JSON Schema)
    pub schema: Option<serde_json::Value>,
    /// Whether this contract is required to pass
    pub required: bool,
}

/// Resource budgets for the invocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendBudgets {
    /// Maximum tokens to generate
    pub max_tokens: usize,
    /// Maximum iterations (for multi-step)
    pub max_iterations: usize,
    /// Latency ceiling in milliseconds (0 = no limit)
    pub latency_ceiling_ms: u64,
    /// Maximum cost in microdollars (0 = no limit)
    pub cost_ceiling_microdollars: u64,
}

impl Default for BackendBudgets {
    fn default() -> Self {
        Self {
            max_tokens: 1024,
            max_iterations: 1,
            latency_ceiling_ms: 0,
            cost_ceiling_microdollars: 0,
        }
    }
}

/// Recall policy for backend requests.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendRecallPolicy {
    pub enabled: bool,
    pub max_candidates: usize,
    pub min_score: f32,
    pub corpus_filter: Option<String>,
}

impl Default for BackendRecallPolicy {
    fn default() -> Self {
        Self {
            enabled: false,
            max_candidates: 5,
            min_score: 0.5,
            corpus_filter: None,
        }
    }
}

/// Adapter policy for backend requests.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendAdapterPolicy {
    /// Explicit adapter ID (authority from outside)
    pub adapter_id: Option<String>,
    /// Whether adapter is required (fail if not available)
    pub required: bool,
}

impl Default for BackendAdapterPolicy {
    fn default() -> Self {
        Self {
            adapter_id: None,
            required: false,
        }
    }
}

// ============================================================================
// Backend Response
// ============================================================================

/// A unified response from any LLM backend.
///
/// Both local kernel and remote providers return this same response type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendResponse {
    /// The generated proposals
    pub proposals: Vec<ProposedContent>,
    /// Contract validation report
    pub contract_report: ContractReport,
    /// Trace link (backend-specific but normalized interface)
    pub trace_link: TraceLink,
    /// Resource usage
    pub usage: BackendUsage,
}

/// Contract validation report for backend responses.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractReport {
    /// Per-contract results
    pub results: Vec<BackendContractResult>,
    /// Overall pass/fail
    pub all_passed: bool,
}

impl ContractReport {
    /// Create an empty passing report.
    pub fn empty_pass() -> Self {
        Self {
            results: vec![],
            all_passed: true,
        }
    }

    /// Create a report from results.
    pub fn from_results(results: Vec<BackendContractResult>) -> Self {
        let all_passed = results.iter().all(|r| r.passed);
        Self {
            results,
            all_passed,
        }
    }
}

/// Result of a single contract check (backend-level).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendContractResult {
    pub name: String,
    pub passed: bool,
    pub diagnostics: Option<String>,
}

impl BackendContractResult {
    /// Create a passing result.
    pub fn pass(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            passed: true,
            diagnostics: None,
        }
    }

    /// Create a failing result with diagnostics.
    pub fn fail(name: impl Into<String>, diagnostics: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            passed: false,
            diagnostics: Some(diagnostics.into()),
        }
    }
}

/// Resource usage from the invocation.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BackendUsage {
    pub input_tokens: usize,
    pub output_tokens: usize,
    pub total_tokens: usize,
    pub latency_ms: u64,
    pub cost_microdollars: Option<u64>,
}

// ============================================================================
// LlmBackend Trait
// ============================================================================

/// The unified backend interface.
///
/// Both local kernel (converge-llm) and remote providers (converge-provider)
/// implement this trait, making "local vs remote" genuinely interchangeable.
///
/// # Implementors
///
/// - `LlamaEngine` (converge-llm) - Local inference with Burn/llama-burn
/// - `AnthropicBackend` (converge-provider) - Remote Claude API
/// - Future: OpenAIBackend, CohereReranker, etc.
///
/// # Thread Safety
///
/// Backends must be `Send + Sync` to support concurrent request handling.
///
/// # Deprecation Notice
///
/// This trait is deprecated in favor of the capability boundary traits in
/// `converge_core::traits`:
///
/// - [`ChatBackend`](crate::traits::ChatBackend) - For chat completion (GAT async)
/// - [`EmbedBackend`](crate::traits::EmbedBackend) - For embedding generation (GAT async)
/// - [`LlmBackend`](crate::traits::LlmBackend) - Umbrella combining both
///
/// The new traits use the GAT async pattern for zero-cost async without
/// `async_trait`. See `converge-core/BOUNDARY.md` for migration guide.
#[deprecated(
    since = "0.2.0",
    note = "Use converge_core::traits::LlmBackend (GAT async) instead. See BOUNDARY.md for migration."
)]
pub trait LlmBackend: Send + Sync {
    /// Backend name for identification and routing.
    fn name(&self) -> &str;

    /// Whether this backend supports deterministic replay.
    ///
    /// - Local backends with fixed seeds: `true`
    /// - Remote backends: `false` (model versions can change)
    fn supports_replay(&self) -> bool;

    /// Execute an LLM request.
    ///
    /// This is the core interface. Implementations handle:
    /// - Prompt formatting
    /// - Model invocation
    /// - Contract validation
    /// - Trace link generation
    fn execute(&self, request: &BackendRequest) -> BackendResult<BackendResponse>;

    /// Check if this backend supports a specific capability.
    ///
    /// Used by routing policies to select appropriate backends.
    fn supports_capability(&self, capability: BackendCapability) -> bool;

    /// List all capabilities this backend supports.
    ///
    /// Default implementation checks each capability individually.
    fn capabilities(&self) -> Vec<BackendCapability> {
        let all_caps = [
            BackendCapability::Replay,
            BackendCapability::Adapters,
            BackendCapability::Recall,
            BackendCapability::StepContracts,
            BackendCapability::FrontierReasoning,
            BackendCapability::FastIteration,
            BackendCapability::Offline,
            BackendCapability::Streaming,
            BackendCapability::Vision,
            BackendCapability::ToolUse,
        ];
        all_caps
            .iter()
            .filter(|cap| self.supports_capability(**cap))
            .copied()
            .collect()
    }
}

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

    #[test]
    fn test_backend_budgets_default() {
        let budgets = BackendBudgets::default();
        assert_eq!(budgets.max_tokens, 1024);
        assert_eq!(budgets.max_iterations, 1);
        assert_eq!(budgets.latency_ceiling_ms, 0);
        assert_eq!(budgets.cost_ceiling_microdollars, 0);
    }

    #[test]
    fn test_message_constructors() {
        let system = Message::system("You are a helpful assistant");
        assert_eq!(system.role, MessageRole::System);
        assert_eq!(system.content, "You are a helpful assistant");

        let user = Message::user("Hello");
        assert_eq!(user.role, MessageRole::User);

        let assistant = Message::assistant("Hi there!");
        assert_eq!(assistant.role, MessageRole::Assistant);
    }

    #[test]
    fn test_contract_report_from_results() {
        let results = vec![
            BackendContractResult::pass("contract1"),
            BackendContractResult::pass("contract2"),
        ];
        let report = ContractReport::from_results(results);
        assert!(report.all_passed);

        let mixed = vec![
            BackendContractResult::pass("contract1"),
            BackendContractResult::fail("contract2", "missing field"),
        ];
        let report = ContractReport::from_results(mixed);
        assert!(!report.all_passed);
    }

    #[test]
    fn test_backend_error_display() {
        let err = BackendError::BudgetExceeded {
            resource: "tokens".to_string(),
            limit: "1024".to_string(),
        };
        assert!(err.to_string().contains("tokens"));
        assert!(err.to_string().contains("1024"));
    }

    #[test]
    fn test_capability_serialization_stable() {
        assert_eq!(
            serde_json::to_string(&BackendCapability::Replay).unwrap(),
            "\"Replay\""
        );
        assert_eq!(
            serde_json::to_string(&BackendCapability::FrontierReasoning).unwrap(),
            "\"FrontierReasoning\""
        );
    }

    #[test]
    fn test_message_role_serialization_stable() {
        assert_eq!(
            serde_json::to_string(&MessageRole::System).unwrap(),
            "\"System\""
        );
        assert_eq!(
            serde_json::to_string(&MessageRole::User).unwrap(),
            "\"User\""
        );
        assert_eq!(
            serde_json::to_string(&MessageRole::Assistant).unwrap(),
            "\"Assistant\""
        );
    }

    // =========================================================================
    // Retry Policy Tests
    // =========================================================================

    #[test]
    fn test_retry_policy_default() {
        let policy = RetryPolicy::default();
        assert_eq!(policy.max_attempts, 3);
        assert_eq!(policy.initial_delay_ms, 100);
        assert_eq!(policy.backoff, BackoffStrategy::Exponential);
    }

    #[test]
    fn test_retry_policy_no_retry() {
        let policy = RetryPolicy::no_retry();
        assert_eq!(policy.max_attempts, 1);
        assert!(!policy.should_retry(1));
    }

    #[test]
    fn test_retry_policy_delay_exponential() {
        let policy = RetryPolicy {
            max_attempts: 5,
            initial_delay_ms: 100,
            max_delay_ms: 10_000,
            backoff: BackoffStrategy::Exponential,
            jitter_percent: 0,
        };

        assert_eq!(policy.delay_for_attempt(1), 100); // 100 * 2^0
        assert_eq!(policy.delay_for_attempt(2), 200); // 100 * 2^1
        assert_eq!(policy.delay_for_attempt(3), 400); // 100 * 2^2
        assert_eq!(policy.delay_for_attempt(4), 800); // 100 * 2^3
    }

    #[test]
    fn test_retry_policy_delay_linear() {
        let policy = RetryPolicy {
            max_attempts: 5,
            initial_delay_ms: 100,
            max_delay_ms: 10_000,
            backoff: BackoffStrategy::Linear,
            jitter_percent: 0,
        };

        assert_eq!(policy.delay_for_attempt(1), 100); // 100 * 1
        assert_eq!(policy.delay_for_attempt(2), 200); // 100 * 2
        assert_eq!(policy.delay_for_attempt(3), 300); // 100 * 3
    }

    #[test]
    fn test_retry_policy_delay_fixed() {
        let policy = RetryPolicy {
            max_attempts: 5,
            initial_delay_ms: 100,
            max_delay_ms: 10_000,
            backoff: BackoffStrategy::Fixed,
            jitter_percent: 0,
        };

        assert_eq!(policy.delay_for_attempt(1), 100);
        assert_eq!(policy.delay_for_attempt(2), 100);
        assert_eq!(policy.delay_for_attempt(3), 100);
    }

    #[test]
    fn test_retry_policy_max_delay_cap() {
        let policy = RetryPolicy {
            max_attempts: 20,
            initial_delay_ms: 1000,
            max_delay_ms: 5000,
            backoff: BackoffStrategy::Exponential,
            jitter_percent: 0,
        };

        // Exponential would be 1000 * 2^9 = 512000, but capped at 5000
        assert_eq!(policy.delay_for_attempt(10), 5000);
    }

    #[test]
    fn test_retry_policy_should_retry() {
        let policy = RetryPolicy {
            max_attempts: 3,
            ..Default::default()
        };

        assert!(policy.should_retry(1));
        assert!(policy.should_retry(2));
        assert!(!policy.should_retry(3));
        assert!(!policy.should_retry(4));
    }

    // =========================================================================
    // Circuit Breaker Tests
    // =========================================================================

    #[test]
    fn test_circuit_breaker_config_default() {
        let config = CircuitBreakerConfig::default();
        assert_eq!(config.failure_threshold, 5);
        assert_eq!(config.success_threshold, 2);
        assert_eq!(config.timeout_ms, 30_000);
    }

    #[test]
    fn test_circuit_breaker_config_sensitive() {
        let config = CircuitBreakerConfig::sensitive();
        assert_eq!(config.failure_threshold, 3);
        assert!(config.failure_threshold < CircuitBreakerConfig::default().failure_threshold);
    }

    #[test]
    fn test_circuit_breaker_config_tolerant() {
        let config = CircuitBreakerConfig::tolerant();
        assert_eq!(config.failure_threshold, 10);
        assert!(config.failure_threshold > CircuitBreakerConfig::default().failure_threshold);
    }

    #[test]
    fn test_circuit_state_default() {
        let state = CircuitState::default();
        assert_eq!(state, CircuitState::Closed);
    }

    // =========================================================================
    // Backend Error Retryable Tests
    // =========================================================================

    #[test]
    fn test_timeout_is_retryable() {
        let err = BackendError::Timeout {
            deadline_ms: 5000,
            elapsed_ms: 5001,
        };
        assert!(err.is_retryable());
        assert!(err.is_overload());
    }

    #[test]
    fn test_unavailable_is_retryable() {
        let err = BackendError::Unavailable {
            message: "Service temporarily unavailable".to_string(),
        };
        assert!(err.is_retryable());
        assert!(err.is_overload());
    }

    #[test]
    fn test_rate_limit_is_retryable() {
        let err = BackendError::ExecutionFailed {
            message: "Rate limit exceeded (429)".to_string(),
        };
        assert!(err.is_retryable());
        assert!(err.is_overload());
    }

    #[test]
    fn test_invalid_request_not_retryable() {
        let err = BackendError::InvalidRequest {
            message: "Missing required field".to_string(),
        };
        assert!(!err.is_retryable());
        assert!(!err.is_overload());
    }

    #[test]
    fn test_budget_exceeded_not_retryable() {
        let err = BackendError::BudgetExceeded {
            resource: "tokens".to_string(),
            limit: "1024".to_string(),
        };
        assert!(!err.is_retryable());
        assert!(!err.is_overload());
    }

    #[test]
    fn test_circuit_open_not_retryable() {
        let err = BackendError::CircuitOpen {
            backend: "anthropic".to_string(),
            retry_after_ms: Some(30_000),
        };
        assert!(!err.is_retryable());
        assert!(!err.is_overload());
    }

    #[test]
    fn test_timeout_error_display() {
        let err = BackendError::Timeout {
            deadline_ms: 5000,
            elapsed_ms: 6000,
        };
        let msg = err.to_string();
        assert!(msg.contains("6000"));
        assert!(msg.contains("5000"));
    }

    #[test]
    fn test_circuit_open_error_display() {
        let err = BackendError::CircuitOpen {
            backend: "test-backend".to_string(),
            retry_after_ms: Some(30_000),
        };
        let msg = err.to_string();
        assert!(msg.contains("test-backend"));
        assert!(msg.contains("30000"));
    }

    #[test]
    fn test_retried_error_display() {
        let err = BackendError::Retried {
            message: "Final error".to_string(),
            attempts: 3,
            was_transient: true,
        };
        let msg = err.to_string();
        assert!(msg.contains("3 attempts"));
        assert!(msg.contains("transient: true"));
    }

    // =========================================================================
    // Serialization Stability Tests
    // =========================================================================

    #[test]
    fn test_retry_policy_serialization_stable() {
        let policy = RetryPolicy::default();
        let json = serde_json::to_string(&policy).unwrap();
        assert!(json.contains("\"max_attempts\":3"));
        assert!(json.contains("\"Exponential\""));

        // Round-trip
        let parsed: RetryPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, policy);
    }

    #[test]
    fn test_circuit_breaker_config_serialization_stable() {
        let config = CircuitBreakerConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("\"failure_threshold\":5"));

        // Round-trip
        let parsed: CircuitBreakerConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, config);
    }
}