Skip to content
codeband is pre-release. These docs describe its first release, and some of what they describe is still being built.

Run a program in the target repository

Run a program in a shell step when your pipeline needs to know whether the code of the target repository passes its tests, its linter or its build. In this guide you run run_program, the example pipeline that runs one program with cb.shell_step, on a small repository with a test. At the end, codeband run prints ✓ program#0 exit code 0 for a test that passes, stopped: the program exited with code 1: and the test’s output for a test that fails, and hello, Grace from a program that got the variable GREETER_NAME by name. If a function step fails, see Troubleshooting at the end of this page.

  • You did the Quickstart: you have a clone of codeband with the binary at build/codeband, and the tools that the Quickstart lists.
  • jq for the check at the end, as jq --version shows.
  • Your terminal is in the codeband folder. Run each command from there unless a function step says otherwise.

A shell step runs one program in a new workspace: a checked-out commit of the target repository, by default the run head, which the engine makes for this one step and removes after it. The engine, the part of the daemon that executes runs, runs the program, not the code server. The program gets PATH, HOME and USER from the daemon’s environment, the variables of the [env] table of the config file, and each variable that the shell step names. Nothing else of the daemon’s environment reaches it. A program that fails raises no error in your pipeline code. Your code reads how the program ended and decides what follows.

  1. Call cb.shell_step in the pipeline

    run_program in examples/py-factory runs the program of its run input and stops the run when the program fails:

    examples/py-factory/py_factory/program.py
    """The pipeline run_program: a shell step that runs a program in the target repository.
    The engine runs the program, not the code server. It runs it in a new workspace, which holds the files of the run head, and
    discards what the program changes there.
    """
    from datetime import timedelta
    from typing import Annotated, Final
    import codeband as cb
    from pydantic import Field
    _OUTPUT_LINES: Final = 10
    """How many of the last output lines the stop reason holds."""
    # The hints of the run input's fields, which the start form of the web interface shows.
    Argv = Annotated[
    tuple[str, ...],
    Field(description='The program and its arguments. No shell reads them: write ["sh", "-c", "..."] to use one.'),
    ]
    Env = Annotated[
    tuple[str, ...],
    Field(description="Names of variables of the daemon's environment. The run records the names, never the values."),
    ]
    @cb.pipeline
    def run_program(argv: Argv, env: Env = (), timeout_seconds: int = 600) -> cb.RunResult | None:
    """Run the program in the target repository. The run stops when the program fails.
    A failed program does not raise: the shell step gives ok=False, and the pipeline code decides what follows.
    """
    result = cb.shell_step(argv, name="program", env=env, timeout=timedelta(seconds=timeout_seconds))
    if not result.ok:
    raise cb.Stop(stop_reason(result))
    return None
    def stop_reason(result: cb.ShellStep) -> str:
    """Say how the program ended, followed by the last lines of its output."""
    if result.timed_out:
    reason = "the program ran longer than its timeout"
    elif result.exit_code is None:
    # The program did not start, a signal stopped it, or the daemon's environment does not set a named variable.
    reason = "the program did not run to its end"
    else:
    reason = f"the program exited with code {result.exit_code}"
    lines = result.output_tail.rstrip("\n").splitlines()[-_OUTPUT_LINES:]
    if not lines:
    return f"{reason} and wrote no output"
    return reason + ":\n" + "\n".join(lines)

    run_program passes each argument of cb.shell_step from its run input, so one pipeline can run any program:

    Argument run_program passes What it does
    argv argv of the run input The program and its arguments. No shell reads them, so ["sh", "-c", "make test && make lint"] is how to use one
    name "program" The name of the shell step. Its first call has the step ID program#0
    env env of the run input The names of the variables that the program gets from the daemon’s environment
    timeout timeout_seconds of the run input, 600 by default The engine stops the program after this time. Without it, the timeout is 10 minutes

    In a pipeline of your own, the program is usually fixed, such as cb.shell_step(["make", "test"], name="tests"). The reference lists the other argument, at, which starts the workspace at another commit.

  2. Read how the program ended

    cb.shell_step returns a cb.ShellStep when the program has ended. stop_reason in the same file reads four of its fields:

    Field Holds run_program uses it for
    ok True when the program exited with code 0 within its timeout Stopping the run when it is False
    exit_code The exit code, or None when it is unknown: after a timeout or a signal, or when the program did not start the program exited with code N
    timed_out True when the program ran longer than its timeout the program ran longer than its timeout
    output_tail The last 64 KiB of standard output and standard error, in the order the program wrote them The last 10 lines of the stop reason

    Test ok, not exit_code. A program that timed out or did not start has exit_code None, which if result.exit_code: reads as a success. run_program raises cb.Stop with the reason, so codeband run prints it and exits with code 3.

  3. Start the daemon and the code server with a variable

    If codeband dev from the Quickstart still runs, press Ctrl-C in its terminal. Open a second terminal, change to the codeband folder and start codeband dev in the example, with a variable in its environment:

    Terminal window
    GREETER_NAME=Grace just examples/py-factory/dev

    The code server logs the pipelines it loaded, run_program among them, and the daemon logs Listening. Both keep running:

    Output
    Nov 25 20:56:22.670 INF Loaded pipelines comp=codeserver pipelines="[intake run_program ship_feature write_hello]" folder=…/codeband/examples/py-factory
    Nov 25 20:56:22.698 INF Serving pipelines comp=codeserver address=unix:~/.local/state/codeband/codeserver.sock
    …
    Nov 25 20:56:22.710 INF Listening comp=daemon socket=…/codeband/daemon.sock

    GREETER_NAME is now in the daemon’s environment. No program gets it until a shell step names it, as step 6 does.

  4. Make a target repository

    Back in the first terminal, make a git repository with a program and its test:

    Terminal window
    git init /tmp/greet
    cat > /tmp/greet/greet.sh <<'EOF'
    echo "hello, ${GREETER_NAME:-world}"
    EOF
    cat > /tmp/greet/test.sh <<'EOF'
    got=$(GREETER_NAME=Ada sh greet.sh)
    if [ "$got" = "hello, Ada" ]; then
    echo "ok: greet.sh greets Ada"
    else
    echo "FAIL: greet.sh printed '$got'"
    exit 1
    fi
    EOF
    git -C /tmp/greet add greet.sh test.sh
    git -C /tmp/greet commit -m "Add greet.sh and its test"

    greet.sh greets the name in GREETER_NAME, or world when the variable is not set. test.sh checks that it greets Ada, and exits with code 1 when it doesn’t. The last command prints the new commit and 2 files changed. A run starts at the HEAD commit of its target repository, so the files must be committed.

  5. Run the test

    codeband dev serves the example under the name of its folder, py-factory, so the pipeline is py-factory/run_program.

    Terminal window
    build/codeband run -v --repo /tmp/greet py-factory/run_program --input '{"argv": ["sh", "test.sh"]}'

    The program runs in the root of the workspace, so test.sh is a path in the repository. -v prints the output of the program as it comes. The test passes, so codeband run prints no code change and exits with code 0. Your run ID is different:

    Output
    Started run hprcv1my8z
    ▸ program#0 shell step: sh test.sh
    ok: greet.sh greets Ada
    ✓ program#0 exit code 0 after 0.1s
    no code change

    Without -v, codeband run prints the output of a shell step only when it fails, as its last 20 lines.

  6. Pass an environment variable by name

    Run greet.sh without naming the variable:

    Terminal window
    build/codeband run -v --repo /tmp/greet py-factory/run_program --input '{"argv": ["sh", "greet.sh"]}'

    The daemon’s environment holds GREETER_NAME, but the program doesn’t get it, so it prints hello, world:

    Output
    Started run pmnz8xv9d4
    ▸ program#0 shell step: sh greet.sh
    hello, world
    ✓ program#0 exit code 0 after 0.1s
    no code change

    Now name the variable in env of the run input, which run_program passes to cb.shell_step:

    Terminal window
    build/codeband run -v --repo /tmp/greet py-factory/run_program --input '{"argv": ["sh", "greet.sh"], "env": ["GREETER_NAME"]}'

    The program gets the daemon’s value and prints hello, Grace:

    Output
    Started run fb77p5rh6m
    ▸ program#0 shell step: sh greet.sh
    hello, Grace
    ✓ program#0 exit code 0 after 0.1s
    no code change

    The run records the name GREETER_NAME, never its value, so a token can reach a program this way. We recommend env of cb.shell_step for a variable that one program needs. A variable that every shell step and every agent step needs, such as a token from a password manager, fits the [env] table of codeband.toml better.

  7. See a test fail

    Change greet.sh so that it says hi, commit the change and run the test again, without -v:

    Terminal window
    echo 'echo "hi, ${GREETER_NAME:-world}"' > /tmp/greet/greet.sh
    git -C /tmp/greet commit -am "Say hi instead of hello"
    build/codeband run --repo /tmp/greet py-factory/run_program --input '{"argv": ["sh", "test.sh"]}'

    The test exits with code 1, so the shell step is not ok, and run_program stops the run. codeband run exits with code 3:

    Output
    Started run ytbn5khhtj
    ▸ program#0 shell step: sh test.sh
    ✗ program#0 exit code 1 after 0.1s
    │ FAIL: greet.sh printed 'hi, Ada'
    stopped: the program exited with code 1:
    FAIL: greet.sh printed 'hi, Ada'

    codeband run prints the ✗ line and, without -v, the last 20 output lines after │ for every failed shell step. The line after stopped: is the reason that stop_reason built from exit_code and output_tail.

