rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
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
//! Pluggable proxy-recording backend (issue #315): the trait extracted from the concrete
//! per-imposter in-memory store, so embedders can persist
//! recordings or share the `proxyOnce` exactly-once gate across engine instances.
//!
//! The default [`LocalProxyStore`] kept the behaviour of the earlier embedded store (removed with
//! the reverse-proxy mode in #975) —
//! same `proxyOnce`/`proxyAlways`/`proxyTransparent` semantics, the same TOCTOU fix
//! (#171/#118), and the same caps — **plus** a release-on-error fix: a claim taken by
//! [`try_claim`](ProxyRecordingStore::try_claim) that never reaches
//! [`record`](ProxyRecordingStore::record) (the upstream call failed) is released via
//! [`release_claim`](ProxyRecordingStore::release_claim), so the signature stays retryable
//! instead of wedging forever.
//!
//! This backend is **port-scoped**: [`RequestSignature`] carries no
//! port, so a manager-scoped store shared across imposters must key by port explicitly to keep
//! identical signatures on different ports from colliding.

use super::mode::ProxyMode;
use super::types::{RecordedResponse, RequestSignature};
use crate::extensions::decorate::BackendUnavailable;
use crate::imposter::Stub;
use parking_lot::{Mutex, RwLock};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

/// Maximum number of recorded responses per signature (proxyAlways mode).
const MAX_RECORDINGS_PER_SIGNATURE: usize = 1000;

/// Maximum total number of unique request signatures to record (per port).
const MAX_TOTAL_SIGNATURES: usize = 10_000;

/// Opaque proof that the holder won the right to record a given `(port, signature)` once.
///
/// Issued by [`ProxyRecordingStore::try_claim`] and consumed by
/// [`record`](ProxyRecordingStore::record) / [`release_claim`](ProxyRecordingStore::release_claim).
/// A token is only valid until the claim it names is settled; a stale token (the claim expired
/// and was re-taken by another caller) records nothing, so a late loser cannot clobber the winner's
/// recording, and [`record`](ProxyRecordingStore::record) reports it with
/// [`ProxyStoreError::StaleClaim`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClaimToken(u64);

/// The recording a `proxyOnce` stub was inserted as: its signature and the claim that settled it.
/// The token is what lets a forget name that recording rather than whichever now holds the
/// signature (issue #1776).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordedClaim {
    pub signature: RequestSignature,
    pub token: ClaimToken,
}

impl ClaimToken {
    /// Construct a token from a raw value. Backends implementing [`ProxyRecordingStore`] mint
    /// their own tokens; the value only needs to be unique within the backend.
    #[must_use]
    pub fn new(value: u64) -> Self {
        Self(value)
    }

    /// The raw token value.
    #[must_use]
    pub fn value(self) -> u64 {
        self.0
    }
}

/// Outcome of [`ProxyRecordingStore::try_claim`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClaimOutcome {
    /// The caller won the claim and must either [`record`](ProxyRecordingStore::record) a
    /// response or [`release_claim`](ProxyRecordingStore::release_claim) it.
    Claimed(ClaimToken),
    /// Another caller already holds the claim (proxyOnce, concurrent first requests). The
    /// caller may still proxy upstream but must not record.
    InFlight,
    /// A response is already recorded for this `(port, signature)`; the caller should replay
    /// via [`lookup`](ProxyRecordingStore::lookup).
    AlreadyRecorded,
}

/// Error returned by a [`ProxyRecordingStore`] that could not answer.
///
/// Two shapes, because a store failure means two genuinely different things and the engine must
/// do opposite things about them. The built-in [`LocalProxyStore`] returns neither.
///
/// `#[non_exhaustive]`: embedders `match` on this, and the two arms below are unlikely to be the
/// last word on how a distributed backend can fail — the same reasoning [`StubPlacement`] carries.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ProxyStoreError {
    /// The backing store could not be reached, and the caller may **degrade**: forward upstream
    /// without recording. This is the right answer when the store is a persistence aid and the
    /// engine still enforces exactly-once itself — losing a recording costs a replay, not
    /// correctness.
    #[error("proxy recording store unavailable: {0}")]
    Unavailable(String),
    /// The store **is** the exactly-once arbiter and could neither grant nor deny the claim, so
    /// the caller must **not** forward: with nothing serializing claims, every request for the
    /// duration of the outage would reach the upstream, which is precisely what `proxyOnce`
    /// exists to prevent. The engine fails the request instead, and the response boundary answers
    /// it through [`backend_error_response`](crate::extensions::decorate::backend_error_response)
    /// — a `503` naming the carried `feature`/`detail`.
    ///
    /// Distinct from [`ClaimOutcome::InFlight`], which also forwards without recording: there a
    /// claim *was* serialized and the duplicate is bounded by the one racing window.
    #[error("proxy claim refused: {0}")]
    Refused(#[source] BackendUnavailable),
    /// The token no longer names the claim, so nothing was recorded. The claim is already gone, so
    /// the caller releases nothing, and under `proxyOnce` it inserts no stub (issue #1777). A store
    /// that answers `Ok` here keeps working, with that request's stub stacking beside the current
    /// one.
    #[error("proxy claim is stale")]
    StaleClaim,
}

/// Convenience alias for fallible proxy-store operations.
pub type Result<T> = std::result::Result<T, ProxyStoreError>;

