Metadata-Version: 2.4
Name: sealgraf-composio
Version: 0.1.0
Summary: Sealgraf spend-approval tools as a Composio local (custom) toolkit: check a purchase against the owner's limits before buying, owner approval on Sealgraf's own surface, sealed receipts.
Author: Sai Arun Gunuganti
License: MIT
Project-URL: Homepage, https://sealgraf.com
Project-URL: Documentation, https://sealgraf.com/developers/setup
Keywords: composio,agents,spend-approval,sealgraf,receipts
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: composio>=0.28
Requires-Dist: httpx>=0.27
Requires-Dist: pydantic>=2
Dynamic: license-file

# Sealgraf for Composio

Four tools that give a Composio agent a spending conscience:
**check before buying, owner approval above the owner's own line, and a
sealed receipt for every completed purchase.**

Sealgraf (https://sealgraf.com) is a spend-approval service for agents.
An agent checks a purchase *before* buying; the check is decided against
limits the owner set in advance; a purchase above the owner's approval
line waits for the owner to decide on Sealgraf's own surface; and when
the purchase completes, the evidence is sealed into a receipt that
neither the agent nor the owner can rewrite afterwards.

This package is a **Composio local (custom) toolkit** for the Python
SDK — the current canonical custom-tool form
(`composio.experimental.Toolkit`, Tool Router sessions). It runs
in-process in your application, next to Composio's catalog tools. It is
not a public-catalog listing; see `../RESEARCH.md` for what that would
take and why this package is the buildable artifact today.

## The four tools

Inside a session they are exposed as `LOCAL_SEALGRAF_*`:

| Tool | What it does |
|---|---|
| `LOCAL_SEALGRAF_CHECK_PERMISSION` | Ask before a purchase. Returns `allow` (with a single-use decision token), `deny`, or `needs_human_approval` with a `preauth_id` to resume later. |
| `LOCAL_SEALGRAF_CLAIM_DECISION_TOKEN` | After the owner approves a waiting purchase in Sealgraf, claim its single-use decision token. |
| `LOCAL_SEALGRAF_COMPLETE_PURCHASE` | Report the finished purchase; Sealgraf seals it into a receipt. |
| `LOCAL_SEALGRAF_GET_RECEIPT` | Fetch state by exactly one id: a receipt id (reads `GET /v1/receipts/{id}` — the sealed receipt) or a preauth id (reads `GET /v1/preauthorize/{id}` — the check record and its current state). With neither id, lists recent receipts. |

The spend loop an agent should follow:

```
check_permission ── allow ─────────────► buy ──► complete_purchase ──► sealed receipt
                 └─ needs_human_approval ─► (owner decides in Sealgraf)
                        ─► claim_decision_token ─► buy ─► complete_purchase
                 └─ deny ─► do not buy
```

## Not the same as Composio's approvals / the HITL toolkit

Composio has an (experimental) session feature that can require the
user's approval before chosen tools run, and its catalog carries a
generic human-approval toolkit (`hitl`). Both are **in-band, per-call
prompts**: a person sitting in the agent's client is asked "run this
tool?", with no limits, no amounts, and nothing kept afterwards beyond
a log line; "always approve" waives the rest of the session.

Sealgraf is different in kind:

- **Limits are set in advance.** The owner pre-commits per-purchase and
  period caps, allowed categories, and an approval threshold (a
  "mandate"). Most checks are decided automatically against those
  limits — no human is paged for routine purchases.
- **Approval happens on Sealgraf's own surface**, out-of-band from the
  agent session, and only the owner can give it: an agent key can never
  approve its own check (enforced server-side).
- **The subject is money, not tool calls**: a specific amount, currency,
  category, and counterparty.
- **The output is a sealed receipt** — the check, the decision (and who
  approved it), and the completion evidence, hash-chained so neither
  side can rewrite the record.

One line: Composio's approval asks *"may this tool run?"* in the
moment; Sealgraf answers *"was this purchase inside the owner's rules,
who said yes when it wasn't, and where is the proof?"* — before,
during, and after the spend.

## Setup

```bash
pip install sealgraf-composio     # or from this repo: pip install ./toolkit
```

Configuration is host-side, via environment — **never** tool arguments
(tool arguments are chosen by the model and visible in prompts/logs;
the key is a credential):

| Variable | Required | Meaning |
|---|---|---|
| `SEALGRAF_API_KEY` | yes | An agent-scope Sealgraf API key, sent as the `X-API-Key` header. The key is bound server-side to one agent and mandate, which is how the tools can leave `mandate_id`, `agent_id` and `currency` unset. |
| `SEALGRAF_BASE_URL` | no | Default `https://sealgraf.com`. Point it at UAT or a local instance for testing. |
| `SEALGRAF_TIMEOUT_SECONDS` | no | Default `20`. Every call has an explicit timeout. |

Plus the usual Composio setup for the host app (`COMPOSIO_API_KEY`).

```python
from composio import Composio
from sealgraf_composio import build_toolkit

composio = Composio()
session = composio.create(
    user_id="default",
    experimental={"custom_toolkits": [build_toolkit(composio)]},
)
tools = session.tools()   # handed to your agent framework as usual
```

See `example_session.py` for a runnable wiring, including direct
`session.execute(...)` calls outside an agent loop.

## Behaviour guarantees

- **Fail closed.** A missing key, timeout, network error, non-2xx
  response, or malformed response raises an error and fails the tool
  run. A tool never returns anything that could be read as permission
  unless Sealgraf actually granted it.
- **`needs_human_approval` is a normal outcome**, not an error. The
  result carries `preauth_id` and `state: "pending_approval"`; the
  agent pauses and resumes with `claim_decision_token` once the owner
  has decided. Only the agent key that ran the check can claim its
  token.
- **Idempotency.** Pass `idempotency_key` to `check_permission` and a
  retried check returns the original record (`replayed: true`) instead
  of stacking a second reservation.
- **Amounts are integer minor units** (cents) everywhere, matching
  Sealgraf's API. Categories are Sealgraf's closed enum of 14.

## Development

```bash
python -m venv .venv && .venv/bin/pip install -e . pytest
cd ../tests && ../toolkit/.venv/bin/python -m pytest . -q
```

The test suite mocks all HTTP and asserts the exact requests against
the production API contract (see `../RESEARCH.md` for the contract and
its code citations, and `../BUILD-RECORD.md` for run evidence).
