Skip to content

The platform for continuously improving AI agents.

3.6 KB
Raw Blame
from __future__ import annotations

import pytest

from overbae.services.finetuning_runner import (
    LIFECYCLE_COMPLETED,
    LIFECYCLE_DEPLOYMENT,
    LIFECYCLE_EVALUATION,
    LIFECYCLE_FAILED,
    LIFECYCLE_SETUP,
    LIFECYCLE_TRAINING,
    lifecycle_stage,
    scrub_backend_names,
)

pytestmark = pytest.mark.covers("state-machines")


def test_setup_before_first_step():
    assert lifecycle_stage("running", {"trained_steps": 0}) == LIFECYCLE_SETUP
    assert lifecycle_stage("running", {"stage": "downloading_base_model"}) == LIFECYCLE_SETUP
    assert lifecycle_stage("queued", {}) == LIFECYCLE_SETUP


def test_training_when_steps_advance():
    assert (
        lifecycle_stage("running", {"trained_steps": 5, "total_steps": 300}) == LIFECYCLE_TRAINING
    )


def test_deployment_from_status():
    assert lifecycle_stage("deploying", {"trained_steps": 300}) == LIFECYCLE_DEPLOYMENT


def test_evaluation_while_final_eval_runs():
    prog = {"judge_evals": [{"kind": "final", "status": "running"}]}
    assert lifecycle_stage("succeeded", prog) == LIFECYCLE_EVALUATION


def test_evaluation_while_incumbent_after_runs_without_final_eval():
    prog = {"judge_evals": [{"kind": "incumbent_after", "status": "running"}]}
    assert lifecycle_stage("succeeded", prog) == LIFECYCLE_EVALUATION


def test_completed_when_final_eval_done():
    prog = {"judge_evals": [{"kind": "final", "status": "completed", "aggregate_score": 0.8}]}
    assert lifecycle_stage("succeeded", prog) == LIFECYCLE_COMPLETED
    # No evals at all (evals disabled) → still COMPLETED once succeeded.
    assert lifecycle_stage("succeeded", {}) == LIFECYCLE_COMPLETED


def test_terminal_offramps():
    assert lifecycle_stage("failed", {"trained_steps": 10}) == LIFECYCLE_FAILED
    assert lifecycle_stage("cancelled", {}) == "cancelled"


def test_backward_compat_missing_progress():
    assert lifecycle_stage("running", None) == LIFECYCLE_SETUP
    assert lifecycle_stage("deploying", None) == LIFECYCLE_DEPLOYMENT
    assert lifecycle_stage("succeeded", None) == LIFECYCLE_COMPLETED


def test_scrub_backend_names_redacts_all_providers():
    assert scrub_backend_names("Client error: Secret hf_access_token not found in Modal") == (
        "Client error: Secret hf_access_token not found in the provider"
    )
    assert scrub_backend_names("NEBIUS_API_KEY is not set") == "the provider_API_KEY is not set"
    assert (
        scrub_backend_names("Deployed via Modal Functions") == "Deployed via the provider Functions"
    )
    assert scrub_backend_names("") == ""
    assert scrub_backend_names("all good, no backend mentioned") == "all good, no backend mentioned"
    # A letter-only trailing boundary would rewrite identifiers like ``modal_shared``.
    assert scrub_backend_names("No module named 'modal_shared'") == "No module named 'modal_shared'"
    assert (
        scrub_backend_names("Pre-warming GPU (L4) — building the cold-start memory snapshot…")
        == "Pre-warming GPU — building the cold-start memory snapshot…"
    )


def test_sanitize_job_error_hides_internal_failures():
    from overbae.services.finetuning_runner import (
        USER_FACING_DEPLOY_FAILURE,
        USER_FACING_TRAIN_FAILURE,
        sanitize_job_error,
    )

    assert (
        sanitize_job_error("RuntimeError: train.py exited with code 1") == USER_FACING_TRAIN_FAILURE
    )
    assert (
        sanitize_job_error("GPU pre-warm failed: CUDA out of memory") == USER_FACING_DEPLOY_FAILURE
    )
    assert sanitize_job_error(
        "Polling timed out — fine-tuning did not complete within 4 hours"
    ) == ("Polling timed out — fine-tuning did not complete within 4 hours")