reasonkit-mem 0.1.7

High-performance vector database & RAG memory layer - hybrid search, embeddings, RAPTOR trees, BM25 fusion, and semantic retrieval for AI systems
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
//! vDreamTeam AI Agent Memory System
//!
//! This module provides structured memory for AI agent teams with:
//! - **Constitutional Layer**: Shared identity, constraints, and governance
//! - **Role-Specific Memory**: Per-agent decisions, lessons, PxP logs
//! - **Cross-Agent Coordination**: Shared logging for multi-agent workflows
//!
//! # Architecture
//!
//! ```text
//! vDreamTeam Memory System
//! ========================
//!
//!            +-----------------------------------+
//!            |       Layer 0: CONSTITUTIONAL    |
//!            |    (Shared, Immutable Core)      |
//!            |  - Core identity & constraints   |
//!            |  - Hard rules (CONS-001 to 016)  |
//!            |  - Mission, values, boundaries   |
//!            +-----------------------------------+
//!                          |
//!     +--------------------+--------------------+
//!     |                    |                    |
//! +-------+          +-------+            +-------+
//! | vCEO  |          | vCTO  |            | vCMO  |
//! | Layer |          | Layer |            | Layer |
//! +-------+          +-------+            +-------+
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use reasonkit_mem::vdreamteam::{VDreamMemory, RoleId, PxPEntry, Consultation};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Load vDreamTeam memory from .agents directory
//!     let mut memory = VDreamMemory::load(".agents").await?;
//!
//!     // Log a PxP consultation
//!     let entry = PxPEntry::new("vcto", "Architecture decision")
//!         .add_consultation(Consultation {
//!             model: "deepseek-v3.2".to_string(),
//!             cli_command: "ollama run deepseek-v3.2:cloud".to_string(),
//!             prompt_summary: "Validate 2-layer architecture".to_string(),
//!             response_summary: "Design validated".to_string(),
//!             confidence: 0.90,
//!         });
//!
//!     memory.log_pxp(entry).await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! # Features
//!
//! Requires the `vdreamteam` feature flag in Cargo.toml.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;

use crate::{MemError, MemResult};

// ============================================================================
// Sub-modules
// ============================================================================

/// MCP Server for vDreamTeam memory queries
pub mod mcp_server;

// Re-export MCP server types
pub use mcp_server::VDreamMCPServer;

// ============================================================================
// Core Types
// ============================================================================

/// Unique identifier for a role (e.g., "vcto", "vceo", "vcmo")
pub type RoleId = String;

/// Main vDreamTeam memory system
///
/// Provides unified access to constitutional and role-specific memory layers.
#[derive(Debug)]
pub struct VDreamMemory {
    /// Base path for .agents directory
    base_path: PathBuf,
    /// Constitutional layer (shared across all roles)
    constitutional: Arc<RwLock<ConstitutionalMemory>>,
    /// Role-specific memory layers
    roles: HashMap<RoleId, Arc<RwLock<RoleMemory>>>,
    /// Cross-agent coordination log path
    cross_agent_log_path: PathBuf,
}

