File size: 48,973 Bytes
3374e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
use crate::config::EngineConfig;
use crate::host_state::HostState;
use crate::http_service::HttpHostService;
use crate::registry::PluginRegistry;
use bex_db::BexDb;
use bex_types::plugin_info::PluginInfo;
use bex_types::{BexError, Manifest};
use parking_lot::RwLock;
use std::path::Path;
use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
use std::sync::Arc;
use wasmtime::component::{Component, Linker};
use wasmtime::{Engine as WasmtimeEngine, ResourceLimiter, Store};

// ── Bindgen: generate typed bindings from WIT ──────────────────────
wasmtime::component::bindgen!({
    path: "../../wit",
    world: "plugin",
});

// Re-export bindgen types for convenience
pub use crate::engine::bex::plugin::common::*;
pub use crate::engine::bex::plugin::http;
pub use crate::engine::bex::plugin::kv;
pub use crate::engine::bex::plugin::secrets;
pub use crate::engine::bex::plugin::log;
pub use crate::engine::bex::plugin::clock;
pub use crate::engine::bex::plugin::rng;
pub use crate::engine::bex::plugin::js;

// ── Engine State Machine ────────────────────────────────────────────

const STATE_NOT_READY: u8 = 0;
const STATE_READY: u8 = 1;
const STATE_DRAINING: u8 = 2;
const STATE_STOPPED: u8 = 3;

// ── Compile Cache ───────────────────────────────────────────────────

/// HMAC-authenticated compile cache. Stores compiled `.cwasm` files
/// on disk with an HMAC-SHA256 tag to detect tampering or stale cache.
struct CompileCache {
    cache_dir: std::path::PathBuf,
    key_material: Vec<u8>,
}

impl CompileCache {
    fn new(cache_dir: std::path::PathBuf) -> Result<Self, BexError> {
        std::fs::create_dir_all(&cache_dir)
            .map_err(|e| BexError::Internal(format!("cache dir: {e}")))?;

        // Derive HMAC key from a stable identifier
        let key_material = b"bex-engine-compile-cache-v2-key".to_vec();

        Ok(Self {
            cache_dir,
            key_material,
        })
    }

    /// Compute the cache file path for a given WASM hash
    fn cache_path(&self, wasm_hash: &[u8]) -> std::path::PathBuf {
        let hex_hash = hex::encode(wasm_hash);
        self.cache_dir.join(format!("{}.cwasm", hex_hash))
    }

    /// Compute HMAC tag for data
    fn compute_tag(&self, data: &[u8]) -> Vec<u8> {
        use hmac::{Hmac, Mac};
        type HmacSha256 = Hmac<sha2::Sha256>;
        let mut mac = HmacSha256::new_from_slice(&self.key_material)
            .expect("HMAC can take key of any size");
        mac.update(data);
        mac.finalize().into_bytes().to_vec()
    }

    /// Try to load a cached compiled component.
    /// Returns None if cache miss or if HMAC validation fails.
    fn load(&self, wasm_bytes: &[u8]) -> Option<Vec<u8>> {
        let wasm_hash = sha2_hash(wasm_bytes);
        let path = self.cache_path(&wasm_hash);
        let cached = std::fs::read(&path).ok()?;

        // Format: [32 bytes HMAC tag][rest is serialized component]
        if cached.len() < 32 {
            return None;
        }
        let (stored_tag, component_data) = cached.split_at(32);
        let expected_tag = self.compute_tag(component_data);

        // Constant-time comparison to prevent timing attacks
        if stored_tag.len() != expected_tag.len() {
            return None;
        }
        let mut diff = 0u8;
        for (a, b) in stored_tag.iter().zip(expected_tag.iter()) {
            diff |= a ^ b;
        }
        if diff != 0 {
            tracing::warn!("Compile cache HMAC mismatch, ignoring cached file");
            return None;
        }

        Some(component_data.to_vec())
    }

    /// Store a compiled component in the cache.
    fn store(&self, wasm_bytes: &[u8], component_data: &[u8]) {
        let wasm_hash = sha2_hash(wasm_bytes);
        let path = self.cache_path(&wasm_hash);
        let tag = self.compute_tag(component_data);

        let mut out = Vec::with_capacity(32 + component_data.len());
        out.extend_from_slice(&tag);
        out.extend_from_slice(component_data);

        if let Err(e) = std::fs::write(&path, &out) {
            tracing::warn!("Failed to write compile cache: {}", e);
        }
    }
}

fn sha2_hash(data: &[u8]) -> Vec<u8> {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(data);
    hasher.finalize().to_vec()
}

/// Compile a WASM component, using the compile cache if available.
fn compile_or_cache(
    wasmtime: &WasmtimeEngine,
    wasm_bytes: &[u8],
    cache: &CompileCache,
) -> Result<Component, BexError> {
    // Try cache first
    if let Some(cached_data) = cache.load(wasm_bytes) {
        // Deserialize from cached compiled module (unsafe is OK here because
        // we trust our own cache files which are HMAC-authenticated)
        if let Ok(component) = unsafe { Component::deserialize(wasmtime, &cached_data) } {
            tracing::debug!("Compile cache hit");
            return Ok(component);
        }
        // If deserialization fails, fall through to recompile
        tracing::warn!("Compile cache deserialize failed, recompiling");
    }

    // Compile from source
    let component = Component::new(wasmtime, wasm_bytes)
        .map_err(|e| BexError::Internal(format!("compile wasm: {e}")))?;

    // Store in cache for future use
    if let Ok(serialized) = component.serialize() {
        cache.store(wasm_bytes, &serialized);
    }

    Ok(component)
}

