microsoft-webui-desktop 3.0.3

Configurable native desktop SDK for WebUI applications
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use super::{
    credentials::constant_equal,
    decode_frame,
    engine::{Core, Counters, Incoming, State},
    error::fail,
    executor::{lock, Budget, Permit, TimerKey},
    session::SubscriptionSlot,
    wire::{ipc_frame::Body, IpcFrame, Kind},
    Cancellation, Endpoint, IpcError, IpcErrorCode, IpcFuture, IpcLimits, IpcOptions, IpcRegistry,
    IpcSession, IpcWindow, MethodKind, NotificationContext, RequestContext,
};
use crate::protocol::{DesktopHttpMethod, DesktopProtocolResponse, DesktopResponseBody};
use futures_channel::oneshot;
use futures_util::{
    future::{AbortHandle, Abortable},
    FutureExt,
};
use std::{
    sync::{atomic::Ordering, Arc, Mutex, OnceLock, Weak},
    time::{Duration, Instant},
};

#[cfg(all(
    test,
    feature = "local-server",
    any(target_os = "macos", target_os = "windows")
))]
#[allow(clippy::disallowed_methods)]
#[path = "local_pin_tests.rs"]
mod local_pin_tests;

/// Host provenance is independent of development permissions.
#[derive(Clone, Debug)]
pub enum IpcHost {
    /// A source-backed application.
    Source {
        /// Canonical fixed application origin, supplied by the adapter.
        origin: String,
    },
    /// A packaged application; development-only methods are always denied.
    Packaged {
        /// Canonical fixed application origin, supplied by the adapter.
        origin: String,
    },
    /// An explicitly retained, bound local listener. The frame holds a
    /// duplicate of the listener; a port number or daemon-loss signal alone
    /// cannot construct this principal.
    #[cfg(feature = "local-server")]
    LocalOwned(Arc<crate::local_server::OwnedLocalServerIpc>),
}
impl IpcHost {
    pub(super) fn is_source(&self) -> bool {
        match self {
            Self::Source { .. } => true,
            #[cfg(feature = "local-server")]
            Self::LocalOwned(_) => true,
            Self::Packaged { .. } => false,
        }
    }
    pub(super) fn origin(&self) -> &str {
        match self {
            Self::Source { origin } | Self::Packaged { origin } => origin,
            #[cfg(feature = "local-server")]
            Self::LocalOwned(owned) => owned.origin.as_str(),
        }
    }
    pub(super) fn active(&self) -> bool {
        match self {
            #[cfg(feature = "local-server")]
            Self::LocalOwned(owned) => owned.lifetime.is_active(),
            _ => true,
        }
    }
    pub(super) fn release_listener_pin(&self) {
        #[cfg(feature = "local-server")]
        if let Self::LocalOwned(owned) = self {
            owned.release_listener_pin();
        }
    }
}

