← Back to blog

Dev notes / 04

Code against the contract, not the runtime

Hide whatever runs your users' code behind a small interface, so you can swap it without rewriting your product.

Diagram for Code against the contract, not the runtime
Diagram · Code against the contract, not the runtime

The first version of “run user code” usually calls Docker straight from a route handler. Then you want gVisor, or a hosted service, or just a fake for tests, and Docker calls have spread to six files.

🔌 Keep the interface small

Start from what your product needs, not from what the runtime offers. Most apps only need four things: start a job, check on it, cancel it, clean it up. In Rust that might be:

trait Executor {
async fn start(&self, job: Job) -> Result<Handle, ExecError>;
async fn status(&self, handle: &Handle) -> Result<Status, ExecError>;
async fn cancel(&self, handle: &Handle) -> Result<(), ExecError>;
async fn cleanup(&self, handle: Handle) -> Result<(), ExecError>;
}

Container IDs, VM configs, and mount points stay inside the implementation. Your handlers never see them.

🧪 Use a fake in your tests

With an interface like that, your tests can run against a fake executor that returns whatever status you want, instantly. You can test “the job timed out” or “cancel arrived too late” without starting a single container.

🔒 An interface doesn’t make anything safe

A clean trait keeps your code organized. It doesn’t isolate anything. Whatever sits behind it still has to pass its own security tests (see Test your sandbox before you trust it).

⚠️ How this looks in mPaka

If you call the mPaka API you get this split for free: you deal with executions and statuses and never with the runtime. Today the only executor wired in locally is mock_no_code_execution, which doesn’t run your code. Your integration should keep working as-is once a real one is admitted.