Capstone Lesson 26: Sandbox Runner with Denylist and Path Jail

The verification gate decides whether a tool call should run. The sandbox decides what happens when it does. This lesson ships a subprocess runner that refuses dangerous executables, refuses dangerous argv shapes, jails every file path to a project root, truncates oversized output, and kills runaway processes on a wall-clock timeout. It is the second of two layers that sit between the model and the operating system.

Build 1.5 hr Python (stdlib)

Prerequisites: Phase 19 · 25 (verification gates and observation budget), Phase 14 · 33 (instructions as constraints), Phase 14 · 38 (verification gates)

#Learning Objectives

  • Build a Sandbox class wrapping subprocess.run with timeout, capture, and truncation.
  • Refuse a command by name against a denylist and by structure against an argv inspector.
  • Refuse any path argument that resolves outside a declared project root.
  • Refuse shell metacharacters when shell mode is off.
  • Return a structured SandboxResult that downstream observability and the eval harness can ingest.
Full Access lesson

Keep going. The rest of this lesson is one step away.

Unlock all 503 lessons, video, code, quizzes and every future update. Pay in your own currency with Paystack.

The text and code of this lesson are based on AI Engineering from Scratch by Rohit Ghumare, MIT licensed.