fleche 6.23.0

Remote job runner for Slurm clusters
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
//! Job execution operations - running and re-running jobs on remote clusters.

use crate::config::{Config, ResolvedJob, SlurmConfig, reject_empty_path_entries};
use crate::error::{FlecheError, Result};
use crate::local;
use crate::ntfy;
use crate::registry::{JobStatus, LiveStatus, Registry};
use crate::runtime::{RuntimeCtx, send_notification};
use crate::slurm::{generate_sbatch_script, get_job_status, submit_job};
use crate::ssh::{SshClient, shell_escape};
use crate::sync::{
    list_input_sync_files, list_project_sync_files, sync_inputs_to_workspace,
    sync_project_to_workspace,
};
use chrono::Utc;
use console::style;
use rand::Rng;
use std::io::Write;
use std::time::Duration;

use super::{job_path, workspace_path};

/// Checks that the system shell is available for local command execution.
///
/// On Unix, checks for `sh`. On Windows, `cmd.exe` is always available.
fn require_shell() -> Result<()> {
    #[cfg(unix)]
    if std::process::Command::new("sh")
        .arg("-c")
        .arg("true")
        .output()
        .is_err()
    {
        return Err(FlecheError::MissingDependency(
            "sh not found. Local execution requires a Unix shell.\n  \
             Windows: Install Git Bash, WSL, or Cygwin"
                .to_string(),
        ));
    }
    Ok(())
}

/// Options for running a job.
#[derive(Debug, Default)]
pub struct RunJobOptions {
    /// Run in background (don't stream output).
    pub background: bool,
    /// Send terminal notification when job completes.
    pub notify: bool,
    /// Send push notifications via ntfy.sh on state changes.
    pub ntfy_topic: Option<String>,
    /// Print generated sbatch script without submitting.
    pub dry_run: bool,
    /// Job ID to wait for before starting.
    pub after: Option<String>,
    /// Number of times to retry on failure (with exponential backoff).
    pub retry: Option<u32>,
    /// Note/annotation to attach to the job.
    pub note: Option<String>,
    /// CLI override: run directly via SSH instead of submitting to Slurm.
    pub exec: bool,
}

/// Runs a job on the remote cluster via Slurm (or locally if host is "local").
///
/// This is the main entry point for job submission. It:
/// 1. Resolves the job configuration with all overrides applied
/// 2. Syncs project code to the shared workspace (remote only)
/// 3. Syncs input files to the workspace (remote only)
/// 4. Creates a job directory for logs/metadata
/// 5. Uploads the generated sbatch script (remote only)
/// 6. Submits the job to Slurm (or runs locally)
/// 7. Streams the job output (unless --bg is specified)
pub async fn run_job(
    config: &Config,
    job_or_command: Option<&str>,
    command_override: Option<&str>,
    env_overrides: &[(String, String)],
    tags: &[(String, String)],
    slurm_overrides: SlurmConfig,
    host_override: Option<&str>,
    opts: RunJobOptions,
    ctx: RuntimeCtx,
) -> Result<()> {
    // Determine if job_or_command is a job name or a command
    let (job_name, actual_command) = if let Some(joc) = job_or_command {
        if config.jobs.contains_key(joc) {
            // It's a job name
            (Some(joc), command_override)
        } else {
            // It's a command (or unrecognized job name - will be used as command)
            (None, Some(joc))
        }
    } else {
        (None, command_override)
    };

    let mut job = config.resolve_job(job_name, actual_command, env_overrides, &slurm_overrides)?;

    // CLI --exec overrides config
    if opts.exec {
        job.exec = true;
    }

    // Determine final host: CLI override -> job definition -> remote.host
    let host = host_override.map_or_else(|| job.host.clone(), String::from);

    // Branch based on host
    if host == "local" {
        return run_job_locally(config, &job, tags, &opts, ctx).await;
    }

    // Direct remote execution (exec mode) bypasses Slurm
    if job.exec {
        return run_job_direct_remote(config, &job, &host, tags, &opts, ctx).await;
    }

    // Resolve dependency if specified
    let dependency_slurm_id = if let Some(ref dep_job_id) = opts.after {
        let registry = Registry::open()?;
        let dep_job = registry.get_job(dep_job_id)?;
        let slurm_id = dep_job
            .slurm_id
            .ok_or_else(|| FlecheError::NoSlurmId(dep_job.id.clone()))?;
        Some(slurm_id)
    } else {
        None
    };

    // Remote execution path
    let job_id = generate_job_id(&job.name);
    let workspace = workspace_path(config);

    if opts.dry_run {
        let job_dir = job_path(config, &job_id);
        let script = generate_sbatch_script(&job_id, &job, &workspace, &job_dir);
        println!(
            "{}",
            style("[dry-run] Generated sbatch script:").bold().yellow()
        );
        println!();
        println!("{script}");
        println!();
        print_dry_run_synced_files(config, &job.inputs).await?;
        return Ok(());
    }

    let job_dir = job_path(config, &job_id);
    let ssh =
        prepare_remote_workspace(config, &host, &workspace, &job_dir, &job.inputs, ctx).await?;

    // Retry loop
    let max_attempts = opts.retry.map_or(1, |r| r + 1);
    let mut attempt = 0;

    loop {
        attempt += 1;

        // Generate new job ID for each attempt
        let job_id = if attempt == 1 {
            job_id.clone()
        } else {
            generate_job_id(&job.name)
        };
        let job_dir = job_path(config, &job_id);

        // Create job directory for this attempt
        if attempt > 1 {
            ssh.mkdir(&job_dir).await?;
        }

        // Generate and upload script
        let script = generate_sbatch_script(&job_id, &job, &workspace, &job_dir);

        println!("{} Submitting job to Slurm...", style("[4/4]").bold().dim());
        ssh.write_file(&format!("{job_dir}/job.sbatch"), &script)
            .await?;

        // Submit job (with optional dependency, only on first attempt)
        let dep = if attempt == 1 {
            dependency_slurm_id.as_deref()
        } else {
            None
        };
        let slurm_id = submit_job(&ssh, &job_dir, dep).await?;

        // Record in registry (note only on first attempt)
        let registry = Registry::open()?;
        let job_note = if attempt == 1 {
            opts.note.as_deref()
        } else {
            None
        };
        registry.insert_job(
            &job_id,
            Some(&slurm_id),
            &job,
            &config.project_name,
            &config.project_path.to_string_lossy(),
            &host,
            &workspace,
            tags,
            job_note,
        )?;

        // Send ntfy pending notification
        if let Some(ref topic) = opts.ntfy_topic {
            ntfy::notify_state_change(
                topic,
                &job_id,
                None,
                JobStatus::Pending,
                opts.note.as_deref(),
            );
        }

        println!();
        if attempt > 1 {
            println!(
                "{} {} (attempt {}/{})",
                style("Job ID:").green().bold(),
                job_id,
                attempt,
                max_attempts
            );
        } else {
            println!("{} {}", style("Job ID:").green().bold(), job_id);
        }
        println!("{} {}", style("Slurm ID:").green().bold(), slurm_id);

        if opts.background {
            if ctx.should_notify(opts.notify) || opts.ntfy_topic.is_some() {
                // Wait for job completion in background and notify
                wait_and_notify(&job_id, &host, opts.ntfy_topic.as_deref(), ctx).await?;
            }
            // Background mode doesn't support retry (we don't wait for completion)
            break;
        }

        // Foreground mode: follow logs and check result
        println!();
        let live = follow_job_logs(
            &host,
            &slurm_id,
            &job_dir,
            opts.ntfy_topic.as_deref(),
            &job_id,
            ctx,
        )
        .await?;

        // Update registry with final status
        registry.update_status(&job_id, &live)?;

        // Check if we should retry
        if live.status == JobStatus::Failed && attempt < max_attempts {
            let delay_secs = ctx.retry_base_delay_secs * (1 << (attempt - 1));
            println!();
            println!(
                "{} Retrying in {} seconds (attempt {}/{})...",
                style("↻").yellow().bold(),
                delay_secs,
                attempt + 1,
                max_attempts
            );
            tokio::time::sleep(Duration::from_secs(delay_secs)).await;
            println!();
        } else {
            break;
        }
    }

    Ok(())
}

