forge-client 0.6.0

MCP client connections to downstream servers for Forge gateway
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
//! Circuit breaker wrapper for tool dispatchers.
//!
//! Prevents cascade failures by tracking consecutive errors per-server and
//! temporarily rejecting calls when the failure threshold is exceeded.

use std::sync::Arc;
use std::time::{Duration, Instant};

use forge_error::DispatchError;
use forge_sandbox::{ResourceDispatcher, ToolDispatcher};
use serde_json::Value;
use tokio::sync::Mutex;

/// Configuration for a circuit breaker.
#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    /// Number of consecutive failures before the circuit opens.
    pub failure_threshold: u32,
    /// How long to wait before probing a tripped circuit.
    pub recovery_timeout: Duration,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 3,
            recovery_timeout: Duration::from_secs(30),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CircuitState {
    Closed,
    Open,
    HalfOpen,
}

/// Internal state for the circuit breaker (shared between tool and resource dispatchers).
pub(crate) struct CircuitBreakerState {
    pub(crate) state: CircuitState,
    pub(crate) consecutive_failures: u32,
    pub(crate) last_failure_time: Option<Instant>,
}

/// A [`ToolDispatcher`] that implements the circuit breaker pattern.
pub struct CircuitBreakerDispatcher {
    inner: Arc<dyn ToolDispatcher>,
    config: CircuitBreakerConfig,
    server_name: String,
    state: Mutex<CircuitBreakerState>,
}

impl CircuitBreakerDispatcher {
    /// Wrap an inner dispatcher with circuit breaker logic.
    pub fn new(
        inner: Arc<dyn ToolDispatcher>,
        config: CircuitBreakerConfig,
        server_name: impl Into<String>,
    ) -> Self {
        Self {
            inner,
            config,
            server_name: server_name.into(),
            state: Mutex::new(CircuitBreakerState {
                state: CircuitState::Closed,
                consecutive_failures: 0,
                last_failure_time: None,
            }),
        }
    }
}

#[async_trait::async_trait]
impl ToolDispatcher for CircuitBreakerDispatcher {
    #[tracing::instrument(skip(self, args), fields(server, tool))]
    async fn call_tool(
        &self,
        server: &str,
        tool: &str,
        args: Value,
    ) -> Result<Value, DispatchError> {
        {
            let mut st = self.state.lock().await;
            match st.state {
                CircuitState::Open => {
                    if let Some(last_fail) = st.last_failure_time {
                        if last_fail.elapsed() >= self.config.recovery_timeout {
                            st.state = CircuitState::HalfOpen;
                            tracing::info!(
                                server = %self.server_name,
                                "circuit breaker half-open, allowing probe call"
                            );
                        } else {
                            return Err(DispatchError::CircuitOpen(self.server_name.clone()));
                        }
                    }
                }
                CircuitState::HalfOpen | CircuitState::Closed => {}
            }
        }

        let result = self.inner.call_tool(server, tool, args).await;

        {
            let mut st = self.state.lock().await;
            match &result {
                Ok(_) => {
                    if st.state == CircuitState::HalfOpen {
                        tracing::info!(
                            server = %self.server_name,
                            "circuit breaker closed after successful probe"
                        );
                    }
                    st.state = CircuitState::Closed;
                    st.consecutive_failures = 0;
                    st.last_failure_time = None;
                }
                Err(e) if e.trips_circuit_breaker() => {
                    st.consecutive_failures += 1;
                    st.last_failure_time = Some(Instant::now());
                    if st.state == CircuitState::HalfOpen {
                        st.state = CircuitState::Open;
                        tracing::warn!(
                            server = %self.server_name,
                            "circuit breaker re-opened after failed probe"
                        );
                    } else if st.consecutive_failures >= self.config.failure_threshold {
                        st.state = CircuitState::Open;
                        tracing::warn!(
                            server = %self.server_name,
                            failures = st.consecutive_failures,
                            "circuit breaker opened"
                        );
                    }
                }
                Err(_) => {
                    // Non-server-fault error (ToolError, ToolNotFound, etc.).
                    // The server responded, so it's alive. In HalfOpen this
                    // proves the server is reachable — close the circuit.
                    if st.state == CircuitState::HalfOpen {
                        tracing::info!(
                            server = %self.server_name,
                            "circuit breaker closed: server responded (non-fault error)"
                        );
                        st.state = CircuitState::Closed;
                        st.consecutive_failures = 0;
                        st.last_failure_time = None;
                    }
                    // In Closed state: don't count, don't reset — transparent
                    // to the failure counter.
                }
            }
        }

        result
    }
}

