vyre-self-substrate 0.6.3

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
//! Public API boundary validation.

/// One public export observed from API surface analysis.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PublicApiExport<'a> {
    /// Crate exposing the symbol.
    pub crate_name: &'a str,
    /// Public symbol path.
    pub symbol: &'a str,
}

/// Public API boundary proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PublicApiBoundaryProof {
    /// Number of exports scanned.
    pub export_count: usize,
}

/// One source file that owns a public API boundary.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PublicApiSourceSurface<'a> {
    /// Crate exposing the public surface.
    pub crate_name: &'a str,
    /// Source file contents for the public root.
    pub source: &'a str,
    /// Whether `pub mod pipeline` is an intentional stable contract in this crate.
    pub allow_public_pipeline_module: bool,
}

/// Public API source-boundary proof over committed sources and docs artifacts.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PublicApiSourceBoundaryProof {
    /// Number of source surfaces scanned.
    pub surface_count: usize,
    /// Number of public source lines scanned.
    pub public_line_count: usize,
}

/// Public API boundary validation errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PublicApiBoundaryError {
    /// No exports were supplied.
    EmptyExports,
    /// Required metadata is empty.
    EmptyMetadata {
        /// Field name.
        field: &'static str,
    },
    /// Internal implementation detail is public.
    InternalDetailExported {
        /// Crate exposing the symbol.
        crate_name: String,
        /// Public symbol path.
        symbol: String,
        /// Forbidden fragment.
        forbidden_fragment: &'static str,
    },
    /// Committed public API source artifact is missing required evidence.
    ArtifactMissingEvidence {
        /// Missing evidence.
        evidence: &'static str,
    },
    /// Public API source scan found too little evidence.
    ArtifactThresholdMiss {
        /// Field.
        field: &'static str,
        /// Observed value.
        observed: usize,
        /// Required value.
        required: usize,
    },
}

impl std::fmt::Display for PublicApiBoundaryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyExports => write!(
                f,
                "public API boundary export scan is empty. Fix: inspect public exports before release."
            ),
            Self::EmptyMetadata { field } => write!(
                f,
                "public API boundary evidence has empty {field}. Fix: record crate and public symbol path."
            ),
            Self::InternalDetailExported {
                crate_name,
                symbol,
                forbidden_fragment,
            } => write!(
                f,
                "crate `{crate_name}` exports internal detail `{symbol}` containing `{forbidden_fragment}`. Fix: keep staging buffers, temp graph encodings, and pipeline internals private."
            ),
            Self::ArtifactMissingEvidence { evidence } => write!(
                f,
                "public API boundary artifact is missing {evidence}. Fix: connect real public source roots and docs artifacts to the boundary gate."
            ),
            Self::ArtifactThresholdMiss {
                field,
                observed,
                required,
            } => write!(
                f,
                "public API boundary artifact {field}={observed} missed required {required}. Fix: scan every release-facing source root before launch."
            ),
        }
    }
}

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

const FORBIDDEN_PUBLIC_FRAGMENTS: &[&str] = &[
    "staging",
    "temporary",
    "temp_graph",
    "pipeline::",
    "internal",
    "private",
];