/// Where a proxy-recorded stub belongs relative to the proxy stub that produced it.
///
/// These are engine semantics, not a hint: a store that publishes stubs itself
/// ([`ProxyRecordingStore::publishes_stubs`]) has to reproduce them, and deriving them from the
/// proxy mode independently is how a publisher silently drifts from the engine.
///
/// `#[non_exhaustive]`: a downstream publisher `match`es on this, and a future placement must not
/// break every one of them — the same reasoning [`EventContext`](crate::imposter::EventContext)
/// carries. Match with a `_` arm and treat an unknown placement as unpublishable rather than
/// guessing a position.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum StubPlacement {
    /// `proxyOnce`: insert **before** the proxy stub that answered, so the recording matches first
    /// next time.
    BeforeProxy,
    /// `proxyAlways`: merge the responses into the first stub **after** the proxy stub that
    /// answered whose predicates are structurally equal (issue #611; an empty list included), else
    /// **append at the end** of the list (issue #1389), as Mountebank does. The proxy keeps running
    /// and recording either way.
    AfterProxyMerging,
}

/// The stub the engine generated from a proxied response, together with where it belongs.
///
/// Handed to [`ProxyRecordingStore::complete`] before the stub is published, so a store can make
/// its own "recorded" commit conditional on publishing this successfully.
///
/// `#[non_exhaustive]`: embedders only ever read this type — the engine is the sole constructor —
/// so the context it will predictably grow must not become a breaking change to the seam whose
/// premise is stability. Same reasoning as [`EventContext`](crate::imposter::EventContext).
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct StubPublication<'a> {
    /// The generated stub, exactly as the engine would insert it.
    pub stub: &'a Stub,
    /// Where the stub belongs relative to the anchor: the stub at
    /// [`anchor_index`](Self::anchor_index), else the first stub proxying to
    /// [`proxy_to`](Self::proxy_to).
    pub placement: StubPlacement,
    /// `proxy.to` of the proxy stub this recording came from. Locates the anchor when
    /// [`anchor_index`](Self::anchor_index) is `None`: the first stub proxying to it.
    pub proxy_to: &'a str,
    /// Index of the stub that answered the request when the recording was made — the anchor
    /// [`placement`](Self::placement) is relative to (issue #1389). `None` when that stub is no
    /// longer in the list, or when no stub answered. Read from the stub list before the stub is
    /// published, so a publisher applying recordings out of order may find it off by one.
    pub anchor_index: Option<usize>,
}

/// Pluggable proxy-recording backend, keyed by imposter port.
pub trait ProxyRecordingStore: Send + Sync {
    /// First caller per `(port, signature)` wins the right to record once. Returns
    /// [`ClaimOutcome::InFlight`] if another caller already holds the claim and
    /// [`ClaimOutcome::AlreadyRecorded`] if a response is already stored.
    ///
    /// `Err(`[`Unavailable`](ProxyStoreError::Unavailable)`)` = backend unavailable; the engine
    /// degrades and forwards without recording. `Err(`[`Refused`](ProxyStoreError::Refused)`)` =
    /// the store arbitrates exactly-once and could not answer; the engine fails the request and
    /// does **not** forward it. Built-ins never fail.
    fn try_claim(&self, port: u16, sig: &RequestSignature) -> Result<ClaimOutcome>;

    /// [`try_claim`](Self::try_claim) for a request answered by a proxy response in `mode` (issue
    /// #1387). Each proxy stub has its own `mode`, as in Mountebank: only a `proxyOnce` request is
    /// gated, and a `proxyAlways` one is always granted a claim, whatever other proxy stubs the
    /// imposter has. The engine calls this, never `try_claim`.
    ///
    /// The default defers to `try_claim`, for a store that resolves the mode itself. A store that
    /// wraps another must forward this too, or the wrapped store applies its constructor's mode.
    fn try_claim_as(
        &self,
        port: u16,
        sig: &RequestSignature,
        mode: ProxyMode,
    ) -> Result<ClaimOutcome> {
        let _ = mode;
        self.try_claim(port, sig)
    }

    /// Releases a claim the engine will not settle, so the signature stays retryable: after a
    /// failed forward or behavior, and when the request is dropped mid-flight (client disconnect,
    /// imposter stop, panic — issue #1193). The last case calls it from a destructor, possibly
    /// during runtime shutdown, so it must be synchronous and quick and must not assume an async
    /// context. Stale tokens (the claim expired and was re-taken) are ignored.
    fn release_claim(&self, port: u16, sig: &RequestSignature, token: ClaimToken);

    /// Records the proxied response against the claim named by `token`. A stale token records
    /// nothing, so a late loser cannot overwrite the winner, and answers
    /// [`ProxyStoreError::StaleClaim`], which the caller does not release (issue #1777).
    ///
    /// Any other `Err` = backend unavailable. On `Err` the caller releases the
    /// claim (via [`release_claim`](Self::release_claim)), so an implementation that fails
    /// mid-record must leave the claim releasable — never hold `(port, signature)` pending
    /// after returning `Err`, or that signature wedges under `proxyOnce`.
    fn record(
        &self,
        port: u16,
        sig: RequestSignature,
        token: ClaimToken,
        resp: RecordedResponse,
    ) -> Result<()>;

    /// Settles the claim once the engine has built the stub the recording will be replayed from,
    /// and **before** that stub is published (issue #910).
    ///
    /// Called instead of [`record`](Self::record) whenever a stub was generated — that is, when
    /// `predicateGenerators`, `addWaitBehavior` or `addDecorateBehavior` is configured and
    /// predicate generation succeeded. When none of those is configured there is nothing to
    /// publish and the engine calls [`record`](Self::record) as before. When generation fails the
    /// engine takes no claim at all (issue #1333): recording a match-all stub would be wrong
    /// (issue #498), and replaying under a key without the request's identity would answer it with
    /// another request's recording.
    ///
    /// This ordering is the point: a store that publishes stubs to a shared or durable backend can
    /// make "this signature is Recorded" strictly conditional on its own publication ack, instead
    /// of committing it against a stub that does not exist yet.
    ///
    /// The predicates are generated from the request before the claim is taken, since with
    /// `predicateGenerators` they are part of the signature (issue #1333), so the claim is not
    /// held across a `predicateGenerators.inject` script.
    ///
    /// The default delegates to [`record`](Self::record), so a store that does not publish is
    /// unaffected.
    ///
    /// `Err` = backend unavailable, including "I could not publish the stub". As with
    /// [`record`](Self::record), [`ProxyStoreError::StaleClaim`] aside, the caller then releases
    /// the claim, so the signature stays
    /// retryable rather than wedging as Recorded-but-stub-less; the client still receives the
    /// upstream response, because the upstream call succeeded and only recording failed.
    fn complete(
        &self,
        port: u16,
        sig: RequestSignature,
        token: ClaimToken,
        resp: RecordedResponse,
        publication: &StubPublication<'_>,
    ) -> Result<()> {
        let _ = publication;
        self.record(port, sig, token, resp)
    }

