hawdb-core 0.34.0

An embedded Rust graph database for local data-plane workloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::time::{Duration, Instant};
use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::fmt::Debug;
use std::fmt::{self, Display, Formatter};
use std::future::Future;
use std::num::NonZeroUsize;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll, Waker};

const NOT_CANCELLED: u8 = 0;
const CANCELLED: u8 = 1;
const RESOURCE_EXHAUSTED: u8 = 2;

#[derive(Debug, Clone)]
pub struct RuntimeCancellationToken {
    state: Arc<RuntimeCancellationState>,
}

#[derive(Debug)]
struct RuntimeCancellationState {
    cancellation_reason: AtomicU8,
    /// Filled from the front: a second parent implies a first one, so a
    /// root token is recognized by `parents[0].is_none()` alone.
    parents: [Option<RuntimeCancellationToken>; 2],
    next_waiter_id: AtomicU64,
    waiters: Mutex<BTreeMap<u64, Waker>>,
}

impl Default for RuntimeCancellationToken {
    fn default() -> Self {
        Self {
            state: Arc::new(RuntimeCancellationState {
                cancellation_reason: AtomicU8::new(NOT_CANCELLED),
                parents: [None, None],
                next_waiter_id: AtomicU64::new(1),
                waiters: Mutex::new(BTreeMap::new()),
            }),
        }
    }
}

impl RuntimeCancellationToken {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn cancel(&self) -> bool {
        self.cancel_with_code(CANCELLED)
    }

    /// Stops cooperating siblings after an unrecoverable resource refusal.
    /// The first local cancellation cause is retained. Cancelling a child does
    /// not cancel its parent; the operation must still return its original error.
    pub fn cancel_for_resource_exhaustion(&self) -> bool {
        self.cancel_with_code(RESOURCE_EXHAUSTED)
    }

    fn cancel_with_code(&self, reason: u8) -> bool {
        if self
            .state
            .cancellation_reason
            .compare_exchange(NOT_CANCELLED, reason, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            return false;
        }
        let waiters = {
            let mut waiters = self
                .state
                .waiters
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            std::mem::take(&mut *waiters)
        };
        for (_, waiter) in waiters {
            waiter.wake();
        }
        true
    }

    #[inline]
    pub fn is_cancelled(&self) -> bool {
        self.reason().is_some()
    }

    /// Hot path of every cooperative cancellation check (#1651): one
    /// acquire load of this token's own state, inlined into the caller. Only
    /// a token that is cancelled or has parents takes the out-of-line walk,
    /// which selects the same reason in the same depth-first order.
    #[inline]
    fn reason(&self) -> Option<RuntimeCancellationReason> {
        let code = self.state.cancellation_reason.load(Ordering::Acquire);
        if code == NOT_CANCELLED && self.state.parents[0].is_none() {
            return None;
        }
        Self::reason_of(&self.state, code)
    }

    #[cold]
    #[inline(never)]
    fn reason_of(state: &RuntimeCancellationState, code: u8) -> Option<RuntimeCancellationReason> {
        match code {
            CANCELLED => Some(RuntimeCancellationReason::Cancelled),
            RESOURCE_EXHAUSTED => Some(RuntimeCancellationReason::ResourceExhausted),
            _ => state
                .parents
                .iter()
                .flatten()
                .find_map(RuntimeCancellationToken::reason),
        }
    }

    pub fn child(&self) -> Self {
        Self {
            state: Arc::new(RuntimeCancellationState {
                cancellation_reason: AtomicU8::new(NOT_CANCELLED),
                parents: [Some(self.clone()), None],
                next_waiter_id: AtomicU64::new(1),
                waiters: Mutex::new(BTreeMap::new()),
            }),
        }
    }

    fn linked(&self, other: &Self) -> Self {
        if Arc::ptr_eq(&self.state, &other.state) {
            return self.child();
        }
        Self {
            state: Arc::new(RuntimeCancellationState {
                cancellation_reason: AtomicU8::new(NOT_CANCELLED),
                parents: [Some(self.clone()), Some(other.clone())],
                next_waiter_id: AtomicU64::new(1),
                waiters: Mutex::new(BTreeMap::new()),
            }),
        }
    }

    /// Resolves when this token or any parent token is cancelled.
    pub fn cancelled(&self) -> RuntimeCancellationFuture<'_> {
        RuntimeCancellationFuture {
            token: self,
            registrations: Vec::new(),
        }
    }
}

