fastmcp-server 0.13.0

MCP server implementation for FastMCP
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
//! Request-bound waiting on an admitted blocking worker, not a private runtime.

use std::cell::RefCell;
use std::future::Future;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll, Wake, Waker};
use std::time::Duration;

use asupersync::{Cx, channel::oneshot};
use fastmcp_core::{McpContext, McpError, McpResult};

use super::{BlockingHandlerLane, LaneInner};

// Periodic liveness checks also observe tightened budgets and closed request
// leases, which need not wake the operation. This is not a new deadline clock.
const LIVENESS_INTERVAL: Duration = Duration::from_millis(10);

struct ActiveWorker {
    lane: Arc<LaneInner>,
    context: McpContext,
    waiting: bool,
}

thread_local! {
    static ACTIVE_WORKER: RefCell<Option<ActiveWorker>> = const { RefCell::new(None) };
}

/// Only the adapter's verified pool closure can install this scope. A public
/// core blocking-lane declaration alone does not admit use of this bridge.
pub(super) struct WorkerScope {
    previous: Option<ActiveWorker>,
    // TLS guards must neither move to nor be shared with another thread.
    _thread_bound: PhantomData<Rc<()>>,
}

impl WorkerScope {
    pub(super) fn enter(lane: Arc<LaneInner>, context: McpContext) -> Self {
        Self {
            previous: ACTIVE_WORKER.with(|slot| {
                slot.replace(Some(ActiveWorker {
                    lane,
                    context,
                    waiting: false,
                }))
            }),
            _thread_bound: PhantomData,
        }
    }
}

impl Drop for WorkerScope {
    fn drop(&mut self) {
        ACTIVE_WORKER.with(|slot| {
            slot.replace(self.previous.take());
        });
    }
}

struct WaitEntry {
    context: McpContext,
    _thread_bound: PhantomData<Rc<()>>,
}

impl WaitEntry {
    fn enter(lane: &BlockingHandlerLane) -> McpResult<Self> {
        lane.verify()?;
        ACTIVE_WORKER.with(|slot| {
            let mut slot = slot.borrow_mut();
            let worker = slot.as_mut().ok_or_else(|| {
                McpError::invalid_request(
                    "blocking wait requires an admitted blocking handler worker",
                )
            })?;
            if !Arc::ptr_eq(&worker.lane, &lane.inner) {
                return Err(McpError::invalid_request(
                    "blocking wait belongs to a different handler lane",
                ));
            }
            if worker.waiting {
                return Err(McpError::invalid_request(
                    "nested blocking handler waits are not supported",
                ));
            }
            worker
                .context
                .ensure_live()
                .map_err(|_| McpError::request_cancelled())?;
            let context = worker.context.clone();
            worker.waiting = true;
            Ok(Self {
                context,
                _thread_bound: PhantomData,
            })
        })
    }
}

impl Drop for WaitEntry {
    fn drop(&mut self) {
        ACTIVE_WORKER.with(|slot| {
            if let Some(worker) = slot.borrow_mut().as_mut() {
                worker.waiting = false;
            }
        });
    }
}

#[derive(Default)]
struct WakeSignal {
    notified: Mutex<bool>,
    changed: Condvar,
}

impl WakeSignal {
    fn notify(&self) {
        *self
            .notified
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = true;
        self.changed.notify_one();
    }

    fn wait(&self) {
        let mut notified = self
            .notified
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if !*notified {
            let (guard, _) = self
                .changed
                .wait_timeout(notified, LIVENESS_INTERVAL)
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            notified = guard;
        }
        *notified = false;
    }
}

impl Wake for WakeSignal {
    fn wake(self: Arc<Self>) {
        self.notify();
    }
    fn wake_by_ref(self: &Arc<Self>) {
        self.notify();
    }
}

