Skip to main content

coreason_meta_engineering_rust/
pvv.rs

1// Copyright (c) 2026 CoReason, Inc.
2// All rights reserved.
3// SPDX-License-Identifier: LicenseRef-Prosperity-3.0
4
5use std::fs;
6use std::path::Path;
7use std::process::Command;
8use sha2::{Sha256, Digest};
9use serde::{Deserialize, Serialize};
10
11#[derive(Serialize, Deserialize, Debug)]
12pub struct OracleExecutionReceipt {
13    pub execution_hash: String,
14    pub solver_urn: String,
15    pub tokens_burned: i64,
16}
17
18pub fn execute_pvv_pipeline(
19    payload: &str,
20    solver_urn: &str,
21    tokens_burned: i64,
22) -> Result<OracleExecutionReceipt, String> {
23    // 1. Epistemic Strip: strip CORESIG lines
24    let mut clean_lines = Vec::new();
25    for line in payload.lines() {
26        if line.trim().starts_with("# CORESIG:") {
27            continue;
28        }
29        clean_lines.push(line);
30    }
31    let clean_payload = clean_lines.join("\n");
32
33    // 2. Syntax Validation:
34    // We check if the file is Python or Rust.
35    // If it starts with common Python imports or has .py style comments, run python compile check.
36    // Otherwise, try parsing as Rust using syn.
37    let is_python = clean_payload.contains("import ") || clean_payload.contains("def ") || clean_payload.contains("class ");
38
39    if is_python {
40        // Create a temp file to compile check Python syntax
41        let temp_name = format!("pvv_syntax_check_{}.py", rand::random::<u64>());
42        let temp_path = std::env::temp_dir().join(temp_name);
43        fs::write(&temp_path, &clean_payload)
44            .map_err(|e| format!("Failed to write temporary file for validation: {}", e))?;
45
46        let python_exe = std::env::var("PYTHON").unwrap_or_else(|_| "python".to_string());
47        let output = Command::new(python_exe)
48            .arg("-m")
49            .arg("py_compile")
50            .arg(&temp_path)
51            .output();
52
53        let _ = fs::remove_file(&temp_path);
54
55        match output {
56            Ok(out) if out.status.success() => {
57                log::info!("Python syntax check passed.");
58            }
59            Ok(out) => {
60                let err_msg = String::from_utf8_lossy(&out.stderr).trim().to_string();
61                return Err(format!("Python SyntaxError: {}", err_msg));
62            }
63            Err(e) => {
64                log::warn!("Failed to invoke python interpreter for syntax check: {}", e);
65            }
66        }
67    } else {
68        // Rust parsing via syn
69        if let Err(e) = syn::parse_str::<syn::File>(&clean_payload) {
70            return Err(format!("Rust SyntaxError: Failed to parse AST: {}", e));
71        }
72        log::info!("Rust AST parsed successfully.");
73    }
74
75    // 3. Hash computation (SHA-256)
76    let mut hasher = Sha256::new();
77    hasher.update(clean_payload.as_bytes());
78    let execution_hash = hex::encode(hasher.finalize());
79
80    Ok(OracleExecutionReceipt {
81        execution_hash,
82        solver_urn: solver_urn.to_string(),
83        tokens_burned,
84    })
85}