/// Runs a job locally (when host is "local").
async fn run_job_locally(
    config: &Config,
    job: &ResolvedJob,
    tags: &[(String, String)],
    opts: &RunJobOptions,
    ctx: RuntimeCtx,
) -> Result<()> {
    require_shell()?;

    // Check dependency if specified
    if let Some(ref dep_job_id) = opts.after {
        let registry = Registry::open()?;
        let dep_job = registry.get_job(dep_job_id)?;

        if dep_job.status != JobStatus::Completed {
            return Err(FlecheError::MissingDependency(format!(
                "Dependency job '{}' has not completed successfully (status: {:?}). \
                 Use 'fleche wait {}' to wait for it.",
                dep_job.id, dep_job.status, dep_job.id
            )));
        }
    }

    let job_id = generate_job_id(&job.name);

    // Warn about features that don't apply locally
    if !job.inputs.is_empty() {
        eprintln!(
            "{}",
            style("Warning: inputs are ignored for local jobs (files are already local)").yellow()
        );
    }
    if !job.outputs.is_empty() {
        eprintln!(
            "{}",
            style("Warning: outputs are ignored for local jobs (files are already local)").yellow()
        );
    }
    if job.slurm.partition.is_some()
        || job.slurm.time.is_some()
        || job.slurm.gpus.is_some()
        || job.slurm.cpus.is_some()
        || job.slurm.memory.is_some()
    {
        eprintln!(
            "{}",
            style("Warning: Slurm options are ignored for local jobs").yellow()
        );
    }

    if opts.dry_run {
        println!("{}", style("[dry-run] Would run locally:").bold().yellow());
        println!();
        println!("  Command: {}", job.command);
        println!("  Working directory: {}", config.project_path.display());
        if !job.env.is_empty() {
            println!("  Environment:");
            for (k, v) in &job.env {
                println!("    {k}={v}");
            }
        }
        return Ok(());
    }

    // Create local job directory
    let job_dir = local::ensure_job_dir(&config.project_path, &job_id)?;

    // Record in registry (with remote_host="local" and remote_path=project_path)
    let registry = Registry::open()?;
    registry.insert_job(
        &job_id,
        None, // No Slurm ID for local jobs
        job,
        &config.project_name,
        &config.project_path.to_string_lossy(),
        "local",
        &config.project_path.to_string_lossy(),
        tags,
        opts.note.as_deref(),
    )?;

    println!("{} {}", style("Job ID:").green().bold(), job_id);
    println!("{} {}", style("Job directory:").dim(), job_dir.display());
    println!();

    if opts.background {
        #[cfg(windows)]
        return Err(FlecheError::MissingDependency(
            "Background local jobs (--bg) are not supported on Windows.\n  \
             Use foreground mode or run in WSL."
                .to_string(),
        ));

        // Run in background
        #[cfg(unix)]
        {
            let pid = local::run_background(&config.project_path, &job_id, &job.command, &job.env)?;
            println!("{} {}", style("PID:").green().bold(), pid);
            println!(
                "{}",
                style("Job running in background. Use 'fleche logs' to view output.").dim()
            );

            // Update status to running
            registry.update_status(&job_id, &LiveStatus::new(JobStatus::Running))?;

            if ctx.should_notify(opts.notify) || opts.ntfy_topic.is_some() {
                // Spawn a background task to wait and notify
                let project_path = config.project_path.clone();
                let job_id_clone = job_id.clone();
                let poll_interval = ctx.poll_interval_local_secs;
                let ntfy_topic = opts.ntfy_topic.clone();
                let note = opts.note.clone();
                let should_term_notify = ctx.should_notify(opts.notify);
                tokio::spawn(async move {
                    let mut prev_status: Option<JobStatus> = Some(JobStatus::Running);
                    loop {
                        tokio::time::sleep(Duration::from_secs(poll_interval)).await;
                        match local::get_local_job_status(&project_path, &job_id_clone) {
                            Ok(live) => {
                                if let Ok(registry) = Registry::open() {
                                    let _ = registry.update_status(&job_id_clone, &live);
                                }
                                if let Some(ref topic) = ntfy_topic {
                                    ntfy::notify_state_change(
                                        topic,
                                        &job_id_clone,
                                        prev_status,
                                        live.status,
                                        note.as_deref(),
                                    );
                                    prev_status = Some(live.status);
                                }
                                match live.status {
                                    JobStatus::Completed => {
                                        if should_term_notify {
                                            send_notification(&format!(
                                                "Job {job_id_clone} completed successfully."
                                            ));
                                        }
                                        break;
                                    }
                                    JobStatus::Failed => {
                                        if should_term_notify {
                                            send_notification(&format!(
                                                "Job {job_id_clone} failed."
                                            ));
                                        }
                                        break;
                                    }
                                    JobStatus::Cancelled => {
                                        if should_term_notify {
                                            send_notification(&format!(
                                                "Job {job_id_clone} was cancelled."
                                            ));
                                        }
                                        break;
                                    }
                                    _ => {}
                                }
                            }
                            Err(_) => break,
                        }
                    }
                });
            }
        }
    } else {
        // Run in foreground with retry support
        let max_attempts = opts.retry.map_or(1, |r| r + 1);
        let mut attempt = 0;

        loop {
            attempt += 1;

            // Generate new job ID for retries
            let job_id = if attempt == 1 {
                job_id.clone()
            } else {
                let new_id = generate_job_id(&job.name);
                let _job_dir = local::ensure_job_dir(&config.project_path, &new_id)?;
                let registry = Registry::open()?;
                registry.insert_job(
                    &new_id,
                    None,
                    job,
                    &config.project_name,
                    &config.project_path.to_string_lossy(),
                    "local",
                    &config.project_path.to_string_lossy(),
                    tags,
                    None, // Retries don't get a note
                )?;
                println!("{} {}", style("Job ID:").green().bold(), new_id);
                println!();
                new_id
            };

            println!(
                "{}",
                style("Running locally (Ctrl+C to cancel)...").yellow()
            );
            if attempt > 1 {
                println!(
                    "{}",
                    style(format!("(attempt {attempt}/{max_attempts})")).dim()
                );
            }
            println!();

            // Update status to running
            let registry = Registry::open()?;
            registry.update_status(&job_id, &LiveStatus::new(JobStatus::Running))?;

            let exit_code =
                local::run_foreground(&config.project_path, &job_id, &job.command, &job.env)?;

            let final_status = if exit_code == 0 {
                JobStatus::Completed
            } else {
                JobStatus::Failed
            };
            registry.update_status(
                &job_id,
                &LiveStatus::with_exit_code(final_status, exit_code),
            )?;

            println!();
            if exit_code == 0 {
                println!("{}", style("Job completed successfully.").green().bold());
                if ctx.should_notify(opts.notify) {
                    send_notification(&format!("Job {job_id} completed successfully."));
                }
                break;
            }

            println!(
                "{} (exit code: {})",
                style("Job failed.").red().bold(),
                exit_code
            );

            // Check if we should retry
            if attempt < max_attempts {
                let delay_secs = ctx.retry_base_delay_secs * (1 << (attempt - 1));
                println!();
                println!(
                    "{} Retrying in {} seconds (attempt {}/{})...",
                    style("↻").yellow().bold(),
                    delay_secs,
                    attempt + 1,
                    max_attempts
                );
                tokio::time::sleep(Duration::from_secs(delay_secs)).await;
                println!();
            } else {
                if ctx.should_notify(opts.notify) {
                    send_notification(&format!("Job {job_id} failed."));
                }
                break;
            }
        }
    }

    Ok(())
}

