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
9 changes: 7 additions & 2 deletions src/agent_env/computers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>`, 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.

Expand Down
8 changes: 5 additions & 3 deletions src/agent_env/computers/disk/omarchy/agentenv-seed
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
Expand Down
30 changes: 26 additions & 4 deletions src/agent_env/env/envs/computer/utils/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)),
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
environment, protocol))
values = env.computer_values()
settings = {name: values.get(name, setting.default) for name, setting in base.config.items()
if setting.phase == "build"}
Expand Down Expand Up @@ -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``
Expand Down
31 changes: 25 additions & 6 deletions src/agent_env/env/envs/computer/utils/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand All @@ -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 "
Expand Down Expand Up @@ -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
Expand Down
49 changes: 36 additions & 13 deletions src/agent_env/env/envs/computer/utils/omarchy_disk.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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``).
Expand Down Expand Up @@ -64,14 +70,18 @@
@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
``<file>:<Class>`` its own server serves, its install steps by name and scope, and the platform they're run for."""

folder: str
prefix: PurePosixPath
install: Install
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:
Expand Down Expand Up @@ -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))}/']
Expand All @@ -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"
Expand Down
Loading
Loading