zlayer-agent 0.13.0

Container runtime agent using libcontainer/youki
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
//! Job execution engine - run-to-completion container lifecycle
//!
//! This module provides the `JobExecutor` which handles run-to-completion
//! workloads (jobs and cron jobs). Unlike services which run indefinitely,
//! jobs run to completion and track their exit status.

use crate::error::{AgentError, Result};
use crate::init::InitOrchestrator;
use crate::overlay_manager::OverlayManager;
use crate::runtime::{ContainerId, Runtime};
// Only the non-Windows attach path matches on the attach kind; on Windows the
// overlay is wired at container-create time inside overlayd.
#[cfg(not(target_os = "windows"))]
use crate::runtime::OverlayAttachKind;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};
use uuid::Uuid;
use zlayer_spec::ServiceSpec;

/// How a job container was attached to the overlay, so it can be detached when
/// the job exits (otherwise every execution leaks a veth + overlay IP).
#[cfg_attr(target_os = "windows", allow(dead_code))]
#[derive(Default)]
enum OverlayAttachment {
    /// Not attached (no overlay manager, no PID, attach failed, or Windows —
    /// where the overlay is wired at container-create time inside overlayd).
    #[default]
    None,
    /// Linux host-process (youki): detach by the PID recorded at attach.
    Pid(u32),
    /// macOS VZ guest: detach by the container id used at attach.
    Guest(String),
    /// macOS host-shared (Seatbelt/native-VZ/libkrun): detach by the container
    /// id used at attach (overlayd allocated a distinct overlay /32 + utun alias).
    HostShared(String),
}

/// Unique identifier for a job execution
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct JobExecutionId(pub String);

impl JobExecutionId {
    /// Create a new random execution ID
    #[must_use]
    pub fn new() -> Self {
        Self(Uuid::new_v4().to_string())
    }
}

impl Default for JobExecutionId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for JobExecutionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Status of a job execution
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobStatus {
    /// Job is queued, waiting to start
    Pending,
    /// Init steps are running
    Initializing,
    /// Main container is running
    Running,
    /// Job completed successfully
    Completed { exit_code: i32, duration: Duration },
    /// Job failed
    Failed {
        reason: String,
        exit_code: Option<i32>,
    },
    /// Job was cancelled
    Cancelled,
}

impl std::fmt::Display for JobStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JobStatus::Pending => write!(f, "pending"),
            JobStatus::Initializing => write!(f, "initializing"),
            JobStatus::Running => write!(f, "running"),
            JobStatus::Completed { exit_code, .. } => write!(f, "completed({exit_code})"),
            JobStatus::Failed { exit_code, .. } => {
                if let Some(code) = exit_code {
                    write!(f, "failed({code})")
                } else {
                    write!(f, "failed")
                }
            }
            JobStatus::Cancelled => write!(f, "cancelled"),
        }
    }
}

/// How the job was triggered
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobTrigger {
    /// Triggered via HTTP endpoint
    Endpoint { remote_addr: Option<String> },
    /// Triggered via CLI
    Cli,
    /// Triggered by cron scheduler
    Scheduler,
    /// Triggered by internal system (dependency, etc.)
    Internal { reason: String },
}

impl std::fmt::Display for JobTrigger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JobTrigger::Endpoint { remote_addr } => {
                if let Some(addr) = remote_addr {
                    write!(f, "endpoint({addr})")
                } else {
                    write!(f, "endpoint")
                }
            }
            JobTrigger::Cli => write!(f, "cli"),
            JobTrigger::Scheduler => write!(f, "scheduler"),
            JobTrigger::Internal { reason } => write!(f, "internal({reason})"),
        }
    }
}

/// A single job execution record
#[derive(Debug, Clone)]
pub struct JobExecution {
    pub id: JobExecutionId,
    pub job_name: String,
    pub status: JobStatus,
    pub started_at: Option<Instant>,
    pub completed_at: Option<Instant>,
    pub container_id: Option<ContainerId>,
    /// Captured stdout/stderr (limited to last N bytes)
    pub logs: Option<String>,
    /// Trigger source (endpoint, cli, scheduler, etc.)
    pub trigger: JobTrigger,
}

/// Configuration for the job executor
#[derive(Debug, Clone)]
pub struct JobExecutorConfig {
    /// Maximum concurrent job executions per job name
    pub max_concurrent: usize,
    /// How long to retain completed job records
    pub retention: Duration,
    /// Maximum log size to capture (in bytes)
    pub max_log_size: usize,
}

impl Default for JobExecutorConfig {
    fn default() -> Self {
        Self {
            max_concurrent: 10,
            retention: Duration::from_secs(3600), // 1 hour
            max_log_size: 1024 * 1024,            // 1 MB
        }
    }
}

