Skip to content
Runtime / Embedding the runtime

Embedding the runtime

Markdown

Run compiled Atracode programs inside your own host program by loading a SEG, supplying inputs, setting a fuel budget and reading outputs.

Preview. Atracode opens at launch, and the commands, tools and API shapes on this page may change before then.

The Atracode runtime can run inside your own program. Your host loads a compiled Semantic Execution Graph (SEG), hands it inputs, sets a fuel budget, runs it and reads the outputs.

This is useful when Atracode code is one part of a larger system: a pricing rule inside a web service, a validation step in a data pipeline, or migrated COBOL logic called from a new application. The embedded program behaves exactly as it does in a standalone build, with the same RAVEN hash and the same result for the same inputs.

Note: The embedding API is a preview. Type names, method names and signatures may change before the runtime is published at github.com/atracode/runtime at launch.

How a host and a program divide the work

A compiled Atracode program is closed. It can only use what it declares, and it never reaches out to the clock, the network, the file system or environment variables on its own. Code that tries to is refused when it is compiled.

That leaves the host in charge of the outside world. If the program needs the current time, a row from a database or the contents of a file, the host reads it and supplies it as an input. The program computes, and the host decides what to do with the outputs.

This split is what keeps embedded programs deterministic. Everything that varies from run to run passes through inputs you can see, log and replay.

What a host does

Embedding follows the same four steps in any host:

  1. Load a compiled SEG. You get one by compiling with atracode compile or as part of atracode build. Its RAVEN hash identifies the behavior you’re loading.
  2. Provide inputs. Use atracode inspect <file> to see what the program declares: its inputs, membrane reads and writes, and bounds.
  3. Set a fuel budget. Every execution needs one, so nothing the host calls can run forever.
  4. Run and read outputs. A run either finishes with outputs, or stops with a located report if fuel runs out.

Example in Rust

This sketch shows the shape of a Rust host. It loads a compiled interest calculation, passes the current date in as an input because the program can’t read the clock itself, and handles both outcomes.

// Preview: names and signatures may change before launch.
use atracode_runtime::{Runtime, Seg, Inputs, Outcome};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load a SEG produced by `atracode compile` or `atracode build`.
    let seg = Seg::load("interest.seg")?;
    println!("running raven:{}", seg.raven_hash());

    // The host supplies everything from the outside world as inputs.
    let today = current_date_from_host();
    let inputs = Inputs::new()
        .set("balance", 1_250_000)
        .set("rate_bp", 425)
        .set("years", 10)
        .set("as_of", today);

    // Every execution runs on a fuel budget.
    let runtime = Runtime::new();
    match runtime.run(&seg, inputs, 5_000_000)? {
        Outcome::Finished(outputs) => {
            println!("balance = {}", outputs.get("balance")?);
        }
        Outcome::OutOfFuel(report) => {
            // Same program, inputs and budget stop at the same point every time.
            eprintln!("stopped: {}", report);
        }
    }
    Ok(())
}

current_date_from_host stands for whatever your application uses to get the date. The point is where it happens: in the host, before the run, so the value is an input you can record.

Running the same thing elsewhere

Because the embedded runtime evaluates the same SEG the same way, you can check a result outside your host. Run the same program with the same inputs through atracode run, or in a standalone build, and you get the same outputs, or the same out-of-fuel stop at the same point.

Other host languages will follow the same load, inputs, fuel, run pattern. Which languages ship with embedding bindings at launch will be listed on this page.

Next steps

  • Runtime What the evaluator is and how it ships
  • Fuel Budgets and out-of-fuel reports
  • Memory membrane How a program’s reads and writes are declared
  • SDKs Call the compiler from your language
esc

Type to search titles, headings and text.