lesscode

Simplify and shrink a codebase that AI wrote

Make the code simpler one step at a time, and know each step kept the behaviour.

You or your coding agent rewrite one function. lesscode-core runs your tests before and after. Then it checks whether your tests still watch that code. It copies the changed files, puts one small mistake into each copy, and sees whether a test fails. You get accepted or refused, and an exit code.

Your own files are never changed. Every planted fault lives in a copy in a separate work folder.

See three real refactorings Install the binary

  • 4 languages: Python, TypeScript, Java, Rust
  • 0 edits to your code: faults go into copies only
  • 1 binary that runs your own test command
a real step, in a TypeScript project
$ lesscode-core step check \
    --declaration step.json --work ../work
step shape-checks: refused
  src/state.ts: isPersistedWorkflowState:
  unnoticed faults rise from 1 to 7

# nine short tests, in a commit of their own
$ lesscode-core step pin \
    --declaration step.json --fault 4 --work ../work
#4 line 224 literal:false->true: admitted

$ lesscode-core step check \
    --declaration step.json --work ../work
step shape-checks: accepted
Example from the real tool, shortened paths.

A diff shows what changed. Lesscode shows what got simpler, and what your tests can no longer see.

Passing tests tell you that the code still runs. They do not tell you whether the tests still look at the part you changed, and a shorter diff does not tell you whether the code got simpler.

Reading a diff compared with a checked step
QuestionReading the diffA checked step
Did the behaviour stay?The tests pass, so you assume it did.The tests pass, and they still notice faults planted in the changed functions.
Is it simpler?The diff looks shorter, so you assume it is.Lesscode counts states, calls and the files that depend on each other, before and after.
Who decides?The reviewer decides by impression.An exit code. You and your pipeline get the same one for the same commit.

The three refactorings below show what that looks like in numbers.

Results

Three real refactorings, before and after

Each one reworks several functions or files in a real project. The numbers come from the example report in the repository. Where a number got worse the bar is amber, because a simpler type can cost a few tokens. Hover over a bar or tap it to see what it counts.

Rust · one file · fewer states

SharedFrame, one type

In a renderer, several threads share one frame. The step turns two fields of its type into one value with three cases, and it removes a flag that nothing ever read.

beforeafterEach measure has its own scale.

Why this is better. The type had 16 possible states and now has 6, and the unchanged tests reach all 6. Half as many planted faults go unnoticed. The price was 57 tokens. Lesscode refused the first attempt as too big and accepted the trimmed one.

The tests pass before and after. The author reported these numbers from recorded runs. Lesscode did not judge this one.

Python · seven files · fewer functions and calls

Lifecycle monitors, 7 files

This step reworks seven files of a finance application together. The project shows counts only, so its files and functions carry numbers instead of names.

beforeafterEach measure has its own scale.

file 1file 6file 7file 8file 9file 10file 11
Before: 69 functions, 89 call-graph edges, 34 cross-file call edges

Point at a dot or tap one to see its calls.

file 1file 6file 7file 8file 9file 10file 11
After: 50 functions, 61 call-graph edges, 28 cross-file call edges

Point at a dot or tap one to see its calls.

Each dot is a function and each line a call. Amber dashed lines are calls between files. The arrow keys move from dot to dot.

Why this is better. The same work now takes 19 fewer functions and 28 fewer calls. The tests pass before and after, and they notice the same planted faults.

The tests pass before and after. The author reported these numbers from recorded runs. Lesscode did not judge this one.

Rust · six files · judged by lesscode

Display path, 6 files

Five places held the answer to one question: does the GPU layer or the web canvas draw the remote picture? Two of them could disagree. Now one value holds it.

beforeafterEach measure has its own scale.

Why this is better. A fallback and a new session can no longer cross. Five pairs of files no longer depend on each other, and the tests notice three more planted faults. The step was not allowed to grow. Lesscode refused three attempts for size before it accepted this one.

The tests pass before and after. Lesscode accepted this step and measured the numbers with step check.

These three come from a larger report. Open the full report to see all six refactorings in three projects, with every metric and its source.

The check at work, 1

A cleaner function that the tests could no longer see into

A function checked six rules in two long conditions. The step writes one rule per statement. It reads better, and all tests still passed. Lesscode refused it.

  1. The rewrite

    Before the step, one planted fault in this function went unnoticed. After it, seven did. You could now break any single rule and no test would fail.

  2. The cause

    The tests had one case for a corrupt state file, and that file was not valid JSON. No test gave the function a file that parsed and had the wrong shape.

    The refusal names the function, the line and the fault, so you know which test is missing.

  3. The pin, then the step

    Nine short tests go in first, in a commit of their own. Each gives the function one misshapen file. step pin admits a test when it passes on the unchanged code and fails on the fault. After that, the same rewrite is accepted.

In the end the tests noticed 11 planted faults in this function, up from 3, and still missed 1. The file grew by 25 tokens, which is inside the 30 the step had declared. You can read the record of this step.

The check at work, 2

An acceptance that would have been wrong

In a Python project, _escape_html replaced five characters with five chained calls. The step calls html.escape instead, and lesscode accepted it.

The export had changed: html.escape writes ' for an apostrophe, the old code wrote '. No test noticed, because no test called the function with any of the five characters. The plan had said so in advance, with the words no fault noticed.

