nmbrs-runtime 0.4.0

Workload execution runtime for nmbrs
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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Binding expression compiler: parses nosqlbench-style binding chains
//! into Polydat Kernel node wiring.
//!
//! Binding syntax: `FuncA(args); FuncB(args); FuncC(args)`
//!
//! This is a semicolon-delimited chain where the output of each
//! function feeds the input of the next, starting from the `cycle`
//! coordinate. So `Hash(); Mod(1000000)` becomes:
//!
//! ```text
//! __binding_h_0 := hash(cycle)
//! binding_name  := mod(__binding_h_0, 1000000)
//! ```

use std::collections::{HashMap, HashSet};

use crate::scope_kernel::ScopeKernel;

use nmbrs_workload::bindpoints;
use nmbrs_workload::model::ParsedOp;

/// A parsed function call in a binding chain.
#[derive(Debug, Clone)]
struct BindingFunc {
    name: String,
    args: Vec<String>,
}

/// Parse a binding expression into a chain of function calls.
///
/// `"Hash(); Mod(1000000)"` → `[BindingFunc{name:"Hash", args:[]}, BindingFunc{name:"Mod", args:["1000000"]}]`
fn parse_binding_chain(expr: &str) -> Vec<BindingFunc> {
    let mut funcs = Vec::new();

    for segment in expr.split(';') {
        let segment = segment.trim();
        if segment.is_empty() {
            continue;
        }

        // Find function name and args
        if let Some(paren_pos) = segment.find('(') {
            let name = segment[..paren_pos].trim().to_string();
            let args_str = &segment[paren_pos + 1..];
            let args_str = args_str.trim_end_matches(')').trim();

            let args: Vec<String> = if args_str.is_empty() {
                Vec::new()
            } else {
                // Split on commas, respecting nested parens
                split_args(args_str)
            };

            funcs.push(BindingFunc { name, args });
        } else {
            // No parens — treat as a nullary function
            funcs.push(BindingFunc {
                name: segment.trim().to_string(),
                args: Vec::new(),
            });
        }
    }

    funcs
}

/// Split comma-separated arguments, respecting nested parentheses and quotes.
fn split_args(s: &str) -> Vec<String> {
    let mut args = Vec::new();
    let mut current = String::new();
    let mut depth = 0;
    let mut in_quote = false;

    for c in s.chars() {
        match c {
            '\'' if !in_quote => {
                in_quote = true;
                current.push(c);
            }
            '\'' if in_quote => {
                in_quote = false;
                current.push(c);
            }
            '(' if !in_quote => {
                depth += 1;
                current.push(c);
            }
            ')' if !in_quote => {
                depth -= 1;
                current.push(c);
            }
            ',' if depth == 0 && !in_quote => {
                args.push(current.trim().to_string());
                current = String::new();
            }
            _ => current.push(c),
        }
    }
    if !current.trim().is_empty() {
        args.push(current.trim().to_string());
    }
    args
}

// build_chain_node and its helpers have been removed.
// Legacy semicolon-chain bindings are now translated to Polydat source
// and compiled through the unified Polydat compiler. See compile_bindings_with_opts.

/// Compile all bindings from a set of ParsedOps into a Polydat Kernel.
///
/// Collects all unique binding names and expressions, plus any
/// unreferenced bind points in op fields (auto-bound to hash+mod).
/// Wires them through the Polydat assembler as proper node chains.
/// Probe the compile level of a Polydat function by name.
///
/// Instantiates a dummy node and calls its intrinsic `compile_level()`.
/// This is the single source of truth — no external classification needed.
/// Probe the compile level of a Polydat function by name.
///
/// Instantiates a node with a representative constant and probes
/// its intrinsic compile level. This is the single source of truth.
/// Probe the compile level of a Polydat function by name.
///
/// Constructs a representative Polydat program containing the function
/// and inspects the resulting kernel's node for its compile level.
/// Uses the unified Polydat compiler — no separate dispatch table.
pub fn probe_compile_level(func_name: &str) -> polydat::ast::CompileLevel {
    let sig = match polydat::dsl::registry::lookup(func_name) {
        Some(s) => s,
        None => return polydat::ast::CompileLevel::Phase1,
    };

    // Build args from per-parameter example values declared in FuncSig.
    // Wire params use their example (typically "cycle"), const params
    // use representative values that pass the node's validation.
    let mut parts = Vec::new();
    let mut has_wire = false;
    for p in sig.params {
        parts.push(p.example.to_string());
        if p.slot_type.is_wire() {
            has_wire = true;
        }
    }
    // Ensure at least one wire input for the coordinates declaration.
    if !has_wire && parts.is_empty() {
        parts.push("cycle".to_string());
    }

    let source = format!("input cycle: u64\nout := {func_name}({})", parts.join(", "));

    // Probe compile level via catch_unwind — fallback is Phase1.
    // Does not replace the global panic hook (not thread-safe).
    // The compile level is a property of the interpreter's node graph,
    // so the probe reads it off the interpreter's program.
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        polydat::dsl::compile::compile_polydat_interpreter(&source)
    }));

    match result {
        Ok(Ok(kernel)) => kernel.program().last_node_compile_level(),
        _ => polydat::ast::CompileLevel::Phase1,
    }
}

