lspf 0.9.1

A Rust framework for building extensible LSP language servers
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
//! Endpoint-neutral protocol session primitives.
//!
//! Correlation, admission, deadline constants, task ownership, writer
//! coordination, and close signaling live here so protocol endpoints share
//! one implementation. Endpoint lifecycle and registration policy remain in
//! their endpoint engines.

use std::collections::HashMap;
use std::future::Future;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use bytes::Bytes;
use futures_channel::mpsc::UnboundedReceiver;
use futures_util::FutureExt;
use futures_util::future::{Either, select};
use futures_util::select_biased;
use lsp_types::error_codes::SERVER_CANCELLED;
use tokio_util::sync::CancellationToken;
use tracing::{Instrument, Span, warn};

use crate::LspError;
use crate::client::ClientHandle;
pub(crate) use crate::client::{OutboundQueue, OutboundRegistry};
use crate::failure::{ConnectionDirection, ConnectionFailureCategory, FailureReporter};
use crate::raw::{JsonRpcError, RawMessage, RequestId};
use crate::resource_policy::ResourcePolicy;
use crate::runtime::{Runtime, TaskHandle, TaskSend};
use crate::service::{HandlerTimeout, ServiceResult};
use crate::sync::{OwnedPermit, Semaphore};
use crate::telemetry::{Completion, ConnectionTrace, Direction, Instant, Resource, ResourceAction};
use crate::transport::{TransportError, TransportReader, TransportWriter};

pub(crate) struct CloseSignal<C> {
    inner: Arc<CloseInner<C>>,
}

/// The endpoint-neutral owner of one connection's mutable protocol machinery.
///
/// Endpoint engines retain lifecycle, registration, and domain state. This
/// module owns the invariants that both directions of an LSP connection need.
pub(crate) struct ProtocolSession<R, P, C> {
    inbound: InboundRegistry,
    handler_timeout: Duration,
    tasks: TaskGroup<R>,
    out_tx: OutboundQueue,
    cancellation: CancellationToken,
    close: CloseSignal<C>,
    peer: P,
    writer_failed: fn() -> C,
    send_task: Option<TaskHandle>,
    closed: bool,
}

/// Cloneable endpoint control over shared shutdown and close mechanics.
/// Endpoint lifecycle policy remains in the endpoint that invokes it.
pub(crate) struct ProtocolControl<P: SessionPeer, C> {
    inbound: InboundRegistry,
    out_tx: OutboundQueue,
    peer: P,
    close: CloseSignal<C>,
}

impl<P: SessionPeer, C> Clone for ProtocolControl<P, C> {
    fn clone(&self) -> Self {
        Self {
            inbound: self.inbound.clone(),
            out_tx: self.out_tx.clone(),
            peer: self.peer.clone(),
            close: self.close.clone(),
        }
    }
}

impl<P: SessionPeer, C> ProtocolControl<P, C> {
    pub(crate) fn successful_shutdown(&self) {
        self.inbound.cancel_all_with_response(&self.out_tx);
        self.peer.close_pending();
    }

    pub(crate) fn request_close(&self, cause: C) {
        self.close.request(cause);
    }
}

#[derive(Clone)]
pub(crate) struct CompletionGate {
    inbound: InboundRegistry,
    outbound: OutboundQueue,
}

pub(crate) enum SessionInput {
    CloseRequested,
    OutboundFailed,
    Message(std::result::Result<RawMessage, TransportError>),
}

impl CompletionGate {
    pub(crate) fn complete(&self, reservation: Reservation, result: Result<Bytes, LspError>) {
        self.inbound.complete(&self.outbound, reservation, result);
    }
}

