End a run with a patch
End a run with a patch when you want to read an agent’s work on your own machine before anyone else sees it. In this guide you run write_hello, the example pipeline that ends with a code change, on a small git repository. At the end, codeband run prints the path of the patch file /tmp/hello.patch, and after git apply the repository holds a new file hello.txt that says hello. If a function step fails, see Troubleshooting at the end of this page.
Before you start
Section titled “Before you start”- You did the Quickstart: you have a clone of codeband with the binary at
build/codeband, and the tools that the Quickstart lists. - Your terminal is in the
codebandfolder. Run each command from there unless a function step says otherwise.
How a run ends with a patch
Section titled “How a run ends with a patch”Your pipeline function returns a code change: a commit with a title and a description. The daemon makes one commit of the code change from it on top of the run’s start commit and writes that commit to a patch file. Your repository stays as it was until you apply the patch. The Run result page explains the change commit and the hidden refs that hold it.
-
Return a code change from the pipeline
write_helloinexamples/py-factoryruns one agent step of the agentwriterand returns acb.RunResultwith acb.CodeChangebuilt from it.writerruns on the fake harness, which follows a script and runs no model, so every agent step writes the same filehello.txt:examples/py-factory/py_factory/hello.py """The pipeline write_hello: one agent step that writes a file, and a run that ends with that change.The agent runs on the fake harness, which follows a script and runs no model, so the agent step writes the same file onevery run."""import codeband as cbclass WriterInput(cb.Model):"""The instruction for the writer. The prompt writer.md shows it to the agent."""instruction: strclass WriterOutput(cb.Model):"""What the writer reports at the end of its agent step."""title: str"""Title of the change, one line."""summary: str"""What the agent step changed and why."""writer = cb.Agent("writer",input=WriterInput,output=WriterOutput,harness=cb.Fake(attempts=[cb.FakeAttempt(text=["writing hello.txt"],write={"hello.txt": "hello\n"},output={"title": "Add hello.txt", "summary": "The writer added hello.txt, which says hello."},)]),)build = cb.Stage("build")@cb.pipeline(stages=[build])def write_hello(instruction: str = "add a file hello.txt that says hello") -> cb.RunResult | None:"""Run one agent step of the writer in the stage build and end the run with its commit.The writer may write, so its agent step ends in a commit. The file it leaves uncommitted goes into that commit too."""with build:agent_step = writer.do(WriterInput(instruction=instruction))change = cb.CodeChange(commit=agent_step.commit, title=agent_step.output.title, description=agent_step.output.summary)return cb.RunResult(code_change=change)agent_step.commitis the agent step’s end commit, and its step output gives the title and the description. The pipeline’s parameterinstructionis the field of its run input, with a default. The prompt template next to the module rendersinstructionof the agent’s input into the prompt:examples/py-factory/py_factory/writer.md Do this task in the repository: {{ input.instruction }}A pipeline that returns
Noneends the run withno code changeand writes no patch. So does a code change whose files equal those of the run’s start commit. -
Start the daemon and the code server
If
codeband devfrom the Quickstart still runs, skip this step: it has loadedwrite_hellotoo. Otherwise, open a second terminal, change to thecodebandfolder and startcodeband devin the example:Terminal window just examples/py-factory/devThe code server logs the pipelines it loaded,
write_helloamong them, and the daemon logsListening. Both keep running:Output Nov 24 17:30:49.736 INF Loaded pipelines comp=codeserver pipelines="[intake run_program ship_feature write_hello]" folder=…/codeband/examples/py-factoryNov 24 17:30:49.761 INF Serving pipelines comp=codeserver address=unix:~/.local/state/codeband/codeserver.sock…Nov 24 17:30:49.773 INF Listening comp=daemon socket=…/codeband/daemon.sock -
Make a target repository
Back in the first terminal, make a git repository with one commit for the run to work on:
Terminal window git init /tmp/greeterecho "# greeter" > /tmp/greeter/README.mdgit -C /tmp/greeter add README.mdgit -C /tmp/greeter commit -m "Add a README"The last command prints the new commit with the subject
Add a README. A run starts atHEADof its target repository, so the repository needs at least one commit. -
Run the pipeline with
--patchcodeband devserves the example under the name of its folder,py-factory, so the pipeline ispy-factory/write_hello.Terminal window build/codeband run --repo /tmp/greeter py-factory/write_hello --patch /tmp/hello.patch--reponames the target repository, and--patchnames the patch file.write_hellohas a default for its one input field, so the run needs no--input.codeband runprints the run ID, the start and the end of the agent step and, as its final line, the path of the patch file, then exits with code 0. Your run ID is different:Output Started run qn90zzsqgf▸ writer#0 agent step of writerwriter#0: writing hello.txt✓ writer#0 finished after 0.2s, $0.00/tmp/hello.patchA relative
--patchpath resolves against the folder where you startcodeband run, not against--repo.--patch hello.patchhere would writehello.patchinto yourcodebandfolder. A path to a folder writesRUN-ID.patchinto it, and without--patchthe run writesRUN-ID.patchinto the current folder.The patch is in the format of
git format-patch. The title of the code change is its subject, and the description is its message. Your commit ID and date differ:Terminal window cat /tmp/hello.patchOutput From f92d8a84fe149086d82f0cbbd9f3c6f86b2ffdaa Mon Sep 17 00:00:00 2001From: codeband <codeband@localhost>Date: Tue, 24 Nov 2026 17:30:58 +0100Subject: [PATCH] Add hello.txtThe writer added hello.txt, which says hello.---hello.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 hello.txtdiff --git a/hello.txt b/hello.txtnew file mode 100644index 0000000..ce01362--- /dev/null+++ b/hello.txt@@ -0,0 +1 @@+hello -
Apply the patch
Change to the target repository and apply the patch to its working tree:
Terminal window cd /tmp/greetergit apply /tmp/hello.patchgit applyprints nothing and exits with code 0. It changes only the working tree and makes no commit.We recommend
git apply, so that you read the code change before you commit it as your own.git am /tmp/hello.patchfits when you want the commit of the code change as codeband made it: it printsApplying: Add hello.txtand makes the commitAdd hello.txton your current branch, with the description as its message andcodeband <codeband@localhost>as its author.
With codeband run --pr, the engine delivers the run’s code change as a pull request on GitHub instead; the Run result page compares the two.
Check that it worked
Section titled “Check that it worked”In /tmp/greeter, list the changed files and show the new one:
git status --shortcat hello.txthello.txt is new and not yet committed, and it says hello:
?? hello.txthelloTo keep the file, commit it with git add hello.txt and git commit. To remove everything this guide made, go back to the codeband folder with cd -, run rm -rf /tmp/greeter /tmp/hello.patch, and press Ctrl-C in the terminal of codeband dev.
Troubleshooting
Section titled “Troubleshooting”For an error that this page doesn’t cover, see Run problems.
start run: run request rejected: patch file /tmp/hello.patch exists
Section titled “start run: run request rejected: patch file /tmp/hello.patch exists”The daemon never overwrites a file, so it refuses the run before it starts, and codeband run exits with code 1. Delete the old patch with rm /tmp/hello.patch, or pass another path to --patch.
After you applied the patch, the error comes after a warning about /tmp/greeter:
Nov 24 17:35:04.357 WRN The repository has uncommitted changes. The run does not see them. repo=/tmp/greeterstart run: run request rejected: patch file /tmp/hello.patch existsThe warning means that hello.txt is not committed yet. A run starts at HEAD without it, so a new run writes hello.txt again.
The warning names the repository by the path that Git gives for it, with symbolic links resolved. On macOS, /tmp is a link to /private/tmp, so the warning shows repo=/private/tmp/greeter.
start run: run request rejected: the folder of patch file … does not exist
Section titled “start run: run request rejected: the folder of patch file … does not exist”The folder in the --patch path is missing, and codeband run exits with code 1. Make the folder with mkdir -p, or pass a path in a folder that exists.
codeband run prints no code change
Section titled “codeband run prints no code change”The run’s start commit already has hello.txt with the same text, so the code change holds no new files and the run writes no patch. This happens after you committed the applied patch, or used git am, and ran write_hello again. Start the run on a repository without hello.txt, such as a new one from step 3.
error: hello.txt: already exists in working directory
Section titled “error: hello.txt: already exists in working directory”git apply found hello.txt in the working tree, because you applied the patch before, and exits with code 1 without changes. Check with git status --short. To apply it again, delete the file with rm hello.txt first.
Related
Section titled “Related”- Run result: how the commit of the code change is made, where the daemon keeps it, and when a pull request fits better than a patch.
- Agent step: the end commit that
agent_step.commitgives, and the workspace the agent ran in. - codeband CLI: every flag, final line and exit code of
codeband run. - Install codeband: add codeband to a factory of your own with uv, and end its runs with a code change.