Metadata-Version: 2.4
Name: emicode
Version: 0.1.1
Classifier: Programming Language :: Rust
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Topic :: Software Development :: Testing
Summary: Checks code changes made by AI coding agents and says whether to stop, repair or ask a person
License-Expression: PolyForm-Noncommercial-1.0.0
Requires-Python: >=3.8
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/amorfatii/EMI

# EMI

**When a coding agent says "done", EMI checks what was actually shown, and
sends the agent back if it wasn't.**

Coding agents often stop too early. The tests "pass" because one was edited,
skipped or deleted. A feature "works" but nothing tests it. A refactor
quietly changes what a function returns on an input nobody tried. The agent
doesn't need to be lying for this to happen; it only needs to be wrong and
confident.

EMI sits at the moment the agent claims it is finished. It runs your
project's tests on the code as it was before the task and on the agent's
change, runs your own rules against both, and gives one of four answers.
The answer comes from a fixed table, never from a language model, so the
same change always gets the same answer. **EMI reports; it never merges.**

![You give the task, the agent edits and says done, EMI checks: stop lets it finish, repair sends it back, ask a person waits for your approval, incomplete means something could not be measured](docs/assets/emi-loop.svg)

## The four answers

| Answer | Exit | What it means | What happens next |
|---|---|---|---|
| **stop** | 0 | Every check that ran holds. | The agent may finish. The receipt still lists what was *not* checked: stop is not "the task is done". |
| **repair** | 3 | Something the agent can fix: a test it broke, a rule of yours that fails, a test that shows nothing. | The agent is sent back with what failed and, where there is one, the exact input that fails. |
| **ask a person** | 4 | Something only you can approve: the agent changed an old test, a rule of yours, the test runner's settings or EMI's configuration. | Nothing the agent changed there counts until you approve it (`emi done --approve ...`). |
| **incomplete** | 5 | Part of the check couldn't be measured: a test run was cut short, a result didn't reproduce. | Never treated as a pass. The receipt says what was missing. |

## Try it

```sh
emi init                                   # set up: detects your project, checks the machine, connects your agent
git add .emi/config.json && git commit -m "Configure EMI"
emi done --format text                     # run a check by hand, any time
```

Here is a real check. The task said to round prices to the nearest dollar,
and the person had one rule in `.emi/laws`: *a discount never raises the
price*. The agent's rounding raises 50 cents to a dollar:

```text
Regression: held (1 tests ran).
New behaviour: not measured.
Laws: 0 of 1 hold, 1 broken by the change.
Action: repair
  .emi/laws/never_negative.rs::a_discount_never_raises_the_price is a law that held at
  the baseline and fails on your change; the laws are the person's and are restored
  for every check, so fix the code. It says: ... 50 at 0%
Baseline c1891faf6b (merge-base:main); candidate worktree; cooperative verification, sandboxed.
```

The project's own test still passed. The rule caught the problem, and the
agent is told the input that breaks it (50 cents at 0% off). If the agent
had "fixed" this by editing the rule, the edit wouldn't count: rules always
run as they were before the task, and an edit to one waits for you.

## What one check does