// ── Epoch Ticker ────────────────────────────────────────────────────

/// Background thread that increments the Wasmtime epoch at regular intervals.
/// This enables wall-clock timeout enforcement via `store.set_epoch_deadline()`.
struct EpochTicker {
    _handle: Option<std::thread::JoinHandle<()>>,
    shutdown: Arc<std::sync::atomic::AtomicBool>,
}

impl EpochTicker {
    fn start(engine: WasmtimeEngine, interval_ms: u64) -> Self {
        let shutdown = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let shutdown_clone = shutdown.clone();
        let eng = engine.clone();

        let handle = std::thread::Builder::new()
            .name("bex-epoch-ticker".to_string())
            .spawn(move || {
                let interval = std::time::Duration::from_millis(interval_ms);
                while !shutdown_clone.load(Ordering::Relaxed) {
                    std::thread::sleep(interval);
                    eng.increment_epoch();
                }
            })
            .expect("failed to spawn epoch ticker thread");

        Self {
            _handle: Some(handle),
            shutdown,
        }
    }
}

impl Drop for EpochTicker {
    fn drop(&mut self) {
        self.shutdown.store(true, Ordering::Relaxed);
    }
}

// ── Engine ─────────────────────────────────────────────────────────

struct EngineInner {
    /// Kept alive via Arc β€” cloned into each HostState for block_on() access.
    /// Field is read via `self.inner.runtime.clone()` in instantiate().
    #[allow(dead_code)] // Suppress false positive: read through Arc clone in HostState::new()
    runtime: Arc<tokio::runtime::Runtime>,
    wasmtime: WasmtimeEngine,
    linker: Linker<HostState>,
    db: Arc<BexDb>,
    http: Arc<HttpHostService>,
    js_pool: Arc<bex_js::JsPool>,
    config: Arc<EngineConfig>,
    registry: RwLock<PluginRegistry>,
    compile_cache: CompileCache,
    state: AtomicU8,
    active_calls: AtomicUsize,
    start_time: std::time::Instant,
    _epoch_ticker: EpochTicker,
    _lock_file: std::fs::File,
}

#[derive(Clone)]
pub struct Engine {
    inner: Arc<EngineInner>,
}

impl Engine {
    pub fn new(config: EngineConfig) -> Result<Self, BexError> {
        // Create data directory
        std::fs::create_dir_all(&config.data_dir)
            .map_err(|e| BexError::Internal(format!("data dir: {e}")))?;

        // Acquire advisory file lock on data directory (fixes Issue #20)
        let lock_path = config.data_dir.join(".bex.lock");
        let lock_file = std::fs::OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(&lock_path)
            .map_err(|e| BexError::Internal(format!("lock file: {e}")))?;

        use fs4::fs_std::FileExt;
        lock_file
            .try_lock_exclusive()
            .map_err(|e| {
                BexError::Internal(format!(
                    "Another BEX engine instance is using this data directory: {e}"
                ))
            })?;

        // Configure Wasmtime with epoch interruption (fixes Problem #3)
        let mut wt_config = wasmtime::Config::new();
        wt_config.wasm_component_model(true);
        wt_config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
        wt_config.consume_fuel(true);
        wt_config.epoch_interruption(true);
        wt_config.max_wasm_stack(4 * 1024 * 1024);

        let wasmtime = WasmtimeEngine::new(&wt_config)
            .map_err(|e| BexError::Internal(format!("wasmtime init: {e}")))?;

        // Create ONE shared tokio runtime (fixes Problem #1)
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(4)
            .enable_all()
            .build()
            .map_err(|e| BexError::Internal(format!("tokio runtime: {e}")))?;

        let runtime = Arc::new(runtime);

        // Open database
        let db = Arc::new(
            BexDb::open(&config.data_dir).map_err(|e| BexError::Storage(e.to_string()))?,
        );

        // Create HTTP service with proper pool config (fixes Issues #16, #17)
        let http = Arc::new(HttpHostService::new(
            &config.user_agent,
            config.http_timeout_ms,
            config.http_pool_idle_timeout_ms,
            config.http_pool_max_idle_per_host,
        ));

        // Build linker ONCE (fixes Problem #2)
        let linker = Self::build_linker(&wasmtime)?;

        // Start epoch ticker thread
        let epoch_ticker = EpochTicker::start(wasmtime.clone(), config.epoch_interval_ms);

        // Compile cache directory
        let cache_dir = config.data_dir.join("cache/wasm");
        let compile_cache = CompileCache::new(cache_dir)?;

        // Create JS worker pool (QuickJS integration)
        // Wire JsPoolConfig from EngineConfig so users can control JS pool settings
        let js_pool_config = bex_js::JsPoolConfig {
            initial_workers: config.js_initial_workers,
            max_workers: config.js_max_workers,
            memory_limit_bytes: config.js_memory_limit_mb as usize * 1024 * 1024,
            default_timeout_ms: config.js_timeout_ms,
            context_idle_ttl_secs: config.js_context_idle_ttl_secs,
            worker_idle_ttl_secs: config.js_worker_idle_ttl_secs,
            max_stack_bytes: config.js_max_stack_bytes,
        };
        let js_pool = Arc::new(
            bex_js::JsPool::new(js_pool_config)
                .map_err(|e| BexError::Internal(format!("JS pool: {e}")))?,
        );

        // Plugins directory for on-disk WASM storage (fixes Problem #4)
        let plugins_dir = config.data_dir.join("plugins/installed");
        std::fs::create_dir_all(&plugins_dir)
            .map_err(|e| BexError::Internal(format!("plugins dir: {e}")))?;

        let config_arc = Arc::new(config);

        let inner = Arc::new(EngineInner {
            runtime,
            wasmtime,
            linker,
            db,
            http,
            js_pool,
            config: config_arc,
            registry: RwLock::new(PluginRegistry::new()),
            compile_cache,
            state: AtomicU8::new(STATE_NOT_READY),
            active_calls: AtomicUsize::new(0),
            start_time: std::time::Instant::now(),
            _epoch_ticker: epoch_ticker,
            _lock_file: lock_file,
        });

        let engine = Self { inner };

        // Reload previously installed plugins from database
        engine.reload_from_db()?;

        // Mark engine as ready
        engine.inner.state.store(STATE_READY, Ordering::Release);

        Ok(engine)
    }

