modelexpress-server 0.8.0

High-performance gRPC server for model serving and management
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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Redis implementation of the collective control-plane backend.
//!
//! One group's root hash, participants, digests and lane hashes are mutated
//! together by a single Lua script, and none of the key helpers below carries a
//! hash tag. That makes the whole keyspace single-slot: this backend targets
//! standalone or replicated Redis, not Redis Cluster, where those keys would
//! land on different slots and every script would fail CROSSSLOT.

use std::collections::HashMap;
use std::fmt::Write as _;
use std::sync::LazyLock;
use std::time::{SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use modelexpress_common::envs;
use modelexpress_common::grpc::refit_collective::{
    CollectiveGroup, CollectiveGroupMembership, CollectiveGroupSpec, CollectiveGroupState,
    CollectiveLane, CollectiveParticipant, CollectiveRole, CollectiveTransfer,
    CollectiveTransferState, CreateCollectiveTransferRequest, JoinCollectiveGroupRequest,
    LaneAssignment, LaneKind, PlanSource, PublishGroupBootstrapRequest,
    ReportCollectiveTransferRequest,
};
use redis::aio::ConnectionManager;
use redis::{AsyncCommands, Script};
use sha2::{Digest, Sha256};
use uuid::Uuid;

use super::{CollectiveBackend, CollectiveBackendError, CollectiveResult};
use crate::refit_collective::lanes::{Lane, LaneLayout};

// EVAL has no `require`, so the helpers the group scripts share live in one
// chunk that is prepended to each of them here, at compile time.
macro_rules! with_common_lua {
    ($script:literal) => {
        concat!(
            include_str!("redis/scripts/common.lua"),
            "\n",
            include_str!($script)
        )
    };
}

const JOIN_GROUP_LUA: &str = with_common_lua!("redis/scripts/join_collective_group.lua");
const PUBLISH_BOOTSTRAP_LUA: &str = with_common_lua!("redis/scripts/publish_group_bootstrap.lua");
const CREATE_TRANSFER_LUA: &str = include_str!("redis/scripts/create_collective_transfer.lua");
const REPORT_TRANSFER_LUA: &str = with_common_lua!("redis/scripts/report_collective_transfer.lua");
const REFRESH_GROUP_LUA: &str = with_common_lua!("redis/scripts/refresh_collective_group.lua");
const DELETE_TRANSFER_LUA: &str = include_str!("redis/scripts/delete_collective_transfer.lua");

// `Script::new` hashes and copies the source, and the scripts never change,
// so each is built once per process.
static JOIN_GROUP_SCRIPT: LazyLock<Script> = LazyLock::new(|| Script::new(JOIN_GROUP_LUA));
static PUBLISH_BOOTSTRAP_SCRIPT: LazyLock<Script> =
    LazyLock::new(|| Script::new(PUBLISH_BOOTSTRAP_LUA));
static CREATE_TRANSFER_SCRIPT: LazyLock<Script> =
    LazyLock::new(|| Script::new(CREATE_TRANSFER_LUA));
static REPORT_TRANSFER_SCRIPT: LazyLock<Script> =
    LazyLock::new(|| Script::new(REPORT_TRANSFER_LUA));
static REFRESH_GROUP_SCRIPT: LazyLock<Script> = LazyLock::new(|| Script::new(REFRESH_GROUP_LUA));
static DELETE_TRANSFER_SCRIPT: LazyLock<Script> =
    LazyLock::new(|| Script::new(DELETE_TRANSFER_LUA));

fn group_key(group_id: &str) -> String {
    format!("mx:refitc:group:{group_id}")
}

fn participants_key(group_id: &str) -> String {
    format!("mx:refitc:group:{group_id}:participants")
}

fn digests_key(group_id: &str) -> String {
    format!("mx:refitc:group:{group_id}:digests")
}

fn active_ops_key(group_id: &str) -> String {
    format!("mx:refitc:group:{group_id}:ops")
}

fn lane_key(group_id: &str, lane_id: u32) -> String {
    format!("mx:refitc:group:{group_id}:lane:{lane_id}")
}

const OPERATION_KEY_PREFIX: &str = "mx:refitc:op:";

fn operation_key(operation_id: &str) -> String {
    format!("{OPERATION_KEY_PREFIX}{operation_id}")
}

fn reported_key(operation_id: &str) -> String {
    format!("mx:refitc:op:{operation_id}:reported")
}

fn operation_idempotency_key(model_name: &str, request_key: &str) -> String {
    format!(
        "mx:refitc:op-request:{}:{model_name}{request_key}",
        model_name.len()
    )
}

fn worker_key(worker_id: &str) -> String {
    format!("mx:refit:worker:{worker_id}")
}

/// Derive a stable group identity from the declared membership.
///
/// Every participant of one operation sends an identical declaration, so they
/// all resolve the same group without a separate create call. A participant
/// that declares a different membership resolves a *different* group, which
/// then never reaches its expected count -- a bounded timeout naming the
/// missing slots, rather than one group with inconsistent geometry.
fn group_id_for(spec: &CollectiveGroupSpec) -> String {
    let mut trainers: Vec<&String> = spec.expected_trainer_slots.iter().collect();
    let mut generators: Vec<&String> = spec.expected_generator_slots.iter().collect();
    trainers.sort();
    generators.sort();

    // Lane order is normalized for the same reason the slot lists are: two
    // participants declaring the same membership in a different vector order
    // would otherwise resolve two groups, each stuck below its expected count.
    // Slot order WITHIN a lane stays significant - it is the rank assignment.
    let mut lanes: Vec<&_> = spec.lanes.iter().collect();
    lanes.sort_by_key(|lane| (lane.lane_id, lane.kind));

    let mut hasher = Sha256::new();
    hasher.update(spec.model_name.as_bytes());
    hasher.update([0]);
    for lane in lanes {
        hasher.update(lane.lane_id.to_le_bytes());
        hasher.update(lane.kind.to_le_bytes());
        for slot in lane.trainer_slots.iter().chain(lane.generator_slots.iter()) {
            hasher.update(slot.as_bytes());
            hasher.update([0]);
        }
        hasher.update([2]);
    }
    hasher.update([0]);
    for slot in &trainers {
        hasher.update(slot.as_bytes());
        hasher.update([0]);
    }
    hasher.update([1]);
    for slot in &generators {
        hasher.update(slot.as_bytes());
        hasher.update([0]);
    }

    let digest = hasher.finalize();
    let mut id = String::with_capacity(32);
    for byte in &digest[..16] {
        let _ = write!(id, "{byte:02x}");
    }
    id
}

fn now_unix_ms() -> CollectiveResult<u64> {
    let millis = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|error| CollectiveBackendError::Internal(format!("system clock error: {error}")))?
        .as_millis();
    u64::try_from(millis).map_err(|_| {
        CollectiveBackendError::Internal("system time does not fit in uint64".to_string())
    })
}

fn redis_error(error: redis::RedisError) -> CollectiveBackendError {
    if error.is_io_error()
        || error.is_cluster_error()
        || matches!(
            error.kind(),
            redis::ErrorKind::BusyLoadingError
                | redis::ErrorKind::MasterDown
                | redis::ErrorKind::ClusterConnectionNotFound
        )
    {
        CollectiveBackendError::Unavailable(error.to_string())
    } else {
        CollectiveBackendError::Internal(error.to_string())
    }
}

fn hex_encode(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len().saturating_mul(2));
    for byte in bytes {
        let _ = write!(out, "{byte:02x}");
    }
    out
}

