openjd-model 0.1.0

Open Job Description model library — parsing, validation, and job creation
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

//! Pass 8: Format string validation.
//!
//! Validates all format string references resolve to defined variables by
//! building scope-appropriate symbol tables and evaluating expressions against
//! them. This works for both base spec (simple `{{Param.X}}` references) and
//! EXPR (full expressions) — the evaluator handles both.

use std::collections::HashSet;

use openjd_expr::eval::ParsedExpression;
use openjd_expr::function_library::FunctionLibrary;
use openjd_expr::path_mapping::PathFormat;
use openjd_expr::symbol_table::SymbolTable;
use openjd_expr::types::ExprType;
use openjd_expr::value::ExprValue;
use openjd_expr::FormatString;

use crate::error::{path_field, path_index, PathElement, ValidationErrors};
use crate::template::*;
use crate::types::{ModelExtension, ValidationContext};

/// Build a symbol table containing Param/RawParam entries from job parameter definitions.
/// RawParam.* is always STRING for PATH types and LIST_STRING for LIST_PATH types,
/// matching Python behavior where RawParam holds the raw unprocessed value.
fn build_param_symtab(jt: &JobTemplate) -> SymbolTable {
    use crate::types::JobParameterType;
    let mut symtab = SymbolTable::new();
    if let Some(params) = &jt.parameter_definitions {
        for p in params {
            let pt = p.job_param_type();
            let expr_type = pt.expr_type();
            symtab
                .set(
                    &format!("Param.{}", p.name()),
                    ExprValue::unresolved(expr_type.clone()),
                )
                .expect("symtab");
            let raw_type = match pt {
                JobParameterType::Path => ExprType::STRING,
                JobParameterType::ListPath => ExprType::list(ExprType::STRING),
                _ => expr_type,
            };
            symtab
                .set(
                    &format!("RawParam.{}", p.name()),
                    ExprValue::unresolved(raw_type),
                )
                .expect("symtab");
        }
    }
    symtab
}

/// Build the template-scope symtab (for job name, host requirements, parameter space ranges).
/// PATH and LIST[PATH] Param.* are excluded (host-only); RawParam.* is always STRING for path types.
fn build_template_scope_symtab(jt: &JobTemplate) -> SymbolTable {
    use crate::types::JobParameterType;
    let mut symtab = SymbolTable::new();
    if let Some(params) = &jt.parameter_definitions {
        for p in params {
            let pt = p.job_param_type();
            let expr_type = pt.expr_type();
            let is_path = matches!(pt, JobParameterType::Path | JobParameterType::ListPath);
            if !is_path {
                symtab
                    .set(
                        &format!("Param.{}", p.name()),
                        ExprValue::unresolved(expr_type.clone()),
                    )
                    .expect("symtab");
            }
            let raw_type = match pt {
                JobParameterType::Path => ExprType::STRING,
                JobParameterType::ListPath => ExprType::list(ExprType::STRING),
                _ => expr_type,
            };
            symtab
                .set(
                    &format!("RawParam.{}", p.name()),
                    ExprValue::unresolved(raw_type),
                )
                .expect("symtab");
        }
    }
    symtab
}

/// Build the session-scope symtab (for environment scripts/variables).
/// Contains: Param.*, RawParam.*, Session.*, Env.File.*, let bindings
/// When `is_step_env` is true, also includes Step.Name (EXPR only).
fn build_session_scope_symtab(
    jt: &JobTemplate,
    env: &Environment,
    is_step_env: bool,
    expr_active: bool,
) -> SymbolTable {
    let mut symtab = build_param_symtab(jt);
    symtab
        .set(
            "Session.WorkingDirectory",
            ExprValue::unresolved(ExprType::PATH),
        )
        .expect("symtab");
    symtab
        .set(
            "Session.HasPathMappingRules",
            ExprValue::unresolved(ExprType::BOOL),
        )
        .expect("symtab");
    symtab
        .set(
            "Session.PathMappingRulesFile",
            ExprValue::unresolved(ExprType::PATH),
        )
        .expect("symtab");
    // Step.Name available in step environments with EXPR
    if is_step_env && expr_active {
        symtab
            .set("Step.Name", ExprValue::unresolved(ExprType::STRING))
            .expect("symtab");
    }
    // Job.Name available in all environments with EXPR
    if expr_active {
        symtab
            .set("Job.Name", ExprValue::unresolved(ExprType::STRING))
            .expect("symtab");
    }
    // Env.File.* from this environment's script
    if let Some(script) = &env.script {
        if let Some(files) = &script.embedded_files {
            for f in files {
                symtab
                    .set(
                        &format!("Env.File.{}", f.name),
                        ExprValue::unresolved(ExprType::PATH),
                    )
                    .expect("symtab");
            }
        }
    }
    symtab
}