/// Validate that public APIs do not expose internal staging or pipeline details.
pub fn validate_public_api_boundary(
    exports: &[PublicApiExport<'_>],
) -> Result<PublicApiBoundaryProof, PublicApiBoundaryError> {
    if exports.is_empty() {
        return Err(PublicApiBoundaryError::EmptyExports);
    }
    for export in exports {
        if export.crate_name.trim().is_empty() {
            return Err(PublicApiBoundaryError::EmptyMetadata {
                field: "crate_name",
            });
        }
        if export.symbol.trim().is_empty() {
            return Err(PublicApiBoundaryError::EmptyMetadata { field: "symbol" });
        }
        let lower = export.symbol.to_ascii_lowercase();
        for forbidden_fragment in FORBIDDEN_PUBLIC_FRAGMENTS {
            if lower.contains(forbidden_fragment) {
                return Err(PublicApiBoundaryError::InternalDetailExported {
                    crate_name: export.crate_name.to_owned(),
                    symbol: export.symbol.to_owned(),
                    forbidden_fragment,
                });
            }
        }
    }

    Ok(PublicApiBoundaryProof {
        export_count: exports.len(),
    })
}

/// Validate real public source roots and docs artifacts for boundary leaks.
pub fn validate_public_api_source_boundaries(
    surfaces: &[PublicApiSourceSurface<'_>],
    vyre_readme_contracts: &str,
    readme_contracts: &str,
    docs_matrix: &str,
) -> Result<PublicApiSourceBoundaryProof, PublicApiBoundaryError> {
    if surfaces.is_empty() {
        return Err(PublicApiBoundaryError::EmptyExports);
    }
    for (artifact, evidence, needle) in [
        (
            vyre_readme_contracts,
            "Vyre README contract schema",
            "\"schema_version\": 2",
        ),
        (
            vyre_readme_contracts,
            "Vyre README zero blockers",
            "\"blockers\": []",
        ),
        (
            vyre_readme_contracts,
            "Vyre README complete tokens",
            "\"missing_tokens\": []",
        ),
        (vyre_readme_contracts, "CUDA public docs token", "\"cuda\""),
        (
            vyre_readme_contracts,
            "Vyre public program token",
            "\"vyre::program\"",
        ),
        (
            readme_contracts,
            "Dataflow consumer README contract schema",
            "\"schema_version\": 2",
        ),
        (
            readme_contracts,
            "Dataflow consumer README zero blockers",
            "\"blockers\": []",
        ),
        (
            readme_contracts,
            "Dataflow consumer README complete tokens",
            "\"missing_tokens\": []",
        ),
        (
            readme_contracts,
            "Dataflow consumer standalone API token",
            "\"dataflow\"",
        ),
        (
            readme_contracts,
            "Dataflow consumer serde feature token",
            "\"serde_evidence\"",
        ),
        (docs_matrix, "docs matrix schema", "\"schema_version\": 5"),
        (
            docs_matrix,
            "docs matrix blockers inventory",
            "\"blockers\"",
        ),
        (
            docs_matrix,
            "docs evidence reference inventory",
            "\"missing_evidence_artifact_refs\"",
        ),
    ] {
        artifact_contains(artifact, evidence, needle)?;
    }

    let mut public_line_count = 0;
    for surface in surfaces {
        if surface.crate_name.trim().is_empty() {
            return Err(PublicApiBoundaryError::EmptyMetadata {
                field: "crate_name",
            });
        }
        if surface.source.trim().is_empty() {
            return Err(PublicApiBoundaryError::EmptyMetadata { field: "source" });
        }
        if surface.crate_name == "vyre-driver-cuda" {
            require_source_contains(surface, "private CUDA pipeline module", "mod pipeline;")?;
            require_source_contains(surface, "private CUDA stream module", "mod stream;")?;
            require_source_contains(surface, "CUDA backend id contract", "CUDA_BACKEND_ID")?;
            require_source_contains(surface, "CUDA backend public contract", "CudaBackend")?;
        }
        if surface.crate_name == "dataflow" {
            require_source_contains(
                surface,
                "CPU parity oracle feature gate",
                "#[cfg(any(test, feature = \"cpu-parity\"))]",
            )?;
            require_source_contains(
                surface,
                "Dataflow consumer dataflow owner module",
                "pub mod graph_layout;",
            )?;
            require_source_contains(
                surface,
                "Dataflow consumer private output scratch contract",
                "mod output_scratch;",
            )?;
        }

        for line in surface.source.lines() {
            let trimmed = line.trim_start();
            let is_public_line = trimmed.starts_with("pub mod ")
                || trimmed.starts_with("pub use ")
                || trimmed.starts_with("pub struct ")
                || trimmed.starts_with("pub enum ")
                || trimmed.starts_with("pub trait ")
                || trimmed.starts_with("pub const ")
                || trimmed.starts_with("pub fn ");
            if !is_public_line {
                continue;
            }
            public_line_count += 1;
            let lower = trimmed.to_ascii_lowercase();
            if lower.starts_with("pub mod pipeline") && !surface.allow_public_pipeline_module {
                return Err(PublicApiBoundaryError::InternalDetailExported {
                    crate_name: surface.crate_name.to_owned(),
                    symbol: trimmed.to_owned(),
                    forbidden_fragment: "pipeline",
                });
            }
            if lower.starts_with("pub mod stream") {
                return Err(PublicApiBoundaryError::InternalDetailExported {
                    crate_name: surface.crate_name.to_owned(),
                    symbol: trimmed.to_owned(),
                    forbidden_fragment: "stream",
                });
            }
            for forbidden_fragment in FORBIDDEN_PUBLIC_FRAGMENTS {
                if *forbidden_fragment == "pipeline::" && surface.allow_public_pipeline_module {
                    continue;
                }
                if lower.contains(forbidden_fragment) {
                    return Err(PublicApiBoundaryError::InternalDetailExported {
                        crate_name: surface.crate_name.to_owned(),
                        symbol: trimmed.to_owned(),
                        forbidden_fragment,
                    });
                }
            }
        }
    }

    artifact_at_least("source surfaces", surfaces.len(), 4)?;
    artifact_at_least("public source lines", public_line_count, 80)?;

    Ok(PublicApiSourceBoundaryProof {
        surface_count: surfaces.len(),
        public_line_count,
    })
}

fn artifact_contains(
    artifact: &str,
    evidence: &'static str,
    needle: &str,
) -> Result<(), PublicApiBoundaryError> {
    if artifact.contains(needle) {
        Ok(())
    } else {
        Err(PublicApiBoundaryError::ArtifactMissingEvidence { evidence })
    }
}

fn artifact_at_least(
    field: &'static str,
    observed: usize,
    required: usize,
) -> Result<(), PublicApiBoundaryError> {
    if observed >= required {
        Ok(())
    } else {
        Err(PublicApiBoundaryError::ArtifactThresholdMiss {
            field,
            observed,
            required,
        })
    }
}

fn require_source_contains(
    surface: &PublicApiSourceSurface<'_>,
    evidence: &'static str,
    needle: &str,
) -> Result<(), PublicApiBoundaryError> {
    if surface.source.contains(needle) {
        Ok(())
    } else {
        Err(PublicApiBoundaryError::ArtifactMissingEvidence { evidence })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;

    #[test]
    fn public_api_boundary_accepts_contract_level_exports() {
        let proof = validate_public_api_boundary(&[
            export("vyre-driver-cuda", "CudaBackend"),
            export("vyre-driver-cuda", "CudaMegakernelExecutionPlan"),
            export("vyre-self-substrate", "OptimizationRegistry"),
        ])
        .expect("Fix: contract-level exports should pass");

        assert_eq!(proof.export_count, 3);
    }

    #[test]
    fn public_api_boundary_rejects_staging_and_pipeline_exports() {
        assert_eq!(
            validate_public_api_boundary(&[export(
                "vyre-driver-cuda",
                "pipeline::CompiledDispatch"
            )])
            .expect_err("pipeline internals should fail"),
            PublicApiBoundaryError::InternalDetailExported {
                crate_name: "vyre-driver-cuda".to_owned(),
                symbol: "pipeline::CompiledDispatch".to_owned(),
                forbidden_fragment: "pipeline::",
            }
        );
        assert_eq!(
            validate_public_api_boundary(&[export("vyre", "TemporaryGraphEncoding")])
                .expect_err("temporary graph encodings should fail"),
            PublicApiBoundaryError::InternalDetailExported {
                crate_name: "vyre".to_owned(),
                symbol: "TemporaryGraphEncoding".to_owned(),
                forbidden_fragment: "temporary",
            }
        );
    }

    #[test]
    fn public_api_boundary_accepts_committed_source_surfaces() {
        let dataflow_source = dataflow_consumer_source();
        let surfaces = source_surfaces(&dataflow_source);
        let proof = validate_public_api_source_boundaries(
            &surfaces,
            include_str!("../../../../release/evidence/docs/vyre-readme-contracts.json"),
            include_str!("../../../../release/evidence/dataflow/readme-contracts.json"),
            include_str!("../../../../release/evidence/docs/docs-matrix.json"),
        )
        .expect("Fix: committed public source surfaces should keep internals behind boundaries");

        assert_eq!(proof.surface_count, 4);
        assert!(proof.public_line_count >= 80);
    }

    #[test]
    fn public_api_boundary_rejects_cuda_pipeline_module_export() {
        let cuda_source = include_str!("../../../../vyre-driver-cuda/src/lib.rs")
            .replace("mod pipeline;", "pub mod pipeline;");
        let dataflow_source = dataflow_consumer_source();
        let surfaces = [
            source_surface("vyre-driver-cuda", &cuda_source, false),
            source_surface(
                "vyre-driver",
                include_str!("../../../../vyre-driver/src/lib.rs"),
                true,
            ),
            source_surface(
                "vyre-libs",
                include_str!("../../../../vyre-libs/src/lib.rs"),
                false,
            ),
            source_surface("dataflow", &dataflow_source, false),
        ];

        assert_eq!(
            validate_public_api_source_boundaries(
                &surfaces,
                include_str!("../../../../release/evidence/docs/vyre-readme-contracts.json"),
                include_str!("../../../../release/evidence/dataflow/readme-contracts.json"),
                include_str!("../../../../release/evidence/docs/docs-matrix.json"),
            )
            .expect_err("CUDA pipeline internals must stay private"),
            PublicApiBoundaryError::InternalDetailExported {
                crate_name: "vyre-driver-cuda".to_owned(),
                symbol: "pub mod pipeline;".to_owned(),
                forbidden_fragment: "pipeline",
            }
        );
    }

    #[test]
    fn public_api_boundary_rejects_incomplete_docs_evidence() {
        let dataflow_source = dataflow_consumer_source();
        let surfaces = source_surfaces(&dataflow_source);
        let docs_matrix = include_str!("../../../../release/evidence/docs/docs-matrix.json")
            .replace("\"schema_version\": 5", "\"schema_version\": 1");

        assert_eq!(
            validate_public_api_source_boundaries(
                &surfaces,
                include_str!("../../../../release/evidence/docs/vyre-readme-contracts.json"),
                include_str!("../../../../release/evidence/dataflow/readme-contracts.json"),
                &docs_matrix,
            )
            .expect_err("boundary proof must not ignore stale docs matrix schema"),
            PublicApiBoundaryError::ArtifactMissingEvidence {
                evidence: "docs matrix schema",
            }
        );
    }

    fn export<'a>(crate_name: &'a str, symbol: &'a str) -> PublicApiExport<'a> {
        PublicApiExport { crate_name, symbol }
    }

    fn source_surfaces<'a>(dataflow_source: &'a str) -> [PublicApiSourceSurface<'a>; 4] {
        [
            source_surface(
                "vyre-driver-cuda",
                include_str!("../../../../vyre-driver-cuda/src/lib.rs"),
                false,
            ),
            source_surface(
                "vyre-driver",
                include_str!("../../../../vyre-driver/src/lib.rs"),
                true,
            ),
            source_surface(
                "vyre-libs",
                include_str!("../../../../vyre-libs/src/lib.rs"),
                false,
            ),
            source_surface("dataflow", dataflow_source, false),
        ]
    }

    fn dataflow_consumer_source() -> String {
        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../../dataflow");
        let entries = fs::read_dir(&root)
            .expect("Fix: release public API boundary tests need adjacent dataflow consumers");
        for entry in entries {
            let path = entry
                .expect("Fix: dataflow consumer discovery must read directory entries")
                .path()
                .join("src/lib.rs");
            let Ok(source) = fs::read_to_string(&path) else {
                continue;
            };
            if source.contains("pub mod graph_layout;")
                && source.contains("mod output_scratch;")
                && source.contains("#[cfg(any(test, feature = \"cpu-parity\"))]")
            {
                return source;
            }
        }
        panic!(
            "Fix: could not discover a dataflow consumer source root with graph layout and scratch contracts"
        );
    }

    fn source_surface<'a>(
        crate_name: &'a str,
        source: &'a str,
        allow_public_pipeline_module: bool,
    ) -> PublicApiSourceSurface<'a> {
        PublicApiSourceSurface {
            crate_name,
            source,
            allow_public_pipeline_module,
        }
    }
}