Metadata-Version: 2.4
Name: django-reststroke
Version: 0.1.0a3
Summary: Accept or refuse each tool call of an AI agent before it goes out, by a rule or by a person, in Django, and audit every decision.
Keywords: django,ai-agents,human-in-the-loop,approval,audit
Author: tryph0n
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Framework :: Django :: 6.1
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Requires-Dist: django>=6.1,<6.3
Requires-Dist: fastmcp>=4.2.6,<4.3 ; extra == 'gate'
Requires-Dist: httpx2>=2.5.0,<3 ; extra == 'gate'
Requires-Python: >=3.12
Project-URL: Documentation, https://django-reststroke.readthedocs.io/
Project-URL: Source, https://github.com/tryph0n/django-reststroke
Project-URL: Issues, https://github.com/tryph0n/django-reststroke/issues
Project-URL: Changelog, https://github.com/tryph0n/django-reststroke/blob/main/CHANGELOG.md
Provides-Extra: gate
Description-Content-Type: text/markdown

# django-reststroke

*Agent actions rest until a human lets them play.*

Accept or refuse each tool call of an AI agent before it goes out, by a rule or by a person, in Django, and audit every decision.

[![PyPI version](https://img.shields.io/pypi/v/django-reststroke)](https://pypi.org/project/django-reststroke/)
[![Python versions](https://img.shields.io/pypi/pyversions/django-reststroke)](https://pypi.org/project/django-reststroke/)
[![Django versions](https://img.shields.io/pypi/frameworkversions/django/django-reststroke)](https://pypi.org/project/django-reststroke/)
[![Documentation status](https://app.readthedocs.org/projects/django-reststroke/badge/?version=latest)](https://django-reststroke.readthedocs.io/)
[![CI](https://github.com/tryph0n/django-reststroke/actions/workflows/ci.yml/badge.svg)](https://github.com/tryph0n/django-reststroke/actions/workflows/ci.yml)
[![License](https://img.shields.io/pypi/l/django-reststroke)](https://github.com/tryph0n/django-reststroke/blob/main/LICENSE)

**Status:** alpha. The API, the settings and the database schema can change between releases, and the [changelog](https://github.com/tryph0n/django-reststroke/blob/main/CHANGELOG.md) lists each breaking change. Not for production use yet.

## What it does

An AI agent that calls tools can act on your systems: read data, send messages, change records. With reststroke, the agent sends each tool call to the gate, a separate process that holds the keys of your tools, and the gate asks your Django project if the call may pass. A rule that you write accepts or refuses the call at once, or the call waits until a person with the right to decide sees its exact arguments and accepts or refuses it. The gate sends only an accepted call to its tool. Each status change of a call gets an audit entry.

### Use cases

- **An instruction hidden in a document asks the agent to send your data out.** The agent obeys: it asks a tool that sends messages to send your list of clients to an outside address. Mark that tool "sends data out", and no rule can accept a call to it: only a person can let it pass. The person sees the exact arguments next to the message of the user, as the agent passed it on, and refuses. The tool never gets the call.
- **Routine calls do not wait for a person.** A rule accepts at once the calls to a read tool that the requester has the right to use, up to the daily budget that you give it; past the budget, the next calls of the day wait for a person. When the tool list that the gate gets shows a changed definition of a tool, no rule accepts a call to that tool until a person reviews the new definition.
- **What the person accepted is what goes out, once.** reststroke keeps the fingerprint of the call that the person accepted. The gate sends that exact call to its tool once, and a call that changed after the acceptance does not go out: it waits again, and a person can only refuse it.
- **Your agent holds no tool key.** Only the gate holds the keys of the tools, and no tool key reaches the agent or reststroke. Django keeps only the hashes of the keys of the gate and of each agent. In the demo, the agent shares a network only with the gate, and a script checks that it reaches the gate and not the tools, the web app or the database.
- **A second person activates each new rule text.** A rule that a person adds or edits in the admin acts only after a second person activates it. A superuser may activate their own text, and the record of the activation marks it. Deleting a rule needs no second person; only a superuser deletes a "refuse" or "ask" rule in force. The models and the admin of reststroke refuse to change or delete the activation records and the audit entries.

## How a tool call passes

The agent is your AI program. It runs outside Django and acts for a **requester**: the user of your Django project in whose name it makes the call, from the users and groups that the agent may act for. The agent sends each tool call to the gate, with its own key. The gate asks reststroke, which checks the keys, the call and the rights of the requester. A check or a rule answers at once. If not, the call waits for a **person who decides**: a user with the right to decide the calls to that tool, who accepts or refuses it before its deadline. Past the deadline, the call gets no. reststroke checks the call and the rights again before the gate sends an accepted call to its tool.

![Activity diagram of the path of one tool call, with one lane for each part and the key that each part holds](https://raw.githubusercontent.com/tryph0n/django-reststroke/main/doc/diagrams/call-path-activity.drawio.png)

## Requirements

- Python 3.12 or later.
- Django 6.1.
- PostgreSQL: the demo and the CI tests run on it.

## Quick start

In your Django project:

1. Install the package: `pip install django-reststroke`. On the machine where the gate runs: `pip install "django-reststroke[gate]"`.
2. Add `"reststroke"` to `INSTALLED_APPS`.
3. In the root URL configuration, include the interface of the gate and the inbox: `path("reststroke/", include("reststroke.urls"))` and `path("inbox/", include("reststroke.inbox_urls"))`.
4. Create the tables: `python manage.py migrate`.
5. Make the key of the gate with `python manage.py new_gate_key gate.key`, and add the hash that it prints to the setting `RESTSTROKE_GATE_KEY_HASHES`.

Then follow [Install in your project](https://django-reststroke.readthedocs.io/page/getting-started/install.html) for the other steps, the cron line of `expire_calls` included, and [Run the gate and add an agent](https://django-reststroke.readthedocs.io/page/how-to/gate-and-agent.html).

## Try the demo

The demo runs in Docker: a Django project with reststroke, the gate, demo tools and a scripted agent that obeys an instruction hidden in a note. You need git and Docker; the [tutorial](https://django-reststroke.readthedocs.io/page/getting-started/tutorial.html) gives the versions and the ports. The demo is in French.

```sh
git clone https://github.com/tryph0n/django-reststroke.git
cd django-reststroke
cp .env.example .env
docker compose up -d --wait
docker compose exec web python manage.py seed_demo
docker compose exec agent python -m scripted_agent --gate-url http://gate:8100/mcp
```

The last command then waits for a person. The [tutorial](https://django-reststroke.readthedocs.io/page/getting-started/tutorial.html) shows how to refuse that call as `bob` in the admin, and what the agent logs then.

## Links

- Documentation: <https://django-reststroke.readthedocs.io/>
- Changelog: [CHANGELOG.md](https://github.com/tryph0n/django-reststroke/blob/main/CHANGELOG.md)
- Security policy: report a vulnerability privately, as [SECURITY.md](https://github.com/tryph0n/django-reststroke/blob/main/.github/SECURITY.md) explains.
- Contributing: [CONTRIBUTING.md](https://github.com/tryph0n/django-reststroke/blob/main/CONTRIBUTING.md), the developer guide.
- License: Apache-2.0, see [LICENSE](https://github.com/tryph0n/django-reststroke/blob/main/LICENSE).

## About the name

Named after the rest stroke, the gypsy jazz picking technique Django Reinhardt used: the pick comes to rest on the next string before the note moves on.
