uni-plugin-custom 2.1.0

Meta-plugin for uni-db — apoc.custom.declare* analogue
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
// Rust guideline compliant
//! Meta-plugin (`apoc.custom.declare*` analogue) for uni-db.
//!
//! This crate ships a built-in plugin whose procedures (`uni.plugin.declareFunction`,
//! `declareProcedure`, `declareAggregate`, `declareTrigger`) accept
//! Cypher source and persist new plugin registrations alongside the
//! framework's [`uni_plugin::PluginRegistry`].
//!
//! # M9 status (this commit)
//!
//! Completed M9 deliverables:
//!
//! * `uni.plugin.declareFunction` — fully wired. Parses the Cypher
//!   expression body at declare time, persists the [`DeclaredPlugin`]
//!   record via [`persistence::Persistence`], and registers a
//!   synthetic [`uni_plugin::traits::scalar::ScalarPluginFn`] into the
//!   shared [`PluginRegistry`].
//! * `uni.plugin.declareProcedure`, `declareAggregate`,
//!   `declareTrigger` — registered as Cypher-callable procedures.
//!   Their declarations are persisted and reachable via
//!   `uni.plugin.listDeclared`; full body execution rides on
//!   downstream host APIs (`ProcedureHost::execute_inner_query` for
//!   procedures; trigger/aggregate body invocation follows the M11
//!   capability work).
//! * `uni.plugin.listDeclared` / `dropDeclared` — extended for
//!   cascade-aware drops.
//! * Reactivation — declarations are reloaded into the registry on
//!   [`CustomPlugin::new`] when constructed with a non-empty
//!   persistence backend.
//! * Capability inheritance — declarations capture the declaring
//!   principal id; the registrar enforces capability gating at
//!   registration time via the synthetic plugin's manifest.
//!
//! # Persistence
//!
//! Proposal §9.7 anchors the persistence schema in a Cypher-visible
//! system label `_DeclaredPlugin`. Writing to that label from inside
//! a procedure requires write-enabled
//! [`uni_plugin::traits::procedure::ProcedureHost`] execution, which
//! does not yet exist (the host's `execute_inner_query` is read-only
//! and does not bind parameters — see
//! `crates/uni-query/src/query/executor/procedure_host.rs`).
//!
//! M9 ships persistence behind a [`persistence::Persistence`] trait
//! with a JSON-sidecar implementation that preserves the exact
//! [`DeclaredPlugin`] shape from §9.7. The cutover to system-label
//! persistence — once write-enabled host execution lands — is a
//! drop-in replacement of the backend; no schema, store, or
//! procedure code changes.

#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![warn(missing_debug_implementations)]

mod aggregate;
mod decode;
mod eval;
mod scalar;

pub mod persistence;

use std::sync::Arc;

use serde::{Deserialize, Serialize};
use thiserror::Error;
use uni_plugin::PluginRegistry;

pub use crate::aggregate::{DeclaredAggregateFn, install_aggregate_into_registry};
pub use crate::persistence::{JsonFilePersistence, NullPersistence, Persistence, PersistenceError};
pub use crate::scalar::DeclaredScalarFn;

/// Errors raised by the meta-plugin.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CustomError {
    /// Declared body could not be parsed.
    #[error("declared plugin body parse failure: {0}")]
    BodyParse(String),

    /// Declared qname conflicts with an existing native registration.
    #[error("declared qname `{0}` is shadowed by a native plugin registration")]
    NativeShadow(String),

    /// Declared plugin depends on a missing or already-dropped qname.
    #[error("declared plugin `{dependent}` depends on missing `{dep}`")]
    DependencyMissing {
        /// The dependent's qname.
        dependent: String,
        /// The missing dependency's qname.
        dep: String,
    },

    /// Cyclic dependencies among declared plugins.
    #[error("dependency cycle in declared plugins: {0:?}")]
    DependencyCycle(Vec<String>),

    /// A persistence backend reported a failure.
    #[error("declared-plugin persistence: {0}")]
    Persistence(#[from] PersistenceError),

    /// Registration into the [`PluginRegistry`] failed.
    #[error("declared-plugin registration: {0}")]
    Registration(String),

    /// The principal lacks a capability required by the declaration.
    #[error("declared-plugin capability denied: caller is missing `{0}`")]
    CapabilityDenied(String),
}

/// Persistent record of a declared plugin (written to
/// `uni_system.declared_plugins` per proposal §9.7 — currently
/// shipped via JSON sidecar; see crate docs).
///
/// Round-trips through `serde` so the same shape persists into the
/// JSON sidecar today and a Cypher property map (system-label
/// persistence) at the M9 cutover commit.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeclaredPlugin {
    /// Qualified name claimed by the declaration.
    pub qname: String,
    /// Kind: `"function" | "procedure" | "aggregate" | "trigger"`.
    pub kind: String,
    /// Cypher / Locy source body.
    pub body: String,
    /// Serialized signature (JSON-encoded — schema depends on `kind`).
    pub signature_json: String,
    /// Qualified names of other declared plugins this depends on.
    pub dependencies: Vec<String>,
    /// Principal id that declared this plugin.
    pub declared_by: String,
    /// Whether this declaration is active (shadowed declarations are
    /// inactive until the shadowing native plugin is removed).
    pub active: bool,
}

/// Top-level meta-plugin handle.
///
/// Implements [`uni_plugin::Plugin`]. Construct via
/// [`CustomPlugin::new`] (with a shared [`PluginRegistry`] Arc and a
/// [`Persistence`] backend) and add to a `Uni` instance through the
/// host's `register_builtin_plugins` flow (`crates/uni/src/api/mod.rs`).
///
/// The plugin owns:
///
/// * `store` — an in-memory [`DeclaredPluginStore`] mirroring every
///   declaration, used for dependency analysis and read-side
///   procedures (`listDeclared`, `dropDeclared`).
/// * `registry` — a shared `Arc<PluginRegistry>` so the declare*
///   procedures can register synthetic [`uni_plugin::Plugin`] values
///   at runtime.
/// * `persistence` — the durable backend that replays declarations on
///   `CustomPlugin::new`.
pub struct CustomPlugin {
    store: Arc<DeclaredPluginStore>,
    registry: Arc<PluginRegistry>,
    persistence: Arc<dyn Persistence>,
    /// Optional synthesizer for declared-procedure and
    /// declared-trigger bodies. Set by the host (e.g., `uni-db`'s
    /// `Uni::build` flow) at construction time. When `None`, declared
    /// procedures/triggers are recorded + persisted but no executable
    /// plugin is registered (today's pre-M11 behavior).
    procedure_synthesizer: Option<Arc<dyn ProcedureBodySynthesizer>>,
    manifest: std::sync::OnceLock<uni_plugin::PluginManifest>,
}

/// Host callback that turns a declared-procedure record into an
/// executable [`uni_plugin::traits::procedure::ProcedurePlugin`].
///
/// `uni-plugin-custom` cannot reach the host's
/// `QueryProcedureHost::execute_inner_query` directly (no dep on
/// `uni-query`), so the M9 cutover for declared-procedure body
/// execution flows through this callback. `uni-db` implements
/// [`ProcedureBodySynthesizer`] using
/// `uni_query::QueryProcedureHost::execute_inner_query` and passes
/// the impl to [`CustomPlugin::with_procedure_synthesizer`].
pub trait ProcedureBodySynthesizer: Send + Sync + std::fmt::Debug {
    /// Build a `ProcedurePlugin` whose `invoke()` runs the Cypher /
    /// Locy body of `decl`. Returns the synthesized plugin (which the
    /// caller registers into the [`PluginRegistry`]) or a string
    /// reason for failure.
    ///
    /// # Errors
    ///
    /// Returns a free-form error string on synthesis failure (bad
    /// signature shape, body parse errors, capability gaps).
    fn synthesize(
        &self,
        decl: &DeclaredPlugin,
    ) -> Result<Arc<dyn uni_plugin::traits::procedure::ProcedurePlugin>, String>;
}

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

impl CustomPlugin {
    /// Reserved plugin id.
    pub const ID: &'static str = "custom";

    /// Construct with the given registry handle and persistence
    /// backend.
    ///
    /// On construction, the persistence backend is queried for every
    /// previously declared plugin and each one is re-installed into
    /// `store` (re-registration into `registry` happens lazily — the
    /// first time the plugin is invoked, or eagerly through
    /// [`Self::reactivate_into_registry`]).
    ///
    /// # Errors
    ///
    /// Returns [`CustomError::Persistence`] if the backend's
    /// `load_all` fails.
    pub fn new(
        registry: Arc<PluginRegistry>,
        persistence: Arc<dyn Persistence>,
    ) -> Result<Self, CustomError> {
        let store = Arc::new(DeclaredPluginStore::new());
        let initial = persistence.load_all()?;
        for plugin in initial {
            // Reinsert with relaxed validation — persisted records
            // may include forward references that the store's
            // dependency check would reject during one-by-one
            // insertion. We trust persisted data.
            store.declare_unchecked(plugin);
        }
        Ok(Self {
            store,
            registry,
            persistence,
            procedure_synthesizer: None,
            manifest: std::sync::OnceLock::new(),
        })
    }