impl VDreamMemory {
    /// Load vDreamTeam memory from a base directory
    ///
    /// # Arguments
    ///
    /// * `base_path` - Path to the .agents directory
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let memory = VDreamMemory::load(".agents").await?;
    /// ```
    pub async fn load(base_path: impl AsRef<Path>) -> MemResult<Self> {
        let base_path = base_path.as_ref().to_path_buf();
        let constitutional_path = base_path.join("constitutional");
        let roles_path = base_path.join("roles");
        let cross_agent_log_path = base_path.join("logs").join("cross_agent.ndjson");

        // Load constitutional layer
        let constitutional = ConstitutionalMemory::load(&constitutional_path).await?;

        // Discover and load role-specific memory
        let mut roles = HashMap::new();
        if roles_path.exists() {
            let mut entries = tokio::fs::read_dir(&roles_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read roles directory: {}", e)))?;

            while let Some(entry) = entries
                .next_entry()
                .await
                .map_err(|e| MemError::storage(format!("Failed to read role entry: {}", e)))?
            {
                let path = entry.path();
                if path.is_dir() {
                    if let Some(role_id) = path.file_name().and_then(|n| n.to_str()) {
                        let role_memory = RoleMemory::load(role_id, &path).await?;
                        roles.insert(role_id.to_string(), Arc::new(RwLock::new(role_memory)));
                    }
                }
            }
        }

        Ok(Self {
            base_path,
            constitutional: Arc::new(RwLock::new(constitutional)),
            roles,
            cross_agent_log_path,
        })
    }

    /// Create a new empty vDreamTeam memory
    pub fn new(base_path: impl AsRef<Path>) -> Self {
        let base_path = base_path.as_ref().to_path_buf();
        Self {
            cross_agent_log_path: base_path.join("logs").join("cross_agent.ndjson"),
            base_path,
            constitutional: Arc::new(RwLock::new(ConstitutionalMemory::default())),
            roles: HashMap::new(),
        }
    }

    /// Get the constitutional memory (read-only)
    pub async fn constitutional(&self) -> MemResult<ConstitutionalMemory> {
        let guard = self.constitutional.read().await;
        Ok(guard.clone())
    }

    /// Get role-specific memory (read-only)
    pub async fn role(&self, role_id: &str) -> MemResult<Option<RoleMemory>> {
        match self.roles.get(role_id) {
            Some(role) => {
                let guard = role.read().await;
                Ok(Some(guard.clone()))
            }
            None => Ok(None),
        }
    }

    /// Log a PxP consultation entry
    ///
    /// Appends to the role's consults.yaml file.
    pub async fn log_pxp(&mut self, entry: PxPEntry) -> MemResult<()> {
        let role_id = entry.role.clone();

        // Ensure role memory exists
        if !self.roles.contains_key(&role_id) {
            return Err(MemError::storage(format!(
                "Role '{}' not found in memory system",
                role_id
            )));
        }

        // Update role memory
        let role = self.roles.get(&role_id).unwrap();
        {
            let mut guard = role.write().await;
            guard.pxp_log.entries.push(entry.clone());
        }

        // Persist to disk
        self.persist_role_pxp(&role_id).await?;

        // Also append to cross-agent log
        self.append_cross_agent_event(CrossAgentEvent::PxPSession {
            timestamp: entry.timestamp,
            role: role_id,
            models: entry
                .consultations
                .iter()
                .map(|c| c.model.clone())
                .collect(),
            topic: entry.decision_context.clone(),
            result: entry.final_decision.clone().unwrap_or_default(),
        })
        .await?;

        Ok(())
    }

    /// Record a decision made by a role
    pub async fn record_decision(&mut self, decision: Decision) -> MemResult<()> {
        let role_id = decision.role.clone();

        if !self.roles.contains_key(&role_id) {
            return Err(MemError::storage(format!(
                "Role '{}' not found in memory system",
                role_id
            )));
        }

        let role = self.roles.get(&role_id).unwrap();
        {
            let mut guard = role.write().await;
            guard.decisions.entries.push(decision.clone());
        }

        self.persist_role_decisions(&role_id).await?;

        // Log to cross-agent
        self.append_cross_agent_event(CrossAgentEvent::Decision {
            timestamp: decision.timestamp,
            role: role_id,
            decision_id: decision.id.clone(),
            title: decision.title.clone(),
            confidence: decision.confidence,
        })
        .await?;

        Ok(())
    }

    /// Record a lesson learned
    pub async fn record_lesson(&mut self, lesson: Lesson) -> MemResult<()> {
        let role_id = lesson.role.clone();

        if !self.roles.contains_key(&role_id) {
            return Err(MemError::storage(format!(
                "Role '{}' not found in memory system",
                role_id
            )));
        }

        let role = self.roles.get(&role_id).unwrap();
        {
            let mut guard = role.write().await;
            guard.lessons.entries.push(lesson.clone());
        }

        self.persist_role_lessons(&role_id).await?;

        Ok(())
    }

    /// Get all roles in the memory system
    pub fn role_ids(&self) -> Vec<&String> {
        self.roles.keys().collect()
    }

    /// Check a constraint by ID
    pub async fn check_constraint(&self, constraint_id: &str) -> MemResult<Option<Constraint>> {
        let guard = self.constitutional.read().await;
        Ok(guard.constraints.get(constraint_id).cloned())
    }

    /// Get PxP statistics for a role
    pub async fn pxp_stats(&self, role_id: &str) -> MemResult<PxPStats> {
        let role = self
            .roles
            .get(role_id)
            .ok_or_else(|| MemError::storage(format!("Role '{}' not found", role_id)))?;

        let guard = role.read().await;
        let entries = &guard.pxp_log.entries;

        let total_consultations: usize = entries.iter().map(|e| e.consultations.len()).sum();

        let avg_confidence = if !entries.is_empty() {
            entries
                .iter()
                .flat_map(|e| e.consultations.iter().map(|c| c.confidence))
                .sum::<f64>()
                / total_consultations.max(1) as f64
        } else {
            0.0
        };

        let mut model_usage: HashMap<String, usize> = HashMap::new();
        for entry in entries {
            for consultation in &entry.consultations {
                *model_usage.entry(consultation.model.clone()).or_default() += 1;
            }
        }

        Ok(PxPStats {
            total_sessions: entries.len(),
            total_consultations,
            avg_confidence,
            model_usage,
        })
    }

    // ========================================================================
    // Private Methods
    // ========================================================================

    async fn persist_role_pxp(&self, role_id: &str) -> MemResult<()> {
        let role = self
            .roles
            .get(role_id)
            .ok_or_else(|| MemError::storage(format!("Role '{}' not found", role_id)))?;

        let guard = role.read().await;
        let path = self
            .base_path
            .join("roles")
            .join(role_id)
            .join("memory")
            .join("consults.yaml");

        let yaml = serde_yaml::to_string(&guard.pxp_log)
            .map_err(|e| MemError::storage(format!("Failed to serialize PxP log: {}", e)))?;

        tokio::fs::create_dir_all(path.parent().unwrap())
            .await
            .map_err(|e| MemError::storage(format!("Failed to create directory: {}", e)))?;

        tokio::fs::write(&path, yaml)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write PxP log: {}", e)))?;

        Ok(())
    }

    async fn persist_role_decisions(&self, role_id: &str) -> MemResult<()> {
        let role = self
            .roles
            .get(role_id)
            .ok_or_else(|| MemError::storage(format!("Role '{}' not found", role_id)))?;

        let guard = role.read().await;
        let path = self
            .base_path
            .join("roles")
            .join(role_id)
            .join("memory")
            .join("decisions.yaml");

        let yaml = serde_yaml::to_string(&guard.decisions)
            .map_err(|e| MemError::storage(format!("Failed to serialize decisions: {}", e)))?;

        tokio::fs::create_dir_all(path.parent().unwrap())
            .await
            .map_err(|e| MemError::storage(format!("Failed to create directory: {}", e)))?;

        tokio::fs::write(&path, yaml)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write decisions: {}", e)))?;

        Ok(())
    }

    async fn persist_role_lessons(&self, role_id: &str) -> MemResult<()> {
        let role = self
            .roles
            .get(role_id)
            .ok_or_else(|| MemError::storage(format!("Role '{}' not found", role_id)))?;

        let guard = role.read().await;
        let path = self
            .base_path
            .join("roles")
            .join(role_id)
            .join("memory")
            .join("lessons.yaml");

        let yaml = serde_yaml::to_string(&guard.lessons)
            .map_err(|e| MemError::storage(format!("Failed to serialize lessons: {}", e)))?;

        tokio::fs::create_dir_all(path.parent().unwrap())
            .await
            .map_err(|e| MemError::storage(format!("Failed to create directory: {}", e)))?;

        tokio::fs::write(&path, yaml)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write lessons: {}", e)))?;

        Ok(())
    }

    async fn append_cross_agent_event(&self, event: CrossAgentEvent) -> MemResult<()> {
        let json = serde_json::to_string(&event)
            .map_err(|e| MemError::storage(format!("Failed to serialize event: {}", e)))?;

        tokio::fs::create_dir_all(self.cross_agent_log_path.parent().unwrap())
            .await
            .map_err(|e| MemError::storage(format!("Failed to create logs directory: {}", e)))?;

        use tokio::io::AsyncWriteExt;
        let mut file = tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.cross_agent_log_path)
            .await
            .map_err(|e| MemError::storage(format!("Failed to open cross-agent log: {}", e)))?;

        file.write_all(format!("{}\n", json).as_bytes())
            .await
            .map_err(|e| MemError::storage(format!("Failed to write event: {}", e)))?;

        Ok(())
    }
}

// ============================================================================
// Constitutional Memory (Layer 0)
// ============================================================================

