๐ Technical report: RustForge AI: A Self-Hosted Rust Gateway for Local Coding Models with Item-Level Context Slicing for Compiler-Error Repair (Zenodo, DOI 10.5281/zenodo.24689082) ยท Code: github.com/mafzalkalwardev/rustforge-ai
If you run an open-weight coding model on your own machine, you quickly hit three practical problems:
- Your editor and agents expect an OpenAI-compatible API.
- A small model is fast for short prompts, a bigger one is better for multi-file or tool-using work, so something has to choose.
- Local models are slow on long prompts, so the client should send as little code as possible.
RustForge AI is my open-source (MIT) attempt to solve all three in one Rust workspace. This post walks through the design and shares one measurement I actually ran: how much context the "send only the broken item" trick removes.
What RustForge AI is
The Cargo workspace has three crates that matter here:
| Crate | Role |
|---|---|
server |
Axum gateway: POST /v1/chat/completions (with SSE streaming), /v1/models, /v1/tools, a rule-based model router, an MCP client, SQLite (WAL) memory and a cron scheduler |
cli (rf-cli) |
Runs cargo check, keeps only errors, slices the failing item with tree-sitter, and runs a bounded repair loop |
identity |
Shared project metadata served at /v1/about
|
Inference itself is done by llama.cpp serving GGUF models. The gateway just decides where each request goes and keeps a record of it.
Routing between a fast model and a deep model
The router is intentionally boring. In order:
- An explicit model name or alias (
fast,3b,deep,14b,agent, ...) wins. - Otherwise a request tag matching an alias decides.
- Any request that declares tools goes to the deep model.
- Otherwise it estimates prompt size as
characters / 3.6; above a threshold (default 512 estimated tokens) it goes deep. - Everything else goes to the fast model.
There is no learned router here. Research systems like FrugalGPT and RouteLLM learn routing policies. A handful of transparent rules was enough for me to start with, and every decision is logged with its reason.
The token-saving repair loop
This is the part I find most interesting.
When cargo check fails, rf-cli:
- Runs
cargo check --message-format=jsonand keeps onlyerror-level diagnostics (no warnings, no build noise). - Parses the file with tree-sitter-rust and finds the smallest item (function, struct, enum, impl block, module, ...) that contains the first error line.
- Sends the model the rendered error list plus only that item's source.
- Takes the first fenced code block from the reply, splices it in place of the item, and throws it away if the file no longer parses.
- Recompiles. At most five iterations.
If no item contains the error line (a use statement, a top-level comment, code inside a macro invocation), it falls back to sending the whole file.
How much context does slicing actually save?
I wanted a number I could defend, so I measured the slicer itself, with no model involved.
Method. A small benchmark program links the rf_cli library. For every non-blank line of every .rs file, it pretends the error is on that line, calls item_containing_line, and records bytes sent / bytes of whole file. I ran it on RustForge itself and on three popular crates from my local Cargo registry. Release build, rustc 1.100.1, 8-vCPU Xeon VM.
| Corpus | Files | Non-blank lines | Whole-file fallback | Median share sent |
|---|---|---|---|---|
| RustForge | 54 | 9,376 | 8.5% | 11.1% |
| tokio 1.54.1 | 380 | 97,748 | 24.6% | 28.1% |
| serde_json 1.0.154 | 37 | 16,491 | 11.8% | 2.2% |
| axum 0.8.9 | 58 | 17,078 | 23.6% | 5.7% |
So for a typical line, the slicer sends a small fraction of the file. But the tail is heavy: the 90th percentile was the whole file for the three crates. Two reasons:
-
Lines outside any item fall back to the whole file. tokio has the most of these because a lot of its code lives inside configuration macros like
cfg_rt! { ... }, which tree-sitter sees as token trees, not items. -
Large items (long
implblocks, modules) are still large.
Parsing cost is negligible: median 0.4โ0.6 ms per file, maximum 9.4 ms.
What this does not show: that the model can fix the error from the item alone. An item can depend on types defined elsewhere that the model never sees. I have not yet run the loop against a model on a benchmark of real errors, so I am not claiming any fix rate.
Security choices
- The server refuses to bind to anything except loopback or the Tailscale range (
100.64.0.0/10,fd7a:115c:a1e0::/48) unless you explicitly setRF_ALLOW_PUBLIC_BIND. - Every
/v1route requires a bearer token, compared without exiting early on the first wrong byte.
Things I would still improve (also listed in the write-up): the token comparison returns early when lengths differ, and tools that post to social networks or touch GitHub are logged but have no extra human-approval step.
Tests and CI
CI runs cargo fmt --check, clippy -D warnings, cargo check and cargo test on Linux x86_64, plus cargo check on an ARM64 runner. Locally, 15 unit tests pass across the identity, server and CLI crates. The slicer and repair loop don't have unit tests yet, which is next on my list.
What's next
- A reproducible repair benchmark of recorded Rust compile errors with known fixes, so I can report an honest fix rate for local models.
- Smarter slicing: pull in referenced type definitions, handle macro bodies.
- Fixing the dry-run behaviour, which currently repeats the same request.
FAQ
What is RustForge AI?
An open-source Rust workspace that puts an OpenAI-compatible API in front of local llama.cpp models, routes each request to a fast or deep model by rules, and includes a CLI that repairs Rust compile errors by sending only the failing item to the model.
Does it work with existing tools?
Anything that speaks the OpenAI chat-completions API can point at the gateway's /v1 endpoint with a bearer token.
How much does item slicing reduce the prompt?
In my measurement over 529 Rust files, the median line would send between 2.2% and 28.1% of its file depending on the codebase, but 8.5% to 24.6% of lines fall back to the whole file.
Does the repair loop actually fix errors?
I haven't measured that yet. The loop's structure follows published work on compiler-feedback repair, but I'm not claiming a fix rate until I have a proper benchmark.
Why Rust for the gateway?
One binary, one SQLite file, async I/O with Tokio, and the same language as the code it repairs, which made the tree-sitter and cargo integration straightforward.
Is it safe to expose on the internet?
By default it refuses to. It binds only to loopback or a Tailscale address unless you override that, and every API route needs a token.
I'm Muhammad Afzal Kalwar, a Rust and Python automation engineer and founder of INDUS Web Agency in Islamabad. The full technical report is on Zenodo: doi.org/10.5281/zenodo.24689082. Code: github.com/mafzalkalwardev/rustforge-ai.
Top comments (0)