struct RuntimeCancellationRegistration {
    state: Weak<RuntimeCancellationState>,
    waiter_id: u64,
}

/// A runtime-neutral future returned by [`RuntimeCancellationToken::cancelled`].
pub struct RuntimeCancellationFuture<'a> {
    token: &'a RuntimeCancellationToken,
    registrations: Vec<RuntimeCancellationRegistration>,
}

impl Future for RuntimeCancellationFuture<'_> {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
        if self.token.is_cancelled() {
            return Poll::Ready(());
        }

        if self.registrations.is_empty() {
            let mut pending = vec![self.token];
            let mut visited = BTreeSet::new();
            while let Some(current) = pending.pop() {
                if !visited.insert(Arc::as_ptr(&current.state)) {
                    continue;
                }
                let waiter_id = current.state.next_waiter_id.fetch_add(1, Ordering::Relaxed);
                current
                    .state
                    .waiters
                    .lock()
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .insert(waiter_id, context.waker().clone());
                self.registrations.push(RuntimeCancellationRegistration {
                    state: Arc::downgrade(&current.state),
                    waiter_id,
                });
                pending.extend(current.state.parents.iter().flatten());
            }
        } else {
            for registration in &self.registrations {
                if let Some(state) = registration.state.upgrade()
                    && let Some(waiter) = state
                        .waiters
                        .lock()
                        .unwrap_or_else(|poisoned| poisoned.into_inner())
                        .get_mut(&registration.waiter_id)
                    && !waiter.will_wake(context.waker())
                {
                    *waiter = context.waker().clone();
                }
            }
        }

        if self.token.is_cancelled() {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

impl Drop for RuntimeCancellationFuture<'_> {
    fn drop(&mut self) {
        for registration in &self.registrations {
            if let Some(state) = registration.state.upgrade() {
                state
                    .waiters
                    .lock()
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .remove(&registration.waiter_id);
            }
        }
    }
}

/// A drop guard for one admitted storage I/O wave.
pub trait RuntimeIoWavePermit: Debug + Send {}

impl<T: Debug + Send> RuntimeIoWavePermit for T {}

/// Execution-owned hook for acquiring the I/O capacity declared at admission.
pub trait RuntimeIoWaveController: Debug + Send + Sync {
    /// Attempts to acquire one I/O wave without blocking the calling thread.
    ///
    /// `Ok(None)` means that the reservation is currently saturated. Async
    /// runtime adapters must yield before retrying instead of spinning.
    fn try_acquire(
        &self,
        slots: NonZeroUsize,
        context: &RuntimeTaskContext,
    ) -> Result<Option<Box<dyn RuntimeIoWavePermit>>, RuntimeIoWaveError>;

    fn acquire(
        &self,
        slots: NonZeroUsize,
        context: &RuntimeTaskContext,
    ) -> Result<Box<dyn RuntimeIoWavePermit>, RuntimeIoWaveError>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeIoWaveError {
    Stopped(RuntimeCancellationReason),
    ReservationExceeded {
        requested_slots: usize,
        reserved_slots: usize,
    },
}

#[derive(Debug)]
pub enum RuntimeIoWaveTryAcquire {
    /// Capacity is available. Ungoverned contexts carry no permit.
    Acquired(Option<Box<dyn RuntimeIoWavePermit>>),
    /// Capacity is currently saturated and the caller should yield.
    Pending,
}

impl Display for RuntimeIoWaveError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Stopped(reason) => write!(formatter, "runtime I/O wave stopped: {reason}"),
            Self::ReservationExceeded {
                requested_slots,
                reserved_slots,
            } => write!(
                formatter,
                "runtime I/O wave requested {requested_slots} slots, exceeding the {reserved_slots}-slot reservation"
            ),
        }
    }
}

impl Error for RuntimeIoWaveError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Stopped(reason) => Some(reason),
            Self::ReservationExceeded { .. } => None,
        }
    }
}

mod memory;
mod work;
pub use memory::{
    RuntimeMemoryController, RuntimeMemoryError, RuntimeMemoryPermit, RuntimeMemoryTarget,
};

