flodl 0.7.0

floDl — a flow-graph deep learning framework built on libtorch
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
//! Heartbeat + dead-rank detection tests.

use super::*;

// -----------------------------------------------------------------
// Heartbeat + dead-rank detection
// -----------------------------------------------------------------

#[test]
fn heartbeat_stale_declares_rank_dead_and_unblocks_should_average() {
    // 3 ranks share a DeadRanks ledger with the coord. Ranks 0 and
    // 1 each send a Batch + SyncAck (a complete averaging cycle).
    // Rank 2 handshakes the control channel but emits no frames
    // at all — its `last_heartbeat` slot never updates past the
    // initial `Instant::now()` from coord construction, so once
    // the configured `heartbeat_timeout_secs` elapses `check_dead_ranks`
    // marks rank 2 dead. `should_average`'s `.filter(!is_dead)`
    // then lets the cycle fire with just ranks 0 & 1, and
    // `poll_cpu_averaging`'s "all-alive-acked" gate finalizes.
    let world_size = 3;
    let dead_ranks = crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            ClusterCoordinatorConfig::new(
                ApplyPolicy::Sync,
                AverageBackend::Cpu,
                world_size,
                ElChe::new(world_size, 1),
            )
            .no_divergence_guard()
            .dead_ranks(dead_for_coord)
            // 1-second window so the test runs in ~1.5s rather than
            // the 30s production default.
            .heartbeat_timeout_secs(1)
        },
        |coord| {
            let start = Instant::now();
            while coord.avg_count() == 0 {
                if start.elapsed() > Duration::from_secs(10) {
                    return Err(TensorError::new(
                        "heartbeat_stale: avg_count never advanced",
                    ));
                }
                coord.tick()?;
                thread::sleep(Duration::from_millis(20));
            }
            assert!(
                coord.avg_count() >= 1,
                "cycle finalized with surviving ranks"
            );
            Ok(())
        },
    );

    // Rank 2: handshake only, then sleep without sending any
    // frames. Coord's heartbeat-staleness check will trigger.
    // Keep alive long enough for the cycle to complete.
    let dead_for_assertion = Arc::clone(&dead_ranks);
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, move |_s, _salt| {
        thread::sleep(Duration::from_millis(3500));
        Ok(())
    });

    let body = |rank: u64| {
        move |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            send_timing(
                s,
                salt,
                TimingMsgWire::Batch {
                    rank,
                    batch_ms: 10.0, data_ms: 0.0,
                    step_count: 1,
                    param_norm: None,
                    batch_loss: 0.5,
                    sync_divergence: None,
                },
            )?;
            // Keepalive: this wait spans the dead-rank-detection window
            // (RequestParams only arrives after rank 2 is reaped), so
            // heartbeat through it or the coord reaps this survivor too.
            let _ = recv_control_keepalive(s, salt, rank, 1)?; // RequestParams
            send_timing(
                s,
                salt,
                TimingMsgWire::SyncAck {
                    rank,
                    step_count: 2,
                    divergence: Some(0.05),
                    post_norm: Some(1.0),
                    pre_norm: Some(1.05),
                },
            )?;
            let _ = recv_control_keepalive(s, salt, rank, 2)?; // Update
            let _ = recv_control_keepalive(s, salt, rank, 2)?; // SetGlobalStep
            Ok(())
        }
    };
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, body(0));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, body(1));
    r0.join().unwrap().expect("rank 0 completes averaging");
    r1.join().unwrap().expect("rank 1 completes averaging");
    let _ = r2.join();
    coord_handle.join().unwrap().expect("coord drives clean");

    // Post-hoc invariant: the shared ledger registers rank 2 dead.
    // Don't assert about ranks 0/1: by the time the test's r2
    // sleep elapses (3.5s) they've been silent past the 1s
    // threshold too and may also have been declared dead. That's
    // not a regression — it's the heartbeat detector doing its
    // job on what looks like additional dead ranks once the run
    // is wrapping up. The load-bearing invariant is that rank 2
    // (the silent one DURING the cycle) was detected as dead.
    assert!(
        dead_for_assertion.is_dead(2),
        "rank 2 must be flagged dead in shared ledger"
    );
}

