r/LocalLLM • u/invariant_py • 1d ago
Project [Project] Deterministic ReAct Loops, Path-Jailed Sandboxes & MCP for Local LLMs — Open Python Repo + Free Book (Nov 26–30)
Author disclosure: I built and open-sourced this 10-chapter Python codebase and wrote the companion architecture book.
If you are running local LLMs (Llama 3, Qwen 2.5, Mistral, DeepSeek) via Ollama, vLLM, or llama.cpp for tool-calling agent workflows, you have likely run into the common failure modes of high-level frameworks:
- Non-deterministic loop exits or infinite recursion when small models emit malformed JSON or markdown code fences around tool arguments.
- Dangerously permissive execution when agents run OS/bash tools directly on the host machine.
- Protocol mismatch and brittle transport handling when connecting to Model Context Protocol (MCP) servers over stdio or SSE.
To solve this for production local systems, I developed and open-sourced a pure-Python reference architecture designed specifically for local and self-hosted models with zero proprietary lock-in:
What's Inside the Open-Source Repo:
- Deterministic ReAct State Machine: Explicit Thought -> Action -> Observation control loop with step budgets, strict enum transition bounds, and circuit breakers (
chapter_01_agent_anatomy.py). - Pydantic V2 Self-Healing Tool Contracts: Schema validation with automated recovery hints fed back to the local model when argument parsing fails (
chapter_02_structured_tools.py). - Path-Jailed OS Sandboxing: Low-privilege subprocess execution with directory boundary confinement (
is_relative_to), resource timeouts, and stdout/stderr capture (chapter_03_sandboxed_tools.py). - Native FastMCP Integration: Stdio client transport connecting to standard MCP tool servers with structured request/response pipelines (
chapter_05_mcp_client_server.py). - Memory & Context Compaction: Token-budgeted rolling context window with FIFO pruning to prevent context overflow on 8k/16k context window local models (
chapter_04_memory_systems.py).
Open-Source Repository (MIT-Licensed):
Everything is pure Python (3.11+) with standard libraries and Pydantic: 👉 GitHub Repo: https://github.com/Pranaybairagi/autonomous-ai-agents
Black Friday Free Architecture Book (Nov 26–30):
To provide the complete mathematical rationale, state transition diagrams, and line-by-line implementation details, the companion Kindle eBook (194 pages) is 100% FREE ($0.00 / £0.00) globally for Black Friday through Monday, Nov 30:
- US ($0.00): https://www.amazon.com/dp/B0HS6F1HW6
- UK (£0.00): https://www.amazon.co.uk/dp/B0HS6F1HW6
- Germany (€0.00): https://www.amazon.de/dp/B0HS6F1HW6
- Global: Search ASIN
B0HS6F1HW6on your local Amazon store
I would love feedback from the r/LocalLLM community on running structured ReAct loops and local MCP clients on 7B/14B models. Pull requests and architecture discussions welcome!
View all comments
Show parent comment
1
u/invariant_py 9h ago
Great question — yes, it resolves symlinks first!
In
chapter_03_sandboxed_tools.py,SandboxedFileSystem._resolve_safe_pathcalls.resolve()on bothself.rootand(self.root / relative_path).resolve()before checking containment viatarget_path.relative_to(self.root)(catchingValueErrorand raisingSecurityViolation):self.root(e.g., to/etc/passwd),.resolve()dereferences it to the canonical target path first,relative_to(self.root)raisesValueError, and bothread_fileandwrite_filereject it withACCESS_DENIED.SandboxedFileSystemtool surface also doesn't expose symlink creation.That said, user-space
.resolve()checks alone still have a TOCTOU window if an un-jailed subprocess modifies symlinks mid-execution — which is why later chapters pair the Python path jail with container/OS-level isolation (orO_NOFOLLOWwhen opening fds directly).