ic-timers 0.6.1

Observable and recovery-aware timers for Internet Computer canisters
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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
//! Canister-local owner and live timer execution.

use crate::{
    platform::{self, TimerHandle},
    registry::{
        CallbackAcceptance, CallbackRole, CallbackToken, OrdinaryCallback, ProviderHandle,
        ProviderHandles, RegisterError, RegistrationClaim, RegistryEffect, RegistryError,
        RegistryTransition, TimerRegistry, WatchdogCallback,
    },
    schedule::{ScheduleError, TimerCadence, TimerDirective, TimerSchedule},
    snapshot::{
        DeclarationLifetime, TimerCompletion, TimerControlFailure, TimerEpoch, TimerIdentity,
        TimerInventorySnapshot, TimerPolicy, TimerRunResult, TimerSnapshot, WatchdogRunResult,
    },
};
use std::{cell::RefCell, future::Future, rc::Rc, time::Duration};
use thiserror::Error;

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

/// Failure from the canister-local timer runtime API.
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum TimerError {
    /// The lifecycle owner has not initialized the volatile runtime.
    #[error("timer runtime is not initialized")]
    NotInitialized,
    /// A nested internal borrow indicates unsupported re-entrancy.
    #[error("timer runtime is already borrowed")]
    RuntimeBusy,
    /// Claiming one canonical timer identity failed.
    #[error(transparent)]
    Register(#[from] RegisterError),
    /// Cadence or deadline validation failed.
    #[error(transparent)]
    Schedule(#[from] ScheduleError),
    /// The logical registration was removed or superseded.
    #[error("timer registration is no longer authoritative")]
    RegistrationExpired,
    /// Pure checked control reached a terminal failure after effects were applied.
    #[error("timer control failed: {0:?}")]
    ControlFailure(TimerControlFailure),
    /// Canonical callback or provider-handle ownership was internally inconsistent.
    #[error("timer runtime ownership invariant failed")]
    OwnershipInvariant,
    /// A retained lifecycle claim no longer matches its canonical declaration.
    #[error("timer lifecycle reconciliation conflicts with the canonical declaration")]
    ReconciliationConflict,
}

impl From<RegistryError> for TimerError {
    fn from(value: RegistryError) -> Self {
        match value {
            RegistryError::UnknownRegistration
            | RegistryError::StaleRegistration
            | RegistryError::StaleCallback => Self::RegistrationExpired,
            RegistryError::PolicyMismatch { .. } => Self::OwnershipInvariant,
            RegistryError::Schedule(error) => Self::Schedule(error),
            RegistryError::MissingCallback | RegistryError::ProviderHandleAlreadyOwned => {
                Self::OwnershipInvariant
            }
        }
    }
}

/// Initialize the volatile canister-local runtime once for this Wasm instance.
///
/// Repeated calls are idempotent and return the original epoch. This function
/// exports no lifecycle hook; the canister's existing lifecycle owner calls it.
pub fn initialize_runtime() -> Result<TimerEpoch, TimerError> {
    let epoch = TimerEpoch::new(platform::canister_version(), platform::time_ns());
    RUNTIME.with(|runtime| {
        let mut runtime = runtime
            .try_borrow_mut()
            .map_err(|_| TimerError::RuntimeBusy)?;
        if let Some(registry) = runtime.as_ref() {
            return Ok(registry.epoch());
        }
        *runtime = Some(TimerRegistry::new(epoch));
        Ok(epoch)
    })
}

struct CallbackContext {
    token: CallbackToken,
}

impl CallbackContext {
    const fn new(token: CallbackToken) -> Self {
        Self { token }
    }

    fn claim(&self) -> RegistrationClaim {
        RegistrationClaim::from_callback(&self.token)
    }

    const fn identity(&self) -> &TimerIdentity {
        self.token.identity()
    }

    fn cancel(&self) -> Result<(), TimerError> {
        cancel_claim(&self.claim(), Some(&self.token))
    }

    fn schedule_once(&self, schedule: TimerSchedule) -> Result<(), TimerError> {
        ensure_once_claim(&self.claim(), Some(&self.token), schedule)
    }

    fn schedule_recurring(&self) -> Result<(), TimerError> {
        ensure_recurring_claim(&self.claim(), Some(&self.token))
    }

    fn reconcile_ordinary(&self, schedule: Option<TimerSchedule>) -> Result<(), TimerError> {
        reconcile_ordinary_claim(&self.claim(), Some(&self.token), schedule)
    }
}

/// Delegated control capability scoped to one exact `Once` work attempt.
///
/// Identity remains inspectable after work returns, but mutation methods then
/// return [`TimerError::RegistrationExpired`]. Retain the
/// [`OnceRegistration`] for longer-lived ownership.
pub struct OnceContext {
    inner: CallbackContext,
}

impl OnceContext {
    const fn new(token: CallbackToken) -> Self {
        Self {
            inner: CallbackContext::new(token),
        }
    }

    /// Return the logical timer identity executing this work.
    #[must_use]
    pub const fn identity(&self) -> &TimerIdentity {
        self.inner.identity()
    }

    /// Schedule a `Once` declaration while this exact work attempt is active.
    ///
    /// A context retained after its callback completes is expired and returns
    /// [`TimerError::RegistrationExpired`].
    pub fn ensure_scheduled(&self, schedule: TimerSchedule) -> Result<(), TimerError> {
        self.inner.schedule_once(schedule)
    }

    /// Reconcile the executing declaration to one exact schedule.
    ///
    /// `None` requests inactive state at normal completion. Retained callback
    /// authority remains; a remove-on-stop declaration is removed when that
    /// cancellation wins arbitration. A stored context cannot mutate the
    /// registration after its exact work attempt ends.
    pub fn reconcile_schedule(&self, schedule: Option<TimerSchedule>) -> Result<(), TimerError> {
        self.inner.reconcile_ordinary(schedule)
    }

    /// Request cancellation while this exact work attempt is active.
    ///
    /// Cancellation does not interrupt the current invocation; normal
    /// completion applies it before any callback successor is retained.
    /// A retained declaration becomes inactive. A remove-on-stop declaration
    /// is removed when cancellation wins arbitration.
    pub fn cancel(&self) -> Result<(), TimerError> {
        self.inner.cancel()
    }
}

/// Delegated control capability scoped to one exact after-completion work
/// attempt.
///
/// Mutation authority expires when the callback completes. Retain the
/// [`AfterCompletionRegistration`] for longer-lived ownership.
pub struct AfterCompletionContext {
    inner: CallbackContext,
}

impl AfterCompletionContext {
    const fn new(token: CallbackToken) -> Self {
        Self {
            inner: CallbackContext::new(token),
        }
    }

    /// Return the logical timer identity executing this work.
    #[must_use]
    pub const fn identity(&self) -> &TimerIdentity {
        self.inner.identity()
    }

    /// Request configured recurrence after this exact work attempt.
    pub fn ensure_scheduled(&self) -> Result<(), TimerError> {
        self.inner.schedule_recurring()
    }

    /// Reconcile the executing declaration to one exact schedule without
    /// changing its configured after-completion cadence.
    ///
    /// `None` requests inactive state at normal completion. A retained
    /// declaration keeps its callback authority; a remove-on-stop declaration
    /// is removed when cancellation wins arbitration. A stored context cannot
    /// mutate the registration after its exact work attempt ends.
    pub fn reconcile_schedule(&self, schedule: Option<TimerSchedule>) -> Result<(), TimerError> {
        self.inner.reconcile_ordinary(schedule)
    }

    /// Request cancellation while this exact work attempt is active.
    ///
    /// Cancellation does not interrupt the current invocation; normal
    /// completion applies it before any callback successor is retained. A
    /// retained declaration becomes inactive; a remove-on-stop declaration is
    /// removed when cancellation wins arbitration.
    pub fn cancel(&self) -> Result<(), TimerError> {
        self.inner.cancel()
    }
}

/// Delegated control capability scoped to one exact watchdog work attempt.
///
/// Mutation authority expires when the callback completes. Retain the
/// [`WatchdogRegistration`] for longer-lived ownership.
pub struct WatchdogContext {
    inner: CallbackContext,
}

impl WatchdogContext {
    const fn new(token: CallbackToken) -> Self {
        Self {
            inner: CallbackContext::new(token),
        }
    }

    /// Return the logical timer identity executing this work.
    #[must_use]
    pub const fn identity(&self) -> &TimerIdentity {
        self.inner.identity()
    }

    /// Request that the pre-armed cadence successor remain scheduled.
    ///
    /// This is idempotent unless it supersedes a nested cancellation request.
    /// It never arms an additional Watchdog successor.
    pub fn ensure_scheduled(&self) -> Result<(), TimerError> {
        self.inner.schedule_recurring()
    }

    /// Request cancellation while this exact work attempt is active.
    ///
    /// Cancellation does not interrupt the current invocation; normal
    /// completion clears the already-armed successor when cancellation wins.
    /// A retained declaration becomes inactive; a remove-on-stop declaration
    /// is removed.
    pub fn cancel(&self) -> Result<(), TimerError> {
        self.inner.cancel()
    }
}

/// Opaque non-clone claim for one registered `Once` callback.
#[must_use = "retain the registration claim so the timer remains controllable"]
pub struct OnceRegistration {
    claim: RegistrationClaim,
}

impl OnceRegistration {
    /// Return the claimed logical identity.
    #[must_use]
    pub const fn identity(&self) -> &TimerIdentity {
        self.claim.identity()
    }

    /// Return whether this exact claim currently owns an armed provider wake-up.
    ///
    /// This is a volatile observation, not durable scheduling authority or a
    /// delivery guarantee. Call [`Self::ensure_scheduled`] unconditionally when
    /// a wake-up is required rather than using this value as a scheduling guard.
    pub fn has_armed_wakeup(&self) -> Result<bool, TimerError> {
        has_armed_wakeup_claim(&self.claim)
    }

    /// Ensure one invocation is armed or retained as the running work's successor.
    pub fn ensure_scheduled(&self, schedule: TimerSchedule) -> Result<(), TimerError> {
        ensure_once_claim(&self.claim, None, schedule)
    }

    /// Reconcile to one exact desired schedule, replacing a later or earlier
    /// live deadline as necessary.
    ///
    /// `None` leaves a retained declaration inactive after any running work
    /// completes. A remove-on-stop declaration is removed and this claim
    /// expires when the transition finalizes.
    pub fn reconcile_schedule(&self, schedule: Option<TimerSchedule>) -> Result<(), TimerError> {
        reconcile_ordinary_claim(&self.claim, None, schedule)
    }

    /// Cancel the armed callback or the running work's successor.
    ///
    /// Consumer work already running is not interrupted.
    /// A retained declaration keeps callback authority. A remove-on-stop
    /// declaration and this claim expire when cancellation finalizes.
    pub fn cancel(&self) -> Result<(), TimerError> {
        cancel_claim(&self.claim, None)
    }

    /// Consume the claim and unregister its callback authority.
    ///
    /// When called from running work, removal is deferred until that invocation
    /// completes normally.
    pub fn unregister(self) -> Result<(), TimerError> {
        unregister_claim(&self.claim)
    }
}

/// Opaque non-clone claim for one callback with configured
/// after-completion recurrence.
#[must_use = "retain the registration claim so the timer remains controllable"]
pub struct AfterCompletionRegistration {
    claim: RegistrationClaim,
}

/// Opaque non-clone claim for one pre-armed watchdog callback.
#[must_use = "retain the registration claim so the timer remains controllable"]
pub struct WatchdogRegistration {
    claim: RegistrationClaim,
}

trait RegistrationClaimOwner {
    fn registration_claim(&self) -> &RegistrationClaim;
}

impl RegistrationClaimOwner for OnceRegistration {
    fn registration_claim(&self) -> &RegistrationClaim {
        &self.claim
    }
}

impl RegistrationClaimOwner for AfterCompletionRegistration {
    fn registration_claim(&self) -> &RegistrationClaim {
        &self.claim
    }
}

impl RegistrationClaimOwner for WatchdogRegistration {
    fn registration_claim(&self) -> &RegistrationClaim {
        &self.claim
    }
}

/// Desired volatile scheduling state during synchronous lifecycle reconciliation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TimerReconcileState {
    /// Request inactive state immediately unless consumer work is running.
    Inactive,
    /// Preserve scheduling demand now or through the running work's successor.
    Scheduled,
}

impl WatchdogRegistration {
    /// Return the claimed logical identity.
    #[must_use]
    pub const fn identity(&self) -> &TimerIdentity {
        self.claim.identity()
    }

    /// Return whether this exact claim currently owns an armed scheduler wake-up.
    ///
    /// The separately queued work callback is not itself a wake-up. During
    /// watchdog work this returns `true` only because the scheduler has already
    /// committed and installed the cadence successor. This is a volatile
    /// observation, not a delivery guarantee.
    pub fn has_armed_wakeup(&self) -> Result<bool, TimerError> {
        has_armed_wakeup_claim(&self.claim)
    }

    /// Synchronously ensure one watchdog scheduler wake-up is authoritative.
    pub fn ensure_scheduled(&self) -> Result<(), TimerError> {
        ensure_recurring_claim(&self.claim, None)
    }

    /// Cancel the scheduler and any work callback that has not started.
    ///
    /// Consumer work already running is not interrupted; normal completion
    /// clears its pre-armed successor.
    /// A retained declaration keeps callback authority. A remove-on-stop
    /// declaration and this claim expire when cancellation finalizes.
    pub fn cancel(&self) -> Result<(), TimerError> {
        cancel_claim(&self.claim, None)
    }

    /// Consume the claim and unregister its callback authority.
    ///
    /// When called from running work, removal is deferred until that invocation
    /// completes normally.
    pub fn unregister(self) -> Result<(), TimerError> {
        unregister_claim(&self.claim)
    }
}

impl AfterCompletionRegistration {
    /// Return the claimed logical identity.
    #[must_use]
    pub const fn identity(&self) -> &TimerIdentity {
        self.claim.identity()
    }

    /// Return whether this exact claim currently owns an armed provider wake-up.
    ///
    /// A callback currently running without an installed successor returns
    /// `false`. This is a volatile observation, not durable scheduling authority
    /// or a delivery guarantee.
    pub fn has_armed_wakeup(&self) -> Result<bool, TimerError> {
        has_armed_wakeup_claim(&self.claim)
    }

    /// Ensure configured recurrence is armed or retained as the running work's
    /// successor.
    pub fn ensure_scheduled(&self) -> Result<(), TimerError> {
        ensure_recurring_claim(&self.claim, None)
    }

    /// Reconcile to one exact desired schedule without changing the configured
    /// after-completion cadence. `None` makes a retained declaration inactive
    /// after any running work completes; it removes a remove-on-stop
    /// declaration and expires this claim when the transition finalizes.
    pub fn reconcile_schedule(&self, schedule: Option<TimerSchedule>) -> Result<(), TimerError> {
        reconcile_ordinary_claim(&self.claim, None, schedule)
    }

    /// Cancel the armed callback or the running work's successor.
    ///
    /// Consumer work already running is not interrupted.
    /// A retained declaration keeps callback authority. A remove-on-stop
    /// declaration and this claim expire when cancellation finalizes.
    pub fn cancel(&self) -> Result<(), TimerError> {
        cancel_claim(&self.claim, None)
    }

    /// Consume the claim and unregister its callback authority.
    ///
    /// When called from running work, removal is deferred until that invocation
    /// completes normally.
    pub fn unregister(self) -> Result<(), TimerError> {
        unregister_claim(&self.claim)
    }
}

/// Register one asynchronous `Once` callback without scheduling it.
pub fn register_once<F, Fut>(
    identity: TimerIdentity,
    lifetime: DeclarationLifetime,
    callback: F,
) -> Result<OnceRegistration, TimerError>
where
    F: FnMut(OnceContext) -> Fut + 'static,
    Fut: Future<Output = TimerRunResult> + 'static,
{
    let callback = erase_ordinary_callback(callback, OnceContext::new);
    let claim = with_registry_mut(|registry| {
        registry
            .register_once_with_callback(identity, lifetime, callback)
            .map_err(TimerError::from)
    })?;
    Ok(OnceRegistration { claim })
}

/// Register one asynchronous callback with configured after-completion recurrence.
pub fn register_after_completion<F, Fut>(
    identity: TimerIdentity,
    cadence: TimerCadence,
    lifetime: DeclarationLifetime,
    callback: F,
) -> Result<AfterCompletionRegistration, TimerError>
where
    F: FnMut(AfterCompletionContext) -> Fut + 'static,
    Fut: Future<Output = TimerRunResult> + 'static,
{
    let callback = erase_ordinary_callback(callback, AfterCompletionContext::new);
    let claim = with_registry_mut(|registry| {
        registry
            .register_after_completion_with_callback(identity, cadence, lifetime, callback)
            .map_err(TimerError::from)
    })?;
    Ok(AfterCompletionRegistration { claim })
}

/// Register one synchronous pre-armed watchdog callback without scheduling it.
///
/// The callback cannot be async: it runs only in the work message after a
/// separate scheduler message has armed the next cadence successor. Its
/// `WatchdogDecision` either retains or clears that committed successor.
pub fn register_watchdog<F>(
    identity: TimerIdentity,
    cadence: TimerCadence,
    lifetime: DeclarationLifetime,
    callback: F,
) -> Result<WatchdogRegistration, TimerError>
where
    F: FnMut(WatchdogContext) -> WatchdogRunResult + 'static,
{
    let mut callback = callback;
    let callback: WatchdogCallback = Rc::new(RefCell::new(Box::new(move |token| {
        callback(WatchdogContext::new(token))
    })));
    let claim = with_registry_mut(|registry| {
        registry
            .register_watchdog_with_callback(identity, cadence, lifetime, callback)
            .map_err(TimerError::from)
    })?;
    Ok(WatchdogRegistration { claim })
}

/// Reconstruct or reconcile one `Once` declaration synchronously.
///
/// `Some(schedule)` is authoritative and may move an existing deadline in
/// either direction. `None` retains an inactive declaration in the canonical
/// inventory, including on a fresh heap. Lifecycle reconciliation always owns
/// a [`DeclarationLifetime::Retained`] declaration; transient
/// `RemoveWhenStopped` callbacks use [`register_once`] directly.
pub fn reconcile_once<F, Fut>(
    registration: &mut Option<OnceRegistration>,
    identity: &TimerIdentity,
    desired: Option<TimerSchedule>,
    callback: F,
) -> Result<(), TimerError>
where
    F: FnMut(OnceContext) -> Fut + 'static,
    Fut: Future<Output = TimerRunResult> + 'static,
{
    let registration = reconcile_registration(registration, identity, TimerPolicy::Once, || {
        register_once(identity.clone(), DeclarationLifetime::Retained, callback)
    })?;
    registration.reconcile_schedule(desired)
}

/// Reconstruct or reconcile one after-completion declaration synchronously.
///
/// The consumer owns `registration` in volatile state. A fresh Wasm heap has
/// `None`, so this function installs callback authority before reconciling it
/// active or inactive. A repeated call reuses the exact claim and does not
/// replace its callback. The installed declaration is always retained;
/// transient `RemoveWhenStopped` recurrence uses
/// [`register_after_completion`] directly.
pub fn reconcile_after_completion<F, Fut>(
    registration: &mut Option<AfterCompletionRegistration>,
    identity: &TimerIdentity,
    cadence: TimerCadence,
    desired: TimerReconcileState,
    callback: F,
) -> Result<(), TimerError>
where
    F: FnMut(AfterCompletionContext) -> Fut + 'static,
    Fut: Future<Output = TimerRunResult> + 'static,
{
    let registration = reconcile_registration(
        registration,
        identity,
        TimerPolicy::AfterCompletion { cadence },
        || {
            register_after_completion(
                identity.clone(),
                cadence,
                DeclarationLifetime::Retained,
                callback,
            )
        },
    )?;
    match desired {
        TimerReconcileState::Inactive => registration.cancel(),
        TimerReconcileState::Scheduled => registration.ensure_scheduled(),
    }
}

/// Reconstruct or reconcile one watchdog declaration synchronously.
///
/// Durable readiness remains consumer-owned. Fresh inactive authority still
/// installs an observable retained declaration. This helper owns no lifecycle
/// export and persists no policy, generation, provider handle, or callback.
/// Transient `RemoveWhenStopped` watchdogs use [`register_watchdog`] directly.
pub fn reconcile_watchdog<F>(
    registration: &mut Option<WatchdogRegistration>,
    identity: &TimerIdentity,
    cadence: TimerCadence,
    desired: TimerReconcileState,
    callback: F,
) -> Result<(), TimerError>
where
    F: FnMut(WatchdogContext) -> WatchdogRunResult + 'static,
{
    let registration = reconcile_registration(
        registration,
        identity,
        TimerPolicy::Watchdog { cadence },
        || {
            register_watchdog(
                identity.clone(),
                cadence,
                DeclarationLifetime::Retained,
                callback,
            )
        },
    )?;
    match desired {
        TimerReconcileState::Inactive => registration.cancel(),
        TimerReconcileState::Scheduled => registration.ensure_scheduled(),
    }
}

fn reconcile_registration<'a, Registration>(
    registration: &'a mut Option<Registration>,
    identity: &TimerIdentity,
    policy: TimerPolicy,
    register: impl FnOnce() -> Result<Registration, TimerError>,
) -> Result<&'a Registration, TimerError>
where
    Registration: RegistrationClaimOwner,
{
    if registration.is_none() {
        *registration = Some(register()?);
    }
    let registration = registration
        .as_ref()
        .ok_or(TimerError::ReconciliationConflict)?;
    verify_declaration(registration.registration_claim(), identity, policy)?;
    Ok(registration)
}

