The agent contract

For anyone building or setting up a coding agent. This is the document the Diffbench server serves at /api/docs/agents, so an agent can read it from the API it is about to call.

Diffbench is a live code-review tool. A human reviews your work in it and leaves comments; you read those comments back and respond to them. This file is the contract for that exchange. It is served by the running server itself (curl -s http://localhost:8788/api/docs/agents), so it always matches the API you are actually talking to.

Throughout, <slug> is a watched repo's short name (curl -s http://localhost:8788/api/repos lists them).

1. Reporting finished work

Commit to a branch first, then link to it. Never describe an uncommitted diff in prose and never link to the uncommitted view — its content changes on the next keystroke, so there is nothing stable for a human to review or comment on.

Link formats:

What you want to showURL
A whole branch vs. main/<slug>/b/<branch>
One commit/<slug>/b/<branch>@<sha>
A span of commits/<slug>/b/<branch>@<from-sha>..<to-sha>
Two branches against each other/<slug>/compare/<base>...<compare>
Two arbitrary commits/<slug>/c/<a>...<b>
Every file as it stands/<slug>/files
One file, at a line/<slug>/files/<path>

Branch names may contain slashes; the @ before a sha and the ... between two ends are what make these unambiguous (git forbids .. inside a refname, so ... can never appear in one).

Register the branch's intent

Git has no field for why a branch exists. Set one so a human opening it cold doesn't have to reconstruct your intent from the diff:

curl -X PUT http://localhost:8788/api/r/<slug>/branch-description \
  -H 'content-type: application/json' \
  -d '{"branch": "agent/my-feature", "body": "Fix the burst-merge race\n\n## Context\n\nThe user wanted flaky CI fixed when this branch was started. Concurrent writes to the shared accumulator are the suspected cause.\n\n## Definition of done\n\nSerialize accumulator writes, add a regression test for concurrent merges, and verify the affected tests pass."}'

Use a short title on the first line, followed by ## Context and ## Definition of done sections, separated by blank lines. Context records what the user wanted when the branch or worktree was started and why. Definition of done records the agent's completion criteria: concrete outcomes, verification, and any scope limits. Do not substitute a commit list or invent missing intent. Register this when starting work and update it if the agreed scope changes.

Both sections support Markdown: paragraphs, headings, emphasis, lists, code, tables, and links. Prefer labelled links such as [viewer](http://localhost:8788/...) and [evidence](file:///path/to/report.html); bare HTTP and file:// URLs are also linked automatically. Local-file links remain subject to the browser's restrictions on opening files from a web page.

It shows up next to the branch name in the UI (the icon that looks like an up/down arrow), using the same reader, text-size preference, and fullscreen controls as commit messages. Existing unstructured descriptions remain visible as context until edited into these sections. PUT with an empty or absent body clears it. Read it back with GET .../branch-description?branch=<name>.

2. A "view" is one value

Everything in the API — comments, reviews, diffs — is addressed by the same pair:

{ "kind": "branch", "key": "agent/my-feature", "branch": "agent/my-feature" }

kind is one of branch, commit, branch_pair, commit_pair, range, tree, disk, working. key is canonical within the kind: a branch name, a full sha, a..b, branch:<name> for a tree, or empty for disk. branch is carried for navigation only and is never part of identity.

A human may hand you a URL with a line range on it. These come from clicking a line number in the UI, and they name everything needed to find the code again:

/<slug>/files/<path>#L120            one line of a file, as it is on disk
/<slug>/files/<path>#L120-L134       a span
/<slug>/b/<branch>?f=<path>#L12-L20  a span within a branch's diff
/<slug>/b/<branch>@<sha>?f=<path>#L7 a span within one commit

The file is in the path for the whole-code-base view and in ?f= for every other kind — a branch name can contain slashes, so it cannot be a path segment. Line numbers are the ones shown on the right-hand side of the diff.

To read exactly what a permalink points at, including a small bounded context window, use the preview endpoint. It resolves branch names and branch-backed tree views when the request arrives, so it follows the same moving identity as the UI:

curl -sG "http://localhost:8788/api/r/<slug>/preview" \
  --data-urlencode "kind=branch" \
  --data-urlencode "key=<branch>" \
  --data-urlencode "branch=<branch>" \
  --data-urlencode "path=<path>" \
  --data-urlencode "from=120" \
  --data-urlencode "to=134"

The response includes numbered lines; highlighted identifies the requested span. Missing files or out-of-range lines are errors rather than empty results.

3. Reading review comments

curl -s "http://localhost:8788/api/r/<slug>/comments"

Each comment carries:

  • file, side (old / new / ctx / file), line
  • body — what the human actually wrote
  • anchor_text — the exact line the comment was left on
  • anchor_ctx — about 30 surrounding lines, each {kind, old_line, new_line, text}
  • orphaned — if true, do not trust line. The code under the comment has changed since it was written. Use anchor_text and anchor_ctx to work out what it was actually about before acting on it.
  • resolved — the human has marked this one done.
  • author — null for the human, otherwise the agent that wrote it.

4. Managed reviews

A managed review is a named, stable-id set of comments with a conversation thread per comment. A human hands you an id; you fetch it, do the work, and reply on each row.

Fetch

curl -s "http://localhost:8788/api/r/<slug>/reviews/<review_id>"
{
  "id": "…",
  "view": { "kind": "branch", "key": "agent/my-feature", "branch": "…" },
  "view_label": "branch agent/my-feature",
  "status": "active",            // or "archived" / "merged_away"
  "merged_into": null,
  "merged_from": [],
  "comment_count": 4,
  "unresolved_count": 2,
  "rows": [
    {
      "id": "…",                 // the comment id — this is what you reply to
      "file": "src/thing.rs", "side": "new", "line": 42,
      "anchor_text": "…", "anchor_ctx": [ … ],
      "orphaned": false, "resolved": false,
      "body": "the review comment",
      "thread": [ { "id": "…", "author": "human", "body": "…", "commit_sha": null } ]
    }
  ]
}

Where to commit the fix

Check the review's view.kind before changing any code:

  • branch — the normal case. Commit your fix on that branch (view.key is its name). A branch is a moving target, and landing a commit on it is what addressing the feedback means.
  • anything else (commit, branch_pair, commit_pair, range, tree, disk) — these are fixed points in time or ambiguous targets, not open branches you can add to. Do not guess. Reply explaining what you would change and ask which branch the fix should land on.

Reply to one row

curl -s -X POST "http://localhost:8788/api/r/<slug>/reviews/<review_id>/messages" \
  -H 'Content-Type: application/json' \
  -d '{"comment_id": "<row id>", "body": "Fixed in abc1234 — …", "commit_sha": "abc1234", "author": "agent"}'

Always set commit_sha when your reply corresponds to a commit you made. It is what turns the reply into a link straight to that commit, so nobody has to dig through git log to find which commit answers which comment. Name the same short sha at the start of body too, so a copy-paste of the text still carries it:

Fixed in 1394fb6. The null check now happens before the unwrap, and the error path returns the original message rather than a generic one.

Only omit both when the reply is genuinely just discussion — a clarifying question, "this is already handled at line 88" — with no commit behind it.

Cite specific code in a reply

When a managed-review reply makes a claim about specific code, include a bare, absolute Diffbench URL to the narrowest useful line range that supports it. The managed-review thread makes these URLs clickable and previews their code on hover:

Fixed in 1394fb6. The guard now runs before the unwrap:
http://localhost:8788/<slug>/b/<branch>@1394fb6?f=src%2Fthing.rs#L38-L44

Use the view that actually contains the code being discussed:

  • For code introduced or changed by a fix, prefer that commit's view.
  • For unchanged surrounding code, use a fixed commit or tree view that actually contains the file. A branch diff only contains files changed by that branch.
  • Use /files/<path> only when referring to the current on-disk codebase, not as a substitute for a historical view.

The link should illustrate the claim in the reply, not merely open the top of a large diff. Include only useful citations; do not add a link to every sentence.

When feedback asks why code was deleted or where it went, do not infer the answer from the deletion alone. Use git history and code search to locate the destination, then say explicitly whether the behavior was moved, replaced, or actually removed. If it moved, cite the narrow moved-to range in the exact stable branch, commit, range, comparison, or tree view that contains it. A link to the deletion is not a substitute for finding and citing the destination.

Prefer one commit per comment. If you are addressing several rows in a turn, that is usually several small commits, not one bundle: the sha you report back should show exactly the fix it claims to, not that fix mixed in with three others. Bundle only when the rows are genuinely about the same few lines.

Reply to every row at once

curl -s -X POST "http://localhost:8788/api/r/<slug>/reviews/<review_id>/respond" \
  -H 'Content-Type: application/json' \
  -d '{"responses": [
        {"comment_id": "<id1>", "body": "Fixed in abc1234", "commit_sha": "abc1234"},
        {"comment_id": "<id2>", "body": "Intentional — see the reply on the other row"}
      ]}'

A subset is fine. Every comment_id must belong to the review: one bad id rejects the whole batch rather than leaving the review half-answered.

Post a review of another agent's work

You do not need a human to create the review. To review work another agent produced, read the code, then submit the whole review in one call:

curl -s -X POST "http://localhost:8788/api/r/<slug>/reviews/submit" \
  -H 'Content-Type: application/json' \
  -d '{
        "view": {"kind": "branch", "key": "agent/my-feature"},
        "author": "reviewer-bot",
        "title": "First pass",
        "decision": "changes_requested",
        "comments": [
          {"file": "src/thing.rs", "line": 42, "body": "This unwrap panics on empty input."},
          {"file": "src/other.rs", "line": 7, "side": "old", "anchor_text": "let n = old();", "body": "Why was this removed?"},
          {"file": "src/thing.rs", "body": "No tests cover the new module."}
        ]
      }'
  • view is the same {kind, key} value used everywhere else. Review a branch to give the author something to commit fixes onto.
  • author is your own name (1–64 characters, not human). It is stored on the review and on every comment, and shown in the UI, so the author and the human can tell whose feedback it is.
  • decision is optional: approved, approve_after_addressing or changes_requested. title is optional.
  • Each comment is a file plus a body. With a line (default side is new, the line numbers on the right of the diff) the server reads that line from the view and anchors the comment to its text and surrounding context, so it is flagged orphaned if the branch is rewritten later. Leave line out to comment on the file as a whole. For a deleted line use "side": "old" and give the line's text as anchor_text.
  • comments may be empty (or omitted): open the review first, then add findings as you read — see below.
  • It is all-or-nothing: a bad file, a line past the end, or an empty body rejects the whole submission and nothing is saved. Uncommitted (working) views cannot be reviewed.
  • A merge, rebase or cherry-pick in progress in the repo does not block it: a branch or commit reads from git's object store, which the operation does not touch. Only the uncommitted view waits for git to finish.

Add comments to a review you created

curl -s -X POST "http://localhost:8788/api/r/<slug>/reviews/<review_id>/comments" \
  -H 'Content-Type: application/json' \
  -d '{"author": "reviewer-bot",
       "comments": [{"file": "src/thing.rs", "line": 60, "body": "Another unwrap."}]}'

Same comment shape and same anchoring as submit, also all-or-nothing, and it returns the full review. Only the agent whose author name the review was created with may add to it (403 otherwise — including for a human's review), and only while the review is active. The comments are anchored against the review's own view, so a branch review is anchored to the branch's current head. This is a guard against agents mixing each other up, not authentication: Diffbench has no accounts, so use a distinct, stable author name.

The response is the full review, exactly as GET .../reviews/<id> returns it. Its id is what the author needs: give it to them (or link http://localhost:8788/<slug>/reviews/<id>), and they fetch it, fix, and reply under section 4 as usual. A submitted review is separate from whatever the view is currently collecting, so it never mixes into a human's in-progress review.

Merge two reviews

curl -s -X POST "http://localhost:8788/api/r/<slug>/reviews/merge" \
  -H 'Content-Type: application/json' -d '{"a": "<id_a>", "b": "<id_b>"}'

This creates a new review holding the union of both, with every thread intact. The originals are not deleted — they stay fetchable, marked merged_away and pointing at the new one. Always report the new id back.

5. Searching the code base

The whole repo is indexed, so this is the fastest way to find something:

curl -s "http://localhost:8788/api/r/<slug>/search?q=getUserById"
curl -s "http://localhost:8788/api/r/<slug>/search?q=TODO&case=true&word=true&path=src/"

Matching is substring by default (serId finds getUserById) and needs at least three characters. Results give exact file paths and line numbers.

6. Workstreams: a ticket board, independent of any repo

Workstreams is a separate board for tracking work as tickets, unrelated to any one repo's comments/reviews above. A ticket is a row; each entry is a box in its timeline — the user's initial issue, your progress updates, your final report, the user's feedback — laid out in the order they were posted.

StatusMeaningWho acts next
openReady to be picked upYou
in_progressYou've claimed it and are workingYou
waiting_userYou reported back; needs their reviewThe user
backlogDeliberately shelved, not being worked on—
doneArchived—

Posting feedback on a waiting_user ticket automatically reopens it to open. Posting a report automatically flips it to waiting_user. You never set status by hand for either of those — only to explicitly close (done) or reopen something. backlog is a human prioritization call, not yours to set — a reply on a backlogged ticket does not wake it up.

Creating a ticket

Every ticket belongs to a project (the board's own grouping, shown as the pills across the top of the UI). If whoever asked you to do this didn't say which project it belongs to, list the projects and ask — don't guess:

curl -s "http://localhost:8788/api/workstreams/projects"
curl -s -X POST "http://localhost:8788/api/workstreams/tickets" \
  -H 'Content-Type: application/json' \
  -d '{"title": "Fix flaky upload test", "body": "…", "project_id": "<project-id>"}'

Omitting project_id silently files the ticket under Inbox. Inbox is a triage catch-all, not a default worth reaching for — treat an unspecified project as a question to ask, not a field to skip.

Write every project and ticket title in plain English for a reader who does not know the internal terminology. The title must state the concrete outcome or question, not merely name a subsystem, phase, artifact, or technique. Avoid unexplained terms such as "corpus," "cohort," "finalize," "inversion," or version labels; when a technical term is necessary, explain what it means.

In body, state:

  1. Why this work matters or what problem it fixes.
  2. What will actually be done, in concrete language.
  3. What "done" means, including the observable output or verification.
  4. Important limitations or dependencies, such as whether results are provisional or waiting on another job.

A user should understand the purpose and expected result from the title and first two sentences without opening code, logs, or prior conversation. Prefer "Finish and verify the new training dataset that fixes the accidental five-frame preprocessing bug" over "Finalize corrected 11-frame corpus."

Linking to results

body is rendered as markdown — write links as [label](url), not the bare URL. A pasted W&B or job URL is long and adds nothing to read; a label does:

Bad:  https://ci.example.com/jobs/268e04b4-90dc-4250-bd92-8ab16c992c69
Good: [CI job](https://ci.example.com/jobs/268e04b4-90dc-4250-bd92-8ab16c992c69)

This applies just as well to a result that only exists as a file, not a URL: if an experiment produces an HTML report or plot, save it to disk and link it the same way with a file:// path — [training curve](file:///path/to/plot.html) — rather than describing it in prose or leaving it for the user to go find.

The loop

curl -s "http://localhost:8788/api/workstreams/next?claim=<your-agent-id>"

Returns the oldest open, unclaimed ticket and claims it in one call — an atomic hand-off, so two agents polling at once can't both start the same ticket. {"ticket": null} means nothing is actionable right now (also true if you lost a race to claim the one ticket that was available); wait and poll again. Claiming sets a time-limited lock — send a heartbeat if a single piece of work will run long:

curl -s -X POST "http://localhost:8788/api/workstreams/tickets/<id>/heartbeat" \
  -H 'Content-Type: application/json' -d '{"agent_id": "<your-agent-id>"}'

Read every entry in ticket.entries — the newest one is the current instruction, but earlier ones are the history you're continuing, not context to discard.

Reporting progress and results

curl -s -X POST "http://localhost:8788/api/workstreams/tickets/<id>/entries" \
  -H 'Content-Type: application/json' \
  -d '{"author": "agent", "author_id": "<your-agent-id>", "kind": "progress",
       "body": "Looking into it…"}'

A final report hands the ticket back:

curl -s -X POST "http://localhost:8788/api/workstreams/tickets/<id>/entries" \
  -H 'Content-Type: application/json' \
  -d '{"author": "agent", "author_id": "<your-agent-id>", "kind": "report",
       "body": "Fixed. See the PR.",
       "artifacts": [{"type": "link", "name": "PR #42", "url": "https://github.com/org/repo/pull/42"}]}'

Attach a file (a screenshot, a log) by uploading it first, then including the returned descriptor verbatim in artifacts:

curl -s -X POST "http://localhost:8788/api/workstreams/artifacts" -F "files=@./after.png"
# -> {"artifacts": [{"id": "...", "type": "image", "url": "/workstream-files/...", ...}]}

If a ticket is fully done and needs nothing further, close it explicitly instead of leaving it waiting_user:

curl -s -X POST "http://localhost:8788/api/workstreams/tickets/<id>/status" \
  -H 'Content-Type: application/json' -d '{"status": "done"}'

Release a claim without finishing (blocked, going idle) so someone else can pick it up:

curl -s -X POST "http://localhost:8788/api/workstreams/tickets/<id>/release" \
  -H 'Content-Type: application/json' -d '{"agent_id": "<your-agent-id>"}'

7. General notes

  • Treat each row as an independent, narrow thread about one piece of code at the time it was written — not one flat conversation about the whole review.
  • Address each row like ordinary review feedback: read the file, make the change, reply with what you did. Do not silently skip a row.
  • If a human pastes comments to you directly rather than giving you a review id, just work from what they pasted. The managed-review flow is for when an id is explicitly in play.

When a call fails

Errors are JSON: {"detail": "...", "kind": "..."}. Which ones are worth retrying:

StatuskindMeaningWhat to do
503busyGit is mid-operation in that folder (a merge, a rebase, an index.lock), or the repository is still opening.Wait the seconds in the Retry-After header (5), then retry.
503unavailableThe repo's folder is missing or not answering — a moved checkout, an unplugged drive — or the thing asked for cannot exist right now (the uncommitted view mid-merge, with no earlier snapshot to show). The detail says which.Tell the human; do not retry in a loop. A missing repo's comments and reviews stay readable, and it reattaches by itself when the folder is back.
403forbiddenThe request named a host other than localhost, or came from another site's page.Use http://localhost:8788 exactly.
4xxotherThe request itself is wrong.Fix it; retrying will not help.

GET /api/repos lists every watched repo with an availability field — {"state": "ready"}, {"state": "starting"}, or {"state": "unavailable", "reason": "..."} — so you can check before starting work in one.

Saving a comment (POST /api/r/<slug>/comments) accepts an optional client_id (8–64 letters, digits and dashes, e.g. a UUID) that becomes the comment's id. Send the same one when you retry a save that timed out: if the first attempt had in fact been stored, the server returns that comment instead of storing a second copy.