cpex-hosts-python 0.3.1

CPEX host — runs Python plugins out-of-process in isolated virtualenvs.
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
// Location: ./crates/cpex-hosts-python/src/legacy/cmf_shim.rs
// Copyright 2026
// SPDX-License-Identifier: Apache-2.0
// Authors: Ted Habeck
//
// Payload shim between PPE's `cmf.*` hooks and the legacy typed hooks.
//
// # Why this exists
//
// PPE dispatches tool, prompt and resource traffic only on `cmf.*` hooks, with
// a CMF `MessagePayload`. Python workers that predate `cmf.*` hook support
// (the released 0.1.x SDK) register no `cmf.*` hook in the worker, and their
// plugins implement `tool_pre_invoke` and friends against the typed payloads
// (`ToolPreInvokePayload` and so on). Without this module such a plugin cannot
// run on PPE at all.
//
// So, for a worker that does not claim the `cmf_hooks` handshake feature, the
// adapter routes each of the six tool, prompt and resource `cmf.*` hooks
// through here:
//
//   outbound: the CMF message ──> the legacy typed payload + legacy hook name
//   inbound:  a modified legacy payload ──> the same CMF message with only the
//             fields the legacy payload carries replaced
//
// Allow, deny and violations pass through untouched; only `modified_payload`
// is translated. A message the legacy payload cannot represent fails closed
// with `HostError::LegacyShim`, never a silent allow.
//
// # Field decisions
//
// Every field of the CMF parts the shim reads. "sent" fields reach the plugin
// and are mapped back. "kept" fields are not sent and survive unchanged, which
// is safe because they neither qualify what is authorized nor carry content a
// plugin could need to rewrite. "refuse" fields are security-relevant and have
// no legacy slot, so a message that sets them fails closed.
//
//   part              field                decision
//   ----------------  -------------------  -------------------------------------
//   tool_call         tool_call_id         kept: correlation only
//                     name, arguments      sent
//                     namespace            refuse when non-empty: a plugin that
//                                          authorizes by bare name would approve
//                                          the same name in any namespace
//   tool_result       tool_call_id         kept: correlation only
//                     tool_name, content   sent (structured content travels
//                                          inside `content` as is)
//                     is_error             refuse when true unless `content`
//                                          carries `"isError": true`; a modified
//                                          result must keep it, and one that
//                                          sets it sets the flag
//   prompt_request    prompt_request_id    kept: correlation only
//                     name, arguments      sent
//                     server_id            refuse when non-empty: same reason as
//                                          tool namespace
//   prompt_result     prompt_request_id    kept: correlation only
//                     prompt_name          sent
//                     messages             sent (role and text; per-message
//                                          `schema_version` and `channel` kept)
//                     content              refuse when non-empty: rendered text
//                                          the plugin cannot see or redact
//                     is_error             refuse when true: no legacy slot
//                     error_message        refuse when non-empty: same as content
//   resource          resource_request_id  sent on post-fetch as `id`, else kept
//                     uri                  sent
//                     name, description    kept: descriptive labels
//                     resource_type        kept: the URI is the address
//                     annotations          kept: labels that only restrict
//                                          downstream, never widen
//                     version              refuse when non-empty: selects which
//                                          revision the URI resolves to
//                     content              sent on post-fetch as `text`; on
//                                          pre-fetch a URI rewrite refuses if set
//                     blob                 refuse when set
//                     mime_type            sent on post-fetch; on pre-fetch a
//                                          URI rewrite refuses if set
//                     size_bytes           not sent; recomputed when post-fetch
//                                          text changes; on pre-fetch a URI
//                                          rewrite refuses if set
//   resource_ref      resource_request_id  kept: correlation only
//                     uri                  sent
//                     name, resource_type  kept: descriptive
//                     range_start/end,     kept: they narrow what is read within
//                     selector             the URI the plugin approved
//   any other part    (the whole part)     post hooks: refuse unless it is a
//                                          text or thinking part with no text.
//                                          The plugin never sees it, so a PII
//                                          filter would pass result content
//                                          through unredacted.
//                                          pre hooks: kept. The plugin
//                                          authorizes the call, request or URI
//                                          it sees, and narration beside it
//                                          goes back unchanged. A worker that
//                                          claims `cmf_hooks` receives the
//                                          whole message instead
//
// # Removing it
//
// Once every supported worker claims `cmf_hooks`, delete this file, its
// `pub mod` line in `legacy/mod.rs`, and the `shim` branch in
// `PythonHookAdapter::invoke` and `build_task`. Nothing else depends on it.

use std::collections::HashMap;

use ppe::praxis_policy_core::cmf::constants::{
    HOOK_CMF_LLM_INPUT, HOOK_CMF_LLM_OUTPUT, HOOK_CMF_PROMPT_POST_INVOKE,
    HOOK_CMF_PROMPT_PRE_INVOKE, HOOK_CMF_RESOURCE_POST_FETCH, HOOK_CMF_RESOURCE_PRE_FETCH,
    HOOK_CMF_TOOL_POST_INVOKE, HOOK_CMF_TOOL_PRE_INVOKE,
};
use ppe::praxis_policy_core::cmf::{ContentPart, Message, MessagePayload, Resource, Role};
use ppe::praxis_policy_core::hooks::payload::PluginPayload;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;

use super::payloads::{
    PromptPostFetchPayload, PromptPreFetchPayload, ResourcePostFetchPayload,
    ResourcePreFetchPayload, ToolPostInvokePayload, ToolPreInvokePayload,
};
use crate::error::HostError;

/// The six `cmf.*` hooks the shim translates, and the legacy hook each one
/// reaches the worker as.
const SHIMMED_HOOKS: &[(&str, Shim)] = &[
    (HOOK_CMF_TOOL_PRE_INVOKE, Shim::ToolPreInvoke),
    (HOOK_CMF_TOOL_POST_INVOKE, Shim::ToolPostInvoke),
    (HOOK_CMF_PROMPT_PRE_INVOKE, Shim::PromptPreFetch),
    (HOOK_CMF_PROMPT_POST_INVOKE, Shim::PromptPostFetch),
    (HOOK_CMF_RESOURCE_PRE_FETCH, Shim::ResourcePreFetch),
    (HOOK_CMF_RESOURCE_POST_FETCH, Shim::ResourcePostFetch),
];

/// Which legacy hook a shimmed `cmf.*` hook maps to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Shim {
    ToolPreInvoke,
    ToolPostInvoke,
    PromptPreFetch,
    PromptPostFetch,
    ResourcePreFetch,
    ResourcePostFetch,
}

impl Shim {
    fn for_hook(hook_name: &str) -> Option<Self> {
        SHIMMED_HOOKS
            .iter()
            .find(|(name, _)| *name == hook_name)
            .map(|(_, shim)| *shim)
    }

