shape-abi-v1 0.3.0

Stable host ABI v1 for Shape capability modules
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
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
//! Shape ABI v1
//!
//! Stable C ABI for host-loadable Shape capability modules.
//! Current capability families include data sources and output sinks.
//!
//! # Design Principles
//!
//! - **Stable C ABI**: Uses `#[repr(C)]` for binary compatibility across Rust versions
//! - **Self-Describing**: Plugins declare their query parameters and output fields
//! - **MessagePack Serialization**: Data exchange uses compact binary format
//! - **Binary Columnar Format**: High-performance direct loading (ABI v2)
//! - **Platform-Agnostic**: Works on native targets
//!
//! # Creating a Data Capability Module
//!
//! ```ignore
//! use shape_abi_v1::*;
//!
//! // Define your plugin info
//! #[no_mangle]
//! pub extern "C" fn shape_plugin_info() -> *const PluginInfo {
//!     static INFO: PluginInfo = PluginInfo {
//!         name: c"my-data-source".as_ptr(),
//!         version: c"1.0.0".as_ptr(),
//!         plugin_type: PluginType::DataSource,
//!         description: c"My custom data source".as_ptr(),
//!     };
//!     &INFO
//! }
//!
//! // Optional but recommended: capability manifest
//! #[no_mangle]
//! pub extern "C" fn shape_capability_manifest() -> *const CapabilityManifest { ... }
//!
//! // Implement the vtable functions...
//! ```

pub mod binary_builder;
pub mod binary_format;

use std::ffi::{c_char, c_void};

// ============================================================================
// Plugin Metadata
// ============================================================================

/// Plugin metadata returned by `shape_plugin_info()`
#[repr(C)]
pub struct PluginInfo {
    /// Plugin name (null-terminated C string)
    pub name: *const c_char,
    /// Plugin version (null-terminated C string, semver format)
    pub version: *const c_char,
    /// Type of plugin
    pub plugin_type: PluginType,
    /// Human-readable description (null-terminated C string)
    pub description: *const c_char,
}

// Safety: PluginInfo contains only const pointers to static strings
// The strings are never modified through these pointers
unsafe impl Sync for PluginInfo {}

/// Type of plugin
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PluginType {
    /// Data source that provides time-series data
    DataSource = 0,
    /// Output sink for alerts and events
    OutputSink = 1,
    /// Language runtime for polyglot interop (Python, TypeScript, etc.)
    LanguageRuntime = 2,
}

/// Capability family exposed by a plugin/module.
///
/// This is intentionally broader than connector-specific concepts so the same
/// ABI can describe data, sinks, compute kernels, model runtimes, etc.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapabilityKind {
    /// Data source/query provider capability.
    DataSource = 0,
    /// Output sink capability for alerts/events.
    OutputSink = 1,
    /// Generic compute kernel capability.
    Compute = 2,
    /// Model/inference runtime capability.
    Model = 3,
    /// Language runtime capability for foreign function blocks.
    LanguageRuntime = 4,
    /// Catch-all for custom capability families.
    Custom = 255,
}

/// Canonical contract name for the built-in data source capability.
pub const CAPABILITY_DATA_SOURCE: &str = "shape.datasource";
/// Canonical contract name for the built-in output sink capability.
pub const CAPABILITY_OUTPUT_SINK: &str = "shape.output_sink";
/// Canonical contract name for the base module capability.
pub const CAPABILITY_MODULE: &str = "shape.module";
/// Canonical contract name for the language runtime capability.
pub const CAPABILITY_LANGUAGE_RUNTIME: &str = "shape.language_runtime";

/// Declares one capability contract implemented by the plugin.
#[repr(C)]
pub struct CapabilityDescriptor {
    /// Capability family.
    pub kind: CapabilityKind,
    /// Contract name (null-terminated C string), e.g. "shape.datasource".
    pub contract: *const c_char,
    /// Contract version (null-terminated C string), e.g. "1".
    pub version: *const c_char,
    /// Reserved capability flags (set to 0 for now).
    pub flags: u64,
}

// Safety: contains only const pointers to static strings.
unsafe impl Sync for CapabilityDescriptor {}

/// Capability manifest returned by `shape_capability_manifest()`.
#[repr(C)]
pub struct CapabilityManifest {
    /// Array of capability descriptors.
    pub capabilities: *const CapabilityDescriptor,
    /// Number of capability descriptors.
    pub capabilities_len: usize,
}

// Safety: contains only const pointers to static data.
unsafe impl Sync for CapabilityManifest {}

// ============================================================================
// Extension Section Claims
// ============================================================================

/// Declares a TOML section claimed by an extension.
///
/// Extensions use this to declare custom config sections in `shape.toml`
/// (e.g., `[native-dependencies]`) without coupling domain-specific concepts
/// into core Shape.
#[repr(C)]
pub struct SectionClaim {
    /// Section name (null-terminated C string), e.g. "native-dependencies"
    pub name: *const c_char,
    /// Whether absence of the section is an error (true) or silently ignored (false)
    pub required: bool,
}

// Safety: SectionClaim contains only const pointers to static strings
unsafe impl Sync for SectionClaim {}

/// Manifest of TOML sections claimed by an extension.
///
/// Returned by the optional `shape_claimed_sections` export. Extensions that
/// don't need custom sections simply omit this export (backwards compatible).
#[repr(C)]
pub struct SectionsManifest {
    /// Array of section claims.
    pub sections: *const SectionClaim,
    /// Number of section claims.
    pub sections_len: usize,
}

// Safety: SectionsManifest contains only const pointers to static data
unsafe impl Sync for SectionsManifest {}

/// Type signature for optional `shape_claimed_sections` export.
///
/// Extensions that need custom TOML sections export this symbol. It is
/// optional — omitting it is valid and means the extension claims no sections.
pub type GetClaimedSectionsFn = unsafe extern "C" fn() -> *const SectionsManifest;

// ============================================================================
// Self-Describing Query Schema
// ============================================================================

/// Parameter types that a data source can accept in queries
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamType {
    /// String value
    String = 0,
    /// Numeric value (f64)
    Number = 1,
    /// Boolean value
    Bool = 2,
    /// Array of strings
    StringArray = 3,
    /// Array of numbers
    NumberArray = 4,
    /// Nested object with its own schema
    Object = 5,
    /// Timestamp (i64 milliseconds since epoch)
    Timestamp = 6,
    /// Duration (f64 seconds)
    Duration = 7,
}

/// Describes a single query parameter
///
/// Plugins use this to declare what parameters they accept,
/// enabling LSP autocomplete and validation.
#[repr(C)]
pub struct QueryParam {
    /// Parameter name (e.g., "symbol", "device_type", "table")
    pub name: *const c_char,

    /// Human-readable description
    pub description: *const c_char,

    /// Parameter type
    pub param_type: ParamType,

    /// Is this parameter required?
    pub required: bool,

    /// Default value (MessagePack encoded, null if no default)
    pub default_value: *const u8,
    /// Length of default_value bytes
    pub default_value_len: usize,

    /// For enum-like params: allowed values (MessagePack array, null if any value allowed)
    pub allowed_values: *const u8,
    /// Length of allowed_values bytes
    pub allowed_values_len: usize,

    /// For Object type: nested schema (pointer to QuerySchema, null otherwise)
    pub nested_schema: *const QuerySchema,
}

// Safety: QueryParam contains only const pointers to static data
// The data is never modified through these pointers
unsafe impl Sync for QueryParam {}

/// Complete schema describing all query parameters for a data source
#[repr(C)]
pub struct QuerySchema {
    /// Array of parameter definitions
    pub params: *const QueryParam,
    /// Number of parameters
    pub params_len: usize,