/// Constitutional memory - shared immutable core for all agents
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConstitutionalMemory {
    /// Core identity (mission, tagline, philosophy)
    pub identity: Identity,
    /// Hard constraints (CONS-001 to CONS-016)
    pub constraints: HashMap<String, Constraint>,
    /// Quality gates
    pub quality_gates: Vec<QualityGate>,
    /// Boundaries (OSS vs Proprietary)
    pub boundaries: Boundaries,
    /// PxP consultation requirements
    pub consultation: ConsultationConfig,
}

impl ConstitutionalMemory {
    /// Load constitutional memory from a directory
    pub async fn load(path: impl AsRef<Path>) -> MemResult<Self> {
        let path = path.as_ref();

        let mut memory = Self::default();

        // Load identity.yaml
        let identity_path = path.join("identity.yaml");
        if identity_path.exists() {
            let content = tokio::fs::read_to_string(&identity_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read identity.yaml: {}", e)))?;
            memory.identity = serde_yaml::from_str(&content)
                .map_err(|e| MemError::storage(format!("Failed to parse identity.yaml: {}", e)))?;
        }

        // Load constraints.yaml
        let constraints_path = path.join("constraints.yaml");
        if constraints_path.exists() {
            let content = tokio::fs::read_to_string(&constraints_path)
                .await
                .map_err(|e| {
                    MemError::storage(format!("Failed to read constraints.yaml: {}", e))
                })?;
            let wrapper: ConstraintsWrapper = serde_yaml::from_str(&content).map_err(|e| {
                MemError::storage(format!("Failed to parse constraints.yaml: {}", e))
            })?;
            // Populate constraint IDs from HashMap keys
            memory.constraints = wrapper
                .constraints
                .into_iter()
                .map(|(key, mut constraint)| {
                    if constraint.id.is_empty() {
                        constraint.id = key.clone();
                    }
                    (key, constraint)
                })
                .collect();
        }

        // Load quality_gates.yaml
        let gates_path = path.join("quality_gates.yaml");
        if gates_path.exists() {
            let content = tokio::fs::read_to_string(&gates_path).await.map_err(|e| {
                MemError::storage(format!("Failed to read quality_gates.yaml: {}", e))
            })?;
            let wrapper: QualityGatesWrapper = serde_yaml::from_str(&content).map_err(|e| {
                MemError::storage(format!("Failed to parse quality_gates.yaml: {}", e))
            })?;
            memory.quality_gates = wrapper.gates;
        }

        // Load boundaries.yaml
        let boundaries_path = path.join("boundaries.yaml");
        if boundaries_path.exists() {
            let content = tokio::fs::read_to_string(&boundaries_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read boundaries.yaml: {}", e)))?;
            memory.boundaries = serde_yaml::from_str(&content).map_err(|e| {
                MemError::storage(format!("Failed to parse boundaries.yaml: {}", e))
            })?;
        }

        // Load consultation.yaml
        let consultation_path = path.join("consultation.yaml");
        if consultation_path.exists() {
            let content = tokio::fs::read_to_string(&consultation_path)
                .await
                .map_err(|e| {
                    MemError::storage(format!("Failed to read consultation.yaml: {}", e))
                })?;
            memory.consultation = serde_yaml::from_str(&content).map_err(|e| {
                MemError::storage(format!("Failed to parse consultation.yaml: {}", e))
            })?;
        }

        Ok(memory)
    }
}

/// Core identity of the vDreamTeam
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Identity {
    pub version: String,
    pub last_updated: String,
    pub mission: String,
    pub tagline: String,
    pub philosophy: String,
    pub principles: Vec<Principle>,
    pub organization: OrganizationInfo,
}

/// A guiding principle
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Principle {
    pub code: String,
    pub description: String,
    pub enforcement: String,
}

/// Organization information
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OrganizationInfo {
    pub name: String,
    pub website: String,
    pub target_arr: String,
}

/// A hard constraint
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Constraint {
    /// Constraint ID (populated from HashMap key if not in YAML)
    #[serde(default)]
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub description: String,
    pub enforcement: String,
    #[serde(default)]
    pub consequence: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct ConstraintsWrapper {
    constraints: HashMap<String, Constraint>,
}

/// A quality gate
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QualityGate {
    #[serde(default)]
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub command: String,
    #[serde(default)]
    pub threshold: String,
    #[serde(default)]
    pub required: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct QualityGatesWrapper {
    gates: Vec<QualityGate>,
}

/// OSS vs Proprietary boundaries
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Boundaries {
    #[serde(default)]
    pub oss_projects: Vec<String>,
    #[serde(default)]
    pub proprietary_projects: Vec<String>,
    #[serde(default)]
    pub never_oss: Vec<String>,
    /// Additional fields from YAML (classifications, rules, etc.)
    #[serde(flatten)]
    pub extra: HashMap<String, serde_yaml::Value>,
}

/// PxP consultation configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConsultationConfig {
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub philosophy: ConsultationPhilosophy,
    #[serde(default)]
    pub cli_tools: HashMap<String, CliTool>,
    #[serde(default)]
    pub tiers: HashMap<String, ConsultationTier>,
    /// Additional fields from YAML
    #[serde(flatten)]
    pub extra: HashMap<String, serde_yaml::Value>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConsultationPhilosophy {
    #[serde(default)]
    pub axiom: String,
    #[serde(default)]
    pub requirement: String,
    #[serde(default)]
    pub minimum_per_session: u32,
    #[serde(default)]
    pub maximum_per_session: u32,
    #[serde(default)]
    pub quality_over_speed: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CliTool {
    #[serde(default)]
    pub command: String,
    #[serde(default)]
    pub model: Option<String>,
    #[serde(default)]
    pub tier: u8,
    #[serde(default)]
    pub specialty: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConsultationTier {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub consultations: String,
    #[serde(default)]
    pub time: String,
    #[serde(default)]
    pub use_when: Vec<String>,
    #[serde(default)]
    pub models: Vec<String>,
}

// ============================================================================
// Role-Specific Memory (Layer 2)
// ============================================================================

/// Role-specific memory layer
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RoleMemory {
    /// Role identifier
    pub role_id: RoleId,
    /// Role identity and configuration
    pub identity: RoleIdentity,
    /// Decision log
    pub decisions: DecisionLog,
    /// Lessons learned
    pub lessons: LessonsLog,
    /// PxP consultation log
    pub pxp_log: PxPLog,
    /// Skills registry
    pub skills: SkillsRegistry,
}

impl RoleMemory {
    /// Load role memory from a directory
    pub async fn load(role_id: &str, path: impl AsRef<Path>) -> MemResult<Self> {
        let path = path.as_ref();
        let memory_path = path.join("memory");

        let mut role = Self {
            role_id: role_id.to_string(),
            ..Default::default()
        };

        // Load identity.yaml
        let identity_path = path.join("identity.yaml");
        if identity_path.exists() {
            let content = tokio::fs::read_to_string(&identity_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read role identity: {}", e)))?;
            role.identity = serde_yaml::from_str(&content)
                .map_err(|e| MemError::storage(format!("Failed to parse role identity: {}", e)))?;
        }

        // Load decisions.yaml
        let decisions_path = memory_path.join("decisions.yaml");
        if decisions_path.exists() {
            let content = tokio::fs::read_to_string(&decisions_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read decisions: {}", e)))?;
            role.decisions = serde_yaml::from_str(&content)
                .map_err(|e| MemError::storage(format!("Failed to parse decisions: {}", e)))?;
        }

        // Load lessons.yaml
        let lessons_path = memory_path.join("lessons.yaml");
        if lessons_path.exists() {
            let content = tokio::fs::read_to_string(&lessons_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read lessons: {}", e)))?;
            role.lessons = serde_yaml::from_str(&content)
                .map_err(|e| MemError::storage(format!("Failed to parse lessons: {}", e)))?;
        }

        // Load consults.yaml (PxP log)
        let pxp_path = memory_path.join("consults.yaml");
        if pxp_path.exists() {
            let content = tokio::fs::read_to_string(&pxp_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read PxP log: {}", e)))?;
            role.pxp_log = serde_yaml::from_str(&content)
                .map_err(|e| MemError::storage(format!("Failed to parse PxP log: {}", e)))?;
        }

        // Load skills.yaml
        let skills_path = memory_path.join("skills.yaml");
        if skills_path.exists() {
            let content = tokio::fs::read_to_string(&skills_path)
                .await
                .map_err(|e| MemError::storage(format!("Failed to read skills: {}", e)))?;
            role.skills = serde_yaml::from_str(&content)
                .map_err(|e| MemError::storage(format!("Failed to parse skills: {}", e)))?;
        }

        Ok(role)
    }
}

/// Role identity and configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RoleIdentity {
    pub version: String,
    pub role_id: String,
    pub full_name: String,
    pub tier: u8,
    pub category: String,
    pub model: ModelConfig,
    pub cli_tools: CliToolsConfig,
    pub responsibilities: RoleResponsibilities,
    pub pxp_requirements: HashMap<String, u32>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ModelConfig {
    pub primary: String,
    pub fallback: Vec<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CliToolsConfig {
    pub primary: String,
    pub alternatives: Vec<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RoleResponsibilities {
    pub core: Vec<String>,
    pub governance: Vec<String>,
    pub coordination: Vec<String>,
}

// ============================================================================
// Decision Tracking
// ============================================================================

/// Log of decisions made by a role
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DecisionLog {
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub role_id: String,
    #[serde(default)]
    pub entries: Vec<Decision>,
}

/// A decision record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Decision {
    pub id: String,
    pub timestamp: DateTime<Utc>,
    pub role: RoleId,
    pub title: String,
    pub context: String,
    pub decision: String,
    pub rationale: String,
    pub alternatives_considered: Vec<String>,
    pub pxp_consultations: Vec<String>,
    pub confidence: f64,
    pub status: DecisionStatus,
}

impl Decision {
    pub fn new(role: impl Into<String>, title: impl Into<String>) -> Self {
        Self {
            id: format!(
                "DEC-{}-{}",
                Utc::now().format("%Y%m%d%H%M%S"),
                uuid::Uuid::new_v4().to_string()[..8].to_uppercase()
            ),
            timestamp: Utc::now(),
            role: role.into(),
            title: title.into(),
            context: String::new(),
            decision: String::new(),
            rationale: String::new(),
            alternatives_considered: Vec::new(),
            pxp_consultations: Vec::new(),
            confidence: 0.0,
            status: DecisionStatus::Pending,
        }
    }

    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.context = context.into();
        self
    }

    pub fn with_decision(mut self, decision: impl Into<String>) -> Self {
        self.decision = decision.into();
        self
    }

    pub fn with_rationale(mut self, rationale: impl Into<String>) -> Self {
        self.rationale = rationale.into();
        self
    }

    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = confidence;
        self
    }

    pub fn finalize(mut self) -> Self {
        self.status = DecisionStatus::Finalized;
        self
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DecisionStatus {
    Pending,
    Finalized,
    Superseded,
    Reverted,
}

// ============================================================================
// Lessons Learned
// ============================================================================

/// Log of lessons learned by a role
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LessonsLog {
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub role_id: String,
    #[serde(default)]
    pub entries: Vec<Lesson>,
}

/// A lesson learned record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lesson {
    pub id: String,
    pub timestamp: DateTime<Utc>,
    pub role: RoleId,
    pub title: String,
    pub category: LessonCategory,
    pub severity: LessonSeverity,
    pub context: String,
    pub mistake: String,
    pub lesson: String,
    pub corrective_actions: Vec<String>,
    pub prevention: String,
}

impl Lesson {
    pub fn new(role: impl Into<String>, title: impl Into<String>) -> Self {
        Self {
            id: format!(
                "LES-{}-{}",
                Utc::now().format("%Y%m%d%H%M%S"),
                uuid::Uuid::new_v4().to_string()[..8].to_uppercase()
            ),
            timestamp: Utc::now(),
            role: role.into(),
            title: title.into(),
            category: LessonCategory::Process,
            severity: LessonSeverity::Medium,
            context: String::new(),
            mistake: String::new(),
            lesson: String::new(),
            corrective_actions: Vec::new(),
            prevention: String::new(),
        }
    }

    pub fn with_category(mut self, category: LessonCategory) -> Self {
        self.category = category;
        self
    }

    pub fn with_severity(mut self, severity: LessonSeverity) -> Self {
        self.severity = severity;
        self
    }

    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.context = context.into();
        self
    }

    pub fn with_mistake(mut self, mistake: impl Into<String>) -> Self {
        self.mistake = mistake.into();
        self
    }

    pub fn with_lesson(mut self, lesson: impl Into<String>) -> Self {
        self.lesson = lesson.into();
        self
    }

    pub fn with_prevention(mut self, prevention: impl Into<String>) -> Self {
        self.prevention = prevention.into();
        self
    }

    pub fn add_corrective_action(mut self, action: impl Into<String>) -> Self {
        self.corrective_actions.push(action.into());
        self
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum LessonCategory {
    Governance,
    Architecture,
    Process,
    Tooling,
    Communication,
    Security,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum LessonSeverity {
    Low,
    Medium,
    High,
    Critical,
}

// ============================================================================
// PxP Consultation Logging
// ============================================================================

/// Log of PxP (Prompt-in-Prompt) consultations
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PxPLog {
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub entries: Vec<PxPEntry>,
}

/// A PxP consultation session entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PxPEntry {
    #[serde(default)]
    pub id: String,
    #[serde(default = "Utc::now")]
    pub timestamp: DateTime<Utc>,
    #[serde(default)]
    pub session_id: Option<String>,
    /// Role ID (populated from parent log if not in YAML)
    #[serde(default)]
    pub role: RoleId,
    #[serde(default)]
    pub decision_context: String,
    #[serde(default)]
    pub consultations: Vec<Consultation>,
    #[serde(default)]
    pub triangulation_result: Option<String>,
    #[serde(default)]
    pub final_decision: Option<String>,
}

impl PxPEntry {
    pub fn new(role: impl Into<String>, decision_context: impl Into<String>) -> Self {
        Self {
            id: format!("PXP-{}", Utc::now().format("%Y%m%d%H%M%S%f")),
            timestamp: Utc::now(),
            session_id: None,
            role: role.into(),
            decision_context: decision_context.into(),
            consultations: Vec::new(),
            triangulation_result: None,
            final_decision: None,
        }
    }

    pub fn with_session(mut self, session_id: impl Into<String>) -> Self {
        self.session_id = Some(session_id.into());
        self
    }

    pub fn add_consultation(mut self, consultation: Consultation) -> Self {
        self.consultations.push(consultation);
        self
    }

    pub fn with_triangulation(mut self, result: impl Into<String>) -> Self {
        self.triangulation_result = Some(result.into());
        self
    }

    pub fn with_final_decision(mut self, decision: impl Into<String>) -> Self {
        self.final_decision = Some(decision.into());
        self
    }
}

/// A single consultation with an AI model
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Consultation {
    #[serde(default)]
    pub model: String,
    #[serde(default)]
    pub cli_command: String,
    #[serde(default)]
    pub prompt_summary: String,
    #[serde(default)]
    pub response_summary: String,
    #[serde(default)]
    pub confidence: f64,
}

// ============================================================================
// Skills Registry
// ============================================================================

/// Registry of skills available to a role
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SkillsRegistry {
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub role_id: String,
    #[serde(default)]
    pub skills: Vec<Skill>,
    /// Additional fields from YAML (cli_tools, etc.)
    #[serde(flatten)]
    pub extra: HashMap<String, serde_yaml::Value>,
}

/// A skill available to a role
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Skill {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub skill_type: SkillType,
    #[serde(default)]
    pub invocation: String,
    #[serde(default)]
    pub proficiency: u8,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SkillType {
    #[default]
    Cli,
    Mcp,
    Internal,
    External,
}

// ============================================================================
// Cross-Agent Coordination
// ============================================================================

/// Events in the cross-agent coordination log
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CrossAgentEvent {
    /// System initialization
    SystemInit {
        timestamp: DateTime<Utc>,
        message: String,
        version: String,
    },
    /// PxP consultation session
    PxPSession {
        timestamp: DateTime<Utc>,
        role: RoleId,
        models: Vec<String>,
        topic: String,
        result: String,
    },
    /// Decision made
    Decision {
        timestamp: DateTime<Utc>,
        role: RoleId,
        decision_id: String,
        title: String,
        confidence: f64,
    },
    /// Role handoff
    Handoff {
        timestamp: DateTime<Utc>,
        from_role: RoleId,
        to_role: RoleId,
        context: String,
    },
    /// Escalation
    Escalation {
        timestamp: DateTime<Utc>,
        from_role: RoleId,
        to_role: RoleId,
        reason: String,
    },
}

// ============================================================================
// Statistics
// ============================================================================

/// PxP consultation statistics for a role
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PxPStats {
    pub total_sessions: usize,
    pub total_consultations: usize,
    pub avg_confidence: f64,
    pub model_usage: HashMap<String, usize>,
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_pxp_entry_creation() {
        let entry = PxPEntry::new("vcto", "Test decision")
            .with_session("test-session-123")
            .add_consultation(Consultation {
                model: "deepseek-v3.2".to_string(),
                cli_command: "ollama run deepseek-v3.2:cloud".to_string(),
                prompt_summary: "Test prompt".to_string(),
                response_summary: "Test response".to_string(),
                confidence: 0.85,
            })
            .with_triangulation("3 models agree")
            .with_final_decision("Proceed with plan A");

        assert_eq!(entry.role, "vcto");
        assert_eq!(entry.consultations.len(), 1);
        assert!(entry.final_decision.is_some());
    }

    #[test]
    fn test_decision_creation() {
        let decision = Decision::new("vcto", "Architecture change")
            .with_context("Need to optimize storage layer")
            .with_decision("Use dual-layer storage")
            .with_rationale("Better performance for hot data")
            .with_confidence(0.92)
            .finalize();

        assert_eq!(decision.role, "vcto");
        assert_eq!(decision.status, DecisionStatus::Finalized);
        assert!(decision.id.starts_with("DEC-"));
    }

    #[test]
    fn test_lesson_creation() {
        let lesson = Lesson::new("vcto", "Quality over Speed")
            .with_category(LessonCategory::Process)
            .with_severity(LessonSeverity::High)
            .with_context("User feedback")
            .with_mistake("Rushing to conclusions")
            .with_lesson("Take more time, use ThinkTools")
            .with_prevention("Apply ThinkTools before concluding")
            .add_corrective_action("Updated consultation.yaml");

        assert_eq!(lesson.role, "vcto");
        assert_eq!(lesson.category, LessonCategory::Process);
        assert_eq!(lesson.severity, LessonSeverity::High);
        assert_eq!(lesson.corrective_actions.len(), 1);
    }

    #[test]
    fn test_cross_agent_event_serialization() {
        let event = CrossAgentEvent::Decision {
            timestamp: Utc::now(),
            role: "vcto".to_string(),
            decision_id: "DEC-001".to_string(),
            title: "Test decision".to_string(),
            confidence: 0.9,
        };

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("DECISION"));
        assert!(json.contains("vcto"));
    }

    #[test]
    fn test_vdream_memory_new() {
        let memory = VDreamMemory::new(".agents");
        assert!(memory.role_ids().is_empty());
    }
}

// ============================================================================
// Integration Tests
// ============================================================================

#[cfg(test)]
mod integration_tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Helper: Create a minimal valid .agents directory structure for testing
    async fn setup_test_agents_dir(base: &Path) -> MemResult<()> {
        // Constitutional layer
        let constitutional = base.join("constitutional");
        tokio::fs::create_dir_all(&constitutional)
            .await
            .map_err(|e| {
                MemError::storage(format!("Failed to create constitutional dir: {}", e))
            })?;

        // identity.yaml
        let identity = r#"
version: "1.0.0"
last_updated: "2026-01-03"
mission: "Test mission"
tagline: "Test tagline"
philosophy: "Test philosophy"
principles: []
organization:
  name: "Test Org"
  website: "https://test.org"
  target_arr: "$100K"
"#;
        tokio::fs::write(constitutional.join("identity.yaml"), identity)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write identity.yaml: {}", e)))?;

        // constraints.yaml
        let constraints = r#"
constraints:
  CONS-001:
    id: "CONS-001"
    name: "Test Constraint"
    description: "A test constraint"
    enforcement: "HARD"
    consequence: "Reject"
"#;
        tokio::fs::write(constitutional.join("constraints.yaml"), constraints)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write constraints.yaml: {}", e)))?;

        // quality_gates.yaml
        let gates = r#"
gates:
  - id: "GATE-001"
    name: "Build"
    command: "cargo build"
    threshold: "Exit 0"
    required: true
"#;
        tokio::fs::write(constitutional.join("quality_gates.yaml"), gates)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write quality_gates.yaml: {}", e)))?;

        // boundaries.yaml
        let boundaries = r#"
oss_projects:
  - reasonkit-core
proprietary_projects:
  - reasonkit-pro
never_oss:
  - rk-research
"#;
        tokio::fs::write(constitutional.join("boundaries.yaml"), boundaries)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write boundaries.yaml: {}", e)))?;

        // consultation.yaml
        let consultation = r#"
version: "4.1.0"
philosophy:
  axiom: "Multiple AI perspectives improve outcomes"
  requirement: "Always consult other models"
  minimum_per_session: 2
  maximum_per_session: 15
  quality_over_speed: true
cli_tools: {}
tiers: {}
"#;
        tokio::fs::write(constitutional.join("consultation.yaml"), consultation)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write consultation.yaml: {}", e)))?;

        // Role: vcto
        let vcto_path = base.join("roles").join("vcto");
        let vcto_memory = vcto_path.join("memory");
        tokio::fs::create_dir_all(&vcto_memory)
            .await
            .map_err(|e| MemError::storage(format!("Failed to create vcto/memory dir: {}", e)))?;

        // vcto/identity.yaml
        let vcto_identity = r#"
version: "1.0.0"
role_id: "vcto"
full_name: "Virtual Chief Technology Officer"
tier: 1
category: "executive"
model:
  primary: "claude-opus-4-5"
  fallback: []
cli_tools:
  primary: "claude -p"
  alternatives: []
responsibilities:
  core:
    - "Architecture decisions"
  governance:
    - "Technical standards"
  coordination:
    - "Team coordination"
pxp_requirements: {}
"#;
        tokio::fs::write(vcto_path.join("identity.yaml"), vcto_identity)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write vcto/identity.yaml: {}", e)))?;

        // vcto/memory/decisions.yaml
        let decisions = r#"
version: "1.0.0"
role_id: "vcto"
entries: []
"#;
        tokio::fs::write(vcto_memory.join("decisions.yaml"), decisions)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write decisions.yaml: {}", e)))?;

        // vcto/memory/lessons.yaml
        let lessons = r#"
version: "1.0.0"
role_id: "vcto"
entries: []
"#;
        tokio::fs::write(vcto_memory.join("lessons.yaml"), lessons)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write lessons.yaml: {}", e)))?;

        // vcto/memory/consults.yaml
        let consults = r#"
version: "1.0.0"
entries: []
"#;
        tokio::fs::write(vcto_memory.join("consults.yaml"), consults)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write consults.yaml: {}", e)))?;

        // vcto/memory/skills.yaml
        let skills = r#"
version: "1.0.0"
role_id: "vcto"
skills: []
"#;
        tokio::fs::write(vcto_memory.join("skills.yaml"), skills)
            .await
            .map_err(|e| MemError::storage(format!("Failed to write skills.yaml: {}", e)))?;

        // Logs directory
        tokio::fs::create_dir_all(base.join("logs"))
            .await
            .map_err(|e| MemError::storage(format!("Failed to create logs dir: {}", e)))?;

        Ok(())
    }

    // ========================================================================
    // Integration Test: Load Valid Directory
    // ========================================================================

    #[tokio::test]
    async fn test_load_valid_directory() {
        // Setup temp directory with valid structure
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Verify constitutional layer loaded
        let constitutional = memory
            .constitutional()
            .await
            .expect("Failed to get constitutional");
        assert_eq!(constitutional.identity.mission, "Test mission");
        assert!(constitutional.constraints.contains_key("CONS-001"));
        assert_eq!(constitutional.quality_gates.len(), 1);
        assert_eq!(constitutional.boundaries.oss_projects.len(), 1);
        assert_eq!(constitutional.consultation.version, "4.1.0");

        // Verify role loaded
        assert_eq!(memory.role_ids().len(), 1);
        assert!(memory.role_ids().contains(&&"vcto".to_string()));

        let role = memory.role("vcto").await.expect("Failed to get role");
        assert!(role.is_some());
        let role = role.unwrap();
        assert_eq!(role.role_id, "vcto");
        assert_eq!(role.identity.full_name, "Virtual Chief Technology Officer");
    }

    // ========================================================================
    // Integration Test: Log PxP Persistence
    // ========================================================================

    #[tokio::test]
    async fn test_log_pxp_persistence() {
        // Setup temp directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let mut memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Create and log a PxP entry
        let entry = PxPEntry::new("vcto", "Test architecture decision")
            .with_session("test-session-001")
            .add_consultation(Consultation {
                model: "deepseek-v3.2".to_string(),
                cli_command: "ollama run deepseek-v3.2:cloud".to_string(),
                prompt_summary: "Validate test approach".to_string(),
                response_summary: "Approach validated".to_string(),
                confidence: 0.88,
            })
            .add_consultation(Consultation {
                model: "claude-opus-4.5".to_string(),
                cli_command: "claude -p".to_string(),
                prompt_summary: "Secondary validation".to_string(),
                response_summary: "Confirmed alignment".to_string(),
                confidence: 0.92,
            })
            .with_triangulation("2/2 models agree")
            .with_final_decision("Proceed with implementation");

        memory.log_pxp(entry).await.expect("Failed to log PxP");

        // Verify file was persisted
        let consults_path = base_path
            .join("roles")
            .join("vcto")
            .join("memory")
            .join("consults.yaml");
        assert!(
            consults_path.exists(),
            "consults.yaml should exist after logging"
        );

        let content = fs::read_to_string(&consults_path).expect("Failed to read consults.yaml");
        assert!(
            content.contains("deepseek-v3.2"),
            "Should contain model name"
        );
        assert!(
            content.contains("Test architecture decision"),
            "Should contain decision context"
        );
        assert!(
            content.contains("2/2 models agree"),
            "Should contain triangulation result"
        );

        // Verify cross-agent log
        let cross_agent_path = base_path.join("logs").join("cross_agent.ndjson");
        assert!(cross_agent_path.exists(), "cross_agent.ndjson should exist");

        let cross_content =
            fs::read_to_string(&cross_agent_path).expect("Failed to read cross_agent");
        // The event type is serialized via serde with tag="event" and SCREAMING_SNAKE_CASE
        // Check for known content that must be in the log
        assert!(
            cross_content.contains("vcto") && cross_content.contains("event"),
            "Cross-agent log should contain role and event data. Got: {}",
            cross_content
        );

        // Reload and verify in-memory state
        let reloaded = VDreamMemory::load(base_path)
            .await
            .expect("Failed to reload VDreamMemory");

        let role = reloaded
            .role("vcto")
            .await
            .expect("Failed to get role")
            .unwrap();
        assert_eq!(role.pxp_log.entries.len(), 1);
        assert_eq!(role.pxp_log.entries[0].consultations.len(), 2);
        assert_eq!(
            role.pxp_log.entries[0].final_decision,
            Some("Proceed with implementation".to_string())
        );
    }

    // ========================================================================
    // Integration Test: Record Decision Storage
    // ========================================================================

    #[tokio::test]
    async fn test_record_decision_storage() {
        // Setup temp directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let mut memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Create and record a decision
        let decision = Decision::new("vcto", "Dual-Layer Storage Architecture")
            .with_context("Need optimized storage for hot and cold data")
            .with_decision("Implement 2-layer storage with hot/cold separation")
            .with_rationale("Provides optimal performance for frequently accessed data while maintaining cost efficiency for archival")
            .with_confidence(0.91)
            .finalize();

        memory
            .record_decision(decision)
            .await
            .expect("Failed to record decision");

        // Verify file was persisted
        let decisions_path = base_path
            .join("roles")
            .join("vcto")
            .join("memory")
            .join("decisions.yaml");
        assert!(
            decisions_path.exists(),
            "decisions.yaml should exist after recording"
        );

        let content = fs::read_to_string(&decisions_path).expect("Failed to read decisions.yaml");
        assert!(
            content.contains("Dual-Layer Storage Architecture"),
            "Should contain title"
        );
        assert!(
            content.contains("2-layer storage"),
            "Should contain decision"
        );
        assert!(content.contains("0.91"), "Should contain confidence");
        assert!(content.contains("finalized"), "Should be finalized");

        // Verify cross-agent log
        let cross_agent_path = base_path.join("logs").join("cross_agent.ndjson");
        let cross_content =
            fs::read_to_string(&cross_agent_path).expect("Failed to read cross_agent");
        assert!(
            cross_content.contains("DECISION"),
            "Should contain Decision event"
        );

        // Reload and verify persistence
        let reloaded = VDreamMemory::load(base_path)
            .await
            .expect("Failed to reload VDreamMemory");

        let role = reloaded
            .role("vcto")
            .await
            .expect("Failed to get role")
            .unwrap();
        assert_eq!(role.decisions.entries.len(), 1);
        assert_eq!(
            role.decisions.entries[0].title,
            "Dual-Layer Storage Architecture"
        );
        assert_eq!(role.decisions.entries[0].status, DecisionStatus::Finalized);
    }

    // ========================================================================
    // Integration Test: Record Lesson Storage
    // ========================================================================

    #[tokio::test]
    async fn test_record_lesson_storage() {
        // Setup temp directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let mut memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Create and record a lesson
        let lesson = Lesson::new("vcto", "Quality Over Speed")
            .with_category(LessonCategory::Process)
            .with_severity(LessonSeverity::High)
            .with_context("User feedback indicated rushed conclusions")
            .with_mistake("Skipping ThinkTools application in favor of quick responses")
            .with_lesson("Always apply relevant ThinkTools before finalizing decisions")
            .with_prevention("Enforce minimum PxP consultation count per session")
            .add_corrective_action("Updated consultation.yaml with stricter minimums")
            .add_corrective_action("Added quality gate for ThinkTools usage");

        memory
            .record_lesson(lesson)
            .await
            .expect("Failed to record lesson");

        // Verify file was persisted
        let lessons_path = base_path
            .join("roles")
            .join("vcto")
            .join("memory")
            .join("lessons.yaml");
        assert!(
            lessons_path.exists(),
            "lessons.yaml should exist after recording"
        );

        let content = fs::read_to_string(&lessons_path).expect("Failed to read lessons.yaml");
        assert!(
            content.contains("Quality Over Speed"),
            "Should contain title"
        );
        assert!(
            content.contains("ThinkTools"),
            "Should contain lesson content"
        );
        assert!(content.contains("PROCESS"), "Should contain category");
        assert!(content.contains("HIGH"), "Should contain severity");
        assert!(
            content.contains("consultation.yaml"),
            "Should contain corrective action"
        );

        // Reload and verify persistence
        let reloaded = VDreamMemory::load(base_path)
            .await
            .expect("Failed to reload VDreamMemory");

        let role = reloaded
            .role("vcto")
            .await
            .expect("Failed to get role")
            .unwrap();
        assert_eq!(role.lessons.entries.len(), 1);
        assert_eq!(role.lessons.entries[0].title, "Quality Over Speed");
        assert_eq!(role.lessons.entries[0].category, LessonCategory::Process);
        assert_eq!(role.lessons.entries[0].severity, LessonSeverity::High);
        assert_eq!(role.lessons.entries[0].corrective_actions.len(), 2);
    }

    // ========================================================================
    // Integration Test: Load Nonexistent Directory (Graceful Fallback)
    // ========================================================================

    #[tokio::test]
    async fn test_load_nonexistent_directory() {
        // VDreamMemory::load gracefully handles missing directories by
        // creating default empty structures. This is by design to allow
        // bootstrapping new projects.
        //
        // To truly test failure, we need a path that causes a hard error
        // (e.g., permission denied or invalid path). For now, we verify
        // that loading a nonexistent path returns an empty/default memory.
        let result = VDreamMemory::load("/nonexistent/path/that/should/not/exist").await;

        // The implementation may either:
        // 1. Fail with an error (preferred for strict validation)
        // 2. Return empty memory (graceful fallback)
        match result {
            Ok(memory) => {
                // If it succeeds, it should be empty (no roles loaded)
                assert!(
                    memory.role_ids().is_empty(),
                    "Should have no roles for nonexistent path"
                );
            }
            Err(err) => {
                // If it fails, error should be meaningful
                let err_string = err.to_string();
                assert!(
                    err_string.contains("Failed to read")
                        || err_string.contains("No such file")
                        || err_string.contains("not found")
                        || err_string.contains("directory"),
                    "Error should indicate path/directory issue: {}",
                    err_string
                );
            }
        }
    }

    // ========================================================================
    // Integration Test: Check Constraint
    // ========================================================================

    #[tokio::test]
    async fn test_check_constraint() {
        // Setup temp directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Check existing constraint
        let constraint = memory
            .check_constraint("CONS-001")
            .await
            .expect("Failed to check constraint");
        assert!(constraint.is_some());
        let constraint = constraint.unwrap();
        assert_eq!(constraint.name, "Test Constraint");
        assert_eq!(constraint.enforcement, "HARD");

        // Check non-existing constraint
        let none = memory
            .check_constraint("CONS-999")
            .await
            .expect("Failed to check non-existing constraint");
        assert!(none.is_none());
    }

    // ========================================================================
    // Integration Test: PxP Statistics
    // ========================================================================

    #[tokio::test]
    async fn test_pxp_stats() {
        // Setup temp directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let mut memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Log multiple PxP entries
        for i in 1..=3 {
            let entry = PxPEntry::new("vcto", format!("Decision {}", i))
                .add_consultation(Consultation {
                    model: "deepseek-v3.2".to_string(),
                    cli_command: "ollama run deepseek-v3.2:cloud".to_string(),
                    prompt_summary: "Prompt".to_string(),
                    response_summary: "Response".to_string(),
                    confidence: 0.8 + (i as f64 * 0.05),
                })
                .add_consultation(Consultation {
                    model: "claude-opus-4.5".to_string(),
                    cli_command: "claude -p".to_string(),
                    prompt_summary: "Prompt 2".to_string(),
                    response_summary: "Response 2".to_string(),
                    confidence: 0.85 + (i as f64 * 0.03),
                });

            memory.log_pxp(entry).await.expect("Failed to log PxP");
        }

        // Get stats
        let stats = memory.pxp_stats("vcto").await.expect("Failed to get stats");

        assert_eq!(stats.total_sessions, 3, "Should have 3 sessions");
        assert_eq!(
            stats.total_consultations, 6,
            "Should have 6 total consultations"
        );
        assert!(
            stats.avg_confidence > 0.8,
            "Average confidence should be > 0.8"
        );
        assert_eq!(stats.model_usage.get("deepseek-v3.2"), Some(&3));
        assert_eq!(stats.model_usage.get("claude-opus-4.5"), Some(&3));
    }

    // ========================================================================
    // Integration Test: Role Not Found Error
    // ========================================================================

    #[tokio::test]
    async fn test_role_not_found_error() {
        // Setup temp directory
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Load memory
        let mut memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Try to log PxP for non-existent role
        let entry = PxPEntry::new("nonexistent_role", "Test");
        let result = memory.log_pxp(entry).await;

        assert!(result.is_err(), "Should fail for non-existent role");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("not found"),
            "Error should indicate role not found: {}",
            err
        );
    }

    // ========================================================================
    // Integration Test: Multiple Roles
    // ========================================================================

    #[tokio::test]
    async fn test_multiple_roles() {
        // Setup temp directory with multiple roles
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let base_path = temp_dir.path();

        // Setup base structure
        setup_test_agents_dir(base_path)
            .await
            .expect("Failed to setup test agents dir");

        // Add a second role (vceo)
        let vceo_path = base_path.join("roles").join("vceo");
        let vceo_memory = vceo_path.join("memory");
        tokio::fs::create_dir_all(&vceo_memory)
            .await
            .expect("Failed to create vceo dir");

        let vceo_identity = r#"
version: "1.0.0"
role_id: "vceo"
full_name: "Virtual Chief Executive Officer"
tier: 1
category: "executive"
model:
  primary: "claude-opus-4-5"
  fallback: []
cli_tools:
  primary: "claude -p"
  alternatives: []
responsibilities:
  core:
    - "Strategic decisions"
  governance:
    - "Overall governance"
  coordination:
    - "Executive coordination"
pxp_requirements: {}
"#;
        tokio::fs::write(vceo_path.join("identity.yaml"), vceo_identity)
            .await
            .expect("Failed to write vceo identity");

        for file in &[
            "decisions.yaml",
            "lessons.yaml",
            "consults.yaml",
            "skills.yaml",
        ] {
            let content = match *file {
                "consults.yaml" => "version: \"1.0.0\"\nentries: []\n",
                "skills.yaml" => "version: \"1.0.0\"\nrole_id: \"vceo\"\nskills: []\n",
                _ => "version: \"1.0.0\"\nrole_id: \"vceo\"\nentries: []\n",
            };
            tokio::fs::write(vceo_memory.join(file), content)
                .await
                .expect("Failed to write vceo memory file");
        }

        // Load memory
        let memory = VDreamMemory::load(base_path)
            .await
            .expect("Failed to load VDreamMemory");

        // Verify both roles loaded
        let role_ids = memory.role_ids();
        assert_eq!(role_ids.len(), 2, "Should have 2 roles");
        assert!(role_ids.iter().any(|r| *r == "vcto"));
        assert!(role_ids.iter().any(|r| *r == "vceo"));

        // Verify each role has correct identity
        let vcto = memory
            .role("vcto")
            .await
            .expect("Failed to get vcto")
            .unwrap();
        assert_eq!(vcto.identity.full_name, "Virtual Chief Technology Officer");

        let vceo = memory
            .role("vceo")
            .await
            .expect("Failed to get vceo")
            .unwrap();
        assert_eq!(vceo.identity.full_name, "Virtual Chief Executive Officer");
    }
}