diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index dc929d354..929c5b051 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -166,8 +166,9 @@ link serves then. Name it as `windows-11`, or by its id, `@local/agentenv-framew A Windows computer env is composed on the same machine (`env/envs/computer/utils/windows_disk.py`): the base booted on an overlay kept in a folder of its own, each layer installed over ssh as `agentadmin` by a PowerShell script composing -writes, as its `[install.windows]` says (its files under `C:\agentenv\drivers\`, its downloads checked by -sha256, its venv on the pinned Python, its PowerShell run scripts, elevated, and its services), then shut down and +writes, as its `[install.windows]` says (its files under `C:\agentenv\drivers\`, or an app's under +`C:\agentenv\apps\`, its downloads checked by sha256, its venv on the pinned Python, its PowerShell run scripts, +elevated, an app's install steps, elevated too, and its services), then shut down and packed onto the base's image, so every computer composed on a base shares one copy of it, and pushed to the env's image store under its key, pinned, as a Linux disk is; a compose of the same inputs on any machine reuses it, and a deploy pulls it. Gate 3 probes it at full. At the session user's sign-in, @@ -177,7 +178,12 @@ media alone, and starts each layer's services in that session, again whenever on with 4 vCPUs and 8 GB, on local Docker with KVM and on `modal_vm`; it has computer-exec and data/add, through the protocol's Windows defaults, so the probe runs its fixture there as on Linux, with Python one-liners for the commands it would give a shell. On `modal_vm` it runs in your own Modal workspace, under the evaluation's terms or a licence of your -own; whether they allow it there is yours to check. A Windows computer composes its driver alone for now, no app. +own; whether they allow it there is yours to check. Composing leaves `C:\agentenv` the admins', as `/opt/agentenv` is +root's on Linux: every user reads and runs what's there, and the session user writes only the services' logs and each +app's `.agentenv` folder. An app's own server runs in the session as its services do, on a loopback port it writes +with a token to that folder, which only the session user, SYSTEM and Administrators reach; it launches the app in a +Job object, which ends whatever the app started. Composing signs in as `agentadmin`, so +an app's install step for the session user can't run on Windows, and gate 1 refuses an app that declares one there. This bundle has no tasks of its own: other bundles pull its computers by alias, and `agent-env run computers` has nothing to run. diff --git a/src/agent_env/env/envs/computer/utils/composer.py b/src/agent_env/env/envs/computer/utils/composer.py index 9241b6141..ae804017e 100644 --- a/src/agent_env/env/envs/computer/utils/composer.py +++ b/src/agent_env/env/envs/computer/utils/composer.py @@ -25,7 +25,7 @@ import urllib.parse from concurrent.futures import Future, ThreadPoolExecutor, as_completed from dataclasses import dataclass, replace -from pathlib import Path, PurePosixPath +from pathlib import Path, PurePosixPath, PureWindowsPath from collections.abc import Callable, Iterator from typing import TYPE_CHECKING, Any, Literal @@ -313,7 +313,7 @@ def _composed_vm_bundle(env: ComputerEnv, base: ComputerArtifact, driver: Comput parts = [_part(layer, _services(f"driver-{gate.folder_name(driver.id, 'driver')}", layer.install), {"MCP_PORT": driver.port}, protocol, False)] parts += [_part(staged, dict(zip(gate.programs(app, base.os.type), [ - f"venv/bin/python -m agentenv_protocol.computer.serve_app {staged.served}", + f"venv/bin/python -m agentenv_protocol.computer.serve_app {shlex.quote(staged.served)}", *(service.command for service in staged.install.services)])), {"ENVIRONMENT_NAME": app.environment_name}, protocol, True) for app, staged in apps] if (lock := _vm_bundle_lock(env, base, link, parts, context, arch, progress)) is not None: @@ -357,9 +357,9 @@ def _composed_windows_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com provider: SandboxProvider, progress: _Progress) -> dict: """The Windows disk of ``arch``: one of its key the env's image store holds, reused; else composed with this machine's Docker and KVM (``windows_disk``), on its base, this machine's, the store's or installed from Microsoft's - media, 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, which keeps two composes of one key on this - machine from breaking each other: the other waits, then reuses it.""" + media, 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, which keeps two composes of one + key on this machine from breaking each other: the other waits, then reuses it.""" pair = f"disk/{arch}" with (_attempting(env, "disk", arch) as attempt, tempfile.TemporaryDirectory(prefix="agentenv-compose-") as work): @@ -367,19 +367,24 @@ def _composed_windows_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com raise builder.Unbuildable(f"no link of {' or '.join(type(link).__name__ for link in provider.links)} boots " f"windows/{arch} disks, so it can't be composed here: compose it on a Linux machine " "with KVM, with the local sandbox provider") - if env.composed_apps(): - raise builder.Unbuildable("a Windows computer composes its driver alone for now, and no app") build = Path(work) / "build" context = build / "context" context.mkdir(parents=True) protocol = _stage_protocol(context) - layer = _stage_driver(context, driver, base, {}) - prefix = windows_disk.DRIVERS / gate.folder_name(driver.id, "driver") - services = _services(f"driver-{gate.folder_name(driver.id, 'driver')}", layer.install) - part = windows_disk.Part(layer.folder, prefix, layer.install, - {name: windows_disk.argv(command, prefix) for name, command in services.items()}, - {"MCP_PORT": driver.port}, - (f"{windows_disk.BUILD}\\context\\protocol",) if protocol else ()) + layer, name = _stage_driver(context, driver, base, {}), gate.folder_name(driver.id, "driver") + driver_prefix = windows_disk.DRIVERS / name + parts = [_windows_part(layer, driver_prefix, { + program: windows_disk.argv(command, driver_prefix) + for program, command in _services(f"driver-{name}", layer.install).items()}, {"MCP_PORT": driver.port}, + protocol)] + for app in env.composed_apps(): + staged = _stage_app(context, app, base, {}) + prefix = PureWindowsPath(staged.prefix) + server = [str(prefix / "venv" / "Scripts" / "python.exe"), "-m", "agentenv_protocol.computer.serve_app", + staged.served] + commands = [server, *(windows_disk.argv(service.command, prefix) for service in staged.install.services)] + parts.append(_windows_part(staged, prefix, dict(zip(gate.programs(app, base.os.type), commands)), + {"ENVIRONMENT_NAME": app.environment_name}, protocol)) values = env.computer_values() settings = {name: values.get(name, setting.default) for name, setting in base.config.items() if setting.phase == "build"} @@ -393,7 +398,8 @@ def say(line: str) -> None: made = windows_disk.base(_key(recipe=_digest(windows_disk.RECIPE), disk_gb=disk_gb, user=user, form="windows-base", format=FORMAT), lambda: windows_disk.media(say), disk_gb, user, say, registry) - script = windows_disk.write(build, [part], settings, {"base": made.image, "layers": [part.folder]}) + script = windows_disk.write(build, parts, user=user, build_settings=settings, + image={"base": made.image, "layers": [part.folder for part in parts]}) expires = made.expires.isoformat(timespec="seconds") key = attempt.key = _key(base=made.image, base_expires=expires, provision=script, context=_digest(context), build_settings=settings, form="disk", arch=arch, format=FORMAT) @@ -494,6 +500,18 @@ def _part(layer: _Layer, programs: dict[str, str], environment: dict[str, Any], environment, _venv(layer), spec if _venv(layer) else None, layer.served, layer.steps) +def _windows_part(layer: _Layer, prefix: PureWindowsPath, services: dict[str, list[str]], + environment: dict[str, Any], protocol: bool) -> windows_disk.Part: + """``layer`` as a Windows disk installs it under ``prefix``: its ``services``' argv by name, with ``environment``, + and a venv's protocol as on Linux: this checkout's source, else an app's pinned to this release, which a driver's + requirements name themselves.""" + extra = "[app]" if layer.app else "" + spec = (f"{windows_disk.BUILD}\\context\\protocol{extra}" if protocol else + f"{_PROTOCOL}{extra}=={importlib.metadata.version(_PROTOCOL)}" if layer.app else None) + return windows_disk.Part(layer.folder, prefix, layer.install, services, environment, (spec,) if spec else (), + layer.served, tuple(step for step, _ in layer.steps)) + + 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`` @@ -745,8 +763,9 @@ def _base(base: ComputerArtifact, arch: str) -> tuple[str, str]: @dataclass(frozen=True) class _Layer: """A driver's or an app's part of a build: its folder in the build context, its prefix in the image, what it - installs, the file of its supervisord programs, and the id of its env. An app's venv also has the protocol's ``app`` extra, for the - app's own server, which listens in a folder of the app's the session user owns.""" + installs, the file of its supervisord programs, and the id of its env. An app's venv also has the protocol's ``app`` + extra, for the app's own server, which listens in a folder of the app's the session user owns; ``served`` is the + ``:`` it serves, and ``steps`` its install steps, by name and scope.""" folder: str prefix: str @@ -778,13 +797,13 @@ def _stage_app(context: Path, app: ComputerAppEnv, base: ComputerArtifact, name.""" install, name = app.install[base.os.type], gate.app_folder(app) file, _, cls = app.module.partition(":") - served = shlex.quote(f"{PurePosixPath(file).relative_to(install.files)}:{cls}") + served = f"{PurePosixPath(file).relative_to(install.files)}:{cls}" layer = _Layer(f"apps/{name}", str(apps_folder(base.os.type) / name), install, f"agentenv-app-{name}.conf", app.id, app=True, served=served, steps=tuple((step.name, step.scope) for step in app.declaration.install_steps if step.os is None or base.os.type in step.os)) programs = dict(zip(gate.programs(app, base.os.type), [ - f"venv/bin/python -m agentenv_protocol.computer.serve_app {served}", + f"venv/bin/python -m agentenv_protocol.computer.serve_app {shlex.quote(served)}", *(service.command for service in install.services)])) _stage(context, layer, f"app {app.id}", app.files, programs, {"ENVIRONMENT_NAME": app.environment_name}, from_store) @@ -915,7 +934,7 @@ def _runs(layer: _Layer) -> list[tuple[str, str]]: a build that write outside the layer's folder.""" return [*((run.scope, f"cd {layer.prefix} && {shlex.quote('./' + run.script)}") for run in layer.install.run or []), *((scope, f"cd {layer.prefix} && venv/bin/python -m agentenv_protocol.computer.run_install_step " - f"{layer.served} {step}") for step, scope in layer.steps)] + f"{shlex.quote(layer.served)} {step}") for step, scope in layer.steps)] def _as(scope: str, user: str, run: str) -> list[str]: diff --git a/src/agent_env/env/envs/computer/utils/gate.py b/src/agent_env/env/envs/computer/utils/gate.py index 5018d4b85..744584c8c 100644 --- a/src/agent_env/env/envs/computer/utils/gate.py +++ b/src/agent_env/env/envs/computer/utils/gate.py @@ -66,7 +66,8 @@ 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, or a port the driver listens on too.""" + doesn't support, no architecture in common, no install for the base's OS, a port the driver listens on too, 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)) @@ -78,6 +79,11 @@ def check_app(base: ComputerArtifact, driver: ComputerDriverEnv, app: ComputerAp problems.append(f"{at}: {app.id} declares no install for {os.type}") elif taken := sorted(set(_ports(app, os.type)) & set(_driver_ports(driver, os.type))): problems.append(f"{at}: {app.id} listens on {', '.join(map(str, taken))}, as {driver.id} does") + 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 " + "computer can't as it's composed, over ssh as its admin; give them scope \"system\" for " + "Windows") return problems diff --git a/src/agent_env/env/envs/computer/utils/macos_vm_bundle.py b/src/agent_env/env/envs/computer/utils/macos_vm_bundle.py index 5d35622b6..f6d37cc86 100644 --- a/src/agent_env/env/envs/computer/utils/macos_vm_bundle.py +++ b/src/agent_env/env/envs/computer/utils/macos_vm_bundle.py @@ -115,7 +115,8 @@ def _installed(part: Part) -> list[str]: if part.served is not None: lines.append(f"mkdir -p {shlex.quote(f'{prefix}/{SOCKET.parent}')}") commands = [*((run.scope, shlex.quote(f"./{run.script}")) for run in install.run or []), - *((scope, f"venv/bin/python -m agentenv_protocol.computer.run_install_step {part.served} {step}") + *((scope, f"venv/bin/python -m agentenv_protocol.computer.run_install_step {shlex.quote(part.served)} " + f"{step}") for step, scope in part.steps)] for scope, command in commands: run = f"cd {quoted} && {command}" diff --git a/src/agent_env/env/envs/computer/utils/windows_disk.py b/src/agent_env/env/envs/computer/utils/windows_disk.py index 64ed718cc..3e5526397 100644 --- a/src/agent_env/env/envs/computer/utils/windows_disk.py +++ b/src/agent_env/env/envs/computer/utils/windows_disk.py @@ -28,7 +28,7 @@ from pathlib import Path, PurePosixPath, PureWindowsPath from typing import Any, NamedTuple -from agentenv_protocol.computer.apps import apps_folder +from agentenv_protocol.computer.app import ADDRESS from agent_env.artifact.artifacts.computer import DISK_BASE_FILE, DISK_FILE, DISK_OWNER from agent_env.artifact.artifacts.docker_image import docker_login, push_local_image @@ -50,7 +50,7 @@ ADMIN = "agentadmin" #: Where agent-env's own files are on a Windows computer, and each layer's folder under them. ROOT = PureWindowsPath("C:/agentenv") -DRIVERS, APPS = ROOT / "drivers", apps_folder("windows") +DRIVERS = ROOT / "drivers" PYTHON = ROOT / "python" / "python.exe" BUILD = ROOT / "build" #: The fewest days of its evaluation a base may have left, as a deploy needs some. @@ -67,7 +67,14 @@ COMPACT_TIMEOUT_S = 1800 #: How often an install looks for setup's end, a compose for the base's ssh, and a shutdown for the guest's. INSTALL_POLL_S, BOOT_POLL_S, SHUTDOWN_POLL_S = 15, 5, 2 +#: How many times a compose tries a layer's download, and how long it waits between, as the base's setup does: a +#: server's errors can last minutes, as GitHub's release downloads' 504s have. A client error, 4xx, but a timeout or +#: a rate limit, fails at once, as no retry mends a wrong URL. +FETCH_TRIES, FETCH_PAUSE_S = 30, 10 CPUS, MEMORY_MB = 4, 8192 +#: The groups and the account the folders under ``ROOT`` grant access to, by their well-known SIDs, as icacls takes +#: them. +ADMINISTRATORS, SYSTEM, USERS = "*S-1-5-32-544", "*S-1-5-18", "*S-1-5-32-545" #: What every service runs with: Python reading and writing files as UTF-8, as on Linux, not in Windows' code page. UTF8 = {"PYTHONUTF8": "1"} #: What a base's install prints once, as it fetches Microsoft's media. @@ -136,7 +143,8 @@ class Base: @dataclass(frozen=True) class Part: """A layer as a Windows 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, what its venv installs beside its requirements, and for an app, the + ``:`` its own server serves and its install steps by name, each run elevated.""" folder: str prefix: PureWindowsPath @@ -144,6 +152,8 @@ class Part: services: dict[str, list[str]] environment: dict[str, Any] wants: tuple[str, ...] = () + served: str | None = None + steps: tuple[str, ...] = () def media_path() -> Path: @@ -322,24 +332,36 @@ def expiring(expires: str | None) -> bool: return expires is None or datetime.fromisoformat(expires) - datetime.now(timezone.utc) < timedelta(days=RENEW_DAYS) -def write(build: Path, parts: list[Part], build_settings: dict[str, Any], image: dict) -> str: - """Write ``build``'s ``provision.ps1``, which installs ``parts`` from ``build/context``, their services for - agentenv-session, ``build_settings`` as every service's ``AGENTENV_SETTING_`` and ``image`` as what the disk - says it holds; its text, which a composed disk's key covers.""" +def write(build: Path, parts: list[Part], *, user: str, build_settings: dict[str, Any], image: dict) -> str: + """Write ``build``'s ``provision.ps1``, which installs ``parts`` from ``build/context``, each app's own folder the + session user ``user``'s, their services for agentenv-session, ``build_settings`` as every service's + ``AGENTENV_SETTING_`` and ``image`` as what the disk says it holds, then leaves ``ROOT`` the admins' + (``_locked``); its text, which a composed disk's key covers.""" lines = ["$ErrorActionPreference = 'Stop'", "$ProgressPreference = 'SilentlyContinue'", f"$build = '{BUILD}'", _CHECKED, _FETCH] for part in parts: - lines += [f"# {part.folder}", *_installed_part(part)] + lines += [f"# {part.folder}", *_installed_part(part, user)] settings = {f"AGENTENV_SETTING_{name.upper()}": str(value) for name, value in build_settings.items()} lines += [_written(ROOT / "build-settings.json", json.dumps(settings)), - _written(ROOT / "image.json", json.dumps(image, sort_keys=True))] + _written(ROOT / "image.json", json.dumps(image, sort_keys=True)), *_locked(user)] script = "\r\n".join(lines) + "\r\n" (build / "provision.ps1").write_text(script) return script -def _installed_part(part: Part) -> list[str]: - """The lines that install ``part``: its files, downloads, venv, run scripts and services.""" +def _locked(user: str) -> list[str]: + """The lines that leave ``ROOT`` the admins', as on Linux ``/opt/agentenv`` is root's: every user reads and runs + what's there, none of the access ``C:\\`` gives every user to change it is left, and the session user ``user`` + writes only the services' logs, and each app's own folder, which keeps its own access.""" + return [_granted_alone(ROOT, {ADMINISTRATORS: "F", SYSTEM: "F", USERS: "RX"}), + f"Checked {{ icacls '{ROOT / 'logs'}' /grant {_ps(f'{user}:(OI)(CI)M')} | Out-Null }}"] + + +def _installed_part(part: Part, user: str) -> list[str]: + """The lines that install ``part``: its files, downloads, venv, an app's own folder, its run scripts and install + steps, and its services. An app's own folder, where its server writes its address and token, holds none of the + access its parents give: only ``user``, the session user its server runs as, SYSTEM and Administrators have it, as + on Linux the session user alone reaches the server's socket.""" prefix, install = part.prefix, part.install lines = [f"New-Item -ItemType Directory -Force '{prefix}' | Out-Null", f"Copy-Item -Recurse -Force \"$build\\context\\{PureWindowsPath(part.folder)}\\*\" '{prefix}'"] @@ -356,9 +378,16 @@ def _installed_part(part: Part) -> list[str]: + " ".join(_ps(want) for want in wants) + " }") lines.append(f"Checked {{ & '{venv / 'Scripts' / 'python.exe'}' -m pip list --format=json | " f"Set-Content -Encoding utf8 '{prefix / 'installed.json'}' }}") + if part.served is not None: + own = prefix / ADDRESS.parent.as_posix() + lines += [f"New-Item -ItemType Directory -Force '{own}' | Out-Null", + _granted_alone(own, {user: "F", SYSTEM: "F", ADMINISTRATORS: "F"})] for run in install.run or []: lines.append(f"$env:PREFIX = '{prefix}'; Push-Location '{prefix}'; Checked {{ & powershell -NoProfile " f"-ExecutionPolicy Bypass -File '{prefix / PureWindowsPath(run.script)}' }}; Pop-Location") + for step in part.steps: + lines.append(f"Push-Location '{prefix}'; Checked {{ & '{prefix / 'venv' / 'Scripts' / 'python.exe'}' -m " + f"agentenv_protocol.computer.run_install_step {_ps(part.served)} {_ps(step)} }}; Pop-Location") for name, argv in part.services.items(): spec = {"name": name, "prefix": str(prefix), "command": argv, "env": {**UTF8, **{key: str(value) for key, value in part.environment.items()}}} @@ -366,6 +395,13 @@ def _installed_part(part: Part) -> list[str]: return lines +def _granted_alone(folder: PureWindowsPath, access: dict[str, str]) -> str: + """The line that leaves ``folder`` with only ``access``, rights by account, which what's under it inherits, and + none of the access its parents give.""" + granted = " ".join(_ps(f"{account}:(OI)(CI){rights}") for account, rights in access.items()) + return f"Checked {{ icacls '{folder}' /inheritance:r /grant:r {granted} | Out-Null }}" + + def argv(command: str, prefix: PureWindowsPath) -> list[str]: """A service's ``command`` as the argv agentenv-session runs, split as Windows quotes it, a word in double quotes one word without them, and a relative program named under ``prefix``.""" @@ -529,6 +565,9 @@ def _ps(value: str) -> str: @{build = "$($v.CurrentBuild).$($v.UBR)"; grace_minutes = [int]$g} | ConvertTo-Json -Compress""" _CHECKED = ("function Checked([scriptblock]$Run) { & $Run; if ($LASTEXITCODE) { throw \"provision: $Run exited " "$LASTEXITCODE\" } }") -_FETCH = ("function Fetch([string]$Url, [string]$Sha256, [string]$To) { Invoke-WebRequest -UseBasicParsing -Uri $Url " - "-OutFile $To; $got = (Get-FileHash -Algorithm SHA256 $To).Hash.ToLower(); if ($got -ne $Sha256) { throw " - "\"provision: $Url came with sha256 $got, not $Sha256\" } }") +_FETCH = ("function Fetch([string]$Url, [string]$Sha256, [string]$To) { for ($try = 1; ; $try++) { try { " + "Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $To; break } catch { " + "$status = [int]$_.Exception.Response.StatusCode; " + f"if ($try -ge {FETCH_TRIES} -or ($status -ge 400 -and $status -lt 500 -and $status -notin 408, 429)) " + f"{{ throw }}; Start-Sleep {FETCH_PAUSE_S} }} }}; $got = (Get-FileHash -Algorithm SHA256 $To).Hash.ToLower(); " + "if ($got -ne $Sha256) { throw \"provision: $Url came with sha256 $got, not $Sha256\" } }") diff --git a/tst/integration/computer/windows/artifacts/check_notes/verify.py b/tst/integration/computer/windows/artifacts/check_notes/verify.py new file mode 100644 index 000000000..cbfa65f5e --- /dev/null +++ b/tst/integration/computer/windows/artifacts/check_notes/verify.py @@ -0,0 +1,64 @@ +"""The notes app composed onto the deployed Windows 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 program and title; its Start Menu +launcher and its launch since the computer booted; what its install left in its folder: the download, its PowerShell +run script's output, run elevated, and its install step's file on the build setting its env fixes; its own folder, +which the session user, SYSTEM and Administrators alone reach; and its server, which answers its token alone.""" +import json + +from agentenv_protocol.computer.card import COMPUTER_READY +from agentenv_protocol.computer.conformance.client import Driver + +PYTHON = "C:/agentenv/apps/notes/venv/Scripts/python.exe" +FACTS = r""" +import asyncio, json, os, subprocess, tempfile, time +from pathlib import Path +import httpx +from agentenv_protocol.computer import session +from agentenv_protocol.computer.app import ADDRESS +from agentenv_protocol.computer.served import served + +prefix = Path("C:/agentenv/apps/notes") +written = Path.home() / ".notes.json" +deadline = time.monotonic() + 60 +while not written.exists() and time.monotonic() < deadline: + time.sleep(0.5) +reached = served(prefix) +tokenless = httpx.get(f"http://127.0.0.1:{reached.port}/.well-known/agent-env.json", trust_env=False, timeout=10) +with reached.sync_client(10) as http: + answered = http.get("/.well-known/agent-env.json").status_code +acl = {} +for path in (prefix / ADDRESS.parent, prefix / ADDRESS): + listed = subprocess.run(["icacls", str(path)], capture_output=True, text=True).stdout.replace(str(path), "", 1) + acl[path.name] = sorted(line.strip().split(":")[0].rpartition("\\")[2] for line in listed.splitlines() + if ":(" in line) +print(json.dumps({ + "notes": json.loads(written.read_text()), + "windows": [window for window in asyncio.run(session.windows()) if window["title"] == "Notes"], + "launcher": (Path(os.environ["APPDATA"]) / "Microsoft/Windows/Start Menu/Programs/notes.lnk").is_file(), + "launched": sorted(path.name for path in (Path(tempfile.gettempdir()) / "agentenv" / "apps").iterdir()), + "run": (prefix / "run.txt").read_text().strip(), "installed": (prefix / "installed.txt").read_text(), + "license": (prefix / "LICENSE.txt").read_text().startswith("A. HISTORY OF THE SOFTWARE"), + "acl": acl, "token": [tokenless.status_code, answered]})) +""" +EXPECTED = { + "notes": {"signature": "-- Ada", "token": "t0ken", "edition": "pro"}, + "windows": [{"instance": "pythonw.exe", "class": "TkTopLevel", "title": "Notes"}], "launcher": True, + "launched": ["notes"], "run": "elevated True in C:\\agentenv\\apps\\notes", "installed": "pro", "license": True, + "acl": {name: ["Administrators", "SYSTEM", "user"] for name in (".agentenv", "server.json")}, + "token": [403, 200]} + + +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/windows/artifacts/check_session/verify.py b/tst/integration/computer/windows/artifacts/check_session/verify.py new file mode 100644 index 000000000..50320c1c8 --- /dev/null +++ b/tst/integration/computer/windows/artifacts/check_session/verify.py @@ -0,0 +1,121 @@ +"""The protocol's Windows session on the deployed computer, run inside it as the session user on the notes app's venv: +a launch's exit code is its app's; a pause holds every process of the app's job, one the app started too, and they run +again after; a stop ends an app with no window at once, one whose window closes when asked within the timeout, and one +whose window won't at the timeout, nothing of it left running after each; the helper killed ends its app; and a +program that isn't there is refused, saying why.""" +import json + +from agentenv_protocol.computer.conformance.client import Driver + +PYTHON = "C:/agentenv/apps/notes/venv/Scripts/python.exe" +SESSION = r""" +import asyncio, json, os, signal, sys, tempfile, time +from pathlib import Path +from agentenv_protocol.computer import _reaping, _session_windows as session + +work = Path(tempfile.mkdtemp()) +pythonw = str(Path(sys.executable).with_name("pythonw.exe")) +(work / "writer.py").write_text( + "import subprocess, sys, time\nfrom pathlib import Path\nout = Path(sys.argv[1])\n" + "if len(sys.argv) > 2:\n subprocess.Popen([sys.executable, __file__, sys.argv[1] + '.child'])\n" + "while True:\n with out.open('a') as f:\n f.write('x')\n time.sleep(0.01)\n") +(work / "window.py").write_text( + "import sys, tkinter as tk\nroot = tk.Tk()\nroot.title(sys.argv[1])\n" + "if sys.argv[2] == 'stubborn':\n root.protocol('WM_DELETE_WINDOW', lambda: None)\nroot.mainloop()\n") +facts, written = {}, {} + + +def sizes(*paths): + '''Each file's size as an open handle reads it, which a stat by name can read behind.''' + read = [] + for path in paths: + try: + with path.open("rb") as file: + read.append(file.seek(0, 2)) + except FileNotFoundError: + read.append(0) + return read + + +async def listed(title): + return any(window["title"] == title for window in await session.windows()) + + +async def main(): + pid = await session.launch([sys.executable, "-c", "import sys; sys.exit(7)"], work, {}) + facts["exit"] = await _reaping.returncode(pid, 30) + + out = work / "writes" + files = (out, Path(f"{out}.child")) + pid = await session.launch([pythonw, str(work / "writer.py"), str(out), "spawn"], work, {}) + await asyncio.sleep(3) + with session.paused(pid): + await asyncio.sleep(0.5) + held = sizes(*files) + await asyncio.sleep(2) + still = sizes(*files) + await asyncio.sleep(2) + facts["pause"] = {"held": held == still and min(held) > 0, + "ran after": all(after > before for after, before in zip(sizes(*files), still)), + "alive": session.alive(pid)} + await session.stop(pid, 10) + ended = sizes(*files) + await asyncio.sleep(1) + written["windowless stop"] = [ended, sizes(*files)] + facts["windowless stop"] = {"alive": session.alive(pid), "wrote after": written["windowless stop"][1] != ended} + + for title, kind, timeout_s in (("Obliging", "obliging", 10), ("Stubborn", "stubborn", 4)): + pid = await session.launch([pythonw, str(work / "window.py"), title, kind], work, {}) + deadline = time.monotonic() + 30 + while not await listed(title) and time.monotonic() < deadline: + await asyncio.sleep(0.5) + shown, started = await listed(title), time.monotonic() + await session.stop(pid, timeout_s) + facts[f"{kind} stop"] = {"shown": shown, "waited out": time.monotonic() - started >= timeout_s, + "alive": session.alive(pid), "listed": await listed(title)} + + out = work / "orphan" + pid = await session.launch([pythonw, str(work / "writer.py"), str(out)], work, {}) + await asyncio.sleep(2) + os.kill(pid, signal.SIGTERM) + await asyncio.sleep(2) + ended = sizes(out) + await asyncio.sleep(1) + written["helper killed"] = [ended, sizes(out)] + facts["helper killed"] = {"alive": session.alive(pid), "wrote after": written["helper killed"][1] != ended} + + try: + await session.launch(["no-such-program.exe"], work, {}) + facts["missing program"] = "started" + except OSError as e: + facts["missing program"] = str(e) + + +asyncio.run(main()) +print(json.dumps({"facts": facts, "written": written})) +""" +EXPECTED = { + "exit": 7, + "pause": {"held": True, "ran after": True, "alive": True}, + "windowless stop": {"alive": False, "wrote after": False}, + "obliging stop": {"shown": True, "waited out": False, "alive": False, "listed": False}, + "stubborn stop": {"shown": True, "waited out": True, "alive": False, "listed": False}, + "helper killed": {"alive": False, "wrote after": False}, +} +MISSING = "couldn't start no-such-program.exe: FileNotFoundError" + + +async def verify(mcp_url, env_url): + driver = await Driver.connect(env_url, 120) + out = await driver.run(PYTHON, "-c", SESSION, timeout_s=240) + try: + read = json.loads(out.get("stdout") or "") + except ValueError: + return [{"id": "session", "result": False, "description": json.dumps(out)[-2000:]}] + facts, written = read["facts"], read["written"] + missing = facts.get("missing program", "") + return [{"id": name, "result": facts.get(name) == expected, + "description": json.dumps({**facts.get(name, {}), "sizes": written[name]} if name in written + else facts.get(name))} + for name, expected in EXPECTED.items()] + [ + {"id": "missing program", "result": missing.startswith(MISSING), "description": missing}] diff --git a/tst/integration/computer/windows/envs/notes-desk/env.toml b/tst/integration/computer/windows/envs/notes-desk/env.toml new file mode 100644 index 000000000..fc2ada7fa --- /dev/null +++ b/tst/integration/computer/windows/envs/notes-desk/env.toml @@ -0,0 +1,4 @@ +type = "computer" +computer = "windows-11" +driver = "cua-driver" +apps = ["notes"] diff --git a/tst/integration/computer/windows/envs/notes/app.py b/tst/integration/computer/windows/envs/notes/app.py new file mode 100644 index 000000000..9fbbeac90 --- /dev/null +++ b/tst/integration/computer/windows/envs/notes/app.py @@ -0,0 +1,38 @@ +"""A notes app for the Windows 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. It requires no Python of its own, so its service and window run in the venv every app gets for its server. +Its install takes a download checked by sha256, and a PowerShell run script and an install step, both run elevated in +its folder, the step on the build setting its env.toml fixes; its window is matched by its title.""" +from pathlib import Path + +from pydantic import BaseModel, SecretStr + +from agentenv_protocol.computer import (ComputerAppEnvironment, Download, Launch, Ready, RunScript, Service, + computer_app, install_step, ready_check, windows) + +LICENSE = "https://raw.githubusercontent.com/python/cpython/v3.12.0/LICENSE" + + +@computer_app(supports=[{"os": {"type": "windows", "display": ["dwm"]}, "arch": ["amd64"]}]) +@windows(downloads=[Download(url=LICENSE, to="LICENSE.txt", + sha256="3b2f81fe21d181c499c59a256c8e1968455d6689d269aa85373bfb6af41da3bf")], + run=[RunScript(script="setup.ps1")], + services=[Service(name="notes", command="venv/Scripts/python.exe serve.py")], + launch=Launch(at="boot", command=["venv/Scripts/pythonw.exe", "window.py"], window={"title": "Notes"}, + ready=Ready(window=True)), + paths=["Notes"]) +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) + + @ready_check() + def written(self) -> bool: + return (Path.home() / ".notes.json").exists() diff --git a/tst/integration/computer/windows/envs/notes/env.toml b/tst/integration/computer/windows/envs/notes/env.toml new file mode 100644 index 000000000..5fb1340dd --- /dev/null +++ b/tst/integration/computer/windows/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/windows/envs/notes/serve.py b/tst/integration/computer/windows/envs/notes/serve.py new file mode 100644 index 000000000..de7e3d931 --- /dev/null +++ b/tst/integration/computer/windows/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/windows/envs/notes/setup.ps1 b/tst/integration/computer/windows/envs/notes/setup.ps1 new file mode 100644 index 000000000..a7db1ce3b --- /dev/null +++ b/tst/integration/computer/windows/envs/notes/setup.ps1 @@ -0,0 +1,4 @@ +# Run elevated once the app's files are in: whether it is, in the folder it was given. +$identity = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() +$elevated = $identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +"elevated $elevated in $env:PREFIX" | Set-Content -Encoding ascii run.txt diff --git a/tst/integration/computer/windows/envs/notes/window.py b/tst/integration/computer/windows/envs/notes/window.py new file mode 100644 index 000000000..48a7649ff --- /dev/null +++ b/tst/integration/computer/windows/envs/notes/window.py @@ -0,0 +1,9 @@ +"""The notes app's window, titled Notes, showing the signature its settings give.""" +import tkinter as tk + +from app import Notes + +root = tk.Tk() +root.title("Notes") +tk.Label(root, text=Notes().settings.signature).pack(padx=40, pady=40) +root.mainloop() diff --git a/tst/integration/computer/windows/tasks/notes.json b/tst/integration/computer/windows/tasks/notes.json new file mode 100644 index 000000000..3cc8b8d93 --- /dev/null +++ b/tst/integration/computer/windows/tasks/notes.json @@ -0,0 +1,11 @@ +[ + {"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": "session", "type": "env_outcome_verifier", "env_id": "notes-desk", "file_artifact_id": "check_session", + "verifier_id": "session", "depends_on": ["before"]}, + {"id": "reset", "type": "reset_env", "env_id": "notes-desk", "depends_on": ["session"]}, + {"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/windows_test.py b/tst/integration/computer/windows_test.py index 4e0e87e68..0cfdc0b4b 100644 --- a/tst/integration/computer/windows_test.py +++ b/tst/integration/computer/windows_test.py @@ -1,7 +1,11 @@ """The Windows test bundle through ``agent-env run``, on local Docker with KVM: its desktop, cua-driver on windows-11, composed on this machine, its base installed from the evaluation ISO in this machine's cache unless one is held here, passes the conformance probe at full, cua-driver's known failure on Windows waived, before and after a reset in place; -and an agent drives Notepad on it through cua-driver's tools. Needs Docker, KVM and the ISO; the agent's task, a model endpoint too.""" +and an agent drives Notepad on it through cua-driver's tools. 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 Windows install says, its own folder and +server the session user's alone, before and after a reset; and on it the protocol's Windows session launches, pauses +and stops apps, and all they start, as an app's server does. Needs Docker, KVM and the ISO; the agent's task, a model +endpoint too.""" from pathlib import Path @@ -38,5 +42,15 @@ def test_an_agent_saves_a_line_in_notepad_through_cua_drivers_tools(state): assert "passed (check: 1)" in ran.output, ran.output +def test_the_notes_app_composed_onto_windows_runs_as_installed_and_the_session_runs_apps_in_their_jobs(state, + monkeypatch): + monkeypatch.setenv("NOTES_TOKEN", "t0ken") + + ran = _run("notes") + + assert ran.exit_code == 0, ran.output + assert "passed (before: 1, session: 1, after: 1)" in ran.output, ran.output + + def _run(task: str): return CliRunner().invoke(cli, ["run", str(BUNDLE), "--task", task]) diff --git a/tst/unit/env/envs/test_computer_windows.py b/tst/unit/env/envs/test_computer_windows.py index 518b2ea18..3803fc587 100644 --- a/tst/unit/env/envs/test_computer_windows.py +++ b/tst/unit/env/envs/test_computer_windows.py @@ -1,8 +1,12 @@ """A Windows computer env, composed on this machine's Docker with KVM: its base installed from Microsoft's media once and -kept here, then the driver installed over ssh by a provision script as its ``[install.windows]`` says and packed as a -thin layer on the base, under a tag of its key, which a compose of the same inputs reuses while this machine holds it; -the env's computer is a disk of the base's form. A Windows base is amd64, disk-only and started by agentenv-session.""" - +kept here, then the driver and each app installed over ssh by a provision script as their ``[install.windows]`` says +and packed as a thin layer on the base, under a tag of its key, which a compose of the same inputs reuses while this +machine holds it, its agentenv folder left the admins' and the session user's to read; the env's computer is a disk +of the base's form. An app's own server runs from its venv, its own folder the session user's alone, its install steps +elevated; one it runs as the session user is refused. A Windows base is amd64, disk-only and started by +agentenv-session. The scripts the Windows test bundle's verifiers run inside the computer are Python.""" + +import ast import json import threading import time @@ -13,12 +17,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, Packages, RunScript from agent_env.artifact.artifacts.computer import ComputerArtifact 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 composer, gate, windows_disk @@ -27,6 +32,7 @@ from agent_env.providers.sandbox_providers.sandbox_provider import SandboxProvider URL = "https://example.com/desk.zip" +BUNDLE = Path(__file__).parents[3] / "integration" / "computer" / "windows" SERVE = "venv/Scripts/python.exe -m agentenv_protocol.computer.serve driver.py:Desk" WINDOWS = {"type": "windows", "version": "11", "edition": "Enterprise Evaluation"} DISK = {"init": "agentenv-session", "input_channel": "nocloud", "min_disk_gb": 64} @@ -123,9 +129,37 @@ def _driver(folder: Path) -> ComputerDriverEnv: return Env.get(put.id, put.version) -def _put(driver: ComputerDriverEnv, link: SandboxProvider, **settings) -> ComputerEnv: - return ComputerEnv.put(id="@local/~/kit/desktop", computer=_base(), driver=driver, environment_name="desktop", - composing=Composing(sandbox_provider=link), computer_settings=settings or None) +def _notes(base: ComputerArtifact | None, driver: ComputerDriverEnv | None, folder: Path, + steps: tuple = (("stamp", "system", None),)) -> ComputerAppEnv: + """A notes app on Windows: a sync service in the venv its own server gets, an elevated run script, ``steps``, its + install steps by name, scope and the OS types they run on, and the edition its env fixes.""" + folder.mkdir(parents=True, exist_ok=True) + for name, text in (("app.py", "class Notes: ...\n"), ("setup.ps1", "'ran' | Set-Content 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.ps1")}) + declaration = AppDeclaration.model_validate({ + "supports": [{"os": {"type": "windows", "display": ["dwm"]}, "arch": ["amd64"]}], "permissions": [], + "settings": {"type": "object", "properties": {}}, "skill": None, "data": [], "ready_checks": [], + "build_settings": {"type": "object", "properties": {"edition": {"type": "string", "default": "home"}}}, + "install_steps": [{"name": name, "scope": scope, "os": os} for name, scope, os in steps], + "os": {"windows": {"install": {"run": [{"script": "setup.ps1"}], "services": [ + {"name": "sync", "command": "venv/Scripts/python.exe 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 _put(driver: ComputerDriverEnv, link: SandboxProvider, apps: list[ComputerAppEnv] | None = None, + base: ComputerArtifact | 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=apps or []) + + +def _written_json(line: str) -> object: + """The JSON a provision script's line writes to its file (``windows_disk._written``).""" + return json.loads(line.split(", '", 1)[1].rsplit("')", 1)[0].replace("''", "'")) def test_a_windows_env_composes_a_thin_disk_on_its_base_here_the_driver_installed_by_its_provision_script( @@ -150,15 +184,71 @@ def test_a_windows_env_composes_a_thin_disk_on_its_base_here_the_driver_installe assert any(line.startswith(f"Checked {{ & '{prefix}\\venv\\Scripts\\python.exe' -m pip install") and f"'-r' '{prefix}\\requirements-windows.txt'" in line for line in lines) daemon = next(line for line in lines if "services\\driver-desk-daemon.json" in line) - assert json.loads(daemon.split(", '", 1)[1].rsplit("')", 1)[0].replace("''", "'")) == { + assert _written_json(daemon) == { "name": "driver-desk-daemon", "prefix": str(prefix), "command": [str(prefix / "venv/Scripts/python.exe"), "driver.py"], "env": {"PYTHONUTF8": "1", "MCP_PORT": "18765"}} + assert lines[-3:] == [ + "Checked { icacls 'C:\\agentenv' /inheritance:r /grant:r '*S-1-5-32-544:(OI)(CI)F' '*S-1-5-18:(OI)(CI)F' " + "'*S-1-5-32-545:(OI)(CI)RX' | Out-Null }", + "Checked { icacls 'C:\\agentenv\\logs' /grant 'user:(OI)(CI)M' | Out-Null }", ""] assert {"provision.ps1", "context/driver/driver.py", "context/driver/install.ps1"} <= set(files) computer = env.computer_artifact() assert computer.os.type == "windows" and computer.forms.disk.init == "agentenv-session" assert computer.forms.disk.image is None and env.metadata["computer"]["disks"] == {"amd64": built["image"]} +def test_an_app_is_a_layer_of_its_own_its_server_on_its_venv_its_folder_the_session_users_its_steps_elevated( + docker, tmp_path): + base = _base() + driver = _driver(tmp_path / "desk") + env = _put(driver, FakeLinux(), apps=[_notes(base, driver, tmp_path / "notes")], base=base) + + assert env.metadata["composed"]["disk"]["amd64"]["status"] == "ready" + ((_, script, files, _),) = docker.composed + prefix, lines = PureWindowsPath("C:/agentenv/apps/notes"), script.split("\r\n") + python = prefix / "venv" / "Scripts" / "python.exe" + installed = lines[lines.index("# apps/notes") + 1:] + assert installed[:5] == [ + f"New-Item -ItemType Directory -Force '{prefix}' | Out-Null", + f"Copy-Item -Recurse -Force \"$build\\context\\apps\\notes\\*\" '{prefix}'", + f"Checked {{ & 'C:\\agentenv\\python\\python.exe' -m venv '{prefix}\\venv' }}", + f"Checked {{ & '{python}' -m pip install -q --disable-pip-version-check " + "'C:\\agentenv\\build\\context\\protocol[app]' }", + f"Checked {{ & '{python}' -m pip list --format=json | Set-Content -Encoding utf8 '{prefix}\\installed.json' }}"] + assert installed[5:9] == [ + f"New-Item -ItemType Directory -Force '{prefix}\\.agentenv' | Out-Null", + f"Checked {{ icacls '{prefix}\\.agentenv' /inheritance:r /grant:r 'user:(OI)(CI)F' '*S-1-5-18:(OI)(CI)F' " + "'*S-1-5-32-544:(OI)(CI)F' | Out-Null }", + f"$env:PREFIX = '{prefix}'; Push-Location '{prefix}'; Checked {{ & powershell -NoProfile -ExecutionPolicy " + f"Bypass -File '{prefix}\\setup.ps1' }}; Pop-Location", + f"Push-Location '{prefix}'; Checked {{ & '{python}' -m agentenv_protocol.computer.run_install_step " + "'app.py:Notes' 'stamp' }; Pop-Location"] + services = {spec["name"]: spec for spec in (_written_json(line) for line in installed if "\\services\\" in line)} + environment = {"PYTHONUTF8": "1", "ENVIRONMENT_NAME": "notes"} + assert services == { + "app-notes": {"name": "app-notes", "prefix": str(prefix), "env": environment, + "command": [str(python), "-m", "agentenv_protocol.computer.serve_app", "app.py:Notes"]}, + "app-notes-sync": {"name": "app-notes-sync", "prefix": str(prefix), "env": environment, + "command": [str(python), "sync.py"]}} + 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 any(line.endswith("\"layers\": [\"driver\", \"apps/notes\"]}')") for line in lines) + assert {"context/apps/notes/app.py", "context/apps/notes/.agentenv/build_settings.json"} <= set(files) + + +def test_an_apps_install_step_for_the_session_user_is_refused_on_windows_alone_or_composed(docker, tmp_path): + base = _base() + driver = _driver(tmp_path / "desk") + steps = (("stamp", "system", None), ("remember", "session", ["linux"]), ("greet", "session", None)) + refused = "@local/~/kit/notes's install steps greet run as the session user, which a Windows computer can't" + + with pytest.raises(ValueError, match=f"computer app env '@local/~/kit/notes': app: {refused}"): + _notes(base, driver, tmp_path / "notes", steps) + with pytest.raises(ValueError, match=rf"apps\[0\]: {refused}"): + _put(driver, FakeLinux(), apps=[_notes(None, None, tmp_path / "notes", steps)], base=base) + assert docker.composed == [] + + def test_a_compose_of_the_same_inputs_reuses_the_disk_the_image_store_holds(docker, tmp_path): driver = _driver(tmp_path / "desk") first = _put(driver, FakeLinux()) @@ -344,3 +434,12 @@ def run(argv, **kwargs): assert ran == [["docker", "run", "--rm", "-v", f"{tmp_path}:/out", *args, "--entrypoint", "qemu-img", "machine:1", "convert", "-O", "qcow2", *over, "/out/disk.img", "/out/compact.img"]] assert (tmp_path / "disk.img").read_text() == "compacted" and not (tmp_path / "compact.img").exists() + + +@pytest.mark.parametrize("artifact, script", [("check_notes", "FACTS"), ("check_session", "SESSION")]) +def test_the_scripts_the_windows_bundles_verifiers_run_inside_the_computer_are_python(artifact, script): + verifier = ast.parse((BUNDLE / "artifacts" / artifact / "verify.py").read_text()) + (text,) = [node.value.value for node in verifier.body + if isinstance(node, ast.Assign) and [target.id for target in node.targets] == [script]] + + ast.parse(text)