fame 0.3.10

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

use std::collections::HashMap;
use std::sync::Arc;

use axum::{
    extract::{Path, Query, State},
    http::StatusCode,
    Json,
};
use serde::Deserialize;
use serde_json::{json, Value};
use tracing::info;

use crate::auth::{AuthenticatedUser, ForwardedToken, InstanceContext, WorkspaceAdmins};
use crate::entity_config::{slug_to_cedar_type, EntityConfig};
use crate::pdt::{
    AuthContext, CreateAssetRequest, CreateRelationRequest, CreateTagRequest, PdtAsset,
    PdtSearchResult,
};
use crate::AppState;

// ---------------------------------------------------------------------------
// Field mapping layer
// ---------------------------------------------------------------------------

/// Extract a field value from a PDT asset based on the field's `map` hint.
///
/// Supported mappings:
/// - `"title_suffix"` — strips the entity's `title_prefix` from the asset title
/// - `"content"`      — the asset's `content` string
/// - `"tag:X"`        — tag with category `X`
/// - `"metadata:X"`   — value from `metadata[X]`
/// - `"created_at"`   — asset `created_at` timestamp
/// - `"updated_at"`   — asset `updated_at` timestamp
fn extract_field_value(
    asset: &PdtAsset,
    field_name: &str,
    field_map: &Option<String>,
    config: &EntityConfig,
) -> Option<Value> {
    match field_map.as_deref() {
        Some("title_suffix") => {
            let title = asset
                .title
                .strip_prefix(&config.title_prefix)
                .unwrap_or(&asset.title);
            if title.is_empty() {
                None
            } else {
                Some(Value::String(title.to_string()))
            }
        }
        Some("content") => {
            if asset.content.is_empty() {
                None
            } else {
                Some(Value::String(asset.content.clone()))
            }
        }
        Some(m) if m.starts_with("tag:") => {
            let tag_name = &m[4..];
            asset
                .get_tag(tag_name)
                .map(|s| Value::String(s.to_string()))
        }
        Some(m) if m.starts_with("metadata:") => {
            let key = &m[9..];
            asset.metadata.get(key).cloned()
        }
        Some("created_at") => Some(Value::String(asset.created_at.clone())),
        Some("updated_at") => Some(Value::String(asset.updated_at.clone())),
        _ => {
            // No mapping hint — try tag with the field name as category
            asset
                .get_tag(field_name)
                .map(|s| Value::String(s.to_string()))
        }
    }
}

/// Extract a field value from a compact PDT search result.
///
/// Same semantics as [`extract_field_value`] but for `PdtSearchResult` which
/// lacks `content` and `metadata`.
fn extract_field_value_compact(
    result: &PdtSearchResult,
    field_name: &str,
    field_map: &Option<String>,
    config: &EntityConfig,
) -> Option<Value> {
    match field_map.as_deref() {
        Some("title_suffix") => {
            let title = result
                .title
                .strip_prefix(&config.title_prefix)
                .unwrap_or(&result.title);
            if title.is_empty() {
                None
            } else {
                Some(Value::String(title.to_string()))
            }
        }
        Some("content") => None, // Not available in compact results
        Some(m) if m.starts_with("tag:") => {
            let tag_name = &m[4..];
            result
                .get_tag(tag_name)
                .map(|s| Value::String(s.to_string()))
        }
        Some(m) if m.starts_with("metadata:") => None, // Not available in compact results
        Some("created_at") | Some("updated_at") => Some(Value::String(result.updated_at.clone())),
        _ => result
            .get_tag(field_name)
            .map(|s| Value::String(s.to_string())),
    }
}

/// Resolve relation fields for an asset by querying PDT for its relations.
///
/// For each relation field in the config, finds the related asset ID and adds
/// it to the entity JSON under the field name.
async fn resolve_relations(
    state: &AppState,
    instance_id: Option<&str>,
    asset_id: &str,
    config: &EntityConfig,
    entity: &mut serde_json::Map<String, Value>,
    token: Option<&str>,
) {
    let pdt = state.pdt.for_instance(instance_id);
    for (field_name, field_config) in config.relation_fields() {
        let rel_type = field_config
            .relation_type
            .as_deref()
            .unwrap_or("related_to");

        if let Ok(relations) = pdt.get_relations(asset_id, None, token).await {
            // Find the first relation matching the configured type where this
            // asset is the source (outgoing relation).
            let related_id = relations.iter().find_map(|r| {
                if r.from_asset_id == asset_id && r.relation_type == rel_type {
                    Some(r.to_asset_id.clone())
                } else {
                    None
                }
            });

            if let Some(id) = related_id {
                entity.insert(field_name.clone(), Value::String(id));
            }
        }
    }
}

/// Map a full PDT asset to an entity JSON object.
pub fn map_asset_to_entity(asset: &PdtAsset, config: &EntityConfig) -> Value {
    let mut entity = serde_json::Map::new();

    // Always include id
    entity.insert("id".to_string(), Value::String(asset.id.clone()));

    for (field_name, field_config) in &config.fields {
        if let Some(v) = extract_field_value(asset, field_name, &field_config.map, config) {
            entity.insert(field_name.clone(), v);
        }
    }

    Value::Object(entity)
}

/// Map a compact search result to an entity JSON object.
pub fn map_compact_to_entity(result: &PdtSearchResult, config: &EntityConfig) -> Value {
    let mut entity = serde_json::Map::new();

    entity.insert("id".to_string(), Value::String(result.id.clone()));

    for (field_name, field_config) in &config.fields {
        if let Some(v) = extract_field_value_compact(result, field_name, &field_config.map, config)
        {
            entity.insert(field_name.clone(), v);
        }
    }

    Value::Object(entity)
}

/// Map a full asset to entity JSON including resolved relations.
pub async fn map_asset_to_entity_with_relations(
    state: &AppState,
    instance_id: Option<&str>,
    asset: &PdtAsset,
    config: &EntityConfig,
    token: Option<&str>,
) -> Value {
    let mut entity_json = match map_asset_to_entity(asset, config) {
        Value::Object(m) => m,
        _ => return Value::Null,
    };

    resolve_relations(
        state,
        instance_id,
        &asset.id,
        config,
        &mut entity_json,
        token,
    )
    .await;

    Value::Object(entity_json)
}

// ---------------------------------------------------------------------------
// Query parameters
// ---------------------------------------------------------------------------

/// Generic query parameters — any key=value pair is accepted as a filter.
/// Known keys are extracted; the rest are passed through as tag filters.
#[derive(Debug, Deserialize, Clone)]
pub struct ListEntitiesQuery {
    /// Filter by status tag
    pub status: Option<String>,
    /// Filter by relation target (e.g. project_id for "related_to")
    pub attached_to: Option<String>,
}

// ---------------------------------------------------------------------------
// Handler context
// ---------------------------------------------------------------------------

/// Resolve entity config from the request path.
fn get_entity_config(state: &AppState, slug: &str) -> Option<Arc<EntityConfig>> {
    state
        .entity_configs
        .iter()
        .find(|c| c.slug == slug || c.slug == format!("{}s", slug))
        .cloned()
}

// ---------------------------------------------------------------------------
// Handlers (inner logic — slug is passed in, not extracted from Path)
// ---------------------------------------------------------------------------

