Skip to content

feat(lean): optimize all Lean tools via Pantograph and LSP incremental elaboration#4

Merged
markm39 merged 6 commits into
masterfrom
worktree-refactor+lean-check-optimization
Mar 28, 2026
Merged

feat(lean): optimize all Lean tools via Pantograph and LSP incremental elaboration#4
markm39 merged 6 commits into
masterfrom
worktree-refactor+lean-check-optimization

Conversation

@markm39
Copy link
Copy Markdown
Collaborator

@markm39 markm39 commented Mar 28, 2026

Summary

Optimizes every slow Lean tool to use fast paths (Pantograph REPL or LSP incremental elaboration), with graceful fallback to the existing lean --threads=4 compiler invocation.

  • lean_check: Pantograph env.inspect fast path (hash lookup, ~ms). Accepts batch exprs array parameter so the LLM can check multiple names in one call.
  • lean_verify: LSP get_diagnostics() fast path via lean-lsp-mcp. After first elaboration (~18s Mathlib import), subsequent verifications are incremental (~6-58ms).
  • lean_eval: Same LSP fast path as lean_verify.
  • lean_search_tactic: Same LSP fast path. "Try this:" suggestions extracted from LSP diagnostics.
  • Sorry detection fixed for LSP diagnostics (backtick quotes vs single quotes).
  • .worktreeinclude updated with lean/.lake and vendor for worktree compatibility.
  • System prompt updated to guide LLM toward batch lean_check usage.

Performance

Tool Before After (warm)
lean_check (single) ~5s <100ms (Pantograph)
lean_check (batch 3) ~15s (3 calls) <100ms (1 Pantograph call)
lean_verify 5-30s 6ms incremental (LSP)
lean_eval 5-30s incremental (LSP)
lean_search_tactic 15-30s incremental (LSP)
lean_screen_tactics ~100ms/tactic unchanged (already Pantograph)
lean_goals ~1-5s unchanged (already MCP)

First call of each session still takes ~18s (Mathlib cold start), which is unavoidable. All subsequent calls benefit from incremental elaboration.

Test plan

  • cargo clippy --workspace -- -D warnings
  • cargo test --workspace (all 25 tests pass)
  • Pantograph integration tests (3): single/batch/unknown lean_check via env.inspect
  • LSP integration tests (7): verify valid/sorry/error, incremental speed, eval, search_tactic, e2e tool dispatch
  • Manual TUI testing: proof session with lean_verify calls
  • Incremental lean_verify measured at 6ms and 58ms in tests
  • Sorry detection confirmed working with LSP backtick-quoted diagnostics

markm39 added 5 commits March 27, 2026 23:14
lean_check was spawning a full `lean --threads=4` process per expression,
re-importing Mathlib each time (~5s per call). Now it uses Pantograph's
env.inspect first (milliseconds per expression since Mathlib is preloaded),
falling back to a single batched Lean invocation for all expressions.

Changes:
- Pantograph fast path in tool_lean_check via env.inspect
- Accept `exprs` array parameter for batch lookups in one call
- System prompt updated to guide LLM toward batch usage
- Integration tests for single, batch, unknown, and missing-args cases
- .worktreeinclude updated with lean/.lake and vendor entries
Pantograph's frontend.process doesn't reliably handle full file
verification: with imports it re-imports Mathlib (~170s), without
imports the elaboration context is wrong. Revert to lean compiler
fallback for lean_verify and lean_eval. lean_check Pantograph
fast path via env.inspect remains (tested and working).
lean_verify now tries the existing LeanLspMcp.get_diagnostics() before
falling back to spawning a fresh lean process. The LSP server keeps
Mathlib loaded after the first elaboration, so subsequent verifications
of the same file only re-elaborate changed portions (~200ms-2s vs 5-30s).

Changes:
- Add verify_scratch_via_lsp() in verify.rs: bridges DiagnosticsResult
  to LeanVerificationSummary with proper sorry detection
- tool_lean_verify tries LSP fast path when ctx.lsp_mcp is available
- verify_node_at accepts optional LSP handle (None at non-tool call sites)
- All call sites updated (pass None where LSP not yet threaded)
…otes

LSP diagnostics use backtick quotes (`sorry`) not single quotes ('sorry').
Added integration tests that confirm:
- Valid proofs verify successfully via LSP
- Sorry proofs are correctly rejected
- Type errors are detected
- Incremental verification is fast (~23ms after cold start)
Route lean_eval and lean_search_tactic through LeanLspMcp.get_diagnostics()
for incremental elaboration, same pattern as lean_verify. Extract
suggestion parsing into shared extract_search_suggestions() function.

All three slow tools now have the LSP fast path:
- lean_verify: ~6ms incremental (was 5-30s)
- lean_eval: incremental after warmup (was 5-30s)
- lean_search_tactic: incremental after warmup (was 15-30s)

Integration tests added for all paths.
@markm39 markm39 changed the title feat(lean): use Pantograph for lean_check, add batch support feat(lean): optimize all Lean tools via Pantograph and LSP incremental elaboration Mar 28, 2026
@markm39 markm39 merged commit 2a10a61 into master Mar 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant