The platform for continuously improving AI agents.
import hashlib
import json
import logging
import math
import shutil
import sqlite3
import tempfile
import time
import uuid
from datetime import datetime, timedelta
from pathlib import Path
import httpx
import modal
from celery.exceptions import SoftTimeLimitExceeded
from django.db import transaction
from django.utils import timezone
from modal_shared.decision_inference import input_digest
from modal_shared.decision_metrics import score_decision
from modal_shared.decisions import DECISION_OBJECTIVES, decision_request
from modal_shared.serving.artifacts import atomic_json, digest_file
from modal_shared.training_release import evaluation_identity
from overbae.core.errors import InputValidationError
from overbae.models import (
Dataset,
DecisionProviderRequest,
FinetuningJob,
NativeEvaluationPlan,
Project,
ResearchAssignment,
TrainingExperiment,
)
from overbae.services.compute_costs import estimate_usage
from overbae.services.datasets import heartbeat, paths, rows, use
from overbae.services.datasets.examples import prepare_native_record
from overbae.services.decision_benchmark_scoring import compare_suite, records
from overbae.services.decision_calibration import fit_decision_calibration
from overbae.services.decision_providers import (
RetryableEvaluationError,
SubmissionUnknownError,
external_step,
qualify_participants,
recover_request,
)
from overbae.services.evaluation_inputs import artifact_lock, replacing
from overbae.services.training_release import current
logger = logging.getLogger(__name__)
PURPOSES = ("development", "final")
TEMPERATURES = [0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 3.0]
def require_available(plan):
if plan.deleted_at is not None:
raise InputValidationError("This evaluation has been deleted. Create a new evaluation.")
@transaction.atomic
def request_cancel(plan, *, user):
"""Marks the plan cancelling; its next advance stops the provider calls and ends it."""
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
require_available(plan)
if plan.state in {"completed", "cancelling", "cancelled"}:
return plan
plan.state = "cancelling"
plan.config = {
**plan.config,
"cancellation": {"requested_at": timezone.now().isoformat(), "by": str(user.pk)},
}
plan.save()
# The task module imports this service; a top-level import would be circular.
from overbae.tasks.native_evaluation import advance_plan
transaction.on_commit(lambda: advance_plan.delay(str(plan.pk)))
return plan
def stop_calls(plan) -> None:
"""Cancels every unfinished provider call, best effort: a failed stop is recorded on the
call and the plan still ends, so a cancel can never hang on the provider."""
calls = {}
for stage, call in plan.calls.items():
if call.get("state") in {"completed", "failed", "cancelled"}:
calls[stage] = call
continue
stopped = {**call, "state": "cancelled"}
if call.get("id"):
try:
modal.FunctionCall.from_id(call["id"]).cancel(terminate_containers=True)
except Exception as exc: # noqa: BLE001 — recorded on the call, never blocks the cancel
logger.warning("Provider call %s did not confirm cancellation: %s", call["id"], exc)
stopped["cancellation_error"] = str(exc)[:500]
calls[stage] = stopped
plan.calls = calls
plan.state = "cancelled"
plan.save()
@transaction.atomic
def delete(plan):
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
if plan.deleted_at is not None:
return
if plan.state not in {"draft", "prepared", "paused", "completed", "failed", "cancelled"}:
raise InputValidationError(
"Pause the evaluation before deleting it. Active or unresolved calls must also be resolved."
)
if (
plan.state != "completed"
and TrainingExperiment.objects.filter(evaluation=plan)
.exclude(state__in=["completed", "failed"])
.exists()
):
raise InputValidationError("An unfinished training experiment still needs this evaluation.")
if (
any(
call.get("lease")
or call.get("state") in {"submitting", "running", "submission_unknown"}
for call in plan.calls.values()
)
or plan.requests.filter(state__in=["submitting", "running", "submission_unknown"]).exists()
):
raise InputValidationError(
"This evaluation has active or unresolved provider work. Resolve it before deleting."
)
# Reused predictions, training experiments and request keys retain their receipts.
plan.deleted_at = timezone.now()
plan.save(update_fields=["deleted_at", "updated_at"])
def runtime():
release = current()
return {
**evaluation_identity(Path(__file__).resolve().parents[2]),
"environment": release["environment"],
"training": release["training"],
}
def training_decisions(cell) -> bool:
report = (cell.intent_report or {}).get("train") or {}
return bool(report.get("ok")) and report.get("format") == "decision"
def check_suite(cell, purpose):
"""A final measurement reads a readable evaluation dataset of native decisions; a
development one also reads decision training rows, whose targets are the references."""
if purpose == "development" and training_decisions(cell):
return
if purpose == "final" and cell.dataset.intent != "eval":
raise InputValidationError("A final evaluation reads an evaluation dataset")
if not cell.fits("eval")[0]:
raise InputValidationError("Select a technically readable evaluation version")
evaluation = (cell.intent_report or {}).get("eval", {})
if evaluation.get("input_type") != "decision" or any(
k != "decision" and v for k, v in evaluation.get("input_types", {}).items()
):
raise InputValidationError(
"Evaluation suites must contain native decision inputs and separate references."
)
def suite_example(row):
"""A row's decision request and its reference, from evaluation or training rows."""
if isinstance(row.input, dict) and "decision" in row.input:
return row.input["decision"], row.expected_output
decision = row.extra.get("decision")
if not isinstance(decision, dict):
return None, row.expected_output
example = prepare_native_record({}, dict(decision), "eval")
return example["input"]["decision"], example["expected_output"]
@transaction.atomic
def create_plan(
project,
*,
name,
request_key,
final_cell,
participants,
baseline,
calibration_cell=None,
bootstrap_samples=1000,
seed=73491,
inference=None,
job=None,
triggered_by=None,
purpose="final",
pinned_runtime=None,
):
if calibration_cell is not None and calibration_cell.id == final_cell.id:
raise InputValidationError("Calibration and final suites must be separate frozen cells.")
suites = {"final": final_cell}
if calibration_cell is not None:
suites = {"calibration": calibration_cell, **suites}
if purpose not in PURPOSES:
raise InputValidationError("Choose a development or final evaluation")
for cell in suites.values():
if cell.dataset.project_id != project.pk:
raise InputValidationError("Evaluation suites must belong to this project.")
check_suite(cell, purpose)
if (
type(bootstrap_samples) is not int
or not 2 <= bootstrap_samples <= 10000
or type(seed) is not int
or not 0 <= seed <= 2**32 - 1
):
raise InputValidationError("Supply 2–10000 bootstrap samples and a nonnegative 32-bit seed")
inference = {
"context_length": 8192,
"batch_size": 64,
"padded_tokens": 32768,
"concurrency": 8,
**(inference or {}),
}
bounds = {
"context_length": (128, 131072),
"batch_size": (1, 256),
"padded_tokens": (128, 262144),
"concurrency": (1, 16),
}
if set(inference) != set(bounds) or any(
type(inference[k]) is not int or not a <= inference[k] <= b for k, (a, b) in bounds.items()
):
raise InputValidationError("Invalid native inference settings")
Project.objects.select_for_update().get(pk=project.pk)
existing = NativeEvaluationPlan.objects.filter(project=project, request_key=request_key).first()
if existing:
require_available(existing)
selected = qualify_participants(project, participants)
if baseline not in {p["key"] for p in selected}:
raise InputValidationError("Choose one of the participants as the baseline")
config = {
"purpose": purpose,
"runtime": existing.config["runtime"]
if existing
# A final qualification runs on the runtime its frozen protocol names.
else pinned_runtime or runtime()
if any(p["kind"] != "external" for p in selected)
else {"adapter": "systemone", **evaluation_identity(Path(__file__).resolve().parents[2])},
"suites": {
role: {
"cell": str(cell.pk),
"dataset": str(cell.dataset_id),
"fingerprint": cell.fingerprint,
"rows": cell.rows,
}
for role, cell in suites.items()
},
"suite_overlap": existing.config.get("suite_overlap") if existing else None,
"participants": selected,
"baseline": baseline,
"inference": inference,
"calibration_method": existing.config.get("calibration_method")
if existing
else "clef_temperature_cross_entropy"
if calibration_cell
else None,
"temperatures": existing.config.get("temperatures", []) if existing else [],
"bootstrap_samples": bootstrap_samples,
"seed": seed,
"logarithm_floor": 1e-12,
"boundaries": existing.config.get("boundaries", {}) if existing else {},
"contamination_limits": "Exact input and declared grouping matches only; paraphrase and pretraining contamination remain unknown. Findings do not change suites or labels.",
}
if existing:
if existing.config.get("authorization"):
config["authorization"] = existing.config["authorization"]
if existing.config.get("reuse"):
config["reuse"] = existing.config["reuse"]
# A plan saved before purpose was recorded is a final measurement.
if {"purpose": "final", **existing.config} != config or existing.name != name:
raise InputValidationError(
"This request key already identifies a different frozen comparison"
)
return existing
for cell in sorted(suites.values(), key=lambda cell: str(cell.dataset_id)):
Dataset.objects.select_for_update().get(pk=cell.dataset_id)
cell.refresh_from_db()
if (
cell.fingerprint
!= config["suites"][
next(role for role, candidate in suites.items() if candidate.pk == cell.pk)
]["fingerprint"]
):
raise InputValidationError("The selected source changed during planning")
use.freeze(cell)
return NativeEvaluationPlan.objects.create(
project=project,
name=name,
request_key=request_key,
job=job,
triggered_by=triggered_by,
calibration_cell=calibration_cell,
final_cell=final_cell,
config=config,
state="draft",
)
def schedule(job, *, calibration_cell, final_cell):
if (job.hyperparameters or {}).get("objective") not in DECISION_OBJECTIVES:
raise InputValidationError("Native evaluation requires a decision training job.")
plan = create_plan(
job.project,
name=job.name or "Decision comparison",
request_key=f"training:{job.pk}",
job=job,
triggered_by=job.triggered_by,
calibration_cell=calibration_cell,
final_cell=final_cell,
participants=[
{"key": "base", "name": "Unchanged base", "kind": "foundation", "job": str(job.pk)},
{"key": "candidate", "name": "Trained model", "kind": "trained", "job": str(job.pk)},
],
baseline="base",
)
return launch(plan)
@transaction.atomic
def launch(plan, *, user=None):
# Tasks import this service; dispatch only after the draft transition commits.
from overbae.tasks.native_evaluation import advance_plan
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
require_available(plan)
if plan.state in {"draft", "prepared"}:
actor = user or plan.triggered_by
plan.config = {
**plan.config,
"authorization": {
"user": str(actor.pk) if actor else None,
"authorized_at": timezone.now().isoformat(),
"scope": "saved_participants_and_suites",
"configuration_sha256": hashlib.sha256(
json.dumps(
{k: v for k, v in plan.config.items() if k != "authorization"},
sort_keys=True,
).encode()
).hexdigest(),
},
}
plan.state = (
"waiting_for_checkpoint" if any(p.get("job") for p in participants(plan)) else "queued"
)
plan.save()
transaction.on_commit(lambda: advance_plan.delay(str(plan.pk)), robust=True)
return plan
def participants(plan):
return plan.config["participants"]
def preparation_identity(participant):
"""Participants share prepared inputs when they run one model with one prompt."""
return participant.get("job") or participant["key"], participant.get("instructions", "")
def preparation_groups(plan):
groups = {}
for participant in participants(plan):
if participant["kind"] == "external" or participant["key"] in plan.config.get("reuse", {}):
continue
identity = preparation_identity(participant)
if identity not in groups:
key = "prepare" if not groups else "prepare_" + participant["key"]
groups[identity] = (key, participant)
return groups
def provider_stage(plan, stage):
role, key = stage.split("_", 1)
groups = preparation_groups(plan)
for action, participant in groups.values():
if key == action:
return role, participant, True, action
participant = next(p for p in participants(plan) if p["key"] == key)
group = groups.get(preparation_identity(participant))
return role, participant, False, group[0] if group else None
def directory(plan, role):
return paths.media_root() / "native-evaluations" / str(plan.pk) / role
def seal_suite(plan, role):
with artifact_lock(directory(plan, role)):
return write_suite(plan, role)
def write_suite(plan, role):
cell = getattr(plan, role + "_cell")
rows.verify(cell)
if cell.fingerprint != plan.config["suites"][role]["fingerprint"]:
raise InputValidationError("Frozen native suite changed.")
target = directory(plan, role)
if target.exists():
manifest = json.loads((target / "manifest.json").read_text())
if any(
digest_file(target / name) != checksum for name, checksum in manifest["files"].items()
):
raise InputValidationError("Sealed native evaluation files changed.")
return target / "inputs.jsonl"
target.parent.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(dir=target.parent) as temporary:
staging = Path(temporary) / "suite"
staging.mkdir()
count = 0
with (
(staging / "inputs.jsonl").open("w") as inputs,
(staging / "references.jsonl").open("w") as references,
(staging / "failures.jsonl").open("w") as failures,
):
for row in rows.iter_rows(cell):
request, reference = suite_example(row)
benchmark = str(
row.extra.get("benchmark") or row.extra.get("source") or cell.dataset.name
)
try:
request = decision_request(request)
n = len(request["options"])
score_decision(
request,
reference,
{"probabilities": [1 / n] * n, "log_probabilities": [-math.log(n)] * n},
)
identity = {"key": str(row.index), "input_sha256": input_digest(request)}
except (ValueError, TypeError, KeyError, ZeroDivisionError) as exc:
failures.write(
json.dumps(
{
"benchmark": benchmark,
"decisions": 1,
"source_row": row.index,
"input": request,
"reference": reference,
"error": str(exc),
}
)
+ "\n"
)
continue
inputs.write(json.dumps({**identity, "decision": request}) + "\n")
references.write(
json.dumps(
{
**identity,
"benchmark": benchmark,
"group": str(
row.extra.get("group")
or row.extra.get("case_id")
or identity["input_sha256"]
),
"kind": request["kind"],
"query": str(
row.extra.get("query") or request.get("question") or "decision"
),
"reference": reference,
"metadata": row.extra.get("metadata") or {},
}
)
+ "\n"
)
count += 1
manifest = {
"decisions": count,
"source_rows": cell.rows,
"cell": str(cell.id),
"fingerprint": cell.fingerprint,
"files": {
name: digest_file(staging / name)
for name in ("inputs.jsonl", "references.jsonl", "failures.jsonl")
},
}
atomic_json(staging / "manifest.json", manifest)
staging.rename(target)
return target / "inputs.jsonl"
def stage_sequence(plan):
stages = ["verify_inputs"]
for role in ("calibration", "final"):
if role not in plan.config["suites"]:
continue
stages.extend(f"{role}_{key}" for key, _ in preparation_groups(plan).values())
stages.extend(f"{role}_{p['key']}" for p in participants(plan))
if role == "calibration":
stages.append("fit_calibration")
return [*stages, "score"]
def calibrated(prediction, temperature):
if isinstance(temperature, dict):
temperature = temperature.get("temperature_by_kind", {}).get(
prediction["kind"], temperature["temperature"]
)
logits = [value / temperature for value in prediction["log_probabilities"]]
maximum = max(logits)
normalizer = maximum + math.log(math.fsum(math.exp(value - maximum) for value in logits))
logp = [value - normalizer for value in logits]
return {
**prediction,
"probabilities": [math.exp(value) for value in logp],
"log_probabilities": logp,
}
def comparison_for_calibration(plan, arm):
source = directory(plan, "calibration")
destination = source / "coverage" / arm
if (destination / "summary.json").exists():
result = json.loads((destination / "summary.json").read_text())
for label, key in (("candidate", arm), ("baseline", plan.config["baseline"])):
if result[label]["predictions_sha256"] != digest_file(
source / f"{key}.jsonl"
) or result[label]["suite_manifest_sha256"] != digest_file(source / "manifest.json"):
raise InputValidationError("Calibration inputs or predictions changed")
return result
baseline = plan.config["baseline"]
return compare_suite(
source,
source / f"{baseline}.jsonl",
source / f"{arm}.jsonl",
destination,
bootstrap_samples=2,
seed=plan.config["seed"],
)
def fit_calibration(plan):
plan.refresh_from_db()
if plan.calibration:
return plan.calibration
source = directory(plan, "calibration")
unavailable = {}
for participant in participants(plan):
arm = participant["key"]
comparison = comparison_for_calibration(plan, arm)
if any(
card["missing_predictions"] or card["invalid_predictions"]
for card in comparison["candidate"]["benchmarks"].values()
):
unavailable[arm] = "Calibration coverage is incomplete or invalid. Raw scores only."
result = {
"fitted_on": "calibration",
"method": plan.config["calibration_method"],
"grid": plan.config["temperatures"],
"source_manifest": digest_file(source / "manifest.json"),
"frozen_at": timezone.now().isoformat(),
"arms": {},
"unavailable": unavailable,
}
with tempfile.TemporaryDirectory() as temporary:
db = sqlite3.connect(Path(temporary) / "references.sqlite")
db.execute("CREATE TABLE refs(key TEXT PRIMARY KEY, hash TEXT, q TEXT)")
for ref in records(source / "references.jsonl"):
if "probabilities" in ref["reference"]:
db.execute(
"INSERT INTO refs VALUES(?,?,?)",
(
ref["key"],
ref["input_sha256"],
json.dumps(ref["reference"]["probabilities"]),
),
)
for arm in (p["key"] for p in participants(plan)):
if arm in unavailable:
continue
totals = [0.0] * len(result["grid"])
count = 0
db.execute("DROP TABLE IF EXISTS calibration_samples")
db.execute("CREATE TABLE calibration_samples(kind TEXT, logits TEXT, target TEXT)")
for prediction in records(source / f"{arm}.jsonl"):
found = db.execute(
"SELECT q FROM refs WHERE key=? AND hash=?",
(prediction["key"], prediction["input_sha256"]),
).fetchone()
if not found:
continue
q = json.loads(found[0])
db.execute(
"INSERT INTO calibration_samples VALUES(?,?,?)",
(
prediction["kind"],
json.dumps(prediction["log_probabilities"]),
found[0],
),
)
for i, temperature in enumerate(result["grid"]):
adjusted = calibrated(prediction, temperature)
totals[i] -= math.fsum(
a * b for a, b in zip(q, adjusted["log_probabilities"], strict=True)
)
count += 1
if not count:
unavailable[arm] = (
"Calibration has no valid probability-labelled decisions. Raw scores only."
)
continue
if plan.config["calibration_method"] == "clef_temperature_cross_entropy":
def samples():
for kind, logits, target in db.execute(
"SELECT kind, logits, target FROM calibration_samples"
):
yield {
"kind": kind,
"logits": json.loads(logits),
"target": json.loads(target),
}
result["arms"][arm] = fit_decision_calibration(samples)
else:
best = min(
range(len(totals)), key=lambda i: (totals[i], abs(result["grid"][i] - 1))
)
result["arms"][arm] = {
"temperature": result["grid"][best],
"decisions": count,
"cross_entropy": totals[best] / count,
}
result["arms"][arm]["prediction_sha256"] = digest_file(source / f"{arm}.jsonl")
db.close()
result["decisions"] = min((arm["decisions"] for arm in result["arms"].values()), default=0)
NativeEvaluationPlan.objects.filter(pk=plan.pk, calibration={}).update(calibration=result)
plan.refresh_from_db()
return plan.calibration
def submit(plan, stage):
require_execution_authority(plan)
role, participant, preparing, group = provider_stage(plan, stage)
selected = plan.config["runtime"]
evaluation_id = f"{plan.id}-{role}" + ("" if group == "prepare" else "-" + group)
model = None
if participant.get("job"):
model = FinetuningJob.objects.get(pk=participant["job"], project_id=plan.project_id)
model_run = model.remote_job_id.split(":", 1)[0]
else:
model_run = None
if preparing:
source = seal_suite(plan, role)
volume = modal.Volume.from_name("overmind-sft", environment_name=selected["environment"])
with volume.batch_upload() as upload:
upload.put_file(source, f"/decision-evaluations/{evaluation_id}/inputs.jsonl")
function = modal.Function.from_name(
selected["app"], "prepare", environment_name=selected["environment"]
)
require_execution_authority(plan)
return function.spawn(
evaluation_id,
model_run,
digest_file(source),
foundation=None if model else participant,
inference={
**plan.config["inference"],
"instructions": participant.get("instructions", ""),
},
).object_id
function = modal.Function.from_name(
selected["app"], "predict", environment_name=selected["environment"]
)
require_execution_authority(plan)
return function.spawn(
evaluation_id,
model_run,
base_only=participant["kind"] == "foundation",
inference=plan.config["inference"],
).object_id
def collect(plan, stage, receipt):
role, participant, preparing, group = provider_stage(plan, stage)
if preparing and receipt.get("instructions", "") != participant.get("instructions", ""):
raise InputValidationError("Prepared instructions differ from the frozen participant")
native_label = "base" if participant["kind"] == "foundation" else "candidate"
filename = "failures.jsonl" if preparing else f"{native_label}.jsonl"
checksum = receipt["failures_sha256" if preparing else "predictions_sha256"]
evaluation_id = f"{plan.id}-{role}" + ("" if group == "prepare" else "-" + group)
target = directory(plan, role) / (
f"{group}-failures.jsonl" if preparing else f"{participant['key']}-provider.jsonl"
)
volume = modal.Volume.from_name(
"overmind-sft", environment_name=plan.config["runtime"]["environment"]
)
with replacing(target) as partial:
with partial.open("wb") as output:
for chunk in volume.read_file(f"/decision-evaluations/{evaluation_id}/{filename}"):
output.write(chunk)
if digest_file(partial) != checksum:
raise InputValidationError("Native prediction artifact checksum differs.")
if preparing:
groups = preparation_groups(plan)
reused = plan.config.get("reuse", {})
for arm in participants(plan):
if (
arm["kind"] != "external"
and arm["key"] not in reused
and groups[preparation_identity(arm)][0] == group
):
with replacing(directory(plan, role) / f"{arm['key']}.failures.jsonl") as copy:
shutil.copyfile(target, copy)
else:
expected_runtime = plan.config["runtime"].get("training")
if expected_runtime and receipt.get("runtime_fingerprint") != expected_runtime:
raise InputValidationError(
"Prediction runtime differs from the pinned evaluation release"
)
prepared = plan.calls[f"{role}_{group}"]["receipt"]
if receipt.get("artifact_identity") != prepared.get("artifact_identity") or receipt.get(
"input_sha256"
) != prepared.get("input_sha256"):
raise InputValidationError("Prepared and predicted native identities differ")
with (
replacing(directory(plan, role) / f"{participant['key']}.jsonl") as written,
written.open("w") as output,
):
for prediction in records(target):
output.write(
json.dumps(
{
**prediction,
"log_probabilities": [
math.log(max(p, plan.config["logarithm_floor"]))
for p in prediction["probabilities"]
]
if plan.config["logarithm_floor"] is not None
else prediction["log_probabilities"],
}
)
+ "\n"
)
def comparison(plan, role, arm, calibrated_output):
source = directory(plan, role)
mode = "calibrated" if calibrated_output else "raw"
baseline = plan.config["baseline"]
destination = source / "comparisons" / arm / mode
prediction_paths = {}
for key in {baseline, arm}:
path = source / f"{key}.jsonl"
if calibrated_output:
adjusted = source / f"{key}-calibrated.jsonl"
with replacing(adjusted) as written, written.open("w") as output:
for prediction in records(path):
output.write(
json.dumps(calibrated(prediction, plan.calibration["arms"][key])) + "\n"
)
failures = path.with_suffix(".failures.jsonl")
if failures.exists():
with replacing(adjusted.with_suffix(".failures.jsonl")) as copy:
shutil.copyfile(failures, copy)
path = adjusted
prediction_paths[key] = path
if (destination / "summary.json").exists():
result = json.loads((destination / "summary.json").read_text())
for label, key in (("baseline", baseline), ("candidate", arm)):
if result[label]["predictions_sha256"] != digest_file(prediction_paths[key]) or result[
label
]["suite_manifest_sha256"] != digest_file(source / "manifest.json"):
raise InputValidationError("Saved scores no longer match their frozen inputs")
return result
return compare_suite(
source,
prediction_paths[baseline],
prediction_paths[arm],
destination,
bootstrap_samples=plan.config["bootstrap_samples"],
seed=plan.config["seed"],
)
def cost_record(plan):
result = {}
for participant in participants(plan):
job = (
FinetuningJob.objects.filter(
pk=participant.get("job"), project_id=plan.project_id
).first()
if participant.get("job")
else None
)
usages = [
attempt.get("usage", {})
for row in plan.requests.filter(participant=participant["key"])
for attempt in (row.attempts or [{"usage": row.usage}])
]
compute = []
for stage, call in plan.calls.items():
if stage in {"verify_inputs", "fit_calibration", "score"}:
continue
if provider_stage(plan, stage)[1]["key"] == participant["key"]:
compute.append((call.get("receipt") or {}).get("compute_usage", {}))
result[participant["key"]] = {
"recorded_training_usd": float(job.cost_usd)
if job and job.cost_usd is not None
else None,
"training_coverage": "training_gpu"
if job and job.provider == "modal"
else "provider_training"
if job
else "not_applicable",
"recorded_evaluation_api_usd": sum(usage.get("response_cost") or 0 for usage in usages),
"unknown_api_attempts": sum(usage.get("response_cost") is None for usage in usages),
"native_compute": estimate_usage(compute)
if participant["kind"] != "external"
else None,
"unreported_components": [
"storage",
"network",
"local_orchestration",
"provider_billing_adjustments",
],
"all_in_invoice_usd": None,
}
return result
def _report_metrics(value):
if isinstance(value, list):
return [_report_metrics(item) for item in value]
if not isinstance(value, dict):
return value
result = {key: _report_metrics(item) for key, item in value.items() if key != "slices"}
if "slices" in value:
result.update(slice_count=len(value["slices"]), slices_in_report=True)
return result
def score(plan):
result = {
"participants": participants(plan),
"baseline": plan.config["baseline"],
"comparisons": {},
"calibration": None,
"calls": {key: value for key, value in plan.calls.items() if key != "score"},
"logarithm_floor": plan.config["logarithm_floor"],
"limitations": plan.config["contamination_limits"],
"costs": cost_record(plan),
}
def modes(arm):
fitted = plan.calibration.get("arms", {})
return [False, True] if arm in fitted and plan.config["baseline"] in fitted else [False]
for participant in participants(plan):
arm = participant["key"]
result["comparisons"][arm] = {
("calibrated" if mode else "raw"): comparison(plan, "final", arm, mode)
for mode in modes(arm)
}
if plan.calibration_cell_id:
result["calibration"] = {
"in_sample": True,
"fit": plan.calibration,
"comparisons": {
p["key"]: {
("calibrated" if mode else "raw"): comparison(
plan, "calibration", p["key"], mode
)
for mode in modes(p["key"])
}
for p in participants(plan)
},
}
if "candidate" in result["comparisons"]:
result.update(result["comparisons"]["candidate"])
coverage = {}
issues = [
{
"stage": stage,
"reason": call.get("error") or "Provider collection was incomplete.",
"details": (call.get("receipt") or {}).get("issues", []),
}
for stage, call in plan.calls.items()
if call.get("error") or (call.get("receipt") or {}).get("issues")
]
for arm, reason in plan.calibration.get("unavailable", {}).items():
issues.append({"stage": "fit_calibration", "participant": arm, "reason": reason})
for role in plan.config["suites"]:
comparisons = (
result["comparisons"] if role == "final" else result["calibration"]["comparisons"]
)
coverage[role] = {
arm: {
field: sum(card[field] for card in modes["raw"]["candidate"]["benchmarks"].values())
for field in (
"expected",
"scored",
"missing_predictions",
"invalid_predictions",
"incompatible_inputs",
)
}
for arm, modes in comparisons.items()
}
incomplete = any(
c["scored"] < c["expected"] for arms in coverage.values() for c in arms.values()
)
result["completion"] = {
"status": "completed_with_issues" if incomplete or issues else "complete",
"coverage": coverage,
"issues": issues,
"scoring_basis": "Available valid predictions only; paired comparisons use shared scored inputs. Missing outputs are not zero scores. Partial coverage may bias results.",
}
directory(plan, "report").mkdir(parents=True, exist_ok=True)
report_path = directory(plan, "report") / "results.json"
atomic_json(report_path, result)
report_lines = [
f"# {plan.name}",
"",
f"Baseline: {plan.config['baseline']}",
"Completed with issues"
if result["completion"]["status"] == "completed_with_issues"
else "Completed",
result["completion"]["scoring_basis"],
"",
plan.config["contamination_limits"],
"",
"Costs are recorded provider usage, not an all-in invoice.",
"",
]
for role, arms in coverage.items():
for arm, counts in arms.items():
report_lines.append(
f"{role} · {arm}: {counts['scored']} / {counts['expected']} scored; {counts['missing_predictions']} missing, {counts['invalid_predictions']} invalid, {counts['incompatible_inputs']} incompatible."
)
for issue in issues:
report_lines.append(
f"Issue · {issue['stage']}: {issue['reason']} {json.dumps(issue.get('details', []))}"
)
for participant, modes in result["comparisons"].items():
for mode, summary in modes.items():
report_lines += [
f"## {participant} · {mode}",
"",
"| Benchmark | Metric | Baseline | Participant | Difference | 95% paired interval |",
"| --- | --- | ---: | ---: | ---: | --- |",
]
for benchmark, card in summary["benchmarks"].items():
for metric, values in card["metrics"].items():
report_lines.append(
f"| {benchmark} | {metric} | {summary['baseline']['benchmarks'][benchmark]['metrics'][metric]['mean']} | {summary['candidate']['benchmarks'][benchmark]['metrics'][metric]['mean']} | {values.get('candidate_minus_baseline')} | {values['interval_95']} |"
)
report_lines += [
"",
"Full coverage, metric denominators, failures and diagnostic slices are retained in results.json.",
"",
]
if plan.calibration_cell_id:
report_lines += [
"Calibration results in results.json are in-sample; fitted temperatures were frozen before final scoring.",
"",
]
with replacing(directory(plan, "report") / "results.md") as written:
written.write_text("\n".join(report_lines))
summary = _report_metrics(result)
summary.pop("calls", None)
summary["report"] = {
"sha256": digest_file(report_path),
"bytes": report_path.stat().st_size,
"format": "json",
"contents": "Complete metrics, diagnostic slices, calibration and provider receipts",
}
return summary
@transaction.atomic
def resume(plan, *, stage=None, call_id=None, user=None):
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
require_available(plan)
if user is not None:
plan.config = {**plan.config, "resumed_by": str(user.pk)}
if plan.state == "paused" and stage is None:
plan.state, plan.error = "running", ""
plan.save()
return plan
if (
plan.state == "paused"
and stage not in {"verify_inputs", "fit_calibration", "score"}
and not (
stage in stage_sequence(plan) and provider_stage(plan, stage)[1]["kind"] == "external"
)
and (not call_id or call_id != plan.calls.get(stage, {}).get("id"))
):
raise InputValidationError("Supply the recorded provider call ID to recover collection")
if plan.state not in {"paused", "failed", "submission_unknown"}:
raise InputValidationError("Only a stopped evaluation can be resumed")
pending = next(
(s for s in stage_sequence(plan) if plan.calls.get(s, {}).get("state") != "completed"), None
)
if pending is None or (stage is not None and stage != pending):
raise InputValidationError("Select the unfinished evaluation stage")
call = dict(plan.calls.get(pending, {}))
local = pending in {"verify_inputs", "fit_calibration", "score"}
external = not local and provider_stage(plan, pending)[1]["kind"] == "external"
if call_id:
if (
local
or external
or not call_id.startswith("fc-")
or (call.get("id") and call["id"] != call_id)
):
raise InputValidationError("Supply the existing native provider call ID")
call.update(id=call_id, state="running")
elif not local and not external and not call.get("id"):
raise InputValidationError(
"An unresolved native submission requires its existing provider call ID"
)
if external:
role, participant, _, _ = provider_stage(plan, pending)
requests = DecisionProviderRequest.objects.filter(
plan=plan, participant=participant["key"], role=role
)
for request in requests.select_for_update().filter(
state__in=["submitting", "submission_unknown"]
):
recover_request(request)
requests.filter(state="failed", response__isnull=False).update(state="received", error="")
call.pop("observe_started_at", None)
call.pop("lease", None)
call.pop("lease_until", None)
if local or external:
plan.calls = {k: v for k, v in plan.calls.items() if k != pending}
else:
call["state"] = "running"
plan.calls = {**plan.calls, pending: call}
plan.state, plan.error = pending, ""
plan.save()
return plan
@transaction.atomic
def prepare(plan):
# The task imports this service and runs file checks outside request transactions.
from overbae.tasks.native_evaluation import advance_plan
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
require_available(plan)
if plan.state == "draft":
plan.state = "preparing"
plan.save()
transaction.on_commit(lambda: advance_plan.delay(str(plan.pk)), robust=True)
return plan
def verify_inputs(plan):
for role in plan.config["suites"]:
seal_suite(plan, role)
if plan.calibration_cell_id and not any(
isinstance(row.expected_output, dict) and "probabilities" in row.expected_output
for row in rows.iter_rows(plan.calibration_cell)
):
raise InputValidationError(
"Temperature calibration requires probability-labelled decisions"
)
evidence = {
"suite_overlap": rows.contamination(plan.calibration_cell, plan.final_cell)
if plan.calibration_cell_id
else None,
"boundaries": {},
}
for participant in participants(plan):
if participant.get("job"):
job = FinetuningJob.objects.select_related("cell__dataset").get(
pk=participant["job"], project_id=plan.project_id
)
evidence["boundaries"][participant["key"]] = {
role: rows.contamination(job.cell, getattr(plan, role + "_cell"))
if job.cell_id
else {"status": "unknown"}
for role in plan.config["suites"]
}
with transaction.atomic():
locked = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
locked.config = {**locked.config, **evidence}
locked.save()
return evidence
@transaction.atomic
def reuse_predictions(plan, *, source, participant, source_participant):
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
require_available(plan)
if plan.state != "draft" or source.project_id != plan.project_id or source.pk == plan.pk:
raise InputValidationError(
"Reuse requires a draft and a different comparison in the same project"
)
target_arm = next((p for p in participants(plan) if p["key"] == participant), None)
source_arm = next((p for p in participants(source) if p["key"] == source_participant), None)
if target_arm is None or source_arm is None:
raise InputValidationError("Select saved participants")
def identity(participant):
return {k: v for k, v in participant.items() if k not in {"key", "name"}}
if identity(target_arm) != identity(source_arm) or any(
plan.config[key] != source.config[key]
for key in ("suites", "runtime", "inference", "logarithm_floor")
):
raise InputValidationError(
"Prediction reuse requires identical sources, model identities, runtime and inference conditions"
)
stages = {}
for role in plan.config["suites"]:
stage = source.calls.get(f"{role}_{source_participant}", {})
if stage.get("state") != "completed":
raise InputValidationError(
"Reuse requires completed predictions for each selected suite"
)
path = directory(source, role) / f"{source_participant}.jsonl"
failure = path.with_suffix(".failures.jsonl")
stages[role] = {
"predictions_sha256": digest_file(path),
"failures_sha256": digest_file(failure) if failure.exists() else None,
"receipt": stage["receipt"],
}
lineage = {
"evaluation": str(source.pk),
"participant": source_participant,
"stages": stages,
"independent_repetition": False,
}
plan.config = {**plan.config, "reuse": {**plan.config.get("reuse", {}), participant: lineage}}
plan.save()
return plan
def collect_reused(plan, role, participant):
lineage = plan.config["reuse"][participant["key"]]
source = NativeEvaluationPlan.objects.get(pk=lineage["evaluation"], project_id=plan.project_id)
pinned = lineage["stages"][role]
seal_suite(plan, role)
seal_suite(source, role)
original = directory(source, role) / f"{lineage['participant']}.jsonl"
target = directory(plan, role) / f"{participant['key']}.jsonl"
for previous, destination, checksum in [
(original, target, pinned["predictions_sha256"]),
(
original.with_suffix(".failures.jsonl"),
target.with_suffix(".failures.jsonl"),
pinned["failures_sha256"],
),
]:
if checksum is not None:
if digest_file(previous) != checksum:
raise InputValidationError("Saved prediction evidence changed")
with replacing(destination) as copy:
shutil.copyfile(previous, copy)
return {
**pinned["receipt"],
"reused_from": str(source.pk),
"reused_participant": lineage["participant"],
"incremental_provider_calls": 0,
}
def ready_stages(plan):
if plan.deleted_at is not None or plan.state in {
"draft",
"prepared",
"paused",
"completed",
"cancelling",
"cancelled",
}:
return []
if plan.calls.get("verify_inputs", {}).get("state") != "completed":
retry_at = plan.calls.get("verify_inputs", {}).get("retry_at")
if retry_at and timezone.now() < datetime.fromisoformat(retry_at):
return []
return (
[]
if plan.calls.get("verify_inputs", {}).get("state") in {"failed", "submission_unknown"}
else ["verify_inputs"]
)
if plan.state == "preparing":
return []
complete = {key for key, call in plan.calls.items() if call.get("state") == "completed"}
ready = []
jobs = {
str(job.pk): job.status
for job in FinetuningJob.objects.filter(
pk__in=[p["job"] for p in participants(plan) if p.get("job")],
project_id=plan.project_id,
)
}
sequence = stage_sequence(plan)
for stage in sequence:
call = plan.calls.get(stage, {})
if call.get("state") in {"completed", "failed", "submission_unknown"}:
continue
if call.get("retry_at") and timezone.now() < datetime.fromisoformat(call["retry_at"]):
continue
if stage == "verify_inputs":
continue
if stage == "fit_calibration":
dependencies = [s for s in sequence if s.startswith("calibration_")]
elif stage == "score":
dependencies = sequence[:-1]
else:
role, participant, preparation, group = provider_stage(plan, stage)
if participant.get("job") and jobs.get(participant["job"]) != "succeeded":
continue
dependencies = [] if preparation or not group else [f"{role}_{group}"]
if role == "final" and plan.calibration_cell_id:
dependencies.append("fit_calibration")
if all(dependency in complete for dependency in dependencies):
ready.append(stage)
return ready
@transaction.atomic
def pause(plan):
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
require_available(plan)
if plan.state not in {"draft", "completed", "cancelling", "cancelled"}:
plan.state = "paused"
plan.save()
return plan
def settle(plan):
if plan.state in {"paused", "cancelling", "cancelled"}:
return
if plan.state == "preparing" and plan.calls.get("verify_inputs", {}).get("state") in {
"running",
"submitting",
"retrying",
}:
return
failures = [
call
for call in plan.calls.values()
if call.get("state") in {"failed", "submission_unknown"}
]
if (
failures
and not ready_stages(plan)
and not any(call.get("state") == "retrying" for call in plan.calls.values())
):
plan.state = (
"submission_unknown"
if any(c["state"] == "submission_unknown" for c in failures)
else "failed"
)
plan.error = "An evaluation stage stopped. Independent eligible work is retained; inspect stage receipts before resuming."
elif plan.state != "completed":
plan.state, plan.error = "running", ""
def record_failure(call, exc, *, can_retry):
transient = isinstance(
exc,
(
RetryableEvaluationError,
ConnectionError,
TimeoutError,
httpx.TransportError,
SoftTimeLimitExceeded,
modal.exception.ConnectionError,
modal.exception.ServiceError,
modal.exception.InternalError,
modal.exception.ResourceExhaustedError,
),
)
if transient and can_retry:
failures = call.get("consecutive_failures", 0) + 1
call["consecutive_failures"] = failures
call["recovery"] = [
*call.get("recovery", []),
{
"at": timezone.now().isoformat(),
"reason": type(exc).__name__,
"attempt": failures,
"outcome": "retry_scheduled" if failures < 3 else "exhausted",
},
]
if failures < 3:
call.update(
state="retrying",
retry_at=(timezone.now() + timedelta(seconds=15 * 2 ** (failures - 1))).isoformat(),
error="Transient interruption; automatic recovery is scheduled.",
)
return
call.pop("retry_at", None)
call.update(
state="submission_unknown"
if isinstance(exc, SubmissionUnknownError) or not can_retry
else "failed",
error="Automatic recovery exhausted."
if transient and can_retry
else "The stage could not be verified. Inspect retained provider receipts and diagnostics.",
)
def advance(plan_id, *, stage=None):
# Monitoring reads this service's frozen suite and calibration helpers.
from overbae.services.native_evaluation_monitor import (
collect_progress,
refresh_safely,
retain_partial,
)
with transaction.atomic():
plan = (
NativeEvaluationPlan.objects.select_for_update(of=("self",))
.select_related(
"job", "project", "triggered_by", "calibration_cell__dataset", "final_cell__dataset"
)
.get(pk=plan_id)
)
if plan.deleted_at is not None or plan.state == "cancelled":
return
if plan.state == "cancelling":
stop_calls(plan)
return
try:
require_execution_authority(plan)
except NativeSubmissionRejectedError as exc:
plan.state = "cancelling"
plan.config = {
**plan.config,
"cancellation": {"requested_at": timezone.now().isoformat(), "reason": exc.detail},
}
plan.save(update_fields=["state", "config", "updated_at"])
stop_calls(plan)
return
failed_jobs = {
str(job.pk): job.status
for job in FinetuningJob.objects.filter(
pk__in=[p["job"] for p in participants(plan) if p.get("job")],
project_id=plan.project_id,
status__in=["failed", "cancelled"],
)
}
if plan.state not in {"draft", "prepared", "paused", "completed"}:
for pending in stage_sequence(plan):
if (
pending in {"verify_inputs", "fit_calibration", "score"}
or plan.calls.get(pending, {}).get("state") == "completed"
):
continue
_, arm, _, _ = provider_stage(plan, pending)
if arm.get("job") in failed_jobs:
plan.calls = {
**plan.calls,
pending: {
"state": "failed",
"error": "Required training checkpoint is unavailable",
"dependency": arm["job"],
"dependency_state": failed_jobs[arm["job"]],
},
}
if failed_jobs:
settle(plan)
plan.save()
available = ready_stages(plan)
stage = stage or next(iter(available), None)
if stage not in available:
return
local = stage in {"verify_inputs", "fit_calibration", "score"}
reused = not local and provider_stage(plan, stage)[1]["key"] in plan.config.get("reuse", {})
external = not local and provider_stage(plan, stage)[1]["kind"] == "external"
preparation_unavailable = False
if not local:
role, _, preparing, group = provider_stage(plan, stage)
preparation_unavailable = not preparing and bool(
(plan.calls.get(f"{role}_{group}", {}).get("receipt") or {}).get("partial")
)
call = dict(plan.calls.get(stage, {}))
call.pop("retry_at", None)
if call.get("lease") or call.get("state") == "submitting":
if stage_held(call):
return
if not local and not external and not reused and not call.get("id"):
plan.calls = {**plan.calls, stage: {**call, "state": "submission_unknown"}}
settle(plan)
plan.save()
return
starting = not call.get("id")
lease = uuid.uuid4().hex
queued_at = call.get("enqueued_at")
queue_wait = (
max(0, (timezone.now() - datetime.fromisoformat(queued_at)).total_seconds())
if queued_at
else None
)
call = {
**call,
"state": "submitting" if starting else "running",
"lease": lease,
"lease_until": heartbeat.lease_end().isoformat(),
"last_queue_wait_seconds": queue_wait,
}
plan.calls = {**plan.calls, stage: call}
preparing_only = plan.state == "preparing"
if not preparing_only:
plan.state = "running"
plan.save()
beat = heartbeat.start(
lambda: renew_stage(plan.pk, stage, lease), owner=f"native evaluation {plan.pk} {stage}"
)
try:
observed_at = time.perf_counter()
receipt = None
try:
if stage == "verify_inputs":
receipt = verify_inputs(plan)
elif stage == "fit_calibration":
receipt = fit_calibration(plan)
elif stage == "score":
receipt = score(plan)
elif reused:
role, participant, _, _ = provider_stage(plan, stage)
receipt = collect_reused(plan, role, participant)
elif external:
role, participant, _, _ = provider_stage(plan, stage)
receipt = external_step(plan, role, participant)
elif preparation_unavailable:
receipt = retain_partial(plan, stage)
receipt["issues"] = [
{"error": "Required input preparation could not be collected."}
]
elif starting:
call["id"] = submit(plan, stage)
receipt = None
else:
receipt = modal.FunctionCall.from_id(call["id"]).get(timeout=0)
collect(plan, stage, receipt)
call.pop("progress_error", None)
call.update(
state="completed"
if receipt is not None and (not external or reused or receipt["completed"])
else "running",
receipt=receipt["report"] if stage == "score" and receipt is not None else receipt,
)
call.pop("error", None)
call.pop("consecutive_failures", None)
except TimeoutError as exc:
if local or external or reused or starting or receipt is not None:
record_failure(
call, exc, can_retry=local or external or reused or bool(call.get("id"))
)
else:
call["state"] = "running"
call.pop("error", None)
call.pop("consecutive_failures", None)
if not starting and not local and not external and not reused:
try:
collect_progress(plan, stage, call)
except Exception:
logger.warning(
"Native progress unavailable: %s %s", plan_id, stage, exc_info=True
)
call["progress_error"] = (
"Live worker progress is unavailable; retained results remain available."
)
except Exception as exc:
logger.exception("Native evaluation stage failed: %s %s", plan_id, stage)
record_failure(call, exc, can_retry=local or external or reused or bool(call.get("id")))
if call["state"] == "failed" and not local:
if not external and not reused and call.get("id"):
try:
collect_progress(plan, stage, call)
except Exception:
logger.warning(
"Final progress unavailable; retaining verified local results: %s %s",
plan_id,
stage,
exc_info=True,
)
try:
call["receipt"] = retain_partial(plan, stage)
call["state"] = "completed"
except Exception:
logger.exception(
"Partial evaluation results could not be retained: %s %s", plan_id, stage
)
elif call["state"] == "failed" and stage == "fit_calibration":
calibration = {
"arms": {},
"unavailable": {p["key"]: call["error"] for p in participants(plan)},
"fitted_on": "calibration",
"decisions": 0,
}
NativeEvaluationPlan.objects.filter(pk=plan.pk, calibration={}).update(
calibration=calibration
)
call.update(state="completed", receipt={"partial": True})
with transaction.atomic():
locked = NativeEvaluationPlan.objects.select_for_update().get(pk=plan.pk)
if locked.calls.get(stage, {}).get("lease") != lease:
return
call["observation_active_seconds"] = (
call.get("observation_active_seconds", 0) + time.perf_counter() - observed_at
)
call["observed_at"] = timezone.now().isoformat()
call["timing_scope"] = (
"local stage execution and provider observation; native provider compute duration is separate"
)
call.pop("enqueued_at", None)
call.pop("lease", None)
call.pop("lease_until", None)
locked.calls = {**locked.calls, stage: call}
if (
stage == "verify_inputs"
and preparing_only
and call["state"] == "completed"
and locked.state != "paused"
):
locked.state = "prepared"
elif stage == "score" and call["state"] == "completed":
locked.state, locked.results = "completed", receipt
else:
settle(locked)
locked.save()
finally:
beat.set()
refresh_safely(locked)
def stage_held(call):
lease_until = call.get("lease_until")
return bool(lease_until) and datetime.fromisoformat(lease_until) > timezone.now()
def renew_stage(plan_id, stage, lease):
with transaction.atomic():
plan = NativeEvaluationPlan.objects.select_for_update().get(pk=plan_id)
call = plan.calls.get(stage) or {}
if call.get("lease") != lease:
return
call = {**call, "lease_until": heartbeat.lease_end().isoformat()}
plan.calls = {**plan.calls, stage: call}
plan.save(update_fields=["calls"])
def describe(plan):
return {
"id": str(plan.id),
"job": str(plan.job_id) if plan.job_id else None,
"project": str(plan.project_id),
"name": plan.name,
"state": plan.state,
"deleted_at": plan.deleted_at.isoformat() if plan.deleted_at else None,
"config": plan.config,
"calls": plan.calls,
"calibration": plan.calibration,
"results": plan.results,
"error": plan.error,
"created_at": plan.created_at.isoformat(),
"updated_at": plan.updated_at.isoformat(),
}
class NativeSubmissionRejectedError(InputValidationError):
pass
def decision_rows(cell):
"""Training and development rows of a decision dataset: each holds its request and its
target in one object, which a development measurement reads as the example and its
reference."""
report = (cell.intent_report or {}).get("train") or {}
return bool(report.get("ok")) and report.get("format") == "decision"
def validate_suite(project, cell, *, purpose):
if cell.dataset.project_id != project.pk:
raise InputValidationError("Evaluation suites must belong to this project.")
if purpose == "final" and cell.dataset.intent != "eval":
raise InputValidationError("Final qualification requires an evaluation dataset")
readable, reason = cell.fits("eval")
if not readable and purpose == "development" and decision_rows(cell):
return
if not readable:
raise InputValidationError(f"Evaluation data is not readable: {reason}")
evaluation = (cell.intent_report or {}).get("eval", {})
if evaluation.get("input_type") != "decision" or any(
key != "decision" and value for key, value in evaluation.get("input_types", {}).items()
):
raise InputValidationError(
"Evaluation suites must contain native decision inputs and separate references."
)
def prediction_snapshot(plan, participant):
if plan.state != "completed":
raise InputValidationError("Select a completed native decision evaluation")
selected = next((item for item in participants(plan) if item["key"] == participant), None)
if selected is None:
raise InputValidationError("Select a retained evaluation participant")
stage = plan.calls.get(f"final_{participant}", {})
receipt = stage.get("receipt", {})
report = (
plan.results.get("comparisons", {}).get(participant, {}).get("raw", {}).get("candidate", {})
)
checksum = report.get("predictions_sha256")
if stage.get("state") != "completed" or not checksum:
raise InputValidationError("The participant has no completed prediction receipt")
seal_suite(plan, "final")
source = directory(plan, "final")
prediction_file = source / f"{participant}.jsonl"
if digest_file(prediction_file) != checksum:
raise InputValidationError("Retained native prediction bytes changed")
provider_checksum = receipt.get("predictions_sha256")
reused = plan.config.get("reuse", {}).get(participant)
if reused:
if reused["stages"]["final"]["predictions_sha256"] != checksum:
raise InputValidationError("Reused native prediction identity changed")
elif provider_checksum:
provider_file = (
prediction_file if receipt.get("partial") else source / f"{participant}-provider.jsonl"
)
if digest_file(provider_file) != provider_checksum:
raise InputValidationError("Retained provider prediction bytes changed")
if digest_file(source / "manifest.json") != report.get("suite_manifest_sha256"):
raise InputValidationError("Retained native population changed")
def keyed(path, field):
result = {}
for item in records(path):
key = str(item[field])
if key in result:
raise InputValidationError("Native evidence contains duplicate row identities")
result[key] = item
return result
inputs = keyed(source / "inputs.jsonl", "key")
references = keyed(source / "references.jsonl", "key")
invalid = keyed(source / "failures.jsonl", "source_row")
predictions = keyed(prediction_file, "key")
if (
inputs.keys() != references.keys()
or inputs.keys() & invalid.keys()
or predictions.keys() - inputs.keys()
):
raise InputValidationError("Native predictions do not match the frozen source population")
materialized = {}
for key, request in inputs.items():
reference, prediction = references[key], predictions.get(key)
if reference["input_sha256"] != request["input_sha256"]:
raise InputValidationError("Native reference identity differs from its input")
error, output = "Missing retained prediction", None
if prediction is not None:
if prediction.get("input_sha256") != request["input_sha256"]:
raise InputValidationError("Native prediction identity differs from its input")
decision = request["decision"]
try:
if prediction.get("kind") != decision["kind"]:
raise ValueError("Prediction kind differs from the frozen input")
score_decision(decision, reference["reference"], prediction)
output = {
"kind": decision["kind"],
"options": decision["options"],
"probabilities": prediction["probabilities"],
"log_probabilities": prediction["log_probabilities"],
}
error = ""
except (ValueError, TypeError, KeyError) as exc:
error = str(exc)
materialized[key] = {"output": output, "error": error}
for key, item in invalid.items():
materialized[key] = {"output": None, "error": item["error"]}
return {
"origin": {
"evaluation": str(plan.pk),
"phase": plan.config.get("purpose", "final"),
"participant": selected,
"cell": str(plan.final_cell_id),
"source_fingerprint": plan.config["suites"]["final"]["fingerprint"],
"predictions_sha256": checksum,
"manifest_sha256": digest_file(source / "manifest.json"),
"receipt": receipt,
},
"rows": materialized,
}
def require_execution_authority(plan):
"""A research run's evaluation submits provider work only while that run may still
evaluate. Evaluations a person started are not checked here."""
owner_id = (plan.config.get("research") or {}).get("assignment")
if not owner_id:
return
# Lazy: research tools call evaluation admission.
from overbae.services import execution_mandates # noqa: PLC0415
plan.refresh_from_db(fields=["config", "state"])
if not Project.objects.filter(pk=plan.project_id, is_active=True).exists():
raise NativeSubmissionRejectedError("Evaluation project is inactive")
authorize = (
execution_mandates.require_continuation
if plan.config.get("authorization", {}).get("scope") == "saved_participants_and_suites"
else execution_mandates.require
)
deadline = plan.config["research"].get("deadline")
if deadline and datetime.fromisoformat(deadline) <= timezone.now():
raise NativeSubmissionRejectedError("Evaluation execution deadline ended")
owner = ResearchAssignment.objects.filter(pk=owner_id, project_id=plan.project_id).first()
if owner is None:
raise NativeSubmissionRejectedError("Evaluation execution owner is unavailable")
try:
authorize(owner, "qualify" if plan.config.get("purpose") == "final" else "evaluate")
except InputValidationError as exc:
raise NativeSubmissionRejectedError(str(exc)) from exc