← Blog

Use case: grading code submissions from strangers, one microVM per submission

Designing an autograder or coding-exercise judge on sandboxes: per-submission isolation, no network, hidden tests copied in after the code, time and memory verdicts from the run result, output caps, and what to log.

Course autograders, coding interview platforms, programming puzzle sites and “try it” boxes in documentation all do the same thing: take code from someone you do not know, run it against tests, and report a verdict. Some of that code will be wrong, some will be hostile, and a few submissions will try to read the tests, fork-bomb the machine or open a reverse shell. This is one of the oldest untrusted-code problems, and AI has made it more common, because now the submitter is sometimes an agent generating code in a loop.

This post describes a judge built on sandboxes, one Firecracker microVM per submission. It uses the Sandscale TypeScript SDK for the calls.

Requirements

  • Isolation between submissions. One submission must not be able to see, change or slow down another, and must not be able to leave anything behind for the next.
  • Tests the submission cannot see in advance. Otherwise the cheapest “solution” is to print the expected output.
  • Verdicts that mean something. Accepted, wrong answer, time limit, memory limit, runtime error and compile error are different outcomes, and the person deserves to know which one they got.
  • Bounded cost. A submission that loops forever costs a time limit, not an evening.

One sandbox per submission

Per submission is the natural unit here. Reusing a box between submissions risks leftovers (files, background processes, modified test harnesses) that change the next verdict. Per-run versus shared sandboxes goes through the general trade-off; for judging strangers’ code, per run is the only safe choice.

A template per language, such as the built-in builtin:python, builtin:node or builtin:go, or your own with compilers preinstalled, gives every submission the same environment. Start each sandbox with:

  • network left at its default, none. A judged program has no reason to reach the internet, and with no network a reverse shell has nowhere to connect.
  • A short timeoutSeconds on the sandbox as a whole, as a backstop.
  • Labels with the submission id and problem id.

Order matters: code first, tests second

  1. Write the submission into the box with sandboxes.writeFile.
  2. Compile it if the language needs it, with its own time limit. A failure here is a compile error verdict.
  3. Only now write the test inputs into the box.
  4. Run the program once per test, with stdin from the test input and a per-test time limit.
  5. Compare output in your judge, outside the sandbox.

Keep expected outputs out of the box entirely. The comparison runs in your service, with the program’s output as data. A submission that finds the inputs in step 4 has at most seen inputs, never answers.

Turning run results into verdicts

sandboxes.run returns a RunResult with exitCode, stdout, stderr, timedOut, oomKilled and durationMs. That maps onto verdicts directly:

Run result Verdict
timedOut is true Time limit exceeded
oomKilled is true Memory limit exceeded
exitCode not 0 Runtime error (show the last lines of stderr)
exitCode 0, output matches Accepted
exitCode 0, output differs Wrong answer

Pass test input through RunOptions.stdin, which accepts bytes or a string, rather than writing it to a file and having the program open it; that keeps the program’s interface the same as most judges’. Set timeoutSeconds per run to the problem’s limit.

The memory limit is the sandbox’s memory, chosen with memoryMb when you create it within your plan’s limits. If a problem needs a lower limit than the smallest practical box, enforce it inside the box as well, for example with ulimit in the run command, and still treat oomKilled as authoritative.

Output caps

A wrong solution can print forever. Cap what you keep: compare only up to the expected length plus a margin, and keep a short tail of stderr for the person. Truncating tool output has patterns for keeping the useful part. If the submitter is an agent, this matters twice, because whatever you return will be fed back into its context.

Fork bombs, disk fills and other mischief

Inside its own microVM, a submission can exhaust that microVM’s processes, memory and disk. Other submissions are in other microVMs, with their own kernels. To your judge, a fork bomb or a disk fill shows up as a time-limit, memory or runtime-error verdict on that one submission. Resource exhaustion in agent sandboxes shows how each kind looks from the outside, so your judge can report it clearly instead of as a mysterious failure.

Stop the sandbox as soon as the verdict is in. Stopping deletes its disk, so nothing carries over.

Throughput and limits

Your plan sets boxes at once: 3 on Free, 30 on Pro and Team. A judge is a queue in front of that number. If the API refuses a create with quota_exceeded or 429, the API limits page says to wait for Retry-After on 429 and not to retry other 4xx answers unchanged. Queue, do not spin.

Stopping each box at its verdict keeps compute tied to the work actually judged. The plans reference says hours are awake vCPU-hours, counted only while a box is awake.

What to log

For each submission: the submission id, problem id, language, template, the sandbox id, each test’s verdict, durationMs and the flags. The sandbox labels link these to Sandscale’s own records. When someone disputes a verdict, you can show exactly which test, which limit and which flag decided it.

When the submitter is an agent

Agents that write code and submit it to a judge in a loop are now a common way to train and evaluate models. The design above holds, with two additions: rate limit submissions per agent, since an agent will retry far faster than a person, and never return expected outputs or hidden test inputs in feedback. An agent that can see the answers will learn the answers. Evaluating a coding agent across many tasks covers the evaluation side.

Run your agents in a box of their own

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

Start free

Keep reading