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
538
539
540
541
//! Deep personal review release gate.

/// Review status for one public release file.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeepReviewRecord<'a> {
    /// File path.
    pub path: &'a str,
    /// Whether the file was read in full.
    pub read_in_full: bool,
    /// Number of findings opened during review.
    pub findings_opened: u32,
    /// Number of findings fixed before release.
    pub findings_fixed: u32,
    /// Review note or artifact path.
    pub evidence: &'a str,
}

/// Deep review proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeepReviewProof {
    /// Number of files reviewed.
    pub reviewed_files: usize,
    /// Number of findings fixed.
    pub findings_fixed: u32,
}

/// Committed hygiene/deep-review artifact proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeepReviewHygieneArtifactProof {
    /// Number of source files covered by the hygiene matrix.
    pub scanned_files: u64,
}

/// Committed deep-review ledger proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeepReviewLedgerArtifactProof {
    /// Number of touched public crate files reviewed in full.
    pub reviewed_files: u64,
}

/// Deep review validation errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DeepReviewGateError {
    /// No review records supplied.
    EmptyRecords,
    /// Required metadata is empty.
    EmptyMetadata {
        /// Path.
        path: String,
        /// Field.
        field: &'static str,
    },
    /// File was not read in full.
    NotReadInFull {
        /// Path.
        path: String,
    },
    /// Findings were not all fixed.
    FindingsNotFixed {
        /// Path.
        path: String,
        /// Opened.
        opened: u32,
        /// Fixed.
        fixed: u32,
    },
    /// Committed hygiene/deep-review evidence is missing required proof.
    ArtifactMissingEvidence {
        /// Missing evidence.
        evidence: &'static str,
    },
    /// Committed hygiene/deep-review evidence is missing a numeric field.
    ArtifactMissingNumber {
        /// Missing field.
        field: &'static str,
    },
    /// Committed hygiene/deep-review evidence missed a threshold.
    ArtifactThresholdMiss {
        /// Field.
        field: &'static str,
        /// Observed value.
        observed: u64,
        /// Required value.
        required: u64,
    },
    /// Deep-review ledger has unresolved findings.
    LedgerUnfixedFindings {
        /// Opened findings.
        opened: u64,
        /// Fixed findings.
        fixed: u64,
    },
}

impl std::fmt::Display for DeepReviewGateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyRecords => write!(
                f,
                "deep review records are empty. Fix: read every public crate file touched by the release and record review evidence."
            ),
            Self::EmptyMetadata { path, field } => write!(
                f,
                "deep review record `{path}` has empty {field}. Fix: record file path and review evidence."
            ),
            Self::NotReadInFull { path } => write!(
                f,
                "deep review record `{path}` was not read in full. Fix: perform full-file personal review before publishing."
            ),
            Self::FindingsNotFixed {
                path,
                opened,
                fixed,
            } => write!(
                f,
                "deep review record `{path}` has {opened} finding(s) opened but {fixed} fixed. Fix: close every review finding before release."
            ),
            Self::ArtifactMissingEvidence { evidence } => write!(
                f,
                "deep review hygiene artifact is missing {evidence}. Fix: back deep-review claims with committed hygiene evidence across release roots."
            ),
            Self::ArtifactMissingNumber { field } => write!(
                f,
                "deep review hygiene artifact has no numeric {field}. Fix: record exact scan counters."
            ),
            Self::ArtifactThresholdMiss {
                field,
                observed,
                required,
            } => write!(
                f,
                "deep review hygiene artifact {field}={observed} missed required {required}. Fix: expand release-root review coverage or close all findings."
            ),
            Self::LedgerUnfixedFindings { opened, fixed } => write!(
                f,
                "deep review ledger has {opened} opened finding(s) but only {fixed} fixed. Fix: resolve every full-file review finding before publishing."
            ),
        }
    }
}

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

