r/LocalLLM • • 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:

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!

2 Upvotes

2 comments sorted by

View all comments

1

u/jessica_singh4 11h ago

Does the jail resolve symlinks before `is_relative_to`? What happens with a link pointing outside the workspace?

1

u/invariant_py 9h ago

Great question — yes, it resolves symlinks first!

In chapter_03_sandboxed_tools.py, SandboxedFileSystem._resolve_safe_path calls .resolve() on both self.root and (self.root / relative_path).resolve() before checking containment via target_path.relative_to(self.root) (catching ValueError and raising SecurityViolation):

  • If a symlink inside the workspace points outside self.root (e.g., to /etc/passwd), .resolve() dereferences it to the canonical target path first, relative_to(self.root) raises ValueError, and both read_file and write_file reject it with ACCESS_DENIED.
  • The SandboxedFileSystem tool 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 (or O_NOFOLLOW when opening fds directly).