Skip to content

The platform for continuously improving AI agents.

2.7 KB
Raw Blame
"""Where a research agent's executor runs. One launch is one agent epoch: the OpenHands runner
in the research image, with the conversation's two volumes at /state and /work, calling the
platform back at RESEARCH_CALLBACK_URL. Modal runs it in a Sandbox; a local engine runs it
in a container for development. Both keep a launch's identity: its release, volumes and
labels are checked whenever the launch is read back."""

from dataclasses import dataclass, field
from typing import Protocol

from overbae.core.errors import InputValidationError

# The research image's entrypoint is python: both hosts run the runner with these arguments.
RUNNER = ("-m", "agent_runtime.openhands_runner")
STATE = "/state"
WORK = "/work"


class ExecutorUnavailableError(RuntimeError):
    """The executor's host did not answer; the caller looks again later."""


@dataclass(frozen=True)
class Launch:
    name: str
    release: str
    conversation: str
    labels: dict[str, str] = field(default_factory=dict)

    @property
    def volumes(self) -> dict[str, str]:
        return {
            f"overmind-research-state-{self.conversation}": STATE,
            f"overmind-research-work-{self.conversation}": WORK,
        }

    def manifest(self) -> dict:
        return {
            "name": self.name,
            "release": self.release,
            "conversation": self.conversation,
            "labels": self.labels,
        }

    @classmethod
    def from_manifest(cls, manifest: dict) -> "Launch":
        return cls(
            name=manifest["name"],
            release=manifest["release"],
            conversation=manifest["conversation"],
            labels=manifest["labels"],
        )


@dataclass(frozen=True)
class Status:
    # created: made but never started (a crash between the two); running; exited.
    state: str
    exit_code: int | None = None

    @property
    def running(self) -> bool:
        return self.state == "running"


class Executor(Protocol):
    def release(self) -> str:
        """The immutable identity of the research image a new conversation starts on."""

    def dependencies(self, release: str) -> str:
        """A digest of a release's dependencies, without the runner's own source."""

    def launch(self, launch: Launch, environment: dict[str, str]) -> None:
        """Start the launch; one of that name that already started is left as it is."""

    def status(self, launch: Launch) -> Status | None:
        """The launch as it is or ended; None when it was never made or is gone."""

    def stop(self, launch: Launch) -> None:
        """Stop the launch and return once it no longer runs."""


def mismatch() -> InputValidationError:
    return InputValidationError("The retained executor identity does not match its launch")