#[derive(Debug, Clone)]
pub struct RuntimeTaskContext {
    cancellation: RuntimeCancellationToken,
    deadline: Option<Instant>,
    admitted_parallelism: NonZeroUsize,
    executor_thread_limit: Option<NonZeroUsize>,
    memory_reservation: Option<RuntimeMemoryReservation>,
    memory_controller: Option<Arc<dyn RuntimeMemoryController>>,
    memory_target: Option<Arc<dyn RuntimeMemoryTarget>>,
    io_wave_controller: Option<Arc<dyn RuntimeIoWaveController>>,
    work_budget: Option<Arc<work::RuntimeWorkBudget>>,
}

/// Memory already reserved for one task by the runtime governor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RuntimeMemoryReservation {
    memory_bytes: u64,
    result_bytes: u64,
}

impl RuntimeMemoryReservation {
    pub const fn new(memory_bytes: u64, result_bytes: u64) -> Self {
        Self {
            memory_bytes,
            result_bytes,
        }
    }

    pub const fn memory_bytes(self) -> u64 {
        self.memory_bytes
    }

    pub const fn result_bytes(self) -> u64 {
        self.result_bytes
    }

    pub const fn intersect(self, other: Self) -> Self {
        Self {
            memory_bytes: if self.memory_bytes < other.memory_bytes {
                self.memory_bytes
            } else {
                other.memory_bytes
            },
            result_bytes: if self.result_bytes < other.result_bytes {
                self.result_bytes
            } else {
                other.result_bytes
            },
        }
    }
}

impl RuntimeTaskContext {
    pub fn new(cancellation: RuntimeCancellationToken, deadline: Option<Instant>) -> Self {
        Self {
            cancellation,
            deadline,
            admitted_parallelism: NonZeroUsize::MIN,
            executor_thread_limit: None,
            memory_reservation: None,
            memory_controller: None,
            memory_target: None,
            io_wave_controller: None,
            work_budget: None,
        }
    }

    pub fn without_deadline(cancellation: RuntimeCancellationToken) -> Self {
        Self::new(cancellation, None)
    }

    pub fn with_timeout(timeout: Duration) -> Self {
        Self::new(
            RuntimeCancellationToken::new(),
            Instant::now().checked_add(timeout),
        )
    }

    pub fn cancellation(&self) -> &RuntimeCancellationToken {
        &self.cancellation
    }

    pub fn child(&self) -> Self {
        Self {
            cancellation: self.cancellation.child(),
            deadline: self.deadline,
            admitted_parallelism: self.admitted_parallelism,
            executor_thread_limit: self.executor_thread_limit,
            memory_reservation: self.memory_reservation,
            memory_controller: self.memory_controller.clone(),
            memory_target: self.memory_target.clone(),
            io_wave_controller: self.io_wave_controller.clone(),
            work_budget: self.work_budget.clone(),
        }
    }

    /// Applies statement constraints without replacing an enclosing admission.
    /// Cancellation from either scope stops the statement; cancelling the combined
    /// scope does not cancel its parents. Budgets remain shared and can only tighten.
    /// Independent controllers cannot represent one admission and are rejected.
    pub fn constrained_by(&self, request: &Self) -> crate::Result<Self> {
        fn controller<T: ?Sized>(
            parent: &Option<Arc<T>>,
            request: &Option<Arc<T>>,
            resource: &str,
        ) -> crate::Result<Option<Arc<T>>> {
            match (parent, request) {
                (Some(parent), Some(request)) if !Arc::ptr_eq(parent, request) => {
                    Err(crate::HawDBError::Execution(format!(
                        "request task context carries a different {resource} admission"
                    )))
                }
                (Some(parent), _) => Ok(Some(parent.clone())),
                (_, request) => Ok(request.clone()),
            }
        }
        fn minimum<T: Ord + Copy>(left: Option<T>, right: Option<T>) -> Option<T> {
            match (left, right) {
                (Some(left), Some(right)) => Some(left.min(right)),
                (left, right) => left.or(right),
            }
        }
        Ok(Self {
            cancellation: self.cancellation.linked(&request.cancellation),
            deadline: minimum(self.deadline, request.deadline),
            admitted_parallelism: self.admitted_parallelism.min(request.admitted_parallelism),
            executor_thread_limit: minimum(
                self.executor_thread_limit,
                request.executor_thread_limit,
            ),
            memory_reservation: match (self.memory_reservation, request.memory_reservation) {
                (Some(parent), Some(request)) => Some(parent.intersect(request)),
                (parent, request) => parent.or(request),
            },
            memory_controller: controller(
                &self.memory_controller,
                &request.memory_controller,
                "memory",
            )?,
            memory_target: controller(
                &self.memory_target,
                &request.memory_target,
                "memory target",
            )?,
            io_wave_controller: controller(
                &self.io_wave_controller,
                &request.io_wave_controller,
                "I/O",
            )?,
            work_budget: work::RuntimeWorkBudget::intersect(
                &self.work_budget,
                &request.work_budget,
            ),
        })
    }

