# Editing a harness

**Who this page is for:** developers changing a harness from the Workbench, and anyone who
reviews such a change or a change an agent proposes. The last section is for developers who
write harnesses that are easy to edit.

The Workbench edits a harness by patching the project's own Python files. There is no second
copy of the harness to keep in step: Python is the source of truth. Edits made on a drawing or
in a setting's panel, or drafted by an agent, collect into an **edit group**. The group is
shown as a diff before anything is written; the changed project is loaded in a fresh process
and checked by gvn's verifier; only then are the files replaced, and the Workbench comes back
up on the changed code.

- **You will be able to:** tell what can be edited where, read a preview (the diff, what else
  changes, the verification), save, follow the suggestions a preview offers until the group
  verifies, approve or
  discard an agent's proposal, and write harness code that edits well.
- **Read first:** [Reading a harness drawing](drawings.md).
- **Time:** about 20 minutes.

## How an edit travels

```mermaid
flowchart LR
    you(["You, or an agent"])
    group["An edit group<br/>set a value · add a route ·<br/>rename a state · …"]
    source["The project's Python<br/>found by reading the source"]
    preview["Preview<br/>diff · also changes · notes"]
    fresh["A fresh process<br/>loads the changed project:<br/>every plan built, every<br/>definition verified"]
    save["Save<br/>each file replaced atomically"]
    restart["The Workbench<br/>starts again on the new code"]
    you --> group --> source --> preview --> fresh
    fresh -- "no problem" --> save --> restart
    fresh -- "a problem" --> preview
```

