hitbox 0.2.4

Asynchronous caching framework.
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
//! FSM state types and resolved state structs.
//!
//! Each state struct represents resolved async data and has a `.transition()` method
//! that returns the appropriate transition enum. The transition enum then has
//! `.into_state()` to convert to the outer `State` enum.
//!
//! Flow: poll future → create state struct → `.transition()` → `.into_state()`

use std::fmt::{self, Debug};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Instant;

use futures::future::BoxFuture;
use futures::ready;
use hitbox_backend::BackendError;
use hitbox_core::{
    BoxContext, CachePolicy, CacheValue, Cacheable, CacheablePolicyData, EntityPolicyConfig,
    Predicate, ReadMode, RequestCachePolicy, ResponseCachePolicy, Upstream,
};
use pin_project::pin_project;
use tokio::sync::OwnedSemaphorePermit;
use tracing::{Instrument, Level, Span, debug, field, instrument::Instrumented, span, warn};

use crate::backend::CacheBackend;
use crate::concurrency::{ConcurrencyDecision, ConcurrencyError, ConcurrencyManager};
use crate::fsm::transitions::{
    AwaitResponseTransition, CheckRequestCachePolicyTransition, CheckResponseCachePolicyTransition,
    ConvertResponseTransition, HandleStaleTransition, InitialTransition, PollCacheTransition,
    PollUpstreamTransition, UpdateCacheTransition,
};
use crate::policy::{EnabledCacheConfig, PolicyConfig, StalePolicy};
use crate::{CacheKey, CacheState, CacheStatus, CacheableRequest, CacheableResponse, Extractor};

// =============================================================================
// Helper Types
// =============================================================================

/// Wrapper for `Option<&CacheKey>` that implements `Display` without allocation.
struct OptionalKey<'a>(Option<&'a CacheKey>);

impl fmt::Display for OptionalKey<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Some(key) => fmt::Display::fmt(key, f),
            None => Ok(()),
        }
    }
}

// =============================================================================
// Type Aliases
// =============================================================================

pub type CacheResult<T> = Result<Option<CacheValue<T>>, BackendError>;
/// Future that polls the cache and returns (result, context)
pub type PollCacheFuture<T> = BoxFuture<'static, (CacheResult<T>, BoxContext)>;
/// Future that updates the cache and returns (backend_result, response, context)
pub type UpdateCacheFuture<T> = BoxFuture<'static, (Result<(), BackendError>, T, BoxContext)>;
pub type AwaitResponseFuture<T> = BoxFuture<'static, Result<T, ConcurrencyError>>;
/// Future that checks request cache policy
pub type RequestCachePolicyFuture<T> = BoxFuture<'static, RequestCachePolicy<T>>;

// =============================================================================
// ConvertResponseFuture - Zero-cost wrapper using GAT
// =============================================================================

/// Future that converts cached value to response using the GAT `FromCachedFuture`.
///
/// This wrapper avoids boxing by directly using the response type's `FromCachedFuture`.
/// For types where `FromCachedFuture = Ready<Self>` (like `CacheableHttpResponse`),
/// this provides zero-cost cache hits with no allocation.
#[pin_project]
pub struct ConvertResponseFuture<Res: CacheableResponse> {
    #[pin]
    inner: Res::FromCachedFuture,
    ctx: Option<BoxContext>,
}

impl<Res: CacheableResponse> ConvertResponseFuture<Res> {
    /// Create a new ConvertResponseFuture from a cached value and context.
    pub fn new(cached: Res::Cached, ctx: BoxContext) -> Self {
        Self {
            inner: Res::from_cached(cached),
            ctx: Some(ctx),
        }
    }
}

impl<Res: CacheableResponse> std::future::Future for ConvertResponseFuture<Res> {
    type Output = (Res, BoxContext);

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        let response = ready!(this.inner.poll(cx));
        Poll::Ready((response, this.ctx.take().expect("polled after completion")))
    }
}

// =============================================================================
// State Enum
// =============================================================================

