orion-server 1.8.1

Turn business logic into live REST/Kafka services, declared as JSON
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
//! From active model rows to what a generation carries: one entry per
//! model that can serve on this node, with its adapters and result
//! expression compiled on the generation's own expression engine, and the
//! reasons anything did not load.
//!
//! A load never aborts. A row a node cannot serve — models disabled here,
//! an admission that has not passed, a stored manifest that no longer
//! parses, an adapter the serving engine refuses — becomes a
//! [`ModelLoadIssue`], and every workflow naming that model by its literal
//! id is quarantined with the reason attached (`runtime::models`), which is
//! the shape a plugin that fails to load already takes. The node keeps
//! serving everything else.
//!
//! The adapters are compiled **here and not at upload**: a `datalogic`
//! program is bound to the engine that compiled it, and every generation
//! builds a fresh one, so the set is rebuilt on every publish — cheap, a
//! handful of expressions — and evaluated only on `generation.engine`'s.
//! Nothing is loaded into a runtime at this point; that is the
//! [`LoadedCache`]'s, on first use or at preload.
//!
//! The same compile serves an **offline** set ([`ModelSet::from_manifests`]):
//! `dry-run` and `orion-server test` build one from the manifests a
//! `--model-dir` holds, on the engine that will evaluate them, with no row
//! and no admission behind any entry — the bytes on disk are what the author
//! is testing, and are trusted as such.

use std::collections::HashMap;
use std::sync::Arc;

use dataflow_rs::datalogic_rs as datalogic;
use serde_json::Value;
use tokio::sync::Semaphore;

use super::admission::Stats;
use super::artifact::ArtifactRef;
use super::cache::LoadedCache;
use super::limits::Limits;
use super::manifest::Manifest;
use super::runtimes::LoadBinding;
use crate::config::ModelsConfig;
use crate::storage::models::{Model, ModelHealth};

/// The task function that runs a model, and the input field that names it.
/// The dependants walk and the quarantine read the authored task, so they
/// need only the spelling; the schema lives beside the handler.
pub const INFER_FUNCTION: &str = "model_infer";
pub const INFER_MODEL_FIELD: &str = "model";

/// One model version this generation can run.
pub struct ModelEntry {
    pub id: String,
    pub version: i64,
    pub digest: String,
    pub manifest: Manifest,
    /// What a load of this model's artifact is given — and, with the
    /// digest, the runtime and the device, what its session is keyed by.
    pub binding: Arc<LoadBinding>,
    pub artifact: ArtifactRef,
    /// What admission read out of the artifact, when the row carries it.
    pub stats: Option<Stats>,
    /// The row's tags. Empty for an offline set, which has no row — they
    /// are a property of the registration, not of the manifest.
    pub tags: Vec<String>,
    /// One compiled adapter per manifest input, in manifest order.
    pub adapters: Vec<(String, datalogic::Logic)>,
    /// The compiled result expression, over `{output name: tensor}`.
    pub result: datalogic::Logic,
    pub limits: Limits,
    /// `limits.max_concurrency` inference slots for this model on this node.
    pub permits: Arc<Semaphore>,
}

/// Why a model version is not serving on this node.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct ModelLoadIssue {
    pub model: String,
    pub version: i64,
    pub digest: String,
    /// `disabled`, `admission`, `manifest`, `artifact`, `adapter`.
    pub stage: &'static str,
    pub reason: String,
}

/// The model half of a generation: what loaded, and what did not.
///
/// Deliberately without an identity of its own, unlike [`PluginSet`]. That
/// fingerprint exists so a reload can tell an unchanged plugin set from a
/// changed one and skip rebuilding the engine; a model set has no such
/// shortcut available to it, because **both** reload paths hand it a fresh
/// expression engine — `Engine::with_new_workflows` compiles on one of its
/// own, exactly as a full rebuild does — and a compiled `datalogic` program
/// belongs to the engine that compiled it. A set carried across would hold
/// adapters no generation evaluates on. One was here once, computed on
/// every load and read by nothing, which is an invitation to write that
/// bug; if a cheaper path is wanted, the thing to change is what a model
/// set is compiled on, not what a reload compares.
///
/// [`PluginSet`]: crate::plugin::PluginSet
#[derive(Default)]
pub struct ModelSet {
    models: HashMap<String, Arc<ModelEntry>>,
    pub issues: Vec<ModelLoadIssue>,
}

