Metadata-Version: 2.5
Name: openenv-kosa8
Version: 0.1.0
Summary: kosa8 Cloud microVM sandboxes as an OpenEnv container provider: run OpenEnv environment servers for RL
Project-URL: Homepage, https://kosa8.com
Project-URL: Documentation, https://github.com/kosa8ai/openenv-kosa8#readme
Project-URL: Repository, https://github.com/kosa8ai/openenv-kosa8
Project-URL: Issues, https://github.com/kosa8ai/openenv-kosa8/issues
Author: kosa8
License: MIT
License-File: LICENSE
License-File: NOTICE
Keywords: kosa8,microvm,openenv,reinforcement-learning,rl,sandbox,trl
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <4.0,>=3.10
Requires-Dist: kosa8<1,>=0.12.0
Requires-Dist: openenv>=0.15.0
Description-Content-Type: text/markdown

# openenv-kosa8

**[kosa8](https://kosa8.com): Zero Trust cybersecurity infrastructure for AI agents.** Every agent sealed in its own microVM. Every action verified and recorded.

Run [OpenEnv](https://github.com/openenv/OpenEnv) environment servers (the RL environment standard used by Hugging Face TRL) in [kosa8 Cloud](https://kosa8.com) microVM sandboxes.

`Kosa8Provider` is an OpenEnv `ContainerProvider`, like the Docker, Modal, Daytona and Hugging Face providers. It boots an environment image as a kosa8 sandbox, starts the server and gives `EnvClient` a `base_url` that carries HTTP and the `/ws` WebSocket. Tested against `openenv` 0.15.

## Install

```bash
pip install openenv-kosa8
```

Set `KOSA8_API_KEY` to a kosa8 Cloud API key (create one at [app.kosa8.com](https://app.kosa8.com), or run `kosa8 login`). The Starter plan is free, no card needed; plans are at [kosa8.com/pricing](https://kosa8.com/pricing/).

## Example

```python
from echo_env import EchoEnv  # or any OpenEnv client
from openenv_kosa8 import Kosa8Provider

provider = Kosa8Provider(image="registry.hf.space/openenv-echo-env:latest")
base_url = provider.start_container()
provider.wait_for_ready(base_url)

# The client stops the provider, deleting the sandbox, when the block ends.
with EchoEnv(base_url=base_url, provider=provider).sync() as env:
    env.reset()
    print(env.call_tool("echo_message", message="Hello!"))
```

Clients that do not need a `base_url` up front (non-MCP clients such as `CodingEnv`) can also take the provider alone: `CodingEnv(provider=provider)` starts it on connect.

For many rollouts, start one provider per environment and run them concurrently (see OpenEnv's [runtime providers guide](https://github.com/openenv/OpenEnv/blob/main/docs/source/guides/runtime-providers.md)).

## How it works

- **Source.** `image` is an OCI registry image, which kosa8 Cloud pulls and boots as the sandbox's root filesystem. Images built by `openenv build`, and Hugging Face Space images such as `registry.hf.space/openenv-echo-env:latest`, work as they are. The image's `CMD` is not run: the provider builds the server command from `openenv.yaml` (`/app/env/openenv.yaml`, or the first one under `/app`), or takes `cmd=`. Build and push Dockerfiles first.
- **Ingress.** The server port (8000 unless `port=` says otherwise) is opened as a kosa8 preview: an `https://` URL that proxies the `/ws` upgrade. A preview is private by default and needs a token header that `EnvClient` does not send, so `start_container()` returns a loopback URL (`http://127.0.0.1:<port>`) whose proxy adds the token and forwards HTTP and WebSocket traffic over `https`/`wss`. `public=True` opens a public preview and returns its `https://` URL; anyone who holds that URL can drive the environment.
- **Lifetime.** Each sandbox gets `ttl_s` (default one hour); kosa8 deletes it when that runs out, and the preview token expires at the same time (seven days at most). The sandbox never auto-sleeps, since a sleeping sandbox would drop a live `/ws` mid-episode. `stop_container()`, `close()` and the client's `with` block delete it, also after a failed start. Each sandbox carries the label `openenv=1`.
- **Security.** The API key stays in your process and never enters the sandbox. Preview tokens and public URLs are never put in errors or logs, and server output is withheld from errors unless `surface_server_logs=True` (then redacted and bounded). Cloud metadata endpoints are not reachable from kosa8 sandboxes; `egress={"internet": False}` denies all other outbound traffic too.

## Options

`Kosa8Provider(**options)`:

| Option | Default | |
|---|---|---|
| `image` | none | OCI image, if not given to `start_container()` |
| `env_vars` | none | environment variables for the server (never logged or returned by kosa8) |
| `api_key` | `KOSA8_API_KEY`, then the key `kosa8 login` saved | kosa8 Cloud API key |
| `cloud_url` | `https://api.kosa8.com` | kosa8 Cloud API address |
| `size` | `"1x2"` | vCPU x GiB: `0.25x0.5`, `0.5x1`, `1x2`, `2x4`, `4x8`, `8x16` or `16x32` |
| `ttl_s` | `3600` | sandbox lifetime, 60 s to 90 days |
| `egress` | the organization's policy | network policy at boot, e.g. `{"internet": False, "allow": ["pypi.org"]}` |
| `labels` | none | extra sandbox labels |
| `cmd` | from `openenv.yaml` | shell command that starts the server |
| `public` | `False` | public preview, returned directly as `https://` |
| `surface_server_logs` | `False` | include redacted server output in startup errors |

`start_container(image=None, port=None, env_vars=None, cmd=None, **create_kwargs)` passes any other keyword (`region`, `disk_gb`, `secrets`, ...) to `kosa8.Cloud().sandboxes.create`. `provider.sandbox_id` is the active sandbox's id.

## Tests

```bash
uv run --group test pytest                                    # unit tests, against fakes
KOSA8_API_KEY=... uv run --group test pytest tests/test_live.py   # live tier, on kosa8 Cloud
```

The unit tests cover sandbox creation and its arguments, server command discovery and quoting, private and public previews, the loopback proxy, readiness and crash reporting (with redaction), cleanup on every failure path, and the exact `kosa8` SDK methods and keyword arguments the provider calls (checked against the installed SDK). The live tier runs OpenEnv's echo environment on kosa8 Cloud with a private and a public preview: `/ws` upgrade `101` (and `401` without the token), `reset()` and `step()`, metadata and default-deny egress checks, and confirms the sandbox is deleted.

## License

MIT (see [NOTICE](NOTICE) for code adapted from OpenEnv under BSD-3-Clause).