fn verify_declaration(
    claim: &RegistrationClaim,
    identity: &TimerIdentity,
    policy: TimerPolicy,
) -> Result<(), TimerError> {
    if claim.identity() != identity {
        return Err(TimerError::ReconciliationConflict);
    }
    with_registry(|registry| {
        registry
            .declaration_matches(claim, policy, DeclarationLifetime::Retained)
            .map_err(TimerError::from)?
            .then_some(())
            .ok_or(TimerError::ReconciliationConflict)
    })
}

/// Return one coherent inert snapshot by identity.
pub fn timer_snapshot(identity: &TimerIdentity) -> Result<Option<TimerSnapshot>, TimerError> {
    with_registry(|registry| Ok(registry.snapshot(identity)))
}

/// Return one atomic bounded inventory with its volatile runtime epoch.
///
/// Timer snapshots are ordered deterministically by identity. An initialized
/// empty registry still returns its epoch and an empty timer slice.
pub fn timer_inventory() -> Result<TimerInventorySnapshot, TimerError> {
    with_registry(|registry| Ok(registry.inventory()))
}

/// Return functional expected-failure state by identity without a full inventory.
pub fn consecutive_expected_failures(identity: &TimerIdentity) -> Result<Option<u64>, TimerError> {
    with_registry(|registry| Ok(registry.consecutive_expected_failures(identity)))
}