#[test]
fn dead_rank_remainder_redistributed_via_extend_partition() {
    // 3-rank Sync+Cpu setup with dispatched epoch + shared
    // DeadRanks ledger + 1s heartbeat timeout. Ranks 0 + 1 send
    // ONE Batch each (representing one batch of training before
    // the failure), then rank 2 goes silent. The coord's
    // heartbeat-stale check declares rank 2 dead; the
    // redistribution path computes rank 2's un-processed
    // remainder (its full partition: 0 batches processed) and
    // emits one ExtendPartition frame to each survivor. The
    // survivors receive the frames over the wire.
    //
    // Invariant: total samples reshard = rank 2's partition size.
    // Each survivor's received slice sums with the others to
    // exactly that count — no samples lost, no samples duplicated.
    let world_size = 3;
    let total_samples = 30;
    let batch_size = 1;
    let dead_ranks = crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            ClusterCoordinatorConfig::new(
                ApplyPolicy::Sync,
                AverageBackend::Cpu,
                world_size,
                ElChe::new(world_size, 1),
            )
            .no_divergence_guard()
            .dead_ranks(dead_for_coord)
            .heartbeat_timeout_secs(1)
            .total_samples(total_samples)
            .batch_size(batch_size)
            .num_epochs(1)
        },
        |coord| {
            coord.dispatch_epoch(0)?;
            let start = Instant::now();
            while !coord.dead_ranks.as_ref().unwrap().is_dead(2) {
                if start.elapsed() > Duration::from_secs(5) {
                    return Err(TensorError::new(
                        "dead_rank_redistribute: rank 2 never declared dead",
                    ));
                }
                coord.tick()?;
                thread::sleep(Duration::from_millis(20));
            }
            Ok(())
        },
    );

    // Ranks 0 + 1 collect StartEpoch + count any ExtendPartition
    // they receive (size). Returned through a shared atomic
    // because fake_rank's body returns Result<()>.
    use std::sync::atomic::AtomicU64;
    let r0_extension = Arc::new(AtomicU64::new(0));
    let r1_extension = Arc::new(AtomicU64::new(0));
    let r0_acc = Arc::clone(&r0_extension);
    let r1_acc = Arc::clone(&r1_extension);

    let make_alive = |rank: u64, acc: Arc<AtomicU64>| {
        move |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            let mut received_start_epoch = false;
            let read_deadline = Instant::now() + Duration::from_secs(4);
            while Instant::now() < read_deadline {
                // Heartbeat each poll so the coord doesn't reap this
                // survivor while it waits for its ExtendPartition — that
                // frame only arrives after rank 2 is detected dead (~1s),
                // well past the 1s heartbeat timeout. (send errors mean the
                // peer is already gone; the recv below surfaces the EOF.)
                let _ = send_timing(s, salt, TimingMsgWire::Heartbeat { rank, step_count: 1 });
                s.set_read_timeout(Some(Duration::from_millis(200))).ok();
                match recv_frame(s, salt) {
                    Ok(Some(frame)) => match frame.decode::<ControlMsgWire>() {
                        Ok(ControlMsgWire::StartEpoch(_)) => {
                            received_start_epoch = true;
                            send_timing(
                                s,
                                salt,
                                TimingMsgWire::Batch {
                                    rank,
                                    batch_ms: 5.0, data_ms: 0.0,
                                    step_count: 1,
                                    param_norm: None,
                                    batch_loss: 0.1,
                                    sync_divergence: None,
                                },
                            )?;
                        }
                        Ok(ControlMsgWire::ExtendPartition {
                            partition_size,
                            ..
                        }) => {
                            acc.fetch_add(partition_size, Ordering::SeqCst);
                        }
                        Ok(_other) => {
                            // RequestParams / SetGlobalStep / etc.
                        }
                        Err(_) => break,
                    },
                    Ok(None) => break,
                    Err(_) => continue,
                }
            }
            assert!(received_start_epoch, "rank {rank} got StartEpoch");
            Ok(())
        }
    };

    // Rank 2 handshakes then sleeps — coord will declare it dead.
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, |_s, _salt| {
        thread::sleep(Duration::from_millis(3500));
        Ok(())
    });
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, make_alive(0, r0_acc));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, make_alive(1, r1_acc));

    r0.join().unwrap().expect("rank 0 path");
    r1.join().unwrap().expect("rank 1 path");
    let _ = r2.join();
    coord_handle.join().unwrap().expect("coord drives clean");

    // Rank 2's partition for a 3-rank, 30-sample equal-split is
    // 10 samples. Since rank 2 sent no Batch (its
    // last_step_count stayed at the epoch-start snapshot),
    // processed_samples = 0, so the entire partition (10) is
    // redistributed across the 2 survivors. Sum across survivors
    // must equal 10 exactly.
    let r0_total = r0_extension.load(Ordering::SeqCst);
    let r1_total = r1_extension.load(Ordering::SeqCst);
    let total_redistributed = r0_total + r1_total;
    assert_eq!(
        total_redistributed, 10,
        "dead rank 2's un-processed remainder (10) must be reshared \
         across survivors; got r0={r0_total}, r1={r1_total}"
    );
}