pub fn compile_bindings(ops: &[ParsedOp]) -> Result<ScopeKernel, String> {
    compile_bindings_with_path(ops, None)
}

/// Scan op params for bind point references (recursively through JSON values).
fn collect_param_bindings(
    params: &HashMap<String, serde_json::Value>,
    exclude: &[String],
    required: &mut Vec<String>,
) {
    for (key, value) in params.iter() {
        // `gutter:` templates resolve at RUNTIME — during-forms on the
        // fiber wires, `final:` at phase end with a status-metric
        // fallback (`{recall}`, `{latency_p50}`) that has no wire
        // declaration anywhere. Unresolved names degrade to visible
        // literal text in the cell, so they must not be collected as
        // required wire references.
        if key == "gutter" {
            continue;
        }
        collect_json_bindings(value, exclude, required);
    }
}

/// Public wrapper for `collect_param_bindings`, used by `scope.rs`.
pub fn collect_param_bindings_into(
    params: &HashMap<String, serde_json::Value>,
    exclude: &[String],
    required: &mut Vec<String>,
) {
    collect_param_bindings(params, exclude, required);
}

fn collect_json_bindings(
    value: &serde_json::Value,
    exclude: &[String],
    required: &mut Vec<String>,
) {
    match value {
        serde_json::Value::String(s) => {
            for name in bindpoints::referenced_bindings(s) {
                if !required.contains(&name) && !exclude.contains(&name) {
                    required.push(name);
                }
            }
        }
        serde_json::Value::Object(map) => {
            for v in map.values() {
                collect_json_bindings(v, exclude, required);
            }
        }
        serde_json::Value::Array(arr) => {
            for v in arr {
                collect_json_bindings(v, exclude, required);
            }
        }
        _ => {}
    }
}

pub fn compile_bindings_with_path(
    ops: &[ParsedOp],
    source_dir: Option<&std::path::Path>,
) -> Result<ScopeKernel, String> {
    compile_bindings_with_opts(ops, source_dir, false)
}

/// Compile a Polydat Kernel from a pre-built `BindingScope`.
///
/// The scope has already been validated and carries structured
/// provenance. This function emits the scope to Polydat source, collects
/// required outputs, and compiles via the standard Polydat compiler.
///
/// `pragmas` carries the chain-walked effective pragma state for
/// the scope (typically obtained from
/// `ScopeTree::nodes[idx].pragmas`). Pragma directives matching
/// the effective state are prepended to the emitted source so
/// the Polydat compiler's existing AST-pragma extraction (SRD 15
/// §"Module-Level Pragmas") drives the assembler's strict-wire
/// flags. Pass `&PragmaSet::default()` to disable pragma effects
/// for legacy callers.
pub fn compile_from_scope(
    scope: &crate::scope::BindingScope,
    source_dir: Option<&std::path::Path>,
    polydat_lib_paths: Vec<std::path::PathBuf>,
    strict: bool,
    context: &str,
    cursor_limit: Option<u64>,
    pragmas: &polydat::dsl::pragmas::PragmaSet,
) -> Result<ScopeKernel, String> {
    let (source, options) = scope_source_and_options(
        scope,
        source_dir,
        polydat_lib_paths,
        strict,
        context,
        cursor_limit,
        pragmas,
    );
    compile_scope_kernel(&source, &options)
}