    /// The legacy hook name: the `hook_type` the worker dispatches on and the
    /// method name a 0.1.x plugin implements.
    fn legacy_hook(self) -> &'static str {
        match self {
            Self::ToolPreInvoke => "tool_pre_invoke",
            Self::ToolPostInvoke => "tool_post_invoke",
            Self::PromptPreFetch => "prompt_pre_fetch",
            Self::PromptPostFetch => "prompt_post_fetch",
            Self::ResourcePreFetch => "resource_pre_fetch",
            Self::ResourcePostFetch => "resource_post_fetch",
        }
    }

    /// Whether this is a post hook, whose result content a plugin may need to
    /// inspect or redact in full.
    fn is_post(self) -> bool {
        matches!(
            self,
            Self::ToolPostInvoke | Self::PromptPostFetch | Self::ResourcePostFetch
        )
    }

    /// The CMF content part the legacy payload is built from, for diagnostics.
    fn part_name(self) -> &'static str {
        match self {
            Self::ToolPreInvoke => "tool_call",
            Self::ToolPostInvoke => "tool_result",
            Self::PromptPreFetch => "prompt_request",
            Self::PromptPostFetch => "prompt_result",
            Self::ResourcePreFetch => "resource or resource_ref",
            Self::ResourcePostFetch => "resource",
        }
    }

    /// Whether `part` is the kind this hook's legacy payload is built from.
    fn matches(self, part: &ContentPart) -> bool {
        match self {
            Self::ToolPreInvoke => matches!(part, ContentPart::ToolCall { .. }),
            Self::ToolPostInvoke => matches!(part, ContentPart::ToolResult { .. }),
            Self::PromptPreFetch => matches!(part, ContentPart::PromptRequest { .. }),
            Self::PromptPostFetch => matches!(part, ContentPart::PromptResult { .. }),
            // A pre-fetch only needs the URI, which both resource parts carry.
            // A post-fetch needs the content, which only `resource` carries; a
            // `resource_ref` there is counted so it fails with a clear reason
            // rather than as "no resource part".
            Self::ResourcePreFetch | Self::ResourcePostFetch => matches!(
                part,
                ContentPart::Resource { .. } | ContentPart::ResourceRef { .. }
            ),
        }
    }
}

/// Whether `hook_name` is a `cmf.*` hook PPE dispatches.
///
/// Every one of these needs either a worker that claims `cmf_hooks` or the
/// shim. The two LLM hooks are included: they have no legacy equivalent, so a
/// worker without `cmf_hooks` cannot serve them and [`to_legacy`] refuses.
pub fn is_cmf_hook(hook_name: &str) -> bool {
    Shim::for_hook(hook_name).is_some()
        || hook_name == HOOK_CMF_LLM_INPUT
        || hook_name == HOOK_CMF_LLM_OUTPUT
}

/// The legacy hook name a shimmed `cmf.*` hook reaches the worker as, or
/// `None` for any other name.
pub fn legacy_hook_name(hook_name: &str) -> Option<&'static str> {
    Shim::for_hook(hook_name).map(Shim::legacy_hook)
}

/// One translated invocation: the legacy payload to send, plus what is needed
/// to map a modified legacy payload back onto the original message.
#[derive(Debug)]
pub struct ShimmedPayload {
    shim: Shim,
    hook_name: String,
    /// The message as dispatched. A modified payload is applied to a clone of
    /// it, so every part and field the legacy payload does not carry survives.
    original: MessagePayload,
    /// Index of the content part the legacy payload was built from.
    part: usize,
    json: Value,
}

impl ShimmedPayload {
    /// The legacy hook name to send as the task's `hook_type`.
    pub fn legacy_hook(&self) -> &'static str {
        self.shim.legacy_hook()
    }

    /// The legacy payload JSON, in the shape the worker's `json_to_payload`
    /// rebuilds into the matching Pydantic model.
    pub fn payload(&self) -> &Value {
        &self.json
    }

    /// Map a worker's modified legacy payload back onto the original message.
    ///
    /// Only the fields the legacy payload carries are written. A modification
    /// the message has nowhere to hold (tool headers, resource metadata, a
    /// prompt result description, a resource blob) fails closed: dropping it
    /// would silently undo what the plugin asked for.
    pub fn apply_modified(&self, modified: Value) -> Result<MessagePayload, HostError> {
        let mut payload = self.original.clone();
        let part = payload
            .message
            .content
            .get_mut(self.part)
            .ok_or_else(|| self.error("the original content part is gone"))?;

        match (self.shim, part) {
            (Shim::ToolPreInvoke, ContentPart::ToolCall { content }) => {
                let legacy: ToolPreInvokePayload = self.parse(modified)?;
                if legacy.headers.as_ref().is_some_and(|h| !h.is_empty()) {
                    return Err(self.error(
                        "the plugin returned `headers`, which a CMF tool call cannot carry",
                    ));
                }
                content.name = legacy.name;
                content.arguments = legacy.args.unwrap_or_default();
            },
            (Shim::ToolPostInvoke, ContentPart::ToolResult { content }) => {
                let legacy: ToolPostInvokePayload = self.parse(modified)?;
                if content.is_error && !result_says_error(&legacy.result) {
                    return Err(self.error(
                        "the plugin cleared `isError` on an error tool result, and the CMF \
                         `is_error` flag is not mapped back",
                    ));
                }
                // A plugin may turn a success into an error; the CMF flag
                // follows, so downstream reads the error the plugin set.
                content.is_error = content.is_error || result_says_error(&legacy.result);
                content.tool_name = legacy.name;
                content.content = legacy.result;
            },
            (Shim::PromptPreFetch, ContentPart::PromptRequest { content }) => {
                let legacy: PromptPreFetchPayload = self.parse(modified)?;
                content.name = legacy.prompt_id;
                content.arguments = legacy
                    .args
                    .unwrap_or_default()
                    .into_iter()
                    .map(|(k, v)| (k, Value::String(v)))
                    .collect();
            },
            (Shim::PromptPostFetch, ContentPart::PromptResult { content }) => {
                let legacy: PromptPostFetchPayload = self.parse(modified)?;
                content.messages = self.messages_from_legacy(legacy.result, &content.messages)?;
                content.prompt_name = legacy.prompt_id;
            },
            (Shim::ResourcePreFetch, ContentPart::Resource { content }) => {
                let uri = self.resource_pre_uri(modified)?;
                // Embedded content, its type and its size describe the
                // original URI. Carrying them onto a rewritten URI would serve
                // the old content under the address the plugin chose.
                if uri != content.uri
                    && (content.is_embedded()
                        || content.mime_type.is_some()
                        || content.size_bytes.is_some())
                {
                    return Err(self.error(
                        "the plugin rewrote the URI of a resource that carries content, \
                         mime_type or size_bytes from the original URI",
                    ));
                }
                content.uri = uri;
            },
            (Shim::ResourcePreFetch, ContentPart::ResourceRef { content }) => {
                content.uri = self.resource_pre_uri(modified)?;
            },
            (Shim::ResourcePostFetch, ContentPart::Resource { content }) => {
                let legacy: ResourcePostFetchPayload = self.parse(modified)?;
                let before = content.content.clone();
                let original_uri = std::mem::replace(&mut content.uri, legacy.uri);
                match legacy.content {
                    Value::Null => content.content = None,
                    Value::String(text) => content.content = Some(text),
                    Value::Object(fields) => {
                        if fields.get("blob").is_some_and(|b| !b.is_null()) {
                            return Err(self.error(
                                "the plugin returned resource `content.blob`; binary resource \
                                 content is not carried by the shim",
                            ));
                        }
                        // `content.id` and `content.uri` have no slot of their
                        // own: the part keeps one id and one URI. A change to
                        // either would otherwise be dropped.
                        if fields.get("id").is_some_and(|id| {
                            id.as_str() != Some(content.resource_request_id.as_str())
                        }) {
                            return Err(self.error(
                                "the plugin changed `content.id`, which is not mapped back",
                            ));
                        }
                        if fields.get("uri").is_some_and(|u| {
                            u.as_str() != Some(content.uri.as_str())
                                && u.as_str() != Some(original_uri.as_str())
                        }) {
                            return Err(self.error(
                                "the plugin changed `content.uri` to a value other than `uri`, \
                                 and only `uri` is mapped back",
                            ));
                        }
                        content.content = match fields.get("text") {
                            None | Some(Value::Null) => None,
                            Some(Value::String(text)) => Some(text.clone()),
                            Some(_) => {
                                return Err(self.error("`content.text` must be a string or null"));
                            },
                        };
                        if let Some(mime) = fields.get("mime_type") {
                            content.mime_type = match mime {
                                Value::Null => None,
                                Value::String(m) => Some(m.clone()),
                                _ => {
                                    return Err(
                                        self.error("`content.mime_type` must be a string or null")
                                    );
                                },
                            };
                        }
                    },
                    _ => return Err(self.error("`content` must be an object, a string, or null")),
                }
                // A size measured from the original text would misstate the
                // text the plugin returned.
                if content.content != before && content.size_bytes.is_some() {
                    content.size_bytes = content.content.as_ref().map(|t| t.len() as u64);
                }
            },
            _ => return Err(self.error("the original content part changed kind")),
        }

        Ok(payload)
    }

    fn resource_pre_uri(&self, modified: Value) -> Result<String, HostError> {
        let legacy: ResourcePreFetchPayload = self.parse(modified)?;
        if legacy.metadata.as_ref().is_some_and(|m| !m.is_empty()) {
            return Err(self.error(
                "the plugin returned resource `metadata`, which a CMF resource part cannot carry",
            ));
        }
        Ok(legacy.uri)
    }

    /// Rebuild CMF prompt messages from a legacy `PromptResult` shape.
    ///
    /// A message keeps its original `schema_version` and `channel` when one
    /// exists at the same position; a message the plugin appended gets the
    /// defaults.
    fn messages_from_legacy(
        &self,
        result: Value,
        original: &[Message],
    ) -> Result<Vec<Message>, HostError> {
        let Value::Object(result) = result else {
            return Err(self.error("`result` must be an object with a `messages` list"));
        };
        if result.get("description").is_some_and(|d| !d.is_null()) {
            return Err(self.error(
                "the plugin returned a prompt result `description`, which a CMF prompt result \
                 cannot carry",
            ));
        }
        let Some(Value::Array(items)) = result.get("messages") else {
            return Err(self.error("`result.messages` must be a list"));
        };

        items
            .iter()
            .enumerate()
            .map(|(i, item)| {
                let role = item
                    .get("role")
                    .cloned()
                    .and_then(|r| serde_json::from_value::<Role>(r).ok())
                    .ok_or_else(|| {
                        self.error(&format!("`result.messages[{i}].role` is not a CMF role"))
                    })?;
                let text = match item.get("content") {
                    Some(Value::Object(c))
                        if c.get("type").and_then(Value::as_str) == Some("text") =>
                    {
                        c.get("text").and_then(Value::as_str)
                    },
                    _ => None,
                }
                .ok_or_else(|| {
                    self.error(&format!(
                        "`result.messages[{i}].content` must be text content \
                         ({{\"type\": \"text\", \"text\": ...}})"
                    ))
                })?;

                let mut message = original
                    .get(i)
                    .cloned()
                    .unwrap_or_else(|| Message::text(role, ""));
                message.role = role;
                message.content = vec![ContentPart::Text {
                    text: text.to_string(),
                }];
                Ok(message)
            })
            .collect()
    }

    fn parse<T: DeserializeOwned>(&self, modified: Value) -> Result<T, HostError> {
        serde_json::from_value(modified).map_err(|e| HostError::Protocol {
            message: format!(
                "worker returned a modified payload that does not match the legacy hook '{}' \
                 (dispatched as '{}'): {e}",
                self.shim.legacy_hook(),
                self.hook_name
            ),
        })
    }

    fn error(&self, message: &str) -> HostError {
        shim_error(&self.hook_name, self.shim.legacy_hook(), message)
    }
}