#[allow(missing_docs)]
#[pin_project(project = StateProj)]
pub enum State<Res, Req, U, ReqP, E>
where
    Res: CacheableResponse,
    Req: CacheableRequest,
    U: Upstream<Req, Response = Res>,
    ReqP: Predicate<Subject = Req>,
    E: Extractor<Subject = Req>,
{
    /// Initial state - all data needed for the first transition
    Initial(Option<Initial<Req, ReqP, E, U>>),
    /// Checking if request should be cached
    CheckRequestCachePolicy {
        #[pin]
        cache_policy_future: RequestCachePolicyFuture<Req>,
        state: Option<CheckRequestCachePolicy<U>>,
    },
    /// Polling the cache backend - context is captured in the future
    PollCache {
        #[pin]
        poll_cache: PollCacheFuture<Res::Cached>,
        state: Option<PollCache<Req, U>>,
    },
    /// Converting cached value to response (cache hit, no refill needed)
    ConvertResponse {
        #[pin]
        response_future: ConvertResponseFuture<Res>,
        state: Option<ConvertResponse>,
    },
    /// Handling stale cache hit - convert to response then apply stale policy
    HandleStale {
        #[pin]
        response_future: ConvertResponseFuture<Res>,
        state: Option<HandleStale<Req, U>>,
    },
    /// Awaiting response from another concurrent request
    AwaitResponse {
        #[pin]
        await_response_future: AwaitResponseFuture<Res>,
        state: Option<AwaitResponse<Req, U>>,
    },
    /// Polling upstream service
    PollUpstream {
        #[pin]
        upstream_future: Instrumented<U::Future>,
        state: Option<PollUpstream>,
    },
    /// Checking if response should be cached
    CheckResponseCachePolicy {
        #[pin]
        cache_policy: BoxFuture<'static, ResponseCachePolicy<Res>>,
        state: Option<CheckResponseCachePolicy>,
    },
    /// Updating cache with response - context is captured in the future
    UpdateCache {
        #[pin]
        update_cache_future: UpdateCacheFuture<Res>,
        state: Option<UpdateCache>,
    },
    /// Final state with response
    Response(Option<Response<Res>>),
}

impl<Res, Req, U, ReqP, E> Debug for State<Res, Req, U, ReqP, E>
where
    Res: CacheableResponse,
    Req: CacheableRequest,
    U: Upstream<Req, Response = Res>,
    ReqP: Predicate<Subject = Req>,
    E: Extractor<Subject = Req>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            State::Initial(_) => f.write_str("State::Initial"),
            State::CheckRequestCachePolicy { .. } => f.write_str("State::CheckRequestCachePolicy"),
            State::PollCache { .. } => f.write_str("State::PollCache"),
            State::ConvertResponse { .. } => f.write_str("State::ConvertResponse"),
            State::HandleStale { .. } => f.write_str("State::HandleStale"),
            State::AwaitResponse { .. } => f.write_str("State::AwaitResponse"),
            State::CheckResponseCachePolicy { .. } => {
                f.write_str("State::CheckResponseCachePolicy")
            }
            State::PollUpstream { .. } => f.write_str("State::PollUpstream"),
            State::UpdateCache { .. } => f.write_str("State::UpdateCache"),
            State::Response(_) => f.write_str("State::Response"),
        }
    }
}

// =============================================================================
// Initial
// =============================================================================

/// Data gathered from Initial state (synchronous).
pub struct Initial<Req, ReqP, E, U> {
    pub request: Req,
    pub predicates: ReqP,
    pub extractors: E,
    pub ctx: BoxContext,
    pub upstream: U,
    /// Tracing span for this state.
    pub span: Span,
}

impl<Req, ReqP, E, U> Initial<Req, ReqP, E, U> {
    /// Create a new Initial state with its tracing span.
    pub fn new(
        request: Req,
        predicates: ReqP,
        extractors: E,
        ctx: BoxContext,
        upstream: U,
        parent: &Span,
    ) -> Self {
        Self {
            request,
            predicates,
            extractors,
            ctx,
            upstream,
            span: span!(parent: parent, Level::TRACE, "fsm.Initial"),
        }
    }
}

