-
Notifications
You must be signed in to change notification settings - Fork 0
docs: add CLAUDE.md with codebase guidance for AI assistants #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lexicoding-systems
wants to merge
1
commit into
main
Choose a base branch
from
claude/add-claude-documentation-Stw1V
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+144
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # CLAUDE.md | ||
|
|
||
| This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
|
||
| ## Project Overview | ||
|
|
||
| Lexecon is a **cryptographic governance engine** for AI safety and regulatory compliance. It provides deterministic (no-LLM) policy evaluation, tamper-evident audit logging, and multi-framework compliance automation (SOC 2, ISO 27001, GDPR, HIPAA, PCI-DSS, NIST CSF, EU AI Act). | ||
|
|
||
| ## Commands | ||
|
|
||
| ### Backend | ||
|
|
||
| ```bash | ||
| pip install -e ".[dev]" # Install with dev dependencies | ||
|
|
||
| make test # Run pytest | ||
| make lint # flake8 + mypy | ||
| make format # black + isort | ||
| make clean # Clean build artifacts | ||
|
|
||
| python3 -m pytest tests/ -q # All tests (quiet) | ||
| python3 -m pytest tests/test_decision_service.py -v # Single test file | ||
| python3 -m pytest tests/ --cov=lexecon --cov-report=term-missing # With coverage | ||
|
|
||
| pre-commit install # Install git hooks | ||
| pre-commit run --all-files # Run all hooks manually | ||
| ``` | ||
|
|
||
| ### Frontend (React) | ||
|
|
||
| ```bash | ||
| cd frontend | ||
| npm start # Dev server | ||
| npm run build # Production build | ||
| npm test # Run tests | ||
| ``` | ||
|
|
||
| ### Run API Server | ||
|
|
||
| ```bash | ||
| lexecon server # via CLI entry point | ||
| # or | ||
| uvicorn src.lexecon.api.server:app --reload | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| ### Service Registry (Dependency Injection) | ||
|
|
||
| All services are instantiated once in `src/lexecon/api/dependencies.py` via a `ServiceRegistry` singleton. FastAPI route handlers receive services through `Depends()`. Never instantiate services directly in routes — always go through the registry. | ||
|
|
||
| ### Decision Workflow | ||
|
|
||
| Every governance decision follows this pipeline: | ||
|
|
||
| ``` | ||
| HTTP Request → Validation → Rate Limit → PolicyEngine → RiskService → LedgerChain → CapabilityToken → Response | ||
| ``` | ||
|
|
||
| 1. **PolicyEngine** (`src/lexecon/policy/`) — evaluates policies using terms (actor/action/resource) and relations (permits/forbids/requires). Three modes: `strict` (deny by default), `permissive` (allow unless forbidden), `paranoid` (require human approval for high risk). Deterministic, <10ms. | ||
|
|
||
| 2. **RiskService** (`src/lexecon/risk/`) — scores 6 dimensions (Security, Privacy, Compliance, Operational, Reputational, Financial) 0–100. Auto-escalates if composite score ≥ 80. | ||
|
|
||
| 3. **LedgerChain** (`src/lexecon/ledger/`) — SHA-256 hash-chained tamper-evident log. Every decision is appended; genesis entry anchors the chain. Do not mutate ledger entries. | ||
|
|
||
| 4. **CapabilityToken** (`src/lexecon/capability/`) — time-limited Ed25519-signed authorization token issued after a permit decision. | ||
|
|
||
| 5. **EvidenceService** (`src/lexecon/evidence/`) — registers immutable artifacts (8 types) associated with decisions. | ||
|
|
||
| ### Security Layer | ||
|
|
||
| - **AuthService** (`src/lexecon/security/auth_service.py`) — RBAC with 4 roles: `viewer`, `operator`, `admin`, `executive`. | ||
| - **AsyncAuthService** — async variant; prefer this in async FastAPI routes. | ||
| - **OIDCService** (`src/lexecon/security/oidc_service.py`) — Google, Azure, Okta OAuth flows. | ||
| - **SignatureService** (`src/lexecon/security/signature_service.py`) — Ed25519 (default) and RSA-4096 signing. | ||
| - **Rate limiting** — configured per-IP, per-user, per-endpoint via `src/lexecon/security/rate_limit_middleware.py`; Redis-backed in production, in-memory fallback. | ||
|
|
||
| ### API Structure | ||
|
|
||
| Routes live in `src/lexecon/api/routers/`. Each router maps to a domain: | ||
|
|
||
| | Router file | Prefix | | ||
| |---|---| | ||
| | `decisions.py` | `/decide` | | ||
| | `policy.py` | `/policies` | | ||
| | `auth.py` | `/auth/*` | | ||
| | `compliance.py` | `/compliance/*` | | ||
| | `eu_compliance.py` | `/eu-ai-act/*` | | ||
| | `compliance_mapping.py` | `/compliance/mapping/*` | | ||
| | `responsibility.py` | `/responsibility/*` | | ||
| | `audit.py` | `/audit-export/*` | | ||
| | `governance.py` | `/api/governance/*` | | ||
|
|
||
| ### Database | ||
|
|
||
| `src/lexecon/db/async_database.py` manages async SQLAlchemy sessions. Database URL resolution order: | ||
| 1. `LEXECON_DATABASE_URL` | ||
| 2. `DATABASE_URL` (Railway/Heroku) | ||
| 3. PostgreSQL default (`postgresql+asyncpg://lexecon:lexecon@localhost/lexecon`) | ||
| 4. SQLite fallback (`sqlite+aiosqlite:///lexecon.db`) | ||
|
|
||
| Tables are created automatically at startup via `Base.metadata.create_all()`. Migration scripts are in `migrations/` (no Alembic; manual approach). | ||
|
|
||
| ### Frontend | ||
|
|
||
| React 18 SPA in `frontend/src/`. The FastAPI server serves the built SPA from `/static` when `LEXECON_ENV=production`. Key frontend directories: | ||
|
|
||
| - `api/` — Axios-based API client | ||
| - `contexts/` — React context providers (auth, tenant) | ||
| - `pages/` — Route-level page components | ||
| - `design-system/` — Shared UI primitives | ||
|
|
||
| ## Key Conventions | ||
|
|
||
| ### Policy Mode | ||
|
|
||
| Set via `LEXECON_POLICY_MODE` env var (`strict` | `permissive` | `paranoid`). `strict` is the default and denies any action not explicitly permitted. | ||
|
|
||
| ### Cryptographic Keys | ||
|
|
||
| `LEXECON_MASTER_KEY` (64-char hex) is the root secret. Never hardcode keys. `KeyManager` in `src/lexecon/identity/` manages derived Ed25519 key pairs. | ||
|
|
||
| ### Compliance Frameworks | ||
|
|
||
| `ComplianceMappingService` (`src/lexecon/compliance/`) maps governance decisions to framework controls automatically. When adding new policy types, check whether they need a mapping entry. | ||
|
|
||
| ### EU AI Act | ||
|
|
||
| `src/lexecon/compliance/eu_ai_act/` handles Articles 11 (technical documentation), 12 (logging), and 14 (human oversight) automation separately from the general compliance module. | ||
|
|
||
| ### Test Coverage | ||
|
|
||
| CI enforces an **82% coverage threshold**. Run `python3 -m pytest tests/ --cov=lexecon --cov-report=term-missing` to check locally before pushing. Shared fixtures are in `tests/conftest.py`. Integration tests are under `tests/integration/`; load tests under `tests/load/` and `tests/k6/`. | ||
|
|
||
| ### Observability | ||
|
|
||
| `src/lexecon/observability/` wraps Prometheus metrics, OpenTelemetry tracing, health checks, and Sentry. Feature flags (`FEATURE_FLAG_TRACING_ENABLED`, `FEATURE_FLAG_METRICS_DETAILED`) control which signals are active. Local Docker Compose (`docker-compose.yml`) starts Prometheus and Grafana alongside the API. | ||
|
|
||
| ### Linting Rules | ||
|
|
||
| - **Line length:** 120 chars (ruff/flake8), 100 chars (black/isort) | ||
| - **mypy:** strict mode, Python 3.9 target, all untyped defs disallowed | ||
| - **docstrings:** interrogate enforces 80% coverage threshold | ||
| - **Commits:** conventional commit format enforced by pre-commit hook | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The security guidance lists roles as
viewer,operator,admin, andexecutive, butRoleinsrc/lexecon/security/auth_service.pydefinesviewer,auditor,compliance_officer, andadmin(see enum values around lines 20–25). Because this file is intended to guide AI-generated changes, documenting non-existent roles will lead to invalid role assignments and authorization checks that fail at runtime when code referencesoperator/executive.Useful? React with 👍 / 👎.