earthsci-ast 0.6.0

Rust implementation of EarthSciML Abstract Syntax Tree Format
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
//! WASM bindings for earthsci-ast
//!
//! This module provides WebAssembly bindings for use with TypeScript/JavaScript.
//!
//! The whole module is gated on `feature = "wasm"` at its `mod` declaration in
//! `lib.rs`, so items here carry no per-item `wasm` gate; the ODE-solver
//! exports are additionally gated on `feature = "solve"` alone.

use crate::{
    EsmFile, graph::component_graph as rust_component_graph, load_string as rust_load_string,
    performance::CompactExpr, stoichiometric_matrix, substitute_in_model,
    substitute_in_reaction_system, to_json as rust_to_json, validate as rust_validate,
};
use wasm_bindgen::prelude::*;

/// The string `JsValue` every fallible export throws: `"{context}: {error}"`.
/// One constructor so the per-site `map_err(|e| JsValue::from_str(...))`
/// closures cannot drift in shape.
fn js_err<E: std::fmt::Display>(context: &str) -> impl Fn(E) -> JsValue + '_ {
    move |e| JsValue::from_str(&format!("{context}: {e}"))
}

/// Serialize any `Serialize` value to a plain JS object (never an ES `Map`),
/// so JS callers get uniform dot-access across every export. All exports go
/// through this one helper — previously some returned `Map`s and some plain
/// objects depending on which serializer they used.
fn to_js<T: serde::Serialize>(value: &T) -> Result<JsValue, JsValue> {
    let serializer = serde_wasm_bindgen::Serializer::new().serialize_maps_as_objects(true);
    value
        .serialize(&serializer)
        .map_err(js_err("Serialization error"))
}

/// Render every model equation and reaction rate of a loaded file with one of
/// the real expression pretty-printers from [`crate::display`] (the same ones
/// the CLI `pretty` command uses).
fn render_expressions(esm_file: &EsmFile, render: fn(&crate::Expr) -> String) -> String {
    let mut out = String::new();
    if let Some(models) = &esm_file.models {
        let mut ids: Vec<&String> = models.keys().collect();
        ids.sort();
        for model_id in ids {
            out.push_str(&format!("Model: {model_id}\n"));
            for (i, eq) in models[model_id].equations.iter().enumerate() {
                out.push_str(&format!(
                    "  Eq {}: {} = {}\n",
                    i + 1,
                    render(&eq.lhs),
                    render(&eq.rhs)
                ));
            }
        }
    }
    if let Some(reaction_systems) = &esm_file.reaction_systems {
        let mut ids: Vec<&String> = reaction_systems.keys().collect();
        ids.sort();
        for rs_id in ids {
            out.push_str(&format!("Reaction System: {rs_id}\n"));
            for (i, reaction) in reaction_systems[rs_id].reactions.iter().enumerate() {
                out.push_str(&format!(
                    "  Reaction {}: rate = {}\n",
                    i + 1,
                    render(&reaction.rate)
                ));
            }
        }
    }
    out
}

/// Load an ESM file from JSON string (WASM version)
#[wasm_bindgen]
pub fn load_string(json_str: &str) -> Result<JsValue, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Load error"))?;
    to_js(&esm_file)
}

/// Serialize an ESM file to a JSON string (WASM version)
#[wasm_bindgen]
pub fn to_json(esm_file_js: &JsValue) -> Result<String, JsValue> {
    let esm_file: EsmFile = serde_wasm_bindgen::from_value(esm_file_js.clone())
        .map_err(js_err("Deserialization error"))?;

    rust_to_json(&esm_file).map_err(js_err("Save error"))
}

/// Validate an ESM file (WASM version)
#[wasm_bindgen]
pub fn validate(json_str: &str) -> Result<JsValue, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;

    let result = rust_validate(&esm_file);
    to_js(&result)
}

/// Pretty-print every equation and reaction rate with the Unicode expression
/// printer ([`crate::display::to_unicode`]) — the same renderer the CLI's
/// `pretty` command uses. (Earlier versions returned a metadata summary
/// instead of rendered math.)
#[wasm_bindgen]
pub fn to_unicode(json_str: &str) -> Result<String, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;
    Ok(render_expressions(&esm_file, crate::display::to_unicode))
}

/// Pretty-print every equation and reaction rate with the LaTeX expression
/// printer ([`crate::display::to_latex`]).
#[wasm_bindgen]
pub fn to_latex(json_str: &str) -> Result<String, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;
    Ok(render_expressions(&esm_file, crate::display::to_latex))
}

/// Pretty-print every equation and reaction rate with the ASCII expression
/// printer ([`crate::display::to_ascii`]) — pure-ASCII output, unlike the
/// Unicode renderer.
#[wasm_bindgen]
pub fn to_ascii(json_str: &str) -> Result<String, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;
    Ok(render_expressions(&esm_file, crate::display::to_ascii))
}