    /// Build the linker once. This includes locked-down WASI and all BEX host interfaces.
    fn build_linker(wasmtime: &WasmtimeEngine) -> Result<Linker<HostState>, BexError> {
        let mut linker = Linker::<HostState>::new(wasmtime);

        // Add WASI support with locked-down context (fixes Problem #6)
        // We provide the WASI imports but HostState creates a WasiCtx with
        // no inherited handles, no filesystem, no env, no sockets.
        wasmtime_wasi::add_to_linker_sync(&mut linker)
            .map_err(|e| BexError::Internal(format!("wasi linker: {e}")))?;

        // Add BEX host interfaces
        Plugin::add_to_linker(&mut linker, |state: &mut HostState| state)
            .map_err(|e| BexError::Internal(format!("linker setup: {e}")))?;

        Ok(linker)
    }

    /// Check engine state before accepting calls
    fn check_ready(&self) -> Result<(), BexError> {
        match self.inner.state.load(Ordering::Acquire) {
            STATE_READY => Ok(()),
            STATE_NOT_READY => Err(BexError::NotReady),
            STATE_DRAINING => Err(BexError::Internal("engine is draining".into())),
            STATE_STOPPED => Err(BexError::Internal("engine is stopped".into())),
            _ => Err(BexError::Internal("invalid engine state".into())),
        }
    }

    /// Reload all previously installed plugins from the database.
    fn reload_from_db(&self) -> Result<(), BexError> {
        let plugins = self
            .inner
            .db
            .list_plugins()
            .map_err(|e| BexError::Storage(e.to_string()))?;

        for info in plugins {
            // Get WASM blob
            let wasm_bytes = match self.inner.db.get_wasm_blob(&info.id) {
                Ok(Some(bytes)) => bytes,
                _ => continue,
            };

            // Get manifest
            let manifest = match self.inner.db.get_manifest(&info.id) {
                Ok(Some(yaml)) => match serde_yaml::from_str::<Manifest>(&yaml) {
                    Ok(m) => m,
                    _ => continue,
                },
                _ => continue,
            };

            // Compile with cache (fixes Problem #5)
            let component = match compile_or_cache(
                &self.inner.wasmtime,
                &wasm_bytes,
                &self.inner.compile_cache,
            ) {
                Ok(c) => Arc::new(c),
                _ => continue,
            };

            // Store WASM on disk for future recompilation (fixes Problem #4)
            let wasm_path = self.inner.config.data_dir.join(format!(
                "plugins/installed/{}/plugin.wasm",
                info.id.replace('.', "_")
            ));
            if let Some(parent) = wasm_path.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            let _ = std::fs::write(&wasm_path, &wasm_bytes);

            let record = {
                let mut r = crate::registry::PluginRecord::new(
                    manifest,
                    component,
                    self.inner.config.circuit_breaker_threshold,
                    self.inner.config.circuit_breaker_cooldown_ms,
                );
                r.enabled = info.enabled;
                r
            };
            self.inner.registry.write().insert(Arc::new(record));
        }

        Ok(())
    }

    // ── Plugin Management ───────────────────────────────────────────

    pub fn install_plugin(&self, path: &Path) -> Result<PluginInfo, BexError> {
        self.check_ready()?;
        let data = std::fs::read(path).map_err(|e| BexError::Internal(format!("read: {e}")))?;
        self.install_bytes(&data)
    }