/// The Polydat source and compile options a scope compiles from.
fn scope_source_and_options(
    scope: &crate::scope::BindingScope,
    source_dir: Option<&std::path::Path>,
    polydat_lib_paths: Vec<std::path::PathBuf>,
    strict: bool,
    context: &str,
    cursor_limit: Option<u64>,
    pragmas: &polydat::dsl::pragmas::PragmaSet,
) -> (String, polydat::dsl::compile::CompileOptions) {
    let body = scope.emit();
    let required = scope.required_outputs();
    let source = prepend_effective_pragmas(pragmas, &body);
    let options = polydat::dsl::compile::CompileOptions {
        source_dir: source_dir.map(std::path::Path::to_path_buf),
        lib_paths: polydat_lib_paths,
        required_outputs: required,
        strict,
        context: context.to_string(),
        cursor_limit,
        ..Default::default()
    };
    (source, options)
}

/// Compile Polydat source into a root scope kernel — a node of nmbrs's
/// scope tree with no parent.
///
/// The interpreter compiles the program scope synthesis reads; the
/// running kernel is the fiber engine's image of the same source and
/// options when it can stand in for that program, and the interpreter
/// kernel otherwise ([`ScopeKernel::root`]). Children are bound under it
/// through the engine-neutral `Kernel` surface. Every root scope is
/// compiled here, in one place, and resolves resources through the pool
/// ([`crate::resource_pool::pool_resources`]) unless `options` names a
/// scope of its own; every kernel bound beneath it joins that scope.
pub fn compile_scope_kernel(
    source: &str,
    options: &polydat::dsl::compile::CompileOptions,
) -> Result<ScopeKernel, String> {
    let options = polydat::dsl::compile::CompileOptions {
        resources: Some(
            options
                .resources
                .clone()
                .unwrap_or_else(crate::resource_pool::pool_resources),
        ),
        ..options.clone()
    };
    let options = &options;
    let interpreter =
        polydat::dsl::compile::compile_polydat_interpreter_with_options(source, options, None)
            .map_err(|e| e.to_string())?;
    let image =
        crate::fiber_engine::source_image(interpreter.program(), source, options, &options.context);
    Ok(ScopeKernel::root(interpreter, image))
}

/// Compile Polydat source to the interpreter program scope synthesis
/// reads, with no running kernel: for a caller that binds the program
/// under a scope itself.
pub fn compile_scope_program(
    source: &str,
    options: &polydat::dsl::compile::CompileOptions,
) -> Result<std::sync::Arc<polydat::kernel::PolydatProgram>, String> {
    polydat::dsl::compile::compile_polydat_interpreter_with_options(source, options, None)
        .map(|k| k.program().clone())
        .map_err(|e| e.to_string())
}

/// Prepend pragma directives matching the chain's effective
/// state. The resulting Polydat source is functionally equivalent to
/// having the pragmas declared locally — the compiler's AST walk
/// picks them up the same way regardless of whether they came
/// from the original source or were synthesised here.
///
/// Provenance (which scope originally declared the pragma) is
/// flattened by this textualization. That's acceptable for the
/// runtime-effect path; diagnostic uses query the scope tree
/// directly via `ScopeTree::ancestors` for path labels.
pub(crate) fn prepend_effective_pragmas(
    pragmas: &polydat::dsl::pragmas::PragmaSet,
    body: &str,
) -> String {
    let mut out = String::new();
    if pragmas.strict_types() && pragmas.strict_values() {
        out.push_str("pragma strict\n");
    } else if pragmas.strict_types() {
        out.push_str("pragma strict_types\n");
    } else if pragmas.strict_values() {
        out.push_str("pragma strict_values\n");
    }
    if !out.is_empty() {
        out.push('\n');
    }
    out.push_str(body);
    out
}

/// The session clock's origin, in epoch milliseconds, declared on the
/// workload root (see [`build_workload_root_kernel`]).
pub const SESSION_START: &str = "session_start";

