# Creating your own computer app environment (https://www.agentenvframework.com/docs/environments/computers/creating)

> Create a computer app environment: the app environment is an atomic application that can run inside a CUA Environment

A computer app is a `computer_app` env: software and its data, built into a computer's image beside
the driver. Its class, a `ComputerAppEnvironment` from the `agentenv-framework-protocol` package,
says how to install the app, launch it and find its data.

|                | MCP environment                   | Computer app                         |
| -------------- | --------------------------------- | ------------------------------------ |
| The agent uses | its tools                         | its window, through the driver       |
| It runs        | in a container behind the gateway | inside a computer, beside the driver |
| It deploys     | alone, or in a multi env          | in a computer env's `apps`, or alone |

1. `LibreOffice` declares its platforms, its apt packages, how it launches and `~/Office`, the folder its data lives in.
2. Writing the office desktop checks the app fits `ubuntu-24.04` and `cua-driver`, then resolves every package and stores each file by sha256.
3. The build installs only those files, offline, with LibreOffice in `/opt/agentenv/apps/libreoffice/`, beside Chromium’s folder and cua-driver’s.
4. At boot the app’s server runs `open.py`, and the computer is ready once LibreOffice’s and Chromium’s windows show.
5. `load_artifact` sends the figures to the app’s `data/add`, which writes them to `~/Office` and relaunches Calc on them.
6. An agent sees both windows through cua-driver, with `computer_screenshot` or cua-driver’s own tools, such as `list_windows`.

Parts of the scene:

- **@computer_app**: The platforms the app runs on, by OS type, display and arch. A computer whose base matches none is refused when it’s written.
- **@linux**: How a Linux computer installs the app. Here, four apt packages: Calc, Writer, its GTK 3 integration and the Colibre icons.
- **Launch**: How the app starts in the desktop session: `open.py`, run at boot with the venv every app gets.
- **window**: Matches the app’s window by its X11 class, instance or title. LibreOffice’s windows have the instance `libreoffice`.
- **Ready**: The app is ready once its window shows. LibreOffice gets 120 seconds instead of the default 60.
- **paths**: The folders the app keeps its data in, under the session user’s home. They serve its data plane: `data/add`, `data/get` and `data/reset`.
- **skill**: A file that tells an agent how to use the app. `add_skills` installs it under the app’s name.
- **Settings**: Values a deploy gives the app, read as `self.settings`. The office desktop sets `open` to `figures.csv`.
- **@install_step**: Runs at build, offline, here as root. `defaults` installs `defaults.xcd`, which turns off LibreOffice’s first-run screens and questions.
- **The app’s folder**: A `computer_app` env: its `env.toml` names the class, `libreoffice.py:LibreOffice`, which sits beside its launch, defaults and skill.
- **The object store**: Keeps every package a computer installs, by sha256, under `agentenv-packages/`. A lock lists them, and equal inputs reuse it.
- **The image store**: Holds the computer’s image, `agentenv-computers:<key>`, keyed by its inputs. Any env with the same inputs reuses it.
- **The office desktop**: A computer env of `ubuntu-24.04`, `cua-driver` and two apps, here in a container on local Docker.
- **The app’s folder in the image**: Its files under `/opt/agentenv/apps/libreoffice/`, with a venv for its server, beside Chromium’s folder and the driver’s.
- **Chromium**: The browser app agent-env ships, on the same computer. The office desktop opens it on these docs.
- **The app’s own server**: Runs from the app’s folder: it launches the app, says whether it’s ready and serves its skill and data plane.
- **LibreOffice Calc**: What `open.py` starts: a new spreadsheet at boot, then `figures.csv` once the load writes it.
- **cua-driver**: Serves the screen to agents over MCP at `/mcp`: the 7 `computer_*` tools and 66 of its own.
- **computer-ready**: Every app’s server must answer and every app launched at boot must be ready before the deploy hands the computer over.
- **The screen**: The desktop session, XFCE on Xvfb at 1280×800, which agents see and act on through the driver.
- **load_artifact**: Loads the `figures` artifact, named for `libreoffice`, into the computer. The framework finds the app on the card and calls its `data/add`.
- **The agent**: Reaches the computer at `/mcp` and sees the apps only through the screen and cua-driver’s tools.