impl<Req, ReqP, E, U> Initial<Req, ReqP, E, U>
where
    Req: CacheableRequest + Send + 'static,
    ReqP: Predicate<Subject = Req> + Send + Sync + 'static,
    E: Extractor<Subject = Req> + Send + Sync + 'static,
    U: Upstream<Req>,
{
    /// Transition from Initial state.
    ///
    /// Based on policy configuration:
    /// - Enabled: create CheckRequestCachePolicy future
    /// - Disabled: call upstream directly
    pub fn transition(mut self, policy: &PolicyConfig) -> InitialTransition<Req, U> {
        match policy {
            PolicyConfig::Enabled(_) => {
                // Box the RPITIT future for storage in FSM state
                let cache_policy_future =
                    Box::pin(self.request.cache_policy(self.predicates, self.extractors));
                InitialTransition::CheckRequestCachePolicy {
                    cache_policy_future,
                    ctx: self.ctx,
                    upstream: self.upstream,
                }
            }
            PolicyConfig::Disabled => {
                let upstream_future = self.upstream.call(self.request);
                InitialTransition::PollUpstream {
                    upstream_future,
                    ctx: self.ctx,
                }
            }
        }
    }
}

impl<Req, ReqP, E, U> std::fmt::Debug for Initial<Req, ReqP, E, U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Initial").finish_non_exhaustive()
    }
}

// =============================================================================
// Response
// =============================================================================

/// Terminal state with final response.
pub struct Response<Res> {
    pub response: Res,
    pub ctx: BoxContext,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl<Res> Response<Res> {
    /// Create a new Response state with its tracing span.
    ///
    /// The `status` and `source` fields will be recorded when the response is finalized.
    pub fn new(response: Res, ctx: BoxContext, parent: &Span) -> Self {
        Self {
            response,
            ctx,
            span: span!(
                parent: parent,
                Level::TRACE,
                "fsm.Response",
                cache.status = field::Empty,
                cache.source = field::Empty
            ),
        }
    }
}

impl<Res> std::fmt::Debug for Response<Res> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Response").finish_non_exhaustive()
    }
}

// =============================================================================
// PollUpstream
// =============================================================================

/// Data for PollUpstream state (non-pinned part).
///
/// The upstream future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct PollUpstream {
    pub permit: Option<OwnedSemaphorePermit>,
    pub ctx: BoxContext,
    pub cache_key: Option<CacheKey>,
    /// Start time for measuring upstream call duration.
    pub upstream_start: Instant,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl PollUpstream {
    /// Create a new PollUpstream state with its tracing span, instrumenting the provided future.
    ///
    /// Returns both the state and the instrumented future, since the future needs to be
    /// instrumented with the same span that's stored in the state.
    pub fn with_future<F: Sized>(
        permit: Option<OwnedSemaphorePermit>,
        ctx: BoxContext,
        cache_key: Option<CacheKey>,
        future: F,
        parent: &Span,
    ) -> (Self, Instrumented<F>) {
        let has_permit = permit.is_some();
        let span = span!(
            parent: parent,
            Level::TRACE,
            "fsm.PollUpstream",
            cache.key = %OptionalKey(cache_key.as_ref()),
            concurrency.permit = has_permit
        );
        (
            Self {
                permit,
                ctx,
                cache_key,
                upstream_start: Instant::now(),
                span: span.clone(),
            },
            future.instrument(span),
        )
    }

    /// Transition from PollUpstream state after future completes.
    ///
    /// This merges the old PollUpstream → UpstreamPolled → next state transitions
    /// into a single step, since UpstreamPolled was a synchronous state.
    pub fn transition<Res, ResP>(
        self,
        upstream_result: Res,
        predicates: ResP,
        policy: &PolicyConfig,
    ) -> PollUpstreamTransition<Res>
    where
        Res: CacheableResponse + Send + 'static,
        ResP: Predicate<Subject = Res::Subject> + Send + Sync + 'static,
    {
        // Record upstream duration metric
        crate::metrics::record_upstream_duration(self.upstream_start.elapsed());

        match self.cache_key {
            Some(cache_key) => {
                let entity_config = match policy {
                    PolicyConfig::Enabled(config) => EntityPolicyConfig {
                        ttl: config.ttl,
                        stale_ttl: config.stale,
                    },
                    PolicyConfig::Disabled => EntityPolicyConfig::default(),
                };
                PollUpstreamTransition::CheckResponseCachePolicy {
                    cache_policy_future: Box::pin(async move {
                        upstream_result
                            .cache_policy(predicates, &entity_config)
                            .await
                    }),
                    permit: self.permit,
                    ctx: self.ctx,
                    cache_key,
                }
            }
            None => PollUpstreamTransition::Response(Response {
                response: upstream_result,
                ctx: self.ctx,
                span: Span::none(),
            }),
        }
    }
}