fn hex_decode(text: &str) -> CollectiveResult<Vec<u8>> {
    let bytes = text.as_bytes();
    if !bytes.len().is_multiple_of(2) {
        return Err(CollectiveBackendError::Internal(
            "bootstrap identifier is not valid hex".to_string(),
        ));
    }
    bytes
        .as_chunks::<2>()
        .0
        .iter()
        .map(|pair| {
            let digits = std::str::from_utf8(pair).map_err(|error| {
                CollectiveBackendError::Internal(format!("invalid bootstrap identifier: {error}"))
            })?;
            u8::from_str_radix(digits, 16).map_err(|error| {
                CollectiveBackendError::Internal(format!("invalid bootstrap identifier: {error}"))
            })
        })
        .collect()
}

fn field<'a>(fields: &'a HashMap<String, String>, name: &str) -> CollectiveResult<&'a str> {
    fields.get(name).map(String::as_str).ok_or_else(|| {
        CollectiveBackendError::Internal(format!("collective record is missing {name}"))
    })
}

fn parse_field<T>(fields: &HashMap<String, String>, name: &str) -> CollectiveResult<T>
where
    T: std::str::FromStr,
    T::Err: std::fmt::Display,
{
    field(fields, name)?.parse().map_err(|error| {
        CollectiveBackendError::Internal(format!("invalid {name} in collective record: {error}"))
    })
}

fn group_state_from_str(text: &str) -> CollectiveResult<CollectiveGroupState> {
    match text {
        "FORMING" => Ok(CollectiveGroupState::Forming),
        "READY" => Ok(CollectiveGroupState::Ready),
        other => Err(CollectiveBackendError::Internal(format!(
            "unknown stored collective group state {other}"
        ))),
    }
}

fn transfer_state_from_str(text: &str) -> CollectiveResult<CollectiveTransferState> {
    match text {
        "PENDING" => Ok(CollectiveTransferState::Pending),
        "RUNNING" => Ok(CollectiveTransferState::Running),
        "COMPLETE" => Ok(CollectiveTransferState::Complete),
        "FAILED" => Ok(CollectiveTransferState::Failed),
        "ABORTED" => Ok(CollectiveTransferState::Aborted),
        other => Err(CollectiveBackendError::Internal(format!(
            "unknown stored collective transfer state {other}"
        ))),
    }
}

