Implement Phase 0b base project against the frozen test contract

All seven services (gateway, ledger, fraud, sanctions, insights, notifier,
audit), db/schema.sql plus optional_indexes.sql, scripts/seed.py and
smoke.py, the gateway-served frontend, and docker-compose.yml. Plain-text
stdlib logging and a weak manual X-Request-Id only -- no observability yet,
by design, per test_no_observability.py. Implements all 13 named chaos-flag
pathologies from docs/PATHOLOGIES.md behind Redis-backed runtime flags.

make smoke and make test-contract pass; make test-baseline and
make test-pathology pass except for a handful of load-sensitive tests
documented in tests/README.md rather than papered over.
This commit is contained in:
Movses Aslanyan
2026-09-05 08:12:30 +03:30
parent ccf1783779
commit 91afea92a1
51 changed files with 2757 additions and 0 deletions
View File
+49
View File
@@ -0,0 +1,49 @@
from contextlib import asynccontextmanager
import asyncio
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from common.db import engine
from common.logging_config import configure_logging
from common.redis_client import redis_client
from . import consumer
SERVICE_NAME = "notifier"
logger = configure_logging(SERVICE_NAME)
@asynccontextmanager
async def lifespan(app: FastAPI):
task = asyncio.create_task(consumer.run())
yield
task.cancel()
app = FastAPI(title=SERVICE_NAME, lifespan=lifespan)
@app.get("/")
async def root():
return {"service": SERVICE_NAME, "version": "0.1.0"}
@app.get("/healthz")
async def healthz(request: Request):
if request.headers.get("X-Ledgerline-Probe") == "chatty-health":
logger.info("received chatty-health healthz probe from gateway")
return {"status": "ok", "service": SERVICE_NAME}
@app.get("/readyz")
async def readyz():
try:
async with engine.connect() as conn:
await conn.exec_driver_sql("SELECT 1")
await redis_client.ping()
except Exception as exc:
logger.exception(f"readyz check failed: {exc}")
return JSONResponse(status_code=503, content={"status": "unavailable"})
return {"status": "ok"}
+8
View File
@@ -0,0 +1,8 @@
"""slow_consumer -- phase-0-base-project-spec.md §7. Owned by `notifier`."""
from common.chaos_flags import is_enabled
SLEEP_SECONDS = 0.25
async def slow_consumer_enabled() -> bool:
return await is_enabled("slow_consumer")
+78
View File
@@ -0,0 +1,78 @@
import asyncio
import logging
import random
import uuid
import sqlalchemy as sa
from common.db import Session
from common.redis_client import redis_client
from common.settings import settings
from . import chaos
logger = logging.getLogger("notifier")
GROUP = "notifier"
CONSUMER = "notifier-1"
async def ensure_group():
try:
await redis_client.xgroup_create(settings.stream_name, GROUP, id="$", mkstream=True)
except Exception as exc:
if "BUSYGROUP" not in str(exc):
raise
async def _handle(fields: dict):
if await chaos.slow_consumer_enabled():
await asyncio.sleep(chaos.SLEEP_SECONDS)
async with Session() as session:
await session.execute(
sa.text(
"INSERT INTO notifications (id, transaction_id, channel, status, attempts, sent_at) "
"VALUES (:id, :tx_id, :channel, 'sent', 1, now())"
),
{
"id": str(uuid.uuid4()),
"tx_id": fields.get("transaction_id"),
"channel": random.choice(["email", "push", "sms"]),
},
)
await session.commit()
async def run():
"""Background consumer loop for the `notifier` consumer group -- a second,
independent group (`audit`) reads the same stream in services/audit/worker.py.
Redis Streams carry no automatic context propagation, deliberately."""
await ensure_group()
logger.info("notifier consumer started")
while True:
try:
resp = await redis_client.xreadgroup(
groupname=GROUP,
consumername=CONSUMER,
streams={settings.stream_name: ">"},
count=10,
block=2000,
)
except asyncio.CancelledError:
raise
except Exception:
logger.exception("notifier consumer read failed")
await asyncio.sleep(1)
continue
if not resp:
continue
for _, messages in resp:
for msg_id, fields in messages:
try:
await _handle(fields)
except Exception:
logger.exception(f"notifier failed to process message {msg_id}")
await redis_client.xack(settings.stream_name, GROUP, msg_id)