The drawing is always the machine that runs: the Workbench reads it from the loaded project,
as [Reading a harness drawing](drawings.md#where-a-drawing-comes-from) says. Reading the source
only decides **where** a part of the harness is written, and so whether it can be edited here.
Code the Workbench cannot edit is still drawn, and is never restructured.

## What can be edited where

Every editable part of a harness is a **target**: a state, a transition, a stage and each of its
settings, a join and each of its settings, the constructor call that builds a stage's child and
each of that call's settings. For each target the Workbench says one of three things, with the
file and lines it is written at:

| Tier | Shown as | When |
|---|---|---|
| `own` | edit here | The place is found exactly, and it is a literal, or a constant read only there. |
| `shared` | edit here, also changes … | The place is found exactly, but more than one part of the project reads it: a constant read elsewhere too (in any of the project's modules, however it is imported), a blueprint chain two plans are built on, a factory several stages use, a stage an eval version reuses. The edit lists everything else it changes, and a save needs you to acknowledge each. |
| `code` | defined in code | Anything else: a function, a reference to an object, a computed value, a loop, a table. The Workbench shows the file and line and one sentence saying why. An agent can draft a patch for it. |

From the release review's entry workflow (`src/release_review/review.py`). Its `candidate`
version (`candidate.py`) is built on the same machine and reuses every stage but the
cross-check, so most of the workflow is shared with it:

| Target | Tier | Why |
|---|---|---|
| `stage:cross_checking:empty` (`"no_cross_checks"`) | edit here | a literal in `FanOut(...)`; the candidate has a cross-check stage of its own |
| `transition:awaiting_approval:release_approved` (where approval leads) | edit here, also changes … | the route's literal in `.branch(...)`, also the candidate's transition |
| `stage:awaiting_approval:expired` (`approval_timed_out`) | edit here, also changes … | a literal in `Host(...)`, in a stage the candidate reuses |
| `join:reviewing_components:early` (`["blocked"]`) | edit here, also changes … | a literal in `Worst(...)`, in a stage the candidate reuses |
| `stage:reviewing_components:window` (`2`) | edit here, also changes … | the constant `COMPONENT_REVIEW_WINDOW`, also read by the plan's `settings` (line 460) and by the candidate's (`candidate.py`, line 93) |
| `child:reviewing_components:instructions` | edit here, also changes … | the constant `COMPONENT_REVIEW_INSTRUCTIONS`, also read by the plan's `settings` and by the candidate's |
| `stage:reviewing_components:items` | defined in code | the function `list_components` |
| `child:reviewing_components:answer` | defined in code | the object `Record(...)` built at line 116 (`VERDICT`) |

Each target also lists the edits it takes, so a drawing offers only those. A setting left at
its default is not written in the source, so it is not a target of its own: the stage, join or
child constructor that takes it lists it among its settings not written yet, with the
constructor's default, and setting a value writes it as a new keyword argument of that call. A
child's run context (`context=`) is run-time data and never a target.

An eval version has a listing of its own, in the same terms. A version that runs the harness's
own class, such as the release review's `baseline`, is that harness: its listing says so, and
editing one never asks to acknowledge the other. The `candidate` lists its own plan and
cross-check stage as edit here, and everything it reuses as shared with `release_review`.

## The edits

An edit group holds any number of these, in order:

| Edit | What it does |
|---|---|
| Set a value | Writes a new value where a stage's, a join's or a child's setting is written: text, a number, a flag, a symbol, a list. A setting not written yet is added as a keyword argument, after the call's last one and in its layout. |
| Add, change or remove a route | Routes a child's final state to a workflow input, routes it elsewhere, or removes the route. |
| Add, remove or retarget a transition | Adds `.on(...)` (with its output, on a Mealy machine), removes one, or points it at another state. Removing a route of a `.branch(...)` also takes its outcome out of the branch's `Alphabet.of(...)`, since a branch routes every outcome it names; removing its last route removes the whole `.branch(...)`. |
| Add a state | A new state with a `Host` stage running an Agent, its two functions and their imports. |
| Remove a state | The state, its transitions and its stage, in every plan built on the same machine. Transitions into a working state go where its first route led; a final state is removed once the group leaves nothing leading to it. |
| Rename a state | The state, everywhere its machine is used. |
| Reorder states | The order of the `.state(...)` calls, which is also the order of a drawing's layout. |
| Swap a join | Replaces a stage's join class with `AllOf`, `AnyOf` or `Quorum`. |
| Remove inputs, remove outputs | Removes inputs nothing reads, or outputs nothing emits, once the whole group is made. A group that still uses one is refused, naming the transition, state or stage that uses it. |

Renaming or removing a state reaches every plan in the project built on the same machine. In
the release review, `ReleaseReviewCandidate` (in `candidate.py`) is built on the same machine
as `ReleaseReview`, so removing `cross_checking` patches both plans.

## Preview

A preview answers, and writes nothing:

- **each edit in a sentence**, such as "Set the window of the stage of reviewing_components to
  3", with its kind, owner and target beside it for programs;
- **the diff** of every file it changes, as text you can read like any code review;
- **what else changes**, for every `shared` target, in a sentence each ("the same setting of
  the version candidate", "COMPONENT_REVIEW_WINDOW is also read at
  src/release_review/candidate.py:93"), which a save needs acknowledged;
- **notes** on what the diff alone does not show: another text still equal to a state's old
  name ("src/…:12 still reads the text 'old'; if it means the state, change it too", which
  still verifies and fails only when run), a comment left above a line that is gone, a line
  now longer than the project's line length, another plan on the same machine with no stage
  for a new state, a comment a rewrite moved;
- **the verification**: the changed project, loaded in a fresh process;
- **what the group still needs**: further edits, ready to add, and what only a person can change
  by hand (see [Follow-up suggestions](#follow-up-suggestions)).

Setting the review window to 3 gives this diff, and says that it also changes "the same setting
of the version candidate" (the candidate reuses the reviewing stage), and that
"COMPONENT_REVIEW_WINDOW is also read at src/release_review/review.py:460" and
"… at src/release_review/candidate.py:93":

```diff
--- a/src/release_review/review.py
+++ b/src/release_review/review.py
@@ -95,3 +95,3 @@
 COMPONENT_REVIEW_TURNS = 2
-COMPONENT_REVIEW_WINDOW = 2  # components reviewed at the same time
+COMPONENT_REVIEW_WINDOW = 3  # components reviewed at the same time
 COMPONENT_REVIEW_ATTEMPTS = 2  # a review that fails is tried once more
```

The patch changes only the text it has to. No formatter runs; comments, line endings and
indentation are kept. A patch that would lose a comment is refused rather than made. Where the
comment sits inside the text or list being replaced, or between the names of `.inputs(...)` or
`.outputs(...)`, the refusal offers to rewrite that one statement: accepted, the value is
written again and each comment moves, unchanged, onto its own line above the statement, shown
in the diff and named in a note. Code outside the editable shape is never rewritten.

### Verification in a fresh process

Before anything is written, the changed files are put in a shadow copy, and the project is
loaded from it in a new Python process: every plan of every harness and of every version is
built, every definition is examined by the [verifier](../reference/glossary.md#verifier), and
every edit is read back to check it took effect. For the window edit:

```text
The changed project loads in a fresh process; 3 plans built and 6 definitions verified.
```

A problem is shown in place, with the verifier's findings (their stable
[codes](../concepts/machines.md#deeper-every-verifier-finding)), the harness and drawing it is
in, and its file and line. A project that cannot load, a process that fails or one that runs
past its time limit is one problem, in words.

### Edits that change the same text

The edits of one group may reach the same text. Pointing a transition elsewhere and removing
the state it leaves from is one group: the transition goes with the state, and a note says so:

> 'Point the transition from cross_checking on cross_checked at needs_attention' is covered by
> removing the state cross_checking

The same change made twice is made once, and removals of inputs (or outputs) of one machine
are made together. Two edits that set the same thing to different values are refused, naming
both ("… set the same thing in src/release_review/review.py to different values; keep one of
them"). Any other overlap is refused naming both edits, and saying to save one of them first
and then make the other.

### Follow-up suggestions

Removing a state often leaves the rest of the machine needing changes too: a transition still
leading to it, an input nothing reads any more, an output nothing emits, a stage whose route
still ends with a removed input. The preview works out every further edit the group needs from
three things: the verifier's findings, the plan checks' problems and what the edits of the
group require of each other. Each one is a ready edit with the reason for it; they are offered
together, each placed with the group and the others, so that adding them all and previewing
again moves the group to one that verifies. Suggestions are never applied on their own.

Removing `cross_checking` from the release review leaves four inputs no transition uses and one
output nothing emits, so the preview fails with five findings (`unused-input cross_checked`, …,
`unused-output cross_check_findings`) and suggests two edits:

> Now no transition uses the inputs cross_check_blocked, cross_check_failed, cross_checked and
> no_cross_checks: remove them.
>
> Now nothing emits the output cross_check_findings: remove it.

Add both to the group, preview again, and the verification passes.

Removing the final state `incomplete` takes two rounds. Alone, the group is refused, because
the `validating` stage still leads to `incomplete`; the preview suggests the edit that removes
that transition (its route and its outcome in the branch's `Alphabet.of(...)`):

> Remove the transition from validating on checklist_incomplete, because incomplete is still
> reached from src/release_review/review.py:146.

With it, the machine no longer uses the input `checklist_incomplete` or the output
`incomplete`, and the checklist gate's own `incomplete` ending still ends the stage with that
input, so the preview suggests three edits together:

> Now no transition uses the input checklist_incomplete: remove it.
>
> Now nothing emits the output incomplete: remove it.
>
> End the stage of validating with checklist_complete instead when its child ends in
> incomplete, because checklist_incomplete is still what the stage of validating ends with
> when its child ends in incomplete (src/release_review/review.py:383).

Add them, preview again, and the verification passes. A `Host` routes every final state of its
child, so the route goes to another symbol the stage ends with rather than away: read that
suggestion before saving, and change it if another ending fits better.

Some of what a group needs is code no edit changes: a judge's `Decision` class that still
answers an outcome, a branch whose outcomes are a constant, a name written where the source is
`code`. The preview says each in one sentence with the file and line to change by hand.
Removing the final state `approved` with its transition leaves `release_approved` unused, but
the release manager's answer is read by a judge:

> release_approved is still produced by ReleaseDecision, the judge of the stage of
> awaiting_approval (src/release_review/review.py:222); change that class by hand so it no
> longer answers release_approved, or remove the state awaiting_approval in the same group.

## Save

Saving runs the same checks as the preview, then replaces each file:

1. Files that changed on disk since the preview are not written over. The edits are placed
   again in the new text, and the answer shows the new diff to look at, or names the edit that
   no longer finds its place.
2. Every `shared` change must be acknowledged.
3. The changed project is verified in a fresh process; any problem writes nothing.
4. Each file is replaced atomically, one after another. If one cannot be written, the files
   replaced before it are put back. A failed save leaves every file byte for byte as it was.

The answer lists each file with the lines added and removed.

## Restart

The Workbench still has the old code imported when a save ends. Started with `gvn eval serve`,
it runs under a supervisor that keeps the port and both tokens and starts the server again on
the changed code after every save. The page notices the restart and reads everything again:
"The project changed and the server restarted; the page read everything again." If the changed
code cannot start, the address answers with what failed, and the Workbench can be asked to try
again once the code is fixed.

Started from Python with `gvn_workbench.app.serve(project)`, without the project's address,
the Workbench runs in one process, and a save says that it has to be stopped and started again.

## Proposals from agents

Agents propose; people approve. The guide or a coding agent drafts an edit group with the
`propose_edits` tool. The tool answers the same preview a person sees, and keeps the group as a
**proposal** under `.gvn/workbench/proposals/`. It never writes a project file.

A proposal is listed with its note, its edits in sentences, the files it touches, whether it
verified, and what else it changes. A proposal whose files changed since it was made is marked
**stale**, with the files. A person reviews it like any preview and saves it, acknowledging what
it also changes, or discards it. Only a request from the page, carrying the page's own token,
saves a proposal: the token an agent holds reaches `/mcp` and nothing else (see
[Files and safety](files-and-safety.md#two-tokens)). A saved proposal is deleted.

## The layout of a drawing

The positions of the boxes on a drawing are kept beside the project, in
`.gvn/workbench/layouts.json`, one entry per drawing. They are never written into the
project's Python.

## Writing a harness that edits well

Everything works on any harness; this shape makes more of it **edit here**. It is the shape of
the release review.

**Name each tunable value once, as a constant, or write it as a literal in the call.** A
constant read in one place is `own`:

```python {.no-run}
COMPONENT_REVIEW_TURNS = 2

def make_component_reviewer(name: str, component: str, memo: Memo) -> Agent:
    """The agent that reviews one component and answers pass or block, with a reason."""
    context = {"release_id": memo.input()["release_id"], "component": component}
    return Agent(
        name,
        COMPONENT_REVIEW_INSTRUCTIONS,
        turns=COMPONENT_REVIEW_TURNS,
        answer=VERDICT,
        context=context,
    )
```

The release review also lists its constants in `Plan(settings=...)`, so that a stored run is only
ever continued by the code it started on ([Settings](../concepts/workflows-in-depth.md#settings)).
That second reading is why its constants are `shared`: an edit changes both, and says so.

**Build each child with one constructor call inside `make`.** The Workbench finds that call
(`child:<stage>`) and its settings (`child:<stage>:instructions`, `turns`, `model_role`, …). A
`make` that builds its child in a loop, through a table or through several assignments is
`code`.

**Write the machine as one blueprint chain, and stages as module constants.** `.state(...)`,
`.on(...)` and the routes of `.branch(...)` written as literals are each a target:

```python {.no-run}
RELEASE_REVIEW = (
    MooreBlueprint("release_review")
    # ...
    .branch(
        "awaiting_approval",
        Alphabet.of(
            "release_approved", "changes_requested", "approval_withdrawn", "approval_timed_out"
        ),
        {
            "release_approved": "approved",
            "changes_requested": "needs_changes",
            "approval_withdrawn": "needs_attention",
            "approval_timed_out": "needs_attention",
        },
    )
    .otherwise_stay()
    .build()
)

ASK_RELEASE_MANAGER = Host(
    Inquiry,
    make=make_release_question,
    start=start_release_question,
    judge=ReleaseDecision(),
    deadline=approval_deadline,
    expired="approval_timed_out",
)
```

**Keep functions for what is code.** `make`, `start`, `judge`, `items`, `count` and `deadline`
are always `code`: they are behaviour, and the Workbench shows where they are rather than
editing them.

## Recap

- Python is the source of truth; an edit group becomes a patch on the project's files.
- Every target is `own` (edit here), `shared` (edit here, also changes …) or `code` (defined in
  code, with file and line).
- A preview shows each edit and what else it changes in sentences, the diff, notes and the
  verification of the changed project in a fresh process; it suggests every further edit the
  group needs, and says what only a person can change by hand, with its file and line.
- A save writes nothing unless every check passes, replaces files atomically and never over a
  file changed since, and the Workbench starts again on the new code.
- Agents propose with `propose_edits`; a person saves the proposal from the page.

**Next:** [The guide and coding agents](guide-and-agents.md).