    /// Attach a host-side synthesizer so declared procedures (and
    /// triggers) can install executable plugins at declare time.
    ///
    /// The host (uni-db) calls this immediately after [`Self::new`].
    /// Synthesizer-less construction remains valid — declared
    /// procedures/triggers are recorded + persisted but not
    /// registered as invocable plugins.
    #[must_use]
    pub fn with_procedure_synthesizer(
        mut self,
        synthesizer: Arc<dyn ProcedureBodySynthesizer>,
    ) -> Self {
        self.procedure_synthesizer = Some(synthesizer);
        self
    }

    /// Construct with no persistence (in-memory only) and a fresh
    /// [`PluginRegistry`] handle.
    ///
    /// Used by tests that exercise the meta-plugin in isolation.
    #[must_use]
    pub fn new_in_memory() -> Self {
        Self::new(Arc::new(PluginRegistry::new()), Arc::new(NullPersistence))
            .expect("NullPersistence cannot fail")
    }

    /// Access the underlying declared-plugin store.
    #[must_use]
    pub fn store(&self) -> &Arc<DeclaredPluginStore> {
        &self.store
    }

    /// Access the shared registry handle.
    #[must_use]
    pub fn registry(&self) -> &Arc<PluginRegistry> {
        &self.registry
    }

    /// Replay every persisted declaration into the registry.
    ///
    /// Called by the host immediately after [`Self::new`] so that
    /// declarations survive restart. Skips declarations whose qname
    /// is already registered as a native plugin (they remain marked
    /// `active=false` in the store).
    ///
    /// # Errors
    ///
    /// Returns [`CustomError::Registration`] on registrar errors
    /// other than `DuplicateRegistration` (which is expected for
    /// shadowed declarations and downgrades the record to inactive).
    pub fn reactivate_into_registry(&self) -> Result<(), CustomError> {
        let mut records = self.store.list();
        records.sort_by_key(|a| a.dependencies.len());
        for record in records {
            let install_result = match record.kind.as_str() {
                "function" => procedures::install_function_into_registry(&self.registry, &record),
                "aggregate" => {
                    crate::aggregate::install_aggregate_into_registry(&self.registry, &record)
                }
                "procedure" | "trigger" => {
                    // M11 A.3: if the host wired a procedure-body
                    // synthesizer (uni-db installs one at Uni::build
                    // time), use it to register an executable
                    // SyntheticProcedurePlugin. Otherwise this is a
                    // record-only declaration (pre-M11 behavior).
                    match self.procedure_synthesizer.as_ref() {
                        Some(synth) => procedures::install_synthesized_procedure(
                            &self.registry,
                            &record,
                            synth.as_ref(),
                        ),
                        None => continue,
                    }
                }
                _ => continue,
            };
            let mut record = record;
            match install_result {
                Ok(()) => {}
                Err(CustomError::NativeShadow(_)) => {
                    record.active = false;
                    self.store.replace(record.clone());
                    let _ = self.persistence.save(&record);
                }
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }

    fn manifest_value() -> uni_plugin::PluginManifest {
        use semver::Version;
        use uni_plugin::{
            AbiRange, Capability, CapabilitySet, Determinism, PluginId, PluginManifest,
            ProvidedSurfaces, Scope, SideEffects,
        };
        PluginManifest {
            id: PluginId::new(Self::ID),
            version: env!("CARGO_PKG_VERSION")
                .parse::<Version>()
                .unwrap_or_else(|_| Version::new(0, 0, 0)),
            abi: AbiRange::parse("^1").expect("manifest ABI range is valid"),
            depends_on: vec![],
            capabilities: CapabilitySet::from_iter_of([
                Capability::Procedure,
                Capability::ProcedureWrites,
                Capability::PluginDeclare,
            ]),
            determinism: Determinism::Nondeterministic,
            side_effects: SideEffects::ReadOnly,
            scope: Scope::Instance,
            hash: None,
            signature: None,
            provides: ProvidedSurfaces::default(),
            docs: "apoc.custom-style meta-plugin: declare procedures / functions / aggregates / triggers from Cypher."
                .to_owned(),
            metadata: std::collections::BTreeMap::new(),
        }
    }
}

impl uni_plugin::Plugin for CustomPlugin {
    fn manifest(&self) -> &uni_plugin::PluginManifest {
        self.manifest.get_or_init(Self::manifest_value)
    }

    fn register(
        &self,
        r: &mut uni_plugin::PluginRegistrar<'_>,
    ) -> Result<(), uni_plugin::PluginError> {
        use uni_plugin::QName;

        r.procedure(
            QName::new(Self::ID, "plugin.listDeclared"),
            procedures::list_declared_signature(),
            std::sync::Arc::new(procedures::ListDeclaredProcedure::new(Arc::clone(
                &self.store,
            ))),
        )?;
        r.procedure(
            QName::new(Self::ID, "plugin.dropDeclared"),
            procedures::drop_declared_signature(),
            std::sync::Arc::new(procedures::DropDeclaredProcedure::new(
                Arc::clone(&self.store),
                Arc::clone(&self.persistence),
                Arc::clone(&self.registry),
            )),
        )?;
        r.procedure(
            QName::new(Self::ID, "plugin.declareFunction"),
            procedures::declare_function_signature(),
            std::sync::Arc::new(procedures::DeclareFunctionProcedure::new(
                Arc::clone(&self.store),
                Arc::clone(&self.persistence),
                Arc::clone(&self.registry),
            )),
        )?;
        r.procedure(
            QName::new(Self::ID, "plugin.declareProcedure"),
            procedures::declare_procedure_signature(),
            std::sync::Arc::new(match self.procedure_synthesizer.as_ref() {
                Some(synth) => procedures::DeclareProcedureProcedure::new_with_synthesis(
                    Arc::clone(&self.store),
                    Arc::clone(&self.persistence),
                    Arc::clone(&self.registry),
                    Arc::clone(synth),
                ),
                None => procedures::DeclareProcedureProcedure::new(
                    Arc::clone(&self.store),
                    Arc::clone(&self.persistence),
                ),
            }),
        )?;
        r.procedure(
            QName::new(Self::ID, "plugin.declareAggregate"),
            procedures::declare_aggregate_signature(),
            std::sync::Arc::new(procedures::DeclareAggregateProcedure::new(
                Arc::clone(&self.store),
                Arc::clone(&self.persistence),
                Arc::clone(&self.registry),
            )),
        )?;
        r.procedure(
            QName::new(Self::ID, "plugin.declareTrigger"),
            procedures::declare_trigger_signature(),
            std::sync::Arc::new(match self.procedure_synthesizer.as_ref() {
                Some(synth) => procedures::DeclareTriggerProcedure::new_with_synthesis(
                    Arc::clone(&self.store),
                    Arc::clone(&self.persistence),
                    Arc::clone(&self.registry),
                    Arc::clone(synth),
                ),
                None => procedures::DeclareTriggerProcedure::new(
                    Arc::clone(&self.store),
                    Arc::clone(&self.persistence),
                ),
            }),
        )?;
        Ok(())
    }
}

/// M9-shipped procedures fronting the declared-plugin store.
pub mod procedures {
    use std::sync::Arc;

    use arrow_array::builder::{BooleanBuilder, StringBuilder};
    use arrow_array::{Array, BooleanArray, RecordBatch, StringArray};
    use arrow_schema::{DataType, Field, Schema, SchemaRef};
    use datafusion::execution::SendableRecordBatchStream;
    use datafusion::logical_expr::ColumnarValue;
    use datafusion::physical_plan::stream::RecordBatchStreamAdapter;
    use datafusion::scalar::ScalarValue;
    use futures::stream;
    use semver::Version;
    use uni_cypher::parse_expression;
    use uni_plugin::traits::procedure::{
        NamedArgType, ProcedureContext, ProcedureMode, ProcedurePlugin, ProcedureSignature,
    };
    use uni_plugin::traits::scalar::{ArgType, ScalarPluginFn};
    use uni_plugin::{
        AbiRange, Capability, CapabilitySet, Determinism, FnError, Plugin, PluginError, PluginId,
        PluginManifest, PluginRegistrar, PluginRegistry, ProvidedSurfaces, QName, Scope,
        SideEffects,
    };

    use super::{CustomError, DeclaredPlugin, DeclaredPluginStore, DeclaredScalarFn, Persistence};
    use crate::decode::{declared_plugin_id, local_part, map_plugin_error, type_str_to_arrow};

    // -------------------------------------------------------------
    // Signatures
    // -------------------------------------------------------------

    /// Signature for `uni.plugin.listDeclared`.
    #[must_use]
    pub fn list_declared_signature() -> ProcedureSignature {
        ProcedureSignature {
            args: vec![],
            yields: vec![
                Field::new("qname", DataType::Utf8, false),
                Field::new("kind", DataType::Utf8, false),
                Field::new("declared_by", DataType::Utf8, false),
                Field::new("active", DataType::Boolean, false),
            ],
            mode: ProcedureMode::Read,
            side_effects: SideEffects::ReadOnly,
            retry_contract: None,
            batch_input: None,
            docs: "List every declared plugin (apoc.custom analogue) with its kind, declarer, and active state.".to_owned(),
        }
    }

    /// Signature for `uni.plugin.dropDeclared`.
    #[must_use]
    pub fn drop_declared_signature() -> ProcedureSignature {
        write_signature(
            vec![named_arg(
                "qname",
                DataType::Utf8,
                "Qualified name of the declared plugin to drop.",
            )],
            "removed",
            "Drop a previously declared plugin. Errors if other declared plugins depend on it.",
        )
    }

    fn named_arg(name: &str, ty: DataType, doc: &str) -> NamedArgType {
        NamedArgType {
            name: smol_str::SmolStr::new(name),
            ty: ArgType::Primitive(ty),
            default: None,
            doc: doc.to_owned(),
        }
    }

    /// Variant of [`named_arg`] that records a default value for the
    /// arg.
    ///
    /// Note: today's procedure dispatch in
    /// `crates/uni-query/src/query/df_graph/procedure_call.rs` does not
    /// auto-fill defaults from the signature; the declare* procedures
    /// instead read the default through [`extract_string_or`]. The
    /// `default` field stays informative for tooling and the eventual
    /// dispatch-side default expansion.
    fn named_arg_default(name: &str, ty: DataType, doc: &str, default: &str) -> NamedArgType {
        NamedArgType {
            name: smol_str::SmolStr::new(name),
            ty: ArgType::Primitive(ty),
            default: Some(ScalarValue::Utf8(Some(default.to_owned()))),
            doc: doc.to_owned(),
        }
    }

    /// Doc string for the trailing `deps_json` arg shared by every
    /// declare* signature.
    const DEPS_JSON_DOC: &str =
        "JSON array of qualified names this declaration depends on (empty by default).";

    fn deps_arg() -> NamedArgType {
        named_arg_default("deps_json", DataType::Utf8, DEPS_JSON_DOC, "[]")
    }

    /// Build a `Write`-mode [`ProcedureSignature`] that yields a single
    /// boolean column.
    ///
    /// Shared by `dropDeclared` and the four `declare*` signatures,
    /// which differ only in their args, the yielded column name, and
    /// the docstring.
    fn write_signature(args: Vec<NamedArgType>, yield_col: &str, docs: &str) -> ProcedureSignature {
        ProcedureSignature {
            args,
            yields: vec![Field::new(yield_col, DataType::Boolean, false)],
            mode: ProcedureMode::Write,
            side_effects: SideEffects::ReadOnly,
            retry_contract: None,
            batch_input: None,
            docs: docs.to_owned(),
        }
    }

    /// Signature for `uni.plugin.declareFunction`.
    #[must_use]
    pub fn declare_function_signature() -> ProcedureSignature {
        write_signature(
            vec![
                named_arg("qname", DataType::Utf8, "Qualified name to register."),
                named_arg("body", DataType::Utf8, "Cypher expression body."),
                named_arg(
                    "return_type",
                    DataType::Utf8,
                    "Return type ('string', 'int', 'float', 'bool').",
                ),
                named_arg(
                    "arg_names_json",
                    DataType::Utf8,
                    "JSON array of argument names, in positional order.",
                ),
                deps_arg(),
            ],
            "registered",
            "Declare a new scalar function. Body is a Cypher expression; arguments are bound by name (positional).",
        )
    }

    /// Signature for `uni.plugin.declareProcedure`.
    #[must_use]
    pub fn declare_procedure_signature() -> ProcedureSignature {
        write_signature(
            vec![
                named_arg("qname", DataType::Utf8, "Qualified name to register."),
                named_arg("body", DataType::Utf8, "Cypher query body."),
                named_arg("mode", DataType::Utf8, "'READ' or 'WRITE'."),
                named_arg(
                    "yield_json",
                    DataType::Utf8,
                    "JSON array describing yielded columns.",
                ),
                deps_arg(),
            ],
            "registered",
            "Declare a new procedure. The body is a full Cypher query; arguments are bound by name.",
        )
    }

    /// Signature for `uni.plugin.declareAggregate`.
    #[must_use]
    pub fn declare_aggregate_signature() -> ProcedureSignature {
        write_signature(
            vec![
                named_arg("qname", DataType::Utf8, "Qualified name to register."),
                named_arg(
                    "init_expr",
                    DataType::Utf8,
                    "Init state expression (no parameters).",
                ),
                named_arg(
                    "update_expr",
                    DataType::Utf8,
                    "Update step expression; binds `$state` plus per-row args.",
                ),
                named_arg(
                    "finalize_expr",
                    DataType::Utf8,
                    "Finalize expression; binds `$state`.",
                ),
                named_arg_default(
                    "return_type",
                    DataType::Utf8,
                    "Return type ('string', 'int', 'float', 'bool').",
                    "float",
                ),
                named_arg_default(
                    "arg_names_json",
                    DataType::Utf8,
                    "JSON array of update-arg names, in positional order.",
                    "[]",
                ),
                deps_arg(),
            ],
            "registered",
            "Declare a new aggregate function from Cypher init / update / finalize expressions.",
        )
    }

    /// Signature for `uni.plugin.declareTrigger`.
    #[must_use]
    pub fn declare_trigger_signature() -> ProcedureSignature {
        write_signature(
            vec![
                named_arg("qname", DataType::Utf8, "Qualified name to register."),
                named_arg(
                    "event_filter",
                    DataType::Utf8,
                    "Event filter (label or relationship pattern).",
                ),
                named_arg(
                    "body",
                    DataType::Utf8,
                    "Cypher body to execute when the trigger fires.",
                ),
                deps_arg(),
            ],
            "registered",
            "Declare a new trigger that fires the given Cypher body on matched mutation events.",
        )
    }

    // -------------------------------------------------------------
    // listDeclared / dropDeclared
    // -------------------------------------------------------------

    /// Implementation of `uni.plugin.listDeclared`.
    #[derive(Debug)]
    pub struct ListDeclaredProcedure {
        store: Arc<DeclaredPluginStore>,
    }

    impl ListDeclaredProcedure {
        /// Construct.
        #[must_use]
        pub fn new(store: Arc<DeclaredPluginStore>) -> Self {
            Self { store }
        }
    }

    impl ProcedurePlugin for ListDeclaredProcedure {
        fn signature(&self) -> &ProcedureSignature {
            static SIG: std::sync::OnceLock<ProcedureSignature> = std::sync::OnceLock::new();
            SIG.get_or_init(list_declared_signature)
        }

        fn invoke(
            &self,
            _ctx: ProcedureContext<'_>,
            _args: &[ColumnarValue],
        ) -> Result<SendableRecordBatchStream, FnError> {
            let rows = self.store.list();
            let mut qname = StringBuilder::new();
            let mut kind = StringBuilder::new();
            let mut declared_by = StringBuilder::new();
            let mut active = BooleanBuilder::new();
            for r in rows {
                qname.append_value(&r.qname);
                kind.append_value(&r.kind);
                declared_by.append_value(&r.declared_by);
                active.append_value(r.active);
            }
            let schema: SchemaRef = Arc::new(Schema::new(vec![
                Field::new("qname", DataType::Utf8, false),
                Field::new("kind", DataType::Utf8, false),
                Field::new("declared_by", DataType::Utf8, false),
                Field::new("active", DataType::Boolean, false),
            ]));
            let cols: Vec<Arc<dyn Array>> = vec![
                Arc::new(qname.finish()),
                Arc::new(kind.finish()),
                Arc::new(declared_by.finish()),
                Arc::new(active.finish()),
            ];
            let batch = RecordBatch::try_new(Arc::clone(&schema), cols)
                .map_err(|e| FnError::new(0xB00, format!("listDeclared: {e}")))?;
            Ok(Box::pin(RecordBatchStreamAdapter::new(
                schema,
                stream::iter(vec![Ok(batch)]),
            )))
        }
    }

    /// Implementation of `uni.plugin.dropDeclared`.
    #[derive(Debug)]
    pub struct DropDeclaredProcedure {
        store: Arc<DeclaredPluginStore>,
        persistence: Arc<dyn Persistence>,
        registry: Arc<PluginRegistry>,
    }

    impl DropDeclaredProcedure {
        /// Construct.
        #[must_use]
        pub fn new(
            store: Arc<DeclaredPluginStore>,
            persistence: Arc<dyn Persistence>,
            registry: Arc<PluginRegistry>,
        ) -> Self {
            Self {
                store,
                persistence,
                registry,
            }
        }
    }

    impl ProcedurePlugin for DropDeclaredProcedure {
        fn signature(&self) -> &ProcedureSignature {
            static SIG: std::sync::OnceLock<ProcedureSignature> = std::sync::OnceLock::new();
            SIG.get_or_init(drop_declared_signature)
        }

        fn invoke(
            &self,
            _ctx: ProcedureContext<'_>,
            args: &[ColumnarValue],
        ) -> Result<SendableRecordBatchStream, FnError> {
            let qname = extract_string(args, 0, "qname")?;
            let existed = self
                .store
                .drop_declared(&qname)
                .map_err(|e| FnError::new(0xB01, format!("dropDeclared: {e}")))?;
            if existed {
                // Remove from registry — bound to the synthetic
                // plugin id we registered under (the qname's
                // namespace). `remove_plugin` is idempotent.
                let pid = PluginId::new(declared_plugin_id(&qname));
                self.registry.remove_plugin(&pid);
                self.persistence
                    .delete(&qname)
                    .map_err(|e| FnError::new(0xB01, format!("dropDeclared persist: {e}")))?;
            }
            single_bool("removed", existed)
        }
    }

    // -------------------------------------------------------------
    // declareFunction
    // -------------------------------------------------------------

    /// Implementation of `uni.plugin.declareFunction`.
    #[derive(Debug)]
    pub struct DeclareFunctionProcedure {
        store: Arc<DeclaredPluginStore>,
        persistence: Arc<dyn Persistence>,
        registry: Arc<PluginRegistry>,
    }

    impl DeclareFunctionProcedure {
        /// Construct.
        #[must_use]
        pub fn new(
            store: Arc<DeclaredPluginStore>,
            persistence: Arc<dyn Persistence>,
            registry: Arc<PluginRegistry>,
        ) -> Self {
            Self {
                store,
                persistence,
                registry,
            }
        }
    }

    impl ProcedurePlugin for DeclareFunctionProcedure {
        fn signature(&self) -> &ProcedureSignature {
            static SIG: std::sync::OnceLock<ProcedureSignature> = std::sync::OnceLock::new();
            SIG.get_or_init(declare_function_signature)
        }

        fn invoke(
            &self,
            ctx: ProcedureContext<'_>,
            args: &[ColumnarValue],
        ) -> Result<SendableRecordBatchStream, FnError> {
            let qname = extract_string(args, 0, "qname")?;
            let body = extract_string(args, 1, "body")?;
            let return_type = extract_string(args, 2, "return_type")?;
            let arg_names_json = extract_string(args, 3, "arg_names_json")?;
            let arg_names: Vec<String> = serde_json::from_str(&arg_names_json).map_err(|e| {
                FnError::new(
                    FnError::CODE_TYPE_COERCION,
                    format!("declareFunction: arg_names_json parse: {e}"),
                )
            })?;
            let dependencies = parse_deps(args, 4)?;
            let declared_by = ctx
                .principal
                .map(|p| p.id.clone())
                .unwrap_or_else(|| "anonymous".to_owned());

            let record = DeclaredPlugin {
                qname: qname.clone(),
                kind: "function".to_owned(),
                body,
                signature_json: serde_json::to_string(&serde_json::json!({
                    "return_type": return_type,
                    "arg_names": arg_names,
                }))
                .unwrap_or_else(|_| "{}".to_owned()),
                dependencies,
                declared_by,
                active: true,
            };

            self.store
                .declare(record.clone())
                .map_err(custom_to_fn_err)?;

            match install_function_into_registry(&self.registry, &record) {
                Ok(()) => {}
                Err(CustomError::NativeShadow(_)) => {
                    let mut record = record.clone();
                    record.active = false;
                    self.store.replace(record.clone());
                    self.persistence.save(&record).map_err(|e| {
                        FnError::new(0xB20, format!("declareFunction persist: {e}"))
                    })?;
                    return single_bool("registered", false);
                }
                Err(e) => {
                    // Roll back the store entry on registration failure.
                    let _ = self.store.drop_declared(&qname);
                    return Err(custom_to_fn_err(e));
                }
            }

            self.persistence
                .save(&record)
                .map_err(|e| FnError::new(0xB20, format!("declareFunction persist: {e}")))?;

            single_bool("registered", true)
        }
    }

    /// Compile a declared-function record into a [`DeclaredScalarFn`]
    /// and register it into `registry` under a synthetic plugin id
    /// derived from the qname's namespace.
    ///
    /// # Errors
    ///
    /// Returns [`CustomError::BodyParse`] if the body fails Cypher
    /// expression parsing, [`CustomError::NativeShadow`] if the qname
    /// is already taken in `registry`, or [`CustomError::Registration`]
    /// on other registrar errors.
    pub fn install_function_into_registry(
        registry: &Arc<PluginRegistry>,
        record: &DeclaredPlugin,
    ) -> Result<(), CustomError> {
        let parsed_body =
            parse_expression(&record.body).map_err(|e| CustomError::BodyParse(format!("{e:?}")))?;
        let sig_meta: serde_json::Value = serde_json::from_str(&record.signature_json)
            .map_err(|e| CustomError::BodyParse(format!("signature_json: {e}")))?;
        let return_type_str = sig_meta
            .get("return_type")
            .and_then(|v| v.as_str())
            .unwrap_or("string");
        let arg_names: Vec<String> = sig_meta
            .get("arg_names")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(str::to_owned))
                    .collect()
            })
            .unwrap_or_default();

