zlayer-agent 0.11.12

Container runtime agent using libcontainer/youki
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
//! WebAssembly runtime implementation using wasmtime
//!
//! Implements the Runtime trait for executing WebAssembly modules with WASI support.
//! This enables running WASM workloads alongside traditional containers.
//!
//! ## Features
//!
//! - **WASI Preview 1 (wasip1)**: Core module support with basic WASI syscalls
//! - **WASI Preview 2 (wasip2)**: Component model support with full WASI interfaces
//! - **Async execution**: Tokio integration for cooperative scheduling
//! - **Epoch-based interruption**: Timeout support via epoch deadlines
//! - **Log capture**: stdout/stderr captured via memory pipes for container logs
//! - **Filesystem mounts**: Bind mounts and named volumes via WASI preopens
//! - **Networking**: Full wasi:sockets support (TCP, UDP, IP name lookup)
//!
//! ## Supported Storage Types
//!
//! - **Bind mounts**: Direct host path mapping to guest path
//! - **Named volumes**: Persistent storage under `{data_dir}/volumes/{name}`
//!
//! ## Unsupported Storage Types (logged as warnings)
//!
//! - **Tmpfs**: Memory-backed mounts (not supported in WASI)
//! - **Anonymous**: Auto-named volumes (not supported for WASM)
//! - **S3**: FUSE-based S3 mounts (not supported in WASI)
//!
//! ## Limitations
//!
//! - No `exec` support (WASM modules are single-process)
//! - No cgroup stats (WASM runs in-process, no kernel isolation)

use crate::cgroups_stats::ContainerStats;
use crate::error::{AgentError, Result};
use crate::runtime::{ContainerId, ContainerState, Runtime};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::instrument;
use wasmtime::component::{Component, Linker as ComponentLinker, ResourceTable};
use wasmtime::{Config, Engine, Linker, Module, Store, StoreLimits, StoreLimitsBuilder};
use wasmtime_wasi::p1::{self, WasiP1Ctx};
// TODO: Phase 6 (stdout/stderr capture) will use these pipe types
#[allow(unused_imports)]
use wasmtime_wasi::p2::pipe::{MemoryInputPipe, MemoryOutputPipe};
use wasmtime_wasi::{DirPerms, FilePerms, WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};
use zlayer_observability::logs::{LogEntry, LogSource, LogStream};
use zlayer_registry::{detect_wasm_version_from_binary, WasiVersion};
use zlayer_spec::{PullPolicy, RegistryAuth, ServiceSpec, StorageSpec, WasmCapabilities};

/// Default capacity for stdout/stderr capture pipes (1MB)
/// TODO: Phase 6 (stdout/stderr capture) will use this constant
#[allow(dead_code)]
const STDIO_PIPE_CAPACITY: usize = 1024 * 1024;

/// Result of WASM module/component execution with captured output
#[derive(Debug, Clone)]
struct ExecutionResult {
    /// Exit code from the WASM execution
    exit_code: i32,
    /// Captured stdout bytes
    stdout: Vec<u8>,
    /// Captured stderr bytes
    stderr: Vec<u8>,
}

/// Configure filesystem mounts on a WASI context builder.
///
/// This function processes the storage specifications and configures preopened
/// directories for supported mount types (Bind and Named volumes).
///
/// # Arguments
/// * `wasi_builder` - The `WasiCtxBuilder` to configure
/// * `mounts` - Slice of `StorageSpec` defining the mounts
///
/// # Returns
/// * `Ok(())` if all supported mounts were configured successfully
/// * `Err(String)` if a mount configuration failed
///
/// # Supported Storage Types
/// * `StorageSpec::Bind` - Direct host path to guest path mapping
/// * `StorageSpec::Named` - Named volumes stored under `{data_dir}/volumes/{name}`
///
/// # Unsupported Storage Types (logged as warnings)
/// * `StorageSpec::Tmpfs` - Memory-backed mounts
/// * `StorageSpec::Anonymous` - Auto-named volumes
/// * `StorageSpec::S3` - FUSE-based S3 mounts
fn configure_wasi_mounts(
    wasi_builder: &mut WasiCtxBuilder,
    mounts: &[StorageSpec],
) -> std::result::Result<(), String> {
    for mount in mounts {
        match mount {
            StorageSpec::Bind {
                source,
                target,
                readonly,
            } => {
                let (dir_perms, file_perms) = if *readonly {
                    (DirPerms::READ, FilePerms::READ)
                } else {
                    (DirPerms::all(), FilePerms::all())
                };

                wasi_builder
                    .preopened_dir(source, target, dir_perms, file_perms)
                    .map_err(|e| {
                        format!("failed to preopen bind mount '{source}' -> '{target}': {e}")
                    })?;

                tracing::debug!(
                    source = %source,
                    target = %target,
                    readonly = %readonly,
                    "configured bind mount for WASM"
                );
            }
            StorageSpec::Named {
                name,
                target,
                readonly,
                ..
            } => {
                // Named volumes are stored under the volumes directory
                let volume_path = zlayer_paths::ZLayerDirs::system_default()
                    .volumes()
                    .join(name)
                    .to_string_lossy()
                    .into_owned();

                let (dir_perms, file_perms) = if *readonly {
                    (DirPerms::READ, FilePerms::READ)
                } else {
                    (DirPerms::all(), FilePerms::all())
                };

                wasi_builder
                    .preopened_dir(&volume_path, target, dir_perms, file_perms)
                    .map_err(|e| {
                        format!("failed to preopen named volume '{name}' at '{target}': {e}")
                    })?;

                tracing::debug!(
                    name = %name,
                    volume_path = %volume_path,
                    target = %target,
                    readonly = %readonly,
                    "configured named volume for WASM"
                );
            }
            StorageSpec::Tmpfs { target, .. } => {
                tracing::warn!(
                    target = %target,
                    "tmpfs storage not supported for WASM, skipping"
                );
            }
            StorageSpec::Anonymous { target, .. } => {
                tracing::warn!(
                    target = %target,
                    "anonymous storage not supported for WASM, skipping"
                );
            }
            StorageSpec::S3 { bucket, target, .. } => {
                tracing::warn!(
                    bucket = %bucket,
                    target = %target,
                    "S3 storage not supported for WASM, skipping"
                );
            }
        }
    }

    Ok(())
}

/// Parse a memory format string like "64Mi", "256Mi", "1Gi" into bytes.
///
/// Supports Kubernetes-style binary suffixes:
/// - `Ki` = kibibytes (1024)
/// - `Mi` = mebibytes (1024^2)
/// - `Gi` = gibibytes (1024^3)
/// - `Ti` = tebibytes (1024^4)
pub(super) fn parse_memory_limit(s: &str) -> std::result::Result<u64, String> {
    let s = s.trim();
    if s.len() < 3 {
        return Err(format!("invalid memory format: {s}"));
    }
    let (num_str, suffix) = s.split_at(s.len() - 2);
    let num: u64 = num_str
        .parse()
        .map_err(|e| format!("invalid number in memory limit '{s}': {e}"))?;
    match suffix {
        "Ki" => Ok(num.saturating_mul(1024)),
        "Mi" => Ok(num.saturating_mul(1024 * 1024)),
        "Gi" => Ok(num.saturating_mul(1024 * 1024 * 1024)),
        "Ti" => Ok(num.saturating_mul(1024 * 1024 * 1024 * 1024)),
        _ => Err(format!("unknown memory suffix '{suffix}' in '{s}'")),
    }
}

