codeshield-runtime 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codeshield_runtime-0.1.0/.gitignore +150 -0
- codeshield_runtime-0.1.0/LICENSE +21 -0
- codeshield_runtime-0.1.0/PKG-INFO +277 -0
- codeshield_runtime-0.1.0/README.md +241 -0
- codeshield_runtime-0.1.0/pyproject.toml +110 -0
- codeshield_runtime-0.1.0/src/codeshield/__init__.py +17 -0
- codeshield_runtime-0.1.0/src/codeshield/__main__.py +10 -0
- codeshield_runtime-0.1.0/src/codeshield/analyzer.py +151 -0
- codeshield_runtime-0.1.0/src/codeshield/classifier.py +163 -0
- codeshield_runtime-0.1.0/src/codeshield/cli.py +101 -0
- codeshield_runtime-0.1.0/src/codeshield/environment.py +256 -0
- codeshield_runtime-0.1.0/src/codeshield/loop.py +420 -0
- codeshield_runtime-0.1.0/src/codeshield/runner.py +221 -0
- codeshield_runtime-0.1.0/src/codeshield/schemas.py +120 -0
- codeshield_runtime-0.1.0/src/codeshield/tools.py +76 -0
- codeshield_runtime-0.1.0/tests/__init__.py +1 -0
- codeshield_runtime-0.1.0/tests/test_cli.py +118 -0
- codeshield_runtime-0.1.0/tests/test_coverage.py +325 -0
- codeshield_runtime-0.1.0/tests/test_engine.py +234 -0
- codeshield_runtime-0.1.0/tests/test_tools.py +37 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
*.manifest
|
|
31
|
+
*.spec
|
|
32
|
+
|
|
33
|
+
# Installer logs
|
|
34
|
+
pip-log.txt
|
|
35
|
+
pip-delete-this-directory.txt
|
|
36
|
+
|
|
37
|
+
# Unit test / coverage reports
|
|
38
|
+
htmlcov/
|
|
39
|
+
.tox/
|
|
40
|
+
.nox/
|
|
41
|
+
.coverage
|
|
42
|
+
.coverage.*
|
|
43
|
+
.cache
|
|
44
|
+
nosetests.xml
|
|
45
|
+
coverage.xml
|
|
46
|
+
*.cover
|
|
47
|
+
*.py,cover
|
|
48
|
+
.hypothesis/
|
|
49
|
+
.pytest_cache/
|
|
50
|
+
cover/
|
|
51
|
+
|
|
52
|
+
# Translations
|
|
53
|
+
*.mo
|
|
54
|
+
*.pot
|
|
55
|
+
|
|
56
|
+
# Django stuff:
|
|
57
|
+
*.log
|
|
58
|
+
local_settings.py
|
|
59
|
+
db.sqlite3
|
|
60
|
+
db.sqlite3-journal
|
|
61
|
+
|
|
62
|
+
# Flask stuff:
|
|
63
|
+
instance/
|
|
64
|
+
.webassets-cache
|
|
65
|
+
|
|
66
|
+
# Scrapy stuff:
|
|
67
|
+
.scrapy
|
|
68
|
+
|
|
69
|
+
# Sphinx documentation
|
|
70
|
+
docs/_build/
|
|
71
|
+
|
|
72
|
+
# PyBuilder
|
|
73
|
+
.pybuilder/
|
|
74
|
+
target/
|
|
75
|
+
|
|
76
|
+
# Jupyter Notebook
|
|
77
|
+
.ipynb_checkpoints
|
|
78
|
+
|
|
79
|
+
# IPython
|
|
80
|
+
profile_default/
|
|
81
|
+
ipython_config.py
|
|
82
|
+
|
|
83
|
+
# pyenv
|
|
84
|
+
.python-version
|
|
85
|
+
|
|
86
|
+
# pipenv
|
|
87
|
+
Pipfile.lock
|
|
88
|
+
|
|
89
|
+
# poetry
|
|
90
|
+
poetry.lock
|
|
91
|
+
|
|
92
|
+
# pdm
|
|
93
|
+
.pdm.toml
|
|
94
|
+
.pdm-python
|
|
95
|
+
.pdm-build/
|
|
96
|
+
|
|
97
|
+
# PEP 582
|
|
98
|
+
__pypackages__/
|
|
99
|
+
|
|
100
|
+
# Celery stuff
|
|
101
|
+
celerybeat-schedule
|
|
102
|
+
celerybeat.pid
|
|
103
|
+
|
|
104
|
+
# SageMath parsed files
|
|
105
|
+
*.sage.py
|
|
106
|
+
|
|
107
|
+
# Environments
|
|
108
|
+
.env
|
|
109
|
+
.venv
|
|
110
|
+
env/
|
|
111
|
+
venv/
|
|
112
|
+
ENV/
|
|
113
|
+
env.bak/
|
|
114
|
+
venv.bak/
|
|
115
|
+
|
|
116
|
+
# Spyder project settings
|
|
117
|
+
.spyderproject
|
|
118
|
+
.spyproject
|
|
119
|
+
|
|
120
|
+
# Rope project settings
|
|
121
|
+
.ropeproject
|
|
122
|
+
|
|
123
|
+
# mkdocs documentation
|
|
124
|
+
/site
|
|
125
|
+
|
|
126
|
+
# mypy
|
|
127
|
+
.mypy_cache/
|
|
128
|
+
.dmypy.json
|
|
129
|
+
dmypy.json
|
|
130
|
+
|
|
131
|
+
# Pyre type checker
|
|
132
|
+
.pyre/
|
|
133
|
+
|
|
134
|
+
# pytype static type analyzer
|
|
135
|
+
.pytype/
|
|
136
|
+
|
|
137
|
+
# Cython debug symbols
|
|
138
|
+
cython_debug/
|
|
139
|
+
|
|
140
|
+
# PyCharm
|
|
141
|
+
.idea/
|
|
142
|
+
|
|
143
|
+
# VS Code
|
|
144
|
+
.vscode/
|
|
145
|
+
|
|
146
|
+
# Local execution engine sandboxes and workspaces
|
|
147
|
+
.sandboxes/
|
|
148
|
+
workspaces/
|
|
149
|
+
tmp/
|
|
150
|
+
*.sandbox
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Autonomous Code Execution Engine contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: codeshield-runtime
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A secure, isolated, and self-healing Python code execution engine powered by uv and AST analysis.
|
|
5
|
+
Project-URL: Homepage, https://github.com/AlgorithmicMind/codeshield
|
|
6
|
+
Project-URL: Repository, https://github.com/AlgorithmicMind/codeshield
|
|
7
|
+
Project-URL: Issues, https://github.com/AlgorithmicMind/codeshield/issues
|
|
8
|
+
Author: Senior Python Core Engineer
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ast,code,execution,sandbox,self-healing,uv
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Requires-Dist: pydantic>=2.0
|
|
23
|
+
Requires-Dist: tenacity>=8.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: build>=1.0.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: twine>=5.0.0; extra == 'dev'
|
|
27
|
+
Provides-Extra: lint
|
|
28
|
+
Requires-Dist: ruff>=0.5.0; extra == 'lint'
|
|
29
|
+
Provides-Extra: llm
|
|
30
|
+
Requires-Dist: google-genai>=0.1.0; extra == 'llm'
|
|
31
|
+
Requires-Dist: python-dotenv>=1.0.0; extra == 'llm'
|
|
32
|
+
Provides-Extra: test
|
|
33
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'test'
|
|
34
|
+
Requires-Dist: pytest>=7.0; extra == 'test'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# CodeShield: Autonomous Code Execution Engine
|
|
38
|
+
|
|
39
|
+
[](https://github.com/AlgorithmicMind/codeshield/actions)
|
|
40
|
+
[](https://www.python.org/)
|
|
41
|
+
[](LICENSE)
|
|
42
|
+
[](https://github.com/astral-sh/ruff)
|
|
43
|
+
|
|
44
|
+
> Deterministic, Isolated, and Self-Healing Python Execution Runtime for AI Agents.
|
|
45
|
+
|
|
46
|
+
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).
|
|
47
|
+
|
|
48
|
+
## Comparison
|
|
49
|
+
|
|
50
|
+
| Feature | Vanilla `subprocess` | Docker Container | CodeShield (This Engine) |
|
|
51
|
+
| :--- | :--- | :--- | :--- |
|
|
52
|
+
| **Startup Overhead** | ~5 – 10 ms | ~1,500 – 3,000 ms | **Sub-second (~30–250 ms via `uv`)** |
|
|
53
|
+
| **Isolation Mechanism** | None (Host Process) | Container Namespaces / cgroups | **Ephemeral Virtualenv (`tempfile` + `uv`)** |
|
|
54
|
+
| **AST Security Gate** | ❌ None | ❌ None | **✅ Static AST inspection (`os.system`, `eval`)** |
|
|
55
|
+
| **Silent Failure Detection** | ❌ None | ❌ None | **✅ Regex scanning for empty DataFrames/NaNs** |
|
|
56
|
+
| **Self-Healing Loop** | ❌ None | ❌ None | **✅ 3-Tier Traceback Diagnosis + LLM Patch (Any Provider)** |
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Architecture
|
|
61
|
+
|
|
62
|
+
```text
|
|
63
|
+
[LLM Generated Code]
|
|
64
|
+
│
|
|
65
|
+
▼
|
|
66
|
+
[AST Static Gate] ──(Syntax/Security Violation)──► [Validation Error Report]
|
|
67
|
+
│ (Passed)
|
|
68
|
+
▼
|
|
69
|
+
[uv Isolated Sandbox] ──(Runtime Error/Silent Failure)──► [Traceback Classifier]
|
|
70
|
+
│ │
|
|
71
|
+
│ (Clean Execution: exit 0) ▼
|
|
72
|
+
▼ [LLM Self-Healing (Any Provider) / Local Heuristic]
|
|
73
|
+
[Verified Output (JSON)] ◄──(AST Validated Patch)─────────────────┘
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Key Features
|
|
79
|
+
|
|
80
|
+
### 1. Ephemeral Sandboxing with Dual-Mode Backend
|
|
81
|
+
|
|
82
|
+
- **Primary**: `uv venv` for ultra-fast environment creation and package installation.
|
|
83
|
+
- **Fallback**: native `python -m venv` + `pip` when `uv` is unavailable, so the engine works out of the box on any machine.
|
|
84
|
+
- Each execution lands in its own temporary workspace that is destroyed after use.
|
|
85
|
+
|
|
86
|
+
### 2. Deterministic AST Security Gates
|
|
87
|
+
|
|
88
|
+
The engine parses every snippet with the standard `ast` module and rejects:
|
|
89
|
+
|
|
90
|
+
- `SyntaxError`s before execution.
|
|
91
|
+
- Bare `except:` / `except Exception:` / `except BaseException:` handlers.
|
|
92
|
+
- Calls to dangerous parametrizable functions: `eval()`, `exec()`, `compile()`.
|
|
93
|
+
- Calls to system/subprocess primitives: `os.system()`, `subprocess.call()`, `subprocess.run()`, `subprocess.Popen()`.
|
|
94
|
+
|
|
95
|
+
### 3. Silent Failure Detection
|
|
96
|
+
|
|
97
|
+
Even when a process exits with `0`, the engine flags suspicious output patterns such as:
|
|
98
|
+
|
|
99
|
+
- `empty DataFrame`
|
|
100
|
+
- `all NaN`
|
|
101
|
+
- `Traceback`
|
|
102
|
+
- `Pipeline failed`
|
|
103
|
+
- `Fatal Error`
|
|
104
|
+
|
|
105
|
+
### 4. Model-Agnostic Self-Healing Loop
|
|
106
|
+
|
|
107
|
+
```text
|
|
108
|
+
AST Validation ──► Sandbox Execution ──► Traceback Classification ──► Patch ──► Re-run
|
|
109
|
+
(3 attempts max)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- **Local heuristic fallback**: handles `NameError`, `ImportError`, `ModuleNotFoundError` by injecting safe imports or placeholder definitions.
|
|
113
|
+
- **LLM-guided healing**: when an LLM is configured (built-in Gemini Flash 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.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Quickstart
|
|
118
|
+
|
|
119
|
+
### Installation
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
# Install from PyPI
|
|
123
|
+
pip install codeshield-runtime
|
|
124
|
+
|
|
125
|
+
# Install with all extras (LLM + Dev tools)
|
|
126
|
+
pip install "codeshield-runtime[llm,dev]"
|
|
127
|
+
|
|
128
|
+
# Or clone for development
|
|
129
|
+
git clone https://github.com/AlgorithmicMind/codeshield.git
|
|
130
|
+
cd codeshield
|
|
131
|
+
|
|
132
|
+
# With uv (recommended)
|
|
133
|
+
uv venv
|
|
134
|
+
uv pip install -e ".[test,lint,llm,dev]"
|
|
135
|
+
|
|
136
|
+
# Or with pip
|
|
137
|
+
python -m venv .venv
|
|
138
|
+
.venv\Scripts\activate # Windows
|
|
139
|
+
pip install -e ".[test,lint,llm,dev]"
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Offline Usage (No API Key)
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from codeshield.loop import SelfHealingEngine
|
|
146
|
+
|
|
147
|
+
engine = SelfHealingEngine(use_llm=False)
|
|
148
|
+
with engine:
|
|
149
|
+
result, diagnosis = engine.run("print('hello world')")
|
|
150
|
+
print(result.stdout)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Model-Agnostic Self-Healing (Plug-and-Play)
|
|
154
|
+
|
|
155
|
+
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:
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
from codeshield.loop import SelfHealingEngine
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def custom_openai_patcher(code: str, diagnosis) -> str:
|
|
162
|
+
# Any LLM call (OpenAI, Anthropic, DeepSeek, Ollama, LiteLLM)
|
|
163
|
+
response = client.chat.completions.create(
|
|
164
|
+
model="gpt-4o-mini",
|
|
165
|
+
messages=[
|
|
166
|
+
{
|
|
167
|
+
"role": "user",
|
|
168
|
+
"content": f"Fix this code:\n{code}\nError: {diagnosis.message}",
|
|
169
|
+
}
|
|
170
|
+
],
|
|
171
|
+
)
|
|
172
|
+
return response.choices[0].message.content
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
engine = SelfHealingEngine(patch_generator=custom_openai_patcher)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Zero-Config Self-Healing with Gemini Flash
|
|
179
|
+
|
|
180
|
+
For the built-in zero-config experience, create a `.env` file from `.env.example`:
|
|
181
|
+
|
|
182
|
+
```text
|
|
183
|
+
GEMINI_API_KEY=your_key_here
|
|
184
|
+
GEMINI_MODEL=gemini-3.7-flash
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from dotenv import load_dotenv
|
|
189
|
+
from codeshield.loop import SelfHealingEngine
|
|
190
|
+
|
|
191
|
+
load_dotenv()
|
|
192
|
+
|
|
193
|
+
engine = SelfHealingEngine()
|
|
194
|
+
with engine:
|
|
195
|
+
result, diagnosis = engine.run('print("Result: " + 42)')
|
|
196
|
+
print(result.stdout) # Result: 42
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Run the included demo:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
python demo.py
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### CLI Usage
|
|
206
|
+
|
|
207
|
+
Execute any Python file directly from the terminal with the built-in CLI:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
python -m codeshield run script.py
|
|
211
|
+
python -m codeshield run script.py --timeout 30
|
|
212
|
+
python -m codeshield run script.py --llm # try LLM self-healing if configured
|
|
213
|
+
python -m codeshield run script.py --no-llm # force local fallback
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## 🤖 Agent Tool Integration (LangChain, CrewAI, OpenAI, Gen AI)
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
from codeshield import create_code_execution_tool
|
|
220
|
+
|
|
221
|
+
# Pass the tool directly to your agent
|
|
222
|
+
tools = [create_code_execution_tool()]
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
`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`.
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Verified Examples
|
|
230
|
+
|
|
231
|
+
The `examples/` folder contains ready-to-run recipes that have been executed and verified:
|
|
232
|
+
|
|
233
|
+
- `01_basic_sandboxing.py`: isolated execution with timing measurements.
|
|
234
|
+
- `02_security_gatekeeper.py`: AST rejection of unsafe code.
|
|
235
|
+
- `03_llm_healing_workflow.py`: self-healing workflow with an LLM or local fallback.
|
|
236
|
+
- `04_agent_tool_dropin.py`: end-to-end agentic tool-calling workflow with dynamic code generation.
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
python examples/01_basic_sandboxing.py
|
|
240
|
+
python examples/02_security_gatekeeper.py
|
|
241
|
+
python examples/03_llm_healing_workflow.py
|
|
242
|
+
python examples/04_agent_tool_dropin.py
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Running Tests & Lint
|
|
246
|
+
|
|
247
|
+
The suite currently has **50 tests** with **>82% code coverage** on `src/codeshield`.
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
ruff check src tests examples
|
|
251
|
+
pytest tests -v --cov=src/codeshield
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Enterprise Architecture & Custom Deployments
|
|
257
|
+
|
|
258
|
+
This repository ships the **core execution and healing engine**. For production multi-tenant deployments, the enterprise extension adds:
|
|
259
|
+
|
|
260
|
+
- **Multi-tenant orchestrator** with queue-based job scheduling.
|
|
261
|
+
- **PostgreSQL state persistence** for execution history, audit trails and replay.
|
|
262
|
+
- **Automated billing and token governance** (cost caps per tenant, per-execution budgets).
|
|
263
|
+
- **Prometheus/Grafana observability**, RBAC, and signed artifact provenance.
|
|
264
|
+
- **SLA-backed support** and custom agentic architecture consulting.
|
|
265
|
+
|
|
266
|
+
**Want the production-grade version or a tailored integration for your platform?**
|
|
267
|
+
|
|
268
|
+
- [Open a GitHub issue](https://github.com/AlgorithmicMind/codeshield/issues)
|
|
269
|
+
- [Connect on LinkedIn](https://www.linkedin.com/in/pedro-castejon-jodar/)
|
|
270
|
+
|
|
271
|
+
We offer enterprise licensing, dedicated onboarding and custom agentic-architecture consulting.
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## License
|
|
276
|
+
|
|
277
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# CodeShield: Autonomous Code Execution Engine
|
|
2
|
+
|
|
3
|
+
[](https://github.com/AlgorithmicMind/codeshield/actions)
|
|
4
|
+
[](https://www.python.org/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://github.com/astral-sh/ruff)
|
|
7
|
+
|
|
8
|
+
> Deterministic, Isolated, and Self-Healing Python Execution Runtime for AI Agents.
|
|
9
|
+
|
|
10
|
+
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).
|
|
11
|
+
|
|
12
|
+
## Comparison
|
|
13
|
+
|
|
14
|
+
| Feature | Vanilla `subprocess` | Docker Container | CodeShield (This Engine) |
|
|
15
|
+
| :--- | :--- | :--- | :--- |
|
|
16
|
+
| **Startup Overhead** | ~5 – 10 ms | ~1,500 – 3,000 ms | **Sub-second (~30–250 ms via `uv`)** |
|
|
17
|
+
| **Isolation Mechanism** | None (Host Process) | Container Namespaces / cgroups | **Ephemeral Virtualenv (`tempfile` + `uv`)** |
|
|
18
|
+
| **AST Security Gate** | ❌ None | ❌ None | **✅ Static AST inspection (`os.system`, `eval`)** |
|
|
19
|
+
| **Silent Failure Detection** | ❌ None | ❌ None | **✅ Regex scanning for empty DataFrames/NaNs** |
|
|
20
|
+
| **Self-Healing Loop** | ❌ None | ❌ None | **✅ 3-Tier Traceback Diagnosis + LLM Patch (Any Provider)** |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Architecture
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
[LLM Generated Code]
|
|
28
|
+
│
|
|
29
|
+
▼
|
|
30
|
+
[AST Static Gate] ──(Syntax/Security Violation)──► [Validation Error Report]
|
|
31
|
+
│ (Passed)
|
|
32
|
+
▼
|
|
33
|
+
[uv Isolated Sandbox] ──(Runtime Error/Silent Failure)──► [Traceback Classifier]
|
|
34
|
+
│ │
|
|
35
|
+
│ (Clean Execution: exit 0) ▼
|
|
36
|
+
▼ [LLM Self-Healing (Any Provider) / Local Heuristic]
|
|
37
|
+
[Verified Output (JSON)] ◄──(AST Validated Patch)─────────────────┘
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Key Features
|
|
43
|
+
|
|
44
|
+
### 1. Ephemeral Sandboxing with Dual-Mode Backend
|
|
45
|
+
|
|
46
|
+
- **Primary**: `uv venv` for ultra-fast environment creation and package installation.
|
|
47
|
+
- **Fallback**: native `python -m venv` + `pip` when `uv` is unavailable, so the engine works out of the box on any machine.
|
|
48
|
+
- Each execution lands in its own temporary workspace that is destroyed after use.
|
|
49
|
+
|
|
50
|
+
### 2. Deterministic AST Security Gates
|
|
51
|
+
|
|
52
|
+
The engine parses every snippet with the standard `ast` module and rejects:
|
|
53
|
+
|
|
54
|
+
- `SyntaxError`s before execution.
|
|
55
|
+
- Bare `except:` / `except Exception:` / `except BaseException:` handlers.
|
|
56
|
+
- Calls to dangerous parametrizable functions: `eval()`, `exec()`, `compile()`.
|
|
57
|
+
- Calls to system/subprocess primitives: `os.system()`, `subprocess.call()`, `subprocess.run()`, `subprocess.Popen()`.
|
|
58
|
+
|
|
59
|
+
### 3. Silent Failure Detection
|
|
60
|
+
|
|
61
|
+
Even when a process exits with `0`, the engine flags suspicious output patterns such as:
|
|
62
|
+
|
|
63
|
+
- `empty DataFrame`
|
|
64
|
+
- `all NaN`
|
|
65
|
+
- `Traceback`
|
|
66
|
+
- `Pipeline failed`
|
|
67
|
+
- `Fatal Error`
|
|
68
|
+
|
|
69
|
+
### 4. Model-Agnostic Self-Healing Loop
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
AST Validation ──► Sandbox Execution ──► Traceback Classification ──► Patch ──► Re-run
|
|
73
|
+
(3 attempts max)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
- **Local heuristic fallback**: handles `NameError`, `ImportError`, `ModuleNotFoundError` by injecting safe imports or placeholder definitions.
|
|
77
|
+
- **LLM-guided healing**: when an LLM is configured (built-in Gemini Flash 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.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Quickstart
|
|
82
|
+
|
|
83
|
+
### Installation
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# Install from PyPI
|
|
87
|
+
pip install codeshield-runtime
|
|
88
|
+
|
|
89
|
+
# Install with all extras (LLM + Dev tools)
|
|
90
|
+
pip install "codeshield-runtime[llm,dev]"
|
|
91
|
+
|
|
92
|
+
# Or clone for development
|
|
93
|
+
git clone https://github.com/AlgorithmicMind/codeshield.git
|
|
94
|
+
cd codeshield
|
|
95
|
+
|
|
96
|
+
# With uv (recommended)
|
|
97
|
+
uv venv
|
|
98
|
+
uv pip install -e ".[test,lint,llm,dev]"
|
|
99
|
+
|
|
100
|
+
# Or with pip
|
|
101
|
+
python -m venv .venv
|
|
102
|
+
.venv\Scripts\activate # Windows
|
|
103
|
+
pip install -e ".[test,lint,llm,dev]"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Offline Usage (No API Key)
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from codeshield.loop import SelfHealingEngine
|
|
110
|
+
|
|
111
|
+
engine = SelfHealingEngine(use_llm=False)
|
|
112
|
+
with engine:
|
|
113
|
+
result, diagnosis = engine.run("print('hello world')")
|
|
114
|
+
print(result.stdout)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Model-Agnostic Self-Healing (Plug-and-Play)
|
|
118
|
+
|
|
119
|
+
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:
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from codeshield.loop import SelfHealingEngine
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def custom_openai_patcher(code: str, diagnosis) -> str:
|
|
126
|
+
# Any LLM call (OpenAI, Anthropic, DeepSeek, Ollama, LiteLLM)
|
|
127
|
+
response = client.chat.completions.create(
|
|
128
|
+
model="gpt-4o-mini",
|
|
129
|
+
messages=[
|
|
130
|
+
{
|
|
131
|
+
"role": "user",
|
|
132
|
+
"content": f"Fix this code:\n{code}\nError: {diagnosis.message}",
|
|
133
|
+
}
|
|
134
|
+
],
|
|
135
|
+
)
|
|
136
|
+
return response.choices[0].message.content
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
engine = SelfHealingEngine(patch_generator=custom_openai_patcher)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Zero-Config Self-Healing with Gemini Flash
|
|
143
|
+
|
|
144
|
+
For the built-in zero-config experience, create a `.env` file from `.env.example`:
|
|
145
|
+
|
|
146
|
+
```text
|
|
147
|
+
GEMINI_API_KEY=your_key_here
|
|
148
|
+
GEMINI_MODEL=gemini-3.7-flash
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from dotenv import load_dotenv
|
|
153
|
+
from codeshield.loop import SelfHealingEngine
|
|
154
|
+
|
|
155
|
+
load_dotenv()
|
|
156
|
+
|
|
157
|
+
engine = SelfHealingEngine()
|
|
158
|
+
with engine:
|
|
159
|
+
result, diagnosis = engine.run('print("Result: " + 42)')
|
|
160
|
+
print(result.stdout) # Result: 42
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Run the included demo:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
python demo.py
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### CLI Usage
|
|
170
|
+
|
|
171
|
+
Execute any Python file directly from the terminal with the built-in CLI:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
python -m codeshield run script.py
|
|
175
|
+
python -m codeshield run script.py --timeout 30
|
|
176
|
+
python -m codeshield run script.py --llm # try LLM self-healing if configured
|
|
177
|
+
python -m codeshield run script.py --no-llm # force local fallback
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 🤖 Agent Tool Integration (LangChain, CrewAI, OpenAI, Gen AI)
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from codeshield import create_code_execution_tool
|
|
184
|
+
|
|
185
|
+
# Pass the tool directly to your agent
|
|
186
|
+
tools = [create_code_execution_tool()]
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`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`.
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Verified Examples
|
|
194
|
+
|
|
195
|
+
The `examples/` folder contains ready-to-run recipes that have been executed and verified:
|
|
196
|
+
|
|
197
|
+
- `01_basic_sandboxing.py`: isolated execution with timing measurements.
|
|
198
|
+
- `02_security_gatekeeper.py`: AST rejection of unsafe code.
|
|
199
|
+
- `03_llm_healing_workflow.py`: self-healing workflow with an LLM or local fallback.
|
|
200
|
+
- `04_agent_tool_dropin.py`: end-to-end agentic tool-calling workflow with dynamic code generation.
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
python examples/01_basic_sandboxing.py
|
|
204
|
+
python examples/02_security_gatekeeper.py
|
|
205
|
+
python examples/03_llm_healing_workflow.py
|
|
206
|
+
python examples/04_agent_tool_dropin.py
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Running Tests & Lint
|
|
210
|
+
|
|
211
|
+
The suite currently has **50 tests** with **>82% code coverage** on `src/codeshield`.
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
ruff check src tests examples
|
|
215
|
+
pytest tests -v --cov=src/codeshield
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Enterprise Architecture & Custom Deployments
|
|
221
|
+
|
|
222
|
+
This repository ships the **core execution and healing engine**. For production multi-tenant deployments, the enterprise extension adds:
|
|
223
|
+
|
|
224
|
+
- **Multi-tenant orchestrator** with queue-based job scheduling.
|
|
225
|
+
- **PostgreSQL state persistence** for execution history, audit trails and replay.
|
|
226
|
+
- **Automated billing and token governance** (cost caps per tenant, per-execution budgets).
|
|
227
|
+
- **Prometheus/Grafana observability**, RBAC, and signed artifact provenance.
|
|
228
|
+
- **SLA-backed support** and custom agentic architecture consulting.
|
|
229
|
+
|
|
230
|
+
**Want the production-grade version or a tailored integration for your platform?**
|
|
231
|
+
|
|
232
|
+
- [Open a GitHub issue](https://github.com/AlgorithmicMind/codeshield/issues)
|
|
233
|
+
- [Connect on LinkedIn](https://www.linkedin.com/in/pedro-castejon-jodar/)
|
|
234
|
+
|
|
235
|
+
We offer enterprise licensing, dedicated onboarding and custom agentic-architecture consulting.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## License
|
|
240
|
+
|
|
241
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
|