/// Job executor handles run-to-completion workloads
pub struct JobExecutor {
    runtime: Arc<dyn Runtime + Send + Sync>,
    /// Active and recent job executions
    executions: Arc<RwLock<HashMap<JobExecutionId, JobExecution>>>,
    /// Job specs (for jobs that need to be stored)
    job_specs: Arc<RwLock<HashMap<String, ServiceSpec>>>,
    /// Overlay manager used to attach each job container to the overlay network
    /// (so the job can reach the daemon API / peer services) and detach it on
    /// exit. `None` when the daemon runs without overlay networking.
    overlay_manager: Option<Arc<RwLock<OverlayManager>>>,
    /// Sink for persisting/revoking per-container scoped tokens. `None`
    /// disables persistence (token minted without a `jti`, not revocable).
    token_sink: Option<Arc<dyn crate::auth::ContainerTokenSink>>,
    /// Configuration
    config: JobExecutorConfig,
    /// Shutdown flag
    shutdown: AtomicBool,
}

impl JobExecutor {
    /// Create a new job executor with default configuration
    pub fn new(runtime: Arc<dyn Runtime + Send + Sync>) -> Self {
        Self::with_config(runtime, JobExecutorConfig::default())
    }

    /// Create a new job executor with custom configuration
    pub fn with_config(runtime: Arc<dyn Runtime + Send + Sync>, config: JobExecutorConfig) -> Self {
        Self {
            runtime,
            executions: Arc::new(RwLock::new(HashMap::new())),
            job_specs: Arc::new(RwLock::new(HashMap::new())),
            overlay_manager: None,
            token_sink: None,
            config,
            shutdown: AtomicBool::new(false),
        }
    }

    /// Attach an overlay manager so job (and cron) containers join the overlay
    /// network and can reach the daemon API / peer services. Without this, a job
    /// container comes up with only a loopback netns and any call to the daemon
    /// node IP fails with `ENETUNREACH`.
    pub fn set_overlay_manager(&mut self, overlay_manager: Arc<RwLock<OverlayManager>>) {
        self.overlay_manager = Some(overlay_manager);
    }

    /// Set the sink used to persist/revoke per-container scoped tokens.
    pub fn set_token_sink(&mut self, sink: Arc<dyn crate::auth::ContainerTokenSink>) {
        self.token_sink = Some(sink);
    }

    /// Register a job spec (for later triggering)
    pub async fn register_job(&self, name: &str, spec: ServiceSpec) {
        let mut specs = self.job_specs.write().await;
        specs.insert(name.to_string(), spec);
        info!(job = %name, "Registered job spec");
    }

    /// Unregister a job spec
    pub async fn unregister_job(&self, name: &str) {
        let mut specs = self.job_specs.write().await;
        specs.remove(name);
        info!(job = %name, "Unregistered job spec");
    }

    /// Get a registered job spec
    pub async fn get_job_spec(&self, name: &str) -> Option<ServiceSpec> {
        let specs = self.job_specs.read().await;
        specs.get(name).cloned()
    }

    /// Names of all registered jobs, sorted.
    pub async fn registered_job_names(&self) -> Vec<String> {
        let specs = self.job_specs.read().await;
        let mut names: Vec<String> = specs.keys().cloned().collect();
        names.sort();
        names
    }

    /// The most recent execution for a job name, if any (the one that started
    /// last; a not-yet-started execution sorts before any started one).
    pub async fn latest_execution(&self, job_name: &str) -> Option<JobExecution> {
        let executions = self.executions.read().await;
        executions
            .values()
            .filter(|e| e.job_name == job_name)
            .max_by_key(|e| e.started_at)
            .cloned()
    }

    /// Trigger a job execution
    ///
    /// # Errors
    /// Returns an error if the job container cannot be created or started.
    pub async fn trigger(
        &self,
        job_name: &str,
        spec: &ServiceSpec,
        trigger: JobTrigger,
    ) -> Result<JobExecutionId> {
        if self.shutdown.load(Ordering::Relaxed) {
            return Err(AgentError::Internal("Job executor is shutting down".into()));
        }

        let exec_id = JobExecutionId::new();

        info!(
            job = %job_name,
            execution_id = %exec_id,
            trigger = %trigger,
            "Triggering job execution"
        );

        // Create execution record
        let execution = JobExecution {
            id: exec_id.clone(),
            job_name: job_name.to_string(),
            status: JobStatus::Pending,
            started_at: None,
            completed_at: None,
            container_id: None,
            logs: None,
            trigger,
        };

        // Store execution record
        {
            let mut executions = self.executions.write().await;
            executions.insert(exec_id.clone(), execution);
        }

        // Spawn the job execution task
        let runtime = self.runtime.clone();
        let spec = spec.clone();
        let exec_id_clone = exec_id.clone();
        let executions = self.executions.clone();
        let job_name = job_name.to_string();
        let max_log_size = self.config.max_log_size;
        let overlay_manager = self.overlay_manager.clone();
        let token_sink = self.token_sink.clone();

        tokio::spawn(async move {
            Box::pin(Self::run_job(
                runtime,
                executions,
                exec_id_clone,
                &job_name,
                spec,
                max_log_size,
                overlay_manager,
                token_sink,
            ))
            .await;
        });

        Ok(exec_id)
    }

    /// Internal: Run a job to completion
    #[allow(clippy::too_many_lines, clippy::too_many_arguments)]
    async fn run_job(
        runtime: Arc<dyn Runtime + Send + Sync>,
        executions: Arc<RwLock<HashMap<JobExecutionId, JobExecution>>>,
        exec_id: JobExecutionId,
        job_name: &str,
        spec: ServiceSpec,
        max_log_size: usize,
        overlay_manager: Option<Arc<RwLock<OverlayManager>>>,
        token_sink: Option<Arc<dyn crate::auth::ContainerTokenSink>>,
    ) {
        let started = Instant::now();

        // Update status to Initializing
        Self::update_status(&executions, &exec_id, |exec| {
            exec.status = JobStatus::Initializing;
            exec.started_at = Some(started);
        })
        .await;

        // Create container ID for this execution
        // Use a unique replica number based on execution ID hash
        let replica = exec_id.0.chars().take(8).collect::<String>();
        let replica_num = u32::from_str_radix(&replica, 16).unwrap_or(0) % 10000;
        let container_id = ContainerId::new(format!("job-{job_name}"), replica_num);

        // Store container ID
        Self::update_status(&executions, &exec_id, |exec| {
            exec.container_id = Some(container_id.clone());
        })
        .await;

        debug!(
            job = %job_name,
            execution_id = %exec_id,
            container_id = %container_id,
            "Creating job container"
        );

        // Pull image
        let image_str = spec.image.name.to_string();
        if let Err(e) = runtime
            .pull_image_with_policy(
                &image_str,
                spec.image.pull_policy,
                None,
                spec.image.source_policy.unwrap_or_default(),
            )
            .await
        {
            error!(
                job = %job_name,
                execution_id = %exec_id,
                error = %e,
                "Image pull failed"
            );
            Self::update_status(&executions, &exec_id, |exec| {
                exec.status = JobStatus::Failed {
                    reason: format!("Image pull failed: {e}"),
                    exit_code: None,
                };
                exec.completed_at = Some(Instant::now());
            })
            .await;
            return;
        }

        // Create container
        if let Err(e) = runtime.create_container(&container_id, &spec).await {
            let error_msg = e.to_string();
            error!(
                job = %job_name,
                execution_id = %exec_id,
                error = %error_msg,
                "Container create failed"
            );
            Self::update_status(&executions, &exec_id, |exec| {
                exec.status = JobStatus::Failed {
                    reason: format!("Container create failed: {error_msg}"),
                    exit_code: None,
                };
                exec.completed_at = Some(Instant::now());
            })
            .await;
            return;
        }

        // Attach the job container to the overlay network BEFORE it starts, so
        // its routes (eth0 service bridge + eth1 global overlay → the daemon node
        // IP) and resolv.conf are in place before init actions and the job's own
        // command run. Without this a job container has only loopback and any call
        // to the daemon node IP fails with `ENETUNREACH`. youki records the init
        // PID + netns at create (paused on the start fifo), so the attach is
        // race-free. Best-effort: attach failure is logged, not fatal.
        let overlay_attachment = Self::attach_overlay(
            overlay_manager.as_ref(),
            runtime.as_ref(),
            &container_id,
            job_name,
            &spec,
        )
        .await;

        // Run init steps
        let init_orchestrator = InitOrchestrator::new(container_id.clone(), spec.init.clone());
        if let Err(e) = init_orchestrator.run().await {
            let error_msg = e.to_string();
            error!(
                job = %job_name,
                execution_id = %exec_id,
                error = %error_msg,
                "Init failed"
            );
            Self::update_status(&executions, &exec_id, |exec| {
                exec.status = JobStatus::Failed {
                    reason: format!("Init failed: {error_msg}"),
                    exit_code: None,
                };
                exec.completed_at = Some(Instant::now());
            })
            .await;
            // Cleanup: detach overlay (reclaim veth/IP) then remove container.
            Self::detach_overlay(
                overlay_manager.as_ref(),
                runtime.as_ref(),
                &container_id,
                &overlay_attachment,
            )
            .await;
            Self::maybe_teardown_job_segment(
                overlay_manager.as_ref(),
                &executions,
                &exec_id,
                job_name,
            )
            .await;
            let _ = runtime.remove_container(&container_id).await;
            Self::revoke_token(token_sink.as_ref(), &container_id).await;
            return;
        }

        // Update status to Running
        Self::update_status(&executions, &exec_id, |exec| {
            exec.status = JobStatus::Running;
        })
        .await;

        debug!(
            job = %job_name,
            execution_id = %exec_id,
            "Starting job container"
        );

        // Start container
        if let Err(e) = runtime.start_container(&container_id).await {
            let error_msg = e.to_string();
            error!(
                job = %job_name,
                execution_id = %exec_id,
                error = %error_msg,
                "Container start failed"
            );
            Self::update_status(&executions, &exec_id, |exec| {
                exec.status = JobStatus::Failed {
                    reason: format!("Container start failed: {error_msg}"),
                    exit_code: None,
                };
                exec.completed_at = Some(Instant::now());
            })
            .await;
            // Cleanup: detach overlay (reclaim veth/IP) then remove container.
            Self::detach_overlay(
                overlay_manager.as_ref(),
                runtime.as_ref(),
                &container_id,
                &overlay_attachment,
            )
            .await;
            Self::maybe_teardown_job_segment(
                overlay_manager.as_ref(),
                &executions,
                &exec_id,
                job_name,
            )
            .await;
            let _ = runtime.remove_container(&container_id).await;
            Self::revoke_token(token_sink.as_ref(), &container_id).await;
            return;
        }

        // Wait for container to exit using the runtime's wait_container method
        let exit_code = runtime.wait_container(&container_id).await;
        let duration = started.elapsed();

        // Collect logs before cleanup using the runtime's get_logs method
        let logs = match runtime.get_logs(&container_id).await {
            Ok(entries) => Some(
                entries
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("\n"),
            ),
            Err(e) => {
                // Fallback to container_logs if get_logs fails
                match runtime.container_logs(&container_id, max_log_size).await {
                    Ok(entries) => Some(
                        entries
                            .iter()
                            .map(ToString::to_string)
                            .collect::<Vec<_>>()
                            .join("\n"),
                    ),
                    Err(e2) => {
                        warn!(
                            job = %job_name,
                            execution_id = %exec_id,
                            error = %e,
                            fallback_error = %e2,
                            "Failed to collect logs"
                        );
                        None
                    }
                }
            }
        };

        // Update final status
        Self::update_status(&executions, &exec_id, |exec| {
            exec.logs = logs;
            exec.completed_at = Some(Instant::now());

            match exit_code {
                Ok(code) => {
                    if code == 0 {
                        info!(
                            job = exec.job_name,
                            execution_id = %exec.id,
                            duration_ms = duration.as_millis(),
                            "Job completed successfully"
                        );
                        exec.status = JobStatus::Completed {
                            exit_code: code,
                            duration,
                        };
                    } else {
                        warn!(
                            job = exec.job_name,
                            execution_id = %exec.id,
                            exit_code = code,
                            duration_ms = duration.as_millis(),
                            "Job failed with non-zero exit code"
                        );
                        exec.status = JobStatus::Failed {
                            reason: format!("Non-zero exit code: {code}"),
                            exit_code: Some(code),
                        };
                    }
                }
                Err(err) => {
                    error!(
                        job = exec.job_name,
                        execution_id = %exec.id,
                        error = %err,
                        "Job execution error"
                    );
                    exec.status = JobStatus::Failed {
                        reason: err.to_string(),
                        exit_code: None,
                    };
                }
            }
        })
        .await;

        // Detach from the overlay (reclaim the veth + overlay IP) BEFORE removing
        // the container, so each job execution doesn't leak network resources.
        Self::detach_overlay(
            overlay_manager.as_ref(),
            runtime.as_ref(),
            &container_id,
            &overlay_attachment,
        )
        .await;
        Self::maybe_teardown_job_segment(overlay_manager.as_ref(), &executions, &exec_id, job_name)
            .await;

        // Cleanup container
        if let Err(e) = runtime.remove_container(&container_id).await {
            warn!(
                job = %job_name,
                execution_id = %exec_id,
                error = %e,
                "Failed to remove job container"
            );
        }

        // Revoke the job container's scoped token (best-effort).
        Self::revoke_token(token_sink.as_ref(), &container_id).await;
    }