/// Build the workload-root [`ScopeKernel`] as a subscope of the
/// workload-params kernel.
///
/// The workload-root is "just another scope" per SRD-67
/// §"Composition with SRD-66": it goes through the same
/// source-matter construction every other scope uses
/// ([`ScopeKernel::build_under`]). The specialised content for the root is:
///
/// - Op-level bindings (rare at root; most workloads put
///   bindings at the workload or phase level).
/// - The workload's `bindings:` block, ingested as Inherited
///   matter.
/// - Workload params as `const` bindings — descendants pick
///   them up via the standard manifest auto-extern.
/// - DCE filter that retains every workload param plus
///   caller-supplied config refs (`cycles=` etc.).
///
/// Replaces the prior `compile_bindings_with_libs_excluding`
/// function — the "_excluding" suffix referred to a now-defunct
/// `exclude` parameter, and the function carried a legacy
/// semicolon-chain Map-bindings branch that no shipped workload
/// shape exercises (workload params are always present, so the
/// `needs_scope` gate always picked the modern path).
// reason: cohesive compile entry point — each argument is an
// independent compile input (parent scope, ops, lib paths, strictness,
// required-names, context label, cursor bound); bundling them into a
// struct would only move the same fields without adding clarity.
#[allow(clippy::too_many_arguments)]
pub fn build_workload_root_kernel(
    parent: &ScopeKernel,
    ops: &[ParsedOp],
    source_dir: Option<&std::path::Path>,
    polydat_lib_paths: Vec<std::path::PathBuf>,
    strict: bool,
    extra_required: &[String],
    context: &str,
    cursor_limit: Option<u64>,
    workload_params: &std::collections::HashMap<String, String>,
    workload_level_polydat: Option<&str>,
) -> Result<ScopeKernel, String> {
    // Build the workload-root scope. workload_params get
    // injected as `const` bindings so descendants resolve them
    // via the standard manifest auto-extern.
    let mut scope = crate::scope::build_scope(
        ops,
        &std::collections::HashMap::new(), // no iteration vars at outer level
        &[],                               // no outer manifest (this IS the outer scope)
        workload_params,
        &std::collections::HashMap::new(), // phases not needed here
        None,                              // no phase cycles
        &[],                               // no excluded names
        None,                              // workload-root has no parent program for AST mode
    )?;

    // SRD-13f §"Wire-reference classification" — the workload's
    // `bindings:` block lives on the workload-root scope as
    // local matter. Ingest before validate/emit so emit()
    // produces a single coherent source.
    if let Some(extra) = workload_level_polydat
        && !extra.trim().is_empty()
    {
        scope.ingest_polydat_source(extra, crate::scope::BindingOrigin::Inherited);
    }
    // The session clock's origin: a const over a volatile reading,
    // captured once when the workload root initializes. It has no
    // inputs, so scope synthesis inlines that one captured value into
    // every descendant that names it, and session-elapsed time —
    // `current_epoch_millis() - session_start` — reads one origin in
    // every scope, fiber, and engine. The capture reads the clock, and a
    // const capture is its own acknowledgment, so strict mode accepts it.
    // A workload param or binding of the same name is the author's own
    // origin and wins.
    let session_clock = !workload_params.contains_key(SESSION_START)
        && !scope.defined_names().contains(SESSION_START);
    if session_clock {
        scope.ingest_polydat_source(
            &format!("const {SESSION_START} := current_epoch_millis()\n"),
            crate::scope::BindingOrigin::Inherited,
        );
    }
    scope.validate().map_err(|e| format!("{context}: {e}"))?;

    // DCE-keepalive list: caller's config refs plus every
    // workload param. Param names are sorted so the resulting
    // output order is deterministic across processes (HashMap
    // iteration is randomised per-process, and `compile_filtered`
    // calls `add_output` in iteration order — non-determinism
    // here would surface as a non-deterministic
    // `canonical_hash` and break checkpoint resume-skip
    // matching).
    let mut scope_required = scope.required_outputs();
    for name in extra_required {
        if !scope_required.contains(name) {
            scope_required.push(name.clone());
        }
    }
    if session_clock && !scope_required.iter().any(|n| n == SESSION_START) {
        scope_required.push(SESSION_START.to_string());
    }
    let mut param_names: Vec<&String> = workload_params.keys().collect();
    param_names.sort();
    for name in param_names {
        if !scope_required.contains(name) {
            scope_required.push(name.clone());
        }
    }

    // Standard scope construction: source matter built under the
    // parent (ScopeKernel::build_under), identical to every other
    // scope's build pathway (SRD-67 §"The construction protocol").
    let mut source = scope.emit();
    // If the workload's authored matter doesn't declare its
    // own `input ...: u64` line, default the workload-root
    // kernel's coordinate input to `cycle`. The wire name is
    // a CONVENTION the runner-driver contract relies on
    // (every dispatch path sets the cycle ordinal on slot 0
    // via set_inputs); workload authors who need a different
    // shape can declare their own inputs line explicitly. The
    // default lets inline workloads (`nmbrs run op="c={cycle}"`)
    // resolve `{cycle}` against the workload-root kernel
    // without forcing every author to write `input cycle: u64`.
    if !source.lines().any(|l| l.trim_start().starts_with("input ")) {
        source = format!("input cycle: u64\n{source}");
    }
    let opts = polydat::kernel::subcontext::CompileOptions {
        workload_dir: source_dir.map(|p| p.to_path_buf()),
        polydat_lib_paths,
        strict,
        required_outputs: scope_required,
        context_label: Some(context.to_string()),
        cursor_limit,
        ..Default::default()
    };
    // Mark workload-param names as inherited so their input slots
    // don't show up in `compute_own_coordinates` (which filters for
    // `IterationExtern`-kind inputs that aren't marked inherited).
    // Without this, switching the workload-param cascade from
    // `const NAME := <literal>` to `extern NAME: T` causes every
    // workload param to show up as a scope-coordinate on the
    // workload-root kernel — and downstream code (phase identity
    // construction, label formatting, scene-tree population) treats
    // those coordinates as iter-vars belonging to this scope. The
    // resulting phase-identity drift between pre-map registration
    // and runtime phase_failed silently breaks checkpoint Failed
    // recording. Marking them inherited keeps the cascade-pass-
    // through semantics: descendants still extern these names from
    // the workload-root's manifest, but the workload-root itself
    // treats them as inherited rather than as its own iter-vars.
    let mut inherited_param_names: Vec<String> = workload_params.keys().cloned().collect();
    inherited_param_names.sort();
    ScopeKernel::build_under(
        parent.kernel(),
        crate::scope_kernel::SourceMatter::source(context, source, opts)
            .inherited(inherited_param_names),
    )
}

