silicera-runtime 0.2.0

Lightweight HNEP load and cheap dispatch for Silicera-specialized binaries
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
//! # silicera-runtime
//!
//! Lightweight HNEP load and cheap decision-tree dispatch.
//!
//! This crate intentionally does **not** depend on `silicera-lab`. Specialized
//! binaries embed or load a profile and select variants without re-running
//! tournaments.
//!
//! On fingerprint mismatch the runtime reports `PROFILE MISMATCH` and falls
//! back to the baseline variant unless `--strict-machine` / strict API is used.

#![warn(missing_docs)]
#![deny(unsafe_op_in_unsafe_fn)]

use std::path::Path;

use serde::{Deserialize, Serialize};
use silicera::brand::{BrandInfo, NAME};
use silicera::fingerprint::Fingerprint;
use silicera::hardware::{detect_hardware, HardwareInfo};
use silicera::hnep::HnepProfile;
use silicera::specialize::DecisionTree;
use silicera::{Result, SiliceraError};

#[cfg(feature = "c-abi")]
pub mod ffi;

/// Embedder-facing about / identity block (version, phase, brand, funding).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuntimeInfo {
    /// Product name.
    pub name: String,
    /// Runtime crate version (same workspace semver).
    pub version: String,
    /// Phase marker.
    pub phase: String,
    /// Brand line.
    pub brand_line: String,
    /// SPDX license.
    pub license: String,
    /// Homepage.
    pub homepage: String,
    /// Repository URL.
    pub repository: String,
    /// Funding / thanks.dev URL.
    pub funding_url: String,
    /// Affiliation disclaimer.
    pub affiliation: String,
}

impl RuntimeInfo {
    /// Capture current runtime identity from the shared brand module.
    pub fn current() -> Self {
        let b = BrandInfo::current();
        Self {
            name: b.name.into(),
            version: b.version.into(),
            phase: b.phase.into(),
            brand_line: b.brand_line.into(),
            license: b.license.into(),
            homepage: b.homepage.into(),
            repository: b.repository.into(),
            funding_url: b.funding_url.into(),
            affiliation: b.affiliation.into(),
        }
    }

    /// Pretty JSON for tooling.
    pub fn to_json_pretty(&self) -> Result<String> {
        Ok(serde_json::to_string_pretty(self)?)
    }
}

/// Convenience: brand/version/phase for embedders.
pub fn runtime_about() -> RuntimeInfo {
    RuntimeInfo::current()
}

/// Runtime policy when the loaded profile does not match the host.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MismatchPolicy {
    /// Print/return mismatch and use baseline fallback.
    FallbackBaseline,
    /// Hard error — refuse to dispatch specialized variants.
    StrictMachine,
}

impl Default for MismatchPolicy {
    fn default() -> Self {
        Self::FallbackBaseline
    }
}

/// Outcome of loading a profile against the current machine.
#[derive(Debug, Clone)]
pub struct LoadedProfile {
    /// Parsed HNEP.
    pub profile: HnepProfile,
    /// Host hardware at load time.
    pub host: HardwareInfo,
    /// Whether fingerprint matched exactly.
    pub exact_match: bool,
    /// Whether silicon class matched (vendor/microarch/family/model).
    pub class_match: bool,
    /// Active mismatch policy.
    pub policy: MismatchPolicy,
    /// True if dispatch should use baseline only.
    pub force_baseline: bool,
    /// Operator-facing status line.
    pub status: String,
}

impl LoadedProfile {
    /// Load from path and compare to live hardware.
    pub fn load(path: &Path, policy: MismatchPolicy) -> Result<Self> {
        let profile = HnepProfile::read_from(path)?;
        let host = detect_hardware()?;
        Self::from_parts(profile, host, policy)
    }

