Stage
A stage is a named part of a pipeline that its code declares, such as build or review. A run is in at most one stage at a time, and every step that starts inside a stage belongs to it.
A run on its own is a flat list of steps: agent steps, shell steps, function steps and human steps. A person who watches it wants to know how far it is, such as “in review, with three reviewers at work”. Stages give the parts of your pipeline the names your team uses, in the order you choose. The run journal records the stage of each step, so every view of a run can group its steps by stage. After this page you know how to declare stages, how your code enters them, and what the run journal records.
How it works
Section titled “How it works”The fix_issue pipeline declares the stages build and review. One agent step of implementer builds the code change, and two agent steps of reviewer review it:
import codeband as cb
class Issue(cb.Model): title: str body: str
class Fix(cb.Model): title: str summary: str
class Review(cb.Model): approved: bool notes: str
implementer = cb.Agent("implementer", input=Issue, output=Fix, harness=cb.ClaudeCode(model="sonnet"))reviewer = cb.Agent("reviewer", input=Issue, output=Review, harness=cb.ClaudeCode(model="sonnet"), access="read")
build = cb.Stage("build")review = cb.Stage("review")
def review_twice(issue: Issue) -> list[cb.AgentStep[Review]]: return [reviewer.do(issue), reviewer.do(issue)]
@cb.pipeline(stages=[build, review])def fix_issue(ticket: cb.Ticket) -> cb.RunResult | None: issue = Issue(title=ticket.title, body=ticket.body) with build: fix = implementer.do(issue)
if not cb.human_step("review", cb.Question(text=f"Review '{fix.output.title}'?"), answer=bool): raise cb.Stop("a person skipped the review")
with review: reviews = review_twice(issue)
if not all(r.output.approved for r in reviews): raise cb.Stop("a reviewer did not approve the fix")
return cb.RunResult(code_change=cb.CodeChange(commit=fix.commit, title=fix.output.title, description=fix.output.summary))- The pipeline declares its stages as an ordered list. The list is part of the factory manifest, so every view knows the stages and their order before any run.
- The run starts outside every stage. It enters
buildwith the block, and the agent stepimplementer#0starts inbuild. - The block ends, and the human step
review#0opens outside every stage block. The human step has no stage. The run stays inbuild, the stage it entered last, while it waits for the answer. - The run enters
review. The agent stepsreviewer#0andreviewer#1start inreview_twice, a helper function that the block calls, so they belong toreviewtoo.
The preview graph shows each stage block with its steps before any run. codeband graph prints stage build above the steps of the block, and the web interface draws the block as a frame with the stage’s name.
The engine records the stage on the run journal entry that starts each step. A run of write_hello, the example pipeline of py-factory that runs its agent step in the stage build, shows it:
jq -r '.stepStarted | select(.) | "\(.stepId) \(.stage // "")"' ~/.local/state/codeband/store/runs/5xmhcze7cb/journal.jsonlwriter#0 buildA step that starts outside every stage block has no stage field. A stage block without steps leaves nothing in the run journal.
The web interface shows the runs of a pipeline on a board, with one column for each declared stage in declared order and one before them for runs without a stage. A run’s card moves when its run enters another stage. A run that waits for an answer or has ended stays in the column of its last stage.
The stage of a step and of a run
Section titled “The stage of a step and of a run”| Thing | Its stage |
|---|---|
| An agent step, shell step or human step | The stage of the block it started in, or none outside every block |
| A function step | The stage of the block it ran in, or none outside every block |
| A run | The stage it entered last. A run that has not entered a stage has none |
Stages and step IDs are separate. The agent step implementer#0 keeps its step ID in any stage, so moving a call into another block does not change it.
With parallel agent steps, a run in review can still have running agent steps that started in build. Each of them keeps the stage it started in.
Stages and run replay
Section titled “Stages and run replay”A recorded stage wins over changed code. When you move a call of implementer from the build block to the review block, a run that recorded implementer#0 in build does not diverge on run replay. The recorded agent step returns its recorded value, and the run journal still says build. Only steps that start after the change get the new stage.
After you rename or remove a stage, the old runs keep the name they recorded.
Rules for your pipeline code
Section titled “Rules for your pipeline code”Declare every stage you enter
Section titled “Declare every stage you enter”A stage block on a stage that the pipeline does not declare is a definition error when the code server loads the factory, so a misspelled stage fails before any run starts. A block whose stage the load can’t see, such as a stage that the code looks up at run time, fails the run instead, such as failed: definition_error: the pipeline enters stage 'deploy', but it declares 'build', 'review'. The declared list fixes the names and their order, so no run shows up in a stage that nobody planned.
Do not put a stage block inside a stage block
Section titled “Do not put a stage block inside a stage block”A run is in one stage at a time, so a stage block inside another fails the run with a definition error: failed: definition_error: the block of stage 'review' is inside the block of stage 'build': a run is in one stage. Close the first block before you open the next.
Use the same stage again for the same kind of work
Section titled “Use the same stage again for the same kind of work”Two blocks can enter the same stage. A loop that goes back from review to build enters build again, and its steps show in the same stage as the first build.
Leave out stages in a small pipeline
Section titled “Leave out stages in a small pipeline”Stages are optional. A pipeline without a list of stages runs the same, and its runs have no stage.
Related
Section titled “Related”- Pipeline explains the pipeline function that declares the stages.
- Step explains the steps that a stage groups and their step IDs.
- Run replay explains why a recorded step keeps its recorded stage.
- The Python SDK reference lists
cb.Stageand thestagesparameter of@cb.pipeline.