TaipanStack Architecture¶
TaipanStack is built on the philosophy of maximum safety by default, combined with blazing-fast asynchronous programming. The internal architecture enforces a strict layered dependency map, statically verified on every commit.
๐ค AI Agents Note: AI assistants modifying architectural components must abide by the rules detailed in the AI Agents Guidelines.
Layered Dependency Model¶
The architecture enforces unidirectional data flow. Upper layers may import from lower layers; lower layers may never import upward.
graph TD
A["๐ข Application Layer<br/>(your business logic)"]
B["๐ก๏ธ Security Layer<br/>guards ยท sanitizers ยท validators ยท decorators"]
C["โ๏ธ Config Layer<br/>models ยท generators ยท version_config"]
D["๐ง Utils Layer<br/>logging ยท filesystem ยท context"]
E["๐ฏ Core Layer<br/>result ยท compat ยท optimizations"]
F["๐ Resilience Layer<br/>circuit_breaker ยท retry ยท watchdogs"]
G["๐ Bridges Layer<br/>http ยท db"]
A --> B
A --> C
A --> D
A --> F
A --> G
F --> D
F --> E
G --> B
G --> F
B --> E
C --> E
D --> E
style A fill:#4A148C,color:#fff
style B fill:#880E4F,color:#fff
style C fill:#1A237E,color:#fff
style D fill:#004D40,color:#fff
style E fill:#BF360C,color:#fff
style F fill:#006064,color:#fff
style G fill:#E65100,color:#fff
Dependency Contracts (Import Linter)¶
These contracts are enforced statically in CI via Import Linter. Any violation fails the build immediately.
| Contract | Rule |
|---|---|
core is independent |
taipanstack.core cannot import from security, utils, config, or resilience |
security is independent |
taipanstack.security cannot import from utils, config, or resilience |
utils is independent |
taipanstack.utils cannot import from security, config, or resilience |
config only uses core |
taipanstack.config cannot import from security, utils, or resilience |
resilience only uses core |
taipanstack.resilience cannot import from security or config |
bridges isolation |
taipanstack.bridges cannot import from config |
Project Structure¶
TaipanStack/
โโโ src/
โ โโโ taipanstack/
โ โโโ __init__.py # Public API surface
โ โโโ bridges/ # ๐ External communication integrations
โ โ โโโ http_bridge.py # Safe HTTP client
โ โ โโโ db_bridge.py # Resilient database connector
โ โ โโโ web_bridge.py # Result-to-HTTP response utilities
โ โโโ config/ # โ๏ธ Configuration models & generators
โ โ โโโ models.py # StackConfig (Pydantic v2)
โ โ โโโ generators.py # Project scaffolding templates
โ โ โโโ version_config.py # Version-aware recommendations
โ โโโ core/ # ๐ฏ Result types, compat, optimizations
โ โ โโโ result.py # Ok / Err / safe
โ โ โโโ compat.py # Python version feature flags
โ โ โโโ optimizations.py # uvloop / orjson bootstrap
โ โโโ resilience/ # ๐ Reliability patterns
โ โ โโโ adaptive/ # Orchestrators and Bulkheads
โ โ โโโ watchdogs/ # System health monitoring
โ โ โโโ circuit_breaker.py # Circuit breaker with on_state_change callback
โ โ โโโ retry.py # Exponential backoff with on_retry callback
โ โโโ security/ # ๐ก๏ธ Guards, sanitizers, validators
โ โ โโโ guards.py # Path traversal, command injection, env guards
โ โ โโโ sanitizers.py # XSS, filename, SQL, env value sanitization
โ โ โโโ validators.py # Email, URL, project name, Python version
โ โ โโโ decorators.py # @safe_path, @safe_env wrappers
โ โโโ utils/ # ๐ง Cross-cutting utilities
โ โโโ cache.py # Caching with @cached decorator
โ โโโ logging.py # Structured logging (structlog)
โ โโโ filesystem.py # Safe file operations
โ โโโ rate_limit.py # Rate limiters
โ โโโ concurrency.py # Async utilities
โ โโโ subprocess.py # Sandboxed subprocess execution
โโโ tests/ # โ
comprehensive test suite, 100% coverage
โ โโโ test_property_sanitizers_expected.py # Hypothesis fuzzing
โ โโโ test_v034_async_retry_circuit.py # Async tests for retry and circuit breaker
โโโ docs/ # ๐ MkDocs Material documentation
โโโ .semgrep/ # ๐ Custom SAST rules
โโโ .github/ # ๐ CI/CD + SBOM/SLSA + Docs workflows
โโโ Dockerfile # ๐ณ Hardened multi-stage container
โโโ pyproject.toml # ๐ Modern dependency management (Poetry)
Concurrency Model¶
When production dependencies are installed (uvloop group), TaipanStack bootstraps uvloop as the asyncio event loop, providing a 2โ4ร throughput improvement over the standard asyncio loop on Linux/macOS.
from taipanstack.core.optimizations import apply_optimizations
apply_optimizations() # Auto-installs uvloop + orjson if available
Python version detection is performed at import time via taipanstack.core.compat:
from taipanstack import PY313, get_features
if PY313:
print(get_features()) # Reports 3.13-specific optimizations
Error Handling Philosophy¶
Exceptions represent unrecoverable panics. For standard, expected failures (network errors, validation failures, missing data), TaipanStack uses the Result monad:
Rule: Never raise to communicate a business failure
# โ DO NOT: leaks exception semantics into caller
def find_user(user_id: str) -> User:
raise UserNotFoundError(...)
# โ
DO: encode failure in the return type
def find_user(user_id: str) -> Result[User, UserNotFoundError]:
return Err(UserNotFoundError(user_id))
Benefits enforced by Mypy strict:
- You cannot forget to handle an
Errโ the type checker forces it - Pattern matching (
match/case) gives exhaustive, safe unpacking - The
@safedecorator wraps any raising function into aResultautomatically
Security Strategy¶
TaipanStack's security layer is fail-closed by design:
| Principle | Implementation |
|---|---|
| No silent failures | Malformed input raises SecurityError or TypeError immediately |
| Boundary validation | All external data validated at entry via pydantic + guards |
| Runtime type guards | Every public security function validates input types at v0.3.1+ |
| AST scanning | Bandit + Semgrep (custom rules) runs on every CI push |
| Supply chain | SBOM (CycloneDX via Syft) + SLSA attestation (Cosign/Sigstore) on every release |