    /// Install a plugin from raw bytes (fixes Issue #13).
    pub fn install_bytes(&self, data: &[u8]) -> Result<PluginInfo, BexError> {
        self.check_ready()?;

        let package = bex_pkg::unpack(data)?;
        package
            .manifest
            .validate(&self.inner.config.host_version)?;

        // Compile with cache
        let component = compile_or_cache(
            &self.inner.wasmtime,
            &package.wasm,
            &self.inner.compile_cache,
        )?;
        let component = Arc::new(component);

        let id = package.manifest.id.clone();

        // Store WASM on disk (fixes Problem #4: don't keep WASM in memory)
        let wasm_path = self.inner.config.data_dir.join(format!(
            "plugins/installed/{}/plugin.wasm",
            id.replace('.', "_")
        ));
        if let Some(parent) = wasm_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let _ = std::fs::write(&wasm_path, &package.wasm);

        // Persist WASM blob and manifest to database
        self.inner
            .db
            .save_wasm_blob(&id, &package.wasm)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        let manifest_yaml = serde_yaml::to_string(&package.manifest)
            .map_err(|e| BexError::Internal(e.to_string()))?;
        self.inner
            .db
            .save_manifest(&id, &manifest_yaml)
            .map_err(|e| BexError::Storage(e.to_string()))?;

        let record = Arc::new(crate::registry::PluginRecord::new(
            package.manifest,
            component,
            self.inner.config.circuit_breaker_threshold,
            self.inner.config.circuit_breaker_cooldown_ms,
        ));
        let info = record.to_plugin_info();
        self.inner
            .db
            .save_plugin_info(&info)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        self.inner.registry.write().insert(record);

        Ok(info)
    }

    /// Install a plugin directly from WASM bytes and a manifest (no package format).
    pub fn install_plugin_raw(
        &self,
        manifest: Manifest,
        wasm_bytes: Vec<u8>,
    ) -> Result<PluginInfo, BexError> {
        self.check_ready()?;
        manifest.validate(&self.inner.config.host_version)?;

        let component = compile_or_cache(
            &self.inner.wasmtime,
            &wasm_bytes,
            &self.inner.compile_cache,
        )?;
        let component = Arc::new(component);

        let id = manifest.id.clone();

        // Store WASM on disk
        let wasm_path = self.inner.config.data_dir.join(format!(
            "plugins/installed/{}/plugin.wasm",
            id.replace('.', "_")
        ));
        if let Some(parent) = wasm_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let _ = std::fs::write(&wasm_path, &wasm_bytes);

        // Persist
        self.inner
            .db
            .save_wasm_blob(&id, &wasm_bytes)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        let manifest_yaml = serde_yaml::to_string(&manifest)
            .map_err(|e| BexError::Internal(e.to_string()))?;
        self.inner
            .db
            .save_manifest(&id, &manifest_yaml)
            .map_err(|e| BexError::Storage(e.to_string()))?;

        let record = Arc::new(crate::registry::PluginRecord::new(
            manifest,
            component,
            self.inner.config.circuit_breaker_threshold,
            self.inner.config.circuit_breaker_cooldown_ms,
        ));
        let info = record.to_plugin_info();
        self.inner
            .db
            .save_plugin_info(&info)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        self.inner.registry.write().insert(record);

        Ok(info)
    }

    pub fn uninstall_plugin(&self, id: &str) -> Result<(), BexError> {
        self.check_ready()?;

        // Wait for active calls to drain (fixes Issue #9)
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while self.inner.active_calls.load(Ordering::Acquire) > 0 {
            if std::time::Instant::now() > deadline {
                return Err(BexError::Internal(
                    "timeout waiting for active calls to drain".into(),
                ));
            }
            std::thread::sleep(std::time::Duration::from_millis(50));
        }

        // Remove WASM from disk
        let wasm_path = self.inner.config.data_dir.join(format!(
            "plugins/installed/{}/plugin.wasm",
            id.replace('.', "_")
        ));
        let _ = std::fs::remove_file(&wasm_path);
        // Try to remove the directory too if empty
        if let Some(parent) = wasm_path.parent() {
            let _ = std::fs::remove_dir(parent);
        }

        // Evict plugin's JS context from the pool
        self.inner.js_pool.evict_plugin(id);

        self.inner.registry.write().remove(id);
        self.inner
            .db
            .remove_plugin(id)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        Ok(())
    }

    pub fn list_plugins(&self) -> Vec<PluginInfo> {
        self.inner.registry.read().list()
    }

    pub fn get_plugin_info(&self, id: &str) -> Option<PluginInfo> {
        self.inner.registry.read().get(id).map(|r| r.to_plugin_info())
    }

    pub fn enable_plugin(&self, id: &str) -> Result<(), BexError> {
        self.check_ready()?;
        let mut reg = self.inner.registry.write();
        let old = reg
            .remove(id)
            .ok_or_else(|| BexError::PluginNotFound(id.into()))?;

        // Create new record with enabled=true
        let new_record = Arc::new(crate::registry::PluginRecord {
            id: old.id.clone(),
            manifest: old.manifest.clone(),
            capabilities: old.capabilities,
            enabled: true,
            installed_at: old.installed_at,
            component: old.component.clone(),
            health: crate::registry::PluginHealth::new(
                self.inner.config.circuit_breaker_threshold,
                self.inner.config.circuit_breaker_cooldown_ms,
            ),
        });
        let info = new_record.to_plugin_info();
        reg.insert(new_record);
        drop(reg);

        self.inner
            .db
            .save_plugin_info(&info)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        Ok(())
    }

    pub fn disable_plugin(&self, id: &str) -> Result<(), BexError> {
        self.check_ready()?;
        let mut reg = self.inner.registry.write();
        let old = reg
            .remove(id)
            .ok_or_else(|| BexError::PluginNotFound(id.into()))?;

        let new_record = Arc::new(crate::registry::PluginRecord {
            id: old.id.clone(),
            manifest: old.manifest.clone(),
            capabilities: old.capabilities,
            enabled: false,
            installed_at: old.installed_at,
            component: old.component.clone(),
            health: crate::registry::PluginHealth::new(
                self.inner.config.circuit_breaker_threshold,
                self.inner.config.circuit_breaker_cooldown_ms,
            ),
        });
        let info = new_record.to_plugin_info();
        reg.insert(new_record);
        drop(reg);

        self.inner
            .db
            .save_plugin_info(&info)
            .map_err(|e| BexError::Storage(e.to_string()))?;
        Ok(())
    }