/// GET /api/v1/{slug} — list entities
async fn list_entities_inner(
    state: &AppState,
    token: Option<&str>,
    instance_id: Option<&str>,
    slug: &str,
    query: &ListEntitiesQuery,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
    let pdt = state.pdt.for_instance(instance_id);
    let config = get_entity_config(state, &slug).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(json!({"error": format!("Unknown entity type: {}", slug)})),
        )
    })?;

    let type_tag = config.type_tag().unwrap_or(&config.slug);

    // Entities sharing a type tag (the three memory types all use
    // `type=agent-memory`) are discriminated by their full default_tags —
    // apply the discriminator on every result, not just the type search.
    let matches_entity = |r: &PdtSearchResult| config.tags_match(&r.tags);

    // If attached_to filter is set, we need to find assets related to that ID
    let mut entities: Vec<Value> = if let Some(ref attached_to_id) = query.attached_to {
        // 1. Find all assets of this type
        let results = pdt
            .search_by_tag("type", type_tag, token)
            .await
            .unwrap_or_default();

        // Filter by agent_id (X-Instance-Id) for per-agent memory isolation
        let results: Vec<PdtSearchResult> = if let Some(ref agent_id) = instance_id {
            results
                .into_iter()
                .filter(|r| r.get_tag("agent") == Some(agent_id))
                .collect()
        } else {
            results
        };

        // 2. For each result, check if it has a relation to attached_to_id
        let mut filtered = Vec::new();
        for r in &results {
            if !matches_entity(r) {
                continue;
            }
            if let Ok(relations) = pdt.get_relations(&r.id, None, token).await {
                let has_relation = relations.iter().any(|rel| {
                    rel.from_asset_id == r.id
                        && rel.to_asset_id == *attached_to_id
                        && config.relation_fields().iter().any(|(_, fc)| {
                            fc.relation_type.as_deref() == Some(rel.relation_type.as_str())
                        })
                });
                if has_relation {
                    let entity = map_compact_to_entity(r, &config);
                    filtered.push(entity);
                }
            }
        }
        filtered
    } else {
        let results = pdt
            .search_by_tag("type", type_tag, token)
            .await
            .unwrap_or_default();

        // Filter by agent_id (X-Instance-Id) for per-agent memory isolation
        let results: Vec<PdtSearchResult> = if let Some(ref agent_id) = instance_id {
            results
                .into_iter()
                .filter(|r| r.get_tag("agent") == Some(agent_id))
                .collect()
        } else {
            results
        };

        // If this entity type has relation fields, skip orphaned entities
        // (those with no outgoing relations). A document that isn't linked
        // to any project/workstream/task/issue should not appear in NGHR.
        let has_relation_fields = !config.relation_fields().is_empty();

        let mut filtered: Vec<Value> = Vec::new();
        for r in &results {
            if !matches_entity(r) {
                continue;
            }
            if has_relation_fields {
                let has_outgoing = match pdt.get_relations(&r.id, None, token).await {
                    Ok(relations) => relations.iter().any(|rel| rel.from_asset_id == r.id),
                    Err(_) => false,
                };
                if !has_outgoing {
                    continue;
                }
            }

            let entity = map_compact_to_entity(r, &config);

            // Apply status filter
            if let Some(ref status_filter) = query.status {
                let entity_status = entity.get("status").and_then(|s| s.as_str());
                if entity_status != Some(status_filter.as_str()) {
                    continue;
                }
            }

            filtered.push(entity);
        }
        filtered
    };

    // Sort by updated_at descending if that field exists
    entities.sort_by(|a, b| {
        let a_time = a.get("updated_at").and_then(|t| t.as_str()).unwrap_or("");
        let b_time = b.get("updated_at").and_then(|t| t.as_str()).unwrap_or("");
        b_time.cmp(a_time)
    });

    let total = entities.len();
    Ok(Json(json!({
        "entities": entities,
        "total": total,
    })))
}

/// GET /api/v1/{slug}/{id} — get a single entity by ID
async fn get_entity_inner(
    state: &AppState,
    token: Option<&str>,
    instance_id: Option<&str>,
    slug: &str,
    id: &str,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
    let pdt = state.pdt.for_instance(instance_id);
    let config = get_entity_config(state, &slug).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(json!({"error": format!("Unknown entity type: {}", slug)})),
        )
    })?;

    let asset = pdt
        .get_asset(id, token)
        .await
        .map_err(|e| (StatusCode::NOT_FOUND, Json(json!({"error": e.to_string()}))))?;

    // Type guard: an asset requested via /semantic-memory/{id} must actually
    // BE a semantic memory. Without this, any asset ID from the same
    // instance resolves and gets mapped as the wrong entity type.
    if !config.tags_match(&asset.tags) {
        return Err((
            StatusCode::NOT_FOUND,
            Json(json!({
                "error": format!("Asset {} is not a {}", id, config.name)
            })),
        ));
    }

    let entity =
        map_asset_to_entity_with_relations(state, instance_id, &asset, &config, token).await;

    Ok(Json(entity))
}

/// POST /api/v1/{slug} — create a new entity
/// Extract the groups claim (string array) from the authenticated user's
/// enriched claims.
fn user_groups(user: &AuthenticatedUser) -> Vec<String> {
    user.claims_extra
        .as_ref()
        .and_then(|c| c.get("groups"))
        .and_then(|g| g.as_array())
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default()
}

/// Pick the group that should OWN a memory created by `groups`:
/// the creator agent's per-agent memory group (`mem-*`) wins; otherwise the
/// workspace admin group (`ws-*-admins`, SPN `@domain` suffix tolerated).
/// None → caller stamps empty auth_context and logs loudly (admin-curable),
/// never rejects the write.
fn pick_owner_group(groups: &[String]) -> Option<String> {
    fn bare(g: &str) -> &str {
        g.split('@').next().unwrap_or(g)
    }
    groups
        .iter()
        .find(|g| bare(g).starts_with("mem-"))
        .cloned()
        .or_else(|| {
            groups
                .iter()
                .find(|g| {
                    let b = bare(g);
                    b.starts_with("ws-") && b.ends_with("-admins")
                })
                .cloned()
        })
}