/// Sole non-cloneable owner of a frame's mutable IPC state.
pub struct IpcWindowOwner {
    core: Arc<Core>,
}
impl IpcWindowOwner {
    /// Validate immutable policy without starting workers or timer threads.
    pub fn new(
        registry: Arc<IpcRegistry>,
        options: IpcOptions,
        host: IpcHost,
    ) -> Result<Self, IpcError> {
        registry.validate()?;
        options.validate()?;
        if registry.notifications.len() > options.limits.max_callbacks_per_document {
            return Err(IpcError::new(IpcErrorCode::InvalidPayload, "startup notification definitions exceed the document callback limit", "raise max_callbacks_per_document within its SDK ceiling or register fewer startup receivers"));
        }
        let allowed = match &host {
            IpcHost::Source { origin } | IpcHost::Packaged { origin } => {
                matches!(
                    origin.as_str(),
                    "webui://app" | "https://app.webui.localhost"
                )
            }
            #[cfg(feature = "local-server")]
            IpcHost::LocalOwned(owned) => owned.lifetime.is_active(),
        };
        if !allowed {
            return Err(fail(IpcErrorCode::PermissionDenied));
        }
        let budget = Budget::new(
            options
                .limits
                .max_worker_tasks_per_frame_including_retired_documents,
            options.limits.max_retained_bytes_per_frame,
        );
        let input_budget = Budget::new(0, options.limits.max_admitted_input_bytes_per_frame);
        let control_budget = Budget::new(
            0,
            options.limits.reserved_control_frames_per_direction
                * (options.limits.max_error_text_bytes_total + 128)
                * 3,
        );
        Ok(Self {
            core: Arc::new(Core {
                registry,
                options,
                host,
                budget,
                input_budget,
                control_budget,
                state: Mutex::new(State {
                    closed: false,
                    navigation: 0,
                    admitted: false,
                    generation: 0,
                    document: None,
                    waiters: Vec::new(),
                    waker: None,
                    wake_pending: false,
                    closed_control: None,
                    activation: None,
                    activation_started: false,
                }),
                workers: OnceLock::new(),
                timer: OnceLock::new(),
                counters: Counters::default(),
            }),
        })
    }
    /// Weak application handle.
    pub fn window(&self) -> IpcWindow {
        IpcWindow {
            core: Arc::downgrade(&self.core),
        }
    }
    /// Weak native adapter handle.
    pub fn bridge(&self) -> IpcBridge {
        IpcBridge {
            core: Arc::downgrade(&self.core),
        }
    }
    /// Close without waiting for uncooperative application code.
    pub fn close(&self) {
        self.core.retire(IpcErrorCode::Closed, true);
    }
}
impl Drop for IpcWindowOwner {
    fn drop(&mut self) {
        self.close();
    }
}