impl std::fmt::Debug for PollUpstream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PollUpstream")
            .field("has_permit", &self.permit.is_some())
            .field("cache_key", &self.cache_key)
            .finish_non_exhaustive()
    }
}

// =============================================================================
// CheckResponseCachePolicy
// =============================================================================

/// Data for CheckResponseCachePolicy state (non-pinned part).
///
/// The cache policy future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct CheckResponseCachePolicy {
    pub permit: Option<OwnedSemaphorePermit>,
    pub ctx: BoxContext,
    pub cache_key: CacheKey,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl CheckResponseCachePolicy {
    /// Create a new CheckResponseCachePolicy state with its tracing span.
    ///
    /// The `cacheable` field will be recorded after the policy check completes.
    pub fn new(
        permit: Option<OwnedSemaphorePermit>,
        ctx: BoxContext,
        cache_key: CacheKey,
        parent: &Span,
    ) -> Self {
        let has_permit = permit.is_some();
        Self {
            permit,
            ctx,
            cache_key: cache_key.clone(),
            span: span!(
                parent: parent,
                Level::TRACE,
                "fsm.CheckResponseCachePolicy",
                cache.key = %cache_key,
                concurrency.permit = has_permit,
                cache.cacheable = field::Empty
            ),
        }
    }

    /// Transition from CheckResponseCachePolicy state after future completes.
    pub fn transition<Res, B, C>(
        self,
        policy: CachePolicy<CacheValue<Res::Cached>, Res>,
        backend: Arc<B>,
        concurrency_manager: &C,
    ) -> CheckResponseCachePolicyTransition<Res>
    where
        Res: CacheableResponse + Send + 'static,
        Res::Cached: Cacheable + Send,
        B: CacheBackend + Send + Sync + 'static,
        C: ConcurrencyManager<Res>,
    {
        // Record cacheable decision to span
        self.span.record(
            "cache.cacheable",
            matches!(&policy, CachePolicy::Cacheable(_)),
        );

        match policy {
            CachePolicy::Cacheable(cache_value) => {
                if self.permit.is_some() {
                    concurrency_manager.resolve(&self.cache_key, &cache_value);
                }
                let cache_key = self.cache_key;
                let mut ctx = self.ctx;
                let update_cache_future = Box::pin(async move {
                    let update_cache_result =
                        backend.set::<Res>(&cache_key, &cache_value, &mut ctx).await;
                    let upstream_result = Res::from_cached(cache_value.into_inner()).await;
                    (update_cache_result, upstream_result, ctx)
                });
                CheckResponseCachePolicyTransition::UpdateCache {
                    update_cache_future,
                }
            }
            CachePolicy::NonCacheable(response) => {
                if self.permit.is_some() {
                    concurrency_manager.cleanup(&self.cache_key);
                }
                CheckResponseCachePolicyTransition::Response(Response {
                    response,
                    ctx: self.ctx,
                    span: Span::none(),
                })
            }
        }
    }
}

