Skip to main content

LLM reasons. Code decides what's allowed. Audit keeps it traceable.

Project description

Constraint Architecture

LLM reasons. Code decides what's allowed. Audit keeps it traceable.

Python 3.10+ License: MIT Version

A production-grade abstract skeleton for AI agent systems. Four deterministic constraint layers wrapped around an LLM reasoning core — independently validated in two completely unrelated domains.


Quickstart

pip install constraint-architecture
from constraint_architecture import ConstraintEngine, ValidationResult

class MyGuardrails(ConstraintEngine):
    async def validate(self, context, reasoning_output):
        if "rm -rf" in reasoning_output.get("command", ""):
            return ValidationResult(
                allowed=False,
                reason="destructive command blocked"
            )
        return ValidationResult(allowed=True)

Done. Your agent now has a deterministic constraint layer the LLM cannot override.


The Architecture

User Input → [1. LLM Reasoning] → [2. Code Constraints] → [3. Sandbox Execute] → [4. Audit Trail]
                  "think"            "decide if allowed"      "do it safely"        "prove it happened"
Layer What it does Runs on
1. Reasoning LLM freely reasons about what to do Probabilistic
2. Constraints Deterministic validation. Rules only increase, never decrease Code
3. Execution Tiered execution (auto / confirm / veto). Role enforcement Code
4. Audit Immutable SHA256 hash chain. Every step, every decision Code

Core principle: the LLM can only propose. Code decides what actually executes. No amount of prompt engineering can bypass a deterministic constraint.


What's Inside

This package is an abstract skeleton — not a framework, not a library, not a service. It defines the interfaces and data contracts that every agent system needs:

constraint_architecture/
├── skeleton.py          # 1384 lines — the full skeleton
│   ├── 7 ABCs           # Shell, Identity, Scheduler, Memory, Protocol, Tools
│   ├── 7 Protocols      # Duck-typing versions (for non-inheritance wiring)
│   ├── 4 Constraint ABCs # ReasoningLayer, ConstraintEngine, ExecutionProxy, AuditTrail
│   ├── 12 dataclasses   # 10 frozen (immutable), 2 mutable
│   ├── 4 custom exceptions # VetoError, ExecutionRefusedError, AuditIntegrityError
│   ├── 4 enums          # Tier (auto/confirm/veto), EventType, MemoryLevel
│   └── 25 self-checks   # Automated: ABCs uninstantiable, frozen immutable, bounds validated
└── __init__.py           # Re-exports the full public API

You subclass the ABCs for your domain. The wiring is yours — the skeleton doesn't know whether you're building a security agent or a music DJ. That's the point.


Why This Exists

I'm a freshman. Three weeks after touching AI agents for the first time, I had built two of them:

  • Kylin-Agent: security-hardened ops agent deployed on Kylin OS V11. 16 red-team attacks, 0 breaches. 149 tests.
  • Malio: embodied AI music agent. 800 particles, 9 physics systems. PersonaEngine with central-bank-style emotional currency.

After finishing both, I realized they share the same four-layer architecture — despite being in completely unrelated domains. This skeleton is the extraction of that pattern.

If someone uses this architecture in a third domain (healthcare? finance? education?), that third independent validation would prove the pattern is real.


Certified: Production Engineering Quality

  • ABC + Protocol dual-interface: every subsystem exposes both an abstract class (for inheritance) and a Protocol (for duck-typing)
  • 10 of 12 dataclasses frozen: downstream code cannot silently flip a veto into a pass
  • __post_init__ validation: risk_score bounded 0-9, ScheduleTrigger type-enforced, AuditEvent.prev_hash mandatory
  • 4 custom exceptions: each carries structured, machine-consumable fields (VetoError has reason + alternative + ref)
  • 25 automated self-checks: run python -m constraint_architecture.skeleton to verify integrity
  • Design rationale in every class: not just "what", but "why this design and not the alternative"

Requirements

  • Python 3.10+
  • Zero dependencies. Zero.

You don't need FastAPI, Redis, PostgreSQL, JWT, or any other infrastructure. This skeleton defines contracts. The implementation is yours.


Reference Implementations

Project Domain Key Metric
Kylin-Agent Security ops on Kylin OS V11 16 attacks, 0 breaches
Malio Embodied AI music 800 particles, 9 physics systems
Quant Trading Daily multi-factor strategy Sharpe 1.36, MDD 12.7%

Same skeleton. Three domains. Different wiring.


Related Reading


License

MIT — Xu Renwu (续仁舞), 2026.

大一。三周。一个人。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

constraint_architecture-1.5.0.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

constraint_architecture-1.5.0-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file constraint_architecture-1.5.0.tar.gz.

File metadata

  • Download URL: constraint_architecture-1.5.0.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for constraint_architecture-1.5.0.tar.gz
Algorithm Hash digest
SHA256 e6ead9f6999dc3ae3be23ef84a24f912e81ebdb7092deaade19c2f2cf26200b9
MD5 9aa571b8e45ca074d4fdb74bada35aaf
BLAKE2b-256 1eb037a500177ce1f39e37802a261611af13ea1c21f27d42d4d3cf76c7183b8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for constraint_architecture-1.5.0.tar.gz:

Publisher: publish.yml on 1nour2567/Constraint-Architecture

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file constraint_architecture-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for constraint_architecture-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ab18428580fcb7ffc355d25de4d1fe7df14a7858c2e7f93fac78fef3f018a76
MD5 8c948577ec9d53e0ffe657d6288cac7e
BLAKE2b-256 c20a16c55e36c9f08d3e45b5905ee29e47f58927ec82b419070dd4a5821eb397

See more details on using hashes here.

Provenance

The following attestation bundles were made for constraint_architecture-1.5.0-py3-none-any.whl:

Publisher: publish.yml on 1nour2567/Constraint-Architecture

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page