    /// Construct from already-loaded pieces (tests / inject mock host).
    pub fn from_parts(
        profile: HnepProfile,
        host: HardwareInfo,
        policy: MismatchPolicy,
    ) -> Result<Self> {
        // Callers can construct profiles directly; do not trust their digest.
        let profile = HnepProfile::parse_str(&serde_json::to_string(&profile)?)?;
        let host_fp = match &host.fingerprint {
            Some(fp) => fp.clone(),
            None => {
                if policy == MismatchPolicy::StrictMachine {
                    return Err(SiliceraError::ProfileMismatch(format!(
                        "strict-machine ({NAME}): host unsupported ({})",
                        host.support.message()
                    )));
                }
                return Ok(Self {
                    status: format!(
                        "PROFILE MISMATCH ({NAME}): host unsupported ({})",
                        host.support.message()
                    ),
                    profile,
                    host,
                    exact_match: false,
                    class_match: false,
                    policy,
                    force_baseline: true,
                });
            }
        };

        let profile_fp = Fingerprint::parse(&profile.header.fingerprint)?;
        let exact_match = host_fp.exact_match(&profile_fp);
        let class_match = host_fp.same_silicon_class(&profile_fp);

        let (force_baseline, status) = if exact_match {
            (false, format!("profile match: exact fingerprint ({NAME})"))
        } else if class_match {
            match policy {
                MismatchPolicy::FallbackBaseline => (
                    true,
                    format!(
                        "PROFILE MISMATCH ({NAME}): topology/cache hash differs (class OK); falling back to baseline\n  profile: {}\n  host:    {}",
                        profile_fp, host_fp
                    ),
                ),
                MismatchPolicy::StrictMachine => {
                    return Err(SiliceraError::ProfileMismatch(format!(
                        "strict-machine ({NAME}): fingerprint mismatch\n  profile: {profile_fp}\n  host:    {host_fp}"
                    )));
                }
            }
        } else {
            match policy {
                MismatchPolicy::FallbackBaseline => (
                    true,
                    format!(
                        "PROFILE MISMATCH ({NAME}): silicon class differs; falling back to baseline\n  profile: {}\n  host:    {}",
                        profile_fp, host_fp
                    ),
                ),
                MismatchPolicy::StrictMachine => {
                    return Err(SiliceraError::ProfileMismatch(format!(
                        "strict-machine ({NAME}): silicon class mismatch\n  profile: {profile_fp}\n  host:    {host_fp}"
                    )));
                }
            }
        };

        Ok(Self {
            profile,
            host,
            exact_match,
            class_match,
            policy,
            force_baseline,
            status,
        })
    }

    /// Select variant for a workload by name (or baseline if forced).
    pub fn select_workload(&self, workload: &str) -> &str {
        if self.force_baseline {
            return "baseline";
        }
        self.profile
            .workloads
            .iter()
            .find(|w| w.name == workload)
            .map(|w| w.winner.as_str())
            .unwrap_or("baseline")
    }

    /// Evaluate size-dependent tree, or baseline on mismatch / missing tree.
    pub fn select_size(&self, size_bytes: u64) -> &str {
        if self.force_baseline {
            return "baseline";
        }
        match &self.profile.decision_tree {
            Some(tree) => tree.evaluate(size_bytes),
            None => "baseline",
        }
    }

    /// Access decision tree when present.
    pub fn decision_tree(&self) -> Option<&DecisionTree> {
        self.profile.decision_tree.as_ref()
    }