/// Prepares remote workspace and job directory, then syncs code and inputs.
async fn prepare_remote_workspace(
    config: &Config,
    host: &str,
    workspace: &str,
    job_dir: &str,
    inputs: &[String],
    ctx: RuntimeCtx,
) -> Result<crate::ssh::SshClient> {
    let ssh = ctx.ssh(host);

    println!(
        "{} Creating remote directories...",
        style("[1/4]").bold().dim()
    );
    ssh.mkdir(workspace).await?;
    ssh.mkdir(job_dir).await?;

    print!("{} Syncing project code...", style("[2/4]").bold().dim());
    let _ = std::io::stdout().flush();
    let stats = sync_project_to_workspace(&config.project_path, host, workspace).await?;
    println!(" {}", style(format!("({})", stats.human_readable())).dim());

    if inputs.is_empty() {
        println!("{} No input files to sync", style("[3/4]").bold().dim());
    } else {
        print!("{} Syncing input files...", style("[3/4]").bold().dim());
        let _ = std::io::stdout().flush();
        let stats = sync_inputs_to_workspace(&config.project_path, inputs, host, workspace).await?;
        println!(" {}", style(format!("({})", stats.human_readable())).dim());
    }

    Ok(ssh)
}

/// Prints the files that would be synced to the remote workspace.
///
/// Used by dry-run to show project code and input files without connecting to
/// the remote.
async fn print_dry_run_synced_files(config: &Config, inputs: &[String]) -> Result<()> {
    let project_files = list_project_sync_files(&config.project_path).await?;
    println!(
        "{}",
        style(format!(
            "[dry-run] Project files to sync ({}):",
            project_files.len()
        ))
        .bold()
        .yellow()
    );
    for file in &project_files {
        println!("  {file}");
    }

    let input_files = list_input_sync_files(&config.project_path, inputs).await?;
    if !input_files.is_empty() {
        println!();
        println!(
            "{}",
            style(format!(
                "[dry-run] Input files to sync ({}):",
                input_files.len()
            ))
            .bold()
            .yellow()
        );
        for file in &input_files {
            println!("  {file}");
        }
    }

    Ok(())
}

