vyre-self-substrate 0.6.1

Vyre self-substrate: vyre using its own primitives on its own scheduler problems. The recursion-thesis layer between vyre-primitives and vyre-driver.
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
//! Typed optimization registry for platform and consumer release work.

use std::collections::{BTreeMap, BTreeSet};

/// Discoverable optimization pass contract.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OptimizationPass {
    /// Stable pass identifier.
    pub id: &'static str,
    /// Owning subsystem.
    pub owner: &'static str,
    /// Pipeline phase.
    pub phase: &'static str,
    /// Correctness invariant preserved by the pass.
    pub invariant: &'static str,
    /// Benchmark or test gate proving the pass.
    pub benchmark: &'static str,
}

/// Stable explanation emitted when an optimization pass fires.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OptimizationPassExplanation<'a> {
    /// Format version for machine consumers.
    pub format: &'static str,
    /// Pass metadata.
    pub pass: OptimizationPass,
    /// Runtime reason the pass fired.
    pub reason: &'a str,
    /// Expected effect of the pass on this input.
    pub expected_effect: &'a str,
}

impl OptimizationPassExplanation<'_> {
    /// Stable single-line record for logs and release artifacts.
    #[must_use]
    pub fn stable_record(&self) -> String {
        let mut record = String::new();
        self.stable_record_into(&mut record);
        record
    }

    /// Write the stable single-line record into caller-owned storage.
    pub fn stable_record_into(&self, out: &mut String) {
        use std::fmt::Write as _;

        out.clear();
        let _ = write!(
            out,
            "{}|pass={}|owner={}|phase={}|invariant={}|benchmark={}|reason={}|effect={}",
            self.format,
            self.pass.id,
            self.pass.owner,
            self.pass.phase,
            self.pass.invariant,
            self.pass.benchmark,
            self.reason,
            self.expected_effect
        );
    }
}

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

    #[test]
    fn registry_accepts_domain_ordered_release_pipeline() {
        let registry = OptimizationRegistry::with_release_builtins();

        registry
            .validate_pass_order([
                "vyrec.gpu-token-classify",
                "vyrec.parser-recovery",
                "vyrec.semantic-decls",
                "dataflow.graph-layout-hash",
                "dataflow.fixed-point-resident-graph",
                "dataflow.ifds-repeated-sequence",
                "cuda.module-cache",
                "cuda.pinned-readback-pool",
                "cuda.megakernel-memory-budget",
                "cuda.megakernel-barrier-min",
            ])
            .expect("Fix: domain-ordered release pipeline should validate");
    }

    #[test]
    fn registry_rejects_frontend_semantics_before_preprocessing() {
        let registry = OptimizationRegistry::with_release_builtins();
        let err = registry
            .validate_pass_order(["vyrec.semantic-decls", "vyrec.gpu-token-classify"])
            .expect_err("semantic analysis cannot precede preprocessing");

        assert!(err.contains("frontend phase order"), "{err}");
        assert!(err.contains("vyrec.gpu-token-classify"), "{err}");
    }

    #[test]
    fn registry_rejects_dataflow_solve_before_graph_layout() {
        let registry = OptimizationRegistry::with_release_builtins();
        let err = registry
            .validate_pass_order([
                "dataflow.ifds-repeated-sequence",
                "dataflow.graph-layout-hash",
            ])
            .expect_err("IFDS solve cannot precede graph layout");

        assert!(err.contains("dataflow phase order"), "{err}");
        assert!(err.contains("dataflow.graph-layout-hash"), "{err}");
    }

    #[test]
    fn registry_rejects_unknown_and_duplicate_passes() {
        let registry = OptimizationRegistry::with_release_builtins();
        let unknown = registry
            .validate_pass_order(["cuda.module-cache", "cuda.not-registered"])
            .expect_err("unknown pass should not schedule");
        assert!(unknown.contains("unknown optimization pass"), "{unknown}");

        let duplicate = registry
            .validate_pass_order(["cuda.module-cache", "cuda.module-cache"])
            .expect_err("duplicate pass should require an explicit wrapper");
        assert!(duplicate.contains("appears more than once"), "{duplicate}");
    }
}

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

    #[test]
    fn registry_emits_stable_pass_fire_explanation() {
        let registry = OptimizationRegistry::with_release_builtins();
        let explanation = registry
            .explain_pass_fire(
                "cuda.megakernel-memory-budget",
                "frontier telemetry predicts peak scratch above dense threshold",
                "select bounded plan before launch allocation",
            )
            .expect("Fix: registered pass should explain");

        assert_eq!(explanation.format, "vyre-optimization-explanation-v1");
        assert_eq!(explanation.pass.owner, "vyre-cuda");
        assert!(explanation
            .stable_record()
            .contains("pass=cuda.megakernel-memory-budget"));
        assert!(explanation
            .stable_record()
            .contains("invariant=peak bytes are bounded before launch"));
    }

    #[test]
    fn pass_explanation_reuses_caller_owned_record_storage() {
        let registry = OptimizationRegistry::with_release_builtins();
        let explanation = registry
            .explain_pass_fire(
                "cuda.kernel-failure-diagnostics",
                "capability gate rejected selected launch",
                "emit actionable missing capability list",
            )
            .expect("Fix: registered CUDA diagnostic pass should explain");
        let mut record = String::with_capacity(512);
        let ptr = record.as_ptr();

        explanation.stable_record_into(&mut record);

        assert_eq!(record.as_ptr(), ptr);
        assert!(record.contains("pass=cuda.kernel-failure-diagnostics"));
        assert!(record.contains("effect=emit actionable missing capability list"));

        let mut phase_matches = Vec::with_capacity(8);
        let matches_ptr = phase_matches.as_ptr();
        registry.by_phase_into("cuda-launch", &mut phase_matches);

        assert_eq!(phase_matches.as_ptr(), matches_ptr);
        assert!(phase_matches
            .iter()
            .any(|pass| pass.id == "cuda.kernel-failure-diagnostics"));
    }

    #[test]
    fn registry_rejects_unstable_pass_explanation_fields() {
        let registry = OptimizationRegistry::with_release_builtins();

        let empty = registry
            .explain_pass_fire("cuda.megakernel-memory-budget", "", "bounded allocation")
            .expect_err("empty reason should be rejected");
        assert!(empty.contains("empty reason"), "{empty}");

        let unstable = registry
            .explain_pass_fire(
                "cuda.megakernel-memory-budget",
                "frontier|telemetry",
                "bounded allocation",
            )
            .expect_err("separator should be rejected");
        assert!(unstable.contains("unstable separator"), "{unstable}");
    }
}

