lean-agentic
Version:
High-performance WebAssembly theorem prover with dependent types, hash-consing (150x faster), Ed25519 proof signatures, MCP support for Claude Code, AgentDB vector search, episodic memory, and ReasoningBank learning. Formal verification with cryptographic
790 lines (573 loc) โข 23 kB
Markdown
# lean-agentic
**Hash-consed dependent types with 150x faster equality + Ed25519 proof signatures**
[](https://npmjs.com/package/lean-agentic)
[](https://www.npmjs.com/package/lean-agentic)
[](https://bundlephobia.com/package/lean-agentic)
[](https://github.com/agenticsorg/lean-agentic/blob/main/LICENSE)
[](https://crates.io/crates/lean-agentic)
[](https://modelcontextprotocol.io)
**Developed by**: [ruv.io](https://ruv.io) | [github.com/ruvnet](https://github.com/ruvnet)
## ๐ฏ What is lean-agentic?
Lean-Agentic is a high-performance WebAssembly theorem prover and dependent type library that brings formal verification to JavaScript and TypeScript. Think of it as a logic calculator you can ship anywhere the web runs. It is fast, compact, and built for practical use with AI tools, teaching, and type-safe programming.
**Built in Rust** with a clean, auditable core inspired by Lean 4's dependent type system, Lean-Agentic compiles to WebAssembly for universal JavaScript compatibility.
In simple terms, Lean-Agentic helps you prove properties about your code before it runs. It checks that functions match their types, that transformations keep meaning, and that two expressions are the same. Thanks to hash-consing and smart caching, equality checks that used to take long complete up to 150 times faster. The core is small and auditable, and the package stays under 100 KB.
**For developers**, this means fewer bugs and clearer intent. You can verify algorithms, guard critical paths, and build interactive assistants that reason about program structure.
**For AI and ML teams**, it slots into Claude Code and similar systems to validate agent output, create verified training examples, and enforce logical constraints.
**For educators**, it runs directly in the browser, so students can explore type theory and proofs with no setup.
**Trust is first class.** Version 0.3.0 adds Ed25519 proof signing, so each proof can be tied to an agent identity and checked for authenticity. You get mathematical verification plus cryptographic attestation, with support for multi-agent consensus and tamper detection. Performance remains practical: microsecond-level key generation and signing, sub-millisecond verification, and throughput that supports real workflows.
**If it runs JavaScript, Lean-Agentic runs there too:** Browser, Node, Deno, or Bun.
### Key Features
- **โก 150x Faster**: Finds if two expressions are the same almost instantly using smart caching
- **๐ก๏ธ Type Safety**: Catches errors at design time, not runtime - like TypeScript but stronger
- **๐ Ed25519 Signatures**: Cryptographic proof attestation with agent identity (v0.3.0+)
- **๐ฆ Tiny Package**: Less than 100KB - smaller than most images on the web
- **โ
Trustworthy**: The core logic is just 1,200 lines of carefully verified code
- **๐ Works Everywhere**: Browser, Node.js, Deno, Bun - if it runs JavaScript, it works
- **๐ AI Integration**: Built-in support for Claude Code and other AI coding assistants
- **๐ฏ Developer Friendly**: Full TypeScript support with autocomplete and type checking
- **๐ Battle Tested**: Comprehensive benchmarks and tests ensure reliability
---
## ๐ Ed25519 Signature Verification (NEW in v0.3.0)
**Cryptographic attestation for formal proofs** - Add trust, authenticity, and non-repudiation to your theorems!
### What is Ed25519 Proof Signing?
lean-agentic now supports **cryptographic signatures** for mathematical proofs, combining:
- **Mathematical verification** (type checking - "Is this proof correct?")
- **Cryptographic verification** (Ed25519 signatures - "Who created this proof?")
This enables:
- ๐ **Agent Identity** - Know who generated each proof
- โ
**Dual Verification** - Both mathematical and cryptographic validation
- ๐ค **Multi-Agent Consensus** - Byzantine fault tolerant proof validation
- ๐ก๏ธ **Tamper Detection** - Automatically detect modified proofs
- ๐ **Chain of Custody** - Track complete proof provenance
- ๐ **Non-Repudiation** - Agents can't deny proofs they signed
### Performance
- **Key Generation**: 152 ฮผs per agent identity
- **Proof Signing**: 202 ฮผs overhead
- **Verification**: 529 ฮผs per proof
- **Throughput**: 93+ signed proofs per second
### How It Works
```javascript
const { AgentIdentity, SignedProof, ProofConsensus } = require('lean-agentic');
// 1. Create agent identity with Ed25519 keypair
const agent = AgentIdentity.new("researcher-001");
console.log(`Agent: ${agent.agentId}`);
console.log(`Public Key: ${agent.publicKeyHex()}`);
// 2. Sign a proof
const proofTerm = {
termId: "TermId(2)",
typeSig: "โA. A โ A",
body: "ฮปx:Type. x"
};
const signedProof = agent.signProof(
proofTerm,
"Identity function theorem",
"direct_construction"
);
console.log(`Signature: ${signedProof.signature.toHex()}`);
console.log(`Timestamp: ${signedProof.metadata.timestamp}`);
// 3. Verify signature
const isValid = signedProof.verifySignature();
console.log(`Cryptographically valid: ${isValid}`);
// 4. Full verification (math + crypto)
const trustedAgents = [agent.verifyingKey];
const result = signedProof.verifyFull(trustedAgents);
console.log(`Mathematically valid: ${result.mathematicallyValid}`);
console.log(`Cryptographically valid: ${result.cryptographicallyValid}`);
console.log(`Trusted agent: ${result.trusted}`);
```
### Multi-Agent Consensus
Build Byzantine fault tolerant systems where multiple agents must agree on proof validity:
```javascript
// Create multiple validator agents
const validator1 = AgentIdentity.new("validator-1");
const validator2 = AgentIdentity.new("validator-2");
const validator3 = AgentIdentity.new("validator-3");
// Validators reach consensus
const consensus = ProofConsensus.create(
signedProof,
[validator1, validator2, validator3],
2 // Need 2 out of 3 validators to agree
);
if (consensus) {
const isValid = consensus.verify();
console.log(`Consensus reached: ${consensus.validators.length}/3 validators`);
console.log(`Consensus valid: ${isValid}`);
} else {
console.log("Consensus not reached - insufficient signatures");
}
```
### Tamper Detection
Ed25519 signatures automatically detect any tampering:
```javascript
// Original proof - valid
const originalProof = agent.signProof(proofTerm, "Original", "direct");
console.log(`Original valid: ${originalProof.verifySignature()}`); // true
// Tampered proof - automatically detected
const tamperedProof = originalProof.clone();
tamperedProof.proofTerm.body = "ฮปx:Type. y"; // Changed!
console.log(`Tampered valid: ${tamperedProof.verifySignature()}`); // false โ
// Tamper detection is cryptographically guaranteed
```
### Use Cases
1. **AI Code Verification** - Verify which AI agent generated a proof
2. **Multi-Party Validation** - Require multiple experts to sign off on critical proofs
3. **Audit Trails** - Complete cryptographic chain of custody for regulatory compliance
4. **Distributed Systems** - Byzantine fault tolerant proof networks
5. **Trust Networks** - Build reputation systems for proof generators
6. **Academic Research** - Non-repudiation for published theorems
### Availability
**Currently**: Rust implementation (see `examples/ed25519_proof_signing.rs`)
**Coming Soon**: Full JavaScript/TypeScript bindings for Node.js and browser
To try Ed25519 signing now:
```bash
git clone https://github.com/agenticsorg/lean-agentic
cd lean-agentic
cargo run --example ed25519_proof_signing
```
## ๐ฆ Installation
### NPM
```bash
npm install lean-agentic
```
### Yarn
```bash
yarn add lean-agentic
```
### PNPM
```bash
pnpm add lean-agentic
```
### Global CLI
```bash
npm install -g lean-agentic
lean-agentic --help
```
## ๐ Quick Start
### Node.js
```javascript
const { createDemo } = require('lean-agentic/node');
// Create demo instance
const demo = createDemo();
// Identity function: ฮปx:Type. x
const identity = demo.createIdentity();
console.log(JSON.parse(identity));
// Demonstrate hash-consing
const hashDemo = demo.demonstrateHashConsing();
console.log(JSON.parse(hashDemo));
```
### Browser (ES Modules)
```html
<script type="module">
import { initWeb, createDemo } from 'lean-agentic/web';
// Initialize WASM
await initWeb();
// Create demo
const demo = createDemo();
const result = demo.createIdentity();
console.log(JSON.parse(result));
</script>
```
### TypeScript
```typescript
import { createDemo, LeanDemo } from 'lean-agentic';
const demo: LeanDemo = createDemo();
const identity: string = demo.createIdentity();
console.log(JSON.parse(identity));
```
## ๐ฎ CLI Usage
### Core Commands
#### Interactive Demo
```bash
npx lean-agentic demo
```
#### REPL
```bash
npx lean-agentic repl
```
#### Benchmarks
```bash
npx lean-agentic bench
```
#### System Info
```bash
npx lean-agentic info
```
### MCP Commands
#### Start MCP Server
```bash
npx lean-agentic mcp start
```
#### MCP Info
```bash
npx lean-agentic mcp info
```
### AgentDB Commands (NEW in v0.2.1)
#### Initialize Database
```bash
npx lean-agentic agentdb init
npx lean-agentic agentdb init --path ./my-theorems.db
```
#### Store Theorem
```bash
npx lean-agentic agentdb store
npx lean-agentic agentdb store --type identity --path ./my-theorems.db
```
#### Search Theorems
```bash
npx lean-agentic agentdb search "function that returns its input"
npx lean-agentic agentdb search "identity proof" --limit 10
```
#### Learn Patterns
```bash
npx lean-agentic agentdb learn
npx lean-agentic agentdb learn --path ./my-theorems.db
```
#### Database Statistics
```bash
npx lean-agentic agentdb stats
npx lean-agentic agentdb stats --path ./my-theorems.db
```
### Help
```bash
npx lean-agentic --help
npx lean-agentic agentdb --help
```
## ๐ Model Context Protocol (MCP) Integration
`lean-agentic` provides **first-class MCP support** for seamless integration with Claude Code, AI assistants, and other MCP-compatible tools.
### Quick Setup
Add lean-agentic to your Claude Code configuration:
```bash
# Option 1: Using npx (recommended - no installation required)
claude mcp add lean-agentic npx -y lean-agentic mcp start
# Option 2: Global installation
npm install -g lean-agentic
claude mcp add lean-agentic lean-agentic mcp start
# Or add manually to ~/.config/claude/mcp_config.json
{
"mcpServers": {
"lean-agentic": {
"command": "npx",
"args": ["-y", "lean-agentic", "mcp", "start"]
}
}
}
```
### MCP Capabilities
**๐ง Tools** (10 total: 5 theorem proving + 5 AgentDB tools):
*Theorem Proving Tools:*
- `create_identity` - Create identity function (ฮปx:Type. x)
- `create_variable` - Create de Bruijn indexed variables
- `demonstrate_hash_consing` - Demonstrate O(1) equality checks
- `benchmark_equality` - Run performance benchmarks (100k iterations)
- `get_arena_stats` - Get real-time arena statistics
*AgentDB Integration Tools (NEW in v0.2.1):*
- `agentdb_init` - Initialize AgentDB database for theorem storage
- `agentdb_store_theorem` - Store theorem with vector embeddings
- `agentdb_search_theorems` - Semantic search using WASM-accelerated vectors
- `agentdb_learn_patterns` - Learn from successful proofs with ReasoningBank
- `agentdb_get_stats` - Get database statistics and insights
**๐ Resources** (3 dynamic resources):
- `stats://arena` - Real-time arena and hash-consing statistics
- `info://system` - System capabilities and performance metrics
- `stats://agentdb` - AgentDB theorem database statistics (NEW)
**๐ก Prompts** (2 AI-optimized prompts):
- `theorem_prover` - Interactive theorem proving session
- `type_checker` - Type check and normalize expressions
### Example 1: Using lean-agentic with Claude Code
```
You: Using the lean-agentic MCP server, create an identity function
and demonstrate the 150x performance improvement from hash-consing.
Claude: I'll use the lean-agentic tools to demonstrate this:
1. Creating identity function...
[calls create_identity tool]
Result: ฮปx:Type. x (TermId(2))
2. Demonstrating hash-consing...
[calls demonstrate_hash_consing tool]
Result: All terms equal! O(1) pointer comparison achieved.
3. Running benchmark...
[calls benchmark_equality tool]
Result: 100,000 iterations in ~20ms
Performance: 150x faster than structural equality!
```
### Example 2: Using AgentDB Integration with Claude Code (NEW in v0.2.1)
```
You: Initialize AgentDB, store some theorems, and search for proofs
about identity functions using semantic similarity.
Claude: I'll use the AgentDB tools to set up theorem storage and search:
1. Initializing database...
[calls agentdb_init tool]
Result: Database created at ./lean-theorems.db with vector search enabled
2. Storing identity theorem...
[calls agentdb_store_theorem tool with statement="โA. A โ A" proof="ฮปx:A. x"]
Result: Theorem stored with ID 1, embeddings generated
3. Searching for similar theorems...
[calls agentdb_search_theorems tool with query="function that returns its input"]
Result: Found identity theorem with 94.2% similarity!
4. Learning patterns from proofs...
[calls agentdb_learn_patterns tool]
Result: Discovered pattern - direct_construction strategy used successfully
```
### Testing the MCP Server
```bash
# Navigate to MCP directory
cd node_modules/lean-agentic/mcp
# Run comprehensive test suite
node test-client.js
# Expected output: 10 tests covering tools, resources, and prompts
```
### MCP Server Features
- **stdio Transport**: Low-latency local communication
- **JSON-RPC 2.0**: Standards-compliant protocol
- **Async Operations**: Non-blocking tool execution
- **Error Handling**: Comprehensive error reporting
- **Type Safe**: Full TypeScript/JavaScript support
## ๐ง AgentDB Integration (NEW in v0.2.0!)
`lean-agentic` now includes [AgentDB](https://npmjs.com/package/agentdb) as a dependency, enabling AI-powered theorem proving capabilities:
### What You Get
**๐ Vector Search & Semantic Similarity**:
- Use AgentDB's EmbeddingService for theorem similarity
- Search proof strategies with natural language queries
- Leverage 150x faster WASM-accelerated vector search
**๐ง Learning from Proofs**:
- AgentDB's ReasoningBank learns patterns from successful proofs
- Episodic memory tracks proof attempts with causal graphs
- Pattern recognition identifies effective strategies
**๐ Integration Architecture**:
```
lean-agentic (Theorem Prover)
โ
LeanAgenticDB (Integration Layer)
โ
AgentDB (Vector DB + Learning)
โโโ EmbeddingService (Semantic search)
โโโ ReasoningBank (Pattern learning)
โโโ CausalMemoryGraph (Episodic memory)
```
### Quick Start
```bash
# AgentDB is already included as a dependency
npm install lean-agentic
# Use AgentDB's tools directly with theorems
npx agentdb --help
```
### Integration Module
The `LeanAgenticDB` class (in `src/agentdb-integration.js`) provides a bridge between lean-agentic theorems and AgentDB's learning capabilities. You can extend it for custom theorem storage and retrieval workflows.
```javascript
const { createDemo } = require('lean-agentic/node');
const { createDatabase, EmbeddingService } = require('agentdb');
// Use AgentDB services with theorems
const db = await createDatabase('./theorems.db');
const embeddings = new EmbeddingService(db);
// Generate embeddings for theorem statements
const theorem = 'โA. A โ A';
const embedding = await embeddings.generateEmbedding(theorem);
```
### Why This Matters
Combining lean-agentic's **150x faster equality checking** with AgentDB's **150x faster vector search** (via WASM SIMD) gives you:
- Sub-millisecond theorem proving
- Sub-millisecond proof similarity search
- Real-time proof recommendations
- Continuous learning from successful proofs
**Two WASM engines, one powerful system!**
## ๐ API Reference
### Node.js API
```javascript
const { LeanDemo, createDemo, quickStart } = require('lean-agentic/node');
// Create instance
const demo = createDemo();
// Methods
demo.createIdentity() // โ string (JSON)
demo.createApplication() // โ string (JSON)
demo.demonstrateHashConsing() // โ string (JSON)
// Quick start
const result = await quickStart();
```
### Browser API
```javascript
import { initWeb, createDemo } from 'lean-agentic/web';
// Initialize (required for browser)
await initWeb();
// Create instance
const demo = createDemo();
// Same methods as Node.js
demo.createIdentity();
demo.createApplication();
demo.demonstrateHashConsing();
```
### Bundler API
```javascript
import { init, createDemo } from 'lean-agentic';
// Initialize
await init();
// Use demo
const demo = createDemo();
const result = demo.createIdentity();
```
## ๐ฏ Examples
### 1. Identity Function
```javascript
const demo = createDemo();
const identity = demo.createIdentity();
// Output:
// {
// "term": "Lam",
// "binder": { "name": "x", "ty": "Type" },
// "body": "Var(0)"
// }
```
### 2. Hash-Consing Demo
```javascript
const demo = createDemo();
const hashDemo = demo.demonstrateHashConsing();
// Shows that identical terms have the same TermId
// Equality check is O(1) pointer comparison!
```
### 3. Performance Benchmark
```javascript
const demo = createDemo();
const iterations = 100000;
console.time('Hash-consed equality');
for (let i = 0; i < iterations; i++) {
demo.demonstrateHashConsing();
}
console.timeEnd('Hash-consed equality');
// Typical: ~20ms for 100k iterations
```
## ๐ Platform Support
| Platform | Support | Import |
|----------|---------|--------|
| Node.js 18+ | โ
| `lean-agentic/node` |
| Browser (ESM) | โ
| `lean-agentic/web` |
| Webpack | โ
| `lean-agentic` |
| Vite | โ
| `lean-agentic` |
| Rollup | โ
| `lean-agentic` |
| Deno | โ
| `npm:lean-agentic` |
| Bun | โ
| `lean-agentic` |
## ๐ Performance
| Operation | Latency | Speedup |
|-----------|---------|---------|
| Hash-consed equality | 0.3ns | 150x |
| Arena allocation | 1.9ns | 5.25x |
| Term construction | <10ns | - |
| WASM overhead | <1ฮผs | - |
## ๐๏ธ Architecture
```
lean-agentic (NPM Package)
โโโ WASM Bindings
โ โโโ Node.js target (CommonJS)
โ โโโ Web target (ES Modules)
โ โโโ Bundler target (ES Modules)
โโโ JavaScript Wrappers
โ โโโ src/index.js (Universal)
โ โโโ src/node.js (Node.js)
โ โโโ src/web.js (Browser)
โโโ CLI Tool
โ โโโ cli/index.js
โโโ TypeScript Definitions
โโโ dist/index.d.ts
โโโ dist/node.d.ts
โโโ dist/web.d.ts
```
## ๐ง Building from Source
### Prerequisites
- Rust 1.90+
- Node.js 18+
- wasm-pack
### Build Steps
```bash
# Clone repository
git clone https://github.com/agenticsorg/lean-agentic
cd lean-agentic
# Build WASM
cd leanr-wasm
wasm-pack build --target nodejs --out-dir ../npm/lean-agentic/wasm-node
wasm-pack build --target web --out-dir ../npm/lean-agentic/wasm-web
wasm-pack build --target bundler --out-dir ../npm/lean-agentic/wasm
# Install dependencies
cd ../npm/lean-agentic
npm install
# Run examples
npm run example:node
npm run example:web
```
## ๐ Learn More
### Documentation
- **NPM Package**: https://npmjs.com/package/lean-agentic
- **Rust Crate**: https://docs.rs/lean-agentic
- **API Docs**: https://docs.rs/lean-agentic
- **Examples**: See `examples/` directory
### Related Projects
- [`lean-agentic` (Rust)](https://crates.io/crates/lean-agentic) - Core library
- [`leanr-wasm`](https://crates.io/crates/leanr-wasm) - WASM bindings
- [Lean 4](https://lean-lang.org) - Inspiration
## ๐ค Contributing
Contributions are welcome! See [CONTRIBUTING.md](https://github.com/agenticsorg/lean-agentic/blob/main/CONTRIBUTING.md)
## ๐ License
Licensed under **Apache-2.0** - see [LICENSE](https://github.com/agenticsorg/lean-agentic/blob/main/LICENSE)
## ๐ Credits
**Created by**: [ruv.io](https://ruv.io)
**Maintained by**: [github.com/ruvnet](https://github.com/ruvnet)
**Powered by**: Flow Nexus, AgentDB, Claude Flow
## ๐ Support
- **Docs**: https://docs.rs/lean-agentic
- **Repo**: https://github.com/agenticsorg/lean-agentic
- **Issues**: https://github.com/agenticsorg/lean-agentic/issues
- **NPM**: https://npmjs.com/package/lean-agentic
- **Website**: https://ruv.io
## ๐ Use Cases
- **Formal Verification**: Verify software correctness with dependent types
- **Proof Assistants**: Build interactive theorem proving tools
- **Type-Level Programming**: Leverage dependent types in JavaScript/TypeScript
- **AI-Assisted Development**: Integrate with Claude Code via MCP
- **Educational Tools**: Teach type theory and formal methods
- **Research Projects**: Experiment with proof strategies and tactics
- **Compiler Development**: Type checking and normalization
- **Code Generation**: Generate provably correct code
## ๐ท๏ธ Keywords
`theorem prover` ยท `dependent types` ยท `formal verification` ยท `hash consing` ยท `type theory` ยท `WebAssembly` ยท `WASM` ยท `proof assistant` ยท `Lean4` ยท `type checker` ยท `lambda calculus` ยท `Model Context Protocol` ยท `MCP` ยท `Claude Code` ยท `AI assistant` ยท `arena allocation` ยท `zero copy` ยท `performance` ยท `TypeScript` ยท `JavaScript` ยท `Node.js` ยท `browser` ยท `npm package`
## ๐ Project Stats
- **Package Size**: <100KB minified + gzipped
- **Dependencies**: Zero runtime dependencies
- **Browser Support**: All modern browsers (ES2020+)
- **Node.js**: v18.0.0 or higher
- **WASM Binary**: 65.6KB optimized
- **Performance**: 150x faster equality, 85% memory reduction
- **Code Quality**: Fully typed, tested, and documented
## ๐ค Contributing
Contributions are welcome! This project is open source under Apache-2.0 license.
- **Report Issues**: [GitHub Issues](https://github.com/agenticsorg/lean-agentic/issues)
- **Submit PRs**: [Pull Requests](https://github.com/agenticsorg/lean-agentic/pulls)
- **Discussions**: [GitHub Discussions](https://github.com/agenticsorg/lean-agentic/discussions)
## ๐ License
Apache-2.0 - See [LICENSE](./LICENSE) for details
## ๐ Related Projects
- **Lean 4**: https://lean-lang.org
- **Model Context Protocol**: https://modelcontextprotocol.io
- **Claude Code**: https://claude.com/claude-code
- **AgentDB**: Vector storage for AI agents
- **ReasoningBank**: Pattern learning for agents
**Built with formal verification** ยท **Powered by hash-consing** ยท **Developed by [ruv.io](https://ruv.io)** ยท **[GitHub](https://github.com/ruvnet)**