dalo 1.11.1

Your team's agent skills, versioned like code. Review, pin, and sync skills, instructions, and hooks into every agent.
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
//! Portable canonical agent packages and provider compilers.
//!
//! This module implements the read-only foundation of RFC 0004.  It discovers
//! only canonical `agents/<name>/AGENT.md` packages, validates their portable
//! contract, hashes their complete package tree, and deterministically compiles
//! safe Claude and Codex projections.  Materializing generated files is kept
//! separate so an incomplete compiler can never overwrite a provider file.

use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize, Serializer};
use sha2::{Digest, Sha256};

use crate::error::{DaloError, DaloResult};
use crate::inventory::SourceInventory;
use crate::source::{SourceConfig, SourceKind};
use crate::store::ApprovalRecord;

/// Canonical package entry-point filename.
pub const AGENT_FILE: &str = "AGENT.md";

const MAX_AGENT_FILE_BYTES: u64 = 1024 * 1024;
const MAX_FRONTMATTER_BYTES: usize = 64 * 1024;
const MAX_PACKAGE_ENTRIES: usize = 256;
const MAX_PACKAGE_DEPTH: usize = 16;
const MAX_PACKAGE_BYTES: u64 = 16 * 1024 * 1024;

type PackageFiles = Vec<(String, Vec<u8>)>;

/// One scan result for canonical packages below a source.
#[derive(Debug, Clone, Default, Serialize)]
pub struct AgentInventory {
    /// Valid canonical agent packages.
    pub agents: Vec<AgentRecord>,
    /// Non-fatal malformed-package findings.
    pub warnings: Vec<AgentInventoryWarning>,
}

/// A valid canonical agent discovered in a source.
#[derive(Debug, Clone, Serialize)]
pub struct AgentRecord {
    /// Source containing the package.
    pub source_id: String,
    /// Source-qualified reference, `<source-id>:<name>`.
    pub source_ref: String,
    /// Portable slot name.
    pub slot_name: String,
    /// Stable optional identity.
    pub id: Option<String>,
    /// Package directory.
    pub path: PathBuf,
    /// Canonical entry point.
    pub agent_file: PathBuf,
    /// Human-facing summary.
    pub description: String,
    /// Declared owners.
    pub owners: Vec<String>,
    /// Declared tags.
    pub tags: Vec<String>,
    /// Provider allowlist; `None` means every compatible provider, while an
    /// explicit empty vector targets no provider.
    pub targets: Option<Vec<String>>,
    /// Required skill references.
    pub skills: Vec<String>,
    /// Portable canonical document.
    pub agent: CanonicalAgent,
    /// SHA-256 of the complete canonical package tree.
    pub content_hash: String,
    /// Whether support files are present in addition to `AGENT.md`.
    pub has_support_files: bool,
}

/// A non-fatal canonical-package inventory warning.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AgentInventoryWarning {
    /// Stable machine-readable warning code.
    pub code: AgentInventoryWarningCode,
    /// Package or entry path.
    pub path: PathBuf,
    /// Actionable detail.
    pub message: String,
}

/// Canonical-package inventory warning codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentInventoryWarningCode {
    /// The package has an invalid document or violates a safety bound.
    InvalidPackage,
    /// The package path is a symlink or contains an unsupported special file.
    UnsafePackageEntry,
    /// Two packages in one source use the same canonical name.
    DuplicateSlotName,
    /// A package directory could not be read.
    UnreadablePath,
}

impl AgentInventoryWarningCode {
    /// Stable snake-case label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::InvalidPackage => "invalid_agent_package",
            Self::UnsafePackageEntry => "unsafe_agent_package_entry",
            Self::DuplicateSlotName => "duplicate_agent_slot_name",
            Self::UnreadablePath => "unreadable_agent_path",
        }
    }
}

impl std::fmt::Display for AgentInventoryWarningCode {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// A canonical, provider-neutral agent document.
#[derive(Debug, Clone, Serialize)]
pub struct CanonicalAgent {
    /// Version of the canonical schema.
    pub schema_version: u32,
    /// Portable agent slot name.
    pub name: String,
    /// Human-facing description.
    pub description: String,
    /// Stable optional identity.
    pub id: Option<String>,
    /// Declared owners.
    pub owners: Vec<String>,
    /// Declared tags.
    pub tags: Vec<String>,
    /// Provider allowlist. Omitted in source means every compatible provider.
    pub targets: Option<Vec<String>>,
    /// Model intent independent from provider model IDs.
    pub model: ModelIntent,
    /// Required skill references.
    pub skills: Vec<String>,
    /// Optional portable hard tool boundary.
    pub tools: Option<ToolBoundary>,
    /// Optional portable hard filesystem boundary.
    pub filesystem: Option<FilesystemBoundary>,
    /// Optional portable hard network boundary.
    pub network: Option<NetworkBoundary>,
    /// Provider-specific overlays retained with the canonical source.
    #[serde(serialize_with = "serialize_redacted_providers")]
    pub providers: BTreeMap<String, serde_json::Value>,
    /// Canonical Markdown system prompt.
    pub prompt: String,
}

/// Model intent used by canonical agents.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ModelIntent {
    /// Provider-neutral preference.
    #[serde(default)]
    pub profile: ModelProfile,
}

/// Provider-neutral model preference.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ModelProfile {
    /// Keep the provider default model and reasoning effort.
    #[default]
    Inherit,
    /// Prefer a faster provider model or lower reasoning effort.
    Fast,
    /// Prefer the provider's balanced option.
    Balanced,
    /// Prefer deeper reasoning.
    Deep,
}

/// An explicit portable tool allowlist.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ToolBoundary {
    /// Allowed portable tool capabilities.  An empty vector denies all tools.
    pub allow: Vec<ToolCapability>,
}

/// Portable tool capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ToolCapability {
    /// Read files.
    ReadFiles,
    /// Write or edit files.
    WriteFiles,
    /// Search files.
    SearchFiles,
    /// Run a shell command.
    RunShell,
    /// Fetch a known web resource.
    FetchWeb,
    /// Search the web.
    SearchWeb,
    /// Use configured MCP tools.
    UseMcp,
    /// Delegate to another agent.
    Delegate,
}

impl ToolCapability {
    fn claude_tools(self) -> &'static [&'static str] {
        match self {
            Self::ReadFiles => &["Read"],
            Self::WriteFiles => &["Edit", "Write"],
            Self::SearchFiles => &["Glob", "Grep"],
            Self::RunShell => &["Bash"],
            Self::FetchWeb => &["WebFetch"],
            Self::SearchWeb => &["WebSearch"],
            Self::UseMcp => &[],
            Self::Delegate => &["Task"],
        }
    }
}

/// An explicit portable filesystem boundary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FilesystemBoundary {
    /// Readable `workspace` scopes.
    #[serde(default)]
    pub read: Vec<String>,
    /// Writable `workspace` scopes.
    #[serde(default)]
    pub write: Vec<String>,
}