/// Re-runs a previous job with the same settings.
///
/// Fetches the job configuration from the registry and submits a new job
/// with the same command, Slurm settings, and environment variables.
pub async fn rerun_job(
    config: &Config,
    job_id: &str,
    tags: &[(String, String)],
    background: bool,
    ntfy_topic: Option<&str>,
    ctx: RuntimeCtx,
) -> Result<()> {
    let registry = Registry::open()?;
    let old_job = registry.get_job(job_id)?;

    // Deserialize the old job's configuration
    let resolved: ResolvedJob = serde_json::from_str(&old_job.config_json)?;

    // Merge old job's tags with new tags (new tags take precedence)
    let mut merged_tags: Vec<(String, String)> = old_job
        .tags
        .iter()
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();
    for (k, v) in tags {
        if let Some(pos) = merged_tags.iter().position(|(key, _)| key == k) {
            merged_tags[pos] = (k.clone(), v.clone());
        } else {
            merged_tags.push((k.clone(), v.clone()));
        }
    }

    // Run with the old job's resolved configuration
    run_job_with_resolved(config, &resolved, &merged_tags, background, ntfy_topic, ctx).await
}

/// Runs a job with an already-resolved configuration.
async fn run_job_with_resolved(
    config: &Config,
    job: &ResolvedJob,
    tags: &[(String, String)],
    background: bool,
    ntfy_topic: Option<&str>,
    ctx: RuntimeCtx,
) -> Result<()> {
    if job.host == "local" {
        let opts = RunJobOptions {
            background,
            notify: false,
            ntfy_topic: ntfy_topic.map(String::from),
            dry_run: false,
            after: None,
            retry: None,
            note: None,
            exec: false,
        };
        return run_job_locally(config, job, tags, &opts, ctx).await;
    }

    // Direct remote execution (exec mode) bypasses Slurm
    if job.exec {
        let opts = RunJobOptions {
            background,
            notify: false,
            ntfy_topic: ntfy_topic.map(String::from),
            dry_run: false,
            after: None,
            retry: None,
            note: None,
            exec: true,
        };
        return run_job_direct_remote(config, job, &job.host, tags, &opts, ctx).await;
    }

    let workspace = workspace_path(config);
    let host = job.host.clone();

    // Generate unique job ID
    let job_id = generate_job_id(&job.name);
    let job_dir = job_path(config, &job_id);
    let ssh =
        prepare_remote_workspace(config, &host, &workspace, &job_dir, &job.inputs, ctx).await?;

    // Generate and upload sbatch script
    println!("{} Submitting job to Slurm...", style("[4/4]").bold().dim());
    let script = generate_sbatch_script(&job_id, job, &workspace, &job_dir);
    ssh.write_file(&format!("{job_dir}/job.sbatch"), &script)
        .await?;

    // Submit job (no dependency for rerun)
    let slurm_id = submit_job(&ssh, &job_dir, None).await?;

    // Record in registry
    let registry = Registry::open()?;
    registry.insert_job(
        &job_id,
        Some(&slurm_id),
        job,
        &config.project_name,
        &config.project_path.to_string_lossy(),
        &host,
        &workspace,
        tags,
        None, // Reruns don't get a note
    )?;

    // Send ntfy pending notification
    if let Some(topic) = ntfy_topic {
        ntfy::notify_state_change(topic, &job_id, None, JobStatus::Pending, None);
    }

    println!();
    println!("{} {}", style("Job ID:").green().bold(), job_id);
    println!("{} {}", style("Slurm ID:").green().bold(), slurm_id);

    if !background {
        println!();
        follow_job_logs(&host, &slurm_id, &job_dir, ntfy_topic, &job_id, ctx).await?;
    }

    Ok(())
}

/// Executes a command directly via SSH (no Slurm), or locally if host is "local".
///
/// For remote: syncs the project and inputs, then runs the command directly over SSH.
/// For local: runs the command directly in the project directory.
/// Useful for quick tests or interactive work.
pub async fn exec_command(
    config: &Config,
    command: &str,
    env_overrides: &[(String, String)],
    host_override: Option<&str>,
    no_sync: bool,
    ctx: RuntimeCtx,
) -> Result<()> {
    let host = host_override.map_or_else(|| config.remote.host.clone(), String::from);

    // Local execution path
    if host == "local" {
        return exec_command_locally(config, command, env_overrides);
    }

    // Remote execution path
    let workspace = workspace_path(config);
    let ssh = ctx.ssh(&host);

    if no_sync {
        println!("Skipping sync, executing command directly...");
    } else {
        // Reject empty input entries before touching the network, so `fleche
        // exec` fails fast with the same error as `fleche run` instead of
        // silently skipping them (see Config::resolve_job).
        for (name, job) in &config.jobs {
            reject_empty_path_entries(name, "inputs", &job.inputs, &job.inputs)?;
        }

        // Create workspace if needed
        println!(
            "{} Creating remote directories...",
            style("[1/3]").bold().dim()
        );
        ssh.mkdir(&workspace).await?;

        // Sync project code to workspace
        print!("{} Syncing project code...", style("[2/3]").bold().dim());
        let _ = std::io::stdout().flush();
        let stats = sync_project_to_workspace(&config.project_path, &host, &workspace).await?;
        println!(" {}", style(format!("({})", stats.human_readable())).dim());

        // Sync global inputs
        let global_inputs: Vec<String> = config
            .jobs
            .values()
            .flat_map(|j| j.inputs.clone())
            .collect();

        if global_inputs.is_empty() {
            println!("{} Executing command...", style("[3/3]").bold().dim());
        } else {
            print!("{} Syncing input files...", style("[3/3]").bold().dim());
            let _ = std::io::stdout().flush();
            let stats =
                sync_inputs_to_workspace(&config.project_path, &global_inputs, &host, &workspace)
                    .await?;
            println!(" {}", style(format!("({})", stats.human_readable())).dim());
        }
    }

    // Build environment string
    let env_str = if env_overrides.is_empty() {
        String::new()
    } else {
        let vars: Vec<String> = env_overrides
            .iter()
            .map(|(k, v)| format!("{}={}", k, shell_escape(v)))
            .collect();
        format!("{} ", vars.join(" "))
    };

    // Execute command in workspace
    println!();
    let full_command = format!("cd {} && {}{}", shell_escape(&workspace), env_str, command);
    let (success, stdout, stderr) = ssh.exec_allow_failure(&full_command).await?;

    // Print output
    if !stdout.is_empty() {
        print!("{stdout}");
    }
    if !stderr.is_empty() {
        eprint!("{stderr}");
    }

    if !success {
        return Err(FlecheError::SshCommand(
            "Command exited with non-zero status".to_string(),
        ));
    }

    Ok(())
}