    /// Attach a job container to the overlay network, mirroring the service
    /// path (`ServiceInstance` create flow): host-process runtimes (Linux youki)
    /// plumb a veth by PID; the macOS VZ guest gets an allocated overlay config
    /// pushed over vsock. `join_global = true` so the container gets the global
    /// overlay interface (and thus a route to the daemon node IP). Returns how it
    /// was attached so the caller can detach on exit. Best-effort: any failure is
    /// logged and yields [`OverlayAttachment::None`].
    #[cfg(not(target_os = "windows"))]
    #[allow(clippy::too_many_lines)]
    async fn attach_overlay(
        overlay_manager: Option<&Arc<RwLock<OverlayManager>>>,
        runtime: &(dyn Runtime + Send + Sync),
        container_id: &ContainerId,
        job_name: &str,
        spec: &ServiceSpec,
    ) -> OverlayAttachment {
        let Some(overlay) = overlay_manager else {
            return OverlayAttachment::None;
        };
        let guard = overlay.read().await;

        // Stand up the per-service overlay segment (bridge / dedicated WG / shared
        // bridge) BEFORE attaching — `attach_container` errors if the service
        // bridge doesn't exist. Idempotent: a repeat job execution reuses the
        // existing bridge. Mirrors the service restore path in the daemon.
        let mode = spec.overlay.as_ref().map(|o| o.mode).unwrap_or_default();
        if let Err(e) = guard.setup_service_overlay(job_name, mode).await {
            warn!(service = %job_name, error = %e, "failed to set up job overlay segment; job will have no overlay network");
            return OverlayAttachment::None;
        }

        // Per-deployment resolv.conf search domain (`<deployment>.<zone> <zone>`)
        // so the job's bare `<svc>` resolves to ITS deployment, matching the
        // service path's `dns_search_domain`. Falls back to the global zone inside
        // `attach_container` when this is `None`.
        let dns_override = guard.dns_domain().and_then(|zone| {
            spec.deployment.as_deref().map(|d| {
                let zone = zone.trim_end_matches('.');
                format!("{d}.{zone} {zone}")
            })
        });

        // Auto-fence isolation-scope modes (`Isolated`/`Dedicated`) to the job
        // name when no explicit `com.zlayer.isolation_network` label is set,
        // mirroring `ServiceInstance::isolation_network`.
        let isolation_network = {
            let explicit = spec
                .labels
                .get(zlayer_types::overlay::ISOLATION_NETWORK_LABEL)
                .cloned();
            crate::overlay_manager::resolve_isolation_network(mode, job_name, explicit)
        };

        match runtime.overlay_attach_kind_for(container_id).await {
            // Host-shared native runtime (macOS Seatbelt / native-VZ / libkrun):
            // overlayd allocates a distinct overlay /32 + utun alias for this job
            // container so it is a first-class overlay member like every other
            // runtime. Detach by the container id used at attach.
            OverlayAttachKind::HostProxy => {
                let cid = container_id.to_string();
                match guard
                    .attach_container_host_shared(
                        &cid,
                        job_name,
                        true,
                        isolation_network.clone(),
                        dns_override,
                    )
                    .await
                {
                    Ok(ip) => {
                        info!(container = %container_id, overlay_ip = %ip, "attached host-shared job container to overlay");
                        if let Err(e) = runtime.attach_overlay_ip(container_id, ip).await {
                            warn!(container = %container_id, error = %e, "failed to start host-shared overlay forwarders for job");
                        }
                        OverlayAttachment::HostShared(cid)
                    }
                    Err(e) => {
                        warn!(container = %container_id, error = %e, "failed to attach host-shared job container to overlay");
                        OverlayAttachment::None
                    }
                }
            }
            // macOS VZ-Linux guest: overlayd allocates the identity, we push it
            // into the guest over vsock.
            OverlayAttachKind::GuestManaged => {
                let cid = container_id.to_string();
                match guard
                    .attach_container_guest(
                        &cid,
                        job_name,
                        true,
                        isolation_network.clone(),
                        dns_override,
                    )
                    .await
                {
                    Ok(cfg) => match runtime.push_overlay_config(container_id, &cfg).await {
                        Ok(()) => {
                            info!(container = %container_id, overlay_ip = %cfg.overlay_ip, "attached job container to overlay (guest)");
                            OverlayAttachment::Guest(cid)
                        }
                        Err(e) => {
                            warn!(container = %container_id, error = %e, "failed to push overlay config into job guest; rolling back");
                            let _ = guard.detach_container_guest(&cid).await;
                            OverlayAttachment::None
                        }
                    },
                    Err(e) => {
                        warn!(container = %container_id, error = %e, "failed to allocate guest overlay config for job");
                        OverlayAttachment::None
                    }
                }
            }
            // Host-process runtimes (Linux youki): plumb a veth by PID.
            _ => match runtime.get_container_pid(container_id).await {
                Ok(Some(pid)) => match guard
                    .attach_container(pid, job_name, true, true, isolation_network, dns_override)
                    .await
                {
                    Ok(ip) => {
                        info!(container = %container_id, overlay_ip = %ip, "attached job container to overlay");
                        OverlayAttachment::Pid(pid)
                    }
                    Err(e) => {
                        warn!(container = %container_id, error = %e, "failed to attach job container to overlay network");
                        OverlayAttachment::None
                    }
                },
                Ok(None) => {
                    debug!(container = %container_id, "skipping job overlay attach - no PID available");
                    OverlayAttachment::None
                }
                Err(e) => {
                    warn!(container = %container_id, error = %e, "failed to read job container PID for overlay attach");
                    OverlayAttachment::None
                }
            },
        }
    }