/// An explicit portable network boundary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NetworkBoundary {
    /// Allowed exact host names, or the lone wildcard `*`.
    pub allow: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct AgentFrontmatter {
    schema_version: u32,
    name: String,
    description: String,
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    owners: Vec<String>,
    #[serde(default)]
    tags: Vec<String>,
    #[serde(default)]
    targets: Option<Vec<String>>,
    #[serde(default)]
    model: ModelIntent,
    #[serde(default)]
    skills: Vec<String>,
    #[serde(default)]
    tools: Option<ToolBoundary>,
    #[serde(default)]
    filesystem: Option<FilesystemBoundary>,
    #[serde(default)]
    network: Option<NetworkBoundary>,
    #[serde(default)]
    providers: BTreeMap<String, serde_json::Value>,
}

/// Scan only `agents/<name>/AGENT.md` packages in one source.
#[must_use]
pub fn scan_source_agents(source_id: &str, source_root: &Path) -> AgentInventory {
    let agents_root = source_root.join("agents");
    match fs::symlink_metadata(&agents_root) {
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return AgentInventory::default();
        }
        Err(error) => {
            return AgentInventory {
                agents: Vec::new(),
                warnings: vec![warning(
                    AgentInventoryWarningCode::UnreadablePath,
                    agents_root,
                    error.to_string(),
                )],
            };
        }
        Ok(metadata) if metadata.file_type().is_symlink() => {
            return AgentInventory {
                agents: Vec::new(),
                warnings: vec![warning(
                    AgentInventoryWarningCode::UnsafePackageEntry,
                    agents_root,
                    "the source `agents` directory must not be a symlink".to_owned(),
                )],
            };
        }
        Ok(metadata) if !metadata.is_dir() => {
            return AgentInventory {
                agents: Vec::new(),
                warnings: vec![warning(
                    AgentInventoryWarningCode::InvalidPackage,
                    agents_root,
                    "the source `agents` path must be a directory".to_owned(),
                )],
            };
        }
        Ok(_) => {}
    }

    let entries = match fs::read_dir(&agents_root) {
        Ok(entries) => entries,
        Err(error) => {
            return AgentInventory {
                agents: Vec::new(),
                warnings: vec![warning(
                    AgentInventoryWarningCode::UnreadablePath,
                    agents_root,
                    error.to_string(),
                )],
            };
        }
    };

    let mut packages = Vec::new();
    let mut warnings = Vec::new();
    for entry in entries {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error) => {
                warnings.push(warning(
                    AgentInventoryWarningCode::UnreadablePath,
                    agents_root.clone(),
                    error.to_string(),
                ));
                continue;
            }
        };
        let path = entry.path();
        let file_type = match entry.file_type() {
            Ok(file_type) => file_type,
            Err(error) => {
                warnings.push(warning(
                    AgentInventoryWarningCode::UnreadablePath,
                    path,
                    error.to_string(),
                ));
                continue;
            }
        };
        if file_type.is_symlink() {
            warnings.push(warning(
                AgentInventoryWarningCode::UnsafePackageEntry,
                path,
                "agent package directories must not be symlinks".to_owned(),
            ));
            continue;
        }
        if !file_type.is_dir() {
            continue;
        }
        if path.join(AGENT_FILE).exists() {
            packages.push(path);
        }
    }
    packages.sort();

    let mut agents = Vec::new();
    for package_path in packages {
        match scan_package(source_id, &package_path) {
            Ok(agent) => agents.push(agent),
            Err(error) => warnings.push(warning(
                if error.contains("symlink") || error.contains("special file") {
                    AgentInventoryWarningCode::UnsafePackageEntry
                } else {
                    AgentInventoryWarningCode::InvalidPackage
                },
                package_path,
                error,
            )),
        }
    }

    let mut paths_by_slot = BTreeMap::<String, Vec<PathBuf>>::new();
    for agent in &agents {
        paths_by_slot
            .entry(agent.slot_name.clone())
            .or_default()
            .push(agent.path.clone());
    }
    let duplicated = paths_by_slot
        .into_iter()
        .filter(|(_, paths)| paths.len() > 1)
        .collect::<Vec<_>>();
    for (slot_name, paths) in duplicated {
        for path in paths {
            warnings.push(warning(
                AgentInventoryWarningCode::DuplicateSlotName,
                path,
                format!("source `{source_id}` contains multiple agents named `{slot_name}`"),
            ));
        }
        agents.retain(|agent| agent.slot_name != slot_name);
    }
    agents.sort_by(|left, right| left.source_ref.cmp(&right.source_ref));
    warnings.sort_by(|left, right| {
        left.path
            .cmp(&right.path)
            .then_with(|| left.code.as_str().cmp(right.code.as_str()))
    });

    AgentInventory { agents, warnings }
}

fn warning(
    code: AgentInventoryWarningCode,
    path: PathBuf,
    message: String,
) -> AgentInventoryWarning {
    AgentInventoryWarning {
        code,
        path,
        message,
    }
}

fn scan_package(source_id: &str, package_path: &Path) -> Result<AgentRecord, String> {
    let package_name = package_path
        .file_name()
        .and_then(|name| name.to_str())
        .ok_or_else(|| "agent package directory name must be valid UTF-8".to_owned())?;
    let (entries, total_bytes) = collect_package_files(package_path)?;
    let content_hash = hash_package_files(&entries);
    let agent_file = package_path.join(AGENT_FILE);
    let agent_bytes = entries
        .iter()
        .find_map(|(path, bytes)| (path == AGENT_FILE).then_some(bytes.as_slice()))
        .ok_or_else(|| format!("package is missing {AGENT_FILE}"))?;
    if agent_bytes.len() as u64 > MAX_AGENT_FILE_BYTES {
        return Err(format!(
            "{AGENT_FILE} exceeds the {MAX_AGENT_FILE_BYTES}-byte safety limit"
        ));
    }
    let document = std::str::from_utf8(agent_bytes)
        .map_err(|_| format!("{AGENT_FILE} must be valid UTF-8"))?;
    let agent = parse_agent_document(document, package_name)?;

    Ok(AgentRecord {
        source_id: source_id.to_owned(),
        source_ref: format!("{source_id}:{}", agent.name),
        slot_name: agent.name.clone(),
        id: agent.id.clone(),
        path: package_path.to_path_buf(),
        agent_file,
        description: agent.description.clone(),
        owners: agent.owners.clone(),
        tags: agent.tags.clone(),
        targets: agent.targets.clone(),
        skills: agent.skills.clone(),
        agent,
        content_hash,
        has_support_files: entries.len() > 1 || total_bytes > agent_bytes.len() as u64,
    })
}

fn collect_package_files(package_path: &Path) -> Result<(PackageFiles, u64), String> {
    let mut entries = Vec::new();
    let mut entry_count = 0;
    let mut total_bytes = 0_u64;
    collect_package_files_inner(
        package_path,
        package_path,
        0,
        &mut entries,
        &mut entry_count,
        &mut total_bytes,
    )?;
    entries.sort_by(|left, right| left.0.cmp(&right.0));
    Ok((entries, total_bytes))
}