impl ModelSet {
    /// The set a node boots on, and the one a node with models disabled
    /// keeps when no model row exists.
    pub fn empty() -> Self {
        Self::default()
    }

    /// Every active row, compiled on `datalogic` — the engine of the
    /// generation this set will be published with. `enabled` is whether
    /// this node has the model runtime at all; `false` makes every row a
    /// `disabled` issue rather than an abort, so the estate stays observable
    /// and the workflows naming those models are quarantined with the
    /// reason. Never fails.
    pub fn load_active(
        rows: &[Model],
        config: &ModelsConfig,
        enabled: bool,
        datalogic: &datalogic::Engine,
    ) -> Self {
        let mut set = ModelSet::default();
        for row in rows {
            let compiled = decode_row(row, enabled).and_then(|item| {
                compile_entry(item, config, datalogic).map_err(|reason| ("adapter", reason))
            });
            match compiled {
                Ok(entry) => {
                    // Per model at debug and the count at info: this runs
                    // on every generation build, so an estate of any size
                    // would otherwise put one line per model into the log
                    // every time any workflow, channel or connector
                    // changed. A model that does *not* load still gets its
                    // own line, because that one is about something being
                    // wrong.
                    tracing::debug!(
                        model = %row.model_id,
                        version = row.version,
                        digest = %row.digest,
                        inputs = ?entry.manifest.input_names().collect::<Vec<_>>(),
                        "Model ready: adapters compiled on this generation"
                    );
                    set.models.insert(row.model_id.clone(), Arc::new(entry));
                }
                Err((stage, reason)) => {
                    tracing::error!(
                        model = %row.model_id,
                        version = row.version,
                        digest = %row.digest,
                        stage,
                        reason = %reason,
                        "Model not available: the workflows naming it are quarantined"
                    );
                    set.issues.push(ModelLoadIssue {
                        model: row.model_id.clone(),
                        version: row.version,
                        digest: row.digest.clone(),
                        stage,
                        reason,
                    });
                }
            }
        }
        if !rows.is_empty() {
            tracing::info!(
                ready = set.models.len(),
                unavailable = set.issues.len(),
                "Model set compiled on this generation"
            );
        }
        set
    }

    /// An offline set: one entry per manifest, compiled on `datalogic` — the
    /// engine of the dry run that will evaluate them — with the digest and
    /// stats the caller read off the file beside each manifest. No admission
    /// stands behind an entry: the bytes are the author's own, and what a
    /// serving node would verify at admission (the digest claim, the probe)
    /// is exactly what an offline run exists to try. A manifest whose
    /// adapters do not compile is an issue, as a row's would be, so the run
    /// that names it fails as `unavailable` with the reason rather than
    /// silently skipping the model.
    pub fn from_manifests(
        manifests: impl IntoIterator<Item = ManifestEntry>,
        config: &ModelsConfig,
        datalogic: &datalogic::Engine,
    ) -> Self {
        let mut set = ModelSet::default();
        let mut parts = Vec::new();
        for entry in manifests {
            let id = entry.manifest.name.clone();
            let digest = entry.digest.clone();
            parts.push(format!("{id}@0:{digest}"));
            let item = CompileItem {
                id: id.clone(),
                version: 0,
                digest: digest.clone(),
                manifest: entry.manifest,
                // Offline the bytes come from the file, not a bucket: the
                // reference names that file so a log line can say where the
                // model was read from, and nothing fetches through it.
                artifact: ArtifactRef {
                    connector: String::new(),
                    key: entry.artifact_path.display().to_string(),
                    digest: digest.clone(),
                    size: None,
                },
                stats: entry.stats,
                // Offline there is no row, and so no registration to have
                // tagged.
                tags: Vec::new(),
            };
            match compile_entry(item, config, datalogic) {
                Ok(compiled) => {
                    set.models.insert(id, Arc::new(compiled));
                }
                Err(reason) => set.issues.push(ModelLoadIssue {
                    model: id,
                    version: 0,
                    digest,
                    stage: "adapter",
                    reason,
                }),
            }
        }
        set
    }