#[test]
fn dead_ranks_optional_default_disables_elastic_membership() {
    // When `dead_ranks` is None in the config (default), the
    // heartbeat-stale check is a no-op and no rank can be
    // declared dead. Same standard cycle as the basic Sync+CPU
    // test, just verifying the opt-in semantics: forgetting to
    // wire the ledger doesn't accidentally declare ranks dead.
    let world_size = 2;
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            cfg_sync_cpu(world_size).heartbeat_timeout_secs(0)
            // No `.dead_ranks(...)` — elastic membership disabled.
        },
        move |coord| {
            // Drive a cycle; sleep enough that timeout-0 WOULD
            // declare every rank dead if elastic membership were
            // active. Verify nothing fires.
            thread::sleep(Duration::from_millis(50));
            coord.tick()?;
            // Still active; no decrement.
            assert_eq!(
                coord.active_count(),
                world_size,
                "dead-rank detection must be off without ledger"
            );
            Ok(())
        },
    );
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, |_, _| Ok(()));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, |_, _| Ok(()));
    r0.join().unwrap().expect("rank 0 handshake");
    r1.join().unwrap().expect("rank 1 handshake");
    coord_handle.join().unwrap().expect("coord drives clean");
}

#[test]
fn observe_meta_runs_during_averaging_cycle_no_anchor_change_in_probe() {
    // Drive one averaging cycle on Sync+NCCL with meta enabled.
    // Each rank reports its LR + one Batch; coord triggers cycle 1.
    // observe_meta runs inside finish_averaging_nccl but the meta
    // is in Probe phase on the first cycle (no calibration), so
    // it returns Noop and the ElChe anchor stays at 1.
    // This guards the wire integrity: enabling the meta must not
    // crash or spuriously nudge the anchor at startup.
    let world_size = 2;
    let initial_anchor = 1usize;
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || cfg_sync_nccl(world_size).meta_controller(true),
        move |coord| {
            let start = Instant::now();
            while coord.avg_count() == 0 {
                if start.elapsed() > Duration::from_secs(5) {
                    return Err(TensorError::new(
                        "observe_meta_runs: avg_count never advanced",
                    ));
                }
                coord.tick()?;
                thread::sleep(Duration::from_millis(10));
            }
            assert_eq!(
                coord.el_che().anchor(),
                initial_anchor,
                "Probe-phase meta must NOT nudge the anchor on first cycle"
            );
            // LR was observed even though no MetaAction fired.
            let lrs = coord.last_lr_per_rank_for_test();
            assert!(lrs.iter().all(|lr| lr.is_some()), "LRs captured");
            Ok(())
        },
    );
    let body = |rank: u32| {
        let rank = rank as u64;
        move |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            send_timing(
                s,
                salt,
                TimingMsgWire::LrUpdate { rank, lr: 0.01 },
            )?;
            send_timing(
                s,
                salt,
                TimingMsgWire::Batch {
                    rank,
                    batch_ms: 10.0, data_ms: 0.0,
                    step_count: 1,
                    param_norm: None,
                    batch_loss: 1.0,
                    sync_divergence: None,
                },
            )?;
            // Wait for SyncNow + SetGlobalStep then ack with SyncAck.
            let _ = recv_control(s, salt)?;
            send_timing(
                s,
                salt,
                TimingMsgWire::SyncAck {
                    rank,
                    step_count: 2,
                    divergence: Some(0.05),
                    post_norm: Some(1.0),
                    pre_norm: Some(1.05),
                },
            )?;
            let _ = recv_control(s, salt)?; // SetGlobalStep
            Ok(())
        }
    };
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, body(0));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, body(1));
    r0.join().unwrap().expect("rank 0 path");
    r1.join().unwrap().expect("rank 1 path");
    coord_handle.join().unwrap().expect("coord cycle 1 with meta on");
}

