hf2q 0.1.1

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
//! ADR-017 §C.1 — `LoaderWrapper<E>`: a `ModelLoader<E>`-decorating
//! pass-through that drives [`crate::serve::kv_persist::KvPersistRegistry::bind_for`]
//! on every successful load and `unbind_for` at evict time.
//!
//! ## Why this exists (Chesterton's fence)
//!
//! Phase A.3's [`crate::serve::kv_persist::BlockPrefixCacheSpiller`]
//! has a per-family hook table keyed on `(repo, quant)`. Per-family
//! hooks that need engine-side state (e.g. Gemma 4 dense K/V buffers
//! living inside `MlxModelWeights`) need their engine handle wired
//! BEFORE the spiller's `post_admit` trigger fires (see
//! `spiller.rs:398-477`). The natural seam is the load path — the
//! moment a fresh `Engine` is constructed.
//!
//! `LoaderWrapper` decorates `Arc<dyn ModelLoader<E>>` to perform that
//! binding without modifying the `ModelLoader` trait surface (which
//! is stable per the iter-212 ship contract). On every successful
//! `load(path, config) -> E`, the wrapper:
//!
//! 1. Reads its `pending_bind` slot for `(repo, quant)`. The slot is
//!    populated by `cmd_serve` immediately before the `pool.load_or_get`
//!    call that drives this loader.
//! 2. Wraps the freshly-loaded `E` in `Arc<E>` and constructs an
//!    `Arc<dyn Any + Send + Sync>` view on the same allocation.
//! 3. Calls `registry.bind_for(repo, quant, engine_dyn)`. The hook's
//!    `EngineBindable::bind_engine` impl performs the downcast — for
//!    Gemma 4 dense it pulls out an `Arc<EngineHandle>`; for the C.1
//!    StubGemma4Spill it's a silent no-op.
//! 4. Reclaims the inner `E` via `Arc::try_unwrap` and returns it to
//!    the manager. This succeeds iff the bind hook didn't keep a clone
//!    of the `Arc`. The `EngineBindable` contract requires hooks to
//!    keep only what they downcast OUT of the Arc (e.g. the inner
//!    `Arc<EngineHandle>` reachable from the type-erased Any), not
//!    the Arc itself.
//!
//! ## Why a `pending_bind` slot instead of a thread-local
//!
//! The `ModelLoader::load(path, config)` signature carries no
//! `(repo, quant)` context — `HotSwapManager::load_or_get` knows
//! those values but the loader callsite inside `load_or_get` does
//! not pass them through (see `multi_model.rs::HotSwapManager`'s
//! load path; modifying that surface is out-of-scope for C.1).
//!
//! Two seams are available without touching `multi_model.rs`:
//!
//! 1. A thread-local set by `cmd_serve` before `load_or_get`. Fragile
//!    under tokio's work-stealing scheduler — the load can resume on
//!    a different thread than the setter.
//! 2. A `Mutex<Option<(String, QuantType)>>` "pending" slot on the
//!    wrapper itself, set by `cmd_serve` before the
//!    synchronous `load_or_get` call. The wrapper consumes the slot
//!    inside its `load(...)` impl. Production `cmd_serve` startup is
//!    single-threaded so there is no setter/load race. Tests assert
//!    the discipline directly (the bind only fires when a pending
//!    slot is set).
//!
//! Option 2 is the chosen design. It keeps the dependency graph
//! local (no env / TLS pollution) and exercises a synchronous
//! contract that's easy to test.
//!
//! ## What the wrapper does NOT do
//!
//! - Does NOT panic on `Arc::try_unwrap` failure. A failure here means
//!   the hook held a clone of the type-erased Arc (which violates the
//!   `EngineBindable` contract). The wrapper surfaces this as a
//!   `LoaderFailed`-shaped `anyhow::Error` so the manager treats the
//!   load as failed (same shape as a real load error). Production
//!   hooks NEVER hit this path; tests assert the shape of the error.
//! - Does NOT enforce that the registry has a hook registered for
//!   `(repo, quant)`. Missing-hook is a silent no-op at the registry
//!   layer (per `KvPersistRegistry::bind_for` semantics).
//! - Does NOT clear the pending slot on load failure — the inner
//!   loader's failure is the operator's signal to retry, and a stale
//!   pending slot is a no-op until the next successful load consumes
//!   it.

use std::any::Any;
use std::marker::PhantomData;
use std::path::Path;
use std::sync::{Arc, Mutex};

use anyhow::Result;

use crate::serve::kv_persist::registry::KvPersistRegistry;
use crate::serve::kv_persist::spiller::{BlockPrefixCacheSpiller, KvCacheSpill};
use crate::serve::multi_model::{EngineConfig, ModelLoader};
use crate::serve::quant_select::QuantType;