/// Substitute expressions in ESM file (WASM version)
#[wasm_bindgen]
pub fn substitute(json_str: &str, bindings_str: &str) -> Result<String, JsValue> {
    use crate::Expr;

    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;

    // Parse bindings as JSON object
    let bindings: serde_json::Value =
        serde_json::from_str(bindings_str).map_err(js_err("Bindings parse error"))?;

    // Convert bindings to Expr objects
    let mut expr_bindings = std::collections::HashMap::new();
    if let serde_json::Value::Object(obj) = bindings {
        for (key, value) in obj {
            let expr = match value {
                serde_json::Value::Number(n) => {
                    if let Some(f) = n.as_f64() {
                        Expr::Number(f)
                    } else {
                        return Err(JsValue::from_str(&format!(
                            "Invalid number in bindings: {n}"
                        )));
                    }
                }
                serde_json::Value::String(s) => {
                    // Try to parse as number first, otherwise treat as variable
                    if let Ok(f) = s.parse::<f64>() {
                        Expr::Number(f)
                    } else {
                        Expr::Variable(s)
                    }
                }
                _ => {
                    return Err(JsValue::from_str(&format!(
                        "Unsupported binding type for key '{key}': {value:?}"
                    )));
                }
            };
            expr_bindings.insert(key, expr);
        }
    }

    let mut result_file = esm_file.clone();

    // Apply substitutions to all models
    if let Some(ref mut models) = result_file.models {
        for model in models.values_mut() {
            *model = substitute_in_model(model, &expr_bindings);
        }
    }

    // Apply substitutions to reaction systems if present
    if let Some(ref mut reactions) = result_file.reaction_systems {
        for reaction_system in reactions.values_mut() {
            *reaction_system = substitute_in_reaction_system(reaction_system, &expr_bindings);
        }
    }

    // Convert back to JSON string
    rust_to_json(&result_file).map_err(js_err("Save error"))
}

/// Create a compact expression for fast evaluation (WASM version)
#[wasm_bindgen]
pub fn create_compact_expression(expr_str: &str) -> Result<JsValue, JsValue> {
    // Parse expression from JSON string
    let expr: crate::Expr = serde_json::from_str(expr_str).map_err(js_err("Parse error"))?;

    let compact = CompactExpr::from_expr(&expr);
    to_js(&compact)
}

/// Compute stoichiometric matrix (WASM version)
#[wasm_bindgen]
pub fn compute_stoichiometric_matrix(reaction_system_str: &str) -> Result<JsValue, JsValue> {
    let reaction_system: crate::ReactionSystem =
        serde_json::from_str(reaction_system_str).map_err(js_err("Parse error"))?;

    let matrix = stoichiometric_matrix(&reaction_system);
    to_js(&matrix)
}

/// Introspect the **flattened** simulation inputs of an `.esm` file (gt-5ws).
///
/// Runs the same `flatten` pass [`solve`] uses, then reports the exact
/// parameter and state names it will accept — already namespaced — together
/// with their defaults and units, plus the system's independent variables. Use
/// this to build a Run UI without guessing the flattened names: the keys
/// returned here are exactly the keys to pass back in `params` / `ic`.
///
/// Returns `{ parameters: Var[], states: Var[], independentVariables: string[] }`
/// where `Var = { name: string, default: number | null, units: string | null }`.
/// A system whose `independentVariables` is not `["t"]` still has an
/// undiscretized spatial operator; discretized (array-op) PDEs report `["t"]`
/// here and run in the browser like any other file (EarthSciAST-akz).
#[wasm_bindgen]
pub fn simulate_inputs(json_str: &str) -> Result<JsValue, JsValue> {
    use crate::types::ModelVariable;
    use indexmap::IndexMap;

    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;
    let flat = crate::flatten(&esm_file).map_err(js_err("Flatten error"))?;

    let to_vars = |vars: &IndexMap<String, ModelVariable>| -> Vec<serde_json::Value> {
        vars.iter()
            .map(|(name, mv)| {
                serde_json::json!({ "name": name, "default": mv.default, "units": mv.units })
            })
            .collect()
    };

    // Each state's `kind`, so a host can tell a settable initial condition from
    // one the solver is going to overwrite. Without it every Run UI re-derives
    // the distinction by walking the raw `.esm` itself — the same rule written
    // twice, in a language that cannot see what flattening already resolved.
    let algebraic: std::collections::HashSet<String> =
        crate::simulate::algebraic_state_names(&flat)
            .into_iter()
            .collect();
    let mut states = to_vars(&flat.state_variables);
    for state in &mut states {
        let kind = match state.get("name").and_then(|n| n.as_str()) {
            Some(name) if algebraic.contains(name) => "algebraic",
            _ => "differential",
        };
        state["kind"] = serde_json::Value::from(kind);
    }

    let out = serde_json::json!({
        "parameters": to_vars(&flat.parameters),
        "states": states,
        "independentVariables": flat.independent_variables,
    });

    to_js(&out)
}

