Metadata-Version: 2.4
Name: kerneva-runtime-trust
Version: 0.7.0
Summary: Runtime authorization for AI agents that move money: ALLOW / REVIEW / BLOCK before execution
License-Expression: MIT
Project-URL: Homepage, https://www.kerneva.com
Project-URL: Documentation, https://www.kerneva.com/docs/quickstart
Project-URL: API Reference, https://www.kerneva.com/docs/api-reference
Project-URL: Integration Guide, https://www.kerneva.com/docs/integration-guide
Project-URL: Evaluation Kit, https://www.kerneva.com/docs/deployment
Project-URL: Source, https://github.com/kerneva
Keywords: ai-agents,ai-safety,guardrails,authorization,financial-agents,fintech,compliance,llm
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Kerneva Runtime Trust

Runtime authorization for AI agents that move money. Every refund, payout, or approval gets an ALLOW / REVIEW / BLOCK decision before execution — and a versioned receipt after.

```python
from kerneva_runtime_trust import guard, RuntimeTrustBlock

@guard(action_type="refund")
def process_refund(amount, customer_id):
    return billing_api.refund(customer_id, amount)

try:
    process_refund(amount=150.0, customer_id="cust_123")
except RuntimeTrustBlock as e:
    print(f"Blocked: {e}")
```

`guard` (also available as `with_runtime_trust`) evaluates every call through
Kerneva's `/evaluate` before the function body runs. On `BLOCK` the function
never executes and `RuntimeTrustBlock` is raised; on `REVIEW` (held for a
person) it never executes and `RuntimeTrustHeld`, a `RuntimeTrustBlock`
subclass, is raised — see [Handling REVIEW](#handling-review).

## Install

```bash
pip install kerneva-runtime-trust
```

## Setup

```bash
export KERNEVA_API_KEY="krv_test_abc123"
export KERNEVA_API_URL="https://api.kerneva.com"   # or http://localhost:8080
```

## Getting good signals

Kerneva reasons over an agent's *trajectory* — within a session and across your
end-customers. A single function call doesn't carry that context, so declare it.
The two that matter most:

- **`action_type`** — match a configured threshold (e.g. `"refund"`). If it has
  no threshold the call is observation-only; the SDK logs a warning so you know.
- **`customer_id`** — the end-customer, so one customer's trajectory doesn't
  bleed into another's.

Set them per-call, or ambiently for a whole interaction with `session(...)`:

```python
import kerneva_runtime_trust as kerneva

with kerneva.session(session_id=ticket_id, agent_id="refund-bot",
                     customer_id="cust_123"):
    process_refund(amount=40.0, customer_id="cust_123")
    process_refund(amount=75.0, customer_id="cust_123")   # shares one trajectory
```

Without a session, each call gets its own session id (so unrelated calls are
never merged) and the SDK warns once that session-scoped analysis is off.

## Handling REVIEW

A `REVIEW` means Kerneva is holding the action for a person to approve (for
example the new-payee hold, or an agent not on the allowlist). By default
`guard` does **not** run the function: it raises `RuntimeTrustHeld`, a subclass
of `RuntimeTrustBlock` carrying the `evaluation_id`, so existing
`except RuntimeTrustBlock` handlers still stop the action.

```python
from kerneva_runtime_trust import RuntimeTrustBlock, RuntimeTrustHeld

try:
    pay_invoice(amount=1200.0, counterparty="acme-ltd")
except RuntimeTrustHeld as held:          # catch before RuntimeTrustBlock
    queue_for_approval(held.evaluation_id, held.reason)
except RuntimeTrustBlock as blocked:
    refuse(blocked)
```

`guard` reports no outcome for a held action, so the evaluation stays open.
Once a person releases it in the dashboard's review queue, run the action and
report it: `KernevaClient().confirm_execution(evaluation_id, "completed")`.
(The API refuses `completed` for a held action until its review is resolved.)

To decide yourself, pass an `on_review` hook. It receives a `ReviewContext`;
raise to halt, return to run the function anyway:

```python
def hold_for_approval(ctx):
    raise NeedsApproval(ctx.evaluation_id, ctx.reason)   # stops the wrapped function

@guard(action_type="refund", on_review=hold_for_approval)
def process_refund(amount, customer_id): ...
```

`hold_on_review=False` restores the behaviour of 0.6.0 and earlier: a Python
warning, then the function runs. An action run without a release (this way or
via a hook that returns) is reported `failed` if it raises and otherwise not at
all — never `completed`.

In Observation Mode the enforced decision is always `ALLOW`, so nothing is held;
a would-be hold shows up only as `recommended_decision`.

## Reliability

- **Fail-closed by default:** if the API is unreachable the action does not run.
  Pass `fail_open=True` for non-critical workflows.
- **`strict=True`** turns integration warnings (misconfiguration) into a raised
  `KernevaConfigError` — useful in development/CI to catch a broken integration.
- **Async:** decorating an `async def` returns an async wrapper.

## Low-level client

```python
from kerneva_runtime_trust import KernevaClient

client = KernevaClient(api_key="krv_test_abc123")
result = client.evaluate(
    agent_id="refund-bot", session_id="ticket-8842",
    action_type="refund", amount=82.0, customer_id="cust_123",
)
# In Observation Mode nothing is blocked; recommended_decision is the shadow
# verdict — what enforcement WOULD do.
print(result.decision, result.recommended_decision, result.warnings)
```

Full documentation: https://www.kerneva.com/docs — quickstart, integration guide, API reference, and the self-hosted evaluation kit.