    /// Example query (MessagePack encoded) for documentation
    pub example_query: *const u8,
    /// Length of example_query bytes
    pub example_query_len: usize,
}

// Safety: QuerySchema contains only const pointers to static data
// The data is never modified through these pointers
unsafe impl Sync for QuerySchema {}

// ============================================================================
// Self-Describing Output Schema
// ============================================================================

/// Describes a single output field produced by the data source
#[repr(C)]
pub struct OutputField {
    /// Field name (e.g., "timestamp", "value", "open", "temperature")
    pub name: *const c_char,

    /// Field type
    pub field_type: ParamType,

    /// Human-readable description
    pub description: *const c_char,
}

// Safety: OutputField contains only const pointers to static strings
// The data is never modified through these pointers
unsafe impl Sync for OutputField {}

/// Schema describing output data structure
#[repr(C)]
pub struct OutputSchema {
    /// Array of field definitions
    pub fields: *const OutputField,
    /// Number of fields
    pub fields_len: usize,
}

// Safety: OutputSchema contains only const pointers to static data
// The data is never modified through these pointers
unsafe impl Sync for OutputSchema {}

// ============================================================================
// Dynamic Schema Discovery (MessagePack-serializable types)
// ============================================================================

/// Data type for schema columns.
///
/// This enum is used in the MessagePack-serialized PluginSchema returned
/// by the `get_source_schema` vtable function.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "PascalCase"))]
pub enum DataType {
    /// Floating-point number
    Number,
    /// Integer value
    Integer,
    /// String value
    String,
    /// Boolean value
    Boolean,
    /// Timestamp (Unix milliseconds)
    Timestamp,
}

/// Information about a single column in the data source.
///
/// This struct is serialized as MessagePack in the response from `get_source_schema`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColumnInfo {
    /// Column name
    pub name: std::string::String,
    /// Column data type
    pub data_type: DataType,
}

/// Schema returned by `get_source_schema` for dynamic schema discovery.
///
/// This struct is serialized as MessagePack. Example:
/// ```json
/// {
///   "columns": [
///     { "name": "timestamp", "data_type": "Timestamp" },
///     { "name": "open", "data_type": "Number" },
///     { "name": "high", "data_type": "Number" },
///     { "name": "low", "data_type": "Number" },
///     { "name": "close", "data_type": "Number" },
///     { "name": "volume", "data_type": "Integer" }
///   ],
///   "timestamp_column": "timestamp"
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PluginSchema {
    /// List of columns provided by this source
    pub columns: Vec<ColumnInfo>,
    /// Which column contains the timestamp/x-axis data
    pub timestamp_column: std::string::String,
}

// ============================================================================
// Module Capability (shape.module)
// ============================================================================

/// Schema for one callable module function.
///
/// This is serialized as MessagePack by module-capability providers.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ModuleFunctionSchema {
    /// Function name as exported in the module namespace.
    pub name: std::string::String,
    /// Human-readable description.
    pub description: std::string::String,
    /// Parameter type names (for signatures/completions).
    pub params: Vec<std::string::String>,
    /// Return type name.
    pub return_type: Option<std::string::String>,
}

/// Module-level schema for a `shape.module` capability.
///
/// Serialized as MessagePack and returned by `get_module_schema`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ModuleSchema {
    /// Module namespace name (e.g., "duckdb").
    pub module_name: std::string::String,
    /// Exported callable functions in this module.
    pub functions: Vec<ModuleFunctionSchema>,
}

// ============================================================================
// Progress Reporting (ABI v2)
// ============================================================================

/// Progress callback function type for reporting load progress.
///
/// Called by plugins during `load_binary` to report progress.
///
/// # Arguments
/// * `phase`: Current phase (0=Connecting, 1=Querying, 2=Fetching, 3=Parsing, 4=Converting)
/// * `rows_processed`: Number of rows processed so far
/// * `total_rows`: Total expected rows (0 if unknown)
/// * `bytes_processed`: Bytes processed so far
/// * `user_data`: User data passed to `load_binary`
///
/// # Returns
/// * 0: Continue loading
/// * Non-zero: Cancel the load operation
pub type ProgressCallbackFn = unsafe extern "C" fn(
    phase: u8,
    rows_processed: u64,
    total_rows: u64,
    bytes_processed: u64,
    user_data: *mut c_void,
) -> i32;

// ============================================================================
// Data Source Plugin VTable
// ============================================================================

/// Function pointer types for data source plugins
#[repr(C)]
pub struct DataSourceVTable {
    /// Initialize the data source with configuration.
    /// `config`: MessagePack-encoded configuration object
    /// Returns: opaque instance pointer, or null on error
    pub init: Option<unsafe extern "C" fn(config: *const u8, config_len: usize) -> *mut c_void>,

    /// Get the query schema for this data source.
    /// Returns a pointer to the QuerySchema struct (must remain valid for plugin lifetime).
    pub get_query_schema: Option<unsafe extern "C" fn(instance: *mut c_void) -> *const QuerySchema>,

    /// Get the output schema for this data source.
    /// Returns a pointer to the OutputSchema struct (must remain valid for plugin lifetime).
    pub get_output_schema:
        Option<unsafe extern "C" fn(instance: *mut c_void) -> *const OutputSchema>,

    /// Query the data schema for a specific source.
    ///
    /// Unlike `get_output_schema` which returns a static schema for the plugin,
    /// this function returns the dynamic schema for a specific data source.
    /// This enables schema discovery at runtime.
    ///
    /// `source_id`: The source identifier (e.g., table name, symbol, device ID)
    /// `out_ptr`: Output pointer to MessagePack-encoded PluginSchema
    /// `out_len`: Output length of the data
    ///
    /// The returned PluginSchema (MessagePack) has structure:
    /// ```json
    /// {
    ///   "columns": [
    ///     { "name": "timestamp", "data_type": "Timestamp" },
    ///     { "name": "value", "data_type": "Number" }
    ///   ],
    ///   "timestamp_column": "timestamp"
    /// }
    /// ```
    ///
    /// Returns: 0 on success, non-zero error code on failure
    /// Caller must free the output buffer with `free_buffer`.
    pub get_source_schema: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            source_id: *const u8,
            source_id_len: usize,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Validate a query before execution.
    /// `query`: MessagePack-encoded query parameters
    /// `out_error`: On error, write error message pointer here (caller must free with `free_string`)
    /// Returns: 0 on success, non-zero error code on failure
    pub validate_query: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            query: *const u8,
            query_len: usize,
            out_error: *mut *mut c_char,
        ) -> i32,
    >,

    /// Load historical data (JSON/MessagePack format - legacy).
    /// `query`: MessagePack-encoded query parameters
    /// `out_ptr`: Output pointer to MessagePack-encoded Series data
    /// `out_len`: Output length of the data
    /// Returns: 0 on success, non-zero error code on failure
    /// Caller must free the output buffer with `free_buffer`.
    pub load: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            query: *const u8,
            query_len: usize,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Load historical data in binary columnar format (ABI v2).
    ///
    /// High-performance data loading that bypasses JSON serialization.
    /// Returns binary data in the format defined by `binary_format` module
    /// that can be directly mapped to SeriesStorage.
    ///
    /// # Arguments
    /// * `instance`: Plugin instance
    /// * `query`: MessagePack-encoded query parameters
    /// * `query_len`: Length of query data
    /// * `granularity`: Progress reporting granularity (0=Coarse, 1=Fine)
    /// * `progress_callback`: Optional callback for progress reporting
    /// * `progress_user_data`: User data passed to progress callback
    /// * `out_ptr`: Output pointer to binary columnar data
    /// * `out_len`: Output length of the data
    ///
    /// Returns: 0 on success, non-zero error code on failure
    /// Caller must free the output buffer with `free_buffer`.
    pub load_binary: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            query: *const u8,
            query_len: usize,
            granularity: u8,
            progress_callback: Option<ProgressCallbackFn>,
            progress_user_data: *mut c_void,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Subscribe to streaming data.
    /// `query`: MessagePack-encoded query parameters
    /// `callback`: Called for each data point (data_ptr, data_len, user_data)
    /// `callback_data`: User data passed to callback
    /// Returns: subscription ID on success, 0 on failure
    pub subscribe: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            query: *const u8,
            query_len: usize,
            callback: unsafe extern "C" fn(*const u8, usize, *mut c_void),
            callback_data: *mut c_void,
        ) -> u64,
    >,

    /// Unsubscribe from streaming data.
    /// `subscription_id`: ID returned by `subscribe`
    /// Returns: 0 on success, non-zero on failure
    pub unsubscribe:
        Option<unsafe extern "C" fn(instance: *mut c_void, subscription_id: u64) -> i32>,

    /// Free a buffer allocated by `load`.
    pub free_buffer: Option<unsafe extern "C" fn(ptr: *mut u8, len: usize)>,

    /// Free an error string allocated by `validate_query`.
    pub free_string: Option<unsafe extern "C" fn(ptr: *mut c_char)>,

    /// Cleanup and destroy the instance.
    pub drop: Option<unsafe extern "C" fn(instance: *mut c_void)>,
}