/// Build a [`StoreLimits`] from the spec-level [`zlayer_spec::WasmConfig`] resource fields.
///
/// Returns `None` when no resource limits are configured (default behaviour).
fn build_store_limits(spec_wasm: Option<&zlayer_spec::WasmConfig>) -> Option<StoreLimits> {
    let wasm = spec_wasm?;

    // Only build limits if max_memory is actually set
    let max_memory = wasm.max_memory.as_ref()?;
    let max_bytes = match parse_memory_limit(max_memory) {
        Ok(b) => b,
        Err(e) => {
            tracing::warn!(max_memory = %max_memory, error = %e, "ignoring invalid max_memory");
            return None;
        }
    };

    let limits = StoreLimitsBuilder::new()
        .memory_size(
            #[allow(clippy::cast_possible_truncation)]
            {
                max_bytes as usize
            },
        )
        .build();

    tracing::info!(max_bytes = max_bytes, "WASM store memory limit configured");
    Some(limits)
}

/// Per-instance resource limit configuration extracted from the spec.
///
/// Passed through to `execute_module` / `execute_component` so that Store-level
/// limits can be applied when execution begins.
#[derive(Debug, Clone)]
struct ResourceLimits {
    /// Parsed memory limit in bytes (from spec `max_memory`)
    store_limits: Option<StoreLimits>,
    /// Fuel budget (0 = unlimited)
    max_fuel: u64,
    /// Epoch interval for cooperative preemption (overrides engine default)
    epoch_interval: Option<Duration>,
}

impl ResourceLimits {
    /// Build from the spec-level `WasmConfig` if present.
    fn from_spec(spec_wasm: Option<&zlayer_spec::WasmConfig>) -> Self {
        let store_limits = build_store_limits(spec_wasm);
        let max_fuel = spec_wasm.map_or(0, |w| w.max_fuel);
        let epoch_interval = spec_wasm.and_then(|w| w.epoch_interval);
        Self {
            store_limits,
            max_fuel,
            epoch_interval,
        }
    }
}

/// `WASIp1` state wrapper that owns both the WASI context and the resource limiter.
///
/// This is needed because `Store::limiter()` requires the limiter to be accessible
/// via a closure from the store's state type, and `WasiP1Ctx` is an external type
/// we cannot modify.
struct WasiP1State {
    /// The underlying `WASIp1` context
    wasi: WasiP1Ctx,
    /// Optional resource limiter for memory/table growth
    limiter: StoreLimits,
}

/// `WASIp2` component state wrapper (extends existing `WasiState`)
///
/// Holds the WASI context, resource table, and resource limiter.
struct WasiP2State {
    /// WASI Preview 2 context with environment, args, and capabilities
    ctx: WasiCtx,
    /// Resource table for component model resources (files, sockets, etc.)
    table: ResourceTable,
    /// Resource limiter for memory/table growth
    limiter: StoreLimits,
}

impl WasiView for WasiP2State {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView {
            ctx: &mut self.ctx,
            table: &mut self.table,
        }
    }
}

/// Configuration for `WasmRuntime`
#[derive(Debug, Clone)]
pub struct WasmConfig {
    /// Directory for caching pulled WASM modules
    pub cache_dir: PathBuf,
    /// Enable epoch-based interruption for timeouts
    pub enable_epochs: bool,
    /// Default epoch deadline (instructions before yield)
    pub epoch_deadline: u64,
    /// Maximum execution time for WASM modules
    pub max_execution_time: Duration,
    /// Optional cache type configuration for blob storage
    /// If None, uses `CacheType::from_env()` with path override from `cache_dir`
    pub cache_type: Option<zlayer_registry::CacheType>,
}

impl Default for WasmConfig {
    fn default() -> Self {
        Self {
            cache_dir: std::env::var("ZLAYER_WASM_CACHE_DIR").map_or_else(
                |_| zlayer_paths::ZLayerDirs::system_default().wasm(),
                PathBuf::from,
            ),
            enable_epochs: true,
            epoch_deadline: 1_000_000, // 1M instructions before yield check
            max_execution_time: Duration::from_secs(3600), // 1 hour default
            cache_type: None,
        }
    }
}

/// State for a WASM instance execution
#[derive(Debug, Clone)]
enum InstanceState {
    /// Instance is pending (module compiled, not started)
    Pending,
    /// Instance is currently running
    Running {
        /// When execution started (used for timeout tracking)
        #[allow(dead_code)]
        started_at: Instant,
    },
    /// Instance has completed successfully
    Completed { exit_code: i32 },
    /// Instance failed with an error
    Failed { reason: String },
}

/// Information about a WASM instance
struct WasmInstance {
    /// Container state
    state: InstanceState,
    /// Image reference
    image: String,
    /// Compiled module bytes (cached)
    module_bytes: Vec<u8>,
    /// WASI version
    wasi_version: WasiVersion,
    /// Captured stdout
    stdout: Vec<u8>,
    /// Captured stderr
    stderr: Vec<u8>,
    /// Environment variables for execution
    env_vars: Vec<(String, String)>,
    /// Command args
    args: Vec<String>,
    /// Filesystem mounts for WASI preopens
    mounts: Vec<StorageSpec>,
    /// Resource limits from the spec-level `WasmConfig`
    resource_limits: ResourceLimits,
    /// Capability grants from the spec-level `WasmConfig` (controls WASI context gating)
    capabilities: Option<WasmCapabilities>,
    /// Execution handle (if running) - returns `ExecutionResult` with captured stdout/stderr
    execution_handle: Option<tokio::task::JoinHandle<std::result::Result<ExecutionResult, String>>>,
}

impl std::fmt::Debug for WasmInstance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WasmInstance")
            .field("state", &self.state)
            .field("image", &self.image)
            .field("wasi_version", &self.wasi_version)
            .field("stdout_len", &self.stdout.len())
            .field("stderr_len", &self.stderr.len())
            .finish_non_exhaustive()
    }
}

/// WebAssembly runtime using wasmtime
///
/// This runtime executes WASM modules with WASI support, providing a lightweight
/// alternative to full container runtimes for compatible workloads.
pub struct WasmRuntime {
    /// Wasmtime engine (shared across all instances)
    engine: Engine,
    /// Configuration
    config: WasmConfig,
    /// Registry puller for fetching WASM artifacts
    registry: Arc<zlayer_registry::ImagePuller>,
    /// Authentication resolver
    auth_resolver: zlayer_core::AuthResolver,
    /// Active instances
    instances: RwLock<HashMap<String, WasmInstance>>,
}

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

impl WasmRuntime {
    /// Create a new `WasmRuntime` with the given configuration
    ///
    /// # Errors
    ///
    /// Returns an error if the cache directory cannot be created, the wasmtime engine
    /// cannot be initialized, or the blob cache cannot be opened.
    pub async fn new(
        config: WasmConfig,
        _auth_ctx: Option<crate::runtime::ContainerAuthContext>,
    ) -> Result<Self> {
        // Create cache directory
        tokio::fs::create_dir_all(&config.cache_dir)
            .await
            .map_err(|e| AgentError::CreateFailed {
                id: "wasm-runtime".to_string(),
                reason: format!("failed to create cache directory: {e}"),
            })?;

        // Configure wasmtime engine
        // Note: We use sync execution in spawn_blocking, so async_support is disabled.
        // Epoch-based interruption still works for timeout/cancellation support.
        let mut engine_config = Config::new();

        if config.enable_epochs {
            engine_config.epoch_interruption(true);
        }

        // Enable fuel metering so that per-instance fuel budgets can be applied.
        // This has negligible cost when fuel is not set on a particular Store.
        engine_config.consume_fuel(true);

        let engine = Engine::new(&engine_config).map_err(|e| AgentError::CreateFailed {
            id: "wasm-runtime".to_string(),
            reason: format!("failed to create wasmtime engine: {e}"),
        })?;

        // Create blob cache for registry using new CacheType configuration
        let blob_cache = if let Some(cache_type) = &config.cache_type {
            cache_type
                .build()
                .await
                .map_err(|e| AgentError::CreateFailed {
                    id: "wasm-runtime".to_string(),
                    reason: format!("failed to open blob cache: {e}"),
                })?
        } else {
            // Use environment-based configuration, but override path if Persistent
            let cache_type =
                zlayer_registry::CacheType::from_env().map_err(|e| AgentError::CreateFailed {
                    id: "wasm-runtime".to_string(),
                    reason: format!("failed to configure blob cache from env: {e}"),
                })?;
            #[allow(clippy::match_wildcard_for_single_variants)]
            let cache_type = match cache_type {
                zlayer_registry::CacheType::Persistent { .. } => {
                    zlayer_registry::CacheType::persistent_at(config.cache_dir.clone())
                }
                other => other,
            };
            cache_type
                .build()
                .await
                .map_err(|e| AgentError::CreateFailed {
                    id: "wasm-runtime".to_string(),
                    reason: format!("failed to open blob cache: {e}"),
                })?
        };

        let registry = Arc::new(zlayer_registry::ImagePuller::with_cache(blob_cache));

        tracing::info!("WASM runtime initialized with wasmtime");

        Ok(Self {
            engine,
            config,
            registry,
            auth_resolver: zlayer_core::AuthResolver::new(zlayer_core::AuthConfig::default()),
            instances: RwLock::new(HashMap::new()),
        })
    }

