UNPKG

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
# lean-agentic **Hash-consed dependent types with 150x faster equality + Ed25519 proof signatures** [![npm](https://img.shields.io/npm/v/lean-agentic)](https://npmjs.com/package/lean-agentic) [![npm downloads](https://img.shields.io/npm/dm/lean-agentic.svg)](https://www.npmjs.com/package/lean-agentic) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/lean-agentic)](https://bundlephobia.com/package/lean-agentic) [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/agenticsorg/lean-agentic/blob/main/LICENSE) [![Crates.io](https://img.shields.io/crates/v/lean-agentic)](https://crates.io/crates/lean-agentic) [![Model Context Protocol](https://img.shields.io/badge/MCP-supported-blue)](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)**