Skip to content

The platform for continuously improving AI agents.

9.9 KB
Raw Blame
"""Train + eval from one source: the cut, the pair of datasets it lands, and
the surfaces that create it."""

from __future__ import annotations

import uuid

import pytest
from mcp_fixtures import call_tool
from rest_framework.test import APIClient
from rest_framework_simplejwt.tokens import RefreshToken

from overbae.core.errors import DatasetError
from overbae.models import Dataset, Project, ProjectMembership, Span, User
from overbae.services.datasets import dispatch, land, paths, store

pytestmark = [pytest.mark.django_db(transaction=True), pytest.mark.covers("data-logic")]

ROWS = [{"input": f"q{i}", "expected_output": f"a{i}"} for i in range(10)]


def _landing() -> land.Landing:
    return land.read_rows([dict(r) for r in ROWS])


def _inputs(part: land.Landing) -> list[str]:
    return [r["input"] for r in part.rows]


def test_split_takes_the_eval_slice_from_the_head_tail_or_a_fixed_draw():
    train, evaluation = _landing().split(eval_percent=20, position="head")
    assert _inputs(evaluation) == ["q0", "q1"]
    assert _inputs(train) == [f"q{i}" for i in range(2, 10)]
    train, evaluation = _landing().split(eval_percent=20, position="tail")
    assert _inputs(evaluation) == ["q8", "q9"]
    first = _landing().split(eval_percent=30, position="random")
    again = _landing().split(eval_percent=30, position="random")
    assert _inputs(first[1]) == _inputs(again[1]) and len(first[1].rows) == 3
    assert sorted(_inputs(first[0]) + _inputs(first[1])) == sorted(_inputs(_landing()))
    assert _inputs(first[0]) == sorted(_inputs(first[0]), key=lambda s: int(s[1:]))


def test_split_keeps_at_least_one_row_on_each_side():
    two = land.read_rows(ROWS[:2])
    train, evaluation = two.split(eval_percent=1, position="tail")
    assert len(train.rows) == 1 and len(evaluation.rows) == 1
    train, evaluation = two.split(eval_percent=99, position="tail")
    assert len(train.rows) == 1 and len(evaluation.rows) == 1
    with pytest.raises(land.LandError):
        land.read_rows(ROWS[:1]).split(eval_percent=50, position="tail")
    with pytest.raises(land.LandError):
        two.split(eval_percent=50, position="middle")


def _project() -> Project:
    return Project.objects.create(name="P", slug=f"p-{uuid.uuid4().hex[:8]}")


def _user() -> User:
    return User.objects.create_user(
        email=f"u-{uuid.uuid4().hex[:6]}@test.com",
        password="pw",
        clerk_user_id=f"clerk_{uuid.uuid4().hex}",
    )


def _client(project) -> APIClient:
    user = _user()
    ProjectMembership.objects.create(user=user, project=project)
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f"Bearer {RefreshToken.for_user(user).access_token}")
    return client


def test_create_split_lands_disjoint_readable_sources_without_diagnosis():
    project, user = _project(), _user()
    train, evaluation = dispatch.create_split(
        project=project,
        user=user,
        name="Support",
        source={"rows": [dict(r) for r in ROWS]},
        eval_percent=20,
        position="tail",
    )
    train.refresh_from_db()
    evaluation.refresh_from_db()
    assert (train.name, train.intent, train.state) == ("Support train", "train", "idle")
    assert (evaluation.name, evaluation.intent, evaluation.state) == (
        "Support eval",
        "eval",
        "idle",
    )
    assert train.source.rows == 8 and evaluation.source.rows == 2
    assert train.source_spec["split"] == {
        "eval_percent": 20,
        "position": "tail",
        "group_by": [],
        "stratify_by": None,
        "deduplicate": True,
        "role": "train",
        "sibling": str(evaluation.id),
    }
    assert evaluation.source_spec["split"]["sibling"] == str(train.id)
    frames = [store.read_frame(paths.cell_path(ds.id, ds.source.id)) for ds in (train, evaluation)]
    assert list(frames[0][store.SOURCE_ROW]) == list(range(8))
    assert list(frames[1][store.SOURCE_ROW]) == [0, 1]


def test_create_split_refuses_a_bad_cut_or_a_short_source_before_creating():
    project, user = _project(), _user()
    for bad in ({"eval_percent": 0, "position": "tail"}, {"eval_percent": 20, "position": "x"}):
        with pytest.raises(DatasetError):
            dispatch.create_split(
                project=project, user=user, name="S", source={"rows": ROWS}, **bad
            )
    with pytest.raises(DatasetError, match="Two rows"):
        dispatch.create_split(
            project=project,
            user=user,
            name="S",
            source={"rows": ROWS[:1]},
            eval_percent=20,
            position="tail",
        )
    assert Dataset.objects.filter(project=project).count() == 0