fn collect_package_files_inner(
    package_root: &Path,
    current: &Path,
    depth: usize,
    files: &mut PackageFiles,
    entry_count: &mut usize,
    total_bytes: &mut u64,
) -> Result<(), String> {
    if depth > MAX_PACKAGE_DEPTH {
        return Err(format!(
            "package exceeds the {MAX_PACKAGE_DEPTH}-directory-level safety limit"
        ));
    }
    let entries = fs::read_dir(current).map_err(|error| error.to_string())?;
    let mut entries = entries
        .map(|entry| entry.map_err(|error| error.to_string()))
        .collect::<Result<Vec<_>, _>>()?;
    entries.sort_by_key(|entry| entry.file_name());

    for entry in entries {
        *entry_count += 1;
        if *entry_count > MAX_PACKAGE_ENTRIES {
            return Err(format!(
                "package exceeds the {MAX_PACKAGE_ENTRIES}-entry safety limit"
            ));
        }
        let path = entry.path();
        let file_type = entry.file_type().map_err(|error| error.to_string())?;
        if file_type.is_symlink() {
            return Err(format!("package contains symlink `{}`", path.display()));
        }
        if file_type.is_dir() {
            collect_package_files_inner(
                package_root,
                &path,
                depth + 1,
                files,
                entry_count,
                total_bytes,
            )?;
            continue;
        }
        if !file_type.is_file() {
            return Err(format!(
                "package contains unsupported special file `{}`",
                path.display()
            ));
        }
        let bytes = fs::read(&path).map_err(|error| error.to_string())?;
        *total_bytes = total_bytes
            .checked_add(bytes.len() as u64)
            .ok_or_else(|| "package byte count overflowed".to_owned())?;
        if *total_bytes > MAX_PACKAGE_BYTES {
            return Err(format!(
                "package exceeds the {MAX_PACKAGE_BYTES}-byte safety limit"
            ));
        }
        files.push((relative_package_path(package_root, &path)?, bytes));
    }
    Ok(())
}

fn relative_package_path(package_root: &Path, path: &Path) -> Result<String, String> {
    let relative = path
        .strip_prefix(package_root)
        .map_err(|_| "package entry escaped its package root".to_owned())?;
    let mut parts = Vec::new();
    for part in relative.components() {
        let std::path::Component::Normal(part) = part else {
            return Err("package contains a non-normal path component".to_owned());
        };
        parts.push(
            part.to_str()
                .ok_or_else(|| "package entry path must be valid UTF-8".to_owned())?,
        );
    }
    Ok(parts.join("/"))
}

fn hash_package_files(entries: &PackageFiles) -> String {
    let mut hash = Sha256::new();
    hash.update(b"dalo-agent-package-v1\0");
    for (path, bytes) in entries {
        hash.update((path.len() as u64).to_be_bytes());
        hash.update(path.as_bytes());
        hash.update(*b"f");
        hash.update((bytes.len() as u64).to_be_bytes());
        hash.update(bytes);
    }
    hash.finalize()
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

fn parse_agent_document(document: &str, package_name: &str) -> Result<CanonicalAgent, String> {
    let rest = document
        .strip_prefix("---\n")
        .or_else(|| document.strip_prefix("---\r\n"))
        .ok_or_else(|| format!("{AGENT_FILE} must begin with an exact `---` frontmatter line"))?;
    let (frontmatter, prompt) = split_frontmatter(rest)?;
    if frontmatter.len() > MAX_FRONTMATTER_BYTES {
        return Err(format!(
            "frontmatter exceeds the {MAX_FRONTMATTER_BYTES}-byte safety limit"
        ));
    }
    reject_unsafe_yaml(frontmatter)?;
    let parsed: AgentFrontmatter = yaml_serde::from_str(frontmatter)
        .map_err(|error| format!("invalid YAML frontmatter: {error}"))?;
    if parsed.schema_version != 1 {
        return Err(format!(
            "unsupported agent schema_version {}; expected 1",
            parsed.schema_version
        ));
    }
    if parsed.name != package_name {
        return Err(format!(
            "frontmatter name `{}` must exactly match package directory `{package_name}`",
            parsed.name
        ));
    }
    if !is_valid_agent_name(&parsed.name) {
        return Err(format!(
            "agent name `{}` is not a portable agent slot name",
            parsed.name
        ));
    }
    if parsed.description.trim().is_empty() {
        return Err("agent description must not be empty".to_owned());
    }
    if prompt.trim().is_empty() {
        return Err("agent Markdown prompt must contain non-whitespace text".to_owned());
    }
    validate_frontmatter(&parsed)?;
    Ok(CanonicalAgent {
        schema_version: parsed.schema_version,
        name: parsed.name,
        description: parsed.description,
        id: parsed.id,
        owners: parsed.owners,
        tags: parsed.tags,
        targets: parsed.targets,
        model: parsed.model,
        skills: parsed.skills,
        tools: parsed.tools,
        filesystem: parsed.filesystem,
        network: parsed.network,
        providers: parsed.providers,
        prompt: prompt.to_owned(),
    })
}

fn split_frontmatter(rest: &str) -> Result<(&str, &str), String> {
    let mut offset = 0;
    for line in rest.split_inclusive('\n') {
        let bare = line
            .strip_suffix('\n')
            .unwrap_or(line)
            .strip_suffix('\r')
            .unwrap_or(line.strip_suffix('\n').unwrap_or(line));
        if bare == "---" {
            return Ok((&rest[..offset], &rest[offset + line.len()..]));
        }
        offset += line.len();
    }
    Err("frontmatter start marker has no matching closing `---` marker".to_owned())
}

fn reject_unsafe_yaml(frontmatter: &str) -> Result<(), String> {
    // `yaml_serde` handles syntax and duplicate map keys.  These lexical guards
    // reject features whose expansion/indirection semantics do not belong in a
    // portable declarative package before deserializing into owned data.
    for line in frontmatter.lines() {
        let trimmed = line.trim_start();
        if trimmed.starts_with("<<:") {
            return Err("YAML merge keys are not allowed".to_owned());
        }
        if contains_unquoted_yaml_token(line, '&') {
            return Err("YAML anchors are not allowed".to_owned());
        }
        if contains_unquoted_yaml_token(line, '*') {
            return Err("YAML aliases are not allowed".to_owned());
        }
        if contains_unquoted_yaml_token(line, '!') {
            return Err("YAML custom tags are not allowed".to_owned());
        }
    }
    Ok(())
}

fn contains_unquoted_yaml_token(line: &str, token: char) -> bool {
    let mut single = false;
    let mut double = false;
    let mut escaped = false;
    for (index, character) in line.char_indices() {
        if double {
            if escaped {
                escaped = false;
            } else if character == '\\' {
                escaped = true;
            } else if character == '"' {
                double = false;
            }
            continue;
        }
        if single {
            if character == '\'' {
                single = false;
            }
            continue;
        }
        match character {
            '\'' => single = true,
            '"' => double = true,
            '#' => break,
            _ if character == token && is_yaml_indicator_position(line, index) => {
                return true;
            }
            _ => {}
        }
    }
    false
}

fn is_yaml_indicator_position(line: &str, index: usize) -> bool {
    let prefix = line[..index].trim_end();
    if prefix.is_empty()
        || prefix.ends_with(':')
        || prefix.ends_with('[')
        || prefix.ends_with('{')
        || prefix.ends_with(',')
    {
        return true;
    }
    prefix
        .strip_suffix('-')
        .is_some_and(|before_dash| before_dash.trim().is_empty())
}

fn serialize_redacted_providers<S>(
    providers: &BTreeMap<String, serde_json::Value>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let redacted = providers
        .iter()
        .map(|(provider, value)| (provider.clone(), redact_provider_value(value, None)))
        .collect::<BTreeMap<_, _>>();
    redacted.serialize(serializer)
}

fn redact_provider_value(value: &serde_json::Value, key: Option<&str>) -> serde_json::Value {
    if key.is_some_and(is_secret_like_key) {
        return serde_json::Value::String("[REDACTED]".to_owned());
    }
    match value {
        serde_json::Value::Object(object) => serde_json::Value::Object(
            object
                .iter()
                .map(|(key, value)| {
                    (
                        key.clone(),
                        redact_provider_value(value, Some(key.as_str())),
                    )
                })
                .collect(),
        ),
        serde_json::Value::Array(values) => serde_json::Value::Array(
            values
                .iter()
                .map(|value| redact_provider_value(value, None))
                .collect(),
        ),
        _ => value.clone(),
    }
}

fn is_secret_like_key(key: &str) -> bool {
    let normalized = key
        .chars()
        .filter(char::is_ascii_alphanumeric)
        .map(|character| character.to_ascii_lowercase())
        .collect::<String>();
    normalized.contains("secret")
        || normalized.contains("token")
        || normalized.contains("password")
        || normalized.contains("credential")
        || normalized.contains("apikey")
        || normalized.contains("privatekey")
        || normalized.contains("accesskey")
        || normalized == "auth"
        || normalized.contains("authorization")
}

fn validate_frontmatter(frontmatter: &AgentFrontmatter) -> Result<(), String> {
    if frontmatter
        .id
        .as_deref()
        .is_some_and(|id| id.trim().is_empty())
    {
        return Err("agent id must not be empty when specified".to_owned());
    }
    validate_strings("owners", &frontmatter.owners)?;
    validate_strings("tags", &frontmatter.tags)?;
    validate_strings("skills", &frontmatter.skills)?;
    if let Some(targets) = &frontmatter.targets {
        validate_strings("targets", targets)?;
        for target in targets {
            if !is_valid_agent_name(target) {
                return Err(format!("target `{target}` is not a valid provider ID"));
            }
        }
    }
    if let Some(tools) = &frontmatter.tools {
        let mut unique = BTreeSet::new();
        for tool in &tools.allow {
            if !unique.insert(*tool) {
                return Err("tools.allow must not contain duplicates".to_owned());
            }
        }
    }
    if let Some(filesystem) = &frontmatter.filesystem {
        validate_filesystem_scopes("filesystem.read", &filesystem.read)?;
        validate_filesystem_scopes("filesystem.write", &filesystem.write)?;
    }
    if let Some(network) = &frontmatter.network {
        if network
            .allow
            .iter()
            .filter(|host| host.as_str() == "*")
            .count()
            > 0
            && network.allow.len() != 1
        {
            return Err("network.allow may use `*` only as its lone value".to_owned());
        }
        for host in &network.allow {
            if host != "*" && !is_valid_hostname(host) {
                return Err(format!(
                    "network host `{host}` is not a valid exact host name"
                ));
            }
        }
    }
    for (provider, overlay) in &frontmatter.providers {
        if !is_valid_agent_name(provider) {
            return Err(format!(
                "provider overlay `{provider}` is not a valid provider ID"
            ));
        }
        let object = overlay
            .as_object()
            .ok_or_else(|| format!("providers.{provider} must be a mapping"))?;
        if object.keys().any(|key| {
            matches!(
                key.as_str(),
                "name" | "description" | "prompt" | "developer_instructions" | "skills"
            )
        }) {
            return Err(format!(
                "providers.{provider} may not replace canonical identity, prompt, or skill fields"
            ));
        }
    }
    Ok(())
}

fn validate_strings(field: &str, values: &[String]) -> Result<(), String> {
    if values.iter().any(|value| value.trim().is_empty()) {
        return Err(format!("{field} must not contain empty values"));
    }
    Ok(())
}

fn validate_filesystem_scopes(field: &str, scopes: &[String]) -> Result<(), String> {
    for scope in scopes {
        let valid = scope == "workspace"
            || scope.strip_prefix("workspace/").is_some_and(|suffix| {
                !suffix.is_empty()
                    && suffix.split('/').all(|part| {
                        !part.is_empty() && part != "." && part != ".." && !part.contains('\\')
                    })
            });
        if !valid {
            return Err(format!(
                "{field} scope `{scope}` must be `workspace` or a normalized path below it"
            ));
        }
    }
    Ok(())
}

fn is_valid_hostname(host: &str) -> bool {
    !host.is_empty()
        && host.len() <= 253
        && host.split('.').all(|label| {
            !label.is_empty()
                && label.len() <= 63
                && !label.starts_with('-')
                && !label.ends_with('-')
                && label
                    .chars()
                    .all(|character| character.is_ascii_alphanumeric() || character == '-')
        })
}

fn is_valid_agent_name(value: &str) -> bool {
    !value.is_empty()
        && value.chars().all(|character| {
            character.is_ascii_lowercase() || character.is_ascii_digit() || character == '-'
        })
        && !value.starts_with('-')
        && !value.ends_with('-')
        && !value.contains("--")
}

/// Provider adapters implemented by this RFC's initial slice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentProvider {
    /// Anthropic Claude Code's global subagent directory.
    Claude,
    /// OpenAI Codex's global subagent directory.
    Codex,
}