async fn create_entity_inner(
    state: &AppState,
    user: &AuthenticatedUser,
    token: Option<&str>,
    instance_id: Option<&str>,
    slug: &str,
    body: Value,
    workspace_admins: WorkspaceAdmins,
) -> Result<(StatusCode, Json<Value>), (StatusCode, Json<Value>)> {
    let pdt = state.pdt.for_instance(instance_id);
    let config = get_entity_config(state, &slug).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(json!({"error": format!("Unknown entity type: {}", slug)})),
        )
    })?;

    // Cedar authorization check
    // NOTE: resource type must be the slug-derived Cedar identifier ("SemanticMemory"),
    // not config.name ("Semantic Memory") — spaces are illegal in Cedar type names
    // and would fail UID construction with a 500.
    //
    // Workspace scoping (0.2.0): when TOCPI creates an agent identity it
    // declares the workspace's admin group via X-Workspace-Admins; the scoped
    // check requires the caller's groups claim to contain it. Without the
    // header, only admin/conductor's unscoped permits can match. The value is
    // persisted into the asset metadata for later (trusted) edit-time checks.
    let mut workspace_admin_group: Option<String> = None;
    if let Some(ref authorizer) = state.authorizer {
        if let Some(ref action) = config.cedar.action_create {
            let claims = user.to_cedar_claims();
            let resource_attrs: Vec<(String, String)> = if slug == "agent-identity" {
                workspace_admin_group = workspace_admins.0.clone();
                workspace_admin_group
                    .clone()
                    .map(|g| {
                        vec![(
                            crate::cedar::enforcement::ADMIN_GROUP_METADATA_KEY.to_string(),
                            g,
                        )]
                    })
                    .unwrap_or_default()
            } else {
                Vec::new()
            };
            crate::cedar::enforcement::check_permission_scoped(
                authorizer,
                &claims,
                action,
                &slug_to_cedar_type(&config.slug),
                "<_>",
                &resource_attrs,
            )
            .map_err(|status| (status, Json(json!({"error": "Access denied"}))))?;
        }
    }

    let body_obj = body.as_object().ok_or_else(|| {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": "Request body must be a JSON object"})),
        )
    })?;

    // Validate required fields
    for (field_name, field_config) in &config.fields {
        if field_config.required && !body_obj.contains_key(field_name) {
            return Err((
                StatusCode::BAD_REQUEST,
                Json(json!({
                    "error": "Validation failed",
                    "details": format!("Missing required field: {}", field_name)
                })),
            ));
        }
    }

    // --- Build title ---
    let title_value = config
        .fields
        .iter()
        .find(|(_, fc)| fc.map.as_deref() == Some("title_suffix"))
        .and_then(|(name, _)| body_obj.get(name))
        .and_then(|v| v.as_str())
        .unwrap_or("Untitled");

    let title = format!("{}{}", config.title_prefix, title_value);

    // --- Build content ---
    let content = config
        .fields
        .iter()
        .find(|(_, fc)| fc.map.as_deref() == Some("content"))
        .and_then(|(name, _)| body_obj.get(name))
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    // --- Build tags ---
    let mut tags: Vec<CreateTagRequest> = Vec::new();

    // Default tags from config
    for (category, value) in &config.default_tags {
        tags.push(CreateTagRequest {
            category: category.clone(),
            value: value.clone(),
        });
    }

    // Inject agent_id from X-Instance-Id as a tag for per-agent memory isolation.
    // The X-Instance-Id header carries the agent's identity (injected by Aether).
    // Fame uses it for dual purpose: PDT multi-tenant routing AND per-agent memory scoping.
    if let Some(ref agent_id) = instance_id {
        tags.push(CreateTagRequest {
            category: "agent".to_string(),
            value: agent_id.to_string(),
        });
    }

    // Fields mapped to tags
    for (field_name, field_config) in &config.fields {
        if let Some(ref m) = field_config.map {
            if let Some(tag_name) = m.strip_prefix("tag:") {
                // Use provided value, or fall back to default
                let value = body_obj
                    .get(field_name)
                    .and_then(|v| v.as_str())
                    .or(field_config.default.as_deref());

                if let Some(v) = value {
                    tags.push(CreateTagRequest {
                        category: tag_name.to_string(),
                        value: v.to_string(),
                    });
                }
            }
        }
    }

    // --- Build metadata ---
    let mut metadata: HashMap<String, Value> = HashMap::new();
    for (field_name, field_config) in &config.fields {
        if let Some(ref m) = field_config.map {
            if let Some(key) = m.strip_prefix("metadata:") {
                if let Some(v) = body_obj.get(field_name) {
                    metadata.insert(key.to_string(), v.clone());
                }
            }
        }
    }

    // Persist the workspace admin group on agent identities — the trusted
    // anchor for edit-time workspace scoping (header is create-time only).
    if let Some(group) = workspace_admin_group.as_ref() {
        metadata.insert(
            crate::cedar::enforcement::ADMIN_GROUP_METADATA_KEY.to_string(),
            Value::String(group.clone()),
        );
    }

    // --- Inherit auth_context from relation target (e.g. attached_to) ---
    let mut parent_auth_ctx = None;
    for (field_name, _field_config) in config.relation_fields() {
        if let Some(target_id) = body_obj.get(field_name).and_then(|v| v.as_str()) {
            parent_auth_ctx = pdt.get_auth_context(target_id, token).await.ok().flatten();
            if parent_auth_ctx.is_some() {
                break;
            }
        }
    }

    // --- auth_context stamp (fallback when nothing is inherited) ---
    // Every memory must be born readable by its owner group: agent creators
    // stamp their per-agent `mem-<prefix>` group, human creators stamp their
    // workspace admin group. Empty auth_context = admin-only-invisible —
    // exactly the bug that made fresh agents blind to their own writes
    // (every memory needed a hand-patch until 2026-08-29). Groups are
    // stamped in the exact claim form so PDT's owner_groups matching sees
    // identical strings on both sides.
    //
    // v0.3.6 (task 3ff56a20, Phase-2 candidate GO): AGENT IDENTITIES stamp
    // with the IDENTITY'S admin_group forms — not the creator's group. The
    // creator's band made charters readable by the creator + admins only;
    // the owning agent stayed blind to its own charter (live: saman 403
    // View on b8496c07, 2026-09-02). tocpi v0.6.3+ sends admin_group in
    // claim form (mem-<agent>@<domain>); it is stamped verbatim. No retro
    // re-banding — owner-scope identities keep their bands (creator
    // boundary, owner ruling).
    let identity_admin_group = if slug == "agent-identity" {
        workspace_admins.0.clone()
    } else {
        None
    };
    let auth_context = parent_auth_ctx.or_else(|| {
        if slug == "agent-identity" {
            return Some(AuthContext {
                visibility: "team".to_string(),
                owner_groups: identity_admin_group.map(|g| vec![g]).unwrap_or_default(),
                confidentiality: String::new(),
            });
        }
        pick_owner_group(&user_groups(user)).map(|group| AuthContext {
            visibility: "team".to_string(),
            owner_groups: vec![group],
            confidentiality: String::new(),
        })
    });
    if auth_context.is_none() {
        tracing::warn!(
            "memory created with EMPTY auth_context: creator '{}' has no mem-* or ws-*-admins group — admin re-stamp required",
            user.user_id
        );
    }

    // --- Create the PDT asset ---
    let asset = pdt
        .create_asset(
            CreateAssetRequest {
                title,
                content,
                tags: Some(tags),
                auth_context,
            },
            token,
        )
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": e.to_string()})),
            )
        })?;

    // If metadata was provided, update the asset to include it
    if !metadata.is_empty() {
        let mut metadata_req = pdt
            .http_client()
            .put(format!("{}/api/assets/{}", pdt.base_url(), asset.id))
            .bearer_auth(token.unwrap_or(""));
        if let Some(id) = instance_id {
            metadata_req = metadata_req.header("X-Instance-Id", id);
        }
        let _ = metadata_req
            .json(&json!({"metadata": metadata}))
            .send()
            .await;
    }

    info!(
        "Created {} entity: {} ({})",
        config.name, title_value, asset.id
    );

    // --- Create relations ---
    for (field_name, field_config) in config.relation_fields() {
        if let Some(target_id) = body_obj.get(field_name).and_then(|v| v.as_str()) {
            let rel_type = field_config
                .relation_type
                .as_deref()
                .unwrap_or("related_to");

            let _ = pdt
                .create_relation(
                    CreateRelationRequest {
                        from_asset_id: asset.id.clone(),
                        to_asset_id: target_id.to_string(),
                        relation_type: rel_type.to_string(),
                        metadata: None,
                    },
                    token,
                )
                .await;

            info!("Linked {} → {} ({})", asset.id, target_id, rel_type);
        }
    }

    // --- Build response ---
    let mut entity = map_asset_to_entity(&asset, &config);

    // Include relation fields in response
    if let Value::Object(ref mut map) = entity {
        for (field_name, _) in config.relation_fields() {
            if let Some(target_id) = body_obj.get(field_name).and_then(|v| v.as_str()) {
                map.insert(field_name.clone(), Value::String(target_id.to_string()));
            }
        }
    }

    Ok((StatusCode::CREATED, Json(entity)))
}

#[cfg(test)]
mod auth_context_stamp_tests {
    use super::pick_owner_group;

    #[test]
    fn agent_mem_group_wins() {
        let groups = vec![
            "pdt-api-agents".to_string(),
            "mem-e69605b1@idp.example.com".to_string(),
        ];
        assert_eq!(
            pick_owner_group(&groups).as_deref(),
            Some("mem-e69605b1@idp.example.com")
        );
    }

    #[test]
    fn human_ws_admins_group_when_no_mem_group() {
        let groups = vec![
            "pdt-api-users@idp.example.com".to_string(),
            "ws-9f2e7d01-4c5b-6a7d-8e9f-001122334455-admins".to_string(),
        ];
        assert_eq!(
            pick_owner_group(&groups).as_deref(),
            Some("ws-9f2e7d01-4c5b-6a7d-8e9f-001122334455-admins")
        );
    }

    #[test]
    fn spn_form_ws_admins_still_matches() {
        let groups = vec!["ws-abc123-admins@idp.example.com".to_string()];
        assert_eq!(
            pick_owner_group(&groups).as_deref(),
            Some("ws-abc123-admins@idp.example.com")
        );
    }

    #[test]
    fn plain_roles_never_own_memories() {
        let groups = vec![
            "pdt-api-users@idp.example.com".to_string(),
            "pdt-api-agents".to_string(),
        ];
        assert_eq!(pick_owner_group(&groups), None);
    }