    // ── Secret / API Key Management ────────────────────────────────

    /// Set a secret/API key for a plugin.
    pub fn secret_set(&self, plugin_id: &str, key: &str, value: &str) -> Result<(), BexError> {
        self.inner
            .db
            .secret_set(plugin_id, key, value)
            .map_err(|e| BexError::Storage(e.to_string()))
    }

    /// Get a secret/API key for a plugin.
    pub fn secret_get(&self, plugin_id: &str, key: &str) -> Result<Option<String>, BexError> {
        self.inner
            .db
            .secret_get(plugin_id, key)
            .map_err(|e| BexError::Storage(e.to_string()))
    }

    /// Delete a secret/API key for a plugin.
    pub fn secret_remove(&self, plugin_id: &str, key: &str) -> Result<bool, BexError> {
        self.inner
            .db
            .secret_remove(plugin_id, key)
            .map_err(|e| BexError::Storage(e.to_string()))
    }

    /// List all secret keys for a plugin.
    pub fn secret_keys(&self, plugin_id: &str) -> Result<Vec<String>, BexError> {
        self.inner
            .db
            .secret_keys(plugin_id, "")
            .map_err(|e| BexError::Storage(e.to_string()))
    }

    // ── Instantiation with epoch deadline ───────────────────────────

    fn instantiate(&self, plugin_id: &str) -> Result<(Plugin, Store<HostState>), BexError> {
        self.check_ready()?;

        let record = self
            .inner
            .registry
            .read()
            .get(plugin_id)
            .cloned()
            .ok_or_else(|| BexError::PluginNotFound(plugin_id.into()))?;

        if !record.enabled {
            return Err(BexError::PluginDisabled(plugin_id.into()));
        }

        // Circuit breaker check
        if !record.health.is_available() {
            return Err(BexError::PluginFault(format!(
                "plugin '{}' is circuit-broken (too many consecutive failures)",
                plugin_id
            )));
        }

        let component = record.component.clone();

        // Create store
        let mut store = Store::new(
            &self.inner.wasmtime,
            HostState::new(
                self.inner.http.clone(),
                self.inner.db.clone(),
                self.inner.js_pool.clone(),
                self.inner.runtime.clone(),
                plugin_id.to_string(),
                record.manifest.clone(),
                Arc::from(self.inner.config.user_agent.as_str()),
                self.inner.config.http_timeout_ms,
                self.inner.config.max_response_bytes,
                self.inner.config.memory_limit_mb,
            ),
        );

        // Set fuel budget
        store
            .set_fuel(self.inner.config.fuel_per_call)
            .map_err(|_| BexError::FuelExhausted)?;

        // Set epoch deadline for wall-clock timeout (fixes Problem #3)
        let epoch_deadline = (self.inner.config.call_timeout_ms as u64
            / self.inner.config.epoch_interval_ms.max(1))
            .max(1);
        store.set_epoch_deadline(epoch_deadline);

        // Set resource limiter from HostState (fixes Problem #6 - memory limiter)
        store.limiter(|state: &mut HostState| -> &mut dyn ResourceLimiter {
            &mut state.limiter
        });

        // Increment active calls counter (fixes Issue #9)
        self.inner.active_calls.fetch_add(1, Ordering::AcqRel);

        // Instantiate using the pre-built linker (fixes Problem #2)
        let instance = self
            .inner
            .linker
            .instantiate(&mut store, &component)
            .map_err(|e| classify_trap(e, plugin_id))?;

        let plugin = Plugin::new(&mut store, &instance)
            .map_err(|e| BexError::PluginFault(format!("bind: {e}")))?;

        Ok((plugin, store))
    }

