Metadata-Version: 2.4
Name: infocion-aegis
Version: 0.6.1
Summary: Aegis governance SDK — tool-call guard, PHI boundary, OTel instrumentation
License-Expression: Apache-2.0
Project-URL: Homepage, https://aegis.infocion.com
Project-URL: Documentation, https://aegis.infocion.com/sdk-guide
Keywords: agent,governance,audit,llm,guardrails,hipaa,phi
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: cryptography<51,>=44
Requires-Dist: opentelemetry-api<2,>=1.42
Requires-Dist: opentelemetry-sdk<2,>=1.42
Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.42
Provides-Extra: client-zone
Requires-Dist: psycopg[binary]<4,>=3.2; extra == "client-zone"
Provides-Extra: freetext
Requires-Dist: presidio-analyzer<3,>=2.2; extra == "freetext"
Provides-Extra: aws
Requires-Dist: botocore<2,>=1.34; extra == "aws"
Provides-Extra: openshell
Requires-Dist: grpcio<2,>=1.70; extra == "openshell"
Requires-Dist: protobuf<7,>=5.29; extra == "openshell"
Provides-Extra: otel
Requires-Dist: opentelemetry-api<2,>=1.42; extra == "otel"
Requires-Dist: opentelemetry-sdk<2,>=1.42; extra == "otel"
Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.42; extra == "otel"
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: psycopg[binary]<4,>=3.2; extra == "dev"
Requires-Dist: langchain-core<2,>=0.3; extra == "dev"
Requires-Dist: langgraph<2,>=0.2; extra == "dev"
Dynamic: license-file

# Infocion Aegis SDK

Governance for AI agents: an allow-listed tool-call guard, brokered short-lived
credentials, PHI redaction for structured fields and clinical notes,
human-in-the-loop approval, and a tamper-evident audit trail. It wraps around
an agent you already have.

```bash
pip install infocion-aegis
```

The distribution is **`infocion-aegis`**; the import is **`aegis_sdk`**.

> **If you meant a different Aegis:** several similarly named packages on PyPI
> are unrelated products with no connection to the Infocion Aegis control
> plane. This one exposes `guard`, `aegis_session` and `context_from_env`, and
> has no `MetricsReporter` and no `AuditLog` — if your code uses either of
> those, it is not this package, which is why nothing reaches your console.

## Quick start

Two environment variables, from your agent's page in the Aegis console:

```bash
export AEGIS_API_BASE=https://your-aegis-host/api/aegis
export AEGIS_AGENT_KEY=agk_...       # Agents → your agent → Enrollment keys
export AEGIS_ENFORCEMENT=development # or 'governed' — see below
```

Then add three lines to the agent. The tool functions stay as they are:

```python
import aegis_sdk

aegis_sdk.init()
tools = aegis_sdk.govern(tools)   # a list, dict, module, or LangChain tools
```

The first run reports each tool's name to the console (Agents → your agent →
Tools). Bind each one to a connector and scope there; the binding reaches the
agent in its signed config, with no code change or restart. An unbound tool is
blocked and logged. A tool that declares an `aegis_cred` keyword argument is
handed a short-lived credential for its connector (hidden from the model's view
of a LangChain tool). When the agent's guardrails require approval, a tool bound
at a non-read scope opens one review request and raises `ApprovalRequired`; the
next call after a reviewer signs proceeds, one approval per action
(`approval_wait=` blocks for the decision instead).

A server handling many jobs calls `init(session=False)` once and wraps each job
in `with aegis_sdk.run():`, so every job gets its own session and token budget.

`init()` makes the context visible to every thread in the process. A process
serving several agents or tenants should use `aegis_session(ctx)` per run instead.

If you would rather keep the binding in code, the explicit form is still supported:

```python
from uuid import uuid4

from aegis_sdk import guard
from aegis_sdk.guard import aegis_session
from aegis_sdk.platform import context_from_env

ctx = context_from_env(session_id=str(uuid4()))


@guard(tool_name="query_trials", connector="clinicaltrials-gov", scope="read")
def query_trials(condition: str, *, aegis_cred=None) -> list[dict]:
    # If this connector is not bound to this agent, the call is BLOCKED and
    # logged — not silently dropped, and not allowed through with a warning.
    return ctms.search(condition=condition, token=aegis_cred.secret)


with aegis_session(ctx):
    results = query_trials("type 2 diabetes")
```

Your agent's graph, prompts and state are untouched. LangGraph, LangChain,
CrewAI or plain Python all work the same way.

## What it does on every guarded call

1. **Allow-list** — is this connector registered and bound to this agent?
2. **Autonomy tier** — may this agent execute at all, or only observe?
3. **Policy** — does the decision point permit this action at this scope?
4. **Credential** — a short-lived, task-scoped token issued per call.
5. **Audit** — allowed or blocked, a hash-chained record is written either way.

A blocked call raises. It never returns `None`, because an agent that treats a
governance refusal as an empty result will carry on reasoning from it.

## Development vs governed

