Skip to content

Coding ​

An agent that edits code does more than write files. It has to show what it changed, check its own work, and let you take a change back. Phase 16 does this in golem around one thing, the change set, and every screen is a view of it. So how does one edit get from the model to the file, the store, and the screens?

Mostly planned

Phase 16 (#934) is not built yet. What exists today is write_file and edit_file, with the diff in the approval, described in Tools. Every other part below is marked planned, with its ticket. When a ticket merges, its mark goes.

The idea ​

In agents generally

A coding agent changes files on your behalf, often many files in one request, and often wrong on the first try. Four things make that safe:

  1. A record. Every write keeps the text before and after, so the agent, and you, can say what changed.
  2. A view. The changes show as diffs, as they happen and as a list at the end.
  3. A check. The agent runs the project's own tests or linter and reads the result, so a mistake shows up before you have to find it.
  4. A way back. Any change can be taken back, at the size you choose.

Small models need two more things. They miss an exact match by a space, so an edit must tolerate harmless differences. And a diff in every tool result fills their short context, so the model should get a short answer while the screen gets the diff.

flowchart LR
  e[An edit] --> r[Recorded]
  r --> v[Shown as a diff]
  r --> c[Checked]
  r --> u[Can be undone]

How agents usually do it ​

ApproachHow it worksGood atCosts
Git is the recordThe agent edits files and you use git diff and git restoreNothing new to learnNeeds a git repository. Cannot tell which tool call made a change. Your own edits mix in
A shadow repositoryThe agent keeps a second git repository of its own, and snapshots into itWorks with the git tools, and leaves your history aloneOne more repository to keep, and still one snapshot per step, not per tool call
Its own snapshotsThe agent stores the text before and after each writePer call, and works with no gitThe agent owns the store and its cleanup
Language serverThe agent asks a language server for errors after an editFast, and exactOne server per language to set up
Project commandsThe agent runs the project's lint, type check, or testsUses what the project already trustsSlower, and a test suite can take minutes

Open questions in every agent: what to do when a file changed on disk under the agent, and how much of a diff the model itself needs to read.

How golem does it ​

In golem

golem keeps its own snapshots in its blob store, and runs the project's commands as checks. The decisions are in the phase issue (#934) and in ADR 057, the change set. One thing in the daemon, the change set, and every screen is a view of it.

The change set ​

Planned: #948 to #952.

A change is one write to one file, a FileChange record:

FieldMeaning
change_id, session_idIts id, and the session
run_id, call_idThe turn and the tool call. Empty for a shell change or an undo made between runs
pathRelative to the session folder, with /
before_sha, after_shaBlobs of the text before and after. before_sha is empty when the file did not exist, after_sha when it was deleted
added, removedLine counts
sourcetool, shell, or revert
revertsThe change an undo took back
tsISO time, UTC

The records go in the table file_changes in golem.db. The texts are blobs of kind file, kept once under their sha256 by the blob store (store/blobs.py). The event file.changed carries the id, the call, the path, the status (created, modified, deleted), the counts, the source, and reverts. It never carries file text.

The net change of a file in a session is its first before_sha against its last after_sha. A file whose two ends are equal is not listed. changes.list returns the net files with a stale flag, and changes.diff returns a unified diff in the format of unified_diff in tools/write.py.

sequenceDiagram
  participant Model
  participant Tool as edit_file
  participant You as Approval
  participant Store as Blobs and file_changes
  participant Screens as Web and TUI
  Model->>Tool: edit_file(path, old_string, new_string)
  Tool->>You: preview, the planned diff
  You-->>Tool: allow
  Tool->>Tool: commit, write the file
  Tool->>Store: blobs for before and after, a file_changes row
  Store-->>Screens: file.changed
  Tool-->>Model: edited src/fetch.py (+18 -4)

Undo ​

Planned: #953 to #956.

changes.revert takes four kinds of target:

TargetWhat it does
changeWrites back the text before that change. On an undo's own change this is a redo
fileWrites back the text the file had before its first change in the session
hunkTakes back one hunk of the file's net diff, by index
runRewinds to before that turn: every file changed in it or a later turn goes back
  • An undo is a change. It writes the old text and records a change with source: "revert", so it shows in the list and can be undone. Nothing is lost.
  • stale. A file is stale when what is on disk is no longer what golem last wrote. That is a conflict: without force, nothing is written for it and the result lists it.
  • Why a turn undo rewinds. Taking back a middle turn alone could leave a file that no turn ever wrote, because a later turn built on it. So undoing a turn also takes back the turns after it.
  • A revert while a run is active is refused. The chat is kept. The model reads a short note before the next user message that names the files put back (the first 20, then "and N more"). The graph state field changes_seen holds the last change the model was told about, so each undo is told once.

Edits that land ​

Planned: #957 to #962. Today edit_file needs an exact match.

A small model often gets old_string right in substance and wrong by a space. edit_file will try three rungs in order, and stop at the first that matches:

RungWhat it ignoresExample
1. ExactNothingreturn x matches return x
2. Trailing spacesSpaces and tabs at the end of a linereturn x matches return x
3. Base indentationThe indent shared by all the linesThe model sends if a:\n b() at column 0, and the file has it inside a function at column 4. The new text is indented the same way
  • More than one match on a rung is an error, never a guess. The model is told, and adds context.
  • On a miss, the error says which lines of the file come closest, so the model can copy them.
  • What the model gets back is one line, edited src/fetch.py (+18 -4). The screen gets the diff from tool.completed and file.changed. Today the model reads the whole diff, so every edit is paid twice in its context.
  • Progress. While the model is still writing a write_file or edit_file call, tool.writing says which file and how many lines so far, at most once a second per call, so a slow write never looks like a hang. It is a live signal like message.delta: golem.db deletes it when the message completes. A provider that sends the whole call at the end sends no tool.writing.

Checks ​

Planned: #972 to #975.

  • Where the commands come from. [coding] check entries in config, or what golem finds in the folder: a check recipe in a justfile, lint and typecheck scripts in package.json, ruff in pyproject.toml, cargo check, go vet. Tests run only when configured, because a suite can take minutes.
  • When. After the model's last reply of a turn, if a file tool or the shell changed something in that turn. They run in the shell sandbox, with no network.
  • Consent. [coding] checks is ask, auto, or off. The default, ask, asks once per session. In autopilot mode ask acts like auto.
  • The feedback loop. A failed check goes back to the model as a message and the run continues, at most fix_rounds times per turn (default 2). check.started and check.completed are run-scoped events.
flowchart LR
  call_model -->|tool calls| tools
  tools --> call_model
  call_model -->|no tool calls, files changed| checks
  checks -->|failed, rounds left| call_model
  checks -->|passed, or rounds used| END
  call_model -->|no tool calls, nothing changed| END

Plan first ​

Planned: #979 and #980.

  • The flag. plan_first is a session flag set with session.set_plan. It is not a fourth permission mode, and combines with any of them.
  • The tool filter. While it is on, the model sees only the read-only and internal tools, plus propose_plan. A call to any other tool is refused.
  • propose_plan. Takes {title, steps: [{text, files}]} and sends plan.proposed. "Build it" turns the flag off and sends Build it. Follow the plan step by step and keep the todo list current.

Screens ​

Every screen reads the same events and calls. The facts come from steps #939 and #940. packages/view/src/changes.ts (#951, planned) folds file.changed into the changes of each turn for both.

WhatWebTerminal
The diff of an editToday: in the approval. Planned: a card in the chat with the path and +18 −4 that opens the file in Changes (#965)Today: in the approval band. Planned: a short diff under each edit (#969)
The list of changesA right panel with tabs Changes, Files, Checks, Todos. A tab appears when its ticket ships (#963). Changes switches between by file and by turn (#964, #968)/changes, a full-screen review file by file and hunk by hunk, built like /mcp and /skills (#971)
A sum of the turnThe by-turn view3 files +43 −4 · /changes · /undo at the end of a turn (#970)
Any file of the projectThe Files tab, through file.read (#966, #967)None planned in this phase
UndoButtons in Changes (#954)/undo and keys in /changes (#955)
ChecksThe Checks tab and a strip under the turn (#975)A line under the turn (#975)
Plan firstA switch beside the mode chip and a plan card (#980)/plan

Today the web has a 20 rem drawer with the todos and the trace inspector, and the TUI has the approval band's diff. The panel replaces the drawer.

Where it lives in the code (today):

  • packages/daemon/golem/tools/write.py: plan_write, plan_edit, commit, unified_diff.
  • packages/daemon/golem/store/blobs.py: BlobStore.
  • packages/web/src/features/diff/ and packages/tui/src/diff-view.ts: the diff drawn in the approval.
  • packages/view/src/diff.ts: the diff logic both clients share.

Why this way ​

Own snapshots, not a shadow git repository (ADR 057, the change set). The text before and after each write goes to golem's blob store. It works in a folder with no git, never writes into your .git, and records per tool call, which git cannot. The cost: golem owns the store, and a file with a secret that golem edited stays in golem.db until the session is deleted.

Commands first, a language server later. The project already trusts its own lint and type check, and they need no extra server. A language server may come later.

Plan first is a switch, not a mode. It combines with careful, balanced, and autopilot, and a mode only says what runs without asking.

One line for the model, the diff for the screen. The context is small on a local model, and the model has no use for a diff it just wrote.

Try it ​

Planned. Phase 16 is not built, so there is nothing to try yet. When it ships, this section will walk through an edit, its card, a check, and an undo.

For now, to see what exists: ask golem to change a file and read the diff in the approval, as in Tools.

The details ​

The contracts of each part are in the step issues: #936 the change set, #937 undo, #938 edits that land, #941 checks, and #943 plan first. This page repeats them in short. Until a ticket ships, its contract is the reference, and the code is the truth for what runs today.

What is recorded ​

Every write by write_file and edit_file; changes a shell command made, as far as golem can see them, in a git repository where the sandbox exists, because git runs in it (#952), and in a folder that is not a git repository by comparing the files before and after (coding/plain_changes.py, #1075): at most 2000 files are watched, a changed or deleted file is reported only when it was a UTF-8 text file of at most 64 kB before the command, and changes.list says in shell (git, files or off) which way applies, off being a folder too big to watch; git worktrees inside the session folder (at most 8) are watched too, found by reading <git folder>/worktrees/*/gitdir, and their files are shown by their path from the session folder; and every undo. Not recorded: binary files, files that are not UTF-8, files over the read limit, and anything outside the session folder. A forked session starts with an empty change set.

The calls and the event ​

NameKindPlanned in
file.changedEvent#949
changes.list {session_id}Call#950
changes.diff {session_id, path or change_id}Call#950
changes.commit {session_id, paths, message}Call#981
changes.commit_info {session_id}Call#981
changes.suggest_message {session_id, paths}Call#981
changes.revert {session_id, target, force}Call#953
file.readCall#966
session.set_planCall#979
plan.proposedEvent#979
check.started, check.completedEvents#973

Files ​

FileWhat it does
packages/daemon/golem/tools/write.pyThe two write tools: plan, preview, apply, commit
packages/daemon/golem/store/blobs.pyThe blob store, one text under its sha256
packages/view/src/diff.tsThe diff logic both clients use
packages/web/src/features/diff/The unified and split diff in the web
packages/tui/src/diff-view.tsThe diff in the terminal

Read more ​