/// Compile all bindings from a set of ParsedOps into a Polydat Kernel.
///
/// When `strict` is true, the Polydat compiler enforces:
/// - Explicit `input ...: u64` declaration (no inference)
/// - All module arguments must be named (no positional)
/// - All module inputs must be provided by caller (no fallthrough)
pub fn compile_bindings_with_opts(
    ops: &[ParsedOp],
    source_dir: Option<&std::path::Path>,
    strict: bool,
) -> Result<ScopeKernel, String> {
    use nmbrs_workload::model::BindingsDef;

    // Check if any op uses Polydat source mode
    let polydat_source = ops.iter().find_map(|op| {
        if let BindingsDef::PolydatSource(src) = &op.bindings {
            if !src.trim().is_empty() {
                Some(src.clone())
            } else {
                None
            }
        } else {
            None
        }
    });

    if let Some(source) = polydat_source {
        // Native Polydat grammar mode: collect referenced bind points from
        // op templates for dead code elimination. Only the bindings
        // actually used by ops are compiled into the kernel.
        let mut required: Vec<String> = Vec::new();
        for op in ops {
            for value in op.op.values() {
                if let Some(s) = value.as_str() {
                    for name in bindpoints::referenced_bindings(s) {
                        if !required.contains(&name) {
                            required.push(name);
                        }
                    }
                }
            }
        }
        let options = polydat::dsl::compile::CompileOptions {
            source_dir: source_dir.map(std::path::Path::to_path_buf),
            required_outputs: required,
            strict,
            ..Default::default()
        };
        return compile_scope_kernel(&source, &options);
    }

    // Legacy mode: translate semicolon-chain bindings into Polydat source
    // and compile through the unified Polydat compiler. No separate dispatch
    // table — every node function available in Polydat grammar is automatically
    // available in legacy chain syntax.
    let mut all_bindings: HashMap<String, String> = HashMap::new();
    for op in ops {
        if let BindingsDef::Map(map) = &op.bindings {
            for (name, expr) in map {
                all_bindings
                    .entry(name.clone())
                    .or_insert_with(|| expr.clone());
            }
        }
    }

    // Collect required outputs from op templates
    let mut required: Vec<String> = Vec::new();
    for op in ops {
        for value in op.op.values() {
            if let Some(s) = value.as_str() {
                for name in bindpoints::referenced_bindings(s) {
                    if !required.contains(&name) {
                        required.push(name);
                    }
                }
            }
        }
    }

    // Translate each legacy chain into Polydat source lines
    let mut polydat_lines: Vec<String> = Vec::new();
    polydat_lines.push("input cycle: u64".into());

    for (binding_name, expr) in &all_bindings {
        let chain = parse_binding_chain(expr);
        if chain.is_empty() {
            return Err(format!("empty binding expression for '{binding_name}'"));
        }

        // Convert each chain step into a Polydat binding.
        // Chain: FuncA(args); FuncB(args) → sequential wiring from cycle.
        let mut prev_wire = "cycle".to_string();

        for (i, func) in chain.iter().enumerate() {
            let is_last = i == chain.len() - 1;
            let target = if is_last {
                binding_name.clone()
            } else {
                format!("__chain_{binding_name}_{i}")
            };

            // Translate legacy function names to Polydat equivalents
            let (func_name, extra_args) = translate_legacy_func(&func.name, &func.args);
            let mut call_args = vec![prev_wire.clone()];
            for arg in &func.args {
                call_args.push(strip_java_long_suffix(arg.trim()).to_string());
            }
            call_args.extend(extra_args);

            polydat_lines.push(format!(
                "{target} := {func_name}({args})",
                args = call_args.join(", ")
            ));

            prev_wire = target;
        }
    }

    // Validate: all bind point references must have a binding or
    // be a declared coordinate (the Polydat compiler auto-exposes
    // coordinates as passthrough outputs, so they resolve without
    // a user-declared binding).
    let coord_names: HashSet<String> = ["cycle".to_string()].into_iter().collect();
    let mut missing: Vec<String> = Vec::new();
    for name in &required {
        if !all_bindings.contains_key(name) && !coord_names.contains(name) {
            missing.push(name.clone());
        }
    }
    if !missing.is_empty() {
        return Err(format!(
            "undeclared bind point references: {}. Add these to your bindings section.",
            missing.join(", ")
        ));
    }

    let polydat_source = polydat_lines.join("\n");
    let options = polydat::dsl::compile::CompileOptions {
        source_dir: source_dir.map(std::path::Path::to_path_buf),
        required_outputs: required,
        strict,
        ..Default::default()
    };
    compile_scope_kernel(&polydat_source, &options)
}