    /// Whether this store publishes generated stubs itself.
    ///
    /// `true` makes the engine skip its own in-process stub insertion entirely, leaving the store
    /// as the only publisher — it must then honour the position carried by
    /// [`StubPublication::placement`]. `false` (the default) keeps the engine's insertion, and the
    /// stub handed to [`complete`](Self::complete) is informational.
    ///
    /// "Skip entirely" is unconditional, and that is the trade a publisher accepts: when no claim
    /// was won — a concurrent `proxyOnce` loser, or [`try_claim`](Self::try_claim) reporting the
    /// backend unavailable — there is no [`complete`](Self::complete) call *and* no local
    /// insertion, so that request's stub is published nowhere. The engine will not quietly keep a
    /// private copy the store's peers do not have.
    fn publishes_stubs(&self) -> bool {
        false
    }

    /// Returns the recorded response for replay, if any.
    fn lookup(&self, port: u16, sig: &RequestSignature) -> Option<RecordedResponse>;

    /// Forgets the `proxyOnce` recording made under `sig` (issue #1386), so the next request with
    /// that signature is claimed, forwarded and recorded again. The engine calls this after a stub
    /// edit (delete, replace, reconcile) removes the stub that recording was inserted as: in
    /// Mountebank the stub *is* the recording, so a deleted recording is recorded anew.
    ///
    /// It is also called on the request path (issue #1705), when a request reaches a `proxyOnce`
    /// stub while every stub recorded under `sig` sits behind it: that recording did not answer,
    /// so the request is forwarded and recorded in front of the answering stub, as in Mountebank.
    /// It must therefore stay quick and synchronous. The engine claims `sig` again right after, so
    /// a store whose `forget_recorded` does nothing keeps replaying, as before.
    ///
    /// A claim still in flight is not a recording yet and must be left to settle. `Err` is logged
    /// by the engine, which does not undo the stub edit that has already happened. The default does
    /// nothing, so a store that cannot forget keeps replaying the recording, as before #1386.
    fn forget_recorded(&self, port: u16, sig: &RequestSignature) -> Result<()> {
        let _ = (port, sig);
        Ok(())
    }

    /// [`forget_recorded`](Self::forget_recorded), only if `token` settled the recording now under
    /// `sig`. The engine decides what to forget a step before it forgets, so a recording landed in
    /// between is not the one it decided about (issue #1776), and calls only this method. The
    /// default forgets by signature, so a store that does not override it keeps that race, and a
    /// delete of an older copy of a recording forgets the signature while a newer stub holds it. A
    /// store that does not publish stubs should override it.
    fn forget_recorded_by(
        &self,
        port: u16,
        sig: &RequestSignature,
        token: ClaimToken,
    ) -> Result<()> {
        let _ = token;
        self.forget_recorded(port, sig)
    }

    /// Clears all saved proxy responses for a port. Backs the DELETE endpoint for saved
    /// proxy responses and the manager's port reclaim on imposter deletion.
    fn clear(&self, port: u16);
}

/// A `proxyOnce` recording and the claim that settled it.
#[derive(Debug)]
struct OnceRecording {
    token: ClaimToken,
    response: RecordedResponse,
}

/// Per-port recording state: the responses map plus the in-flight claim gate.
#[derive(Debug, Default)]
struct PortState {
    /// `proxyOnce` recordings: what the gate answers `AlreadyRecorded` from.
    responses: RwLock<HashMap<RequestSignature, OnceRecording>>,
    /// `proxyAlways` recordings, kept apart so a `proxyAlways` stub recording an identity never
    /// makes a `proxyOnce` stub replay it (issue #1387).
    always: RwLock<HashMap<RequestSignature, Vec<RecordedResponse>>>,
    /// Signatures with an in-flight `proxyOnce` claim, mapped to the active claim token.
    /// Presence is the TOCTOU-safe "someone is recording this" flag (#171/#118); the token
    /// lets [`record`](ProxyRecordingStore::record) and
    /// [`release_claim`](ProxyRecordingStore::release_claim) reject stale callers.
    pending: Mutex<HashMap<RequestSignature, u64>>,
}

/// Reference proxy-recording backend with the exact semantics of the embedded store it
/// replaced, plus the release-on-error fix and explicit port scoping.
///
/// The engine claims through [`try_claim_as`](ProxyRecordingStore::try_claim_as) with the mode of
/// the proxy response being served, so one store serves any mix of modes (issue #1387). The
/// constructor's mode is what [`try_claim`](ProxyRecordingStore::try_claim) applies, for a caller
/// that does not name one.
#[derive(Debug)]
pub struct LocalProxyStore {
    mode: ProxyMode,
    ports: RwLock<HashMap<u16, Arc<PortState>>>,
    /// Monotonic source of claim tokens, unique across ports and time.
    next_token: AtomicU64,
}