    /// Windows containers receive their overlay (HCN) at container-create time
    /// inside overlayd, so there is no post-start attach step for jobs.
    #[cfg(target_os = "windows")]
    #[allow(clippy::unused_async)] // signature must match the Linux attach for the shared call site
    async fn attach_overlay(
        _overlay_manager: Option<&Arc<RwLock<OverlayManager>>>,
        _runtime: &(dyn Runtime + Send + Sync),
        _container_id: &ContainerId,
        _job_name: &str,
        _spec: &ServiceSpec,
    ) -> OverlayAttachment {
        OverlayAttachment::None
    }

    /// Release the overlay resources held by a job container. Mirrors the
    /// service path's detach-by-recorded-handle so an exited one-shot container
    /// never leaks its veth + overlay IP.
    #[cfg(not(target_os = "windows"))]
    async fn detach_overlay(
        overlay_manager: Option<&Arc<RwLock<OverlayManager>>>,
        runtime: &(dyn Runtime + Send + Sync),
        container_id: &ContainerId,
        attachment: &OverlayAttachment,
    ) {
        let Some(overlay) = overlay_manager else {
            return;
        };
        let guard = overlay.read().await;
        match attachment {
            OverlayAttachment::Pid(pid) => {
                if let Err(e) = guard.detach_container(*pid).await {
                    warn!(pid = pid, error = %e, "failed to detach job container from overlay (veth/IP may leak)");
                }
            }
            OverlayAttachment::Guest(id) => {
                if let Err(e) = guard.detach_container_guest(id).await {
                    warn!(id = %id, error = %e, "failed to detach job guest from overlay");
                }
            }
            OverlayAttachment::HostShared(id) => {
                if let Err(e) = runtime.detach_overlay_ip(container_id).await {
                    warn!(container = %container_id, error = %e, "failed to stop host-shared overlay forwarders for job");
                }
                if let Err(e) = guard.detach_container_host_shared(id).await {
                    warn!(id = %id, error = %e, "failed to detach host-shared job container from overlay");
                }
            }
            OverlayAttachment::None => {}
        }
    }

