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
//! Vyrec C dialect matrix validation.

use std::collections::BTreeSet;

/// Frontend phase tracked against clang.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum CDialectPhase {
    /// C preprocessing.
    Preprocessing,
    /// Lexing and token classification.
    Lexing,
    /// Parsing.
    Parsing,
    /// Semantic analysis.
    SemanticAnalysis,
    /// Diagnostics.
    Diagnostics,
    /// Lowering/codegen steps intentionally outside this beta release.
    UnsupportedLowerStep,
}

/// Phase compatibility state.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CDialectSupport {
    /// Compatible with the declared clang scope.
    Compatible,
    /// Partially compatible with explicit gap evidence.
    Partial,
    /// Unsupported in this release.
    Unsupported,
}

/// One dialect matrix row.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CDialectMatrixRow<'a> {
    /// Phase.
    pub phase: CDialectPhase,
    /// C dialect label, e.g. C11/GNU11.
    pub dialect: &'a str,
    /// Support state.
    pub support: CDialectSupport,
    /// Exact test command or release evidence command.
    pub command: &'a str,
    /// Evidence path.
    pub evidence: &'a str,
}

/// Dialect matrix proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CDialectMatrixProof {
    /// Number of rows.
    pub row_count: usize,
    /// Number of unsupported lower-step rows.
    pub unsupported_lower_rows: usize,
}

/// Committed C dialect target manifest proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CDialectTargetManifestProof {
    /// Number of frozen translation-unit source entries detected.
    pub source_count: usize,
}

/// Dialect matrix validation errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CDialectMatrixError {
    /// Matrix is empty.
    EmptyMatrix,
    /// Required metadata is empty.
    EmptyMetadata {
        /// Phase.
        phase: CDialectPhase,
        /// Field.
        field: &'static str,
    },
    /// Command does not use cargo_full.
    CommandDoesNotUseCargoFull {
        /// Phase.
        phase: CDialectPhase,
        /// Command.
        command: String,
    },
    /// Required phase is missing.
    MissingPhase {
        /// Missing phase.
        phase: CDialectPhase,
    },
    /// Parser or semantic phase is marked unsupported instead of exposed as partial/failing evidence.
    HidesParserOrSemanticGap {
        /// Phase.
        phase: CDialectPhase,
    },
    /// A lower-step row is not marked unsupported.
    LowerStepNotUnsupported,
    /// Committed C dialect target manifest is missing required evidence.
    ManifestMissingEvidence {
        /// Missing evidence field.
        evidence: &'static str,
    },
    /// Committed C dialect target manifest does not meet a threshold.
    ManifestThresholdMiss {
        /// Field name.
        field: &'static str,
        /// Observed value.
        observed: usize,
        /// Required value.
        required: usize,
    },
}

impl std::fmt::Display for CDialectMatrixError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyMatrix => write!(
                f,
                "C dialect matrix is empty. Fix: list preprocessing, lexing, parsing, semantic analysis, diagnostics, and unsupported lower steps."
            ),
            Self::EmptyMetadata { phase, field } => write!(
                f,
                "C dialect matrix row {phase:?} has empty {field}. Fix: every row needs dialect, command, and evidence."
            ),
            Self::CommandDoesNotUseCargoFull { phase, command } => write!(
                f,
                "C dialect matrix row {phase:?} uses `{command}` instead of ./cargo_full. Fix: make dialect evidence reproducible through cargo_full."
            ),
            Self::MissingPhase { phase } => write!(
                f,
                "C dialect matrix is missing {phase:?}. Fix: document every pre-lowering phase and unsupported lower steps."
            ),
            Self::HidesParserOrSemanticGap { phase } => write!(
                f,
                "C dialect matrix marks {phase:?} unsupported. Fix: parser and semantic gaps must be partial/failing evidence, not beta limitations."
            ),
            Self::LowerStepNotUnsupported => write!(
                f,
                "C dialect matrix lower-step row is not unsupported. Fix: this beta release may only mark lower steps unsupported."
            ),
            Self::ManifestMissingEvidence { evidence } => write!(
                f,
                "C dialect target manifest is missing {evidence}. Fix: keep the frozen clang parity target explicit and GPU-first."
            ),
            Self::ManifestThresholdMiss {
                field,
                observed,
                required,
            } => write!(
                f,
                "C dialect target manifest {field}={observed} missed required {required}. Fix: restore the full frozen Linux subsystem target coverage."
            ),
        }
    }
}

