Skip to main content
Sandboxed logic for Rust, JavaScript, and Python

JSON that runs.

Stof is JSON with functions. Put the logic next to the data, and your app runs the document in a sandbox that can only touch what you hand it.

A model can write it. A person can read it. Your app can run it.

cargo add stofpip install stof
doc.stofedit me
{
    "customer": "acme",
    "plan": { "rate": 0.02, "included": 1000 },

    fn price(units: int) -> float {
        const billable = units - self.plan.included;
        if (billable <= 0) return 0;
        billable * self.plan.rate
    }
}
app.ts
import { StofDoc } from '@formata/stof'; const doc = await StofDoc.new(src); await doc.call('price', 5000);
output
…

A model wrote it. Check it before it runs.

Models write Stof well, because it reads like JSON and JavaScript. Parse a draft, run it against the cases you care about, and ship it only if it passes. No deploy, and nothing runs outside your process.

draft.stofPlayground ↗
// A model drafted this rule for one customer.
draft: r#"
  fn discount(units: int) -> float {
      units > 50_000 ? 0.9 : 0
  }
"#

#[main]
fn main() {
  using rule = new {};
  parse(self.draft, rule);

  // Before it ships, your app checks what it does.
  for (const units in [100, 10_000, 60_000]) {
      const off = rule.discount(units);
      if (off < 0 || off > 0.5) {
          pln(`rejected: ${(off * 100).round()}% off at ${units} units`);
          return;
      }
  }
  pln('approved');
}
Output

The config checks itself.

Declare the rule once on a type, point it at any object. No schema file, no validation library.

server.stofPlayground ↗
#[type]
Server: {
  #[schema((v: int): bool => v > 0 && v < 65536)]
  int port: 8080
}

config: { port: 70000 }

#[main]
fn main() {
  pln(<Server>.schemafy(self.config));   // false
  self.config.port = 443;
  pln(<Server>.schemafy(self.config));   // true
}
Output

It can't touch what you didn't hand it.

No file system, network, or environment unless your app turns them on. The sandbox isn't a setting, it's how a document runs. Your app gives a document functions with doc.lib(...), and that's all it can call.

sandbox.stofPlayground ↗
#[main]
fn main() {
  fs::read('/etc/passwd');
}
Output

Same document. Your language.

Load the document, hand it the functions it may call, set a time limit, and call it. The same document runs in TypeScript, Python, and Rust, and gives the same answer in each.

import { StofDoc } from '@formata/stof';

// policy: Stof text a model, a customer, or your team wrote
const doc = await StofDoc.new();
// the only function it can call
doc.lib('App', 'plan', (id: string) => plans.get(id));
doc.setMaxExecutionTime(50); // ms
doc.parse(policy);

const price = await doc.call('price', 'acme', 1200);

Stof runs every customer's pricing at Limitr, in production, on every call.