// ---------------------------------------------------------------------------
// Legacy function name translation (virtdata → GK)
//
// This is a compatibility overlay that maps Java nosqlbench function
// names to their nmbrs Polydat equivalents. It lives ONLY in the binding
// chain compiler — the Polydat registry and node implementations are not
// polluted with legacy names.
// ---------------------------------------------------------------------------

/// Translate a legacy Java nosqlbench function name to its Polydat equivalent.
///
/// Returns `(polydat_func_name, extra_args)` where extra_args are additional
/// arguments to append (e.g., for ToString → format_u64 which needs a radix).
fn translate_legacy_func(name: &str, args: &[String]) -> (String, Vec<String>) {
    match name.to_lowercase().as_str() {
        // Direct name mappings (same semantics, different naming convention)
        "hash" => ("hash".into(), vec![]),
        "identity" => ("identity".into(), vec![]),
        "add" => ("add".into(), vec![]),
        "mul" => ("mul".into(), vec![]),
        "div" => ("div".into(), vec![]),
        "mod" => ("mod".into(), vec![]),
        "clamp" => ("clamp".into(), vec![]),

        // String conversions
        "tostring" | "to_string" => ("format_u64".into(), vec!["10".into()]),
        "tohexstring" => ("format_u64".into(), vec!["16".into()]),
        "tooctalstring" => ("format_u64".into(), vec!["8".into()]),
        "tobinarystring" => ("format_u64".into(), vec!["2".into()]),

        // Distributions (Java names → Polydat equivalents)
        // Uniform(min, max) → hash_range(input, max-min) + add(min)
        // This is approximate — Java Uniform samples from a distribution;
        // Polydat hash_range does modular hash. Close enough for key distribution.
        "uniform" => {
            if args.len() >= 2 {
                // Uniform(min, max) → mod(hash(input), range) then add(min)
                // We approximate with hash_range which takes just max
                ("hash_range".into(), vec![])
            } else {
                ("hash_range".into(), vec![])
            }
        }

        // Zipf, Normal, etc. — map to icd_ variants
        "normal" | "gaussian" => ("icd_normal".into(), vec![]),
        "zipf" => ("dist_zipf".into(), vec![]),

        // Hash-based
        "hashrange" | "hash_range" => ("hash_range".into(), vec![]),
        "hashinterval" | "hash_interval" => ("hash_interval".into(), vec![]),

        // Number formatting
        "format" | "printf" => ("printf".into(), vec![]),
        "numbernamesto_string" | "numbernames" => ("number_to_words".into(), vec![]),

        // Shuffle / permutation
        "shuffle" => ("shuffle".into(), vec![]),

        // Long suffix stripping (Java allows 1000000000L)
        _ => {
            // Default: lowercase the name and hope the Polydat registry has it
            (name.to_lowercase(), vec![])
        }
    }
}