    /// Select a workload only when its winner is supported by the application
    /// and meets its confidence floor. Always provide a real baseline handler.
    /// The available IDs must already be filtered for the host's ISA support.
    pub fn guarded_workload<'a>(
        &'a self,
        workload: &str,
        available: &[&str],
        minimum: silicera::hnep::Confidence,
    ) -> DispatchDecision<'a> {
        if self.force_baseline {
            return DispatchDecision::baseline(DispatchReason::MachineMismatch);
        }
        let Some(entry) = self.profile.workloads.iter().find(|w| w.name == workload) else {
            return DispatchDecision::baseline(DispatchReason::MissingWorkload);
        };
        if entry.winner == "baseline" {
            return DispatchDecision::baseline(DispatchReason::ProfileBaseline);
        }
        if entry.confidence.rank() < minimum.rank() {
            return DispatchDecision::baseline(DispatchReason::InsufficientConfidence);
        }
        if !available.contains(&entry.winner.as_str()) {
            return DispatchDecision::baseline(DispatchReason::UnavailableVariant);
        }
        DispatchDecision {
            variant: &entry.winner,
            reason: DispatchReason::Selected,
        }
    }

    /// Guard size dispatch against variant IDs absent from the host-compatible
    /// application registry. This does not infer confidence from tree leaves.
    pub fn guarded_size<'a>(&'a self, bytes: u64, available: &[&str]) -> DispatchDecision<'a> {
        if self.force_baseline {
            return DispatchDecision::baseline(DispatchReason::MachineMismatch);
        }
        let Some(tree) = &self.profile.decision_tree else {
            return DispatchDecision::baseline(DispatchReason::MissingTree);
        };
        let variant = tree.evaluate(bytes);
        if variant == "baseline" {
            return DispatchDecision::baseline(DispatchReason::ProfileBaseline);
        }
        if !available.contains(&variant) {
            return DispatchDecision::baseline(DispatchReason::UnavailableVariant);
        }
        DispatchDecision {
            variant,
            reason: DispatchReason::Selected,
        }
    }
}

/// Stable, machine-readable explanation of a guarded dispatch decision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DispatchReason {
    /// A supported specialized implementation was selected.
    Selected,
    /// The profile explicitly selected baseline.
    ProfileBaseline,
    /// The machine does not match the profile.
    MachineMismatch,
    /// The workload is absent from the profile.
    MissingWorkload,
    /// No size decision tree was recorded.
    MissingTree,
    /// The workload confidence is below the requested floor.
    InsufficientConfidence,
    /// The application cannot execute this variant on this host.
    UnavailableVariant,
}

/// Allocation-free selection result for applications with a variant registry.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct DispatchDecision<'a> {
    /// Variant to execute; `baseline` must always be implemented by the caller.
    pub variant: &'a str,
    /// Why this variant was selected.
    pub reason: DispatchReason,
}

impl DispatchDecision<'_> {
    fn baseline(reason: DispatchReason) -> Self {
        Self {
            variant: "baseline",
            reason,
        }
    }
}

/// Convenience: dispatch API used by specialized binaries.
pub struct Dispatcher {
    loaded: LoadedProfile,
}

impl Dispatcher {
    /// Load profile with policy.
    pub fn open(path: &Path, policy: MismatchPolicy) -> Result<Self> {
        Ok(Self {
            loaded: LoadedProfile::load(path, policy)?,
        })
    }

    /// Status line (match / mismatch).
    pub fn status(&self) -> &str {
        &self.loaded.status
    }

    /// Whether baseline is forced.
    pub fn using_baseline(&self) -> bool {
        self.loaded.force_baseline
    }

    /// Select by workload name.
    pub fn workload(&self, name: &str) -> &str {
        self.loaded.select_workload(name)
    }

    /// Select by working-set size.
    pub fn size(&self, bytes: u64) -> &str {
        self.loaded.select_size(bytes)
    }

    /// Borrow loaded profile.
    pub fn loaded(&self) -> &LoadedProfile {
        &self.loaded
    }