fn shim_error(hook_name: &str, legacy: &str, message: &str) -> HostError {
    HostError::LegacyShim {
        hook: hook_name.to_string(),
        message: format!("translating to the legacy '{legacy}' payload: {message}"),
    }
}

/// Translate a `cmf.*` hook invocation into the legacy hook a worker without
/// `cmf_hooks` support understands.
///
/// Fails closed for the LLM hooks (no legacy equivalent), for a payload that
/// is not a `MessagePayload`, and for a message that does not hold exactly one
/// content part of the kind the hook's legacy payload is built from.
pub fn to_legacy(
    hook_name: &str,
    payload: &dyn PluginPayload,
) -> Result<ShimmedPayload, HostError> {
    let Some(shim) = Shim::for_hook(hook_name) else {
        return Err(HostError::LegacyShim {
            hook: hook_name.to_string(),
            message: "it has no legacy hook equivalent, so only a worker that claims the \
                      `cmf_hooks` feature can run it. Upgrade the cpex package in the plugin's \
                      venv, or remove the hook from the plugin's `hooks:` list."
                .into(),
        });
    };
    let legacy = shim.legacy_hook();

    let original = payload
        .as_any()
        .downcast_ref::<MessagePayload>()
        .ok_or_else(|| shim_error(hook_name, legacy, "the payload is not a CMF MessagePayload"))?
        .clone();

    let candidates: Vec<usize> = original
        .message
        .content
        .iter()
        .enumerate()
        .filter(|(_, p)| shim.matches(p))
        .map(|(i, _)| i)
        .collect();
    let part = match candidates.as_slice() {
        [only] => *only,
        [] => {
            return Err(shim_error(
                hook_name,
                legacy,
                &format!("the message has no {} content part", shim.part_name()),
            ));
        },
        many => {
            return Err(shim_error(
                hook_name,
                legacy,
                &format!(
                    "the message has {} {} content parts and the legacy payload holds one",
                    many.len(),
                    shim.part_name()
                ),
            ));
        },
    };

    // The legacy payload holds the one part. On a post hook anything else in
    // the message would go downstream on allow without the plugin having seen
    // it. On a pre hook the plugin authorizes the part it sees, and siblings
    // are returned unchanged.
    if let Some(sibling) = original
        .message
        .content
        .iter()
        .enumerate()
        .find(|(i, p)| shim.is_post() && *i != part && carries_content(p))
        .map(|(_, p)| p)
    {
        return Err(shim_error(
            hook_name,
            legacy,
            &format!(
                "the message holds a {} content part beside the {} part, which the legacy \
                 payload cannot carry, so a plugin could not inspect or redact it",
                part_kind(sibling),
                shim.part_name()
            ),
        ));
    }

    let err = |message: &str| shim_error(hook_name, legacy, message);
    let json = match (shim, &original.message.content[part]) {
        (Shim::ToolPreInvoke, ContentPart::ToolCall { content }) => {
            if is_set(&content.namespace) {
                return Err(err(
                    "the tool call has a `namespace`, which the legacy payload cannot carry; \
                     a plugin that authorizes by bare `name` would approve it in any namespace",
                ));
            }
            to_json(
                &ToolPreInvokePayload {
                    name: content.name.clone(),
                    args: Some(content.arguments.clone()),
                    headers: None,
                },
                &err,
            )?
        },
        (Shim::ToolPostInvoke, ContentPart::ToolResult { content }) => {
            if content.is_error && !result_says_error(&content.content) {
                return Err(err(
                    "the tool result has `is_error` set, and its `content` does not carry \
                     `\"isError\": true` for the legacy plugin to see",
                ));
            }
            to_json(
                &ToolPostInvokePayload {
                    name: content.tool_name.clone(),
                    result: content.content.clone(),
                },
                &err,
            )?
        },
        (Shim::PromptPreFetch, ContentPart::PromptRequest { content }) => {
            if is_set(&content.server_id) {
                return Err(err(
                    "the prompt request has a `server_id`, which the legacy payload cannot \
                     carry; a plugin that authorizes by bare `prompt_id` would approve it on \
                     any server",
                ));
            }
            // dict[str, str] on the Python side, which Pydantic will not coerce
            // a number or an object into.
            let mut args = HashMap::with_capacity(content.arguments.len());
            for (key, value) in &content.arguments {
                let Value::String(s) = value else {
                    return Err(err(&format!(
                        "prompt argument '{key}' is not a string, and the legacy payload's \
                         `args` is dict[str, str]"
                    )));
                };
                args.insert(key.clone(), s.clone());
            }
            to_json(
                &PromptPreFetchPayload {
                    prompt_id: content.name.clone(),
                    args: Some(args),
                },
                &err,
            )?
        },
        (Shim::PromptPostFetch, ContentPart::PromptResult { content }) => {
            // The legacy result holds only messages. Text left in these fields
            // would survive a plugin that redacted the messages.
            if is_set(&content.content) {
                return Err(err(
                    "the prompt result has rendered `content`, which the legacy payload cannot \
                     carry, so a plugin could not inspect or redact it",
                ));
            }
            if is_set(&content.error_message) {
                return Err(err(
                    "the prompt result has an `error_message`, which the legacy payload cannot \
                     carry, so a plugin could not inspect or redact it",
                ));
            }
            if content.is_error {
                return Err(err(
                    "the prompt result has `is_error` set, which the legacy payload cannot carry",
                ));
            }
            let mut messages = Vec::with_capacity(content.messages.len());
            for (i, message) in content.messages.iter().enumerate() {
                let [ContentPart::Text { text }] = message.content.as_slice() else {
                    return Err(err(&format!(
                        "prompt result message {i} is not a single text part, and the legacy \
                         prompt message holds one text content"
                    )));
                };
                messages.push(serde_json::json!({
                    "role": message.role,
                    "content": { "type": "text", "text": text },
                }));
            }
            to_json(
                &PromptPostFetchPayload {
                    prompt_id: content.prompt_name.clone(),
                    result: serde_json::json!({ "messages": messages, "description": null }),
                },
                &err,
            )?
        },
        (Shim::ResourcePreFetch, ContentPart::Resource { content }) => {
            refuse_resource_version(content, &err)?;
            to_json(
                &ResourcePreFetchPayload {
                    uri: content.uri.clone(),
                    metadata: None,
                },
                &err,
            )?
        },
        (Shim::ResourcePreFetch, ContentPart::ResourceRef { content }) => to_json(
            &ResourcePreFetchPayload {
                uri: content.uri.clone(),
                metadata: None,
            },
            &err,
        )?,
        (Shim::ResourcePostFetch, ContentPart::Resource { content }) => {
            refuse_resource_version(content, &err)?;
            if content.blob.is_some() {
                return Err(err(
                    "the resource carries binary `blob` content, which the shim does not \
                     translate",
                ));
            }
            // The legacy `ResourceContent` shape 0.1.x plugins read.
            to_json(
                &ResourcePostFetchPayload {
                    uri: content.uri.clone(),
                    content: serde_json::json!({
                        "type": "resource",
                        "id": content.resource_request_id,
                        "uri": content.uri,
                        "mime_type": content.mime_type,
                        "text": content.content,
                    }),
                },
                &err,
            )?
        },
        (Shim::ResourcePostFetch, ContentPart::ResourceRef { .. }) => {
            return Err(err(
                "the message holds a resource_ref with no content, and the legacy post-fetch \
                 payload carries the content",
            ));
        },
        // `candidates` only holds parts `shim.matches` accepted, and every
        // accepted pairing is handled above.
        _ => return Err(err("unexpected content part")),
    };

    Ok(ShimmedPayload {
        shim,
        hook_name: hook_name.to_string(),
        original,
        part,
        json,
    })
}

