Skip to main content

xpile_oracle/
lib.rs

1//! Oracle trait.
2//!
3//! An [`Oracle`] runs the *original* source (CPython, gcc-compiled C,
4//! the ruchy interpreter) on an input fixture, captures outputs, and
5//! lets the agent compare them against transpiled Rust output. This
6//! is the semantic gate the agent must pass to exit successfully.
7//!
8//! The pattern is borrowed from alchemize: extract reference values
9//! *before* the agent runs, then validate against them.
10
11use serde::{Deserialize, Serialize};
12use std::path::Path;
13
14#[derive(Debug, thiserror::Error)]
15pub enum OracleError {
16    #[error("capture failed: {0}")]
17    Capture(String),
18    #[error("comparison failed: {0}")]
19    Compare(String),
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct Fixture {
24    pub inputs: Vec<serde_json::Value>,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct CapturedOutputs {
29    pub outputs: Vec<serde_json::Value>,
30}
31
32#[derive(Debug, Clone)]
33pub enum ComparisonResult {
34    Match,
35    Divergence {
36        index: usize,
37        expected: String,
38        actual: String,
39    },
40}
41
42pub trait Oracle: Send + Sync {
43    fn language(&self) -> &'static str;
44
45    fn capture(&self, source: &Path, fixture: &Fixture) -> Result<CapturedOutputs, OracleError>;
46
47    fn compare(&self, expected: &CapturedOutputs, actual: &CapturedOutputs) -> ComparisonResult;
48}