# τ²-bench loop

> Improve a customer-service playbook on a train split, then prove the gain on a held-out split that the dependency graph keeps the loop away from.

The shape here is a loop followed by a chain the loop cannot reach. The folder is `examples/benchmarks/05_tau2_pass_k_loop`.

## The benchmark

[τ²-bench](https://github.com/sierra-research/tau2-bench) (Sierra) scores customer-service agents on long, policy-bound conversations with a simulated user over real tools, in airline, retail, and telecom domains. Its metric is **pass^k**: a task counts only if all k attempts succeed, the right bar for workflows where one slip in twenty steps sinks the task. The airline domain ships a split: 30 train tasks, 20 test tasks.

## Why this shape

A self-improvement loop is proven only when it is measured on a held-out split, with the cost reported next to the gain. The comparison of the harness landscape found that no shipped product does this (see [the harness landscape](/docs/inspiration/landscape/)). This swarm writes that method into the dependency graph, so the separation is a property of the file, not a request in a prompt.

```text
coach ──playbook──> runner[shard_a..d] ──results──> coach      rounds on the train split
coach ──finish──> heldout ──> reporter                         the test split, once
```

| Agent | Model | Starts when | What it does |
|---|---|---|---|
| `coach` | Opus | swarm starts | writes playbook v0 (empty), reads failures, writes v1 to vN, finishes with the best version |
| `runner[*]` (4 shards) | Sonnet | swarm starts | runs its train task ids at 4 trials for every version the coach posts |
| `heldout` | Sonnet | coach done (`inputs`) | runs the 20 test tasks with v0 and with the best version, same models, same trials |
| `reporter` | Sonnet | coach and heldout done | one table: train gain, held-out gain, cost |

`done_when = agent.reporter.output.held_out_gain >= 0.10`. A loop whose gain does not survive the held-out split leaves the swarm without meeting `done_when`. That is the point.

## The swarm file

```hcl title="main.cvy"
# tau2-bench: improve a customer-service playbook, and prove it on held-out tasks.
#
# tau2-bench scores an agent on long, policy-bound conversations with a
# simulated user over real tools. Its metric is pass^k: a task counts only if
# all k attempts succeed. The airline domain ships a split: 30 train tasks, 20
# test tasks.
#
# This swarm is the self-improvement method from research/benchmark.md section 6,
# written as a dependency graph: improve on one split, measure on a held-out
# split the loop never saw, report the cost next to the gain.
#
# Shape: a loop, then a chain the loop cannot reach.
#
#   coach ──playbook──> runner[shard_a..d] ──results──> coach      rounds, train split only
#   coach ──finish──> heldout ──> reporter                         test split, once
#
# The held-out evaluator has inputs = agent.coach.output, so it cannot start
# until the coach has finished, and the coach, being finished, cannot react to
# what it finds. The train/test separation is enforced by the graph, not by
# asking nicely. done_when reads the reporter, the last agent in the chain, so
# the swarm cannot end before the report exists.

swarm "tau2_airline_playbook" {
  goal         = <<-EOT
    Raise pass^4 of the airline customer-service agent in tau2-bench by
    improving its playbook on the train split, then prove the gain on the
    held-out test split and report the cost of the loop next to the gain.
  EOT
  done_when    = agent.reporter.output.held_out_gain >= 0.10
  max_runtime  = "10h"
  max_cost_usd = 250
  overseer = {
    provider = provider.anthropic
    model    = "claude-sonnet-5"
  }
}

provider "anthropic" {
  source  = "claude_agent_sdk"
  api_key = "ANTHROPIC_API_KEY"
}

topic "playbook" { description = "Coach: a new playbook version. Version, path, what changed. Each message starts a round on every shard" }
topic "results" { description = "Runners: one message per shard per round. pass^4 per task, failing task ids with a transcript path, cost" }
topic "human" { description = "Budget changes and early stops; nothing else" }

# The loop's brain. Writes the playbook, reads failures, revises, decides when
# the train split is good enough, and finishes. Its finish releases the
# held-out stage. The knobs of the experiment are vars, not prompt edits.
agent "coach" {
  provider = provider.anthropic
  model    = "claude-opus-5"
  prompt   = file("prompts/coach.md")
  vars = {
    runs_dir   = env("TAU2_RUNS_DIR")
    domain     = "airline"
    target     = "0.80"
    max_rounds = "6"
    shards     = "shard_a, shard_b, shard_c, shard_d"
  }
  tools   = ["bash", "read_file", "write_file", "edit_file"]
  writes  = [topic.playbook, topic.human]
  watches = [topic.results, topic.human]
  output = {
    version              = number
    rounds               = number
    baseline_train_pass4 = number
    train_pass4          = number
    playbook_path        = string
    loop_cost_usd        = number
  }
  max_turns = 60
}

# Four runners from one block. The map value is the shard's task ids, taken
# from the "train" list of data/tau2/domains/airline/split_tasks.json. Nothing
# from the test split appears in this file.
agent "runner" {
  for_each = {
    shard_a = "0,1,3,4,5,7,9,10"
    shard_b = "11,12,14,15,17,20,21,23"
    shard_c = "27,28,33,34,36,38,39"
    shard_d = "40,41,42,43,46,47,49"
  }
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/runner.md")
  vars = {
    shard    = each.key
    task_ids = each.value
    runs_dir = env("TAU2_RUNS_DIR")
    domain   = "airline"
    trials   = "4"
  }
  tools     = ["bash", "read_file"]
  writes    = [topic.results]
  watches   = [topic.playbook]
  max_turns = 12
}

# The held-out stage. Starts only when the coach is done. Runs the test split
# with the baseline and with the final playbook: same models, same trials.
agent "heldout" {
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/heldout.md")
  vars = {
    runs_dir = env("TAU2_RUNS_DIR")
    domain   = "airline"
    trials   = "4"
  }
  tools  = ["bash", "read_file"]
  inputs = { final = agent.coach.output }
  output = {
    baseline_pass4 = number
    improved_pass4 = number
    tasks          = number
    trials         = number
    eval_cost_usd  = number
  }
}

# Writes the one table that matters: gain on train, gain held out, cost of the loop.
agent "reporter" {
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/reporter.md")
  vars     = { runs_dir = env("TAU2_RUNS_DIR") }
  tools    = ["read_file", "write_file"]
  inputs = {
    train = agent.coach.output
    test  = agent.heldout.output
  }
  output = {
    train_gain    = number
    held_out_gain = number
    loop_cost_usd = number
    report_path   = string
  }
}
```

## Key prompts

The coach never runs the benchmark. It starts from an empty playbook so there is a baseline to beat.

```text title="prompts/coach.md (excerpt)"
FIRST TURN: THE BASELINE
1. Read the domain policy the agent already receives (tau2's
   data/tau2/domains/{{domain}}/policy.md). Do not copy it into the playbook.
   The playbook is what the policy does not say: how to handle the user, in
   what order to verify, when to refuse, which tool to call with what.
2. Write an EMPTY playbook as v0 ({{runs_dir}}/playbooks/v0.md, a one-line
   header). Publish to "playbook": "v0 {{runs_dir}}/playbooks/v0.md baseline".
   Every runner runs its shard with v0. That is the number you must beat.
3. Call report_progress(summary) and stop.
```

The held-out evaluator measures and does not explain.

```text title="prompts/heldout.md (excerpt)"
RULES
- Do not read the train transcripts, the coach's history, or the playbook
  diffs. You measure; you do not explain.
- Do not edit the playbook. If it fails to load, finish with both numbers
  equal to the baseline and say why in your progress report.
- Never run the test split more than once per playbook. One number each.
```

## How a run unfolds

1. The coach and the four runners start. The coach reads the domain policy, writes an empty `v0.md`, publishes `v0 ... baseline` to `playbook`, and stops. The `heldout` and `reporter` agents stay Pending because their `inputs` are not satisfied.
2. The `playbook` message wakes every runner. Each runs its shard of train task ids at 4 trials with that playbook, and publishes to `results`: pass^4 per task, failing task ids with transcript paths, and cost.
3. The coach wakes on each result. A round is complete only when all four shards have reported for the current version; until then it records what arrived and stops. When complete, it computes train pass^4 (tasks where every trial passed, divided by 30), sums cost, appends to `history.tsv`, reads at least five whole failing transcripts, groups failures by cause, and writes the next playbook version.
4. It publishes the new version to `playbook`, which starts the next round on every shard. This repeats until train pass^4 reaches the target (0.80) or `max_rounds` (6) is spent. The coach then finishes with `version`, `rounds`, `baseline_train_pass4`, `train_pass4`, `playbook_path`, and `loop_cost_usd`.
5. The coach is Done, so `heldout` starts. It runs the 20 test tasks twice with the same models and trials, once with `v0` and once with the best playbook, and finishes with both pass^4 numbers and the evaluation cost. The coach, being finished, cannot react to what it finds, and it never saw a test-split number.
6. `heldout` is Done, so `reporter` starts with `train` and `test`. It writes one table: train gain, held-out gain, and the cost of the loop, and finishes with `train_gain`, `held_out_gain`, `loop_cost_usd`, and `report_path`.
7. If `held_out_gain >= 0.10`, `done_when` is true and the run ends Done. Otherwise the swarm does not meet `done_when`, and the report still exists.

## DSL features shown

- A separation enforced by the graph, not by instructions. See [`agent`](/docs/dsl/agent/) for `inputs`.
- A loop followed by a chain. The train loop is pure board (`playbook`, `results`); the held-out stage is pure graph (`inputs`). One file holds both. See [topics and the board](/docs/concepts/board/).
- `for_each` over a map of shards whose values are the train task ids, with `each.value` landing in the prompt as `{{task_ids}}`. The test ids appear nowhere in the file.
- Experiment knobs as `vars`: target, max rounds, trials, domain. Change the experiment without touching a prompt.
- `done_when` on the last agent in the chain, so the swarm cannot end before the report exists, with a numeric bar on a typed output. See [`swarm`](/docs/dsl/swarm/).
- Cost next to gain: the runners report tau2's recorded cost per round, the coach sums it, the reporter prints it.

## Run it

```sh
pip install tau2-bench                            # or clone sierra-research/tau2-bench
export TAU2_RUNS_DIR=/srv/tau2/runs
export ANTHROPIC_API_KEY=... OPENAI_API_KEY=...   # convoyd's environment; the user simulator runs on an OpenAI model

convoy validate
convoy run
convoy watch tau2_airline_playbook --topic results
convoy say tau2_airline_playbook human "stop after this round" --to coach
```

## Machine assumptions

- `playbook_agent` is a small custom agent registered with tau2 (the repo documents custom agents) that appends the file named by `TAU2_PLAYBOOK` to the agent's system prompt after the domain policy. It is the only adapter this swarm needs. Nothing in the benchmark's policy, tasks, or scoring changes.
- The task ids in `main.cvy` are the airline `train` list from `data/tau2/domains/airline/split_tasks.json` at the time of writing. Regenerate the shards if the split changes.
- The model under test and the user-simulator model are named in the runner and held-out prompts in tau2's `provider/model` form. They must stay identical across every run; the playbook is the only variable.