/// Run a simulation in the browser (WASM version, gt-5ws / spike S1).
///
/// Flattens and solves the `.esm` file through diffsol's Faer backend, entirely
/// client-side. Pure-ODE / 0-D box models and — since the `simulate_array` wasm
/// gate was lifted (EarthSciAST-akz) — array-op and discretized-PDE
/// files both run here through the same dispatch the native backend uses.
/// **Spherical/geodesic geometry** (conservative regridding) runs here too, as of
/// the move to the pure-Rust `s2rst` kernel: it links into this
/// `wasm32-unknown-unknown` build like any other dependency, so the clip runs
/// in-module with no host setup. (Previously these leaf ops hit a wasm stub and
/// returned a runtime `GeometryError`, because the C++ s2geometry kernel could
/// not be linked and had to be reached through a separate Emscripten module.)
///
/// Arguments:
/// - `json_str`: the `.esm` file as a JSON string.
/// - `t0`, `t_end`: the integration interval.
/// - `params_str`: JSON object mapping parameter name → value (`{}` for none).
/// - `ic_str`: JSON object mapping state name → initial value (`{}` to use the
///   model's `default`s).
/// - `opts_str`: JSON object, all fields optional —
///   `{ "alg": "bdf"|"sdirk"|"erk", "abstol": f64, "reltol": f64,
///      "maxiters": u32, "outputPoints": u32 }`. `maxiters` caps accepted
///   integrator steps and leaves the run uncapped when omitted. `outputPoints` samples the
///   solution at that many evenly spaced times in `[t0, t_end]` (nice for
///   plotting); omit it to get the solver's natural step grid. The names are
///   the canonical SciML ones (`API_SPEC.md` §4); the pre-harmonization
///   `solver` / `maxSteps` spellings are still accepted as aliases so an
///   existing host keeps working.
/// - `progress`: optional observer, called as `progress(fraction, t, step)`
///   once before the first step and then after **every** accepted step.
///   `fraction` is the covered share of `[t0, t_end]`, clamped to `[0, 1]`.
///   Return **`false`** to cancel the run. A cancel is NOT an error: the call
///   resolves with the trajectory computed so far and `metadata.retcode ===
///   "Terminated"`. Any other return value — including `undefined`, which is
///   what a callback with no `return` gives — continues. A callback that throws
///   is treated as a cancel.
///
///   It is called on every step and is **not throttled here**: the integrator
///   has no portable clock (`Instant::now()` panics on `wasm32-unknown-unknown`),
///   so a host that wants to rate-limit should do it in JS, where
///   `performance.now()` works. Keep it cheap — a 0-D model can accept
///   thousands of steps in a fraction of a second.
///
/// Returns a JS object `{ time: number[], state: number[][],
/// stateVariableNames: string[], retcode: string, metadata: {...} }` where
/// `state[i][k]` is variable `stateVariableNames[i]` at `time[k]`, and
/// `retcode` is the SciML return code — `"Success"` means the run reached
/// `t_end`, anything else means it stopped early and the trajectory ends there.
///
/// Named `solve`, not `simulate`: `simulate` is deleted in every binding
/// (`esm-libraries-spec.md` §2.5.1). This export builds the EsmProblem and solves
/// it in one call because a `wasm_bindgen` boundary cannot hand a host a
/// `EsmProblem` handle without a lifetime story JS has no way to honour.
#[cfg(feature = "solve")]
#[wasm_bindgen]
pub fn solve(
    json_str: &str,
    t0: f64,
    t_end: f64,
    params_str: &str,
    ic_str: &str,
    opts_str: &str,
    progress: Option<js_sys::Function>,
) -> Result<JsValue, JsValue> {
    use crate::problem::{ProblemOptions, esm_problem, solve as rust_solve};

    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;

    let opts = parse_solve_options(opts_str, t0, t_end, progress)?;

    // Build once, then solve — the same two steps every native caller takes,
    // collapsed here for a host that runs a document ONCE. A host that runs the
    // same document repeatedly should hold a [`Problem`] instead and pay for the
    // build once.
    let prob = esm_problem(
        &esm_file,
        (t0, t_end),
        ProblemOptions {
            p: parse_binding_map(params_str, "Params")?,
            u0: parse_binding_map(ic_str, "Initial-conditions")?,
            rhs: crate::problem::Rhs::Always,
            ..Default::default()
        },
    )
    .map_err(js_err("EsmProblem build error"))?;
    let sol = rust_solve(&prob, &opts).map_err(js_err("Solve error"))?;

    to_js(&solution_json(&sol))
}

/// A JSON `{name: number}` map, empty when the string is.
///
/// Shared by [`solve`], [`observed_fields`] and [`Problem`] so the three cannot
/// disagree about what an empty binding map means.
fn parse_binding_map(
    s: &str,
    what: &str,
) -> Result<std::collections::HashMap<String, f64>, JsValue> {
    let s = s.trim();
    if s.is_empty() {
        return Ok(std::collections::HashMap::new());
    }
    serde_json::from_str(s).map_err(js_err(&format!("{what} parse error")))
}