// A cancellation wake or a periodic liveness check does not make application
// work ready. Keep that distinction even when wake notifications coalesce.
// Clear readiness BEFORE polling so a wake from inside poll remains pending.
struct OperationWake {
    ready: AtomicBool,
    signal: Arc<WakeSignal>,
}

impl OperationWake {
    fn new(signal: Arc<WakeSignal>) -> Self {
        Self {
            ready: AtomicBool::new(true),
            signal,
        }
    }

    fn take_ready(&self) -> bool {
        self.ready.swap(false, Ordering::AcqRel)
    }

    fn notify(&self) {
        self.ready.store(true, Ordering::Release);
        self.signal.notify();
    }
}

impl Wake for OperationWake {
    fn wake(self: Arc<Self>) {
        self.notify();
    }

    fn wake_by_ref(self: &Arc<Self>) {
        self.notify();
    }
}

impl BlockingHandlerLane {
    /// Waits for one asynchronous operation inside a handler admitted to this
    /// lane. For example, a synchronous tool can call
    /// `self.lane.wait_for(ctx.sample("Summarize this result", 128))`.
    ///
    /// This uses the current worker's retained request context. It creates no
    /// runtime, reactor, timer driver, thread, or fresh request budget. The
    /// caller must keep driving its runtime, including any peer response pump.
    /// Synchronous tools, resources, prompts, and completion handlers registered
    /// through this lane's adapters can use the same operation.
    ///
    /// Request/runtime cancellation, deadline expiry, and lease closure are
    /// observed before and after polling, even when the operation never wakes.
    /// Each application poll also consumes one checkpoint from the retained,
    /// clone-shared request budget. Exhaustion refuses the next poll, not the
    /// result of the last admitted poll; later waits cannot replenish quota.
    /// After the first poll, only the operation's own waker makes it ready to
    /// poll again. Maintenance checks do not spend application poll quota.
    /// Cancellation drops the operation on this worker; the lane remains
    /// charged until the enclosing handler and result custody actually end.
    /// The future's poll and drop implementations must not block. Neither this
    /// bridge nor the adapter can preempt a blocking syscall or destructor.
    ///
    /// Do not await work which requires another slot on an exhausted blocking
    /// pool. Only independent caller-driven operations can make progress while
    /// this worker waits. Closing lane admission does not cancel existing work.
    ///
    /// # Errors
    ///
    /// Rejects use outside this lane's admitted worker, from a different lane,
    /// or recursively inside another wait, without polling the supplied future.
    /// Returns request cancellation if its retained context is no longer live
    /// or cannot admit another poll, and otherwise preserves the operation's
    /// exact result or error.
    pub fn wait_for<T>(&self, operation: impl Future<Output = McpResult<T>>) -> McpResult<T> {
        let entry = WaitEntry::enter(self)?;
        let ctx = &entry.context;
        let _current = Cx::set_current(Some(ctx.cx().clone()));
        // Bind futures after the scope and ambient context so their cleanup
        // runs before either is released, including when polling panics.
        let mut operation = std::pin::pin!(operation);
        let mut request_cancelled = std::pin::pin!(ctx.request_cancelled());
        let (_sender, mut receiver) = oneshot::channel::<()>();
        let mut runtime_cancelled = std::pin::pin!(receiver.recv(ctx.cx()));
        let signal = Arc::new(WakeSignal::default());
        let control_waker = Waker::from(Arc::clone(&signal));
        let mut control_task = Context::from_waker(&control_waker);
        let operation_wake = Arc::new(OperationWake::new(Arc::clone(&signal)));
        let operation_waker = Waker::from(Arc::clone(&operation_wake));
        let mut operation_task = Context::from_waker(&operation_waker);

        loop {
            self.verify()?;
            ctx.ensure_live()
                .map_err(|_| McpError::request_cancelled())?;
            if request_cancelled
                .as_mut()
                .poll(&mut control_task)
                .is_ready()
                || runtime_cancelled
                    .as_mut()
                    .poll(&mut control_task)
                    .is_ready()
            {
                return Err(McpError::request_cancelled());
            }
            if operation_wake.take_ready() {
                // Liveness alone deliberately does not consume poll quota.
                // Admit application work through the request ledger, not
                // Cx::checkpoint, which can cancel a shared runtime context.
                ctx.checkpoint()
                    .map_err(|_| McpError::request_cancelled())?;
                let result = operation.as_mut().poll(&mut operation_task);
                ctx.ensure_live()
                    .map_err(|_| McpError::request_cancelled())?;
                if let Poll::Ready(result) = result {
                    return result;
                }
            }
            // A private condition variable avoids consuming another bridge's
            // thread::park token. Its retained bit also covers wake-before-wait.
            signal.wait();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use asupersync::time::Sleep;
    use std::future::{pending, poll_fn};
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn runtime() -> asupersync::runtime::Runtime {
        asupersync::runtime::RuntimeBuilder::current_thread()
            .with_reactor(asupersync::runtime::reactor::create_reactor().unwrap())
            .blocking_threads(1, 1)
            .build()
            .unwrap()
    }

    fn cleanup_context(cx: &Cx) -> McpContext {
        McpContext::new(cx.clone(), 99)
            .with_operation_deadline(Some(cx.now().saturating_add_nanos(5_000_000_000)))
    }

    #[test]
    fn wait_uses_the_admitted_context_and_the_callers_timer_driver() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let poller = std::thread::current().id();
            let result = lane
                .execute(&ctx, &cx, move |worker_ctx| {
                    assert_ne!(std::thread::current().id(), poller);
                    let expected_task = worker_ctx.task_id();
                    let borrowed = String::from("borrowed result");
                    admitted.wait_for(async {
                        assert_eq!(Cx::current().unwrap().task_id(), expected_task);
                        Sleep::new(worker_ctx.cx().now().saturating_add_nanos(1_000_000)).await;
                        assert_eq!(Cx::current().unwrap().task_id(), expected_task);
                        assert_eq!(worker_ctx.request_id(), 7);
                        Ok(borrowed.len())
                    })
                })
                .await
                .unwrap();
            assert_eq!(result, "borrowed result".len());
            assert_eq!(lane.in_flight().unwrap(), 0);
            assert!(ctx.ensure_live().is_ok());
        });
    }

    #[test]
    fn driver_wrong_lane_and_nested_waits_refuse_without_polling() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let polls = Arc::new(AtomicUsize::new(0));
            // Even manually declaring a core blocking lane grants no worker.
            let declared = fastmcp_core::runtime::enter_blocking_lane();
            assert!(
                lane.wait_for(async {
                    polls.fetch_add(1, Ordering::SeqCst);
                    Ok(1)
                })
                .is_err()
            );
            assert_eq!(polls.load(Ordering::SeqCst), 0);
            drop(declared);
            let admitted = lane.clone();
            let other = BlockingHandlerLane::new(1).unwrap();
            let observed = Arc::clone(&polls);
            let result = lane
                .execute(&ctx, &cx, move |_| {
                    assert!(
                        other
                            .wait_for(async {
                                observed.fetch_add(1, Ordering::SeqCst);
                                Ok(2)
                            })
                            .is_err()
                    );
                    admitted.wait_for(async {
                        assert!(
                            admitted
                                .wait_for(async {
                                    observed.fetch_add(1, Ordering::SeqCst);
                                    Ok(3)
                                })
                                .is_err()
                        );
                        Ok(41)
                    })
                })
                .await
                .unwrap();
            assert_eq!(result, 41);
            assert_eq!(polls.load(Ordering::SeqCst), 0);
            assert_eq!(lane.in_flight().unwrap(), 0);
        });
    }

    struct DropObserved(Arc<AtomicUsize>);
    impl Drop for DropObserved {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::SeqCst);
        }
    }

    #[test]
    fn cancellation_drops_a_non_waking_operation_and_recovers_worker_capacity() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let drops = Arc::new(AtomicUsize::new(0));
            let observed = Arc::clone(&drops);
            let (started, mut entered) = oneshot::channel::<()>();
            let mut call = Box::pin(lane.execute(&ctx, &cx, move |_| {
                let result = admitted.wait_for(async move {
                    let _drop = DropObserved(observed);
                    started.send_blocking(()).unwrap();
                    pending::<McpResult<()>>().await
                });
                assert_eq!(
                    admitted.in_flight().unwrap(),
                    1,
                    "the enclosing worker still owns its reservation"
                );
                result
            }));
            poll_fn(|task| {
                assert!(call.as_mut().poll(task).is_pending());
                Poll::Ready(())
            })
            .await;
            entered.recv(&cx).await.unwrap();
            ctx.request_cancellation().cancel();
            assert!(call.await.is_err());
            let sibling = cleanup_context(&cx);
            lane.wait_idle(&sibling).await.unwrap();
            assert_eq!(drops.load(Ordering::SeqCst), 1);
            assert_eq!(lane.in_flight().unwrap(), 0);
            assert_eq!(lane.execute(&sibling, &cx, |_| Ok(42)).await.unwrap(), 42);
            assert!(sibling.ensure_live().is_ok());
        });
    }

    #[test]
    fn abandoning_the_async_call_cancels_its_waiting_worker_not_a_sibling() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let drops = Arc::new(AtomicUsize::new(0));
            let observed = Arc::clone(&drops);
            let (started, mut entered) = oneshot::channel::<()>();
            let mut call = Box::pin(lane.execute(&ctx, &cx, move |_| {
                admitted.wait_for(async move {
                    let _drop = DropObserved(observed);
                    started.send_blocking(()).unwrap();
                    pending::<McpResult<()>>().await
                })
            }));
            poll_fn(|task| {
                assert!(call.as_mut().poll(task).is_pending());
                Poll::Ready(())
            })
            .await;
            entered.recv(&cx).await.unwrap();
            drop(call);
            let sibling = cleanup_context(&cx);
            lane.wait_idle(&sibling).await.unwrap();
            assert_eq!(drops.load(Ordering::SeqCst), 1);
            assert_eq!(lane.in_flight().unwrap(), 0);
            assert_eq!(lane.execute(&sibling, &cx, |_| Ok(43)).await.unwrap(), 43);
            assert!(
                ctx.ensure_live().is_ok(),
                "worker abort must not cancel the calling context"
            );
            assert!(sibling.ensure_live().is_ok());
        });
    }

    #[test]
    fn deadline_stops_a_non_waking_wait_without_releasing_capacity_early() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7)
                .with_operation_deadline(Some(cx.now().saturating_add_nanos(1_000_000_000)));
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let drops = Arc::new(AtomicUsize::new(0));
            let observed = Arc::clone(&drops);
            let result = lane
                .execute(&ctx, &cx, move |_| {
                    let result = admitted.wait_for(async move {
                        let _drop = DropObserved(observed);
                        pending::<McpResult<()>>().await
                    });
                    assert_eq!(admitted.in_flight().unwrap(), 1);
                    result
                })
                .await;
            assert!(result.is_err());
            lane.wait_idle(&cleanup_context(&cx)).await.unwrap();
            assert_eq!(drops.load(Ordering::SeqCst), 1);
            assert_eq!(lane.in_flight().unwrap(), 0);
            assert!(
                cx.checkpoint().is_ok(),
                "request deadline must not cancel the caller"
            );
        });
    }

    #[test]
    fn cancellation_during_poll_discards_the_ready_value_and_unpolled_work() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let drops = Arc::new(AtomicUsize::new(0));
            let observed = Arc::clone(&drops);
            let polls = Arc::new(AtomicUsize::new(0));
            let forbidden = Arc::clone(&polls);
            assert!(
                lane.execute(&ctx, &cx, move |worker_ctx| {
                    assert!(
                        admitted
                            .wait_for(async {
                                worker_ctx.request_cancellation().cancel();
                                Ok(DropObserved(observed))
                            })
                            .is_err()
                    );
                    assert!(
                        admitted
                            .wait_for(async {
                                forbidden.fetch_add(1, Ordering::SeqCst);
                                Ok(1)
                            })
                            .is_err()
                    );
                    Ok(())
                })
                .await
                .is_err()
            );
            lane.wait_idle(&cleanup_context(&cx)).await.unwrap();
            assert_eq!(drops.load(Ordering::SeqCst), 1);
            assert_eq!(polls.load(Ordering::SeqCst), 0);
        });
    }

    #[test]
    fn wait_scope_unwinds_without_leaking_to_reused_workers() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let error = lane
                .execute::<(), _>(&ctx, &cx, move |_| {
                    admitted.wait_for(async { panic!("private-wait-panic-canary") })
                })
                .await
                .unwrap_err();
            assert!(!error.to_string().contains("private-wait-panic-canary"));
            let probe_lane = lane.clone();
            let mut probe = cx
                .spawn_blocking(move |_| {
                    probe_lane
                        .wait_for(async { panic!("unadmitted future must not be polled") })
                        .map(|(): ()| ())
                })
                .unwrap();
            assert!(probe.join(&cx).await.unwrap().is_err());
            let admitted = lane.clone();
            assert_eq!(
                lane.execute(&ctx, &cx, move |_| { admitted.wait_for(async { Ok(73) }) })
                    .await
                    .unwrap(),
                73
            );
            assert_eq!(lane.in_flight().unwrap(), 0);
        });
    }

    #[test]
    fn closed_admission_allows_current_wait_and_preserves_operation_errors() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = McpContext::new(cx.clone(), 7);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let value = lane
                .execute(&ctx, &cx, move |_| {
                    admitted.close()?;
                    let error = admitted
                        .wait_for::<()>(async {
                            Err(McpError::invalid_params("preserved-operation-error"))
                        })
                        .unwrap_err();
                    assert!(error.to_string().contains("preserved-operation-error"));
                    admitted.wait_for(async { Ok(19) })
                })
                .await
                .unwrap();
            assert_eq!(value, 19);
            assert!(lane.execute(&ctx, &cx, |_| Ok(20)).await.is_err());
            assert_eq!(lane.in_flight().unwrap(), 0);
        });
    }

    #[test]
    fn wait_charges_each_application_poll_and_refuses_only_unadmitted_work() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let lane = BlockingHandlerLane::new(1).unwrap();
            for (quota, needed, expected_polls, succeeds) in [
                (0, 1, 0, false),
                (1, 1, 1, true),
                (1, 2, 1, false),
                (2, 2, 2, true),
            ] {
                let ctx = cleanup_context(&cx);
                let admitted = lane.clone();
                let (result, polls, remaining) = lane
                    .execute(&ctx, &cx, move |worker_ctx| {
                        // Tighten AFTER pool admission so the case measures
                        // application polling rather than spawn bookkeeping.
                        let bounded = worker_ctx
                            .clone()
                            .with_budget_ceiling(asupersync::Budget::new().with_poll_quota(quota));
                        let mut polls = 0;
                        let result = admitted.wait_for(poll_fn(|task| {
                            polls += 1;
                            if polls < needed {
                                task.waker().wake_by_ref();
                                Poll::Pending
                            } else {
                                Poll::Ready(Ok(41))
                            }
                        }));
                        Ok((result, polls, bounded.budget().poll_quota))
                    })
                    .await
                    .unwrap();
                assert_eq!(polls, expected_polls, "quota={quota}, needed={needed}");
                assert_eq!(remaining, quota - expected_polls);
                assert_eq!(ctx.budget().poll_quota, remaining);
                if succeeds {
                    assert_eq!(result.unwrap(), 41);
                } else {
                    assert_eq!(
                        result.unwrap_err().code,
                        fastmcp_core::McpErrorCode::RequestCancelled
                    );
                }
                assert!(!cx.is_cancel_requested());
                assert_eq!(lane.in_flight().unwrap(), 0);
                let sibling = cleanup_context(&cx);
                assert_eq!(lane.execute(&sibling, &cx, |_| Ok(42)).await.unwrap(), 42);
            }
        });
    }

    #[test]
    fn sequential_waits_cannot_replenish_the_shared_request_poll_budget() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = cleanup_context(&cx);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let values = lane
                .execute(&ctx, &cx, move |worker_ctx| {
                    let bounded = worker_ctx
                        .clone()
                        .with_budget_ceiling(asupersync::Budget::new().with_poll_quota(2));
                    let first = admitted.wait_for(async { Ok(11) })?;
                    assert_eq!(bounded.budget().poll_quota, 1);
                    let second = admitted.wait_for(async { Ok(31) })?;
                    assert_eq!(bounded.budget().poll_quota, 0);
                    let mut forbidden_polls = 0;
                    let error = admitted
                        .wait_for(async {
                            forbidden_polls += 1;
                            Ok(99)
                        })
                        .unwrap_err();
                    assert_eq!(error.code, fastmcp_core::McpErrorCode::RequestCancelled);
                    assert_eq!(forbidden_polls, 0);
                    Ok(first + second)
                })
                .await
                .unwrap();
            assert_eq!(values, 42);
            assert_eq!(ctx.budget().poll_quota, 0);
            assert!(!cx.is_cancel_requested());
            assert_eq!(lane.in_flight().unwrap(), 0);
        });
    }

    #[test]
    fn poll_exhaustion_drops_the_future_while_the_worker_still_owns_capacity() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let ctx = cleanup_context(&cx);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let admitted = lane.clone();
            let drops = Arc::new(AtomicUsize::new(0));
            let observed = Arc::clone(&drops);
            lane.execute(&ctx, &cx, move |worker_ctx| {
                let _bounded = worker_ctx
                    .clone()
                    .with_budget_ceiling(asupersync::Budget::new().with_poll_quota(1));
                let guard = DropObserved(Arc::clone(&observed));
                let mut polls = 0;
                let error = admitted
                    .wait_for(async {
                        let _guard = guard;
                        poll_fn(|task| {
                            polls += 1;
                            if polls == 1 {
                                task.waker().wake_by_ref();
                                Poll::Pending
                            } else {
                                Poll::Ready(Ok(()))
                            }
                        })
                        .await
                    })
                    .unwrap_err();
                assert_eq!(error.code, fastmcp_core::McpErrorCode::RequestCancelled);
                assert_eq!(polls, 1);
                assert_eq!(observed.load(Ordering::SeqCst), 1);
                assert_eq!(admitted.in_flight().unwrap(), 1);
                Ok(())
            })
            .await
            .unwrap();
            assert_eq!(drops.load(Ordering::SeqCst), 1);
            assert_eq!(lane.in_flight().unwrap(), 0);
            assert!(!cx.is_cancel_requested());
        });
    }

    #[test]
    fn control_wakes_do_not_spend_operation_readiness_or_lose_real_wakes() {
        let signal = Arc::new(WakeSignal::default());
        let operation = Arc::new(OperationWake::new(Arc::clone(&signal)));
        let control_waker = Waker::from(Arc::clone(&signal));
        let operation_waker = Waker::from(Arc::clone(&operation));
        assert!(operation.take_ready(), "the initial poll is admitted");
        assert!(!operation.take_ready());

        control_waker.wake_by_ref();
        signal.wait();
        assert!(
            !operation.take_ready(),
            "control activity is not application work"
        );

        // Several wakes coalesce into one ready poll, including a control wake.
        operation_waker.wake_by_ref();
        control_waker.wake_by_ref();
        operation_waker.wake_by_ref();
        signal.wait();
        assert!(operation.take_ready());
        assert!(!operation.take_ready());

        // Once readiness is taken, a wake from inside poll must survive until
        // the next iteration rather than being cleared after poll returns.
        operation_waker.wake_by_ref();
        assert!(operation.take_ready());
        operation_waker.wake_by_ref();
        signal.wait();
        assert!(operation.take_ready());
        assert!(!operation.take_ready());
    }

    #[test]
    fn idle_maintenance_preserves_poll_quota_and_tightening_precedes_woken_work() {
        runtime().block_on(async {
            let cx = Cx::current().unwrap();
            let lane = BlockingHandlerLane::new(1).unwrap();
            for tighten_before_wake in [false, true] {
                let ctx = cleanup_context(&cx);
                let admitted = lane.clone();
                let polls = Arc::new(AtomicUsize::new(0));
                let observed_polls = Arc::clone(&polls);
                let drops = Arc::new(AtomicUsize::new(0));
                let observed_drops = Arc::clone(&drops);
                let released = Arc::new(AtomicBool::new(false));
                let worker_released = Arc::clone(&released);
                let (started, mut entered) = oneshot::channel::<Waker>();
                let mut call = Box::pin(lane.execute(&ctx, &cx, move |worker_ctx| {
                    let _bounded = worker_ctx
                        .clone()
                        .with_budget_ceiling(asupersync::Budget::new().with_poll_quota(2));
                    let guard = DropObserved(observed_drops);
                    let mut started = Some(started);
                    admitted.wait_for(async {
                        let _guard = guard;
                        poll_fn(|task| {
                            observed_polls.fetch_add(1, Ordering::SeqCst);
                            if let Some(started) = started.take() {
                                started.send_blocking(task.waker().clone()).unwrap();
                            }
                            if worker_released.load(Ordering::Acquire) {
                                Poll::Ready(Ok(41))
                            } else {
                                Poll::Pending
                            }
                        })
                        .await
                    })
                }));
                poll_fn(|task| {
                    assert!(call.as_mut().poll(task).is_pending());
                    Poll::Ready(())
                })
                .await;
                let operation_waker = entered.recv(&cx).await.unwrap();
                // The handshake, not a sleep, establishes the first poll.
                // Leave the operation asleep across several maintenance ticks.
                Sleep::new(cx.now().saturating_add_nanos(100_000_000)).await;
                assert_eq!(polls.load(Ordering::SeqCst), 1);
                assert_eq!(ctx.budget().poll_quota, 1);
                assert_eq!(drops.load(Ordering::SeqCst), 0);
                if tighten_before_wake {
                    let _tightened = ctx
                        .clone()
                        .with_budget_ceiling(asupersync::Budget::new().with_poll_quota(0));
                }
                released.store(true, Ordering::Release);
                operation_waker.wake();
                let result = call.await;
                if tighten_before_wake {
                    assert_eq!(
                        result.unwrap_err().code,
                        fastmcp_core::McpErrorCode::RequestCancelled
                    );
                    assert_eq!(polls.load(Ordering::SeqCst), 1);
                } else {
                    assert_eq!(result.unwrap(), 41);
                    assert_eq!(polls.load(Ordering::SeqCst), 2);
                }
                let sibling = cleanup_context(&cx);
                lane.wait_idle(&sibling).await.unwrap();
                assert_eq!(drops.load(Ordering::SeqCst), 1);
                assert_eq!(lane.in_flight().unwrap(), 0);
                assert!(!cx.is_cancel_requested());
                assert_eq!(lane.execute(&sibling, &cx, |_| Ok(42)).await.unwrap(), 42);
            }
        });
    }
}