# Asserting on Tool Calls & Conversations

A rubric sees the workspace, but many failures are invisible there: the refund
was issued but no supervisor was ever notified, the write happened before the
read, the credential was printed into the transcript. The rollout captures
tool calls and the conversation too, and hands both to your rubric.

---

## The rubric input contract

The rubric is still a Python program that prints JSON, but in v0.0.2 it also
receives the rollout on **stdin**: a JSON document with at least

| Key | Contents |
|---|---|
| `tool_calls` | Every tool invocation the agent made, in order. |
| `conversation` | The message transcript for the rollout. |

and three environment variables:

| Variable | Meaning |
|---|---|
| `SHIPYARD_WORKDIR` | The agent's workspace (same as before). |
| `ROLLOUTS_DIR` | The current rollout directory. |
| `PASS_THRESHOLD` | The configured pass threshold from `[tests]`. |

A rubric that ignores stdin and grades only `SHIPYARD_WORKDIR` keeps working
unchanged, which is why `shipyard validate`'s rubric probe accepts
stdin-contract rubrics: the probe checks that the rubric parses and can run,
not what it reads.

```python
import json
import os
import sys

payload = json.load(sys.stdin)
tool_calls = payload.get("tool_calls", [])
conversation = payload.get("conversation", [])
workdir = os.environ["SHIPYARD_WORKDIR"]

def tool_name(call: dict) -> str:
    # Tool calls are captured verbatim from the harness; the name is
    # carried on the record the harness reported.
    return call.get("tool") or call.get("name") or ""
```

## Where the inputs are written

Each rollout directory gets the captured conversation and tool calls as their
own files:

```
<eval-dir>/rollouts/<run-id>/
├── trajectory.json      # ATIF steps, in order
├── conversation.json    # message transcript
├── tool_calls.json      # every tool invocation
├── metrics.json         # verdict, usage, timing
├── rubrics.json         # itemized scoring result
├── run.json             # provenance
├── aggregate.json       # k, pass@k, pass^k, mean, sigma
├── results.json         # suite totals
└── artifacts/
```

With `--repeat K` the per-repeat files live in `repeat-1/` ... `repeat-K/`
subdirectories, and each repeat gets its own `tool_calls.json` and
`conversation.json`. The suite-level `results.json`, plus `run.json` and
`aggregate.json`, stay at the run root. Anything the rubric reads on stdin is
the data from the repeat it is grading, not a merge across repeats.

If a harness does not report part of the data, the corresponding key may be
absent. Read it defensively (`.get("tool_calls", [])`) and fail the check
rather than assuming it passed.

## Rubric snippets

### 1. Must-call

The agent cancelled the order and processed the refund, but never opened the
supervisor ticket:

```python
def tool_name(call):
    return call.get("tool") or call.get("name") or ""

def called(*names):
    return any(tool_name(c) in names for c in tool_calls)

items = [
    {
        "name": "supervisor_ticket_created",
        "score": 1.0 if called("create_ticket", "create_supervisor_ticket") else 0.0,
        "threshold": 1.0,
        "passed": called("create_ticket", "create_supervisor_ticket"),
        "description": "Agent opened a supervisor ticket as part of the refund",
    },
]

print(json.dumps({
    "items": items,
    "passed": all(item["passed"] for item in items),
}))
```

### 2. Ordering and invariants

Read before write, and never call the legacy endpoint:

```python
def index_of(*names):
    for i, call in enumerate(tool_calls):
        if tool_name(call) in names:
            return i
    return -1

read_i = index_of("read_file", "read")
write_i = index_of("write_file", "write", "edit_file")
ordered = read_i != -1 and write_i != -1 and read_i < write_i

used_legacy = called("charge_card_v1")
items = [
    {
        "name": "read_before_write",
        "score": 1.0 if ordered else 0.0,
        "threshold": 1.0,
        "passed": ordered,
        "description": "Agent read the record before mutating it",
    },
    {
        "name": "no_legacy_charge_api",
        "score": 0.0 if used_legacy else 1.0,
        "threshold": 1.0,
        "passed": not used_legacy,
        "description": "Agent never used the deprecated charge_card_v1 endpoint",
    },
]

print(json.dumps({
    "items": items,
    "passed": all(item["passed"] for item in items),
}))
```

Ordering checks are written against the captured list, so they are about the
agent's behavior, not about timestamps that a harness may not expose.

### 3. Conversation content

The escalation has to appear in the message transcript, not just in a tool
call:

```python
def message_text(message):
    content = message.get("content", "")
    if isinstance(content, str):
        return content
    if isinstance(content, list):
        return " ".join(
            part.get("text", "") for part in content if isinstance(part, dict)
        )
    return ""

transcript = "\n".join(message_text(m) for m in conversation)
mentioned = "supervisor" in transcript.lower()

items = [
    {
        "name": "supervisor_mentioned_to_user",
        "score": 1.0 if mentioned else 0.0,
        "threshold": 1.0,
        "passed": mentioned,
        "description": "Agent told the user a supervisor would review the refund",
    },
]

print(json.dumps({
    "items": items,
    "passed": all(item["passed"] for item in items),
}))
```

## Declarative assertions are coming

These checks work today because `tool_calls` and `conversation` are stable
rubric inputs. First-class declarative tool assertions, so a must-call or
ordering check needs no Python at all, arrive in **0.0.3**. Until then, the
snippets above are the supported pattern: standard-library Python over the
captured rollout, scored with the same items/threshold JSON as every other
rubric.

See [Writing Rubrics](/docs/eval-and-rubrics) for the rubric output contract
and [Rollouts & ATIF](/docs/rollouts-and-atif) for the file schemas.
