The platform for continuously improving AI agents.
from typing import Any, Literal
from pydantic import Field, model_validator
from agent_runtime.contracts import Contract
from overbae.services.experiment_catalog import COMPONENTS, resolved
class Position(Contract):
x: float = Field(ge=-100000, le=100000)
y: float = Field(ge=-100000, le=100000)
class Operation(Contract):
tool: Literal[
"workspace",
"platform",
"training",
"serving",
"benchmark",
"prediction",
"score",
"component",
]
arguments: dict = Field(
default_factory=dict,
description='The complete direct tool-call payload, including its envelope. For training creation: {"operation":"create","arguments":{"run":{"dataset":"...","cell":"...","base_model":"...","hyperparameters":{...}},"trials":1}}. Do not flatten inner arguments or rename operation to action.',
)
StageKind = Literal[
"ingest",
"trace_dataset",
"check_data",
"export_data",
"inference",
"verify_deployment",
"performance",
"stop_deployment",
"component",
"suite",
"validate",
"data",
"transform",
"split",
"model",
"train",
"predict",
"evaluate",
"compare",
"qualify",
"publish",
"research",
"condition",
"join",
"subflow",
"map",
"iterate",
]
class Block(Contract):
id: str = Field(min_length=1, max_length=100, pattern=r"^[a-zA-Z0-9_-]+$")
kind: StageKind
title: str = Field(min_length=1, max_length=200)
position: Position
config: dict = Field(
default_factory=dict,
description="Executable component settings. Model inputs select config.model. Data inputs pin config.cell; the saved node records its dataset and fingerprint. Training programs contain config.files and config.manifest. Native operations use operation.arguments; other config fields are validated by the component compiler.",
)
operation: Operation | None = None
notes: str = Field(default="", max_length=8000)
@model_validator(mode="after")
def meaningful_operation(self):
if self.operation is None:
return self
tool, args = self.operation.tool, self.operation.arguments
name = args.get("name") if tool == "platform" else args.get("operation")
valid = (tool, name) in COMPONENTS[self.kind].operations
if not valid:
raise ValueError("Use an operation that produces this node's declared output")
return self
class Connection(Contract):
source: str
target: str
output: str
input: str
class Parameter(Contract):
type: Literal["string", "number", "integer", "boolean", "object", "array"]
description: str = Field(default="", max_length=1000)
default: Any = None
required: bool = True
enum: list[Any] | None = None
class Output(Contract):
node: str
port: str
class Document(Contract):
parameters: dict[str, Parameter] = Field(default_factory=dict, max_length=50)
outputs: dict[str, Output] = Field(default_factory=dict, max_length=30)
nodes: list[Block] = Field(default_factory=list, max_length=100)
edges: list[Connection] = Field(default_factory=list, max_length=250)
@model_validator(mode="after")
def topology(self):
nodes = {n.id: n for n in self.nodes}
if len(nodes) != len(self.nodes):
raise ValueError("Node identities must be unique")
incoming, outgoing, ports = dict.fromkeys(nodes, 0), {n: [] for n in nodes}, set()
for edge in self.edges:
if edge.source not in nodes or edge.target not in nodes:
raise ValueError("Connections must reference existing nodes")
if edge.input == "model" and isinstance(nodes[edge.target].config.get("model"), dict):
raise ValueError("Select a model inline or connect a produced model, not both")
output = resolved(nodes[edge.source]).outputs.get(edge.output)
target = resolved(nodes[edge.target]).inputs.get(edge.input)
if output is None or output != target:
source_ports = resolved(nodes[edge.source]).outputs
target_ports = resolved(nodes[edge.target]).inputs
raise ValueError(
f"Incompatible connection {edge.source}.{edge.output} -> "
f"{edge.target}.{edge.input}. Source outputs: {source_ports}; "
f"target inputs: {target_ports}. Connect ports with the same artifact type."
)
if (edge.target, edge.input) in ports and edge.input not in resolved(
nodes[edge.target]
).many:
raise ValueError(
f"Input {edge.target}.{edge.input} already has a connection. "
"Use a collection input to connect multiple reports."
)
ports.add((edge.target, edge.input))
incoming[edge.target] += 1
outgoing[edge.source].append(edge.target)
queue = [n for n, count in incoming.items() if not count]
visited = []
while queue:
node = queue.pop()
visited.append(node)
for target in outgoing[node]:
incoming[target] -= 1
if incoming[target] == 0:
queue.append(target)
if len(visited) != len(nodes):
raise ValueError("Experiment connections must be acyclic")
for output in self.outputs.values():
if output.node not in nodes or output.port not in resolved(nodes[output.node]).outputs:
available = resolved(nodes[output.node]).outputs if output.node in nodes else {}
raise ValueError(
f"Flow output {output.node}.{output.port} is unavailable. "
f"Declared outputs for that node: {available}."
)
return self