/// Executes a command locally (when host is "local").
fn exec_command_locally(
    config: &Config,
    command: &str,
    env_overrides: &[(String, String)],
) -> Result<()> {
    require_shell()?;

    println!(
        "{} Executing command locally...",
        style("[1/1]").bold().dim()
    );
    println!();

    let mut cmd = local::shell_command(command);
    cmd.current_dir(&config.project_path);

    // Add environment variables
    for (k, v) in env_overrides {
        cmd.env(k, v);
    }

    let status = cmd.status()?;

    if !status.success() {
        return Err(FlecheError::SshCommand(
            "Command exited with non-zero status".to_string(),
        ));
    }

    Ok(())
}

/// Generates a unique job ID from the job name and current timestamp.
fn generate_job_id(job_name: &str) -> String {
    let now = Utc::now();
    let suffix: String = rand::thread_rng()
        .sample_iter(&rand::distributions::Alphanumeric)
        .take(4)
        .map(char::from)
        .collect::<String>()
        .to_lowercase();
    format!(
        "{}-{}-{}",
        job_name,
        now.format("%Y%m%d-%H%M%S-%3f"),
        suffix
    )
}

/// Follows job logs and automatically exits when the job finishes.
///
/// Returns the final live status when the job completes.
async fn follow_job_logs(
    host: &str,
    slurm_id: &str,
    job_dir: &str,
    ntfy_topic: Option<&str>,
    job_id: &str,
    ctx: RuntimeCtx,
) -> Result<LiveStatus> {
    println!(
        "{}",
        style("Streaming output (Ctrl+C to disconnect, job keeps running)...").yellow()
    );

    let ssh = ctx.ssh(host);
    let stdout_path = format!("{job_dir}/job.out");
    let stderr_path = format!("{job_dir}/job.err");
    let mut child = ssh.tail_follow(&[&stdout_path, &stderr_path])?;

    // Poll job status until it reaches a terminal state
    let slurm_id = slurm_id.to_string();
    let host = host.to_string();
    let slurm_id_for_check = slurm_id.clone();
    let host_for_check = host.clone();
    let ntfy_topic_owned = ntfy_topic.map(String::from);
    let job_id_owned = job_id.to_string();
    let status_check = async move {
        let mut prev_status: Option<JobStatus> = None;
        loop {
            tokio::time::sleep(Duration::from_secs(ctx.poll_interval_remote_secs)).await;
            let ssh = ctx.ssh(&host_for_check);
            if let Ok(live) = get_job_status(&ssh, &slurm_id_for_check).await {
                if let Some(ref topic) = ntfy_topic_owned {
                    ntfy::notify_state_change(topic, &job_id_owned, prev_status, live.status, None);
                    prev_status = Some(live.status);
                }
                match live.status {
                    JobStatus::Completed | JobStatus::Failed | JobStatus::Cancelled => {
                        return live;
                    }
                    _ => {}
                }
            }
        }
    };

    // Wait for either the tail process to exit or the job to finish
    let live = tokio::select! {
        _ = child.wait() => {
            // Tail exited on its own - check final status.
            // Retry a few times because Slurm accounting (sacct) can lag behind
            // the actual job completion, causing a transient lookup failure.
            let ssh = ctx.ssh(&host);
            let mut result = None;
            for attempt in 0..6 {
                if attempt > 0 {
                    tokio::time::sleep(Duration::from_secs(2)).await;
                }
                if let Ok(live) = get_job_status(&ssh, &slurm_id).await {
                    result = Some(live);
                    break;
                }
            }
            result.unwrap_or_else(|| LiveStatus::new(JobStatus::Failed))
        }
        result = status_check => {
            // Job finished, kill tail and print status
            let _ = child.kill().await;

            // Give a moment for any final output to flush
            tokio::time::sleep(Duration::from_millis(500)).await;

            result
        }
    };

    println!();
    let message = match live.status {
        JobStatus::Completed => "Job completed successfully.".to_string(),
        JobStatus::Failed => match live.exit_code {
            Some(code) => format!("Job failed (exit code: {code})."),
            None => "Job failed.".to_string(),
        },
        JobStatus::Cancelled => "Job cancelled.".to_string(),
        _ => "Job finished.".to_string(),
    };

    match live.status {
        JobStatus::Completed => {
            println!("{}", style(&message).green().bold());
        }
        JobStatus::Failed => {
            println!("{}", style(&message).red().bold());
        }
        JobStatus::Cancelled => {
            println!("{}", style(&message).yellow().bold());
        }
        _ => {}
    }

    send_notification(&message);

    Ok(live)
}