// ============================================================================
// Output Sink Plugin VTable
// ============================================================================

/// Function pointer types for output sink plugins (alerts, webhooks, etc.)
#[repr(C)]
pub struct OutputSinkVTable {
    /// Initialize the output sink with configuration.
    /// `config`: MessagePack-encoded configuration object
    /// Returns: opaque instance pointer, or null on error
    pub init: Option<unsafe extern "C" fn(config: *const u8, config_len: usize) -> *mut c_void>,

    /// Get the tags this sink handles (for routing).
    /// Returns a MessagePack-encoded array of strings.
    /// Empty array means sink handles all alerts.
    pub get_handled_tags: Option<
        unsafe extern "C" fn(instance: *mut c_void, out_ptr: *mut *mut u8, out_len: *mut usize),
    >,

    /// Send an alert.
    /// `alert`: MessagePack-encoded Alert struct
    /// Returns: 0 on success, non-zero error code on failure
    pub send: Option<
        unsafe extern "C" fn(instance: *mut c_void, alert: *const u8, alert_len: usize) -> i32,
    >,

    /// Flush any pending alerts.
    /// Returns: 0 on success, non-zero error code on failure
    pub flush: Option<unsafe extern "C" fn(instance: *mut c_void) -> i32>,

    /// Free a buffer allocated by `get_handled_tags`.
    pub free_buffer: Option<unsafe extern "C" fn(ptr: *mut u8, len: usize)>,

    /// Cleanup and destroy the instance.
    pub drop: Option<unsafe extern "C" fn(instance: *mut c_void)>,
}

// ============================================================================
// Module Plugin VTable
// ============================================================================

/// Payload kind returned by `ModuleVTable::invoke_ex`.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModuleInvokeResultKind {
    /// MessagePack-encoded `shape_wire::WireValue` payload.
    WireValueMsgpack = 0,
    /// Arrow IPC bytes for a single table result (fast path, no wire envelope).
    TableArrowIpc = 1,
}

/// Extended invoke payload for module capability calls.
#[repr(C)]
pub struct ModuleInvokeResult {
    /// Payload encoding kind.
    pub kind: ModuleInvokeResultKind,
    /// Pointer to plugin-owned payload bytes.
    pub payload_ptr: *mut u8,
    /// Length in bytes of `payload_ptr`.
    pub payload_len: usize,
}

impl ModuleInvokeResult {
    /// Empty invoke result with no payload.
    pub const fn empty() -> Self {
        Self {
            kind: ModuleInvokeResultKind::WireValueMsgpack,
            payload_ptr: core::ptr::null_mut(),
            payload_len: 0,
        }
    }
}

/// Function pointer types for the base module capability (`shape.module`).
#[repr(C)]
pub struct ModuleVTable {
    /// Initialize module instance with MessagePack-encoded config.
    pub init: Option<unsafe extern "C" fn(config: *const u8, config_len: usize) -> *mut c_void>,

    /// Return MessagePack-encoded [`ModuleSchema`].
    ///
    /// The caller must free the output buffer with `free_buffer`.
    pub get_module_schema: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Return MessagePack-encoded module artifacts payload.
    ///
    /// This is an opaque host-defined payload for bundled Shape modules
    /// (source and/or precompiled artifacts). ABI keeps this generic.
    ///
    /// The caller must free the output buffer with `free_buffer`.
    pub get_module_artifacts: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Invoke a module function with MessagePack-encoded `shape_wire::WireValue` array.
    ///
    /// `function` is a UTF-8 function name (bytes).
    /// `args` is a MessagePack-encoded `Vec<shape_wire::WireValue>` payload.
    /// On success, `out_ptr/out_len` contain MessagePack-encoded `shape_wire::WireValue`.
    pub invoke: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            function: *const u8,
            function_len: usize,
            args: *const u8,
            args_len: usize,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Invoke a module function and return a typed payload (`WireValue` or table IPC).
    ///
    /// `function` is a UTF-8 function name (bytes).
    /// `args` is a MessagePack-encoded `Vec<shape_wire::WireValue>` payload.
    /// On success, `out` must be filled with a valid payload descriptor.
    pub invoke_ex: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            function: *const u8,
            function_len: usize,
            args: *const u8,
            args_len: usize,
            out: *mut ModuleInvokeResult,
        ) -> i32,
    >,

    /// Free a buffer allocated by `get_module_schema`, `invoke`, or `invoke_ex`.
    pub free_buffer: Option<unsafe extern "C" fn(ptr: *mut u8, len: usize)>,

    /// Cleanup and destroy the instance.
    pub drop: Option<unsafe extern "C" fn(instance: *mut c_void)>,
}

// ============================================================================
// Language Runtime Plugin VTable
// ============================================================================

/// Error model for a language runtime.
///
/// Describes whether a runtime's foreign function calls can fail at runtime
/// due to the inherent dynamism of the language.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorModel {
    /// Runtime errors are possible on every call (Python, JS, Ruby).
    /// Foreign function return types are automatically wrapped in `Result<T>`.
    Dynamic = 0,
    /// The language has compile-time type safety. Foreign functions return
    /// `T` directly; runtime errors are not expected under normal operation.
    Static = 1,
}

/// VTable for language runtime plugins (Python, Julia, SQL, etc.).
///
/// Language runtimes enable `fn <language> name(...) { body }` blocks in Shape.
/// The runtime compiles and invokes foreign language code, providing type
/// marshaling between Shape values and native language objects.
#[repr(C)]
pub struct LanguageRuntimeVTable {
    /// Initialize the runtime with MessagePack-encoded config.
    /// Returns: opaque instance pointer, or null on error.
    pub init: Option<unsafe extern "C" fn(config: *const u8, config_len: usize) -> *mut c_void>,

    /// Register Shape type schemas for stub generation (e.g. `.pyi` files).
    /// `types_msgpack`: MessagePack-encoded `Vec<TypeSchemaExport>`.
    /// Returns: 0 on success.
    pub register_types: Option<
        unsafe extern "C" fn(instance: *mut c_void, types: *const u8, types_len: usize) -> i32,
    >,

