Skip to main content

lawkeep

Only review what your laws don't protect.

lawkeep is an open-source layer for AI-assisted coding. You decide on a short list of laws: rules your code must obey for every input, each with one plain-English sentence. Your coding agent writes and maintains the checks behind them. lawkeep records every law change, asks you before any law gets weaker, runs one gate that says ALL LAWS HOLD or SOME LAWS FAIL, and shows you which changed code no law protects, so that is the only code you still need to read.

Status: pre-alpha. v0.1 is being built in this repository and is not released yet. The names lawkeep on PyPI and npm are placeholders. Commands and file formats may still change. The design is in docs/PLAN.md.

The promise, honestly

  • Every law runs on every lawkeep check (unless you name single laws with --law, and then the result says it was a partial run), against many generated inputs, and every failure comes with the simplest input that breaks it. A law that skips itself while it runs counts as an error, not a pass.
  • The AI never changes your laws silently. Every law change is recorded: what changed, which agent changed it, why, and who said yes. A change made by editing files directly is flagged as an unrecorded law change.
  • Weakening a law needs your yes. Stricter changes can go ahead automatically if you allow it; loosening, deleting or skipping a law, or trying fewer cases, waits for a person. lawkeep decides the direction from evidence (sample inputs and planted bugs) plus a few code rules; when it cannot tell, it asks.
  • lawkeep tells you when your laws are too weak to catch real bugs (it plants bugs and checks whether any law notices). A new law must pass this quick check before it goes in without asking.

What lawkeep does not promise: that your code is correct beyond what your laws say, or that an agent cannot get around it. lawkeep records and shows; it does not force a review process on your team. Nothing in v0.1 is "proven": that word is reserved for machine-checked proofs (level L4, planned for v0.5).

Known limits