    /// Carries the CPU parallelism already reserved by the runtime governor.
    ///
    /// This is an execution ceiling, not a request to create worker threads.
    /// Executors must still apply their operator memory and input-size limits.
    pub fn with_admitted_parallelism(mut self, admitted_parallelism: NonZeroUsize) -> Self {
        self.admitted_parallelism = admitted_parallelism;
        self
    }

    pub fn admitted_parallelism(&self) -> NonZeroUsize {
        self.admitted_parallelism
    }

    /// Carries the physical executor-thread ceiling derived by the runtime governor.
    ///
    /// This is distinct from [`Self::admitted_parallelism`], which is the CPU
    /// reservation for one task. An absent limit denotes an ungoverned library call.
    pub fn with_executor_thread_limit(mut self, limit: NonZeroUsize) -> Self {
        self.executor_thread_limit = Some(
            self.executor_thread_limit
                .map_or(limit, |current| current.min(limit)),
        );
        self
    }

    pub fn executor_thread_limit(&self) -> Option<NonZeroUsize> {
        self.executor_thread_limit
    }

    /// Carries the memory already reserved by the runtime governor.
    ///
    /// Governed executors must use this reservation instead of a static
    /// per-query configuration limit. An absent reservation denotes an
    /// ungoverned library call and preserves the configured fallback.
    pub fn with_memory_reservation(mut self, reservation: RuntimeMemoryReservation) -> Self {
        self.memory_reservation = Some(reservation);
        self
    }

    pub fn memory_reservation(&self) -> Option<RuntimeMemoryReservation> {
        self.memory_reservation
    }

    /// Whether execution carries dynamic resource controllers rather than only
    /// a static ceiling. Synchronous adapters must preserve these controllers
    /// and must not replace an originating governor admission with a local cap.
    pub fn has_resource_controllers(&self) -> bool {
        self.memory_controller.is_some()
            || self.memory_target.is_some()
            || self.io_wave_controller.is_some()
    }

    /// Carries host-driven pressure feedback to revocable operator work areas.
    pub fn with_memory_target(mut self, target: Arc<dyn RuntimeMemoryTarget>) -> Self {
        self.memory_target = Some(target);
        self
    }

    /// The target can only tighten the caller's ceiling. Operators retain a
    /// bounded progress floor and continue to enforce all hard allocation limits.
    pub fn working_memory_target(&self, hard_limit: u64) -> u64 {
        self.memory_target.as_ref().map_or(hard_limit, |target| {
            target.target_bytes(hard_limit).min(hard_limit)
        })
    }

    /// Binds actual allocation ownership to an already admitted task.
    pub fn with_memory_controller(mut self, controller: Arc<dyn RuntimeMemoryController>) -> Self {
        self.memory_controller = Some(controller);
        self
    }

    /// Reserve before allocating and retain the returned lease until destruction.
    /// Clones and children share their controller. Ungoverned contexts return
    /// `None`; a supplied static ceiling still limits an individual allocation.
    pub fn reserve_working_memory(
        &self,
        bytes: u64,
    ) -> Result<Option<Box<dyn RuntimeMemoryPermit>>, RuntimeMemoryError> {
        self.checkpoint().map_err(RuntimeMemoryError::Stopped)?;
        if let Some(reservation) = self.memory_reservation
            && bytes > reservation.memory_bytes()
        {
            return Err(RuntimeMemoryError::ReservationExceeded {
                requested_bytes: bytes,
                available_bytes: reservation.memory_bytes(),
            });
        }
        self.memory_controller
            .as_ref()
            .map(|controller| {
                controller.reserve(
                    bytes,
                    self.memory_reservation
                        .map_or(u64::MAX, RuntimeMemoryReservation::memory_bytes),
                )
            })
            .transpose()
    }

    /// Binds the controller owned by a successful runtime admission.
    pub fn with_io_wave_controller(mut self, controller: Arc<dyn RuntimeIoWaveController>) -> Self {
        self.io_wave_controller = Some(controller);
        self
    }