        let return_dt = type_str_to_arrow(return_type_str).ok_or_else(|| {
            CustomError::BodyParse(format!("unknown return type `{return_type_str}`"))
        })?;
        let arg_pairs: Vec<(String, DataType)> = arg_names
            .iter()
            .map(|n| (n.clone(), DataType::Utf8))
            .collect();
        let signature = DeclaredScalarFn::build_signature(return_dt, &arg_pairs);
        let scalar_fn = DeclaredScalarFn::new(parsed_body, arg_names, signature.clone());

        // Cypher canonicalizes function names to lowercase at
        // lookup time; mirror that here so user-declared camelCase
        // qnames are still resolvable.
        let qname = QName::new(
            declared_plugin_id(&record.qname),
            local_part(&record.qname).to_ascii_lowercase(),
        );
        let plugin = SyntheticScalarPlugin {
            plugin_id: PluginId::new(declared_plugin_id(&record.qname)),
            qname,
            signature,
            function: Arc::new(scalar_fn) as Arc<dyn ScalarPluginFn>,
            manifest: std::sync::OnceLock::new(),
        };
        let manifest = plugin.manifest().clone();
        let caps = manifest.capabilities.clone();
        let mut r = PluginRegistrar::new(manifest.id, &caps, registry);
        plugin
            .register(&mut r)
            .map_err(|e| map_plugin_error(e, &record.qname))?;
        r.commit_to_registry()
            .map_err(|e| map_plugin_error(e, &record.qname))?;
        Ok(())
    }

    /// Install a synthesized procedure (M9 cutover, M11 A.3).
    ///
    /// The synthesizer builds a host-side `ProcedurePlugin` whose
    /// `invoke()` runs the declared body via the write-enabled
    /// `QueryProcedureHost::execute_inner_query`. We pull its
    /// `signature()` and register it under the declared qname.
    pub(super) fn install_synthesized_procedure(
        registry: &Arc<PluginRegistry>,
        record: &DeclaredPlugin,
        synthesizer: &dyn crate::ProcedureBodySynthesizer,
    ) -> Result<(), CustomError> {
        let plugin = synthesizer
            .synthesize(record)
            .map_err(CustomError::Registration)?;
        let qname = QName::new(
            declared_plugin_id(&record.qname),
            local_part(&record.qname).to_ascii_lowercase(),
        );
        let signature = plugin.signature().clone();
        let caps = {
            let mut s = uni_plugin::CapabilitySet::new();
            s.insert(uni_plugin::Capability::Procedure);
            // Inherit declared write/schema/dbms variant from the
            // signature so the registrar's capability gate accepts
            // the registration.
            match signature.mode {
                uni_plugin::traits::procedure::ProcedureMode::Write => {
                    s.insert(uni_plugin::Capability::ProcedureWrites);
                }
                uni_plugin::traits::procedure::ProcedureMode::Schema => {
                    s.insert(uni_plugin::Capability::ProcedureSchema);
                }
                uni_plugin::traits::procedure::ProcedureMode::Dbms => {
                    s.insert(uni_plugin::Capability::ProcedureDbms);
                }
                // `Read` and any future modes require no extra
                // capability beyond the base `Procedure`.
                _ => {}
            }
            s
        };
        let plugin_id = uni_plugin::PluginId::new(declared_plugin_id(&record.qname));
        let mut r = PluginRegistrar::new(plugin_id, &caps, registry);
        r.procedure(qname, signature, plugin)
            .map_err(|e| map_plugin_error(e, &record.qname))?;
        r.commit_to_registry()
            .map_err(|e| map_plugin_error(e, &record.qname))?;
        Ok(())
    }

    /// Synthetic [`Plugin`] wrapping a single declared scalar function.
    struct SyntheticScalarPlugin {
        plugin_id: PluginId,
        qname: QName,
        signature: uni_plugin::traits::scalar::FnSignature,
        function: Arc<dyn ScalarPluginFn>,
        /// Lazily-built, then cached, manifest. Each synthetic plugin
        /// has a distinct manifest, so it cannot be a shared static;
        /// the `OnceLock` gives `manifest()` a stable `&` reference
        /// without leaking a fresh `Box` on every call.
        manifest: std::sync::OnceLock<PluginManifest>,
    }

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

    impl SyntheticScalarPlugin {
        fn build_manifest(&self) -> PluginManifest {
            PluginManifest {
                id: self.plugin_id.clone(),
                version: Version::new(0, 0, 1),
                abi: AbiRange::parse("^1").expect("manifest ABI range is valid"),
                depends_on: vec![],
                capabilities: CapabilitySet::from_iter_of([Capability::ScalarFn]),
                determinism: Determinism::Pure,
                side_effects: SideEffects::ReadOnly,
                scope: Scope::Instance,
                hash: None,
                signature: None,
                provides: ProvidedSurfaces::default(),
                docs: "Declared scalar function (apoc.custom analogue).".to_owned(),
                metadata: std::collections::BTreeMap::new(),
            }
        }
    }

    impl Plugin for SyntheticScalarPlugin {
        fn manifest(&self) -> &PluginManifest {
            self.manifest.get_or_init(|| self.build_manifest())
        }

        fn register(&self, r: &mut PluginRegistrar<'_>) -> Result<(), PluginError> {
            r.scalar_fn(
                self.qname.clone(),
                self.signature.clone(),
                Arc::clone(&self.function),
            )?;
            Ok(())
        }
    }

    // -------------------------------------------------------------
    // declareAggregate
    // -------------------------------------------------------------

    /// Implementation of `uni.plugin.declareAggregate`.
    ///
    /// Parses three Cypher expression bodies (`init` / `update` /
    /// `finalize`) at declare time, persists a
    /// [`DeclaredPlugin`] record with kind `"aggregate"`, and registers
    /// a synthetic [`uni_plugin::traits::aggregate::AggregatePluginFn`]
    /// (`DeclaredAggregateFn`) into the shared registry. The new
    /// aggregate becomes invokable from Cypher (`RETURN myAgg(x)`) via
    /// the planner fall-through to
    /// `crate::query::df_udaf_plugin::PluginAggregateUdaf` in
    /// `uni-query`.
    #[derive(Debug)]
    pub struct DeclareAggregateProcedure {
        store: Arc<DeclaredPluginStore>,
        persistence: Arc<dyn Persistence>,
        registry: Arc<PluginRegistry>,
    }

    impl DeclareAggregateProcedure {
        /// Construct.
        #[must_use]
        pub fn new(
            store: Arc<DeclaredPluginStore>,
            persistence: Arc<dyn Persistence>,
            registry: Arc<PluginRegistry>,
        ) -> Self {
            Self {
                store,
                persistence,
                registry,
            }
        }
    }

    impl ProcedurePlugin for DeclareAggregateProcedure {
        fn signature(&self) -> &ProcedureSignature {
            static SIG: std::sync::OnceLock<ProcedureSignature> = std::sync::OnceLock::new();
            SIG.get_or_init(declare_aggregate_signature)
        }

        fn invoke(
            &self,
            ctx: ProcedureContext<'_>,
            args: &[ColumnarValue],
        ) -> Result<SendableRecordBatchStream, FnError> {
            let qname = extract_string(args, 0, "qname")?;
            let init_src = extract_string(args, 1, "init_expr")?;
            let update_src = extract_string(args, 2, "update_expr")?;
            let finalize_src = extract_string(args, 3, "finalize_expr")?;
            let return_type = extract_string_or(args, 4, "float");
            let arg_names_json = extract_string_or(args, 5, "[]");
            let arg_names: Vec<String> = serde_json::from_str(&arg_names_json).map_err(|e| {
                FnError::new(
                    FnError::CODE_TYPE_COERCION,
                    format!("declareAggregate: arg_names_json parse: {e}"),
                )
            })?;
            let dependencies = parse_deps(args, 6)?;
            let declared_by = ctx
                .principal
                .map(|p| p.id.clone())
                .unwrap_or_else(|| "anonymous".to_owned());

            let record = DeclaredPlugin {
                qname: qname.clone(),
                kind: "aggregate".to_owned(),
                // `body` is informational — the three Cypher source
                // strings travel through `signature_json` (single JSON
                // blob) so persistence round-trips them together.
                body: update_src.clone(),
                signature_json: serde_json::to_string(&serde_json::json!({
                    "init": init_src,
                    "update": update_src,
                    "finalize": finalize_src,
                    "return_type": return_type,
                    "arg_names": arg_names,
                }))
                .unwrap_or_else(|_| "{}".to_owned()),
                dependencies,
                declared_by,
                active: true,
            };

            self.store
                .declare(record.clone())
                .map_err(custom_to_fn_err)?;

            match crate::aggregate::install_aggregate_into_registry(&self.registry, &record) {
                Ok(()) => {}
                Err(CustomError::NativeShadow(_)) => {
                    let mut record = record.clone();
                    record.active = false;
                    self.store.replace(record.clone());
                    self.persistence.save(&record).map_err(|e| {
                        FnError::new(0xB21, format!("declareAggregate persist: {e}"))
                    })?;
                    return single_bool("registered", false);
                }
                Err(e) => {
                    let _ = self.store.drop_declared(&qname);
                    return Err(custom_to_fn_err(e));
                }
            }

            self.persistence
                .save(&record)
                .map_err(|e| FnError::new(0xB21, format!("declareAggregate persist: {e}")))?;

            single_bool("registered", true)
        }
    }

    // -------------------------------------------------------------
    // declareProcedure / declareTrigger
    // (record-and-persist; full body execution rides on M11's
    // write-enabled `ProcedureHost::execute_inner_query`)
    // -------------------------------------------------------------

    macro_rules! declare_kind_procedure {
        ($name:ident, $sig_fn:ident, $kind:literal, $field_count:literal) => {
            /// Record-and-persist implementation for a declare* kind.
            ///
            /// Stores the declaration through [`Persistence`]. When a
            /// host-supplied procedure-body synthesizer is attached,
            /// the declaration also installs an executable plugin via
            /// `crate::procedures::install_synthesized_procedure`
            /// (M11 A.3).
            #[derive(Debug)]
            pub struct $name {
                store: Arc<DeclaredPluginStore>,
                persistence: Arc<dyn Persistence>,
                registry: Arc<uni_plugin::PluginRegistry>,
                synthesizer:
                    Option<Arc<dyn crate::ProcedureBodySynthesizer>>,
            }

            impl $name {
                /// Construct without a synthesizer (record-only).
                #[must_use]
                pub fn new(
                    store: Arc<DeclaredPluginStore>,
                    persistence: Arc<dyn Persistence>,
                ) -> Self {
                    Self {
                        store,
                        persistence,
                        registry: Arc::new(uni_plugin::PluginRegistry::new()),
                        synthesizer: None,
                    }
                }

                /// Construct with a host-supplied synthesizer so
                /// declarations install executable plugins at
                /// declare time (M11 A.3).
                #[must_use]
                pub fn new_with_synthesis(
                    store: Arc<DeclaredPluginStore>,
                    persistence: Arc<dyn Persistence>,
                    registry: Arc<uni_plugin::PluginRegistry>,
                    synthesizer: Arc<dyn crate::ProcedureBodySynthesizer>,
                ) -> Self {
                    Self {
                        store,
                        persistence,
                        registry,
                        synthesizer: Some(synthesizer),
                    }
                }
            }

            impl ProcedurePlugin for $name {
                fn signature(&self) -> &ProcedureSignature {
                    static SIG: std::sync::OnceLock<ProcedureSignature> =
                        std::sync::OnceLock::new();
                    SIG.get_or_init($sig_fn)
                }

                fn invoke(
                    &self,
                    ctx: ProcedureContext<'_>,
                    args: &[ColumnarValue],
                ) -> Result<SendableRecordBatchStream, FnError> {
                    let qname = extract_string(args, 0, "qname")?;
                    // Name the persisted keys after the declared
                    // signature's positional args (e.g. `body`,
                    // `event_filter`, `yield_json`) instead of opaque
                    // `arg1`/`arg2` placeholders.
                    let sig_args = $sig_fn().args;
                    let mut sig = serde_json::Map::new();
                    // `$field_count - 1` skips the trailing `deps_json`
                    // arg, which is parsed separately via `parse_deps`.
                    for i in 1..($field_count - 1) {
                        let key = sig_args
                            .get(i)
                            .map(|a| a.name.to_string())
                            .unwrap_or_else(|| format!("arg{i}"));
                        let v = extract_string(args, i, &key)?;
                        sig.insert(key, serde_json::Value::String(v));
                    }
                    // M11 A.1: for procedure-kind declarations, extract
                    // the `mode` arg (position 2 — qname=0, body=1,
                    // mode=2) and (a) gate WRITE-mode declarations on
                    // the principal's `ProcedureWrites` capability,
                    // (b) stash `mode` under a named key so the host's
                    // `SyntheticProcedurePlugin` can read it back
                    // without relying on positional `arg2`.
                    if $kind == "procedure" {
                        if let Ok(mode_str) = extract_string(args, 2, "mode") {
                            let mode_uc = mode_str.to_ascii_uppercase();
                            if mode_uc == "WRITE" {
                                let has_writes = ctx
                                    .principal
                                    .map(|p| {
                                        p.capabilities.contains_variant(
                                            &uni_plugin::Capability::ProcedureWrites,
                                        )
                                    })
                                    .unwrap_or(false);
                                if !has_writes {
                                    return Err(FnError::new(
                                        0xB09,
                                        format!(
                                            "declareProcedure WRITE for `{qname}` denied: \
                                             principal lacks `Capability::ProcedureWrites`"
                                        ),
                                    ));
                                }
                            }
                            sig.insert(
                                "mode".to_owned(),
                                serde_json::Value::String(mode_uc),
                            );
                        }
                    }
                    let dependencies = parse_deps(args, $field_count - 1)?;
                    let declared_by = ctx
                        .principal
                        .map(|p| p.id.clone())
                        .unwrap_or_else(|| "anonymous".to_owned());
                    // `body` mirrors the first positional arg after
                    // `qname` (position 1), looked up by its signature
                    // name.
                    let body = sig_args
                        .get(1)
                        .map(|a| a.name.to_string())
                        .and_then(|key| sig.get(&key))
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_owned();
                    let record = DeclaredPlugin {
                        qname: qname.clone(),
                        kind: $kind.to_owned(),
                        body,
                        signature_json: serde_json::to_string(&sig).unwrap_or_default(),
                        dependencies,
                        declared_by,
                        active: true,
                    };
                    self.store
                        .declare(record.clone())
                        .map_err(custom_to_fn_err)?;
                    self.persistence
                        .save(&record)
                        .map_err(|e| FnError::new(0xB30, format!("declare persist: {e}")))?;
                    // M11 A.3: if the host attached a synthesizer,
                    // install the executable plugin at declare time
                    // so subsequent `CALL <qname>(...)` invocations
                    // dispatch through it.
                    if let Some(synth) = self.synthesizer.as_ref() {
                        if let Err(e) = crate::procedures::install_synthesized_procedure(
                            &self.registry,
                            &record,
                            synth.as_ref(),
                        ) {
                            // NativeShadow is expected when the qname
                            // is already taken; downgrade the record
                            // to inactive but do not fail the
                            // declaration.
                            match e {
                                CustomError::NativeShadow(_) => {
                                    let mut shadowed = record.clone();
                                    shadowed.active = false;
                                    self.store.replace(shadowed.clone());
                                    let _ = self.persistence.save(&shadowed);
                                }
                                other => {
                                    return Err(FnError::new(
                                        0xB31,
                                        format!("declare synthesize: {other}"),
                                    ));
                                }
                            }
                        }
                    }
                    single_bool("registered", true)
                }
            }
        };
    }

    declare_kind_procedure!(
        DeclareProcedureProcedure,
        declare_procedure_signature,
        "procedure",
        5
    );
    declare_kind_procedure!(
        DeclareTriggerProcedure,
        declare_trigger_signature,
        "trigger",
        4
    );

    // -------------------------------------------------------------
    // helpers
    // -------------------------------------------------------------

    /// Decode a present [`ColumnarValue`] into a Utf8 [`String`].
    ///
    /// Returns `Some` only for a non-null Utf8 scalar or the first
    /// non-null element of a `StringArray`; every other shape (null,
    /// empty array, non-Utf8) yields `None`. Shared by [`extract_string`]
    /// and [`extract_string_or`].
    fn columnar_utf8(cv: &ColumnarValue) -> Option<String> {
        match cv {
            ColumnarValue::Scalar(ScalarValue::Utf8(Some(s))) => Some(s.clone()),
            ColumnarValue::Array(arr) => arr
                .as_any()
                .downcast_ref::<StringArray>()
                .and_then(|a| a.iter().next().flatten().map(|s| s.to_owned())),
            _ => None,
        }
    }

    /// Like [`extract_string`] but returns `default` when the argument
    /// is missing, null, or not a Utf8 string. Used for trailing
    /// optional args (`deps_json`, defaulted-on-declare* signatures)
    /// since the current procedure dispatch path does not auto-fill
    /// defaults from the [`ProcedureSignature`].
    fn extract_string_or(args: &[ColumnarValue], i: usize, default: &str) -> String {
        args.get(i)
            .and_then(columnar_utf8)
            .unwrap_or_else(|| default.to_owned())
    }

    /// Parse the `deps_json` arg at position `i` into a `Vec<String>`,
    /// defaulting to an empty vec when absent or null.
    fn parse_deps(args: &[ColumnarValue], i: usize) -> Result<Vec<String>, FnError> {
        let raw = extract_string_or(args, i, "[]");
        serde_json::from_str::<Vec<String>>(&raw).map_err(|e| {
            FnError::new(
                FnError::CODE_TYPE_COERCION,
                format!("declare: deps_json parse: {e}"),
            )
        })
    }

    fn extract_string(args: &[ColumnarValue], i: usize, name: &str) -> Result<String, FnError> {
        let cv = args.get(i).ok_or_else(|| {
            FnError::new(
                FnError::CODE_TYPE_COERCION,
                format!("declare procedure missing arg `{name}` at position {i}"),
            )
        })?;
        if let Some(s) = columnar_utf8(cv) {
            return Ok(s);
        }
        // Present but unusable: keep the original diagnostics —
        // explicit null vs anything else (non-Utf8 scalar, non-string
        // array, empty / null-first array).
        let msg = match cv {
            ColumnarValue::Scalar(ScalarValue::Utf8(None) | ScalarValue::Null) => {
                format!("declare procedure arg `{name}` was null")
            }
            _ => format!("declare procedure arg `{name}` not Utf8"),
        };
        Err(FnError::new(FnError::CODE_TYPE_COERCION, msg))
    }

    fn single_bool(col: &str, v: bool) -> Result<SendableRecordBatchStream, FnError> {
        let schema: SchemaRef =
            Arc::new(Schema::new(vec![Field::new(col, DataType::Boolean, false)]));
        let arr: Arc<dyn Array> = Arc::new(BooleanArray::from(vec![v]));
        let batch = RecordBatch::try_new(Arc::clone(&schema), vec![arr])
            .map_err(|e| FnError::new(0xB02, format!("single bool: {e}")))?;
        Ok(Box::pin(RecordBatchStreamAdapter::new(
            schema,
            stream::iter(vec![Ok(batch)]),
        )))
    }

    fn custom_to_fn_err(e: CustomError) -> FnError {
        let code = match &e {
            CustomError::DependencyCycle(_) => 0xB03,
            CustomError::DependencyMissing { .. } => 0xB04,
            CustomError::NativeShadow(_) => 0xB05,
            CustomError::BodyParse(_) => 0xB06,
            CustomError::Persistence(_) => 0xB07,
            CustomError::Registration(_) => 0xB08,
            CustomError::CapabilityDenied(_) => 0xB09,
        };
        FnError::new(code, e.to_string())
    }
}