impl AgentProvider {
    /// Parse a provider ID.
    pub fn parse(value: &str) -> DaloResult<Self> {
        match value {
            "claude" => Ok(Self::Claude),
            "codex" => Ok(Self::Codex),
            _ => Err(DaloError::InvalidArgument {
                reason: format!("unsupported agent provider `{value}`; expected claude or codex"),
            }),
        }
    }

    /// Canonical provider ID.
    #[must_use]
    pub const fn id(self) -> &'static str {
        match self {
            Self::Claude => "claude",
            Self::Codex => "codex",
        }
    }

    /// Generated native filename for an agent slot.
    #[must_use]
    pub fn filename(self, name: &str) -> String {
        match self {
            Self::Claude => format!("{name}.md"),
            Self::Codex => format!("{name}.toml"),
        }
    }
}

/// Field-level portability outcome, ordered by severity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CompatibilityResult {
    /// Native output retains the field directly.
    Exact,
    /// Native output enforces the same value through a deterministic mapping.
    Mapped,
    /// The provider receives instructions but cannot enforce the field.
    GuidanceOnly,
    /// The provider does not consume this non-safety field.
    Unsupported,
    /// The requested projection would be unsafe or invalid.
    Blocked,
}

impl CompatibilityResult {
    /// Stable snake-case label used in text and JSON reports.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Exact => "exact",
            Self::Mapped => "mapped",
            Self::GuidanceOnly => "guidance_only",
            Self::Unsupported => "unsupported",
            Self::Blocked => "blocked",
        }
    }
}

impl std::fmt::Display for CompatibilityResult {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// One field-level provider compatibility finding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CompatibilityFinding {
    /// Canonical field path.
    pub field: String,
    /// Translation fidelity.
    pub result: CompatibilityResult,
    /// Explanation and remediation where relevant.
    pub message: String,
    /// Native value or field mapping when one exists.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub native_mapping: Option<String>,
}

impl CompatibilityFinding {
    /// Whether this finding is Dalo-only identity metadata omitted by native adapters.
    #[must_use]
    pub fn is_dalo_identity_metadata(&self) -> bool {
        matches!(self.field.as_str(), "id" | "owners" | "tags")
    }
}