The run journal records the names of the variables that the program got, and no value. Print them for the run of step 6 that named GREETER_NAME:

Terminal window
jq -c '.stepStarted.shellStep // empty | {env, variables}' \
~/.local/state/codeband/store/runs/RUN_ID/journal.jsonl

Put the run ID that codeband run printed in place of RUN_ID. The command prints one line:

Output
{"env":["GREETER_NAME"],"variables":["GREETER_NAME"]}

env is what the shell step named, and variables holds the names of every variable that the program got from [env] and from env. When the daemon’s environment holds a login such as ANTHROPIC_API_KEY, variables names it too, because every shell step gets it. The run of greet.sh without env prints {"env":null,"variables":null}.

Give the program a timeout shorter than its run time:

Terminal window
build/codeband run --repo /tmp/greet py-factory/run_program --input '{"argv": ["sleep", "5"], "timeout_seconds": 1}'

After one second the engine sends SIGTERM to the program, and SIGKILL 10 seconds later if it still runs. The shell step has timed_out True and exit_code None, and the run stops with exit code 3:

Output
Started run 4ggyyzkcn0
▸ program#0 shell step: sleep 5
✗ program#0 timed out after 1.1s
stopped: the program ran longer than its timeout and wrote no output

Run rm -rf /tmp/greet and press Ctrl-C in the terminal of codeband dev.

