Metadata-Version: 2.5
Name: crewai-kosa8
Version: 0.1.0
Summary: kosa8 Cloud microVM sandboxes as CrewAI tools: shell exec, Python and files
Project-URL: Homepage, https://kosa8.com
Project-URL: Documentation, https://github.com/kosa8ai/crewai-kosa8#readme
Project-URL: Repository, https://github.com/kosa8ai/crewai-kosa8
Project-URL: Issues, https://github.com/kosa8ai/crewai-kosa8/issues
Author: kosa8
License: MIT
License-File: LICENSE
Keywords: agents,code-interpreter,crewai,crewai-tools,kosa8,microvm,sandbox
Classifier: Intended Audience :: Developers
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: <3.14,>=3.10
Requires-Dist: crewai>=1.0
Requires-Dist: kosa8<1,>=0.12.0
Description-Content-Type: text/markdown

# crewai-kosa8

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

Give a [CrewAI](https://www.crewai.com/) agent a [kosa8 Cloud](https://kosa8.com) microVM sandbox to run shell commands, Python and file edits in, instead of your machine.

Three tools, shaped like the E2B and Daytona sandbox tools in `crewai-tools`, so you can swap one for another:

- **`Kosa8ExecTool`** (`kosa8 Sandbox Exec`): run a shell command, get the exit code, stdout and stderr.
- **`Kosa8PythonTool`** (`kosa8 Sandbox Python`): run Python in a stateful kernel, get stdout, stderr, the last expression's value and any error.
- **`Kosa8FileTool`** (`kosa8 Sandbox Files`): read, write, append, list, delete, mkdir, info, exists, move, find (content grep), search (name glob), chmod and replace.

## Install

```bash
pip install crewai-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 and needs no card; plans are at [kosa8.com/pricing](https://kosa8.com/pricing/).

```bash
export KOSA8_API_KEY="k8c_live_..."
```

`KOSA8_CLOUD_URL` overrides the API address (default `https://api.kosa8.com`).

## Example

```python
import kosa8
from crewai import Agent, Crew, Task
from crewai_kosa8 import Kosa8ExecTool, Kosa8FileTool, Kosa8PythonTool

# One sandbox for the whole crew; `with` deletes it on the way out, error or not.
with kosa8.Cloud().sandboxes.create(template="python", persistent=True, ttl_s=3600) as box:
    coder = Agent(
        role="Engineer",
        goal="Write and test small programs",
        backstory="You work only inside the kosa8 sandbox.",
        tools=[Kosa8ExecTool(sandbox_id=box.id), Kosa8FileTool(sandbox_id=box.id), Kosa8PythonTool(sandbox_id=box.id)],
    )
    task = Task(
        description="Write /workspace/fizzbuzz.py, run it for 1..15 and report the output.",
        expected_output="The program's output",
        agent=coder,
    )
    print(Crew(agents=[coder], tasks=[task]).kickoff())
```

Each tool can also be called directly:

```python
from crewai_kosa8 import Kosa8PythonTool

print(Kosa8PythonTool().run(code="print(sum(range(10)))"))
```

## Sandbox lifecycle

All three tools share the same lifecycle controls from `Kosa8BaseTool`:

| Mode | When the sandbox is created | When it is deleted |
| --- | --- | --- |
| **Ephemeral** (default, `persistent=False`) | On every call | At the end of that same call |
| **Persistent** (`persistent=True`) | Lazily on first use | At process exit (via `atexit`), or on `tool.close()` |
| **Attach** (`sandbox_id="sbx_..."`) | Never: the tool attaches to an existing sandbox | Never: the tool will not delete a sandbox it did not create |

Every sandbox a tool creates also gets a lifetime (`ttl_s`, default 3600 s): kosa8 Cloud deletes it then even if the process dies before cleanup runs.

Each persistent tool keeps its own sandbox. To share one between tools, attach the others to the first one's id:

```python
exec_tool = Kosa8ExecTool(persistent=True)
exec_tool.run(command="pip install httpx")
files = Kosa8FileTool(sandbox_id=exec_tool.active_sandbox_id)
```

## Options

Every tool takes these:

| Option | Default | |
| --- | --- | --- |
| `api_key` | `KOSA8_API_KEY`, then the key `kosa8 login` saved | kosa8 Cloud API key |
| `base_url` | `KOSA8_CLOUD_URL`, then `https://api.kosa8.com` | API address |
| `persistent` | `False` | reuse one sandbox for every call of this tool |
| `sandbox_id` | none | attach to an existing sandbox |
| `template` | `"python"` | kosa8 template for new sandboxes |
| `ttl_s` | `3600` | lifetime of new sandboxes, in seconds |
| `labels` | none | labels on new sandboxes, merged over `{"integration": "crewai"}` |
| `create_params` | none | anything else for `cloud.sandboxes.create`: `size` (`0.25x0.5` to `16x32`), `image`, `region`, `env`, `egress` (for example `{"internet": False}`), `secrets`, `persistent`, ... |
| `workdir` | `/workspace` | where `Kosa8ExecTool` commands start when no `cwd` is given |

## How it maps to kosa8

- `Kosa8ExecTool` runs `cd <cwd> && <command>` with kosa8's buffered exec. A buffered command runs at most 90 s; a longer one comes back as `{"timed_out": true, ...}`. Each stream keeps its first and last 1 MiB (`"truncated": true` when the middle was dropped).
- `Kosa8PythonTool` uses kosa8's code kernel (`sandbox.run_code`), so variables persist per `context` within a sandbox. The kernel needs kosa8's long-lived container, so sandboxes this tool creates are made with `persistent=True`. Attached to a sandbox without one, it runs the code as a `python3` script instead. Images and other rich results are reported by type, not returned as bytes.
- `Kosa8FileTool` uses the kosa8 files API for read, write, append, list, delete, mkdir, info, exists and replace, and a shell command for move, find, search and chmod.
- Only `/workspace` is promised to persist between commands unless the sandbox was created with `create_params={"persistent": True}`.
- kosa8 Cloud errors (for example a payment or plan limit, with its `upgrade_url`) are raised as the SDK's typed errors, and CrewAI shows them to the agent.

## Tests

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

The live tier (skipped without `KOSA8_API_KEY`) creates sandboxes with a 600 s lifetime and a `crewai-kosa8-test` label, runs every tool and a CrewAI `Agent` driven by a scripted LLM (no model provider is called), and checks that no sandbox it made is left behind.

## License

MIT