/// Result of compiling one canonical agent for one provider.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AgentCompilation {
    /// Destination provider.
    pub provider: AgentProvider,
    /// Whether this provider is excluded by the canonical `targets` allowlist.
    pub not_targeted: bool,
    /// Complete generated native preview when safe to materialize.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bytes: Option<String>,
    /// Ordered field-level compatibility report.
    pub findings: Vec<CompatibilityFinding>,
    /// Greatest finding severity.
    pub overall: CompatibilityResult,
}

/// One agent selected, pending approval, or shadowed by deterministic source
/// precedence.
#[derive(Debug, Clone, Serialize)]
pub struct ResolvedAgent {
    /// Canonical package record.
    #[serde(flatten)]
    pub agent: AgentRecord,
    /// Source kind used for agent approval semantics.
    pub source_kind: SourceKind,
    /// Source precedence; smaller values win.
    pub source_priority: i32,
}

/// A selected agent that lost its global slot to another selected agent.
#[derive(Debug, Clone, Serialize)]
pub struct ShadowedAgent {
    /// Agent that was not selected.
    pub agent: ResolvedAgent,
    /// Source-qualified selected winner.
    pub shadowed_by: String,
}

/// Deterministic agent resolution independent from skill resolution.
#[derive(Debug, Clone, Default, Serialize)]
pub struct AgentResolution {
    /// Selected global winners, one per agent slot.
    pub active_agents: Vec<ResolvedAgent>,
    /// Candidates requiring an explicit agent-scoped approval.
    pub pending_approval_agents: Vec<ResolvedAgent>,
    /// Approved candidates shadowed by selected global winners.
    pub shadowed_agents: Vec<ShadowedAgent>,
}

/// Read-only report used by `dalo agent list`.
#[derive(Debug, Clone, Default, Serialize)]
pub struct AgentListReport {
    /// Agent resolution across currently enabled sources.
    pub resolution: AgentResolution,
    /// Invalid package findings retained from source inventories.
    pub inventory_warnings: Vec<AgentInventoryWarning>,
    /// Sources whose inventory could not be read.
    pub source_errors: Vec<String>,
}

/// Read-only report used by `dalo agent show`.
#[derive(Debug, Clone, Serialize)]
pub struct AgentShowReport {
    /// Canonical package selected by its source-qualified reference.
    pub agent: AgentRecord,
    /// Deterministic provider previews. They never write target files.
    pub compilations: Vec<AgentCompilation>,
}

#[derive(Debug, Clone)]
struct ResolutionCandidate {
    agent: ResolvedAgent,
}

/// Resolve agent slots from source inventories.
///
/// Existing skill, source, author, and organization approvals never match an
/// agent.  Local agents are immediately eligible; every other source needs an
/// explicit `scope = "agent"` record whose value is the source-qualified slot
/// or stable ID.
#[must_use]
pub fn resolve_agents(
    sources: &[SourceConfig],
    inventories: &[SourceInventory],
    approvals: &[ApprovalRecord],
) -> AgentResolution {
    let sources_by_id = sources
        .iter()
        .filter(|source| source.enabled)
        .map(|source| (source.id.as_str(), source))
        .collect::<BTreeMap<_, _>>();
    let mut candidates = Vec::new();
    for inventory in inventories {
        let Some(source) = sources_by_id.get(inventory.source_id.as_str()) else {
            continue;
        };
        for record in &inventory.agents {
            candidates.push(ResolutionCandidate {
                agent: ResolvedAgent {
                    agent: record.clone(),
                    source_kind: source.kind,
                    source_priority: source.priority,
                },
            });
        }
    }
    candidates.sort_by(|left, right| {
        left.agent
            .source_priority
            .cmp(&right.agent.source_priority)
            .then_with(|| left.agent.agent.source_id.cmp(&right.agent.agent.source_id))
            .then_with(|| {
                left.agent
                    .agent
                    .source_ref
                    .cmp(&right.agent.agent.source_ref)
            })
    });

    let mut groups = BTreeMap::<String, Vec<ResolutionCandidate>>::new();
    for candidate in candidates {
        groups
            .entry(candidate.agent.agent.slot_name.clone())
            .or_default()
            .push(candidate);
    }
    let mut resolution = AgentResolution::default();
    for group in groups.into_values() {
        for candidate in &group {
            if !agent_is_approved(&candidate.agent, approvals) {
                resolution
                    .pending_approval_agents
                    .push(candidate.agent.clone());
            }
        }
        let Some(winner_index) = group
            .iter()
            .position(|candidate| agent_is_approved(&candidate.agent, approvals))
        else {
            continue;
        };
        let winner = group[winner_index].agent.clone();
        for candidate in group.iter().skip(winner_index + 1) {
            if agent_is_approved(&candidate.agent, approvals) {
                resolution.shadowed_agents.push(ShadowedAgent {
                    agent: candidate.agent.clone(),
                    shadowed_by: winner.agent.source_ref.clone(),
                });
            }
        }
        resolution.active_agents.push(winner);
    }
    resolution
        .active_agents
        .sort_by(|left, right| left.agent.source_ref.cmp(&right.agent.source_ref));
    resolution
        .pending_approval_agents
        .sort_by(|left, right| left.agent.source_ref.cmp(&right.agent.source_ref));
    resolution.shadowed_agents.sort_by(|left, right| {
        left.agent
            .agent
            .source_ref
            .cmp(&right.agent.agent.source_ref)
    });
    resolution
}

fn agent_is_approved(candidate: &ResolvedAgent, approvals: &[ApprovalRecord]) -> bool {
    // Source trust deliberately plays no part: a trusted source, including a
    // project source approved by its declaration, never activates an agent
    // without an explicit, source-qualified `agent` approval.
    candidate.source_kind == SourceKind::Local
        || approvals.iter().any(|approval| {
            approval.scope == "agent"
                && (approval.value == candidate.agent.source_ref
                    || candidate.agent.id.as_ref().is_some_and(|id| {
                        approval.value == format!("{}:{id}", candidate.agent.source_id)
                    }))
        })
}

/// Find a canonical agent by a source-qualified slot or stable ID.
pub fn find_agent(
    sources: &[SourceConfig],
    inventories: &[SourceInventory],
    reference: &str,
) -> DaloResult<AgentRecord> {
    let (source_id, selector) = reference
        .split_once(':')
        .filter(|(source_id, selector)| !source_id.is_empty() && !selector.is_empty())
        .ok_or_else(|| DaloError::InvalidArgument {
            reason: "agent references must use `<source>:<name>`, for example `local:reviewer`"
                .to_owned(),
        })?;
    if !sources.iter().any(|source| source.id == source_id) {
        return Err(DaloError::unknown_source(
            source_id,
            sources.iter().map(|source| source.id.clone()).collect(),
        ));
    }
    let candidates = inventories
        .iter()
        .find(|inventory| inventory.source_id == source_id)
        .map(|inventory| {
            inventory
                .agents
                .iter()
                .filter(|agent| {
                    agent.slot_name == selector || agent.id.as_deref() == Some(selector)
                })
                .cloned()
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    match candidates.as_slice() {
        [agent] => Ok(agent.clone()),
        [] => Err(DaloError::InvalidArgument {
            reason: format!(
                "agent `{reference}` was not found; use `dalo agent list` to inspect discovered packages"
            ),
        }),
        _ => Err(DaloError::InvalidArgument {
            reason: format!("agent reference `{reference}` is ambiguous"),
        }),
    }
}

/// Compile one canonical agent without writing a provider file.
#[must_use]
pub fn compile(agent: &CanonicalAgent, provider: AgentProvider) -> AgentCompilation {
    if agent
        .targets
        .as_ref()
        .is_some_and(|targets| !targets.iter().any(|target| target == provider.id()))
    {
        return AgentCompilation {
            provider,
            not_targeted: true,
            bytes: None,
            findings: Vec::new(),
            overall: CompatibilityResult::Exact,
        };
    }

    let mut findings = baseline_findings(agent, provider);
    match provider {
        AgentProvider::Claude => compile_claude(agent, &mut findings),
        AgentProvider::Codex => compile_codex(agent, &mut findings),
    }
    findings.sort_by(|left, right| left.field.cmp(&right.field));
    let overall = findings
        .iter()
        .map(|finding| finding.result)
        .max()
        .unwrap_or(CompatibilityResult::Exact);
    let bytes = (overall != CompatibilityResult::Blocked).then(|| match provider {
        AgentProvider::Claude => render_claude(agent),
        AgentProvider::Codex => render_codex(agent),
    });
    AgentCompilation {
        provider,
        not_targeted: false,
        bytes,
        findings,
        overall,
    }
}

/// Compile a discovered package while reporting non-projectable support files.
///
/// The canonical entry point alone is sufficient for native rendering, but a
/// provider projection must not imply that Claude or Codex can read arbitrary
/// neighboring package files.  The finding is intentionally non-blocking: the
/// canonical package and its hash still retain those files for audit and future
/// adapters.
#[must_use]
pub fn compile_record(record: &AgentRecord, provider: AgentProvider) -> AgentCompilation {
    let mut compilation = compile(&record.agent, provider);
    if !compilation.not_targeted && record.has_support_files {
        compilation.findings.push(unsupported(
            "additional_package_files",
            "native provider projections do not expose canonical package support files; move reusable content into a required skill",
        ));
        compilation
            .findings
            .sort_by(|left, right| left.field.cmp(&right.field));
        compilation.overall = compilation
            .findings
            .iter()
            .map(|finding| finding.result)
            .max()
            .unwrap_or(CompatibilityResult::Exact);
    }
    compilation
}

fn baseline_findings(agent: &CanonicalAgent, provider: AgentProvider) -> Vec<CompatibilityFinding> {
    let mut findings = vec![
        exact("name", "native agent name"),
        exact("description", "native description"),
        exact("prompt", "native system prompt"),
        exact("targets", "Dalo projection allowlist"),
        mapped(
            "model.profile",
            model_mapping(provider, agent.model.profile),
        ),
    ];
    let declared_identity_fields = [
        (agent.id.is_some(), "id"),
        (!agent.owners.is_empty(), "owners"),
        (!agent.tags.is_empty(), "tags"),
    ];
    for (is_declared, field) in declared_identity_fields {
        if !is_declared {
            continue;
        }
        findings.push(unsupported(
            field,
            "retained by Dalo but not emitted as native metadata",
        ));
    }
    findings
}

fn compile_claude(agent: &CanonicalAgent, findings: &mut Vec<CompatibilityFinding>) {
    if !agent.skills.is_empty() {
        findings.push(exact("skills", "Claude native skill preload metadata"));
    }
    if let Some(tools) = &agent.tools {
        if tools.allow.is_empty() {
            findings.push(blocked(
                "tools.allow",
                "Claude has no representation for an empty tool allowlist",
            ));
        } else if tools.allow.contains(&ToolCapability::UseMcp) {
            findings.push(blocked(
                "tools.allow",
                "Claude has no portable exact allowlist representation for use-mcp",
            ));
        } else {
            findings.push(mapped("tools.allow", "Claude tool allowlist"));
        }
    }
    add_unsupported_safety_boundaries(agent, findings, "Claude");
    validate_overlay(agent, AgentProvider::Claude, &["model"], findings);
}

fn compile_codex(agent: &CanonicalAgent, findings: &mut Vec<CompatibilityFinding>) {
    if !agent.skills.is_empty() {
        findings.push(CompatibilityFinding {
            field: "skills".to_owned(),
            result: CompatibilityResult::GuidanceOnly,
            message: "Codex receives deterministic skill-use guidance; it cannot preload a required skill".to_owned(),
            native_mapping: Some("developer_instructions".to_owned()),
        });
    }
    if agent.tools.is_some() {
        findings.push(blocked(
            "tools.allow",
            "Codex cannot enforce a portable hard tool allowlist",
        ));
    }
    add_unsupported_safety_boundaries(agent, findings, "Codex");
    validate_overlay(
        agent,
        AgentProvider::Codex,
        &["model", "model_reasoning_effort"],
        findings,
    );
}

fn add_unsupported_safety_boundaries(
    agent: &CanonicalAgent,
    findings: &mut Vec<CompatibilityFinding>,
    provider: &str,
) {
    if agent.filesystem.is_some() {
        findings.push(blocked(
            "filesystem",
            &format!("{provider} has no verified equivalent for this portable filesystem boundary"),
        ));
    }
    if agent.network.is_some() {
        findings.push(blocked(
            "network.allow",
            &format!("{provider} has no verified equivalent for this portable network boundary"),
        ));
    }
}

fn validate_overlay(
    agent: &CanonicalAgent,
    provider: AgentProvider,
    permitted: &[&str],
    findings: &mut Vec<CompatibilityFinding>,
) {
    let Some(value) = agent.providers.get(provider.id()) else {
        return;
    };
    let Some(overlay) = value.as_object() else {
        findings.push(blocked(
            &format!("providers.{}", provider.id()),
            "provider overlay must be a mapping",
        ));
        return;
    };
    for (key, value) in overlay {
        let field = format!("providers.{}.{}", provider.id(), key);
        if !permitted.contains(&key.as_str()) {
            findings.push(blocked(
                &field,
                "this provider overlay key cannot be represented without changing canonical safety semantics",
            ));
        } else if value.as_str().is_none() {
            findings.push(blocked(&field, "provider overlay values must be strings"));
        } else {
            findings.push(exact(&field, "exact provider-native overlay"));
        }
    }
}

fn render_claude(agent: &CanonicalAgent) -> String {
    let mut output = String::from("---\n");
    output.push_str("name: ");
    output.push_str(&yaml_string(&agent.name));
    output.push('\n');
    output.push_str("description: ");
    output.push_str(&yaml_string(&agent.description));
    output.push('\n');
    if let Some(model) = overlay_string(agent, AgentProvider::Claude, "model")
        .map(str::to_owned)
        .or_else(|| claude_model(agent.model.profile).map(str::to_owned))
    {
        output.push_str("model: ");
        output.push_str(&yaml_string(&model));
        output.push('\n');
    }
    if !agent.skills.is_empty() {
        output.push_str("skills:\n");
        for skill in &agent.skills {
            output.push_str("  - ");
            output.push_str(&yaml_string(skill));
            output.push('\n');
        }
    }
    if let Some(tools) = &agent.tools {
        let mut native = BTreeSet::new();
        for tool in &tools.allow {
            native.extend(tool.claude_tools().iter().copied());
        }
        output.push_str("tools:\n");
        for tool in native {
            output.push_str("  - ");
            output.push_str(&yaml_string(tool));
            output.push('\n');
        }
    }
    output.push_str("---\n");
    output.push_str(&agent.prompt);
    output
}

fn render_codex(agent: &CanonicalAgent) -> String {
    let mut output = String::new();
    output.push_str("name = ");
    output.push_str(&toml_string(&agent.name));
    output.push('\n');
    output.push_str("description = ");
    output.push_str(&toml_string(&agent.description));
    output.push('\n');
    if let Some(model) = overlay_string(agent, AgentProvider::Codex, "model") {
        output.push_str("model = ");
        output.push_str(&toml_string(model));
        output.push('\n');
    }
    if let Some(reasoning_effort) =
        overlay_string(agent, AgentProvider::Codex, "model_reasoning_effort")
            .or_else(|| codex_reasoning_effort(agent.model.profile))
    {
        output.push_str("model_reasoning_effort = ");
        output.push_str(&toml_string(reasoning_effort));
        output.push('\n');
    }
    let mut instructions = agent.prompt.clone();
    if !agent.skills.is_empty() {
        if !instructions.ends_with('\n') {
            instructions.push('\n');
        }
        instructions
            .push_str("\nRequired skills (must be available in the Codex skill directory):\n");
        for skill in &agent.skills {
            instructions.push_str("- Use `");
            instructions.push_str(skill);
            instructions.push_str("` when it applies.\n");
        }
    }
    output.push_str("developer_instructions = ");
    output.push_str(&toml_string(&instructions));
    output.push('\n');
    output
}

fn overlay_string<'a>(
    agent: &'a CanonicalAgent,
    provider: AgentProvider,
    key: &str,
) -> Option<&'a str> {
    agent.providers.get(provider.id())?.get(key)?.as_str()
}

