The platform for continuously improving AI agents.
"""Prepare a dataset version from a transform spec: compile it, retain it as a pipeline and
run that pipeline on the pinned source through the same path every other pipeline takes."""
import io
import json
from django.db import transaction
from overbae.core.errors import DatasetError
from overbae.models import Cell, DatasetPipelineRun
from overbae.services.datasets import pipeline_packages, transform_compiler, workbench
from overbae.services.datasets.transform_spec import TransformSpec
def prepare(
dataset,
user,
*,
source_cell,
spec,
request_key,
source_fingerprint=None,
mode="publish",
preview_rows=100,
actor=None,
author=None,
):
"""The pipeline run that prepares ``source_cell`` as ``spec`` states. An identical retry
returns the same run; the same key with a different spec or source conflicts. A
``source_fingerprint`` pins the bytes the caller inspected."""
# Research team contracts import dataset preparation through native services.
from overbae.services import research_team
if not isinstance(request_key, str) or not 1 <= len(request_key) <= 128:
raise DatasetError("Supply a request key of up to 128 characters.", code="request_key")
spec = TransformSpec.model_validate(spec)
source = (
Cell.objects.select_related("dataset")
.filter(pk=source_cell, dataset__project_id=dataset.project_id, state=Cell.State.OK)
.first()
)
if source is None or not source.fingerprint:
raise DatasetError("Select a readable version of this project.", code="source_conflict")
if source_fingerprint is not None and source_fingerprint != source.fingerprint:
raise DatasetError(
"The source version or fingerprint does not match.", code="source_conflict"
)
existing = (
DatasetPipelineRun.objects.filter(dataset=dataset, request_key=request_key)
.select_related("pipeline__package")
.first()
)
if existing:
try:
files = pipeline_packages.files(existing.pipeline.package)
retained = TransformSpec.model_validate(json.loads(files["plan.json"])["spec"])
except (AttributeError, KeyError, TypeError, ValueError, DatasetError):
raise DatasetError(
"This request key identifies different work.", code="conflict"
) from None
if retained != spec:
raise DatasetError(
"This request key identifies a different preparation.", code="conflict"
)
with transaction.atomic():
if actor is not None:
research_team.current_turn(actor)
return workbench.submit(
dataset,
user,
source_cell=source.pk,
source_fingerprint=source.fingerprint,
request_key=request_key,
pipeline=existing.pipeline_id,
mode=mode,
preview_rows=preview_rows,
)
package, _ = transform_compiler.build(spec, source)
retained = pipeline_packages.save(
dataset.project,
user,
io.BytesIO(package),
actor=actor,
author=author,
source_cells={str(source.pk)},
)
with transaction.atomic():
if actor is not None:
research_team.current_turn(actor)
pipeline = workbench.save_pipeline(
dataset.project,
user,
name=f"Prepare {spec.target.column}",
request_key=request_key,
package=retained.pk,
dataset=dataset,
)
return workbench.submit(
dataset,
user,
source_cell=source.pk,
source_fingerprint=source.fingerprint,
request_key=request_key,
pipeline=pipeline.pk,
mode=mode,
preview_rows=preview_rows,
)
def describe(run):
"""A preparation run for whoever asked for it: its state, the version it published and
what the platform verified about that version."""
record = workbench.run_record(run)
steps = run.result.get("steps") or []
return {
**record,
"checks": steps[-1].get("preparation")
if steps and not record["result"].get("source_access")
else None,
}