diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index 14fa43808..38125d0bb 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -215,10 +215,15 @@ venv on the system Python, its run scripts), and its services become systemd use start with the session, and again with it, through `with-seed`. The composed disk has no ssh: only a compose logs in, and it boots the base. At every boot, before SDDM starts the session, `agentenv-seed.service` reads the deploy's environment from the NoCloud seed with the Linux disk's own `seed-env` and -writes it for `with-seed`, root's and the session user's alone, the seed's trust files first; it sets the hostname and +writes it for `with-seed`, root's and the session user's alone, the seed's trust files first; it makes the folder an +app's server marks its launches in (`/run/agentenv/apps`) the session user's; it sets the hostname and the computer's `timezone` (UTC by default; a deploy is refused one that isn't an IANA zone, and one the disk doesn't have is written as the refusal every service then names), names `host.docker.internal` at QEMU's address for the launcher, and writes the display's mode for Hyprland -from the computer's `resolution`. Gate 3 probes it at full. An Omarchy computer has no apps for now: gate 1 refuses one. When +from the computer's `resolution`. Gate 3 probes it at full. Each app is a layer of its own beside the driver, under +`/opt/agentenv/apps/`, installed as its `[install."linux/omarchy"]` says: its venv with the protocol's `app` +extra, its own `.agentenv/` folder the session user's alone, its run scripts and install steps as their scopes say, +and its server and services as user units beside the driver's. Gate 1 refuses an app asking for `shm_mb`, and a +driver or app whose Linux install names apt packages, which Omarchy has no apt for. When Hyprland crashes, its watchdog brings it back in safe mode, without cua-driver's plugin, and cua-driver reports the computer failed for good. diff --git a/src/agent_env/computers/disk/omarchy/agentenv-seed b/src/agent_env/computers/disk/omarchy/agentenv-seed index 77404596e..4f009ae08 100755 --- a/src/agent_env/computers/disk/omarchy/agentenv-seed +++ b/src/agent_env/computers/disk/omarchy/agentenv-seed @@ -2,9 +2,10 @@ # agentenv-seed: run each boot by agentenv-seed.service, before SDDM starts the session. It reads the launcher's NoCloud # seed (CIDATA, as agentenv_protocol.computer.seed defines it) as the Linux disk's init does, with the same seed-env: # the deploy's environment written for with-seed, which each of the computer's services starts through, the seed's trust -# files before it; the hostname; host.docker.internal at QEMU's address for the launcher; the time zone; and the -# display's mode, for the session's Hyprland. A seed this disk can't read, or a time zone it doesn't have, is written as -# its refusal, so every service then stops naming it. +# files before it; the folder an app's server marks its launches in, the session user's, as /run starts each boot empty; +# the hostname; host.docker.internal at QEMU's address for the launcher; the time zone; and the display's mode, for the +# session's Hyprland. A seed this disk can't read, or a time zone it doesn't have, is written as its refusal, so every +# service then stops naming it. set -euo pipefail . /opt/agentenv/seed-env user=$(cat /opt/agentenv/session-user) @@ -38,6 +39,7 @@ else exports="$exports"$'\n'"refused='its time zone, ${timezone//\'/}, is not in /usr/share/zoneinfo'; false" fi install -d -m 750 -g "$user" /run/agentenv +install -d -m 700 -o "$user" -g "$user" /run/agentenv/apps install -m 640 -g "$user" /dev/null /run/agentenv/environment printf '%s\n' "$exports" > /run/agentenv/environment hostname "${hostname:-computer}" diff --git a/src/agent_env/env/envs/computer/utils/composer.py b/src/agent_env/env/envs/computer/utils/composer.py index 1819d93d4..309f5521c 100644 --- a/src/agent_env/env/envs/computer/utils/composer.py +++ b/src/agent_env/env/envs/computer/utils/composer.py @@ -508,8 +508,9 @@ def _composed_omarchy_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com provider: SandboxProvider, progress: _Progress) -> dict: """The Omarchy disk of ``arch``: one of its key the env's image store holds, reused; else composed with this machine's Docker and KVM (``omarchy_disk``), on its base, this machine's, the store's or built from Omarchy's own - image, the driver installed over ssh as a layer on it, packed and pushed to the store under its key, as Linux disks - are. Then probed at full (gate 3). All of it holds a lock of its key, as a Windows disk's compose does.""" + image, the driver and each app installed over ssh as a layer on it, packed and pushed to the store under its key, + as Linux disks are. Then probed at full (gate 3). All of it holds a lock of its key, as a Windows disk's compose + does.""" pair = f"disk/{arch}" with (_attempting(env, "disk", arch) as attempt, tempfile.TemporaryDirectory(prefix="agentenv-compose-") as work): @@ -523,10 +524,20 @@ def _composed_omarchy_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com protocol = _stage_protocol(context) layer, name = _stage_driver(context, driver, base, {}), gate.folder_name(driver.id, "driver") prefix = omarchy_disk.DRIVERS / name - parts = [omarchy_disk.Part(layer.folder, prefix, layer.install, { + parts = [_omarchy_part(layer, prefix, { program: omarchy_disk.argv(command, prefix) for program, command in _services(f"driver-{name}", layer.install).items()}, {"MCP_PORT": driver.port}, - (f"{omarchy_disk.BUILD}/context/protocol",) if protocol else ())] + protocol)] + for app in env.composed_apps(): + staged = _stage_app(context, app, base, {}) + prefix = PurePosixPath(staged.prefix) + server = [str(prefix / "venv" / "bin" / "python"), "-m", "agentenv_protocol.computer.serve_app", + staged.served] + commands = [server, *(omarchy_disk.argv(service.command, prefix) for service in staged.install.services)] + environment = {"ENVIRONMENT_NAME": app.environment_name, + **({PLATFORM_ENV: staged.platform} if staged.platform else {})} + parts.append(_omarchy_part(staged, prefix, dict(zip(gate.programs(app, platform_of(base.os)), commands)), + environment, protocol)) values = env.computer_values() settings = {name: values.get(name, setting.default) for name, setting in base.config.items() if setting.phase == "build"} @@ -572,6 +583,17 @@ def _windows_part(layer: _Layer, prefix: PureWindowsPath, services: dict[str, li layer.served, tuple(step for step, _ in layer.steps)) +def _omarchy_part(layer: _Layer, prefix: PurePosixPath, services: dict[str, list[str]], environment: dict[str, Any], + protocol: bool) -> omarchy_disk.Part: + """``layer`` as an Omarchy disk installs it under ``prefix``: its ``services``' argv by name, with ``environment``, + an app's own server, install steps and platform, and a venv's protocol as on Windows (``_windows_part``).""" + extra = "[app]" if layer.app else "" + spec = (f"{omarchy_disk.BUILD}/context/protocol{extra}" if protocol else + f"{_PROTOCOL}{extra}=={importlib.metadata.version(_PROTOCOL)}" if layer.app else None) + return omarchy_disk.Part(layer.folder, prefix, layer.install, services, environment, (spec,) if spec else (), + layer.served, layer.steps, layer.platform) + + def _probed(env: ComputerEnv, driver: ComputerDriverEnv, form: str, arch: str, result: dict, sandbox_provider: SandboxProvider, progress: _Progress) -> dict: """Gate 3: the build of ``result`` booted on the first link of ``sandbox_provider`` that runs ``arch``'s ``form`` diff --git a/src/agent_env/env/envs/computer/utils/gate.py b/src/agent_env/env/envs/computer/utils/gate.py index b56882daf..bd63a596b 100644 --- a/src/agent_env/env/envs/computer/utils/gate.py +++ b/src/agent_env/env/envs/computer/utils/gate.py @@ -18,7 +18,7 @@ from agent_env.artifact.artifacts.computer import FORMS, ComputerArtifact, SettingValue from agent_env.config.loader import ENV_REF_PREFIX, SECRET_REF_PREFIX, is_bare_reference from agent_env.providers.sandbox_providers.sandbox import NetworkPolicy -from agent_env.env.envs.computer.utils.install import for_os, platform_of +from agent_env.env.envs.computer.utils.install import Install, for_os, platform_of from agent_env.store.image_store.oci_registry_credentials import names_registry if TYPE_CHECKING: @@ -69,9 +69,10 @@ def arches(base: ComputerArtifact, driver: ComputerDriverEnv, apps: Sequence[Com def check_app(base: ComputerArtifact, driver: ComputerDriverEnv, app: ComputerAppEnv, at: str = "app") -> list[str]: """Every way ``app`` doesn't fit a composition of ``base`` and ``driver``, each named from ``at``: a platform it - doesn't support, no architecture in common, no install for the base's OS, a port the driver listens on too, any app - on Omarchy, whose disks compose a driver alone for now, or on Windows an install step for the session user, as a - Windows computer is composed over ssh as its admin.""" + doesn't support, no architecture in common, no install for the base's OS, a port the driver listens on too, on + Omarchy apt's packages (``_apt_on_omarchy``), shared memory, whose size its disk doesn't take yet, or a service as + root, as its systemd runs every service as the session user, or on Windows an install step for the session user, + as a Windows computer is composed over ssh as its admin.""" os = base.os if not _matching(base, app.declaration.supports): named = ", ".join(_shown(platform) for entry in app.declaration.supports for platform in supported(entry)) @@ -84,8 +85,14 @@ def check_app(base: ComputerArtifact, driver: ComputerDriverEnv, app: ComputerAp elif taken := sorted(set(_ports(app, platform_of(os))) & set(_driver_ports(driver, os))): problems.append(f"{at}: {app.id} listens on {', '.join(map(str, taken))}, as {driver.id} does") if platform_of(os) == "linux/omarchy": - problems.append(f"{at}: an Omarchy computer composes its driver alone for now; {app.id} would need its install " - "composed onto Omarchy's disk, which agent-env doesn't do yet") + problems += _apt_on_omarchy(app.install, os, at, app.id) + install = for_os(app.install, os) + if install is not None and install.shm_mb: + problems.append(f"{at}: {app.id} asks for {install.shm_mb} MiB of /dev/shm, which an Omarchy computer " + "doesn't size yet: its /dev/shm is systemd's, sized at boot") + problems += [f"{at}: {app.id}'s service {service.name!r} runs as root, and {base.id}'s systemd runs every " + f"program as {base.session.user}" for service in (install.services if install else []) + if service.scope == "system"] if os.type == "windows" and (session := [step.name for step in app.declaration.install_steps if step.scope == "session" and (step.os is None or os.type in step.os)]): problems.append(f"{at}: {app.id}'s install steps {', '.join(session)} run as the session user, which a Windows " @@ -253,9 +260,21 @@ def _platform(base: ComputerArtifact, driver: ComputerDriverEnv) -> list[str]: init = form.init if form is not None else "launchd" problems += [f"driver: its service {service.name!r} runs as root, and {base.id}'s {init} runs every program as " f"{base.session.user}" for service in install.services if service.scope == "system"] + problems += _apt_on_omarchy(driver.install, os, "driver", driver.id) return problems +def _apt_on_omarchy(installs: dict[str, Install], os: Any, at: str, who: str) -> list[str]: + """Why ``who``'s install, of ``installs``, doesn't fit an Omarchy computer: it's its Linux one, as it declares none + for linux/omarchy, and names apt's packages or sources, which Omarchy has no apt for, so they'd be left out.""" + install = for_os(installs, os) + if (platform_of(os) != "linux/omarchy" or "linux/omarchy" in installs or install is None + or (install.packages is None and not install.sources)): + return [] + return [f"{at}: {who}'s Linux install names apt packages, which an Omarchy computer has no apt for; declare its " + "install for linux/omarchy"] + + def _matching(base: ComputerArtifact, supports: list) -> list[Platform]: """The platforms of ``supports``, as a driver or app declares them, that the base's OS is.""" os = base.os diff --git a/src/agent_env/env/envs/computer/utils/omarchy_disk.py b/src/agent_env/env/envs/computer/utils/omarchy_disk.py index f43a67535..662eb85e9 100644 --- a/src/agent_env/env/envs/computer/utils/omarchy_disk.py +++ b/src/agent_env/env/envs/computer/utils/omarchy_disk.py @@ -1,10 +1,10 @@ """Omarchy computers' disks, thin disks (``thin_disk``) made with this machine's Docker and KVM. A base is Omarchy's own VM image, fetched once into this machine's cache from its artifact's source and checked by sha256, booted once by the QEMU launcher as a linux-desktop guest, with root's ssh key passed as a systemd credential, made a computer's base by -the recipe of ``computers/disk/omarchy`` run as root over ssh, then packed whole as a thin disk's base. A composed disk's -parts are installed by a bash script, ``provision.sh``, run as root: each layer's files, downloads, venv and run scripts, -and its services as systemd user units of the graphical session, which start through ``with-seed``, so each boot's -deploy environment is theirs.""" +the recipe of ``computers/disk/omarchy`` run as root over ssh, then packed whole as a thin disk's base. A composed +disk's parts are installed by a bash script, ``provision.sh``, run as root: each layer's files, downloads, venv and run +scripts, an app's own folder, the session user's alone, and its install steps, and each layer's services as systemd user +units of the graphical session, which start through ``with-seed``, so each boot's deploy environment is theirs.""" from __future__ import annotations @@ -20,6 +20,9 @@ from pathlib import Path, PurePosixPath from typing import Any +from agentenv_protocol.computer.app import SOCKET +from agentenv_protocol.computer.platform import PLATFORM_ENV + from agent_env.artifact.artifacts.computer import DiskSource from agent_env.config.paths import cache_root from agent_env.env.envs.computer.utils import thin_disk @@ -35,6 +38,9 @@ #: Where agent-env's own files are on an Omarchy computer, each layer's folder under them, and its services' units. ROOT = PurePosixPath("/opt/agentenv") DRIVERS = ROOT / "drivers" +#: Where an app's folder goes, where the driver finds the apps composed onto its computer +#: (``agentenv_protocol.computer.apps``). +APPS = ROOT / "apps" BUILD = ROOT / "build" UNITS = PurePosixPath("/etc/systemd/user") #: What starts every service, with the deploy's environment (``computers/disk/omarchy/with-seed``). @@ -64,7 +70,8 @@ @dataclass(frozen=True) class Part: """A layer as an Omarchy disk installs it: its folder under ``context/``, its prefix, its install, its services' - argv by name, the environment they're given, and what its venv installs beside its requirements.""" + argv by name, the environment they're given, and what its venv installs beside its requirements; for an app, the + ``:`` its own server serves, its install steps by name and scope, and the platform they're run for.""" folder: str prefix: PurePosixPath @@ -72,6 +79,9 @@ class Part: services: dict[str, list[str]] environment: dict[str, Any] wants: tuple[str, ...] = () + served: str | None = None + steps: tuple[tuple[str, str], ...] = () + platform: str | None = None def source_path(sha256: str) -> Path: @@ -172,9 +182,9 @@ def write(build: Path, parts: list[Part], *, user: str, build_settings: dict[str def _installed_part(part: Part) -> list[str]: - """The lines that install ``part``: its files, downloads, venv and run scripts, each as root or, for ``session``, - as the session user in their home, and its services as user units of the graphical session, each enabled for every - user.""" + """The lines that install ``part``: its files, downloads, venv, an app's own folder, where its server listens, the + session user's alone, then its run scripts and install steps, each as root or, for ``session``, as the session user + in their home, and its services as user units of the graphical session, each enabled for every user.""" prefix, install = part.prefix, part.install lines = [f"install -d {shlex.quote(str(prefix))}", f'cp -a "$build/context/{part.folder}/." {shlex.quote(str(prefix))}/'] @@ -189,25 +199,38 @@ def _installed_part(part: Part) -> list[str]: lines += [f"python3 -m venv {shlex.quote(str(venv))}", f"{venv}/bin/pip install -q --disable-pip-version-check {' '.join(map(shlex.quote, wants))}", f"{venv}/bin/pip list --format=json > {shlex.quote(str(prefix / 'installed.json'))}"] + if part.served is not None: + own = shlex.quote(str(prefix / SOCKET.parent)) + lines.append(f'install -d {own} && chown "$user": {own} && chmod 700 {own}') for run in install.run or []: - as_user = 'runuser -u "$user" -- env HOME="$home" ' if run.scope == "session" else "" - lines.append(f"(cd {shlex.quote(str(prefix))} && {as_user}PREFIX={shlex.quote(str(prefix))} " + lines.append(f"(cd {shlex.quote(str(prefix))} && {_as(run.scope)}PREFIX={shlex.quote(str(prefix))} " f"bash {shlex.quote(str(prefix / run.script))})") + for step, scope in part.steps: + platform = f"{PLATFORM_ENV}={shlex.quote(part.platform)} " if part.platform else "" + lines.append(f"(cd {shlex.quote(str(prefix))} && {_as(scope)}{platform}{prefix / 'venv' / 'bin' / 'python'} -m " + f"agentenv_protocol.computer.run_install_step {shlex.quote(part.served)} {shlex.quote(step)})") for name, argv in part.services.items(): lines += [_written(UNITS / f"agentenv-{name}.service", unit(name, prefix, argv, part.environment)), f"systemctl --global enable --quiet agentenv-{name}.service"] return lines +def _as(scope: str) -> str: + """What a command starts with to run as ``scope`` says: as root, provision's own user, or for ``session``, as the + session user in their home.""" + return 'runuser -u "$user" -- env HOME="$home" ' if scope == "session" else "" + + def unit(name: str, prefix: PurePosixPath, argv: list[str], environment: dict[str, Any]) -> str: """The systemd user unit of the service ``name``: started with the graphical session, which gives it Hyprland's instance and Wayland's display, and again with it, as Hyprland restarts under a new instance signature; through - ``with-seed``, in its ``prefix``, with ``environment``; again whenever it exits.""" + ``with-seed``, in its ``prefix``, with ``environment`` set after the deploy's, so a layer's own variables win, as on + every OS; again whenever it exits.""" + own = ["/usr/bin/env", *(f"{key}={value}" for key, value in environment.items())] if environment else [] lines = ["[Unit]", f"Description=agent-env: {name}", "PartOf=graphical-session.target", "After=graphical-session.target", "ConditionEnvironment=WAYLAND_DISPLAY", "", "[Service]", f"WorkingDirectory={prefix}", - *(f"Environment={_unit_word(f'{key}={value}')}" for key, value in environment.items()), - "ExecStart=" + " ".join(_unit_word(word) for word in (str(WITH_SEED), *argv)), + "ExecStart=" + " ".join(_unit_word(word) for word in (str(WITH_SEED), *own, *argv)), "Restart=always", "RestartSec=1", "", "[Install]", "WantedBy=graphical-session.target"] return "\n".join(lines) + "\n" diff --git a/tst/integration/computer/omarchy/artifacts/check_notes/verify.py b/tst/integration/computer/omarchy/artifacts/check_notes/verify.py new file mode 100644 index 000000000..7c9a142c1 --- /dev/null +++ b/tst/integration/computer/omarchy/artifacts/check_notes/verify.py @@ -0,0 +1,51 @@ +"""The notes app composed onto the deployed Omarchy computer: ready once its server says its window is shown and its +@ready_check passes; and, read inside the computer as the session user, on the app's own venv: what its service wrote on +the deploy's settings, its secret resolved at boot; its window, listed by its title; what its install left: the +download, its run script's output, run as root in its folder, its root install step's file on the build setting its env +fixes, and its session install step's, written as the session user in their home; and its own folder, the session +user's alone.""" +import json + +from agentenv_protocol.computer.card import COMPUTER_READY +from agentenv_protocol.computer.conformance.client import Driver + +PYTHON = "/opt/agentenv/apps/notes/venv/bin/python" +FACTS = r""" +import asyncio, json, pwd, stat, time +from pathlib import Path +from agentenv_protocol.computer import session +from agentenv_protocol.computer.app import SOCKET + +prefix = Path("/opt/agentenv/apps/notes") +written = Path.home() / ".notes.json" +deadline = time.monotonic() + 60 +while not written.exists() and time.monotonic() < deadline: + time.sleep(0.5) +own, home = (prefix / SOCKET).parent.stat(), Path.home() / ".notes-installed" +print(json.dumps({ + "notes": json.loads(written.read_text()), + "windows": [window["title"] for window in asyncio.run(session.windows()) if window["title"] == "Notes"], + "run": (prefix / "run.txt").read_text().strip(), "installed": (prefix / "installed.txt").read_text(), + "home": [home.read_text(), pwd.getpwuid(home.stat().st_uid).pw_name], + "license": (prefix / "LICENSE.txt").read_text().startswith("A. HISTORY OF THE SOFTWARE"), + "own": [pwd.getpwuid(own.st_uid).pw_name, oct(stat.S_IMODE(own.st_mode))]})) +""" +EXPECTED = { + "notes": {"signature": "-- Ada", "token": "t0ken", "edition": "pro"}, "windows": ["Notes"], + "run": "root in /opt/agentenv/apps/notes", "installed": "pro", "home": ["omarchy", "omarchy"], "license": True, + "own": ["omarchy", "0o700"]} + + +async def verify(mcp_url, env_url): + driver = await Driver.connect(env_url, 120) + status = await driver.invoke(COMPUTER_READY, "status") + app = [check for check in status.get("checks", []) if check.get("name") == "app notes"] + results = [{"id": "ready", "result": bool(status.get("ready")) and [c.get("ok") for c in app] == [True], + "description": json.dumps(app)}] + out = await driver.run(PYTHON, "-c", FACTS, timeout_s=120) + try: + facts = json.loads(out.get("stdout") or "") + except ValueError: + return results + [{"id": "facts", "result": False, "description": json.dumps(out)[-2000:]}] + return results + [{"id": name, "result": facts.get(name) == expected, + "description": json.dumps(facts.get(name))} for name, expected in EXPECTED.items()] diff --git a/tst/integration/computer/omarchy/envs/notes-desk/env.toml b/tst/integration/computer/omarchy/envs/notes-desk/env.toml new file mode 100644 index 000000000..d42291ea2 --- /dev/null +++ b/tst/integration/computer/omarchy/envs/notes-desk/env.toml @@ -0,0 +1,4 @@ +type = "computer" +computer = "omarchy-4.5" +driver = "cua-driver" +apps = ["notes"] diff --git a/tst/integration/computer/omarchy/envs/notes/app.py b/tst/integration/computer/omarchy/envs/notes/app.py new file mode 100644 index 000000000..3cc6875ac --- /dev/null +++ b/tst/integration/computer/omarchy/envs/notes/app.py @@ -0,0 +1,45 @@ +"""A notes app for the Omarchy computer tests, composed onto a desktop as notes-desk: its service writes the settings a +deploy gave it to ~/.notes.json, and its own server launches its window once it serves; it's ready once both have +happened. Its install, Omarchy's alone, takes a download checked by sha256, a run script and an install step run as +root in its folder, the step on the build setting its env.toml fixes, and an install step run as the session user in +their home. Its window is GTK 3 on the system Python, which Omarchy ships with PyGObject, matched by its title.""" +import os +import pwd +from pathlib import Path + +from pydantic import BaseModel, SecretStr + +from agentenv_protocol.computer import (ComputerAppEnvironment, Download, Launch, Ready, RunScript, Service, + computer_app, install_step, linux, ready_check) + +LICENSE = "https://raw.githubusercontent.com/python/cpython/v3.12.0/LICENSE" + + +@computer_app(supports=[{"os": {"type": "linux", "distribution": ["omarchy"], "display": ["wayland"]}, + "arch": ["amd64"]}]) +@linux(distribution="omarchy", + downloads=[Download(url=LICENSE, to="LICENSE.txt", + sha256="3b2f81fe21d181c499c59a256c8e1968455d6689d269aa85373bfb6af41da3bf")], + run=[RunScript(script="setup.sh")], + services=[Service(name="notes", command="venv/bin/python serve.py")], + launch=Launch(at="boot", command=["/usr/bin/python3", "window.py"], window={"title": "Notes"}, + ready=Ready(window=True))) +class Notes(ComputerAppEnvironment): + class Settings(BaseModel): + signature: str = "-- notes" + token: SecretStr = SecretStr("") + + class BuildSettings(BaseModel): + edition: str = "home" + + @install_step() + def stamp(self) -> None: + Path("installed.txt").write_text(self.build_settings.edition) + + @install_step(scope="session") + def home(self) -> None: + (Path.home() / ".notes-installed").write_text(pwd.getpwuid(os.getuid()).pw_name) + + @ready_check() + def written(self) -> bool: + return (Path.home() / ".notes.json").exists() diff --git a/tst/integration/computer/omarchy/envs/notes/env.toml b/tst/integration/computer/omarchy/envs/notes/env.toml new file mode 100644 index 000000000..5fb1340dd --- /dev/null +++ b/tst/integration/computer/omarchy/envs/notes/env.toml @@ -0,0 +1,3 @@ +type = "computer_app" +module = "app.py:Notes" +build_settings = { edition = "pro" } diff --git a/tst/integration/computer/omarchy/envs/notes/serve.py b/tst/integration/computer/omarchy/envs/notes/serve.py new file mode 100644 index 000000000..de7e3d931 --- /dev/null +++ b/tst/integration/computer/omarchy/envs/notes/serve.py @@ -0,0 +1,12 @@ +import json +import time +from pathlib import Path + +from app import Notes + +notes = Notes() +Path.home().joinpath(".notes.json").write_text(json.dumps( + {"signature": notes.settings.signature, "token": notes.settings.token.get_secret_value(), + "edition": notes.build_settings.edition})) +while True: + time.sleep(3600) diff --git a/tst/integration/computer/omarchy/envs/notes/setup.sh b/tst/integration/computer/omarchy/envs/notes/setup.sh new file mode 100644 index 000000000..191a5db99 --- /dev/null +++ b/tst/integration/computer/omarchy/envs/notes/setup.sh @@ -0,0 +1,2 @@ +# Run as root once the app's files are in: who ran it, in the folder it was given. +echo "$(id -un) in $PREFIX" > run.txt diff --git a/tst/integration/computer/omarchy/envs/notes/window.py b/tst/integration/computer/omarchy/envs/notes/window.py new file mode 100644 index 000000000..0ef630374 --- /dev/null +++ b/tst/integration/computer/omarchy/envs/notes/window.py @@ -0,0 +1,11 @@ +"""The notes app's window, titled Notes: GTK 3 on Omarchy's own Python.""" +import gi + +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk + +window = Gtk.Window(title="Notes") +window.add(Gtk.Label(label="Notes")) +window.connect("destroy", Gtk.main_quit) +window.show_all() +Gtk.main() diff --git a/tst/integration/computer/omarchy/tasks/notes.json b/tst/integration/computer/omarchy/tasks/notes.json new file mode 100644 index 000000000..74b5afcab --- /dev/null +++ b/tst/integration/computer/omarchy/tasks/notes.json @@ -0,0 +1,9 @@ +[ + {"id": "env", "type": "deploy_env", "env_id": "notes-desk", + "env_params": {"app_settings": {"notes": {"signature": "-- Ada", "token": "env:NOTES_TOKEN"}}}}, + {"id": "before", "type": "env_outcome_verifier", "env_id": "notes-desk", "file_artifact_id": "check_notes", + "verifier_id": "before"}, + {"id": "reset", "type": "reset_env", "env_id": "notes-desk", "depends_on": ["before"]}, + {"id": "after", "type": "env_outcome_verifier", "env_id": "notes-desk", "file_artifact_id": "check_notes", + "verifier_id": "after", "depends_on": ["reset"]} +] diff --git a/tst/integration/computer/omarchy_test.py b/tst/integration/computer/omarchy_test.py index 10c8aba68..dde882a0c 100644 --- a/tst/integration/computer/omarchy_test.py +++ b/tst/integration/computer/omarchy_test.py @@ -1,6 +1,8 @@ """The Omarchy test bundle through ``agent-env run``, on local Docker with KVM: its desktop, cua-driver on omarchy-4.5, composed on this machine, its base built from Omarchy's image in this machine's cache unless one is held here, passes -the conformance probe at full. Needs Docker, KVM and the image.""" +the conformance probe at full. And the bundle's notes app, composed onto it as a layer, runs on its deploy's settings, +its window up and its install done as its Omarchy install says, its own folder the session user's alone, before and +after a reset. Needs Docker, KVM and the image.""" from pathlib import Path @@ -20,3 +22,12 @@ def test_the_omarchy_desktop_passes_the_probe_at_full(state): assert ran.exit_code == 0, ran.output assert "passed (probe: 1)" in ran.output, ran.output + + +def test_the_notes_app_composed_onto_omarchy_runs_as_installed_before_and_after_a_reset(state, monkeypatch): + monkeypatch.setenv("NOTES_TOKEN", "t0ken") + + ran = CliRunner().invoke(cli, ["run", str(BUNDLE), "--task", "notes"]) + + assert ran.exit_code == 0, ran.output + assert "passed (before: 1, after: 1)" in ran.output, ran.output diff --git a/tst/unit/env/envs/test_computer_omarchy.py b/tst/unit/env/envs/test_computer_omarchy.py index 86d915507..0235f0515 100644 --- a/tst/unit/env/envs/test_computer_omarchy.py +++ b/tst/unit/env/envs/test_computer_omarchy.py @@ -1,9 +1,9 @@ """An Omarchy computer env, composed on this machine's Docker with KVM: its base built once from Omarchy's own image in this machine's cache, then the driver installed over ssh by a bash provision script as its ``[install."linux/omarchy"]`` says, its services systemd user units of the graphical session started through with-seed, and packed as a thin layer on -the base under a tag of its key. An Omarchy base is a disk alone, built from its source, started by systemd; its apps -are refused for now. The base's build boots Omarchy's image as a linux-desktop guest, root's key a systemd credential, -and runs the recipe, which refuses an image its artifact mislabels.""" +the base under a tag of its key, with each app a layer of its own beside it. An Omarchy base is a disk alone, built from +its source, started by systemd. The base's build boots Omarchy's image as a linux-desktop guest, root's key a systemd +credential, and runs the recipe, which refuses an image its artifact mislabels.""" import base64 import hashlib @@ -16,12 +16,13 @@ import pytest from pydantic import ValidationError -from agentenv_protocol.computer import DriverDeclaration +from agentenv_protocol.computer import AppDeclaration, DriverDeclaration from agentenv_protocol.computer.app import Download, RunScript from agent_env.artifact.artifacts.computer import ComputerArtifact, DiskForm, DiskSource from agent_env.artifact.artifacts.file_artifact_universe import FileArtifactUniverse from agent_env.env.env import Env +from agent_env.env.envs.computer.app_env import ComputerAppEnv from agent_env.env.envs.computer.driver_env import ComputerDriverEnv from agent_env.env.envs.computer.env import ComputerEnv from agent_env.env.envs.computer.utils import boot, composer, gate, omarchy_disk, thin_disk @@ -129,10 +130,32 @@ def _driver(folder: Path, services: list[Service] | None = None) -> ComputerDriv def _put(driver: ComputerDriverEnv, link: SandboxProvider, base: ComputerArtifact | None = None, - **settings) -> ComputerEnv: + apps: list[ComputerAppEnv] | None = None, **settings) -> ComputerEnv: return ComputerEnv.put(id="@local/~/kit/desktop", computer=base or _base(), driver=driver, environment_name="desktop", composing=Composing(sandbox_provider=link), - computer_settings=settings or None, apps=[]) + computer_settings=settings or None, apps=apps or []) + + +def _notes(base: ComputerArtifact, driver: ComputerDriverEnv, folder: Path, key: str = "linux/omarchy", + install: dict | None = None) -> ComputerAppEnv: + """A notes app on Omarchy, its install declared for ``key``: a sync service in the venv its own server gets, a run + script, an install step for root and one for the session user, and the edition its env fixes.""" + folder.mkdir(parents=True, exist_ok=True) + for name, text in (("app.py", "class Notes: ...\n"), ("setup.sh", "id -un > run.txt\n")): + (folder / name).write_text(text) + files = FileArtifactUniverse.put_bundled("@local/~/kit/notes__files", files={ + name: folder / name for name in ("app.py", "setup.sh")}) + declaration = AppDeclaration.model_validate({ + "supports": OMARCHY_SUPPORTS, "permissions": [], "settings": {"type": "object", "properties": {}}, + "skill": None, "data": [], "ready_checks": [], + "build_settings": {"type": "object", "properties": {"edition": {"type": "string", "default": "home"}}}, + "install_steps": [{"name": "stamp", "scope": "system", "os": None}, + {"name": "home", "scope": "session", "os": None}], + "os": {key: {"install": install or {"run": [{"script": "setup.sh"}], "services": [ + {"name": "sync", "command": "venv/bin/python sync.py"}]}}}}) + return ComputerAppEnv.put(id="@local/~/kit/notes", environment_name="notes", module="app.py:Notes", files=files, + declaration=declaration, computer=base, driver=driver, compose=False, + build_settings={"edition": "pro"}) def test_an_omarchy_env_composes_a_thin_disk_on_its_base_built_from_omarchys_image_the_driver_as_user_units( @@ -164,7 +187,8 @@ def test_an_omarchy_env_composes_a_thin_disk_on_its_base_built_from_omarchys_ima unit = omarchy_disk.unit("driver-desk-daemon", PurePosixPath(prefix), [f"{prefix}/venv/bin/python", "driver.py"], {"MCP_PORT": 18765}) assert f"printf %s {shlex.quote(unit)} > /etc/systemd/user/agentenv-driver-desk-daemon.service" in script - assert f"ExecStart=/opt/agentenv/with-seed {prefix}/venv/bin/python driver.py" in unit.splitlines() + assert f"ExecStart=/opt/agentenv/with-seed /usr/bin/env MCP_PORT=18765 {prefix}/venv/bin/python driver.py" in ( + unit.splitlines()) assert {"provision.sh", "context/driver/driver.py", "context/driver/install.sh"} <= set(files) computer = env.computer_artifact() assert computer.os.distribution == "omarchy" and computer.forms.disk.init == "systemd" @@ -187,15 +211,69 @@ def test_a_machine_that_boots_no_linux_disk_composes_no_omarchy_disk(docker, tmp assert docker.builds == [] and docker.composed == [] -def test_an_omarchy_computer_refuses_apps_for_now(local_stores, cli_routing, tmp_path): - app = SimpleNamespace(id="@local/~/kit/notes", declaration=SimpleNamespace(supports=OMARCHY_SUPPORTS, - install_steps=[]), - install={"linux/omarchy": Install(services=[Service(name="sync", command="sync")])}) - - problems = gate.check_app(_base(), _driver(tmp_path / "desk"), app) - - assert problems == ["app: an Omarchy computer composes its driver alone for now; @local/~/kit/notes would need its " - "install composed onto Omarchy's disk, which agent-env doesn't do yet"] +def test_an_app_is_a_layer_of_its_own_its_server_on_its_venv_its_folder_the_session_users_its_steps_as_scoped( + docker, tmp_path): + base = _base() + driver = _driver(tmp_path / "desk") + env = _put(driver, FakeLinux(), base=base, apps=[_notes(base, driver, tmp_path / "notes")]) + + assert env.metadata["composed"]["disk"]["amd64"]["status"] == "ready" + ((_, _, script, files, _),) = docker.composed + prefix, lines = PurePosixPath("/opt/agentenv/apps/notes"), script.splitlines() + python = prefix / "venv" / "bin" / "python" + installed = lines[lines.index("# apps/notes") + 1:] + assert installed[:8] == [ + f"install -d {prefix}", f'cp -a "$build/context/apps/notes/." {prefix}/', f"python3 -m venv {prefix}/venv", + f"{prefix}/venv/bin/pip install -q --disable-pip-version-check '/opt/agentenv/build/context/protocol[app]'", + f"{prefix}/venv/bin/pip list --format=json > {prefix}/installed.json", + f'install -d {prefix}/.agentenv && chown "$user": {prefix}/.agentenv && chmod 700 {prefix}/.agentenv', + f"(cd {prefix} && PREFIX={prefix} bash {prefix}/setup.sh)", + f"(cd {prefix} && AGENTENV_PLATFORM=linux/omarchy {python} -m agentenv_protocol.computer.run_install_step " + "app.py:Notes stamp)"] + assert installed[8] == (f'(cd {prefix} && runuser -u "$user" -- env HOME="$home" AGENTENV_PLATFORM=linux/omarchy ' + f"{python} -m agentenv_protocol.computer.run_install_step app.py:Notes home)") + environment = {"ENVIRONMENT_NAME": "notes", "AGENTENV_PLATFORM": "linux/omarchy"} + for name, argv in (("app-notes", [str(python), "-m", "agentenv_protocol.computer.serve_app", "app.py:Notes"]), + ("app-notes-sync", [str(python), "sync.py"])): + unit = omarchy_disk.unit(name, prefix, argv, environment) + assert f"printf %s {shlex.quote(unit)} > /etc/systemd/user/agentenv-{name}.service" in script + assert f"systemctl --global enable --quiet agentenv-{name}.service" in installed + driver_lines = lines[lines.index("# driver"):lines.index("# apps/notes")] + assert not any(".agentenv" in line or "run_install_step" in line for line in driver_lines) + assert {"context/apps/notes/app.py", "context/apps/notes/.agentenv/build_settings.json"} <= set(files) + + +def test_an_app_asking_for_shared_memory_is_refused_on_omarchy(local_stores, cli_routing, tmp_path): + base, driver = _base(), _driver(tmp_path / "desk") + + with pytest.raises(ValueError, match="app: @local/~/kit/notes asks for 512 MiB of /dev/shm, which an Omarchy " + "computer doesn't size yet: its /dev/shm is systemd's, sized at boot"): + _notes(base, driver, tmp_path / "notes", install={"services": [{"name": "sync", "command": "sync"}], + "shm_mb": 512}) + + +def test_an_app_service_that_runs_as_root_is_refused_on_omarchy_as_a_drivers_is(local_stores, cli_routing, tmp_path): + base, driver = _base(), _driver(tmp_path / "desk") + + with pytest.raises(ValueError, match="app: @local/~/kit/notes's service 'sync' runs as root, and " + "@local/~/kit/omarchy-4.5's systemd runs every program as omarchy"): + _notes(base, driver, tmp_path / "notes", install={"services": [ + {"name": "sync", "command": "sync", "scope": "system"}]}) + + +def test_a_linux_install_with_apt_packages_is_refused_on_omarchy_for_an_app_and_a_driver(docker, tmp_path): + base, driver = _base(), _driver(tmp_path / "desk") + apt = {"packages": {"apt": ["htop"]}, "services": [{"name": "sync", "command": "sync"}]} + + with pytest.raises(ValueError, match="app: @local/~/kit/notes's Linux install names apt packages, which an " + "Omarchy computer has no apt for; declare its install for linux/omarchy"): + _notes(base, driver, tmp_path / "notes", key="linux", install=apt) + linux = ComputerDriverEnv.put( + id="@local/~/kit/linux-desk", module="driver.py:Desk", files=driver.files, port=18765, + install={"linux": Install(packages={"apt": ["htop"]}, services=[Service(name="driver", command=SERVE)])}, + declaration=driver.declaration) + with pytest.raises(ValueError, match="driver: @local/~/kit/linux-desk's Linux install names apt packages"): + _put(Env.get(linux.id, linux.version), FakeLinux(), base=base) @pytest.mark.parametrize("changes, error", [ @@ -281,15 +359,14 @@ def test_an_omarchy_disk_boots_at_the_size_hyprland_wants_and_other_linux_disks_ assert boot.entry_for(boot.DISK_SIZES, "windows") == (4.0, 8192) -def test_a_unit_quotes_what_systemd_would_otherwise_split_or_expand(): +def test_a_unit_quotes_what_systemd_would_otherwise_split_or_expand_and_sets_its_own_variables_after_the_deploys(): text = omarchy_disk.unit("app-desk-sync", PurePosixPath("/opt/agentenv/apps/desk"), ["/opt/agentenv/apps/desk/venv/bin/python", "sync.py", "--title", "100% $HOME's \"desk\""], {"ENVIRONMENT_NAME": "desk", "GREETING": "hello world"}) - assert ('ExecStart=/opt/agentenv/with-seed /opt/agentenv/apps/desk/venv/bin/python sync.py --title ' - '"100%% $$HOME\'s \\"desk\\""') in text.splitlines() - assert 'Environment="GREETING=hello world"' in text.splitlines() - assert "Environment=ENVIRONMENT_NAME=desk" in text.splitlines() + assert ('ExecStart=/opt/agentenv/with-seed /usr/bin/env ENVIRONMENT_NAME=desk "GREETING=hello world" ' + '/opt/agentenv/apps/desk/venv/bin/python sync.py --title "100%% $$HOME\'s \\"desk\\""') in text.splitlines() + assert not any(line.startswith("Environment=") for line in text.splitlines()) assert "WantedBy=graphical-session.target" in text.splitlines()