    #[test]
    fn mem_group_preferred_over_ws_admins() {
        let groups = vec!["ws-abc-admins".to_string(), "mem-ff766ee2".to_string()];
        assert_eq!(pick_owner_group(&groups).as_deref(), Some("mem-ff766ee2"));
    }
}
/// PATCH /api/v1/{slug}/{id}/status — update entity status
async fn update_entity_status_inner(
    state: &AppState,
    user: &AuthenticatedUser,
    token: Option<&str>,
    instance_id: Option<&str>,
    slug: &str,
    id: &str,
    body: Value,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
    let pdt = state.pdt.for_instance(instance_id);
    let config = get_entity_config(state, &slug).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(json!({"error": format!("Unknown entity type: {}", slug)})),
        )
    })?;

    // Cedar authorization check
    // Same slug-derived resource type as create (see note above).
    //
    // Workspace scoping (0.2.0): for agent identities the anchor comes from
    // the STORED asset metadata (admin_group), never from a caller header —
    // callers cannot edit identities they don't administer by declaring a
    // group of their own.
    if let Some(ref authorizer) = state.authorizer {
        if let Some(ref action) = config.cedar.action_edit {
            let claims = user.to_cedar_claims();
            let resource_attrs: Vec<(String, String)> = if slug == "agent-identity" {
                pdt.get_asset(id, token)
                    .await
                    .ok()
                    .and_then(|asset| {
                        asset
                            .metadata
                            .get(crate::cedar::enforcement::ADMIN_GROUP_METADATA_KEY)
                            .and_then(|v| v.as_str())
                            .map(|g| {
                                vec![(
                                    crate::cedar::enforcement::ADMIN_GROUP_METADATA_KEY.to_string(),
                                    g.to_string(),
                                )]
                            })
                    })
                    .unwrap_or_default()
            } else {
                Vec::new()
            };
            crate::cedar::enforcement::check_permission_scoped(
                authorizer,
                &claims,
                action,
                &slug_to_cedar_type(&config.slug),
                id,
                &resource_attrs,
            )
            .map_err(|status| (status, Json(json!({"error": "Access denied"}))))?;
        }
    }

    let new_status = body.get("status").and_then(|s| s.as_str()).ok_or_else(|| {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": "Missing 'status' field"})),
        )
    })?;

    // Validate status is in allowed values
    if let Some(allowed) = config.status_values() {
        if !allowed.iter().any(|v| v == new_status) {
            return Err((
                StatusCode::BAD_REQUEST,
                Json(json!({
                    "error": "Invalid status",
                    "allowed_values": allowed,
                    "got": new_status
                })),
            ));
        }
    }

    let asset = pdt
        .update_asset_tag(id, "status", new_status, token)
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": e.to_string()})),
            )
        })?;

    let entity =
        map_asset_to_entity_with_relations(state, instance_id, &asset, &config, token).await;

    Ok(Json(entity))
}

/// Update an agent identity's content (charter). PATCH /api/v1/{slug}s/{id}/content
///
/// Agent-identity only (v0.3.0) — the identity content update surface that
/// Cedar `EditAgentIdentity` was designed for but that never shipped. The
/// tocpi control plane (PATCH /api/agents/{id}) calls this so content lands
/// in place as the agent's live identity — update, never duplicate.
///
/// Semantics mirror tocpi v0.6.0 for consistency:
/// - `content` required; empty string = explicit wipe (logged loudly)
/// - response echoes old→new sha256 so callers verify what landed
/// - unknown id → loud 404, NEVER create-on-miss (provisioning is tocpi's
///   job via the create route)
/// - content only — title/status stay on their existing surfaces
///
/// Authorization reuses the wired workspace-scoped `EditAgentIdentity` path:
/// the anchor comes from the STORED asset metadata (admin_group), never from
/// a caller header.
///
/// Instance scoping is REQUIRED: identity assets live in the agent's nested
/// DB (created via X-Instance-Id routing). A request without the header
/// would silently look in the global DB — rejected with 400 instead.
async fn update_identity_content_inner(
    state: &AppState,
    user: &AuthenticatedUser,
    token: Option<&str>,
    instance_id: Option<&str>,
    slug: &str,
    id: &str,
    body: Value,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
    let instance_id = instance_id.ok_or_else(|| {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": "X-Instance-Id header required: agent identity content                 updates are instance-scoped (the identity lives in the agent's nested DB)"})),
        )
    })?;
    let pdt = state.pdt.for_instance(Some(instance_id));
    let config = get_entity_config(state, slug).ok_or_else(|| {
        (
            StatusCode::NOT_FOUND,
            Json(json!({"error": format!("Unknown entity type: {}", slug)})),
        )
    })?;

    // Loud 404 BEFORE authorization: an unknown id is a caller error, not an
    // access-control outcome, and the Cedar check needs the stored
    // admin_group from the asset anyway.
    let asset = pdt.get_asset(id, token).await.map_err(|e| {
        (
            StatusCode::NOT_FOUND,
            Json(json!({"error": format!("Agent identity not found: {} ({})", id, e)})),
        )
    })?;

    // Cedar authorization check — same slug-derived resource type and
    // workspace scoping as the status update: the anchor comes from the
    // STORED asset metadata (admin_group), never from a caller header.
    if let Some(ref authorizer) = state.authorizer {
        if let Some(ref action) = config.cedar.action_edit {
            let claims = user.to_cedar_claims();
            let resource_attrs: Vec<(String, String)> = asset
                .metadata
                .get(crate::cedar::enforcement::ADMIN_GROUP_METADATA_KEY)
                .and_then(|v| v.as_str())
                .map(|g| {
                    vec![(
                        crate::cedar::enforcement::ADMIN_GROUP_METADATA_KEY.to_string(),
                        g.to_string(),
                    )]
                })
                .unwrap_or_default();
            crate::cedar::enforcement::check_permission_scoped(
                authorizer,
                &claims,
                action,
                &slug_to_cedar_type(&config.slug),
                id,
                &resource_attrs,
            )
            .map_err(|status| (status, Json(json!({"error": "Access denied"}))))?;
        }
    }

    // Content extraction: required, must be a string. Empty = explicit wipe.
    let new_content = body
        .get("content")
        .and_then(|c| c.as_str())
        .ok_or_else(|| {
            (
                StatusCode::BAD_REQUEST,
                Json(json!({"error": "Missing or non-string 'content' field"})),
            )
        })?
        .to_string();
    if new_content.trim().is_empty() {
        tracing::warn!(
            "EXPLICIT CONTENT WIPE: user {} wiped content of agent identity {}",
            user.user_id,
            id
        );
    }

    // old→new sha256 echo (parity with tocpi v0.6.0)
    use sha2::Digest;
    let hash = |s: &str| format!("sha256:{:x}", sha2::Sha256::digest(s.as_bytes()));
    let old_hash = if asset.content.is_empty() {
        None
    } else {
        Some(hash(&asset.content))
    };

    let asset = pdt
        .update_asset_content(id, &new_content, token)
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": format!("Failed to update identity content: {}", e)})),
            )
        })?;

    tracing::info!(
        "Agent identity {} content updated by user {} ({} chars)",
        id,
        user.user_id,
        new_content.len()
    );

    let mut entity =
        map_asset_to_entity_with_relations(state, Some(instance_id), &asset, &config, token).await;
    if let Some(obj) = entity.as_object_mut() {
        obj.insert(
            "content_hash".to_string(),
            json!({
                "old": old_hash,
                "new": hash(&new_content),
            }),
        );
    }

    Ok(Json(entity))
}

// ---------------------------------------------------------------------------
// Router builder
// ---------------------------------------------------------------------------

