Skip to content

The platform for continuously improving AI agents.

19.1 KB
Raw Blame
import uuid

from django.conf import settings
from django.db import models

from .leases import Leased


class TrainingPreparation(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    cell = models.ForeignKey(
        "overbae.Cell", on_delete=models.CASCADE, related_name="training_preparations"
    )
    validation_cell = models.ForeignKey(
        "overbae.Cell",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name="validation_preparations",
    )
    signature = models.CharField(max_length=64, unique=True)
    config = models.JSONField(default=dict)
    state = models.CharField(max_length=16, default="queued")
    remote_id = models.CharField(max_length=255, blank=True, default="")
    # Names the worker that started the preparation; its writes and its spawn check it.
    attempt = models.UUIDField(null=True, blank=True)
    report = models.JSONField(default=dict)
    error = models.TextField(blank=True, default="")
    deadline = models.DateTimeField()
    touched_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def current_training_job(self):
        # A new request after a cancel adds a job; the cancelled one stays as history.
        jobs = self.training_jobs.order_by("created_at")
        return jobs.exclude(status="cancelled").first() or jobs.last()


class TrainingExperiment(Leased):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    project = models.ForeignKey("overbae.Project", on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    purpose = models.TextField()
    request_key = models.CharField(max_length=128)
    variants = models.JSONField(default=list)
    protocol = models.JSONField(default=dict)
    evaluation = models.ForeignKey(
        "overbae.NativeEvaluationPlan", on_delete=models.PROTECT, null=True, blank=True
    )
    state = models.CharField(max_length=24, default="draft")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["project", "request_key"], name="unique_training_experiment_request"
            )
        ]