/// Build the task-scope symtab (for step scripts).
/// Contains: Param.*, RawParam.*, Session.*, Task.Param.*, Task.RawParam.*,
///           Task.File.*, Job.Name, Step.Name, Env.File.* from step envs,
///           plus let bindings.
fn build_task_scope_symtab(
    jt: &JobTemplate,
    step: &StepTemplate,
    expr_active: bool,
) -> SymbolTable {
    let mut symtab = build_param_symtab(jt);

    // Session scope
    symtab
        .set(
            "Session.WorkingDirectory",
            ExprValue::unresolved(ExprType::PATH),
        )
        .expect("symtab");
    symtab
        .set(
            "Session.HasPathMappingRules",
            ExprValue::unresolved(ExprType::BOOL),
        )
        .expect("symtab");
    symtab
        .set(
            "Session.PathMappingRulesFile",
            ExprValue::unresolved(ExprType::PATH),
        )
        .expect("symtab");

    // EXPR-only built-ins
    if expr_active {
        symtab
            .set("Job.Name", ExprValue::unresolved(ExprType::STRING))
            .expect("symtab");
        symtab
            .set("Step.Name", ExprValue::unresolved(ExprType::STRING))
            .expect("symtab");
    }

    // Task parameters
    if let Some(ps) = &step.parameter_space {
        for tp in &ps.task_parameter_definitions {
            let tp_type = match tp {
                TaskParameterDefinition::INT(_) => ExprType::INT,
                TaskParameterDefinition::CHUNK_INT(_) => ExprType::RANGE_EXPR,
                TaskParameterDefinition::FLOAT(_) => ExprType::FLOAT,
                TaskParameterDefinition::STRING(_) => ExprType::STRING,
                TaskParameterDefinition::PATH(_) => ExprType::PATH,
            };
            symtab
                .set(
                    &format!("Task.Param.{}", tp.name()),
                    ExprValue::unresolved(tp_type.clone()),
                )
                .expect("symtab");
            // Task.RawParam.* for PATH is STRING (raw unprocessed value)
            let raw_type = match tp {
                TaskParameterDefinition::PATH(_) => ExprType::STRING,
                _ => tp_type,
            };
            symtab
                .set(
                    &format!("Task.RawParam.{}", tp.name()),
                    ExprValue::unresolved(raw_type),
                )
                .expect("symtab");
        }
    }

    // Task.File.* from step script embedded files
    if let Some(script) = step
        .resolve_syntax_sugar()
        .ok()
        .flatten()
        .as_ref()
        .or(step.script.as_ref())
    {
        if let Some(files) = &script.embedded_files {
            for f in files {
                symtab
                    .set(
                        &format!("Task.File.{}", f.name),
                        ExprValue::unresolved(ExprType::PATH),
                    )
                    .expect("symtab");
            }
        }
    }

    // Env.File.* is NOT available in step scripts — it is only available
    // within environment scripts (§7.3). The task-scope symtab is used for
    // step script validation, so we do not add Env.File.* here.

    // Let bindings are evaluated during validation (validate_let_bindings),
    // which adds them to the symtab with inferred types. The symtab passed
    // here is cloned and mutated during validation, so we don't add them here.

    symtab
}