/// Main-document capability principal, constructed only from trusted native
/// top-level commit tracking and canonical origin, never a message callback.
#[derive(Clone, Debug)]
pub struct CommittedMainDocument {
    /// Strictly increasing top-level navigation identity.
    pub navigation: u64,
    /// Platform-canonicalized fixed app origin.
    pub origin: String,
}
/// Generated renderer schema handshake.
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Hello {
    /// Must match the current framework envelope version (`IPC_VERSION`, currently 3).
    pub wire_version: u32,
    /// Exact generated contract name.
    pub contract_name: String,
    /// Exact breaking schema version.
    pub contract_major: u32,
    /// Exact normalized lowercase SHA-256 digest.
    pub schema_hash: String,
}
/// Bounded adapter admission metadata.
pub struct Admission {
    /// Renderer schema identity.
    pub hello: Hello,
    /// Echoed activation, compared atomically with native-owned proof.
    pub proof: super::DocumentActivation,
}
/// Successful document admission. Tokens must not appear in URLs or logs.
#[derive(Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionInfo {
    /// Serialize as a canonical decimal string at the native JSON boundary.
    #[serde(serialize_with = "super::admission::decimal")]
    pub generation: u64,
    /// Cryptographic 128-bit lowercase hexadecimal credential.
    pub token: String,
    /// Validated numeric budgets.
    pub limits: IpcLimits,
}
/// Payload-free native event-loop wake source.
pub trait IpcWake: Send + Sync + 'static {
    /// Schedule a native control drain; never poll application code here.
    fn wake(&self) -> Result<(), IpcError>;
}
/// Bounded native push metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NativeControl {
    /// Outbound frames are available.
    Ready {
        /// Canonical decimal string on native JSON.
        generation: u64,
    },
    /// A document connection was revoked.
    Closed {
        /// Revoked generation.
        generation: u64,
        /// Stable terminal category.
        code: IpcErrorCode,
    },
}
/// Owned native protocol request. Adapters must cap growth before constructing it.
pub struct OwnedIpcHttpRequest {
    /// Trusted native navigation identity.
    pub navigation: u64,
    /// Only POST ingress and GET drain are supported.
    pub method: DesktopHttpMethod,
    /// Reserved IPC route without query strings.
    pub path: String,
    /// Session header, never a query parameter.
    pub token: String,
    /// Complete bounded protobuf frame.
    pub body: Vec<u8>,
    /// Reservation obtained before body allocation/copy; transferred into work.
    pub input_permit: IpcInputPermit,
}
/// Non-cloneable reservation for native-owned body capacity. The SDK transfers
/// it into admitted work; a 204 does not release a blocked handler's credit.
pub struct IpcInputPermit {
    core: Weak<Core>,
    input: Permit,
    retained: Permit,
    emergency: bool,
    max: usize,
}
impl IpcInputPermit {
    /// Reserve additional credit before growing the body buffer.
    pub fn try_grow(&mut self, additional_bytes: usize) -> Result<(), IpcError> {
        let core = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))?;
        if lock(&core.state).closed || !core.host.active() {
            return Err(fail(IpcErrorCode::Closed));
        }
        if additional_bytes > self.max.saturating_sub(self.input.bytes()) {
            return Err(fail(IpcErrorCode::PayloadTooLarge));
        }
        let result = self.grow_existing(additional_bytes);
        if result.is_err() && !self.emergency {
            let size = self.input.bytes() + additional_bytes;
            let control_max = core.options.limits.max_error_text_bytes_total + 128;
            if size <= control_max {
                let input = core.control_budget.reserve(0, size)?;
                let retained = core.control_budget.reserve(0, size)?;
                self.input = input;
                self.retained = retained;
                self.emergency = true;
                self.max = control_max;
                return Ok(());
            }
        }
        result
    }
    fn grow_existing(&mut self, additional_bytes: usize) -> Result<(), IpcError> {
        self.input.grow(additional_bytes)?;
        if let Err(error) = self.retained.grow(additional_bytes) {
            self.input.shrink(additional_bytes);
            return Err(error);
        }
        Ok(())
    }
    fn require_data(&mut self, core: &Arc<Core>) -> Result<(), IpcError> {
        if self.emergency {
            let input = core.input_budget.reserve(0, self.input.bytes())?;
            let retained = core.budget.reserve(0, self.input.bytes())?;
            self.input = input;
            self.retained = retained;
            self.emergency = false;
        }
        Ok(())
    }
}
pub(super) struct InputCredits {
    pub _input: IpcInputPermit,
    pub _copy: Permit,
}
/// Per-frame weak native facade; its futures contain response completions only.
#[derive(Clone)]
pub struct IpcBridge {
    pub(super) core: Weak<Core>,
}
impl IpcBridge {
    /// Native input cap for a reserved path. Apply before allocating body bytes.
    pub fn max_request_body_bytes(&self, _path: &str) -> Result<usize, IpcError> {
        let core = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))?;
        Ok(core.options.limits.max_frame_bytes)
    }
    /// Whether this live frame has an explicitly configured application schema.
    /// Disabled/default frames need no native bootstrap or wake-source setup.
    pub fn is_enabled(&self) -> bool {
        self.core.upgrade().is_some_and(|core| {
            core.registry.is_enabled() && core.host.active() && !lock(&core.state).closed
        })
    }

    /// Install the single coalesced native wake source.
    pub fn attach_waker(&self, wake: Arc<dyn IpcWake>) -> Result<(), IpcError> {
        let core = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))?;
        {
            let mut state = lock(&core.state);
            if state.closed {
                return Err(fail(IpcErrorCode::Closed));
            }
            if state.waker.is_some() {
                return Err(fail(IpcErrorCode::InvalidFrame));
            }
            state.waker = Some(wake);
        }
        core.signal();
        Ok(())
    }
    /// Revoke the old document before replacement. Same-document routing must
    /// not call this method. A backwards/repeated identity is ignored.
    pub fn navigate(&self, navigation: u64) {
        let Some(core) = self.core.upgrade() else {
            return;
        };
        core.navigate(navigation);
    }
    /// Authenticate and close only this document connection. A generation alone
    /// grants no authority; stale credentials cannot retire a replacement.
    /// Trusted native teardown must use `close` or `navigate` instead.
    pub fn disconnect_authenticated(&self, generation: u64, token: &str) -> Result<(), IpcError> {
        let core = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))?;
        core.disconnect_authenticated(generation, token)
    }
    /// Terminal shutdown without joining application workers.
    pub fn close(&self) {
        if let Some(core) = self.core.upgrade() {
            core.retire(IpcErrorCode::Closed, true);
        }
    }
    /// Charge complete input capacity before adapters allocate or copy bytes.
    /// Small control ingress has separately bounded emergency storage, so a
    /// saturated application-byte budget cannot prevent cancellation/completion.
    pub fn reserve_input(&self, bytes: usize) -> Result<IpcInputPermit, IpcError> {
        let core = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))?;
        if lock(&core.state).closed || !core.host.active() {
            return Err(fail(IpcErrorCode::Closed));
        }
        if bytes > core.options.limits.max_frame_bytes {
            return Err(fail(IpcErrorCode::PayloadTooLarge));
        }
        let normal = (|| {
            Ok((
                core.input_budget.reserve(0, bytes)?,
                core.budget.reserve(0, bytes)?,
            ))
        })();
        let (input, retained, emergency, max) = match normal {
            Ok((input, retained)) => (input, retained, false, core.options.limits.max_frame_bytes),
            Err(_) if bytes <= core.options.limits.max_error_text_bytes_total + 128 => (
                core.control_budget.reserve(0, bytes)?,
                core.control_budget.reserve(0, bytes)?,
                true,
                core.options.limits.max_error_text_bytes_total + 128,
            ),
            Err(error) => return Err(error),
        };
        Ok(IpcInputPermit {
            core: self.core.clone(),
            input,
            retained,
            emergency,
            max,
        })
    }
    /// Drain coalesced metadata on the native thread. No application bytes.
    pub fn take_control(&self) -> Result<Option<NativeControl>, IpcError> {
        let core = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))?;
        let mut state = lock(&core.state);
        if state.closed {
            return Ok(None);
        }
        state.wake_pending = false;
        if let Some(control) = state.closed_control.take() {
            return Ok(Some(control));
        }
        if let Some(doc) = &mut state.document {
            if doc.ready {
                doc.ready = false;
                return Ok(Some(NativeControl::Ready {
                    generation: doc.generation,
                }));
            }
        }
        Ok(None)
    }
    /// Validate bounded envelope/control metadata and reserve capacity immediately.
    /// Returned futures never decode application payloads or run user handlers.
    /// Adapters add `Cache-Control: no-store` to every response.
    pub fn submit(&self, request: OwnedIpcHttpRequest) -> IpcFuture<DesktopProtocolResponse> {
        let navigation = request.navigation;
        let weak = self.core.clone();
        let result = self
            .core
            .upgrade()
            .ok_or_else(|| fail(IpcErrorCode::Closed))
            .and_then(|core| {
                if !core.host.active() {
                    return Err(fail(IpcErrorCode::Closed));
                }
                let result = submit(&core, request);
                if result.is_err() {
                    core.counters.rejected.fetch_add(1, Ordering::Relaxed);
                }
                result
            });
        Box::pin(async move {
            Ok(match result {
                Ok((response, _drain)) => {
                    let core = weak.upgrade().ok_or_else(|| fail(IpcErrorCode::Closed))?;
                    let state = lock(&core.state);
                    if state.closed {
                        return Err(fail(IpcErrorCode::Closed));
                    }
                    if state.navigation != navigation {
                        return Err(fail(IpcErrorCode::Navigated));
                    }
                    if state.document.is_none() {
                        return Err(fail(IpcErrorCode::Closed));
                    }
                    response
                }
                Err(error) => http_error(error),
            })
        })
    }
}