/// [`solve`]'s options object, including the progress observer.
///
/// Factored out so the free function and [`Problem::solve`] read the SAME
/// spelling of `alg` / `solver`, the same `outputPoints`, and the same cancel
/// convention. Two parsers for one options object is two behaviours waiting to
/// diverge.
#[cfg(feature = "solve")]
fn parse_solve_options(
    opts_str: &str,
    t0: f64,
    t_end: f64,
    progress: Option<js_sys::Function>,
) -> Result<crate::simulate::SolveOptions, JsValue> {
    use crate::simulate::{Alg, Flow, Progress, ProgressFn, SolveOptions};

    let opts_json: serde_json::Value = {
        let s = opts_str.trim();
        if s.is_empty() {
            serde_json::json!({})
        } else {
            serde_json::from_str(s).map_err(js_err("Options parse error"))?
        }
    };

    let mut opts = SolveOptions::default();
    // Canonical `alg` first, the pre-harmonization `solver` as an alias.
    let alg_name = opts_json
        .get("alg")
        .or_else(|| opts_json.get("solver"))
        .and_then(|v| v.as_str());
    if let Some(s) = alg_name {
        opts.alg =
            Alg::from_name(s).ok_or_else(|| JsValue::from_str(&format!("Unknown alg '{s}'")))?;
    }
    // `Some(v)`: `SolveOptions::{abstol,reltol}` are `Option<f64>` so that "the
    // caller said nothing" stays distinguishable from "the caller asked for the
    // default value" -- esm-spec §2.2.2 puts the document's `solver` block
    // between the two. A key absent from the options JSON leaves `None` and lets
    // the document win.
    if let Some(v) = opts_json.get("abstol").and_then(|v| v.as_f64()) {
        opts.abstol = Some(v);
    }
    if let Some(v) = opts_json.get("reltol").and_then(|v| v.as_f64()) {
        opts.reltol = Some(v);
    }
    if let Some(v) = opts_json
        .get("maxiters")
        .or_else(|| opts_json.get("maxSteps"))
        .and_then(|v| v.as_u64())
    {
        opts.maxiters = Some(v as usize);
    }
    if let Some(n) = opts_json.get("outputPoints").and_then(|v| v.as_u64()) {
        opts.sample_evenly(t0, t_end, n as usize);
    }

    // Positional `(fraction, t, step)` rather than an options object: this fires
    // on every accepted step, and allocating a JS object per step for values the
    // host already knows (it passed t0/t_end/maxSteps in) is pure overhead.
    //
    // Cancel is an explicit `=== false` and nothing else. Treating "falsy" as
    // cancel would make the most natural observer — `(f) => postMessage(f)`,
    // which returns `undefined` — abort on its first call.
    if let Some(cb) = progress {
        let observer: ProgressFn = std::sync::Arc::new(move |p: &Progress<'_>| {
            match cb.call3(
                &JsValue::NULL,
                &JsValue::from_f64(p.fraction()),
                &JsValue::from_f64(p.t),
                &JsValue::from_f64(p.step as f64),
            ) {
                Ok(v) => {
                    if v == JsValue::FALSE {
                        Flow::Cancel
                    } else {
                        Flow::Continue
                    }
                }
                // The observer threw. Stopping is the conservative reading: a
                // host whose progress reporting is broken should not be left
                // with a run it can no longer see or interrupt.
                Err(_) => Flow::Cancel,
            }
        });
        opts.progress = Some(observer);
    }
    Ok(opts)
}

/// A solution in the object shape [`solve`] has always returned.
#[cfg(feature = "solve")]
fn solution_json(sol: &crate::simulate::Solution) -> serde_json::Value {
    serde_json::json!({
        "time": sol.time,
        "state": sol.state,
        "stateVariableNames": sol.state_variable_names,
        // The SciML return code (esm-libraries-spec §2.5.3). A host tells "ran
        // to t_end" from "stopped early, here is why" by reading THIS, not by
        // comparing step counters or parsing an error string.
        "retcode": sol.retcode.name(),
        "metadata": metadata_json(sol),
    })
}

/// [`solve`]'s `metadata` object.
#[cfg(feature = "solve")]
fn metadata_json(sol: &crate::simulate::Solution) -> serde_json::Value {
    serde_json::json!({
        "alg": sol.metadata.alg,
        "nRhsCalls": sol.metadata.n_rhs_calls,
        "nJacobianCalls": sol.metadata.n_jacobian_calls,
        "nAcceptedSteps": sol.metadata.n_accepted_steps,
        "nRejectedSteps": sol.metadata.n_rejected_steps,
        // Rules the vectorized tape could not compile: `[{rule, reason}, …]`.
        // Always empty here: `solve` builds through `esm_problem`, whose
        // strict compilers refuse such a model at construction, and the
        // `interpreter` builds no tape. Kept so the metadata shape is stable.
        "tapeFallbacks": sol.metadata.tape_fallbacks.iter()
            .map(|(rule, reason)| serde_json::json!({"rule": rule, "reason": reason}))
            .collect::<Vec<_>>(),
    })
}

/// Read back every build-time observed field of a document, evaluated once.
///
/// The counterpart to [`solve`] for a document with **no state variables**: a
/// `system_kind: "nonlinear"` file whose whole content is its observed graph has
/// nothing to integrate, `solve` refuses it with
/// [`SimulateError::NotDynamic`](crate::SimulateError::NotDynamic), and
/// `observed_field` is how such a document's results are read (API_SPEC §5.8).
/// Without this export a wasm host had no way to reach that path at all — the
/// only entry point that runs a model was `solve`, so a browser presented with a
/// state-free document could only hand it to the ODE solver and report whatever
/// the integrator said about a zero-length state vector.
///
/// Arguments mirror [`solve`]'s leading five. `t0`/`t_end` are the problem's
/// span: nothing here integrates over it, but construction takes a span and a
/// time-dependent loader is sampled against it. `params_str` and `ic_str` are
/// the same JSON `{name: number}` maps, bound as `p` and `u0`.
///
/// Returns `{ names: string[], fields: { [name]: { shape: number[], values:
/// number[] } } }`. `names` is [`EsmProblem::observed_field_names`] —
/// component-qualified, sorted, and the spelling that resolves however many
/// components the document has. `values` is the field in row-major (C) order,
/// and `shape` is empty for a rank-0 observed, whose `values` is then a single
/// number. Both are present for every name, so a host need not call twice.
///
/// A document that DOES have state variables is not an error here: it simply
/// reports whatever fields its build materialized, which for an ordinary ODE
/// model is usually none. Deciding which of the two entry points a document
/// wants is the host's job, and `names.length` is not the way to do it —
/// [`crate::classification`] answers it from the document.
#[wasm_bindgen]
pub fn observed_fields(
    json_str: &str,
    t0: f64,
    t_end: f64,
    params_str: &str,
    ic_str: &str,
) -> Result<JsValue, JsValue> {
    use crate::problem::{ProblemOptions, esm_problem, observed_field};

    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;

    let prob = esm_problem(
        &esm_file,
        (t0, t_end),
        ProblemOptions {
            p: parse_binding_map(params_str, "Params")?,
            u0: parse_binding_map(ic_str, "Initial-conditions")?,
            ..Default::default()
        },
    )
    .map_err(js_err("EsmProblem build error"))?;

    let names = prob.observed_field_names();
    let mut fields = serde_json::Map::with_capacity(names.len());
    for name in &names {
        // Every name came from `observed_field_names`, so a miss is a defect in
        // the resolver rather than a caller error — report it as one instead of
        // silently returning a short map.
        let a = observed_field(&prob, name).map_err(js_err(&format!("observed_field({name})")))?;
        fields.insert(
            name.clone(),
            serde_json::json!({
                "shape": a.shape(),
                "values": a.iter().copied().collect::<Vec<f64>>(),
            }),
        );
    }

    to_js(&serde_json::json!({ "names": names, "fields": fields }))
}

/// Compile a document's RHS onto the vectorized tape and report which rules
/// did NOT make it — WITHOUT integrating anything.
///
/// The companion to [`solve`]'s `metadata.tapeFallbacks`, for the case that
/// motivated it: a model with a fallback in a tendency equation may take hours,
/// so the metadata that would have named the culprit never arrives. This runs
/// the build alone (milliseconds) and answers the same question up front.
///
/// Returns `{ nRules, nTaped, fallbacks: [{ rule, reason }, …] }`. A non-empty
/// `fallbacks` means the model has no tape at all: `native` (the default
/// compiler) refuses it, naming the first of them, and only the `interpreter`
/// can run it. See `esm-spec.md` §9.6.10
/// for the authoring patterns that keep rules vectorizable.
///
/// Files whose RHS is not array-compilable at all (pure scalar ODE systems)
/// report `nRules: 0` — they never build a tape and have nothing to fall back
/// from.
#[wasm_bindgen]
pub fn tape_report(json_str: &str) -> Result<JsValue, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;
    let compiled = match crate::simulate_array::ArrayCompiled::from_file(&esm_file) {
        Ok(c) => c,
        // Not an array model: no tape, nothing to report.
        Err(_) => {
            return to_js(&serde_json::json!({
                "nRules": 0, "nTaped": 0, "fallbacks": [],
            }));
        }
    };
    let report = compiled.debug_build_tape_report();
    let out = serde_json::json!({
        "nRules": report.n_rules,
        "nTaped": report.n_taped,
        "fallbacks": report.fallbacks.iter()
            .map(|(rule, reason)| serde_json::json!({"rule": rule, "reason": reason}))
            .collect::<Vec<_>>(),
    });
    to_js(&out)
}

/// Generate component graph for ESM file (WASM version)
#[wasm_bindgen]
pub fn component_graph(json_str: &str) -> Result<JsValue, JsValue> {
    let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;

    let graph = rust_component_graph(&esm_file);
    to_js(&graph)
}

/// Area of a lon/lat polygon `ring` under `manifold` (RFC §8.1). `ring_json` is a
/// JSON array of `[lon, lat]` pairs in **degrees**; `manifold` is one of
/// `"planar" | "spherical" | "geodesic"`. Returns the planar shoelace area for
/// `planar`, or the great-circle area in **steradians** for `spherical`/`geodesic`.
///
/// The spherical/geodesic path runs the in-module `s2rst` kernel — no host setup,
/// no JS bridge. Only a degenerate ring errors.
#[wasm_bindgen]
pub fn polygon_area(ring_json: &str, manifold: &str) -> Result<f64, JsValue> {
    let ring: Vec<(f64, f64)> =
        serde_json::from_str(ring_json).map_err(js_err("ring parse error"))?;
    let m = crate::geometry::Manifold::from_flag(manifold)
        .ok_or_else(|| JsValue::from_str(&format!("unknown manifold '{manifold}'")))?;
    crate::geometry::polygon_area(&ring, m).map_err(|e| JsValue::from_str(&e.to_string()))
}

/// Clip lon/lat polygon rings `a` and `b` on `manifold` and return the overlap
/// ring as a JSON array of `[lon, lat]` pairs (degrees; `[]` for a disjoint or
/// edge-touching clip). `a_json`/`b_json` are JSON `[lon, lat]` arrays; `manifold`
/// is `"planar" | "spherical" | "geodesic"`. Like [`polygon_area`], every
/// manifold is served in-module — the spherical/geodesic clip needs no host setup.
#[wasm_bindgen]
pub fn intersect_polygon(a_json: &str, b_json: &str, manifold: &str) -> Result<String, JsValue> {
    let a: Vec<(f64, f64)> = serde_json::from_str(a_json).map_err(js_err("a parse error"))?;
    let b: Vec<(f64, f64)> = serde_json::from_str(b_json).map_err(js_err("b parse error"))?;
    let m = crate::geometry::Manifold::from_flag(manifold)
        .ok_or_else(|| JsValue::from_str(&format!("unknown manifold '{manifold}'")))?;
    let ring = crate::geometry::intersect_polygon(&a, &b, m)
        .map_err(|e| JsValue::from_str(&e.to_string()))?;
    serde_json::to_string(&ring).map_err(js_err("serialize error"))
}

/// Report the crate and supported-schema versions. (The native performance
/// feature flags were dropped from this report: they are never enabled in a
/// wasm build, so advertising them here was misleading.)
#[wasm_bindgen]
pub fn get_version_info() -> JsValue {
    let info = serde_json::json!({
        "library_version": crate::LIBRARY_VERSION,
        "schema_version": crate::SCHEMA_VERSION,
    });
    to_js(&info).unwrap_or(JsValue::NULL)
}

/// Benchmark parsing performance (WASM version)
#[wasm_bindgen]
pub fn benchmark_parsing(json_str: &str, iterations: u32) -> Result<f64, JsValue> {
    let start = js_sys::Date::now();

    for _ in 0..iterations {
        rust_load_string(json_str).map_err(js_err("Parse error"))?;
    }

    let end = js_sys::Date::now();
    let total_time = end - start;

    Ok(total_time / iterations as f64)
}

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

    #[test]
    fn test_new_wasm_exports_compile() {
        let json = r#"
            {
              "esm": "1.0.0",
              "metadata": {
                "name": "Test Model",
                "description": "A simple test model for WASM exports"
              },
              "models": {
                "SimpleModel": {
                  "variables": {
                    "x": {
                      "type": "unknown",
                      "units": "m",
                      "default": 1.0
                    },
                    "k": {
                      "type": "parameter",
                      "default": 0.5
                    }
                  },
                  "equations": [
                    {
                      "lhs": {
                        "op": "D",
                        "args": [
                          "x"
                        ]
                      },
                      "rhs": {
                        "op": "*",
                        "args": [
                          "k",
                          "x"
                        ]
                      }
                    }
                  ]
                }
              }
            }
            "#;

        // Test that the core functions work (without WASM feature for regular tests)
        let esm_file = rust_load_string(json).expect("Should load valid ESM file");
        let graph = rust_component_graph(&esm_file);

        assert_eq!(graph.nodes.len(), 1, "Should have 1 model node");
        assert_eq!(graph.edges.len(), 0, "Should have no edges");
        assert_eq!(graph.nodes[0].id, "SimpleModel");

        println!("✓ New WASM export functions compile and core functionality works");
    }
}

// =============================================================================
// The Problem / Solution handles
// =============================================================================
//
// [`solve`] and [`observed_fields`] each build a problem, use it once, and throw
// it away, because a `wasm_bindgen` export cannot take one as an argument. That
// is right for a host that runs a document once — the app's Run button — and
// wrong for one that runs the same document repeatedly: a parameter sweep pays
// for flatten, value invention and compile on every member, and reading an
// observed back means solving a second time.
//
// `EsmProblem` is fully owned (`Rc<JsonValue>`, `Rc<Backend>`,
// `Rc<BuildProducts>`) and carries no lifetime, so it satisfies wasm-bindgen's
// `'static` bound and can be handed over as an opaque handle. What JS owes in
// exchange is DISPOSAL: a pointer-backed wasm-bindgen class is not reachable by
// the JavaScript garbage collector, so a handle that is never `free()`d leaks
// its compiled backend and every build product with it. Both classes below are
// `try`/`finally` material.
//
//   const prob = Problem.build(json, 0, 10, '{}', '{}', '{}')
//   try {
//     const sol = prob.solve('{"alg":"bdf"}')
//     try { console.log(sol.observed('NOx')) } finally { sol.free() }
//   } finally { prob.free() }

/// A built [`crate::problem::EsmProblem`], reusable across solves.
///
/// `Rc` rather than a bare value so a [`Solution`] can hold the problem it came
/// from: an observed's trajectory is a function of both, and making the host
/// pass the problem back in would let it pass a DIFFERENT one — silently
/// producing numbers from the wrong parameter bindings.
#[cfg(feature = "solve")]
#[wasm_bindgen]
pub struct Problem {
    inner: std::rc::Rc<crate::problem::EsmProblem>,
}

#[cfg(feature = "solve")]
#[wasm_bindgen]
impl Problem {
    /// Build a problem from a document. The arguments are [`solve`]'s leading
    /// five, and mean the same things.
    ///
    /// Not a `constructor`: wasm-bindgen constructors cannot be fallible in a
    /// way that reads well from JS, and building a document is the step most
    /// likely to fail.
    pub fn build(
        json_str: &str,
        t0: f64,
        t_end: f64,
        params_str: &str,
        ic_str: &str,
    ) -> Result<Problem, JsValue> {
        Problem::build_with(json_str, t0, t_end, params_str, ic_str, None)
    }

