Platform. This teaching runner requires an environment that supports the requested POSIX resource limits, including a settable address-space limit. Use Linux for the supplied exercises. A rejected resource limit prevents the child from starting; this is an environment failure, not a verdict on the candidate program.
Platform. This teaching runner requires an environment that supports the requested POSIX resource limits, including a settable address-space limit. Use Linux for the supplied exercises. A rejected resource limit prevents the child from starting; this is an environment failure, not a verdict on the candidate program.
Running generated code means running code nobody has read. The runner has to survive programs that loop forever, allocate without limit, crash, print noise, try to touch files, or fail to parse. It should also return results in a form that cannot be corrupted by the program under test.
The parent writes a JSON request to a fresh child interpreter started with python -I (isolated mode, empty environment). The child sets address-space, CPU-time and file-size limits with the resource module, replaces standard output and error with the null device while keeping a duplicate of the original output descriptor for its answer, installs an alarm that raises an exception when a single call takes more than 0.2 seconds, and compiles the candidate in a namespace whose built-in functions are a short allowlist: arithmetic, containers, a few exception classes, and no open, __import__, eval, exec or print. Each input is deep-copied before the call so a program that mutates its argument cannot affect the next test. The parent kills the whole run after a wall-clock limit. Results are cached by a hash of the program and inputs, which is what keeps the experiments inside the time budget.
Every misbehaviour is turned into a recorded outcome. The infinite loop ends as a timeout on each input (after the first timeout the child marks the remaining inputs as timeouts rather than waiting again). The memory bomb becomes a MemoryError instead of exhausting the machine. import os fails with an ImportError because the namespace has no import hook, opening a file and printing both fail with NameError because those names are absent, and a syntax error is reported as a load error. The program that is busy but finite passes, so the limits are not so tight that they reject honest work.
This is not a security boundary. It shows that a careful runner is cheap to build and that most accidents are contained. It does not contain a determined adversary. Restricted-built-in sandboxes in CPython are known to be escapable through the object graph of the language (stated from memory, not proved here; the Python documentation warns against treating them as secure), the resource limits do not cover network access, other processes of the same user, or kernel bugs, and I deliberately set no process-count limit because it would apply to the whole user account. Execute untrusted code only inside a container or virtual machine with no network, no credentials and a throwaway file system, and treat the Python-level restrictions here as a second layer that makes accidents cheap. A reviewer should read the companion book example before deploying anything like it.
Continue with the book
Explore the full treatment, worked examples, and exercises in Applied AI for Generation and Control.
Companion article for Applied AI for Generation and Control, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