    /// Pre-compile a foreign function body.
    ///
    /// * `name`: function name (UTF-8)
    /// * `source`: dedented body text (UTF-8)
    /// * `param_names_msgpack`: MessagePack `Vec<String>` of parameter names
    /// * `param_types_msgpack`: MessagePack `Vec<String>` of Shape type names
    /// * `return_type`: Shape return type name (UTF-8, empty if none)
    /// * `is_async`: whether the function was declared `async` in Shape
    ///
    /// Returns: opaque compiled function handle, or null on error.
    /// On error, writes a UTF-8 error message to `out_error` / `out_error_len`
    /// (caller frees via `free_buffer`).
    pub compile: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            name: *const u8,
            name_len: usize,
            source: *const u8,
            source_len: usize,
            param_names: *const u8,
            param_names_len: usize,
            param_types: *const u8,
            param_types_len: usize,
            return_type: *const u8,
            return_type_len: usize,
            is_async: bool,
            out_error: *mut *mut u8,
            out_error_len: *mut usize,
        ) -> *mut c_void,
    >,

    /// Invoke a compiled function with MessagePack-encoded arguments.
    ///
    /// `args_msgpack`: MessagePack-encoded argument array.
    /// On success, writes MessagePack-encoded result to `out_ptr` / `out_len`.
    /// Returns: 0 on success, non-zero on error.
    pub invoke: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            handle: *mut c_void,
            args: *const u8,
            args_len: usize,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Release a compiled function handle.
    pub dispose_function: Option<unsafe extern "C" fn(instance: *mut c_void, handle: *mut c_void)>,

    /// Return the language identifier (null-terminated C string, e.g. "python").
    /// The returned pointer must remain valid for the lifetime of the instance.
    pub language_id: Option<unsafe extern "C" fn(instance: *mut c_void) -> *const c_char>,

    /// Return MessagePack-encoded `LanguageRuntimeLspConfig`.
    /// Caller frees via `free_buffer`.
    pub get_lsp_config: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,

    /// Free a buffer allocated by compile/invoke/get_lsp_config.
    pub free_buffer: Option<unsafe extern "C" fn(ptr: *mut u8, len: usize)>,

    /// Cleanup and destroy the runtime instance.
    pub drop: Option<unsafe extern "C" fn(instance: *mut c_void)>,

    /// Error model for this language runtime.
    ///
    /// `Dynamic` (0) means every call can fail at runtime — return values are
    /// automatically wrapped in `Result<T>`.  `Static` (1) means the language
    /// has compile-time type safety and runtime errors are not expected.
    ///
    /// Defaults to `Dynamic` (0) when zero-initialized.
    pub error_model: ErrorModel,

    /// Return a bundled `.shape` module source for this language runtime.
    ///
    /// The returned buffer is a UTF-8 string containing Shape source code
    /// that defines the extension's namespace (e.g., `python`, `typescript`).
    /// The host compiles this source and makes it importable under the
    /// extension's own namespace -- NOT under `std::*`.
    ///
    /// Caller frees via `free_buffer`. Returns 0 on success.
    /// If the extension has no bundled source, set this to `None`.
    pub get_shape_source: Option<
        unsafe extern "C" fn(
            instance: *mut c_void,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32,
    >,
}

/// LSP configuration for a language runtime, returned by `get_lsp_config`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LanguageRuntimeLspConfig {
    /// Language identifier (e.g. "python").
    pub language_id: std::string::String,
    /// Command to start the child language server.
    pub server_command: Vec<std::string::String>,
    /// File extension for virtual documents (e.g. ".py").
    pub file_extension: std::string::String,
    /// Extra search paths for the child LSP (e.g. stub directories).
    pub extra_paths: Vec<std::string::String>,
}

/// Exported Shape type schema for foreign language runtimes.
///
/// Serialized as MessagePack and passed to `register_types()`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TypeSchemaExport {
    /// Type name.
    pub name: std::string::String,
    /// Kind of type.
    pub kind: TypeSchemaExportKind,
    /// Fields (for struct types).
    pub fields: Vec<TypeFieldExport>,
    /// Enum variants (for enum types).
    pub enum_variants: Option<Vec<EnumVariantExport>>,
}

/// Kind of exported type schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TypeSchemaExportKind {
    Struct,
    Enum,
    Alias,
}

/// A single field in an exported type schema.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TypeFieldExport {
    /// Field name.
    pub name: std::string::String,
    /// Shape type name (e.g. "number", "string", "Array<Candle>").
    pub type_name: std::string::String,
    /// Whether the field is optional.
    pub optional: bool,
    /// Human-readable description.
    pub description: Option<std::string::String>,
}

/// A single enum variant in an exported type schema.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EnumVariantExport {
    /// Variant name.
    pub name: std::string::String,
    /// Payload fields (if any).
    pub payload_fields: Option<Vec<TypeFieldExport>>,
}

// ============================================================================
// Required Plugin Exports
// ============================================================================

/// Type signature for `shape_plugin_info` export
pub type GetPluginInfoFn = unsafe extern "C" fn() -> *const PluginInfo;

/// Type signature for `shape_data_source_vtable` export
pub type GetDataSourceVTableFn = unsafe extern "C" fn() -> *const DataSourceVTable;

/// Type signature for `shape_output_sink_vtable` export
pub type GetOutputSinkVTableFn = unsafe extern "C" fn() -> *const OutputSinkVTable;
/// Type signature for `shape_module_vtable` export.
pub type GetModuleVTableFn = unsafe extern "C" fn() -> *const ModuleVTable;
/// Type signature for `shape_language_runtime_vtable` export.
pub type GetLanguageRuntimeVTableFn = unsafe extern "C" fn() -> *const LanguageRuntimeVTable;
/// Type signature for optional `shape_capability_manifest` export
pub type GetCapabilityManifestFn = unsafe extern "C" fn() -> *const CapabilityManifest;
/// Type signature for optional generic `shape_capability_vtable` export
///
/// When present, this is preferred over capability-specific symbol names.
/// `contract` is a UTF-8 byte slice (for example `shape.datasource`).
/// Return null when the contract is not implemented by this module.
pub type GetCapabilityVTableFn =
    unsafe extern "C" fn(contract: *const u8, contract_len: usize) -> *const c_void;

// ============================================================================
// Error Codes
// ============================================================================

/// Standard error codes returned by plugin functions
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PluginError {
    /// Operation succeeded
    Success = 0,
    /// Invalid argument
    InvalidArgument = 1,
    /// Query validation failed
    ValidationFailed = 2,
    /// Connection error
    ConnectionError = 3,
    /// Data not found
    NotFound = 4,
    /// Timeout
    Timeout = 5,
    /// Permission denied
    PermissionDenied = 6,
    /// Internal error
    InternalError = 7,
    /// Not implemented
    NotImplemented = 8,
    /// Resource exhausted
    ResourceExhausted = 9,
    /// Plugin not initialized
    NotInitialized = 10,
}

// ============================================================================
// Permission Model (Self-Describing)
// ============================================================================

use std::collections::BTreeSet;
use std::fmt;

/// Category of a permission, used for grouping in human-readable displays.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PermissionCategory {
    /// Filesystem access (read, write, scoped).
    Filesystem,
    /// Network access (connect, listen, scoped).
    Network,
    /// System-level capabilities (process, env, time, random).
    System,
    /// Sandbox controls (virtual fs, deterministic runtime, output capture).
    Sandbox,
}

impl PermissionCategory {
    /// Human-readable name for this category.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Filesystem => "Filesystem",
            Self::Network => "Network",
            Self::System => "System",
            Self::Sandbox => "Sandbox",
        }
    }
}