These are the honest edges of v0.1. Each is shown to you when lawkeep can see it, but none is made impossible:

  • The code under test is trusted. Laws call your code in the same Python process as lawkeep's law runner, so code written to cheat could change the results from inside (for example by patching Hypothesis). lawkeep flags source files that import lawkeep, pytest or Hypothesis, read lawkeep's settings, raise skips, or shadow a standard-library or installed module, and results travel through a private channel that is closed before your code's clean-up runs. Flagged is not prevented: read flagged files before trusting a green result.
  • Approvals record who claimed to approve. lawkeep cannot check who typed "yes". Approvals made without a terminal (for example by an agent after its permission prompt) are recorded as such and stay on the lawkeep approve list until someone in a terminal confirms or undoes them (both are recorded).
  • "Is a person there?" is a guess. Agents use it to choose between stopping to ask and parking a change for later. lawkeep reads LAWKEEP_PERSON_PRESENT, CI, whether the command was typed in a terminal, and two Claude Code variables; other agents' interactive sessions read as "nobody there". The guess never changes what lawkeep allows.
  • "Same meaning" and "stricter" are evidence, not proof. They come from sample inputs, deliberately broken versions and planted bugs, plus code rules (only a consistent rename counts as the same meaning; a change to a law's input generators always asks).
  • Objects that lie about comparisons. Code that returns values whose == or <= always say "yes" passes equality and bound laws. lawkeep flags comparison methods that always return True, but cannot catch every disguise.
  • Git decides what is "new". Several checks compare with a git base (HEAD locally, the pull request's base in CI). Once a change is committed locally, lawkeep check --base main (or CI) still shows it; plain lawkeep check may not.
  • Filtered inputs. A law that throws most generated inputs away (with assume()) checks fewer cases than it seems; the report says how many inputs were thrown away. assume() called from your code instead of a law counts as an error.

What a law looks like

# laws/refunds.py
from billing.refunds import RefundTooLarge, refund
from lawkeep import given, law, st

@law("A refund never exceeds the original payment")
@given(charge=st.charges(), amount=st.money(0, 1000))
def refund_never_exceeds_payment(charge, amount):
    try:
        result = refund(charge, amount)
    except RefundTooLarge:
        return
    assert result.total <= charge.total

A law is a normal pytest test written with Hypothesis, plus a sentence. You judge the sentence and a table of concrete examples ("payment $40, refund $50 → refused"); you never have to read the code.

Quick start: just ask your coding agent

You do not need to type commands or read code. Once lawkeep is installed in your project, talk to your coding agent (Claude Code, Codex, Cursor, Copilot or another):

  • "Set up lawkeep for this project."
  • "Add a law that refunds can never be more than the customer paid."
  • "Show me the laws waiting for my approval." Then answer yes or no to each, after looking at the examples it shows you.
  • "Check the laws." / "Did any law change in this session?"
  • "Which parts of this change are not protected by a law?"

The agent runs the same lawkeep commands a developer would and explains the results in plain English. If a law blocks what you asked for, a well-behaved agent stops and asks you instead of weakening the law; lawkeep treats that as a normal, successful outcome.

Long runs without you (overnight, or in CI): the agent parks and keeps going. When a law blocks it, it proposes the change with its reason (anything that loosens a law waits for you), never works around the law or approves anything, finishes everything that does not depend on that law, and ends with a summary: done / waiting for you / not done because blocked. In the morning, lawkeep approve shows one waiting list of everything that piled up, grouped and counted, with a suggestion for each kind; "N things wait for your decision" also shows at the start of the next session, in lawkeep check, in lawkeep report and in the pull-request comment.

The non-technical guide walks through a week with lawkeep.

Quick start: developers

uv add --dev lawkeep            # or: pip install lawkeep
lawkeep init --agent claude-code
lawkeep draft billing.refunds --request "refunds can never exceed the payment"
lawkeep approve                 # one waiting list: drafts, proposals, unrecorded edits, unconfirmed approvals
lawkeep check                   # ALL LAWS HOLD / SOME LAWS FAIL
lawkeep strength                # are the laws strong enough to catch planted bugs?
lawkeep report --base main      # law changes + which changed functions no law protects

Every command takes --json (one JSON document, for agents and scripts) and returns a stable result code:

Code Meaning
0 all laws hold
1 some laws fail
2 waiting for a person's decision (for example an unrecorded law change, or a weakening proposal)
3 setup problem

What lawkeep check prints when a law fails (the billing example with its planted bug, plain text as in a CI log, shortened; in a terminal it is in colour):

+------------------------------------------------------------------------------+
| SOME LAWS FAIL                                                               |
| 3 of 6 laws broke on your current code. Fix the code, not the law.           |
+------------------------------------------------------------------------------+

Law                                                                       Result
--------------------------------------------------------------------------------
“Refunding exactly what was paid gives back exactly the payment and the   Fails
whole card fee”
“Rounding money gives whole cents and is never off by more than half a    Holds
cent”
...

+-  This law fails  -----------------------------------------------------------+
| “Refunding exactly what was paid gives back exactly the payment and the      |
| whole card fee”                                                              |
|                                                                              |
| The input that breaks it: charge: a charge of $0.25 with no discount code    |
| Why it failed:            The law expected 0.25, but the code gave 0.26.     |
|                                                                              |
| What the inputs mean                                                         |
|   charge: a sample charge that lawkeep made up for testing                   |
| ...                                                                          |
| law id refunds.full_refund_gives_back_exact_payment · file laws/refunds.py   |
+------------------------------------------------------------------------------+

lawkeep tried up to 200 inputs per law (the same inputs every run, so the same
code gives the same result) · profile pr · took 3.1s
Next step: Fix the code so each failing law holds for the example shown, then
           run `lawkeep check` again. If you think a law itself is wrong, stop
           and ask a person (`lawkeep propose`); do not edit laws/ directly.

lawkeep check --details adds law ids, timings and every note; --json gives the same result as one JSON document for agents. Output is plain text (no colour) when it is not a terminal, in CI, or with NO_COLOR set.

A $0.25 payment would be refunded as $0.26. The failing input is saved and retried on every later run.

More in the developer guide and the law recipes.

In pull requests

The lawkeep GitHub Action runs the gate on a fresh checkout and posts one comment per pull request (updated, never duplicated) with the verdict, the law changes and their approvals, and the protection report. It does not block merges unless you set fail-on-broken-laws: "true".

# .github/workflows/lawkeep.yml
on: pull_request
permissions: { contents: read, pull-requests: write }
jobs:
  laws:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }
      - uses: lawkeep/lawkeep@v0     # available once v0.1 is tagged

Full templates (pull requests and a nightly deep search) are in .github/workflows/templates/.

Supported coding agents

v0.1 supports Claude Code, OpenAI Codex, Cursor and GitHub Copilot from day one, and any other agent through a shared base: the lawkeep CLI, an MCP server (lawkeep mcp, tools check, propose, report), an AGENTS.md snippet and a git pre-commit hook. Agents with their own hooks and permission prompts get a richer adapter on top, so "yes" comes from the prompt you already use. lawkeep draft starts a fresh, separate session of your own agent to write laws from what you asked for (no extra account or API key). The exact launch command for each agent is still being confirmed during the v0.1 build.

What v0.1 covers

  • Python 3.10+, laws as @law pytest tests on Hypothesis (level L2), CrossHair as an experimental symbolic backend (L3).
  • init, draft, approve, check, strength, propose, report, plus hook and mcp for agents.
  • Permissions in lawkeep.toml, the law history (laws.lock + laws/history.jsonl), flags for unrecorded law changes and side-door files (conftest.py outside laws/, sitecustomize.py, .pth, stray .pyc, skip markers).
  • Saved failing inputs replayed on every run; fixed examples on pull requests; nightly deep search.
  • Law strength: quick check with dumb function versions, planted bugs (mutmut), vacuity, coverage.
  • The protection report on pull requests and at the end of each agent session.
  • A cheating test suite that plays known tricks against an example project.
  • Two made-up example projects.

Not in v0.1: other programming languages (TypeScript and others come last, v0.6), laws against a real database, fake outside services and replay, race conditions, machine-checked proofs (Dafny, v0.5), strict blocking mode (v1.0, optional), and any hosted service. See the roadmap in docs/PLAN.md.

Working on lawkeep itself

uv sync --all-extras                                          # install lawkeep + dev tools
uv run pytest -q                                              # the whole suite (~6-7 minutes)
uv run ruff check src tests && uv run ruff format --check src tests   # lint and format
uv run pyright                                                # type check
uv run lawkeep check                                          # lawkeep checks its own laws
  • Types: lawkeep is fully typed. src/lawkeep passes pyright in strict mode and the tests pass it in standard mode (settings in [tool.pyright] in pyproject.toml; basedpyright reads the same settings). The package ships a py.typed marker, so your editor and type checker use lawkeep's types in your project too.
  • lawkeep checks itself: this repository has its own lawkeep.toml, laws.lock and laws/ (laws about lawkeep.exitcodes), a guard hook for Claude Code and a lawkeep.yml workflow. Change those laws only through lawkeep propose / lawkeep approve, like in any project.

Learn more

  • docs/PLAN.md: design, principles, roadmap, risks, decisions.
  • docs/guide/: guides for non-technical users and developers, and law recipes.
  • docs/ARCHITECTURE.md: module contract for contributors.
  • AGENTS.md: instructions for coding agents working on lawkeep itself.

Licence: Apache-2.0.

Metadata

Release files for lawkeep 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lawkeep 0.1.0
File Size Uploaded Provenance Actions
lawkeep-0.1.0.tar.gz 668.9 kB Without provenance Details for lawkeep-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lawkeep 0.1.0
File Interpreter ABI Platform Actions
lawkeep-0.1.0-py3-none-any.whl Python 3 none any Details for lawkeep-0.1.0-py3-none-any.whl

Total release size: 990.4 kB

Release files / lawkeep-0.1.0.tar.gz

Download URL lawkeep-0.1.0.tar.gz
Size 668.9 kB
Tags Source
SHA-256 checksum
How to use checksums
68216d00756c727d6ff1bdc9cecf83284eea01d1a487dadff4983c35969558e2
BLAKE2b-256 checksum
How to use checksums
2eb9013a6e3282c4902996651d1a98830ac761b9de1e9a2fe6be75ac6cb398d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.11

Release files / lawkeep-0.1.0-py3-none-any.whl

Download URL lawkeep-0.1.0-py3-none-any.whl
Size 321.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
073ba5e3e23e05132e4c9db439a421d91d9ca90395fefc64f55d382a1861dea4
BLAKE2b-256 checksum
How to use checksums
38d252c84cf0c106ae9001ec4f0890b3e8e69fecad15b429591442bd4f48e4ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.11

Security for lawkeep 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page