For an error that this page doesn’t cover, see Run problems.

codeband: the daemon's environment does not set GREETER_NAME

Section titled “codeband: the daemon's environment does not set GREETER_NAME”

The shell step names GREETER_NAME, but you started codeband dev without it. The engine does not start the program, so the shell step is not ok, its exit_code is None, and the run stops with stopped: the program did not run to its end: and this line. Press Ctrl-C in the terminal of codeband dev and start it again as in step 3.

codeband: start greet.sh: exec: "greet.sh": executable file not found in $PATH

Section titled “codeband: start greet.sh: exec: "greet.sh": executable file not found in $PATH”

The first item of argv has no /, so the engine looks for the program on the PATH, not in the workspace. The program does not start, so the run stops:

Output
✗ program#0 failed after 0.1s
│ codeband: start greet.sh: exec: "greet.sh": executable file not found in $PATH
stopped: the program did not run to its end:
codeband: start greet.sh: exec: "greet.sh": executable file not found in $PATH

Run the file through its interpreter, as in ["sh", "greet.sh"].

The program exits with code 126 and prints nothing, but passes in your terminal

Section titled “The program exits with code 126 and prints nothing, but passes in your terminal”

The program of a shell step does not get the variables that your shell profile sets, and a tool manager such as asdf needs them. The line reads ✗ program#0 exit code 126 after 0.1s. Add the variables that the tool needs to the [env] table of codeband.toml, as A shell step fails, but the same command passes in an agent step shows.

The test passes after you changed greet.sh, and codeband run logs The repository has uncommitted changes. The run does not see them.

Section titled “The test passes after you changed greet.sh, and codeband run logs The repository has uncommitted changes. The run does not see them.”

A run starts at the HEAD commit of its target repository, so the workspace has greet.sh as you last committed it. Commit the change with git -C /tmp/greet commit -am "Say hi instead of hello" and start the run again.

  • Step: the workspace of a shell step, the variables its program gets, and how the engine stops it.
  • Python SDK: every argument of cb.shell_step and every field of cb.ShellStep, changed_paths among them.
  • codeband.toml: the [env] table and the order in which a program gets its variables.
  • codeband CLI: every line that codeband run prints while it follows a run.