impl std::fmt::Debug for CheckResponseCachePolicy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CheckResponseCachePolicy")
            .field("has_permit", &self.permit.is_some())
            .field("cache_key", &self.cache_key)
            .finish_non_exhaustive()
    }
}

// =============================================================================
// CheckRequestCachePolicy
// =============================================================================

/// Data for CheckRequestCachePolicy state (non-pinned part).
///
/// The cache policy future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct CheckRequestCachePolicy<U> {
    pub ctx: BoxContext,
    pub upstream: U,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl<U> CheckRequestCachePolicy<U> {
    /// Create a new CheckRequestCachePolicy state with its tracing span.
    ///
    /// The `cacheable` field will be recorded after the policy check completes.
    pub fn new(ctx: BoxContext, upstream: U, parent: &Span) -> Self {
        Self {
            ctx,
            upstream,
            span: span!(parent: parent, Level::TRACE, "fsm.CheckRequestCachePolicy", cache.cacheable = field::Empty),
        }
    }

    /// Transition from CheckRequestCachePolicy state after future completes.
    pub fn transition<Req, Res, B>(
        mut self,
        policy: RequestCachePolicy<Req>,
        backend: Arc<B>,
        cache_key_storage: &mut Option<CacheKey>,
    ) -> CheckRequestCachePolicyTransition<Req, Res, U>
    where
        Req: CacheableRequest,
        Res: CacheableResponse,
        Res::Cached: Cacheable + Send,
        U: Upstream<Req, Response = Res>,
        B: CacheBackend + Send + Sync + 'static,
    {
        // Record cacheable decision to span
        self.span.record(
            "cache.cacheable",
            matches!(&policy, CachePolicy::Cacheable(_)),
        );
        match policy {
            CachePolicy::Cacheable(CacheablePolicyData { key, request }) => {
                let cache_key_for_get = key.clone();
                debug!(?key, "FSM looking up cache key");
                let _ = cache_key_storage.insert(key.clone());
                let mut ctx = self.ctx;
                let poll_cache = Box::pin(async move {
                    let result = backend.get::<Res>(&cache_key_for_get, &mut ctx).await;
                    debug!(
                        found = result.as_ref().map(|r| r.is_some()).unwrap_or(false),
                        "FSM cache lookup result"
                    );
                    (result, ctx)
                });
                CheckRequestCachePolicyTransition::PollCache {
                    poll_cache,
                    request,
                    cache_key: key,
                    upstream: self.upstream,
                }
            }
            CachePolicy::NonCacheable(request) => {
                let upstream_future = self.upstream.call(request);
                CheckRequestCachePolicyTransition::PollUpstream {
                    upstream_future,
                    ctx: self.ctx,
                }
            }
        }
    }
}

impl<U> std::fmt::Debug for CheckRequestCachePolicy<U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CheckRequestCachePolicy")
            .finish_non_exhaustive()
    }
}

// =============================================================================
// PollCache
// =============================================================================