impl<R: Runtime, P: SessionPeer, C> ProtocolSession<R, P, C> {
    /// Create every piece of mutable protocol machinery for one connection.
    pub(crate) fn start<W, F>(
        runtime: R,
        policy: ResourcePolicy,
        writer: W,
        trace: ConnectionTrace,
        connection_span: Span,
        failure_reporter: FailureReporter,
        writer_failed: fn() -> C,
        make_peer: F,
    ) -> (Self, P)
    where
        W: TransportWriter + 'static,
        P: TaskSend + 'static,
        C: TaskSend + 'static,
        F: FnOnce(OutboundQueue, OutboundRegistry, Option<Duration>) -> P,
    {
        let (out_tx, out_rx) = OutboundQueue::bounded_with_reporter(
            policy.max_outbound_messages,
            policy.max_outbound_bytes,
            trace,
            failure_reporter.clone(),
        );
        let peer = make_peer(
            out_tx.clone(),
            OutboundRegistry::default(),
            policy.outbound_request_timeout,
        );
        let close = CloseSignal::new();
        let send_task = runtime.spawn(
            send_loop_with_trace(
                writer,
                out_rx,
                peer.clone(),
                close.clone(),
                writer_failed,
                trace,
                failure_reporter.clone(),
            )
            .instrument(connection_span),
        );
        let session = Self {
            inbound: InboundRegistry::new_with_reporter(
                policy.max_inbound_requests,
                trace,
                failure_reporter,
            ),
            handler_timeout: policy.handler_timeout,
            tasks: TaskGroup::new(runtime),
            out_tx,
            cancellation: CancellationToken::new(),
            close,
            peer: peer.clone(),
            writer_failed,
            send_task: Some(send_task),
            closed: false,
        };
        (session, peer)
    }

    /// Run the one idempotent protocol close operation.
    pub(crate) async fn close(&mut self) {
        if self.closed {
            return;
        }
        self.closed = true;
        self.peer.close_connection();
        self.cancellation.cancel();
        self.peer.close_pending();
        self.inbound.close_all();
        self.peer.clear_endpoint_registries();
        self.tasks.abort_and_join().await;
        self.peer.close_outbound();
        if let Some(send_task) = self.send_task.take() {
            send_task.join().await;
        }
    }

    pub(crate) fn close_requested(&self) -> CancellationToken {
        self.close.requested()
    }

    pub(crate) fn control(&self) -> ProtocolControl<P, C> {
        ProtocolControl {
            inbound: self.inbound.clone(),
            out_tx: self.out_tx.clone(),
            peer: self.peer.clone(),
            close: self.close.clone(),
        }
    }

    pub(crate) fn outbound_failed(&self) -> CancellationToken {
        self.out_tx.failure()
    }

    pub(crate) fn request_close(&self, cause: C) {
        self.close.request(cause);
    }

    pub(crate) fn take_close_cause(&self) -> Option<C> {
        self.close.take_cause()
    }

    pub(crate) fn reject_inbound(&self, id: RequestId, error: LspError) {
        let _ = self.out_tx.send_required(error_response(id, &error));
    }

    pub(crate) fn complete_cancelled(&self, reservation: Reservation) {
        enqueue_encoded(
            &self.out_tx,
            reservation.id,
            Err(LspError::RequestCancelled),
        );
    }

    pub(crate) fn send_protocol_error(&self, error: JsonRpcError) {
        let _ = self
            .out_tx
            .send_required(RawMessage::ProtocolError { error });
    }

    pub(crate) fn handler_timeout(&self) -> Duration {
        self.handler_timeout
    }

    pub(crate) fn cancellation_child(&self) -> CancellationToken {
        self.cancellation.child_token()
    }

    pub(crate) fn reserve_inbound(
        &self,
        id: RequestId,
        method: &str,
        cancellable: bool,
    ) -> Result<ReservedRequest, InboundReserveError> {
        let parent = cancellable.then_some(&self.cancellation);
        self.inbound.reserve_method(id, method, parent)
    }

    pub(crate) fn complete_inbound(
        &self,
        reservation: Reservation,
        result: Result<Bytes, LspError>,
    ) {
        self.inbound.complete(&self.out_tx, reservation, result);
    }

    pub(crate) fn cancel_inbound(&self, id: &RequestId) -> Option<Reservation> {
        self.inbound.claim_cancellation(id)
    }

    pub(crate) fn cancel_all_inbound_with_response(&self) {
        self.inbound.cancel_all_with_response(&self.out_tx);
    }

    pub(crate) fn completion_gate(&self) -> CompletionGate {
        CompletionGate {
            inbound: self.inbound.clone(),
            outbound: self.out_tx.clone(),
        }
    }

    pub(crate) fn spawn<F>(&mut self, future: F, permit: Arc<OwnedPermit>)
    where
        F: Future<Output = ()> + TaskSend + 'static,
    {
        self.tasks.spawn(future, permit);
    }

