vyre-driver 0.6.1

Driver layer: registry, runtime, pipeline, routing, diagnostics. Substrate-agnostic backend machinery. Part of the vyre GPU compiler.
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
//! Runtime TOML dialect loader (A-B5).
//!
//! The Rust path registers ops at link time via `inventory::submit!`.
//! Some consumers  -  DSL authors, CVE-rule contributors, community
//! Nuclei-template-style writers  -  want to drop a TOML file in a
//! directory and have the runtime pick it up without recompiling.
//!
//! This module provides that mechanism for the **metadata** part of
//! a dialect: op id, dialect name, category, signature, laws. The
//! behavioral part (`cpu_ref`, `primary_text`, etc.) still comes from
//! Rust because TOML can't declaratively describe a compute kernel.
//! External dialect crates can thus ship the behavioral half as Rust
//! and the declarative half as TOML  -  the TOML supports community
//! contributions of new rule-like ops whose behavior is composed from
//! existing primitives.
//!
//! Load sequence:
//!
//! 1. `VYRE_DIALECT_PATH` colon-separated directories are scanned for
//!    `*.toml` files.
//! 2. Each file is parsed against the [`DialectManifest`] schema.
//! 3. Manifests are registered into an in-memory [`TomlDialectStore`].
//! 4. Consumers query the store via [`TomlDialectStore::dialect`] and
//!    [`TomlDialectStore::ops_in`].
//!
//! Runtime TOML ops are *additive*  -  they don't override an
//! inventory-registered OpDef with the same id. A conflict is
//! surfaced as a Diagnostic so downstream consumers can disambiguate.

use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::diagnostics::{Diagnostic, DiagnosticCode};

const DIALECT_PATH_ENV: &str = "VYRE_DIALECT_PATH";
const MAX_DIALECT_TOML_BYTES: u64 = 1024 * 1024;

/// Top-level TOML schema for a dialect manifest.
///
/// Every file in `VYRE_DIALECT_PATH` is parsed into one of these.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DialectManifest {
    /// Dialect identifier (e.g. `"community.cve_rules"`).
    pub dialect: String,
    /// Version of this dialect revision (semver string).
    pub version: String,
    /// Optional human-readable note surfaced in diagnostics.
    #[serde(default)]
    pub description: Option<String>,
    /// List of ops.
    #[serde(default)]
    pub ops: Vec<OpManifest>,
}

/// Per-op TOML entry.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpManifest {
    /// Fully qualified op id (`<dialect>.<name>`).
    pub id: String,
    /// Category  -  `"A"`, `"B"`, or `"C"`.
    pub category: String,
    /// Optional free-form summary (one line) surfaced in catalogs.
    #[serde(default)]
    pub summary: Option<String>,
    /// Declarative input list  -  `(name, type)` pairs.
    #[serde(default)]
    pub inputs: Vec<(String, String)>,
    /// Declarative output list  -  `(name, type)` pairs.
    #[serde(default)]
    pub outputs: Vec<(String, String)>,
    /// Algebraic-law tags the op claims to satisfy.
    #[serde(default)]
    pub laws: Vec<String>,
}

/// In-memory store of every TOML-loaded dialect manifest.
#[derive(Debug, Default, Clone)]
pub struct TomlDialectStore {
    manifests: BTreeMap<String, DialectManifest>,
    diagnostics: Vec<Diagnostic>,
}