With two tests committed first and admitted by step pin, the same change was refused. The change that keeps ' was accepted, with seven lines removed and two added.

Where the plan says "no fault noticed", an acceptance tells you nothing until the function is pinned. The plan shows these functions before you start.

the same function, before and after the pins
Faults noticed and the verdict, without and with pins
StatenoticedunnoticedVerdict
No pins, html.escape015accepted, wrongly
Two pins, html.escapen/an/arefused: tests fail
Two pins, keeps '30accepted
These numbers are from the step escape-html. You can read the record of this step.

The path

Three things you do

You work in three moves, and each one is a single command. None of them edits your code. You or your agent write every change.

How it plants faults without touching your code

A planted fault is one small mistake made on purpose, such as a false turned into true. Lesscode never writes it into your repository. It copies your files into a work folder, the one you name with --work, puts the mistake into the copy and runs your tests there.

If a test fails, your tests notice that kind of mistake. If every test passes, a real mistake in the same place would get through as well. Lesscode does this once for each fault and reports the count. The method is known as mutation testing.

See what your tests protect

step plan plants faults in copies of your files and lists each function with the faults your tests let through.

$ lesscode-core step plan \
    --config run.json \
    --work ../work --out plan.json

Protect what you will change

Where a function has unnoticed faults, add tests first. step pin admits a test only if it fails on the fault. For many faults, step protect works through a whole batch.

$ lesscode-core step pin \
    --declaration step.json \
    --fault 4 --work ../work

Simplify, and let the check decide

Make one small change on its own branch. step check runs your tests before and after, and plants faults in the changed functions and their callers.

$ lesscode-core step check \
    --declaration step.json \
    --work ../work
two plans, planted faults by outcome
Each unnoticed fault is a behaviour a change could alter unseen. In the Rust file, twelve functions had no noticed fault at all.

An answer a pipeline can read

Every step command ends with one of four exit codes, so a script can act on the answer without reading any text.

Exit codes of every step command
ExitMeaning
0accepted, or done
1refused: the faults and functions are named
2not judged: a run was incomplete
3bad input, or work on disk that is not committed

The verdict is a JSON file that records the commits and file hashes it judged. You and your pipeline get the same answer for the same commit.

Made for the gate before a release

Every command runs unattended and answers with an exit code. It opens no page and asks no question. That fits the two places where you hold a release back: a Git pre-push hook on your machine, and a required check in GitHub Actions or another CI before you merge and tag.

$ lesscode-core step install-hook
$ lesscode-core step guard \
    --branch "$BRANCH" --revision "$COMMIT"

The hook stops the push of a step branch unless an accepted verdict covers exactly the files in it. Anyone can skip a local hook with --no-verify. Nobody can skip the same guard once the pipeline requires it. The boundary

Around the step

What else is in the binary

The checked step is the main thing. The same binary also does three jobs around it.

A report of several refactorings

step set puts refactorings from one project or several into one report: state, branches, call edges, test strength and size, before and after. step set-pdf makes the PDF. Every number says whether lesscode measured it or its author reported it.

Open an example report: six refactorings, three projects

A page that follows a run

With --open, a page in your browser follows the run and becomes its report when the run ends. Your own machine serves it, and it loads nothing from the network.

How the page works

A model, outside the tool

The commands above call no model. If a model writes the change, it does that inside your agent. A second binary, lesscode-model, measures whether an endpoint is good enough for a task set and tells you when it got worse.

The model endpoint guide

Limits

What an accepted step does not show

Lesscode is a research project with a public log of what worked and what did not. The check is mechanical. These things stay with you.

  • Accepted does not mean proven equal. The step passed your tests, and in the functions that ran, the tests notice as many planted faults as before. That is all it says.
  • It writes no test and no refactoring. It judges what a person or a model wrote.
  • A pin fixes a behaviour. Whether it is the behaviour you want is your call.
  • Nothing checks whether a step was worth taking, or whether the code reads better afterwards.
  • A compiled language takes time. One Rust file with 191 faults took 13.8 minutes with three copies. A second plan on unchanged code is immediate.

Questions

Simplifying code created with AI

These are the questions people ask first, with short answers.

How do I shrink an AI-generated codebase without breaking it?

In small steps, each on its own branch. For every step lesscode runs your tests on both sides and plants faults in the changed functions and their callers. A step that makes the tests blind to a behaviour is refused, however clean it reads.

Does lesscode rewrite the code for me?

No. There is no model inside the tool. You or your coding agent write the smaller version; lesscode measures and judges it. A skill file tells an agent how to work in checked steps.

What does simpler mean here?

Fewer states a type can be in, fewer functions and calls, fewer files that depend on each other. Lines and tokens only tell you how big a step was. See the three refactorings.

Install

One binary, signed releases

Lesscode is a single binary. One line installs it, and one file of your project is enough to try it.

Linux and macOS

$ curl -fsSL https://github.com/norandom/lesscode/releases/latest/download/install.sh | sh

Windows, PowerShell

> irm https://github.com/norandom/lesscode/releases/latest/download/install.ps1 | iex

Each release has archives for Linux and macOS on x86-64 and arm64 and for Windows on x86-64, with checksums and a signature. You can see all releases on GitHub.

Then try it on one file

  1. Write a run.json: your files, your test paths, your test command. The run configuration
  2. Run step plan on one file you want to change.
  3. Follow Refactoring in checked steps, or give your agent the skill for it.