/// Validate deep personal review evidence.
pub fn validate_deep_review_gate(
    records: &[DeepReviewRecord<'_>],
) -> Result<DeepReviewProof, DeepReviewGateError> {
    if records.is_empty() {
        return Err(DeepReviewGateError::EmptyRecords);
    }
    let mut fixed_total = 0_u32;
    for record in records {
        if record.path.trim().is_empty() {
            return Err(DeepReviewGateError::EmptyMetadata {
                path: record.path.to_owned(),
                field: "path",
            });
        }
        if record.evidence.trim().is_empty() {
            return Err(DeepReviewGateError::EmptyMetadata {
                path: record.path.to_owned(),
                field: "evidence",
            });
        }
        if !record.read_in_full {
            return Err(DeepReviewGateError::NotReadInFull {
                path: record.path.to_owned(),
            });
        }
        if record.findings_opened != record.findings_fixed {
            return Err(DeepReviewGateError::FindingsNotFixed {
                path: record.path.to_owned(),
                opened: record.findings_opened,
                fixed: record.findings_fixed,
            });
        }
        fixed_total = fixed_total.saturating_add(record.findings_fixed);
    }
    Ok(DeepReviewProof {
        reviewed_files: records.len(),
        findings_fixed: fixed_total,
    })
}

/// Validate committed hygiene evidence backing deep review.
pub fn validate_deep_review_hygiene_artifacts(
    hygiene_matrix: &str,
    hygiene_proof: &str,
    audit_location_scan: &str,
    public_doc_scan: &str,
) -> Result<DeepReviewHygieneArtifactProof, DeepReviewGateError> {
    for (evidence, needle) in [
        ("hygiene schema", "\"schema_version\": 1"),
        ("zero blockers", "\"blockers\": []"),
        ("zero findings", "\"findings\": []"),
        ("Vyre root coverage", "/matching/vyre"),
        ("dataflow workspace root coverage", "/libs/dataflow/"),
        ("Vyrec root coverage", "/tools/vyrec"),
        ("downstream analyzer root coverage", "/libs/tools/"),
        ("grammar generator root coverage", "/libs/shared/"),
        ("CUDA driver surface", "\"cuda_driver_crate\": true"),
        ("WGPU driver surface", "\"wgpu_driver_crate\": true"),
        ("dataflow workspace surface", "\"dataflow_workspace\": true"),
        (
            "downstream analyzer tool surface",
            "\"downstream_analyzer_tool\": true",
        ),
        (
            "security grammar generator surface",
            "\"security_grammar_generator\": true",
        ),
        ("release scripts surface", "\"release_scripts\": true"),
        ("GitHub workflow surface", "\"github_workflows\": true"),
        (
            "branch protection surface",
            "\"branch_protection_controls\": true",
        ),
        (
            "hidden fallback pattern family",
            "\"hidden_fallback_patterns\"",
        ),
        (
            "resource bound pattern family",
            "\"resource_bound_patterns\"",
        ),
        (
            "release tooling pattern family",
            "\"release_tooling_patterns\"",
        ),
        ("CPU demotion scanner", "cpu_demotion"),
        ("false no-GPU skip scanner", "gpu_unavailable_skip"),
        ("raw cargo scanner", "raw_workspace_cargo"),
        ("heredoc scanner", "heredoc"),
    ] {
        artifact_contains(hygiene_matrix, evidence, needle)?;
    }

    for (evidence, needle) in [
        ("hygiene proof title", "# Release hygiene proof"),
        ("no stubs scan", "no-stubs-scan.json"),
        ("no hidden fallback scan", "no-hidden-fallback-scan.json"),
        ("resource bound scan", "resource-bound-scan.json"),
        ("error surface scan", "error-surface-scan.json"),
        ("cargo wrapper scan", "cargo-wrapper-scan.json"),
        ("audit location scan", "audit-location-scan.json"),
        ("public doc scan", "public-doc-scan.json"),
        ("test hygiene scan", "test-hygiene-scan.json"),
        ("branch protection controls", "branch-protection controls"),
    ] {
        artifact_contains(hygiene_proof, evidence, needle)?;
    }

    for (source, scan_name) in [
        (audit_location_scan, "audit-location"),
        (public_doc_scan, "public-docs"),
    ] {
        artifact_contains(source, "scan schema", "\"schema_version\": 1")?;
        artifact_contains(source, "scan name", scan_name)?;
        artifact_contains(source, "empty scan findings", "\"findings\": []")?;
        artifact_contains(source, "empty scan blockers", "\"blockers\": []")?;
    }

    let scanned_files = artifact_number_field(hygiene_matrix, "scanned_files")?;
    artifact_at_least("scanned_files", scanned_files, 3000)?;

    Ok(DeepReviewHygieneArtifactProof { scanned_files })
}

/// Validate a committed full-file review ledger for every public crate file touched by release work.
pub fn validate_deep_review_ledger_artifact(
    ledger: &str,
) -> Result<DeepReviewLedgerArtifactProof, DeepReviewGateError> {
    for (evidence, needle) in [
        ("ledger schema", "\"schema_version\": 1"),
        (
            "active plan path",
            "\"plan_path\": \"release/plans/paradigm-shift-100-concrete.md\"",
        ),
        (
            "touched public crate file list",
            "\"touched_public_crate_files\"",
        ),
        ("review records", "\"review_records\""),
        ("read-in-full proof", "\"read_in_full\": true"),
        ("zero blockers", "\"blockers\": []"),
        ("Vyre crate review", "\"crate\": \"vyre\""),
        (
            "dataflow consumer crate review",
            "\"crate_role\": \"dataflow-consumer\"",
        ),
        ("Vyrec crate review", "\"crate\": \"vyrec\""),
        (
            "CUDA driver crate review",
            "\"crate\": \"vyre-driver-cuda\"",
        ),
        ("findings opened count", "\"findings_opened\""),
        ("findings fixed count", "\"findings_fixed\""),
    ] {
        artifact_contains(ledger, evidence, needle)?;
    }

    let reviewed_files = artifact_number_field(ledger, "reviewed_files")?;
    artifact_at_least("reviewed_files", reviewed_files, 1)?;
    let findings_opened = artifact_number_field(ledger, "findings_opened")?;
    let findings_fixed = artifact_number_field(ledger, "findings_fixed")?;
    if findings_fixed < findings_opened {
        return Err(DeepReviewGateError::LedgerUnfixedFindings {
            opened: findings_opened,
            fixed: findings_fixed,
        });
    }

    Ok(DeepReviewLedgerArtifactProof { reviewed_files })
}

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

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

fn artifact_number_field(artifact: &str, field: &'static str) -> Result<u64, DeepReviewGateError> {
    let key = format!("\"{field}\"");
    let start = artifact
        .find(&key)
        .ok_or(DeepReviewGateError::ArtifactMissingNumber { field })?;
    let after_key = &artifact[start + key.len()..];
    let colon = after_key
        .find(':')
        .ok_or(DeepReviewGateError::ArtifactMissingNumber { field })?;
    let after_colon = after_key[colon + 1..].trim_start();
    let digits = after_colon
        .chars()
        .take_while(|ch| ch.is_ascii_digit())
        .collect::<String>();
    if digits.is_empty() {
        return Err(DeepReviewGateError::ArtifactMissingNumber { field });
    }
    digits
        .parse::<u64>()
        .map_err(|_| DeepReviewGateError::ArtifactMissingNumber { field })
}

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

    #[test]
    fn deep_review_accepts_full_review_with_fixed_findings() {
        let proof = validate_deep_review_gate(&[
            record("vyre-driver-cuda/src/lib.rs", true, 2, 2),
            record("vyre-self-substrate/src/lib.rs", true, 0, 0),
        ])
        .expect("Fix: complete review records should pass");

        assert_eq!(proof.reviewed_files, 2);
        assert_eq!(proof.findings_fixed, 2);
    }

    #[test]
    fn deep_review_rejects_partial_file_review() {
        assert_eq!(
            validate_deep_review_gate(&[record("vyre/src/lib.rs", false, 0, 0)])
                .expect_err("partial review should fail"),
            DeepReviewGateError::NotReadInFull {
                path: "vyre/src/lib.rs".to_owned(),
            }
        );
    }

    #[test]
    fn deep_review_rejects_unfixed_findings() {
        assert_eq!(
            validate_deep_review_gate(&[record("vyre/src/lib.rs", true, 3, 2)])
                .expect_err("unfixed findings should fail"),
            DeepReviewGateError::FindingsNotFixed {
                path: "vyre/src/lib.rs".to_owned(),
                opened: 3,
                fixed: 2,
            }
        );
    }

    #[test]
    fn deep_review_accepts_committed_hygiene_artifacts() {
        let proof = validate_deep_review_hygiene_artifacts(
            include_str!("../../../../release/evidence/hygiene/hygiene-matrix.json"),
            include_str!("../../../../release/evidence/docs/release-hygiene-proof.md"),
            include_str!("../../../../release/evidence/hygiene/audit-location-scan.json"),
            include_str!("../../../../release/evidence/hygiene/public-doc-scan.json"),
        )
        .expect("Fix: committed hygiene artifacts should pass deep-review evidence checks");

        assert!(proof.scanned_files >= 3000);
    }

    #[test]
    fn deep_review_rejects_hygiene_artifact_with_blockers() {
        let hygiene_matrix = r#"{
          "schema_version": 1,
          "blockers": ["cpu-demotion"],
          "findings": [],
          "scanned_files": 3000,
          "scanned_roots": ["/matching/vyre", "/libs/dataflow/consumer", "/tools/vyrec", "/libs/tools/analyzer", "/libs/shared/security-grammar-gen"],
          "release_surface_coverage": {
            "cuda_driver_crate": true,
            "wgpu_driver_crate": true,
            "release_scripts": true,
            "github_workflows": true,
            "branch_protection_controls": true,
            "hidden_fallback_patterns": ["cpu_demotion", "gpu_unavailable_skip"],
            "resource_bound_patterns": [],
            "release_tooling_patterns": ["raw_workspace_cargo", "heredoc"]
          }
        }"#;
        let proof = "# Release hygiene proof no-stubs-scan.json no-hidden-fallback-scan.json resource-bound-scan.json error-surface-scan.json cargo-wrapper-scan.json audit-location-scan.json public-doc-scan.json test-hygiene-scan.json branch-protection controls";
        let scan = r#"{"schema_version":1,"scan":"audit-location","findings":[],"blockers":[]}"#;
        let docs = r#"{"schema_version":1,"scan":"public-docs","findings":[],"blockers":[]}"#;

        assert_eq!(
            validate_deep_review_hygiene_artifacts(hygiene_matrix, proof, scan, docs)
                .expect_err("hygiene blockers should fail"),
            DeepReviewGateError::ArtifactMissingEvidence {
                evidence: "zero blockers",
            }
        );
    }


    #[test]
    fn deep_review_accepts_complete_full_file_review_ledger() {
        let proof = validate_deep_review_ledger_artifact(
            r#"{
              "schema_version": 1,
              "plan_path": "release/plans/paradigm-shift-100-concrete.md",
              "touched_public_crate_files": [
                "vyre/src/lib.rs",
                "libs/dataflow/consumer/src/lib.rs",
                "tools/vyrec/src/main.rs",
                "vyre-driver-cuda/src/lib.rs"
              ],
              "review_records": [
                {"crate": "vyre", "path": "vyre/src/lib.rs", "read_in_full": true},
                {"crate": "dataflow-consumer", "crate_role": "dataflow-consumer", "path": "libs/dataflow/consumer/src/lib.rs", "read_in_full": true},
                {"crate": "vyrec", "path": "tools/vyrec/src/main.rs", "read_in_full": true},
                {"crate": "vyre-driver-cuda", "path": "vyre-driver-cuda/src/lib.rs", "read_in_full": true}
              ],
              "reviewed_files": 4,
              "findings_opened": 3,
              "findings_fixed": 3,
              "blockers": []
            }"#,
        )
        .expect("Fix: complete full-file review ledger should pass");

        assert_eq!(proof.reviewed_files, 4);
    }

    #[test]
    fn deep_review_rejects_hygiene_scan_as_full_file_review_ledger() {
        let err = validate_deep_review_ledger_artifact(include_str!(
            "../../../../release/evidence/hygiene/hygiene-matrix.json"
        ))
        .expect_err("hygiene scans are not personal full-file review ledgers");

        assert_eq!(
            err,
            DeepReviewGateError::ArtifactMissingEvidence {
                evidence: "active plan path",
            }
        );
    }

    #[test]
    fn deep_review_rejects_ledger_with_unfixed_findings() {
        let err = validate_deep_review_ledger_artifact(
            r#"{
              "schema_version": 1,
              "plan_path": "release/plans/paradigm-shift-100-concrete.md",
              "touched_public_crate_files": ["vyre/src/lib.rs"],
              "review_records": [
                {"crate": "vyre", "path": "vyre/src/lib.rs", "read_in_full": true},
                {"crate": "dataflow-consumer", "crate_role": "dataflow-consumer", "path": "libs/dataflow/consumer/src/lib.rs", "read_in_full": true},
                {"crate": "vyrec", "path": "tools/vyrec/src/main.rs", "read_in_full": true},
                {"crate": "vyre-driver-cuda", "path": "vyre-driver-cuda/src/lib.rs", "read_in_full": true}
              ],
              "reviewed_files": 4,
              "findings_opened": 2,
              "findings_fixed": 1,
              "blockers": []
            }"#,
        )
        .expect_err("unfixed full-file review findings should block release");

        assert_eq!(
            err,
            DeepReviewGateError::LedgerUnfixedFindings {
                opened: 2,
                fixed: 1,
            }
        );
    }

    fn record(
        path: &'static str,
        read_in_full: bool,
        findings_opened: u32,
        findings_fixed: u32,
    ) -> DeepReviewRecord<'static> {
        DeepReviewRecord {
            path,
            read_in_full,
            findings_opened,
            findings_fixed,
            evidence: "release/reviews/deep-review.md",
        }
    }
}