    /// The entry serving `model_id` — the active version — if this node
    /// loaded it.
    pub fn get(&self, model_id: &str) -> Option<&Arc<ModelEntry>> {
        self.models.get(model_id)
    }

    /// Why `model_id` is not serving here, if an active row exists for it
    /// and did not load.
    pub fn issue_for(&self, model_id: &str) -> Option<&ModelLoadIssue> {
        self.issues.iter().find(|i| i.model == model_id)
    }

    /// The ids this set serves, in no particular order.
    pub fn ids(&self) -> impl Iterator<Item = &str> {
        self.models.keys().map(String::as_str)
    }

    /// Every entry this set serves.
    pub fn entries(&self) -> impl Iterator<Item = &Arc<ModelEntry>> {
        self.models.values()
    }

    pub fn is_empty(&self) -> bool {
        self.models.is_empty()
    }

    /// This node's account of one version, where the generation has one:
    /// `loaded` (with the runtime, device and resident bytes) while `cache`
    /// holds the version's digest on any runtime, `evicted` when it was
    /// loaded and is no longer, `failed` — or `disabled` — when the set
    /// carries an issue for it. `None` for a version the generation neither
    /// serves nor refused, so the caller keeps the admission-derived state.
    pub fn health_of(
        &self,
        model_id: &str,
        version: i64,
        cache: &LoadedCache,
    ) -> Option<ModelHealth> {
        if let Some(entry) = self.get(model_id).filter(|e| e.version == version) {
            if let Some((key, bytes)) = cache.loaded_for(&entry.digest, entry.binding.fingerprint())
            {
                return Some(ModelHealth {
                    state: "loaded".to_string(),
                    runtime: Some(key.runtime.to_string()),
                    device: Some(key.device),
                    resident_bytes: Some(bytes),
                    reason: None,
                });
            }
            if cache.was_evicted(&entry.digest, entry.binding.fingerprint()) {
                return Some(ModelHealth {
                    state: "evicted".to_string(),
                    ..ModelHealth::default()
                });
            }
            return None;
        }
        self.issues
            .iter()
            .find(|i| i.model == model_id && i.version == version)
            .map(|issue| ModelHealth {
                state: if issue.stage == "disabled" {
                    "disabled".to_string()
                } else {
                    "failed".to_string()
                },
                reason: Some(format!("{}: {}", issue.stage, issue.reason)),
                ..ModelHealth::default()
            })
    }
}

/// A manifest an offline set is built from: the file beside it, hashed and
/// read, by whoever found it on disk (`definitions::ModelDefinition`).
#[derive(Debug, Clone)]
pub struct ManifestEntry {
    pub manifest: Manifest,
    /// The artifact file the manifest's `artifact` names, resolved beside it.
    pub artifact_path: std::path::PathBuf,
    /// `sha256:…` of that file.
    pub digest: String,
    /// What `lint` read out of the graph, when it could — the offline twin
    /// of what admission records, minus the probe.
    pub stats: Option<Stats>,
}

/// What one entry is compiled from, once a row (or a manifest) has been
/// decoded: the part of the load that is the same on a node and offline.
struct CompileItem {
    id: String,
    version: i64,
    digest: String,
    manifest: Manifest,
    artifact: ArtifactRef,
    stats: Option<Stats>,
    tags: Vec<String>,
}

