Skip to main content

Reliable, Rust-Powered Agentic AI Framework with Transactional Memory.

Project description

Theus Framework (v2.2.6)

PyPI version License: MIT Python 3.12+

"A Process-Oriented Operating System for AI Agents, powered by a Rust Microkernel."


🧭 Where do I start?

Theus is vast. Use our Interactive Documentation Map to find your path:


🚀 Key Features (v2.2.6)

"Data is the Asset. Code is the Liability. Theus protects the Asset."

Theus is a next-generation architectural framework that treats your application not as a collection of objects, but as a deterministic workflow of processes. It introduces the Process-Oriented Programming (POP) paradigm to solve the chaos of state management in complex systems like AI Agents, Core Banking, and Industrial Automation.


🌪️ The Problem

In modern software (OOP, EDA, Microservices), the biggest source of bugs is State Management:

  • Implicit Mutations: Who changed user.balance? Was it the PaymentService or the RefundHandler?
  • Race Conditions: Transient events corrupting persistent data.
  • Zombie State: Old references pointing to stale data.
  • Audit Gaps: We log what happened, but we can't mathematically prove why it was allowed.

🛡️ The Theus Solution

Theus acts as a micro-kernel for your logic, enforcing strict architectural invariants at runtime:

1. The 3-Axis Context Model

State is no longer a "bag of variables". It is a 3D space defined by:

  • Layer: Global (Config), Domain (Session), Local (Process).
  • Semantic: Input (Read-only), Output (Write-only), SideEffect (env), Error.
  • Zone:
    • DATA: Persistent Assets (Replayable).
    • SIGNAL: Transient Events (Reset on Read).
    • META: Observability (Logs/Traces).
    • HEAVY: High-Perf Tensors/Blobs (Zero-Copy, Non-Transactional).
                                     [Y] SEMANTIC
                             (Input, Output, SideEffect, Error)
                                      ^
                                      |
                                      |
                                      |                +------+------+
                                      |               /|             /|
                                      +--------------+ |  CONTEXT   + |----------> [Z] ZONE
                                     /               | |  OBJECT    | |      (Data, Signal, Meta, Heavy)
                                    /                | +------------+ |
                                   /                 |/             |/
                                  /                  +------+------+
                                 v
                            [X] LAYER
                     (Global, Domain, Local)

2. Zero-Trust Memory

  • Default Deny: Processes cannot access ANY data unless explicitly declared in a @process Contract.
  • Immutability: Inputs are physically frozen (FrozenList, FrozenDict).
  • Isolation: Signals cannot be used as Inputs for Business Logic (Architectural Boundary enforcement).

3. Industrial-Grade Audit

  • Active Defense: Rules (min, max, regex) are enforced at Input/Output Gates.
  • Severity Levels:
    • S (Safety): Emergency Stop.
    • A (Abort): Hard Stop Workflow.
    • B (Block): Rollback Transaction.
    • C (Campaign): Warning.
  • Resilience: Configurable tolerance thresholds (e.g., "Allow 2 glitches, block on 3rd").

📦 Installation

Theus requires Python 3.12+ to leverage advanced typing and dataclasses.

pip install theus

⚡ Quick Start: Building a Bank

This example demonstrates Contracts, Zoning, and Transaction safety.

1. Define the Context (The Asset)

from dataclasses import dataclass, field
from theus.context import BaseSystemContext, BaseDomainContext, BaseGlobalContext

@dataclass
class BankDomain(BaseDomainContext):
    # DATA ZONE: Persistent Assets
    accounts: dict = field(default_factory=dict) # {user_id: balance}
    total_reserves: int = 1_000_000
    
    # SIGNAL ZONE: Control Flow
    sig_fraud_detected: bool = False

@dataclass
class BankSystem(BaseSystemContext):
    domain_ctx: BankDomain = field(default_factory=BankDomain)
    global_ctx: BaseGlobalContext = field(default_factory=BaseGlobalContext)

2. Define the Process (The Logic)

from theus.contracts import process

@process(
    # STRICT CONTRACT
    inputs=['domain_ctx.accounts'],
    outputs=['domain_ctx.accounts', 'domain_ctx.total_reserves', 'domain_ctx.sig_fraud_detected'],
    errors=['ValueError']
)
def transfer(ctx, from_user: str, to_user: str, amount: int):
    # 1. Input Validation
    if amount <= 0:
        raise ValueError("Amount must be positive")
    
    # 2. Business Logic (Operating on Shadow Copies)
    sender_bal = ctx.domain_ctx.accounts.get(from_user, 0)
    
    if sender_bal < amount:
        # Trigger Signal
        ctx.domain_ctx.sig_fraud_detected = True
        return "Failed: Insufficient Funds"

    # 3. Mutation (Optimistic Write)
    ctx.domain_ctx.accounts[from_user] -= amount
    ctx.domain_ctx.accounts[to_user] = ctx.domain_ctx.accounts.get(to_user, 0) + amount
    
    return "Success"

3. Run with Safety (The Engine)

from theus.engine import TheusEngine

# Setup Data
sys_ctx = BankSystem()
sys_ctx.domain_ctx.accounts = {"Alice": 1000, "Bob": 0}

# Initialize Engine
engine = TheusEngine(sys_ctx, strict_mode=True)

# 🚀 PRO TIP: Auto-Discovery
# Instead of registering manually, you can scan an entire directory:
# engine.scan_and_register("src/processes")

engine.register_process("transfer", transfer)

# Execute
result = engine.run_process("transfer", from_user="Alice", to_user="Bob", amount=500)

print(f"Result: {result}")
print(f"Alice: {sys_ctx.domain_ctx.accounts['Alice']}") # 500