/// Waits for a job to complete and sends a terminal notification.
///
/// Polls the job status every few seconds until it reaches a terminal state.
async fn wait_and_notify(
    job_id: &str,
    remote_host: &str,
    ntfy_topic: Option<&str>,
    ctx: RuntimeCtx,
) -> Result<()> {
    println!(
        "{}",
        style("Waiting for job to complete (will notify when done)...").dim()
    );

    let registry = Registry::open()?;
    let ssh = ctx.ssh(remote_host);
    let mut prev_status: Option<JobStatus> = None;

    loop {
        let job = registry.get_job(job_id)?;
        if let Some(ref slurm_id) = job.slurm_id {
            let live = get_job_status(&ssh, slurm_id).await?;
            registry.update_status(job_id, &live)?;

            if let Some(topic) = ntfy_topic {
                ntfy::notify_state_change(topic, job_id, prev_status, live.status, None);
                prev_status = Some(live.status);
            }

            match live.status {
                JobStatus::Completed => {
                    let message = format!("Job {job_id} completed successfully.");
                    println!("{}", style(&message).green().bold());
                    send_notification(&message);
                    return Ok(());
                }
                JobStatus::Failed => {
                    let message = format!("Job {job_id} failed.");
                    println!("{}", style(&message).red().bold());
                    send_notification(&message);
                    return Ok(());
                }
                JobStatus::Cancelled => {
                    let message = format!("Job {job_id} was cancelled.");
                    println!("{}", style(&message).yellow().bold());
                    send_notification(&message);
                    return Ok(());
                }
                _ => {}
            }
        }

        tokio::time::sleep(Duration::from_secs(ctx.poll_interval_remote_secs)).await;
    }
}

/// Runs a job directly on a remote host via SSH (no Slurm).
async fn run_job_direct_remote(
    config: &Config,
    job: &ResolvedJob,
    host: &str,
    tags: &[(String, String)],
    opts: &RunJobOptions,
    ctx: RuntimeCtx,
) -> Result<()> {
    // Warn about Slurm options that don't apply in exec mode
    if job.slurm.partition.is_some()
        || job.slurm.time.is_some()
        || job.slurm.gpus.is_some()
        || job.slurm.cpus.is_some()
        || job.slurm.memory.is_some()
    {
        eprintln!(
            "{}",
            style("Warning: Slurm options are ignored for exec jobs").yellow()
        );
    }

    let job_id = generate_job_id(&job.name);
    let workspace = workspace_path(config);
    let job_dir = job_path(config, &job_id);

    if opts.dry_run {
        let script = generate_exec_script(job, &workspace, &job_dir);
        println!(
            "{}",
            style("[dry-run] Generated exec script:").bold().yellow()
        );
        println!();
        println!("{script}");
        println!();
        print_dry_run_synced_files(config, &job.inputs).await?;
        return Ok(());
    }

    let ssh =
        prepare_remote_workspace(config, host, &workspace, &job_dir, &job.inputs, ctx).await?;

    // Retry loop
    let max_attempts = opts.retry.map_or(1, |r| r + 1);
    let mut attempt = 0;

    loop {
        attempt += 1;

        // Generate new job ID for each attempt
        let job_id = if attempt == 1 {
            job_id.clone()
        } else {
            generate_job_id(&job.name)
        };
        let job_dir = job_path(config, &job_id);

        // Create job directory for this attempt
        if attempt > 1 {
            ssh.mkdir(&job_dir).await?;
        }

        // Generate and upload exec script
        let script = generate_exec_script(job, &workspace, &job_dir);

        println!(
            "{} Starting remote exec job...",
            style("[4/4]").bold().dim()
        );
        ssh.write_file(&format!("{job_dir}/run.sh"), &script)
            .await?;

        // Start the job via nohup
        ssh.exec(&format!(
            "nohup sh {job_dir}/run.sh > /dev/null 2>&1 & echo started"
        ))
        .await?;

        // Record in registry (slurm_id = None for exec jobs)
        let registry = Registry::open()?;
        let job_note = if attempt == 1 {
            opts.note.as_deref()
        } else {
            None
        };
        registry.insert_job(
            &job_id,
            None, // No Slurm ID for exec jobs
            job,
            &config.project_name,
            &config.project_path.to_string_lossy(),
            host,
            &workspace,
            tags,
            job_note,
        )?;

        // Update status to running
        registry.update_status(&job_id, &LiveStatus::new(JobStatus::Running))?;

        // Send ntfy running notification (exec jobs go straight to running)
        if let Some(ref topic) = opts.ntfy_topic {
            ntfy::notify_state_change(
                topic,
                &job_id,
                None,
                JobStatus::Running,
                opts.note.as_deref(),
            );
        }

        println!();
        if attempt > 1 {
            println!(
                "{} {} (attempt {}/{})",
                style("Job ID:").green().bold(),
                job_id,
                attempt,
                max_attempts
            );
        } else {
            println!("{} {}", style("Job ID:").green().bold(), job_id);
        }

        if opts.background {
            println!(
                "{}",
                style("Job running in background. Use 'fleche logs' to view output.").dim()
            );

            if ctx.should_notify(opts.notify) || opts.ntfy_topic.is_some() {
                wait_and_notify_direct(&job_id, host, &job_dir, opts.ntfy_topic.as_deref(), ctx)
                    .await?;
            }
            // Background mode doesn't support retry
            break;
        }

        // Foreground mode: follow logs and check result
        println!();
        let live = follow_direct_job_logs(host, &job_dir, opts.ntfy_topic.as_deref(), &job_id, ctx)
            .await?;

        // Update registry with final status
        registry.update_status(&job_id, &live)?;

        // Check if we should retry
        if live.status == JobStatus::Failed && attempt < max_attempts {
            let delay_secs = ctx.retry_base_delay_secs * (1 << (attempt - 1));
            println!();
            println!(
                "{} Retrying in {} seconds (attempt {}/{})...",
                style("↻").yellow().bold(),
                delay_secs,
                attempt + 1,
                max_attempts
            );
            tokio::time::sleep(Duration::from_secs(delay_secs)).await;
            println!();
        } else {
            break;
        }
    }

    Ok(())
}