/// The row half of a load: the node has the runtime, the verdict passed,
/// and the stored columns decode. Nothing here touches an engine.
fn decode_row(row: &Model, enabled: bool) -> Result<CompileItem, (&'static str, String)> {
    if !enabled {
        return Err((
            "disabled",
            "models are disabled on this node (models.enabled = false)".to_string(),
        ));
    }
    // The verdict first: a row whose artifact no node has verified — or
    // that one refused — is not run here whatever its manifest says.
    let admission: Value = serde_json::from_str(&row.admission_json)
        .map_err(|e| ("admission", format!("stored admission does not parse: {e}")))?;
    let state = admission
        .get("state")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    if state != "passed" {
        let mut reason = format!("admission is '{state}'");
        if let Some(stage) = admission.get("stage").and_then(Value::as_str) {
            reason.push_str(&format!(" at stage '{stage}'"));
        }
        if let Some(why) = admission.get("reason").and_then(Value::as_str) {
            reason.push_str(&format!(": {why}"));
        } else if state == "pending" {
            reason.push_str(": no node has verified the artifact yet");
        }
        return Err(("admission", reason));
    }
    // Decoded, not re-validated, as the admission worker does: the row holds
    // the validated form, and a rule added since it was written is
    // preflight's to report. What *is* checked again is compilation, in
    // `compile_entry`, because the serving engine is the one that has to
    // accept it.
    let manifest: Manifest = serde_json::from_str(&row.manifest_json)
        .map_err(|e| ("manifest", format!("stored manifest does not parse: {e}")))?;
    let artifact: ArtifactRef = serde_json::from_str(&row.artifact_json).map_err(|e| {
        (
            "artifact",
            format!("stored artifact reference does not parse: {e}"),
        )
    })?;
    let stats = row
        .stats_json
        .as_deref()
        .and_then(|s| serde_json::from_str::<Stats>(s).ok());
    Ok(CompileItem {
        id: row.model_id.clone(),
        version: row.version,
        digest: row.digest.clone(),
        manifest,
        artifact,
        stats,
        // A tag list that does not parse is not a reason to refuse a model
        // the node can otherwise serve: it costs the row its `preload_tags`
        // membership and nothing else.
        tags: serde_json::from_str(&row.tags_json).unwrap_or_default(),
    })
}

/// The engine half: every adapter and the result compiled on `datalogic`,
/// the limits from the config, the permits sized by them. `Err` is the
/// reason the first expression that did not compile gave.
fn compile_entry(
    item: CompileItem,
    config: &ModelsConfig,
    datalogic: &datalogic::Engine,
) -> Result<ModelEntry, String> {
    let mut adapters = Vec::with_capacity(item.manifest.inputs.len());
    for input in &item.manifest.inputs {
        let logic = datalogic
            .compile(&item.manifest.adapter_for(input))
            .map_err(|e| format!("adapter for input '{}' does not compile: {e}", input.name))?;
        adapters.push((input.name.clone(), logic));
    }
    let result = datalogic
        .compile(&item.manifest.result_logic())
        .map_err(|e| format!("result expression does not compile: {e}"))?;

    let limits = Limits::effective(config, &item.id);
    Ok(ModelEntry {
        id: item.id,
        version: item.version,
        digest: item.digest,
        binding: Arc::new(LoadBinding::of(&item.manifest)),
        manifest: item.manifest,
        artifact: item.artifact,
        stats: item.stats,
        tags: item.tags,
        adapters,
        result,
        limits,
        permits: Arc::new(Semaphore::new(limits.max_concurrency as usize)),
    })
}