🛠️ CLI Tools

Theus provides a powerful CLI suite to accelerate development and maintain architectural integrity.

  • py -m theus.cli init <project_name>: Scaffolds a new project with the standard V2 structure (src/, specs/, workflows/).
  • py -m theus.cli audit gen-spec: Scans your @process functions and automatically populates specs/audit_recipe.yaml with rule skeletons.
  • py -m theus.cli audit inspect <process_name>: Inspects the effective audit rules, side effects, and error contracts for a specific process.
  • py -m theus.cli schema gen: Infers and generates specs/context_schema.yaml from your Python Dataclass definitions.
  • py -m theus.cli check: Runs the POP Linter to enforce architectural purity.
    • POP-E01: No print() (Use ctx.log).
    • POP-E02: No open() (Use Outbox).
    • POP-E03: No requests (No Side Effects).
    • POP-E04: No global (Strict Context).

🧠 Advanced Architecture

The Transaction Engine

Theus uses a Hybrid Transaction Model:

  • Scalars: Updated in-place with an Undo Log (for speed).
  • Collections: Updated via Shadow Copy (for safety). If a process crashes or is blocked by Audit, Theus rolls back the entire state instantly.

The Heavy Zone (Optimization)

For AI workloads (Images, Tensors) > 1MB, use heavy_ variables.

  • Behavior: Writes bypass the Transaction Log (Zero-Copy).
  • Trade-off: Changes to Heavy data are NOT reverted on Rollback.

🚀 High Performance Training (New in v2.2)

For Pure Training Loops (Simulations/Games) where Transaction safety is overkill:

engine = TheusEngine(sys_ctx, strict_mode=False)
  • Effect: Completely disables Rust Transaction Layer (Zero Overhead).
  • Performance: Native Python execution speed.
  • Trade-off: No Rollback protection.

The Audit Recipe (audit.yaml)

Decouple your business rules from your code.

process_recipes:
  transfer:
    inputs:
      - field: "amount"
        max: 10000        # Max transfer limit
        level: "B"        # Block transaction
    outputs:
      - field: "domain.total_reserves"
        min: 0            # Reserves must never be negative
        level: "S"        # Safety Interlock (Stop System)

The Orchestrator (FSM)

Manage complex flows using workflow.yaml:

states:
  IDLE:
    events:
      CMD_TX: "PROCESSING"
  PROCESSING:
    entry: "transfer"
    events:
      EVT_SUCCESS: "NOTIFY"
      EVT_FAIL: "IDLE"

📚 Documentation


⚖️ License

Maintained by: Hoàng Đỗ Huy

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

theus-3.0.0.tar.gz (327.7 kB view details)

Uploaded Source

Built Distributions

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

theus-3.0.0-cp312-abi3-win_amd64.whl (571.8 kB view details)

Uploaded CPython 3.12+Windows x86-64

theus-3.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (794.9 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

theus-3.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (791.7 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

theus-3.0.0-cp312-abi3-macosx_11_0_arm64.whl (714.6 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

theus-3.0.0-cp312-abi3-macosx_10_12_x86_64.whl (728.7 kB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

Details for the file theus-3.0.0.tar.gz.

File metadata

  • Download URL: theus-3.0.0.tar.gz
  • Upload date:
  • Size: 327.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for theus-3.0.0.tar.gz
Algorithm Hash digest
SHA256 ba9d90d088aa4709315aa483e9191f8ce233be5a5d89ef18b689709c5073212e
MD5 f0eefc2584a00f50f9678704f12d6d5f
BLAKE2b-256 d1dfd7416b281f88e32a71f49c292628e472bca30868fa7c5a56e894664ba2ca

See more details on using hashes here.

File details

Details for the file theus-3.0.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: theus-3.0.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 571.8 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for theus-3.0.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6dd70c91c4def3cece98d729566d5a0933e2cacae8e5d446c7e906c1083a91be
MD5 91f6db0bde5d56a7b5b71b4261957431
BLAKE2b-256 284079ee6e4253723095372cbab124cb8736ba02a7dc567012bee0f07723d796

See more details on using hashes here.

File details

Details for the file theus-3.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for theus-3.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e6b0bdda6dabbaa4181aa03f699c83706180ebd811486e3d621dd96b990669d9
MD5 0c10f20263e188787cafbfbe60117200
BLAKE2b-256 dacb2478e665a3c1c06c53eb8e0132928c54ebf30a71365494a1c622293f2d6a

See more details on using hashes here.

File details

Details for the file theus-3.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for theus-3.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5e0c126cb34b2f90eeab160f74d31b00655a2e58f63ca9816b7c3531a7edf8a7
MD5 499c9b84343c51daa94fd39be42f8cbc
BLAKE2b-256 b75fc489fc9720275274ce0f0a95218204fa2a8371097c76c7e93c0740858e5b

See more details on using hashes here.

File details

Details for the file theus-3.0.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for theus-3.0.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 711db2632c569062daeefac62cf427bc1544e850d232fbe9b987d1c086966dec
MD5 82dc540bfe740e422a8e8cfc993c5f7b
BLAKE2b-256 757bca722b39efe0ec311cb0447d3dfc375e514e3ced5f603490a0aa7d20de26

See more details on using hashes here.

File details

Details for the file theus-3.0.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for theus-3.0.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b5856e506eff6b9e9d68aafc9cb767616f06f5f1770bfdf1f1aa7266492b995e
MD5 c13c626beaf330141caf4f4711184950
BLAKE2b-256 4c3791697a4dfa4c9d4926781a1f58b5b3202d6e5f5cbf90ec93300e5b6cd8fa

See more details on using hashes here.

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