    /// Create a new `WasmRuntime` with default configuration
    ///
    /// # Errors
    ///
    /// Returns an error if the runtime cannot be initialized.
    pub async fn with_defaults() -> Result<Self> {
        Self::new(WasmConfig::default(), None).await
    }

    /// Create a new `WasmRuntime` with custom auth configuration
    ///
    /// # Errors
    ///
    /// Returns an error if the runtime cannot be initialized.
    pub async fn with_auth(
        config: WasmConfig,
        auth_config: zlayer_core::AuthConfig,
    ) -> Result<Self> {
        let mut runtime = Self::new(config, None).await?;
        runtime.auth_resolver = zlayer_core::AuthResolver::new(auth_config);
        Ok(runtime)
    }

    /// Get the instance ID string from a `ContainerId`
    #[allow(clippy::unused_self)]
    fn instance_id(&self, id: &ContainerId) -> String {
        format!("wasm-{}-{}", id.service, id.replica)
    }

    /// Build environment variables from `ServiceSpec`
    #[allow(clippy::unused_self)]
    fn build_env_vars(&self, spec: &ServiceSpec) -> Vec<(String, String)> {
        let mut env = Vec::new();

        // Add default environment variables
        env.push((
            "PATH".to_string(),
            "/usr/local/bin:/usr/bin:/bin".to_string(),
        ));

        // Resolve and add spec environment variables
        let resolved = crate::env::resolve_env_vars_with_warnings(&spec.env);
        match resolved {
            Ok(result) => {
                for warning in &result.warnings {
                    tracing::warn!("env resolution warning: {}", warning);
                }
                for var in result.vars {
                    if let Some((key, value)) = var.split_once('=') {
                        env.push((key.to_string(), value.to_string()));
                    }
                }
            }
            Err(e) => {
                tracing::warn!("failed to resolve env vars: {}", e);
            }
        }

        env
    }

    /// Build command arguments from `ServiceSpec`
    #[allow(clippy::unused_self)]
    fn build_args(&self, spec: &ServiceSpec) -> Vec<String> {
        let mut args = Vec::new();

        // Module name as argv[0]
        args.push(spec.image.name.to_string());

        // Add entrypoint args if present
        if let Some(entrypoint) = &spec.command.entrypoint {
            args.extend_from_slice(entrypoint);
        }

        // Add command args if present
        if let Some(cmd_args) = &spec.command.args {
            args.extend_from_slice(cmd_args);
        }

        args
    }

    /// Execute a WASM module asynchronously
    ///
    /// # Arguments
    /// * `engine` - The wasmtime Engine
    /// * `module_bytes` - The compiled WASM module bytes
    /// * `env_vars` - Environment variables to set
    /// * `args` - Command line arguments
    /// * `mounts` - Filesystem mounts to configure as preopens
    /// * `epoch_deadline` - Epoch deadline for interruption
    /// * `enable_epochs` - Whether to enable epoch-based interruption
    /// * `resource_limits` - Per-instance resource limits from the spec
    /// * `capabilities` - Optional capability grants controlling WASI context.
    ///   `WASIp1` modules don't support `ZLayer` custom interfaces, so only
    ///   WASI-level capabilities (CLI, filesystem, sockets) are relevant.
    ///   When `None`, all capabilities are enabled (backward-compatible).
    ///
    /// # Returns
    /// `ExecutionResult` containing the exit code and captured stdout/stderr
    #[allow(clippy::too_many_arguments)]
    async fn execute_module(
        engine: Engine,
        module_bytes: Vec<u8>,
        env_vars: Vec<(String, String)>,
        args: Vec<String>,
        mounts: Vec<StorageSpec>,
        epoch_deadline: u64,
        enable_epochs: bool,
        resource_limits: ResourceLimits,
        capabilities: Option<WasmCapabilities>,
    ) -> std::result::Result<ExecutionResult, String> {
        // This runs in a blocking context because wasmtime operations are CPU-bound
        tokio::task::spawn_blocking(move || {
            // Compile module
            let module = Module::new(&engine, &module_bytes)
                .map_err(|e| format!("failed to compile module: {e}"))?;

            // Build WASI context with environment and args, gated by capabilities
            let mut wasi_builder = WasiCtxBuilder::new();

            if let Some(ref caps) = capabilities {
                // Apply capability-gated WASI context (handles CLI, sockets)
                super::wasm_host::configure_wasi_ctx_with_capabilities(&mut wasi_builder, caps);

                // Always set explicit env vars and args for module execution
                for (key, value) in &env_vars {
                    wasi_builder.env(key, value);
                }
                wasi_builder.args(&args);

                // Only configure filesystem mounts if filesystem capability is enabled
                if caps.filesystem {
                    configure_wasi_mounts(&mut wasi_builder, &mounts)?;
                } else {
                    tracing::debug!("filesystem capability disabled, skipping WASI mounts");
                }
            } else {
                // Backward-compatible: all capabilities enabled
                for (key, value) in &env_vars {
                    wasi_builder.env(key, value);
                }
                wasi_builder.args(&args);
                configure_wasi_mounts(&mut wasi_builder, &mounts)?;
                wasi_builder.inherit_network();
            }

            // Create memory pipes for stdout/stderr capture
            // These are cloneable (Arc<Mutex> internally) so we can read after execution
            let stdout_pipe = MemoryOutputPipe::new(STDIO_PIPE_CAPACITY);
            let stderr_pipe = MemoryOutputPipe::new(STDIO_PIPE_CAPACITY);

            // Configure stdio with memory pipes for capture
            wasi_builder.stdin(MemoryInputPipe::new(Vec::new())); // Empty stdin
            wasi_builder.stdout(stdout_pipe.clone());
            wasi_builder.stderr(stderr_pipe.clone());

            let wasi_ctx = wasi_builder.build_p1();

            // Build store limits from the per-instance resource limits
            let limiter = resource_limits
                .store_limits
                .clone()
                .unwrap_or_else(|| StoreLimitsBuilder::new().build());

            // Create store with wrapped state (WasiP1Ctx + limiter)
            let state = WasiP1State {
                wasi: wasi_ctx,
                limiter,
            };
            let mut store = Store::new(&engine, state);

            // Attach the resource limiter to the store
            store.limiter(|s| &mut s.limiter);

            // Set epoch deadline for interruption if enabled
            if enable_epochs {
                store.set_epoch_deadline(epoch_deadline);
            }

            // Apply fuel budget if configured, otherwise set unlimited fuel.
            // consume_fuel(true) is set on the engine, so stores start with 0 fuel
            // and will immediately trap unless fuel is explicitly provided.
            if resource_limits.max_fuel > 0 {
                store
                    .set_fuel(resource_limits.max_fuel)
                    .map_err(|e| format!("failed to set fuel: {e}"))?;
                tracing::debug!(fuel = resource_limits.max_fuel, "WASM fuel budget set");
            } else {
                store
                    .set_fuel(u64::MAX)
                    .map_err(|e| format!("failed to set default fuel: {e}"))?;
            }

            // Create linker and add WASI (closure extracts WasiP1Ctx from our wrapper)
            let mut linker: Linker<WasiP1State> = Linker::new(&engine);
            p1::add_to_linker_sync(&mut linker, |state| &mut state.wasi)
                .map_err(|e| format!("failed to add WASI to linker: {e}"))?;

            // Instantiate the module
            let instance = linker
                .instantiate(&mut store, &module)
                .map_err(|e| format!("failed to instantiate module: {e}"))?;

            // Look for _start (WASI command) or main function
            let start_func = instance
                .get_func(&mut store, "_start")
                .or_else(|| instance.get_func(&mut store, "main"));

            let exit_code = match start_func {
                Some(func) => {
                    // Call the entry function
                    match func.call(&mut store, &[], &mut []) {
                        Ok(()) => 0,
                        Err(e) => {
                            // Check for WASI exit
                            if let Some(exit) = e.downcast_ref::<wasmtime_wasi::I32Exit>() {
                                exit.0
                            } else {
                                // Capture output even on error before returning
                                let stdout = stdout_pipe.contents().to_vec();
                                let stderr = stderr_pipe.contents().to_vec();
                                return Err(format!(
                                    "execution error: {} (stdout: {} bytes, stderr: {} bytes)",
                                    e,
                                    stdout.len(),
                                    stderr.len()
                                ));
                            }
                        }
                    }
                }
                None => return Err("no _start or main function found".to_string()),
            };

            // Capture output from the pipes after execution
            let stdout = stdout_pipe.contents().to_vec();
            let stderr = stderr_pipe.contents().to_vec();

            Ok(ExecutionResult {
                exit_code,
                stdout,
                stderr,
            })
        })
        .await
        .map_err(|e| format!("task join error: {e}"))?
    }