/// Every `(task id, model id)` pair in `tasks` where a task calls
/// `model_infer` with a literal `input.model`, through task groups. A
/// computed reference — an expression, a template — is not seen: the model
/// it resolves to is decided per message. The task id falls back to the
/// step path for a task without one.
pub fn literal_references(tasks: &Value) -> Vec<(String, String)> {
    crate::engine::walk_steps(tasks)
        .tasks
        .into_iter()
        .filter_map(|(path, task)| {
            let function = task.get("function")?;
            if function.get("name").and_then(Value::as_str) != Some(INFER_FUNCTION) {
                return None;
            }
            let named = function.get("input")?.get(INFER_MODEL_FIELD)?.as_str()?;
            let id = task
                .get("id")
                .and_then(Value::as_str)
                .map(str::to_string)
                .unwrap_or(path);
            Some((id, named.to_string()))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::fixture;
    use serde_json::json;

    fn engine() -> datalogic::Engine {
        crate::engine::operators::add_to_datalogic(
            datalogic::Engine::builder()
                .with_templating(true)
                .with_template_key_escape('$'),
        )
        .build()
    }

    fn row(admission: Value, manifest_json: &str) -> Model {
        let now = chrono::Utc::now().naive_utc();
        Model {
            model_id: "ada.c4-tiny".to_string(),
            version: 3,
            status: "active".to_string(),
            digest: "sha256:abc".to_string(),
            manifest_json: manifest_json.to_string(),
            artifact_json: json!({"connector": "bucket", "key": "k", "digest": "sha256:abc"})
                .to_string(),
            admission_json: admission.to_string(),
            stats_json: Some(
                json!({"parameters": 1479, "nodes": 4, "artifact_bytes": 6171, "probe_ms": 0.1,
                    "ir_version": 8, "opset": 17, "runtime": "tract", "device": "cpu"})
                .to_string(),
            ),
            tags_json: "[]".to_string(),
            signature: None,
            created_at: now,
            updated_at: now,
        }
    }

    fn passed() -> Value {
        json!({"state": "passed", "node": "n", "at": "2026-09-13T00:00:00"})
    }

    /// A resident session of no size, standing in for a loaded plan.
    struct Resident;

    impl crate::model::LoadedModel for Resident {
        fn digest(&self) -> &str {
            "sha256:abc"
        }
        fn resident_bytes(&self) -> usize {
            1
        }
        fn run(
            &self,
            _inputs: Vec<dataflow_rs::datavalue::OwnedDataTensor>,
        ) -> Result<Vec<dataflow_rs::datavalue::OwnedDataTensor>, crate::model::RunError> {
            Ok(Vec::new())
        }
    }

    /// Health answers for the model that asked, not for the bytes.
    ///
    /// Two models over one artifact are one digest and two sessions. While
    /// residency was read by digest alone, a model reported `loaded` on the
    /// strength of the *other* model's session — and the load it was waiting
    /// for had not happened.
    #[tokio::test]
    async fn health_is_read_per_binding_not_per_digest() {
        let config = ModelsConfig::default();
        let (order_a, order_b) = fixture::two_out();
        let mut rows = [
            row(passed(), fixture::TWO_OUT_A),
            row(passed(), fixture::TWO_OUT_B),
        ];
        rows[0].model_id = order_a.name.clone();
        rows[1].model_id = order_b.name.clone();
        let set = ModelSet::load_active(&rows, &config, true, &engine());
        assert!(set.issues.is_empty(), "{:?}", set.issues);
        let entry_a = set.get(&order_a.name).expect("loaded").clone();
        let entry_b = set.get(&order_b.name).expect("loaded").clone();
        assert_eq!(entry_a.digest, entry_b.digest, "one artifact");
        assert_ne!(
            entry_a.binding.fingerprint(),
            entry_b.binding.fingerprint(),
            "two bindings"
        );

        // Nothing resident: neither reports a state of its own.
        let cache = LoadedCache::new(1024);
        assert!(set.health_of(&order_a.name, 3, &cache).is_none());
        assert!(set.health_of(&order_b.name, 3, &cache).is_none());

        // Only `a`'s session is resident, so only `a` is loaded.
        cache
            .get_or_load(
                crate::model::CacheKey {
                    digest: entry_a.digest.clone(),
                    runtime: "tract",
                    device: "cpu".to_string(),
                    binding: entry_a.binding.fingerprint().to_string(),
                },
                || async { Ok(Arc::new(Resident) as Arc<dyn crate::model::LoadedModel>) },
            )
            .await
            .expect("loads");
        assert_eq!(
            set.health_of(&order_a.name, 3, &cache).map(|h| h.state),
            Some("loaded".to_string())
        );
        assert!(
            set.health_of(&order_b.name, 3, &cache).is_none(),
            "the other model's session is not this one's"
        );
    }

    /// The fixture row loads: one compiled adapter per input, the result
    /// compiled, the stats decoded, the limits and permits from the config.
    #[test]
    fn an_admitted_row_loads_with_its_adapters_compiled() {
        let config = ModelsConfig::default();
        let set = ModelSet::load_active(
            &[row(passed(), fixture::MANIFEST)],
            &config,
            true,
            &engine(),
        );
        assert!(set.issues.is_empty(), "{:?}", set.issues);
        let entry = set.get("ada.c4-tiny").expect("loaded");
        assert_eq!(entry.version, 3);
        assert_eq!(entry.digest, "sha256:abc");
        assert_eq!(entry.adapters.len(), 1);
        assert_eq!(entry.adapters[0].0, "board");
        assert_eq!(entry.stats.as_ref().map(|s| s.parameters), Some(1479));
        assert_eq!(entry.limits, Limits::effective(&config, "ada.c4-tiny"));
        assert_eq!(
            entry.permits.available_permits(),
            config.max_concurrency_per_model as usize
        );
        assert_eq!(set.ids().collect::<Vec<_>>(), ["ada.c4-tiny"]);
        assert!(set.issue_for("ada.c4-tiny").is_none());

        // The adapters evaluate on the engine they were compiled on, and
        // produce the declared tensor from the fixture's `data.board` shape.
        let engine = engine();
        let set =
            ModelSet::load_active(&[row(passed(), fixture::MANIFEST)], &config, true, &engine);
        let entry = set.get("ada.c4-tiny").expect("loaded");
        let board: Vec<Vec<Vec<f32>>> = vec![vec![vec![0.0; 7]; 6]; 2];
        let root =
            dataflow_rs::datavalue::OwnedDataValue::from(&json!({"data": {"board": [board]}}));
        let arena = datalogic::bumpalo::Bump::new();
        let value = engine
            .evaluate(&entry.adapters[0].1, &root, &arena)
            .expect("evaluates")
            .to_owned();
        let tensor = value.as_tensor().expect("a tensor");
        assert_eq!(tensor.shape(), [1, 2, 6, 7]);
        assert_eq!(tensor.dtype().name(), "f32");
    }

    /// Each way a row does not load is an issue naming its stage, and the
    /// row is absent from the served set.
    #[test]
    fn every_issue_kind_is_reported_and_keeps_the_model_out() {
        let config = ModelsConfig::default();
        let engine = engine();
        let cases: Vec<(&str, Model, &str)> = vec![
            (
                "disabled",
                row(passed(), fixture::MANIFEST),
                "models.enabled = false",
            ),
            (
                "admission",
                row(json!({"state": "pending"}), fixture::MANIFEST),
                "admission is 'pending'",
            ),
            (
                "admission",
                row(
                    json!({"state": "failed", "stage": "fetch", "reason": "GET answered HTTP 503"}),
                    fixture::MANIFEST,
                ),
                "at stage 'fetch': GET answered HTTP 503",
            ),
            ("manifest", row(passed(), "{not json"), "does not parse"),
            // Compiled on the engine it is given: a serving engine runs in
            // templating mode, where a multi-key object is an output
            // template, so the refusal is exercised on a bare engine, where
            // the same object is an unknown operator.
            (
                "adapter",
                row(
                    passed(),
                    &fixture::MANIFEST.replace(
                        r#"{ "tensor": [{ "var": "data.board" }, "f32"] }"#,
                        r#"{ "tensor": [{ "var": "data.board" }, "f32"], "and": [1] }"#,
                    ),
                ),
                "adapter for input 'board' does not compile",
            ),
        ];
        let bare = datalogic::Engine::builder().build();
        for (stage, row, needle) in cases {
            let enabled = stage != "disabled";
            let on = if stage == "adapter" { &bare } else { &engine };
            let set = ModelSet::load_active(std::slice::from_ref(&row), &config, enabled, on);
            assert!(set.get("ada.c4-tiny").is_none(), "{stage}: must not serve");
            let issue = set.issue_for("ada.c4-tiny").expect(stage);
            assert_eq!(issue.stage, stage, "{}", issue.reason);
            assert!(issue.reason.contains(needle), "{stage}: {}", issue.reason);
            assert_eq!(issue.version, 3);
            assert_eq!(issue.digest, "sha256:abc");
            // The health view names the stage and the reason.
            let cache = LoadedCache::new(1 << 20);
            let health = set.health_of("ada.c4-tiny", 3, &cache).expect("an account");
            assert_eq!(
                health.state,
                if stage == "disabled" {
                    "disabled"
                } else {
                    "failed"
                }
            );
            assert!(health.reason.as_deref().unwrap_or("").starts_with(stage));
            assert!(set.health_of("ada.c4-tiny", 2, &cache).is_none());
        }
        // An unparseable artifact reference has its own stage.
        let mut broken = row(passed(), fixture::MANIFEST);
        broken.artifact_json = "nope".to_string();
        let set = ModelSet::load_active(&[broken], &config, true, &engine);
        assert_eq!(
            set.issue_for("ada.c4-tiny").expect("issue").stage,
            "artifact"
        );
    }

    /// An offline set compiles the same entries a row set does, without a
    /// row: version `0`, the file's digest, the stats the caller read, and
    /// a manifest whose adapters do not compile becomes an `adapter` issue
    /// rather than a silent absence.
    #[test]
    fn an_offline_set_is_built_from_manifests_alone() {
        let config = ModelsConfig::default();
        let engine = engine();
        let entry = |text: &str| ManifestEntry {
            manifest: Manifest::parse(text).expect("valid"),
            artifact_path: std::path::PathBuf::from("c4-tiny.onnx"),
            digest: "sha256:abc".to_string(),
            stats: None,
        };
        let set = ModelSet::from_manifests([entry(fixture::MANIFEST)], &config, &engine);
        assert!(set.issues.is_empty(), "{:?}", set.issues);
        let loaded = set.get("ada.c4-tiny").expect("compiled");
        assert_eq!(loaded.version, 0);
        assert_eq!(loaded.digest, "sha256:abc");
        assert_eq!(loaded.artifact.key, "c4-tiny.onnx");
        assert_eq!(loaded.adapters.len(), 1);

        let bare = datalogic::Engine::builder().build();
        let set = ModelSet::from_manifests(
            [entry(&fixture::MANIFEST.replace(
                r#"{ "tensor": [{ "var": "data.board" }, "f32"] }"#,
                r#"{ "tensor": [{ "var": "data.board" }, "f32"], "and": [1] }"#,
            ))],
            &config,
            &bare,
        );
        assert!(set.get("ada.c4-tiny").is_none());
        let issue = set.issue_for("ada.c4-tiny").expect("an issue");
        assert_eq!(issue.stage, "adapter");
        assert_eq!(issue.version, 0);
    }

    /// Why a model set has no identity to compare, and why comparing one
    /// would be a bug.
    ///
    /// A reload that leaves the plugins alone takes the cheap engine path,
    /// `Engine::with_new_workflows`, and the tempting saving is to carry the
    /// previous model set across when the rows have not changed — the
    /// plugin half skips its rebuild on exactly that comparison. It is
    /// unsound here, and this is the fact that makes it so: that path
    /// compiles on a *fresh* expression engine, as a full rebuild does, and
    /// a compiled program belongs to the engine that compiled it. A carried
    /// set would hold adapters no generation evaluates on.
    ///
    /// If this ever fails, upstream has changed and the saving is worth
    /// revisiting.
    #[test]
    fn a_reload_hands_the_model_set_a_new_expression_engine() {
        let engine = dataflow_rs::Engine::builder().build().expect("builds");
        let next = engine.with_new_workflows(Vec::new()).expect("reloads");
        assert!(
            !Arc::ptr_eq(engine.datalogic(), next.datalogic()),
            "the cheap reload path reused its expression engine, so a model \
             set could now be carried across one"
        );
        assert!(ModelSet::empty().is_empty());
    }

    /// Literal references are found through task groups; computed ones and
    /// other functions are not references.
    #[test]
    fn literal_references_walk_groups_and_skip_computed_ones() {
        let infer = |id: &str, model: Value| {
            json!({"id": id, "name": id, "function": {"name": "model_infer",
                "input": {"model": model, "input": {"var": ""}}}})
        };
        let tasks = json!([
            infer("plain", json!("ada.c4-tiny")),
            {"id": "group", "tasks": [infer("nested", json!("ada.other"))]},
            infer("computed", json!({"var": "data.model"})),
            {"id": "log", "name": "log", "function": {"name": "log",
                "input": {"message": "ada.c4-tiny"}}},
        ]);
        assert_eq!(
            literal_references(&tasks),
            vec![
                ("plain".to_string(), "ada.c4-tiny".to_string()),
                ("nested".to_string(), "ada.other".to_string()),
            ]
        );
        assert!(literal_references(&json!([])).is_empty());
    }
}