// -------------------------------------------------------------
// DeclaredPluginStore
// -------------------------------------------------------------

/// In-memory store for declared plugins.
///
/// The store is the source of truth for dependency analysis and
/// listing; persistence rides through [`Persistence`] and replays
/// the same records through this store on construction.
#[derive(Debug, Default)]
pub struct DeclaredPluginStore {
    by_qname: std::sync::RwLock<std::collections::BTreeMap<String, DeclaredPlugin>>,
}

impl DeclaredPluginStore {
    /// Construct an empty store.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Declare a new plugin or replace an existing declaration with
    /// dependency + cycle validation.
    ///
    /// # Errors
    ///
    /// Returns [`CustomError::DependencyMissing`] if any declared
    /// dependency is not present in the store. Returns
    /// [`CustomError::DependencyCycle`] if adding this plugin would
    /// introduce a cycle.
    pub fn declare(&self, plugin: DeclaredPlugin) -> Result<(), CustomError> {
        {
            let map = self.by_qname.read().expect("declared-plugin lock poisoned");
            for dep in &plugin.dependencies {
                if !map.contains_key(dep) {
                    return Err(CustomError::DependencyMissing {
                        dependent: plugin.qname.clone(),
                        dep: dep.clone(),
                    });
                }
            }
            if would_introduce_cycle(&map, &plugin) {
                return Err(CustomError::DependencyCycle(chain_starting_at(
                    &map, &plugin,
                )));
            }
        }
        self.by_qname
            .write()
            .expect("declared-plugin lock poisoned")
            .insert(plugin.qname.clone(), plugin);
        Ok(())
    }