fn claude_model(intent: ModelProfile) -> Option<&'static str> {
    match intent {
        ModelProfile::Inherit => None,
        ModelProfile::Fast => Some("haiku"),
        ModelProfile::Balanced => Some("sonnet"),
        ModelProfile::Deep => Some("opus"),
    }
}

fn codex_reasoning_effort(intent: ModelProfile) -> Option<&'static str> {
    match intent {
        ModelProfile::Inherit => None,
        ModelProfile::Fast => Some("low"),
        ModelProfile::Balanced => Some("medium"),
        ModelProfile::Deep => Some("high"),
    }
}

fn model_mapping(provider: AgentProvider, intent: ModelProfile) -> &'static str {
    match (provider, intent) {
        (AgentProvider::Claude, ModelProfile::Inherit) => "Claude inherited/default model",
        (AgentProvider::Claude, ModelProfile::Fast) => "Claude model haiku",
        (AgentProvider::Claude, ModelProfile::Balanced) => "Claude model sonnet",
        (AgentProvider::Claude, ModelProfile::Deep) => "Claude model opus",
        (AgentProvider::Codex, ModelProfile::Inherit) => {
            "Codex inherited model and reasoning effort"
        }
        (AgentProvider::Codex, ModelProfile::Fast) => {
            "Codex inherited model with low reasoning effort"
        }
        (AgentProvider::Codex, ModelProfile::Balanced) => {
            "Codex inherited model with medium reasoning effort"
        }
        (AgentProvider::Codex, ModelProfile::Deep) => {
            "Codex inherited model with high reasoning effort"
        }
    }
}

fn exact(field: &str, mapping: &str) -> CompatibilityFinding {
    CompatibilityFinding {
        field: field.to_owned(),
        result: CompatibilityResult::Exact,
        message: "preserved directly by the provider adapter".to_owned(),
        native_mapping: Some(mapping.to_owned()),
    }
}

fn mapped(field: &str, mapping: &str) -> CompatibilityFinding {
    CompatibilityFinding {
        field: field.to_owned(),
        result: CompatibilityResult::Mapped,
        message: "preserved through a deterministic provider mapping".to_owned(),
        native_mapping: Some(mapping.to_owned()),
    }
}

fn unsupported(field: &str, message: &str) -> CompatibilityFinding {
    CompatibilityFinding {
        field: field.to_owned(),
        result: CompatibilityResult::Unsupported,
        message: message.to_owned(),
        native_mapping: None,
    }
}

fn blocked(field: &str, message: &str) -> CompatibilityFinding {
    CompatibilityFinding {
        field: field.to_owned(),
        result: CompatibilityResult::Blocked,
        message: message.to_owned(),
        native_mapping: None,
    }
}

fn yaml_string(value: &str) -> String {
    // JSON strings are valid quoted YAML scalars and give us deterministic
    // escaping without relying on a serializer's map ordering.
    serde_json::to_string(value).expect("string serialization cannot fail")
}