/// Validate a format string against a symbol table, reporting errors at the given path.
fn validate_fs(
    fs: &FormatString,
    symtab: &SymbolTable,
    lib: &FunctionLibrary,
    path: &[PathElement],
    errors: &mut ValidationErrors,
) {
    if fs.is_literal() {
        return;
    }
    if let Err(e) = fs.validate_expressions(symtab, lib) {
        let mut spans = Vec::new();
        if let Some(ref expr_err) = e.expression_error {
            if !expr_err.sub_errors().is_empty() {
                // Compound error (e.g., if/else both branches fail) — one span per sub-error
                for sub in expr_err.sub_errors() {
                    if let (Some(expr), Some(col), Some(end_col)) =
                        (sub.expr(), sub.col_offset(), sub.end_col_offset())
                    {
                        spans.push(crate::error::DiagnosticSpan {
                            summary: sub.message(),
                            source: expr.to_string(),
                            start: col,
                            end: end_col,
                            caret: sub.caret_offset().unwrap_or(0),
                        });
                    }
                }
            }
        }
        // Fallback: single span covering the whole interpolation
        if spans.is_empty() {
            spans.push(crate::error::DiagnosticSpan {
                summary: e.message.clone(),
                source: e.input.clone(),
                start: e.start,
                end: e.end,
                caret: 0,
            });
        }
        let summary = if let Some(ref expr_err) = e.expression_error {
            expr_err.message()
        } else {
            e.message.clone()
        };
        let detail = crate::error::ErrorDetail { summary, spans };
        errors.add_with_detail(
            path,
            format!(
                "Failed to parse interpolation expression at [{}, {}]. {}",
                e.start, e.end, e.message
            ),
            detail,
        );
    }
}

/// Validate a format string in an action (command + args).
fn validate_action_fs(
    action: &Action,
    symtab: &SymbolTable,
    lib: &FunctionLibrary,
    action_path: &[PathElement],
    errors: &mut ValidationErrors,
) {
    validate_fs(
        &action.command,
        symtab,
        lib,
        &path_field(action_path, "command"),
        errors,
    );
    if let Some(args) = &action.args {
        let args_path = path_field(action_path, "args");
        for (j, arg) in args.iter().enumerate() {
            validate_fs(arg, symtab, lib, &path_index(&args_path, j), errors);
        }
    }
}