impl LocalProxyStore {
    /// Create a store whose [`try_claim`](ProxyRecordingStore::try_claim) applies `mode`.
    #[must_use]
    pub fn new(mode: ProxyMode) -> Self {
        Self {
            mode,
            ports: RwLock::new(HashMap::new()),
            next_token: AtomicU64::new(0),
        }
    }

    /// The mode [`try_claim`](ProxyRecordingStore::try_claim) applies.
    #[must_use]
    pub fn mode(&self) -> ProxyMode {
        self.mode
    }

    fn slot(&self, port: u16) -> Arc<PortState> {
        if let Some(slot) = self.ports.read().get(&port) {
            return Arc::clone(slot);
        }
        Arc::clone(self.ports.write().entry(port).or_default())
    }

    fn mint_token(&self) -> u64 {
        self.next_token.fetch_add(1, Ordering::SeqCst)
    }

    #[cfg(test)]
    fn recorded_len(&self, port: u16, sig: &RequestSignature) -> usize {
        self.recordings(port, sig).len()
    }

    #[cfg(test)]
    fn recordings(&self, port: u16, sig: &RequestSignature) -> Vec<RecordedResponse> {
        let state = self.slot(port);
        let once: Vec<RecordedResponse> = state
            .responses
            .read()
            .get(sig)
            .map(|recording| recording.response.clone())
            .into_iter()
            .collect();
        let always = state.always.read().get(sig).cloned().unwrap_or_default();
        once.into_iter().chain(always).collect()
    }
}

/// Set on a token for a claim that does not gate (`proxyAlways`, `proxyTransparent`), so `record`
/// and `release_claim` tell the two kinds of claim apart without consulting a mode. Tokens are
/// counted from zero and never reach it.
const UNGATED: u64 = 1 << 63;

impl ProxyRecordingStore for LocalProxyStore {
    fn try_claim(&self, port: u16, sig: &RequestSignature) -> Result<ClaimOutcome> {
        self.try_claim_as(port, sig, self.mode)
    }

    fn try_claim_as(
        &self,
        port: u16,
        sig: &RequestSignature,
        mode: ProxyMode,
    ) -> Result<ClaimOutcome> {
        match mode {
            ProxyMode::ProxyOnce => {
                let state = self.slot(port);
                // Hold the responses read guard across the pending claim so record() (which
                // needs responses.write()) cannot land a new recording between the
                // "not recorded" check and the claim — the #171/#118 TOCTOU fix.
                let responses = state.responses.read();
                if responses.contains_key(sig) {
                    return Ok(ClaimOutcome::AlreadyRecorded);
                }
                let mut pending = state.pending.lock();
                if pending.contains_key(sig) {
                    return Ok(ClaimOutcome::InFlight);
                }
                let token = self.mint_token();
                pending.insert(sig.clone(), token);
                Ok(ClaimOutcome::Claimed(ClaimToken(token)))
            }
            // proxyAlways / proxyTransparent never gate: every request proxies. The claim is a
            // formality so the caller path is uniform.
            ProxyMode::ProxyAlways | ProxyMode::ProxyTransparent => Ok(ClaimOutcome::Claimed(
                ClaimToken(self.mint_token() | UNGATED),
            )),
        }
    }

    fn release_claim(&self, port: u16, sig: &RequestSignature, token: ClaimToken) {
        if token.0 & UNGATED != 0 {
            return;
        }
        let state = self.slot(port);
        let mut pending = state.pending.lock();
        // Only release if the claim is still the one this token named; a stale token whose
        // claim was already re-taken must not free the new holder.
        if pending.get(sig).copied() == Some(token.0) {
            pending.remove(sig);
        }
    }

    fn record(
        &self,
        port: u16,
        sig: RequestSignature,
        token: ClaimToken,
        resp: RecordedResponse,
    ) -> Result<()> {
        let state = self.slot(port);
        if token.0 & UNGATED != 0 {
            // A store built for proxyTransparent records nothing an ungated claim brings.
            if self.mode == ProxyMode::ProxyTransparent {
                return Ok(());
            }
            let mut responses = state.always.write();
            if responses.len() >= MAX_TOTAL_SIGNATURES && !responses.contains_key(&sig) {
                tracing::debug!(
                    max = MAX_TOTAL_SIGNATURES,
                    "Recording store full, dropping new recording"
                );
                return Ok(());
            }
            let recordings = responses.entry(sig).or_default();
            if recordings.len() >= MAX_RECORDINGS_PER_SIGNATURE {
                tracing::debug!(
                    max = MAX_RECORDINGS_PER_SIGNATURE,
                    "Recording limit reached, dropping oldest"
                );
                recordings.remove(0);
            }
            recordings.push(resp);
            return Ok(());
        }
        // Lock order (responses then pending) matches try_claim to avoid deadlock.
        let mut responses = state.responses.write();
        let mut pending = state.pending.lock();
        // Stale or already-settled claim: drop the recording rather than clobber.
        if pending.get(&sig).copied() != Some(token.0) {
            tracing::debug!("Stale claim token; dropping recording (claim released/re-taken)");
            return Err(ProxyStoreError::StaleClaim);
        }
        if responses.len() >= MAX_TOTAL_SIGNATURES && !responses.contains_key(&sig) {
            tracing::debug!(
                max = MAX_TOTAL_SIGNATURES,
                "Recording store full, dropping new recording"
            );
            pending.remove(&sig);
            return Ok(());
        }
        responses.entry(sig.clone()).or_insert(OnceRecording {
            token,
            response: resp,
        });
        pending.remove(&sig);
        Ok(())
    }