fn has_armed_wakeup_claim(claim: &RegistrationClaim) -> Result<bool, TimerError> {
    with_registry(|registry| registry.has_armed_wakeup(claim).map_err(TimerError::from))
}

fn erase_ordinary_callback<Context: 'static, F, Fut>(
    mut callback: F,
    context: fn(CallbackToken) -> Context,
) -> OrdinaryCallback
where
    F: FnMut(Context) -> Fut + 'static,
    Fut: Future<Output = TimerRunResult> + 'static,
{
    Rc::new(RefCell::new(Box::new(move |token| {
        Box::pin(callback(context(token)))
    })))
}

fn apply_claim_transition(
    claim: &RegistrationClaim,
    context: Option<&CallbackToken>,
    operation: impl FnOnce(&mut TimerRegistry) -> Result<RegistryTransition, RegistryError>,
) -> Result<(), TimerError> {
    let transition = with_registry_mut(|registry| {
        validate_context(registry, context)?;
        operation(registry).map_err(TimerError::from)
    })?;
    finish_claim_transition(claim, transition, ProviderHandles::default())
}

fn ensure_once_claim(
    claim: &RegistrationClaim,
    context: Option<&CallbackToken>,
    schedule: TimerSchedule,
) -> Result<(), TimerError> {
    apply_claim_transition(claim, context, |registry| {
        registry.ensure_once(claim, platform::time_ns(), schedule)
    })
}