pub fn validate_format_strings(
    jt: &JobTemplate,
    ctx: &ValidationContext,
    errors: &mut ValidationErrors,
) {
    let expr_active = ctx.profile.has_extension(ModelExtension::Expr);
    // Template/task-range validation uses HostContext::None (host functions
    // are not available in those scopes). Session/task scopes use
    // HostContext::Unresolved so apply_path_mapping type-checks.
    let template_profile = ctx.profile.to_expr_profile(openjd_expr::HostContext::None);
    let host_profile = ctx
        .profile
        .to_expr_profile(openjd_expr::HostContext::Unresolved);
    let template_lib = openjd_expr::FunctionLibrary::for_profile(&template_profile);
    let host_lib = openjd_expr::FunctionLibrary::for_profile(&host_profile);

    // ── Job name: template scope (Param/RawParam only) ──
    let template_symtab = build_template_scope_symtab(jt);
    validate_fs(
        &jt.name,
        &template_symtab,
        &template_lib,
        &path_field(&[], "name"),
        errors,
    );

    // ── Host requirements: template scope + step let bindings ──
    for (i, step) in jt.steps.iter().enumerate() {
        let step_path = vec![PathElement::Field("steps".into()), PathElement::Index(i)];
        if let Some(hr) = &step.host_requirements {
            // Build a symtab with Param/RawParam + step let bindings
            let mut hr_symtab = build_template_scope_symtab(jt);
            if expr_active {
                hr_symtab
                    .set("Job.Name", ExprValue::unresolved(ExprType::STRING))
                    .expect("symtab");
                hr_symtab
                    .set("Step.Name", ExprValue::unresolved(ExprType::STRING))
                    .expect("symtab");
                if let Some(bindings) = &step.let_bindings {
                    let let_path = path_field(&step_path, "let");
                    let mut hr_let_names = HashSet::new();
                    validate_let_bindings(
                        bindings,
                        &let_path,
                        &HashSet::new(),
                        &mut hr_let_names,
                        &mut hr_symtab,
                        &template_lib,
                        &template_profile,
                        errors,
                    );
                }
            }
            let hr_path = path_field(&step_path, "hostRequirements");
            if let Some(amounts) = &hr.amounts {
                for (j, amt) in amounts.iter().enumerate() {
                    let amt_path = path_index(&path_field(&hr_path, "amounts"), j);
                    if let Some(min) = &amt.min {
                        validate_fs(
                            min,
                            &hr_symtab,
                            &template_lib,
                            &path_field(&amt_path, "min"),
                            errors,
                        );
                    }
                    if let Some(max) = &amt.max {
                        validate_fs(
                            max,
                            &hr_symtab,
                            &template_lib,
                            &path_field(&amt_path, "max"),
                            errors,
                        );
                    }
                }
            }
            if let Some(attrs) = &hr.attributes {
                for (j, attr) in attrs.iter().enumerate() {
                    let attr_path = path_index(&path_field(&hr_path, "attributes"), j);
                    if let Some(any_of) = &attr.any_of {
                        for (k, v) in any_of.iter().enumerate() {
                            validate_fs(
                                v,
                                &hr_symtab,
                                &template_lib,
                                &path_index(&path_field(&attr_path, "anyOf"), k),
                                errors,
                            );
                        }
                    }
                    if let Some(all_of) = &attr.all_of {
                        for (k, v) in all_of.iter().enumerate() {
                            validate_fs(
                                v,
                                &hr_symtab,
                                &template_lib,
                                &path_index(&path_field(&attr_path, "allOf"), k),
                                errors,
                            );
                        }
                    }
                }
            }
        }
    }

    // ── Job environments: session scope ──
    if let Some(envs) = &jt.job_environments {
        let envs_path = path_field(&[], "jobEnvironments");
        for (i, env) in envs.iter().enumerate() {
            let mut env_symtab = build_session_scope_symtab(jt, env, false, expr_active);
            // Evaluate env script let bindings
            if expr_active {
                if let Some(script) = &env.script {
                    if let Some(bindings) = &script.let_bindings {
                        let let_path =
                            path_field(&path_field(&path_index(&envs_path, i), "script"), "let");
                        let mut env_let_names = HashSet::new();
                        validate_let_bindings(
                            bindings,
                            &let_path,
                            &HashSet::new(),
                            &mut env_let_names,
                            &mut env_symtab,
                            &host_lib,
                            &host_profile,
                            errors,
                        );
                    }
                }
            }
            validate_env_format_strings(
                env,
                &env_symtab,
                &host_lib,
                &path_index(&envs_path, i),
                errors,
            );
        }
    }

    // ── Steps ──
    for (i, step) in jt.steps.iter().enumerate() {
        let step_path = vec![PathElement::Field("steps".into()), PathElement::Index(i)];

        // Task parameter ranges use TEMPLATE scope (no PATH Param.*)
        if let Some(ps) = &step.parameter_space {
            let ps_path = path_field(&step_path, "parameterSpace");
            let tpd_path = path_field(&ps_path, "taskParameterDefinitions");
            let mut range_symtab = build_template_scope_symtab(jt);
            if expr_active {
                range_symtab
                    .set("Job.Name", ExprValue::unresolved(ExprType::STRING))
                    .expect("symtab");
                range_symtab
                    .set("Step.Name", ExprValue::unresolved(ExprType::STRING))
                    .expect("symtab");
                // Step-level let bindings are in template scope and must be
                // visible in parameterSpace range expressions.
                if let Some(bindings) = &step.let_bindings {
                    for binding in bindings {
                        if let Some(eq_pos) = binding.find('=') {
                            let name = binding[..eq_pos].trim();
                            let expr_str = binding[eq_pos + 1..].trim();
                            if !name.is_empty() && !expr_str.is_empty() {
                                match openjd_expr::eval::ParsedExpression::with_profile(
                                    expr_str,
                                    &template_profile,
                                ) {
                                    Ok(parsed) => {
                                        match parsed
                                            .with_path_format(PathFormat::Posix)
                                            .with_library(&template_lib)
                                            .evaluate(&[&range_symtab as &SymbolTable])
                                        {
                                            Ok(val) => {
                                                let _ = range_symtab.set(name, val);
                                            }
                                            Err(e) => {
                                                errors.add(
                                                    &step_path,
                                                    format!("let binding '{name}': {e}"),
                                                );
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        errors
                                            .add(&step_path, format!("let binding '{name}': {e}"));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            for (j, tp) in ps.task_parameter_definitions.iter().enumerate() {
                let p_path = path_index(&tpd_path, j);
                match tp {
                    TaskParameterDefinition::INT(t) => {
                        if let crate::template::IntRange::Expression(expr) = &t.range {
                            validate_fs(
                                expr,
                                &range_symtab,
                                &template_lib,
                                &path_field(&p_path, "range"),
                                errors,
                            );
                        }
                    }
                    TaskParameterDefinition::STRING(t) => {
                        if let crate::template::StringRange::List(items) = &t.range {
                            for (k, item) in items.iter().enumerate() {
                                validate_fs(
                                    item,
                                    &range_symtab,
                                    &template_lib,
                                    &path_index(&path_field(&p_path, "range"), k),
                                    errors,
                                );
                            }
                        }
                    }
                    TaskParameterDefinition::PATH(t) => {
                        if let crate::template::StringRange::List(items) = &t.range {
                            for (k, item) in items.iter().enumerate() {
                                validate_fs(
                                    item,
                                    &range_symtab,
                                    &template_lib,
                                    &path_index(&path_field(&p_path, "range"), k),
                                    errors,
                                );
                            }
                        }
                    }
                    TaskParameterDefinition::CHUNK_INT(t) => {
                        if let crate::template::IntRange::Expression(expr) = &t.range {
                            validate_fs(
                                expr,
                                &range_symtab,
                                &template_lib,
                                &path_field(&p_path, "range"),
                                errors,
                            );
                        }
                    }
                    _ => {}
                }
            }
        }

        let mut task_symtab = build_task_scope_symtab(jt, step, expr_active);

        // Let bindings: validate and evaluate into symtab if EXPR, reject if not.
        // Step-level let bindings are TEMPLATE scope (template_lib, no Session.*, no PATH Param.*).
        // Script-level let bindings are TASK scope (host_lib).
        if let Some(bindings) = &step.let_bindings {
            let let_path = path_field(&step_path, "let");
            if !expr_active {
                errors.add(&let_path, "'let' requires the EXPR extension.");
            } else {
                // Build a template-scope symtab for step let bindings:
                // Param.* (non-PATH), RawParam.*, Job.Name, Step.Name — no Session.*, no Task.*
                let mut step_let_symtab = build_template_scope_symtab(jt);
                step_let_symtab
                    .set("Job.Name", ExprValue::unresolved(ExprType::STRING))
                    .expect("symtab");
                step_let_symtab
                    .set("Step.Name", ExprValue::unresolved(ExprType::STRING))
                    .expect("symtab");
                let mut step_let_names = HashSet::new();
                validate_let_bindings(
                    bindings,
                    &let_path,
                    &HashSet::new(),
                    &mut step_let_names,
                    &mut step_let_symtab,
                    &template_lib,
                    &template_profile,
                    errors,
                );
                // Copy evaluated let bindings into task_symtab so script-level code can use them
                for name in &step_let_names {
                    if let Some(val) = step_let_symtab.get_value(name) {
                        let _ = task_symtab.set(name, val.clone());
                    }
                }
            }
        }

        if let Some(script) = &step.script {
            let script_path = path_field(&step_path, "script");

            // Script-level let bindings (TASK scope — host_lib)
            if let Some(bindings) = &script.let_bindings {
                let let_path = path_field(&script_path, "let");
                if !expr_active {
                    errors.add(&let_path, "'let' requires the EXPR extension.");
                } else {
                    let enclosing: HashSet<String> = step
                        .let_bindings
                        .as_ref()
                        .map(|bs| {
                            bs.iter()
                                .filter_map(|b| b.find('=').map(|eq| b[..eq].trim().to_string()))
                                .collect()
                        })
                        .unwrap_or_default();
                    let mut script_let_names = HashSet::new();
                    validate_let_bindings(
                        bindings,
                        &let_path,
                        &enclosing,
                        &mut script_let_names,
                        &mut task_symtab,
                        &host_lib,
                        &host_profile,
                        errors,
                    );
                }
            }

            // Complex expressions: reject if not EXPR
            if !expr_active {
                let action_path = path_field(&path_field(&script_path, "actions"), "onRun");
                if script.actions.on_run.command.has_complex_expressions() {
                    errors.add(
                        &path_field(&action_path, "command"),
                        "complex expressions require the EXPR extension.",
                    );
                }
                if let Some(args) = &script.actions.on_run.args {
                    let args_path = path_field(&action_path, "args");
                    for (j, arg) in args.iter().enumerate() {
                        if arg.has_complex_expressions() {
                            errors.add(
                                &path_index(&args_path, j),
                                "complex expressions require the EXPR extension.",
                            );
                        }
                    }
                }
            }

            // Validate all format string references (both base and EXPR)
            let action_path = path_field(&path_field(&script_path, "actions"), "onRun");
            validate_action_fs(
                &script.actions.on_run,
                &task_symtab,
                &host_lib,
                &action_path,
                errors,
            );

            // Timeout
            if let Some(timeout) = &script.actions.on_run.timeout {
                validate_fs(
                    timeout,
                    &task_symtab,
                    &host_lib,
                    &path_field(&action_path, "timeout"),
                    errors,
                );
            }
            if let Some(CancelationMode::NotifyThenTerminate {
                notify_period_in_seconds: Some(notify),
            }) = &script.actions.on_run.cancelation
            {
                validate_fs(
                    notify,
                    &task_symtab,
                    &host_lib,
                    &path_field(&action_path, "cancelation"),
                    errors,
                );
            }

            // Embedded files
            if let Some(files) = &script.embedded_files {
                let files_path = path_field(&script_path, "embeddedFiles");
                for (j, f) in files.iter().enumerate() {
                    let f_path = path_index(&files_path, j);
                    if let Some(data) = &f.data {
                        validate_fs(
                            data,
                            &task_symtab,
                            &host_lib,
                            &path_field(&f_path, "data"),
                            errors,
                        );
                    }
                    if let Some(filename) = &f.filename {
                        validate_fs(
                            filename,
                            &task_symtab,
                            &host_lib,
                            &path_field(&f_path, "filename"),
                            errors,
                        );
                    }
                }
            }

            // EXPR-only: comprehension variable validation
            if expr_active {
                let mut all_let_names: HashSet<String> = HashSet::new();
                if let Some(bindings) = &step.let_bindings {
                    for b in bindings {
                        if let Some(eq) = b.find('=') {
                            all_let_names.insert(b[..eq].trim().to_string());
                        }
                    }
                }
                if let Some(bindings) = &script.let_bindings {
                    for b in bindings {
                        if let Some(eq) = b.find('=') {
                            all_let_names.insert(b[..eq].trim().to_string());
                        }
                    }
                }
                if !all_let_names.is_empty() {
                    if let Err(e) = script
                        .actions
                        .on_run
                        .command
                        .validate_comprehension_vars(&all_let_names)
                    {
                        errors.add(&path_field(&action_path, "command"), e.to_string());
                    }
                    if let Some(args) = &script.actions.on_run.args {
                        let args_path = path_field(&action_path, "args");
                        for (j, arg) in args.iter().enumerate() {
                            if let Err(e) = arg.validate_comprehension_vars(&all_let_names) {
                                errors.add(&path_index(&args_path, j), e.to_string());
                            }
                        }
                    }
                }
            }
        }

        // Step environments: session scope + step let bindings
        if let Some(envs) = &step.step_environments {
            let envs_path = path_field(&step_path, "stepEnvironments");
            for (j, env) in envs.iter().enumerate() {
                let mut env_symtab = build_session_scope_symtab(jt, env, true, expr_active);
                // Copy step-level let binding values (already evaluated with inferred types)
                if expr_active {
                    if let Some(bindings) = &step.let_bindings {
                        for b in bindings {
                            if let Some(eq) = b.find('=') {
                                let name = b[..eq].trim();
                                if !name.is_empty() {
                                    if let Some(
                                        openjd_expr::symbol_table::SymbolTableEntry::Value(val),
                                    ) = task_symtab.get(name)
                                    {
                                        let _ = env_symtab.set(name, val.clone());
                                    }
                                }
                            }
                        }
                    }
                }
                // Evaluate env script let bindings into env_symtab
                if let Some(script) = &env.script {
                    if let Some(bindings) = &script.let_bindings {
                        if expr_active {
                            let env_let_path = path_field(
                                &path_field(&path_index(&envs_path, j), "script"),
                                "let",
                            );
                            let enclosing: HashSet<String> = step
                                .let_bindings
                                .as_ref()
                                .map(|bs| {
                                    bs.iter()
                                        .filter_map(|b| {
                                            b.find('=').map(|eq| b[..eq].trim().to_string())
                                        })
                                        .collect()
                                })
                                .unwrap_or_default();
                            let mut env_let_names = HashSet::new();
                            validate_let_bindings(
                                bindings,
                                &env_let_path,
                                &enclosing,
                                &mut env_let_names,
                                &mut env_symtab,
                                &host_lib,
                                &host_profile,
                                errors,
                            );
                        }
                    }
                }
                validate_env_format_strings(
                    env,
                    &env_symtab,
                    &host_lib,
                    &path_index(&envs_path, j),
                    errors,
                );
            }
        }

        // SimpleAction let bindings (requires both FB1 and EXPR)
        if expr_active {
            for sa in [
                &step.bash,
                &step.python,
                &step.cmd,
                &step.powershell,
                &step.node,
            ]
            .into_iter()
            .flatten()
            {
                let mut sa_let_names: HashSet<String> = HashSet::new();
                if let Some(bindings) = &step.let_bindings {
                    for b in bindings {
                        if let Some(eq) = b.find('=') {
                            sa_let_names.insert(b[..eq].trim().to_string());
                        }
                    }
                }
                if let Some(let_bindings) = &sa.let_bindings {
                    if ctx.profile.has_extension(ModelExtension::FeatureBundle1) {
                        let enclosing: HashSet<String> = step
                            .let_bindings
                            .as_ref()
                            .map(|bs| {
                                bs.iter()
                                    .filter_map(|b| {
                                        b.find('=').map(|eq| b[..eq].trim().to_string())
                                    })
                                    .collect()
                            })
                            .unwrap_or_default();
                        let mut new_names = HashSet::new();
                        validate_let_bindings(
                            let_bindings,
                            &step_path,
                            &enclosing,
                            &mut new_names,
                            &mut task_symtab,
                            &host_lib,
                            &host_profile,
                            errors,
                        );
                        sa_let_names.extend(new_names);
                    }
                }
                if !sa_let_names.is_empty() {
                    match FormatString::new(&sa.script) {
                        Ok(fs) => {
                            if let Err(e) = fs.validate_comprehension_vars(&sa_let_names) {
                                errors.add(&step_path, e.to_string());
                            }
                        }
                        Err(e) => {
                            errors.add(&step_path, format!("SimpleAction script: {e}"));
                        }
                    }
                    if let Some(args) = &sa.args {
                        for arg in args {
                            if let Err(e) = arg.validate_comprehension_vars(&sa_let_names) {
                                errors.add(&step_path, e.to_string());
                            }
                        }
                    }
                }
            }
        }
    }

    // Environment script comprehension validation (EXPR only)
    if expr_active {
        validate_env_comprehensions(&jt.job_environments, errors);
        for step in &jt.steps {
            validate_env_comprehensions(&step.step_environments, errors);
        }
    }
}

/// Validate format strings within an environment (variables + script actions).
fn validate_env_format_strings(
    env: &Environment,
    symtab: &SymbolTable,
    lib: &FunctionLibrary,
    path: &[PathElement],
    errors: &mut ValidationErrors,
) {
    if let Some(vars) = &env.variables {
        let vars_path = path_field(path, "variables");
        for (name, value) in vars {
            validate_fs(value, symtab, lib, &path_field(&vars_path, name), errors);
        }
    }
    if let Some(script) = &env.script {
        let script_path = path_field(path, "script");
        let actions_path = path_field(&script_path, "actions");
        if let Some(action) = &script.actions.on_enter {
            validate_action_fs(
                action,
                symtab,
                lib,
                &path_field(&actions_path, "onEnter"),
                errors,
            );
        }
        if let Some(action) = &script.actions.on_exit {
            validate_action_fs(
                action,
                symtab,
                lib,
                &path_field(&actions_path, "onExit"),
                errors,
            );
        }
        if let Some(files) = &script.embedded_files {
            let files_path = path_field(&script_path, "embeddedFiles");
            for (j, f) in files.iter().enumerate() {
                let f_path = path_index(&files_path, j);
                if let Some(data) = &f.data {
                    validate_fs(data, symtab, lib, &path_field(&f_path, "data"), errors);
                }
                if let Some(filename) = &f.filename {
                    validate_fs(
                        filename,
                        symtab,
                        lib,
                        &path_field(&f_path, "filename"),
                        errors,
                    );
                }
            }
        }
    }
}

fn validate_env_comprehensions(envs: &Option<Vec<Environment>>, errors: &mut ValidationErrors) {
    if let Some(envs) = envs {
        for env in envs {
            if let Some(script) = &env.script {
                let mut env_let_names: HashSet<String> = HashSet::new();
                if let Some(bindings) = &script.let_bindings {
                    for b in bindings {
                        if let Some(eq) = b.find('=') {
                            env_let_names.insert(b[..eq].trim().to_string());
                        }
                    }
                }
                if !env_let_names.is_empty() {
                    let path: Vec<PathElement> = vec![];
                    for action in [&script.actions.on_enter, &script.actions.on_exit]
                        .into_iter()
                        .flatten()
                    {
                        if let Err(e) = action.command.validate_comprehension_vars(&env_let_names) {
                            errors.add(&path, e.to_string());
                        }
                        if let Some(args) = &action.args {
                            for arg in args {
                                if let Err(e) = arg.validate_comprehension_vars(&env_let_names) {
                                    errors.add(&path, e.to_string());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn validate_let_bindings(
    bindings: &[String],
    path: &[PathElement],
    enclosing_names: &HashSet<String>,
    out_names: &mut HashSet<String>,
    symtab: &mut SymbolTable,
    lib: &FunctionLibrary,
    profile: &openjd_expr::ExprProfile,
    errors: &mut ValidationErrors,
) {
    if bindings.is_empty() {
        errors.add(path, "if provided, must not be empty.");
        return;
    }
    if bindings.len() > 50 {
        errors.add(path, "must not contain more than 50 bindings.");
    }
    let mut names = HashSet::new();
    for (i, binding) in bindings.iter().enumerate() {
        let b_path = path_index(path, i);
        let Some(eq_pos) = binding.find('=') else {
            errors.add(&b_path, format!("missing '=' in '{binding}'."));
            continue;
        };
        let name = binding[..eq_pos].trim();
        let expr = binding[eq_pos + 1..].trim();
        if name.is_empty() {
            errors.add(&b_path, "has empty name.");
            continue;
        }
        if expr.is_empty() {
            errors.add(
                &b_path,
                format!("binding '{name}' has no expression after '='."),
            );
            continue;
        }
        let first = name.chars().next().unwrap();
        if !first.is_ascii_lowercase() && first != '_' {
            errors.add(
                &b_path,
                format!("name '{name}' must start with lowercase letter or underscore."),
            );
        }
        if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
            errors.add(
                &b_path,
                format!("name '{name}' contains invalid characters."),
            );
        }
        if !names.insert(name.to_string()) {
            errors.add(&b_path, format!("duplicate name '{name}'."));
        }
        if enclosing_names.contains(name) {
            errors.add(&b_path, format!("'{name}' shadows enclosing scope."));
        }
        out_names.insert(name.to_string());

        // Evaluate the expression for type checking (Phase 1: static type check).
        // The prefix is included in error messages so caret positions align with
        // the full binding string, matching Python's behavior.
        let expr_start =
            eq_pos + 1 + binding[eq_pos + 1..].len() - binding[eq_pos + 1..].trim_start().len();
        let prefix = &binding[..expr_start];
        match ParsedExpression::with_profile(expr, profile) {
            Ok(parsed) => {
                // Check self-reference using the parsed AST's accessed symbols
                // rather than heuristic regex matching on the raw expression string.
                if parsed.accessed_symbols().contains(name) {
                    errors.add(&b_path, format!("'{name}' references itself."));
                }
                match parsed.with_library(lib).evaluate(&[symtab as &SymbolTable]) {
                    Ok(result) => {
                        // Set the binding in the symtab with its inferred value/type
                        // so subsequent bindings and format strings see the correct type.
                        let _ = symtab.set(name, result);
                    }
                    Err(e) => {
                        errors.add(
                            &b_path,
                            format!(
                                "Invalid expression in let binding '{name}': {}",
                                e.message_with_expr_prefix(prefix)
                            ),
                        );
                        // Still add as unresolved(ANY) so later bindings don't cascade errors
                        let _ = symtab.set(name, ExprValue::unresolved(ExprType::ANY));
                    }
                }
            }
            Err(e) => {
                errors.add(
                    &b_path,
                    format!(
                        "Invalid expression in let binding '{name}': {}",
                        e.message_with_expr_prefix(prefix)
                    ),
                );
                let _ = symtab.set(name, ExprValue::unresolved(ExprType::ANY));
            }
        }
    }
}