Writing a runtime adapter

A runtime adapter is one crate under crates/superfluid-adapters/, built on superfluid-adapter-kit. It tells the daemon what the runtime is, whether it runs here, how to install it, how to pull its models, and how to load one. The kit supplies the worker executable, the check report, serving over Link W, installing and conformance checks.

cratewhat it is
superfluid-adapter-kitthe Runtime trait, Loaded, worker_main, install, Pull, conformance
superfluid-adapter-mockthe smallest adapter; a template
superfluid-adapter-baserta full engine over libbaseRT
superfluid-adapter-llamacppprimitives over llama.cpp's C API; tokenizer/ is the GGUF tokenizer library the daemon loads
superfluid-adapter-mlxprimitives over mlx-lm in an embedded CPython

A runtime plugs in as a full engine (implements superfluid_engine::Engine) or as a primitive runtime (implements superfluid_executor::RuntimePrimitives; the generic executor provides the tick contract, prefix cache, sampling, grammars and park/resume). Most new runtimes should be primitive runtimes.

Steps

  1. Create crates/superfluid-adapters/superfluid-adapter-<id>/ depending on superfluid-adapter-kit, and add it to the workspace members. The id is lowercase letters, digits, ., - and _.

  2. Implement superfluid_adapter_kit::Runtime for a unit struct.

  3. Add the worker binary src/bin/superfluid-worker-<id>.rs with a [[bin]] entry:

    fn main() {
        superfluid_adapter_kit::worker_main(&superfluid_adapter_<id>::<Runtime>)
    }
    
  4. Add tests/kit_conformance.rs running conformance::runtime and conformance::worker, and run the tick-contract harness (superfluid_engine::testing::tick_contract) on the engine.

  5. To pull models by id: pull_options() and pull().

  6. To make it installable: a recipe.json of tested builds (each asset's size and SHA-256) and plan(); override install() if unpacking archives into lib/ is not enough.

  7. To ship it: build and verify its worker in scripts/build_runtime_adapters.sh.

  8. Optionally, to link it into a source build: a cargo feature on superfluid, a line in linked::runtimes(), and its id in linked::FEATURES.

The daemon holds no list of runtimes; it finds superfluid-worker-<id> on disk (see Runtimes).

The Runtime trait

pub trait Runtime: Send + Sync {
    fn info(&self) -> RuntimeInfo;                                        // required
    fn probe(&self, args: &WorkerArgs) -> Result<Probe, String>;          // required
    fn open(&self, args: &WorkerArgs) -> Result<Loaded, String>;          // required

    fn flags(&self) -> &'static [&'static str] { &[] }
    fn options(&self) -> &'static [ServeOption] { &[] }        // knobs of this runtime, --<id>-<name> <value> on serve
    fn needs_model(&self) -> bool { true }
    fn prepare(&self, _args: &WorkerArgs) {}
    fn ahead(&self, _args: &WorkerArgs) -> Result<Option<String>, String> { Ok(None) }
    fn named(&self, _args: &WorkerArgs) -> Option<String> { None }
    fn static_capabilities(&self) -> String { String::new() }
    fn compat(&self) -> serde_json::Value { json!({}) }           // what the adapter was built and tested for: `tested` versions, by convention (basert: `pinned_engines`, with their ABI)
    fn reads(&self, path: &Path) -> Result<(), String> { /* by info().formats */ }
    fn model_facts(&self, _path: &Path) -> Option<Result<String, String>> { None }
    fn sizing(&self, _path: &Path, _args: &WorkerArgs) -> Option<Result<Sizing, String>> { None }
    fn tokenizer(&self, _model: &Path) -> Option<Result<Arc<dyn Tokenizer>, String>> { None }
    fn plan(&self, _req: &Request, _host: &Host) -> Option<Result<Vec<Plan>, String>> { None }
    fn versions(&self, host: &Host) -> Vec<Candidate> { /* each `tested` version of compat(), planned here */ }
    fn unlisted(&self) -> Option<Unlisted> { None }              // which versions left out of versions() install without --untested, and what that asks of one
    fn pull_options(&self) -> Option<&'static [PullOption]> { None }
    fn pull(&self, _pull: &Pull, _args: &WorkerArgs) -> Result<PathBuf, String> { /* refuses */ }
    fn install(&self, plan: &Plan, dir: &Path) -> Result<Vec<(String, String)>, String> { /* fetch into dir/lib */ }
}
methodanswers
info()id, aliases, formats it reads (Format::file by extension or magic, Format::directory by contents), and the tokenizer source (HuggingFace, Library(stem) or Engine)
probe()whether it runs here: version and devices, or why not. A Device carries memory, as the runtime reads it, and budget, what the adapter plans a model and its cache against there (0 where it plans nothing), which superfluid runtimes shows for every runtime alike
open()load the model: Loaded::primitives(runtime, superfluid_adapter_kit::context(args)) or Loaded::engine(engine, rings)
flags()extra worker flags it reads (--name); anything else is refused
options()the knobs it takes on serve as --<id>-<name> <value>, each with a kind (Int, IntFrom(least), IntIn(least, most), Float, FloatFrom(least), OnOff, Choice, Text; bound a number the runtime would clamp, wrap or fail on), a default for the help and a description; the daemon checks a value against the kind before the worker starts and forwards it as --<name> <value>, the worker refuses a bad one at start, and the adapter reads it with superfluid_adapter_kit::option(args, name). A knob every runtime could take is a flag of serve instead
prepare()process-wide setup in the worker (an interpreter home, a library path)
ahead()work for a model before its first load, run after a pull by the executable that will load it (superfluid-workerd ahead); basert compiles the model's Neural Engine programs. A line to print, or none; an error leaves the work to the first load
named()the library the operator named, if any; the daemon then installs nothing
static_capabilities()the capability record any model on this runtime has; for primitives, superfluid_executor::capabilities::static_descriptor
reads()a stricter format check than info().formats, loading no weights
model_facts()architecture and published sampling defaults, without loading
sizing()what --max-context auto sizes the window from, read without loading weights: the device's budget, the weights' bytes, KV bytes per token and the trained context; check --model reports it as sizing (llamacpp, mlx)
tokenizer()an in-process tokenizer, for a daemon that links the adapter
plan(), install()what installing fetches, and how
versions(), unlisted()the versions it installs on its own, and, when some it does not list install without --untested too (basert: any release that states this build's ABI), a clause saying which, and what the rule asks of a version (basert: engine ABI <n>), which an install of an unlisted version keeps in its runtime.json as requires; superfluid runtime versions prints the clause, and counts an install newer than every listed version that installs here as one the adapter installs when its requires is what this build's rule asks
pull_options(), pull()--pull-<name> options, and fetching a model by id with the runtime's own tool

The worker executable

worker_main gives every runtime the same command line:

superfluid-worker-<id> version
superfluid-worker-<id> check [--model <path>]
superfluid-worker-<id> plan [--version V] [--backend B] [--from <url|path>] [--sha256 H] [--untested]
superfluid-worker-<id> install --into <dir> --plan <json>
superfluid-worker-<id> compat
superfluid-worker-<id> versions
superfluid-worker-<id> pull <org/model[:tag]> [--offline] [--<option> [value]]...
superfluid-worker-<id> --frames-fd N --fd-channel-fd M --model <path> [--max-context N] [--max-batch N]
superfluid-worker-<id> --link <socket> --model <path> [--max-context N] [--max-batch N]

The daemon hands a worker its two Link W streams as inherited descriptors (--frames-fd, --fd-channel-fd) on Unix. A Windows child inherits no sockets, so there the daemon listens on a unix socket of its own and the worker connects both streams to it (--link). A shared-memory segment then travels as a descriptor on the fd channel (SCM_RIGHTS) on Unix, and on Windows as a handle the worker takes out of the daemon's process, which the fd channel names.

commandexit codes
version, check0 when the runtime runs here (and reads --model); 1 otherwise. Neither loads weights
plan, install, pull0, or 1 with the reason
compat, versions0, JSON on stdout: what the adapter was built and tested for; the versions it installs on its own, each planned for this host, with unlisted (superfluid runtime versions), answered from the recipe alone. 1 with the reason on stderr, which the daemon quotes; an adapter from before them exits 2 at the unknown command
serving0 on clean close; 2 on a bad command line; 3 when the model does not load; 1 when serving fails

check prints the JSON report the daemon reads: runtime id, version, aliases, Link W versions (8), available, formats, tokenizer, devices, static capabilities, pull options, and with --model whether it reads the model and its facts.

Primitive runtime semantics

RuntimePrimitives is synchronous, one loaded model on one thread. The executor owns time, lanes and policy.

primitivecontract
describe()a RuntimeDescriptor once at open: batch and sequence limits, window, vocabulary size, page size, KV bytes per token, whether truncation is partial or head-only, export encodings, whether it samples on the device, and how much of the prefix cache it affords in its pool (cache_resident_cells, 0 for no bound) and as lossless exports out of it (cache_exported_cells, 0 for none)
seq_create, seq_free, seq_copy, seq_truncate, seq_boundarysequence handles; truncation to 0 must always succeed
seq_export, seq_importopaque state payloads; the executor wraps them in a versioned, checksummed envelope
step(feeds)one packed forward pass; logits rows back for feeds that ask
step_sampled(feeds, specs)optional device sampling; Ok(None) means "not honoured" and the executor samples on the host
shed, vocabulary, mem_counters, is_eosoptional: cache release, token bytes for grammars, memory counters, stop set

Errors are typed (PrimError): Unsupported, UnknownSeq, OutOfBoundary, Capacity, Fault(code) (stops the step's lanes, the tick continues), Fatal (the worker exits and the daemon starts a fresh one).

A runtime that samples on the device must draw by the host sampler's rules at the position the executor names, so continuations stay exact.

Conformance

suitechecks
conformance::runtime(&MyRuntime)ids, aliases, flags, formats and the check report parse as the daemon expects; reads agrees with the formats; no model loaded
conformance::worker(path, "<id>")the built worker's check, version, unknown-flag and missing-stream exits
superfluid_engine::testing::tick_contractthe tick contract as engine-independent cases (admit/prefill/decode, determinism per seed, rejections, faults, seeded readmission, ...)

Real-model tests compare shapes within a tolerance (rows_agree, top2_margin, tokens_agree), because batching and chunking move logits by rounding. They skip without a model (SUPERFLUID_TEST_GGUF, SUPERFLUID_TEST_MLX).

Packaging

scripts/build_runtime_adapters.sh <stage-dir> builds the workers a release ships and verifies each: macOS minimum version, exported tokenizer symbols, the MLX worker's weak Python link, that each worker starts with no runtime installed and says how to install it, and that it can plan a tested install. A release places workers in libexec/superfluid/; runtime install copies the worker into the install it makes.

The mock adapter, as a template

use superfluid_adapter_kit::{Loaded, Probe, Runtime, RuntimeInfo, TokenizerSource, WorkerArgs};
use superfluid_engine::{EngineConfig, MockEngine};

pub struct Mock;

impl Runtime for Mock {
    fn info(&self) -> RuntimeInfo {
        RuntimeInfo { id: "mock", aliases: &[], formats: Vec::new(), tokenizer: TokenizerSource::Engine }
    }

    fn flags(&self) -> &'static [&'static str] {
        &["--mock-record"]
    }

    fn needs_model(&self) -> bool {
        false
    }

    fn probe(&self, _args: &WorkerArgs) -> Result<Probe, String> {
        Ok(Probe { version: "mock".into(), devices: Vec::new() })
    }

    fn open(&self, args: &WorkerArgs) -> Result<Loaded, String> {
        let mut engine = MockEngine::new(EngineConfig::default());
        if let Some(record) = args.extra("--mock-record") {
            engine = engine.with_capability_descriptor(record.to_string());
        }
        Ok(Loaded::engine(engine, None))
    }
}

The llama.cpp adapter is the fuller example of a runtime loaded as a shared library; the MLX adapter of one that embeds an interpreter.