/// Decorating `ModelLoader<E>` that drives the C.1 engine-binding
/// registry. See module docs for the full design rationale.
///
/// ## Phase B-dense.2 — factory substitution seam
///
/// `LoaderWrapper::load`'s post-load callback now calls
/// `registry.try_substitute_on_load(repo, quant, engine_dyn)` BEFORE
/// the existing `registry.bind_for(...)` step. If a registered
/// `FamilyHookFactory` produces a real per-family hook (e.g.
/// `Gemma4DenseSpill` from `Gemma4DenseSpillFactory`), the wrapper
/// also updates the *spiller's* `register_family` map (via the
/// optional `spiller` field) so the on-disk snapshot/restore
/// dispatch lands on the real hook, not the C.1 stub.
///
/// Both updates run atomically per the P1-1 fix (adversarial review
/// 2026-05-01 §P1-1): the wrapper holds a dedicated
/// `substitute_lock: Mutex<()>` across BOTH the registry's
/// `try_substitute_on_load` write AND the spiller's
/// `register_family` write, so a concurrent `pre_evict` / `post_admit`
/// triggered from another tokio task NEVER observes the registry as
/// "real-substituted" while the spiller still carries the C.1 stub.
/// Without this lock, the registry write and the spiller write are
/// two independent `RwLock` writers and a tiny (~µs) divergence
/// window exists between them. If substitution returns `None` (no
/// factory OR factory rejects the engine type), the wrapper falls
/// through to `bind_for` on the existing stub registration — a clean
/// no-op for the auto-Arc<E> path.
pub struct LoaderWrapper<E> {
    /// Underlying loader. Production wires `DefaultModelLoader`;
    /// tests substitute a mock.
    inner: Arc<dyn ModelLoader<E>>,
    /// C.1 registry — `bind_for` / `unbind_for` are routed through
    /// this `Arc`. Cheap-clones into the wrapper at construction.
    registry: Arc<KvPersistRegistry>,
    /// Operator-set `(repo, quant)` for the next pending load.
    /// Consumed (taken) inside `ModelLoader::load`. Always `None`
    /// outside of an in-flight `set_pending_bind` → `load_or_get`
    /// transition.
    pending_bind: Mutex<Option<(String, QuantType)>>,
    /// Phase B-dense.2 — optional spiller for the
    /// `try_substitute_on_load` flow. When set, a successful factory
    /// substitution also overwrites the spiller's per-family hook
    /// table so the spiller's `pre_evict` / `post_admit` triggers
    /// dispatch on the real hook. When None (e.g. C.1-only test
    /// fixtures), the substitution path skips the spiller update —
    /// the registry's hook map is still updated, but the spiller
    /// keeps its prior `register_family` entry.
    spiller: Mutex<Option<Arc<BlockPrefixCacheSpiller<E>>>>,
    /// P1-1 atomicity fix (adversarial review 2026-05-01 §P1-1).
    /// Held by `load(...)` across the two-step sequence:
    ///
    ///   1. `registry.try_substitute_on_load(...)` (write-locks the
    ///      registry's `hooks` map internally), then
    ///   2. `update_spiller_registration(...)` (write-locks the
    ///      spiller's `registrations` map internally).
    ///
    /// Without this critical section the two write-locks are
    /// independent: a concurrent reader (e.g. `pre_evict` from a
    /// tokio task) can observe a state where the registry already
    /// has the real substituted hook but the spiller still has the
    /// C.1 stub. The lock is never held across calls into the inner
    /// `ModelLoader` (only around the post-load substitution), so
    /// it does not serialize the load itself.
    substitute_lock: Mutex<()>,
    /// PhantomData to bind the `E` generic to the wrapper. The
    /// `fn(E)` form makes `LoaderWrapper<E>` invariant in `E` and
    /// avoids unnecessary auto-trait implications.
    _phantom: PhantomData<fn(E)>,
}

impl<E> LoaderWrapper<E>
where
    E: Send + Sync + 'static,
{
    /// Construct a wrapper around `inner` driving `registry`. The
    /// wrapper starts with no pending bind; `cmd_serve` arms the slot
    /// before each `pool.load_or_get` call.
    pub fn new(inner: Arc<dyn ModelLoader<E>>, registry: Arc<KvPersistRegistry>) -> Self {
        Self {
            inner,
            registry,
            pending_bind: Mutex::new(None),
            spiller: Mutex::new(None),
            substitute_lock: Mutex::new(()),
            _phantom: PhantomData,
        }
    }

    /// Phase B-dense.2 — wire the spiller for the
    /// `try_substitute_on_load` flow. When set, a successful factory
    /// substitution also calls `spiller.register_family(repo, quant,
    /// kv_hook)` so the on-disk snapshot/restore dispatch lands on
    /// the real per-family hook.
    ///
    /// `cmd_serve` calls this immediately after constructing the
    /// `BlockPrefixCacheSpiller` and the `LoaderWrapper`, before
    /// arming `set_pending_bind`. C.1-only callers (no factory
    /// registered) may safely skip this — the wrapper degrades to
    /// the C.1 behavior (registry-only update on bind_for).
    pub fn set_spiller(&self, spiller: Arc<BlockPrefixCacheSpiller<E>>) {
        let mut g = self
            .spiller
            .lock()
            .expect("LoaderWrapper::spiller Mutex poisoned");
        *g = Some(spiller);
    }

    /// Arm the pending-bind slot for the next `load(...)` call. Must
    /// be invoked synchronously immediately before the load_or_get
    /// driver call (see module docs for the synchronous-contract
    /// rationale).
    ///
    /// Calling twice without an intervening `load(...)` overwrites the
    /// prior pending — the second `(repo, quant)` wins.
    pub fn set_pending_bind(&self, repo: String, quant: QuantType) {
        let mut g = self
            .pending_bind
            .lock()
            .expect("LoaderWrapper::pending_bind Mutex poisoned");
        *g = Some((repo, quant));
    }

    /// Drop the pending-bind slot without firing a load. Used by
    /// `cmd_serve` if the load sequence is aborted before it can
    /// drive `load_or_get` (defensive — the production path always
    /// drives the load_or_get immediately so this should never fire).
    pub fn clear_pending_bind(&self) {
        let mut g = self
            .pending_bind
            .lock()
            .expect("LoaderWrapper::pending_bind Mutex poisoned");
        *g = None;
    }

    /// Test / diagnostic accessor: returns the current pending slot
    /// without consuming it.
    pub fn pending_bind(&self) -> Option<(String, QuantType)> {
        let g = self
            .pending_bind
            .lock()
            .expect("LoaderWrapper::pending_bind Mutex poisoned");
        g.clone()
    }

    /// Drive `unbind_for(repo, quant)` directly through the registry.
    /// Phase C.1's evict-path wire-up calls this from `cmd_serve`'s
    /// graceful-shutdown / explicit-evict paths so stale engine
    /// handles drop in lock-step with the manager's evict.
    ///
    /// Idempotent (matches `KvPersistRegistry::unbind_for`
    /// semantics — no-op when no hook is registered).
    pub fn drive_unbind(&self, repo: &str, quant: QuantType) {
        self.registry.unbind_for(repo, quant);
    }

    /// Test / diagnostic accessor: returns the registry Arc for
    /// downstream inspection.
    pub fn registry(&self) -> Arc<KvPersistRegistry> {
        Arc::clone(&self.registry)
    }

    /// Phase B-dense.2 — update the spiller's `register_family` map
    /// after a successful factory substitution. No-op when no
    /// spiller is wired (C.1-only callers / unit tests).
    ///
    /// Called from inside `load(...)` after
    /// `registry.try_substitute_on_load` returns `Some(kv_hook)`.
    /// The spiller's `register_family` overwrites any prior C.1
    /// stub registration (matches its existing
    /// "freshest-registration-wins" semantic).
    ///
    /// P1-1 (adversarial review 2026-05-01): the caller MUST hold
    /// `LoaderWrapper::substitute_lock` across both the preceding
    /// `try_substitute_on_load` call and this update so the two
    /// internal-RwLock writes are observably atomic to any
    /// concurrent `pre_evict` / `post_admit` reader.
    fn update_spiller_registration(
        &self,
        repo: &str,
        quant: QuantType,
        kv_hook: Arc<Mutex<dyn KvCacheSpill>>,
    ) {
        let g = self
            .spiller
            .lock()
            .expect("LoaderWrapper::spiller Mutex poisoned");
        if let Some(spiller_arc) = g.as_ref() {
            spiller_arc.register_family(repo.to_string(), quant, kv_hook);
        }
    }
}