/// Extensible registry with duplicate and metadata validation.
#[derive(Clone, Debug, Default)]
pub struct OptimizationRegistry {
    passes: BTreeMap<&'static str, OptimizationPass>,
}

impl OptimizationRegistry {
    /// Registry populated with release-path builtins.
    #[must_use]
    pub fn with_release_builtins() -> Self {
        let mut registry = Self::default();
        for pass in RELEASE_OPTIMIZATION_PASSES {
            let _ = registry.register(*pass);
        }
        registry
    }

    /// Register one pass descriptor.
    pub fn register(&mut self, pass: OptimizationPass) -> Result<(), String> {
        validate_pass(pass)?;
        if self.passes.insert(pass.id, pass).is_some() {
            return Err(format!(
                "optimization registry duplicate pass id `{}`. Fix: choose one stable owner for each optimization.",
                pass.id
            ));
        }
        Ok(())
    }

    /// Get a pass by stable id.
    #[must_use]
    pub fn get(&self, id: &str) -> Option<&OptimizationPass> {
        self.passes.get(id)
    }

    /// Iterate passes in stable id order.
    pub fn iter(&self) -> impl Iterator<Item = &OptimizationPass> {
        self.passes.values()
    }

    /// Query passes by phase.
    #[must_use]
    pub fn by_phase(&self, phase: &str) -> Vec<&OptimizationPass> {
        let mut passes = Vec::new();
        self.by_phase_into(phase, &mut passes);
        passes
    }