/// Generates a wrapper script for direct remote execution.
///
/// The script writes a PID file, sets up the environment, runs the command,
/// and writes the exit code on completion. This mirrors the local background
/// execution pattern but runs on the remote host.
fn generate_exec_script(job: &ResolvedJob, workspace: &str, job_dir: &str) -> String {
    let mut script = String::from("#!/bin/sh\n");
    script.push_str(&format!("echo $$ > {job_dir}/pid\n"));
    script.push_str(&format!("cd {}\n", shell_escape(workspace)));

    // Environment variables
    for (key, value) in &job.env {
        script.push_str(&format!("export {}={}\n", key, shell_escape(value)));
    }

    // Command with output redirection
    script.push_str(&format!(
        "{} > {job_dir}/job.out 2> {job_dir}/job.err\n",
        job.command
    ));
    script.push_str(&format!("echo $? > {job_dir}/exit_code\n"));

    script
}

/// Checks the status of a remote direct (exec) job by inspecting files via SSH.
///
/// Checks in order:
/// 1. `exit_code` file exists → completed (0) or failed (non-zero)
/// 2. `pid` file exists and process running → running
/// 3. `pid` file exists but process gone → failed (crashed without exit code)
/// 4. Neither file → pending
pub async fn get_remote_direct_job_status(ssh: &SshClient, job_dir: &str) -> Result<LiveStatus> {
    // Check if exit_code exists
    let (has_exit_code, exit_code_content, _) = ssh
        .exec_allow_failure(&format!("cat {job_dir}/exit_code 2>/dev/null"))
        .await?;

    if has_exit_code && !exit_code_content.trim().is_empty() {
        let code: i32 = exit_code_content.trim().parse().unwrap_or(1);
        let status = if code == 0 {
            JobStatus::Completed
        } else {
            JobStatus::Failed
        };
        return Ok(LiveStatus::with_exit_code(status, code));
    }

    // Check if PID exists and process is running
    let (has_pid, pid_content, _) = ssh
        .exec_allow_failure(&format!("cat {job_dir}/pid 2>/dev/null"))
        .await?;

    if has_pid && !pid_content.trim().is_empty() {
        let pid = pid_content.trim();
        // Check if process is still running
        let (is_running, _, _) = ssh
            .exec_allow_failure(&format!("kill -0 {pid} 2>/dev/null"))
            .await?;

        if is_running {
            return Ok(LiveStatus::new(JobStatus::Running));
        }

        // PID exists but process is gone - job failed without writing exit code
        return Ok(LiveStatus::new(JobStatus::Failed));
    }

    // No PID file - job hasn't started yet
    Ok(LiveStatus::new(JobStatus::Pending))
}

/// Follows logs of a remote direct job and polls for completion.
///
/// Returns the final live status when the job completes.
async fn follow_direct_job_logs(
    host: &str,
    job_dir: &str,
    ntfy_topic: Option<&str>,
    job_id: &str,
    ctx: RuntimeCtx,
) -> Result<LiveStatus> {
    println!(
        "{}",
        style("Streaming output (Ctrl+C to disconnect, job keeps running)...").yellow()
    );

    let ssh = ctx.ssh(host);
    let stdout_path = format!("{job_dir}/job.out");
    let stderr_path = format!("{job_dir}/job.err");
    let mut child = ssh.tail_follow(&[&stdout_path, &stderr_path])?;

    // Poll job status until it reaches a terminal state
    let job_dir_owned = job_dir.to_string();
    let host_owned = host.to_string();
    let ntfy_topic_owned = ntfy_topic.map(String::from);
    let job_id_owned = job_id.to_string();
    let status_check = async move {
        let mut prev_status: Option<JobStatus> = None;
        loop {
            tokio::time::sleep(Duration::from_secs(ctx.poll_interval_remote_secs)).await;
            let ssh = ctx.ssh(&host_owned);
            if let Ok(live) = get_remote_direct_job_status(&ssh, &job_dir_owned).await {
                if let Some(ref topic) = ntfy_topic_owned {
                    ntfy::notify_state_change(topic, &job_id_owned, prev_status, live.status, None);
                    prev_status = Some(live.status);
                }
                match live.status {
                    JobStatus::Completed | JobStatus::Failed | JobStatus::Cancelled => {
                        return live;
                    }
                    _ => {}
                }
            }
        }
    };

    // Wait for either the tail process to exit or the job to finish
    let live = tokio::select! {
        _ = child.wait() => {
            // Tail exited on its own - check final status with retries
            let ssh = ctx.ssh(host);
            let mut result = None;
            for attempt in 0..6 {
                if attempt > 0 {
                    tokio::time::sleep(Duration::from_secs(2)).await;
                }
                if let Ok(live) = get_remote_direct_job_status(&ssh, job_dir).await {
                    result = Some(live);
                    break;
                }
            }
            result.unwrap_or_else(|| LiveStatus::new(JobStatus::Failed))
        }
        result = status_check => {
            // Job finished, kill tail
            let _ = child.kill().await;
            tokio::time::sleep(Duration::from_millis(500)).await;
            result
        }
    };

    println!();
    let message = match live.status {
        JobStatus::Completed => "Job completed successfully.".to_string(),
        JobStatus::Failed => match live.exit_code {
            Some(code) => format!("Job failed (exit code: {code})."),
            None => "Job failed.".to_string(),
        },
        JobStatus::Cancelled => "Job cancelled.".to_string(),
        _ => "Job finished.".to_string(),
    };

    match live.status {
        JobStatus::Completed => {
            println!("{}", style(&message).green().bold());
        }
        JobStatus::Failed => {
            println!("{}", style(&message).red().bold());
        }
        JobStatus::Cancelled => {
            println!("{}", style(&message).yellow().bold());
        }
        _ => {}
    }

    send_notification(&message);

    Ok(live)
}