struct DrainGuard {
    core: Weak<Core>,
    generation: u64,
}
impl Drop for DrainGuard {
    fn drop(&mut self) {
        if let Some(core) = self.core.upgrade() {
            let mut state = lock(&core.state);
            if let Ok(doc) = Core::document(&mut state, self.generation) {
                doc.drain_inflight = false;
            }
        }
    }
}

fn submit(
    core: &Arc<Core>,
    mut request: OwnedIpcHttpRequest,
) -> Result<(DesktopProtocolResponse, Option<DrainGuard>), IpcError> {
    let generation = authenticate(core, &request)?;
    match (&request.method, request.path.as_str()) {
        (DesktopHttpMethod::Get, "/_webui/ipc/outbound") if request.body.is_empty() => {
            drain(core, generation)
        }
        (DesktopHttpMethod::Post, "/_webui/ipc") => {
            let received = Instant::now();
            let (mut copy, emergency_copy) = match core.budget.reserve(0, request.body.len()) {
                Ok(copy) => (copy, false),
                Err(_)
                    if request.body.len()
                        <= core.options.limits.max_error_text_bytes_total + 128 =>
                {
                    (core.control_budget.reserve(0, request.body.len())?, true)
                }
                Err(error) => return Err(error),
            };
            let frame = decode_frame(&request.body, &core.options.limits)?;
            if frame.generation != generation {
                return Err(fail(IpcErrorCode::Navigated));
            }
            if matches!(Kind::try_from(frame.kind), Ok(Kind::Request | Kind::Notify)) {
                request.input_permit.require_data(core)?;
                if emergency_copy {
                    copy = core.budget.reserve(0, request.body.len())?;
                }
            }
            let credits = InputCredits {
                _input: request.input_permit,
                _copy: copy,
            };
            dispatch(core, frame, received, credits)?;
            Ok((empty(), None))
        }
        _ => Err(fail(IpcErrorCode::InvalidFrame)),
    }
}