    pub(crate) async fn reap_finished(&mut self) {
        self.tasks.reap_finished().await;
    }

    /// Select the next endpoint-neutral input and reap completed handler tasks
    /// immediately before returning a Transport message for dispatch.
    pub(crate) async fn next_input<Rd: TransportReader>(
        &mut self,
        reader: &mut Rd,
    ) -> SessionInput {
        let requested = self.close_requested();
        let outbound_failed = self.outbound_failed();
        let input = select_biased! {
            () = requested.cancelled().fuse() => SessionInput::CloseRequested,
            () = outbound_failed.cancelled().fuse() => SessionInput::OutboundFailed,
            message = reader.recv().fuse() => SessionInput::Message(message),
        };
        if matches!(input, SessionInput::Message(_)) {
            self.reap_finished().await;
        }
        input
    }

    /// Take the recorded close cause, applying required-writer-failure
    /// precedence after close has quiesced all connection tasks.
    pub(crate) fn final_close_cause(&self) -> C {
        let recorded = self
            .take_close_cause()
            .expect("every path out of an endpoint read-loop records its close cause");
        if self.outbound_failed().is_cancelled() {
            (self.writer_failed)()
        } else {
            recorded
        }
    }
}

impl<R, P, C> Drop for ProtocolSession<R, P, C> {
    fn drop(&mut self) {
        for task in &self.tasks.handles {
            task.handle.abort();
        }
        if let Some(send_task) = &self.send_task {
            send_task.abort();
        }
    }
}

/// Operations the shared session needs from either endpoint's peer handle.
pub(crate) trait SessionPeer: Clone {
    fn close_connection(&self);
    fn close_pending(&self);
    fn clear_endpoint_registries(&self);
    fn close_outbound(&self);
    fn outbound_closing(&self) -> CancellationToken;
    fn record_outbound_done(&self);
    fn discard_outbound(&self);
}

impl SessionPeer for ClientHandle {
    fn close_connection(&self) {
        ClientHandle::close_connection(self);
    }

    fn close_pending(&self) {
        self.outbound_registry().close_all();
    }

    fn clear_endpoint_registries(&self) {
        self.progress_registry().clear();
    }

    fn close_outbound(&self) {
        ClientHandle::close_outbound(self);
    }

    fn outbound_closing(&self) -> CancellationToken {
        ClientHandle::outbound_closing(self)
    }

    fn record_outbound_done(&self) {
        self.record_done();
    }

    fn discard_outbound(&self) {
        ClientHandle::discard_outbound(self);
    }
}

impl<C> Clone for CloseSignal<C> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

struct CloseInner<C> {
    cause: Mutex<Option<C>>,
    requested: CancellationToken,
}

impl<C> CloseSignal<C> {
    pub(crate) fn new() -> Self {
        Self {
            inner: Arc::new(CloseInner {
                cause: Mutex::new(None),
                requested: CancellationToken::new(),
            }),
        }
    }

    /// Request the one close operation. The first caller records the
    /// provisional `cause` and wakes the read-loop; a later caller leaves it
    /// untouched and observes that same close rather than starting a second
    /// one. Required outbound admission failure can override this provisional
    /// cause when the final outcome is selected (ADR 0026).
    pub(crate) fn request(&self, cause: C) {
        {
            let mut recorded = self.inner.cause.lock().unwrap();
            if recorded.is_none() {
                *recorded = Some(cause);
            }
        }
        self.inner.requested.cancel();
    }

    /// The token that fires once any caller has requested closure.
    pub(crate) fn requested(&self) -> CancellationToken {
        self.inner.requested.clone()
    }

    /// Take the recorded cause. Called once, by the read-loop, after the close
    /// operation has run.
    pub(crate) fn take_cause(&self) -> Option<C> {
        self.inner.cause.lock().unwrap().take()
    }
}

pub(crate) struct TaskGroup<R> {
    pub(crate) runtime: R,
    pub(crate) handles: Vec<InboundTask>,
}

pub(crate) struct InboundTask {
    pub(crate) handle: TaskHandle,
    _permit: Arc<OwnedPermit>,
}

#[derive(Clone, Copy, Default, Eq, PartialEq)]
struct RequestGeneration(u64);