    /// Insert / replace without dependency validation. Used by the
    /// reactivation path (records from persistence are trusted).
    pub fn declare_unchecked(&self, plugin: DeclaredPlugin) {
        self.by_qname
            .write()
            .expect("declared-plugin lock poisoned")
            .insert(plugin.qname.clone(), plugin);
    }

    /// Look up a declared plugin by qname.
    #[must_use]
    pub fn get(&self, qname: &str) -> Option<DeclaredPlugin> {
        self.by_qname
            .read()
            .expect("declared-plugin lock poisoned")
            .get(qname)
            .cloned()
    }

    /// Drop a declared plugin.
    ///
    /// Returns `true` if the plugin existed.
    ///
    /// # Errors
    ///
    /// Returns [`CustomError::DependencyMissing`] if the plugin is a
    /// dependency of another declared plugin (cascade mode lives at
    /// [`Self::drop_cascade`]).
    pub fn drop_declared(&self, qname: &str) -> Result<bool, CustomError> {
        let mut map = self
            .by_qname
            .write()
            .expect("declared-plugin lock poisoned");
        for other in map.values() {
            if other.dependencies.iter().any(|d| d == qname) {
                return Err(CustomError::DependencyMissing {
                    dependent: other.qname.clone(),
                    dep: qname.to_owned(),
                });
            }
        }
        Ok(map.remove(qname).is_some())
    }