impl TomlDialectStore {
    /// Build a store by scanning the directories in
    /// `VYRE_DIALECT_PATH`. The env var itself is optional, but every
    /// configured entry must resolve to a readable directory; missing
    /// rule roots are surfaced as diagnostics instead of being treated
    /// as an empty community knowledge database.
    #[must_use]
    pub fn from_env() -> Self {
        let mut store = Self::default();
        if let Some(path) = dialect_path_value() {
            for entry in path.split(':') {
                if entry.is_empty() {
                    store.diagnostics.push(
                        Diagnostic::error(
                            "E-TOML-EMPTY-DIALECT-PATH",
                            format!("{DIALECT_PATH_ENV} contains an empty path entry"),
                        )
                        .with_fix(
                            "remove empty entries or point them at an explicit dialect directory",
                        ),
                    );
                    continue;
                }
                let dir = Path::new(entry);
                if dir.is_dir() {
                    store.scan_dir(dir);
                } else {
                    store.diagnostics.push(
                        Diagnostic::error(
                            "E-TOML-DIALECT-DIR-MISSING",
                            format!(
                                "{DIALECT_PATH_ENV} entry `{}` is not a directory",
                                dir.display()
                            ),
                        )
                        .with_fix("create the directory, fix the path, or unset VYRE_DIALECT_PATH"),
                    );
                }
            }
        }
        store
    }

    /// Scan one directory for `*.toml` manifests. Invalid manifests
    /// surface as [`Diagnostic`]s attached to the store; the scan
    /// never short-circuits.
    pub fn scan_dir(&mut self, dir: &Path) {
        let Ok(entries) = fs::read_dir(dir) else {
            self.diagnostics.push(
                Diagnostic::error(
                    "E-TOML-DIALECT-DIR-UNREADABLE",
                    format!("TOML dialect directory `{}` is unreadable", dir.display()),
                )
                .with_fix(
                    "fix directory permissions or remove the directory from VYRE_DIALECT_PATH",
                ),
            );
            return;
        };
        for entry in entries {
            let path = match entry {
                Ok(entry) => entry.path(),
                Err(error) => {
                    self.diagnostics.push(
                        Diagnostic::error(
                            "E-TOML-DIALECT-DIR-ENTRY",
                            format!(
                                "failed to read an entry from TOML dialect directory `{}`: {error}",
                                dir.display()
                            ),
                        )
                        .with_fix("fix directory permissions or remove the unreadable entry"),
                    );
                    continue;
                }
            };
            if path.extension().and_then(|s| s.to_str()) != Some("toml") {
                continue;
            }
            self.load_file(&path);
        }
    }