    /// Acquires capacity for one storage I/O wave when this is an admitted context.
    ///
    /// Raw contexts have no controller and return `Ok(None)`. The returned
    /// permit must remain live only while physical reads are in flight.
    pub fn acquire_io_wave(
        &self,
        slots: NonZeroUsize,
    ) -> Result<Option<Box<dyn RuntimeIoWavePermit>>, RuntimeIoWaveError> {
        self.checkpoint().map_err(RuntimeIoWaveError::Stopped)?;
        self.io_wave_controller
            .as_ref()
            .map(|controller| controller.acquire(slots, self))
            .transpose()
    }

    /// Attempts to acquire capacity for one storage I/O wave without blocking.
    ///
    /// Raw, ungoverned contexts return `Acquired(None)`. Governed contexts
    /// return `Pending` while another wave owns the requested capacity.
    pub fn try_acquire_io_wave(
        &self,
        slots: NonZeroUsize,
    ) -> Result<RuntimeIoWaveTryAcquire, RuntimeIoWaveError> {
        self.checkpoint().map_err(RuntimeIoWaveError::Stopped)?;
        let Some(controller) = &self.io_wave_controller else {
            return Ok(RuntimeIoWaveTryAcquire::Acquired(None));
        };
        controller.try_acquire(slots, self).map(|permit| {
            permit.map_or(RuntimeIoWaveTryAcquire::Pending, |permit| {
                RuntimeIoWaveTryAcquire::Acquired(Some(permit))
            })
        })
    }

    /// Starts a bounded execution scope while retaining every parent limit and
    /// the original cancellation, memory and I/O admission. Clones share usage.
    pub fn with_work_limit(mut self, limit: usize) -> Self {
        self.work_budget = Some(Arc::new(work::RuntimeWorkBudget::new(
            limit,
            self.work_budget.take(),
        )));
        self
    }

    pub fn has_work_limit(&self) -> bool {
        self.work_budget.is_some()
    }

    pub fn work_units(&self) -> usize {
        self.work_budget.as_ref().map_or(0, |budget| budget.used())
    }

    /// Preserve a typed refusal across adapters whose error transport is text.
    pub fn check_work_limit(&self) -> crate::Result<()> {
        match self
            .work_budget
            .as_ref()
            .and_then(|budget| budget.refusal())
        {
            Some(error) => Err(error.into()),
            None => Ok(()),
        }
    }

    /// Charge explicit candidate work before examining it, including rejected
    /// candidates. Checkpoints and allocation byte counts are not work units.
    pub fn charge_work(&self, units: usize) -> crate::Result<()> {
        let Some(budget) = &self.work_budget else {
            return Ok(());
        };
        self.checkpoint().map_err(|reason| {
            crate::HawDBError::Execution(format!("runtime task stopped: {reason}"))
        })?;
        budget.charge(units)
    }

    pub fn deadline(&self) -> Option<Instant> {
        self.deadline
    }

    pub fn remaining(&self) -> Option<Duration> {
        self.deadline
            .map(|deadline| deadline.saturating_duration_since(Instant::now()))
    }

    #[inline]
    pub fn checkpoint(&self) -> Result<(), RuntimeCancellationReason> {
        self.check_cancellation()?;
        if self.deadline.is_some() {
            self.check_deadline_at(Instant::now())?;
        }
        Ok(())
    }

    /// Observe cancellation without reading the clock. Batched callers pair
    /// this per-step check with [`Self::check_deadline_at`] at bounded
    /// boundaries that already own a timestamp.
    #[inline]
    pub fn check_cancellation(&self) -> Result<(), RuntimeCancellationReason> {
        match self.cancellation.reason() {
            Some(reason) => Err(reason),
            None => Ok(()),
        }
    }

    /// Compare the deadline with a caller-supplied timestamp.
    #[inline]
    pub fn check_deadline_at(&self, now: Instant) -> Result<(), RuntimeCancellationReason> {
        if self.deadline.is_some_and(|deadline| now >= deadline) {
            return Err(RuntimeCancellationReason::DeadlineExceeded);
        }
        Ok(())
    }
}

impl Default for RuntimeTaskContext {
    fn default() -> Self {
        Self::without_deadline(RuntimeCancellationToken::new())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeCancellationReason {
    Cancelled,
    DeadlineExceeded,
    ResourceExhausted,
}

impl RuntimeCancellationReason {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Cancelled => "cancelled",
            Self::DeadlineExceeded => "deadline_exceeded",
            Self::ResourceExhausted => "resource_exhausted",
        }
    }
}