    /// Drop a declared plugin together with every dependent.
    ///
    /// Returns the qnames removed in topological (leaves-first)
    /// order.
    pub fn drop_cascade(&self, qname: &str) -> Vec<String> {
        let mut removed = Vec::new();
        let mut map = self
            .by_qname
            .write()
            .expect("declared-plugin lock poisoned");
        let mut stack = vec![qname.to_owned()];
        while let Some(target) = stack.pop() {
            let dependents: Vec<String> = map
                .iter()
                .filter(|(_, p)| p.dependencies.iter().any(|d| d == &target))
                .map(|(k, _)| k.clone())
                .collect();
            if dependents.is_empty() {
                if map.remove(&target).is_some() {
                    removed.push(target);
                }
            } else {
                stack.push(target);
                for d in dependents {
                    stack.push(d);
                }
            }
        }
        removed
    }

    /// Replace an existing record (no validation). Used for
    /// shadow-flag updates.
    pub fn replace(&self, plugin: DeclaredPlugin) {
        self.declare_unchecked(plugin);
    }

    /// List every declared plugin.
    #[must_use]
    pub fn list(&self) -> Vec<DeclaredPlugin> {
        self.by_qname
            .read()
            .expect("declared-plugin lock poisoned")
            .values()
            .cloned()
            .collect()
    }
}

