Skip to main content

CodeShield: Autonomous Code Execution Engine

CI Python License: MIT Code Style: Ruff PyPI PyPI Downloads

Deterministic, Isolated, and Self-Healing Python Execution Runtime for AI Agents.

This open-source engine executes Python code generated by LLMs inside a disposable, isolated sandbox, validates it statically with the Python ast module, and recovers from runtime errors through a deterministic self-healing loop backed by local heuristics and plug-and-play LLM-guided patch generation (OpenAI, Anthropic Claude, DeepSeek, Ollama, Google Gemini).

Comparison

Feature Vanilla subprocess Docker Container CodeShield (This Engine)
Startup Overhead ~5 – 10 ms ~1,500 – 3,000 ms Sub-second (~30–250 ms via uv)
Isolation Mechanism None (Host Process) Container Namespaces / cgroups Ephemeral Virtualenv + Env Allowlist + Scoped cwd + Tree Kill
AST Security Gate ❌ None ❌ None ✅ Fail-closed AST gate (aliases, dunders, blocked modules, dangerous calls)
Silent Failure Detection ❌ None ❌ None ✅ Regex scanning for empty DataFrames/NaNs
Self-Healing Loop ❌ None ❌ None ✅ 3-Tier Traceback Diagnosis + LLM Patch (Any Provider)

Architecture

[LLM Generated Code]
        │
        ▼
[AST Static Gate] ──(Syntax/Security Violation)──► [Validation Error Report]
        │ (Passed)
        ▼
[uv Isolated Sandbox] ──(Runtime Error/Silent Failure)──► [Traceback Classifier]
        │                                                          │
        │ (Clean Execution: exit 0)                                ▼
        ▼                                               [LLM Self-Healing (Any Provider) / Local Heuristic]
[Verified Output (JSON)] ◄──(AST Validated Patch)─────────────────┘

Key Features

1. Ephemeral Sandboxing with Hardened Subprocess Controls

  • Primary: uv venv for ultra-fast environment creation and package installation, with native python -m venv + pip fallback.
  • Minimal Environment Allowlist: host secrets (GEMINI_API_KEY, cloud tokens, .env values) are never inherited by the child process. Pass explicit variables via CodeExecutionRequest(env={...}) or SubprocessRunner(env_allowlist=[...]).
  • Workspace & Resource Guardrails: scripts execute with cwd and temp directories pinned inside the disposable workspace, strict path-traversal & PEP 508 package validation, bounded stdout/stderr capture (max_output_bytes), redacted host paths in tracebacks, and process-tree termination on timeout.

2. Fail-Closed AST Security Gates

The engine parses every snippet (and every LLM-generated patch) with the standard ast module and fails closed on:

  • SyntaxErrors before execution.
  • Bare except: / except Exception: / except BaseException: handlers (including tuple handlers).
  • Dangerous builtins & reflection (eval(), exec(), compile(), __import__(), getattr(), globals(), etc.) even when aliased or reassigned.
  • Blocked high-risk modules (subprocess, ctypes, importlib, pickle, socket, shutil, builtins, etc.) and dangerous os/sys APIs (os.system, os.popen, os.environ, sys.modules, sys._getframe).
  • Reflection dunder traversal (__subclasses__, __builtins__, __globals__, __class__, __bases__, etc.).

3. Silent Failure Detection

Even when a process exits with 0, the engine flags suspicious output patterns such as:

  • empty DataFrame
  • all NaN
  • Traceback
  • Pipeline failed
  • Fatal Error

4. Model-Agnostic Self-Healing Loop

AST Validation ──► Sandbox Execution ──► Traceback Classification ──► Patch ──► Re-run
                                      (3 attempts max)
  • Local heuristic fallback: handles NameError, ImportError, ModuleNotFoundError by injecting safe imports or placeholder definitions.
  • LLM-guided healing: when an LLM is configured (built-in Gemini 4 by default, or any custom provider via patch_generator), it asks the model for a corrected version of the code, validates it with the AST gate, and re-executes the patched snippet.

Quickstart

Installation

# Install from PyPI
pip install codeshield-runtime

# Install with all extras (LLM + Dev tools)
pip install "codeshield-runtime[llm,dev]"