    fn lookup(&self, port: u16, sig: &RequestSignature) -> Option<RecordedResponse> {
        let state = self.slot(port);
        let first = |map: &HashMap<RequestSignature, Vec<RecordedResponse>>| {
            map.get(sig)
                .and_then(|responses| responses.first().cloned())
        };
        // The engine looks up only after a proxyOnce `AlreadyRecorded`, so it must never be handed
        // a proxyAlways recording; a store built for proxyAlways still replays its own.
        if self.mode == ProxyMode::ProxyAlways {
            first(&state.always.read())
        } else {
            let responses = state.responses.read();
            responses
                .get(sig)
                .map(|recording| recording.response.clone())
        }
    }

    fn forget_recorded(&self, port: u16, sig: &RequestSignature) -> Result<()> {
        if let Some(state) = self.ports.read().get(&port) {
            state.responses.write().remove(sig);
        }
        Ok(())
    }

    fn forget_recorded_by(
        &self,
        port: u16,
        sig: &RequestSignature,
        token: ClaimToken,
    ) -> Result<()> {
        if let Some(state) = self.ports.read().get(&port) {
            let mut responses = state.responses.write();
            if responses.get(sig).is_some_and(|r| r.token == token) {
                responses.remove(sig);
            }
        }
        Ok(())
    }