/// Register entity routes dynamically based on loaded configs.
///
/// For each entity config with `handler = "generic"`, registers routes:
///
/// ```text
/// GET    /api/v1/{slug}s            → list_entities
/// GET    /api/v1/{slug}s/{id}       → get_entity
/// POST   /api/v1/{slug}s            → create_entity
/// PATCH  /api/v1/{slug}s/{id}/status → update_entity_status
/// ```
///
/// Called from `main.rs` after configs are loaded.
pub fn register_entity_routes(
    router: axum::Router<AppState>,
    configs: &[Arc<EntityConfig>],
) -> axum::Router<AppState> {
    use axum::routing::{get, patch};

    let mut router = router;

    for config in configs {
        if config.handler != "generic" {
            continue;
        }

        // Pluralise slug for the path (e.g. "document" → "documents")
        let base = format!("/api/v1/{}s", config.slug);
        let slug = config.slug.clone();

        tracing::info!("Registering entity routes for {} at {}", config.name, base);

        // GET + POST /api/v1/{slug}s
        let slug_for_list = slug.clone();
        router = router.route(
            &base,
            get(
                move |State(state): State<AppState>,
                      ForwardedToken(token): ForwardedToken,
                      instance: InstanceContext,
                      Query(query): Query<ListEntitiesQuery>| {
                    let slug = slug_for_list.clone();
                    async move {
                        list_entities_inner(
                            &state,
                            token.as_deref(),
                            instance.as_deref(),
                            &slug,
                            &query,
                        )
                        .await
                    }
                },
            )
            .post(
                move |State(state): State<AppState>,
                      user: AuthenticatedUser,
                      ForwardedToken(token): ForwardedToken,
                      instance: InstanceContext,
                      workspace_admins: WorkspaceAdmins,
                      Json(body): Json<Value>| {
                    let slug = slug.clone();
                    async move {
                        create_entity_inner(
                            &state,
                            &user,
                            token.as_deref(),
                            instance.as_deref(),
                            &slug,
                            body,
                            workspace_admins,
                        )
                        .await
                    }
                },
            ),
        );

        // GET /api/v1/{slug}s/{id}
        let slug_for_get = config.slug.clone();
        router = router.route(
            &format!("{}/{{id}}", base),
            get(
                move |State(state): State<AppState>,
                      ForwardedToken(token): ForwardedToken,
                      instance: InstanceContext,
                      Path(id): Path<String>| {
                    let slug = slug_for_get.clone();
                    async move {
                        get_entity_inner(&state, token.as_deref(), instance.as_deref(), &slug, &id)
                            .await
                    }
                },
            ),
        );

        // PATCH /api/v1/{slug}s/{id}/content — agent identity charters only
        // (v0.3.0): the governed identity-content write path. Scoped to
        // agent-identity on purpose; other entities keep their surfaces.
        if config.slug == "agent-identity" {
            let slug_for_content = config.slug.clone();
            router = router.route(
                &format!("{}/{{id}}/content", base),
                patch(
                    move |State(state): State<AppState>,
                          user: AuthenticatedUser,
                          ForwardedToken(token): ForwardedToken,
                          instance: InstanceContext,
                          Path(id): Path<String>,
                          Json(body): Json<Value>| {
                        let slug = slug_for_content.clone();
                        async move {
                            update_identity_content_inner(
                                &state,
                                &user,
                                token.as_deref(),
                                instance.as_deref(),
                                &slug,
                                &id,
                                body,
                            )
                            .await
                        }
                    },
                ),
            );
        }

        // PATCH /api/v1/{slug}s/{id}/status
        let slug_for_patch = config.slug.clone();
        router = router.route(
            &format!("{}/{{id}}/status", base),
            patch(
                move |State(state): State<AppState>,
                      user: AuthenticatedUser,
                      ForwardedToken(token): ForwardedToken,
                      instance: InstanceContext,
                      Path(id): Path<String>,
                      Json(body): Json<Value>| {
                    let slug = slug_for_patch.clone();
                    async move {
                        update_entity_status_inner(
                            &state,
                            &user,
                            token.as_deref(),
                            instance.as_deref(),
                            &slug,
                            &id,
                            body,
                        )
                        .await
                    }
                },
            ),
        );
    }

    router
}

/// Serve available entity configs (for Torpi to discover).
pub async fn list_entity_configs(State(state): State<AppState>) -> Json<Value> {
    let configs: Vec<Value> = state
        .entity_configs
        .iter()
        .map(|c| {
            json!({
                "name": c.name,
                "slug": c.slug,
                "title_prefix": c.title_prefix,
                "fields": c.fields.iter().map(|(name, fc)| {
                    json!({
                        "name": name,
                        "type": format!("{:?}", fc.field_type).to_lowercase(),
                        "required": fc.required,
                        "label": fc.label,
                        "values": fc.values,
                        "badge": fc.badge,
                        "in_views": fc.in_views,
                    })
                }).collect::<Vec<_>>(),
                "views": c.views.iter().map(|(_name, vc)| {
                    json!({
                        "layout": vc.layout,
                        "fields": vc.fields,
                    })
                }).collect::<Vec<_>>(),
            })
        })
        .collect();

    Json(json!({
        "entities": configs,
        "total": configs.len(),
    }))
}

// ---------------------------------------------------------------------------
// Tests — update_identity_content_inner: mock PDT + real Cedar policies
// ---------------------------------------------------------------------------

#[cfg(test)]
mod identity_content_tests {
    //! Integration tests for update_identity_content_inner.
    //!
    //! TWO BACKEND TIERS, selected automatically:
    //! - **real-pdt**: a real PDT server (dev auth, sqlite backend) booted
    //!   from a PREBUILT sibling binary (`../pdt/target/debug/pdt`, or
    //!   `FAME_IT_PDT_BIN`). Full tenant routing — nested-DB provisioning,
    //!   X-Instance-Id isolation, real wire.
    //!   The binary must be built with `--features sqlite-backend`: a
    //!   default-features build ignores `PDT_DB_BACKEND=sqlite` (the arm is
    //!   cfg-gated), falls back to mongodb, and never becomes healthy.
    //! - **mock-pdt** (always available, CI default): an in-process axum
    //!   server speaking PDT's wire format (tag objects carry ids, assets
    //!   use `_id`) — keeps the gate green without a sibling checkout.
    //!
    //! The tier is announced on stdout; tests are identical for both.
    //! NOTE: never build pdt on demand inside tests — a missing binary
    //! silently downgrades to the mock tier (loud eprintln).

    use super::*;
    use crate::pdt::{CreateAssetRequest, CreateTagRequest, PdtClient};
    use axum::response::IntoResponse;
    use axum::routing::{get, post, put};
    use std::process::{Child, Command, Stdio};
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Mutex;

    const ADMIN_GROUP: &str = "ws-1-admins";
    const OLD: &str = "old charter";
    const NEW: &str = "new charter";

    fn user_with(role: &str, groups: &[&str]) -> AuthenticatedUser {
        AuthenticatedUser {
            user_id: "user-test".to_string(),
            username: Some("tester".to_string()),
            email: None,
            claims_extra: Some(std::collections::HashMap::from([
                (
                    "role".to_string(),
                    serde_json::Value::String(role.to_string()),
                ),
                (
                    "groups".to_string(),
                    serde_json::Value::Array(
                        groups
                            .iter()
                            .map(|g| serde_json::Value::String(g.to_string()))
                            .collect(),
                    ),
                ),
            ])),
        }
    }

    fn sha_hex(s: &str) -> String {
        use sha2::Digest;
        format!("sha256:{:x}", sha2::Sha256::digest(s.as_bytes()))
    }

    // ------------------------------------------------------------------
    // Tier selection
    // ------------------------------------------------------------------

    fn prebuilt_pdt_bin() -> Option<std::path::PathBuf> {
        if let Ok(p) = std::env::var("FAME_IT_PDT_BIN") {
            let b = std::path::PathBuf::from(p);
            if b.exists() {
                return Some(b);
            }
        }
        let b = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()?
            .join("pdt/target/debug/pdt");
        b.exists().then_some(b)
    }

    struct PdtProc(Child);
    impl Drop for PdtProc {
        fn drop(&mut self) {
            let _ = self.0.kill();
            let _ = self.0.wait();
        }
    }

    async fn free_port() -> u16 {
        let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = l.local_addr().unwrap().port();
        drop(l);
        port
    }