impl RequestGeneration {
    fn take_next(&mut self) -> Self {
        let generation = *self;
        self.0 += 1;
        generation
    }
}

impl<R: Runtime> TaskGroup<R> {
    pub(crate) fn new(runtime: R) -> Self {
        Self {
            runtime,
            handles: Vec::new(),
        }
    }

    pub(crate) fn spawn<F>(&mut self, future: F, permit: Arc<OwnedPermit>)
    where
        F: Future<Output = ()> + TaskSend + 'static,
    {
        self.handles.push(InboundTask {
            handle: self.runtime.spawn(future),
            _permit: permit,
        });
    }

    pub(crate) async fn reap_finished(&mut self) {
        let mut running = Vec::with_capacity(self.handles.len());
        for task in std::mem::take(&mut self.handles) {
            if task.handle.is_finished() {
                task.handle.join().await;
            } else {
                running.push(task);
            }
        }
        self.handles = running;
    }

    pub(crate) async fn abort_and_join(&mut self) {
        for task in &self.handles {
            task.handle.abort();
        }
        self.join_all().await;
    }

    async fn join_all(&mut self) {
        for task in std::mem::take(&mut self.handles) {
            task.handle.join().await;
        }
    }
}

/// One accepted inbound request: its wire ID plus the generation that claimed
/// that ID.
///
/// A peer may legitimately reuse a request ID once the previous request with
/// that ID has been answered, so the ID alone does not identify a request for
/// the lifetime of its task. The generation makes the completion gate
/// identity-scoped: a task whose result arrives after its own entry was claimed
/// — by `$/cancelRequest`, by `shutdown`, or by session close — cannot then
/// claim the entry a later request has since reserved under the same ID.
pub(crate) struct Reservation {
    pub(crate) id: RequestId,
    method: String,
    started: Instant,
    generation: RequestGeneration,
    pub(crate) _permit: Arc<OwnedPermit>,
}

pub(crate) struct InboundEntry {
    method: String,
    started: Instant,
    generation: RequestGeneration,
    /// `None` for `initialize`, the one request that is not cancellable.
    cancellation: Option<CancellationToken>,
    _permit: Arc<OwnedPermit>,
}

pub(crate) struct InboundInner {
    pub(crate) entries: HashMap<RequestId, InboundEntry>,
    next_generation: RequestGeneration,
}

#[derive(Clone)]
pub(crate) struct InboundRegistry {
    pub(crate) inner: Arc<Mutex<InboundInner>>,
    capacity: Arc<Semaphore>,
    trace: ConnectionTrace,
    failure_reporter: FailureReporter,
    limit: usize,
}

#[derive(Debug)]
pub(crate) enum InboundReserveError {
    DuplicateId,
    CapacityExhausted,
}

pub(crate) const INBOUND_CAPACITY_EXHAUSTED: &str = "inbound request capacity exhausted";
pub(crate) const HANDLER_DEADLINE_EXPIRED: &str = "handler deadline expired";

/// Race one admitted handler against cancellation and the deadline selected
/// by the endpoint's Layer stack.
pub(crate) async fn run_handler_with_deadline<F>(
    handler: F,
    cancellation: CancellationToken,
    handler_timeout: HandlerTimeout,
) -> ServiceResult
where
    F: Future<Output = ServiceResult>,
{
    let completion = select(Box::pin(handler), Box::pin(cancellation.cancelled()));
    let result = match select(
        Box::pin(completion),
        Box::pin(handler_timeout.wait_until_armed()),
    )
    .await
    {
        Either::Left((Either::Left((result, _)), _)) => result,
        Either::Left((Either::Right(((), handler)), _)) => cooperatively_cancelled_result(handler),
        Either::Right(((), completion)) => {
            match select(
                Box::pin(completion),
                Box::pin(crate::runtime::sleep(handler_timeout.get())),
            )
            .await
            {
                Either::Left((Either::Left((result, _)), _)) => result,
                Either::Left((Either::Right(((), handler)), _)) => {
                    cooperatively_cancelled_result(handler)
                }
                Either::Right(((), completion)) => {
                    handler_timeout.finish(crate::telemetry::DeadlineAction::Expired);
                    cancellation.cancel();
                    let _ = completion.now_or_never();
                    ServiceResult::Error(LspError::ServerError {
                        code: SERVER_CANCELLED as i32,
                        message: HANDLER_DEADLINE_EXPIRED.to_string(),
                        data: None,
                    })
                }
            }
        }
    };
    handler_timeout.finish(match &result {
        ServiceResult::Error(LspError::RequestCancelled) => {
            crate::telemetry::DeadlineAction::Cancelled
        }
        ServiceResult::Error(LspError::ServerError { message, .. })
            if message == HANDLER_DEADLINE_EXPIRED =>
        {
            crate::telemetry::DeadlineAction::Expired
        }
        _ => crate::telemetry::DeadlineAction::Completed,
    });
    result
}

