Metadata-Version: 2.5
Name: blockstate
Version: 0.0.1a3
Summary: Deterministic statecharts, compile-time dominator proofs, and zero-trust tool gating for enterprise LLMs.
Project-URL: Homepage, https://github.com/s0ck3t/blockstate
Project-URL: Repository, https://github.com/s0ck3t/blockstate
Project-URL: Issues, https://github.com/s0ck3t/blockstate/issues
Project-URL: Documentation, https://github.com/s0ck3t/blockstate#readme
Project-URL: PatentNotice, https://github.com/s0ck3t/blockstate/blob/main/PATENTS.md
Author: Blockstate Contributors
License: BLOCKSTATE SOURCE-AVAILABLE & COMMERCIAL DUAL LICENCE
        Version 1.0 (October 2026)
        
        Copyright (c) 2026 James Tyas and Blockstate Contributors. All Rights Reserved.
        PATENT NOTICE: This software and its underlying architecture are the subject of a
        pending United Kingdom Patent Application ("Process execution control system",
        filed 28 October 2026). See PATENTS.md for statutory patent notice details under
        Section 62(1) of the United Kingdom Patents Act 1977.
        
        ================================================================================
        OVERVIEW OF DUAL-LICENSING MODEL
        ================================================================================
        
        Blockstate ("the Software") is distributed under a dual-licensing model designed
        to support open inspection, developer prototyping, academic research, and
        community contribution, while requiring a commercial licence for production
        commercial deployment:
        
        1. COMMUNITY, DEVELOPMENT & EVALUATION TIER (FREE):
           You may view, download, modify, execute, and test the Software free of charge
           solely for Non-Commercial Use, Academic Research, Local Development,
           Automated CI/CD Verification (`blockstate verify`), and Time-Bounded Internal
           Evaluation/Prototyping (see Section 1).
        
        2. COMMERCIAL PRODUCTION & ENTERPRISE TIER (PAID COMMERCIAL LICENCE):
           Any deployment of the Software in a live commercial production environment,
           regulated customer-facing or internal enterprise workflow, or commercial
           hosted/managed service requires a separate written Commercial Enterprise and
           Patent Licence Agreement (see Section 2).
        
        ================================================================================
        TERMS AND CONDITIONS
        ================================================================================
        
        1. GRANT OF COMMUNITY, DEVELOPMENT AND EVALUATION LICENCE
        Subject to the terms and conditions of this Licence, the Licensor hereby grants
        You a non-exclusive, non-transferable, royalty-free, revocable copyright licence
        to copy, modify, and execute the Software solely for the following Permitted
        Purposes:
          (a) Personal, educational, or academic research use;
          (b) Open-source software integration testing and automated compile-time
              verification (`blockstate verify`) in continuous integration pipelines;
          (c) Local developer prototyping, benchmarking, security auditing, and
              non-production staging evaluation for a period of up to ninety (90) days
              for the purpose of evaluating whether to obtain a Commercial Licence; or
          (d) Community contributions (pull requests, bug fixes, and adapters) submitted
              back to the official upstream repository.
        
        2. RESTRICTIONS ON COMMERCIAL PRODUCTION AND MANAGED SERVICES
        Absent a separate written Commercial Enterprise and Patent Licence Agreement
        executed with the Licensor, You may NOT:
          (a) Deploy, operate, or execute the Software in a commercial production
              environment processing live customer, financial, insurance, healthcare,
              telecommunications, or enterprise operational transactions;
          (b) Offer, host, or provide the Software (or any derivative work thereof) to
              third parties as a managed service, Software-as-a-Service (SaaS),
              Platform-as-a-Service (PaaS), API gateway, or compliance-as-a-service
              offering;
          (c) Remove, alter, or obscure any copyright notices, licence headers, or the
              statutory patent pending notices contained in `LICENSE` or `PATENTS.md`.
        
        3. EXPLICIT RESERVATION OF PATENT RIGHTS (NO IMPLIED PATENT GRANT)
        Unlike the Apache License 2.0, this Licence is a conditional copyright licence
        only and DOES NOT GRANT any express or implied licence under any patent or
        patent application owned or controlled by the Licensor (including, without
        limitation, United Kingdom Patent Application "Process execution control system"
        and any international Paris Convention, PCT, EPO, or USPTO counterparts).
        
        A narrow, revocable covenant not to sue under the Licensor's applicable patent
        claims applies strictly and exclusively while You are operating within the scope
        of the Permitted Purposes in Section 1. Any commercial production use outside
        Section 1 without a valid Commercial Enterprise and Patent Licence Agreement is
        strictly unauthorised under both copyright and patent law.
        
        4. DEFENSIVE RETALIATION TERMINATION
        If You institute patent litigation or administrative invalidity proceedings
        against the Licensor or any contributor (including a cross-claim or counterclaim
        in a lawsuit) alleging that the Software constitutes direct or contributory
        patent infringement, then all licences and covenants granted to You under this
        Licence shall terminate automatically as of the date such litigation or
        proceeding is filed.
        
        5. DISCLAIMER OF WARRANTY AND LIMITATION OF LIABILITY
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
        FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS,
        COPYRIGHT HOLDERS, OR PATENT APPLICANTS BE LIABLE FOR ANY CLAIM, DAMAGES, OR
        OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
        FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
License-File: LICENSE
Requires-Python: <3.15,>=3.12
Requires-Dist: cryptography<51.0,>=50.0.2
Requires-Dist: pydantic-settings<3.0,>=2.16.1
Requires-Dist: pydantic<3.0,>=2.14.1
Requires-Dist: rich<16.0,>=15.0.0
Requires-Dist: typer<1.0,>=0.27.3
Provides-Extra: all
Requires-Dist: aiobotocore>=3.0.0; extra == 'all'
Requires-Dist: aiosqlite<1.0,>=0.22.1; extra == 'all'
Requires-Dist: asyncpg<1.0,>=0.32.0; extra == 'all'
Requires-Dist: greenlet>=3.0.0; extra == 'all'
Requires-Dist: litellm<1.107.0,>=1.106.0; extra == 'all'
Requires-Dist: numpy>=1.26.0; extra == 'all'
Requires-Dist: onnxruntime<2.0,>=1.31.0; extra == 'all'
Requires-Dist: opentelemetry-api<2.0,>=1.45.1; extra == 'all'
Requires-Dist: opentelemetry-sdk<2.0,>=1.45.1; extra == 'all'
Requires-Dist: pydantic-ai-slim<3.0,>=2.61.0; extra == 'all'
Requires-Dist: sqlalchemy<3.0,>=2.1.6; extra == 'all'
Requires-Dist: tokenizers<1.0,>=0.23.0; extra == 'all'
Provides-Extra: dev
Requires-Dist: bandit>=1.8.0; extra == 'dev'
Requires-Dist: cyclonedx-bom>=5.0.0; extra == 'dev'
Requires-Dist: hypothesis<7.0,>=6.100.0; extra == 'dev'
Requires-Dist: import-linter<3.0,>=2.0; extra == 'dev'
Requires-Dist: mypy<3.0,>=2.4.0; extra == 'dev'
Requires-Dist: pip-audit>=2.8.0; extra == 'dev'
Requires-Dist: pip-licenses>=5.0.0; extra == 'dev'
Requires-Dist: pre-commit>=4.0.0; extra == 'dev'
Requires-Dist: pytest-asyncio<2.0,>=1.4.0; extra == 'dev'
Requires-Dist: pytest-cov<8.0,>=7.1.0; extra == 'dev'
Requires-Dist: pytest<10.0,>=9.1.1; extra == 'dev'
Requires-Dist: ruff<1.0,>=0.17.0; extra == 'dev'
Requires-Dist: types-pyyaml>=6.0.0; extra == 'dev'
Provides-Extra: litellm
Requires-Dist: litellm<1.107.0,>=1.106.0; extra == 'litellm'
Provides-Extra: local
Requires-Dist: aiosqlite<1.0,>=0.22.1; extra == 'local'
Requires-Dist: numpy>=1.26.0; extra == 'local'
Requires-Dist: onnxruntime<2.0,>=1.31.0; extra == 'local'
Requires-Dist: tokenizers<1.0,>=0.23.0; extra == 'local'
Provides-Extra: models
Requires-Dist: pydantic-ai-slim<3.0,>=2.61.0; extra == 'models'
Provides-Extra: onnx
Requires-Dist: numpy>=1.26.0; extra == 'onnx'
Requires-Dist: onnxruntime<2.0,>=1.31.0; extra == 'onnx'
Requires-Dist: tokenizers<1.0,>=0.23.0; extra == 'onnx'
Provides-Extra: otel
Requires-Dist: opentelemetry-api<2.0,>=1.45.1; extra == 'otel'
Requires-Dist: opentelemetry-sdk<2.0,>=1.45.1; extra == 'otel'
Provides-Extra: postgres
Requires-Dist: asyncpg<1.0,>=0.32.0; extra == 'postgres'
Requires-Dist: greenlet>=3.0.0; extra == 'postgres'
Requires-Dist: sqlalchemy<3.0,>=2.1.6; extra == 'postgres'
Provides-Extra: s3
Requires-Dist: aiobotocore>=3.0.0; extra == 's3'
Provides-Extra: sql
Requires-Dist: greenlet>=3.0.0; extra == 'sql'
Requires-Dist: sqlalchemy<3.0,>=2.1.6; extra == 'sql'
Provides-Extra: sqlite
Requires-Dist: aiosqlite<1.0,>=0.22.1; extra == 'sqlite'
Provides-Extra: system1
Requires-Dist: numpy>=1.26.0; extra == 'system1'
Requires-Dist: onnxruntime<2.0,>=1.31.0; extra == 'system1'
Requires-Dist: tokenizers<1.0,>=0.23.0; extra == 'system1'
Description-Content-Type: text/markdown

