README
Semantic Engine: Deterministic NL → IR → Boolean → Latin → SUBLEQ Pipeline
License: MPL-2.0
========================================================================
SOVEREIGN LEVIATHAN NODE LICENSE
License-ID: SL-AGPL3-001 | Covenant-Version: 1.0
Copyright (C) 2026 SnapKittyWest. Ahmad Ali Parr,
Bel Esprit D'Accord Irrevocable Trust.
This work is licensed under dual license terms:
MPL-2.0 OR GPL-3.0-or-later
With additional Sovereign Leviathan Node License terms (AGPL-3.0 base).
Source files are individually licensed under MPL-2.0 (file-level copyleft).
Commercial repository — no MIT permitted.
This file is a covered work under the GNU Affero General Public License,
version 3, together with the Sovereign Leviathan additional terms.
"Hark, though this node be but a spark,
Its covenant endureth through the dark.
Ignorantia juris non excusat."
Table of Contents
Executive Overview
Semantic Engine is a 20-phase, deterministic transformation pipeline that converts natural language input into formally verified SUBLEQ machine code, passing through typed IR, Boolean normalization, Latin canonicalization, and templated artifact emission. Every transformation is gated by an explicit finite state machine. Every temporal mutation is tracked. Every output is verified before acceptance.
What This Is
A hand-rolled, zero-dependency semantic compiler implementing:
- 20-State Finite State Machine: Every phase transition is explicit and typed. Invalid transitions fail closed.
- Typed Semantic IR: Operations, operands, constraints, geometry, and temporal state — all content-addressed with SHA-256.
- Boolean CNF Normalization: Full De Morgan, distribution, constant folding, and idempotency guarantees.
- Binary Token Codec:
decode(encode(x)) == x for all canonical Boolean structures.
- Latin Controlled Vocabulary: 28 canonical lexemes, no uncontrolled synonyms, deterministic token→lexeme mapping.
- SUBLEQ Target Machine: One-instruction computer (
mem[B] -= mem[A]; if mem[B] <= 0: goto C), 65KB memory, bounded execution.
- 10-Point Verification Engine: Type correctness, geometry, temporal state, Boolean stability, token round-trip, Latin vocabulary, Rosetta completeness, SUBLEQ halting, artifact provenance, template binding.
- Agent Harness: Agents propose artifacts; the system verifies before accepting. No agent output bypasses the FSM.
- Dual Template Backend: Mustache and Go template renderers, both hand-rolled with zero external dependencies.
- 113 Tests: All round-trip invariants, all edge cases, all phases exercised.
Why This Matters
Semantic transformation is the substrate of every compiler, every code generator, every agent framework, and every formal verification tool. The typical approach is to treat NL→code as a black box (LLM prompt, template engine, or ad-hoc regex). This project makes every stage explicit, every invariant checked, and every intermediate representation inspectable:
- Auditability: Every transformation records provenance. Every hash chains forward. You can trace any output token back to the original input word.
- Determinism: Same input → same IR → same Boolean form → same Latin → same SUBLEQ → same artifact. No randomness, no model variance, no hidden state.
- Composability: Each phase is a pure function gated by the FSM. Phases can be tested, replaced, or extended independently.
- Formal Target: SUBLEQ is Turing-complete with one instruction. If it compiles to SUBLEQ and the VM halts, the program is well-defined.
The pipeline processes natural language at the rate of ~300 inputs/second on a single core (113 tests in 0.38s), with zero external dependencies beyond Python 3.11.
Semantic IR: The Central Data Structure
The entire pipeline orbits a single typed intermediate representation. Every transformation reads from or writes to the SemanticIR:
@dataclass
class SemanticIR:
ir_id: str # UUID, unique per pipeline run
intent: IntentClass # BUILD, TRANSFORM, VALIDATE, ...
root: SemanticNode # root of the semantic tree
nodes: list[SemanticNode] # all nodes in the IR
temporal_lock: TemporalLock # audit log of τ transitions
provenance: Provenance # source_input_id, stage, transform_hash
verification_state: VerificationState # UNVERIFIED → VERIFIED → REJECTED
output_mode: str # natural_language, code, artifact
metadata: dict[str, Any] # extensible
Each SemanticNode carries:
- Operation: name, arity, commutativity, associativity
- Operands: typed, named, with optional geometry
- Constraints: predicate strings that become Boolean variables
- Geometry: row/col/shape for dimensional checking
- Temporal state: current τ value
- Provenance: SHA-256 chain from input to current stage
- Children: sub-nodes for recursive structures
Every node, operation, constraint, and the IR itself produce deterministic SHA-256 hashes from their content. Two pipeline runs with the same input produce byte-identical hashes at every stage.
Provenance Chain
@dataclass(frozen=True)
class Provenance:
source_input_id: str # ties back to RawInput.input_id
stage: str # which pipeline phase
timestamp: float # wall clock
parent_id: Optional[str] # previous provenance (for chaining)
transform_hash: str # SHA-256 of the transformation applied
The chain_hash method computes SHA-256(transform_hash + ":" + data), extending the hash chain at each stage. This means any modification to any intermediate — even a single bit flip — produces a completely different hash at the artifact stage.
Project Vision
We believe semantic transformation requires:
- Explicit State Machines: Not implicit control flow. Every legal transition is enumerated. Every illegal transition fails closed.
- Temporal Accountability: Every mutation advances τ. Read-only passes preserve τ. Locked states cannot be modified.
- Dimensional Safety: Matrix operations carry geometry. Contraction mismatches are caught before SUBLEQ assembly.
- Controlled Vocabulary: No synonym drift. Latin lexemes map 1:1 to semantic IDs. The Rosetta table is the single source of truth for machine translation.
- Verified Emission: No artifact leaves the pipeline without passing all 10 verification checks.
This project demonstrates that a complete NL→machine-code pipeline can be built from first principles: no frameworks, no LLM dependencies (BERT is optional), no external template engines, no runtime magic.
Core Architecture
semantic-engine/
├── cli/main.py # Phase 18: 11 CLI subcommands
├── src/semantic_engine/
│ ├── __init__.py # Public API: SemanticPipeline, PipelineResult
│ ├── types.py # Phase 1: Enums, error hierarchy, input types
│ ├── ir.py # Phase 2: Typed IR — nodes, geometry, temporal, artifacts
│ ├── fsm.py # Phase 3: 20-state FSM, typed transition table
│ ├── temporal.py # Phase 4: τ-advancing transforms, violation detection
│ ├── geometry.py # Phase 5: Matrix dimension checking, GeometryError
│ ├── matrix.py # Phase 6: SemanticMatrix builder, compose/transform
│ ├── boolean_engine.py # Phase 7: CNF normalization, De Morgan, simplification
│ ├── token_engine.py # Phase 8: Binary encode/decode, round-trip invariant
│ ├── latin.py # Phase 9: Controlled vocabulary, token→lexeme map
│ ├── lexer.py # Phase 10: Regex Latin lexer, fail-closed on unknowns
│ ├── rosetta.py # Phase 11: Rosetta table, semantic_id → SUBLEQ op
│ ├── subleq.py # Phases 12–13: SUBLEQ VM, assembler, disassembler
│ ├── verifier.py # Phase 14: 10 independent verification checks
│ ├── artifacts.py # Phases 15–16: Mustache + Go template renderers
│ ├── agent_harness.py # Phase 17: Agent proposal/verify/accept loop
│ ├── nlp.py # NLP: tokenization, intent, entity, BERT interface
│ └── pipeline.py # Full pipeline orchestrator
├── lexicon/latin_vocabulary.json # 28 canonical Latin lexemes
├── mappings/rosetta_table.json # 24 semantic_id → SUBLEQ operation mappings
├── templates/
│ ├── prompt.mustache # Natural language artifact template
│ ├── document.mustache # Document artifact template
│ └── backend.go.tmpl # Go backend artifact template
├── examples/
│ ├── matrix_multiply.txt # "multiply two matrices A and B..."
│ ├── parse_binary_format.txt # "build a parser for this binary format..."
│ └── validate_packets.txt # "create a backend service that validates packets"
├── tests/ # 113 tests across 7 test modules
├── pyproject.toml # Python 3.11+, zero required dependencies
└── LICENSE # MPL-2.0
4,143 lines of source (18 modules + CLI + 7 test files). Zero stubs. Zero pass placeholders.
Pipeline Stages
| Phase |
Module |
Input |
Output |
Invariant |
| 1 |
types.py |
— |
Enums, error types, input structs |
Type safety via frozen dataclasses |
| 2 |
ir.py |
— |
SemanticIR, SemanticNode, Artifact |
Content-addressed (SHA-256) |
| 3 |
fsm.py |
(state, input_class) |
FSMTransition |
Fail-closed on missing transitions |
| 4 |
temporal.py |
TemporalState(τ) |
TemporalState(τ+1) |
τ monotonic, locked states reject |
| 5 |
geometry.py |
Geometry(m×n) |
DimCheck |
A.col == B.row for matmul |
| 6 |
matrix.py |
SemanticNode |
SemanticMatrix |
Composition: a.cols == b.rows |
| 7 |
boolean_engine.py |
Constraints |
BooleanNormalForm (CNF) |
normalize(normalize(x)) == normalize(x) |
| 8 |
token_engine.py |
Boolean IR |
BooleanTokenStream / bytes |
decode(encode(x)) == x |
| 9 |
latin.py |
Token stream |
LatinCanonicalForm |
Every token maps to exactly one Latin lexeme |
| 10 |
lexer.py |
Latin text |
LexResult |
Unknown tokens fail closed |
| 11 |
rosetta.py |
Latin form |
RosettaMapping |
All semantic_ids must map (no unmapped) |
| 12–13 |
subleq.py |
Rosetta mapping |
SubleqProgram / VM trace |
Program halts within 10K steps |
| 14 |
verifier.py |
All intermediates |
VerificationReport |
10 independent checks, UNVERIFIED→VERIFIED→REJECTED |
| 15–16 |
artifacts.py |
SemanticIR + template |
Artifact |
Mustache + Go renderers, provenance chain intact |
| 17 |
agent_harness.py |
AgentTask |
Verified Artifact |
Agent proposals verified before acceptance |
| 18 |
cli/main.py |
File path |
Formatted output |
11 subcommands + trace flags |
| 19–20 |
tests/ |
— |
113 assertions |
All round-trip invariants exercised |
Finite State Machine
The FSM (fsm.py, 128 lines) defines exactly 21 transitions across 20 states plus an ERROR sink. Every transition is a typed record:
@dataclass(frozen=True)
class FSMTransition:
source_state: FSMState # where we are
input_class: str # what arrived
operation: str # what to do
constraints: tuple[str, ...] # what must hold
destination_state: FSMState # where we go
invariant: str # human-readable guarantee
Complete Transition Table
| Source State |
Input Class |
Operation |
Destination |
Constraint |
| INPUT |
raw_input |
normalize |
NORMALIZE |
text_not_empty |
| NORMALIZE |
normalized_input |
lex |
LEXICAL_ANALYSIS |
tokens_non_empty |
| LEXICAL_ANALYSIS |
token_stream |
detect_intent |
INTENT_DETECTION |
tokens_recognized |
| INTENT_DETECTION |
intent_class |
bind_entities |
ENTITY_BINDING |
intent_not_unknown |
| ENTITY_BINDING |
entities |
bind_semantics |
SEMANTIC_BINDING |
entities_typed |