    /// Execute a `WASIp2` component
    ///
    /// `WASIp2` components use the WebAssembly Component Model and provide richer
    /// WASI interfaces compared to Preview 1 modules. This method handles:
    /// - Component compilation and instantiation
    /// - WASI Preview 2 interface linking (wasi:cli, wasi:io, etc.)
    /// - Filesystem mounts via preopened directories
    /// - Calling the wasi:cli/run export
    ///
    /// # Arguments
    /// * `engine` - The wasmtime Engine
    /// * `component_bytes` - The compiled WASM component bytes
    /// * `env_vars` - Environment variables to set
    /// * `args` - Command line arguments
    /// * `mounts` - Filesystem mounts to configure as preopens
    /// * `epoch_deadline` - Epoch deadline for interruption
    /// * `enable_epochs` - Whether to enable epoch-based interruption
    /// * `resource_limits` - Per-instance resource limits from the spec
    /// * `capabilities` - Optional capability grants for WASI context gating.
    ///   When `None`, all capabilities are enabled (backward-compatible).
    ///   When `Some`, the WASI context is configured based on the capabilities:
    ///   - CLI, filesystem, sockets gated at `WasiCtx` level
    ///   - `ZLayer` custom interfaces not applicable here (`WASIp2` only)
    ///
    /// # Returns
    /// `ExecutionResult` containing the exit code and captured stdout/stderr
    #[allow(clippy::too_many_arguments, clippy::too_many_lines)]
    #[instrument(
        skip(engine, component_bytes, env_vars, args, mounts, resource_limits, capabilities),
        fields(
            component_size = component_bytes.len(),
            args_count = args.len(),
            mounts_count = mounts.len(),
        )
    )]
    async fn execute_component(
        engine: Engine,
        component_bytes: Vec<u8>,
        env_vars: Vec<(String, String)>,
        args: Vec<String>,
        mounts: Vec<StorageSpec>,
        epoch_deadline: u64,
        enable_epochs: bool,
        resource_limits: ResourceLimits,
        capabilities: Option<WasmCapabilities>,
    ) -> std::result::Result<ExecutionResult, String> {
        // Component model operations are CPU-bound, run in blocking context
        tokio::task::spawn_blocking(move || {
            // Compile the component
            let component = Component::from_binary(&engine, &component_bytes)
                .map_err(|e| format!("failed to compile component: {e}"))?;

            // Build WASIp2 context with environment and args, gated by capabilities
            let mut wasi_builder = WasiCtxBuilder::new();

            if let Some(ref caps) = capabilities {
                // Apply capability-gated WASI context (handles CLI, sockets)
                super::wasm_host::configure_wasi_ctx_with_capabilities(&mut wasi_builder, caps);

                // Always set explicit env vars and args
                for (key, value) in &env_vars {
                    wasi_builder.env(key, value);
                }
                wasi_builder.args(&args);

                // Only configure filesystem mounts if filesystem capability is enabled
                if caps.filesystem {
                    configure_wasi_mounts(&mut wasi_builder, &mounts)?;
                } else {
                    tracing::debug!("filesystem capability disabled, skipping WASI mounts");
                }
            } else {
                // Backward-compatible: all capabilities enabled
                for (key, value) in &env_vars {
                    wasi_builder.env(key, value);
                }
                wasi_builder.args(&args);
                configure_wasi_mounts(&mut wasi_builder, &mounts)?;
                wasi_builder.inherit_network();
            }

            // Create memory pipes for stdout/stderr capture
            // These are cloneable (Arc<Mutex> internally) so we can read after execution
            let stdout_pipe = MemoryOutputPipe::new(STDIO_PIPE_CAPACITY);
            let stderr_pipe = MemoryOutputPipe::new(STDIO_PIPE_CAPACITY);

            // Configure stdio with memory pipes for capture
            wasi_builder.stdin(MemoryInputPipe::new(Vec::new())); // Empty stdin
            wasi_builder.stdout(stdout_pipe.clone());
            wasi_builder.stderr(stderr_pipe.clone());

            // Build the WASIp2 context
            let wasi_ctx = wasi_builder.build();

            // Create resource table for component model resources
            let table = ResourceTable::new();

            // Build store limits from the per-instance resource limits
            let limiter = resource_limits
                .store_limits
                .clone()
                .unwrap_or_else(|| StoreLimitsBuilder::new().build());

            // Create our WasiP2State that implements WasiView (with limiter)
            let state = WasiP2State {
                ctx: wasi_ctx,
                table,
                limiter,
            };

            // Create store with our WASI state
            let mut store = Store::new(&engine, state);

            // Attach the resource limiter to the store
            store.limiter(|s| &mut s.limiter);

            // Set epoch deadline for interruption if enabled
            if enable_epochs {
                store.set_epoch_deadline(epoch_deadline);
            }

            // Apply fuel budget if configured, otherwise set unlimited fuel.
            // consume_fuel(true) is set on the engine, so stores start with 0 fuel
            // and will immediately trap unless fuel is explicitly provided.
            if resource_limits.max_fuel > 0 {
                store
                    .set_fuel(resource_limits.max_fuel)
                    .map_err(|e| format!("failed to set fuel: {e}"))?;
                tracing::debug!(fuel = resource_limits.max_fuel, "WASM fuel budget set");
            } else {
                store
                    .set_fuel(u64::MAX)
                    .map_err(|e| format!("failed to set default fuel: {e}"))?;
            }

            // Create component linker and add WASIp2 interfaces
            let mut linker: ComponentLinker<WasiP2State> = ComponentLinker::new(&engine);
            wasmtime_wasi::p2::add_to_linker_sync(&mut linker)
                .map_err(|e| format!("failed to add WASIp2 to linker: {e}"))?;

            // Try to instantiate as a wasi:cli/command component
            // This is the standard entry point for CLI-style WASM components
            let instance = linker
                .instantiate(&mut store, &component)
                .map_err(|e| format!("failed to instantiate component: {e}"))?;

            // Helper to create result with captured output
            let make_result = |exit_code: i32| ExecutionResult {
                exit_code,
                stdout: stdout_pipe.contents().to_vec(),
                stderr: stderr_pipe.contents().to_vec(),
            };

            // Try to get the wasi:cli/run export first (preferred for wasip2)
            // The run function has signature: func run() -> result<_, error>
            if let Some(run_func) = instance.get_func(&mut store, "wasi:cli/run@0.2.0#run") {
                // Call the run function
                match run_func.call(&mut store, &[], &mut []) {
                    Ok(()) => return Ok(make_result(0)),
                    Err(e) => {
                        // Check for WASI exit
                        if let Some(exit) = e.downcast_ref::<wasmtime_wasi::I32Exit>() {
                            return Ok(make_result(exit.0));
                        }
                        return Err(format!("wasi:cli/run execution error: {e}"));
                    }
                }
            }

            // Fall back to _start if run is not found (for wasip1-style components)
            if let Some(start_func) = instance.get_func(&mut store, "_start") {
                match start_func.call(&mut store, &[], &mut []) {
                    Ok(()) => return Ok(make_result(0)),
                    Err(e) => {
                        // Check for WASI exit
                        if let Some(exit) = e.downcast_ref::<wasmtime_wasi::I32Exit>() {
                            return Ok(make_result(exit.0));
                        }
                        return Err(format!("_start execution error: {e}"));
                    }
                }
            }

            // Try main as last resort
            if let Some(main_func) = instance.get_func(&mut store, "main") {
                match main_func.call(&mut store, &[], &mut []) {
                    Ok(()) => return Ok(make_result(0)),
                    Err(e) => {
                        if let Some(exit) = e.downcast_ref::<wasmtime_wasi::I32Exit>() {
                            return Ok(make_result(exit.0));
                        }
                        return Err(format!("main execution error: {e}"));
                    }
                }
            }

            Err("no wasi:cli/run, _start, or main function found in component".to_string())
        })
        .await
        .map_err(|e| format!("task join error: {e}"))?
    }
}

