# Self-improvement

This guide explains how one gvn harness improves another: it reads the other's recorded
runs, proposes a change, trials it, and asks a person before adopting it. At the end you
know which part of gvn provides each step, what a harness of yours must offer to be
improved this way, and what a demonstration showed: a small harness going from 3 of 6 tasks
passing to 6 of 6.

gvn's guiding principle is recursive self-improvement: a harness built with gvn should be
able to inspect, evaluate, change and re-verify harnesses built with gvn, including itself.
A **harness** is the program around a model: its agents, tools, policy and evaluation.

- **You will be able to:** name the part of gvn behind each step of that loop, say how the
  improver in [gvn-domains](../domains/index.md) (gvn's sibling package of example
  harnesses) uses them to improve another harness, and list what it relies on.
- **Read first:** [Replay and evaluation](replay-and-evaluation.md) and
  [Diagrams and diffs](diagrams-and-diffs.md); the improver is built from both.
- **Time:** about 15 minutes. This page is mostly explanation, with one short example.

[The demonstration](#the-demonstration) is reported in full below, so reading is enough. It
is a test suite in gvn's source repository (under `packages/gvn-domains/tests`). It ships
with the source, so running it takes a checkout of the repository; the installed packages
are enough for everything else on this page.

## Where this fits

Self-improvement is not a new box in the [big picture](../quickstart.md#the-big-picture). It
is a loop over boxes you already know: the **journal** records what runs did, the **actor**'s
machine is data that can be read and compared, the verifier checks any changed machine,
replay re-runs recorded events against it, and the **person who approves** decides whether to
adopt it.

```mermaid
flowchart LR
    observe["Observe<br/>journal and replay"]
    propose["Propose<br/>a change to the definition"]
    verify["Verify<br/>the verifier"]
    evaluate["Evaluate<br/>replay and an evaluation"]
    promote["Promote<br/>a person approves"]
    observe --> propose --> verify --> evaluate --> promote --> observe
```

The page first maps each step of that loop to the part of gvn that provides it, then shows
the improver, a harness from gvn-domains that runs the loop on another harness, and ends
with what the improver relies on.

## The loop

The design document describes the loop as five steps. Each one rests on a part of gvn you
have already met:

| Step | What it needs | What gvn provides |
|---|---|---|
| **Observe** | a complete record of what runs did | the journal: every event, in order, plus snapshots; [replay](replay-and-evaluation.md) reads it back as traces |
| **Propose** | a change that can be inspected before it runs | definitions are data: they can be exported, [drawn and diffed](diagrams-and-diffs.md) |
| **Verify** | proof that the change is still a complete machine | the verifier runs on every definition before it can exist; a [workflow](../concepts/workflows.md)'s plan is checked as a whole |
| **Evaluate** | a comparison on real history, offline | `Replay.against` feeds recorded events to the candidate; an `Evaluation` grades both; `Scorecard.regressions` names what broke |
| **Promote** | adoption without disturbing runs in flight | the saved [Dto](../reference/glossary.md#dto) identifies the control definition it ran on. Changes to an active control definition are checked on restore. Factory behavior can change under the same fingerprint, so test and migrate parked runs as described in [Designing a large harness](large-harnesses.md#test-the-boundary-at-several-levels). |

The same loop applies to gvn's own development: the fast test suite and the verifier are
what let an agent change gvn and know within seconds whether it broke something.

## The improver

`gvn_domains.meta.improver` is a harness whose job is to improve another harness. It is
built from the same parts as any other harness: a [workflow](../concepts/workflows.md) of
agents and tools, a policy, an evaluation. gvn-domains lists its harnesses in a browsable
**catalog**; the improver is one [catalog](../domains/index.md) entry like the rest, and
[its domain page](../domains/meta.md) shows its plan.

### What it may change

The improver never writes code. A target makes itself improvable by implementing
`Tunable`:

- `settings()`: the plain JSON configuration the target currently runs with;
- `menu()`: a **finite** tuple of `Adjustment`s, each a named, described, small change of
  settings, written by the target's author;
- `build(settings)`: makes a fresh actor of the target under any settings;
- `actor_class()` and `evaluation()`: what replay needs to rebuild recorded runs, and what
  a good run looks like.

Proposing means picking a name from the menu. Nothing else can be proposed: a proposal off
the menu is refused by the tool, and the proposer may choose again.

Here is one menu entry, and what it does to a target's settings. An adjustment's own
`settings` are only the settings it changes; `apply(current=...)` writes them over the
target's current settings and returns the result, the **applied** settings (tool outputs and
the `adopt` call carry them as `"applied"`). Then `Verdict`, the improver's judge, compares a
trial's baseline with its candidate and answers `better`, `no_better` or `worse` (trials and
the judge are explained in the next section):

```python
from gvn_domains.meta.improver import Adjustment, Verdict

budget = Adjustment(name="budget_4", description="Allow four model turns.", settings={"turns": 4})
print(budget.settings())  # {'turns': 4}
print(budget.apply(current={"turns": 3, "variant": "plain"}))  # {'turns': 4, 'variant': 'plain'}

trial = {"baseline": {"passed": 3}, "candidate": {"passed": 5}, "regressions": []}
print(Verdict().decide(trial))  # Symbol('better')
```

### How a run goes

A run of the improver is an `Improvement` workflow, started with a brief (the input a run
starts with) naming the target and a goal. Several of its stages are **errands**: an errand
([`gvn.harness.errand.Errand`](../concepts/errands.md)) is an actor that makes tool calls
fixed in code, with no model, under the same policy and approval as an agent's calls.
Observing, trialling and adopting are deterministic work, so they are errands. The run moves
through these stages:

1. **Observe.** An errand grades the target's recorded runs with the target's own
   evaluation. If nothing fails, the run ends `exhausted`: there is nothing to fix.
2. **Propose.** An agent (the only part that uses a model) reads the failures, the
   target's machine as a [table](diagrams-and-diffs.md#table), its settings and its menu,
   and every earlier trial of this run, then proposes one adjustment. Its tools only read,
   except `propose`.
3. **Trial.** An errand replays the recorded runs against the candidate and grades both
   sides. When the candidate changes the machine, the trial also reports the kernel
   `Difference` in words.
4. **Decide.** In a workflow, a stage's **judge** is the Decision that picks where the run
   goes after that stage. The trialling stage's judge asks `Verdict`: `worse` when
   any run that passed now fails, `better` when more runs pass and none regresses,
   `no_better` otherwise. `worse` and `no_better` go back to proposing; the judge answers
   `out_of_rounds` instead of anything but `better` once the stage has been visited `ROUNDS`
   times, so the loop is bounded in the machine, and `trial_failed` when the trial errand did
   not finish its work.
5. **Approve.** Adopting needs a person. The policy says `needs_approval` for `adopt`, and
   the request shows the person the settings and the evidence ("3/6 -> 5/6 runs pass").
6. **Adopt.** An errand records the new settings in a `Registry` (a port where adopted settings
   are recorded and read back by whoever deploys the target), keyed by the run
   (`"<run>#adopt"`), so a crash or an interrupt-and-resume records one adoption, not two;
   the registry refuses if something else was adopted since the run began. Deploying the
   adopted settings is up to whoever reads the registry.

The run's [context](../concepts/run-context.md) is the brief plus the run's name and the
target's settings at the moment it started. Every tool refuses, and the policy forbids, a
call for another target; whether the target moved before an adoption is checked by the
registry, atomically.

### What a trial shows

A trial replays **recorded events** against the candidate. As
[Replay and evaluation](replay-and-evaluation.md#what-replay-answers)
explains, that is exact for changes in how the actor handles those events (a turn budget,
the tools offered, a workflow's routing, joins and judges); what a model would have said
differently needs new runs.

Each `Adjustment` says whether it is
`replayable`. For one that is not, such as a change of instructions, the trial uses an
optional `Rehearsal` [port](../reference/glossary.md#port) (an interface the harness
calls, which you implement): the recorded briefs are run again, as new runs, under the
candidate settings, against a real or scripted model. Without a rehearsal those runs are
reported untestable and the verdict is `no_better`. Replay also leaves some runs open even
for replayable changes: a run that ran out of turns cannot show, from its recorded replies,
whether more turns would have helped. A rehearsal decides those too.

## The demonstration

`packages/gvn-domains/tests/test_dom_meta_demo.py` runs the loop end to end, offline, on a
small test target ("chores") with six tasks:

1. As shipped, three of the six tasks run out of turns: 3/6 pass.
2. The improver proposes a bigger budget. Replay keeps the three passing runs passing and
   leaves the three failures open; a rehearsal shows two of them now pass. A person
   approves, and the setting is adopted.
3. **New** runs of the target, built from the registry's settings, pass 5/6. These are not
   replays; the pass rate really went up.
4. Applied again, to the improved target and its new runs, the improver first finds that the
   same adjustment is now no change at all, then tries a bigger one, which fixes the last
   task. New runs pass 6/6.
5. Applied a third time, it finds nothing failing and ends `exhausted`, adopting nothing.

A second test applies the improver, unmodified, to the [support desk](../domains/service.md),
a harness it was not written for, through a `Tunable` wrapper in the test suite. There the
trial reports one candidate's machine change in the kernel's words ("transition
triaging/needs_human now goes to answering (was escalating)").

To run both yourself, from the root of a checkout of the repository:

```bash
uv run pytest packages/gvn-domains/tests/test_dom_meta_demo.py packages/gvn-domains/tests/test_dom_meta_support.py -q
```

## What the improver relies on

- **A menu written by the target's author.** Every possible change is on it. The improver's
  contribution is choosing, trialling and asking.
- **The target's evaluation.** The improver judges a trial by the target's own checks, so the
  checks decide what counts as a good run. In the support desk test, a refund request
  "resolved" from the knowledge base passes the desk's own evaluation; a check that sends
  refunds to a person is what makes the improver treat that as a failure.
- **A rehearsal for model behaviour.** Instruction changes, and runs that stopped before the
  interesting part, are decided by a rehearsal against a real or scripted model.
- **A person approving every adoption.** Adoption only records settings. Deploying them, and
  finishing the runs that started on the old settings, is up to whoever reads the registry.
- **Approval of `adopt` enforced twice.** The policy and the errand's machine require it,
  and because the errand asks for its calls with the standard request kinds, `Gated([ADOPT])`
  (`ADOPT` is the adopt tool that `gvn_domains.meta.improver` defines) can check it on every
  recorded run.
- **Scripted proposals in the demonstrations.** The tests use scripted models to make the
  proposals, so they run offline.

## Evaluating on new runs

Replay evaluates a change on recorded runs. When the change is to what a model is told, or to
what a tool returns, only new runs can show whether it helps: [Evals](../evals/index.md) run a
version of a harness on a set of cases, several times each, score every trial with code, a
judge model or a person, and compare two versions case by case. A comparison's verdict
(`gvn.evals.comparison.Verdict`) is a Decision with the outcomes `better`, `no_better`, `worse`
and `inconclusive`, so an improving workflow can route on it.

## Recap

- The loop is observe, propose, verify, evaluate, promote; the journal, definitions as data,
  the verifier, replay with evaluations, and fingerprints provide each step.
- The improver picks an adjustment from a finite menu its target's author wrote, trials it on
  recorded runs (and, when replay cannot tell, on a rehearsal), and asks a person before
  adopting.
- Its verdicts follow the target's evaluation and, for model behaviour, the rehearsal.

**Next:** to make a harness of yours improvable and run the improver on it, read
[the meta domain page](../domains/meta.md), which shows the improver's plan and how it is
built; to measure a change to a prompt or a tool on new runs, read
[Evals](../evals/index.md).
