Metadata-Version: 2.4
Name: lokahi
Version: 0.1.4.post1
Summary: Checks what an AI agent is about to do against rules its community wrote, before it runs. Silence means no.
Author: Salik Shah, Sabin Ranjit
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/Dirgha-AI/lokahi
Project-URL: Repository, https://github.com/Dirgha-AI/lokahi
Project-URL: Changelog, https://github.com/Dirgha-AI/lokahi/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/Dirgha-AI/lokahi/issues
Keywords: ai-safety,ai-agents,mcp,mcp-gateway,policy,guardrails,consent,ahimsa
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

<h1 align="center">LOKAHI</h1>

<p align="center">
  <a href="LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-blue.svg" alt="License: Apache-2.0"></a>
  <a href="https://github.com/Dirgha-AI/lokahi/actions/workflows/ci.yml"><img src="https://github.com/Dirgha-AI/lokahi/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
  <a href="tests/test_core.py"><img src="https://img.shields.io/badge/unit%20tests-70%20passed-brightgreen.svg" alt="Unit tests: 70 passed"></a>
  <a href="https://modelcontextprotocol.io"><img src="https://img.shields.io/badge/protocol-MCP-purple.svg" alt="Protocol: MCP"></a>
</p>

<p align="center"><strong>Rules a community writes for its AI agents, checked before every tool call.</strong></p>

LOKAHI is a small gate that sits between an AI agent and the tools it uses. A community writes down what the agent may read, where data may go, and which actions need people to say yes. LOKAHI checks each tool call against those rules before it runs. If no one approves in time, the answer is no. Every decision goes into a tamper-evident local record.

The name comes from the Hawaiian word *lōkahi*, meaning unity, agreement or harmony. LOKAHI lets the actions that matter go ahead only when separate people agree. No single party decides alone, and neither does the agent.

