Skip to content

The platform for continuously improving AI agents.

2.2 KB
Raw Blame
from copy import deepcopy

import pytest
from test_experiment_engine import checkpoint_schema as checkpoint_schema
from test_experiment_engine import flow

from overbae.celery import app
from overbae.models import APIToken, DataPartitionPlan, ResearchAction

from .stack import drain, settle

pytestmark = pytest.mark.covers("recovery")


def test_saved_flow_advances_once_on_worker_and_keeps_its_revision(
    rest_for, worker, checkpoint_schema, agent_wakes
):
    experiment, user = flow()
    key, _ = APIToken.create_for_user(user, project=experiment.project)
    rest = rest_for(key)
    path = f"/api/research-experiments/{experiment.pk}/"
    review = rest.request("POST", path + "plan/", json={"revision": 2})
    assert review["cost_usd"] is None
    request = {
        "revision": 2,
        "request_key": "native-execution",
        "plan_fingerprint": review["fingerprint"],
    }
    started = rest.request("POST", path + "run/", json=request)
    run = started["record"]["runs"][0]["id"]

    document = deepcopy(started["record"]["document"])
    document["parameters"]["seed"]["default"] = 53
    rest.request(
        "POST",
        path + "revise/",
        json={"expected_version": 2, "request_key": "edit-future", "document": document},
    )
    replay = rest.request("POST", path + "run/", json=request)
    assert replay["record"]["runs"][0]["id"] == run
    for _ in range(2):
        app.send_task("overbae.tasks.research.advance_flow", args=[run])

    def terminal():
        current = rest.request("GET", path)["record"]["runs"][0]
        return current if current["state"] in {"completed", "failed", "exhausted"} else None

    finished = settle(worker, terminal)
    assert finished["state"] == "completed", finished["result"]
    assert finished["revision"] == 2
    assert finished["parameters"]["seed"] == 37
    assert set(finished["result"]["outputs"]) == {"training", "development"}
    plans = DataPartitionPlan.objects.filter(project=experiment.project)
    assert plans.count() == 1
    actions = ResearchAction.objects.filter(agent__assignment_id=run)
    assert actions.filter(tool="platform").count() == 1
    assert not actions.filter(tool="model").exists()
    drain(worker)