impl Display for RuntimeCancellationReason {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl Error for RuntimeCancellationReason {}

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

    #[derive(Debug)]
    struct RecordingIoController {
        active_slots: Arc<AtomicUsize>,
    }

    #[derive(Debug)]
    struct RecordingIoPermit {
        active_slots: Arc<AtomicUsize>,
        slots: usize,
    }

    impl Drop for RecordingIoPermit {
        fn drop(&mut self) {
            self.active_slots.fetch_sub(self.slots, Ordering::AcqRel);
        }
    }

    impl RuntimeIoWaveController for RecordingIoController {
        fn try_acquire(
            &self,
            slots: NonZeroUsize,
            context: &RuntimeTaskContext,
        ) -> Result<Option<Box<dyn RuntimeIoWavePermit>>, RuntimeIoWaveError> {
            self.acquire(slots, context).map(Some)
        }

        fn acquire(
            &self,
            slots: NonZeroUsize,
            context: &RuntimeTaskContext,
        ) -> Result<Box<dyn RuntimeIoWavePermit>, RuntimeIoWaveError> {
            context.checkpoint().map_err(RuntimeIoWaveError::Stopped)?;
            self.active_slots.fetch_add(slots.get(), Ordering::AcqRel);
            Ok(Box::new(RecordingIoPermit {
                active_slots: Arc::clone(&self.active_slots),
                slots: slots.get(),
            }))
        }
    }

    #[test]
    fn cancellation_is_shared_across_context_clones() {
        let token = RuntimeCancellationToken::new();
        let context = RuntimeTaskContext::without_deadline(token.clone());
        assert!(context.checkpoint().is_ok());
        assert!(token.cancel());
        assert!(!token.cancel());
        assert_eq!(
            context.checkpoint(),
            Err(RuntimeCancellationReason::Cancelled)
        );
    }

    #[test]
    fn expired_deadline_fails_at_a_cooperative_checkpoint() {
        let context = RuntimeTaskContext::new(
            RuntimeCancellationToken::new(),
            Some(Instant::now() - Duration::from_millis(1)),
        );
        assert_eq!(
            context.checkpoint(),
            Err(RuntimeCancellationReason::DeadlineExceeded)
        );
    }

    #[test]
    fn child_cancellation_is_local_and_parent_cancellation_propagates() {
        let parent_token = RuntimeCancellationToken::new();
        let parent = RuntimeTaskContext::without_deadline(parent_token.clone());
        let child = parent.child();

        assert!(child.cancellation().cancel());
        assert!(child.checkpoint().is_err());
        assert!(parent.checkpoint().is_ok());

        let sibling = parent.child();
        assert!(parent_token.cancel());
        assert!(parent.checkpoint().is_err());
        assert!(sibling.checkpoint().is_err());
    }

    #[test]
    fn resource_exhaustion_keeps_its_cause_and_is_local_to_the_child() {
        let parent = RuntimeTaskContext::default();
        let child = parent.child();
        let clone = child.clone();
        let grandchild = child.child();
        assert!(child.cancellation().cancel_for_resource_exhaustion());
        assert!(!clone.cancellation().cancel());
        assert!(!clone.cancellation().cancel_for_resource_exhaustion());
        for context in [&child, &clone, &grandchild] {
            assert_eq!(
                context.checkpoint(),
                Err(RuntimeCancellationReason::ResourceExhausted)
            );
        }
        assert_eq!(parent.checkpoint(), Ok(()));
        assert_eq!(parent.child().checkpoint(), Ok(()));

        let cancelled = parent.child();
        assert!(cancelled.cancellation().cancel());
        assert!(!cancelled.cancellation().cancel_for_resource_exhaustion());
        assert_eq!(
            cancelled.checkpoint(),
            Err(RuntimeCancellationReason::Cancelled)
        );
    }