impl<E> ModelLoader<E> for LoaderWrapper<E>
where
    E: Send + Sync + 'static,
{
    /// Load via the inner loader, then drive the registry's
    /// `bind_for` for the pending `(repo, quant)` (if any). Returns
    /// the freshly-loaded `E` to the manager.
    ///
    /// Returns `Err(...)` iff the inner loader failed OR the bind
    /// hook held a clone of the type-erased Arc (contract violation;
    /// see module docs).
    fn load(&self, path: &Path, config: &EngineConfig) -> Result<E> {
        // 1. Drive the inner loader. Propagate errors verbatim — the
        //    wrapper does NOT bind on failure (per Hypothesis 2 in the
        //    spec; covered by the loader-wrapper-does-not-bind-on-load-failure
        //    test).
        let engine = self.inner.load(path, config)?;

        // 2. Consume the pending-bind slot. If empty, pass-through —
        //    the bind only fires when `cmd_serve` armed it.
        let pending = {
            let mut g = self
                .pending_bind
                .lock()
                .expect("LoaderWrapper::pending_bind Mutex poisoned");
            g.take()
        };
        let Some((repo, quant)) = pending else {
            return Ok(engine);
        };

        // 3. Build a type-erased Arc view on the freshly-loaded
        //    engine and drive the registry.
        //
        // Phase B-dense.2: BEFORE bind_for, call
        // `try_substitute_on_load`. If a factory is registered for
        // (repo, quant) AND the engine type matches (e.g.
        // `Arc<EngineHandle>` for Gemma4 production wiring), the
        // factory produces a real per-family hook tuple
        // `(kv_hook, bindable_hook)`. The registry's hook map is
        // already updated by `try_substitute_on_load`; the wrapper
        // also updates the spiller's `register_family` map so the
        // on-disk snapshot/restore dispatch lands on the real hook.
        //
        // If `try_substitute_on_load` returns None (no factory OR
        // factory rejects the engine type), the substitution is a
        // no-op and the existing C.1 stub registration remains in
        // the spiller + registry. Post-B-dense.2-follow-up
        // (commit 420ef94, gemma4_dense.rs:1373-1390): the
        // Gemma4DenseSpillFactory downcasts `Arc<Engine>` FIRST and
        // reads the cached `KvSpillDescriptor` from
        // `EngineInner.kv_spill_descriptor` (set in
        // `Engine::spawn` at engine.rs:1503-1530 for
        // `LoadedModel::Gemma`), so the auto-Arc<E> path SUCCEEDS
        // for Gemma 4 production loads. The Arc<EngineHandle>
        // fallback path is for B-dense.1 backwards-compat tests.
        let arc_engine: Arc<E> = Arc::new(engine);
        let dyn_view: Arc<dyn Any + Send + Sync> = Arc::clone(&arc_engine) as _;

        // 3a. Try substitution. The clone of dyn_view is consumed by
        //     try_substitute_on_load (it must take ownership of the
        //     Arc<dyn Any>). On Some(_), update the spiller's
        //     register_family table to keep registry + spiller in
        //     lock-step.
        //
        // P1-1 atomicity (adversarial review 2026-05-01 §P1-1):
        // `try_substitute_on_load` write-locks the registry's
        // internal `hooks` map, and `update_spiller_registration`
        // write-locks the spiller's internal `registrations` map —
        // two SEPARATE locks. Without an outer critical section a
        // concurrent reader (e.g. `pre_evict` from a tokio task)
        // could observe (registry=REAL, spiller=STUB) in the µs
        // window between the two write-locks. We hold
        // `substitute_lock` across BOTH operations so the two-map
        // update is observably atomic from any concurrent reader.
        // The lock is never held across the inner loader call
        // (above) or `bind_for` (below), so it does not serialize
        // the load itself or the C.1 stub-bind path.
        let dyn_view_for_substitute: Arc<dyn Any + Send + Sync> = Arc::clone(&dyn_view);
        {
            let _substitute_guard = self
                .substitute_lock
                .lock()
                .expect("LoaderWrapper::substitute_lock Mutex poisoned");
            if let Some(kv_hook) =
                self.registry
                    .try_substitute_on_load(&repo, quant, dyn_view_for_substitute)
            {
                self.update_spiller_registration(&repo, quant, kv_hook);
            }
        }

        // 3b. The C.1 bind_for flow always fires after the optional
        //     B-dense.2 substitution — for the auto-Arc<E> path, the
        //     stub's bind_engine is a silent no-op; for the explicit
        //     Arc<EngineHandle> path (which cmd_serve drives via a
        //     SEPARATE post-load bind, not this auto-call),
        //     try_substitute_on_load already wired the real spill
        //     and bind_for is redundant but harmless.
        self.registry.bind_for(&repo, quant, dyn_view);

        // 4. Reclaim the inner E. `Arc::try_unwrap` succeeds iff the
        //    only outstanding strong reference is `arc_engine` itself.
        //    The contract on `EngineBindable::bind_engine` requires
        //    hooks to drop the type-erased Arc before returning (they
        //    keep at most a downcast-derived inner Arc — e.g.
        //    `Arc<EngineHandle>` rather than the original
        //    `Arc<dyn Any>`).
        Arc::try_unwrap(arc_engine).map_err(|_| {
            anyhow::anyhow!(
                "LoaderWrapper: registry hook for (repo={repo}, quant={}) \
                 retained a clone of the type-erased engine Arc; \
                 this violates the EngineBindable contract",
                quant.as_str()
            )
        })
    }
}

