Metadata-Version: 2.4
Name: kla-governance
Version: 0.1.0
Summary: KLA governance SDK for Python: propose agent actions, wait on human approvals, and resume governed execution through the KLA Control Plane
Author-email: KLA Digital <engineering@kla-digital.io>
Maintainer-email: KLA Digital <engineering@kla-digital.io>
License-Expression: Apache-2.0
Project-URL: Homepage, https://kla.digital
Project-URL: Repository, https://github.com/kla-digital/monolith.git
Project-URL: Issues, https://github.com/kla-digital/monolith/issues
Project-URL: Documentation, https://kla.digital/docs/guides/governance-sdk-quickstart-python
Keywords: governance,ai-agents,human-approval,policy,audit,kla
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: httpx>=0.24.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: isort>=5.0.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Dynamic: license-file

# kla-governance

KLA governance SDK for Python. It lets an agent propose actions to the KLA Control Plane, receive one of the four policy outcomes (`allow`, `warn`, `require_approval`, `block`), wait on human approval, and resume governed execution with sealed evidence on the server side.

The SDK implements the agent action ingress contract:

- `POST /v1/agent-actions` proposes an action.
- `GET /v1/agent-actions/{proposalId}` reads the recorded state.
- `POST /v1/agent-actions/{proposalId}/cancel` cancels a proposal before execution.
- `POST /v1/agent-actions/{proposalId}/resume` redeems an approval and executes exactly once.

Tenant and principal are derived server-side from the verified token. The SDK never sends a tenant field in the request body.

## Install

```bash
pip install kla-governance
```

Publication status: the package is currently unreleased. The install command applies after the
reviewed `0.1.0` artifact is published. Python 3.10 or later is required. The only runtime
dependency is `httpx`.

## Usage

```python
from kla_governance import ActionRequest, AgentIdentity, DecisionDenied, KLAClient

client = KLAClient(
    base_url="https://api.kla.digital",
    credential=get_access_token,  # client-credentials Bearer token or thunk
    agent=AgentIdentity(agent_id="agt_9f81a7", release_version="1.4.2"),
    mandate_id="mnd_2c41e0",
)

def process_payment(account_id: str, amount: float) -> Receipt:
    return client.execute_action(
        ActionRequest(
            tool="payments",
            operation="charge",
            destination="stripe:acct_main",
            parameters={"account_id": account_id, "amount": amount},
        ),
        proposal_id=stable_id_for(account_id, amount),
    )
```

Call `client.resolve_binding()` during startup checks to confirm that the configured agent Release
and mandate revision are active in the current tenant. This read-only request performs no policy
evaluation or action execution. `AsyncKLAClient.resolve_binding()` provides the async form.

`execute_action` sends the action to KLA's registered connector, waits through the Decision Desk
flow when policy requires approval, and returns `execution.result` from the server. KLA owns the
side effect. A malformed executed response without its execution record raises
`response_schema_invalid`; `ExecutionMissing` remains the final defensive guard after settlement.
`DecisionDenied` covers `block`, a denied approval, and an expired approval. `ApprovalTimeout`
means the Decision Request remains pending after `timeout`. `AsyncKLAClient` mirrors this API with
`async` methods.

`ExecutionEvidencePending` means the connector ran while evidence sealing remains incomplete. The
SDK withholds every result in this state. The error retains the decision and receipt references.
Reconcile that proposal; a new proposal represents a new logical action.

The SDK validates the complete ingress response before returning a decision or result. It requires
the exact schema version, proposal and action binding, canonical lifecycle states, a SHA-256 result
binding, and matching receipt references. A malformed or inconsistent response raises
`KLATransportError` with `code="response_schema_invalid"`.

## Retry rules

- Reuse the same `proposal_id` when retrying an action; a committed proposal replays its recorded outcome.
- 503 responses and network errors are retried automatically with backoff and jitter.
- 4xx responses are terminal for that proposal and surface as `KLATransportError` with `retryable=False`.
- `Revoked`, `EvidenceUnavailable`, `ExecutorUnavailable`, and `Cancelled` identify common
  fail-closed branches without parsing error text.

## Framework adapters

`governed_tool` supports existing decorated framework tools. It binds the decorated signature,
delegates to `execute_action`, and returns the server result. The decorated function body is never
invoked. Approval-predicate helpers were removed because they could not preserve one KLA proposal
identity through approval and execution.

## Local sandbox

`LocalSandboxClient` is an exported deterministic test double. It accepts static outcomes, performs
no network or destination call, and redacts parameter values from its call record. Every result has
`production=False` and `environment="local-sandbox"`, giving it a distinct type from live
`KLAClient.execute_action` results.

```python
from kla_governance import LocalSandboxClient, LocalSandboxScenario

sandbox = LocalSandboxClient([
    LocalSandboxScenario(
        disposition="require_approval", approval_status="approved", result={"id": "local-1"}
    )
])
result = sandbox.execute_action(action)
assert result.environment == "local-sandbox"
```

## License

Apache-2.0
