diff --git a/AGENTS.md b/AGENTS.md index c80ddc3a8..d2b46950e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -140,8 +140,9 @@ and deserialized through a registry. | `agents/` | The `agents` bundle: the agents agent-env ships, one folder each under `agents/agents/`, with a smoke task each under `agents/tasks/`. Its Claude Code, `@local/agentenv-framework/agents/claude-code`, is the default agent. An agent's Python packages are its folder's `requirements.txt`, installed in its image, never in agent-env's own environment. A run that pulls one of its agents reads the whole bundle, so `tst/unit/bundle/shipped_bundles_test.py` parses and checks every shipped bundle. | | `computers/` | The `computers` bundle: the base computers agent-env ships as `computer` artifacts, `ubuntu-24.04` (XFCE on Xvfb, settings as `AGENTENV_SETTING_`; it starts as root, its container form's `start_as`, whose supervisord, `root.conf`, runs the clock follower as `agentenv-clock` and the session's supervisord as the session user, which runs every session program), and `omarchy-4.5`, a disk alone built on the composing machine from Omarchy's own VM image (its `[forms.disk]` `source`) by `disk/omarchy/`'s recipe, its services systemd user units of the graphical session that start through `with-seed`, booted by the launcher as a `linux-desktop` guest (`env/envs/computer/utils/omarchy_disk.py`, on `thin_disk.py`, which Windows' disks use too), aliased through `[artifacts]`. It has no tasks either. `disk/linux/` is the Linux disk form's recipe: `boot/`, whose Dockerfile makes a disk form (a GPT disk of an EFI system partition holding one UKI, Ubuntu's kernel with a busybox initramfs whose init reads a NoCloud seed and `switch_root`s into the image's entrypoint, and an ext4 root filesystem that is a container image's, plus `/etc/agentenv/vm/image.env` from its OCI config), `tools/`, the disk tools that make it unprivileged on the builder's own platform (`mkfs.ext4 -d`), and `launcher/`, the QEMU launcher that boots one under KVM. A base's `[forms.disk]` with no image is made from its container form; the composer makes each arch's disk from its composed container, keeping the base disk's EFI system partition, keyed by the container's key, the base disk and the recipe; gate 3 probes it where a provider boots disks natively (`disk_image_platforms()`), and a deploy's first boot with KVM probes one left `unverified`; a deploy naming `form = "disk"` boots it through `create_vm(image=..., env=...)`, its settings in the seed. `artifacts/ubuntu-24.04/clock/` is the computer's clock, in the base's folder since an image builds only from its own: agent-env's build of libfaketime 0.9.12 (`libfaketime.patch` and the two `agentenv_clock_*.c` files it includes, GPL-2.0-only as libfaketime is, built by `build.sh` from upstream's sha256-pinned tarball, in a stage of the base's Dockerfile of its own) and the TLS shim, which `/etc/ld.so.preload` loads into every process and which fake only the session user's: clock reads and waits for a set clock time follow the page the follower (`clock-follower`) writes (`clockpage.py`), monotonic and relative waits staying real, with its default settings (`clock.conf`); `tst/clock` gates it. The follower long-polls the gateway's `/clock/watch` under the config core writes through the root channel when a multi env deploys the computer and after a reset (`env/envs/computer/utils/clock.py`), each time waiting on the computer's `sync_time` until it follows, a warning naming why when it doesn't (arming the clock refuses such a computer), and the driver serves `clock/v1` `sync_time` where the follower is (`agentenv_protocol.computer.clock`), not on a disk guest, whose follower core can't reach. | | `browsers/` | The `browsers` bundle: the website browsers agent-env ships, today `chromium`, a `computer_app` env (Chrome for Testing at the build Playwright pins, for amd64 and arm64, set up for unattended use by managed policies in `/etc/opt/chrome_for_testing/policies/managed`, opened at boot on its `start_urls`, its profile its data). All of it, its pin, install step, policies and command, is in `envs/chromium/chromium.py`; `tst/integration/computer/chromium_app_test.py` runs it. Like `drivers`, it has no tasks. | +| `apps/` | The `apps` bundle: the computer apps agent-env ships, today `libreoffice`, a `computer_app` env (Writer, Impress and Calc from Ubuntu's apt with GTK3 and fonts with Office's metrics, for x11 on amd64 and arm64). Its install step writes `agentenv.xcd` into `/etc/libreoffice/registry/`, a configuration layer under the profile that keeps it from asking anything (no tips, no first-run or version bars, no keep-format question, no lock files, no document recovery), and writes each of LibreOffice's desktop entries into `/usr/local/share/applications/`, opening through its `open.py`, which has the app's server launch it when none runs and then hands the files to that copy (LibreOffice runs one per user), so a file opened by its type, as the file manager does, and every launcher, go to the copy the server stops around a load and a reset; a launch while it runs goes to that copy, starting no other (`_launch_running`). It launches on demand, the Start Center first, ready once a LibreOffice window shows; its data is its profile, which a reset takes back to none. Like `browsers`, it has no tasks; the `office` example uses it. | | `drivers/` | The `drivers` bundle: the computer drivers agent-env ships as `computer_driver` envs, the reference driver (X11 on Linux, recording through `x11grab`) aliased `reference`, the page driver aliased `page`, a browser computer's, whose screen is the browser's active tab and which serves Playwright MCP's `browser_*` tools beside `computer_*` from a Playwright MCP it runs attached to the browser over a CDP address the relay issues it (`browser_evaluate` and `browser_run_code_unsafe` hidden by default; each call an entry in the browser's log, begun and ended with `browser/v1` `calls/begin` and `calls/end`, that its CDP commands name as `for`; `scripts/upgrade_playwright_mcp.py` pins its version), and trycua's cua-driver aliased `cua-driver` (its daemon from the pinned PyPI wheel, its own instructions, tools and skill resources served to agents through `@server_instructions`, `forwarded_tools` and `@resource`, its tools beside `computer_*`, `computer/v1` on its desktop-scope calls, its skill pack leading the computer skill; `tst/integration/computer/cua_driver_test.py` runs it). It has no tasks: a bundle with neither tasks nor evals is refused unless its `aliases.toml` names something for other bundles to use. The shipped `desktop` example composes it with the `ubuntu-24.04` base into a computer env, which `tst/integration/computer/` deploys, probes, records and has an agent use. | -| `examples/` | The example bundles agent-env ships (`hello`, and `echo-mcp`, which needs Docker, and `desktop`, a computer env of `ubuntu-24.04` and `reference` with a probe task and an agent task). Tests run them, but they are examples for users, not fixtures. Every shipped bundle is registered under `agent_env.bundles` in `pyproject.toml`, ` = ""` shipping `src///`. | +| `examples/` | The example bundles agent-env ships (`hello`, and `echo-mcp`, which needs Docker, and `desktop`, a computer env of `ubuntu-24.04` and `reference` with a probe task and an agent task, and `office`, the same with the `apps` bundle's `libreoffice`, whose agent task writes a .docx and a .pptx with an animation). Tests run them, but they are examples for users, not fixtures. Every shipped bundle is registered under `agent_env.bundles` in `pyproject.toml`, ` = ""` shipping `src///`. | ### Training helpers diff --git a/pyproject.toml b/pyproject.toml index 78971dc4b..816798cfe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -182,6 +182,7 @@ agent-env = "agent_env.cli:cli" [project.entry-points."agent_env.bundles"] agents = "agent_env" +apps = "agent_env" browsers = "agent_env" computers = "agent_env" drivers = "agent_env" @@ -190,6 +191,7 @@ hello = "agent_env.examples" echo-mcp = "agent_env.examples" mail-mcp = "agent_env.examples" next-env-event = "agent_env.examples" +office = "agent_env.examples" advance-to-next-event = "agent_env.examples" [project.urls] diff --git a/src/agent_env/apps/README.md b/src/agent_env/apps/README.md new file mode 100644 index 000000000..eb55affe8 --- /dev/null +++ b/src/agent_env/apps/README.md @@ -0,0 +1,28 @@ +The computer apps that come with agent-env, today `libreoffice`, a `computer_app` env a computer env lists in its +`apps`. Name it from another bundle as `libreoffice`, the alias `aliases.toml` gives it, or by its id, +`@local/agentenv-framework/apps/libreoffice`. + +`envs/libreoffice/` is LibreOffice for an X11 desktop on Linux (`amd64` and `arm64`): Writer, Impress and Calc from +Ubuntu's apt, with the GTK3 interface, at the versions a put's package lock pins, and fonts with the metrics of +Office's, Carlito for Calibri, Caladea for Cambria and Liberation for Arial, Times New Roman and Courier New, so Office +files keep their layout. + +- **Unattended use:** its install step writes `agentenv.xcd` into `/etc/libreoffice/registry/`, a layer of LibreOffice's + configuration under the session user's profile: no tip of the day, no first-run, new-version, "Get involved" or + donation bar, no question whether to keep an Office format on save, no lock files beside documents, and no document + recovery after the app was stopped around a load or a reset. +- **Launch:** on demand, through its server, the Start Center first (`soffice --nologo`), ready once a LibreOffice + window shows. Every LibreOffice launcher on the computer runs `open.py`: the install step writes each of LibreOffice's + desktop entries into `/usr/local/share/applications/`, which takes precedence, the same but for that. With no copy + running, `open.py` has the server launch one and waits until it's ready, then hands the files to it, as LibreOffice + runs one copy per user, so the server starts every copy and a load or a reset can stop it. A file the file manager or + `exo-open` opens goes there by its type: a .docx, .doc, .odt or .rtf to Writer, a .pptx, .ppt or .odp to Impress, an + .xlsx, .xls, .ods or .csv to Calc, a CSV comma-separated in UTF-8 with no import dialog. A launch while it runs goes + to the copy that runs, as one of LibreOffice's own does, starting no other. +- **Data:** its profile, `~/.config/libreoffice`, which the image has none of, so each deploy and each reset starts with + a clean one, under the same defaults. Documents are the computer's own, in Documents and Downloads. +- **Skill:** `SKILL.md`, for an agent working through its windows: opening each app, saving in Office formats, slides, + animations and formulas. + +The `office` example's desktop composes it with the `ubuntu-24.04` base and the `reference` driver, and +`tst/integration/computer/libreoffice_app_test.py` deploys it. diff --git a/src/agent_env/apps/aliases.toml b/src/agent_env/apps/aliases.toml new file mode 100644 index 000000000..48abf59ad --- /dev/null +++ b/src/agent_env/apps/aliases.toml @@ -0,0 +1,3 @@ +# Ids a computer env's apps, or a task, can name these apps by; a store's env of the id wins over an alias. +[envs] +libreoffice = ["libreoffice"] diff --git a/src/agent_env/apps/envs/libreoffice/SKILL.md b/src/agent_env/apps/envs/libreoffice/SKILL.md new file mode 100644 index 000000000..c6450a1e5 --- /dev/null +++ b/src/agent_env/apps/envs/libreoffice/SKILL.md @@ -0,0 +1,48 @@ +--- +description: Write documents, presentations and spreadsheets in LibreOffice on this computer, and save them in Office formats. +--- +# LibreOffice on this computer + +LibreOffice Writer (documents), Impress (presentations) and Calc (spreadsheets) are installed, with fonts that match +Office's (Carlito for Calibri, Caladea for Cambria, Liberation for Arial, Times New Roman and Courier New). It asks +nothing when it starts and keeps no lock files beside documents. + +## Opening + +- **A file:** double-click it in the file manager, for instance in Documents or Downloads. A .docx, .doc, .odt or .rtf + opens in Writer; a .pptx, .ppt or .odp in Impress; an .xlsx, .xls, .ods or .csv in Calc. A CSV opens as + comma-separated UTF-8, with no import dialog. +- **Something new:** Applications menu > Office > LibreOffice Writer, Impress or Calc. LibreOffice alone opens the Start + Center, where Writer Document, Impress Presentation and Calc Spreadsheet each start a new file. + +## Saving in Office formats + +- **File > Save As** (Ctrl+Shift+S). Choose the type in the dialog's file type list, "Word 2007–365 (.docx)", + "PowerPoint 2007–365 (.pptx)" or "Excel 2007–365 (.xlsx)", type the name, and press Save. It saves in that format + without asking whether to keep it. +- **Ctrl+S** saves a file opened in an Office format in that format. A new file's first Ctrl+S opens Save As. +- Save into Documents unless you're told where. + +## Writer + +- Headings: the paragraph style box on the toolbar, or Ctrl+1 and Ctrl+2; body text is Ctrl+0. +- Bold, italic and underline: Ctrl+B, Ctrl+I, Ctrl+U. Lists: the bullet and numbering buttons on the toolbar. +- A table: Table > Insert Table (Ctrl+F12). + +## Impress + +- A new slide: Slide > New Slide (Ctrl+M). Its layout: the sidebar's Properties deck, Layouts. +- Click a placeholder to type its title or text. +- **An animation:** click the object to select it, open View > Animation, press the + button (Add Effect) and pick an + effect, for instance Entrance > Appear or Fade In. A slide's transition is Slide > Slide Transition. +- The slideshow: F5; Esc leaves it. + +## Calc + +- Type in a cell and press Enter or Tab. A formula starts with =, for instance =SUM(B2:B10). +- Ctrl+Home goes to A1. + +## When something's in the way + +- A dialog waits for an answer; Esc closes most. +- A window you can't see is in the panel's window list at the top of the screen. diff --git a/src/agent_env/apps/envs/libreoffice/agentenv.xcd b/src/agent_env/apps/envs/libreoffice/agentenv.xcd new file mode 100644 index 000000000..6560a1dc9 --- /dev/null +++ b/src/agent_env/apps/envs/libreoffice/agentenv.xcd @@ -0,0 +1,31 @@ + + + + + + + false + false + false + false + false + + false + + + + 24.2 + 4102444800 + 4102444800 + + + + false + false + + diff --git a/src/agent_env/apps/envs/libreoffice/env.toml b/src/agent_env/apps/envs/libreoffice/env.toml new file mode 100644 index 000000000..96cfcc22a --- /dev/null +++ b/src/agent_env/apps/envs/libreoffice/env.toml @@ -0,0 +1,4 @@ +type = "computer_app" +module = "libreoffice.py:LibreOffice" +computer = "ubuntu-24.04" +driver = "reference" diff --git a/src/agent_env/apps/envs/libreoffice/libreoffice.py b/src/agent_env/apps/envs/libreoffice/libreoffice.py new file mode 100644 index 000000000..4566b5819 --- /dev/null +++ b/src/agent_env/apps/envs/libreoffice/libreoffice.py @@ -0,0 +1,76 @@ +"""agentenv's LibreOffice app: Writer, Impress and Calc from Ubuntu's apt, with the GTK3 interface and fonts with the +metrics of Office's (Carlito for Calibri, Caladea for Cambria, Liberation for Arial, Times New Roman and Courier New), +so Office files keep their layout. Its defaults (``agentenv.xcd``), a layer of LibreOffice's own configuration under the +session user's profile, keep it from asking anything an agent didn't. It launches on demand, through its server, the +Start Center first; every LibreOffice launcher on the computer, those that open a file by its type included, goes +through ``open.py``, so its server starts every copy and a load or a reset can stop it. Its data is its profile, which +the image has none of, so each deploy and each reset starts with a clean one, under the same defaults.""" +import asyncio +import json +import re +import subprocess +from pathlib import Path + +from agentenv_protocol.computer import (ComputerAppEnvironment, Launch, Packages, Ready, computer_app, install_step, + linux) + +#: Writer, Impress and Calc, with GTK3 for the desktop, and the fonts with Office's metrics. +PACKAGES = ["libreoffice-writer", "libreoffice-impress", "libreoffice-calc", "libreoffice-gtk3", + "fonts-crosextra-carlito", "fonts-crosextra-caladea", "fonts-liberation2"] +SOFFICE = "/usr/lib/libreoffice/program/soffice" +#: Every LibreOffice main window's WM_CLASS instance, the Start Center's and each document's; its dialogs are soffice's. +WINDOW = {"instance": "libreoffice"} +PROFILE = ".config/libreoffice" +#: Where ``open.py`` leaves what it opens for its server's launch, beside the server. +OPENS = Path(".agentenv", "opens") +#: Where its defaults go: the first layer of its configuration on Debian and Ubuntu, read before the user's profile. +REGISTRY = Path("/etc/libreoffice/registry") +#: Its own launchers, and those that take their place: a desktop entry here has precedence over one of the same name in +#: /usr/share/applications. +DESKTOP_ENTRIES, OVERRIDES = Path("/usr/share/applications"), Path("/usr/local/share/applications") + + +@computer_app(supports=[{"os": {"type": "linux", "display": ["x11"]}, "arch": ["amd64", "arm64"]}]) +@linux(packages=Packages(apt=PACKAGES), paths=[PROFILE], skill="SKILL.md", + launch=Launch(at="demand", command=[SOFFICE, "--nologo"], window=WINDOW, ready=Ready(window=True))) +class LibreOffice(ComputerAppEnvironment): + """A launch while it runs goes to the copy that runs: what waits in ``OPENS``, oldest first, or with nothing + waiting, a launch as one of LibreOffice's own, which opens no window while a document is open. A second copy its + server started would only hand it to the first and exit, leaving its server with no copy it knows; and handed on in + its server's launch, it's handed on in the turn a load or a reset takes too, so neither stops that copy in + between.""" + + def _launch_running(self): + return self._hand_on + + async def _hand_on(self) -> None: + calls = [] + for request in sorted(OPENS.glob("*.json")): + try: + calls += json.loads(request.read_text(encoding="utf-8")) + request.unlink() + except FileNotFoundError: + continue + for arguments in calls or [[]]: + handed = await asyncio.create_subprocess_exec(SOFFICE, "--nologo", *arguments) + await handed.wait() + + @install_step(scope="system", os="linux") + def configure(self) -> None: + """Its defaults in its configuration's first layer, at the series installed; and each of its desktop entries + in its place, the same but for opening through ``open.py``.""" + folder = Path.cwd() + version = subprocess.run(["dpkg-query", "-W", "-f=${Version}", "libreoffice-core"], check=True, + capture_output=True, text=True).stdout + series = re.match(r"(?:\d+:)?(\d+\.\d+)", version).group(1) + defaults = (folder / "agentenv.xcd").read_text(encoding="utf-8") + (REGISTRY / "agentenv.xcd").write_text(re.sub( + r'()[^<]*', rf"\g<1>{series}", defaults), + encoding="utf-8") + OVERRIDES.mkdir(parents=True, exist_ok=True) + opener = f"{folder}/venv/bin/python {folder}/open.py" + for entry in sorted(DESKTOP_ENTRIES.glob("libreoffice-*.desktop")): + text = entry.read_text(encoding="utf-8") + (OVERRIDES / entry.name).write_text(re.sub(r"^Exec=libreoffice\b", f"Exec={opener}", text, flags=re.M), + encoding="utf-8") + subprocess.run(["update-desktop-database", str(OVERRIDES)], check=True) diff --git a/src/agent_env/apps/envs/libreoffice/open.py b/src/agent_env/apps/envs/libreoffice/open.py new file mode 100644 index 000000000..4ac7404c8 --- /dev/null +++ b/src/agent_env/apps/envs/libreoffice/open.py @@ -0,0 +1,82 @@ +"""Open files in LibreOffice through the app's own server, as every LibreOffice launcher on the computer does (its +install step points them here): ``open.py [--writer|--impress|--calc|...] [FILE...]``. + +The server stops and relaunches the app around a load and a reset, and only the copy it started, so this never runs +``soffice`` itself: it leaves what it opens in ``OPENS`` and has the server launch the app, whose launch hands it to the +copy that runs, LibreOffice running one per user, in the turn a load or a reset takes too, so neither stops that copy +in between. With none running, the server starts one, and its launch once that's ready hands it what waits. A CSV opens +comma-separated in UTF-8, without Calc's text import dialog.""" +from __future__ import annotations + +import json +import os +import sys +import time +import uuid +from pathlib import Path +from typing import Optional + +from agentenv_protocol.computer.apps import endpoint +from agentenv_protocol.computer.served import served +from agentenv_protocol.types import WELL_KNOWN_PATH + +FOLDER = Path(__file__).resolve().parent +#: Where an open waits for the server's launch: a JSON list of ``soffice`` argument lists, named so the oldest sorts +#: first. +OPENS = FOLDER / ".agentenv" / "opens" +#: Comma-separated, double-quoted, UTF-8 (76), from the first line: what a CSV the agent opens most likely is. +CSV = "--infilter=CSV:44,34,76,1" +READY_S = 60.0 +#: How often it asks the server whether the app it launched is ready. +READY_POLL_S = 0.25 + + +def invocations(argv: list[str]) -> list[list[str]]: + """The ``soffice`` arguments ``argv`` opens with, each file by its absolute path, as the server's folder isn't + this one's: CSV files on their own, with ``CSV``'s filter, after the rest.""" + argv = [arg if arg.startswith("-") or "://" in arg else os.path.abspath(arg) for arg in argv] + csv = [arg for arg in argv if arg.lower().endswith(".csv")] + rest = [arg for arg in argv if arg not in csv] + return ([rest] if rest or not csv else []) + ([[CSV, *csv]] if csv else []) + + +def waiting(calls: list[list[str]]) -> Path: + """``calls`` left for the server's launch, written beside their place and renamed into it, so it never reads half + of them.""" + OPENS.mkdir(parents=True, exist_ok=True) + request = OPENS / f"{time.time_ns()}-{uuid.uuid4().hex}.json" + staged = request.with_suffix(".staged") + staged.write_text(json.dumps(calls), encoding="utf-8") + staged.replace(request) + return request + + +def handed_on(request: Optional[Path], timeout_s: float = READY_S) -> None: + """The server's launch of the app, again once it's ready while ``request`` still waits, as it does when the launch + had to start the app.""" + with served(FOLDER).sync_client(timeout=timeout_s) as http: + card = http.get(WELL_KNOWN_PATH).raise_for_status().json() + deadline = time.monotonic() + timeout_s + while True: + http.post(endpoint(card, "launch"), json={}).raise_for_status() + if request is None or not request.exists(): + return + while not http.get(endpoint(card, "ready")).raise_for_status().json().get("ready"): + if time.monotonic() > deadline: + raise SystemExit(f"LibreOffice wasn't ready within {timeout_s:g} s of its server launching it") + time.sleep(READY_POLL_S) + if time.monotonic() > deadline: + raise SystemExit(f"LibreOffice's server didn't hand it {request.name} within {timeout_s:g} s") + + +def main(argv: list[str]) -> None: + request = waiting(invocations(argv)) if argv else None + try: + handed_on(request) + finally: + if request is not None: + request.unlink(missing_ok=True) + + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/src/agent_env/examples/office/README.md b/src/agent_env/examples/office/README.md new file mode 100644 index 000000000..4c5f3c6e2 --- /dev/null +++ b/src/agent_env/examples/office/README.md @@ -0,0 +1,15 @@ +A Linux desktop with LibreOffice that an agent writes Office files on through the `computer_*` tools. + +`envs/desktop/env.toml` is a computer env of the shipped `ubuntu-24.04` base computer, the `reference` driver and the +`apps` bundle's `libreoffice`, named by their aliases. A run composes it first, which takes a few minutes the first time +and is reused after. + +`tasks/probe.json` deploys it and runs the conformance probe at `full` against it, with `artifacts/check_probe/`. It +needs Docker and the local registry on `localhost:5000`, and no model. + +`tasks/author.json` has Claude Code, given the computer's skills (`add_skills` `env_ids`), write a memo as a .docx and a +two-slide presentation as a .pptx whose second slide has a list with an entrance animation, in LibreOffice, while the +display is recorded; `artifacts/check_office/` reads both files inside the computer and checks the memo's title and +paragraphs, the slides' titles and list, and the animation. It needs a model endpoint as well. + +Run them with `agent-env run office`, or one with `agent-env run office --task probe`. diff --git a/src/agent_env/examples/office/artifacts/check_office/verify.py b/src/agent_env/examples/office/artifacts/check_office/verify.py new file mode 100644 index 000000000..66b3818b2 --- /dev/null +++ b/src/agent_env/examples/office/artifacts/check_office/verify.py @@ -0,0 +1,86 @@ +"""Whether the agent saved the memo and the presentation the task asks for, in Office formats, read inside the computer +through the driver's computer-exec: the memo a .docx with its title and at least two paragraphs under it; the +presentation a .pptx of two slides titled as asked, the second with a list of three items, that list a shape an entrance +animation targets.""" +import json +import uuid + +from agentenv_protocol import client as protocol + +EXEC = "urn:agentenv:computer-exec/v1" +#: Run in the computer: what each file holds, as JSON; each slide's shapes with their paragraphs, and whether an +#: entrance animation targets them. +READ = r''' +import json, re, sys, zipfile +import xml.etree.ElementTree as ET +from pathlib import Path + +A = "{http://schemas.openxmlformats.org/drawingml/2006/main}" +P = "{http://schemas.openxmlformats.org/presentationml/2006/main}" +W = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}" + +def paragraphs(element, paragraph, run): + found = [] + for p in element.iter(paragraph): + text = "".join(t.text or "" for t in p.iter(run)).strip() + if text: + found.append(text) + return found + +def shapes(slide): + entering = {target.get("spid") for ctn in slide.iter(P + "cTn") if ctn.get("presetClass") == "entr" + for target in ctn.iter(P + "spTgt")} + found = [] + for sp in slide.iter(P + "sp"): + named = sp.find(f"{P}nvSpPr/{P}cNvPr") + shape_id = named.get("id") if named is not None else None + found.append({"paragraphs": paragraphs(sp, A + "p", A + "t"), "enters": shape_id in entering}) + return found + +def opened(name): + path = Path.home() / "Documents" / name + try: + return zipfile.ZipFile(path) + except (OSError, zipfile.BadZipFile) as e: + return f"{path} isn't there or isn't an Office file: {e}" + +out = {} +memo = opened("quarterly-plan.docx") +out["memo"] = memo if isinstance(memo, str) else { + "paragraphs": paragraphs(ET.fromstring(memo.read("word/document.xml")), W + "p", W + "t")} +deck = opened("quarterly-plan.pptx") +if isinstance(deck, str): + out["deck"] = deck +else: + slides = sorted((n for n in deck.namelist() if re.fullmatch(r"ppt/slides/slide\d+\.xml", n)), + key=lambda n: int(re.search(r"\d+", n).group())) + out["deck"] = {"slides": [shapes(ET.fromstring(deck.read(n))) for n in slides]} +json.dump(out, sys.stdout) +''' + + +async def verify(mcp_url, env_url): + card = await protocol.get_card(env_url) + ran = await protocol.invoke_extension(env_url, card, EXEC, {"argv": ["python3", "-c", READ], + "request_id": uuid.uuid4().hex}, method="run") + found = json.loads(ran["stdout"]) if ran["exit_code"] == 0 else {} + memo, deck = found.get("memo", ran.get("stderr", "")[-300:]), found.get("deck", "") + paragraphs = memo.get("paragraphs", []) if isinstance(memo, dict) else [] + titled = "Quarterly Plan" in paragraphs + under = paragraphs[paragraphs.index("Quarterly Plan") + 1:] if titled else [] + slides = deck.get("slides", []) if isinstance(deck, dict) else [] + texts = [[shape["paragraphs"] for shape in slide] for slide in slides] + second = slides[1] if len(slides) == 2 else [] + lists = [shape for shape in second if len(shape["paragraphs"]) == 3 and "Next Steps" not in shape["paragraphs"]] + titled_slides = len(slides) == 2 and any("Quarterly Plan" in shape["paragraphs"] for shape in slides[0]) and any( + shape["paragraphs"] == ["Next Steps"] for shape in second) + animated = any(shape["enters"] for shape in lists) + return [ + {"id": "memo", "result": titled and len(under) >= 2, + "description": f"the memo's paragraphs: {paragraphs}" if isinstance(memo, dict) else memo}, + {"id": "slides", "result": titled_slides and bool(lists), + "description": f"the slides' shapes' text: {texts}" if isinstance(deck, dict) else deck}, + {"id": "animation", "result": animated, + "description": "an entrance animation targets the second slide's list of three" if animated else + "no entrance animation targets a list of three on the second slide"}, + ] diff --git a/src/agent_env/examples/office/artifacts/check_probe/verify.py b/src/agent_env/examples/office/artifacts/check_probe/verify.py new file mode 100644 index 000000000..066d7cf08 --- /dev/null +++ b/src/agent_env/examples/office/artifacts/check_probe/verify.py @@ -0,0 +1,9 @@ +"""The conformance probe at full against the deployed computer: one result per check, passing when it passed or +didn't apply.""" +from agentenv_protocol.computer.conformance import run_probe + + +async def verify(mcp_url, env_url): + report = await run_probe(env_url, "full") + return [{"id": f"{check.check} {check.name}", "result": check.status in ("passed", "not_applicable"), + "description": check.detail or check.status} for check in report.checks] diff --git a/src/agent_env/examples/office/envs/desktop/env.toml b/src/agent_env/examples/office/envs/desktop/env.toml new file mode 100644 index 000000000..1f07d10ca --- /dev/null +++ b/src/agent_env/examples/office/envs/desktop/env.toml @@ -0,0 +1,4 @@ +type = "computer" +computer = "ubuntu-24.04" +driver = "reference" +apps = ["libreoffice"] diff --git a/src/agent_env/examples/office/tasks/author.json b/src/agent_env/examples/office/tasks/author.json new file mode 100644 index 000000000..24be78e6e --- /dev/null +++ b/src/agent_env/examples/office/tasks/author.json @@ -0,0 +1,11 @@ +[ + {"id": "env", "type": "deploy_env", "env_id": "desktop"}, + {"id": "agent", "type": "deploy_agent", "env_ids": ["desktop"]}, + {"id": "skills", "type": "add_skills", "env_ids": ["desktop"]}, + {"id": "record", "type": "start_computer_recording", "env_id": "desktop", "name": "agent", "depends_on": ["env"]}, + {"id": "task", "type": "prompt_agent", "depends_on": ["skills", "record"], + "prompt": "Use LibreOffice on this computer to write two files, both saved in the Documents folder. First, a memo titled Quarterly Plan, with at least two paragraphs under the title, saved as quarterly-plan.docx in Word format. Second, a presentation of two slides, saved as quarterly-plan.pptx in PowerPoint format: the first slide titled Quarterly Plan, and the second titled Next Steps, with a list of three steps that appears with an entrance animation. Use only the computer_* tools, and reply DONE once both files are saved."}, + {"id": "check", "type": "env_outcome_verifier", "env_id": "desktop", "file_artifact_id": "check_office", + "verifier_id": "office", "depends_on": ["task"]}, + {"id": "end", "type": "end_computer_recording", "name": "agent", "depends_on": ["task"]} +] diff --git a/src/agent_env/examples/office/tasks/probe.json b/src/agent_env/examples/office/tasks/probe.json new file mode 100644 index 000000000..2fd068aec --- /dev/null +++ b/src/agent_env/examples/office/tasks/probe.json @@ -0,0 +1,5 @@ +[ + {"id": "env", "type": "deploy_env", "env_id": "desktop"}, + {"id": "probe", "type": "env_outcome_verifier", "env_id": "desktop", "file_artifact_id": "check_probe", + "verifier_id": "probe"} +] diff --git a/tst/installer/test_container_journey.py b/tst/installer/test_container_journey.py index d13237c00..bfbe0172f 100644 --- a/tst/installer/test_container_journey.py +++ b/tst/installer/test_container_journey.py @@ -133,7 +133,7 @@ def test_a_new_user_installs_agent_env_adds_plugins_runs_a_task_and_removes_them container.ok("python", "-m", "pip", "install", "-q", "--no-index", "--find-links", "/wheels", "uv") container.ok("uv", "tool", "install", "agentenv-framework") rows = container.ok("agent-env", "plugin", "list").stdout.split("STATUS\n", 1)[1] - assert re.fullmatch(r"agentenv-framework +\S+ +10 bundles +ok\n", rows), rows + assert re.fullmatch(r"agentenv-framework +\S+ +12 bundles +ok\n", rows), rows added = container.ok("agent-env", "plugin", "add", browser, grader, "--yes") assert "toy_navigate active" in added.stdout and "toy_grade active" in added.stdout diff --git a/tst/integration/computer/conftest.py b/tst/integration/computer/conftest.py index 720388a17..c8803f0bf 100644 --- a/tst/integration/computer/conftest.py +++ b/tst/integration/computer/conftest.py @@ -89,6 +89,14 @@ def desktop(tmp_path_factory): yield from _composed(tmp_path_factory.mktemp("desktop"), parse_bundle(bundle.root, id_root=bundle.id_root)) +@pytest.fixture(scope="module") +def office_desktop(tmp_path_factory): + """The shipped office example's computer env, the ubuntu-24.04 base and the reference driver with the apps bundle's + libreoffice, composed as ``desktop`` is.""" + bundle = find_bundle("office") + yield from _composed(tmp_path_factory.mktemp("office"), parse_bundle(bundle.root, id_root=bundle.id_root)) + + @pytest.fixture(scope="module") def cua_desktop(tmp_path_factory): """The cua-driver test bundle's desktop, the ubuntu-24.04 base with the cua-driver driver, composed as ``desktop`` diff --git a/tst/integration/computer/libreoffice_app_test.py b/tst/integration/computer/libreoffice_app_test.py new file mode 100644 index 000000000..6b18102cb --- /dev/null +++ b/tst/integration/computer/libreoffice_app_test.py @@ -0,0 +1,181 @@ +"""The apps bundle's libreoffice on the ubuntu-24.04 base, as the office example's desktop lists it: gate 3 makes this +machine's arch ready with it; deployed, it isn't launched until it's opened, and the fonts with Office's metrics stand +in for Office's; a .docx, a .pptx, an .xlsx and a .csv opened by their type, as the file manager opens them, each open +in their app in the one copy its server launched, asking nothing, with no lock file beside them, and a .docx saves in +its own format without asking; its menu launcher, run while it runs, goes to that copy, starting no other; and a reset +of its data stops it, takes its profile back to none and launches it again, as quiet. Needs Docker and the local +registry on localhost:5000.""" + +import asyncio +import platform +import re + +import httpx +import pytest +from mcp import ClientSession +from mcp.client.streamable_http import streamablehttp_client + +from agentenv_protocol import client as protocol_v1 +from agentenv_protocol.computer import apps +from agentenv_protocol.types import WELL_KNOWN_PATH + +from agent_env.env import Env +from agent_env.env.envs.computer.utils import inside +from agent_env.store.routing import namespace_routing +from agent_env.task.teardown import teardown_run +from agent_env.task_step.context import TaskStepContext + +pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] + +ARCH = {"x86_64": "amd64", "amd64": "amd64", "aarch64": "arm64", "arm64": "arm64"}[platform.machine().lower()] +FOLDER = "/opt/agentenv/apps/libreoffice" +LAUNCHER = f"{FOLDER}/venv/bin/python -m agentenv_protocol.computer.launch {FOLDER}" +#: The fonts each of Office's resolves to. +FONTS = {"Calibri": "Carlito", "Cambria": "Caladea", "Arial": "Liberation Sans", "Times New Roman": "Liberation Serif", + "Courier New": "Liberation Mono"} +#: Office files made in the computer with LibreOffice itself, headless, before its app is launched: a .docx and an +#: .xlsx converted from text, a .pptx imported from a PDF of that text, and the CSV itself. +MAKE_FILES = ("cd ~/Documents && printf 'Agenda\\nFirst item\\n' > memo.txt && printf 'name,amount\\nrent,1200\\n' > " + "costs.csv && soffice --headless --convert-to docx memo.txt && soffice --headless --convert-to xlsx " + "costs.csv && soffice --headless --convert-to pdf memo.txt && soffice --headless " + "--infilter=impress_pdf_import --convert-to pptx memo.pdf && rm memo.txt memo.pdf && ls") +#: Each file opened, and the class of the window it opens in: the memo last, so its window is the one on top when it's +#: typed in. +OPENS = {"costs.csv": "libreoffice-calc", "costs.xlsx": "libreoffice-calc", "memo.pptx": "libreoffice-impress", + "memo.docx": "libreoffice-writer"} +#: Each top-level window: its WM_CLASS instance and class, and its title. +WINDOWS = ('for w in $(xprop -root _NET_CLIENT_LIST | sed "s/.*# //; s/,//g"); do ' + 'xprop -id "$w" WM_CLASS _NET_WM_NAME | tr "\\n" " "; echo; done') +WINDOW_LINE = r'WM_CLASS\(STRING\) = "([^"]*)", "([^"]*)"\s*_NET_WM_NAME\(UTF8_STRING\) = "(.*)"' +PROCESSES = "pgrep -c -x soffice.bin || true" +#: A file in its profile, which a reset of its data takes away. +MARKER = ".config/libreoffice/agentenv-marker" + + +def test_gate_3_makes_this_machines_arch_ready_with_libreoffice(office_desktop): + results = office_desktop.metadata["composed"]["container"] + + assert results[ARCH]["status"] == "ready" and results[ARCH]["probe"]["passed"], results[ARCH] + assert [app.environment_name for app in office_desktop.composed_apps()] == ["libreoffice"] + + +@pytest.mark.asyncio +async def test_office_files_open_by_type_in_the_copy_its_server_launched_asking_nothing_and_a_reset_brings_it_back( + office_desktop): + with namespace_routing(): + deployed = await Env.get(office_desktop.id).deploy() + context = TaskStepContext(deployed_envs=[deployed]) + try: + card = await _app(deployed, WELL_KNOWN_PATH) + before = await _app(deployed, apps.endpoint(card, "ready")) + fonts = {office: (await _run(deployed, ["fc-match", "-f", "%{family}", office])).split(",")[0] + for office in FONTS} + made = (await _run(deployed, ["sh", "-c", MAKE_FILES])).split() + opened = {} + for name, cls in OPENS.items(): + await _run(deployed, ["exo-open", f"Documents/{name}"], timeout_s=120) + opened[name] = await _window_of(deployed, cls, name) + asked = await _dialogs(deployed) + copies = await _run(deployed, ["sh", "-c", PROCESSES]) + ready = await _app(deployed, apps.endpoint(card, "ready")) + locks = await _run(deployed, ["sh", "-c", "ls -a Documents | grep -c '^\\.~lock' || true"]) + saved_before = await _run(deployed, ["stat", "-c", "%Y", "Documents/memo.docx"]) + await _typed_and_saved(deployed, "libreoffice-writer", " and more") + saved_after = await _run(deployed, ["stat", "-c", "%Y", "Documents/memo.docx"]) + asked_on_save = await _dialogs(deployed) + await _run(deployed, ["sh", "-c", LAUNCHER]) + copies_after_launcher = await _run(deployed, ["sh", "-c", PROCESSES]) + ready_after_launcher = await _app(deployed, apps.endpoint(card, "ready")) + await _run(deployed, ["touch", MARKER]) + await protocol_v1.reset_data(f"{deployed.environment_url}/svc/libreoffice", + headers=deployed.control_headers) + relaunched = await _ready(deployed, card) + marker = await _run(deployed, ["sh", "-c", f"test -e {MARKER} && echo kept || echo gone"]) + copies_after_reset = await _run(deployed, ["sh", "-c", PROCESSES]) + await _run(deployed, ["exo-open", "Documents/memo.docx"], timeout_s=120) + reopened = await _window_of(deployed, "libreoffice-writer", "memo.docx") + asked_after_reset = await _dialogs(deployed) + finally: + report = await teardown_run(context) + + assert before["launched"] is False and "not launched yet" in before["problems"], before + assert fonts == FONTS, fonts + assert {"memo.docx", "memo.pptx", "costs.xlsx", "costs.csv"} <= set(made), made + assert all(opened.values()), opened + assert asked == [] and asked_on_save == [] and asked_after_reset == [], (asked, asked_on_save, asked_after_reset) + assert copies == "1" and copies_after_launcher == "1" and ready["ready"], (copies, copies_after_launcher, ready) + assert locks == "0" and int(saved_after) > int(saved_before) + assert ready_after_launcher["ready"] and relaunched["ready"] and reopened, (ready_after_launcher, relaunched, + reopened) + assert marker == "gone" and copies_after_reset == "1", (marker, copies_after_reset) + assert not report.failed + + +async def _run(deployed, argv: list[str], timeout_s: float = 60) -> str: + ran = await inside.run(deployed, argv, timeout_s=timeout_s) + assert ran["exit_code"] == 0, ran + return ran["stdout"].strip() + + +async def _windows(deployed) -> list[tuple[str, str, str]]: + """Each top-level window's WM_CLASS instance and class, and its title.""" + listed = await _run(deployed, ["sh", "-c", WINDOWS]) + found = [] + for line in listed.splitlines(): + if match := re.search(WINDOW_LINE, line): + found.append(match.groups()) + return found + + +async def _window_of(deployed, cls: str, title: str, within_s: float = 60) -> str | None: + """The title of a window of ``cls`` whose title holds ``title``, once one shows.""" + for _ in range(int(within_s)): + if found := [name for _, klass, name in await _windows(deployed) if klass == cls and title in name]: + return found[0] + await asyncio.sleep(1) + return None + + +async def _dialogs(deployed) -> list[str]: + """The titles of LibreOffice's dialogs showing, a second after the last action: its dialogs are soffice's windows, + its documents libreoffice's.""" + await asyncio.sleep(1) + return [name for instance, _, name in await _windows(deployed) if instance == "soffice"] + + +async def _typed_and_saved(deployed, cls: str, text: str) -> None: + """``text`` typed at the end of the largest window of ``cls`` and Ctrl+S pressed, through the computer's tools, as + an agent types and saves.""" + tree = await _run(deployed, ["xwininfo", "-root", "-tree"]) + windows = [(int(w) * int(h), int(x) + int(w) // 2, int(y) + int(h) // 2) for w, h, x, y in re.findall( + rf'\("libreoffice" "{cls}"\)\s+(\d+)x(\d+)\+-?\d+\+-?\d+\s+\+(-?\d+)\+(-?\d+)', tree)] + assert windows, tree + _, x, y = max(windows) + async with streamablehttp_client(f"{deployed.environment_url}/mcp") as (read, write, _): + async with ClientSession(read, write) as session: + await session.initialize() + await session.call_tool("computer_screenshot", {}) + for call, args in (("computer_click", {"x": x, "y": y}), ("computer_key", {"keys": ["Control", "End"]}), + ("computer_type", {"text": text}), ("computer_key", {"keys": ["Control", "s"]})): + done = await session.call_tool(call, args) + assert not done.isError, done + await asyncio.sleep(0.5) + await asyncio.sleep(2) + + +async def _app(deployed, path: str) -> dict: + async with httpx.AsyncClient(timeout=30) as http: + response = await http.get(f"{deployed.environment_url}/svc/libreoffice{path}", headers=deployed.control_headers) + response.raise_for_status() + return response.json() + + +async def _ready(deployed, card: dict) -> dict: + """The app's readiness once it says it's ready, or as it last said within a minute.""" + for _ in range(60): + readiness = await _app(deployed, apps.endpoint(card, "ready")) + if readiness["ready"]: + return readiness + await asyncio.sleep(1) + return readiness + diff --git a/tst/integration/computer/office_example_test.py b/tst/integration/computer/office_example_test.py new file mode 100644 index 000000000..0fed79ff0 --- /dev/null +++ b/tst/integration/computer/office_example_test.py @@ -0,0 +1,133 @@ +"""The shipped office example: its check, run against a computer of its desktop, says each file isn't there before +any is, fails the animation of a deck whose list doesn't enter, with no animation or with its title's alone, and passes +the memo and deck LibreOffice saves in Office formats, converted headless from text and from a flat ODP whose second +slide's list enters with an animation; and an agent given LibreOffice's skill writes both through the computer_* tools +under a recording. Needs Docker and the local registry; the agent's task, a model endpoint too.""" + +import importlib.util +import re + +import pytest +from click.testing import CliRunner + +from agentenv_protocol import client as protocol_v1 + +from agent_env.cli import cli +from agent_env.env import Env +from agent_env.env.envs.computer.utils import inside +from agent_env.bundle.installed import find_bundle +from agent_env.store.routing import namespace_routing +from agent_env.task.teardown import teardown_run +from agent_env.task_step.context import TaskStepContext +from tst.integration.computer.conftest import run_context +from tst.util.capabilities import skip_without_model_endpoint + +pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] + +MEMO = "Quarterly Plan\nWe grow the team this quarter.\nWe ship the new release by March.\n" +#: The deck as a flat ODP: two slides titled as the task asks, the second's list of three entering with an animation. +DECK = """ + + + + + Quarterly Plan + + + Next Steps + + HireShip + Review + + + + + + + + +""" +#: The same deck with its second slide's title entering instead of its list. +TITLE_ANIMATED = DECK.replace('smil:targetElement="steps"', 'smil:targetElement="heading"') +UNANIMATED = re.sub(r'.*?', "", DECK, flags=re.S) +WRITE = ("import pathlib, sys; path = pathlib.Path(sys.argv[1]); path.parent.mkdir(parents=True, exist_ok=True); " + "path.write_text(sys.argv[2])") +#: Each file converted into Documents, headless, under the name the task asks for. +CONVERT = ("cd /tmp/office && soffice --headless --convert-to docx --outdir ~/Documents " + "quarterly-plan.txt && soffice --headless --convert-to pptx --outdir ~/Documents quarterly-plan.fodp") + + +@pytest.mark.asyncio +async def test_the_check_names_what_isnt_there_or_isnt_animated_and_passes_what_libreoffice_saves(office_desktop): + verify = _verify() + with namespace_routing(): + deployed = await Env.get(office_desktop.id).deploy() + context = TaskStepContext(deployed_envs=[deployed]) + try: + with protocol_v1.authorized({deployed.environment_url: deployed.control_headers}): + missing = await verify(f"{deployed.environment_url}/mcp", deployed.environment_url) + await _write(deployed, "/tmp/office/quarterly-plan.txt", MEMO) + await _write(deployed, "/tmp/office/quarterly-plan.fodp", UNANIMATED) + await _run(deployed, ["sh", "-c", CONVERT]) + unanimated = await verify(f"{deployed.environment_url}/mcp", deployed.environment_url) + await _write(deployed, "/tmp/office/quarterly-plan.fodp", TITLE_ANIMATED) + await _run(deployed, ["sh", "-c", CONVERT]) + title_animated = await verify(f"{deployed.environment_url}/mcp", deployed.environment_url) + await _write(deployed, "/tmp/office/quarterly-plan.fodp", DECK) + await _run(deployed, ["sh", "-c", CONVERT]) + saved = await verify(f"{deployed.environment_url}/mcp", deployed.environment_url) + finally: + report = await teardown_run(context) + + assert {result["id"]: result["result"] for result in missing} == {"memo": False, "slides": False, + "animation": False}, missing + assert "quarterly-plan.docx isn't there" in missing[0]["description"], missing + assert {result["id"]: result["result"] for result in unanimated} == {"memo": True, "slides": True, + "animation": False}, unanimated + assert {result["id"]: result["result"] for result in title_animated} == {"memo": True, "slides": True, + "animation": False}, title_animated + assert all(result["result"] for result in saved), saved + assert not report.failed + + +@skip_without_model_endpoint() +def test_an_agent_given_libreoffices_skill_writes_the_memo_and_the_animated_deck_under_a_recording(state): + ran = CliRunner().invoke(cli, ["run", "office", "--task", "author"]) + + assert ran.exit_code == 0, ran.output + assert "passed (check: 1)" in ran.output, ran.output + context = run_context(ran.output) + entry = context["metadata"]["computer_recordings"]["agent"] + assert (entry["state"], entry["missing_segments"]) == ("ended", 0) + skills = context["metadata"]["env_skills"]["default-agent"]["desktop"] + assert any("Saving in Office formats" in skill for skill in skills.values()), list(skills) + + +def _verify(): + """The office example's check, loaded from its bundle as the verifier step loads it.""" + path = find_bundle("office").root / "artifacts" / "check_office" / "verify.py" + spec = importlib.util.spec_from_file_location("check_office", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module.verify + + +async def _write(deployed, path: str, text: str) -> None: + await _run(deployed, ["python3", "-c", WRITE, path, text]) + + +async def _run(deployed, argv: list[str]) -> str: + ran = await inside.run(deployed, argv, timeout_s=120) + assert ran["exit_code"] == 0, ran + return ran["stdout"] diff --git a/tst/unit/bundle/installed_test.py b/tst/unit/bundle/installed_test.py index f20354e69..644d08a2f 100644 --- a/tst/unit/bundle/installed_test.py +++ b/tst/unit/bundle/installed_test.py @@ -127,10 +127,10 @@ def test_the_plugin_report_counts_the_bundles_agent_env_ships(monkeypatch, quiet assert result.exit_code == 0, result.output (core,) = json.loads(result.stdout)["plugins"] assert [(c["group"], c["name"], c["status"]) for c in core["contributions"]] == [ - (BUNDLES, "advance-to-next-event", "active"), (BUNDLES, "agents", "active"), (BUNDLES, "browsers", "active"), - (BUNDLES, "computers", "active"), (BUNDLES, "desktop", "active"), (BUNDLES, "drivers", "active"), - (BUNDLES, "echo-mcp", "active"), (BUNDLES, "hello", "active"), (BUNDLES, "mail-mcp", "active"), - (BUNDLES, "next-env-event", "active")] + (BUNDLES, "advance-to-next-event", "active"), (BUNDLES, "agents", "active"), (BUNDLES, "apps", "active"), + (BUNDLES, "browsers", "active"), (BUNDLES, "computers", "active"), (BUNDLES, "desktop", "active"), + (BUNDLES, "drivers", "active"), (BUNDLES, "echo-mcp", "active"), (BUNDLES, "hello", "active"), + (BUNDLES, "mail-mcp", "active"), (BUNDLES, "next-env-event", "active"), (BUNDLES, "office", "active")] assert "\n agent-env itself\n" in shown and "requires:" not in shown diff --git a/tst/unit/bundle/shipped_bundles_test.py b/tst/unit/bundle/shipped_bundles_test.py index 666041e8d..7e8399df6 100644 --- a/tst/unit/bundle/shipped_bundles_test.py +++ b/tst/unit/bundle/shipped_bundles_test.py @@ -26,8 +26,8 @@ def _shipped() -> dict: def test_agentenv_framework_ships_its_bundles(): - assert SHIPPED == ["advance-to-next-event", "agents", "browsers", "computers", "desktop", "drivers", "echo-mcp", "hello", - "mail-mcp", "next-env-event"] + assert SHIPPED == ["advance-to-next-event", "agents", "apps", "browsers", "computers", "desktop", "drivers", "echo-mcp", + "hello", "mail-mcp", "next-env-event", "office"] @pytest.mark.parametrize("name", SHIPPED) @@ -54,7 +54,7 @@ def test_the_shipped_aliases_are_agent_envs_alone_and_claude_code_names_the_defa -def test_the_shipped_env_aliases_name_the_mail_env_the_validator_deploys_the_drivers_and_the_browsers(): +def test_the_shipped_env_aliases_name_the_mail_env_the_validator_deploys_the_drivers_the_browsers_and_the_apps(): table = installed_aliases(tuple(_shipped().values()), ENV_ALIASES) assert table.refused == {} and table.refused_claims == {} @@ -65,7 +65,8 @@ def test_the_shipped_env_aliases_name_the_mail_env_the_validator_deploys_the_dri "macos-harness": "@local/agentenv-framework/drivers/macos-harness", "codex-computer-use": "@local/agentenv-framework/drivers/codex-computer-use", "aside": "@local/agentenv-framework/drivers/aside", - "chromium": "@local/agentenv-framework/browsers/chromium"} + "chromium": "@local/agentenv-framework/browsers/chromium", + "libreoffice": "@local/agentenv-framework/apps/libreoffice"} assert A2AAgent.VALIDATION_ENV_ID == "mail" def test_a_task_naming_no_agent_plans_with_the_shipped_default(tmp_path, monkeypatch, local_stores, cli_routing): diff --git a/tst/unit/env/envs/test_libreoffice_app.py b/tst/unit/env/envs/test_libreoffice_app.py new file mode 100644 index 000000000..a9f4863c8 --- /dev/null +++ b/tst/unit/env/envs/test_libreoffice_app.py @@ -0,0 +1,189 @@ +"""The apps bundle's libreoffice as a put reads it: the installed ``apps`` bundle names it ``libreoffice``; its class +declares Writer, Impress and Calc from apt with GTK3 and the fonts with Office's metrics, its profile as its data, its +skill, and a launch on demand of the Start Center, ready once any LibreOffice window shows; its install step writes its +defaults at the series installed and each LibreOffice desktop entry in its place, opening through ``open.py``; and +``open.py`` leaves what it opens for the server's launch, a CSV with its own filter, launching again once the app is +ready when the launch had to start it, and the server's launch hands what waits to the copy that runs.""" +import asyncio +import json +import os +import sys +from contextlib import contextmanager + +import pytest + +from agentenv_protocol.computer._load import load_class +from agentenv_protocol.computer.app import ComputerAppEnvironment + +from agent_env.bundle import parse_bundle +from agent_env.bundle.installed import find_bundle + +FOLDER = find_bundle("apps").root / "envs" / "libreoffice" + + +@pytest.fixture +def libreoffice(): + sys.modules.pop("libreoffice", None) + cls = load_class(FOLDER / "libreoffice.py", "LibreOffice", ComputerAppEnvironment) + yield sys.modules[cls.__module__] + sys.modules.pop("libreoffice", None) + + +@pytest.fixture +def opener(): + sys.modules.pop("libreoffice_open", None) + import importlib.util + spec = importlib.util.spec_from_file_location("libreoffice_open", FOLDER / "open.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + yield module + + +def test_the_apps_bundle_names_its_libreoffice_app_libreoffice(): + bundle = find_bundle("apps") + parsed = parse_bundle(bundle.root, id_root=bundle.id_root) + + assert [(entry.id, entry.type) for entry in parsed.entries] == [("@local/agentenv-framework/apps/libreoffice", + "computer_app")] + assert parsed.aliases == {"envs": {"libreoffice": ("libreoffice",)}} + + +def test_it_declares_its_packages_profile_skill_and_a_launch_on_demand_ready_on_any_of_its_windows(libreoffice): + declared = libreoffice.LibreOffice.declaration() + linux = declared.os["linux"] + + assert {"libreoffice-writer", "libreoffice-impress", "libreoffice-calc", "libreoffice-gtk3", + "fonts-crosextra-carlito", "fonts-crosextra-caladea", "fonts-liberation2"} == set( + linux.install["packages"]["apt"]) + assert linux.paths == [".config/libreoffice"] and linux.skill.file == "SKILL.md" + assert (linux.launch.at, linux.launch.command, linux.launch.window) == ( + "demand", ["/usr/lib/libreoffice/program/soffice", "--nologo"], {"instance": "libreoffice"}) + assert linux.launch.ready.window + assert [(step.name, step.scope) for step in declared.install_steps] == [("configure", "system")] + assert declared.supports == [{"os": {"type": "linux", "display": ["x11"]}, "arch": ["amd64", "arm64"]}] + + +def test_its_install_step_writes_its_defaults_at_the_series_installed_and_points_each_launcher_at_open( + libreoffice, tmp_path, monkeypatch): + entries, overrides, registry = tmp_path / "share", tmp_path / "local", tmp_path / "registry" + entries.mkdir() + registry.mkdir() + (entries / "libreoffice-writer.desktop").write_text( + "[Desktop Entry]\nName=LibreOffice Writer\nExec=libreoffice --writer %U\nMimeType=application/msword;\n" + "[Desktop Action NewDocument]\nExec=libreoffice --writer\n") + (entries / "other.desktop").write_text("[Desktop Entry]\nExec=other\n") + ran = [] + + def run(argv, **kwargs): + ran.append(argv) + return type("Ran", (), {"stdout": "4:25.8.1-0ubuntu1"})() + + monkeypatch.setattr(libreoffice, "DESKTOP_ENTRIES", entries) + monkeypatch.setattr(libreoffice, "OVERRIDES", overrides) + monkeypatch.setattr(libreoffice, "REGISTRY", registry) + monkeypatch.setattr(libreoffice.subprocess, "run", run) + monkeypatch.chdir(FOLDER) + + libreoffice.LibreOffice.configure(object.__new__(libreoffice.LibreOffice)) + + opener = f"{FOLDER}/venv/bin/python {FOLDER}/open.py" + assert (overrides / "libreoffice-writer.desktop").read_text() == ( + f"[Desktop Entry]\nName=LibreOffice Writer\nExec={opener} --writer %U\nMimeType=application/msword;\n" + f"[Desktop Action NewDocument]\nExec={opener} --writer\n") + assert sorted(path.name for path in overrides.iterdir()) == ["libreoffice-writer.desktop"] + defaults = (registry / "agentenv.xcd").read_text() + shipped = (FOLDER / "agentenv.xcd").read_text() + assert '25.8' in defaults + assert defaults == shipped.replace('"ooSetupLastVersion" oor:op="fuse">24.2<', + '"ooSetupLastVersion" oor:op="fuse">25.8<') != shipped + assert ran[-1] == ["update-desktop-database", str(overrides)] + + +@pytest.mark.parametrize("argv, calls", [ + ([], [[]]), + (["--writer"], [["--writer"]]), + (["a.docx", "file:///b.pptx"], [["{cwd}/a.docx", "file:///b.pptx"]]), + (["costs.CSV"], [["--infilter=CSV:44,34,76,1", "{cwd}/costs.CSV"]]), + (["a.docx", "/data/costs.csv"], [["{cwd}/a.docx"], ["--infilter=CSV:44,34,76,1", "/data/costs.csv"]]), +]) +def test_each_file_opens_by_its_absolute_path_a_csv_on_its_own_with_its_filter_after_the_rest(opener, tmp_path, + monkeypatch, argv, + calls): + monkeypatch.chdir(tmp_path) + + assert opener.invocations(argv) == [[arg.format(cwd=os.getcwd()) for arg in call] for call in calls] + + +@pytest.mark.parametrize("argv, left", [(["/d/a.docx", "/d/costs.csv"], [["/d/a.docx"], [ + "--infilter=CSV:44,34,76,1", "/d/costs.csv"]]), ([], None)]) +def test_it_leaves_what_it_opens_for_the_servers_launch_and_takes_it_back_after(opener, tmp_path, monkeypatch, argv, + left): + handed = [] + monkeypatch.setattr(opener, "OPENS", tmp_path / "opens") + monkeypatch.setattr(opener, "handed_on", lambda request: handed.append( + request and (request.parent, json.loads(request.read_text())))) + + opener.main(argv) + + assert handed == [left and (tmp_path / "opens", left)] + assert not list((tmp_path / "opens").glob("*")) if left else not (tmp_path / "opens").exists() + + +@pytest.mark.parametrize("running, posts", [(True, 1), (False, 2)]) +def test_it_launches_again_once_the_app_is_ready_when_the_launch_had_to_start_it(opener, tmp_path, monkeypatch, + running, posts): + request = tmp_path / "1-a.json" + request.write_text("[]") + asked = [] + + class Http: + def get(self, path): + asked.append(path) + return _answer({"capabilities": {}} if path == "/.well-known/agent-env.json" else {"ready": True}) + + def post(self, path, json): + asked.append(f"POST {path}") + if running or asked.count(f"POST {path}") == 2: + request.unlink() + return _answer({"pid": 7}) + + @contextmanager + def client(timeout): + yield Http() + + monkeypatch.setattr(opener, "served", lambda folder: type("Served", (), {"sync_client": staticmethod(client)})()) + monkeypatch.setattr(opener, "endpoint", lambda card, op: f"/{op}") + + opener.handed_on(request) + + assert [path for path in asked if path.startswith("POST")] == ["POST /launch"] * posts + assert ("/ready" in asked) is (not running) + + +def test_the_servers_launch_hands_what_waits_oldest_first_to_the_copy_that_runs_or_launches_it_bare( + libreoffice, opener, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + (tmp_path / libreoffice.OPENS).mkdir(parents=True) + (tmp_path / libreoffice.OPENS / "2-b.json").write_text(json.dumps([["--infilter=CSV:44,34,76,1", "/d/c.csv"]])) + (tmp_path / libreoffice.OPENS / "1-a.json").write_text(json.dumps([["/d/a.docx"]])) + (tmp_path / libreoffice.OPENS / "3-c.staged").write_text("[") + ran = [] + + async def exec_(*argv): + ran.append(list(argv)) + return type("Handed", (), {"wait": staticmethod(lambda: asyncio.sleep(0))})() + + monkeypatch.setattr(libreoffice.asyncio, "create_subprocess_exec", exec_) + hand_on = object.__new__(libreoffice.LibreOffice)._launch_running() + + asyncio.run(hand_on()) + asyncio.run(hand_on()) + + soffice = [libreoffice.SOFFICE, "--nologo"] + assert ran == [[*soffice, "/d/a.docx"], [*soffice, "--infilter=CSV:44,34,76,1", "/d/c.csv"], soffice] + assert [path.name for path in (tmp_path / libreoffice.OPENS).iterdir()] == ["3-c.staged"] + assert opener.OPENS == FOLDER / libreoffice.OPENS + + +def _answer(body: dict): + return type("Answer", (), {"raise_for_status": lambda self: self, "json": lambda self: body})() diff --git a/tst/unit/test_clean_install_wheel.py b/tst/unit/test_clean_install_wheel.py index 44522dbdd..4ad962954 100644 --- a/tst/unit/test_clean_install_wheel.py +++ b/tst/unit/test_clean_install_wheel.py @@ -68,12 +68,13 @@ def test_the_gate_checks_the_folder_of_every_shipped_bundle(): roots = {name: root.relative_to(SCRIPT.parents[2]).as_posix() for name, root in clean_install.bundle_roots().items()} assert roots == {"advance-to-next-event": "src/agent_env/examples/advance-to-next-event", - "agents": "src/agent_env/agents", "browsers": "src/agent_env/browsers", - "computers": "src/agent_env/computers", + "agents": "src/agent_env/agents", "apps": "src/agent_env/apps", + "browsers": "src/agent_env/browsers", "computers": "src/agent_env/computers", "desktop": "src/agent_env/examples/desktop", "drivers": "src/agent_env/drivers", "echo-mcp": "src/agent_env/examples/echo-mcp", "hello": "src/agent_env/examples/hello", "mail-mcp": "src/agent_env/examples/mail-mcp", - "next-env-event": "src/agent_env/examples/next-env-event"} + "next-env-event": "src/agent_env/examples/next-env-event", + "office": "src/agent_env/examples/office"} def test_the_checkout_makes_each_published_image_as_the_installed_sources_do_when_they_are_its_own(monkeypatch):