/// Data for PollCache state (non-pinned part).
///
/// The poll cache future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct PollCache<Req, U> {
    pub request: Req,
    pub cache_key: CacheKey,
    pub upstream: U,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl<Req, U> PollCache<Req, U> {
    /// Create a new PollCache state with its tracing span.
    pub fn new(request: Req, cache_key: CacheKey, upstream: U, parent: &Span) -> Self {
        Self {
            request,
            cache_key: cache_key.clone(),
            upstream,
            span: span!(parent: parent, Level::TRACE, "fsm.PollCache", cache.key = %cache_key, concurrency.decision = field::Empty),
        }
    }

    /// Transition from PollCache state after future completes.
    ///
    /// On cache miss or expired, checks concurrency policy and transitions directly
    /// to either `ConcurrentPollUpstream` or `PollUpstream`.
    pub fn transition<Res, B, C>(
        self,
        cache_result: CacheResult<Res::Cached>,
        mut ctx: BoxContext,
        backend: Arc<B>,
        policy: &PolicyConfig,
        concurrency_manager: &C,
    ) -> PollCacheTransition<Res, Req, U>
    where
        Res: CacheableResponse + Send + 'static,
        Res::Cached: Cacheable + Send,
        B: CacheBackend + Send + Sync + 'static,
        U: Upstream<Req, Response = Res>,
        C: ConcurrencyManager<Res>,
    {
        let cached = cache_result
            .inspect_err(|err| warn!("Cache error: {err:?}"))
            .unwrap_or_default();

        match cached {
            Some(cached_value) => {
                let cache_state = cached_value.cache_state();
                ctx.set_status(CacheStatus::Hit);

                match cache_state {
                    CacheState::Actual(value) => {
                        if ctx.read_mode() == ReadMode::Refill {
                            let cache_key = self.cache_key;
                            let update_cache_future = Box::pin(async move {
                                let update_result =
                                    backend.set::<Res>(&cache_key, &value, &mut ctx).await;
                                let response = Res::from_cached(value.into_inner()).await;
                                (update_result, response, ctx)
                            });
                            PollCacheTransition::UpdateCache {
                                update_cache_future,
                            }
                        } else {
                            let cache_key = self.cache_key;
                            // Zero-cost conversion using GAT - no boxing!
                            let response_future =
                                ConvertResponseFuture::new(value.into_inner(), ctx);
                            PollCacheTransition::ConvertResponse {
                                response_future,
                                cache_key,
                            }
                        }
                    }
                    CacheState::Stale(value) => {
                        let cache_key = self.cache_key;
                        let request = self.request;
                        let upstream = self.upstream;
                        // Zero-cost conversion using GAT - no boxing!
                        let response_future = ConvertResponseFuture::new(value.into_inner(), ctx);
                        PollCacheTransition::HandleStale {
                            response_future,
                            request,
                            cache_key,
                            upstream,
                        }
                    }
                    CacheState::Expired(_value) => {
                        ctx.set_status(CacheStatus::Miss);
                        self.transition_to_upstream(ctx, policy, concurrency_manager)
                    }
                }
            }
            None => self.transition_to_upstream(ctx, policy, concurrency_manager),
        }
    }

    /// Helper to transition to upstream based on concurrency policy.
    fn transition_to_upstream<Res, C>(
        mut self,
        ctx: BoxContext,
        policy: &PolicyConfig,
        concurrency_manager: &C,
    ) -> PollCacheTransition<Res, Req, U>
    where
        Res: CacheableResponse,
        U: Upstream<Req, Response = Res>,
        C: ConcurrencyManager<Res>,
    {
        match policy {
            PolicyConfig::Enabled(EnabledCacheConfig {
                concurrency: Some(concurrency),
                ..
            }) => match concurrency_manager.check(&self.cache_key, *concurrency) {
                ConcurrencyDecision::Proceed(permit) => {
                    self.span.record("concurrency.decision", "proceed");
                    let upstream_future = self.upstream.call(self.request);
                    PollCacheTransition::PollUpstream {
                        upstream_future,
                        permit: Some(permit),
                        ctx,
                        cache_key: self.cache_key,
                    }
                }
                ConcurrencyDecision::ProceedWithoutPermit => {
                    self.span
                        .record("concurrency.decision", "proceed_without_permit");
                    let upstream_future = self.upstream.call(self.request);
                    PollCacheTransition::PollUpstream {
                        upstream_future,
                        permit: None,
                        ctx,
                        cache_key: self.cache_key,
                    }
                }
                ConcurrencyDecision::Await(await_future) => {
                    self.span.record("concurrency.decision", "await");
                    PollCacheTransition::AwaitResponse {
                        await_response_future: await_future,
                        request: self.request,
                        ctx,
                        cache_key: self.cache_key,
                        upstream: self.upstream,
                    }
                }
            },
            _ => {
                self.span.record("concurrency.decision", "disabled");
                let upstream_future = self.upstream.call(self.request);
                PollCacheTransition::PollUpstream {
                    upstream_future,
                    permit: None,
                    ctx,
                    cache_key: self.cache_key,
                }
            }
        }
    }
}