    /// [`Problem::build`] naming WHICH compiler builds the right-hand side
    /// (API_SPEC §5.8's closed vocabulary — `native`, `interpreter`, `xla`,
    /// `mtk`, `sympy`). `undefined` is `native`, and `native` is strict.
    ///
    /// A separate entry point rather than a sixth argument on `build`, because
    /// `build`'s five arguments are `solve`'s leading five and a host calls
    /// them positionally.
    #[wasm_bindgen(js_name = buildWith)]
    pub fn build_with(
        json_str: &str,
        t0: f64,
        t_end: f64,
        params_str: &str,
        ic_str: &str,
        compiler: Option<String>,
    ) -> Result<Problem, JsValue> {
        use crate::problem::{Compiler, ProblemOptions, Rhs, esm_problem};

        let compiler = match compiler.as_deref() {
            None | Some("") => None,
            Some(name) => Some(Compiler::parse_named(name).map_err(|m| JsValue::from_str(&m))?),
        };
        let esm_file = rust_load_string(json_str).map_err(js_err("Parse error"))?;
        let prob = esm_problem(
            &esm_file,
            (t0, t_end),
            ProblemOptions {
                p: parse_binding_map(params_str, "Params")?,
                u0: parse_binding_map(ic_str, "Initial-conditions")?,
                // `Auto`, not `Always`: a handle is also how a host inspects a
                // STATIC document, and `Always` would fail to build the very
                // documents `observed_field` exists to answer for. A host that
                // means to integrate calls `solve`, which says so with its own
                // error if there is nothing to integrate.
                rhs: Rhs::Auto,
                compiler,
                ..Default::default()
            },
        )
        .map_err(js_err("EsmProblem build error"))?;
        Ok(Problem {
            inner: std::rc::Rc::new(prob),
        })
    }

    /// The compiler that built this problem — API_SPEC §5.8's vocabulary
    /// spelling, e.g. `"native"`.
    pub fn compiler(&self) -> String {
        self.inner.compiler().as_str().to_string()
    }

    /// Where every rule landed:
    /// `{ compiler, nTaped, nOracle, fusedGroups, rules: [{ rule, kind,
    /// cadence, tier, reason }, …] }`.
    ///
    /// The host's only window into the build, and the one that answers the
    /// question `tapeReport` answers only half of: a rule that DID lower is
    /// named here too, with the cadence tier that says what it costs.
    #[wasm_bindgen(js_name = compilerReport)]
    pub fn compiler_report(&self) -> Result<JsValue, JsValue> {
        let report = self.inner.compiler_report();
        to_js(&serde_json::json!({
            "compiler": report.compiler().as_str(),
            "nTaped": report.n_taped(),
            "nOracle": report.n_oracle(),
            "fusedGroups": report.fused_groups(),
            "fusedInstructions": report.fused_instructions(),
            "rules": report.rules().iter().map(|r| serde_json::json!({
                "rule": r.rule,
                "kind": r.kind,
                "cadence": r.cadence,
                "tier": r.tier,
                "reason": r.reason,
            })).collect::<Vec<_>>(),
        }))
    }

    /// Integrate, returning a [`Solution`] handle.
    ///
    /// `opts_str` and `progress` are [`solve`]'s, unchanged — including the
    /// `alg` / `solver` alias and the `=== false` cancel convention.
    pub fn solve(
        &self,
        opts_str: &str,
        progress: Option<js_sys::Function>,
    ) -> Result<Solution, JsValue> {
        let (t0, t_end) = self.inner.tspan();
        let opts = parse_solve_options(opts_str, t0, t_end, progress)?;
        let sol = crate::problem::solve(&self.inner, &opts).map_err(js_err("Solve error"))?;
        Ok(Solution {
            inner: sol,
            problem: self.inner.clone(),
        })
    }

    /// A NEW problem with different bindings, sharing this one's compiled
    /// right-hand side and build products (§2.5.5) — the point of holding a
    /// handle at all.
    ///
    /// Empty JSON leaves that half unchanged; `tspan_str` is `[t0, t_end]` or
    /// empty. The original is untouched, so a sweep keeps one problem and
    /// derives a member per point.
    pub fn remake(
        &self,
        params_str: &str,
        ic_str: &str,
        tspan_str: &str,
    ) -> Result<Problem, JsValue> {
        let tspan = {
            let s = tspan_str.trim();
            if s.is_empty() {
                None
            } else {
                let v: Vec<f64> = serde_json::from_str(s).map_err(js_err("Tspan parse error"))?;
                if v.len() != 2 {
                    return Err(JsValue::from_str("Tspan must be [t0, t_end]"));
                }
                Some((v[0], v[1]))
            }
        };
        let next = crate::problem::remake(
            &self.inner,
            &crate::problem::Remake {
                p: parse_binding_map(params_str, "Params")?,
                u0: parse_binding_map(ic_str, "Initial-conditions")?,
                tspan,
                callbacks: None,
            },
        )
        .map_err(js_err("Remake error"))?;
        Ok(Problem {
            inner: std::rc::Rc::new(next),
        })
    }

    /// One BUILD-time observed field, as `{ shape, values }` (API_SPEC §5.8).
    ///
    /// The handle form of [`observed_fields`]. For an observed of a document
    /// that integrates, ask the [`Solution`]: a build-time field is a constant
    /// and a trajectory is not.
    pub fn observed_field(&self, name: &str) -> Result<JsValue, JsValue> {
        let a = crate::problem::observed_field(&self.inner, name)
            .map_err(js_err(&format!("observed_field({name})")))?;
        to_js(&serde_json::json!({
            "shape": a.shape(),
            "values": a.iter().copied().collect::<Vec<f64>>(),
        }))
    }