/// Strip Java long literal suffix (e.g., "1000000000L" → "1000000000")
fn strip_java_long_suffix(arg: &str) -> &str {
    arg.strip_suffix('L')
        .or_else(|| arg.strip_suffix('l'))
        .unwrap_or(arg)
}

/// SRD-13f Push D: translate a Map-form bindings block (legacy
/// semicolon-chain syntax, e.g. `{user_id: "Hash(); Mod(1000000)"}`)
/// into Polydat source lines. Returns one or more `name := func(args)`
/// lines per entry — multi-step chains expand to intermediate
/// `__chain_<name>_<i> := ...` wires.
///
/// Does NOT prepend `input cycle: u64`; that's owned by the
/// enclosing scope's emit. Intended for routing workload-level
/// `bindings:` directly to the workload-root kernel without
/// going through the parser merge (which retired in Push D).
pub fn legacy_chain_map_to_polydat_lines(
    map: &std::collections::HashMap<String, String>,
) -> Result<String, String> {
    let mut polydat_lines: Vec<String> = Vec::new();
    for (binding_name, expr) in map {
        let chain = parse_binding_chain(expr);
        if chain.is_empty() {
            return Err(format!("empty binding expression for '{binding_name}'"));
        }
        let mut prev_wire = "cycle".to_string();
        for (i, func) in chain.iter().enumerate() {
            let is_last = i == chain.len() - 1;
            let target = if is_last {
                binding_name.clone()
            } else {
                format!("__chain_{binding_name}_{i}")
            };
            let (func_name, extra_args) = translate_legacy_func(&func.name, &func.args);
            let mut call_args = vec![prev_wire.clone()];
            for arg in &func.args {
                call_args.push(strip_java_long_suffix(arg.trim()).to_string());
            }
            call_args.extend(extra_args);
            polydat_lines.push(format!(
                "{target} := {func_name}({args})",
                args = call_args.join(", ")
            ));
            prev_wire = target;
        }
    }
    Ok(polydat_lines.join("\n"))
}

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

    use polydat::dsl::pragmas::{Pragma, PragmaSet};

    #[test]
    fn prepend_pragmas_strict_alias() {
        let pragmas = PragmaSet {
            entries: vec![Pragma {
                name: "strict".into(),
                args: vec![],
                line: 1,
            }],
        };
        let body = "id := cycle\n";
        let out = prepend_effective_pragmas(&pragmas, body);
        // `strict` activates both — emit single combined directive.
        assert!(out.starts_with("pragma strict\n"));
        assert!(out.contains("id := cycle"));
    }

    #[test]
    fn prepend_pragmas_individual_modes() {
        let pragmas = PragmaSet {
            entries: vec![Pragma {
                name: "strict_values".into(),
                args: vec![],
                line: 1,
            }],
        };
        let out = prepend_effective_pragmas(&pragmas, "x := cycle");
        assert!(out.starts_with("pragma strict_values\n"));
        assert!(!out.contains("strict_types"));
    }

    #[test]
    fn prepend_pragmas_no_op_when_empty() {
        let pragmas = PragmaSet::default();
        let out = prepend_effective_pragmas(&pragmas, "x := cycle");
        assert_eq!(out, "x := cycle");
    }

    #[test]
    fn prepend_pragmas_walks_parent_chain() {
        // Parent declares strict_values. Child declares nothing.
        // The child's nested set still produces the prepended
        // directive — that's the load-bearing behavior for SRD 18b
        // cross-scope propagation.
        let parent = PragmaSet {
            entries: vec![Pragma {
                name: "strict_values".into(),
                args: vec![],
                line: 1,
            }],
        };
        let child = parent.nested(&[]);
        let out = prepend_effective_pragmas(&child, "x := cycle");
        assert!(
            out.starts_with("pragma strict_values\n"),
            "expected pragma to flow from parent chain, got:\n{out}"
        );
    }

    #[test]
    fn parse_simple_chain() {
        let chain = parse_binding_chain("Hash(); Mod(1000000)");
        assert_eq!(chain.len(), 2);
        assert_eq!(chain[0].name, "Hash");
        assert!(chain[0].args.is_empty());
        assert_eq!(chain[1].name, "Mod");
        assert_eq!(chain[1].args, vec!["1000000"]);
    }

    #[test]
    fn parse_identity() {
        let chain = parse_binding_chain("Identity()");
        assert_eq!(chain.len(), 1);
        assert_eq!(chain[0].name, "Identity");
    }

    #[test]
    fn parse_with_string_arg() {
        let chain = parse_binding_chain("Template('user-{}', ToString())");
        assert_eq!(chain.len(), 1);
        assert_eq!(chain[0].name, "Template");
        assert_eq!(chain[0].args.len(), 2);
    }

    #[test]
    fn parse_long_chain() {
        let chain = parse_binding_chain("Add(10); Hash(); Mod(100); ToString()");
        assert_eq!(chain.len(), 4);
        assert_eq!(chain[0].name, "Add");
        assert_eq!(chain[1].name, "Hash");
        assert_eq!(chain[2].name, "Mod");
        assert_eq!(chain[3].name, "ToString");
    }

    #[test]
    fn parse_with_long_suffix() {
        let chain = parse_binding_chain("Mod(1000000000L)");
        assert_eq!(chain[0].args, vec!["1000000000L"]);
        // The L suffix is preserved in the chain parse.
        // The Polydat compiler handles it during node construction.
    }

    #[test]
    fn compile_identity_binding() {
        let ops = vec![{
            let mut op = ParsedOp::simple("test", "{myval}");
            op.bindings.insert("myval".into(), "Identity()".into());
            op
        }];
        let mut kernel = compile_bindings(&ops).unwrap();
        kernel.set_inputs(&[42]);
        assert_eq!(kernel.pull("myval").as_u64(), 42);
    }

    #[test]
    fn compile_hash_mod_binding() {
        let ops = vec![{
            let mut op = ParsedOp::simple("test", "{id}");
            op.bindings
                .insert("id".into(), "Hash(); Mod(1000000)".into());
            op
        }];
        let mut kernel = compile_bindings(&ops).unwrap();
        kernel.set_inputs(&[42]);
        let val = kernel.pull("id").as_u64();
        assert!(val < 1_000_000, "got {val}");
    }

    #[test]
    fn compile_hash_mod_deterministic() {
        let ops = vec![{
            let mut op = ParsedOp::simple("test", "{id}");
            op.bindings.insert("id".into(), "Hash(); Mod(100)".into());
            op
        }];
        let mut kernel = compile_bindings(&ops).unwrap();
        kernel.set_inputs(&[42]);
        let v1 = kernel.pull("id").as_u64();
        kernel.set_inputs(&[42]);
        let v2 = kernel.pull("id").as_u64();
        assert_eq!(v1, v2);
    }

    #[test]
    fn compile_multiple_bindings() {
        let ops = vec![{
            let mut op = ParsedOp::simple("test", "{a} {b}");
            op.bindings.insert("a".into(), "Identity()".into());
            op.bindings.insert("b".into(), "Hash(); Mod(100)".into());
            op
        }];
        let mut kernel = compile_bindings(&ops).unwrap();
        kernel.set_inputs(&[5]);
        assert_eq!(kernel.pull("a").as_u64(), 5);
        assert!(kernel.pull("b").as_u64() < 100);
    }

    #[test]
    fn compile_rejects_undeclared_bind_points() {
        // Op references {mystery} but no binding declared → error
        let ops = vec![ParsedOp::simple("test", "val={mystery}")];
        let result = compile_bindings(&ops);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("undeclared bind point"));
    }

    #[test]
    fn compile_add_chain() {
        let ops = vec![{
            let mut op = ParsedOp::simple("test", "{val}");
            op.bindings
                .insert("val".into(), "Add(100); Mod(1000)".into());
            op
        }];
        let mut kernel = compile_bindings(&ops).unwrap();
        kernel.set_inputs(&[5]);
        // 5 + 100 = 105, 105 % 1000 = 105
        assert_eq!(kernel.pull("val").as_u64(), 105);
    }

    #[test]
    fn compile_provides_cycle_output() {
        let ops = vec![ParsedOp::simple("test", "cycle={cycle}")];
        let mut kernel = compile_bindings(&ops).unwrap();
        kernel.set_inputs(&[99]);
        assert_eq!(kernel.pull("cycle").as_u64(), 99);
    }

    #[test]
    fn legacy_tostring_translates() {
        let (name, _) = translate_legacy_func("ToString", &[]);
        assert_eq!(name, "format_u64");
    }

    #[test]
    fn legacy_uniform_translates() {
        let (name, _) = translate_legacy_func("Uniform", &["0".into(), "1000".into()]);
        assert_eq!(name, "hash_range");
    }
}