    async fn wait_healthy(base: &str) {
        let http = reqwest::Client::new();
        let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(15);
        loop {
            if tokio::time::Instant::now() > deadline {
                panic!("pdt did not become healthy at {base}");
            }
            if let Ok(resp) = http.get(format!("{base}/health")).send().await {
                if resp.status().is_success() {
                    return;
                }
            }
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
    }

    async fn spawn_real_pdt(bin: std::path::PathBuf) -> (String, PdtProc) {
        let n = SEQ.fetch_add(1, Ordering::SeqCst);
        let tmp = std::env::temp_dir().join(format!("fame-pdt-it-{}-{n}", std::process::id()));
        std::fs::create_dir_all(tmp.join("instances")).expect("tmp instances dir");
        let port = free_port().await;
        let child = Command::new(&bin)
            .env("PDT_HOST", "127.0.0.1")
            .env("PDT_PORT", port.to_string())
            .env("PDT_DB_BACKEND", "sqlite")
            .env("SQLITE_PATH", tmp.join("global.db"))
            .env("PDT_INSTANCES_DIR", tmp.join("instances"))
            .env("AUTH_ENABLED", "false")
            .env("AUTH_DEV_MODE", "true")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("failed to spawn prebuilt pdt binary");
        let base = format!("http://127.0.0.1:{port}");
        wait_healthy(&base).await;
        (base, PdtProc(child))
    }

    // ------------------------------------------------------------------
    // Mock tier — in-process, wire-accurate PDT stand-in
    // ------------------------------------------------------------------
    // Wire lessons baked in (learned against the real server):
    // - asset objects carry "_id" (fame PdtAsset renames id)
    // - TAG objects carry "id" + "category" + "value" (fame PdtTag requires id)
    // - /api/search returns { "data": [ ... ] }
    // - PUT merges partial JSON (content/title/metadata)
    // - POST persists auth_context verbatim, else AuthContext::default() —
    //   real repos: request.auth_context.or_else(|| Some(default)) (v0.3.7)

    #[derive(Default)]
    struct MockDb {
        assets: Vec<serde_json::Value>,
        next_id: u32,
    }

    struct MockState {
        db: Mutex<MockDb>,
    }

    fn mock_tag(db: &mut MockDb, category: &str, value: &str) -> serde_json::Value {
        db.next_id += 1;
        serde_json::json!({
            "id": format!("tag-{}", db.next_id),
            "category": category,
            "value": value,
            "added_by": "mock",
            "added_at": "2026-08-29T00:00:00Z"
        })
    }

    async fn spawn_mock_pdt() -> String {
        let state = std::sync::Arc::new(MockState {
            db: Mutex::new(MockDb::default()),
        });

        // POST /api/assets — create
        let st = state.clone();
        let create_route = post(
            move |headers: axum::http::HeaderMap, Json(body): Json<serde_json::Value>| {
                let st = st.clone();
                async move {
                    let mut db = st.db.lock().unwrap();
                    db.next_id += 1;
                    let id = format!("ident-{:04}", db.next_id);
                    let mut tags: Vec<serde_json::Value> = Vec::new();
                    if let Some(list) = body.get("tags").and_then(|t| t.as_array()) {
                        for t in list {
                            tags.push(mock_tag(
                                &mut db,
                                t["category"].as_str().unwrap_or_default(),
                                t["value"].as_str().unwrap_or_default(),
                            ));
                        }
                    }
                    let _ = headers; // instance routing is process-global in the mock
                                     // Wire parity (v0.3.7 CI lesson): real PDT persists
                                     // auth_context at creation — request value verbatim, else
                                     // AuthContext::default() (sqlite_asset_repository.rs:112,
                                     // mongo_asset_repository.rs:53). Generic PUT below still
                                     // drops it: real UpdateAssetRequest has no such field.
                    let auth_context = body
                        .get("auth_context")
                        .cloned()
                        .filter(|v| v.is_object())
                        .unwrap_or_else(|| {
                            serde_json::json!({
                                "visibility": "",
                                "owner_groups": [],
                                "confidentiality": ""
                            })
                        });
                    let asset = serde_json::json!({
                        "_id": id,
                        "title": body.get("title").cloned().unwrap_or(serde_json::Value::Null),
                        "content": body.get("content").cloned().unwrap_or(serde_json::Value::String(String::new())),
                        "tags": tags,
                        "metadata": {},
                        "auth_context": auth_context,
                        "created_at": "2026-08-29T00:00:00Z",
                        "updated_at": "2026-08-29T00:00:00Z"
                    });
                    db.assets.push(asset.clone());
                    axum::Json(asset).into_response()
                }
            },
        );

        // GET/PUT /api/assets/{id}
        let st_get = state.clone();
        let st_put = state.clone();
        let asset_route = get(move |Path(id): Path<String>| {
            let st = st_get.clone();
            async move {
                use axum::response::IntoResponse;
                let db = st.db.lock().unwrap();
                match db.assets.iter().find(|a| a["_id"] == id) {
                    Some(a) => axum::Json(a.clone()).into_response(),
                    None => (
                        axum::http::StatusCode::NOT_FOUND,
                        axum::Json(serde_json::json!({"error": "not found"})),
                    )
                        .into_response(),
                }
            }
        })
        .put(
            move |Path(id): Path<String>, Json(body): Json<serde_json::Value>| {
                let st = st_put.clone();
                async move {
                    use axum::response::IntoResponse;
                    let mut db = st.db.lock().unwrap();
                    match db.assets.iter_mut().find(|a| a["_id"] == id) {
                        Some(a) => {
                            if let Some(c) = body.get("content") {
                                a["content"] = c.clone();
                            }
                            if let Some(t) = body.get("title") {
                                a["title"] = t.clone();
                            }
                            if let Some(m) = body.get("metadata").and_then(|m| m.as_object()) {
                                let md = a["metadata"].as_object_mut().unwrap();
                                for (k, v) in m {
                                    md.insert(k.clone(), v.clone());
                                }
                            }
                            a["updated_at"] =
                                serde_json::Value::String("2026-08-31T12:00:00Z".into());
                            axum::Json(a.clone()).into_response()
                        }
                        None => (
                            axum::http::StatusCode::NOT_FOUND,
                            axum::Json(serde_json::json!({"error": "not found"})),
                        )
                            .into_response(),
                    }
                }
            },
        );

        // GET /api/assets/{id}/relations
        let relations_route =
            get(move |Path(_id): Path<String>| async move { axum::Json(serde_json::json!([])) });

        // GET /api/search — compact results, { data: [...] }
        let st = state.clone();
        let search_route = get(
            move |Query(q): Query<std::collections::HashMap<String, String>>| {
                let st = st.clone();
                async move {
                    use axum::response::IntoResponse;
                    let tag = q.get("tag").cloned().unwrap_or_default();
                    let (cat, val) = match tag.split_once(':') {
                        Some((c, v)) => (c.to_string(), v.to_string()),
                        None => (tag.clone(), String::new()),
                    };
                    let db = st.db.lock().unwrap();
                    let data: Vec<serde_json::Value> = db
                        .assets
                        .iter()
                        .filter(|a| {
                            a["tags"].as_array().is_some_and(|tags| {
                                tags.iter().any(|t| {
                                    t["category"] == serde_json::Value::String(cat.clone())
                                        && t["value"] == serde_json::Value::String(val.clone())
                                })
                            })
                        })
                        .map(|a| {
                            serde_json::json!({
                                "_id": a["_id"],
                                "title": a["title"],
                                "tags": a["tags"],
                                "updated_at": a["updated_at"]
                            })
                        })
                        .collect();
                    axum::Json(serde_json::json!({ "data": data })).into_response()
                }
            },
        );

        let app = axum::Router::new()
            .route("/api/assets", create_route)
            .route("/api/assets/{id}", asset_route)
            .route("/api/assets/{id}/relations", relations_route)
            .route("/api/search", search_route);
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        format!("http://{addr}")
    }

    use axum::extract::Query;

    static SEQ: AtomicU32 = AtomicU32::new(0);

    // ------------------------------------------------------------------
    // Shared environment
    // ------------------------------------------------------------------

    struct TestEnv {
        state: AppState,
        base: String,
        ws: String,
        agent: String,
        identity_id: String,
        _proc: Option<PdtProc>,
    }

    async fn test_env() -> TestEnv {
        let (tier, base, proc) = match prebuilt_pdt_bin() {
            Some(bin) => {
                let (base, proc) = spawn_real_pdt(bin).await;
                ("real-pdt", base, Some(proc))
            }
            None => {
                eprintln!(
                    "identity_content_tests: backend = mock-pdt (prebuilt pdt binary not found; \
                     build ../pdt with `cargo build --no-default-features --features \
                     sqlite-backend` to run the real tier)"
                );
                ("mock-pdt", spawn_mock_pdt().await, None)
            }
        };
        let _ = tier;

        let http = reqwest::Client::new();

        // Provisioning (real tier only — the mock has no tenant routing;
        // per-test processes keep mock-tier tests isolated).
        let ws = uuid::Uuid::new_v4().to_string();
        let agent = uuid::Uuid::new_v4().to_string();
        if proc.is_some() {
            for (id, parent) in [(&ws, None), (&agent, Some(&ws))] {
                let resp = http
                    .post(format!("{base}/api/instances/{id}/provision"))
                    .query(&[("parent", parent.map(|p| p.to_string()))])
                    .send()
                    .await
                    .expect("provision request");
                assert!(
                    resp.status().is_success(),
                    "provision {id} failed: {}",
                    resp.status()
                );
            }
        }

        // Real Cedar authorizer over the same embedded policies prod uses.
        let cedar_config: pep::cedar::CedarConfig = crate::config::CedarConfig {
            enabled: true,
            policy_path: "./policies".to_string(),
            schema_path: "./policies/schema.cedarschema".to_string(),
            validate_on_load: true,
            policy_store_url: None,
            policy_store_token: None,
        }
        .into();
        let authorizer = pep::cedar::CedarAuthorizer::new_with_policy_store(cedar_config)
            .await
            .expect("authorizer loads from embedded policies");

        // Real entity configs from the repo's entities/ directory.
        let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("entities");
        let entity_configs: Vec<_> =
            crate::entity_config::load_entity_configs(dir.to_str().unwrap())
                .into_iter()
                .map(Arc::new)
                .collect();
        assert!(entity_configs.iter().any(|c| c.slug == "agent-identity"));

        let config: crate::config::Config =
            toml::from_str(&format!("host = '127.0.0.1'\nport = 0\npdt_url = '{base}'"))
                .expect("minimal test config parses");
        let state = AppState {
            config: Arc::new(config),
            pdt: Arc::new(PdtClient::new(&base)),
            authorizer: Some(Arc::new(authorizer)),
            entity_configs,
        };

        // Seed the identity asset THROUGH fame's own client (same wire as
        // prod), then persist the workspace admin_group anchor the way the
        // create flow does (raw PUT with X-Instance-Id).
        let identity = state
            .pdt
            .for_instance(Some(&agent))
            .create_asset(
                CreateAssetRequest {
                    title: "Identity: Test Agent".to_string(),
                    content: Some(OLD.to_string()),
                    tags: Some(vec![
                        CreateTagRequest {
                            category: "type".to_string(),
                            value: "agent-identity".to_string(),
                        },
                        CreateTagRequest {
                            category: "agent".to_string(),
                            value: agent.clone(),
                        },
                    ]),
                    auth_context: None,
                },
                None,
            )
            .await
            .expect("seed identity via pdt client");
        let resp = http
            .put(format!("{base}/api/assets/{}", identity.id))
            .header("X-Instance-Id", &agent)
            .json(&serde_json::json!({ "metadata": { "admin_group": ADMIN_GROUP } }))
            .send()
            .await
            .expect("metadata patch");
        assert!(resp.status().is_success(), "metadata patch failed");

        TestEnv {
            state,
            base,
            ws,
            agent,
            identity_id: identity.id,
            _proc: proc,
        }
    }

    fn body(content: &str) -> Value {
        serde_json::json!({ "content": content })
    }

    async fn call(
        env: &TestEnv,
        user: &AuthenticatedUser,
        instance: Option<&str>,
        id: &str,
        body: Value,
    ) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
        update_identity_content_inner(&env.state, user, None, instance, "agent-identity", id, body)
            .await
    }

