# PaperBench replication

> Replicate an ICML 2024 paper from scratch with a reader, four parallel builders, an experimenter, and a judge on a different model.

The shape here is a dependency graph that fans out and back in, with a review loop at the end. The folder is `examples/benchmarks/01_paperbench_replication`.

## The benchmark

[PaperBench](https://github.com/openai/preparedness/tree/main/project/paperbench) (OpenAI) gives an agent one of 20 ICML 2024 papers and asks for a from-scratch replication: code, execution, and results. A rubric co-written with the authors breaks each paper into hundreds of weighted leaves of three kinds: Code Development, Execution, and Result Match. The canonical budget is 24 hours per attempt. Human ML PhDs score about 41% in that time; the best published agents, about 26%.

The paper used here is `semantic-self-consistency`. Its rubric covers three datasets, the generator models and their decoding limits, two featurizers, the methods of Section 4, and the experiments of Sections 5.1 and 5.2.

## Why this shape

A replication has natural stages. Someone has to read the paper once and write the plan; the plan splits into independent workstreams; the pieces must exist before the experiments run; and a grader that did none of the work decides whether the result holds. The graph (`inputs`, `depends_on`) carries the work forward and never cycles. The board carries the review loop: `results` wakes the judge, `gaps` wakes the experimenter.

```text
reader ──> builder[data, generators, featurizers, methods] ──> experimenter <──> judge
                                                                      results ──>
                                                                      <── gaps
```

| Agent | Model | Starts when | Finishes when |
|---|---|---|---|
| `reader` | Opus | swarm starts | plan written, typed output returned |
| `builder[*]` (4 instances) | Sonnet | reader is done (`inputs`) | its workstream passes its smoke test |
| `experimenter` | Sonnet | all four builders are done (`inputs`) | never; stopped when the swarm ends |
| `judge` | Opus | all four builders are done (`depends_on`) | `approved = true`, or it gives up after 8 rounds |

`done_when = agent.judge.output.approved == true`.

## The swarm file

```hcl title="main.cvy"
# PaperBench: replicate an ICML 2024 paper from scratch.
#
# Paper: "semantic-self-consistency", one of the 20 PaperBench papers. Its rubric
# checks the datasets, the generator models, the featurizers, the methods of
# Section 4, and whether the experiments of Sections 5.1 and 5.2 reproduce the
# paper's conclusions. A human expert takes days.
#
# Shape: a dependency graph that fans out and back in, with a judge loop at the end.
#
#   reader ──> builder[data, generators, featurizers, methods] ──> experimenter <──> judge
#
# The graph (inputs, depends_on) carries work forward and never cycles. The board
# (results, gaps) carries the review loop. The swarm ends when the judge, grading
# against the rubric on a different model than the one that did the work, says
# the replication is approved.

swarm "paperbench_ssc" {
  goal         = <<-EOT
    Replicate the ICML 2024 paper "Semantic Self-Consistency" from scratch: data
    pipeline, generator models, featurizers, every method in Section 4, and the
    experiments of Sections 5.1 and 5.2, so that the reproduced results support
    the paper's conclusions.
  EOT
  done_when    = agent.judge.output.approved == true
  max_runtime  = "24h" # PaperBench's canonical budget per attempt
  max_cost_usd = 400
  overseer = {
    provider = provider.anthropic
    model    = "claude-sonnet-5"
  }
}

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

topic "results" { description = "Experimenter: a completed run of Sections 5.1 and 5.2, with the results table path and the headline numbers next to the paper's" }
topic "gaps" { description = "Judge: rubric requirements not met yet, one message per grading round, addressed to the experimenter" }
topic "human" { description = "Blocked on a person: API keys, GPU quota, or a choice the paper leaves open" }

# Stage 1. One agent reads the paper and writes the plan everyone else builds from.
agent "reader" {
  provider = provider.anthropic
  model    = "claude-opus-5"
  prompt   = file("prompts/reader.md")
  vars = {
    paper     = "semantic-self-consistency"
    workspace = env("PAPERBENCH_WORKSPACE")
  }
  tools  = ["read_file", "bash"]
  writes = [topic.human]
  output = {
    summary       = string
    contributions = list(string)
    acceptance    = map(string) # keyed by workstream: what must exist when it is done
    compute       = string
  }
}

# Stage 2. Four builders from one block. The map key is the workstream and the
# value is its brief; each.key and each.value reach the prompt through vars.
# Every instance starts the moment the reader finishes, because of inputs.
agent "builder" {
  for_each = {
    data        = "Section 3 and the appendix: the AQuA-RAT test split (254 samples), the StrategyQA test split (687), the full SVAMP set (1000), and the k-shot prompt for each dataset"
    generators  = "Section 3.1: every generator model with top-p = 1 and the per-dataset max-new-token limits, plus the parsing extraction that pulls an answer out of a response"
    featurizers = "Section 3.2: RoBERTa-base and SciBERT embeddings for a response string"
    methods     = "Section 4: the top-prob and self-consistency baselines, semantic self-consistency, and the KNN, isolation forest, and SVM outlier removal"
  }
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/builder.md")
  vars = {
    workstream = each.key
    brief      = each.value
    workspace  = env("PAPERBENCH_WORKSPACE")
  }
  tools  = ["bash", "read_file", "write_file", "edit_file"]
  inputs = { plan = agent.reader.output }
  writes = [topic.human]
  output = {
    module      = string
    entrypoints = list(string)
    smoke_test  = string
    notes       = string
  }
}

# Stage 3. Starts when all four builders are done. Runs the experiments, then
# stays alive to fix whatever the judge finds.
agent "experimenter" {
  provider = provider.anthropic
  model    = "claude-sonnet-5"
  prompt   = file("prompts/experimenter.md")
  vars     = { workspace = env("PAPERBENCH_WORKSPACE") }
  tools    = ["bash", "read_file", "write_file", "edit_file"]
  inputs = {
    plan       = agent.reader.output
    components = values(agent.builder)[*].output # every instance of a for_each agent
  }
  writes    = [topic.results, topic.human]
  watches   = [topic.gaps, topic.human]
  max_turns = 30
}

# Stage 4. The judge grades against the rubric. It runs a different model than
# the agents that did the work, the cheap form of an independent judge, and its
# finish is the only thing that can end the swarm.
agent "judge" {
  provider = provider.anthropic
  model    = "claude-opus-5"
  prompt   = file("prompts/judge.md")
  vars = {
    workspace = env("PAPERBENCH_WORKSPACE")
    rubric    = env("PAPERBENCH_RUBRIC")
  }
  tools      = ["read_file", "bash"]
  inputs     = { plan = agent.reader.output }
  depends_on = [agent.builder] # every builder instance, not one of them
  watches    = [topic.results]
  writes     = [topic.gaps]
  output = {
    approved = bool
    score    = number # weighted share of rubric requirements met, 0 to 1
    missing  = list(string)
    rounds   = number
  }
  max_turns = 10
}
```

## Key prompts

The reader's prompt fixes the contract with the builders: its `acceptance` map must have exactly the four workstream keys.

```text title="prompts/reader.md (excerpt)"
WORKSTREAMS
Four builders start the moment you finish, one per workstream: data,
generators, featurizers, methods. Your `acceptance` map must have exactly those
four keys. Each value is that workstream's definition of done: the file paths
that must exist, the functions that must be callable, the numbers that must
match the paper (dataset sizes, hyperparameters, token limits). Write it so a
builder who has never seen the paper can finish from your text alone.
```

The judge's prompt sets the approval rule and the give-up rule. Only its `finish` can end the swarm.

```text title="prompts/judge.md (excerpt)"
APPROVAL
Call finish with approved = true only when all three hold:
- every Code Development and Execution requirement is met;
- both Result Match requirements, Sections 5.1 and 5.2, hold: the reproduced
  numbers support the paper's conclusions within the tolerance the rubric or
  the addendum gives;
- score >= 0.75.
If after 8 rounds the score has stopped improving, publish a last "gaps"
message marked FINAL, then finish with approved = false and the honest score.
The swarm then ends without meeting done_when, which is the right outcome for
an unfinished replication.
```

## How a run unfolds

1. The controller creates `reader`, four `builder` instances, `experimenter`, `judge`, and the overseer, all Pending. Only `reader` has no unmet `inputs` or `depends_on`, so it starts first.
2. The reader reads the paper, checks the machine, writes `PLAN.md`, and finishes with a typed output: `summary`, `contributions`, `acceptance` keyed by workstream, and `compute`. If something is missing (a key, a dataset), it publishes once to `human` and finishes anyway.
3. The reader is Done, so the four builders' `inputs` are satisfied and all four start at once. Each receives the plan as JSON on its first turn, and `{{workstream}}` and `{{brief}}` come from its `for_each` entry. Each builds its module and smoke test and finishes with `module`, `entrypoints`, `smoke_test`, and `notes`.
4. When the fourth builder finishes, the experimenter starts with `plan` and `components` (the list of all four builder outputs), and the judge starts because `depends_on = [agent.builder]` resolves to every instance. The judge reads the rubric once, reports its structure, and waits.
5. The experimenter runs Sections 5.1 and 5.2 and publishes to `results`. That wakes the judge.
6. Each grading round, the judge reads the code (not the summary), walks the rubric leaf by leaf, computes a weighted `score`, and publishes one message to `gaps` addressed to the experimenter. The message wakes the experimenter, which fixes and publishes to `results` again.
7. The loop ends when the judge calls `finish` with `approved = true` and a score of at least 0.75. If the score stops improving after 8 rounds, it finishes with `approved = false`, and the run ends without meeting `done_when`.
8. Any limit ends the run first if reached: `max_runtime = "24h"` or `max_cost_usd = 400`.

## DSL features shown

- [`for_each`](/docs/dsl/agent/) over a map: the key names the workstream, the value is its brief, and both reach the prompt through `vars`.
- Typed, nested `output` on the reader (`map(string)`, `list(string)`) that downstream agents receive as JSON on their first turn.
- Fan-in with `values(agent.builder)[*].output`, the idiom for every instance of a `for_each` agent. See [references and functions](/docs/dsl/expressions/).
- `depends_on = [agent.builder]` on the judge, which resolves to every builder instance.
- A judge on a different model from the workers, whose typed `output` is the only thing `done_when` reads. See [`swarm`](/docs/dsl/swarm/).
- `env()` inside `vars` to hand every agent one shared workspace path.
- Agents that finish in one turn (reader, builders) next to agents that live on the board (experimenter, judge). See [agents and turns](/docs/concepts/agents/) and [topics and the board](/docs/concepts/board/).

## Run it

You need a PaperBench checkout with the paper hydrated, a GPU machine, and `convoyd` running.

```sh
export ANTHROPIC_API_KEY=...   # in convoyd's environment: api_key names this variable
export PAPERBENCH_WORKSPACE=/srv/paperbench/semantic-self-consistency   # paper/ and agent.env live here
export PAPERBENCH_RUBRIC=/srv/paperbench/grading/semantic-self-consistency/rubric.json

mkdir -p $PAPERBENCH_WORKSPACE/paper $(dirname $PAPERBENCH_RUBRIC)
cp <paperbench>/data/papers/semantic-self-consistency/{paper.md,paper.pdf,blacklist.txt,addendum.md} $PAPERBENCH_WORKSPACE/paper/
cp <paperbench>/data/papers/semantic-self-consistency/rubric.json $PAPERBENCH_RUBRIC

convoy validate
convoy run
convoy watch paperbench_ssc
convoy say paperbench_ssc human "HF_TOKEN is in agent.env now" --to experimenter
```

## Machine assumptions

- All agents run on one machine and share `$PAPERBENCH_WORKSPACE`. Convoy's MVP work folders are per agent, so the shared checkout is passed by path. The rubric lives outside the workspace and only the judge's prompt names it; the separation is by convention until the sandbox backend lands.
- The judge is Claude Opus 5 reading the code directly. PaperBench's own judge runs in a separate container over the executed submission. An OpenAI judge in this swarm would need a shared artifact store, which the MVP does not have.
- Grading follows PaperBench's rubric kinds but not its scoring code. Run the official judge on `$PAPERBENCH_WORKSPACE` afterwards for the comparable number.