impl<Req, U> std::fmt::Debug for PollCache<Req, U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PollCache")
            .field("cache_key", &self.cache_key)
            .finish_non_exhaustive()
    }
}

// =============================================================================
// ConvertResponse
// =============================================================================

/// Data for ConvertResponse state (non-pinned part).
///
/// The response future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct ConvertResponse {
    /// Cache key for logging/tracing purposes.
    pub cache_key: CacheKey,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl ConvertResponse {
    /// Create a new ConvertResponse state with its tracing span.
    pub fn new(cache_key: CacheKey, parent: &Span) -> Self {
        Self {
            cache_key: cache_key.clone(),
            span: span!(parent: parent, Level::TRACE, "fsm.ConvertResponse", cache.key = %cache_key),
        }
    }

    /// Transition from ConvertResponse state after future completes.
    pub fn transition<Res>(self, response: Res, ctx: BoxContext) -> ConvertResponseTransition<Res> {
        debug!(cache.key = %self.cache_key, "ConvertResponse transition");
        ConvertResponseTransition::Response(Response {
            response,
            ctx,
            span: Span::none(),
        })
    }
}

impl std::fmt::Debug for ConvertResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConvertResponse")
            .field("cache_key", &self.cache_key)
            .finish_non_exhaustive()
    }
}

// =============================================================================
// HandleStale
// =============================================================================

/// Data for HandleStale state (non-pinned part).
///
/// The response future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct HandleStale<Req, U> {
    pub request: Req,
    pub cache_key: CacheKey,
    pub upstream: U,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl<Req, U> HandleStale<Req, U> {
    /// Create a new HandleStale state with its tracing span.
    pub fn new(request: Req, cache_key: CacheKey, upstream: U, parent: &Span) -> Self {
        Self {
            request,
            cache_key: cache_key.clone(),
            upstream,
            span: span!(parent: parent, Level::TRACE, "fsm.HandleStale", cache.key = %cache_key, stale.policy = field::Empty),
        }
    }
}

/// Data needed for background offload revalidation.
pub struct OffloadData<Req, U> {
    pub request: Req,
    pub cache_key: CacheKey,
    pub upstream: U,
}

/// Result of HandleStale transition, including optional offload data.
pub struct HandleStaleResult<Res, Req, U>
where
    U: Upstream<Req, Response = Res>,
{
    pub transition: HandleStaleTransition<Res, Req, U>,
    pub offload_data: Option<OffloadData<Req, U>>,
}

impl<Req, U> HandleStale<Req, U> {
    /// Transition from HandleStale state after future completes.
    ///
    /// Returns a result containing the transition and optional offload data.
    /// For `StalePolicy::OffloadRevalidate`, the caller is responsible for spawning
    /// the background revalidation using the returned `offload_data`.
    pub fn transition<Res>(
        mut self,
        response: Res,
        mut ctx: BoxContext,
        policy: &PolicyConfig,
    ) -> HandleStaleResult<Res, Req, U>
    where
        Res: CacheableResponse,
        U: Upstream<Req, Response = Res>,
    {
        let stale_policy = match policy {
            PolicyConfig::Enabled(EnabledCacheConfig { policy, .. }) => policy.stale,
            PolicyConfig::Disabled => StalePolicy::Return,
        };

        match stale_policy {
            StalePolicy::Return => {
                self.span.record("stale.policy", "return");
                ctx.set_status(CacheStatus::Stale);
                HandleStaleResult {
                    transition: HandleStaleTransition::Response(Response {
                        response,
                        ctx,
                        span: Span::none(),
                    }),
                    offload_data: None,
                }
            }
            StalePolicy::Revalidate => {
                self.span.record("stale.policy", "revalidate");
                ctx.set_status(CacheStatus::Miss);
                let upstream_future = self.upstream.call(self.request);
                HandleStaleResult {
                    transition: HandleStaleTransition::Revalidate {
                        upstream_future,
                        ctx,
                        cache_key: self.cache_key,
                    },
                    offload_data: None,
                }
            }
            StalePolicy::OffloadRevalidate => {
                self.span.record("stale.policy", "offload");
                ctx.set_status(CacheStatus::Stale);
                HandleStaleResult {
                    transition: HandleStaleTransition::Response(Response {
                        response,
                        ctx,
                        span: Span::none(),
                    }),
                    offload_data: Some(OffloadData {
                        request: self.request,
                        cache_key: self.cache_key,
                        upstream: self.upstream,
                    }),
                }
            }
        }
    }
}