    /// Load a single TOML file. Errors become diagnostics; the
    /// function never panics.
    pub fn load_file(&mut self, path: &Path) {
        let Ok(contents) = read_toml_bounded(path) else {
            self.diagnostics.push(
                Diagnostic::error(
                    "E-TOML-UNREADABLE",
                    format!("TOML dialect file `{}` is unreadable", path.display()),
                )
                .with_fix("confirm file permissions and that VYRE_DIALECT_PATH points at an intended directory"),
            );
            return;
        };
        match toml::from_str::<DialectManifest>(&contents) {
            Ok(mut manifest) => {
                if manifest.dialect.trim().is_empty() {
                    self.diagnostics.push(
                        Diagnostic::error(
                            "E-TOML-EMPTY-DIALECT",
                            format!(
                                "TOML dialect file `{}` has an empty dialect id",
                                path.display()
                            ),
                        )
                        .with_fix("set `dialect` to a stable non-empty identifier"),
                    );
                    return;
                }
                if manifest.version.trim().is_empty() {
                    self.diagnostics.push(
                        Diagnostic::error(
                            "E-TOML-EMPTY-VERSION",
                            format!(
                                "dialect manifest `{}` has an empty version",
                                manifest.dialect
                            ),
                        )
                        .with_fix("set `version` to a stable non-empty version string"),
                    );
                    return;
                }
                // Sanity pass: every op id must begin with the
                // dialect prefix. Reject the whole manifest on any
                // invalid op id so a partial community rule database
                // cannot load with missing operations.
                let mut invalid_manifest = false;
                let mut seen_ops = BTreeSet::new();
                manifest.ops.retain(|op| {
                    let mut keep = true;
                    if !op.id.starts_with(&format!("{}.", manifest.dialect)) {
                        invalid_manifest = true;
                        self.diagnostics.push(
                            Diagnostic::error(
                                "E-TOML-BAD-OP-ID",
                                format!(
                                    "op id `{}` does not start with dialect prefix `{}.`",
                                    op.id, manifest.dialect
                                ),
                            )
                            .with_fix("rename the op to `<dialect>.<name>`"),
                        );
                        keep = false;
                    }
                    if !matches!(op.category.as_str(), "A" | "B" | "C") {
                        invalid_manifest = true;
                        self.diagnostics.push(
                            Diagnostic::error(
                                "E-TOML-BAD-CATEGORY",
                                format!("op `{}` has invalid category `{}`", op.id, op.category),
                            )
                            .with_fix("set category to `A`, `B`, or `C`"),
                        );
                        keep = false;
                    }
                    if keep && !seen_ops.insert(op.id.clone()) {
                        invalid_manifest = true;
                        self.diagnostics.push(
                            Diagnostic::error(
                                "E-TOML-DUPLICATE-OP",
                                format!(
                                    "dialect manifest `{}` declares op `{}` more than once",
                                    manifest.dialect, op.id
                                ),
                            )
                            .with_fix("keep one declaration per stable op id"),
                        );
                        keep = false;
                    }
                    keep
                });
                if invalid_manifest {
                    self.diagnostics.push(
                        Diagnostic::error(
                            "E-TOML-MANIFEST-REJECTED",
                            format!(
                                "dialect manifest `{}` was rejected because one or more op declarations are invalid",
                                manifest.dialect
                            ),
                        )
                        .with_fix("fix every op declaration in the manifest before loading this dialect"),
                    );
                    return;
                }
                // Keep the highest-versioned manifest per dialect, but only
                // after validating this file. Losing version selection must
                // not hide malformed community metadata.
                if let Some(existing) = self.manifests.get(&manifest.dialect) {
                    if existing.version >= manifest.version {
                        self.diagnostics.push(Diagnostic::note(
                            "N-TOML-DIALECT-SHADOWED",
                            format!(
                                "dialect `{}` has multiple manifests; keeping version {} over {}",
                                manifest.dialect, existing.version, manifest.version
                            ),
                        ));
                        return;
                    }
                    self.diagnostics.push(Diagnostic::note(
                        "N-TOML-DIALECT-SHADOWED",
                        format!(
                            "dialect `{}` has multiple manifests; keeping version {} over {}",
                            manifest.dialect, manifest.version, existing.version
                        ),
                    ));
                }
                self.manifests.insert(manifest.dialect.clone(), manifest);
            }
            Err(err) => {
                self.diagnostics.push(
                    Diagnostic::error(
                        "E-TOML-PARSE",
                        format!("TOML dialect file `{}` is malformed: {err}", path.display()),
                    )
                    .with_fix("validate the file against the DialectManifest schema"),
                );
            }
        }
    }

    /// Look up one dialect manifest by name.
    #[must_use]
    pub fn dialect(&self, id: &str) -> Option<&DialectManifest> {
        self.manifests.get(id)
    }

    /// List every op declared in the given dialect.
    #[must_use]
    pub fn ops_in(&self, dialect: &str) -> &[OpManifest] {
        self.manifests
            .get(dialect)
            .map(|m| m.ops.as_slice())
            .unwrap_or(&[])
    }

    /// Return every loaded dialect manifest.
    #[must_use]
    pub fn manifests(&self) -> Vec<&DialectManifest> {
        let mut manifests = Vec::new();
        vyre_foundation::allocation::try_reserve_vec_to_capacity(
            &mut manifests,
            self.manifests.len(),
        )
        .unwrap_or_else(|error| {
                panic!(
                    "Vyre TOML registry could not reserve {} manifest reference slot(s): {error}. Fix: split manifest loading into pages or reduce loaded dialect files.",
                    self.manifests.len()
                )
            });
        manifests.extend(self.manifests.values());
        manifests
    }