    /// Windows: overlay teardown happens at container-remove time inside
    /// overlayd, so there is no explicit job detach step.
    #[cfg(target_os = "windows")]
    #[allow(clippy::unused_async)] // signature must match the Linux detach for the shared call site
    async fn detach_overlay(
        _overlay_manager: Option<&Arc<RwLock<OverlayManager>>>,
        _runtime: &(dyn Runtime + Send + Sync),
        _container_id: &ContainerId,
        _attachment: &OverlayAttachment,
    ) {
    }

    /// Tear down the shared per-job overlay bridge segment created by
    /// `attach_overlay`'s `setup_service_overlay(job_name, mode)`. `detach_overlay`
    /// only reclaims the container's veth/IP — without this the per-job bridge
    /// (`zl-<deployment>-<instance>-<jobname>-b`) leaks one segment per execution.
    #[cfg(not(target_os = "windows"))]
    async fn maybe_teardown_job_segment(
        overlay_manager: Option<&Arc<RwLock<OverlayManager>>>,
        executions: &Arc<RwLock<HashMap<JobExecutionId, JobExecution>>>,
        exec_id: &JobExecutionId,
        job_name: &str,
    ) {
        let Some(overlay) = overlay_manager else {
            return;
        };
        // Only tear down the shared per-job bridge when NO OTHER execution of the
        // same job is still active — concurrent/overlapping cron runs share the
        // bridge by job_name, so tearing it down under a live sibling would cut
        // its network. This execution's own record is already terminal here.
        if Self::other_active_execution_exists(executions, exec_id, job_name).await {
            return;
        }
        let guard = overlay.read().await;
        guard.teardown_service_overlay(job_name).await;
    }

    /// Windows: overlay teardown happens at container-remove time inside
    /// overlayd, so there is no explicit per-job segment teardown step.
    #[cfg(target_os = "windows")]
    #[allow(clippy::unused_async)] // signature parity with the Linux path; Windows tears down at container-remove inside overlayd
    async fn maybe_teardown_job_segment(
        _overlay_manager: Option<&Arc<RwLock<OverlayManager>>>,
        _executions: &Arc<RwLock<HashMap<JobExecutionId, JobExecution>>>,
        _exec_id: &JobExecutionId,
        _job_name: &str,
    ) {
    }

    /// Whether any execution OTHER than `exec_id` is still active (pending /
    /// initializing / running) for the same `job_name`. Used to gate per-job
    /// bridge teardown so an overlapping cron sibling isn't cut off.
    #[cfg(not(target_os = "windows"))]
    async fn other_active_execution_exists(
        executions: &Arc<RwLock<HashMap<JobExecutionId, JobExecution>>>,
        exec_id: &JobExecutionId,
        job_name: &str,
    ) -> bool {
        let execs = executions.read().await;
        execs.values().any(|e| {
            &e.id != exec_id
                && e.job_name == job_name
                && matches!(
                    e.status,
                    JobStatus::Pending | JobStatus::Initializing | JobStatus::Running
                )
        })
    }

    /// Revoke a job container's scoped token (best-effort). The jti matches the
    /// deterministic `container:<service>:<service>-<replica>` string the runtime
    /// minted under, where `service == container_id.service` (`job-<job_name>`).
    async fn revoke_token(
        token_sink: Option<&Arc<dyn crate::auth::ContainerTokenSink>>,
        container_id: &ContainerId,
    ) {
        if let Some(sink) = token_sink {
            sink.revoke(&format!(
                "container:{}:{}-{}",
                container_id.service, container_id.service, container_id.replica
            ))
            .await;
        }
    }

    async fn update_status<F>(
        executions: &RwLock<HashMap<JobExecutionId, JobExecution>>,
        exec_id: &JobExecutionId,
        f: F,
    ) where
        F: FnOnce(&mut JobExecution),
    {
        let mut execs = executions.write().await;
        if let Some(exec) = execs.get_mut(exec_id) {
            f(exec);
        }
    }

    /// Get the status of a job execution
    pub async fn get_execution(&self, exec_id: &JobExecutionId) -> Option<JobExecution> {
        let executions = self.executions.read().await;
        executions.get(exec_id).cloned()
    }

    /// List all executions for a job
    pub async fn list_executions(&self, job_name: &str) -> Vec<JobExecution> {
        let executions = self.executions.read().await;
        executions
            .values()
            .filter(|e| e.job_name == job_name)
            .cloned()
            .collect()
    }

    /// List all executions (across all jobs)
    pub async fn list_all_executions(&self) -> Vec<JobExecution> {
        let executions = self.executions.read().await;
        executions.values().cloned().collect()
    }

    /// Cancel a running job execution
    ///
    /// # Errors
    /// Returns an error if the execution is not found or not in a cancellable state.
    pub async fn cancel(&self, exec_id: &JobExecutionId) -> Result<()> {
        let mut executions = self.executions.write().await;
        if let Some(execution) = executions.get_mut(exec_id) {
            if matches!(
                execution.status,
                JobStatus::Pending | JobStatus::Initializing | JobStatus::Running
            ) {
                if let Some(ref container_id) = execution.container_id {
                    self.runtime
                        .stop_container(container_id, Duration::from_secs(10))
                        .await?;
                    self.runtime.remove_container(container_id).await?;
                    Self::revoke_token(self.token_sink.as_ref(), container_id).await;
                }
                execution.status = JobStatus::Cancelled;
                execution.completed_at = Some(Instant::now());
                info!(
                    job = %execution.job_name,
                    execution_id = %exec_id,
                    "Job execution cancelled"
                );
            }
        }
        Ok(())
    }

    /// Clean up old execution records
    pub async fn cleanup_old_executions(&self) {
        let now = Instant::now();
        let mut executions = self.executions.write().await;
        let before_count = executions.len();
        executions.retain(|_, exec| match exec.completed_at {
            Some(completed) => now.duration_since(completed) < self.config.retention,
            None => true, // Keep running executions
        });
        let removed = before_count - executions.len();
        if removed > 0 {
            debug!(removed = removed, "Cleaned up old job execution records");
        }
    }