/// A [`ResourceDispatcher`] wrapper with independent circuit breaker state.
pub struct CircuitBreakerResourceDispatcher {
    inner: Arc<dyn ResourceDispatcher>,
    server_name: String,
    config: CircuitBreakerConfig,
    state: Arc<Mutex<CircuitBreakerState>>,
}

impl CircuitBreakerResourceDispatcher {
    /// Wrap a resource dispatcher with circuit breaker logic.
    pub fn new(
        inner: Arc<dyn ResourceDispatcher>,
        config: CircuitBreakerConfig,
        server_name: impl Into<String>,
    ) -> Self {
        Self {
            inner,
            config,
            server_name: server_name.into(),
            state: Arc::new(Mutex::new(CircuitBreakerState {
                state: CircuitState::Closed,
                consecutive_failures: 0,
                last_failure_time: None,
            })),
        }
    }
}

#[async_trait::async_trait]
impl ResourceDispatcher for CircuitBreakerResourceDispatcher {
    #[tracing::instrument(skip(self), fields(server, uri))]
    async fn read_resource(
        &self,
        server: &str,
        uri: &str,
    ) -> Result<serde_json::Value, DispatchError> {
        {
            let mut st = self.state.lock().await;
            match st.state {
                CircuitState::Open => {
                    if let Some(last_fail) = st.last_failure_time {
                        if last_fail.elapsed() >= self.config.recovery_timeout {
                            st.state = CircuitState::HalfOpen;
                        } else {
                            return Err(DispatchError::CircuitOpen(self.server_name.clone()));
                        }
                    }
                }
                CircuitState::HalfOpen | CircuitState::Closed => {}
            }
        }

        let result = self.inner.read_resource(server, uri).await;

        {
            let mut st = self.state.lock().await;
            match &result {
                Ok(_) => {
                    st.state = CircuitState::Closed;
                    st.consecutive_failures = 0;
                    st.last_failure_time = None;
                }
                Err(e) if e.trips_circuit_breaker() => {
                    st.consecutive_failures += 1;
                    st.last_failure_time = Some(Instant::now());
                    if st.state == CircuitState::HalfOpen
                        || st.consecutive_failures >= self.config.failure_threshold
                    {
                        st.state = CircuitState::Open;
                    }
                }
                Err(_) => {
                    if st.state == CircuitState::HalfOpen {
                        st.state = CircuitState::Closed;
                        st.consecutive_failures = 0;
                        st.last_failure_time = None;
                    }
                }
            }
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    struct OkDispatcher;

    #[async_trait::async_trait]
    impl ToolDispatcher for OkDispatcher {
        async fn call_tool(
            &self,
            _server: &str,
            tool: &str,
            _args: Value,
        ) -> Result<Value, DispatchError> {
            Ok(serde_json::json!({"tool": tool, "status": "ok"}))
        }
    }

    struct FailDispatcher {
        calls: AtomicUsize,
    }

    impl FailDispatcher {
        fn new() -> Self {
            Self {
                calls: AtomicUsize::new(0),
            }
        }
        fn call_count(&self) -> usize {
            self.calls.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl ToolDispatcher for FailDispatcher {
        async fn call_tool(
            &self,
            _server: &str,
            _tool: &str,
            _args: Value,
        ) -> Result<Value, DispatchError> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Err(DispatchError::Timeout {
                server: "s".into(),
                timeout_ms: 5000,
            })
        }
    }

    /// Dispatcher that fails N times then succeeds.
    struct FailThenOkDispatcher {
        calls: AtomicUsize,
        fail_count: usize,
    }

    #[async_trait::async_trait]
    impl ToolDispatcher for FailThenOkDispatcher {
        async fn call_tool(
            &self,
            _server: &str,
            tool: &str,
            _args: Value,
        ) -> Result<Value, DispatchError> {
            let n = self.calls.fetch_add(1, Ordering::SeqCst);
            if n < self.fail_count {
                Err(DispatchError::Timeout {
                    server: "s".into(),
                    timeout_ms: 5000,
                })
            } else {
                Ok(serde_json::json!({"tool": tool, "status": "ok"}))
            }
        }
    }

    fn test_config(threshold: u32, recovery_ms: u64) -> CircuitBreakerConfig {
        CircuitBreakerConfig {
            failure_threshold: threshold,
            recovery_timeout: Duration::from_millis(recovery_ms),
        }
    }

    #[tokio::test]
    async fn passes_through_on_success() {
        let inner = Arc::new(OkDispatcher);
        let cb = CircuitBreakerDispatcher::new(inner, test_config(3, 1000), "test");
        let result = cb.call_tool("test", "echo", serde_json::json!({})).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap()["status"], "ok");
    }

    #[tokio::test]
    async fn opens_after_threshold_failures() {
        let inner = Arc::new(FailDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner.clone(), test_config(3, 60_000), "flaky");

        for _ in 0..3 {
            let _ = cb.call_tool("flaky", "tool", serde_json::json!({})).await;
        }
        assert_eq!(inner.call_count(), 3);

        let result = cb.call_tool("flaky", "tool", serde_json::json!({})).await;
        assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));
        assert_eq!(
            inner.call_count(),
            3,
            "inner should not be called when open"
        );
    }