    /// Wrap a plugin call with panic catching, circuit breaker, and active call tracking.
    fn call_plugin_inner<R>(
        &self,
        plugin_id: &str,
        call: impl FnOnce(Plugin, &mut Store<HostState>) -> wasmtime::Result<Result<R, PluginError>>,
    ) -> Result<R, BexError>
    where
        R: Send + 'static,
    {
        let (instance, mut store) = self.instantiate(plugin_id)?;

        // Execute the call with panic catching (fixes stability issue)
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            call(instance, &mut store)
        }));

        // Decrement active calls
        self.inner.active_calls.fetch_sub(1, Ordering::AcqRel);

        // Get the plugin record for circuit breaker
        let record = self.inner.registry.read().get(plugin_id).cloned();

        match result {
            Ok(Ok(Ok(val))) => {
                // Success β€” record for circuit breaker
                if let Some(r) = record {
                    r.health.record_success();
                }
                Ok(val)
            }
            Ok(Ok(Err(plugin_err))) => {
                // Plugin returned a structured error (fixes Issue #14)
                if let Some(r) = record {
                    r.health.record_failure();
                }
                Err(classify_plugin_error(plugin_err))
            }
            Ok(Err(trap)) => {
                // Wasmtime trap β€” convert to BexError
                if let Some(r) = record {
                    r.health.record_failure();
                }
                Err(classify_trap(trap, plugin_id))
            }
            Err(_panic_payload) => {
                // Rust panic inside Wasmtime β€” auto-disable plugin
                tracing::error!(plugin = %plugin_id, "WASM call panicked");
                let _ = self.disable_plugin(plugin_id);
                Err(BexError::PluginFault(
                    "panic β€” plugin auto-disabled".into(),
                ))
            }
        }
    }

    // ── Typed plugin API calls ────────────────────────────────────────

    pub fn call_get_home(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
    ) -> Result<Vec<HomeSection>, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_get_home(store, ctx)
        })
    }

    pub fn call_search(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        query: &str,
        filters: &SearchFilters,
    ) -> Result<PagedResult, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_search(store, ctx, query, filters)
        })
    }

    pub fn call_get_info(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        id: &str,
    ) -> Result<MediaInfo, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_get_info(store, ctx, id)
        })
    }

    pub fn call_get_servers(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        id: &str,
    ) -> Result<Vec<Server>, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_get_servers(store, ctx, id)
        })
    }

    pub fn call_resolve_stream(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        server: &Server,
    ) -> Result<StreamSource, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_resolve_stream(store, ctx, server)
        })
    }

    pub fn call_search_subtitles(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        query: &SubtitleQuery,
    ) -> Result<Vec<SubtitleEntry>, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_search_subtitles(store, ctx, query)
        })
    }

    pub fn call_download_subtitle(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        id: &str,
    ) -> Result<SubtitleFile, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_download_subtitle(store, ctx, id)
        })
    }

    pub fn call_get_articles(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
    ) -> Result<Vec<ArticleSection>, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_get_articles(store, ctx)
        })
    }

    pub fn call_search_articles(
        &self,
        plugin_id: &str,
        ctx: &RequestContext,
        query: &str,
    ) -> Result<Vec<Article>, BexError> {
        self.call_plugin_inner(plugin_id, |instance, store| {
            instance.api().call_search_articles(store, ctx, query)
        })
    }

    // ── Convenience: default context ────────────────────────────────

    pub fn default_ctx() -> RequestContext {
        RequestContext {
            request_id: format!(
                "{:x}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis()
            ),
            locale: None,
            region: None,
            safe_mode: false,
            hints: vec![],
        }
    }

    // ── JSON-based calls for the Pure C ABI FFI layer ───────────────

    pub fn call_get_home_json(&self, plugin_id: &str) -> Result<String, BexError> {
        let result = self.call_get_home(plugin_id, &Self::default_ctx())?;
        serde_json::to_string(&convert::home_sections_to_json(&result))
            .map_err(|e| BexError::Internal(e.to_string()))
    }

    pub fn call_search_json(&self, plugin_id: &str, query: &str) -> Result<String, BexError> {
        let filters = SearchFilters {
            kind: None,
            page: PageCursor {
                token: None,
                limit: None,
            },
            fast_match: false,
        };
        let result = self.call_search(plugin_id, &Self::default_ctx(), query, &filters)?;
        serde_json::to_string(&convert::paged_result_to_json(&result))
            .map_err(|e| BexError::Internal(e.to_string()))
    }

    pub fn call_get_info_json(
        &self,
        plugin_id: &str,
        media_id: &str,
    ) -> Result<String, BexError> {
        let result = self.call_get_info(plugin_id, &Self::default_ctx(), media_id)?;
        serde_json::to_string(&convert::media_info_to_json(&result))
            .map_err(|e| BexError::Internal(e.to_string()))
    }

    pub fn call_get_servers_json(
        &self,
        plugin_id: &str,
        id: &str,
    ) -> Result<String, BexError> {
        let result = self.call_get_servers(plugin_id, &Self::default_ctx(), id)?;
        serde_json::to_string(&convert::servers_to_json(&result))
            .map_err(|e| BexError::Internal(e.to_string()))
    }

    pub fn call_resolve_stream_json(
        &self,
        plugin_id: &str,
        server_json: &str,
    ) -> Result<String, BexError> {
        let server_json_val: bex_types::stream::Server = serde_json::from_str(server_json)
            .map_err(|e| BexError::Internal(format!("parse server: {e}")))?;
        let server = convert::json_to_server(&server_json_val);
        let result = self.call_resolve_stream(plugin_id, &Self::default_ctx(), &server)?;
        serde_json::to_string(&convert::stream_source_to_json(&result))
            .map_err(|e| BexError::Internal(e.to_string()))
    }

    // ── Stats and Shutdown ───────────────────────────────────────────

    pub fn stats(&self) -> bex_types::engine_types::EngineStats {
        let reg = self.inner.registry.read();
        let total = reg.list().len();
        let enabled = reg.list().iter().filter(|p| p.enabled).count();
        let active = self.inner.active_calls.load(Ordering::Acquire);
        bex_types::engine_types::EngineStats {
            uptime_ms: self.inner.start_time.elapsed().as_millis() as u64,
            total_plugins: total,
            enabled_plugins: enabled,
            active_calls: active,
        }
    }

    pub fn shutdown(&self) {
        tracing::info!("BEX Engine shutting down...");

        // Transition to draining state (fixes Issue #8)
        self.inner.state.store(STATE_DRAINING, Ordering::Release);

        // Wait for active calls to drain (brief window for CLI responsiveness)
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        while self.inner.active_calls.load(Ordering::Acquire) > 0 {
            if std::time::Instant::now() > deadline {
                tracing::warn!(
                    "Shutdown: {} active calls still running after 2s timeout",
                    self.inner.active_calls.load(Ordering::Acquire)
                );
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(50));
        }

        // Mark as stopped
        self.inner.state.store(STATE_STOPPED, Ordering::Release);
        tracing::info!("BEX Engine shut down complete");
    }
}