    #[test]
    fn reason_fast_path_and_ancestor_walk_select_the_same_first_cause() {
        // Root tokens answer from their own state; descendants walk parents
        // depth first, first parent before second (#1651).
        let root = RuntimeCancellationToken::new();
        assert!(root.state.parents[0].is_none());
        assert_eq!(root.reason(), None);
        let first = RuntimeCancellationToken::new();
        let second = RuntimeCancellationToken::new();
        let linked = first.linked(&second);
        let grandchild = linked.child();
        assert_eq!(linked.reason(), None);
        assert_eq!(grandchild.reason(), None);
        assert!(second.cancel_for_resource_exhaustion());
        assert_eq!(
            grandchild.reason(),
            Some(RuntimeCancellationReason::ResourceExhausted)
        );
        assert!(first.cancel());
        for token in [&linked, &grandchild] {
            assert_eq!(token.reason(), Some(RuntimeCancellationReason::Cancelled));
        }
        assert!(grandchild.cancel_for_resource_exhaustion());
        assert_eq!(
            grandchild.reason(),
            Some(RuntimeCancellationReason::ResourceExhausted)
        );
        assert_eq!(linked.reason(), Some(RuntimeCancellationReason::Cancelled));
        assert_eq!(root.reason(), None);
        assert!(root.cancel());
        assert!(root.is_cancelled());
    }

    #[derive(Default)]
    struct WakeCounter(AtomicUsize);

    impl Wake for WakeCounter {
        fn wake(self: Arc<Self>) {
            self.0.fetch_add(1, Ordering::AcqRel);
        }
    }

    #[test]
    fn constrained_context_observes_both_cancellations_without_cancelling_parents() {
        for cancel_parent in [false, true] {
            let parent = RuntimeTaskContext::default();
            let request = RuntimeTaskContext::default();
            let combined = parent.constrained_by(&request).unwrap();
            let counter = Arc::new(WakeCounter::default());
            let waker = Waker::from(counter.clone());
            let mut context = Context::from_waker(&waker);
            let mut cancelled = Box::pin(combined.cancellation().cancelled());
            assert!(cancelled.as_mut().poll(&mut context).is_pending());
            if cancel_parent {
                parent.cancellation().cancel();
            } else {
                request.cancellation().cancel();
            }
            assert_eq!(counter.0.load(Ordering::Acquire), 1);
            assert!(cancelled.as_mut().poll(&mut context).is_ready());
            assert_eq!(
                combined.checkpoint(),
                Err(RuntimeCancellationReason::Cancelled)
            );
        }
        let parent = RuntimeTaskContext::default();
        let request = RuntimeTaskContext::default();
        let combined = parent.constrained_by(&request).unwrap();
        combined.cancellation().cancel();
        assert!(parent.checkpoint().is_ok());
        assert!(request.checkpoint().is_ok());
    }

    #[test]
    fn constrained_descendants_register_shared_ancestors_once_and_unregister_on_drop() {
        let parent = RuntimeTaskContext::default();
        let request = parent.child();
        let combined = parent.constrained_by(&request).unwrap();
        let waker = Waker::from(Arc::new(WakeCounter::default()));
        let mut context = Context::from_waker(&waker);
        let mut cancelled = Box::pin(combined.cancellation().cancelled());
        assert!(cancelled.as_mut().poll(&mut context).is_pending());
        assert_eq!(parent.cancellation.state.waiters.lock().unwrap().len(), 1);
        assert_eq!(request.cancellation.state.waiters.lock().unwrap().len(), 1);
        drop(cancelled);
        assert!(parent.cancellation.state.waiters.lock().unwrap().is_empty());
        assert!(request
            .cancellation
            .state
            .waiters
            .lock()
            .unwrap()
            .is_empty());
    }

    #[test]
    fn constrained_context_tightens_limits_and_keeps_the_original_io_controller() {
        let active_slots = Arc::new(AtomicUsize::new(0));
        let parent = RuntimeTaskContext::with_timeout(Duration::from_secs(5))
            .with_memory_reservation(RuntimeMemoryReservation::new(100, 20))
            .with_admitted_parallelism(NonZeroUsize::new(4).unwrap())
            .with_io_wave_controller(Arc::new(RecordingIoController {
                active_slots: active_slots.clone(),
            }));
        let request = RuntimeTaskContext::with_timeout(Duration::from_secs(10))
            .with_memory_reservation(RuntimeMemoryReservation::new(30, 50))
            .with_admitted_parallelism(NonZeroUsize::new(2).unwrap());
        let combined = parent.constrained_by(&request).unwrap();
        assert_eq!(combined.deadline(), parent.deadline());
        assert_eq!(
            combined.memory_reservation(),
            Some(RuntimeMemoryReservation::new(30, 20))
        );
        assert_eq!(combined.admitted_parallelism().get(), 2);
        let io = combined.acquire_io_wave(NonZeroUsize::MIN).unwrap();
        assert_eq!(active_slots.load(Ordering::Acquire), 1);
        drop(io);
        assert_eq!(active_slots.load(Ordering::Acquire), 0);
        assert!(parent.constrained_by(&parent.child()).is_ok());
        let independent =
            request.with_io_wave_controller(Arc::new(RecordingIoController { active_slots }));
        assert!(parent.constrained_by(&independent).is_err());
    }

