The platform for continuously improving AI agents.
import uuid
from django.conf import settings
from django.db import models
from django.db.models import Q
class Experiment(models.Model):
"""A saved flow of stages and its conversation. Each save is an immutable revision; a run
executes one revision."""
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)
objective = models.TextField(blank=True, default="")
# The agent's working notes: plan, findings, open questions. People read them.
notes = models.TextField(blank=True, default="", db_default="")
# The newest revision's number; 0 until the first save.
version = models.PositiveIntegerField(default=0)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name="+"
)
request_key = models.CharField(max_length=128, blank=True, default="")
# What the keyed request asked for: a retry that asks for something else is refused.
request_fingerprint = models.CharField(max_length=64, blank=True, default="")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
deleted_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["-updated_at"]
constraints = [
models.UniqueConstraint(
fields=["project", "request_key"],
condition=~Q(request_key=""),
name="experiment_request_unique",
)
]
class ExperimentRevision(models.Model):
experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE, related_name="revisions")
number = models.PositiveIntegerField()
# Parameters, retained outputs and stages: services/experiments/document.py.
document = models.JSONField()
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name="+"
)
request_key = models.CharField(max_length=128, blank=True, default="")
# What the save changes, in its author's words; blank for a save that gave none.
note = models.CharField(max_length=500, blank=True, default="")
# The earlier revision whose document this one restored.
restored_from = models.PositiveIntegerField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["number"]
constraints = [
models.UniqueConstraint(
fields=["experiment", "number"], name="experiment_revision_unique"
)
]
class ExperimentRun(models.Model):
"""One execution of a revision. A variants, subflow or iterate stage runs its flow as a
child run under its step."""
class State(models.TextChoices):
RUNNING = "running"
STOPPING = "stopping"
COMPLETED = "completed"
# Ended with a failed or unreachable stage.
UNMET = "unmet"
STOPPED = "stopped"
class Origin(models.TextChoices):
CONSOLE = "console"
MCP = "mcp"
ASSISTANT = "assistant"
TRIGGER = "trigger"
STAGE = "stage"
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE, related_name="runs")
revision = models.ForeignKey(ExperimentRevision, on_delete=models.PROTECT, related_name="+")
parent = models.ForeignKey(
"ExperimentStep", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
)
# Numbers the experiment's runs, children included.
number = models.PositiveIntegerField()
parameters = models.JSONField(default=dict)
state = models.CharField(max_length=16, choices=State, default=State.RUNNING)
origin = models.CharField(max_length=16, choices=Origin)
# The plan the starter reviewed: a run whose plan changed since is refused.
plan = models.CharField(max_length=64)
reuse_run = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True)
rerun_from = models.JSONField(default=list)
request_key = models.CharField(max_length=128, blank=True, default="")
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
deadline = models.DateTimeField()
# When the engine last advanced the run: a running run that stopped moving shows it.
checked_at = models.DateTimeField(null=True, blank=True)
# Why it ended, and the retained outputs it declared.
result = models.JSONField(default=dict)
created_at = models.DateTimeField(auto_now_add=True)
ended_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["-number"]
constraints = [
models.UniqueConstraint(fields=["experiment", "number"], name="experiment_run_number"),
models.UniqueConstraint(
fields=["experiment", "request_key"],
condition=~Q(request_key=""),
name="experiment_run_request_unique",
),
]
class ExperimentStep(models.Model):
"""One attempt at one stage of a run."""
class State(models.TextChoices):
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
SKIPPED = "skipped"
FAILED = "failed"
CANCELLED = "cancelled"
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
run = models.ForeignKey(ExperimentRun, on_delete=models.CASCADE, related_name="steps")
node = models.CharField(max_length=64)
attempt = models.PositiveSmallIntegerField(default=1)
state = models.CharField(max_length=16, choices=State, default=State.PENDING)
# The values bound to its input ports when it started, and those it produced.
inputs = models.JSONField(default=dict)
outputs = models.JSONField(default=dict)
# The stage's kind, settings and upstream identities: an unchanged stage of a later run
# may reuse this attempt's outputs.
identity = models.CharField(max_length=64, blank=True, default="")
# The platform records it waits on, as get_job reads them: [{"kind", "id", "status"}].
receipts = models.JSONField(default=list)
# Every tool call it made, with its arguments and result summary.
calls = models.JSONField(default=list)
error = models.TextField(blank=True, default="")
# Written when it ends: services/experiments/failures.py.
report = models.JSONField(default=dict)
reused_from = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True)
started_at = models.DateTimeField(null=True, blank=True)
ended_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["node", "attempt"]
constraints = [
models.UniqueConstraint(
fields=["run", "node", "attempt"], name="experiment_step_unique"
)
]
class ExperimentEvent(models.Model):
"""The experiment's sequenced log: revisions, runs, stage transitions and trigger fires."""
experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE, related_name="events")
run = models.ForeignKey(ExperimentRun, on_delete=models.CASCADE, null=True, blank=True)
sequence = models.PositiveIntegerField()
kind = models.CharField(max_length=32)
payload = models.JSONField(default=dict)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["sequence"]
constraints = [
models.UniqueConstraint(
fields=["experiment", "sequence"], name="experiment_event_sequence"
)
]
class ExperimentTrigger(models.Model):
"""Sends an instruction to the experiment's agent on a schedule or when traces fail."""
class Kind(models.TextChoices):
SCHEDULE = "schedule"
FAILING_TRACES = "failing_traces"
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE, related_name="triggers")
kind = models.CharField(max_length=16, choices=Kind)
instruction = models.TextField()
# Schedule: the interval. Failing traces: the window the threshold counts over.
hours = models.PositiveSmallIntegerField()
capability = models.ForeignKey(
"overbae.Capability", on_delete=models.CASCADE, null=True, blank=True
)
threshold = models.PositiveIntegerField(null=True, blank=True)
max_sends = models.PositiveSmallIntegerField()
# Unattended work stops once the credits the ledger charged to the experiment since the
# trigger was made reach this.
max_spend_credits = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True)
sends = models.PositiveSmallIntegerField(default=0)
next_at = models.DateTimeField(null=True, blank=True)
last_sent_at = models.DateTimeField(null=True, blank=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
deleted_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["created_at"]