diff --git a/AGENTS.md b/AGENTS.md index d547c1c53..305f56ce2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -133,7 +133,7 @@ and deserialized through a registry. | `openenv/` | `build_app(bundle)`: the [OpenEnv](https://github.com/openenv/OpenEnv) server for a bundle's tasks, each session playing one episode through the Episode API, with the envs' MCP tools, an added `end_turn` tool and the task's verifier score as the reward. It prepares bundles as `agent-env run` does (`prepare_bundle(played=True)`, without the agent the client plays) and keeps namespace routing on for the app's life. `openenv` comes with the `openenv` extra, a floor with no cap; this package and `training/` are the only ones that import it. Each task's description in the Task API names its `toolset` (the played agent's envs, role, and whether it gets `bash`). With `token`, every request and WebSocket but `/health` must carry `Authorization: Bearer `, for a server others reach. | | `training/` | One module per outside trainer, each behind its own extra, and the modules they share; [Training helpers](#training-helpers) lists them. | | `runner/` | The `[runner]` seam: `Runner.submit()` returns a `RunHandle` (`run_id`, `instance_id`) before the run starts; `LocalRunner` is built in. `runner/store.py` is the run index every runner writes and the explorer reads. | -| `bundle/` | A bundle is a folder of entities (`envs/`, `agents/`, `artifacts/`, `skills/`, `tasks/.json`, `evals/.toml`) that runs without a registry: `agent-env run` plans it, writes it into the `@local` namespace (reusing each version whose inputs haven't changed) and runs its evals or tasks; `agent-env run hf:///[@][/]` (`bundle/hub.py`) runs one from a Hugging Face Hub dataset, picked from the commit's file list (the path, else `bundles/`, else the root) and downloaded alone with the card into a folder per commit, under ids rooted at `@hf//`, which only that path produces, each run's context recording the commit (`metadata["bundle_source"]`); `agent-env bundle init` writes a stored task or eval as one, with every env, agent and artifact it uses in its own folder (each image as its build context, pruned to what its Dockerfile reads and pinned to the Python and npm versions of the stored image, or, given `--image-ref ID=REF`, as that registry image pinned by digest with its context beside it, `image = { ref, dockerfile }`, which a run pulls and builds only when it can't; `task_images` lists the images an import writes, downloading nothing, so an export can push them first) so it runs with no store; `agent-env bundle register` writes a folder into the configured stores under registry ids (``, and an installed bundle's agent, env or artifact a reference pulls in under its alias, an env only into a shared store), each task and eval naming the versions it reads and each image it builds pushed, through a ledger in the target store so registering again writes only what changed. | +| `bundle/` | A bundle is a folder of entities (`envs/`, `agents/`, `artifacts/`, `skills/`, `tasks/.json`, `evals/.toml`) that runs without a registry: `agent-env run` plans it, writes it into the `@local` namespace (reusing each version whose inputs haven't changed) and runs its evals or tasks; `agent-env run hf:///[@][/]` (`bundle/hub.py`) runs one from a Hugging Face Hub dataset, picked from the commit's file list (the path, else `bundles/`, else the root) and downloaded alone with the card into a folder per commit, under ids rooted at `@hf//`, which only that path produces, each run's context recording the commit (`metadata["bundle_source"]`); `agent-env bundle init` writes a stored task or eval as one, with every env, agent and artifact it uses in its own folder (each image as its build context, pruned to what its Dockerfile reads and pinned to the Python and npm versions of the stored image, or, given `--image-ref ID=REF`, as that registry image pinned by digest with its context beside it, `image = { ref, dockerfile }`, which a run pulls and builds only when it can't; `task_images` lists the images an import writes, downloading nothing, so an export can push them first; a computer env as its base, driver and apps, each with its files, never what it composed, which a run of the folder composes, and an env or artifact of an installed bundle, there or anywhere an entity names one, by its id) so it runs with no store; `agent-env bundle register` writes a folder into the configured stores under registry ids (``, and an installed bundle's agent, env or artifact a reference pulls in under its alias, an env only into a shared store), each task and eval naming the versions it reads and each image it builds pushed, through a ledger in the target store so registering again writes only what changed. | | `eval/` | `Eval` (`eval`), a versioned list of task references, and its store. | | `plugins/` | Entry-point plugins (see Extension points): the group names, `inventory()`, which `agent-env plugin list/show/check` report, `load_failures` and `settings`. The top level is public; the underscored modules are internal. | | `explorer/` | Optional local web UI: `agent-env up`, binds loopback `:8234`. | diff --git a/src/agent_env/artifact/artifacts/computer.py b/src/agent_env/artifact/artifacts/computer.py index cab72b294..fbe31c592 100644 --- a/src/agent_env/artifact/artifacts/computer.py +++ b/src/agent_env/artifact/artifacts/computer.py @@ -35,6 +35,7 @@ if TYPE_CHECKING: from agent_env.bundle.authoring import AuthoringContext + from agent_env.entity.export import ExportContext FORMS = ("container", "disk", "vm_bundle") #: A VM bundle's image that is already on the host that boots it, by its name there, as composing leaves one. @@ -365,6 +366,22 @@ def from_toml(cls, data: dict[str, Any], ctx: AuthoringContext) -> Self: except ValueError as e: ctx.refuse([str(e)]) + def to_toml(self, out: ExportContext) -> dict: + """The artifact.toml of a folder each form's image is built from, or names from a registry, which ``from_toml`` + reads back as it.""" + forms = self.forms.model_dump(mode="json", exclude_none=True) + if self.forms.container is not None: + forms["container"]["image"] = out.image(self.image(), "forms.container.image") + if (disk := self.disk_image()) is not None: + forms["disk"]["image"] = out.image(disk, "forms.disk.image") + toml = {"type": self.type, **({"description": self.description} if self.description else {}), + "arch": list(self.arch), "os": self.os.model_dump(mode="json", exclude_none=True), + "session": self.session.model_dump(mode="json"), "forms": forms} + if self.config: + toml["config"] = {name: setting.model_dump(mode="json", exclude_defaults=True) + for name, setting in self.config.items()} + return toml + def image(self) -> DockerImageArtifact: """The container form's image artifact, refused when the store has no docker_image artifact of that id and version.""" diff --git a/src/agent_env/bundle/importer.py b/src/agent_env/bundle/importer.py index 562e30b47..60666da21 100644 --- a/src/agent_env/bundle/importer.py +++ b/src/agent_env/bundle/importer.py @@ -149,6 +149,9 @@ def __init__(self, importer: _Importer, folder: str, toml_name: str | None): def ref(self, entity: Any) -> str | dict: kind = _kind_of(entity) + if (bundle := self.importer._installed_for(kind, entity.id)) is not None: + self.importer.floating.append(Floating(self.folder, kind, entity.id, _from_bundle(bundle))) + return entity.id local = self.importer.local(kind, entity, self.folder) return local if local is not None else {kind.value: entity.id, "version": entity.version} @@ -534,7 +537,7 @@ def _installed_for(self, kind: EntityKind, entity_id: str) -> InstalledBundle | def _from_installed(self, site: RefSite, where: str, bundle: InstalledBundle) -> None: """An installed bundle's entity has no fixed version, so it isn't pinned, and a pin the store held is dropped.""" - why = f"it comes with the installed bundle {bundle.qualified}, which a run writes it from" + why = _from_bundle(bundle) if site.version is not None and site.version_key is not None: why += f"; the stored pin to v{site.version} is dropped, as such an entity has no fixed version" _unpin(site) @@ -579,6 +582,10 @@ def _names_of(kind: BundleKind) -> str: return BundleKind.ARTIFACT.value if kind is BundleKind.SKILL else kind.value +def _from_bundle(bundle: InstalledBundle) -> str: + return f"it comes with the installed bundle {bundle.qualified}, which a run writes it from" + + def _kind_of(entity: Any) -> EntityKind: if isinstance(entity, Env): return EntityKind.ENV diff --git a/src/agent_env/env/envs/computer/app_env.py b/src/agent_env/env/envs/computer/app_env.py index ad3708e65..52d059a3c 100644 --- a/src/agent_env/env/envs/computer/app_env.py +++ b/src/agent_env/env/envs/computer/app_env.py @@ -30,6 +30,7 @@ from collections.abc import Iterable from agent_env.bundle.authoring import AuthoringContext + from agent_env.entity.export import ExportContext #: What an app deployed alone adds to its name to name its computer. @@ -171,6 +172,18 @@ def from_toml(cls, data: dict, ctx: AuthoringContext) -> ComputerAppEnv: except ValueError as e: ctx.refuse([str(e)]) + def to_toml(self, out: ExportContext) -> dict: + """The env.toml of a folder holding the app's files, which ``from_toml`` reads back as it.""" + self.files.to_toml(out) + toml = {"type": self.type, "environment_name": self.environment_name, "module": self.module} + if self.computer is not None: + toml |= {"computer": out.ref(self.computer), "driver": out.ref(self.driver)} + if self.build_settings: + toml["build_settings"] = dict(self.build_settings) + if self.settings: + toml["settings"] = dict(self.settings) + return toml + @classmethod def accept_toml(cls, data: dict, ctx: AuthoringContext) -> dict: """The keys of ``data``, an env.toml, a computer app env takes: ``module = ":"``, naming a file of diff --git a/src/agent_env/env/envs/computer/driver_env.py b/src/agent_env/env/envs/computer/driver_env.py index 6d34d7be0..3e6b8ecab 100644 --- a/src/agent_env/env/envs/computer/driver_env.py +++ b/src/agent_env/env/envs/computer/driver_env.py @@ -22,6 +22,7 @@ if TYPE_CHECKING: from agent_env.bundle.authoring import AuthoringContext + from agent_env.entity.export import ExportContext from agent_env.env.env import DeployedEnv #: How long reading a driver's class at put may take. @@ -91,6 +92,20 @@ def from_toml(cls, data: dict, ctx: AuthoringContext) -> ComputerDriverEnv: install=fields["install"], port=fields.get("port", DEFAULT_PORT), known_failures=fields.get("known_failures")) + def to_toml(self, out: ExportContext) -> dict: + """The env.toml of a folder holding the driver's files, which ``from_toml`` reads back as it.""" + self.files.to_toml(out) + toml = {"type": self.type, "module": self.module} + if self.port != DEFAULT_PORT: + toml["port"] = self.port + if self.install: + toml["install"] = {key: install.model_dump(mode="json", exclude_none=True, exclude_defaults=True) + for key, install in self.install.items()} + if self.known_failures: + toml["known_failures"] = {key: [failure.model_dump(mode="json", exclude_defaults=True) for failure in failures] + for key, failures in self.known_failures.items()} + return toml + @classmethod def accept_toml(cls, data: dict, ctx: AuthoringContext) -> dict: """The keys of ``data``, an env.toml, a computer driver env takes: ``module = ":"``, naming a file diff --git a/src/agent_env/env/envs/computer/env.py b/src/agent_env/env/envs/computer/env.py index f84940eed..2b9dd7fbf 100644 --- a/src/agent_env/env/envs/computer/env.py +++ b/src/agent_env/env/envs/computer/env.py @@ -48,6 +48,7 @@ if TYPE_CHECKING: from agent_env.artifact import EnvironmentArtifact, EnvironmentUniverseArtifact from agent_env.bundle.authoring import AuthoringContext + from agent_env.entity.export import ExportContext from agent_env.env.env import DeployedEnv from agent_env.env.envs.computer.app_env import ComputerAppEnv from agent_env.providers.env_providers.env_provider import EnvironmentProvider @@ -168,6 +169,18 @@ def from_toml(cls, data: dict, ctx: AuthoringContext) -> ComputerEnv: except ValueError as e: ctx.refuse([str(e)]) + def to_toml(self, out: ExportContext) -> dict: + """The env.toml of a computer env naming its base, driver and apps, each app with its settings beside it, which + ``from_toml`` reads back as it. What it composed isn't written: a run of the folder composes it.""" + toml = {"type": self.type, "environment_name": self.environment_name, "computer": out.ref(self.computer), + "driver": out.ref(self.driver)} + if self.apps: + toml["apps"] = [_app_entry(out.ref(app), self.app_settings.get(app.environment_name)) for app in self.apps] + for key in ("display", "network", "computer_settings", "tools"): + if value := getattr(self, key): + toml[key] = value + return toml + @classmethod def accept_toml(cls, data: dict, ctx: AuthoringContext) -> dict: """The keys of ``data``, an env.toml, a computer env takes: the ``computer`` and ``driver`` it's composed of, @@ -521,6 +534,13 @@ def _listed_apps(refs: list, ctx: AuthoringContext) -> tuple[list[ComputerAppEnv return apps, app_settings +def _app_entry(ref: str | dict, settings: Optional[dict]) -> str | dict: + """An env.toml's ``apps`` entry: the app's ref, in a table with its ``settings`` beside it when it has some.""" + if not settings: + return ref + return {**(ref if isinstance(ref, dict) else {"env": ref}), "settings": dict(settings)} + + def _not_an_app(env: Env) -> str | None: return f"{env.id} is a {env.type} env, not a {APP_TYPE} env" if env.type != APP_TYPE else None diff --git a/tst/unit/bundle/computer_to_toml_edges_test.py b/tst/unit/bundle/computer_to_toml_edges_test.py new file mode 100644 index 000000000..7015b98de --- /dev/null +++ b/tst/unit/bundle/computer_to_toml_edges_test.py @@ -0,0 +1,155 @@ +"""The edges of writing a computer env into a folder: a folder a run wrote back exports as the same folder, odd setting +values, distribution installs, binary and unicode files keep what they hold, a base named from a registry with a disk +built from a source, an app deployed alone, a driver two computers share, and a stored context that's gone.""" + +import json + +import pytest + +from agent_env.artifact.artifacts.computer import ComputerArtifact +from agent_env.bundle import BundleError, parse_bundle +from agent_env.bundle.importer import import_task +from agent_env.bundle.installed import installed_bundles +from agent_env.bundle.materialize import materialize +from agent_env.bundle.plan import plan_bundle +from agent_env.bundle.resolve import resolve_bundle +from agent_env.config import get_config +from agent_env.env import Env +from tst.unit.bundle._support import DIGEST, layout, local_builds +from tst.unit.bundle.computer_driver_env_test import DRIVER, SERVE +from tst.unit.bundle.computer_to_toml_test import BASE, OWN, OWN_FILES +from tst.util.oci_registry import UBUNTU_LAYER, Registry + +OFFICE = 'type = "multi"\ncomputer_envs = ["desktop"]\n' +UBUNTU_ONLY = [{"os": {"type": "linux", "distribution": "ubuntu", "display": ["x11"]}, "arch": ["amd64", "arm64"]}] + + +def _store(root, files, steps): + """Write a bundle of ``files`` whose task ``t`` is ``steps``, and return its id root.""" + layout(root, {**files, "tasks/t.json": json.dumps(steps)}) + plan = plan_bundle(resolve_bundle(parse_bundle(root)), installed=installed_bundles) + materialize(plan, build_here=local_builds(plan)) + return plan.bundle.bundle.id_root + + +def _run(folder): + """Write ``folder`` back as a run of it does, and return its id root.""" + plan = plan_bundle(resolve_bundle(parse_bundle(folder)), installed=installed_bundles) + materialize(plan, build_here=local_builds(plan)) + return plan.bundle.bundle.id_root + + +def _tree(folder): + """Every file of an imported folder by path, but the two that name where it came from.""" + return {path.relative_to(folder).as_posix(): path.read_bytes() for path in folder.rglob("*") + if path.is_file() and path.name not in ("provenance.json", "README.md")} + + +DEPLOY_OFFICE = [{"id": "env", "type": "deploy_env", "env_id": "office"}] + + +def test_the_folder_a_run_wrote_back_exports_as_the_same_folder(tmp_path, composed): + root = _store(tmp_path / "desk", {**OWN_FILES, "envs/desktop/env.toml": OWN, "envs/office/env.toml": OFFICE}, + DEPLOY_OFFICE) + import_task(f"{root}/t", tmp_path / "once") + once = _tree(tmp_path / "once") + + again = _run(tmp_path / "once") + import_task(f"{again}/t", tmp_path / "twice") + + assert _tree(tmp_path / "twice") == once + + +def test_odd_values_distribution_installs_and_binary_and_unicode_files_come_back_as_they_were(tmp_path, composed): + base = BASE + ('\n[config.depth]\ndefault = 24\nphase = "deploy"\n\n[config.scale]\ndefault = 1.5\nphase = "deploy"\n' + '\n[config.dark]\ndefault = false\nphase = "deploy"\n') + desktop = OWN.replace('theme = "dark"', 'depth = 30\nscale = 2.25\ndark = true').replace( + 'settings = { signature = "-- Ada" }', 'settings = { signature = "-- Ada", password = "env:MAIL_PASSWORD" }') + driver = ('type = "computer_driver"\nmodule = "driver.py:Desk"\n\n[install.linux]\n' + f'{SERVE}\n\n[install."linux/ubuntu"]\npython = "requirements.txt"\n{SERVE}\n') + files = {**OWN_FILES, "artifacts/base/artifact.toml": base, "envs/desktop/env.toml": desktop, + "envs/office/env.toml": OFFICE, "envs/desk/env.toml": driver, + "envs/desk/driver.py": DRIVER.format(supports=UBUNTU_ONLY, buttons=("left", "right")), + "envs/desk/requirements.txt": "mss>=9,<11\n", "envs/mail/docs/naïve note.md": "résumé\n"} + blob = bytes(range(256)) * 4 + (tmp_path / "desk" / "envs" / "mail" / "icons").mkdir(parents=True) + (tmp_path / "desk" / "envs" / "mail" / "icons" / "app.png").write_bytes(blob) + root = _store(tmp_path / "desk", files, DEPLOY_OFFICE) + stored = Env.get(f"{root}/desktop") + + import_task(f"{root}/t", tmp_path / "out") + + (read,) = Env.get(f"{_run(tmp_path / 'out')}/office").computer_envs + assert read.computer_settings == stored.computer_settings == {"depth": 30, "scale": 2.25, "dark": True} + assert [type(read.computer_settings[name]) for name in ("depth", "scale", "dark")] == [int, float, bool] + assert read.computer.config == stored.computer.config + assert read.app_settings["mail"]["password"] == "env:MAIL_PASSWORD" + assert set(read.driver.install) == {"linux", "linux/ubuntu"} and read.driver.install == stored.driver.install + mail = next(app for app in read.apps if app.environment_name == "mail") + files = {path: file.load() for path, file in mail.files.get_file_artifacts().items()} + assert files["icons/app.png"] == blob and files["docs/naïve note.md"] == "résumé\n".encode() + + +def test_a_base_named_from_a_registry_with_a_disk_built_from_a_source_is_written_as_both(tmp_path, composed, registry, + monkeypatch): + served = Registry().serve(monkeypatch) + amd64, arm64 = served.image(UBUNTU_LAYER), served.image(UBUNTU_LAYER, arch="arm64") + served.manifests[DIGEST] = served.manifests[served.index(amd64=amd64, arm64=arm64)] + source = f'{{ url = "https://images.example/noble.img", sha256 = "{"b" * 64}" }}' + base = BASE.replace('image = { dockerfile = "container/Dockerfile" }', 'image = { ref = "ghcr.io/acme/desk:1" }') + ( + f'\n[forms.disk]\nsource = {source}\ninit = "supervisord"\ninput_channel = "nocloud"\nmin_disk_gb = 16\n') + files = {**OWN_FILES, "artifacts/base/artifact.toml": base, "envs/desktop/env.toml": OWN, + "envs/office/env.toml": OFFICE} + del files["artifacts/base/container/Dockerfile"], files["artifacts/base/container/start"] + root = _store(tmp_path / "desk", files, DEPLOY_OFFICE) + stored = ComputerArtifact.get(f"{root}/base") + + imported = import_task(f"{root}/t", tmp_path / "out") + + assert sorted(path.name for path in (tmp_path / "out" / "artifacts" / "base").iterdir()) == ["artifact.toml"] + assert [(pulled.folder, pulled.image) for pulled in imported.pulled] == [ + ("artifacts/base", f"ghcr.io/acme/desk:1@{DIGEST}")] + read = ComputerArtifact.get(f"{_run(tmp_path / 'out')}/base") + assert read.image().image_name == stored.image().image_name == f"ghcr.io/acme/desk:1@{DIGEST}" + assert read.forms.disk == stored.forms.disk and read.forms.disk.source.url == "https://images.example/noble.img" + + +def test_an_app_deployed_alone_is_written_with_the_computer_and_driver_it_deploys_on(tmp_path, composed): + files = {**OWN_FILES, "envs/mail/env.toml": OWN_FILES["envs/mail/env.toml"] + 'computer = "base"\ndriver = "desk"\n'} + root = _store(tmp_path / "desk", files, [{"id": "env", "type": "deploy_env", "env_id": "mail"}]) + + imported = import_task(f"{root}/t", tmp_path / "out") + + assert sorted(entity.folder for entity in imported.written) == ["artifacts/base", "envs/desk", "envs/mail"] + read = Env.get(f"{_run(tmp_path / 'out')}/mail") + assert (read.environment_name, read.build_settings) == ("mail", {"edition": "pro"}) + assert (read.computer.description, read.driver.module) == ("a desk", "driver.py:Desk") + + +def test_a_driver_and_base_two_computers_share_are_written_once(tmp_path, composed): + laptop = 'type = "computer"\ncomputer = "base"\ndriver = "desk"\n' + files = {**OWN_FILES, "envs/desktop/env.toml": OWN, "envs/laptop/env.toml": laptop, + "envs/office/env.toml": 'type = "multi"\ncomputer_envs = ["desktop", "laptop"]\n'} + root = _store(tmp_path / "desk", files, DEPLOY_OFFICE) + + imported = import_task(f"{root}/t", tmp_path / "out") + + folders = [entity.folder for entity in imported.written] + assert sorted(folders) == sorted(set(folders)) and {"envs/desk", "artifacts/base"} <= set(folders) + computers = Env.get(f"{_run(tmp_path / 'out')}/office").computer_envs + assert [env.environment_name for env in computers] == ["desktop", "laptop"] + assert computers[0].driver.id == computers[1].driver.id and computers[0].computer.id == computers[1].computer.id + + +def test_a_base_whose_stored_context_is_gone_fails_the_import_naming_it_and_writes_nothing(tmp_path, composed): + root = _store(tmp_path / "desk", {**OWN_FILES, "envs/desktop/env.toml": OWN, "envs/office/env.toml": OFFICE}, + DEPLOY_OFFICE) + url = ComputerArtifact.get(f"{root}/base").image().build_context_object_url + store = get_config().get_object_store_at(url) + store._resolve(store.get_object_key(url)).unlink() + + with pytest.raises(BundleError) as caught: + import_task(f"{root}/t", tmp_path / "out") + + assert any("artifacts/base" in problem for problem in caught.value.problems), caught.value.problems + assert not (tmp_path / "out").exists() or not any((tmp_path / "out").iterdir()) diff --git a/tst/unit/bundle/computer_to_toml_test.py b/tst/unit/bundle/computer_to_toml_test.py new file mode 100644 index 000000000..9d07cb8bf --- /dev/null +++ b/tst/unit/bundle/computer_to_toml_test.py @@ -0,0 +1,150 @@ +"""``bundle init`` writes a computer env into the folder, as an import that leaves nothing in the store must: its base +computer with the image it's built from, its driver and apps with their files, and the multi env holding it, which a run +of the folder writes back as they were stored; a shipped base or driver stays named by its installed id. The composer is +replaced by a recorder.""" + +import json + +from agent_env.artifact.artifacts.computer import ComputerArtifact +from agent_env.bundle import parse_bundle +from agent_env.bundle.importer import import_task +from agent_env.bundle.installed import installed_bundles +from agent_env.bundle.materialize import materialize +from agent_env.bundle.plan import plan_bundle +from agent_env.bundle.resolve import resolve_bundle +from agent_env.env import Env +from agent_env.env.envs.computer.env import ComputerEnv +from tst.unit.bundle._support import layout, local_builds +from tst.unit.bundle.computer_app_env_test import APP, TOML as APP_TOML +from tst.unit.bundle.computer_driver_env_test import DRIVER, INSTALLED, LINUX, REQUIREMENTS +from tst.unit.bundle.computer_env_folder_test import DESKTOP, REFERENCE, UBUNTU + +BASE = '''type = "computer" +description = "a desk" +arch = ["amd64", "arm64"] +os = { type = "linux", distribution = "ubuntu", version = "24.04", display = "x11" } +session = { user = "user" } + +[forms.container] +image = { dockerfile = "container/Dockerfile" } +init = "supervisord" +input_channel = "env" +start_as = "root" + +[config.resolution] +default = "1280x800" +phase = "deploy" + +[config.theme] +default = "light" +phase = "deploy" +''' +OWN = '''type = "computer" +computer = "base" +driver = "desk" +apps = ["notes", { env = "mail", install = "build", settings = { signature = "-- Ada" } }] +display = { width = 1600, height = 900 } +tools = "both" + +[computer_settings] +theme = "dark" +''' +KNOWN = '\n[[known_failures.linux]]\ncheck = "P3"\nname = "drag"\nreason = "xdotool drops the release"\n' +APPS = {f"envs/{name}/{file}": text for name in ("mail", "notes") for file, text in ( + ("env.toml", f'{APP_TOML}build_settings = {{ edition = "pro" }}\n' if name == "mail" else APP_TOML), + ("app.py", APP.format(supports=LINUX, skill="skill.md", linux_skill="skill-linux.md")), + ("requirements.txt", "agentenv-framework-protocol\n"), ("skill.md", f"# {name}\n"), + ("skill-linux.md", f"# {name} on Linux\n"))} +OWN_FILES = { + "artifacts/base/artifact.toml": BASE, + "artifacts/base/container/Dockerfile": "FROM scratch\nCOPY container/start /start\n", + "artifacts/base/container/start": "#!/bin/sh\n", + "envs/desk/env.toml": INSTALLED + KNOWN, + "envs/desk/driver.py": DRIVER.format(supports=LINUX, buttons=("left", "right")), + "envs/desk/linux/requirements.txt": REQUIREMENTS, "envs/desk/linux/ppa.asc": "key", + "envs/desk/linux/setup.sh": "#!/bin/sh\n", "envs/desk/linux/profile.sh": "#!/bin/sh\n", + **APPS, +} + + +def _stored(root, desktop, extra=None): + """Write a bundle whose task deploys the multi env ``office`` holding the computer env ``desktop``, and return the + stored task's id.""" + layout(root, {"envs/desktop/env.toml": desktop, "envs/office/env.toml": 'type = "multi"\ncomputer_envs = ["desktop"]\n', + "tasks/t.json": json.dumps([{"id": "env", "type": "deploy_env", "env_id": "office"}]), **(extra or {})}) + plan = plan_bundle(resolve_bundle(parse_bundle(root)), installed=installed_bundles) + materialize(plan, build_here=local_builds(plan)) + return f"{plan.bundle.bundle.id_root}/t" + + +def _written_back(folder): + """Run the imported ``folder`` as ``agent-env run`` writes it, with no store, and return its computer env.""" + plan = plan_bundle(resolve_bundle(parse_bundle(folder)), installed=installed_bundles) + materialize(plan, build_here=local_builds(plan)) + office = Env.get(f"{plan.bundle.bundle.id_root}/office") + (desktop,) = office.computer_envs + return desktop + + +def _files(universe) -> dict[str, bytes]: + return {path: file.load() for path, file in universe.get_file_artifacts().items()} + + +def _same(read, stored, *fields): + assert {field: getattr(read, field) for field in fields} == {field: getattr(stored, field) for field in fields} + + +def test_a_computer_env_with_its_own_base_driver_and_apps_reads_back_from_the_folder_as_stored(tmp_path, composed): + task = _stored(tmp_path / "desk", OWN, OWN_FILES) + stored = Env.get(task.rsplit("/", 1)[0] + "/desktop") + + imported = import_task(task, tmp_path / "out") + + assert imported.pinned == () + assert sorted(entity.folder for entity in imported.written) == [ + "artifacts/base", "envs/desk", "envs/desktop", "envs/mail", "envs/notes", "envs/office"] + read = _written_back(tmp_path / "out") + assert isinstance(read, ComputerEnv) and read.id != stored.id + _same(read, stored, "environment_name", "display", "network", "computer_settings", "tools", "app_settings") + _same(read.computer, stored.computer, "description", "arch", "os", "session", "config") + assert read.computer.forms.container.start_as == "root" + assert read.computer.image().dockerfile_path == "container/Dockerfile" + assert [failure.check for failure in stored.driver.known_failures["linux"]] == ["P3"] + _same(read.driver, stored.driver, "module", "port", "install", "known_failures", "declaration") + assert _files(read.driver.files) == _files(stored.driver.files) + assert [app.environment_name for app in read.apps] == ["notes", "mail"] + for app, original in zip(read.apps, stored.apps): + _same(app, original, "module", "declaration", "build_settings", "settings") + assert _files(app.files) == _files(original.files) + assert composed[-1][0] == read.id + + +def test_a_shipped_base_and_driver_stay_named_by_their_installed_ids(tmp_path, composed): + task = _stored(tmp_path / "desk", DESKTOP) + + imported = import_task(task, tmp_path / "out") + + assert sorted(entity.folder for entity in imported.written) == ["envs/desktop", "envs/office"] + assert {(floating.where, floating.id) for floating in imported.floating} >= { + ("envs/desktop", UBUNTU), ("envs/desktop", REFERENCE)} + toml = (tmp_path / "out" / "envs" / "desktop" / "env.toml").read_text() + assert f'computer = "{UBUNTU}"' in toml and f'driver = "{REFERENCE}"' in toml + read = _written_back(tmp_path / "out") + assert (read.computer.id, read.driver.id) == (UBUNTU, REFERENCE) + assert (read.computer_settings, read.display) == ({"timezone": "Europe/Paris"}, {"width": 1600, "height": 900}) + + +def test_a_computer_artifact_reads_back_with_its_forms_and_settings(tmp_path, composed): + task = _stored(tmp_path / "desk", OWN, OWN_FILES) + stored = ComputerArtifact.get(task.rsplit("/", 1)[0] + "/base") + + import_task(task, tmp_path / "out") + + toml = (tmp_path / "out" / "artifacts" / "base" / "artifact.toml").read_text() + assert "validated_images" not in toml + assert sorted(path.relative_to(tmp_path / "out" / "artifacts" / "base").as_posix() + for path in (tmp_path / "out" / "artifacts" / "base").rglob("*") if path.is_file()) == [ + "artifact.toml", "container/Dockerfile", "container/start"] + read = _written_back(tmp_path / "out").computer + assert read.model_dump(include={"arch", "os", "session", "config"}) == stored.model_dump( + include={"arch", "os", "session", "config"})