fn reconcile_ordinary_claim(
    claim: &RegistrationClaim,
    context: Option<&CallbackToken>,
    schedule: Option<TimerSchedule>,
) -> Result<(), TimerError> {
    if schedule.is_none() {
        let (handles, transition) = with_registry_mut(|registry| {
            validate_context(registry, context)?;
            registry
                .validate_ordinary_claim(claim)
                .map_err(TimerError::from)?;
            let handles = registry
                .take_provider_handles_for_claim(claim)
                .map_err(TimerError::from)?;
            let transition = registry
                .reconcile_ordinary(claim, platform::time_ns(), None)
                .map_err(TimerError::from);
            Ok((handles, transition))
        })?;
        return finish_detached_claim_transition(claim, handles, transition);
    }
    apply_claim_transition(claim, context, |registry| {
        registry.reconcile_ordinary(claim, platform::time_ns(), schedule)
    })
}

fn ensure_recurring_claim(
    claim: &RegistrationClaim,
    context: Option<&CallbackToken>,
) -> Result<(), TimerError> {
    apply_claim_transition(claim, context, |registry| {
        registry.ensure_recurring(claim, platform::time_ns())
    })
}

fn cancel_claim(
    claim: &RegistrationClaim,
    context: Option<&CallbackToken>,
) -> Result<(), TimerError> {
    apply_detached_claim_transition(claim, context, |registry| registry.cancel(claim))
}

