Run replay
A run replay is how codeband moves a run forward: it runs your pipeline again from the start against the run journal, and each step that already has a record returns its recorded step output instead of running again. One run replay that the code server does for the engine is an invocation. It ends at the first step without a record or when the pipeline ends, and a run moves forward one invocation at a time.
A pipeline is a plain function with branches and loops, and a run of it can take hours: an agent step runs for minutes, and a human step can wait days for an answer. codeband keeps no paused function in memory for that time. The run journal, the ordered list of journal entries that records everything the run has produced so far, holds the value of every step, and the code server runs the function again from the start whenever the run must go on. A run that waits holds no process, the code server keeps nothing between invocations, and you can fix pipeline code while a run waits without losing its recorded agent steps. After this page you know what one invocation does, which rules your pipeline code follows, and what happens when the code changes during a run.
How it works
Section titled “How it works”The intake pipeline of the example py-factory plans tickets in plain code, then calls two function steps: received_at reads the clock and post_summary sends a summary. It returns the tickets as the new tickets of its run result.
"""The pipelines of this factory. pyproject.toml lists them under [tool.codeband]."""
import contextlibfrom datetime import datetime
import codeband as cb
from py_factory.function_steps import post_summary, received_atfrom py_factory.models import Plan, Ticketfrom py_factory.planning import plan_tickets
@cb.pipelinedef intake(title: str, body: str, max_tickets: int = 5) -> cb.RunResult | None: """Plan one ticket per Markdown bullet of the body of a request for work, as a person writes it.
The run stops when the body has no bullet or more than max_tickets. It ends with the tickets in its run result and no code change, and the daemon delivers the tickets. """ plan = plan_tickets(title, body) if not plan.tickets: raise cb.Stop("the request has no tickets: write each one as a '- ' bullet")
if len(plan.tickets) > max_tickets: raise cb.Stop(f"the request has {len(plan.tickets)} tickets, more than {max_tickets}: split it")
received = received_at()
# The run still ends with its tickets when the summary fails to send. The run journal keeps the function step's # error. with contextlib.suppress(cb.FunctionStepFailedError): post_summary(_summary(plan, received))
return cb.RunResult(tickets=[_new_ticket(plan, t) for t in plan.tickets])
def _new_ticket(plan: Plan, ticket: Ticket) -> cb.NewTicket: return cb.NewTicket(title=ticket.title, body=f"Part of the request {plan.title}.", tags=[ticket.kind, ticket.size])
def _summary(plan: Plan, received: datetime) -> str: lines = [f"{plan.title}: {len(plan.tickets)} tickets at {received:%Y-%m-%d %H:%M:%S} UTC"] lines += [f"- {t.kind}, {t.size}: {t.title}" for t in plan.tickets]
return "\n".join(lines)A run of intake takes three invocations:
| Invocation | plan_tickets |
received_at#0 |
post_summary#0 |
The invocation ends with |
|---|---|---|---|---|
| 1 | Runs | Runs | The value of received_at#0 |
|
| 2 | Runs | Recorded value | Runs | The value of post_summary#0 |
| 3 | Runs | Recorded value | Recorded value | The pipeline’s return: new tickets and no code change |
The engine inside the daemon and the code server, which runs your pipeline code, share the work of each invocation. Architecture shows both processes.
- The engine sends the code server the pipeline’s name, the run input, the start commit and every recorded step with its value, all read from the run journal.
- The code server runs the pipeline function from the start. Plain code, such as
plan_tickets, runs on every invocation. - Each step that pipeline code asks for gets its step ID, such as
received_at#0. When the run journal has a value for that ID, the step returns the value and does not run. - The first step without a record ends the invocation. A function step runs in the code server, and the invocation ends with its value. An agent step, a shell step or a human step goes back to the engine as a new step.
- The engine writes the value to the run journal. It runs an agent step or a shell step and records its value, or waits for the answer to a human step. Then it invokes again.
- The invocation in which the pipeline function returns or stops the run ends the run.
Plain pipeline code runs once per invocation, about once per step, and each step runs once per run. An invocation of a short pipeline takes milliseconds, while an agent step runs for minutes, so the repeated pipeline code costs little.
The run journal holds everything a run needs, so a run also continues after the daemon restarts. The engine invokes again from the last journal entry.
How a run replay finds a recorded step
Section titled “How a run replay finds a recorded step”A step ID is the step’s name, a # and a counter, such as notes#0. The counter counts the steps of one name in the order that pipeline code asks for them, from 0. A run replay looks up a recorded step by its step ID, not by its position in the run.
So you can remove or add a step without moving the others. The pipeline release calls the function steps received_at and notes, runs the shell step tests and asks the human step approve. Say a run of release waits at approve#0. You remove the function step received_at from the code, change the argument of notes, start the code server again and answer the human step. notes#0 still finds its record, and the run finishes. If a run replay used positions, every step after the removed one would match the wrong record, and the run could not go on with the fixed code.
A counter shifts only for its own name. A new notes function step that pipeline code asks for before the recorded one gets the step ID notes#0 and gets the recorded value of the old notes#0.
When pipeline code changes during a run
Section titled “When pipeline code changes during a run”The code server loads the factory once, when it starts. codeband dev starts it again when a file of the factory changes, which is a factory reload. A code server that you started yourself loads changed code when you start it again. When a run is going on, its next invocation runs the new code against the run journal. The run records the warning the pipeline code changed since this run started with the first invocation on the new code. While the code server is stopped, or the changed code does not load, the run keeps its status and the engine invokes it again, after 1 second at first and up to every 30 seconds. After a factory reload, it invokes as soon as the new code server answers.
| Change to a recorded step | What happens |
|---|---|
| Its input changed: a function step’s arguments, an agent step’s input or prompt, a shell step’s arguments, environment variable names or timeout, or a human step’s question | It returns its recorded value. The run records the warning the input of notes#0 changed since it was recorded; it returned its recorded value |
| Pipeline code no longer reaches it | The run records the warning the pipeline did not reach recorded step received_at#0 when the pipeline ends |
| Its step ID now names another kind of step, such as a function step that became a shell step | The run fails with diverged |
| It is an agent step or a shell step that would now start at another commit | The run fails with diverged |
| Its recorded value does not decode as the new type, for example after a function step’s return type changed | The run fails with invalid_record |
Recorded values win because agent steps cost time and money. If any changed input ran the step again, fixing a pipeline while a run waits would throw away the agent steps it already paid for. The run would also hold two values for one step, where each step of a run has exactly one. To run a recorded agent step again with a new prompt, fork the run.
A changed start commit is the exception, because an agent step or a shell step works on the code at its start commit. Say the run of release runs its shell step tests at the run head. While tests#0 runs, you add an agent step of the agent bumper, which may write, before tests and start the code server again. bumper#0 runs and moves the run head to its end commit, so tests would now start there. The value of tests#0 comes from code without bumper’s change, and reusing it would leave out that work without any sign. The next invocation fails the run instead, and codeband run my-pipelines/release prints:
Started run ymykxe28s1failed: diverged: tests#0: recorded step tests#0 started at 9d55d6014638e67aaa570740951ab29f11ddfb21, the code now starts it at b5f3e330367f0181e129dc294d3d5db9ef09a4c6bumper#0 ran before the run failed, because the new code reached it before tests#0. A run that failed stays failed. Run problems has the fix: start a new run.
The same rules hold for a run that waits at a human step. The invocation that checks the answer runs the new code, and a recorded agent step or shell step that would start at another commit fails the run with diverged. When the new code asks for a new step before the human step, such as an agent step of bumper before approve#0, the run rejects the answer with the pipeline stops before it reaches human step approve#0; the run records where it stops and drops the answer. The run runs bumper#0 and invokes again. If the new code reaches the human step after that, the run waits for the answer again.
When the new code fails before it reaches the human step, the run records the failure and ends failed. The answer does not count: codeband answer prints answer: answer rejected: human step approve#0: the run failed before it reached the human step: with the failure, and exits with code 1. codeband show prints the run with the status failed and the same reason. A run that failed stays failed: fix the code and start a new run.
A factory that no longer loads fails no run. Say you define the function step notes twice in the source of release while the run waits at approve#0. The code server reports the reason, and the daemon refuses an answer to the human step with that reason. codeband answer exits with code 1:
answer: check the answer: the factory does not load: notes is defined twice: as function step notes in my_pipelines.release and as function step notes in my_pipelines.releaseThe human step stays open. Fix the code and answer again.
The warnings go into the invocationFinished journal entry of the invocation in the run journal, and codeband run prints each one as ! STEP: MESSAGE, or ! MESSAGE without a step, while it follows the run. This command prints the warnings of the release run without received_at:
jq -r '.invocationFinished.warnings[]?.message' ~/.local/state/codeband/store/runs/rrr7ns8t8t/journal.jsonlthe input of notes#0 changed since it was recorded; it returned its recorded valuethe pipeline did not reach recorded step received_at#0the pipeline code changed since this run startedRules for your pipeline code
Section titled “Rules for your pipeline code”Give the same steps in the same order on every invocation
Section titled “Give the same steps in the same order on every invocation”The engine knows what a run did only from its run journal. Pipeline code that asks for other steps on the next invocation than on the last one gets values that were recorded for other work, or fails the run. Branch only on the run input and on values that steps returned.
Read values that change between invocations in a function step
Section titled “Read values that change between invocations in a function step”The clock, random numbers, environment variables, files and responses from other services can differ on each invocation. Read them in a function step, which records the value once. intake reads the clock in the function step received_at, so every invocation sees the same time.
Put side effects in a step
Section titled “Put side effects in a step”Plain pipeline code runs again on every invocation. A message that it sends goes out once per invocation, and a run of intake would send three. Put the side effect in a function step, a shell step or an agent step, which runs once per run. codeband does not check pipeline code for side effects.
Make each function step safe to run twice
Section titled “Make each function step safe to run twice”A function step runs at least once. When the code server or the daemon stops after the function step’s side effect and before the engine records its value, the next invocation runs the function step again. A function step that writes files can name them after a recorded value, such as the time from received_at, so a second run of it writes the same files again instead of new ones. Handle a function step that fails or times out makes a webhook call safe to repeat. New tickets need no function step: a pipeline returns them in its run result, and codeband delivers them when the run ends.
Give each kind of step its own name
Section titled “Give each kind of step its own name”The kinds of step share the counter of a name. Pipeline code that uses one name for a function step and for a shell step fails the run with definition_error. Run problems shows the message.
Change pipeline code between runs
Section titled “Change pipeline code between runs”We recommend that you change pipeline code when no run is going on, or start a new run after the change. A run that is going on keeps its recorded values, and a change of kind or of start commit fails it. When you must fix a run that waits, change only code after the last step the run has recorded.
Related
Section titled “Related”- Handle a function step that fails or times out catches a failed function step, tries it again and makes its side effect safe to repeat.
- Step explains function steps, shell steps and their step IDs.
- Run explains the run journal that a run replay reads and the ways a run ends.
- Run problems fixes a run that fails with
diverged. - Python SDK lists the rules for names and step IDs with their messages.