mirror of
https://github.com/x1xhlol/system-prompts-and-models-of-ai-tools.git
synced 2026-06-18 07:19:35 +00:00
Program F — Multi-Tenancy RLS (Row-Level Security):
alembic 20260417_0002_add_rls.py: Enables RLS on 23 tenant-scoped tables.
database_rls.py: set_tenant_context() helpers for SET LOCAL app.tenant_id.
middleware/tenant_rls.py: Extracts tenant_id from JWT on every request.
Default-deny when no context. PostgreSQL only (CI safe on SQLite).
Result: OWASP A01:2025 — access control enforced at DB layer.
Program G — Idempotency Standard:
models/idempotency_key.py: IdempotencyKey table with TTL + SHA256 hash.
services/idempotency_service.py: get_existing/store with request fingerprint.
middleware/idempotency.py: HTTP middleware on POST/PUT/PATCH.
Result: Duplicate side effects prevented on retry.
Program E — Persistent Durable Execution:
models/durable_checkpoint.py: DurableCheckpoint with sequence_num + status.
services/durable_runtime.py: start_run/checkpoint/complete/resume/list_incomplete.
Result: Workflows survive crashes — resume from last persisted checkpoint.
Program K — OpenTelemetry:
observability/otel.py: init/span/inject_correlation_id with graceful
degradation when OTel packages absent.
openclaw/gateway.py: Wraps execute() in span, binds correlation_id to
trace_id. Bridge between business correlation and production observability.
Program J — Release Gate Hardening:
docs/governance/release-gates.md: Documents 3 mandatory gates.
.github/workflows/dealix-ci.yml: Adds release_readiness_matrix as CI step.
release_readiness_matrix.py: Updated to check 41/41 components.
Verification:
architecture_brief.py: 40/40 PASS
release_readiness_matrix.py: 41/41 PASS
https://claude.ai/code/session_01W1rJthWDkasijTdXCfxVHs
94 lines
3.2 KiB
Python
94 lines
3.2 KiB
Python
"""Idempotency Middleware — checks Idempotency-Key header on POST/PUT.
|
|
|
|
If key exists, returns cached response (no side effects).
|
|
Otherwise, stores response after successful execution.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from starlette.requests import Request
|
|
from starlette.responses import JSONResponse, Response
|
|
|
|
|
|
IDEMPOTENT_METHODS = {"POST", "PUT", "PATCH"}
|
|
|
|
|
|
class IdempotencyMiddleware(BaseHTTPMiddleware):
|
|
"""Middleware: idempotent retry support via Idempotency-Key header.
|
|
|
|
Behavior:
|
|
- GET/DELETE: pass through (naturally idempotent)
|
|
- POST/PUT/PATCH without header: pass through (caller opted out)
|
|
- POST/PUT/PATCH with header + key found: return cached response
|
|
- POST/PUT/PATCH with header + key new: execute, cache response
|
|
"""
|
|
|
|
async def dispatch(self, request: Request, call_next) -> Response:
|
|
if request.method not in IDEMPOTENT_METHODS:
|
|
return await call_next(request)
|
|
|
|
key = request.headers.get("idempotency-key")
|
|
if not key:
|
|
return await call_next(request)
|
|
|
|
# Lookup cached response
|
|
try:
|
|
from app.database import async_session
|
|
from app.services.idempotency_service import idempotency_service
|
|
|
|
tenant_id = getattr(request.state, "tenant_id", None) or ""
|
|
|
|
async with async_session() as db:
|
|
cached = await idempotency_service.get_existing(
|
|
db, key=key, tenant_id=str(tenant_id)
|
|
)
|
|
if cached:
|
|
return JSONResponse(
|
|
cached["response"],
|
|
status_code=int(cached["status_code"]),
|
|
headers={"X-Idempotency-Cached": "true"},
|
|
)
|
|
except Exception:
|
|
# If lookup fails, fall through to normal execution
|
|
pass
|
|
|
|
# Execute request
|
|
response = await call_next(request)
|
|
|
|
# Cache response if successful
|
|
try:
|
|
if 200 <= response.status_code < 300:
|
|
from app.database import async_session
|
|
from app.services.idempotency_service import idempotency_service
|
|
|
|
tenant_id = getattr(request.state, "tenant_id", None) or ""
|
|
|
|
# Read response body
|
|
body = b""
|
|
async for chunk in response.body_iterator:
|
|
body += chunk
|
|
|
|
response_data = json.loads(body) if body else {}
|
|
async with async_session() as db:
|
|
try:
|
|
await idempotency_service.store(
|
|
db, key=key, tenant_id=str(tenant_id),
|
|
endpoint=str(request.url.path),
|
|
request_body=None,
|
|
response=response_data,
|
|
status_code=response.status_code,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
return JSONResponse(
|
|
response_data, status_code=response.status_code,
|
|
headers={"X-Idempotency-Stored": "true"},
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
return response
|