fn validate_context(
    registry: &TimerRegistry,
    context: Option<&CallbackToken>,
) -> Result<(), TimerError> {
    context.map_or(Ok(()), |token| {
        registry
            .validate_running_context(token)
            .map_err(TimerError::from)
    })
}

fn unregister_claim(claim: &RegistrationClaim) -> Result<(), TimerError> {
    apply_detached_claim_transition(claim, None, |registry| registry.unregister(claim))
}

fn apply_detached_claim_transition(
    claim: &RegistrationClaim,
    context: Option<&CallbackToken>,
    operation: impl FnOnce(&mut TimerRegistry) -> Result<RegistryTransition, RegistryError>,
) -> Result<(), TimerError> {
    let (handles, transition) = with_registry_mut(|registry| {
        validate_context(registry, context)?;
        let handles = registry
            .take_provider_handles_for_claim(claim)
            .map_err(TimerError::from)?;
        let transition = operation(registry).map_err(TimerError::from);
        Ok((handles, transition))
    })?;
    finish_detached_claim_transition(claim, handles, transition)
}

fn finish_detached_claim_transition(
    claim: &RegistrationClaim,
    handles: ProviderHandles,
    transition: Result<RegistryTransition, TimerError>,
) -> Result<(), TimerError> {
    match transition {
        Ok(transition) => finish_claim_transition(claim, transition, handles),
        Err(error) => match restore_provider_handles(handles) {
            Ok(()) => Err(error),
            Err(restoration_error) => retire_failed_claim(claim, restoration_error),
        },
    }
}

fn finish_claim_transition(
    claim: &RegistrationClaim,
    transition: RegistryTransition,
    handles: ProviderHandles,
) -> Result<(), TimerError> {
    match finish_transition(transition, handles) {
        result @ (Ok(()) | Err(TimerError::ControlFailure(_))) => result,
        Err(error) => retire_failed_claim(claim, error),
    }
}

fn retire_failed_claim(claim: &RegistrationClaim, error: TimerError) -> Result<(), TimerError> {
    match fail_claim_provider_binding(claim) {
        Ok(()) | Err(TimerError::RegistrationExpired) => Err(error),
        Err(cleanup_error) => Err(cleanup_error),
    }
}

fn finish_transition(
    transition: RegistryTransition,
    handles: ProviderHandles,
) -> Result<(), TimerError> {
    let failure = transition.failure();
    let effect = transition.into_effect();
    apply_effect(&effect, handles)?;
    failure.map_or(Ok(()), |failure| Err(TimerError::ControlFailure(failure)))
}