# Or clone for development
git clone https://github.com/AlgorithmicMind/codeshield.git
cd codeshield

# With uv (recommended)
uv venv
uv pip install -e ".[test,lint,llm,dev]"

# Or with pip
python -m venv .venv
.venv\Scripts\activate  # Windows
pip install -e ".[test,lint,llm,dev]"

Offline Usage (No API Key)

from codeshield import SelfHealingEngine

# The sandbox is created and destroyed automatically on every ``run`` call.
engine = SelfHealingEngine(use_llm=False)
result, diagnosis = engine.run("print('hello world')")
print(result.stdout)

# Use a ``with`` block to reuse a single sandbox across multiple runs.
with SelfHealingEngine(use_llm=False) as reusable_engine:
    first, _ = reusable_engine.run("print(1 + 1)")
    second, _ = reusable_engine.run("print(2 + 2)")
    print(first.stdout, second.stdout)

Model-Agnostic Self-Healing (Plug-and-Play)

CodeShield is not locked into a single LLM. Pass any Python callable as the patch_generator to use OpenAI, Anthropic Claude, DeepSeek, Ollama, LiteLLM or your own service:

from codeshield import SelfHealingEngine


def custom_llm_patcher(code: str, diagnosis) -> str:
    # Compatible with any frontier provider: GPT-5.6, Claude Sonnet 5, DeepSeek V4, Ollama
    response = client.chat.completions.create(
        model="gpt-5.6-luna",  # or "claude-sonnet-5", "deepseek-v4-flash"
        messages=[
            {
                "role": "user",
                "content": f"Fix this code:\n{code}\nError: {diagnosis.message}",
            }
        ],
    )
    return response.choices[0].message.content


engine = SelfHealingEngine(patch_generator=custom_llm_patcher)

# Broken code -> AST gate -> sandbox execution -> traceback classification ->
# custom patch -> AST re-validation -> re-execution, all in a single call.
result, diagnosis = engine.run('print("Result: " + 42)')
print(result.stdout)  # Result: 42

Zero-Config Self-Healing with Gemini 4

For the built-in zero-config experience, create a .env file from .env.example:

GEMINI_API_KEY=your_key_here
GEMINI_MODEL=gemini-4-hydrogen
from dotenv import load_dotenv

from codeshield import SelfHealingEngine

load_dotenv()

engine = SelfHealingEngine()
with engine:
    result, diagnosis = engine.run('print("Result: " + 42)')
    print(result.stdout)  # Result: 42

Run the included demo:

python demo.py

CLI Usage

Execute any Python file directly from the terminal with the built-in CLI:

python -m codeshield run script.py
python -m codeshield run script.py --timeout 30
python -m codeshield run script.py --llm        # try LLM self-healing if configured
python -m codeshield run script.py --no-llm     # force local fallback

🤖 Agent Tool Integration (LangChain, CrewAI, OpenAI, Gen AI)

from codeshield import create_code_execution_tool

# Standard usage: built-in Gemini healing when configured, local heuristic otherwise
tools = [create_code_execution_tool()]

# Or bring your own model: the patcher is forwarded to the internal engine
tools = [create_code_execution_tool(patch_generator=custom_llm_patcher)]

create_code_execution_tool() returns a ready-to-register execute_python_code(code: str) -> str function. It runs the provided Python in a self-healing sandbox and returns either the stdout or a structured error report with error_type and stderr.

The callable exposes real type hints and a Google-style docstring, so any SDK that builds a function schema from a plain Python callable can register it directly.


Public API

Everything is re-exported at the package root, so imports never need internal submodules:

from codeshield import (
    DEFAULT_GEMINI_MODEL,
    ASTSecurityError,
    CodeExecutionRequest,
    ErrorDiagnosis,
    ExecutionResult,
    GeminiPatchGenerator,
    LLMPatchError,
    PatchProposal,
    SandboxError,
    SandboxManager,
    SelfHealingEngine,
    SelfHealingError,
    SubprocessRunner,
    SubprocessRunnerError,
    TracebackClassifier,
    ValidationReport,
    create_code_execution_tool,
    validate_syntax_and_safety,
)

ASTSecurityError subclasses SelfHealingError and is raised whenever the static AST gate blocks either the original snippet or a generated patch.

Explicit Environment Variables