class FinetuningJob(models.Model):
    """API-side handle for one training run. A Celery worker dispatches the run
    to the configured provider and reconciles the remote state back onto this row.
    """

    class Status(models.TextChoices):
        PREPROCESSING = "preprocessing", "Preparing data"
        AWAITING_LAUNCH = "awaiting_launch", "Awaiting launch"
        SUBMISSION_UNKNOWN = "submission_unknown"
        QUEUED = "queued"
        PREPARING = "preparing"
        RUNNING = "running"
        DEPLOYING = "deploying"  # training done, waiting for Modal deployment
        SUCCEEDED = "succeeded"
        FAILED = "failed"
        CANCELLED = "cancelled"

    class Tier(models.TextChoices):
        COMPACT = "compact"
        SMALL = "small"
        MID = "mid"
        LARGE = "large"

    class Provider(models.TextChoices):
        # Legacy providers — retired backends kept so existing job rows render;
        # new submissions only go to MODAL.
        NEBIUS = "nebius"
        TINKER = "tinker"
        MODAL = "modal"

    class SplitMethod(models.TextChoices):
        ORDERED = "ordered"  # last N% by row index
        RANDOM = "random"  # GroupShuffleSplit, trace-safe (default)

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    project = models.ForeignKey(
        "overbae.Project",
        on_delete=models.CASCADE,
        related_name="finetuning_jobs",
    )
    capability = models.ForeignKey(
        "overbae.Capability",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="finetuning_jobs",
    )
    dataset = models.ForeignKey(
        "overbae.Dataset",
        on_delete=models.PROTECT,
        related_name="finetuning_jobs",
    )
    # The cells the job trains and validates on; the dataset FKs only group
    # jobs per dataset.
    cell = models.ForeignKey(
        "overbae.Cell",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="finetuning_jobs",
    )
    validation_cell = models.ForeignKey(
        "overbae.Cell",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="validation_finetuning_jobs",
    )
    eval_cell = models.ForeignKey(
        "overbae.Cell",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="evaluation_finetuning_jobs",
    )
    validation_enabled = models.BooleanField(default=True)
    validation_split_ratio = models.FloatField(default=0.2)
    validation_dataset = models.ForeignKey(
        "overbae.Dataset",
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="validation_finetuning_jobs",
    )
    split_method = models.CharField(
        max_length=16,
        choices=SplitMethod.choices,
        default=SplitMethod.RANDOM,
    )
    triggered_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="triggered_finetuning_jobs",
    )

    eval_dataset = models.ForeignKey(
        "overbae.Dataset",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="eval_finetuning_jobs",
    )
    eval_set = models.ForeignKey(
        "overbae.EvalSet",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="finetuning_jobs",
    )
    eval_incumbent_before = models.BooleanField(default=False)
    eval_judge_model = models.CharField(max_length=255, blank=True, default="")
    benchmark_models = models.JSONField(default=list, blank=True)
    eval_incumbent_after = models.BooleanField(default=False)
    eval_model_before = models.BooleanField(default=True)
    eval_model_after = models.BooleanField(default=True)

    request_key = models.CharField(max_length=128, null=True, blank=True)
    requested_configuration = models.JSONField(default=dict, blank=True)
    effective_configuration = models.JSONField(default=dict, blank=True)
    provider_submission = models.JSONField(default=dict, blank=True)
    accepted_findings = models.JSONField(default=list, blank=True)
    name = models.CharField(max_length=255, blank=True, default="")
    use_case = models.TextField(blank=True, default="")

    # Shared by the experiments the wizard launches together; null for jobs from
    # the legacy single-job dialog.
    group_id = models.UUIDField(null=True, blank=True, db_index=True)
    model_tier = models.CharField(max_length=20, choices=Tier.choices, blank=True, default="")

    # Set once on submission; never changes.
    provider = models.CharField(max_length=20, choices=Provider.choices, default=Provider.MODAL)

    base_model = models.CharField(max_length=255)
    preparation = models.ForeignKey(
        TrainingPreparation,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name="training_jobs",
    )
    # Forwarded verbatim to the provider (learning_rate, n_epochs, batch_size,
    # lora params, …).
    hyperparameters = models.JSONField(default=dict, blank=True)

    # The capability's production model at submit time, snapshotted so the delta
    # answers "did the fine-tune beat what we run today?" rather than comparing
    # against the base model of the family being trained. Empty when the job has
    # no capability or no resolvable model; the baseline then uses base_model.
    baseline_model = models.CharField(max_length=255, blank=True, default="")

    status = models.CharField(
        max_length=20, choices=Status.choices, default=Status.QUEUED, db_index=True
    )

    remote_job_id = models.CharField(max_length=255, blank=True, default="", db_index=True)

    # Set on success.
    model_weights_location = models.CharField(max_length=1024, blank=True, default="")
    output_model_name = models.CharField(max_length=255, blank=True, default="")

    # Rewritten each poll cycle by progress_from_snapshot: {trained_steps,
    # total_steps, percent, eta_seconds, elapsed_seconds, tokens_processed,
    # phase, provider_status, latest_train_loss, latest_eval_loss,
    # metrics: {loss, learning_rate, grad_norm}, checkpoints: [...]}
    progress = models.JSONField(default=dict, blank=True)
    # Written on terminal status; on success {"epoch_losses": [...], "model":
    # "...", "metrics": {...}, "checkpoints": [...]}.
    result = models.JSONField(default=dict, blank=True)

    error_message = models.TextField(blank=True, default="")
    retry_count = models.PositiveIntegerField(default=0)
    # 0 by default: a failure surfaces immediately for manual review instead of
    # re-running silently.
    max_retries = models.PositiveIntegerField(default=0)

    celery_task_id = models.CharField(max_length=255, blank=True, default="")

    # Recorded training charge in USD.
    cost_usd = models.DecimalField(max_digits=12, decimal_places=4, null=True, blank=True)
    billed_minutes = models.PositiveIntegerField(null=True, blank=True)
    cost_synced_at = models.DateTimeField(null=True, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    started_at = models.DateTimeField(null=True, blank=True)
    completed_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ["-created_at"]
        constraints = [
            models.UniqueConstraint(
                fields=["project", "request_key"], name="finetune_project_request_unique"
            )
        ]
        indexes = [
            models.Index(fields=["project", "-created_at"]),
            models.Index(fields=["status", "-created_at"]),
            models.Index(fields=["group_id", "-created_at"]),
        ]

    def __str__(self):
        return f"FinetuningJob {self.id} [{self.provider}] ({self.base_model} → {self.status})"

    @property
    def is_terminal(self) -> bool:
        return self.status in {
            self.Status.SUCCEEDED,
            self.Status.FAILED,
            self.Status.CANCELLED,
        }


class FinetuningJobEvent(models.Model):
    """Append-only timeline entry. Epoch-level losses live in
    ``FinetuningJob.result``, not here.
    """

    class EventType(models.TextChoices):
        STATUS_CHANGE = "status_change"
        PROGRESS = "progress"
        CHECKPOINT = "checkpoint"
        LOG = "log"
        ERROR = "error"

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    job = models.ForeignKey(FinetuningJob, on_delete=models.CASCADE, related_name="events")
    event_type = models.CharField(max_length=20, choices=EventType.choices, default=EventType.LOG)
    message = models.TextField(blank=True, default="")
    data = models.JSONField(default=dict, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-created_at"]
        indexes = [models.Index(fields=["job", "-created_at"])]

    def __str__(self):
        return f"{self.job_id} [{self.event_type}] {self.message[:40]}"


class FinetuningJobEval(models.Model):
    """One selected model/time evaluation; checkpoint rows retain historical results."""

    class Kind(models.TextChoices):
        BASELINE = "baseline"
        COMPARATOR = "comparator"
        INCUMBENT_AFTER = "incumbent_after"
        MODEL_BEFORE = "model_before"
        CHECKPOINT = "checkpoint"
        FINAL = "final"

    class Status(models.TextChoices):
        PENDING = "pending"
        RUNNING = "running"
        COMPLETED = "completed"
        FAILED = "failed"
        CANCELLED = "cancelled"
        SKIPPED = "skipped"

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    job = models.ForeignKey(FinetuningJob, on_delete=models.CASCADE, related_name="job_evals")
    eval_run = models.ForeignKey(
        "overbae.EvalRun",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="finetuning_job_evals",
    )
    kind = models.CharField(max_length=20, choices=Kind.choices)
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
    # Provider checkpoint id / step when kind=checkpoint; empty for baseline.
    checkpoint_id = models.CharField(max_length=255, blank=True, default="")
    checkpoint_step = models.IntegerField(null=True, blank=True)
    # Exact model id passed to inference (base model, ckpt path, or final output).
    model_id = models.CharField(max_length=512, blank=True, default="")
    # Mean judge score across metrics (0–1-ish); null until the EvalRun finishes.
    aggregate_score = models.FloatField(null=True, blank=True)
    # aggregate_score − baseline aggregate_score (null until both exist).
    baseline_delta = models.FloatField(null=True, blank=True)
    # Set only when the eval dataset's references are labels: {classes:
    # [{label, precision, recall, f1, support}], aggregates: {accuracy, n,
    # macro/micro/weighted}, confusion_matrix: {labels, matrix}}.
    class_metrics = models.JSONField(null=True, blank=True)
    error_message = models.TextField(blank=True, default="")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["checkpoint_step", "created_at"]
        indexes = [
            models.Index(fields=["job", "kind"]),
            models.Index(fields=["job", "checkpoint_id"]),
        ]
        constraints = [
            models.UniqueConstraint(
                fields=["job", "kind"],
                condition=models.Q(kind__in=["incumbent_after", "model_before"]),
                name="uniq_ft_job_eval_optional",
            ),
            models.UniqueConstraint(
                fields=["job", "kind"],
                condition=models.Q(kind="baseline"),
                name="uniq_ft_job_eval_baseline",
            ),
            models.UniqueConstraint(
                fields=["job", "kind"],
                condition=models.Q(kind="final"),
                name="uniq_ft_job_eval_final",
            ),
            models.UniqueConstraint(
                fields=["job", "checkpoint_id"],
                condition=models.Q(kind="checkpoint") & ~models.Q(checkpoint_id=""),
                name="uniq_ft_job_eval_checkpoint",
            ),
        ]

    def __str__(self):
        return f"FinetuningJobEval {self.kind} job={self.job_id} ({self.status})"


class NativeEvaluationPlan(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    project = models.ForeignKey("overbae.Project", on_delete=models.CASCADE)
    triggered_by = models.ForeignKey(
        "overbae.User",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="native_evaluations",
    )
    name = models.CharField(max_length=255, default="Decision comparison")
    request_key = models.CharField(max_length=128, null=True, blank=True)
    job = models.OneToOneField(
        FinetuningJob,
        on_delete=models.PROTECT,
        related_name="native_evaluation",
        null=True,
        blank=True,
    )
    calibration_cell = models.ForeignKey(
        "overbae.Cell",
        on_delete=models.PROTECT,
        related_name="native_calibration_plans",
        null=True,
        blank=True,
    )
    final_cell = models.ForeignKey(
        "overbae.Cell", on_delete=models.PROTECT, related_name="native_final_plans"
    )
    config = models.JSONField(default=dict)
    state = models.CharField(max_length=32, default="waiting_for_checkpoint", db_index=True)
    calls = models.JSONField(default=dict)
    calibration = models.JSONField(default=dict)
    results = models.JSONField(default=dict)
    error = models.TextField(blank=True, default="")
    deleted_at = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["project", "request_key"], name="unique_native_evaluation_request"
            )
        ]


class DecisionProviderRequest(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    plan = models.ForeignKey(
        NativeEvaluationPlan, on_delete=models.CASCADE, related_name="requests"
    )
    participant = models.CharField(max_length=64)
    role = models.CharField(max_length=16)
    input_sha256 = models.CharField(max_length=64)
    state = models.CharField(max_length=24, default="pending", db_index=True)
    attempts = models.JSONField(default=list)
    response = models.JSONField(null=True, blank=True)
    prediction = models.JSONField(null=True, blank=True)
    diagnostics = models.JSONField(default=dict)
    usage = models.JSONField(default=dict)
    error = models.TextField(blank=True, default="")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["plan", "participant", "role", "input_sha256"],
                name="unique_decision_provider_request",
            )
        ]


class DecisionPerformanceRun(Leased):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    project = models.ForeignKey("overbae.Project", on_delete=models.CASCADE)
    evaluation = models.ForeignKey(NativeEvaluationPlan, on_delete=models.PROTECT)
    name = models.CharField(max_length=255)
    request_key = models.CharField(max_length=128)
    workload = models.JSONField()
    state = models.CharField(max_length=24, default="queued")
    results = models.JSONField(default=dict)
    error = models.TextField(default="", blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["project", "request_key"], name="unique_decision_performance_run"
            )
        ]


class DecisionPerformanceRequest(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    run = models.ForeignKey(
        DecisionPerformanceRun, on_delete=models.CASCADE, related_name="requests"
    )
    participant = models.CharField(max_length=64)
    position = models.PositiveIntegerField()
    state = models.CharField(max_length=24, default="pending")
    call_id = models.CharField(max_length=255, default="", blank=True)
    response = models.JSONField(null=True)
    usage = models.JSONField(default=dict)
    latency_ms = models.FloatField(null=True)
    error = models.TextField(default="", blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["run", "participant", "position"],
                name="unique_decision_performance_request",
            )
        ]