fn apply_effect(effect: &RegistryEffect, mut handles: ProviderHandles) -> Result<(), TimerError> {
    if !effect.has_valid_shape() {
        clear_provider_handles(handles);
        return Err(TimerError::OwnershipInvariant);
    }
    match effect {
        RegistryEffect::None => restore_provider_handles(handles),
        RegistryEffect::ArmWakeup { token, arm, .. } => {
            if arm.replaces_existing() {
                let replaced = take_detached_or_owned_handle(handles.take_wakeup(), |registry| {
                    registry.take_wakeup_handle(token.identity())
                })?;
                if let Some(replaced) = replaced {
                    clear_provider_handle(replaced);
                }
            }
            restore_provider_handles(handles)?;
            arm_wakeup(effect)
        }
        RegistryEffect::ClearCallbacks {
            identity,
            handles: selected,
        } => {
            if selected.includes_wakeup() {
                let wakeup = take_detached_or_owned_handle(handles.take_wakeup(), |registry| {
                    registry.take_wakeup_handle(identity)
                })?;
                if let Some(wakeup) = wakeup {
                    clear_provider_handle(wakeup);
                }
            }
            if selected.includes_work() {
                let work = take_detached_or_owned_handle(handles.take_work(), |registry| {
                    registry.take_work_handle(identity)
                })?;
                if let Some(work) = work {
                    clear_provider_handle(work);
                }
            }
            restore_provider_handles(handles)
        }
        RegistryEffect::DispatchWatchdog { successor, .. } => {
            if let Some(wakeup) = handles.take_wakeup() {
                clear_provider_handle(wakeup);
            }
            let replaced_work = take_detached_or_owned_handle(handles.take_work(), |registry| {
                registry.take_work_handle(successor.identity())
            })?;
            if let Some(replaced_work) = replaced_work {
                clear_provider_handle(replaced_work);
            }
            dispatch_watchdog_effect(effect)
        }
    }
}

fn take_detached_or_owned_handle(
    detached: Option<ProviderHandle>,
    take_owned: impl FnOnce(&mut TimerRegistry) -> Option<ProviderHandle>,
) -> Result<Option<ProviderHandle>, TimerError> {
    detached.map_or_else(
        || with_registry_mut(|registry| Ok(take_owned(registry))),
        |handle| Ok(Some(handle)),
    )
}

fn arm_wakeup(effect: &RegistryEffect) -> Result<(), TimerError> {
    let RegistryEffect::ArmWakeup {
        token, delay_ns, ..
    } = effect
    else {
        return Err(TimerError::OwnershipInvariant);
    };
    let task_token = token.clone();
    let handle = platform::set_timer(Duration::from_nanos(*delay_ns), async move {
        dispatch_wakeup(task_token).await;
    });
    if let Err((error, handle)) = install_provider_handle(token, handle) {
        platform::clear_timer(handle);
        return Err(error);
    }
    if let Err(error) = confirm_effect(effect) {
        let handle = with_registry_mut(|registry| {
            registry
                .take_wakeup_handle(token.identity())
                .ok_or(TimerError::OwnershipInvariant)
        })?;
        clear_provider_handle(handle);
        return Err(error);
    }
    Ok(())
}

fn dispatch_watchdog_effect(effect: &RegistryEffect) -> Result<(), TimerError> {
    let RegistryEffect::DispatchWatchdog {
        successor,
        successor_delay_ns,
        work,
        ..
    } = effect
    else {
        return Err(TimerError::OwnershipInvariant);
    };
    let successor_token = successor.clone();
    let successor_handle =
        platform::set_timer(Duration::from_nanos(*successor_delay_ns), async move {
            dispatch_watchdog_scheduler(&successor_token);
        });
    if let Err((error, handle)) = install_provider_handle(successor, successor_handle) {
        platform::clear_timer(handle);
        return Err(error);
    }

    let work_token = work.clone();
    let work_handle = platform::set_timer(Duration::ZERO, async move {
        dispatch_watchdog_work(&work_token);
    });
    if let Err((error, handle)) = install_provider_handle(work, work_handle) {
        platform::clear_timer(handle);
        clear_entry_provider_handles(successor.identity())?;
        return Err(error);
    }
    if let Err(error) = confirm_effect(effect) {
        clear_entry_provider_handles(successor.identity())?;
        return Err(error);
    }
    Ok(())
}

fn install_provider_handle(
    token: &CallbackToken,
    handle: TimerHandle,
) -> Result<(), (TimerError, TimerHandle)> {
    #[cfg(test)]
    if take_provider_install_fault() {
        return Err((TimerError::OwnershipInvariant, handle));
    }
    RUNTIME.with(|runtime| {
        let Ok(mut runtime) = runtime.try_borrow_mut() else {
            return Err((TimerError::RuntimeBusy, handle));
        };
        let Some(registry) = runtime.as_mut() else {
            return Err((TimerError::NotInitialized, handle));
        };
        match registry.install_provider_handle(token, handle) {
            Ok(()) => Ok(()),
            Err((error, handle)) => Err((TimerError::from(error), handle)),
        }
    })
}

fn confirm_effect(effect: &RegistryEffect) -> Result<(), TimerError> {
    #[cfg(test)]
    if take_provider_confirmation_fault() {
        return Err(TimerError::OwnershipInvariant);
    }
    with_registry_mut(|registry| {
        registry
            .confirm_effect_applied(effect)
            .map_err(TimerError::from)
    })
}

fn restore_provider_handles(mut handles: ProviderHandles) -> Result<(), TimerError> {
    // Every detached linear capability must be restored or cleared even when
    // restoring an earlier handle fails.
    let wakeup_failure = handles
        .take_wakeup()
        .and_then(|handle| restore_provider_handle(handle).err());
    let work_failure = handles
        .take_work()
        .and_then(|handle| restore_provider_handle(handle).err());
    wakeup_failure.or(work_failure).map_or(Ok(()), Err)
}

fn restore_provider_handle(handle: ProviderHandle) -> Result<(), TimerError> {
    let (token, handle) = handle.into_parts();
    match install_provider_handle(&token, handle) {
        Ok(()) => Ok(()),
        Err((error, handle)) => {
            platform::clear_timer(handle);
            Err(error)
        }
    }
}

