Metadata-Version: 2.5
Name: maigp-agent-core
Version: 1.2.0
Summary: MAIGP Agent Governance Core — shared lifecycle for all framework adapters (Mediated AI Governance Protocol)
Requires-Python: >=3.11
Requires-Dist: maigp-client>=5.3.0
Provides-Extra: agency
Requires-Dist: aiap-client<2,>=1.1.0; extra == 'agency'
Provides-Extra: agency-aws
Requires-Dist: aiap-client[aws]<2,>=1.1.0; extra == 'agency-aws'
Provides-Extra: evidence
Requires-Dist: amigo-bedrock>=0.2.0; extra == 'evidence'
Description-Content-Type: text/markdown

# maigp-agent-core

Shared agent-governance lifecycle for every MAIGP framework adapter.

Part of **MAIGP** — the Mediated AI Governance Protocol: a runtime consent / scope / evidence layer for AI systems. Every governed call is **CHECK**ed before it runs (policy, scope, jurisdiction, circuit-breaker) and **RECORD**ed + **TRACE**d as tamper-evident evidence after.

## What it governs

Governs the AIGP agent loop itself — the primitive that all framework adapters build on.

## Governance model

This adapter maps the framework's lifecycle onto the AIGP agent-governance loop provided by [`maigp-agent-core`](https://pypi.org/project/maigp-agent-core/):

| Framework event | AIGP action |
|---|---|
| run / invocation start | **CHECK** (`pre_invoke`) — allowed? under what authority? |
| each model reply | token accounting (`on_model_call`) |
| each tool / function call | tool governance (`on_tool_call`) |
| run complete | **RECORD** + **TRACE** (`post_invoke`) |
| failure | `on_error` (recorded, then re-raised) |


## Install

```bash
pip install maigp-agent-core
```

## Quick start

```python
from aigp_agent_core import AgentGovernance

gov = AgentGovernance(gov_url="https://gov.example.com",
                      app_id="my-agent", hmac_secret="...")

# Start of an agent run
gov.pre_invoke(agent_name="analyst", model_id="claude-opus-4", user_id="u1")

# Agentic control points (RFC-agentic): gate tools, plans, escalations
decision = await gov.on_tool_call("search", params={"q": "..."})
await gov.plan_submit(steps=[{"tool": "search"}, {"tool": "write"}])
await gov.escalate(reason="budget exceeded", action={"type": "pause"})

# Token accounting + completion
gov.on_model_call(input_tokens=500, output_tokens=200)
await gov.step_complete(step_number=1, status="SUCCESS")
await gov.post_invoke(status="SUCCESS")
```

## AIAP agency gate (opt-in, 1.2.0)

AIGP CHECK decides *what* may run; an AIAP agency grant (RFC-001) decides *who* may act, with what
authority. The gate is **off by default**. When a deployment passes an `AgencyGate`, `pre_invoke`
obtains a grant from the Agency Broker after an allowing CHECK, and the agent acts with the grant's
credentials instead of its ambient ones.

```bash
pip install 'maigp-agent-core[agency]'        # [agency-aws] adds boto3 for agency_session()
```

```python
from aigp_agent_core import AgentGovernance, AgencyGate, AgencyScope

gate = AgencyGate(
    broker_url="https://broker.example.com",
    agent_id="MY_AGENT",
    token_provider=current_agent_token,          # callable (sync or async): the agent's OWN short-lived token
    scopes={                                     # per deployment: the actions each use case really needs
        "analyst": AgencyScope(resources=("bedrock:InvokeModel",),
                               resource_arns=("arn:aws:bedrock:us-east-1::foundation-model/my-model",)),
    },
)
gov = AgentGovernance(gov_url, app_id, hmac_secret, agent_id="MY_AGENT", agency=gate)

gov.pre_invoke(agent_name="analyst", model_id="my-model")   # CHECK, then the agency grant
bedrock = gov.agency_session("us-east-1").client("bedrock-runtime")
```

- **Token mode only.** The broker authenticates the agent's own token (aiap-client 1.1.0 token mode).
  There is no MAC / shared-secret mode and no static token argument: pass a provider that returns the
  current token.
- **Resources are configuration.** A use case without a scope is refused; nothing is defaulted. The broker
  refuses anything outside the agent's enrolled template.
- **The AIGP trace id is sent** (`X-AIGP-Trace-Id`), so the decision is linked to the trace.
- **The grant is kept and reused** until `renew_before_seconds` (default 120) before it expires.
- **Fail closed in every mode**, REPORT included: a refusal, an unreachable broker, a broker error, a
  malformed answer, an empty token, a failing provider or a missing `aiap-client` raise
  `AgencyRequiredError` (a `GovernanceBlockedError`, with a typed `code`).
- Hook-based adapters that do not call `pre_invoke` call **`gov.require_agency(use_case)`** (or
  `await gov.arequire_agency(...)`) after their CHECK and before acting. `gov.agency_grant` is the current
  grant; `await gov.release_agency()` releases the kept grants (e.g. at shutdown).

## Who should use this

You normally install a **framework adapter** (e.g. `maigp-crewai`, `maigp-microsoft-agent-framework`) which depends on this package. Install `maigp-agent-core` directly only when writing a custom adapter for a framework not yet covered.

## Control points

`pre_invoke` · `require_agency` · `on_model_call` · `on_tool_call` · `plan_submit` · `escalate` · `attest_input` · `step_complete` · `post_invoke` · `on_error`.

## Links

- Protocol, specs & full adapter catalog: https://github.com/owner-spec/aigp-protocol
- Issues: https://github.com/owner-spec/aigp-protocol/issues

---

License: Proprietary. © Kanjani AI Research & Causum.