    /// Diagnostics accumulated during load.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Convenience: check whether a given op id is declared by any
    /// loaded TOML manifest.
    #[must_use]
    pub fn contains_op(&self, op_id: &str) -> bool {
        self.manifests
            .values()
            .any(|m| m.ops.iter().any(|op| op.id == op_id))
    }
}

fn dialect_path_value() -> Option<String> {
    #[cfg(test)]
    {
        if let Some(path) = dialect_path_override()
            .lock()
            .ok()
            .and_then(|guard| guard.clone())
        {
            return Some(path);
        }
    }
    std::env::var(DIALECT_PATH_ENV).ok()
}

#[cfg(test)]
fn dialect_path_override() -> &'static std::sync::Mutex<Option<String>> {
    static OVERRIDE: std::sync::OnceLock<std::sync::Mutex<Option<String>>> =
        std::sync::OnceLock::new();
    OVERRIDE.get_or_init(|| std::sync::Mutex::new(None))
}

fn read_toml_bounded(path: &Path) -> std::io::Result<String> {
    use std::io::Read as _;

    let mut file = fs::File::open(path)?;
    let metadata = file.metadata()?;
    if metadata.len() > MAX_DIALECT_TOML_BYTES {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("dialect TOML exceeds {MAX_DIALECT_TOML_BYTES} byte limit"),
        ));
    }
    let mut text = String::with_capacity(metadata.len() as usize);
    file.by_ref()
        .take(MAX_DIALECT_TOML_BYTES + 1)
        .read_to_string(&mut text)?;
    if text.len() as u64 > MAX_DIALECT_TOML_BYTES {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "dialect TOML exceeded bounded read limit",
        ));
    }
    Ok(text)
}

/// Stable diagnostic code family for TOML loader issues. Tooling
/// hangs rules off these codes; do not rename.
pub const CODE_PARSE: DiagnosticCode = DiagnosticCode(std::borrow::Cow::Borrowed("E-TOML-PARSE"));