    #[tokio::test]
    async fn rejects_when_open() {
        let inner = Arc::new(FailDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner.clone(), test_config(2, 60_000), "s");

        for _ in 0..2 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        for _ in 0..5 {
            let result = cb.call_tool("s", "t", serde_json::json!({})).await;
            assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));
        }
        assert_eq!(
            inner.call_count(),
            2,
            "no additional calls should reach inner"
        );
    }

    #[tokio::test]
    async fn half_open_after_recovery_timeout() {
        let inner = Arc::new(FailThenOkDispatcher {
            calls: AtomicUsize::new(0),
            fail_count: 3,
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(3, 50), "s");

        for _ in 0..3 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_err());

        tokio::time::sleep(Duration::from_millis(60)).await;

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_ok(), "probe should succeed after recovery");
    }

    #[tokio::test]
    async fn probe_failure_reopens_circuit() {
        let inner = Arc::new(FailDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner.clone(), test_config(2, 50), "s");

        for _ in 0..2 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        tokio::time::sleep(Duration::from_millis(60)).await;

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_err());

        let before = inner.call_count();
        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));
        assert_eq!(
            inner.call_count(),
            before,
            "should not reach inner after probe failure"
        );
    }

    #[tokio::test]
    async fn success_resets_failure_counter() {
        let inner = Arc::new(FailThenOkDispatcher {
            calls: AtomicUsize::new(0),
            fail_count: 2,
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(3, 60_000), "s");

        let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        let _ = cb.call_tool("s", "t", serde_json::json!({})).await;

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_ok());

        let st = cb.state.lock().await;
        assert_eq!(st.state, CircuitState::Closed);
        assert_eq!(st.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn error_message_includes_server_and_failure_count() {
        let inner = Arc::new(FailDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner, test_config(2, 60_000), "my-server");

        for _ in 0..2 {
            let _ = cb
                .call_tool("my-server", "tool", serde_json::json!({}))
                .await;
        }

        let err = cb
            .call_tool("my-server", "tool", serde_json::json!({}))
            .await
            .unwrap_err();
        assert!(matches!(err, DispatchError::CircuitOpen(ref s) if s == "my-server"));
    }

    // --- v0.2 Resource Circuit Breaker Test (RS-C08) ---

    struct FailResourceDispatcher;

    #[async_trait::async_trait]
    impl ResourceDispatcher for FailResourceDispatcher {
        async fn read_resource(&self, _server: &str, _uri: &str) -> Result<Value, DispatchError> {
            Err(DispatchError::Timeout {
                server: "flaky".into(),
                timeout_ms: 5000,
            })
        }
    }

    #[tokio::test]
    async fn rs_c08_circuit_breaker_trips_on_repeated_resource_failures() {
        let inner: Arc<dyn ResourceDispatcher> = Arc::new(FailResourceDispatcher);
        let cb = CircuitBreakerResourceDispatcher::new(inner, test_config(2, 60_000), "flaky");

        for _ in 0..2 {
            let _ = cb.read_resource("flaky", "file:///log").await;
        }

        let result = cb.read_resource("flaky", "file:///log").await;
        assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));
    }

    #[tokio::test]
    async fn probe_success_closes_circuit() {
        let inner = Arc::new(FailThenOkDispatcher {
            calls: AtomicUsize::new(0),
            fail_count: 2,
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(2, 50), "s");

        for _ in 0..2 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        tokio::time::sleep(Duration::from_millis(60)).await;

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_ok());

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_ok());
    }

    // --- Error classification tests (circuit breaker fix) ---

    /// Dispatcher that returns ToolError (non-server-fault).
    struct ToolErrorDispatcher {
        calls: AtomicUsize,
    }

    impl ToolErrorDispatcher {
        fn new() -> Self {
            Self {
                calls: AtomicUsize::new(0),
            }
        }
        fn call_count(&self) -> usize {
            self.calls.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl ToolDispatcher for ToolErrorDispatcher {
        async fn call_tool(
            &self,
            _server: &str,
            _tool: &str,
            _args: Value,
        ) -> Result<Value, DispatchError> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Err(DispatchError::ToolError {
                server: "s".into(),
                tool: "scan".into(),
                message: "Invalid params: missing field 'base_url'".into(),
            })
        }
    }

    #[async_trait::async_trait]
    impl ResourceDispatcher for ToolErrorDispatcher {
        async fn read_resource(&self, _server: &str, _uri: &str) -> Result<Value, DispatchError> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Err(DispatchError::ToolError {
                server: "s".into(),
                tool: "read".into(),
                message: "Invalid params".into(),
            })
        }
    }

    /// Dispatcher that returns a configurable sequence of results.
    struct SequencedDispatcher {
        /// Each entry: true = server fault (Timeout), false = client error (ToolError)
        sequence: Vec<Option<bool>>,
        calls: AtomicUsize,
    }

    #[async_trait::async_trait]
    impl ToolDispatcher for SequencedDispatcher {
        async fn call_tool(
            &self,
            _server: &str,
            tool: &str,
            _args: Value,
        ) -> Result<Value, DispatchError> {
            let n = self.calls.fetch_add(1, Ordering::SeqCst);
            match self.sequence.get(n) {
                Some(Some(true)) => Err(DispatchError::Timeout {
                    server: "s".into(),
                    timeout_ms: 5000,
                }),
                Some(Some(false)) => Err(DispatchError::ToolError {
                    server: "s".into(),
                    tool: tool.into(),
                    message: "bad params".into(),
                }),
                Some(None) => Ok(serde_json::json!({"tool": tool, "ok": true})),
                None => Ok(serde_json::json!({"tool": tool, "ok": true})),
            }
        }
    }

    #[tokio::test]
    async fn tool_error_does_not_count_toward_threshold() {
        let inner = Arc::new(ToolErrorDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner.clone(), test_config(3, 60_000), "arbiter");

        // 10 tool errors — circuit should stay closed
        for _ in 0..10 {
            let result = cb.call_tool("arbiter", "scan", serde_json::json!({})).await;
            assert!(result.is_err());
            assert!(
                matches!(result, Err(DispatchError::ToolError { .. })),
                "expected ToolError"
            );
        }

        // All 10 calls reached the inner dispatcher (circuit never opened)
        assert_eq!(inner.call_count(), 10);

        // Verify circuit is still closed
        let st = cb.state.lock().await;
        assert_eq!(st.state, CircuitState::Closed);
        assert_eq!(st.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn timeout_still_trips_after_threshold() {
        // Regression: server faults still trip the breaker
        let inner = Arc::new(FailDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner.clone(), test_config(3, 60_000), "s");

        for _ in 0..3 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));
        assert_eq!(inner.call_count(), 3);
    }

    #[tokio::test]
    async fn mixed_errors_only_server_faults_count() {
        // Sequence: ToolError, Timeout, ToolError, Timeout, ToolError, Timeout
        // Only 3 Timeouts → should open
        let inner = Arc::new(SequencedDispatcher {
            sequence: vec![
                Some(false), // ToolError
                Some(true),  // Timeout → failures=1
                Some(false), // ToolError
                Some(true),  // Timeout → failures=2
                Some(false), // ToolError
                Some(true),  // Timeout → failures=3 → OPEN
            ],
            calls: AtomicUsize::new(0),
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(3, 60_000), "s");

        for _ in 0..6 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        // 7th call should hit open circuit
        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(
            matches!(result, Err(DispatchError::CircuitOpen(_))),
            "expected CircuitOpen after 3 timeouts, got: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn client_error_preserves_failure_counter() {
        // Sequence: Timeout, ToolError, Timeout
        // The ToolError should NOT reset the failure counter
        let inner = Arc::new(SequencedDispatcher {
            sequence: vec![
                Some(true),  // Timeout → failures=1
                Some(false), // ToolError → failures stays 1
                Some(true),  // Timeout → failures=2
            ],
            calls: AtomicUsize::new(0),
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(3, 60_000), "s");

        for _ in 0..3 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        let st = cb.state.lock().await;
        assert_eq!(
            st.consecutive_failures, 2,
            "ToolError should not reset counter"
        );
        assert_eq!(st.state, CircuitState::Closed);
    }

    #[tokio::test]
    async fn success_still_resets_counter_after_tool_errors() {
        // Sequence: Timeout, ToolError, Ok
        // The Ok should reset the failure counter
        let inner = Arc::new(SequencedDispatcher {
            sequence: vec![
                Some(true),  // Timeout → failures=1
                Some(false), // ToolError → failures stays 1
                None,        // Ok → failures=0
            ],
            calls: AtomicUsize::new(0),
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(3, 60_000), "s");

        for _ in 0..3 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        let st = cb.state.lock().await;
        assert_eq!(st.consecutive_failures, 0);
        assert_eq!(st.state, CircuitState::Closed);
    }

    #[tokio::test]
    async fn half_open_probe_tool_error_closes_circuit() {
        // Trip the breaker, wait for HalfOpen, then probe with ToolError.
        // Server responded → circuit should close.
        let inner = Arc::new(SequencedDispatcher {
            sequence: vec![
                Some(true),  // Timeout → failures=1
                Some(true),  // Timeout → failures=2 → OPEN
                Some(false), // ToolError probe → server alive → CLOSE
                None,        // Ok (verify circuit is closed)
            ],
            calls: AtomicUsize::new(0),
        });
        let cb = CircuitBreakerDispatcher::new(inner, test_config(2, 50), "s");

        // Trip the breaker
        let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        let _ = cb.call_tool("s", "t", serde_json::json!({})).await;

        // Verify open
        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));

        // Wait for recovery
        tokio::time::sleep(Duration::from_millis(60)).await;

        // Probe returns ToolError → server is alive → close circuit
        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(
            matches!(result, Err(DispatchError::ToolError { .. })),
            "probe should return ToolError, got: {:?}",
            result
        );

        // Circuit should be closed now
        let st = cb.state.lock().await;
        assert_eq!(st.state, CircuitState::Closed);
        assert_eq!(st.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn half_open_probe_timeout_reopens_circuit() {
        // Regression: server faults in HalfOpen still reopen
        let inner = Arc::new(FailDispatcher::new());
        let cb = CircuitBreakerDispatcher::new(inner, test_config(2, 50), "s");

        for _ in 0..2 {
            let _ = cb.call_tool("s", "t", serde_json::json!({})).await;
        }

        tokio::time::sleep(Duration::from_millis(60)).await;

        // Probe fails with Timeout → reopen
        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(result.is_err());

        // Should be open again
        let result = cb.call_tool("s", "t", serde_json::json!({})).await;
        assert!(matches!(result, Err(DispatchError::CircuitOpen(_))));
    }

    #[tokio::test]
    async fn resource_tool_error_does_not_trip_breaker() {
        let inner: Arc<dyn ResourceDispatcher> = Arc::new(ToolErrorDispatcher::new());
        let cb = CircuitBreakerResourceDispatcher::new(inner, test_config(2, 60_000), "s");

        // 5 tool errors — circuit should stay closed
        for _ in 0..5 {
            let result = cb.read_resource("s", "file:///log").await;
            assert!(matches!(result, Err(DispatchError::ToolError { .. })));
        }

        let st = cb.state.lock().await;
        assert_eq!(st.state, CircuitState::Closed);
        assert_eq!(st.consecutive_failures, 0);
    }
}