    fn clear(&self, port: u16) {
        if let Some(state) = self.ports.read().get(&port) {
            let mut once = state.responses.write();
            let mut always = state.always.write();
            once.clear();
            always.clear();
            state.pending.lock().clear();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::util::unix_timestamp;

    fn sig(path: &str) -> RequestSignature {
        RequestSignature::new("GET", path, None, &[])
    }

    fn generated_stub() -> Stub {
        serde_json::from_value(serde_json::json!({
            "predicates": [{ "equals": { "path": "/test" } }],
            "responses": [{ "is": { "statusCode": 200, "body": "recorded" } }],
        }))
        .expect("valid stub")
    }

    fn resp(status: u16, body: &str) -> RecordedResponse {
        RecordedResponse {
            status,
            headers: Vec::new(),
            body: body.as_bytes().to_vec(),
            latency_ms: Some(10),
            timestamp_secs: unix_timestamp(),
        }
    }

    fn claim_token(outcome: ClaimOutcome) -> ClaimToken {
        match outcome {
            ClaimOutcome::Claimed(t) => t,
            other => panic!("expected Claimed, got {other:?}"),
        }
    }

    // AC6: proxyOnce records the first response only and replays it.
    #[test]
    fn local_proxy_once_records_first_only() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/test");

        let token = claim_token(store.try_claim(1, &s).unwrap());
        store
            .record(1, s.clone(), token, resp(200, "first"))
            .unwrap();

        // Now recorded: a second claim is refused, replay serves the first response.
        assert_eq!(
            store.try_claim(1, &s).unwrap(),
            ClaimOutcome::AlreadyRecorded
        );
        let recorded = store.lookup(1, &s).unwrap();
        assert_eq!(recorded.status, 200);
        assert_eq!(recorded.body, b"first");
        assert_eq!(store.recorded_len(1, &s), 1);
    }

    // AC6: proxyAlways appends every recording.
    #[test]
    fn local_proxy_always_records_all() {
        let store = LocalProxyStore::new(ProxyMode::ProxyAlways);
        let s = sig("/test");

        let t1 = claim_token(store.try_claim(1, &s).unwrap());
        store.record(1, s.clone(), t1, resp(200, "first")).unwrap();
        let t2 = claim_token(store.try_claim(1, &s).unwrap());
        store.record(1, s.clone(), t2, resp(201, "second")).unwrap();

        assert_eq!(store.recorded_len(1, &s), 2);
        // Replay serves the first.
        assert_eq!(store.lookup(1, &s).unwrap().status, 200);
    }

    // AC6: proxyAlways evicts the oldest recording past the per-signature cap.
    #[test]
    fn local_proxy_always_evicts_oldest() {
        let store = LocalProxyStore::new(ProxyMode::ProxyAlways);
        let s = sig("/evict");

        for i in 0..=MAX_RECORDINGS_PER_SIGNATURE {
            let t = claim_token(store.try_claim(1, &s).unwrap());
            store
                .record(1, s.clone(), t, resp(200, &format!("response-{i}")))
                .unwrap();
        }

        let recordings = store.recordings(1, &s);
        assert_eq!(recordings.len(), MAX_RECORDINGS_PER_SIGNATURE);
        assert_eq!(recordings[0].body, b"response-1", "oldest evicted first");
    }

    // AC6: proxyTransparent never records.
    #[test]
    fn local_proxy_transparent_never_records() {
        let store = LocalProxyStore::new(ProxyMode::ProxyTransparent);
        let s = sig("/test");

        let t = claim_token(store.try_claim(1, &s).unwrap());
        store.record(1, s.clone(), t, resp(200, "body")).unwrap();

        assert!(store.lookup(1, &s).is_none());
        assert_eq!(store.recorded_len(1, &s), 0);
    }

    // Issue #1387: the gate is the requesting proxy's mode, not the mode the store was built with.
    #[test]
    fn a_proxy_always_claim_never_gates_on_a_store_built_for_proxy_once() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/always");

        let t1 = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyAlways).unwrap());
        store.record(1, s.clone(), t1, resp(200, "first")).unwrap();

        assert!(matches!(
            store.try_claim_as(1, &s, ProxyMode::ProxyAlways).unwrap(),
            ClaimOutcome::Claimed(_)
        ));
    }

    #[test]
    fn a_proxy_once_claim_gates_on_a_store_built_without_a_proxy() {
        let store = LocalProxyStore::new(ProxyMode::ProxyTransparent);
        let s = sig("/once");

        let t1 = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        assert_eq!(
            store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::InFlight
        );
        store.record(1, s.clone(), t1, resp(200, "first")).unwrap();

        assert_eq!(
            store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::AlreadyRecorded
        );
        assert_eq!(store.lookup(1, &s).unwrap().body, b"first");
    }

    // A proxyAlways recording of an identity is not a proxyOnce recording of it: a proxyOnce stub
    // reached with the same signature must still forward and record its own.
    #[test]
    fn a_proxy_always_recording_does_not_satisfy_a_proxy_once_claim() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/shared");

        let t1 = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyAlways).unwrap());
        store.record(1, s.clone(), t1, resp(200, "always")).unwrap();

        let t2 = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        store.record(1, s.clone(), t2, resp(201, "once")).unwrap();
        assert_eq!(
            store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::AlreadyRecorded
        );
        assert_eq!(store.lookup(1, &s).unwrap().body, b"once");
    }

    // Releasing a proxyAlways claim must not free a proxyOnce claim in flight for the signature.
    #[test]
    fn releasing_a_proxy_always_claim_leaves_a_proxy_once_claim_in_flight() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/inflight");

        let _once = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        let always = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyAlways).unwrap());
        store.release_claim(1, &s, always);

        assert_eq!(
            store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::InFlight
        );
    }

    // A proxyAlways claim still records every response, whatever mode the store was built with.
    #[test]
    fn a_proxy_always_claim_appends_on_a_store_built_for_proxy_once() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/append");

        for body in ["a", "b"] {
            let t = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyAlways).unwrap());
            store.record(1, s.clone(), t, resp(200, body)).unwrap();
        }

        assert_eq!(store.recorded_len(1, &s), 2);
    }

    // A stale proxyOnce token is dropped on any store, not appended as a proxyAlways recording.
    #[test]
    fn a_stale_proxy_once_token_is_dropped_on_a_store_built_for_proxy_always() {
        let store = LocalProxyStore::new(ProxyMode::ProxyAlways);
        let s = sig("/stale-always");

        let stale = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        store.release_claim(1, &s, stale);
        let _live = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        assert!(matches!(
            store.record(1, s.clone(), stale, resp(200, "late")),
            Err(ProxyStoreError::StaleClaim)
        ));

        assert_eq!(store.recorded_len(1, &s), 0);
        assert!(store.lookup(1, &s).is_none());
    }

    // Releasing a proxyOnce claim frees it whatever mode the store was built with.
    #[test]
    fn a_released_proxy_once_claim_is_reclaimable_on_a_store_built_without_a_proxy() {
        let store = LocalProxyStore::new(ProxyMode::ProxyTransparent);
        let s = sig("/release");

        let t = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        store.release_claim(1, &s, t);

        assert!(matches!(
            store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::Claimed(_)
        ));
    }

    #[test]
    fn clear_drops_proxy_always_recordings_too() {
        let store = LocalProxyStore::new(ProxyMode::ProxyAlways);
        let s = sig("/clear");

        let t = claim_token(store.try_claim(1, &s).unwrap());
        store.record(1, s.clone(), t, resp(200, "always")).unwrap();
        store.clear(1);

        assert_eq!(store.recorded_len(1, &s), 0);
        assert!(store.lookup(1, &s).is_none());
    }

    // `lookup` replays proxyAlways recordings only on a store built for proxyAlways: the engine
    // looks up after a proxyOnce `AlreadyRecorded` and must never get another stub's recording.
    #[test]
    fn lookup_serves_proxy_always_recordings_only_on_a_store_built_for_them() {
        let s = sig("/lookup");
        let always = LocalProxyStore::new(ProxyMode::ProxyAlways);
        let once = LocalProxyStore::new(ProxyMode::ProxyOnce);
        for store in [&always, &once] {
            let t = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyAlways).unwrap());
            store.record(1, s.clone(), t, resp(200, "always")).unwrap();
        }

        assert_eq!(always.lookup(1, &s).unwrap().body, b"always");
        assert!(once.lookup(1, &s).is_none());
    }

    // Issue #1776: a forget names the recording it decided about, not whichever is there now.
    #[test]
    fn forget_recorded_by_leaves_a_newer_recording_alone() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/token");
        let old = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        store.record(1, s.clone(), old, resp(200, "old")).unwrap();
        store.forget_recorded_by(1, &s, old).unwrap();
        assert!(store.lookup(1, &s).is_none(), "its own token forgets it");

        let new = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());
        store.record(1, s.clone(), new, resp(200, "new")).unwrap();
        store.forget_recorded_by(1, &s, old).unwrap();
        assert_eq!(store.lookup(1, &s).unwrap().body, b"new");
    }

    // A store that only implements `forget_recorded` keeps forgetting through it.
    #[test]
    fn forget_recorded_by_defaults_to_forget_recorded() {
        #[derive(Default)]
        struct Forgets(Mutex<Vec<RequestSignature>>);
        impl ProxyRecordingStore for Forgets {
            fn try_claim(&self, _: u16, _: &RequestSignature) -> Result<ClaimOutcome> {
                Ok(ClaimOutcome::AlreadyRecorded)
            }
            fn release_claim(&self, _: u16, _: &RequestSignature, _: ClaimToken) {}
            fn record(
                &self,
                _: u16,
                _: RequestSignature,
                _: ClaimToken,
                _: RecordedResponse,
            ) -> Result<()> {
                Ok(())
            }
            fn lookup(&self, _: u16, _: &RequestSignature) -> Option<RecordedResponse> {
                None
            }
            fn forget_recorded(&self, _: u16, sig: &RequestSignature) -> Result<()> {
                self.0.lock().push(sig.clone());
                Ok(())
            }
            fn clear(&self, _: u16) {}
        }
        let store = Forgets::default();
        let s = sig("/default");
        store.forget_recorded_by(1, &s, ClaimToken(7)).unwrap();
        assert_eq!(*store.0.lock(), vec![s]);
    }

    // Issue #1386: forgetting a recording makes exactly that signature claimable again.
    #[test]
    fn forget_recorded_frees_exactly_that_signature() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let (a, b) = (sig("/a"), sig("/b"));
        for (port, s) in [(1, &a), (1, &b), (2, &a)] {
            let t = claim_token(store.try_claim_as(port, s, ProxyMode::ProxyOnce).unwrap());
            store.record(port, s.clone(), t, resp(200, "rec")).unwrap();
        }

        store.forget_recorded(1, &a).unwrap();

        assert!(matches!(
            store.try_claim_as(1, &a, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::Claimed(_)
        ));
        assert!(store.lookup(1, &a).is_none());
        assert_eq!(
            store.try_claim_as(1, &b, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::AlreadyRecorded,
            "another signature on the port keeps its recording"
        );
        assert_eq!(
            store.try_claim_as(2, &a, ProxyMode::ProxyOnce).unwrap(),
            ClaimOutcome::AlreadyRecorded,
            "the same signature on another port keeps its recording"
        );
    }

    // A claim in flight is not a recording yet: forgetting leaves it pending, so it still settles.
    #[test]
    fn forget_recorded_leaves_an_in_flight_claim_alone() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/inflight");
        let t = claim_token(store.try_claim_as(1, &s, ProxyMode::ProxyOnce).unwrap());

        store.forget_recorded(1, &s).unwrap();
        store.record(1, s.clone(), t, resp(200, "settled")).unwrap();

        assert_eq!(store.lookup(1, &s).unwrap().body, b"settled");
    }

    // The trait default keeps a store that cannot forget replaying, as before #1386.
    #[test]
    fn the_default_forget_recorded_is_a_no_op() {
        struct Fixed;
        impl ProxyRecordingStore for Fixed {
            fn try_claim(&self, _: u16, _: &RequestSignature) -> Result<ClaimOutcome> {
                Ok(ClaimOutcome::AlreadyRecorded)
            }
            fn release_claim(&self, _: u16, _: &RequestSignature, _: ClaimToken) {}
            fn record(
                &self,
                _: u16,
                _: RequestSignature,
                _: ClaimToken,
                _: RecordedResponse,
            ) -> Result<()> {
                Ok(())
            }
            fn lookup(&self, _: u16, _: &RequestSignature) -> Option<RecordedResponse> {
                None
            }
            fn clear(&self, _: u16) {}
        }

        assert!(Fixed.forget_recorded(1, &sig("/x")).is_ok());
        assert_eq!(
            Fixed.try_claim(1, &sig("/x")).unwrap(),
            ClaimOutcome::AlreadyRecorded
        );
    }

    // The trait default defers to `try_claim`, so a store that resolves its own mode is unchanged.
    #[test]
    fn the_default_try_claim_as_defers_to_try_claim() {
        struct AlwaysRecorded;
        impl ProxyRecordingStore for AlwaysRecorded {
            fn try_claim(&self, _: u16, _: &RequestSignature) -> Result<ClaimOutcome> {
                Ok(ClaimOutcome::AlreadyRecorded)
            }
            fn release_claim(&self, _: u16, _: &RequestSignature, _: ClaimToken) {}
            fn record(
                &self,
                _: u16,
                _: RequestSignature,
                _: ClaimToken,
                _: RecordedResponse,
            ) -> Result<()> {
                Ok(())
            }
            fn lookup(&self, _: u16, _: &RequestSignature) -> Option<RecordedResponse> {
                None
            }
            fn clear(&self, _: u16) {}
        }

        assert_eq!(
            AlwaysRecorded
                .try_claim_as(1, &sig("/x"), ProxyMode::ProxyAlways)
                .unwrap(),
            ClaimOutcome::AlreadyRecorded
        );
    }

    // AC3: concurrent claimers get exactly one Claimed; the rest see InFlight.
    #[test]
    fn local_concurrent_try_claim_one_winner() {
        use std::thread;

        let store = Arc::new(LocalProxyStore::new(ProxyMode::ProxyOnce));
        let s = sig("/concurrent");
        let claimed = Arc::new(AtomicU64::new(0));

        let handles: Vec<_> = (0..16)
            .map(|_| {
                let store = Arc::clone(&store);
                let s = s.clone();
                let claimed = Arc::clone(&claimed);
                thread::spawn(move || {
                    if let ClaimOutcome::Claimed(_) = store.try_claim(1, &s).unwrap() {
                        claimed.fetch_add(1, Ordering::SeqCst);
                    }
                })
            })
            .collect();
        for h in handles {
            h.join().unwrap();
        }

        assert_eq!(
            claimed.load(Ordering::SeqCst),
            1,
            "exactly one claimer wins in proxyOnce"
        );
    }

    // AC2 (the wedge regression): a released claim makes the signature reclaimable. Fails on
    // the pre-fix code where a failed upstream call left the signature pending forever.
    #[test]
    fn local_release_makes_signature_reclaimable() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/wedge");

        // First caller claims, then the upstream call "fails" so it releases.
        let t1 = claim_token(store.try_claim(1, &s).unwrap());
        store.release_claim(1, &s, t1);

        // A subsequent request must be able to claim and record — not be told InFlight forever.
        let outcome = store.try_claim(1, &s).unwrap();
        let t2 = claim_token(outcome);
        store
            .record(1, s.clone(), t2, resp(200, "recovered"))
            .unwrap();
        assert_eq!(store.lookup(1, &s).unwrap().body, b"recovered");
    }

    // AC4: a stale token (its claim expired and was re-taken) neither releases nor records.
    #[test]
    fn local_stale_token_release_and_record_ignored() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/stale");

        let t1 = claim_token(store.try_claim(1, &s).unwrap());
        store.release_claim(1, &s, t1); // claim freed
        let t2 = claim_token(store.try_claim(1, &s).unwrap()); // re-taken by a new caller

        // Stale release must not free t2's live claim.
        store.release_claim(1, &s, t1);
        assert_eq!(
            store.try_claim(1, &s).unwrap(),
            ClaimOutcome::InFlight,
            "stale release must not free the live claim"
        );

        // Stale record must be dropped, not stored.
        assert!(matches!(
            store.record(1, s.clone(), t1, resp(500, "stale")),
            Err(ProxyStoreError::StaleClaim)
        ));
        assert!(
            store.lookup(1, &s).is_none(),
            "stale record must be ignored"
        );

        // The live holder can still record.
        store.record(1, s.clone(), t2, resp(200, "fresh")).unwrap();
        assert_eq!(store.lookup(1, &s).unwrap().body, b"fresh");
    }

    // AC5: identical signatures on different ports do not collide through a shared store.
    #[test]
    fn local_ports_are_isolated() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/shared");

        let t = claim_token(store.try_claim(1, &s).unwrap());
        store.record(1, s.clone(), t, resp(200, "port-1")).unwrap();

        // Port 2 sees the signature as fresh, not AlreadyRecorded.
        assert!(matches!(
            store.try_claim(2, &s).unwrap(),
            ClaimOutcome::Claimed(_)
        ));
        assert!(store.lookup(2, &s).is_none());
        assert_eq!(store.lookup(1, &s).unwrap().body, b"port-1");
    }

    // clear() drops a port's recordings and reopens the gate.
    #[test]
    fn local_clear_resets_port() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/clear");

        let t = claim_token(store.try_claim(1, &s).unwrap());
        store.record(1, s.clone(), t, resp(200, "body")).unwrap();
        assert!(store.lookup(1, &s).is_some());

        store.clear(1);
        assert!(store.lookup(1, &s).is_none());
        assert!(matches!(
            store.try_claim(1, &s).unwrap(),
            ClaimOutcome::Claimed(_)
        ));
    }

    /// Implements only the methods that existed before #910 — the shape every pre-#910 store has.
    /// Its mere existence is the source-compatibility assertion; the tests below use it to prove
    /// the new defaults behave as documented.
    #[derive(Default)]
    struct LegacyStore {
        recorded: Mutex<Vec<(u16, RequestSignature, RecordedResponse)>>,
    }

    impl ProxyRecordingStore for LegacyStore {
        fn try_claim(&self, _port: u16, _sig: &RequestSignature) -> Result<ClaimOutcome> {
            Ok(ClaimOutcome::Claimed(ClaimToken::new(1)))
        }
        fn release_claim(&self, _port: u16, _sig: &RequestSignature, _token: ClaimToken) {}
        fn record(
            &self,
            port: u16,
            sig: RequestSignature,
            _token: ClaimToken,
            resp: RecordedResponse,
        ) -> Result<()> {
            self.recorded.lock().push((port, sig, resp));
            Ok(())
        }
        fn lookup(&self, _port: u16, _sig: &RequestSignature) -> Option<RecordedResponse> {
            None
        }
        fn clear(&self, _port: u16) {}
    }

    // AC2/AC5 (#910): a store written before `complete` existed still compiles, and the default
    // `complete` delegates to its `record`. Driven through `dyn` so object safety is asserted too.
    #[test]
    fn default_complete_delegates_to_record() {
        let store = LegacyStore::default();
        let stub = generated_stub();
        let publication = StubPublication {
            stub: &stub,
            placement: StubPlacement::BeforeProxy,
            proxy_to: "http://upstream",
            anchor_index: None,
        };

        let dynamic: &dyn ProxyRecordingStore = &store;
        assert!(
            !dynamic.publishes_stubs(),
            "a store publishes nothing unless it opts in"
        );
        dynamic
            .complete(
                7,
                sig("/legacy"),
                ClaimToken::new(1),
                resp(200, "ok"),
                &publication,
            )
            .expect("default complete delegates to record");

        let recorded = store.recorded.lock();
        assert_eq!(recorded.len(), 1, "the recording reached record()");
        assert_eq!(recorded[0].0, 7, "port is passed through");
        assert_eq!(recorded[0].2.body, b"ok");
    }

    // AC1/AC6 (#910): completing a claim through `complete` is recording — same visible result as
    // `record`, so `LocalProxyStore` is unchanged by the new seam.
    #[test]
    fn local_complete_records_like_record() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);
        let s = sig("/complete");
        let stub = generated_stub();
        let publication = StubPublication {
            stub: &stub,
            placement: StubPlacement::BeforeProxy,
            proxy_to: "http://upstream",
            anchor_index: None,
        };

        let t = claim_token(store.try_claim(1, &s).unwrap());
        store
            .complete(1, s.clone(), t, resp(200, "done"), &publication)
            .unwrap();

        assert_eq!(store.lookup(1, &s).unwrap().body, b"done");
        assert_eq!(
            store.try_claim(1, &s).unwrap(),
            ClaimOutcome::AlreadyRecorded,
            "complete settles the claim exactly as record does"
        );
        assert!(!store.publishes_stubs());
    }

    // The total-signatures cap drops new signatures once full (proxyOnce), as before.
    #[test]
    fn local_total_signatures_cap_drops_new() {
        let store = LocalProxyStore::new(ProxyMode::ProxyOnce);

        for i in 0..MAX_TOTAL_SIGNATURES {
            let s = sig(&format!("/path-{i}"));
            let t = claim_token(store.try_claim(1, &s).unwrap());
            store.record(1, s, t, resp(200, "ok")).unwrap();
        }

        let overflow = sig("/overflow");
        let t = claim_token(store.try_claim(1, &overflow).unwrap());
        store
            .record(1, overflow.clone(), t, resp(200, "dropped"))
            .unwrap();
        assert!(
            store.lookup(1, &overflow).is_none(),
            "overflow signature dropped once the port is full"
        );
    }
}