fn cooperatively_cancelled_result<F>(handler: F) -> ServiceResult
where
    F: Future<Output = ServiceResult>,
{
    let _ = handler.now_or_never();
    ServiceResult::Error(LspError::RequestCancelled)
}

pub(crate) struct ReservedRequest {
    pub(crate) reservation: Reservation,
    pub(crate) cancellation: Option<CancellationToken>,
}

impl InboundRegistry {
    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub(crate) fn new(capacity: usize) -> Self {
        let trace = ConnectionTrace::new();
        Self::new_with_reporter(capacity, trace, FailureReporter::new(None, trace.id()))
    }

    pub(crate) fn new_with_reporter(
        capacity: usize,
        trace: ConnectionTrace,
        failure_reporter: FailureReporter,
    ) -> Self {
        Self {
            inner: Arc::new(Mutex::new(InboundInner {
                entries: HashMap::new(),
                next_generation: RequestGeneration::default(),
            })),
            capacity: Semaphore::shared(capacity),
            trace,
            failure_reporter,
            limit: capacity,
        }
    }

    /// Reserve capacity and `id` before allocating request-scoped cancellation
    /// state. A duplicate never replaces or cancels the original (ADR 0018),
    /// and an exhausted connection never grows its registry or task group.
    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub(crate) fn reserve(
        &self,
        id: RequestId,
        cancellation_parent: Option<&CancellationToken>,
    ) -> std::result::Result<ReservedRequest, InboundReserveError> {
        self.reserve_method(id, "test/request", cancellation_parent)
    }

    pub(crate) fn reserve_method(
        &self,
        id: RequestId,
        method: &str,
        cancellation_parent: Option<&CancellationToken>,
    ) -> std::result::Result<ReservedRequest, InboundReserveError> {
        let mut inner = self.inner.lock().unwrap();
        if inner.entries.contains_key(&id) {
            self.trace.resource_budget(
                Resource::InboundRequests,
                ResourceAction::Reject,
                inner.entries.len(),
                self.limit,
                None,
            );
            return Err(InboundReserveError::DuplicateId);
        }
        let Some(permit) = self.capacity.try_acquire_owned() else {
            let current = inner.entries.len();
            self.trace.resource_budget(
                Resource::InboundRequests,
                ResourceAction::Reject,
                current,
                self.limit,
                None,
            );
            drop(inner);
            self.failure_reporter
                .report_unvalidated_inbound_method(ConnectionFailureCategory::Overload, Some(&id));
            return Err(InboundReserveError::CapacityExhausted);
        };
        let permit = Arc::new(permit);
        let cancellation = cancellation_parent.map(CancellationToken::child_token);
        let started = Instant::now();
        let generation = inner.next_generation.take_next();
        inner.entries.insert(
            id.clone(),
            InboundEntry {
                method: method.to_string(),
                started,
                generation,
                cancellation: cancellation.clone(),
                _permit: Arc::clone(&permit),
            },
        );
        let current = inner.entries.len();
        drop(inner);
        self.trace.resource_budget(
            Resource::InboundRequests,
            ResourceAction::Admit,
            current,
            self.limit,
            None,
        );
        Ok(ReservedRequest {
            reservation: Reservation {
                id,
                method: method.to_string(),
                started,
                generation,
                _permit: permit,
            },
            cancellation,
        })
    }

