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
2 changes: 1 addition & 1 deletion AGENTS.md

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/agent_env/bundle/materialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def through_ledger(write: Write, write_fn: Callable[[], int]) -> None:
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)):
why := _unbootable(plan, write, done[_key(write)].version, composing)):
unbootable.append(why)
if unbootable:
raise BundleError(unbootable)
Expand Down Expand Up @@ -352,24 +352,25 @@ 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."""
def _unbootable(plan: Plan, write: Write, version: int, composing: Composing) -> str | None:
"""Why the computer env ``write`` left at ``version`` can't be deployed, when its compose recorded builds of
``composing``'s forms 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()
forms = composing.forms(env, composing.provider())
records = [(f"{form}/{arch}", record) for form, by_arch in env.metadata.get("composed", {}).items() if form in forms
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"
how = (", on a --sandbox that runs a skipped arch 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")
f"written: register again to compose what it left, or compose it with agent-env env build --id "
f"{write.id} --version {version}{how}, and then register again")


def _write_env(plan: Plan, write: Write, composing: Composing = Composing()) -> int:
Expand Down
8 changes: 5 additions & 3 deletions src/agent_env/bundle/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ def register_bundle(
name and a dash unless given. An id the store holds already takes a new version, whoever wrote it. ``on_progress``
is given a line for each write, as it's checked or written, and before each image is built. A folder with no
bundle.toml that uses plugin packages is given one, or told what to declare, as ``declaring`` says: by default as a
run of the folder would (``declaring_for``). ``sandbox`` is the provider its computer envs compose on, by default
the env's, emulating an arch no link runs natively unless ``emulate`` is False, which leaves it skipped.
run of the folder would (``declaring_for``). ``sandbox`` is the provider its tasks deploy on, as a run's is: each
computer env composes only the forms its deploys there boot, and without it every form its base declares. Either
way it composes on the env's provider, emulating an arch no link of that one 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. 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)
composing = Composing.for_run(build_sandbox_provider(sandbox), emulate) if sandbox else Composing(None, emulate)
with namespace_routing():
bundle = parse_bundle(Path(root))
resolved = resolve_bundle(bundle)
Expand Down
10 changes: 6 additions & 4 deletions src/agent_env/cli/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ def _report(imported: BundleImport, folder: Path) -> None:
@click.option("--dry-run", is_flag=True, help="Check everything and say what would be written; write nothing.")
@click.option("--yes", is_flag=True, help="Write to a store other than this machine's own without asking.")
@click.option("--sandbox", default=None,
help="The sandbox provider the bundle's computer envs compose on, by default the env's; comma-separated "
"for a chain, each arch going to the first link that runs it natively.")
help="The sandbox provider the registered tasks deploy on, comma-separated for a chain, as agent-env "
"run's: each computer env composes only the forms its deploys there boot, and without it every form "
"its base declares, on this machine either way. A deploy that boots a form not composed composes it "
"first.")
@click.option("--no-emulate", is_flag=True,
help="Skip, rather than emulate, an arch no link of the sandbox provider runs natively; a later register "
"or env build builds it.")
help="Skip, rather than emulate, a computer env's arch the provider it composes on doesn't run "
"natively; a later register or env build builds it.")
def register(folder: Path, tasks: tuple[str, ...], evals: tuple[str, ...], prefix: str | None,
dry_run: bool, yes: bool, sandbox: str | None, no_emulate: bool):
"""Write the bundle at FOLDER into the configured stores under registry ids, so whatever runs the stores' tasks
Expand Down
2 changes: 1 addition & 1 deletion tst/unit/bundle/computer_env_folder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def compose(env, pairs=None, composing=Composing()):
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",)
f"agent-env env build --id {env_id} --version 1, and then register again",)
with pytest.raises(NotFoundError):
Task.get(task_id)

Expand Down
70 changes: 59 additions & 11 deletions tst/unit/bundle/register_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess
from dataclasses import fields
from pathlib import Path
from types import SimpleNamespace

import pytest
from click.testing import CliRunner
Expand Down Expand Up @@ -513,38 +514,85 @@ 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."""
def _desktop(home, monkeypatch, compose):
"""A bundle of a computer env on the shipped base and driver, registered into a shared store as the hub is, under
their aliases, ``compose`` standing in for the composer."""
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"))
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"}])}
return layout(home / "desk", files)


def _link(containers=(), disks=()):
"""A sandbox provider that runs containers of ``containers`` and boots disks of ``disks`` natively."""
link = SimpleNamespace(native_platforms=lambda: frozenset(f"linux/{arch}" for arch in containers),
disk_image_platforms=lambda: frozenset(f"linux/{arch}" for arch in disks), ON_THIS_MACHINE=True)
link.links = (link,)
return link


_SANDBOXES = {"docker": _link(["amd64"]), "vms": _link(disks=["amd64"]),
"chain": SimpleNamespace(links=(_link(["amd64"]), _link(disks=["amd64"])))}


@pytest.mark.parametrize("sandbox, forms", [(None, ["container", "disk"]), ("docker", ["container"]),
("vms", ["disk"]), ("chain", ["container", "disk"])])
def test_a_registration_composes_here_the_forms_its_deploys_on_its_sandbox_boot_else_every_form(
home, composed, monkeypatch, sandbox, forms):
composings = []
root = _desktop(home, monkeypatch, lambda env, pairs=None, composing=None: composings.append(composing))
monkeypatch.setattr(register_module, "build_sandbox_provider", _SANDBOXES.__getitem__)

result = CliRunner().invoke(cli, ["bundle", "register", str(root), "--yes",
*(["--sandbox", sandbox] if sandbox else [])])

assert result.exit_code == 0, result.output
[used] = composings
assert used.forms(Env.get("desk-desktop"), used.provider()) == forms
assert (used.sandbox_provider, used.deploys_on) == (None, _SANDBOXES.get(sandbox))


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."""
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"])
result = CliRunner().invoke(cli, ["bundle", "register", str(_desktop(home, monkeypatch, compose)), "--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
"again to compose what it left, or compose it with agent-env env build --id desk-desktop --version 1, on a "
"--sandbox that runs a skipped arch natively, 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_registration_for_deploys_that_boot_containers_fails_on_a_failed_container_whatever_disk_was_built(
home, composed, monkeypatch):
def compose(env, pairs=None, composing=None):
env.update_metadata({**env.metadata, "composed": {
"container": {"amd64": {"status": "failed", "composer": composition()}},
"disk": {"amd64": {"status": "unverified", "image": "disk", "composer": composition()}}}})

root = _desktop(home, monkeypatch, compose)
monkeypatch.setattr(register_module, "build_sandbox_provider", _SANDBOXES.__getitem__)

result = CliRunner().invoke(cli, ["bundle", "register", str(root), "--yes", "--sandbox", "docker"])

assert result.exit_code == 1 and "has no build a deploy can boot (container/amd64 failed), so" in result.output


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