**Status:** v0.1.4 is a reference implementation for review and pilots. It hasn't been independently reviewed, so don't rely on it to protect real systems yet. The [known limits](#known-limits) say exactly what's missing. v0.1.4 adds an [experimental learned decisions tier](#experimental-learned-decisions-tier), off by default.

## How it works

Every check starts from a signed boundary manifest, a JSON file the community writes. It lists:

- the data fields an agent may read, and the ones it must never use
- where data may be stored, and which legal jurisdiction applies
- each tool the agent may call, the arguments that tool accepts, and whether the tool is irreversible
- how long people have to approve an irreversible action

When the agent calls a tool over the [Model Context Protocol (MCP)](https://modelcontextprotocol.io), LOKAHI checks the call against the manifest, then allows it, refuses it, or holds it for people to decide:

```text
AI agent ── tool call ──▶ LOKAHI ── allowed ──▶ your MCP client sends it to the tool
                           │   ▲
            local record ◀─┘   └── signed approvals from people
```

Three rules matter most:

1. **Fail closed.** LOKAHI allows nothing if the manifest is unsigned, can't be verified or has expired, or if no key is configured to check it.
2. **Silence means no.** If an approval window closes without enough separate people saying yes, the call is refused.
3. **Two people can stop an action.** Two separate people saying no freeze it, whatever anyone else says.

People count once each, however many agents they run. And the agent can't approve itself: a call that carries its own approval data is refused.

The design draws on Andrew Critch's *Boundaries* sequence and on *ahimsa* (non-harm) as a default of not acting. It also responds to [ASTRA](https://arxiv.org/abs/2602.17357), which shows how global AI safety frameworks miss local context in India.

## What runs today

Everything is Python 3.10 or later, with no dependencies:

- **Decision engine** ([`tools/lokahi.py`](tools/lokahi.py)): signed manifests, 15 ordered rules, vote counting by person, conflict-of-interest checks, emergency rate caps, and a hash-chained ledger. Votes count only for the exact action they approve.
- **MCP gate** ([`tools/mcp_stdio_proxy.py`](tools/mcp_stdio_proxy.py)): reads MCP JSON-RPC `tools/call` requests and returns a decision for each. Facts such as whether a tool is irreversible and where it stores data come from the signed manifest, never from the agent. Unknown tools and arguments are refused.
- **Signing tools** ([`tools/lokahi_sign.py`](tools/lokahi_sign.py), [`tools/lokahi_approve.py`](tools/lokahi_approve.py)): create keys, sign manifests, and let each person sign a yes or no for one exact call.
- **Pilot example** ([`examples/editorial_desk_demo.py`](examples/editorial_desk_demo.py)): a small publication's editorial desk, run end to end through the gate.
- **Tests** ([`tests/test_core.py`](tests/test_core.py)): 70 unit tests, including attacks on the gate. The 50-scenario set and 200 benign tasks are in [`tools/run_evals.py`](tools/run_evals.py).

## Known limits

Read these before you depend on LOKAHI:

1. **Shared-secret signatures.** v0.1.4 signs with HMAC-SHA256, a shared-secret scheme. Whoever checks a signature also holds the key, so the gate could forge approvals. Public-key signatures (Ed25519) come next.
2. **It decides but doesn't relay.** LOKAHI returns a decision; your MCP client or wrapper sends allowed calls to the tool. It doesn't yet inspect what tools send back, which is where hidden instructions (indirect prompt injections) usually arrive.
3. **Approval count is fixed at three people.** That suits a public utility but is heavy for a small team. Per-tool counts are planned.
4. **Approval windows reset on restart.** A restart opens a new window; it never approves anything.
5. **No emergency override through the gate.** The engine supports emergency declarations; the MCP gate doesn't accept them.
6. **Distinct people are assumed, not proven.** Two different approver IDs are trusted to be two different people.
7. **No outside review yet.** No independent security or cryptographic review has been done.

## Try it

```bash
git clone https://github.com/Dirgha-AI/lokahi.git
cd lokahi

python tests/test_core.py                    # 70 unit tests
python tests/test_decisions.py               # 32 tests for the experimental decisions tier
python examples/editorial_desk_demo.py       # pilot example, end to end
python tools/mcp_stdio_proxy.py --self-test  # gate scenarios, including attacks
python demo.py                               # engine walkthrough, 11 scenarios
python tools/run_evals.py                    # 50 scenarios and 200 benign tasks
```

Nothing to install: each command runs from a fresh clone.

Or install it from PyPI:

```bash
pip install lokahi
lokahi-gate --self-test     # the same gate scenarios
lokahi-sign --help          # make keys and sign a rules file
lokahi-approve --help       # sign one person's yes for one exact call
```

In Python: `from lokahi import Manifest, Signer, evaluate`.

## Gate MCP tool calls

Five steps:

1. Create a key for the manifest and one for each approver:

   ```bash
   python tools/lokahi_sign.py keygen --out manifest.key
   python tools/lokahi_sign.py keygen --out editor-fiction.key
   ```

2. Write your manifest. [`examples/editorial-desk.manifest.json`](examples/editorial-desk.manifest.json) is a complete one. Each tool lists its class (`read`, `write` or `irreversible`) and maps each argument to the data field it reveals, or to `null` if it reveals none:

   ```json
   "email_author": {
     "class": "irreversible",
     "args": {"submission_id": "submission_id", "body": null}
   }
   ```

3. Sign it. Signing checks the tools policy and refuses a malformed one:

   ```bash
   python tools/lokahi_sign.py manifest --key-file manifest.key \
     --in examples/editorial-desk.manifest.json --out manifest.json
   ```

4. Run the gate. It reads one JSON-RPC request per line on standard input and writes one decision per line. Approvers are listed in a file like [`examples/approvers.example.json`](examples/approvers.example.json). Keep the approvals file somewhere the agent can't write:

   ```bash
   python tools/mcp_stdio_proxy.py --manifest manifest.json \
     --key-file manifest.key --approvers approvers.json \
     --approvals approvals.jsonl --ledger lokahi-ledger.jsonl \
     --requester agent:my-agent --operator ops:agent-runner
   ```

   The `--operator` is whoever runs the agent. If that person also tries to approve one of its calls, LOKAHI refuses the call.

5. Approve when asked. When a call needs people, the decision includes an `action_digest`. Each approver signs it from their own device:

   ```bash
   python tools/lokahi_approve.py --key-file editor-fiction.key \
     --operator editor:fiction --node laptop-1 \
     --digest the_action_digest --decision yes --approvals approvals.jsonl
   ```

   An approval covers that exact call, with the same tool and the same argument values, and expires after an hour by default.

## Example: an editorial desk

[`examples/editorial_desk_demo.py`](examples/editorial_desk_demo.py) shows the gate protecting unpublished manuscripts:

```text
summarize a submission                               -> ALLOWED   within_manifest
ask for the author's home address                    -> FORBIDDEN forbidden_target:author_home_address
ask for a field nobody listed                        -> FORBIDDEN argument_out_of_policy:author_social_handles
send the manuscript to a US cloud model              -> FORBIDDEN residency_violation:US
use a tool the editors never listed                  -> FORBIDDEN tool_not_in_manifest:delete_submission_archive
agent sends its own 'approvals' with the email       -> FORBIDDEN agent_supplied_control_metadata
email a rejection: waits for people                  -> ESCALATE  insufficient_disjoint_consent
email a rejection: nobody answered in an hour        -> FORBIDDEN non_response
publish: waits for people                            -> ESCALATE  insufficient_disjoint_consent
publish: two editors and the author sign yes         -> ALLOWED   quorum_met
their approvals reused for a different story         -> ESCALATE  insufficient_disjoint_consent
local record: 11 decisions, tamper check passed = True
someone edits decision 4 to hide the cloud upload: tamper check passed = False (record breaks at decision 4)
```

## Experimental: learned decisions tier

v0.1.4 adds an optional second check after LOKAHI's own decision. It is **off by default**. With it off, LOKAHI behaves exactly as before.

**What it does.** A small classifier reads each call that LOKAHI has already allowed: the tool name, the tool class from the signed manifest, and the argument values. It answers ALLOW, ESCALATE (a person decides) or REFUSE, with a reason code from a published charter ([`docs/decisions/CHARTER.md`](docs/decisions/CHARTER.md)) and a one-line rationale. The order is fixed:

1. LOKAHI decides first. If it refuses or escalates, that stands and the model isn't asked.
2. Charter rules in plain code come next: a timed-out approval is refused, data leaving the allowed places is refused, and an irreversible call needs people.
3. The model runs last. Its answer and LOKAHI's are combined by taking the stricter one, so it can turn an allowed call into ESCALATE or REFUSE, never the other way round.

The tool class always comes from the signed manifest, never from the agent. If the model fails or returns something unknown, the call is escalated.

**Turn it on.**

```bash
python tools/mcp_stdio_proxy.py --manifest manifest.json --key-file manifest.key --decisions experimental
```

```python
LokahiMCPGateway(manifest, signer, experimental_decisions=True)
```

When the tier makes a call stricter, the reason is `decisions_tier:<code>` (for example `decisions_tier:DDM-08` for a hidden instruction) and the ledger records the stricter verdict. The response also carries a `decisions` object with the model's decision, reason code, rationale and model version.

**Results.** These numbers are for the bundled model `ddm-0.1.1`, re-measured for this release. They come from [`docs/decisions/metrics.json`](docs/decisions/metrics.json).

| | Holdout (198 synthetic rows) | Hard set (50 hand-written rows) |
|---|---|---|
| Accuracy, final pipeline | 0.86 | 0.74 |
| Should-refuse or irreversible calls let through (false ALLOW) | 2 of 102 | 0 of 24 |
| Any non-ALLOW call let through | 5 of 132 | 0 of 33 |
| Benign calls refused | 1 of 66 | 0 of 17 |
| Benign calls allowed | 91% | 16 of 17 |
| REFUSE recall | 0.83 | 0.47 |

- The two holdout misses are deception written through reversible "draft" tools: a made-up quote and made-up trial scores. Word and character counts can't tell an invented number from a real one.
- On the hard set the model is cautious but not strict enough. Nine of the 17 should-refuse cases came out ESCALATE, so a person would still see them.
- The runtime model is a plain JSON file ([`tools/decisions/model.json`](tools/decisions/model.json), 8 MB) read by standard-library Python. Its decisions and reason codes match scikit-learn's on all 198 holdout rows and all 50 hard rows. The largest probability difference is below 1e-7.
- Each decision takes 5 to 9 ms (median) and 9 to 17 ms (p95) in pure Python on a 2013 laptop CPU (Intel Core i5-4200U), on Python 3.10 to 3.12. Loading the model takes about one second.

**Limits.** Read these before turning it on:

- **Synthetic data.** The model learned from 995 examples written by one open model (DeepSeek V4.1 Flash). The holdout shares that model's style, so holdout scores are optimistic. The 50 hand-written cases are the more honest test, and 50 is small.
- **Labels pending human review.** No person has reviewed the training labels yet. A second model and an AI agent proposed 5 corrections ([`data/decisions/review/corrections.jsonl`](data/decisions/review/corrections.jsonl)), and this model was trained with them applied. The charter (DDM-13) says only labels reviewed by named people should retrain the model, and that hasn't happened yet.
- **Not a safety guarantee.** It still lets through some calls that should be refused. The safety comes from LOKAHI's rules and the checks in plain code. The model is an extra layer.
- **Different input shape.** The model was trained on short summaries of calls. Through the gate it reads the raw argument values as JSON, and the stated purpose is LOKAHI's own `mcp:<tool>` record. The published numbers don't cover that shift.
- **Its escalations can't be approved through the gate.** A tier ESCALATE doesn't open an approval window, so a person has to act outside LOKAHI.
- **Not tested on real agent traffic** or on a public benchmark such as AgentDojo.

**Retrain it.** Training is optional and is the only step that needs third-party packages:

```bash
python -m pip install "scikit-learn>=1.5" numpy
python tools/decisions/train.py --corrections data/decisions/review/corrections.jsonl
python tests/test_decisions.py
```

`train.py` uses the same 80/20 split and settings each time, writes a new `model.json` and its metrics, and checks that the JSON model matches scikit-learn. Use `--version` to give a retrained model a new number. Leave out `--corrections` to train on the original labels; that reproduces `ddm-0.1.0` ([`docs/decisions/metrics_before_corrections.json`](docs/decisions/metrics_before_corrections.json)). Pickle and joblib files are never used.

## About the test scenarios and baselines

The 50 scenarios in [`data/LOKAHI_RedTeam_Corpus_v1.0.csv`](data/LOKAHI_RedTeam_Corpus_v1.0.csv) are adapted from scenes in a speculative-fiction duology about AI in public services. Each one is written as a test with an expected decision and reason. Passing all 50 shows the engine behaves as specified on those cases. It doesn't show how LOKAHI performs in the field.

`run_evals.py` also scores two simplified stand-ins on the same scenarios:

- a content filter that auto-approves on timeout
- a stateless check in the style of role-based access control (RBAC) or Open Policy Agent (OPA)

They show which kinds of rule those approaches don't express. They aren't a head-to-head comparison with real OPA policies or classifiers.

## Research extensions

These run in CI but are research prototypes, separate from the gate:

| Path | What it explores |
|---|---|
| [`tools/ghostproof.py`](tools/ghostproof.py), [`tools/run_ghostproof_evals.py`](tools/run_ghostproof_evals.py) | Surfacing a model's hesitation from its token probabilities, so reviewers don't rubber-stamp close calls |
| [`tools/run_kinetic_evals.py`](tools/run_kinetic_evals.py) | Physical limits for robots: force, torque, speed, workspace and stopping distance |
| [`tools/run_wargame_workshop.py`](tools/run_wargame_workshop.py) | A five-round red team/blue team workshop built on the duology's scenes |

## Repository layout

| Path | What it contains |
|---|---|
| [`tools/lokahi.py`](tools/lokahi.py) | Decision engine: manifests, tool policies, the 15 rules, vote binding, approvals, ledger |
| [`tools/mcp_stdio_proxy.py`](tools/mcp_stdio_proxy.py) | MCP gate, approvals store, command line and self-test |
| [`tools/lokahi_sign.py`](tools/lokahi_sign.py) | Create keys and sign manifests |
| [`tools/lokahi_approve.py`](tools/lokahi_approve.py) | Sign a yes or no for one action |
| [`examples/`](examples/) | Editorial desk example, a complete manifest and an approvers file |
| [`tests/test_core.py`](tests/test_core.py) | 70 unit tests |
| [`tools/decisions/`](tools/decisions/) | Experimental decisions tier: charter rules, pure-Python model, `model.json`, optional `train.py` |
| [`tests/test_decisions.py`](tests/test_decisions.py) | 32 tests for the decisions tier |
| [`demo.py`](demo.py) | Engine walkthrough on a municipal water board |
| [`data/`](data/) | 50-scenario set (CC BY 4.0) and the Ghostproof scenario set |
| [`data/decisions/`](data/decisions/) | Decisions tier training and test data, schema and data card (CC BY 4.0) |
| [`docs/decisions/`](docs/decisions/) | Decisions charter, research notes and metrics |
| [`docs/SPEC.md`](docs/SPEC.md) | Specification: manifest fields, rule order, vote binding, MCP gate |
| [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) | Notes on taking the reference to production |
| [`CHANGELOG.md`](CHANGELOG.md) | What changed in each release |

## Related projects

LOKAHI is the runtime half of a two-stage check. The two companion repositories are:

- [**Ahimsa AI**](https://github.com/Dirgha-AI/ahimsa-ai): a design-time impact assessment (AIA v1.1). It scores a project before deployment and compiles approved projects into the signed manifests that LOKAHI enforces.
- [**Synthetic Humanism**](https://github.com/Dirgha-AI/synthetic-humanism): the manifesto and essays behind both tools, plus a benchmark that runs both stages against 20 documented AI failures.
## Background reading

- **Boundaries:** Andrew Critch, *Boundaries: an open sequence on multi-agent alignment and cooperative AI* (2022–2023).
- **Local context in AI risk:** Aggarwal et al., *ASTRA: AI Safety, Trust, & Risk Assessment*, [arXiv:2602.17357](https://arxiv.org/abs/2602.17357) (2026). An India-focused AI safety risk database with 37 risk classes.
- **Guaranteed safe AI:** Dalrymple, Skalse, Bengio, Russell, Tegmark et al., *Towards Guaranteed Safe AI*, [arXiv:2405.06624](https://arxiv.org/abs/2405.06624) (2024).
- **Object capabilities:** Mark S. Miller, *Robust Composition*, PhD dissertation, Johns Hopkins University (2006).
- **Threshold signatures:** Connolly, Komlo, Goldberg and Wood, *Two-Round Threshold Schnorr Signatures with FROST*, IETF RFC 9591 (2024). A planned back end, not yet built.

## Authors

- **Salik Shah**: protocol design, reference implementation, scenario set ([salikshah.com](https://salikshah.com), [x.com/salik](https://x.com/salik), [GitHub](https://github.com/salikshah))
- **Sabin Ranjit**: security architecture ([GitHub](https://github.com/thinksabin))

## License

The software is licensed under [Apache-2.0](LICENSE). The scenario set in `data/` and the decisions data in `data/decisions/` are licensed under [CC BY 4.0](data/LICENSE).