impl fmt::Display for PermissionCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// A single, self-describing permission that a plugin can request.
///
/// Each variant carries enough metadata to produce human-readable prompts
/// (e.g., "Allow plugin X to read the filesystem?").
///
/// Permissions are intentionally **not** bitflags — they are named, enumerable,
/// and carry documentation so that hosts can display meaningful permission
/// dialogs and plugins can declare exactly what they need.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Permission {
    // -- Filesystem --
    /// Read files and directories.
    FsRead,
    /// Write, create, and delete files and directories.
    FsWrite,
    /// Filesystem access scoped to specific paths (see `PermissionGrant`).
    FsScoped,

    // -- Network --
    /// Open outbound network connections.
    NetConnect,
    /// Listen for inbound network connections.
    NetListen,
    /// Network access scoped to specific hosts/ports (see `PermissionGrant`).
    NetScoped,

    // -- System --
    /// Spawn child processes.
    Process,
    /// Read environment variables.
    Env,
    /// Access wall-clock time.
    Time,
    /// Access random number generation.
    Random,

    // -- Sandbox controls --
    /// Plugin operates against a virtual filesystem instead of the real one.
    Vfs,
    /// Plugin runs in a deterministic runtime (fixed time, seeded RNG).
    Deterministic,
    /// Plugin output is captured for inspection rather than emitted directly.
    Capture,
    /// Memory usage is limited to a configured ceiling.
    MemLimited,
    /// Wall-clock execution time is capped.
    TimeLimited,
    /// Output volume is capped (bytes or records).
    OutputLimited,
}

impl Permission {
    /// Short machine-readable name (stable across versions).
    pub fn name(&self) -> &'static str {
        match self {
            Self::FsRead => "fs.read",
            Self::FsWrite => "fs.write",
            Self::FsScoped => "fs.scoped",
            Self::NetConnect => "net.connect",
            Self::NetListen => "net.listen",
            Self::NetScoped => "net.scoped",
            Self::Process => "sys.process",
            Self::Env => "sys.env",
            Self::Time => "sys.time",
            Self::Random => "sys.random",
            Self::Vfs => "sandbox.vfs",
            Self::Deterministic => "sandbox.deterministic",
            Self::Capture => "sandbox.capture",
            Self::MemLimited => "sandbox.mem_limited",
            Self::TimeLimited => "sandbox.time_limited",
            Self::OutputLimited => "sandbox.output_limited",
        }
    }

    /// Human-readable description suitable for permission prompts.
    pub fn description(&self) -> &'static str {
        match self {
            Self::FsRead => "Read files and directories",
            Self::FsWrite => "Write, create, and delete files and directories",
            Self::FsScoped => "Filesystem access scoped to specific paths",
            Self::NetConnect => "Open outbound network connections",
            Self::NetListen => "Listen for inbound network connections",
            Self::NetScoped => "Network access scoped to specific hosts/ports",
            Self::Process => "Spawn child processes",
            Self::Env => "Read environment variables",
            Self::Time => "Access wall-clock time",
            Self::Random => "Access random number generation",
            Self::Vfs => "Operate against a virtual filesystem",
            Self::Deterministic => "Run in a deterministic runtime (fixed time, seeded RNG)",
            Self::Capture => "Output is captured for inspection",
            Self::MemLimited => "Memory usage is limited to a configured ceiling",
            Self::TimeLimited => "Execution time is capped",
            Self::OutputLimited => "Output volume is capped",
        }
    }

    /// Category this permission belongs to.
    pub fn category(&self) -> PermissionCategory {
        match self {
            Self::FsRead | Self::FsWrite | Self::FsScoped => PermissionCategory::Filesystem,
            Self::NetConnect | Self::NetListen | Self::NetScoped => PermissionCategory::Network,
            Self::Process | Self::Env | Self::Time | Self::Random => PermissionCategory::System,
            Self::Vfs
            | Self::Deterministic
            | Self::Capture
            | Self::MemLimited
            | Self::TimeLimited
            | Self::OutputLimited => PermissionCategory::Sandbox,
        }
    }

    /// All permission variants (useful for enumeration / display).
    pub fn all_variants() -> &'static [Permission] {
        &[
            Self::FsRead,
            Self::FsWrite,
            Self::FsScoped,
            Self::NetConnect,
            Self::NetListen,
            Self::NetScoped,
            Self::Process,
            Self::Env,
            Self::Time,
            Self::Random,
            Self::Vfs,
            Self::Deterministic,
            Self::Capture,
            Self::MemLimited,
            Self::TimeLimited,
            Self::OutputLimited,
        ]
    }
}

impl fmt::Display for Permission {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// A set of permissions with set-algebraic operations.
///
/// Backed by a `BTreeSet` so iteration order is deterministic and
/// serialization is stable.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PermissionSet {
    permissions: BTreeSet<Permission>,
}

impl Default for PermissionSet {
    fn default() -> Self {
        Self::pure()
    }
}

impl PermissionSet {
    /// Empty permission set (pure computation — no capabilities).
    pub fn pure() -> Self {
        Self {
            permissions: BTreeSet::new(),
        }
    }

    /// Read-only access: filesystem read + env + time.
    pub fn readonly() -> Self {
        Self {
            permissions: [Permission::FsRead, Permission::Env, Permission::Time]
                .into_iter()
                .collect(),
        }
    }

    /// Full (unrestricted) permissions — every variant.
    pub fn full() -> Self {
        Self {
            permissions: Permission::all_variants().iter().copied().collect(),
        }
    }

    /// Create a set from an iterator of permissions.
    pub fn from_iter(iter: impl IntoIterator<Item = Permission>) -> Self {
        Self {
            permissions: iter.into_iter().collect(),
        }
    }

    /// Add a permission to the set. Returns whether it was newly inserted.
    pub fn insert(&mut self, perm: Permission) -> bool {
        self.permissions.insert(perm)
    }

    /// Remove a permission from the set. Returns whether it was present.
    pub fn remove(&mut self, perm: &Permission) -> bool {
        self.permissions.remove(perm)
    }

    /// Check whether a specific permission is in the set.
    pub fn contains(&self, perm: &Permission) -> bool {
        self.permissions.contains(perm)
    }

    /// True if this set is a subset of `other`.
    pub fn is_subset(&self, other: &PermissionSet) -> bool {
        self.permissions.is_subset(&other.permissions)
    }

    /// True if this set is a superset of `other`.
    pub fn is_superset(&self, other: &PermissionSet) -> bool {
        self.permissions.is_superset(&other.permissions)
    }

    /// Set union (all permissions from both sets).
    pub fn union(&self, other: &PermissionSet) -> PermissionSet {
        PermissionSet {
            permissions: self
                .permissions
                .union(&other.permissions)
                .copied()
                .collect(),
        }
    }

    /// Set intersection (only permissions in both sets).
    pub fn intersection(&self, other: &PermissionSet) -> PermissionSet {
        PermissionSet {
            permissions: self
                .permissions
                .intersection(&other.permissions)
                .copied()
                .collect(),
        }
    }

    /// Set difference (permissions in self but not in other).
    pub fn difference(&self, other: &PermissionSet) -> PermissionSet {
        PermissionSet {
            permissions: self
                .permissions
                .difference(&other.permissions)
                .copied()
                .collect(),
        }
    }

    /// True when the set is empty (no permissions).
    pub fn is_empty(&self) -> bool {
        self.permissions.is_empty()
    }

    /// Number of permissions in the set.
    pub fn len(&self) -> usize {
        self.permissions.len()
    }

    /// Iterate over the permissions in deterministic order.
    pub fn iter(&self) -> impl Iterator<Item = &Permission> {
        self.permissions.iter()
    }

    /// Return permissions grouped by category.
    pub fn by_category(&self) -> std::collections::BTreeMap<PermissionCategory, Vec<Permission>> {
        let mut map = std::collections::BTreeMap::new();
        for perm in &self.permissions {
            map.entry(perm.category())
                .or_insert_with(Vec::new)
                .push(*perm);
        }
        map
    }
}