/// Whether an optional string field holds a value. An empty string carries
/// nothing and counts as unset.
fn is_set(field: &Option<String>) -> bool {
    field.as_deref().is_some_and(|s| !s.is_empty())
}

/// Whether a part the legacy payload does not carry holds anything a plugin
/// would need to see. Only a text or thinking part with no text holds nothing.
fn carries_content(part: &ContentPart) -> bool {
    match part {
        ContentPart::Text { text } | ContentPart::Thinking { text } => !text.trim().is_empty(),
        _ => true,
    }
}

/// A content part's `content_type`, for diagnostics.
fn part_kind(part: &ContentPart) -> &'static str {
    match part {
        ContentPart::Text { .. } => "text",
        ContentPart::Thinking { .. } => "thinking",
        ContentPart::ToolCall { .. } => "tool_call",
        ContentPart::ToolResult { .. } => "tool_result",
        ContentPart::Resource { .. } => "resource",
        ContentPart::ResourceRef { .. } => "resource_ref",
        ContentPart::PromptRequest { .. } => "prompt_request",
        ContentPart::PromptResult { .. } => "prompt_result",
        ContentPart::Image { .. } => "image",
        ContentPart::Video { .. } => "video",
        ContentPart::Audio { .. } => "audio",
        ContentPart::Document { .. } => "document",
    }
}

/// Whether a legacy tool result tells the plugin it is an error, the MCP
/// `CallToolResult` way.
fn result_says_error(result: &Value) -> bool {
    result.get("isError") == Some(&Value::Bool(true))
}

fn refuse_resource_version(
    resource: &Resource,
    err: &dyn Fn(&str) -> HostError,
) -> Result<(), HostError> {
    if is_set(&resource.version) {
        return Err(err(
            "the resource has a `version`, which the legacy payload cannot carry; a plugin \
             that authorizes by `uri` would approve any revision",
        ));
    }
    Ok(())
}

fn to_json<T: Serialize>(value: &T, err: &dyn Fn(&str) -> HostError) -> Result<Value, HostError> {
    serde_json::to_value(value).map_err(|e| err(&format!("could not serialize: {e}")))
}

#[cfg(test)]
mod tests {
    use ppe::praxis_policy_core::cmf::{
        Channel, PromptRequest, PromptResult, Resource, ResourceReference, ResourceType, ToolCall,
        ToolResult,
    };
    use serde_json::json;

    use super::*;

    // --- fixtures -----------------------------------------------------------

    /// A text part that carries text.
    fn context_text() -> ContentPart {
        ContentPart::Text {
            text: "unrelated context".into(),
        }
    }

    /// A message with the target part between two empty parts: the only
    /// siblings the shim accepts, so every round trip can assert they survive.
    fn message_around(part: ContentPart) -> MessagePayload {
        let mut message = Message::with_content(
            Role::Assistant,
            vec![
                ContentPart::Text {
                    text: String::new(),
                },
                part,
                ContentPart::Thinking { text: " ".into() },
            ],
        );
        message.channel = Some(Channel::Commentary);
        MessagePayload { message }
    }

    fn tool_call(args: Value) -> ContentPart {
        ContentPart::ToolCall {
            content: ToolCall {
                tool_call_id: "call-1".into(),
                name: "search".into(),
                arguments: serde_json::from_value(args).unwrap(),
                namespace: None,
            },
        }
    }

    fn tool_result(content: Value) -> ContentPart {
        ContentPart::ToolResult {
            content: ToolResult {
                tool_call_id: "call-1".into(),
                tool_name: "search".into(),
                content,
                is_error: false,
            },
        }
    }

    fn prompt_request(args: Value) -> ContentPart {
        ContentPart::PromptRequest {
            content: PromptRequest {
                prompt_request_id: "pr-1".into(),
                name: "greeting".into(),
                arguments: serde_json::from_value(args).unwrap(),
                server_id: None,
            },
        }
    }

    fn prompt_result(messages: Vec<Message>) -> ContentPart {
        ContentPart::PromptResult {
            content: PromptResult {
                prompt_request_id: "pr-1".into(),
                prompt_name: "greeting".into(),
                messages,
                content: None,
                is_error: false,
                error_message: None,
            },
        }
    }