    /// Roundtrip: content lands in place, hashes echo, exactly ONE identity
    /// record exists (no duplicates).
    #[tokio::test]
    async fn roundtrip_updates_in_place_with_hash_echo() {
        let env = test_env().await;
        let resp = call(
            &env,
            &user_with("user", &[ADMIN_GROUP]),
            Some(&env.agent),
            &env.identity_id,
            body(NEW),
        )
        .await
        .expect("scoped user may edit identity content");

        let resp = resp.0;
        assert_eq!(resp["content"], NEW, "content lands");
        assert_eq!(resp["content_hash"]["old"], sha_hex(OLD), "old hash echoes");
        assert_eq!(resp["content_hash"]["new"], sha_hex(NEW), "new hash echoes");

        let inst = env.state.pdt.for_instance(Some(&env.agent));
        let read = inst
            .get_asset(&env.identity_id, None)
            .await
            .expect("identity readable");
        assert_eq!(read.content, NEW);
        let idents = inst
            .search_by_tag("type", "agent-identity", None)
            .await
            .expect("identity search");
        assert_eq!(idents.len(), 1, "zero duplicate identity records");
    }

    /// Unknown identity → loud 404, never create-on-miss.
    #[tokio::test]
    async fn unknown_identity_is_loud_404() {
        let env = test_env().await;
        let err = call(
            &env,
            &user_with("admin", &[]),
            Some(&env.agent),
            "ident-missing",
            body(NEW),
        )
        .await
        .expect_err("unknown id must 404");
        assert_eq!(err.0, StatusCode::NOT_FOUND);
        assert!(
            err.1 .0["error"]
                .as_str()
                .unwrap()
                .contains("Agent identity not found"),
            "loud message: {}",
            err.1 .0
        );
    }

    /// Cedar deny: principal outside the workspace admin_group cannot edit.
    #[tokio::test]
    async fn cedar_denies_principal_outside_admin_group() {
        let env = test_env().await;
        let err = call(
            &env,
            &user_with("user", &["some-other-group"]),
            Some(&env.agent),
            &env.identity_id,
            body(NEW),
        )
        .await
        .expect_err("out-of-group user must be denied");
        assert_eq!(err.0, StatusCode::FORBIDDEN);
        assert_eq!(err.1 .0["error"], "Access denied");
        let asset = env
            .state
            .pdt
            .for_instance(Some(&env.agent))
            .get_asset(&env.identity_id, None)
            .await
            .unwrap();
        assert_eq!(asset.content, OLD, "denied write must not land");
    }

    /// Admin role bypasses group scoping (policy: admin permits all).
    #[tokio::test]
    async fn admin_role_edits_without_group() {
        let env = test_env().await;
        let resp = call(
            &env,
            &user_with("admin", &[]),
            Some(&env.agent),
            &env.identity_id,
            body(NEW),
        )
        .await
        .expect("admin may edit");
        assert_eq!(resp.0["content"], NEW);
    }

    /// GUARD 3 (Phase 1): agent PATCH on its own identity → ALLOW. Prod
    /// shape: tocpi v0.6.3 stamps the identity admin_group = the agent's
    /// mem-* group, so the caller's own group matches by construction.
    #[tokio::test]
    async fn agent_role_edits_own_identity() {
        let env = test_env().await;
        let resp = call(
            &env,
            &user_with("agent", &[ADMIN_GROUP]),
            Some(&env.agent),
            &env.identity_id,
            body("self-written charter"),
        )
        .await
        .expect("agent may edit own identity — admin_group matches its group");
        assert_eq!(resp.0["content"], "self-written charter");
    }

    /// GUARD (v0.3.6, task 3ff56a20): identity create birth-stamps
    /// auth_context from the workspace_admins header — exact claim forms,
    /// team visibility. Regression for the View-deny class (saman 403 on
    /// own charter, 2026-09-02).
    #[tokio::test]
    async fn identity_create_birth_stamps_auth_context() {
        let env = test_env().await;
        let resp = create_entity_inner(
            &env.state,
            &user_with("service", &[]),
            None,
            Some(&env.agent),
            "agent-identity",
            serde_json::json!({"title": "Fresh Charter", "content": NEW}),
            WorkspaceAdmins(Some("mem-fresh@idp.example.com".to_string())),
        )
        .await
        .expect("bootstrap create");
        assert_eq!(resp.0, StatusCode::CREATED);

        // The stamp is ON THE ASSET at birth — zero hand-stamps.
        let id = resp.1 .0["id"].as_str().expect("entity id");
        let asset = env
            .state
            .pdt
            .for_instance(Some(&env.agent))
            .get_asset(id, None)
            .await
            .expect("created identity readable");
        let ac = asset.auth_context.expect("auth_context present at birth");
        assert_eq!(ac.visibility, "team");
        assert_eq!(
            ac.owner_groups,
            vec!["mem-fresh@idp.example.com".to_string()]
        );
        assert_eq!(ac.confidentiality, "");
    }