#[test]
fn max_failure_threshold_breach_dispatches_shutdown_with_save() {
    // 3-rank CPU cluster, max_failure=Absolute(1). All ranks
    // handshake then go silent — within heartbeat_timeout_secs the
    // first stale-heartbeat detection trips the threshold, the
    // coord broadcasts ShutdownWithSave to every rank, and the
    // dispatched flag flips.
    let world_size = 3;
    let dead_ranks =
        crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            ClusterCoordinatorConfig::new(
                ApplyPolicy::Sync,
                AverageBackend::Cpu,
                world_size,
                ElChe::new(world_size, 1),
            )
            .no_divergence_guard()
            .dead_ranks(dead_for_coord)
            .heartbeat_timeout_secs(1)
            .max_failure(
                crate::distributed::max_failure::MaxFailureThreshold::Absolute(1),
            )
        },
        |coord| {
            let start = Instant::now();
            while !coord.shutdown_with_save_dispatched() {
                if start.elapsed() > Duration::from_secs(10) {
                    return Err(TensorError::new(
                        "max_failure: ShutdownWithSave never dispatched",
                    ));
                }
                coord.tick()?;
                thread::sleep(Duration::from_millis(20));
            }
            Ok(())
        },
    );

    fn drain_shutdown_with_save(
        s: &mut TcpStream,
        salt: &SessionSalt,
    ) -> Result<()> {
        // Bound the wait — the coord's heartbeat_timeout=1s means
        // dispatch lands ~1.0-1.5s after handshake.
        s.set_read_timeout(Some(Duration::from_secs(5)))
            .map_err(|e| TensorError::new(&format!("timeout: {e}")))?;
        let msg = recv_control(s, salt)?;
        match msg {
            ControlMsgWire::ShutdownWithSave { reason } => {
                let r = crate::distributed::SaveReason::from_u8(reason)
                    .expect("known SaveReason variant");
                if r != crate::distributed::SaveReason::MaxFailureExceeded {
                    return Err(TensorError::new(&format!(
                        "expected MaxFailureExceeded, got {r:?}"
                    )));
                }
                Ok(())
            }
            other => Err(TensorError::new(&format!(
                "expected ShutdownWithSave, got {other:?}"
            ))),
        }
    }

    let r0 = fake_rank(
        port,
        0,
        world_size as u32,
        TEST_SALT,
        drain_shutdown_with_save,
    );
    let r1 = fake_rank(
        port,
        1,
        world_size as u32,
        TEST_SALT,
        drain_shutdown_with_save,
    );
    let r2 = fake_rank(
        port,
        2,
        world_size as u32,
        TEST_SALT,
        drain_shutdown_with_save,
    );
    r0.join().unwrap().expect("rank 0 receives ShutdownWithSave");
    r1.join().unwrap().expect("rank 1 receives ShutdownWithSave");
    r2.join().unwrap().expect("rank 2 receives ShutdownWithSave");
    coord_handle.join().unwrap().expect("coord dispatched broadcast");
}

#[test]
fn controller_writes_meta_json_on_shutdown_with_save() {
    // 3-rank cluster with `save_path` configured. Force a
    // max_failure breach so the coord calls
    // `dispatch_shutdown_with_save`. Assert the controller wrote
    // `<save_path>.meta.json` with the expected reason +
    // world_size + ElCheState present. The `.fdl` + `.optim`
    // bundle members are the workers' responsibility (covered by
    // the worker-side `shutdown_with_save_writes_model_and_optim_*`
    // test) — only `.meta.json` is asserted here.
    let world_size = 3;
    let dir = std::env::temp_dir().join(format!(
        "flodl_coord_meta_{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let stem = dir.join("coord_ckpt");
    let stem_str = stem.to_str().unwrap().to_string();

    let dead_ranks =
        crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let stem_for_coord = stem_str.clone();
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            ClusterCoordinatorConfig::new(
                ApplyPolicy::Sync,
                AverageBackend::Cpu,
                world_size,
                ElChe::new(world_size, 3),
            )
            .no_divergence_guard()
            .dead_ranks(dead_for_coord)
            .heartbeat_timeout_secs(1)
            .max_failure(
                crate::distributed::max_failure::MaxFailureThreshold::Absolute(1),
            )
            .save_path(stem_for_coord.clone())
        },
        |coord| {
            let start = Instant::now();
            while !coord.shutdown_with_save_dispatched() {
                if start.elapsed() > Duration::from_secs(10) {
                    return Err(TensorError::new(
                        "coord meta: ShutdownWithSave never dispatched",
                    ));
                }
                coord.tick()?;
                thread::sleep(Duration::from_millis(20));
            }
            Ok(())
        },
    );

    // Fake ranks: handshake, then go silent. Coord's heartbeat
    // timeout (1s) trips max_failure (1) → dispatch_shutdown_with_save
    // fires → meta.json gets written.
    let r0 = fake_rank(
        port,
        0,
        world_size as u32,
        TEST_SALT,
        |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            s.set_read_timeout(Some(Duration::from_secs(5))).ok();
            let _ = recv_control(s, salt)?; // ShutdownWithSave
            Ok(())
        },
    );
    let r1 = fake_rank(
        port,
        1,
        world_size as u32,
        TEST_SALT,
        |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            s.set_read_timeout(Some(Duration::from_secs(5))).ok();
            let _ = recv_control(s, salt)?;
            Ok(())
        },
    );
    let r2 = fake_rank(
        port,
        2,
        world_size as u32,
        TEST_SALT,
        |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            s.set_read_timeout(Some(Duration::from_secs(5))).ok();
            let _ = recv_control(s, salt)?;
            Ok(())
        },
    );
    r0.join().unwrap().expect("rank 0 path");
    r1.join().unwrap().expect("rank 1 path");
    r2.join().unwrap().expect("rank 2 path");
    coord_handle.join().unwrap().expect("coord dispatched");

    let meta_path =
        crate::distributed::CheckpointBundle::meta_path(&stem_str);
    assert!(
        meta_path.exists(),
        "controller meta.json missing at {}",
        meta_path.display(),
    );
    let meta =
        crate::distributed::CheckpointMeta::read_from_file(&meta_path)
            .expect("controller-written meta parses");
    assert_eq!(meta.world_size_at_save, world_size);
    assert_eq!(
        meta.save_reason,
        crate::distributed::SaveReason::MaxFailureExceeded,
    );
    // ElCheState present and reflects coord's ElChe trajectory.
    let state = meta
        .elche_state
        .expect("controller writes elche_state into meta");
    assert_eq!(state.anchor, 3);
    assert_eq!(state.smoothed_ms_per_batch.len(), world_size);

    std::fs::remove_dir_all(&dir).ok();
}

