AI-powered code generation with formal correctness guarantees for distributed systems using CEGIS and Z3 SMT solver.
Guardrails: Atomic automatically generates formally verified TypeScript handlers for distributed systems. It uses:
- π€ AI Code Generation: GPT-4 via OpenRouter
- β Formal Verification: Z3 SMT Solver proves correctness
- π CEGIS Loop: Iterative synthesis with counterexample-guided repair
- π‘οΈ Fault Tolerance: Handles at-least-once delivery, reordering, crashes
Writing correct distributed systems code is hard. Common bugs include:
- Double-spending under duplicate message delivery
- Race conditions from message reordering
- Partial state updates after crashes
- Idempotency violations
Guardrails: Atomic proves your code is correct before you deploy it.
- Node.js 20+
- pnpm (package manager)
- OpenRouter API Key: openrouter.ai/keys
Optional (for production):
- Clerk account: clerk.com
- MongoDB Atlas: mongodb.com/atlas
# 1. Clone the repository
git clone https://github.com/yourusername/guardrails-atomic.git
cd guardrails-atomic
# 2. Install dependencies
pnpm install
# 3. Set up environment variables
cp .env.local.example .env.local
# Edit .env.local and add your OPENROUTER_API_KEY
# 4. Run development server
pnpm devUser YAML Spec
β
βββββββββββββββββββββββββββββββββββ
β CEGIS Loop (max 8 iterations) β
β β
β 1. LLM generates TypeScript β
β 2. Translate to Z3 constraints β
β 3. Run Z3 solver β
β 4. If 'sat' (bug found): β
β β Extract counterexample β
β β Repair code with LLM β
β 5. If 'unsat' (verified): β
β
βββββββββββββββββββββββββββββββββββ
β
Formally Verified Code
-
Describe your distributed system handler in YAML:
name: transfer_atomic invariants: - type: idempotent # No double-spending - type: conservation # Balance preserved faultModel: delivery: at_least_once # Messages may duplicate
-
Generate code with AI
-
Verify with Z3:
- β If bugs found β Z3 provides counterexample β LLM repairs
- β If verified β You get proven-correct code!
name: transfer_atomic
signature:
params:
- name: state
type: Map<Acct,int>
- name: from
type: Acct
- name: to
type: Acct
- name: amt
type: int
- name: req_id
type: UUID
returnType: Map<Acct,int>
preconditions:
- amt >= 0
- from != to
- state[from] >= amt
postconditions:
- sum(result.values()) == sum(state.values())
invariants:
- type: idempotent
- type: conservation
faultModel:
delivery: at_least_once
reorder: true
crash_restart: true
bounds:
accts: 3
retries: 2function transfer_atomic(
state: Map<string, number>,
from: string,
to: string,
amt: number,
req_id: string,
processedRequests: Set<string>
): Map<string, number> {
// Idempotency check
if (processedRequests.has(req_id)) {
return state; // Already processed, no-op
}
// Precondition checks
if (amt < 0 || from === to || (state.get(from) ?? 0) < amt) {
throw new Error('Precondition violated');
}
// Atomic state update
const newState = new Map(state);
newState.set(from, (newState.get(from) ?? 0) - amt);
newState.set(to, (newState.get(to) ?? 0) + amt);
// Mark as processed
processedRequests.add(req_id);
return newState;
}Guarantees: β Idempotent - duplicate messages have no effect β Conservation - total balance preserved β Atomic - no partial updates β Formally verified by Z3 SMT solver
- π Architecture Guide - System design, CEGIS loop, Z3 integration
- π οΈ Development Guide - Setup, testing, debugging
- π€ CLAUDE.md - AI assistant guide (for Claude Code)
| Component | Technology |
|---|---|
| Frontend/Backend | Next.js 15, React 19 |
| Language | TypeScript (100%) |
| Formal Verification | Z3 SMT Solver |
| AI Code Generation | OpenRouter (GPT-4) |
| Authentication | Clerk |
| Database | MongoDB Atlas |
| Testing | Vitest |
| Styling | Tailwind CSS |
guardrails-atomic/
βββ app/ # Next.js App Router
β βββ api/ # API routes
β β βββ verify/ # Main verification endpoint
β β βββ generate-spec/ # NL to YAML generator
β β βββ ...
β βββ verify/ # Verification UI
β βββ examples/ # Example specs
β
βββ lib/ # Core logic
β βββ core/ # CEGIS loop
β βββ verification/ # Z3 integration
β βββ types/ # TypeScript types
β
βββ components/ # React components
βββ docs/ # Documentation
βββ tests/ # Test suite
βββ scripts/ # Build scripts
# Run all tests
pnpm test
# Watch mode
pnpm test --watch
# UI mode
pnpm test:ui# Development
pnpm dev
# Production build
pnpm build
pnpm start
# Linting
pnpm lint- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- π User-provided API keys (bring your own OpenRouter/OpenAI key)
- πΎ Caching of verified implementations
- π Multi-language code generation (Python, Go, Rust)
- π§© Custom invariants via user-defined predicates
- π€ Real-time collaboration
- Neural-guided synthesis (ML to predict good candidates)
- Compositional verification (verify modules independently)
- Automatic fault model inference from tests
MIT
- Z3 Solver: Microsoft Research
- OpenRouter: LLM API aggregation
- Next.js & Vercel: Web framework and deployment
- Clerk: Authentication
- MongoDB: Database
Built during ALIHacks (2025) Modernized and production-ready (2025)