    /// Signal shutdown
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Relaxed);
    }

    /// Check if executor is shutting down
    pub fn is_shutting_down(&self) -> bool {
        self.shutdown.load(Ordering::Relaxed)
    }

    /// Get the number of active (non-completed) executions
    pub async fn active_execution_count(&self) -> usize {
        let executions = self.executions.read().await;
        executions
            .values()
            .filter(|e| {
                matches!(
                    e.status,
                    JobStatus::Pending | JobStatus::Initializing | JobStatus::Running
                )
            })
            .count()
    }
}

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

    fn mock_job_spec() -> ServiceSpec {
        use zlayer_spec::*;
        serde_yaml::from_str::<DeploymentSpec>(
            r"
version: v1
deployment: test
services:
  backup:
    rtype: job
    image:
      name: backup:latest
",
        )
        .unwrap()
        .services
        .remove("backup")
        .unwrap()
    }

    #[tokio::test]
    async fn test_job_execution_id() {
        let id1 = JobExecutionId::new();
        let id2 = JobExecutionId::new();
        assert_ne!(id1, id2);
        assert!(!id1.0.is_empty());
    }

    #[tokio::test]
    async fn test_job_executor_trigger() {
        let runtime: Arc<dyn Runtime + Send + Sync> = Arc::new(MockRuntime::new());
        let executor = JobExecutor::new(runtime);

        let spec = mock_job_spec();
        let exec_id = executor
            .trigger("backup", &spec, JobTrigger::Cli)
            .await
            .unwrap();

        // Give the job a moment to start
        tokio::time::sleep(Duration::from_millis(50)).await;

        let execution = executor.get_execution(&exec_id).await;
        assert!(execution.is_some());

        let exec = execution.unwrap();
        assert_eq!(exec.job_name, "backup");
        assert!(matches!(exec.trigger, JobTrigger::Cli));
    }

    #[tokio::test]
    async fn test_job_executor_list_executions() {
        let runtime: Arc<dyn Runtime + Send + Sync> = Arc::new(MockRuntime::new());
        let executor = JobExecutor::new(runtime);

        let spec = mock_job_spec();

        // Trigger multiple executions
        executor
            .trigger("backup", &spec, JobTrigger::Cli)
            .await
            .unwrap();
        executor
            .trigger("backup", &spec, JobTrigger::Scheduler)
            .await
            .unwrap();

        tokio::time::sleep(Duration::from_millis(50)).await;

        let executions = executor.list_executions("backup").await;
        assert_eq!(executions.len(), 2);
    }

    #[tokio::test]
    async fn test_job_executor_register_spec() {
        let runtime: Arc<dyn Runtime + Send + Sync> = Arc::new(MockRuntime::new());
        let executor = JobExecutor::new(runtime);

        let spec = mock_job_spec();
        executor.register_job("backup", spec.clone()).await;

        let retrieved = executor.get_job_spec("backup").await;
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().image.name, spec.image.name);
    }

    #[cfg(not(target_os = "windows"))]
    fn job_exec(id: &str, job_name: &str, status: JobStatus) -> JobExecution {
        JobExecution {
            id: JobExecutionId(id.to_string()),
            job_name: job_name.to_string(),
            status,
            started_at: None,
            completed_at: None,
            container_id: None,
            logs: None,
            trigger: JobTrigger::Cli,
        }
    }

    #[cfg(not(target_os = "windows"))]
    #[tokio::test]
    async fn test_other_active_execution_exists() {
        use std::collections::HashMap;

        let current = JobExecutionId("current".to_string());

        // (a) current terminal + sibling same-job Running -> true
        {
            let mut map: HashMap<JobExecutionId, JobExecution> = HashMap::new();
            map.insert(
                current.clone(),
                job_exec(
                    "current",
                    "backup",
                    JobStatus::Completed {
                        exit_code: 0,
                        duration: Duration::from_secs(1),
                    },
                ),
            );
            map.insert(
                JobExecutionId("sibling".to_string()),
                job_exec("sibling", "backup", JobStatus::Running),
            );
            let executions = Arc::new(RwLock::new(map));
            assert!(
                JobExecutor::other_active_execution_exists(&executions, &current, "backup").await
            );
        }

        // (b) current terminal + sibling same-job Completed -> false
        {
            let mut map: HashMap<JobExecutionId, JobExecution> = HashMap::new();
            map.insert(
                current.clone(),
                job_exec(
                    "current",
                    "backup",
                    JobStatus::Failed {
                        reason: "boom".into(),
                        exit_code: Some(1),
                    },
                ),
            );
            map.insert(
                JobExecutionId("sibling".to_string()),
                job_exec(
                    "sibling",
                    "backup",
                    JobStatus::Completed {
                        exit_code: 0,
                        duration: Duration::from_secs(1),
                    },
                ),
            );
            let executions = Arc::new(RwLock::new(map));
            assert!(
                !JobExecutor::other_active_execution_exists(&executions, &current, "backup").await
            );
        }

        // (c) a Running execution of a DIFFERENT job_name -> false
        {
            let mut map: HashMap<JobExecutionId, JobExecution> = HashMap::new();
            map.insert(
                current.clone(),
                job_exec(
                    "current",
                    "backup",
                    JobStatus::Completed {
                        exit_code: 0,
                        duration: Duration::from_secs(1),
                    },
                ),
            );
            map.insert(
                JobExecutionId("other".to_string()),
                job_exec("other", "restore", JobStatus::Running),
            );
            let executions = Arc::new(RwLock::new(map));
            assert!(
                !JobExecutor::other_active_execution_exists(&executions, &current, "backup").await
            );
        }
    }

    #[tokio::test]
    async fn test_job_status_display() {
        assert_eq!(format!("{}", JobStatus::Pending), "pending");
        assert_eq!(format!("{}", JobStatus::Running), "running");
        assert_eq!(
            format!(
                "{}",
                JobStatus::Completed {
                    exit_code: 0,
                    duration: Duration::from_secs(10)
                }
            ),
            "completed(0)"
        );
        assert_eq!(
            format!(
                "{}",
                JobStatus::Failed {
                    reason: "error".into(),
                    exit_code: Some(1)
                }
            ),
            "failed(1)"
        );
    }
}