Metadata-Version: 2.5
Name: nodii-usage
Version: 0.2.0
Summary: Nodii usage library — fail-closed STOP check over a local projection, the meter and the one usage-metered payload (polyglot; TS @nodii/usage is the contract).
Project-URL: Homepage, https://github.com/cognion-nucleus/nodii-libs
Project-URL: Repository, https://github.com/cognion-nucleus/nodii-libs
Author-email: Nodii <ops@nodii.co>
License: MIT
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.11
Requires-Dist: nodii-db-rls<1.0.0,>=0.11.0
Requires-Dist: nodii-notify<1.0.0,>=0.1.0
Requires-Dist: nodii-outbox-dispatcher<1.0.0,>=0.7.0
Requires-Dist: nodii-replica-consumer<1.0.0,>=0.40.0
Description-Content-Type: text/markdown

# nodii-usage

Python port of `@nodii/usage` (S08-39, LIB-EVT-22): the STOP check, the meter and
the one usage-metered payload. A producer never holds a price and never names a
payer.

```python
from nodii_usage import OtherUnit, UsageConfig, define_usage

usage = define_usage(
    UsageConfig(
        source_service="nodii-geo-service",
        topic_prefix="geo",
        outbox_table="geo_outbox",
        pool=pool,  # asyncpg.Pool
        slice_keys=("geo.geocode_lookup.google",),
        projection_ready=lambda: snapshot_drained,
    )
)

decision = await usage.check_stop(tenant_id, "geo.geocode_lookup.google")
if decision.status != "allowed":
    ...  # `unavailable` is a refusal too: retry later, or suppress a broadcast

async with conn.transaction():
    await usage.meter(conn, OtherUnit(...))
```

- `check_stop` fails closed: any failure returns `status="unavailable"`, it never
  raises.
- `apply_usage_denial` is the one write path. SetUsageControl, the event and the
  snapshot all go through it, and the highest source version wins.
- `define_usage_denial_replica` gives the replica spec; run
  `get_usage_migration_sql()` for the projection and marker tables.

`keys_gen.py` is generated; never edit it.

```bash
uv run pytest python/usage/tests/
```

## Known limit: widening the slice

The projection stores only the denials inside the service's slice, and one version of a tenant's denials is applied once. A service that adds a key to its slice therefore reads that key as allowed, for a tenant already denied on it, until tenant-service emits that tenant's next version. A redelivery or a snapshot of the same version does not rewrite the row. The fix is an open question on STANDARDIZATIONS-AUDIT-19.