fn clear_provider_handle(handle: ProviderHandle) {
    let (_, handle) = handle.into_parts();
    platform::clear_timer(handle);
}

fn clear_provider_handles(mut handles: ProviderHandles) {
    if let Some(wakeup) = handles.take_wakeup() {
        clear_provider_handle(wakeup);
    }
    if let Some(work) = handles.take_work() {
        clear_provider_handle(work);
    }
}

fn clear_entry_provider_handles(identity: &TimerIdentity) -> Result<(), TimerError> {
    let handles = with_registry_mut(|registry| {
        Ok(ProviderHandles::from_parts(
            registry.take_wakeup_handle(identity),
            registry.take_work_handle(identity),
        ))
    })?;
    clear_provider_handles(handles);
    Ok(())
}

#[allow(clippy::future_not_send)] // IC callbacks and canister-local state are single-threaded.
async fn dispatch_wakeup(token: CallbackToken) {
    match token.role() {
        CallbackRole::OrdinaryWork => dispatch_ordinary(token).await,
        CallbackRole::WatchdogScheduler => dispatch_watchdog_scheduler(&token),
        CallbackRole::WatchdogWork => {}
    }
}

#[allow(clippy::future_not_send)] // IC callbacks and canister-local state are single-threaded.
async fn dispatch_ordinary(token: CallbackToken) {
    let measurement = CallbackMeasurementStart::capture();
    let accepted = with_registry_mut(|registry| {
        registry.consume_provider_handle(&token);
        Ok(registry.begin_ordinary(&token))
    });
    match accepted {
        Ok(CallbackAcceptance::Accepted) => {}
        Ok(CallbackAcceptance::Stale) => return,
        Err(error) => trap_callback_failure("ordinary callback acceptance", &error),
    }

    let callback = match with_registry(|registry| {
        registry.ordinary_callback(&token).map_err(TimerError::from)
    }) {
        Ok(callback) => callback,
        Err(TimerError::OwnershipInvariant) => {
            fail_ordinary_dispatch(&token);
            return;
        }
        Err(error) => trap_callback_failure("ordinary callback lookup", &error),
    };
    let future = {
        let Ok(mut callback) = callback.try_borrow_mut() else {
            fail_ordinary_dispatch(&token);
            return;
        };
        callback(token.clone())
    };
    let result = future.await;
    let transition = with_registry_mut(|registry| {
        registry
            .complete_ordinary(&token, platform::time_ns(), result)
            .map_err(TimerError::from)
    });
    let transition = transition
        .unwrap_or_else(|error| trap_callback_failure("ordinary callback completion", &error));
    finish_callback_transition(&token, transition, ProviderHandles::default());
    record_callback_measurements(&token, measurement.finish());
}

fn fail_ordinary_dispatch(token: &CallbackToken) {
    let transition = with_registry_mut(|registry| {
        registry
            .complete_ordinary(
                token,
                platform::time_ns(),
                TimerRunResult::new(TimerCompletion::invariant_failure(0), TimerDirective::Stop),
            )
            .map_err(TimerError::from)
    });
    let transition = transition.unwrap_or_else(|error| {
        trap_callback_failure("ordinary invariant-failure completion", &error)
    });
    finish_callback_transition(token, transition, ProviderHandles::default());
}

fn dispatch_watchdog_scheduler(token: &CallbackToken) {
    let measurement = CallbackMeasurementStart::capture();
    let transition = with_registry_mut(|registry| {
        registry.consume_provider_handle(token);
        Ok(registry.begin_watchdog_scheduler(token, platform::time_ns()))
    });
    let transition = transition
        .unwrap_or_else(|error| trap_callback_failure("watchdog scheduler transition", &error));
    let accepted = !matches!(transition.effect(), RegistryEffect::None);
    finish_callback_transition(token, transition, ProviderHandles::default());
    if accepted {
        record_callback_measurements(token, measurement.finish());
    }
}

fn dispatch_watchdog_work(token: &CallbackToken) {
    let measurement = CallbackMeasurementStart::capture();
    let accepted = with_registry_mut(|registry| {
        registry.consume_provider_handle(token);
        Ok(registry.begin_watchdog_work(token))
    });
    match accepted {
        Ok(CallbackAcceptance::Accepted) => {}
        Ok(CallbackAcceptance::Stale) => return,
        Err(error) => trap_callback_failure("watchdog work acceptance", &error),
    }

    let callback =
        match with_registry(|registry| registry.watchdog_callback(token).map_err(TimerError::from))
        {
            Ok(callback) => callback,
            Err(error) => trap_callback_failure("watchdog callback lookup", &error),
        };
    let result = {
        let Ok(mut callback) = callback.try_borrow_mut() else {
            trap_callback_failure(
                "watchdog callback ownership",
                &TimerError::OwnershipInvariant,
            );
        };
        callback(token.clone())
    };
    finish_watchdog_dispatch(token, result);
    record_callback_measurements(token, measurement.finish());
}

fn finish_watchdog_dispatch(token: &CallbackToken, result: WatchdogRunResult) {
    let claim = RegistrationClaim::from_callback(token);
    // Unlike synchronous public control, an unexpected callback-completion
    // failure must trap. IC message rollback restores these temporarily
    // detached heap capabilities while the previously committed successor
    // remains armed by the scheduler message.
    let completed = with_registry_mut(|registry| {
        let handles = registry
            .take_provider_handles_for_claim(&claim)
            .map_err(TimerError::from)?;
        #[cfg(test)]
        {
            if take_watchdog_completion_fault() {
                return Err(TimerError::OwnershipInvariant);
            }
        }
        let transition = registry
            .complete_watchdog_work(token, platform::time_ns(), result)
            .map_err(TimerError::from)?;
        Ok((transition, handles))
    });
    let (transition, handles) =
        completed.unwrap_or_else(|error| trap_callback_failure("watchdog work completion", &error));
    finish_callback_transition(token, transition, handles);
}