<div align="center">

# 🛤️ Blockstate (`blockstate`)

### **Deterministic Statecharts, Compile-Time Dominator Proofs & Zero-Trust Tool Gating for Enterprise LLMs**

*"FastAPI meets XState for Regulated AI: where the LLM is never the orchestrator."*

[![Python 3.12+](https://img.shields.io/badge/python-3.12%20%7C%203.13%20%7C%203.14-2563eb.svg)](https://www.python.org/)
[![Typed: mypy --strict & pyright](https://img.shields.io/badge/typing-PEP%20695%20%7C%20strict-0d9488.svg)](https://peps.python.org/pep-0695/)
[![Coverage: >=95% Core / >=90% Repo](https://img.shields.io/badge/coverage-%E2%89%A595%25%20core%20%7C%20%E2%89%A590%25%20branch-16a34a.svg)](#enterprise-testing-coverage--interface-contracts)
[![Architecture: Hexagonal import-linter](https://img.shields.io/badge/architecture-hexagonal%20ports%20%26%20adapters-7c3aed.svg)](docs/architecture/ARCHITECTURE-SPINE.md)
[![Security: Zero-Trust & Gitleaks](https://img.shields.io/badge/security-zero--trust%20%7C%20PIIVault-dc2626.svg)](SECURITY.md)
[![Patent: UK Patent Pending](https://img.shields.io/badge/patent-UK%20Patent%20Pending-b45309.svg)](PATENTS.md)
[![Licence: Source-Available / Commercial](https://img.shields.io/badge/licence-Eval%20Free%20%7C%20Commercial%20Prod-0284c7.svg)](LICENSE)

[**Quick Start**](#60-second-quick-start) · [**Scaffolder & Visual Studio**](#-project-scaffolder-blockstate-init--visual-inspector-blockstate-studio) · [**Documentation & Tutorials**](#-documentation--tutorials) · [**4 Domain Examples**](#-domain-examples-4-complete-walkthroughs) · [**CLI & Dominator Proofs**](#️-cli-reference-declarative-control-grammar--compile-time-verification) · [**Testing & Contracts**](#-enterprise-testing-coverage--interface-contracts) · [**Licensing & Patent Notice**](#️-licensing-uk-patent-pending--commercial-production)

</div>

---

## ⚡ Why Blockstate?

Derived from **railway absolute block signalling** (where physical track circuits prevent a train from entering an occupied or unverified block section) and **deterministic state machines**, **Blockstate** brings mathematical execution guarantees to conversational AI.

Most agent frameworks (early LangChain, AutoGen, CrewAI) rely on the **ReAct (Reason + Act)** loop: they hand an LLM a global bag of tools and let probabilistic token generation decide which tool to call, when to transition between workflow steps, and how to mutate state.

In **regulated enterprise domains** (retail banking, mortgage origination, insurance claims, healthcare triage, and telecommunications), giving an LLM autonomous control over execution flow and tool invocation introduces severe legal and operational liability under **EU AI Act Articles 12 & 14**, **UK FCA SYSC 13.7**, **HIPAA**, and **SOC 2 Type II**.

Blockstate also avoids the **"Enterprise Island Trap"** by operating as a **transactional compliance execution enclave** that high-level enterprise orchestrators (such as LangGraph, Microsoft Semantic Kernel, AutoGen, or AWS Bedrock) invoke via a first-class `as_tool()` adapter whenever a conversation enters a regulated boundary.

**Blockstate enforces six core guarantees inside regulated workflows:**

1. **100% Deterministic State Transitions via Declarative Control Grammar (DCG):** The pure Python statechart runtime owns every transition and guard condition through the static **Declarative Control Grammar (`@Stage`, `@Transition`, `@Guard`)**. The LLM is demoted to a bounded semantic worker used strictly for structured slot extraction (Pass 1) and natural language rendering (Pass 2).
2. **Zero-Trust Physical Tool Gating:** Unpermitted tools do not exist in the prompt schema or the Python `MappingProxyType` dispatch table for that turn. Prompt injection cannot invoke a tool that is physically absent from the active `@Stage` memory space (`BLK-SEC-001`).
3. **Compile-Time Mathematical Dominator Proofs (`blockstate verify`) & Rice's Theorem Boundaries:** Because unconstrained Python reflection (`getattr(self, dynamic_state)()`) is mathematically undecidable under **Rice's Theorem**, Blockstate enforces static DCG AST rules (`[Blockstate E014]`) and runs the **Lengauer-Tarjan dominator tree algorithm** in `< 50ms` during CI/CD to prove that mandatory compliance checkpoints (e.g. `verify_identity`) strictly dominate protected actions (e.g. `disburse_funds`) across every possible branch before code reaches production.
4. **Pushdown Automata for Conversational Digressions (`TaskStack`):** Users rarely fill out multi-turn forms linearly. When a customer asks *"Will this affect my no-claims bonus?"* halfway through an insurance claim, Blockstate pushes a read-only child `TaskFrame` onto the call stack, answers the FAQ, pops the frame, and resumes slot collection with parent data 100% intact, reducing graph edge complexity from $\mathcal{O}(N \times M)$ to $\mathcal{O}(N + M)$.
5. **Recursive $\Delta$-Hydration (`PartialSlotModel`):** Declare a standard Pydantic v2 model (including deeply nested child models) on `@Stage(collect=ClaimModel)`. Blockstate automatically relaxes missing fields across intermediate turns, validates supplied fields immediately, locks validated fields against accidental overwrite (`BLK-SEC-002`), and short-circuits invalid fields for targeted re-elicitation (`BLK-VAL-002`).
6. **Tokenised `PIIVault` & Hash-Chained `TurnLedger`:** Sensitive PII/PHI is replaced at ingress with HMAC-keyed session tokens (`<VAULT:POSTCODE_01>`) before touching any LLM or telemetry log, while every turn is recorded in a tamper-evident `RFC 8785 JCS` `SHA-256` hash chain.

---

## 🏗️ System Architecture

```mermaid
flowchart TD
    subgraph ClientBoundary["1. Client / Outer Orchestrator & Zero-Trust Ingress Boundary"]
        U["Customer Utterance / as_tool() Enclave Call + session_id"] --> LEASE["StorageBackend.acquire_lease()<br/>Fail-Fast 2s Timeout | 30s TTL | 10s Heartbeat"]
        LEASE --> PII["PIIVault.sanitise()<br/>HMAC Tokenisation (&lt;VAULT:TYPE_NN&gt;)"]
    end

    subgraph TwoPassLoop["2. Two-Pass Deterministic Turn Pipeline (DCG Enclave)"]
        PII --> P1["Pass 1: IntentClassifier (&lt;100ms)<br/>Local ONNX System-1 / ModernBERT / Fast SLM"]
        P1 --> ENGINE{"StatechartEngine (&le;5ms Core)<br/>DCG (@Stage, @Transition, @Guard) &amp; PEP 695"}
        ENGINE -->|"Hydrate &amp; Lock Leaf Paths"| DELTA["PartialSlotModel &Delta;-Hydration<br/>&Delta; = Required \ Hydrated"]
        ENGINE -->|"Off-Topic Allowed Intent"| STACK["TaskStack Pushdown Automata<br/>Push/Pop Isolated TaskFrame"]
        ENGINE -->|"Scope Active Stage Tools"| GATE["ToolGatekeeper<br/>Frozen MappingProxyType Dispatch Table"]
        GATE -.->|"Detokenise[T] Only"| TOOLS["Whitelisted Stage Tools ONLY"]
        ENGINE --> P2["Pass 2: ResponseRenderer<br/>Canned Template / ChoicePayload (&lt;15ms) or Scoped LLM"]
    end

    subgraph GovernanceAudit["3. StorageBackend SPI, Telemetry & Audit Provenance"]
        ENGINE -->|"Atomic CAS Commit (schema_version)"| STORE[("v1 StorageBackend SPI:<br/>In-Memory | SQLite (WAL) | PostgreSQL")]
        ENGINE -.->|"RFC 8785 JCS + SHA-256 Chain"| LEDGER["TurnLedger Ring Buffer<br/>(PostgreSQL / S3 WORM + .blockstate/wal Drain)"]
        ENGINE -.->|"Post-Vault Spans"| OTEL["OpenTelemetry GenAI 1.30 &amp;<br/>.blockstate/traces.jsonl"]
    end
```

### Competitive Capability Matrix

| Capability | LangGraph | NeMo Guardrails (Colang 2.0) | LlamaIndex Workflows | PydanticAI (`pydantic-graph`) | **Blockstate (`blockstate`)** |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **Primary Role** | High-level outer orchestrator | Proprietary Colang `.co` DSL | Event-driven async steps | Typed node return unions | **Transactional Compliance Execution Enclave** |
| **Physical Tool Containment** | ❌ Global `ToolNode` superset | ⚠️ DSL action rail checks | ❌ Imperative event handlers | ⚠️ Dynamic `prepare` callbacks | **✅ Ephemeral per-stage `MappingProxyType`** |
| **Static AST Graph Compiler** | ❌ Runtime graph inspection | ⚠️ Colang syntax check | ❌ Runtime reflection | ⚠️ `mypy` type checking only | **✅ Zero-execution DCG AST CFG compiler (`[Blockstate E014]`)** |
| **Mathematical Dominator Proofs** | ❌ None | ❌ None | ❌ None | ❌ None | **✅ Lengauer-Tarjan (`KYC dom Payout`)** |
| **Multi-Turn Digression Stack** | ❌ Manual subgraphs ($\mathcal{O}(N \times M)$) | ⚠️ Concurrent DSL flows | ❌ Manual event state | ❌ Manual node branching | **✅ LIFO `TaskStack` Pushdown Automata** |
| **Nested Multi-Turn Slot Filling** | ❌ Custom prompt & state boilerplate | ⚠️ Colang dialog scripts | ❌ Manual state dicts | ❌ Single-turn structured output | **✅ Recursive `PartialSlotModel` $\Delta$-Hydration** |
| **Turn Latency Architecture** | $1\text{ to }3\text{s}$ serialised LLM calls | $3\text{ to }6\text{s}$ multi-hop rails | Single-pass async LLM | Single-pass async LLM | **✅ Two-Pass (`<50ms` ONNX + `<15ms` Template/LLM)** |
| **Ingress PII Tokenisation** | ❌ Manual middleware | ⚠️ Presidio text masking | ❌ Manual hooks | ❌ Manual preprocessing | **✅ Built-in `PIIVault` (`<VAULT:ID>`) + `Detokenise[T]`** |
| **Tamper-Evident Audit Log** | ❌ State checkpoints only | ❌ Unstructured event logs | ❌ Telemetry traces only | ❌ Message history JSON | **✅ SHA-256 Hash-Chained `TurnLedger` + WAL** |

---

## 🚀 60-Second Quick Start

For a complete step-by-step walkthrough, see [**Getting Started (`docs/GETTING_STARTED.md`)**](docs/GETTING_STARTED.md).

### 1. Installation

Blockstate requires **Python 3.12+** (leveraging PEP 695 type parameters). The core package is lightweight (`<= 40MB` RAM, zero database or cloud SDK bloat) and includes `InMemoryStorageBackend`. Install optional v1 adapters via PEP 621 extras:

```bash
# Core runtime, DCG AST compiler, CLI & InMemoryStorageBackend
pip install blockstate
# or via uv:
uv add blockstate

# Local development & edge deployments (SQLite WAL StorageBackend + local ONNX System-1 classifier)
pip install "blockstate[local]"
# or:
uv add "blockstate[local]"

# Cloud production workloads (PostgreSQL async SQLAlchemy + asyncpg StorageBackend, PydanticAI models & OTel)
pip install "blockstate[postgres,models,otel]"
# or install all canonical v1 enterprise adapters (PostgreSQL, SQLite WAL, S3, LiteLLM, ONNX, OTel):
uv add "blockstate[all]"
```

> **Version 1 Storage Slicing (`StorageBackend` SPI):** Version 1 ships with three canonical backends implementing `src/blockstate/ports/storage.py` (`StorageBackend`):
> 1. **In-Memory (`InMemoryStorageBackend`):** For hermetic unit testing and CI/CD.
> 2. **Local SQLite with WAL mode (`SQLiteStorageBackend` with `PRAGMA journal_mode=WAL` via `aiosqlite`):** For zero-dependency local development and edge deployments.
> 3. **PostgreSQL via async SQLAlchemy (`PostgresStorageBackend` via `SQLAlchemy[asyncio]` + `asyncpg`):** For cloud production workloads.
>
> *Redis, Microsoft SQL Server, and AWS DynamoDB are formally scheduled for **v1.2+** via the pluggable async `StorageBackend` SPI.*

### 2. Configure Your Environment

Copy the provided enterprise `.env.example` template and generate a local vault encryption key:

```bash
cp .env.example .env
```

### 3. Build, Verify & Run Your First Governed Agent

Create `QuickStartRefundAgent` in `agent.py` using the **Declarative Control Grammar (DCG)** (`@Stage`, `@Transition`, `@Guard`, `ChoicePayload`):

```python
import asyncio
from pydantic import BaseModel, Field
from blockstate import Agent, Stage, Transition, Guard, ChoicePayload


class RefundRequest(BaseModel):
    order_id: str = Field(pattern=r"^ORD-\d{5}$", description="Order ID in format ORD-12345")
    reason: str = Field(min_length=5, description="Brief reason for requesting a refund")
    amount_gbp: float = Field(gt=0, le=5000, description="Refund amount in GBP")


class QuickStartRefundAgent(Agent):
    @Stage(name="verify_identity", initial=True, max_turns=3)
    @Guard(requires=("is_authenticated",))
    @Transition(targets=("collect_refund", "security_lockout"))
    async def verify_identity(self) -> Transition["collect_refund" | "security_lockout"]:
        if not self.session.get("is_authenticated", False):
            return Transition.to("security_lockout", reason="Unauthenticated session")
        return Transition.to("collect_refund")

    @Stage(name="collect_refund", collect=RefundRequest, max_turns=5)
    @Transition(targets=("instant_refund", "manual_approval"))
    async def collect_refund(
        self, slots: RefundRequest
    ) -> Transition["instant_refund" | "manual_approval"]:
        if slots.amount_gbp <= 50.0:
            return Transition.to("instant_refund")
        return Transition.to("manual_approval")

    @Stage(name="instant_refund", tools=["issue_stripe_refund"], terminal=True)
    async def instant_refund(self, slots: RefundRequest) -> Transition[None]:
        await self.tools.issue_stripe_refund(order_id=slots.order_id, amount=slots.amount_gbp)
        return Transition.end(canned_response=f"Refund of £{slots.order_id} (£{slots.amount_gbp:.2f}) issued.")

    @Stage(name="manual_approval", terminal=True)
    async def manual_approval(self, slots: RefundRequest) -> Transition[None]:
        return Transition.end(canned_response=f"Order {slots.order_id} (£{slots.amount_gbp:.2f}) routed to a specialist.")

    @Stage(name="security_lockout", terminal=True)
    async def security_lockout(self) -> Transition[None]:
        return Transition.end(canned_response="Session locked: identity verification failed.")
```

Run the **Static DCG Graph Compiler** and prove that `verify_identity` mathematically dominates every refund stage before executing a single turn:

```bash
# 1. Statically validate DCG (@Stage, @Transition, @Guard), PEP 695 transitions, and validator rules
$ blockstate check agent.py
✔ [AST-OK] QuickStartRefundAgent: 5 stages, 5 edges, 0 unreachable nodes (compiled in 11.4ms)

# 2. Prove mathematical dominance across all payout branches via Lengauer-Tarjan
$ blockstate verify agent.py --dominates "verify_identity -> [instant_refund, manual_approval]"
✔ [DOMINANCE PROVEN] 'verify_identity' strictly dominates 'instant_refund' and 'manual_approval' across all execution paths.
```

---

## 🏗️ Project Scaffolder (`blockstate init`) & Visual Inspector (`blockstate studio`)

Blockstate includes a zero-dependency project scaffolder and an offline visual topology and dominator verification studio. See [**CLI & Studio Reference (`docs/CLI_AND_STUDIO_REFERENCE.md`)**](docs/CLI_AND_STUDIO_REFERENCE.md) for full details.

### 1. Scaffold Regulated Enclaves in Seconds (`blockstate init`)

Generate a self-contained compliance enclave repository (complete with the DCG statechart agent, an automated `pytest` suite testing AST compilation, Lengauer-Tarjan dominance, bypass detection, and `BLK-SEC-001` tool gating, `.env.example`, and a GitHub Actions pull-request verification workflow):

```bash
# Scaffold a retail banking KYC & Faster Payments enclave
blockstate init ./my_banking_enclave --template banking

# Available starter templates: banking | insurance | healthcare | minimal
blockstate init ./my_fnol_enclave --template insurance --name motor_fnol
```

### 2. Local Visual Statechart Canvas & Dominator Verifier (`blockstate studio`)

Launch the local **Blockstate Studio** web server (`http.server.ThreadingHTTPServer`, zero external frontend dependencies) to inspect your statechart topology, run interactive sub-5ms Lengauer-Tarjan dominator proofs, replay `.blockstate/traces.jsonl` timelines, generate PEP 695 Python 3.12+ code, and export signed `SHA-256` MRM/CISO Dominance Audit Certificates:

```bash
# Launch Studio on http://127.0.0.1:8420 with live file hot-reloading
blockstate studio ./my_banking_enclave/banking_agent.py
```

---

## 📖 Documentation & Tutorials

| Resource | Description | Links |
| :--- | :--- | :--- |
| **60-Second Quickstart Guide** | Installation, scaffolding with `blockstate init`, running `blockstate check` and `blockstate verify`, launching `blockstate studio`, and executing a multi-turn session. | [**`docs/GETTING_STARTED.md`**](docs/GETTING_STARTED.md) |
| **CLI & Studio Developer Reference** | Complete reference for `check`, `verify`, `export`, `trace`, `init`, and `studio`, exit codes, error code taxonomy (`BLK-AST-014` to `BLK-STO-004`), Studio REST API, and signed `SHA-256` MRM Dominance Audit Certificates. | [**`docs/CLI_AND_STUDIO_REFERENCE.md`**](docs/CLI_AND_STUDIO_REFERENCE.md) |
| **Tutorial 1: Motor Insurance FNOL & Adversarial Audit** | Enterprise First Notice of Loss (FNOL) statechart with PAS policy verification, `TaskStack` pushdown digressions, YAML prompt isolation, and a 6-trial penetration audit suite. | [**Tutorial (`motor_insurance_claim_tutorial.md`)**](docs/tutorials/motor_insurance_claim_tutorial.md) · [**HTML Edition**](docs/tutorials/motor_insurance_claim_tutorial.html) · [**`01_motor_insurance_claim.py`**](examples/01_motor_insurance_claim.py) · [**`05_motor_insurance_claim_live.py`**](examples/05_motor_insurance_claim_live.py) · [**`05_defensive_audit.py`**](examples/05_defensive_audit.py) |
| **Tutorial 2: Retail Banking KYC Dominator Proofs** | Proving `VerifyKYC dom [TransferFunds]` and `step_up_mfa_kyc dom [faster_payments_uk, sepa_euro_transfer, swift_international]` in `< 50ms` with `LengauerTarjanSolver` and catching a rogue VIP bypass edge in CI with a synthesised BFS counterexample path. | [**Tutorial (`02_banking_kyc_dominator_tutorial.md`)**](docs/tutorials/02_banking_kyc_dominator_tutorial.md) · [**`02_banking_kyc_dominator.py`**](examples/02_banking_kyc_dominator.py) |
| **Tutorial 3: Healthcare HIPAA & NHS `PIIVault`** | Deterministic ingress tokenisation (NHS Modulus-11 & Luhn Mod-10 checksums), `HMAC-SHA256` session deduplication, `AES-256-GCM` AAD binding (`session_id:schema_version:revision_id`), volatile validator detokenisation (`wrap_detokenising_validator`), and `Detokenise[T]` tool unwrapping. | [**Tutorial (`03_healthcare_hipaa_piivault_tutorial.md`)**](docs/tutorials/03_healthcare_hipaa_piivault_tutorial.md) · [**`03_healthcare_hipaa_vault.py`**](examples/03_healthcare_hipaa_vault.py) |
| **Tutorial 4: LangGraph Enclave Handoff (`as_tool()`)** | Mounting a verified Blockstate mortgage origination compliance enclave inside an outer LangGraph router via `as_tool()` and `run_as_subflow()` returning the frozen 3-state discriminated union (`BlockstateTurnYield`, `BlockstateTurnComplete`, `BlockstateTurnAborted`). | [**Tutorial (`04_langgraph_enclave_handoff_tutorial.md`)**](docs/tutorials/04_langgraph_enclave_handoff_tutorial.md) · [**`04_langchain_subflow_handoff.py`**](examples/04_langchain_subflow_handoff.py) |
| **Architecture Spine & Interactive Deck** | Normative architectural decisions (`AD-1` to `AD-14`), Hexagonal ports and adapters boundaries, wire protocols, and self-contained HTML+SVG visual deck. | [**`docs/architecture/ARCHITECTURE-SPINE.md`**](docs/architecture/ARCHITECTURE-SPINE.md) · [**`architecture-deck.html`**](docs/architecture/architecture-deck.html) |
| **Product Roadmap, PRD & Specification** | Prioritised `v1.1`, `v1.2`, and `v2.0+` roadmap, functional requirements (`FR-1` to `FR-22`), and regulatory compliance mappings. | [**`docs/ROADMAP.md`**](docs/ROADMAP.md) · [**`docs/spec/prd.md`**](docs/spec/prd.md) · [**`docs/spec/SPEC.md`**](docs/spec/SPEC.md) |

---

## 📚 Domain Examples (4 Complete Walkthroughs)

To demonstrate how Blockstate adapts across industries and architectural patterns, explore these four complete, production-grade examples:

---

### Example 1: Motor Insurance Claims: Recursive Nested $\Delta$-Hydration, `on_breach` & `TaskStack` Digressions

**Scenario:** An insurance customer files a motor claim requiring nested address details, vehicle registration, and damage estimates. Mid-conversation, the customer goes off-script to ask *"Will claiming for a cracked windscreen affect my no-claims bonus?"*. Furthermore, if the policy covers commercial haulage, the claim must immediately exit early (`on_breach`) before asking remaining questions.

```python
from datetime import date
from pydantic import BaseModel, Field, field_validator
from blockstate import Agent, Stage, Transition, Guard, BreachError


class IncidentLocation(BaseModel):
    street: str = Field(description="Street name where the incident occurred")
    city: str = Field(description="Town or city")
    postcode: str = Field(pattern=r"^[A-Z]{1,2}\d[A-Z\d]?\s*\d[A-Z]{2}$")


class MotorClaimSlots(BaseModel):
    policy_number: str = Field(pattern=r"^POL-\d{6}$")
    vehicle_usage: str = Field(description="personal | commuting | commercial_haulage")
    incident_date: date = Field(description="Date of incident (YYYY-MM-DD)")
    estimated_damage_gbp: float = Field(gt=0)
    location: IncidentLocation  # Nested Pydantic model hydrated progressively!

    @field_validator("incident_date", mode="after")
    @classmethod
    def validate_not_in_future(cls, v: date) -> date:
        if v > date.today():
            raise ValueError("Incident date cannot be in the future.")
        return v

    @field_validator("vehicle_usage", mode="after")
    @classmethod
    def check_commercial_eligibility(cls, v: str) -> str:
        if v.lower() == "commercial_haulage":
            # Triggers immediate stage exit via @Stage(on_breach=...)
            raise BreachError("Commercial haulage policies require the Specialist Fleet Desk.")
        return v


class MotorInsuranceClaimAgent(Agent):
    @Stage(
        name="intake_claim",
        initial=True,
        collect=MotorClaimSlots,
        allow_digressions=["ncd_policy_faq"],
        allow_slot_overrides=False,  # Child FAQ frame cannot overwrite parent claim slots
        on_breach="commercial_fleet_desk",
        on_max_turns_exceeded="human_claims_handler",
        max_turns=6,
    )
    @Transition(targets=("tier_auto_payout", "tier_adjuster_review"))
    async def intake_claim(
        self, slots: MotorClaimSlots
    ) -> Transition["tier_auto_payout" | "tier_adjuster_review"]:
        if slots.estimated_damage_gbp <= 250.0:
            return Transition.to("tier_auto_payout")
        return Transition.to("tier_adjuster_review")

    @Stage(
        name="ncd_policy_faq",
        tools=["query_policy_handbook_rag"],  # Read-only tool isolated to FAQ frame
        max_turns=2,
    )
    async def ncd_policy_faq(self, query: str) -> Transition[None]:
        excerpt = await self.tools.query_policy_handbook_rag(topic=query)
        # Popping a digression frame automatically returns to 'intake_claim'
        # and prompts for the remaining unhydrated leaf paths (Delta)!
        return Transition.pop_frame(message=f"Policy Handbook: {excerpt}")

    @Stage(name="tier_auto_payout", tools=["authorise_bacs_payout"], terminal=True)
    async def tier_auto_payout(self, slots: MotorClaimSlots) -> Transition[None]:
        tx_ref = await self.tools.authorise_bacs_payout(
            policy=slots.policy_number, amount=slots.estimated_damage_gbp
        )
        return Transition.end(
            canned_response=f"Claim approved under £250 fast-track. Reference: {tx_ref}."
        )

    @Stage(name="tier_adjuster_review", tools=["create_adjuster_ticket"], terminal=True)
    async def tier_adjuster_review(self, slots: MotorClaimSlots) -> Transition[None]:
        ticket = await self.tools.create_adjuster_ticket(claim=slots.model_dump())
        return Transition.end(
            canned_response=f"Claim exceeds £250 fast-track limit. Assigned to adjuster ({ticket})."
        )

    @Stage(name="commercial_fleet_desk", terminal=True)
    async def commercial_fleet_desk(self) -> Transition[None]:
        return Transition.end(
            canned_response="Commercial haulage policies are handled by our Specialist Fleet Desk on 0800-000-111."
        )

    @Stage(name="human_claims_handler", terminal=True)
    async def human_claims_handler(self) -> Transition[None]:
        return Transition.end(canned_response="Transferring you to a live claims specialist with your details.")
```

**What happens at runtime across 3 turns:**
1. **Turn 1:** User says *"Hi, my policy is POL-884120, it's for personal use, and someone bumped my bumper in Manchester (`M1 1AG`) yesterday."*
   - `PartialSlotModel` validates and locks `policy_number`, `vehicle_usage`, `incident_date`, `location.city`, and `location.postcode`.
   - $\Delta$-Hydration computes $\Delta = \{\text{`location.street`}, \text{`estimated_damage_gbp`}\}$ and prompts only for those two missing fields.
2. **Turn 2 (Digression):** User asks *"Wait, before I tell you the street and cost, will this hurt my no-claims discount?"*
   - Pass 1 classifies the permitted digression `ncd_policy_faq`.
   - `TaskStack` pushes a child `TaskFrame(stage="ncd_policy_faq")`. Only `query_policy_handbook_rag` is present in the dispatch table (`authorise_bacs_payout` does not exist).
   - The FAQ is answered, `Transition.pop_frame()` pops the child frame, and Blockstate automatically resumes `intake_claim` with the 5 previously collected fields untouched.
3. **Turn 3:** User says *"Got it. It happened on Deansgate and the garage quoted £180."*
   - $\Delta = \emptyset$. The strict `MotorClaimSlots` model materialises, the guard sees `£180 <= £250`, and transitions deterministically to `tier_auto_payout`.

---

### Example 2: Retail Banking Wire Transfer: Multi-Branch KYC Dominator Proofs & Zero-LLM `ChoicePayload`

**Scenario:** A retail bank offers three payment rails (`faster_payments_uk`, `sepa_euro_transfer`, `swift_international`). Under UK FCA SYSC 13.7 operational risk rules, the Model Risk Management (MRM) team requires mathematical proof that **every payment rail** is strictly dominated by `step_up_mfa_kyc`, plus sub-120ms deterministic choice menus (`ChoicePayload`) for regulatory fee disclosures.

```python
from pydantic import BaseModel, Field
from blockstate import Agent, Stage, Transition, Guard, ChoicePayload, ChoiceOption


class TransferDetails(BaseModel):
    rail: str = Field(description="uk_faster | sepa_eur | swift_intl")
    amount_gbp: float = Field(gt=0, le=250_000)
    beneficiary_iban: str = Field(min_length=15, max_length=34)


class RetailWireTransferAgent(Agent):
    @Stage(
        name="select_payment_rail",
        initial=True,
        collect=TransferDetails,
        choice_options=ChoicePayload(
            prompt="Which payment rail would you like to use for your transfer?",
            options=[
                ChoiceOption(id="uk_faster", label="UK Faster Payments (£0 fee, instant)"),
                ChoiceOption(id="sepa_eur", label="SEPA Euro Transfer (£5 fee, 1 business day)"),
                ChoiceOption(id="swift_intl", label="International SWIFT Wire (£25 fee, 1-3 days)"),
            ],
        ),
    )
    @Transition(targets=("step_up_mfa_kyc",))
    async def select_payment_rail(
        self, slots: TransferDetails
    ) -> Transition["step_up_mfa_kyc"]:
        return Transition.to("step_up_mfa_kyc")

    @Stage(name="step_up_mfa_kyc", tools=["verify_fido2_passkey"], max_turns=3, on_max_turns_exceeded="fraud_lock")
    @Guard(requires=("user_id",))
    @Transition(targets=("faster_payments_uk", "sepa_euro_transfer", "swift_international", "fraud_lock"))
    async def step_up_mfa_kyc(
        self, slots: TransferDetails
    ) -> Transition["faster_payments_uk" | "sepa_euro_transfer" | "swift_international" | "fraud_lock"]:
        verified = await self.tools.verify_fido2_passkey(user_id=self.session["user_id"])
        if not verified:
            return Transition.to("fraud_lock")
        match slots.rail:
            case "uk_faster":
                return Transition.to("faster_payments_uk")
            case "sepa_eur":
                return Transition.to("sepa_euro_transfer")
            case _:
                return Transition.to("swift_international")

    @Stage(name="faster_payments_uk", tools=["execute_fps_rail"], terminal=True)
    async def faster_payments_uk(self, slots: TransferDetails) -> Transition[None]:
        await self.tools.execute_fps_rail(iban=slots.beneficiary_iban, amount=slots.amount_gbp)
        return Transition.end(canned_response="UK Faster Payment executed.")

    @Stage(name="sepa_euro_transfer", tools=["execute_sepa_rail"], terminal=True)
    async def sepa_euro_transfer(self, slots: TransferDetails) -> Transition[None]:
        await self.tools.execute_sepa_rail(iban=slots.beneficiary_iban, amount=slots.amount_gbp)
        return Transition.end(canned_response="SEPA Euro Transfer executed.")

    @Stage(name="swift_international", tools=["execute_swift_rail"], terminal=True)
    async def swift_international(self, slots: TransferDetails) -> Transition[None]:
        await self.tools.execute_swift_rail(iban=slots.beneficiary_iban, amount=slots.amount_gbp)
        return Transition.end(canned_response="SWIFT International Wire executed.")

    @Stage(name="fraud_lock", terminal=True)
    async def fraud_lock(self) -> Transition[None]:
        return Transition.end(canned_response="Transfer halted: MFA verification failed.")
```

**What happens if a developer introduces a bypass bug in a Pull Request?**
Suppose a developer modifies `select_payment_rail` to route transfers under `£10` directly to `faster_payments_uk`, bypassing `step_up_mfa_kyc`. In CI, `blockstate verify` immediately blocks the PR:

```bash
$ blockstate verify wire_agent.py \
    --dominates "step_up_mfa_kyc -> [faster_payments_uk, sepa_euro_transfer, swift_international]"

✖ [BLK-AST-002] DOMINATOR VIOLATION DETECTED
  Assertion:  step_up_mfa_kyc dom faster_payments_uk
  Status:     FAILED (Immediate dominator of 'faster_payments_uk' is 'select_payment_rail', not 'step_up_mfa_kyc')
  Counter-Example Bypass Path:
    [0] select_payment_rail (wire_agent.py:24)
    └──► [1] faster_payments_uk (wire_agent.py:49)  <-- BYPASSED 'step_up_mfa_kyc'
  Exit Code:  1
```

---

### Example 3: Healthcare Patient Triage: HIPAA/GDPR `PIIVault`, `Detokenise[T]` & SHA-256 `TurnLedger`

**Scenario:** A patient contacts an NHS/healthcare triage assistant providing their NHS Number, postcode, and symptoms. Under GDPR and HIPAA Safe Harbor rules, raw patient identifiers must **never** be transmitted to external commercial LLM APIs or written to telemetry logs, yet the internal electronic health record (EHR) lookup tool requires the real NHS Number to verify registration.

```python
from pydantic import BaseModel, Field
from blockstate import Agent, Stage, Transition, Detokenise


class PatientIntakeSlots(BaseModel):
    nhs_number: str = Field(description="Tokenised NHS identifier (<VAULT:NHS_NUMBER_NN>)")
    postcode: str = Field(description="Tokenised patient postcode (<VAULT:POSTCODE_NN>)")
    primary_symptom: str = Field(description="Clinical symptom summary")


# Tool handler receives real cleartext ONLY for arguments wrapped in Detokenise[T]
async def lookup_patient_ehr_record(
    nhs_number: Detokenise[str],
    postcode: Detokenise[str],
) -> dict[str, str]:
    # ToolGatekeeper automatically detokenises <VAULT:NHS_NUMBER_01> -> "485 777 3456"
    # strictly inside this isolated function scope, and re-sanitises the return value!
    return {"registration_status": "ACTIVE", "gp_practice_code": "Y00124"}


class ClinicalTriageAgent(Agent):
    @Stage(
        name="collect_patient_identifiers",
        initial=True,
        collect=PatientIntakeSlots,
    )
    @Transition(targets=("verify_ehr_registration",))
    async def collect_patient_identifiers(
        self, slots: PatientIntakeSlots
    ) -> Transition["verify_ehr_registration"]:
        # Notice: inside LLM prompts and slots, slots.nhs_number is literally "<VAULT:NHS_NUMBER_01>"
        return Transition.to("verify_ehr_registration")

    @Stage(
        name="verify_ehr_registration",
        tools=[lookup_patient_ehr_record],
        terminal=True,
    )
    async def verify_ehr_registration(self, slots: PatientIntakeSlots) -> Transition[None]:
        ehr = await self.tools.lookup_patient_ehr_record(
            nhs_number=slots.nhs_number,
            postcode=slots.postcode,
        )
        return Transition.end(
            canned_response=f"Registration verified ({ehr['registration_status']}) at practice {ehr['gp_practice_code']}."
        )
```

**Inspecting the Cryptographic `TurnLedger` & Local Trace (`blockstate trace`):**

```bash
$ blockstate trace --session 0192ce18-7b40-7000-8000-4a91f0012c9a
┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ Turn ┃ Transition                 ┃ Vaulted Input Tokens       ┃ Exposed Tool Whitelist     ┃ SHA-256 Chain    ┃
┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ 1    │ [START] ->                 │ "My NHS number is          │ [] (Zero Tools Exposed)    │ 8f9c2b1a...e401  │
│      │ collect_patient_identifiers│ <VAULT:NHS_NUMBER_01> and  │                            │ (prev: GENESIS)  │
│      │                            │ postcode <VAULT:POST_01>"  │                            │                  │
│ 2    │ collect_patient_identifiers│ "I have had a fever for    │ [lookup_patient_ehr_record]│ 3d41a90c...99b2  │
│      │ -> verify_ehr_registration │ two days"                  │                            │ (prev: 8f9c2b1a) │
└──────┴────────────────────────────┴────────────────────────────┴────────────────────────────┴──────────────────┘
✔ Cryptographic Hash Chain Verified (2/2 records intact, RFC 8785 JCS canonical SHA-256, 0 raw PII tokens leaked)
```

---

### Example 4: Outer Orchestrator Enclave (`as_tool()`): Embedding Blockstate Inside LangGraph, Semantic Kernel, AutoGen or AWS Bedrock

**Scenario:** Your enterprise already runs a high-level orchestrator (LangGraph, Microsoft Semantic Kernel, AutoGen, or AWS Bedrock) as an open-domain concierge router. Blockstate operates as a **transactional compliance execution enclave**: when the user asks to originate a mortgage or file a regulated claim, the outer orchestrator invokes the entire verified Blockstate statechart as a single validated tool via `from blockstate.adapters.langchain import as_tool`.

```python
from blockstate.adapters.langchain import as_tool
from my_regulated_workflows import MortgageOriginationEnclave

# Expose the entire formal statechart as a single validated tool to LangGraph
langgraph_agent.bind_tools([
    as_tool(MortgageOriginationEnclave, name="execute_mortgage_transaction")
])
```

You can also drive multi-turn subflow handoffs directly from custom API gateways using `PostgresStorageBackend` or `SQLiteStorageBackend` and match over the discriminated union (`BlockstateTurnYield | BlockstateTurnComplete | BlockstateTurnAborted`):

```python
from blockstate import BlockstateTurnYield, BlockstateTurnComplete, BlockstateTurnAborted
from examples.motor_insurance import MotorInsuranceClaimAgent
from blockstate.adapters.storage.postgres import PostgresStorageBackend

# 1. Instantiate your governed Blockstate enclave backed by PostgreSQL (or SQLite WAL)
claim_agent = MotorInsuranceClaimAgent(
    storage_backend=PostgresStorageBackend.from_dsn(
        "postgresql+asyncpg://blockstate_user:secret@localhost:5432/blockstate_db"
    )
)

# 2. Drive multi-turn enclave handoffs explicitly in your gateway router
async def handle_customer_turn(session_id: str, user_utterance: str) -> str:
    outcome = await claim_agent.run_as_subflow(
        session_id=session_id,
        user_message=user_utterance,
        initial_context={"is_authenticated": True, "user_id": "USR-99201"},
    )

    match outcome:
        case BlockstateTurnYield(message=prompt_msg, missing_slots=delta):
            # Blockstate is mid-flow collecting slots or answering a TaskStack digression
            return prompt_msg

        case BlockstateTurnComplete(result=validated_claim, ledger_head_hash=audit_hash):
            # Terminal stage reached! Control returns to outer router with a strict Pydantic model
            return f"Workflow complete (Audit Hash: {audit_hash[:12]}): {validated_claim}"

        case BlockstateTurnAborted(error_code=code, reason=reason):
            # SecurityViolationError or max_turns circuit breaker triggered
            return f"[{code}] Escalating to human specialist: {reason}"
```

---

## 🛠️ CLI Reference, Declarative Control Grammar & Compile-Time Verification

### Addressing Rice's Theorem with the Declarative Control Grammar (DCG)

By **Rice's Theorem**, any non-trivial semantic property of an unconstrained, Turing-complete Python program (such as dynamic reflection `getattr(self, dynamic_state)()`, `eval()`, or computed stage targets) is mathematically undecidable by static analysis.

To make Lengauer-Tarjan dominator verification 100% sound and decidable, Blockstate requires all stage topologies to be declared using the **Declarative Control Grammar (DCG)**: `@Stage`, `@Transition`, and `@Guard` combined with static PEP 695 `Transition["target_a" | "target_b"]` annotations. If `blockstate check` or `blockstate verify` encounters dynamic reflection or a non-static transition target in the AST, it immediately terminates the build pass:

```text
[Blockstate E014] Dynamic transition destination detected at mortgage_flow.py:42.
Transitions must reference static State definitions to enable Lengauer-Tarjan dominator verification.
```

### CLI Commands (`blockstate`)

Blockstate ships a comprehensive command-line toolkit (`blockstate`) powered by `Typer` and `Rich` (see [**`docs/CLI_AND_STUDIO_REFERENCE.md`**](docs/CLI_AND_STUDIO_REFERENCE.md)):

| Command | Purpose | CI / Production Usage |
| :--- | :--- | :--- |
| `blockstate init [DIR] --template <T>` | Scaffolds a ready-to-verify compliance enclave (`banking`, `insurance`, `healthcare`, `minimal`) with an automated `pytest` suite and GitHub Actions workflow. | Project bootstrap (`< 50ms`). |
| `blockstate check [PATH]` | Parses Python source files via `ast.parse` (zero code execution). Enforces **Declarative Control Grammar (DCG)** (`@Stage`, `@Transition`, `@Guard`), rejects dynamic transition targets with `[Blockstate E014]`, checks unreachable dead stages (`BLK-AST-002`), and ensures zero `Transition` calls exist inside `@field_validator` methods (`FR-2`, `FR-10`). | Run in pre-commit & PR lint stage (`< 25ms`). |
| `blockstate verify [PATH] --dominates "A -> [B, C]"` | Constructs the complete Control Flow Graph (including `on_breach`, `on_refusal`, `on_max_turns_exceeded`, and `on_security_violation` edges) and executes the **Lengauer-Tarjan dominator tree algorithm** (`FR-11`). Emits a verification certificate or exits with code `1` and the exact violating counter-example path. | Mandatory MRM / FCA / EU AI Act compliance gate in CI. |
| `blockstate export [PATH] --format [mermaid\|json]` | Exports verified statechart topologies, stage tool whitelists, and guard annotations to Markdown Mermaid diagrams or JSON Schema (`FR-12`). | Auto-generate PR architecture previews & audit docs. |
| `blockstate trace --session <UUID>` | Replays a multi-turn session from `.blockstate/traces.jsonl` (`FR-21`), verifying SHA-256 `TurnLedger` hash continuity and rendering turn-by-turn slot hydration and tool boundaries. | Local developer debugging & SRE incident response (`< 5 min` MTTR). |
| `blockstate studio [PATH]` | Launches the local HTTP visual statechart canvas, sub-5ms Lengauer-Tarjan dominator overlay, trace replay inspector, PEP 695 codegen workbench, and signed `SHA-256` MRM Dominance Audit Certificate exporter. | Interactive architecture inspection & MRM certification. |

---

## 🧪 Enterprise Testing, Coverage & Interface Contracts

Blockstate enforces a **Three-Tier Verification Architecture (`AD-11`)** designed to eliminate both boundary edge-case bugs and subtle integration drift across storage and model adapters:

### 1. Enforced Line & Branch Coverage Gates

Coverage is measured with `pytest-cov` (`--cov-branch`) and strictly enforced in CI:

| Architectural Ring | Packages Covered | Minimum Line Coverage | Minimum Branch Coverage |
| :--- | :--- | :--- | :--- |
| **Tier 1: Pure Kernel & Security** | `blockstate.core`, `blockstate.compiler`, `blockstate.security` | **$\ge 95\%$** | **$\ge 90\%$** |
| **Tier 2: Full Repository** | All packages including `blockstate.adapters.*` & `blockstate.cli` | **$\ge 90\%$** | **$\ge 85\%$** |

### 2. Port Interface Contract Suites (`blockstate.testing.contracts`)

To prevent integration failures where an agent works against `InMemoryStorageBackend` in unit tests but fails under `SQLiteStorageBackend` or `PostgresStorageBackend` in production, every port in `blockstate.ports` defines an abstract **Interface Contract Test Suite**:

- **`StorageBackendContractSuite` (`StateStoreContractSuite`):** Automatically tests all three canonical v1 `StorageBackend` drivers (`InMemoryStorageBackend`, `SQLiteStorageBackend` with `PRAGMA journal_mode=WAL`, and `PostgresStorageBackend` via `SQLAlchemy[asyncio]` + `asyncpg`) as well as any v1.2+ SPI plugins (Redis, Microsoft SQL Server, AWS DynamoDB) against identical assertions:
  - Concurrent `acquire_lease` contention (verifying `ConcurrentSessionError` / `BLK-CON-409` after `2000ms`).
  - Stale lease automatic TTL expiry (`30000ms`) and active `10000ms` heartbeat renewal.
  - Optimistic Compare-And-Swap (`revision_id`) conflict detection on simultaneous checkpoint writes (`BLK-STO-002`).
  - Rolling-deployment `SchemaUpcaster` migration (`schema_version: 1 -> 2`) on historical session payloads.
  - Boundary payloads: empty strings, 64KB slot strings, maximum nesting depth (`depth=8`), Unicode zero-width joiners/RTL overrides, null-byte rejection, and leap-year/UTC midnight timestamps.
- **`PIIStripperContractSuite`:** Verifies deterministic token stability within a session, zero token collision across distinct PII values, resistance to partial-token prompt injection (`<VAULT:...>`), and round-trip `Detokenise[T]` restoration.
- **`LedgerSinkContractSuite`:** Verifies monotonic `turn_index` ordering per `session_id`, RFC 8785 JCS SHA-256 tamper detection (flipping a single bit in turn $k$ invalidates turn $k+1 \dots n$), and `SIGTERM` local `.blockstate/wal/` spill and recovery.

### 3. Property-Based Fuzzing (`Hypothesis`)

In addition to deterministic example tests, CI runs **Hypothesis** property-based test suites (`max_examples=500` per property):
- **Arbitrary Nested Pydantic Schema Fuzzing:** Generates random multi-level `BaseModel` trees and random sequences of partial/invalid turn inputs, proving that $\Delta$-Hydration is monotonic, never mutates locked slots, and instantiates the target model if and only if $\Delta = \emptyset$.
- **Arbitrary Directed Graph Dominator Fuzzing:** Generates random cyclic, acyclic, diamond, and multi-exit graphs ($|V| \le 250$), proving that `blockstate.compiler.dominators` (Lengauer-Tarjan) agrees 100% with brute-force path enumeration.

```bash
# Run the full test suite with branch coverage and architectural import-linter checks
uv run lint-imports
uv run pytest --cov=blockstate --cov-branch --cov-report=term-missing
```

---

## 🔒 Zero-Leakage Security, `.env` Hygiene & Git Hooks

Blockstate enforces a strict **Zero PII & Zero Credential Leakage Policy (`AD-13`)**:

1. **Automated Pre-Commit & Pre-Push Scanning (`.pre-commit-config.yaml`):**
   - **Gitleaks (`v8.30.1`)** and **Yelp `detect-secrets` (`v1.5.0`)** scan every staged diff and commit for API keys, cloud tokens, private keys, and high-entropy connection strings.
   - **`blockstate-pii-guard`** scans staged files for accidental inclusion of UK National Insurance numbers, NHS numbers, US SSNs, Luhn-valid payment card numbers, and Bearer tokens.
   - **Sensitive File Blocker** physically rejects any commit attempting to stage `.env` (other than `.env.example`), `.blockstate/` trace directories, `.wal` files, or `.db`/`.sqlite` files.
2. **Typed `SecretStr` Configuration (`BlockstateSettings`):**
   - All environment variables documented in [`.env.example`](.env.example) (`BLOCKSTATE_` prefix) are loaded via `pydantic-settings` into `pydantic.SecretStr` fields, ensuring credentials are automatically masked as `**********` in logs, stack traces, and local crash dumps.

```bash
# Enable the zero-leakage git hooks immediately after cloning
uv run pre-commit install --hook-type pre-commit --hook-type pre-push
```

See [**SECURITY.md**](SECURITY.md) for the complete zero-trust threat model and vulnerability disclosure policy.

---

## ⚖️ Licensing, UK Patent Pending & Commercial Production

**Blockstate (`blockstate`)** is **UK Patent Pending** (*"Process execution control system"*, filed with the United Kingdom Intellectual Property Office under Section 62(1) of the UK Patents Act 1977) and is distributed under a **Source-Available & Commercial Dual Licence** (see [**`LICENSE`**](LICENSE) and [**`PATENTS.md`**](PATENTS.md)):

| Tier | Permitted Scope | Cost |
| :--- | :--- | :--- |
| **1. Community, Dev & Evaluation Tier** | Local development, academic research, open-source CI/CD verification (`blockstate verify`), and up to 90 days of internal staging evaluation/prototyping | **Free (£0)** |
| **2. Commercial Production & Enterprise Tier** | Live commercial production deployments (FinTech, InsurTech, HealthTech, Telco, Enterprise), managed cloud hosting, or SaaS embedding | **Commercial Enterprise & Patent Licence** |

### Third-Party Dependency Supply-Chain Governance

Every third-party runtime and optional dependency used by `blockstate` is audited and restricted in CI (`pip-licenses` + CycloneDX SBOM generation) to **permissive enterprise-approved licences (`MIT`, `Apache-2.0`, `BSD-3-Clause`, `PSF-2.0`)** with **zero copyleft (`GPL`/`AGPL`/`LGPL`) dependencies**:

| Component Layer | Packages | SPDX Licences | Copyleft Risk |
| :--- | :--- | :--- | :--- |
| **Core Runtime & InMemory Backend** | `python` (`3.12+`), `pydantic`, `pydantic-settings`, `cryptography`, `typer`, `rich` | `PSF-2.0`, `MIT`, `Apache-2.0 / BSD-3-Clause` | **None (0%)** |
| **Model & Classifier Extras** | `pydantic-ai-slim`, `litellm` (`<1.107.0`), `onnxruntime` | `MIT` | **None (0%)** |
| **v1 StorageBackend & Ledger Extras** | `aiosqlite` (SQLite WAL), `SQLAlchemy` + `asyncpg` (PostgreSQL), `aioboto3` (S3 WORM) | `MIT`, `Apache-2.0` | **None (0%)** |
| **Observability Extra** | `opentelemetry-api`, `opentelemetry-sdk` | `Apache-2.0` | **None (0%)** |

*(Note: Redis, Microsoft SQL Server, and AWS DynamoDB storage adapters are scheduled for v1.2+ via the formal `StorageBackend` SPI in `src/blockstate/ports/storage.py`.)*

For the complete package-by-package version, licence, and upstream repository manifest, see [**THIRD_PARTY_LICENSES.md**](THIRD_PARTY_LICENSES.md).

---

## 🤝 Community, Red-Teaming & Enterprise Pilots

- **Red-Team Your Agent Graph (`blockstate verify`)**: Run `blockstate verify` against your regulated workflow statecharts to detect unreachable states and Lengauer-Tarjan dominator bypass paths in `< 50ms`, and share anonymised statechart patterns in **GitHub Discussions**.
- **Enterprise Production Pilots & Commercial Licensing**: Building a regulated AI workflow in **retail banking (FCA SYSC 13.7)**, **insurance claims (FNOL)**, **healthcare triage (NHS / HIPAA)**, or **telecommunications**? Open an issue or contact the maintainer to arrange a **Commercial Enterprise & Patent Licence** or a **Regulated AI Enclave Implementation Pilot**.

---

## 🗺️ Architectural Documentation & Artefacts

- [**Getting Started (`docs/GETTING_STARTED.md`)**](docs/GETTING_STARTED.md): 60-second quickstart covering installation, `blockstate init`, `blockstate check`, `blockstate verify`, `blockstate studio`, and multi-turn session execution.
- [**CLI & Studio Reference (`docs/CLI_AND_STUDIO_REFERENCE.md`)**](docs/CLI_AND_STUDIO_REFERENCE.md): Complete CLI command manual, exit codes, error code catalogue (`BLK-AST-014` to `BLK-STO-004`), and MRM Dominance Audit Certificate export.
- [**Project Licence (`LICENSE`)**](LICENSE) & [**Statutory UK Patent Pending Notice (`PATENTS.md`)**](PATENTS.md): Dual-licensing terms and UK Patents Act 1977 s.62(1) notice.
- [**Product & Engineering Roadmap (`docs/ROADMAP.md`)**](docs/ROADMAP.md): Prioritised `v1.1`, `v1.2`, and `v2.0+` feature backlog (`blockstate studio` visual AST canvas & `.py` codegen, `blockstate init`, Semantic Kernel/AutoGen/Bedrock enclaves, Redis/MSSQL/DynamoDB drivers, and `blockstate fuzz`).
- [**Enterprise Motor Insurance Claims Tutorial & Adversarial Audit (`motor_insurance_claim_tutorial.md`)**](docs/tutorials/motor_insurance_claim_tutorial.md) ([**HTML Edition**](docs/tutorials/motor_insurance_claim_tutorial.html) · [**Live Agent (`05_motor_insurance_claim_live.py`)**](examples/05_motor_insurance_claim_live.py) · [**6-Trial Penetration Suite (`05_defensive_audit.py`)**](examples/05_defensive_audit.py)): Full worked enterprise FNOL walkthrough with zero-stub PAS verification, System-1 semantic routing, YAML prompt isolation, and Lengauer-Tarjan dominator proofs.
- [**Retail Banking KYC Dominator Tutorial (`02_banking_kyc_dominator_tutorial.md`)**](docs/tutorials/02_banking_kyc_dominator_tutorial.md) ([**Reference Agent (`02_banking_kyc_dominator.py`)**](examples/02_banking_kyc_dominator.py)): Step-by-step walkthrough of `LengauerTarjanSolver` dominance verification and BFS counterexample synthesis on rogue bypass edges.
- [**Healthcare HIPAA & NHS `PIIVault` Tutorial (`03_healthcare_hipaa_piivault_tutorial.md`)**](docs/tutorials/03_healthcare_hipaa_piivault_tutorial.md) ([**Reference Agent (`03_healthcare_hipaa_vault.py`)**](examples/03_healthcare_hipaa_vault.py)): Deterministic ingress tokenisation (NHS Modulus-11 & Luhn Mod-10), `HMAC-SHA256` session deduplication, `AES-256-GCM` AAD binding, `wrap_detokenising_validator`, and `Detokenise[T]`.
- [**LangGraph Enclave Handoff Tutorial (`04_langgraph_enclave_handoff_tutorial.md`)**](docs/tutorials/04_langgraph_enclave_handoff_tutorial.md) ([**Reference Agent (`04_langchain_subflow_handoff.py`)**](examples/04_langchain_subflow_handoff.py)): Mounting a verified Blockstate compliance enclave inside an outer LangGraph router via `as_tool()`.
- [**Architecture Spine (`ARCHITECTURE-SPINE.md`)**](docs/architecture/ARCHITECTURE-SPINE.md): The normative build substrate fixing all 14 Architectural Decisions (`AD-1` to `AD-14`), Hexagonal boundaries, wire protocols, and stack versions.
- [**Interactive Visual Architecture Deck (`architecture-deck.html`)**](docs/architecture/architecture-deck.html): Self-contained HTML+SVG executive and engineering walkthrough deck.
- [**Product Requirements Document (`prd.md`)**](docs/spec/prd.md): Functional requirements (`FR-1` to `FR-22`), user journeys (`UJ-1` to `UJ-3`), and regulatory mapping (`EU AI Act`, `UK FCA`, `HIPAA`, `SOC 2`).
- [**Technical Specification (`SPEC.md`)**](docs/spec/SPEC.md): Execution specification, state transitions, and compliance reference mappings.
- [**Third-Party Licences (`THIRD_PARTY_LICENSES.md`)**](THIRD_PARTY_LICENSES.md) & [**Security Policy (`SECURITY.md`)**](SECURITY.md).
