Metadata-Version: 2.4
Name: nyxa-audit
Version: 0.0.1
Summary: Append-only audit log for Nyxa — auth, grants, denials, membership
Author-email: Al-Amin Islam Nerob <alamin@aincoder.com>
License-Expression: MIT
Project-URL: Repository, https://github.com/NyxaDev/Nyxa
Requires-Python: >=3.13
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: nyxa>=0.0.1
Dynamic: license-file

# nyxa-audit

Append-only compliance audit log for Nyxa — immutable `AuditRecord`
rows for auth events, grants, denials, and membership changes, with a
retention window and JSON export. No update/delete API exists on
records by design.

## Features

- **`app.audit` service** — an `AuditLogger` bound by `AuditProvider`;
  writes into an `audit_logs` table (`id`, `action`, `actor`,
  `resource`, `meta`, `created_at`).
- **Auto-migration** — the provider creates `audit_logs` at boot when
  missing (sqlite-friendly schema via `app.db.schema()`).
- **Immutable records** — `record()` appends and returns a frozen
  `AuditRecord` dataclass; nothing can mutate or delete rows through
  the API.
- **Retention + export** — `export(since=…)` reads records newer than
  `audit.retention_days` (default 90) as immutable records ordered by
  id — ready for JSON-lines compliance tooling.
- **Plugin auto-discovery** — registered via the `nyxa.plugins` entry
  point; no manual provider wiring.

## Install

```bash
nyxa add nyxa-audit
```

Requires a configured database connection — the logger writes through
`app.db`.

## Usage

```python
record = await app.audit.record(
    "gate.denied",
    actor="user:2",
    resource="post:9",
    meta={"action": "edit"},
)

records = await app.audit.export()  # last 90 days
records = await app.audit.export(since=yesterday)  # explicit window
```

## Public API

- **`AuditLogger`** (`nyxa_audit.logger`, re-exported at package
  level) — `AuditLogger(app)`; `await logger.record(action, *,
  actor="system", resource=None, meta=None) -> AuditRecord`,
  `await logger.export(*, since=None) -> Sequence[AuditRecord]`,
  `logger.retention_days -> int`.
- **`AuditRecord`** (`nyxa_audit.logger`) — frozen dataclass:
  `id`, `action`, `actor`, `resource`, `meta`, `created_at` (UTC).
- **`AuditProvider`** (`nyxa_audit.provider`) — binds `app.audit`,
  contributes the `audit` config namespace, and creates `audit_logs`
  on boot.

## Configuration

`config/audit.py`:

```python
config = {
    "retention_days": 90,  # export window lower bound, in days
}
```

## Testing

Use a sqlite-backed test app — the provider creates the table on boot:

```python
audit = app.audit
await audit.record("auth.signed_in", actor="user:1")
exported = await audit.export()
assert exported[-1].action == "auth.signed_in"
```

## License

MIT
