Skip to content

Developer: write code until the tests pass ​

examples/engineering/code-writer.halo policies |<> loops

Reads a test file (the spec), has an agent write the implementation, runs the tests, and while they fail sends the agent the failing output to fix (|<> loop). The tests decide when it's done, not the model. The code runs in a Docker sandbox with a copy of workdir, not on your machine, and with no network: model-written code can't touch your files or call out. The finished file comes back as a diff, and download writes it next to the tests only once they pass.

Open in the playground and play with it →

bash
halo run examples/engineering/code-writer.halo --arg workdir=calc \
--arg test_file=calc.test.ts --arg target_file=calc.ts \
--arg task="Evaluate arithmetic expressions"
(workdir is a folder under the current one; Docker must be running)
halo
// ============================================================
// Developer: write code until the tests pass
// ============================================================
// Reads a test file (the spec), has an agent write the implementation,
// runs the tests, and while they fail sends the agent the failing
// output to fix (|<> loop). The tests decide when it's done, not the model.
//
// The code runs in a Docker sandbox with a copy of `workdir`, not on
// your machine, and with no network: model-written code can't touch
// your files or call out. The finished file comes back as a diff, and
// `download` writes it next to the tests only once they pass.
//
// RUN:  halo run examples/engineering/code-writer.halo --arg workdir=calc \
//         --arg test_file=calc.test.ts --arg target_file=calc.ts \
//         --arg task="Evaluate arithmetic expressions"
//       (workdir is a folder under the current one; Docker must be running)
// ============================================================

policy SafeCode : Block(DestructiveOps) & Block(APIKey) & MaxSpend(0.50)

// A copy of the project in a Bun container, cut off from the network.
tool Box : Remote("docker", image: "oven/bun:1", from: "{{workdir}}", network: false)

agent Coder [anthropic/claude-sonnet-4-5, temp: 0.0]
  system """
    You are a careful TypeScript engineer working in a Bun project.
    Write the complete contents of the requested file: no Markdown fences,
    no commentary, no placeholders. Make the tests pass without special-
    casing test inputs. When test output is shown, fix the root cause.
  """
  requires SafeCode

pipeline WriteCode(task: String, workdir: String, test_file: String, target_file: String) -> Result:
  task
    // 1. The tests are the spec
    |> Box.read(path: test_file) as spec

    // 2. Write the implementation
    |> Coder.implement(
         "Task: {{task}}. Write {{target_file}} so that {{test_file}} passes.",
         context.data.spec.content
       ) as code

    // 3. Save it in the sandbox (dropping any ``` fence lines) and run the tests there
    //    (pipefail: the exit code is bun test's)
    |> Box.write(path: target_file, content: context.data.code) as saved
    |> Box.run(command: "grep -v '^```' {{target_file}} > .code.tmp; mv .code.tmp {{target_file}}; set -o pipefail; bun test {{test_file}} 2>&1 | tail -60") as result

    // 4. Failing? The coder sees the test output and tries again
    |<> loop(Coder, "context.data.result.exitCode != 0", max: 4) as fixes

    // 5. Green: bring the file home
    |> Box.download(from: target_file, to: "{{workdir}}/{{target_file}}") as written
    |> System.Value(context.data.result.stdout)

curl -fsSL https://halolang.com/install.sh | sh