// ---------------------------------------------------------------------------
// Tests.
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::serve::kv_persist::EngineBindable;
    use std::sync::atomic::{AtomicU32, Ordering};

    /// Synthetic engine type used by the loader tests. Holds a marker
    /// the tests can read back to verify the engine round-trips intact
    /// through the wrapper.
    #[derive(Debug)]
    struct TestEngine {
        marker: u32,
    }

    /// Mock `ModelLoader<TestEngine>` that returns either a fresh
    /// `TestEngine` or an error, depending on its `fail_next` flag.
    /// Records every load call for assertion.
    struct MockLoader {
        load_count: AtomicU32,
        next_marker: AtomicU32,
        fail_next: AtomicU32, // 0 = succeed, 1 = fail
    }

    impl MockLoader {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                load_count: AtomicU32::new(0),
                next_marker: AtomicU32::new(0xC1A1),
                fail_next: AtomicU32::new(0),
            })
        }

        fn force_fail_next(&self) {
            self.fail_next.store(1, Ordering::SeqCst);
        }

        fn load_count(&self) -> u32 {
            self.load_count.load(Ordering::SeqCst)
        }
    }

    impl ModelLoader<TestEngine> for MockLoader {
        fn load(&self, _path: &Path, _config: &EngineConfig) -> Result<TestEngine> {
            self.load_count.fetch_add(1, Ordering::SeqCst);
            if self.fail_next.swap(0, Ordering::SeqCst) == 1 {
                anyhow::bail!("mock loader: synthetic load failure")
            }
            let m = self.next_marker.fetch_add(1, Ordering::SeqCst);
            Ok(TestEngine { marker: m })
        }
    }

    /// Mock `EngineBindable` that records every bind/unbind. Mirrors
    /// the registry tests' mock but lives here so the loader_wrapper
    /// tests are self-contained.
    struct LwMockBindable {
        binds: AtomicU32,
        unbinds: AtomicU32,
        /// If `true`, retain a clone of the engine_dyn Arc on bind.
        /// Used to simulate the contract-violation path.
        retain_arc: bool,
        retained: std::sync::Mutex<Option<Arc<dyn Any + Send + Sync>>>,
    }

    impl LwMockBindable {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                binds: AtomicU32::new(0),
                unbinds: AtomicU32::new(0),
                retain_arc: false,
                retained: std::sync::Mutex::new(None),
            })
        }

        fn new_retaining() -> Arc<Self> {
            Arc::new(Self {
                binds: AtomicU32::new(0),
                unbinds: AtomicU32::new(0),
                retain_arc: true,
                retained: std::sync::Mutex::new(None),
            })
        }

        fn bind_count(&self) -> u32 {
            self.binds.load(Ordering::SeqCst)
        }

        fn unbind_count(&self) -> u32 {
            self.unbinds.load(Ordering::SeqCst)
        }
    }

    impl EngineBindable for LwMockBindable {
        fn bind_engine(&self, engine_dyn: Arc<dyn Any + Send + Sync>) {
            self.binds.fetch_add(1, Ordering::SeqCst);
            if self.retain_arc {
                let mut g = self.retained.lock().unwrap();
                *g = Some(engine_dyn);
            }
            // else: drop the Arc (bind compliant — the wrapper's
            // try_unwrap will succeed).
        }
        fn unbind_engine(&self) {
            self.unbinds.fetch_add(1, Ordering::SeqCst);
            let mut g = self.retained.lock().unwrap();
            *g = None;
        }
    }

    fn dummy_path_and_config() -> (std::path::PathBuf, EngineConfig) {
        (
            std::path::PathBuf::from("/dev/null/synthetic.gguf"),
            EngineConfig {
                tokenizer_path: None,
                config_path: None,
                queue_capacity: 32,
                warmup_synchronously: false,
                kv_metrics_sink: None,
                dwq_overlay_path: None,
                // ADR-040 Phase C iter-4 (C4) — test fixture uses
                // SerialFifo (ADR-005 byte-equivalent path).
                engine_mode: crate::serve::api::engine::EngineMode::SerialFifo,
            },
        )
    }

    // ===== Test 1 ==========================================================
    #[test]
    fn loader_wrapper_passes_through_load_when_no_registry_match() {
        // Spec test 4: no pending bind ⇒ wrapper transparently
        // returns the inner loader's engine; no registry call fires.
        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());
        let mock_hook = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            mock_hook.clone() as Arc<dyn EngineBindable>,
        );

        let wrapper: LoaderWrapper<TestEngine> =
            LoaderWrapper::new(inner.clone() as Arc<dyn ModelLoader<TestEngine>>, registry);
        let (path, cfg) = dummy_path_and_config();
        let engine = wrapper.load(&path, &cfg).expect("inner load OK");
        assert!(engine.marker >= 0xC1A1);
        assert_eq!(inner.load_count(), 1);
        // No pending bind ⇒ no bind fired.
        assert_eq!(mock_hook.bind_count(), 0);
    }

    // ===== Test 2 ==========================================================
    #[test]
    fn loader_wrapper_calls_bind_after_successful_load() {
        // Spec test 5: with a pending bind set + a registered hook,
        // the wrapper's load fires bind_engine on the hook and
        // returns the engine intact.
        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());
        let mock_hook = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            mock_hook.clone() as Arc<dyn EngineBindable>,
        );

        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner.clone() as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );
        wrapper.set_pending_bind("acme/m1".to_string(), QuantType::Q4_K_M);
        assert_eq!(
            wrapper.pending_bind(),
            Some(("acme/m1".to_string(), QuantType::Q4_K_M))
        );

        let (path, cfg) = dummy_path_and_config();
        let engine = wrapper.load(&path, &cfg).expect("inner load OK");
        assert!(engine.marker >= 0xC1A1);
        assert_eq!(inner.load_count(), 1);
        assert_eq!(mock_hook.bind_count(), 1, "bind_engine fired exactly once");
        // Pending slot consumed.
        assert_eq!(wrapper.pending_bind(), None);
    }

    // ===== Test 3 ==========================================================
    #[test]
    fn loader_wrapper_does_not_bind_on_load_failure() {
        // Spec test 6: when the inner loader returns Err, the
        // wrapper does NOT call bind_for (the engine doesn't exist).
        // The pending slot is left intact (operator may retry).
        let inner = MockLoader::new();
        inner.force_fail_next();
        let registry = Arc::new(KvPersistRegistry::new());
        let mock_hook = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            mock_hook.clone() as Arc<dyn EngineBindable>,
        );
        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner.clone() as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );
        wrapper.set_pending_bind("acme/m1".to_string(), QuantType::Q4_K_M);

        let (path, cfg) = dummy_path_and_config();
        let result = wrapper.load(&path, &cfg);
        assert!(result.is_err(), "inner failure propagates");
        assert_eq!(mock_hook.bind_count(), 0, "no bind on load failure");
        // Pending slot intact (operator may retry).
        assert_eq!(
            wrapper.pending_bind(),
            Some(("acme/m1".to_string(), QuantType::Q4_K_M))
        );
    }

    // ===== Test 4 ==========================================================
    #[test]
    fn loader_wrapper_drive_unbind_calls_registry_unbind() {
        // Spec test 7: explicit unbind through the wrapper drives
        // the registry's unbind_for, firing unbind_engine on the hook.
        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());
        let mock_hook = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            mock_hook.clone() as Arc<dyn EngineBindable>,
        );
        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );

        wrapper.drive_unbind("acme/m1", QuantType::Q4_K_M);
        assert_eq!(mock_hook.unbind_count(), 1);
        // No-op on unknown key.
        wrapper.drive_unbind("unknown/repo", QuantType::Q4_K_M);
        assert_eq!(mock_hook.unbind_count(), 1);
    }

    // ===== Test 5 ==========================================================
    #[test]
    fn loader_wrapper_clear_pending_bind_drops_slot() {
        // Defensive path: clear_pending_bind drops the slot without
        // a load_or_get. Subsequent load is a pass-through.
        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());
        let mock_hook = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            mock_hook.clone() as Arc<dyn EngineBindable>,
        );
        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );

        wrapper.set_pending_bind("acme/m1".to_string(), QuantType::Q4_K_M);
        wrapper.clear_pending_bind();
        assert_eq!(wrapper.pending_bind(), None);

        let (path, cfg) = dummy_path_and_config();
        let _engine = wrapper.load(&path, &cfg).expect("load");
        assert_eq!(mock_hook.bind_count(), 0, "no bind after clear");
    }

    // ===== Test 6 ==========================================================
    #[test]
    fn loader_wrapper_contract_violation_surfaces_error() {
        // When a hook retains a clone of the type-erased engine Arc
        // (violates the EngineBindable contract), the wrapper's
        // `Arc::try_unwrap` fails → wrapper returns Err. The error
        // mentions the (repo, quant) for operator-actionable reporting.
        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());
        let bad_hook = LwMockBindable::new_retaining();
        registry.register(
            "acme/bad".to_string(),
            QuantType::Q4_K_M,
            bad_hook.clone() as Arc<dyn EngineBindable>,
        );
        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );
        wrapper.set_pending_bind("acme/bad".to_string(), QuantType::Q4_K_M);

        let (path, cfg) = dummy_path_and_config();
        let result = wrapper.load(&path, &cfg);
        assert!(result.is_err(), "contract violation surfaces as Err");
        let msg = format!("{}", result.err().unwrap());
        assert!(msg.contains("acme/bad"), "error mentions repo: {msg}");
        // Bind fired before the error.
        assert_eq!(bad_hook.bind_count(), 1);

        // Cleanup: unbind clears the retained Arc so the test's
        // ref-count cycle doesn't leak.
        wrapper.drive_unbind("acme/bad", QuantType::Q4_K_M);
    }

    // ===== Test 7 (cmd_serve flag-on smoke) =================================
    #[test]
    fn cmd_serve_constructs_spiller_when_flag_on_smoke() {
        // Spec test 11 (bonus) — integration-style smoke that mirrors
        // the cmd_serve flag-on wire-up sequence. Rather than spinning
        // up a real HTTP server (out of scope for unit tests), this
        // test exercises the same sequence cmd_serve runs:
        //
        //   1. Build BlockPrefixCacheSpiller-equivalent substrate (we
        //      use the registry directly; the spiller's internal state
        //      isn't observed here — that's spiller.rs's tests' job).
        //   2. Register a stub family hook + bind it through both the
        //      KvPersistRegistry view and a parallel
        //      Arc<Mutex<dyn KvCacheSpill>> view (mirrors cmd_serve's
        //      spiller.register_family + registry.register call pair).
        //   3. Build a LoaderWrapper around a mock loader.
        //   4. Arm pending_bind, drive load, verify bind fires.
        //   5. Arm unbind, verify unbind fires.
        //
        // What this falsifies: a regression that decouples the
        // registry from the LoaderWrapper would break this — the
        // bind_count stays at zero. That's the Hypothesis-2 falsifier
        // for the C.1 ship gate.
        use crate::serve::kv_persist::{KvCacheSpill, StubGemma4Spill};
        use std::sync::Mutex;

        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());

        // Register a stub hook with an EngineBindable-aware mock so
        // we can observe the bind. Production cmd_serve registers
        // `Arc<StubGemma4Spill>` directly; here we use a recording
        // mock to preserve the round-trip assertion.
        let recorder = LwMockBindable::new();
        registry.register(
            "google/gemma-4".to_string(),
            QuantType::Q4_K_M,
            recorder.clone() as Arc<dyn EngineBindable>,
        );

        // Parallel KvCacheSpill registration shape (the spiller
        // doesn't fire here because we're not wiring the full
        // HotSwapManager — but constructing this Arc proves the
        // type plumbs through, mirroring cmd_serve's
        // `spiller.register_family(repo, quant, Arc::new(Mutex::new(StubGemma4Spill)))`
        // call).
        let _kv_hook: Arc<Mutex<dyn KvCacheSpill>> = Arc::new(Mutex::new(StubGemma4Spill));

        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner.clone() as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );

        // Production cmd_serve sequence:
        //   wrapper.set_pending_bind(repo, quant);
        //   pool.load_or_get(repo, quant, gguf_path, cfg);
        // We exercise the equivalent via wrapper.load directly.
        wrapper.set_pending_bind("google/gemma-4".to_string(), QuantType::Q4_K_M);
        let (path, cfg) = dummy_path_and_config();
        let _engine = wrapper.load(&path, &cfg).expect("load OK");
        assert_eq!(recorder.bind_count(), 1, "bind fired through registry");
        assert_eq!(inner.load_count(), 1);

        // Unbind path (exercised at evict in production).
        wrapper.drive_unbind("google/gemma-4", QuantType::Q4_K_M);
        assert_eq!(recorder.unbind_count(), 1, "unbind fired through registry");

        // Wire-up scoreboard — used as a regression breadcrumb.
        eprintln!(
            "[C.1 smoke] PASS — wrapper.load fired {} binds, drive_unbind fired {} unbinds",
            recorder.bind_count(),
            recorder.unbind_count()
        );
    }

    // ===== Phase B-dense.2 — substitute-on-load tests =======================

    /// Mock kv-side spill for use as the kv_hook half of the factory
    /// tuple. Records that it was substituted so tests can assert
    /// the spiller-side update fired.
    struct LwMockKvSpill {
        block_alignment_value: u32,
    }

    impl crate::serve::kv_persist::spiller::KvCacheSpill for LwMockKvSpill {
        fn block_alignment(&self) -> u32 {
            self.block_alignment_value
        }
        fn snapshot_block(
            &self,
            _layer_rank: usize,
            _range: std::ops::Range<u32>,
        ) -> Option<Vec<u8>> {
            None
        }
        fn restore_block(
            &mut self,
            _layer_rank: usize,
            _range: std::ops::Range<u32>,
            _payload: &[u8],
        ) -> std::result::Result<(), crate::serve::multi_model::SpillErrorKind> {
            Ok(())
        }
    }

    /// Synthetic engine type expected by the factory.
    #[derive(Debug)]
    struct LwExpectedHandle {
        _marker: u32,
    }

    /// Mock factory that succeeds when downcast to
    /// `Arc<LwExpectedHandle>` and produces an LwMockKvSpill +
    /// LwMockBindable tuple.
    struct LwMockFactory;

    impl crate::serve::kv_persist::registry::FamilyHookFactory for LwMockFactory {
        fn try_construct(
            &self,
            engine_dyn: Arc<dyn Any + Send + Sync>,
        ) -> Option<(
            Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>>,
            Arc<dyn EngineBindable>,
        )> {
            engine_dyn
                .downcast::<LwExpectedHandle>()
                .ok()
                .map(|_handle_arc| {
                    let kv: Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>> =
                        Arc::new(Mutex::new(LwMockKvSpill {
                            // 256 = BLOCK_TOKENS — a real Gemma4DenseSpill
                            // would also return this.
                            block_alignment_value: 256,
                        }));
                    let bindable: Arc<dyn EngineBindable> = LwMockBindable::new();
                    (kv, bindable)
                })
        }
    }

    /// Mock loader that returns an `LwExpectedHandle`-compatible
    /// type-erased engine. The wrapper's `Arc<E>` carries this type
    /// directly so the factory's downcast succeeds.
    struct LwMockLoaderHandle;

    impl ModelLoader<LwExpectedHandle> for LwMockLoaderHandle {
        fn load(&self, _path: &Path, _config: &EngineConfig) -> Result<LwExpectedHandle> {
            Ok(LwExpectedHandle { _marker: 0xCAFE })
        }
    }

    /// Spec test 9: with a registered factory + matching engine type,
    /// `LoaderWrapper::load` substitutes the spiller's hook (when a
    /// spiller is wired). Falsifier: the spiller's registered_count
    /// stays at 1 with the old hook, OR the spiller's lookup yields
    /// the old hook's block_alignment (not 256).
    #[test]
    fn loader_wrapper_substitutes_on_load_when_factory_matches() {
        use crate::serve::kv_persist::block_store::DiskBlockStore;
        use crate::serve::kv_persist::registry::FamilyHookFactory;
        use crate::serve::kv_persist::writer::AsyncWriterHandle;
        use crate::serve::kv_persist::{BlockPrefixCacheSpiller, DEFAULT_CHANNEL_CAPACITY};

        // Build a real spiller (cheap; tempdir-backed substrate).
        let tmp = std::env::temp_dir().join(format!(
            "hf2q-lw-substitute-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        ));
        std::fs::create_dir_all(&tmp).expect("mkdir tmp");
        let store = Arc::new(DiskBlockStore::new(tmp.clone(), 0).expect("DiskBlockStore"));
        let writer = Arc::new(AsyncWriterHandle::spawn(
            Arc::clone(&store),
            DEFAULT_CHANNEL_CAPACITY,
        ));
        let spiller: Arc<BlockPrefixCacheSpiller<LwExpectedHandle>> = Arc::new(
            BlockPrefixCacheSpiller::new(Arc::clone(&store), Arc::clone(&writer)),
        );

        // Pre-register a stub family hook so the spiller's count
        // starts at 1.
        struct OldStub;
        impl crate::serve::kv_persist::spiller::KvCacheSpill for OldStub {
            fn block_alignment(&self) -> u32 {
                999
            }
            fn snapshot_block(
                &self,
                _layer_rank: usize,
                _range: std::ops::Range<u32>,
            ) -> Option<Vec<u8>> {
                None
            }
            fn restore_block(
                &mut self,
                _layer_rank: usize,
                _range: std::ops::Range<u32>,
                _payload: &[u8],
            ) -> std::result::Result<(), crate::serve::multi_model::SpillErrorKind> {
                Ok(())
            }
        }
        let old_stub: Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>> =
            Arc::new(Mutex::new(OldStub));
        spiller.register_family("acme/m1".to_string(), QuantType::Q4_K_M, old_stub);
        assert_eq!(spiller.registered_count(), 1);

        let registry = Arc::new(KvPersistRegistry::new());
        // Pre-register the stub bindable too (the C.1 wire-up
        // contract) so the hooks map starts populated.
        let pre_stub_bindable = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            pre_stub_bindable.clone() as Arc<dyn EngineBindable>,
        );
        // Register the factory.
        let factory: Arc<dyn FamilyHookFactory> = Arc::new(LwMockFactory);
        registry.register_factory("acme/m1".to_string(), QuantType::Q4_K_M, factory);

        // Build the LoaderWrapper around an LwMockLoaderHandle so
        // load returns LwExpectedHandle (which the factory accepts).
        let inner: Arc<dyn ModelLoader<LwExpectedHandle>> = Arc::new(LwMockLoaderHandle);
        let wrapper: LoaderWrapper<LwExpectedHandle> =
            LoaderWrapper::new(inner, Arc::clone(&registry));
        wrapper.set_spiller(Arc::clone(&spiller));
        wrapper.set_pending_bind("acme/m1".to_string(), QuantType::Q4_K_M);

        let (path, cfg) = dummy_path_and_config();
        let _engine = wrapper.load(&path, &cfg).expect("load OK");

        // Spiller's hook for (acme/m1, Q4_K_M) is the SUBSTITUTED
        // LwMockKvSpill (block_alignment == 256), not OldStub
        // (block_alignment == 999). The spiller's count is still 1
        // (overwrite, not insert).
        assert_eq!(spiller.registered_count(), 1);

        // The factory-substituted bindable should override the C.1
        // stub. We can't directly inspect block_alignment without
        // calling pre_evict (which needs a LoadedEngine fixture), so
        // we assert via the registry: bind_for fires the SUBSTITUTED
        // bindable (a fresh LwMockBindable), NOT the original
        // pre_stub_bindable. The original stub's bind_count stays 0.
        assert_eq!(
            pre_stub_bindable.bind_count(),
            0,
            "factory substitution overwrites the registry's hook entry"
        );
    }

    /// Spec test 10: when the factory's `try_construct` returns None
    /// (engine type mismatch), the spiller's hook table is NOT
    /// modified. The original C.1 stub registration remains live.
    /// Falsifier: spiller.registered_count grows past 1 OR the
    /// spiller's hook for the key is replaced.
    #[test]
    fn loader_wrapper_does_not_substitute_on_factory_mismatch() {
        use crate::serve::kv_persist::block_store::DiskBlockStore;
        use crate::serve::kv_persist::registry::FamilyHookFactory;
        use crate::serve::kv_persist::writer::AsyncWriterHandle;
        use crate::serve::kv_persist::{BlockPrefixCacheSpiller, DEFAULT_CHANNEL_CAPACITY};

        let tmp = std::env::temp_dir().join(format!(
            "hf2q-lw-no-substitute-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        ));
        std::fs::create_dir_all(&tmp).expect("mkdir tmp");
        let store = Arc::new(DiskBlockStore::new(tmp.clone(), 0).expect("DiskBlockStore"));
        let writer = Arc::new(AsyncWriterHandle::spawn(
            Arc::clone(&store),
            DEFAULT_CHANNEL_CAPACITY,
        ));
        // Use the same `TestEngine` E type from the rest of this
        // test module. The wrapper will deliver Arc<TestEngine> to
        // the factory; LwMockFactory expects Arc<LwExpectedHandle>
        // → mismatch → None.
        let spiller: Arc<BlockPrefixCacheSpiller<TestEngine>> = Arc::new(
            BlockPrefixCacheSpiller::new(Arc::clone(&store), Arc::clone(&writer)),
        );

        struct InitialStub;
        impl crate::serve::kv_persist::spiller::KvCacheSpill for InitialStub {
            fn block_alignment(&self) -> u32 {
                111
            }
            fn snapshot_block(
                &self,
                _layer_rank: usize,
                _range: std::ops::Range<u32>,
            ) -> Option<Vec<u8>> {
                None
            }
            fn restore_block(
                &mut self,
                _layer_rank: usize,
                _range: std::ops::Range<u32>,
                _payload: &[u8],
            ) -> std::result::Result<(), crate::serve::multi_model::SpillErrorKind> {
                Ok(())
            }
        }
        let initial_stub: Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>> =
            Arc::new(Mutex::new(InitialStub));
        spiller.register_family("acme/m1".to_string(), QuantType::Q4_K_M, initial_stub);

        let registry = Arc::new(KvPersistRegistry::new());
        let stub_bindable = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            stub_bindable.clone() as Arc<dyn EngineBindable>,
        );
        // Register the factory — but the loader returns TestEngine
        // (not LwExpectedHandle), so try_construct will None-out.
        let factory: Arc<dyn FamilyHookFactory> = Arc::new(LwMockFactory);
        registry.register_factory("acme/m1".to_string(), QuantType::Q4_K_M, factory);

        let inner = MockLoader::new();
        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner.clone() as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );
        wrapper.set_spiller(Arc::clone(&spiller));
        wrapper.set_pending_bind("acme/m1".to_string(), QuantType::Q4_K_M);

        let (path, cfg) = dummy_path_and_config();
        let _engine = wrapper.load(&path, &cfg).expect("load OK");

        // No substitution — spiller's count is still 1.
        assert_eq!(spiller.registered_count(), 1);
        // The C.1 stub bindable still received the bind_for fallout
        // (every load fires bind_for unconditionally per the design
        // doc above).
        assert_eq!(
            stub_bindable.bind_count(),
            1,
            "C.1 stub still fires bind_for on the auto-Arc<E> mismatched path"
        );
    }

    // ===== P1-1 regression =================================================

    /// P1-1 regression — adversarial review §P1-1 2026-05-01:
    /// `try_substitute_on_load` + `update_spiller_registration` must
    /// hold a single critical section so concurrent `pre_evict` /
    /// `post_admit` observers never see registry-real ↔ spiller-stub
    /// divergence. Pre-fix: the registry's `hooks` write-lock and the
    /// spiller's `registrations` write-lock were independent; a
    /// reader could observe the registry as substituted while the
    /// spiller still held the C.1 stub for ~µs.
    ///
    /// Verification strategy:
    ///   (1) Capture the kv_hook `Arc` produced by the factory in a
    ///       shared probe `Mutex<Option<Arc<...>>>`. The same Arc
    ///       must be observable in the spiller's registration
    ///       AFTER `wrapper.load` returns.
    ///   (2) `Arc::strong_count` on the probe Arc must reflect TWO
    ///       owners (the factory's captured clone + the spiller's
    ///       registered clone) — proving the spiller actually got
    ///       the substituted hook, not a stale stub.
    ///   (3) Spawn N concurrent readers polling
    ///       `spiller.registered_count()` while the load is in
    ///       flight; their observed count must always be 1
    ///       (pre-stub OR substituted, never zero, never split into
    ///       2). This is a probabilistic falsifier — the µs window
    ///       is small but the assertion is loud if it ever flips.
    #[test]
    fn p1_1_concurrent_substitute_does_not_split_registry_and_spiller_state() {
        use crate::serve::kv_persist::block_store::DiskBlockStore;
        use crate::serve::kv_persist::registry::FamilyHookFactory;
        use crate::serve::kv_persist::writer::AsyncWriterHandle;
        use crate::serve::kv_persist::{BlockPrefixCacheSpiller, DEFAULT_CHANNEL_CAPACITY};
        use std::sync::atomic::{AtomicBool, Ordering};

        // ---- fixture ----
        let tmp = std::env::temp_dir().join(format!(
            "hf2q-lw-p1-1-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        ));
        std::fs::create_dir_all(&tmp).expect("mkdir tmp");
        let store = Arc::new(DiskBlockStore::new(tmp.clone(), 0).expect("DiskBlockStore"));
        let writer = Arc::new(AsyncWriterHandle::spawn(
            Arc::clone(&store),
            DEFAULT_CHANNEL_CAPACITY,
        ));
        let spiller: Arc<BlockPrefixCacheSpiller<LwExpectedHandle>> = Arc::new(
            BlockPrefixCacheSpiller::new(Arc::clone(&store), Arc::clone(&writer)),
        );

        // Pre-register the C.1 stub so the spiller's count starts at 1
        // (matches production: cmd_serve registers the stub before any
        // substitute-on-load can fire).
        struct PreStub;
        impl crate::serve::kv_persist::spiller::KvCacheSpill for PreStub {
            fn block_alignment(&self) -> u32 {
                111
            }
            fn snapshot_block(&self, _: usize, _: std::ops::Range<u32>) -> Option<Vec<u8>> {
                None
            }
            fn restore_block(
                &mut self,
                _: usize,
                _: std::ops::Range<u32>,
                _: &[u8],
            ) -> std::result::Result<(), crate::serve::multi_model::SpillErrorKind> {
                Ok(())
            }
        }
        let pre_stub: Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>> =
            Arc::new(Mutex::new(PreStub));
        spiller.register_family("acme/m1".to_string(), QuantType::Q4_K_M, pre_stub);
        assert_eq!(spiller.registered_count(), 1);

        // Probe captures the kv_hook Arc the factory produces so the
        // test can assert ptr_eq against the spiller-side registration.
        let probe: Arc<
            Mutex<Option<Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>>>>,
        > = Arc::new(Mutex::new(None));

        struct ProbingFactory {
            probe:
                Arc<Mutex<Option<Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>>>>>,
        }
        impl FamilyHookFactory for ProbingFactory {
            fn try_construct(
                &self,
                engine_dyn: Arc<dyn Any + Send + Sync>,
            ) -> Option<(
                Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>>,
                Arc<dyn EngineBindable>,
            )> {
                engine_dyn
                    .downcast::<LwExpectedHandle>()
                    .ok()
                    .map(|_handle_arc| {
                        let kv: Arc<Mutex<dyn crate::serve::kv_persist::spiller::KvCacheSpill>> =
                            Arc::new(Mutex::new(LwMockKvSpill {
                                block_alignment_value: 256,
                            }));
                        // Capture a clone of the kv Arc for the
                        // post-condition ptr_eq assertion.
                        {
                            let mut g = self.probe.lock().expect("probe poisoned");
                            *g = Some(Arc::clone(&kv));
                        }
                        let bindable: Arc<dyn EngineBindable> = LwMockBindable::new();
                        (kv, bindable)
                    })
            }
        }

        let registry = Arc::new(KvPersistRegistry::new());
        let pre_stub_bindable = LwMockBindable::new();
        registry.register(
            "acme/m1".to_string(),
            QuantType::Q4_K_M,
            pre_stub_bindable.clone() as Arc<dyn EngineBindable>,
        );
        let factory: Arc<dyn FamilyHookFactory> = Arc::new(ProbingFactory {
            probe: Arc::clone(&probe),
        });
        registry.register_factory("acme/m1".to_string(), QuantType::Q4_K_M, factory);

        let inner: Arc<dyn ModelLoader<LwExpectedHandle>> = Arc::new(LwMockLoaderHandle);
        let wrapper: Arc<LoaderWrapper<LwExpectedHandle>> =
            Arc::new(LoaderWrapper::new(inner, Arc::clone(&registry)));
        wrapper.set_spiller(Arc::clone(&spiller));
        wrapper.set_pending_bind("acme/m1".to_string(), QuantType::Q4_K_M);

        // ---- concurrent reader probes ----
        // Spawn N readers that watch `spiller.registered_count()`
        // while load is in flight. The count must always be exactly 1
        // (pre-stub OR substituted; never 0 and never 2). Pre-fix the
        // count is still 1 because both ops insert/overwrite into the
        // same key — so this assertion is the WEAK signal. The STRONG
        // signal is the post-condition Arc::ptr_eq below; this stress
        // loop just confirms no deadlock / panic was introduced.
        let stop = Arc::new(AtomicBool::new(false));
        let mut readers = Vec::new();
        for _ in 0..4 {
            let s = Arc::clone(&spiller);
            let stop_c = Arc::clone(&stop);
            readers.push(std::thread::spawn(move || {
                let mut observed = Vec::new();
                while !stop_c.load(Ordering::Relaxed) {
                    observed.push(s.registered_count());
                }
                observed
            }));
        }

        // ---- drive the substitution ----
        let (path, cfg) = dummy_path_and_config();
        let _engine = wrapper.load(&path, &cfg).expect("load OK");

        stop.store(true, Ordering::Relaxed);
        for r in readers {
            let observed = r.join().expect("reader thread");
            for c in observed {
                assert!(
                    c == 1,
                    "P1-1 falsifier: spiller.registered_count must \
                     remain 1 across substitute-on-load (saw {c})"
                );
            }
        }

        // ---- post-condition: registry + spiller agree ----

        // (1) Spiller still has exactly 1 family registered
        //     (overwrite, not insert).
        assert_eq!(spiller.registered_count(), 1);

        // (2) The pre-stub bindable did NOT receive bind_for (the
        //     factory substitution overwrote the registry hook
        //     before bind_for fired).
        assert_eq!(
            pre_stub_bindable.bind_count(),
            0,
            "registry hook was substituted before bind_for fired"
        );

        // (3) The factory was invoked and captured a kv_hook Arc.
        let probed = probe.lock().expect("probe poisoned").take();
        let probed = probed.expect(
            "P1-1 fixture: factory must have produced a kv_hook (else \
             substitution didn't fire and the test is decorative)",
        );

        // (4) The factory-produced Arc is now held by AT LEAST the
        //     spiller (it was passed into register_family). Pre-fix
        //     this would still hold post-load (the bug was a
        //     transient mid-load divergence, not a final-state
        //     divergence) — but combined with the lock-discipline
        //     assertion above (substitute_lock guards both writes),
        //     the post-condition + the absence of any 0-count
        //     reader observation together attest the atomic
        //     handover. Strong-count >= 2: probe + spiller's
        //     internal HashMap.
        assert!(
            Arc::strong_count(&probed) >= 2,
            "P1-1 post-condition: factory-produced kv_hook must be \
             retained by the spiller's registration (probe + spiller \
             expected; got strong_count={})",
            Arc::strong_count(&probed)
        );

        // ---- cleanup ----
        // Drop spiller + writer so the AsyncWriterHandle thread exits
        // cleanly and the temp dir can be removed.
        drop(wrapper);
        drop(spiller);
        drop(writer);
        drop(store);
        let _ = std::fs::remove_dir_all(&tmp);
    }

    // ===== Test 8 ==========================================================
    #[test]
    fn loader_wrapper_set_pending_bind_overwrites_prior() {
        // Calling set_pending_bind twice without an intervening load
        // overwrites — the second pair is what fires.
        let inner = MockLoader::new();
        let registry = Arc::new(KvPersistRegistry::new());
        let mock_a = LwMockBindable::new();
        let mock_b = LwMockBindable::new();
        registry.register(
            "acme/a".to_string(),
            QuantType::Q4_K_M,
            mock_a.clone() as Arc<dyn EngineBindable>,
        );
        registry.register(
            "acme/b".to_string(),
            QuantType::Q4_K_M,
            mock_b.clone() as Arc<dyn EngineBindable>,
        );
        let wrapper: LoaderWrapper<TestEngine> = LoaderWrapper::new(
            inner as Arc<dyn ModelLoader<TestEngine>>,
            Arc::clone(&registry),
        );

        wrapper.set_pending_bind("acme/a".to_string(), QuantType::Q4_K_M);
        wrapper.set_pending_bind("acme/b".to_string(), QuantType::Q4_K_M);

        let (path, cfg) = dummy_path_and_config();
        let _engine = wrapper.load(&path, &cfg).expect("load");
        assert_eq!(mock_a.bind_count(), 0);
        assert_eq!(mock_b.bind_count(), 1);
    }
}