{"slug": "building-a-repository-aware-ai-coding-loop-in-rust", "title": "Building a Repository-Aware AI Coding Loop in Rust", "summary": "A developer built Loop Engine, an open-source Rust CLI that runs a repository-aware AI coding workflow of plan, edit, verify, review, and reflect against a local codebase. The tool uses OpenRouter for model access, allows a different model per phase, detects build systems like Cargo, Go, npm, and pytest for verification, and uses optimistic concurrency to prevent agents from silently overwriting developer changes.", "body_md": "Most AI coding examples stop after generating code.\n\nThe model receives a prompt, returns a proposed implementation, and the application prints the result. That is useful for experimentation, but it is not a complete engineering workflow. Code is only useful after it has been written to the repository, compiled, tested, and reviewed.\n\nI built [Loop Engine](https://github.com/anggadb/loop-engine) to explore a more practical approach:\n\n```\nplan → edit → verify → review → reflect\n```\n\nLoop Engine is an open-source Rust CLI that runs this workflow against a local repository. It uses OpenRouter for model access and allows a different model to handle each phase.\n\nA normal run performs the following steps:\n\nThe loop only reports completion when:\n\nA model cannot complete the workflow merely by returning the word `COMPLETE`.\n\nThe engine executes file operations and local verification commands, so predictable behavior matters.\n\nRust gives the project:\n\nThe engine also uses optimistic concurrency for file updates. An existing file must be read before it can be written. Immediately before writing, the engine confirms that the file still matches the version the agent read.\n\nThis prevents the agent from silently overwriting a change made by the developer during the run.\n\nClone the repository:\n\n```\ngit clone https://github.com/anggadb/loop-engine.git\ncd loop-engine\n```\n\nInstall the CLI:\n\n```\ncargo install --path . --locked\n```\n\nCreate your local settings:\n\n```\nCopy-Item .env.example .env\nCopy-Item loop-engine.json.example loop-engine.json\n```\n\nAdd an OpenRouter API key to `.env`:\n\n```\nOPENROUTER_API_KEY=your-openrouter-key\nOPENROUTER_HTTP_REFERER=your-localhost-url\nOPENROUTER_X_TITLE=Loop Engine\n```\n\nThe environment file and local model configuration are excluded from Git.\n\nEach phase can use a different OpenRouter model:\n\n```\n{\n  \"models\": {\n    \"plan\": \"openai/gpt-4.1-mini\",\n    \"implement\": \"openai/gpt-5.1-codex\",\n    \"review\": \"openai/gpt-4.1-mini\",\n    \"reflect\": \"openai/gpt-4.1-mini\"\n  },\n  \"requests\": {\n    \"timeout_seconds\": 600\n  },\n  \"execution\": {\n    \"max_tool_calls\": 30,\n    \"timeout_seconds\": 120,\n    \"checks\": []\n  }\n}\n```\n\nFor free experimentation, the phase models can be replaced with an available free model:\n\n```\n{\n  \"models\": {\n    \"plan\": \"qwen/qwen3-coder:free\",\n    \"implement\": \"qwen/qwen3-coder:free\",\n    \"review\": \"qwen/qwen3-coder:free\",\n    \"reflect\": \"qwen/qwen3-coder:free\"\n  },\n  \"execution\": {\n    \"max_tool_calls\": 30,\n    \"timeout_seconds\": 120,\n    \"checks\": []\n  }\n}\n```\n\nFree models have stricter rate limits and may be less reliable. The exact list of available models can also change.\n\nBefore sending repository content to a model, inspect the generated snapshot:\n\n```\nloop-engine --repo \"C:\\projects\\my-app\" --inspect\n```\n\nThis command does not load the API key or make an OpenRouter request.\n\nThe snapshot is bounded and excludes hidden entries, common dependency directories, generated output, symlinks, binary files, and credential-like filenames. It still cannot guarantee that source files contain no sensitive values, so reviewing the snapshot remains important.\n\nLoop Engine detects common build systems from files in the target directory:\n\n| Repository marker | Verification | \n|---|---|\n| `Cargo.toml` | `cargo test` | \n| `go.mod` | `go test ./...` | \n| `package.json` | Available `test` ,`typecheck` , and`build` scripts | \n| Pytest configuration | `python -m pytest` | \n\nYou can preview the selected checks without running them:\n\n```\nloop-engine --repo \"C:\\projects\\my-app\" --inspect-checks\n```\n\nExample output for a Go repository:\n\n```\n{\n  \"source\": \"detected\",\n  \"checks\": [\n    {\n      \"program\": \"go\",\n      \"args\": [\"test\", \"./...\"]\n    }\n  ]\n}\n```\n\nExplicit checks override detection:\n\n```\n{\n  \"execution\": {\n    \"max_tool_calls\": 30,\n    \"timeout_seconds\": 180,\n    \"checks\": [\n      {\n        \"program\": \"go\",\n        \"args\": [\"test\", \"./...\"]\n      }\n    ]\n  }\n}\n```\n\nThe model cannot invent arbitrary shell commands. It can request verification, but the engine only executes commands resolved from trusted configuration or fixed detection rules.\n\nTo run the engine against a repository:\n\n```\nloop-engine `\n  --repo \"C:\\projects\\my-app\" `\n  --env-file \"C:\\tools\\loop-engine\\.env\" `\n  --config \"C:\\tools\\loop-engine\\loop-engine.json\" `\n  \"Remove the deprecated endpoint and update its tests\" `\n  --iterations 3\n```\n\nDuring implementation, the coding agent can request these operations:\n\nThe engine always runs authoritative verification again after the final edit.\n\nExit code `0` means the work passed the completion rules. Exit code `2` means the iteration or tool budget ended before verified completion. Other nonzero codes indicate execution errors.\n\nEvery run creates a `.loop-engine` directory inside the target repository:\n\n```\n.loop-engine/\n  run-<id>.jsonl\n  run-<id>/\n    iteration-001/\n      0001-plan.jsonl\n      0002-implement.jsonl\n      0003-implement.jsonl\n      0004-review.jsonl\n      0005-reflect.jsonl\n```\n\nEach prompt log records:\n\nThe request is written before the API call starts. If the process is interrupted or the provider times out, the input remains available for diagnosis.\n\nThe main journal records original and replacement file content before every write. This provides a manual recovery path if a run fails after modifying files.\n\nLogs may contain source code and model output, so `.loop-engine/` should remain excluded from version control.\n\nAn incomplete result includes a `stop_reason`, such as:\n\n```\nverification_failed\ntool_budget_exhausted\nunresolved_tool_error\nno_changes\ninvalid_review_response\nreview_requires_changes\n```\n\nIt also contains:\n\nEdits remain in the target repository after an incomplete run. This makes the result inspectable, but it also means the tool should preferably be used in a clean branch or disposable checkout.\n\nLoop Engine is still experimental.\n\nIt currently:\n\nThe verification process is not an operating-system sandbox. Project build scripts may access the network, environment variables, and files available to the user.\n\nThe interesting part of an AI coding agent is not the initial code response. It is the control loop around that response.\n\nThe model needs constrained tools, real observations, explicit verification, durable logs, and a completion rule that cannot be satisfied by confidence alone.\n\nLoop Engine is my attempt to make that loop small enough to understand while still useful against real repositories.\n\nThe source is available on GitHub:\n\n👉 [github.com/anggadb/loop-engine](https://github.com/anggadb/loop-engine)\n\nFeedback, issues, and contributions are welcome.", "url": "https://wpnews.pro/news/building-a-repository-aware-ai-coding-loop-in-rust", "canonical_source": "https://dev.to/anggbchtr/building-a-repository-aware-ai-coding-loop-in-rust-1ef5", "published_at": "2026-09-10 16:13:27+00:00", "updated_at": "2026-09-10 16:52:46.932570+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-products"], "entities": ["Loop Engine", "Rust", "OpenRouter", "GitHub", "Cargo", "Go", "pytest"], "alternates": {"html": "https://wpnews.pro/news/building-a-repository-aware-ai-coding-loop-in-rust", "markdown": "https://wpnews.pro/news/building-a-repository-aware-ai-coding-loop-in-rust.md", "text": "https://wpnews.pro/news/building-a-repository-aware-ai-coding-loop-in-rust.txt", "jsonld": "https://wpnews.pro/news/building-a-repository-aware-ai-coding-loop-in-rust.jsonld"}}