fn authenticate(core: &Arc<Core>, request: &OwnedIpcHttpRequest) -> Result<u64, IpcError> {
    if !core.host.active() {
        return Err(fail(IpcErrorCode::Closed));
    }
    if request.body.len() > core.options.limits.max_frame_bytes {
        return Err(fail(IpcErrorCode::PayloadTooLarge));
    }
    if !Weak::ptr_eq(&request.input_permit.core, &Arc::downgrade(core))
        || request.input_permit.input.bytes() < request.body.capacity()
    {
        return Err(fail(IpcErrorCode::InvalidFrame));
    }
    if request.path.len() > 64 || request.token.len() != 32 {
        return Err(fail(IpcErrorCode::InvalidFrame));
    }
    {
        let state = lock(&core.state);
        if state.closed {
            return Err(fail(IpcErrorCode::Closed));
        }
        let doc = state
            .document
            .as_ref()
            .ok_or_else(|| fail(IpcErrorCode::NotReady))?;
        if doc.navigation != request.navigation
            || !constant_equal(doc.token.as_bytes(), request.token.as_bytes())
        {
            return Err(fail(IpcErrorCode::PermissionDenied));
        }
        Ok(doc.generation)
    }
}

fn drain(
    core: &Arc<Core>,
    generation: u64,
) -> Result<(DesktopProtocolResponse, Option<DrainGuard>), IpcError> {
    let mut state = lock(&core.state);
    let doc = Core::document(&mut state, generation)?;
    if doc.drain_inflight {
        return Err(fail(IpcErrorCode::Overloaded));
    }
    doc.drain_inflight = true;
    let drain = DrainGuard {
        core: Arc::downgrade(core),
        generation,
    };
    let Some(queued) = doc.queue.pop_front() else {
        return Ok((empty(), Some(drain)));
    };
    if queued.control {
        doc.control_slots -= 1;
        if queued.bytes.len() > core.options.limits.max_error_text_bytes_total + 128 {
            doc.data_bytes -= queued.bytes.len();
        }
    } else {
        doc.data_frames -= 1;
        doc.data_bytes -= queued.bytes.len();
    }
    Ok((
        DesktopProtocolResponse::new(
            200,
            "application/x-protobuf",
            DesktopResponseBody::with_guard(queued.bytes, queued._memory),
        ),
        Some(drain),
    ))
}
fn empty() -> DesktopProtocolResponse {
    DesktopProtocolResponse::new(204, "application/x-protobuf", Vec::new())
}
fn http_error(error: IpcError) -> DesktopProtocolResponse {
    let status = match error.code {
        IpcErrorCode::PermissionDenied => 401,
        IpcErrorCode::Navigated | IpcErrorCode::NotReady | IpcErrorCode::SchemaMismatch => 409,
        IpcErrorCode::PayloadTooLarge => 413,
        IpcErrorCode::Overloaded => 429,
        IpcErrorCode::Closed | IpcErrorCode::Transport => 503,
        _ => 400,
    };
    DesktopProtocolResponse::new(
        status,
        "application/x-protobuf",
        error.wire(2048).encode_to_vec(),
    )
}
fn dispatch(
    core: &Arc<Core>,
    frame: IpcFrame,
    received: Instant,
    credits: InputCredits,
) -> Result<(), IpcError> {
    match Kind::try_from(frame.kind).map_err(|_| fail(IpcErrorCode::InvalidFrame))? {
        Kind::Request | Kind::Notify => invoke(core, frame, received, credits),
        Kind::Result | Kind::Error | Kind::Accept => reply(core, frame, credits),
        Kind::Cancel => {
            let incoming = {
                let mut state = lock(&core.state);
                let doc = Core::document(&mut state, frame.generation)?;
                let incoming = doc.incoming.remove(&frame.id);
                if incoming.is_some() {
                    doc.control_slots -= 1;
                }
                incoming
            };
            if let Some(incoming) = incoming {
                core.remove_timer(
                    incoming.deadline,
                    TimerKey {
                        generation: frame.generation,
                        id: frame.id,
                        local: false,
                    },
                );
                incoming.cancellation.cancel();
                incoming.abort.abort();
            } else {
                core.counters.stale_replies.fetch_add(1, Ordering::Relaxed);
            }
            Ok(())
        }
        Kind::Unspecified => Err(fail(IpcErrorCode::InvalidFrame)),
    }
}
fn reply(core: &Core, frame: IpcFrame, credits: InputCredits) -> Result<(), IpcError> {
    let (sender, result) = {
        let mut state = lock(&core.state);
        let doc = Core::document(&mut state, frame.generation)?;
        let Some(pending) = doc.pending.get_mut(&frame.id) else {
            core.counters.stale_replies.fetch_add(1, Ordering::Relaxed);
            return Ok(());
        };
        if !pending.sent
            || (frame.kind == Kind::Accept as i32 && pending.kind != MethodKind::Notification)
            || (frame.kind == Kind::Result as i32 && pending.kind != MethodKind::Rpc)
        {
            return Err(fail(IpcErrorCode::InvalidFrame));
        }
        let result = match frame.body {
            Some(Body::Payload(bytes)) => Ok(super::engine::ReceivedPayload {
                bytes,
                _credits: credits,
            }),
            Some(Body::Error(error)) => Err(IpcError::from_wire(
                error,
                core.options.limits.max_error_text_bytes_total,
            )),
            None => Ok(super::engine::ReceivedPayload {
                bytes: Vec::new(),
                _credits: credits,
            }),
        };
        (pending.sender.take(), result)
    };
    if let Some(sender) = sender {
        let _ = sender.send(result);
    } else {
        core.counters.stale_replies.fetch_add(1, Ordering::Relaxed);
    }
    Ok(())
}

fn invoke(
    core: &Arc<Core>,
    frame: IpcFrame,
    received: Instant,
    credits: InputCredits,
) -> Result<(), IpcError> {
    let kind = if frame.kind == Kind::Request as i32 {
        MethodKind::Rpc
    } else {
        MethodKind::Notification
    };
    let method = core.allowed(frame.method_id, Endpoint::Host, kind);
    {
        let mut state = lock(&core.state);
        let doc = Core::document(&mut state, frame.generation)?;
        if frame.id <= doc.last_peer_id {
            return Err(fail(IpcErrorCode::InvalidFrame));
        }
        doc.last_peer_id = frame.id;
        if let Err(error) = method {
            let reserve = core.reservation(doc)?;
            core.enqueue_control(
                doc,
                frame.id,
                (
                    Kind::Error,
                    Some(Body::Error(
                        error.wire(core.options.limits.max_error_text_bytes_total),
                    )),
                ),
                reserve,
            );
            drop(state);
            core.signal();
            return Ok(());
        }
    }
    if kind == MethodKind::Notification {
        return notify(core, frame, credits);
    }
    request(core, frame, received, credits)
}
fn request(
    core: &Arc<Core>,
    frame: IpcFrame,
    received: Instant,
    credits: InputCredits,
) -> Result<(), IpcError> {
    let permit = core
        .budget
        .reserve(1, core.options.limits.max_frame_bytes)?;
    let workers = core.workers()?;
    let timer = core.timer()?;
    let deadline = received
        .checked_add(Duration::from_millis(u64::from(frame.timeout_ms)))
        .ok_or_else(|| fail(IpcErrorCode::InvalidFrame))?;
    let (abort, registration) = AbortHandle::new_pair();
    let cancellation = Cancellation::default();
    {
        let mut state = lock(&core.state);
        let doc = Core::document(&mut state, frame.generation)?;
        if doc.incoming.len() >= core.options.limits.max_pending_calls_per_direction {
            return Err(fail(IpcErrorCode::Overloaded));
        }
        let reserve = core.reservation(doc)?;
        doc.incoming.insert(
            frame.id,
            Incoming {
                cancellation: cancellation.clone(),
                abort,
                deadline,
                reserve,
            },
        );
        timer.insert(
            deadline,
            TimerKey {
                generation: frame.generation,
                id: frame.id,
                local: false,
            },
        );
    }
    let weak = Arc::downgrade(core);
    let handler = core.registry.handlers.get(&frame.method_id).cloned();
    let limits = core.options.limits.clone();
    let generation = frame.generation;
    let id = frame.id;
    let task = async move {
        let _permit = permit;
        let _credits = credits;
        let result = Abortable::new(
            async {
                if cancellation.is_cancelled() || Instant::now() >= deadline {
                    return Err(fail(IpcErrorCode::DeadlineExceeded));
                }
                let Some(handler) = handler else {
                    return Err(fail(IpcErrorCode::ReceiverUnavailable));
                };
                let Some(Body::Payload(bytes)) = frame.body else {
                    return Err(fail(IpcErrorCode::InvalidFrame));
                };
                let context = RequestContext {
                    session: IpcSession {
                        core: weak.clone(),
                        generation,
                    },
                    cancellation,
                    deadline,
                };
                handler(context, bytes, limits).await
            },
            registration,
        )
        .await;
        if let (Ok(result), Some(core)) = (result, weak.upgrade()) {
            core.complete_incoming(generation, id, result);
        }
    };
    if let Err(error) = workers.spawn(task) {
        core.complete_incoming(generation, id, Err(error));
    }
    core.counters.accepted.fetch_add(1, Ordering::Relaxed);
    Ok(())
}