/// Waits for a remote direct job to complete and sends a notification.
async fn wait_and_notify_direct(
    job_id: &str,
    remote_host: &str,
    job_dir: &str,
    ntfy_topic: Option<&str>,
    ctx: RuntimeCtx,
) -> Result<()> {
    println!(
        "{}",
        style("Waiting for job to complete (will notify when done)...").dim()
    );

    let ssh = ctx.ssh(remote_host);
    let mut prev_status: Option<JobStatus> = None;

    loop {
        let live = get_remote_direct_job_status(&ssh, job_dir).await?;

        if let Ok(registry) = Registry::open() {
            let _ = registry.update_status(job_id, &live);
        }

        if let Some(topic) = ntfy_topic {
            ntfy::notify_state_change(topic, job_id, prev_status, live.status, None);
            prev_status = Some(live.status);
        }

        match live.status {
            JobStatus::Completed => {
                let message = format!("Job {job_id} completed successfully.");
                println!("{}", style(&message).green().bold());
                send_notification(&message);
                return Ok(());
            }
            JobStatus::Failed => {
                let message = format!("Job {job_id} failed.");
                println!("{}", style(&message).red().bold());
                send_notification(&message);
                return Ok(());
            }
            JobStatus::Cancelled => {
                let message = format!("Job {job_id} was cancelled.");
                println!("{}", style(&message).yellow().bold());
                send_notification(&message);
                return Ok(());
            }
            _ => {}
        }

        tokio::time::sleep(Duration::from_secs(ctx.poll_interval_remote_secs)).await;
    }
}

/// Cancels a remote direct job by killing its PID via SSH.
///
/// Returns `Ok(true)` if the process was killed, `Ok(false)` if no PID found.
pub async fn cancel_remote_direct_job(ssh: &SshClient, job_dir: &str) -> Result<bool> {
    // Read PID file
    let (has_pid, pid_content, _) = ssh
        .exec_allow_failure(&format!("cat {job_dir}/pid 2>/dev/null"))
        .await?;

    if !has_pid || pid_content.trim().is_empty() {
        return Ok(false);
    }

    let pid = pid_content.trim();

    // Kill the process
    let (success, _, _) = ssh
        .exec_allow_failure(&format!("kill {pid} 2>/dev/null"))
        .await?;

    if success {
        // Write exit code to indicate cancellation (143 = 128 + 15 SIGTERM)
        let _ = ssh
            .exec_allow_failure(&format!("echo 143 > {job_dir}/exit_code"))
            .await;
    }

    Ok(success)
}

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

    #[test]
    fn test_generate_job_id_format() {
        let id = generate_job_id("train");

        // Starts with job name
        assert!(id.starts_with("train-"));

        // Has expected structure: name-YYYYMMDD-HHMMSS-mmm-xxxx
        let parts: Vec<&str> = id.split('-').collect();
        assert_eq!(parts.len(), 5); // train, date, time, millis, suffix

        // Timestamp parts are numeric
        assert!(parts[1].chars().all(|c| c.is_ascii_digit())); // YYYYMMDD
        assert!(parts[2].chars().all(|c| c.is_ascii_digit())); // HHMMSS

        // Suffix is 4 lowercase alphanumeric
        assert_eq!(parts[4].len(), 4);
        assert!(
            parts[4]
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
        );
    }

    #[test]
    fn test_generate_job_id_uniqueness() {
        let ids: Vec<String> = (0..100).map(|_| generate_job_id("test")).collect();
        let unique: std::collections::HashSet<_> = ids.iter().collect();
        assert_eq!(ids.len(), unique.len());
    }

    #[test]
    fn test_generate_job_id_with_hyphenated_name() {
        let id = generate_job_id("my-job");

        assert!(id.starts_with("my-job-"));

        // Still has correct structure despite hyphens in name
        let suffix = id.split('-').next_back().unwrap();
        assert_eq!(suffix.len(), 4);
    }

    #[test]
    fn test_generate_exec_script_basic() {
        use crate::config::{ResolvedJob, SlurmConfig};
        use indexmap::IndexMap;

        let job = ResolvedJob {
            name: "test".to_string(),
            command: "echo hello".to_string(),
            inputs: vec![],
            outputs: vec![],
            slurm: SlurmConfig::default(),
            env: IndexMap::new(),
            host: "cluster".to_string(),
            exec: true,
        };

        let script = generate_exec_script(&job, "/workspace", "/jobs/test-123");

        assert!(script.starts_with("#!/bin/sh\n"));
        assert!(script.contains("echo $$ > /jobs/test-123/pid"));
        assert!(script.contains("cd '/workspace'"));
        assert!(script.contains("echo hello > /jobs/test-123/job.out 2> /jobs/test-123/job.err"));
        assert!(script.contains("echo $? > /jobs/test-123/exit_code"));
    }

    #[test]
    fn test_generate_exec_script_with_env() {
        use crate::config::{ResolvedJob, SlurmConfig};
        use indexmap::IndexMap;

        let mut env = IndexMap::new();
        env.insert("FOO".to_string(), "bar".to_string());
        env.insert("PATH_VAR".to_string(), "/some/path".to_string());

        let job = ResolvedJob {
            name: "test".to_string(),
            command: "python train.py".to_string(),
            inputs: vec![],
            outputs: vec![],
            slurm: SlurmConfig::default(),
            env,
            host: "cluster".to_string(),
            exec: true,
        };

        let script = generate_exec_script(&job, "/ws", "/jobs/test-456");

        assert!(script.contains("export FOO='bar'"));
        assert!(script.contains("export PATH_VAR='/some/path'"));
        assert!(script.contains("python train.py > /jobs/test-456/job.out"));
    }
}