Agentic Event Store & Enterprise Audit Infrastructure
An append-only event sourcing system for Apex Financial Services' multi-agent AI platform. Provides immutable audit trails for commercial loan application processing with optimistic concurrency control, CQRS projections, and cryptographic integrity verification.
- Python 3.11+
- PostgreSQL 16+
# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install core + dev dependencies
pip install -r requirements.txt
pip install pytest pytest-asyncio fakerdocker run -d \
-e POSTGRES_PASSWORD=apex \
-e POSTGRES_DB=apex_ledger \
-p 5432:5432 \
--name apex-postgres \
postgres:16psql -h localhost -U postgres -d apex_ledger -f schema.sqlcp .env.example .env
# Edit .env with your API keys# Validate schema (no DB needed)
python datagen/generate_all.py --skip-db --skip-docs --validate-only
# Generate all data (requires PostgreSQL)
python datagen/generate_all.py --db-url postgresql://postgres:apex@localhost/apex_ledger# Phase 0 (schema validation — must pass first)
pytest tests/test_schema_and_generator.py -v
# Phase 1 (EventStore core — InMemoryEventStore)
pytest tests/phase1/test_event_store.py -v
# Concurrency tests (OCC double-decision)
pytest tests/test_concurrency.py -v
# All tests
pytest tests/ -vthe-ledger/
├── ledger/ # Core application package
│ ├── event_store.py # EventStore (6 async methods) + InMemoryEventStore
│ ├── domain/
│ │ └── aggregates/
│ │ ├── loan_application.py # LoanApplication aggregate (state machine)
│ │ └── agent_session.py # AgentSession aggregate (Gas Town pattern)
│ ├── commands/
│ │ └── handlers.py # Command handlers (4-step pattern)
│ ├── schema/
│ │ └── events.py # 45 Pydantic event types + EVENT_REGISTRY
│ ├── agents/ # AI agent framework (Phase 2-3)
│ ├── projections/ # CQRS projections (Phase 4)
│ ├── registry/ # Applicant registry client
│ └── upcasters.py # Schema evolution (Phase 4)
├── tests/
│ ├── phase1/test_event_store.py # Phase 1 gate tests (11 tests)
│ ├── test_concurrency.py # OCC double-decision + suite (11 tests)
│ └── test_schema_and_generator.py
├── datagen/ # Seed data generators
├── data/seed_events.jsonl # 1,200+ pre-generated events
├── schema.sql # PostgreSQL DDL (4 tables)
├── DOMAIN_NOTES.md # Domain analysis answers
├── pyproject.toml
└── README.md
- Append-only PostgreSQL store with
SELECT ... FOR UPDATEfor DB-level OCC - Transactional outbox — events + outbox written atomically in single transaction
- 6 async methods:
stream_version,append,load_stream,load_all,get_event,get_stream_metadata - UpcasterRegistry for schema evolution without mutating stored events
- LoanApplicationAggregate — 7+ lifecycle states, per-event dispatch handlers, business rule guards
- AgentSessionAggregate — Gas Town persistent ledger pattern, model version enforcement
- Command handlers — load → validate → determine → append with multi-aggregate support
| Table | Purpose |
|---|---|
events |
Immutable event log with identity-based global ordering |
event_streams |
Stream metadata + version for OCC |
projection_checkpoints |
Async daemon position tracking |
outbox |
Guaranteed event delivery via transactional outbox |
| Aggregate | Stream ID | Purpose |
|---|---|---|
| LoanApplication | loan-{id} |
Full loan lifecycle |
| AgentSession | agent-{agent_id}-{session_id} |
AI agent work sessions |
| ComplianceRecord | compliance-{id} |
Regulatory checks |
| AuditLedger | audit-{type}-{id} |
Cryptographic integrity |
- State machine — only valid transitions allowed (DomainError on violation)
- Gas Town pattern — agents must declare context before decisions
- Model version locking — no duplicate credit analyses
- Confidence floor — score < 0.6 forces REFER recommendation
- Compliance dependency — approval requires all checks passed
- Causal chain — decisions must reference contributing sessions