// Rendezvous retry: the seeded generator (rank 0) is marked dead
// via the shared DeadRanks ledger, so `check_rendezvous_timeout`
// fires on the next tick and the coord retries from rank 1. Both
// the wire frame (rank 1 receives RequestNewNcclId) and the
// internal state (pending.generator_rank == 1, tried_generators ==
// [0]) are exercised.
#[test]
fn rendezvous_retry_picks_next_survivor_on_generator_death() {
    let world_size = 3;
    let dead_ranks = crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let dead_for_test = Arc::clone(&dead_ranks);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            cfg_sync_nccl(world_size)
                .dead_ranks(dead_for_coord)
                .heartbeat_timeout_secs(60)
                .rendezvous_timeout_secs(60)
        },
        move |coord| {
            // Seed a pending rendezvous where rank 0 was the
            // initially-picked generator; the retry path must skip
            // it (dead) and reach rank 1 next.
            coord.test_seed_rendezvous_pending(0, vec![0, 1, 2], 0);
            dead_for_test.declare_dead(0);
            coord.tick()?; // fires check_rendezvous_timeout
            assert_eq!(
                coord.rendezvous_pending_generator(),
                Some(1),
                "retry must pick rank 1 (next ascending survivor)"
            );
            assert_eq!(
                coord.rendezvous_tried_generators(),
                vec![0],
                "rank 0 recorded as tried"
            );
            Ok(())
        },
    );

    // Rank 0 mimics having died — handshakes (so the coord's
    // accept loop unblocks) then exits. Coord's send to rank 0
    // happens at seed time, which is before this rank exits; the
    // RETRY send (to rank 1) is what we observe.
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, |_s, _salt| Ok(()));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, move |s, salt| {
        // Expect the retry's RequestNewNcclId from the coord.
        let msg = recv_control(s, salt)?;
        match msg {
            ControlMsgWire::RequestNewNcclId => Ok(()),
            other => Err(TensorError::new(&format!(
                "rank 1 expected RequestNewNcclId, got {other:?}"
            ))),
        }
    });
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, |_s, _salt| Ok(()));

    r0.join().unwrap().expect("rank 0 handshake");
    r1.join().unwrap().expect("rank 1 receives RequestNewNcclId");
    r2.join().unwrap().expect("rank 2 handshake");
    coord_handle.join().unwrap().expect("coord drives clean");
}

// Slow-generator case: the seeded rendezvous's `initiated_at` is
// shifted into the past (10s) beyond a 1s timeout, no rank is
// declared dead. The retry fires on timeout alone.
#[test]
fn rendezvous_retry_fires_on_timeout_without_death() {
    let world_size = 3;
    let dead_ranks = crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            cfg_sync_nccl(world_size)
                .dead_ranks(dead_for_coord)
                .heartbeat_timeout_secs(60)
                .rendezvous_timeout_secs(1)
        },
        move |coord| {
            coord.test_seed_rendezvous_pending(0, vec![0, 1, 2], 10);
            coord.tick()?; // timeout > 1s elapsed → retry
            assert_eq!(
                coord.rendezvous_pending_generator(),
                Some(1),
                "timeout retry must pick the next ascending survivor (rank 0 timed out)"
            );
            assert_eq!(
                coord.rendezvous_tried_generators(),
                vec![0],
                "rank 0 recorded as tried on timeout"
            );
            Ok(())
        },
    );

    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, |_s, _salt| Ok(()));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, move |s, salt| {
        let msg = recv_control(s, salt)?;
        match msg {
            ControlMsgWire::RequestNewNcclId => Ok(()),
            other => Err(TensorError::new(&format!(
                "rank 1 expected RequestNewNcclId on timeout retry, got {other:?}"
            ))),
        }
    });
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, |_s, _salt| Ok(()));

    r0.join().unwrap().expect("rank 0 handshake");
    r1.join().unwrap().expect("rank 1 receives RequestNewNcclId on timeout");
    r2.join().unwrap().expect("rank 2 handshake");
    coord_handle.join().unwrap().expect("coord drives clean");
}