    /// Brand / version about block for embedders.
    pub fn about(&self) -> RuntimeInfo {
        runtime_about()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use silicera::brand::{
        AFFILIATION_DISCLAIMER, BRAND_LINE, FUNDING_URL, HOMEPAGE, LICENSE, PHASE, REPOSITORY,
        VERSION,
    };
    use silicera::hardware::{EnvironmentSnapshot, MockHardware, SupportStatus};
    use silicera::hnep::{Confidence, HnepHeader, HnepProfile, IntegrityDigest, WorkloadEntry};
    use silicera::knowledge::{KnowledgePack, Microarch};
    use silicera::HardwareBackend;

    fn make_profile(fp: &str) -> HnepProfile {
        let header = HnepHeader {
            format: silicera::hnep::HNEP_FORMAT.into(),
            version: silicera::hnep::HNEP_VERSION,
            silicera_version: silicera::VERSION.into(),
            created_at: chrono::Utc::now().to_rfc3339(),
            fingerprint: fp.into(),
            label: "test".into(),
        };
        let environment = EnvironmentSnapshot::capture();
        let workloads = vec![WorkloadEntry {
            name: "demo".into(),
            winner: "fast".into(),
            confidence: Confidence::Medium,
            rationale: "test".into(),
            winner_median_ns: Some(90.0),
            baseline_median_ns: Some(100.0),
        }];
        let size_classes: Vec<silicera::SizeClassEntry> = Vec::new();
        let profile = HnepProfile {
            header: header.clone(),
            environment: environment.clone(),
            workloads: workloads.clone(),
            size_classes: size_classes.clone(),
            decision_tree: None,
            digest: IntegrityDigest {
                alg: "sha256".into(),
                hex: String::new(),
            },
        };
        #[derive(serde::Serialize)]
        struct Payload {
            header: HnepHeader,
            environment: EnvironmentSnapshot,
            workloads: Vec<WorkloadEntry>,
            size_classes: Vec<silicera::SizeClassEntry>,
            decision_tree: Option<silicera::specialize::DecisionTree>,
        }
        let p = Payload {
            header: profile.header.clone(),
            environment: profile.environment.clone(),
            workloads: profile.workloads.clone(),
            size_classes: profile.size_classes.clone(),
            decision_tree: None,
        };
        let bytes = serde_json::to_vec(&p).unwrap();
        let digest = IntegrityDigest::sha256(&bytes);
        HnepProfile { digest, ..profile }
    }

    #[test]
    fn mismatch_falls_back() {
        let kp = KnowledgePack::builtin();
        let host = MockHardware::zen5_dual_ccd().discover(&kp).unwrap();
        let fp = host.fingerprint.as_ref().unwrap().value.clone();
        let profile = make_profile(&fp);
        let loaded =
            LoadedProfile::from_parts(profile, host, MismatchPolicy::FallbackBaseline).unwrap();
        assert!(!loaded.force_baseline);
        assert_eq!(loaded.select_workload("demo"), "fast");
    }

    #[test]
    fn wrong_machine_baseline() {
        let kp = KnowledgePack::builtin();
        let host = MockHardware::zen5_dual_ccd().discover(&kp).unwrap();
        let mut profile = make_profile("SLC:AMD:ZEN4:19:61:00:deadbeefdeadbeef:cafebabecafebabe");
        // Fix digest for wrong fp profile
        #[derive(serde::Serialize)]
        struct Payload {
            header: HnepHeader,
            environment: EnvironmentSnapshot,
            workloads: Vec<WorkloadEntry>,
            size_classes: Vec<silicera::SizeClassEntry>,
            decision_tree: Option<silicera::specialize::DecisionTree>,
        }
        let p = Payload {
            header: profile.header.clone(),
            environment: profile.environment.clone(),
            workloads: profile.workloads.clone(),
            size_classes: profile.size_classes.clone(),
            decision_tree: None,
        };
        profile.digest = IntegrityDigest::sha256(&serde_json::to_vec(&p).unwrap());
        let loaded =
            LoadedProfile::from_parts(profile, host, MismatchPolicy::FallbackBaseline).unwrap();
        assert!(loaded.force_baseline);
        assert_eq!(loaded.select_workload("demo"), "baseline");
        assert!(loaded.status.contains("PROFILE MISMATCH"));
        assert!(loaded.status.contains(NAME));
        let _ = SupportStatus::Unsupported { reason: "x".into() };
        let _ = Microarch::Zen5;
    }

    #[test]
    fn runtime_about_has_funding() {
        let about = runtime_about();
        assert_eq!(about.name, NAME);
        assert_eq!(about.phase, PHASE);
        assert_eq!(about.version, VERSION);
        assert!(about.funding_url.contains("thanks.dev/u/gh/theworker02"));
        assert_eq!(about.brand_line, BRAND_LINE);
        assert_eq!(about.license, LICENSE);
        assert_eq!(about.homepage, HOMEPAGE);
        assert_eq!(about.repository, REPOSITORY);
        assert_eq!(about.funding_url, FUNDING_URL);
        assert_eq!(about.affiliation, AFFILIATION_DISCLAIMER);
    }

    #[test]
    fn unsupported_host_obeys_strict_policy() {
        for policy in [
            MismatchPolicy::FallbackBaseline,
            MismatchPolicy::StrictMachine,
        ] {
            let host = MockHardware::unsupported_intel()
                .discover(&KnowledgePack::builtin())
                .unwrap();
            let profile = make_profile("SLC:AMD:ZEN4:19:61:00:deadbeefdeadbeef:cafebabecafebabe");
            let result = LoadedProfile::from_parts(profile, host, policy);
            if policy == MismatchPolicy::StrictMachine {
                assert!(matches!(result, Err(SiliceraError::ProfileMismatch(_))));
            } else {
                let loaded = result.unwrap();
                assert_eq!(
                    loaded
                        .guarded_workload("demo", &["fast"], Confidence::Medium)
                        .reason,
                    DispatchReason::MachineMismatch
                );
                assert_eq!(loaded.select_size(100), "baseline");
            }
        }
    }

    #[test]
    fn direct_load_rejects_tampering_and_unknown_schema() {
        let host = MockHardware::zen5_dual_ccd()
            .discover(&KnowledgePack::builtin())
            .unwrap();
        let mut profile = make_profile(&host.fingerprint.as_ref().unwrap().value);
        profile.workloads[0].winner = "tampered".into();
        assert!(LoadedProfile::from_parts(
            profile.clone(),
            host.clone(),
            MismatchPolicy::FallbackBaseline
        )
        .is_err());
        profile.header.version = 999;
        profile.recompute_digest().unwrap();
        assert!(
            LoadedProfile::from_parts(profile, host, MismatchPolicy::FallbackBaseline).is_err()
        );
    }

    #[test]
    fn guarded_dispatch_checks_registry_confidence_and_boundaries() {
        let host = MockHardware::zen5_dual_ccd()
            .discover(&KnowledgePack::builtin())
            .unwrap();
        let mut profile = make_profile(&host.fingerprint.as_ref().unwrap().value);
        profile.decision_tree = Some(DecisionTree::from_thresholds(
            10, 20, 30, "fast", "missing", "baseline", "fast", "baseline",
        ));
        profile.recompute_digest().unwrap();
        let loaded =
            LoadedProfile::from_parts(profile, host, MismatchPolicy::StrictMachine).unwrap();
        assert_eq!(
            loaded
                .guarded_workload("demo", &["fast"], Confidence::Medium)
                .variant,
            "fast"
        );
        assert_eq!(
            loaded
                .guarded_workload("demo", &["fast"], Confidence::High)
                .reason,
            DispatchReason::InsufficientConfidence
        );
        assert_eq!(
            loaded.guarded_workload("demo", &[], Confidence::Low).reason,
            DispatchReason::UnavailableVariant
        );
        assert_eq!(
            loaded
                .guarded_workload("unknown", &["fast"], Confidence::Low)
                .reason,
            DispatchReason::MissingWorkload
        );
        assert_eq!(loaded.guarded_size(9, &["fast"]).variant, "fast");
        assert_eq!(
            loaded.guarded_size(10, &["fast"]).reason,
            DispatchReason::UnavailableVariant
        );
        assert_eq!(
            loaded.guarded_size(20, &["fast"]).reason,
            DispatchReason::ProfileBaseline
        );
        assert_eq!(loaded.guarded_size(u64::MAX, &["fast"]).variant, "fast");
    }
}