    fn resource(text: Option<&str>) -> Resource {
        Resource {
            resource_request_id: "res-1".into(),
            uri: "file:///tmp/a.txt".into(),
            name: Some("a".into()),
            description: None,
            resource_type: ResourceType::File,
            content: text.map(str::to_string),
            blob: None,
            mime_type: Some("text/plain".into()),
            size_bytes: Some(5),
            annotations: HashMap::from([("k".into(), json!("v"))]),
            version: None,
        }
    }

    fn resource_ref() -> ContentPart {
        ContentPart::ResourceRef {
            content: ResourceReference {
                resource_request_id: "res-1".into(),
                uri: "file:///tmp/a.txt".into(),
                name: None,
                resource_type: ResourceType::File,
                range_start: Some(1),
                range_end: None,
                selector: None,
            },
        }
    }

    fn json_of(payload: &MessagePayload) -> Value {
        serde_json::to_value(payload).unwrap()
    }

    /// Assert the parts around index 1 and the message-level fields are what
    /// `message_around` built.
    fn assert_surroundings_kept(payload: &MessagePayload) {
        let json = json_of(payload);
        assert_eq!(
            json["message"]["content"][0],
            json!({"content_type": "text", "text": ""})
        );
        assert_eq!(
            json["message"]["content"][2],
            json!({"content_type": "thinking", "text": " "})
        );
        assert_eq!(json["message"]["role"], "assistant");
        assert_eq!(json["message"]["channel"], "commentary");
        assert_eq!(payload.message.content.len(), 3);
    }

    /// The JSON the target part serializes to, for a whole-part comparison.
    fn part_json(payload: &MessagePayload) -> Value {
        json_of(payload)["message"]["content"][1].clone()
    }

    fn assert_shim_error(err: HostError, hook: &str, needle: &str) {
        let text = err.to_string();
        assert!(matches!(err, HostError::LegacyShim { .. }), "got {err:?}");
        assert!(text.contains(hook), "the error must name the hook: {text}");
        assert!(text.contains(needle), "expected '{needle}' in: {text}");
    }

    // --- hook table ---------------------------------------------------------

    #[test]
    fn the_six_tool_prompt_and_resource_hooks_map_to_legacy_names() {
        for (cmf, legacy) in [
            ("cmf.tool_pre_invoke", "tool_pre_invoke"),
            ("cmf.tool_post_invoke", "tool_post_invoke"),
            ("cmf.prompt_pre_invoke", "prompt_pre_fetch"),
            ("cmf.prompt_post_invoke", "prompt_post_fetch"),
            ("cmf.resource_pre_fetch", "resource_pre_fetch"),
            ("cmf.resource_post_fetch", "resource_post_fetch"),
        ] {
            assert_eq!(legacy_hook_name(cmf), Some(legacy), "{cmf}");
            assert!(is_cmf_hook(cmf));
        }
    }

    #[test]
    fn every_cmf_hook_ppe_dispatches_is_covered() {
        // A new upstream cmf hook must be either shimmed or refused, never
        // forwarded under a name the old worker does not know.
        for hook in ppe::praxis_policy_core::hooks::builtin_hook_types() {
            let name = hook.as_str();
            if name.starts_with("cmf.") {
                assert!(
                    is_cmf_hook(name),
                    "'{name}' is not covered by the shim table"
                );
            }
        }
    }

    #[test]
    fn non_cmf_hooks_are_not_shimmed() {
        for hook in [
            "tool_pre_invoke",
            "identity.resolve",
            "token.delegate",
            "cmf.custom",
        ] {
            assert!(!is_cmf_hook(hook), "{hook}");
            assert_eq!(legacy_hook_name(hook), None, "{hook}");
        }
    }

    // --- cmf.tool_pre_invoke ------------------------------------------------

    #[test]
    fn tool_pre_invoke_sends_name_and_args() {
        let payload = message_around(tool_call(json!({"q": "rust", "n": 3})));
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();

        assert_eq!(shimmed.legacy_hook(), "tool_pre_invoke");
        assert_eq!(
            shimmed.payload(),
            &json!({"name": "search", "args": {"q": "rust", "n": 3}})
        );
    }