Host environment variables (including API keys loaded from .env) are never leaked into executed code. To expose specific variables to a snippet, pass them explicitly:

from codeshield import CodeExecutionRequest, SelfHealingEngine

request = CodeExecutionRequest(
    code="import json\nprint(json.dumps({'status': 'ok'}))",
    env={"APP_MODE": "sandbox"},
)
with SelfHealingEngine(use_llm=False) as engine:
    result, _ = engine.run(request)

Security Model & Threat Boundaries

CodeShield provides application-layer defense-in-depth tailored for LLM-generated data, math, and automation snippets (fail-closed AST checks, env allowlisting, scoped workspace cwd, path traversal & pip-flag validation, bounded output capture, host-path redaction, and process-tree termination).

Threat boundary: Ephemeral virtualenvs and static AST inspection do not provide OS kernel isolation (namespaces, seccomp, cgroups, or network firewalls). For untrusted or adversarial multi-tenant workloads, run CodeShield inside a hardened container or microVM (Docker with --network=none --read-only, gVisor, or Firecracker).

See SECURITY.md for the full security policy and private vulnerability disclosure instructions.


Verified Examples

The examples/ folder contains ready-to-run recipes that have been executed and verified:

  • 01_basic_sandboxing.py: isolated execution with timing measurements.
  • 02_security_gatekeeper.py: AST rejection of unsafe code.
  • 03_llm_healing_workflow.py: self-healing workflow with an LLM or local fallback.
  • 04_agent_tool_dropin.py: dual-phase agentic trace, printing agent thought, tool call, sandbox runtime and final answer for both a legitimate analytics round and a security-defense round where the AST gate blocks a shell escape.
  • 05_custom_llm_openai_compatible.py: model-agnostic, API-key-free self-healing with a custom patch_generator.
python examples/01_basic_sandboxing.py
python examples/02_security_gatekeeper.py
python examples/03_llm_healing_workflow.py
python examples/04_agent_tool_dropin.py
python examples/05_custom_llm_openai_compatible.py

Running Tests & Lint

The suite currently has 110 tests with >85% code coverage on src/codeshield.

ruff check src tests examples demo.py
pytest tests -v --cov=src/codeshield

Enterprise Architecture & Custom Deployments

This repository ships the core execution and healing engine. For production multi-tenant deployments, the enterprise extension adds:

  • Multi-tenant orchestrator with queue-based job scheduling.
  • PostgreSQL state persistence for execution history, audit trails and replay.
  • Automated billing and token governance (cost caps per tenant, per-execution budgets).
  • Prometheus/Grafana observability, RBAC, and signed artifact provenance.
  • SLA-backed support and custom agentic architecture consulting.

Want the production-grade version or a tailored integration for your platform?

We offer enterprise licensing, dedicated onboarding and custom agentic-architecture consulting.


License

This project is licensed under the MIT License. See LICENSE for details.

Metadata

Release files for codeshield-runtime 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codeshield-runtime 0.2.0
File Size Uploaded Provenance Actions
codeshield_runtime-0.2.0.tar.gz 37.6 kB Without provenance Details for codeshield_runtime-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for codeshield-runtime 0.2.0
File Interpreter ABI Platform Actions
codeshield_runtime-0.2.0-py3-none-any.whl Python 3 none any Details for codeshield_runtime-0.2.0-py3-none-any.whl

Total release size: 70.6 kB

Release files / codeshield_runtime-0.2.0.tar.gz

Download URL codeshield_runtime-0.2.0.tar.gz
Size 37.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2f6dd9b0e086cedfc5fa4d96786857abb19efa53a880565f680afe49115f180f
BLAKE2b-256 checksum
How to use checksums
469891d9a6b56893c5307bda78c884c973e1e8f4afc010990fcf44d9dd022944
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.4

Release files / codeshield_runtime-0.2.0-py3-none-any.whl

Download URL codeshield_runtime-0.2.0-py3-none-any.whl
Size 33.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ca710a775a27419042c73a51dc918d053625de4702616e101fd55add641fcba
BLAKE2b-256 checksum
How to use checksums
8cd8c1aa0a7e2ce8f2f4d0af673ddaf99f1a1164bc334aa3e9e62e4241b8a15c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.4

Security for codeshield-runtime 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page