/// The declared lane set, flattened for the group hash: one line per lane,
/// `lane_id|kind|trainer_slots|generator_slots`, slot lists comma separated.
/// `validate_spec` rejects `\n`, `|` and `,` in every slot id, so none of the
/// three separators can occur inside one.
fn encode_lanes(lanes: &[Lane]) -> String {
    lanes
        .iter()
        .map(|lane| {
            format!(
                "{}|{}|{}|{}",
                lane.lane_id,
                i32::from(lane.kind),
                lane.trainer_slots.join(","),
                lane.generator_slots.join(",")
            )
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn decode_lanes(text: &str) -> CollectiveResult<Vec<Lane>> {
    if text.is_empty() {
        return Ok(Vec::new());
    }
    text.split('\n')
        .map(|line| {
            let mut parts = line.split('|');
            let lane_id: u32 = parts.next().unwrap_or_default().parse().map_err(|error| {
                CollectiveBackendError::Internal(format!("invalid stored lane_id: {error}"))
            })?;
            let kind_code: i32 = parts.next().unwrap_or_default().parse().map_err(|error| {
                CollectiveBackendError::Internal(format!("invalid stored lane kind: {error}"))
            })?;
            let kind = LaneKind::try_from(kind_code).map_err(|_| {
                CollectiveBackendError::Internal(format!("unknown stored lane kind {kind_code}"))
            })?;
            let trainer_slots = split_csv(parts.next().unwrap_or_default());
            let generator_slots = split_csv(parts.next().unwrap_or_default());
            if parts.next().is_some() {
                return Err(CollectiveBackendError::Internal(
                    "stored lane record has extra fields".to_string(),
                ));
            }
            Ok(Lane {
                lane_id,
                kind,
                trainer_slots,
                generator_slots,
            })
        })
        .collect()
}

fn lanes_from_spec(spec: &CollectiveGroupSpec) -> Vec<Lane> {
    spec.lanes
        .iter()
        .map(|lane| Lane {
            lane_id: lane.lane_id,
            kind: LaneKind::try_from(lane.kind).unwrap_or(LaneKind::Unspecified),
            trainer_slots: lane.trainer_slots.clone(),
            generator_slots: lane.generator_slots.clone(),
        })
        .collect()
}

fn split_csv(text: &str) -> Vec<String> {
    if text.is_empty() {
        return Vec::new();
    }
    text.split(',').map(str::to_string).collect()
}

fn split_slots(text: &str) -> Vec<String> {
    if text.is_empty() {
        return Vec::new();
    }
    text.split('\n').map(str::to_string).collect()
}

/// Parse one `worker_id|role|index_in_role|joined_epoch` record.
fn participant_from_record(slot_id: &str, record: &str) -> CollectiveResult<CollectiveParticipant> {
    let mut parts = record.split('|');
    let worker_id = parts.next().unwrap_or_default().to_string();
    let role = match parts.next().unwrap_or_default() {
        "TRAINER" => CollectiveRole::Trainer,
        "GENERATOR" => CollectiveRole::Generator,
        other => {
            return Err(CollectiveBackendError::Internal(format!(
                "unknown participant role {other}"
            )));
        }
    };
    let index_in_role: u32 = parts.next().unwrap_or_default().parse().map_err(|error| {
        CollectiveBackendError::Internal(format!("invalid participant index: {error}"))
    })?;
    let joined_epoch: u64 = parts.next().unwrap_or_default().parse().map_err(|error| {
        CollectiveBackendError::Internal(format!("invalid participant epoch: {error}"))
    })?;
    if parts.next().is_some() {
        return Err(CollectiveBackendError::Internal(
            "participant record has extra fields".to_string(),
        ));
    }

    Ok(CollectiveParticipant {
        slot_id: slot_id.to_string(),
        worker_id,
        role: role.into(),
        index_in_role,
        rank_in_lane: 0,
        joined_epoch,
    })
}

pub struct RedisCollectiveBackend {
    connection: ConnectionManager,
}

impl RedisCollectiveBackend {
    pub async fn connect(url: &str) -> CollectiveResult<Self> {
        let client = redis::Client::open(url).map_err(redis_error)?;
        let connection = ConnectionManager::new(client).await.map_err(redis_error)?;
        Ok(Self { connection })
    }

    fn layout_for(spec: &CollectiveGroupSpec) -> CollectiveResult<LaneLayout> {
        LaneLayout::new(
            lanes_from_spec(spec),
            &spec.expected_trainer_slots,
            &spec.expected_generator_slots,
        )
        .map_err(|error| CollectiveBackendError::InvalidArgument(error.to_string()))
    }

    async fn read_group_once(&self, group_id: &str) -> CollectiveResult<CollectiveGroup> {
        let mut connection = self.connection.clone();
        let fields: HashMap<String, String> = connection
            .hgetall(group_key(group_id))
            .await
            .map_err(redis_error)?;
        if fields.is_empty() {
            return Err(CollectiveBackendError::NotFound(format!(
                "collective group {group_id} was not found"
            )));
        }

        let epoch: u64 = parse_field(&fields, "epoch")?;
        let trainer_slots = split_slots(field(&fields, "expected_trainer_slots")?);
        let generator_slots = split_slots(field(&fields, "expected_generator_slots")?);
        let layout = LaneLayout::new(
            decode_lanes(field(&fields, "lanes")?)?,
            &trainer_slots,
            &generator_slots,
        )
        .map_err(|error| CollectiveBackendError::Internal(error.to_string()))?;

        let records: HashMap<String, String> = connection
            .hgetall(participants_key(group_id))
            .await
            .map_err(redis_error)?;

        // Place every participant in one pass. Rebuilding the assignment per
        // lane instead would parse and re-assign the whole membership
        // `lane_count` times, and `publish_bootstrap` reads a group twice.
        let mut lane_participants: HashMap<u32, Vec<CollectiveParticipant>> = layout
            .lanes()
            .iter()
            .map(|lane| (lane.lane_id, Vec::new()))
            .collect();
        for (slot_id, record) in &records {
            let participant = participant_from_record(slot_id, record)?;
            let assignments = layout.assign(slot_id).map_err(|error| {
                CollectiveBackendError::Internal(format!(
                    "stored participant {slot_id} has an invalid lane assignment: {error}"
                ))
            })?;
            for assignment in assignments {
                if let Some(lane) = lane_participants.get_mut(&assignment.lane_id) {
                    let mut placed = participant.clone();
                    placed.rank_in_lane = assignment.rank_in_lane;
                    lane.push(placed);
                }
            }
        }
        for participants in lane_participants.values_mut() {
            participants.sort_by_key(|p| p.rank_in_lane);
        }

        let mut lane_pipe = redis::pipe();
        for lane in layout.lanes() {
            lane_pipe.hgetall(lane_key(group_id, lane.lane_id));
        }
        let lane_hashes: Vec<HashMap<String, String>> = lane_pipe
            .query_async(&mut connection)
            .await
            .map_err(redis_error)?;

        let mut lanes: Vec<CollectiveLane> = Vec::with_capacity(layout.lanes().len());
        for (position, declared) in layout.lanes().iter().enumerate() {
            let lane_id = declared.lane_id;
            let kind = declared.kind;
            let world_size = declared.world_size();
            let participants = lane_participants.remove(&lane_id).unwrap_or_default();
            let empty = HashMap::new();
            let lane_fields = lane_hashes.get(position).unwrap_or(&empty);
            let nccl_unique_id = match lane_fields.get("nccl_unique_id") {
                Some(text) => hex_decode(text)?,
                None => Vec::new(),
            };
            let bootstrap_epoch: u64 = lane_fields
                .get("bootstrap_epoch")
                .and_then(|value| value.parse().ok())
                .unwrap_or(0);

            lanes.push(CollectiveLane {
                lane_id,
                kind: kind.into(),
                world_size,
                nccl_unique_id,
                bootstrap_epoch,
                participants,
            });
        }

        // The digests hash already holds every slot's reported digest; the
        // group only ever stored the latest. Surfacing the disagreement is the
        // difference between a diagnosable cohort split and an unexplained
        // FORMING that never resolves.
        let group_digest = field(&fields, "plan_digest")?.to_string();
        let reported: HashMap<String, String> = connection
            .hgetall(digests_key(group_id))
            .await
            .map_err(redis_error)?;
        let mut disagreeing_slots: Vec<String> = reported
            .into_iter()
            .filter(|(_, digest)| *digest != group_digest)
            .map(|(slot_id, _)| slot_id)
            .collect();
        disagreeing_slots.sort();

        let plan_source_worker = field(&fields, "plan_source_worker_id")?.to_string();
        let plan_source = if plan_source_worker.is_empty() {
            None
        } else {
            Some(PlanSource {
                worker_id: plan_source_worker,
                endpoint: field(&fields, "plan_source_endpoint")?.to_string(),
                digest: field(&fields, "plan_source_digest")?.to_string(),
            })
        };

        Ok(CollectiveGroup {
            group_id: group_id.to_string(),
            model_name: field(&fields, "model_name")?.to_string(),
            epoch,
            state: group_state_from_str(field(&fields, "state")?)?.into(),
            lanes,
            plan_source,
            plan_digest: group_digest,
            expected_trainer_slots: trainer_slots,
            expected_generator_slots: generator_slots,
            created_at_unix_ms: parse_field(&fields, "created_at_unix_ms")?,
            disagreeing_slots,
        })
    }

    async fn refresh_group_state(&self, group_id: &str) -> CollectiveResult<()> {
        let mut connection = self.connection.clone();
        let stored: Option<String> = connection
            .hget(group_key(group_id), "lanes")
            .await
            .map_err(redis_error)?;
        let Some(stored) = stored else {
            return Err(CollectiveBackendError::NotFound(format!(
                "collective group {group_id} was not found"
            )));
        };
        let lanes = decode_lanes(&stored)?;

        let mut script = REFRESH_GROUP_SCRIPT.prepare_invoke();
        script.key(group_key(group_id));
        script.key(participants_key(group_id));
        script.key(digests_key(group_id));
        script.key(active_ops_key(group_id));
        for lane in &lanes {
            script.key(lane_key(group_id, lane.lane_id));
        }
        let outcome: String = script
            .arg(OPERATION_KEY_PREFIX)
            .invoke_async(&mut connection)
            .await
            .map_err(redis_error)?;
        if outcome == "NOTFOUND" {
            return Err(CollectiveBackendError::NotFound(format!(
                "collective group {group_id} was not found"
            )));
        }
        if !outcome.starts_with("OK:") {
            return Err(CollectiveBackendError::Internal(format!(
                "unexpected group refresh outcome {outcome}"
            )));
        }
        Ok(())
    }

    async fn read_group(&self, group_id: &str) -> CollectiveResult<CollectiveGroup> {
        // A group spans several Redis hashes. Retry if a concurrent join or
        // bootstrap changes the root record while those hashes are being read,
        // so callers never receive READY paired with another epoch's lanes.
        for _ in 0..5 {
            self.refresh_group_state(group_id).await?;
            let group = self.read_group_once(group_id).await?;
            let mut connection = self.connection.clone();
            let fields: HashMap<String, String> = connection
                .hgetall(group_key(group_id))
                .await
                .map_err(redis_error)?;
            if !fields.is_empty()
                && parse_field::<u64>(&fields, "epoch")? == group.epoch
                && i32::from(group_state_from_str(field(&fields, "state")?)?) == group.state
            {
                return Ok(group);
            }
        }
        Err(CollectiveBackendError::Unavailable(format!(
            "collective group {group_id} changed while it was being read"
        )))
    }

    async fn read_transfer(&self, operation_id: &str) -> CollectiveResult<CollectiveTransfer> {
        let mut connection = self.connection.clone();
        let fields: HashMap<String, String> = connection
            .hgetall(operation_key(operation_id))
            .await
            .map_err(redis_error)?;
        if fields.is_empty() {
            return Err(CollectiveBackendError::NotFound(format!(
                "collective transfer {operation_id} was not found"
            )));
        }
        let reported: Vec<String> = connection
            .smembers(reported_key(operation_id))
            .await
            .map_err(redis_error)?;

        Ok(CollectiveTransfer {
            operation_id: operation_id.to_string(),
            group_id: field(&fields, "group_id")?.to_string(),
            epoch: parse_field(&fields, "epoch")?,
            version_id: field(&fields, "version_id")?.to_string(),
            model_name: field(&fields, "model_name")?.to_string(),
            idempotency_key: field(&fields, "idempotency_key")?.to_string(),
            state: transfer_state_from_str(field(&fields, "state")?)?.into(),
            created_at_unix_ms: parse_field(&fields, "created_at_unix_ms")?,
            reported_worker_ids: reported,
            failure_message: field(&fields, "failure_message")?.to_string(),
        })
    }
}

#[async_trait]
impl CollectiveBackend for RedisCollectiveBackend {
    async fn join_group(
        &self,
        request: &JoinCollectiveGroupRequest,
    ) -> CollectiveResult<CollectiveGroupMembership> {
        let spec = request.spec.as_ref().ok_or_else(|| {
            CollectiveBackendError::InvalidArgument("spec is required".to_string())
        })?;
        let layout = Self::layout_for(spec)?;
        let role = CollectiveRole::try_from(request.role).unwrap_or(CollectiveRole::Unspecified);
        let assignments = layout
            .assign(&request.slot_id)
            .map_err(|error| CollectiveBackendError::InvalidArgument(error.to_string()))?;

        let group_id = group_id_for(spec);
        let mut keys = vec![
            group_key(&group_id),
            participants_key(&group_id),
            digests_key(&group_id),
            worker_key(&request.worker_id),
            active_ops_key(&group_id),
        ];
        for lane in layout.lanes() {
            keys.push(lane_key(&group_id, lane.lane_id));
        }

        let role_text = match role {
            CollectiveRole::Trainer => "TRAINER",
            CollectiveRole::Generator => "GENERATOR",
            CollectiveRole::Unspecified => {
                return Err(CollectiveBackendError::InvalidArgument(
                    "role must be specified".to_string(),
                ));
            }
        };
        let expected_total = layout.trainer_count.saturating_add(layout.generator_count);
        let default_source = PlanSource::default();
        let plan_source = request.plan_source.as_ref().unwrap_or(&default_source);

        let mut script = JOIN_GROUP_SCRIPT.prepare_invoke();
        for key in &keys {
            script.key(key);
        }
        let outcome: String = script
            .arg(&group_id)
            .arg(&spec.model_name)
            .arg(encode_lanes(layout.lanes()))
            .arg(expected_total)
            .arg(&request.slot_id)
            .arg(&request.worker_id)
            .arg(role_text)
            .arg(request.index_in_role)
            .arg(&request.plan_digest)
            .arg(&plan_source.worker_id)
            .arg(&plan_source.endpoint)
            .arg(&plan_source.digest)
            .arg(spec.expected_trainer_slots.join("\n"))
            .arg(spec.expected_generator_slots.join("\n"))
            .arg(now_unix_ms()?)
            .arg(OPERATION_KEY_PREFIX)
            .invoke_async(&mut self.connection.clone())
            .await
            .map_err(redis_error)?;

        match outcome.as_str() {
            "UNEXPECTED_SLOT" => {
                return Err(CollectiveBackendError::InvalidArgument(format!(
                    "slot {} is not expected for role {role_text}",
                    request.slot_id
                )));
            }
            "UNREGISTERED" => {
                return Err(CollectiveBackendError::FailedPrecondition(format!(
                    "worker {} has no live matching registration",
                    request.worker_id
                )));
            }
            "INVALID_PLAN_SOURCE" => {
                return Err(CollectiveBackendError::InvalidArgument(
                    "only a live trainer at index 0 may advertise a plan source, and only \
                     under its own worker_id"
                        .to_string(),
                ));
            }
            "CONFLICTING_ASSIGNMENT" => {
                return Err(CollectiveBackendError::AlreadyExists(format!(
                    "slot {} is already bound to a different role or ordinal",
                    request.slot_id
                )));
            }
            "DUPLICATE_RANK" => {
                return Err(CollectiveBackendError::AlreadyExists(format!(
                    "another slot already owns {role_text} index {}",
                    request.index_in_role
                )));
            }
            "DUPLICATE_WORKER" => {
                return Err(CollectiveBackendError::AlreadyExists(format!(
                    "worker {} is already admitted under another slot",
                    request.worker_id
                )));
            }
            "CORRUPT_PARTICIPANT" => {
                return Err(CollectiveBackendError::Internal(
                    "collective group contains a malformed participant record".to_string(),
                ));
            }
            _ => {}
        }

        let mut parts = outcome.split(':');
        if parts.next() != Some("OK") {
            return Err(CollectiveBackendError::Internal(format!(
                "unexpected join outcome {outcome}"
            )));
        }
        let epoch: u64 = parts
            .next()
            .and_then(|value| value.parse().ok())
            .ok_or_else(|| {
                CollectiveBackendError::Internal("join returned no epoch".to_string())
            })?;
        let state = group_state_from_str(parts.next().unwrap_or("FORMING"))?;

        Ok(CollectiveGroupMembership {
            group_id,
            epoch,
            assignments: assignments
                .iter()
                .map(|a| LaneAssignment {
                    lane_id: a.lane_id,
                    kind: a.kind.into(),
                    rank_in_lane: a.rank_in_lane,
                    world_size: a.world_size,
                })
                .collect(),
            state: state.into(),
            is_bootstrap_leader: layout.is_bootstrap_leader(&request.slot_id),
        })
    }

    async fn get_group(&self, group_id: &str) -> CollectiveResult<CollectiveGroup> {
        self.read_group(group_id).await
    }

    async fn publish_bootstrap(
        &self,
        request: &PublishGroupBootstrapRequest,
    ) -> CollectiveResult<CollectiveGroup> {
        let group = self.read_group(&request.group_id).await?;
        // Lane ids are whatever the caller declared, so membership is the
        // test, not a range check against the count.
        let lane = group
            .lanes
            .iter()
            .find(|lane| lane.lane_id == request.lane_id)
            .ok_or_else(|| {
                CollectiveBackendError::InvalidArgument(format!(
                    "lane {} is not declared by this group",
                    request.lane_id
                ))
            })?;
        let leader_slot = lane
            .participants
            .iter()
            .find(|participant| participant.rank_in_lane == 0)
            .map(|participant| participant.slot_id.as_str())
            .ok_or_else(|| {
                CollectiveBackendError::FailedPrecondition(format!(
                    "lane {} has no admitted rank-0 participant",
                    request.lane_id
                ))
            })?;

        let mut script = PUBLISH_BOOTSTRAP_SCRIPT.prepare_invoke();
        script.key(group_key(&request.group_id));
        script.key(participants_key(&request.group_id));
        script.key(digests_key(&request.group_id));
        script.key(lane_key(&request.group_id, request.lane_id));
        for lane in &group.lanes {
            script.key(lane_key(&request.group_id, lane.lane_id));
        }

        let outcome: String = script
            .arg(request.epoch)
            .arg(&request.worker_id)
            .arg(hex_encode(&request.nccl_unique_id))
            .arg(leader_slot)
            .invoke_async(&mut self.connection.clone())
            .await
            .map_err(redis_error)?;

        if outcome == "NOTFOUND" {
            return Err(CollectiveBackendError::NotFound(format!(
                "collective group {} was not found",
                request.group_id
            )));
        }
        if let Some(current) = outcome.strip_prefix("STALE:") {
            return Err(CollectiveBackendError::FailedPrecondition(format!(
                "bootstrap for epoch {} was rejected; the group is at epoch {current}",
                request.epoch
            )));
        }
        if outcome == "NOTLEADER" {
            return Err(CollectiveBackendError::FailedPrecondition(format!(
                "worker {} is not the live rank-0 participant for lane {}",
                request.worker_id, request.lane_id
            )));
        }
        if outcome == "CONFLICT" {
            return Err(CollectiveBackendError::AlreadyExists(format!(
                "lane {} already has a different bootstrap for epoch {}",
                request.lane_id, request.epoch
            )));
        }
        if !outcome.starts_with("OK:") {
            return Err(CollectiveBackendError::Internal(format!(
                "unexpected bootstrap outcome {outcome}"
            )));
        }

        self.read_group(&request.group_id).await
    }

    async fn create_transfer(
        &self,
        request: &CreateCollectiveTransferRequest,
    ) -> CollectiveResult<CollectiveTransfer> {
        let spec = request.spec.as_ref().ok_or_else(|| {
            CollectiveBackendError::InvalidArgument("spec is required".to_string())
        })?;
        Self::layout_for(spec)?;
        if request.expected_epoch == 0 {
            return Err(CollectiveBackendError::InvalidArgument(
                "expected_epoch is required: name the READY group epoch to transfer at".to_string(),
            ));
        }
        let group_id = group_id_for(spec);
        let operation_id = Uuid::new_v4().simple().to_string();

        match self.refresh_group_state(&group_id).await {
            Ok(()) | Err(CollectiveBackendError::NotFound(_)) => {}
            Err(error) => return Err(error),
        }

        let outcome: String = CREATE_TRANSFER_SCRIPT
            .key(operation_key(&operation_id))
            .key(operation_idempotency_key(
                &spec.model_name,
                &request.idempotency_key,
            ))
            .key(group_key(&group_id))
            .key(active_ops_key(&group_id))
            .arg(&operation_id)
            .arg(&group_id)
            .arg(&request.version_id)
            .arg(&spec.model_name)
            .arg(&request.idempotency_key)
            .arg("PENDING")
            .arg(now_unix_ms()?)
            .arg(OPERATION_KEY_PREFIX)
            .arg(envs::nccl_refit_transfer_timeout_secs())
            .arg(request.expected_epoch)
            .invoke_async(&mut self.connection.clone())
            .await
            .map_err(redis_error)?;

        match outcome.as_str() {
            "CREATED" => self.read_transfer(&operation_id).await,
            other if other.starts_with("NOTREADY:") => {
                let mut parts = other.trim_start_matches("NOTREADY:").splitn(2, ':');
                let epoch = parts.next().unwrap_or_default();
                let state = parts.next().unwrap_or_default();
                Err(CollectiveBackendError::FailedPrecondition(format!(
                    "collective group {group_id} is {state} at epoch {epoch}, not READY at \
                     the expected epoch {}; no transfer was created",
                    request.expected_epoch
                )))
            }
            "COLLISION" => Err(CollectiveBackendError::Internal(
                "generated operation id already exists".to_string(),
            )),
            "NOGROUP" => Err(CollectiveBackendError::FailedPrecondition(
                "no collective group has formed for this membership yet".to_string(),
            )),
            other if other.starts_with("BUSY:") => {
                let busy = other.trim_start_matches("BUSY:");
                Err(CollectiveBackendError::FailedPrecondition(format!(
                    "collective transfer {busy} is still in flight at this group epoch; \
                     it must complete, fail or be aborted before another is created"
                )))
            }
            other => match other.strip_prefix("EXISTING:") {
                Some(existing) => {
                    let transfer = self.read_transfer(existing).await?;
                    if transfer.group_id == group_id
                        && transfer.version_id == request.version_id
                        && transfer.model_name == spec.model_name
                        && transfer.idempotency_key == request.idempotency_key
                    {
                        Ok(transfer)
                    } else {
                        Err(CollectiveBackendError::AlreadyExists(
                            "idempotency_key was already used for a different collective transfer"
                                .to_string(),
                        ))
                    }
                }
                None => Err(CollectiveBackendError::Internal(format!(
                    "unexpected create outcome {other}"
                ))),
            },
        }
    }

    async fn get_transfer(&self, operation_id: &str) -> CollectiveResult<CollectiveTransfer> {
        self.read_transfer(operation_id).await
    }

    async fn delete_transfer(&self, operation_id: &str) -> CollectiveResult<CollectiveTransfer> {
        let transfer = self.read_transfer(operation_id).await?;
        if !matches!(
            CollectiveTransferState::try_from(transfer.state),
            Ok(CollectiveTransferState::Complete)
                | Ok(CollectiveTransferState::Failed)
                | Ok(CollectiveTransferState::Aborted)
        ) {
            return Err(CollectiveBackendError::FailedPrecondition(format!(
                "collective transfer {operation_id} is not terminal"
            )));
        }

        let outcome: String = DELETE_TRANSFER_SCRIPT
            .key(operation_key(operation_id))
            .key(reported_key(operation_id))
            .key(operation_idempotency_key(
                &transfer.model_name,
                &transfer.idempotency_key,
            ))
            .key(active_ops_key(&transfer.group_id))
            .arg(operation_id)
            .invoke_async(&mut self.connection.clone())
            .await
            .map_err(redis_error)?;
        match outcome.as_str() {
            "DELETED" => Ok(transfer),
            "NOTFOUND" => Err(CollectiveBackendError::NotFound(format!(
                "collective transfer {operation_id} was not found"
            ))),
            "NOTTERMINAL" => Err(CollectiveBackendError::FailedPrecondition(format!(
                "collective transfer {operation_id} is not terminal"
            ))),
            other => Err(CollectiveBackendError::Internal(format!(
                "unexpected delete outcome {other}"
            ))),
        }
    }

    async fn report_transfer(
        &self,
        request: &ReportCollectiveTransferRequest,
    ) -> CollectiveResult<CollectiveTransfer> {
        let group = self.read_group(&request.group_id).await?;
        let mut script = REPORT_TRANSFER_SCRIPT.prepare_invoke();
        script.key(operation_key(&request.operation_id));
        script.key(reported_key(&request.operation_id));
        script.key(group_key(&request.group_id));
        script.key(participants_key(&request.group_id));
        script.key(active_ops_key(&request.group_id));
        for lane in &group.lanes {
            script.key(lane_key(&request.group_id, lane.lane_id));
        }
        let outcome: String = script
            .arg(&request.operation_id)
            .arg(&request.group_id)
            .arg(request.epoch)
            .arg(&request.worker_id)
            .arg(i32::from(request.succeeded))
            .arg(&request.message)
            .invoke_async(&mut self.connection.clone())
            .await
            .map_err(redis_error)?;

        match outcome.as_str() {
            "NOTFOUND" => Err(CollectiveBackendError::NotFound(format!(
                "collective transfer {} was not found",
                request.operation_id
            ))),
            "WRONGGROUP" => Err(CollectiveBackendError::InvalidArgument(
                "the report names a different group than the operation".to_string(),
            )),
            "NOTADMITTED" => Err(CollectiveBackendError::FailedPrecondition(format!(
                "worker {} is not an admitted generation of this group",
                request.worker_id
            ))),
            "NOTREADY" => Err(CollectiveBackendError::FailedPrecondition(format!(
                "collective group {} is not READY for reports",
                request.group_id
            ))),
            other => {
                if let Some(operation_epoch) = other.strip_prefix("OPSTALE:") {
                    return Err(CollectiveBackendError::FailedPrecondition(format!(
                        "report for epoch {} does not match the operation epoch {operation_epoch}",
                        request.epoch
                    )));
                }
                if let Some(current) = other.strip_prefix("STALE:") {
                    return Err(CollectiveBackendError::FailedPrecondition(format!(
                        "report for epoch {} was rejected; the group is at epoch {current}",
                        request.epoch
                    )));
                }
                if other.starts_with("OK:") {
                    self.read_transfer(&request.operation_id).await
                } else {
                    Err(CollectiveBackendError::Internal(format!(
                        "unexpected report outcome {other}"
                    )))
                }
            }
        }
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use modelexpress_common::grpc::refit_collective::LaneSpec;

    /// One reshard lane per `lane_count`, splitting the trainers evenly across
    /// them, plus a broadcast lane spanning everyone. The split is the TEST's
    /// choice: nothing in MX computes it.
    fn spec(
        model: &str,
        trainers: &[&str],
        generators: &[&str],
        lane_count: u32,
    ) -> CollectiveGroupSpec {
        let trainer_slots: Vec<String> = trainers.iter().map(|s| (*s).to_string()).collect();
        let generator_slots: Vec<String> = generators.iter().map(|s| (*s).to_string()).collect();
        let per_lane = trainer_slots.len().div_ceil(lane_count.max(1) as usize);
        let mut lanes: Vec<LaneSpec> = trainer_slots
            .chunks(per_lane.max(1))
            .enumerate()
            .map(|(index, chunk)| LaneSpec {
                lane_id: u32::try_from(index).unwrap_or(0),
                kind: LaneKind::Reshard.into(),
                trainer_slots: chunk.to_vec(),
                generator_slots: generator_slots.clone(),
            })
            .collect();
        lanes.push(LaneSpec {
            lane_id: u32::try_from(lanes.len()).unwrap_or(0),
            kind: LaneKind::Broadcast.into(),
            trainer_slots: trainer_slots.clone(),
            generator_slots: generator_slots.clone(),
        });
        CollectiveGroupSpec {
            model_name: model.to_string(),
            expected_trainer_slots: trainer_slots,
            expected_generator_slots: generator_slots,
            lanes,
        }
    }

    /// The `local function` names one Lua chunk defines.
    fn lua_function_names(source: &str) -> Vec<&str> {
        source
            .lines()
            .filter_map(|line| line.strip_prefix("local function "))
            .filter_map(|rest| rest.split('(').next())
            .collect()
    }

    #[test]
    fn group_scripts_share_one_copy_of_the_common_helpers() {
        // Two drift bugs came from these helpers being copied per script. Each
        // script that uses them gets common.lua prepended and must not carry
        // its own definition of any of them again.
        let common = include_str!("redis/scripts/common.lua");
        let shared = lua_function_names(common);
        assert!(shared.contains(&"parse_participant"));
        assert!(shared.contains(&"registration_matches"));
        assert!(shared.contains(&"evaluate_readiness"));
        for (name, built, own) in [
            (
                "join",
                JOIN_GROUP_LUA,
                include_str!("redis/scripts/join_collective_group.lua"),
            ),
            (
                "publish",
                PUBLISH_BOOTSTRAP_LUA,
                include_str!("redis/scripts/publish_group_bootstrap.lua"),
            ),
            (
                "report",
                REPORT_TRANSFER_LUA,
                include_str!("redis/scripts/report_collective_transfer.lua"),
            ),
            (
                "refresh",
                REFRESH_GROUP_LUA,
                include_str!("redis/scripts/refresh_collective_group.lua"),
            ),
        ] {
            assert!(
                built.starts_with(common),
                "{name} is built without common.lua"
            );
            assert!(built.ends_with(own), "{name} is built without its own body");
            for function in lua_function_names(own) {
                assert!(
                    !shared.contains(&function),
                    "{name} redefines the shared helper {function}"
                );
            }
        }
    }

    #[test]
    fn group_id_is_stable_under_membership_ordering() {
        // Workers enumerate their peers in whatever order the framework hands
        // them over; declaring the same MEMBERSHIP must still land on one
        // group, so the expected-slot lists are order insensitive.
        let mut a = spec("m", &["t0", "t1"], &["g0", "g1"], 1);
        let mut b = spec("m", &["t0", "t1"], &["g0", "g1"], 1);
        a.expected_trainer_slots.reverse();
        b.expected_generator_slots.reverse();
        assert_eq!(group_id_for(&a), group_id_for(&b));
    }

    #[test]
    fn group_id_separates_lanes_that_order_their_ranks_differently() {
        // Lane order is NOT membership: it is the rank assignment. Two callers
        // that put different slots at rank 0 need different communicators, so
        // they must not resolve to the same group and silently disagree.
        let a = spec("m", &["t0", "t1"], &["g0"], 1);
        let mut b = spec("m", &["t0", "t1"], &["g0"], 1);
        for lane in &mut b.lanes {
            lane.trainer_slots.reverse();
        }
        assert_ne!(group_id_for(&a), group_id_for(&b));
    }

    #[test]
    fn group_id_is_stable_under_lane_declaration_order() {
        // The lane VECTOR's order is not membership either. Two participants
        // that list the same lanes in a different order must resolve one
        // group; resolving two would leave each below its expected count and
        // report that as a missing-slot timeout.
        let a = spec("m", &["t0", "t1"], &["g0"], 2);
        let mut b = spec("m", &["t0", "t1"], &["g0"], 2);
        b.lanes.reverse();
        assert_eq!(group_id_for(&a), group_id_for(&b));
    }

    #[test]
    fn group_id_separates_different_lane_counts() {
        assert_ne!(
            group_id_for(&spec("m", &["t0", "t1"], &["g0"], 1)),
            group_id_for(&spec("m", &["t0", "t1"], &["g0"], 2))
        );
    }

    #[test]
    fn group_id_separates_distinct_memberships() {
        let base = spec("m", &["t0", "t1"], &["g0"], 1);
        assert_ne!(
            group_id_for(&base),
            group_id_for(&spec("other", &["t0", "t1"], &["g0"], 1))
        );
        // A different admitted generator subset is a different communicator.
        assert_ne!(
            group_id_for(&base),
            group_id_for(&spec("m", &["t0", "t1"], &["g0", "g1"], 1))
        );
        assert_ne!(
            group_id_for(&base),
            group_id_for(&spec("m", &["t0", "t1"], &["g0"], 2))
        );
    }

    #[test]
    fn group_id_does_not_collide_across_the_role_boundary() {
        // Without a separator between the two slot lists, moving a name from
        // one role to the other would hash identically.
        assert_ne!(
            group_id_for(&spec("m", &["a", "b"], &["c"], 1)),
            group_id_for(&spec("m", &["a"], &["b", "c"], 1))
        );
    }

    #[test]
    fn hex_round_trips_a_bootstrap_identifier() {
        let id: Vec<u8> = (0..128u32).map(|i| u8::try_from(i).unwrap_or(0)).collect();
        assert_eq!(hex_decode(&hex_encode(&id)).expect("round trip"), id);
    }

    #[test]
    fn malformed_bootstrap_identifiers_are_rejected() {
        assert!(hex_decode("abc").is_err());
        assert!(hex_decode("zz").is_err());
    }

    #[test]
    fn participant_records_round_trip() {
        let trainer = participant_from_record("t0", "w1|TRAINER|3|7").expect("trainer record");
        assert_eq!(trainer.worker_id, "w1");
        assert_eq!(trainer.index_in_role, 3);
        assert_eq!(trainer.joined_epoch, 7);

        let generator = participant_from_record("g0", "w2|GENERATOR|0|7").expect("generator");
        assert_eq!(generator.worker_id, "w2");

        assert!(participant_from_record("x", "w|BOGUS|0|7").is_err());
        // A record has exactly four fields; anything longer is refused rather
        // than parsed from a prefix.
        assert!(participant_from_record("x", "w|TRAINER|3|1|7").is_err());
    }

    #[test]
    fn stored_states_decode_or_are_refused() {
        for (text, state) in [
            ("FORMING", CollectiveGroupState::Forming),
            ("READY", CollectiveGroupState::Ready),
        ] {
            assert_eq!(group_state_from_str(text).expect("group state"), state);
        }
        for (text, state) in [
            ("PENDING", CollectiveTransferState::Pending),
            ("RUNNING", CollectiveTransferState::Running),
            ("COMPLETE", CollectiveTransferState::Complete),
            ("FAILED", CollectiveTransferState::Failed),
            ("ABORTED", CollectiveTransferState::Aborted),
        ] {
            assert_eq!(
                transfer_state_from_str(text).expect("transfer state"),
                state
            );
        }
        // A corrupt or newer record is an error, not a silent initial state.
        // RELEASING was never written and is no longer a group state.
        for text in ["", "forming", "BOGUS", "RELEASING"] {
            assert!(matches!(
                group_state_from_str(text),
                Err(CollectiveBackendError::Internal(_))
            ));
        }
        for text in ["", "pending", "BOGUS"] {
            assert!(matches!(
                transfer_state_from_str(text),
                Err(CollectiveBackendError::Internal(_))
            ));
        }
    }

    #[test]
    fn the_declared_lane_set_round_trips_through_the_group_hash() {
        let lanes = vec![
            Lane {
                lane_id: 0,
                kind: LaneKind::Reshard,
                trainer_slots: vec!["t0".to_string(), "t1".to_string()],
                generator_slots: vec!["g0".to_string()],
            },
            Lane {
                lane_id: 9,
                kind: LaneKind::Broadcast,
                trainer_slots: vec!["t0".to_string(), "t1".to_string()],
                generator_slots: vec!["g0".to_string()],
            },
        ];
        let decoded = decode_lanes(&encode_lanes(&lanes)).expect("round trip");
        assert_eq!(decoded, lanes);
        assert_eq!(decode_lanes("").expect("empty"), Vec::new());
        assert!(decode_lanes("0|1|t0|g0|extra").is_err());
        assert!(decode_lanes("notanumber|1|t0|g0").is_err());
    }

    #[tokio::test]
    #[ignore = "requires Redis at MX_TEST_REDIS_URL"]
    async fn forming_group_with_an_active_operation_refreshes() {
        // KEYS[4] of the refresh script is the active-operations SET. The
        // bootstrap scan of a FORMING group must start at the lane hashes, or
        // HGET on that set fails with WRONGTYPE once an operation is active.
        let redis_url = std::env::var("MX_TEST_REDIS_URL")
            .unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
        let backend = RedisCollectiveBackend::connect(&redis_url)
            .await
            .expect("connect");
        let model = format!("refresh-active-op-test/{}", Uuid::new_v4());
        let worker_id = Uuid::new_v4().to_string();
        let mut connection = backend.connection.clone();
        let () = connection
            .hset_multiple(
                worker_key(&worker_id),
                &[
                    ("worker_id", worker_id.as_str()),
                    ("role", "1"),
                    ("model_name", model.as_str()),
                ],
            )
            .await
            .expect("register worker");
        let () = connection
            .expire(worker_key(&worker_id), 60)
            .await
            .expect("expire registration");

        // Two slots expected, one joins: the group stays FORMING.
        let group_spec = spec(&model, &["t0"], &["g0"], 1);
        backend
            .join_group(&JoinCollectiveGroupRequest {
                spec: Some(group_spec.clone()),
                slot_id: "t0".to_string(),
                worker_id: worker_id.clone(),
                role: CollectiveRole::Trainer.into(),
                index_in_role: 0,
                plan_digest: "digest".to_string(),
                plan_source: None,
            })
            .await
            .expect("join");

        // A FORMING group refuses a create, so seed the in-flight operation
        // the way an epoch-1 create on a since-demoted group would leave it.
        let group_id = group_id_for(&group_spec);
        let operation_id = Uuid::new_v4().simple().to_string();
        let () = connection
            .hset_multiple(
                operation_key(&operation_id),
                &[
                    ("operation_id", operation_id.as_str()),
                    ("group_id", group_id.as_str()),
                    ("epoch", "1"),
                    ("state", "PENDING"),
                ],
            )
            .await
            .expect("seed operation");
        let () = connection
            .sadd(active_ops_key(&group_id), &operation_id)
            .await
            .expect("seed active operation");
        let active: u64 = connection
            .scard(active_ops_key(&group_id))
            .await
            .expect("scard");
        assert_eq!(active, 1, "the transfer must be tracked as active");

        let group = backend.get_group(&group_id).await.expect("refresh");
        assert_eq!(group.state, i32::from(CollectiveGroupState::Forming));
    }

    #[tokio::test]
    #[ignore = "requires Redis at MX_TEST_REDIS_URL"]
    async fn recreating_an_evicted_group_aborts_the_old_incarnations_operations() {
        // If only the root hash is lost, a join recreates the group at epoch 1.
        // An operation left in the active set from the old incarnation must
        // not be completed by reports from the new cohort at that same epoch.
        let backend = RedisCollectiveBackend::connect(&test_redis_url())
            .await
            .expect("connect");
        let model = format!("recreate-group-test/{}", Uuid::new_v4());
        let trainer = Uuid::new_v4().to_string();
        register_worker(&backend, &trainer, CollectiveRole::Trainer, &model).await;
        let group_spec = spec(&model, &["t0"], &["g0"], 1);
        join(
            &backend,
            &group_spec,
            "t0",
            &trainer,
            CollectiveRole::Trainer,
            "d1",
        )
        .await;

        let group_id = group_id_for(&group_spec);
        let operation_id = Uuid::new_v4().simple().to_string();
        let mut connection = backend.connection.clone();
        let () = connection
            .hset_multiple(
                operation_key(&operation_id),
                &[
                    ("operation_id", operation_id.as_str()),
                    ("group_id", group_id.as_str()),
                    ("epoch", "1"),
                    ("state", "PENDING"),
                ],
            )
            .await
            .expect("seed operation");
        let () = connection
            .sadd(active_ops_key(&group_id), &operation_id)
            .await
            .expect("seed active operation");

        let () = connection
            .del(group_key(&group_id))
            .await
            .expect("evict root hash");
        join(
            &backend,
            &group_spec,
            "t0",
            &trainer,
            CollectiveRole::Trainer,
            "d1",
        )
        .await;

        let group = backend.get_group(&group_id).await.expect("get group");
        assert_eq!(group.epoch, 1, "the recreated group starts over at epoch 1");
        let state: String = connection
            .hget(operation_key(&operation_id), "state")
            .await
            .expect("operation state");
        assert_eq!(state, "ABORTED");
        let active: u64 = connection
            .scard(active_ops_key(&group_id))
            .await
            .expect("scard");
        assert_eq!(active, 0, "the old incarnation's operation is not active");
    }

    async fn register_worker(
        backend: &RedisCollectiveBackend,
        worker_id: &str,
        role: CollectiveRole,
        model: &str,
    ) {
        let role_code = if role == CollectiveRole::Trainer {
            "1"
        } else {
            "2"
        };
        let mut connection = backend.connection.clone();
        let () = connection
            .hset_multiple(
                worker_key(worker_id),
                &[
                    ("worker_id", worker_id),
                    ("role", role_code),
                    ("model_name", model),
                ],
            )
            .await
            .expect("register worker");
        let () = connection
            .expire(worker_key(worker_id), 60)
            .await
            .expect("expire registration");
    }

    async fn join(
        backend: &RedisCollectiveBackend,
        group_spec: &CollectiveGroupSpec,
        slot_id: &str,
        worker_id: &str,
        role: CollectiveRole,
        plan_digest: &str,
    ) {
        backend
            .join_group(&JoinCollectiveGroupRequest {
                spec: Some(group_spec.clone()),
                slot_id: slot_id.to_string(),
                worker_id: worker_id.to_string(),
                role: role.into(),
                index_in_role: 0,
                plan_digest: plan_digest.to_string(),
                plan_source: None,
            })
            .await
            .expect("join");
    }

    fn test_redis_url() -> String {
        std::env::var("MX_TEST_REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string())
    }

    #[tokio::test]
    #[ignore = "requires Redis at MX_TEST_REDIS_URL"]
    async fn participants_report_the_epoch_they_joined_at() {
        // t0 rejoins with a new plan, which opens epoch 2. g0 is still on its
        // epoch 1 record, and the group must say so, or a client sees every
        // slot present and cannot tell who has to rejoin.
        let backend = RedisCollectiveBackend::connect(&test_redis_url())
            .await
            .expect("connect");
        let model = format!("joined-epoch-test/{}", Uuid::new_v4());
        let trainer = Uuid::new_v4().to_string();
        let generator = Uuid::new_v4().to_string();
        register_worker(&backend, &trainer, CollectiveRole::Trainer, &model).await;
        register_worker(&backend, &generator, CollectiveRole::Generator, &model).await;
        let group_spec = spec(&model, &["t0"], &["g0"], 1);
        join(
            &backend,
            &group_spec,
            "t0",
            &trainer,
            CollectiveRole::Trainer,
            "d1",
        )
        .await;
        join(
            &backend,
            &group_spec,
            "g0",
            &generator,
            CollectiveRole::Generator,
            "d1",
        )
        .await;
        join(
            &backend,
            &group_spec,
            "t0",
            &trainer,
            CollectiveRole::Trainer,
            "d2",
        )
        .await;

        let group = backend
            .get_group(&group_id_for(&group_spec))
            .await
            .expect("get group");
        assert_eq!(group.epoch, 2);
        let broadcast = group
            .lanes
            .iter()
            .find(|lane| lane.kind == i32::from(LaneKind::Broadcast))
            .expect("broadcast lane");
        let joined: HashMap<&str, u64> = broadcast
            .participants
            .iter()
            .map(|p| (p.slot_id.as_str(), p.joined_epoch))
            .collect();
        assert_eq!(joined.get("t0"), Some(&2));
        assert_eq!(joined.get("g0"), Some(&1));
    }

    fn create_request(
        group_spec: &CollectiveGroupSpec,
        version: &str,
        key: &str,
    ) -> CreateCollectiveTransferRequest {
        create_request_at(group_spec, version, key, 1)
    }

    fn create_request_at(
        group_spec: &CollectiveGroupSpec,
        version: &str,
        key: &str,
        expected_epoch: u64,
    ) -> CreateCollectiveTransferRequest {
        CreateCollectiveTransferRequest {
            spec: Some(group_spec.clone()),
            version_id: version.to_string(),
            idempotency_key: key.to_string(),
            expected_epoch,
        }
    }

    /// Join t0 and g0 and publish both lanes' bootstraps: READY at epoch 1.
    async fn ready_group(backend: &RedisCollectiveBackend, test: &str) -> CollectiveGroupSpec {
        let model = format!("{test}/{}", Uuid::new_v4());
        let trainer = Uuid::new_v4().to_string();
        let generator = Uuid::new_v4().to_string();
        register_worker(backend, &trainer, CollectiveRole::Trainer, &model).await;
        register_worker(backend, &generator, CollectiveRole::Generator, &model).await;
        let group_spec = spec(&model, &["t0"], &["g0"], 1);
        join(
            backend,
            &group_spec,
            "t0",
            &trainer,
            CollectiveRole::Trainer,
            "d",
        )
        .await;
        join(
            backend,
            &group_spec,
            "g0",
            &generator,
            CollectiveRole::Generator,
            "d",
        )
        .await;
        let group_id = group_id_for(&group_spec);
        for (lane_id, fill) in [(0, 1u8), (1, 2u8)] {
            backend
                .publish_bootstrap(&PublishGroupBootstrapRequest {
                    group_id: group_id.clone(),
                    epoch: 1,
                    lane_id,
                    worker_id: trainer.clone(),
                    nccl_unique_id: vec![fill; 128],
                })
                .await
                .expect("publish lane bootstrap");
        }
        let group = backend.get_group(&group_id).await.expect("get group");
        assert_eq!(group.state, i32::from(CollectiveGroupState::Ready));
        assert_eq!(group.epoch, 1);
        group_spec
    }

    /// Assert a refused create left neither an operation nor a reservation.
    async fn assert_nothing_admitted(
        backend: &RedisCollectiveBackend,
        group_spec: &CollectiveGroupSpec,
        key: &str,
    ) {
        let mut connection = backend.connection.clone();
        let active: u64 = connection
            .scard(active_ops_key(&group_id_for(group_spec)))
            .await
            .expect("scard");
        assert_eq!(active, 0, "a refused create must not leave an operation");
        let reserved: bool = connection
            .exists(operation_idempotency_key(&group_spec.model_name, key))
            .await
            .expect("exists");
        assert!(!reserved, "a refused create must not leave a reservation");
    }

    #[tokio::test]
    #[ignore = "requires Redis at MX_TEST_REDIS_URL"]
    async fn create_is_admitted_only_at_the_ready_epoch_it_names() {
        let backend = RedisCollectiveBackend::connect(&test_redis_url())
            .await
            .expect("connect");

        // FORMING: one of two slots joined. Even the current epoch is refused.
        let model = format!("create-forming-test/{}", Uuid::new_v4());
        let trainer = Uuid::new_v4().to_string();
        register_worker(&backend, &trainer, CollectiveRole::Trainer, &model).await;
        let forming = spec(&model, &["t0"], &["g0"], 1);
        join(
            &backend,
            &forming,
            "t0",
            &trainer,
            CollectiveRole::Trainer,
            "d",
        )
        .await;
        let key = Uuid::new_v4().to_string();
        match backend
            .create_transfer(&create_request_at(&forming, "v1", &key, 1))
            .await
        {
            Err(CollectiveBackendError::FailedPrecondition(_)) => {}
            other => panic!("expected FailedPrecondition while FORMING, got {other:?}"),
        }
        assert_nothing_admitted(&backend, &forming, &key).await;

        // READY at epoch 1, but the caller names another epoch.
        let group_spec = ready_group(&backend, "create-epoch-test").await;
        for stale in [2, 7] {
            let key = Uuid::new_v4().to_string();
            match backend
                .create_transfer(&create_request_at(&group_spec, "v1", &key, stale))
                .await
            {
                Err(CollectiveBackendError::FailedPrecondition(_)) => {}
                other => panic!("expected FailedPrecondition at epoch {stale}, got {other:?}"),
            }
            assert_nothing_admitted(&backend, &group_spec, &key).await;
        }

        // No epoch at all is an invalid request, not a guess at the current one.
        let key = Uuid::new_v4().to_string();
        match backend
            .create_transfer(&create_request_at(&group_spec, "v1", &key, 0))
            .await
        {
            Err(CollectiveBackendError::InvalidArgument(_)) => {}
            other => panic!("expected InvalidArgument without an epoch, got {other:?}"),
        }
        assert_nothing_admitted(&backend, &group_spec, &key).await;

        // The READY epoch itself is admitted.
        let created = backend
            .create_transfer(&create_request_at(&group_spec, "v1", &key, 1))
            .await
            .expect("create at the READY epoch");
        assert_eq!(created.epoch, 1);
    }

    #[tokio::test]
    #[ignore = "requires Redis at MX_TEST_REDIS_URL"]
    async fn one_transfer_in_flight_per_group_epoch() {
        let backend = RedisCollectiveBackend::connect(&test_redis_url())
            .await
            .expect("connect");
        let group_spec = ready_group(&backend, "one-in-flight-test").await;

        // A split cohort: two halves create concurrently under different keys
        // at the same epoch. Exactly one may own the epoch.
        let key_a = Uuid::new_v4().to_string();
        let key_b = Uuid::new_v4().to_string();
        let request_a = create_request(&group_spec, "v1", &key_a);
        let request_b = create_request(&group_spec, "v1", &key_b);
        let (a, b) = tokio::join!(
            backend.create_transfer(&request_a),
            backend.create_transfer(&request_b),
        );
        let (winner, loser) = match (a, b) {
            (Ok(op), Err(error)) | (Err(error), Ok(op)) => (op, error),
            other => panic!("expected exactly one create to succeed, got {other:?}"),
        };
        match loser {
            CollectiveBackendError::FailedPrecondition(message) => {
                assert!(message.contains(&winner.operation_id), "{message}");
            }
            other => panic!("expected FailedPrecondition, got {other:?}"),
        }

        // A later different-key create is refused too, while the winner's own
        // key still replays to the same operation.
        let request_c = create_request(&group_spec, "v2", &Uuid::new_v4().to_string());
        assert!(matches!(
            backend.create_transfer(&request_c).await,
            Err(CollectiveBackendError::FailedPrecondition(_))
        ));
        let replay = backend
            .create_transfer(&create_request(&group_spec, "v1", &winner.idempotency_key))
            .await
            .expect("same-key replay");
        assert_eq!(replay.operation_id, winner.operation_id);

        // Once the winner is terminal the epoch is free again.
        let mut connection = backend.connection.clone();
        let () = connection
            .hset(operation_key(&winner.operation_id), "state", "COMPLETE")
            .await
            .expect("mark complete");
        backend
            .create_transfer(&request_c)
            .await
            .expect("create after the first is terminal");
    }
}