impl fmt::Display for PermissionSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let names: Vec<&str> = self.permissions.iter().map(|p| p.name()).collect();
        write!(f, "{{{}}}", names.join(", "))
    }
}

impl<const N: usize> From<[Permission; N]> for PermissionSet {
    fn from(arr: [Permission; N]) -> Self {
        Self {
            permissions: arr.into_iter().collect(),
        }
    }
}

impl std::iter::FromIterator<Permission> for PermissionSet {
    fn from_iter<I: IntoIterator<Item = Permission>>(iter: I) -> Self {
        Self {
            permissions: iter.into_iter().collect(),
        }
    }
}

impl IntoIterator for PermissionSet {
    type Item = Permission;
    type IntoIter = std::collections::btree_set::IntoIter<Permission>;

    fn into_iter(self) -> Self::IntoIter {
        self.permissions.into_iter()
    }
}

impl<'a> IntoIterator for &'a PermissionSet {
    type Item = &'a Permission;
    type IntoIter = std::collections::btree_set::Iter<'a, Permission>;

    fn into_iter(self) -> Self::IntoIter {
        self.permissions.iter()
    }
}

/// Scope constraints for a permission grant.
///
/// When attached to a `PermissionGrant`, these constrain *where* or *how much*
/// a permission applies. For example, `FsScoped` with `allowed_paths` limits
/// filesystem access to specific directories.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ScopeConstraints {
    /// Allowed filesystem paths (glob patterns). Only relevant for `FsScoped`.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Vec::is_empty")
    )]
    pub allowed_paths: Vec<std::string::String>,

    /// Allowed network hosts (host:port patterns). Only relevant for `NetScoped`.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Vec::is_empty")
    )]
    pub allowed_hosts: Vec<std::string::String>,

    /// Maximum memory in bytes. Only relevant for `MemLimited`.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub max_memory_bytes: Option<u64>,

    /// Maximum execution time in milliseconds. Only relevant for `TimeLimited`.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub max_time_ms: Option<u64>,

    /// Maximum output bytes. Only relevant for `OutputLimited`.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub max_output_bytes: Option<u64>,
}

impl ScopeConstraints {
    /// Unconstrained (no limits).
    pub fn none() -> Self {
        Self {
            allowed_paths: Vec::new(),
            allowed_hosts: Vec::new(),
            max_memory_bytes: None,
            max_time_ms: None,
            max_output_bytes: None,
        }
    }
}

impl Default for ScopeConstraints {
    fn default() -> Self {
        Self::none()
    }
}

/// A single granted permission with optional scope constraints.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PermissionGrant {
    /// The permission being granted.
    pub permission: Permission,
    /// Optional scope constraints narrowing the grant.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub constraints: Option<ScopeConstraints>,
}

impl PermissionGrant {
    /// Grant a permission without scope constraints.
    pub fn unconstrained(permission: Permission) -> Self {
        Self {
            permission,
            constraints: None,
        }
    }

    /// Grant a permission with scope constraints.
    pub fn scoped(permission: Permission, constraints: ScopeConstraints) -> Self {
        Self {
            permission,
            constraints: Some(constraints),
        }
    }
}

// ============================================================================
// Alert Types (for Output Sinks)
// ============================================================================

/// Alert severity levels
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlertSeverity {
    Debug = 0,
    Info = 1,
    Warning = 2,
    Error = 3,
    Critical = 4,
}

/// C-compatible alert structure for serialization reference
///
/// Actual alerts are MessagePack-encoded with this structure:
/// ```json
/// {
///   "id": "uuid-string",
///   "severity": 1,  // AlertSeverity value
///   "title": "Alert title",
///   "message": "Detailed message",
///   "data": { "key": "value" },  // Arbitrary structured data
///   "tags": ["tag1", "tag2"],
///   "timestamp": 1706054400000  // Unix millis
/// }
/// ```
#[repr(C)]
pub struct AlertHeader {
    /// Alert severity
    pub severity: AlertSeverity,
    /// Timestamp in milliseconds since Unix epoch
    pub timestamp_ms: i64,
}

// ============================================================================
// Version Checking
// ============================================================================

/// ABI version for compatibility checking
/// ABI version for compatibility checking
///
/// Version history:
/// - v1: Initial release with MessagePack-based load()
/// - v2: Added load_binary() for high-performance binary columnar format
/// - v3: Added module invoke_ex() typed payloads for table fast-path marshalling
pub const ABI_VERSION: u32 = 3;

/// Get the ABI version (plugins should export this)
pub type GetAbiVersionFn = unsafe extern "C" fn() -> u32;

// ============================================================================
// Helper Macros (for plugin authors)
// ============================================================================