fn toml_string(value: &str) -> String {
    toml::Value::String(value.to_owned()).to_string()
}

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

    fn write_agent(root: &Path, name: &str, contents: &str) -> PathBuf {
        let package = root.join("agents").join(name);
        fs::create_dir_all(&package).expect("package directory should be created");
        fs::write(package.join(AGENT_FILE), contents).expect("agent file should be written");
        package
    }

    #[test]
    fn scan_should_discover_and_hash_a_valid_canonical_package() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        let package = write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\nmodel:\n  profile: balanced\nskills: [pr-review]\n---\nReview the requested change.\n",
        );
        fs::write(package.join("notes.txt"), "support").expect("support file should be written");

        let inventory = scan_source_agents("local", temp.path());

        assert!(inventory.warnings.is_empty());
        assert_eq!(inventory.agents.len(), 1);
        let agent = &inventory.agents[0];
        assert_eq!(agent.source_ref, "local:reviewer");
        assert_eq!(agent.content_hash.len(), 64);
        assert!(agent.has_support_files);
    }

    #[test]
    fn scan_should_reject_noncanonical_names_and_missing_prompt() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: Reviewer\ndescription: Reviews code\n---\n",
        );
        let inventory = scan_source_agents("local", temp.path());
        assert!(inventory.agents.is_empty());
        assert_eq!(
            inventory.warnings[0].code,
            AgentInventoryWarningCode::InvalidPackage
        );
    }

    #[test]
    fn scan_should_reject_symlinks_anywhere_in_a_package() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        let package = write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\n---\nReview.\n",
        );
        std::os::unix::fs::symlink("AGENT.md", package.join("linked.md"))
            .expect("symlink should be created");
        let inventory = scan_source_agents("local", temp.path());
        assert!(inventory.agents.is_empty());
        assert_eq!(
            inventory.warnings[0].code,
            AgentInventoryWarningCode::UnsafePackageEntry
        );
    }

    #[test]
    fn compilation_should_be_deterministic_and_preserve_safe_mappings() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\nmodel:\n  profile: balanced\nskills:\n  - pr-review\ntools:\n  allow: [read-files, search-files]\nproviders:\n  claude:\n    model: sonnet-4\n---\nReview carefully.\n",
        );
        let agent = scan_source_agents("local", temp.path())
            .agents
            .remove(0)
            .agent;
        let first = compile(&agent, AgentProvider::Claude);
        let second = compile(&agent, AgentProvider::Claude);
        assert_eq!(first.bytes, second.bytes);
        assert_eq!(first.overall, CompatibilityResult::Mapped);
        let output = first.bytes.expect("safe compilation");
        assert!(output.contains("model: \"sonnet-4\""));
        assert!(output.contains("skills:"));
    }

    #[test]
    fn identity_metadata_should_only_be_reported_when_declared() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\nid: reviewer-v1\nowners: [platform]\ntags: [review]\n---\nReview carefully.\n",
        );
        let agent = scan_source_agents("local", temp.path())
            .agents
            .remove(0)
            .agent;

        let compilation = compile(&agent, AgentProvider::Claude);

        assert_eq!(compilation.overall, CompatibilityResult::Unsupported);
        assert_eq!(
            compilation
                .findings
                .iter()
                .filter(|finding| finding.is_dalo_identity_metadata())
                .count(),
            3
        );
    }

    #[test]
    fn scan_should_allow_punctuation_in_plain_yaml_scalars() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Review bugs!\nowners: [R&D reviewer]\n---\nReview bugs!\n",
        );

        let inventory = scan_source_agents("local", temp.path());

        assert!(inventory.warnings.is_empty());
        assert_eq!(inventory.agents[0].description, "Review bugs!");
        assert_eq!(inventory.agents[0].owners, ["R&D reviewer"]);
    }

    #[test]
    fn unsafe_yaml_indicators_are_rejected_at_scalar_boundaries() {
        assert!(reject_unsafe_yaml("description: !secret\n").is_err());
        assert!(reject_unsafe_yaml("description: &anchor value\n").is_err());
        assert!(reject_unsafe_yaml("description: *alias\n").is_err());
        assert!(reject_unsafe_yaml("description: ordinary! punctuation\n").is_ok());
    }

    #[test]
    fn json_serialization_redacts_secret_like_provider_overlays() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\nproviders:\n  claude:\n    model: sonnet-4\n    api_key: super-secret\n    nested:\n      access_token: nested-secret\n---\nReview carefully.\n",
        );
        let agent = scan_source_agents("local", temp.path())
            .agents
            .remove(0)
            .agent;

        let json = serde_json::to_value(agent).expect("agent should serialize");

        assert_eq!(json["providers"]["claude"]["model"], "sonnet-4");
        assert_eq!(json["providers"]["claude"]["api_key"], "[REDACTED]");
        assert_eq!(
            json["providers"]["claude"]["nested"]["access_token"],
            "[REDACTED]"
        );
    }

    #[test]
    fn package_support_files_should_be_visible_as_unsupported() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        let package = write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\n---\nReview carefully.\n",
        );
        fs::write(package.join("checklist.md"), "Check the diff.")
            .expect("support file should be written");
        let record = scan_source_agents("local", temp.path()).agents.remove(0);

        let compilation = compile_record(&record, AgentProvider::Claude);

        assert!(
            compilation
                .findings
                .iter()
                .any(|finding| finding.field == "additional_package_files")
        );
    }

    #[test]
    fn codex_should_block_explicit_tool_boundaries() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\ntools:\n  allow: []\n---\nReview carefully.\n",
        );
        let agent = scan_source_agents("local", temp.path())
            .agents
            .remove(0)
            .agent;
        let compilation = compile(&agent, AgentProvider::Codex);
        assert_eq!(compilation.overall, CompatibilityResult::Blocked);
        assert!(compilation.bytes.is_none());
    }

    #[test]
    fn claude_should_block_an_empty_tool_allowlist_without_rendering_tools() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\ntools:\n  allow: []\n---\nReview carefully.\n",
        );
        let agent = scan_source_agents("local", temp.path())
            .agents
            .remove(0)
            .agent;

        let compilation = compile(&agent, AgentProvider::Claude);

        assert_eq!(compilation.overall, CompatibilityResult::Blocked);
        assert!(compilation.bytes.is_none());
        assert!(compilation.findings.iter().any(|finding| {
            finding.field == "tools.allow"
                && finding.result == CompatibilityResult::Blocked
                && finding.message == "Claude has no representation for an empty tool allowlist"
        }));
    }

    #[test]
    fn empty_target_allowlist_should_not_compile_for_any_provider() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Reviews code\ntargets: []\n---\nReview carefully.\n",
        );
        let agent = scan_source_agents("local", temp.path())
            .agents
            .remove(0)
            .agent;
        assert!(compile(&agent, AgentProvider::Claude).not_targeted);
        assert!(compile(&agent, AgentProvider::Codex).not_targeted);
    }

    #[test]
    fn resolution_should_not_let_unapproved_team_agent_suppress_local_winner() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        let team_root = temp.path().join("team");
        let local_root = temp.path().join("local");
        write_agent(
            &team_root,
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Team reviewer\n---\nReview.\n",
        );
        write_agent(
            &local_root,
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Local reviewer\n---\nReview locally.\n",
        );
        let sources = vec![
            source("team", SourceKind::Team, &team_root, 0),
            source("local", SourceKind::Local, &local_root, 10),
        ];
        let inventories = vec![
            source_inventory("team", &team_root),
            source_inventory("local", &local_root),
        ];

        let resolution = resolve_agents(&sources, &inventories, &[]);

        assert_eq!(
            resolution.active_agents[0].agent.source_ref,
            "local:reviewer"
        );
        assert_eq!(
            resolution.pending_approval_agents[0].agent.source_ref,
            "team:reviewer"
        );
    }

    #[test]
    fn agent_approval_scope_should_not_match_skill_approvals() {
        let temp = tempfile::tempdir().expect("temp directory should be created");
        write_agent(
            temp.path(),
            "reviewer",
            "---\nschema_version: 1\nname: reviewer\ndescription: Team reviewer\n---\nReview.\n",
        );
        let sources = vec![source("team", SourceKind::Team, temp.path(), 0)];
        let inventories = vec![source_inventory("team", temp.path())];
        let skill_approval = ApprovalRecord {
            scope: "skill".to_owned(),
            value: "team:reviewer".to_owned(),
            granted_at_unix: None,
        };
        let agent_approval = ApprovalRecord {
            scope: "agent".to_owned(),
            value: "team:reviewer".to_owned(),
            granted_at_unix: None,
        };

        assert!(
            resolve_agents(&sources, &inventories, &[skill_approval])
                .active_agents
                .is_empty()
        );
        assert_eq!(
            resolve_agents(&sources, &inventories, &[agent_approval]).active_agents[0]
                .agent
                .source_ref,
            "team:reviewer"
        );
    }

    fn source(id: &str, kind: SourceKind, path: &Path, priority: i32) -> SourceConfig {
        SourceConfig {
            id: id.to_owned(),
            kind,
            path: path.to_path_buf(),
            subpath: None,
            priority,
            namespace: None,
            enabled: true,
            trusted: false,
            url: None,
            branch: None,
            update_policy: None,
            selection: Vec::new(),
            declared_by: None,
            declared_ref: None,
        }
    }

    fn source_inventory(source_id: &str, root: &Path) -> SourceInventory {
        let agent_inventory = scan_source_agents(source_id, root);
        SourceInventory {
            source_id: source_id.to_owned(),
            skills: Vec::new(),
            agents: agent_inventory.agents,
            plugins: Vec::new(),
            warnings: Vec::new(),
            agent_warnings: agent_inventory.warnings,
            plugin_warnings: Vec::new(),
        }
    }
}