    /// Query passes by phase into caller-owned storage.
    pub fn by_phase_into<'a>(&'a self, phase: &str, out: &mut Vec<&'a OptimizationPass>) {
        out.clear();
        out.extend(self.passes.values().filter(|pass| pass.phase == phase));
    }

    /// Number of registered passes.
    #[must_use]
    pub fn len(&self) -> usize {
        self.passes.len()
    }

    /// True when no passes are registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.passes.is_empty()
    }

    /// Validate uniqueness and required metadata.
    pub fn validate(&self) -> Result<(), String> {
        let mut ids = BTreeSet::new();
        for pass in self.passes.values().copied() {
            validate_pass(pass)?;
            if !ids.insert(pass.id) {
                return Err(format!(
                    "optimization registry duplicate pass id `{}`. Fix: pass ids must be globally unique.",
                    pass.id
                ));
            }
        }
        Ok(())
    }

    /// Validate that a proposed optimization sequence respects domain phase order.
    pub fn validate_pass_order<'a, I>(&self, pass_ids: I) -> Result<(), String>
    where
        I: IntoIterator<Item = &'a str>,
    {
        let mut seen = BTreeSet::new();
        let mut latest_by_domain: BTreeMap<&'static str, (&'static str, &'static str, u16)> =
            BTreeMap::new();

        for pass_id in pass_ids {
            if !seen.insert(pass_id) {
                return Err(format!(
                    "optimization pass `{pass_id}` appears more than once. Fix: make repeated passes explicit with a distinct fixed-point wrapper."
                ));
            }

            let pass = self.get(pass_id).ok_or_else(|| {
                format!(
                    "unknown optimization pass `{pass_id}`. Fix: register the pass before scheduling it."
                )
            })?;
            let domain = order_domain(pass.owner);
            let rank = phase_rank(pass.owner, pass.phase).ok_or_else(|| {
                format!(
                    "optimization pass `{}` uses unordered phase `{}`. Fix: add the phase to the registry ordering contract before scheduling it.",
                    pass.id, pass.phase
                )
            })?;

            if let Some((previous_id, previous_phase, previous_rank)) = latest_by_domain.get(domain)
            {
                if rank < *previous_rank {
                    return Err(format!(
                        "optimization pass `{}` phase `{}` cannot run after `{}` phase `{}`. Fix: preserve {} phase order.",
                        pass.id, pass.phase, previous_id, previous_phase, domain
                    ));
                }
            }

            latest_by_domain.insert(domain, (pass.id, pass.phase, rank));
        }

        Ok(())
    }

    /// Build a stable explanation for a fired pass.
    pub fn explain_pass_fire<'a>(
        &self,
        pass_id: &str,
        reason: &'a str,
        expected_effect: &'a str,
    ) -> Result<OptimizationPassExplanation<'a>, String> {
        validate_stable_field("reason", reason)?;
        validate_stable_field("expected_effect", expected_effect)?;
        let pass = self.get(pass_id).ok_or_else(|| {
            format!(
                "unknown optimization pass `{pass_id}`. Fix: register the pass before explaining it."
            )
        })?;

        Ok(OptimizationPassExplanation {
            format: "vyre-optimization-explanation-v1",
            pass: *pass,
            reason,
            expected_effect,
        })
    }
}

fn validate_pass(pass: OptimizationPass) -> Result<(), String> {
    for (field, value) in [
        ("id", pass.id),
        ("owner", pass.owner),
        ("phase", pass.phase),
        ("invariant", pass.invariant),
        ("benchmark", pass.benchmark),
    ] {
        if value.trim().is_empty() {
            return Err(format!(
                "optimization pass `{}` has empty {field}. Fix: every pass needs owner, phase, invariant, and benchmark metadata.",
                pass.id
            ));
        }
    }
    if phase_rank(pass.owner, pass.phase).is_none() {
        return Err(format!(
            "optimization pass `{}` uses unordered phase `{}` for owner `{}`. Fix: add the phase to the registry ordering contract before release.",
            pass.id, pass.phase, pass.owner
        ));
    }
    Ok(())
}

fn validate_stable_field(field: &str, value: &str) -> Result<(), String> {
    if value.trim().is_empty() {
        return Err(format!(
            "optimization explanation has empty {field}. Fix: explain why the pass fired and what effect it should have."
        ));
    }
    if value
        .bytes()
        .any(|byte| byte == b'|' || byte == b'\n' || byte == b'\r' || byte == b'\t')
    {
        return Err(format!(
            "optimization explanation {field} contains an unstable separator. Fix: use plain single-line text without pipe or tab separators."
        ));
    }
    Ok(())
}