/// Generate the full set of `#[no_mangle]` C ABI exports for a language runtime
/// extension plugin.
///
/// This eliminates the boilerplate that is otherwise duplicated across every
/// language runtime extension (e.g. `extensions/python/src/lib.rs` and
/// `extensions/typescript/src/lib.rs`).
///
/// # Generated exports
///
/// - `shape_plugin_info()` — plugin metadata
/// - `shape_abi_version()` — ABI version tag
/// - `shape_capability_manifest()` — declares a single LanguageRuntime capability
/// - `shape_language_runtime_vtable()` — the VTable itself
/// - `shape_capability_vtable(contract, len)` — generic vtable dispatch
///
/// # Example
///
/// ```ignore
/// shape_abi_v1::language_runtime_plugin! {
///     name: c"python",
///     version: c"0.1.0",
///     description: c"Python language runtime for foreign function blocks",
///     vtable: {
///         init: runtime::python_init,
///         register_types: runtime::python_register_types,
///         compile: runtime::python_compile,
///         invoke: runtime::python_invoke,
///         dispose_function: runtime::python_dispose_function,
///         language_id: runtime::python_language_id,
///         get_lsp_config: runtime::python_get_lsp_config,
///         free_buffer: runtime::python_free_buffer,
///         drop: runtime::python_drop,
///     }
/// }
/// ```
#[macro_export]
macro_rules! language_runtime_plugin {
    // Arm WITH shape_source: embeds a `.shape` module artifact in the extension.
    (
        name: $name:expr,
        version: $version:expr,
        description: $description:expr,
        shape_source: $shape_source:expr,
        vtable: {
            init: $init:expr,
            register_types: $register_types:expr,
            compile: $compile:expr,
            invoke: $invoke:expr,
            dispose_function: $dispose_function:expr,
            language_id: $language_id:expr,
            get_lsp_config: $get_lsp_config:expr,
            free_buffer: $free_buffer:expr,
            drop: $drop_fn:expr $(,)?
        } $(,)?
    ) => {
        $crate::language_runtime_plugin!(@internal
            name: $name,
            version: $version,
            description: $description,
            shape_source_opt: Some($shape_source),
            vtable: {
                init: $init,
                register_types: $register_types,
                compile: $compile,
                invoke: $invoke,
                dispose_function: $dispose_function,
                language_id: $language_id,
                get_lsp_config: $get_lsp_config,
                free_buffer: $free_buffer,
                drop: $drop_fn,
            }
        );
    };

    // Arm WITHOUT shape_source: backward-compatible, no bundled module.
    (
        name: $name:expr,
        version: $version:expr,
        description: $description:expr,
        vtable: {
            init: $init:expr,
            register_types: $register_types:expr,
            compile: $compile:expr,
            invoke: $invoke:expr,
            dispose_function: $dispose_function:expr,
            language_id: $language_id:expr,
            get_lsp_config: $get_lsp_config:expr,
            free_buffer: $free_buffer:expr,
            drop: $drop_fn:expr $(,)?
        } $(,)?
    ) => {
        $crate::language_runtime_plugin!(@internal
            name: $name,
            version: $version,
            description: $description,
            shape_source_opt: None,
            vtable: {
                init: $init,
                register_types: $register_types,
                compile: $compile,
                invoke: $invoke,
                dispose_function: $dispose_function,
                language_id: $language_id,
                get_lsp_config: $get_lsp_config,
                free_buffer: $free_buffer,
                drop: $drop_fn,
            }
        );
    };

    // Internal implementation arm.
    (@internal
        name: $name:expr,
        version: $version:expr,
        description: $description:expr,
        shape_source_opt: $shape_source_opt:expr,
        vtable: {
            init: $init:expr,
            register_types: $register_types:expr,
            compile: $compile:expr,
            invoke: $invoke:expr,
            dispose_function: $dispose_function:expr,
            language_id: $language_id:expr,
            get_lsp_config: $get_lsp_config:expr,
            free_buffer: $free_buffer:expr,
            drop: $drop_fn:expr $(,)?
        } $(,)?
    ) => {
        #[unsafe(no_mangle)]
        pub extern "C" fn shape_plugin_info() -> *const $crate::PluginInfo {
            static INFO: $crate::PluginInfo = $crate::PluginInfo {
                name: $name.as_ptr(),
                version: $version.as_ptr(),
                plugin_type: $crate::PluginType::DataSource,
                description: $description.as_ptr(),
            };
            &INFO
        }

        #[unsafe(no_mangle)]
        pub extern "C" fn shape_abi_version() -> u32 {
            $crate::ABI_VERSION
        }

        #[unsafe(no_mangle)]
        pub extern "C" fn shape_capability_manifest() -> *const $crate::CapabilityManifest {
            static CAPABILITIES: [$crate::CapabilityDescriptor; 1] =
                [$crate::CapabilityDescriptor {
                    kind: $crate::CapabilityKind::LanguageRuntime,
                    contract: c"shape.language_runtime".as_ptr(),
                    version: c"1".as_ptr(),
                    flags: 0,
                }];
            static MANIFEST: $crate::CapabilityManifest = $crate::CapabilityManifest {
                capabilities: CAPABILITIES.as_ptr(),
                capabilities_len: CAPABILITIES.len(),
            };
            &MANIFEST
        }

        /// Return the bundled `.shape` source for this language runtime, if any.
        ///
        /// Writes a UTF-8 string to `out_ptr`/`out_len`. Caller frees via
        /// `free_buffer`. Returns 0 on success (even when no source is bundled,
        /// in which case `out_ptr` is set to null).
        unsafe extern "C" fn __shape_get_shape_source(
            _instance: *mut ::std::ffi::c_void,
            out_ptr: *mut *mut u8,
            out_len: *mut usize,
        ) -> i32 {
            const SOURCE: Option<&str> = $shape_source_opt;
            if out_ptr.is_null() || out_len.is_null() {
                return 1;
            }
            match SOURCE {
                Some(src) => {
                    let mut bytes = src.as_bytes().to_vec();
                    let len = bytes.len();
                    let ptr = bytes.as_mut_ptr();
                    ::std::mem::forget(bytes);
                    unsafe {
                        *out_ptr = ptr;
                        *out_len = len;
                    }
                    0
                }
                None => {
                    unsafe {
                        *out_ptr = ::std::ptr::null_mut();
                        *out_len = 0;
                    }
                    0
                }
            }
        }

        #[unsafe(no_mangle)]
        pub extern "C" fn shape_language_runtime_vtable() -> *const $crate::LanguageRuntimeVTable {
            static VTABLE: $crate::LanguageRuntimeVTable = $crate::LanguageRuntimeVTable {
                init: Some($init),
                register_types: Some($register_types),
                compile: Some($compile),
                invoke: Some($invoke),
                dispose_function: Some($dispose_function),
                language_id: Some($language_id),
                get_lsp_config: Some($get_lsp_config),
                free_buffer: Some($free_buffer),
                drop: Some($drop_fn),
                error_model: $crate::ErrorModel::Dynamic,
                get_shape_source: Some(__shape_get_shape_source),
            };
            &VTABLE
        }

        #[unsafe(no_mangle)]
        pub extern "C" fn shape_capability_vtable(
            contract: *const u8,
            contract_len: usize,
        ) -> *const ::std::ffi::c_void {
            if contract.is_null() {
                return ::std::ptr::null();
            }
            let contract =
                unsafe { ::std::slice::from_raw_parts(contract, contract_len) };
            if contract == $crate::CAPABILITY_LANGUAGE_RUNTIME.as_bytes() {
                shape_language_runtime_vtable() as *const ::std::ffi::c_void
            } else {
                ::std::ptr::null()
            }
        }
    };
}

/// Macro to define a static QueryParam with const strings
#[macro_export]
macro_rules! query_param {
    (
        name: $name:expr,
        description: $desc:expr,
        param_type: $ptype:expr,
        required: $req:expr
    ) => {
        $crate::QueryParam {
            name: concat!($name, "\0").as_ptr() as *const core::ffi::c_char,
            description: concat!($desc, "\0").as_ptr() as *const core::ffi::c_char,
            param_type: $ptype,
            required: $req,
            default_value: core::ptr::null(),
            default_value_len: 0,
            allowed_values: core::ptr::null(),
            allowed_values_len: 0,
            nested_schema: core::ptr::null(),
        }
    };
}

/// Macro to define a static OutputField with const strings
#[macro_export]
macro_rules! output_field {
    (
        name: $name:expr,
        field_type: $ftype:expr,
        description: $desc:expr
    ) => {
        $crate::OutputField {
            name: concat!($name, "\0").as_ptr() as *const core::ffi::c_char,
            field_type: $ftype,
            description: concat!($desc, "\0").as_ptr() as *const core::ffi::c_char,
        }
    };
}

// ============================================================================
// Safety Documentation
// ============================================================================

// # Safety Requirements for Plugin Authors
//
// 1. All `*const c_char` strings must be null-terminated
// 2. All MessagePack buffers must be valid MessagePack data
// 3. Instance pointers must be valid for the lifetime of the plugin
// 4. Callbacks must not panic across the FFI boundary
// 5. Memory allocated by plugin must be freed by plugin's free functions
// 6. Schemas must remain valid for the lifetime of the plugin instance