`pip install` alone does not make an agent contained. On a laptop or ordinary
VPS the process can reach a model provider directly — `import openai` bypasses
any decorator — so that mode is `development` and the console shows the agent
as `UNATTESTED`. It is fully registered, policy-checked and audited; it is not
*contained*.

A **governed** deployment runs the agent inside a network whose egress is
restricted, so the only route out is the gateway. The SDK verifies this at
startup and refuses to run if it is false.

Use development mode to build and test. Do not put real PHI through it.

## Inside an NVIDIA OpenShell sandbox

Where the host allows it, the agent can run inside an
[NVIDIA OpenShell](https://github.com/NVIDIA/OpenShell) sandbox: Landlock limits
its files, and every connection goes through a supervisor that allows only the
agent's bound connectors. Aegis builds and signs that policy, inspects outgoing
request bodies for PHI, and refuses any attempt to widen it. Turn on
**Require OpenShell sandbox** on the agent's OpenShell tab in the console, then:

```bash
aegis-openshell qualify          # can this host's Docker kernel run sandboxes?
aegis-openshell up --image <agent image> --port 8001 -- <the agent's command>
aegis-openshell logs             # every allowed and denied connection
aegis-openshell logs --agent     # the agent's own output (stays on the host)
aegis-openshell status           # services, forwarded ports, certificate expiry
aegis-openshell down
```

It needs Docker, plus `AEGIS_API_BASE`, `AEGIS_AGENT_KEY` and
`AEGIS_POLICY_BUNDLE_PUBLIC_KEY` (shown on the OpenShell tab). Keys and
certificates are generated on the host and stay there. The agent itself never
holds its Aegis key: it receives an OpenShell placeholder, swapped for the real
key only on requests to Aegis, which needs this package (0.6.1 or later) in the
agent's image. While the requirement is on, `aegis_sdk.init()` raises
`SandboxRequired` outside a sandbox.

Flags worth knowing: `--port HOSTPORT:PORT`, `--bind ADDRESS` (loopback by
default; `0.0.0.0` also needs `--allow-network-exposure`), `--env NAME`, and
`--vault-address` / `--vault-token-file` / `--vault-ca` to keep provider
credentials in the hospital's own vault. One governed agent per host.

## Tracing

Spans go to a collector **you** run, in your own environment. Aegis's cloud has
no span ingest and will not be given one — a span is a PHI surface, because the
search criteria in a database-query span *are* the patient criteria.

Tracing is off until you turn it on. A process that never calls
`configure_tracing()` gets OpenTelemetry's no-op tracer and records nothing:
nothing emitted, nothing buffered, nothing dropped.

```python
from aegis_sdk import configure_tracing, flush_tracing

configure_tracing(service_name="my-agent")   # once, at startup
...
flush_tracing()                              # before the process exits
```

`AEGIS_OTLP_ENDPOINT` defaults to `http://phoenix:6006`, which is a
**client-zone Docker hostname** — it resolves inside the client-zone bundle and
nowhere else. To trace during development, run your own collector and point the
SDK at it:

```bash
docker run -d --name phoenix -p 6006:6006 arizephoenix/phoenix:latest
export AEGIS_OTLP_ENDPOINT=http://localhost:6006
```

Skipping that is fine; the exporter will simply warn on every export that the
host is unreachable. Leaving tracing off costs nothing in the console —
sessions, token counts, redaction tallies and guard decisions travel over your
agent key independently of OpenTelemetry.

## Extras

| Install | When |
|---|---|
| `infocion-aegis` | An agent anywhere. Reports to the control plane over its enrollment key. |
| `infocion-aegis[client-zone]` | Inside a governed client zone: adds the PostgreSQL driver for local audit detail and session state. |
| `infocion-aegis[freetext]` | Agents that send notes or comments to a cloud model. Adds the NER pass; also run `python -m spacy download en_core_web_sm` (or `en_core_web_lg` for better recall). Governed mode refuses note fields without it. |
| `infocion-aegis[openshell]` | Only inside the extension image `aegis-openshell up` builds (gRPC for the OpenShell extensions). The host command itself needs just the base package. |
| `infocion-aegis[aws]` | Reference-tier secrets in a vault that trusts AWS IAM (`aws_iam` auth). Set `AEGIS_VAULT_AWS_STS_REGION` if your vault uses a regional STS endpoint. |

## Clinical notes

Narrative fields (`note`, `clinical_note`, `comment`, `narrative`, SDTM `COVAL`
and any you add per agent) are scrubbed before they leave your environment:
Safe Harbor dates reduced to the year, ages over 89, phones, emails, record
numbers, addresses, ZIP codes, employers, names after titles and relations,
NER-detected names and places, and every identifier already present in the
record. Clinical vocabulary is kept: gene symbols (BRCA1), eponyms (Hodgkin
lymphoma, Ann Arbor stage) and drug brands.

This is redaction, not a certified de-identification. NER recall is below 100%;
a relative named by first name alone with no title or relation can be missed.
For note-heavy workloads prefer a model inside your own environment.

## Documentation

Full integration guide, including the audit action vocabulary, custody tiers
and failure modes, is downloadable from the **SDK & Integration** page of your
Aegis console.