    #[test]
    fn cancellation_future_is_woken_by_parent_cancellation() {
        let parent = RuntimeCancellationToken::new();
        let child = parent.child();
        let counter = Arc::new(WakeCounter::default());
        let waker = Waker::from(Arc::clone(&counter));
        let mut context = Context::from_waker(&waker);
        let mut cancelled = Box::pin(child.cancelled());

        assert!(cancelled.as_mut().poll(&mut context).is_pending());
        assert!(parent.cancel());
        assert_eq!(counter.0.load(Ordering::Acquire), 1);
        assert!(cancelled.as_mut().poll(&mut context).is_ready());
    }

    #[test]
    fn resource_exhaustion_wakes_waiters_on_descendants() {
        let parent = RuntimeTaskContext::default();
        let child = parent.child();
        let counter = Arc::new(WakeCounter::default());
        let waker = Waker::from(Arc::clone(&counter));
        let mut context = Context::from_waker(&waker);
        let mut cancelled = Box::pin(child.cancellation().cancelled());
        assert!(cancelled.as_mut().poll(&mut context).is_pending());
        assert!(parent.cancellation().cancel_for_resource_exhaustion());
        assert_eq!(counter.0.load(Ordering::Acquire), 1);
        assert!(cancelled.as_mut().poll(&mut context).is_ready());
        assert_eq!(
            child.checkpoint(),
            Err(RuntimeCancellationReason::ResourceExhausted)
        );
    }

    #[test]
    fn dropped_cancellation_future_unregisters_from_its_parent_chain() {
        let parent = RuntimeCancellationToken::new();
        let child = parent.child();
        let counter = Arc::new(WakeCounter::default());
        let waker = Waker::from(Arc::clone(&counter));
        let mut context = Context::from_waker(&waker);
        let mut cancelled = Box::pin(child.cancelled());

        assert!(cancelled.as_mut().poll(&mut context).is_pending());
        drop(cancelled);
        assert!(parent.cancel());
        assert_eq!(counter.0.load(Ordering::Acquire), 0);
    }

    #[test]
    fn child_preserves_admitted_parallelism() {
        let context = RuntimeTaskContext::default()
            .with_admitted_parallelism(NonZeroUsize::new(4).expect("test parallelism is non-zero"))
            .with_executor_thread_limit(
                NonZeroUsize::new(2).expect("test thread limit is non-zero"),
            );

        assert_eq!(context.admitted_parallelism().get(), 4);
        assert_eq!(context.child().admitted_parallelism().get(), 4);
        assert_eq!(context.child().executor_thread_limit().unwrap().get(), 2);
    }

    #[test]
    fn child_preserves_memory_reservation() {
        let reservation = RuntimeMemoryReservation::new(128, 32);
        let context = RuntimeTaskContext::default().with_memory_reservation(reservation);

        assert_eq!(context.memory_reservation(), Some(reservation));
        assert_eq!(context.child().memory_reservation(), Some(reservation));
    }

    #[test]
    fn child_preserves_io_controller_and_permit_releases_slots() {
        let active_slots = Arc::new(AtomicUsize::new(0));
        let context = RuntimeTaskContext::default().with_io_wave_controller(Arc::new(
            RecordingIoController {
                active_slots: Arc::clone(&active_slots),
            },
        ));

        let permit = context
            .child()
            .acquire_io_wave(NonZeroUsize::new(2).unwrap())
            .unwrap()
            .unwrap();
        assert_eq!(active_slots.load(Ordering::Acquire), 2);
        drop(permit);
        assert_eq!(active_slots.load(Ordering::Acquire), 0);
    }

    #[test]
    fn ungoverned_try_acquire_is_immediately_ready() {
        let acquired = RuntimeTaskContext::default()
            .try_acquire_io_wave(NonZeroUsize::new(2).unwrap())
            .unwrap();

        assert!(matches!(acquired, RuntimeIoWaveTryAcquire::Acquired(None)));
    }
}