    #[test]
    fn tool_pre_invoke_unchanged_round_trip_preserves_the_message() {
        let payload = message_around(tool_call(json!({"q": "rust"})));
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();

        // What the 0.1.x worker's `model_dump(mode="json")` returns for an
        // unmodified payload: every field, `headers` as null.
        let back = shimmed
            .apply_modified(json!({"name": "search", "args": {"q": "rust"}, "headers": null}))
            .unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn tool_pre_invoke_modified_args_map_back() {
        let payload = message_around(tool_call(json!({"q": "alice@example.com"})));
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();

        let back = shimmed
            .apply_modified(
                json!({"name": "search", "args": {"q": "[REDACTED]", "extra": 1}, "headers": {}}),
            )
            .unwrap();
        assert_eq!(
            part_json(&back),
            json!({"content_type": "tool_call", "content": {
                "tool_call_id": "call-1",
                "name": "search",
                "arguments": {"q": "[REDACTED]", "extra": 1},
            }})
        );
        assert_surroundings_kept(&back);
    }

    #[test]
    fn tool_pre_invoke_returned_headers_fail_closed() {
        let payload = message_around(tool_call(json!({})));
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"name": "search", "args": {}, "headers": {"X-Injected": "1"}}))
            .unwrap_err();
        assert_shim_error(err, "cmf.tool_pre_invoke", "headers");
    }

    #[test]
    fn tool_pre_invoke_without_a_tool_call_fails_closed() {
        let payload = MessagePayload {
            message: Message::text(Role::User, "what is the weather?"),
        };
        let err = to_legacy("cmf.tool_pre_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.tool_pre_invoke", "no tool_call content part");
    }

    #[test]
    fn tool_pre_invoke_with_two_tool_calls_fails_closed() {
        let payload = MessagePayload {
            message: Message::with_content(
                Role::Assistant,
                vec![tool_call(json!({})), tool_call(json!({}))],
            ),
        };
        let err = to_legacy("cmf.tool_pre_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.tool_pre_invoke", "2 tool_call content parts");
    }

    #[test]
    fn a_modified_payload_missing_required_fields_is_a_protocol_error() {
        let payload = message_around(tool_call(json!({})));
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();
        let err = shimmed.apply_modified(json!({"args": {}})).unwrap_err();
        assert!(matches!(err, HostError::Protocol { .. }), "got {err:?}");
        assert!(err.to_string().contains("tool_pre_invoke"), "{err}");
    }

    // --- cmf.tool_post_invoke -----------------------------------------------

    #[test]
    fn tool_post_invoke_sends_name_and_result() {
        let result = json!({"content": [{"type": "text", "text": "3 hits"}], "isError": false});
        let payload = message_around(tool_result(result.clone()));
        let shimmed = to_legacy("cmf.tool_post_invoke", &payload).unwrap();

        assert_eq!(shimmed.legacy_hook(), "tool_post_invoke");
        assert_eq!(
            shimmed.payload(),
            &json!({"name": "search", "result": result})
        );

        let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn tool_post_invoke_modified_result_maps_back() {
        let payload = message_around(tool_result(json!({"text": "ssn 123-45-6789"})));
        let shimmed = to_legacy("cmf.tool_post_invoke", &payload).unwrap();

        let back = shimmed
            .apply_modified(json!({"name": "search", "result": {"text": "ssn [REDACTED]"}}))
            .unwrap();
        assert_eq!(
            part_json(&back),
            json!({"content_type": "tool_result", "content": {
                "tool_call_id": "call-1",
                "tool_name": "search",
                "content": {"text": "ssn [REDACTED]"},
                "is_error": false,
            }})
        );
        assert_surroundings_kept(&back);
    }

    #[test]
    fn tool_post_invoke_without_a_tool_result_fails_closed() {
        let payload = message_around(tool_call(json!({})));
        let err = to_legacy("cmf.tool_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.tool_post_invoke", "no tool_result content part");
    }

    // --- cmf.prompt_pre_invoke ----------------------------------------------

    #[test]
    fn prompt_pre_invoke_sends_prompt_id_and_string_args() {
        let payload = message_around(prompt_request(json!({"lang": "en"})));
        let shimmed = to_legacy("cmf.prompt_pre_invoke", &payload).unwrap();

        assert_eq!(shimmed.legacy_hook(), "prompt_pre_fetch");
        assert_eq!(
            shimmed.payload(),
            &json!({"prompt_id": "greeting", "args": {"lang": "en"}})
        );

        let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn prompt_pre_invoke_modified_args_map_back() {
        let payload = message_around(prompt_request(json!({"lang": "en", "who": "bob"})));
        let shimmed = to_legacy("cmf.prompt_pre_invoke", &payload).unwrap();

        let back = shimmed
            .apply_modified(json!({"prompt_id": "greeting", "args": {"lang": "fr"}}))
            .unwrap();
        assert_eq!(
            part_json(&back),
            json!({"content_type": "prompt_request", "content": {
                "prompt_request_id": "pr-1",
                "name": "greeting",
                "arguments": {"lang": "fr"},
            }})
        );
        assert_surroundings_kept(&back);
    }

    #[test]
    fn prompt_pre_invoke_with_a_non_string_argument_fails_closed() {
        let payload = message_around(prompt_request(json!({"count": 5})));
        let err = to_legacy("cmf.prompt_pre_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.prompt_pre_invoke", "'count' is not a string");
    }

    #[test]
    fn prompt_pre_invoke_without_a_prompt_request_fails_closed() {
        let payload = message_around(tool_call(json!({})));
        let err = to_legacy("cmf.prompt_pre_invoke", &payload).unwrap_err();
        assert_shim_error(
            err,
            "cmf.prompt_pre_invoke",
            "no prompt_request content part",
        );
    }

    // --- cmf.prompt_post_invoke ---------------------------------------------

    #[test]
    fn prompt_post_invoke_sends_mcp_shaped_messages() {
        let payload = message_around(prompt_result(vec![
            Message::text(Role::System, "be brief"),
            Message::text(Role::User, "hi bob"),
        ]));
        let shimmed = to_legacy("cmf.prompt_post_invoke", &payload).unwrap();

        assert_eq!(shimmed.legacy_hook(), "prompt_post_fetch");
        assert_eq!(
            shimmed.payload(),
            &json!({"prompt_id": "greeting", "result": {
                "messages": [
                    {"role": "system", "content": {"type": "text", "text": "be brief"}},
                    {"role": "user", "content": {"type": "text", "text": "hi bob"}},
                ],
                "description": null,
            }})
        );

        let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn prompt_post_invoke_modified_messages_map_back() {
        let mut first = Message::text(Role::User, "hi bob");
        first.channel = Some(Channel::Final);
        let payload = message_around(prompt_result(vec![first]));
        let shimmed = to_legacy("cmf.prompt_post_invoke", &payload).unwrap();

        // Pydantic dumps the fixture TextContent's extra fields; they are
        // tolerated and dropped.
        let back = shimmed
            .apply_modified(json!({"prompt_id": "greeting", "result": {
                "messages": [
                    {"role": "user", "content": {"type": "text", "text": "hi [NAME]", "annotations": null}},
                    {"role": "assistant", "content": {"type": "text", "text": "appended"}},
                ],
                "description": null,
            }}))
            .unwrap();

        let part = part_json(&back);
        assert_eq!(
            part["content"]["prompt_request_id"], "pr-1",
            "unmapped fields survive"
        );
        let messages = &part["content"]["messages"];
        assert_eq!(messages[0]["content"][0]["text"], "hi [NAME]");
        assert_eq!(
            messages[0]["channel"], "final",
            "the original message's channel is kept"
        );
        assert_eq!(messages[1]["role"], "assistant");
        assert_eq!(messages[1]["content"][0]["text"], "appended");
        assert_surroundings_kept(&back);
    }

    #[test]
    fn prompt_post_invoke_with_a_multipart_message_fails_closed() {
        let payload = message_around(prompt_result(vec![Message::with_content(
            Role::User,
            vec![context_text(), context_text()],
        )]));
        let err = to_legacy("cmf.prompt_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.prompt_post_invoke", "not a single text part");
    }

    #[test]
    fn prompt_post_invoke_returned_description_fails_closed() {
        let payload = message_around(prompt_result(vec![]));
        let shimmed = to_legacy("cmf.prompt_post_invoke", &payload).unwrap();
        let err = shimmed
            .apply_modified(
                json!({"prompt_id": "greeting", "result": {"messages": [], "description": "x"}}),
            )
            .unwrap_err();
        assert_shim_error(err, "cmf.prompt_post_invoke", "description");
    }

    #[test]
    fn prompt_post_invoke_returned_non_text_content_fails_closed() {
        let payload = message_around(prompt_result(vec![]));
        let shimmed = to_legacy("cmf.prompt_post_invoke", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"prompt_id": "greeting", "result": {"messages": [
                {"role": "user", "content": {"type": "image", "data": "..."}}
            ]}}))
            .unwrap_err();
        assert_shim_error(err, "cmf.prompt_post_invoke", "messages[0].content");
    }

    // --- cmf.resource_pre_fetch ---------------------------------------------

    #[test]
    fn resource_pre_fetch_sends_the_uri_from_a_reference() {
        let payload = message_around(resource_ref());
        let shimmed = to_legacy("cmf.resource_pre_fetch", &payload).unwrap();

        assert_eq!(shimmed.legacy_hook(), "resource_pre_fetch");
        assert_eq!(shimmed.payload(), &json!({"uri": "file:///tmp/a.txt"}));

        // `metadata: {}` is what Pydantic's default dumps as.
        let back = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "metadata": {}}))
            .unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    /// A resource with no fetched content yet: nothing describes the URI
    /// beyond labels.
    fn unfetched_resource() -> Resource {
        Resource {
            mime_type: None,
            size_bytes: None,
            ..resource(None)
        }
    }

    #[test]
    fn resource_pre_fetch_modified_uri_maps_back_onto_an_unfetched_resource() {
        let payload = message_around(ContentPart::Resource {
            content: unfetched_resource(),
        });
        let shimmed = to_legacy("cmf.resource_pre_fetch", &payload).unwrap();

        let back = shimmed
            .apply_modified(json!({"uri": "file:///safe/a.txt", "metadata": {}}))
            .unwrap();
        let mut expected = unfetched_resource();
        expected.uri = "file:///safe/a.txt".into();
        assert_eq!(
            part_json(&back),
            serde_json::to_value(ContentPart::Resource { content: expected }).unwrap()
        );
        assert_surroundings_kept(&back);
    }

    #[test]
    fn resource_pre_fetch_returned_metadata_fails_closed() {
        let payload = message_around(resource_ref());
        let shimmed = to_legacy("cmf.resource_pre_fetch", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "metadata": {"etag": "x"}}))
            .unwrap_err();
        assert_shim_error(err, "cmf.resource_pre_fetch", "metadata");
    }

    #[test]
    fn resource_pre_fetch_without_a_resource_fails_closed() {
        let payload = MessagePayload {
            message: Message::text(Role::User, "no resource here"),
        };
        let err = to_legacy("cmf.resource_pre_fetch", &payload).unwrap_err();
        assert_shim_error(err, "cmf.resource_pre_fetch", "no resource or resource_ref");
    }

    // --- cmf.resource_post_fetch --------------------------------------------

    #[test]
    fn resource_post_fetch_sends_legacy_resource_content() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("hello")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();

        assert_eq!(shimmed.legacy_hook(), "resource_post_fetch");
        assert_eq!(
            shimmed.payload(),
            &json!({"uri": "file:///tmp/a.txt", "content": {
                "type": "resource",
                "id": "res-1",
                "uri": "file:///tmp/a.txt",
                "mime_type": "text/plain",
                "text": "hello",
            }})
        );

        let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn resource_post_fetch_modified_content_maps_back() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("password=hunter2")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();

        let back = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "content": {
                "type": "resource", "id": "res-1", "uri": "file:///tmp/a.txt",
                "mime_type": "text/plain", "text": "password=[REDACTED]", "blob": null,
            }}))
            .unwrap();
        let mut expected = resource(Some("password=[REDACTED]"));
        expected.size_bytes = Some("password=[REDACTED]".len() as u64);
        assert_eq!(
            part_json(&back),
            serde_json::to_value(ContentPart::Resource { content: expected }).unwrap(),
            "the text changes and the size follows it; name and annotations survive"
        );
        assert_surroundings_kept(&back);
    }

    #[test]
    fn resource_post_fetch_accepts_plain_string_content() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("a")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();
        let back = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "content": "b"}))
            .unwrap();
        assert_eq!(part_json(&back)["content"]["content"], "b");
    }

    #[test]
    fn resource_post_fetch_with_a_blob_fails_closed() {
        let mut binary = resource(None);
        binary.blob = Some(vec![0, 1, 2]);
        let payload = message_around(ContentPart::Resource { content: binary });
        let err = to_legacy("cmf.resource_post_fetch", &payload).unwrap_err();
        assert_shim_error(err, "cmf.resource_post_fetch", "blob");
    }

    #[test]
    fn resource_post_fetch_with_only_a_reference_fails_closed() {
        let payload = message_around(resource_ref());
        let err = to_legacy("cmf.resource_post_fetch", &payload).unwrap_err();
        assert_shim_error(
            err,
            "cmf.resource_post_fetch",
            "resource_ref with no content",
        );
    }

    #[test]
    fn resource_post_fetch_returned_blob_fails_closed() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("a")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "content": {"blob": "AAEC"}}))
            .unwrap_err();
        assert_shim_error(err, "cmf.resource_post_fetch", "blob");
    }

    // --- fields the legacy payload cannot carry ------------------------------

    #[test]
    fn tool_pre_invoke_with_a_namespace_fails_closed() {
        let mut part = tool_call(json!({}));
        if let ContentPart::ToolCall { content } = &mut part {
            content.namespace = Some("other-tenant".into());
        }
        let err = to_legacy("cmf.tool_pre_invoke", &message_around(part)).unwrap_err();
        assert_shim_error(err, "cmf.tool_pre_invoke", "namespace");
    }

    #[test]
    fn tool_pre_invoke_with_an_empty_namespace_is_unqualified() {
        let mut part = tool_call(json!({}));
        if let ContentPart::ToolCall { content } = &mut part {
            content.namespace = Some(String::new());
        }
        let payload = message_around(part);
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();
        let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    fn error_tool_result(content: Value) -> ContentPart {
        let mut part = tool_result(content);
        if let ContentPart::ToolResult { content } = &mut part {
            content.is_error = true;
        }
        part
    }

    #[test]
    fn tool_post_invoke_error_the_result_does_not_show_fails_closed() {
        let payload = message_around(error_tool_result(json!({"text": "boom"})));
        let err = to_legacy("cmf.tool_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.tool_post_invoke", "is_error");
    }

    #[test]
    fn tool_post_invoke_error_the_result_shows_round_trips() {
        let payload = message_around(error_tool_result(json!({"content": [], "isError": true})));
        let shimmed = to_legacy("cmf.tool_post_invoke", &payload).unwrap();
        let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn tool_post_invoke_clearing_is_error_fails_closed() {
        let payload = message_around(error_tool_result(json!({"content": [], "isError": true})));
        let shimmed = to_legacy("cmf.tool_post_invoke", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"name": "search", "result": {"content": [], "isError": false}}))
            .unwrap_err();
        assert_shim_error(err, "cmf.tool_post_invoke", "isError");
    }

    #[test]
    fn prompt_pre_invoke_with_a_server_id_fails_closed() {
        let mut part = prompt_request(json!({}));
        if let ContentPart::PromptRequest { content } = &mut part {
            content.server_id = Some("other-server".into());
        }
        let err = to_legacy("cmf.prompt_pre_invoke", &message_around(part)).unwrap_err();
        assert_shim_error(err, "cmf.prompt_pre_invoke", "server_id");
    }

    fn prompt_result_with(edit: impl FnOnce(&mut PromptResult)) -> MessagePayload {
        let mut part = prompt_result(vec![Message::text(Role::User, "hi bob")]);
        if let ContentPart::PromptResult { content } = &mut part {
            edit(content);
        }
        message_around(part)
    }

    #[test]
    fn prompt_post_invoke_with_rendered_content_fails_closed() {
        // A plugin that redacts `messages` would leave "hi bob" here.
        let payload = prompt_result_with(|r| r.content = Some("hi bob".into()));
        let err = to_legacy("cmf.prompt_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.prompt_post_invoke", "`content`");
    }

    #[test]
    fn prompt_post_invoke_with_an_error_message_fails_closed() {
        let payload = prompt_result_with(|r| r.error_message = Some("failed for bob".into()));
        let err = to_legacy("cmf.prompt_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.prompt_post_invoke", "error_message");
    }

    #[test]
    fn prompt_post_invoke_with_is_error_fails_closed() {
        let payload = prompt_result_with(|r| r.is_error = true);
        let err = to_legacy("cmf.prompt_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.prompt_post_invoke", "is_error");
    }

    #[test]
    fn resource_hooks_with_a_version_fail_closed() {
        let mut versioned = resource(Some("a"));
        versioned.version = Some("v1".into());
        let payload = message_around(ContentPart::Resource { content: versioned });
        for hook in ["cmf.resource_pre_fetch", "cmf.resource_post_fetch"] {
            let err = to_legacy(hook, &payload).unwrap_err();
            assert_shim_error(err, hook, "version");
        }
    }

    #[test]
    fn resource_pre_fetch_unchanged_uri_keeps_embedded_content() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("hello")),
        });
        let shimmed = to_legacy("cmf.resource_pre_fetch", &payload).unwrap();
        let back = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "metadata": {}}))
            .unwrap();
        assert_eq!(json_of(&back), json_of(&payload));
    }

    #[test]
    fn resource_pre_fetch_uri_rewrite_on_fetched_content_fails_closed() {
        let embedded = resource(Some("hello"));
        let mut sized = unfetched_resource();
        sized.size_bytes = Some(5);
        let mut typed = unfetched_resource();
        typed.mime_type = Some("text/plain".into());
        let mut binary = unfetched_resource();
        binary.blob = Some(vec![1]);
        for res in [embedded, sized, typed, binary] {
            let payload = message_around(ContentPart::Resource { content: res });
            let shimmed = to_legacy("cmf.resource_pre_fetch", &payload).unwrap();
            let err = shimmed
                .apply_modified(json!({"uri": "file:///safe/a.txt", "metadata": {}}))
                .unwrap_err();
            assert_shim_error(err, "cmf.resource_pre_fetch", "rewrote the URI");
        }
    }

    #[test]
    fn resource_post_fetch_changed_content_id_fails_closed() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("a")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "content": {
                "id": "res-2", "uri": "file:///tmp/a.txt", "text": "a",
            }}))
            .unwrap_err();
        assert_shim_error(err, "cmf.resource_post_fetch", "content.id");
    }

    #[test]
    fn resource_post_fetch_content_uri_alone_changed_fails_closed() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("a")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();
        let err = shimmed
            .apply_modified(json!({"uri": "file:///tmp/a.txt", "content": {
                "id": "res-1", "uri": "file:///safe/a.txt", "text": "a",
            }}))
            .unwrap_err();
        assert_shim_error(err, "cmf.resource_post_fetch", "content.uri");
    }

    #[test]
    fn resource_post_fetch_top_level_uri_change_maps_back() {
        let payload = message_around(ContentPart::Resource {
            content: resource(Some("a")),
        });
        let shimmed = to_legacy("cmf.resource_post_fetch", &payload).unwrap();
        let back = shimmed
            .apply_modified(json!({"uri": "file:///safe/a.txt", "content": {
                "id": "res-1", "uri": "file:///tmp/a.txt", "text": "a",
            }}))
            .unwrap();
        assert_eq!(part_json(&back)["content"]["uri"], "file:///safe/a.txt");
        assert_eq!(
            part_json(&back)["content"]["size_bytes"],
            5,
            "text unchanged"
        );
    }

    #[test]
    fn tool_post_invoke_a_plugin_setting_is_error_sets_the_flag() {
        let payload = message_around(tool_result(json!({"content": [], "isError": false})));
        let shimmed = to_legacy("cmf.tool_post_invoke", &payload).unwrap();
        let back = shimmed
            .apply_modified(json!({"name": "search", "result": {"content": [], "isError": true}}))
            .unwrap();
        assert_eq!(part_json(&back)["content"]["is_error"], true);
        assert_eq!(part_json(&back)["content"]["content"]["isError"], true);
    }

    // --- sibling content parts ---------------------------------------------

    /// Every shimmed hook, with a message holding just its part.
    fn each_hook_with_its_part() -> Vec<(&'static str, ContentPart)> {
        vec![
            ("cmf.tool_pre_invoke", tool_call(json!({}))),
            ("cmf.tool_post_invoke", tool_result(json!({"text": "ok"}))),
            ("cmf.prompt_pre_invoke", prompt_request(json!({}))),
            ("cmf.prompt_post_invoke", prompt_result(vec![])),
            ("cmf.resource_pre_fetch", resource_ref()),
            (
                "cmf.resource_post_fetch",
                ContentPart::Resource {
                    content: resource(Some("a")),
                },
            ),
        ]
    }

    /// Whether a shimmed hook is a post hook.
    fn is_post_hook(hook: &str) -> bool {
        hook.contains("_post_")
    }

    /// Sibling parts that carry text.
    fn texty_siblings() -> [ContentPart; 2] {
        [
            ContentPart::Text {
                text: "SSN 123-45-6789".into(),
            },
            ContentPart::Thinking {
                text: "SSN 123-45-6789".into(),
            },
        ]
    }

    #[test]
    fn a_sibling_text_part_fails_closed_on_post_hooks() {
        // The plugin would approve the one result part it sees while the text
        // beside it went downstream unredacted.
        for (hook, part) in each_hook_with_its_part()
            .into_iter()
            .filter(|(h, _)| is_post_hook(h))
        {
            for sibling in texty_siblings() {
                let payload = MessagePayload {
                    message: Message::with_content(Role::Assistant, vec![sibling, part.clone()]),
                };
                let err = to_legacy(hook, &payload).unwrap_err();
                assert_shim_error(err, hook, "beside the");
            }
        }
    }

    #[test]
    fn a_sibling_text_part_passes_through_on_pre_hooks() {
        // The plugin authorizes the part it sees. Narration beside it is
        // returned unchanged, through both an allow and a modification.
        for (hook, part) in each_hook_with_its_part()
            .into_iter()
            .filter(|(h, _)| !is_post_hook(h))
        {
            for sibling in texty_siblings() {
                let payload = MessagePayload {
                    message: Message::with_content(
                        Role::Assistant,
                        vec![sibling.clone(), part.clone()],
                    ),
                };
                let shimmed = to_legacy(hook, &payload).unwrap();
                let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
                assert_eq!(back.message.content.len(), 2, "{hook}");
                assert_eq!(
                    serde_json::to_value(&back.message.content[0]).unwrap(),
                    serde_json::to_value(&sibling).unwrap(),
                    "{hook}"
                );
                assert_eq!(
                    json_of(&back),
                    json_of(&payload),
                    "{hook}: an unmodified round trip changes nothing"
                );
            }
        }
    }

    #[test]
    fn a_pre_hook_rewrite_keeps_the_narration_sibling() {
        let narration = ContentPart::Text {
            text: "Let me look that up.".into(),
        };
        let payload = MessagePayload {
            message: Message::with_content(
                Role::Assistant,
                vec![narration.clone(), tool_call(json!({"q": "a"}))],
            ),
        };
        let shimmed = to_legacy("cmf.tool_pre_invoke", &payload).unwrap();
        let back = shimmed
            .apply_modified(json!({"name": "search", "args": {"q": "b"}}))
            .unwrap();
        let json = json_of(&back);
        assert_eq!(
            json["message"]["content"][0],
            serde_json::to_value(&narration).unwrap()
        );
        assert_eq!(
            json["message"]["content"][1]["content"]["arguments"]["q"],
            "b"
        );
    }

    #[test]
    fn a_sibling_part_of_another_kind_fails_closed() {
        let payload = MessagePayload {
            message: Message::with_content(
                Role::Assistant,
                vec![
                    tool_result(json!({"text": "ok"})),
                    ContentPart::ToolCall {
                        content: ToolCall {
                            tool_call_id: "call-2".into(),
                            name: "other".into(),
                            arguments: HashMap::new(),
                            namespace: None,
                        },
                    },
                ],
            ),
        };
        let err = to_legacy("cmf.tool_post_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.tool_post_invoke", "tool_call content part beside");
    }

    #[test]
    fn a_sibling_refusal_names_no_content() {
        let payload = MessagePayload {
            message: Message::with_content(
                Role::Assistant,
                vec![
                    ContentPart::Text {
                        text: "SECRET-SIBLING-TEXT".into(),
                    },
                    tool_result(json!({"text": "ok"})),
                ],
            ),
        };
        let err = to_legacy("cmf.tool_post_invoke", &payload).unwrap_err();
        assert!(!err.to_string().contains("SECRET-SIBLING-TEXT"), "{err}");
    }

    #[test]
    fn empty_sibling_parts_are_kept() {
        for (hook, part) in each_hook_with_its_part() {
            let payload = message_around(part);
            let shimmed = to_legacy(hook, &payload).unwrap();
            let back = shimmed.apply_modified(shimmed.payload().clone()).unwrap();
            assert_surroundings_kept(&back);
        }
    }

    // --- refusals -----------------------------------------------------------

    #[test]
    fn llm_hooks_have_no_legacy_equivalent_and_fail_closed() {
        let payload = MessagePayload {
            message: Message::text(Role::User, "hello"),
        };
        for hook in ["cmf.llm_input", "cmf.llm_output"] {
            assert!(is_cmf_hook(hook));
            let err = to_legacy(hook, &payload).unwrap_err();
            assert_shim_error(err, hook, "cmf_hooks");
        }
    }

    #[test]
    fn a_non_message_payload_fails_closed() {
        let payload = ToolPreInvokePayload {
            name: "search".into(),
            ..Default::default()
        };
        let err = to_legacy("cmf.tool_pre_invoke", &payload).unwrap_err();
        assert_shim_error(err, "cmf.tool_pre_invoke", "not a CMF MessagePayload");
    }

    #[test]
    fn shim_errors_carry_no_payload_values() {
        // Error text reaches logs. A refusal must name shapes, not content.
        let payload = MessagePayload {
            message: Message::text(Role::User, "SECRET-USER-TEXT"),
        };
        let err = to_legacy("cmf.tool_pre_invoke", &payload).unwrap_err();
        assert!(!err.to_string().contains("SECRET-USER-TEXT"), "{err}");
    }
}