ai-kodu-runner-protocol 0.1.0

Versioned JobSpec, JobResult and failure protocol for AI Kodu Runner
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
// Copyright 2026 Kotelnikovekb
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://apache.org
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow::{Context, Result, bail};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::{fmt, fs, path::Path};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JobSpec {
    pub api_version: String,
    pub id: String,
    #[serde(default)]
    pub attempt: u32,
    pub executor: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub execution: Option<ExecutionRequirements>,
    pub image: String,
    #[serde(default)]
    pub command: Vec<String>,
    pub working_directory: String,
    pub workspace: WorkspaceSpec,
    #[serde(default)]
    pub environment_from_runner: Vec<String>,
    #[serde(default)]
    pub secrets: Vec<SecretSpec>,
    pub resources: Resources,
    pub network: NetworkSpec,
    #[serde(default)]
    pub artifacts: Vec<String>,
    /// Explicit compatibility escape hatch for images that must write outside
    /// /workspace, for example Flutter SDK cache or an agent log directory.
    #[serde(default)]
    pub writable_rootfs: bool,
    #[serde(default)]
    pub workflow: Option<WorkflowSpec>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JobSpecV1beta1 {
    pub api_version: String,
    pub id: String,
    #[serde(default)]
    pub attempt: u32,
    pub image: String,
    #[serde(default)]
    pub command: Vec<String>,
    pub working_directory: String,
    pub workspace: WorkspaceSpec,
    #[serde(default)]
    pub environment_from_runner: Vec<String>,
    #[serde(default)]
    pub secrets: Vec<SecretSpec>,
    pub resources: Resources,
    pub network: NetworkSpec,
    #[serde(default)]
    pub artifacts: Vec<String>,
    #[serde(default)]
    pub writable_rootfs: bool,
    #[serde(default)]
    pub workflow: Option<WorkflowSpec>,
    pub execution: ExecutionRequirements,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct ExecutionRequirements {
    pub isolation: IsolationLevel,
    #[serde(default)]
    pub capabilities: Vec<String>,
}

#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum IsolationLevel {
    #[default]
    Container,
    Sandboxed,
    Dedicated,
}

impl From<JobSpecV1beta1> for JobSpec {
    fn from(spec: JobSpecV1beta1) -> Self {
        Self {
            api_version: spec.api_version,
            id: spec.id,
            attempt: spec.attempt,
            // The adapter keeps the Community executor identity for the
            // existing runtime while execution requirements remain explicit.
            executor: "docker".into(),
            execution: Some(spec.execution),
            image: spec.image,
            command: spec.command,
            working_directory: spec.working_directory,
            workspace: spec.workspace,
            environment_from_runner: spec.environment_from_runner,
            secrets: spec.secrets,
            resources: spec.resources,
            network: spec.network,
            artifacts: spec.artifacts,
            writable_rootfs: spec.writable_rootfs,
            workflow: spec.workflow,
        }
    }
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum WorkspaceSpec {
    Local {
        path: String,
    },
    ArchiveUrl {
        url: String,
    },
    Git {
        clone_url: String,
        base_branch: String,
        branch: String,
        #[serde(default)]
        base_sha: Option<String>,
        #[serde(default)]
        head_sha: Option<String>,
        username: String,
        token: String,
        commit_message: String,
        #[serde(default)]
        publish_mode: GitPublishMode,
    },
}

#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GitPublishMode {
    Disabled,
    #[default]
    IfChanged,
    Required,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Resources {
    pub cpu: f64,
    pub memory_mb: i64,
    pub pids: i64,
    pub timeout_seconds: u64,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NetworkSpec {
    pub mode: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct WorkflowSpec {
    #[serde(default)]
    pub setup: Vec<CommandSpec>,
    #[serde(default)]
    pub services: Vec<ServiceSpec>,
    #[serde(default)]
    pub initialize: Option<CommandSpec>,
    pub agent: CommandSpec,
    #[serde(default)]
    pub verifiers: Vec<VerifierSpec>,
    #[serde(default = "default_max_iterations")]
    pub max_iterations: u32,
    #[serde(default = "default_feedback_file")]
    pub feedback_file: String,
    #[serde(default)]
    pub publish: Option<CommandSpec>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ServiceSpec {
    pub name: String,
    pub image: String,
    #[serde(default)]
    pub alias: Option<String>,
    #[serde(default)]
    pub command: Vec<String>,
    #[serde(default)]
    pub environment: std::collections::HashMap<String, String>,
    #[serde(default)]
    pub healthcheck: Option<HealthcheckSpec>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HealthcheckSpec {
    pub command: Vec<String>,
    #[serde(default = "default_healthcheck_timeout")]
    pub timeout_seconds: u64,
}
fn default_healthcheck_timeout() -> u64 {
    60
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CommandSpec {
    pub command: Vec<String>,
    #[serde(default)]
    pub working_directory: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VerifierSpec {
    pub name: String,
    pub command: Vec<String>,
    #[serde(default)]
    pub working_directory: Option<String>,
    #[serde(default)]
    pub required: bool,
}
fn default_max_iterations() -> u32 {
    3
}
fn default_feedback_file() -> String {
    "/workspace/.runner/feedback.md".into()
}

pub fn validate_feedback_file(value: &str) -> Result<std::path::PathBuf> {
    let relative = value
        .strip_prefix("/workspace/")
        .or_else(|| (!value.starts_with('/')).then_some(value))
        .ok_or_else(|| anyhow::anyhow!("feedback_file must be inside /workspace"))?;
    if relative.is_empty()
        || relative.contains('\\')
        || relative.contains(':')
        || relative == "."
        || relative.starts_with("./")
        || relative.contains("/./")
        || relative.ends_with("/.")
    {
        bail!("invalid feedback_file path")
    }
    let path = Path::new(relative);
    if path.is_absolute()
        || path.components().any(|component| {
            matches!(
                component,
                std::path::Component::CurDir
                    | std::path::Component::ParentDir
                    | std::path::Component::RootDir
                    | std::path::Component::Prefix(_)
            )
        })
    {
        bail!("invalid feedback_file path")
    }
    Ok(path.to_path_buf())
}
#[derive(Clone, Deserialize, Serialize)]
pub struct SecretSpec {
    pub name: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub secret_ref: Option<String>,
}
impl fmt::Debug for SecretSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SecretSpec")
            .field("name", &self.name)
            .field("value", &"[REDACTED]")
            .field(
                "secret_ref",
                &self.secret_ref.as_ref().map(|_| "[REDACTED]"),
            )
            .finish()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum FailureKind {
    Validation,
    Policy,
    Secret,
    Execution,
    Cancellation,
    Timeout,
    Infrastructure,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FailureInfo {
    pub kind: FailureKind,
    pub code: String,
    pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobResult {
    pub job_id: String,
    pub attempt: u32,
    pub status: String,
    pub exit_code: Option<i64>,
    pub started_at: String,
    pub finished_at: String,
    pub duration_ms: u128,
    pub log_truncated: bool,
    pub stdout: String,
    pub stderr: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_summary: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub failure: Option<FailureInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub failed_phase: Option<String>,
    pub artifacts: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub artifact_dir: Option<String>,
    pub sandbox: SandboxResult,
}

#[derive(Debug, Clone, Serialize)]
pub struct LogChunk {
    pub attempt: u32,
    pub sequence: u64,
    pub stream: String,
    pub phase: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub level: Option<String>,
    pub message: String,
}

pub fn error_summary(status: &str, stdout: &str, stderr: &str) -> Option<String> {
    if status == "completed" {
        return None;
    }
    let source = if stderr.trim().is_empty() {
        stdout
    } else {
        stderr
    };
    let line = source.lines().rev().find(|line| !line.trim().is_empty())?;
    let summary = line.trim();
    Some(summary.chars().take(500).collect())
}

impl JobResult {
    pub fn failed_from_error(spec: &JobSpec, error: impl std::fmt::Display) -> Self {
        Self::failed_from_failure(
            spec,
            FailureInfo {
                kind: FailureKind::Execution,
                code: "executor_error".into(),
                message: error.to_string(),
            },
        )
    }

    pub fn failed_from_failure(spec: &JobSpec, failure: FailureInfo) -> Self {
        let now = Utc::now().to_rfc3339();
        Self {
            job_id: spec.id.clone(),
            attempt: spec.attempt,
            status: "failed".into(),
            exit_code: None,
            started_at: now.clone(),
            finished_at: now,
            duration_ms: 0,
            log_truncated: false,
            stdout: String::new(),
            stderr: failure.message.clone(),
            error_summary: Some(failure.message.clone()),
            failure: Some(failure),
            failed_phase: None,
            artifacts: Vec::new(),
            artifact_dir: None,
            sandbox: SandboxResult {
                executor: spec.executor.clone(),
                container_id: String::new(),
                image_id: None,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        ExecutionRequirements, FailureInfo, FailureKind, IsolationLevel, JobSpec, JobSpecV1beta1,
        SecretSpec, error_summary, validate_feedback_file,
    };

    #[test]
    fn v1alpha1_job_spec_fixture_remains_readable_and_valid() {
        let value: serde_json::Value =
            serde_json::from_str(include_str!("../tests/fixtures/job-spec-v1alpha1.json")).unwrap();
        let spec: JobSpec = serde_json::from_value(value.clone()).unwrap();

        spec.validate(false).unwrap();
        assert_eq!(spec.api_version, "ai-kodu-runner.dev/v1alpha1");
        assert_eq!(spec.id, "fixture-job");
        assert_eq!(serde_json::to_value(spec).unwrap(), value);
    }

    #[test]
    fn v1alpha1_job_result_fixture_preserves_required_shape() {
        let value: serde_json::Value =
            serde_json::from_str(include_str!("../tests/fixtures/job-result-v1alpha1.json"))
                .unwrap();

        assert_eq!(value["status"], "completed");
        assert_eq!(value["exit_code"], 0);
        assert_eq!(value["sandbox"]["executor"], "docker");
        assert!(value.get("error_summary").is_none());
        assert!(value.get("failed_phase").is_none());
        assert!(value.get("artifact_dir").is_none());
    }

    #[test]
    fn feedback_file_validation_accepts_workspace_relative_path() {
        assert_eq!(
            validate_feedback_file("/workspace/.runner/feedback.md").unwrap(),
            std::path::PathBuf::from(".runner/feedback.md")
        );
        assert_eq!(
            validate_feedback_file(".runner/feedback.md").unwrap(),
            std::path::PathBuf::from(".runner/feedback.md")
        );
    }

    #[test]
    fn feedback_file_validation_rejects_escape_and_platform_prefixes() {
        for path in [
            "../outside.md",
            "/workspace/../outside.md",
            "/tmp/outside.md",
            "C:\\outside.md",
            "\\\\server\\share\\outside.md",
            ".runner/./feedback.md",
        ] {
            assert!(
                validate_feedback_file(path).is_err(),
                "path should be rejected: {path}"
            );
        }
    }

    #[test]
    fn v1beta1_fixture_adapts_to_canonical_job_spec() {
        let document = include_str!("../tests/fixtures/job-spec-v1beta1.json");
        let value: serde_json::Value = serde_json::from_str(document).unwrap();
        let beta: JobSpecV1beta1 = serde_json::from_value(value).unwrap();
        let spec: JobSpec = beta.into();

        spec.validate(false).unwrap();
        assert_eq!(spec.api_version, "ai-kodu-runner.dev/v1beta1");
        assert_eq!(spec.executor, "docker");
        assert_eq!(
            spec.execution,
            Some(ExecutionRequirements {
                isolation: IsolationLevel::Container,
                capabilities: vec!["artifacts".into(), "streaming_logs".into()]
            })
        );
    }

    #[test]
    fn loader_adapts_v1beta1_and_rejects_unknown_versions() {
        let spec =
            JobSpec::from_json(include_str!("../tests/fixtures/job-spec-v1beta1.json")).unwrap();
        assert_eq!(spec.id, "fixture-beta-job");

        let error = JobSpec::from_json(r#"{"api_version":"ai-kodu-runner.dev/v9"}"#).unwrap_err();
        assert!(error.to_string().contains("unsupported api_version"));
    }

    #[test]
    fn v1beta1_requires_execution_requirements() {
        let mut spec = JobSpec {
            api_version: "ai-kodu-runner.dev/v1beta1".into(),
            id: "missing-execution".into(),
            attempt: 1,
            executor: "docker".into(),
            execution: None,
            image: "example/runner:latest".into(),
            command: vec!["true".into()],
            working_directory: "/workspace".into(),
            workspace: super::WorkspaceSpec::Local { path: ".".into() },
            environment_from_runner: Vec::new(),
            secrets: Vec::new(),
            resources: super::Resources {
                cpu: 1.0,
                memory_mb: 256,
                pids: 64,
                timeout_seconds: 60,
            },
            network: super::NetworkSpec {
                mode: "none".into(),
            },
            artifacts: Vec::new(),
            writable_rootfs: false,
            workflow: None,
        };

        assert!(spec.validate(false).is_err());
        spec.execution = Some(ExecutionRequirements {
            isolation: IsolationLevel::Sandboxed,
            capabilities: vec!["sandboxed".into()],
        });
        assert!(spec.validate(false).is_ok());
    }

    #[test]
    fn secret_ref_is_serialized_without_secret_value() {
        let spec = SecretSpec {
            name: "MODEL_KEY".into(),
            value: String::new(),
            secret_ref: Some("vault://team/model-key".into()),
        };
        let json = serde_json::to_value(&spec).unwrap();

        assert_eq!(json["secret_ref"], "vault://team/model-key");
        assert!(json.get("value").is_none());
        assert!(!format!("{spec:?}").contains("vault://team/model-key"));
    }

    #[test]
    fn failed_result_contains_typed_failure() {
        let spec =
            JobSpec::from_json(include_str!("../tests/fixtures/job-spec-v1alpha1.json")).unwrap();
        let result = super::JobResult::failed_from_failure(
            &spec,
            FailureInfo {
                kind: FailureKind::Secret,
                code: "secret_ref_unresolvable".into(),
                message: "secret_ref is not resolvable by the Community executor".into(),
            },
        );
        let json = serde_json::to_value(result).unwrap();

        assert_eq!(json["failure"]["kind"], "secret");
        assert_eq!(json["failure"]["code"], "secret_ref_unresolvable");
    }

    #[test]
    fn error_summary_prefers_stderr_and_truncates() {
        assert_eq!(
            error_summary("failed", "agent output", "first\nUnexpected server error\n"),
            Some("Unexpected server error".into())
        );
        assert_eq!(error_summary("completed", "", ""), None);
    }
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxResult {
    pub executor: String,
    pub container_id: String,
    pub image_id: Option<String>,
}
impl JobSpec {
    pub fn from_path(p: &Path) -> Result<Self> {
        let document =
            fs::read_to_string(p).with_context(|| format!("read job {}", p.display()))?;
        Self::from_json(&document)
    }

    pub fn from_json(document: &str) -> Result<Self> {
        let value: serde_json::Value = serde_json::from_str(document).context("parse job JSON")?;
        match value.get("api_version").and_then(serde_json::Value::as_str) {
            Some("ai-kodu-runner.dev/v1alpha1") => {
                serde_json::from_value(value).context("parse v1alpha1 job JSON")
            }
            Some("ai-kodu-runner.dev/v1beta1") => {
                let spec: JobSpecV1beta1 =
                    serde_json::from_value(value).context("parse v1beta1 job JSON")?;
                Ok(spec.into())
            }
            Some(version) => bail!("unsupported api_version: {version}"),
            None => bail!("missing api_version"),
        }
    }
    pub fn validate(&self, daemon: bool) -> Result<()> {
        if !["ai-kodu-runner.dev/v1alpha1", "ai-kodu-runner.dev/v1beta1"]
            .contains(&self.api_version.as_str())
        {
            bail!("unsupported api_version")
        }
        if self.executor != "docker" {
            bail!("unsupported executor")
        }
        if self.api_version == "ai-kodu-runner.dev/v1beta1" && self.execution.is_none() {
            bail!("v1beta1 execution requirements are required")
        }
        if let Some(execution) = &self.execution {
            if execution.capabilities.len() > 32 {
                bail!("too many execution capabilities")
            }
            let mut capabilities = std::collections::HashSet::new();
            for capability in &execution.capabilities {
                if capability.is_empty()
                    || capability.len() > 64
                    || !capability
                        .bytes()
                        .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.'))
                    || !capabilities.insert(capability)
                {
                    bail!("invalid execution capability")
                }
            }
        }
        if self.workflow.is_none() && self.command.is_empty() {
            bail!("command must not be empty")
        }
        if let Some(workflow) = &self.workflow {
            if workflow
                .initialize
                .as_ref()
                .is_some_and(|command| command.command.is_empty())
            {
                bail!("workflow initialize command must not be empty")
            }
            if workflow.agent.command.is_empty() {
                bail!("workflow agent command must not be empty")
            }
            if workflow.max_iterations == 0 || workflow.max_iterations > 20 {
                bail!("workflow max_iterations must be between 1 and 20")
            }
            validate_feedback_file(&workflow.feedback_file)?;
            if workflow
                .verifiers
                .iter()
                .any(|v| v.name.trim().is_empty() || v.command.is_empty())
            {
                bail!("workflow verifier names and commands must not be empty")
            }
            let mut service_names = std::collections::HashSet::new();
            if workflow.services.len() > 16 {
                bail!("too many workflow services")
            }
            for service in &workflow.services {
                if service.name.trim().is_empty()
                    || !service_names.insert(&service.name)
                    || service.image.trim().is_empty()
                    || service.image.contains(char::is_whitespace)
                    || !service
                        .name
                        .bytes()
                        .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-' || b == b'.')
                {
                    bail!("invalid workflow service")
                }
                if daemon && !service.image.contains("@sha256:") {
                    bail!("daemon workflow services require image digests")
                }
                if let Some(alias) = &service.alias
                    && (alias.trim().is_empty()
                        || alias.contains(char::is_whitespace)
                        || !alias.bytes().all(|b| {
                            b.is_ascii_alphanumeric() || b == b'_' || b == b'-' || b == b'.'
                        }))
                {
                    bail!("invalid workflow service alias")
                }
                if let Some(healthcheck) = &service.healthcheck
                    && (healthcheck.command.is_empty()
                        || healthcheck.timeout_seconds == 0
                        || healthcheck.timeout_seconds > 3600)
                {
                    bail!("invalid workflow service healthcheck")
                }
            }
        }
        if self.image.trim().is_empty() || self.image.contains(char::is_whitespace) {
            bail!("invalid image reference")
        }
        if daemon && !self.image.contains("@sha256:") {
            bail!("daemon jobs require an image digest")
        }
        if !["bridge", "none"].contains(&self.network.mode.as_str()) {
            bail!("unsupported network mode")
        }
        if self.secrets.len() > 32 {
            bail!("too many secrets")
        }
        for secret in &self.secrets {
            if secret.name.is_empty()
                || secret.name.len() > 128
                || !secret
                    .name
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b == b'_')
            {
                bail!("invalid secret name")
            }
            if secret.secret_ref.is_some() && !secret.value.is_empty() {
                bail!("secret must use either value or secret_ref")
            }
            if let Some(secret_ref) = &secret.secret_ref
                && (secret_ref.is_empty() || secret_ref.len() > 512)
            {
                bail!("invalid secret_ref")
            }
            if secret.value.len() > 64 * 1024 {
                bail!("secret is too large")
            }
        }
        Ok(())
    }
}