// ── Trap classification ─────────────────────────────────────────────

/// Classify a Wasmtime trap into a structured BexError.
fn classify_trap(error: wasmtime::Error, plugin_id: &str) -> BexError {
    let msg = error.to_string();

    // Check for epoch interruption (timeout)
    if msg.contains("epoch") || msg.contains("timeout") {
        return BexError::Timeout { ms: 0 };
    }

    // Check for fuel exhaustion
    if msg.contains("fuel") {
        return BexError::FuelExhausted;
    }

    // Check for out-of-memory
    if msg.contains("memory") && (msg.contains("grow") || msg.contains("limit")) {
        return BexError::PluginFault(format!("out of memory: {}", plugin_id));
    }

    // Default: generic plugin fault
    BexError::PluginFault(msg)
}

/// Classify a plugin error from the WIT bindgen into a structured BexError.
fn classify_plugin_error(err: PluginError) -> BexError {
    match err {
        PluginError::Network(msg) => BexError::Network(msg),
        PluginError::Parse(msg) => BexError::PluginError(msg),
        PluginError::NotFound => BexError::PluginError("not found".into()),
        PluginError::Unauthorized => BexError::PluginError("unauthorized".into()),
        PluginError::Forbidden => BexError::PluginError("forbidden".into()),
        PluginError::RateLimited(Some(secs)) => BexError::Timeout {
            ms: secs * 1000,
        },
        PluginError::RateLimited(None) => BexError::Timeout { ms: 0 },
        PluginError::Timeout => BexError::Timeout { ms: 0 },
        PluginError::Cancelled => BexError::Cancelled,
        PluginError::Unsupported => {
            BexError::Unsupported("plugin does not support this operation".into())
        }
        PluginError::InvalidInput(msg) => BexError::PluginError(msg),
        PluginError::Internal(msg) => BexError::PluginError(msg),
    }
}

// ── Conversion between bindgen types and JSON-serializable types ─────
pub mod convert {
    use super::*;

    pub fn home_sections_to_json(sections: &[HomeSection]) -> Vec<serde_json::Value> {
        sections
            .iter()
            .map(|s| serde_json::to_value(home_section_to_json(s)).unwrap_or_default())
            .collect()
    }

    pub fn home_section_to_json(s: &HomeSection) -> bex_types::media::HomeSection {
        bex_types::media::HomeSection {
            id: s.id.clone(),
            title: s.title.clone(),
            subtitle: s.subtitle.clone(),
            items: s.items.iter().map(media_card_to_json).collect(),
            next_page: s.next_page.clone(),
            layout: format!("{:?}", s.layout).to_lowercase(),
            show_rank: s.show_rank,
            categories: s
                .categories
                .iter()
                .map(|c| bex_types::media::CategoryLink {
                    id: c.id.clone(),
                    title: c.title.clone(),
                    subtitle: c.subtitle.clone(),
                    image: c.image.as_ref().map(image_to_json),
                })
                .collect(),
            extra: s
                .extra
                .iter()
                .map(|a| (a.key.clone(), a.value.clone()))
                .collect(),
        }
    }

    pub fn media_card_to_json(c: &MediaCard) -> bex_types::media::MediaCard {
        bex_types::media::MediaCard {
            id: c.id.clone(),
            title: c.title.clone(),
            kind: c.kind.map(|k| format!("{:?}", k).to_lowercase()),
            images: c.images.as_ref().map(image_set_to_json),
            original_title: c.original_title.clone(),
            tagline: c.tagline.clone(),
            year: c.year.clone(),
            score: c.score,
            genres: c.genres.clone(),
            status: c.status.map(|s| format!("{:?}", s).to_lowercase()),
            content_rating: c.content_rating.clone(),
            url: c.url.clone(),
            ids: c
                .ids
                .iter()
                .map(|id| bex_types::media::LinkedId {
                    source: id.source.clone(),
                    id: id.id.clone(),
                })
                .collect(),
            extra: c
                .extra
                .iter()
                .map(|a| (a.key.clone(), a.value.clone()))
                .collect(),
        }
    }

    pub fn image_set_to_json(s: &ImageSet) -> bex_types::media::ImageSet {
        bex_types::media::ImageSet {
            low: s.low.as_ref().map(image_to_json),
            medium: s.medium.as_ref().map(image_to_json),
            high: s.high.as_ref().map(image_to_json),
            backdrop: s.backdrop.as_ref().map(image_to_json),
            logo: s.logo.as_ref().map(image_to_json),
        }
    }

    pub fn image_to_json(i: &Image) -> bex_types::media::Image {
        bex_types::media::Image {
            url: i.url.clone(),
            layout: format!("{:?}", i.layout).to_lowercase(),
            width: i.width,
            height: i.height,
            blurhash: i.blurhash.clone(),
        }
    }

