Architecture — Agda-Native AI Reasoning Environment¶
This document describes the high-level architecture of the agda-native-air
system: the components, which layer each one belongs to, how data flows between
them, and what is implemented today versus planned.
For the vision and rationale see MANIFESTO.md; for the full
plan and milestone schedule see PLAN.md and
GITHUB_PROJECT.md.
Overview¶
The system connects a frontier AI agent to the Agda type-checker through a small
stack of purpose-built layers. The agent supplies strategy and reasoning; Agda
is the oracle that decides what is actually correct; the layers in between let the
agent inspect proof state, propose terms, and retrieve relevant definitions.
The proof search in strux-driver is a second client of the same layers: it
proposes candidates by rule or by retrieval where the agent would reason, and
Agda judges them the same way.
┌────────────────────────────────────────────────┐
│ User / editor (Emacs agda-mode, VS Code, …) │
└───────────────────────┬────────────────────────┘
▼
┌────────────────────────────────────────────────┐ ┌──────────────────────────────┐
│ Frontier LLM agent (Claude Code, Codex, …) │ │ Proof search (strux-driver) │
│ strategy, planning, error interpretation │ │ beam search over obligations│
│ │ │ a client of the bridge │
└───────────────────────┬────────────────────────┘ └────────┬─────────────────────┘
│ MCP (JSON-RPC over stdio) │ MCP, as a client
▼ ┌────────────────────┘
┌──────────────────────────────────────────▼─────┐ ┌──────────────────────────────┐
│ agda-mcp — Bridge layer │◀────│ Retrieval corpus │
│ Haskell MCP server │ srch│ agda-strux → strux-driver │
│ proof-state tools + corpus search tools │ └──────────────────────────────┘
└───────────────────────┬────────────────────────┘
│ injects AgdaDojang macros and runs the agda binary
│ (v0: one subprocess per call)
▼
┌────────────────────────────────────────────────┐
│ agda-dojang — Interaction layer │ reportGoalCtx / reflection
│ repo-local Agda library + Python harness │ macros on the library path
└───────────────────────┬────────────────────────┘
▼
┌────────────────────────────────────────────────┐
│ Agda type-checker — the oracle │
└────────────────────────────────────────────────┘
Optional (the system runs end-to-end without these):
┌────────────────────────────────────────────────┐
│ Local specialist models (GPU / Jetson) │ premise selection, type-aware
│ wired into agda-mcp as extra tools │ embeddings, proof-term ranking,
│ │ routine completion
└────────────────────────────────────────────────┘
Key design property: works without local models¶
The system works end-to-end with just three moving parts: agda-dojang,
agda-mcp, and a frontier LLM agent. Local specialist models are optional
accelerators, not prerequisites — they can improve retrieval quality and proof
success, reduce frontier-API costs, and enable offline or low-connectivity use,
but nothing in the core loop depends on them.
This is a deliberate choice (see MANIFESTO.md): the primary
reasoning agent is always a frontier model, and the project builds the
environment around it rather than trying to replace it. It keeps the baseline
reproducible on any machine — no GPU required — and lets the local-model work in
Milestones 2 and 4 proceed as measured, swappable enhancements behind the same
MCP tool interface.
Components¶
| Component | Layer | Status | Role |
|---|---|---|---|
agda-mcp |
Bridge | Implemented (v0.3.0 on main; v0.2.0 is the latest release) |
MCP server exposing Agda proof-state interaction and corpus search to any MCP-compatible agent. |
agda-dojang |
Interaction | Implemented | Repo-local Agda library (AgdaDojang.Debug reflection macros) plus a Python proof-completion harness; realizes goal inspection, hole filling, and diagnostics. |
agda-strux |
Retrieval (extraction) | Implemented | Haskell backend linking Agda-as-a-library; its agda-json executable emits canonical JSONL from Agda source. |
strux-driver |
Retrieval (ETL) | Implemented | Scala driver that runs agda-json, validates and transforms its JSONL, and hosts the benchmark runner (struxdriver.benchmark) and the proof search (struxdriver.search, its own row below). |
Proof search (struxdriver.search) |
Search | Implemented (P0–P2) | Beam search over proof obligations that drives agda-mcp as a client, with Agda as the only judge of every step; fixed and retrieval proposers landed, a policy proposer (P3) is planned. See proof-search/overview.md and ADR 0001. |
| Local specialist models | Accelerators (optional) | Planned (M2 / M4) | Premise selection, type-aware embeddings, proof-term ranking, and routine completion, surfaced as extra agda-mcp tools. |
Layers in detail¶
Bridge — agda-mcp¶
- Role. Translate MCP tool calls from the agent into Agda interactions and corpus lookups, and return structured JSON. It holds no proof-search strategy of its own — that belongs to the agent.
- Inputs. MCP requests (
initialize,tools/list,tools/call) over JSON-RPC on stdio; an optional agda-strux JSONL corpus via--corpus. - Outputs. Structured tool results: goal/context, typecheck verdicts, diagnostics, and search hits.
- Status. v0.3.0 on
main, unreleased; the latest release is v0.2.0. Thirteen tools: four proof-state (get_goal,fill_hole,check_file,get_diagnostics), the whole-project gate (check_project, which runs the project's ownmaketarget orEverythingmodule and never misreports its exit code), five live-query tools (type_of,normalize,resolve_name,definition_of,exports_of), and three corpus-backed tools (search_by_name,search_by_type,get_dependencies; all registered only when a corpus is supplied).search_by_name'sinScopeAtkeeps the names a file can write, each typed by the interaction lane in that file's scope; the scope-awaresearch_in_scopebehind it is presented when--exposenames it (issue #203). With--auto, a fifteenth tool,auto, runs Agda's own proof search (Mimer) at a hole and answers a candidate term, never a verdict (issue #205). Verdict-producing tools shell out to theagdabinary once per call; the live queries are answered by a persistentagda --interaction-jsonchild per project root (issue #75,docs/agda-mcp/agda-mcp-interaction-lane.md); an Agda-as-a-library API is future work.
Interaction — agda-dojang¶
- Role. Define the proof actions the bridge wraps: extract
(goal, context)at a hole, propose a candidate term, and report typecheck feedback. - Inputs. An Agda source file with a
{!!}hole, plus a macro injection (reportGoalCtx) on the Agda library path. - Outputs. Marker blocks (
AGDADOJANG_REQ_BEGIN/END) parsed from Agda's stderr output into structured goal/context data, and typecheck pass/fail results. - Status. Implemented, including a deterministic scripted policy backend and the proof-completion evaluation harness used by the fixtures and benchmark.
Retrieval — agda-strux + strux-driver¶
- Role. Turn Agda libraries into a structured, queryable corpus so the bridge can search by name, type, and dependency neighborhood without invoking Agda.
- Inputs. Agda source modules (
agda-strux); the canonical JSONL those emit (strux-driver). - Outputs. Canonical JSONL rows (name, type, dependencies, kind, …) and
retrieval-friendly derived views;
strux-driveralso produces benchmark reports. - Status. Implemented. See
representation.mdfor the JSONL data contract.
Search — struxdriver.search¶
- Role. Prove benchmark obligations by search: propose candidate terms for the open hole, ask the bridge whether each typechecks, commit the ones that do, and claim a proof only when no obligation remains and a final whole-file check is green. It is a client of the bridge like the agent, not part of it, and it holds the search strategy the bridge deliberately does not.
- Inputs. A benchmark obligation (one module, one hole) from
data/benchmarks/; the bridge's proof-state and live-query tools; a corpus, through the bridge's search tools, when retrieval proposals are on. - Outputs. Per-run reports on the shared
eval-proof-completion.v0schema (results.jsonl,fixtures.jsonl), a timing ledger,report.json, and the solved modules. - Status. P0 to P2 landed and measured: a fixed action space, then
retrieval over the standard-library and agda-algebras corpora; P3, proposals
from a learned policy, is direction. How it works is
proof-search/overview.md; its decisions and numbers are ADR 0001.
Accelerators — local specialist models¶
- Role. Cheap, domain-specific models for narrow subtasks: ranking premises, embedding types for nearest-neighbor search, pre-filtering candidate terms, and completing routine obligations without a frontier call.
- Status. Not yet implemented — planned for Milestones 2 and 4. When added
they attach to
agda-mcpas additional tools, so the interface the agent sees does not change whether or not they are present.
Data flow¶
- The agent starts
agda-mcpas a subprocess and exchanges JSON-RPC messages over stdio (the MCP protocol). - For a proof-state tool,
agda-mcpapplies the correspondingagda-dojangaction — injecting thereportGoalCtxmacro or substituting a candidate — and runs theagdabinary with the AgdaDojang library on its path (v0: a fresh subprocess per call). - Agda type-checks;
agda-mcpparses the result (marker block or error) and returns structured JSON to the agent. - For a search tool,
agda-mcpanswers from the in-memory corpus index built byagda-strux+strux-driver— no Agda invocation. - When present, local models add ranking/completion tools alongside the above; the agent calls them the same way it calls any other tool.
- The proof search (
strux-driver) drives steps 1 to 4 as a client in the agent's place: a proposer (fixed rules, or retrieval over the corpus) supplies the candidate terms the agent would supply, and Agda judges them the same way.
Interaction model¶
A typical agent session follows a propose–check–refine loop.
- Agent calls
check_fileto load an Agda file with holes. - Agent calls
get_goalfor a specific hole to inspect its type and context. - Agent reasons about the goal (using its own knowledge, optionally augmented by the search tools to find relevant lemmas).
- Agent calls
fill_holewith a candidate term. - If the term typechecks, the hole is filled (possibly creating sub-holes; go to 2).
- If the term fails, the agent reads the error, revises, and retries (go to 3).
- Repeat until all holes are filled or the agent gives up.
This is the same loop that AgdaDojang's scripted policy backend demo implements, but driven by a frontier LLM instead of a fixed script; the proof search runs it with a machine proposer in the agent's place.
Language choice: Haskell¶
agda-mcp is implemented in Haskell for three reasons.
- Agda itself is implemented in Haskell, so a future version of the server can
call an AgdaDojang Haskell API directly with no subprocess boundary (the v0
implementation still shells out to the
agdabinary per tool call). - The extraction backend (
agda-strux) is already Haskell, giving shared types and shared build infrastructure (Cabal / Nix) under one toolchain. - Key collaborators work primarily in Haskell and Agda.
Alternatives considered and rejected: Rust (no Agda-as-library access), Python
(dynamically typed), TypeScript (alien to the team). The v0 server uses a minimal
hand-written MCP stdio transport covering the three methods it needs
(initialize, tools/list, tools/call) rather than pulling in a third-party
MCP library.
Related documents¶
MANIFESTO.md— vision and the frontier-agent-plus-environment thesis.PLAN.md— architecture context, milestones, and the local-model plan.GITHUB_PROJECT.md— living GitHub project roadmap with issue-level detail.agda-mcp/README.md— Bridge layer: tool surface, flags, transport, and module structure.representation.md— data contract foragda-struxJSONL output and derived views.proof-search/overview.md— how the proof search works, with a worked example.adr/0001-proof-search-on-agda-mcp.md— the proof search's decisions and measured record.adr/0002-agda-mcp.md— the agda-mcp server's design record.