Skip to content

The platform for continuously improving AI agents.

4.7 KB
Raw Blame
"""The train contract and the training export agree on every row shape."""

from __future__ import annotations

import pandas as pd
import pytest

from overbae.services.datasets import contract
from overbae.services.datasets.rows import row_from_record
from overbae.services.finetuning_validator import row_to_finetuning_line, validate_rows

pytestmark = pytest.mark.covers("data-logic")

TURNS = [{"role": "user", "content": "hi"}, {"role": "assistant", "content": "hello"}]
TOOLS = [{"type": "function", "function": {"name": "f", "parameters": {"type": "object"}}}]

SHAPES = {
    "transcript": {"messages": TURNS},
    "transcript with tools": {"messages": TURNS, "tools": TOOLS},
    "trace row": {"input": "hi", "output": "hello", "messages": TURNS, "trace_id": "t1"},
    "json text": {
        "messages": '[{"role": "user", "content": "hi"}, {"role": "assistant", "content": "yo"}]'
    },
    "no assistant turn": {"messages": TURNS[:1]},
    "empty transcript": {"messages": []},
    "not a list": {"messages": "hello"},
    "eval row": {"input": "q", "expected_output": "a"},
    "prompt and completion": {"prompt": "q", "completion": "a"},
}


@pytest.mark.parametrize("name", SHAPES)
def test_a_row_fits_train_exactly_when_the_export_trains_on_it(name):
    record = SHAPES[name]
    fits = contract.measure(pd.DataFrame([record]))["train"]["ok"]
    try:
        exported = validate_rows([row_to_finetuning_line(row_from_record(0, record))]).valid
    except ValueError:
        exported = False
    assert fits is exported
    assert fits is (name in {"transcript", "transcript with tools", "trace row", "json text"})


def test_one_bad_row_fails_the_train_contract_and_names_it():
    df = pd.DataFrame([{"messages": TURNS}, {"messages": None}])
    report = contract.measure(df)["train"]
    assert report["ok"] is False
    assert report["failures"] == [{"row": 1, "reason": "messages is empty"}]


@pytest.mark.parametrize(
    ("rows", "reason"),
    [
        ([{"question": "q"}], "no input column"),
        (
            [{"input": "q", "expected_output": "a"}, {"input": " ", "expected_output": "a"}],
            "1 rows have an empty input",
        ),
        ([{"input": "q", "expected_output": None}], "no expected_output values"),
        ([{"input": "q"}], "no expected_output values"),
    ],
)
def test_the_eval_contract_says_what_is_missing(rows, reason):
    report = contract.measure(pd.DataFrame(rows))["eval"]
    assert (report["ok"], report["reason"]) == (False, reason)


def test_an_empty_table_fits_nothing():
    report = contract.measure(pd.DataFrame())
    assert report["train"] == report["eval"] == {"ok": False, "reason": "no rows"}


def test_a_table_without_text_is_marked_as_not_fixable():
    numbers = contract.measure(pd.DataFrame([{"sensor": 1, "temp": 20.5}]))
    words = contract.measure(pd.DataFrame([{"prompt": "hi", "completion": "hello"}]))
    assert numbers["train"]["fixable"] is numbers["eval"]["fixable"] is False
    assert "fixable" not in words["train"] and "fixable" not in words["eval"]


def test_a_transcript_that_landing_cut_does_not_fit_train():
    from overbae.services.datasets import land

    huge = [
        {"role": "user", "content": "x" * (land.MAX_CELL_CHARS + 10)},
        {
            "role": "assistant",
            "content": None,
            "tool_calls": [
                {
                    "id": "c1",
                    "type": "function",
                    "function": {"name": "f", "arguments": '{"a": "' + "y" * 400_000 + '"}'},
                }
            ],
        },
        {"role": "tool", "tool_call_id": "c1", "content": "ok"},
        {"role": "assistant", "content": "done"},
    ]
    row = land.bounded_row({"messages": huge})
    report = contract.measure(pd.DataFrame([row, {"messages": TURNS}]))["train"]
    assert report["ok"] is False
    assert report["failures"] == [{"row": 0, "reason": "a value was cut at landing"}]


def test_a_batched_eval_output_is_judged_by_every_frame():
    decision = {
        "decision": {"state": "s", "question": "q?", "kind": "choice", "options": ["a", "b"]}
    }
    reference = {"probabilities": [1.0, 0.0]}
    referenced = pd.DataFrame([{"input": decision, "expected_output": reference}])

    def frames(*parts):
        return contract.measure_frames(
            ((part, None) for part in parts), ["input", "expected_output"]
        )

    # A later frame's decision without a reference fails the output.
    unreferenced = pd.DataFrame([{"input": decision, "expected_output": None}])
    assert not frames(referenced, unreferenced)["eval"]["ok"]
    # A text frame without references still counts its input type.
    text = pd.DataFrame([{"input": "plain question", "expected_output": None}])
    assert set(frames(referenced, text)["eval"]["input_types"]) == {"decision", "text"}