impl<Req, U> std::fmt::Debug for HandleStale<Req, U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HandleStale")
            .field("cache_key", &self.cache_key)
            .finish_non_exhaustive()
    }
}

// =============================================================================
// AwaitResponse
// =============================================================================

/// Data for AwaitResponse state (non-pinned part).
///
/// The await response future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct AwaitResponse<Req, U> {
    pub request: Req,
    pub ctx: BoxContext,
    pub cache_key: CacheKey,
    pub upstream: U,
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl<Req, U> AwaitResponse<Req, U> {
    /// Create a new AwaitResponse state with its tracing span.
    pub fn new(
        request: Req,
        ctx: BoxContext,
        cache_key: CacheKey,
        upstream: U,
        parent: &Span,
    ) -> Self {
        Self {
            request,
            ctx,
            cache_key: cache_key.clone(),
            upstream,
            span: span!(parent: parent, Level::TRACE, "fsm.AwaitResponse", cache.key = %cache_key),
        }
    }

    /// Transition from AwaitResponse state after future completes.
    ///
    /// On success, returns the response directly.
    /// On concurrency error, falls back to calling upstream.
    pub fn transition<Res, C>(
        mut self,
        result: Result<Res, ConcurrencyError>,
        concurrency_manager: &C,
    ) -> AwaitResponseTransition<Res, Req, U>
    where
        Res: CacheableResponse,
        U: Upstream<Req, Response = Res>,
        C: ConcurrencyManager<Res>,
    {
        let ctx = self.ctx;

        match result {
            Ok(response) => AwaitResponseTransition::Response(Response {
                response,
                ctx,
                span: Span::none(),
            }),
            Err(ref concurrency_error) => {
                match concurrency_error {
                    ConcurrencyError::Lagged(n) => {
                        debug!(
                            "Concurrency channel lagged by {} messages, falling back to upstream",
                            n
                        );
                    }
                    ConcurrencyError::Closed => {
                        debug!(
                            "Concurrency channel closed, cleaning up stale entry and falling back to upstream"
                        );
                        concurrency_manager.cleanup(&self.cache_key);
                    }
                }

                let upstream_future = self.upstream.call(self.request);

                AwaitResponseTransition::PollUpstream {
                    upstream_future,
                    ctx,
                    cache_key: self.cache_key,
                }
            }
        }
    }
}

impl<Req, U> std::fmt::Debug for AwaitResponse<Req, U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AwaitResponse")
            .field("cache_key", &self.cache_key)
            .finish_non_exhaustive()
    }
}

// =============================================================================
// UpdateCache
// =============================================================================

/// Data for UpdateCache state (non-pinned part).
///
/// The update cache future is stored separately in the State enum to allow pinning.
/// When the future completes, this data is taken and passed to transition().
pub struct UpdateCache {
    /// Tracing span for this state (created on entry, entered on each poll).
    pub span: Span,
}

impl UpdateCache {
    /// Create a new UpdateCache state with its tracing span.
    pub fn new(parent: &Span) -> Self {
        Self {
            span: span!(parent: parent, Level::TRACE, "fsm.UpdateCache"),
        }
    }

    /// Transition from UpdateCache state after future completes.
    pub fn transition<Res>(self, response: Res, ctx: BoxContext) -> UpdateCacheTransition<Res> {
        UpdateCacheTransition::Response(Response {
            response,
            ctx,
            span: Span::none(),
        })
    }
}

impl std::fmt::Debug for UpdateCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UpdateCache").finish_non_exhaustive()
    }
}