    /// GUARD (v0.3.6 behavioral): new-agent self-PATCH with ZERO
    /// hand-stamps — create (birth-stamped) → agent edits its own charter
    /// through the scoped permit. The create→edit chain closes.
    #[tokio::test]
    async fn new_agent_self_patch_after_birth_zero_hand_stamps() {
        let env = test_env().await;
        let resp = create_entity_inner(
            &env.state,
            &user_with("service", &[]),
            None,
            Some(&env.agent),
            "agent-identity",
            serde_json::json!({"title": "Fresh Charter", "content": OLD}),
            WorkspaceAdmins(Some("mem-fresh@idp.example.com".to_string())),
        )
        .await
        .expect("bootstrap create");
        let id = resp.1 .0["id"].as_str().expect("entity id").to_string();

        // The owning agent (its mem-* group matches the birth stamp) edits
        // its own charter — no hand-stamps anywhere in between.
        let patched = update_identity_content_inner(
            &env.state,
            &user_with("agent", &["mem-fresh@idp.example.com"]),
            None,
            Some(&env.agent),
            "agent-identity",
            &id,
            serde_json::json!({ "content": NEW }),
        )
        .await
        .expect("self-PATCH allowed on birth-stamped identity");
        assert_eq!(patched.0["content"], NEW);
        assert_eq!(patched.0["content_hash"]["new"], {
            use sha2::Digest;
            format!("sha256:{:x}", sha2::Sha256::digest(NEW.as_bytes()))
        });
    }

    /// GUARD 1 (Phase 1): cross-agent PATCH → DENY VIA CEDAR — the policy
    /// decision itself ("Access denied" from the scoped EditAgentIdentity
    /// permit), NOT a downstream registry-resolution accident. The asset is
    /// fully resolvable here; only the policy says no.
    #[tokio::test]
    async fn agent_edit_cross_identity_denied_via_cedar() {
        let env = test_env().await;
        // Another agent (mem-<other> ≠ the identity's admin_group).
        let err = call(
            &env,
            &user_with("agent", &["mem-someoneelse"]),
            Some(&env.agent),
            &env.identity_id,
            body("hijack attempt"),
        )
        .await
        .expect_err("cross-agent edit must be denied by policy");
        assert_eq!(err.0, StatusCode::FORBIDDEN);
        assert_eq!(
            err.1 .0["error"], "Access denied",
            "Cedar decision, not resolution error"
        );
        let asset = env
            .state
            .pdt
            .for_instance(Some(&env.agent))
            .get_asset(&env.identity_id, None)
            .await
            .unwrap();
        assert_eq!(asset.content, OLD, "denied write must not land");
    }

    /// HARD CONSTRAINT (owner ruling): owner-scope identities stay outside
    /// agent write scope — excluded BY CONSTRUCTION. An agent's groups
    /// contain only its own mem-*; owner identities either carry no
    /// admin_group (unmatchable) or a foreign owner group.
    #[tokio::test]
    async fn owner_stamped_identities_outside_agent_write_scope() {
        let env = test_env().await;

        // Case A: identity with NO admin_group (e.g. pre-Phase-1 charter) —
        // `resource has admin_group` is false, scoped permit can't match.
        let err = call(
            &env,
            &user_with("agent", &["mem-agentown"]),
            Some(&env.agent),
            &env.identity_id,
            body("still denied"),
        )
        .await
        .expect_err("unstamped identity must deny agents");
        assert_eq!(err.0, StatusCode::FORBIDDEN);
        assert_eq!(err.1 .0["error"], "Access denied");

        // Case B: identity stamped with a FOREIGN owner group.
        let owner_asset = env
            .state
            .pdt
            .for_instance(Some(&env.agent))
            .create_asset(
                crate::pdt::CreateAssetRequest {
                    title: "Identity: Owner-scope".to_string(),
                    content: Some(OLD.to_string()),
                    tags: Some(vec![crate::pdt::CreateTagRequest {
                        category: "type".to_string(),
                        value: "agent-identity".to_string(),
                    }]),
                    auth_context: None,
                },
                None,
            )
            .await
            .expect("owner identity seeded");
        let http = reqwest::Client::new();
        let resp = http
            .put(format!("{}/api/assets/{}", env.base, owner_asset.id))
            .header("X-Instance-Id", &env.agent)
            .json(&serde_json::json!({ "metadata": { "admin_group": "farzan-owner-scope" } }))
            .send()
            .await
            .expect("owner stamp");
        assert!(resp.status().is_success());

        let err = call(
            &env,
            &user_with("agent", &["mem-agentown"]),
            Some(&env.agent),
            &owner_asset.id,
            body("hijack"),
        )
        .await
        .expect_err("foreign admin_group must deny agents");
        assert_eq!(err.0, StatusCode::FORBIDDEN);
        assert_eq!(err.1 .0["error"], "Access denied");
    }

    /// Empty content = explicit wipe: allowed, stored empty. The entity
    /// mapper omits empty fields, so verify via hash echo + stored asset.
    #[tokio::test]
    async fn empty_content_is_explicit_wipe() {
        let env = test_env().await;
        let resp = call(
            &env,
            &user_with("user", &[ADMIN_GROUP]),
            Some(&env.agent),
            &env.identity_id,
            body(""),
        )
        .await
        .expect("explicit wipe allowed");
        assert_eq!(resp.0["content_hash"]["new"], sha_hex(""));
        let asset = env
            .state
            .pdt
            .for_instance(Some(&env.agent))
            .get_asset(&env.identity_id, None)
            .await
            .unwrap();
        assert_eq!(asset.content, "", "wipe stored as empty string");
    }

    /// Missing instance header → loud 400: identity content is
    /// instance-scoped; a global-DB lookup would silently miss.
    #[tokio::test]
    async fn missing_instance_header_is_loud_400() {
        let env = test_env().await;
        let err = call(
            &env,
            &user_with("admin", &[]),
            None,
            &env.identity_id,
            body(NEW),
        )
        .await
        .expect_err("missing instance header must 400");
        assert_eq!(err.0, StatusCode::BAD_REQUEST);
        assert!(
            err.1 .0["error"]
                .as_str()
                .unwrap()
                .contains("X-Instance-Id header required"),
            "loud message: {}",
            err.1 .0
        );
    }

    /// Manager-agent bootstrap: role=agent may CREATE a missing identity in
    /// a report agent's home instance (charter provisioning, fame 0.3.2).
    /// This is the exact Farzan→Saman/Ravand/Bonyan charter flow.
    #[tokio::test]
    async fn agent_role_creates_missing_identity_allowed() {
        let env = test_env().await;
        let report_instance = uuid::Uuid::new_v4().to_string();
        let new_id = uuid::Uuid::new_v4().to_string();
        let resp = create_entity_inner(
            &env.state,
            &user_with("agent", &[]),
            None,
            Some(&report_instance),
            "agent-identity",
            serde_json::json!({"title": "Provisioned identity (bootstrap)", "content": NEW}),
            WorkspaceAdmins(None),
        )
        .await
        .expect("manager agent may bootstrap a report identity");
        assert_eq!(resp.0, StatusCode::CREATED);
        assert_eq!(resp.1 .0["content"], NEW);
    }

    /// Tocpi's service principal may bootstrap identity content too
    /// (control-plane provisioning on behalf of the platform).
    #[tokio::test]
    async fn service_role_creates_missing_identity_allowed() {
        let env = test_env().await;
        let report_instance = uuid::Uuid::new_v4().to_string();
        let new_id = uuid::Uuid::new_v4().to_string();
        let resp = create_entity_inner(
            &env.state,
            &user_with("service", &[]),
            None,
            Some(&report_instance),
            "agent-identity",
            serde_json::json!({"title": "Provisioned identity (bootstrap)", "content": NEW}),
            WorkspaceAdmins(None),
        )
        .await
        .expect("service principal may bootstrap identity");
        assert_eq!(resp.0, StatusCode::CREATED);
        assert_eq!(resp.1 .0["content"], NEW);
    }
}