def test_raw_decisions_split_by_evidence_and_options_without_splitting_target_variants():
    project, user = _project(), _user()
    rows = []
    for i in range(6):
        row = {
            "question": "Which option follows from the evidence?",
            "state": f"Case {i // 2}",
            "kind": "choice",
            "options": ["yes", "no"] if i % 2 else ["allow", "deny"],
            "target": [1.0, 0.0],
            "source": "example",
        }
        rows.extend([row, {**row, "target": [0.0, 1.0], "source": "another-copy"}])
    train, evaluation = dispatch.create_split(
        project=project,
        user=user,
        name="Runtime decisions",
        source={"rows": rows},
        eval_percent=50,
        position="head",
    )
    train.refresh_from_db()
    evaluation.refresh_from_db()
    assert train.state == evaluation.state == "idle"
    frames = [store.read_frame(paths.cell_path(ds.id, ds.source.id)) for ds in (train, evaluation)]
    assert [len(frame) for frame in frames] == [6, 6]
    clusters = [
        {(row["state"], tuple(row["options"])) for row in frame.to_dict("records")}
        for frame in frames
    ]
    assert len(clusters[0]) == len(clusters[1]) == 3
    assert not clusters[0] & clusters[1]
    assert train.source_spec["contamination_report"]["content_overlap"] == 0


def test_split_endpoint_returns_the_pair_and_validates_the_cut():
    project = _project()
    client = _client(project)
    body = {
        "name": "Support",
        "project": str(project.id),
        "source": {"rows": ROWS},
        "eval_percent": 30,
        "position": "head",
    }
    res = client.post("/api/datasets/split/", body, format="json")
    assert res.status_code == 201, res.content
    assert res.data["train"]["intent"] == "train" and res.data["eval"]["intent"] == "eval"
    assert res.data["train"]["name"] == "Support train"
    train = Dataset.objects.get(pk=res.data["train"]["id"])
    evaluation = Dataset.objects.get(pk=res.data["eval"]["id"])
    assert train.source.rows == 7 and evaluation.source.rows == 3
    bad = client.post("/api/datasets/split/", {**body, "eval_percent": 100}, format="json")
    assert bad.status_code == 400 and "eval_percent" in bad.data
    bad = client.post("/api/datasets/split/", {**body, "position": "middle"}, format="json")
    assert bad.status_code == 400 and "position" in bad.data


@pytest.mark.django_db(transaction=True)
def test_mcp_create_from_traces_with_split_returns_both_datasets():

    from overbae.models import APIToken
    from overbae.services.mcp.context import MCPContext

    project, user = _project(), _user()
    ProjectMembership.objects.create(user=user, project=project)
    token = APIToken(
        scope={
            "scope": "project",
            "resourceIds": [str(project.id)],
            "permission": ["read", "write"],
        }
    )
    context = MCPContext(user=user, token=token, project=project)
    traces = []
    for _ in range(4):
        trace_id = uuid.uuid4().hex
        traces.append(trace_id)
        Span.objects.create(
            span_id=uuid.uuid4().hex[:16],
            trace_id=trace_id,
            project=project,
            span_type="entry_point",
            name="run",
            start_time_ns=1,
            end_time_ns=2,
            duration_ns=1,
            attributes={"overmind.input.data": trace_id, "overmind.output.data": "answer"},
        )

    def call(arguments):
        return call_tool("create_dataset_from_traces", arguments, context)

    refused = call({"name": "T", "trace_ids": traces, "intent": "train", "split": {}})
    assert refused.isError is True
    result = call(
        {"name": "T", "trace_ids": traces, "split": {"eval_percent": 25, "position": "head"}}
    )
    assert result.isError is False, result.structuredContent
    body = result.structuredContent
    assert body["dataset"]["name"] == "T train" and body["eval_dataset"]["name"] == "T eval"
    assert body["traces"] == 4 and len(body["resource_links"]) == 4
    train = Dataset.objects.get(pk=body["dataset"]["id"])
    evaluation = Dataset.objects.get(pk=body["eval_dataset"]["id"])
    assert train.source.rows == 3 and evaluation.source.rows == 1


def test_native_decision_targets_do_not_split_identical_inputs():
    from overbae.services.datasets.partition import content_key

    decision = {
        "state": "A coin is tossed.",
        "question": "Which side?",
        "kind": "choice",
        "options": ["Heads", "Tails"],
    }
    first = {"decision": {**decision, "target_probabilities": [0.2, 0.8]}}
    second = {"decision": {**decision, "target_probabilities": [0.6, 0.4], "weight": 2}}
    assert content_key(first) == content_key(second)
    different = {"decision": {**second["decision"], "state": "Two coins are tossed."}}
    assert content_key(first) != content_key(different)


def test_shared_group_identity_keeps_question_variants_together():
    from overbae.services.datasets.partition import split_rows

    records = [
        {
            "group_id": f"case-{case}",
            "question": f"Question {variant}",
            "state": f"Case {case}",
            "target": [0, 1],
        }
        for case in range(10)
        for variant in range(3)
    ]
    train, validation, report = split_rows(records, eval_percent=20, position="random")
    assert not ({r["group_id"] for r in train} & {r["group_id"] for r in validation})
    assert "group_id" in report["group_by"]