struct Delivery {
    slot: Arc<SubscriptionSlot>,
    previous: Option<oneshot::Receiver<()>>,
    next: oneshot::Sender<()>,
}
fn notify(core: &Arc<Core>, frame: IpcFrame, credits: InputCredits) -> Result<(), IpcError> {
    let workers = core.workers()?;
    let method = core.allowed(frame.method_id, Endpoint::Host, MethodKind::Notification)?;
    let (deliveries, reserve, permit, callback_permit, notification_permit) = {
        let mut state = lock(&core.state);
        let doc = Core::document(&mut state, frame.generation)?;
        let count = doc
            .subscriptions
            .values()
            .filter(|slot| slot.method == frame.method_id && slot.active.load(Ordering::Acquire))
            .count();
        let permit = core.budget.reserve(
            count + 1,
            frame
                .encoded_len()
                .checked_mul(count)
                .ok_or_else(|| fail(IpcErrorCode::Overloaded))?,
        )?;
        let callback_permit = doc.callback_budget.reserve(count, 0)?;
        let notification_permit = doc.notification_budget.reserve(1, 0)?;
        let reserve = core.reservation(doc)?;
        let mut deliveries = Vec::with_capacity(count);
        for slot in doc
            .subscriptions
            .values()
            .filter(|slot| slot.method == frame.method_id && slot.active.load(Ordering::Acquire))
        {
            let (next, tail) = oneshot::channel();
            let previous = lock(&slot.tail).replace(tail);
            deliveries.push(Delivery {
                slot: Arc::clone(slot),
                previous,
                next,
            });
        }

        (
            deliveries,
            reserve,
            permit,
            callback_permit,
            notification_permit,
        )
    };
    let weak = Arc::downgrade(core);
    let limits = core.options.limits.clone();
    let generation = frame.generation;
    let id = frame.id;
    let task = async move {
        // The shared fanout permit survives until every callback actually exits.
        let permit = Arc::new((permit, callback_permit, notification_permit, credits));
        let Some(Body::Payload(bytes)) = frame.body else {
            return;
        };
        let validation = (method.validate_request)(&bytes, &limits);
        let invoke_callbacks = validation.is_ok();
        let Some(core) = weak.upgrade() else {
            return;
        };
        {
            let mut state = lock(&core.state);
            let Ok(doc) = Core::document(&mut state, generation) else {
                return;
            };
            match validation {
                Ok(()) => core.enqueue_control(doc, id, (Kind::Accept, None), reserve),
                Err(error) => {
                    core.enqueue_control(
                        doc,
                        id,
                        (
                            Kind::Error,
                            Some(Body::Error(error.wire(limits.max_error_text_bytes_total))),
                        ),
                        reserve,
                    );
                }
            }
        }
        core.signal();
        let bytes = Arc::new(bytes);
        for delivery in deliveries {
            let weak = weak.clone();
            let bytes = Arc::clone(&bytes);
            let limits = limits.clone();
            let permit = Arc::clone(&permit);
            let work = async move {
                let _permit = permit;
                let _next = delivery.next;
                // Even a rejected notification is an ordering node. Waiting
                // outside callback cancellation prevents a skipped middle node
                // from releasing successors past an unfinished predecessor.
                if let Some(previous) = delivery.previous {
                    let _ = previous.await;
                }
                if !invoke_callbacks {
                    return;
                }
                let slot = delivery.slot;
                let cancellation = slot.cancellation.cancelled();
                let callback = async {
                    if !slot.active.load(Ordering::Acquire) {
                        return;
                    }
                    let callback = lock(&slot.callback).as_ref().map(Arc::clone);
                    if let Some(callback) = callback {
                        if !slot.active.load(Ordering::Acquire) {
                            return;
                        }
                        let context = NotificationContext {
                            session: IpcSession {
                                core: weak.clone(),
                                generation,
                            },
                        };
                        if callback(context, bytes, limits).await.is_err() {
                            if let Some(core) = weak.upgrade() {
                                core.counters
                                    .callback_errors
                                    .fetch_add(1, Ordering::Relaxed);
                            }
                        }
                    }
                }
                .boxed();
                let _ = futures_util::future::select(callback, cancellation).await;
            };
            if let Err(error) = core.workers().and_then(|pool| pool.spawn(work)) {
                core.retire(error.code, true);
                return;
            }
        }
    };
    workers.spawn(task)?;
    core.counters.accepted.fetch_add(1, Ordering::Relaxed);
    Ok(())
}