How math-ai-org/mathcode works
Purpose and entry points
MathCode is a distributable terminal AI assistant that turns natural-language math problems into Lean 4 theorems and attempted formal proofs. The primary invocation paths are the installed mathcode launcher created by setup, the bundle-local wrapper ./run, and browser mode through ./run webui. Outputs are intended for LeanFormalizations/ (generated at runtime).
Grounding: run, setup.sh, index.html
Bootstrap, runtime repair, and launch boundary
setup.sh is the installation/control-plane entry point. It provisions .env, local extension directories, a user-local launcher, Lean tooling, and validates or downloads platform-specific release runtime assets. The checked-in repository is a bootstrap bundle: the principal CLI/WebUI executables and vendored runtime are expected to be restored from release archives rather than represented in this tree.
run is the runtime wrapper: it sources bundle configuration and establishes the intended local environment before invoking the packaged CLI or WebUI helper.
Grounding: setup.sh, run, .env.example
AI session and provider boundary
The packaged MathCode CLI owns interactive sessions, slash commands, scheduling, agent tasks, formalization/planning/proving orchestration, and optional WebUI daemon behavior. Its default model path uses Codex authentication; alternate provider routes are configured through environment variables. Provider SDKs and the core implementation are bundled into release binaries, not available as editable source here.
External boundaries include Codex/OpenAI authentication, Anthropic-compatible APIs, OpenRouter/Atlas-compatible Responses endpoints, and optional cloud-provider credential paths.
Grounding: .env.example, run, setup.sh
Formalization and proof pipeline
The central flow is: user prompt → LLM formalization into Lean theorem → strategy planning/lemma discovery → iterative Lean compilation and repair → persisted proof artifact. Optional modes expand this into multi-planner parallelism, tree-of-subgoals decomposition with stitching, and tool-using agent-mode proving.
Successful theorems may be persisted into a theorem library; conversational assumptions can be stored as compile-checked axioms; vault/graph output can feed later planning. These runtime-managed stores are outside the checked-in tree.
Grounding: lean-workspace/MathCodeLean.lean, lean-workspace/lakefile.toml, run
Lean execution environment
Lean 4 and Lake are material runtime dependencies. The project defines a dedicated workspace and toolchain pin, with setup preferring bundle-local elan tooling and only using system Lean/Lake via explicit opt-in. The workspace is the compilation boundary for generated theorem code and Mathlib-backed proof checking. Persistent REPL and LSP modes are optional accelerators around this boundary.
Grounding: lean-workspace/lean-toolchain, lean-workspace/lakefile.toml, bin/lean-check
Analysis-tool extension boundary
Python tools are auto-discoverable extensions, separate from the core bundled CLI. They support proof and library inspection—axiom checks, sorry analysis, proof statistics, and library search—and require Python 3.12+ only when used. The Lean-check launcher is a focused bridge for invoking Lean validation.
Grounding: tools/axiom_checker.py, tools/sorry_analyzer.py, bin/lean-check
Customization and deployment surfaces
Skills, Python tools, and manifest-based plugins are independently discoverable extension surfaces. Plugins can contribute commands, agents, hooks, MCP servers, and related behavior; therefore they form a trust and capability boundary around the core session runtime. Static browser-facing project content is separate from the local WebUI daemon.
Grounding: skills/README.md, plugins/README.md, index.html
Main entry points: setup.sh Setup controller · run Runtime wrapper · lakefile.toml Lean workspace · .env.example Environment template
Components 18
Bootstrap & Launch
- Setup controller · Installs launcher, tooling, configuration, and release runtime assets.
setup.sh - Environment template · Configures providers and runtime options.
.env.example - Runtime wrapper · Loads the bundle environment and starts CLI or WebUI.
run - Installed mathcode · User-local command installed by setup.
Bundled Runtime
Release-restored executables own the core session behavior.
- Release runtime assets · Platform-specific CLI, WebUI, and vendored dependencies restored from releases.
- MathCode session runtime · Handles commands, scheduling, agents, and formalization workflows.
- AI providers · Codex auth is default; alternate compatible providers are environment-configured.
- Formalization & planning · Produces Lean statements and plans, lemmas, subgoals, or agent tasks.
- Compile & repair loop · Checks generated Lean and revises attempted proofs.
- Runtime theorem stores · Persists proofs, theorem library entries, axioms, and vault/graph data.
Lean Proof Environment
- Lean workspace · Mathlib-backed compilation boundary for generated theorem code.
lean-workspace/lakefile.toml - Lean project module
lean-workspace/MathCodeLean.lean - Lean toolchain pin
lean-workspace/lean-toolchain - Lean-check bridge · Focused launcher for Lean validation.
bin/lean-check
Extensions & UI
- Python analysis tools · Optional axiom, sorry, proof-statistics, and library-inspection extensions.
tools/axiom_checker.py - Skills
skills/README.md - Plugin manifests · Can contribute commands, agents, hooks, and MCP servers.
plugins/README.md - Browser project content · Separate static content for browser-facing mode.
index.html
Connections 20
- Setup controller → Environment template: provisions
- Setup controller → Installed mathcode: installs
- Setup controller → Release runtime assets: validates or downloads
- Setup controller → Lean toolchain pin: provisions tooling for
- Installed mathcode → Runtime wrapper: invokes
- Runtime wrapper → Environment template: sources
- Runtime wrapper → Release runtime assets: launches
- Release runtime assets → MathCode session runtime: contains
- MathCode session runtime → AI providers: uses
- MathCode session runtime → Formalization & planning: orchestrates
- Formalization & planning → Compile & repair loop: submits theorem and proof
- Compile & repair loop → Lean workspace: compiles in
- Lean workspace → Lean project module: includes
- Lean workspace → Lean toolchain pin: uses
- Lean-check bridge → Lean workspace: validates against
- Compile & repair loop → Runtime theorem stores: persists successful proofs
- MathCode session runtime → Skills: discovers
- MathCode session runtime → Plugin manifests: loads
- MathCode session runtime → Python analysis tools: discovers optional tools
- Runtime wrapper → Browser project content: serves in web mode