    /// Claim the completion gate for `reservation` and enqueue its one response.
    /// Does nothing if some other path already claimed that entry.
    pub(crate) fn complete(
        &self,
        out_tx: &OutboundQueue,
        reservation: Reservation,
        result: std::result::Result<Bytes, LspError>,
    ) {
        let current = {
            let mut inner = self.inner.lock().unwrap();
            match inner.entries.get(&reservation.id) {
                Some(entry) if entry.generation == reservation.generation => {
                    inner.entries.remove(&reservation.id);
                    Some(inner.entries.len())
                }
                _ => None,
            }
        };
        if let Some(current) = current {
            self.trace.resource_budget(
                Resource::InboundRequests,
                ResourceAction::Release,
                current,
                self.limit,
                None,
            );
            let completion = completion_kind(&result);
            self.trace.request_completed(
                &reservation.method,
                &reservation.id,
                reservation.started,
                Direction::Inbound,
                completion,
            );
            enqueue_encoded(out_tx, reservation.id, result);
        }
    }

    pub(crate) fn claim_cancellation(&self, id: &RequestId) -> Option<Reservation> {
        let claimed = {
            let mut inner = self.inner.lock().unwrap();
            let entry = match inner.entries.get(id) {
                Some(entry) if entry.cancellation.is_some() => inner.entries.remove(id),
                _ => None,
            };
            entry.map(|entry| (entry, inner.entries.len()))
        };
        claimed.map(|(entry, current)| {
            self.trace.resource_budget(
                Resource::InboundRequests,
                ResourceAction::Release,
                current,
                self.limit,
                None,
            );
            self.trace.request_completed(
                &entry.method,
                id,
                entry.started,
                Direction::Inbound,
                Completion::Cancelled,
            );
            let token = entry
                .cancellation
                .expect("only cancellable entries are claimed");
            token.cancel();
            Reservation {
                id: id.clone(),
                method: entry.method,
                started: entry.started,
                generation: entry.generation,
                _permit: entry._permit,
            }
        })
    }

    /// Cancel and answer every still-registered request, emptying the registry.
    ///
    /// Used by a successful `shutdown`, which leaves the connection alive long
    /// enough to deliver each cancellation. Removing the entry also claims the
    /// completion gate, so the handler's own late result is dropped and every
    /// cancelled request still receives exactly one response.
    pub(crate) fn cancel_all_with_response(&self, out_tx: &OutboundQueue) {
        let entries = std::mem::take(&mut self.inner.lock().unwrap().entries);
        if !entries.is_empty() {
            self.trace.resource_budget(
                Resource::InboundRequests,
                ResourceAction::Release,
                0,
                self.limit,
                None,
            );
        }
        for (id, entry) in entries {
            self.trace.request_completed(
                &entry.method,
                &id,
                entry.started,
                Direction::Inbound,
                Completion::Cancelled,
            );
            if let Some(cancellation) = entry.cancellation {
                cancellation.cancel();
            }
            enqueue_encoded(out_tx, id, Err(LspError::RequestCancelled));
        }
    }

    /// Cancel every still-registered request and empty the registry without
    /// answering.
    ///
    /// Used by session close, where the peer has either gone away or asked to
    /// exit: there is no one left to receive a cancellation. `shutdown` is the
    /// one ending that still answers, through
    /// [`cancel_all_with_response`](Self::cancel_all_with_response).
    pub(crate) fn close_all(&self) {
        let entries = std::mem::take(&mut self.inner.lock().unwrap().entries);
        if !entries.is_empty() {
            self.trace.resource_budget(
                Resource::InboundRequests,
                ResourceAction::Release,
                0,
                self.limit,
                None,
            );
        }
        for (id, entry) in entries {
            self.trace.request_completed(
                &entry.method,
                &id,
                entry.started,
                Direction::Inbound,
                Completion::ConnectionClosed,
            );
            if let Some(cancellation) = entry.cancellation {
                cancellation.cancel();
            }
        }
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
pub(crate) async fn send_loop<W: TransportWriter, P: SessionPeer, C>(
    writer: W,
    out_rx: UnboundedReceiver<RawMessage>,
    peer: P,
    close: CloseSignal<C>,
    writer_failed: fn() -> C,
) {
    let trace = ConnectionTrace::new();
    send_loop_with_trace(
        writer,
        out_rx,
        peer,
        close,
        writer_failed,
        trace,
        FailureReporter::new(None, trace.id()),
    )
    .await;
}

pub(crate) async fn send_loop_with_trace<W: TransportWriter, P: SessionPeer, C>(
    mut writer: W,
    mut out_rx: UnboundedReceiver<RawMessage>,
    peer: P,
    close: CloseSignal<C>,
    writer_failed: fn() -> C,
    trace: ConnectionTrace,
    failure_reporter: FailureReporter,
) {
    let outbound_closing = peer.outbound_closing();
    loop {
        let msg = select_biased! {
            msg = out_rx.recv().fuse() => msg,
            () = outbound_closing.cancelled().fuse() => {
                out_rx.close();
                break;
            }
        };
        // A closed channel (its receiver half dropped) is a `RecvError`: the
        // engine has shut the queue down, so nothing further can be enqueued.
        let Ok(msg) = msg else {
            peer.close_outbound();
            break;
        };
        // The depth counts what is still queued, so each message is decremented
        // once its transport send has succeeded or failed — including the
        // terminally failed send, after which the loop returns.
        if let Err(e) =
            send_outbound(&mut writer, msg, peer.clone(), trace, &failure_reporter).await
        {
            warn!(error = %e, "send_loop: transport write failed");
            abandon_outbound(&mut out_rx, &peer, &close, writer_failed);
            // ADR 0018: the writer reports its terminal failure and performs no
            // registry or task cleanup of its own; the engine runs the one
            // close operation. Accounting is released here because the
            // receiver is abandoning every message it retained.
            return;
        }
    }
    while let Ok(msg) = out_rx.recv().await {
        if let Err(e) =
            send_outbound(&mut writer, msg, peer.clone(), trace, &failure_reporter).await
        {
            warn!(error = %e, "send_loop: transport write failed while draining");
            abandon_outbound(&mut out_rx, &peer, &close, writer_failed);
            return;
        }
    }
    if let Err(e) = writer.shutdown().await {
        failure_reporter.report(
            ConnectionFailureCategory::Close,
            Some(ConnectionDirection::Outbound),
            None,
            None,
        );
        warn!(error = %e, "send_loop: transport shutdown failed");
        close.request(writer_failed());
    }
}

async fn send_outbound<W: TransportWriter, P: SessionPeer>(
    writer: &mut W,
    message: RawMessage,
    peer: P,
    trace: ConnectionTrace,
    failure_reporter: &FailureReporter,
) -> std::result::Result<(), TransportError> {
    trace.message(Direction::Outbound, &message);
    let method = message.method().map(str::to_owned);
    let request_id = message.id().cloned();
    let result = writer.send(message).await;
    peer.record_outbound_done();
    if result.is_err() {
        failure_reporter.report(
            ConnectionFailureCategory::Transport,
            Some(ConnectionDirection::Outbound),
            method.as_deref(),
            request_id.as_ref(),
        );
    }
    result
}

fn abandon_outbound<P: SessionPeer, C>(
    out_rx: &mut UnboundedReceiver<RawMessage>,
    peer: &P,
    close: &CloseSignal<C>,
    writer_failed: fn() -> C,
) {
    out_rx.close();
    peer.discard_outbound();
    close.request(writer_failed());
}

pub(crate) fn enqueue_encoded(
    out_tx: &OutboundQueue,
    id: RequestId,
    result: std::result::Result<Bytes, LspError>,
) {
    let response = match result {
        Ok(bytes) => RawMessage::Response {
            id,
            result: Ok(bytes),
        },
        Err(err) => error_response(id, &err),
    };
    let _ = out_tx.send_required(response);
}

fn completion_kind(result: &std::result::Result<Bytes, LspError>) -> Completion {
    match result {
        Ok(_) => Completion::Success,
        Err(LspError::RequestCancelled) => Completion::Cancelled,
        Err(LspError::ServerError { message, .. }) if message == HANDLER_DEADLINE_EXPIRED => {
            Completion::DeadlineExpired
        }
        Err(_) => Completion::Error,
    }
}

pub(crate) fn error_response(id: RequestId, err: &LspError) -> RawMessage {
    RawMessage::Response {
        id,
        result: Err(JsonRpcError {
            code: err.code(),
            message: err.message(),
            data: err.data().cloned(),
        }),
    }
}