fn would_introduce_cycle(
    map: &std::collections::BTreeMap<String, DeclaredPlugin>,
    candidate: &DeclaredPlugin,
) -> bool {
    fn reachable(
        map: &std::collections::BTreeMap<String, DeclaredPlugin>,
        start: &str,
        target: &str,
        visited: &mut std::collections::BTreeSet<String>,
    ) -> bool {
        if start == target {
            return true;
        }
        if !visited.insert(start.to_owned()) {
            return false;
        }
        if let Some(node) = map.get(start) {
            for d in &node.dependencies {
                if reachable(map, d, target, visited) {
                    return true;
                }
            }
        }
        false
    }
    let mut visited = std::collections::BTreeSet::new();
    candidate
        .dependencies
        .iter()
        .any(|d| reachable(map, d, &candidate.qname, &mut visited))
}

/// Reconstruct the dependency cycle that would be introduced by adding
/// `candidate` to `map`.
///
/// Returned vector starts and ends with `candidate.qname`, with the
/// intermediate nodes naming the chain that closes the cycle (e.g.
/// `["a", "b", "c", "a"]`). If no cycle is reachable from any of
/// `candidate`'s dependencies, a single-element vector containing only
/// `candidate.qname` is returned as a defensive fallback.
fn chain_starting_at(
    map: &std::collections::BTreeMap<String, DeclaredPlugin>,
    candidate: &DeclaredPlugin,
) -> Vec<String> {
    fn dfs(
        map: &std::collections::BTreeMap<String, DeclaredPlugin>,
        node: &str,
        target: &str,
        stack: &mut Vec<String>,
        visited: &mut std::collections::BTreeSet<String>,
    ) -> bool {
        stack.push(node.to_owned());
        if node == target {
            return true;
        }
        if !visited.insert(node.to_owned()) {
            stack.pop();
            return false;
        }
        if let Some(declared) = map.get(node) {
            for dep in &declared.dependencies {
                if dfs(map, dep, target, stack, visited) {
                    return true;
                }
            }
        }
        stack.pop();
        false
    }

    let mut visited = std::collections::BTreeSet::new();
    for dep in &candidate.dependencies {
        let mut stack = vec![candidate.qname.clone()];
        if dfs(map, dep, &candidate.qname, &mut stack, &mut visited) {
            return stack;
        }
    }
    vec![candidate.qname.clone()]
}

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

    #[test]
    fn declared_plugin_round_trip_json() {
        let d = DeclaredPlugin {
            qname: "mycorp.fullName".to_owned(),
            kind: "function".to_owned(),
            body: "$first + ' ' + $last".to_owned(),
            signature_json: r#"{"args":["string","string"],"returns":"string"}"#.to_owned(),
            dependencies: vec![],
            declared_by: "alice".to_owned(),
            active: true,
        };
        let s = serde_json::to_string(&d).unwrap();
        let parsed: DeclaredPlugin = serde_json::from_str(&s).unwrap();
        assert_eq!(d, parsed);
    }

    #[test]
    fn custom_plugin_constructs_in_memory() {
        let _ = CustomPlugin::new_in_memory();
    }

    // M11 A.4: synthesizer integration tests.

    /// Mock synthesizer that produces a trivial ProcedurePlugin
    /// suitable for testing the registration path without depending
    /// on `uni-query`.
    #[derive(Debug)]
    struct StubSynthesizer {
        synthesized_count: std::sync::atomic::AtomicUsize,
    }

    impl StubSynthesizer {
        fn new() -> Self {
            Self {
                synthesized_count: std::sync::atomic::AtomicUsize::new(0),
            }
        }

        fn count(&self) -> usize {
            self.synthesized_count
                .load(std::sync::atomic::Ordering::SeqCst)
        }
    }

    impl crate::ProcedureBodySynthesizer for StubSynthesizer {
        fn synthesize(
            &self,
            _decl: &DeclaredPlugin,
        ) -> Result<Arc<dyn uni_plugin::traits::procedure::ProcedurePlugin>, String> {
            self.synthesized_count
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Ok(Arc::new(StubProcedure {
                signature: stub_signature(),
            }))
        }
    }

    #[derive(Debug)]
    struct StubProcedure {
        signature: uni_plugin::traits::procedure::ProcedureSignature,
    }

    fn stub_signature() -> uni_plugin::traits::procedure::ProcedureSignature {
        use arrow_schema::{DataType, Field};
        uni_plugin::traits::procedure::ProcedureSignature {
            args: vec![],
            yields: vec![Field::new("ok", DataType::Boolean, false)],
            mode: uni_plugin::traits::procedure::ProcedureMode::Read,
            side_effects: uni_plugin::SideEffects::ReadOnly,
            retry_contract: None,
            batch_input: None,
            docs: "stub".to_owned(),
        }
    }

    impl uni_plugin::traits::procedure::ProcedurePlugin for StubProcedure {
        fn signature(&self) -> &uni_plugin::traits::procedure::ProcedureSignature {
            &self.signature
        }

        fn invoke(
            &self,
            _ctx: uni_plugin::traits::procedure::ProcedureContext<'_>,
            _args: &[datafusion::logical_expr::ColumnarValue],
        ) -> Result<datafusion::execution::SendableRecordBatchStream, uni_plugin::FnError> {
            unimplemented!(
                "StubProcedure does not execute; the synthesizer test only checks registration"
            )
        }
    }

    #[test]
    fn synthesizer_synthesize_called_on_reactivate() {
        let synth = Arc::new(StubSynthesizer::new());
        let store = Arc::new(DeclaredPluginStore::new());
        // Pre-populate a procedure-kind declaration.
        store
            .declare(DeclaredPlugin {
                qname: "mycorp.findFriends".to_owned(),
                kind: "procedure".to_owned(),
                body: "MATCH (p)-[:KNOWS]->(f) RETURN f".to_owned(),
                signature_json: "{}".to_owned(),
                dependencies: vec![],
                declared_by: "test".to_owned(),
                active: true,
            })
            .unwrap();

        let registry = Arc::new(uni_plugin::PluginRegistry::new());
        // We can't construct CustomPlugin with this pre-populated
        // store directly (its `new` reloads via persistence). Build
        // by hand and then call reactivate_into_registry.
        let plugin = CustomPlugin {
            store: Arc::clone(&store),
            registry: Arc::clone(&registry),
            persistence: Arc::new(NullPersistence),
            procedure_synthesizer: Some(synth.clone()),
            manifest: std::sync::OnceLock::new(),
        };
        plugin
            .reactivate_into_registry()
            .expect("reactivate must call synthesizer for procedure-kind records");
        assert_eq!(
            synth.count(),
            1,
            "synthesizer should have been called for the one procedure declaration"
        );
    }

    #[test]
    fn reactivate_skips_procedure_when_no_synthesizer() {
        let store = Arc::new(DeclaredPluginStore::new());
        store
            .declare(DeclaredPlugin {
                qname: "mycorp.findFriends".to_owned(),
                kind: "procedure".to_owned(),
                body: "MATCH (p)-[:KNOWS]->(f) RETURN f".to_owned(),
                signature_json: "{}".to_owned(),
                dependencies: vec![],
                declared_by: "test".to_owned(),
                active: true,
            })
            .unwrap();

        let registry = Arc::new(uni_plugin::PluginRegistry::new());
        let plugin = CustomPlugin {
            store,
            registry,
            persistence: Arc::new(NullPersistence),
            procedure_synthesizer: None, // no synthesizer
            manifest: std::sync::OnceLock::new(),
        };
        plugin
            .reactivate_into_registry()
            .expect("reactivate must succeed even with procedure records when no synthesizer");
        // No assertion needed — the absence of a panic is the
        // pre-M11 behavior we preserve.
    }

    // M11 A.1: capability-gate tests for `declareProcedure WRITE`.

    fn utf8_scalar(s: &str) -> datafusion::logical_expr::ColumnarValue {
        datafusion::logical_expr::ColumnarValue::Scalar(datafusion::scalar::ScalarValue::Utf8(
            Some(s.to_owned()),
        ))
    }

    fn drive_declare_procedure(
        args: &[datafusion::logical_expr::ColumnarValue],
        principal: Option<&uni_plugin::traits::connector::Principal>,
    ) -> Result<(), uni_plugin::FnError> {
        let store = Arc::new(DeclaredPluginStore::new());
        let decl = procedures::DeclareProcedureProcedure::new(store, Arc::new(NullPersistence));
        let mut ctx = uni_plugin::traits::procedure::ProcedureContext::new();
        if let Some(p) = principal {
            ctx = ctx.with_principal(p);
        }
        use uni_plugin::traits::procedure::ProcedurePlugin;
        decl.invoke(ctx, args).map(|_| ())
    }

    #[test]
    fn declare_procedure_write_rejected_without_procedure_writes() {
        let args = vec![
            utf8_scalar("mycorp.deleteAll"),
            utf8_scalar("MATCH (n) DETACH DELETE n"),
            utf8_scalar("WRITE"),
            utf8_scalar("[]"),
            utf8_scalar("[]"),
        ];
        let p = uni_plugin::traits::connector::Principal {
            id: "alice".to_owned(),
            groups: vec![],
            capabilities: uni_plugin::CapabilitySet::new(),
        };
        let err = drive_declare_procedure(&args, Some(&p))
            .expect_err("WRITE without ProcedureWrites must fail");
        assert_eq!(err.code, 0xB09, "expected capability-denied code 0xB09");
    }

    #[test]
    fn declare_procedure_write_allowed_with_procedure_writes() {
        let args = vec![
            utf8_scalar("mycorp.deleteAll"),
            utf8_scalar("MATCH (n) DETACH DELETE n"),
            utf8_scalar("WRITE"),
            utf8_scalar("[]"),
            utf8_scalar("[]"),
        ];
        let mut caps = uni_plugin::CapabilitySet::new();
        caps.insert(uni_plugin::Capability::ProcedureWrites);
        let p = uni_plugin::traits::connector::Principal {
            id: "admin".to_owned(),
            groups: vec!["admin".to_owned()],
            capabilities: caps,
        };
        drive_declare_procedure(&args, Some(&p)).expect("WRITE with ProcedureWrites must succeed");
    }

    #[test]
    fn declare_procedure_read_does_not_require_procedure_writes() {
        let args = vec![
            utf8_scalar("mycorp.findFriends"),
            utf8_scalar("MATCH (p)-[:KNOWS]->(f) RETURN f"),
            utf8_scalar("READ"),
            utf8_scalar("[]"),
            utf8_scalar("[]"),
        ];
        let p = uni_plugin::traits::connector::Principal::anonymous();
        drive_declare_procedure(&args, Some(&p))
            .expect("READ mode declaration must not require ProcedureWrites");
    }

    fn make(qname: &str, deps: &[&str]) -> DeclaredPlugin {
        DeclaredPlugin {
            qname: qname.to_owned(),
            kind: "function".to_owned(),
            body: String::new(),
            signature_json: "{}".to_owned(),
            dependencies: deps.iter().map(|s| s.to_string()).collect(),
            declared_by: "test".to_owned(),
            active: true,
        }
    }

    #[test]
    fn store_declare_and_get() {
        let s = DeclaredPluginStore::new();
        s.declare(make("a.foo", &[])).unwrap();
        assert_eq!(s.get("a.foo").unwrap().qname, "a.foo");
    }

    #[test]
    fn store_rejects_missing_dependency() {
        let s = DeclaredPluginStore::new();
        match s.declare(make("a.foo", &["a.bar"])) {
            Err(CustomError::DependencyMissing { dependent, dep }) => {
                assert_eq!(dependent, "a.foo");
                assert_eq!(dep, "a.bar");
            }
            other => panic!("expected DependencyMissing, got {other:?}"),
        }
    }

    #[test]
    fn store_detects_cycle() {
        let s = DeclaredPluginStore::new();
        s.declare(make("a", &[])).unwrap();
        s.declare(make("b", &["a"])).unwrap();
        match s.declare(make("a", &["b"])) {
            Err(CustomError::DependencyCycle(_)) => {}
            other => panic!("expected DependencyCycle, got {other:?}"),
        }
    }

    #[test]
    fn store_protects_against_drop_with_dependents() {
        let s = DeclaredPluginStore::new();
        s.declare(make("a", &[])).unwrap();
        s.declare(make("b", &["a"])).unwrap();
        assert!(s.drop_declared("a").is_err());
        assert!(s.drop_declared("b").unwrap());
        assert!(s.drop_declared("a").unwrap());
    }

    #[test]
    fn store_cascade_removes_dependents() {
        let s = DeclaredPluginStore::new();
        s.declare(make("a", &[])).unwrap();
        s.declare(make("b", &["a"])).unwrap();
        s.declare(make("c", &["b"])).unwrap();
        let removed = s.drop_cascade("a");
        assert_eq!(removed.len(), 3);
        assert!(removed.iter().any(|q| q == "a"));
        assert!(removed.iter().any(|q| q == "b"));
        assert!(removed.iter().any(|q| q == "c"));
        assert!(s.list().is_empty());
    }

    #[test]
    fn store_list_returns_all_declared() {
        let s = DeclaredPluginStore::new();
        s.declare(make("x", &[])).unwrap();
        s.declare(make("y", &[])).unwrap();
        assert_eq!(s.list().len(), 2);
    }
}