// Exhaustion: the rendezvous's survivor pool is empty (manufactured
// via the test seam to short-circuit the cohort-filter logic), so
// `check_rendezvous_timeout` falls into the no-candidates branch
// and dispatches `ShutdownWithSave` instead of hanging the cohort
// on a rendezvous that can never complete. All three ranks remain
// alive in TCP for the broadcast to land cleanly.
#[test]
fn rendezvous_exhaustion_dispatches_shutdown_with_save() {
    let world_size = 3;

    let dir = std::env::temp_dir().join(format!(
        "flodl_rdv_exhaust_{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let stem = dir.join("ckpt").to_string_lossy().into_owned();

    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            cfg_sync_nccl_with_dataset(world_size, 12)
                .heartbeat_timeout_secs(60)
                .rendezvous_timeout_secs(1)
                .save_path(stem.clone())
        },
        move |coord| {
            // initiated 10s ago → timed_out=true; empty survivor
            // pool → no next candidate → exhaustion branch fires.
            coord.test_seed_rendezvous_pending(
                0,
                Vec::new(),
                10,
            );
            coord.tick()?;
            assert!(
                coord.rendezvous_pending_generator().is_none(),
                "exhausted pool must clear pending"
            );
            assert!(
                coord.shutdown_with_save_dispatched(),
                "exhausted pool must dispatch ShutdownWithSave"
            );
            Ok(())
        },
    );

    // Fake ranks must drain the ShutdownWithSave broadcast — otherwise
    // the coord's send into a closed socket trips a broken-pipe error.
    fn drain_shutdown(s: &mut TcpStream, salt: &SessionSalt) -> Result<()> {
        s.set_read_timeout(Some(Duration::from_secs(5)))
            .map_err(|e| TensorError::new(&format!("timeout: {e}")))?;
        match recv_control(s, salt)? {
            ControlMsgWire::ShutdownWithSave { .. } => Ok(()),
            other => Err(TensorError::new(&format!(
                "expected ShutdownWithSave, got {other:?}"
            ))),
        }
    }
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, drain_shutdown);
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, drain_shutdown);
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, drain_shutdown);
    r0.join().unwrap().expect("rank 0 receives ShutdownWithSave");
    r1.join().unwrap().expect("rank 1 receives ShutdownWithSave");
    r2.join().unwrap().expect("rank 2 receives ShutdownWithSave");
    coord_handle.join().unwrap().expect("coord drives clean");

    std::fs::remove_dir_all(&dir).ok();
}

/// Regression for the epoch-transition stall: after aggregating
/// epoch N (non-progressive Sync), the coord must dispatch epoch
/// N+1; once N+1 == num_epochs it must broadcast `Shutdown`.
/// Without this, workers idle in `wait_for_epoch_plan` after the
/// final `EpochAggregated` and the launcher hangs.
#[test]
fn epoch_transition_dispatches_next_then_shutdowns_at_horizon() {
    let world_size = 2;
    let num_epochs = 2;
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || cfg_sync_cpu(world_size)
            .total_samples(8)
            .batch_size(4)
            .num_epochs(num_epochs),
        move |coord| {
            coord.dispatch_epoch(0)?;
            let start = Instant::now();
            // Drive ticks until the coord observes both ranks have
            // closed (tick returns false). The fix in
            // `drain_metrics_and_aggregate` broadcasts `Shutdown`
            // after the final epoch aggregates; readers see EOF
            // when ranks exit, `is_finished()` flips, alive=false.
            loop {
                if start.elapsed() > Duration::from_secs(10) {
                    return Err(TensorError::new(
                        "coord did not drain within 10s",
                    ));
                }
                if !coord.tick()? {
                    break;
                }
                thread::sleep(Duration::from_millis(5));
            }
            assert_eq!(
                coord.last_aggregated_epoch(),
                Some(num_epochs - 1),
                "both epochs must have aggregated",
            );
            Ok(())
        },
    );

    fn rank_body(
        rank: u64,
        num_epochs: usize,
    ) -> impl Fn(&mut TcpStream, &SessionSalt) -> Result<()> {
        move |s, salt| {
            let mut completed = 0usize;
            let mut saw_shutdown = false;
            while !saw_shutdown {
                let msg = recv_control(s, salt)?;
                match msg {
                    ControlMsgWire::StartEpoch(plan) => {
                        send_metrics(s, salt, MetricsMsgWire {
                            rank,
                            epoch: plan.epoch,
                            avg_loss: 0.5,
                            batches_processed: 2,
                            epoch_ms: 50.0,
                            samples_processed: 4,
                            share_complete_ms: 0.0,
                            compute_only_ms: 50.0,
                            data_starve_ms: 0.0,
                            scalars: std::collections::HashMap::new(),
                            resources: None,
                        })?;
                        completed += 1;
                    }
                    ControlMsgWire::Shutdown
                    | ControlMsgWire::ShutdownWithSave { .. } => {
                        saw_shutdown = true;
                    }
                    // SetEpochCallbackRole / EpochAggregated / any
                    // unrelated control frames are observed but
                    // don't drive state in this regression test.
                    _ => {}
                }
            }
            if completed != num_epochs {
                return Err(TensorError::new(&format!(
                    "rank {rank}: received Shutdown after {completed} epochs \
                     (expected {num_epochs})",
                )));
            }
            Ok(())
        }
    }
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT,
        rank_body(0, num_epochs));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT,
        rank_body(1, num_epochs));
    r0.join().unwrap().expect("rank 0 completed all epochs");
    r1.join().unwrap().expect("rank 1 completed all epochs");
    coord_handle.join().unwrap().expect("coord finishes cleanly");
}