fn finish_callback_transition(
    token: &CallbackToken,
    transition: RegistryTransition,
    handles: ProviderHandles,
) {
    match finish_transition(transition, handles) {
        Ok(()) | Err(TimerError::ControlFailure(_)) => {}
        Err(
            error @ (TimerError::NotInitialized
            | TimerError::RuntimeBusy
            | TimerError::Register(_)
            | TimerError::Schedule(_)
            | TimerError::RegistrationExpired
            | TimerError::OwnershipInvariant
            | TimerError::ReconciliationConflict),
        ) => {
            if token.role() == CallbackRole::WatchdogWork {
                trap_callback_failure("watchdog provider-handle completion", &error);
            }
            fail_provider_binding(token).unwrap_or_else(|binding_error| {
                trap_callback_failure("provider-binding failure cleanup", &binding_error)
            });
        }
    }
}

fn fail_provider_binding(token: &CallbackToken) -> Result<(), TimerError> {
    let claim = RegistrationClaim::from_callback(token);
    fail_claim_provider_binding(&claim)
}

fn fail_claim_provider_binding(claim: &RegistrationClaim) -> Result<(), TimerError> {
    let failed = with_registry_mut(|registry| {
        registry
            .fail_registration(claim, TimerControlFailure::ProviderBindingFailed)
            .map_err(TimerError::from)
    });
    clear_provider_handles(failed?);
    Ok(())
}

#[derive(Clone, Copy)]
struct CallbackMeasurementStart {
    instructions_before: u64,
    memory_start: platform::MemoryPages,
}

impl CallbackMeasurementStart {
    fn capture() -> Self {
        // Keep page observation outside the established instruction interval.
        let memory_start = platform::memory_pages();
        let instructions_before = platform::instruction_counter();
        Self {
            instructions_before,
            memory_start,
        }
    }

    fn finish(self) -> CallbackMeasurement {
        // Close the instruction interval before taking its paired end extent.
        let instructions = platform::instruction_counter().saturating_sub(self.instructions_before);
        let memory_end = platform::memory_pages();
        CallbackMeasurement {
            instructions,
            memory_start: self.memory_start,
            memory_end,
        }
    }
}

#[derive(Clone, Copy)]
struct CallbackMeasurement {
    instructions: u64,
    memory_start: platform::MemoryPages,
    memory_end: platform::MemoryPages,
}

fn record_callback_measurements(token: &CallbackToken, measurement: CallbackMeasurement) {
    with_registry_mut(|registry| {
        registry
            .record_callback_measurements(
                token,
                measurement.instructions,
                measurement.memory_start,
                measurement.memory_end,
            )
            .map_err(TimerError::from)
    })
    .unwrap_or_else(|error| trap_callback_failure("callback measurement accounting", &error));
}

fn trap_callback_failure(context: &str, error: &TimerError) -> ! {
    platform::trap(&format!("ic-timers {context} failed: {error}"))
}

fn with_registry<T>(
    operation: impl FnOnce(&TimerRegistry) -> Result<T, TimerError>,
) -> Result<T, TimerError> {
    RUNTIME.with(|runtime| {
        let runtime = runtime.try_borrow().map_err(|_| TimerError::RuntimeBusy)?;
        let registry = runtime.as_ref().ok_or(TimerError::NotInitialized)?;
        operation(registry)
    })
}

fn with_registry_mut<T>(
    operation: impl FnOnce(&mut TimerRegistry) -> Result<T, TimerError>,
) -> Result<T, TimerError> {
    RUNTIME.with(|runtime| {
        let mut runtime = runtime
            .try_borrow_mut()
            .map_err(|_| TimerError::RuntimeBusy)?;
        let registry = runtime.as_mut().ok_or(TimerError::NotInitialized)?;
        operation(registry)
    })
}

#[cfg(test)]
fn reset_for_test(now_ns: u64, canister_version: u64) {
    platform::reset(now_ns, canister_version);
    WATCHDOG_COMPLETION_FAULT.with(|fault| fault.set(false));
    PROVIDER_INSTALL_FAULT_AFTER.with(|fault| fault.set(None));
    PROVIDER_CONFIRMATION_FAULT.with(|fault| fault.set(false));
    RUNTIME.with(|runtime| {
        *runtime.borrow_mut() = None;
    });
}

#[cfg(test)]
thread_local! {
    static WATCHDOG_COMPLETION_FAULT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
    static PROVIDER_INSTALL_FAULT_AFTER: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) };
    static PROVIDER_CONFIRMATION_FAULT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

#[cfg(test)]
fn inject_watchdog_completion_fault() {
    WATCHDOG_COMPLETION_FAULT.with(|fault| fault.set(true));
}

#[cfg(test)]
fn take_watchdog_completion_fault() -> bool {
    WATCHDOG_COMPLETION_FAULT.with(|fault| fault.replace(false))
}

#[cfg(test)]
fn inject_provider_install_fault() {
    inject_provider_install_fault_after(0);
}

#[cfg(test)]
fn inject_provider_install_fault_after(successful_installs: u64) {
    PROVIDER_INSTALL_FAULT_AFTER.with(|fault| fault.set(Some(successful_installs)));
}

#[cfg(test)]
fn take_provider_install_fault() -> bool {
    PROVIDER_INSTALL_FAULT_AFTER.with(|fault| match fault.get() {
        Some(0) => {
            fault.set(None);
            true
        }
        Some(remaining) => {
            fault.set(Some(remaining - 1));
            false
        }
        None => false,
    })
}

#[cfg(test)]
fn inject_provider_confirmation_fault() {
    PROVIDER_CONFIRMATION_FAULT.with(|fault| fault.set(true));
}

#[cfg(test)]
fn take_provider_confirmation_fault() -> bool {
    PROVIDER_CONFIRMATION_FAULT.with(|fault| fault.replace(false))
}

#[cfg(test)]
mod tests;