// ============================================================================
// Tests — Permission Model
// ============================================================================

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

    // -- Permission enum introspection --

    #[test]
    fn permission_name_is_dotted() {
        for perm in Permission::all_variants() {
            let name = perm.name();
            assert!(
                name.contains('.'),
                "Permission name '{}' should contain a dot",
                name
            );
        }
    }

    #[test]
    fn permission_description_is_nonempty() {
        for perm in Permission::all_variants() {
            assert!(!perm.description().is_empty());
        }
    }

    #[test]
    fn permission_category_roundtrip() {
        assert_eq!(
            Permission::FsRead.category(),
            PermissionCategory::Filesystem
        );
        assert_eq!(
            Permission::FsWrite.category(),
            PermissionCategory::Filesystem
        );
        assert_eq!(
            Permission::NetConnect.category(),
            PermissionCategory::Network
        );
        assert_eq!(
            Permission::NetListen.category(),
            PermissionCategory::Network
        );
        assert_eq!(Permission::Process.category(), PermissionCategory::System);
        assert_eq!(Permission::Env.category(), PermissionCategory::System);
        assert_eq!(Permission::Time.category(), PermissionCategory::System);
        assert_eq!(Permission::Random.category(), PermissionCategory::System);
        assert_eq!(Permission::Vfs.category(), PermissionCategory::Sandbox);
        assert_eq!(
            Permission::Deterministic.category(),
            PermissionCategory::Sandbox
        );
    }

    #[test]
    fn permission_display() {
        assert_eq!(format!("{}", Permission::FsRead), "fs.read");
        assert_eq!(format!("{}", Permission::NetConnect), "net.connect");
    }

    #[test]
    fn all_variants_is_exhaustive() {
        // If a new variant is added but not listed in all_variants,
        // the match in name()/description()/category() will catch it at compile time.
        // This test just verifies the count is sane (>= 16 known variants).
        assert!(Permission::all_variants().len() >= 16);
    }

    // -- PermissionSet constructors --

    #[test]
    fn pure_is_empty() {
        let set = PermissionSet::pure();
        assert!(set.is_empty());
        assert_eq!(set.len(), 0);
    }

    #[test]
    fn readonly_contains_expected() {
        let set = PermissionSet::readonly();
        assert!(set.contains(&Permission::FsRead));
        assert!(set.contains(&Permission::Env));
        assert!(set.contains(&Permission::Time));
        assert!(!set.contains(&Permission::FsWrite));
        assert!(!set.contains(&Permission::NetConnect));
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn full_contains_all() {
        let set = PermissionSet::full();
        for perm in Permission::all_variants() {
            assert!(set.contains(perm), "full() missing {:?}", perm);
        }
    }

    // -- Set algebra --

    #[test]
    fn union_combines() {
        let a = PermissionSet::from([Permission::FsRead, Permission::NetConnect]);
        let b = PermissionSet::from([Permission::FsWrite, Permission::NetConnect]);
        let u = a.union(&b);
        assert_eq!(u.len(), 3);
        assert!(u.contains(&Permission::FsRead));
        assert!(u.contains(&Permission::FsWrite));
        assert!(u.contains(&Permission::NetConnect));
    }

    #[test]
    fn intersection_narrows() {
        let a = PermissionSet::from([Permission::FsRead, Permission::NetConnect]);
        let b = PermissionSet::from([Permission::FsWrite, Permission::NetConnect]);
        let i = a.intersection(&b);
        assert_eq!(i.len(), 1);
        assert!(i.contains(&Permission::NetConnect));
    }

    #[test]
    fn difference_subtracts() {
        let a = PermissionSet::from([Permission::FsRead, Permission::FsWrite, Permission::Env]);
        let b = PermissionSet::from([Permission::FsWrite]);
        let d = a.difference(&b);
        assert_eq!(d.len(), 2);
        assert!(d.contains(&Permission::FsRead));
        assert!(d.contains(&Permission::Env));
        assert!(!d.contains(&Permission::FsWrite));
    }

    #[test]
    fn subset_superset() {
        let small = PermissionSet::from([Permission::FsRead]);
        let big = PermissionSet::from([Permission::FsRead, Permission::FsWrite]);
        assert!(small.is_subset(&big));
        assert!(!big.is_subset(&small));
        assert!(big.is_superset(&small));
        assert!(!small.is_superset(&big));
    }

    #[test]
    fn insert_and_remove() {
        let mut set = PermissionSet::pure();
        assert!(set.insert(Permission::Time));
        assert!(!set.insert(Permission::Time)); // duplicate
        assert_eq!(set.len(), 1);
        assert!(set.remove(&Permission::Time));
        assert!(!set.remove(&Permission::Time)); // already removed
        assert!(set.is_empty());
    }

    // -- Display --

    #[test]
    fn permission_set_display() {
        let set = PermissionSet::from([Permission::FsRead, Permission::Env]);
        let s = format!("{}", set);
        // BTreeSet ordering: FsRead < Env based on Ord derive
        assert!(s.starts_with('{'));
        assert!(s.ends_with('}'));
        assert!(s.contains("fs.read"));
        assert!(s.contains("sys.env"));
    }

    // -- by_category --

    #[test]
    fn by_category_groups() {
        let set = PermissionSet::from([
            Permission::FsRead,
            Permission::FsWrite,
            Permission::NetConnect,
            Permission::Time,
            Permission::Vfs,
        ]);
        let cats = set.by_category();
        assert_eq!(cats[&PermissionCategory::Filesystem].len(), 2);
        assert_eq!(cats[&PermissionCategory::Network].len(), 1);
        assert_eq!(cats[&PermissionCategory::System].len(), 1);
        assert_eq!(cats[&PermissionCategory::Sandbox].len(), 1);
    }

    // -- FromIterator / IntoIterator --

    #[test]
    fn collect_from_iterator() {
        let perms = vec![Permission::FsRead, Permission::Env];
        let set: PermissionSet = perms.into_iter().collect();
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn into_iter_owned() {
        let set = PermissionSet::from([Permission::FsRead, Permission::Env]);
        let v: Vec<Permission> = set.into_iter().collect();
        assert_eq!(v.len(), 2);
    }

    #[test]
    fn into_iter_ref() {
        let set = PermissionSet::from([Permission::FsRead, Permission::Env]);
        let v: Vec<&Permission> = (&set).into_iter().collect();
        assert_eq!(v.len(), 2);
    }

    #[test]
    fn from_array() {
        let set = PermissionSet::from([Permission::Process, Permission::Random]);
        assert_eq!(set.len(), 2);
        assert!(set.contains(&Permission::Process));
        assert!(set.contains(&Permission::Random));
    }

    // -- PermissionGrant --

    #[test]
    fn unconstrained_grant() {
        let g = PermissionGrant::unconstrained(Permission::FsRead);
        assert_eq!(g.permission, Permission::FsRead);
        assert!(g.constraints.is_none());
    }

    #[test]
    fn scoped_grant_with_paths() {
        let c = ScopeConstraints {
            allowed_paths: vec!["/tmp/*".into(), "/data/**".into()],
            ..Default::default()
        };
        let g = PermissionGrant::scoped(Permission::FsScoped, c);
        assert_eq!(g.permission, Permission::FsScoped);
        let sc = g.constraints.unwrap();
        assert_eq!(sc.allowed_paths.len(), 2);
        assert!(sc.allowed_hosts.is_empty());
    }

    #[test]
    fn scoped_grant_with_limits() {
        let c = ScopeConstraints {
            max_memory_bytes: Some(1024 * 1024 * 64),
            max_time_ms: Some(5000),
            max_output_bytes: Some(1024 * 1024),
            ..Default::default()
        };
        let g = PermissionGrant::scoped(Permission::MemLimited, c);
        let sc = g.constraints.unwrap();
        assert_eq!(sc.max_memory_bytes, Some(64 * 1024 * 1024));
        assert_eq!(sc.max_time_ms, Some(5000));
    }

    // -- PermissionCategory display --

    #[test]
    fn category_display() {
        assert_eq!(format!("{}", PermissionCategory::Filesystem), "Filesystem");
        assert_eq!(format!("{}", PermissionCategory::Network), "Network");
        assert_eq!(format!("{}", PermissionCategory::System), "System");
        assert_eq!(format!("{}", PermissionCategory::Sandbox), "Sandbox");
    }

    // -- Equality / ordering --

    #[test]
    fn permission_set_equality() {
        let a = PermissionSet::from([Permission::FsRead, Permission::Env]);
        let b = PermissionSet::from([Permission::Env, Permission::FsRead]);
        assert_eq!(a, b);
    }

    #[test]
    fn permission_ord_is_deterministic() {
        // BTreeSet iteration should always be in the same order
        let set = PermissionSet::from([Permission::Random, Permission::FsRead, Permission::Vfs]);
        let names: Vec<&str> = set.iter().map(|p| p.name()).collect();
        let mut sorted = names.clone();
        sorted.sort();
        // Since BTreeSet uses Ord, the iteration order should already be sorted
        // by the derived Ord (which is variant declaration order).
        // We just verify it's deterministic by checking two iterations match.
        let names2: Vec<&str> = set.iter().map(|p| p.name()).collect();
        assert_eq!(names, names2);
    }
}