/// Externally-reported death (launcher child supervision) takes the
/// SAME side-effect chain as heartbeat staleness — but fires on the
/// next tick instead of after the staleness window. Mirror of
/// `heartbeat_stale_rank_declared_dead_and_cycle_completes` with the
/// staleness window set far past the test budget (30s): the ONLY way
/// rank 2 can be declared dead in time is the reported-deaths drain,
/// and the cycle must still finalize with the survivors.
#[test]
fn reported_death_declared_via_drain_and_cycle_completes() {
    let world_size = 3;
    let dead_ranks = crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let reported: crate::distributed::cluster_coordinator::ReportedDeaths =
        Arc::new(std::sync::Mutex::new(Vec::new()));
    let reported_for_coord = Arc::clone(&reported);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            ClusterCoordinatorConfig::new(
                ApplyPolicy::Sync,
                AverageBackend::Cpu,
                world_size,
                ElChe::new(world_size, 1),
            )
            .no_divergence_guard()
            .dead_ranks(dead_for_coord)
            .reported_deaths(reported_for_coord)
            // Staleness must NOT fire inside the test budget — the
            // reported-deaths drain is the only detector in play.
            .heartbeat_timeout_secs(30)
        },
        |coord| {
            let start = Instant::now();
            while coord.avg_count() == 0 {
                if start.elapsed() > Duration::from_secs(10) {
                    return Err(TensorError::new(
                        "reported_death: avg_count never advanced",
                    ));
                }
                coord.tick()?;
                thread::sleep(Duration::from_millis(20));
            }
            Ok(())
        },
    );

    // Rank 2 handshakes then goes silent (simulating the process that
    // just died — its child-exit is what supervision reports).
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, move |_s, _salt| {
        thread::sleep(Duration::from_millis(3500));
        Ok(())
    });

    // The "launcher": report rank 2 dead shortly after the cohort has
    // handshaked. The coord's next tick drains it through
    // process_rank_death.
    let reporter = {
        let q = Arc::clone(&reported);
        thread::spawn(move || {
            thread::sleep(Duration::from_millis(800));
            q.lock().unwrap().push(2);
        })
    };

    let body = |rank: u64| {
        move |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            send_timing(
                s,
                salt,
                TimingMsgWire::Batch {
                    rank,
                    batch_ms: 10.0, data_ms: 0.0,
                    step_count: 1,
                    param_norm: None,
                    batch_loss: 0.5,
                    sync_divergence: None,
                },
            )?;
            let _ = recv_control_keepalive(s, salt, rank, 1)?; // RequestParams
            send_timing(
                s,
                salt,
                TimingMsgWire::SyncAck {
                    rank,
                    step_count: 2,
                    divergence: Some(0.05),
                    post_norm: Some(1.0),
                    pre_norm: Some(1.05),
                },
            )?;
            let _ = recv_control_keepalive(s, salt, rank, 2)?; // Update
            let _ = recv_control_keepalive(s, salt, rank, 2)?; // SetGlobalStep
            Ok(())
        }
    };
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, body(0));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, body(1));
    r0.join().unwrap().expect("rank 0 completes averaging");
    r1.join().unwrap().expect("rank 1 completes averaging");
    let _ = r2.join();
    let _ = reporter.join();
    coord_handle.join().unwrap().expect("coord drives clean");

    assert!(
        dead_ranks.is_dead(2),
        "reported death must land in the shared ledger via the drain"
    );
    assert!(
        !dead_ranks.is_dead(0) && !dead_ranks.is_dead(1),
        "survivors must not be reaped (staleness window is 30s)"
    );
    assert!(
        reported.lock().unwrap().is_empty(),
        "queue must be drained by the tick"
    );
}