## The app

This page writes `libreoffice`: Calc and Writer from Ubuntu's archive, with documents in `~/Office`.
[Composing](https://www.agentenvframework.com/docs/environments/computers/composing.md#apps) puts it on an office desktop with
Chromium and `cua-driver`. The app is a folder of a bundle, here `office`. To start a bundle, copy
the shipped `desktop` one: `agent-env run` with no bundle prints where it lives.

```text title="envs/libreoffice/"
env.toml        the env
libreoffice.py  the class
open.py         the launch
defaults.xcd    LibreOffice's defaults
skill.md        the skill for agents
```

![The office desktop on Ubuntu 24.04: LibreOffice Calc on figures.csv with rows Q1 to Q4 on the left, Chromium on the AgentEnv Framework docs on the right](https://www.agentenvframework.com/environments/computers/office-desktop.png)

*The office desktop from `computer_screenshot`, its windows set side by side, after a scripted run
typed the Q4 row.*

## Write it

```python title="envs/libreoffice/libreoffice.py"
"""LibreOffice Calc and Writer from Ubuntu's archive. It keeps its documents in ~/Office and opens the one its `open`
setting names when it launches."""
import shutil

from pydantic import BaseModel

from agentenv_protocol.computer import (ComputerAppEnvironment, Launch, Packages, Ready, computer_app, install_step,
                                        linux)

PACKAGES = ["libreoffice-calc", "libreoffice-writer", "libreoffice-gtk3", "libreoffice-style-colibre"]


@computer_app(supports=[{"os": {"type": "linux", "display": ["x11"]}, "arch": ["amd64", "arm64"]}])
@linux(packages=Packages(apt=PACKAGES),
       launch=Launch(command=["venv/bin/python", "open.py"], window={"instance": "libreoffice"},
                     ready=Ready(window=True, timeout_s=120)),
       paths=["Office"])
class LibreOffice(ComputerAppEnvironment):
    skill = "skill.md"

    class Settings(BaseModel):
        open: str = ""

    @install_step(scope="system")
    def defaults(self) -> None:
        shutil.copy("defaults.xcd", "/etc/libreoffice/registry/agentenv.xcd")
```

`@computer_app` names the platforms the app runs on. `@linux` says how a Linux computer installs and
launches it. Writing the bundle reads the class in a process of its own, so the app's dependencies
needn't be installed. An app that doesn't fit its computer is refused then, with every problem at
once.

```toml title="envs/libreoffice/env.toml"
type = "computer_app"
module = "libreoffice.py:LibreOffice"
computer = "ubuntu-24.04"
driver = "cua-driver"
```

| Key                  | Default           | Meaning                                                                        |
| -------------------- | ----------------- | ------------------------------------------------------------------------------ |
| `type`               | required          | `"computer_app"`                                                               |
| `module`             | required          | The class, as `"<file>:<class>"`                                               |
| `environment_name`   | the folder's name | Names its settings, data, skill and folder in the image                        |
| `computer`, `driver` | none              | The base and driver to [deploy it alone](#deploy-it-alone) on; both or neither |
| `settings`           | none              | Defaults for its `Settings`                                                    |
| `build_settings`     | none              | Values of its `BuildSettings`, fixed in the image                              |

## Install it

`@linux` takes these fields. `libreoffice` uses `packages`, `launch` and `paths`.

| Field                        | Default | Meaning                                                                             |
| ---------------------------- | ------- | ----------------------------------------------------------------------------------- |
| `files`                      | `"."`   | The folder of the app's files copied into the image                                 |
| `packages`                   | none    | `Packages(apt=[...])`: Ubuntu packages                                              |
| `sources`                    | none    | `AptSource(...)`: another apt repository and its key                                |
| `python`                     | none    | A requirements file for the app's venv                                              |
| `downloads`                  | none    | `Download(url=..., sha256=..., to=...)`                                             |
| `run`                        | none    | `RunScript(script=...)`: a script run as root, or as the session user               |
| `services`                   | none    | `Service(name=..., command=...)`: programs the computer keeps running               |
| `ports`, `shm_mb`            | none    | Ports it listens on; shared memory it needs, in MiB                                 |
| `launch`, `paths`, `skill`   | none    | [Launch it](#launch-it), [the data plane](#the-data-plane), [the skill](#the-skill) |
| `settings`, `build_settings` | none    | Fields added to the class's on Linux                                                |

Writing the bundle resolves every package in a container of the base,
stores each file in the object store by sha256 and lists them in a lock. The build installs only
the lock's files, offline. The app lands in `/opt/agentenv/apps/libreoffice/`, with a venv for its
own server, which the app may use too.

An `@install_step` method does what the fields can't. It runs at build, offline, as root, or as the
session user with `scope="session"`. `defaults` installs this file, which turns off LibreOffice's
first-run screens, lock files, save-format question and document recovery:

```xml title="envs/libreoffice/defaults.xcd"
<?xml version="1.0" encoding="UTF-8"?>
<oor:data xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oor="http://openoffice.org/2001/registry">
<dependency file="main"/>
<oor:component-data oor:name="Common" oor:package="org.openoffice.Office">
 <node oor:name="Misc">
  <prop oor:name="ShowTipOfTheDay" oor:op="fuse"><value>false</value></prop>
  <prop oor:name="FirstRun" oor:op="fuse"><value>false</value></prop>
  <prop oor:name="UseLocking" oor:op="fuse"><value>false</value></prop>
  <prop oor:name="SymbolStyle" oor:op="fuse"><value>colibre</value></prop>
 </node>
 <node oor:name="Save"><node oor:name="Document"><prop oor:name="WarnAlienFormat" oor:op="fuse"><value>false</value></prop></node></node>
</oor:component-data>
<oor:component-data oor:name="Setup" oor:package="org.openoffice">
 <node oor:name="Product">
  <prop oor:name="ooSetupLastVersion" oor:op="fuse"><value>24.2</value></prop>
  <prop oor:name="LastTimeGetInvolvedShown" oor:op="fuse"><value>4102444800</value></prop>
  <prop oor:name="LastTimeDonateShown" oor:op="fuse"><value>4102444800</value></prop>
 </node>
</oor:component-data>
<oor:component-data oor:name="Recovery" oor:package="org.openoffice.Office">
 <node oor:name="RecoveryInfo"><prop oor:name="Enabled" oor:op="fuse"><value>false</value></prop></node>
</oor:component-data>
</oor:data>
```

## Launch it

`Launch` runs `command` from the app's folder in the desktop session, at boot by default, or with
`at="demand"` when the agent opens it. `window` matches its window by X11 `class`, `instance` or
`title`. `Ready` waits for that window, a `port` or a `command`, within `timeout_s`: 60 seconds
unless set. A deploy waits until every app launched at boot is ready.

```python title="envs/libreoffice/open.py"
"""Start LibreOffice on the document its `open` setting names in ~/Office, or on a new spreadsheet."""
import os
from pathlib import Path

from libreoffice import LibreOffice

SOFFICE = "/usr/lib/libreoffice/program/soffice"
CSV = "--infilter=CSV:44,34,76,1"

name = LibreOffice().settings.open
document = Path.home() / "Office" / name
if name and document.is_file():
    args = [CSV, str(document)] if document.suffix == ".csv" else [str(document)]
else:
    args = ["--calc"]
os.execv(SOFFICE, [SOFFICE, "--nologo", *args])
```

The `--infilter` option opens a CSV without Calc's import dialog.

## Settings

`Settings` holds what a deploy may change, read as `self.settings`. Each layer overrides the one
before it, field by field:

| Layer          | Where                       | Example                                                        |
| -------------- | --------------------------- | -------------------------------------------------------------- |
| The class      | `Settings`' defaults        | `open: str = ""`                                               |
| The app        | its `env.toml`'s `settings` | `settings = { open = "figures.csv" }`                          |
| A computer env | its `apps` entry            | `{ env = "libreoffice", settings = { open = "figures.csv" } }` |
| A deploy       | `deploy_env`'s `env_params` | `{"app_settings": {"libreoffice": {"open": "q4-notes.txt"}}}`  |

A setting the class doesn't declare is refused before anything runs. A `SecretStr` field takes
only a `secret:KEY` or `env:NAME` reference. A deploy can't change `BuildSettings`: they are part
of the image.

## The data plane

`paths=["Office"]` makes `~/Office` the app's data. The app's server serves it:

| Method       | What it does                              |
| ------------ | ----------------------------------------- |
| `data/add`   | Writes files under the paths              |
| `data/get`   | Returns the files under the paths         |
| `data/reset` | Puts the paths back as the image has them |

A load and a reset stop the app and launch it again. The framework loads an `environment` artifact
named after the app, with files at their paths under the home:

```toml title="artifacts/figures/artifact.toml"
type = "environment"
environment_name = "libreoffice"
```

```csv title="artifacts/figures/Office/figures.csv"
quarter,revenue,costs
Q1,120,80
Q2,135,86
Q3,150,91
```

```text title="artifacts/figures/Office/q4-notes.txt"
Q4 closed with revenue of 165 and costs of 97.
```

> [!WARNING]
> An artifact folder with one file is stored at its bare filename, outside its folder, and `data/add`
> then refuses it. Give the seed a second file, as `figures` does.

This task loads the seed into the office desktop and reads it back through the app's `data/get`:

```json title="tasks/figures.json"
[
  {"id": "env", "type": "deploy_env", "env_id": "desktop"},
  {"id": "load", "type": "load_artifact", "env_id": "desktop", "artifact_id": "figures"},
  {"id": "check", "type": "env_outcome_verifier", "env_id": "desktop", "file_artifact_id": "read_back",
   "verifier_id": "figures"}
]
```

```python title="artifacts/read_back/verify.py"
"""The figures LibreOffice gives back through its own data plane, which must be the ones loaded into it."""
from agentenv_protocol import client as protocol
from agentenv_protocol.computer import part_bytes
from agentenv_protocol.types import uploaded_object_path

LOADED = {"Office/figures.csv": "quarter,revenue,costs\nQ1,120,80\nQ2,135,86\nQ3,150,91\n",
          "Office/q4-notes.txt": "Q4 closed with revenue of 165 and costs of 97.\n"}


async def verify(mcp_url, env_url):
    app = protocol.child_named(await protocol.get_card(env_url), "libreoffice")
    answer = await protocol.get_data(app.base_url(env_url))
    files = {uploaded_object_path(part): (await part_bytes(part)).decode() for part in answer.parts}
    back = files == LOADED
    return [{"id": "figures", "result": back, "description": "LibreOffice gives back the figures loaded into it"
             if back else f"LibreOffice gives back {sorted(files)}"}]
```

```bash title="Terminal"
agent-env run ./office --task figures
```

```text title="Output (excerpt)"
cua-driver → @local/agentenv-framework/drivers/cua-driver (the alias agentenv-framework's drivers bundle gives it)
chromium → @local/agentenv-framework/browsers/chromium (the alias agentenv-framework's browsers bundle gives it)
ubuntu-24.04 → @local/agentenv-framework/computers/ubuntu-24.04 (the alias agentenv-framework's computers bundle gives it)
artifacts/figures: v2, unchanged
artifacts/read_back: v2, unchanged
agentenv-framework/computers:artifacts/ubuntu-24.04 (Dockerfile image): v1, unchanged
agentenv-framework/computers:artifacts/ubuntu-24.04: v1, unchanged
agentenv-framework/drivers:envs/cua-driver: v1, unchanged
envs/libreoffice: v3, unchanged
envs/libreoffice: composing container/arm64 on LocalSandboxProvider
envs/libreoffice: container/arm64: failed at resolve: this machine can't build linux/arm64: its Docker has no emulation for it (0 s)
agentenv-framework/browsers:envs/chromium: v1, unchanged
envs/desktop: v3, unchanged
envs/desktop: composing container/arm64 on LocalSandboxProvider
envs/desktop: container/arm64: failed at resolve: this machine can't build linux/arm64: its Docker has no emulation for it (0 s)
tasks/figures.json: v1, unchanged
[tasks/figures.json] step 1/3 env (deploy_env)
[tasks/figures.json] step 1/3 env done in 4.5s
[tasks/figures.json] step 2/3 load (load_artifact)
[tasks/figures.json] step 2/3 load done in 0.4s
[tasks/figures.json] step 3/3 check (env_outcome_verifier)
[tasks/figures.json] step 3/3 check done in 0.1s
[tasks/figures.json] passed in 5.1s

Tasks:
  tasks/figures.json v1: passed (check: 1), 5.1s, instance @local/~/office/figures-kdlxok16

Tore down 1 sandbox.
```

The load can answer before the app's window is back, so a step that acts on the window waits for it
first.

## The skill

```md title="envs/libreoffice/skill.md"
---
description: Edit spreadsheets and documents in LibreOffice on this computer
---
LibreOffice Calc (spreadsheets) and Writer (documents) are installed. Documents are in `~/Office`; the one this
computer was given opens in Calc when it starts.

- Type in a cell and press Tab to move right or Enter to move down. A formula starts with `=`.
- Ctrl+S saves in the file's own format, a CSV as CSV, without asking.
- Open another file with File > Open (Ctrl+O). Start a document with File > New > Text Document.
```

`skill = "skill.md"` serves this file. An `add_skills` step naming the computer env installs it for
the agent under the app's name, beside the computer's own skill:

```json title="A step of tasks/office.json"
{"id": "skills", "type": "add_skills", "env_ids": ["desktop"]}
```

## Deploy it alone

`computer` and `driver` in the app's `env.toml` let it deploy alone, as a computer with only this
app on it, its card named `libreoffice-driver`. Writing the app composes and probes that computer.
These are its lines from a run that changed `libreoffice.py`, with its packages already locked:

```text title="Output"
envs/libreoffice: composing container/amd64, container/arm64 on LocalSandboxProvider
envs/libreoffice: container/amd64: building natively with this machine's Docker
envs/libreoffice: container/amd64: building the image
envs/libreoffice: container/arm64: failed at resolve: this machine can't build linux/arm64: its Docker has no emulation for it (1 s)
envs/libreoffice: container/amd64: pushing the image
envs/libreoffice: container/amd64: probing on LocalSandboxProvider
envs/libreoffice: container/amd64: ready (1 min 5 s)
envs/libreoffice: v3 (files changed: libreoffice.py)
```

The arm64 build fails because this amd64 machine's Docker can't emulate arm64. A `deploy_env` step
with `"env_id": "libreoffice"` deploys the computer. [What happens when you write
it](https://www.agentenvframework.com/docs/environments/computers/composing.md#what-happens-when-you-write-it) explains the builds and
their statuses.

## Other operating systems

A class declares each OS with a decorator of its own, and a computer installs the one for its base.

| OS      | Decorator                        | What differs                                                                         |
| ------- | -------------------------------- | ------------------------------------------------------------------------------------ |
| Windows | `@windows(...)`                  | No apt; PowerShell `run` scripts; `display: ["dwm"]`                                 |
| Omarchy | `@linux(distribution="omarchy")` | Replaces `@linux` there; no apt or `shm_mb`; `display: ["wayland"]`                  |
| macOS   | `@install("macos", spec)`        | `downloads`, LaunchAgent services, permission `grants`; ready by `port` or `command` |

`@install_step` and `@ready_check` take `os=` to run on some of them. The framework ships no macOS
app.