#[async_trait::async_trait]
impl Runtime for WasmRuntime {
    /// Pull a WASM image from a registry
    ///
    /// This pulls the WASM artifact and caches the binary locally.
    #[instrument(
        skip(self),
        fields(
            otel.name = "wasm.pull",
            container.image.name = %image,
        )
    )]
    async fn pull_image(&self, image: &str) -> Result<()> {
        self.pull_image_with_policy(image, PullPolicy::IfNotPresent, None)
            .await
    }

    /// Pull a WASM image with a specific policy.
    ///
    /// The `_auth` parameter is accepted for trait conformance (§3.10) but
    /// currently ignored: the WASM runtime resolves credentials through the
    /// existing `AuthResolver` (hostname-based lookup in the secret store).
    /// Callers that need inline auth should use the Docker runtime.
    #[instrument(
        skip(self, _auth),
        fields(
            otel.name = "wasm.pull",
            container.image.name = %image,
            pull_policy = ?policy,
        )
    )]
    async fn pull_image_with_policy(
        &self,
        image: &str,
        policy: PullPolicy,
        _auth: Option<&RegistryAuth>,
    ) -> Result<()> {
        // Handle Never policy
        if matches!(policy, PullPolicy::Never) {
            tracing::debug!(image = %image, "pull policy is Never, skipping pull");
            return Ok(());
        }

        // For IfNotPresent, check if we have the WASM cached
        let cache_key = image.replace(['/', ':', '@'], "_");
        let cache_path = self.config.cache_dir.join(format!("{cache_key}.wasm"));

        if matches!(policy, PullPolicy::IfNotPresent) && cache_path.exists() {
            tracing::debug!(image = %image, "WASM module already cached");
            return Ok(());
        }

        let auth = self.auth_resolver.resolve(image);

        tracing::info!(image = %image, "pulling WASM artifact from registry");

        // Pull WASM binary from registry
        let (wasm_bytes, wasm_info) =
            self.registry
                .pull_wasm(image, &auth)
                .await
                .map_err(|e| AgentError::PullFailed {
                    image: image.to_string(),
                    reason: format!("failed to pull WASM artifact: {e}"),
                })?;

        // Cache the WASM binary
        tokio::fs::write(&cache_path, &wasm_bytes)
            .await
            .map_err(|e| AgentError::PullFailed {
                image: image.to_string(),
                reason: format!("failed to cache WASM binary: {e}"),
            })?;

        tracing::info!(
            image = %image,
            wasi_version = %wasm_info.wasi_version,
            size = wasm_bytes.len(),
            "WASM artifact pulled and cached"
        );

        Ok(())
    }

    /// Create a WASM container (compile and prepare for execution)
    #[instrument(
        skip(self, spec),
        fields(
            otel.name = "wasm.create",
            container.id = %self.instance_id(id),
            service.name = %id.service,
            container.image.name = %spec.image.name,
        )
    )]
    async fn create_container(&self, id: &ContainerId, spec: &ServiceSpec) -> Result<()> {
        let instance_id = self.instance_id(id);
        let image = spec.image.name.to_string();

        tracing::info!(
            instance = %instance_id,
            image = %image,
            "creating WASM instance"
        );

        // Load WASM binary from cache or pull
        let cache_key = image.replace(['/', ':', '@'], "_");
        let cache_path = self.config.cache_dir.join(format!("{cache_key}.wasm"));

        // Track whether we loaded from local cache (vs pulled from registry)
        let loaded_from_cache = cache_path.exists();

        let (module_bytes, wasi_version) = if loaded_from_cache {
            // Read from local cache
            let bytes =
                tokio::fs::read(&cache_path)
                    .await
                    .map_err(|e| AgentError::CreateFailed {
                        id: instance_id.clone(),
                        reason: format!("failed to read cached WASM: {e}"),
                    })?;
            // Detect WASI version from the binary itself
            let detected_version = detect_wasm_version_from_binary(&bytes);
            tracing::debug!(
                instance = %instance_id,
                wasi_version = %detected_version,
                "detected WASI version from cached binary"
            );
            (bytes, detected_version)
        } else {
            // Pull from registry - get WASI version from manifest
            let auth = self.auth_resolver.resolve(&image);
            let (wasm_bytes, wasm_info) =
                self.registry.pull_wasm(&image, &auth).await.map_err(|e| {
                    AgentError::CreateFailed {
                        id: instance_id.clone(),
                        reason: format!("failed to pull WASM: {e}"),
                    }
                })?;

            tokio::fs::write(&cache_path, &wasm_bytes)
                .await
                .map_err(|e| AgentError::CreateFailed {
                    id: instance_id.clone(),
                    reason: format!("failed to cache WASM: {e}"),
                })?;

            (wasm_bytes, wasm_info.wasi_version)
        };

        // Build environment and args
        let env_vars = self.build_env_vars(spec);
        let args = self.build_args(spec);

        // Clone storage mounts from spec
        let mounts = spec.storage.clone();

        // Log mount configuration
        if !mounts.is_empty() {
            tracing::info!(
                instance = %instance_id,
                mount_count = mounts.len(),
                "WASM instance has filesystem mounts configured"
            );
        }

        // Extract resource limits from the spec-level WasmConfig
        let resource_limits = ResourceLimits::from_spec(spec.wasm.as_ref());

        if resource_limits.store_limits.is_some() || resource_limits.max_fuel > 0 {
            tracing::info!(
                instance = %instance_id,
                has_memory_limit = resource_limits.store_limits.is_some(),
                max_fuel = resource_limits.max_fuel,
                has_epoch_interval = resource_limits.epoch_interval.is_some(),
                "WASM resource limits configured from spec"
            );
        }

        // Extract capabilities from the spec-level WasmConfig, falling back
        // to ServiceType defaults if no explicit capabilities are provided.
        // This ensures each WASM service type gets its appropriate default
        // security sandbox even when capabilities aren't explicitly configured.
        let capabilities = spec
            .wasm
            .as_ref()
            .and_then(|w| w.capabilities.clone())
            .or_else(|| spec.service_type.default_wasm_capabilities());

        if let Some(ref caps) = capabilities {
            tracing::info!(
                instance = %instance_id,
                config = caps.config,
                keyvalue = caps.keyvalue,
                logging = caps.logging,
                secrets = caps.secrets,
                metrics = caps.metrics,
                http_client = caps.http_client,
                cli = caps.cli,
                filesystem = caps.filesystem,
                sockets = caps.sockets,
                "WASM capabilities configured"
            );
        }

        // Create instance entry
        let instance = WasmInstance {
            state: InstanceState::Pending,
            image: image.clone(),
            module_bytes,
            wasi_version,
            stdout: Vec::new(),
            stderr: Vec::new(),
            env_vars,
            args,
            mounts,
            resource_limits,
            capabilities,
            execution_handle: None,
        };

        // Store instance
        {
            let mut instances = self.instances.write().await;
            instances.insert(instance_id.clone(), instance);
        }

        tracing::info!(
            instance = %instance_id,
            "WASM instance created"
        );

        Ok(())
    }

    /// Start a WASM container (begin execution)
    #[instrument(
        skip(self),
        fields(
            otel.name = "wasm.start",
            container.id = %self.instance_id(id),
            service.name = %id.service,
        )
    )]
    async fn start_container(&self, id: &ContainerId) -> Result<()> {
        let instance_id = self.instance_id(id);

        tracing::info!(instance = %instance_id, "starting WASM instance");

        // Get instance and extract data for execution
        let (wasm_bytes, wasi_version, env_vars, args, mounts, resource_limits, capabilities) = {
            let mut instances = self.instances.write().await;
            let instance = instances
                .get_mut(&instance_id)
                .ok_or_else(|| AgentError::NotFound {
                    container: instance_id.clone(),
                    reason: "WASM instance not found".to_string(),
                })?;

            // Update state to running
            instance.state = InstanceState::Running {
                started_at: Instant::now(),
            };

            (
                instance.module_bytes.clone(),
                instance.wasi_version.clone(),
                instance.env_vars.clone(),
                instance.args.clone(),
                instance.mounts.clone(),
                instance.resource_limits.clone(),
                instance.capabilities.clone(),
            )
        };

        // Detect if this is a component (WASIp2) or module (WASIp1) from the binary
        // The stored wasi_version from manifest takes precedence, but we also check binary
        let is_component = match &wasi_version {
            WasiVersion::Preview2 => true,
            WasiVersion::Preview1 => false,
            WasiVersion::Unknown => {
                // Fall back to binary detection
                let detected = detect_wasm_version_from_binary(&wasm_bytes);
                detected.is_preview2()
            }
        };

        tracing::info!(
            instance = %instance_id,
            wasi_version = %wasi_version,
            is_component = is_component,
            mount_count = mounts.len(),
            "starting WASM execution"
        );

        // Spawn execution task based on component vs module
        let engine = self.engine.clone();
        let epoch_deadline = self.config.epoch_deadline;
        let enable_epochs = self.config.enable_epochs;
        let instance_id_clone = instance_id.clone();

        let handle = if is_component {
            tokio::spawn(async move {
                Self::execute_component(
                    engine,
                    wasm_bytes,
                    env_vars,
                    args,
                    mounts,
                    epoch_deadline,
                    enable_epochs,
                    resource_limits,
                    capabilities,
                )
                .await
            })
        } else {
            tokio::spawn(async move {
                Self::execute_module(
                    engine,
                    wasm_bytes,
                    env_vars,
                    args,
                    mounts,
                    epoch_deadline,
                    enable_epochs,
                    resource_limits,
                    capabilities,
                )
                .await
            })
        };

        // Store handle and update state
        {
            let mut instances = self.instances.write().await;
            if let Some(instance) = instances.get_mut(&instance_id) {
                instance.execution_handle = Some(handle);
            }
        }

        tracing::info!(
            instance = %instance_id_clone,
            "WASM instance started"
        );

        Ok(())
    }

    /// Stop a WASM container (cancel execution)
    #[instrument(
        skip(self),
        fields(
            otel.name = "wasm.stop",
            container.id = %self.instance_id(id),
            service.name = %id.service,
        )
    )]
    async fn stop_container(&self, id: &ContainerId, timeout: Duration) -> Result<()> {
        let instance_id = self.instance_id(id);

        tracing::info!(
            instance = %instance_id,
            timeout = ?timeout,
            "stopping WASM instance"
        );

        // Get and abort the execution handle
        let handle = {
            let mut instances = self.instances.write().await;
            let instance = instances
                .get_mut(&instance_id)
                .ok_or_else(|| AgentError::NotFound {
                    container: instance_id.clone(),
                    reason: "WASM instance not found".to_string(),
                })?;

            // If not running, nothing to stop
            if !matches!(instance.state, InstanceState::Running { .. }) {
                return Ok(());
            }

            instance.execution_handle.take()
        };

        // Abort the execution if we have a handle
        if let Some(handle) = handle {
            // Wait for graceful completion or abort after timeout
            let result = tokio::time::timeout(timeout, handle).await;

            match result {
                Ok(Ok(Ok(exec_result))) => {
                    let mut instances = self.instances.write().await;
                    if let Some(instance) = instances.get_mut(&instance_id) {
                        // Update captured stdout/stderr if available
                        instance.stdout = exec_result.stdout;
                        instance.stderr = exec_result.stderr;
                        instance.state = InstanceState::Completed {
                            exit_code: exec_result.exit_code,
                        };
                    }
                }
                Ok(Ok(Err(e))) => {
                    let mut instances = self.instances.write().await;
                    if let Some(instance) = instances.get_mut(&instance_id) {
                        instance.state = InstanceState::Failed { reason: e };
                    }
                }
                Ok(Err(join_error)) => {
                    let mut instances = self.instances.write().await;
                    if let Some(instance) = instances.get_mut(&instance_id) {
                        instance.state = InstanceState::Failed {
                            reason: format!("task join error: {join_error}"),
                        };
                    }
                }
                Err(_timeout) => {
                    // Timeout - mark as failed
                    let mut instances = self.instances.write().await;
                    if let Some(instance) = instances.get_mut(&instance_id) {
                        instance.state = InstanceState::Failed {
                            reason: "execution timed out".to_string(),
                        };
                    }
                }
            }
        }

        tracing::info!(instance = %instance_id, "WASM instance stopped");

        Ok(())
    }

    /// Remove a WASM container (cleanup)
    #[instrument(
        skip(self),
        fields(
            otel.name = "wasm.remove",
            container.id = %self.instance_id(id),
            service.name = %id.service,
        )
    )]
    async fn remove_container(&self, id: &ContainerId) -> Result<()> {
        let instance_id = self.instance_id(id);

        tracing::info!(instance = %instance_id, "removing WASM instance");

        // Remove from instances map
        let mut instances = self.instances.write().await;
        if let Some(mut instance) = instances.remove(&instance_id) {
            // Abort any running execution
            if let Some(handle) = instance.execution_handle.take() {
                handle.abort();
            }
        }

        tracing::info!(instance = %instance_id, "WASM instance removed");

        Ok(())
    }

    /// Get container state
    #[instrument(
        skip(self),
        fields(
            otel.name = "wasm.state",
            container.id = %self.instance_id(id),
        )
    )]
    async fn container_state(&self, id: &ContainerId) -> Result<ContainerState> {
        let instance_id = self.instance_id(id);

        let instances = self.instances.read().await;
        let instance = instances
            .get(&instance_id)
            .ok_or_else(|| AgentError::NotFound {
                container: instance_id.clone(),
                reason: "WASM instance not found".to_string(),
            })?;

        let state = match &instance.state {
            InstanceState::Pending => ContainerState::Pending,
            InstanceState::Running { .. } => ContainerState::Running,
            InstanceState::Completed { exit_code } => ContainerState::Exited { code: *exit_code },
            InstanceState::Failed { reason } => ContainerState::Failed {
                reason: reason.clone(),
            },
        };

        Ok(state)
    }

    /// Get container logs
    async fn container_logs(&self, id: &ContainerId, tail: usize) -> Result<Vec<LogEntry>> {
        let instance_id = self.instance_id(id);

        let instances = self.instances.read().await;
        let instance = instances
            .get(&instance_id)
            .ok_or_else(|| AgentError::NotFound {
                container: instance_id.clone(),
                reason: "WASM instance not found".to_string(),
            })?;

        let source = LogSource::Container(instance_id.clone());
        let mut entries = Vec::new();

        for line in String::from_utf8_lossy(&instance.stdout).lines() {
            entries.push(LogEntry {
                timestamp: chrono::Utc::now(),
                stream: LogStream::Stdout,
                message: line.to_string(),
                source: source.clone(),
                service: Some(id.service.clone()),
                deployment: None,
            });
        }

        for line in String::from_utf8_lossy(&instance.stderr).lines() {
            entries.push(LogEntry {
                timestamp: chrono::Utc::now(),
                stream: LogStream::Stderr,
                message: line.to_string(),
                source: source.clone(),
                service: Some(id.service.clone()),
                deployment: None,
            });
        }

        // Apply tail limit
        if tail > 0 && entries.len() > tail {
            entries = entries.split_off(entries.len() - tail);
        }

        Ok(entries)
    }

    /// Execute command in container
    ///
    /// WASM modules don't support exec - return an error.
    async fn exec(&self, id: &ContainerId, _cmd: &[String]) -> Result<(i32, String, String)> {
        let instance_id = self.instance_id(id);

        Err(AgentError::Internal(format!(
            "exec not supported for WASM instance '{instance_id}': WASM modules are single-process and don't support exec"
        )))
    }

    /// Get container resource statistics
    ///
    /// # Design Decision: Empty Statistics
    ///
    /// WASM modules run in-process within the wasmtime runtime and do not have
    /// kernel-level isolation like containers. As a result:
    ///
    /// - **No cgroup stats**: WASM has no cgroup, so CPU/memory accounting at the
    ///   kernel level is not available.
    /// - **Shared process memory**: WASM linear memory is part of the host process
    ///   heap, making per-instance memory measurement impractical.
    /// - **CPU time**: While wasmtime tracks fuel/epochs for interruption, it does
    ///   not expose CPU time metrics in a format compatible with cgroup stats.
    ///
    /// For WASM resource monitoring, consider:
    /// - Using wasmtime's fuel metering for instruction-level accounting
    /// - Monitoring the host process's overall resource usage
    /// - Implementing application-level metrics within the WASM module
    ///
    /// Returns zero values for all metrics with `u64::MAX` as the memory limit
    /// (indicating no limit), which is semantically correct for WASM modules.
    async fn get_container_stats(&self, id: &ContainerId) -> Result<ContainerStats> {
        let instance_id = self.instance_id(id);

        // Check instance exists
        let instances = self.instances.read().await;
        if !instances.contains_key(&instance_id) {
            return Err(AgentError::NotFound {
                container: instance_id.clone(),
                reason: "WASM instance not found".to_string(),
            });
        }

        // Return empty stats - WASM has no cgroup isolation for kernel-level resource tracking
        Ok(ContainerStats {
            cpu_usage_usec: 0,
            memory_bytes: 0,
            memory_limit: u64::MAX,
            timestamp: Instant::now(),
        })
    }

    /// Wait for container to exit
    async fn wait_container(&self, id: &ContainerId) -> Result<i32> {
        let instance_id = self.instance_id(id);

        tracing::debug!(instance = %instance_id, "waiting for WASM instance to exit");

        // Poll state until exited
        let poll_interval = Duration::from_millis(100);
        let max_wait = self.config.max_execution_time;
        let start = Instant::now();

        loop {
            if start.elapsed() > max_wait {
                return Err(AgentError::Timeout { timeout: max_wait });
            }

            // Check if we need to poll execution result
            {
                let mut instances = self.instances.write().await;
                if let Some(instance) = instances.get_mut(&instance_id) {
                    // Check if handle completed
                    if let Some(handle) = &mut instance.execution_handle {
                        if handle.is_finished() {
                            let handle = instance.execution_handle.take().unwrap();
                            match handle.await {
                                Ok(Ok(exec_result)) => {
                                    // Update captured stdout/stderr if available
                                    instance.stdout = exec_result.stdout;
                                    instance.stderr = exec_result.stderr;
                                    instance.state = InstanceState::Completed {
                                        exit_code: exec_result.exit_code,
                                    };
                                }
                                Ok(Err(e)) => {
                                    instance.state = InstanceState::Failed { reason: e };
                                }
                                Err(e) => {
                                    instance.state = InstanceState::Failed {
                                        reason: format!("task join error: {e}"),
                                    };
                                }
                            }
                        }
                    }

                    // Check state
                    match &instance.state {
                        InstanceState::Completed { exit_code } => {
                            return Ok(*exit_code);
                        }
                        InstanceState::Failed { reason } => {
                            return Err(AgentError::Internal(format!(
                                "WASM execution failed: {reason}"
                            )));
                        }
                        InstanceState::Pending | InstanceState::Running { .. } => {
                            // Continue waiting
                        }
                    }
                } else {
                    return Err(AgentError::NotFound {
                        container: instance_id.clone(),
                        reason: "WASM instance not found".to_string(),
                    });
                }
            }

            tokio::time::sleep(poll_interval).await;
        }
    }

    /// Get container logs as structured entries
    async fn get_logs(&self, id: &ContainerId) -> Result<Vec<LogEntry>> {
        // Delegate to container_logs with tail=0 (all lines)
        self.container_logs(id, 0).await
    }

    /// Get the PID of a WASM instance
    ///
    /// WASM modules run in-process within the wasmtime runtime - they don't have
    /// their own OS process. Therefore, this method always returns `None`.
    ///
    /// This is correct behavior because:
    /// - WASM workloads don't need overlay network attachment since they run in-process
    /// - There is no separate process to track or manage
    /// - The calling code should handle `None` gracefully (skip overlay attachment)
    async fn get_container_pid(&self, _id: &ContainerId) -> Result<Option<u32>> {
        // WASM modules run in-process, they don't have their own PID
        // Return None to indicate no separate process exists
        Ok(None)
    }

    async fn get_container_ip(&self, _id: &ContainerId) -> Result<Option<std::net::IpAddr>> {
        // WASM modules run in-process and bind directly to host ports via WASI sockets.
        // They're reachable at localhost but don't have a separate container IP.
        Ok(Some(std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)))
    }

    /// Abort a running WASM instance.
    ///
    /// WASM modules don't have POSIX signals; the `signal` parameter is
    /// validated for API parity with container backends but otherwise ignored
    /// — any accepted signal triggers a wasmtime interruption via the
    /// execution handle's abort.
    async fn kill_container(&self, id: &ContainerId, signal: Option<&str>) -> Result<()> {
        let _canonical = crate::runtime::validate_signal(signal.unwrap_or("SIGKILL"))?;
        let instance_id = self.instance_id(id);

        tracing::info!(instance = %instance_id, "killing WASM instance");

        let mut instances = self.instances.write().await;
        let instance = instances
            .get_mut(&instance_id)
            .ok_or_else(|| AgentError::NotFound {
                container: instance_id.clone(),
                reason: "WASM instance not found".to_string(),
            })?;

        if !matches!(instance.state, InstanceState::Running { .. }) {
            // Idempotent: already terminal, nothing to do.
            return Ok(());
        }

        if let Some(handle) = instance.execution_handle.take() {
            handle.abort();
        }
        instance.state = InstanceState::Failed {
            reason: "killed".to_string(),
        };
        Ok(())
    }

    /// Tag a cached WASM image.
    ///
    /// The WASM runtime caches modules on disk keyed by the source reference.
    /// Tagging re-uses that cache entry under the new reference so subsequent
    /// pulls with the new reference resolve locally.
    async fn tag_image(&self, source: &str, target: &str) -> Result<()> {
        if source.trim().is_empty() || target.trim().is_empty() {
            return Err(AgentError::InvalidSpec(
                "source and target must be non-empty image references".to_string(),
            ));
        }
        if source == target {
            return Ok(());
        }

        let sanitize = |s: &str| s.replace(['/', ':', '@'], "_");
        let src_path = self
            .config
            .cache_dir
            .join(format!("{}.wasm", sanitize(source)));
        let dst_path = self
            .config
            .cache_dir
            .join(format!("{}.wasm", sanitize(target)));

        if !src_path.exists() {
            return Err(AgentError::NotFound {
                container: source.to_string(),
                reason: format!(
                    "source WASM module '{source}' not found in cache ({})",
                    src_path.display()
                ),
            });
        }

        // Overwrite dst_path by copying the module bytes. Copying (vs. hard
        // linking) keeps behaviour identical across filesystems.
        tokio::fs::copy(&src_path, &dst_path).await.map_err(|e| {
            AgentError::Internal(format!(
                "failed to tag WASM module '{source}' -> '{target}': {e}"
            ))
        })?;

        tracing::info!(source = %source, target = %target, "tagged WASM module");
        Ok(())
    }
}

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

    #[test]
    fn test_wasm_config_default() {
        let config = WasmConfig::default();

        assert_eq!(
            config.cache_dir,
            zlayer_paths::ZLayerDirs::system_default().wasm()
        );
        assert!(config.enable_epochs);
        assert_eq!(config.epoch_deadline, 1_000_000);
        assert_eq!(config.max_execution_time, Duration::from_secs(3600));
        assert!(config.cache_type.is_none());
    }

    #[test]
    fn test_instance_id_generation() {
        let id = ContainerId {
            service: "myservice".to_string(),
            replica: 1,
        };

        let expected = "wasm-myservice-1";
        let result = format!("wasm-{}-{}", id.service, id.replica);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_cache_key_sanitization() {
        let images = vec![
            ("ghcr.io/org/module:v1.0", "ghcr.io_org_module_v1.0"),
            (
                "registry.example.com/wasm@sha256:abc",
                "registry.example.com_wasm_sha256_abc",
            ),
        ];

        for (image, expected) in images {
            let sanitized = image.replace(['/', ':', '@'], "_");
            assert_eq!(sanitized, expected);
        }
    }

    #[test]
    fn test_instance_state_debug() {
        let pending = InstanceState::Pending;
        let running = InstanceState::Running {
            started_at: Instant::now(),
        };
        let completed = InstanceState::Completed { exit_code: 0 };
        let failed = InstanceState::Failed {
            reason: "test error".to_string(),
        };

        // Ensure Debug trait is implemented
        assert!(!format!("{pending:?}").is_empty());
        assert!(!format!("{running:?}").is_empty());
        assert!(!format!("{completed:?}").is_empty());
        assert!(!format!("{failed:?}").is_empty());
    }

    #[test]
    fn test_wasm_config_clone() {
        let config = WasmConfig {
            cache_dir: PathBuf::from("/custom/cache"),
            enable_epochs: false,
            epoch_deadline: 500_000,
            max_execution_time: Duration::from_secs(60),
            cache_type: Some(zlayer_registry::CacheType::Memory),
        };

        let cloned = config.clone();

        assert_eq!(cloned.cache_dir, config.cache_dir);
        assert_eq!(cloned.enable_epochs, config.enable_epochs);
        assert_eq!(cloned.epoch_deadline, config.epoch_deadline);
        assert_eq!(cloned.max_execution_time, config.max_execution_time);
        assert!(cloned.cache_type.is_some());
    }

    #[tokio::test]
    async fn test_wasm_network_enabled() {
        // Verify that WasiCtxBuilder with inherit_network compiles and works
        // This ensures the wasi:sockets interfaces are properly available
        let mut builder = WasiCtxBuilder::new();
        builder.inherit_network();
        let _ctx = builder.build();
        // If this compiles and runs, networking is properly configured
    }

    #[test]
    fn test_parse_memory_limit_valid() {
        assert_eq!(parse_memory_limit("64Mi").unwrap(), 64 * 1024 * 1024);
        assert_eq!(parse_memory_limit("256Mi").unwrap(), 256 * 1024 * 1024);
        assert_eq!(parse_memory_limit("1Gi").unwrap(), 1024 * 1024 * 1024);
        assert_eq!(
            parse_memory_limit("2Ti").unwrap(),
            2 * 1024 * 1024 * 1024 * 1024
        );
        assert_eq!(parse_memory_limit("512Ki").unwrap(), 512 * 1024);
    }

    #[test]
    fn test_parse_memory_limit_with_whitespace() {
        assert_eq!(parse_memory_limit("  64Mi  ").unwrap(), 64 * 1024 * 1024);
    }

    #[test]
    fn test_parse_memory_limit_invalid() {
        assert!(parse_memory_limit("").is_err());
        assert!(parse_memory_limit("64").is_err());
        assert!(parse_memory_limit("Mi").is_err());
        assert!(parse_memory_limit("64MB").is_err());
        assert!(parse_memory_limit("abcMi").is_err());
    }

    #[test]
    fn test_build_store_limits_with_memory() {
        let wasm_config = zlayer_spec::WasmConfig {
            max_memory: Some("64Mi".to_string()),
            ..Default::default()
        };
        let limits = build_store_limits(Some(&wasm_config));
        assert!(limits.is_some());
    }

    #[test]
    fn test_build_store_limits_without_memory() {
        let wasm_config = zlayer_spec::WasmConfig::default();
        let limits = build_store_limits(Some(&wasm_config));
        assert!(limits.is_none());
    }

    #[test]
    fn test_build_store_limits_none_config() {
        let limits = build_store_limits(None);
        assert!(limits.is_none());
    }

    #[test]
    fn test_resource_limits_from_spec() {
        let wasm_config = zlayer_spec::WasmConfig {
            max_memory: Some("128Mi".to_string()),
            max_fuel: 1_000_000,
            epoch_interval: Some(Duration::from_millis(100)),
            ..Default::default()
        };
        let limits = ResourceLimits::from_spec(Some(&wasm_config));
        assert!(limits.store_limits.is_some());
        assert_eq!(limits.max_fuel, 1_000_000);
        assert_eq!(limits.epoch_interval, Some(Duration::from_millis(100)));
    }

    #[test]
    fn test_resource_limits_from_spec_none() {
        let limits = ResourceLimits::from_spec(None);
        assert!(limits.store_limits.is_none());
        assert_eq!(limits.max_fuel, 0);
        assert!(limits.epoch_interval.is_none());
    }
}