From 3a237fa26557d6b8e5dcc832de02453b42060966 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 14:09:13 -0800 Subject: [PATCH 1/4] feat(website): live website envs, real sites a multi env's computers reach at their own names (FD-4945) A website env with live = true is a real site, such as github.com, whose server serves none of its pages: only its card, a data plane over the real site's API, ready (the account works) and session (@session_state), which browser_sign_in loads into the browser. - The env stores live, allow_hosts and settings beside domains; put reads them from the server's @website(...) when they aren't given, and refuses a value the source contradicts. A secret setting (a SecretStr field) is only a secret: or env: reference, which the gateway resolves as it writes the site's container. - The gateway gives a live site no site route, and a computer's site table leaves it out. - A computer whose network is an allowlist may reach the live sites' names; one that allows every host is left as it is. A name a synthetic site's names cover is that site's. - A computer routing sites may open start tabs only at names a website of the run claims, checked at save. - A run that deploys a live site is marked live, reproducible: false. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- packages/agentenv-protocol/README.md | 44 ++++ .../src/agentenv_protocol/website/__init__.py | 8 +- .../agentenv_protocol/website/environment.py | 80 +++++- .../agentenv_protocol/website/readiness.py | 3 + .../tests/test_website_live.py | 101 ++++++++ src/agent_env/bundle/authoring.py | 19 +- src/agent_env/cli/env/website.py | 40 ++- src/agent_env/env/env.py | 5 + src/agent_env/env/envs/_multi_computers.py | 35 ++- src/agent_env/env/envs/computer/env.py | 10 +- .../env/envs/computer/utils/sites.py | 6 +- src/agent_env/env/envs/multi_env.py | 40 ++- src/agent_env/env/envs/website.py | 126 +++++++++- .../env_providers/env_computer_provider.py | 21 +- .../env_providers/env_gateway_provider.py | 36 ++- .../task_step/task_steps/deploy_env.py | 5 + src/agent_env/utils/card_naming.py | 140 ++++++++++- tst/data/live_github_website/Dockerfile | 12 + tst/data/live_github_website/server.py | 56 +++++ .../computer/website_browser_test.py | 75 +++++- tst/unit/cli/website_live_cli_test.py | 72 ++++++ tst/unit/env/envs/test_multi_env_computers.py | 5 +- tst/unit/env/envs/test_website_live.py | 227 ++++++++++++++++++ .../env/envs/test_website_server_image.py | 2 +- tst/unit/task_step/deploy_env_test.py | 19 +- 26 files changed, 1130 insertions(+), 59 deletions(-) create mode 100644 packages/agentenv-protocol/tests/test_website_live.py create mode 100644 tst/data/live_github_website/Dockerfile create mode 100644 tst/data/live_github_website/server.py create mode 100644 tst/unit/cli/website_live_cli_test.py create mode 100644 tst/unit/env/envs/test_website_live.py diff --git a/AGENTS.md b/AGENTS.md index 20fbbdb4a..5a7abf335 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -121,7 +121,7 @@ and deserialized through a registry. | Package | Contents | |---|---| -| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | +| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | | `artifact/` | Immutable versioned artifacts; every `put()` creates a new version. The document goes to the document store, binary payloads to the object store. Every artifact but an environment universe, whose `metadata` key holds its legacy metadata file ids, carries a `metadata` dict outside its versioned fields, changed by compare-and-swap (`merge_metadata`). Built-ins: `CliArtifact` (`cli`), `ComputerArtifact` (`computer`, a base computer: its architectures, OS, session user, settings and container form, whose image `put` reads from its registry into `metadata.validated_images`), `FileArtifact` (`file`), `FileArtifactUniverse` (`file_artifact_universe`), `DockerImageArtifact` (`docker_image`), `EnvironmentArtifact` (`environment`: its environment's files by path, stored as the one wrapped `file_artifact_ref` every environment artifact had when it has one file at its filename, else as `file_artifact_refs`; `get_file_artifacts()` reads either, and a load hands each file to the env's `data/add` with its path, which a pre-v1 load, taking one file, refuses), `EnvironmentUniverseArtifact` (`environment_universe`), `SkillArtifact` (`skill`); `[artifacts] type_aliases` maps another stored spelling of a type onto one of them. A `DockerImageArtifact` holds a tar.gz of its image, or, written by `put_ref` (`--image-ref` on `env mcp-server put` and `a2a-agent put`), only a registry reference with its tag pinned to a digest, which sandboxes pull, or a published image's tag with its `image_digest` and its build context, which sandboxes pull by the digest and build from the context when they can't (the infra images, and a bundle agent built from a shipped agent's sources, `Plan.published`), or, written by `put_context`, only its build context, which a VM sandbox builds before running it (`VmSandbox.load_docker_images`) and Modal builds before creating its container (`ModalSandboxProvider.prepare_image`, outside a fallback chain's deadline); anything else that runs images by name (a container on the local provider, a lone server on any provider but Modal) refuses such an image. A build context (`utils/docker/context.py`) is what `docker build` would send, `.dockerignore` applied, written so equal files give equal bytes and an equal `source_digest`. | | `task/`, `task_step/` | A `Task` holds its `TaskStep`s inline; `Task.run()` executes them as a DAG. `depends_on` (None means all prior steps) gates a step, independent steps run concurrently, `fail_task_on_error` makes a failure fatal or tolerated, `retry_config` rolls a failed span back through the step journal and re-dispatches it. Built-in steps live in `task_step/task_steps/` (`deploy_env`, `deploy_agent`, `prompt_agent`, the verifiers under `verifiers/`, among them `start_computer_recording` and `end_computer_recording`, and more). On an env whose card serves computer-exec/v1 (a computer), `run_code` with `env_id` and `run_container_unit_tests_verifier` with `env_id` run inside it as its session user through the driver: files staged with `data/add` and read with `data/get`, commands run with `run`; `env_outcome_verifier` hands a `verify` that takes an `env_url` keyword the env's address. A computer's framework-only routes (everything but MCP and the card) take the per-deploy control token its provider boots it with (`DeployedEnv.control_token`, `AGENTENV_CONTROL_TOKEN`); framework calls send `deployed.control_headers`, `Task.run` runs every step under `protocol_v1.authorized` with each deployed env's (a `verify` included), and a `run_code` script inside the computer gets `input["env"]["headers"]`. `teardown_run` and the `teardown_sandboxes` step share one core (`task/teardown.py` `tear_down`): the recordings the run left open end with fresh grants, each env still up closes (`Env.close`; a computer env ends any recording still open), what stayed open is registered from its namespace, then every sandbox is terminated by id. `task/episode/` runs a task with some of its agents played from outside, as a training loop does (`EpisodeSpec`). | | `store/` | Four store ABCs with local and cloud implementations: `DocumentStore` (SQLite, MongoDB, DynamoDB, Firestore with MongoDB compatibility), `ObjectStore` (filesystem, S3, Cloud Storage), `ImageStore` (local OCI registry, ECR), `SecretStore` (env vars or file, AWS Secrets Manager, Google Cloud Secret Manager). `VersionedEntityStore` implements the shared versioned get/put logic, `QueryBuilder` is the immutable chained query API, `store/base.py` holds the error types. `store/routing.py` is the local namespaces, `@local` and `@hf` (`is_local_id`), each with a documents file of its own: while the CLI or a `build_app` server runs, a local id and what a local task's run records go to local stores beside the configured ones, and that run's writes to the configured stores are refused. A new backend must pass the conformance kits in `tst/store/`. | diff --git a/packages/agentenv-protocol/README.md b/packages/agentenv-protocol/README.md index ce561f047..d0bd49b35 100644 --- a/packages/agentenv-protocol/README.md +++ b/packages/agentenv-protocol/README.md @@ -490,6 +490,50 @@ called with the caller's role in `ToolContext`. The site's `/mcp` is one of the browsing route (`/website//`) never reaches. `python -m agentenv_protocol.website.conformance` checks a declared endpoint (`mcp`): it answers `initialize` and lists the tools the card does. +## Live websites + +A live site is a real one, such as github.com, that a task puts in a multi env beside synthetic sites. Its server serves +none of its pages: the real site does, and the computer reaches it over its own network. The server holds what agentenv +knows of the site: its card, a data plane that seeds and reads the task's account through the real site's API, `ready` +saying the account works, and `session`, the account's cookies and local storage, which `browser_sign_in` loads into the +browser. + +```python +from pydantic import BaseModel, SecretStr +from agentenv_protocol.website import WebsiteEnvironment, ready_check, session_state, website + + +@website(domains=["github.com"], live=True, allow_hosts=["*.githubassets.com", "api.github.com"]) +class GitHub(WebsiteEnvironment): + class Settings(BaseModel): + token: SecretStr # in env.toml as a secret:KEY or env:NAME reference + + @ready_check + def account_works(self) -> bool: + ... # the API answers for self.settings.token + + @session_state + def account(self, user: str | None) -> dict: + ... # {"cookies": [...], "local_storage": [...]} that sign a browser in +``` + +- **Its names:** `domains` are the real site's, where start tabs may open and `browser_sign_in` sets the session's + cookies. `allow_hosts` are the other hosts its pages reach, such as its assets and API. A computer whose network is an + allowlist may reach both; one that allows every host is left as it is. A name a synthetic site of the run also claims + is that site's. +- **Its card:** `website/v1` says `live` (`WEBSITE_LIVE_PARAM`) and `allow_hosts` (`WEBSITE_ALLOW_HOSTS_PARAM`) beside + `domains`, and names no routes. The server serves no `/api/session` page route. `@website` refuses a live site + with no domains or a static folder, and `allow_hosts` on a site that isn't live. +- **Its session:** the method marked `@session_state` answers `session {user}`. A live site must mark one, since only + the real site's cookies sign a browser in to it. Each cookie carries no domain and a `max_age`, as a synthetic + site's does. +- **Settings:** any site may declare `Settings`. A deploy hands them to its server in `WEBSITE_SETTINGS_ENV`, a JSON + object, and the site reads them as `self.settings`, unless it has a member of that name of its own. A `SecretStr` + field's value in env.toml is a `secret:` or `env:` reference, which agentenv resolves only as it writes the server's + container, and every record shows it as withheld. +- **The run:** a run that deploys a live site is marked live. The deploy record says `live`, and the run's metadata lists + its live sites with `reproducible: false`, because the real site may change between runs. + ## Computer drivers A computer driver is what runs inside a computer an agent uses through screenshots and input. Write diff --git a/packages/agentenv-protocol/src/agentenv_protocol/website/__init__.py b/packages/agentenv-protocol/src/agentenv_protocol/website/__init__.py index 48d678519..de53f2176 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/website/__init__.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/website/__init__.py @@ -10,10 +10,12 @@ from .clock import CLOCK_EXTENSION_URI, UNIVERSE_NOW_HEADER, ClockUnavailable, UniverseClock from .environment import ( SESSION_PATH, + WEBSITE_SETTINGS_ENV, WebsiteDeclaration, WebsiteEnvironment, host_problem, ready_check, + session_state, website, ) from .errors import ApiError, BadRequest, Conflict, Forbidden, NotFound, Unauthorized @@ -21,8 +23,10 @@ DEFAULT_HEALTH_PATH, READY_TIMEOUT_S, SITE_READY_TIMEOUT_S, + WEBSITE_ALLOW_HOSTS_PARAM, WEBSITE_BROWSE_URL_PARAM, WEBSITE_EXTENSION_URI, + WEBSITE_LIVE_PARAM, WEBSITE_READY_OP, WebsiteReadiness, env_readiness, @@ -32,11 +36,11 @@ from .session import SESSION_COOKIE, SESSION_SECRET_ENV, SessionSigner __all__ = [ - "WebsiteEnvironment", "WebsiteDeclaration", "website", "api", "ApiContext", "ApiRoute", "ready_check", "host_problem", + "WebsiteEnvironment", "WebsiteDeclaration", "website", "api", "ApiContext", "ApiRoute", "ready_check", "session_state", "host_problem", "ApiError", "BadRequest", "Unauthorized", "Forbidden", "NotFound", "Conflict", "UniverseClock", "ClockUnavailable", "SessionSigner", "reserved_path", "WEBSITE_EXTENSION_URI", "CLOCK_EXTENSION_URI", "UNIVERSE_NOW_HEADER", "DEFAULT_HEALTH_PATH", "SESSION_PATH", "SESSION_COOKIE", "SESSION_SECRET_ENV", "ROLE_KEY_ENV", "ROLE_KEY_HEADER", - "WEBSITE_READY_OP", "WEBSITE_BROWSE_URL_PARAM", "READY_TIMEOUT_S", "SITE_READY_TIMEOUT_S", "WebsiteReadiness", + "WEBSITE_READY_OP", "WEBSITE_BROWSE_URL_PARAM", "WEBSITE_LIVE_PARAM", "WEBSITE_ALLOW_HOSTS_PARAM", "WEBSITE_SETTINGS_ENV", "READY_TIMEOUT_S", "SITE_READY_TIMEOUT_S", "WebsiteReadiness", "website_readiness", "env_readiness", ] diff --git a/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py b/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py index 89296f618..15e08e11c 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py @@ -14,11 +14,19 @@ One route is built in, replaced by a site's own ``@api`` route at its very path and answering before one whose path parameters would also match it: ``/api/session``, which a frontend calls to learn who is signed in, signing the acting -user in when nobody is (GET), or to sign out (DELETE).""" +user in when nobody is (GET), or to sign out (DELETE). + +A live site, ``@website(live=True, ...)``, is a real site, such as github.com, that its server serves no pages of: the +real site does, at its ``domains``, its pages reaching the ``allow_hosts`` too. Its server holds what agentenv knows of +it: the card, a data plane that seeds and reads its account through the real site's API, ``ready`` saying the account +works, and ``session``, the account's cookies and local storage, from the method marked ``@session_state``. The site's +``Settings``, which a deploy hands it as ``WEBSITE_SETTINGS_ENV`` (its credentials, ``SecretStr`` fields, among them), are +``self.settings``, unless the site has a member of that name of its own; any site may declare them.""" from __future__ import annotations import asyncio import inspect +import json import math import os import re @@ -27,6 +35,7 @@ from typing import Any, ClassVar, NamedTuple, Optional from urllib.parse import unquote +from pydantic import BaseModel from starlette.applications import Starlette from starlette.datastructures import MutableHeaders from starlette.middleware import Middleware @@ -46,15 +55,19 @@ ) from .clock import CLOCK_EXTENSION_URI, UNIVERSE_NOW_HEADER, ClockUnavailable, UniverseClock, clock_patch_script, iso from .errors import ApiError, NotFound -from .readiness import SITE_READY_TIMEOUT_S, WEBSITE_EXTENSION_URI, WEBSITE_READY_OP, WebsiteReadiness +from .readiness import (SITE_READY_TIMEOUT_S, WEBSITE_ALLOW_HOSTS_PARAM, WEBSITE_EXTENSION_URI, WEBSITE_LIVE_PARAM, + WEBSITE_READY_OP, WebsiteReadiness) from .routes import (ApiContext, ApiRoute, endpoint, error_response, forwarded_prefix, openapi, openapi_path, request_role, reserved_path, routes_of) from .session import SessionSigner, session_cookie_name SESSION_PATH = "/api/session" +#: The variable a deploy hands a site its settings in, a JSON object of each by its name. +WEBSITE_SETTINGS_ENV = "AGENTENV_WEBSITE_SETTINGS" CHECK_TIMEOUT_S = 10.0 _WEBSITE_ATTR = "_aee_website" _READY_ATTR = "_aee_website_ready" +_SESSION_ATTR = "_aee_website_session" _HEAD = re.compile(rb"]*)?>", re.IGNORECASE) _PREAMBLE = re.compile(rb"\A(?:\s||]*>|]*)?>)*", re.IGNORECASE | re.DOTALL) _HOST = re.compile(r"(\*\.)?[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)*") @@ -62,23 +75,37 @@ class WebsiteDeclaration(NamedTuple): """What ``@website`` declares: the site's domains, its static folder (relative to the class's file), how long it - may take to be ready after its server starts, and whether its HTML pages read the universe's time.""" + may take to be ready after its server starts, whether its HTML pages read the universe's time, and, for a live + site, that it is one and the other hosts its pages reach.""" domains: tuple[str, ...] = () static: Optional[str] = None ready_timeout_s: float = SITE_READY_TIMEOUT_S clock_patch: bool = False + live: bool = False + allow_hosts: tuple[str, ...] = () def website(_cls: type | None = None, *, domains: list[str] | tuple[str, ...] = (), static: str | None = None, - ready_timeout_s: float = SITE_READY_TIMEOUT_S, clock_patch: bool = False) -> Any: - """Declare a WebsiteEnvironment's site.""" + ready_timeout_s: float = SITE_READY_TIMEOUT_S, clock_patch: bool = False, live: bool = False, + allow_hosts: list[str] | tuple[str, ...] = ()) -> Any: + """Declare a WebsiteEnvironment's site; ``live=True`` a real one, at its ``domains``, whose pages reach the + ``allow_hosts`` too.""" if _cls is not None: raise TypeError("use @website(...) with parentheses") - if isinstance(domains, str): - raise ValueError(f"domains is a list of host names, not the string {domains!r}") - if problems := [problem for host in domains if (problem := host_problem(host))]: - raise ValueError(f"domains: {problems[0]}") + for field, hosts in (("domains", domains), ("allow_hosts", allow_hosts)): + if isinstance(hosts, str): + raise ValueError(f"{field} is a list of host names, not the string {hosts!r}") + if problems := [problem for host in hosts if (problem := host_problem(host))]: + raise ValueError(f"{field}: {problems[0]}") + if not isinstance(live, bool): + raise ValueError(f"live is True or False, not {live!r}") + if live and not domains: + raise ValueError("a live site names the domains the real site is at") + if live and static is not None: + raise ValueError("a live site serves no pages, the real site does, so it takes no static folder") + if allow_hosts and not live: + raise ValueError("allow_hosts are the other hosts a live site's pages reach; declare live=True") if isinstance(ready_timeout_s, bool) or not isinstance(ready_timeout_s, (int, float)) or not ( 0 < ready_timeout_s < math.inf): raise ValueError(f"ready_timeout_s is a number of seconds above 0, not {ready_timeout_s!r}") @@ -86,7 +113,8 @@ def website(_cls: type | None = None, *, domains: list[str] | tuple[str, ...] = def deco(cls: type) -> type: if not (isinstance(cls, type) and issubclass(cls, WebsiteEnvironment)): raise TypeError(f"@website declares a WebsiteEnvironment subclass, not {cls!r}") - setattr(cls, _WEBSITE_ATTR, WebsiteDeclaration(tuple(domains), static, float(ready_timeout_s), clock_patch)) + setattr(cls, _WEBSITE_ATTR, WebsiteDeclaration(tuple(domains), static, float(ready_timeout_s), clock_patch, live, + tuple(allow_hosts))) return cls return deco @@ -100,6 +128,14 @@ def host_problem(host: Any) -> str | None: return None +def session_state(fn: Any) -> Any: + """Mark the method that answers ``website/v1``'s ``session``: it takes ``user``, the account to sign in as or + None, and returns ``{cookies, local_storage}``, each cookie with no domain and a ``max_age``. A live site marks + one, since only the real site's cookies sign a browser in to it; a site that marks none signs its own cookie.""" + setattr(fn, _SESSION_ATTR, True) + return fn + + def ready_check(fn: Any) -> Any: """Mark a method that says whether the site is usable yet: ``website/v1``'s ``ready`` says ready once every one returns a truthy value. They run at once, each within ``CHECK_TIMEOUT_S``, a plain ``def`` in a worker thread.""" @@ -115,6 +151,10 @@ class WebsiteEnvironment(AgentEnvEnvironment): app: ClassVar[Any] = None asgi: Any = None + class Settings(BaseModel): + """The site's settings, which a deploy hands it: a site declares its own; a ``SecretStr`` field is a secret, + whose env.toml value is a ``secret:`` or ``env:`` reference agentenv resolves only as it starts the server.""" + def __init_subclass__(cls, **kwargs: Any) -> None: super().__init_subclass__(**kwargs) routes_of(cls) @@ -124,6 +164,9 @@ def __init__(self) -> None: self._signer = SessionSigner(cookie_name=session_cookie_name(self._build_card().name)) self._started = time.monotonic() self._checking: dict[str, asyncio.Future] = {} + settings = type(self).Settings.model_validate(json.loads(os.environ.get(WEBSITE_SETTINGS_ENV) or "{}")) + if not hasattr(type(self), "settings"): # a site's own member named settings, an @api route say, stays its own + self.settings = settings @classmethod def declaration(cls) -> WebsiteDeclaration: @@ -180,7 +223,7 @@ def create_app(self) -> Starlette: else: asgi = Starlette(middleware=middleware) AgentEnvStarletteApplication(self._build_card(), self).add_routes_to_app(asgi) - if SESSION_PATH not in taken: + if SESSION_PATH not in taken and not declaration.live: asgi.router.routes.append(Route(SESSION_PATH, self._session, methods=["GET", "DELETE"])) asgi.router.routes.extend(Route(r.path, _unreserved(endpoint(getattr(self, r.fn.__name__), r, self.context)), methods=[r.method]) for r in api_routes) @@ -209,6 +252,9 @@ def serve(self) -> None: def extension_params(self, uri: str) -> dict: if uri == WEBSITE_EXTENSION_URI: declaration = self.declaration() + if declaration.live: + return {"domains": list(declaration.domains), WEBSITE_LIVE_PARAM: True, + WEBSITE_ALLOW_HOSTS_PARAM: list(declaration.allow_hosts)} return {"domains": list(declaration.domains), "routes": [r.advertised() for r in self.routes()], "session_cookie": self.session_cookie, "session_path": SESSION_PATH} @@ -242,6 +288,18 @@ async def _website_ready(self) -> dict: @extension(WEBSITE_EXTENSION_URI, op="session") async def _website_session(self, user: Optional[str] = None) -> dict: + marked = [method for method, _marker in _marked_members(self, _SESSION_ATTR)] + if len(marked) > 1: + raise TypeError(f"{type(self).__qualname__} marks {len(marked)} methods @session_state; mark one") + if marked: + state = await _maybe_await(marked[0](user)) + if not (isinstance(state, dict) and isinstance(state.get("cookies", []), list) + and isinstance(state.get("local_storage", []), list)): + raise TypeError(f"@session_state {marked[0].__name__} returns {{cookies, local_storage}}, not {state!r}") + return {"cookies": state.get("cookies", []), "local_storage": state.get("local_storage", [])} + if self.declaration().live: + raise TypeError(f"{type(self).__qualname__} is a live site and marks no @session_state method, so it can't " + "sign a browser in") return {"cookies": [self._session_signer().cookie(user)], "local_storage": []} async def _not_ready(self) -> list[str]: diff --git a/packages/agentenv-protocol/src/agentenv_protocol/website/readiness.py b/packages/agentenv-protocol/src/agentenv_protocol/website/readiness.py index 91764f46c..fa2327f20 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/website/readiness.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/website/readiness.py @@ -20,6 +20,9 @@ WEBSITE_EXTENSION_URI = "urn:agentenv:website/v1" WEBSITE_READY_OP = "ready" WEBSITE_BROWSE_URL_PARAM = "browse_url" +#: A live site's ``website/v1`` params: that it is one, and the other hosts its pages reach. +WEBSITE_LIVE_PARAM = "live" +WEBSITE_ALLOW_HOSTS_PARAM = "allow_hosts" READY_TIMEOUT_S = 10.0 SITE_READY_TIMEOUT_S = 300.0 DEFAULT_HEALTH_PATH = "/api/health" diff --git a/packages/agentenv-protocol/tests/test_website_live.py b/packages/agentenv-protocol/tests/test_website_live.py new file mode 100644 index 000000000..b0709dfb6 --- /dev/null +++ b/packages/agentenv-protocol/tests/test_website_live.py @@ -0,0 +1,101 @@ +"""A live site on WebsiteEnvironment: a real site, such as github.com, whose server serves none of its pages, only the +card, with website/v1's live params, a data plane, ready and the account's session from its ``@session_state`` method; +and a site's ``Settings``, which a deploy hands it in ``WEBSITE_SETTINGS_ENV``.""" + +from __future__ import annotations + +import asyncio +import json + +import pytest +from pydantic import BaseModel, SecretStr +from starlette.testclient import TestClient + +from agentenv_protocol.website import ( + WEBSITE_ALLOW_HOSTS_PARAM, + WEBSITE_EXTENSION_URI, + WEBSITE_LIVE_PARAM, + WEBSITE_SETTINGS_ENV, + WebsiteEnvironment, + session_state, + website, +) + + +@website(domains=["github.com"], live=True, allow_hosts=["*.githubassets.com", "api.github.com"]) +class GitHub(WebsiteEnvironment): + class Settings(BaseModel): + token: SecretStr + org: str = "octo" + + @session_state + async def account(self, user: str | None) -> dict: + value = f"{user}:{self.settings.token.get_secret_value()}" + return {"cookies": [{"name": "user_session", "value": value, "path": "/", "max_age": 60}], "local_storage": []} + + +@pytest.fixture +def github(monkeypatch) -> GitHub: + monkeypatch.setenv(WEBSITE_SETTINGS_ENV, json.dumps({"token": "t0"})) + return GitHub() + + +def _params(client: TestClient) -> dict: + card = client.get("/.well-known/agent-env.json").json() + return next(e for e in card["capabilities"]["extensions"] if e["uri"] == WEBSITE_EXTENSION_URI)["params"] + + +def test_a_live_sites_card_names_the_real_site_and_the_hosts_its_pages_reach_and_it_serves_no_pages(github) -> None: + client = TestClient(github.create_app()) + + params = _params(client) + + assert {key: params[key] for key in ("domains", WEBSITE_LIVE_PARAM, WEBSITE_ALLOW_HOSTS_PARAM)} == { + "domains": ["github.com"], WEBSITE_LIVE_PARAM: True, + WEBSITE_ALLOW_HOSTS_PARAM: ["*.githubassets.com", "api.github.com"]} + assert not {"routes", "session_cookie", "session_path"} & params.keys() + assert params["methods"]["session"]["endpoint"] == "/agentenv/ext/website/session" + assert (client.get("/").status_code, client.get("/api/session").status_code) == (404, 404) + + +def test_a_live_sites_session_is_its_accounts_as_its_session_state_method_says(github) -> None: + client = TestClient(github.create_app()) + + state = client.post("/agentenv/ext/website/session", json={"user": "alex"}).json() + + assert state == {"cookies": [{"name": "user_session", "value": "alex:t0", "path": "/", "max_age": 60}], + "local_storage": []} + + +def test_a_sites_settings_arrive_from_the_deploy_and_a_secret_stays_one(github, monkeypatch) -> None: + assert (github.settings.org, github.settings.token.get_secret_value()) == ("octo", "t0") + assert "t0" not in repr(github.settings) + monkeypatch.delenv(WEBSITE_SETTINGS_ENV) + + @website(domains=["shop.test"]) + class Shop(WebsiteEnvironment): + pass + + assert Shop().settings.model_dump() == {} + + +def test_a_live_site_marking_no_session_state_method_cant_sign_a_browser_in() -> None: + @website(domains=["github.com"], live=True) + class Bare(WebsiteEnvironment): + pass + + with pytest.raises(TypeError, match="marks no @session_state method"): + asyncio.run(Bare()._website_session("alex")) + + +@pytest.mark.parametrize(("kwargs", "problem"), [ + ({"live": True}, "names the domains the real site is at"), + ({"domains": ["github.com"], "live": True, "static": "dist/"}, "serves no pages"), + ({"domains": ["github.com"], "allow_hosts": ["api.github.com"]}, "declare live=True"), + ({"domains": ["github.com"], "live": True, "allow_hosts": "api.github.com"}, "not the string"), + ({"domains": ["github.com"], "live": True, "allow_hosts": ["https://api.github.com"]}, "not a host name"), + ({"domains": ["github.com"], "live": "yes"}, "True or False"), +], ids=["no-domains", "static", "allow-hosts-not-live", "allow-hosts-string", "allow-hosts-url", "live-not-bool"]) +def test_website_refuses_a_live_declaration_that_cant_be_one(kwargs, problem) -> None: + with pytest.raises(ValueError, match=problem): + website(**kwargs) diff --git a/src/agent_env/bundle/authoring.py b/src/agent_env/bundle/authoring.py index 4fbf71218..deee849e2 100644 --- a/src/agent_env/bundle/authoring.py +++ b/src/agent_env/bundle/authoring.py @@ -22,7 +22,7 @@ from agent_env.providers.env_providers.constants import GATEWAY_SERVICE_NAMES from agent_env.providers.env_providers.env_gateway_provider import EnvironmentGatewayProvider, environment_name_problem from agent_env.providers.env_providers.env_provider import _env_provider_class -from agent_env.utils.card_naming import card_names_in_files +from agent_env.utils.card_naming import WebsiteSource, card_names_in_files, website_source_in_files from agent_env.utils.paths import os_reason from ._fs import relative, show, with_article @@ -118,6 +118,23 @@ def card_names(self, image_key: str) -> list[str] | None: return None return card_names_in_files(build_context_files(self.bundle, self.entry), dockerfile) + def website_source(self, image_key: str) -> WebsiteSource | None: + """What the one ``@website(...)`` class in the source of the image ``image_key`` builds from this folder + declares, read as ``card_names`` reads its card's name; None when it names an image, or its source declares + none or several.""" + value = self.entry.config.get(image_key) if isinstance(self.entry.config, dict) else None + if value is None: + dockerfile = default_dockerfile(image_key) + elif isinstance(value, dict) and value.keys() == {"dockerfile"} and isinstance(value["dockerfile"], str): + dockerfile = value["dockerfile"] + else: + return None + try: + files = build_context_files(self.bundle, self.entry) + except BundleError: # a folder with nothing to build has no source to read + return None + return website_source_in_files(files, dockerfile) + def accept(self, data: dict, /, **takes: type) -> dict: """The keys of ``data`` a type takes, each checked against the type given; any other key but ``type`` and ``id`` is refused.""" diff --git a/src/agent_env/cli/env/website.py b/src/agent_env/cli/env/website.py index 4da68bf44..f96191591 100644 --- a/src/agent_env/cli/env/website.py +++ b/src/agent_env/cli/env/website.py @@ -15,9 +15,9 @@ refuse_unwritable_ids, resolve_environment_name, ) -from agent_env.utils.card_naming import card_name_from_github, card_name_from_source +from agent_env.utils.card_naming import card_name_from_github, card_name_from_source, website_source_from_github, website_source_from_source from agent_env.utils.docker.build import DEFAULT_BUILD_PLATFORM, build_image -from agent_env.env.envs.website import WebsiteEnv, domains_problem, health_path_problem +from agent_env.env.envs.website import WebsiteEnv, declared_live, domains_problem, health_path_problem, live_problems from agent_env.providers.env_providers.constants import AGENT_ENV_WEBSITE_HEALTH_PATH from agent_env.store.ids import derive_id, image_repository @@ -52,6 +52,14 @@ def website(): @click.option("--domain", "domains", multiple=True, help="A name the site is reached by, such as slack.com or *.slack.com (repeatable); without one, the " "names its card declares") +@click.option("--live/--no-live", "live", default=None, + help="A live site: a real one, which the server serves no pages of (default: what its @website(...) says)") +@click.option("--allow-host", "allow_hosts", multiple=True, + help="Another host a live site's pages reach, such as *.githubassets.com (repeatable; default: its " + "@website(...)'s allow_hosts)") +@click.option("--setting", "setting_pairs", multiple=True, + help="A setting its server is handed, KEY=VALUE (repeatable); a secret's value is a secret:KEY or " + "env:NAME reference") @environment_name_options @env_provider_type_option("What deploys the env: 'gateway' (a gateway in front of the website), or the type of an installed " "agent_env.env_providers plugin", "website") @@ -71,6 +79,9 @@ def put( frontend_docker_context_github_url: str | None, health_path: str, domains: tuple[str, ...], + live: bool | None, + allow_hosts: tuple[str, ...], + setting_pairs: tuple[str, ...], environment_name: str | None, env_provider_type: str, metadata_pairs: tuple[str, ...], @@ -111,7 +122,30 @@ def put( sys.exit(1) key, value = pair.split("=", 1) user_metadata[key] = value - site = {"health_path": health_path, "domains": list(domains)} + settings = {} + for pair in setting_pairs: + if "=" not in pair: + click.echo(f"Error: --setting {pair!r} is KEY=VALUE", err=True) + sys.exit(1) + key, value = pair.split("=", 1) + settings[key] = value + if use_github: + source = website_source_from_github(backend_dockerfile_github_url, backend_docker_context_github_url, + github_token=os.environ.get("GITHUB_TOKEN")) + elif backend_dockerfile: + source = website_source_from_source(backend_dockerfile, backend_docker_context) + else: + source = None + declared, problems = declared_live(source, live=live, domains=list(domains) or None, + allow_hosts=list(allow_hosts) or None) + site = {"health_path": health_path, "domains": list(domains), **declared, "settings": settings} + problems += live_problems(site["live"], site["domains"], site["allow_hosts"], settings, site["secret_settings"]) + if problems: + click.echo("Error: " + "; ".join(problems), err=True) + sys.exit(1) + if site["live"]: + click.echo(f"A live site at {', '.join(site['domains'])}" + + (f", its pages reaching {', '.join(site['allow_hosts'])}" if site["allow_hosts"] else "")) if use_github: if environment_name is None: diff --git a/src/agent_env/env/env.py b/src/agent_env/env/env.py index c3a922065..76a1676a2 100644 --- a/src/agent_env/env/env.py +++ b/src/agent_env/env/env.py @@ -412,6 +412,11 @@ def provider_toml(self, out: ExportContext, env_provider_type: str) -> dict: async def deploy(self, **kwargs) -> DeployedEnv: raise NotImplementedError(f"{type(self).__name__} must implement deploy()") + def live_websites(self) -> list[dict]: + """The live websites a deploy of it reaches the real sites of, as a run records them; none but a live website + env's, or a multi env's that holds some.""" + return [] + def deploy_refusal(self, *, sandbox_type: Optional[str] = None, env_params: Optional[dict] = None, child_envs: Optional[dict] = None, **options) -> Optional[str]: """Why deploy() would refuse these options before building anything, or None. A type that checks its options diff --git a/src/agent_env/env/envs/_multi_computers.py b/src/agent_env/env/envs/_multi_computers.py index 5af404003..c749c479e 100644 --- a/src/agent_env/env/envs/_multi_computers.py +++ b/src/agent_env/env/envs/_multi_computers.py @@ -9,6 +9,7 @@ from collections import Counter from collections.abc import Awaitable, Callable, Iterable, Mapping from typing import TYPE_CHECKING, Any, Optional +from urllib.parse import urlsplit from agentenv_protocol.computer import tool_prefix_problem @@ -54,6 +55,32 @@ def app_name_problems(apps: Mapping[str, Iterable[str]], others: Iterable[str]) "find an environment by its name" for computer, names in apps.items() for name in names if taken[name] > 1] +def start_tab_problems(computer: ComputerEnv, entry: Optional[dict], names: Iterable[str]) -> list[str]: + """Why a computer routing the multi env's websites can't open its browsers' start tabs: each http or https start + URL, its app's own setting under its child_envs entry's ``app_settings``, names a host one of ``names`` covers, + the routed sites' and the live sites' (their domains and ``allow_hosts``). A computer that routes no sites isn't + asked.""" + params = entry.get("env_params") if isinstance(entry, dict) else None + if not isinstance(params, dict) or not params.get("site_routing"): + return [] + given = params.get("app_settings") if isinstance(params.get("app_settings"), dict) else {} + defaults, names, problems = computer.app_defaults(), list(names), [] + for app in computer.composed_apps(): + if not app.declaration.browser: + continue + own = given.get(app.environment_name) + urls = {**defaults.get(app.environment_name, {}), **(own if isinstance(own, dict) else {})}.get("start_urls") + for url in urls if isinstance(urls, list) else []: + parts = urlsplit(url) if isinstance(url, str) else None + if parts is None or parts.scheme not in ("http", "https") or not parts.hostname: + continue + if not any(sites.matches(name, parts.hostname) for name in names): + problems.append(f"{app.environment_name}'s start tab {url} names {parts.hostname}, which none of the " + "multi env's websites claims; add a website env at that name, or a live one (live = " + "true) whose domains or allow_hosts name it") + return problems + + def deploy_options(entry: Optional[dict], sandbox_type: str | None) -> dict: """A computer's deploy options from its child_envs entry: on ``sandbox_type``, the multi env's, unless it names one.""" return {"sandbox_type": sandbox_type, **(entry or {})} @@ -134,14 +161,15 @@ def resets(self, deployed: DeployedEnv) -> list[tuple[str, Awaitable[None]]]: for env in self.envs if env.environment_name in records] def deployer(self, child_envs: Optional[dict], *, sandbox_type: str | None, ttl_seconds: int, - attribution: Optional[Attribution], websites: Mapping[str, str] + attribution: Optional[Attribution], websites: Mapping[str, str], live_hosts: Iterable[str] = () ) -> Callable[[Optional[str]], Awaitable[list[ExternalChild]]]: """The gateway's ``children``: every computer deployed as its child_envs entry says, its tools served under its - name and ``_``, and its network allowing ``reach``, the gateway's address when the gateway has one yet; then each - as the gateway fronts it. A computer routing the multi env's ``websites`` (their environment names by env id) + name and ``_``, and its network allowing ``reach``, the gateway's address when the gateway has one yet, and the + ``live_hosts`` of the multi env's live websites; then each as the gateway fronts it. A computer routing the multi env's ``websites`` (their environment names by env id) boots with its boot apps held, for ``finish`` to route them once the gateway is up. A gateway deploy tried again deploys them again, after closing the earlier try's.""" tries = 0 + live = tuple(live_hosts) self._routes = {} for name, entry in (child_envs or {}).items(): if routes_later(entry): @@ -156,6 +184,7 @@ async def deploy(reach: Optional[str]) -> list[ExternalChild]: results = await asyncio.gather(*(env.deploy( ttl_seconds=ttl_seconds, attribution=attribution, tool_prefix=f"{env.environment_name}_", reach=reach or "", sites_later=env.environment_name in self._routes, + **({"live_hosts": live} if live else {}), **deploy_options((child_envs or {}).get(env.environment_name), sandbox_type)) for env in self.envs), return_exceptions=True) if failed := [result for result in results if isinstance(result, BaseException)]: diff --git a/src/agent_env/env/envs/computer/env.py b/src/agent_env/env/envs/computer/env.py index c530ba54d..9f3db7041 100644 --- a/src/agent_env/env/envs/computer/env.py +++ b/src/agent_env/env/envs/computer/env.py @@ -280,15 +280,17 @@ async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10, memory_mb: int | None = None, sandbox_type: str | None = None, env_state_type: str | None = None, env_state_instance_id: str | None = None, *, attribution: Optional[Attribution] = None, env_params: Optional[dict] = None, tool_prefix: str = "", reach: str = "", - sites_later: bool = False) -> DeployedEnv: + sites_later: bool = False, live_hosts: tuple[str, ...] = ()) -> DeployedEnv: """With ``tool_prefix``, its driver serves its tools under it, with ``reach``, a network that restricts its - egress allows that URL's host too, and with ``sites_later`` it routes the sites ``env_params.site_routing`` - reaches when ``route_sites`` gives them: what a multi env gives each of its computers.""" + egress allows that URL's host too, and the ``live_hosts`` of the run's live websites, and with ``sites_later`` + it routes the sites ``env_params.site_routing`` reaches when ``route_sites`` gives them: what a multi env gives + each of its computers.""" return await deploy_through_provider( self, environment_name=self.environment_name, ttl_seconds=ttl_seconds, sandbox_type=sandbox_type, then=self._give_tools, disk_size_gb=disk_size_gb, gateway_mode=gateway_mode, cpu=cpu, memory_mb=memory_mb, env_state_type=env_state_type, env_state_instance_id=env_state_instance_id, attribution=attribution, - env_params=env_params, tool_prefix=tool_prefix, reach=reach, sites_later=sites_later) + env_params=env_params, tool_prefix=tool_prefix, reach=reach, sites_later=sites_later, + live_hosts=tuple(live_hosts)) async def route_sites(self, sites: list[dict]) -> None: """Route ``sites`` on this computer, deployed with ``sites_later``, launch its held apps and finish its boot; diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index dcfa618bf..d3f943b99 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -34,6 +34,7 @@ import httpx from agentenv_protocol import client as protocol_client from agentenv_protocol.website import WEBSITE_EXTENSION_URI as WEBSITE +from agentenv_protocol.website import WEBSITE_LIVE_PARAM from agentenv_protocol.website_browser import SITE_PROXY_ENV from cryptography import x509 from cryptography.hazmat.primitives import hashes, serialization @@ -486,7 +487,8 @@ async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[s ``only`` keeps the websites of the envs it names, each of which must be deployed, and ``services`` those served as the names it holds, each website's environment_name, which its card's name needn't be. Sites of different envs that share a name are each named for their env (``_env_qualified``), so they're routed apart, while two - that claim one domain are still refused.""" + that claim one domain are still refused. A live site, whose params say ``live``, is no site the computer routes: + the real one is at its names, which the computer reaches over its own network.""" from agentenv_protocol import client as protocol_v1 from agentenv_protocol.website import WEBSITE_BROWSE_URL_PARAM, WEBSITE_EXTENSION_URI @@ -510,7 +512,7 @@ async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[s params = protocol_v1.extension_params(child.card, WEBSITE_EXTENSION_URI) browse = params.get(WEBSITE_BROWSE_URL_PARAM) service = served_as(child) - if services is not None and service not in services: + if (services is not None and service not in services) or params.get(WEBSITE_LIVE_PARAM) is True: named.add(service) continue url = (_joined(address, browse) if isinstance(browse, str) and browse and address diff --git a/src/agent_env/env/envs/multi_env.py b/src/agent_env/env/envs/multi_env.py index a4a66ced3..be158b4a1 100644 --- a/src/agent_env/env/envs/multi_env.py +++ b/src/agent_env/env/envs/multi_env.py @@ -98,7 +98,9 @@ def __init__(self, id: str, version: Optional[int], mcp_server_envs: list[MCPSer self.env_provider_type = env_provider_type # the children's own types are ignored: this one deploys them all self.mcp_server_envs = mcp_server_envs self.website_envs = website_envs or [] - if conflicts := sites.name_conflicts([sites.Claim(name, sites.SYNTHETIC, f"{env.environment_name} at website_envs[{i}]") + # A live site's names are the real site's, which a synthetic site claiming one of them wins. + if conflicts := sites.name_conflicts([sites.Claim(name, sites.ALLOWLIST if env.live else sites.SYNTHETIC, + f"{env.environment_name} at website_envs[{i}]") for i, env in enumerate(self.website_envs) for name in dict.fromkeys(env.domains)]): raise sites.SiteConflict(f"multi env {id!r}: its websites claim names twice: {'; '.join(conflicts)}") @@ -205,7 +207,8 @@ async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10, gatew raise ValueError(refusal) self._computers.begin() computers = dict(children=self._computers.deployer(child_envs, sandbox_type=sandbox_type, ttl_seconds=ttl_seconds, - attribution=attribution, websites=self._websites()), + attribution=attribution, websites=self._websites(), + live_hosts=self.live_hosts()), then=self._computers.finish) if self.computer_envs else {} try: deployed_env = await deploy_through_provider( @@ -228,6 +231,37 @@ def _websites(self) -> dict[str, str]: """Its websites' environment names by env id, the ids a computer's ``site_routing`` may name.""" return {env.id: env.environment_name for env in self.website_envs} + def _start_tab_names(self, entry: Optional[dict]) -> list[str]: + """The names a computer with ``entry`` may open start tabs at: those of the websites its ``site_routing`` + routes, all of them or those it lists, each site's own or its fallback name, and every live site's domains and + ``allow_hosts``, which the computer reaches the real sites at.""" + params = entry.get("env_params") if isinstance(entry, dict) else None + routing = params.get("site_routing") if isinstance(params, dict) else None + listed = routing if isinstance(routing, list) else None + names = [name for env in self.website_envs if not env.live and (listed is None or env.id in listed) + for name in env.domains or [sites.fallback_name(env.environment_name)]] + return names + [name for env in self.website_envs if env.live for name in [*env.domains, *env.allow_hosts]] + + def live_hosts(self) -> list[str]: + """The hosts its live websites reach the real sites at, each one's domains and ``allow_hosts``, which a + computer whose network is an allowlist is let reach; a name one of its synthetic websites' names covers is + that site's, routed to it, and left out, while a wider one is kept, the synthetic name still routed within + it.""" + synthetic = [name for env in self.website_envs if not env.live for name in env.domains] + hosts = [] + for env in (env for env in self.website_envs if env.live): + for host in dict.fromkeys([*env.domains, *env.allow_hosts]): + if claimed := next((name for name in synthetic if sites.covers(name, host)), None): + logger.warning(f"multi env '{self.id}': live site {env.environment_name!r} names {host!r}, which " + f"the synthetic site at {claimed!r} claims, so the computer reaches that site there") + elif host not in hosts: + hosts.append(host) + return hosts + + def live_websites(self) -> list[dict]: + """Its live websites, as a run records them (``WebsiteEnv.live_websites``).""" + return [site for env in self.website_envs for site in env.live_websites()] + def save_refusal(self) -> str | None: """Why saving the env would be refused, or None: its env_provider_type can't deploy it. How each computer fits a sandbox is a deploy's question, as it is for a computer saved alone (``deploy_refusal``).""" @@ -265,6 +299,8 @@ def child_envs_refusal(self, child_envs: Optional[dict], *, sandbox_type: str | if entry is None or (isinstance(entry, dict) and set(entry) <= set(CHILD_ENV_OPTIONS)): if refusal := _multi_computers.refusal(computer, entry or {}, sandbox_type, self._websites()): problems.append(f"child_envs.{name}: {refusal}" if name in child_envs else refusal) + problems += [f"child_envs.{name}: {problem}" for problem in _multi_computers.start_tab_problems( + computer, entry, self._start_tab_names(entry))] return "; ".join(problems) or None @classmethod diff --git a/src/agent_env/env/envs/website.py b/src/agent_env/env/envs/website.py index 62ceb925e..2b661e32f 100644 --- a/src/agent_env/env/envs/website.py +++ b/src/agent_env/env/envs/website.py @@ -3,7 +3,14 @@ A deploy reads whether the site is ready from its env card, the protocol layer: ``website/v1``'s ``ready``, as ``computer-app/v1``'s says of an app. ``health_path`` (env.toml's ``health``) is read only for a site whose card has no -``ready``, or that serves no card, and is deprecated.""" +``ready``, or that serves no card, and is deprecated. + +A live website env (``live``) is a real site, at its ``domains``, its pages reaching the ``allow_hosts`` too: its server +serves none of its pages, only its card, data plane, readiness and session, so a computer reaches the real site over +its own network, and a gateway gives it no site route. The env keeps them, as it keeps ``domains``, since a computer's +network is fixed before any card is read. ``settings`` are what its server is handed as ``WEBSITE_SETTINGS_ENV``, +each of its ``secret_settings`` (its ``Settings``' ``SecretStr`` fields) a ``secret:`` or ``env:`` reference, resolved only +when its container is written.""" from __future__ import annotations @@ -19,11 +26,13 @@ from agentenv_protocol import client as protocol_v1 from agent_env.artifact import Artifact, DockerImageArtifact, EnvironmentArtifact +from agent_env.config import ENV_REF_PREFIX, SECRET_REF_PREFIX from agent_env.artifact.artifacts.docker_image import ProgressCallback, refuse_local_github_build from agent_env.entity.refs import EntityRef from agent_env.env.env import Env, gateway_url_of from agent_env.env.envs.computer.utils.sites import name_problem from agent_env.store.ids import derive_id +from agent_env.utils.card_naming import WebsiteSource from agent_env.env import legacy_protocol from agent_env.env.envs._deployment import ( as_builtin, builtin_provider_for, close_deployed, close_replaced, deploy_refusal, deploy_through_provider, host_staging_refusal, @@ -53,7 +62,8 @@ class WebsiteEnv(Env): EntityRef.artifact("frontend_image", artifact_type="docker_image", optional=True)) toml_keys: ClassVar[dict[str, type]] = {"backend_image": object, "frontend_image": object, "environment_name": str, - "health": str, "domains": list, "env_provider_type": str} + "health": str, "domains": list, "env_provider_type": str, "live": bool, + "allow_hosts": list, "settings": dict} def __init__( self, @@ -68,6 +78,10 @@ def __init__( domains: Optional[list[str]] = None, metadata: Optional[dict[str, str]] = None, env_provider_type: str = "gateway", + live: bool = False, + allow_hosts: Optional[list[str]] = None, + settings: Optional[dict[str, Any]] = None, + secret_settings: Optional[list[str]] = None, ): super().__init__(id, version, metadata=metadata) if not environment_name: @@ -85,6 +99,12 @@ def __init__( if problem := domains_problem(domains or []): raise ValueError(f"website env {id!r}: {problem}") self.domains = list(domains or []) + if problems := live_problems(live, self.domains, allow_hosts or [], settings or {}, secret_settings or []): + raise ValueError(f"website env {id!r}: {'; '.join(problems)}") + self.live = live + self.allow_hosts = list(allow_hosts or []) + self.settings = dict(settings or {}) + self.secret_settings = list(secret_settings or []) self.environment_name = environment_name self.env_provider_type = env_provider_type self._sandbox = None @@ -94,6 +114,14 @@ def __init__( self._deployed: Optional[DeployedEnv] = None self._replaced: list[tuple] = [] # the provider and sandbox of each deployment a later deploy replaced, for close() + def live_websites(self) -> list[dict]: + """The live site it is, as a run records one: its env id, environment name, domains and ``allow_hosts``; + none for a synthetic site.""" + if not self.live: + return [] + return [{"env_id": self.id, "environment_name": self.environment_name, "domains": list(self.domains), + "allow_hosts": list(self.allow_hosts)}] + @property def backend_docker_image_artifact(self) -> DockerImageArtifact: """The server image, under the name it had while a site's pages came from a separate frontend image.""" @@ -114,6 +142,14 @@ def to_dict(self) -> dict: base["health_path"] = self.health_path if self.domains: base["domains"] = list(self.domains) + if self.live: + base["live"] = True + if self.allow_hosts: + base["allow_hosts"] = list(self.allow_hosts) + if self.settings: + base["settings"] = dict(self.settings) + if self.secret_settings: + base["secret_settings"] = list(self.secret_settings) base["service_name"] = self.environment_name base["environment_name"] = self.environment_name # dual-write with service_name during the rename base["env_provider_type"] = self.env_provider_type @@ -134,6 +170,10 @@ def from_dict(cls, data: dict) -> WebsiteEnv: domains=data.get("domains") or [], metadata=data.get("metadata", {}), env_provider_type=data.get("env_provider_type", "gateway"), + live=bool(data.get("live", False)), + allow_hosts=data.get("allow_hosts") or [], + settings=data.get("settings") or {}, + secret_settings=data.get("secret_settings") or [], ) @classmethod @@ -157,7 +197,11 @@ def from_toml(cls, data: dict, ctx: AuthoringContext) -> WebsiteEnv: environment_name=fields["environment_name"], health_path=fields.get("health", DEFAULT_HEALTH_PATH), domains=fields.get("domains") or [], - env_provider_type=fields["env_provider_type"]) + env_provider_type=fields["env_provider_type"], + live=fields.get("live", False), + allow_hosts=fields.get("allow_hosts") or [], + settings=fields.get("settings") or {}, + secret_settings=fields.get("secret_settings") or []) def to_toml(self, out: ExportContext) -> dict: """The env.toml of a folder this env's images are built from, which ``from_toml`` reads back as it.""" @@ -169,6 +213,12 @@ def to_toml(self, out: ExportContext) -> dict: toml["health"] = self.health_path if self.domains: toml["domains"] = list(self.domains) + if self.live: + toml["live"] = True + if self.allow_hosts: + toml["allow_hosts"] = list(self.allow_hosts) + if self.settings: + toml["settings"] = dict(self.settings) return {**toml, **self.provider_toml(out, self.env_provider_type)} @classmethod @@ -182,7 +232,15 @@ def accept_toml(cls, data: dict, ctx: AuthoringContext) -> dict: domains = data.get("domains", []) if isinstance(domains, list) and (problem := domains_problem(domains)): ctx.refuse([ctx.config_problem(problem)]) - return ctx.accept_env(data, cls, named_by="backend_image") + fields = ctx.accept_env(data, cls, named_by="backend_image") + declared, problems = declared_live(ctx.website_source("backend_image"), live=fields.get("live"), + domains=fields.get("domains"), allow_hosts=fields.get("allow_hosts")) + problems += live_problems(declared["live"], declared.get("domains", fields.get("domains") or []), + declared["allow_hosts"], fields.get("settings") or {}, declared["secret_settings"]) + fields.update({key: value for key, value in declared.items() if value}) + if problems: + ctx.refuse([ctx.config_problem(problem) for problem in problems]) + return fields async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10, gateway_mode: GatewayMode = GatewayMode.PERFORMANCE, cpu: float | None = None, memory_mb: int | None = None, sandbox_type: str | None = None, env_state_type: str | None = None, env_state_instance_id: str | None = None, artifact_id: str | None = None, artifact_version: int | None = None, *, attribution: Optional[Attribution] = None) -> DeployedEnv: return await deploy_through_provider( @@ -357,6 +415,10 @@ async def put_from_github( env_provider_type: str = "gateway", health_path: str = DEFAULT_HEALTH_PATH, domains: list[str] | None = None, + live: bool = False, + allow_hosts: list[str] | None = None, + settings: dict[str, Any] | None = None, + secret_settings: list[str] | None = None, ) -> WebsiteEnv: """Build the server image, from ``backend_dockerfile_github_url``, and any deprecated frontend image from GitHub in parallel, and create a WebsiteEnv.""" @@ -407,6 +469,10 @@ async def put_from_github( env_provider_type=env_provider_type, health_path=health_path, domains=domains, + live=live, + allow_hosts=allow_hosts, + settings=settings, + secret_settings=secret_settings, ) return env @@ -419,6 +485,58 @@ def health_path_problem(health_path: str) -> str | None: return f"health: {health_path!r} is not a path of letters, digits, '.', '_', '~', '-' and '/' starting with '/'" +def declared_live(source: Optional[WebsiteSource], *, live: Optional[bool] = None, domains: Optional[list[str]] = None, + allow_hosts: Optional[list[str]] = None) -> tuple[dict, list[str]]: + """A website env's ``live``, ``allow_hosts``, ``secret_settings`` and, for a live one, ``domains``: each one + given, else what ``source``, its server's ``@website(...)``, declares; and the problems, one for each value given + that the source declares otherwise, since the env and the site's card would then disagree. A synthetic site's + domains stay as given: without them its card's are read at deploy.""" + source = source or WebsiteSource() + problems = [f"{key} is {given!r} here, but the site's @website(...) declares {key}={declared!r}" + for key, given, declared in (("live", live, source.live), ("allow_hosts", allow_hosts, source.allow_hosts)) + if given is not None and declared is not None and given != declared] + resolved = live if live is not None else bool(source.live) + fields = {"live": resolved, "allow_hosts": list(allow_hosts if allow_hosts is not None else source.allow_hosts or []), + "secret_settings": list(source.secret_settings)} + if resolved: + if domains and source.domains is not None and list(domains) != source.domains: + problems.append(f"domains are {list(domains)!r} here, but the site's @website(...) declares " + f"domains={source.domains!r}") + fields["domains"] = list(domains or source.domains or []) + return fields, problems + + +def live_problems(live: Any, domains: list[str], allow_hosts: list[str], settings: dict, secret_settings: list[str] + ) -> list[str]: + """Why these can't be a website env's: a live site names the domains the real site is at, its ``allow_hosts`` are + host names as ``domains`` are and only a live site's, each setting's value is a string, number or boolean, and each + of the secret settings given is a ``secret:`` or ``env:`` reference, never its value.""" + problems = [] + if not isinstance(live, bool): + problems.append(f"live is true or false, not {live!r}") + elif live and not domains: + problems.append("a live website env names the domains the real site is at") + if not isinstance(allow_hosts, list): + problems.append(f"allow_hosts is a list of host names, not {allow_hosts!r}") + elif allow_hosts: + if live is not True: + problems.append("allow_hosts are the other hosts a live site's pages reach; set live = true") + if problem := domains_problem(allow_hosts): + problems.append(problem.replace("domains:", "allow_hosts:", 1)) + if not isinstance(settings, dict): + return [*problems, f"settings is a table of a setting's value by its name, not {settings!r}"] + for name, value in settings.items(): + if not isinstance(name, str) or not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", name): + problems.append(f"settings: {name!r} isn't a setting's name") + elif not isinstance(value, (str, int, float, bool)): + problems.append(f"settings.{name} is a string, number or boolean, not {value!r}") + elif name in secret_settings and not (isinstance(value, str) and value.startswith((SECRET_REF_PREFIX, + ENV_REF_PREFIX))): + problems.append(f"settings.{name} is a secret, so it's a {SECRET_REF_PREFIX}KEY or {ENV_REF_PREFIX}NAME " + "reference, never its value") + return problems + + def domains_problem(domains: list[str]) -> str | None: """Why ``domains`` can't be the names a site is reached by, or None: each a DNS name, as a computer's site table takes it, a wildcard only as its whole first label.""" diff --git a/src/agent_env/providers/env_providers/env_computer_provider.py b/src/agent_env/providers/env_providers/env_computer_provider.py index d088f4e06..6d29537a2 100644 --- a/src/agent_env/providers/env_providers/env_computer_provider.py +++ b/src/agent_env/providers/env_providers/env_computer_provider.py @@ -81,11 +81,13 @@ async def deploy( tool_prefix: str = "", reach: str = "", sites_later: bool = False, + live_hosts: tuple[str, ...] = (), ) -> DeployedSandboxEnv: """The env's first ready build, in its base's order of forms and arches (or of ``env_params.form``), that the sandbox provider runs natively, booted, once what it boots that no compose started is composed; returns the record once the driver is ready with the env's display. With ``tool_prefix``, its driver serves its tools under it; with ``reach``, - a URL this process reaches, a network that restricts the computer's egress allows its host too. With + a URL this process reaches, a network that restricts the computer's egress allows its host too, and the + ``live_hosts`` of the run's live websites. With ``sites_later``, a computer routing sites, whose sites aren't up yet, boots with its boot apps held and returns once its driver answers; ``route_sites`` routes them and finishes its boot.""" from agent_env.env.envs.computer.env import ComputerEnv @@ -119,7 +121,8 @@ async def deploy( "computer runs inside a launcher container it can't reach; deploy a container form") attempt = functools.partial(self._boot, env, builds, params, values, ttl_seconds=ttl_seconds, disk_size_gb=disk_size_gb, cpu=cpu, memory_mb=memory_mb, attribution=attribution, - tool_prefix=tool_prefix, reach=reach, sites_later=sites_later) + tool_prefix=tool_prefix, reach=reach, sites_later=sites_later, + live_hosts=tuple(live_hosts)) return await self._run(sandbox_provider, env.id, attempt) async def route_sites(self, env: ComputerEnv, record: DeployedSandboxEnv, sites: list[dict]) -> None: @@ -141,7 +144,7 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par values: dict, sandbox_provider: SandboxProvider, *, ttl_seconds: int, disk_size_gb: float, cpu: float | None, memory_mb: int | None, attribution: Optional[Attribution], tool_prefix: str = "", reach: str = "", - sites_later: bool = False) -> DeployedSandboxEnv: + sites_later: bool = False, live_hosts: tuple[str, ...] = ()) -> DeployedSandboxEnv: """One attempt on one sandbox provider: the first build it runs natively, booted and readied; its record.""" from agent_env.env.envs.computer.env import ComputerEnv @@ -166,7 +169,8 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par values = _values(computer, values) app_settings = recorded_app_settings(env.composed_apps(), computer_params.app_settings(env, params.app_settings), platform_of(env.computer.os)) - policy = _reaching(NetworkPolicy.from_dict(env.network) if env.network else None, sandbox_provider, reach) + policy = _reaching(NetworkPolicy.from_dict(env.network) if env.network else None, sandbox_provider, reach, + live_hosts) booted = await boot( sandbox_provider, image=build["image"], port=env.driver.port, network_policy=policy, token=token, environment=_environment(env, computer, values, params, tool_prefix, sites_later=sites_later), @@ -334,10 +338,13 @@ async def end_recordings(record: DeployedSandboxEnv, reason: str) -> None: logger.warning(f"'{record.env_id}': its recordings weren't ended before its computer went: {e}") -def _reaching(policy: Optional[NetworkPolicy], sandbox_provider: SandboxProvider, url: str) -> Optional[NetworkPolicy]: - """``policy``, also allowing ``url``'s host as the sandbox provider's sandboxes reach it when it lists the hosts allowed.""" +def _reaching(policy: Optional[NetworkPolicy], sandbox_provider: SandboxProvider, url: str, + live_hosts: tuple[str, ...] = ()) -> Optional[NetworkPolicy]: + """``policy``, also allowing ``url``'s host as the sandbox provider's sandboxes reach it, and ``live_hosts``, when + it lists the hosts allowed; a network that allows every host is left as it is.""" host = urlsplit(sandbox_provider.url_from_sandbox(url)).hostname if url else None - return policy.with_hosts([host]) if policy is not None and host else policy + hosts = [*([host] if host else []), *live_hosts] + return policy.with_hosts(hosts) if policy is not None and hosts else policy def _values(computer: ComputerArtifact, values: dict) -> dict: diff --git a/src/agent_env/providers/env_providers/env_gateway_provider.py b/src/agent_env/providers/env_providers/env_gateway_provider.py index e1cd8beb2..6fc0d55e8 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -21,12 +21,14 @@ SITE_READY_TIMEOUT_S, WEBSITE_EXTENSION_URI, WEBSITE_READY_OP, + WEBSITE_SETTINGS_ENV, WebsiteReadiness, website_readiness, ) from agent_env.artifact.artifacts.docker_image import pull_problem from agent_env.attribution import Attribution +from agent_env.config import get_config from agent_env.env.env import DeployedGatewayEnv, _mcp_url from agent_env.env.gateway import AGENT_ENV_GATEWAY_MCP_PORT, ExternalChild, GatewayMode from agent_env.env.gateway.constants import ( @@ -149,6 +151,10 @@ class WebsiteConfig: frontend_image: str | None environment_name: str health_path: str = AGENT_ENV_WEBSITE_HEALTH_PATH + # A live site's server serves no pages, so the gateway gives it no site route; ``settings`` are handed to its + # server, each ``secret:`` or ``env:`` reference resolved only as the compose file is written. + live: bool = False + settings: dict = field(default_factory=dict) @dataclass @@ -500,9 +506,10 @@ def create_docker_compose( f"{wc.environment_name}=http://{wc.environment_name}-{AGENT_ENV_WEBSITE_FRONTEND_SUFFIX}:80" if wc.frontend_image else f"{wc.environment_name}=http://{wc.environment_name}-{AGENT_ENV_WEBSITE_BACKEND_SUFFIX}:{AGENT_ENV_WEBSITE_BACKEND_PORT}" - for wc in website_configs + for wc in website_configs if not wc.live ) - gateway_env.append(f" - WEBSITE_URLS={website_urls}") + if website_urls: + gateway_env.append(f" - WEBSITE_URLS={website_urls}") for wc in website_configs: rest_proxy_urls.append( f"{wc.environment_name}=http://{wc.environment_name}-{AGENT_ENV_WEBSITE_BACKEND_SUFFIX}:{AGENT_ENV_WEBSITE_BACKEND_PORT}" @@ -559,6 +566,7 @@ def create_docker_compose( f" - PORT={AGENT_ENV_WEBSITE_BACKEND_PORT}", f" - {SESSION_SECRET_ENV}={secrets.token_hex(32)}", " - PYTHONUNBUFFERED=1", + *_settings_lines(wc.settings), " healthcheck:", f' test: ["CMD-SHELL", "curl -sf -o /dev/null http://localhost:{AGENT_ENV_WEBSITE_BACKEND_PORT}{WELL_KNOWN_PATH}' f' || curl -sf -o /dev/null http://localhost:{AGENT_ENV_WEBSITE_BACKEND_PORT}{wc.health_path}"]', @@ -566,7 +574,7 @@ def create_docker_compose( " timeout: 5s", " start_period: 30s", " retries: 3", - *_network_lines(_ENV_NETWORK, *([] if wc.frontend_image else sites_network)), + *_network_lines(_ENV_NETWORK, *([] if wc.frontend_image or wc.live else sites_network)), "", ]) if not wc.frontend_image: @@ -956,8 +964,8 @@ async def _stand_up(_spec): if website_configs: website_frontend_urls = { wc.environment_name: f"{gateway_url}/website/{wc.environment_name}/" - for wc in website_configs - } + for wc in website_configs if not wc.live + } or None for svc_name, url in website_frontend_urls.items(): logger.info(f"Website frontend ({svc_name}): {url}") @@ -1554,7 +1562,8 @@ def website_config(env: WebsiteEnv) -> WebsiteConfig: frontend = env.frontend_docker_image_artifact return WebsiteConfig(backend_image=env.server_docker_image_artifact.image_name, frontend_image=frontend.image_name if frontend else None, - environment_name=env.environment_name, health_path=env.health_path) + environment_name=env.environment_name, health_path=env.health_path, live=env.live, + settings=dict(env.settings)) def _gateway_topology(env: Env) -> _GatewayTopology: @@ -1617,6 +1626,7 @@ def _compose_name_problems(mcp_servers: list[MCPServerConfig], website_configs: # rendered compose is logged. Matches the key to the left of the first '=' on an # ` - KEY=VALUE` env line. _SECRET_ENV_KEY_RE = re.compile(r"^(\s*-\s*)([A-Za-z0-9_]*(?:SECRET|PASSWORD|TOKEN|API_KEY)[A-Za-z0-9_]*)=.*$") +_SETTINGS_LINE_RE = re.compile(rf'^(\s*-\s*")({WEBSITE_SETTINGS_ENV})=.*$') def _compose_ps_rows(text: str) -> list[dict]: @@ -1651,12 +1661,22 @@ def _network_lines(*networks: str) -> list[str]: def _redact_compose_secrets(compose_content: str) -> str: - """Mask secret-valued env lines so the logged compose never carries a live secret.""" + """Mask secret-valued env lines so the logged compose never carries a live secret, a site's settings among them.""" return "\n".join( - _SECRET_ENV_KEY_RE.sub(r"\1\2=***", line) for line in compose_content.splitlines() + _SETTINGS_LINE_RE.sub(r'\1\2=***"', _SECRET_ENV_KEY_RE.sub(r"\1\2=***", line)) + for line in compose_content.splitlines() ) +def _settings_lines(settings: dict) -> list[str]: + """The compose line handing a site its ``settings`` in ``WEBSITE_SETTINGS_ENV``, each ``secret:`` or ``env:`` + reference resolved, quoted so any value is one YAML string; none for a site with none.""" + if not settings: + return [] + resolved = {name: get_config().resolve_reference(value) for name, value in settings.items()} + return [f" - {json.dumps(f'{WEBSITE_SETTINGS_ENV}={json.dumps(resolved)}')}"] + + # Budget for reading the gateway's /state once its card is served: answering it runs tool discovery, one server at a # time, if startup's didn't complete. _STATE_READ_TIMEOUT_S = 60.0 diff --git a/src/agent_env/task_step/task_steps/deploy_env.py b/src/agent_env/task_step/task_steps/deploy_env.py index d579968d7..1ff00eaa3 100644 --- a/src/agent_env/task_step/task_steps/deploy_env.py +++ b/src/agent_env/task_step/task_steps/deploy_env.py @@ -209,6 +209,11 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: # create_instance runs inside env.deploy(), so an annotation set after it # returns has to be persisted explicitly to reach the instance record. annotations = {"deploy_step_id": self.id} + # A live website's real site may change between runs, so a run that reaches one can't be repeated exactly. + if live := env.live_websites(): + annotations["live"] = True + context.metadata.setdefault("live_websites", {})[self.env_id] = live + context.metadata["reproducible"] = False deployed_env.metadata = {**(deployed_env.metadata or {}), **annotations} if deployed_env.instance_id: update_env_instance_metadata(deployed_env.instance_id, annotations) diff --git a/src/agent_env/utils/card_naming.py b/src/agent_env/utils/card_naming.py index 57ded2a75..9aa3a870f 100644 --- a/src/agent_env/utils/card_naming.py +++ b/src/agent_env/utils/card_naming.py @@ -1,12 +1,27 @@ -"""Derive an env name from ``@environment_card(name=...)`` declarations (import-light: the hub imports this).""" +"""Derive an env name from ``@environment_card(name=...)`` declarations, and what a site's ``@website(...)`` declares of +it (``WebsiteSource``), from source that's read, never run (import-light: the hub imports this).""" import ast import posixpath -from collections.abc import Mapping +from collections.abc import Iterable, Mapping +from dataclasses import dataclass, field from pathlib import Path +from typing import Optional import httpx + +@dataclass(frozen=True) +class WebsiteSource: + """What the one ``@website(...)`` class of a site's source declares of it: ``live``, ``domains`` and + ``allow_hosts`` where they're given as literals (None where not), and the fields its ``Settings`` declares as + ``SecretStr``, which an env stores as its secret settings.""" + + live: Optional[bool] = None + domains: Optional[list[str]] = None + allow_hosts: Optional[list[str]] = None + secret_settings: list[str] = field(default_factory=list) + def card_name_from_source(dockerfile: str, context: str | None) -> str | None: """Derive the env name by statically reading `@environment_card(name="...")` from the server's source — the Dockerfile's own directory (where the server's source lives), @@ -51,22 +66,135 @@ def card_name_from_github( def _github_card_names(client: httpx.Client, owner: str, repo: str, ref: str | None, directory: str) -> list[str]: + return sorted({name for text in _github_sources(client, owner, repo, ref, directory) + for name in _card_names_in_source(text)}) + + +def _github_sources(client: httpx.Client, owner: str, repo: str, ref: str | None, directory: str) -> list[str]: + """The text of each ``.py`` file under ``directory`` on GitHub, tests aside.""" ref_q = f"?ref={ref}" if ref else "" resp = client.get(f"https://api.github.com/repos/{owner}/{repo}/contents/{directory}{ref_q}") resp.raise_for_status() entries = resp.json() if not isinstance(entries, list): return [] - found: set[str] = set() + texts: list[str] = [] for entry in entries: name = entry.get("name") or "" if entry.get("type") == "dir": - found |= set(_github_card_names(client, owner, repo, ref, entry["path"])) + texts += _github_sources(client, owner, repo, ref, entry["path"]) elif entry.get("type") == "file" and name.endswith(".py") and "test" not in name: download_url = entry.get("download_url") if download_url: - found |= _card_names_in_source(client.get(download_url).text) - return sorted(found) + texts.append(client.get(download_url).text) + return texts + + +def website_source_from_source(dockerfile: str, context: str | None) -> Optional[WebsiteSource]: + """What the one ``@website(...)`` class in a server's source declares, read as ``card_name_from_source`` reads the + name: the Dockerfile's folder, else the build context. None when there's none, or several.""" + found = _website_sources(_folder_texts(Path(dockerfile).resolve().parent)) + if not found and context: + found = _website_sources(_folder_texts(Path(context).resolve())) + return _one(found) + + +def website_source_from_github(dockerfile_github_url: str, context_github_url: str | None = None, *, + github_token: str | None = None) -> Optional[WebsiteSource]: + """``website_source_from_source`` for a server whose source is on GitHub; None on any failure.""" + try: + from agent_env.artifact.artifacts.docker_image import DockerImageArtifact + + parts = DockerImageArtifact._parse_github_url(dockerfile_github_url) + if not parts.path: + return None + headers = {"Accept": "application/vnd.github+json"} + if github_token: + headers["Authorization"] = f"Bearer {github_token}" + with httpx.Client(timeout=20, headers=headers, follow_redirects=True) as client: + found = _website_sources(_github_sources(client, parts.owner, parts.repo, parts.ref, + posixpath.dirname(parts.path))) + if not found and context_github_url: + cparts = DockerImageArtifact._parse_github_url(context_github_url) + if cparts.path is not None: + found = _website_sources(_github_sources(client, cparts.owner, cparts.repo, cparts.ref, + cparts.path)) + return _one(found) + except Exception: + return None + + +def website_source_in_files(files: Mapping[str, Path], dockerfile: str) -> Optional[WebsiteSource]: + """``website_source_from_source`` among ``files``, keyed by POSIX path, as ``card_names_in_files`` reads them.""" + folder = posixpath.dirname(dockerfile) + sources = {key: path for key, path in files.items() if key.endswith(".py")} + near = {key: path for key, path in sources.items() if not folder or key.startswith(f"{folder}/")} + for chosen in (near, sources): + if found := _website_sources(_read(path) for path in chosen.values()): + return _one(found) + return None + + +def _folder_texts(root: Path) -> list[str]: + return [_read(py) for py in root.rglob("*.py")] + + +def _read(path: Path) -> str: + try: + return path.read_text(encoding="utf-8") + except (OSError, UnicodeDecodeError): + return "" + + +def _one(found: list[WebsiteSource]) -> Optional[WebsiteSource]: + distinct = [source for i, source in enumerate(found) if source not in found[:i]] + return distinct[0] if len(distinct) == 1 else None + + +def _website_sources(texts: Iterable[str]) -> list[WebsiteSource]: + found = [] + for text in texts: + if "website" not in text: + continue + try: + tree = ast.parse(text) + except SyntaxError: + continue + for node in ast.walk(tree): + if isinstance(node, ast.ClassDef): + call = next((d for d in node.decorator_list if _decorator_name(d) == "website"), None) + if isinstance(call, ast.Call): + found.append(_website_source(call, node)) + return found + + +def _website_source(call: ast.Call, cls: ast.ClassDef) -> WebsiteSource: + given = {kw.arg: kw.value for kw in call.keywords if kw.arg} + live = given.get("live") + settings = next((n for n in cls.body if isinstance(n, ast.ClassDef) and n.name == "Settings"), None) + secrets = [n.target.id for n in (settings.body if settings is not None else []) + if isinstance(n, ast.AnnAssign) and isinstance(n.target, ast.Name) and _secret(n.annotation)] + return WebsiteSource(live=live.value if isinstance(live, ast.Constant) and isinstance(live.value, bool) else None, + domains=_names(given.get("domains")), allow_hosts=_names(given.get("allow_hosts")), + secret_settings=secrets) + + +def _names(node: Optional[ast.expr]) -> Optional[list[str]]: + if isinstance(node, (ast.List, ast.Tuple)) and all( + isinstance(e, ast.Constant) and isinstance(e.value, str) for e in node.elts): + return [e.value for e in node.elts] + return None + + +def _secret(annotation: ast.expr) -> bool: + """Whether ``annotation`` is ``SecretStr``, optional or not.""" + return any(isinstance(n, ast.Name) and n.id == "SecretStr" or isinstance(n, ast.Attribute) and n.attr == "SecretStr" + for n in ast.walk(annotation)) + + +def _decorator_name(decorator: ast.expr) -> Optional[str]: + func = decorator.func if isinstance(decorator, ast.Call) else decorator + return func.id if isinstance(func, ast.Name) else func.attr if isinstance(func, ast.Attribute) else None def card_names_in_files(files: Mapping[str, Path], dockerfile: str) -> list[str]: diff --git a/tst/data/live_github_website/Dockerfile b/tst/data/live_github_website/Dockerfile new file mode 100644 index 000000000..7080524ee --- /dev/null +++ b/tst/data/live_github_website/Dockerfile @@ -0,0 +1,12 @@ +FROM mirror.gcr.io/library/python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf +RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/* +WORKDIR /app +RUN pip install --no-cache-dir starlette uvicorn pydantic httpx +COPY agentenv_protocol /app/agentenv_protocol +COPY server.py /app/server.py +ENV PYTHONPATH=/app +ENV PORT=8000 +EXPOSE 8000 +RUN useradd --create-home --uid 10001 appuser +USER appuser +CMD ["python", "server.py"] diff --git a/tst/data/live_github_website/server.py b/tst/data/live_github_website/server.py new file mode 100644 index 000000000..b9923dd49 --- /dev/null +++ b/tst/data/live_github_website/server.py @@ -0,0 +1,56 @@ +"""A live site for integration tests: github.com, whose pages its server serves none of. Its data plane reads a public +repository through api.github.com and keeps what a task loads; its session is a test cookie, which GitHub ignores.""" +import json +import urllib.request +from typing import Optional + +from pydantic import BaseModel + +from agentenv_protocol import DataPart, add_data, get_data, reset_data +from agentenv_protocol.website import WebsiteEnvironment, ready_check, session_state, website + + +@website(domains=["github.com"], live=True, + allow_hosts=["*.github.com", "*.githubassets.com", "*.githubusercontent.com"]) +class GitHub(WebsiteEnvironment): + class Settings(BaseModel): + repository: str = "octocat/Hello-World" + + def __init__(self) -> None: + super().__init__() + self.loaded: list = [] + self._repository: Optional[dict] = None + + def repository(self) -> dict: + """The repository as the public API answers, read once: an unauthenticated caller has 60 calls an hour.""" + if self._repository is None: + url = f"https://api.github.com/repos/{self.settings.repository}" + with urllib.request.urlopen(urllib.request.Request(url, headers={"Accept": "application/vnd.github+json"}), + timeout=10) as response: + self._repository = json.load(response) + return self._repository + + @ready_check + def api_answers(self) -> bool: + return self.repository()["full_name"] == self.settings.repository + + @session_state + def account(self, user: Optional[str]) -> dict: + return {"cookies": [{"name": "agentenv_test_session", "value": user or "", "path": "/", "max_age": 600}], + "local_storage": []} + + @reset_data + async def _reset(self) -> None: + self.loaded.clear() + + @add_data + async def _add(self, parts: list) -> None: + self.loaded.extend(part.data for part in parts if part.kind == "data") + + @get_data + async def _state(self) -> list: + return [DataPart(data={"repository": self.repository()["full_name"], "loaded": self.loaded})] + + +if __name__ == "__main__": + GitHub().serve() diff --git a/tst/integration/computer/website_browser_test.py b/tst/integration/computer/website_browser_test.py index 3076977b5..a3fc07c38 100644 --- a/tst/integration/computer/website_browser_test.py +++ b/tst/integration/computer/website_browser_test.py @@ -6,13 +6,16 @@ relaunches it on its profile template, on a new pipe its ready check answers over; and reset_env, a reboot, brings the template back too. Its computer's /dev/shm is Docker's 64 MiB, so it keeps its shared memory in files. In a multi env beside a site that offers website/v1's session, -browser_sign_in signs it in, and again after reset_env starts it afresh. Needs Docker and the local registry on -localhost:5000.""" +browser_sign_in signs it in, and again after reset_env starts it afresh. Beside a live site, github.com, which the +computer reaches over its own network and leaves out of its site table, the browser loads the real site signed in to it +as well as the synthetic one, and the run is marked live. Needs Docker, the local registry on localhost:5000 and, for +the live site, github.com.""" import asyncio import json import platform import re +import time import uuid from typing import Optional @@ -289,3 +292,71 @@ async def test_browser_sign_in_signs_a_multi_envs_browser_in_to_its_site_and_aga assert not report.failed assert (before, signed, after_reset, again) == (None, "alex", None, "alex") assert context.metadata["browser_steps"]["in"]["names"] == ["shop.test"] + + +@pytest.fixture(scope="module") +def live_site(session_site): + """A live website env at github.com, under session_site's local id: its server reads a public repository through + api.github.com.""" + uid = uuid.uuid4().hex[:8] + with namespace_routing(): + return WebsiteEnv.put(id=f"{session_site.id.rsplit('/', 1)[0]}/github", environment_name="github", + domains=["github.com"], live=True, + allow_hosts=["*.github.com", "*.githubassets.com", "*.githubusercontent.com"], + server_docker_image_artifact=_image("live_github_website", f"live-github-{uid}")) + + +async def _browser(computer: DeployedEnv, op: str, params: Optional[dict] = None) -> dict: + owner, _, card = find_browser(computer, None) + return await protocol_v1.invoke_extension(owner.environment_url, card, BROWSER, params or {}, 60, method=op, + headers=owner.control_headers) + + +async def _loads(computer: DeployedEnv, url: str, title: str, timeout_s: float = 90) -> object: + """True once the tab ``tabs/open`` opens at ``url`` shows ``title`` in its own, else the tabs it last listed.""" + await _browser(computer, "tabs/open", {"url": url}) + deadline, tabs = time.monotonic() + timeout_s, [] + while time.monotonic() < deadline: + tabs = (await _browser(computer, "tabs/list"))["tabs"] + if any(tab["url"].startswith(url) and title in (tab.get("title") or "") for tab in tabs): + return True + await asyncio.sleep(2) + return tabs + + +@pytest.mark.asyncio +async def test_a_live_site_beside_a_synthetic_one_loads_at_its_real_names_signed_in_and_marks_the_run_live( + website_browser_desktop, session_site, live_site): + with namespace_routing(): + office = MultiEnv.put(id=f"{session_site.id.rsplit('/', 1)[0]}/office-live", mcp_server_envs=[], + website_envs=[session_site, live_site], + computer_envs=[Env.get(website_browser_desktop.id)], name="office") + context = TaskStepContext() + try: + [deployed] = (await DeployEnvTaskStep( + id="office", version=None, env_id=office.id, sandbox_type="local", ttl_seconds=900, + child_envs={"desktop": {**CONTAINER, "env_params": {"form": "container", "site_routing": True}}}, + ).execute(context)).deployed_envs + computer = DeployedEnv.from_dict(deployed.child_envs["desktop"]) + for site, user in ((live_site, "alex"), (session_site, "bea")): + await BrowserSignInTaskStep(id=f"in-{site.environment_name}", version=None, env_id=office.id, + site=site.id, user=user).execute(context) + github = await _loads(computer, "https://github.com/", "GitHub") + cookies = (await _browser(computer, "cookies/get", {"urls": ["https://github.com/"]}))["cookies"] + shop_user = await _signed_in(computer) + child = deployed.child_env("github", mcp=False) + exported = await protocol_v1.get_data(child.base_url(deployed.environment_url), + headers=deployed.control_headers) + finally: + report = await teardown_run(context) + + assert not report.failed + assert computer.metadata["computer"]["sites"]["names"]["routes"] == {"shop": ["shop.test"]} + assert github is True, github + assert {"name": "agentenv_test_session", "value": "alex"} in [ + {key: cookie[key] for key in ("name", "value")} for cookie in cookies] + assert shop_user == "bea" + assert "octocat/Hello-World" in json.dumps(exported.model_dump(mode="json")) + assert context.metadata["browser_steps"]["in-github"]["names"] == ["github.com"] + assert (context.metadata["reproducible"], [site["environment_name"] for site in + context.metadata["live_websites"][office.id]]) == (False, ["github"]) diff --git a/tst/unit/cli/website_live_cli_test.py b/tst/unit/cli/website_live_cli_test.py new file mode 100644 index 000000000..e2914045a --- /dev/null +++ b/tst/unit/cli/website_live_cli_test.py @@ -0,0 +1,72 @@ +"""agent-env env website put for a live site: live, its names, allow_hosts and secret settings read from the server's +@website(...) when no flag gives them, a flag the source contradicts refused, a secret's value refused, and a registry +image's stated with flags.""" + +from unittest.mock import patch + +from click.testing import CliRunner + +from agent_env.artifact.artifacts.docker_image import DockerImageArtifact +from agent_env.artifact.store import get_artifact_store +from agent_env.cli.env.website import website +from agent_env.env import Env + +SOURCE = '''from pydantic import BaseModel, SecretStr +from agentenv_protocol.website import WebsiteEnvironment, website + + +@website(domains=["github.com"], live=True, allow_hosts=["*.githubassets.com"]) +class GitHub(WebsiteEnvironment): + class Settings(BaseModel): + token: SecretStr +''' + + +def _stored(id, **_): + return get_artifact_store().put_document(DockerImageArtifact( + id=id, description="", image_name=f"registry.example/{id}@sha256:" + "a" * 64)) + + +def _put(*args): + return CliRunner().invoke(website, ["put", "--id", "github-site", "--environment-name", "github", *args, + "--skip-validation"]) + + +def _built(tmp_path, *args): + (tmp_path / "Dockerfile").write_text("FROM scratch\n") + (tmp_path / "site.py").write_text(SOURCE) + with patch("agent_env.cli.env.website.build_image"), \ + patch("agent_env.artifact.artifacts.docker_image.DockerImageArtifact.put", + side_effect=lambda **k: _stored(k["id"])): + return _put("--server-dockerfile", str(tmp_path / "Dockerfile"), *args) + + +def test_a_live_site_is_stored_as_its_source_declares_it(local_stores, tmp_path): + result = _built(tmp_path, "--setting", "token=env:GH_TOKEN") + + assert result.exit_code == 0, result.output + assert "A live site at github.com, its pages reaching *.githubassets.com" in result.output + env = Env.get("github-site") + assert (env.live, env.domains, env.allow_hosts, env.settings, env.secret_settings) == ( + True, ["github.com"], ["*.githubassets.com"], {"token": "env:GH_TOKEN"}, ["token"]) + + +def test_put_refuses_a_flag_the_source_contradicts_and_a_secrets_value(local_stores, tmp_path): + contradicted = _built(tmp_path, "--no-live") + plain = _built(tmp_path, "--setting", "token=ghp_plain") + + assert contradicted.exit_code == 1 and "live is False here, but the site's @website(...) declares live=True" in ( + contradicted.output) + assert plain.exit_code == 1 and "settings.token is a secret" in plain.output + + +def test_a_registry_images_live_site_is_stated_with_flags(local_stores): + with patch("agent_env.cli.env.website.DockerImageArtifact.put_ref", side_effect=lambda id, **k: _stored(id)): + result = _put("--server-image-ref", "ghcr.io/acme/github:3", "--live", "--domain", "github.com", + "--allow-host", "api.github.com") + unnamed = _put("--server-image-ref", "ghcr.io/acme/github:3", "--live") + + assert result.exit_code == 0, result.output + env = Env.get("github-site") + assert (env.live, env.domains, env.allow_hosts) == (True, ["github.com"], ["api.github.com"]) + assert unnamed.exit_code == 1 and "a live website env names the domains the real site is at" in unnamed.output diff --git a/tst/unit/env/envs/test_multi_env_computers.py b/tst/unit/env/envs/test_multi_env_computers.py index 4a8a92683..7b3da1199 100644 --- a/tst/unit/env/envs/test_multi_env_computers.py +++ b/tst/unit/env/envs/test_multi_env_computers.py @@ -52,7 +52,10 @@ def __init__(self, name: str, *, fails: Exception | None = None, refusal: str | self.apps, self.reset_fails, self.route_fails, self.load_fails = apps, resets, routes, loads def composed_apps(self): - return [SimpleNamespace(environment_name=app) for app in self.apps] + return [SimpleNamespace(environment_name=app, declaration=SimpleNamespace(browser=False)) for app in self.apps] + + def app_defaults(self): + return {app: {} for app in self.apps} def holds(self, environment_name): return environment_name in (self.environment_name, *self.apps) diff --git a/tst/unit/env/envs/test_website_live.py b/tst/unit/env/envs/test_website_live.py new file mode 100644 index 000000000..2bd32e059 --- /dev/null +++ b/tst/unit/env/envs/test_website_live.py @@ -0,0 +1,227 @@ +"""Live website envs: a website env with ``live = true`` is a real site, such as github.com, whose server serves none of +its pages. The env stores ``live``, ``allow_hosts`` and ``settings`` as it stores ``domains``, read from its server's +``@website(...)`` when they aren't given. A gateway gives it no site route and hands its server its settings, each +reference resolved only then. A computer routing a multi env's sites leaves it out of its site table, and one whose +network is an allowlist may reach its names, a synthetic site's claim winning. A computer's start tabs must name a +site of the run.""" + +from __future__ import annotations + +import json +import logging +import re +from types import SimpleNamespace +from unittest.mock import MagicMock + +import pytest +import yaml +from agentenv_protocol.website import WEBSITE_EXTENSION_URI, WEBSITE_SETTINGS_ENV + +from agent_env.artifact import Artifact +from agent_env.env.env import DeployedGatewayEnv, Env +from agent_env.env.envs._multi_computers import start_tab_problems +from agent_env.env.envs.computer.utils import sites +from agent_env.env.envs.multi_env import MultiEnv +from agent_env.env.envs.service_db import ServiceDBConfig +from agent_env.env.envs.website import WebsiteEnv, declared_live +from agent_env.providers.env_providers.constants import GATEWAY_SERVICE_NAME +from agent_env.providers.env_providers.env_computer_provider import _reaching +from agent_env.providers.env_providers.env_gateway_provider import (EnvironmentGatewayProvider, WebsiteConfig, + _redact_compose_secrets, website_config) +from agent_env.providers.env_state import LocalPostgresStateProvider +from agent_env.providers.sandbox_providers.sandbox import NetworkPolicy +from agent_env.utils import card_naming +from agent_env.utils.card_naming import WebsiteSource + +SOURCE = '''from pydantic import BaseModel, SecretStr +from agentenv_protocol.website import WebsiteEnvironment, website + + +@website(domains=["github.com"], live=True, allow_hosts=["*.githubassets.com"]) +class GitHub(WebsiteEnvironment): + class Settings(BaseModel): + token: SecretStr + org: str = "octo" +''' + + +def _live(**overrides) -> WebsiteEnv: + fields = dict(id="github-site", version=1, server_docker_image_artifact=MagicMock(image_name="github-server"), + environment_name="github", domains=["github.com"], live=True, + allow_hosts=["*.githubassets.com", "api.github.com"], settings={"token": "env:GH_TOKEN", "org": "octo"}, + secret_settings=["token"]) + return WebsiteEnv(**{**fields, **overrides}) + + +def _synthetic(name: str = "shop", domains: tuple[str, ...] = ("shop.test",)) -> WebsiteEnv: + return WebsiteEnv(id=f"{name}-site", version=1, server_docker_image_artifact=MagicMock(image_name=f"{name}-server"), + environment_name=name, domains=list(domains)) + + +def test_a_live_website_env_keeps_what_it_is_in_its_record_and_its_env_toml(monkeypatch): + env = _live() + data = env.to_dict() + monkeypatch.setattr(Artifact, "get", classmethod(lambda cls, id, version=None: env.server_docker_image_artifact)) + monkeypatch.setattr(WebsiteEnv, "provider_toml", lambda self, out, provider_type: {}) + + back = WebsiteEnv.from_dict(data) + toml = env.to_toml(MagicMock()) + + assert (back.live, back.allow_hosts, back.settings, back.secret_settings) == ( + True, ["*.githubassets.com", "api.github.com"], {"token": "env:GH_TOKEN", "org": "octo"}, ["token"]) + assert {key: toml[key] for key in ("live", "allow_hosts", "settings")} == { + "live": True, "allow_hosts": ["*.githubassets.com", "api.github.com"], + "settings": {"token": "env:GH_TOKEN", "org": "octo"}} + assert "secret_settings" not in toml # read from the site's source + assert not {"live", "allow_hosts", "settings", "secret_settings"} & _synthetic().to_dict().keys() + + +@pytest.mark.parametrize(("overrides", "problem"), [ + ({"domains": []}, "a live website env names the domains the real site is at"), + ({"live": False}, "allow_hosts are the other hosts a live site's pages reach; set live = true"), + ({"allow_hosts": ["https://github.com"]}, "allow_hosts:"), + ({"settings": {"token": "ghp_plain"}}, "settings.token is a secret, so it's a secret:KEY or env:NAME reference"), + ({"settings": {"token": "env:T", "orgs": ["a"]}}, "settings.orgs is a string, number or boolean"), + ({"settings": {"token": "env:T", "1org": "a"}}, "settings: '1org' isn't a setting's name"), +], ids=["no-domains", "allow-hosts-not-live", "bad-allow-host", "plain-secret", "list-setting", "bad-name"]) +def test_a_website_env_refuses_what_a_live_site_cant_be(overrides, problem): + with pytest.raises(ValueError, match=re.escape(problem)): + _live(**overrides) + + +def test_put_reads_live_its_names_allow_hosts_and_secret_settings_from_the_servers_source(tmp_path): + (tmp_path / "Dockerfile").write_text("FROM scratch\n") + (tmp_path / "site.py").write_text(SOURCE) + + source = card_naming.website_source_from_source(str(tmp_path / "Dockerfile"), None) + + assert source == WebsiteSource(live=True, domains=["github.com"], allow_hosts=["*.githubassets.com"], + secret_settings=["token"]) + assert declared_live(source) == ({"live": True, "allow_hosts": ["*.githubassets.com"], "secret_settings": ["token"], + "domains": ["github.com"]}, []) + _, problems = declared_live(source, live=False, allow_hosts=["api.github.com"]) + assert problems == ["live is False here, but the site's @website(...) declares live=True", + "allow_hosts is ['api.github.com'] here, but the site's @website(...) declares " + "allow_hosts=['*.githubassets.com']"] + + +def test_a_synthetic_sites_names_stay_as_given_and_a_source_with_two_sites_or_no_literals_declares_nothing(tmp_path): + assert declared_live(WebsiteSource(live=False, domains=["shop.test"])) == ( + {"live": False, "allow_hosts": [], "secret_settings": []}, []) + other = SOURCE.replace('live=True, allow_hosts=["*.githubassets.com"]', "live=True").replace("GitHub", "Gist") + (tmp_path / "a.py").write_text(SOURCE) + (tmp_path / "b.py").write_text(other) + assert card_naming.website_source_in_files({"a.py": tmp_path / "a.py", "b.py": tmp_path / "b.py"}, "Dockerfile") is None + (tmp_path / "c.py").write_text("@website(domains=DOMAINS, live=LIVE)\nclass Site(WebsiteEnvironment):\n pass\n") + assert card_naming.website_source_in_files({"c.py": tmp_path / "c.py"}, "Dockerfile") == WebsiteSource() + + +def test_an_env_toml_takes_what_the_source_declares_and_refuses_what_it_contradicts(): + def refuse(problems): + raise ValueError(problems) + + ctx = SimpleNamespace(config_problem=lambda problem: f"env.toml: {problem}", refuse=refuse, + accept_env=lambda data, cls, named_by: dict(data), + website_source=lambda image_key: WebsiteSource(live=True, domains=["github.com"], + allow_hosts=["api.github.com"], + secret_settings=["token"])) + data = {"backend_image": "server", "environment_name": "github", "settings": {"token": "secret:gh"}} + + assert WebsiteEnv.accept_toml(data, ctx) == {**data, "live": True, "domains": ["github.com"], + "allow_hosts": ["api.github.com"], "secret_settings": ["token"]} + with pytest.raises(ValueError, match="live is False here"): + WebsiteEnv.accept_toml({**data, "live": False}, ctx) + with pytest.raises(ValueError, match="settings.token is a secret"): + WebsiteEnv.accept_toml({**data, "settings": {"token": "ghp_plain"}}, ctx) + + +def test_a_live_site_gets_no_site_route_and_its_settings_resolved_only_in_its_container(monkeypatch): + monkeypatch.setenv("GH_TOKEN", "ghp_resolved") + + compose = EnvironmentGatewayProvider().create_docker_compose( + mcp_servers=[], gateway_image="agent-gateway", + website_configs=[website_config(_live()), + WebsiteConfig(backend_image="shop-server", frontend_image=None, environment_name="shop")], + state_provider=LocalPostgresStateProvider(service_db_config=ServiceDBConfig()), + state_instance=LocalPostgresStateProvider.default_instance()) + + services = yaml.safe_load(compose)["services"] + gateway = dict(item.split("=", 1) for item in services[GATEWAY_SERVICE_NAME]["environment"]) + assert gateway["WEBSITE_URLS"] == "shop=http://shop-website-backend:8000" + assert "github=http://github-website-backend:8000" in gateway["REST_PROXY_URLS"].split(",") + handed = dict(item.split("=", 1) for item in services["github-website-backend"]["environment"]) + assert json.loads(handed[WEBSITE_SETTINGS_ENV]) == {"token": "ghp_resolved", "org": "octo"} + assert WEBSITE_SETTINGS_ENV not in dict(item.split("=", 1) for item in services["shop-website-backend"]["environment"]) + assert "ghp_resolved" not in _redact_compose_secrets(compose) + + +@pytest.mark.asyncio +async def test_a_computer_routes_a_multi_envs_synthetic_sites_and_leaves_its_live_one_to_the_real_site(monkeypatch): + record = DeployedGatewayEnv( + env_id="office", env_version=1, sandbox_id="gw", sandbox_type="local", gateway_url="http://gw", + environment_card={"name": "office", "children_environments": [ + {"name": "shop", "environment_name": "shop", "url": "/svc/shop/agentenv", "capabilities": {"extensions": [ + {"uri": WEBSITE_EXTENSION_URI, "params": {"domains": ["shop.test"], "browse_url": "/website/shop/"}}]}}, + {"name": "github", "environment_name": "github", "url": "/svc/github/agentenv", "capabilities": { + "extensions": [{"uri": WEBSITE_EXTENSION_URI, "params": { + "domains": ["github.com"], "live": True, "allow_hosts": ["api.github.com"]}}]}}]}) + monkeypatch.setattr(Env, "get", classmethod(lambda cls, id, version=None: SimpleNamespace( + website_envs=[_synthetic(), _live()]))) + + found = await sites.deployed_sites([record]) + + assert [(site["name"], site["domains"]) for site in found] == [("shop", ["shop.test"])] + + +def test_a_multi_env_lets_its_computers_reach_its_live_sites_names_a_synthetic_sites_claim_winning(caplog): + office = MultiEnv(id="office", version=1, mcp_server_envs=[], + website_envs=[_synthetic(name="api", domains=("api.github.com",)), _live()]) + + with caplog.at_level(logging.WARNING): + hosts = office.live_hosts() + + assert hosts == ["github.com", "*.githubassets.com"] + assert "names 'api.github.com', which the synthetic site at 'api.github.com' claims" in caplog.text + gist = MultiEnv(id="office", version=1, mcp_server_envs=[], + website_envs=[_synthetic(name="gist", domains=("gist.github.com",)), _live(allow_hosts=["*.github.com"])]) + assert gist.live_hosts() == ["github.com", "*.github.com"] # the wider name stays, gist.github.com routed within it + assert office.live_websites() == [{"env_id": "github-site", "environment_name": "github", + "domains": ["github.com"], "allow_hosts": ["*.githubassets.com", "api.github.com"]}] + + +def test_a_live_and_a_synthetic_site_may_claim_one_name_and_two_live_ones_may_not(): + MultiEnv(id="office", version=1, mcp_server_envs=[], website_envs=[_synthetic(domains=("github.com",)), _live()]) + with pytest.raises(sites.SiteConflict, match="'github.com'"): + MultiEnv(id="office", version=1, mcp_server_envs=[], + website_envs=[_live(), _live(id="gist-site", environment_name="gist", allow_hosts=[])]) + + +def test_a_computer_routing_sites_opens_start_tabs_only_at_names_a_website_of_the_run_claims(): + chromium = SimpleNamespace(environment_name="chromium", declaration=SimpleNamespace(browser=True)) + computer = SimpleNamespace(composed_apps=lambda: [chromium], + app_defaults=lambda: {"chromium": {"start_urls": ["https://shop.test/"]}}) + office = MultiEnv(id="office", version=1, mcp_server_envs=[], website_envs=[_synthetic(), _live()]) + + def entry(urls=None, routing=True): + settings = {"app_settings": {"chromium": {"start_urls": urls}}} if urls is not None else {} + return {"env_params": {**({"site_routing": routing} if routing else {}), **settings}} + + names = office._start_tab_names(entry()) + assert names == ["shop.test", "github.com", "*.githubassets.com", "api.github.com"] + assert office._start_tab_names(entry(routing=["another-site"])) == names[1:] + assert start_tab_problems(computer, entry(), names) == [] + assert start_tab_problems(computer, entry(["https://github.com/", "https://x.githubassets.com/", "about:blank"]), + names) == [] + [problem] = start_tab_problems(computer, entry(["https://example.com/"]), names) + assert "chromium's start tab https://example.com/ names example.com, which none of the multi env's" in problem + assert start_tab_problems(computer, entry(["https://example.com/"], routing=False), names) == [] + + +def test_a_computers_allowlist_takes_the_live_hosts_and_one_that_allows_everything_stays_as_it_is(): + provider = SimpleNamespace(url_from_sandbox=lambda url: url) + allowlist = NetworkPolicy(mode="allowlist", allow_hosts=("pypi.org",)) + + assert _reaching(allowlist, provider, "https://gw.example", ("github.com", "*.githubassets.com")).allow_hosts == ( + "pypi.org", "gw.example", "github.com", "*.githubassets.com") + assert _reaching(NetworkPolicy(), provider, "https://gw.example", ("github.com",)) == NetworkPolicy() + assert _reaching(None, provider, "", ("github.com",)) is None diff --git a/tst/unit/env/envs/test_website_server_image.py b/tst/unit/env/envs/test_website_server_image.py index 8bfc78af5..f7bd47483 100644 --- a/tst/unit/env/envs/test_website_server_image.py +++ b/tst/unit/env/envs/test_website_server_image.py @@ -64,7 +64,7 @@ def refuse(problems): raise ValueError(problems) ctx = SimpleNamespace(config_problem=lambda problem: f"env.toml: {problem}", refuse=refuse, - accept_env=lambda data, cls, named_by: data) + accept_env=lambda data, cls, named_by: data, website_source=lambda image_key: None) data = {"backend_image": "server", "environment_name": "slack", "health": "/healthz"} assert WebsiteEnv.accept_toml(data, ctx) == data with pytest.raises(ValueError, match="env.toml: health: 'healthz' is not a path"): diff --git a/tst/unit/task_step/deploy_env_test.py b/tst/unit/task_step/deploy_env_test.py index f3b916f22..d0f47a62a 100644 --- a/tst/unit/task_step/deploy_env_test.py +++ b/tst/unit/task_step/deploy_env_test.py @@ -23,9 +23,10 @@ def _deployed_env(metadata): ) -def _fake_env(deployed_env): +def _fake_env(deployed_env, live_websites=()): env = MagicMock() env.deploy = AsyncMock(return_value=deployed_env) + env.live_websites.return_value = list(live_websites) return env @@ -190,6 +191,21 @@ async def test_execute_persists_annotations_to_the_instance_record(): assert deployed_env.metadata["deploy_step_id"] == "t-1.deploy_env" +@pytest.mark.asyncio +async def test_a_run_that_deploys_a_live_website_is_marked_live_and_not_reproducible(): + deployed_env = _deployed_env(None) + deployed_env.instance_id = "art-1-abcd1234" + live = [{"env_id": "github-site", "environment_name": "github", "domains": ["github.com"], "allow_hosts": []}] + step = DeployEnvTaskStep(id="t-1.deploy_env", version=None, env_id="art-1", env_version=2) + + with patch("agent_env.env.env.Env") as Env, patch(_UPDATE_TARGET) as update: + Env.get.return_value = _fake_env(deployed_env, live) + context = await step.execute(TaskStepContext()) + + update.assert_called_once_with("art-1-abcd1234", {"deploy_step_id": "t-1.deploy_env", "live": True}) + assert (context.metadata["live_websites"], context.metadata["reproducible"]) == ({"art-1": live}, False) + + @pytest.mark.asyncio async def test_execute_copies_no_run_metadata_onto_the_instance_record(): deployed_env = _deployed_env(None) @@ -359,6 +375,7 @@ def _preflight(env, **options) -> list[str]: def _env_with_fixed_deploy(deployed_env): """An env whose deploy() takes a fixed param list, like every built-in env.""" env = MagicMock() + env.live_websites.return_value = [] async def deploy(ttl_seconds=10800, disk_size_gb=10, gateway_mode=None, cpu=None, memory_mb=None, sandbox_type=None, priority=None, env_state_type=None, From 74cebee47da9df752deb5a6da2b0240e6e1a077f Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 15:01:57 -0800 Subject: [PATCH 2/4] fix(website): review fixes for live sites; a browser alone reaches them through its forward port - A browser alone's forward port refused every host but its sites, so a live site's hosts never loaded there. It now takes them straight to the real site (CONNECT tunnelled, a URL sent on as its path), a synthetic name still winning and never an address or a .localhost name; with only live sites it is installed all the same. - A VM gateway whose sites were all live crashed listing their frontends. - Compose interpolated a $ in a site's settings; each is doubled. - A secret setting with a ?default held its value; it must be a bare reference, as a computer's secret settings are. - A source leaving live and allow_hosts out declares their defaults, so a flag can't contradict it. - The save leaves a start tab to the deploy while a routed site stores no names, since its card may declare any. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- src/agent_env/config/__init__.py | 2 + src/agent_env/config/loader.py | 7 ++ src/agent_env/env/envs/computer/utils/gate.py | 12 +-- .../env/envs/computer/utils/site_proxy.py | 66 ++++++++++++++-- .../env/envs/computer/utils/sites.py | 17 ++-- src/agent_env/env/envs/multi_env.py | 20 +++-- src/agent_env/env/envs/website.py | 7 +- .../env_providers/env_computer_provider.py | 12 +-- .../env_providers/env_gateway_provider.py | 24 +++--- src/agent_env/utils/card_naming.py | 13 +++- .../computer/browser_computer_test.py | 27 +++++++ tst/unit/env/envs/test_computer_env.py | 2 +- tst/unit/env/envs/test_computer_site_proxy.py | 77 ++++++++++++++++++- tst/unit/env/envs/test_computer_sites.py | 18 +++++ tst/unit/env/envs/test_website_live.py | 42 ++++++++-- 16 files changed, 282 insertions(+), 66 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 5a7abf335..a3f39c29a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -121,7 +121,7 @@ and deserialized through a registry. | Package | Contents | |---|---| -| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | +| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), a browser alone's forward port takes them straight to the real site (`site_proxy.py` `direct`), and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | | `artifact/` | Immutable versioned artifacts; every `put()` creates a new version. The document goes to the document store, binary payloads to the object store. Every artifact but an environment universe, whose `metadata` key holds its legacy metadata file ids, carries a `metadata` dict outside its versioned fields, changed by compare-and-swap (`merge_metadata`). Built-ins: `CliArtifact` (`cli`), `ComputerArtifact` (`computer`, a base computer: its architectures, OS, session user, settings and container form, whose image `put` reads from its registry into `metadata.validated_images`), `FileArtifact` (`file`), `FileArtifactUniverse` (`file_artifact_universe`), `DockerImageArtifact` (`docker_image`), `EnvironmentArtifact` (`environment`: its environment's files by path, stored as the one wrapped `file_artifact_ref` every environment artifact had when it has one file at its filename, else as `file_artifact_refs`; `get_file_artifacts()` reads either, and a load hands each file to the env's `data/add` with its path, which a pre-v1 load, taking one file, refuses), `EnvironmentUniverseArtifact` (`environment_universe`), `SkillArtifact` (`skill`); `[artifacts] type_aliases` maps another stored spelling of a type onto one of them. A `DockerImageArtifact` holds a tar.gz of its image, or, written by `put_ref` (`--image-ref` on `env mcp-server put` and `a2a-agent put`), only a registry reference with its tag pinned to a digest, which sandboxes pull, or a published image's tag with its `image_digest` and its build context, which sandboxes pull by the digest and build from the context when they can't (the infra images, and a bundle agent built from a shipped agent's sources, `Plan.published`), or, written by `put_context`, only its build context, which a VM sandbox builds before running it (`VmSandbox.load_docker_images`) and Modal builds before creating its container (`ModalSandboxProvider.prepare_image`, outside a fallback chain's deadline); anything else that runs images by name (a container on the local provider, a lone server on any provider but Modal) refuses such an image. A build context (`utils/docker/context.py`) is what `docker build` would send, `.dockerignore` applied, written so equal files give equal bytes and an equal `source_digest`. | | `task/`, `task_step/` | A `Task` holds its `TaskStep`s inline; `Task.run()` executes them as a DAG. `depends_on` (None means all prior steps) gates a step, independent steps run concurrently, `fail_task_on_error` makes a failure fatal or tolerated, `retry_config` rolls a failed span back through the step journal and re-dispatches it. Built-in steps live in `task_step/task_steps/` (`deploy_env`, `deploy_agent`, `prompt_agent`, the verifiers under `verifiers/`, among them `start_computer_recording` and `end_computer_recording`, and more). On an env whose card serves computer-exec/v1 (a computer), `run_code` with `env_id` and `run_container_unit_tests_verifier` with `env_id` run inside it as its session user through the driver: files staged with `data/add` and read with `data/get`, commands run with `run`; `env_outcome_verifier` hands a `verify` that takes an `env_url` keyword the env's address. A computer's framework-only routes (everything but MCP and the card) take the per-deploy control token its provider boots it with (`DeployedEnv.control_token`, `AGENTENV_CONTROL_TOKEN`); framework calls send `deployed.control_headers`, `Task.run` runs every step under `protocol_v1.authorized` with each deployed env's (a `verify` included), and a `run_code` script inside the computer gets `input["env"]["headers"]`. `teardown_run` and the `teardown_sandboxes` step share one core (`task/teardown.py` `tear_down`): the recordings the run left open end with fresh grants, each env still up closes (`Env.close`; a computer env ends any recording still open), what stayed open is registered from its namespace, then every sandbox is terminated by id. `task/episode/` runs a task with some of its agents played from outside, as a training loop does (`EpisodeSpec`). | | `store/` | Four store ABCs with local and cloud implementations: `DocumentStore` (SQLite, MongoDB, DynamoDB, Firestore with MongoDB compatibility), `ObjectStore` (filesystem, S3, Cloud Storage), `ImageStore` (local OCI registry, ECR), `SecretStore` (env vars or file, AWS Secrets Manager, Google Cloud Secret Manager). `VersionedEntityStore` implements the shared versioned get/put logic, `QueryBuilder` is the immutable chained query API, `store/base.py` holds the error types. `store/routing.py` is the local namespaces, `@local` and `@hf` (`is_local_id`), each with a documents file of its own: while the CLI or a `build_app` server runs, a local id and what a local task's run records go to local stores beside the configured ones, and that run's writes to the configured stores are refused. A new backend must pass the conformance kits in `tst/store/`. | diff --git a/src/agent_env/config/__init__.py b/src/agent_env/config/__init__.py index d4e6e8bad..a3f334a43 100644 --- a/src/agent_env/config/__init__.py +++ b/src/agent_env/config/__init__.py @@ -12,6 +12,7 @@ build_store, discover_config_path, interpolate, + is_bare_reference, load_config_file, load_impl, ) @@ -39,6 +40,7 @@ "build_store", "discover_config_path", "interpolate", + "is_bare_reference", "load_config_file", "load_impl", "Config", diff --git a/src/agent_env/config/loader.py b/src/agent_env/config/loader.py index 60efa8ea6..e6a903936 100644 --- a/src/agent_env/config/loader.py +++ b/src/agent_env/config/loader.py @@ -134,6 +134,13 @@ def interpolate(value: Any, *, secret_resolver: Optional[SecretResolver] = None) return value +def is_bare_reference(value: Any) -> bool: + """Whether ``value`` names a secret rather than holding one: an ``env:NAME`` or ``secret:KEY`` reference with a + name and no ``?`` fallback, whose default would be a value.""" + return (isinstance(value, str) and value.startswith((ENV_REF_PREFIX, SECRET_REF_PREFIX)) and "?" not in value + and value not in (ENV_REF_PREFIX, SECRET_REF_PREFIX)) + + def _resolve_ref(prefix: str, value: str, resolve: SecretResolver) -> str: ref, sep, default = value[len(prefix):].partition("?") resolved = resolve(ref) diff --git a/src/agent_env/env/envs/computer/utils/gate.py b/src/agent_env/env/envs/computer/utils/gate.py index 1987b2dc3..b7bafede2 100644 --- a/src/agent_env/env/envs/computer/utils/gate.py +++ b/src/agent_env/env/envs/computer/utils/gate.py @@ -14,7 +14,7 @@ from jsonschema import Draft202012Validator from agent_env.artifact.artifacts.computer import FORMS, ComputerArtifact, SettingValue -from agent_env.config.loader import ENV_REF_PREFIX, SECRET_REF_PREFIX +from agent_env.config.loader import ENV_REF_PREFIX, SECRET_REF_PREFIX, is_bare_reference from agent_env.providers.sandbox_providers.sandbox import NetworkPolicy from agent_env.env.envs.computer.utils.install import for_os, platform_of from agent_env.store.image_store.oci_registry_credentials import names_registry @@ -169,7 +169,7 @@ def check_app_settings(app: ComputerAppEnv, values: dict[str, Any], at: str, os_ else f"{at}.{name}: {app.id} declares no setting {name!r}" for name in values if name not in properties] problems += [f"{at}.{name} is a secret, so it takes a {SECRET_REF_PREFIX}KEY or {ENV_REF_PREFIX}NAME reference " "with no ?default, not its value" for name in values if name in secrets - and not (_names_a_secret(values[name]) or values[name] is None and _optional(properties[name]))] + and not (is_bare_reference(values[name]) or values[name] is None and _optional(properties[name]))] problems += [f"{at}: {name!r} is a required property" for name in required if name in secrets and name not in values] plain = {**schema, "properties": {name: setting for name, setting in properties.items() if name not in secrets}, "required": [name for name in required if name not in secrets]} @@ -207,7 +207,7 @@ def check_computer_settings(base: ComputerArtifact, values: Any) -> tuple[dict[s if (setting := base.config.get(name)) is None: problems.append(f"computer_settings.{name}: {base.id} declares no setting {name!r}") elif setting.secret: - if _names_a_secret(value): + if is_bare_reference(value): checked[name] = value else: problems.append(f"computer_settings.{name} is a secret, so it takes a {SECRET_REF_PREFIX}KEY or " @@ -223,12 +223,6 @@ def check_computer_settings(base: ComputerArtifact, values: Any) -> tuple[dict[s return checked, problems -def _names_a_secret(value: Any) -> bool: - """Whether ``value`` names a secret rather than holding it: a bare reference, as a stored context keeps one.""" - return (isinstance(value, str) and value.startswith((ENV_REF_PREFIX, SECRET_REF_PREFIX)) and "?" not in value - and value not in (ENV_REF_PREFIX, SECRET_REF_PREFIX)) - - def _platform(base: ComputerArtifact, driver: ComputerDriverEnv) -> list[str]: os = base.os if not _matching(base, driver.declaration.supports): diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index c92fdfeb9..6442dab12 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -8,8 +8,8 @@ checks, as the session user, that each name loads. ``serve`` listens on loopback: on 443 for a name served over HTTPS, on 80 to redirect those to HTTPS and to serve ``.localhost`` names over HTTP, on 53 for DNS, over UDP and TCP, when a name is a wildcard, which a hosts file can't list, and, on a browser computer, on ``proxy_port`` (3129) as a forward -proxy, a browser alone's only route out, which serves the same sites to ``CONNECT`` and to requests naming their URLs -and refuses any other host at once; it then drops to ``SERVE_USER`` (``nobody``), else the session user, so the agent, +proxy, a browser alone's only route out, which serves the same sites to ``CONNECT`` and to requests naming their URLs, +takes a live site's hosts (``direct``) straight to them, and refuses any other host at once; it then drops to ``SERVE_USER`` (``nobody``), else the session user, so the agent, which runs as the session user, can't stop it, and ``keep``, as root, starts it again should it end. The site sees itself at ``/`` under its own name: the path goes upstream under the route's prefix, ``X-Forwarded-Host`` and ``X-Forwarded-Proto`` say the name and scheme the browser used, ``AgentEnv-Site-Root`` has the gateway tell the site no @@ -23,6 +23,7 @@ import asyncio import base64 import hashlib +import ipaddress import json import os import re @@ -152,6 +153,7 @@ class Config: proxy_port: Optional[int] = None dns_port: Optional[int] = None nameservers: list[str] = field(default_factory=list) + direct: list[str] = field(default_factory=list) @classmethod def load(cls, path: str) -> Config: @@ -170,7 +172,7 @@ def load(cls, path: str) -> Config: listen=data.get("listen", LOOPBACK), https_port=data.get("https_port", 443), http_port=data.get("http_port", 80), proxy_port=data.get("proxy_port"), dns_port=data.get("dns_port"), - nameservers=list(data.get("nameservers") or [])) + nameservers=list(data.get("nameservers") or []), direct=list(data.get("direct") or [])) def site_for(self, host: str) -> Optional[Site]: """The site ``host`` names: one naming it exactly before one whose wildcard covers it.""" @@ -181,6 +183,17 @@ def site_for(self, host: str) -> Optional[Site]: return site return None + def is_direct(self, host: str) -> bool: + """Whether the forward port takes ``host`` straight to it: a name of a live site's (``direct``), never an + address or a ``.localhost`` name.""" + host = host.lower().rstrip(".") + try: + ipaddress.ip_address(host) + return False + except ValueError: + pass + return bool(host) and not is_local(host) and any(matches(pattern, host) for pattern in self.direct) + def header(headers: list[tuple[str, str]], name: str) -> Optional[str]: return next((value for key, value in headers if key.lower() == name), None) @@ -271,6 +284,18 @@ def upstream_request(method: str, target: str, headers: list[tuple[str, str]], s return ("\r\n".join(lines) + "\r\n\r\n").encode("latin-1") +def direct_head(start: str, headers: list[tuple[str, str]]) -> bytes: + """A request a browser alone named by its URL, as it goes straight to a live site's host: the path alone on its + request line, the proxy's own headers dropped, and a connection that closes after it.""" + method, target, version = start.split(" ") + parts = urlsplit(target) + kept = [(key, value) for key, value in headers + if key.lower() not in {"connection", "keep-alive", "proxy-connection", "proxy-authorization"}] + lines = [f"{method} {urlunsplit(('', '', parts.path or '/', parts.query, ''))} {version}", + *(f"{key}: {value}" for key, value in kept), "Connection: close"] + return ("\r\n".join(lines) + "\r\n\r\n").encode("latin-1") + + def response_head(status_line: str, headers: list[tuple[str, str]], site: Site, *, scheme: str, host: str, connection: str) -> bytes: _, _, status = status_line.partition(" ") @@ -391,19 +416,30 @@ async def _requests(self, reader: asyncio.StreamReader, writer: asyncio.StreamWr async def _proxied(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, peer: str) -> None: while (head := await read_head(reader)) is not None: - method, _, rest = parse_head(head)[0].partition(" ") + start, headers = parse_head(head) + method, _, rest = start.partition(" ") if method == "CONNECT": await self._tunnel(reader, writer, peer, rest.rpartition(" ")[0]) return + target = urlsplit(rest.rpartition(" ")[0]) + if target.hostname and self.config.site_for(target.hostname) is None and self.config.is_direct( + target.hostname): + await self._direct(reader, writer, target.hostname, target.port or 80, direct_head(start, headers)) + return if not await self.exchange(reader, writer, "http", peer, head=head, forward=True): return async def _tunnel(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, peer: str, authority: str) -> None: """A ``CONNECT`` to ``authority``: the site it names, whose certificate the tunnel's TLS needs before any - request in it, then each request in it, as the port serves them.""" + request in it, then each request in it, as the port serves them; or a live site's host, tunnelled straight + to it.""" host = split_host(authority) if (site := self.config.site_for(host)) is None: + if self.config.is_direct(host): + port = authority.rpartition(":")[2] if host != authority else "" + await self._direct(reader, writer, host, int(port) if port.isdigit() else 443) + return self._refuse(writer, "CONNECT", authority) return writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") @@ -415,6 +451,23 @@ async def _tunnel(self, reader: asyncio.StreamReader, writer: asyncio.StreamWrit await writer.start_tls(self.tls) await self._requests(reader, writer, "https" if tls else "http", peer, forward=True) + @staticmethod + async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, host: str, port: int, + head: Optional[bytes] = None) -> None: + """``host``, a live site's, reached straight at ``port``: a tunnel, answered as established, for a ``CONNECT``, + else ``head`` sent on; then both ends relayed until either closes.""" + try: + up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(host, port), UPSTREAM_TIMEOUT_S) + except (OSError, asyncio.TimeoutError) as e: + writer.write(simple_response("502 Bad Gateway", f"{host}:{port} didn't answer: {e}\n", FAILED)) + return + if head is None: + writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") + else: + up_writer.write(head) + await asyncio.gather(writer.drain(), up_writer.drain()) + await asyncio.gather(splice(reader, up_writer), splice(up_reader, writer)) + @staticmethod def _refuse(writer: asyncio.StreamWriter, method: str, host: str) -> None: print(f"site proxy: refused {method} {host}: no site of this computer", file=sys.stderr, flush=True) @@ -1002,7 +1055,8 @@ def install(config_path: str, paths: Paths = Paths()) -> dict: for name, value in given["trust_environment"].items())) trust["environment"] = str(paths.profile) serving = {"user": user, "serve_user": serve_user(user), "sites": sites, - **{key: given[key] for key in ("listen", "https_port", "http_port", "proxy_port") if key in given}} + **{key: given[key] for key in ("listen", "https_port", "http_port", "proxy_port", "direct") + if key in given}} if wildcard: serving.update(dns_port=given.get("dns_port", 53), nameservers=nameservers) if upstream_headers: diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index d3f943b99..98eba5325 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -304,12 +304,12 @@ def browser_environment(display: str) -> dict[str, str]: def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], os_type: str, forward_port: bool, - upstream_headers: Optional[dict[str, dict[str, str]]] = None) -> dict: + upstream_headers: Optional[dict[str, dict[str, str]]] = None, direct: Iterable[str] = ()) -> dict: """What ``site_proxy.py install`` reads: the session user, the CA, the trust files on an ``os_type`` computer and the variables naming them, which its profile script sets as the computer's boot environment does, each route with its certificate and key, and the headers its gateway's sandbox provider asks requests to it to carry (``upstream_headers``, by site), which the proxy sends upstream alone, and with ``forward_port``, for a browser - alone, the forward port.""" + alone, the forward port and the live sites' hosts it takes straight to them (``direct``).""" sites = [] for route in table.routes: site = {"name": route.site, "upstream": route.url, "domains": list(route.domains)} @@ -320,7 +320,7 @@ def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], os_ty sites.append(site) return {"user": user, "ca_pem": issued.ca_pem, "sites": sites, "log": str(LOG), "trust_files": trust_files(os_type), "trust_environment": dict(TRUST_ENVIRONMENTS[os_type]), - **({"proxy_port": FORWARD_PROXY_PORT} if forward_port else {})} + **({"proxy_port": FORWARD_PROXY_PORT, "direct": list(direct)} if forward_port else {})} def reachable_problem(policy: Optional[NetworkPolicy], routes: list[Route]) -> Optional[str]: @@ -425,18 +425,19 @@ async def _exec(self, argv: list[str]) -> str: async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], network: Optional[dict], - valid_for: timedelta, label: str, os_type: str, display: str) -> dict: + valid_for: timedelta, label: str, os_type: str, display: str, live_hosts: Iterable[str] = ()) -> dict: """Have the ``os_type`` computer on ``sandbox`` reach each of ``sites`` by its names: the table made, the CA and certificates issued, the proxy installed and started as root, with its forward port on a computer with no - ``display``, a browser alone's, and each name checked as ``user``; what the deploy record keeps of it. Raises - naming a name that didn't load.""" + ``display``, a browser alone's, which also takes the run's ``live_hosts`` straight to them, and each name checked + as ``user``; what the deploy record keeps of it. Raises naming a name that didn't load.""" requested = NetworkPolicy.from_dict(network) if network else None table = site_table(sites, requested.allow_hosts if requested else ()) table.routes = [Route(r.site, sandbox.url_from_sandbox(r.url), r.domains) for r in table.routes] applied = getattr(sandbox, "network_policy", None) or requested if (problem := reachable_problem(applied, table.routes) or gateway_problem(table.routes)) is not None: raise RuntimeError(f"site routing: {problem}") - if not table.routes: + direct = list(live_hosts) if display == ALONE_DISPLAY else [] + if not table.routes and not direct: return {"names": table.to_dict()} from agent_env.providers.sandbox_providers.sandbox_provider import sandbox_request_headers_for_url @@ -447,7 +448,7 @@ async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], net await channel.write(PROXY, _PROXY_SOURCE.read_text()) await channel.write_secret(CONFIG, json.dumps(proxy_config(table, issued, user=user, os_type=os_type, forward_port=display == ALONE_DISPLAY, - upstream_headers=upstream_headers))) + upstream_headers=upstream_headers, direct=direct))) await channel.run(["chmod", "0600", str(CONFIG)]) output = await channel.run(["python3", str(PROXY), "install", str(CONFIG)]) report = json.loads(output.strip().splitlines()[-1]) diff --git a/src/agent_env/env/envs/multi_env.py b/src/agent_env/env/envs/multi_env.py index be158b4a1..1b8a0bbb5 100644 --- a/src/agent_env/env/envs/multi_env.py +++ b/src/agent_env/env/envs/multi_env.py @@ -231,16 +231,19 @@ def _websites(self) -> dict[str, str]: """Its websites' environment names by env id, the ids a computer's ``site_routing`` may name.""" return {env.id: env.environment_name for env in self.website_envs} - def _start_tab_names(self, entry: Optional[dict]) -> list[str]: + def _start_tab_names(self, entry: Optional[dict]) -> Optional[list[str]]: """The names a computer with ``entry`` may open start tabs at: those of the websites its ``site_routing`` - routes, all of them or those it lists, each site's own or its fallback name, and every live site's domains and - ``allow_hosts``, which the computer reaches the real sites at.""" + routes, all of them or those it lists, and every live site's domains and ``allow_hosts``, which the computer + reaches the real sites at; None while a routed site stores no names, since its card may declare any at + deploy, which checks the start tabs then.""" params = entry.get("env_params") if isinstance(entry, dict) else None routing = params.get("site_routing") if isinstance(params, dict) else None listed = routing if isinstance(routing, list) else None - names = [name for env in self.website_envs if not env.live and (listed is None or env.id in listed) - for name in env.domains or [sites.fallback_name(env.environment_name)]] - return names + [name for env in self.website_envs if env.live for name in [*env.domains, *env.allow_hosts]] + routed = [env for env in self.website_envs if not env.live and (listed is None or env.id in listed)] + if any(not env.domains for env in routed): + return None + return [name for env in routed for name in env.domains] + [ + name for env in self.website_envs if env.live for name in [*env.domains, *env.allow_hosts]] def live_hosts(self) -> list[str]: """The hosts its live websites reach the real sites at, each one's domains and ``allow_hosts``, which a @@ -299,8 +302,9 @@ def child_envs_refusal(self, child_envs: Optional[dict], *, sandbox_type: str | if entry is None or (isinstance(entry, dict) and set(entry) <= set(CHILD_ENV_OPTIONS)): if refusal := _multi_computers.refusal(computer, entry or {}, sandbox_type, self._websites()): problems.append(f"child_envs.{name}: {refusal}" if name in child_envs else refusal) - problems += [f"child_envs.{name}: {problem}" for problem in _multi_computers.start_tab_problems( - computer, entry, self._start_tab_names(entry))] + if (names := self._start_tab_names(entry)) is not None: + problems += [f"child_envs.{name}: {problem}" + for problem in _multi_computers.start_tab_problems(computer, entry, names)] return "; ".join(problems) or None @classmethod diff --git a/src/agent_env/env/envs/website.py b/src/agent_env/env/envs/website.py index 2b661e32f..4ee1c3e93 100644 --- a/src/agent_env/env/envs/website.py +++ b/src/agent_env/env/envs/website.py @@ -26,7 +26,7 @@ from agentenv_protocol import client as protocol_v1 from agent_env.artifact import Artifact, DockerImageArtifact, EnvironmentArtifact -from agent_env.config import ENV_REF_PREFIX, SECRET_REF_PREFIX +from agent_env.config import ENV_REF_PREFIX, SECRET_REF_PREFIX, is_bare_reference from agent_env.artifact.artifacts.docker_image import ProgressCallback, refuse_local_github_build from agent_env.entity.refs import EntityRef from agent_env.env.env import Env, gateway_url_of @@ -530,10 +530,9 @@ def live_problems(live: Any, domains: list[str], allow_hosts: list[str], setting problems.append(f"settings: {name!r} isn't a setting's name") elif not isinstance(value, (str, int, float, bool)): problems.append(f"settings.{name} is a string, number or boolean, not {value!r}") - elif name in secret_settings and not (isinstance(value, str) and value.startswith((SECRET_REF_PREFIX, - ENV_REF_PREFIX))): + elif name in secret_settings and not is_bare_reference(value): problems.append(f"settings.{name} is a secret, so it's a {SECRET_REF_PREFIX}KEY or {ENV_REF_PREFIX}NAME " - "reference, never its value") + "reference with no ?default, never its value") return problems diff --git a/src/agent_env/providers/env_providers/env_computer_provider.py b/src/agent_env/providers/env_providers/env_computer_provider.py index 6d29537a2..066848e39 100644 --- a/src/agent_env/providers/env_providers/env_computer_provider.py +++ b/src/agent_env/providers/env_providers/env_computer_provider.py @@ -194,6 +194,7 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par "computer": {"id": env.metadata["computer"]["id"], "version": env.metadata["computer"]["version"]}, "port": env.driver.port, "display": values.get("resolution"), "network": env.network, "computer_settings": recorded_settings(computer, values), + **({"live_hosts": list(live_hosts)} if live_hosts else {}), **({"app_settings": app_settings} if app_settings else {}), **({"tool_prefix": tool_prefix} if tool_prefix else {}), "env_params": params.model_dump(exclude_defaults=True)}}, @@ -245,7 +246,8 @@ async def _finish_boot(env: ComputerEnv, record: DeployedSandboxEnv, sandbox: Sa didn't load. What the record keeps of the routing, or None without it.""" routed = None if params.site_routing: - routed = await _route_sites(env, sandbox, params, valid_for_s) + routed = await _route_sites(env, sandbox, params, valid_for_s, + tuple(record.metadata["computer"].get("live_hosts") or ())) await _launch_held_apps(env, record, record.environment_url, params.sites) await _require_display(record, resolution) if start_tabs := await _start_tabs(record): @@ -255,12 +257,12 @@ async def _finish_boot(env: ComputerEnv, record: DeployedSandboxEnv, sandbox: Sa async def _route_sites(env: ComputerEnv, sandbox: Sandbox, params: computer_params.ComputerParams, - valid_for_s: int) -> dict: - """The computer on ``sandbox`` routed to ``params.sites`` under a CA of its own, valid ``valid_for_s``; what its - record keeps of it.""" + valid_for_s: int, live_hosts: tuple[str, ...] = ()) -> dict: + """The computer on ``sandbox`` routed to ``params.sites`` under a CA of its own, valid ``valid_for_s``, a browser + alone's forward port taking ``live_hosts`` straight to them; what its record keeps of it.""" routed = await sites.route(sandbox, params.sites, user=env.computer.session.user, network=env.network, valid_for=timedelta(seconds=valid_for_s), label=env.environment_name, - os_type=env.computer.os.type, display=env.computer.os.display) + os_type=env.computer.os.type, display=env.computer.os.display, live_hosts=live_hosts) return {**routed, "valid_for_s": valid_for_s} diff --git a/src/agent_env/providers/env_providers/env_gateway_provider.py b/src/agent_env/providers/env_providers/env_gateway_provider.py index 6fc0d55e8..78cd52454 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -959,15 +959,9 @@ async def _stand_up(_spec): mcp_url = _mcp_url(gateway_url, card) logger.info(f"MCP endpoint: {mcp_url}") - # Build frontend URLs proxied through the gateway - website_frontend_urls: dict[str, str] | None = None - if website_configs: - website_frontend_urls = { - wc.environment_name: f"{gateway_url}/website/{wc.environment_name}/" - for wc in website_configs if not wc.live - } or None - for svc_name, url in website_frontend_urls.items(): - logger.info(f"Website frontend ({svc_name}): {url}") + website_frontend_urls = _frontend_urls(gateway_url, website_configs) + for svc_name, url in (website_frontend_urls or {}).items(): + logger.info(f"Website frontend ({svc_name}): {url}") logger.info(f"Gateway URL: {gateway_url}") return DeployedGateway( @@ -1668,13 +1662,21 @@ def _redact_compose_secrets(compose_content: str) -> str: ) +def _frontend_urls(gateway_url: str, website_configs: Optional[list[WebsiteConfig]]) -> Optional[dict[str, str]]: + """Each synthetic site's page route through the gateway, by its environment name; None with none, as when every + site is live.""" + return {wc.environment_name: f"{gateway_url}/website/{wc.environment_name}/" + for wc in website_configs or [] if not wc.live} or None + + def _settings_lines(settings: dict) -> list[str]: """The compose line handing a site its ``settings`` in ``WEBSITE_SETTINGS_ENV``, each ``secret:`` or ``env:`` - reference resolved, quoted so any value is one YAML string; none for a site with none.""" + reference resolved, quoted so any value is one YAML string, with each ``$`` doubled so Compose interpolates none; + none for a site with none.""" if not settings: return [] resolved = {name: get_config().resolve_reference(value) for name, value in settings.items()} - return [f" - {json.dumps(f'{WEBSITE_SETTINGS_ENV}={json.dumps(resolved)}')}"] + return [f" - {json.dumps(f'{WEBSITE_SETTINGS_ENV}={json.dumps(resolved)}'.replace('$', '$$'))}"] # Budget for reading the gateway's /state once its card is served: answering it runs tool discovery, one server at a diff --git a/src/agent_env/utils/card_naming.py b/src/agent_env/utils/card_naming.py index 9aa3a870f..c319c1e40 100644 --- a/src/agent_env/utils/card_naming.py +++ b/src/agent_env/utils/card_naming.py @@ -169,14 +169,21 @@ def _website_sources(texts: Iterable[str]) -> list[WebsiteSource]: def _website_source(call: ast.Call, cls: ast.ClassDef) -> WebsiteSource: + """What ``call``, a class's ``@website(...)``, declares: ``live`` and ``allow_hosts`` as written, their defaults + when left out, and unknown (None) when written as other than a literal or when the call unpacks ``**`` keywords, + which may hold them.""" given = {kw.arg: kw.value for kw in call.keywords if kw.arg} + unpacked = any(kw.arg is None for kw in call.keywords) live = given.get("live") settings = next((n for n in cls.body if isinstance(n, ast.ClassDef) and n.name == "Settings"), None) secrets = [n.target.id for n in (settings.body if settings is not None else []) if isinstance(n, ast.AnnAssign) and isinstance(n.target, ast.Name) and _secret(n.annotation)] - return WebsiteSource(live=live.value if isinstance(live, ast.Constant) and isinstance(live.value, bool) else None, - domains=_names(given.get("domains")), allow_hosts=_names(given.get("allow_hosts")), - secret_settings=secrets) + return WebsiteSource( + live=(live.value if isinstance(live, ast.Constant) and isinstance(live.value, bool) else None) if live else ( + None if unpacked else False), + domains=_names(given.get("domains")), + allow_hosts=_names(given["allow_hosts"]) if "allow_hosts" in given else None if unpacked else [], + secret_settings=secrets) def _names(node: Optional[ast.expr]) -> Optional[list[str]]: diff --git a/tst/integration/computer/browser_computer_test.py b/tst/integration/computer/browser_computer_test.py index b093b2fec..eeca7dba5 100644 --- a/tst/integration/computer/browser_computer_test.py +++ b/tst/integration/computer/browser_computer_test.py @@ -622,6 +622,33 @@ async def test_routing_sites_it_goes_out_only_through_the_forward_port_which_the assert not report.failed +@pytest.mark.asyncio +async def test_routing_sites_beside_live_ones_its_forward_port_takes_the_live_hosts_straight_to_the_real_site( + browser_computer): + live = ("github.com", "*.githubassets.com") + with host_pages() as port, namespace_routing(): + params = {"site_routing": True, + "sites": [{"name": "shop", "url": f"http://127.0.0.1:{port}/website/shop/", "domains": ["shop.test"]}], + "app_settings": {"chromium": {"start_urls": ["https://github.com/"]}}} + deployed = await Env.get(browser_computer.id).deploy(env_params=params, live_hosts=live) + context = TaskStepContext(deployed_envs=[deployed]) + try: + pages = {url: await _through_port(deployed, url) for url in ( + "https://github.com/", "http://github.com/", "https://github.githubassets.com/favicons/favicon.svg", + "https://shop.test/alpha", "https://example.com/")} + tabs = await tabs_of(deployed) + finally: + report = await teardown_run(context) + + assert deployed.metadata["computer"]["live_hosts"] == list(live) + assert all(pages[url].startswith("200 ") for url in ( + "https://github.com/", "http://github.com/", "https://github.githubassets.com/favicons/favicon.svg", + "https://shop.test/alpha")), pages + assert pages["https://example.com/"].startswith("error") and "403" in pages["https://example.com/"], pages + assert any(tab["url"].startswith("https://github.com/") and "GitHub" in (tab.get("title") or "") for tab in tabs), tabs + assert not report.failed + + async def _kill(deployed, pattern: str, oldest: bool = False) -> None: """SIGKILL the newest, or oldest, process matching ``pattern``, a second after computer-exec has answered, since the driver that runs the command may be the one killed.""" diff --git a/tst/unit/env/envs/test_computer_env.py b/tst/unit/env/envs/test_computer_env.py index 2e1baafd5..f202c482a 100644 --- a/tst/unit/env/envs/test_computer_env.py +++ b/tst/unit/env/envs/test_computer_env.py @@ -1047,7 +1047,7 @@ async def test_a_computer_with_site_routing_reaches_the_websites_the_run_deploye assert [site.model_dump() for site in sites] == [{"name": "notes", "url": "http://localhost:8080/website/notes/", "domains": ["notes.test"]}] assert options == {"user": "user", "network": None, "valid_for": timedelta(seconds=3600), "label": "desktop", - "os_type": "linux", "display": "x11"} + "os_type": "linux", "display": "x11", "live_hosts": ()} assert boots.boots[0]["environment"][HOLD_BOOT_LAUNCH_ENV] == "1" record = context.deployed_envs[-1].metadata["computer"] assert record["sites"] == {"names": {"routes": {"notes": ["notes.test"]}}, "ca": "CA PEM", "valid_for_s": 3600} diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index 0584d1ff4..a6e28d6b8 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -392,6 +392,74 @@ async def test_the_forward_port_refuses_any_other_host_at_once_and_logs_it(tmp_p assert f"site proxy: refused {method} {named}: no site of this computer" in capsys.readouterr().err +@contextlib.contextmanager +def _live_hosts_at(monkeypatch, upstream_port): + """Connections the proxy opens to a live site's host landing on ``upstream_port`` on loopback, and each host it + opened one to recorded.""" + opened, real = [], asyncio.open_connection + + async def open_connection(host, port, **kwargs): + if host == "127.0.0.1": + return await real(host, port, **kwargs) + opened.append((host, port)) + return await real("127.0.0.1", upstream_port, **kwargs) + + monkeypatch.setattr(site_proxy.asyncio, "open_connection", open_connection) + yield opened + + +@pytest.mark.asyncio +async def test_the_forward_port_tunnels_a_live_sites_host_straight_to_it_and_sends_its_url_on_as_a_path( + tmp_path, monkeypatch): + async with Upstream() as upstream, Upstream() as live: + config, _ = _config(tmp_path, upstream.port) + config.direct = ["github.com", "*.githubassets.com"] + with _live_hosts_at(monkeypatch, live.port) as opened: + async with _forward_port(config) as port: + reader, writer = await asyncio.open_connection("127.0.0.1", port) + writer.write(b"CONNECT github.githubassets.com:8443 HTTP/1.1\r\nHost: github.githubassets.com\r\n\r\n") + established = await reader.readuntil(b"\r\n\r\n") + tunnelled = await _exchange(port, b"GET /x HTTP/1.1\r\nHost: github.githubassets.com\r\n\r\n", + reader_writer=(reader, writer)) + writer.close() + plain = await _exchange(port, b"GET http://github.com/a?b=1 HTTP/1.1\r\nHost: github.com\r\n" + b"Proxy-Connection: keep-alive\r\n\r\n") + plain[3][1].close() + + assert established.startswith(b"HTTP/1.1 200 ") and opened == [("github.githubassets.com", 8443), ("github.com", 80)] + assert (tunnelled[0], tunnelled[2], plain[0], plain[2]) == ("HTTP/1.1 200 OK", b"hello", "HTTP/1.1 200 OK", b"hello") + assert [request[0] for request in live.requests] == ["GET /x HTTP/1.1", "GET /a?b=1 HTTP/1.1"] + assert live.requests[1][1]["connection"] == "close" and "proxy-connection" not in live.requests[1][1] + assert upstream.requests == [] + + +@pytest.mark.asyncio +async def test_a_sites_name_within_a_live_hosts_wildcard_is_the_sites_and_no_address_is_ever_taken_straight( + tmp_path, monkeypatch, capsys): + async with Upstream() as upstream, Upstream() as live: + config, issued = _config(tmp_path, upstream.port, gist=["gist.github.com"]) + config.direct = ["*.github.com", "127.0.0.1", "github.localhost"] + with _live_hosts_at(monkeypatch, live.port) as opened: + async with _forward_port(config) as port: + reader, writer = await asyncio.open_connection("127.0.0.1", port) + writer.write(b"CONNECT gist.github.com:443 HTTP/1.1\r\nHost: gist.github.com:443\r\n\r\n") + await reader.readuntil(b"\r\n\r\n") + await writer.start_tls(ssl.create_default_context(cadata=issued.ca_pem), + server_hostname="gist.github.com") + site = await _exchange(port, b"GET / HTTP/1.1\r\nHost: gist.github.com\r\n\r\n", + reader_writer=(reader, writer)) + writer.close() + refused = [await _exchange(port, line + b"\r\n\r\n") for line in ( + b"CONNECT 127.0.0.1:18765 HTTP/1.1\r\nHost: 127.0.0.1:18765", + b"GET http://github.localhost/ HTTP/1.1\r\nHost: github.localhost")] + + assert site[0] == "HTTP/1.1 200 OK" and upstream.requests[0][0] == "GET /website/gist/ HTTP/1.1" + assert [answer[0] for answer in refused] == ["HTTP/1.1 403 Forbidden"] * 2 + assert opened == [] and live.requests == [] + assert [Config(user=None, sites=[], direct=["*.github.com", "10.0.0.1"]).is_direct(host) for host in ( + "api.github.com", "github.com", "a.b.github.com", "10.0.0.1")] == [True, False, False, False] + + @pytest.mark.asyncio async def test_the_forward_port_streams_an_event_stream_as_it_comes(tmp_path): release = asyncio.Event() @@ -728,7 +796,7 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on SiteParams(name="bare", url="http://gw/website/bare/")]) issued = sites.issue(table.routes, valid_for=timedelta(hours=1), label="t") (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None, os_type="linux", - forward_port=True))) + forward_port=True, direct=["github.com"]))) started = [] monkeypatch.setattr(site_proxy, "start", lambda path, log: started.append(Config.load(path))) monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {"sites": {"notes.test": {"status": 200}}, @@ -741,7 +809,8 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on assert [(s.name, s.domains, bool(s.cert)) for s in config.sites] == [ ("notes", ["notes.test"], True), ("chat", ["*.chat.test"], True), ("bare", ["bare.localhost"], False)] assert (config.dns_port, config.nameservers) == (53, ["127.0.0.11"]) - assert config.proxy_port == sites.FORWARD_PROXY_PORT == 3129 + assert (config.proxy_port, config.direct) == (sites.FORWARD_PROXY_PORT, ["github.com"]) + assert sites.FORWARD_PROXY_PORT == 3129 assert [(folder / name).stat().st_mode & 0o777 for name in ("notes.key", "config.json")] == [0o600, 0o600] assert folder.stat().st_mode & 0o777 == 0o755 assert "key_pem" not in (folder / "serve.json").read_text() @@ -1075,5 +1144,5 @@ def test_the_proxy_is_standard_library_alone_so_the_bases_own_python_runs_it(): imported = {line.split()[1].split(".")[0] for line in source.splitlines() if line.startswith(("import ", "from ")) and not line.startswith("from __future__")} - assert imported <= {"asyncio", "base64", "hashlib", "json", "os", "pwd", "re", "shutil", "socket", "ssl", "struct", - "subprocess", "sys", "time", "urllib", "dataclasses", "pathlib", "typing"} + assert imported <= {"asyncio", "base64", "hashlib", "ipaddress", "json", "os", "pwd", "re", "shutil", "socket", "ssl", + "struct", "subprocess", "sys", "time", "urllib", "dataclasses", "pathlib", "typing"} diff --git a/tst/unit/env/envs/test_computer_sites.py b/tst/unit/env/envs/test_computer_sites.py index 7222ed005..5815c8c71 100644 --- a/tst/unit/env/envs/test_computer_sites.py +++ b/tst/unit/env/envs/test_computer_sites.py @@ -216,6 +216,24 @@ async def test_route_gives_a_browser_alone_the_forward_port_and_a_computer_with_ assert [config.get("proxy_port") for config in configs] == [3129, None] +@pytest.mark.asyncio +async def test_a_browser_alones_forward_port_takes_the_live_sites_hosts_straight_to_them_with_no_site_to_route(): + routed, alone, desktop = FakeVm(_REPORT), FakeVm(_REPORT), FakeVm(_REPORT) + live = ("github.com", "*.githubassets.com") + + await sites.route(routed, [_site("notes", "notes.test")], user="user", network=None, valid_for=timedelta(hours=1), + label="browser", os_type="linux", display="none", live_hosts=live) + for sandbox, display in ((alone, "none"), (desktop, "x11")): + await sites.route(sandbox, [], user="user", network=None, valid_for=timedelta(hours=1), label="browser", + os_type="linux", display=display, live_hosts=live) + + beside = json.loads(routed.written["/etc/agentenv/sites/config.json"]) + only = json.loads(alone.written["/etc/agentenv/sites/config.json"]) + assert (beside["proxy_port"], beside["direct"]) == (3129, list(live)) + assert (only["sites"], only["ca_pem"], only["proxy_port"], only["direct"]) == ([], None, 3129, list(live)) + assert desktop.written == {} and desktop.scripts == [] + + @pytest.mark.asyncio async def test_the_config_holding_the_keys_is_written_over_stdin_into_a_private_staging_folder_never_as_text(): sandbox = FakeVm(_REPORT) diff --git a/tst/unit/env/envs/test_website_live.py b/tst/unit/env/envs/test_website_live.py index 2bd32e059..ce132eff7 100644 --- a/tst/unit/env/envs/test_website_live.py +++ b/tst/unit/env/envs/test_website_live.py @@ -27,7 +27,8 @@ from agent_env.providers.env_providers.constants import GATEWAY_SERVICE_NAME from agent_env.providers.env_providers.env_computer_provider import _reaching from agent_env.providers.env_providers.env_gateway_provider import (EnvironmentGatewayProvider, WebsiteConfig, - _redact_compose_secrets, website_config) + _frontend_urls, _redact_compose_secrets, + website_config) from agent_env.providers.env_state import LocalPostgresStateProvider from agent_env.providers.sandbox_providers.sandbox import NetworkPolicy from agent_env.utils import card_naming @@ -81,9 +82,13 @@ def test_a_live_website_env_keeps_what_it_is_in_its_record_and_its_env_toml(monk ({"live": False}, "allow_hosts are the other hosts a live site's pages reach; set live = true"), ({"allow_hosts": ["https://github.com"]}, "allow_hosts:"), ({"settings": {"token": "ghp_plain"}}, "settings.token is a secret, so it's a secret:KEY or env:NAME reference"), + ({"settings": {"token": "env:GH_TOKEN?ghp_plain"}}, "settings.token is a secret, so it's a secret:KEY or env:NAME " + "reference with no ?default"), + ({"settings": {"token": "secret:"}}, "settings.token is a secret"), ({"settings": {"token": "env:T", "orgs": ["a"]}}, "settings.orgs is a string, number or boolean"), ({"settings": {"token": "env:T", "1org": "a"}}, "settings: '1org' isn't a setting's name"), -], ids=["no-domains", "allow-hosts-not-live", "bad-allow-host", "plain-secret", "list-setting", "bad-name"]) +], ids=["no-domains", "allow-hosts-not-live", "bad-allow-host", "plain-secret", "fallback-secret", "nameless-secret", + "list-setting", "bad-name"]) def test_a_website_env_refuses_what_a_live_site_cant_be(overrides, problem): with pytest.raises(ValueError, match=re.escape(problem)): _live(**overrides) @@ -113,7 +118,22 @@ def test_a_synthetic_sites_names_stay_as_given_and_a_source_with_two_sites_or_no (tmp_path / "b.py").write_text(other) assert card_naming.website_source_in_files({"a.py": tmp_path / "a.py", "b.py": tmp_path / "b.py"}, "Dockerfile") is None (tmp_path / "c.py").write_text("@website(domains=DOMAINS, live=LIVE)\nclass Site(WebsiteEnvironment):\n pass\n") - assert card_naming.website_source_in_files({"c.py": tmp_path / "c.py"}, "Dockerfile") == WebsiteSource() + assert card_naming.website_source_in_files({"c.py": tmp_path / "c.py"}, "Dockerfile") == WebsiteSource( + allow_hosts=[]) + (tmp_path / "d.py").write_text("@website(**OPTIONS)\nclass Site(WebsiteEnvironment):\n pass\n") + assert card_naming.website_source_in_files({"d.py": tmp_path / "d.py"}, "Dockerfile") == WebsiteSource() + + +def test_a_source_leaving_live_and_allow_hosts_out_declares_their_defaults_so_a_flag_cant_contradict_it(tmp_path): + (tmp_path / "Dockerfile").write_text("FROM scratch\n") + (tmp_path / "site.py").write_text('@website(domains=["shop.test"])\nclass Shop(WebsiteEnvironment):\n pass\n') + + source = card_naming.website_source_from_source(str(tmp_path / "Dockerfile"), None) + _, problems = declared_live(source, live=True, allow_hosts=["api.shop.test"]) + + assert source == WebsiteSource(live=False, domains=["shop.test"], allow_hosts=[]) + assert problems == ["live is True here, but the site's @website(...) declares live=False", + "allow_hosts is ['api.shop.test'] here, but the site's @website(...) declares allow_hosts=[]"] def test_an_env_toml_takes_what_the_source_declares_and_refuses_what_it_contradicts(): @@ -136,7 +156,7 @@ def refuse(problems): def test_a_live_site_gets_no_site_route_and_its_settings_resolved_only_in_its_container(monkeypatch): - monkeypatch.setenv("GH_TOKEN", "ghp_resolved") + monkeypatch.setenv("GH_TOKEN", "ghp_re$$olved${HOME}") compose = EnvironmentGatewayProvider().create_docker_compose( mcp_servers=[], gateway_image="agent-gateway", @@ -150,9 +170,14 @@ def test_a_live_site_gets_no_site_route_and_its_settings_resolved_only_in_its_co assert gateway["WEBSITE_URLS"] == "shop=http://shop-website-backend:8000" assert "github=http://github-website-backend:8000" in gateway["REST_PROXY_URLS"].split(",") handed = dict(item.split("=", 1) for item in services["github-website-backend"]["environment"]) - assert json.loads(handed[WEBSITE_SETTINGS_ENV]) == {"token": "ghp_resolved", "org": "octo"} + # Compose reads $$ as a $ and interpolates no other. + assert json.loads(handed[WEBSITE_SETTINGS_ENV].replace("$$", "$")) == {"token": "ghp_re$$olved${HOME}", "org": "octo"} + assert "$" not in handed[WEBSITE_SETTINGS_ENV].replace("$$", "") assert WEBSITE_SETTINGS_ENV not in dict(item.split("=", 1) for item in services["shop-website-backend"]["environment"]) - assert "ghp_resolved" not in _redact_compose_secrets(compose) + assert "olved" not in _redact_compose_secrets(compose) + assert (_frontend_urls("https://gw", [website_config(_live())]), + _frontend_urls("https://gw", [website_config(_live()), website_config(_synthetic())])) == ( + None, {"shop": "https://gw/website/shop/"}) @pytest.mark.asyncio @@ -215,6 +240,11 @@ def entry(urls=None, routing=True): [problem] = start_tab_problems(computer, entry(["https://example.com/"]), names) assert "chromium's start tab https://example.com/ names example.com, which none of the multi env's" in problem assert start_tab_problems(computer, entry(["https://example.com/"], routing=False), names) == [] + # A routed site storing no names takes those its card declares at deploy, which checks the tabs then. + carded = MultiEnv(id="office", version=1, mcp_server_envs=[], website_envs=[_synthetic(domains=()), _live()]) + assert carded._start_tab_names(entry()) is None + assert carded._start_tab_names(entry(routing=["github-site"])) == ["github.com", "*.githubassets.com", + "api.github.com"] def test_a_computers_allowlist_takes_the_live_hosts_and_one_that_allows_everything_stays_as_it_is(): From 9a2525cb6a146781fe3031bae013cb4d6ad342a8 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 15:20:31 -0800 Subject: [PATCH 3/4] fix(website): check a routing computer's start tabs against its sites' card names at deploy The save leaves a computer's start tabs to the deploy while a routed site stores no names, and the deploy checked none, so a tab at a host no website claims opened. Once the gateway is up, before a computer routes its sites, its start tabs are checked against the names their cards declare and the live hosts; one no website claims fails the deploy. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/envs/_multi_computers.py | 16 +++++++++-- tst/unit/env/envs/test_multi_env_computers.py | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/agent_env/env/envs/_multi_computers.py b/src/agent_env/env/envs/_multi_computers.py index c749c479e..d956c9f6d 100644 --- a/src/agent_env/env/envs/_multi_computers.py +++ b/src/agent_env/env/envs/_multi_computers.py @@ -127,6 +127,8 @@ def __init__(self, envs: list[ComputerEnv], owner: str) -> None: self.envs = envs self._owner = owner # the multi env's id, for what's logged self._routes: dict[str, Optional[list[str]]] = {} # each computer routing sites: the names it routes, or all + self._entries: dict = {} # the deploy's child_envs entries, by computer + self._live: tuple[str, ...] = () # the live hosts of the multi env's live websites self._replaced: list[ComputerEnv] = [] self._earlier: Optional[tuple[list[ComputerEnv], list[ComputerEnv]]] = None @@ -170,7 +172,7 @@ def deployer(self, child_envs: Optional[dict], *, sandbox_type: str | None, ttl_ deploys them again, after closing the earlier try's.""" tries = 0 live = tuple(live_hosts) - self._routes = {} + self._routes, self._entries, self._live = {}, dict(child_envs or {}), live for name, entry in (child_envs or {}).items(): if routes_later(entry): routing = entry["env_params"]["site_routing"] @@ -197,11 +199,19 @@ async def deploy(reach: Optional[str]) -> list[ExternalChild]: async def finish(self, deployed: DeployedEnv) -> None: """Once the gateway is up, before ``deployed``, the multi env's record, is registered: each computer that routes - the multi env's websites routes them, those its ``site_routing`` names or all, once they're ready, and launches - its held apps; then each computer's own record is kept in ``deployed`` (``record``).""" + the multi env's websites, those its ``site_routing`` names or all, has its start tabs checked against their + names as the cards declare them and the live hosts (``start_tab_problems``), then routes them once they're + ready and launches its held apps; then each computer's own record is kept in ``deployed`` (``record``).""" if self._routes: # Selected by the name each website is served as, its environment_name, which its card's name needn't be. routed = {name: await sites.deployed_sites([deployed], services=only) for name, only in self._routes.items()} + if problems := [f"computer {env.environment_name!r}: {problem}" for env in self.envs + if env.environment_name in routed for problem in start_tab_problems( + env, self._entries.get(env.environment_name), [ + *(name for site in routed[env.environment_name] + for name in site.get("domains") or [sites.fallback_name(site["name"])]), + *self._live])]: + raise ValueError(f"multi env '{self._owner}': " + "; ".join(problems)) await sites.require_ready([deployed]) await each(self._owner, "route its sites", [(f"computer {env.environment_name!r}", env.route_sites(routed[env.environment_name])) diff --git a/tst/unit/env/envs/test_multi_env_computers.py b/tst/unit/env/envs/test_multi_env_computers.py index 7b3da1199..a70468067 100644 --- a/tst/unit/env/envs/test_multi_env_computers.py +++ b/tst/unit/env/envs/test_multi_env_computers.py @@ -642,6 +642,34 @@ async def test_a_computer_routing_sites_boots_held_and_routes_the_multi_envs_web assert set(deployed.child_envs) == {"desktop", "laptop", "kiosk"} +@pytest.mark.asyncio +async def test_a_routing_computers_start_tabs_are_checked_against_the_names_its_sites_cards_declare_before_it_routes( + gateway, providers, monkeypatch): + monkeypatch.setattr(_multi_computers.sites, "deployed_sites", AsyncMock(return_value=[ + {"name": "shop", "url": "u", "domains": ["shop.test"]}, {"name": "blog", "url": "u"}])) + monkeypatch.setattr(_multi_computers.sites, "require_ready", AsyncMock()) + + def office(urls): + desktop = FakeComputer("desktop", log=gateway.log, apps=("browser",)) + desktop.composed_apps = lambda: [SimpleNamespace(environment_name="browser", + declaration=SimpleNamespace(browser=True))] + websites = [MagicMock(id=f"{name}-site", environment_name=name, domains=[], live=False, allow_hosts=[]) + for name in ("shop", "blog")] # names the save can't know: their cards declare them + return MultiEnv(id="office", version=1, mcp_server_envs=[], website_envs=websites, computer_envs=[desktop]), { + "desktop": {"env_params": {"site_routing": True, "app_settings": {"browser": {"start_urls": urls}}}}} + + env, child_envs = office(["https://shop.test/", "http://blog.localhost/"]) + await env.deploy(child_envs=child_envs) + routed = gateway.log[-1] + env, child_envs = office(["https://example.com/"]) + with pytest.raises(ValueError, match=re.escape("multi env 'office': computer 'desktop': browser's start tab " + "https://example.com/ names example.com, which none of the multi")): + await env.deploy(child_envs=child_envs) + + assert routed == ("routed", "desktop", ["shop", "blog"]) + assert gateway.log[-2:] == [("close", "desktop"), ("close", "gateway")] + + @pytest.mark.asyncio async def test_a_computer_that_fails_to_route_its_sites_fails_the_deploy_and_closes_everything(gateway, providers, monkeypatch): From 1ef4cd036c3fc192601762f193411b8df8490553 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 16:23:53 -0800 Subject: [PATCH 4/4] fix(website): a Settings field annotated "SecretStr" as a string is a secret setting too The source reader matched SecretStr only as a name, so a quoted annotation, "SecretStr" or Optional["SecretStr"], left the field out of secret_settings and its value could be saved as given. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/utils/card_naming.py | 15 ++++++++++++--- tst/unit/env/envs/test_website_live.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/agent_env/utils/card_naming.py b/src/agent_env/utils/card_naming.py index c319c1e40..225d83c76 100644 --- a/src/agent_env/utils/card_naming.py +++ b/src/agent_env/utils/card_naming.py @@ -194,9 +194,18 @@ def _names(node: Optional[ast.expr]) -> Optional[list[str]]: def _secret(annotation: ast.expr) -> bool: - """Whether ``annotation`` is ``SecretStr``, optional or not.""" - return any(isinstance(n, ast.Name) and n.id == "SecretStr" or isinstance(n, ast.Attribute) and n.attr == "SecretStr" - for n in ast.walk(annotation)) + """Whether ``annotation`` is ``SecretStr``, optional or not, written as a name or as a string (``"SecretStr"``, + ``Optional["SecretStr"]``).""" + for n in ast.walk(annotation): + if isinstance(n, ast.Name) and n.id == "SecretStr" or isinstance(n, ast.Attribute) and n.attr == "SecretStr": + return True + if isinstance(n, ast.Constant) and isinstance(n.value, str): + try: + if _secret(ast.parse(n.value, mode="eval").body): + return True + except SyntaxError: + continue + return False def _decorator_name(decorator: ast.expr) -> Optional[str]: diff --git a/tst/unit/env/envs/test_website_live.py b/tst/unit/env/envs/test_website_live.py index ce132eff7..ad6bc059e 100644 --- a/tst/unit/env/envs/test_website_live.py +++ b/tst/unit/env/envs/test_website_live.py @@ -124,6 +124,19 @@ def test_a_synthetic_sites_names_stay_as_given_and_a_source_with_two_sites_or_no assert card_naming.website_source_in_files({"d.py": tmp_path / "d.py"}, "Dockerfile") == WebsiteSource() +def test_a_secret_setting_annotated_as_a_string_is_a_secret_too(tmp_path): + (tmp_path / "Dockerfile").write_text("FROM scratch\n") + (tmp_path / "site.py").write_text(SOURCE.replace(" token: SecretStr\n", ' token: "SecretStr"\n' + ' key: Optional["SecretStr"] = None\n' + ' note: "str" = ""\n')) + + source = card_naming.website_source_from_source(str(tmp_path / "Dockerfile"), None) + + assert source.secret_settings == ["token", "key"] + with pytest.raises(ValueError, match="settings.token is a secret"): + _live(settings={"token": "ghp_plain"}, secret_settings=source.secret_settings) + + def test_a_source_leaving_live_and_allow_hosts_out_declares_their_defaults_so_a_flag_cant_contradict_it(tmp_path): (tmp_path / "Dockerfile").write_text("FROM scratch\n") (tmp_path / "site.py").write_text('@website(domains=["shop.test"])\nclass Shop(WebsiteEnvironment):\n pass\n')