fn order_domain(owner: &'static str) -> &'static str {
    match owner {
        "vyrec" => "frontend",
        "dataflow" => "dataflow",
        "vyre-cuda" | "vyre-driver" | "vyre-primitives" | "vyre-self" => "runtime",
        _ => "release",
    }
}

fn phase_rank(owner: &'static str, phase: &'static str) -> Option<u16> {
    match order_domain(owner) {
        "frontend" => match phase {
            "preprocessing" => Some(10),
            "diagnostics" => Some(20),
            "parsing" => Some(30),
            "semantic-analysis" => Some(40),
            "frontend-tests" | "frontend-fuzz" => Some(80),
            "frontend-release" => Some(90),
            _ => None,
        },
        "dataflow" => match phase {
            "graph-layout" => Some(10),
            "dataflow-safety" => Some(20),
            "dataflow-residency" | "ifds-residency" => Some(30),
            "dataflow-planning" => Some(40),
            "dataflow-dispatch" => Some(50),
            "dataflow-batch" | "dataflow-output" | "ifds-solve" | "reaching" | "live"
            | "points-to" | "slice" | "bitset" => Some(60),
            "dataflow-tests" => Some(80),
            "dataflow-benchmark" => Some(90),
            _ => None,
        },
        "runtime" => match phase {
            "architecture" => Some(0),
            "api" => Some(5),
            "aot" | "binding" | "cuda-jit" | "optimizer" => Some(10),
            "cuda-memory"
            | "cuda-resident-io"
            | "cuda-resident-sequence"
            | "memory"
            | "bitset"
            | "math"
            | "reduce"
            | "hash"
            | "encoding"
            | "tensor"
            | "text" => Some(20),
            "lowering" | "dispatch-routing" | "pipeline" => Some(30),
            "graph" => Some(35),
            "cuda-launch" => Some(40),
            "diagnostics" => Some(45),
            "cuda-graph" | "megakernel-cache" | "megakernel-memory" => Some(50),
            "megakernel-scheduler" | "graph-traversal" => Some(60),
            "benchmark" => Some(90),
            _ => None,
        },
        "release" => match phase {
            "release" | "release-gate" => Some(100),
            _ => Some(100),
        },
        _ => None,
    }
}


pub use super::optimization_release_passes::RELEASE_OPTIMIZATION_PASSES;

#[cfg(test)]
mod tests {
    use super::{OptimizationPass, OptimizationRegistry, RELEASE_OPTIMIZATION_PASSES};

    #[test]
    fn release_registry_has_at_least_one_hundred_discoverable_passes() {
        let registry = OptimizationRegistry::with_release_builtins();

        assert!(
            registry.len() >= 100,
            "Fix: release optimization registry must enumerate at least 100 concrete passes; got {}.",
            registry.len()
        );
        registry
            .validate()
            .expect("Fix: release optimization registry metadata must be complete and unique.");
    }

    #[test]
    fn release_registry_covers_cuda_dataflow_frontend_and_release_gates() {
        let registry = OptimizationRegistry::with_release_builtins();

        for phase in [
            "cuda-resident-io",
            "megakernel-scheduler",
            "dataflow-residency",
            "preprocessing",
            "semantic-analysis",
            "release-gate",
        ] {
            assert_ne!(
                registry.by_phase(phase).len(),
                0,
                "Fix: optimization registry must expose phase `{phase}`."
            );
        }
    }

    #[test]
    fn registry_rejects_duplicate_or_empty_metadata() {
        let mut registry = OptimizationRegistry::default();
        let pass = RELEASE_OPTIMIZATION_PASSES[0];
        registry.register(pass).expect("Fix: first pass registers");
        assert!(matches!(registry.register(pass), Err(_)),
            "Fix: duplicate optimization pass ids must be rejected."
        );
        assert!(
            registry
                .register(OptimizationPass {
                    id: "bad.empty-owner",
                    owner: "",
                    phase: "phase",
                    invariant: "invariant",
                    benchmark: "bench",
                })
                .is_err(),
            "Fix: empty optimization metadata must be rejected."
        );
    }

    #[test]
    fn registry_rejects_unordered_release_phase_metadata() {
        let mut registry = OptimizationRegistry::default();
        let err = registry
            .register(OptimizationPass {
                id: "bad.unordered-phase",
                owner: "vyre-cuda",
                phase: "mystery-phase",
                invariant: "phase must be ordered",
                benchmark: "phase_order_contract",
            })
            .expect_err("unordered release phases must be rejected");

        assert!(err.contains("unordered phase"), "{err}");
        OptimizationRegistry::with_release_builtins()
            .validate()
            .expect("Fix: all built-in release optimization phases must be ordered");
    }
}