/// The `Exiting` latch is CLEAN-completion-only, and it is sharp on
/// both edges:
///
/// - A cleanly-exited rank stops heartbeating by design, so the latch
///   must suppress the staleness detector AND a late/spurious death
///   report for it — one `active_count` decrement, no ledger declare,
///   no bogus DeclareDead broadcast during teardown. This test pins
///   that suppression.
/// - The flip side (the 2026-07-22 cadence-wedge bug): an ERROR exit
///   that sends `Exiting` inherits the same suppression and its death
///   is never processed — no ElChe recompute, no partition
///   redistribution — wedging a cadence cohort on the dead rank's
///   unfinished window. That is why `ClusterWorker::teardown(clean)`
///   only reports `Exiting` on clean completion; error exits stay
///   silent so the reported-death drain (tested above) runs the full
///   death chain.
#[test]
fn exiting_latch_suppresses_late_death_report_exactly_once() {
    let world_size = 3;
    let dead_ranks = crate::distributed::controller::DeadRanks::new(world_size);
    let dead_for_coord = Arc::clone(&dead_ranks);
    let reported: crate::distributed::cluster_coordinator::ReportedDeaths =
        Arc::new(std::sync::Mutex::new(Vec::new()));
    let reported_for_coord = Arc::clone(&reported);
    let (port, coord_handle) = spawn_coord(
        world_size,
        move || {
            ClusterCoordinatorConfig::new(
                ApplyPolicy::Sync,
                AverageBackend::Cpu,
                world_size,
                ElChe::new(world_size, 1),
            )
            .no_divergence_guard()
            .dead_ranks(dead_for_coord)
            .reported_deaths(reported_for_coord)
            // Staleness must NOT fire inside the test budget.
            .heartbeat_timeout_secs(30)
        },
        |coord| {
            // Drive ticks long enough for the Exiting frame and the
            // late death report to both be processed, then assert the
            // single decrement.
            let start = Instant::now();
            while start.elapsed() < Duration::from_secs(2) {
                coord.tick()?;
                thread::sleep(Duration::from_millis(20));
            }
            if coord.active_count() != 2 {
                return Err(TensorError::new(&format!(
                    "active_count must decrement exactly once for a \
                     cleanly-exited rank (Exiting latch), got {} of {}",
                    coord.active_count(),
                    3,
                )));
            }
            Ok(())
        },
    );

    // Rank 2: announce a CLEAN exit, then stay connected (the process
    // may linger through teardown).
    let r2 = fake_rank(port, 2, world_size as u32, TEST_SALT, move |s, salt| {
        send_timing(s, salt, TimingMsgWire::Exiting { rank: 2 })?;
        thread::sleep(Duration::from_millis(2500));
        Ok(())
    });

    // A late death report for the SAME rank (e.g. supervision seeing
    // the process go away after its clean exit): must be swallowed by
    // the latch, not double-processed.
    let reporter = {
        let q = Arc::clone(&reported);
        thread::spawn(move || {
            thread::sleep(Duration::from_millis(600));
            q.lock().unwrap().push(2);
        })
    };

    // Ranks 0 and 1: heartbeat via a Batch frame, then linger.
    let body = |rank: u64| {
        move |s: &mut TcpStream, salt: &SessionSalt| -> Result<()> {
            send_timing(
                s,
                salt,
                TimingMsgWire::Batch {
                    rank,
                    batch_ms: 10.0, data_ms: 0.0,
                    step_count: 1,
                    param_norm: None,
                    batch_loss: 0.5,
                    sync_divergence: None,
                },
            )?;
            thread::sleep(Duration::from_millis(2500));
            Ok(())
        }
    };
    let r0 = fake_rank(port, 0, world_size as u32, TEST_SALT, body(0));
    let r1 = fake_rank(port, 1, world_size as u32, TEST_SALT, body(1));
    let _ = r0.join();
    let _ = r1.join();
    let _ = r2.join();
    let _ = reporter.join();
    coord_handle.join().unwrap().expect("coord drives clean");

    assert!(
        !dead_ranks.is_dead(2),
        "a cleanly-exited rank must never be declared dead by a late \
         report (the drain skips exited ranks)"
    );
    assert!(
        reported.lock().unwrap().is_empty(),
        "the late report must still be drained (consumed, not left queued)"
    );
}