Gives AI agents repo-local memory and architectural guardrails. Frames capture work snapshots with reference points, blockers, next actions, and touched modules. Recall pulls relevant prior context on demand, optionally enriched with Atlas neighborhood maps showing module dependencies and permissions. Policy files define architectural boundaries as code, enforced via CLI checks in CI. The MCP surface exposes remember, recall, check, and instruction generation as tool calls. Stores everything in SQLite and repository files, not cloud services. Useful when your agent keeps asking you to repeat project constraints, architecture decisions, or context from interrupted work sessions.
Local memory and policy context for AI coding agents.
Don't trust the README. Ask your agent.
"Would repo-local memory and policy context help you work in this codebase with fewer repeated explanations, fewer forgotten constraints, and better handoffs between sessions?"
If the answer is yes, Lex is the layer that gives your agent somewhere durable to put that context.
60-second test | Quick Start | Agent Integration | Documentation | API Reference | Contributing
Lex is repo-local memory and policy context for AI coding agents. It stores structured work snapshots, recalls relevant prior context, and connects that context to repository policy when a policy file is available.
Agents get:
Humans do not need to understand the whole framework before adoption. The first useful question is simply whether your agent keeps asking you to repeat context the repository could remember.
Paste this into Copilot, Claude, Cursor, or another coding agent while it has access to your repository:
Look at this repository. Would a repo-local memory and policy layer help you avoid repeated context setup, forgotten project constraints, or poor handoffs? If yes, where would Lex help first?
A useful answer usually names a recurring handoff problem: interrupted feature work, architecture-sensitive modules, policy constraints, release workflows, or places where agents repeatedly need the same explanation.
Frames capture meaningful moments in a development workflow:
lex remember \
--reference-point "Implementing user authentication" \
--summary "Added JWT validation to API middleware" \
--next "Wire up password reset flow" \
--modules "services/auth,api/middleware" \
--blockers "Need PermissionService access - forbidden edge in policy" \
--jira "AUTH-123"
Frames are meant for durable agent context: what changed, what remains, where it happened, and what should be respected next time.
Later, an agent can recall relevant context by topic:
lex recall "authentication"
# Returns matching context, blockers, next action, and relevant module neighborhood.
Recall is the everyday adoption path: before changing code, ask Lex what prior work, blockers, and nearby boundaries matter.
When a Frame is recalled, Lex can provide an Atlas Frame: the touched modules plus their immediate neighborhood, including dependencies, dependents, and permissions.
This fold-radius approach keeps recall focused on nearby repository context instead of flooding the agent with the whole codebase.
Lex policy files define repository boundaries as code:
{
"modules": {
"ui/components": {
"owns": ["src/ui/components/**"],
"mayCall": ["services/auth", "ui/shared"],
"forbidden": [
{
"target": "database/queries",
"reason": "UI must not access database directly. Use API layer."
}
]
}
}
}
Enforce in CI:
lex check merged-facts.json
# Violation: ui/components -> database/queries (forbidden edge)
# Reason: UI must not access database directly. Use API layer.
Lex can project canonical assistant instructions into host-specific files:
# Your canonical instructions live in one place
.smartergpt/instructions/lex.md
# Project them to host-specific files
lex instructions generate
# Creates: .github/copilot-instructions.md, .cursorrules
The marker system preserves human content while keeping generated agent guidance deterministic.
# Install globally (recommended)
npm install -g @smartergpt/lex
# Or locally in your project
npm install @smartergpt/lex
Requires Node.js 20+ and currently supports Node.js 20 through 24, matching the package engine range.
WSL users should use a native WSL install on PATH, not Windows npm shims or npm's _npx cache. See WSL Native Lex Install for the recommended user-local install, checkout symlink bridge, and native SQLite build requirements.
Lex supports structured output (--json), recoverable errors (AXError), and Frame Schema v3 for orchestrator integration. Commands provide both human-readable and machine-parseable output where supported.
# Zero-to-value initialization
npx @smartergpt/lex init --yes
# Auto-detects project type (Node.js, Python, Rust, Go, etc.)
# Creates:
# .smartergpt/ - Workspace with prompts, policy, and instructions
# .github/copilot-instructions.md - IDE instructions with LEX markers
# .cursorrules - Cursor IDE instructions (if Cursor detected)
# lex.yaml - Configuration with sensible defaults
# .smartergpt/lex/memory.db - SQLite database (initialized on first use)
# Shows MCP server configuration guidance
# Idempotent: safe to run multiple times
# Interactive mode (prompts for first Frame)
lex init --interactive
# Generate seed policy from directory structure
lex init --policy
# Scans src/ for TypeScript/JavaScript modules
# Generates .smartergpt/lex/lexmap.policy.json with discovered modules
# Example: src/memory/store/ -> memory/store module with src/memory/store/** match pattern
# Force reinitialize (overwrite existing files)
lex init --force
During init, Lex detects common project types, creates IDE instruction files with LEX markers, writes lex.yaml, initializes .smartergpt/lex/memory.db, and prints MCP server configuration guidance.
lex remember \
--reference-point "Refactoring payment processing" \
--summary "Extracted validation logic to PaymentValidator" \
--next "Add unit tests for edge cases" \
--modules "services/payment"
lex recall "payment"
# Shows your context, blockers, and architectural neighborhood.
Use Lex with any MCP-compatible AI assistant, including VS Code and Claude Desktop:
# Install the MCP wrapper
npm install -g @smartergpt/lex-mcp
# Or run directly
npx @smartergpt/lex-mcp
VS Code configuration (.vscode/mcp.json):
{
"servers": {
"lex": {
"command": "npx",
"args": ["@smartergpt/lex-mcp"],
"env": {
"LEX_WORKSPACE_ROOT": "${workspaceFolder}"
}
}
}
}
Claude Desktop configuration:
{
"mcpServers": {
"lex": {
"command": "npx",
"args": ["@smartergpt/lex-mcp"]
}
}
}
Lex is designed to be consumed by agents and tools, not only read by humans.
lex remember, lex recall, lex check, lex timeline, lex instructions, and database maintenance commands are scriptable entry points.@smartergpt/lex-mcp exposes Lex through Model Context Protocol for assistants that support MCP.code, message, context, and nextActions[] fields.The MCP server exposes 14 tools for episodic memory, policy validation, and architectural analysis:
frame_create, frame_search, frame_get, frame_list, frame_validatepolicy_check, timeline_show, atlas_analyzesystem_introspect, help, hints_getcontradictions_scan, db_stats, turncost_calculateMCP is an adapter surface. Lex itself is the memory and policy core.
Dogfooding examples are available in examples/dogfood/. They show real Frames from building Lex with Lex.
Humans see a local, inspectable record instead of an opaque assistant memory:
.smartergpt/lex/memory.db by default..smartergpt/lex/logs/lex.log.ndjson with structured fields such as timestamp, level, operation, duration_ms, metadata, and error.lex check merged-facts.json.# Create a timestamped backup (memory-20251123.sqlite)
lex db backup --rotate 7
# Keeps last 7 backups, stored in .smartergpt/lex/backups/
# Optimize database (rebuild and compact)
lex db vacuum
# Set backup retention via environment variable
export LEX_BACKUP_RETENTION=14 # Keep 14 most recent backups
Logs rotate automatically at 100MB and are silent in test mode unless LEX_LOG_NDJSON=1 is set.
Lex is the memory and policy core.
@smartergpt/lex/lexsona) for persona and guardrail integration. Lex core remains persona-agnostic.A useful mental model: Lex remembers and explains context; AXF exposes bounded capabilities; LexRunner coordinates work; LexSona derives behavioral constraints.
Lex provides multiple entry points for agents, tools, and application code:
import { saveFrame, searchFrames, getDb, closeDb } from '@smartergpt/lex';
const db = getDb(); // Uses .smartergpt/lex/memory.db
await saveFrame(db, {
referencePoint: 'authentication flow',
summaryCaption: 'Added password validation',
statusSnapshot: { nextAction: 'Wire up permission check' },
moduleScope: ['services/auth', 'services/password'],
branch: 'feature/auth',
jira: 'AUTH-123'
});
const results = await searchFrames(db, { referencePoint: 'authentication' });
closeDb(db);
| Import | Purpose | Documentation |
|---|---|---|
@smartergpt/lex | Core API + store operations | API Usage |
@smartergpt/lex/cli | Programmatic CLI access | CLI Output |
@smartergpt/lex/cli-output | CLI JSON utilities | CLI Output |
@smartergpt/lex/store | Direct database operations | Store Contracts |
@smartergpt/lex/types | All shared types | API Usage |
@smartergpt/lex/errors | AXError schema and utilities (v2.0+) | AX Contract |
@smartergpt/lex/policy | Policy loading & validation | API Usage |
@smartergpt/lex/atlas | Atlas Frame generation | Architecture |
@smartergpt/lex/atlas/code-unit | Code unit schemas | Atlas |
@smartergpt/lex/atlas/schemas | Atlas schemas | Atlas |
@smartergpt/lex/aliases | Module alias resolution | Aliases |
@smartergpt/lex/module-ids | Module ID validation | API Usage |
@smartergpt/lex/memory | Frame payload validation | API Usage |
@smartergpt/lex/logger | NDJSON logging | API Usage |
@smartergpt/lex/lexsona | Behavioral memory socket (v2.0+) | Control Stack |
@smartergpt/lex/prompts | Template system | Canon Architecture |
Current Version: 2.7.1 (Changelog)
Current Lex releases include structured output, recoverable errors, and Frame Schema v3 for agent and orchestrator integration.
Commonly used for:
Current capability highlights:
runId, planHash, and toolCalls for orchestration (Schema Docs)code, message, context, and nextActions[] for programmatic recoverylex remember --json and lex timeline --json with machine-parseable event streamslex instructions CLI for syncing AI instructions across IDEsrecordCorrection/getRules) exported via @smartergpt/lex/lexsonaLex 2.0.0 introduced the public behavioral memory socket (@smartergpt/lex/lexsona) for persona-based workflows. LexSona is a separate private package that consumes this socket to enable offline-capable persona modes. Lex itself is persona-agnostic.
See the changelog for release history and version-specific notes.
Contributions are welcome.
Follow the repo's commit conventions and signing requirements documented in the contributing guide.
MIT License. See LICENSE for full text.
Lex uses TypeScript project references for deterministic, incremental builds:
npm run build # Compile with project references
npm run clean # Clean build artifacts
npm run typecheck # Type-check without emitting
Why NodeNext module resolution?
.ts files with .js import extensions..js files work correctly in Node.js ESM.Run CI checks locally without touching GitHub:
npm run local-ci # Run full CI suite locally
npm run local-ci:nonet # Run without network access
This uses ci.Dockerfile for local parity with CI checks.
While TypeScript scanning is built in, Python and PHP scanners are available as examples:
# Scan Python codebase
python examples/scanners/python/scan.py src/ > python-facts.json
# Scan PHP codebase
php examples/scanners/php/scan.php src/ > php-facts.json
# Merge with TypeScript facts
lex merge ts-facts.json python-facts.json > merged-facts.json
# Check policy
lex check merged-facts.json
Security note: external scanners execute arbitrary code. Review before use.
Lex uses a precedence chain for configuration:
LEX_CANON_DIR=/custom/canon (highest).smartergpt/prompts/prompts/ (lowest)# Customize locally
cp prompts/remember.md .smartergpt/prompts/
vim .smartergpt/prompts/remember.md
# Or use custom directory
LEX_CANON_DIR=/my/custom/canon lex remember ...
| Variable | Purpose | Default |
|---|---|---|
LEX_LOG_LEVEL | Log verbosity (silent, trace, debug, info, warn, error, fatal) | info (tests: silent) |
LEX_LOG_PRETTY | Pretty-print logs (1 = enabled) | Auto-detect TTY |
LEX_POLICY_PATH | Custom policy file location | .smartergpt/lex/lexmap.policy.json |
LEX_DB_PATH | Database location | .smartergpt/lex/memory.db |
LEX_DB_KEY | Database encryption passphrase (required in production) | None (unencrypted) |
LEX_GIT_MODE | Git integration (off, live) | off |
LEX_DEFAULT_BRANCH | Override default branch detection | Auto-detect from git |
LEX_CANON_DIR | Override canonical resources root | Package defaults |
LEX_PROMPTS_DIR | Override prompts directory | Package defaults |
LEX_SCHEMAS_DIR | Override schemas directory | Package defaults |
LEX_CLI_OUTPUT_MODE | CLI output format (plain or jsonl) | plain |
LEX_BACKUP_RETENTION | Number of database backups to retain | 7 |
SMARTERGPT_PROFILE | Profile configuration path | .smartergpt/profile.yml |
Protect your Frame data with SQLCipher encryption:
# Enable encryption for new databases
export LEX_DB_KEY="your-strong-passphrase-here"
lex remember --reference-point "work" --summary "Encrypted!"
# Migrate existing database
lex db encrypt --verify
# Production mode requires encryption
export NODE_ENV="production"
export LEX_DB_KEY="production-passphrase"
Key features:
NODE_ENV=production)# Clone repository
git clone https://github.com/Guffawaffle/lex.git
cd lex
# Install dependencies
npm ci
# Build
npm run build
# Run tests
npm test
# Local CI (full suite)
npm run local-ci
lex/
|-- src/ # TypeScript source (no .js files)
| |-- memory/ # Frame storage and MCP server
| |-- policy/ # Policy enforcement and scanners
| |-- shared/ # Shared utilities and types
| `-- index.ts # Main entry point
|-- dist/ # Build output (gitignored)
|-- canon/ # Canonical prompts and schemas
|-- docs/ # Documentation
|-- examples/ # Usage examples and optional scanners
|-- test/ # Test suite
`-- .smartergpt/ # Local workspace (gitignored)
npm test # Run all tests (excludes git tests)
npm run test:coverage # With coverage report
npm run test:watch # Watch mode
npm run test:git # Git integration tests (requires non-interactive signing)
Git tests are quarantined due to mandatory GPG signing in this environment. See test/README.md for details.
npm run lint # ESLint checks
npm run format # Prettier formatting
npm run typecheck # TypeScript validation
LEX_WORKSPACE_ROOTRoot directory for the workspace. Defaults to current working directory.
LEX_MEMORY_DBPath to SQLite database file. Defaults to .smartergpt/lex/lex.db in workspace root.
LEX_DEBUGSet to any value to enable debug logging to stderr.
io.github.ericm1018/skillfm-llm-cost-optimizer-openai-anthropic-usage
io.github.mikerawsonnz/llm-orchestration-agent
io.github.mikerawsonnz/authenticated-llm-agent
labforgedev/copilot-memory-mcp
csoai-org/agent-prompt-injection-firewall-mcp
io.github.mikerawsonnz/authenticated-multi-llm-agent