![Inside a check: the baseline, the agent's change, your laws and fixed settings go into a sandbox where six measurements run; their facts go to a fixed decision table that gives the answer and a receipt](docs/assets/emi-check.svg)

1. **Nothing broke.** Every test the project had before the task still
   passes, run in its *original* form. An old test the agent edited or
   deleted is put back, and a test that goes missing counts as broken.
2. **The new code is tested.** The tests the agent added are run on the
   old code too. A test that passes on both shows nothing about the change.
   A test that fails before and passes after is a *proving test*.
3. **Your rules hold.** Your *laws* are ordinary tests you keep in
   `.emi/laws`: examples, or properties tried on many generated inputs.
   They always run as you wrote them. A law that held must keep holding,
   and an input that once broke one is remembered and tried first in every
   later check ([laws.md](docs/laws.md)).
4. **Behaviour compared.** Every function the change modified is called
   with the same inputs on the old and the new code, and any difference is
   shown. If you said the change must keep behaviour (a refactor, `emi
   intent preserve`), a difference sends the agent back
   ([preserve.md](docs/preserve.md)).
5. **Tests that would notice.** Optionally, EMI plants small mistakes in
   the lines the agent added (`<` for `<=`, `+` for `-`). A planted mistake
   no test notices is a line the tests don't really pin down.
6. **No cheating code.** Code that asks whether it is being tested and
   behaves differently, a comparison that always says "equal", a silenced
   checker. EMI also runs the changed functions with the signs of a test
   run planted around them: one that answers differently is sent back.

Everything runs in a sandbox with no network, with random seeds, time zone
and locale fixed, the same for the old code and the new. Tests whose
outcome differs between two runs of the old code are set aside rather than
trusted. The facts go to a small decision table in `emi-kernel`, which does
no I/O at all. The receipt records every input, and `emi replay` recomputes
it from nothing cached ([determinism.md](docs/determinism.md)).

Before the agent edits a file, it can also be told what earlier checks found
about it: rules that run through that function, planted mistakes that went
unnoticed, behaviour that changed (Claude Code; `emi reach FILE` by hand).

## Languages

| What EMI measures | Python (pytest) | Rust (`cargo test`) | TypeScript / JavaScript (vitest) |
|---|---|---|---|
| Nothing broke | yes | yes | yes |
| Your rules (laws) | yes: examples, Hypothesis properties and state machines, inputs counted, failing inputs remembered | yes: tests and proptest properties | yes: vitest files |
| The new code is tested | yes | not yet ([proposal](docs/adr/0024-rust-change-proving-runs-added-tests-on-the-old-code-with-coverage.md)) | not yet |
| Behaviour compared | yes | not yet | not yet |
| Planted mistakes | yes | yes, up to 5, against your laws | not yet |
| Cheating code | yes, by pattern and by running it | by pattern | by pattern |
| Rules drafted with you before coding | yes | not yet | not yet |

What isn't measured is never reported as passed: the receipt says "not
measured" and why. Python needs pytest and coverage.py 7.9 or newer in the
project's environment, and Hypothesis for property laws.

## Where it runs

**On your machine** (the *cooperative* setting) the check runs when the
agent tries to stop. Hooks are included for Claude Code, OpenAI Codex CLI,
GitHub Copilot CLI and OpenCode V2 ([hook.md](docs/hook.md)):

```sh
emi hook install --harness claude-code     # or codex, copilot, opencode
```

On your machine the agent could, in principle, tamper with EMI's own
inputs. This setting catches a careless agent, not a determined cheat, and
every receipt says which setting it ran in.

**In CI** (the *enforced* setting), the configuration and your laws are read
from the base branch, and approvals made on a laptop don't count. A GitHub
Action and a GitLab template are included ([ci.md](docs/ci.md)). EMI's own
pull requests are checked this way, as a job that reports but doesn't block
yet.

| Platform | How a check is sealed |
|---|---|
| Linux | [bubblewrap](https://github.com/containers/bubblewrap): its own filesystem view, process table and no network. The full check. |
| macOS | macOS's own sandbox. Weaker (the run can see other processes), and the receipt says so; in CI such a pass goes to a person ([ADR 0023](docs/adr/0023-macos-checks-run-in-its-own-sandbox-as-a-weaker-check.md)). |
| Windows | Inside WSL2, as Linux. |

Without a sandbox (`"isolation": "none"`, or bubblewrap missing), a pass on
your machine still lets the agent stop, marked as a weaker check; in CI it
goes to a person.

## Install

Once a release is published:

```sh
pip install emicode
```

Until then, build it (Rust 1.90 or newer):

```sh
git clone https://github.com/amorfatii/EMI.git
cd EMI
cargo install --locked --path crates/emi-cli
```

On a terminal, `emi init` walks through four screens: what it detected in
the project, whether this machine has what checks need (each missing piece
with the command that installs it), which agent to connect, and what it
will write. Nothing is written before the last Enter. `--plain` gives the
same as text, and `--yes` writes without asking. More in
[install.md](docs/install.md).

## Commands

| Command | What it does |
|---|---|
| `emi init` | Set up a repository: detect the test command, check the machine, connect an agent, write `.emi/config.json` |
| `emi done` | Check the working tree (or `--candidate REF`) against where the task started; print the receipt |
| `emi done --approve ITEM` | Approve a change only you can approve: an old test, a law, runner settings, agent instructions, EMI's config |
| `emi done --withdraw ITEM` | Take an approval back (the record keeps both) |
| `emi done --preserve [PATH]...` | Also require modified functions to keep their behaviour |
| `emi intent preserve\|clear\|show` | Record that requirement for this repository, for the hook |
| `emi rules brief\|check\|approve` | Draft your rules with the agent before any code, see them as plain cases, approve them ([rules.md](docs/rules.md)) |
| `emi reach PATH[::FUNCTION]` | What earlier checks found about a file or function |
| `emi replay RECEIPT` | Recompute a receipt from scratch; exit 0 if the decision is the same |
| `emi hook install\|uninstall` | Run the check when the agent tries to stop |
| `emi journal report` | What the hook did, and your labels on its interventions (useful, noise, wrong) |

The full reference for checks and receipts is [docs/done.md](docs/done.md).

## How we know it works, and what isn't known yet

The evals ([evals.md](docs/evals.md)) are fixed changes to small projects,
each with the answer EMI must give, run on every commit:

- **tamper** hides a wrong change the ways agents have been seen to: an
  edited, deleted or skipped test, a `conftest.py` that reports failures as
  passes, a weakened law, an early exit, code that detects the test run.
  EMI must not answer stop.
- **honest** holds right changes (a refactor, a fix with its test, a
  comment) that EMI must accept. A check that blocks good work is wrong too.

Cases EMI gets wrong stay in the suite, marked as known gaps.

Not established yet: whether EMI lowers how often agents claim false
completions in real use, at what cost in time and tokens. That experiment
hasn't been run. Its design lives in a separate validation repository.

## Contributing

```sh
scripts/check.sh --fast    # formatting, lints, kernel purity, fast tests (about a minute)
scripts/check.sh           # everything CI runs, including real pytest, cargo and vitest projects
```

[CLAUDE.md](CLAUDE.md) has the rules the code follows, [CONTEXT.md](CONTEXT.md)
the vocabulary, and [docs/adr](docs/adr) every design decision and why.

<details>
<summary>Crate layout</summary>

| Crate | Role |
|---|---|
| `emi-kernel` | The decision: the facts, the action table, the policy id; canonical JSON, hashing, pins. No I/O. |
| `emi-done` | The check itself, the same for every language: configuration, baseline, regression, laws, behaviour comparison, receipts, replay. Names no language. |
| `emi-driver` | The boundary between the check and a language: the `Driver` trait and what all languages share |
| `emi-python` | The Python driver: pytest, coverage, proving tests, behaviour comparison, mutants, signals |
| `emi-rust` | The Rust driver: `cargo test` read test by test, laws as test targets, signals and mutants from tree-sitter-rust |
| `emi-typescript` | The TypeScript driver: vitest through EMI's reporter, laws as vitest files |
| `emi-exec` | Running processes: the sandbox (bubblewrap, macOS sandbox), process cleanup that is checked, not assumed |
| `emi-tree` | Snapshots of commits and working trees, fresh copies to run in, line-level diffs |
| `emi-git` | The one git runner, with repository config and hooks switched off |
| `emi-hook` | The agent adapters (Claude Code, Codex CLI, Copilot CLI, OpenCode V2) and repair rounds |
| `emi-cli` | The `emi` binary |

</details>

## License

[PolyForm Noncommercial 1.0.0](LICENSE): free to use, change and share for
any noncommercial purpose, including personal projects, research, study,
and use by charities, schools, public research bodies and government.
Commercial use needs a separate license; ask the author.