    /// Every name [`Problem::observed_field`] can answer for, sorted.
    #[wasm_bindgen(js_name = observedFieldNames)]
    pub fn observed_field_names(&self) -> Vec<String> {
        self.inner.observed_field_names()
    }

    /// Flattened state-variable names, in solver order.
    #[wasm_bindgen(js_name = stateVariableNames)]
    pub fn state_variable_names(&self) -> Vec<String> {
        self.inner.state_variable_names()
    }

    /// Flattened parameter names.
    #[wasm_bindgen(js_name = parameterNames)]
    pub fn parameter_names(&self) -> Vec<String> {
        self.inner.parameter_names()
    }

    /// Whether there is anything to integrate. `false` means [`Problem::solve`]
    /// will refuse, and the results are read with
    /// [`Problem::observed_field`].
    #[wasm_bindgen(js_name = isDynamic)]
    pub fn is_dynamic(&self) -> bool {
        self.inner.is_dynamic()
    }

    /// The integration interval, as `[t0, t_end]`.
    pub fn tspan(&self) -> Vec<f64> {
        let (a, b) = self.inner.tspan();
        vec![a, b]
    }
}

/// A trajectory, plus the problem that produced it.
///
/// Holding the problem is what lets [`Solution::observed`] answer at all: an
/// observed is a pure function of `(state, params, t)`, the problem holds the
/// function and this holds the arguments.
#[cfg(feature = "solve")]
#[wasm_bindgen]
pub struct Solution {
    inner: crate::simulate::Solution,
    problem: std::rc::Rc<crate::problem::EsmProblem>,
}

#[cfg(feature = "solve")]
#[wasm_bindgen]
impl Solution {
    /// The whole trajectory in [`solve`]'s object shape, so a host can move
    /// between the two APIs without a second decoder.
    #[wasm_bindgen(js_name = toJSON)]
    pub fn to_json(&self) -> Result<JsValue, JsValue> {
        to_js(&solution_json(&self.inner))
    }

    /// Output times.
    pub fn time(&self) -> Vec<f64> {
        self.inner.time.clone()
    }

    /// Flattened state-variable names, parallel to the rows of the state.
    #[wasm_bindgen(js_name = stateVariableNames)]
    pub fn state_variable_names(&self) -> Vec<String> {
        self.inner.state_variable_names.clone()
    }

    /// One state variable's trajectory, by name (esm-libraries-spec §2.5.7).
    ///
    /// Resolves the exact flattened name, then a unique bare tail. Returns an
    /// error rather than an empty array for a name it does not have, so a typo
    /// is not a row of zeros.
    pub fn get(&self, name: &str) -> Result<Vec<f64>, JsValue> {
        self.inner
            .get(name)
            .map(|v| v.to_vec())
            .ok_or_else(|| JsValue::from_str(&format!("no state variable named '{name}'")))
    }

    /// One OBSERVED variable's trajectory over the output grid.
    ///
    /// The reason this class holds its problem. A `Solution` carries state rows
    /// only — in every binding — so before this there was no way to read back
    /// an observed of a model that integrates. Name resolution is API_SPEC
    /// §5.8's rule.
    pub fn observed(&self, name: &str) -> Result<Vec<f64>, JsValue> {
        crate::problem::observed_trajectory(&self.problem, &self.inner, name)
            .map_err(js_err(&format!("observed({name})")))
    }

    /// Several observeds in ONE pass over the output grid.
    ///
    /// `names_str` is a JSON array; the result is an object keyed by the names
    /// that were ASKED FOR. The graph is walked once per output time however
    /// many names are given, so this is materially cheaper than a loop over
    /// [`Solution::observed`].
    ///
    /// **Tolerant, where [`Solution::observed`] is strict.** A name that is not
    /// an observed variable is simply ABSENT from the result rather than an
    /// error — most often because it is a STATE, which the caller already has.
    /// That is what a host reading a model's authored assertions needs: it
    /// knows the variable names and not which kind each is, and one state in
    /// the list must not cost it the other answers. A caller that wants "this
    /// specific name or an explanation" asks [`Solution::observed`].
    #[wasm_bindgen(js_name = observedMany)]
    pub fn observed_many(&self, names_str: &str) -> Result<JsValue, JsValue> {
        let names: Vec<String> =
            serde_json::from_str(names_str).map_err(js_err("Names parse error"))?;
        let rows = crate::problem::observed_trajectories(&self.problem, &self.inner, &names)
            .map_err(js_err("observedMany"))?;
        let out: serde_json::Map<String, serde_json::Value> = rows
            .into_iter()
            .map(|(n, row)| (n, serde_json::json!(row)))
            .collect();
        to_js(&out)
    }

    /// Every observed variable this solution can report a trajectory for.
    #[wasm_bindgen(js_name = observedNames)]
    pub fn observed_names(&self) -> Vec<String> {
        self.problem.observed_variable_names()
    }

    /// The SciML return code. `"Success"` means the run reached `t_end`.
    pub fn retcode(&self) -> String {
        self.inner.retcode.name().to_string()
    }

    /// Solver provenance and step counters.
    pub fn metadata(&self) -> Result<JsValue, JsValue> {
        to_js(&metadata_json(&self.inner))
    }
}