impl std::error::Error for CDialectMatrixError {}

const REQUIRED_PHASES: &[CDialectPhase] = &[
    CDialectPhase::Preprocessing,
    CDialectPhase::Lexing,
    CDialectPhase::Parsing,
    CDialectPhase::SemanticAnalysis,
    CDialectPhase::Diagnostics,
    CDialectPhase::UnsupportedLowerStep,
];

/// Validate Vyrec C dialect matrix evidence.
pub fn validate_c_dialect_matrix(
    rows: &[CDialectMatrixRow<'_>],
) -> Result<CDialectMatrixProof, CDialectMatrixError> {
    if rows.is_empty() {
        return Err(CDialectMatrixError::EmptyMatrix);
    }

    let mut phases = BTreeSet::new();
    let mut unsupported_lower_rows = 0_usize;
    for row in rows {
        for (field, value) in [
            ("dialect", row.dialect),
            ("command", row.command),
            ("evidence", row.evidence),
        ] {
            if value.trim().is_empty() {
                return Err(CDialectMatrixError::EmptyMetadata {
                    phase: row.phase,
                    field,
                });
            }
        }
        if !row.command.trim_start().starts_with("./cargo_full ") {
            return Err(CDialectMatrixError::CommandDoesNotUseCargoFull {
                phase: row.phase,
                command: row.command.to_owned(),
            });
        }
        if matches!(
            row.phase,
            CDialectPhase::Parsing | CDialectPhase::SemanticAnalysis
        ) && row.support == CDialectSupport::Unsupported
        {
            return Err(CDialectMatrixError::HidesParserOrSemanticGap { phase: row.phase });
        }
        if row.phase == CDialectPhase::UnsupportedLowerStep {
            if row.support != CDialectSupport::Unsupported {
                return Err(CDialectMatrixError::LowerStepNotUnsupported);
            }
            unsupported_lower_rows += 1;
        }
        phases.insert(row.phase);
    }

    for phase in REQUIRED_PHASES {
        if !phases.contains(phase) {
            return Err(CDialectMatrixError::MissingPhase { phase: *phase });
        }
    }

    Ok(CDialectMatrixProof {
        row_count: rows.len(),
        unsupported_lower_rows,
    })
}

/// Validate the committed frozen C dialect target manifest source.
pub fn validate_c_dialect_target_manifest(
    manifest: &str,
) -> Result<CDialectTargetManifestProof, CDialectMatrixError> {
    for (evidence, needle) in [
        ("schema", "schema = \"vyrec.parity.target.v1\""),
        ("Linux lib/math target id", "id = \"linux-lib-math-v6.8\""),
        (
            "frozen Linux commit",
            "commit = \"90d1f30371ae3337beb01666b226320728d35c70\"",
        ),
        ("subsystem root", "subsystem_root = \"lib/math\""),
        ("GNU11 language", "language = \"gnu11\""),
        ("lowering excluded", "lowering = false"),
        ("pre-lowering required", "pre_lowering_required = true"),
        (
            "semantic analysis parity scope",
            "clang_parity_through = \"semantic-analysis\"",
        ),
        (
            "CPU oracle-only execution",
            "cpu_execution_allowed = \"oracle-only\"",
        ),
        ("GPU execution required", "gpu_execution_required = true"),
        (
            "zero silent CPU demotions",
            "zero_silent_cpu_demotions = true",
        ),
        ("zero false no-GPU skips", "zero_false_no_gpu_skips = true"),
        (
            "resident GPU frontend gate",
            "resident_gpu_frontend_required = true",
        ),
        ("clang baseline required", "clang_baseline_required = true"),
        ("preprocessor proof category", "preprocessor = ["),
        ("lexer proof category", "lexer = ["),
        ("parser proof category", "parser = ["),
        ("semantic analysis proof category", "semantic_analysis = ["),
        ("ABI layout proof category", "abi_layout = ["),
        ("performance proof category", "performance = ["),
    ] {
        manifest_contains(manifest, evidence, needle)?;
    }

    let source_count = manifest.matches("lib/math/").filter(|_| true).count();
    if source_count < 12 {
        return Err(CDialectMatrixError::ManifestThresholdMiss {
            field: "lib/math source/header mentions",
            observed: source_count,
            required: 12,
        });
    }

    Ok(CDialectTargetManifestProof { source_count })
}

fn manifest_contains(
    manifest: &str,
    evidence: &'static str,
    needle: &str,
) -> Result<(), CDialectMatrixError> {
    if manifest.contains(needle) {
        Ok(())
    } else {
        Err(CDialectMatrixError::ManifestMissingEvidence { evidence })
    }
}

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

    #[test]
    fn dialect_matrix_accepts_all_phases_and_lower_step_scope() {
        let proof =
            validate_c_dialect_matrix(&rows()).expect("Fix: complete dialect matrix should pass");

        assert_eq!(proof.row_count, 6);
        assert_eq!(proof.unsupported_lower_rows, 1);
    }

    #[test]
    fn dialect_matrix_rejects_hidden_semantic_gap() {
        let mut rows = rows();
        rows[3].support = CDialectSupport::Unsupported;

        assert_eq!(
            validate_c_dialect_matrix(&rows).expect_err("semantic unsupported should fail"),
            CDialectMatrixError::HidesParserOrSemanticGap {
                phase: CDialectPhase::SemanticAnalysis,
            }
        );
    }

    #[test]
    fn dialect_matrix_rejects_raw_cargo_and_missing_phase() {
        let mut raw_cargo_rows = rows();
        raw_cargo_rows[0].command = "cargo test";
        assert_eq!(
            validate_c_dialect_matrix(&raw_cargo_rows).expect_err("raw cargo should fail"),
            CDialectMatrixError::CommandDoesNotUseCargoFull {
                phase: CDialectPhase::Preprocessing,
                command: "cargo test".to_owned(),
            }
        );

        let mut missing_phase_rows = rows();
        missing_phase_rows.pop();
        assert_eq!(
            validate_c_dialect_matrix(&missing_phase_rows)
                .expect_err("missing lower step should fail"),
            CDialectMatrixError::MissingPhase {
                phase: CDialectPhase::UnsupportedLowerStep,
            }
        );
    }

    #[test]
    fn dialect_matrix_accepts_committed_linux_math_target_manifest() {
        let proof = validate_c_dialect_target_manifest(include_str!(
            "../../../../vyre-frontend-c/parity/linux_math_v6_8.toml"
        ))
        .expect("Fix: committed Linux lib/math target manifest should pass");

        assert!(proof.source_count >= 12);
    }

    #[test]
    fn dialect_matrix_rejects_cpu_execution_manifest() {
        let manifest = r#"
            schema = "vyrec.parity.target.v1"
            id = "linux-lib-math-v6.8"
            commit = "90d1f30371ae3337beb01666b226320728d35c70"
            subsystem_root = "lib/math"
            language = "gnu11"
            lowering = false
            pre_lowering_required = true
            clang_parity_through = "semantic-analysis"
            cpu_execution_allowed = "production"
            gpu_execution_required = true
            zero_silent_cpu_demotions = true
            zero_false_no_gpu_skips = true
            resident_gpu_frontend_required = true
            clang_baseline_required = true
            preprocessor = []
            lexer = []
            parser = []
            semantic_analysis = []
            abi_layout = []
            performance = []
        "#;

        assert_eq!(
            validate_c_dialect_target_manifest(manifest)
                .expect_err("production CPU execution should fail"),
            CDialectMatrixError::ManifestMissingEvidence {
                evidence: "CPU oracle-only execution",
            }
        );
    }

    fn rows() -> Vec<CDialectMatrixRow<'static>> {
        vec![
            row(CDialectPhase::Preprocessing, CDialectSupport::Partial),
            row(CDialectPhase::Lexing, CDialectSupport::Compatible),
            row(CDialectPhase::Parsing, CDialectSupport::Partial),
            row(CDialectPhase::SemanticAnalysis, CDialectSupport::Partial),
            row(CDialectPhase::Diagnostics, CDialectSupport::Compatible),
            row(
                CDialectPhase::UnsupportedLowerStep,
                CDialectSupport::Unsupported,
            ),
        ]
    }

    fn row(phase: CDialectPhase, support: CDialectSupport) -> CDialectMatrixRow<'static> {
        CDialectMatrixRow {
            phase,
            dialect: "gnu11",
            support,
            command: "./cargo_full test -j1 -p vyrec",
            evidence: "release/parity/vyrec-c-dialect-matrix.md",
        }
    }
}