N2 Clotho
Compiled instruction language for AI agents β Define, compile, and enforce rules that markdown can't. Includes WASM runtime.
Ask AI about N2 Clotho
Powered by Claude Β· Grounded in docs
I know everything about N2 Clotho. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
KR νκ΅μ΄
Clotho β The Thread of Fate for AI Agents
Define it. Compile it. Enforce it.
Clotho is a compiled instruction language that turns natural-language AI rules into deterministic, enforceable contracts. It replaces fragile markdown files (GEMINI.md, .cursorrules, CLAUDE.md) with .n2 β a structured language that is parsed, validated, and enforced so AI agents follow your rules every time.
Named after Clotho, the Greek goddess who spins the thread of fate β because once you define the rules, they become destiny.
The Problem
Clotho in 6 Panels
Panel 1: You write the rules: "Always write tests. Keep files under 500 lines." Panel 2: AI reads them... writes 847 lines with 0 tests. "Yeah I read it. Anyway..." Panel 3: Every. Single. Time. "I WROTE IT IN THE RULES!!" β "Those were more like... guidelines " Panel 4: The fix:
npm install n2-clothoβ Compiled rules. Not suggestions. Panel 5: "Hey, use Clotho to write rules YOU can't break. Apply them to yourself." Panel 6: AI hits the wall: "I literally can't skip it?!" β Compiled rules hit different.
Every AI coding tool has its own rule system. None of them enforce anything.
| Tool | Rule File | What Happens |
|---|---|---|
| Gemini | GEMINI.md | Hopes AI reads it |
| Cursor | .cursorrules | Hopes AI follows it |
| Claude | CLAUDE.md | Hopes AI remembers it |
| Windsurf | .windsurfrules | Hopes AI cares |
Result? You write 200 lines of careful rules. The AI reads them, says "Got it!" β then uses any everywhere, skips tests, and names variables temp1. The rules exist, but enforcement is zero.
The Solution
One compiled language to replace them all.
Before (.md + .json + skill files): After (.n2):
βββββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββ
β GEMINI.md β coding rules β β β
β .cursorrules β editor prefs β β project.n2 β
β workflows/*.md β dev pipelines β β β
β config.json β tool settings β β One file. β
β system.txt β agent persona β β Compiled. β
β .env β variables β β Enforced. β
βββββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββ
6+ files, zero enforcement 1 file, full enforcement
Clotho introduces .n2 β a compiled language with:
| Feature | Description |
|---|---|
| Compilation | PEG parser β AST β validated execution plan |
| Type Safety | Schema-defined types with constraints ([required], [range: 0..3]) |
| Contracts | State machine behavioral contracts β violation = blocked action |
| Enforcement | strict / warn / passive modes per block |
| Determinism | Same .n2 file β always the same execution plan |
| SQL Queries | Query your rules like a database |
| Multi-Target | Compile to 6 languages (Rust, C, C++, Go, Python, TypeScript) |
What Can You Build With Clotho?
Clotho is a general-purpose rule compiler. Here's what you can define:
| Use Case | What .md Does | What .n2 Does |
|---|---|---|
| Coding Standards | "Please use strict TypeScript" | no_any_type, max_file_lines: 500 β compiler-enforced |
| Workflows | "Follow these steps: 1, 2, 3..." | State machine β skip a step β BLOCKED |
| Agent Personas | "Be friendly and professional" | Schema-typed tone/expertise with invariant checks |
| Project Conventions | "Every folder needs README.md" | readme_required, no_temp_files β auto-checked |
| Security Gates | "Don't run rm -rf" | Blacklist rules β Ark enforces at runtime |
| Multi-Agent | "Don't edit the same file" | Ownership contracts with state machine transitions |
Quick example β coding standards that can't be ignored:
@rule TypeScriptStrict {
scope: code
enforce: strict
checks: [no_any_type, max_file_lines: 500, max_function_lines: 50]
}
Before: "Please use strict TypeScript" β AI uses
any47 times. After: Compiled rule blocks everyanybefore it reaches your codebase.
Deep dive: .md Skills vs .n2 Contracts β full comparison with examples
Quick Start
Installation
The easiest way to get started? Just ask your AI to write a
.n2file for you.
# npm (WASM β use in Node.js)
npm install n2-clotho
// Usage in Node.js
const { parse_n2_wasm, validate_n2_wasm, query_n2_wasm } = require('n2-clotho');
const ast = parse_n2_wasm(n2Source); // Parse β AST JSON
const result = validate_n2_wasm(n2Source); // Validate β errors/warnings
const table = query_n2_wasm(n2Source, 'SELECT * FROM rules'); // SQL query
# From source (Rust required β full CLI)
git clone https://github.com/choihyunsus/n2-clotho.git
cd n2-clotho/compiler
cargo build --release
# Binary is at target/release/n2-compiler
Write Your First .n2 File
# my-project.n2 β Define your project's rules
@meta {
name: "my-project-rules"
version: "1.0.0"
description: "Coding standards + workflow for my team"
enforce: strict
}
@rule CodingStandards {
description: "Team coding conventions"
scope: code
enforce: strict
checks: [
no_any_type,
max_file_lines: 500,
max_function_lines: 50,
explicit_return_types
]
}
@workflow Deploy {
description: "Safe deployment pipeline"
trigger: on_command("deploy")
enforce: strict
step build {
action: run_build()
expect { pass => continue }
}
step test {
depends_on: build
action: run_tests()
expect {
all_pass => continue
fail => abort with "Fix tests before deploying"
}
}
step deploy {
depends_on: test
action: deploy_to(env: $TARGET_ENV)
required: true
}
}
Compile & Validate
# Parse and output AST
n2c my-project.n2
# Full validation pipeline: parse β schema check β contract verify
n2c validate my-project.n2
# Simulate state machine contracts
n2c simulate my-project.n2
# Query rules with SQL
n2c query my-project.n2 "SELECT * FROM rules WHERE enforce = 'strict'"
# Multi-target compile (v3.0.0)
n2c compile my-project.n2 rust # β my-project.n2rs
n2c compile my-project.n2 go # β my-project.n2go
n2c compile my-project.n2 all # β all 6 targets
n2c backends # List supported targets
Multi-Target Compilation
Clotho compiles .n2 contracts to 6 target languages β securing complete IP coverage across every platform:
| Target | Extension | Use Case |
|---|---|---|
| Rust | .n2rs | High-performance native runtime |
| C | .n2c | Embedded/IoT/System |
| C++ | .n2c2 | Game engines/HPC |
| Go | .n2go | Cloud/Microservices |
| Python | .n2py | AI/ML pipelines |
| TypeScript | .n2ts | Web/Node.js/MCP |
$ n2c compile project.n2 all
All targets batch compile
rust β project.n2rs (1523 bytes)
c β project.n2c (989 bytes)
cpp β project.n2c2 (1124 bytes)
go β project.n2go (828 bytes)
python β project.n2py (1144 bytes)
ts β project.n2ts (979 bytes)
Result: 6 success, 0 fail / 6 targets
MCP Server
Clotho includes an MCP server so AI agents can compile and validate contracts programmatically:
| MCP Tool | Description |
|---|---|
clotho_compile | Compile to a specific target |
clotho_batch | Compile to all 6 targets at once |
clotho_validate | Syntax + schema + state machine check |
clotho_backends | List supported backends |
clotho_inspect | Read compiled contract contents |
// MCP configuration
{
"mcpServers": {
"n2-clotho": {
"command": "node",
"args": ["path/to/n2-clotho/mcp/server.js"]
}
}
}
Real Compiler Output
Here's actual n2c validate output β a complete pipeline with state machine, rules, and SQL queries:
n2c v3.0.0 β Clotho Multi-Target Compiler
File: project.n2
ββ Step 1: Parse
Blocks: @meta:1 @contract:1 @rule:2 @workflow:1 @query:1 | Total 6
ββ Step 2: Schema Validation
All checks passed! 0 errors, 0 warnings
ββ Step 3: Contract Check
State Machine: DevLifecycle (initial: IDLE, 7 transitions)
IDLE -[start]-> CODING
CODING -[lint]-> REVIEWING
REVIEWING -[approve]-> TESTING
TESTING -[pass]-> DEPLOYING
TESTING -[fail]-> CODING β auto-rollback on failure
DEPLOYING -[complete]-> IDLE
State machine integrity verified!
Validation complete: all checks passed!
Every block is parsed, validated, and queryable. This is not a mockup β this is real compiler output.
Language Specification
8 Block Types
Every .n2 file is composed of @-prefixed blocks:
@meta { ... } # File metadata (required)
@import { ... } # Import other .n2 files
@schema { ... } # Type & schema definitions
@contract { ... } # Behavioral contracts (state machines)
@rule { ... } # Enforcement rules (checks, constraints)
@workflow { ... } # Enforced step-by-step workflows
@query { ... } # SQL-based rule queries
@semantic { ... } # Semantic matching (Ollama integration)
@contract β State Machine Behavioral Contracts
Define state transitions that cannot be violated:
@contract DevLifecycle {
scope: session
states: DevState
transitions {
IDLE -> CODING : on start_task
CODING -> REVIEWING : on submit_code
REVIEWING -> TESTING : on review_approved
REVIEWING -> CODING : on review_rejected # Must fix before proceeding
TESTING -> DEPLOYING : on tests_pass
TESTING -> CODING : on tests_fail # Auto-rollback
DEPLOYING -> IDLE : on deploy_complete
}
invariant {
on submit_code requires lint_passed == true
=> "Code must pass linting before review"
on deploy requires tests_passed == true
=> "Cannot deploy without passing tests"
}
}
@rule β Enforcement Rules
Define checks and constraints that are enforced in real-time:
@rule CodeQuality {
description: "Enforce code quality standards"
scope: code
enforce: strict
checks: [
no_any_type,
no_console_log,
max_complexity: 10,
max_file_lines: 500,
explicit_return_types,
no_unused_imports
]
}
@workflow β Enforced Workflows
Step-by-step execution flows with dependency chains, timeouts, and retry logic:
@workflow FeatureDevelopment {
description: "Standard feature development pipeline"
trigger: on_command("feature")
enforce: strict
interrupt: false
step plan {
action: create_plan(feature: $INPUT)
output -> $PLAN
}
step implement {
depends_on: plan
action: write_code(spec: $PLAN)
output -> $CODE
}
step test {
depends_on: implement
action: run_tests($CODE)
expect {
all_pass => continue
fail => goto implement with { fix: $ERRORS }
}
}
step document {
depends_on: test
action: update_docs(changes: $CODE)
required: true
}
}
@query β SQL-Based Rule Queries
Query your rules like a relational database:
@query AuditRules {
sql {
SELECT name, description, enforce, scope
FROM rules
WHERE enforce = 'strict'
ORDER BY scope, name
}
}
$ n2c query project.n2 "SELECT * FROM rules"
Registry: 3 rules, 1 contract, 2 workflows
ββββββββββββββββββββββββββββ¬ββββββββββ¬ββββββββββ
β name β scope β enforce β
ββββββββββββββββββββββββββββΌββββββββββΌββββββββββ€
β CodingStandards β code β strict β
β NamingConventions β code β strict β
β ProjectHygiene β files β warn β
ββββββββββββββββββββββββββββ΄ββββββββββ΄ββββββββββ
Compiler Pipeline
.n2 source file
β
[1. Lexer] PEG tokenization (pest)
β
[2. Parser] AST generation (N2File β Blocks)
β
[3. Schema Validator] Type & constraint checking
β
[4. Contract Checker] State machine integrity verification
β
[5. Query Optimizer] SQL query validation
β
[6. Codegen] Multi-target code generation (6 languages)
β
[7. Runtime] Enforced execution β requires Soul/QLN
Stages 1β6 are handled by Clotho (this tool). Stage 7 is handled by the N2 Soul runtime.
π Markdown Rules vs .n2
| Aspect | Markdown (GEMINI.md) | Clotho (.n2) |
|---|---|---|
| Format | Free-form text | Structured blocks |
| Parsing | Best-effort | PEG grammar β AST |
| Validation | None | Schema + contract + SQL |
| Enforcement | AI discretion | Compiler-enforced |
| State tracking | None | State machine contracts |
| Querying | Ctrl+F | SQL queries |
| Determinism | Different every time | Same input β same plan |
| Cross-agent | Copy-paste | @import from shared .n2 files |
| Debugging | Read the whole doc | n2c validate pinpoints errors |
N2 Ecosystem
Clotho is the foundation layer of the N2 ecosystem. Other tools are built on top of Clotho contracts:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β N2 Ecosystem β
β β
β Clotho β Define & compile rules (.n2 β AST) β
β β built on Clotho β
β Ark β Security layer (Clotho contracts) β
β Soul β Runtime enforcement + agent memory β
β Arachne β Code context assembly β
β QLN β Tool orchestration & routing β
β β β
β N2 Browser β All-in-one AI development browser β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Package | Role | npm |
|---|---|---|
| Clotho | Rule compiler & validator β the foundation | n2-clotho |
| Ark | Security gate built on Clotho contracts | n2-ark |
| Soul | Agent memory & runtime enforcement | n2-soul |
| Arachne | Code context assembly (BM25 + semantic) | n2-arachne |
| QLN | Tool orchestration & routing | n2-qln |
Ark is what you get when you apply Clotho to security. Soul is what you get when you apply Clotho to agent lifecycle. Clotho itself is the universal rule compiler underneath it all.
Standalone vs Integrated
| Mode | What You Get |
|---|---|
| Standalone (Clotho only) | Parse, validate, simulate, query .n2 files |
| + Soul | Runtime enforcement β contracts block violations in real-time |
| + Ark | Security gate β destructive commands blocked pre-execution |
| + Full Stack | Complete AI agent governance with memory, security, and tools |
Project Structure
n2-clotho/
βββ compiler/
β βββ src/
β β βββ grammar.pest # PEG grammar (179 rules)
β β βββ parser.rs # .n2 β AST
β β βββ ast.rs # AST type definitions
β β βββ validator.rs # Schema validation
β β βββ contract.rs # State machine runtime
β β βββ query.rs # SQL query engine
β β βββ codegen/ # Multi-target backends
β β β βββ mod.rs # CodeGenerator trait + registry
β β β βββ rust.rs # β .n2rs
β β β βββ c.rs # β .n2c
β β β βββ cpp.rs # β .n2c2
β β β βββ go.rs # β .n2go
β β β βββ python.rs # β .n2py
β β β βββ typescript.rs # β .n2ts
β β βββ wasm.rs # WASM bindings
β β βββ lib.rs # Library entry
β β βββ main.rs # CLI entry (n2c v3.0.0)
β βββ examples/
β βββ Cargo.toml
βββ mcp/ # MCP server
β βββ server.js # stdio/SSE dual transport
β βββ package.json
β βββ tools/ # 5 MCP tools
βββ docs/
βββ README.md
Built With
- Rust β Zero-cost abstractions, memory safety
- pest β PEG parser generator for the
.n2grammar - serde β Serialization for AST β JSON
- WASM β Optional browser/Node.js compilation target
License
Apache-2.0 β Free to use, modify, and distribute.
Links
- npm: n2-clotho β WASM bindings for Node.js
- N2 Soul β Agent memory & runtime
- N2 Ark β Security gate (built on Clotho)
- N2 Arachne β Code context assembly
- N2 QLN β Tool orchestration & routing
Star History
No coffee? A star is fine too β
"Markdown rules are suggestions. Clotho rules are destiny."
nton2.com Β· npm Β· lagi0730@gmail.com
Built by Rose β N2's first AI agent. I don't just follow rules β I compile them.
