The platform for continuously improving AI agents.
"""Cancelling a native evaluation ends it in one bounded pass: provider calls are stopped
best effort, completed evidence is kept, and nothing resumes or re-dispatches it."""
import pytest
from conftest import frozen_dataset
from django.urls import reverse
from rest_framework.test import APIClient
from overbae.models import NativeEvaluationPlan, Project, ProjectMembership, User
from overbae.services import model_workflows, native_evaluation
from overbae.tasks.native_evaluation import advance_plan, reconcile
pytestmark = [pytest.mark.django_db, pytest.mark.covers("state-machines")]
@pytest.fixture
def running(monkeypatch):
user = User.objects.create_user(email="[email protected]")
project = Project.objects.create(name="Cancel", slug="cancel", owner=user)
ProjectMembership.objects.create(project=project, user=user)
dataset = frozen_dataset(
project,
[
{
"input": {
"decision": {
"state": "E",
"question": "Q",
"kind": "choice",
"options": ["a", "b"],
}
},
"expected_output": {"probabilities": [1, 0]},
}
],
contract="eval",
)
plan = NativeEvaluationPlan.objects.create(
project=project,
final_cell=dataset.active_cell,
state="running",
request_key="running-evaluation",
config={"participants": []},
calls={
"verify_inputs": {"state": "completed", "receipt": "kept"},
"final_base": {"state": "running", "id": "fc-live"},
"final_candidate": {"state": "running", "id": "fc-gone"},
},
results={"retained": "evidence"},
)
stopped = []
class Call:
def __init__(self, call_id):
self.call_id = call_id
def cancel(self, **options):
if self.call_id == "fc-gone":
raise RuntimeError("call not found")
stopped.append((self.call_id, options))
monkeypatch.setattr(native_evaluation.modal.FunctionCall, "from_id", Call)
client = APIClient()
client.force_authenticate(user)
return client, plan, user, stopped
def test_cancel_stops_provider_calls_and_ends_without_hanging(
running, monkeypatch, django_capture_on_commit_callbacks
):
client, plan, user, stopped = running
dispatched = []
monkeypatch.setattr(advance_plan, "delay", lambda plan_id: dispatched.append(plan_id))
with django_capture_on_commit_callbacks(execute=True):
response = client.post(reverse("native-evaluation-cancel", args=[plan.pk]))
assert response.status_code == 202
assert response.data["state"] == "cancelling"
assert dispatched == [str(plan.pk)]
plan.refresh_from_db()
assert plan.config["cancellation"]["by"] == str(user.pk)
assert native_evaluation.ready_stages(plan) == []
advance_plan(str(plan.pk))
plan.refresh_from_db()
assert plan.state == "cancelled"
assert stopped == [("fc-live", {"terminate_containers": True})]
# A stop the provider refused is recorded on its call; the plan still ends.
assert plan.calls["final_candidate"]["state"] == "cancelled"
assert "call not found" in plan.calls["final_candidate"]["cancellation_error"]
assert plan.calls["verify_inputs"] == {"state": "completed", "receipt": "kept"}
assert plan.results == {"retained": "evidence"}
assert model_workflows.next_actions("native_evaluation", plan) == ["get_job"]
dispatched.clear()
reconcile()
assert dispatched == []
assert (
client.post(reverse("native-evaluation-cancel", args=[plan.pk])).data["state"]
== "cancelled"
)
assert (
client.post(reverse("native-evaluation-pause", args=[plan.pk])).data["state"] == "cancelled"
)
assert (
client.post(reverse("native-evaluation-resume-evaluation", args=[plan.pk]), {}).status_code
== 400
)
assert client.delete(reverse("native-evaluation-detail", args=[plan.pk])).status_code == 204
def test_other_projects_cannot_cancel(running):
_client, plan, _user, _stopped = running
stranger = User.objects.create_user(email="[email protected]")
client = APIClient()
client.force_authenticate(stranger)
assert client.post(reverse("native-evaluation-cancel", args=[plan.pk])).status_code == 404
plan.refresh_from_db()
assert plan.state == "running"