    pub fn paged_result_to_json(r: &PagedResult) -> bex_types::media::PagedResult {
        bex_types::media::PagedResult {
            items: r.items.iter().map(media_card_to_json).collect(),
            categories: r
                .categories
                .iter()
                .map(|c| bex_types::media::CategoryLink {
                    id: c.id.clone(),
                    title: c.title.clone(),
                    subtitle: c.subtitle.clone(),
                    image: c.image.as_ref().map(image_to_json),
                })
                .collect(),
            next_page: r.next_page.clone(),
        }
    }

    #[allow(clippy::too_many_lines)]
    pub fn media_info_to_json(m: &MediaInfo) -> bex_types::media::MediaInfo {
        bex_types::media::MediaInfo {
            id: m.id.clone(),
            title: m.title.clone(),
            kind: format!("{:?}", m.kind).to_lowercase(),
            images: m.images.as_ref().map(image_set_to_json),
            original_title: m.original_title.clone(),
            description: m.description.clone(),
            score: m.score,
            scored_by: m.scored_by,
            year: m.year.clone(),
            release_date: m.release_date.clone(),
            genres: m.genres.clone(),
            tags: m.tags.clone(),
            status: m.status.map(|s| format!("{:?}", s).to_lowercase()),
            content_rating: m.content_rating.clone(),
            seasons: m
                .seasons
                .iter()
                .map(|s| bex_types::media::Season {
                    id: s.id.clone(),
                    title: s.title.clone(),
                    number: s.number,
                    year: s.year,
                    episodes: s
                        .episodes
                        .iter()
                        .map(|e| bex_types::media::Episode {
                            id: e.id.clone(),
                            title: e.title.clone(),
                            number: e.number,
                            season: e.season,
                            images: e.images.as_ref().map(image_set_to_json),
                            description: e.description.clone(),
                            released: e.released.clone(),
                            score: e.score,
                            url: e.url.clone(),
                            tags: e.tags.clone(),
                            extra: e
                                .extra
                                .iter()
                                .map(|a| (a.key.clone(), a.value.clone()))
                                .collect(),
                        })
                        .collect(),
                })
                .collect(),
            cast: m
                .cast
                .iter()
                .map(|p| bex_types::media::Person {
                    id: p.id.clone(),
                    name: p.name.clone(),
                    image: p.image.as_ref().map(image_set_to_json),
                    role: p.role.clone(),
                    url: p.url.clone(),
                })
                .collect(),
            crew: m
                .crew
                .iter()
                .map(|p| bex_types::media::Person {
                    id: p.id.clone(),
                    name: p.name.clone(),
                    image: p.image.as_ref().map(image_set_to_json),
                    role: p.role.clone(),
                    url: p.url.clone(),
                })
                .collect(),
            runtime_minutes: m.runtime_minutes,
            trailer_url: m.trailer_url.clone(),
            ids: m
                .ids
                .iter()
                .map(|id| bex_types::media::LinkedId {
                    source: id.source.clone(),
                    id: id.id.clone(),
                })
                .collect(),
            studio: m.studio.clone(),
            country: m.country.clone(),
            language: m.language.clone(),
            url: m.url.clone(),
            extra: m
                .extra
                .iter()
                .map(|a| (a.key.clone(), a.value.clone()))
                .collect(),
        }
    }

    pub fn servers_to_json(servers: &[Server]) -> Vec<bex_types::stream::Server> {
        servers
            .iter()
            .map(|s| bex_types::stream::Server {
                id: s.id.clone(),
                label: s.label.clone(),
                url: s.url.clone(),
                priority: s.priority,
                extra: s
                    .extra
                    .iter()
                    .map(|a| (a.key.clone(), a.value.clone()))
                    .collect(),
            })
            .collect()
    }

    pub fn json_to_server(s: &bex_types::stream::Server) -> Server {
        Server {
            id: s.id.clone(),
            label: s.label.clone(),
            url: s.url.clone(),
            priority: s.priority,
            extra: s
                .extra
                .iter()
                .map(|(k, v)| Attr {
                    key: k.clone(),
                    value: v.clone(),
                })
                .collect(),
        }
    }

    pub fn stream_source_to_json(s: &StreamSource) -> bex_types::stream::StreamSource {
        bex_types::stream::StreamSource {
            id: s.id.clone(),
            label: s.label.clone(),
            format: format!("{:?}", s.format).to_lowercase(),
            manifest_url: s.manifest_url.clone(),
            videos: s
                .videos
                .iter()
                .map(|v| bex_types::stream::VideoTrack {
                    resolution: bex_types::stream::VideoResolution {
                        width: v.resolution.width,
                        height: v.resolution.height,
                        hdr: v.resolution.hdr,
                        label: v.resolution.label.clone(),
                    },
                    url: v.url.clone(),
                    mime_type: v.mime_type.clone(),
                    bitrate: v.bitrate,
                    codecs: v.codecs.clone(),
                })
                .collect(),
            subtitles: s
                .subtitles
                .iter()
                .map(|st| bex_types::stream::SubtitleTrack {
                    label: st.label.clone(),
                    url: st.url.clone(),
                    language: st.language.clone(),
                    format: st.format.clone(),
                })
                .collect(),
            headers: s
                .headers
                .iter()
                .map(|a| (a.key.clone(), a.value.clone()))
                .collect(),
            extra: s
                .extra
                .iter()
                .map(|a| (a.key.clone(), a.value.clone()))
                .collect(),
        }
    }
}