Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/agent_env/bundle/materialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from agent_env.env import Env
from agent_env.env.envs.computer.env import ComputerEnv
from agent_env.env.envs.computer.utils import composer
from agent_env.env.envs.computer.utils.composed import BUILT, stale
from agent_env.env.envs.computer.utils.composer import Composing
from agent_env.env.registry import get_env_registry
from agent_env.eval import Eval, EvalTask
Expand All @@ -49,6 +50,10 @@
from .plan import Plan, Write, artifact_writer, env_writer, unpinned_store_refs
from .resolve import BuiltImage, BundleImage, ReferencedImage, build_step

#: How much of each build's reason the problem naming a computer env no deploy can boot quotes: a failed build's reason
#: holds the build's error, which can run to pages.
_REASON_CHARS = 300


@dataclass(frozen=True)
class Materialized:
Expand Down Expand Up @@ -91,6 +96,7 @@ def materialize(
composing: Composing = Composing(),
on_progress: Callable[[Write, str], None] | None = None,
sandbox: str | None = None,
bootable: bool = False,
) -> Materialization:
"""Write ``plan``'s entities, tasks and evals. The first write that fails stops it, and every earlier
one stays: they're in the ledger, so the next run reuses them. ``on_wait`` is called when another run
Expand All @@ -107,7 +113,9 @@ def materialize(
reading what the run would write first isn't preflighted, since the store doesn't hold it yet.

Each computer env written is composed as ``composing`` says, and so is what one reused has left, as ``agent-env
env build`` does; ``on_progress``, when given, is told how each goes, a line at a time with its write.
env build`` does; ``on_progress``, when given, is told how each goes, a line at a time with its write. With
``bootable``, as a registration has it, a computer env written or reused whose compose left no build a deploy can
boot stops it once every entity is written, before any task or eval, raising BundleError naming each such env.

``ledger`` keeps the rows, this run's in the ``@local`` namespace's store unless given; it also says whether each
task and eval names the versions it reads (``Ledger.pins_references``) and whether each image the bundle builds is
Expand All @@ -122,6 +130,7 @@ def materialize(
evals = [write for write in plan.writes if write.kind is BundleKind.EVAL]
done: dict[tuple[str, str], Materialized] = {}
left: list[str] = []
unbootable: list[str] = []

def through_ledger(write: Write, write_fn: Callable[[], int]) -> None:
with _noted(plan, write, "checking" if dry_run else "writing"):
Expand Down Expand Up @@ -151,6 +160,11 @@ def through_ledger(write: Write, write_fn: Callable[[], int]) -> None:
if write.kind is BundleKind.ENV and done[_key(write)].reused and _compose_reused(
write, done[_key(write)].version, _composing(composing, on_progress, write), dry_run):
left.append(write.id)
if bootable and not dry_run and write.kind is BundleKind.ENV and (
why := _unbootable(plan, write, done[_key(write)].version)):
unbootable.append(why)
if unbootable:
raise BundleError(unbootable)
unwritten = {_key(item.write) for item in done.values() if not item.reused} if dry_run else set()
written = done if ledger.pins_references else None
built, problems, unchecked = {}, [], []
Expand Down Expand Up @@ -338,6 +352,26 @@ def _compose_reused(write: Write, version: int, composing: Composing, dry_run: b
return True


def _unbootable(plan: Plan, write: Write, version: int) -> str | None:
"""Why the computer env ``write`` left at ``version`` can't be deployed, when its compose recorded builds but none a
deploy boots: each failed, was skipped, or another agent-env recorded it."""
env = Env.get(write.id, version)
if not isinstance(env, ComputerEnv) or env.computer is None:
return None
records = [(f"{form}/{arch}", record) for form, by_arch in env.metadata.get("composed", {}).items()
for arch, record in by_arch.items()]
if not records or any(record.get("status") in BUILT and not stale(record) for _, record in records):
return None
statuses = env.build_statuses()
quoted = {pair: f" ({record['reason'][:_REASON_CHARS]})" for pair, record in records if record.get("reason")}
builds = "; ".join(f"{pair} {statuses[pair]}{quoted.get(pair, '')}" for pair, _ in records)
how = (", emulating a skipped arch or on a --sandbox that runs it natively"
if any(record.get("status") == "skipped" for _, record in records) else "")
return (f"{_path(plan, write)}: {write.id} v{version} has no build a deploy can boot ({builds}), so no task was "
f"written: register again to compose what it left{how}, or compose it with agent-env env build --id "
f"{write.id} --version {version} and then register again")


def _write_env(plan: Plan, write: Write, composing: Composing = Composing()) -> int:
entry = write.source.entry
cls = get_env_registry()[entry.type]
Expand Down
6 changes: 4 additions & 2 deletions src/agent_env/bundle/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def register_bundle(
the env's, emulating an arch no link runs natively unless ``emulate`` is False, which leaves it skipped.

It turns namespace routing on while it runs. Raises BundleError listing every problem found before anything is
written; a write that fails stops it, and every earlier one stays, in the ledger, for the next run to reuse."""
written; a write that fails stops it, and every earlier one stays, in the ledger, for the next run to reuse. So does
a computer env whose compose left no build a deploy can boot, once every entity is written and before any task:
registering again composes what it has left."""
composing = Composing(build_sandbox_provider(sandbox) if sandbox else None, emulate)
with namespace_routing():
bundle = parse_bundle(Path(root))
Expand All @@ -116,7 +118,7 @@ def register_bundle(
on_wait=lambda: say("waiting for another agent-env run to finish writing this bundle's ids"),
on_build=lambda write: say(f" {_label(registered, write)}: building with docker and pushing"),
on_write=lambda done: say(f" {registered_line(registered, done)}"), composing=composing,
on_progress=lambda write, line: say(f" {_label(registered, write)}: {line}"))
on_progress=lambda write, line: say(f" {_label(registered, write)}: {line}"), bootable=True)
return Registration(registered, materialization, ids, tuple(_floating(registered)), identity, shared, requires)


Expand Down
3 changes: 2 additions & 1 deletion src/agent_env/cli/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def register(folder: Path, tasks: tuple[str, ...], evals: tuple[str, ...], prefi
registration. The plugin packages the bundle's bundle.toml requires must be installed, as for a run; a folder
with no bundle.toml that uses plugin packages is given one. An id the store holds already takes a new version,
whoever wrote it. A store other than this machine's is written only after you confirm, or with --yes. Registering
again writes only what changed since."""
again writes only what changed since. A computer env whose compose leaves no build a deploy can boot fails the
registration once every entity is written, before any task: registering again composes it again."""
options = {"tasks": tasks, "evals": evals, "prefix": prefix, "sandbox": sandbox, "emulate": not no_emulate}
click.echo(f"Registering {folder} into:")
for line in describe_target():
Expand Down
34 changes: 34 additions & 0 deletions tst/unit/bundle/computer_env_folder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
from agent_env.bundle.run import dry_run_bundle
from agent_env.env import Env
from agent_env.env.envs.computer.utils.composed import composition
from agent_env.env.envs.computer.utils import composer as composer_module
from agent_env.env.envs.computer.utils.composer import Composing
from agent_env.env.envs.computer.env import ComputerEnv
from agent_env.env.envs.multi_env import MultiEnv
from agent_env.store.base import NotFoundError
from agent_env.task import Task
from agent_env.providers.sandbox_providers.local_docker.provider import LocalSandboxProvider
from tst.unit.bundle._support import layout, local_builds
from tst.unit.bundle.computer_app_env_test import APP, TOML as APP_TOML
Expand Down Expand Up @@ -108,6 +111,37 @@ def test_a_reused_computer_env_composes_what_it_has_left_on_the_runs_provider_an
("disk", "amd64"), ("disk", "arm64")])]


def test_a_computer_env_no_deploy_could_boot_stops_its_tasks_until_composing_it_again_builds_one(tmp_path, composed,
monkeypatch):
"""With ``bootable``, as a registration materializes: the env stays written, and its reuse composes what it has
left."""
results = iter(["failed", "ready"])

def compose(env, pairs=None, composing=Composing()):
composed.append(pairs)
env.update_metadata({**env.metadata, "composed": {"container": {
"amd64": {"status": next(results), "composer": composition()},
"arm64": {"status": "ready", "composer": "another agent-env's"}}}})

monkeypatch.setattr(composer_module, "compose", compose)
root = _bundle(tmp_path / "desk")
env_id, task_id = (f"{_plan(root).bundle.bundle.id_root}/{name}" for name in ("desktop", "t"))

with pytest.raises(BundleError) as stopped:
materialize(_plan(root), build_here=local_builds(_plan(root)), bootable=True)
assert stopped.value.problems == (
f"envs/desktop: {env_id} v1 has no build a deploy can boot (container/amd64 failed; container/arm64 ready, by "
f"another composer), so no task was written: register again to compose what it left, or compose it with "
f"agent-env env build --id {env_id} --version 1 and then register again",)
with pytest.raises(NotFoundError):
Task.get(task_id)

again = materialize(_plan(root), build_here=local_builds(_plan(root)), bootable=True)

assert composed == [None, [("container", "amd64"), ("container", "arm64"), ("disk", "amd64"), ("disk", "arm64")]]
assert [done.write.id for done in again.writes if not done.reused] == [task_id]


def test_a_dry_run_says_what_it_would_compose_of_a_reused_computer_env(tmp_path, composed):
"""Only the forms its deploys on the run's sandbox boot: local Docker's, the containers."""
root = _bundle(tmp_path / "desk")
Expand Down
34 changes: 34 additions & 0 deletions tst/unit/bundle/register_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from agent_env.bundle import register as register_module
from agent_env.artifact.artifacts import computer as computer_artifact_module
from agent_env.env import store as env_store_module
from agent_env.env.envs.computer.utils import composer as composer_module
from agent_env.env.envs.computer.utils.composed import composition
from agent_env.env.published_images import COMPUTER_IMAGES
from agent_env.bundle.register import REGISTRATIONS, _remote_name, register_bundle
from agent_env.cli import cli
Expand Down Expand Up @@ -511,6 +513,38 @@ def test_chromium_registers_into_a_shared_store_with_its_driver_and_base_compute
assert (chromium.type, chromium.computer.id, chromium.driver.id) == ("computer_app", "ubuntu-24.04-browser", "page")


def test_a_computer_env_whose_compose_leaves_nothing_a_deploy_boots_fails_the_registration_before_its_tasks(
home, composed, monkeypatch):
"""As a builder whose disk filled."""
for module in (register_module, env_store_module):
monkeypatch.setattr(module, "shared_store", lambda store: True)
bases = {image.ref() for image in COMPUTER_IMAGES}
monkeypatch.setattr("agent_env.env.published_images._registry_digest", lambda ref: DIGEST if ref in bases else None)
monkeypatch.setattr(computer_artifact_module, "_inspect",
lambda image, arches, read: computer_artifact_module._unread(arches, "no registry in a test"))

def compose(env, pairs=None, composing=None):
env.update_metadata({**env.metadata, "composed": {"container": {
"amd64": {"status": "failed", "reason": "no space left on device", "composer": composition()},
"arm64": {"status": "skipped", "reason": "no link runs linux/arm64 natively", "composer": composition()}}}})

monkeypatch.setattr(composer_module, "compose", compose)
files = {"envs/desktop/env.toml": 'type = "computer"\ncomputer = "ubuntu-24.04"\ndriver = "reference"\n',
"tasks/t.json": json.dumps([{"id": "env", "type": "deploy_env", "env_id": "desktop"}])}

result = CliRunner().invoke(cli, ["bundle", "register", str(layout(home / "desk", files)), "--yes"])

assert result.exit_code == 1, result.output
assert ("envs/desktop: desk-desktop v1 has no build a deploy can boot (container/amd64 failed (no space left on "
"device); container/arm64 skipped (no link runs linux/arm64 natively)), so no task was written: register "
"again to compose what it left, emulating a skipped arch or on a --sandbox that runs it natively, or "
"compose it with agent-env env build --id desk-desktop --version 1 and then register again"
) in result.output
assert Env.get("desk-desktop").version == 1
with pytest.raises(NotFoundError):
Task.get("desk-t")


def test_a_write_that_fails_leaves_no_pending_row(home, monkeypatch):
def failing(*args, **kwargs):
raise RuntimeError("the store went away")
Expand Down
Loading