/// Compute an absolute path relative to the workspace root. Handy
/// for tests that stage TOML fixtures under `tests/fixtures`.
#[must_use]
pub fn workspace_dialect_fixture_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("dialect")
}

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

    fn write_tmp(contents: &str) -> tempfile::NamedTempFile {
        // Many tests need a .toml file on disk; NamedTempFile
        // exposes its path and cleans up on drop.
        let mut file = tempfile::Builder::new()
            .suffix(".toml")
            .tempfile()
            .expect("Fix: tmp file");
        file.write_all(contents.as_bytes()).expect("Fix: write");
        file.flush().expect("Fix: flush");
        file
    }

    #[test]
    fn parses_minimal_dialect() {
        let file = write_tmp(
            r#"
dialect = "community.test"
version = "1.0.0"
ops = [
  { id = "community.test.pass", category = "A", summary = "no-op" },
]
"#,
        );
        let mut store = TomlDialectStore::default();
        store.load_file(file.path());
        assert!(store.dialect("community.test").is_some());
        assert_eq!(store.ops_in("community.test").len(), 1);
        assert!(store.contains_op("community.test.pass"));
        assert_eq!(store.diagnostics().len(), 0);
    }


    #[test]
    fn rejects_mismatched_op_prefix_with_diagnostic() {
        let file = write_tmp(
            r#"
dialect = "community.test"
version = "1.0.0"
ops = [
  { id = "other.not_my_dialect", category = "A" },
]
"#,
        );
        let mut store = TomlDialectStore::default();
        store.load_file(file.path());
        assert_eq!(store.ops_in("community.test").len(), 0);
        assert!(store
            .diagnostics()
            .iter()
            .any(|d| d.code.as_str() == "E-TOML-BAD-OP-ID"));
    }

    #[test]
    fn malformed_toml_produces_parse_error_diagnostic() {
        let file = write_tmp("not-toml-at-all =");
        let mut store = TomlDialectStore::default();
        store.load_file(file.path());
        assert!(store
            .diagnostics()
            .iter()
            .any(|d| d.code.as_str() == "E-TOML-PARSE"));
    }

    #[test]
    fn invalid_op_category_rejects_manifest() {
        let file = write_tmp(
            r#"
dialect = "community.bad_category"
version = "1.0.0"
ops = [ { id = "community.bad_category.scan", category = "Z" } ]
"#,
        );
        let mut store = TomlDialectStore::default();
        store.load_file(file.path());
        assert!(store.dialect("community.bad_category").is_none());
        assert!(store
            .diagnostics()
            .iter()
            .any(|d| d.code.as_str() == "E-TOML-BAD-CATEGORY"));
        assert!(store
            .diagnostics()
            .iter()
            .any(|d| d.code.as_str() == "E-TOML-MANIFEST-REJECTED"));
    }

    #[test]
    fn duplicate_manifest_op_rejects_manifest() {
        let file = write_tmp(
            r#"
dialect = "community.duplicate"
version = "1.0.0"
ops = [
  { id = "community.duplicate.scan", category = "B" },
  { id = "community.duplicate.scan", category = "B" },
]
"#,
        );
        let mut store = TomlDialectStore::default();
        store.load_file(file.path());
        assert!(store.dialect("community.duplicate").is_none());
        assert!(store
            .diagnostics()
            .iter()
            .any(|d| d.code.as_str() == "E-TOML-DUPLICATE-OP"));
    }

    #[test]
    fn shadowed_manifest_keeps_highest_version() {
        let older = write_tmp(
            r#"
dialect = "community.versioned"
version = "1.0.0"
ops = []
"#,
        );
        let newer = write_tmp(
            r#"
dialect = "community.versioned"
version = "2.0.0"
ops = [ { id = "community.versioned.new", category = "B" } ]
"#,
        );
        let mut store = TomlDialectStore::default();
        store.load_file(older.path());
        store.load_file(newer.path());
        assert_eq!(
            store.dialect("community.versioned").unwrap().version,
            "2.0.0"
        );
        assert_eq!(store.ops_in("community.versioned").len(), 1);
    }

    #[test]
    fn shadowed_manifest_is_still_validated() {
        let newer = write_tmp(
            r#"
dialect = "community.versioned_invalid"
version = "2.0.0"
ops = [ { id = "community.versioned_invalid.good", category = "B" } ]
"#,
        );
        let older_invalid = write_tmp(
            r#"
dialect = "community.versioned_invalid"
version = "1.0.0"
ops = [ { id = "wrong.bad", category = "B" } ]
"#,
        );
        let mut store = TomlDialectStore::default();
        store.load_file(newer.path());
        store.load_file(older_invalid.path());
        assert_eq!(store.ops_in("community.versioned_invalid").len(), 1);
        assert!(store
            .diagnostics()
            .iter()
            .any(|d| d.code.as_str() == "E-TOML-BAD-OP-ID"));
    }

    #[test]
    fn env_scan_reports_missing_directories() {
        // VYRE_DIALECT_PATH points at directories that do not exist.
        // Missing Tier-B rule roots are configuration errors: they
        // must not be silently mistaken for an empty knowledge base.
        *dialect_path_override()
            .lock()
            .expect("Fix: dialect path test override lock must not be poisoned") =
            Some("/no/such/dir:/also/not/real".to_string());
        let store = TomlDialectStore::from_env();
        assert!(store.manifests.is_empty());
        assert_eq!(
            store
                .diagnostics
                .iter()
                .filter(|d| d.code.as_str() == "E-TOML-DIALECT-DIR-MISSING")
                .count(),
            2
        );
        *dialect_path_override()
            .lock()
            .expect("Fix: dialect path test override lock must not be poisoned") = None;
    }

    #[test]
    fn code_constants_are_stable() {
        assert_eq!(CODE_PARSE.as_str(), "E-TOML-PARSE");
    }
}