hybrid_local
by daheym
README.md
# Hybrid Codex and local LLM
> **Status: experimental prototype.** The worker has been tested with mocked inference, MCP stdio, and real Docker validation. Live DGX Spark, Open WebUI, and Home Assistant integration still needs verification in your environment. Start with a disposable repository and review every patch before applying it.
Codex owns task design, review and acceptance. A local stdio MCP worker delegates patch generation to one OpenAI-compatible model on the DGX Spark. It applies proposals to a detached Git worktree, runs optional offline validation and returns a structured review result. Open WebUI and Home Assistant use the same vLLM server independently.
## Upstream credits
- The worker adapts the MIT-licensed unified-diff parser from [local-llm-delegator](https://github.com/tushrv/local-llm-delegator). See the included [license notice](LICENSE.local-llm-delegator).
- The bounded implementation and repair loop draws inspiration from [local-vibes](https://github.com/OpenSourceWTF/local-vibes).
## Project guides
- [Upstream assessment and architecture decision](docs/upstream-assessment.md)
- [DGX Spark, WireGuard and Open WebUI](docs/dgx-and-webui.md)
- [Home Assistant Assist](docs/home-assistant.md)
## Install the worker
Use Python 3.11+, Git, and a local Docker engine in Linux-container mode for validation. Install in a virtual environment. On Windows:
```powershell
python -m venv .venv
.venv/Scripts/python.exe -m pip install -r requirements.txt
docker pull python:3.12-slim
Copy-Item worker.example.toml worker.toml
```
On Linux or macOS use `.venv/bin/python` as the interpreter and `cp` for the config. If your Python installation ships without pip, create the environment with `uv venv` and install using `uv pip install --python <venv-python> -r requirements.txt`.
Set the target Git checkout root and a private task-state directory outside it in `worker.toml`. The target needs a committed HEAD. Authorize exact text file paths and select trusted container images and argv for your checks. Use a locally pulled, preferably digest-pinned image with the project's test dependencies already installed. The validation container gets a read-only copy of the task's approved text files, so include all source modules, test files and config required by your check. Checks that need write access can use `/tmp`. Restrict the developer host's Docker engine to trusted local administrators.
## Connect to inference
Use an existing authenticated OpenAI-compatible Chat Completions endpoint or follow the [DGX setup](docs/dgx-and-webui.md). Set the endpoint, served model ID, and key in the shell that will launch Codex. For example, in PowerShell 7:
```powershell
$env:LOCAL_LLM_BASE_URL = 'http://10.44.0.2:8000/v1'
$env:LOCAL_LLM_MODEL = 'spark-local'
$env:LOCAL_LLM_API_KEY = Read-Host 'Local inference API key' -MaskInput
.venv/Scripts/python.exe server.py smoke --capabilities
```
Replace the example URL and model with your own. [.env.example](.env.example) lists optional streaming, timeout, retry, and output settings. The worker reads the process environment.
Chat Completions is the baseline. The probe reports streaming, structured JSON schema and automatic tool round-trip separately. Set `LOCAL_LLM_STREAM=1` only after SSE works with the selected endpoint. The worker uses plain JSON in assistant text and local patch validation, so it works without native tool calling or server-side structured outputs. Start with small scopes when a model struggles to emit JSON or correct diff hunks.
## Run your first task
For a disposable example that matches `worker.example.toml`, create and commit a calculator and a test that expects an explicit error message:
```powershell
$demoRepo = Join-Path ([System.IO.Path]::GetTempPath()) ("hybrid-demo-" + [guid]::NewGuid())
$demoState = "$demoRepo-state"
New-Item -ItemType Directory -Path "$demoRepo/tests" | Out-Null
@'
def divide(numerator, denominator):
return numerator / denominator
'@ | Set-Content -Encoding ascii "$demoRepo/calculator.py"
@'
import unittest
from calculator import divide
class CalculatorTests(unittest.TestCase):
def test_normal_division(self):
self.assertEqual(divide(6, 2), 3)
def test_zero_denominator(self):
with self.assertRaisesRegex(ZeroDivisionError, "denominator must be nonzero"):
divide(1, 0)
'@ | Set-Content -Encoding ascii "$demoRepo/tests/test_calculator.py"
git -C $demoRepo init
git -C $demoRepo -c core.autocrlf=input add calculator.py tests/test_calculator.py
git -C $demoRepo -c user.name=Demo -c user.email=demo@example.invalid commit -m 'Add calculator fixture'
$demoRepo -replace '\\', '/'
$demoState -replace '\\', '/'
```
1. Put the two printed paths in `worker.toml` as `repository` and `state_directory`. Keep the sample file allowlists and `unit` check for this fixture.
2. Merge [codex.example.toml](codex.example.toml) into your user Codex `config.toml`. Use absolute paths to the worker files and virtual environment. From the shell with the inference variables set, run `codex mcp list` and confirm `hybrid_local` is listed. Start the local session with `codex -C $demoRepo`. This setup uses a Codex client with access to local stdio MCP. [Codex MCP configuration](https://learn.chatgpt.com/docs/extend/mcp)
3. Give Codex this prompt:
> Use local_implement to make calculator.py raise ZeroDivisionError with the message "denominator must be nonzero" when the denominator is zero. Set write_paths to ["calculator.py"], read_paths to ["tests/test_calculator.py"], and checks to ["unit"]. Poll local_task_status and show me the diff and test result. If the change needs revision, send review feedback through local_fix and show me the revised result.
4. Review the diff and the `unit` result. For this example, the approved diff should preserve normal division and the check should pass. You can inspect the staged changes yourself with `git -C "<worktree from task status>" diff --cached`. Apply only the diff you approve through your usual Git workflow.
`local_implement` and `local_write_tests` accept `task`, `write_paths`, optional `read_paths` and optional named `checks`. Paths apply to a committed snapshot, so stage and commit prerequisite work yourself before delegation. Use `local_write_tests` with only test files in `write_paths`. `local_fix(task_id, feedback)` carries the same base, files and checks into a new worktree, starting from the prior diff.
Poll the returned ID using `local_task_status`. Terminal states are `ready_for_review`, `checks_failed` and `failed`. Status includes an iteration counter when running, modified paths, cumulative staged diff, executed tests, summaries and errors. A ready result with an empty tests list had no automated validation. Inspect the patch and test output for correctness. Accept by integrating the reviewed diff yourself through your regular Git workflow, or reject by leaving it unapplied. The worker never commits or merges an implementation.
Successful and failed worktrees are retained. To remove a terminal task's worktree after review, run `python server.py --config worker.toml cleanup <task_id> --yes`. The JSON record and diff remain available. If the stdio worker is interrupted, its record may still say queued or running. After confirming the worker is stopped, use `git worktree list` and inspect the task path. Remove only that identified worktree with Git's worktree removal command after preserving any useful diff. An interrupted Docker check may leave a container named `hybrid-check-<id>`. Inspect it and remove the identified container after stopping the worker.
## Boundaries and troubleshooting
- Use a trusted target repository and trusted Git configuration. The worker owns the task directories and should be the only process modifying their files or staged changes. Git hooks and global/system Git config are disabled for the worker. Local Git configuration still applies. Keep repository filters and local config under developer control.
- Only specified UTF-8 Git blobs are sent to the model. Review task descriptions, approved files and validation output for secrets and personal data. Keep credentials out of the authorized files. API credentials travel in the inference request header. The model has no filesystem access or shell tool.
- The Git worktree isolates changes from your primary checkout. Docker is the execution boundary for generated code, with no network, a read-only snapshot, no repository metadata or host credentials, a bounded tmpfs, a non-root user and resource limits. The worker requires Docker for validation and has no host execution fallback. For adversarial repositories or stronger isolation, use a dedicated VM with a local Docker engine. Keep the engine and validation images patched.
- If the worker returns HTTP 401, check the token and `/v1` URL. For timeouts, check WireGuard routes, firewall rules, vLLM queue depth and the configured deadlines. For truncated replies, reduce the allowed context or raise the generation budget within the model context limit.
- If a check cannot import modules, add the required approved files to the task's read scope and use a trusted image with dependencies installed. If Git rejects a patch on Windows, use normalized LF source in Git and verify the task's base and paths. Invalid patches appear in task errors.
## Tests
Run `.venv/Scripts/python.exe -m unittest discover -s tests -v`. These tests create a temporary Git repo and start a local mocked HTTP endpoint. To also exercise real Docker validation, automatic repair and the MCP correction loop, pull `python:3.12-slim`, set `HYBRID_TEST_DOCKER=1` and rerun. No DGX is needed. On live infrastructure, run the inference smoke test, the [concurrent shared-model smoke test](docs/dgx-and-webui.md#accounts-updates-and-smoke-test), and [Assist acceptance checks](docs/home-assistant.md#assist-acceptance-checks).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues