3tears-agent-audit
0.89.0
0.88.0
0.86.0
0.85.0
0.84.0
0.83.0
0.82.0
0.81.0
0.80.0
0.79.0
0.78.0
0.77.0
0.76.0
0.75.0
0.74.0
0.73.0
0.72.0
0.71.0
0.70.1
0.70.0
0.69.0
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.1
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.0
0.57.0
0.56.3
0.56.2
0.56.1
0.56.0
0.55.0
0.54.0
0.53.0
0.52.1
0.52.0
0.51.1
0.51.0
0.50.0
0.49.0
0.48.0
0.47.1
0.47.0
0.46.1
0.46.0
0.45.1
0.45.0
0.44.0
0.43.0
0.42.0
0.41.4
0.41.3
0.41.2
0.41.1
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.1
0.35.0
0.34.0
0.33.0
0.32.1
0.32.0
0.31.0
0.30.1
0.30.0
0.29.0
0.28.0
0.27.0
0.26.1
0.26.0
0.25.0
0.24.7
0.24.6
0.24.5
0.24.4
0.24.3
0.24.2
0.24.1
0.24.0
0.23.9
0.23.8
0.23.7
0.23.6
0.23.5
0.23.3
0.23.2
0.23.11
0.23.10
0.23.1
0.23.0
0.22.5
0.22.4
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
0.20.0
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.0
0.17.9
0.17.8
0.17.7
0.17.6
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.1
0.16.0
0.15.0
0.14.1
0.14.0
0.89.0
0.88.0
0.86.0
0.85.0
0.84.0
0.83.0
0.82.0
0.81.0
0.80.0
0.79.0
0.78.0
0.77.0
0.76.0
0.75.0
0.74.0
0.73.0
0.72.0
0.71.0
0.70.1
0.70.0
0.69.0
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.1
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.0
0.57.0
0.56.3
0.56.2
0.56.1
0.56.0
0.55.0
0.54.0
0.53.0
0.52.1
0.52.0
0.51.1
0.51.0
0.50.0
0.49.0
0.48.0
0.47.1
0.47.0
0.46.1
0.46.0
0.45.1
0.45.0
0.44.0
0.43.0
0.42.0
0.41.4
0.41.3
0.41.2
0.41.1
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.1
0.35.0
0.34.0
0.33.0
0.32.1
0.32.0
0.31.0
0.30.1
0.30.0
0.29.0
0.28.0
0.27.0
0.26.1
0.26.0
0.25.0
0.24.7
0.24.6
0.24.5
0.24.4
0.24.3
0.24.2
0.24.1
0.24.0
0.23.9
0.23.8
0.23.7
0.23.6
0.23.5
0.23.3
0.23.2
0.23.11
0.23.10
0.23.1
0.23.0
0.22.5
0.22.4
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
0.20.0
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.0
0.17.9
0.17.8
0.17.7
0.17.6
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.1
0.16.0
0.15.0
0.14.1
0.14.0
توضیحات
unified audit envelope, fire-and-forget publish helper, and the audit erasure rule and hub anonymization request for the 3tears platform
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | 3tears-agent-audit-0.89.0 |
| نام | 3tears-agent-audit |
| نسخه کتابخانه | 0.89.0 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | pace |
| ایمیل نویسنده | - |
| آدرس صفحه اصلی | - |
| آدرس اینترنتی | https://pypi.org/project/3tears-agent-audit/ |
| مجوز | - |
# 3tears-agent-audit
Unified audit envelope, fire-and-forget publish helper, and the erasure rule and
hub request for anonymizing audit records, for the 3tears platform.
## Purpose
Single `AuditEvent` envelope + single `publish_audit` helper used by every
domain (workspace, rbac, memory, custom tools) so the audit pipeline is
one subject tree, one consumer, one table, one admin query API. Replaces
domain-specific envelopes (`WorkspaceAuditEnvelope`,
`RbacAuditEnvelope`) that produced slightly-different wire shapes per domain
and made cross-domain audit queries require a UNION.
Publishing is the package's core: `AuditEvent` and `publish_audit` import nothing
beyond the NATS client. On a hub deployment the HUB persists every event into its
platform table, and the package's persistence code is never used; an agent there
erases a person from that table by asking the hub (`request_audit_anonymization`),
and the hub applies the erasure rule.
A deployment with NO hub (one application owning its own control plane) owns its
audit table, and `threetears.agent.audit.persist` is its persister:
- `ensure_audit_events_table(db)` creates the table, with every envelope field and
`ip_address`, and migrates an existing one: every column the insert names beyond
`id` and the four required fields (`timestamp`, `event_type`, `action`,
`correlation_id`) is added if missing, with the default the CREATE gives it.
Idempotency is on the envelope `id`.
- `start_audit_persister(nats, db, durable=..., storage="memory")` runs a shared
durable pull consumer with a dead-letter subject. A malformed event is dropped;
a database fault is retried, then dead-lettered.
- `storage` must match every other declarer of the `audit` stream.
- `durable` must be unique per table.
- `prune_audit_events(db, older_than=...)` is batched retention.
- `anonymize_audit_rows(db, actor_user_ids=...)` is erasure: every row and id is
kept, and `details` and `ip_address` are rewritten by the platform's rule and no
other -- it takes no replacement anonymizer. A family whose keys are safe in this
deployment declares them with `declare_safe_detail_keys`. It returns the same
`AuditAnonymization` the hub path does, and never answers the hub's anonymize
subject.
`db` is anything with asyncpg's `execute`/`fetch`, e.g. a pool.
## Public API
```python
from threetears.agent.audit import AuditEvent, publish_audit
```
- `AuditEvent` -- pydantic `BaseModel` with `extra='forbid'`, timezone-aware
`timestamp` validator, closed `event_type` string family (dotted verb,
e.g. `workspace.fs_write`, `rbac.assignment.create`). All common identity
fields are typed columns on the envelope; event-type-specific extras live
in `details: dict[str, Any]`.
- `publish_audit(event, nats_client, namespace)` -- fire-and-forget async
helper. Serializes the envelope via `model_dump_json()` and awaits one
`nats_client.publish` on `{namespace}.audit.{event_type}`. On any publish
failure logs at WARN and returns; never raises.
## Erasure: anonymize, never delete
```python
from threetears.agent.audit import anonymize_details, anonymize_ip
row.details = anonymize_details(row.details, event_type=row.event_type)
row.ip_address = anonymize_ip(row.ip_address)
```
An audit record is never deleted by erasure and no id on it changes. Every
erasure path (a GDPR request, a principal anonymization, a customer
offboarding) routes the record's content through this one rule instead of
writing its own:
- `anonymize_details(details, *, event_type)` keeps every key of `details`,
keeps the value under a safe key (a dict inside it is judged key by key),
and replaces the WHOLE value under any other key with `ANONYMIZED_MARKER`
(`"[anonymized]"`) -- a subtree and the keys a user chose inside it alike.
`None` stays `None`. Pure and idempotent.
- `anonymize_ip(value) -> str | None` returns `None`: an address is removed, not
truncated. It is typed as the column, so its result assigns back without an ignore.
- `SAFE_DETAIL_KEYS` is the explicit safe list. A key not on it is masked, so
a field nobody classified fails safe. `PERSONAL_DETAIL_KEYS` records the keys
classified as able to carry personal data.
- `declare_safe_detail_keys(event_type_prefix, keys)` widens the safe set for
one event family, and `safe_detail_keys_for(event_type)` is the single
lookup. A declaration is visible only in the process that makes it, so a
family whose events the hub erases from the platform audit table is declared
in `threetears/agent/audit/anonymize.py` itself.
- `is_classified_detail_key(key, *, event_type)` answers whether a key was
classified. The gate that holds producers to the classification judges by this
same function, injected because the gate cannot import this package: the
`threetears.enforcement.audit_details` domain, which every producing repo runs
over its own `src/` with `safe_keys_for=safe_detail_keys_for,
is_classified=is_classified_detail_key`, and 3tears runs it as
`tests/enforcement/test_audit_details_keys_are_classified.py`.
## An agent erasing a person: the hub's copy of its audit rows
```python
from threetears.agent.audit import request_audit_anonymization
result = await request_audit_anonymization(
nats_client,
identity_token=current_identity_token(),
agent_id=my_agent_id,
actor_user_ids=[respondent_admission_user_id],
)
# result.rows_matched, result.rows_changed
```
The hub holds the audit rows an agent's events became. This asks it to anonymize the
ones that agent published about those actors, on `{ns}.hub.audit.anonymize`
(`Subjects.hub_audit_anonymize()`), with the same rule as above: rows kept, ids kept,
`details` and `ip_address` anonymized. The hub takes the agent from the verified identity
token and touches only rows whose agent is the caller. A refusal (`INVALID_REQUEST`,
`IDENTITY_REFUSED`, `AGENT_MISMATCH`) raises `AuditAnonymizeRefusedError` with the
hub's `error_code`, and retrying meets it again -- including a refusal that carries no
correlation id, since a hub that could not decode the body had none to echo. No token, a
timeout, a reply that does not decode or carries another request's correlation id, and
the hub's `ANONYMIZE_FAILED` raise `AuditAnonymizeUnavailableError`, which is safe to
retry. The contract, including every
obligation of the hub's responder, is the docstring of `threetears/agent/audit/erasure.py`.
## Design commitments
- **Fire-and-forget.** Audit publish failures must never break the producing
call. The helper catches every exception, logs at WARN, and returns.
- **Typed wire contract.** `extra='forbid'` + timezone-aware validator catch
publisher-side drift at construction time, not at the consumer's decode.
- **No domain-specific envelope types.** Every domain publishes the same
model; `event_type` conveys the domain.
- **No dual-emit.** There is no legacy envelope the consumer still accepts in
parallel. Emission sites migrate in the same PR that deletes the legacy
envelope module.
## Subject naming
`{namespace}.audit.{event_type}` where `event_type` is the dotted event
name (e.g. `{namespace}.audit.workspace.fs_write`). The consumer
subscribes to `{namespace}.audit.>` so new event types route automatically
without consumer-side changes.
نیازمندی
| مقدار | نام |
|---|---|
| - | 3tears-nats[client]<0.90.0,>=0.89.0 |
| - | 3tears-observe<0.90.0,>=0.89.0 |
| - | 3tears[security]<0.90.0,>=0.89.0 |
| - | pydantic>=2.0 |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.14 | Python |
نحوه نصب
نصب پکیج whl 3tears-agent-audit-0.89.0:
pip install 3tears-agent-audit.whl
نصب پکیج tar.gz 3tears-agent-audit-0.89.0:
pip install 3tears-agent-audit.tar.gz